diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2012-09-26 09:09:34 +0200 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2012-09-27 15:53:15 +0200 |
commit | e74c432fdbce2b3eaaddcba8c733cf7ccc0ee2ed (patch) | |
tree | 60d6f731754304ea8a23ea49d076dd7128449df2 | |
parent | 94e280dee6655f9d4242ab5fd1f46c97975c7182 (diff) | |
download | scala-e74c432fdbce2b3eaaddcba8c733cf7ccc0ee2ed.tar.gz scala-e74c432fdbce2b3eaaddcba8c733cf7ccc0ee2ed.tar.bz2 scala-e74c432fdbce2b3eaaddcba8c733cf7ccc0ee2ed.zip |
simplifies Context.Run and Context.CompilationUnit
By turning them from abstract types into full-fledged traits
implemented by our internal Run and CompilationUnit.
7 files changed, 40 insertions, 39 deletions
diff --git a/src/compiler/scala/reflect/macros/runtime/Infrastructure.scala b/src/compiler/scala/reflect/macros/runtime/Infrastructure.scala index 87ac97ff27..3a1df5f7ce 100644 --- a/src/compiler/scala/reflect/macros/runtime/Infrastructure.scala +++ b/src/compiler/scala/reflect/macros/runtime/Infrastructure.scala @@ -10,15 +10,4 @@ trait Infrastructure { val currentClassPath: List[java.net.URL] = universe.classPath.asURLs - type Run = universe.Run - - object Run extends RunExtractor { - def unapply(run: Run): Option[(CompilationUnit, List[CompilationUnit])] = Some((run.currentUnit, run.units.toList)) - } - - type CompilationUnit = universe.CompilationUnit - - object CompilationUnit extends CompilationUnitExtractor { - def unapply(compilationUnit: CompilationUnit): Option[(java.io.File, Array[Char], Tree)] = Some((compilationUnit.source.file.file, compilationUnit.source.content, compilationUnit.body)) - } } diff --git a/src/compiler/scala/tools/nsc/CompilationUnits.scala b/src/compiler/scala/tools/nsc/CompilationUnits.scala index 6d3fd2e09e..5a2d5ef165 100644 --- a/src/compiler/scala/tools/nsc/CompilationUnits.scala +++ b/src/compiler/scala/tools/nsc/CompilationUnits.scala @@ -23,7 +23,7 @@ trait CompilationUnits { self: Global => /** One unit of compilation that has been submitted to the compiler. * It typically corresponds to a single file of source code. It includes * error-reporting hooks. */ - class CompilationUnit(val source: SourceFile) { + class CompilationUnit(val source: SourceFile) extends CompilationUnitContextApi { /** the fresh name creator */ var fresh: FreshNameCreator = new FreshNameCreator.Default diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 0fbd930ad7..708824ede1 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -1204,7 +1204,7 @@ class Global(var currentSettings: Settings, var reporter: Reporter) /** A Run is a single execution of the compiler on a sets of units */ - class Run { + class Run extends RunContextApi { /** Have been running into too many init order issues with Run * during erroneous conditions. Moved all these vals up to the * top of the file so at least they're not trivially null. diff --git a/src/reflect/scala/reflect/macros/Aliases.scala b/src/reflect/scala/reflect/macros/Aliases.scala index 0073d57725..754335d50d 100644 --- a/src/reflect/scala/reflect/macros/Aliases.scala +++ b/src/reflect/scala/reflect/macros/Aliases.scala @@ -13,6 +13,8 @@ trait Aliases { type Position = universe.Position type Scope = universe.Scope type Modifiers = universe.Modifiers + type Run = universe.Run + type CompilationUnit = universe.CompilationUnit type Expr[+T] = universe.Expr[T] val Expr = universe.Expr diff --git a/src/reflect/scala/reflect/macros/Infrastructure.scala b/src/reflect/scala/reflect/macros/Infrastructure.scala index f918770a3a..2b0af6857b 100644 --- a/src/reflect/scala/reflect/macros/Infrastructure.scala +++ b/src/reflect/scala/reflect/macros/Infrastructure.scala @@ -12,28 +12,4 @@ trait Infrastructure { */ val currentClassPath: List[java.net.URL] - /** As seen by macro API, compilation run is an opaque type that can be deconstructed into: - * 1) Current compilation unit - * 2) List of all compilation units that comprise the run - */ - type Run - - val Run: RunExtractor - - abstract class RunExtractor { - def unapply(run: Run): Option[(CompilationUnit, List[CompilationUnit])] - } - - /** As seen by macro API, compilation unit is an opaque type that can be deconstructed into: - * 1) File that corresponds to the unit (if not applicable, null) - * 2) Content of the file (if not applicable, empty array) - * 3) Body, i.e. the AST that represents the compilation unit - */ - type CompilationUnit - - val CompilationUnit: CompilationUnitExtractor - - abstract class CompilationUnitExtractor { - def unapply(compilationUnit: CompilationUnit): Option[(java.io.File, Array[Char], Tree)] - } } diff --git a/src/reflect/scala/reflect/macros/Universe.scala b/src/reflect/scala/reflect/macros/Universe.scala index 233033f35b..97d0a8d98a 100644 --- a/src/reflect/scala/reflect/macros/Universe.scala +++ b/src/reflect/scala/reflect/macros/Universe.scala @@ -122,4 +122,39 @@ abstract class Universe extends scala.reflect.api.Universe { /** Convert type of a captured variable to *Ref type. */ def capturedVariableType(vble: Symbol): Type + + type Run <: RunContextApi + + /** Compilation run uniquely identifies current invocation of the compiler + * (e.g. can be used to implement per-run caches for macros) and provides access to units of work + * of the invocation (currently processed unit of work and the list of all units). + */ + trait RunContextApi { + /** Currently processed unit of work (a real or a virtual file). */ + def currentUnit: CompilationUnit + + /** All units of work comprising this compilation run. */ + def units: Iterator[CompilationUnit] + } + + type CompilationUnit <: CompilationUnitContextApi + + /** Compilation unit describes a unit of work of the compilation run. + * It provides such information as file name, textual representation of the unit and the underlying AST. + */ + trait CompilationUnitContextApi { + /** Source file corresponding to this compilation unit. + * + * Exposes information about the file as a part of a real or virtual file system + * along with the contents of that file. + * + * The return type is [[scala.reflect.io.AbstractFile]], which belongs to an experimental part of Scala reflection. + * It should not be used unless you know what you are doing. In subsequent releases, this API will be refined + * and exposed as a part of scala.reflect.api. + */ + def source: scala.reflect.internal.util.SourceFile + + /** The AST that corresponds to this compilation unit. */ + def body: Tree + } }
\ No newline at end of file diff --git a/test/files/run/macro-openmacros/Impls_Macros_1.scala b/test/files/run/macro-openmacros/Impls_Macros_1.scala index 38d46c5185..b863ac048b 100644 --- a/test/files/run/macro-openmacros/Impls_Macros_1.scala +++ b/test/files/run/macro-openmacros/Impls_Macros_1.scala @@ -4,8 +4,7 @@ object Macros { def impl(c: Context): c.Expr[Unit] = { // we're macros, so we can reflect against our source path // so we don't need any partests to clean up after us! - val c.CompilationUnit(file, _, _) = c.enclosingUnit - val dir = file.getCanonicalFile.getParentFile + val dir = c.enclosingUnit.source.file.file.getCanonicalFile.getParentFile def normalizePaths(s: String) = { val base = (dir.getCanonicalPath + java.io.File.separator).replace('\\', '/') var regex = """\Q%s\E""" format base |