@@ -3,13 +3,16 @@ package tastyreflect
33
44import dotty .tools .dotc .ast .{Trees , tpd , untpd }
55import dotty .tools .dotc .ast .tpd .TreeOps
6+ import dotty .tools .dotc .typer .Typer
67import dotty .tools .dotc .core ._
78import dotty .tools .dotc .core .Flags ._
89import dotty .tools .dotc .core .StdNames .nme
910import dotty .tools .dotc .core .quoted .PickledQuotes
1011import dotty .tools .dotc .core .Symbols ._
1112import dotty .tools .dotc .core .Decorators ._
1213import dotty .tools .dotc .tastyreflect .FromSymbol .{definitionFromSym , packageDefFromSym }
14+ import dotty .tools .dotc .parsing .Parsers .Parser
15+ import dotty .tools .dotc .util .SourceFile
1316
1417import scala .tasty .reflect .Kernel
1518
@@ -36,9 +39,15 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
3639 def error (msg : => String , pos : Position )(implicit ctx : Context ): Unit =
3740 ctx.error(msg, pos)
3841
42+ def error (msg : => String , sourceFile : SourceFile , start : Int , end : Int )(implicit ctx : Context ): Unit =
43+ ctx.error(msg, util.SourcePosition (sourceFile, util.Spans .Span (start, end)))
44+
3945 def warning (msg : => String , pos : Position )(implicit ctx : Context ): Unit =
4046 ctx.warning(msg, pos)
4147
48+ def warning (msg : => String , sourceFile : SourceFile , start : Int , end : Int )(implicit ctx : Context ): Unit =
49+ ctx.error(msg, util.SourcePosition (sourceFile, util.Spans .Span (start, end)))
50+
4251 //
4352 // Settings
4453 //
@@ -47,6 +56,26 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
4756
4857 def Settings_color (self : Settings ): Boolean = self.color.value(rootContext) == " always"
4958
59+ //
60+ // MISC
61+ //
62+ /** Whether the code type checks in the given context?
63+ *
64+ * @param code The code to be type checked
65+ *
66+ * The code should be a sequence of expressions or statements that may appear in a block.
67+ */
68+ def typeChecks (code : String )(implicit ctx : Context ): Boolean = {
69+ val ctx2 = ctx.fresh.setNewTyperState().setTyper(new Typer )
70+ val tree = new Parser (SourceFile .virtual(" tasty-reflect" , code))(ctx2).block()
71+
72+ if (ctx2.reporter.hasErrors) false
73+ else {
74+ ctx2.typer.typed(tree)(ctx2)
75+ ! ctx2.reporter.hasErrors
76+ }
77+ }
78+
5079 //
5180 // TREES
5281 //
@@ -493,7 +522,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
493522 type Match = tpd.Match
494523
495524 def matchMatch (x : Term )(implicit ctx : Context ): Option [Match ] = x match {
496- case x : tpd.Match => Some (x)
525+ case x : tpd.Match if ! x.selector.isEmpty => Some (x)
497526 case _ => None
498527 }
499528
@@ -506,6 +535,21 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
506535 def Match_copy (original : Tree )(selector : Term , cases : List [CaseDef ])(implicit ctx : Context ): Match =
507536 tpd.cpy.Match (original)(selector, cases)
508537
538+ type ImplicitMatch = tpd.Match
539+
540+ def matchImplicitMatch (x : Term )(implicit ctx : Context ): Option [Match ] = x match {
541+ case x : tpd.Match if x.selector.isEmpty => Some (x)
542+ case _ => None
543+ }
544+
545+ def ImplicitMatch_cases (self : Match )(implicit ctx : Context ): List [CaseDef ] = self.cases
546+
547+ def ImplicitMatch_apply (cases : List [CaseDef ])(implicit ctx : Context ): ImplicitMatch =
548+ withDefaultPos(ctx => tpd.Match (tpd.EmptyTree , cases)(ctx))
549+
550+ def ImplicitMatch_copy (original : Tree )(cases : List [CaseDef ])(implicit ctx : Context ): ImplicitMatch =
551+ tpd.cpy.Match (original)(tpd.EmptyTree , cases)
552+
509553 type Try = tpd.Try
510554
511555 def matchTry (x : Term )(implicit ctx : Context ): Option [Try ] = x match {
@@ -1297,7 +1341,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
12971341
12981342 def Position_exists (self : Position ): Boolean = self.exists
12991343
1300- def Position_sourceFile (self : Position ): java.nio.file. Path = self.source.file.jpath
1344+ def Position_sourceFile (self : Position ): SourceFile = self.source
13011345
13021346 def Position_startLine (self : Position ): Int = self.startLine
13031347
@@ -1310,6 +1354,16 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
13101354 def Position_sourceCode (self : Position ): String =
13111355 new String (self.source.content(), self.start, self.end - self.start)
13121356
1357+ //
1358+ // SOURCE FILES
1359+ //
1360+
1361+ type SourceFile = util.SourceFile
1362+
1363+ def SourceFile_jpath (self : SourceFile ): java.nio.file.Path = self.file.jpath
1364+
1365+ def SourceFile_content (self : SourceFile ): String = new String (self.content())
1366+
13131367 //
13141368 // COMMENTS
13151369 //
0 commit comments