-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathCastingOps.scala
75 lines (59 loc) · 3.39 KB
/
CastingOps.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package scala.lms
package common
import java.io.PrintWriter
import scala.lms.util.OverloadHack
import scala.reflect.SourceContext
trait CastingOps extends Variables with OverloadHack {
this: ImplicitOps =>
//implicit def anyToCastingOps[A:Manifest](lhs: A) = new CastingOpsCls(lhs)
implicit def repAnyToCastingOps[A:Manifest](lhs: Rep[A]) = new CastingOpsCls(lhs)
implicit def varAnyToCastingOps[A:Manifest](lhs: Var[A]) = new CastingOpsCls(readVar(lhs))
class CastingOpsCls[A:Manifest](lhs: Rep[A]){
def IsInstanceOf[B:Manifest](implicit pos: SourceContext): Rep[Boolean] = rep_isinstanceof(lhs, manifest[A], manifest[B])
def AsInstanceOf[B:Manifest](implicit pos: SourceContext): Rep[B] = rep_asinstanceof(lhs, manifest[A], manifest[B])
}
def rep_isinstanceof[A,B](lhs: Rep[A], mA: Manifest[A], mB: Manifest[B])(implicit pos: SourceContext) : Rep[Boolean]
def rep_asinstanceof[A,B:Manifest](lhs: Rep[A], mA: Manifest[A], mB: Manifest[B])(implicit pos: SourceContext) : Rep[B]
}
trait CastingOpsExp extends CastingOps with BaseExp with EffectExp {
this: ImplicitOps =>
case class RepIsInstanceOf[A,B](lhs: Exp[A], mA: Manifest[A], mB: Manifest[B]) extends Def[Boolean]
case class RepAsInstanceOf[A,B:Manifest](lhs: Exp[A], mA: Manifest[A], mB: Manifest[B]) extends Def[B]
def rep_isinstanceof[A,B](lhs: Exp[A], mA: Manifest[A], mB: Manifest[B])(implicit pos: SourceContext) = RepIsInstanceOf(lhs,mA,mB)
def rep_asinstanceof[A,B:Manifest](lhs: Exp[A], mA: Manifest[A], mB: Manifest[B])(implicit pos: SourceContext) : Exp[B] = toAtom(RepAsInstanceOf(lhs,mA,mB))(mB,pos)
override def mirror[A:Manifest](e: Def[A], f: Transformer)(implicit pos: SourceContext): Exp[A] = (e match {
case RepAsInstanceOf(lhs, mA, mB) => rep_asinstanceof(f(lhs),mA,mB)(mtype(mB),pos)
case Reflect(e@RepAsInstanceOf(lhs, mA, mB), u, es) => reflectMirrored(Reflect(RepAsInstanceOf(f(lhs),mA,mB)(mtype(mB)), mapOver(f,u), f(es)))(mtype(manifest[A]), pos)
case _ => super.mirror(e,f)
}).asInstanceOf[Exp[A]]
}
trait CastingOpsExpOpt extends CastingOpsExp {
this: ImplicitOps =>
override def rep_isinstanceof[A,B](lhs: Exp[A], mA: Manifest[A], mB: Manifest[B])(implicit pos: SourceContext) =
if (mA <:< mB) unit(true) else super.rep_isinstanceof(lhs, mA, mB)
override def rep_asinstanceof[A,B:Manifest](lhs: Exp[A], mA: Manifest[A], mB: Manifest[B])(implicit pos: SourceContext) : Exp[B] =
if (mA == mB) lhs.asInstanceOf[Exp[B]] else super.rep_asinstanceof(lhs, mA, mB)
}
trait ScalaGenCastingOps extends ScalaGenBase {
val IR: CastingOpsExp
import IR._
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
case RepIsInstanceOf(x,mA,mB) => emitValDef(sym, src"$x.isInstanceOf[$mB]")
case RepAsInstanceOf(x,mA,mB) => emitValDef(sym, src"$x.asInstanceOf[$mB]")
case _ => super.emitNode(sym, rhs)
}
}
trait CLikeGenCastingOps extends CLikeGenBase {
val IR: CastingOpsExp
import IR._
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
rhs match {
//case RepIsInstanceOf(x,mA,mB) => //TODO: How?
case RepAsInstanceOf(x,mA,mB) => emitValDef(sym, "(%s) %s".format(remapWithRef(mB),quote(x)))
case _ => super.emitNode(sym, rhs)
}
}
}
trait CudaGenCastingOps extends CudaGenBase with CLikeGenCastingOps
trait OpenCLGenCastingOps extends OpenCLGenBase with CLikeGenCastingOps
trait CGenCastingOps extends CGenBase with CLikeGenCastingOps