@@ -3,13 +3,16 @@ package tastyreflect
3
3
4
4
import dotty .tools .dotc .ast .{Trees , tpd , untpd }
5
5
import dotty .tools .dotc .ast .tpd .TreeOps
6
+ import dotty .tools .dotc .typer .Typer
6
7
import dotty .tools .dotc .core ._
7
8
import dotty .tools .dotc .core .Flags ._
8
9
import dotty .tools .dotc .core .StdNames .nme
9
10
import dotty .tools .dotc .core .quoted .PickledQuotes
10
11
import dotty .tools .dotc .core .Symbols ._
11
12
import dotty .tools .dotc .core .Decorators ._
12
13
import dotty .tools .dotc .tastyreflect .FromSymbol .{definitionFromSym , packageDefFromSym }
14
+ import dotty .tools .dotc .parsing .Parsers .Parser
15
+ import dotty .tools .dotc .util .SourceFile
13
16
14
17
import scala .tasty .reflect .Kernel
15
18
@@ -36,9 +39,15 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
36
39
def error (msg : => String , pos : Position )(implicit ctx : Context ): Unit =
37
40
ctx.error(msg, pos)
38
41
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
+
39
45
def warning (msg : => String , pos : Position )(implicit ctx : Context ): Unit =
40
46
ctx.warning(msg, pos)
41
47
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
+
42
51
//
43
52
// Settings
44
53
//
@@ -47,6 +56,26 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
47
56
48
57
def Settings_color (self : Settings ): Boolean = self.color.value(rootContext) == " always"
49
58
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
+
50
79
//
51
80
// TREES
52
81
//
@@ -493,7 +522,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
493
522
type Match = tpd.Match
494
523
495
524
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)
497
526
case _ => None
498
527
}
499
528
@@ -506,6 +535,21 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
506
535
def Match_copy (original : Tree )(selector : Term , cases : List [CaseDef ])(implicit ctx : Context ): Match =
507
536
tpd.cpy.Match (original)(selector, cases)
508
537
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
+
509
553
type Try = tpd.Try
510
554
511
555
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.
1297
1341
1298
1342
def Position_exists (self : Position ): Boolean = self.exists
1299
1343
1300
- def Position_sourceFile (self : Position ): java.nio.file. Path = self.source.file.jpath
1344
+ def Position_sourceFile (self : Position ): SourceFile = self.source
1301
1345
1302
1346
def Position_startLine (self : Position ): Int = self.startLine
1303
1347
@@ -1310,6 +1354,16 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
1310
1354
def Position_sourceCode (self : Position ): String =
1311
1355
new String (self.source.content(), self.start, self.end - self.start)
1312
1356
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
+
1313
1367
//
1314
1368
// COMMENTS
1315
1369
//
0 commit comments