Skip to content

Commit 93d4c8c

Browse files
authored
Merge pull request #1539 from dotty-staging/drop-modifiers
Drop modifiers
2 parents ec28ea1 + 887a63a commit 93d4c8c

File tree

11 files changed

+111
-128
lines changed

11 files changed

+111
-128
lines changed

src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
7070
type Bind = tpd.Bind
7171
type New = tpd.New
7272
type Super = tpd.Super
73-
type Modifiers = tpd.Modifiers
73+
type Modifiers = Null
7474
type Annotation = Annotations.Annotation
7575
type ArrayValue = tpd.JavaSeqLiteral
7676
type ApplyDynamic = Null
@@ -944,7 +944,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
944944
}
945945

946946
object ValDef extends ValDefDeconstructor {
947-
def _1: Modifiers = field.mods
947+
def _1: Modifiers = null
948948
def _2: Name = field.name
949949
def _3: Tree = field.tpt
950950
def _4: Tree = field.rhs
@@ -1055,7 +1055,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
10551055
}
10561056

10571057
object DefDef extends DefDefDeconstructor {
1058-
def _1: Modifiers = field.mods
1058+
def _1: Modifiers = null
10591059
def _2: Name = field.name
10601060
def _3: List[TypeDef] = field.tparams
10611061
def _4: List[List[ValDef]] = field.vparamss
@@ -1081,7 +1081,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
10811081
}
10821082

