summaryrefslogtreecommitdiff
path: root/sources/meta/scalac/ast/MetaTree.java
blob: 3ae0c9970d2fded3617e21a6ba2824f08dacf1cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package meta.scalac.ast;

import meta.java.Type;

public class MetaTree extends AbstractTreeExpander {

    //########################################################################
    // Public Methods

    public void printEmptyArrays() {
        printEmptyArrays(tree.getType(0), "EMPTY", tree.arrays);
        for (int i = 0; i < tree.nodes.length; i++) {
            TreeNode node = tree.nodes[i];
            printEmptyArrays(node.getType(0), node + "_EMPTY", node.arrays);
        }
    }

    public void printEmptyArrays(Type base, String prefix, int maxRank) {
        Type type = base;
        for (int rank = 1; rank <= maxRank; rank++) {
            type = Type.Array(type);
            writer.print("public static final ").print(type).print(" "+prefix);
            for (int i = 0; i < rank; i++) writer.print("_ARRAY");
            writer.print(" = new ").print(base).print("[0]");
            for (int i = 1; i < rank; i++) writer.print("[]");
            writer.println(";");
        }
    }

    public void printTreeCases() {
        for (int i = 0; i < tree.nodes.length; i++)
            printTreeCase(tree.nodes[i]);
    }

    private void printTreeCase(TreeNode node) {
        writer.printDescription(new String[] {
            node.description,
            "- kind         : " + description(node.kind),
            "- symbol       : " + description(node.symbol),
            "- introduced by: " + node.start.name,
            "- eliminated by: " + node.stop.name,
        });
        node.printDecl(writer.print("public case "), null, false);
        if (node.fields != null) {
            writer.lbrace();
            writer.println("assert CheckTreeNodes.instance.checkNode(this);");
            writer.rbrace();
        } else {
            writer.println(";");
        }
        if (node == tree.n_Empty)
            writer.print("static { " + node + ".type = Type.NoType; }");
        writer.println();
    }

    private String description(TreeKind kind) {
        switch (kind) {
        case Any : return "this tree is of any kind";
        case Type: return "this tree is a type";
        case Term: return "this tree is a term";
        case Dual: return "this tree is a type or a term";
        case Test: return "this tree is a type or a term " +
                       "(determined by the kind of the name field)";
        case None: return "this tree is neither a type nor a term";
        default  : throw new Error(kind.getClass().getName());
        }
    }

    private String description(TreeSymbol symbol) {
        switch (symbol) {
        case NoSym           : return "this tree has no symbol";
        case HasSym(_, false): return "this tree references a symbol";
        case HasSym(_, true ): return "this tree defines a symbol";
        default              : throw new Error(symbol.getClass().getName());
        }
    }

    public void printIsKind() {
        printIsKind(tree.nodes, TreeKind.Type);
        printIsKind(tree.nodes, TreeKind.Term);
    }

    private void printIsKind(TreeNode[] nodes, TreeKind kind) {
        writer.println("/** Returns true if this tree is a " +
            kind.toString().toLowerCase() + ". */");
        writer.print("public boolean is" + kind + "()").lbrace();
        writer.println("switch (this) {");

        for (int i = 0; i < nodes.length; i++)
            if (nodes[i].kind != TreeKind.Test && nodes[i].kind.isA(kind))
                nodes[i].printCase(writer, true).println();
        writer.indent().println("return true;").undent();

        for (int i = 0; i < nodes.length; i++) {
            if (nodes[i].kind != TreeKind.Test) continue;
            writer.print("case " + nodes[i].name + "(");
            for (int j = 0; j < nodes[i].fields.length; j++) {
                if (j > 0) writer.print(", ");
                switch (nodes[i].fields[j].type) {
                case TreeType.Name(Test):
                    writer.print(nodes[i].fields[j].type + " name");
                    break;
                default:
                    writer.print("_");
                    break;
                }
            }
            writer.println("):");
            writer.indent().print("return ");
            switch (kind) {
            case TreeKind.Type:
                writer.print("name.isTypeName() && (symbol() == null || !symbol().isConstructor()) || name == Name.ERROR");
                break;
            case TreeKind.Term:
                writer.print("name.isTermName() || (symbol() != null && symbol().isConstructor())");
                break;
            default:
                throw new Error("unexpected kind " + kind);
            }
            writer.println(";").undent();
        }

	writer.println("default:");
        writer.indent().println("return false;").undent();

        writer.println("}");
        writer.rbrace();
        writer.println();
    }

    public void printExtClasses() {
        for (int i = 0;i < tree.nodes.length;i++) printExtClass(tree.nodes[i]);
    }

    private void printExtClass(TreeNode node) {
        TreeField symbol = node.getSymbol();
        if (symbol == null) return;
        writer.print("public static class Ext"+node+" extends "+node).lbrace();
        symbol.print(writer.print("private "), true).println(";");
        writer.println();

        printExtConstructor(node, false);
        printExtConstructor(node, true);
        writer.println();

        writer.print("public boolean hasSymbol()").lbrace();
        writer.println("return true;");
        writer.rbrace();
        writer.println();

        if (node.definesSymbol()) {
            writer.print("public boolean definesSymbol()").lbrace();
            writer.println("return true;");
            writer.rbrace();
            writer.println();
        }

        writer.print("public ").print(symbol.type).print(" symbol()");
        writer.lbrace();
        writer.println("return " + symbol + ";");
        writer.rbrace();
        writer.println();

        writer.print("public ").print(tree.getType(0)).print(" setSymbol");
        symbol.print(writer.print("("), true).print(")").lbrace();
        printSetSymbol(symbol);
        for (int i = 0; i < node.fields.length; i++) {
            TreeField field = node.fields[i];
            TreeFieldLink link = field.link;
            if (link == null) continue;
            link.print(writer.print("this."+field+" = "),symbol).println(";");
        }
        writer.println("return this;");
        writer.rbrace();

        writer.rbrace();
        writer.println();
    }

    private void printExtConstructor(TreeNode node, boolean withSymbol) {
        node.printDecl(writer.print("public Ext"), null, withSymbol).lbrace();
        TreeField symbol = node.getSymbol();
        writer.print("super(");
        for (int i = 0; i < node.fields.length; i++) {
            if (i > 0) writer.print(", ");
            if (withSymbol && node.fields[i].link != null) {
                node.fields[i].link.print(writer, symbol);
            } else {
                writer.print(node.fields[i].name);
            }
        }
        writer.println(");");
        if (withSymbol) printSetSymbol(symbol);
        writer.rbrace();
    }

    private void printSetSymbol(TreeField symbol) {
        writer.println("assert " + symbol + " != null : \"null symbol\";");
        writer.println("this." + symbol + " = " + symbol + ";");
    }

    public void printEmptyCases() {
        for (int i = 0;i < tree.nodes.length;i++) {
            tree.nodes[i].printCase(writer, false).println();
            writer.println();
        }
    }

    //########################################################################
}