diff options
author | Miguel Garcia <miguelalfredo.garcia@epfl.ch> | 2013-05-23 15:26:12 +0200 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2013-05-26 19:13:58 -0700 |
commit | 63325fe072aeac54e3651af9184dd8499c617d6d (patch) | |
tree | 69cbe4d1a0a5f31e5da2ec458b14d8328901d4ad | |
parent | 4837fa3d84436908cb98b29ea143b14c4a2cffeb (diff) | |
download | scala-63325fe072aeac54e3651af9184dd8499c617d6d.tar.gz scala-63325fe072aeac54e3651af9184dd8499c617d6d.tar.bz2 scala-63325fe072aeac54e3651af9184dd8499c617d6d.zip |
readability for intoConstructors transformer
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/Constructors.scala | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/Constructors.scala b/src/compiler/scala/tools/nsc/transform/Constructors.scala index bd8385f7bc..ff6a964396 100644 --- a/src/compiler/scala/tools/nsc/transform/Constructors.scala +++ b/src/compiler/scala/tools/nsc/transform/Constructors.scala @@ -371,6 +371,8 @@ abstract class Constructors extends Transform with ast.TreeDSL { val specializedFlag: Symbol = clazz.info.decl(nme.SPECIALIZED_INSTANCE) val shouldGuard = (specializedFlag != NoSymbol) && !clazz.hasFlag(SPECIALIZED) + val isDelayedInitSubclass = (clazz isSubClass DelayedInitClass) + case class ConstrInfo( constr: DefDef, // The primary constructor constrParams: List[Symbol], // ... and its parameters @@ -407,37 +409,63 @@ abstract class Constructors extends Transform with ast.TreeDSL { var usesSpecializedField: Boolean = false - // A transformer for expressions that go into the constructor - val intoConstructorTransformer = new Transformer { - def isParamRef(sym: Symbol) = - sym.isParamAccessor && - sym.owner == clazz && - !(clazz isSubClass DelayedInitClass) && + /* + * A transformer for expressions that go into the constructor. + */ + private class IntoCtorTransformer extends Transformer { + + private def isParamRef(sym: Symbol) = { sym.isParamAccessor && sym.owner == clazz } + + /* + * Terminology: a stationary location is never written after being read. + */ + private def isStationaryParamRef(sym: Symbol) = { + isParamRef(sym) && !(sym.isGetter && sym.accessed.isVariable) && !sym.isSetter + } + private def possiblySpecialized(s: Symbol) = specializeTypes.specializedTypeVars(s).nonEmpty + + /* + * whether `sym` denotes a param-accessor (ie a field) that fulfills all of: + * (a) has stationary value, ie the same value provided via the corresponding ctor-arg; and + * (b) isn't subject to specialization. We might be processing statements for: + * (b.1) the constructur in the generic (super-)class; or + * (b.2) the constructor in the specialized (sub-)class. + * (c) isn't part of a DelayedInit subclass. + */ + private def canBeSupplanted(sym: Symbol) = { !isDelayedInitSubclass && isStationaryParamRef(sym) && !possiblySpecialized(sym) } + override def transform(tree: Tree): Tree = tree match { + case Apply(Select(This(_), _), List()) => // references to parameter accessor methods of own class become references to parameters // outer accessors become references to $outer parameter - if (isParamRef(tree.symbol) && !possiblySpecialized(tree.symbol)) + if (canBeSupplanted(tree.symbol)) gen.mkAttributedIdent(parameter(tree.symbol.accessed)) setPos tree.pos else if (tree.symbol.outerSource == clazz && !clazz.isImplClass) gen.mkAttributedIdent(parameterNamed(nme.OUTER)) setPos tree.pos else super.transform(tree) - case Select(This(_), _) if (isParamRef(tree.symbol) && !possiblySpecialized(tree.symbol)) => + + case Select(This(_), _) if (canBeSupplanted(tree.symbol)) => // references to parameter accessor field of own class become references to parameters gen.mkAttributedIdent(parameter(tree.symbol)) setPos tree.pos + case Select(_, _) => - if (specializeTypes.specializedTypeVars(tree.symbol).nonEmpty) + if (possiblySpecialized(tree.symbol)) usesSpecializedField = true super.transform(tree) + case _ => super.transform(tree) } + } + private val intoConstructorTransformer = new IntoCtorTransformer + // Move tree into constructor, take care of changing owner from `oldowner` to constructor symbol def intoConstructor(oldowner: Symbol, tree: Tree) = intoConstructorTransformer transform tree.changeOwner(oldowner -> constr.symbol) @@ -534,8 +562,6 @@ abstract class Constructors extends Transform with ast.TreeDSL { constrStatBuf += intoConstructor(impl.symbol, stat) } - val isDelayedInitSubclass = (clazz isSubClass DelayedInitClass) - populateOmittables() // Initialize all parameters fields that must be kept. |