Skip to content

Commit ef088f9

Browse files
committed
refactored for class type A, Bs (K<m,n>)
1 parent dac2630 commit ef088f9

File tree

11 files changed

+219
-42
lines changed

11 files changed

+219
-42
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +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 _ =>
230-
// report.error("unexpected type in to Java TypeB: " + tpe)
231-
// TypeHints.TypeB.NO_HINT // fallback, should not happen
229+
case dotty.tools.dotc.transform.TypeB.K(y, x) => new TypeHints.TypeB(TypeHints.TypeB.K_KIND, y, x)
232230

233231
def addMethodTypeParameterCountAttribute(mw: asm.MethodVisitor, count: Int): Unit =
234232
if (count > 0){
@@ -237,14 +235,17 @@ trait BCodeHelpers extends BCodeIdiomatic {
237235
}
238236

239237
def addMethodReturnTypeAttribute(mw: asm.MethodVisitor, tpe: dotty.tools.dotc.transform.TypeB): Unit =
240-
tpe match
241-
case dotty.tools.dotc.transform.TypeB.M(index) =>
242-
val typeB = new TypeHints.TypeB(TypeHints.TypeB.M_KIND, index)
243-
val attr = new MethodReturnType(typeB)
244-
mw.visitAttribute(attr)
245-
case dotty.tools.dotc.transform.TypeB.None => //do nothing
246-
// case _ =>
247-
// report.error("Unexpected type for method return type attribute: " + tpe)
238+
val typeB =
239+
tpe match
240+
case dotty.tools.dotc.transform.TypeB.K(outerClassIndex, index) =>
241+
new TypeHints.TypeB(TypeHints.TypeB.K_KIND, outerClassIndex, index)
242+
case dotty.tools.dotc.transform.TypeB.M(index) =>
243+
new TypeHints.TypeB(TypeHints.TypeB.M_KIND, index)
244+
case dotty.tools.dotc.transform.TypeB.None => TypeHints.TypeB.NO_HINT//do nothing
245+
if (typeB != TypeHints.TypeB.NO_HINT) {
246+
val attr = new MethodReturnType(typeB)
247+
mw.visitAttribute(attr)
248+
}
248249

249250

250251
def addMethodParameterTypeAttribute(mw: asm.MethodVisitor, lst: List[dotty.tools.dotc.transform.TypeB]) : Unit =

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ trait BCodeIdiomatic {
430430
tpe match
431431
case dotty.tools.dotc.transform.TypeB.None => TypeHints.TypeB.NO_HINT
432432
case dotty.tools.dotc.transform.TypeB.M(index) => new TypeHints.TypeB(TypeHints.TypeB.M_KIND, index)
433+
case dotty.tools.dotc.transform.TypeB.K(y, x) => new TypeHints.TypeB(TypeHints.TypeB.K_KIND, y, x)
433434

434435
def toJTypeA(tpe: dotty.tools.dotc.transform.TypeA): TypeHints.TypeA =
435436
tpe match
@@ -442,7 +443,7 @@ trait BCodeIdiomatic {
442443
case dotty.tools.dotc.transform.TypeA.Short => TypeHints.TypeA.TYPEA_SHORT
443444
case dotty.tools.dotc.transform.TypeA.Boolean => TypeHints.TypeA.TYPEA_BOOLEAN
444445
case dotty.tools.dotc.transform.TypeA.M(x) => TypeHints.TypeA(TypeHints.TypeA.M_KIND, x)
445-
case dotty.tools.dotc.transform.TypeA.K(x) => TypeHints.TypeA(TypeHints.TypeA.K_KIND, x)
446+
case dotty.tools.dotc.transform.TypeA.K(y, x) => TypeHints.TypeA(TypeHints.TypeA.K_KIND, y, x)
446447
case dotty.tools.dotc.transform.TypeA.Ref => TypeHints.TypeA.TYPEA_REFERENCE
447448

448449

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dotty.tools.backend.jvm.attributes;
2+
3+
import scala.tools.asm.Attribute;
4+
import scala.tools.asm.ByteVector;
5+
import scala.tools.asm.ClassReader;
6+
import scala.tools.asm.ClassWriter;
7+
import scala.tools.asm.Label;
8+
9+
/*
10+
ClassTypeParameterCount_attribute {
11+
u2 attribute_name_index;
12+
u4 attribute_length;
13+
u2 count;
14+
}
15+
*/
16+
public class ClassTypeParameterCount extends Attribute{
17+
private final int count;
18+
public ClassTypeParameterCount() {
19+
super("ClassTypeParameterCount");
20+
this.count = 0;
21+
}
22+
public ClassTypeParameterCount(int count) {
23+
super("ClassTypeParameterCount");
24+
this.count = count;
25+
}
26+
public int getCount() {
27+
return count;
28+
}
29+
@Override
30+
public boolean isUnknown() {
31+
return false;
32+
}
33+
@Override
34+
protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
35+
int localCount = cr.readUnsignedShort(off);
36+
return new MethodTypeParameterCount(localCount);
37+
}
38+
39+
@Override
40+
protected ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStack, int maxLocals) {
41+
ByteVector bv = new ByteVector();
42+
bv.putShort(count);
43+
return bv;
44+
}
45+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package dotty.tools.backend.jvm.attributes;
2+
3+
import scala.tools.asm.Attribute;
4+
import scala.tools.asm.ByteVector;
5+
import scala.tools.asm.ClassReader;
6+
import scala.tools.asm.ClassWriter;
7+
import scala.tools.asm.Label;
8+
9+
/*
10+
FieldType_attribute{
11+
u2 attribute_name_index;
12+
u4 attribute_length;
13+
u1 K_M_indicator; (should be always K?)
14+
u2 outer_class_indicator
15+
u2 index;
16+
}
17+
*/
18+
public class FieldType extends Attribute{
19+
private final TypeHints.TypeB typeB;
20+
21+
public FieldType() {
22+
super("FieldType");
23+
this.typeB = TypeHints.TypeB.NO_HINT;
24+
}
25+
26+
public FieldType(TypeHints.TypeB typeB) {
27+
super("FieldType");
28+
this.typeB = typeB;
29+
}
30+
31+
public TypeHints.TypeB getTypeB() {
32+
return typeB;
33+
}
34+
35+
@Override
36+
public boolean isUnknown() {
37+
return false;
38+
}
39+
40+
@Override
41+
@SuppressWarnings("UnusedAssignment")
42+
public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
43+
int cur = off;
44+
byte kind = (byte) cr.readByte(cur);
45+
cur += 1;
46+
int outerClassIndex = cr.readUnsignedShort(cur);
47+
cur += 2;
48+
int index = cr.readUnsignedShort(cur);
49+
cur += 2;
50+
return new FieldType(new TypeHints.TypeB(kind, outerClassIndex, index));
51+
}
52+
53+
@Override
54+
public ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
55+
ByteVector bv = new ByteVector();
56+
bv.putByte(typeB.getKind());
57+
bv.putShort(typeB.getOuterClassIndex());
58+
bv.putShort(typeB.getIndex());
59+
return bv;
60+
}
61+
62+
}

