Skip to content

Commit 3e1c1d1

Browse files
committed
Revert "Remove OldTastyInspector from scaladoc"
This reverts commit 931e9f2.
1 parent fc27369 commit 3e1c1d1

File tree

5 files changed

+186
-41
lines changed

5 files changed

+186
-41
lines changed

scaladoc/src/dotty/tools/scaladoc/ScalaModuleProvider.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ case class Module(rootPackage: Member, members: Map[DRI, Member])
88

99
object ScalaModuleProvider:
1010
def mkModule()(using ctx: DocContext): Module =
11-
val (result, rootDoc) = ScaladocTastyInspector.loadDocs()
11+
val (result, rootDoc) = ScaladocTastyInspector().result()
1212
val (rootPck, rest) = result.partition(_.name == "API")
1313
val (emptyPackages, nonemptyPackages) = (rest ++ rootPck.flatMap(_.members))
1414
.filter(p => p.members.nonEmpty || p.docs.nonEmpty).sortBy(_.name)

scaladoc/src/dotty/tools/scaladoc/tasty/TastyParser.scala

+31-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package tasty
55
import java.util.regex.Pattern
66

77
import scala.util.{Try, Success, Failure}
8-
import scala.tasty.inspector.{TastyInspector, Inspector, Tasty}
8+
import scala.tasty.inspector.DocTastyInspector
99
import scala.quoted._
1010

1111
import dotty.tools.dotc
@@ -24,12 +24,24 @@ import ScaladocSupport._
2424
*
2525
* Delegates most of the work to [[TastyParser]] [[dotty.tools.scaladoc.tasty.TastyParser]].
2626
*/
27-
case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
27+
case class ScaladocTastyInspector()(using ctx: DocContext) extends DocTastyInspector:
2828

2929
private val topLevels = Seq.newBuilder[(String, Member)]
3030
private var rootDoc: Option[Comment] = None
3131

32-
def inspect(using Quotes)(tastys: List[scala.tasty.inspector.Tasty[quotes.type]]): Unit =
32+
def processCompilationUnit(using Quotes)(root: reflect.Tree): Unit = ()
33+
34+
override def postProcess(using Quotes): Unit =
35+
// hack into the compiler to get a list of all top-level trees
36+
// in principle, to do this, one would collect trees in processCompilationUnit
37+
// however, path-dependent types disallow doing so w/o using casts
38+
inline def hackForeachTree(thunk: reflect.Tree => Unit): Unit =
39+
given dctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
40+
dctx.run.nn.units.foreach { compilationUnit =>
41+
// mirrors code from TastyInspector
42+
thunk(compilationUnit.tpdTree.asInstanceOf[reflect.Tree])
43+
}
44+
3345
val symbolsToSkip: Set[reflect.Symbol] =
3446
ctx.args.identifiersToSkip.flatMap { ref =>
3547
val qrSymbol = reflect.Symbol
@@ -104,8 +116,7 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
104116
rootDoc = Some(parseCommentString(using parser.qctx, summon[DocContext])(content, topLevelPck, None))
105117
}
106118

