Permalink
Browse files

The new reflection

A must read: "SIP: Scala Reflection":
https://docs.google.com/document/d/1Z1VhhNPplbUpaZPIYdc0_EUv5RiGQ2X4oqp0i-vz1qw/edit

Highlights:
* Architecture has undergone a dramatic rehash.
* Universes and mirrors are now separate entities:
universes host reflection artifacts (trees, symbols, types, etc),
mirrors abstract loading of those artifacts (e.g. JavaMirror loads stuff
using a classloader and annotation unpickler, while GlobalMirror uses
internal compiler classreader to achieve the same goal).
* No static reflection mirror is imposed on the user.
One is free to choose between lightweight mirrors and full-blown
classloader-based mirror (read below).
* Public reflection API is split into scala.reflect.base and scala.reflect.api.
The former represents a minimalistic snapshot that is exactly enough to
build reified trees and types. To build, but not to analyze - everything smart
(for example, getting a type signature) is implemented in scala.reflect.api.
* Both reflection domains have their own universe: scala.reflect.basis and
scala.reflect.runtime.universe. The former is super lightweight and doesn't
involve any classloaders, while the latter represents a stripped down compiler.
* Classloader problems from 2.10.0-M3 are solved.
* Exprs and type tags are now bound to a mirror upon creation.
* However there is an easy way to migrate exprs and type tags between mirrors
and even between universes.
* This means that no classloader is imposed on the user of type tags and exprs.
If one doesn't like a classloader that's there (associated with tag's mirror),
one can create a custom mirror and migrate the tag or the expr to it.
* There is a shortcut that works in most cases. Requesting a type tag from
a full-blown universe will create that tag in a mirror that corresponds to
the callsite classloader aka `getClass.getClassLoader`. This imposes no
obligations on the programmer, since Type construction is lazy, so one
can always migrate a tag into a different mirror.

Migration notes for 2.10.0-M3 users:
* Incantations in Predef are gone, some of them have moved to scala.reflect.
* Everything path-dependent requires implicit prefix (for example, to refer
to a type tag, you need to explicitly specify the universe it belongs to,
e.g. reflect.basis.TypeTag or reflect.runtime.universe.TypeTag).
* ArrayTags have been removed, ConcreteTypeTag have been renamed to TypeTags,
TypeTags have been renamed to AbsTypeTags. Look for the reasoning in the
nearby children of this commit. Why not in this commit? Scroll this message
to the very bottom to find out the reason.
* Some of the functions have been renamed or moved around.
The rule of thumb is to look for anything non-trivial in scala.reflect.api.
Some of tree build utils have been moved to Universe.build.
* staticModule and staticClass have been moved from universes to mirrors
* ClassTag.erasure => ClassTag.runtimeClass
* For the sake of purity, type tags no longer have erasures.
Use multiple context bounds (e.g. def foo[T: ru.TypeTag : ClassTag](...) = ...)
if you're interested in having both erasures and types for type parameters.
* reify now rolls back macro applications.
* Runtime evaluation is now explicit, requires import scala.tools.reflect.Eval
and scala-compiler.jar on the classpath.
* Macro context now has separate universe and mirror fields.
* Most of the useful stuff is declared in c.universe,
so be sure to change your "import c.universe._" to "import c.mirror._".
* Due to the changes in expressions and type tags, their regular factories
are now really difficult to use. We acknowledge that macro users need to
frequently create exprs and tags, so we added old-style factories to context.
Bottom line: almost always prepend Expr(...)/TypeTag(...) with "c.".
* Expr.eval has been renamed to Expr.splice.
* Expr.value no longer splices (it can still be used to express cross-stage
path-dependent types as specified in SIP-16).
* c.reifyTree now has a mirror parameter that lets one customize the initial
mirror the resulting Expr will be bound to. If you provide EmptyTree, then
the reifier will automatically pick a reasonable mirror (callsite classloader
mirror for a full-blown universe and rootMirror for a basis universe).
Bottom line: this parameter should be EmptyTree in 99% of cases.
* c.reifyErasure => c.reifyRuntimeClass.

Known issues:
* API is really raw, need your feedback.
* All reflection artifacts are now represented by abstract types.
This means that pattern matching against them will emit unchecked warnings.
Adriaan is working on a patch that will fix that.

WARNING, FELLOW CODE EXPLORER! You have entered a turbulence zone.
For this commit and its nearby parents and children
tests are not guaranteed to work. Things get back to normal only after
the "repairs the tests after the refactoring spree" commit.

Why so weird? These twentish changesets were once parts of a humongous blob,
which spanned 1200 files and 15 kLOC. I did my best to split up the blob,
so that the individual parts of the code compile and make sense in isolation.
However doing the same for tests would be too much work.
  • Loading branch information...
1 parent 8ce4787 commit 6bb5975289c5b11cb8c88dd4629286956b5d3d27 @xeno-by xeno-by committed Jun 6, 2012
Showing with 9,001 additions and 6,403 deletions.
  1. +1 −1 lib/scala-compiler.jar.desired.sha1
  2. +1 −1 lib/scala-library.jar.desired.sha1
  3. +19 −0 src/compiler/scala/reflect/api/JavaUniverse.scala
  4. +38 −0 src/compiler/scala/reflect/api/TagInterop.scala
  5. +2 −2 ...ary/scala/reflect/api/RequiredFile.scala → compiler/scala/reflect/internal/AbstractFileApi.scala}
  6. +10 −6 src/compiler/scala/reflect/internal/AnnotationInfos.scala
  7. +69 −0 src/compiler/scala/reflect/internal/BuildUtils.scala
  8. +3 −1 src/compiler/scala/reflect/internal/Constants.scala
  9. +196 −319 src/compiler/scala/reflect/internal/Definitions.scala
  10. +66 −0 src/compiler/scala/reflect/internal/FlagSets.scala
  11. +0 −41 src/compiler/scala/reflect/internal/Flags.scala
  12. +0 −60 src/compiler/scala/reflect/internal/FreeVars.scala
  13. +1 −0 src/compiler/scala/reflect/internal/HasFlags.scala
  14. +4 −5 src/compiler/scala/reflect/internal/Importers.scala
  15. +243 −0 src/compiler/scala/reflect/internal/Mirrors.scala
  16. +8 −2 src/compiler/scala/reflect/internal/Names.scala
  17. +1 −0 src/compiler/scala/reflect/internal/Positions.scala
  18. +1 −1 src/compiler/scala/reflect/internal/Required.scala
  19. +2 −0 src/compiler/scala/reflect/internal/Scopes.scala
  20. +2 −0 src/compiler/scala/reflect/internal/StdAttachments.scala
  21. +21 −0 src/compiler/scala/reflect/internal/StdCreators.scala
  22. +157 −129 src/compiler/scala/reflect/internal/StdNames.scala
  23. +14 −9 src/compiler/scala/reflect/internal/SymbolTable.scala
  24. +71 −93 src/compiler/scala/reflect/internal/Symbols.scala
  25. +0 −66 src/compiler/scala/reflect/internal/TreeBuildUtil.scala
  26. +9 −1 src/compiler/scala/reflect/internal/TreeGen.scala
  27. +6 −29 src/compiler/scala/reflect/internal/TreeInfo.scala
  28. +4 −2 src/compiler/scala/reflect/internal/TreePrinters.scala
  29. +1,241 −84 src/compiler/scala/reflect/internal/Trees.scala
  30. +51 −24 src/compiler/scala/reflect/internal/Types.scala
  31. +6 −0 src/compiler/scala/reflect/internal/package.scala
  32. +8 −7 src/compiler/scala/reflect/internal/pickling/UnPickler.scala
  33. +1 −1 src/compiler/scala/reflect/internal/util/TraceSymbolActivity.scala
  34. +1 −1 src/compiler/scala/reflect/makro/runtime/AbortMacroException.scala
  35. +3 −1 src/compiler/scala/reflect/makro/runtime/Aliases.scala
  36. +2 −1 src/compiler/scala/reflect/makro/runtime/Context.scala
  37. +8 −0 src/compiler/scala/reflect/makro/runtime/Exprs.scala
  38. +3 −1 src/compiler/scala/reflect/makro/runtime/FrontEnds.scala
  39. +6 −12 src/compiler/scala/reflect/makro/runtime/Reifiers.scala
  40. +0 −10 src/compiler/scala/reflect/makro/runtime/Symbols.scala
  41. +9 −0 src/compiler/scala/reflect/makro/runtime/TypeTags.scala
  42. +1 −88 src/compiler/scala/reflect/reify/NodePrinters.scala
  43. +1 −16 src/compiler/scala/reflect/reify/Taggers.scala
  44. +7 −7 src/compiler/scala/reflect/reify/codegen/Symbols.scala
  45. +1 −5 src/compiler/scala/reflect/reify/codegen/Trees.scala
  46. +1 −1 src/compiler/scala/reflect/runtime/AbstractFile.scala
  47. +0 −25 src/compiler/scala/reflect/runtime/ClassLoaders.scala
  48. +0 −92 src/compiler/scala/reflect/runtime/ConversionUtil.scala
  49. +981 −0 src/compiler/scala/reflect/runtime/JavaMirrors.scala
  50. +0 −697 src/compiler/scala/reflect/runtime/JavaToScala.scala
  51. +33 −0 src/compiler/scala/reflect/runtime/JavaUniverse.scala
  52. +0 −85 src/compiler/scala/reflect/runtime/Mirror.scala
  53. +12 −0 src/compiler/scala/reflect/runtime/ReflectSetup.scala
  54. +2 −12 src/{library/scala/reflect → compiler/scala/reflect/runtime}/ReflectionUtils.scala
  55. +0 −87 src/compiler/scala/reflect/runtime/ScalaToJava.scala
  56. +36 −19 src/compiler/scala/reflect/runtime/SymbolLoaders.scala
  57. +3 −2 src/compiler/scala/reflect/runtime/SymbolTable.scala
  58. +7 −7 src/compiler/scala/reflect/runtime/SynchronizedSymbols.scala
  59. +7 −6 src/compiler/scala/reflect/runtime/SynchronizedTypes.scala
  60. +52 −0 src/compiler/scala/reflect/runtime/TwoWayCache.scala
  61. +0 −43 src/compiler/scala/reflect/runtime/Universe.scala
  62. +10 −2 src/compiler/scala/reflect/runtime/package.scala
  63. +2 −1 src/compiler/scala/tools/cmd/FromString.scala
  64. +0 −64 src/compiler/scala/tools/nsc/ClassLoaders.scala
  65. +54 −23 src/compiler/scala/tools/nsc/Global.scala
  66. +22 −6 src/compiler/scala/tools/nsc/ReflectGlobal.scala
  67. +3 −4 src/compiler/scala/tools/nsc/ReflectMain.scala
  68. +7 −0 src/compiler/scala/tools/nsc/ReflectSetup.scala
  69. +0 −84 src/compiler/scala/tools/nsc/ToolBoxes.scala
  70. +2 −2 src/compiler/scala/tools/nsc/ast/DocComments.scala
  71. +0 −26 src/compiler/scala/tools/nsc/ast/FreeVars.scala
  72. +1 −1 src/compiler/scala/tools/nsc/ast/NodePrinters.scala
  73. +1 −1 src/compiler/scala/tools/nsc/ast/TreeBrowsers.scala
  74. +4 −1 src/compiler/scala/tools/nsc/ast/Trees.scala
  75. +1 −1 src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
  76. +4 −1 src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
  77. +5 −5 src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala
  78. +2 −2 src/compiler/scala/tools/nsc/backend/jvm/GenAndroid.scala
  79. +4 −4 src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala
  80. +2 −1 src/compiler/scala/tools/nsc/doc/Uncompilable.scala
  81. +3 −2 src/compiler/scala/tools/nsc/doc/model/ModelFactory.scala
  82. +1 −1 src/compiler/scala/tools/nsc/interactive/Picklers.scala
  83. +1 −1 src/compiler/scala/tools/nsc/interactive/RangePositions.scala
  84. +2 −2 src/compiler/scala/tools/nsc/interpreter/ILoop.scala
  85. +23 −32 src/compiler/scala/tools/nsc/interpreter/IMain.scala
  86. +2 −1 src/compiler/scala/tools/nsc/interpreter/JLineCompletion.scala
  87. +6 −5 src/compiler/scala/tools/nsc/interpreter/NamedParam.scala
  88. +21 −19 src/compiler/scala/tools/nsc/interpreter/Power.scala
  89. +5 −4 src/compiler/scala/tools/nsc/interpreter/ReplVals.scala
  90. +12 −10 src/compiler/scala/tools/nsc/interpreter/TypeStrings.scala
  91. +1 −2 src/compiler/scala/tools/nsc/io/AbstractFile.scala
  92. +1 −1 src/compiler/scala/tools/nsc/scratchpad/Executor.scala
  93. +7 −7 src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala
  94. +4 −4 src/compiler/scala/tools/nsc/symtab/classfile/ICodeReader.scala
  95. +1 −1 src/compiler/scala/tools/nsc/symtab/clr/TypeParser.scala
  96. +1 −1 src/compiler/scala/tools/nsc/transform/ExplicitOuter.scala
  97. +3 −3 src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala
  98. +1 −1 src/compiler/scala/tools/nsc/transform/Mixin.scala
  99. +11 −4 src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala
  100. +4 −4 src/compiler/scala/tools/nsc/typechecker/Contexts.scala
  101. +1 −1 src/compiler/scala/tools/nsc/typechecker/DestructureTypes.scala
  102. +26 −7 src/compiler/scala/tools/nsc/typechecker/Implicits.scala
  103. +1 −1 src/compiler/scala/tools/nsc/typechecker/Infer.scala
  104. +10 −16 src/compiler/scala/tools/nsc/typechecker/Macros.scala
  105. +21 −14 src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala
  106. +1 −1 src/compiler/scala/tools/nsc/typechecker/Namers.scala
  107. +6 −6 src/compiler/scala/tools/nsc/typechecker/PatternMatching.scala
  108. +1 −1 src/compiler/scala/tools/nsc/typechecker/RefChecks.scala
  109. +2 −2 src/compiler/scala/tools/nsc/typechecker/SuperAccessors.scala
  110. +2 −2 src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala
  111. +1 −1 src/compiler/scala/tools/nsc/typechecker/Taggings.scala
  112. +1 −1 src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala
  113. +7 −7 src/compiler/scala/tools/nsc/typechecker/Typers.scala
  114. +4 −1 src/compiler/scala/tools/nsc/typechecker/Unapplies.scala
  115. +17 −26 src/compiler/scala/tools/nsc/util/Position.scala
  116. +1 −1 src/compiler/scala/tools/nsc/util/ScalaClassLoader.scala
  117. +0 −1 src/compiler/scala/tools/reflect/FastTrack.scala
  118. +7 −5 src/compiler/scala/{reflect/internal → tools/reflect}/FrontEnds.scala
  119. +23 −8 src/compiler/scala/tools/reflect/StdTags.scala
  120. +93 −0 src/compiler/scala/tools/reflect/ToolBox.scala
  121. +122 −99 src/compiler/scala/{reflect/runtime/ToolBoxes.scala → tools/reflect/ToolBoxFactory.scala}
  122. +33 −0 src/compiler/scala/tools/reflect/package.scala
  123. +8 −8 src/continuations/plugin/scala/tools/selectivecps/CPSUtils.scala
  124. +1 −1 src/continuations/plugin/scala/tools/selectivecps/SelectiveCPSTransform.scala
  125. +0 −2 src/library/scala/Predef.scala
  126. +16 −0 src/library/scala/ref/WeakReference.scala
  127. +1 −1 src/library/scala/reflect/ArrayTag.scala
  128. +5 −3 src/library/scala/reflect/ClassTag.scala
  129. +0 −783 src/library/scala/reflect/DummyMirror.scala
  130. +0 −23 src/library/scala/reflect/ErasureTag.scala
  131. +0 −1 src/library/scala/reflect/Manifest.scala
  132. +0 −34 src/library/scala/reflect/TagInterop.scala
  133. +16 −31 src/library/scala/reflect/api/AnnotationInfos.scala
  134. +0 −29 src/library/scala/reflect/api/Attachment.scala
  135. +0 −16 src/library/scala/reflect/api/ClassLoaders.scala
  136. +4 −14 src/library/scala/reflect/api/Constants.scala
  137. +48 −5 src/library/scala/reflect/api/Exprs.scala
  138. +112 −0 src/library/scala/reflect/api/FlagSets.scala
  139. +0 −42 src/library/scala/reflect/api/FreeVars.scala
  140. +5 −1 src/library/scala/reflect/api/FrontEnds.scala
  141. +2 −0 src/library/scala/reflect/api/Importers.scala
  142. +0 −100 src/library/scala/reflect/api/Mirror.scala
  143. +213 −0 src/library/scala/reflect/api/Mirrors.scala
  144. +0 −82 src/library/scala/reflect/api/Modifier.scala
  145. +9 −31 src/library/scala/reflect/api/Names.scala
  146. +28 −34 src/library/scala/reflect/api/Positions.scala
  147. +36 −133 src/library/scala/reflect/api/StandardDefinitions.scala
  148. +12 −19 src/library/scala/reflect/api/StandardNames.scala
  149. +134 −196 src/library/scala/reflect/api/Symbols.scala
  150. +0 −90 src/library/scala/reflect/api/ToolBoxes.scala
  151. +0 −159 src/library/scala/reflect/api/TreeBuildUtil.scala
  152. +12 −15 src/library/scala/reflect/api/TreePrinters.scala
  153. +415 −1,456 src/library/scala/reflect/api/Trees.scala
  154. +98 −264 src/library/scala/reflect/api/Types.scala
  155. +15 −18 src/library/scala/reflect/api/Universe.scala
  156. +12 −0 src/library/scala/reflect/api/package.scala
  157. +44 −0 src/library/scala/reflect/base/AnnotationInfos.scala
  158. +42 −0 src/library/scala/reflect/base/Attachments.scala
  159. +763 −0 src/library/scala/reflect/base/Base.scala
  160. +90 −0 src/library/scala/reflect/base/BuildUtils.scala
  161. +20 −0 src/library/scala/reflect/base/Constants.scala
  162. +17 −0 src/library/scala/reflect/base/FlagSets.scala
  163. +25 −0 src/library/scala/reflect/base/MirrorOf.scala
  164. +12 −0 src/library/scala/reflect/base/Mirrors.scala
  165. +53 −0 src/library/scala/reflect/base/Names.scala
  166. +22 −0 src/library/scala/reflect/base/Positions.scala
  167. +7 −2 src/library/scala/reflect/{api → base}/Scopes.scala
  168. +75 −0 src/library/scala/reflect/base/StandardDefinitions.scala
  169. +29 −0 src/library/scala/reflect/base/StandardNames.scala
  170. +272 −0 src/library/scala/reflect/base/Symbols.scala
  171. +29 −0 src/library/scala/reflect/base/TagInterop.scala
  172. +6 −0 src/library/scala/reflect/base/TreeCreator.scala
  173. +1,459 −0 src/library/scala/reflect/base/Trees.scala
  174. +6 −0 src/library/scala/reflect/base/TypeCreator.scala
  175. +83 −84 src/library/scala/reflect/{api → base}/TypeTags.scala
  176. +432 −0 src/library/scala/reflect/base/Types.scala
  177. +18 −0 src/library/scala/reflect/base/Universe.scala
  178. +29 −0 src/library/scala/reflect/compat.scala
  179. +3 −1 src/library/scala/reflect/makro/Aliases.scala
  180. +3 −2 src/library/scala/reflect/makro/Context.scala
  181. +7 −0 src/library/scala/reflect/makro/Exprs.scala
  182. +3 −1 src/library/scala/reflect/makro/FrontEnds.scala
  183. +2 −2 src/library/scala/reflect/makro/Reifiers.scala
  184. +0 −24 src/library/scala/reflect/makro/Symbols.scala
  185. +59 −0 src/library/scala/reflect/makro/TreeBuilder.scala
  186. +8 −0 src/library/scala/reflect/makro/TypeTags.scala
  187. +117 −0 src/library/scala/reflect/makro/Universe.scala
  188. +6 −7 src/library/scala/reflect/makro/internal/package.scala
  189. +9 −49 src/library/scala/reflect/package.scala
  190. +3 −3 src/partest/scala/tools/partest/CompilerTest.scala
