summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-05-07 22:50:30 +0200
committerGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-05-07 22:50:30 +0200
commitf52d31f1ffd90506934ab31aff3c50b9d24d6ec6 (patch)
tree7b7b3a04ac38f1185fb4b42582a1a21d18c6476c
parentcc58870344e9eea88b8eb18b99176f6db04c7724 (diff)
downloadscala-f52d31f1ffd90506934ab31aff3c50b9d24d6ec6.tar.gz
scala-f52d31f1ffd90506934ab31aff3c50b9d24d6ec6.tar.bz2
scala-f52d31f1ffd90506934ab31aff3c50b9d24d6ec6.zip
Refactor FastTrack to use composition instead of inheritance.
Instead of mixing in FastTrack into Macros trait just have a member with an instance of FastTrack. The motivation for this change is to reduce the overall use of inheritance is Scala compiler and FastTrack seems like a nice target for first step. It's an implementation detail of Scala compiler that we are free to modify. Previously, `fastTrack` method would be inherited from FastTrack trait and called from clients (sub classes). The `fastTrack` method returned Map. Now, the `fastTrack` viariable is of type `FastTrack`. In order for clients to continue to work we had to implement three operations called by clients: contains, apply, get. Alternatively, we could keep the `fastTrack` method and import it in clients. This approach is likely to be more common in the future when bigger pieces of code get refactored.
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Macros.scala4
-rw-r--r--src/compiler/scala/tools/reflect/FastTrack.scala9
2 files changed, 9 insertions, 4 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
index 9cf92ca5b9..f4456998c0 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
@@ -42,7 +42,7 @@ import Fingerprint._
* (Expr(elems))
* (TypeTag(Int))
*/
-trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers {
+trait Macros extends MacroRuntimes with Traces with Helpers {
self: Analyzer =>
import global._
@@ -50,6 +50,8 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers {
import treeInfo.{isRepeatedParamType => _, _}
import MacrosStats._
+ lazy val fastTrack = new FastTrack[self.type](self)
+
def globalSettings = global.settings
protected def findMacroClassLoader(): ClassLoader = {
diff --git a/src/compiler/scala/tools/reflect/FastTrack.scala b/src/compiler/scala/tools/reflect/FastTrack.scala
index 8630ecf69e..64cf3d0847 100644
--- a/src/compiler/scala/tools/reflect/FastTrack.scala
+++ b/src/compiler/scala/tools/reflect/FastTrack.scala
@@ -10,14 +10,18 @@ import scala.tools.reflect.quasiquotes.{ Quasiquotes => QuasiquoteImpls }
/** Optimizes system macro expansions by hardwiring them directly to their implementations
* bypassing standard reflective load and invoke to avoid the overhead of Java/Scala reflection.
*/
-trait FastTrack {
- self: Macros with Analyzer =>
+class FastTrack[MacrosAndAnalyzer <: Macros with Analyzer](val macros: MacrosAndAnalyzer) {
+ import macros._
import global._
import definitions._
import scala.language.implicitConversions
import treeInfo.Applied
+ def contains(symbol: Symbol): Boolean = fastTrackCache().contains(symbol)
+ def apply(symbol: Symbol): FastTrackEntry = fastTrackCache().apply(symbol)
+ def get(symbol: Symbol): Option[FastTrackEntry] = fastTrackCache().get(symbol)
+
private implicit def context2taggers(c0: MacroContext): Taggers { val c: c0.type } =
new { val c: c0.type = c0 } with Taggers
private implicit def context2macroimplementations(c0: MacroContext): FormatInterpolator { val c: c0.type } =
@@ -39,7 +43,6 @@ trait FastTrack {
}
/** A map from a set of pre-established macro symbols to their implementations. */
- def fastTrack: Map[Symbol, FastTrackEntry] = fastTrackCache()
private val fastTrackCache = perRunCaches.newGeneric[Map[Symbol, FastTrackEntry]] {
val runDefinitions = currentRun.runDefinitions
import runDefinitions._