107-
for tasty <- tastys do {
108-
val root = tasty.ast
119+
hackForeachTree { root =>
109120
if !isSkipped(root.symbol) then
110121
val treeRoot = root.asInstanceOf[parser.qctx.reflect.Tree]
111122
processRootDocIfNeeded(treeRoot)
@@ -127,47 +138,33 @@ case class ScaladocTastyInspector()(using ctx: DocContext) extends Inspector:
127138
topLevels += "scala" -> Member(scalaPckg.fullName, "", scalaPckg.dri, Kind.Package)
128139
topLevels += mergeAnyRefAliasAndObject(parser)
129140

130-
131-
132-
def mergeAnyRefAliasAndObject(parser: TastyParser) =
133-
import parser.qctx.reflect._
134-
val javaLangObjectDef = defn.ObjectClass.tree.asInstanceOf[ClassDef]
135-
val objectMembers = parser.extractPatchedMembers(javaLangObjectDef)
136-
val aM = parser.parseTypeDef(defn.AnyRefClass.tree.asInstanceOf[TypeDef])
137-
"scala" -> aM.copy(
138-
kind = Kind.Class(Nil, Nil),
139-
members = objectMembers
140-
)
141-
142-
object ScaladocTastyInspector:
143-
144-
def loadDocs()(using ctx: DocContext): (List[Member], Option[Comment]) =
141+
def result(): (List[Member], Option[Comment]) =
142+
topLevels.clear()
143+
rootDoc = None
145144
val filePaths = ctx.args.tastyFiles.map(_.getAbsolutePath).toList
146145
val classpath = ctx.args.classpath.split(java.io.File.pathSeparator).toList
147146

148-
val inspector = new ScaladocTastyInspector
149-
150-
val (tastyPaths, nonTastyPaths) = filePaths.partition(_.endsWith(".tasty"))
151-
val (jarPaths, invalidPaths) = nonTastyPaths.partition(_.endsWith(".jar"))
152-
153-
for invalidPath <- invalidPaths do
154-
report.error("File extension is not `tasty` or `jar`: " + invalidPath)
155-
156-
if tastyPaths.nonEmpty then
157-
TastyInspector.inspectAllTastyFiles(tastyPaths, jarPaths, classpath)(inspector)
147+
if filePaths.nonEmpty then inspectFilesInContext(classpath, filePaths)
158148

159-
val all = inspector.topLevels.result()
149+
val all = topLevels.result()
160150
all.groupBy(_._1).map { case (pckName, members) =>
161151
val (pcks, rest) = members.map(_._2).partition(_.kind == Kind.Package)
162152
val basePck = pcks.reduce( (p1, p2) =>
163153
val withNewMembers = p1.withNewMembers(p2.members)
164154
if withNewMembers.docs.isEmpty then withNewMembers.withDocs(p2.docs) else withNewMembers
165155
)
166156
basePck.withMembers((basePck.members ++ rest).sortBy(_.name))
167-
}.toList -> inspector.rootDoc
168-
169-
end ScaladocTastyInspector
157+
}.toList -> rootDoc
170158

159+
def mergeAnyRefAliasAndObject(parser: TastyParser) =
160+
import parser.qctx.reflect._
161+
val javaLangObjectDef = defn.ObjectClass.tree.asInstanceOf[ClassDef]
162+
val objectMembers = parser.extractPatchedMembers(javaLangObjectDef)
163+
val aM = parser.parseTypeDef(defn.AnyRefClass.tree.asInstanceOf[TypeDef])
164+
"scala" -> aM.copy(
165+
kind = Kind.Class(Nil, Nil),
166+
members = objectMembers
167+
)
171168
/** Parses a single Tasty compilation unit. */
172169
case class TastyParser(
173170
qctx: Quotes,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scala.tasty.inspector
2+
3+
import dotty.tools.dotc.core.Contexts.Context
4+
5+
abstract class DocTastyInspector extends OldTastyInspector:
6+
def inspectFilesInDocContext(
7+
classpath: List[String],
8+
filePaths: List[String])(
9+
using Context): Unit = inspectFilesInContext(classpath, filePaths)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

scaladoc/test/dotty/tools/scaladoc/tasty/comments/MemberLookupTests.scala

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.scaladoc
22
package tasty.comments
33

4-
import scala.quoted.*
4+
import scala.quoted.Quotes
55

66
import org.junit.{Test, Rule}
77
import org.junit.Assert.{assertSame, assertTrue}
@@ -198,11 +198,14 @@ class MemberLookupTests {
198198

199199
@Test
200200
def test(): Unit = {
201-
import scala.tasty.inspector.*
202-
class MyInspector extends Inspector:
201+
import scala.tasty.inspector.OldTastyInspector
202+
class Inspector extends OldTastyInspector:
203+
var alreadyRan: Boolean = false
203204

204-
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit =
205-
this.test()
205+
override def processCompilationUnit(using ctx: quoted.Quotes)(root: ctx.reflect.Tree): Unit =
206+
if !alreadyRan then
207+
this.test()
208+
alreadyRan = true
206209

207210
def test()(using q: Quotes): Unit = {
208211
import dotty.tools.scaladoc.tasty.comments.MemberLookup
@@ -212,6 +215,6 @@ class MemberLookupTests {
212215
cases.testAll()
213216
}
214217

215-
TastyInspector.inspectTastyFiles(TestUtils.listOurClasses())(new MyInspector)
218+
Inspector().inspectTastyFiles(TestUtils.listOurClasses())
216219
}
217220
}

0 commit comments

Comments
 (0)