-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Symbols.scala
920 lines (796 loc) · 36.4 KB
/
Symbols.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
package dotty.tools
package dotc
package core
import Periods._
import Names._
import Scopes._
import Flags._
import Decorators._
import Symbols._
import Contexts._
import Phases._
import SymDenotations._
import Denotations._
import printing.Texts._
import printing.Printer
import Types._
import util.Spans._
import DenotTransformers._
import StdNames._
import NameOps._
import transform.SymUtils._
import NameKinds.LazyImplicitName
import ast.tpd
import tpd.{Tree, TreeProvider, TreeOps}
import ast.TreeTypeMap
import Constants.Constant
import Variances.{Variance, varianceFromInt}
import reporting.Message
import collection.mutable
import io.AbstractFile
import language.implicitConversions
import util.{SourceFile, NoSource, Property, SourcePosition, SrcPos, EqHashMap}
import scala.collection.JavaConverters._
import scala.annotation.internal.sharable
import config.Printers.typr
object Symbols {
implicit def eqSymbol: CanEqual[Symbol, Symbol] = CanEqual.derived
/** Tree attachment containing the identifiers in a tree as a sorted array */
val Ids: Property.Key[Array[String]] = new Property.Key
/** A Symbol represents a Scala definition/declaration or a package.
* @param coord The coordinates of the symbol (a position or an index)
* @param id A unique identifier of the symbol (unique per ContextBase)
*/
class Symbol private[Symbols] (private var myCoord: Coord, val id: Int, val nestingLevel: Int)
extends Designator, ParamInfo, SrcPos, printing.Showable {
type ThisName <: Name
//assert(id != 723)
def coord: Coord = myCoord
/** Set the coordinate of this class, this is only useful when the coordinate is
* not known at symbol creation. This is the case for root symbols
* unpickled from TASTY.
*
* @pre coord == NoCoord
*/
private[core] def coord_=(c: Coord): Unit = {
// assert(myCoord == NoCoord)
// This assertion fails for CommentPickling test.
// TODO: figure out what's wrong in the setup of CommentPicklingTest and re-enable assertion.
myCoord = c
}
private var myDefTree: Tree = null
/** The tree defining the symbol at pickler time, EmptyTree if none was retained */
def defTree: Tree =
if (myDefTree == null) tpd.EmptyTree else myDefTree
/** Set defining tree if this symbol retains its definition tree */
def defTree_=(tree: Tree)(using Context): Unit =
if (retainsDefTree) myDefTree = tree
/** Does this symbol retain its definition tree?
* A good policy for this needs to balance costs and benefits, where
* costs are mainly memoty leaks, in particular across runs.
*/
def retainsDefTree(using Context): Boolean =
ctx.settings.YretainTrees.value ||
denot.owner.isTerm || // no risk of leaking memory after a run for these
denot.isOneOf(InlineOrProxy) || // need to keep inline info
ctx.settings.YcheckInit.value // initialization check
/** The last denotation of this symbol */
private var lastDenot: SymDenotation = _
private var checkedPeriod: Period = Nowhere
private[core] def invalidateDenotCache(): Unit = { checkedPeriod = Nowhere }
/** Set the denotation of this symbol */
private[core] def denot_=(d: SymDenotation): Unit = {
util.Stats.record("Symbol.denot_=")
lastDenot = d
checkedPeriod = Nowhere
}
/** The current denotation of this symbol */
final def denot(using Context): SymDenotation = {
util.Stats.record("Symbol.denot")
val lastd = lastDenot
if (checkedPeriod == ctx.period) lastd
else computeDenot(lastd)
}
private def computeDenot(lastd: SymDenotation)(using Context): SymDenotation = {
util.Stats.record("Symbol.computeDenot")
val now = ctx.period
checkedPeriod = now
if (lastd.validFor contains now) lastd else recomputeDenot(lastd)
}
/** Overridden in NoSymbol */
protected def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = {
util.Stats.record("Symbol.recomputeDenot")
val newd = lastd.current.asInstanceOf[SymDenotation]
lastDenot = newd
newd
}
/** The original denotation of this symbol, without forcing anything */
final def originDenotation: SymDenotation =
lastDenot.initial
/** The last known denotation of this symbol, without going through `current` */
final def lastKnownDenotation: SymDenotation =
lastDenot
private[core] def defRunId: RunId =
if (lastDenot == null) NoRunId else lastDenot.validFor.runId
/** Does this symbol come from a currently compiled source file? */
final def isDefinedInCurrentRun(using Context): Boolean =
span.exists && defRunId == ctx.runId && {
try
val file = associatedFile
file != null && ctx.run.files.contains(file)
catch case ex: StaleSymbol =>
// can happen for constructor proxy companions. Test case is pos-macros/i9484.
false
}
/** Is symbol valid in current run? */
final def isValidInCurrentRun(using Context): Boolean =
(lastDenot.validFor.runId == ctx.runId || stillValid(lastDenot)) &&
(lastDenot.symbol eq this)
// the last condition is needed because under ctx.staleOK overwritten
// members keep denotations pointing to the new symbol, so the validity
// periods check out OK. But once a package member is overridden it is not longer
// valid. If the option would be removed, the check would be no longer needed.
final def isTerm(using Context): Boolean =
(if (defRunId == ctx.runId) lastDenot else denot).isTerm
final def isType(using Context): Boolean =
(if (defRunId == ctx.runId) lastDenot else denot).isType
final def asTerm(using Context): TermSymbol = {
assert(isTerm, s"asTerm called on not-a-Term $this" );
asInstanceOf[TermSymbol]
}
final def asType(using Context): TypeSymbol = {
assert(isType, s"isType called on not-a-Type $this");
asInstanceOf[TypeSymbol]
}
final def isClass: Boolean = isInstanceOf[ClassSymbol]
final def asClass: ClassSymbol = asInstanceOf[ClassSymbol]
/** Test whether symbol is private. This
* conservatively returns `false` if symbol does not yet have a denotation, or denotation
* is a class that is not yet read.
*/
final def isPrivate(using Context): Boolean = {
val d = lastDenot
d != null && d.flagsUNSAFE.is(Private)
}
/** Is the symbol a pattern bound symbol?
*/
final def isPatternBound(using Context): Boolean =
!isClass && this.is(Case, butNot = Enum | Module)
/** The symbol's signature if it is completed or a method, NotAMethod otherwise. */
final def signature(using Context): Signature =
if (lastDenot != null && (lastDenot.isCompleted || lastDenot.is(Method)))
denot.signature
else
Signature.NotAMethod
/** Special cased here, because it may be used on naked symbols in substituters */
final def isStatic(using Context): Boolean =
lastDenot != null && lastDenot.initial.isStatic
/** This symbol entered into owner's scope (owner must be a class). */
final def entered(using Context): this.type = {
if (this.owner.isClass) {
this.owner.asClass.enter(this)
if (this.is(Module)) this.owner.asClass.enter(this.moduleClass)
}
this
}
/** Enter this symbol in its class owner after given `phase`. Create a fresh
* denotation for its owner class if the class does not already have one
* that starts being valid after `phase`.
* @pre Symbol is a class member
*/
def enteredAfter(phase: DenotTransformer)(using Context): this.type =
if ctx.phaseId != phase.next.id then
atPhase(phase.next)(enteredAfter(phase))
else this.owner match {
case owner: ClassSymbol =>
if (owner.is(Package)) {
denot.validFor |= InitialPeriod
if (this.is(Module)) this.moduleClass.validFor |= InitialPeriod
}
else owner.ensureFreshScopeAfter(phase)
assert(isPrivate || phase.changesMembers, i"$this entered in $owner at undeclared phase $phase")
entered
case _ => this
}
/** Remove symbol from scope of owning class */
final def drop()(using Context): Unit = {
this.owner.asClass.delete(this)
if (this.is(Module)) this.owner.asClass.delete(this.moduleClass)
}
/** Remove symbol from scope of owning class after given `phase`. Create a fresh
* denotation for its owner class if the class does not already have one that starts being valid after `phase`.
* @pre Symbol is a class member
*/
def dropAfter(phase: DenotTransformer)(using Context): Unit =
if ctx.phaseId != phase.next.id then
atPhase(phase.next)(dropAfter(phase))
else {
assert (!this.owner.is(Package))
this.owner.asClass.ensureFreshScopeAfter(phase)
assert(isPrivate || phase.changesMembers, i"$this deleted in ${this.owner} at undeclared phase $phase")
drop()
}
/** This symbol, if it exists, otherwise the result of evaluating `that` */
inline def orElse(inline that: Symbol)(using Context): Symbol =
if (this.exists) this else that
/** If this symbol satisfies predicate `p` this symbol, otherwise `NoSymbol` */
def filter(p: Symbol => Boolean): Symbol = if (p(this)) this else NoSymbol
/** The current name of this symbol */
final def name(using Context): ThisName = denot.name.asInstanceOf[ThisName]
/** The source or class file from which this class or
* the class containing this symbol was generated, null if not applicable.
* Note that this the returned classfile might be the top-level class
* containing this symbol instead of the directly enclosing class.
* Overridden in ClassSymbol
*/
def associatedFile(using Context): AbstractFile =
if (lastDenot == null) null else lastDenot.topLevelClass.associatedFile
/** The class file from which this class was generated, null if not applicable. */
final def binaryFile(using Context): AbstractFile = {
val file = associatedFile
if (file != null && file.extension == "class") file else null
}
/** A trap to avoid calling x.symbol on something that is already a symbol.
* This would be expanded to `toDenot(x).symbol` which is guaraneteed to be
* the same as `x`.
* With the given setup, all such calls will give implicit-not found errors
*/
final def symbol(implicit ev: DontUseSymbolOnSymbol): Nothing = unsupported("symbol")
type DontUseSymbolOnSymbol
final def source(using Context): SourceFile = {
def valid(src: SourceFile): SourceFile =
if (src.exists && src.file.extension != "class") src
else NoSource
if (!denot.exists) NoSource
else
valid(defTree.source) match {
case NoSource =>
valid(denot.owner.source) match {
case NoSource =>
this match {
case cls: ClassSymbol => valid(cls.sourceOfClass)
case _ if denot.is(Module) => valid(denot.moduleClass.source)
case _ => NoSource
}
case src => src
}
case src => src
}
}
/** A symbol related to `sym` that is defined in source code.
*
* @see enclosingSourceSymbols
*/
@annotation.tailrec final def sourceSymbol(using Context): Symbol =
if (!denot.exists)
this
else if (denot.is(ModuleVal))
this.moduleClass.sourceSymbol // The module val always has a zero-extent position
else if (denot.is(Synthetic)) {
val linked = denot.linkedClass
if (linked.exists && !linked.is(Synthetic))
linked
else
denot.owner.sourceSymbol
}
else if (denot.isPrimaryConstructor)
denot.owner.sourceSymbol
else this
/** The position of this symbol, or NoSpan if the symbol was not loaded
* from source or from TASTY. This is always a zero-extent position.
*/
final def span: Span = if (coord.isSpan) coord.toSpan else NoSpan
final def sourcePos(using Context): SourcePosition = {
val src = source
(if (src.exists) src else ctx.source).atSpan(span)
}
/** This positioned item, widened to `SrcPos`. Used to make clear we only need the
* position, typically for error reporting.
*/
final def srcPos: SrcPos = this
// ParamInfo types and methods
def isTypeParam(using Context): Boolean = denot.is(TypeParam)
def paramName(using Context): ThisName = name.asInstanceOf[ThisName]
def paramInfo(using Context): Type = denot.info
def paramInfoAsSeenFrom(pre: Type)(using Context): Type = pre.memberInfo(this)
def paramInfoOrCompleter(using Context): Type = denot.infoOrCompleter
def paramVariance(using Context): Variance = denot.variance
def paramRef(using Context): TypeRef = denot.typeRef
// -------- Printing --------------------------------------------------------
/** The prefix string to be used when displaying this symbol without denotation */
protected def prefixString: String = "Symbol"
override def toString: String =
if (lastDenot == null) s"Naked$prefixString#$id"
else lastDenot.toString// + "#" + id // !!! DEBUG
def toText(printer: Printer): Text = printer.toText(this)
def showLocated(using Context): String = ctx.printer.locatedText(this).show
def showExtendedLocation(using Context): String = ctx.printer.extendedLocationText(this).show
def showDcl(using Context): String = ctx.printer.dclText(this).show
def showKind(using Context): String = ctx.printer.kindString(this)
def showName(using Context): String = ctx.printer.nameString(this)
def showFullName(using Context): String = ctx.printer.fullNameString(this)
override def hashCode(): Int = id // for debugging.
}
type TermSymbol = Symbol { type ThisName = TermName }
type TypeSymbol = Symbol { type ThisName = TypeName }
class ClassSymbol private[Symbols] (coord: Coord, val assocFile: AbstractFile, id: Int, nestingLevel: Int)
extends Symbol(coord, id, nestingLevel) {
type ThisName = TypeName
type TreeOrProvider = tpd.TreeProvider | tpd.Tree
private var myTree: TreeOrProvider = tpd.EmptyTree
/** If this is a top-level class and `-Yretain-trees` (or `-from-tasty`) is set.
* Returns the TypeDef tree (possibly wrapped inside PackageDefs) for this class, otherwise EmptyTree.
* This will force the info of the class.
*/
def rootTree(using Context): Tree = rootTreeContaining("")
/** Same as `tree` but load tree only if `id == ""` or the tree might contain `id`.
* For Tasty trees this means consulting whether the name table defines `id`.
* For already loaded trees, we maintain the referenced ids in an attachment.
*/
def rootTreeContaining(id: String)(using Context): Tree = {
denot.infoOrCompleter match {
case _: NoCompleter =>
case _ => denot.ensureCompleted()
}
myTree match {
case fn: TreeProvider =>
if (id.isEmpty || fn.mightContain(id)) {
val tree = fn.tree
myTree = tree
tree
}
else tpd.EmptyTree
case tree: Tree @ unchecked =>
if (id.isEmpty || mightContain(tree, id)) tree else tpd.EmptyTree
}
}
def rootTreeOrProvider: TreeOrProvider = myTree
private[dotc] def rootTreeOrProvider_=(t: TreeOrProvider)(using Context): Unit =
myTree = t
private def mightContain(tree: Tree, id: String)(using Context): Boolean = {
val ids = tree.getAttachment(Ids) match {
case Some(ids) => ids
case None =>
val idSet = mutable.SortedSet[String]()
tree.foreachSubTree {
case tree: tpd.NameTree if tree.name.toTermName.isInstanceOf[SimpleName] =>
idSet += tree.name.toString
case _ =>
}
val ids = idSet.toArray
tree.putAttachment(Ids, ids)
ids
}
ids.binarySearch(id) >= 0
}
/** The source or class file from which this class was generated, null if not applicable. */
override def associatedFile(using Context): AbstractFile =
if assocFile != null || this.is(Package) || this.owner.is(Package) then assocFile
else super.associatedFile
private var mySource: SourceFile = NoSource
final def sourceOfClass(using Context): SourceFile = {
if !mySource.exists && !denot.is(Package) then
// this allows sources to be added in annotations after `sourceOfClass` is first called
val file = associatedFile
if file != null && file.extension != "class" then
mySource = ctx.getSource(file)
else
mySource = defn.patchSource(this)
if !mySource.exists then
mySource = atPhaseNoLater(flattenPhase) {
denot.topLevelClass.unforcedAnnotation(defn.SourceFileAnnot) match
case Some(sourceAnnot) => sourceAnnot.argumentConstant(0) match
case Some(Constant(path: String)) => ctx.getSource(path)
case none => NoSource
case none => NoSource
}
mySource
}
final def classDenot(using Context): ClassDenotation =
denot.asInstanceOf[ClassDenotation]
override protected def prefixString: String = "ClassSymbol"
}
@sharable object NoSymbol extends Symbol(NoCoord, 0, 0) {
override def associatedFile(using Context): AbstractFile = NoSource.file
override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = NoDenotation
}
NoDenotation // force it in order to set `denot` field of NoSymbol
extension [N <: Name](sym: Symbol { type ThisName = N })(using Context) {
/** Copy a symbol, overriding selective fields.
* Note that `coord` and `associatedFile` will be set from the fields in `owner`, not
* the fields in `sym`.
*/
def copy(
owner: Symbol = sym.owner,
name: N = sym.name,
flags: FlagSet = sym.flags,
info: Type = sym.info,
privateWithin: Symbol = sym.privateWithin,
coord: Coord = NoCoord, // Can be `= owner.coord` once we bootstrap
associatedFile: AbstractFile = null // Can be `= owner.associatedFile` once we bootstrap
): Symbol = {
val coord1 = if (coord == NoCoord) owner.coord else coord
val associatedFile1 = if (associatedFile == null) owner.associatedFile else associatedFile
if (sym.isClass)
newClassSymbol(owner, name.asTypeName, flags, _ => info, privateWithin, coord1, associatedFile1)
else
newSymbol(owner, name, flags, info, privateWithin, coord1)
}
}
/** Makes all denotation operations available on symbols */
implicit def toDenot(sym: Symbol)(using Context): SymDenotation = sym.denot
/** Makes all class denotation operations available on class symbols */
implicit def toClassDenot(cls: ClassSymbol)(using Context): ClassDenotation = cls.classDenot
/** The Definitions object */
def defn(using Context): Definitions = ctx.definitions
/** The current class */
def currentClass(using Context): ClassSymbol = ctx.owner.enclosingClass.asClass
type MutableSymbolMap[T] = EqHashMap[Symbol, T]
def MutableSymbolMap[T](): EqHashMap[Symbol, T] = EqHashMap[Symbol, T]()
def MutableSymbolMap[T](initialCapacity: Int): EqHashMap[Symbol, T] = EqHashMap[Symbol, T](initialCapacity)
// ---- Symbol creation methods ----------------------------------
/** Create a symbol from its fields (info may be lazy) */
def newSymbol[N <: Name](
owner: Symbol,
name: N,
flags: FlagSet,
info: Type,
privateWithin: Symbol = NoSymbol,
coord: Coord = NoCoord)(using Context): Symbol { type ThisName = N } = {
val sym = new Symbol(coord, ctx.base.nextSymId, ctx.nestingLevel).asInstanceOf[Symbol { type ThisName = N }]
val denot = SymDenotation(sym, owner, name, flags, info, privateWithin)
sym.denot = denot
sym
}
/** Create a class symbol from its non-info fields and a function
* producing its info (the produced info may be lazy).
*/
def newClassSymbol(
owner: Symbol,
name: TypeName,
flags: FlagSet,
infoFn: ClassSymbol => Type,
privateWithin: Symbol = NoSymbol,
coord: Coord = NoCoord,
assocFile: AbstractFile = null)(using Context): ClassSymbol
= {
val cls = new ClassSymbol(coord, assocFile, ctx.base.nextSymId, ctx.nestingLevel)
val denot = SymDenotation(cls, owner, name, flags, infoFn(cls), privateWithin)
cls.denot = denot
cls
}
/** Create a class symbol from its non-info fields and the fields of its info. */
def newCompleteClassSymbol(
owner: Symbol,
name: TypeName,
flags: FlagSet,
parents: List[TypeRef],
decls: Scope,
selfInfo: Type = NoType,
privateWithin: Symbol = NoSymbol,
coord: Coord = NoCoord,
assocFile: AbstractFile = null)(using Context): ClassSymbol =
newClassSymbol(
owner, name, flags,
ClassInfo(owner.thisType, _, parents, decls, selfInfo),
privateWithin, coord, assocFile)
/** Same as `newCompleteClassSymbol` except that `parents` can be a list of arbitrary
* types which get normalized into type refs and parameter bindings.
*/
def newNormalizedClassSymbol(
owner: Symbol,
name: TypeName,
flags: FlagSet,
parentTypes: List[Type],
decls: Scope,
selfInfo: Type = NoType,
privateWithin: Symbol = NoSymbol,
coord: Coord = NoCoord,
assocFile: AbstractFile = null)(using Context): ClassSymbol = {
def completer = new LazyType {
def complete(denot: SymDenotation)(using Context): Unit = {
val cls = denot.asClass.classSymbol
val decls = newScope
denot.info = ClassInfo(owner.thisType, cls, parentTypes.map(_.dealias), decls)
}
}
newClassSymbol(owner, name, flags, completer, privateWithin, coord, assocFile)
}
def newRefinedClassSymbol(coord: Coord = NoCoord)(using Context): ClassSymbol =
newCompleteClassSymbol(ctx.owner, tpnme.REFINE_CLASS, NonMember, parents = Nil, newScope, coord = coord)
/** Create a module symbol with associated module class
* from its non-info fields and a function producing the info
* of the module class (this info may be lazy).
*/
def newModuleSymbol(
owner: Symbol,
name: TermName,
modFlags: FlagSet,
clsFlags: FlagSet,
infoFn: (TermSymbol, ClassSymbol) => Type, // typically a ModuleClassCompleterWithDecls
privateWithin: Symbol = NoSymbol,
coord: Coord = NoCoord,
assocFile: AbstractFile = null)(using Context): TermSymbol
= {
val base = owner.thisType
val modclsFlags = clsFlags | ModuleClassCreationFlags
val modclsName = name.toTypeName.adjustIfModuleClass(modclsFlags)
val module = newSymbol(
owner, name, modFlags | ModuleValCreationFlags, NoCompleter, privateWithin, coord)
val modcls = newClassSymbol(
owner, modclsName, modclsFlags, infoFn(module, _), privateWithin, coord, assocFile)
module.info =
if (modcls.isCompleted) TypeRef(owner.thisType, modcls)
else new ModuleCompleter(modcls)
module
}
/** Create a module symbol with associated module class
* from its non-info fields and the fields of the module class info.
* @param flags The combined flags of the module and the module class
* These are masked with RetainedModuleValFlags/RetainedModuleClassFlags.
*/
def newCompleteModuleSymbol(
owner: Symbol,
name: TermName,
modFlags: FlagSet,
clsFlags: FlagSet,
parents: List[TypeRef],
decls: Scope,
privateWithin: Symbol = NoSymbol,
coord: Coord = NoCoord,
assocFile: AbstractFile = null)(using Context): TermSymbol =
newModuleSymbol(
owner, name, modFlags, clsFlags,
(module, modcls) => ClassInfo(
owner.thisType, modcls, parents, decls, TermRef(owner.thisType, module)),
privateWithin, coord, assocFile)
/** Create a package symbol with associated package class
* from its non-info fields and a lazy type for loading the package's members.
*/
def newPackageSymbol(
owner: Symbol,
name: TermName,
infoFn: (TermSymbol, ClassSymbol) => LazyType)(using Context): TermSymbol =
newModuleSymbol(owner, name, PackageCreationFlags, PackageCreationFlags, infoFn)
/** Create a package symbol with associated package class
* from its non-info fields its member scope.
*/
def newCompletePackageSymbol(
owner: Symbol,
name: TermName,
modFlags: FlagSet = EmptyFlags,
clsFlags: FlagSet = EmptyFlags,
decls: Scope = newScope(0))(using Context): TermSymbol =
newCompleteModuleSymbol(
owner, name,
modFlags | PackageCreationFlags, clsFlags | PackageCreationFlags,
Nil, decls)
/** Define a new symbol associated with a Bind or pattern wildcard and, by default, make it gadt narrowable. */
def newPatternBoundSymbol(
name: Name,
info: Type,
span: Span,
addToGadt: Boolean = true,
flags: FlagSet = EmptyFlags)(using Context): Symbol = {
val sym = newSymbol(ctx.owner, name, Case | flags, info, coord = span)
if (addToGadt && name.isTypeName) ctx.gadt.addToConstraint(sym)
sym
}
/** Create a stub symbol that will issue a missing reference error
* when attempted to be completed.
*/
def newStubSymbol(owner: Symbol, name: Name, file: AbstractFile = null)(using Context): Symbol = {
def stubCompleter = new StubInfo()
val normalizedOwner = if (owner.is(ModuleVal)) owner.moduleClass else owner
typr.println(s"creating stub for ${name.show}, owner = ${normalizedOwner.denot.debugString}, file = $file")
typr.println(s"decls = ${normalizedOwner.unforcedDecls.toList.map(_.debugString).mkString("\n ")}") // !!! DEBUG
//if (base.settings.debug.value) throw new Error()
val stub = name match {
case name: TermName =>
newModuleSymbol(normalizedOwner, name, EmptyFlags, EmptyFlags, stubCompleter, assocFile = file)
case name: TypeName =>
newClassSymbol(normalizedOwner, name, EmptyFlags, stubCompleter, assocFile = file)
}
stub
}
/** Create the local template dummy of given class `cls`.
* In a template
*
* trait T { val fld: Int; { val x: int = 2 }; val fld2 = { val y = 2; y }}
*
* the owner of `x` is the local dummy of the template. The owner of the local
* dummy is then the class of the template itself. By contrast, the owner of `y`
* would be `fld2`. There is a single local dummy per template.
*/
def newLocalDummy(cls: Symbol, coord: Coord = NoCoord)(using Context): TermSymbol =
newSymbol(cls, nme.localDummyName(cls), NonMember, NoType)
/** Create an import symbol pointing back to given qualifier `expr`. */
def newImportSymbol(owner: Symbol, expr: Tree, coord: Coord = NoCoord)(using Context): TermSymbol =
newImportSymbol(owner, ImportType(expr), coord = coord)
/** Create an import symbol with given `info`. */
def newImportSymbol(owner: Symbol, info: Type, coord: Coord)(using Context): TermSymbol =
newSymbol(owner, nme.IMPORT, Synthetic | NonMember, info, coord = coord)
/** Create a class constructor symbol for given class `cls`. */
def newConstructor(
cls: ClassSymbol,
flags: FlagSet,
paramNames: List[TermName],
paramTypes: List[Type],
privateWithin: Symbol = NoSymbol,
coord: Coord = NoCoord)(using Context): TermSymbol =
newSymbol(cls, nme.CONSTRUCTOR, flags | Method, MethodType(paramNames, paramTypes, cls.typeRef), privateWithin, coord)
/** Create an anonymous function symbol */
def newAnonFun(owner: Symbol, info: Type, coord: Coord = NoCoord)(using Context): TermSymbol =
newSymbol(owner, nme.ANON_FUN, Synthetic | Method, info, coord = coord)
/** Create an empty default constructor symbol for given class `cls`. */
def newDefaultConstructor(cls: ClassSymbol)(using Context): TermSymbol =
newConstructor(cls, EmptyFlags, Nil, Nil)
def newLazyImplicit(info: Type, coord: Coord = NoCoord)(using Context): TermSymbol =
newSymbol(ctx.owner, LazyImplicitName.fresh(), EmptyFlags, info, coord = coord)
/** Create a symbol representing a selftype declaration for class `cls`. */
def newSelfSym(
cls: ClassSymbol,
name: TermName = nme.WILDCARD,
selfInfo: Type = NoType)(using Context): TermSymbol =
newSymbol(cls, name, SelfSymFlags, selfInfo orElse cls.classInfo.selfType, coord = cls.coord)
/** Create new type parameters with given owner, names, and flags.
* @param boundsFn A function that, given type refs to the newly created
* parameters returns a list of their bounds.
*/
def newTypeParams(
owner: Symbol,
names: List[TypeName],
flags: FlagSet,
boundsFn: List[TypeRef] => List[Type])(using Context): List[TypeSymbol] = {
val tparamBuf = new mutable.ListBuffer[TypeSymbol]
val trefBuf = new mutable.ListBuffer[TypeRef]
for (name <- names) {
val tparam = newSymbol(
owner, name, flags | owner.typeParamCreationFlags, NoType, coord = owner.coord)
tparamBuf += tparam
trefBuf += TypeRef(owner.thisType, tparam)
}
val tparams = tparamBuf.toList
val bounds = boundsFn(trefBuf.toList)
for (tparam, bound) <- tparams.lazyZip(bounds) do
tparam.info = bound
tparams
}
/** Create a new skolem symbol. This is not the same as SkolemType, even though the
* motivation (create a singleton referencing to a type) is similar.
*/
def newSkolem(tp: Type)(using Context): TermSymbol =
newSymbol(defn.RootClass, nme.SKOLEM, SyntheticArtifact | NonMember | Permanent, tp)
def newErrorSymbol(owner: Symbol, name: Name, msg: Message)(using Context): Symbol = {
val errType = ErrorType(msg)
newSymbol(owner, name, SyntheticArtifact,
if (name.isTypeName) TypeAlias(errType) else errType)
}
/** Map given symbols, subjecting their attributes to the mappings
* defined in the given TreeTypeMap `ttmap`.
* Cross symbol references are brought over from originals to copies.
* Do not copy any symbols if all attributes of all symbols stay the same.
*/
def mapSymbols(originals: List[Symbol], ttmap: TreeTypeMap, mapAlways: Boolean = false)(using Context): List[Symbol] =
if (originals.forall(sym =>
(ttmap.mapType(sym.info) eq sym.info) &&
!(ttmap.oldOwners contains sym.owner)) && !mapAlways)
originals
else {
val copies: List[Symbol] = for (original <- originals) yield
val odenot = original.denot
original.copy(
owner = ttmap.mapOwner(odenot.owner),
flags = odenot.flags &~ Touched,
info = NoCompleter,
privateWithin = ttmap.mapOwner(odenot.privateWithin),
coord = original.coord)
val ttmap1 = ttmap.withSubstitution(originals, copies)
originals.lazyZip(copies) foreach { (original, copy) =>
val odenot = original.denot
val completer = new LazyType:
def complete(denot: SymDenotation)(using Context): Unit =
val oinfo = original.info match
case ClassInfo(pre, _, parents, decls, selfInfo) =>
assert(original.isClass)
val parents1 = parents.mapConserve(ttmap.mapType)
val otypeParams = original.typeParams
if otypeParams.isEmpty then
ClassInfo(pre, copy.asClass, parents1, decls.cloneScope, selfInfo)
else
// copy type params, enter other definitions unchanged
// type parameters need to be copied early, since other type
// computations depend on them.
val decls1 = newScope
val newTypeParams = mapSymbols(original.typeParams, ttmap1, mapAlways = true)
newTypeParams.foreach(decls1.enter)
for sym <- decls do if !sym.is(TypeParam) then decls1.enter(sym)
val parents2 = parents1.map(_.substSym(otypeParams, newTypeParams))
val selfInfo1 = selfInfo match
case selfInfo: Type => selfInfo.substSym(otypeParams, newTypeParams)
case _ => selfInfo
ClassInfo(pre, copy.asClass, parents2, decls1, selfInfo1)
case oinfo => oinfo
denot.info = oinfo // needed as otherwise we won't be able to go from Sym -> parents & etc
// Note that this is a hack, but hack commonly used in Dotty
// The same thing is done by other completers all the time
denot.info = ttmap1.mapType(oinfo)
denot.annotations = odenot.annotations.mapConserve(ttmap1.apply)
end completer
copy.info = completer
copy.denot match
case cd: ClassDenotation =>
cd.registeredCompanion = original.registeredCompanion.subst(originals, copies)
case _ =>
}
copies.foreach(_.ensureCompleted()) // avoid memory leak
// Update Child annotations of classes encountered previously to new values
// if some child is among the mapped symbols
for orig <- ttmap1.substFrom do
if orig.is(Sealed) && orig.children.exists(originals.contains) then
val sealedCopy = orig.subst(ttmap1.substFrom, ttmap1.substTo)
sealedCopy.annotations = sealedCopy.annotations.mapConserve(ttmap1.apply)
copies
}
/** Matches lists of term symbols, including the empty list.
* All symbols in the list are assumed to be of the same kind.
*/
object TermSymbols:
def unapply(xs: List[Symbol])(using Context): Option[List[TermSymbol]] = xs match
case (x: Symbol) :: _ if x.isType => None
case _ => Some(xs.asInstanceOf[List[TermSymbol]])
/** Matches lists of type symbols, excluding the empty list.
* All symbols in the list are assumed to be of the same kind.
*/
object TypeSymbols:
def unapply(xs: List[Symbol])(using Context): Option[List[TypeSymbol]] = xs match
case (x: Symbol) :: _ if x.isType => Some(xs.asInstanceOf[List[TypeSymbol]])
case _ => None
// ----- Locating predefined symbols ----------------------------------------
def requiredPackage(path: PreName)(using Context): TermSymbol = {
val name = path.toTermName
staticRef(name, isPackage = true).requiredSymbol("package", name)(_.is(Package)).asTerm
}
def requiredPackageRef(path: PreName)(using Context): TermRef = requiredPackage(path).termRef
def requiredClass(path: PreName)(using Context): ClassSymbol = {
val name = path.toTypeName
staticRef(name).requiredSymbol("class", name)(_.isClass) match {
case cls: ClassSymbol => cls
case sym => defn.AnyClass
}
}
def requiredClassRef(path: PreName)(using Context): TypeRef = requiredClass(path).typeRef
/** Get ClassSymbol if class is either defined in current compilation run
* or present on classpath. Returns NoSymbol otherwise.
*/
def getClassIfDefined(path: PreName)(using Context): Symbol =
staticRef(path.toTypeName, generateStubs = false)
.disambiguate(_.isClass).symbol
/** Get a List of ClassSymbols which are either defined in current compilation
* run or present on classpath.
*/
def getClassesIfDefined(paths: List[PreName])(using Context): List[ClassSymbol] =
paths.map(getClassIfDefined).filter(_.exists).map(_.asInstanceOf[ClassSymbol])
/** Get ClassSymbol if package is either defined in current compilation run
* or present on classpath. Returns NoSymbol otherwise.
*/
def getPackageClassIfDefined(path: PreName)(using Context): Symbol =
staticRef(path.toTypeName, isPackage = true, generateStubs = false)
.disambiguate(_ is PackageClass).symbol
def requiredModule(path: PreName)(using Context): TermSymbol = {
val name = path.toTermName
staticRef(name).requiredSymbol("object", name)(_.is(Module)).asTerm
}
/** Get module symbol if the module is either defined in current compilation run
* or present on classpath. Returns NoSymbol otherwise.
*/
def getModuleIfDefined(path: PreName)(using Context): Symbol =
staticRef(path.toTermName, generateStubs = false)
.disambiguate(_.is(Module)).symbol
def requiredModuleRef(path: PreName)(using Context): TermRef = requiredModule(path).termRef
def requiredMethod(path: PreName)(using Context): TermSymbol = {
val name = path.toTermName
staticRef(name).requiredSymbol("method", name)(_.is(Method)).asTerm
}
def requiredMethodRef(path: PreName)(using Context): TermRef = requiredMethod(path).termRef
}