summaryrefslogtreecommitdiff
path: root/test/files/run/macro-range
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-04-12 01:59:46 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-04-12 02:04:14 +0200
commit814cf34fb00f9ccb001249f4b3445ebc4f9942c9 (patch)
tree24dd54da571d27f10b0c482a6e08932c318fd7b2 /test/files/run/macro-range
parentdb3056f11730da19e4e56f09f12e300bda62f57c (diff)
downloadscala-814cf34fb00f9ccb001249f4b3445ebc4f9942c9.tar.gz
scala-814cf34fb00f9ccb001249f4b3445ebc4f9942c9.tar.bz2
scala-814cf34fb00f9ccb001249f4b3445ebc4f9942c9.zip
Next generation of macros
Implements SIP 16: Self-cleaning macros: http://bit.ly/wjjXTZ Features: * Macro defs * Reification * Type tags * Manifests aliased to type tags * Extended reflection API * Several hundred tests * 1111 changed files Not yet implemented: * Reification of refined types * Expr.value splicing * Named and default macro expansions * Intricacies of interaction between macros and implicits * Emission of debug information for macros (compliant with JSR-45) Dedicated to Yuri Alekseyevich Gagarin
Diffstat (limited to 'test/files/run/macro-range')
-rw-r--r--test/files/run/macro-range/Common_1.scala48
-rw-r--r--test/files/run/macro-range/Expansion_Impossible_2.scala53
-rw-r--r--test/files/run/macro-range/Expansion_Possible_3.scala7
-rw-r--r--test/files/run/macro-range/macro_range_1.scala99
-rw-r--r--test/files/run/macro-range/macro_range_2.scala99
5 files changed, 108 insertions, 198 deletions
diff --git a/test/files/run/macro-range/Common_1.scala b/test/files/run/macro-range/Common_1.scala
new file mode 100644
index 0000000000..bd46e1f529
--- /dev/null
+++ b/test/files/run/macro-range/Common_1.scala
@@ -0,0 +1,48 @@
+import reflect.api.Modifier
+import reflect.makro.Context
+
+abstract class RangeDefault {
+ val from, to: Int
+ def foreach(f: Int => Unit) = {
+ var i = from
+ while (i < to) { f(i); i += 1 }
+ }
+}
+
+/** This class should go into reflect.macro once it is a bit more stable. */
+abstract class Utils {
+ val context: Context
+ import context.mirror._
+
+ class TreeSubstituter(from: List[Symbol], to: List[Tree]) extends Transformer {
+ override def transform(tree: Tree): Tree = tree match {
+ case Ident(_) =>
+ def subst(from: List[Symbol], to: List[Tree]): Tree =
+ if (from.isEmpty) tree
+ else if (tree.symbol == from.head) to.head.duplicate // TODO: does it ever make sense *not* to perform a shallowDuplicate on `to.head`?
+ else subst(from.tail, to.tail);
+ subst(from, to)
+ case _ =>
+ val tree1 = super.transform(tree)
+ if (tree1 ne tree) tree1.tpe = null
+ tree1
+ }
+ }
+ def makeApply(fn: Tree, args: List[Tree]): Tree = fn match {
+ case Function(vparams, body) =>
+ new TreeSubstituter(vparams map (_.symbol), args) transform body
+ case Block(stats, expr) =>
+ Block(stats, makeApply(expr, args))
+ case _ =>
+ // todo. read the compiler config and print if -Ydebug is set
+ //println("no beta on "+fn+" "+fn.getClass)
+ Apply(fn, args)
+ }
+ def makeWhile(lname: TermName, cond: Tree, body: Tree): Tree = {
+ val continu = Apply(Ident(lname), Nil)
+ val rhs = If(cond, Block(List(body), continu), Literal(Constant()))
+ LabelDef(lname, Nil, rhs)
+ }
+ def makeBinop(left: Tree, op: String, right: Tree): Tree =
+ Apply(Select(left, newTermName(op)), List(right))
+}
diff --git a/test/files/run/macro-range/Expansion_Impossible_2.scala b/test/files/run/macro-range/Expansion_Impossible_2.scala
new file mode 100644
index 0000000000..7a093b74ee
--- /dev/null
+++ b/test/files/run/macro-range/Expansion_Impossible_2.scala
@@ -0,0 +1,53 @@
+import reflect.api.Modifier
+import reflect.makro.Context
+
+object Impls {
+ def foreach(c: Context)(f: c.Expr[Int => Unit]): c.Expr[Unit] = {
+ // todo. read the compiler config and print if -Ydebug is set
+ //println("macro-expand, _this = "+ _this)
+ object utils extends Utils { val context: c.type = c }
+ import utils._
+ import c.mirror._
+
+ val initName = newTermName("<init>")
+ // Either:
+ // scala"{ var i = $low; val h = $hi; while (i < h) { $f(i); i = i + 1 } }
+ // or:
+ // scala"($_this: RangeDefault).foreach($f)"
+ c.prefix.tree match {
+ case Apply(Select(New(tpt), initName), List(lo, hi)) if tpt.symbol.fullName == "Range" =>
+ val iname = newTermName("$i")
+ val hname = newTermName("$h")
+ def iref = Ident(iname)
+ def href = Ident(hname)
+ val labelname = newTermName("$while")
+ val cond = makeBinop(iref, "$less", href)
+ val body = Block(
+ List(makeApply(f, List(iref))),
+ Assign(iref, makeBinop(iref, "$plus", Literal(Constant(1)))))
+ val generated =
+ Block(
+ List(
+ ValDef(Modifiers(Set(Modifier.mutable)), iname, TypeTree(), lo),
+ ValDef(Modifiers(), hname, TypeTree(), hi)),
+ makeWhile(labelname, cond, body))
+ // todo. read the compiler config and print if -Ydebug is set
+ //tools.nsc.util.trace("generated: ")(generated)
+ generated
+ case _ =>
+ Apply(
+ Select(
+ Typed(c.prefix, Ident(newTypeName("RangeDefault"))),
+ newTermName("foreach")),
+ List(f))
+ }
+ }
+}
+
+class Range(val from: Int, val to: Int) extends RangeDefault {
+ override def foreach(f: Int => Unit): Unit = macro Impls.foreach
+}
+
+object Test extends App {
+ new Range(1, 10) foreach println
+} \ No newline at end of file
diff --git a/test/files/run/macro-range/Expansion_Possible_3.scala b/test/files/run/macro-range/Expansion_Possible_3.scala
new file mode 100644
index 0000000000..e7ecbcc362
--- /dev/null
+++ b/test/files/run/macro-range/Expansion_Possible_3.scala
@@ -0,0 +1,7 @@
+class Range(val from: Int, val to: Int) extends RangeDefault {
+ override def foreach(f: Int => Unit): Unit = macro Impls.foreach
+}
+
+object Test extends App {
+ new Range(1, 10) foreach println
+} \ No newline at end of file
diff --git a/test/files/run/macro-range/macro_range_1.scala b/test/files/run/macro-range/macro_range_1.scala
deleted file mode 100644
index fdfe7169ad..0000000000
--- a/test/files/run/macro-range/macro_range_1.scala
+++ /dev/null
@@ -1,99 +0,0 @@
-import reflect.api.Modifier
-import reflect.macro.Context
-
-abstract class RangeDefault {
- val from, to: Int
- def foreach(f: Int => Unit) = {
- var i = from
- while (i < to) { f(i); i += 1 }
- }
-}
-
-/** This class should go into reflect.macro once it is a bit more stable. */
-abstract class Utils {
- val context: Context
- import context._
-
- class TreeSubstituter(from: List[Symbol], to: List[Tree]) extends Transformer {
- override def transform(tree: Tree): Tree = tree match {
- case Ident(_) =>
- def subst(from: List[Symbol], to: List[Tree]): Tree =
- if (from.isEmpty) tree
- else if (tree.symbol == from.head) to.head.duplicate // TODO: does it ever make sense *not* to perform a shallowDuplicate on `to.head`?
- else subst(from.tail, to.tail);
- subst(from, to)
- case _ =>
- val tree1 = super.transform(tree)
- if (tree1 ne tree) tree1.tpe = null
- tree1
- }
- }
- def makeApply(fn: Tree, args: List[Tree]): Tree = fn match {
- case Function(vparams, body) =>
- new TreeSubstituter(vparams map (_.symbol), args) transform body
- case Block(stats, expr) =>
- Block(stats, makeApply(expr, args))
- case _ =>
- // todo. read the compiler config and print if -Ydebug is set
- //println("no beta on "+fn+" "+fn.getClass)
- Apply(fn, args)
- }
- def makeWhile(lname: TermName, cond: Tree, body: Tree): Tree = {
- val continu = Apply(Ident(lname), Nil)
- val rhs = If(cond, Block(List(body), continu), Literal(Constant()))
- LabelDef(lname, Nil, rhs)
- }
- def makeBinop(left: Tree, op: String, right: Tree): Tree =
- Apply(Select(left, newTermName(op)), List(right))
-}
-
-class Range(val from: Int, val to: Int) extends RangeDefault {
- override def macro foreach(f: Int => Unit): Unit = {
- // todo. read the compiler config and print if -Ydebug is set
- //println("macro-expand, _this = "+ _this)
- import _context._
- object utils extends Utils {
- val context: _context.type = _context
- }
- import utils._
-
- val initName = newTermName("<init>")
- // Either:
- // scala"{ var i = $low; val h = $hi; while (i < h) { $f(i); i = i + 1 } }
- // or:
- // scala"($_this: RangeDefault).foreach($f)"
- _this match {
- case Apply(Select(New(tpt), initName), List(lo, hi)) if tpt.symbol.fullName == "Range" =>
- val iname = newTermName("$i")
- val hname = newTermName("$h")
- def iref = Ident(iname)
- def href = Ident(hname)
- val labelname = newTermName("$while")
- val cond = makeBinop(iref, "$less", href)
- val body = Block(
- List(makeApply(f, List(iref))),
- Assign(iref, makeBinop(iref, "$plus", Literal(Constant(1)))))
- val generated =
- Block(
- List(
- ValDef(Modifiers(Set(Modifier.mutable)), iname, TypeTree(), lo),
- ValDef(Modifiers(), hname, TypeTree(), hi)),
- makeWhile(labelname, cond, body))
- // todo. read the compiler config and print if -Ydebug is set
- //tools.nsc.util.trace("generated: ")(generated)
- generated
- case _ =>
- Apply(
- Select(
- Typed(_this, Ident(newTypeName("RangeDefault"))),
- newTermName("foreach")),
- List(f))
- }
- }
-}
-
-object Test extends App {
-
- new Range(1, 10) foreach println
-
-}
diff --git a/test/files/run/macro-range/macro_range_2.scala b/test/files/run/macro-range/macro_range_2.scala
deleted file mode 100644
index fdfe7169ad..0000000000
--- a/test/files/run/macro-range/macro_range_2.scala
+++ /dev/null
@@ -1,99 +0,0 @@
-import reflect.api.Modifier
-import reflect.macro.Context
-
-abstract class RangeDefault {
- val from, to: Int
- def foreach(f: Int => Unit) = {
- var i = from
- while (i < to) { f(i); i += 1 }
- }
-}
-
-/** This class should go into reflect.macro once it is a bit more stable. */
-abstract class Utils {
- val context: Context
- import context._
-
- class TreeSubstituter(from: List[Symbol], to: List[Tree]) extends Transformer {
- override def transform(tree: Tree): Tree = tree match {
- case Ident(_) =>
- def subst(from: List[Symbol], to: List[Tree]): Tree =
- if (from.isEmpty) tree
- else if (tree.symbol == from.head) to.head.duplicate // TODO: does it ever make sense *not* to perform a shallowDuplicate on `to.head`?
- else subst(from.tail, to.tail);
- subst(from, to)
- case _ =>
- val tree1 = super.transform(tree)
- if (tree1 ne tree) tree1.tpe = null
- tree1
- }
- }
- def makeApply(fn: Tree, args: List[Tree]): Tree = fn match {
- case Function(vparams, body) =>
- new TreeSubstituter(vparams map (_.symbol), args) transform body
- case Block(stats, expr) =>
- Block(stats, makeApply(expr, args))
- case _ =>
- // todo. read the compiler config and print if -Ydebug is set
- //println("no beta on "+fn+" "+fn.getClass)
- Apply(fn, args)
- }
- def makeWhile(lname: TermName, cond: Tree, body: Tree): Tree = {
- val continu = Apply(Ident(lname), Nil)
- val rhs = If(cond, Block(List(body), continu), Literal(Constant()))
- LabelDef(lname, Nil, rhs)
- }
- def makeBinop(left: Tree, op: String, right: Tree): Tree =
- Apply(Select(left, newTermName(op)), List(right))
-}
-
-class Range(val from: Int, val to: Int) extends RangeDefault {
- override def macro foreach(f: Int => Unit): Unit = {
- // todo. read the compiler config and print if -Ydebug is set
- //println("macro-expand, _this = "+ _this)
- import _context._
- object utils extends Utils {
- val context: _context.type = _context
- }
- import utils._
-
- val initName = newTermName("<init>")
- // Either:
- // scala"{ var i = $low; val h = $hi; while (i < h) { $f(i); i = i + 1 } }
- // or:
- // scala"($_this: RangeDefault).foreach($f)"
- _this match {
- case Apply(Select(New(tpt), initName), List(lo, hi)) if tpt.symbol.fullName == "Range" =>
- val iname = newTermName("$i")
- val hname = newTermName("$h")
- def iref = Ident(iname)
- def href = Ident(hname)
- val labelname = newTermName("$while")
- val cond = makeBinop(iref, "$less", href)
- val body = Block(
- List(makeApply(f, List(iref))),
- Assign(iref, makeBinop(iref, "$plus", Literal(Constant(1)))))
- val generated =
- Block(
- List(
- ValDef(Modifiers(Set(Modifier.mutable)), iname, TypeTree(), lo),
- ValDef(Modifiers(), hname, TypeTree(), hi)),
- makeWhile(labelname, cond, body))
- // todo. read the compiler config and print if -Ydebug is set
- //tools.nsc.util.trace("generated: ")(generated)
- generated
- case _ =>
- Apply(
- Select(
- Typed(_this, Ident(newTypeName("RangeDefault"))),
- newTermName("foreach")),
- List(f))
- }
- }
-}
-
-object Test extends App {
-
- new Range(1, 10) foreach println
-
-}