diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2012-12-25 02:37:31 +0100 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2013-01-05 01:39:15 +0300 |
commit | 348c8fac9f897f9661f84e32949b8a4e0c99e93a (patch) | |
tree | 810b2d7d53451ceba5e43e1687a86cdedffd39f1 /src/reflect/scala/reflect/internal/TreeGen.scala | |
parent | ed4f4798df2ac104d9557de2241d8e89283b0662 (diff) | |
download | scala-348c8fac9f897f9661f84e32949b8a4e0c99e93a.tar.gz scala-348c8fac9f897f9661f84e32949b8a4e0c99e93a.tar.bz2 scala-348c8fac9f897f9661f84e32949b8a4e0c99e93a.zip |
adds c.introduceTopLevel
The first in the family of mutators for the global symbol table,
`introduceTopLevel` is capable of creating synthetic top-level
classes and modules.
The addition of nme.EMPTY_PACKAGE_NAME is necessary to let
programmers insert definitions into the empty package. That's explicitly
discouraged in the docs, but at times might come in handy.
This patch introduce workarounds to avoid incompatibilities with SBT.
First of all SBT doesn't like VirtualFiles having JFile set to null.
Secondly SBT gets confused when someone depends on synthetic files
added by c.introduceTopLevel.
Strictly speaking these problems require changes to SBT, and that will be
done later. However the main target of the patch is paradise/macros,
which needs to be useful immediately, therefore we apply workarounds.
Diffstat (limited to 'src/reflect/scala/reflect/internal/TreeGen.scala')
-rw-r--r-- | src/reflect/scala/reflect/internal/TreeGen.scala | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/reflect/scala/reflect/internal/TreeGen.scala b/src/reflect/scala/reflect/internal/TreeGen.scala index 0954432c77..6a2006e56f 100644 --- a/src/reflect/scala/reflect/internal/TreeGen.scala +++ b/src/reflect/scala/reflect/internal/TreeGen.scala @@ -113,7 +113,7 @@ abstract class TreeGen extends macros.TreeBuilder { } /** Builds a reference to given symbol with given stable prefix. */ - def mkAttributedRef(pre: Type, sym: Symbol): Tree = { + def mkAttributedRef(pre: Type, sym: Symbol): RefTree = { val qual = mkAttributedQualifier(pre) qual match { case EmptyTree => mkAttributedIdent(sym) @@ -123,10 +123,17 @@ abstract class TreeGen extends macros.TreeBuilder { } /** Builds a reference to given symbol. */ - def mkAttributedRef(sym: Symbol): Tree = + def mkAttributedRef(sym: Symbol): RefTree = if (sym.owner.isClass) mkAttributedRef(sym.owner.thisType, sym) else mkAttributedIdent(sym) + def mkUnattributedRef(sym: Symbol): RefTree = mkUnattributedRef(sym.fullNameAsName('.')) + + def mkUnattributedRef(fullName: Name): RefTree = { + val hd :: tl = nme.segments(fullName.toString, assumeTerm = fullName.isTermName) + tl.foldLeft(Ident(hd): RefTree)(Select(_,_)) + } + /** Replaces tree type with a stable type if possible */ def stabilize(tree: Tree): Tree = stableTypeFor(tree) match { case Some(tp) => tree setType tp @@ -153,13 +160,13 @@ abstract class TreeGen extends macros.TreeBuilder { def mkAttributedStableRef(sym: Symbol): Tree = stabilize(mkAttributedRef(sym)) - def mkAttributedThis(sym: Symbol): Tree = + def mkAttributedThis(sym: Symbol): This = This(sym.name.toTypeName) setSymbol sym setType sym.thisType - def mkAttributedIdent(sym: Symbol): Tree = + def mkAttributedIdent(sym: Symbol): RefTree = Ident(sym.name) setSymbol sym setType sym.tpeHK - def mkAttributedSelect(qual: Tree, sym: Symbol): Tree = { + def mkAttributedSelect(qual: Tree, sym: Symbol): RefTree = { // Tests involving the repl fail without the .isEmptyPackage condition. if (qual.symbol != null && (qual.symbol.isEffectiveRoot || qual.symbol.isEmptyPackage)) mkAttributedIdent(sym) @@ -283,4 +290,8 @@ abstract class TreeGen extends macros.TreeBuilder { assert(ReflectRuntimeUniverse != NoSymbol) mkAttributedRef(ReflectRuntimeUniverse) setType singleType(ReflectRuntimeUniverse.owner.thisPrefix, ReflectRuntimeUniverse) } + + def mkPackageDef(packageName: String, stats: List[Tree]): PackageDef = { + PackageDef(mkUnattributedRef(newTermName(packageName)), stats) + } } |