@@ -102,29 +102,27 @@ object DesugarEnums {
102
102
103
103
/** A creation method for a value of enum type `E`, which is defined as follows:
104
104
*
105
- * private def $new(tag: Int, name: String) = new E {
106
- * def enumTag = tag
105
+ * private def $new(tag: Int, name_: String) = new E {
106
+ * override def ordinal = tag
107
+ * override def name = name_
107
108
* override def toString = name
108
109
* $values.register(this)
109
110
* }
110
111
*/
111
112
private def enumValueCreator (implicit ctx : Context ) = {
112
113
def param (name : TermName , typ : Type ) =
113
114
ValDef (name, TypeTree (typ), EmptyTree ).withFlags(Param )
114
- val enumTagDef =
115
- DefDef (nme.enumTag, Nil , Nil , TypeTree (), Ident (nme.tag))
116
- val toStringDef =
117
- DefDef (nme.toString_, Nil , Nil , TypeTree (), Ident (nme.name))
118
- .withFlags(Override )
115
+ val ordinalDef = ordinalMeth(Ident (nme.tag))
116
+ val nameDef = nameMeth(Ident (nme.name_))
119
117
val creator = New (Template (
120
118
constr = emptyConstructor,
121
119
parents = enumClassRef :: Nil ,
122
120
derived = Nil ,
123
121
self = EmptyValDef ,
124
- body = List (enumTagDef, toStringDef ) ++ registerCall
122
+ body = List (ordinalDef, nameDef, toStringMethAsName ) ++ registerCall
125
123
).withAttachment(ExtendsSingletonMirror , ()))
126
124
DefDef (nme.DOLLAR_NEW , Nil ,
127
- List (List (param(nme.tag, defn.IntType ), param(nme.name , defn.StringType ))),
125
+ List (List (param(nme.tag, defn.IntType ), param(nme.name_ , defn.StringType ))),
128
126
TypeTree (), creator).withFlags(Private | Synthetic )
129
127
}
130
128
@@ -232,7 +230,7 @@ object DesugarEnums {
232
230
* - scaffolding containing the necessary definitions for singleton enum cases
233
231
* unless that scaffolding was already generated by a previous call to `nextEnumKind`.
234
232
*/
235
- def nextEnumTag (kind : CaseKind .Value )(implicit ctx : Context ): (Int , List [Tree ]) = {
233
+ def nextOrdinal (kind : CaseKind .Value )(implicit ctx : Context ): (Int , List [Tree ]) = {
236
234
val (count, seenKind) = ctx.tree.removeAttachment(EnumCaseCount ).getOrElse((0 , CaseKind .Class ))
237
235
val minKind = if (kind < seenKind) kind else seenKind
238
236
ctx.tree.pushAttachment(EnumCaseCount , (count + 1 , minKind))
@@ -244,14 +242,23 @@ object DesugarEnums {
244
242
(count, scaffolding)
245
243
}
246
244
247
- /** A pair consisting of
248
- * - a method returning the next enum tag
249
- * - scaffolding as defined in `nextEnumTag`
250
- */
251
- def enumTagMeth (kind : CaseKind .Value )(implicit ctx : Context ): (DefDef , List [Tree ]) = {
252
- val (tag, scaffolding) = nextEnumTag(kind)
253
- (DefDef (nme.enumTag, Nil , Nil , TypeTree (), Literal (Constant (tag))), scaffolding)
254
- }
245
+ def ordinalMeth (body : Tree )(implicit ctx : Context ): DefDef =
246
+ DefDef (nme.ordinal, Nil , Nil , TypeTree (defn.IntType ), body).withFlags(Override )
247
+
248
+ def nameMeth (body : Tree )(implicit ctx : Context ): DefDef =
249
+ DefDef (nme.name, Nil , Nil , TypeTree (defn.StringType ), body).withFlags(Override )
250
+
251
+ def toStringMeth (body : Tree )(implicit ctx : Context ): DefDef =
252
+ DefDef (nme.toString_, Nil , Nil , TypeTree (defn.StringType ), body).withFlags(Override )
253
+
254
+ def ordinalMethLit (ord : Int )(implicit ctx : Context ): DefDef =
255
+ ordinalMeth(Literal (Constant (ord)))
256
+
257
+ def nameMethLit (name : String )(implicit ctx : Context ): DefDef =
258
+ nameMeth(Literal (Constant (name)))
259
+
260
+ def toStringMethAsName (implicit ctx : Context ): DefDef =
261
+ toStringMeth(Ident (nme.name))
255
262
256
263
/** Expand a module definition representing a parameterless enum case */
257
264
def expandEnumModule (name : TermName , impl : Template , mods : Modifiers , span : Span )(implicit ctx : Context ): Tree = {
@@ -260,11 +267,10 @@ object DesugarEnums {
260
267
else if (impl.parents.isEmpty)
261
268
expandSimpleEnumCase(name, mods, span)
262
269
else {
263
- def toStringMeth =
264
- DefDef (nme.toString_, Nil , Nil , TypeTree (defn.StringType ), Literal (Constant (name.toString)))
265
- .withFlags(Override )
266
- val (tagMeth, scaffolding) = enumTagMeth(CaseKind .Object )
267
- val impl1 = cpy.Template (impl)(body = List (tagMeth, toStringMeth) ++ registerCall)
270
+ val (tag, scaffolding) = nextOrdinal(CaseKind .Object )
271
+ val ordinalDef = ordinalMethLit(tag)
272
+ val nameDef = nameMethLit(name.toString)
273
+ val impl1 = cpy.Template (impl)(body = List (ordinalDef, nameDef, toStringMethAsName) ++ registerCall)
268
274
.withAttachment(ExtendsSingletonMirror , ())
269
275
val vdef = ValDef (name, TypeTree (), New (impl1)).withMods(mods | Final )
270
276
flatTree(scaffolding ::: vdef :: Nil ).withSpan(span)
@@ -280,7 +286,7 @@ object DesugarEnums {
280
286
expandEnumModule(name, impl, mods, span)
281
287
}
282
288
else {
283
- val (tag, scaffolding) = nextEnumTag (CaseKind .Simple )
289
+ val (tag, scaffolding) = nextOrdinal (CaseKind .Simple )
284
290
val creator = Apply (Ident (nme.DOLLAR_NEW ), List (Literal (Constant (tag)), Literal (Constant (name.toString))))
285
291
val vdef = ValDef (name, enumClassRef, creator).withMods(mods | Final )
286
292
flatTree(scaffolding ::: vdef :: Nil ).withSpan(span)
0 commit comments