Skip to content

Commit 261c3d4

Browse files
authored
Merge pull request #3334 from dotty-staging/change-transform-phases-2
Eliminate AnnotationTransformer
2 parents 040bed3 + a1bc154 commit 261c3d4

File tree

6 files changed

+39
-49
lines changed

6 files changed

+39
-49
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dotty.tools.backend.jvm
22

33
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.ast.Trees
45
import dotty.tools.dotc
56
import dotty.tools.dotc.backend.jvm.DottyPrimitives
67
import dotty.tools.dotc.core.Flags.FlagSet
@@ -231,11 +232,16 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
231232
annot.atp.typeSymbol.getAnnotation(AnnotationRetentionAttr).
232233
flatMap(_.argumentConstant(0).map(_.symbolValue)).getOrElse(AnnotationRetentionClassAttr)
233234

235+
private def normalizeArgument(arg: Tree): Tree = arg match {
236+
case Trees.NamedArg(_, arg1) => normalizeArgument(arg1)
237+
case Trees.Typed(arg1, _) => normalizeArgument(arg1)
238+
case _ => arg
239+
}
240+
234241
private def emitArgument(av: AnnotationVisitor,
235242
name: String,
236243
arg: Tree, bcodeStore: BCodeHelpers)(innerClasesStore: bcodeStore.BCInnerClassGen): Unit = {
237-
(arg: @unchecked) match {
238-
244+
(normalizeArgument(arg): @unchecked) match {
239245
case Literal(const @ Constant(_)) =>
240246
const.tag match {
241247
case BooleanTag | ByteTag | ShortTag | CharTag | IntTag | LongTag | FloatTag | DoubleTag => av.visit(name, const.value)
@@ -251,16 +257,18 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
251257
case t: TypeApply if (t.fun.symbol == Predef_classOf) =>
252258
av.visit(name, t.args.head.tpe.classSymbol.denot.info.toTypeKind(bcodeStore)(innerClasesStore).toASMType)
253259
case t: tpd.Select =>
254-
if (t.symbol.denot.is(Flags.Enum)) {
260+
if (t.symbol.denot.owner.is(Flags.Enum)) {
255261
val edesc = innerClasesStore.typeDescriptor(t.tpe.asInstanceOf[bcodeStore.int.Type]) // the class descriptor of the enumeration class.
256262
val evalue = t.symbol.name.mangledString // value the actual enumeration value.
257263
av.visitEnum(name, edesc, evalue)
258264
} else {
259-
assert(toDenot(t.symbol).name.is(DefaultGetterName), toDenot(t.symbol).name.debugString) // this should be default getter. do not emmit.
265+
// println(i"not an enum: ${t.symbol} / ${t.symbol.denot.owner} / ${t.symbol.denot.owner.isTerm} / ${t.symbol.denot.owner.flags}")
266+
assert(toDenot(t.symbol).name.is(DefaultGetterName),
267+
s"${toDenot(t.symbol).name.debugString}") // this should be default getter. do not emmit.
260268
}
261269
case t: SeqLiteral =>
262270
val arrAnnotV: AnnotationVisitor = av.visitArray(name)
263-
for(arg <- t.elems) { emitArgument(arrAnnotV, null, arg, bcodeStore)(innerClasesStore) }
271+
for (arg <- t.elems) { emitArgument(arrAnnotV, null, arg, bcodeStore)(innerClasesStore) }
264272
arrAnnotV.visitEnd()
265273

266274
case Apply(fun, args) if fun.symbol == defn.ArrayClass.primaryConstructor ||
@@ -272,11 +280,15 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
272280
fun.asInstanceOf[Apply].args
273281
} else args
274282

275-
val flatArgs = actualArgs.flatMap {
276-
case t: tpd.SeqLiteral => t.elems
277-
case e => List(e)
283+
val flatArgs = actualArgs.flatMap { arg =>
284+
normalizeArgument(arg) match {
285+
case t: tpd.SeqLiteral => t.elems
286+
case e => List(e)
287+
}
288+
}
289+
for(arg <- flatArgs) {
290+
emitArgument(arrAnnotV, null, arg, bcodeStore)(innerClasesStore)
278291
}
279-
for(arg <- flatArgs) { emitArgument(arrAnnotV, null, arg, bcodeStore)(innerClasesStore) }
280292
arrAnnotV.visitEnd()
281293
/*
282294
case sb @ ScalaSigBytes(bytes) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ object Flags {
506506
Accessor | AbsOverride | Stable | Captured | Synchronized
507507

508508
/** Flags that can apply to a module class */
509-
final val RetainedModuleClassFlags: FlagSet = RetainedModuleValAndClassFlags | ImplClass
509+
final val RetainedModuleClassFlags: FlagSet = RetainedModuleValAndClassFlags | ImplClass | Enum
510510

511511
/** Packages and package classes always have these flags set */
512512
final val PackageCreationFlags =

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ object JavaParsers {
601601
atPos(cdef.pos) {
602602
assert(cdef.pos.exists)
603603
ModuleDef(cdef.name.toTermName,
604-
makeTemplate(List(), statics, List(), false)).withMods((cdef.mods & (Flags.AccessFlags | Flags.JavaDefined)).toTermFlags)
604+
makeTemplate(List(), statics, List(), false)).withMods((cdef.mods & Flags.RetainedModuleClassFlags).toTermFlags)
605605
}
606606

607607
def importCompanionObject(cdef: TypeDef): Tree =

compiler/src/dotty/tools/dotc/transform/ElimRepeated.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import core._
55
import Names._
66
import StdNames.nme
77
import Types._
8-
import dotty.tools.dotc.transform.TreeTransforms.{AnnotationTransformer, TransformerInfo, MiniPhaseTransform, TreeTransformer}
8+
import dotty.tools.dotc.transform.TreeTransforms.{TransformerInfo, MiniPhaseTransform, TreeTransformer}
99
import ast.Trees._
1010
import Flags._
1111
import Contexts.Context
@@ -25,7 +25,7 @@ import TypeUtils._
2525
/** A transformer that removes repeated parameters (T*) from all types, replacing
2626
* them with Seq types.
2727
*/
28-
class ElimRepeated extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer =>
28+
class ElimRepeated extends MiniPhaseTransform with InfoTransformer { thisTransformer =>
2929
import ast.tpd._
3030

3131
override def phaseName = "elimRepeated"
@@ -74,9 +74,10 @@ class ElimRepeated extends MiniPhaseTransform with InfoTransformer with Annotati
7474
transformTypeOfTree(tree)
7575

7676
override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo): Tree = {
77-
val formals = (tree.fun.tpe.widen: @unchecked) match {
78-
case mt: MethodType => mt.paramInfos
79-
}
77+
val formals =
78+
ctx.atPhase(thisTransformer) { implicit ctx =>
79+
tree.fun.tpe.widen.asInstanceOf[MethodType].paramInfos
80+
}
8081
val args1 = tree.args.zipWithConserve(formals) { (arg, formal) =>
8182
arg match {
8283
case arg: Typed if isWildcardStarArg(arg) =>
@@ -111,13 +112,13 @@ class ElimRepeated extends MiniPhaseTransform with InfoTransformer with Annotati
111112
/** If method overrides a Java varargs method, add a varargs bridge.
112113
* Also transform trees inside method annotation
113114
*/
114-
override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo): Tree = {
115-
assert(ctx.phase == thisTransformer)
116-
if (tree.symbol.info.isVarArgsMethod && overridesJava(tree.symbol))
117-
addVarArgsBridge(tree)
118-
else
119-
tree
120-
}
115+
override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo): Tree =
116+
ctx.atPhase(thisTransformer) { implicit ctx =>
117+
if (tree.symbol.info.isVarArgsMethod && overridesJava(tree.symbol))
118+
addVarArgsBridge(tree)
119+
else
120+
tree
121+
}
121122

122123
/** Add a Java varargs bridge
123124
* @param ddef the original method definition which is assumed to override

compiler/src/dotty/tools/dotc/transform/FirstTransform.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import StdNames._
3535
* if (true) A else B ==> A
3636
* if (false) A else B ==> B
3737
*/
38-
class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer =>
38+
class FirstTransform extends MiniPhaseTransform with InfoTransformer { thisTransformer =>
3939
import ast.tpd._
4040

4141
override def phaseName = "firstTransform"

compiler/src/dotty/tools/dotc/transform/TreeTransform.scala

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ object TreeTransforms {
6161

6262
def phase: MiniPhase
6363

64-
def treeTransformPhase: Phase = phase.next
64+
/** The phase at which the tree is transformed */
65+
final def treeTransformPhase: Phase = phase.next
6566

6667
val cpy: TypedTreeCopier = cpyBetweenPhases
6768

@@ -173,30 +174,6 @@ object TreeTransforms {
173174
def phase = this
174175
}
175176

176-
/** A helper trait to transform annotations on MemberDefs */
177-
trait AnnotationTransformer extends MiniPhaseTransform with DenotTransformer {
178-
179-
val annotationTransformer = mkTreeTransformer
180-
override final def treeTransformPhase = this
181-
// need to run at own phase because otherwise we get ahead of ourselves in transforming denotations
182-
183-
abstract override def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation =
184-
super.transform(ref) match {
185-
case ref1: SymDenotation if ref1.symbol.isDefinedInCurrentRun =>
186-
val annots = ref1.annotations
187-
val annotTrees = annots.map(_.tree)
188-
val annotTrees1 = annotTrees.mapConserve(annotationTransformer.macroTransform)
189-
if (annotTrees eq annotTrees1) ref1
190-
else {
191-
val derivedAnnots = (annots, annotTrees1).zipped.map((annot, annotTree1) =>
192-
annot.derivedAnnotation(annotTree1))
193-
ref1.copySymDenotation(annotations = derivedAnnots).copyCaches(ref1, ctx.phase.next)
194-
}
195-
case ref1 =>
196-
ref1
197-
}
198-
}
199-
200177
private class NoTreeTransform extends TreeTransform {
201178
def phase = unsupported("phase")
202179
}

0 commit comments

Comments
 (0)