Skip to content

Commit

Permalink
Include the type converted to as part of ImplicitConvert
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Romanov committed Nov 26, 2015
1 parent c620534 commit 973e84a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/common/ImplicitOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ trait ImplicitOps extends Base {
}

trait ImplicitOpsExp extends ImplicitOps with BaseExp {
case class ImplicitConvert[X,Y](x: Exp[X])(implicit val mX: Manifest[X], val mY: Manifest[Y]) extends Def[Y]
case class ImplicitConvert[X,Y](x: Exp[X], mY: Manifest[Y])(implicit val mX: Manifest[X]) extends Def[Y]

def implicit_convert[X,Y](x: Exp[X])(implicit c: X => Y, mX: Manifest[X], mY: Manifest[Y], pos: SourceContext) : Rep[Y] = {
if (mX == mY) x.asInstanceOf[Rep[Y]] else ImplicitConvert[X,Y](x)
if (mX == mY) x.asInstanceOf[Rep[Y]] else ImplicitConvert[X,Y](x, mY)
}

override def mirror[A:Manifest](e: Def[A], f: Transformer)(implicit pos: SourceContext): Exp[A] = (e match {
case im@ImplicitConvert(x) => toAtom(ImplicitConvert(f(x))(im.mX,im.mY))(mtype(manifest[A]),pos)
case im@ImplicitConvert(x, mY) => toAtom(ImplicitConvert(f(x), mY)(im.mX))(mtype(manifest[A]),pos)
case _ => super.mirror(e,f)
}).asInstanceOf[Exp[A]]

Expand All @@ -33,9 +33,9 @@ trait ScalaGenImplicitOps extends ScalaGenBase {
import IR._

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
// TODO: this valDef is redundant; we really just want the conversion to be a no-op in the generated code.
// TODO: but we still need to link the defs together
case ImplicitConvert(x) => emitValDef(sym, quote(x))
// Make sure it's typed to trigger the implicit conversion
// Otherwise we can get type mismatch in generated code
case ImplicitConvert(x, mY) => emitTypedValDef(sym, quote(x))
case _ => super.emitNode(sym, rhs)
}
}
Expand All @@ -46,8 +46,8 @@ trait CLikeGenImplicitOps extends CLikeGenBase {

override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
case im@ImplicitConvert(x) =>
gen"${im.mY} $sym = (${im.mY})$x;"
case ImplicitConvert(x, mY) =>
gen"$mY $sym = ($mY)$x;"
case _ => super.emitNode(sym, rhs)
}
}
Expand Down
26 changes: 21 additions & 5 deletions src/internal/ScalaCodegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,21 @@ trait ScalaCodegen extends GenericCodegen with Config {
fileName.substring(i + 1)
}

def emitValDef(sym: Sym[Any], rhs: String): Unit = {
val extra = if ((sourceinfo < 2) || sym.pos.isEmpty) "" else {
val context = sym.pos(0)
private def valDefExtra(sym: IR.Sym[Any]): String = {
if ((sourceinfo < 2) || sym.pos.isEmpty)
""
else {
val context = sym.pos.head
" // " + relativePath(context.fileName) + ":" + context.line
}
stream.println("val " + quote(sym) + " = " + rhs + extra)
}

def emitValDef(sym: Sym[Any], rhs: String): Unit = {
stream.println("val " + quote(sym) + " = " + rhs + valDefExtra(sym))
}

def emitTypedValDef(sym: Sym[Any], rhs: String): Unit = {
stream.println(src"val $sym: ${sym.tp} = $rhs" + valDefExtra(sym))
}

def emitVarDef(sym: Sym[Variable[Any]], rhs: String): Unit = {
Expand Down Expand Up @@ -132,7 +141,14 @@ trait ScalaNestedCodegen extends GenericNestedCodegen with ScalaCodegen {
else
super.emitValDef(sym,rhs)
}


// special case for recursive vals
override def emitTypedValDef(sym: Sym[Any], rhs: String): Unit = {
if (recursive contains sym)
stream.println(quote(sym) + " = " + rhs) // we have a forward declaration above.
else
super.emitTypedValDef(sym,rhs)
}
}


Expand Down

0 comments on commit 973e84a

Please sign in to comment.