Skip to content

Commit ebdf900

Browse files
committed
findMember of RefinedTypes
1 parent 37d8d6c commit ebdf900

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

compiler/src/dotty/tools/dotc/semanticdb/SymbolOps.scala

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ object SymbolOps:
1616
extension (sym: Symbol)
1717
def sig(using LinkMode, Context): s.Signature =
1818
import TypeOps._
19+
println("")
20+
println(sym.toString)
21+
println(s"=========sym.info================")
22+
pprint.pprintln(sym.info)
1923
val sig = sym.info.toSemanticSig(sym)
20-
// println("")
21-
// println(sym.toString)
22-
// println(s"=========sym.info================")
23-
// pprint.pprintln(sym.info)
24-
// pprint.pprintln(sig)
24+
println(s"=========sig================")
25+
pprint.pprintln(sig)
2526
sig
2627

2728
object TypeOps:
@@ -35,16 +36,16 @@ object TypeOps:
3536
s.MethodSignature(
3637
stparams,
3738
sparamss,
38-
mt.resType.toSemanticType
39+
mt.resType.toSemanticType(sym)
3940
)
4041

4142
case cls: ClassInfo =>
4243
val stparams =
4344
if (cls.cls.typeParams.nonEmpty)
4445
Some(cls.cls.typeParams.sscope)
4546
else None
46-
val sparents = cls.parents.map(_.toSemanticType)
47-
val sself = cls.selfType.toSemanticType
47+
val sparents = cls.parents.map(_.toSemanticType(sym))
48+
val sself = cls.selfType.toSemanticType(sym)
4849
val decls = cls.decls.toList.sscope(using LinkMode.HardlinkChildren)
4950
s.ClassSignature(stparams, sparents, sself, Some(decls))
5051

@@ -53,8 +54,8 @@ object TypeOps:
5354
// type Poly[T] = List[T] is equivalent with
5455
// type Poly = [T] =>> List[T]
5556
val stparams = Some(s.Scope())
56-
val slo = lo.toSemanticType
57-
val shi = hi.toSemanticType
57+
val slo = lo.toSemanticType(sym)
58+
val shi = hi.toSemanticType(sym)
5859
s.TypeSignature(stparams, slo, shi)
5960

6061
case pt: PolyType =>
@@ -72,12 +73,12 @@ object TypeOps:
7273

7374
case other =>
7475
s.ValueSignature(
75-
other.toSemanticType
76+
other.toSemanticType(sym)
7677
)
7778
}
7879
loop(tpe)
7980

80-
private def toSemanticType(using LinkMode, Context): s.Type =
81+
private def toSemanticType(using LinkMode)(using ctx: Context)(sym: Symbol): s.Type =
8182
import ConstantOps._
8283
def loop(tpe: Type): s.Type = tpe match {
8384
case ExprType(tpe) =>
@@ -94,9 +95,22 @@ object TypeOps:
9495
val ssym = symbolName(desig.asInstanceOf[Symbol])
9596
s.SingleType(spre, ssym)
9697

97-
// TODO: TypeParamRef and TermParamRef
98-
// but how can we retrieve their symbols? (for representing them as TypeRef and TermRef on Semanticdb)
99-
// or should we add new types to Semanticdb
98+
case tref: TypeParamRef =>
99+
// println(s"rawParamss: ${sym.rawParamss}")
100+
// println(s"num: ${tref.paramNum}, name: ${tref.paramName}")
101+
val paramref = sym.rawParamss.flatMap { params =>
102+
if (params.length > tref.paramNum) Some(params(tref.paramNum))
103+
else None
104+
}.find(p => p.name == tref.paramName)
105+
paramref match {
106+
case Some(ref) =>
107+
val ssym = symbolName(ref)
108+
s.TypeRef(s.Type.Empty, ssym, Seq.empty)
109+
case None => // shouldn't happen
110+
s.Type.Empty
111+
}
112+
113+
// TODO: TermParamRef
100114

101115
case ThisType(TypeRef(_, desig)) if desig.isInstanceOf[Symbol] =>
102116
val ssym = symbolName(desig.asInstanceOf[Symbol])
@@ -125,22 +139,41 @@ object TypeOps:
125139
// refinedName = x
126140
// refinedInfo = TypeRef(..., Int)
127141
// )
128-
def getParent(parent: Type): Type = parent match {
129-
case RefinedType(p, _, _) => getParent(p)
130-
case _ => parent
142+
143+
type RefinedInfo = (core.Names.Name, Type)
144+
def flatten(tpe: Type, acc: List[RefinedInfo]): (Type, List[RefinedInfo]) = tpe match {
145+
case RefinedType(parent, name, info) =>
146+
flatten(parent, acc :+ (name, info))
147+
case _ =>
148+
(tpe, acc)
131149
}
150+
val (parent, refinedInfos) = flatten(rt, List.empty)
132151

133-
val realParent = getParent(parent)
134-
val stpe = realParent match {
152+
val stpe = parent match {
135153
// val tp: M with N { def k: Int } = ???
136154
case AndType(x, y) =>
137155
s.WithType(Seq(loop(x), loop(y))) // TODO: for M with N with L
138156
case _ =>
139157
s.WithType(Seq(loop(parent)))
140158
}
141-
// TODO: collect all refinement symbols
142-
// val sdecls = decls.sscope(using LinkMode.HardlinkChildren)
143-
s.StructuralType(stpe, None)
159+
val decls = for {
160+
(name, info) <- refinedInfos
161+
} yield {
162+
// do we need this?
163+
// isLegalPrefix returns true outside in typer
164+
val pre = ctx.typeAssigner.maybeSkolemizePrefix(rt, name)
165+
val denot = rt.findMember(name, pre)
166+
// assert(denot.info eq info, s"(${denot.info.show}) is not eq to (${info.show})")
167+
println(s"denot.info: ${denot.info}, info: ${info}, sym: ${denot.symbol}")
168+
val sym =
169+
if denot.symbol.exists then
170+
println(s"refined ${denot.symbol.show} with ${info.show}")
171+
denot.symbol
172+
else ???
173+
sym
174+
}
175+
val sdecls = decls.sscope(using LinkMode.HardlinkChildren)
176+
s.StructuralType(stpe, Some(sdecls))
144177

145178
case rec: RecType =>
146179
loop(rec.parent) // should be handled as RefinedType

0 commit comments

Comments
 (0)