12
12
13
13
package org .scalajs .ir
14
14
15
- import scala .annotation .switch
16
-
17
15
import Names ._
18
16
import Trees ._
19
17
@@ -62,25 +60,14 @@ object Types {
62
60
}
63
61
}
64
62
65
- sealed abstract class PrimTypeWithRef (private [Types ] val charCode : Char ) extends PrimType {
66
- /* The `charCode` arguably belongs to the corresponding `PrimRef` more than
67
- * to this class. We define it this way so that we don't have an apparent
68
- * circular dependency between the `PrimTypeWithRef`s and their `PrimRef`s.
69
- */
70
-
71
- def primRef : PrimRef = (charCode : @ switch) match {
72
- case 'V' => VoidRef
73
- case 'Z' => BooleanRef
74
- case 'C' => CharRef
75
- case 'B' => ByteRef
76
- case 'S' => ShortRef
77
- case 'I' => IntRef
78
- case 'J' => LongRef
79
- case 'F' => FloatRef
80
- case 'D' => DoubleRef
81
- case 'N' => NullRef
82
- case 'E' => NothingRef
83
- }
63
+ /* Each PrimTypeWithRef creates its corresponding `PrimRef`. Therefore, it
64
+ * takes the parameters that need to be passed to the `PrimRef` constructor.
65
+ * This little dance ensures proper initialization safety between
66
+ * `PrimTypeWithRef`s and `PrimRef`s.
67
+ */
68
+ sealed abstract class PrimTypeWithRef (primRefCharCode : Char , primRefDisplayName : String )
69
+ extends PrimType {
70
+ val primRef : PrimRef = new PrimRef (this )(primRefCharCode, primRefDisplayName)
84
71
}
85
72
86
73
/** Any type.
@@ -111,50 +98,50 @@ object Types {
111
98
* Expressions from which one can never come back are typed as `Nothing`.
112
99
* For example, `throw` and `return`.
113
100
*/
114
- case object NothingType extends PrimTypeWithRef ('E' )
101
+ case object NothingType extends PrimTypeWithRef ('E' , " nothing " )
115
102
116
103
/** The type of `undefined`. */
117
104
case object UndefType extends PrimType
118
105
119
106
/** Boolean type.
120
107
* It does not accept `null` nor `undefined`.
121
108
*/
122
- case object BooleanType extends PrimTypeWithRef ('Z' )
109
+ case object BooleanType extends PrimTypeWithRef ('Z' , " boolean " )
123
110
124
111
/** `Char` type, a 16-bit UTF-16 code unit.
125
112
* It does not accept `null` nor `undefined`.
126
113
*/
127
- case object CharType extends PrimTypeWithRef ('C' )
114
+ case object CharType extends PrimTypeWithRef ('C' , " char " )
128
115
129
116
/** 8-bit signed integer type.
130
117
* It does not accept `null` nor `undefined`.
131
118
*/
132
- case object ByteType extends PrimTypeWithRef ('B' )
119
+ case object ByteType extends PrimTypeWithRef ('B' , " byte " )
133
120
134
121
/** 16-bit signed integer type.
135
122
* It does not accept `null` nor `undefined`.
136
123
*/
137
- case object ShortType extends PrimTypeWithRef ('S' )
124
+ case object ShortType extends PrimTypeWithRef ('S' , " short " )
138
125
139
126
/** 32-bit signed integer type.
140
127
* It does not accept `null` nor `undefined`.
141
128
*/
142
- case object IntType extends PrimTypeWithRef ('I' )
129
+ case object IntType extends PrimTypeWithRef ('I' , " int " )
143
130
144
131
/** 64-bit signed integer type.
145
132
* It does not accept `null` nor `undefined`.
146
133
*/
147
- case object LongType extends PrimTypeWithRef ('J' )
134
+ case object LongType extends PrimTypeWithRef ('J' , " long " )
148
135
149
136
/** Float type (32-bit).
150
137
* It does not accept `null` nor `undefined`.
151
138
*/
152
- case object FloatType extends PrimTypeWithRef ('F' )
139
+ case object FloatType extends PrimTypeWithRef ('F' , " float " )
153
140
154
141
/** Double type (64-bit).
155
142
* It does not accept `null` nor `undefined`.
156
143
*/
157
- case object DoubleType extends PrimTypeWithRef ('D' )
144
+ case object DoubleType extends PrimTypeWithRef ('D' , " double " )
158
145
159
146
/** String type.
160
147
* It does not accept `null` nor `undefined`.
@@ -165,7 +152,7 @@ object Types {
165
152
* It does not accept `undefined`.
166
153
* The null type is a subtype of all class types and array types.
167
154
*/
168
- case object NullType extends PrimTypeWithRef ('N' )
155
+ case object NullType extends PrimTypeWithRef ('N' , " null " )
169
156
170
157
/** Class (or interface) type. */
171
158
final case class ClassType (className : ClassName , nullable : Boolean ) extends Type {
@@ -215,7 +202,7 @@ object Types {
215
202
}
216
203
217
204
/** Void type, the top of type of our type system. */
218
- case object VoidType extends PrimTypeWithRef ('V' )
205
+ case object VoidType extends PrimTypeWithRef ('V' , " void " )
219
206
220
207
@ deprecated(" Use VoidType instead" , since = " 1.18.0" )
221
208
lazy val NoType : VoidType .type = VoidType
@@ -271,7 +258,8 @@ object Types {
271
258
sealed abstract class NonArrayTypeRef extends TypeRef
272
259
273
260
/** Primitive type reference. */
274
- final case class PrimRef private [ir] (tpe : PrimTypeWithRef )
261
+ final case class PrimRef private [Types ] (tpe : PrimTypeWithRef )(
262
+ charCodeInit : Char , displayNameInit : String ) // "Init" variants so we can have good Scaladoc on the val's
275
263
extends NonArrayTypeRef {
276
264
277
265
/** The display name of this primitive type.
@@ -283,19 +271,7 @@ object Types {
283
271
* For `NullType` and `NothingType`, the names are `"null"` and
284
272
* `"nothing"`, respectively.
285
273
*/
286
- val displayName : String = (tpe.charCode: @ switch) match {
287
- case 'V' => " void"
288
- case 'Z' => " boolean"
289
- case 'C' => " char"
290
- case 'B' => " byte"
291
- case 'S' => " short"
292
- case 'I' => " int"
293
- case 'J' => " long"
294
- case 'F' => " float"
295
- case 'D' => " double"
296
- case 'N' => " null"
297
- case 'E' => " nothing"
298
- }
274
+ val displayName : String = displayNameInit
299
275
300
276
/** The char code of this primitive type.
301
277
*
@@ -307,20 +283,30 @@ object Types {
307
283
* For `NullType` and `NothingType`, the char codes are `'N'` and `'E'`,
308
284
* respectively.
309
285
*/
310
- val charCode : Char = tpe.charCode
286
+ val charCode : Char = charCodeInit
287
+
288
+ // Make sure the `case class` does not produce a public copy method
289
+ private def copy (tpe : PrimTypeWithRef )(charCodeInit : Char , displayNameInit : String ): PrimRef =
290
+ throw new Error (" dead code" )
291
+ }
292
+
293
+ object PrimRef {
294
+ // Make sure the `case class` does not produce a public apply method
295
+ private def apply (tpe : PrimTypeWithRef )(charCodeInit : Char , displayNameInit : String ): PrimRef =
296
+ throw new Error (" dead code" )
311
297
}
312
298
313
- final val VoidRef = PrimRef ( VoidType )
314
- final val BooleanRef = PrimRef ( BooleanType )
315
- final val CharRef = PrimRef ( CharType )
316
- final val ByteRef = PrimRef ( ByteType )
317
- final val ShortRef = PrimRef ( ShortType )
318
- final val IntRef = PrimRef ( IntType )
319
- final val LongRef = PrimRef ( LongType )
320
- final val FloatRef = PrimRef ( FloatType )
321
- final val DoubleRef = PrimRef ( DoubleType )
322
- final val NullRef = PrimRef ( NullType )
323
- final val NothingRef = PrimRef ( NothingType )
299
+ final val VoidRef = VoidType .primRef
300
+ final val BooleanRef = BooleanType .primRef
301
+ final val CharRef = CharType .primRef
302
+ final val ByteRef = ByteType .primRef
303
+ final val ShortRef = ShortType .primRef
304
+ final val IntRef = IntType .primRef
305
+ final val LongRef = LongType .primRef
306
+ final val FloatRef = FloatType .primRef
307
+ final val DoubleRef = DoubleType .primRef
308
+ final val NullRef = NullType .primRef
309
+ final val NothingRef = NothingType .primRef
324
310
325
311
/** Class (or interface) type. */
326
312
final case class ClassRef (className : ClassName ) extends NonArrayTypeRef {
0 commit comments