diff options
author | Martin Odersky <odersky@gmail.com> | 2011-09-20 15:13:16 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2011-09-20 15:13:16 +0000 |
commit | 088094b1c82ee38b10d84c4826b740a4e0f5f0fe (patch) | |
tree | 126be0feb2d2c910692f141117110f3e0fd29971 /src | |
parent | f6d4d84dd7eb5de3e7f698c7f627d483b6999142 (diff) | |
download | scala-088094b1c82ee38b10d84c4826b740a4e0f5f0fe.tar.gz scala-088094b1c82ee38b10d84c4826b740a4e0f5f0fe.tar.bz2 scala-088094b1c82ee38b10d84c4826b740a4e0f5f0fe.zip |
Changes to Liftcode to use new reflection seman...
Changes to Liftcode to use new reflection semantics, where a compiler
uses type checking.
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/reflect/runtime/Mirror.scala | 2 | ||||
-rw-r--r-- | src/compiler/scala/reflect/runtime/RuntimeTypes.scala | 14 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/LiftCode.scala | 52 | ||||
-rw-r--r-- | src/library/scala/reflect/Code.scala | 4 |
4 files changed, 49 insertions, 23 deletions
diff --git a/src/compiler/scala/reflect/runtime/Mirror.scala b/src/compiler/scala/reflect/runtime/Mirror.scala index 0591de5f78..9e3f51dc98 100644 --- a/src/compiler/scala/reflect/runtime/Mirror.scala +++ b/src/compiler/scala/reflect/runtime/Mirror.scala @@ -39,6 +39,8 @@ class Mirror extends Universe with RuntimeTypes with api.Mirror { def staticClass(name: String): Symbol = definitions.getClass(newTypeName(name)) def staticModule(name: String): Symbol = definitions.getModule(newTermName(name)) + def freeVar(name: String, info: Type, value: Any) = new FreeVar(name, info, value) + /** Selects type symbol with given name from the defined members of prefix type */ def selectType(owner: Symbol, name: String): Symbol = diff --git a/src/compiler/scala/reflect/runtime/RuntimeTypes.scala b/src/compiler/scala/reflect/runtime/RuntimeTypes.scala index 6a2b0d17b1..26681dff4d 100644 --- a/src/compiler/scala/reflect/runtime/RuntimeTypes.scala +++ b/src/compiler/scala/reflect/runtime/RuntimeTypes.scala @@ -5,13 +5,15 @@ import collection.mutable.ListBuffer trait RuntimeTypes extends Universe with api.RuntimeTypes { - def freeValue(x: Any): Tree = FreeValue(x) + case class FreeVar(_name: TermName, _tpe: Type, value: Any) extends TermSymbol(definitions.RootClass, NoPosition, _name) { + setInfo(_tpe) - // to do: replace with generalized - // case class Literal(x: Any), - // once calls to the deprecated factory Literal(x: Any) has been eliminated from all code. - case class FreeValue(any: Any) extends Tree { - protected def errorSubtrees = Nil + override def hashCode = value.hashCode + + override def equals(other: Any): Boolean = other match { + case FreeVar(_, _, value1) => value.asInstanceOf[AnyRef] eq value1.asInstanceOf[AnyRef] + case _ => false + } } case class InstanceRefSymbol(value: AnyRef) extends TermSymbol(NoSymbol, NoPosition, nme.EMPTY) diff --git a/src/compiler/scala/tools/nsc/transform/LiftCode.scala b/src/compiler/scala/tools/nsc/transform/LiftCode.scala index bc91f7d8da..ce7938a60e 100644 --- a/src/compiler/scala/tools/nsc/transform/LiftCode.scala +++ b/src/compiler/scala/tools/nsc/transform/LiftCode.scala @@ -97,21 +97,28 @@ abstract class LiftCode extends Transform with TypingTransformers { private val localSyms = mutable.ArrayBuffer[Symbol]() private val symIndex = mutable.HashMap[Symbol, Int]() + private var localClasses = Set[Symbol]() private val typeTree = mutable.HashMap[Type, Tree]() private val typeTreeCount = mutable.HashMap[Tree, Int]() /** Generate tree of the form * - * { val $localSyms = Array(sym1, ..., symN) - * localSyms(1).setInfo(tpe1) + * { val $mr = scala.reflect.runtime.Mirror + * val $memo = new scala.reflext.runtime.Memoizer + * $local1 = new TypeSymbol(owner1, NoPosition, name1) * ... - * localSyms(N).setInfo(tpeN) + * $localN = new TermSymbol(ownerN, NoPositiion, nameN) + * $local1.setInfo(tpe1) + * ... + * $localN.setInfo(tpeN) + * $localN.setAnnotations(annotsN) * rtree * } * * where * - * - `symI` are the symbols defined locally in `tree` + * - `$localI` are free type symbols in the environment, as well as local symbols + * of refinement types. * - `tpeI` are the info's of `symI` * - `rtree` is code that generates `tree` at runtime, maintaining all attributes. */ @@ -131,6 +138,8 @@ abstract class LiftCode extends Transform with TypingTransformers { // helper methods + private def localName(sym: Symbol) = localPrefix + symIndex(sym) + private def call(fname: String, args: Tree*): Tree = Apply(termPath(fname), args.toList) @@ -178,7 +187,7 @@ abstract class LiftCode extends Transform with TypingTransformers { private def reifySymRef(sym: Symbol): Tree = { symIndex get sym match { case Some(idx) => - Ident(localPrefix + symIndex(sym)) + Ident(localName(sym)) case None => if (sym.isStatic) mirrorCall(if (sym.isType) "staticClass" else "staticModule", reify(sym.fullName)) @@ -192,10 +201,15 @@ abstract class LiftCode extends Transform with TypingTransformers { else mirrorCall("selectTerm", rowner, rname, reify(sym.tpe)) } else { - println("Late local: "+sym) - assert(!sym.isParameter, sym+"/"+sym.owner+"/"+sym.owner.info+"/"+sym.paramPos) - registerLocalSymbol(sym) - reifySymRef(sym) + if (sym.isTerm) { + println("Free: "+sym) + mirrorCall("freeVar", reify(sym.name.toString), reify(sym.tpe), Ident(sym)) + } else { + println("Late local: "+sym) + assert(!sym.isParameter, sym+"/"+sym.owner+"/"+sym.owner.info+"/"+sym.paramPos) + registerLocalSymbol(sym) + reifySymRef(sym) + } } } } @@ -209,7 +223,7 @@ abstract class LiftCode extends Transform with TypingTransformers { List(List(reify(sym.owner), reify(sym.pos), reify(sym.name)))) if (sym.flags != 0L) rsym = Apply(Select(rsym, "setFlag"), List(Literal(Constant(sym.flags)))) - ValDef(NoMods, localPrefix + symIndex(sym), TypeTree(), rsym) + ValDef(NoMods, localName(sym), TypeTree(), rsym) } /** Generate code to add type and annotation info to a reified symbol @@ -268,17 +282,23 @@ abstract class LiftCode extends Transform with TypingTransformers { /** Reify a tree */ private def reifyTree(tree: Tree): Tree = tree match { - case tree @ This(_) if !(symIndex isDefinedAt tree.symbol) => - reifyFree(tree) - case tree @ Ident(_) if !(symIndex isDefinedAt tree.symbol) => + case EmptyTree => + reifyCaseObject(tree) + case ClassDef(_, _, _, _) => + localClasses += tree.symbol + reifyCaseClassInstance(tree) + case tree @ This(_) if !(localClasses contains tree.symbol) => reifyFree(tree) case _ => - var rtree = reifyCaseClassInstance(tree.asInstanceOf[Product]) +// var rtree = + reifyCaseClassInstance(tree.asInstanceOf[Product]) +/* if (tree.isDef || tree.isInstanceOf[Function]) registerLocalSymbol(tree.symbol) if (tree.hasSymbol) rtree = Apply(Select(rtree, "setSymbol"), List(reifySymRef(tree.symbol))) Apply(Select(rtree, "setType"), List(reifyType(tree.tpe))) +*/ } /** Reify a free reference. The result will be either a mirror reference @@ -287,8 +307,8 @@ abstract class LiftCode extends Transform with TypingTransformers { private def reifyFree(tree: Tree): Tree = if (tree.symbol.hasFlag(MODULE) && tree.symbol.isStatic) reify(termPath(tree.symbol.fullName)) - else - mirrorCall("Literal", tree) + else // make an Ident to a freeVar + mirrorCall("Ident", reifySymRef(tree.symbol)) /** Reify an arbitary value */ private def reify(value: Any): Tree = { diff --git a/src/library/scala/reflect/Code.scala b/src/library/scala/reflect/Code.scala index 52a7f2119a..ed20e242fd 100644 --- a/src/library/scala/reflect/Code.scala +++ b/src/library/scala/reflect/Code.scala @@ -11,7 +11,9 @@ package scala.reflect /** This type is required by the compiler and <b>should not be used in client code</b>. */ -class Code[T](val tree: scala.reflect.mirror.Tree) +class Code[T](val tree: scala.reflect.mirror.Tree) { + +} /** This type is required by the compiler and <b>should not be used in client code</b>. */ object Code { |