test/langtools/tools/javac/api/ast/CodeBuilder.java
author jlahoda
Tue, 02 Apr 2019 16:51:25 +0200
branchjlahoda-tree-builder
changeset 57300 c79e191854e4
parent 57297 ad0be596956b
permissions -rw-r--r--
Adding support for modifiers.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     1
/*
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     4
 *
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    10
 *
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    16
 *
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    20
 *
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    23
 * questions.
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    24
 */
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    25
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    26
import com.sun.source.tree.BinaryTree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    27
import com.sun.source.tree.ClassTree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    28
import com.sun.source.tree.CompilationUnitTree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    29
import com.sun.source.tree.IdentifierTree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    30
import com.sun.source.tree.LiteralTree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    31
import com.sun.source.tree.PrimitiveTypeTree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    32
import com.sun.source.tree.Tree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    33
import com.sun.source.tree.VariableTree;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    34
import com.sun.source.util.TreeScanner;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    35
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    36
import java.util.HashSet;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    37
import java.util.Locale;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    38
import java.util.Set;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    39
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
    40
import com.sun.source.tree.BlockTree;
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
    41
import com.sun.source.tree.IfTree;
57296
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    42
import com.sun.source.tree.MemberSelectTree;
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
    43
import com.sun.source.tree.MethodTree;
57296
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    44
import com.sun.source.tree.ParameterizedTypeTree;
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
    45
import com.sun.source.tree.ReturnTree;
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
    46
import com.sun.source.tree.StatementTree;
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    47
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    48
public class CodeBuilder {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    49
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    50
    public static String buildCodeToGenerate(Tree t, String builder) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    51
        StringBuilder result = new StringBuilder();
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    52
        new TreeScanner<Void, Void>() {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    53
            private final Set<String> usedNames = new HashSet<>();
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    54
            private String currentBuilder = builder;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    55
            @Override
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    56
            public Void visitClass(ClassTree node, Void p) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    57
                result.append(currentBuilder() + "._class(\"" + node.getSimpleName() + "\", ");
57296
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    58
                doScan("C", () -> {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    59
//                    R r = scan(node.getModifiers(), p);
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    60
//                    r = scanAndReduce(node.getTypeParameters(), p, r);
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    61
                    if (node.getExtendsClause() != null) {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    62
                        result.append(currentBuilder() + ".superclass(");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    63
                        handleDeclaredType(node.getExtendsClause());
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    64
                        result.append(")");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    65
                    }
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    66
                    for (Tree impl : node.getImplementsClause()) {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    67
                        result.append(currentBuilder() + ".superinterface(");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    68
                        handleDeclaredType(impl);
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    69
                        result.append(")");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    70
                    }
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    71
                    scan(node.getMembers(), p);
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
    72
                });
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    73
                result.append(")");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    74
                return null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    75
            }
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
    76
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    77
            @Override
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    78
            public Void visitCompilationUnit(CompilationUnitTree node, Void p) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    79
                result.append(currentBuilder() + ".createCompilationUnitTree(");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    80
                doScan("U", () -> super.visitCompilationUnit(node, p));
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    81
                result.append(")");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    82
                return null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    83
            }
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
    84
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    85
            @Override
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    86
            public Void visitVariable(VariableTree node, Void p) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    87
                result.append(currentBuilder() + ".field(\"" + node.getName() + "\", "); //XXX: field/vs local variable!
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    88
                doScan("T", node.getType());
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    89
                result.append(", ");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    90
                doScan("F", () -> {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    91
                    //TODO: modifiers....
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    92
                    if (node.getInitializer() != null) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    93
                        result.append(currentBuilder() + ".init(");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    94
                        doScan("E", node.getInitializer());
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    95
                        result.append(")");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    96
                    }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    97
                });
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    98
                result.append(")");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
    99
                return null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   100
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   101
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   102
            @Override
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   103
            public Void visitMethod(MethodTree node, Void p) {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   104
                result.append(currentBuilder() + ".method(\"" + node.getName() + "\", ");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   105
                doScan("T", node.getReturnType());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   106
                result.append(", ");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   107
                doScan("M", () -> {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   108
                    //TODO: other attributes!
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   109
                    for (VariableTree param : node.getParameters()) {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   110
                        result.append(currentBuilder() + ".parameter(");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   111
                        doScan("T", param.getType());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   112
                        result.append(", ");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   113
                        doScan("P", () -> {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   114
                            result.append(currentBuilder() + ".name(\"" + param.getName() + "\")");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   115
                        });
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   116
                        //TODO: other attributes!
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   117
                        result.append(")");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   118
                    }
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   119
                    if (node.getBody() != null) {//TODO: test no/null body!
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   120
                        result.append(currentBuilder() + ".body(");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   121
                        doScan("B", node.getBody());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   122
                        result.append(")");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   123
                    }
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   124
                });
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   125
                result.append(")");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   126
                return null;
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   127
            }
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   128
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   129
            @Override
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   130
            public Void visitPrimitiveType(PrimitiveTypeTree node, Void p) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   131
                result.append(currentBuilder() + "._" + node.getPrimitiveTypeKind().name().toLowerCase(Locale.ROOT) + "()");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   132
                return null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   133
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   134
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   135
            @Override