10831083
object ClassDef extends ClassDefDeconstructor {
1084-
def _1: Modifiers = field.mods
1084+
def _1: Modifiers = null
10851085
def _2: Name = field.name
10861086
def _4: Template = field.rhs.asInstanceOf[Template]
10871087
def _3: List[TypeDef] = Nil

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,6 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
249249
/** Is this case guarded? */
250250
def isGuardedCase(cdef: CaseDef) = cdef.guard ne EmptyTree
251251

252-
/** True iff definition is a val or def with no right-hand-side, or it
253-
* is an abstract typoe declaration
254-
*/
255-
def lacksDefinition(mdef: MemberDef)(implicit ctx: Context) = mdef match {
256-
case mdef: ValOrDefDef =>
257-
mdef.unforcedRhs == EmptyTree && !mdef.name.isConstructorName && !mdef.mods.is(ParamAccessor)
258-
case mdef: TypeDef =>
259-
mdef.rhs.isEmpty || mdef.rhs.isInstanceOf[TypeBoundsTree]
260-
case _ => false
261-
}
262-
263252
/** The underlying pattern ignoring any bindings */
264253
def unbind(x: Tree): Tree = unsplice(x) match {
265254
case Bind(_, y) => unbind(y)
@@ -279,9 +268,21 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
279268

280269
trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped] =>
281270
import TreeInfo._
271+
import untpd._
272+
273+
/** True iff definition is a val or def with no right-hand-side, or it
274+
* is an abstract typoe declaration
275+
*/
276+
def lacksDefinition(mdef: MemberDef)(implicit ctx: Context) = mdef match {
277+
case mdef: ValOrDefDef =>
278+
mdef.unforcedRhs == EmptyTree && !mdef.name.isConstructorName && !mdef.mods.is(ParamAccessor)
279+
case mdef: TypeDef =>
280+
mdef.rhs.isEmpty || mdef.rhs.isInstanceOf[TypeBoundsTree]
281+
case _ => false
282+
}
282283

283284
def isFunctionWithUnknownParamType(tree: Tree) = tree match {
284-
case untpd.Function(args, _) =>
285+
case Function(args, _) =>
285286
args.exists {
286287
case ValDef(_, tpt, _) => tpt.isEmpty
287288
case _ => false
@@ -307,7 +308,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
307308
| DefDef(_, _, _, _, _) =>
308309
Pure
309310
case vdef @ ValDef(_, _, _) =>
310-
if (vdef.mods is Mutable) Impure else exprPurity(vdef.rhs)
311+
if (vdef.symbol.flags is Mutable) Impure else exprPurity(vdef.rhs)
311312
case _ =>
312313
Impure
313314
}

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

Lines changed: 22 additions & 65 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,41 @@ 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[ast] 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
300+
301+
/** The position of the name defined by this definition.
302+
* This is a point position if the definition is synthetic, or a range position
303+
* if the definition comes from source.
304+
* It might also be that the definition does not have a position (for instance when synthesized by
305+
* a calling chain from `viewExists`), in that case the return position is NoPosition.
306+
*/
307+
def namePos =
308+
if (pos.exists)
309+
if (rawMods.is(Synthetic)) Position(pos.point, pos.point)
310+
else Position(pos.point, pos.point + name.length, pos.point)
311+
else pos
312+
313+
344314
}
345315

346316
/** A ValDef or DefDef tree */
@@ -727,16 +697,14 @@ object Trees {
727697
class EmptyValDef[T >: Untyped] extends ValDef[T](
728698
nme.WILDCARD, genericEmptyTree[T], genericEmptyTree[T]) with WithoutTypeOrPos[T] {
729699
override def isEmpty: Boolean = true
730-
setMods(Modifiers[T](PrivateLocal))
700+
setMods(untpd.Modifiers(PrivateLocal))
731701
}
732702

733703
@sharable val theEmptyTree: Thicket[Type] = Thicket(Nil)
734704
@sharable val theEmptyValDef = new EmptyValDef[Type]
735-
@sharable val theEmptyModifiers = new Modifiers()
736705

737706
def genericEmptyValDef[T >: Untyped]: ValDef[T] = theEmptyValDef.asInstanceOf[ValDef[T]]
738707
def genericEmptyTree[T >: Untyped]: Thicket[T] = theEmptyTree.asInstanceOf[Thicket[T]]
739-
def genericEmptyModifiers[T >: Untyped]: Modifiers[T] = theEmptyModifiers.asInstanceOf[Modifiers[T]]
740708

741709
def flatten[T >: Untyped](trees: List[Tree[T]]): List[Tree[T]] = {
742710
var buf: ListBuffer[Tree[T]] = null
@@ -795,7 +763,6 @@ object Trees {
795763

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

798-
type Modifiers = Trees.Modifiers[T]
799766
type Tree = Trees.Tree[T]
800767
type TypTree = Trees.TypTree[T]
801768
type TermTree = Trees.TermTree[T]
@@ -853,14 +820,9 @@ object Trees {
853820

854821
@sharable val EmptyTree: Thicket = genericEmptyTree
855822
@sharable val EmptyValDef: ValDef = genericEmptyValDef
856-
@sharable val EmptyModifiers: Modifiers = genericEmptyModifiers
857823

858824
// ----- Auxiliary creation methods ------------------
859825

860-
def Modifiers(flags: FlagSet = EmptyFlags,
861-
privateWithin: TypeName = tpnme.EMPTY,
862-
annotations: List[Tree] = Nil) = new Modifiers(flags, privateWithin, annotations)
863-
864826
def Thicket(trees: List[Tree]): Thicket = new Thicket(trees)
865827
def Thicket(): Thicket = EmptyTree
866828
def Thicket(x1: Tree, x2: Tree): Thicket = Thicket(x1 :: x2 :: Nil)
@@ -870,11 +832,6 @@ object Trees {
870832
case ys => Thicket(ys)
871833
}
872834

873-
// ----- Accessing modifiers ----------------------------------------------------
874-
875-
abstract class ModsDeco { def mods: Modifiers }
876-
implicit def modsDeco(mdef: MemberDef)(implicit ctx: Context): ModsDeco
877-
878835
// ----- Helper classes for copying, transforming, accumulating -----------------
879836

880837
val cpy: TreeCopier

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
1919

2020
private def ta(implicit ctx: Context) = ctx.typeAssigner
2121

22-
def Modifiers(sym: Symbol)(implicit ctx: Context): Modifiers = Modifiers(
23-
sym.flags & (if (sym.isType) ModifierFlags | VarianceFlags else ModifierFlags),
24-
if (sym.privateWithin.exists) sym.privateWithin.asType.name else tpnme.EMPTY,
25-
sym.annotations map (_.tree))
26-
2722
def Ident(tp: NamedType)(implicit ctx: Context): Ident =
2823
ta.assignType(untpd.Ident(tp.name), tp)
2924

@@ -446,10 +441,6 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
446441
} else foldOver(sym, tree)
447442
}
448443

449-
implicit class modsDeco(mdef: MemberDef)(implicit ctx: Context) extends ModsDeco {
450-
def mods = if (mdef.hasType) Modifiers(mdef.symbol) else mdef.rawMods
451-
}
452-
453444
override val cpy = new TypedTreeCopier
454445

455446
class TypedTreeCopier extends TreeCopier {

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

Lines changed: 49 additions & 14 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
@@ -264,22 +310,11 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
264310
/** A repeated argument such as `arg: _*` */
265311
def repeated(arg: Tree)(implicit ctx: Context) = Typed(arg, Ident(tpnme.WILDCARD_STAR))
266312

267-
// ------- Decorators -------------------------------------------------
313+
// ----- Accessing modifiers ----------------------------------------------------
268314

269-
implicit class UntypedTreeDecorator(val self: Tree) extends AnyVal {
270-
def locateEnclosing(base: List[Tree], pos: Position): List[Tree] = {
271-
def encloses(elem: Any) = elem match {
272-
case t: Tree => t.pos contains pos
273-
case _ => false
274-
}
275-
base.productIterator find encloses match {
276-
case Some(tree: Tree) => locateEnclosing(tree :: base, pos)
277-
case none => base
278-
}
279-
}
280-
}
315+
abstract class ModsDecorator { def mods: Modifiers }
281316

282-
implicit class modsDeco(val mdef: MemberDef)(implicit ctx: Context) extends ModsDeco {
317+
implicit class modsDeco(val mdef: MemberDef)(implicit ctx: Context) {
283318
def mods = mdef.rawMods
284319
}
285320

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,12 @@ 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))
734-
tree.withMods(mods) // record annotations in tree so that tree positions can be filled in.
732+
if (sym.annotations.isEmpty) untpd.EmptyModifiers
733+
else untpd.Modifiers(annotations = sym.annotations.map(_.tree))
734+
tree.withMods(mods)
735+
// record annotations in tree so that tree positions can be filled in.
736+
// Note: Once the inline PR with its changes to positions is in, this should be
737+
// no longer necessary.
735738
goto(end)
736739
setPos(start, tree)
737740
}

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 =

src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,15 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
161161
import untpd.{modsDeco => _, _}
162162

163163
/** Print modifiers from symbols if tree has type, overriding the untpd behavior. */
164-
implicit def modsDeco(mdef: untpd.MemberDef)(implicit ctx: Context): untpd.ModsDeco =
165-
tpd.modsDeco(mdef.asInstanceOf[tpd.MemberDef]).asInstanceOf[untpd.ModsDeco]
164+
implicit def modsDeco(mdef: untpd.MemberDef)(implicit ctx: Context): untpd.ModsDecorator =
165+
new untpd.ModsDecorator {
166+
def mods = if (mdef.hasType) Modifiers(mdef.symbol) else mdef.rawMods
167+
}
168+
169+
def Modifiers(sym: Symbol)(implicit ctx: Context): Modifiers = untpd.Modifiers(
170+
sym.flags & (if (sym.isType) ModifierFlags | VarianceFlags else ModifierFlags),
171+
if (sym.privateWithin.exists) sym.privateWithin.asType.name else tpnme.EMPTY,
172+
sym.annotations map (_.tree))
166173

167174
def isLocalThis(tree: Tree) = tree.typeOpt match {
168175
case tp: ThisType => tp.cls == ctx.owner.enclosingClass

0 commit comments

Comments
 (0)