Skip to content

Commit 28010ee

Browse files
committed
More Coontext refactorings
1 parent d62ffc8 commit 28010ee

File tree

1 file changed

+83
-72
lines changed

1 file changed

+83
-72
lines changed

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

Lines changed: 83 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import printing._
2828
import config.{JavaPlatform, SJSPlatform, Platform, ScalaSettings}
2929
import classfile.ReusableDataReader
3030
import StdNames.nme
31+
import compiletime.uninitialized
3132

3233
import scala.annotation.internal.sharable
3334

@@ -310,7 +311,7 @@ object Contexts {
310311
/** If -Ydebug is on, the top of the stack trace where this context
311312
* was created, otherwise `null`.
312313
*/
313-
private var creationTrace: Array[StackTraceElement] = _
314+
private var creationTrace: Array[StackTraceElement] = uninitialized
314315
315316
private def setCreationTrace() =
316317
creationTrace = (new Throwable).getStackTrace().take(20)
@@ -517,55 +518,41 @@ object Contexts {
517518
*/
518519
class FreshContext(base: ContextBase) extends Context(base) {
519520

520-
private var _outer: Context = _
521-
protected def outer_=(outer: Context): Unit = _outer = outer
521+
private var _outer: Context = uninitialized
522522
def outer: Context = _outer
523523

524-
private var _period: Period = _
525-
protected def period_=(period: Period): Unit =
526-
assert(period.firstPhaseId == period.lastPhaseId, period)
524+
private var _period: Period = uninitialized
527525
_period = period
528526
final def period: Period = _period
529527

530-
private var _mode: Mode = _
531-
protected def mode_=(mode: Mode): Unit = _mode = mode
528+
private var _mode: Mode = uninitialized
532529
final def mode: Mode = _mode
533530

534-
private var _owner: Symbol = _
535-
protected def owner_=(owner: Symbol): Unit = _owner = owner
531+
private var _owner: Symbol = uninitialized
536532
final def owner: Symbol = _owner
537533

538-
/** The current tree */
539534
private var _tree: Tree[?]= _
540-
protected def tree_=(tree: Tree[?]): Unit = _tree = tree
541535
final def tree: Tree[?] = _tree
542536

543-
private var _scope: Scope = _
544-
protected def scope_=(scope: Scope): Unit = _scope = scope
537+
private var _scope: Scope = uninitialized
545538
final def scope: Scope = _scope
546539

547-
private var _typerState: TyperState = _
548-
protected def typerState_=(typerState: TyperState): Unit = _typerState = typerState
540+
private var _typerState: TyperState = uninitialized
549541
final def typerState: TyperState = _typerState
550542

551-
private var _gadt: GadtConstraint = _
552-
protected def gadt_=(gadt: GadtConstraint): Unit = _gadt = gadt
543+
private var _gadt: GadtConstraint = uninitialized
553544
final def gadt: GadtConstraint = _gadt
554545

555-
private var _searchHistory: SearchHistory = _
556-
protected def searchHistory_= (searchHistory: SearchHistory): Unit = _searchHistory = searchHistory
546+
private var _searchHistory: SearchHistory = uninitialized
557547
final def searchHistory: SearchHistory = _searchHistory
558548

559-
private var _source: SourceFile = _
560-
protected def source_=(source: SourceFile): Unit = _source = source
549+
private var _source: SourceFile = uninitialized
561550
final def source: SourceFile = _source
562551

563-
private var _moreProperties: Map[Key[Any], Any] = _
564-
protected def moreProperties_=(moreProperties: Map[Key[Any], Any]): Unit = _moreProperties = moreProperties
552+
private var _moreProperties: Map[Key[Any], Any] = uninitialized
565553
final def moreProperties: Map[Key[Any], Any] = _moreProperties
566554

567-
private var _store: Store = _
568-
protected def store_=(store: Store): Unit = _store = store
555+
private var _store: Store = uninitialized
569556
final def store: Store = _store
570557

571558
/** Initialize all context fields, except typerState, which has to be set separately
@@ -593,51 +580,74 @@ object Contexts {
593580

594581
def setPeriod(period: Period): this.type =
595582
util.Stats.record("Context.setPeriod")
596-
this.period = period
583+
assert(period.firstPhaseId == period.lastPhaseId, period)
584+
this._period = period
597585
this
586+
598587
def setMode(mode: Mode): this.type =
599588
util.Stats.record("Context.setMode")
600-
this.mode = mode
589+
this._mode = mode
601590
this
591+
602592
def setOwner(owner: Symbol): this.type =
603593
util.Stats.record("Context.setOwner")
604594
assert(owner != NoSymbol)
605-
this.owner = owner
595+
this._owner = owner
606596
this
597+
607598
def setTree(tree: Tree[?]): this.type =
608599
util.Stats.record("Context.setTree")
609-
this.tree = tree
600+
this._tree = tree
610601
this
611-
def setScope(scope: Scope): this.type = { this.scope = scope; this }
602+
603+
def setScope(scope: Scope): this.type =
604+
this._scope = scope
605+
this
606+
612607
def setNewScope: this.type =
613608
util.Stats.record("Context.setScope")
614-
this.scope = newScope
609+
this._scope = newScope
615610
this
616-
def setTyperState(typerState: TyperState): this.type = { this.typerState = typerState; this }
617-
def setNewTyperState(): this.type = setTyperState(typerState.fresh(committable = true))
618-
def setExploreTyperState(): this.type = setTyperState(typerState.fresh(committable = false))
619-
def setReporter(reporter: Reporter): this.type = setTyperState(typerState.fresh().setReporter(reporter))
620-
def setTyper(typer: Typer): this.type = { this.scope = typer.scope; setTypeAssigner(typer) }
611+
612+
def setTyperState(typerState: TyperState): this.type =
613+
this._typerState = typerState
614+
this
615+
def setNewTyperState(): this.type =
616+
setTyperState(typerState.fresh(committable = true))
617+
def setExploreTyperState(): this.type =
618+
setTyperState(typerState.fresh(committable = false))
619+
def setReporter(reporter: Reporter): this.type =
620+
setTyperState(typerState.fresh().setReporter(reporter))
621+
622+
def setTyper(typer: Typer): this.type =
623+
this._scope = typer.scope
624+
setTypeAssigner(typer)
625+
621626
def setGadt(gadt: GadtConstraint): this.type =
622627
util.Stats.record("Context.setGadt")
623-
this.gadt = gadt
628+
this._gadt = gadt
624629
this
625-
def setFreshGADTBounds: this.type = setGadt(gadt.fresh)
630+
def setFreshGADTBounds: this.type =
631+
setGadt(gadt.fresh)
632+
626633
def setSearchHistory(searchHistory: SearchHistory): this.type =
627634
util.Stats.record("Context.setSearchHistory")
628-
this.searchHistory = searchHistory
635+
this._searchHistory = searchHistory
629636
this
637+
630638
def setSource(source: SourceFile): this.type =
631639
util.Stats.record("Context.setSource")
632-
this.source = source
640+
this._source = source
633641
this
642+
634643
private def setMoreProperties(moreProperties: Map[Key[Any], Any]): this.type =
635644
util.Stats.record("Context.setMoreProperties")
636-
this.moreProperties = moreProperties
645+
this._moreProperties = moreProperties
637646
this
647+
638648
private def setStore(store: Store): this.type =
639649
util.Stats.record("Context.setStore")
640-
this.store = store
650+
this._store = store
641651
this
642652

643653
def setCompilationUnit(compilationUnit: CompilationUnit): this.type = {
@@ -692,6 +702,28 @@ object Contexts {
692702
def setDebug: this.type = setSetting(base.settings.Ydebug, true)
693703
}
694704

705+
object FreshContext:
706+
/** Defines an initial context with given context base and possible settings. */
707+
def initial(base: ContextBase, settingsGroup: SettingGroup): Context =
708+
val c = new FreshContext(base)
709+
c._outer = NoContext
710+
c._period = InitialPeriod
711+
c._mode = Mode.None
712+
c._typerState = TyperState.initialState()
713+
c._owner = NoSymbol
714+
c._tree = untpd.EmptyTree
715+
c._moreProperties = Map(MessageLimiter -> DefaultMessageLimiter())
716+
c._scope = EmptyScope
717+
c._source = NoSource
718+
c._store = initialStore
719+
.updated(settingsStateLoc, settingsGroup.defaultState)
720+
.updated(notNullInfosLoc, Nil)
721+
.updated(compilationUnitLoc, NoCompilationUnit)
722+
c._searchHistory = new SearchRoot
723+
c._gadt = GadtConstraint.empty
724+
c
725+
end FreshContext
726+
695727
given ops: AnyRef with
696728
extension (c: Context)
697729
def addNotNullInfo(info: NotNullInfo) =
@@ -807,30 +839,9 @@ object Contexts {
807839
finally ctx.base.comparersInUse = saved
808840
end comparing
809841

810-
/** A class defining the initial context with given context base
811-
* and set of possible settings.
812-
*/
813-
private class InitialContext(base: ContextBase, settingsGroup: SettingGroup) extends FreshContext(base) {
814-
outer = NoContext
815-
period = InitialPeriod
816-
mode = Mode.None
817-
typerState = TyperState.initialState()
818-
owner = NoSymbol
819-
tree = untpd.EmptyTree
820-
moreProperties = Map(MessageLimiter -> DefaultMessageLimiter())
821-
scope = EmptyScope
822-
source = NoSource
823-
store = initialStore
824-
.updated(settingsStateLoc, settingsGroup.defaultState)
825-
.updated(notNullInfosLoc, Nil)
826-
.updated(compilationUnitLoc, NoCompilationUnit)
827-
searchHistory = new SearchRoot
828-
gadt = GadtConstraint.empty
829-
}
830-
831842
@sharable val NoContext: Context = new FreshContext((null: ContextBase | Null).uncheckedNN) {
832-
source = NoSource
833843
override val implicits: ContextualImplicits = new ContextualImplicits(Nil, null, false)(this: @unchecked)
844+
setSource(NoSource)
834845
}
835846

836847
/** A context base defines state and associated methods that exist once per
@@ -844,10 +855,10 @@ object Contexts {
844855
val settings: ScalaSettings = new ScalaSettings
845856

846857
/** The initial context */
847-
val initialCtx: Context = new InitialContext(this, settings)
858+
val initialCtx: Context = FreshContext.initial(this: @unchecked, settings)
848859

849860
/** The platform, initialized by `initPlatform()`. */
850-
private var _platform: Platform | Null = _
861+
private var _platform: Platform | Null = uninitialized
851862

852863
/** The platform */
853864
def platform: Platform = {
@@ -933,18 +944,18 @@ object Contexts {
933944

934945
// Phases state
935946

936-
private[core] var phasesPlan: List[List[Phase]] = _
947+
private[core] var phasesPlan: List[List[Phase]] = uninitialized
937948

938949
/** Phases by id */
939-
private[dotc] var phases: Array[Phase] = _
950+
private[dotc] var phases: Array[Phase] = uninitialized
940951

941952
/** Phases with consecutive Transforms grouped into a single phase, Empty array if fusion is disabled */
942953
private[core] var fusedPhases: Array[Phase] = Array.empty[Phase]
943954

944955
/** Next denotation transformer id */
945-
private[core] var nextDenotTransformerId: Array[Int] = _
956+
private[core] var nextDenotTransformerId: Array[Int] = uninitialized
946957

947-
private[core] var denotTransformers: Array[DenotTransformer] = _
958+
private[core] var denotTransformers: Array[DenotTransformer] = uninitialized
948959

949960
/** Flag to suppress inlining, set after overflow */
950961
private[dotc] var stopInlining: Boolean = false
@@ -978,7 +989,7 @@ object Contexts {
978989

979990
private[core] val reusableDataReader = ReusableInstance(new ReusableDataReader())
980991

981-
private[dotc] var wConfCache: (List[String], WConf) = _
992+
private[dotc] var wConfCache: (List[String], WConf) = uninitialized
982993

983994
def sharedCharArray(len: Int): Array[Char] =
984995
while len > charArray.length do

0 commit comments

Comments
 (0)