57296
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   136
            public Void visitParameterizedType(ParameterizedTypeTree node, Void p) {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   137
                scan(node.getType(), null);
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   138
                result.append(", ");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   139
                doScan("A", () -> {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   140
                    for (Tree ta : node.getTypeArguments()) {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   141
                        result.append(currentBuilder() + ".type(");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   142
                        handleDeclaredType(ta);
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   143
                        result.append(")");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   144
                    }
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   145
                });
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   146
                return null;
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   147
            }
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   148
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   149
            @Override
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   150
            public Void visitLiteral(LiteralTree node, Void p) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   151
                result.append(currentBuilder() + ".literal(" + node.getValue() + ")");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   152
                return null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   153
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   154
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   155
            @Override
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   156
            public Void visitBinary(BinaryTree node, Void p) {
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   157
                String methodName;
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   158
                switch (node.getKind()) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   159
                    case PLUS:
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   160
                        methodName = "plus"; break;
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   161
                    case EQUAL_TO:
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   162
                        methodName = "equal_to"; break;
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   163
                    default: throw new IllegalStateException("Not handled: " + node.getKind());
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   164
                }
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   165
                result.append(currentBuilder() + "." + methodName + "(");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   166
                doScan("E", node.getLeftOperand());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   167
                result.append(", ");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   168
                doScan("E", node.getRightOperand());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   169
                result.append(")");
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   170
                return null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   171
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   172
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   173
            @Override
57296
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   174
            public Void visitMemberSelect(MemberSelectTree node, Void p) {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   175
                //TODO: use ident(String...) for "flat" selects
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   176
                result.append(currentBuilder() + ".select(");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   177
                doScan("E", node.getExpression());
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   178
                result.append(", \"");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   179
                result.append(node.getIdentifier());
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   180
                result.append("\")");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   181
                return null;
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   182
            }
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   183
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   184
            @Override
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   185
            public Void visitIdentifier(IdentifierTree node, Void p) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   186
                result.append(currentBuilder() + ".ident(\"" + node.getName() + "\")");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   187
                return null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   188
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   189
57297
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   190
            @Override
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   191
            public Void visitIf(IfTree node, Void p) {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   192
                result.append(currentBuilder() + "._if(");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   193
                doScan("E", node.getCondition());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   194
                result.append(", ");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   195
                doScan("S", node.getThenStatement());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   196
                if (node.getElseStatement() != null) {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   197
                    result.append(", ");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   198
                    doScan("S", node.getElseStatement());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   199
                }
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   200
                result.append(")");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   201
                return null;
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   202
            }
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   203
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   204
            @Override
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   205
            public Void visitReturn(ReturnTree node, Void p) {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   206
                result.append(currentBuilder() + "._return(");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   207
                if (node.getExpression()!= null) {
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   208
                    doScan("E", node.getExpression());
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   209
                }
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   210
                result.append(")");
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   211
                return null;
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   212
            }
ad0be596956b Adding basic support for method bodies.
jlahoda
parents: 57296
diff changeset
   213
57296
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   214
            private void handleDeclaredType(Tree t) {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   215
                doScan("T", () -> {
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   216
                    result.append(currentBuilder() + "._class(");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   217
                    doScan("Q", t);
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   218
                    result.append(")");
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   219
                });
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   220
            }
464cc8d22d94 Experimenting with types modelling.
jlahoda
parents: 57295
diff changeset
   221
57295
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   222
            private String currentBuilder() {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   223
                if (currentBuilder != null) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   224
                    String res = currentBuilder;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   225
                    currentBuilder = null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   226
                    return res;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   227
                }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   228
                return "";
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   229
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   230
            private void doScan(String preferredBuilderName, Tree... subTreesToScan) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   231
                doScan(preferredBuilderName, () -> {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   232
                    for (Tree tree : subTreesToScan) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   233
                        scan(tree, null);
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   234
                    }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   235
                });
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   236
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   237
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   238
            private void doScan(String preferredBuilderName, Runnable scan) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   239
                String prevBuilder = currentBuilder;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   240
                String builder = inferBuilderName(preferredBuilderName);
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   241
                try {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   242
                    result.append(builder + " -> ");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   243
                    int len = result.length();
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   244
                    currentBuilder = builder;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   245
                    scan.run();
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   246
                    if (result.length() == len) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   247
                        result.append("{}");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   248
                    }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   249
                } finally {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   250
                    currentBuilder = prevBuilder;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   251
                    usedNames.remove(builder);
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   252
                }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   253
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   254
            private String inferBuilderName(String preferredBuilderName) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   255
                Integer idx = null;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   256
                while (true) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   257
                    String thisBuilder = preferredBuilderName + (idx != null ? idx : "");
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   258
                    if (usedNames.add(thisBuilder)) {
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   259
                        return thisBuilder;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   260
                    }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   261
                    idx = idx != null ? idx + 1 : 1;
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   262
                }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   263
            }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   264
        }.scan(t, null);
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   265
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   266
        return result.toString();
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   267
    }
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   268
5497ee9d40f4 Adding a prototype of CodeBuilder to build a sequence of TreeBuilder calls to construct a given AST.
jlahoda
parents:
diff changeset
   269
}