compiler/src/dotty/tools/backend/jvm/attributes/InstructionTypeArguments.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
import scala.tools.asm.ClassWriter;
1010
import scala.tools.asm.Label;
1111

12+
/*
13+
InstructionTypeArguments_attribute {
14+
u2 attribute_name_index;
15+
u4 attribute_length;
16+
u2 typehint_length;
17+
{ u2 byecode_offset
18+
u2 typeA_number
19+
{ u1 K_M_indicator
20+
u2 outer_class_indicator
21+
u2 index
22+
} typeAs[typeA_number]
23+
} typeHints[typehint_length];
24+
}
25+
*/
1226
public class InstructionTypeArguments extends Attribute {
1327
private final List<TypeHints.TypeAHint> typeArguments;
1428
public InstructionTypeArguments() {
@@ -38,9 +52,11 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
3852
for (int j = 0 ; j < typeAnum; j++) {
3953
byte kind = (byte) cr.readByte(cur);
4054
cur += 1;
55+
int outerClassIndex = cr.readUnsignedShort(cur);
56+
cur += 2;
4157
int index = cr.readUnsignedShort(cur);
4258
cur += 2;
43-
typeList.add(new TypeHints.TypeA(kind, index));
59+
typeList.add(new TypeHints.TypeA(kind, outerClassIndex, index));
4460
}
4561
typeHints.add(new TypeHints.TypeAHint(bytecodeOffset, typeList));
4662
}
@@ -56,6 +72,7 @@ public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStac
5672
bv.putShort(typeHint.getTypeList().size());
5773
for (TypeHints.TypeA typeA : typeHint.getTypeList()) {
5874
bv.putByte(typeA.getKind());
75+
bv.putShort(typeA.getOuterClassIndex());
5976
bv.putShort(typeA.getIndex());
6077
}
6178
}

compiler/src/dotty/tools/backend/jvm/attributes/InvokeReturnType.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
import scala.tools.asm.ClassWriter;
1010
import scala.tools.asm.Label;
1111

