summaryrefslogtreecommitdiff
path: root/src/compiler
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-12-25 02:37:31 +0100
committerEugene Burmako <xeno.by@gmail.com>2013-01-05 01:39:15 +0300
commit348c8fac9f897f9661f84e32949b8a4e0c99e93a (patch)
tree810b2d7d53451ceba5e43e1687a86cdedffd39f1 /src/compiler
parented4f4798df2ac104d9557de2241d8e89283b0662 (diff)
downloadscala-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/compiler')
-rw-r--r--src/compiler/scala/reflect/macros/runtime/Context.scala1
-rw-r--r--src/compiler/scala/reflect/macros/runtime/Synthetics.scala83
-rw-r--r--src/compiler/scala/tools/nsc/CompilationUnits.scala24
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/SyntaxAnalyzer.scala12
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala2
5 files changed, 115 insertions, 7 deletions
diff --git a/src/compiler/scala/reflect/macros/runtime/Context.scala b/src/compiler/scala/reflect/macros/runtime/Context.scala
index 8e8b0fcea1..76c684f6d7 100644
--- a/src/compiler/scala/reflect/macros/runtime/Context.scala
+++ b/src/compiler/scala/reflect/macros/runtime/Context.scala
@@ -14,6 +14,7 @@ abstract class Context extends scala.reflect.macros.Context
with Parsers
with Evals
with ExprUtils
+ with Synthetics
with Traces {
val universe: Global
diff --git a/src/compiler/scala/reflect/macros/runtime/Synthetics.scala b/src/compiler/scala/reflect/macros/runtime/Synthetics.scala
new file mode 100644
index 0000000000..73f3ab8d20
--- /dev/null
+++ b/src/compiler/scala/reflect/macros/runtime/Synthetics.scala
@@ -0,0 +1,83 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2013 LAMP/EPFL
+ */
+
+package scala.reflect.macros
+package runtime
+
+import java.util.UUID._
+import scala.reflect.internal.Flags._
+import scala.reflect.internal.util.BatchSourceFile
+import scala.reflect.io.VirtualFile
+
+trait Synthetics {
+ self: Context =>
+
+ import global._
+ import mirror.wrapMissing
+
+ // getClassIfDefined and getModuleIfDefined cannot be used here
+ // because they don't work for stuff declared in the empty package
+ // (as specified in SLS, code inside non-empty packages cannot see
+ // declarations from the empty package, so compiler internals
+ // default to ignoring contents of the empty package)
+ // to the contrast, staticModule and staticClass are designed
+ // to be a part of the reflection API and, therefore, they
+ // correctly resolve all names
+ private def topLevelSymbol(name: Name): Symbol = wrapMissing {
+ if (name.isTermName) mirror.staticModule(name.toString)
+ else mirror.staticClass(name.toString)
+ }
+
+ def topLevelDef(name: Name): Tree =
+ enclosingRun.units.toList.map(_.body).flatMap {
+ // it's okay to check `stat.symbol` here, because currently macros expand strictly after namer
+ // which means that by the earliest time one can call this method all top-level definitions will have already been entered
+ case PackageDef(_, stats) => stats filter (stat => stat.symbol != NoSymbol && stat.symbol == topLevelSymbol(name))
+ case _ => Nil // should never happen, but better be safe than sorry
+ }.headOption getOrElse EmptyTree
+
+ def topLevelRef(name: Name): Tree = {
+ if (topLevelDef(name).nonEmpty) gen.mkUnattributedRef(name)
+ else EmptyTree
+ }
+
+ // TODO: provide a way to specify a pretty name for debugging purposes
+ private def randomFileName() = (
+ "macroSynthetic-" + randomUUID().toString.replace("-", "") + ".scala"
+ )
+
+ def introduceTopLevel[T: PackageSpec](packagePrototype: T, definition: universe.ImplDef): RefTree =
+ introduceTopLevel(packagePrototype, List(definition)).head
+
+ def introduceTopLevel[T: PackageSpec](packagePrototype: T, definitions: universe.ImplDef*): List[RefTree] =
+ introduceTopLevel(packagePrototype, definitions.toList)
+
+ private def introduceTopLevel[T: PackageSpec](packagePrototype: T, definitions: List[universe.ImplDef]): List[RefTree] = {
+ val code @ PackageDef(pid, _) = implicitly[PackageSpec[T]].mkPackageDef(packagePrototype, definitions)
+ val syntheticFileName = randomFileName()
+ // compatibility with SBT
+ // on the one hand, we need to specify some jfile here, otherwise sbt crashes with an NPE (SI-6870)
+ // on the other hand, we can't specify the obvious enclosingUnit, because then sbt somehow fails to run tests using type macros
+ // okay, now let's specify a guaranteedly non-existent file in an existing directory (so that we don't run into permission problems)
+ val relatedJfile = enclosingUnit.source.file.file
+ val fakeJfile = if (relatedJfile != null) new java.io.File(relatedJfile.getParent, syntheticFileName) else null
+ val virtualFile = new VirtualFile(syntheticFileName) { override def file = fakeJfile }
+ val sourceFile = new BatchSourceFile(virtualFile, code.toString)
+ val unit = new CompilationUnit(sourceFile)
+ unit.body = code
+ universe.currentRun.compileLate(unit)
+ definitions map (definition => Select(pid, definition.name))
+ }
+
+ protected def mkPackageDef(name: String, stats: List[Tree]) = gen.mkPackageDef(name, stats)
+
+ protected def mkPackageDef(name: TermName, stats: List[Tree]) = gen.mkPackageDef(name.toString, stats)
+
+ protected def mkPackageDef(tree: RefTree, stats: List[Tree]) = PackageDef(tree, stats)
+
+ protected def mkPackageDef(sym: Symbol, stats: List[Tree]) = {
+ assert(sym hasFlag PACKAGE, s"expected a package or package class symbol, found: $sym")
+ gen.mkPackageDef(sym.fullName.toString, stats)
+ }
+}
diff --git a/src/compiler/scala/tools/nsc/CompilationUnits.scala b/src/compiler/scala/tools/nsc/CompilationUnits.scala
index a2108b8ced..15d365ab8c 100644
--- a/src/compiler/scala/tools/nsc/CompilationUnits.scala
+++ b/src/compiler/scala/tools/nsc/CompilationUnits.scala
@@ -39,11 +39,31 @@ trait CompilationUnits { self: Global =>
/** Note: depends now contains toplevel classes.
* To get their sourcefiles, you need to dereference with .sourcefile
*/
- val depends = mutable.HashSet[Symbol]()
+ private[this] val _depends = mutable.HashSet[Symbol]()
+ // SBT compatibility (SI-6875)
+ //
+ // imagine we have a file named A.scala, which defines a trait named Foo and a module named Main
+ // Main contains a call to a macro, which calls c.introduceTopLevel to define a mock for Foo
+ // c.introduceTopLevel creates a virtual file Virt35af32.scala, which contains a class named FooMock extending Foo,
+ // and macro expansion instantiates FooMock. the stage is now set. let's see what happens next.
+ //
+ // without this workaround in scalac or without being patched itself, sbt will think that
+ // * Virt35af32 depends on A (because it extends Foo from A)
+ // * A depends on Virt35af32 (because it contains a macro expansion referring to FooMock from Virt35af32)
+ //
+ // after compiling A.scala, SBT will notice that it has a new source file named Virt35af32.
+ // it will also think that this file hasn't yet been compiled and since A depends on it
+ // it will think that A needs to be recompiled.
+ //
+ // recompilation will lead to another macro expansion. that another macro expansion might choose to create a fresh mock,
+ // producing another virtual file, say, Virtee509a, which will again trick SBT into thinking that A needs a recompile,
+ // which will lead to another macro expansion, which will produce another virtual file and so on
+ def depends = if (exists && !source.file.isVirtual) _depends else mutable.HashSet[Symbol]()
/** so we can relink
*/
- val defined = mutable.HashSet[Symbol]()
+ private[this] val _defined = mutable.HashSet[Symbol]()
+ def defined = if (exists && !source.file.isVirtual) _defined else mutable.HashSet[Symbol]()
/** Synthetic definitions generated by namer, eliminated by typer.
*/
diff --git a/src/compiler/scala/tools/nsc/ast/parser/SyntaxAnalyzer.scala b/src/compiler/scala/tools/nsc/ast/parser/SyntaxAnalyzer.scala
index 8a9ce8907e..f1bf590ebf 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/SyntaxAnalyzer.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/SyntaxAnalyzer.scala
@@ -23,10 +23,14 @@ abstract class SyntaxAnalyzer extends SubComponent with Parsers with MarkupParse
def apply(unit: global.CompilationUnit) {
import global._
informProgress("parsing " + unit)
- unit.body =
- if (unit.isJava) new JavaUnitParser(unit).parse()
- else if (reporter.incompleteHandled) new UnitParser(unit).parse()
- else new UnitParser(unit).smartParse()
+ // if the body is already filled in, do nothing
+ // otherwise compileLate is going to overwrite bodies of synthetic source files
+ if (unit.body == EmptyTree) {
+ unit.body =
+ if (unit.isJava) new JavaUnitParser(unit).parse()
+ else if (reporter.incompleteHandled) new UnitParser(unit).parse()
+ else new UnitParser(unit).smartParse()
+ }
if (settings.Yrangepos.value && !reporter.hasErrors)
validatePositions(unit.body)
diff --git a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
index 14c8d85836..dfc1196f2e 100644
--- a/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/NamesDefaults.scala
@@ -394,7 +394,7 @@ trait NamesDefaults { self: Analyzer =>
// TODO #3649 can create spurious errors when companion object is gone (because it becomes unlinked from scope)
if (defGetter == NoSymbol) None // prevent crash in erroneous trees, #3649
else {
- var default1 = qual match {
+ var default1: Tree = qual match {
case Some(q) => gen.mkAttributedSelect(q.duplicate, defGetter)
case None => gen.mkAttributedRef(defGetter)