Skip to content

Commit d62ffc8

Browse files
committed
Refactor context
- Handle all context state (except caches) in FreshContext. - Drop unsused operations - Make NoContext a simple val instead of an object
1 parent e09175e commit d62ffc8

File tree

1 file changed

+98
-87
lines changed

1 file changed

+98
-87
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 98 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ object Contexts {
123123
*/
124124
abstract class Context(val base: ContextBase) { thiscontext =>
125125

126-
given Context = this
126+
protected given Context = this
127+
128+
def outer: Context
127129

128130
/** All outer contexts, ending in `base.initialCtx` and then `NoContext` */
129131
def outersIterator: Iterator[Context] = new Iterator[Context] {
@@ -132,65 +134,20 @@ object Contexts {
132134
def next = { val c = current; current = current.outer; c }
133135
}
134136

135-
/** The outer context */
136-
private var _outer: Context = _
137-
protected def outer_=(outer: Context): Unit = _outer = outer
138-
final def outer: Context = _outer
139-
140-
/** The current context */
141-
private var _period: Period = _
142-
protected def period_=(period: Period): Unit = {
143-
assert(period.firstPhaseId == period.lastPhaseId, period)
144-
_period = period
145-
}
146-
final def period: Period = _period
147-
148-
/** The scope nesting level */
149-
private var _mode: Mode = _
150-
protected def mode_=(mode: Mode): Unit = _mode = mode
151-
final def mode: Mode = _mode
152-
153-
/** The current owner symbol */
154-
private var _owner: Symbol = _
155-
protected def owner_=(owner: Symbol): Unit = _owner = owner
156-
final def owner: Symbol = _owner
157-
158-
/** The current tree */
159-
private var _tree: Tree[?]= _
160-
protected def tree_=(tree: Tree[?]): Unit = _tree = tree
161-
final def tree: Tree[?] = _tree
162-
163-
/** The current scope */
164-
private var _scope: Scope = _
165-
protected def scope_=(scope: Scope): Unit = _scope = scope
166-
final def scope: Scope = _scope
167-
168-
/** The current typerstate */
169-
private var _typerState: TyperState = _
170-
protected def typerState_=(typerState: TyperState): Unit = _typerState = typerState
171-
final def typerState: TyperState = _typerState
172-
173-
/** The current bounds in force for type parameters appearing in a GADT */
174-
private var _gadt: GadtConstraint = _
175-
protected def gadt_=(gadt: GadtConstraint): Unit = _gadt = gadt
176-
final def gadt: GadtConstraint = _gadt
177-
178-
/** The history of implicit searches that are currently active */
179-
private var _searchHistory: SearchHistory = _
180-
protected def searchHistory_= (searchHistory: SearchHistory): Unit = _searchHistory = searchHistory
181-
final def searchHistory: SearchHistory = _searchHistory
182-
183-
/** The current source file */
184-
private var _source: SourceFile = _
185-
protected def source_=(source: SourceFile): Unit = _source = source
186-
final def source: SourceFile = _source
137+
def period: Period
138+
def mode: Mode
139+
def owner: Symbol
140+
def tree: Tree[?]
141+
def scope: Scope
142+
def typerState: TyperState
143+
def gadt: GadtConstraint
144+
def searchHistory: SearchHistory
145+
def source: SourceFile
187146

188147
/** A map in which more contextual properties can be stored
189148
* Typically used for attributes that are read and written only in special situations.
190149
*/
191-
private var _moreProperties: Map[Key[Any], Any] = _
192-
protected def moreProperties_=(moreProperties: Map[Key[Any], Any]): Unit = _moreProperties = moreProperties
193-
final def moreProperties: Map[Key[Any], Any] = _moreProperties
150+
def moreProperties: Map[Key[Any], Any]
194151

195152
def property[T](key: Key[T]): Option[T] =
196153
moreProperties.get(key).asInstanceOf[Option[T]]
@@ -200,9 +157,7 @@ object Contexts {
200157
* Access to store entries is much faster than access to properties, and only
201158
* slightly slower than a normal field access would be.
202159
*/
203-
private var _store: Store = _
204-
protected def store_=(store: Store): Unit = _store = store
205-
final def store: Store = _store
160+
def store: Store
206161

207162
/** The compiler callback implementation, or null if no callback will be called. */
208163
def compilerCallback: CompilerCallback = store(compilerCallbackLoc)
@@ -240,7 +195,7 @@ object Contexts {
240195
def typeAssigner: TypeAssigner = store(typeAssignerLoc)
241196

242197
/** The new implicit references that are introduced by this scope */
243-
protected var implicitsCache: ContextualImplicits | Null = null
198+
private var implicitsCache: ContextualImplicits | Null = null
244199
def implicits: ContextualImplicits = {
245200
if (implicitsCache == null)
246201
implicitsCache = {
@@ -305,7 +260,6 @@ object Contexts {
305260
/** AbstractFile with given path, memoized */
306261
def getFile(name: String): AbstractFile = getFile(name.toTermName)
307262

308-
309263
private var related: SimpleIdentityMap[Phase | SourceFile, Context] | Null = null
310264

311265
private def lookup(key: Phase | SourceFile): Context | Null =
@@ -491,30 +445,6 @@ object Contexts {
491445
/** Is the explicit nulls option set? */
492446
def explicitNulls: Boolean = base.settings.YexplicitNulls.value
493447

494-
/** Initialize all context fields, except typerState, which has to be set separately
495-
* @param outer The outer context
496-
* @param origin The context from which fields are copied
497-
*/
498-
private[Contexts] def init(outer: Context, origin: Context): this.type = {
499-
_outer = outer
500-
_period = origin.period
501-
_mode = origin.mode
502-
_owner = origin.owner
503-
_tree = origin.tree
504-
_scope = origin.scope
505-
_gadt = origin.gadt
506-
_searchHistory = origin.searchHistory
507-
_source = origin.source
508-
_moreProperties = origin.moreProperties
509-
_store = origin.store
510-
this
511-
}
512-
513-
def reuseIn(outer: Context): this.type =
514-
implicitsCache = null
515-
related = null
516-
init(outer, outer)
517-
518448
/** A fresh clone of this context embedded in this context. */
519449
def fresh: FreshContext = freshOver(this)
520450

@@ -565,6 +495,13 @@ object Contexts {
565495
def uniques: util.WeakHashSet[Type] = base.uniques
566496

567497
def initialize()(using Context): Unit = base.initialize()
498+
499+
protected def resetCaches(): Unit =
500+
implicitsCache = null
501+
related = null
502+
503+
/** Reuse this context as a fresh context nested inside `outer` */
504+
def reuseIn(outer: Context): this.type
568505
}
569506

570507
/** A condensed context provides only a small memory footprint over
@@ -579,6 +516,81 @@ object Contexts {
579516
* of its attributes using the with... methods.
580517
*/
581518
class FreshContext(base: ContextBase) extends Context(base) {
519+
520+
private var _outer: Context = _
521+
protected def outer_=(outer: Context): Unit = _outer = outer
522+
def outer: Context = _outer
523+
524+
private var _period: Period = _
525+
protected def period_=(period: Period): Unit =
526+
assert(period.firstPhaseId == period.lastPhaseId, period)
527+
_period = period
528+
final def period: Period = _period
529+
530+
private var _mode: Mode = _
531+
protected def mode_=(mode: Mode): Unit = _mode = mode
532+
final def mode: Mode = _mode
533+
534+
private var _owner: Symbol = _
535+
protected def owner_=(owner: Symbol): Unit = _owner = owner
536+
final def owner: Symbol = _owner
537+
538+
/** The current tree */
539+
private var _tree: Tree[?]= _
540+
protected def tree_=(tree: Tree[?]): Unit = _tree = tree
541+
final def tree: Tree[?] = _tree
542+
543+
private var _scope: Scope = _
544+
protected def scope_=(scope: Scope): Unit = _scope = scope
545+
final def scope: Scope = _scope
546+
547+
private var _typerState: TyperState = _
548+
protected def typerState_=(typerState: TyperState): Unit = _typerState = typerState
549+
final def typerState: TyperState = _typerState
550+
551+
private var _gadt: GadtConstraint = _
552+
protected def gadt_=(gadt: GadtConstraint): Unit = _gadt = gadt
553+
final def gadt: GadtConstraint = _gadt
554+
555+
private var _searchHistory: SearchHistory = _
556+
protected def searchHistory_= (searchHistory: SearchHistory): Unit = _searchHistory = searchHistory
557+
final def searchHistory: SearchHistory = _searchHistory
558+
559+
private var _source: SourceFile = _
560+
protected def source_=(source: SourceFile): Unit = _source = source
561+
final def source: SourceFile = _source
562+
563+
private var _moreProperties: Map[Key[Any], Any] = _
564+
protected def moreProperties_=(moreProperties: Map[Key[Any], Any]): Unit = _moreProperties = moreProperties
565+
final def moreProperties: Map[Key[Any], Any] = _moreProperties
566+
567+
private var _store: Store = _
568+
protected def store_=(store: Store): Unit = _store = store
569+
final def store: Store = _store
570+
571+
/** Initialize all context fields, except typerState, which has to be set separately
572+
* @param outer The outer context
573+
* @param origin The context from which fields are copied
574+
*/
575+
private[Contexts] def init(outer: Context, origin: Context): this.type = {
576+
_outer = outer
577+
_period = origin.period
578+
_mode = origin.mode
579+
_owner = origin.owner
580+
_tree = origin.tree
581+
_scope = origin.scope
582+
_gadt = origin.gadt
583+
_searchHistory = origin.searchHistory
584+
_source = origin.source
585+
_moreProperties = origin.moreProperties
586+
_store = origin.store
587+
this
588+
}
589+
590+
def reuseIn(outer: Context): this.type =
591+
resetCaches()
592+
init(outer, outer)
593+
582594
def setPeriod(period: Period): this.type =
583595
util.Stats.record("Context.setPeriod")
584596
this.period = period
@@ -627,7 +639,6 @@ object Contexts {
627639
util.Stats.record("Context.setStore")
628640
this.store = store
629641
this
630-
def setImplicits(implicits: ContextualImplicits): this.type = { this.implicitsCache = implicits; this }
631642

632643
def setCompilationUnit(compilationUnit: CompilationUnit): this.type = {
633644
setSource(compilationUnit.source)
@@ -817,7 +828,7 @@ object Contexts {
817828
gadt = GadtConstraint.empty
818829
}
819830

820-
@sharable object NoContext extends Context((null: ContextBase | Null).uncheckedNN) {
831+
@sharable val NoContext: Context = new FreshContext((null: ContextBase | Null).uncheckedNN) {
821832
source = NoSource
822833
override val implicits: ContextualImplicits = new ContextualImplicits(Nil, null, false)(this: @unchecked)
823834
}

0 commit comments

Comments
 (0)