From c0ed9aec10ba8ea3e3c0ecc22f2e9f92e550ad10 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 18 Feb 2013 17:07:13 +0100 Subject: Addec convenience methods for symbol creation. --- src/dotty/tools/dotc/core/Symbols.scala | 55 ++++++++++++++++++---- src/dotty/tools/dotc/core/TypedTreeGen.scala | 15 ++---- .../tools/dotc/core/pickling/ClassfileParser.scala | 6 +-- 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/dotty/tools/dotc/core/Symbols.scala b/src/dotty/tools/dotc/core/Symbols.scala index cd644df4e..68579f15f 100644 --- a/src/dotty/tools/dotc/core/Symbols.scala +++ b/src/dotty/tools/dotc/core/Symbols.scala @@ -19,6 +19,8 @@ import io.AbstractFile /** Creation methods for symbols */ trait Symbols { this: Context => +// ---- Fundamental symbol creation methods ---------------------------------- + def newLazySymbol[N <: Name](owner: Symbol, name: N, initFlags: FlagSet, completer: SymCompleter, coord: Coord = NoCoord) = new Symbol(coord, new LazySymDenotation(_, owner, name, initFlags, completer)) { type ThisName = N @@ -45,9 +47,6 @@ trait Symbols { this: Context => (module, modcls) } - def newLazyPackageSymbols(owner: Symbol, name: TermName, completer: ClassCompleter) = - newLazyModuleSymbols(owner, name, PackageCreationFlags, completer) - def newSymbol[N <: Name](owner: Symbol, name: N, flags: FlagSet, info: Type, privateWithin: Symbol = NoSymbol, coord: Coord = NoCoord) = new Symbol(coord, CompleteSymDenotation(_, owner, name, flags, info, privateWithin)) { type ThisName = N @@ -90,13 +89,6 @@ trait Symbols { this: Context => (module, modcls) } - def newPackageSymbols( - owner: Symbol, - name: TermName, - decls: Scope = newScope) = - newModuleSymbols( - owner, name, PackageCreationFlags, PackageCreationFlags, Nil, NoSymbol, decls) - def newStubSymbol(owner: Symbol, name: Name, file: AbstractFile = null): Symbol = { def stub = new StubCompleter(ctx.condensed) name match { @@ -105,12 +97,55 @@ trait Symbols { this: Context => } } +// ---- Derived symbol creation methods ------------------------------------- + + def newLazyPackageSymbols(owner: Symbol, name: TermName, completer: ClassCompleter) = + newLazyModuleSymbols(owner, name, PackageCreationFlags, completer) + + def newPackageSymbols( + owner: Symbol, + name: TermName, + decls: Scope = newScope) = + newModuleSymbols( + owner, name, PackageCreationFlags, PackageCreationFlags, Nil, NoSymbol, decls) + def newLocalDummy(cls: Symbol, coord: Coord = NoCoord) = newSymbol(cls, nme.localDummyName(cls), EmptyFlags, NoType) def newImportSymbol(expr: TypedTree, coord: Coord = NoCoord) = newSymbol(NoSymbol, nme.IMPORT, EmptyFlags, ImportType(expr), coord = coord) + def newConstructor(cls: ClassSymbol, flags: FlagSet, paramNames: List[TermName], paramTypes: List[Type], privateWithin: Symbol = NoSymbol, coord: Coord = NoCoord) = + newSymbol(cls, nme.CONSTRUCTOR, flags, MethodType(paramNames, paramTypes)(_ => cls.typeConstructor), privateWithin, coord) + + def newDefaultConstructor(cls: ClassSymbol) = + newConstructor(cls, EmptyFlags, Nil, Nil) + + def newSelfSym(cls: ClassSymbol) = + ctx.newSymbol(cls, nme.THIS, SyntheticArtifact, cls.selfType) + + /** Create new type parameters with given owner, names, and flags. + * @param boundsFn A function that, given type refs to the newly created + * parameters returns a list of their bounds. + */ + def newTypeParams( + owner: Symbol, + names: List[TypeName], + flags: FlagSet, + boundsFn: List[TypeRef] => List[Type]) = { + lazy val tparams: List[TypeSymbol] = names map { name => + newLazySymbol(owner, name, flags | TypeParam, { denot => + denot.info = bounds(denot.symbol.asType) + }) + } + lazy val bounds = (tparams zip boundsFn(tparams map (_.typeConstructor))).toMap + tparams + } + + private val reverseApply = (x: TypeSymbol, f: TypeSymbol => TypeBounds) => f(x) + +// ----- Locating predefined symbols ---------------------------------------- + def requiredPackage(path: PreName): TermSymbol = base.staticRef(path.toTermName).requiredSymbol(_.isPackage).asTerm diff --git a/src/dotty/tools/dotc/core/TypedTreeGen.scala b/src/dotty/tools/dotc/core/TypedTreeGen.scala index 95bb3a324..cfa1810df 100644 --- a/src/dotty/tools/dotc/core/TypedTreeGen.scala +++ b/src/dotty/tools/dotc/core/TypedTreeGen.scala @@ -140,15 +140,10 @@ object TypedTrees { val (tparams, mtp) = sym.info match { case tp: PolyType => - val paramBounds = (tp.paramNames zip tp.paramBounds).toMap - def typeParam(name: TypeName): TypeSymbol = - ctx.newLazySymbol(sym, name, TypeParam, { denot => - denot.info = new InstPolyMap(tp, tparamRefs) apply - paramBounds(denot.symbol.asType.name) - }) - lazy val tparams = tp.paramNames map typeParam - lazy val tparamRefs = tparams map (_.typeConstructor) - (tparams, tp.instantiate(tparamRefs)) + def paramBounds(trefs: List[TypeRef]): List[Type] = + tp.paramBounds map new InstPolyMap(tp, trefs) + val tparams = ctx.newTypeParams(sym, tp.paramNames, EmptyFlags, paramBounds) + (tparams, tp.instantiate(tparams map (_.typeConstructor))) case tp => (Nil, tp) } @@ -177,7 +172,7 @@ object TypedTrees { val parents = cls.info.parents map (TypeTree(_)) val selfType = if (cls.selfType eq cls.typeConstructor) EmptyValDef - else ValDef(ctx.newSymbol(cls, nme.THIS, SyntheticArtifact, cls.selfType)) + else ValDef(ctx.newSelfSym(cls)) def isOwnTypeParamAccessor(stat: TypedTree) = stat.symbol.owner == cls && (stat.symbol is TypeParam) val (tparamAccessors, rest) = body partition isOwnTypeParamAccessor diff --git a/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala b/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala index 5ac401fe7..2507d277b 100644 --- a/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala +++ b/src/dotty/tools/dotc/core/pickling/ClassfileParser.scala @@ -125,11 +125,7 @@ class ClassfileParser( instanceScope.lookup(nme.CONSTRUCTOR) == NoSymbol && !(sflags is Flags.Interface) if (needsConstructor) - instanceScope enter - cctx.newSymbol( - classRoot.symbol, - nme.CONSTRUCTOR, Flags.EmptyFlags, - MethodType(Nil, Nil)(_ => classRoot.typeConstructor)) + instanceScope enter cctx.newDefaultConstructor(classRoot.symbol.asClass) classInfo = parseAttributes(classRoot.symbol, classInfo) assignClassFields(classRoot, classInfo, classRoot.typeConstructor) -- cgit v1.2.3