Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST. jlahoda-tree-builder
authorjlahoda
Thu, 28 Mar 2019 11:31:30 +0100
branchjlahoda-tree-builder
changeset 57295 5497ee9d40f4
parent 57267 97aaf02ed830
child 57296 464cc8d22d94
Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
src/jdk.compiler/share/classes/com/sun/source/util/TreeBuilder.java
src/jdk.compiler/share/classes/com/sun/tools/javac/api/TreeBuilderImpl.java
test/langtools/tools/javac/api/ast/ASTBuilder.java
test/langtools/tools/javac/api/ast/CodeBuilder.java
--- a/src/jdk.compiler/share/classes/com/sun/source/util/TreeBuilder.java	Fri Mar 15 14:21:44 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/source/util/TreeBuilder.java	Thu Mar 28 11:31:30 2019 +0100
@@ -125,6 +125,7 @@
         void plus(Consumer<Expression> lhs, Consumer<Expression> rhs);
         void cond(Consumer<Expression> cond, Consumer<Expression> truePart, Consumer<Expression> falsePart);
         void ident(String... qnames);
+        void literal(Object value);
     }
 
     interface Statements {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/TreeBuilderImpl.java	Fri Mar 15 14:21:44 2019 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/TreeBuilderImpl.java	Thu Mar 28 11:31:30 2019 +0100
@@ -116,7 +116,7 @@
             }
             VariableImpl vi = new VariableImpl(ti.type, name);
             field.accept(vi);
-            result.defs = result.defs.prepend(vi.result);
+            result.defs = result.defs.append(vi.result);
             return this;
         }
 
@@ -184,7 +184,17 @@
 
         @Override
         public Variable init(Consumer<Expression> init) {
-            throw new UnsupportedOperationException("Not supported yet.");
+            ExpressionImpl expr = new ExpressionImpl();
+
+            init.accept(expr);
+
+            if (expr.expr == null) {
+                throw new IllegalStateException("Expression not provided!");
+            }
+
+            result.init = expr.expr;
+
+            return this;
         }
 
         @Override
@@ -203,4 +213,38 @@
         }
         
     }
+
+    private final class ExpressionImpl implements Expression {
+
+        private JCExpression expr;
+
+        @Override
+        public void minusminus(Consumer<Expression> expr) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void plus(Consumer<Expression> lhs, Consumer<Expression> rhs) {
+            ExpressionImpl left = new ExpressionImpl();
+            lhs.accept(left);
+            ExpressionImpl right = new ExpressionImpl();
+            rhs.accept(right);
+            expr = make.Binary(Tag.PLUS, left.expr, right.expr); //XXX: check exprs filled!
+        }
+
+        @Override
+        public void cond(Consumer<Expression> cond, Consumer<Expression> truePart, Consumer<Expression> falsePart) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void ident(String... qnames) {
+            expr = make.Ident(names.fromString(qnames[0])); //XXX
+        }
+
+        @Override
+        public void literal(Object value) {
+            expr = make.Literal(value);
+        }
+    }
 }
--- a/test/langtools/tools/javac/api/ast/ASTBuilder.java	Fri Mar 15 14:21:44 2019 +0100
+++ b/test/langtools/tools/javac/api/ast/ASTBuilder.java	Thu Mar 28 11:31:30 2019 +0100
@@ -25,8 +25,12 @@
  * @test
  * @bug 9999999
  * @summary XXX
- * @modules java.compiler
- *          jdk.compiler
+ * @library /tools/lib
+ * @modules
+ *      jdk.compiler/com.sun.tools.javac.api
+ *      jdk.compiler/com.sun.tools.javac.main
+ * @build toolbox.ToolBox CodeBuilder
+ * @run main ASTBuilder
  */
 
 import java.net.URI;
@@ -46,6 +50,16 @@
 import com.sun.source.util.Trees;
 import com.sun.source.util.TreeScanner;
 
