diff options
author | Lukas Rytz <lukas.rytz@epfl.ch> | 2013-01-29 13:53:30 +0100 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@epfl.ch> | 2013-02-03 21:08:34 +0100 |
commit | 7e836f83e2930755f5d6b896a140909eb686289d (patch) | |
tree | c4e8d2b0b5a8452a6fcda8bca5f0451e08778735 /test | |
parent | b74c33eb860622e3630949ee0eeac9c15e8df166 (diff) | |
download | scala-7e836f83e2930755f5d6b896a140909eb686289d.tar.gz scala-7e836f83e2930755f5d6b896a140909eb686289d.tar.bz2 scala-7e836f83e2930755f5d6b896a140909eb686289d.zip |
Analyzer Plugins
AnnotationCheckers are insufficient because they live outside the
compiler cake and it's not possible to pass a Typer into an annotation
checker.
Analyzer plugins hook into important places of the compiler:
- when the namer assigns a type to a symbol (plus a special hook for
accessors)
- before typing a tree, to modify the expected type
- after typing a tree, to modify the type assigned to the tree
Analyzer plugins and annotation checker can be activated only during
selected phases of the compiler.
Refactored the CPS plugin to use an analyzer plugin (since
adaptToAnnotations is now part of analyzer plugins, no longer
annotation checkers).
Diffstat (limited to 'test')
-rw-r--r-- | test/files/run/analyzerPlugins.check | 197 | ||||
-rw-r--r-- | test/files/run/analyzerPlugins.scala | 121 |
2 files changed, 318 insertions, 0 deletions
diff --git a/test/files/run/analyzerPlugins.check b/test/files/run/analyzerPlugins.check new file mode 100644 index 0000000000..8856fef5b3 --- /dev/null +++ b/test/files/run/analyzerPlugins.check @@ -0,0 +1,197 @@ +adaptBoundsToAnnots(List( <: Int), List(type T), List(Int @testAnn)) [2] +annotationsConform(Boolean @testAnn, Boolean) [1] +annotationsConform(Boolean(false), Boolean @testAnn) [1] +annotationsConform(Int @testAnn, ?A) [1] +annotationsConform(Int @testAnn, Any) [1] +annotationsConform(Int @testAnn, Int) [2] +annotationsConform(Int(1) @testAnn, Int) [1] +annotationsConform(Int(1), Int @testAnn) [1] +annotationsConform(Nothing, Int @testAnn) [2] +annotationsConform(String @testAnn, String) [1] +canAdaptAnnotations(Trees$Ident, String) [1] +canAdaptAnnotations(Trees$Select, ?) [1] +canAdaptAnnotations(Trees$Select, Boolean @testAnn) [1] +canAdaptAnnotations(Trees$Select, Boolean) [1] +canAdaptAnnotations(Trees$Select, String @testAnn) [1] +canAdaptAnnotations(Trees$TypeTree, ?) [10] +canAdaptAnnotations(Trees$Typed, ?) [3] +canAdaptAnnotations(Trees$Typed, Any) [1] +canAdaptAnnotations(Trees$Typed, Int) [1] +lub(List(Int @testAnn, Int)) [1] +pluginsPt(?, Trees$Annotated) [7] +pluginsPt(?, Trees$Apply) [8] +pluginsPt(?, Trees$ApplyImplicitView) [2] +pluginsPt(?, Trees$Assign) [7] +pluginsPt(?, Trees$Block) [4] +pluginsPt(?, Trees$ClassDef) [2] +pluginsPt(?, Trees$DefDef) [14] +pluginsPt(?, Trees$Ident) [51] +pluginsPt(?, Trees$If) [2] +pluginsPt(?, Trees$Literal) [16] +pluginsPt(?, Trees$New) [5] +pluginsPt(?, Trees$PackageDef) [1] +pluginsPt(?, Trees$Return) [1] +pluginsPt(?, Trees$Select) [51] +pluginsPt(?, Trees$Super) [2] +pluginsPt(?, Trees$This) [20] +pluginsPt(?, Trees$TypeApply) [3] +pluginsPt(?, Trees$TypeBoundsTree) [2] +pluginsPt(?, Trees$TypeDef) [1] +pluginsPt(?, Trees$TypeTree) [38] +pluginsPt(?, Trees$Typed) [1] +pluginsPt(?, Trees$ValDef) [21] +pluginsPt(Any, Trees$Literal) [2] +pluginsPt(Any, Trees$Typed) [1] +pluginsPt(Array[Any], Trees$ArrayValue) [1] +pluginsPt(Boolean @testAnn, Trees$Literal) [1] +pluginsPt(Boolean @testAnn, Trees$Select) [1] +pluginsPt(Boolean, Trees$Apply) [1] +pluginsPt(Boolean, Trees$Ident) [1] +pluginsPt(Boolean, Trees$Literal) [1] +pluginsPt(Double, Trees$Select) [1] +pluginsPt(Int @testAnn, Trees$Literal) [1] +pluginsPt(Int, Trees$Apply) [1] +pluginsPt(Int, Trees$Ident) [2] +pluginsPt(Int, Trees$If) [1] +pluginsPt(Int, Trees$Literal) [5] +pluginsPt(Int, Trees$Select) [3] +pluginsPt(List, Trees$Apply) [1] +pluginsPt(List[Any], Trees$Select) [1] +pluginsPt(String @testAnn, Trees$Select) [1] +pluginsPt(String, Trees$Apply) [1] +pluginsPt(String, Trees$Block) [2] +pluginsPt(String, Trees$Ident) [4] +pluginsPt(String, Trees$Literal) [1] +pluginsPt(String, Trees$Select) [1] +pluginsPt(String, Trees$Typed) [1] +pluginsPt(Unit, Trees$Assign) [1] +pluginsPt(scala.annotation.Annotation, Trees$Apply) [5] +pluginsTypeSig(<none>, Trees$Template) [2] +pluginsTypeSig(class A, Trees$ClassDef) [1] +pluginsTypeSig(class testAnn, Trees$ClassDef) [1] +pluginsTypeSig(constructor A, Trees$DefDef) [2] +pluginsTypeSig(constructor testAnn, Trees$DefDef) [1] +pluginsTypeSig(method foo, Trees$DefDef) [1] +pluginsTypeSig(method method, Trees$DefDef) [1] +pluginsTypeSig(method nested, Trees$DefDef) [1] +pluginsTypeSig(type T, Trees$TypeDef) [2] +pluginsTypeSig(value annotField, Trees$ValDef) [2] +pluginsTypeSig(value f, Trees$ValDef) [1] +pluginsTypeSig(value inferField, Trees$ValDef) [2] +pluginsTypeSig(value lub1, Trees$ValDef) [2] +pluginsTypeSig(value lub2, Trees$ValDef) [2] +pluginsTypeSig(value param, Trees$ValDef) [2] +pluginsTypeSig(value str, Trees$ValDef) [1] +pluginsTypeSig(value x, Trees$ValDef) [4] +pluginsTypeSig(value y, Trees$ValDef) [4] +pluginsTypeSig(variable count, Trees$ValDef) [3] +pluginsTypeSigAccessor(value annotField) [1] +pluginsTypeSigAccessor(value inferField) [1] +pluginsTypeSigAccessor(value lub1) [1] +pluginsTypeSigAccessor(value lub2) [1] +pluginsTypeSigAccessor(value x) [1] +pluginsTypeSigAccessor(value y) [1] +pluginsTypeSigAccessor(variable count) [2] +pluginsTyped( <: Int, Trees$TypeBoundsTree) [2] +pluginsTyped(()Object, Trees$Select) [1] +pluginsTyped(()String, Trees$Ident) [1] +pluginsTyped(()String, Trees$TypeApply) [1] +pluginsTyped(()scala.annotation.Annotation, Trees$Select) [1] +pluginsTyped(()testAnn, Trees$Select) [10] +pluginsTyped((str: String)A <and> (param: Double)A, Trees$Select) [1] +pluginsTyped((x$1: Any)Boolean <and> (x: Double)Boolean <and> (x: Float)Boolean <and> (x: Long)Boolean <and> (x: Int)Boolean <and> (x: Char)Boolean <and> (x: Short)Boolean <and> (x: Byte)Boolean, Trees$Select) [1] +pluginsTyped((x$1: Int)Unit, Trees$Select) [1] +pluginsTyped((x: Double)Double <and> (x: Float)Float <and> (x: Long)Long <and> (x: Int)Int <and> (x: Char)Int <and> (x: Short)Int <and> (x: Byte)Int <and> (x: String)String, Trees$Select) [1] +pluginsTyped((x: String)scala.collection.immutable.StringOps, Trees$Select) [2] +pluginsTyped((xs: Array[Any])scala.collection.mutable.WrappedArray[Any], Trees$TypeApply) [1] +pluginsTyped(<empty>.type, Trees$Ident) [1] +pluginsTyped(<error>, Trees$Select) [1] +pluginsTyped(<notype>, Trees$ClassDef) [2] +pluginsTyped(<notype>, Trees$DefDef) [14] +pluginsTyped(<notype>, Trees$PackageDef) [1] +pluginsTyped(<notype>, Trees$TypeDef) [1] +pluginsTyped(<notype>, Trees$ValDef) [21] +pluginsTyped(<root>, Trees$Ident) [1] +pluginsTyped(=> Boolean @testAnn, Trees$Select) [1] +pluginsTyped(=> Double, Trees$Select) [4] +pluginsTyped(=> Int, Trees$Select) [5] +pluginsTyped(=> Int, Trees$TypeApply) [1] +pluginsTyped(=> String @testAnn, Trees$Select) [1] +pluginsTyped(A, Trees$Apply) [1] +pluginsTyped(A, Trees$Ident) [2] +pluginsTyped(A, Trees$This) [8] +pluginsTyped(A, Trees$TypeTree) [4] +pluginsTyped(A.super.type, Trees$Super) [1] +pluginsTyped(A.this.type, Trees$This) [11] +pluginsTyped(Any, Trees$TypeTree) [1] +pluginsTyped(AnyRef, Trees$Select) [4] +pluginsTyped(Array[Any], Trees$ArrayValue) [1] +pluginsTyped(Boolean @testAnn, Trees$Select) [1] +pluginsTyped(Boolean @testAnn, Trees$TypeTree) [4] +pluginsTyped(Boolean(false), Trees$Literal) [2] +pluginsTyped(Boolean, Trees$Apply) [1] +pluginsTyped(Boolean, Trees$Select) [4] +pluginsTyped(Char('c'), Trees$Literal) [2] +pluginsTyped(Double, Trees$Select) [6] +pluginsTyped(Int @testAnn, Trees$TypeTree) [2] +pluginsTyped(Int @testAnn, Trees$Typed) [2] +pluginsTyped(Int(0), Trees$Literal) [3] +pluginsTyped(Int(1) @testAnn, Trees$Typed) [1] +pluginsTyped(Int(1), Trees$Literal) [8] +pluginsTyped(Int(2), Trees$Literal) [1] +pluginsTyped(Int, Trees$Apply) [1] +pluginsTyped(Int, Trees$Ident) [2] +pluginsTyped(Int, Trees$If) [2] +pluginsTyped(Int, Trees$Select) [15] +pluginsTyped(Int, Trees$TypeTree) [13] +pluginsTyped(List, Trees$Apply) [1] +pluginsTyped(List, Trees$Select) [1] +pluginsTyped(List[Any], Trees$Apply) [1] +pluginsTyped(List[Any], Trees$Select) [1] +pluginsTyped(List[Any], Trees$TypeTree) [3] +pluginsTyped(Nothing, Trees$Return) [1] +pluginsTyped(Nothing, Trees$Select) [2] +pluginsTyped(Object, Trees$Apply) [1] +pluginsTyped(String @testAnn, Trees$Ident) [1] +pluginsTyped(String @testAnn, Trees$Select) [1] +pluginsTyped(String @testAnn, Trees$TypeTree) [4] +pluginsTyped(String(""), Trees$Literal) [2] +pluginsTyped(String("huhu"), Trees$Literal) [1] +pluginsTyped(String("str") @testAnn, Trees$Typed) [1] +pluginsTyped(String("str"), Trees$Literal) [1] +pluginsTyped(String("str"), Trees$Typed) [1] +pluginsTyped(String("two"), Trees$Literal) [2] +pluginsTyped(String, Trees$Apply) [2] +pluginsTyped(String, Trees$Block) [2] +pluginsTyped(String, Trees$Ident) [1] +pluginsTyped(String, Trees$Select) [9] +pluginsTyped(String, Trees$TypeTree) [7] +pluginsTyped(Unit, Trees$Apply) [2] +pluginsTyped(Unit, Trees$Assign) [8] +pluginsTyped(Unit, Trees$Block) [4] +pluginsTyped(Unit, Trees$If) [1] +pluginsTyped(Unit, Trees$Literal) [5] +pluginsTyped(Unit, Trees$TypeTree) [1] +pluginsTyped([A](xs: A*)List[A], Trees$Select) [1] +pluginsTyped([T <: Int]=> Int, Trees$Select) [1] +pluginsTyped([T0 >: ? <: ?]()T0, Trees$Select) [1] +pluginsTyped([T](xs: Array[T])scala.collection.mutable.WrappedArray[T], Trees$Select) [1] +pluginsTyped(annotation.type, Trees$Select) [4] +pluginsTyped(math.type, Trees$Select) [9] +pluginsTyped(scala.annotation.Annotation, Trees$Apply) [1] +pluginsTyped(scala.annotation.TypeConstraint, Trees$Select) [4] +pluginsTyped(scala.annotation.TypeConstraint, Trees$TypeTree) [2] +pluginsTyped(scala.collection.immutable.List.type, Trees$Select) [2] +pluginsTyped(scala.collection.immutable.StringOps, Trees$ApplyImplicitView) [2] +pluginsTyped(scala.collection.mutable.WrappedArray[Any], Trees$Apply) [1] +pluginsTyped(scala.type, Trees$Ident) [1] +pluginsTyped(scala.type, Trees$Select) [1] +pluginsTyped(str.type, Trees$Ident) [3] +pluginsTyped(testAnn, Trees$Apply) [5] +pluginsTyped(testAnn, Trees$Ident) [5] +pluginsTyped(testAnn, Trees$New) [5] +pluginsTyped(testAnn, Trees$This) [1] +pluginsTyped(testAnn, Trees$TypeTree) [2] +pluginsTyped(testAnn.super.type, Trees$Super) [1] +pluginsTyped(type, Trees$Select) [1] +pluginsTypedReturn(return f, String) [1] diff --git a/test/files/run/analyzerPlugins.scala b/test/files/run/analyzerPlugins.scala new file mode 100644 index 0000000000..daef83fa30 --- /dev/null +++ b/test/files/run/analyzerPlugins.scala @@ -0,0 +1,121 @@ +import scala.tools.partest._ +import scala.tools.nsc._ + +object Test extends DirectTest { + + override def extraSettings: String = "-usejavacp" + + def code = """ + class testAnn extends annotation.TypeConstraint + + class A(param: Double) extends { val x: Int = 1; val y = "two"; type T = A } with AnyRef { + val inferField = ("str": @testAnn) + val annotField: Boolean @testAnn = false + + val lub1 = List('c', (1: Int @testAnn), "") + val lub2 = if (annotField) (1: @testAnn) else 2 + + def foo[T <: Int] = 0 + foo[Int @testAnn] + + var count = 0 + + math.random // some statement + + def method: String = { + math.random + val f = inferField + + def nested(): String = { + if(count == 1) + return f + "huhu" + } + nested() + } + + def this(str: String) { + this(str.toDouble) + math.random + count += 1 + } + } + """.trim + + + def show() { + val global = newCompiler() + import global._ + import analyzer._ + + val output = collection.mutable.ListBuffer[String]() + + object annotChecker extends AnnotationChecker { + def hasTestAnn(tps: Type*) = { + tps exists (_.annotations.map(_.toString) contains "testAnn") + } + + def annotationsConform(tpe1: Type, tpe2: Type): Boolean = { + if (hasTestAnn(tpe1, tpe2)) + output += s"annotationsConform($tpe1, $tpe2)" + true + } + + override def annotationsLub(tp: Type, ts: List[Type]): Type = { + if (hasTestAnn(ts: _*)) + output += s"lub($ts)" + tp + } + + override def adaptBoundsToAnnotations(bounds: List[TypeBounds], tparams: List[Symbol], targs: List[Type]): List[TypeBounds] = { + if (hasTestAnn(targs: _*)) + output += s"adaptBoundsToAnnots($bounds, $tparams, $targs)" + bounds + } + } + + object analyzerPlugin extends AnalyzerPlugin { + def treeClass(t: Tree) = t.getClass.toString.split('.').last + + override def pluginsPt(pt: Type, typer: Typer, tree: Tree, mode: Int): Type = { + output += s"pluginsPt($pt, ${treeClass(tree)})" + pt + } + + override def pluginsTyped(tpe: Type, typer: Typer, tree: Tree, mode: Int, pt: Type): Type = { + output += s"pluginsTyped($tpe, ${treeClass(tree)})" + tpe + } + + override def pluginsTypeSig(tpe: Type, typer: Typer, defTree: Tree, pt: Type): Type = { + output += s"pluginsTypeSig(${defTree.symbol}, ${treeClass(defTree)})" + tpe + } + + override def pluginsTypeSigAccessor(tpe: Type, typer: Typer, tree: ValDef, sym: Symbol): Type = { + output += s"pluginsTypeSigAccessor(${tree.symbol})" + tpe + } + + + override def canAdaptAnnotations(tree: Tree, typer: Typer, mode: Int, pt: Type): Boolean = { + output += s"canAdaptAnnotations(${treeClass(tree)}, $pt)" + false + } + + override def pluginsTypedReturn(tpe: Type, typer: Typer, tree: Return, pt: Type): Type = { + output += s"pluginsTypedReturn($tree, $pt)" + tpe + } + + } + + addAnnotationChecker(annotChecker) + addAnalyzerPlugin(analyzerPlugin) + compileString(global)(code) + + val res = output.groupBy(identity).mapValues(_.size).map { case (k,v) => s"$k [$v]" }.toList.sorted + println(res.mkString("\n")) + } + +} |