package scala.tools.nsc
package typechecker
import scala.collection.mutable.{HashMap, WeakHashMap}
import scala.ref.WeakReference
import symtab.Flags
import symtab.Flags._
trait Namers { self: Analyzer =>
import global._
import definitions._
class DeSkolemizeMap(tparams: List[Symbol]) extends TypeMap {
def apply(tp: Type): Type = tp match {
case TypeRef(pre, sym, args)
if (sym.isTypeSkolem && (tparams contains sym.deSkolemize)) =>
mapOver(typeRef(NoPrefix, sym.deSkolemize, args))
case _ =>
mapOver(tp)
}
}
private class NormalNamer(context : Context) extends Namer(context)
def newNamer(context : Context) : Namer = new NormalNamer(context)
private val caseClassOfModuleClass = new WeakHashMap[Symbol, WeakReference[ClassDef]]
private[typechecker] val classAndNamerOfModule = new HashMap[Symbol, (ClassDef, Namer)]
def resetNamer() {
classAndNamerOfModule.clear
}
abstract class Namer(val context: Context) {
val typer = newTyper(context)
def setPrivateWithin[Sym <: Symbol](tree: Tree, sym: Sym, mods: Modifiers): Sym = {
if (mods.hasAccessBoundary)
sym.privateWithin = typer.qualifyingClass(tree, mods.privateWithin, true)
sym
}
def inConstructorFlag: Long =
if (context.owner.isConstructor && !context.inConstructorSuffix || context.owner.isEarlyInitialized) INCONSTRUCTOR
else 0l
def moduleClassFlags(moduleFlags: Long) =
(moduleFlags & ModuleToClassFlags) | FINAL | inConstructorFlag
def updatePosFlags(sym: Symbol, pos: Position, flags: Long): Symbol = {
if (settings.debug.value) log("overwriting " + sym)
val lockedFlag = sym.flags & LOCKED
sym.reset(NoType)
sym setPos pos
sym.flags = flags | lockedFlag
if (sym.isModule && sym.moduleClass != NoSymbol)
updatePosFlags(sym.moduleClass, pos, moduleClassFlags(flags))
var companion: Symbol = NoSymbol
if (sym.owner.isPackageClass && {companion = companionSymbolOf(sym, context); companion != NoSymbol} &&
(companion.rawInfo.isInstanceOf[loaders.SymbolLoader] ||
companion.rawInfo.isComplete && runId(sym.validTo) != currentRunId))
companion.setInfo(NoType)
sym
}
private def isCopyGetter(meth: Symbol) = {
meth.name startsWith (nme.copy + nme.DEFAULT_GETTER_STRING)
}
private def isTemplateContext(context: Context): Boolean = context.tree match {
case Template(_, _, _) => true
case Import(_, _) => isTemplateContext(context.outer)
case _ => false
}
private var innerNamerCache: Namer = null
protected def makeConstructorScope(classContext : Context) : Context = {
val outerContext = classContext.outer.outer
outerContext.makeNewScope(outerContext.tree, outerContext.owner)
}
def namerOf(sym: Symbol): Namer = {
def innerNamer: Namer = {
if (innerNamerCache eq null)
innerNamerCache =
if (!isTemplateContext(context)) this
else newNamer(context.make(context.tree, context.owner, new Scope))
innerNamerCache
}
def primaryConstructorParamNamer: Namer = {
val classContext = context.enclClass
val paramContext = makeConstructorScope(classContext)
val unsafeTypeParams = context.owner.unsafeTypeParams
unsafeTypeParams foreach(sym => paramContext.scope.enter(sym))
newNamer(paramContext)
}
def usePrimary = sym.isTerm && (
(sym.isParamAccessor) ||
(sym.isParameter && sym.owner.isPrimaryConstructor)
)
if (usePrimary) primaryConstructorParamNamer
else innerNamer
}
protected def conflict(newS : Symbol, oldS : Symbol) : Boolean = {
(!oldS.isSourceMethod ||
nme.isSetterName(newS.name) ||
newS.owner.isPackageClass) &&
!((newS.owner.isTypeParameter || newS.owner.isAbstractType) &&
newS.name.length==1 && newS.name(0)=='_')
}
private def setInfo[Sym <: Symbol](sym : Sym)(tpe : LazyType) : Sym = sym.setInfo(tpe)
private def doubleDefError(pos: Position, sym: Symbol) {
context.error(pos,
sym.name.toString() + " is already defined as " +
(if (sym.isSynthetic)
"(compiler-generated) "+ (if (sym.isModule) "case class companion " else "")
else "") +
(if (sym.isCase) "case class " + sym.name else sym.toString()))
}
private def inCurrentScope(m: Symbol): Boolean = {
if (context.owner.isClass) context.owner == m.owner
else m.owner.isClass && context.scope == m.owner.info.decls
}
def enterInScope(sym: Symbol): Symbol = enterInScope(sym, context.scope)
def enterInScope(sym: Symbol, scope: Scope): Symbol = {
if (!(sym.isSourceMethod && sym.owner.isClass && !sym.owner.isPackageClass)) {
var prev = scope.lookupEntry(sym.name)
if ((prev ne null) && prev.owner == scope && conflict(sym, prev.sym)) {
doubleDefError(sym.pos, prev.sym)
sym setInfo ErrorType
scope unlink prev.sym
scope enter sym
} else scope enter sym
} else scope enter sym
}
def enterPackageSymbol(pos: Position, pid: RefTree, pkgOwner: Symbol): Symbol = {
val owner = pid match {
case Ident(name) =>
pkgOwner
case Select(qual: RefTree, name) =>
enterPackageSymbol(pos, qual, pkgOwner).moduleClass
}
var pkg = owner.info.decls.lookup(pid.name)
if (!pkg.isPackage || owner != pkg.owner) {
pkg = owner.newPackage(pos, pid.name.toTermName)
pkg.moduleClass.setInfo(new PackageClassInfoType(new Scope, pkg.moduleClass))
pkg.setInfo(pkg.moduleClass.tpe)
enterInScope(pkg, owner.info.decls)
}
pkg
}
def enterClassSymbol(tree : ClassDef): Symbol = {
var c: Symbol = context.scope.lookup(tree.name)
if (c.isType && c.owner.isPackageClass && context.scope == c.owner.info.decls && currentRun.canRedefine(c)) {
updatePosFlags(c, tree.pos, tree.mods.flags)
setPrivateWithin(tree, c, tree.mods)
} else {
var sym = context.owner.newClass(tree.pos, tree.name)
sym = sym.setFlag(tree.mods.flags | inConstructorFlag)
sym = setPrivateWithin(tree, sym, tree.mods)
c = enterInScope(sym)
}
if (c.owner.isPackageClass) {
val file = context.unit.source.file
val clazz = c.asInstanceOf[ClassSymbol]
if (settings.debug.value && (clazz.sourceFile ne null) && !clazz.sourceFile.equals(file)) {
Console.err.println("SOURCE MISMATCH: " + clazz.sourceFile + " vs. " + file + " SYM=" + c);
}
clazz.sourceFile = file
if (clazz.sourceFile ne null) {
assert(currentRun.canRedefine(clazz) || clazz.sourceFile == currentRun.symSource(c));
currentRun.symSource(c) = clazz.sourceFile
}
registerTopLevelSym(clazz)
}
assert(c.name.toString.indexOf('(') == -1)
c
}
def enterModuleSymbol(tree : ModuleDef): Symbol = {
var m: Symbol = context.scope.lookup(tree.name)
val moduleFlags = tree.mods.flags | MODULE | FINAL
if (m.isModule && !m.isPackage && inCurrentScope(m) &&
(currentRun.canRedefine(m) || m.isSynthetic)) {
updatePosFlags(m, tree.pos, moduleFlags)
setPrivateWithin(tree, m, tree.mods)
if (m.moduleClass != NoSymbol)
setPrivateWithin(tree, m.moduleClass, tree.mods)
context.unit.synthetics -= m
} else {
m = context.owner.newModule(tree.pos, tree.name)
m.setFlag(moduleFlags)
m = setPrivateWithin(tree, m, tree.mods)
m = enterInScope(m)
m.moduleClass.setFlag(moduleClassFlags(moduleFlags))
setPrivateWithin(tree, m.moduleClass, tree.mods)
}
if (m.owner.isPackageClass && !m.isPackage) {
m.moduleClass.sourceFile = context.unit.source.file
currentRun.symSource(m) = m.moduleClass.sourceFile
registerTopLevelSym(m)
}
m
}
def enterSyms(trees: List[Tree]): Namer = {
var namer : Namer = this
for (tree <- trees) {
val txt = namer.enterSym(tree)
if (txt ne namer.context) namer = newNamer(txt)
}
namer
}
def newTypeSkolems(tparams: List[Symbol]): List[Symbol] = {
val tskolems = tparams map (_.newTypeSkolem)
val ltp = new LazyType {
override def complete(sym: Symbol) {
sym setInfo sym.deSkolemize.info.substSym(tparams, tskolems)
}
}
tskolems foreach (_.setInfo(ltp))
tskolems
}
def skolemize(tparams: List[TypeDef]) {
val tskolems = newTypeSkolems(tparams map (_.symbol))
for ((tparam, tskolem) <- tparams zip tskolems) tparam.symbol = tskolem
}
def applicableTypeParams(owner: Symbol): List[Symbol] =
if (owner.isTerm || owner.isPackageClass) List()
else applicableTypeParams(owner.owner) ::: owner.typeParams
def ensureCompanionObject(tree: ClassDef, creator: => Tree): Symbol = {
val m = companionModuleOf(tree.symbol, context)
if (m != NoSymbol && currentRun.compiles(m)) m
else enterSyntheticSym(creator)
}
private def enterSymFinishWith(tree: Tree, tparams: List[TypeDef]) {
val sym = tree.symbol
if (settings.debug.value) log("entered " + sym + " in " + context.owner + ", scope-id = " + context.scope.## )
var ltype = namerOf(sym).typeCompleter(tree)
if (tparams nonEmpty) {
if(!sym.isAbstractType)
newNamer(context.makeNewScope(tree, sym)).enterSyms(tparams)
ltype = new PolyTypeCompleter(tparams, ltype, tree, sym, context)
if (sym.isTerm) skolemize(tparams)
}
if (sym.name == nme.copy || isCopyGetter(sym)) {
setInfo(sym)(mkTypeCompleter(tree)(copySym => {
def copyIsSynthetic() = sym.owner.info.member(nme.copy).isSynthetic
if (sym.isSynthetic && (!sym.hasDefaultFlag || copyIsSynthetic())) {
val constrType = copySym.owner.primaryConstructor.tpe
val subst = new SubstSymMap(copySym.owner.typeParams, tparams map (_.symbol))
for ((params, cparams) <- tree.asInstanceOf[DefDef].vparamss.zip(constrType.paramss);
(param, cparam) <- params.zip(cparams)) {
param.tpt.setType(subst(cparam.tpe))
}
}
ltype.complete(sym)
}))
} else setInfo(sym)(ltype)
}
def enterIfNotThere(sym: Symbol) {
val scope = context.scope
var e = scope.lookupEntry(sym.name)
while ((e ne null) && (e.owner eq scope) && (e.sym ne sym)) e = e.tail
if (!((e ne null) && (e.owner eq scope))) context.scope.enter(sym)
}
def enterSym(tree: Tree): Context = {
def finishWith(tparams: List[TypeDef]) { enterSymFinishWith(tree, tparams) }
def finish() = finishWith(Nil)
def sym = tree.symbol
if (sym != NoSymbol) {
if (forInteractive && sym != null && sym.owner.isTerm) {
enterIfNotThere(sym)
if (sym.isLazy) {
val acc = sym.lazyAccessor
if (acc != NoSymbol) enterIfNotThere(acc)
}
defaultParametersOfMethod(sym) foreach enterIfNotThere
}
return this.context
}
try {
val owner = context.owner
tree match {
case PackageDef(pid, stats) =>
tree.symbol = enterPackageSymbol(tree.pos, pid,
if (context.owner == EmptyPackageClass) RootClass else context.owner)
val namer = newNamer(context.make(tree, sym.moduleClass, sym.info.decls))
namer enterSyms stats
case tree @ ClassDef(mods, name, tparams, impl) =>
tree.symbol = enterClassSymbol(tree)
finishWith(tparams)
if (mods.isCase) {
if (treeInfo.firstConstructorArgs(impl.body).size > MaxFunctionArity)
context.error(tree.pos, "Implementation restriction: case classes cannot have more than " + MaxFunctionArity + " parameters.")
val m = ensureCompanionObject(tree, caseModuleDef(tree))
caseClassOfModuleClass(m.moduleClass) = new WeakReference(tree)
}
val hasDefault = impl.body exists {
case DefDef(_, nme.CONSTRUCTOR, _, vparamss, _, _) => vparamss.flatten exists (_.mods.hasDefault)
case _ => false
}
if (hasDefault) {
val m = ensureCompanionObject(tree, companionModuleDef(tree))
classAndNamerOfModule(m) = (tree, null)
}
case tree @ ModuleDef(mods, name, _) =>
tree.symbol = enterModuleSymbol(tree)
sym.moduleClass setInfo namerOf(sym).moduleClassTypeCompleter(tree)
finish
case vd @ ValDef(mods, name, tp, rhs) =>
if ((!context.owner.isClass ||
(mods.isPrivateLocal && !mods.isCaseAccessor) ||
name.startsWith(nme.OUTER) ||
context.unit.isJava) &&
!mods.isLazy) {
val vsym = owner.newValue(tree.pos, name).setFlag(mods.flags);
if(context.unit.isJava) setPrivateWithin(tree, vsym, mods)
tree.symbol = enterInScope(vsym)
finish
} else {
val mods1 =
if (mods.isPrivateLocal && !mods.isLazy) {
context.error(tree.pos, "private[this] not allowed for case class parameters")
mods &~ LOCAL
} else mods
if (nme.isSetterName(name))
context.error(tree.pos, "Names of vals or vars may not end in `_='")
val getter = enterAccessorMethod(tree, name, getterFlags(mods1.flags), mods1)
setInfo(getter)(namerOf(getter).getterTypeCompleter(vd))
if (mods1.isMutable) {
val setter = enterAccessorMethod(tree, nme.getterToSetter(name), setterFlags(mods1.flags), mods1)
setInfo(setter)(namerOf(setter).setterTypeCompleter(vd))
}
tree.symbol =
if (mods1.isDeferred) {
getter setPos tree.pos
} else {
val vsym =
if (!context.owner.isClass) {
assert(mods1.isLazy)
owner.newValue(tree.pos, name + "$lzy" ).setFlag((mods1.flags | MUTABLE) & ~IMPLICIT)
} else {
val mFlag = if (mods1.isLazy) MUTABLE else 0
val lFlag = if (mods.isPrivateLocal) 0 else LOCAL
val newflags = mods1.flags & FieldFlags | PRIVATE | lFlag | mFlag
owner.newValue(tree.pos, nme.getterToLocal(name)) setFlag newflags
}
enterInScope(vsym)
setInfo(vsym)(namerOf(vsym).typeCompleter(tree))
if (mods1.isLazy)
vsym.setLazyAccessor(getter)
vsym
}
addBeanGetterSetter(vd, getter)
}
case DefDef(mods, nme.CONSTRUCTOR, tparams, _, _, _) =>
val sym = owner.newConstructor(tree.pos).setFlag(mods.flags | owner.getFlag(ConstrFlags))
setPrivateWithin(tree, sym, mods)
tree.symbol = enterInScope(sym)
finishWith(tparams)
case DefDef(mods, name, tparams, _, _, _) =>
tree.symbol = enterNewMethod(tree, name, mods.flags, mods, tree.pos)
if (mods.annotations.exists(ann => isAnn(ann, "bridge")))
tree.symbol setFlag BRIDGE
finishWith(tparams)
case TypeDef(mods, name, tparams, _) =>
var flags: Long = mods.flags
if ((flags & PARAM) != 0L) flags |= DEFERRED
val sym = new TypeSymbol(owner, tree.pos, name).setFlag(flags)
setPrivateWithin(tree, sym, mods)
tree.symbol = enterInScope(sym)
finishWith(tparams)
case DocDef(_, defn) =>
enterSym(defn)
case imp @ Import(_, _) =>
tree.symbol = NoSymbol.newImport(tree.pos)
setInfo(sym)(namerOf(sym).typeCompleter(tree))
return context.makeNewImport(imp)
case _ =>
}
}
catch {
case ex: TypeError =>
typer.reportTypeError(tree.pos, ex)
this.context
}
this.context
}
def enterSyntheticSym(tree: Tree): Symbol = {
enterSym(tree)
context.unit.synthetics(tree.symbol) = tree
tree.symbol
}
def enterNewMethod(tree: Tree, name: Name, flags: Long, mods: Modifiers, pos: Position): TermSymbol = {
val sym = context.owner.newMethod(pos, name.toTermName).setFlag(flags)
setPrivateWithin(tree, sym, mods)
enterInScope(sym)
sym
}
def enterAccessorMethod(tree: Tree, name: Name, flags: Long, mods: Modifiers): TermSymbol =
enterNewMethod(tree, name, flags, mods, tree.pos.focus)
def isAnn(ann: Tree, demand: String) = ann match {
case Apply(Select(New(Ident(name)), _), _) =>
name.toString == demand
case Apply(Select(New(Select(pre, name)), _), _) =>
name.toString == demand
case _ => false
}
private def addBeanGetterSetter(vd: ValDef, getter: Symbol) {
val ValDef(mods, name, tpt, _) = vd
val hasBP = mods.annotations.exists(isAnn(_, "BeanProperty"))
val hasBoolBP = mods.annotations.exists(isAnn(_, "BooleanBeanProperty"))
if ((hasBP || hasBoolBP) && !forMSIL) {
if (!name(0).isLetter)
context.error(vd.pos, "`BeanProperty' annotation can be applied "+
"only to fields that start with a letter")
else if (mods.isPrivate)
context.error(vd.pos, "`BeanProperty' annotation can only be applied "+
"to non-private fields")
else {
val flags = mods.flags & (DEFERRED | OVERRIDE | STATIC)
val beanName = name.toString.capitalize
val getterName = if (hasBoolBP) "is" + beanName
else "get" + beanName
val getterMods = Modifiers(flags, mods.privateWithin, Nil, mods.positions)
val beanGetterDef = atPos(vd.pos.focus) {
DefDef(getterMods, getterName, Nil, List(Nil), tpt.duplicate,
if (mods.isDeferred) EmptyTree
else Select(This(getter.owner.name.toTypeName), name)) }
enterSyntheticSym(beanGetterDef)
if (mods.isMutable) {
val setterName = "set" + beanName
val setter = enterAccessorMethod(vd, setterName, flags, mods)
.setPos(vd.pos.focus)
setInfo(setter)(namerOf(setter).setterTypeCompleter(vd))
}
}
}
}
def typeCompleter(tree: Tree) = mkTypeCompleter(tree) { sym =>
if (settings.debug.value) log("defining " + sym + Flags.flagsToString(sym.flags)+sym.locationString)
val tp = typeSig(tree)
tp match {
case TypeBounds(lo, hi) =>
for (t <- lo) {
t match {
case TypeRef(_, sym, _) => sym.initialize
case _ =>
}
}
case _ =>
}
sym.setInfo(if (sym.isJavaDefined) RestrictJavaArraysMap(tp) else tp)
if ((sym.isAliasType || sym.isAbstractType) && !sym.isParameter &&
!typer.checkNonCyclic(tree.pos, tp))
sym.setInfo(ErrorType)
if (settings.debug.value) log("defined " + sym);
validate(sym)
}
def moduleClassTypeCompleter(tree: Tree) = {
mkTypeCompleter(tree) { sym =>
val moduleSymbol = tree.symbol
assert(moduleSymbol.moduleClass == sym)
moduleSymbol.info
}
}
def getterTypeCompleter(vd: ValDef) = mkTypeCompleter(vd) { sym =>
if (settings.debug.value) log("defining " + sym)
val tp = typeSig(vd)
sym.setInfo(NullaryMethodType(tp))
if (settings.debug.value) log("defined " + sym)
validate(sym)
}
def setterTypeCompleter(vd: ValDef) = mkTypeCompleter(vd) { sym =>
if (settings.debug.value) log("defining " + sym)
val param = sym.newSyntheticValueParam(typeSig(vd))
sym.setInfo(MethodType(List(param), UnitClass.tpe))
if (settings.debug.value) log("defined " + sym)
validate(sym)
}
def selfTypeCompleter(tree: Tree) = mkTypeCompleter(tree) { sym =>
var selftpe = typer.typedType(tree).tpe
if (!(selftpe.typeSymbol isNonBottomSubClass sym.owner))
selftpe = intersectionType(List(sym.owner.tpe, selftpe))
sym.setInfo(selftpe)
}
private def widenIfNecessary(sym: Symbol, tpe: Type, pt: Type): Type = {
val getter =
if (sym.isValue && sym.owner.isClass && sym.isPrivate)
sym.getter(sym.owner)
else sym
def isHidden(tp: Type): Boolean = tp match {
case SingleType(pre, sym) =>
(sym isLessAccessibleThan getter) || isHidden(pre)
case ThisType(sym) =>
sym isLessAccessibleThan getter
case p: SimpleTypeProxy =>
isHidden(p.underlying)
case _ =>
false
}
val tpe1 = tpe.deconst
val tpe2 = tpe1.widen
if (tpe.typeSymbolDirect.isModuleClass) tpe1
else if (sym.isVariable || sym.isMethod && !sym.hasAccessorFlag)
if (tpe2 <:< pt) tpe2 else tpe1
else if (isHidden(tpe)) tpe2
else if (!sym.isFinal) tpe1
else tpe
}
def enterValueParams(owner: Symbol, vparamss: List[List[ValDef]]): List[List[Symbol]] = {
def enterValueParam(param: ValDef): Symbol = {
param.symbol = setInfo(
enterInScope{
val sym = owner.newValueParameter(param.pos, param.name).
setFlag(param.mods.flags & (BYNAMEPARAM | IMPLICIT | DEFAULTPARAM))
setPrivateWithin(param, sym, param.mods)
})(typeCompleter(param))
param.symbol
}
vparamss.map(_.map(enterValueParam))
}
private def templateSig(templ: Template): Type = {
val clazz = context.owner
def checkParent(tpt: Tree): Type = {
val tp = tpt.tpe
if (tp.typeSymbol == context.owner) {
context.error(tpt.pos, ""+tp.typeSymbol+" inherits itself")
AnyRefClass.tpe
} else if (tp.isError) {
AnyRefClass.tpe
} else {
tp
}
}
def enterSelf(self: ValDef) {
if (!self.tpt.isEmpty) {
clazz.typeOfThis = selfTypeCompleter(self.tpt)
self.symbol = clazz.thisSym.setPos(self.pos)
} else {
self.tpt defineType NoType
if (self.name != nme.WILDCARD) {
clazz.typeOfThis = clazz.tpe
self.symbol = clazz.thisSym
} else if (self ne emptyValDef) {
self.symbol = clazz.newThisSym(self.pos) setInfo clazz.tpe
}
}
if (self.name != nme.WILDCARD) {
self.symbol.name = self.name
self.symbol = context.scope enter self.symbol
}
}
var parents = typer.parentTypes(templ) map checkParent
enterSelf(templ.self)
val decls = new Scope
val templateNamer = newNamer(context.make(templ, clazz, decls))
.enterSyms(templ.body)
if (clazz.isModuleClass) {
Namers.this.caseClassOfModuleClass get clazz foreach { cdefRef =>
val cdef = cdefRef()
addApplyUnapply(cdef, templateNamer)
caseClassOfModuleClass -= clazz
}
}
if (clazz.isClass && !clazz.hasModuleFlag) {
Namers.this.caseClassOfModuleClass get companionModuleOf(clazz, context).moduleClass map { cdefRef =>
val cdef = cdefRef()
def hasCopy(decls: Scope) = (decls lookup nme.copy) != NoSymbol
if (!hasCopy(decls) &&
!parents.exists(p => hasCopy(p.typeSymbol.info.decls)) &&
!parents.flatMap(_.baseClasses).distinct.exists(bc => hasCopy(bc.info.decls)))
addCopyMethod(cdef, templateNamer)
}
}
val module = clazz.sourceModule
classAndNamerOfModule get module match {
case Some((cdef, _)) => classAndNamerOfModule(module) = (cdef, templateNamer)
case None =>
}
if (opt.verbose) {
log(
"ClassInfoType(\n%s,\n%s,\n%s)".format(
" " + (parents map (_.typeSymbol) mkString ", "),
if (global.opt.debug) decls.toList map (">> " + _) mkString("\n", "\n", "") else " <decls>",
" " + clazz)
)
}
ClassInfoType(parents, decls, clazz)
}
private def classSig(tparams: List[TypeDef], impl: Template): Type =
polyType(typer.reenterTypeParams(tparams), templateSig(impl))
private def methodSig(mods: Modifiers, tparams: List[TypeDef],
vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree): Type = {
val meth = context.owner
val tparamSyms = typer.reenterTypeParams(tparams)
var vparamSymss = enterValueParams(meth, vparamss)
if (tpt.isEmpty && meth.name == nme.CONSTRUCTOR) {
tpt defineType context.enclClass.owner.tpe
tpt setPos meth.pos.focus
}
def makeMethodType(vparams: List[Symbol], restpe: Type) = {
val params = vparams map (vparam =>
if (meth hasFlag JAVA) vparam.setInfo(objToAny(vparam.tpe)) else vparam)
if (meth hasFlag JAVA) JavaMethodType(params, restpe)
else MethodType(params, restpe)
}
def thisMethodType(restpe: Type) = {
import scala.collection.mutable.ListBuffer
val okParams = ListBuffer[Symbol]()
val checkDependencies: TypeTraverser = new TypeTraverser {
def traverse(tp: Type) = {
tp match {
case SingleType(_, sym) =>
if (sym.owner == meth && sym.isValueParameter && !(okParams contains sym))
context.error(
sym.pos,
"illegal dependent method type"+
(if (settings.YdepMethTpes.value)
": parameter appears in the type of another parameter in the same section or an earlier one"
else ""))
case _ =>
mapOver(tp)
}
this
}
}
for(vps <- vparamSymss) {
for(p <- vps) checkDependencies(p.info)
if(settings.YdepMethTpes.value) okParams ++= vps
}
checkDependencies(restpe)
polyType(
tparamSyms,
if (vparamSymss.isEmpty) NullaryMethodType(restpe)
else (vparamSymss :\ restpe) (makeMethodType))
}
var resultPt = if (tpt.isEmpty) WildcardType else typer.typedType(tpt).tpe
val site = meth.owner.thisType
def overriddenSymbol = intersectionType(meth.owner.info.parents).nonPrivateMember(meth.name).filter(sym => {
sym != NoSymbol && (site.memberType(sym) matches thisMethodType(resultPt).substSym(tparams map (_.symbol), tparamSyms))
})
if (meth.owner.isClass && (tpt.isEmpty || vparamss.exists(_.exists(_.tpt.isEmpty)))) {
for (vparams <- vparamss; vparam <- vparams)
if (vparam.tpt.isEmpty) vparam.symbol setInfo WildcardType
val overridden = overriddenSymbol
if (overridden != NoSymbol && !overridden.isOverloaded) {
overridden.cookJavaRawInfo()
resultPt = site.memberType(overridden) match {
case PolyType(tparams, rt) => rt.substSym(tparams, tparamSyms)
case mt => mt
}
for (vparams <- vparamss) {
var pps = resultPt.params
for (vparam <- vparams) {
if (vparam.tpt.isEmpty) {
val paramtpe = pps.head.tpe
vparam.symbol setInfo paramtpe
vparam.tpt defineType paramtpe
vparam.tpt setPos vparam.pos.focus
}
pps = pps.tail
}
resultPt = resultPt.resultType
}
resultPt match {
case NullaryMethodType(rtpe) => resultPt = rtpe
case MethodType(List(), rtpe) => resultPt = rtpe
case _ =>
}
if (tpt.isEmpty) {
meth setInfo thisMethodType(resultPt)
}
}
}
if (meth.owner.isClass && vparamss.isEmpty && overriddenSymbol.alternatives.exists(
_.info.isInstanceOf[MethodType])) {
vparamSymss = List(List())
}
for (vparams <- vparamss; vparam <- vparams if vparam.tpt.isEmpty) {
context.error(vparam.pos, "missing parameter type")
vparam.tpt defineType ErrorType
}
addDefaultGetters(meth, vparamss, tparams, overriddenSymbol)
thisMethodType({
val rt = if (tpt.isEmpty) {
val pt = resultPt.substSym(tparamSyms, tparams map (_.symbol))
tpt defineType widenIfNecessary(meth, typer.computeType(rhs, pt), pt)
tpt setPos meth.pos.focus
tpt.tpe
} else typer.typedType(tpt).tpe
if (meth.hasDefaultFlag)
rt.withAnnotation(AnnotationInfo(definitions.uncheckedVarianceClass.tpe, List(), List()))
else rt
})
}
private def addDefaultGetters(meth: Symbol, vparamss: List[List[ValDef]], tparams: List[TypeDef], overriddenSymbol: => Symbol) {
val isConstr = meth.isConstructor
val overridden = if (isConstr || !meth.owner.isClass) NoSymbol
else overriddenSymbol
val overrides = overridden != NoSymbol && !overridden.isOverloaded
var baseParamss = overridden.tpe.paramss
if (vparamss.isEmpty && baseParamss == List(Nil)) baseParamss = Nil
if (vparamss == List(Nil) && baseParamss.isEmpty) baseParamss = List(Nil)
assert(!overrides || vparamss.length == baseParamss.length, ""+ meth.fullName + ", "+ overridden.fullName)
var ownerNamer: Option[Namer] = None
var moduleNamer: Option[(ClassDef, Namer)] = None
var posCounter = 1
(List[List[ValDef]]() /: (vparamss))((previous: List[List[ValDef]], vparams: List[ValDef]) => {
assert(!overrides || vparams.length == baseParamss.head.length, ""+ meth.fullName + ", "+ overridden.fullName)
var baseParams = if (overrides) baseParamss.head else Nil
for (vparam <- vparams) {
val sym = vparam.symbol
val baseHasDefault = overrides && baseParams.head.hasDefaultFlag
if (sym.hasDefaultFlag) {
val oflag = if (baseHasDefault) OVERRIDE else 0
val name = nme.defaultGetterName(meth.name, posCounter)
var deftParams = tparams map copyUntyped[TypeDef]
val defvParamss = previous map (_.map(p => {
val p1 = atPos(p.pos.focus) { ValDef(p.mods &~ DEFAULTPARAM, p.name, p.tpt.duplicate, EmptyTree) }
UnTyper.traverse(p1)
p1
}))
val parentNamer = if (isConstr) {
val (cdef, nmr) = moduleNamer.getOrElse {
val module = companionModuleOf(meth.owner, context)
module.initialize
classAndNamerOfModule get module match {
case s @ Some((cdef, nmr)) if nmr != null =>
moduleNamer = s
(cdef, nmr)
case _ =>
return
}
}
deftParams = cdef.tparams map copyUntypedInvariant
nmr
} else {
ownerNamer.getOrElse {
val ctx = context.nextEnclosing(c => c.scope.toList.contains(meth))
assert(ctx != NoContext)
val nmr = newNamer(ctx)
ownerNamer = Some(nmr)
nmr
}
}
val names = deftParams map { case TypeDef(_, name, _, _) => name }
object subst extends Transformer {
override def transform(tree: Tree): Tree = tree match {
case Ident(name) if (names contains name) =>
TypeTree()
case _ =>
super.transform(tree)
}
def apply(tree: Tree) = {
val r = transform(tree)
if (r.exists(_.isEmpty)) TypeTree()
else r
}
}
val defTpt = subst(copyUntyped(vparam.tpt match {
case AppliedTypeTree(_, List(arg)) if sym.hasFlag(BYNAMEPARAM) => arg
case t => t
}))
val defRhs = copyUntyped(vparam.rhs)
val defaultTree = atPos(vparam.pos.focus) {
DefDef(
Modifiers(meth.flags & (PRIVATE | PROTECTED | FINAL)) | SYNTHETIC | DEFAULTPARAM | oflag,
name, deftParams, defvParamss, defTpt, defRhs)
}
if (!isConstr)
meth.owner.resetFlag(INTERFACE)
val default = parentNamer.enterSyntheticSym(defaultTree)
if (forInteractive && default.owner.isTerm) {
defaultParametersOfMethod(meth) += default
}
} else if (baseHasDefault) {
sym.setFlag(DEFAULTPARAM)
}
posCounter += 1
if (overrides) baseParams = baseParams.tail
}
if (overrides) baseParamss = baseParamss.tail
previous ::: List(vparams)
})
}
private def typeDefSig(tpsym: Symbol, tparams: List[TypeDef], rhs: Tree) = {
val tparamSyms = typer.reenterTypeParams(tparams)
val tp = typer.typedType(rhs).tpe match {
case TypeBounds(lt, rt) if (lt.isError || rt.isError) =>
TypeBounds.empty
case tp @ TypeBounds(lt, rt) if (tpsym hasFlag JAVA) =>
TypeBounds(lt, objToAny(rt))
case tp =>
tp
}
polyType(tparamSyms, tp)
}
def addApplyUnapply(cdef: ClassDef, namer: Namer) {
if (!cdef.symbol.hasAbstractFlag)
namer.enterSyntheticSym(caseModuleApplyMeth(cdef))
namer.enterSyntheticSym(caseModuleUnapplyMeth(cdef))
}
def addCopyMethod(cdef: ClassDef, namer: Namer) {
caseClassCopyMeth(cdef) foreach (namer.enterSyntheticSym(_))
}
def typeSig(tree: Tree): Type = {
def annotate(annotated: Symbol) = {
if (!annotated.isInitialized) tree match {
case defn: MemberDef =>
val ainfos = defn.mods.annotations filter { _ != null } map { ann =>
LazyAnnotationInfo(() => typer.typedAnnotation(ann))
}
if (!ainfos.isEmpty)
annotated.setAnnotations(ainfos)
if (annotated.isTypeSkolem)
annotated.deSkolemize.setAnnotations(ainfos)
case _ =>
}
}
val sym: Symbol = tree.symbol
annotate(sym)
if (sym.isModule) annotate(sym.moduleClass)
val result =
try {
tree match {
case ClassDef(_, _, tparams, impl) =>
newNamer(context.makeNewScope(tree, sym)).classSig(tparams, impl)
case ModuleDef(_, _, impl) =>
val clazz = sym.moduleClass
clazz.setInfo(newNamer(context.makeNewScope(tree, clazz)).templateSig(impl))
clazz.tpe
case DefDef(mods, _, tparams, vparamss, tpt, rhs) =>
newNamer(context.makeNewScope(tree, sym)).methodSig(mods, tparams, vparamss, tpt, rhs)
case vdef @ ValDef(mods, name, tpt, rhs) =>
val typer1 = typer.constrTyperIf(sym.hasFlag(PARAM | PRESUPER) && !mods.isJavaDefined && sym.owner.isConstructor)
if (tpt.isEmpty) {
if (rhs.isEmpty) {
context.error(tpt.pos, "missing parameter type");
ErrorType
} else {
tpt defineType widenIfNecessary(
sym,
newTyper(typer1.context.make(vdef, sym)).computeType(rhs, WildcardType),
WildcardType)
tpt setPos vdef.pos.focus
tpt.tpe
}
} else typer1.typedType(tpt).tpe
case TypeDef(_, _, tparams, rhs) =>
newNamer(context.makeNewScope(tree, sym)).typeDefSig(sym, tparams, rhs)
case Import(expr, selectors) =>
val expr1 = typer.typedQualifier(expr)
val base = expr1.tpe
typer.checkStable(expr1)
if ((expr1.symbol ne null) && expr1.symbol.isRootPackage) context.error(tree.pos, "_root_ cannot be imported")
def checkNotRedundant(pos: Position, from: Name, to: Name): Boolean = {
if (!tree.symbol.isSynthetic &&
!((expr1.symbol ne null) && expr1.symbol.isInterpreterWrapper) &&
base.member(from) != NoSymbol) {
val e = context.scope.lookupEntry(to)
def warnRedundant(sym: Symbol) =
context.unit.warning(pos, "imported `"+to+
"' is permanently hidden by definition of "+sym+
sym.locationString)
if ((e ne null) && e.owner == context.scope && e.sym.exists) {
warnRedundant(e.sym); return false
} else if (context eq context.enclClass) {
val defSym = context.prefix.member(to) filter (
sym => sym.exists && context.isAccessible(sym, context.prefix, false))
if (defSym != NoSymbol) { warnRedundant(defSym); return false }
}
}
true
}
def isValidSelector(from: Name)(fun : => Unit) {
if (from.bothNames forall (x => (base nonLocalMember x) == NoSymbol))
fun
}
def checkSelectors(selectors: List[ImportSelector]): Unit = selectors match {
case ImportSelector(from, _, to, _) :: rest =>
if (from != nme.WILDCARD && base != ErrorType) {
isValidSelector(from) {
if (currentRun.compileSourceFor(expr, from)) {
typeSig(tree)
return
}
def notMember = context.error(tree.pos, from.decode + " is not a member of " + expr)
if (from endsWith nme.raw.DOLLAR)
isValidSelector(from stripEnd "$")(notMember)
else
notMember
}
if (checkNotRedundant(tree.pos, from, to))
checkNotRedundant(tree.pos, from.toTypeName, to.toTypeName)
}
if (from != nme.WILDCARD && (rest.exists (sel => sel.name == from)))
context.error(tree.pos, from.decode + " is renamed twice")
if ((to ne null) && to != nme.WILDCARD && (rest exists (sel => sel.rename == to)))
context.error(tree.pos, to.decode + " appears twice as a target of a renaming")
checkSelectors(rest)
case Nil =>
}
checkSelectors(selectors)
transformed(tree) = treeCopy.Import(tree, expr1, selectors)
ImportType(expr1)
}
} catch {
case ex: TypeError =>
typer.reportTypeError(tree.pos, ex)
ErrorType
}
result match {
case PolyType(tparams @ (tp :: _), _) if tp.owner.isTerm =>
new DeSkolemizeMap(tparams) mapOver result
case _ =>
result
}
}
private object RestrictJavaArraysMap extends TypeMap {
def apply(tp: Type): Type = tp match {
case TypeRef(pre, ArrayClass, List(elemtp))
if elemtp.typeSymbol.isAbstractType && !(elemtp <:< definitions.ObjectClass.tpe) =>
TypeRef(pre, ArrayClass, List(intersectionType(List(elemtp, definitions.ObjectClass.tpe))))
case _ =>
mapOver(tp)
}
}
def validate(sym: Symbol) {
def checkNoConflict(flag1: Int, flag2: Int) {
if (sym.hasFlag(flag1) && sym.hasFlag(flag2))
context.error(sym.pos,
if (flag1 == DEFERRED)
"abstract member may not have " + Flags.flagsToString(flag2) + " modifier";
else
"illegal combination of modifiers: " +
Flags.flagsToString(flag1) + " and " + Flags.flagsToString(flag2) +
" for: " + sym);
}
if (sym.hasFlag(IMPLICIT) && !sym.isTerm)
context.error(sym.pos, "`implicit' modifier can be used only for values, variables and methods")
if (sym.hasFlag(IMPLICIT) && sym.owner.isPackageClass)
context.error(sym.pos, "`implicit' modifier cannot be used for top-level objects")
if (sym.hasFlag(SEALED) && !sym.isClass)
context.error(sym.pos, "`sealed' modifier can be used only for classes")
if (sym.hasFlag(ABSTRACT) && !sym.isClass)
context.error(sym.pos, "`abstract' modifier can be used only for classes; " +
"\nit should be omitted for abstract members")
if (sym.hasFlag(OVERRIDE | ABSOVERRIDE) && !sym.hasFlag(TRAIT) && sym.isClass)
context.error(sym.pos, "`override' modifier not allowed for classes")
if (sym.hasFlag(OVERRIDE | ABSOVERRIDE) && sym.isConstructor)
context.error(sym.pos, "`override' modifier not allowed for constructors")
if (sym.hasFlag(ABSOVERRIDE) && !sym.owner.isTrait)
context.error(sym.pos, "`abstract override' modifier only allowed for members of traits")
if (sym.isLazy && sym.hasFlag(PRESUPER))
context.error(sym.pos, "`lazy' definitions may not be initialized early")
if (sym.info.typeSymbol == FunctionClass(0) &&
sym.isValueParameter && sym.owner.isCaseClass)
context.error(sym.pos, "pass-by-name arguments not allowed for case class parameters")
if (sym hasFlag DEFERRED) {
if (sym.hasAnnotation(definitions.NativeAttr))
sym.resetFlag(DEFERRED)
else if (!sym.isValueParameter && !sym.isTypeParameterOrSkolem &&
!context.tree.isInstanceOf[ExistentialTypeTree] &&
(!sym.owner.isClass || sym.owner.isModuleClass || sym.owner.isAnonymousClass)) {
context.error(sym.pos,
"only classes can have declared but undefined members" + varNotice(sym))
sym.resetFlag(DEFERRED)
}
}
checkNoConflict(DEFERRED, PRIVATE)
checkNoConflict(FINAL, SEALED)
checkNoConflict(PRIVATE, PROTECTED)
checkNoConflict(ABSTRACT, FINAL)
checkNoConflict(DEFERRED, FINAL)
checkNoConflict(ABSTRACT, OVERRIDE)
}
}
abstract class TypeCompleter extends LazyType {
val tree: Tree
}
var lockedCount = 0
def mkTypeCompleter(t: Tree)(c: Symbol => Unit) = new TypeCompleter {
val tree = t
override def complete(sym: Symbol) = try {
lockedCount += 1
c(sym)
} finally {
lockedCount -= 1
}
}
class PolyTypeCompleter(tparams: List[Tree], restp: TypeCompleter, owner: Tree, ownerSym: Symbol, ctx: Context) extends TypeCompleter {
override val typeParams: List[Symbol]= tparams map (_.symbol)
override val tree = restp.tree
override def complete(sym: Symbol) = try {
lockedCount += 1
if(ownerSym.isAbstractType)
newNamer(ctx.makeNewScope(owner, ownerSym)).enterSyms(tparams)
restp.complete(sym)
} finally {
lockedCount -= 1
}
}
def underlying(member: Symbol): Symbol =
if (member.hasAccessorFlag) {
if (member.isDeferred) {
val getter = if (member.isSetter) member.getter(member.owner) else member
val result = getter.owner.newValue(getter.pos, getter.name.toTermName)
.setInfo(getter.tpe.resultType)
.setFlag(DEFERRED)
if (getter.setter(member.owner) != NoSymbol) result.setFlag(MUTABLE)
result
} else member.accessed
} else member
def companionModuleOf(clazz: Symbol, context: Context): Symbol = {
try {
var res = clazz.companionModule
if (res == NoSymbol)
res = context.lookup(clazz.name.toTermName, clazz.owner).suchThat(sym =>
sym.hasModuleFlag && sym.isCoDefinedWith(clazz))
res
} catch {
case e: InvalidCompanions =>
context.error(clazz.pos, e.getMessage)
NoSymbol
}
}
def companionClassOf(module: Symbol, context: Context): Symbol = {
try {
var res = module.companionClass
if (res == NoSymbol)
res = context.lookup(module.name.toTypeName, module.owner).suchThat(_.isCoDefinedWith(module))
res
} catch {
case e: InvalidCompanions =>
context.error(module.pos, e.getMessage)
NoSymbol
}
}
def companionSymbolOf(sym: Symbol, context: Context) =
if (sym.isTerm) companionClassOf(sym, context)
else if (sym.isClass) companionModuleOf(sym, context)
else NoSymbol
def varNotice(sym: Symbol): String =
if (underlying(sym).isVariable)
"\n(Note that variables need to be initialized to be defined)"
else ""
}