Skip to content

Commit 0a99b6b

Browse files
committed
fixed attributes format to new format; added ReifiedValue type; changed signature for reified values to new ReifiedValue type
1 parent ca2d35e commit 0a99b6b

File tree

9 files changed

+49
-60
lines changed

9 files changed

+49
-60
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
u2 attribute_name_index;
1212
u4 attribute_length;
1313
u1 K_M_indicator; (should be always K?)
14-
u2 outer_class_indicator
1514
u2 index;
1615
}
1716
*/
@@ -43,18 +42,15 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
4342
int cur = off;
4443
byte kind = (byte) cr.readByte(cur);
4544
cur += 1;
46-
int outerClassIndex = cr.readUnsignedShort(cur);
47-
cur += 2;
4845
int index = cr.readUnsignedShort(cur);
4946
cur += 2;
50-
return new FieldType(new TypeHints.TypeB(kind, outerClassIndex, index));
47+
return new FieldType(new TypeHints.TypeB(kind, index));
5148
}
5249

5350
@Override
5451
public ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
5552
ByteVector bv = new ByteVector();
5653
bv.putByte(typeB.getKind());
57-
bv.putShort(typeB.getOuterClassIndex());
5854
bv.putShort(typeB.getIndex());
5955
return bv;
6056
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
{ u2 byecode_offset
1818
u2 typeA_number
1919
{ u1 K_M_indicator
20-
u2 outer_class_indicator
2120
u2 index
2221
} typeAs[typeA_number]
2322
} typeHints[typehint_length];
@@ -52,11 +51,9 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
5251
for (int j = 0 ; j < typeAnum; j++) {
5352
byte kind = (byte) cr.readByte(cur);
5453
cur += 1;
55-
int outerClassIndex = cr.readUnsignedShort(cur);
56-
cur += 2;
5754
int index = cr.readUnsignedShort(cur);
5855
cur += 2;
59-
typeList.add(new TypeHints.TypeA(kind, outerClassIndex, index));
56+
typeList.add(new TypeHints.TypeA(kind, index));
6057
}
6158
typeHints.add(new TypeHints.TypeAHint(bytecodeOffset, typeList));
6259
}
@@ -72,7 +69,6 @@ public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStac
7269
bv.putShort(typeHint.getTypeList().size());
7370
for (TypeHints.TypeA typeA : typeHint.getTypeList()) {
7471
bv.putByte(typeA.getKind());
75-
bv.putShort(typeA.getOuterClassIndex());
7672
bv.putShort(typeA.getIndex());
7773
}
7874
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
u2 typehint_length;
1717
{ u2 byecode_offset
1818
u1 K_M_indicator
19-
u2 outer_class_indicator
2019
u2 index
2120
} typeHints[typehint_length];
2221
}
@@ -60,11 +59,9 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
6059
cur += 2;
6160
byte kind = (byte) cr.readByte(cur);
6261
cur += 1;
63-
int outerClassIndex = cr.readUnsignedShort(cur);
64-
cur += 2;
6562
int index = cr.readUnsignedShort(cur);
6663
cur += 2;
67-
TypeHints.TypeB typeB = new TypeHints.TypeB(kind, outerClassIndex, index);
64+
TypeHints.TypeB typeB = new TypeHints.TypeB(kind, index);
6865
typeHints.add(new TypeHints.TypeBHint(bytecodeOffset, typeB));
6966
}
7067
return new InvokeReturnType(typeHintLength, typeHints);
@@ -78,7 +75,6 @@ public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStac
7875
bv.putShort(typeHint.getBytecodeOffset());
7976
TypeHints.TypeB typeB = typeHint.getTypeB();
8077
bv.putByte(typeB.getKind());
81-
bv.putShort(typeB.getOuterClassIndex());
8278
bv.putShort(typeB.getIndex());
8379
}
8480
return bv;

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
u4 attribute_length;
1616
u2 parameter_count;
1717
{ u1 K_M_indicator
18-
u2 outer_class_indicator
1918
u2 index
2019
} typeBs[parameter_count];
2120
}
@@ -57,8 +56,6 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
5756
for (int i = 0; i < parameterCount; i++) {
5857
byte kind = (byte) cr.readByte(cur);
5958
cur += 1;
60-
int outerClassIndex = cr.readUnsignedShort(cur);
61-
cur += 2;
6259
int index = cr.readUnsignedShort(cur);
6360
cur += 2;
6461
typeBs.add(new TypeHints.TypeB(kind, index));
@@ -72,7 +69,6 @@ public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStac
7269
bv.putShort(count);
7370
for (TypeHints.TypeB typeB : typeList) {
7471
bv.putByte(typeB.getKind());
75-
bv.putShort(typeB.getOuterClassIndex());
7672
bv.putShort(typeB.getIndex());
7773
}
7874
return bv;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
u2 attribute_name_index;
1212
u4 attribute_length;
1313
u1 K_M_indicator
14-
u2 outer_class_indicator
1514
u2 index
1615
}
1716
*/
@@ -42,18 +41,15 @@ public Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff,
4241
int cur = off;
4342
byte kind = (byte) cr.readByte(cur);
4443
cur += 1;
45-
int outerClassIndex = cr.readUnsignedShort(cur);
46-
cur += 2;
4744
int index = cr.readUnsignedShort(cur);
4845
cur += 2;
49-
return new MethodReturnType(new TypeHints.TypeB(kind, outerClassIndex, index));
46+
return new MethodReturnType(new TypeHints.TypeB(kind, index));
5047
}
5148

