Skip to content

Commit a8f11ef

Browse files
committed
change TastyFlags to AnyVal
1 parent 7325a37 commit a8f11ef

File tree

3 files changed

+52
-93
lines changed

3 files changed

+52
-93
lines changed

src/compiler/scala/tools/nsc/tasty/TastyFlags.scala

+25-85
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,46 @@ package scala.tools.nsc.tasty
22

33
/**Flags from TASTy with no equivalent in scalac
44
*/
5-
trait TastyFlags { self =>
6-
type TastyFlagSet
7-
type SingletonSets[_]
8-
9-
val EmptyFlags: TastyFlagSet
10-
val Erased: TastyFlagSet
11-
val Internal: TastyFlagSet
12-
val Inline: TastyFlagSet
13-
val InlineProxy: TastyFlagSet
14-
val Opaque: TastyFlagSet
15-
val Scala2x: TastyFlagSet
16-
val Extension: TastyFlagSet
17-
val Given: TastyFlagSet
18-
val Exported: TastyFlagSet
19-
val NoInits: TastyFlagSet
20-
21-
protected def union(as: TastyFlagSet, bs: TastyFlagSet): TastyFlagSet
22-
protected def intersect(as: TastyFlagSet, bs: TastyFlagSet): TastyFlagSet
23-
protected def equal(set: TastyFlagSet, other: TastyFlagSet): Boolean
24-
protected def remove(set: TastyFlagSet, mask: TastyFlagSet): TastyFlagSet
25-
protected def toSingletonSets(multiset: TastyFlagSet): SingletonSets[TastyFlagSet]
26-
protected def map[A](set: SingletonSets[TastyFlagSet], f: TastyFlagSet => A): Iterable[A]
27-
}
28-
295
object TastyFlags {
306

31-
type TastyFlagSet = Live.TastyFlagSet
32-
type SingletonSets[T] = Live.SingletonSets[T]
33-
34-
implicit final class TastyFlagSetAPI(private val flagset: TastyFlagSet) extends AnyVal {
35-
36-
def toSingletonSets: SingletonSets[TastyFlagSet] = Live.toSingletonSets(flagset)
37-
def |(other: TastyFlagSet): TastyFlagSet = Live.union(flagset, other)
38-
def &(mask: TastyFlagSet): TastyFlagSet = Live.intersect(flagset, mask)
39-
def ===(set: TastyFlagSet): Boolean = Live.equal(flagset, set)
40-
def &~(mask: TastyFlagSet): TastyFlagSet = Live.remove(flagset, mask)
41-
def unary_! : Boolean = flagset === Live.EmptyFlags
42-
def is(mask: TastyFlagSet): Boolean = (flagset & mask).hasFlags
7+
val EmptyFlags: TastyFlagSet = TastyFlagSet(0)
8+
val Erased: TastyFlagSet = TastyFlagSet(1 << 0)
9+
val Internal: TastyFlagSet = TastyFlagSet(1 << 1)
10+
val Inline: TastyFlagSet = TastyFlagSet(1 << 2)
11+
val InlineProxy: TastyFlagSet = TastyFlagSet(1 << 3)
12+
val Opaque: TastyFlagSet = TastyFlagSet(1 << 4)
13+
val Scala2x: TastyFlagSet = TastyFlagSet(1 << 5)
14+
val Extension: TastyFlagSet = TastyFlagSet(1 << 6)
15+
val Given: TastyFlagSet = TastyFlagSet(1 << 7)
16+
val Exported: TastyFlagSet = TastyFlagSet(1 << 8)
17+
val NoInits: TastyFlagSet = TastyFlagSet(1 << 9)
18+
19+
case class TastyFlagSet private[TastyFlags](private val flags: Int) extends AnyVal {
20+
def toSingletonSets: SingletonSets = SingletonSets(flags)
21+
def |(other: TastyFlagSet): TastyFlagSet = TastyFlagSet(flags | other.flags)
22+
def &(mask: TastyFlagSet): TastyFlagSet = TastyFlagSet(flags & mask.flags)
23+
def &~(mask: TastyFlagSet): TastyFlagSet = TastyFlagSet(flags & ~mask.flags)
24+
def unary_! : Boolean = this == EmptyFlags
25+
def is(mask: TastyFlagSet): Boolean = (this & mask).hasFlags
4326
def is(mask: TastyFlagSet, butNot: TastyFlagSet): Boolean = if (!butNot) is(mask) else is(mask) && not(butNot)
4427
def not(mask: TastyFlagSet): Boolean = !is(mask)
45-
def hasFlags: Boolean = !(!flagset)
46-
def except(mask: TastyFlagSet): (Boolean, TastyFlagSet) = (is(mask), flagset &~ mask)
47-
48-
def show: String = {
49-
import Live._
50-
if (!flagset) "EmptyFlags"
51-
else flagset.toSingletonSets.map {
52-
case f if f === Erased => "Erased"
53-
case f if f === Internal => "Internal"
54-
case f if f === Inline => "Inline"
55-
case f if f === InlineProxy => "InlineProxy"
56-
case f if f === Opaque => "Opaque"
57-
case f if f === Scala2x => "Scala2x"
58-
case f if f === Extension => "Extension"
59-
case f if f === Given => "Given"
60-
case f if f === Exported => "Exported"
61-
case f if f === NoInits => "NoInits"
62-
}.mkString(" | ")
63-
}
64-
65-
}
66-
67-
implicit final class SingletonSetsAPI(private val flagsets: SingletonSets[TastyFlagSet]) extends AnyVal {
68-
def map[A](f: TastyFlagSet => A): Iterable[A] = Live.map(flagsets, f)
28+
def hasFlags: Boolean = !(!this)
29+
def except(mask: TastyFlagSet): (Boolean, TastyFlagSet) = is(mask) -> (this &~ mask)
6930
}
7031

71-
val Live: TastyFlags = new TastyFlags {
72-
73-
type TastyFlagSet = Int
74-
type SingletonSets[X] = X
75-
76-
val EmptyFlags = 0
77-
val Erased = 1 << 0
78-
val Internal = 1 << 1
79-
val Inline = 1 << 2
80-
val InlineProxy = 1 << 3
81-
val Opaque = 1 << 4
82-
val Scala2x = 1 << 5
83-
val Extension = 1 << 6
84-
val Given = 1 << 7
85-
val Exported = 1 << 8
86-
val NoInits = 1 << 9
87-
88-
final def union(a: TastyFlagSet, b: TastyFlagSet) = a | b
89-
final def intersect(a: TastyFlagSet, b: TastyFlagSet) = a & b
90-
final def equal(set: TastyFlagSet, other: TastyFlagSet) = set == other
91-
final def remove(set: TastyFlagSet, mask: TastyFlagSet) = set & ~mask
92-
final def toSingletonSets(set: TastyFlagSet) = set
93-
94-
final def map[A](set: SingletonSets[TastyFlagSet], f: TastyFlagSet => A) = {
32+
case class SingletonSets private[TastyFlags](private val set: Int) extends AnyVal {
33+
def map[A](f: TastyFlagSet => A): Iterable[A] = {
9534
val buf = Iterable.newBuilder[A]
9635
var i = 0
9736
while (i <= 9) {
9837
val flag = 1 << i
9938
if ((flag & set) != 0) {
100-
buf += f(flag)
39+
buf += f(TastyFlagSet(flag))
10140
}
10241
i += 1
10342
}
10443
buf.result
10544
}
10645
}
46+
10747
}

src/compiler/scala/tools/nsc/tasty/TastyUniverse.scala

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ trait TastyUniverse { self =>
88
val symbolTable: SymbolTable
99

1010
import symbolTable._
11-
import TastyFlags.Live._
11+
import TastyFlags._
1212
import FlagSets._
1313
import Contexts._
1414

@@ -395,4 +395,23 @@ trait TastyUniverse { self =>
395395
}
396396

397397
def showSym(sym: Symbol): String = s"$sym # ${sym.hashCode}"
398+
399+
def show(flags: FlagSet): String = symbolTable.show(flags)
400+
401+
def show(flags: TastyFlagSet): String =
402+
if (!flags) "EmptyFlags"
403+
else flags.toSingletonSets.map { f =>
404+
(f: @unchecked) match {
405+
case Erased => "Erased"
406+
case Internal => "Internal"
407+
case Inline => "Inline"
408+
case InlineProxy => "InlineProxy"
409+
case Opaque => "Opaque"
410+
case Scala2x => "Scala2x"
411+
case Extension => "Extension"
412+
case Given => "Given"
413+
case Exported => "Exported"
414+
case NoInits => "NoInits"
415+
}
416+
} mkString(" | ")
398417
}

src/compiler/scala/tools/nsc/tasty/TreeUnpickler.scala

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class TreeUnpickler(reader: TastyReader,
2323
import SymbolOps._
2424
import TreeUnpickler._
2525
import MaybeCycle._
26-
import TastyFlags.Live._
26+
import TastyFlags._
2727
import Signature._
2828
import Contexts._
2929

@@ -550,9 +550,9 @@ abstract class TreeUnpickler(reader: TastyReader,
550550
if (!tastyFlagSet)
551551
show(flags)
552552
else if (givenFlags == NoFlags)
553-
tastyFlagSet.show
553+
show(tastyFlagSet)
554554
else
555-
show(flags) + " | " + tastyFlagSet.show
555+
show(flags) + " | " + show(tastyFlagSet)
556556
}
557557
ctx.log(s"""creating symbol $name${if (privateWithin ne NoSymbol) s" private within $privateWithin" else ""} at $start with flags $showFlags""")
558558
def adjustIfModule(completer: TastyLazyType) = {
@@ -796,7 +796,7 @@ abstract class TreeUnpickler(reader: TastyReader,
796796
val noCycle = tag match {
797797
case DEFDEF =>
798798
val (isExtension, exceptExtension) = completer.tastyFlagSet.except(Extension)
799-
assertTasty(!exceptExtension, s"unsupported Scala 3 flags on def: ${exceptExtension.show}")
799+
assertTasty(!exceptExtension, s"unsupported Scala 3 flags on def: ${show(exceptExtension)}")
800800
if (isExtension) ctx.log(s"$name is a Scala 3 extension method.")
801801
val tparams = readParams[NoCycle](TYPEPARAM)(localCtx)
802802
val vparamss = readParamss(localCtx)
@@ -809,13 +809,13 @@ abstract class TreeUnpickler(reader: TastyReader,
809809
NoCycle(at = symAddr)
810810
case VALDEF => // valdef in TASTy is either a module value or a method forwarder to a local value.
811811
val (isInline, exceptInline) = completer.tastyFlagSet.except(Inline)
812-
assertTasty(!exceptInline, s"unsupported flags on val: ${exceptInline.show}")
812+
assertTasty(!exceptInline, s"unsupported flags on val: ${show(exceptInline)}")
813813
val tpe = readTpt()(localCtx).tpe
814814
if (isInline) assertTasty(tpe.isInstanceOf[ConstantType], s"inline val ${sym.nameString} with non-constant type $tpe")
815815
sym.info = if (sym.flags.not(Module)) internal.nullaryMethodType(tpe) else tpe // TODO: really?
816816
NoCycle(at = symAddr)
817817
case TYPEDEF | TYPEPARAM =>
818-
assertTasty(!completer.tastyFlagSet, s"unsupported Scala 3 flags on type: ${completer.tastyFlagSet.show}")
818+
assertTasty(!completer.tastyFlagSet, s"unsupported Scala 3 flags on type: ${show(completer.tastyFlagSet)}")
819819
if (sym.isClass) {
820820
sym.owner.ensureCompleted()
821821
readTemplate(symAddr)(localCtx)
@@ -843,7 +843,7 @@ abstract class TreeUnpickler(reader: TastyReader,
843843
NoCycle(at = symAddr)
844844
}
845845
case PARAM =>
846-
assertTasty(!completer.tastyFlagSet, s"unsupported flags on parameter: ${completer.tastyFlagSet.show}")
846+
assertTasty(!completer.tastyFlagSet, s"unsupported flags on parameter: ${show(completer.tastyFlagSet)}")
847847
val tpt = readTpt()(localCtx)
848848
if (nothingButMods(end) && sym.not(ParamAccessor)) {
849849
sym.info = tpt.tpe

0 commit comments

Comments
 (0)