summaryrefslogtreecommitdiff
path: root/docs/examples/plugintemplate/src/plugintemplate/TemplateInfoTransformComponent.scala
blob: 71069aed6f6d25e745732c2c20bf26c33d716258 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package plugintemplate

import scala.tools.nsc._
import scala.tools.nsc.plugins.PluginComponent
import scala.tools.nsc.transform.InfoTransform
// import scala.tools.nsc.transform.TypingTransformers

/** This class implements a plugin component using tree transformers and
 *  InfoTransformer. An InfoTransformer will be automatically created
 *  and registered in <code>SymbolTable.infoTransformers</code>. If
 *  a <code>Typer</code> is needed during transformation, the component
 *  should mix in <code>TypingTransformers</code>. This provides a local
 *  variable <code>localTyper: Typer</code> that is always updated to
 *  the current context.
 *
 *  @todo Adapt the name of this class to the plugin, and implement it.
 */
class TemplateInfoTransformComponent(val global: Global) extends PluginComponent
                                                         // with TypingTransformers
                                                         with InfoTransform {

  import global._
  import global.definitions._

  val runsAfter = List[String]("refchecks")
  /** The phase name of the compiler plugin
   *  @todo Adapt to specific plugin.
   */
  val phaseName = "plugintemplateinfotransform"

  def transformInfo(sym: Symbol, tp: Type): Type = infoTransformer.mapOver(tp)

  def newTransformer(unit: CompilationUnit) = new TemplateTransformer

  /** The type transformation applied by this component. The trait InfoTransform
   *  will create an instance of InfoTransformer applying this TypeMap. The type
   *  map will be applied when computing a symbol's type in all phases
   *  <em>after</em> "plugintemplateinfotransform".
   *
   *  @todo Implement.
   */
  private val infoTransformer = new TypeMap {
    def apply(tp: Type): Type = tp match {
      case MethodType(pts, rt) =>
          println("methodType (_, _, ..) => "+ rt)
          tp
      case _ => mapOver(tp)
    }
  }

  /** The tree transformer that implements the behavior of this
   *  component. Change the superclass to <code>TypingTransformer</code>
   *  to make a local typechecker <code>localTyper</code> available.
   *
   *  @todo Implement.
   */
  class TemplateTransformer extends /*Typing*/ Transformer {
    /** When using <code>preTransform</code>, each node is
     *  visited before its children.
     */
    def preTransform(tree: Tree): Tree = tree match {
      case ValDef(_, name, _, _) =>
        println("pre-info-transforming valdef "+ name)
        tree
      case _ => tree
    }

    /** When using <code>postTransform</code>, each node is
     *  visited after its children.
     */
    def postTransform(tree: Tree): Tree = tree match {
      case _ => tree
    }

    override def transform(tree: Tree): Tree = {
      postTransform(super.transform(preTransform(tree)))
    }
  }
}