diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2012-12-30 18:34:23 -0800 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2012-12-30 18:34:23 -0800 |
commit | 71082268f0f61be837828533830f37ef7f1f6117 (patch) | |
tree | 030c693028cc4f0ab155b2cc47d1d052d780fb45 /src/reflect | |
parent | 2b3c6606a637746c14785f4de0829d9a9598e899 (diff) | |
parent | 2375e2d878e6c493d9ab3097ef1d13d8641a6209 (diff) | |
download | scala-71082268f0f61be837828533830f37ef7f1f6117.tar.gz scala-71082268f0f61be837828533830f37ef7f1f6117.tar.bz2 scala-71082268f0f61be837828533830f37ef7f1f6117.zip |
Merge pull request #1816 from scalamacros/topic/enclosures
enclosures are now strongly typed and are no longer vals
Diffstat (limited to 'src/reflect')
-rw-r--r-- | src/reflect/scala/reflect/macros/Enclosures.scala | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/src/reflect/scala/reflect/macros/Enclosures.scala b/src/reflect/scala/reflect/macros/Enclosures.scala index c48656b366..1e366ccbc3 100644 --- a/src/reflect/scala/reflect/macros/Enclosures.scala +++ b/src/reflect/scala/reflect/macros/Enclosures.scala @@ -15,7 +15,7 @@ trait Enclosures { /** The tree that undergoes macro expansion. * Can be useful to get an offset or a range position of the entire tree being processed. */ - val macroApplication: Tree + def macroApplication: Tree /** Contexts that represent macros in-flight, including the current one. Very much like a stack trace, but for macros only. * Can be useful for interoperating with other macros and for imposing compiler-friendly limits on macro expansion. @@ -27,7 +27,7 @@ trait Enclosures { * Unlike `openMacros`, this is a val, which means that it gets initialized when the context is created * and always stays the same regardless of whatever happens during macro expansion. */ - val enclosingMacros: List[Context] + def enclosingMacros: List[Context] /** Types along with corresponding trees for which implicit arguments are currently searched. * Can be useful to get information about an application with an implicit parameter that is materialized during current macro expansion. @@ -35,28 +35,56 @@ trait Enclosures { * Unlike `openImplicits`, this is a val, which means that it gets initialized when the context is created * and always stays the same regardless of whatever happens during macro expansion. */ - val enclosingImplicits: List[(Type, Tree)] + def enclosingImplicits: List[(Type, Tree)] /** Tries to guess a position for the enclosing application. * But that is simple, right? Just dereference ``pos'' of ``macroApplication''? Not really. * If we're in a synthetic macro expansion (no positions), we must do our best to infer the position of something that triggerd this expansion. * Surprisingly, quite often we can do this by navigation the ``enclosingMacros'' stack. */ - val enclosingPosition: Position + def enclosingPosition: Position /** Tree that corresponds to the enclosing method, or EmptyTree if not applicable. */ - val enclosingMethod: Tree + @deprecated("Use enclosingDef instead, but be wary of changes in semantics", "2.10.1") + def enclosingMethod: Tree /** Tree that corresponds to the enclosing class, or EmptyTree if not applicable. */ - val enclosingClass: Tree + @deprecated("Use enclosingImpl instead, but be wary of changes in semantics", "2.10.1") + def enclosingClass: Tree + + /** Tree that corresponds to the enclosing DefDef tree. + * Throws `EnclosureException` if there's no such enclosing tree. + */ + def enclosingDef: universe.DefDef + + /** Tree that corresponds to the enclosing Template tree. + * Throws `EnclosureException` if there's no such enclosing tree. + */ + def enclosingTemplate: universe.Template + + /** Tree that corresponds to the enclosing ImplDef tree (i.e. either ClassDef or ModuleDef). + * Throws `EnclosureException` if there's no such enclosing tree. + */ + def enclosingImpl: universe.ImplDef + + /** Tree that corresponds to the enclosing PackageDef tree. + * Throws `EnclosureException` if there's no such enclosing tree. + */ + def enclosingPackage: universe.PackageDef /** Compilation unit that contains this macro application. */ - val enclosingUnit: CompilationUnit + def enclosingUnit: CompilationUnit /** Compilation run that contains this macro application. */ - val enclosingRun: Run + def enclosingRun: Run + + /** Indicates than one of the enclosure methods failed to find a tree + * of required type among enclosing trees. + */ + case class EnclosureException(expected: Class[_], enclosingTrees: List[Tree]) + extends Exception(s"Couldn't find a tree of type $expected among enclosing trees $enclosingTrees") }
\ No newline at end of file |