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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
|
/* NSC -- new Scala compiler
* Copyright 2005-2011 LAMP/EPFL
* @author Martin Odersky
*/
package scala.tools.nsc
package typechecker
import scala.collection.mutable
import scala.annotation.tailrec
import scala.ref.WeakReference
import symtab.Flags._
import scala.tools.nsc.io.AbstractFile
/** This trait declares methods to create symbols and to enter them into scopes.
*
* @author Martin Odersky
* @version 1.0
*/
trait Namers extends MethodSynthesis {
self: Analyzer =>
import global._
import definitions._
private var _lockedCount = 0
def lockedCount = this._lockedCount
/** Replaces any Idents for which cond is true with fresh TypeTrees().
* Does the same for any trees containing EmptyTrees.
*/
private class TypeTreeSubstituter(cond: Name => Boolean) extends Transformer {
override def transform(tree: Tree): Tree = tree match {
case Ident(name) if cond(name) => TypeTree()
case _ => super.transform(tree)
}
def apply(tree: Tree) = {
val r = transform(tree)
if (r.exists(_.isEmpty)) TypeTree()
else r
}
}
private def isTemplateContext(ctx: Context): Boolean = ctx.tree match {
case Template(_, _, _) => true
case Import(_, _) => isTemplateContext(ctx.outer)
case _ => false
}
private class NormalNamer(context: Context) extends Namer(context)
def newNamer(context: Context): Namer = new NormalNamer(context)
def newNamerFor(context: Context, tree: Tree): Namer =
newNamer(context.makeNewScope(tree, tree.symbol))
// In the typeCompleter (templateSig) of a case class (resp it's module),
// synthetic `copy` (reps `apply`, `unapply`) methods are added. To compute
// their signatures, the corresponding ClassDef is needed.
// During naming, for each case class module symbol, the corresponding ClassDef
// is stored in this map. The map is cleared lazily, i.e. when the new symbol
// is created with the same name, the old one (if present) is wiped out, or the
// entry is deleted when it is used and no longer needed.
private val classOfModuleClass = perRunCaches.newWeakMap[Symbol, WeakReference[ClassDef]]()
// Default getters of constructors are added to the companion object in the
// typeCompleter of the constructor (methodSig). To compute the signature,
// we need the ClassDef. To create and enter the symbols into the companion
// object, we need the templateNamer of that module class.
// This map is extended during naming of classes, the Namer is added in when
// it's available, i.e. in the type completer (templateSig) of the module class.
private[typechecker] val classAndNamerOfModule = perRunCaches.newMap[Symbol, (ClassDef, Namer)]()
def resetNamer() {
classAndNamerOfModule.clear()
}
abstract class Namer(val context: Context) extends MethodSynth with NamerContextErrors { thisNamer =>
import NamerErrorGen._
val typer = newTyper(context)
private lazy val innerNamer =
if (isTemplateContext(context)) createInnerNamer() else this
def createNamer(tree: Tree): Namer = {
val sym = tree match {
case ModuleDef(_, _, _) => tree.symbol.moduleClass
case _ => tree.symbol
}
newNamer(context.makeNewScope(tree, sym))
}
def createInnerNamer() = {
newNamer(context.make(context.tree, owner, newScope))
}
def createPrimaryConstructorParameterNamer: Namer = { //todo: can we merge this with SCCmode?
val classContext = context.enclClass
val outerContext = classContext.outer.outer
val paramContext = outerContext.makeNewScope(outerContext.tree, outerContext.owner)
owner.unsafeTypeParams foreach (paramContext.scope enter _)
newNamer(paramContext)
}
def enclosingNamerWithScope(scope: Scope) = {
var cx = context
while (cx != NoContext && cx.scope != scope) cx = cx.outer
if (cx == NoContext || cx == context) thisNamer
else newNamer(cx)
}
def enterValueParams(vparamss: List[List[ValDef]]): List[List[Symbol]] = {
mmap(vparamss) { param =>
val sym = assignSymbol(param, param.name, mask = ValueParameterFlags)
setPrivateWithin(param, sym)
enterInScope(sym)
sym setInfo monoTypeCompleter(param)
}
}
protected def owner = context.owner
private def contextFile = context.unit.source.file
private def typeErrorHandler[T](tree: Tree, alt: T): PartialFunction[Throwable, T] = {
case ex: TypeError =>
// H@ need to ensure that we handle only cyclic references
TypeSigError(tree, ex)
alt
}
// PRIVATE | LOCAL are fields generated for primary constructor arguments
// @PP: ...or fields declared as private[this]. PARAMACCESSOR marks constructor arguments.
// Neither gets accessors so the code is as far as I know still correct.
def noEnterGetterSetter(vd: ValDef) = !vd.mods.isLazy && (
!owner.isClass
|| (vd.mods.isPrivateLocal && !vd.mods.isCaseAccessor)
|| (vd.name startsWith nme.OUTER)
|| (context.unit.isJava)
)
def noFinishGetterSetter(vd: ValDef) = (
vd.mods.isPrivateLocal
|| vd.symbol.isModuleVar
|| vd.symbol.isLazy
)
def setPrivateWithin[Sym <: Symbol](tree: Tree, sym: Sym, mods: Modifiers): Sym =
if (sym.isPrivateLocal || !mods.hasAccessBoundary) sym
else sym setPrivateWithin (
typer.qualifyingClass(tree, mods.privateWithin, true) match {
case None =>
NoSymbol
case Some(sym) =>
sym
}
)
def setPrivateWithin(tree: MemberDef, sym: Symbol): Symbol =
setPrivateWithin(tree, sym, tree.mods)
def inConstructorFlag: Long =
if (owner.isConstructor && !context.inConstructorSuffix || owner.isEarlyInitialized) INCONSTRUCTOR
else 0l
def moduleClassFlags(moduleFlags: Long) =
(moduleFlags & ModuleToClassFlags) | inConstructorFlag
private def resetKeepingFlags(sym: Symbol, keeping: Long): Symbol = {
val keep = sym.flags & keeping
sym reset NoType
sym setFlag keep
}
def updatePosFlags(sym: Symbol, pos: Position, flags: Long): Symbol = {
debuglog("[overwrite] " + sym)
resetKeepingFlags(sym, LOCKED)
sym setFlag flags
sym setPos pos
if (sym.isModule && sym.moduleClass != NoSymbol)
updatePosFlags(sym.moduleClass, pos, moduleClassFlags(flags))
if (sym.owner.isPackageClass) {
val companion = companionSymbolOf(sym, context)
if (companion != NoSymbol) {
val assignNoType = companion.rawInfo match {
case _: SymLoader => true
case tp => tp.isComplete && (runId(sym.validTo) != currentRunId)
}
// pre-set linked symbol to NoType, in case it is not loaded together with this symbol.
if (assignNoType)
companion setInfo NoType
}
}
sym
}
def namerOf(sym: Symbol): Namer = {
val usePrimary = sym.isTerm && (
(sym.isParamAccessor)
|| (sym.isParameter && sym.owner.isPrimaryConstructor)
)
if (usePrimary) createPrimaryConstructorParameterNamer
else innerNamer
}
protected def conflict(newS: Symbol, oldS: Symbol) = (
( !oldS.isSourceMethod
|| nme.isSetterName(newS.name)
|| newS.owner.isPackageClass
) &&
!( // @M: allow repeated use of `_` for higher-order type params
(newS.owner.isTypeParameter || newS.owner.isAbstractType)
// FIXME: name comparisons not successful, are these underscores
// sometimes nme.WILDCARD and sometimes tpnme.WILDCARD?
&& (newS.name.toString == nme.WILDCARD.toString)
)
)
private def allowsOverload(sym: Symbol) = (
sym.isSourceMethod && sym.owner.isClass && !sym.owner.isPackageClass
)
private def inCurrentScope(m: Symbol): Boolean = {
if (owner.isClass) owner == m.owner
else m.owner.isClass && context.scope == m.owner.info.decls
}
/** Enter symbol into context's scope and return symbol itself */
def enterInScope(sym: Symbol): Symbol = enterInScope(sym, context.scope)
/** Enter symbol into given scope and return symbol itself */
def enterInScope(sym: Symbol, scope: Scope): Symbol = {
// allow for overloaded methods
if (!allowsOverload(sym)) {
val prev = scope.lookupEntry(sym.name)
if ((prev ne null) && prev.owner == scope && conflict(sym, prev.sym)) {
DoubleDefError(sym, prev.sym)
sym setInfo ErrorType
scope unlink prev.sym // let them co-exist...
// FIXME: The comment "let them co-exist" is confusing given that the
// line it comments unlinks one of them. What does it intend?
}
}
scope enter sym
}
def enterSym(tree: Tree): Context = {
def dispatch() = {
var returnContext = this.context
tree match {
case tree @ PackageDef(_, _) => enterPackage(tree)
case tree @ ClassDef(_, _, _, _) => enterClassDef(tree)
case tree @ ModuleDef(_, _, _) => enterModuleDef(tree)
case tree @ ValDef(_, _, _, _) => enterValDef(tree)
case tree @ DefDef(_, _, _, _, _, _) => enterDefDef(tree)
case tree @ TypeDef(_, _, _, _) => enterTypeDef(tree)
case DocDef(_, defn) => enterSym(defn)
case tree @ Import(_, _) =>
assignSymbol(tree)
returnContext = context.makeNewImport(tree)
case _ =>
}
returnContext
}
tree.symbol match {
case NoSymbol => try dispatch() catch typeErrorHandler(tree, this.context)
case sym => enterExistingSym(sym)
}
}
/** Creates a new symbol and assigns it to the tree, returning the symbol
*/
def assignSymbol(tree: Tree): Symbol =
logAssignSymbol(tree, tree match {
case PackageDef(pid, _) => createPackageSymbol(tree.pos, pid)
case Import(_, _) => createImportSymbol(tree)
case mdef: MemberDef => createMemberSymbol(mdef, mdef.name, -1L)
case _ => abort("Unexpected tree: " + tree)
})
def assignSymbol(tree: MemberDef, name: Name, mask: Long): Symbol =
logAssignSymbol(tree, createMemberSymbol(tree, name, mask))
def assignAndEnterSymbol(tree: MemberDef): Symbol = {
val sym = assignSymbol(tree, tree.name, -1L)
setPrivateWithin(tree, sym)
enterInScope(sym)
}
def assignAndEnterFinishedSymbol(tree: MemberDef): Symbol = {
val sym = assignAndEnterSymbol(tree)
sym setInfo completerOf(tree)
// log("[+info] " + sym.fullLocationString)
sym
}
private def logAssignSymbol(tree: Tree, sym: Symbol): Symbol = {
sym.name.toTermName match {
case nme.IMPORT | nme.OUTER | nme.ANON_CLASS_NAME | nme.ANON_FUN_NAME | nme.CONSTRUCTOR => ()
case _ =>
log("[+symbol] " + sym.debugLocationString)
}
tree.symbol = sym
sym
}
/** Create a new symbol at the context owner based on the given tree.
* A different name can be given. If the modifier flags should not be
* be transferred to the symbol as they are, supply a mask containing
* the flags to keep.
*/
private def createMemberSymbol(tree: MemberDef, name: Name, mask: Long): Symbol = {
val pos = tree.pos
val isParameter = tree.mods.isParameter
val flags = tree.mods.flags & mask
tree match {
case TypeDef(_, _, _, _) if isParameter => owner.newTypeParameter(name.toTypeName, pos, flags)
case TypeDef(_, _, _, _) => owner.newTypeSymbol(name.toTypeName, pos, flags)
case DefDef(_, nme.CONSTRUCTOR, _, _, _, _) => owner.newConstructor(pos, flags)
case DefDef(_, _, _, _, _, _) => owner.newMethod(name.toTermName, pos, flags)
case ClassDef(_, _, _, _) => owner.newClassSymbol(name.toTypeName, pos, flags)
case ModuleDef(_, _, _) => owner.newModule(name, pos, flags)
case ValDef(_, _, _, _) if isParameter => owner.newValueParameter(name, pos, flags)
case PackageDef(pid, _) => createPackageSymbol(pos, pid)
case ValDef(_, _, _, _) => owner.newValue(name, pos, flags)
}
}
private def createFieldSymbol(tree: ValDef): TermSymbol =
owner.newValue(nme.getterToLocal(tree.name), tree.pos, tree.mods.flags & FieldFlags | PrivateLocal)
private def createImportSymbol(tree: Tree) =
NoSymbol.newImport(tree.pos) setInfo completerOf(tree)
/** All PackageClassInfoTypes come from here. */
private def createPackageSymbol(pos: Position, pid: RefTree): Symbol = {
val pkgOwner = pid match {
case Ident(_) => if (owner == EmptyPackageClass) RootClass else owner
case Select(qual: RefTree, _) => createPackageSymbol(pos, qual).moduleClass
}
val existing = pkgOwner.info.decls.lookup(pid.name)
if (existing.isPackage && pkgOwner == existing.owner)
existing
else {
val pkg = pkgOwner.newPackage(pid.name.toTermName, pos)
val pkgClass = pkg.moduleClass
val pkgClassInfo = new PackageClassInfoType(newPackageScope(pkgClass), pkgClass)
pkgClass setInfo pkgClassInfo
pkg setInfo pkgClass.tpe
enterInScope(pkg, pkgOwner.info.decls)
}
}
private def enterClassSymbol(tree: ClassDef, clazz: ClassSymbol): Symbol = {
val file = contextFile
if (clazz.sourceFile != null && clazz.sourceFile != contextFile)
debugwarn("!!! Source mismatch in " + clazz + ": " + clazz.sourceFile + " vs. " + contextFile)
clazz.sourceFile = contextFile
if (clazz.sourceFile != null) {
assert(currentRun.canRedefine(clazz) || clazz.sourceFile == currentRun.symSource(clazz), clazz.sourceFile)
currentRun.symSource(clazz) = clazz.sourceFile
}
registerTopLevelSym(clazz)
assert(clazz.name.toString.indexOf('(') < 0, clazz.name) // )
clazz
}
def enterClassSymbol(tree: ClassDef): Symbol = {
val existing = context.scope.lookup(tree.name)
val isRedefinition = (
existing.isType
&& existing.owner.isPackageClass
&& context.scope == existing.owner.info.decls
&& currentRun.canRedefine(existing)
)
val clazz: Symbol = {
if (isRedefinition) {
updatePosFlags(existing, tree.pos, tree.mods.flags)
setPrivateWithin(tree, existing)
existing
}
else assignAndEnterSymbol(tree) setFlag inConstructorFlag
}
clazz match {
case csym: ClassSymbol if csym.owner.isPackageClass => enterClassSymbol(tree, csym)
case _ => clazz
}
}
def enterModuleDef(tree: ModuleDef) = {
val sym = enterModuleSymbol(tree)
sym.moduleClass setInfo namerOf(sym).moduleClassTypeCompleter(tree)
sym setInfo completerOf(tree)
}
/** Enter a module symbol. The tree parameter can be either
* a module definition or a class definition.
*/
def enterModuleSymbol(tree : ModuleDef): Symbol = {
var m: Symbol = context.scope.lookup(tree.name)
val moduleFlags = tree.mods.flags | MODULE
if (m.isModule && !m.isPackage && inCurrentScope(m) && (currentRun.canRedefine(m) || m.isSynthetic)) {
updatePosFlags(m, tree.pos, moduleFlags)
setPrivateWithin(tree, m)
if (m.moduleClass != NoSymbol)
setPrivateWithin(tree, m.moduleClass)
context.unit.synthetics -= m
tree.symbol = m
}
else {
m = assignAndEnterSymbol(tree)
m.moduleClass setFlag moduleClassFlags(moduleFlags)
setPrivateWithin(tree, m.moduleClass)
}
if (m.owner.isPackageClass && !m.isPackage) {
m.moduleClass.sourceFile = contextFile
currentRun.symSource(m) = m.moduleClass.sourceFile
registerTopLevelSym(m)
}
m
}
def enterSyms(trees: List[Tree]): Namer = {
trees.foldLeft(this: Namer) { (namer, t) =>
val ctx = namer enterSym t
if (ctx eq namer.context) namer
else newNamer(ctx)
}
}
def applicableTypeParams(owner: Symbol): List[Symbol] =
if (owner.isTerm || owner.isPackageClass) Nil
else applicableTypeParams(owner.owner) ::: owner.typeParams
/** If no companion object for clazz exists yet, create one by applying `creator` to
* class definition tree.
* @return the companion object symbol.
*/
def ensureCompanionObject(cdef: ClassDef, creator: ClassDef => Tree = companionModuleDef(_)): Symbol = {
val m = companionSymbolOf(cdef.symbol, context)
// @luc: not sure why "currentRun.compiles(m)" is needed, things breaks
// otherwise. documentation welcome.
//
// @PP: I tried to reverse engineer said documentation. The only tests
// which fail are buildmanager tests, as follows. Given A.scala:
// case class Foo()
// If you recompile A.scala, the Changes Map is
// Map(class Foo -> Nil, object Foo -> Nil)
// But if you remove the 'currentRun.compiles(m)' condition, it is
// Map(class Foo -> Nil)
// What exactly this implies and whether this is a sensible way to
// enforce it, I don't know.
//
// @martin: currentRun.compiles is needed because we might have a stale
// companion object from another run in scope. In that case we should still
// overwrite the object. I.e.
// Compile run #1: object Foo { ... }
// Compile run #2: case class Foo ...
// The object Foo is still in scope, but because it is not compiled in current run
// it should be ditched and a new one created.
if (m != NoSymbol && currentRun.compiles(m)) m
else enterSyntheticSym(atPos(cdef.pos.focus)(creator(cdef)))
}
private def checkSelectors(tree: Import): Unit = {
import DuplicatesErrorKinds._
val Import(expr, selectors) = tree
val base = expr.tpe
def checkNotRedundant(pos: Position, from: Name, to0: Name) {
def check(to: Name) = {
val e = context.scope.lookupEntry(to)
if (e != null && e.owner == context.scope && e.sym.exists)
typer.permanentlyHiddenWarning(pos, to0, e.sym)
else if (context ne context.enclClass) {
val defSym = context.prefix.member(to) filter (
sym => sym.exists && context.isAccessible(sym, context.prefix, false))
if (defSym != NoSymbol)
typer.permanentlyHiddenWarning(pos, to0, defSym)
}
}
if (!tree.symbol.isSynthetic && expr.symbol != null && !expr.symbol.isInterpreterWrapper) {
if (base.member(from) != NoSymbol)
check(to0)
if (base.member(from.toTypeName) != NoSymbol)
check(to0.toTypeName)
}
}
def checkSelector(s: ImportSelector) = {
val ImportSelector(from, fromPos, to, _) = s
def isValid(original: Name) =
original.bothNames forall (x => (base nonLocalMember x) == NoSymbol)
if (from != nme.WILDCARD && base != ErrorType) {
if (isValid(from)) {
if (currentRun.compileSourceFor(expr, from)) {
// side effecting, apparently
typeSig(tree)
}
// for Java code importing Scala objects
else if (!nme.isModuleName(from) || isValid(nme.stripModuleSuffix(from))) {
typer.TyperErrorGen.NotAMemberError(tree, expr, from)
typer.infer.setError(tree)
}
}
// Setting the position at the import means that if there is
// more than one hidden name, the second will not be warned.
// So it is the position of the actual hidden name.
checkNotRedundant(tree.pos withPoint fromPos, from, to)
}
}
def noDuplicates(names: List[Name], check: DuplicatesErrorKinds.Value) {
def loop(xs: List[Name]): Unit = xs match {
case Nil => ()
case hd :: tl =>
if (hd == nme.WILDCARD || !(tl contains hd)) loop(tl)
else DuplicatesError(tree, hd, check)
}
loop(names filterNot (x => x == null || x == nme.WILDCARD))
}
selectors foreach checkSelector
// checks on the whole set
noDuplicates(selectors map (_.name), RenamedTwice)
noDuplicates(selectors map (_.rename), AppearsTwice)
}
def enterCopyMethodOrGetter(tree: Tree, tparams: List[TypeDef]): Symbol = {
val sym = tree.symbol
val lazyType = completerOf(tree, tparams)
def completeCopyFirst = sym.isSynthetic && (!sym.hasDefaultFlag || sym.owner.info.member(nme.copy).isSynthetic)
def completeCopyMethod(clazz: Symbol) {
// the 'copy' method of case classes needs a special type completer to make
// bug0054.scala (and others) work. the copy method has to take exactly the same
// parameter types as the primary constructor.
val constructorType = clazz.primaryConstructor.tpe
val subst = new SubstSymMap(clazz.typeParams, tparams map (_.symbol))
val vparamss = tree match { case x: DefDef => x.vparamss ; case _ => Nil }
val cparamss = constructorType.paramss
map2(vparamss, cparamss)((vparams, cparams) =>
map2(vparams, cparams)((param, cparam) =>
// need to clone the type cparam.tpe???
// problem is: we don't have the new owner yet (the new param symbol)
param.tpt setType subst(cparam.tpe)
)
)
}
sym setInfo {
mkTypeCompleter(tree) { copySym =>
if (completeCopyFirst)
completeCopyMethod(copySym.owner)
lazyType complete sym
}
}
}
def completerOf(tree: Tree): TypeCompleter = completerOf(tree, treeInfo.typeParameters(tree))
def completerOf(tree: Tree, tparams: List[TypeDef]): TypeCompleter = {
val mono = namerOf(tree.symbol) monoTypeCompleter tree
if (tparams.isEmpty) mono
else {
//@M! TypeDef's type params are handled differently
//@M e.g., in [A[x <: B], B], A and B are entered first as both are in scope in the definition of x
//@M x is only in scope in `A[x <: B]'
if (!tree.symbol.isAbstractType) //@M TODO: change to isTypeMember ?
createNamer(tree) enterSyms tparams
new PolyTypeCompleter(tparams, mono, tree, context) //@M
}
}
def enterIfNotThere(sym: Symbol) {
val scope = context.scope
@tailrec def search(e: ScopeEntry) {
if ((e eq null) || (e.owner ne scope))
scope enter sym
else if (e.sym ne sym) // otherwise, aborts since we found sym
search(e.tail)
}
search(scope lookupEntry sym.name)
}
def enterValDef(tree: ValDef) {
if (noEnterGetterSetter(tree))
assignAndEnterFinishedSymbol(tree)
else
enterGetterSetter(tree)
}
def enterLazyVal(tree: ValDef, lazyAccessor: Symbol): TermSymbol = {
// If the owner is not a class, this is a lazy val from a method,
// with no associated field. It has an accessor with $lzy appended to its name and
// its flags are set differently. The implicit flag is reset because otherwise
// a local implicit "lazy val x" will create an ambiguity with itself
// via "x$lzy" as can be seen in test #3927.
val sym = (
if (owner.isClass) createFieldSymbol(tree)
else owner.newValue(tree.name append nme.LAZY_LOCAL, tree.pos, tree.mods.flags & ~IMPLICIT)
)
enterValSymbol(tree, sym setFlag MUTABLE setLazyAccessor lazyAccessor)
}
def enterStrictVal(tree: ValDef): TermSymbol = {
enterValSymbol(tree, createFieldSymbol(tree))
}
def enterValSymbol(tree: ValDef, sym: TermSymbol): TermSymbol = {
enterInScope(sym)
sym setInfo namerOf(sym).monoTypeCompleter(tree)
}
def enterPackage(tree: PackageDef) {
val sym = assignSymbol(tree)
newNamer(context.make(tree, sym.moduleClass, sym.info.decls)) enterSyms tree.stats
}
def enterTypeDef(tree: TypeDef) = assignAndEnterFinishedSymbol(tree)
def enterDefDef(tree: DefDef): Unit = tree match {
case DefDef(_, nme.CONSTRUCTOR, _, _, _, _) =>
assignAndEnterFinishedSymbol(tree)
case DefDef(mods, name, tparams, _, _, _) =>
val bridgeFlag = if (mods hasAnnotationNamed tpnme.bridgeAnnot) BRIDGE else 0
val sym = assignAndEnterSymbol(tree) setFlag bridgeFlag
if (name == nme.copy || tree.symbol.name.startsWith(nme.copy + nme.DEFAULT_GETTER_STRING))
enterCopyMethodOrGetter(tree, tparams)
else
sym setInfo completerOf(tree, tparams)
if (mods hasFlag MACRO) {
if (!(sym.owner.isClass && sym.owner.isStatic))
context.error(tree.pos, "macro definition must appear in globally accessible class")
}
}
def enterClassDef(tree: ClassDef) {
val ClassDef(mods, name, tparams, impl) = tree
tree.symbol = enterClassSymbol(tree)
tree.symbol setInfo completerOf(tree)
if (mods.isCase) {
if (treeInfo.firstConstructorArgs(impl.body).size > MaxFunctionArity)
MaxParametersCaseClassError(tree)
val m = ensureCompanionObject(tree, caseModuleDef)
classOfModuleClass(m.moduleClass) = new WeakReference(tree)
}
val hasDefault = impl.body exists {
case DefDef(_, nme.CONSTRUCTOR, _, vparamss, _, _) => mexists(vparamss)(_.mods.hasDefault)
case _ => false
}
if (hasDefault) {
val m = ensureCompanionObject(tree)
classAndNamerOfModule(m) = (tree, null)
}
val hasMacro = impl.body exists {
case DefDef(mods, _, _, _, _, _) => mods hasFlag MACRO
case _ => false
}
if (hasMacro) {
val m = ensureCompanionObject(tree)
classOfModuleClass(m.moduleClass) = new WeakReference(tree)
}
val owner = tree.symbol.owner
if (owner.isPackageObjectClass) {
context.unit.warning(tree.pos,
"it is not recommended to define classes/objects inside of package objects.\n" +
"If possible, define " + tree.symbol + " in " + owner.skipPackageObject + " instead."
)
}
// Suggested location only.
if (mods.isImplicit)
enterImplicitClass(tree)
}
// this logic is needed in case typer was interrupted half
// way through and then comes back to do the tree again. In
// that case the definitions that were already attributed as
// well as any default parameters of such methods need to be
// re-entered in the current scope.
protected def enterExistingSym(sym: Symbol): Context = {
if (forInteractive && sym != null && sym.owner.isTerm) {
enterIfNotThere(sym)
if (sym.isLazy) {
val acc = sym.lazyAccessor
if (acc != NoSymbol) enterIfNotThere(acc)
}
defaultParametersOfMethod(sym) foreach enterIfNotThere
}
this.context
}
def enterSyntheticSym(tree: Tree): Symbol = {
enterSym(tree)
context.unit.synthetics(tree.symbol) = tree
tree.symbol
}
// --- Lazy Type Assignment --------------------------------------------------
def initializeLowerBounds(tp: Type): Type = {
tp match {
case TypeBounds(lo, _) =>
// check that lower bound is not an F-bound
for (TypeRef(_, sym, _) <- lo)
sym.initialize
case _ =>
}
tp
}
def monoTypeCompleter(tree: Tree) = mkTypeCompleter(tree) { sym =>
logAndValidate(sym) {
val tp = initializeLowerBounds(typeSig(tree))
sym setInfo {
if (sym.isJavaDefined) RestrictJavaArraysMap(tp)
else tp
}
// this early test is there to avoid infinite baseTypes when
// adding setters and getters --> bug798
val needsCycleCheck = (sym.isAliasType || sym.isAbstractType) && !sym.isParameter
if (needsCycleCheck && !typer.checkNonCyclic(tree.pos, tp))
sym setInfo ErrorType
}
// tree match {
// case ClassDef(_, _, _, impl) =>
// val parentsOK = (
// treeInfo.isInterface(sym, impl.body)
// || (sym eq ArrayClass)
// || (sym isSubClass AnyValClass)
// )
// if (!parentsOK)
// ensureParent(sym, AnyRefClass)
// case _ => ()
// }
}
def moduleClassTypeCompleter(tree: Tree) = {
mkTypeCompleter(tree) { sym =>
val moduleSymbol = tree.symbol
assert(moduleSymbol.moduleClass == sym, moduleSymbol.moduleClass)
moduleSymbol.info // sets moduleClass info as a side effect.
}
}
def accessorTypeCompleter(tree: ValDef, isSetter: Boolean = false) = mkTypeCompleter(tree) { sym =>
logAndValidate(sym) {
sym setInfo {
if (isSetter)
MethodType(List(sym.newSyntheticValueParam(typeSig(tree))), UnitClass.tpe)
else
NullaryMethodType(typeSig(tree))
}
}
}
def selfTypeCompleter(tree: Tree) = mkTypeCompleter(tree) { sym =>
val selftpe = typer.typedType(tree).tpe
sym setInfo {
if (selftpe.typeSymbol isNonBottomSubClass sym.owner) selftpe
else intersectionType(List(sym.owner.tpe, selftpe))
}
}
/** This method has a big impact on the eventual compiled code.
* At this point many values have the most specific possible
* type (e.g. in val x = 42, x's type is Int(42), not Int) but
* most need to be widened to avoid undesirable propagation of
* those singleton types.
*
* However, the compilation of pattern matches into switch
* statements depends on constant folding, which will only take
* place for those values which aren't widened. The "final"
* modifier is the present means of signaling that a constant
* value should not be widened, so it has a use even in situations
* whether it is otherwise redundant (such as in a singleton.)
*/
private def widenIfNecessary(sym: Symbol, tpe: Type, pt: Type): Type = {
val getter =
if (sym.isValue && sym.owner.isClass && sym.isPrivate)
sym.getter(sym.owner)
else sym
def isHidden(tp: Type): Boolean = tp match {
case SingleType(pre, sym) =>
(sym isLessAccessibleThan getter) || isHidden(pre)
case ThisType(sym) =>
sym isLessAccessibleThan getter
case p: SimpleTypeProxy =>
isHidden(p.underlying)
case _ =>
false
}
val tpe1 = dropRepeatedParamType(tpe.deconst)
val tpe2 = tpe1.widen
// This infers Foo.type instead of "object Foo"
// See Infer#adjustTypeArgs for the polymorphic case.
if (tpe.typeSymbolDirect.isModuleClass) tpe1
else if (sym.isVariable || sym.isMethod && !sym.hasAccessorFlag)
if (tpe2 <:< pt) tpe2 else tpe1
else if (isHidden(tpe)) tpe2
// In an attempt to make pattern matches involving method local vals
// compilable into switches, for a time I had a more generous condition:
// `if (sym.isFinal || sym.isLocal) tpe else tpe1`
// This led to issues with expressions like classOf[List[_]] which apparently
// depend on being deconst-ed here, so this is again the original:
else if (!sym.isFinal) tpe1
else tpe
}
/** Computes the type of the body in a ValDef or DefDef, and
* assigns the type to the tpt's node. Returns the type.
*/
private def assignTypeToTree(tree: ValOrDefDef, defnTyper: Typer, pt: Type): Type = {
// compute result type from rhs
val typedBody = defnTyper.computeType(tree.rhs, pt)
val sym = if (owner.isMethod) owner else tree.symbol
val typedDefn = widenIfNecessary(sym, typedBody, pt)
assignTypeToTree(tree, typedDefn)
}
private def assignTypeToTree(tree: ValOrDefDef, tpe: Type): Type = {
tree.tpt defineType tpe setPos tree.pos.focus
tree.tpt.tpe
}
// owner is the class with the self type
def enterSelf(self: ValDef) {
val ValDef(_, name, tpt, _) = self
if (self eq emptyValDef)
return
val hasName = name != nme.WILDCARD
val hasType = !tpt.isEmpty
if (!hasType)
tpt defineType NoType
if (hasType || hasName) {
owner.typeOfThis =
if (hasType) selfTypeCompleter(tpt)
else owner.tpe
}
val sym = (
if (hasType) owner.thisSym setPos self.pos
else if (hasName) owner.thisSym
else owner.newThisSym(self.pos) setInfo owner.tpe
)
if (hasName)
sym.name = name
self.symbol = context.scope enter sym
}
private def templateSig(templ: Template): Type = {
val clazz = context.owner
def checkParent(tpt: Tree): Type = {
val tp = tpt.tpe
val inheritsSelf = tp.typeSymbol == owner
if (inheritsSelf)
InheritsItselfError(tpt)
if (inheritsSelf || tp.isError) AnyRefClass.tpe
else tp
}
val parents = typer.parentTypes(templ) map checkParent
enterSelf(templ.self)
val decls = newScope
val templateNamer = newNamer(context.make(templ, clazz, decls))
templateNamer enterSyms templ.body
// add apply and unapply methods to companion objects of case classes,
// unless they exist already; here, "clazz" is the module class
if (clazz.isModuleClass) {
Namers.this.classOfModuleClass get clazz foreach { cdefRef =>
val cdef = cdefRef()
if (cdef.mods.isCase) addApplyUnapply(cdef, templateNamer)
if (settings.Xmacros.value) addMacroMethods(cdef.impl, templateNamer)
classOfModuleClass -= clazz
}
if (settings.Xmacros.value) addMacroMethods(templ, templateNamer)
}
// add the copy method to case classes; this needs to be done here, not in SyntheticMethods, because
// the namer phase must traverse this copy method to create default getters for its parameters.
// here, clazz is the ClassSymbol of the case class (not the module).
// @check: this seems to work only if the type completer of the class runs before the one of the
// module class: the one from the module class removes the entry from classOfModuleClass (see above).
if (clazz.isClass && !clazz.hasModuleFlag) {
val modClass = companionSymbolOf(clazz, context).moduleClass
Namers.this.classOfModuleClass get modClass map { cdefRef =>
val cdef = cdefRef()
def hasCopy(decls: Scope) = (decls lookup nme.copy) != NoSymbol
if (cdef.mods.isCase && !hasCopy(decls) &&
!parents.exists(p => hasCopy(p.typeSymbol.info.decls)) &&
!parents.flatMap(_.baseClasses).distinct.exists(bc => hasCopy(bc.info.decls)))
addCopyMethod(cdef, templateNamer)
}
}
// if default getters (for constructor defaults) need to be added to that module, here's the namer
// to use. clazz is the ModuleClass. sourceModule works also for classes defined in methods.
val module = clazz.sourceModule
classAndNamerOfModule get module foreach {
case (cdef, _) =>
classAndNamerOfModule(module) = (cdef, templateNamer)
}
ClassInfoType(parents, decls, clazz)
}
private def classSig(tparams: List[TypeDef], impl: Template): Type = {
val tparams0 = typer.reenterTypeParams(tparams)
val resultType = templateSig(impl)
GenPolyType(tparams0, resultType)
}
private def methodSig(ddef: DefDef, mods: Modifiers, tparams: List[TypeDef],
vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree): Type = {
val meth = owner
val clazz = meth.owner
// enters the skolemized version into scope, returns the deSkolemized symbols
val tparamSyms = typer.reenterTypeParams(tparams)
// since the skolemized tparams are in scope, the TypeRefs in vparamSymss refer to skolemized tparams
var vparamSymss = enterValueParams(vparamss)
// DEPMETTODO: do we need to skolemize value parameter symbols?
if (tpt.isEmpty && meth.name == nme.CONSTRUCTOR) {
tpt defineType context.enclClass.owner.tpe
tpt setPos meth.pos.focus
}
var resultPt = if (tpt.isEmpty) WildcardType else typer.typedType(tpt).tpe
val site = clazz.thisType
/** Called for all value parameter lists, right to left
* @param vparams the symbols of one parameter list
* @param restpe the result type (possibly a MethodType)
*/
def makeMethodType(vparams: List[Symbol], restpe: Type) = {
// TODODEPMET: check that we actually don't need to do anything here
// new dependent method types: probably OK already, since 'enterValueParams' above
// enters them in scope, and all have a lazy type. so they may depend on other params. but: need to
// check that params only depend on ones in earlier sections, not the same. (done by checkDependencies,
// so re-use / adapt that)
if (owner.isJavaDefined)
// TODODEPMET necessary?? new dependent types: replace symbols in restpe with the ones in vparams
JavaMethodType(vparams map (p => p setInfo objToAny(p.tpe)), restpe)
else
MethodType(vparams, restpe)
}
def thisMethodType(restpe: Type) = {
val checkDependencies = new DependentTypeChecker(context)(this)
checkDependencies check vparamSymss
// DEPMETTODO: check not needed when they become on by default
checkDependencies(restpe)
GenPolyType(
tparamSyms, // deSkolemized symbols -- TODO: check that their infos don't refer to method args?
if (vparamSymss.isEmpty) NullaryMethodType(restpe)
// vparamss refer (if they do) to skolemized tparams
else (vparamSymss :\ restpe) (makeMethodType)
)
}
def transformedResult =
thisMethodType(resultPt).substSym(tparams map (_.symbol), tparamSyms)
// luc: added .substSym from skolemized to deSkolemized
// site.memberType(sym): PolyType(tparams, MethodType(..., ...))
// ==> all references to tparams are deSkolemized
// thisMethodType: tparams in PolyType are deSkolemized, the references in the MethodTypes are skolemized.
// ==> the two didn't match
//
// for instance, B.foo would not override A.foo, and the default on parameter b would not be inherited
// class A { def foo[T](a: T)(b: T = a) = a }
// class B extends A { override def foo[U](a: U)(b: U) = b }
def overriddenSymbol =
intersectionType(clazz.info.parents).nonPrivateMember(meth.name).filter { sym =>
sym != NoSymbol && (site.memberType(sym) matches transformedResult)
}
// TODO: see whether this or something similar would work instead.
//
// def overriddenSymbol = meth.nextOverriddenSymbol
// fill in result type and parameter types from overridden symbol if there is a unique one.
if (clazz.isClass && (tpt.isEmpty || mexists(vparamss)(_.tpt.isEmpty))) {
// try to complete from matching definition in base type
mforeach(vparamss)(v => if (v.tpt.isEmpty) v.symbol setInfo WildcardType)
val overridden = overriddenSymbol
if (overridden != NoSymbol && !overridden.isOverloaded) {
overridden.cookJavaRawInfo() // #3404 xform java rawtypes into existentials
resultPt = site.memberType(overridden) match {
case PolyType(tparams, rt) => rt.substSym(tparams, tparamSyms)
case mt => mt
}
for (vparams <- vparamss) {
var pps = resultPt.params
for (vparam <- vparams) {
if (vparam.tpt.isEmpty) {
val paramtpe = pps.head.tpe
vparam.symbol setInfo paramtpe
vparam.tpt defineType paramtpe setPos vparam.pos.focus
}
pps = pps.tail
}
resultPt = resultPt.resultType
}
resultPt match {
case NullaryMethodType(rtpe) => resultPt = rtpe
case MethodType(List(), rtpe) => resultPt = rtpe
case _ =>
}
if (tpt.isEmpty) {
// provisionally assign `meth` a method type with inherited result type
// that way, we can leave out the result type even if method is recursive.
meth setInfo thisMethodType(resultPt)
}
}
}
// Add a () parameter section if this overrides some method with () parameters.
if (clazz.isClass && vparamss.isEmpty && overriddenSymbol.alternatives.exists(
_.info.isInstanceOf[MethodType])) {
vparamSymss = List(List())
}
mforeach(vparamss) { vparam =>
if (vparam.tpt.isEmpty) {
MissingParameterOrValTypeError(vparam)
vparam.tpt defineType ErrorType
}
}
addDefaultGetters(meth, vparamss, tparams, overriddenSymbol)
thisMethodType({
val rt = (
if (!tpt.isEmpty) {
typer.typedType(tpt).tpe
} else if (meth.isMacro) {
assignTypeToTree(ddef, AnyClass.tpe)
} else {
// replace deSkolemized symbols with skolemized ones
// (for resultPt computed by looking at overridden symbol, right?)
val pt = resultPt.substSym(tparamSyms, tparams map (_.symbol))
assignTypeToTree(ddef, typer, pt)
}
)
// #2382: return type of default getters are always @uncheckedVariance
if (meth.hasDefaultFlag)
rt.withAnnotation(AnnotationInfo(uncheckedVarianceClass.tpe, List(), List()))
else rt
})
}
/**
* For every default argument, insert a method computing that default
*
* Also adds the "override" and "defaultparam" (for inherited defaults) flags
* Typer is too late, if an inherited default is used before the method is
* typechecked, the corresponding param would not yet have the "defaultparam"
* flag.
*/
private def addDefaultGetters(meth: Symbol, vparamss: List[List[ValDef]], tparams: List[TypeDef], overriddenSymbol: => Symbol) {
val clazz = meth.owner
val isConstr = meth.isConstructor
val overridden = if (isConstr || !clazz.isClass) NoSymbol else overriddenSymbol
val overrides = overridden != NoSymbol && !overridden.isOverloaded
// value parameters of the base class (whose defaults might be overridden)
var baseParamss = (vparamss, overridden.tpe.paramss) match {
// match empty and missing parameter list
case (Nil, List(Nil)) => Nil
case (List(Nil), Nil) => List(Nil)
case (_, paramss) => paramss
}
assert(
!overrides || vparamss.length == baseParamss.length,
"" + meth.fullName + ", "+ overridden.fullName
)
// cache the namer used for entering the default getter symbols
var ownerNamer: Option[Namer] = None
var moduleNamer: Option[(ClassDef, Namer)] = None
var posCounter = 1
// For each value parameter, create the getter method if it has a
// default argument. previous denotes the parameter lists which
// are on the left side of the current one. These get added to the
// default getter. Example:
//
// def foo(a: Int)(b: Int = a) becomes
// foo$default$1(a: Int) = a
//
vparamss.foldLeft(Nil: List[List[ValDef]]) { (previous, vparams) =>
assert(!overrides || vparams.length == baseParamss.head.length, ""+ meth.fullName + ", "+ overridden.fullName)
var baseParams = if (overrides) baseParamss.head else Nil
for (vparam <- vparams) {
val sym = vparam.symbol
// true if the corresponding parameter of the base class has a default argument
val baseHasDefault = overrides && baseParams.head.hasDefaultFlag
if (sym.hasDefaultFlag) {
// generate a default getter for that argument
val oflag = if (baseHasDefault) OVERRIDE else 0
val name = nme.defaultGetterName(meth.name, posCounter)
// Create trees for the defaultGetter. Uses tools from Unapplies.scala
var deftParams = tparams map copyUntyped[TypeDef]
val defvParamss = mmap(previous) { p =>
// in the default getter, remove the default parameter
val p1 = atPos(p.pos.focus) { ValDef(p.mods &~ DEFAULTPARAM, p.name, p.tpt.duplicate, EmptyTree) }
UnTyper.traverse(p1)
p1
}
val parentNamer = if (isConstr) {
val (cdef, nmr) = moduleNamer.getOrElse {
val module = companionSymbolOf(clazz, context)
module.initialize // call type completer (typedTemplate), adds the
// module's templateNamer to classAndNamerOfModule
classAndNamerOfModule get module match {
case s @ Some((cdef, nmr)) if nmr != null =>
moduleNamer = s
(cdef, nmr)
case _ =>
return // fix #3649 (prevent crash in erroneous source code)
// nmr == null can happen in IDE; this is really an ugly hack on top[ of an ugly hack but it seems to work
}
}
deftParams = cdef.tparams map copyUntypedInvariant
nmr
}
else ownerNamer getOrElse {
val ctx = context.nextEnclosing(c => c.scope.toList.contains(meth))
assert(ctx != NoContext, meth)
val nmr = newNamer(ctx)
ownerNamer = Some(nmr)
nmr
}
// If the parameter type mentions any type parameter of the method, let the compiler infer the
// return type of the default getter => allow "def foo[T](x: T = 1)" to compile.
// This is better than always using Wildcard for inferring the result type, for example in
// def f(i: Int, m: Int => Int = identity _) = m(i)
// if we use Wildcard as expected, we get "Nothing => Nothing", and the default is not usable.
val names = deftParams map { case TypeDef(_, name, _, _) => name }
val subst = new TypeTreeSubstituter(names contains _)
val defTpt = subst(copyUntyped(vparam.tpt match {
// default getter for by-name params
case AppliedTypeTree(_, List(arg)) if sym.hasFlag(BYNAMEPARAM) => arg
case t => t
}))
val defRhs = copyUntyped(vparam.rhs)
val defaultTree = atPos(vparam.pos.focus) {
DefDef(
Modifiers(meth.flags & DefaultGetterFlags) | SYNTHETIC | DEFAULTPARAM | oflag,
name, deftParams, defvParamss, defTpt, defRhs)
}
if (!isConstr)
clazz.resetFlag(INTERFACE) // there's a concrete member now
val default = parentNamer.enterSyntheticSym(defaultTree)
if (forInteractive && default.owner.isTerm) {
// enter into map from method symbols to default arguments.
// if compiling the same local block several times (which can happen in interactive mode)
// we might otherwise not find the default symbol, because the second time it the
// method symbol will be re-entered in the scope but the default parameter will not.
defaultParametersOfMethod(meth) += default
}
} else if (baseHasDefault) {
// the parameter does not have a default itself, but the
// corresponding parameter in the base class does.
sym.setFlag(DEFAULTPARAM)
}
posCounter += 1
if (overrides) baseParams = baseParams.tail
}
if (overrides) baseParamss = baseParamss.tail
previous :+ vparams
}
}
//@M! an abstract type definition (abstract type member/type parameter)
// may take type parameters, which are in scope in its bounds
private def typeDefSig(tpsym: Symbol, tparams: List[TypeDef], rhs: Tree) = {
// log("typeDefSig(" + tpsym + ", " + tparams + ")")
val tparamSyms = typer.reenterTypeParams(tparams) //@M make tparams available in scope (just for this abstypedef)
val tp = typer.typedType(rhs).tpe match {
case TypeBounds(lt, rt) if (lt.isError || rt.isError) =>
TypeBounds.empty
case tp @ TypeBounds(lt, rt) if (tpsym hasFlag JAVA) =>
TypeBounds(lt, objToAny(rt))
case tp =>
tp
}
// see neg/bug1275, #3419
// used to do a rudimentary kind check here to ensure overriding in refinements
// doesn't change a type member's arity (number of type parameters), e.g.
//
// trait T { type X[A] }
// type S = T { type X }
// val x: S
//
// X in x.X[A] will get rebound to the X in the refinement, which
// does not take any type parameters. This mismatch does not crash
// the compiler (anymore), but leads to weird type errors, as
// x.X[A] will become NoType internally. It's not obvious the
// error refers to the X in the refinement and not the original X.
//
// However, separate compilation requires the symbol info to be
// loaded to do this check, but loading the info will probably
// lead to spurious cyclic errors. So omit the check.
GenPolyType(tparamSyms, tp)
}
/** Given a case class
* case class C[Ts] (ps: Us)
* Add the following methods to toScope:
* 1. if case class is not abstract, add
* <synthetic> <case> def apply[Ts](ps: Us): C[Ts] = new C[Ts](ps)
* 2. add a method
* <synthetic> <case> def unapply[Ts](x: C[Ts]) = <ret-val>
* where <ret-val> is the caseClassUnapplyReturnValue of class C (see UnApplies.scala)
*
* @param cdef is the class definition of the case class
* @param namer is the namer of the module class (the comp. obj)
*/
def addApplyUnapply(cdef: ClassDef, namer: Namer) {
if (!cdef.symbol.hasAbstractFlag)
namer.enterSyntheticSym(caseModuleApplyMeth(cdef))
namer.enterSyntheticSym(caseModuleUnapplyMeth(cdef))
}
def addCopyMethod(cdef: ClassDef, namer: Namer) {
caseClassCopyMeth(cdef) foreach namer.enterSyntheticSym
}
def typeSig(tree: Tree): Type = {
// log("typeSig " + tree)
/** For definitions, transform Annotation trees to AnnotationInfos, assign
* them to the sym's annotations. Type annotations: see Typer.typedAnnotated
* We have to parse definition annotations here (not in the typer when traversing
* the MemberDef tree): the typer looks at annotations of certain symbols; if
* they were added only in typer, depending on the compilation order, they may
* or may not be visible.
*/
def annotate(annotated: Symbol) = {
// typeSig might be called multiple times, e.g. on a ValDef: val, getter, setter
// parse the annotations only once.
if (!annotated.isInitialized) tree match {
case defn: MemberDef =>
val ainfos = defn.mods.annotations filterNot (_ eq null) map { ann =>
// need to be lazy, #1782
AnnotationInfo lazily (typer typedAnnotation ann)
}
if (ainfos.nonEmpty) {
annotated setAnnotations ainfos
if (annotated.isTypeSkolem)
annotated.deSkolemize setAnnotations ainfos
}
case _ =>
}
}
val sym: Symbol = tree.symbol
// @Lukas: I am not sure this is the right way to do things.
// We used to only decorate the module class with annotations, which is
// clearly wrong. Now we decorate both the class and the object.
// But maybe some annotations are only meant for one of these but not for the other?
//
// TODO: meta-annotations to indicate class vs. object.
annotate(sym)
if (sym.isModule) annotate(sym.moduleClass)
def getSig = tree match {
case cdef @ ClassDef(_, name, tparams, impl) =>
val clazz = tree.symbol
val result = createNamer(tree).classSig(tparams, impl)
clazz setInfo result
if (clazz.isDerivedValueClass) {
log("Ensuring companion for derived value class " + name + " at " + cdef.pos.show)
clazz setFlag FINAL
enclosingNamerWithScope(clazz.owner.info.decls).ensureCompanionObject(cdef)
}
result
case ModuleDef(_, _, impl) =>
val clazz = sym.moduleClass
clazz setInfo createNamer(tree).templateSig(impl)
clazz.tpe
case ddef @ DefDef(mods, _, tparams, vparamss, tpt, rhs) =>
// TODO: cleanup parameter list
createNamer(tree).methodSig(ddef, mods, tparams, vparamss, tpt, rhs)
case vdef @ ValDef(mods, name, tpt, rhs) =>
val isBeforeSupercall = (
(sym hasFlag PARAM | PRESUPER)
&& !mods.isJavaDefined
&& sym.owner.isConstructor
)
val typer1 = typer.constrTyperIf(isBeforeSupercall)
if (tpt.isEmpty) {
if (rhs.isEmpty) {
MissingParameterOrValTypeError(tpt)
ErrorType
}
else assignTypeToTree(vdef, newTyper(typer1.context.make(vdef, sym)), WildcardType)
}
else typer1.typedType(tpt).tpe
case TypeDef(_, _, tparams, rhs) =>
createNamer(tree).typeDefSig(sym, tparams, rhs) //@M!
case Import(expr, selectors) =>
val expr1 = typer.typedQualifier(expr)
typer checkStable expr1
if (expr1.symbol != null && expr1.symbol.isRootPackage)
RootImportError(tree)
val newImport = treeCopy.Import(tree, expr1, selectors).asInstanceOf[Import]
checkSelectors(newImport)
transformed(tree) = newImport
// copy symbol and type attributes back into old expression
// so that the structure builder will find it.
expr.symbol = expr1.symbol
expr.tpe = expr1.tpe
ImportType(expr1)
}
val result =
try getSig
catch typeErrorHandler(tree, ErrorType)
result match {
case PolyType(tparams @ (tp :: _), _) if tp.owner.isTerm => deskolemizeTypeParams(tparams)(result)
case _ => result
}
}
def includeParent(tpe: Type, parent: Symbol): Type = tpe match {
case PolyType(tparams, restpe) =>
PolyType(tparams, includeParent(restpe, parent))
case ClassInfoType(parents, decls, clazz) =>
if (parents exists (_.typeSymbol == parent)) tpe
else ClassInfoType(parents :+ parent.tpe, decls, clazz)
case _ =>
tpe
}
def ensureParent(clazz: Symbol, parent: Symbol) = {
val info0 = clazz.info
val info1 = includeParent(info0, parent)
if (info0 ne info1) clazz setInfo info1
}
class LogTransitions[S](onEnter: S => String, onExit: S => String) {
val enabled = settings.debug.value
@inline final def apply[T](entity: S)(body: => T): T = {
if (enabled) log(onEnter(entity))
try body
finally if (enabled) log(onExit(entity))
}
}
private val logDefinition = new LogTransitions[Symbol](
sym => "[define] >> " + sym.defaultFlagString + " " + sym.fullLocationString,
sym => "[define] << " + sym
)
private def logAndValidate(sym: Symbol)(body: => Unit) {
logDefinition(sym)(body)
validate(sym)
}
/** Convert Java generic array type T[] to (T with Object)[]
* (this is necessary because such arrays have a representation which is incompatible
* with arrays of primitive types.)
*/
private object RestrictJavaArraysMap extends TypeMap {
def apply(tp: Type): Type = tp match {
case TypeRef(pre, ArrayClass, List(elemtp))
if elemtp.typeSymbol.isAbstractType && !(elemtp <:< ObjectClass.tpe) =>
TypeRef(pre, ArrayClass, List(intersectionType(List(elemtp, ObjectClass.tpe))))
case _ =>
mapOver(tp)
}
}
/** Check that symbol's definition is well-formed. This means:
* - no conflicting modifiers
* - `abstract` modifier only for classes
* - `override` modifier never for classes
* - `def` modifier never for parameters of case classes
* - declarations only in mixins or abstract classes (when not @native)
*/
def validate(sym: Symbol) {
import SymValidateErrors._
def fail(kind: SymValidateErrors.Value) = SymbolValidationError(sym, kind)
def checkWithDeferred(flag: Int) {
if (sym hasFlag flag)
AbstractMemberWithModiferError(sym, flag)
}
def checkNoConflict(flag1: Int, flag2: Int) {
if (sym hasAllFlags flag1 | flag2)
IllegalModifierCombination(sym, flag1, flag2)
}
if (sym.isImplicit) {
if (sym.isConstructor)
fail(ImplicitConstr)
if (!sym.isTerm)
fail(ImplicitNotTerm)
if (sym.owner.isPackageClass)
fail(ImplicitTopObject)
}
if (sym.isClass) {
if (sym.isAnyOverride && !sym.hasFlag(TRAIT))
fail(OverrideClass)
} else {
if (sym.isSealed)
fail(SealedNonClass)
if (sym.hasFlag(ABSTRACT))
fail(AbstractNonClass)
}
if (sym.isConstructor && sym.isAnyOverride)
fail(OverrideConstr)
if (sym.isAbstractOverride && !sym.owner.isTrait)
fail(AbstractOverride)
if (sym.isLazy && sym.hasFlag(PRESUPER))
fail(LazyAndEarlyInit)
if (sym.info.typeSymbol == FunctionClass(0) && sym.isValueParameter && sym.owner.isCaseClass)
fail(ByNameParameter)
if (sym.isDeferred) {
// Is this symbol type always allowed the deferred flag?
def symbolAllowsDeferred = (
sym.isValueParameter
|| sym.isTypeParameterOrSkolem
|| context.tree.isInstanceOf[ExistentialTypeTree]
)
// Does the symbol owner require no undefined members?
def ownerRequiresConcrete = (
!sym.owner.isClass
|| sym.owner.isModuleClass
|| sym.owner.isAnonymousClass
)
if (sym hasAnnotation NativeAttr)
sym resetFlag DEFERRED
else if (!symbolAllowsDeferred && ownerRequiresConcrete)
fail(AbstractVar)
checkWithDeferred(PRIVATE)
checkWithDeferred(FINAL)
}
checkNoConflict(FINAL, SEALED)
checkNoConflict(PRIVATE, PROTECTED)
// checkNoConflict(PRIVATE, OVERRIDE) // this one leads to bad error messages like #4174, so catch in refchecks
// checkNoConflict(PRIVATE, FINAL) // can't do this because FINAL also means compile-time constant
// checkNoConflict(ABSTRACT, FINAL) // this one gives a bad error for non-@inline classes which extend AnyVal
// @PP: I added this as a sanity check because these flags are supposed to be
// converted to ABSOVERRIDE before arriving here.
checkNoConflict(ABSTRACT, OVERRIDE)
}
}
abstract class TypeCompleter extends LazyType {
val tree: Tree
}
def mkTypeCompleter(t: Tree)(c: Symbol => Unit) = new LockingTypeCompleter {
val tree = t
def completeImpl(sym: Symbol) = c(sym)
}
trait LockingTypeCompleter extends TypeCompleter {
def completeImpl(sym: Symbol): Unit
override def complete(sym: Symbol) = {
_lockedCount += 1
try completeImpl(sym)
finally _lockedCount -= 1
}
}
/** A class representing a lazy type with known type parameters.
*/
class PolyTypeCompleter(tparams: List[TypeDef], restp: TypeCompleter, owner: Tree, ctx: Context) extends LockingTypeCompleter {
private val ownerSym = owner.symbol
override val typeParams = tparams map (_.symbol) //@M
override val tree = restp.tree
if (ownerSym.isTerm) {
val skolems = deriveFreshSkolems(tparams map (_.symbol))
map2(tparams, skolems)(_ setSymbol _)
}
def completeImpl(sym: Symbol) = {
// @M an abstract type's type parameters are entered.
// TODO: change to isTypeMember ?
if (ownerSym.isAbstractType)
newNamerFor(ctx, owner) enterSyms tparams //@M
restp complete sym
}
}
// Can we relax these restrictions? For motivation, see
// test/files/pos/depmet_implicit_oopsla_session_2.scala
// neg/depmet_try_implicit.scala
//
// We should allow forward references since type selections on
// implicit args are like type parameters.
// def foo[T](a: T, x: w.T2)(implicit w: ComputeT2[T])
// is more compact than:
// def foo[T, T2](a: T, x: T2)(implicit w: ComputeT2[T, T2])
// moreover, the latter is not an encoding of the former, which hides type
// inference of T2, so you can specify T while T2 is purely computed
private class DependentTypeChecker(ctx: Context)(namer: Namer) extends TypeTraverser {
private[this] val okParams = mutable.Set[Symbol]()
private[this] val method = ctx.owner
def traverse(tp: Type) = tp match {
case SingleType(_, sym) =>
if (sym.owner == method && sym.isValueParameter && !okParams(sym))
namer.NamerErrorGen.IllegalDependentMethTpeError(sym)(ctx)
case _ => mapOver(tp)
}
def check(vparamss: List[List[Symbol]]) {
for (vps <- vparamss) {
for (p <- vps)
this(p.info)
// can only refer to symbols in earlier parameter sections
// (if the extension is enabled)
okParams ++= vps
}
}
}
@deprecated("Use underlyingSymbol instead", "2.10.0")
def underlying(member: Symbol): Symbol = underlyingSymbol(member)
@deprecated("Use `companionSymbolOf` instead", "2.10.0")
def companionClassOf(module: Symbol, ctx: Context): Symbol = companionSymbolOf(module, ctx)
@deprecated("Use `companionSymbolOf` instead", "2.10.0")
def companionModuleOf(clazz: Symbol, ctx: Context): Symbol = companionSymbolOf(clazz, ctx)
/** The companion class or companion module of `original`.
* Calling .companionModule does not work for classes defined inside methods.
*
* !!! Then why don't we fix companionModule? Does the presence of these
* methods imply all the places in the compiler calling sym.companionModule are
* bugs waiting to be reported? If not, why not? When exactly do we need to
* call this method?
*/
def companionSymbolOf(original: Symbol, ctx: Context): Symbol = {
try original.companionSymbol match {
case NoSymbol =>
ctx.lookup(original.name.companionName, original.owner).suchThat(sym =>
(original.isTerm || sym.hasModuleFlag) &&
(sym isCoDefinedWith original)
)
case sym => sym
}
catch {
case e: InvalidCompanions =>
ctx.unit.error(original.pos, e.getMessage)
NoSymbol
}
}
}
|