Skip to content

Commit 48bab6d

Browse files
committed
Cleanup SyntheticsSupport
1 parent 4f4ff7d commit 48bab6d

File tree

4 files changed

+60
-48
lines changed

4 files changed

+60
-48
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ import collection.JavaConverters._
44
import dotty.tools.scaladoc._
55
import dotty.tools.scaladoc.{Signature => DSignature}
66

7+
import scala.quoted._
8+
79
import SymOps._
810
import NameNormalizer._
11+
import SyntheticsSupport._
912

1013
trait ClassLikeSupport:
1114
self: TastyParser =>
1215
import qctx.reflect._
1316

1417
private given qctx.type = qctx
1518

16-
private def bareClasslikeKind(symbol: Symbol): Kind =
17-
if symbol.flags.is(Flags.Module) then Kind.Object
18-
else if symbol.flags.is(Flags.Trait) then Kind.Trait(Nil, Nil)
19-
else if symbol.flags.is(Flags.Enum) then Kind.Enum(Nil, Nil)
20-
else if symbol.flags.is(Flags.Enum) && symbol.flags.is(Flags.Case) then Kind.EnumCase(Kind.Object)
21-
else Kind.Class(Nil, Nil)
19+
private def bareClasslikeKind(using Quotes)(symbol: quotes.reflect.Symbol): Kind =
20+
import quotes.reflect._
21+
if symbol.flags.is(Flags.Module) then Kind.Object
22+
else if symbol.flags.is(Flags.Trait) then Kind.Trait(Nil, Nil)
23+
else if symbol.flags.is(Flags.Enum) then Kind.Enum(Nil, Nil)
24+
else if symbol.flags.is(Flags.Enum) && symbol.flags.is(Flags.Case) then Kind.EnumCase(Kind.Object)
25+
else Kind.Class(Nil, Nil)
2226

2327
private def kindForClasslike(classDef: ClassDef): Kind =
2428
def typeArgs = classDef.getTypeParams.map(mkTypeArgument(_))
@@ -213,14 +217,16 @@ trait ClassLikeSupport:
213217
}
214218
).map(_.copy(inheritedFrom = inheritance))
215219

216-
extension (c: ClassDef)
220+
extension (using Quotes)(c: quotes.reflect.ClassDef)
221+
217222
def membersToDocument = c.body.filterNot(_.symbol.isHiddenByVisibility)
218223

219224
def getNonTrivialInheritedMemberTrees =
220225
c.symbol.getmembers.filterNot(s => s.isHiddenByVisibility || s.maybeOwner == c.symbol)
221226
.filter(s => s.maybeOwner != defn.ObjectClass && s.maybeOwner != defn.AnyClass)
222227
.map(_.tree)
223228

