Skip to content

Commit 992c72e

Browse files
committed
Make Modifiers untyped only.
The typed variant is no longer needed. This means modifiers can safely be ignored in typed trees if we so choose.
1 parent 517aafc commit 992c72e

File tree

4 files changed

+58
-63
lines changed

4 files changed

+58
-63
lines changed

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

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -33,51 +33,7 @@ object Trees {
3333
/** Attachment key for trees with documentation strings attached */
3434
val DocComment = new Attachment.Key[Comment]
3535

36-
/** Modifiers and annotations for definitions
37-
* @param flags The set flags
38-
* @param privateWithin If a private or protected has is followed by a
39-
* qualifier [q], the name q, "" as a typename otherwise.
40-
* @param annotations The annotations preceding the modifiers
41-
*/
42-
case class Modifiers[-T >: Untyped] (
43-
flags: FlagSet = EmptyFlags,
44-
privateWithin: TypeName = tpnme.EMPTY,
45-
annotations: List[Tree[T]] = Nil) extends Positioned with Cloneable {
46-
47-
def is(fs: FlagSet): Boolean = flags is fs
48-
def is(fc: FlagConjunction): Boolean = flags is fc
49-
50-
def | (fs: FlagSet): Modifiers[T] = withFlags(flags | fs)
51-
def & (fs: FlagSet): Modifiers[T] = withFlags(flags & fs)
52-
def &~(fs: FlagSet): Modifiers[T] = withFlags(flags &~ fs)
53-
54-
def toTypeFlags: Modifiers[T] = withFlags(flags.toTypeFlags)
55-
def toTermFlags: Modifiers[T] = withFlags(flags.toTermFlags)
56-
57-
def withFlags(flags: FlagSet) =
58-
if (this.flags == flags) this
59-
else copy(flags = flags)
60-
61-
def withAddedAnnotation[U >: Untyped <: T](annot: Tree[U]): Modifiers[U] =
62-
if (annotations.exists(_ eq annot)) this
63-
else withAnnotations(annotations :+ annot)
64-
65-
def withAnnotations[U >: Untyped <: T](annots: List[Tree[U]]): Modifiers[U] =
66-
if (annots eq annotations) this
67-
else copy(annotations = annots)
68-
69-
def withPrivateWithin(pw: TypeName) =
70-
if (pw.isEmpty) this
71-
else copy(privateWithin = pw)
72-
73-
def hasFlags = flags != EmptyFlags
74-
def hasAnnotations = annotations.nonEmpty
75-
def hasPrivateWithin = privateWithin != tpnme.EMPTY
76-
77-
def tokenPos: Seq[(Token, Position)] = ???
78-
}
79-
80-
@sharable private var nextId = 0 // for debugging
36+
@sharable private var nextId = 0 // for debugging
8137

8238
type LazyTree = AnyRef /* really: Tree | Lazy[Tree] */
8339
type LazyTreeList = AnyRef /* really: List[Tree] | Lazy[List[Tree]] */
@@ -320,27 +276,27 @@ object Trees {
320276
abstract class MemberDef[-T >: Untyped] extends NameTree[T] with DefTree[T] {
321277
type ThisTree[-T >: Untyped] <: MemberDef[T]
322278

323-
private[this] var myMods: Modifiers[T] = null
279+
private[this] var myMods: untpd.Modifiers = null
324280

325-
private[dotc] def rawMods: Modifiers[T] =
326-
if (myMods == null) genericEmptyModifiers else myMods
281+
private[dotc] def rawMods: untpd.Modifiers =
282+
if (myMods == null) untpd.EmptyModifiers else myMods
327283

328284
def rawComment: Option[Comment] = getAttachment(DocComment)
329285

330-
def withMods(mods: Modifiers[Untyped]): ThisTree[Untyped] = {
286+
def withMods(mods: untpd.Modifiers): ThisTree[Untyped] = {
331287
val tree = if (myMods == null || (myMods == mods)) this else clone.asInstanceOf[MemberDef[Untyped]]
332288
tree.setMods(mods)
333289
tree.asInstanceOf[ThisTree[Untyped]]
334290
}
335291

336-
def withFlags(flags: FlagSet): ThisTree[Untyped] = withMods(Modifiers(flags))
292+
def withFlags(flags: FlagSet): ThisTree[Untyped] = withMods(untpd.Modifiers(flags))
337293

338294
def setComment(comment: Option[Comment]): ThisTree[Untyped] = {
339295
comment.map(putAttachment(DocComment, _))
340296
asInstanceOf[ThisTree[Untyped]]
341297
}
342298

343-
protected def setMods(mods: Modifiers[T @uncheckedVariance]) = myMods = mods
299+
protected def setMods(mods: untpd.Modifiers) = myMods = mods
344300
}
345301

346302
/** A ValDef or DefDef tree */
@@ -727,16 +683,14 @@ object Trees {
727683
class EmptyValDef[T >: Untyped] extends ValDef[T](
728684
nme.WILDCARD, genericEmptyTree[T], genericEmptyTree[T]) with WithoutTypeOrPos[T] {
729685
override def isEmpty: Boolean = true
730-
setMods(Modifiers[T](PrivateLocal))
686+
setMods(untpd.Modifiers(PrivateLocal))
731687
}
732688

733689
@sharable val theEmptyTree: Thicket[Type] = Thicket(Nil)
734690
@sharable val theEmptyValDef = new EmptyValDef[Type]
735-
@sharable val theEmptyModifiers = new Modifiers()
736691

737692
def genericEmptyValDef[T >: Untyped]: ValDef[T] = theEmptyValDef.asInstanceOf[ValDef[T]]
738693
def genericEmptyTree[T >: Untyped]: Thicket[T] = theEmptyTree.asInstanceOf[Thicket[T]]
739-
def genericEmptyModifiers[T >: Untyped]: Modifiers[T] = theEmptyModifiers.asInstanceOf[Modifiers[T]]
740694

741695
def flatten[T >: Untyped](trees: List[Tree[T]]): List[Tree[T]] = {
742696
var buf: ListBuffer[Tree[T]] = null
@@ -795,7 +749,6 @@ object Trees {
795749

796750
abstract class Instance[T >: Untyped <: Type] extends DotClass { inst =>
797751

798-
type Modifiers = Trees.Modifiers[T]
799752
type Tree = Trees.Tree[T]
800753
type TypTree = Trees.TypTree[T]
801754
type TermTree = Trees.TermTree[T]
@@ -853,14 +806,9 @@ object Trees {
853806

854807
@sharable val EmptyTree: Thicket = genericEmptyTree
855808
@sharable val EmptyValDef: ValDef = genericEmptyValDef
856-
@sharable val EmptyModifiers: Modifiers = genericEmptyModifiers
857809

858810
// ----- Auxiliary creation methods ------------------
859811

860-
def Modifiers(flags: FlagSet = EmptyFlags,
861-
privateWithin: TypeName = tpnme.EMPTY,
862-
annotations: List[Tree] = Nil) = new Modifiers(flags, privateWithin, annotations)
863-
864812
def Thicket(trees: List[Tree]): Thicket = new Thicket(trees)
865813
def Thicket(): Thicket = EmptyTree
866814
def Thicket(x1: Tree, x2: Tree): Thicket = Thicket(x1 :: x2 :: Nil)

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,52 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
8686
*/
8787
class InfixOpBlock(leftOperand: Tree, rightOp: Tree) extends Block(leftOperand :: Nil, rightOp)
8888

89+
// ----- Modifiers -----------------------------------------------------
90+
91+
/** Modifiers and annotations for definitions
92+
* @param flags The set flags
93+
* @param privateWithin If a private or protected has is followed by a
94+
* qualifier [q], the name q, "" as a typename otherwise.
95+
* @param annotations The annotations preceding the modifiers
96+
*/
97+
case class Modifiers (
98+
flags: FlagSet = EmptyFlags,
99+
privateWithin: TypeName = tpnme.EMPTY,
100+
annotations: List[Tree] = Nil) extends Positioned with Cloneable {
101+
102+
def is(fs: FlagSet): Boolean = flags is fs
103+
def is(fc: FlagConjunction): Boolean = flags is fc
104+
105+
def | (fs: FlagSet): Modifiers = withFlags(flags | fs)
106+
def & (fs: FlagSet): Modifiers = withFlags(flags & fs)
107+
def &~(fs: FlagSet): Modifiers = withFlags(flags &~ fs)
108+
109+
def toTypeFlags: Modifiers = withFlags(flags.toTypeFlags)
110+
def toTermFlags: Modifiers = withFlags(flags.toTermFlags)
111+
112+
def withFlags(flags: FlagSet) =
113+
if (this.flags == flags) this
114+
else copy(flags = flags)
115+
116+
def withAddedAnnotation(annot: Tree): Modifiers =
117+
if (annotations.exists(_ eq annot)) this
118+
else withAnnotations(annotations :+ annot)
119+
120+
def withAnnotations(annots: List[Tree]): Modifiers =
121+
if (annots eq annotations) this
122+
else copy(annotations = annots)
123+
124+
def withPrivateWithin(pw: TypeName) =
125+
if (pw.isEmpty) this
126+
else copy(privateWithin = pw)
127+
128+
def hasFlags = flags != EmptyFlags
129+
def hasAnnotations = annotations.nonEmpty
130+
def hasPrivateWithin = privateWithin != tpnme.EMPTY
131+
}
132+
133+
@sharable val EmptyModifiers: Modifiers = new Modifiers()
134+
89135
// ----- TypeTrees that refer to other tree's symbols -------------------
90136

91137
/** A type tree that gets its type from some other tree's symbol. Enters the

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,8 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
729729
}
730730
}
731731
val mods =
732-
if (sym.annotations.isEmpty) EmptyModifiers
733-
else Modifiers(annotations = sym.annotations.map(_.tree))
732+
if (sym.annotations.isEmpty) untpd.EmptyModifiers
733+
else untpd.Modifiers(annotations = sym.annotations.map(_.tree))
734734
tree.withMods(mods) // record annotations in tree so that tree positions can be filled in.
735735
goto(end)
736736
setPos(start, tree)

src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import StdNames._, Denotations._, NameOps._, Flags._, Constants._, Annotations._
1212
import dotty.tools.dotc.typer.ProtoTypes.{FunProtoTyped, FunProto}
1313
import util.Positions._
1414
import dotty.tools.dotc.ast.{tpd, Trees, untpd}, ast.tpd._
15+
import ast.untpd.Modifiers
1516
import printing.Texts._
1617
import printing.Printer
1718
import io.AbstractFile
@@ -1236,7 +1237,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
12361237
val pflags = (pflagsHi.toLong << 32) + pflagsLo
12371238
val flags = unpickleScalaFlags(pflags, isType)
12381239
val privateWithin = readNameRef().asTypeName
1239-
Trees.Modifiers[Type](flags, privateWithin, Nil)
1240+
Modifiers(flags, privateWithin, Nil)
12401241
}
12411242

12421243
protected def readTemplateRef()(implicit ctx: Context): Template =

0 commit comments

Comments
 (0)