+import javax.tools.StandardLocation;
+
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.IdentifierTree;
+import com.sun.source.tree.MemberReferenceTree;
+import com.sun.source.tree.MemberSelectTree;
+import com.sun.source.tree.MethodTree;
+import com.sun.source.tree.VariableTree;
+import toolbox.ToolBox.MemoryFileManager;
+
 public class ASTBuilder {
     
     public static void main(String[] args) throws Exception {
@@ -53,15 +67,19 @@
                 "    int x;" +
                 "}",
                 U -> U._class("Test", C -> C.field("x", Type::_int)));
+        runTest("class Test {" +
+                "    int x1 = 2;" +
+                "    int x2 = 2 + x1;" +
+                "}");
     }
 
     private static void runTest(String expectedCode, Consumer<CompilationUnit> actualBuilder) throws Exception {
         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
         assert tool != null;
 
-        JavacTask expecteTask = (JavacTask) tool.getTask(null, null, null,
+        JavacTask expectedTask = (JavacTask) tool.getTask(null, null, null,
             null, null, Arrays.asList(new MyFileObject(expectedCode)));
-        String expectedDump = dumpTree(expecteTask.parse().iterator().next());
+        String expectedDump = dumpTree(expectedTask.parse().iterator().next());
 
         JavacTask ct = (JavacTask) tool.getTask(null, null, null,
             null, null, Arrays.asList(new MyFileObject("")));
@@ -79,6 +97,55 @@
         }
     }
 
+    private static void runTest(String expectedCode) throws Exception {
+        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+        assert tool != null;
+
+        JavacTask expectedTask = (JavacTask) tool.getTask(null, null, null,
+            null, null, Arrays.asList(new MyFileObject(expectedCode)));
+        CompilationUnitTree expectedCUT = expectedTask.parse().iterator().next();
+        String builderCode = CodeBuilder.buildCodeToGenerate(expectedCUT, "builder");
+        System.err.println("builderCode:");
+        System.err.println(builderCode);
+        String expectedCodeGen = "import com.sun.source.tree.*; import com.sun.source.util.*; public class CodeGen { public static CompilationUnitTree build(TreeBuilder builder) { return " + builderCode + "; } }";
+        String expectedDump = dumpTree(expectedCUT);
+
+        try (MemoryFileManager mfm = new MemoryFileManager(tool.getStandardFileManager(null, null, null))) {
+            Boolean res = tool.getTask(null, mfm, null,
+                                       null, null, Arrays.asList(new MyFileObject(expectedCodeGen)))
+                              .call();
+            if (!res) {
+                throw new IllegalStateException("Could not compile the generated code!");
+            }
+            
+            ClassLoader codeCL = new ClassLoader(ASTBuilder.class.getClassLoader()) {
+                @Override
+                protected java.lang.Class<?> findClass(String name) throws ClassNotFoundException {
+                    byte[] bytecode = mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, name);
+                    if (bytecode != null) {
+                        return defineClass(name, bytecode, 0, bytecode.length);
+                    }
+                    return super.findClass(name);
+                }
+            };
+
+            JavacTask ct = (JavacTask) tool.getTask(null, null, null,
+                null, null, Arrays.asList(new MyFileObject("")));
+            ct.parse(); //init javac
+            Trees t = Trees.instance(ct);
+
+            TreeBuilder builder = t.getTreeBuilder();
+
+            java.lang.Class<?> codeGen = codeCL.loadClass("CodeGen");
+            CompilationUnitTree cut = (CompilationUnitTree) codeGen.getDeclaredMethod("build", TreeBuilder.class).invoke(null, builder);
+            String actualDump = dumpTree(cut);
+
+            if (!actualDump.equals(expectedDump)) {
+                throw new AssertionError("Expected and actual dump differ. Expected: " + expectedDump + "; actual: " + actualDump);
+            }
+        }
+    }
+
     static String dumpTree(Tree t) {
         StringBuilder result = new StringBuilder();
         new TreeScanner<Void, Void>() {
@@ -110,6 +177,36 @@
                 result.append(")");
                 return null;
             }
+            @Override
+            public Void visitIdentifier(IdentifierTree node, Void p) {
+                result.append(node.getName());
+                return super.visitIdentifier(node, p);
+            }
+            @Override
+            public Void visitMemberSelect(MemberSelectTree node, Void p) {
+                result.append(node.getIdentifier());
+                return super.visitMemberSelect(node, p);
+            }
+            @Override
+            public Void visitMemberReference(MemberReferenceTree node, Void p) {
+                result.append(node.getName());
+                return super.visitMemberReference(node, p);
+            }
+            @Override
+            public Void visitClass(ClassTree node, Void p) {
+                result.append(node.getSimpleName());
+                return super.visitClass(node, p);
+            }
+            @Override
+            public Void visitMethod(MethodTree node, Void p) {
+                result.append(node.getName());
+                return super.visitMethod(node, p);
+            }
+            @Override
+            public Void visitVariable(VariableTree node, Void p) {
+                result.append(node.getName());
+                return super.visitVariable(node, p);
+            }
         }.scan(t, null);
         return result.toString();
     }
@@ -126,5 +223,10 @@
         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
             return text;
         }
