Skip to content

Commit 112f39a

Browse files
committed
Use FlagSet directly instead of defKind
Instead of classifying class members with a TreeInfo.DefKind, we use directly the flags into which kinds were previously translated.
1 parent 4eb1072 commit 112f39a

File tree

5 files changed

+33
-38
lines changed

5 files changed

+33
-38
lines changed

src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
2525
case _ => false
2626
}
2727

28-
/** Does tree contain an initialization part when seen as a member of a class or trait?
28+
/** The largest subset of {NoInits, PureInterface} that a
29+
* trait enclosing this statement can have as flags.
30+
* Does tree contain an initialization part when seen as a member of a class or trait?
2931
*/
30-
def defKind(tree: Tree): DefKind = unsplice(tree) match {
31-
case EmptyTree | _: Import => InterfaceDef
32-
case tree: TypeDef => if (tree.isClassDef) NoInitDef else InterfaceDef
33-
case tree: DefDef => if (tree.unforcedRhs == EmptyTree) InterfaceDef else NoInitDef
34-
case tree: ValDef => if (tree.unforcedRhs == EmptyTree) InterfaceDef else GeneralDef
35-
case _ => GeneralDef
32+
def defKind(tree: Tree): FlagSet = unsplice(tree) match {
33+
case EmptyTree | _: Import => NoInitsInterface
34+
case tree: TypeDef => if (tree.isClassDef) NoInits else NoInitsInterface
35+
case tree: DefDef => if (tree.unforcedRhs == EmptyTree) NoInitsInterface else NoInits
36+
case tree: ValDef => if (tree.unforcedRhs == EmptyTree) NoInitsInterface else EmptyFlags
37+
case _ => EmptyFlags
3638
}
3739

3840
def isOpAssign(tree: Tree) = unsplice(tree) match {
@@ -517,7 +519,6 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
517519
}
518520

519521
object TreeInfo {
520-
521522
class PurityLevel(val x: Int) extends AnyVal {
522523
def >= (that: PurityLevel) = x >= that.x
523524
def min(that: PurityLevel) = new PurityLevel(x min that.x)
@@ -526,15 +527,6 @@ object TreeInfo {
526527
val Pure = new PurityLevel(2)
527528
val Idempotent = new PurityLevel(1)
528529
val Impure = new PurityLevel(0)
529-
530-
case class DefKind(val x: Int) extends AnyVal {
531-
def >= (that: DefKind) = x >= that.x
532-
def min(that: DefKind) = new DefKind(x min that.x)
533-
}
534-
535-
val InterfaceDef = new DefKind(2)
536-
val NoInitDef = new DefKind(1)
537-
val GeneralDef = new DefKind(0)
538530
}
539531

540532
/** a Match(Typed(_, tpt), _) must be translated into a switch if isSwitchAnnotation(tpt.tpe)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ object Flags {
467467
/** Pure interfaces always have these flags */
468468
final val PureInterfaceCreationFlags = Trait | NoInits | PureInterface
469469

470+
final val NoInitsInterface = NoInits | PureInterface
471+
470472
/** The flags of the self symbol */
471473
final val SelfSymFlags = Private | Local | Deferred
472474

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ object SymDenotations {
115115
/** Unset given flags(s) of this denotation */
116116
final def resetFlag(flags: FlagSet): Unit = { myFlags &~= flags }
117117

118-
final def setFlagsFromDefKind(kind: TreeInfo.DefKind): Unit =
119-
if (kind >= TreeInfo.NoInitDef) {
120-
setFlag(NoInits)
121-
if (kind == TreeInfo.InterfaceDef && myFlags.is(Trait)) setFlag(PureInterface)
122-
}
118+
/** Set applicable flags from `flags` which is a subset of {NoInits, PureInterface} */
119+
final def setApplicableFlags(flags: FlagSet): Unit = {
120+
val mask = if (myFlags.is(Trait)) NoInitsInterface else NoInits
121+
setFlag(flags & mask)
122+
}
123123

124124
/** Has this denotation one of the flags in `fs` set? */
125125
final def is(fs: FlagSet)(implicit ctx: Context) = {

src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Contexts._, Symbols._, Types._, Scopes._, SymDenotations._, Names._, Name
77
import StdNames._, Denotations._, Flags._, Constants._, Annotations._
88
import util.Positions._
99
import ast.{tpd, Trees, untpd}
10-
import ast.TreeInfo._
1110
import Trees._
1211
import Decorators._
1312
import TastyUnpickler._, TastyBuffer._, PositionPickler._
@@ -351,9 +350,10 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
351350
}
352351

353352
/** Create symbol of definition node and enter in symAtAddr map
354-
* @return true iff the definition does not contain initialization code
353+
* @return the largest subset of {NoInits, PureInterface} that a
354+
* trait owning this symbol can have as flags.
355355
*/
356-
def createSymbol()(implicit ctx: Context): DefKind = {
356+
def createSymbol()(implicit ctx: Context): FlagSet = {
357357
val start = currentAddr
358358
val tag = readByte()
359359
val end = readEnd()
@@ -409,10 +409,10 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
409409
sym.completer.withDecls(newScope)
410410
forkAt(templateStart).indexTemplateParams()(localContext(sym))
411411
}
412-
if (isClass) NoInitDef
413-
else if (sym.isType || sym.isConstructor || flags.is(Deferred)) InterfaceDef
414-
else if (tag == VALDEF) GeneralDef
415-
else NoInitDef
412+
if (isClass) NoInits
413+
else if (sym.isType || sym.isConstructor || flags.is(Deferred)) NoInitsInterface
414+
else if (tag == VALDEF) EmptyFlags
415+
else NoInits
416416
}
417417

418418
/** Read modifier list into triplet of flags, annotations and a privateWithin
@@ -476,25 +476,26 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
476476

477477
/** Create symbols for a definitions in statement sequence between
478478
* current address and `end`.
479-
* @return true iff none of the statements contains initialization code
479+
* @return the largest subset of {NoInits, PureInterface} that a
480+
* trait owning the indexed statements can have as flags.
480481
*/
481-
def indexStats(end: Addr)(implicit ctx: Context): DefKind = {
482-
val defKinds =
482+
def indexStats(end: Addr)(implicit ctx: Context): FlagSet = {
483+
val flagss =
483484
until(end) {
484485
nextByte match {
485486
case VALDEF | DEFDEF | TYPEDEF | TYPEPARAM | PARAM =>
486487
createSymbol()
487488
case IMPORT =>
488489
skipTree()
489-
InterfaceDef
490+
NoInitsInterface
490491
case PACKAGE =>
491492
processPackage { (pid, end) => implicit ctx => indexStats(end) }
492493
case _ =>
493494
skipTree()
494-
GeneralDef
495+
EmptyFlags
495496
}
496497
}
497-
(InterfaceDef /: defKinds)(_ min _)
498+
(NoInitsInterface /: flagss)(_ & _)
498499
}
499500

500501
/** Process package with given operation `op`. The operation takes as arguments
@@ -635,7 +636,7 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
635636
}
636637
else EmptyValDef
637638
setClsInfo(parentRefs, if (self.isEmpty) NoType else self.tpt.tpe)
638-
cls.setFlagsFromDefKind(fork.indexStats(end))
639+
cls.setApplicableFlags(fork.indexStats(end))
639640
val constr = readIndexedDef().asInstanceOf[DefDef]
640641

641642
def mergeTypeParamsAndAliases(tparams: List[TypeDef], stats: List[Tree]): (List[Tree], List[Tree]) =

src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ class Namer { typer: Typer =>
563563

564564
index(rest)(inClassContext(selfInfo))
565565
denot.info = ClassInfo(cls.owner.thisType, cls, parentRefs, decls, selfInfo)
566-
cls.setFlagsFromDefKind(
567-
(TreeInfo.InterfaceDef /: impl.body)((kind, stat) => kind min defKind(stat)))
566+
cls.setApplicableFlags(
567+
(NoInitsInterface /: impl.body)((fs, stat) => fs & defKind(stat)))
568568
}
569569
}
570570

0 commit comments

Comments
 (0)