1
- // Copy of tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala
2
- // FIXME remove this copy of the file
3
-
4
1
package scala .tasty .inspector
5
2
6
3
import scala .quoted ._
@@ -13,43 +10,45 @@ import dotty.tools.dotc.core.Contexts.Context
13
10
import dotty .tools .dotc .core .Mode
14
11
import dotty .tools .dotc .core .Phases .Phase
15
12
import dotty .tools .dotc .fromtasty ._
16
- import dotty .tools .dotc .quoted .QuotesCache
17
13
import dotty .tools .dotc .util .ClasspathFromClassloader
18
14
import dotty .tools .dotc .CompilationUnit
19
15
import dotty .tools .unsupported
20
16
import dotty .tools .dotc .report
21
17
22
18
import java .io .File .pathSeparator
23
19
24
- object TastyInspector :
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 = ()
25
30
26
31
/** Load and process TASTy files using TASTy reflect
27
32
*
28
33
* @param tastyFiles List of paths of `.tasty` files
29
- *
30
- * @return boolean value indicating whether the process succeeded
31
34
*/
32
- def inspectTastyFiles (tastyFiles : List [String ])( inspector : Inspector ) : Boolean =
33
- inspectAllTastyFiles(tastyFiles, Nil , Nil )(inspector)
35
+ def inspectTastyFiles (tastyFiles : List [String ]): Boolean =
36
+ inspectAllTastyFiles(tastyFiles, Nil , Nil )
34
37
35
38
/** Load and process TASTy files in a `jar` file using TASTy reflect
36
39
*
37
40
* @param jars Path of `.jar` file
38
- *
39
- * @return boolean value indicating whether the process succeeded
40
41
*/
41
- def inspectTastyFilesInJar (jar : String )( inspector : Inspector ) : Boolean =
42
- inspectAllTastyFiles(Nil , List (jar), Nil )(inspector)
42
+ def inspectTastyFilesInJar (jar : String ): Boolean =
43
+ inspectAllTastyFiles(Nil , List (jar), Nil )
43
44
44
45
/** Load and process TASTy files using TASTy reflect
45
46
*
46
47
* @param tastyFiles List of paths of `.tasty` files
47
48
* @param jars List of path of `.jar` files
48
49
* @param dependenciesClasspath Classpath with extra dependencies needed to load class in the `.tasty` files
49
- *
50
- * @return boolean value indicating whether the process succeeded
51
50
*/
52
- def inspectAllTastyFiles (tastyFiles : List [String ], jars : List [String ], dependenciesClasspath : List [String ])( inspector : Inspector ) : Boolean =
51
+ def inspectAllTastyFiles (tastyFiles : List [String ], jars : List [String ], dependenciesClasspath : List [String ]): Boolean =
53
52
def checkFile (fileName : String , ext : String ): Unit =
54
53
val file = dotty.tools.io.Path (fileName)
55
54
if file.extension != ext then
@@ -59,30 +58,40 @@ object TastyInspector:
59
58
tastyFiles.foreach(checkFile(_, " tasty" ))
60
59
jars.foreach(checkFile(_, " jar" ))
61
60
val files = tastyFiles ::: jars
62
- inspectFiles(dependenciesClasspath, files)(inspector)
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
+
63
74
64
- private def inspectorDriver (inspector : Inspector ) =
75
+ private def inspectorDriver () =
65
76
class InspectorDriver extends Driver :
66
77
override protected def newCompiler (implicit ctx : Context ): Compiler = new TastyFromClass
67
78
68
79
class TastyInspectorPhase extends Phase :
69
80
override def phaseName : String = " tastyInspector"
70
81
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)
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)
82
92
units
83
93
84
94
override def run (implicit ctx : Context ): Unit = unsupported(" run" )
85
- end TastyInspectorPhase
86
95
87
96
class TastyFromClass extends TASTYCompiler :
88
97
@@ -96,6 +105,7 @@ object TastyInspector:
96
105
97
106
override protected def backendPhases : List [List [Phase ]] =
98
107
List (new TastyInspectorPhase ) :: // Perform a callback for each compilation unit
108
+ List (new TastyInspectorFinishPhase ) :: // Perform a final callback
99
109
Nil
100
110
101
111
override def newRun (implicit ctx : Context ): Run =
@@ -113,14 +123,14 @@ object TastyInspector:
113
123
(" -from-tasty" :: " -Yretain-trees" :: " -classpath" :: fullClasspath :: classes).toArray
114
124
115
125
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
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
122
132
123
133
end inspectFiles
124
134
125
135
126
- end TastyInspector
136
+ end OldTastyInspector
0 commit comments