diff options
Diffstat (limited to 'src/compiler/scala/reflect/runtime/ToolBoxes.scala')
-rw-r--r-- | src/compiler/scala/reflect/runtime/ToolBoxes.scala | 363 |
1 files changed, 251 insertions, 112 deletions
diff --git a/src/compiler/scala/reflect/runtime/ToolBoxes.scala b/src/compiler/scala/reflect/runtime/ToolBoxes.scala index 28f12b378f..6d832a590f 100644 --- a/src/compiler/scala/reflect/runtime/ToolBoxes.scala +++ b/src/compiler/scala/reflect/runtime/ToolBoxes.scala @@ -1,28 +1,30 @@ package scala.reflect package runtime -import scala.tools.nsc -import scala.tools.nsc.reporters.Reporter -import scala.tools.nsc.reporters.StoreReporter -import scala.tools.nsc.reporters.AbstractReporter +import scala.tools.nsc.reporters._ import scala.tools.nsc.ReflectGlobal import scala.tools.nsc.CompilerCommand import scala.tools.nsc.Global import scala.tools.nsc.typechecker.Modes import scala.tools.nsc.io.VirtualDirectory import scala.tools.nsc.interpreter.AbstractFileClassLoader -import reflect.{mirror => rm} import scala.tools.nsc.util.FreshNameCreator import scala.reflect.internal.Flags import scala.tools.nsc.util.{NoSourceFile, NoFile} import java.lang.{Class => jClass} -import scala.tools.nsc.util.trace +import scala.compat.Platform.EOL trait ToolBoxes extends { self: Universe => - class ToolBox(val reporter: Reporter = new StoreReporter, val options: String = "") { + import self.{Reporter => ApiReporter} + import scala.tools.nsc.reporters.{Reporter => NscReporter} - class ToolBoxGlobal(settings0: nsc.Settings, reporter0: nsc.reporters.Reporter) extends ReflectGlobal(settings0, reporter0) { + def mkToolBox(reporter: ApiReporter = mkSilentReporter(), options: String = "") = new ToolBox(reporter, options) + + class ToolBox(val reporter: ApiReporter, val options: String) extends AbsToolBox { + + class ToolBoxGlobal(settings: scala.tools.nsc.Settings, reporter: NscReporter) + extends ReflectGlobal(settings, reporter, ToolBox.this.classLoader) { import definitions._ private val trace = scala.tools.nsc.util.trace when settings.debug.value @@ -36,64 +38,7 @@ trait ToolBoxes extends { self: Universe => newTermName("__wrapper$" + wrapCount) } - private def moduleFileName(className: String) = className + "$" - - private def isFree(t: Tree) = t.isInstanceOf[Ident] && t.symbol.isInstanceOf[FreeVar] - - def typedTopLevelExpr(tree: Tree, pt: Type): Tree = { - // !!! Why is this is in the empty package? If it's only to make - // it inaccessible then please put it somewhere designed for that - // rather than polluting the empty package with synthetics. - trace("typing: ")(showAttributed(tree, true, true, settings.Yshowsymkinds.value)) - val ownerClass = EmptyPackageClass.newClassWithInfo(newTypeName("<expression-owner>"), List(ObjectClass.tpe), newScope) - val owner = ownerClass.newLocalDummy(tree.pos) - val ttree = typer.atOwner(tree, owner).typed(tree, analyzer.EXPRmode, pt) - trace("typed: ")(showAttributed(ttree, true, true, settings.Yshowsymkinds.value)) - ttree - } - - def defOwner(tree: Tree): Symbol = tree find (_.isDef) map (_.symbol) match { - case Some(sym) if sym != null && sym != NoSymbol => sym.owner - case _ => NoSymbol - } - - def wrapInObject(expr: Tree, fvs: List[Symbol]): ModuleDef = { - val obj = EmptyPackageClass.newModule(nextWrapperModuleName()) - val minfo = ClassInfoType(List(ObjectClass.tpe), newScope, obj.moduleClass) - obj.moduleClass setInfo minfo - obj setInfo obj.moduleClass.tpe - val meth = obj.moduleClass.newMethod(newTermName(wrapperMethodName)) - def makeParam(fv: Symbol) = meth.newValueParameter(fv.name.toTermName) setInfo fv.tpe - meth setInfo MethodType(fvs map makeParam, AnyClass.tpe) - minfo.decls enter meth - trace("wrapping ")(defOwner(expr) -> meth) - val methdef = DefDef(meth, expr changeOwner (defOwner(expr) -> meth)) - val moduledef = ModuleDef( - obj, - Template( - List(TypeTree(ObjectClass.tpe)), - emptyValDef, - NoMods, - List(), - List(List()), - List(methdef), - NoPosition)) - trace("wrapped: ")(showAttributed(moduledef, true, true, settings.Yshowsymkinds.value)) - val cleanedUp = resetLocalAttrs(moduledef) - trace("cleaned up: ")(showAttributed(cleanedUp, true, true, settings.Yshowsymkinds.value)) - cleanedUp - } - - def wrapInPackage(clazz: Tree): PackageDef = - PackageDef(Ident(nme.EMPTY_PACKAGE_NAME), List(clazz)) - - def wrapInCompilationUnit(tree: Tree): CompilationUnit = { - val unit = new CompilationUnit(NoSourceFile) - unit.body = tree - unit - } - - def compileExpr(expr: Tree, fvs: List[Symbol]): String = { + def verifyExpr(expr: Tree): Unit = { // Previously toolboxes used to typecheck their inputs before compiling. // Actually, the initial demo by Martin first typechecked the reified tree, // then ran it, which typechecked it again, and only then launched the @@ -104,44 +49,190 @@ trait ToolBoxes extends { self: Universe => // That's why we cannot allow inputs of toolboxes to be typechecked, // at least not until the aforementioned issue is closed. val typed = expr filter (t => t.tpe != null && t.tpe != NoType && !t.isInstanceOf[TypeTree]) - if (!typed.isEmpty) { - throw new Error("cannot compile trees that are already typed") + if (!typed.isEmpty) throw new ToolBoxError(ToolBox.this, "reflective toolbox has failed: cannot operate on trees that are already typed") + + val freeTypes = this.freeTypes(expr) + if (freeTypes.length > 0) { + var msg = "reflective toolbox has failed:" + EOL + msg += "unresolved free type variables (namely: " + (freeTypes map (ft => "%s %s".format(ft.name, ft.origin)) mkString ", ") + "). " + msg += "have you forgot to use TypeTag annotations for type parameters external to a reifee? " + msg += "if you have troubles tracking free type variables, consider using -Xlog-free-types" + throw new ToolBoxError(ToolBox.this, msg) } + } - val mdef = wrapInObject(expr, fvs) - val pdef = wrapInPackage(mdef) - val unit = wrapInCompilationUnit(pdef) - val run = new Run - run.compileUnits(List(unit), run.namerPhase) - mdef.symbol.fullName + def typeCheckExpr(expr0: Tree, pt: Type, silent: Boolean = false, withImplicitViewsDisabled: Boolean = false, withMacrosDisabled: Boolean = false): Tree = { + verifyExpr(expr0) + + // need to wrap the expr, because otherwise you won't be able to typecheck macros against something that contains free vars + // [Eugene] get rid of the copy/paste w.r.t compileExpr + val freeTerms = this.freeTerms(expr0) + val freeTermNames = collection.mutable.Map[Symbol, TermName]() + freeTerms foreach (ft => { + var name = ft.name.toString + val namesakes = freeTerms takeWhile (_ != ft) filter (ft2 => ft != ft2 && ft.name == ft2.name) + if (namesakes.length > 0) name += ("$" + (namesakes.length + 1)) + freeTermNames += (ft -> newTermName(name + nme.MIRROR_FREE_VALUE_SUFFIX)) + }) + var expr = new Transformer { + override def transform(tree: Tree): Tree = + if (tree.hasSymbol && tree.symbol.isFreeTerm) { + tree match { + case Ident(_) => + Ident(freeTermNames(tree.symbol)) + case _ => + throw new Error("internal error: %s (%s, %s) is not supported".format(tree, tree.productPrefix, tree.getClass)) + } + } else { + super.transform(tree) + } + }.transform(expr0) + val dummies = freeTerms map (freeTerm => ValDef(NoMods, freeTermNames(freeTerm), TypeTree(freeTerm.info), Select(Ident(PredefModule), newTermName("$qmark$qmark$qmark")))) + expr = Block(dummies, expr) + + // [Eugene] how can we implement that? + // !!! Why is this is in the empty package? If it's only to make + // it inaccessible then please put it somewhere designed for that + // rather than polluting the empty package with synthetics. + val ownerClass = EmptyPackageClass.newClassWithInfo(newTypeName("<expression-owner>"), List(ObjectClass.tpe), newScope) + val owner = ownerClass.newLocalDummy(expr.pos) + var currentTyper = typer.atOwner(expr, owner) + val wrapper1 = if (!withImplicitViewsDisabled) (currentTyper.context.withImplicitsEnabled[Tree] _) else (currentTyper.context.withImplicitsDisabled[Tree] _) + val wrapper2 = if (!withMacrosDisabled) (currentTyper.context.withMacrosEnabled[Tree] _) else (currentTyper.context.withMacrosDisabled[Tree] _) + def wrapper (tree: => Tree) = wrapper1(wrapper2(tree)) + + phase = (new Run).typerPhase // need to set a phase to something <= typerPhase, otherwise implicits in typedSelect will be disabled + currentTyper.context.setReportErrors() // need to manually set context mode, otherwise typer.silent will throw exceptions + reporter.reset() + + trace("typing (implicit views = %s, macros = %s): ".format(!withImplicitViewsDisabled, !withMacrosDisabled))(showAttributed(expr, true, true, settings.Yshowsymkinds.value)) + wrapper(currentTyper.silent(_.typed(expr, analyzer.EXPRmode, pt)) match { + case analyzer.SilentResultValue(result) => + trace("success: ")(showAttributed(result, true, true, settings.Yshowsymkinds.value)) + var Block(dummies, unwrapped) = result + var reversedFreeTermNames = freeTermNames map (_.swap) + // todo. also fixup singleton types + unwrapped = new Transformer { + override def transform(tree: Tree): Tree = + tree match { + case Ident(name) if reversedFreeTermNames contains name => + Ident(reversedFreeTermNames(name)) + case _ => + super.transform(tree) + } + }.transform(unwrapped) + new TreeTypeSubstituter(dummies map (_.symbol), dummies map (dummy => SingleType(NoPrefix, reversedFreeTermNames(dummy.symbol.name)))).traverse(unwrapped) + unwrapped + case error @ analyzer.SilentTypeError(_) => + trace("failed: ")(error.err.errMsg) + if (!silent) throw new ToolBoxError(ToolBox.this, "reflective typecheck has failed: %s".format(error.err.errMsg)) + EmptyTree + }) } - private def getMethod(jclazz: jClass[_], name: String) = - jclazz.getDeclaredMethods.find(_.getName == name).get + def compileExpr(expr: Tree): (Object, java.lang.reflect.Method) = { + verifyExpr(expr) + + def wrapExpr(expr0: Tree): Tree = { + def defOwner(tree: Tree): Symbol = tree find (_.isDef) map (_.symbol) match { + case Some(sym) if sym != null && sym != NoSymbol => sym.owner + case _ => NoSymbol + } + + val freeTerms = this.freeTerms(expr0) + val freeTermNames = collection.mutable.Map[Symbol, TermName]() + freeTerms foreach (ft => { + var name = ft.name.toString + val namesakes = freeTerms takeWhile (_ != ft) filter (ft2 => ft != ft2 && ft.name == ft2.name) + if (namesakes.length > 0) name += ("$" + (namesakes.length + 1)) + freeTermNames += (ft -> newTermName(name + nme.MIRROR_FREE_VALUE_SUFFIX)) + }) + val expr = new Transformer { + override def transform(tree: Tree): Tree = + if (tree.hasSymbol && tree.symbol.isFreeTerm) { + tree match { + case Ident(_) => + Apply(Ident(freeTermNames(tree.symbol)), List()) + case _ => + throw new Error("internal error: %s (%s, %s) is not supported".format(tree, tree.productPrefix, tree.getClass)) + } + } else { + super.transform(tree) + } + }.transform(expr0) + + val obj = EmptyPackageClass.newModule(nextWrapperModuleName()) + val minfo = ClassInfoType(List(ObjectClass.tpe), newScope, obj.moduleClass) + obj.moduleClass setInfo minfo + obj setInfo obj.moduleClass.tpe + val meth = obj.moduleClass.newMethod(newTermName(wrapperMethodName)) + def makeParam(fv: Symbol) = { + // [Eugene] conventional way of doing this? + val underlying = fv.tpe.resultType + val tpe = appliedType(definitions.FunctionClass(0).tpe, List(underlying)) + meth.newValueParameter(freeTermNames(fv)) setInfo tpe + } + meth setInfo MethodType(freeTerms map makeParam, AnyClass.tpe) + minfo.decls enter meth + trace("wrapping ")(defOwner(expr) -> meth) + val methdef = DefDef(meth, expr changeOwner (defOwner(expr) -> meth)) + val moduledef = ModuleDef( + obj, + Template( + List(TypeTree(ObjectClass.tpe)), + emptyValDef, + NoMods, + List(), + List(List()), + List(methdef), + NoPosition)) + trace("wrapped: ")(showAttributed(moduledef, true, true, settings.Yshowsymkinds.value)) + var cleanedUp = resetLocalAttrs(moduledef) + trace("cleaned up: ")(showAttributed(cleanedUp, true, true, settings.Yshowsymkinds.value)) + cleanedUp + } - def runExpr(expr: Tree): Any = { - val fvs = (expr filter isFree map (_.symbol)).distinct + val mdef = wrapExpr(expr) + val pdef = PackageDef(Ident(nme.EMPTY_PACKAGE_NAME), List(mdef)) + val unit = new CompilationUnit(NoSourceFile) + unit.body = pdef + val run = new Run reporter.reset() - val className = compileExpr(expr, fvs) + run.compileUnits(List(unit), run.namerPhase) if (reporter.hasErrors) { - throw new Error("reflective compilation has failed") + var msg = "reflective compilation has failed: " + EOL + EOL + msg += ToolBox.this.reporter.infos map (_.msg) mkString EOL + throw new ToolBoxError(ToolBox.this, msg) } + val className = mdef.symbol.fullName if (settings.debug.value) println("generated: "+className) + def moduleFileName(className: String) = className + "$" val jclazz = jClass.forName(moduleFileName(className), true, classLoader) val jmeth = jclazz.getDeclaredMethods.find(_.getName == wrapperMethodName).get val jfield = jclazz.getDeclaredFields.find(_.getName == NameTransformer.MODULE_INSTANCE_NAME).get val singleton = jfield.get(null) + (singleton, jmeth) + } + + def runExpr(expr: Tree, freeTypes: Map[TypeName, Type] = Map[TypeName, Type]()): Any = { + val freeTerms = this.freeTerms(expr) // need to calculate them here, because later on they will be erased + val thunks = freeTerms map (fte => () => fte.value) // need to be lazy in order not to distort evaluation order + // @odersky writes: Not sure we will be able to drop this. I forgot the reason why we dereference () functions, // but there must have been one. So I propose to leave old version in comments to be resurrected if the problem resurfaces. -// val result = jmeth.invoke(singleton, fvs map (sym => sym.asInstanceOf[FreeVar].value.asInstanceOf[AnyRef]): _*) + // @Eugene writes: this dates back to the days when one could only reify functions + // hence, blocks were translated into nullary functions, so + // presumably, it was useful to immediately evaluate them to get the result of a block +// val result = jmeth.invoke(singleton, freeTerms map (sym => sym.asInstanceOf[FreeTermVar].value.asInstanceOf[AnyRef]): _*) // if (etpe.typeSymbol != FunctionClass(0)) result // else { // val applyMeth = result.getClass.getMethod("apply") // applyMeth.invoke(result) // } - jmeth.invoke(singleton, fvs map (sym => sym.asInstanceOf[FreeVar].value.asInstanceOf[AnyRef]): _*) + val (singleton, jmeth) = compileExpr(expr) + jmeth.invoke(singleton, thunks map (_.asInstanceOf[AnyRef]): _*) } def showAttributed(tree: Tree, printTypes: Boolean = true, printIds: Boolean = true, printKinds: Boolean = false): String = { @@ -161,6 +252,7 @@ trait ToolBoxes extends { self: Universe => } } + // todo. is not going to work with quoted arguments with embedded whitespaces lazy val arguments = options.split(" ") lazy val virtualDirectory = @@ -170,49 +262,96 @@ trait ToolBoxes extends { self: Universe => } lazy val compiler: ToolBoxGlobal = { - val errorFn: String => Unit = reporter.error(scala.tools.nsc.util.NoPosition, _) - val command = reporter match { - case reporter: AbstractReporter => new CompilerCommand(arguments.toList, reporter.settings, errorFn) - case _ => new CompilerCommand(arguments.toList, errorFn) + try { + val errorFn: String => Unit = msg => reporter.log(NoPosition, msg, reporter.ERROR) + // [Eugene] settings shouldn't be passed via reporters, this is crazy +// val command = reporter match { +// case reporter: AbstractReporter => new CompilerCommand(arguments.toList, reporter.settings, errorFn) +// case _ => new CompilerCommand(arguments.toList, errorFn) +// } + val command = new CompilerCommand(arguments.toList, errorFn) + command.settings.outputDirs setSingleOutput virtualDirectory + val nscReporter = new ApiToNscReporterProxy(reporter) { val settings = command.settings } + val instance = new ToolBoxGlobal(command.settings, nscReporter) + if (nscReporter.hasErrors) { + var msg = "reflective compilation has failed: cannot initialize the compiler: " + EOL + EOL + msg += reporter.infos map (_.msg) mkString EOL + throw new ToolBoxError(this, msg) + } + instance.phase = (new instance.Run).typerPhase // need to manually set a phase, because otherwise TypeHistory will crash + instance + } catch { + case ex: Throwable => + var msg = "reflective compilation has failed: cannot initialize the compiler due to %s".format(ex.toString) + throw new ToolBoxError(this, msg, ex) } - - command.settings.outputDirs setSingleOutput virtualDirectory - val instance = new ToolBoxGlobal(command.settings, reporter) - - // need to establish a run an phase because otherwise we run into an assertion in TypeHistory - // that states that the period must be different from NoPeriod - val run = new instance.Run - instance.phase = run.refchecksPhase - instance } - lazy val importer = new compiler.Importer { - val from: self.type = self - } + // @Eugene: how do I make this work without casts? + // lazy val importer = compiler.mkImporter(self) + lazy val importer = compiler.mkImporter(self).asInstanceOf[compiler.Importer { val from: self.type }] lazy val exporter = importer.reverse - lazy val classLoader = new AbstractFileClassLoader(virtualDirectory, defaultReflectiveClassLoader) + lazy val classLoader = new AbstractFileClassLoader(virtualDirectory, self.classLoader) - def typeCheck(tree: rm.Tree, expectedType: rm.Type): rm.Tree = { - if (compiler.settings.verbose.value) println("typing "+tree+", pt = "+expectedType) - val ctree: compiler.Tree = importer.importTree(tree.asInstanceOf[Tree]) - val pt: compiler.Type = importer.importType(expectedType.asInstanceOf[Type]) - val ttree: compiler.Tree = compiler.typedTopLevelExpr(ctree, pt) - val rmttree = exporter.importTree(ttree).asInstanceOf[rm.Tree] + def typeCheck(tree: Tree, expectedType: Type = WildcardType, freeTypes: Map[FreeType, Type] = Map[FreeType, Type](), silent: Boolean = false, withImplicitViewsDisabled: Boolean = false, withMacrosDisabled: Boolean = false): Tree = { + if (compiler.settings.verbose.value) println("typing "+tree+", expectedType = "+expectedType+", freeTypes = "+freeTypes) + var ctree: compiler.Tree = importer.importTree(tree) + var cexpectedType: compiler.Type = importer.importType(expectedType) + + if (compiler.settings.verbose.value) println("substituting "+ctree+", expectedType = "+expectedType) + val cfreeTypes: Map[compiler.FreeType, compiler.Type] = freeTypes map { case (k, v) => (importer.importSymbol(k).asInstanceOf[compiler.FreeType], importer.importType(v)) } + ctree = compiler.substituteFreeTypes(ctree, cfreeTypes) + cexpectedType = compiler.substituteFreeTypes(cexpectedType, cfreeTypes) + + if (compiler.settings.verbose.value) println("typing "+ctree+", expectedType = "+expectedType) + val ttree: compiler.Tree = compiler.typeCheckExpr(ctree, cexpectedType, silent = silent, withImplicitViewsDisabled = withImplicitViewsDisabled, withMacrosDisabled = withMacrosDisabled) + val rmttree = exporter.importTree(ttree) rmttree } - def typeCheck(tree: rm.Tree): rm.Tree = - typeCheck(tree, WildcardType.asInstanceOf[rm.Type]) + def inferImplicitValue(pt: Type, silent: Boolean = true, withMacrosDisabled: Boolean = false): Tree = + // todo. implement this + ??? + + def inferImplicitView(tree: Tree, from: Type, to: Type, silent: Boolean = true, withMacrosDisabled: Boolean = false, reportAmbiguous: Boolean = true): Tree = + // todo. implement this + ??? + + def resetAllAttrs[T <: Tree](tree: T): T = { + val ctree: compiler.Tree = importer.importTree(tree) + val ttree: compiler.Tree = compiler.resetAllAttrs(ctree) + val rmttree = exporter.importTree(ttree) + rmttree.asInstanceOf[T] + } + + def resetLocalAttrs[T <: Tree](tree: T): T = { + val ctree: compiler.Tree = importer.importTree(tree) + val ttree: compiler.Tree = compiler.resetLocalAttrs(ctree) + val rmttree = exporter.importTree(ttree) + rmttree.asInstanceOf[T] + } + + def showAttributed(tree: Tree, printTypes: Boolean = true, printIds: Boolean = true, printKinds: Boolean = false): String = + compiler.showAttributed(importer.importTree(tree), printTypes, printIds, printKinds) + + def runExpr(tree: Tree, freeTypes: Map[FreeType, Type] = Map[FreeType, Type]()): Any = { + if (compiler.settings.verbose.value) println("running "+tree+", freeTypes = "+freeTypes) + var ctree: compiler.Tree = importer.importTree(tree) - def showAttributed(tree: rm.Tree, printTypes: Boolean = true, printIds: Boolean = true, printKinds: Boolean = false): String = - compiler.showAttributed(importer.importTree(tree.asInstanceOf[Tree]), printTypes, printIds, printKinds) + if (compiler.settings.verbose.value) println("substituting "+ctree) + val cfreeTypes: Map[compiler.FreeType, compiler.Type] = freeTypes map { case (k, v) => (importer.importSymbol(k).asInstanceOf[compiler.FreeType], importer.importType(v)) } + ctree = compiler.substituteFreeTypes(ctree, cfreeTypes) - def runExpr(tree: rm.Tree): Any = { - if (compiler.settings.verbose.value) println("running "+tree) - val ctree: compiler.Tree = importer.importTree(tree.asInstanceOf[Tree]) + if (compiler.settings.verbose.value) println("running "+ctree) compiler.runExpr(ctree) } + + class ToolBoxError(val toolBox: ToolBox, val message: String, val cause: Throwable = null) extends Throwable(message, cause) + + object ToolBoxError extends ToolBoxErrorExtractor { + def unapply(error: ToolBoxError): Option[(ToolBox, String)] = Some((error.toolBox, error.message)) + } } } |