From 131ab5c1261608715cf2fde3aa44a04777cbda64 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 18 Jun 2015 13:27:43 +0200 Subject: Avoid double context creation when modes change on a fresh context. Move addMode and friends to two decorators, one for Context, the other for FreshContext. Implement behavior accordingly. This avoids creating two contexts in situations like: c.fresh.setxploreTyperState.addMode(...) Mow we can write c.fresh.addMode(...).setExploreTyperState Because addMode returns a fresh context when applied to a fresh context. Note that we specifically do not want virtual dispatch of addMode, that's why it was moved to a decorator. Also: removed mention of ".fresh: when just forllowed by an addMode, because that one is redundant. --- src/dotty/tools/dotc/core/Contexts.scala | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/dotty/tools/dotc/core/Contexts.scala') diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala index b00896117..c9deaab10 100644 --- a/src/dotty/tools/dotc/core/Contexts.scala +++ b/src/dotty/tools/dotc/core/Contexts.scala @@ -386,13 +386,6 @@ object Contexts { final def withOwner(owner: Symbol): Context = if (owner ne this.owner) fresh.setOwner(owner) else this - final def withMode(mode: Mode): Context = - if (mode != this.mode) fresh.setMode(mode) else this - - final def addMode(mode: Mode): Context = withMode(this.mode | mode) - final def maskMode(mode: Mode): Context = withMode(this.mode & mode) - final def retractMode(mode: Mode): Context = withMode(this.mode &~ mode) - override def toString = "Context(\n" + (outersIterator map ( ctx => s" owner = ${ctx.owner}, scope = ${ctx.scope}") mkString "\n") @@ -444,6 +437,21 @@ object Contexts { def setDebug = setSetting(base.settings.debug, true) } + implicit class ModeChanges(val c: Context) extends AnyVal { + final def withMode(mode: Mode): Context = + if (mode != c.mode) c.fresh.setMode(mode) else c + + final def addMode(mode: Mode): Context = withMode(c.mode | mode) + final def maskMode(mode: Mode): Context = withMode(c.mode & mode) + final def retractMode(mode: Mode): Context = withMode(c.mode &~ mode) + } + + implicit class FreshModeChanges(val c: FreshContext) extends AnyVal { + final def addMode(mode: Mode): c.type = c.setMode(c.mode | mode) + final def maskMode(mode: Mode): c.type = c.setMode(c.mode & mode) + final def retractMode(mode: Mode): c.type = c.setMode(c.mode &~ mode) + } + /** A class defining the initial context with given context base * and set of possible settings. */ -- cgit v1.2.3