diff options
author | Martin Odersky <odersky@gmail.com> | 2013-01-29 09:44:37 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2013-01-29 09:44:37 +0100 |
commit | bbc4f7a3234937e5f79e8310e6fff2f9b4af0f98 (patch) | |
tree | 36537cd033d5e37a489839f2970245df9db5e544 /src/dotty/tools/dotc/core/Contexts.scala | |
parent | 9770566c50baff03a7e61344c203b29db8750e8f (diff) | |
download | dotty-bbc4f7a3234937e5f79e8310e6fff2f9b4af0f98.tar.gz dotty-bbc4f7a3234937e5f79e8310e6fff2f9b4af0f98.tar.bz2 dotty-bbc4f7a3234937e5f79e8310e6fff2f9b4af0f98.zip |
New Context architecture based on cloning
Diffstat (limited to 'src/dotty/tools/dotc/core/Contexts.scala')
-rw-r--r-- | src/dotty/tools/dotc/core/Contexts.scala | 111 |
1 files changed, 78 insertions, 33 deletions
diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala index 645820792..6bfc6233b 100644 --- a/src/dotty/tools/dotc/core/Contexts.scala +++ b/src/dotty/tools/dotc/core/Contexts.scala @@ -13,52 +13,95 @@ import collection.immutable.BitSet object Contexts { - val NoContext: Context = null - - abstract class Context extends Periods with Substituters with TypeOps { + abstract class Context extends Periods with Substituters with TypeOps with Cloneable { implicit val ctx: Context = this - val underlying: Context - val root: RootContext - val period: Period - def constraints: Constraints - def typeComparer: TypeComparer - def printer: Printer = ??? - def names: NameTable + + def base: ContextBase + + private[this] var _underlying: Context = _ + protected def underlying_=(underlying: Context) = _underlying = underlying + def underlying: Context = _underlying + + private[this] var _period: Period = _ + protected def period_=(period: Period) = _period = period + def period: Period = _period + + private[this] var _constraints: Constraints = _ + protected def constraints_=(constraints: Constraints) = _constraints = constraints + def constraints: Constraints = _constraints + + private[this] var _typeComparer: TypeComparer = _ + protected def typeComparer_=(typeComparer: TypeComparer) = _typeComparer = typeComparer + + def typeComparer: TypeComparer = { + if ((_typeComparer eq underlying.typeComparer) && + (constraints ne underlying.constraints)) + _typeComparer = new TypeComparer(this) + _typeComparer + } + + private[this] var _printer: Printer = _ + protected def printer_=(printer: Printer) = _printer = printer + def printer: Printer = _printer + + private[this] var _owner: Symbol = _ + protected def owner_=(owner: Symbol) = _owner = owner + def owner: Symbol = _owner + + private[this] var _diagnostics: Option[StringBuilder] = _ + protected def diagnostics_=(diagnostics: Option[StringBuilder]) = _diagnostics = diagnostics + def diagnostics: Option[StringBuilder] = _diagnostics + + def diagnose(str: => String) = + for (sb <- diagnostics) { + sb.setLength(0) + sb.append(str) + } + + + def phase: Phase = ??? // phase(period.phaseId) def enclClass: Context = ??? - def phase: Phase = ??? - def owner: Symbol = ??? def erasedTypes: Boolean = ??? +// def settings: Settings = ??? + def warning(msg: String) = ??? + + def fresh: FreshContext = { + val newctx = super.clone.asInstanceOf[FreshContext] + newctx.underlying = this + newctx + } + } - abstract class DiagnosticsContext(ctx: Context) extends SubContext(ctx) { - var diagnostics: () => String + abstract class FreshContext extends Context { + def withPeriod(period: Period): this.type = { this.period = period; this } + def withPhase(pid: PhaseId): this.type = withPeriod(Period(runId, pid)) + def withConstraints(constraints: Constraints): this.type = { this.constraints = constraints; this } + def withPrinter(printer: Printer): this.type = { this.printer = printer; this } + def withOwner(owner: Symbol): this.type = { this.owner = owner; this } + def withDiagnostics(diagnostics: Option[StringBuilder]): this.type = { this.diagnostics = diagnostics; this } } - abstract class SubContext(val underlying: Context) extends Context { - val root: RootContext = underlying.root - val period: Period = underlying.period - val constraints = underlying.constraints - def names: NameTable = root.names - lazy val typeComparer = - if (constraints eq underlying.constraints) underlying.typeComparer - else new TypeComparer(this) + private class InitialContext(val base: ContextBase) extends FreshContext { + underlying = NoContext + period = Nowhere + constraints = Map() + printer = new StdPrinter + owner = NoSymbol } - class RootContext extends Context - with Transformers { + object NoContext extends Context { + val base = unsupported("base") + } - val underlying: Context = throw new UnsupportedOperationException("RootContext.underlying") - def typeComparer: TypeComparer = ??? + class ContextBase extends Transformers.TransformerBase + with Printers.PrinterBase { - val root: RootContext = this - val period = Nowhere - val names: NameTable = new NameTable - val variance = 1 + val initialCtx: Context = new InitialContext(this) - var lastPhaseId: Int = NoPhaseId - lazy val definitions = new Definitions()(this) + val names: NameTable = new NameTable - val constraints: Constraints = Map() + lazy val definitions = new Definitions()(initialCtx) // Symbols state /** A map from a superclass id to the class that has it */ @@ -94,6 +137,8 @@ object Contexts { private[core] val pendingVolatiles = new mutable.HashSet[Type] } + implicit def ctxToBase(ctx: Context): ContextBase = ctx.base + /** Initial size of superId table */ private final val InitialSuperIdsSize = 4096 |