summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Macros.scala7
-rw-r--r--src/compiler/scala/tools/reflect/ReflectGlobal.scala16
-rw-r--r--test/files/run/toolbox_expand_macro.check1
-rw-r--r--test/files/run/toolbox_expand_macro.scala23
4 files changed, 47 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
index 031245346b..3ed128cbc5 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
@@ -55,6 +55,13 @@ trait Macros extends MacroRuntimes with Traces with Helpers {
def globalSettings = global.settings
+ /** Obtains a `ClassLoader` instance used for macro expansion.
+ *
+ * By default a new `ScalaClassLoader` is created using the classpath
+ * from global and the classloader of self as parent.
+ *
+ * Mirrors with runtime definitions (e.g. Repl) need to adjust this method.
+ */
protected def findMacroClassLoader(): ClassLoader = {
val classpath = global.classPath.asURLs
macroLogVerbose("macro classloader: initializing from -cp: %s".format(classpath))
diff --git a/src/compiler/scala/tools/reflect/ReflectGlobal.scala b/src/compiler/scala/tools/reflect/ReflectGlobal.scala
index ac63232967..e30d1ed7cd 100644
--- a/src/compiler/scala/tools/reflect/ReflectGlobal.scala
+++ b/src/compiler/scala/tools/reflect/ReflectGlobal.scala
@@ -1,9 +1,11 @@
package scala.tools
package reflect
+import scala.reflect.internal.util.ScalaClassLoader
import scala.tools.nsc.Global
import scala.tools.nsc.reporters.Reporter
import scala.tools.nsc.Settings
+import scala.tools.nsc.typechecker.Analyzer
/** A version of Global that uses reflection to get class
* infos, instead of reading class or source files.
@@ -11,6 +13,20 @@ import scala.tools.nsc.Settings
class ReflectGlobal(currentSettings: Settings, reporter: Reporter, override val rootClassLoader: ClassLoader)
extends Global(currentSettings, reporter) with scala.tools.reflect.ReflectSetup with scala.reflect.runtime.SymbolTable {
+ override lazy val analyzer = new {
+ val global: ReflectGlobal.this.type = ReflectGlobal.this
+ } with Analyzer {
+ /** Obtains the classLoader used for runtime macro expansion.
+ *
+ * Macro expansion can use everything available in [[global.classPath]] or [[rootClassLoader]].
+ * The [[rootClassLoader]] is used to obtain runtime defined macros.
+ */
+ override protected def findMacroClassLoader(): ClassLoader = {
+ val classpath = global.classPath.asURLs
+ ScalaClassLoader.fromURLs(classpath, rootClassLoader)
+ }
+ }
+
override def transformedType(sym: Symbol) =
postErasure.transformInfo(sym,
erasure.transformInfo(sym,
diff --git a/test/files/run/toolbox_expand_macro.check b/test/files/run/toolbox_expand_macro.check
new file mode 100644
index 0000000000..d81cc0710e
--- /dev/null
+++ b/test/files/run/toolbox_expand_macro.check
@@ -0,0 +1 @@
+42
diff --git a/test/files/run/toolbox_expand_macro.scala b/test/files/run/toolbox_expand_macro.scala
new file mode 100644
index 0000000000..a52e449168
--- /dev/null
+++ b/test/files/run/toolbox_expand_macro.scala
@@ -0,0 +1,23 @@
+import scala.reflect.runtime.universe._
+import scala.reflect.runtime.{universe => ru}
+import scala.reflect.runtime.{currentMirror => cm}
+import scala.tools.reflect.{ToolBox}
+
+object Test extends App {
+ val toolBox = cm.mkToolBox()
+ val x = 21
+ val runtimeMacro =
+ q"""object RuntimeMacro {
+ import scala.reflect.macros.whitebox.Context
+ import scala.language.experimental.macros
+
+ def add(y: Int): Int = macro addImpl
+ def addImpl(c: Context)(y: c.Expr[Int]): c.Expr[Int] = {
+ import c.universe._
+ val x = $x
+ c.Expr[Int](q"$$x + $$y")
+ }
+ }"""
+ val s = toolBox.define(runtimeMacro)
+ println(toolBox.eval(q"$s.add(21)"))
+}