diff options
author | paltherr <paltherr@epfl.ch> | 2003-04-14 15:51:50 +0000 |
---|---|---|
committer | paltherr <paltherr@epfl.ch> | 2003-04-14 15:51:50 +0000 |
commit | 217d42413b0d649ebabe40ac0cb43ec961727bf2 (patch) | |
tree | cc07b4b5f8eca1e123b379b3ac8fe598eecdd45d /sources/meta/scalac | |
parent | 79c7c73561244fb170a51c832631e6526b277a7a (diff) | |
download | scala-217d42413b0d649ebabe40ac0cb43ec961727bf2.tar.gz scala-217d42413b0d649ebabe40ac0cb43ec961727bf2.tar.bz2 scala-217d42413b0d649ebabe40ac0cb43ec961727bf2.zip |
- Added MetaLazyTreeCopier.java
- Added MetaStrictTreeCopier.java
- Added MetaTransformer.java
- Added MetaTraverser.java
- Added MetaTree.java
- Added MetaTreeCopier.java
Diffstat (limited to 'sources/meta/scalac')
-rw-r--r-- | sources/meta/scalac/ast/MetaLazyTreeCopier.java | 42 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaStrictTreeCopier.java | 28 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTransformer.java | 65 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTraverser.java | 25 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTree.java | 179 | ||||
-rw-r--r-- | sources/meta/scalac/ast/MetaTreeCopier.java | 27 |
6 files changed, 366 insertions, 0 deletions
diff --git a/sources/meta/scalac/ast/MetaLazyTreeCopier.java b/sources/meta/scalac/ast/MetaLazyTreeCopier.java new file mode 100644 index 0000000000..a3d770b0d1 --- /dev/null +++ b/sources/meta/scalac/ast/MetaLazyTreeCopier.java @@ -0,0 +1,42 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package meta.scalac.ast; + +public class MetaLazyTreeCopier extends AbstractTreeMethodExpander { + + //######################################################################## + // Public Methods + + public void printTreeMethod(TreeNode node) { + printTreeMethodHeader(node, tree.t_Tree + " tree"); + writer.lbrace(); + if (node.fields.length > 0) { + writer.println(node.name + " t = (" + node.name + ")tree;"); + 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); + } + writer.println(")"); + writer.println("return t;").undent(); + } else { + writer.print(node.name).space(); + } + + writer.print("t = make." + node.name + "("); + node.printArgs(writer, "tree.pos").println(");"); + writer.println("t.type = tree.type;"); + if (node.hasSymbol()) writer.println("t.setSymbol(tree.symbol());"); + writer.println("return t;"); + writer.rbrace(); + } + + //######################################################################## +} diff --git a/sources/meta/scalac/ast/MetaStrictTreeCopier.java b/sources/meta/scalac/ast/MetaStrictTreeCopier.java new file mode 100644 index 0000000000..9cc050396a --- /dev/null +++ b/sources/meta/scalac/ast/MetaStrictTreeCopier.java @@ -0,0 +1,28 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package meta.scalac.ast; + +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(");"); + writer.println("t.type = tree.type;"); + if (node.hasSymbol()) writer.println("t.setSymbol(tree.symbol());"); + writer.println("return t;"); + writer.rbrace(); + } + + //######################################################################## +} diff --git a/sources/meta/scalac/ast/MetaTransformer.java b/sources/meta/scalac/ast/MetaTransformer.java new file mode 100644 index 0000000000..c4f2794851 --- /dev/null +++ b/sources/meta/scalac/ast/MetaTransformer.java @@ -0,0 +1,65 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package meta.scalac.ast; + +import meta.java.Type; + +public class MetaTransformer extends AbstractTreeCaseExpander { + + //######################################################################## + // Public Methods + + public void printTreeCaseBody(TreeNode node) { + 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); + } + writer.println(");"); + } + } + + public void printTransformArrays() { + printTransformArrayOf(tree.t_Tree, false); + printTransformArrayOf(tree.t_Trees, 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); + } + + 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]")+";"); + 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("return res;"); + writer.rbrace(); + writer.rbrace(); + writer.println("return ts;"); + writer.rbrace(); + writer.line(); + } + + //######################################################################## +} diff --git a/sources/meta/scalac/ast/MetaTraverser.java b/sources/meta/scalac/ast/MetaTraverser.java new file mode 100644 index 0000000000..4e00b924c9 --- /dev/null +++ b/sources/meta/scalac/ast/MetaTraverser.java @@ -0,0 +1,25 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package meta.scalac.ast; + +public class MetaTraverser extends AbstractTreeCaseExpander { + + //######################################################################## + // Public Methods + + public void printTreeCaseBody(TreeNode node) { + if (node.fields != null) + for (int i = 0; i < node.fields.length; i++) + if (Tree.isTree(node.fields[i].type)) + writer.println("traverse(" + node.fields[i] + ");"); + writer.println("return;"); + } + + //######################################################################## +} diff --git a/sources/meta/scalac/ast/MetaTree.java b/sources/meta/scalac/ast/MetaTree.java new file mode 100644 index 0000000000..84089a708b --- /dev/null +++ b/sources/meta/scalac/ast/MetaTree.java @@ -0,0 +1,179 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ 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() { + writer.print("public static final "); + writer.print(tree.t_Trees.toString()); + writer.print(" EMPTY_ARRAY = new ").print(tree.NAME).println("[0];"); + 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(";"); + } + } + } + + 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, + }); + writer.print("public case ").print(node.name); + if (node.fields != null) { + node.printParams(writer.print("(")).print(")"); + writer.lbrace(); + writer.println("// !!! assert TreeChecker.instance.check(this);"); + writer.rbrace(); + } else if (node == tree.n_Empty) { + writer.print("; static { "+node.name+".type = "). + print(tree.t_Type).println(".NoType; }"); + } else { + writer.println(";"); + } + 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: return "this tree references a symbol"; + case DefSym: 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 + "()"); + writer.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 "); + if (kind != TreeKind.Type) writer.print("!"); + writer.println("name.isTypeName();").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++) + printExtTreeNode(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;"); + writer.println(); + + node.printParams(writer.print("public Ext"+node.name+"(")).print(")"); + writer.lbrace(); + node.printArgs(writer.print("super(")).println(");"); + writer.rbrace(); + writer.println(); + + writer.print("public boolean hasSymbol()"); + writer.lbrace(); + writer.println("return true;"); + writer.rbrace(); + writer.println(); + + if (node.symbol == TreeSymbol.DefSym) { + writer.print("public boolean definesSymbol()"); + writer.lbrace(); + writer.println("return true;"); + writer.rbrace(); + writer.println(); + } + + writer.print("public ").print(tree.t_Symbol).print(" symbol()"); + writer.lbrace(); + 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;"); + writer.println("return this;"); + writer.rbrace(); + writer.println(); + + writer.rbrace(); + writer.println(); + } + + //######################################################################## +} diff --git a/sources/meta/scalac/ast/MetaTreeCopier.java b/sources/meta/scalac/ast/MetaTreeCopier.java new file mode 100644 index 0000000000..699cfd1c94 --- /dev/null +++ b/sources/meta/scalac/ast/MetaTreeCopier.java @@ -0,0 +1,27 @@ +/* ____ ____ ____ ____ ______ *\ +** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala ** +** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL ** +** /_____/\____/\___/\____/____/ ** +\* */ + +// $Id$ + +package meta.scalac.ast; + +public class MetaTreeCopier extends AbstractTreeMethodExpander { + + //######################################################################## + // Public Methods + + // !!! + public String getName() { + return "TreeCopyFactory"; + } + + public void printTreeMethod(TreeNode node) { + printTreeMethodHeader(node, tree.t_Tree + " tree"); + writer.println(";"); + } + + //######################################################################## +} |