Skip to content

Commit 513225c

Browse files
committed
WIP
1 parent 8c17945 commit 513225c

File tree

1 file changed

+68
-53
lines changed

1 file changed

+68
-53
lines changed

compiler/src/dotty/tools/dotc/transform/ErasurePreservation.scala

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ class ErasurePreservation extends MiniPhase {
2828

2929
override def description: String = ErasurePreservation.description
3030

31+
def getOuterTyParamSyms(sym: Symbol)(using Context): List[Symbol] =
32+
if !sym.exists then List()
33+
else getOuterTyParamSyms(sym.owner) ++ (sym.paramSymss.flatten)
34+
35+
def typeParamToTypeBKM(tr: TypeRef, sourceSym: Symbol)(using Context): TypeB = trace.force(i"${tr}, ${tr.symbol.owner}"){
36+
val outerTyParams = getOuterTyParamSyms(sourceSym)
37+
val owner = tr.symbol.owner
38+
if owner.isClass then
39+
val ind = (outerTyParams++owner.typeParams).indexOf(tr.symbol)
40+
val n = debrujin(sourceSym.enclosingClass, owner)
41+
if ind != -1 then TypeB.K(n, ind) else TypeB.None
42+
else
43+
val ind = (outerTyParams++owner.paramSymss.flatten).indexWhere(tr.isRef(_))
44+
if ind != -1 then TypeB.M(ind) else TypeB.None
45+
}
46+
47+
def typeParamToTypeAKM(tr: TypeRef, sourceSym: Symbol)(using Context): TypeA =
48+
val outerTyParams = getOuterTyParamSyms(sourceSym)
49+
val owner = tr.symbol.owner
50+
if owner.isClass then
51+
val ind = (outerTyParams++owner.typeParams).indexOf(tr.symbol)
52+
val n = debrujin(sourceSym.enclosingClass, owner)
53+
if ind != -1 then TypeA.K(n, ind) else ???
54+
else
55+
val ind = (outerTyParams++owner.paramSymss.flatten).indexWhere(tr.isRef(_))
56+
if ind != -1 then TypeA.M(ind) else ???
57+
3158
def toTypeA(tp: Type, sourceSym: Symbol)(using Context): TypeA = trace(i"toTypeA ${tp}, ${sourceSym}") {tp.widen match
3259
case tpr: TypeParamRef => TypeA.M(tpr.paramNum)
3360
case tr: TypeRef =>
@@ -41,16 +68,7 @@ class ErasurePreservation extends MiniPhase {
4168
if tr.isRef(defn.ShortClass) then TypeA.Short else
4269
if tr.isRef(defn.BooleanClass) then TypeA.Boolean else
4370
if tr.symbol.isTypeParam then
44-
val owner = tr.symbol.owner
45-
if owner.isClass then
46-
val ind = owner.typeParams.indexOf(tr.symbol)
47-
val n = debrujin(sourceSym.enclosingClass, owner)
48-
if ind != -1 then TypeA.K(n, ind) else ???
49-
else
50-
val ind = owner.paramSymss.headOption match
51-
case None => assert(false, i"Got unexpected type ${tp}")
52-
case Some(value) => value.indexWhere(tr.isRef(_))
53-
if ind != -1 then TypeA.M(ind) else ???
71+
typeParamToTypeAKM(tr, sourceSym)
5472
else TypeA.Ref
5573
case _ =>
5674
TypeA.Ref
@@ -63,62 +81,59 @@ class ErasurePreservation extends MiniPhase {
6381
debrujin(source.owner, outer)+1
6482
}
6583

66-
def toTypeB(tp: Type, sourceSym: Symbol)(using Context): TypeB = trace(i"toTypeB ${tp}, ${sourceSym}"){ tp match
67-
case tpr: TypeParamRef => TypeB.M(tpr.paramNum)
84+
def toTypeB(tp: Type, sourceSym: Symbol, method: Type)(using Context): TypeB = trace.force(i"toTypeB ${tp}, ${sourceSym}"){ tp match
85+
case tpr: TypeParamRef =>
86+
val outerTyParams = getOuterTyParamSyms(sourceSym)
87+
TypeB.M(outerTyParams.length+indexTypeParam(method, tpr))
6888
case tr: TypeRef if tr.symbol.isTypeParam =>
69-
val owner = tr.symbol.owner
70-
if owner.isClass then
71-
val ind = owner.typeParams.indexOf(tr.symbol)
72-
val n = debrujin(sourceSym.enclosingClass, owner)
73-
if ind != -1 then TypeB.K(n, ind) else TypeB.None
74-
else
75-
val ind = owner.paramSymss.headOption match
76-
case None => assert(false, i"Got unexpected type ${tp}")
77-
case Some(value) => value.indexWhere(tr.isRef(_))
78-
if ind != -1 then TypeB.M(ind) else TypeB.None
89+
typeParamToTypeBKM(tr, sourceSym)
7990
case _ => TypeB.None
8091
}
8192

8293
def toReturnTypeB(tp: Type, sourceSym: Symbol)(using Context): TypeB = tp match
8394
case tr: TypeRef if tr.symbol.isTypeParam =>
84-
val owner = tr.symbol.owner
85-
if owner.isClass then
86-
val ind = owner.typeParams.indexOf(tr.symbol)
87-
val n = debrujin(sourceSym.enclosingClass, owner)
88-
// val n = debrujin(ctx.owner, owner)
89-
if ind != -1 then TypeB.K(n, ind) else TypeB.None
90-
else
91-
val ind = owner.paramSymss.headOption match
92-
case None => assert(false, i"Got unexpected type ${tp}")
93-
case Some(value) => value.indexWhere(tr.isRef(_))
94-
if ind != -1 then TypeB.M(ind) else TypeB.None
95+
typeParamToTypeBKM(tr, sourceSym)
9596
case _ => TypeB.None
9697

97-
override def transformDefDef(tree: tpd.DefDef)(using Context): tpd.Tree = trace(i"transformDefDef $tree, ${tree.tpe}, ${tree.tpe.widen}"){
98-
tree.tpe.widen match
99-
case pt: PolyType =>
100-
pt.resType match
101-
case mt: MethodType =>
102-
tree.putAttachment(MethodParameterReturnType,
103-
(pt.paramInfos.size, mt.paramInfos.map(p => toTypeB(p, ctx.owner)), toTypeB(mt.resType, ctx.owner)))
104-
case other =>
105-
tree.putAttachment(MethodParameterReturnType,
106-
(pt.paramInfos.size, Nil, toTypeB(other.widenExpr, ctx.owner)))
98+
def indexTypeParam(method: Type, tpr: TypeParamRef): Int = method match
99+
case pt: PolyType =>
100+
if tpr.binder == pt then
101+
tpr.paramNum
102+
else indexTypeParam(pt.resType, tpr) + pt.paramNames.size
103+
case mt: MethodType => indexTypeParam(mt.resType, tpr)
104+
case _ => ???
105+
106+
override def transformDefDef(tree: tpd.DefDef)(using Context): tpd.Tree = trace.force(i"transformDefDef $tree, ${tree.tpe}, ${tree.tpe.widen}"){
107+
val tup = tree.tpe.widen match
108+
case pt: PolyType => pt.resType match
109+
case mt: MethodType =>
110+
// println(mt.paramInfos)
111+
// println(mt.paramInfoss)
112+
// println(tree.tpe.widen)
113+
val ptParams = pt.paramInfoss.flatten
114+
Some((ptParams.size, mt.paramInfoss.flatten.map(p => toTypeB(p, ctx.owner, pt)), toTypeB(mt.resType, ctx.owner, pt)))
115+
case other =>
116+
Some((pt.paramInfoss.flatten.size, Nil, toTypeB(other.widenExpr, ctx.owner, pt)))
107117
case mt: MethodType =>
108-
val params = mt.paramInfos.map(p => toTypeB(p, ctx.owner))
109-
// println(i"MethodType")
110-
val ret = toTypeB(mt.resType, ctx.owner)
118+
val params = mt.paramInfos.map(p => toTypeB(p, ctx.owner, mt))
119+
val ret = toTypeB(mt.resType, ctx.owner, mt)
111120
if (params.exists(_ != TypeB.None) || ret != TypeB.None) then
112-
tree.putAttachment(MethodParameterReturnType,
113-
(0, params, ret))
114-
case tr: TypeRef =>
115-
tree.putAttachment(MethodParameterReturnType, (0, Nil, toTypeB(tr, ctx.owner)))
121+
Some((0, params, ret))
122+
else
123+
None
124+
// case tr: TypeRef => Some((0, Nil, toTypeB(tr, ctx.owner)))
116125
case et: ExprType =>
117126
???
118-
val ret = toTypeB(et.widenExpr, ctx.owner)
127+
val ret = toTypeB(et.widenExpr, ctx.owner, ???)
119128
if (ret != TypeB.None) then
120-
tree.putAttachment(MethodParameterReturnType, (0, Nil, ret))
121-
case other => ()
129+
Some((0, Nil, ret))
130+
else
131+
None
132+
case other => None
133+
if tup.isDefined then
134+
val outerTyParams = getOuterTyParamSyms(ctx.owner)
135+
val (paramCount, paramRefs, retType) = tup.get
136+
tree.putAttachment(MethodParameterReturnType, (paramCount+outerTyParams.length, paramRefs, retType))
122137
tree
123138
}
124139

0 commit comments

Comments
 (0)