@@ -1 +1 @@
-fbdefcf1a7a2bcd71373f08acd13e84d869ceeaf ?scala-compiler.jar
+388fae20e2e54836af67c752ffed8ff5540b82ea ?scala-compiler.jar
@@ -1 +1 @@
-e19956225d174f0f3bc0a1d014f208b68d399d66 ?scala-library.jar
+6e68e00a30b7328b55de7362f902619066094de7 ?scala-library.jar
@@ -0,0 +1,19 @@
+package scala.reflect
+package api
+
+// [Martin] Moved to compiler because it needs to see runtime.Universe
+// The two will be united in scala-reflect anyway.
+trait JavaUniverse extends Universe with Mirrors with TagInterop { self =>
+
+ type RuntimeClass = java.lang.Class[_]
+
+ override type Mirror >: Null <: JavaMirror
+
+ trait JavaMirror extends MirrorOf[self.type] with RuntimeMirror {
+ val classLoader: ClassLoader
+ override def toString = s"JavaMirror with ${runtime.ReflectionUtils.show(classLoader)}"
+ }
+
+ def runtimeMirror(cl: ClassLoader): Mirror
+}
+
@@ -0,0 +1,38 @@
+package scala.reflect
+package api
+
+import scala.reflect.base.TypeCreator
+import scala.reflect.base.{Universe => BaseUniverse}
+
+// [Martin] Moved to compiler because it needs to see runtime.Universe
+// The two will be united in scala-reflect anyway.
+trait TagInterop { self: JavaUniverse =>
+
+ // [Eugene++] would be great if we could approximate the interop without any mirrors
+ // todo. think how to implement that
+
+ override def concreteTypeTagToManifest[T: ClassTag](mirror0: Any, tag: base.Universe # ConcreteTypeTag[T]): Manifest[T] = {
+ // [Eugene++] implement more sophisticated logic
+ // Martin said it'd be okay to simply copypaste `Implicits.manifestOfType`
+ val mirror = mirror0.asInstanceOf[Mirror]
+ val erasure = mirror.runtimeClass(tag.in(mirror).tpe)
+ Manifest.classType(erasure).asInstanceOf[Manifest[T]]
+ }
+
+ override def manifestToConcreteTypeTag[T](mirror0: Any, manifest: Manifest[T]): base.Universe # ConcreteTypeTag[T] =
+ ConcreteTypeTag(mirror0.asInstanceOf[Mirror], new TypeCreator {
+ def apply[U <: BaseUniverse with Singleton](mirror: MirrorOf[U]): U # Type = {
+ mirror.universe match {
+ case ju: JavaUniverse =>
+ val jm = mirror.asInstanceOf[ju.Mirror]
+ val sym = jm.reflectClass(manifest.erasure).symbol
+ val tpe =
+ if (manifest.typeArguments.isEmpty) sym.asType
+ else ju.appliedType(sym.asTypeConstructor, manifest.typeArguments map (targ => ju.manifestToConcreteTypeTag(jm, targ)) map (_.in(jm).tpe))
+ tpe.asInstanceOf[U # Type]
+ case u =>
+ u.manifestToConcreteTypeTag(mirror.asInstanceOf[u.Mirror], manifest).in(mirror).tpe
+ }
+ }
+ })
+}
@@ -1,7 +1,7 @@
package scala.reflect
-package api
+package internal
-trait RequiredFile {
+trait AbstractFileApi {
def path: String
def canonicalPath: String
}
@@ -46,23 +46,26 @@ trait AnnotationInfos extends api.AnnotationInfos { self: SymbolTable =>
* - or nested classfile annotations
*/
abstract class ClassfileAnnotArg extends Product
+ implicit val ClassfileAnnotArgTag = ClassTag[ClassfileAnnotArg](classOf[ClassfileAnnotArg])
/** Represents a compile-time Constant (`Boolean`, `Byte`, `Short`,
* `Char`, `Int`, `Long`, `Float`, `Double`, `String`, `java.lang.Class` or
* an instance of a Java enumeration value).
*/
case class LiteralAnnotArg(const: Constant)
- extends ClassfileAnnotArg {
+ extends ClassfileAnnotArg with LiteralAnnotArgApi {
override def toString = const.escapedStringValue
}
+ implicit val LiteralAnnotArgTag = ClassTag[LiteralAnnotArg](classOf[LiteralAnnotArg])
object LiteralAnnotArg extends LiteralAnnotArgExtractor
/** Represents an array of classfile annotation arguments */
case class ArrayAnnotArg(args: Array[ClassfileAnnotArg])
- extends ClassfileAnnotArg {
+ extends ClassfileAnnotArg with ArrayAnnotArgApi {
override def toString = args.mkString("[", ", ", "]")
}
+ implicit val ArrayAnnotArgTag = ClassTag[ArrayAnnotArg](classOf[ArrayAnnotArg])
object ArrayAnnotArg extends ArrayAnnotArgExtractor
@@ -105,11 +108,12 @@ trait AnnotationInfos extends api.AnnotationInfos { self: SymbolTable =>
}
/** Represents a nested classfile annotation */
- case class NestedAnnotArg(annInfo: AnnotationInfo) extends ClassfileAnnotArg {
+ case class NestedAnnotArg(annInfo: AnnotationInfo) extends ClassfileAnnotArg with NestedAnnotArgApi {
// The nested annotation should not have any Scala annotation arguments
assert(annInfo.args.isEmpty, annInfo.args)
override def toString = annInfo.toString
}
+ implicit val NestedAnnotArgTag = ClassTag[NestedAnnotArg](classOf[NestedAnnotArg])
object NestedAnnotArg extends NestedAnnotArgExtractor
@@ -191,7 +195,7 @@ trait AnnotationInfos extends api.AnnotationInfos { self: SymbolTable =>
*
* `assocs` stores arguments to classfile annotations as name-value pairs.
*/
- sealed abstract class AnnotationInfo {
+ sealed abstract class AnnotationInfo extends AnnotationInfoApi {
def atp: Type
def args: List[Tree]
def assocs: List[(Name, ClassfileAnnotArg)]
@@ -258,7 +262,7 @@ trait AnnotationInfos extends api.AnnotationInfos { self: SymbolTable =>
/** Change all ident's with Symbol "from" to instead use symbol "to" */
def substIdentSyms(from: Symbol, to: Symbol) =
- AnnotationInfo(atp, args map (_ substTreeSyms (from -> to)), assocs) setPos pos
+ AnnotationInfo(atp, args map (_ substituteSymbols (List(from), List(to))), assocs) setPos pos
def stringArg(index: Int) = constantAtIndex(index) map (_.stringValue)
def intArg(index: Int) = constantAtIndex(index) map (_.intValue)
@@ -283,7 +287,7 @@ trait AnnotationInfos extends api.AnnotationInfos { self: SymbolTable =>
}
}
- lazy val classfileAnnotArgTag: ArrayTag[ClassfileAnnotArg] = arrayTag[ClassfileAnnotArg]
+ implicit val AnnotationInfoTag = ClassTag[AnnotationInfo](classOf[AnnotationInfo])
object UnmappableAnnotation extends CompleteAnnotationInfo(NoType, Nil, Nil)
}
@@ -0,0 +1,69 @@
+package scala.reflect
+package internal
+
+import Flags._
+
+trait BuildUtils extends base.BuildUtils { self: SymbolTable =>
+
+ class BuildImpl extends BuildBase {
+
+ def selectType(owner: Symbol, name: String): TypeSymbol = {
+ val result = owner.info.decl(newTypeName(name))
+ if (result ne NoSymbol) result.asTypeSymbol
+ else MissingRequirementError.notFound("type %s in %s".format(name, owner.fullName))
+ }
+
+ def selectTerm(owner: Symbol, name: String): TermSymbol = {
+ val sym = owner.info.decl(newTermName(name))
+ val result =
+ if (sym.isOverloaded) sym.suchThat(!_.isMethod)
+ else sym
+ if (result ne NoSymbol) result.asTermSymbol
+ else MissingRequirementError.notFound("term %s in %s".format(name, owner.fullName))
+ }
+
+ def selectOverloadedMethod(owner: Symbol, name: String, index: Int): MethodSymbol = {
+ val result = owner.info.decl(newTermName(name)).alternatives(index)
+ if (result ne NoSymbol) result.asMethodSymbol
+ else MissingRequirementError.notFound("overloaded method %s #%d in %s".format(name, index, owner.fullName))
+ }
+
+ def newFreeTerm(name: String, info: Type, value: => Any, flags: Long = 0L, origin: String = null): FreeTermSymbol =
+ newFreeTermSymbol(newTermName(name), info, value, flags, origin)
+
+ def newFreeType(name: String, info: Type, value: => Any, flags: Long = 0L, origin: String = null): FreeTypeSymbol =
+ newFreeTypeSymbol(newTypeName(name), info, value, (if (flags == 0L) PARAM else flags) | DEFERRED, origin)
+
+ def newFreeExistential(name: String, info: Type, value: => Any, flags: Long = 0L, origin: String = null): FreeTypeSymbol =
+ newFreeTypeSymbol(newTypeName(name), info, value, (if (flags == 0L) EXISTENTIAL else flags) | DEFERRED, origin)
+
+ def newNestedSymbol(owner: Symbol, name: Name, pos: Position, flags: Long, isClass: Boolean): Symbol =
+ owner.newNestedSymbol(name, pos, flags, isClass)
+
+ def setAnnotations[S <: Symbol](sym: S, annots: List[AnnotationInfo]): S =
+ sym.setAnnotations(annots)
+
+ def setTypeSignature[S <: Symbol](sym: S, tpe: Type): S =
+ sym.setTypeSignature(tpe)
+
+ def flagsFromBits(bits: Long): FlagSet = bits
+
+ def emptyValDef: ValDef = self.emptyValDef
+
+ def This(sym: Symbol): Tree = self.This(sym)
+
+ def Select(qualifier: Tree, sym: Symbol): Select = self.Select(qualifier, sym)
+
+ def Ident(sym: Symbol): Ident = self.Ident(sym)
+
+ def TypeTree(tp: Type): TypeTree = self.TypeTree(tp)
+
+ def thisPrefix(sym: Symbol): Type = sym.thisPrefix
+
+ def setType[T <: Tree](tree: T, tpe: Type): T = { tree.setType(tpe); tree }
+
+ def setSymbol[T <: Tree](tree: T, sym: Symbol): T = { tree.setSymbol(sym); tree }
+ }
+
+ val build: BuildBase = new BuildImpl
+}
@@ -30,7 +30,7 @@ trait Constants extends api.Constants {
// For supporting java enumerations inside java annotations (see ClassfileParser)
final val EnumTag = 13
- case class Constant(value: Any) extends AbsConstant {
+ case class Constant(value: Any) extends ConstantApi {
val tag: Int = value match {
case null => NullTag
case x: Unit => UnitTag
@@ -235,4 +235,6 @@ trait Constants extends api.Constants {
}
object Constant extends ConstantExtractor
+
+ implicit val ConstantTag = ClassTag[Constant](classOf[Constant])
}
Oops, something went wrong. Retry.

0 comments on commit 6bb5975

Please sign in to comment.