Skip to content

Commit 6b5b4fa

Browse files
committed
Implement K(m,n)
1 parent 3f9987c commit 6b5b4fa

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ trait BCodeHelpers extends BCodeIdiomatic {
226226
tpe match
227227
case dotty.tools.dotc.transform.TypeB.None => TypeHints.TypeB.NO_HINT
228228
case dotty.tools.dotc.transform.TypeB.M(index) => new TypeHints.TypeB(TypeHints.TypeB.M_KIND, index)
229-
case dotty.tools.dotc.transform.TypeB.K(index) => new TypeHints.TypeB(TypeHints.TypeB.K_KIND, index)
229+
case dotty.tools.dotc.transform.TypeB.K(_, index) => new TypeHints.TypeB(TypeHints.TypeB.K_KIND, index)
230230
// case _ =>
231231
// report.error("unexpected type in to Java TypeB: " + tpe)
232232
// TypeHints.TypeB.NO_HINT // fallback, should not happen
@@ -244,7 +244,7 @@ trait BCodeHelpers extends BCodeIdiomatic {
244244
val attr = new MethodReturnType(typeB)
245245
mw.visitAttribute(attr)
246246
case dotty.tools.dotc.transform.TypeB.None => //do nothing
247-
case dotty.tools.dotc.transform.TypeB.K(index) =>
247+
case dotty.tools.dotc.transform.TypeB.K(_, index) =>
248248
// case _ =>
249249
// report.error("Unexpected type for method return type attribute: " + tpe)
250250

compiler/src/dotty/tools/backend/jvm/BCodeIdiomatic.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ trait BCodeIdiomatic {
443443
case dotty.tools.dotc.transform.TypeA.Short => TypeHints.TypeA.TYPEA_SHORT
444444
case dotty.tools.dotc.transform.TypeA.Boolean => TypeHints.TypeA.TYPEA_BOOLEAN
445445
case dotty.tools.dotc.transform.TypeA.M(x) => TypeHints.TypeA(TypeHints.TypeA.M_KIND, x)
446-
case dotty.tools.dotc.transform.TypeA.K(x) => TypeHints.TypeA(TypeHints.TypeA.K_KIND, x)
446+
case dotty.tools.dotc.transform.TypeA.K(_, x) => TypeHints.TypeA(TypeHints.TypeA.K_KIND, x)
447447
case dotty.tools.dotc.transform.TypeA.Ref => TypeHints.TypeA.TYPEA_REFERENCE
448448

449449

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import reporting.*
2020
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
2121
import dotty.tools.dotc.util.Property
2222
import dotty.tools.dotc.cc.pathOwner
23+
import scala.annotation.tailrec
2324

2425
class ErasurePreservation extends MiniPhase with InfoTransformer {
2526

2627
override def phaseName: String = ErasurePreservation.name
2728

2829
override def description: String = ErasurePreservation.description
2930

30-
def toTypeA(tp: Type)(using Context): TypeA = trace(s"toTypeA ${tp}") {tp match
31+
def toTypeA(tp: Type, sourceSym: Symbol)(using Context): TypeA = trace(s"toTypeA ${tp}") {tp match
3132
case tpr: TypeParamRef => TypeA.M(tpr.paramNum)
3233
case tr: TypeRef =>
3334
// println(tr.symbol.owner.paramSymss)
@@ -43,7 +44,8 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
4344
val owner = tr.symbol.owner
4445
if owner.isClass then
4546
val ind = owner.typeParams.indexOf(tr.symbol)
46-
if ind != -1 then TypeA.K(ind) else ???
47+
val n = debrujin(sourceSym.enclosingClass, owner)
48+
if ind != -1 then TypeA.K(n, ind) else ???
4749
else
4850
val ind = owner.paramSymss.headOption match
4951
case None => assert(false, i"Got unexpected type ${tp}")
@@ -53,14 +55,19 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
5355
case _ => assert(false)
5456
}
5557

58+
def debrujin(source: Symbol, outer: Symbol)(using Context): Int = trace(i"debrujin: $source, $outer") {
59+
if (source.enclosingClass == outer) then 0
60+
else debrujin(source.owner, outer)+1
61+
}
5662

57-
def toTypeB(tp: Type)(using Context): TypeB = trace(i"toTypeB ${tp}"){ tp match
63+
def toTypeB(tp: Type, sourceSym: Symbol)(using Context): TypeB = trace(i"toTypeB ${tp}"){ tp match
5864
case tpr: TypeParamRef => TypeB.M(tpr.paramNum)
5965
case tr: TypeRef if tr.symbol.isTypeParam =>
6066
val owner = tr.symbol.owner
6167
if owner.isClass then
6268
val ind = owner.typeParams.indexOf(tr.symbol)
63-
if ind != -1 then TypeB.K(ind) else TypeB.None
69+
val n = debrujin(sourceSym.enclosingClass, owner)
70+
if ind != -1 then TypeB.K(n, ind) else TypeB.None
6471
else
6572
val ind = owner.paramSymss.headOption match
6673
case None => assert(false, i"Got unexpected type ${tp}")
@@ -69,12 +76,13 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
6976
case _ => TypeB.None
7077
}
7178

72-
def toReturnTypeB(tp: Type)(using Context): TypeB = tp match
79+
def toReturnTypeB(tp: Type, sourceSym: Symbol)(using Context): TypeB = tp match
7380
case tr: TypeRef if tr.symbol.isTypeParam =>
7481
val owner = tr.symbol.owner
7582
if owner.isClass then
7683
val ind = owner.typeParams.indexOf(tr.symbol)
77-
if ind != -1 then TypeB.K(ind) else TypeB.None
84+
val n = debrujin(sourceSym.enclosingClass, owner)
85+
if ind != -1 then TypeB.K(n, ind) else TypeB.None
7886
else
7987
val ind = owner.paramSymss.headOption match
8088
case None => assert(false, i"Got unexpected type ${tp}")
@@ -87,16 +95,17 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
8795
case pt: PolyType =>
8896
pt.resType match
8997
case mt: MethodType =>
90-
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, mt.paramInfos.map(toTypeB), toTypeB(mt.resType)))
98+
// println(i"sym context: $sym, ${sym.enclosingClass}, ${sym.enclosingClass.owner}")
99+
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, mt.paramInfos.map(p => toTypeB(p, sym)), toTypeB(mt.resType, sym)))
91100
case other =>
92-
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, Nil, toTypeB(other.widenExpr)))
101+
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, Nil, toTypeB(other.widenExpr, sym)))
93102
case mt: MethodType =>
94-
val params = mt.paramInfos.map(toTypeB)
95-
val ret = toTypeB(mt.resType)
103+
val params = mt.paramInfos.map(p => toTypeB(p, sym))
104+
val ret = toTypeB(mt.resType, sym)
96105
if (params.exists(_ != TypeB.None) || ret != TypeB.None) then
97106
sym.addAnnotation(ErasedInfo(0, params, ret))
98107
case et: ExprType =>
99-
val ret = toTypeB(et.widenExpr)
108+
val ret = toTypeB(et.widenExpr, sym)
100109
if (ret != TypeB.None) then
101110
sym.addAnnotation(ErasedInfo(0, Nil, ret))
102111
()
@@ -105,11 +114,11 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
105114
}
106115

107116
override def transformApply(tree: tpd.Apply)(using Context): tpd.Tree =
108-
tree.putAttachment(InvokeReturnType, toReturnTypeB(tree.tpe))
117+
tree.putAttachment(InvokeReturnType, toReturnTypeB(tree.tpe, tree.symbol))
109118
tree
110119

111120
override def transformTypeApply(tree: tpd.TypeApply)(using Context): tpd.Tree =
112-
val args = tree.args.map(_.tpe).map(toTypeA)
121+
val args = tree.args.map(_.tpe).map(p => toTypeA(p, tree.symbol))
113122
tree.fun.putAttachment(InstructionTypeArguments, args) // Pattern match args based on their types
114123
tree
115124

@@ -135,15 +144,15 @@ enum TypeA:
135144
case Boolean
136145
case M(paramNum: Int)
137146
case K(
138-
// outer: Int,
147+
outer: Int,
139148
paramNum: Int)
140149
case Ref
141150

142151
enum TypeB:
143152
case None
144153
case M(paramNum: Int)
145154
case K(
146-
// outer: Int,
155+
outer: Int,
147156
paramNum: Int)
148157
// case class TypeB(tp: Type)
149158

0 commit comments

Comments
 (0)