Skip to content

Commit 2418725

Browse files
Merge pull request #9646 from dotty-staging/use-quote-context-for-in-tasty-inspector
Use quote context for in tasty inspector
2 parents 75c8176 + 4568ca2 commit 2418725

File tree

14 files changed

+161
-168
lines changed

14 files changed

+161
-168
lines changed

tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package scala.tasty.inspector
22

3-
import scala.tasty.Reflection
3+
import scala.quoted._
44

55
import dotty.tools.dotc.Compiler
66
import dotty.tools.dotc.Driver
@@ -18,7 +18,7 @@ trait TastyInspector:
1818
self =>
1919

2020
/** Process a TASTy file using TASTy reflect */
21-
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit
21+
protected def processCompilationUnit(using QuoteContext)(root: qctx.tasty.Tree): Unit
2222

2323
/** Load and process TASTy files using TASTy reflect
2424
*
@@ -58,7 +58,7 @@ trait TastyInspector:
5858

5959
override def run(implicit ctx: Context): Unit =
6060
val qctx = QuoteContextImpl()
61-
self.processCompilationUnit(qctx.tasty)(ctx.compilationUnit.tpdTree.asInstanceOf[qctx.tasty.Tree])
61+
self.processCompilationUnit(using qctx)(ctx.compilationUnit.tpdTree.asInstanceOf[qctx.tasty.Tree])
6262

6363
end TastyInspectorPhase
6464

tastydoc/src/dotty/tastydoc/TastyExtractor.scala

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package dotty.tastydoc
22

3-
import scala.tasty.Reflection
3+
import scala.quoted._
44
import dotty.tastydoc.comment.{CommentParser, CommentCleaner, Comment, WikiComment, MarkdownComment}
55
import dotty.tastydoc.references._
66
import dotty.tastydoc.representations._
77

88
/** A trait containing useful methods for extracting information from the reflect API */
99
trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentCleaner{
10-
def extractPath(reflect: Reflection)(symbol: reflect.Symbol) : List[String] = {
11-
import reflect.{given _, _}
10+
def extractPath(using QuoteContext)(symbol: qctx.tasty.Symbol) : List[String] = {
11+
import qctx.tasty._
1212

1313
val pathArray = symbol.show.split("\\.") // NOTE: this should print w/o colors, inspect afterwards
1414
pathArray.iterator.slice(0, pathArray.length - 1).toList
1515
}
1616

17-
def extractModifiers(reflect: Reflection)(flags: reflect.Flags, privateWithin: Option[reflect.Type], protectedWithin: Option[reflect.Type]) : (List[String], Option[Reference], Option[Reference]) = {
18-
import reflect.{given _, _}
17+
def extractModifiers(using QuoteContext)(flags: qctx.tasty.Flags, privateWithin: Option[qctx.tasty.Type], protectedWithin: Option[qctx.tasty.Type]) : (List[String], Option[Reference], Option[Reference]) = {
18+
import qctx.tasty._
1919

2020
(((if(flags.is(Flags.Override)) "override" else "") ::
2121
(if(flags.is(Flags.Private)) "private" else "")::
@@ -30,18 +30,16 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
3030
Nil) filter (_ != ""),
3131

3232
privateWithin match {
33-
case Some(t) => Some(convertTypeToReference(reflect)(t))
33+
case Some(t) => Some(convertTypeToReference(t))
3434
case None => None
3535
},
3636
protectedWithin match {
37-
case Some(t) => Some(convertTypeToReference(reflect)(t))
37+
case Some(t) => Some(convertTypeToReference(t))
3838
case None => None
3939
})
4040
}
4141

42-
def extractComments(reflect: Reflection)(comment: Option[reflect.Comment], rep: Representation) : (Map[String, EmulatedPackageRepresentation], String) => Option[Comment] = {
43-
import reflect.{given _, _}
44-
42+
def extractComments(using QuoteContext)(comment: Option[qctx.tasty.Comment], rep: Representation) : (Map[String, EmulatedPackageRepresentation], String) => Option[Comment] = {
4543
comment match {
4644
case Some(com) =>
4745
(packages, userDocSyntax) => {
@@ -58,13 +56,13 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
5856
}
5957
}
6058

61-
def extractClassMembers(reflect: Reflection)(body: List[reflect.Statement], symbol: reflect.Symbol, parentRepresentation: Some[Representation])(using mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) : List[Representation with Modifiers] = {
62-
import reflect.{given _, _}
59+
def extractClassMembers(using QuoteContext)(body: List[qctx.tasty.Statement], symbol: qctx.tasty.Symbol, parentRepresentation: Some[Representation])(using mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) : List[Representation with Modifiers] = {
60+
import qctx.tasty._
6361

6462
/** Filter fields which shouldn't be displayed in the doc
6563
*/
66-
def filterSymbol(symbol: reflect.Symbol): Boolean = {
67-
val ownerPath = extractPath(reflect)(symbol.owner)
64+
def filterSymbol(symbol: Symbol): Boolean = {
65+
val ownerPath = extractPath(symbol.owner)
6866

6967
!symbol.flags.is(Flags.Synthetic) &&
7068
!symbol.flags.is(Flags.Artifact) &&
@@ -79,16 +77,16 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
7977
case _: ValDef => None //No val/var, they are appended with symbol.fields below
8078
case _: Inlined => None //Inlined aren't desirable members
8179
case x => Some(x)
82-
}.filter(x => filterSymbol(x.symbol)).map(convertToRepresentation(reflect)(_, parentRepresentation)) ++
83-
symbol.methods.filter(x => filterSymbol(x)).map{x => convertToRepresentation(reflect)(x.tree, parentRepresentation)} ++
80+
}.filter(x => filterSymbol(x.symbol)).map(convertToRepresentation(_, parentRepresentation)) ++
81+
symbol.methods.filter(x => filterSymbol(x)).map{x => convertToRepresentation(x.tree, parentRepresentation)} ++
8482
symbol.fields.filter { x =>
8583
filterSymbol(x)
8684
}.flatMap {
8785
case x if x.isValDef => Some(x)
88-
// case reflect.IsValDefSymbol(x) => Some(x)
86+
// case qctx.tasty.IsValDefSymbol(x) => Some(x)
8987
case _ => None
9088
}.map { x =>
91-
convertToRepresentation(reflect)(x.tree, parentRepresentation)
89+
convertToRepresentation(x.tree, parentRepresentation)
9290
}
9391
)
9492
.flatMap{
@@ -98,12 +96,12 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
9896
.sortBy(_.name)
9997
}
10098

101-
def extractParents(reflect: Reflection)(parents: List[reflect.Tree]): List[Reference] = {
102-
import reflect.{given _, _}
99+
def extractParents(using QuoteContext)(parents: List[qctx.tasty.Tree]): List[Reference] = {
100+
import qctx.tasty._
103101

104102
val parentsReferences = parents.map{
105-
case c: TypeTree => convertTypeToReference(reflect)(c.tpe)
106-
case c: Term => convertTypeToReference(reflect)(c.tpe)
103+
case c: TypeTree => convertTypeToReference(c.tpe)
104+
case c: Term => convertTypeToReference(c.tpe)
107105
case _ => throw Exception("Unhandeld case in parents. Please open an issue.")
108106
}
109107

@@ -117,12 +115,12 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
117115
*
118116
* @return (is case, is a trait, is an object, the kind as a String)
119117
*/
120-
def extractKind(reflect: Reflection)(flags: reflect.Flags): (Boolean, Boolean, Boolean, String) = {
121-
import reflect.{given _, _}
118+
def extractKind(using QuoteContext)(flags: qctx.tasty.Flags): (Boolean, Boolean, Boolean, String) = {
119+
import qctx.tasty._
122120

123-
val isCase = flags.is(reflect.Flags.Case)
124-
val isTrait = flags.is(reflect.Flags.Trait)
125-
val isObject = flags.is(reflect.Flags.Object)
121+
val isCase = flags.is(Flags.Case)
122+
val isTrait = flags.is(Flags.Trait)
123+
val isObject = flags.is(Flags.Object)
126124
val kind = {
127125
if(isTrait){
128126
"trait"
@@ -142,38 +140,38 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
142140
(isCase, isTrait, isObject, kind)
143141
}
144142

145-
def extractCompanion(reflect: Reflection)(companionModule: Option[reflect.Symbol], companionClass: Option[reflect.Symbol], companionIsObject: Boolean): Option[CompanionReference] = {
146-
import reflect.{given _, _}
143+
def extractCompanion(using QuoteContext)(companionModule: Option[qctx.tasty.Symbol], companionClass: Option[qctx.tasty.Symbol], companionIsObject: Boolean): Option[CompanionReference] = {
144+
import qctx.tasty._
147145

148146
if(companionIsObject){
149147
companionModule match {
150148
case Some(c) =>
151-
val path = extractPath(reflect)(c)
152-
val (_, _, _, kind) = extractKind(reflect)(c.flags)
149+
val path = extractPath(c)
150+
val (_, _, _, kind) = extractKind(c.flags)
153151
Some(CompanionReference(c.name + "$", path.mkString("/", "/", ""), kind))
154152
case None => None
155153
}
156154
}else{
157155
companionClass match {
158156
case Some(c) =>
159-
val path = extractPath(reflect)(c)
160-
val (_, _, _, kind) = extractKind(reflect)(c.flags)
157+
val path = extractPath(c)
158+
val (_, _, _, kind) = extractKind(c.flags)
161159
Some(CompanionReference(c.name, path.mkString("/", "/", ""), kind))
162160
case None => None
163161
}
164162
}
165163
}
166164

167-
def extractAnnotations(reflect: Reflection)(annots: List[reflect.Term]): List[TypeReference] = {
168-
import reflect.{given _, _}
165+
def extractAnnotations(using QuoteContext)(annots: List[qctx.tasty.Term]): List[TypeReference] = {
166+
import qctx.tasty._
169167

170168
def keepAnnot(label: String, link: String): Boolean = {
171169
!(label == "SourceFile" && link == "/internal") &&
172170
!(label == "Child" && link == "/internal")
173171
}
174172

175173
annots.flatMap{a =>
176-
convertTypeToReference(reflect)(a.tpe) match {
174+
convertTypeToReference(a.tpe) match {
177175
case ref@TypeReference(label, link, _, _) if keepAnnot(label, link) => Some(ref)
178176
case _ => None
179177
}

tastydoc/src/dotty/tastydoc/TastyTypeConverter.scala

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package dotty.tastydoc
22

3-
import scala.tasty.Reflection
3+
import scala.quoted._
44
import dotty.tastydoc.references._
55

66
/** Trait containing methods for converting from Reflect types to References */
77
trait TastyTypeConverter {
88

9-
def convertTypeOrBoundsToReference(reflect: Reflection)(typeOrBounds: reflect.TypeOrBounds): Reference = {
10-
import reflect.{given _, _}
9+
def convertTypeOrBoundsToReference(using QuoteContext)(typeOrBounds: qctx.tasty.TypeOrBounds): Reference = {
10+
import qctx.tasty._
1111

1212
def anyOrNothing(reference: Reference): Boolean = reference match {
1313
case TypeReference("Any", "/scala", _, _) => true
@@ -16,24 +16,24 @@ trait TastyTypeConverter {
1616
}
1717

1818
typeOrBounds match {
19-
case tpe: Type => convertTypeToReference(reflect)(tpe)
19+
case tpe: Type => convertTypeToReference(tpe)
2020
case TypeBounds(low, hi) =>
21-
val lowRef = convertTypeToReference(reflect)(low)
22-
val hiRef = convertTypeToReference(reflect)(hi)
21+
val lowRef = convertTypeToReference(low)
22+
val hiRef = convertTypeToReference(hi)
2323
if(hiRef == lowRef){
2424
hiRef
2525
}else{
2626
BoundsReference(lowRef, hiRef)
2727
}
28-
case reflect.NoPrefix() => EmptyReference
28+
case NoPrefix() => EmptyReference
2929
}
3030
}
3131

32-
def convertTypeToReference(reflect: Reflection)(tp: reflect.Type): Reference = {
33-
import reflect.{given _, _}
32+
def convertTypeToReference(using QuoteContext)(tp: qctx.tasty.Type): Reference = {
33+
import qctx.tasty._
3434

3535
//Inner method to avoid passing the reflection each time
36-
def inner(tp: reflect.Type): Reference = tp match {
36+
def inner(tp: Type): Reference = tp match {
3737
case OrType(left, right) => OrTypeReference(inner(left), inner(right))
3838
case AndType(left, right) => AndTypeReference(inner(left), inner(right))
3939
case ByNameType(tpe) => ByNameReference(inner(tpe))
@@ -42,13 +42,13 @@ trait TastyTypeConverter {
4242
case AnnotatedType(tpe, _) => inner(tpe)
4343
case TypeLambda(paramNames, paramTypes, resType) => ConstantReference(tp.show) //TOFIX
4444
case Refinement(parent, name, info) =>
45-
val tuple = convertTypeOrBoundsToReference(reflect)(info) match {
45+
val tuple = convertTypeOrBoundsToReference(info) match {
4646
case r if (info match {case info: TypeBounds => true case _ => false}) => ("type", name, r)
4747
case r@TypeReference(_, _, _, _) => ("val", name, r)
4848
case ByNameReference(rChild) => ("def", name, rChild)
4949
case r => throw new Exception("Match error in info of Refinement. This should not happen, please open an issue. " + r)
5050
}
51-
convertTypeToReference(reflect)(parent) match {
51+
convertTypeToReference(parent) match {
5252
case RefinedReference(p, ls) =>
5353
RefinedReference(p, ls:+tuple)
5454
case t => RefinedReference(t, List(tuple))
@@ -58,51 +58,51 @@ trait TastyTypeConverter {
5858
case TypeReference(label, link, _, hasOwnFile) =>
5959
if(link == "/scala"){
6060
if(label.matches("Function[1-9]") || label.matches("Function[1-9][0-9]")){
61-
val argsAndReturn = typeOrBoundsList.map(convertTypeOrBoundsToReference(reflect)(_))
61+
val argsAndReturn = typeOrBoundsList.map(convertTypeOrBoundsToReference(_))
6262
FunctionReference(argsAndReturn.take(argsAndReturn.size - 1), argsAndReturn.last, false)
6363
}else if(label.matches("Tuple[1-9]") || label.matches("Tuple[1-9][0-9]")){
64-
TupleReference(typeOrBoundsList.map(convertTypeOrBoundsToReference(reflect)(_)))
64+
TupleReference(typeOrBoundsList.map(convertTypeOrBoundsToReference(_)))
6565
}else{
66-
TypeReference(label, link, typeOrBoundsList.map(convertTypeOrBoundsToReference(reflect)(_)), hasOwnFile)
66+
TypeReference(label, link, typeOrBoundsList.map(convertTypeOrBoundsToReference(_)), hasOwnFile)
6767
}
6868
}else{
69-
TypeReference(label, link, typeOrBoundsList.map(convertTypeOrBoundsToReference(reflect)(_)), hasOwnFile)
69+
TypeReference(label, link, typeOrBoundsList.map(convertTypeOrBoundsToReference(_)), hasOwnFile)
7070
}
7171
case _ => throw Exception("Match error in AppliedType. This should not happen, please open an issue. " + tp)
7272
}
7373
case tp @ TypeRef(qual, typeName) =>
74-
convertTypeOrBoundsToReference(reflect)(qual) match {
74+
convertTypeOrBoundsToReference(qual) match {
7575
case TypeReference(label, link, xs, _) => TypeReference(typeName, link + "/" + label, xs, true)
7676
case EmptyReference => TypeReference(typeName, "", Nil, true)
7777
case _ if tp.typeSymbol.exists =>
7878
tp.typeSymbol match {
7979
// NOTE: Only TypeRefs can reference ClassDefSymbols
8080
case sym if sym.isClassDef => //Need to be split because these types have their own file
81-
convertTypeOrBoundsToReference(reflect)(qual) match {
81+
convertTypeOrBoundsToReference(qual) match {
8282
case TypeReference(label, link, xs, _) => TypeReference(sym.name, link + "/" + label, xs, true)
8383
case EmptyReference if sym.name == "<root>" | sym.name == "_root_" => EmptyReference
8484
case EmptyReference => TypeReference(sym.name, "", Nil, true)
85-
case _ => throw Exception("Match error in SymRef/TypeOrBounds/ClassDef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(reflect)(qual))
85+
case _ => throw Exception("Match error in SymRef/TypeOrBounds/ClassDef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual))
8686
}
8787

8888
// NOTE: This branch handles packages, which are now TypeRefs
8989
case sym if sym.isTerm || sym.isTypeDef =>
90-
convertTypeOrBoundsToReference(reflect)(qual) match {
90+
convertTypeOrBoundsToReference(qual) match {
9191
case TypeReference(label, link, xs, _) => TypeReference(sym.name, link + "/" + label, xs)
9292
case EmptyReference if sym.name == "<root>" | sym.name == "_root_" => EmptyReference
9393
case EmptyReference => TypeReference(sym.name, "", Nil)
94-
case _ => throw Exception("Match error in SymRef/TypeOrBounds/Other. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(reflect)(qual))
94+
case _ => throw Exception("Match error in SymRef/TypeOrBounds/Other. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual))
9595
}
9696
case sym => throw Exception("Match error in SymRef. This should not happen, please open an issue. " + sym)
9797
}
9898
case _ =>
99-
throw Exception("Match error in TypeRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(reflect)(qual))
99+
throw Exception("Match error in TypeRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual))
100100
}
101101
case TermRef(qual, typeName) =>
102-
convertTypeOrBoundsToReference(reflect)(qual) match {
102+
convertTypeOrBoundsToReference(qual) match {
103103
case TypeReference(label, link, xs, _) => TypeReference(typeName + "$", link + "/" + label, xs)
104104
case EmptyReference => TypeReference(typeName, "", Nil)
105-
case _ => throw Exception("Match error in TermRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(reflect)(qual))
105+
case _ => throw Exception("Match error in TermRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(qual))
106106
}
107107

108108
// NOTE: old SymRefs are now either TypeRefs or TermRefs - the logic here needs to be moved into above branches
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tastydoc
22

3-
import scala.tasty.Reflection
3+
import scala.quoted._
44
import scala.tasty.inspector.TastyInspector
55

66
import dotty.tastydoc.representations._
@@ -11,9 +11,7 @@ import dotty.tastydoc.representations._
1111
*/
1212
class TastydocInspector(mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) extends TastyInspector {
1313

14-
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit = {
15-
import reflect._
16-
17-
representations.convertToRepresentation(reflect)(root, None)(using mutablePackagesMap)
14+
protected def processCompilationUnit(using QuoteContext)(root: qctx.tasty.Tree): Unit = {
15+
representations.convertToRepresentation(root, None)(using mutablePackagesMap)
1816
}
1917
}

0 commit comments

Comments
 (0)