symbols.scala
来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 1,613 行 · 第 1/4 页
SCALA
1,613 行
} else if (isModule) moduleClass.infoString(tp) else tp match { case PolyType(tparams, res) => typeParamsString + infoString(res) case MethodType(pts, res) => pts.mkString("(", ",", ")") + infoString(res) case _ => ": " + tp } } def infosString = infos.toString() /** String representation of symbol's variance */ def varianceString: String = if (variance == 1) "+" else if (variance == -1) "-" else "" /** String representation of symbol's definition */ def defString: String = { val f = if (settings.debug.value) flags else if (owner.isRefinementClass) flags & ExplicitFlags & ~OVERRIDE else flags & ExplicitFlags compose(List(flagsToString(f), keyString, varianceString + nameString + (if (hasRawInfo) infoString(rawInfo) else "<_>"))) } /** Concatenate strings separated by spaces */ private def compose(ss: List[String]): String = ss.filter("" !=).mkString("", " ", "") /** String representation of existentially bound variable */ def existentialToString = { val tname = name.toString if ((tname endsWith ".type") && (info.bounds.hi.typeSymbol isSubClass SingletonClass) && !settings.debug.value) "val "+tname.substring(0, tname.length - 5)+": "+dropSingletonType(info.bounds.hi) else defString } } /** A class for term symbols */ class TermSymbol(initOwner: Symbol, initPos: Position, initName: Name) extends Symbol(initOwner, initPos, initName) { override def isTerm = true/* val marker = initName.toString match { case "forCLDC" => new MarkForCLDC case _ => null }*/ privateWithin = NoSymbol protected var referenced: Symbol = NoSymbol def cloneSymbolImpl(owner: Symbol): Symbol = { val clone = new TermSymbol(owner, pos, name) clone.referenced = referenced clone } override def alias: Symbol = if (hasFlag(SUPERACCESSOR | PARAMACCESSOR | MIXEDIN)) initialize.referenced else NoSymbol def setAlias(alias: Symbol): TermSymbol = { assert(alias != NoSymbol, this) assert(!(alias hasFlag OVERLOADED), alias) assert(hasFlag(SUPERACCESSOR | PARAMACCESSOR | MIXEDIN), this) referenced = alias this } override def outerSource: Symbol = if (name endsWith nme.OUTER) initialize.referenced else NoSymbol override def moduleClass: Symbol = if (hasFlag(MODULE)) referenced else NoSymbol def setModuleClass(clazz: Symbol): TermSymbol = { assert(hasFlag(MODULE)) referenced = clazz this } def setLazyAccessor(sym: Symbol): TermSymbol = { // @S: in IDE setLazyAccessor can be called multiple times on same sym assert(hasFlag(LAZY) && (referenced == NoSymbol || referenced == sym), this) referenced = sym this } override def lazyAccessor: Symbol = { assert(hasFlag(LAZY), this) referenced } } /** A class for module symbols */ class ModuleSymbol(initOwner: Symbol, initPos: Position, initName: Name) extends TermSymbol(initOwner, initPos, initName) { private var flatname = nme.EMPTY override def owner: Symbol = if (phase.flatClasses && !hasFlag(METHOD) && rawowner != NoSymbol && !rawowner.isPackageClass) rawowner.owner else rawowner override def name: Name = if (phase.flatClasses && !hasFlag(METHOD) && rawowner != NoSymbol && !rawowner.isPackageClass) { if (flatname == nme.EMPTY) { assert(rawowner.isClass) flatname = newTermName(rawowner.name.toString() + "$" + rawname) } flatname } else rawname override def cloneSymbolImpl(owner: Symbol): Symbol = { val clone = new ModuleSymbol(owner, pos, name) clone.referenced = referenced clone } } /** A class of type symbols. Alias and abstract types are direct instances * of this class. Classes are instances of a subclass. */ class TypeSymbol(initOwner: Symbol, initPos: Position, initName: Name) extends Symbol(initOwner, initPos, initName) { privateWithin = NoSymbol private var tyconCache: Type = null private var tyconRunId = NoRunId private var tpeCache: Type = _ private var tpePeriod = NoPeriod override def isType = true override def isTypeMember = true override def isAbstractType = isDeferred override def isAliasType = !isDeferred override def tpe: Type = { if (tpeCache eq NoType) throw CyclicReference(this, typeConstructor) if (tpePeriod != currentPeriod) { if (isValid(tpePeriod)) { tpePeriod = currentPeriod } else { if (isInitialized) tpePeriod = currentPeriod tpeCache = NoType val targs = if (phase.erasedTypes && this != ArrayClass) List() else unsafeTypeParams map (_.typeConstructor) //@M! use typeConstructor to generate dummy type arguments, // sym.tpe should not be called on a symbol that's supposed to be a higher-kinded type // memberType should be used instead, that's why it uses tpeHK and not tpe tpeCache = typeRef(if (hasFlag(PARAM | EXISTENTIAL)) NoPrefix else owner.thisType, this, targs) } } assert(tpeCache ne null/*, "" + this + " " + phase*/)//debug tpeCache } override def typeConstructor: Type = { if ((tyconCache eq null) || tyconRunId != currentRunId) { tyconCache = typeRef(if (hasFlag(PARAM | EXISTENTIAL)) NoPrefix else owner.thisType, this, List()) tyconRunId = currentRunId } assert(tyconCache ne null) tyconCache } override def setInfo(tp: Type): this.type = { tpePeriod = NoPeriod tyconCache = null if (tp.isComplete) tp match { case PolyType(_, _) => resetFlag(MONOMORPHIC) case NoType | AnnotatedType(_, _, _) => ; case _ => setFlag(MONOMORPHIC) } super.setInfo(tp) this } override def reset(completer: Type) { super.reset(completer) tpePeriod = NoPeriod tyconRunId = NoRunId } def cloneSymbolImpl(owner: Symbol): Symbol = new TypeSymbol(owner, pos, name) if (util.Statistics.enabled) typeSymbolCount = typeSymbolCount + 1 } /** A class for type parameters viewed from inside their scopes */ class TypeSkolem(initOwner: Symbol, initPos: Position, initName: Name, origin: AnyRef) extends TypeSymbol(initOwner, initPos, initName) { /** The skolemization level in place when the skolem was constructed */ val level = skolemizationLevel override def isSkolem = true override def deSkolemize = origin match { case s: Symbol => s case _ => this } override def unpackLocation = origin override def typeParams = info.typeParams //@M! (not deSkolemize.typeParams!!), also can't leave superclass definition: use info, not rawInfo override def cloneSymbolImpl(owner: Symbol): Symbol = { throw new Error("should not clone a type skolem") } override def nameString: String = if (settings.debug.value) (super.nameString + "&" + level) else super.nameString } /** A class for class symbols */ class ClassSymbol(initOwner: Symbol, initPos: Position, initName: Name) extends TypeSymbol(initOwner, initPos, initName) { /** The classfile from which this class was loaded. Maybe null. */ var classFile: AbstractFile = null; private var source: AbstractFile = null override def sourceFile = if (owner.isPackageClass) source else super.sourceFile override def sourceFile_=(f: AbstractFile) { //System.err.println("set source file of " + this + ": " + f); attachSource(this, f) source = f } override def isFromClassFile = { if (classFile ne null) true else if (owner.isPackageClass) false else super.isFromClassFile } private var thissym: Symbol = this override def isClass: Boolean = true override def isTypeMember = false override def isAbstractType = false override def isAliasType = false override def reset(completer: Type) { super.reset(completer) thissym = this } private var flatname = nme.EMPTY override def owner: Symbol = if (phase.flatClasses && rawowner != NoSymbol && !rawowner.isPackageClass) rawowner.owner else rawowner override def name: Name = if (phase.flatClasses && rawowner != NoSymbol && !rawowner.isPackageClass) { if (flatname == nme.EMPTY) { assert(rawowner.isClass) flatname = newTypeName(rawowner.name.toString() + "$" + rawname) } flatname } else rawname private var thisTypeCache: Type = _ private var thisTypePeriod = NoPeriod /** the type this.type in this class */ override def thisType: Type = { val period = thisTypePeriod if (period != currentPeriod) { thisTypePeriod = currentPeriod if (!isValid(period)) thisTypeCache = mkThisType(this) } thisTypeCache } /** A symbol carrying the self type of the class as its type */ override def thisSym: Symbol = thissym override def typeOfThis: Type = if (getFlag(MODULE | IMPLCLASS) == MODULE && owner != NoSymbol) singleType(owner.thisType, sourceModule) else thissym.tpe /** Sets the self type of the class */ override def typeOfThis_=(tp: Type) { thissym = newThisSym(pos).setInfo(tp) } override def cloneSymbolImpl(owner: Symbol): Symbol = { assert(!isModuleClass) val clone = new ClassSymbol(owner, pos, name) if (thisSym != this) { clone.typeOfThis = typeOfThis clone.thisSym.name = thisSym.name } clone } override def sourceModule = if (isModuleClass) linkedModuleOfClass else NoSymbol private var childSet: Set[Symbol] = emptySymbolSet override def children: Set[Symbol] = childSet override def addChild(sym: Symbol) { childSet = childSet + sym } if (util.Statistics.enabled) classSymbolCount = classSymbolCount + 1 } /** A class for module class symbols * Note: Not all module classes are of this type; when unpickled, we get * plain class symbols! */ class ModuleClassSymbol(owner: Symbol, pos: Position, name: Name) extends ClassSymbol(owner, pos, name) { private var module: Symbol = null def this(module: TermSymbol) = { this(module.owner, module.pos, module.name.toTypeName) setFlag(module.getFlag(ModuleToClassFlags) | MODULE | FINAL) setSourceModule(module) } override def sourceModule = module def setSourceModule(module: Symbol) { this.module = module } } /** An object repreesenting a missing symbol */ object NoSymbol extends Symbol(null, NoPosition, nme.NOSYMBOL) { setInfo(NoType) privateWithin = this override def setInfo(info: Type): this.type = { infos = TypeHistory(1, NoType, null) rawflags = rawflags & ~ LOCKED validTo = currentPeriod this } override def defString: String = toString override def enclClass: Symbol = this override def toplevelClass: Symbol = this override def enclMethod: Symbol = this override def owner: Symbol = throw new Error("no-symbol does not have owner") override def sourceFile: AbstractFile = null override def ownerChain: List[Symbol] = List() override def alternatives: List[Symbol] = List() override def reset(completer: Type) {} override def info: Type = NoType override def rawInfo: Type = NoType override def accessBoundary(base: Symbol): Symbol = RootClass def cloneSymbolImpl(owner: Symbol): Symbol = throw new Error() } def cloneSymbols(syms: List[Symbol]): List[Symbol] = { val syms1 = syms map (_.cloneSymbol) for (sym1 <- syms1) sym1.setInfo(sym1.info.substSym(syms, syms1)) syms1 } def cloneSymbols(syms: List[Symbol], owner: Symbol): List[Symbol] = { val syms1 = syms map (_.cloneSymbol(owner)) for (sym1 <- syms1) sym1.setInfo(sym1.info.substSym(syms, syms1)) syms1 } /** An exception for cyclic references of symbol definitions */ case class CyclicReference(sym: Symbol, info: Type) extends TypeError("illegal cyclic reference involving " + sym) /** A class for type histories */ private sealed case class TypeHistory(var validFrom: Period, info: Type, prev: TypeHistory) { assert((prev eq null) || phaseId(validFrom) > phaseId(prev.validFrom), this) assert(validFrom != NoPeriod) override def toString() = "TypeHistory(" + phaseOf(validFrom)+":"+runId(validFrom) + "," + info + "," + prev + ")" }/* var occs = 0class MarkForCLDC { val atRun: Int = currentRunId occs += 1 println("new "+getClass+" at "+atRun+" ("+occs+" total)") override def finalize() { occs -=1 println("drop "+getClass+" from "+atRun+" ("+occs+" total)") }}*/}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?