From 837d38fa2026eb0d925c4439aa24478c4b810232 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 8 Jun 2018 08:28:46 +0200 Subject: [PATCH] Add Tasty compilation unit source file --- .../dotty/tools/dotc/tastyreflect/TastyImpl.scala | 2 ++ library/src/scala/tasty/Tasty.scala | 3 +++ tests/run/tasty-getfile.check | 1 + tests/run/tasty-getfile/App_2.scala | 6 ++++++ tests/run/tasty-getfile/Macro_1.scala | 15 +++++++++++++++ 5 files changed, 27 insertions(+) create mode 100644 tests/run/tasty-getfile.check create mode 100644 tests/run/tasty-getfile/App_2.scala create mode 100644 tests/run/tasty-getfile/Macro_1.scala diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala b/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala index 7d33317a9eef..39fba0d81f88 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala @@ -41,6 +41,8 @@ object TastyImpl extends scala.tasty.Tasty { def ContextDeco(ctx: Context): ContextAPI = new ContextAPI { def owner: Definition = FromSymbol.definition(ctx.owner)(ctx) + + def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath } // ===== Id ======================================================= diff --git a/library/src/scala/tasty/Tasty.scala b/library/src/scala/tasty/Tasty.scala index e66f4fe76fc4..ee80e5093d3d 100644 --- a/library/src/scala/tasty/Tasty.scala +++ b/library/src/scala/tasty/Tasty.scala @@ -31,6 +31,9 @@ abstract class Tasty { tasty => trait ContextAPI { def owner: Definition + + /** Returns the source file being compiled. The path is relative to the current working directory. */ + def source: java.nio.file.Path } implicit def ContextDeco(ctx: Context): ContextAPI diff --git a/tests/run/tasty-getfile.check b/tests/run/tasty-getfile.check new file mode 100644 index 000000000000..f414a7f6b11c --- /dev/null +++ b/tests/run/tasty-getfile.check @@ -0,0 +1 @@ +App_2.scala diff --git a/tests/run/tasty-getfile/App_2.scala b/tests/run/tasty-getfile/App_2.scala new file mode 100644 index 000000000000..ca5531badfc7 --- /dev/null +++ b/tests/run/tasty-getfile/App_2.scala @@ -0,0 +1,6 @@ + +object Test { + def main(args: Array[String]): Unit = { + println(SourceFiles.getThisFile) + } +} diff --git a/tests/run/tasty-getfile/Macro_1.scala b/tests/run/tasty-getfile/Macro_1.scala new file mode 100644 index 000000000000..b6822bb3638f --- /dev/null +++ b/tests/run/tasty-getfile/Macro_1.scala @@ -0,0 +1,15 @@ +import scala.quoted._ + +import scala.tasty.Universe + +object SourceFiles { + + implicit inline def getThisFile: String = + ~getThisFileImpl(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~ + + private def getThisFileImpl(implicit u: Universe): Expr[String] = { + import u.tasty._ + u.context.source.getFileName.toString.toExpr + } + +}