diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-03 00:40:13 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-07-03 00:40:13 -0700 |
commit | bc2e6fe8bf20b2aa985ea78a2c610ac33497da38 (patch) | |
tree | 5c8fd6a4121994ce6a62addf9566e13aa4e7e9f1 /src/reflect | |
parent | 035224a17def57b25f7a8ed69d050451b8e7103d (diff) | |
parent | 96dd73146f4317feeb59a242d692977f87c5211d (diff) | |
download | scala-bc2e6fe8bf20b2aa985ea78a2c610ac33497da38.tar.gz scala-bc2e6fe8bf20b2aa985ea78a2c610ac33497da38.tar.bz2 scala-bc2e6fe8bf20b2aa985ea78a2c610ac33497da38.zip |
Merge pull request #810 from scalamacros/pullrequest/manifests-and-ticket6005
two pullrequests from this morning, combined to merge changes to starrs
Diffstat (limited to 'src/reflect')
-rw-r--r-- | src/reflect/scala/reflect/api/Printers.scala | 39 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Definitions.scala | 7 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Printers.scala | 75 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/StdNames.scala | 4 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Symbols.scala | 6 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Types.scala | 22 | ||||
-rw-r--r-- | src/reflect/scala/reflect/runtime/JavaMirrors.scala | 1 | ||||
-rw-r--r-- | src/reflect/scala/reflect/runtime/ReflectionUtils.scala | 15 |
8 files changed, 107 insertions, 62 deletions
diff --git a/src/reflect/scala/reflect/api/Printers.scala b/src/reflect/scala/reflect/api/Printers.scala index 7f4ff8a7fb..27d3b8ba7d 100644 --- a/src/reflect/scala/reflect/api/Printers.scala +++ b/src/reflect/scala/reflect/api/Printers.scala @@ -10,12 +10,15 @@ trait Printers { self: Universe => protected var printTypes = false protected var printIds = false protected var printKinds = false + protected var printMirrors = false def withTypes: this.type = { printTypes = true; this } def withoutTypes: this.type = { printTypes = false; this } def withIds: this.type = { printIds = true; this } def withoutIds: this.type = { printIds = false; this } def withKinds: this.type = { printKinds = true; this } def withoutKinds: this.type = { printKinds = false; this } + def withMirrors: this.type = { printMirrors = true; this } + def withoutMirrors: this.type = { printMirrors = false; this } } case class BooleanFlag(val value: Option[Boolean]) @@ -25,13 +28,14 @@ trait Printers { self: Universe => implicit def optionToBooleanFlag(value: Option[Boolean]): BooleanFlag = BooleanFlag(value) } - protected def render(what: Any, mkPrinter: PrintWriter => TreePrinter, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None): String = { + protected def render(what: Any, mkPrinter: PrintWriter => TreePrinter, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None, printMirrors: BooleanFlag = None): String = { val buffer = new StringWriter() val writer = new PrintWriter(buffer) var printer = mkPrinter(writer) printTypes.value.map(printTypes => if (printTypes) printer.withTypes else printer.withoutTypes) printIds.value.map(printIds => if (printIds) printer.withIds else printer.withoutIds) printKinds.value.map(printKinds => if (printKinds) printer.withKinds else printer.withoutKinds) + printMirrors.value.map(printMirrors => if (printMirrors) printer.withMirrors else printer.withoutMirrors) printer.print(what) writer.flush() buffer.toString @@ -40,42 +44,25 @@ trait Printers { self: Universe => /** By default trees are printed with `show` */ override protected def treeToString(tree: Tree) = show(tree) - /** Renders a prettified representation of a tree. + /** Renders a prettified representation of a reflection artifact. * Typically it looks very close to the Scala code it represents. - * This function is used in Tree.toString. */ - def show(tree: Tree, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None): String = - render(tree, newTreePrinter(_), printTypes, printIds, printKinds) + def show(any: Any, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None, printMirrors: BooleanFlag = None): String = + render(any, newTreePrinter(_), printTypes, printIds, printKinds, printMirrors) - /** Hook to define what `show(tree)` means. + /** Hook to define what `show(...)` means. */ def newTreePrinter(out: PrintWriter): TreePrinter - /** Renders internal structure of a tree. + /** Renders internal structure of a reflection artifact. */ - def showRaw(tree: Tree, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None): String = - render(tree, newRawTreePrinter(_), printTypes, printIds, printKinds) + def showRaw(any: Any, printTypes: BooleanFlag = None, printIds: BooleanFlag = None, printKinds: BooleanFlag = None, printMirrors: BooleanFlag = None): String = + render(any, newRawTreePrinter(_), printTypes, printIds, printKinds, printMirrors) - /** Hook to define what `showRaw(tree)` means. + /** Hook to define what `showRaw(...)` means. */ def newRawTreePrinter(out: PrintWriter): TreePrinter - /** Renders a prettified representation of a symbol. - */ - def show(sym: Symbol): String = sym.toString - - /** Renders internal structure of a symbol. - */ - def showRaw(sym: Symbol): String = render(sym, newRawTreePrinter(_)) - - /** Renders a prettified representation of a type. - */ - def show(tpe: Type): String = tpe.toString - - /** Renders internal structure of a type. - */ - def showRaw(tpe: Type): String = render(tpe, newRawTreePrinter(_)) - /** Renders a prettified representation of a name. */ def show(name: Name): String diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index 320cd3ddae..7891433b4f 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -35,7 +35,6 @@ trait Definitions extends api.StandardDefinitions { lazy val AnyRefTpe = definitions.AnyRefClass.asType lazy val NothingTpe = definitions.NothingClass.asType lazy val NullTpe = definitions.NullClass.asType - lazy val StringTpe = definitions.StringClass.asType /** Since both the value parameter types and the result type may * require access to the type parameter symbols, we model polymorphic @@ -454,10 +453,10 @@ trait Definitions extends api.StandardDefinitions { def ReflectRuntimeUniverse = if (ReflectRuntimePackage != NoSymbol) getMemberValue(ReflectRuntimePackage, nme.universe) else NoSymbol def ReflectRuntimeCurrentMirror = if (ReflectRuntimePackage != NoSymbol) getMemberMethod(ReflectRuntimePackage, nme.currentMirror) else NoSymbol - lazy val PartialManifestClass = requiredClass[scala.reflect.ClassManifest[_]] - lazy val PartialManifestModule = requiredModule[scala.reflect.ClassManifest.type] + lazy val PartialManifestClass = getMemberType(ReflectPackage, tpnme.ClassManifest) + lazy val PartialManifestModule = requiredModule[scala.reflect.ClassManifestFactory.type] lazy val FullManifestClass = requiredClass[scala.reflect.Manifest[_]] - lazy val FullManifestModule = requiredModule[scala.reflect.Manifest.type] + lazy val FullManifestModule = requiredModule[scala.reflect.ManifestFactory.type] lazy val OptManifestClass = requiredClass[scala.reflect.OptManifest[_]] lazy val NoManifest = requiredModule[scala.reflect.NoManifest.type] diff --git a/src/reflect/scala/reflect/internal/Printers.scala b/src/reflect/scala/reflect/internal/Printers.scala index 82a8c42f7c..c018ddc88e 100644 --- a/src/reflect/scala/reflect/internal/Printers.scala +++ b/src/reflect/scala/reflect/internal/Printers.scala @@ -10,6 +10,7 @@ package internal import java.io.{ OutputStream, PrintWriter, StringWriter, Writer } import Flags._ +import compat.Platform.EOL trait Printers extends api.Printers { self: SymbolTable => @@ -65,6 +66,7 @@ trait Printers extends api.Printers { self: SymbolTable => printTypes = settings.printtypes.value printIds = settings.uniqid.value printKinds = settings.Yshowsymkinds.value + printMirrors = false // typically there's no point to print mirrors inside the compiler, as there is only one mirror there protected def doPrintPositions = settings.Xprintpos.value def indent() = indentMargin += indentStep @@ -477,22 +479,61 @@ trait Printers extends api.Printers { self: SymbolTable => def flush = { /* do nothing */ } } - // provides footnotes for types - private var typeCounter = 0 - private val typeMap = collection.mutable.WeakHashMap[Type, Int]() - def newRawTreePrinter(writer: PrintWriter): RawTreePrinter = new RawTreePrinter(writer) def newRawTreePrinter(stream: OutputStream): RawTreePrinter = newRawTreePrinter(new PrintWriter(stream)) def newRawTreePrinter(): RawTreePrinter = newRawTreePrinter(new PrintWriter(ConsoleWriter)) + // provides footnotes for types and mirrors + import scala.collection.mutable.{Map, WeakHashMap, SortedSet} + private val footnoteIndex = new FootnoteIndex + private class FootnoteIndex { + private val index = Map[Class[_], WeakHashMap[Any, Int]]() + private def classIndex[T: ClassTag] = index.getOrElseUpdate(classTag[T].runtimeClass, WeakHashMap[Any, Int]()) + private val counters = Map[Class[_], Int]() + private def nextCounter[T: ClassTag] = { + val clazz = classTag[T].runtimeClass + counters.getOrElseUpdate(clazz, 0) + counters(clazz) = counters(clazz) + 1 + counters(clazz) + } + + def mkFootnotes() = new Footnotes + class Footnotes { + private val footnotes = Map[Class[_], SortedSet[Int]]() + private def classFootnotes[T: ClassTag] = footnotes.getOrElseUpdate(classTag[T].runtimeClass, SortedSet[Int]()) + + def put[T: ClassTag](any: T): Int = { + val index = classIndex[T].getOrElseUpdate(any, nextCounter[T]) + classFootnotes[T] += index + index + } + + def get[T: ClassTag]: List[(Int, Any)] = + classFootnotes[T].toList map (fi => (fi, classIndex[T].find{ case (any, ii) => ii == fi }.get._1)) + + def print[T: ClassTag](printer: Printers.super.TreePrinter): Unit = { + val footnotes = get[T] + if (footnotes.nonEmpty) { + printer.print(EOL) + footnotes.zipWithIndex foreach { + case ((fi, any), ii) => + printer.print("[", fi, "] ", any) + if (ii < footnotes.length - 1) printer.print(EOL) + } + } + } + } + } + // emits more or less verbatim representation of the provided tree class RawTreePrinter(out: PrintWriter) extends super.TreePrinter { private var depth = 0 - private var footnotes = collection.mutable.Map[Int, Type]() - private var printingFootnotes = false private var printTypesInFootnotes = true + private var printingFootnotes = false + private var footnotes = footnoteIndex.mkFootnotes() def print(args: Any*): Unit = { + // don't print type footnotes if the argument is a mere type if (depth == 0 && args.length == 1 && args(0) != null && args(0).isInstanceOf[Type]) printTypesInFootnotes = false @@ -544,14 +585,15 @@ trait Printers extends api.Printers { self: SymbolTable => else print(sym.name) if (printIds) print("#", sym.id) if (printKinds) print("#", sym.abbreviatedKindString) + if (printMirrors) print("%M", footnotes.put[MirrorOf[_]](mirrorThatLoaded(sym))) case NoType => print("NoType") case NoPrefix => print("NoPrefix") - case tpe: Type if printTypesInFootnotes && !printingFootnotes => - val index = typeMap.getOrElseUpdate(tpe, { typeCounter += 1; typeCounter }) - footnotes(index) = tpe - print("[", index, "]") + case tpe: Type => + val defer = printTypesInFootnotes && !printingFootnotes + if (defer) print("[", footnotes.put(tpe), "]") + else printProduct(tpe.asInstanceOf[Product]) case mods: Modifiers => print("Modifiers(") if (mods.flags != NoFlags || mods.privateWithin != tpnme.EMPTY || mods.annotations.nonEmpty) print(show(mods.flags)) @@ -569,16 +611,11 @@ trait Printers extends api.Printers { self: SymbolTable => out.print(arg) } depth -= 1 - if (depth == 0 && footnotes.nonEmpty && !printingFootnotes) { + if (depth == 0 && !printingFootnotes) { printingFootnotes = true - out.println() - val typeIndices = footnotes.keys.toList.sorted - typeIndices.zipWithIndex foreach { - case (typeIndex, i) => - print("[" + typeIndex + "] ") - print(footnotes(typeIndex)) - if (i < typeIndices.length - 1) out.println() - } + footnotes.print[Type](this) + footnotes.print[MirrorOf[_]](this) + printingFootnotes = false } } diff --git a/src/reflect/scala/reflect/internal/StdNames.scala b/src/reflect/scala/reflect/internal/StdNames.scala index bd02013037..72a99589d5 100644 --- a/src/reflect/scala/reflect/internal/StdNames.scala +++ b/src/reflect/scala/reflect/internal/StdNames.scala @@ -232,6 +232,7 @@ trait StdNames { final val Annotation: NameType = "Annotation" final val ClassfileAnnotation: NameType = "ClassfileAnnotation" + final val ClassManifest: NameType = "ClassManifest" final val Enum: NameType = "Enum" final val Group: NameType = "Group" final val Tree: NameType = "Tree" @@ -639,8 +640,8 @@ trait StdNames { val bytes: NameType = "bytes" val canEqual_ : NameType = "canEqual" val checkInitialized: NameType = "checkInitialized" + val ClassManifestFactory: NameType = "ClassManifestFactory" val classOf: NameType = "classOf" - val classTagToClassManifest: NameType = "classTagToClassManifest" val clone_ : NameType = if (forMSIL) "MemberwiseClone" else "clone" // sn.OClone causes checkinit failure val conforms: NameType = "conforms" val copy: NameType = "copy" @@ -696,6 +697,7 @@ trait StdNames { val macroContext : NameType = "c" val main: NameType = "main" val manifest: NameType = "manifest" + val ManifestFactory: NameType = "ManifestFactory" val manifestToTypeTag: NameType = "manifestToTypeTag" val map: NameType = "map" val materializeClassTag: NameType = "materializeClassTag" diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala index abdb344b92..119c3d42fd 100644 --- a/src/reflect/scala/reflect/internal/Symbols.scala +++ b/src/reflect/scala/reflect/internal/Symbols.scala @@ -899,15 +899,12 @@ trait Symbols extends api.Symbols { self: SymbolTable => if (owner.isTerm) return false if (isLocalDummy) return false + if (isAliasType) return true if (isType && isNonClassType) return false if (isRefinementClass) return false return true } - // [Eugene] is it a good idea to add ``dealias'' to Symbol? - /** Expands type aliases */ - def dealias: Symbol = this - /** The variance of this symbol as an integer */ final def variance: Int = if (isCovariant) 1 @@ -2563,7 +2560,6 @@ trait Symbols extends api.Symbols { self: SymbolTable => extends TypeSymbol(initOwner, initPos, initName) { type TypeOfClonedSymbol = TypeSymbol final override def isAliasType = true - final override def dealias = info.typeSymbol.dealias override def cloneSymbolImpl(owner: Symbol, newFlags: Long): TypeSymbol = owner.newNonClassSymbol(name, pos, newFlags) } diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala index 4cf2cceb81..5a382cf518 100644 --- a/src/reflect/scala/reflect/internal/Types.scala +++ b/src/reflect/scala/reflect/internal/Types.scala @@ -568,6 +568,24 @@ trait Types extends api.Types { self: SymbolTable => /** Expands type aliases. */ def dealias = this + def etaExpand: Type = this + + /** Performs a single step of beta-reduction on types. + * Given: + * + * type C[T] = B[T] + * type B[T] = A + * class A + * + * The following will happen after `betaReduce` is invoked: + * TypeRef(pre, <C>, List(Int)) is replaced by + * TypeRef(pre, <B>, List(Int)) + * + * Unlike `dealias`, which recursively applies beta reduction, until it's stuck, + * `betaReduce` performs exactly one step and then returns. + */ + def betaReduce: Type = this + /** For a classtype or refined type, its defined or declared members; * inherited by subtypes and typerefs. * The empty scope for all other types. @@ -2110,7 +2128,7 @@ trait Types extends api.Types { self: SymbolTable => // // this crashes pos/depmet_implicit_tpbetareduce.scala // appliedType(sym.info, typeArgs).asSeenFrom(pre, sym.owner) - def betaReduce = transform(sym.info.resultType) + override def betaReduce = transform(sym.info.resultType) // #3731: return sym1 for which holds: pre bound sym.name to sym and // pre1 now binds sym.name to sym1, conceptually exactly the same @@ -2221,7 +2239,7 @@ trait Types extends api.Types { self: SymbolTable => || pre.isGround && args.forall(_.isGround) ) - def etaExpand: Type = { + override def etaExpand: Type = { // must initialise symbol, see test/files/pos/ticket0137.scala val tpars = initializedTypeParams if (tpars.isEmpty) this diff --git a/src/reflect/scala/reflect/runtime/JavaMirrors.scala b/src/reflect/scala/reflect/runtime/JavaMirrors.scala index 38d280ec73..41955170bd 100644 --- a/src/reflect/scala/reflect/runtime/JavaMirrors.scala +++ b/src/reflect/scala/reflect/runtime/JavaMirrors.scala @@ -972,6 +972,7 @@ trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: Sym case ExistentialType(_, rtpe) => typeToJavaClass(rtpe) case TypeRef(_, ArrayClass, List(elemtpe)) => jArrayClass(typeToJavaClass(elemtpe)) case TypeRef(_, sym: ClassSymbol, _) => classToJava(sym.asClassSymbol) + case tpe @ TypeRef(_, sym: AliasTypeSymbol, _) => typeToJavaClass(tpe.dealias) case _ => throw new NoClassDefFoundError("no Java class corresponding to "+tpe+" found") } } diff --git a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala index 4e82fe8ad2..7839850529 100644 --- a/src/reflect/scala/reflect/runtime/ReflectionUtils.scala +++ b/src/reflect/scala/reflect/runtime/ReflectionUtils.scala @@ -37,12 +37,17 @@ object ReflectionUtils { systemProperties find (_._1 endsWith ".boot.class.path") map (_._2) getOrElse "" ) - def show(cl: ClassLoader) = { + def show(cl: ClassLoader): String = { + def isAbstractFileClassLoader(clazz: Class[_]): Boolean = { + if (clazz == null) return false + if (clazz.getName == "scala.tools.nsc.interpreter.AbstractFileClassLoader") return true + return isAbstractFileClassLoader(clazz.getSuperclass) + } def inferClasspath(cl: ClassLoader): String = cl match { case cl: java.net.URLClassLoader => - "[" + (cl.getURLs mkString ",") + "]" - case cl if cl != null && cl.getClass.getName == "scala.tools.nsc.interpreter.AbstractFileClassLoader" => - "[" + cl.asInstanceOf[{val root: scala.reflect.internal.AbstractFileApi}].root + "] and " + inferClasspath(cl.getParent) + (cl.getURLs mkString ",") + case cl if cl != null && isAbstractFileClassLoader(cl.getClass) => + cl.asInstanceOf[{val root: scala.reflect.internal.AbstractFileApi}].root.canonicalPath case null => inferBootClasspath case _ => @@ -50,7 +55,7 @@ object ReflectionUtils { } cl match { case cl if cl != null => - "%s of type %s with classpath %s".format(cl, cl.getClass, inferClasspath(cl)) + "%s of type %s with classpath [%s] and parent being %s".format(cl, cl.getClass, inferClasspath(cl), show(cl.getParent)) case null => "primordial classloader with boot classpath [%s]".format(inferClasspath(cl)) } |