229+
extension (c: ClassDef)
224230
def extractMembers: Seq[Member] = {
225231
val inherited = c.getNonTrivialInheritedMemberTrees.collect {
226232
case dd: DefDef if !dd.symbol.isClassConstructor && !(dd.symbol.isSuperBridgeMethod || dd.symbol.isDefaultHelperMethod) => dd
@@ -270,7 +276,7 @@ trait ClassLikeSupport:
270276
if parentSymbol != defn.ObjectClass && parentSymbol != defn.AnyClass
271277
yield (parentTree, parentSymbol)
272278

273-
def getConstructors: List[Symbol] = membersToDocument.collect {
279+
def getConstructors: List[Symbol] = c.membersToDocument.collect {
274280
case d: DefDef if d.symbol.isClassConstructor && c.constructor.symbol != d.symbol => d.symbol
275281
}.toList
276282

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

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,53 @@ package tasty
33

44
import scala.quoted._
55

6-
trait SyntheticsSupport:
7-
self: TastyParser =>
6+
object SyntheticsSupport:
87

9-
import qctx.reflect._
8+
extension (using Quotes)(t: quotes.reflect.TypeRepr)
9+
def isTupleType: Boolean = t.hackIsTupleType(t)
1010

11-
extension (t: TypeRepr)
12-
def isTupleType: Boolean = hackIsTupleType(using qctx)(t)
11+
def isCompiletimeAppliedType: Boolean = t.hackIsCompiletimeAppliedType(t)
1312

14-
def isCompiletimeAppliedType: Boolean = hackIsCompiletimeAppliedType(using qctx)(t)
15-
16-
def hackIsTupleType(using Quotes)(rtpe: qctx.reflect.TypeRepr): Boolean =
13+
private def hackIsTupleType(rtpe: quotes.reflect.TypeRepr): Boolean =
1714
import dotty.tools.dotc
18-
given ctx: dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
15+
given ctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
1916
val tpe = rtpe.asInstanceOf[dotc.core.Types.Type]
2017
ctx.definitions.isTupleType(tpe)
2118

22-
def hackIsCompiletimeAppliedType(using Quotes)(rtpe: qctx.reflect.TypeRepr): Boolean =
19+
private def hackIsCompiletimeAppliedType(rtpe: quotes.reflect.TypeRepr): Boolean =
2320
import dotty.tools.dotc
24-
given ctx: dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
21+
given ctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
2522
val tpe = rtpe.asInstanceOf[dotc.core.Types.Type]
2623
ctx.definitions.isCompiletimeAppliedType(tpe.typeSymbol)
24+
end extension
2725

28-
extension (s: Symbol)
29-
def isSyntheticFunc: Boolean = s.flags.is(Flags.Synthetic) || s.flags.is(Flags.FieldAccessor) || isDefaultHelperMethod
26+
extension (using Quotes)(s: quotes.reflect.Symbol)
27+
def isSyntheticFunc: Boolean =
28+
import quotes.reflect._
29+
s.flags.is(Flags.Synthetic) || s.flags.is(Flags.FieldAccessor) || s.isDefaultHelperMethod
3030

3131
def isSuperBridgeMethod: Boolean = s.name.contains("$super$")
3232

3333
def isDefaultHelperMethod: Boolean = ".*\\$default\\$\\d+$".r.matches(s.name)
3434

35-
def isOpaque: Boolean = s.flags.is(Flags.Opaque)
35+
def isOpaque: Boolean =
36+
import quotes.reflect._
37+
s.flags.is(Flags.Opaque)
38+
39+
def isInfix: Boolean = hackIsInfix(s)
3640

37-
def isInfix: Boolean = hackIsInfix(using qctx)(s)
41+
def getmembers: List[quotes.reflect.Symbol] = hackGetmembers(s)
3842

39-
def getmembers: List[Symbol] = hackGetmembers(using qctx)(s)
43+
end extension
4044

41-
def isValidPos(pos: Position) =
42-
if hackExists(using qctx)(pos) then pos.start != pos.end else false
45+
def isValidPos(using Quotes)(pos: quotes.reflect.Position) =
46+
if hackExists(pos) then pos.start != pos.end else false
4347

44-
def isSyntheticField(c: Symbol) =
48+
def isSyntheticField(using Quotes)(c: quotes.reflect.Symbol) =
49+
import quotes.reflect._
4550
c.flags.is(Flags.CaseAccessor) || (c.flags.is(Flags.Module) && !c.flags.is(Flags.Given))
4651

47-
def constructorWithoutParamLists(c: ClassDef): Boolean =
52+
def constructorWithoutParamLists(using Quotes)(c: quotes.reflect.ClassDef): Boolean =
4853
!isValidPos(c.constructor.pos) || {
4954
val end = c.constructor.pos.end
5055
val typesEnd = c.constructor.leadingTypeParams.lastOption.fold(end - 1)(_.pos.end)
@@ -53,21 +58,21 @@ trait SyntheticsSupport:
5358
}
5459

5560
// TODO: #49 Remove it after TASTY-Reflect release with published flag Extension
56-
def hackIsInfix(using Quotes)(rsym: qctx.reflect.Symbol): Boolean = {
57-
import qctx.reflect._
61+
private def hackIsInfix(using Quotes)(rsym: quotes.reflect.Symbol): Boolean = {
62+
import quotes.reflect._
5863
import dotty.tools.dotc
59-
given ctx: dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
64+
given ctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
6065
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
6166
ctx.definitions.isInfix(sym)
6267
}
6368
/* We need there to filter out symbols with certain flagsets, because these symbols come from compiler and TASTY can't handle them well.
6469
They are valdefs that describe case companion objects and cases from enum.
6570
TASTY crashed when calling _.tree on them.
6671
*/
67-
def hackGetmembers(using Quotes)(rsym: qctx.reflect.Symbol): List[qctx.reflect.Symbol] = {
68-
import qctx.reflect._
72+
private def hackGetmembers(using Quotes)(rsym: quotes.reflect.Symbol): List[quotes.reflect.Symbol] = {
73+
import quotes.reflect._
6974
import dotty.tools.dotc
70-
given ctx: dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
75+
given ctx: dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
7176
val sym = rsym.asInstanceOf[dotc.core.Symbols.Symbol]
7277
sym.typeRef.appliedTo(sym.typeParams.map(_.typeRef)).allMembers.iterator.map(_.symbol)
7378
.collect {
@@ -78,39 +83,39 @@ trait SyntheticsSupport:
7883
}.toList
7984
}
8085

81-
def hackGetSupertypes(using Quotes)(rdef: qctx.reflect.ClassDef) = {
82-
import qctx.reflect._
86+
private def hackGetSupertypes(using Quotes)(rdef: quotes.reflect.ClassDef) = {
87+
import quotes.reflect._
8388
import dotty.tools.dotc
84-
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
89+
given dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
8590
val classdef = rdef.asInstanceOf[dotc.ast.tpd.TypeDef]
8691
val ref = classdef.symbol.info.asInstanceOf[dotc.core.Types.ClassInfo].appliedRef
8792
val baseTypes: List[(dotc.core.Symbols.Symbol, dotc.core.Types.Type)] =
8893
ref.baseClasses.map(b => b -> ref.baseType(b))
8994
baseTypes.asInstanceOf[List[(Symbol, TypeRepr)]]
9095
}
9196

92-
def hackExists(using Quotes)(rpos: qctx.reflect.Position) = {
93-
import qctx.reflect._
97+
private def hackExists(using Quotes)(rpos: quotes.reflect.Position) = {
98+
import quotes.reflect._
9499
import dotty.tools.dotc
95100
import dotty.tools.dotc.util.Spans._
96-
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
101+
given dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
97102
val pos = rpos.asInstanceOf[dotc.util.SourcePosition]
98103
pos.exists
99104
}
100105

101-
def getSupertypes(using Quotes)(c: ClassDef) = hackGetSupertypes(c).tail
106+
def getSupertypes(using Quotes)(c: quotes.reflect.ClassDef) = hackGetSupertypes(c).tail
102107

103-
def typeForClass(c: ClassDef): TypeRepr =
104-
import qctx.reflect._
108+
def typeForClass(using Quotes)(c: quotes.reflect.ClassDef): quotes.reflect.TypeRepr =
109+
import quotes.reflect._
105110
import dotty.tools.dotc
106-
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
111+
given dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
107112
val cSym = c.symbol.asInstanceOf[dotc.core.Symbols.Symbol]
108113
cSym.typeRef.appliedTo(cSym.typeParams.map(_.typeRef)).asInstanceOf[TypeRepr]
109114

110-
def memberInfo(c: ClassDef, symbol: Symbol): TypeRepr =
111-
import qctx.reflect._
115+
def memberInfo(using Quotes)(c: quotes.reflect.ClassDef, symbol: quotes.reflect.Symbol): quotes.reflect.TypeRepr =
116+
import quotes.reflect._
112117
import dotty.tools.dotc
113-
given dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
118+
given dotc.core.Contexts.Context = quotes.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
114119
typeForClass(c).asInstanceOf[dotc.core.Types.Type]
115120
.memberInfo(symbol.asInstanceOf[dotc.core.Symbols.Symbol])
116121
.asInstanceOf[TypeRepr]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ case class TastyParser(
178178
isSkipped: qctx.reflect.Symbol => Boolean
179179
)(
180180
using val ctx: DocContext
181-
) extends BasicSupport with TypesSupport with ClassLikeSupport with SyntheticsSupport with PackageSupport:
181+
) extends BasicSupport with TypesSupport with ClassLikeSupport with PackageSupport:
182182
import qctx.reflect._
183183

184184
private given qctx.type = qctx

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package tasty
44
import collection.JavaConverters._
55

66
import NameNormalizer._
7+
import SyntheticsSupport._
78

89
trait TypesSupport:
910
self: TastyParser =>

0 commit comments

Comments
 (0)