5249
@Override
5350
public ByteVector write(ClassWriter cw, byte[] code, int codeLength, int maxStack, int maxLocals) {
5451
ByteVector bv = new ByteVector();
5552
bv.putByte(typeB.getKind());
56-
bv.putShort(typeB.getOuterClassIndex());
5753
bv.putShort(typeB.getIndex());
5854
return bv;
5955
}

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,19 @@ public static class TypeA {
3838
new TypeA(REFERENCE, 0);
3939

4040
private final byte kind;
41-
private final int outerClassIndex;
4241
private final int index;
4342

4443
public TypeA(byte kind, int index) {
4544
this.kind = kind;
4645
this.index = index;
47-
this.outerClassIndex = 0;
48-
}
49-
50-
public TypeA(byte kind, int outerClassIndex, int index) {
51-
assert kind == K_KIND;
52-
this.kind = kind;
53-
this.outerClassIndex = outerClassIndex;
54-
this.index = index;
5546
}
5647

5748
public byte getKind() { return kind; }
58-
public int getOuterClassIndex() { return outerClassIndex; }
5949
public int getIndex() { return index; }
6050

6151
@Override
6252
public String toString() {
6353
return "TypeA{kind=" + (char) kind +
64-
", outerClassIndex=" + outerClassIndex +
6554
", index=" + index +
6655
'}';
6756
}
@@ -110,24 +99,14 @@ public static class TypeB {
11099
public static final byte ARR_M_KIND = 'm';
111100

112101
private final byte kind;
113-
private final int outerClassIndex;
114102
private final int index;
115103

116104
public TypeB(byte kind, int index) {
117105
this.kind = kind;
118106
this.index = index;
119-
this.outerClassIndex = 0;
120-
}
121-
122-
public TypeB(byte kind, int outerClassIndex, int index) {
123-
assert kind == K_KIND || kind == ARR_K_KIND;
124-
this.kind = kind;
125-
this.outerClassIndex = outerClassIndex;
126-
this.index = index;
127107
}
128108

129109
public byte getKind() { return kind; }
130-
public int getOuterClassIndex() { return outerClassIndex; }
131110
public int getIndex() { return index; }
132111

133112
public boolean isNoHint() {
@@ -140,7 +119,6 @@ public String toString() {
140119
return "TypeB{NO_HINT}";
141120
}
142121
return "TypeB{kind=" + (char) kind +
143-
", outerClassIndex=" + outerClassIndex +
144122
", index=" + index +
145123
'}';
146124
}

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ class Definitions {
332332
newPermanentSymbol(ScalaRuntimePackageClass, "ObjectAny".toTypeName, EmptyFlags, TypeAlias(ObjectType)).entered
333333
def ObjectAnyType : TypeRef = ObjectAnySymbol.typeRef
334334

335+
@tu lazy val ReifiedValueSymbol: TypeSymbol =
336+
newPermanentSymbol(ScalaRuntimePackageClass, "ReifiedValue".toTypeName, EmptyFlags, TypeAlias(ByteType)).entered
337+
def ReifiedValueType : TypeRef = ReifiedValueSymbol.typeRef
338+
335339
/** A type alias of Object used to represent any reference to Object in a Java
336340
* signature, the secret sauce is that subtype checking treats it specially:
337341
*

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

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AddReifiedTypes extends MiniPhase with InfoTransformer {
3030
def addReifiedParams(tp: Type, typeParamNames: List[Name])(using Context): Type = tp match {
3131
case mt: MethodType =>
3232
val newParamNames = typeParamNames.map(name => termName(s"reified_$name"))
33-
val newParamTypes = newParamNames.map(_ => defn.AnyType)
33+
val newParamTypes = newParamNames.map(_ => defn.ReifiedValueType)
3434
mt.derivedLambdaType(
3535
mt.paramNames ++ newParamNames,
3636
mt.paramInfos ++ newParamTypes,
@@ -59,7 +59,7 @@ class AddReifiedTypes extends MiniPhase with InfoTransformer {
5959
sym,
6060
paramName,
6161
Flags.Param,
62-
defn.AnyType
62+
defn.ReifiedValueType,
6363
).asTerm
6464
ValDef(paramSym)
6565
}
@@ -83,21 +83,48 @@ class AddReifiedTypes extends MiniPhase with InfoTransformer {
8383

8484
override def transformApply(tree: Apply)(using Context): Tree = {
8585
val sym = tree.fun.symbol
86-
if (sym.exists && sym.is(Flags.Method) && sym.info.isInstanceOf[PolyType]) {
87-
val reifiedArgs = tree.fun match {
88-
case TypeApply(_, targs) =>
89-
targs.map(targ => createReifiedValue(targ.tpe))
90-
case _ =>
91-
val poly = sym.info.asInstanceOf[PolyType]
92-
poly.paramNames.map(_ => createReifiedValue(defn.AnyType))
86+
if (sym.exists && sym.info.isInstanceOf[PolyType]) {
87+
val poly = sym.info.asInstanceOf[PolyType]
88+
val methodType = poly.resultType match {
89+
case mt: MethodType => mt
90+
case _ => null
91+
}
92+
93+
if (methodType != null) {
94+
val expectedParams = methodType.paramInfos.length
95+
val currentArgs = tree.args.length
96+
97+
if (currentArgs < expectedParams) {
98+
val reifiedArgs = tree.fun match {
99+
case TypeApply(_, targs) =>
100+
targs.map(targ => createReifiedValue(targ.tpe))
101+
case _ =>
102+
poly.paramNames.map(_ => createReifiedValue(defn.ReifiedValueType))
103+
}
104+
return cpy.Apply(tree)(tree.fun, tree.args ++ reifiedArgs)
105+
}
93106
}
94-
cpy.Apply(tree)(tree.fun, tree.args ++ reifiedArgs)
95-
} else {
96-
tree
97107
}
108+
tree
98109
}
99110

100111
def createReifiedValue(tpe: Type)(using Context): Tree = {
101-
Literal(Constant(()))
112+
val value = tpe.widen match
113+
case tpr: TypeParamRef => 'L' //TODO
114+
case tr: TypeRef =>
115+
if tr.isRef(defn.ByteClass) then 'B' else
116+
if tr.isRef(defn.CharClass) then 'C' else
117+
if tr.isRef(defn.DoubleClass) then 'D' else
118+
if tr.isRef(defn.FloatClass) then 'F' else
119+
if tr.isRef(defn.IntClass) then 'I' else
120+
if tr.isRef(defn.LongClass) then 'J' else
121+
if tr.isRef(defn.ShortClass) then 'S' else
122+
if tr.isRef(defn.BooleanClass) then 'Z' else
123+
if tr.isRef(defn.AnyClass) then 'L' else
124+
if tr.symbol.isTypeParam then
125+
'L' //TODO
126+
else
127+
'L' //TODO
128+
Literal(Constant(value.toByte))
102129
}
103130
}

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ trait TypeAssigner {
298298
def assignType(tree: untpd.Apply, fn: Tree, args: List[Tree])(using Context): Apply = {
299299
val ownType = fn.tpe.widen match {
300300
case fntpe: MethodType =>
301-
if fntpe.paramInfos.hasSameLengthAs(args) || ctx.phase.prev.relaxedTyping then
301+
if fntpe.paramInfos.hasSameLengthAs(args) || ctx.phase.prev.relaxedTyping || ctx.phase.prev.phaseName == "addReifiedTypes" then
302302
if fntpe.isResultDependent then safeSubstMethodParams(fntpe, args.tpes)
303303
else fntpe.resultType // fast path optimization
304304
else

0 commit comments

Comments
 (0)