1+ // Copy of tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala
2+ // FIXME remove this copy of the file
3+
14package scala .tasty .inspector
25
36import scala .quoted ._
@@ -10,45 +13,43 @@ import dotty.tools.dotc.core.Contexts.Context
1013import dotty .tools .dotc .core .Mode
1114import dotty .tools .dotc .core .Phases .Phase
1215import dotty .tools .dotc .fromtasty ._
16+ import dotty .tools .dotc .quoted .QuotesCache
1317import dotty .tools .dotc .util .ClasspathFromClassloader
1418import dotty .tools .dotc .CompilationUnit
1519import dotty .tools .unsupported
1620import dotty .tools .dotc .report
1721
1822import java .io .File .pathSeparator
1923
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 :
3025
3126 /** Load and process TASTy files using TASTy reflect
3227 *
3328 * @param tastyFiles List of paths of `.tasty` files
29+ *
30+ * @return boolean value indicating whether the process succeeded
3431 */
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)
3734
3835 /** Load and process TASTy files in a `jar` file using TASTy reflect
3936 *
4037 * @param jars Path of `.jar` file
38+ *
39+ * @return boolean value indicating whether the process succeeded
4140 */
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)
4443
4544 /** Load and process TASTy files using TASTy reflect
4645 *
4746 * @param tastyFiles List of paths of `.tasty` files
4847 * @param jars List of path of `.jar` files
4948 * @param dependenciesClasspath Classpath with extra dependencies needed to load class in the `.tasty` files
49+ *
50+ * @return boolean value indicating whether the process succeeded
5051 */
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 =
5253 def checkFile (fileName : String , ext : String ): Unit =
5354 val file = dotty.tools.io.Path (fileName)
5455 if file.extension != ext then
@@ -58,40 +59,30 @@ trait OldTastyInspector:
5859 tastyFiles.foreach(checkFile(_, " tasty" ))
5960 jars.foreach(checkFile(_, " jar" ))
6061 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)
7463
75- private def inspectorDriver () =
64+ private def inspectorDriver (inspector : Inspector ) =
7665 class InspectorDriver extends Driver :
7766 override protected def newCompiler (implicit ctx : Context ): Compiler = new TastyFromClass
7867
7968 class TastyInspectorPhase extends Phase :
8069 override def phaseName : String = " tastyInspector"
8170
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)
9282 units
9383
9484 override def run (implicit ctx : Context ): Unit = unsupported(" run" )
85+ end TastyInspectorPhase
9586
9687 class TastyFromClass extends TASTYCompiler :
9788
@@ -105,7 +96,6 @@ trait OldTastyInspector:
10596
10697 override protected def backendPhases : List [List [Phase ]] =
10798 List (new TastyInspectorPhase ) :: // Perform a callback for each compilation unit
108- List (new TastyInspectorFinishPhase ) :: // Perform a final callback
10999 Nil
110100
111101 override def newRun (implicit ctx : Context ): Run =
@@ -123,14 +113,14 @@ trait OldTastyInspector:
123113 (" -from-tasty" :: " -Yretain-trees" :: " -classpath" :: fullClasspath :: classes).toArray
124114
125115
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
132122
133123 end inspectFiles
134124
135125
136- end OldTastyInspector
126+ end TastyInspector
0 commit comments