1
+ // Copy of tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala
2
+ // FIXME remove this copy of the file
3
+
1
4
package scala .tasty .inspector
2
5
3
6
import scala .quoted ._
@@ -10,45 +13,43 @@ import dotty.tools.dotc.core.Contexts.Context
10
13
import dotty .tools .dotc .core .Mode
11
14
import dotty .tools .dotc .core .Phases .Phase
12
15
import dotty .tools .dotc .fromtasty ._
16
+ import dotty .tools .dotc .quoted .QuotesCache
13
17
import dotty .tools .dotc .util .ClasspathFromClassloader
14
18
import dotty .tools .dotc .CompilationUnit
15
19
import dotty .tools .unsupported
16
20
import dotty .tools .dotc .report
17
21
18
22
import java .io .File .pathSeparator
19
23
20
- // COPY OF OLD IMPLEMENTATION
21
- // TODO: update to new implementation
22
- trait OldTastyInspector :
23
- self =>
24
-
25
- /** Process a TASTy file using TASTy reflect */
26
- protected def processCompilationUnit (using Quotes )(root : quotes.reflect.Tree ): Unit
27
-
28
- /** Called after all compilation units are processed */
29
- protected def postProcess (using Quotes ): Unit = ()
24
+ object TastyInspector :
30
25
31
26
/** Load and process TASTy files using TASTy reflect
32
27
*
33
28
* @param tastyFiles List of paths of `.tasty` files
29
+ *
30
+ * @return boolean value indicating whether the process succeeded
34
31
*/
35
- def inspectTastyFiles (tastyFiles : List [String ]): Boolean =
36
- inspectAllTastyFiles(tastyFiles, Nil , Nil )
32
+ def inspectTastyFiles (tastyFiles : List [String ])( inspector : Inspector ) : Boolean =
33
+ inspectAllTastyFiles(tastyFiles, Nil , Nil )(inspector)
37
34
38
35
/** Load and process TASTy files in a `jar` file using TASTy reflect
39
36
*
40
37
* @param jars Path of `.jar` file
38
+ *
39
+ * @return boolean value indicating whether the process succeeded
41
40
*/
42
- def inspectTastyFilesInJar (jar : String ): Boolean =
43
- inspectAllTastyFiles(Nil , List (jar), Nil )
41
+ def inspectTastyFilesInJar (jar : String )( inspector : Inspector ) : Boolean =
42
+ inspectAllTastyFiles(Nil , List (jar), Nil )(inspector)
44
43
45
44
/** Load and process TASTy files using TASTy reflect
46
45
*
47
46
* @param tastyFiles List of paths of `.tasty` files
48
47
* @param jars List of path of `.jar` files
49
48
* @param dependenciesClasspath Classpath with extra dependencies needed to load class in the `.tasty` files
49
+ *
50
+ * @return boolean value indicating whether the process succeeded
50
51
*/
51
- def inspectAllTastyFiles (tastyFiles : List [String ], jars : List [String ], dependenciesClasspath : List [String ]): Boolean =
52
+ def inspectAllTastyFiles (tastyFiles : List [String ], jars : List [String ], dependenciesClasspath : List [String ])( inspector : Inspector ) : Boolean =
52
53
def checkFile (fileName : String , ext : String ): Unit =
53
54
val file = dotty.tools.io.Path (fileName)
54
55
if file.extension != ext then
@@ -58,40 +59,30 @@ trait OldTastyInspector:
58
59
tastyFiles.foreach(checkFile(_, " tasty" ))
59
60
jars.foreach(checkFile(_, " jar" ))
60
61
val files = tastyFiles ::: jars
61
- files.nonEmpty && inspectFiles(dependenciesClasspath, files)
62
-
63
- /** Load and process TASTy files using TASTy reflect and provided context
64
- *
65
- * Used in doctool to reuse reporter and setup provided by sbt
66
- *
67
- * @param classes List of paths of `.tasty` and `.jar` files (no validation is performed)
68
- * @param classpath Classpath with extra dependencies needed to load class in the `.tasty` files
69
- */
70
- protected [inspector] def inspectFilesInContext (classpath : List [String ], classes : List [String ])(using Context ): Unit =
71
- if (classes.isEmpty) report.error(" Parameter classes should no be empty" )
72
- inspectorDriver().process(inspectorArgs(classpath, classes), summon[Context ])
73
-
62
+ inspectFiles(dependenciesClasspath, files)(inspector)
74
63
75
- private def inspectorDriver () =
64
+ private def inspectorDriver (inspector : Inspector ) =
76
65
class InspectorDriver extends Driver :
77
66
override protected def newCompiler (implicit ctx : Context ): Compiler = new TastyFromClass
78
67
79
68
class TastyInspectorPhase extends Phase :
80
69
override def phaseName : String = " tastyInspector"
81
70
82
- override def run (implicit ctx : Context ): Unit =
83
- val qctx = QuotesImpl ()
84
- self.processCompilationUnit(using qctx)(ctx.compilationUnit.tpdTree.asInstanceOf [qctx.reflect.Tree ])
85
-
86
- class TastyInspectorFinishPhase extends Phase :
87
- override def phaseName : String = " tastyInspectorFinish"
88
-
89
- override def runOn (units : List [CompilationUnit ])(using Context ): List [CompilationUnit ] =
90
- val qctx = QuotesImpl ()
91
- self.postProcess(using qctx)
71
+ override def runOn (units : List [CompilationUnit ])(using ctx0 : Context ): List [CompilationUnit ] =
72
+ val ctx = QuotesCache .init(ctx0.fresh)
73
+ runOnImpl(units)(using ctx)
74
+
75
+ private def runOnImpl (units : List [CompilationUnit ])(using Context ): List [CompilationUnit ] =
76
+ val quotesImpl = QuotesImpl ()
77
+ class TastyImpl (val path : String , val ast : quotesImpl.reflect.Tree ) extends Tasty [quotesImpl.type ] {
78
+ val quotes = quotesImpl
79
+ }
80
+ val tastys = units.map(unit => new TastyImpl (unit.source.path , unit.tpdTree.asInstanceOf [quotesImpl.reflect.Tree ]))
81
+ inspector.inspect(using quotesImpl)(tastys)
92
82
units
93
83
94
84
override def run (implicit ctx : Context ): Unit = unsupported(" run" )
85
+ end TastyInspectorPhase
95
86
96
87
class TastyFromClass extends TASTYCompiler :
97
88
@@ -105,7 +96,6 @@ trait OldTastyInspector:
105
96
106
97
override protected def backendPhases : List [List [Phase ]] =
107
98
List (new TastyInspectorPhase ) :: // Perform a callback for each compilation unit
108
- List (new TastyInspectorFinishPhase ) :: // Perform a final callback
109
99
Nil
110
100
111
101
override def newRun (implicit ctx : Context ): Run =
@@ -123,14 +113,14 @@ trait OldTastyInspector:
123
113
(" -from-tasty" :: " -Yretain-trees" :: " -classpath" :: fullClasspath :: classes).toArray
124
114
125
115
126
- private def inspectFiles (classpath : List [String ], classes : List [String ]): Boolean =
127
- if ( classes.isEmpty)
128
- throw new IllegalArgumentException ( " Parameter classes should no be empty " )
129
-
130
- val reporter = inspectorDriver().process(inspectorArgs(classpath, classes))
131
- reporter.hasErrors
116
+ private def inspectFiles (classpath : List [String ], classes : List [String ])( inspector : Inspector ) : Boolean =
117
+ classes match
118
+ case Nil => true
119
+ case _ =>
120
+ val reporter = inspectorDriver(inspector ).process(inspectorArgs(classpath, classes))
121
+ ! reporter.hasErrors
132
122
133
123
end inspectFiles
134
124
135
125
136
- end OldTastyInspector
126
+ end TastyInspector
0 commit comments