Skip to content

Commit

Permalink
adds Trees.replace(Tree, Tree)
Browse files Browse the repository at this point in the history
Currently dead code. Has proven to be useful to implement type macros,
therefore I'm moving it to scala.reflect.internal.Trees.
  • Loading branch information
xeno-by committed Jan 9, 2013
1 parent 7550799 commit 5660b7a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/compiler/scala/tools/nsc/interactive/Global.scala
Expand Up @@ -1031,15 +1031,6 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "")

// ---------------- Helper classes ---------------------------

/** A transformer that replaces tree `from` with tree `to` in a given tree */
class TreeReplacer(from: Tree, to: Tree) extends Transformer {
override def transform(t: Tree): Tree = {
if (t == from) to
else if ((t.pos includes from.pos) || t.pos.isTransparent) super.transform(t)
else t
}
}

/** The typer run */
class TyperRun extends Run {
// units is always empty
Expand Down
13 changes: 13 additions & 0 deletions src/reflect/scala/reflect/internal/Trees.scala
Expand Up @@ -164,6 +164,9 @@ trait Trees extends api.Trees { self: SymbolTable =>
override def substituteThis(clazz: Symbol, to: Tree): Tree =
new ThisSubstituter(clazz, to) transform this

def replace(from: Tree, to: Tree): Tree =
new TreeReplacer(from, to, positionAware = false) transform this

def hasSymbolWhich(f: Symbol => Boolean) =
(symbol ne null) && (symbol ne NoSymbol) && f(symbol)

Expand Down Expand Up @@ -1381,6 +1384,16 @@ trait Trees extends api.Trees { self: SymbolTable =>
if (tree eq orig) super.transform(tree)
else tree
}

/** A transformer that replaces tree `from` with tree `to` in a given tree */
class TreeReplacer(from: Tree, to: Tree, positionAware: Boolean) extends Transformer {
override def transform(t: Tree): Tree = {
if (t == from) to
else if (!positionAware || (t.pos includes from.pos) || t.pos.isTransparent) super.transform(t)
else t
}
}

// Create a readable string describing a substitution.
private def substituterString(fromStr: String, toStr: String, from: List[Any], to: List[Any]): String = {
"subst[%s, %s](%s)".format(fromStr, toStr, (from, to).zipped map (_ + " -> " + _) mkString ", ")
Expand Down

0 comments on commit 5660b7a

Please sign in to comment.