From 114c99691674873393223a11a9aa9168c3f41d77 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Thu, 30 Jan 2014 10:01:31 +0300 Subject: establishes scala.reflect.api#internal Reflection API exhibits a tension inherent to experimental things: on the one hand we want it to grow into a beautiful and robust API, but on the other hand we have to deal with immaturity of underlying mechanisms by providing not very pretty solutions to enable important use cases. In Scala 2.10, which was our first stab at reflection API, we didn't have a systematic approach to dealing with this tension, sometimes exposing too much of internals (e.g. Symbol.deSkolemize) and sometimes exposing too little (e.g. there's still no facility to change owners, to do typing transformations, etc). This resulted in certain confusion with some internal APIs living among public ones, scaring the newcomers, and some internal APIs only available via casting, which requires intimate knowledge of the compiler and breaks compatibility guarantees. This led to creation of the `internal` API module for the reflection API, which provides advanced APIs necessary for macros that push boundaries of the state of the art, clearly demarcating them from the more or less straightforward rest and providing compatibility guarantees on par with the rest of the reflection API. This commit does break source compatibility with reflection API in 2.10, but the next commit is going to introduce a strategy of dealing with that. --- .../scala/reflect/macros/contexts/Evals.scala | 2 +- .../scala/reflect/reify/codegen/GenTypes.scala | 37 +++++++++++----------- .../scala/reflect/reify/utils/Extractors.scala | 20 ++++++------ .../scala/reflect/reify/utils/NodePrinters.scala | 3 +- src/compiler/scala/tools/nsc/Global.scala | 6 ++-- .../scala/tools/nsc/typechecker/Implicits.scala | 3 +- src/compiler/scala/tools/reflect/StdTags.scala | 3 +- .../scala/tools/reflect/ToolBoxFactory.scala | 2 +- .../scala/tools/reflect/quasiquotes/Holes.scala | 10 +++--- .../scala/tools/reflect/quasiquotes/Reifiers.scala | 6 ++-- 10 files changed, 45 insertions(+), 47 deletions(-) (limited to 'src/compiler/scala') diff --git a/src/compiler/scala/reflect/macros/contexts/Evals.scala b/src/compiler/scala/reflect/macros/contexts/Evals.scala index 180a998c39..a715af986c 100644 --- a/src/compiler/scala/reflect/macros/contexts/Evals.scala +++ b/src/compiler/scala/reflect/macros/contexts/Evals.scala @@ -9,7 +9,7 @@ trait Evals { private lazy val evalMirror = ru.runtimeMirror(universe.analyzer.defaultMacroClassloader) private lazy val evalToolBox = evalMirror.mkToolBox() - private lazy val evalImporter = ru.mkImporter(universe).asInstanceOf[ru.Importer { val from: universe.type }] + private lazy val evalImporter = ru.internal.createImporter(universe).asInstanceOf[ru.Importer { val from: universe.type }] def eval[T](expr: Expr[T]): T = { expr.tree match { diff --git a/src/compiler/scala/reflect/reify/codegen/GenTypes.scala b/src/compiler/scala/reflect/reify/codegen/GenTypes.scala index a90a3a338b..a6b69e239f 100644 --- a/src/compiler/scala/reflect/reify/codegen/GenTypes.scala +++ b/src/compiler/scala/reflect/reify/codegen/GenTypes.scala @@ -45,21 +45,21 @@ trait GenTypes { case tpe @ ThisType(clazz) if clazz.isModuleClass && clazz.isStatic => val module = reify(clazz.sourceModule) val moduleClass = Select(Select(module, nme.asModule), nme.moduleClass) - mirrorFactoryCall(nme.ThisType, moduleClass) - case tpe @ ThisType(_) => - reifyProduct(tpe) + mirrorBuildCall(nme.ThisType, moduleClass) + case tpe @ ThisType(sym) => + reifyBuildCall(nme.ThisType, sym) case tpe @ SuperType(thistpe, supertpe) => - reifyProduct(tpe) + reifyBuildCall(nme.SuperType, thistpe, supertpe) case tpe @ SingleType(pre, sym) => - reifyProduct(tpe) + reifyBuildCall(nme.SingleType, pre, sym) case tpe @ ConstantType(value) => - mirrorFactoryCall(nme.ConstantType, reifyProduct(value)) + mirrorBuildCall(nme.ConstantType, reifyProduct(value)) case tpe @ TypeRef(pre, sym, args) => - reifyProduct(tpe) + reifyBuildCall(nme.TypeRef, pre, sym, args) case tpe @ TypeBounds(lo, hi) => - reifyProduct(tpe) + reifyBuildCall(nme.TypeBounds, lo, hi) case tpe @ NullaryMethodType(restpe) => - reifyProduct(tpe) + reifyBuildCall(nme.NullaryMethodType, restpe) case tpe @ AnnotatedType(anns, underlying) => reifyAnnotatedType(tpe) case _ => @@ -119,7 +119,8 @@ trait GenTypes { // todo. write a test for this if (ReflectRuntimeUniverse == NoSymbol) CannotConvertManifestToTagWithoutScalaReflect(tpe, manifestInScope) val cm = typer.typed(Ident(ReflectRuntimeCurrentMirror)) - val tagTree = gen.mkMethodCall(ReflectRuntimeUniverse, nme.manifestToTypeTag, List(tpe), List(cm, manifestInScope)) + val internal = gen.mkAttributedSelect(gen.mkAttributedRef(ReflectRuntimeUniverse), UniverseInternal) + val tagTree = gen.mkMethodCall(Select(internal, nme.manifestToTypeTag), List(tpe), List(cm, manifestInScope)) Select(Apply(Select(tagTree, nme.in), List(Ident(nme.MIRROR_SHORT))), nme.tpe) case _ => EmptyTree @@ -157,13 +158,13 @@ trait GenTypes { */ private def reifySemiConcreteTypeMember(tpe: Type): Tree = tpe match { case tpe @ TypeRef(pre @ SingleType(prepre, presym), sym, args) if sym.isAbstractType && !sym.isExistential => - mirrorFactoryCall(nme.TypeRef, reify(pre), mirrorBuildCall(nme.selectType, reify(sym.owner), reify(sym.name.toString)), reify(args)) + mirrorBuildCall(nme.TypeRef, reify(pre), mirrorBuildCall(nme.selectType, reify(sym.owner), reify(sym.name.toString)), reify(args)) } /** Reify an annotated type, i.e. the one that makes us deal with AnnotationInfos */ private def reifyAnnotatedType(tpe: AnnotatedType): Tree = { val AnnotatedType(anns, underlying) = tpe - mirrorFactoryCall(nme.AnnotatedType, mkList(anns map reifyAnnotationInfo), reify(underlying)) + mirrorBuildCall(nme.AnnotatedType, mkList(anns map reifyAnnotationInfo), reify(underlying)) } /** Reify a tough type, i.e. the one that leads to creation of auxiliary symbols */ @@ -172,25 +173,25 @@ trait GenTypes { def reifyScope(scope: Scope): Tree = { scope foreach reifySymDef - mirrorCall(nme.newScopeWith, scope.toList map reify: _*) + mirrorBuildCall(nme.newScopeWith, scope.toList map reify: _*) } tpe match { case tpe @ RefinedType(parents, decls) => reifySymDef(tpe.typeSymbol) - mirrorFactoryCall(tpe, reify(parents), reifyScope(decls), reify(tpe.typeSymbol)) + mirrorBuildCall(nme.RefinedType, reify(parents), reifyScope(decls), reify(tpe.typeSymbol)) case tpe @ ExistentialType(tparams, underlying) => tparams foreach reifySymDef - mirrorFactoryCall(tpe, reify(tparams), reify(underlying)) + reifyBuildCall(nme.ExistentialType, tparams, underlying) case tpe @ ClassInfoType(parents, decls, clazz) => reifySymDef(clazz) - mirrorFactoryCall(tpe, reify(parents), reifyScope(decls), reify(tpe.typeSymbol)) + mirrorBuildCall(nme.ClassInfoType, reify(parents), reifyScope(decls), reify(tpe.typeSymbol)) case tpe @ MethodType(params, restpe) => params foreach reifySymDef - mirrorFactoryCall(tpe, reify(params), reify(restpe)) + reifyBuildCall(nme.MethodType, params, restpe) case tpe @ PolyType(tparams, underlying) => tparams foreach reifySymDef - mirrorFactoryCall(tpe, reify(tparams), reify(underlying)) + reifyBuildCall(nme.PolyType, tparams, underlying) case _ => throw new Error("internal error: %s (%s) is not supported".format(tpe, tpe.kind)) } diff --git a/src/compiler/scala/reflect/reify/utils/Extractors.scala b/src/compiler/scala/reflect/reify/utils/Extractors.scala index d052127956..cfc42e31a9 100644 --- a/src/compiler/scala/reflect/reify/utils/Extractors.scala +++ b/src/compiler/scala/reflect/reify/utils/Extractors.scala @@ -183,12 +183,12 @@ trait Extractors { tree match { case ValDef(_, name, _, Apply( - Select(Select(uref1 @ Ident(_), build1), freeTermFactory), + Select(Select(Select(uref1 @ Ident(_), internal1), rs1), freeTermFactory), _ :+ - ApplyCall(Select(Select(uref2 @ Ident(_), build2), flagsRepr), List(Literal(Constant(flags: Long)))) :+ + ApplyCall(Select(Select(Select(uref2 @ Ident(_), internal2), rs2), flagsRepr), List(Literal(Constant(flags: Long)))) :+ Literal(Constant(origin: String)))) - if uref1.name == nme.UNIVERSE_SHORT && build1 == nme.build && acceptFreeTermFactory(freeTermFactory) && - uref2.name == nme.UNIVERSE_SHORT && build2 == nme.build && flagsRepr == nme.FlagsRepr => + if uref1.name == nme.UNIVERSE_SHORT && internal1 == nme.internal && rs1 == nme.reificationSupport && acceptFreeTermFactory(freeTermFactory) && + uref2.name == nme.UNIVERSE_SHORT && internal2 == nme.internal && rs2 == nme.reificationSupport && flagsRepr == nme.FlagsRepr => Some((uref1, name, reifyBinding(tree), flags, origin)) case _ => None @@ -201,8 +201,8 @@ trait Extractors { object FreeRef { def unapply(tree: Tree): Option[(Tree, TermName)] = tree match { - case Apply(Select(Select(uref @ Ident(_), build), ident), List(Ident(name: TermName))) - if build == nme.build && ident == nme.Ident && name.startsWith(nme.REIFY_FREE_PREFIX) => + case Apply(Select(Select(Select(uref @ Ident(_), internal), rs), ident), List(Ident(name: TermName))) + if internal == nme.internal && rs == nme.reificationSupport && ident == nme.Ident && name.startsWith(nme.REIFY_FREE_PREFIX) => Some((uref, name)) case _ => None @@ -213,15 +213,15 @@ trait Extractors { def unapply(tree: Tree): Option[(Tree, TermName, Long, Boolean)] = tree match { case ValDef(_, name, _, Apply( - Select(Select(uref1 @ Ident(_), build1), newNestedSymbol), + Select(Select(Select(uref1 @ Ident(_), internal1), rs1), newNestedSymbol), List( _, _, _, - ApplyCall(Select(Select(uref2 @ Ident(_), build2), flagsRepr), List(Literal(Constant(flags: Long)))), + ApplyCall(Select(Select(Select(uref2 @ Ident(_), internal2), rs2), flagsRepr), List(Literal(Constant(flags: Long)))), Literal(Constant(isClass: Boolean))))) - if uref1.name == nme.UNIVERSE_SHORT && build1 == nme.build && newNestedSymbol == nme.newNestedSymbol && - uref2.name == nme.UNIVERSE_SHORT && build2 == nme.build && flagsRepr == nme.FlagsRepr => + if uref1.name == nme.UNIVERSE_SHORT && internal1 == nme.internal && rs1 == nme.reificationSupport && newNestedSymbol == nme.newNestedSymbol && + uref2.name == nme.UNIVERSE_SHORT && internal2 == nme.internal && rs2 == nme.reificationSupport && flagsRepr == nme.FlagsRepr => Some((uref1, name, flags, isClass)) case _ => None diff --git a/src/compiler/scala/reflect/reify/utils/NodePrinters.scala b/src/compiler/scala/reflect/reify/utils/NodePrinters.scala index e37b861461..3b91d28360 100644 --- a/src/compiler/scala/reflect/reify/utils/NodePrinters.scala +++ b/src/compiler/scala/reflect/reify/utils/NodePrinters.scala @@ -32,7 +32,7 @@ trait NodePrinters { s = "List\\[List\\[.*?\\].*?\\]".r.replaceAllIn(s, "List") s = "List\\[.*?\\]".r.replaceAllIn(s, "List") s = s.replace("immutable.this.Nil", "List()") - s = """build\.FlagsRepr\((\d+)[lL]\)""".r.replaceAllIn(s, m => { + s = """internal\.reificationSupport\.FlagsRepr\((\d+)[lL]\)""".r.replaceAllIn(s, m => { flagsAreUsed = true show(m.group(1).toLong) }) @@ -76,7 +76,6 @@ trait NodePrinters { if (mirrorIsUsed) printout += mirror.replace("Mirror[", "scala.reflect.api.Mirror[").trim val imports = scala.collection.mutable.ListBuffer[String]() imports += nme.UNIVERSE_SHORT.toString - // if (buildIsUsed) imports += nme.build if (mirrorIsUsed) imports += nme.MIRROR_SHORT.toString if (flagsAreUsed) imports += nme.Flag.toString printout += s"""import ${imports map (_ + "._") mkString ", "}""" diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 5ce0238b3b..03b76ed99e 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -34,6 +34,7 @@ import backend.jvm.GenASM import backend.opt.{ Inliners, InlineExceptionHandlers, ConstantOptimization, ClosureElimination, DeadCodeElimination } import backend.icode.analysis._ import scala.language.postfixOps +import scala.tools.nsc.ast.{TreeGen => AstTreeGen} class Global(var currentSettings: Settings, var reporter: Reporter) extends SymbolTable @@ -105,13 +106,10 @@ class Global(var currentSettings: Settings, var reporter: Reporter) // sub-components -------------------------------------------------- - /** Generate ASTs */ - type TreeGen = scala.tools.nsc.ast.TreeGen - /** Tree generation, usually based on existing symbols. */ override object gen extends { val global: Global.this.type = Global.this - } with TreeGen { + } with AstTreeGen { def mkAttributedCast(tree: Tree, pt: Type): Tree = typer.typed(mkCast(tree, pt)) } diff --git a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala index d3b5564f60..2bb874a8aa 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Implicits.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Implicits.scala @@ -1271,7 +1271,8 @@ trait Implicits { return SearchFailure } val cm = typed(Ident(ReflectRuntimeCurrentMirror)) - val interop = gen.mkMethodCall(ReflectRuntimeUniverse, nme.typeTagToManifest, List(tp), List(cm, tagInScope)) + val internal = gen.mkAttributedSelect(gen.mkAttributedRef(ReflectRuntimeUniverse), UniverseInternal) + val interop = gen.mkMethodCall(Select(internal, nme.typeTagToManifest), List(tp), List(cm, tagInScope)) wrapResult(interop) } } else { diff --git a/src/compiler/scala/tools/reflect/StdTags.scala b/src/compiler/scala/tools/reflect/StdTags.scala index 5c53c81e8b..ee352c5e02 100644 --- a/src/compiler/scala/tools/reflect/StdTags.scala +++ b/src/compiler/scala/tools/reflect/StdTags.scala @@ -18,8 +18,7 @@ trait StdTags { new TypeCreator { def apply[U <: ApiUniverse with Singleton](m: Mirror[U]): U # Type = { val u = m.universe - val pre = u.ThisType(m.staticPackage("scala.collection.immutable").moduleClass.asInstanceOf[u.Symbol]) - u.TypeRef(pre, u.definitions.ListClass, List(u.definitions.StringClass.toTypeConstructor)) + u.appliedType(u.definitions.ListClass.toType, List(u.definitions.StringClass.toType)) } }) diff --git a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala index b43b4653eb..ce6382bec8 100644 --- a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala +++ b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala @@ -217,7 +217,7 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => val (expr, freeTerms) = extractFreeTerms(expr0, wrapFreeTermRefs = true) val (obj, _) = rootMirror.EmptyPackageClass.newModuleAndClassSymbol( - nextWrapperModuleName()) + nextWrapperModuleName(), NoPosition, NoFlags) val minfo = ClassInfoType(List(ObjectTpe), newScope, obj.moduleClass) obj.moduleClass setInfo minfo diff --git a/src/compiler/scala/tools/reflect/quasiquotes/Holes.scala b/src/compiler/scala/tools/reflect/quasiquotes/Holes.scala index 2027d43264..c2f1bf430d 100644 --- a/src/compiler/scala/tools/reflect/quasiquotes/Holes.scala +++ b/src/compiler/scala/tools/reflect/quasiquotes/Holes.scala @@ -120,8 +120,8 @@ trait Holes { self: Quasiquotes => } private def toStats(tree: Tree): Tree = - // q"$u.build.toStats($tree)" - Apply(Select(Select(u, nme.build), nme.toStats), tree :: Nil) + // q"$u.internal.reificationSupport.toStats($tree)" + Apply(Select(Select(Select(u, nme.internal), nme.reificationSupport), nme.toStats), tree :: Nil) private def toList(tree: Tree, tpe: Type): Tree = if (isListType(tpe)) tree @@ -234,10 +234,10 @@ trait Holes { self: Quasiquotes => } val lifter = inferUnliftable(tpe) assert(helperName.isTermName) - // q"val $name: $u.build.${helperName.toTypeName} = $u.build.$helperName($lifter)" + // q"val $name: $u.internal.reificationSupport.${helperName.toTypeName} = $u.internal.reificationSupport.$helperName($lifter)" ValDef(NoMods, name, - AppliedTypeTree(Select(Select(u, nme.build), helperName.toTypeName), List(TypeTree(tpe))), - Apply(Select(Select(u, nme.build), helperName), lifter :: Nil)) + AppliedTypeTree(Select(Select(Select(u, nme.internal), nme.reificationSupport), helperName.toTypeName), List(TypeTree(tpe))), + Apply(Select(Select(Select(u, nme.internal), nme.reificationSupport), helperName), lifter :: Nil)) } } } diff --git a/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala b/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala index 017e966f63..9078228314 100644 --- a/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala +++ b/src/compiler/scala/tools/reflect/quasiquotes/Reifiers.scala @@ -64,9 +64,9 @@ trait Reifiers { self: Quasiquotes => val FreshName(prefix) = origname val nameTypeName = if (origname.isTermName) tpnme.TermName else tpnme.TypeName val freshName = if (origname.isTermName) nme.freshTermName else nme.freshTypeName - // q"val ${names.head}: $u.$nameTypeName = $u.build.$freshName($prefix)" + // q"val ${names.head}: $u.$nameTypeName = $u.internal.reificationSupport.$freshName($prefix)" ValDef(NoMods, names.head, Select(u, nameTypeName), - Apply(Select(Select(u, nme.build), freshName), Literal(Constant(prefix)) :: Nil)) + Apply(Select(Select(Select(u, nme.internal), nme.reificationSupport), freshName), Literal(Constant(prefix)) :: Nil)) }.toList // q"..$freshdefs; $tree" SyntacticBlock(freshdefs :+ tree) @@ -358,7 +358,7 @@ trait Reifiers { self: Quasiquotes => Apply(Select(universe, name), args.toList) override def mirrorBuildCall(name: TermName, args: Tree*): Tree = - Apply(Select(Select(universe, nme.build), name), args.toList) + Apply(Select(Select(Select(universe, nme.internal), nme.reificationSupport), name), args.toList) override def scalaFactoryCall(name: String, args: Tree*): Tree = call("scala." + name, args: _*) -- cgit v1.2.3