|
| 1 | +package scala.tasty.inspector |
| 2 | + |
| 3 | +import scala.quoted._ |
| 4 | +import scala.quoted.runtime.impl.QuotesImpl |
| 5 | + |
| 6 | +import dotty.tools.dotc.Compiler |
| 7 | +import dotty.tools.dotc.Driver |
| 8 | +import dotty.tools.dotc.Run |
| 9 | +import dotty.tools.dotc.core.Contexts.Context |
| 10 | +import dotty.tools.dotc.core.Mode |
| 11 | +import dotty.tools.dotc.core.Phases.Phase |
| 12 | +import dotty.tools.dotc.fromtasty._ |
| 13 | +import dotty.tools.dotc.util.ClasspathFromClassloader |
| 14 | +import dotty.tools.dotc.CompilationUnit |
| 15 | +import dotty.tools.unsupported |
| 16 | +import dotty.tools.dotc.report |
| 17 | + |
| 18 | +import java.io.File.pathSeparator |
| 19 | + |
| 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 = () |
| 30 | + |
| 31 | + /** Load and process TASTy files using TASTy reflect |
| 32 | + * |
| 33 | + * @param tastyFiles List of paths of `.tasty` files |
| 34 | + */ |
| 35 | + def inspectTastyFiles(tastyFiles: List[String]): Boolean = |
| 36 | + inspectAllTastyFiles(tastyFiles, Nil, Nil) |
| 37 | + |
| 38 | + /** Load and process TASTy files in a `jar` file using TASTy reflect |
| 39 | + * |
| 40 | + * @param jars Path of `.jar` file |
| 41 | + */ |
| 42 | + def inspectTastyFilesInJar(jar: String): Boolean = |
| 43 | + inspectAllTastyFiles(Nil, List(jar), Nil) |
| 44 | + |
| 45 | + /** Load and process TASTy files using TASTy reflect |
| 46 | + * |
| 47 | + * @param tastyFiles List of paths of `.tasty` files |
| 48 | + * @param jars List of path of `.jar` files |
| 49 | + * @param dependenciesClasspath Classpath with extra dependencies needed to load class in the `.tasty` files |
| 50 | + */ |
| 51 | + def inspectAllTastyFiles(tastyFiles: List[String], jars: List[String], dependenciesClasspath: List[String]): Boolean = |
| 52 | + def checkFile(fileName: String, ext: String): Unit = |
| 53 | + val file = dotty.tools.io.Path(fileName) |
| 54 | + if file.extension != ext then |
| 55 | + throw new IllegalArgumentException(s"File extension is not `.$ext`: $file") |
| 56 | + else if !file.exists then |
| 57 | + throw new IllegalArgumentException(s"File not found: ${file.toAbsolute}") |
| 58 | + tastyFiles.foreach(checkFile(_, "tasty")) |
| 59 | + jars.foreach(checkFile(_, "jar")) |
| 60 | + 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 | + |
| 74 | + |
| 75 | + private def inspectorDriver() = |
| 76 | + class InspectorDriver extends Driver: |
| 77 | + override protected def newCompiler(implicit ctx: Context): Compiler = new TastyFromClass |
| 78 | + |
| 79 | + class TastyInspectorPhase extends Phase: |
| 80 | + override def phaseName: String = "tastyInspector" |
| 81 | + |
| 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) |
| 92 | + units |
| 93 | + |
| 94 | + override def run(implicit ctx: Context): Unit = unsupported("run") |
| 95 | + |
| 96 | + class TastyFromClass extends TASTYCompiler: |
| 97 | + |
| 98 | + override protected def frontendPhases: List[List[Phase]] = |
| 99 | + List(new ReadTasty) :: // Load classes from tasty |
| 100 | + Nil |
| 101 | + |
| 102 | + override protected def picklerPhases: List[List[Phase]] = Nil |
| 103 | + |
| 104 | + override protected def transformPhases: List[List[Phase]] = Nil |
| 105 | + |
| 106 | + override protected def backendPhases: List[List[Phase]] = |
| 107 | + List(new TastyInspectorPhase) :: // Perform a callback for each compilation unit |
| 108 | + List(new TastyInspectorFinishPhase) :: // Perform a final callback |
| 109 | + Nil |
| 110 | + |
| 111 | + override def newRun(implicit ctx: Context): Run = |
| 112 | + reset() |
| 113 | + val ctx2 = ctx.fresh |
| 114 | + .addMode(Mode.ReadPositions) |
| 115 | + .setSetting(ctx.settings.YreadComments, true) |
| 116 | + new TASTYRun(this, ctx2) |
| 117 | + |
| 118 | + new InspectorDriver |
| 119 | + |
| 120 | + private def inspectorArgs(classpath: List[String], classes: List[String]): Array[String] = |
| 121 | + val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader) |
| 122 | + val fullClasspath = (classpath :+ currentClasspath).mkString(pathSeparator) |
| 123 | + ("-from-tasty" :: "-Yretain-trees" :: "-classpath" :: fullClasspath :: classes).toArray |
| 124 | + |
| 125 | + |
| 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 |
| 132 | + |
| 133 | + end inspectFiles |
| 134 | + |
| 135 | + |
| 136 | +end OldTastyInspector |
0 commit comments