Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify RefNode: Only one Symbol per Node allowed
  • Loading branch information
szeiger committed Oct 19, 2012
1 parent a568b8f commit e7458ce
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
16 changes: 5 additions & 11 deletions src/main/scala/scala/slick/ast/Dump.scala
Expand Up @@ -42,15 +42,9 @@ class DumpContext(val out: PrintWriter, val typed: Boolean = true) {
val toScan = newRefs.toSet
newRefs.clear()
toScan.foreach { _.target.foreach {
case r: RefNode =>
r.nodeReferences.foreach {
case g: IntrinsicSymbol =>
if(!refs.contains(g)) {
refs += g
newRefs += g
}
case _ =>
}
case r @ RefNode(g: IntrinsicSymbol) if !refs.contains(g) =>
refs += g
newRefs += g
case _ =>
}}
}
Expand All @@ -61,7 +55,7 @@ class DumpContext(val out: PrintWriter, val typed: Boolean = true) {
def dump(tree: Node, prefix: String, name: String) {
tree match {
case n: DefNode => n.nodeGenerators.foreach(t => addDef(t._1))
case n: RefNode => n.nodeReferences.foreach(addRef)
case RefNode(s) => addRef(s)
case _ =>
}
val typeInfo = tree match {
Expand All @@ -72,7 +66,7 @@ class DumpContext(val out: PrintWriter, val typed: Boolean = true) {
case Path(l @ (_ :: _ :: _)) =>
// Print paths on a single line
out.println(prefix + name + Path.toString(l) + typeInfo)
tree.foreach { case n: RefNode => n.nodeReferences.foreach(addRef) }
tree.foreach { case RefNode(s) => addRef(s) }
case _ =>
out.println(prefix + name + tree + typeInfo)
for((chg, n) <- tree.nodeChildren.zip(tree.nodeChildNames))
Expand Down
14 changes: 7 additions & 7 deletions src/main/scala/scala/slick/ast/Node.scala
Expand Up @@ -291,8 +291,8 @@ final case class Select(in: Node, field: Symbol) extends UnaryNode with RefNode
def child = in
override def nodeChildNames = Seq("in")
protected[this] def nodeRebuild(child: Node) = copy(in = child)
def nodeReferences: Seq[Symbol] = Seq(field)
protected[this] def nodeRebuildWithReferences(gen: IndexedSeq[Symbol]) = copy(field = gen(0))
def nodeReference = field
protected[this] def nodeRebuildWithReference(s: Symbol) = copy(field = s)
override def toString = Path.unapply(this) match {
case Some(l) => super.toString + " for " + Path.toString(l)
case None => super.toString
Expand All @@ -302,8 +302,8 @@ final case class Select(in: Node, field: Symbol) extends UnaryNode with RefNode
case class Apply(sym: Symbol, children: Seq[Node]) extends RefNode {
def nodeChildren = children
protected[this] def nodeRebuild(ch: IndexedSeq[scala.slick.ast.Node]) = copy(children = ch)
def nodeReferences: Seq[Symbol] = Seq(sym)
protected[this] def nodeRebuildWithReferences(syms: IndexedSeq[Symbol]) = copy(sym = syms(0))
def nodeReference = sym
protected[this] def nodeRebuildWithReference(s: Symbol) = copy(sym = s)
override def toString = "Apply "+sym
}

Expand All @@ -313,14 +313,14 @@ object Apply {
new Apply(sym, children) with TypedNode {
def tpe = tp
override protected[this] def nodeRebuild(ch: IndexedSeq[scala.slick.ast.Node]) = Apply(sym, ch, tp)
override protected[this] def nodeRebuildWithReferences(syms: IndexedSeq[Symbol]) = Apply(syms(0), children, tp)
override protected[this] def nodeRebuildWithReference(s: Symbol) = Apply(s, children, tp)
}
}

/** A reference to a Symbol */
case class Ref(sym: Symbol) extends NullaryNode with RefNode {
def nodeReferences = Seq(sym)
protected[this] def nodeRebuildWithReferences(gen: IndexedSeq[Symbol]) = copy(sym = gen(0))
def nodeReference = sym
protected[this] def nodeRebuildWithReference(s: Symbol) = copy(sym = s)
}

/** A constructor/extractor for nested Selects starting at a Ref */
Expand Down
14 changes: 9 additions & 5 deletions src/main/scala/scala/slick/ast/Symbol.scala
Expand Up @@ -57,12 +57,16 @@ trait DefNode extends Node {
mapOrNone(nodeGenerators.map(_._1), f).fold[Node](this)(s => nodeRebuildWithGenerators(s.toIndexedSeq))
}

/** A Node which references Symbols. */
/** A Node which references a Symbol. */
trait RefNode extends Node {
def nodeReferences: Seq[Symbol]
final def nodeMapReferences(f: Symbol => Symbol): Node =
mapOrNone(nodeReferences, f).fold[Node](this)(s => nodeRebuildWithReferences(s.toIndexedSeq))
protected[this] def nodeRebuildWithReferences(gen: IndexedSeq[Symbol]): Node
def nodeReference: Symbol
final def nodeWithReference(s: Symbol): RefNode =
if(s eq nodeReference) this else nodeRebuildWithReference(s)
protected[this] def nodeRebuildWithReference(s: Symbol): RefNode
}

object RefNode {
def unapply(r: RefNode) = Some(r.nodeReference)
}

/** Provides names for symbols */
Expand Down
6 changes: 1 addition & 5 deletions src/main/scala/scala/slick/compiler/Inline.scala
Expand Up @@ -20,11 +20,7 @@ class Inline(unique: Boolean = true, paths: Boolean = true, from: Boolean = true
def apply(tree: Node, state: CompilationState): Node = {
val counts = new HashMap[AnonSymbol, Int]
tree.foreach {
case r: RefNode => r.nodeReferences.foreach {
case a: AnonSymbol =>
counts += a -> (counts.getOrElse(a, 0) + 1)
case s =>
}
case RefNode(a: AnonSymbol) => counts += a -> (counts.getOrElse(a, 0) + 1)
case _ =>
}
val (tree2, globals) = tree match {
Expand Down
13 changes: 5 additions & 8 deletions src/main/scala/scala/slick/compiler/LocalizeRefs.scala
Expand Up @@ -11,14 +11,11 @@ class LocalizeRefs extends Phase {
val newNodes = new HashMap[AnonSymbol, Node]
val tr = new Transformer {
def replace = {
case r: RefNode => r.nodeMapReferences {
case IntrinsicSymbol(target) =>
val s = new AnonSymbol
map += s -> target
newNodes += s -> target
s
case s => s
}
case r @ RefNode(IntrinsicSymbol(target)) =>
val s = new AnonSymbol
map += s -> target
newNodes += s -> target
r.nodeWithReference(s)
}
}
val tree2 = tr.once(tree)
Expand Down

0 comments on commit e7458ce

Please sign in to comment.