summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpaltherr <paltherr@epfl.ch>2003-07-21 15:57:52 +0000
committerpaltherr <paltherr@epfl.ch>2003-07-21 15:57:52 +0000
commita146e0762d9b134c1a2300691349f65f7ed04ac1 (patch)
tree08621f29939ebf0f5adac67692be83d0f96750a8
parent9b09c3e8d9f05e69764225be7516df6e5e578b9d (diff)
downloadscala-a146e0762d9b134c1a2300691349f65f7ed04ac1.tar.gz
scala-a146e0762d9b134c1a2300691349f65f7ed04ac1.tar.bz2
scala-a146e0762d9b134c1a2300691349f65f7ed04ac1.zip
- Added TreeSymbolCloner.java
-rw-r--r--sources/scalac/ast/TreeSymbolCloner.java103
1 files changed, 103 insertions, 0 deletions
diff --git a/sources/scalac/ast/TreeSymbolCloner.java b/sources/scalac/ast/TreeSymbolCloner.java
new file mode 100644
index 0000000000..70b1b236c3
--- /dev/null
+++ b/sources/scalac/ast/TreeSymbolCloner.java
@@ -0,0 +1,103 @@
+/* ____ ____ ____ ____ ______ *\
+** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
+** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
+** /_____/\____/\___/\____/____/ **
+\* */
+
+// $Id$
+
+package scalac.ast;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import scalac.symtab.Symbol;
+import scalac.symtab.SymbolCloner;
+import scalac.util.Name;
+import scalac.util.Debug;
+
+/**
+ * This class implements a tree traverser that clones specified
+ * symbols it encounters.
+ */
+public class TreeSymbolCloner extends Traverser {
+
+ //########################################################################
+ // Private Fields
+
+ // !!! replace Idents in patterns by ValDefs and remove this field
+ /** Indicates whether we are in a pattern */
+ private boolean inPattern = false;
+
+ //########################################################################
+ // Public Fields
+
+ /** The symbol cloner used to clone symbols */
+ public final SymbolCloner cloner;
+
+ //########################################################################
+ // Public Constructors
+
+ /** Initializes a new instance. */
+ public TreeSymbolCloner(SymbolCloner cloner) {
+ this.cloner = cloner;
+ }
+
+ //########################################################################
+ // Public Methods
+
+ /**
+ * Returns true iff the symbol of the given tree symbol must be
+ * cloned. The default implementation returns true iff the tree
+ * defines a symbol and that symbol hasn't been cloned yet.
+ */
+ public boolean mustCloneSymbolOf(Tree tree) {
+ // !!! replace Idents in patterns by ValDefs and remove this switch
+ switch (tree) {
+ case Ident(Name name):
+ if (!inPattern || !name.isVariable()) return false; else break;
+ default:
+ if (!tree.definesSymbol()) return false; else break;
+ }
+ return !cloner.clones.containsKey(tree.symbol());
+ }
+
+ /**
+ * Clones the given symbol. The default implementation invokes
+ * getOwnerOfCloneOf to obtain the owner of the cloned symbol and
+ * then invokes the symbol cloner to clone the symbol.
+ */
+ public Symbol cloneSymbol(Symbol symbol) {
+ return cloner.cloneSymbol(symbol);
+ }
+
+ /**
+ * Traverses the tree and clones symbols. The default
+ * implemenation invokes cloneSymbol with the symbol of every tree
+ * nodes for which mustCloneSymbolOf returns true.
+ */
+ public void traverse(Tree tree) {
+ if (mustCloneSymbolOf(tree)) cloneSymbol(tree.symbol());
+ // !!!replace Idents in patterns by ValDefs and remove this switch
+ switch (tree) {
+ case PatDef(_, Tree pat, Tree rhs):
+ inPattern = true;
+ traverse(pat);
+ inPattern = false;
+ traverse(rhs);
+ return;
+ case CaseDef(Tree pat, Tree guard, Tree body):
+ inPattern = true;
+ traverse(pat);
+ inPattern = false;
+ traverse(guard);
+ traverse(body);
+ return;
+ default:
+ super.traverse(tree);
+ return;
+ }
+ }
+
+ //########################################################################
+}