summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2012-04-11 18:08:46 -0700
committerMartin Odersky <odersky@gmail.com>2012-04-11 18:08:46 -0700
commit2c28680ca95c042c5b66de6c4b58c8367599cb44 (patch)
treeeca3ceade0237e2b2ae0116a650cd2630a037422 /src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala
parent699bb5886ec9e73d7c90be1a280e7420d30e6e9b (diff)
downloadscala-2c28680ca95c042c5b66de6c4b58c8367599cb44.tar.gz
scala-2c28680ca95c042c5b66de6c4b58c8367599cb44.tar.bz2
scala-2c28680ca95c042c5b66de6c4b58c8367599cb44.zip
Implementation of SIP 13 - implicit classes
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala')
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala89
1 files changed, 58 insertions, 31 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala b/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala
index 3d8c2ea564..e622ad7d4b 100644
--- a/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala
@@ -155,28 +155,23 @@ trait MethodSynthesis {
/** There are two key methods in here.
*
- * 1) enterGetterSetter is called from Namer with a ValDef which
- * may need accessors. Some setup is performed. In general this
- * creates symbols and enters them into the scope of the owner.
+ * 1) Enter methods such as enterGetterSetterare called
+ * from Namer with a tree which may generate further trees such as accessors or
+ * implicit wrappers. Some setup is performed. In general this creates symbols
+ * and enters them into the scope of the owner.
*
- * 2) finishGetterSetter is called from Typer when a Template is typed.
+ * 2) addDerivedTrees is called from Typer when a Template is typed.
* It completes the job, returning a list of trees with their symbols
- * set to those created in enterGetterSetter. Those trees then become
+ * set to those created in the enter methods. Those trees then become
* part of the typed template.
*/
trait MethodSynth {
self: Namer =>
import NamerErrorGen._
-
- /** TODO - synthesize method.
- */
- def enterImplicitClass(tree: ClassDef) {
- /** e.g.
- val ClassDef(mods, name, tparams, impl) = tree
- val converter = ImplicitClassConverter(tree).createAndEnterSymbol()
- ...
- */
+
+ def enterImplicitWrapper(tree: ClassDef) {
+ ImplicitClassWrapper(tree).createAndEnterSymbol()
}
def enterGetterSetter(tree: ValDef) {
@@ -203,7 +198,8 @@ trait MethodSynthesis {
enterBeans(tree)
}
- def finishGetterSetter(typer: Typer, stat: Tree): List[Tree] = stat match {
+
+ def addDerivedTrees(typer: Typer, stat: Tree): List[Tree] = stat match {
case vd @ ValDef(mods, name, tpt, rhs) if !noFinishGetterSetter(vd) && !vd.symbol.isLazy =>
// If we don't save the annotations, they seem to wander off.
val annotations = stat.symbol.initialize.annotations
@@ -234,35 +230,59 @@ trait MethodSynthesis {
field ::: standardAccessors(vd) ::: beanAccessors(vd)
}
+ /** This trait assembles what's needed for synthesizing derived methods.
+ * Important: Typically, instances of this trait are created TWICE for each derived
+ * symbol; once form Namers in an enter method, and once from Typers in addDerivedTrees.
+ * So it's important that creating an instance of Derived does not have a side effect,
+ * or if it has a side effect, control that it is done only once.
+ */
trait Derived {
- /** The tree from which we are deriving a synthetic member. */
+
+ /** The tree from which we are deriving a synthetic member. Typically, that's
+ * given as an argument of the instance. */
def tree: Tree
+
+ /** The name of the method */
def name: TermName
+
+ /** The flags that are retained from the original symbol */
+
def flagsMask: Long
+
+ /** The flags that the derived symbol has in addition to those retained from
+ * the original symbol*/
def flagsExtra: Long
-
- /** The tree, symbol, and type completer for the synthetic member. */
+
+ /** type completer for the synthetic member.
+ */
def completer(sym: Symbol): Type
+
+ /** The derived symbol. It is assumed that this symbol already exists and has been
+ * entered in the parent scope when derivedSym is called */
def derivedSym: Symbol
+
+ /** The definition tree of the derived symbol. */
def derivedTree: Tree
}
-
+
trait DerivedFromMemberDef extends Derived {
def tree: MemberDef
-
+ def enclClass: Symbol
+
// Final methods to make the rest easier to reason about.
final def mods = tree.mods
final def basisSym = tree.symbol
- final def enclClass = basisSym.enclClass
final def derivedFlags: Long = basisSym.flags & flagsMask | flagsExtra
}
-
+
trait DerivedFromClassDef extends DerivedFromMemberDef {
def tree: ClassDef
+ final def enclClass = basisSym.owner.enclClass
}
trait DerivedFromValDef extends DerivedFromMemberDef {
def tree: ValDef
+ final def enclClass = basisSym.enclClass
/** Which meta-annotation is associated with this kind of entity.
* Presently one of: field, getter, setter, beanGetter, beanSetter, param.
@@ -334,15 +354,22 @@ trait MethodSynthesis {
/** A synthetic method which performs the implicit conversion implied by
* the declaration of an implicit class. Yet to be written.
*/
- case class ImplicitClassConverter(tree: ClassDef) extends DerivedFromClassDef {
- def completer(sym: Symbol): Type = ???
- def derivedSym: Symbol = ???
- def derivedTree: DefDef = ???
- def flagsExtra: Long = ???
- def flagsMask: Long = ???
- def name: TermName = ???
- }
-
+ case class ImplicitClassWrapper(tree: ClassDef) extends DerivedFromClassDef {
+ def completer(sym: Symbol): Type = ??? // not needed
+ def createAndEnterSymbol(): Symbol = enterSyntheticSym(derivedTree)
+ def derivedSym: Symbol = {
+ val result = enclClass.info decl name
+ assert(result != NoSymbol, "not found: "+name+" in "+enclClass+" "+enclClass.info.decls)
+ result
+ }
+ def derivedTree: DefDef = util.trace("derivedTree = ")(
+ factoryMeth(mods & flagsMask | flagsExtra, name, tree, symbolic = false)
+ )
+ def flagsExtra: Long = METHOD | IMPLICIT
+ def flagsMask: Long = AccessFlags
+ def name: TermName = nme.implicitWrapperName(tree.name)
+ }
+
case class Getter(tree: ValDef) extends DerivedGetter {
def name = tree.name
def category = GetterTargetClass