From 814cf34fb00f9ccb001249f4b3445ebc4f9942c9 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Thu, 12 Apr 2012 01:59:46 +0200 Subject: 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 --- .../run/macro-sip19-revised/Impls_Macros_1.scala | 34 ++++++++++++++++++++++ test/files/run/macro-sip19-revised/Test_2.scala | 12 ++++++++ 2 files changed, 46 insertions(+) create mode 100644 test/files/run/macro-sip19-revised/Impls_Macros_1.scala create mode 100644 test/files/run/macro-sip19-revised/Test_2.scala (limited to 'test/files/run/macro-sip19-revised') diff --git a/test/files/run/macro-sip19-revised/Impls_Macros_1.scala b/test/files/run/macro-sip19-revised/Impls_Macros_1.scala new file mode 100644 index 0000000000..e8f6e1df85 --- /dev/null +++ b/test/files/run/macro-sip19-revised/Impls_Macros_1.scala @@ -0,0 +1,34 @@ +import scala.reflect.makro.Context + +object Macros { + def impl(c: Context) = { + import c.mirror._ + + val inscope = c.inferImplicitValue(staticClass("SourceLocation").asType) + val outer = Expr[SourceLocation](if (!inscope.isEmpty) inscope else Literal(Constant(null))) + + val Apply(fun, args) = c.enclosingImplicits(0)._2 + val fileName = fun.pos.fileInfo.getName + val line = fun.pos.line + val charOffset = fun.pos.point + c.reify { SourceLocation1(outer.eval, c.literal(fileName).eval, c.literal(line).eval, c.literal(charOffset).eval) } + } + + implicit def sourceLocation: SourceLocation1 = macro impl +} + +trait SourceLocation { + /** Source location of the outermost call */ + val outer: SourceLocation + + /** The name of the source file */ + val fileName: String + + /** The line number */ + val line: Int + + /** The character offset */ + val charOffset: Int +} + +case class SourceLocation1(val outer: SourceLocation, val fileName: String, val line: Int, val charOffset: Int) extends SourceLocation diff --git a/test/files/run/macro-sip19-revised/Test_2.scala b/test/files/run/macro-sip19-revised/Test_2.scala new file mode 100644 index 0000000000..d9a4d7d4fc --- /dev/null +++ b/test/files/run/macro-sip19-revised/Test_2.scala @@ -0,0 +1,12 @@ +import Macros._ + +object Test extends App { + def foo(x: Int, y: Int)(implicit loc: SourceLocation): Int = { + println("hey, i've been called from %s".format(loc)) + if (x < y) foo(y, x) + else if (y == 0) x + else foo(x - y, y) + } + + println(foo(4, 2)) +} -- cgit v1.2.3