-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Definitions.scala
1862 lines (1626 loc) · 97.4 KB
/
Definitions.scala
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala
package reflect
package internal
import scala.annotation.{meta, migration, nowarn, tailrec}
import scala.collection.mutable
import Flags._
import scala.reflect.api.{Universe => ApiUniverse}
import PartialFunction.cond
trait Definitions extends api.StandardDefinitions {
self: SymbolTable =>
import rootMirror.{getModuleByName, getPackage, getClassByName, getRequiredClass, getRequiredModule, getClassIfDefined, getModuleIfDefined, getPackageIfDefined, getPackageObjectIfDefined, requiredClass, requiredModule}
object definitions extends DefinitionsClass
/** Since both the value parameter types and the result type may
* require access to the type parameter symbols, we model polymorphic
* creation as a function from those symbols to (formal types, result type).
* The Option is to distinguish between nullary methods and nilary methods.
*/
private type PolyMethodCreator = List[Symbol] => (Option[List[Type]], Type)
private def enterNewClass(owner: Symbol, name: TypeName, parents: List[Type], flags: Long = 0L): ClassSymbol = {
val clazz = owner.newClassSymbol(name, NoPosition, flags)
clazz.setInfoAndEnter(ClassInfoType(parents, newScope, clazz)).markAllCompleted()
}
private def newMethod(owner: Symbol, name: TermName, formals: List[Type], mkMeth: List[TermSymbol] => Type, flags: Long): MethodSymbol = {
val msym = owner.newMethod(name.encode, NoPosition, flags)
val params = msym.newSyntheticValueParams(formals)
val info = mkMeth(params)
msym.setInfo(info).markAllCompleted()
}
private def enterNewMethod(owner: Symbol, name: TermName, formals: List[Type], restpe: Type, flags: Long = 0L): MethodSymbol =
owner.info.decls enter newMethod(owner, name, formals, MethodType(_, restpe), flags)
private def enterNewNullaryMethod(owner: Symbol, name: TermName, restpe: Type, flags: Long): MethodSymbol =
owner.info.decls enter newMethod(owner, name, Nil, _ => NullaryMethodType(restpe), flags)
// the scala value classes
trait ValueClassDefinitions {
self: DefinitionsClass =>
import ClassfileConstants._
private[this] val nameToWeight = Map[Name, Int](
tpnme.Byte -> 2,
tpnme.Char -> 3,
tpnme.Short -> 4,
tpnme.Int -> 12,
tpnme.Long -> 24,
tpnme.Float -> 48,
tpnme.Double -> 96
)
private[this] val nameToTag = Map[Name, Char](
tpnme.Byte -> BYTE_TAG,
tpnme.Char -> CHAR_TAG,
tpnme.Short -> SHORT_TAG,
tpnme.Int -> INT_TAG,
tpnme.Long -> LONG_TAG,
tpnme.Float -> FLOAT_TAG,
tpnme.Double -> DOUBLE_TAG,
tpnme.Boolean -> BOOL_TAG,
tpnme.Unit -> VOID_TAG
)
private[Definitions] def catastrophicFailure() =
abort("Could not find value classes! This is a catastrophic failure. scala " +
scala.util.Properties.versionString)
private def valueClassSymbol(name: TypeName): ClassSymbol = {
getMember(ScalaPackageClass, name) match {
case x: ClassSymbol => x
case _ => catastrophicFailure()
}
}
private[Definitions] def classesMap[T](f: Name => T): Map[Symbol, T] = symbolsMap(ScalaValueClassesNoUnit, f)
private def symbolsMap[T](syms: List[Symbol], f: Name => T): Map[Symbol, T] = mapFrom(syms)(x => f(x.name))
private def symbolsMapFilt[T](syms: List[Symbol], p: Name => Boolean, f: Name => T) = symbolsMap(syms filter (x => p(x.name)), f)
private def boxedName(name: Name) = sn.Boxed(name.toTypeName)
lazy val abbrvTag = symbolsMap(ScalaValueClasses, nameToTag) withDefaultValue OBJECT_TAG
lazy val numericWeight = symbolsMapFilt(ScalaValueClasses, nameToWeight.keySet, nameToWeight)
lazy val boxedModule = classesMap(x => getModuleByName(boxedName(x)))
lazy val boxedClass = classesMap(x => getClassByName(boxedName(x)))
lazy val refClass = classesMap(x => getRequiredClass("scala.runtime." + x + "Ref"))
lazy val volatileRefClass = classesMap(x => getRequiredClass("scala.runtime.Volatile" + x + "Ref"))
lazy val lazyHolders = symbolsMap(ScalaValueClasses, x => getClassIfDefined("scala.runtime.Lazy" + x))
lazy val LazyRefClass = getClassIfDefined("scala.runtime.LazyRef")
lazy val LazyUnitClass = getClassIfDefined("scala.runtime.LazyUnit")
lazy val allRefClasses: Set[Symbol] = {
refClass.values.toSet ++ volatileRefClass.values.toSet ++ Set(VolatileObjectRefClass, ObjectRefClass)
}
def isNumericSubClass(sub: Symbol, sup: Symbol) = (
isNumericValueClass(sub)
&& isNumericValueClass(sup)
&& (numericWeight(sup) % numericWeight(sub) == 0)
)
/** Is symbol a numeric value class? */
def isNumericValueClass(sym: Symbol) = ScalaNumericValueClassesSet contains sym
def isGetClass(sym: Symbol) = (
sym.name == nme.getClass_ // this condition is for performance only, this is called from `Typer#stabilize`.
&& getClassMethods(sym)
)
lazy val UnitClass = valueClassSymbol(tpnme.Unit)
lazy val ByteClass = valueClassSymbol(tpnme.Byte)
lazy val ShortClass = valueClassSymbol(tpnme.Short)
lazy val CharClass = valueClassSymbol(tpnme.Char)
lazy val IntClass = valueClassSymbol(tpnme.Int)
lazy val LongClass = valueClassSymbol(tpnme.Long)
lazy val FloatClass = valueClassSymbol(tpnme.Float)
lazy val DoubleClass = valueClassSymbol(tpnme.Double)
lazy val BooleanClass = valueClassSymbol(tpnme.Boolean)
def Boolean_and = getMemberMethod(BooleanClass, nme.ZAND)
def Boolean_or = getMemberMethod(BooleanClass, nme.ZOR)
def Boolean_not = getMemberMethod(BooleanClass, nme.UNARY_!)
lazy val UnitTpe = UnitClass.tpe
lazy val ByteTpe = ByteClass.tpe
lazy val ShortTpe = ShortClass.tpe
lazy val CharTpe = CharClass.tpe
lazy val IntTpe = IntClass.tpe
lazy val LongTpe = LongClass.tpe
lazy val FloatTpe = FloatClass.tpe
lazy val DoubleTpe = DoubleClass.tpe
lazy val BooleanTpe = BooleanClass.tpe
lazy val ScalaNumericValueClasses = ScalaValueClasses filterNot Set[Symbol](UnitClass, BooleanClass)
lazy val ScalaValueClassesNoUnit = ScalaValueClasses filterNot (_ eq UnitClass)
lazy val ScalaValueClasses: List[ClassSymbol] = List(
UnitClass,
BooleanClass,
ByteClass,
ShortClass,
CharClass,
IntClass,
LongClass,
FloatClass,
DoubleClass
)
lazy val ScalaValueClassesSet: SymbolSet = new SymbolSet(ScalaValueClasses)
lazy val ScalaNumericValueClassesSet: SymbolSet = new SymbolSet(ScalaNumericValueClasses)
final class SymbolSet(syms: List[Symbol]) {
private[this] val ids: Array[Symbol] = syms.toArray
private[this] val commonOwner =
if (syms.isEmpty) null else {
val hhOwner = syms.head.rawowner
if (syms.tail.forall(_.rawowner == hhOwner)) hhOwner else null
}
final def contains(sym: Symbol): Boolean = {
if (commonOwner != null && (commonOwner ne sym.rawowner))
return false
val array = ids
var i = 0
while (i < array.length) {
if (array(i) eq sym) return true
i += 1
}
false
}
}
def ScalaPrimitiveValueClasses: List[ClassSymbol] = ScalaValueClasses
def underlyingOfValueClass(clazz: Symbol): Type =
clazz.derivedValueClassUnbox.tpe.resultType
}
abstract class DefinitionsClass extends DefinitionsApi with ValueClassDefinitions {
private[this] var isInitialized = false
def isDefinitionsInitialized = isInitialized
// It becomes tricky to create dedicated objects for other symbols because
// of initialization order issues.
lazy val JavaLangPackage = getPackage("java.lang")
lazy val JavaLangPackageClass = JavaLangPackage.moduleClass.asClass
lazy val ScalaPackage = getPackage("scala")
lazy val ScalaPackageClass = ScalaPackage.moduleClass.asClass
lazy val RuntimePackage = getPackage("scala.runtime")
lazy val RuntimePackageClass = RuntimePackage.moduleClass.asClass
def javaTypeToValueClass(jtype: Class[_]): Symbol = jtype match {
case java.lang.Void.TYPE => UnitClass
case java.lang.Byte.TYPE => ByteClass
case java.lang.Character.TYPE => CharClass
case java.lang.Short.TYPE => ShortClass
case java.lang.Integer.TYPE => IntClass
case java.lang.Long.TYPE => LongClass
case java.lang.Float.TYPE => FloatClass
case java.lang.Double.TYPE => DoubleClass
case java.lang.Boolean.TYPE => BooleanClass
case _ => NoSymbol
}
def valueClassToJavaType(sym: Symbol): Class[_] = sym match {
case UnitClass => java.lang.Void.TYPE
case ByteClass => java.lang.Byte.TYPE
case CharClass => java.lang.Character.TYPE
case ShortClass => java.lang.Short.TYPE
case IntClass => java.lang.Integer.TYPE
case LongClass => java.lang.Long.TYPE
case FloatClass => java.lang.Float.TYPE
case DoubleClass => java.lang.Double.TYPE
case BooleanClass => java.lang.Boolean.TYPE
case _ => null
}
/** Fully initialize the symbol, type, or scope.
*/
def fullyInitializeSymbol(sym: Symbol): Symbol = {
sym.initialize
// Watch out for those darn raw types on method parameters
if (sym.owner.initialize.isJavaDefined)
sym.cookJavaRawInfo()
fullyInitializeType(sym.info)
fullyInitializeType(sym.tpe_*)
sym
}
def fullyInitializeType(tp: Type): Type = {
tp.typeParams foreach fullyInitializeSymbol
mforeach(tp.paramss)(fullyInitializeSymbol)
tp
}
def fullyInitializeScope(scope: Scope): Scope = {
scope.sorted foreach fullyInitializeSymbol
scope
}
/** Is this symbol a member of Object or Any? */
def isUniversalMember(sym: Symbol) = ObjectClass isSubClass sym.owner
/** Is this symbol unimportable? Unimportable symbols include:
* - constructors, because <init> is not a real name
* - private[this] members, which cannot be referenced from anywhere else
* - members of Any or Object, because every instance will inherit a
* definition which supersedes the imported one, unless renamed
*/
def isUnimportable(sym: Symbol) = (
(sym eq NoSymbol)
|| sym.isConstructor
|| sym.isPrivateLocal
)
def isUnimportableUnlessRenamed(sym: Symbol) = isUnimportable(sym) || isUniversalMember(sym)
def isImportable(sym: Symbol) = !isUnimportable(sym)
/** Is this type equivalent to Any, AnyVal, or AnyRef? */
def isTrivialTopType(tp: Type) = (
tp =:= AnyTpe
|| tp =:= AnyValTpe
|| tp =:= AnyRefTpe
)
def isUnitType(tp: Type) = tp.typeSymbol == UnitClass && tp.annotations.isEmpty
private def fixupAsAnyTrait(tpe: Type): Type = tpe match {
case ClassInfoType(parents, decls, clazz) =>
if (parents.head.typeSymbol == AnyClass) tpe
else {
assert(parents.head.typeSymbol == ObjectClass, parents)
ClassInfoType(AnyTpe :: parents.tail, decls, clazz)
}
case PolyType(tparams, restpe) => PolyType(tparams, fixupAsAnyTrait(restpe))
case _ => throw new MatchError(tpe)
}
// top types
lazy val AnyClass = enterNewClass(ScalaPackageClass, tpnme.Any, Nil, ABSTRACT).markAllCompleted()
lazy val AnyRefClass = newAlias(ScalaPackageClass, tpnme.AnyRef, ObjectTpe).markAllCompleted()
lazy val ObjectClass = getRequiredClass("java.lang.Object")
// Cached types for core monomorphic classes
lazy val AnyRefTpe = AnyRefClass.tpe
lazy val AnyTpe = AnyClass.tpe
lazy val AnyValTpe = AnyValClass.tpe
lazy val BoxedUnitTpe = BoxedUnitClass.tpe
lazy val NothingTpe = NothingClass.tpe
lazy val NullTpe = NullClass.tpe
/** Represents `java.lang.Object` as referenced from Scala code. */
lazy val ObjectTpe = ObjectClass.tpe
/** ObjectTpeJava is a TypeRef that's structurally equal to ObjectTpe, but with its own object identity.
*
* When referenced from Java (source or bytecode), `Object` should be considered equal to Scala's `Any`,
* as these types are both conceptually the top of the subtyping lattice of the respective languages.
*
* We use `ObjectTpeJava`'s identity to equate it, but not `ObjectTpe`, to `AnyTpe` in subtyping and type equality.
*/
lazy val ObjectTpeJava = new ObjectTpeJavaRef
lazy val SerializableTpe = SerializableClass.tpe
lazy val StringTpe = StringClass.tpe
lazy val ThrowableTpe = ThrowableClass.tpe
lazy val ConstantTrue = ConstantType(Constant(true))
lazy val ConstantFalse = ConstantType(Constant(false))
lazy val ConstantNull = ConstantType(Constant(null))
lazy val AnyValClass: ClassSymbol = (ScalaPackageClass.info member tpnme.AnyVal orElse {
val anyval = enterNewClass(ScalaPackageClass, tpnme.AnyVal, AnyTpe :: Nil, ABSTRACT)
val av_constr = anyval.newClassConstructor(NoPosition)
anyval.info.decls enter av_constr
anyval.markAllCompleted()
}).asInstanceOf[ClassSymbol]
def AnyVal_getClass = getMemberMethod(AnyValClass, nme.getClass_)
// bottom types
lazy val RuntimeNothingClass = requiredClass[scala.runtime.Nothing$]
lazy val RuntimeNullClass = requiredClass[scala.runtime.Null$]
sealed abstract class BottomClassSymbol(name: TypeName, parent: Symbol) extends ClassSymbol(ScalaPackageClass, NoPosition, name) {
locally {
this initFlags ABSTRACT | FINAL
this setInfoAndEnter ClassInfoType(List(parent.tpe), newScope, this)
this.markAllCompleted()
}
final override def isBottomClass = true
final override def isThreadsafe(purpose: SymbolOps): Boolean = true
}
final object NothingClass extends BottomClassSymbol(tpnme.Nothing, AnyClass) {
override def isSubClass(that: Symbol) = true
}
final object NullClass extends BottomClassSymbol(tpnme.Null, AnyRefClass) {
override def isSubClass(that: Symbol) = (
(that eq AnyClass)
|| (that ne NothingClass) && (that isSubClass ObjectClass)
)
}
// exceptions and other throwables
lazy val ClassCastExceptionClass = requiredClass[ClassCastException]
lazy val IndexOutOfBoundsExceptionClass = getClassByName("java.lang.IndexOutOfBoundsException")
lazy val InvocationTargetExceptionClass = getClassByName("java.lang.reflect.InvocationTargetException")
lazy val MatchErrorClass = requiredClass[MatchError]
lazy val NonLocalReturnControlClass = requiredClass[scala.runtime.NonLocalReturnControl[_]]
lazy val NullPointerExceptionClass = getClassByName("java.lang.NullPointerException")
lazy val ThrowableClass = getClassByName("java.lang.Throwable")
lazy val UninitializedErrorClass = requiredClass[UninitializedFieldError]
lazy val RuntimeExceptionClass = requiredClass[RuntimeException]
lazy val IllegalArgExceptionClass = requiredClass[IllegalArgumentException]
lazy val UninitializedFieldConstructor = UninitializedErrorClass.primaryConstructor
// fundamental reference classes
lazy val PartialFunctionClass = requiredClass[PartialFunction[_,_]]
lazy val AbstractPartialFunctionClass = requiredClass[scala.runtime.AbstractPartialFunction[_,_]]
lazy val SymbolClass = requiredClass[scala.Symbol]
lazy val StringClass = requiredClass[java.lang.String]
lazy val StringModule = StringClass.linkedClassOfClass
lazy val ClassClass = requiredClass[java.lang.Class[_]]
def Class_getMethod = getMemberMethod(ClassClass, nme.getMethod_)
lazy val DynamicClass = requiredClass[Dynamic]
// fundamental modules
// Modules whose members are in the default namespace
// scala/bug#5941: ScalaPackage and JavaLangPackage are never ever shared between mirrors
// as a result, `Int` becomes `scala.Int` and `String` becomes `java.lang.String`
// I could just change `isOmittablePrefix`, but there's more to it, so I'm leaving this as a todo for now
lazy val UnqualifiedModules = List(PredefModule, ScalaPackage, JavaLangPackage)
// Those modules and their module classes
lazy val UnqualifiedOwners = UnqualifiedModules.toSet ++ UnqualifiedModules.map(_.moduleClass)
lazy val PredefModule = requiredModule[scala.Predef.type]
def Predef_??? = getMemberMethod(PredefModule, nme.???)
def Predef_locally = getMemberMethod(PredefModule, nme.locally)
def isPredefMemberNamed(sym: Symbol, name: Name) = (
(sym.name == name) && (sym.owner == PredefModule.moduleClass)
)
def wrapVarargsArrayMethod(tp: Type) = getMemberMethod(ScalaRunTimeModule, wrapVarargsArrayMethodName(tp))
/** Specialization.
*/
lazy val SpecializableModule = requiredModule[Specializable]
lazy val ScalaRunTimeModule = requiredModule[scala.runtime.ScalaRunTime.type]
lazy val SymbolModule = requiredModule[scala.Symbol.type]
def Symbol_apply = getMemberMethod(SymbolModule, nme.apply)
// classes with special meanings
lazy val ScalaNumberClass = requiredClass[scala.math.ScalaNumber]
lazy val DelayedInitClass = requiredClass[scala.DelayedInit]: @nowarn("cat=deprecation")
def delayedInitMethod = getMemberMethod(DelayedInitClass, nme.delayedInit)
lazy val TypeConstraintClass = requiredClass[scala.annotation.TypeConstraint]
lazy val SingletonClass = enterNewClass(ScalaPackageClass, tpnme.Singleton, AnyTpe :: Nil, ABSTRACT | TRAIT | FINAL).markAllCompleted()
lazy val ListOfSingletonClassTpe = SingletonClass.tpe :: Nil
lazy val SerializableClass = requiredClass[java.io.Serializable] modifyInfo fixupAsAnyTrait
lazy val ComparableClass = requiredClass[java.lang.Comparable[_]] modifyInfo fixupAsAnyTrait
lazy val JavaCloneableClass = requiredClass[java.lang.Cloneable] modifyInfo fixupAsAnyTrait
lazy val JavaNumberClass = requiredClass[java.lang.Number]
lazy val JavaEnumClass = requiredClass[java.lang.Enum[_]]
lazy val JavaUtilMap = requiredClass[java.util.Map[_, _]]
lazy val JavaUtilHashMap = requiredClass[java.util.HashMap[_, _]]
lazy val JavaRecordClass = getClassIfDefined("java.lang.Record")
lazy val ByNameParamClass = specialPolyClass(tpnme.BYNAME_PARAM_CLASS_NAME, COVARIANT)(_ => AnyTpe)
lazy val JavaRepeatedParamClass = specialPolyClass(tpnme.JAVA_REPEATED_PARAM_CLASS_NAME, COVARIANT)(tparam => arrayType(tparam.tpe))
lazy val RepeatedParamClass = specialPolyClass(tpnme.REPEATED_PARAM_CLASS_NAME, COVARIANT)(tparam => seqType(tparam.tpe))
def isByNameParamType(tp: Type) = tp.typeSymbol == ByNameParamClass
def isScalaRepeatedParamType(tp: Type) = tp.typeSymbol == RepeatedParamClass
def isJavaRepeatedParamType(tp: Type) = tp.typeSymbol == JavaRepeatedParamClass
def isRepeatedParamType(tp: Type) = isScalaRepeatedParamType(tp) || isJavaRepeatedParamType(tp)
def isRepeated(param: Symbol) = isRepeatedParamType(param.tpe_*)
def isByName(param: Symbol) = isByNameParamType(param.tpe_*)
def isCastSymbol(sym: Symbol) = sym == Any_asInstanceOf || sym == Object_asInstanceOf
def isTypeTestSymbol(sym: Symbol) = sym == Any_isInstanceOf || sym == Object_isInstanceOf
def isJavaVarArgsMethod(m: Symbol) = m.isMethod && (m.rawInfo match {
case completer: LazyType => completer.isJavaVarargsMethod
case _ => isJavaVarArgs(m.info.params)
})
def isJavaVarArgs(params: scala.collection.Seq[Symbol]) = !params.isEmpty && isJavaRepeatedParamType(params.last.tpe)
def isScalaVarArgs(params: scala.collection.Seq[Symbol]) = !params.isEmpty && isScalaRepeatedParamType(params.last.tpe)
def isVarArgsList(params: scala.collection.Seq[Symbol]) = !params.isEmpty && isRepeatedParamType(params.last.tpe)
def isVarArgTypes(formals: scala.collection.Seq[Type]) = !formals.isEmpty && isRepeatedParamType(formals.last)
def firstParamType(tpe: Type): Type = tpe.paramTypes match {
case p :: _ => p
case _ => NoType
}
def isImplicitParamss(paramss: List[List[Symbol]]) = paramss match {
case (p :: _) :: _ => p.isImplicit
case _ => false
}
@tailrec
final def hasRepeatedParam(tp: Type): Boolean = tp match {
case MethodType(formals, restpe) => isScalaVarArgs(formals) || hasRepeatedParam(restpe)
case PolyType(_, restpe) => hasRepeatedParam(restpe)
case _ => false
}
// wrapping and unwrapping
def dropByName(tp: Type): Type = elementExtract(ByNameParamClass, tp) orElse tp
def dropRepeated(tp: Type): Type = (
if (isJavaRepeatedParamType(tp)) elementExtract(JavaRepeatedParamClass, tp) orElse tp
else if (isScalaRepeatedParamType(tp)) elementExtract(RepeatedParamClass, tp) orElse tp
else tp
)
def repeatedToSingle(tp: Type): Type = elementExtract(RepeatedParamClass, tp) orElse elementExtract(JavaRepeatedParamClass, tp) orElse tp
// We don't need to deal with JavaRepeatedParamClass here, as `repeatedToSeq` is only called in the patmat translation for Scala sources.
def repeatedToSeq(tp: Type): Type = elementTransform(RepeatedParamClass, tp)(seqType) orElse tp
def seqToRepeated(tp: Type): Type = elementTransform(SeqClass, tp)(scalaRepeatedType) orElse tp
def isReferenceArray(tp: Type) = elementTest(ArrayClass, tp)(elemtp => elemtp <:< AnyRefTpe || (elemtp eq ObjectTpeJava))
def isArrayOfSymbol(tp: Type, elem: Symbol) = elementTest(ArrayClass, tp)(_.typeSymbol == elem)
def elementType(container: Symbol, tp: Type): Type = elementExtract(container, tp)
// Classes treated specially with respect to -Ywarn-unused
lazy val SubTypeClass = requiredClass[scala.<:<[_,_]]
lazy val SameTypeClass = requiredClass[scala.=:=[_,_]]
lazy val DummyImplicitClass = requiredClass[scala.DummyImplicit]
// collections classes
lazy val ConsClass = requiredClass[scala.collection.immutable.::[_]]
lazy val IteratorClass = requiredClass[scala.collection.Iterator[_]]
lazy val IterableClass = requiredClass[scala.collection.Iterable[_]]
lazy val ListClass = requiredClass[scala.collection.immutable.List[_]]
def List_cons = getMemberMethod(ListClass, nme.CONS)
@migration("SeqClass now refers to scala.collection.immutable.Seq", "2.13.0")
lazy val SeqClass = requiredClass[scala.collection.immutable.Seq[_]]
lazy val SeqFactoryClass = requiredModule[scala.collection.SeqFactory.type]
lazy val UnapplySeqWrapperClass = getTypeMember(SeqFactoryClass, tpnme.UnapplySeqWrapper)
lazy val JavaStringBuilderClass = requiredClass[java.lang.StringBuilder]
lazy val JavaStringBufferClass = requiredClass[java.lang.StringBuffer]
lazy val JavaCharSequenceClass = requiredClass[java.lang.CharSequence]
@deprecated("Use IterableClass instead of TraversableClass", "2.13.0")
def TraversableClass = IterableClass
lazy val ListModule = requiredModule[scala.collection.immutable.List.type]
def List_apply = getMemberMethod(ListModule, nme.apply)
lazy val ListModuleAlias = getMemberValue(ScalaPackageClass, nme.List)
lazy val NilModule = requiredModule[scala.collection.immutable.Nil.type]
@migration("SeqModule now refers to scala.collection.immutable.Seq", "2.13.0")
lazy val SeqModule = requiredModule[scala.collection.immutable.Seq.type]
lazy val Collection_SeqModule = requiredModule[scala.collection.Seq.type]
// arrays and their members
lazy val ArrayModule = requiredModule[scala.Array.type]
lazy val ArrayModule_overloadedApply = getMemberMethod(ArrayModule, nme.apply)
def ArrayModule_genericApply = ArrayModule_overloadedApply.suchThat(_.paramss.flatten.last.tpe.typeSymbol == ClassTagClass) // [T: ClassTag](xs: T*): Array[T]
def ArrayModule_apply(tp: Type) = ArrayModule_overloadedApply.suchThat(_.tpe.resultType =:= arrayType(tp)) // (p1: AnyVal1, ps: AnyVal1*): Array[AnyVal1]
lazy val ArrayClass = getRequiredClass("scala.Array") // requiredClass[scala.Array[_]]
lazy val Array_apply = getMemberMethod(ArrayClass, nme.apply)
lazy val Array_update = getMemberMethod(ArrayClass, nme.update)
lazy val Array_length = getMemberMethod(ArrayClass, nme.length)
lazy val Array_clone = getMemberMethod(ArrayClass, nme.clone_)
// reflection / structural types
lazy val SoftReferenceClass = requiredClass[java.lang.ref.SoftReference[_]]
lazy val MethodClass = getClassByName("java.lang.reflect.Method")
lazy val EmptyMethodCacheClass = requiredClass[scala.runtime.EmptyMethodCache]
lazy val MethodCacheClass = requiredClass[scala.runtime.MethodCache]
def methodCache_find = getMemberMethod(MethodCacheClass, nme.find_)
def methodCache_add = getMemberMethod(MethodCacheClass, nme.add_)
lazy val StructuralCallSite = getClassIfDefined("scala.runtime.StructuralCallSite")
def StructuralCallSite_bootstrap = getMemberMethod(StructuralCallSite.linkedClassOfClass, sn.Bootstrap)
// Marker for invokedynamic runtime.StructuralCall.bootstrap
lazy val StructuralCallSite_dummy = NoSymbol.newMethodSymbol(nme.apply).setInfo(NullaryMethodType(StructuralCallSite.tpe))
def StructuralCallSite_find = getMemberIfDefined(StructuralCallSite, nme.find_)
def StructuralCallSite_add = getMemberIfDefined(StructuralCallSite, nme.add_)
def StructuralCallSite_getParameterTypes = getMemberIfDefined(StructuralCallSite, nme.parameterTypes)
lazy val SymbolLiteral = getClassIfDefined("scala.runtime.SymbolLiteral")
def SymbolLiteral_bootstrap = getMemberIfDefined(SymbolLiteral.linkedClassOfClass, sn.Bootstrap)
def SymbolLiteral_dummy = NoSymbol.newMethodSymbol(nme.apply).setInfo(NullaryMethodType(SymbolModule.companionClass.tpe))
// XML
lazy val ScalaXmlTopScope = getModuleIfDefined("scala.xml.TopScope")
lazy val ScalaXmlPackage = getPackageIfDefined("scala.xml")
// scala.reflect
lazy val ReflectPackage = requiredModule[scala.reflect.`package`.type]
lazy val ReflectApiPackage = getPackageObjectIfDefined("scala.reflect.api") // defined in scala-reflect.jar, so we need to be careful
lazy val ReflectRuntimePackage = getPackageObjectIfDefined("scala.reflect.runtime") // defined in scala-reflect.jar, so we need to be careful
def ReflectRuntimeUniverse = ReflectRuntimePackage.map(sym => getDeclValue(sym, nme.universe))
def ReflectRuntimeCurrentMirror = ReflectRuntimePackage.map(sym => getDeclMethod(sym, nme.currentMirror))
lazy val UniverseClass = getClassIfDefined("scala.reflect.api.Universe") // defined in scala-reflect.jar, so we need to be careful
def UniverseInternal = getMemberValue(UniverseClass, nme.internal)
lazy val PartialManifestModule = requiredModule[scala.reflect.ClassManifestFactory.type]
lazy val FullManifestClass = requiredClass[scala.reflect.Manifest[_]]
lazy val FullManifestModule = requiredModule[scala.reflect.ManifestFactory.type]
lazy val OptManifestClass = requiredClass[scala.reflect.OptManifest[_]]
lazy val NoManifest = requiredModule[scala.reflect.NoManifest.type]
lazy val TreesClass = getClassIfDefined("scala.reflect.api.Trees") // defined in scala-reflect.jar, so we need to be careful
lazy val ExprsClass = getClassIfDefined("scala.reflect.api.Exprs") // defined in scala-reflect.jar, so we need to be careful
def ExprClass = ExprsClass.map(sym => getMemberClass(sym, tpnme.Expr))
def ExprSplice = ExprClass.map(sym => getMemberMethod(sym, nme.splice))
def ExprValue = ExprClass.map(sym => getMemberMethod(sym, nme.value))
lazy val ClassTagModule = requiredModule[scala.reflect.ClassTag[_]]
lazy val ClassTagClass = requiredClass[scala.reflect.ClassTag[_]]
lazy val TypeTagsClass = getClassIfDefined("scala.reflect.api.TypeTags") // defined in scala-reflect.jar, so we need to be careful
lazy val ApiUniverseClass = getClassIfDefined("scala.reflect.api.Universe") // defined in scala-reflect.jar, so we need to be careful
lazy val ApiQuasiquotesClass = getClassIfDefined("scala.reflect.api.Quasiquotes") // defined in scala-reflect.jar, so we need to be careful
lazy val JavaUniverseClass = getClassIfDefined("scala.reflect.api.JavaUniverse") // defined in scala-reflect.jar, so we need to be careful
lazy val MirrorClass = getClassIfDefined("scala.reflect.api.Mirror") // defined in scala-reflect.jar, so we need to be careful
lazy val TypeCreatorClass = getClassIfDefined("scala.reflect.api.TypeCreator") // defined in scala-reflect.jar, so we need to be careful
lazy val TreeCreatorClass = getClassIfDefined("scala.reflect.api.TreeCreator") // defined in scala-reflect.jar, so we need to be careful
lazy val BlackboxContextClass = getClassIfDefined("scala.reflect.macros.blackbox.Context") // defined in scala-reflect.jar, so we need to be careful
lazy val WhiteboxContextClass = getClassIfDefined("scala.reflect.macros.whitebox.Context") // defined in scala-reflect.jar, so we need to be careful
def MacroContextPrefix = BlackboxContextClass.map(sym => getMemberMethod(sym, nme.prefix))
def MacroContextPrefixType = BlackboxContextClass.map(sym => getTypeMember(sym, tpnme.PrefixType))
def MacroContextUniverse = BlackboxContextClass.map(sym => getMemberMethod(sym, nme.universe))
def MacroContextExprClass = BlackboxContextClass.map(sym => getTypeMember(sym, tpnme.Expr))
def MacroContextWeakTypeTagClass = BlackboxContextClass.map(sym => getTypeMember(sym, tpnme.WeakTypeTag))
def MacroContextTreeType = BlackboxContextClass.map(sym => getTypeMember(sym, tpnme.Tree))
lazy val MacroImplAnnotation = requiredClass[scala.reflect.macros.internal.macroImpl]
/**Implementation of a class that is identical to `scala.reflect.macros.internal.macroImpl`,
* but only exists at compile time
*/
lazy val MacroImplLocationAnnotation = {
val internalPkg = MacroImplAnnotation.owner.suchThat(_.isPackageClass)
val MacroImplLocation = internalPkg.newClassSymbol(tpnme.macroImplLocation, NoPosition)
MacroImplLocation.setPrivateWithin(ScalaPackage)
MacroImplLocation.setInfoAndEnter(ClassInfoType(AnnotationClass.tpe :: Nil, newScope, MacroImplLocation))
// getter
MacroImplLocation.newMethod(
name = nme.unpickledMacroImpl,
newFlags = STABLE | ACCESSOR | PARAMACCESSOR
).setInfoAndEnter(internal.nullaryMethodType(AnyTpe)).markAllCompleted()
// field
MacroImplLocation.newValue(
name = nme.unpickledMacroImpl,
newFlags = PRIVATE | LOCAL | PARAMACCESSOR
).setInfoAndEnter(AnyTpe).markAllCompleted()
// ctor
val ctor = MacroImplLocation.newConstructor(NoPosition)
val param = ctor.newValueParameter(nme.unpickledMacroImpl).setInfo(AnyTpe)
ctor.setInfoAndEnter(MethodType(param :: Nil, MacroImplLocation.tpe)).markAllCompleted()
MacroImplLocation.addAnnotation(
sym = CompileTimeOnlyAttr,
arg = Literal(Constant(
s"illegal reference to $MacroImplLocation, it is an implementation detail of unpickling TASTy"))
)
MacroImplLocation.markAllCompleted()
}
lazy val StringContextClass = requiredClass[scala.StringContext]
lazy val StringContextModule = requiredModule[scala.StringContext.type]
lazy val ValueOfClass = getClassIfDefined("scala.ValueOf")
// scala/bug#8392 a reflection universe on classpath may not have
// quasiquotes, if e.g. crosstyping with -Xsource on
lazy val QuasiquoteClass = if (ApiUniverseClass != NoSymbol) ApiQuasiquotesClass.info.decl(tpnme.Quasiquote) else NoSymbol
lazy val QuasiquoteClass_api = if (QuasiquoteClass != NoSymbol) QuasiquoteClass.info.decl(tpnme.api) else NoSymbol
lazy val QuasiquoteClass_api_apply = if (QuasiquoteClass_api != NoSymbol) getDeclMethod(QuasiquoteClass_api, nme.apply) else NoSymbol
lazy val QuasiquoteClass_api_unapply = if (QuasiquoteClass_api != NoSymbol) getDeclMethod(QuasiquoteClass_api, nme.unapply) else NoSymbol
lazy val ScalaSignatureAnnotation = requiredClass[scala.reflect.ScalaSignature]
lazy val ScalaLongSignatureAnnotation = requiredClass[scala.reflect.ScalaLongSignature]
lazy val MethodHandleClass = getClassIfDefined("java.lang.invoke.MethodHandle")
lazy val VarHandleClass = getClassIfDefined("java.lang.invoke.VarHandle")
// Option classes
lazy val OptionClass: ClassSymbol = requiredClass[Option[_]]
lazy val OptionModule: ModuleSymbol = requiredModule[scala.Option.type]
lazy val SomeClass: ClassSymbol = requiredClass[Some[_]]
lazy val NoneModule: ModuleSymbol = requiredModule[scala.None.type]
lazy val SomeModule: ModuleSymbol = requiredModule[scala.Some.type]
// Serialization
lazy val ModuleSerializationProxyClass: ClassSymbol = requiredClass[scala.runtime.ModuleSerializationProxy]
def compilerTypeFromTag(tt: ApiUniverse # WeakTypeTag[_]): Type = tt.in(rootMirror).tpe
def compilerSymbolFromTag(tt: ApiUniverse # WeakTypeTag[_]): Symbol = tt.in(rootMirror).tpe.typeSymbol
// The given symbol is a method with the right name and signature to be a runnable java program.
def isJavaMainMethod(sym: Symbol) = (sym.name == nme.main) && (sym.info match {
case MethodType(p :: Nil, restpe) => isArrayOfSymbol(p.tpe, StringClass) && restpe.typeSymbol == UnitClass
case _ => false
})
// The given class has a main method.
def hasJavaMainMethod(sym: Symbol): Boolean = sym.tpe.member(nme.main).alternatives.exists(isJavaMainMethod)
class VarArityClass(name: String, maxArity: Int, countFrom: Int = 0, init: Option[ClassSymbol] = None) extends VarArityClassApi {
private[this] val offset = countFrom - init.size
private def isDefinedAt(i: Int) = i < seq.length + offset && i >= offset
val seq: IndexedSeq[ClassSymbol] = (init ++: countFrom.to(maxArity).map { i => getRequiredClass("scala." + name + i) }).toVector
private[this] val symSet = new SymbolSet(seq.toList)
def contains(sym: Symbol): Boolean = symSet.contains(sym)
def apply(i: Int) = if (isDefinedAt(i)) seq(i - offset) else NoSymbol
def specificType(args: List[Type], others: List[Type] = Nil): Type = {
val arity = args.length
if (!isDefinedAt(arity)) NoType
else appliedType(apply(arity), args ::: others)
}
}
// would be created synthetically for the default args. We call all objects in this method from the generated code
// in JavaUniverseForce, so it is clearer to define this explicitly define this in source.
object VarArityClass
val MaxTupleArity, MaxProductArity, MaxFunctionArity = 22
// A unit test checks these are kept in synch with the library.
val MaxTupleAritySpecialized, MaxProductAritySpecialized, MaxFunctionAritySpecialized = 2
lazy val ProductClass = new VarArityClass("Product", MaxProductArity, countFrom = 1, init = Some(UnitClass))
lazy val TupleClass = new VarArityClass("Tuple", MaxTupleArity, countFrom = 1)
lazy val FunctionClass = new VarArityClass("Function", MaxFunctionArity)
lazy val AbstractFunctionClass = new VarArityClass("runtime.AbstractFunction", MaxFunctionArity)
/** Creators for TupleN, ProductN, FunctionN. */
def tupleType(elems: List[Type]) = TupleClass.specificType(elems)
def functionType(formals: List[Type], restpe: Type) = FunctionClass.specificType(formals, restpe :: Nil)
def abstractFunctionType(formals: List[Type], restpe: Type) = AbstractFunctionClass.specificType(formals, restpe :: Nil)
def wrapVarargsArrayMethodName(elemtp: Type): TermName = elemtp.typeSymbol match {
case ByteClass => nme.wrapByteArray
case ShortClass => nme.wrapShortArray
case CharClass => nme.wrapCharArray
case IntClass => nme.wrapIntArray
case LongClass => nme.wrapLongArray
case FloatClass => nme.wrapFloatArray
case DoubleClass => nme.wrapDoubleArray
case BooleanClass => nme.wrapBooleanArray
case UnitClass => nme.wrapUnitArray
case _ =>
if ((elemtp <:< AnyRefTpe) && !isPhantomClass(elemtp.typeSymbol)) nme.wrapRefArray
else nme.genericWrapArray
}
def isTupleSymbol(sym: Symbol) = TupleClass contains unspecializedSymbol(sym)
def isFunctionSymbol(sym: Symbol) = FunctionClass contains unspecializedSymbol(sym)
def isAbstractFunctionSymbol(sym: Symbol) = AbstractFunctionClass contains unspecializedSymbol(sym)
def isProductNSymbol(sym: Symbol) = ProductClass contains unspecializedSymbol(sym)
lazy val TryClass = requiredClass[scala.util.Try[_]]
lazy val FailureClass = requiredClass[scala.util.Failure[_]]
lazy val SuccessClass = requiredClass[scala.util.Success[_]]
lazy val FutureClass = requiredClass[scala.concurrent.Future[_]]
lazy val PromiseClass = requiredClass[scala.concurrent.Promise[_]]
lazy val NonFatalClass = requiredClass[scala.util.control.NonFatal.type]
def unspecializedSymbol(sym: Symbol): Symbol = {
if (sym hasFlag SPECIALIZED) {
// add initialization from its generic class constructor
val genericName = nme.unspecializedName(sym.name)
val member = sym.owner.info.decl(genericName.toTypeName)
member
}
else sym
}
def unspecializedTypeArgs(tp: Type): List[Type] =
(tp baseType unspecializedSymbol(tp.typeSymbolDirect)).typeArgs
object MacroContextType {
def unapply(tp: Type) = {
def isOneOfContextTypes(tp: Type) =
tp =:= BlackboxContextClass.tpe || tp =:= WhiteboxContextClass.tpe
def isPrefix(sym: Symbol) =
sym.allOverriddenSymbols.contains(MacroContextPrefixType)
tp.dealias match {
case RefinedType(List(tp), Scope(sym)) if isOneOfContextTypes(tp) && isPrefix(sym) => Some(tp)
case tp if isOneOfContextTypes(tp) => Some(tp)
case _ => None
}
}
}
def isMacroContextType(tp: Type) = MacroContextType.unapply(tp).isDefined
def isWhiteboxContextType(tp: Type) =
isMacroContextType(tp) && (tp <:< WhiteboxContextClass.tpe)
private def macroBundleParamInfo(tp: Type) = {
val ctor = tp.erasure.typeSymbol.primaryConstructor
ctor.paramss match {
case List(List(c)) =>
val sym = c.info.typeSymbol
val isContextCompatible = sym.isNonBottomSubClass(BlackboxContextClass) || sym.isNonBottomSubClass(WhiteboxContextClass)
if (isContextCompatible) c.info else NoType
case _ =>
NoType
}
}
def looksLikeMacroBundleType(tp: Type) =
macroBundleParamInfo(tp) != NoType
def isMacroBundleType(tp: Type) = {
val isMonomorphic = tp.typeSymbol.typeParams.isEmpty
val isContextCompatible = isMacroContextType(macroBundleParamInfo(tp))
val hasSingleConstructor = !tp.declaration(nme.CONSTRUCTOR).isOverloaded
val nonAbstract = !tp.erasure.typeSymbol.isAbstractClass
isMonomorphic && isContextCompatible && hasSingleConstructor && nonAbstract
}
def isBlackboxMacroBundleType(tp: Type) = {
val isBundle = isMacroBundleType(tp)
val unwrappedContext = MacroContextType.unapply(macroBundleParamInfo(tp)).getOrElse(NoType)
val isBlackbox = unwrappedContext =:= BlackboxContextClass.tpe
isBundle && isBlackbox
}
def isListType(tp: Type) = tp.typeSymbol.isNonBottomSubClass(ListClass)
def isIterableType(tp: Type) = tp.typeSymbol.isNonBottomSubClass(IterableClass)
// These "direct" calls perform no dealiasing. They are most needed when
// printing types when one wants to preserve the true nature of the type.
def isFunctionTypeDirect(tp: Type) = !tp.isHigherKinded && isFunctionSymbol(tp.typeSymbolDirect)
def isTupleTypeDirect(tp: Type) = !tp.isHigherKinded && isTupleSymbol(tp.typeSymbolDirect)
// Note that these call .dealiasWiden and not .normalize, the latter of which
// tends to change the course of events by forcing types.
def isFunctionType(tp: Type) = isFunctionTypeDirect(tp.dealiasWiden)
// Are we expecting something function-ish? This considers FunctionN / SAM / ProtoType that matches functions
def isFunctionProto(pt: Type): Boolean =
(isFunctionType(pt)
|| (pt match { case pt: ProtoType => pt.expectsFunctionType case _ => false }) // TODO: this does not work for Function0
|| samOf(pt).exists
)
// @requires pt.typeSymbol == PartialFunctionClass
def partialFunctionArgResTypeFromProto(pt: Type): (Type, Type) =
pt match {
case oap: OverloadedArgProto => (oap.hofParamTypes.head, WildcardType)
case _ =>
val arg :: res :: Nil = pt.baseType(PartialFunctionClass).typeArgs: @unchecked
(arg, res)
}
// the number of arguments expected by the function described by `tp` (a FunctionN or SAM type),
// or `-1` if `tp` does not represent a function type or SAM
// for use during typers (after fields, samOf will be confused by abstract accessors for trait fields)
def functionArityFromType(tp: Type) = {
val dealiased = tp.dealiasWiden
if (isFunctionTypeDirect(dealiased)) dealiased.typeArgs.length - 1
else samOf(tp) match {
case samSym if samSym.exists => samSym.info.params.length
case _ => -1
}
}
// the argument types expected by the function described by `tp` (a FunctionN or PartialFunction or SAM type),
// or `Nil` if `tp` does not represent a function type or PartialFunction or SAM (or if it happens to be Function0...)
def functionOrPfOrSamArgTypes(tp: Type): List[Type] = {
val dealiased = tp.dealiasWiden
if (isFunctionTypeDirect(dealiased) || isPartialFunctionType(dealiased)) dealiased.typeArgs.init
else samOf(tp) match {
case samSym if samSym.exists => tp.memberInfo(samSym).paramTypes
case _ => Nil
}
}
/**
* Convert a SAM type to the corresponding FunctionType,
* extrapolating BoundedWildcardTypes in the process
* (no type precision is lost by the extrapolation,
* but this facilitates dealing with the types arising from Java's use-site variance).
*/
def samToFunctionType(tp: Type, sam: Symbol = NoSymbol): Type =
tp match {
case pt: ProtoType => pt.asFunctionType
case _ =>
val samSym = sam orElse samOf(tp)
def correspondingFunctionSymbol = {
val numVparams = samSym.info.params.length
if (numVparams > definitions.MaxFunctionArity) NoSymbol
else FunctionClass(numVparams)
}
if (samSym.exists && tp.typeSymbol != correspondingFunctionSymbol) // don't treat Functions as SAMs
wildcardExtrapolation(methodToExpressionTp(tp memberInfo samSym))
else NoType
}
/** Automatically perform the following conversions on expression types:
* A method type becomes the corresponding function type.
* A nullary method type becomes its result type.
* Implicit parameters are skipped.
* This method seems to be performance critical.
*/
final def methodToExpressionTp(tp: Type): Type = tp match {
case PolyType(_, restpe) =>
logResult(sm"""|Normalizing PolyType in infer:
| was: $restpe
| now""")(methodToExpressionTp(restpe))
case mt @ MethodType(_, restpe) if mt.isImplicit => methodToExpressionTp(restpe)
case mt @ MethodType(_, restpe) if !mt.isDependentMethodType =>
if (phase.erasedTypes) FunctionClass(mt.params.length).tpe
else functionType(mt.paramTypes, methodToExpressionTp(restpe))
case NullaryMethodType(restpe) => methodToExpressionTp(restpe)
case ExistentialType(tparams, qtpe) => newExistentialType(tparams, methodToExpressionTp(qtpe))
case _ => tp // @MAT aliases already handled by subtyping
}
// the SAM's parameters and the Function's formals must have the same length
// (varargs etc don't come into play, as we're comparing signatures, not checking an application)
def samMatchesFunctionBasedOnArity(sam: Symbol, formals: List[Any]): Boolean =
sam.exists && sameLength(sam.info.params, formals)
def isTupleType(tp: Type) = isTupleTypeDirect(tp.dealiasWiden)
def tupleComponents(tp: Type) = tp.dealiasWiden.typeArgs
lazy val ProductRootClass: ClassSymbol = requiredClass[scala.Product]
def Product_productArity = getMemberMethod(ProductRootClass, nme.productArity)
def Product_productElement = getMemberMethod(ProductRootClass, nme.productElement)
def Product_productElementName = getMemberIfDefined(ProductRootClass, nme.productElementName)
def Product_iterator = getMemberMethod(ProductRootClass, nme.productIterator)
def Product_productPrefix = getMemberMethod(ProductRootClass, nme.productPrefix)
def Product_canEqual = getMemberMethod(ProductRootClass, nme.canEqual_)
def productProj(z:Symbol, j: Int): TermSymbol = getMemberValue(z, nme.productAccessorName(j))
/** if tpe <: ProductN[T1,...,TN], returns List(T1,...,TN) else Nil */
@deprecated("no longer used", "2.11.0") def getProductArgs(tpe: Type): List[Type] = tpe.baseClasses find isProductNSymbol match {
case Some(x) => tpe.baseType(x).typeArgs
case _ => Nil
}
@deprecated("no longer used", "2.11.0") def unapplyUnwrap(tpe:Type) = tpe.finalResultType.dealiasWiden match {
case RefinedType(p :: _, _) => p.dealiasWiden
case tp => tp
}
def dropNullaryMethod(tp: Type) = tp match {
case NullaryMethodType(restpe) => restpe
case _ => tp
}
/** An implementation of finalResultType which does only what
* finalResultType is documented to do. Defining it externally to
* Type helps ensure people can't come to depend on accidental
* aspects of its behavior. This is all of it!
*/
@tailrec
final def finalResultType(tp: Type): Type = tp match {
case PolyType(_, restpe) => finalResultType(restpe)
case MethodType(_, restpe) => finalResultType(restpe)
case NullaryMethodType(restpe) => finalResultType(restpe)
case _ => tp
}
/** Similarly, putting all the isStable logic in one place.
* This makes it like 1000x easier to see the overall logic
* of the method.
*/
@tailrec
final def isStable(tp: Type): Boolean = tp match {
case NoPrefix | _: SingletonType => true
case TypeRef(_, NothingClass | SingletonClass, _) => true
case TypeRef(_, sym, _) if sym.isAbstractType => tp.upperBound.typeSymbol.isSubClass(SingletonClass)
case TypeRef(pre, sym, _) if sym.isModuleClass => isStable(pre)
case _: TypeRef => val norm = tp.normalize; (norm ne tp) && isStable(norm)
case TypeVar(origin, _) => isStable(origin)
case ExistentialType(qs, underlying) => isStable(deriveTypeWithWildcards(qs)(underlying))
case _: SimpleTypeProxy => isStable(tp.underlying)
case _ => false
}
final def isVolatile(tp: Type): Boolean = {
// need to be careful not to fall into an infinite recursion here
// because volatile checking is done before all cycles are detected.
// the case to avoid is an abstract type directly or
// indirectly upper-bounded by itself. See #2918
def isVolatileAbstractType: Boolean = {
def sym = tp.typeSymbol
def volatileUpperBound = isVolatile(tp.upperBound)
def safeIsVolatile = (
if (volatileRecursions < TypeConstants.LogVolatileThreshold)
volatileUpperBound
// we can return true when pendingVolatiles contains sym, because
// a cycle will be detected afterwards and an error will result anyway.
else pendingVolatiles(sym) || {
pendingVolatiles += sym
try volatileUpperBound finally pendingVolatiles -= sym
}
)
volatileRecursions += 1
try safeIsVolatile finally volatileRecursions -= 1
}
/* A refined type P1 with ... with Pn { decls } is volatile if
* one of the parent types Pi is an abstract type, and
* either i > 1, or decls or a following parent Pj, j > 1, contributes
* an abstract member.
* A type contributes an abstract member if it has an abstract member which
* is also a member of the whole refined type. A scope `decls` contributes
* an abstract member if it has an abstract definition which is also
* a member of the whole type.
*/
def isVolatileRefinedType: Boolean = {
val RefinedType(parents, decls) = (tp: @unchecked)
def isVisibleDeferred(m: Symbol) = m.isDeferred && ((tp nonPrivateMember m.name).alternatives contains m)
def contributesAbstractMembers(p: Type) = p.deferredMembers exists isVisibleDeferred
def dropConcreteParents = parents dropWhile (p => !p.typeSymbol.isAbstractType)
(parents exists isVolatile) || {
dropConcreteParents match {
case Nil => false
case ps => (ps ne parents) || (ps.tail exists contributesAbstractMembers) || (decls exists isVisibleDeferred)
}
}
}
def isVolatileTypeRef(tr: TypeRef) = {
val dealised = tr.dealias
if (dealised ne tr) isVolatile(dealised)
else if (tr.sym.isAbstractType) isVolatileAbstractType
else false
}
tp match {
case ThisType(_) => false
case SingleType(_, sym) => isVolatile(tp.underlying) && (sym.hasVolatileType || !sym.isStable)
case NullaryMethodType(restpe) => isVolatile(restpe)
case PolyType(_, restpe) => isVolatile(restpe)
case tr: TypeRef => isVolatileTypeRef(tr)
case RefinedType(_, _) => isVolatileRefinedType
case TypeVar(origin, _) => isVolatile(origin)
case _: SimpleTypeProxy => isVolatile(tp.underlying)
case _ => false
}
}
private[this] var volatileRecursions: Int = 0
private[this] val pendingVolatiles = mutable.HashSet[Symbol]()
def functionNBaseType(tp: Type): Type = tp.baseClasses find isFunctionSymbol match {
case Some(sym) => tp baseType unspecializedSymbol(sym)
case _ => tp
}
def isPartialFunctionType(tp: Type): Boolean = {
val sym = tp.typeSymbol
(sym eq PartialFunctionClass) || (sym eq AbstractPartialFunctionClass)
}
private[this] val samCache = perRunCaches.newAnyRefMap[Symbol, Symbol]()