Skip to content

Commit b3d3b33

Browse files
Merge pull request #4632 from dotty-staging/reformat-tasty-reflect-mods
Improve Tasty reflect modifiers api
2 parents b942b9e + afdc43c commit b3d3b33

File tree

4 files changed

+33
-96
lines changed

4 files changed

+33
-96
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,24 @@ object TastyImpl extends scala.tasty.Tasty {
137137

138138
def owner(implicit ctx: Context): Definition = FromSymbol.definition(x.symbol.owner)
139139

140-
def flags(implicit ctx: Contexts.Context): FlagSet =
140+
def flags(implicit ctx: Context): FlagSet =
141141
new FlagSet(x.symbol.flags)
142142

143-
def mods(implicit ctx: Context): List[Modifier] = {
144-
val privateWithin = x.symbol.privateWithin
145-
val isProtected = x.symbol.is(core.Flags.Protected)
146-
ModFlags(new FlagSet(x.symbol.flags)) ::
147-
(if (privateWithin.exists) List(ModQual(privateWithin.typeRef, isProtected)) else Nil) :::
148-
x.symbol.annotations.map(t => ModAnnot(t.tree))
143+
def privateWithin(implicit ctx: Context): Option[Type] = {
144+
val within = x.symbol.privateWithin
145+
if (within.exists && !x.symbol.is(core.Flags.Protected)) Some(within.typeRef)
146+
else None
149147
}
150148

149+
def protectedWithin(implicit ctx: Context): Option[Type] = {
150+
val within = x.symbol.privateWithin
151+
if (within.exists && x.symbol.is(core.Flags.Protected)) Some(within.typeRef)
152+
else None
153+
}
154+
155+
def annots(implicit ctx: Context): List[Term] =
156+
x.symbol.annotations.map(_.tree)
157+
151158
def localContext(implicit ctx: Context): Context =
152159
if (x.hasType && x.symbol.exists) ctx.withOwner(x.symbol)
153160
else ctx
@@ -909,51 +916,6 @@ object TastyImpl extends scala.tasty.Tasty {
909916

910917
}
911918

912-
913-
// ===== Modifier =================================================
914-
915-
type Modifier = ModImpl
916-
917-
trait ModImpl
918-
case class ModAnnot(tree: Term) extends ModImpl
919-
case class ModFlags(flags: FlagSet) extends ModImpl
920-
case class ModQual(tp: Type, protect: Boolean) extends ModImpl
921-
922-
def modifierClassTag: ClassTag[Modifier] = implicitly[ClassTag[Modifier]]
923-
924-
925-
object Modifier extends ModifierModule {
926-
927-
object Annotation extends AnnotationExtractor {
928-
def unapply(x: Modifier)(implicit ctx: Context): Option[Term] = x match {
929-
case ModAnnot(tree) => Some(tree)
930-
case _ => None
931-
}
932-
}
933-
934-
object Flags extends FlagsExtractor {
935-
def unapply(x: Modifier)(implicit ctx: Context): Option[FlagSet] = x match {
936-
case ModFlags(flags) => Some(flags)
937-
case _ => None
938-
}
939-
}
940-
941-
object QualifiedPrivate extends QualifiedPrivateExtractor {
942-
def unapply(x: Modifier)(implicit ctx: Context): Option[Type] = x match {
943-
case ModQual(tp, false) => Some(tp)
944-
case _ => None
945-
}
946-
}
947-
948-
object QualifiedProtected extends QualifiedProtectedExtractor {
949-
def unapply(x: Modifier)(implicit ctx: Context): Option[Type] = x match {
950-
case ModQual(tp, true) => Some(tp)
951-
case _ => None
952-
}
953-
}
954-
955-
}
956-
957919
// ===== Signature ================================================
958920

959921
type Signature = core.Signature

library/src/scala/tasty/Tasty.scala

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ abstract class Tasty { tasty =>
116116

117117
trait DefinitionAPI {
118118
def flags(implicit ctx: Context): FlagSet
119-
def mods(implicit ctx: Context): List[Modifier]
119+
def privateWithin(implicit ctx: Context): Option[Type]
120+
def protectedWithin(implicit ctx: Context): Option[Type]
121+
def annots(implicit ctx: Context): List[Term]
120122
def owner(implicit ctx: Context): Definition
121123
def localContext(implicit ctx: Context): Context
122124
}
@@ -680,38 +682,6 @@ abstract class Tasty { tasty =>
680682

681683
}
682684

683-
// ===== Modifiers ================================================
684-
685-
type Modifier
686-
687-
implicit def modifierClassTag: ClassTag[Modifier]
688-
689-
val Modifier: ModifierModule
690-
abstract class ModifierModule {
691-
692-
val Annotation: AnnotationExtractor
693-
abstract class AnnotationExtractor {
694-
def unapply(x: Modifier)(implicit ctx: Context): Option[Term]
695-
}
696-
697-
val Flags: FlagsExtractor
698-
abstract class FlagsExtractor {
699-
def unapply(x: Modifier)(implicit ctx: Context): Option[FlagSet]
700-
}
701-
702-
val QualifiedPrivate: QualifiedPrivateExtractor
703-
abstract class QualifiedPrivateExtractor {
704-
def unapply(x: Modifier)(implicit ctx: Context): Option[Type]
705-
}
706-
707-
val QualifiedProtected: QualifiedProtectedExtractor
708-
abstract class QualifiedProtectedExtractor {
709-
def unapply(x: Modifier)(implicit ctx: Context): Option[Type]
710-
}
711-
712-
}
713-
714-
715685
// ===== Signature ================================================
716686

717687
type Signature

library/src/scala/tasty/util/ShowExtractors.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,6 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
195195
this += "NoPrefix()"
196196
}
197197

198-
def visitModifier(x: Modifier): Buffer = x match {
199-
case Modifier.Flags(flags) => this += "Modifier.Flags(" += flags.toString += ")"
200-
case Modifier.QualifiedPrivate(tp) => this += "Modifier.QualifiedPrivate(" += tp += ")"
201-
case Modifier.QualifiedProtected(tp) => this += "Modifier.QualifiedProtected(" += tp += ")"
202-
case Modifier.Annotation(tree) => this += "Modifier.Annotation(" += tree += ")"
203-
}
204-
205198
def visitId(x: Id): Buffer = {
206199
val Id(name) = x
207200
this += "Id(\"" += name += "\")"

tests/pos/tasty/definitions.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,30 @@ object definitions {
3030

3131
// Does DefDef need a `def tpe: MethodType | PolyType`?
3232
case class ValDef(name: String, tpt: TypeTree, rhs: Option[Term]) extends Definition {
33-
def mods: List[Modifier] = ???
33+
def flags: FlagSet = ???
34+
def privateWithin: Option[Type] = ???
35+
def protectedWithin: Option[Type] = ???
36+
def annots: List[Term] = ???
3437
}
3538
case class DefDef(name: String, typeParams: List[TypeDef], paramss: List[List[ValDef]],
3639
returnTpt: TypeTree, rhs: Option[Term]) extends Definition {
37-
def mods: List[Modifier] = ???
40+
def flags: FlagSet = ???
41+
def privateWithin: Option[Type] = ???
42+
def protectedWithin: Option[Type] = ???
43+
def annots: List[Term] = ???
3844
}
3945
case class TypeDef(name: String, rhs: TypeTree | TypeBoundsTree) extends Definition {
40-
def mods: List[Modifier] = ???
46+
def flags: FlagSet = ???
47+
def privateWithin: Option[Type] = ???
48+
def protectedWithin: Option[Type] = ???
49+
def annots: List[Term] = ???
4150
}
4251
case class ClassDef(name: String, constructor: DefDef, parents: List[Term | TypeTree],
4352
self: Option[ValDef], body: List[Statement]) extends Definition {
44-
def mods: List[Modifier] = ???
53+
def flags: FlagSet = ???
54+
def privateWithin: Option[Type] = ???
55+
def protectedWithin: Option[Type] = ???
56+
def annots: List[Term] = ???
4557
}
4658
case class PackageDef(name: String, override val owner: PackageDef) extends Definition {
4759
def members: List[Statement] = ???

0 commit comments

Comments
 (0)