summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/reflect
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2014-01-29 18:15:31 +0300
committerEugene Burmako <xeno.by@gmail.com>2014-02-14 14:19:44 +0100
commit2c05f0139758613fbe26a5c03d60a9da29f2f5e5 (patch)
tree7c64843ff5137539172fee6106282b8b7bda94e9 /src/compiler/scala/tools/reflect
parent0268e03cb461b0c7e8ae2082894988395fc0994a (diff)
downloadscala-2c05f0139758613fbe26a5c03d60a9da29f2f5e5.tar.gz
scala-2c05f0139758613fbe26a5c03d60a9da29f2f5e5.tar.bz2
scala-2c05f0139758613fbe26a5c03d60a9da29f2f5e5.zip
SI-6814 adds typechecker modes to c.typecheck
As per multiple user requests, this commit introduces a shortcut to typecheck trees under multiple different modes: terms (EXPRmode, was exposed in Scala 2.10) and types (TYPEmode). Looking into the rest of a dozen of internal typechecker modes, to the best of my knowledge, I can’t find other modes that we could expose. FUNmode is useful, but very situational. PATTERNmode is useful, but also situational, because we don’t expand macros inside patterns except for whitebox extractor macros. The rest (e.g. POLYmode or TAPPmode) are too low-level.
Diffstat (limited to 'src/compiler/scala/tools/reflect')
-rw-r--r--src/compiler/scala/tools/reflect/ToolBox.scala28
-rw-r--r--src/compiler/scala/tools/reflect/ToolBoxFactory.scala13
2 files changed, 31 insertions, 10 deletions
diff --git a/src/compiler/scala/tools/reflect/ToolBox.scala b/src/compiler/scala/tools/reflect/ToolBox.scala
index 4a3db09909..f47db49718 100644
--- a/src/compiler/scala/tools/reflect/ToolBox.scala
+++ b/src/compiler/scala/tools/reflect/ToolBox.scala
@@ -21,19 +21,35 @@ trait ToolBox[U <: scala.reflect.api.Universe] {
*/
def frontEnd: FrontEnd
+ /** Represents mode of operations of the typechecker underlying `c.typecheck` calls.
+ * Is necessary since the shape of the typechecked tree alone is not enough to guess how it should be typechecked.
+ * Can be EXPRmode (typecheck as a term) or TYPEmode (typecheck as a type).
+ */
+ type TypecheckMode
+
+ /** Indicates that an argument to `c.typecheck` should be typechecked as a term.
+ * This is the default typechecking mode in Scala 2.11 and the only one supported in Scala 2.10.
+ */
+ val TERMmode: TypecheckMode
+
+ /** Indicates that an argument to `c.typecheck` should be typechecked as a type.
+ */
+ val TYPEmode: TypecheckMode
+
/** @see `Typers.typecheck`
*/
@deprecated("Use `tb.typecheck` instead", "2.11.0")
def typeCheck(tree: u.Tree, pt: u.Type = u.WildcardType, silent: Boolean = false, withImplicitViewsDisabled: Boolean = false, withMacrosDisabled: Boolean = false): u.Tree =
- typecheck(tree, pt, silent, withImplicitViewsDisabled, withMacrosDisabled)
+ typecheck(tree, TERMmode, pt, silent, withImplicitViewsDisabled, withMacrosDisabled)
- /** Typechecks a tree using this ToolBox.
+ /** Typechecks a tree against the expected type `pt`
+ * under typechecking mode specified in `mode` with [[EXPRmode]] being default.
* This populates symbols and types of the tree and possibly transforms it to reflect certain desugarings.
*
* If the tree has unresolved type variables (represented as instances of `FreeTypeSymbol` symbols),
* then they all have to be resolved first using `Tree.substituteTypes`, or an error occurs.
*
- * If `silent` is false, `TypeError` will be thrown in case of a typecheck error.
+ * If `silent` is false, `ToolBoxError` will be thrown in case of a typecheck error.
* If `silent` is true, the typecheck is silent and will return `EmptyTree` if an error occurs.
* Such errors don't vanish and can be inspected by turning on -Ydebug.
*
@@ -41,7 +57,7 @@ trait ToolBox[U <: scala.reflect.api.Universe] {
* `withImplicitViewsDisabled` recursively prohibits implicit views (though, implicit vals will still be looked up and filled in), default value is false
* `withMacrosDisabled` recursively prohibits macro expansions and macro-based implicits, default value is false
*/
- def typecheck(tree: u.Tree, pt: u.Type = u.WildcardType, silent: Boolean = false, withImplicitViewsDisabled: Boolean = false, withMacrosDisabled: Boolean = false): u.Tree
+ def typecheck(tree: u.Tree, mode: TypecheckMode = TERMmode, pt: u.Type = u.WildcardType, silent: Boolean = false, withImplicitViewsDisabled: Boolean = false, withMacrosDisabled: Boolean = false): u.Tree
/** Infers an implicit value of the expected type `pt` in top-level context.
* Optional `pos` parameter provides a position that will be associated with the implicit search.
@@ -50,7 +66,7 @@ trait ToolBox[U <: scala.reflect.api.Universe] {
* this API won't take into account the lexical context of the callsite, because
* currently it's impossible to reify it.
*
- * If `silent` is false, `TypeError` will be thrown in case of an inference error.
+ * If `silent` is false, `ToolBoxError` will be thrown in case of an inference error.
* If `silent` is true, the typecheck is silent and will return `EmptyTree` if an error occurs.
* Such errors don't vanish and can be inspected by turning on -Xlog-implicits.
* Unlike in `typecheck`, `silent` is true by default.
@@ -64,7 +80,7 @@ trait ToolBox[U <: scala.reflect.api.Universe] {
* this API won't take into account the lexical context of the callsite, because
* currently it's impossible to reify it.
*
- * If `silent` is false, `TypeError` will be thrown in case of an inference error.
+ * If `silent` is false, `ToolBoxError` will be thrown in case of an inference error.
* If `silent` is true, the typecheck is silent and will return `EmptyTree` if an error occurs.
* Such errors don't vanish and can be inspected by turning on -Xlog-implicits.
* Unlike in `typecheck`, `silent` is true by default.
diff --git a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala
index 7bae3203c2..b43b4653eb 100644
--- a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala
+++ b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala
@@ -171,11 +171,11 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
unwrapped
}
- def typecheck(expr: Tree, pt: Type, silent: Boolean, withImplicitViewsDisabled: Boolean, withMacrosDisabled: Boolean): Tree =
+ def typecheck(expr: Tree, pt: Type, mode: scala.reflect.internal.Mode, silent: Boolean, withImplicitViewsDisabled: Boolean, withMacrosDisabled: Boolean): Tree =
transformDuringTyper(expr, withImplicitViewsDisabled = withImplicitViewsDisabled, withMacrosDisabled = withMacrosDisabled)(
(currentTyper, expr) => {
trace("typing (implicit views = %s, macros = %s): ".format(!withImplicitViewsDisabled, !withMacrosDisabled))(showAttributed(expr, true, true, settings.Yshowsymkinds.value))
- currentTyper.silent(_.typed(expr, pt), reportAmbiguousErrors = false) match {
+ currentTyper.silent(_.typed(expr, mode, pt), reportAmbiguousErrors = false) match {
case analyzer.SilentResultValue(result) =>
trace("success: ")(showAttributed(result, true, true, settings.Yshowsymkinds.value))
result
@@ -361,7 +361,12 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
}
}
- def typecheck(tree: u.Tree, expectedType: u.Type, silent: Boolean = false, withImplicitViewsDisabled: Boolean = false, withMacrosDisabled: Boolean = false): u.Tree = withCompilerApi { compilerApi =>
+ type TypecheckMode = scala.reflect.internal.Mode
+ val TypecheckMode = scala.reflect.internal.Mode
+ val TERMmode = TypecheckMode.EXPRmode
+ val TYPEmode = TypecheckMode.TYPEmode | TypecheckMode.FUNmode
+
+ def typecheck(tree: u.Tree, mode: TypecheckMode = TERMmode, expectedType: u.Type, silent: Boolean = false, withImplicitViewsDisabled: Boolean = false, withMacrosDisabled: Boolean = false): u.Tree = withCompilerApi { compilerApi =>
import compilerApi._
if (compiler.settings.verbose) println("importing "+tree+", expectedType = "+expectedType)
@@ -369,7 +374,7 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf =>
val cexpectedType: compiler.Type = importer.importType(expectedType)
if (compiler.settings.verbose) println("typing "+ctree+", expectedType = "+expectedType)
- val ttree: compiler.Tree = compiler.typecheck(ctree, cexpectedType, silent = silent, withImplicitViewsDisabled = withImplicitViewsDisabled, withMacrosDisabled = withMacrosDisabled)
+ val ttree: compiler.Tree = compiler.typecheck(ctree, cexpectedType, mode, silent = silent, withImplicitViewsDisabled = withImplicitViewsDisabled, withMacrosDisabled = withMacrosDisabled)
val uttree = exporter.importTree(ttree)
uttree
}