From 7679d12888f172d0abcb2caa85c297e25f533eca Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 20 Aug 2016 13:52:01 +0300 Subject: [PATCH] Replace NodeModifier by ElementModifier --- .../main/java/org/teavm/ast/ClassNode.java | 9 ++---- .../main/java/org/teavm/ast/FieldNode.java | 9 ++---- .../main/java/org/teavm/ast/MethodNode.java | 5 +-- .../main/java/org/teavm/ast/NodeModifier.java | 27 ---------------- .../teavm/ast/decompilation/Decompiler.java | 31 ++++--------------- core/src/main/java/org/teavm/cache/AstIO.java | 12 +++---- .../javascript/NameFrequencyEstimator.java | 7 ++--- .../java/org/teavm/javascript/Renderer.java | 27 ++++++++-------- 8 files changed, 37 insertions(+), 90 deletions(-) delete mode 100644 core/src/main/java/org/teavm/ast/NodeModifier.java diff --git a/core/src/main/java/org/teavm/ast/ClassNode.java b/core/src/main/java/org/teavm/ast/ClassNode.java index 65f2ed466..3bc4048e9 100644 --- a/core/src/main/java/org/teavm/ast/ClassNode.java +++ b/core/src/main/java/org/teavm/ast/ClassNode.java @@ -19,15 +19,12 @@ import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.Set; +import org.teavm.model.ElementModifier; -/** - * - * @author Alexey Andreev - */ public class ClassNode { private String name; private String parentName; - private Set modifiers = EnumSet.noneOf(NodeModifier.class); + private Set modifiers = EnumSet.noneOf(ElementModifier.class); private List fields = new ArrayList<>(); private List methods = new ArrayList<>(); private List interfaces = new ArrayList<>(); @@ -57,7 +54,7 @@ public class ClassNode { return interfaces; } - public Set getModifiers() { + public Set getModifiers() { return modifiers; } } diff --git a/core/src/main/java/org/teavm/ast/FieldNode.java b/core/src/main/java/org/teavm/ast/FieldNode.java index 2d833f478..f02393edd 100644 --- a/core/src/main/java/org/teavm/ast/FieldNode.java +++ b/core/src/main/java/org/teavm/ast/FieldNode.java @@ -17,16 +17,13 @@ package org.teavm.ast; import java.util.EnumSet; import java.util.Set; +import org.teavm.model.ElementModifier; import org.teavm.model.ValueType; -/** - * - * @author Alexey Andreev - */ public class FieldNode { private String name; private ValueType type; - private Set modifiers = EnumSet.noneOf(NodeModifier.class); + private Set modifiers = EnumSet.noneOf(ElementModifier.class); private Object initialValue; public FieldNode(String name, ValueType type) { @@ -38,7 +35,7 @@ public class FieldNode { return name; } - public Set getModifiers() { + public Set getModifiers() { return modifiers; } diff --git a/core/src/main/java/org/teavm/ast/MethodNode.java b/core/src/main/java/org/teavm/ast/MethodNode.java index 3e15ffdb8..98ffa9bba 100644 --- a/core/src/main/java/org/teavm/ast/MethodNode.java +++ b/core/src/main/java/org/teavm/ast/MethodNode.java @@ -18,11 +18,12 @@ package org.teavm.ast; import java.util.EnumSet; import java.util.List; import java.util.Set; +import org.teavm.model.ElementModifier; import org.teavm.model.MethodReference; public abstract class MethodNode { private MethodReference reference; - private Set modifiers = EnumSet.noneOf(NodeModifier.class); + private Set modifiers = EnumSet.noneOf(ElementModifier.class); public MethodNode(MethodReference reference) { this.reference = reference; @@ -33,7 +34,7 @@ public abstract class MethodNode { return reference; } - public Set getModifiers() { + public Set getModifiers() { return modifiers; } diff --git a/core/src/main/java/org/teavm/ast/NodeModifier.java b/core/src/main/java/org/teavm/ast/NodeModifier.java deleted file mode 100644 index 22b342862..000000000 --- a/core/src/main/java/org/teavm/ast/NodeModifier.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2016 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.teavm.ast; - -/** - * - * @author Alexey Andreev - */ -public enum NodeModifier { - STATIC, - INTERFACE, - ENUM, - SYNCHRONIZED -} diff --git a/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java b/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java index 0878aed82..c2b38e6c1 100644 --- a/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java +++ b/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java @@ -21,7 +21,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Deque; -import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -37,7 +36,6 @@ import org.teavm.ast.GotoPartStatement; import org.teavm.ast.IdentifiedStatement; import org.teavm.ast.MethodNode; import org.teavm.ast.NativeMethodNode; -import org.teavm.ast.NodeModifier; import org.teavm.ast.RegularMethodNode; import org.teavm.ast.SequentialStatement; import org.teavm.ast.Statement; @@ -61,10 +59,10 @@ import org.teavm.model.ClassHolderSource; import org.teavm.model.ElementModifier; import org.teavm.model.FieldHolder; import org.teavm.model.Instruction; -import org.teavm.model.TextLocation; import org.teavm.model.MethodHolder; import org.teavm.model.MethodReference; import org.teavm.model.Program; +import org.teavm.model.TextLocation; import org.teavm.model.TryCatchBlock; import org.teavm.model.ValueType; import org.teavm.model.Variable; @@ -193,7 +191,7 @@ public class Decompiler { ClassNode clsNode = new ClassNode(cls.getName(), cls.getParent()); for (FieldHolder field : cls.getFields()) { FieldNode fieldNode = new FieldNode(field.getName(), field.getType()); - fieldNode.getModifiers().addAll(mapModifiers(field.getModifiers())); + fieldNode.getModifiers().addAll(field.getModifiers()); fieldNode.setInitialValue(field.getInitialValue()); clsNode.getFields().add(fieldNode); } @@ -209,7 +207,7 @@ public class Decompiler { clsNode.getMethods().add(methodNode); } clsNode.getInterfaces().addAll(cls.getInterfaces()); - clsNode.getModifiers().addAll(mapModifiers(cls.getModifiers())); + clsNode.getModifiers().addAll(cls.getModifiers()); return clsNode; } @@ -238,7 +236,7 @@ public class Decompiler { } NativeMethodNode methodNode = new NativeMethodNode(new MethodReference(method.getOwnerName(), method.getDescriptor())); - methodNode.getModifiers().addAll(mapModifiers(method.getModifiers())); + methodNode.getModifiers().addAll(method.getModifiers()); methodNode.setGenerator(generator); methodNode.setAsync(asyncMethods.contains(method.getReference())); return methodNode; @@ -278,7 +276,7 @@ public class Decompiler { Optimizer optimizer = new Optimizer(); optimizer.optimize(methodNode, method.getProgram()); - methodNode.getModifiers().addAll(mapModifiers(method.getModifiers())); + methodNode.getModifiers().addAll(method.getModifiers()); int paramCount = Math.min(method.getSignature().length, program.variableCount()); for (int i = 0; i < paramCount; ++i) { Variable var = program.variableAt(i); @@ -345,7 +343,7 @@ public class Decompiler { Optimizer optimizer = new Optimizer(); optimizer.optimize(node, splitter); - node.getModifiers().addAll(mapModifiers(method.getModifiers())); + node.getModifiers().addAll(method.getModifiers()); int paramCount = Math.min(method.getSignature().length, program.variableCount()); for (int i = 0; i < paramCount; ++i) { Variable var = program.variableAt(i); @@ -555,23 +553,6 @@ public class Decompiler { } } - private Set mapModifiers(Set modifiers) { - Set result = EnumSet.noneOf(NodeModifier.class); - if (modifiers.contains(ElementModifier.STATIC)) { - result.add(NodeModifier.STATIC); - } - if (modifiers.contains(ElementModifier.INTERFACE)) { - result.add(NodeModifier.INTERFACE); - } - if (modifiers.contains(ElementModifier.ENUM)) { - result.add(NodeModifier.ENUM); - } - if (modifiers.contains(ElementModifier.SYNCHRONIZED)) { - result.add(NodeModifier.SYNCHRONIZED); - } - return result; - } - private List createBlocks(int start) { List result = new ArrayList<>(); while (currentNode != null && currentNode.getStart() == start) { diff --git a/core/src/main/java/org/teavm/cache/AstIO.java b/core/src/main/java/org/teavm/cache/AstIO.java index db67b352f..996a5306e 100644 --- a/core/src/main/java/org/teavm/cache/AstIO.java +++ b/core/src/main/java/org/teavm/cache/AstIO.java @@ -51,7 +51,6 @@ import org.teavm.ast.MonitorExitStatement; import org.teavm.ast.NewArrayExpr; import org.teavm.ast.NewExpr; import org.teavm.ast.NewMultiArrayExpr; -import org.teavm.ast.NodeModifier; import org.teavm.ast.OperationType; import org.teavm.ast.PrimitiveCastExpr; import org.teavm.ast.QualificationExpr; @@ -71,6 +70,7 @@ import org.teavm.ast.UnwrapArrayExpr; import org.teavm.ast.VariableExpr; import org.teavm.ast.VariableNode; import org.teavm.ast.WhileStatement; +import org.teavm.model.ElementModifier; import org.teavm.model.FieldReference; import org.teavm.model.MethodDescriptor; import org.teavm.model.MethodReference; @@ -80,7 +80,7 @@ import org.teavm.model.instructions.ArrayElementType; import org.teavm.model.util.VariableType; public class AstIO { - private static final NodeModifier[] nodeModifiers = NodeModifier.values(); + private static final ElementModifier[] nodeModifiers = ElementModifier.values(); private static final BinaryOperation[] binaryOperations = BinaryOperation.values(); private static final UnaryOperation[] unaryOperations = UnaryOperation.values(); private static final ArrayElementType[] arrayElementTypes = ArrayElementType.values(); @@ -196,16 +196,16 @@ public class AstIO { } } - private int packModifiers(Set modifiers) { + private int packModifiers(Set modifiers) { int packed = 0; - for (NodeModifier modifier : modifiers) { + for (ElementModifier modifier : modifiers) { packed |= 1 << modifier.ordinal(); } return packed; } - private Set unpackModifiers(int packed) { - EnumSet modifiers = EnumSet.noneOf(NodeModifier.class); + private Set unpackModifiers(int packed) { + EnumSet modifiers = EnumSet.noneOf(ElementModifier.class); while (packed != 0) { int shift = Integer.numberOfTrailingZeros(packed); modifiers.add(nodeModifiers[shift]); diff --git a/core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java b/core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java index 995d5cc53..1ff965eb1 100644 --- a/core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java +++ b/core/src/main/java/org/teavm/javascript/NameFrequencyEstimator.java @@ -44,7 +44,6 @@ import org.teavm.ast.NativeMethodNode; import org.teavm.ast.NewArrayExpr; import org.teavm.ast.NewExpr; import org.teavm.ast.NewMultiArrayExpr; -import org.teavm.ast.NodeModifier; import org.teavm.ast.PrimitiveCastExpr; import org.teavm.ast.QualificationExpr; import org.teavm.ast.RegularMethodNode; @@ -100,7 +99,7 @@ class NameFrequencyEstimator implements StatementVisitor, ExprVisitor, MethodNod } for (FieldNode field : cls.getFields()) { consumer.consume(new FieldReference(cls.getName(), field.getName())); - if (field.getModifiers().contains(NodeModifier.STATIC)) { + if (field.getModifiers().contains(ElementModifier.STATIC)) { consumer.consume(cls.getName()); } } @@ -113,11 +112,11 @@ class NameFrequencyEstimator implements StatementVisitor, ExprVisitor, MethodNod if (asyncFamilyMethods.contains(method.getReference())) { consumer.consume(method.getReference()); } - if (clinit != null && (method.getModifiers().contains(NodeModifier.STATIC) + if (clinit != null && (method.getModifiers().contains(ElementModifier.STATIC) || method.getReference().getName().equals(""))) { consumer.consume(method.getReference()); } - if (!method.getModifiers().contains(NodeModifier.STATIC)) { + if (!method.getModifiers().contains(ElementModifier.STATIC)) { consumer.consume(method.getReference().getDescriptor()); consumer.consume(method.getReference()); } diff --git a/core/src/main/java/org/teavm/javascript/Renderer.java b/core/src/main/java/org/teavm/javascript/Renderer.java index 337d80323..ea7ef265c 100644 --- a/core/src/main/java/org/teavm/javascript/Renderer.java +++ b/core/src/main/java/org/teavm/javascript/Renderer.java @@ -57,7 +57,6 @@ import org.teavm.ast.NativeMethodNode; import org.teavm.ast.NewArrayExpr; import org.teavm.ast.NewExpr; import org.teavm.ast.NewMultiArrayExpr; -import org.teavm.ast.NodeModifier; import org.teavm.ast.OperationType; import org.teavm.ast.PrimitiveCastExpr; import org.teavm.ast.QualificationExpr; @@ -382,7 +381,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext List nonStaticFields = new ArrayList<>(); List staticFields = new ArrayList<>(); for (FieldNode field : cls.getFields()) { - if (field.getModifiers().contains(NodeModifier.STATIC)) { + if (field.getModifiers().contains(ElementModifier.STATIC)) { staticFields.add(field); } else { nonStaticFields.add(field); @@ -443,7 +442,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext boolean needsClinit = clinit != null; List clinitMethods = new ArrayList<>(); for (MethodNode method : cls.getMethods()) { - if (needsClinit && (method.getModifiers().contains(NodeModifier.STATIC) + if (needsClinit && (method.getModifiers().contains(ElementModifier.STATIC) || method.getReference().getName().equals(""))) { clinitMethods.add(method); } else { @@ -463,10 +462,10 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext .append("();").softNewLine(); writer.outdent().append("}").newLine(); } - if (!cls.getModifiers().contains(NodeModifier.INTERFACE)) { + if (!cls.getModifiers().contains(ElementModifier.INTERFACE)) { for (MethodNode method : cls.getMethods()) { cls.getMethods(); - if (!method.getModifiers().contains(NodeModifier.STATIC)) { + if (!method.getModifiers().contains(ElementModifier.STATIC)) { if (method.getReference().getName().equals("")) { renderInitializer(method); } @@ -512,7 +511,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } writer.append("],").ws(); int flags = 0; - if (cls.getModifiers().contains(NodeModifier.ENUM)) { + if (cls.getModifiers().contains(ElementModifier.ENUM)) { flags |= 1; } writer.append(flags).append(',').ws(); @@ -528,11 +527,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext List stubNames = new ArrayList<>(); List virtualMethods = new ArrayList<>(); for (MethodNode method : cls.getMethods()) { - if (clinit != null && (method.getModifiers().contains(NodeModifier.STATIC) + if (clinit != null && (method.getModifiers().contains(ElementModifier.STATIC) || method.getReference().getName().equals(""))) { stubNames.add(naming.getFullNameFor(method.getReference())); } - if (!method.getModifiers().contains(NodeModifier.STATIC)) { + if (!method.getModifiers().contains(ElementModifier.STATIC)) { virtualMethods.add(method); } } @@ -665,7 +664,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.append("function ").append(name).append("("); } int startParam = 0; - if (method.getModifiers().contains(NodeModifier.STATIC)) { + if (method.getModifiers().contains(ElementModifier.STATIC)) { startParam = 1; } for (int i = startParam; i <= ref.parameterCount(); ++i) { @@ -784,7 +783,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } int firstToSave = 0; - if (methodNode.getModifiers().contains(NodeModifier.STATIC)) { + if (methodNode.getModifiers().contains(ElementModifier.STATIC)) { firstToSave = 1; } @@ -804,7 +803,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.softNewLine(); writer.outdent().append("}").softNewLine(); - if (methodNode.getModifiers().contains(NodeModifier.SYNCHRONIZED)) { + if (methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { writer.append("try").ws().append('{').indent().softNewLine(); } writer.append(mainLoopName()).append(":").ws().append("while").ws().append("(true)") @@ -813,7 +812,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext .append('{').softNewLine(); for (int i = 0; i < methodNode.getBody().size(); ++i) { writer.append("case ").append(i).append(":").indent().softNewLine(); - if (i == 0 && methodNode.getModifiers().contains(NodeModifier.SYNCHRONIZED)) { + if (i == 0 && methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { writer.appendMethodBody(new MethodReference(Object.class, "monitorEnter", Object.class, void.class)); writer.append("("); @@ -830,7 +829,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext writer.append("default:").ws().appendFunction("$rt_invalidPointer").append("();").softNewLine(); writer.append("}}").softNewLine(); - if (methodNode.getModifiers().contains(NodeModifier.SYNCHRONIZED)) { + if (methodNode.getModifiers().contains(ElementModifier.SYNCHRONIZED)) { writer.outdent().append("}").ws().append("finally").ws().append('{').indent().softNewLine(); writer.append("if").ws().append("(!").appendFunction("$rt_suspending").append("())") .ws().append("{").indent().softNewLine(); @@ -901,7 +900,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } private void appendMonitor(MethodNode methodNode) throws IOException { - if (methodNode.getModifiers().contains(NodeModifier.STATIC)) { + if (methodNode.getModifiers().contains(ElementModifier.STATIC)) { writer.appendFunction("$rt_cls").append("(") .appendClass(methodNode.getReference().getClassName()).append(")"); } else {