12+
/*
13+
InvokeReturnType_attribute {
14+
u2 attribute_name_index;
15+
u4 attribute_length;
16+
u2 typehint_length;
17+
{ u2 byecode_offset
18+
u1 K_M_indicator
19+
u2 outer_class_indicator
20+
u2 index
21+
} typeHints[typehint_length];
22+
}
23+
*/
1224
public class InvokeReturnType extends Attribute{
1325
private final int count;
1426
private final List<TypeHints.TypeBHint> typeList;
@@ -46,14 +58,13 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
4658
for (int i = 0; i < typeHintLength; i++){
4759
int bytecodeOffset = cr.readUnsignedShort(cur);
4860
cur += 2;
49-
int typeBnum = cr.readUnsignedShort(cur);
50-
assert typeBnum == 1 : " can only have one typeB";
51-
cur += 2;
5261
byte kind = (byte) cr.readByte(cur);
5362
cur += 1;
63+
int outerClassIndex = cr.readUnsignedShort(cur);
64+
cur += 2;
5465
int index = cr.readUnsignedShort(cur);
5566
cur += 2;
56-
TypeHints.TypeB typeB = new TypeHints.TypeB(kind, index);
67+
TypeHints.TypeB typeB = new TypeHints.TypeB(kind, outerClassIndex, index);
5768
typeHints.add(new TypeHints.TypeBHint(bytecodeOffset, typeB));
5869
}
5970
return new InvokeReturnType(typeHintLength, typeHints);
@@ -65,9 +76,9 @@ public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStac
6576
bv.putShort(count);
6677
for (TypeHints.TypeBHint typeHint : typeList) {
6778
bv.putShort(typeHint.getBytecodeOffset());
68-
bv.putShort(1);
6979
TypeHints.TypeB typeB = typeHint.getTypeB();
7080
bv.putByte(typeB.getKind());
81+
bv.putShort(typeB.getOuterClassIndex());
7182
bv.putShort(typeB.getIndex());
7283
}
7384
return bv;

compiler/src/dotty/tools/backend/jvm/attributes/MethodParameterType.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
import scala.tools.asm.ClassWriter;
1010
import scala.tools.asm.Label;
1111

12+
/*
13+
MethodParameterType_attribute {
14+
u2 attribute_name_index;
15+
u4 attribute_length;
16+
u2 parameter_count;
17+
{ u1 K_M_indicator
18+
u2 outer_class_indicator
19+
u2 index
20+
} typeBs[parameter_count];
21+
}
22+
*/
1223
public class MethodParameterType extends Attribute{
1324
private final int count;
1425
private final List<TypeHints.TypeB> typeList;
@@ -46,6 +57,8 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
4657
for (int i = 0; i < parameterCount; i++) {
4758
byte kind = (byte) cr.readByte(cur);
4859
cur += 1;
60+
int outerClassIndex = cr.readUnsignedShort(cur);
61+
cur += 2;
4962
int index = cr.readUnsignedShort(cur);
5063
cur += 2;
5164
typeBs.add(new TypeHints.TypeB(kind, index));
@@ -59,6 +72,7 @@ public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStac
5972
bv.putShort(count);
6073
for (TypeHints.TypeB typeB : typeList) {
6174
bv.putByte(typeB.getKind());
75+
bv.putShort(typeB.getOuterClassIndex());
6276
bv.putShort(typeB.getIndex());
6377
}
6478
return bv;

compiler/src/dotty/tools/backend/jvm/attributes/MethodReturnType.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
import scala.tools.asm.ClassWriter;
77
import scala.tools.asm.Label;
88

9+
/*
10+
MethodReturnType_attribute{
11+
u2 attribute_name_index;
12+
u4 attribute_length;
13+
u1 K_M_indicator
14+
u2 outer_class_indicator
15+
u2 index
16+
}
17+
*/
918
public class MethodReturnType extends Attribute{
1019
private final TypeHints.TypeB typeB;
1120

@@ -28,19 +37,23 @@ public boolean isUnknown() {
2837
}
2938

3039
@Override
40+
@SuppressWarnings("UnusedAssignment")
3141
public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
3242
int cur = off;
3343
byte kind = (byte) cr.readByte(cur);
3444
cur += 1;
45+
int outerClassIndex = cr.readUnsignedShort(cur);
46+
cur += 2;
3547
int index = cr.readUnsignedShort(cur);
36-
//cur += 2;
37-
return new MethodReturnType(new TypeHints.TypeB(kind, index));
48+
cur += 2;
49+
return new MethodReturnType(new TypeHints.TypeB(kind, outerClassIndex, index));
3850
}
3951

4052
@Override
4153
public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStack, int maxLocals) {
4254
ByteVector bv = new ByteVector();
4355
bv.putByte(typeB.getKind());
56+
bv.putShort(typeB.getOuterClassIndex());
4457
bv.putShort(typeB.getIndex());
4558
return bv;
4659
}

compiler/src/dotty/tools/backend/jvm/attributes/MethodTypeParameterCount.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
import scala.tools.asm.ClassWriter;
77
import scala.tools.asm.Label;
88

9+
/*
10+
MethodTypeParameterCount_attribute {
11+
u2 attribute_name_index;
12+
u4 attribute_length;
13+
u2 count;
14+
}
15+
*/
916
public class MethodTypeParameterCount extends Attribute{
1017
private final int count;
1118
public MethodTypeParameterCount() {

0 commit comments

Comments
 (0)