Skip to content

Commit 34b7d35

Browse files
committed
New functionality: changeOwnerAfter
Changes owners after a phase without copying the tree. This should be more suitable for the changeOwner operations used in the tree transforms so far, which are linear, i.e. no tree duplication is needed.
1 parent 8c22e76 commit 34b7d35

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import transform.SymUtils._
66
import core._
77
import util.Positions._, Types._, Contexts._, Constants._, Names._, Flags._
88
import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._, Symbols._
9-
import Denotations._, Decorators._
9+
import Denotations._, Decorators._, DenotTransformers._
1010
import config.Printers._
1111
import typer.Mode
1212
import collection.mutable
@@ -537,6 +537,28 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
537537
loop(from, Nil, to :: Nil)
538538
}
539539

540+
/** After phase `trans`, set the owner of every definition in this tree that was formerly
541+
* owner by `from` to `to`.
542+
*/
543+
def changeOwnerAfter(from: Symbol, to: Symbol, trans: DenotTransformer)(implicit ctx: Context): ThisTree = {
544+
assert(ctx.phase == trans.next)
545+
val traverser = new TreeTraverser {
546+
def traverse(tree: Tree) = tree match {
547+
case tree: DefTree =>
548+
val sym = tree.symbol
549+
if (sym.denot(ctx.withPhase(trans)).owner == from) {
550+
println(i"change owner $from -> $to of $sym")
551+
sym.copySymDenotation(owner = to).installAfter(trans)
552+
}
553+
if (sym.isWeakOwner) traverseChildren(tree)
554+
case _ =>
555+
traverseChildren(tree)
556+
}
557+
}
558+
traverser.traverse(tree)
559+
tree
560+
}
561+
540562
def select(name: Name)(implicit ctx: Context): Select =
541563
Select(tree, name)
542564

0 commit comments

Comments
 (0)