diff options
author | paltherr <paltherr@epfl.ch> | 2003-04-30 07:44:06 +0000 |
---|---|---|
committer | paltherr <paltherr@epfl.ch> | 2003-04-30 07:44:06 +0000 |
commit | fa72586d0b7c8fef92643d1d1402c8d94bea9c3d (patch) | |
tree | b6deef5e7f28373abe0f5157f55fc27d39fd7a0e /sources/meta/scalac | |
parent | 4d1dfaffedbe38038fcfe2fc3af01f1c949a53fe (diff) | |
download | scala-fa72586d0b7c8fef92643d1d1402c8d94bea9c3d.tar.gz scala-fa72586d0b7c8fef92643d1d1402c8d94bea9c3d.tar.bz2 scala-fa72586d0b7c8fef92643d1d1402c8d94bea9c3d.zip |
- Added methods which take a symbol in TreeFact...
- Added methods which take a symbol in TreeFactories and TreeCopiers.
Diffstat (limited to 'sources/meta/scalac')
-rw-r--r-- | sources/meta/scalac/ast/AbstractTreeCaseExpander.java | 4 | ||||
-rw-r--r-- | sources/meta/scalac/ast/AbstractTreeExpander.java | 11 | ||||
-rw-r--r-- | sources/meta/scalac/ast/AbstractTreeMethodExpander.java | 13 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaDefaultTreeFactory.java | 7 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaLazyTreeCopier.java | 40 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaStrictTreeCopier.java | 26 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTransformer.java | 62 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTraverser.java | 19 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTree.java | 129 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTreeCopier.java | 8 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTreeFactory.java | 5 | ||||
-rw-r--r-- | sources/meta/scalac/ast/Tree.java | 128 | ||||
-rw-r--r-- | sources/meta/scalac/ast/TreeField.java | 23 | ||||
-rw-r--r-- | sources/meta/scalac/ast/TreeFieldLink.java | 6 | ||||
-rw-r--r-- | sources/meta/scalac/ast/TreeNode.java | 191 | ||||
-rw-r--r-- | sources/meta/scalac/ast/TreeSymbol.java | 5 | ||||
-rw-r--r-- | sources/meta/scalac/ast/TreeType.java | 15 |
17 files changed, 438 insertions, 254 deletions
diff --git a/sources/meta/scalac/ast/AbstractTreeCaseExpander.java b/sources/meta/scalac/ast/AbstractTreeCaseExpander.java index aa026ed3cf..c7e023c16f 100644 --- a/sources/meta/scalac/ast/AbstractTreeCaseExpander.java +++ b/sources/meta/scalac/ast/AbstractTreeCaseExpander.java @@ -15,7 +15,7 @@ public abstract class AbstractTreeCaseExpander extends AbstractTreeExpander { // Public Constructors public AbstractTreeCaseExpander() { - writer.importType(tree.t_Debug); + writer.importType(t_Debug); } //######################################################################## @@ -27,7 +27,7 @@ public abstract class AbstractTreeCaseExpander extends AbstractTreeExpander { printTreeCases(); writer.println("default:"); writer.indent(); - writer.print("throw ").print(tree.t_Debug). + writer.print("throw ").print(t_Debug). println(".abort(\"unknown tree\", tree);"); writer.undent(); writer.println("}"); diff --git a/sources/meta/scalac/ast/AbstractTreeExpander.java b/sources/meta/scalac/ast/AbstractTreeExpander.java index e7a162d7fe..503a9651a2 100644 --- a/sources/meta/scalac/ast/AbstractTreeExpander.java +++ b/sources/meta/scalac/ast/AbstractTreeExpander.java @@ -8,6 +8,7 @@ package meta.scalac.ast; +import meta.java.Type; import meta.java.AbstractJavaExpander; /** A base class for expanders that handle tree nodes. */ @@ -16,18 +17,24 @@ public abstract class AbstractTreeExpander extends AbstractJavaExpander { //######################################################################## // Public Fields + public final Type t_Debug; public final Tree tree; //######################################################################## // Public Constructors public AbstractTreeExpander() { + this.t_Debug = TreeType.Reference("scalac.util", "Debug"); this.tree = new Tree(); - writer.importFrom(tree.t_Tree); - for (int i = 0; i < tree.nodes.length; i++) + writer.importFrom(t_Debug); + writer.importFrom(tree.getType(0)); + for (int i = 0; i < tree.nodes.length; i++) { if (tree.nodes[i].fields != null) for (int j = 0; j < tree.nodes[i].fields.length; j++) writer.importType(tree.nodes[i].fields[j].type); + if (tree.nodes[i].hasSymbol()) + writer.importType(tree.nodes[i].getSymbol().type); + } } //######################################################################## diff --git a/sources/meta/scalac/ast/AbstractTreeMethodExpander.java b/sources/meta/scalac/ast/AbstractTreeMethodExpander.java index 78000ccb94..7444759111 100644 --- a/sources/meta/scalac/ast/AbstractTreeMethodExpander.java +++ b/sources/meta/scalac/ast/AbstractTreeMethodExpander.java @@ -16,17 +16,16 @@ public abstract class AbstractTreeMethodExpander extends AbstractTreeExpander { public void printTreeMethods() { for (int i = 0; i < tree.nodes.length; i++) { - if (tree.nodes[i].fields != null) printTreeMethod(tree.nodes[i]); + if (tree.nodes[i].fields != null) { + printTreeMethod(tree.nodes[i], false); + if (tree.nodes[i].hasSymbol()) + printTreeMethod(tree.nodes[i], true); + } writer.println(); } } - public abstract void printTreeMethod(TreeNode node); - - public void printTreeMethodHeader(TreeNode node, String prefix) { - writer.print("public " + node.name + " " + node.name + "("); - node.printParams(writer, prefix).print(")"); - } + public abstract void printTreeMethod(TreeNode node, boolean withSymbol); //######################################################################## } diff --git a/sources/meta/scalac/ast/MetaDefaultTreeFactory.java b/sources/meta/scalac/ast/MetaDefaultTreeFactory.java index 957f46439a..fde4fe7773 100644 --- a/sources/meta/scalac/ast/MetaDefaultTreeFactory.java +++ b/sources/meta/scalac/ast/MetaDefaultTreeFactory.java @@ -13,10 +13,9 @@ public class MetaDefaultTreeFactory extends AbstractTreeMethodExpander { //######################################################################## // Public Methods - public void printTreeMethod(TreeNode node) { - printTreeMethodHeader(node, "int pos"); - writer.lbrace(); - node.printNew(writer.print(node.name + " t = ")).println(";"); + public void printTreeMethod(TreeNode node, boolean withSymbol) { + node.printMethod(writer, "int pos", withSymbol).lbrace(); + node.printNew(writer.print(node + " t = "), withSymbol).println(";"); writer.println("t.pos = pos;"); writer.println("return t;"); writer.rbrace(); diff --git a/sources/meta/scalac/ast/MetaLazyTreeCopier.java b/sources/meta/scalac/ast/MetaLazyTreeCopier.java index 56ed1e3e00..c389ab9404 100644 --- a/sources/meta/scalac/ast/MetaLazyTreeCopier.java +++ b/sources/meta/scalac/ast/MetaLazyTreeCopier.java @@ -13,26 +13,38 @@ public class MetaLazyTreeCopier extends AbstractTreeMethodExpander { //######################################################################## // Public Methods - public void printTreeMethod(TreeNode node) { - printTreeMethodHeader(node, tree.t_Tree + " tree"); - writer.lbrace(); - writer.println(node.name + " t = (" + node.name + ")tree;"); - if (node.fields.length > 0) { + public void printTreeMethod(TreeNode node, boolean withSymbol) { + TreeField symbol = node.getSymbol(); + node.printMethod(writer, tree.getFormal("tree"), withSymbol).lbrace(); + if (!withSymbol && node.hasLinkedFields()) + writer.println("assert tree.symbol() == null : "+ + "\"tree's symbol is not null\";"); + writer.print(node.getType(0)).print(" t = ("). + print(node.getType(0)).println(")tree;"); + TreeField[] fields = node.getFields(withSymbol); + // !!! why do we copy if there is no symbol and no field + if (withSymbol || node.fields.length > 0) { writer.print("if (").indent(); - for (int i = 0; i < node.fields.length; i++) { - if (i > 0) writer.println(" &&"); - String name = node.fields[i].name; - writer.print("t." + name + " == " + name); + if (withSymbol) writer.print("t.symbol() == " + symbol); + for (int i = 0; i < fields.length; i++) { + if (i > 0 ? true : withSymbol) writer.println(" &&"); + writer.print("t." + fields[i] + " == " + fields[i]); } writer.println(")"); writer.println("return t;").undent(); - writer.print("t = copier." + node.name + "("); - node.printArgs(writer, "tree").println(");"); - writer.println("t.type = tree.type;"); - if (node.hasSymbol())writer.println("t.setSymbol(tree.symbol());"); } - writer.println("return t;"); + writer.print("return copier."); + node.printCall(writer, "tree", withSymbol).println(";"); writer.rbrace(); + + if (withSymbol && node.hasLinkedFields()) { + node.printMethod(writer, tree.getFormal("tree"), false, true); + writer.lbrace(); + symbol.print(writer, true).println(" = tree.symbol();"); + node.printCall(writer.print("return "), "tree", true).println(";"); + writer.rbrace(); + return; + } } //######################################################################## diff --git a/sources/meta/scalac/ast/MetaStrictTreeCopier.java b/sources/meta/scalac/ast/MetaStrictTreeCopier.java index 9cc050396a..cd0ec6bf58 100644 --- a/sources/meta/scalac/ast/MetaStrictTreeCopier.java +++ b/sources/meta/scalac/ast/MetaStrictTreeCopier.java @@ -13,16 +13,30 @@ public class MetaStrictTreeCopier extends AbstractTreeMethodExpander { //######################################################################## // Public Methods - public void printTreeMethod(TreeNode node) { - printTreeMethodHeader(node, tree.t_Tree + " tree"); - writer.lbrace(); - writer.print(node.name + " t = make." + node.name + "("); - node.printArgs(writer, "tree.pos").println(");"); + public void printTreeMethod(TreeNode node, boolean withSymbol) { + TreeField symbol = node.getSymbol(); + node.printMethod(writer, tree.getFormal("tree"), withSymbol).lbrace(); + if (!withSymbol && node.hasLinkedFields()) + writer.println("assert tree.symbol() == null : "+ + "\"tree's symbol is not null\";"); + writer.print(node.getType(0)).print(" t = make."); + node.printCall(writer, "tree.pos", withSymbol).println(";"); writer.println("t.type = tree.type;"); - if (node.hasSymbol()) writer.println("t.setSymbol(tree.symbol());"); + if (!withSymbol && node.hasSymbol() && !node.hasLinkedFields()) + writer.println("t.setSymbol(tree.symbol());"); writer.println("return t;"); writer.rbrace(); + + if (withSymbol && node.hasLinkedFields()) { + node.printMethod(writer, tree.getFormal("tree"), false, true); + writer.lbrace(); + symbol.print(writer, true).println(" = tree.symbol();"); + node.printCall(writer.print("return "), "tree", true).println(";"); + writer.rbrace(); + } } //######################################################################## } + + diff --git a/sources/meta/scalac/ast/MetaTransformer.java b/sources/meta/scalac/ast/MetaTransformer.java index c4f2794851..79ebebaf2f 100644 --- a/sources/meta/scalac/ast/MetaTransformer.java +++ b/sources/meta/scalac/ast/MetaTransformer.java @@ -19,40 +19,52 @@ public class MetaTransformer extends AbstractTreeCaseExpander { if (node.fields == null) { writer.println("return tree;"); } else { - writer.print("return copy." + node + "(tree"); - for (int i = 0; i < node.fields.length; i++) { - writer.print(", "); - if (Tree.isTree(node.fields[i].type)) - writer.print("transform(" + node.fields[i] + ")"); - else - writer.print(node.fields[i].name); + if (node.hasSymbol()) { + writer.print("if (tree.symbol() != null)").lbrace(); + printTransformNode(node, true); + writer.undent().print("} else").lbrace(); } - writer.println(");"); + printTransformNode(node, false); + if (node.hasSymbol()) writer.rbrace(); } } + public void printTransformNode(TreeNode node, boolean withSymbol) { + TreeField[] fields = node.getFields(withSymbol); + writer.print("return copy." + node + "(tree"); + if (withSymbol) writer.print(", tree.symbol()"); + for (int i = 0; i < fields.length; i++) { + writer.print(", "); + if (Tree.isTree(fields[i].type)) + writer.print("transform(" + fields[i].name + ")"); + else + writer.print(fields[i].name); + } + writer.println(");"); + } + public void printTransformArrays() { - printTransformArrayOf(tree.t_Tree, false); - printTransformArrayOf(tree.t_Trees, false); + for (int j = 1; j <= tree.arrays; j++) + printTransformArray(tree.getType(j), false); for (int i = 0; i < tree.nodes.length; i++) - for (int j = 0; j < tree.nodes[i].arrays; j++) - printTransformArrayOf(tree.nodes[i].getType(j), j == 0); + for (int j = 1; j <= tree.nodes[i].arrays; j++) + printTransformArray(tree.nodes[i].getType(j), j == 1); } - public void printTransformArrayOf(Type type, boolean needCast) { - String cast = needCast ? "(" + type + ")" : ""; - writer.print("public "+type+"[] transform("+type+"[] ts)"); - writer.lbrace(); - writer.print("for (int i = 0; i < ts.length; i++)"); - writer.lbrace(); - writer.println((needCast?tree.t_Tree:type)+" t = transform(ts[i]);"); - writer.print("if (t != ts[i])"); - writer.lbrace(); - writer.println(type+"[] res = new "+type.newArray("[ts.length]")+";"); + public void printTransformArray(Type type, boolean needCast) { + Type item = type.getItemType(); + Type erased = needCast ? tree.getType(0) : item; + String cast = needCast ? "(" + item + ")" : ""; + writer.print("public ").print(type).print(" transform"). + print("(").print(type).print(" ts)").lbrace(); + writer.print("for (int i = 0; i < ts.length; i++)").lbrace(); + writer.println(erased + " t = transform(ts[i]);"); + writer.print("if (t != ts[i])").lbrace(); + writer.println(type+" res = new "+item.newArray("[ts.length]")+";"); writer.println("System.arraycopy(ts, 0, res, 0, i);"); - writer.println("res[i] = "+cast+"t;"); - writer.println("for (int j = i + 1; j < ts.length; j++)"); - writer.println("res[j] = "+cast+"transform(ts[j]);"); + writer.println("res[i++] = "+cast+"t;"); + writer.print("for (; i < ts.length; i++)"). + println("res[i] = "+cast+"transform(ts[i]);"); writer.println("return res;"); writer.rbrace(); writer.rbrace(); diff --git a/sources/meta/scalac/ast/MetaTraverser.java b/sources/meta/scalac/ast/MetaTraverser.java index 4e00b924c9..0f166510db 100644 --- a/sources/meta/scalac/ast/MetaTraverser.java +++ b/sources/meta/scalac/ast/MetaTraverser.java @@ -8,6 +8,8 @@ package meta.scalac.ast; +import meta.java.Type; + public class MetaTraverser extends AbstractTreeCaseExpander { //######################################################################## @@ -21,5 +23,22 @@ public class MetaTraverser extends AbstractTreeCaseExpander { writer.println("return;"); } + public void printTraverseArrays() { + int max = tree.arrays; + for (int i = 0; i < tree.nodes.length; i++) + max = Math.max(max, tree.nodes[i].arrays); + for (int i = 1; i <= max; i++) + printTraverseArray(tree.getType(i)); + } + + public void printTraverseArray(Type type) { + writer.print("public void traverse"). + print("(").print(type).print(" trees)").lbrace(); + writer.print("for (int i = 0; i < trees.length; i++) "). + println("traverse(trees[i]);"); + writer.rbrace(); + writer.line(); + } + //######################################################################## } diff --git a/sources/meta/scalac/ast/MetaTree.java b/sources/meta/scalac/ast/MetaTree.java index 461ec184f5..a8b5979636 100644 --- a/sources/meta/scalac/ast/MetaTree.java +++ b/sources/meta/scalac/ast/MetaTree.java @@ -16,20 +16,22 @@ public class MetaTree extends AbstractTreeExpander { // Public Methods public void printEmptyArrays() { - writer.print("public static final "); - writer.print(tree.t_Trees.toString()); - writer.print(" EMPTY_ARRAY = new ").print(tree.NAME).println("[0];"); + printEmptyArrays(tree.getType(0), "EMPTY", tree.arrays); for (int i = 0; i < tree.nodes.length; i++) { TreeNode node = tree.nodes[i]; - for (int j = 1; j <= node.arrays; j++) { - writer.print("public static final "); - writer.print(node.getType(j).toString()); - writer.space().print(node.name).print("_EMPTY"); - for (int k = 0; k < j; k++) writer.print("_ARRAY"); - writer.print(" = new ").print(node.name).print("[0]"); - for (int k = 1; k < j; k++) writer.print("[]"); - writer.println(";"); - } + 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(";"); } } @@ -46,39 +48,38 @@ public class MetaTree extends AbstractTreeExpander { "- introduced by: " + node.start.name, "- eliminated by: " + node.stop.name, }); - writer.print("public case ").print(node.name); + node.printDecl(writer.print("public case "), null, false); if (node.fields != null) { - node.printParams(writer.print("(")).print(")"); writer.lbrace(); writer.println("assert CheckTreeNodes.instance.checkNode(this);"); writer.rbrace(); - } else if (node == tree.n_Empty) { - writer.print("; static { "+node.name+".type = "). - print(tree.t_Type).println(".NoType; }"); } 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 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 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()); + default : throw new Error(kind.getClass().getName()); } } private String description(TreeSymbol symbol) { switch (symbol) { - case NoSym : return "this tree has no symbol"; - case HasSym: return "this tree references a symbol"; - case DefSym: return "this tree defines a symbol"; - default : throw new Error(symbol.getClass().getName()); + 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()); } } @@ -88,9 +89,9 @@ public class MetaTree extends AbstractTreeExpander { } 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 + "()"); - writer.lbrace(); + 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++) @@ -136,63 +137,75 @@ public class MetaTree extends AbstractTreeExpander { } public void printExtClasses() { - for (int i = 0; i < tree.nodes.length; i++) - printExtTreeNode(tree.nodes[i]); + for (int i = 0;i < tree.nodes.length;i++) printExtClass(tree.nodes[i]); } - private void printExtTreeNode(TreeNode node) { - if (node.symbol == TreeSymbol.NoSym) return; - writer.print("public static class Ext"+node+" extends "+node); - writer.lbrace(); - writer.print("private ").print(tree.t_Symbol).println(" symbol;"); + 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(); - node.printParams(writer.print("public Ext"+node.name+"(")).print(")"); - writer.lbrace(); - node.printArgs(writer.print("super(")).println(");"); - writer.rbrace(); + printExtConstructor(node, false); + printExtConstructor(node, true); writer.println(); - writer.print("public boolean hasSymbol()"); - writer.lbrace(); + writer.print("public boolean hasSymbol()").lbrace(); writer.println("return true;"); writer.rbrace(); writer.println(); - if (node.symbol == TreeSymbol.DefSym) { - writer.print("public boolean definesSymbol()"); - writer.lbrace(); + if (node.definesSymbol()) { + writer.print("public boolean definesSymbol()").lbrace(); writer.println("return true;"); writer.rbrace(); writer.println(); } - writer.print("public ").print(tree.t_Symbol).print(" symbol()"); + writer.print("public ").print(symbol.type).print(" symbol()"); writer.lbrace(); - writer.println("return symbol;"); + writer.println("return " + symbol + ";"); writer.rbrace(); writer.println(); - writer.print("public Tree setSymbol(").print(tree.t_Symbol).print(" symbol)"); - writer.lbrace(); - writer.println("this.symbol = symbol;"); - if (node.hasLinkedFields()) { - writer.print("if (symbol != null)").lbrace(); - for (int i = 0; i < node.fields.length; i++) { - TreeField field = node.fields[i]; - TreeFieldLink link = field.link; - if (link == null) continue; - writer.println("this."+field+" = symbol."+link.getLink()+";"); - } - writer.rbrace(); + 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.println(); 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 + ";"); + } + //######################################################################## } diff --git a/sources/meta/scalac/ast/MetaTreeCopier.java b/sources/meta/scalac/ast/MetaTreeCopier.java index 5ee43f4f90..785c3a99da 100644 --- a/sources/meta/scalac/ast/MetaTreeCopier.java +++ b/sources/meta/scalac/ast/MetaTreeCopier.java @@ -13,9 +13,13 @@ public class MetaTreeCopier extends AbstractTreeMethodExpander { //######################################################################## // Public Methods - public void printTreeMethod(TreeNode node) { - printTreeMethodHeader(node, tree.t_Tree + " tree"); + public void printTreeMethod(TreeNode node, boolean withSymbol) { + node.printMethod(writer,tree.getFormal("tree"),withSymbol); writer.println(";"); + if (withSymbol && node.hasLinkedFields()) { + node.printMethod(writer, tree.getFormal("tree"), false, true); + writer.println(";"); + } } //######################################################################## diff --git a/sources/meta/scalac/ast/MetaTreeFactory.java b/sources/meta/scalac/ast/MetaTreeFactory.java index f7222b402b..28b1f381c9 100644 --- a/sources/meta/scalac/ast/MetaTreeFactory.java +++ b/sources/meta/scalac/ast/MetaTreeFactory.java @@ -13,9 +13,8 @@ public class MetaTreeFactory extends AbstractTreeMethodExpander { //######################################################################## // Public Methods - public void printTreeMethod(TreeNode node) { - printTreeMethodHeader(node, "int pos"); - writer.println(";"); + public void printTreeMethod(TreeNode node, boolean withSymbol) { + node.printMethod(writer, "int pos", withSymbol).println(";"); } //######################################################################## diff --git a/sources/meta/scalac/ast/Tree.java b/sources/meta/scalac/ast/Tree.java index c06f3c6437..a7a933069b 100644 --- a/sources/meta/scalac/ast/Tree.java +++ b/sources/meta/scalac/ast/Tree.java @@ -18,32 +18,53 @@ import meta.scalac.Phase; public class Tree { //######################################################################## - // Public Constants + // Private Fields - public static final String PACKAGE = "scalac.ast"; - public static final String NAME = "Tree"; + private final TreeKind + Any = TreeKind.Any, + Type = TreeKind.Type, + Term = TreeKind.Term, + Dual = TreeKind.Dual, + Test = TreeKind.Test, + None = TreeKind.None; - //######################################################################## - // Private Constants + private final Type + t_int = TreeType.INT, + t_Object = TreeType.Reference(null, "Object"), + t_Global = TreeType.Reference("scalac", "Global"), + t_Unit = TreeType.Reference("scalac", "Unit"), + t_TreeGen = TreeType.Reference("scalac.ast", "TreeGen"), + t_Symbol = TreeType.Reference("scalac.symtab", "Symbol"), + t_Type = TreeType.Reference("scalac.symtab", "Type"), + t_Name = TreeType.Name(Any), + t_TypeName = TreeType.Name(Type), + t_TermName = TreeType.Name(Term), + t_TestName = TreeType.Name(Test), + t_Names = TreeType.Array(t_Name), + t_Tree = getType(0), + t_TypeTree = getType(0, Type), + t_TermTree = getType(0, Term), + t_Trees = getType(1), + t_TypeTrees = getType(1, Type), + t_TermTrees = getType(1, Term); - private static final TreeKind Any = TreeKind.Any; - private static final TreeKind Type = TreeKind.Type; - private static final TreeKind Term = TreeKind.Term; - private static final TreeKind Dual = TreeKind.Dual; - private static final TreeKind Test = TreeKind.Test; - private static final TreeKind None = TreeKind.None; + private final TreeFieldLink + SymFlags = TreeFieldLink.SymFlags, + SymName = TreeFieldLink.SymName; - private static final TreeSymbol NoSym = TreeSymbol.NoSym; - private static final TreeSymbol HasSym = TreeSymbol.HasSym; - private static final TreeSymbol DefSym = TreeSymbol.DefSym; + private final TreeField + tree_symbol = new TreeField(t_Symbol, "symbol"); - private static final TreeFieldLink SymFlags = TreeFieldLink.SymFlags; - private static final TreeFieldLink SymName = TreeFieldLink.SymName; + private final TreeSymbol + NoSym = TreeSymbol.NoSym, + HasSym = TreeSymbol.HasSym(tree_symbol, false), + DefSym = TreeSymbol.HasSym(tree_symbol, true); - private final List list = new ArrayList(); + private final List list + = new ArrayList(); //######################################################################## - // Public Constants + // Public Fields public final TreeNode n_Bad = node("Bad" , Any , HasSym), @@ -57,7 +78,7 @@ public class Tree { n_TypeDef = node("TypeDef" , None, DefSym), n_Import = node("Import" , None, HasSym), n_CaseDef = node("CaseDef" , None, NoSym), - n_Template = node("Template" , None, HasSym), // !!! HasSym + n_Template = node("Template" , None, HasSym), n_LabelDef = node("LabelDef" , Term, DefSym), n_Block = node("Block" , Term, NoSym), n_Tuple = node("Tuple" , Term, NoSym), @@ -83,38 +104,7 @@ public class Tree { n_CovariantType = node("CovariantType" , Type, NoSym); public final TreeNode[] nodes; - - public final Type - t_int = TreeType.INT, - t_Object = TreeType.Reference(null, "Object"), - t_Global = TreeType.Reference("scalac", "Global"), - t_Unit = TreeType.Reference("scalac", "Unit"), - t_TreeGen = TreeType.Reference("scalac.ast", "TreeGen"), - t_Symbol = TreeType.Reference("scalac.symtab", "Symbol"), - t_Type = TreeType.Reference("scalac.symtab", "Type"), - t_Debug = TreeType.Reference("scalac.util", "Debug"), - t_Name = TreeType.Name(Any), - t_TypeName = TreeType.Name(Type), - t_TermName = TreeType.Name(Term), - t_TestName = TreeType.Name(Test), - t_Names = TreeType.Array(t_Name), - t_Tree = TreeType.Tree(Any), - t_TypeTree = TreeType.Tree(Type), - t_TermTree = TreeType.Tree(Term), - t_Trees = TreeType.Array(t_Tree), - t_TypeTrees = TreeType.Array(t_TypeTree), - t_TermTrees = TreeType.Array(t_TermTree), - t_Treess = TreeType.Array(t_Trees), - t_ValDef = n_ValDef.getType(0), - t_ValDefs = n_ValDef.getType(1), - t_ValDefss = n_ValDef.getType(2), - t_TypeDef = n_TypeDef.getType(0), - t_TypeDefs = n_TypeDef.getType(1), - t_Template = n_Template.getType(0), - t_CaseDef = n_CaseDef.getType(0), - t_CaseDefs = n_CaseDef.getType(1), - t_Ident = n_Ident.getType(0), - t_Idents = n_Ident.getType(1); + public int arrays; //######################################################################## // Public Constructors @@ -136,16 +126,16 @@ public class Tree { setRange(Phase.PARSER, Phase.END). addField(t_int, "mods", SymFlags). addField(t_TypeName, "name", SymName). - addField(t_TypeDefs, "tparams"). - addField(t_ValDefss, "vparams"). + addField(n_TypeDef.getType(1), "tparams"). + addField(n_ValDef.getType(2), "vparams"). addField(t_TypeTree, "tpe"). - addField(t_Template, "impl"); + addField(n_Template.getType(0), "impl"); n_PackageDef. setDescription("Package declaration"). setRange(Phase.PARSER, Phase.UNKNOWN). addField(t_TermTree, "packaged"). - addField(t_Template, "impl"); + addField(n_Template.getType(0), "impl"); n_ModuleDef. setDescription("Module declaration"). @@ -153,7 +143,7 @@ public class Tree { addField(t_int, "mods", SymFlags). addField(t_TermName, "name", SymName). addField(t_TypeTree, "tpe"). - addField(t_Template, "impl"); + addField(n_Template.getType(0), "impl"); n_ValDef. setDescription("Value declaration (var or let)"). @@ -176,8 +166,8 @@ public class Tree { setRange(Phase.PARSER, Phase.END). addField(t_int, "mods", SymFlags). addField(t_TermName, "name", SymName). - addField(t_TypeDefs, "tparams"). - addField(t_ValDefss, "vparams"). + addField(n_TypeDef.getType(1), "tparams"). + addField(n_ValDef.getType(2), "vparams"). addField(t_TypeTree, "tpe"). addField(t_TermTree, "rhs"); @@ -227,13 +217,13 @@ public class Tree { n_Visitor. setDescription("Visitor (a sequence of cases)"). setRange(Phase.PARSER, Phase.TRANSMATCH). - addField(t_CaseDefs, "cases"); + addField(n_CaseDef.getType(1), "cases"); n_Function. setDescription("Anonymous function"). setRange(Phase.PARSER, Phase.ANALYZER). - addField(t_ValDefs, "vparams"). + addField(n_ValDef.getType(1), "vparams"). addField(t_TermTree, "body"); n_Assign. @@ -252,7 +242,7 @@ public class Tree { n_New. setDescription("Instantiation"). setRange(Phase.PARSER, Phase.END). - addField(t_Template, "templ"); + addField(n_Template.getType(0), "templ"); n_Typed. setDescription("Type annotation"). @@ -355,6 +345,22 @@ public class Tree { } //######################################################################## + // Public Methods + + public Type getType(int rank) { + return getType(rank, Any); + } + + public Type getType(int rank, TreeKind kind) { + arrays = Math.max(arrays , rank); + return rank==0 ? TreeType.Tree(kind) : TreeType.Array(getType(rank-1)); + } + + public String getFormal(String name) { + return getType(0) + " " + name; + } + + //######################################################################## // Private Methods private TreeNode node(String name, TreeKind kind, TreeSymbol symbol) { diff --git a/sources/meta/scalac/ast/TreeField.java b/sources/meta/scalac/ast/TreeField.java index f27c8c1698..19df9cb42e 100644 --- a/sources/meta/scalac/ast/TreeField.java +++ b/sources/meta/scalac/ast/TreeField.java @@ -9,6 +9,7 @@ package meta.scalac.ast; import meta.java.Type; +import meta.java.JavaWriter; /** This class describes a tree node field. */ public class TreeField { @@ -23,6 +24,10 @@ public class TreeField { //######################################################################## // Public Constructors + public TreeField(Type type, String name) { + this(type, name, null); + } + public TreeField(Type type, String name, TreeFieldLink link) { this.type = type; this.name = name; @@ -30,8 +35,26 @@ public class TreeField { } //######################################################################## + // Public Function + + public static JavaWriter print(JavaWriter writer, TreeField[] fields, + boolean withType) + { + for (int i = 0; i < fields.length; i++) { + if (i > 0) writer.print(", "); + fields[i].print(writer, withType); + } + return writer; + } + + //######################################################################## // Public Methods + public JavaWriter print(JavaWriter writer, boolean withType) { + if (withType) writer.print(type).space(); + return writer.print(name); + } + public String toString() { return name; } diff --git a/sources/meta/scalac/ast/TreeFieldLink.java b/sources/meta/scalac/ast/TreeFieldLink.java index b338ccd6eb..62e8dcfb10 100644 --- a/sources/meta/scalac/ast/TreeFieldLink.java +++ b/sources/meta/scalac/ast/TreeFieldLink.java @@ -8,6 +8,8 @@ package meta.scalac.ast; +import meta.java.JavaWriter; + /** * This class describes the possible links between a given field of a * tree and the symbol of that tree. @@ -50,5 +52,9 @@ public class TreeFieldLink { } } + public JavaWriter print(JavaWriter writer, TreeField symbol) { + return writer.print(symbol.name).print('.').print(getLink()); + } + //######################################################################## } diff --git a/sources/meta/scalac/ast/TreeNode.java b/sources/meta/scalac/ast/TreeNode.java index 5a985bc3ae..340eaa8109 100644 --- a/sources/meta/scalac/ast/TreeNode.java +++ b/sources/meta/scalac/ast/TreeNode.java @@ -44,30 +44,7 @@ public class TreeNode { } //######################################################################## - // Public Methods - - public boolean hasExtClass() { - return hasSymbol(); - } - - public boolean hasSymbol() { - return symbol == symbol.HasSym || symbol == symbol.DefSym; - } - - public boolean hasLinkedFields() { - for (int i = 0; i < fields.length; i++) - if (fields[i].link != null) return true; - return false; - } - - public boolean definesSymbol() { - return symbol == symbol.DefSym; - } - - public Type getType(int rank) { - arrays = Math.max(arrays , rank); - return rank == 0 ? TreeType.Node(this) : Type.Array(getType(rank - 1)); - } + // Public Methods - Initializing public TreeNode setDescription(String description) { this.description = description; @@ -90,9 +67,8 @@ public class TreeNode { } public TreeNode addField(Type type, String name, TreeFieldLink link) { - if (link != null) - if (symbol != TreeSymbol.HasSym && symbol != TreeSymbol.DefSym) - throw new Error("node "+this+" may not have linked fields"); + if (link != null && !hasSymbol()) + throw new Error("node "+this+" may not have linked fields"); TreeField[] array = new TreeField[fields.length + 1]; for (int i = 0; i < fields.length; i++) array[i] = fields[i]; array[fields.length] = new TreeField(type, name, link); @@ -100,52 +76,60 @@ public class TreeNode { return this; } - public JavaWriter printCase(JavaWriter writer, boolean wildcards) { - writer.print("case ").print(name); - if (fields!=null) printParams(writer.print("("),wildcards).print(")"); - return writer.print(":").space(); - } + //######################################################################## + // Public Methods - Querying - public JavaWriter printParams(JavaWriter writer) { - return printParams(writer, false); + public Type getType(int rank) { + arrays = Math.max(arrays , rank); + return rank == 0 ? TreeType.Node(this) : Type.Array(getType(rank - 1)); } - public JavaWriter printParams(JavaWriter writer, String prefix) { - return printParams(printPrefix(writer, prefix)); + public boolean hasExtClass() { + return hasSymbol(); } - public JavaWriter printParams(JavaWriter writer, boolean wildcards) { - if (fields != null) for (int i = 0; i < fields.length; i++) { - if (i > 0) writer.print(", "); - if (wildcards) writer.print("_"); - else writer.print(fields[i].type).space().print(fields[i].name); + public boolean hasSymbol() { + switch (symbol) { + case TreeSymbol.HasSym(_, _): + return true; + default: + return false; } - return writer; } - public JavaWriter printArgs(JavaWriter writer) { - if (fields != null) for (int i = 0; i < fields.length; i++) { - if (i > 0) writer.print(", "); - writer.print(fields[i].name); + public boolean definesSymbol() { + switch (symbol) { + case TreeSymbol.HasSym(_, true): + return true; + default: + return false; } - return writer; } - public JavaWriter printArgs(JavaWriter writer, String prefix) { - return printArgs(printPrefix(writer, prefix)); + public TreeField getSymbol() { + switch (symbol) { + case TreeSymbol.HasSym(TreeField field, _): + return field; + default: + return null; + } } - public JavaWriter printPrefix(JavaWriter writer, String prefix) { - if (prefix != null) { - writer.print(prefix); - if (fields != null && fields.length > 0) writer.print(", "); - } - return writer; + public boolean hasLinkedFields() { + for (int i = 0; i < fields.length; i++) + if (fields[i].link != null) return true; + return false; } - public JavaWriter printNew(JavaWriter writer) { - String classname = (hasExtClass() ? "Ext" : "") + name; - return printArgs(writer.print("new " + classname + "(")).print(")"); + public TreeField[] getFields(boolean withoutLinkedFields) { + if (fields == null || !withoutLinkedFields) return fields; + int count = 0;; + for (int i = 0; i < fields.length; i++) + if (fields[i].link == null) count++; + TreeField[] array = new TreeField[count]; + for (int i = 0, j = 0; i < fields.length; i++) + if (fields[i].link == null) array[j++] = fields[i]; + return array; } public String toString() { @@ -153,4 +137,95 @@ public class TreeNode { } //######################################################################## + // Public Methods - Printing + + public JavaWriter printCase(JavaWriter writer, boolean withWildcards) { + writer.print("case "); + if (fields != null && withWildcards) { + writer.print(name).print('('); + for (int i = 0; i < fields.length; i++) { + if (i > 0) writer.print(", "); + writer.print("_"); + } + writer.print(')'); + } else { + printDecl(writer, null, false); + } + return writer.print(":").space(); + } + + public JavaWriter printNew(JavaWriter writer, boolean withSymbol) { + writer.print("new "); + if (hasExtClass()) writer.print("Ext"); + return printCall(writer, null, withSymbol); + } + + public JavaWriter printMethod(JavaWriter writer, String prefix, + boolean withSymbol) + { + return printMethod(writer, prefix, withSymbol, withSymbol); + } + + public JavaWriter printMethod(JavaWriter writer, String prefix, + boolean withSymbol, boolean withoutLinkedFields) + { + writer.print("public ").print(name).space(); + return printDecl(writer, prefix, withSymbol, withoutLinkedFields); + } + + public JavaWriter printDecl(JavaWriter writer, String prefix, + boolean withSymbol) + { + return printDecl(writer, prefix, withSymbol, withSymbol); + } + + public JavaWriter printDecl(JavaWriter writer, String prefix, + boolean withSymbol, boolean withoutLinkedFields) + { + return printPattern(writer,true,prefix,withSymbol,withoutLinkedFields); + } + + public JavaWriter printCall(JavaWriter writer, String prefix, + boolean withSymbol) + { + return printCall(writer, prefix, withSymbol, withSymbol); + } + + public JavaWriter printCall(JavaWriter writer, String prefix, + boolean withSymbol, boolean withoutLinkedFields) + { + return printPattern( + writer, false, prefix, withSymbol, withoutLinkedFields); + } + + public JavaWriter printPattern(JavaWriter writer, boolean withType, + String prefix, boolean withSymbol, boolean withoutLinkedFields) + { + writer.print(name); + if (fields != null || prefix != null || withSymbol) { + writer.print('('); + printFields(writer,withType,prefix,withSymbol,withoutLinkedFields); + writer.print(')'); + } + return writer; + } + + public JavaWriter printFields(JavaWriter writer, boolean withType, + String prefix, boolean withSymbol, boolean withoutLinkedFields) + { + TreeField[] fields = getFields(withoutLinkedFields); + if (prefix != null) { + writer.print(prefix); + if (withSymbol || (fields != null && fields.length > 0)) + writer.print(", "); + } + if (withSymbol) { + getSymbol().print(writer, withType); + if (fields != null && fields.length > 0) writer.print(", "); + } + TreeField.print(writer, fields, withType); + return writer; + } + + //######################################################################## } diff --git a/sources/meta/scalac/ast/TreeSymbol.java b/sources/meta/scalac/ast/TreeSymbol.java index 8040239800..1e53be4013 100644 --- a/sources/meta/scalac/ast/TreeSymbol.java +++ b/sources/meta/scalac/ast/TreeSymbol.java @@ -18,10 +18,7 @@ public class TreeSymbol { public case NoSym; /** Indicates the presence of a symbol. */ - public case HasSym; - - /** Indicates the presence of a symbol that is define here. */ - public case DefSym; + public case HasSym(TreeField field, boolean isDef); //######################################################################## } diff --git a/sources/meta/scalac/ast/TreeType.java b/sources/meta/scalac/ast/TreeType.java index f15e7ec676..52c2e6d9b3 100644 --- a/sources/meta/scalac/ast/TreeType.java +++ b/sources/meta/scalac/ast/TreeType.java @@ -14,15 +14,14 @@ import meta.java.Type; public class TreeType extends Type { //######################################################################## - // Public Constants + // Private Constants - // !!! - public static final String NAME_PACKAGE = "scalac.util"; - public static final String NAME_NAME = "Name"; - public static final String NAME_FULLNAME = NAME_PACKAGE + "." + NAME_NAME; - public static final String TREE_PACKAGE = meta.scalac.ast.Tree.PACKAGE; - public static final String TREE_NAME = meta.scalac.ast.Tree.NAME; - public static final String TREE_FULLNAME = TREE_PACKAGE + "." + TREE_NAME; + private static final String NAME_PACKAGE = "scalac.util"; + private static final String NAME_NAME = "Name"; + private static final String NAME_FULLNAME = NAME_PACKAGE + "." + NAME_NAME; + private static final String TREE_PACKAGE = "scalac.ast"; + private static final String TREE_NAME = "Tree"; + private static final String TREE_FULLNAME = TREE_PACKAGE + "." + TREE_NAME; //######################################################################## // Public Cases |