+
+        @Override
+        public boolean isNameCompatible(String simpleName, Kind kind) {
+            return true;
+        }
     }
-}
\ No newline at end of file
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/api/ast/CodeBuilder.java	Thu Mar 28 11:31:30 2019 +0100
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.sun.source.tree.BinaryTree;
+import com.sun.source.tree.ClassTree;
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.IdentifierTree;
+import com.sun.source.tree.LiteralTree;
+import com.sun.source.tree.PrimitiveTypeTree;
+import com.sun.source.tree.Tree;
+import com.sun.source.tree.VariableTree;
+import com.sun.source.util.TreeScanner;
+
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Set;
+
+
+public class CodeBuilder {
+
+    public static String buildCodeToGenerate(Tree t, String builder) {
+        StringBuilder result = new StringBuilder();
+        new TreeScanner<Void, Void>() {
+            private final Set<String> usedNames = new HashSet<>();
+            private String currentBuilder = builder;
+            @Override
+            public Void visitClass(ClassTree node, Void p) {
+                result.append(currentBuilder() + "._class(\"" + node.getSimpleName() + "\", ");
+                doScan("C", () -> super.visitClass(node, p));
+                result.append(")");
+                return null;
+            }
+            @Override
+            public Void visitCompilationUnit(CompilationUnitTree node, Void p) {
+                result.append(currentBuilder() + ".createCompilationUnitTree(");
+                doScan("U", () -> super.visitCompilationUnit(node, p));
+                result.append(")");
+                return null;
+            }
+            @Override
+            public Void visitVariable(VariableTree node, Void p) {
+                result.append(currentBuilder() + ".field(\"" + node.getName() + "\", "); //XXX: field/vs local variable!
+                doScan("T", node.getType());
+                result.append(", ");
+                doScan("F", () -> {
+                    //TODO: modifiers....
+                    if (node.getInitializer() != null) {
+                        result.append(currentBuilder() + ".init(");
+                        doScan("E", node.getInitializer());
+                        result.append(")");
+                    }
+                });
+                result.append(")");
+                return null;
+            }
+
+            @Override
+            public Void visitPrimitiveType(PrimitiveTypeTree node, Void p) {
+                result.append(currentBuilder() + "._" + node.getPrimitiveTypeKind().name().toLowerCase(Locale.ROOT) + "()");
+                return null;
+            }
+
+            @Override
+            public Void visitLiteral(LiteralTree node, Void p) {
+                result.append(currentBuilder() + ".literal(" + node.getValue() + ")");
+                return null;
+            }
+
+            @Override
+            public Void visitBinary(BinaryTree node, Void p) {
+                switch (node.getKind()) {
+                    case PLUS:
+                        result.append(currentBuilder() + ".plus(");
+                        doScan("E", node.getLeftOperand());
+                        result.append(", ");
+                        doScan("E", node.getRightOperand());
+                        result.append(")");
+                        break;
+                    default: throw new IllegalStateException("Not handled: " + node.getKind());
+                }
+                return null;
+            }
+
+            @Override
+            public Void visitIdentifier(IdentifierTree node, Void p) {
+                result.append(currentBuilder() + ".ident(\"" + node.getName() + "\")");
+                return null;
+            }
+
+            private String currentBuilder() {
+                if (currentBuilder != null) {
+                    String res = currentBuilder;
+                    currentBuilder = null;
+                    return res;
+                }
+                return "";
+            }
+            private void doScan(String preferredBuilderName, Tree... subTreesToScan) {
+                doScan(preferredBuilderName, () -> {
+                    for (Tree tree : subTreesToScan) {
+                        scan(tree, null);
+                    }
+                });
+            }
+
+            private void doScan(String preferredBuilderName, Runnable scan) {
+                String prevBuilder = currentBuilder;
+                String builder = inferBuilderName(preferredBuilderName);
+                try {
+                    result.append(builder + " -> ");
+                    int len = result.length();
+                    currentBuilder = builder;
+                    scan.run();
+                    if (result.length() == len) {
+                        result.append("{}");
+                    }
+                } finally {
+                    currentBuilder = prevBuilder;
+                    usedNames.remove(builder);
+                }
+            }
+            private String inferBuilderName(String preferredBuilderName) {
+                Integer idx = null;
+                while (true) {
+                    String thisBuilder = preferredBuilderName + (idx != null ? idx : "");
+                    if (usedNames.add(thisBuilder)) {
+                        return thisBuilder;
+                    }
+                    idx = idx != null ? idx + 1 : 1;
+                }
+            }
+        }.scan(t, null);
+
+        return result.toString();
+    }
+
+}