1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
package dotty.tools
package dotc
package core
import Types._, Contexts._, Symbols._, Denotations._, SymDenotations._, StdNames._, Names._
import Flags._, Scopes._, Decorators._, NameOps._, util.Positions._
import pickling.UnPickler.ensureConstructor
import scala.annotation.{ switch, meta }
import scala.collection.{ mutable, immutable }
import PartialFunction._
import collection.mutable
import scala.reflect.api.{ Universe => ApiUniverse }
object Definitions {
val MaxFunctionArity, MaxTupleArity = 22
}
/** A class defining symbols and types of standard definitions */
class Definitions {
import Definitions._
private implicit var ctx: Context = _
private def newSymbol[N <: Name](owner: Symbol, name: N, flags: FlagSet, info: Type) =
ctx.newSymbol(owner, name, flags | Permanent, info)
private def newClassSymbol(owner: Symbol, name: TypeName, flags: FlagSet, infoFn: ClassSymbol => Type) =
ctx.newClassSymbol(owner, name, flags | Permanent, infoFn).entered
private def newCompleteClassSymbol(owner: Symbol, name: TypeName, flags: FlagSet, parents: List[TypeRef], decls: Scope = newScope) =
ctx.newCompleteClassSymbol(owner, name, flags | Permanent, parents, decls).entered
private def newTopClassSymbol(name: TypeName, flags: FlagSet, parents: List[TypeRef]) =
completeClass(newCompleteClassSymbol(ScalaPackageClass, name, flags, parents))
private def newTypeField(cls: ClassSymbol, name: TypeName, flags: FlagSet, scope: MutableScope) =
scope.enter(newSymbol(cls, name, flags, TypeBounds.empty))
private def newTypeParam(cls: ClassSymbol, name: TypeName, flags: FlagSet, scope: MutableScope) =
newTypeField(cls, name, flags | ClassTypeParamCreationFlags, scope)
private def newSyntheticTypeParam(cls: ClassSymbol, scope: MutableScope, paramFlags: FlagSet, suffix: String = "T0") =
newTypeParam(cls, suffix.toTypeName.expandedName(cls), ExpandedName | paramFlags, scope)
private def specialPolyClass(name: TypeName, paramFlags: FlagSet, parentConstrs: Type*): ClassSymbol = {
val completer = new LazyType {
def complete(denot: SymDenotation)(implicit ctx: Context): Unit = {
val cls = denot.asClass.classSymbol
val paramDecls = newScope
val typeParam = newSyntheticTypeParam(cls, paramDecls, paramFlags)
def instantiate(tpe: Type) =
if (tpe.typeParams.nonEmpty) tpe.appliedTo(typeParam.typeRef)
else tpe
val parents = parentConstrs.toList map instantiate
val parentRefs: List[TypeRef] = ctx.normalizeToClassRefs(parents, cls, paramDecls)
denot.info = ClassInfo(ScalaPackageClass.thisType, cls, parentRefs, paramDecls)
}
}
newClassSymbol(ScalaPackageClass, name, EmptyFlags, completer)
}
private def newMethod(cls: ClassSymbol, name: TermName, info: Type, flags: FlagSet = EmptyFlags): TermSymbol =
newSymbol(cls, name.encode, flags | Method, info).entered.asTerm
private def newAliasType(name: TypeName, tpe: Type, flags: FlagSet = EmptyFlags): TypeSymbol = {
val sym = newSymbol(ScalaPackageClass, name, flags, TypeAlias(tpe))
ScalaPackageClass.preDecls.enter(sym)
sym
}
private def newPolyMethod(cls: ClassSymbol, name: TermName, typeParamCount: Int,
resultTypeFn: PolyType => Type, flags: FlagSet = EmptyFlags) = {
val tparamNames = tpnme.syntheticTypeParamNames(typeParamCount)
val tparamBounds = tparamNames map (_ => TypeBounds.empty)
val ptype = PolyType(tparamNames)(_ => tparamBounds, resultTypeFn)
newMethod(cls, name, ptype, flags)
}
private def newT1ParameterlessMethod(cls: ClassSymbol, name: TermName, resultTypeFn: PolyType => Type, flags: FlagSet) =
newPolyMethod(cls, name, 1, resultTypeFn, flags)
private def newT1EmptyParamsMethod(cls: ClassSymbol, name: TermName, resultTypeFn: PolyType => Type, flags: FlagSet) =
newPolyMethod(cls, name, 1, pt => MethodType(Nil, resultTypeFn(pt)), flags)
private def mkArityArray(name: String, arity: Int, countFrom: Int): Array[ClassSymbol] = {
val arr = new Array[ClassSymbol](arity + 1)
for (i <- countFrom to arity) arr(i) = ctx.requiredClass(name + i)
arr
}
private def completeClass(cls: ClassSymbol): ClassSymbol = {
ensureConstructor(cls, EmptyScope)
if (cls.linkedClass.exists) cls.linkedClass.info = NoType
cls
}
lazy val RootClass: ClassSymbol = ctx.newPackageSymbol(
NoSymbol, nme.ROOT, (root, rootcls) => ctx.rootLoader(root)).moduleClass.asClass
lazy val RootPackage: TermSymbol = ctx.newSymbol(
NoSymbol, nme.ROOTPKG, PackageCreationFlags, TypeRef(NoPrefix, RootClass))
lazy val EmptyPackageClass = ctx.newCompletePackageSymbol(RootClass, nme.EMPTY_PACKAGE).moduleClass.asClass
lazy val EmptyPackageVal = EmptyPackageClass.sourceModule.entered
lazy val ScalaPackageVal = ctx.requiredPackage("scala")
lazy val ScalaMathPackageVal = ctx.requiredPackage("scala.math")
lazy val ScalaPackageClass = ScalaPackageVal.moduleClass.asClass
lazy val JavaPackageVal = ctx.requiredPackage("java")
lazy val JavaLangPackageVal = ctx.requiredPackage("java.lang")
// fundamental modules
lazy val SysPackage = ctx.requiredModule("scala.sys.package")
def Sys_error = ctx.requiredMethod(SysPackage.moduleClass.asClass, nme.error)
/** Note: We cannot have same named methods defined in Object and Any (and AnyVal, for that matter)
* because after erasure the Any and AnyVal references get remapped to the Object methods
* which would result in a double binding assertion failure.
* Instead we do the following:
*
* - Have some methods exist only in Any, and remap them with the Erasure denotation
* transformer to be owned by Object.
* - Have other methods exist only in Object.
* To achieve this, we synthesize all Any and Object methods; Object methods no longer get
* loaded from a classfile.
*
* There's a remaining question about `getClass`. In Scala2.x `getClass` was handled by compiler magic.
* This is deemed too cumersome for Dotty and therefore right now `getClass` gets no special treatment;
* it's just a method on `Any` which returns the raw type `java.lang.Class`. An alternative
* way to get better `getClass` typing would be to treat `getClass` as a method of a generic
* decorator which gets remapped in a later phase to Object#getClass. Then we could give it
* the right type without changing the typechecker:
*
* implicit class AnyGetClass[T](val x: T) extends AnyVal {
* def getClass: java.lang.Class[T] = ???
* }
*/
lazy val AnyClass: ClassSymbol = completeClass(newCompleteClassSymbol(ScalaPackageClass, tpnme.Any, Abstract, Nil))
lazy val AnyValClass: ClassSymbol = completeClass(newCompleteClassSymbol(ScalaPackageClass, tpnme.AnyVal, Abstract, List(AnyClass.typeRef)))
lazy val Any_== = newMethod(AnyClass, nme.EQ, methOfAny(BooleanType), Final)
lazy val Any_!= = newMethod(AnyClass, nme.NE, methOfAny(BooleanType), Final)
lazy val Any_equals = newMethod(AnyClass, nme.equals_, methOfAny(BooleanType))
lazy val Any_hashCode = newMethod(AnyClass, nme.hashCode_, MethodType(Nil, IntType))
lazy val Any_toString = newMethod(AnyClass, nme.toString_, MethodType(Nil, StringType))
lazy val Any_## = newMethod(AnyClass, nme.HASHHASH, ExprType(IntType), Final)
lazy val Any_getClass = newMethod(AnyClass, nme.getClass_, MethodType(Nil, ClassClass.typeRef), Final)
lazy val Any_isInstanceOf = newT1ParameterlessMethod(AnyClass, nme.isInstanceOf_, _ => BooleanType, Final)
lazy val Any_asInstanceOf = newT1ParameterlessMethod(AnyClass, nme.asInstanceOf_, PolyParam(_, 0), Final)
def AnyMethods = List(Any_==, Any_!=, Any_equals, Any_hashCode,
Any_toString, Any_##, Any_getClass, Any_isInstanceOf, Any_asInstanceOf)
lazy val ObjectClass: ClassSymbol = {
val cls = ctx.requiredClass("java.lang.Object")
assert(!cls.isCompleted, "race for completing java.lang.Object")
cls.info = ClassInfo(cls.owner.thisType, cls, AnyClass.typeRef :: Nil, newScope)
completeClass(cls)
}
lazy val AnyRefAlias: TypeSymbol = newAliasType(tpnme.AnyRef, ObjectType)
lazy val Object_eq = newMethod(ObjectClass, nme.eq, methOfAnyRef(BooleanType), Final)
lazy val Object_ne = newMethod(ObjectClass, nme.ne, methOfAnyRef(BooleanType), Final)
lazy val Object_synchronized = newPolyMethod(ObjectClass, nme.synchronized_, 1,
pt => MethodType(List(PolyParam(pt, 0)), PolyParam(pt, 0)), Final)
lazy val Object_clone = newMethod(ObjectClass, nme.clone_, MethodType(Nil, ObjectType), Protected)
lazy val Object_finalize = newMethod(ObjectClass, nme.finalize_, MethodType(Nil, UnitType), Protected)
lazy val Object_notify = newMethod(ObjectClass, nme.notify_, MethodType(Nil, UnitType))
lazy val Object_notifyAll = newMethod(ObjectClass, nme.notifyAll_, MethodType(Nil, UnitType))
lazy val Object_wait = newMethod(ObjectClass, nme.wait_, MethodType(Nil, UnitType))
lazy val Object_waitL = newMethod(ObjectClass, nme.wait_, MethodType(LongType :: Nil, UnitType))
lazy val Object_waitLI = newMethod(ObjectClass, nme.wait_, MethodType(LongType :: IntType :: Nil, UnitType))
def ObjectMethods = List(Object_eq, Object_ne, Object_synchronized, Object_clone,
Object_finalize, Object_notify, Object_notifyAll, Object_wait, Object_waitL, Object_waitLI)
/** Dummy method needed by elimByName */
lazy val dummyApply = newPolyMethod(
RootClass, nme.dummyApply, 1,
pt => MethodType(List(FunctionType(Nil, PolyParam(pt, 0))), PolyParam(pt, 0)))
lazy val NothingClass: ClassSymbol = newCompleteClassSymbol(
ScalaPackageClass, tpnme.Nothing, AbstractFinal, List(AnyClass.typeRef))
lazy val NullClass: ClassSymbol = newCompleteClassSymbol(
ScalaPackageClass, tpnme.Null, AbstractFinal, List(ObjectClass.typeRef))
lazy val ScalaPredefModule = ctx.requiredModule("scala.Predef")
lazy val ScalaRuntimeModule = ctx.requiredModule("scala.runtime.ScalaRunTime")
lazy val ScalaRuntimeClass = ScalaRuntimeModule.moduleClass.asClass
def runtimeMethod(name: PreName) = ctx.requiredMethod(ScalaRuntimeClass, name)
lazy val BoxesRunTimeModule = ctx.requiredModule("scala.runtime.BoxesRunTime")
lazy val BoxesRunTimeClass = BoxesRunTimeModule.moduleClass.asClass
lazy val ScalaStaticsModule = ctx.requiredModule("scala.runtime.Statics")
lazy val ScalaStaticsClass = ScalaStaticsModule.moduleClass.asClass
def staticsMethod(name: PreName) = ctx.requiredMethod(ScalaStaticsClass, name)
lazy val DottyPredefModule = ctx.requiredModule("dotty.DottyPredef")
lazy val DottyArraysModule = ctx.requiredModule("dotty.runtime.Arrays")
def newRefArrayMethod = ctx.requiredMethod(DottyArraysModule.moduleClass.asClass, "newRefArray")
lazy val NilModule = ctx.requiredModule("scala.collection.immutable.Nil")
lazy val PredefConformsClass = ctx.requiredClass("scala.Predef." + tpnme.Conforms)
// lazy val FunctionClass: ClassSymbol = ctx.requiredClass("scala.Function")
lazy val SingletonClass: ClassSymbol =
// needed as a synthetic class because Scala 2.x refers to it in classfiles
// but does not define it as an explicit class.
newCompleteClassSymbol(
ScalaPackageClass, tpnme.Singleton, PureInterfaceCreationFlags | Final,
List(AnyClass.typeRef), EmptyScope)
lazy val SeqClass: ClassSymbol = ctx.requiredClass("scala.collection.Seq")
lazy val Seq_apply = ctx.requiredMethod(SeqClass, nme.apply)
lazy val Seq_head = ctx.requiredMethod(SeqClass, nme.head)
lazy val ArrayClass: ClassSymbol = ctx.requiredClass("scala.Array")
lazy val Array_apply = ctx.requiredMethod(ArrayClass, nme.apply)
lazy val Array_update = ctx.requiredMethod(ArrayClass, nme.update)
lazy val Array_length = ctx.requiredMethod(ArrayClass, nme.length)
lazy val Array_clone = ctx.requiredMethod(ArrayClass, nme.clone_)
lazy val ArrayConstructor = ctx.requiredMethod(ArrayClass, nme.CONSTRUCTOR)
lazy val traversableDropMethod = ctx.requiredMethod(ScalaRuntimeClass, nme.drop)
lazy val uncheckedStableClass: ClassSymbol = ctx.requiredClass("scala.annotation.unchecked.uncheckedStable")
lazy val UnitClass = valueClassSymbol("scala.Unit", BoxedUnitClass, java.lang.Void.TYPE, UnitEnc)
lazy val BooleanClass = valueClassSymbol("scala.Boolean", BoxedBooleanClass, java.lang.Boolean.TYPE, BooleanEnc)
lazy val Boolean_! = BooleanClass.requiredMethod(nme.UNARY_!)
lazy val Boolean_&& = BooleanClass.requiredMethod(nme.ZAND)
lazy val Boolean_|| = BooleanClass.requiredMethod(nme.ZOR)
lazy val ByteClass = valueClassSymbol("scala.Byte", BoxedByteClass, java.lang.Byte.TYPE, ByteEnc)
lazy val ShortClass = valueClassSymbol("scala.Short", BoxedShortClass, java.lang.Short.TYPE, ShortEnc)
lazy val CharClass = valueClassSymbol("scala.Char", BoxedCharClass, java.lang.Character.TYPE, CharEnc)
lazy val IntClass = valueClassSymbol("scala.Int", BoxedIntClass, java.lang.Integer.TYPE, IntEnc)
lazy val Int_- = IntClass.requiredMethod(nme.MINUS, List(IntType))
lazy val Int_+ = IntClass.requiredMethod(nme.PLUS, List(IntType))
lazy val Int_/ = IntClass.requiredMethod(nme.DIV, List(IntType))
lazy val Int_* = IntClass.requiredMethod(nme.MUL, List(IntType))
lazy val Int_== = IntClass.requiredMethod(nme.EQ, List(IntType))
lazy val Int_>= = IntClass.requiredMethod(nme.GE, List(IntType))
lazy val Int_<= = IntClass.requiredMethod(nme.LE, List(IntType))
lazy val LongClass = valueClassSymbol("scala.Long", BoxedLongClass, java.lang.Long.TYPE, LongEnc)
lazy val Long_XOR_Long = LongClass.info.member(nme.XOR).requiredSymbol(
x => (x is Method) && (x.info.firstParamTypes.head isRef defn.LongClass)
)
lazy val Long_LSR_Int = LongClass.info.member(nme.LSR).requiredSymbol(
x => (x is Method) && (x.info.firstParamTypes.head isRef defn.IntClass)
)
lazy val FloatClass = valueClassSymbol("scala.Float", BoxedFloatClass, java.lang.Float.TYPE, FloatEnc)
lazy val DoubleClass = valueClassSymbol("scala.Double", BoxedDoubleClass, java.lang.Double.TYPE, DoubleEnc)
lazy val BoxedUnitClass = ctx.requiredClass("scala.runtime.BoxedUnit")
lazy val BoxedUnit_UNIT = BoxedUnitClass.linkedClass.requiredValue("UNIT")
lazy val BoxedBooleanClass = ctx.requiredClass("java.lang.Boolean")
lazy val BoxedByteClass = ctx.requiredClass("java.lang.Byte")
lazy val BoxedShortClass = ctx.requiredClass("java.lang.Short")
lazy val BoxedCharClass = ctx.requiredClass("java.lang.Character")
lazy val BoxedIntClass = ctx.requiredClass("java.lang.Integer")
lazy val BoxedLongClass = ctx.requiredClass("java.lang.Long")
lazy val BoxedFloatClass = ctx.requiredClass("java.lang.Float")
lazy val BoxedDoubleClass = ctx.requiredClass("java.lang.Double")
lazy val ByNameParamClass2x = specialPolyClass(tpnme.BYNAME_PARAM_CLASS, Covariant, AnyType)
lazy val EqualsPatternClass = specialPolyClass(tpnme.EQUALS_PATTERN, EmptyFlags, AnyType)
lazy val RepeatedParamClass = specialPolyClass(tpnme.REPEATED_PARAM_CLASS, Covariant, ObjectType, SeqType)
// fundamental classes
lazy val StringClass = ctx.requiredClass("java.lang.String")
lazy val StringModule = StringClass.linkedClass
lazy val String_+ = newMethod(StringClass, nme.raw.PLUS, methOfAny(StringType), Final)
lazy val String_valueOf_Object = StringModule.info.member(nme.valueOf).suchThat(_.info.firstParamTypes match {
case List(pt) => (pt isRef AnyClass) || (pt isRef ObjectClass)
case _ => false
}).symbol
// in scalac modified to have Any as parrent
lazy val SerializableClass = ctx.requiredClass("scala.Serializable")
lazy val JavaCloneableClass = ctx.requiredClass("java.lang.Cloneable")
lazy val StringBuilderClass = ctx.requiredClass("scala.collection.mutable.StringBuilder")
lazy val NullPointerExceptionClass = ctx.requiredClass(jnme.NPException)
lazy val MatchErrorClass = ctx.requiredClass("scala.MatchError")
lazy val MatchErrorType = MatchErrorClass.typeRef
lazy val StringAddClass = ctx.requiredClass("scala.runtime.StringAdd")
lazy val StringAdd_+ = StringAddClass.requiredMethod(nme.raw.PLUS)
lazy val PairClass = ctx.requiredClass("dotty.Pair")
lazy val PartialFunctionClass = ctx.requiredClass("scala.PartialFunction")
lazy val AbstractPartialFunctionClass = ctx.requiredClass("scala.runtime.AbstractPartialFunction")
lazy val SymbolClass = ctx.requiredClass("scala.Symbol")
lazy val ClassClass = ctx.requiredClass("java.lang.Class")
lazy val DynamicClass = ctx.requiredClass("scala.Dynamic")
lazy val OptionClass = ctx.requiredClass("scala.Option")
lazy val BoxedNumberClass = ctx.requiredClass("java.lang.Number")
lazy val ThrowableClass = ctx.requiredClass("java.lang.Throwable")
lazy val ClassCastExceptionClass = ctx.requiredClass("java.lang.ClassCastException")
lazy val JavaSerializableClass = ctx.requiredClass("java.lang.Serializable")
lazy val ComparableClass = ctx.requiredClass("java.lang.Comparable")
lazy val ProductClass = ctx.requiredClass("scala.Product")
lazy val Product_canEqual = ProductClass.requiredMethod(nme.canEqual_)
lazy val Product_productArity = ProductClass.requiredMethod(nme.productArity)
lazy val LanguageModuleClass = ctx.requiredModule("dotty.language").moduleClass.asClass
// Annotation base classes
lazy val AnnotationClass = ctx.requiredClass("scala.annotation.Annotation")
lazy val ClassfileAnnotationClass = ctx.requiredClass("scala.annotation.ClassfileAnnotation")
lazy val StaticAnnotationClass = ctx.requiredClass("scala.annotation.StaticAnnotation")
lazy val TailrecAnnotationClass = ctx.requiredClass("scala.annotation.tailrec")
lazy val RemoteAnnot = ctx.requiredClass("scala.remote")
lazy val SerialVersionUIDAnnot = ctx.requiredClass("scala.SerialVersionUID")
lazy val TransientAnnot = ctx.requiredClass("scala.transient")
lazy val NativeAnnot = ctx.requiredClass("scala.native")
lazy val ScalaStrictFPAnnot = ctx.requiredClass("scala.annotation.strictfp")
// Annotation classes
lazy val AliasAnnot = ctx.requiredClass("dotty.annotation.internal.Alias")
lazy val ChildAnnot = ctx.requiredClass("dotty.annotation.internal.Child")
lazy val RepeatedAnnot = ctx.requiredClass("dotty.annotation.internal.Repeated")
lazy val InvariantBetweenClass = ctx.requiredClass("dotty.annotation.internal.InvariantBetween")
lazy val CovariantBetweenClass = ctx.requiredClass("dotty.annotation.internal.CovariantBetween")
lazy val ContravariantBetweenClass = ctx.requiredClass("dotty.annotation.internal.ContravariantBetween")
lazy val ScalaSignatureAnnot = ctx.requiredClass("scala.reflect.ScalaSignature")
lazy val ScalaLongSignatureAnnot = ctx.requiredClass("scala.reflect.ScalaLongSignature")
lazy val DeprecatedAnnot = ctx.requiredClass("scala.deprecated")
lazy val MigrationAnnot = ctx.requiredClass("scala.annotation.migration")
lazy val AnnotationDefaultAnnot = ctx.requiredClass("dotty.annotation.internal.AnnotationDefault")
lazy val ThrowsAnnot = ctx.requiredClass("scala.throws")
lazy val UncheckedAnnot = ctx.requiredClass("scala.unchecked")
lazy val UncheckedStableAnnot = ctx.requiredClass("scala.annotation.unchecked.uncheckedStable")
lazy val UncheckedVarianceAnnot = ctx.requiredClass("scala.annotation.unchecked.uncheckedVariance")
lazy val VolatileAnnot = ctx.requiredClass("scala.volatile")
// convenient one-parameter method types
def methOfAny(tp: Type) = MethodType(List(AnyType), tp)
def methOfAnyVal(tp: Type) = MethodType(List(AnyValType), tp)
def methOfAnyRef(tp: Type) = MethodType(List(ObjectType), tp)
// Derived types
def AnyType: Type = AnyClass.typeRef
def AnyValType: Type = AnyValClass.typeRef
def ObjectType: Type = ObjectClass.typeRef
def AnyRefType: Type = AnyRefAlias.typeRef
def NothingType: Type = NothingClass.typeRef
def NullType: Type = NullClass.typeRef
def SeqType: Type = SeqClass.typeRef
def UnitType: Type = UnitClass.typeRef
def BooleanType: Type = BooleanClass.typeRef
def ByteType: Type = ByteClass.typeRef
def ShortType: Type = ShortClass.typeRef
def CharType: Type = CharClass.typeRef
def IntType: Type = IntClass.typeRef
def LongType: Type = LongClass.typeRef
def FloatType: Type = FloatClass.typeRef
def DoubleType: Type = DoubleClass.typeRef
def PairType: Type = PairClass.typeRef
def StringType: Type = StringClass.typeRef
def RepeatedParamType = RepeatedParamClass.typeRef
def ThrowableType = ThrowableClass.typeRef
def OptionType = OptionClass.typeRef
def VolatileAnnotType = VolatileAnnot.typeRef
def ClassType(arg: Type)(implicit ctx: Context) = {
val ctype = ClassClass.typeRef
if (ctx.phase.erasedTypes) ctype else ctype.appliedTo(arg)
}
/** The enumeration type, goven a value of the enumeration */
def EnumType(sym: Symbol)(implicit ctx: Context) =
// given (in java): "class A { enum E { VAL1 } }"
// - sym: the symbol of the actual enumeration value (VAL1)
// - .owner: the ModuleClassSymbol of the enumeration (object E)
// - .linkedClass: the ClassSymbol of the enumeration (class E)
sym.owner.linkedClass.typeRef
object FunctionType {
def apply(args: List[Type], resultType: Type)(implicit ctx: Context) =
FunctionClass(args.length).typeRef.appliedTo(args ::: resultType :: Nil)
def unapply(ft: Type)(implicit ctx: Context)/*: Option[(List[Type], Type)]*/ = {
// -language:keepUnions difference: unapply needs result type because inferred type
// is Some[(List[Type], Type)] | None, which is not a legal unapply type.
val tsym = ft.typeSymbol
lazy val targs = ft.argInfos
if ((FunctionClasses contains tsym) &&
(targs.length - 1 <= MaxFunctionArity) &&
(FunctionClass(targs.length - 1) == tsym)) Some(targs.init, targs.last)
else None
}
}
object ArrayType {
def apply(elem: Type)(implicit ctx: Context) =
if (ctx.erasedTypes) JavaArrayType(elem)
else ArrayClass.typeRef.appliedTo(elem :: Nil)
def unapply(tp: Type)(implicit ctx: Context): Option[Type] = tp.dealias match {
case at: RefinedType if (at isRef ArrayClass) && at.argInfos.length == 1 => Some(at.argInfos.head)
case _ => None
}
}
object MultiArrayType {
def apply(elem: Type, ndims: Int)(implicit ctx: Context): Type =
if (ndims == 0) elem else ArrayType(apply(elem, ndims - 1))
def unapply(tp: Type)(implicit ctx: Context): Option[(Type, Int)] = tp match {
case ArrayType(elemtp) =>
elemtp match {
case MultiArrayType(finalElemTp, n) => Some(finalElemTp, n + 1)
case _ => Some(elemtp, 1)
}
case _ =>
None
}
}
// ----- Symbol sets ---------------------------------------------------
lazy val AbstractFunctionClass = mkArityArray("scala.runtime.AbstractFunction", MaxFunctionArity, 0)
lazy val FunctionClass = mkArityArray("scala.Function", MaxFunctionArity, 0)
lazy val Function0_apply = FunctionClass(0).requiredMethod(nme.apply)
lazy val TupleClass = mkArityArray("scala.Tuple", MaxTupleArity, 2)
lazy val ProductNClass = mkArityArray("scala.Product", MaxTupleArity, 0)
lazy val FunctionClasses: Set[Symbol] = FunctionClass.toSet
lazy val TupleClasses: Set[Symbol] = TupleClass.toSet
lazy val ProductClasses: Set[Symbol] = ProductNClass.toSet
/** `Modules whose members are in the default namespace and their module classes */
lazy val UnqualifiedOwners = RootImports.toSet ++ RootImports.map(_.moduleClass)
lazy val PhantomClasses = Set[Symbol](AnyClass, AnyValClass, NullClass, NothingClass)
lazy val isPolymorphicAfterErasure = Set[Symbol](Any_isInstanceOf, Any_asInstanceOf, newRefArrayMethod)
lazy val RootImports = List[Symbol](JavaLangPackageVal, ScalaPackageVal, ScalaPredefModule, DottyPredefModule)
lazy val overriddenBySynthetic = Set[Symbol](Any_equals, Any_hashCode, Any_toString, Product_canEqual)
def isTupleType(tp: Type)(implicit ctx: Context) = {
val arity = tp.dealias.argInfos.length
arity <= MaxTupleArity && (tp isRef TupleClass(arity))
}
def tupleType(elems: List[Type]) = {
TupleClass(elems.size).typeRef.appliedTo(elems)
}
def isProductSubType(tp: Type)(implicit ctx: Context) =
(tp derivesFrom ProductClass) && tp.baseClasses.exists(ProductClasses contains _)
def isFunctionType(tp: Type)(implicit ctx: Context) = {
val arity = functionArity(tp)
0 <= arity && arity <= MaxFunctionArity && (tp isRef FunctionClass(arity))
}
def functionArity(tp: Type)(implicit ctx: Context) = tp.dealias.argInfos.length - 1
// ----- LambdaXYZ traits ------------------------------------------
private var myLambdaTraits: Set[Symbol] = Set()
/** The set of HigherKindedXYZ traits encountered so far */
def lambdaTraits: Set[Symbol] = myLambdaTraits
private var lambdaTraitForVariances = mutable.Map[List[Int], ClassSymbol]()
/** The HigherKinded trait corresponding to symbols `boundSyms` (which are assumed
* to be the type parameters of a higher-kided type). This is a class symbol that
* would be generated by the following schema.
*
* class LambdaXYZ extends Object with P1 with ... with Pn {
* type v_1 $hk$Arg0; ...; type v_N $hk$ArgN;
* type Apply
* }
*
* Here:
*
* - v_i are the variances of the bound symbols (i.e. +, -, or empty).
* - XYZ is a string of length N with one letter for each variant of a bound symbol,
* using `P` (positive variance), `N` (negative variance), `I` (invariant).
* - for each positive or negative variance v_i there is a parent trait Pj which
* is the same as LambdaXYZ except that it has `I` in i-th position.
*/
def lambdaTrait(vcs: List[Int]): ClassSymbol = {
assert(vcs.nonEmpty)
def varianceFlags(v: Int) = v match {
case -1 => Contravariant
case 0 => EmptyFlags
case 1 => Covariant
}
val completer = new LazyType {
def complete(denot: SymDenotation)(implicit ctx: Context): Unit = {
val cls = denot.asClass.classSymbol
val paramDecls = newScope
for (i <- 0 until vcs.length)
newTypeParam(cls, tpnme.lambdaArgName(i), varianceFlags(vcs(i)), paramDecls)
newTypeField(cls, tpnme.Apply, Covariant, paramDecls)
val parentTraitRefs =
for (i <- 0 until vcs.length if vcs(i) != 0)
yield lambdaTrait(vcs.updated(i, 0)).typeRef
denot.info = ClassInfo(
ScalaPackageClass.thisType, cls, ObjectClass.typeRef :: parentTraitRefs.toList, paramDecls)
}
}
val traitName = tpnme.lambdaTraitName(vcs)
def createTrait = {
val cls = newClassSymbol(
ScalaPackageClass,
traitName,
PureInterfaceCreationFlags | Synthetic,
completer)
myLambdaTraits += cls
cls
}
lambdaTraitForVariances.getOrElseUpdate(vcs, createTrait)
}
// ----- primitive value class machinery ------------------------------------------
lazy val ScalaNumericValueClasses: collection.Set[Symbol] = Set(
ByteClass,
ShortClass,
CharClass,
IntClass,
LongClass,
FloatClass,
DoubleClass)
lazy val ScalaValueClasses: collection.Set[Symbol] = ScalaNumericValueClasses + UnitClass + BooleanClass
lazy val ScalaBoxedClasses = ScalaValueClasses map boxedClass
private[this] val _boxedClass = mutable.Map[Symbol, Symbol]()
private[this] val _unboxedClass = mutable.Map[Symbol, Symbol]()
private[this] val _javaTypeToValueClass = mutable.Map[Class[_], Symbol]()
private[this] val _valueClassToJavaType = mutable.Map[Symbol, Class[_]]()
private[this] val _valueClassEnc = mutable.Map[Symbol, Int]()
val boxedClass: collection.Map[Symbol, Symbol] = _boxedClass
val unboxedClass: collection.Map[Symbol, Symbol] = _boxedClass
val javaTypeToValueClass: collection.Map[Class[_], Symbol] = _javaTypeToValueClass
val valueClassToJavaType: collection.Map[Symbol, Class[_]] = _valueClassToJavaType
val valueClassEnc: collection.Map[Symbol, Int] = _valueClassEnc
private def valueClassSymbol(name: String, boxed: ClassSymbol, jtype: Class[_], enc: Int): ClassSymbol = {
val vcls = ctx.requiredClass(name)
_unboxedClass(boxed) = vcls
_boxedClass(vcls) = boxed
_javaTypeToValueClass(jtype) = vcls
_valueClassToJavaType(vcls) = jtype
_valueClassEnc(vcls) = enc
vcls
}
/** The classes for which a Ref type exists. */
lazy val refClasses: collection.Set[Symbol] = ScalaNumericValueClasses + BooleanClass + ObjectClass
lazy val refClass: Map[Symbol, Symbol] =
refClasses.map(rc => rc -> ctx.requiredClass(s"scala.runtime.${rc.name}Ref")).toMap
lazy val volatileRefClass: Map[Symbol, Symbol] =
refClasses.map(rc => rc -> ctx.requiredClass(s"scala.runtime.Volatile${rc.name}Ref")).toMap
def wrapArrayMethodName(elemtp: Type): TermName = {
val cls = elemtp.classSymbol
if (cls.isPrimitiveValueClass) nme.wrapXArray(cls.name)
else if (cls.derivesFrom(ObjectClass) && !cls.isPhantomClass) nme.wrapRefArray
else nme.genericWrapArray
}
val ByteEnc = 2
val ShortEnc = ByteEnc * 3
val CharEnc = 5
val IntEnc = ShortEnc * CharEnc
val LongEnc = IntEnc * 7
val FloatEnc = LongEnc * 11
val DoubleEnc = FloatEnc * 13
val BooleanEnc = 17
val UnitEnc = 19
def isValueSubClass(cls1: Symbol, cls2: Symbol)(implicit ctx: Context) =
valueClassEnc(cls2) % valueClassEnc(cls1) == 0
// ----- Initialization ---------------------------------------------------
/** Lists core classes that don't have underlying bytecode, but are synthesized on-the-fly in every reflection universe */
lazy val syntheticCoreClasses = List(
AnyClass,
AnyRefAlias,
RepeatedParamClass,
ByNameParamClass2x,
AnyValClass,
NullClass,
NothingClass,
SingletonClass,
EqualsPatternClass,
EmptyPackageVal)
/** Lists core methods that don't have underlying bytecode, but are synthesized on-the-fly in every reflection universe */
lazy val syntheticCoreMethods = AnyMethods ++ ObjectMethods ++ List(String_+)
private[this] var _isInitialized = false
def isInitialized = _isInitialized
def init(implicit ctx: Context) = {
this.ctx = ctx
if (!_isInitialized) {
// force initialization of every symbol that is synthesized or hijacked by the compiler
val forced = syntheticCoreClasses ++ syntheticCoreMethods ++ ScalaValueClasses
_isInitialized = true
}
}
}
|