Skip to content

Commit e8366c0

Browse files
committed
Added new utility methods to SymDenotations and refactored creation.
1 parent dc137d3 commit e8366c0

File tree

6 files changed

+105
-24
lines changed

6 files changed

+105
-24
lines changed

src/dotty/tools/dotc/core/Denotations.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ object Denotations {
355355

356356
// ------ DenotationSet ops ----------------------------------------------
357357

358+
def first = this
358359
def toDenot(implicit ctx: Context) = this
359360
def containsSig(sig: Signature)(implicit ctx: Context) =
360361
signature == sig
@@ -395,6 +396,7 @@ object Denotations {
395396
*/
396397
trait DenotationSet {
397398
def exists: Boolean
399+
def first: Denotation
398400
def toDenot(implicit ctx: Context): Denotation
399401
def containsSig(sig: Signature)(implicit ctx: Context): Boolean
400402
def filterDisjoint(denots: DenotationSet)(implicit ctx: Context): DenotationSet
@@ -415,6 +417,7 @@ object Denotations {
415417
else if ((s1 eq denots2) && (s2 eq denots2)) this
416418
else new DenotUnion(s1, s2)
417419
def exists = true
420+
def first = denots1.first
418421
def toDenot(implicit ctx: Context) = denots1.toDenot & denots2.toDenot
419422
def containsSig(sig: Signature)(implicit ctx: Context) =
420423
(denots1 containsSig sig) || (denots2 containsSig sig)

src/dotty/tools/dotc/core/Flags.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ object Flags {
9595
private def flagString(idx: Int): Set[String] =
9696
kindIndices.map(flagName(idx)).filterNot(_.isEmpty)
9797

98+
def flagStrings: Seq[String] = (2 to MaxFlag).flatMap(flagString)
99+
98100
/** The string representation of this flag set */
99-
override def toString =
100-
(2 to MaxFlag).flatMap(flagString).mkString(" ")
101+
override def toString = flagStrings.mkString(" ")
101102
}
102103

103104
/** A class representing flag sets that should be tested
@@ -173,7 +174,7 @@ object Flags {
173174
/** Labeled with `final` modifier */
174175
final val Final = commonFlag(6, "final")
175176

176-
/** A method. !!! needed? */
177+
/** A method symbol. */
177178
final val Method = termFlag(7, "<method>")
178179

179180
/** Labeled with `abstract` modifier (an abstract class) */
@@ -368,12 +369,21 @@ object Flags {
368369
* that sets the type parameter */
369370
final val RetainedTypeArgFlags = Covariant | Contravariant | Protected | Local
370371

372+
/** A type parameter with synthesized name */
373+
final val ExpandedTypeParam = allOf(ExpandedName, TypeParam)
374+
375+
/** A Java interface */
376+
final val JavaInterface = allOf(JavaDefined, Trait)
377+
371378
/** Labeled private[this] */
372379
final val PrivateLocal = allOf(Private, Local)
373380

374381
/** Labeled protected[this] */
375382
final val ProtectedLocal = allOf(Protected, Local)
376383

384+
/** The flags of a type parameter */
385+
final val TypeParamFlags = Protected | Local
386+
377387
/** Labeled `private` or `protected[local]` */
378388
final val PrivateOrLocal = oneOf(Private, Local)
379389

src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object NameOps {
4949
implicit class NameDecorator(val name: Name) extends AnyVal {
5050
import nme._
5151

52-
def isConstructorName = name == CONSTRUCTOR || name == MIXIN_CONSTRUCTOR
52+
def isConstructorName = name == CONSTRUCTOR || name == TRAIT_CONSTRUCTOR
5353
def isExceptionResultName = name startsWith EXCEPTION_RESULT_PREFIX
5454
def isImplClassName = name endsWith IMPL_CLASS_SUFFIX
5555
def isLocalDummyName = name startsWith LOCALDUMMY_PREFIX
@@ -159,6 +159,11 @@ object NameOps {
159159
*/
160160
def expandedName(base: Symbol, separator: Name = nme.EXPAND_SEPARATOR)(implicit ctx: Context): TypeName =
161161
(base.fullName('$') ++ separator ++ name).toTypeName
162+
163+
def unexpandedName(separator: Name = nme.EXPAND_SEPARATOR) = {
164+
val idx = name.lastIndexOfSlice(separator)
165+
if (idx < 0) name else name drop (idx + separator.length)
166+
}
162167
}
163168

164169
implicit class TermNameDecorator(val name: TermName) extends AnyVal {

src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ object StdNames {
228228
val REIFY_FREE_THIS_SUFFIX: N = "$this"
229229
val REIFY_FREE_VALUE_SUFFIX: N = "$value"
230230
val REIFY_SYMDEF_PREFIX: N = "symdef$"
231-
val MIXIN_CONSTRUCTOR: N = "$init$"
231+
val TRAIT_CONSTRUCTOR: N = "$init$"
232232
val MODULE_INSTANCE_FIELD: N = NameTransformer.MODULE_INSTANCE_NAME // "MODULE$"
233233
val OUTER: N = "$outer"
234234
val OUTER_LOCAL: N = "$outer "

src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33

44
import Periods._, Contexts._, Symbols._, Denotations._, Names._, Annotations._
55
import Types._, Flags._, Decorators._, Transformers._, StdNames._, Scopes._
6+
import NameOps._
67
import Scopes.Scope
78
import collection.mutable
89
import collection.immutable.BitSet
@@ -217,6 +218,28 @@ object SymDenotations {
217218
/** Is this a subclass of the given class `base`? */
218219
def isSubClass(base: Symbol)(implicit ctx: Context) = false
219220

221+
/** Is this a setter? */
222+
def isGetter = (this is Accessor) && !originalName.isSetterName
223+
224+
/** Is this a user defined "def" method? Excluded are accessors and stable values */
225+
def isSourceMethod = this is (Method, butNot = Accessor | Stable)
226+
227+
/** Is this a setter? */
228+
def isSetter = (this is Accessor) && originalName.isSetterName
229+
230+
/** is this the constructor of a class? */
231+
def isClassConstructor = name == nme.CONSTRUCTOR
232+
233+
/** Is this the constructor of a trait? */
234+
def isTraitConstructor = name == nme.TRAIT_CONSTRUCTOR
235+
236+
/** Is this the constructor of a trait or a class */
237+
def isConstructor = name.isConstructorName
238+
239+
/** Does this symbol denote the primary constructor of its enclosing class? */
240+
final def isPrimaryConstructor(implicit ctx: Context) =
241+
isConstructor && owner.primaryConstructor == this
242+
220243
/** Is this a subclass of `base`,
221244
* and is the denoting symbol also different from `Null` or `Nothing`?
222245
*/
@@ -433,6 +456,9 @@ object SymDenotations {
433456
else defn.RootClass
434457
}
435458

459+
/** The primary constructor of a class or trait, NoSymbol if not applicable. */
460+
def primaryConstructor(implicit ctx: Context): Symbol = NoSymbol
461+
436462
// ----- type-related ------------------------------------------------
437463

438464
/** The type parameters of a class symbol, Nil for all other symbols */
@@ -441,13 +467,19 @@ object SymDenotations {
441467
/** The type This(cls), where cls is this class, NoPrefix for all other symbols */
442468
def thisType(implicit ctx: Context): Type = NoPrefix
443469

444-
/** The type representing the type constructor for this type.
470+
/** The named typeref representing the type constructor for this type.
445471
* @throws ClassCastException is this is not a type
446472
*/
447473
def typeConstructor(implicit ctx: Context): TypeRef =
448-
if (isPackageClass) TypeRef(owner.thisType, symbol.asType)
474+
if (isPackageClass) symbolicRef
449475
else TypeRef(owner.thisType, name.asTypeName)
450476

477+
/** The symbolic typeref representing the type constructor for this type.
478+
* @throws ClassCastException is this is not a type
479+
*/
480+
def symbolicRef(implicit ctx: Context): TypeRef =
481+
TypeRef(owner.thisType, symbol.asType)
482+
451483
/** The variance of this type parameter as an Int, with
452484
* +1 = Covariant, -1 = Contravariant, 0 = Nonvariant, or not a type parameter
453485
*/
@@ -468,7 +500,7 @@ object SymDenotations {
468500
initFlags: FlagSet = this.flags,
469501
privateWithin: Symbol = this.privateWithin,
470502
info: Type = this.info) =
471-
new CompleteSymDenotation(sym, owner, name, initFlags, privateWithin, info)
503+
CompleteSymDenotation(sym, owner, name, initFlags, info, privateWithin)
472504
}
473505

474506
/** The contents of a class definition during a period
@@ -495,6 +527,8 @@ object SymDenotations {
495527
/** The symbols defined directly in this class */
496528
def decls: Scope
497529

530+
def name: TypeName
531+
498532
override val info = {
499533
implicit val ctx = initctx
500534
ClassInfo(owner.thisType, this)
@@ -752,17 +786,23 @@ object SymDenotations {
752786
fn
753787
}
754788

789+
override def primaryConstructor(implicit ctx: Context): Symbol = {
790+
val cname =
791+
if (this is Trait | ImplClass) nme.TRAIT_CONSTRUCTOR else nme.CONSTRUCTOR
792+
decls.denotsNamed(cname).first.symbol
793+
}
794+
755795
def copyClass(
756796
sym: ClassSymbol,
757797
owner: Symbol = this.owner,
758-
name: Name = this.name,
798+
name: TypeName = this.name,
759799
initFlags: FlagSet = this.flags,
760800
privateWithin: Symbol = this.privateWithin,
761801
parents: List[TypeRef] = this.parents,
762802
selfType: Type = this.selfType,
763803
decls: Scope = this.decls,
764804
assocFile: AbstractFile = this.assocFile)(implicit ctx: Context) =
765-
new CompleteClassDenotation(sym, owner, name, initFlags, privateWithin, parents, selfType, decls, assocFile)(ctx)
805+
new CompleteClassDenotation(sym, owner, name, initFlags, parents, privateWithin, selfType, decls, assocFile)(ctx)
766806
}
767807

768808
// -------- Concrete classes for instantiating denotations --------------------------
@@ -772,10 +812,14 @@ object SymDenotations {
772812
val owner: Symbol,
773813
val name: Name,
774814
initFlags: FlagSet,
775-
val privateWithin: Symbol,
776-
val info: Type
815+
val info: Type,
816+
val privateWithin: Symbol
777817
) extends SymDenotation(initFlags)
778818

819+
def CompleteSymDenotation(symbol: Symbol, owner: Symbol, name: Name, initFlags: FlagSet,
820+
info: Type, privateWithin: Symbol = NoSymbol) =
821+
new CompleteSymDenotation(symbol, owner, name, initFlags, info, privateWithin)
822+
779823
class LazySymDenotation(
780824
val symbol: Symbol,
781825
val owner: Symbol,
@@ -792,28 +836,41 @@ object SymDenotations {
792836
override def info = { if (info == null) tryComplete(); _info }
793837
}
794838

839+
def LazySymDenotation(symbol: Symbol, owner: Symbol, name: Name, initFlags: FlagSet,
840+
completer: SymCompleter) =
841+
new LazySymDenotation(symbol, owner, name, initFlags, completer)
842+
795843
class CompleteClassDenotation(
796844
val symbol: ClassSymbol,
797845
val owner: Symbol,
798-
val name: Name,
846+
val name: TypeName,
799847
initFlags: FlagSet,
800-
val privateWithin: Symbol,
801848
val parents: List[TypeRef],
849+
val privateWithin: Symbol,
802850
optSelfType: Type,
803851
val decls: Scope,
804-
assocFile: AbstractFile = null
805-
)(initctx: Context) extends ClassDenotation(initFlags, assocFile)(initctx) {
806-
val selfType = if (optSelfType == NoType) thisType(initctx) else optSelfType
852+
assocFile: AbstractFile)(initctx: Context)
853+
extends ClassDenotation(initFlags, assocFile)(initctx) {
854+
val selfType = if (optSelfType == NoType) typeConstructor(initctx) else optSelfType
807855
final def preCompleteDecls = decls
808856
}
809857

858+
def CompleteClassDenotation(
859+
symbol: ClassSymbol, owner: Symbol, name: TypeName, initFlags: FlagSet, parents: List[TypeRef],
860+
privateWithin: Symbol = NoSymbol,
861+
optSelfType: Type = NoType,
862+
decls: Scope = newScope,
863+
assocFile: AbstractFile = null)(implicit ctx: Context) =
864+
new CompleteClassDenotation(symbol, owner, name, initFlags, parents,
865+
privateWithin, optSelfType, decls, assocFile)(ctx)
866+
810867
class LazyClassDenotation(
811868
val symbol: ClassSymbol,
812869
val owner: Symbol,
813-
val name: Name,
870+
val name: TypeName,
814871
initFlags: FlagSet,
815872
var completer: ClassCompleter,
816-
assocFile: AbstractFile = null
873+
assocFile: AbstractFile
817874
)(initctx: Context) extends ClassDenotation(initFlags, assocFile)(initctx) with isLazy[LazyClassDenotation] {
818875

819876
private[this] var _parents: List[TypeRef] = null
@@ -832,6 +889,11 @@ object SymDenotations {
832889
final override def exists(implicit ctx: Context) = { ensureCompleted(); _parents != null }
833890
}
834891

892+
def LazyClassDenotation(
893+
symbol: ClassSymbol, owner: Symbol, name: TypeName, initFlags: FlagSet,
894+
completer: ClassCompleter, assocFile: AbstractFile = null)(implicit ctx: Context) =
895+
new LazyClassDenotation(symbol, owner, name, initFlags, completer, assocFile)(ctx)
896+
835897
object NoDenotation extends SymDenotation(EmptyFlags) {
836898
override def isTerm = false
837899
override def isType = false
@@ -923,7 +985,7 @@ object SymDenotations {
923985
case denot: LazyClassDenotation =>
924986
denot.privateWithin = NoSymbol
925987
denot.parents = Nil
926-
denot.selfType = denot.thisType
988+
denot.selfType = denot.typeConstructor
927989
denot.decls = EmptyScope
928990
}
929991

src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ trait Symbols { this: Context =>
4747
newLazyModuleSymbols(owner, name, PackageCreationFlags, completer)
4848

4949
def newSymbol[N <: Name](owner: Symbol, name: N, flags: FlagSet, info: Type, privateWithin: Symbol = NoSymbol) =
50-
new Symbol(new CompleteSymDenotation(_, owner, name, flags, privateWithin, info)) {
50+
new Symbol(CompleteSymDenotation(_, owner, name, flags, info, privateWithin)) {
5151
type ThisName = N
5252
}
5353

@@ -62,7 +62,7 @@ trait Symbols { this: Context =>
6262
assocFile: AbstractFile = null)
6363
=
6464
new ClassSymbol(new CompleteClassDenotation(
65-
_, owner, name, flags, privateWithin, parents, optSelfType, decls, assocFile)(this))
65+
_, owner, name, flags, parents, privateWithin, optSelfType, decls, assocFile)(this))
6666

6767
def newModuleSymbols(
6868
owner: Symbol,
@@ -166,7 +166,7 @@ object Symbols {
166166

167167
def show(implicit ctx: Context): String = ctx.show(this)
168168
def showLocated(implicit ctx: Context): String = ctx.showLocated(this)
169-
def showDef(implicit ctx: Context): String = ctx.showDef(this)
169+
def showDcl(implicit ctx: Context): String = ctx.showDcl(this)
170170
def showKind(implicit tcx: Context): String = ???
171171
def showName(implicit ctx: Context): String = ???
172172
}
@@ -186,7 +186,8 @@ object Symbols {
186186
def superId(implicit ctx: Context): Int = {
187187
val hint = superIdHint
188188
val key = this.typeConstructor
189-
if (hint >= 0 && hint <= ctx.lastSuperId && (ctx.classOfId(hint) eq key)) hint
189+
if (hint >= 0 && hint <= ctx.lastSuperId && (ctx.classOfId(hint) eq key))
190+
hint
190191
else {
191192
val id = ctx.superIdOfClass get key match {
192193
case Some(id) =>

0 commit comments

Comments
 (0)