Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vavr.control.Option;
import org.eclipse.jdt.annotation.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -301,6 +302,7 @@ private static ImExpr wrapImplicitToString(ExprBinary concat, Expr operand, ImEx
ImMethod method = t.getMethodFor(funcDef);
ImTypeArguments typeArguments = getFunctionCallTypeArguments(
t, signature, operand, method.getImplementation().getTypeVariables());
typeArguments.addAll(0, erasedReceiverTypeArguments(operand, translated, funcDef.attrNearestClassOrInterface(), t));
return ImMethodCall(operand, method, typeArguments, translated, ImExprs(), false);
}

Expand Down Expand Up @@ -415,17 +417,17 @@ private static ImExpr translateNameDef(NameRef e, ImTranslator t, ImFunction f)

if (e instanceof AstElementWithIndexes withIndexes) {
if (indexGetOverload != null) {
ImExpr receiver = JassIm.ImMemberAccess(e, implicitParam.imTranslateExpr(t, f), JassIm.ImTypeArguments(), v, JassIm.ImExprs());
ImExpr receiver = memberAccess(e, implicitParam, implicitParam.imTranslateExpr(t, f), v, varDef, JassIm.ImExprs(), t);
ImExpr index = withIndexes.getIndexes().get(0).imTranslateExpr(t, f);
ImFunction calledFunc = t.getFuncFor(indexGetOverload.getDef());
return ImFunctionCall(e, calledFunc, ImTypeArguments(), ImExprs(receiver, index), false, CallType.NORMAL);
}
ImExpr index1 = implicitParam.imTranslateExpr(t, f);
ImExpr index2 = withIndexes.getIndexes().get(0).imTranslateExpr(t, f);
return JassIm.ImMemberAccess(e, index1, JassIm.ImTypeArguments(), v, JassIm.ImExprs(index2));
return memberAccess(e, implicitParam, index1, v, varDef, JassIm.ImExprs(index2), t);
} else {
ImExpr index = implicitParam.imTranslateExpr(t, f);
return JassIm.ImMemberAccess(e, index, JassIm.ImTypeArguments(), v, JassIm.ImExprs());
return memberAccess(e, implicitParam, index, v, varDef, JassIm.ImExprs(), t);
}
} else {
// direct var access
Expand Down Expand Up @@ -456,6 +458,45 @@ private static ImExpr translateNameDef(NameRef e, ImTranslator t, ImFunction f)
}
}

/** The access to {@code field} of {@code receiver}, translated to {@code translatedReceiver}. */
static ImMemberAccess memberAccess(Element trace, Expr receiver, ImExpr translatedReceiver, ImVar field,
VarDef fieldDef, ImExprs indexes, ImTranslator t) {
ImTypeArguments typeArguments = ImTypeArguments(
erasedReceiverTypeArguments(receiver, translatedReceiver, fieldDef.attrNearestClassOrInterface(), t));
return JassIm.ImMemberAccess(trace, translatedReceiver, typeArguments, field, indexes);
}

/**
* The type arguments of {@code owner}, the class of a member reached through {@code receiver}, when the receiver
* is an old-generic value.
* <p>
* EliminateGenerics gives a member access or method call the type arguments of the member's class from the
* receiver's IM type. An old-generic value is erased to {@code any} there, so in {@code h.item.value} with
* {@code h} a {@code Holder<Box<int>>} the type argument of {@code Box} would be gone and the access would reach
* the field of the unspecialised class. The receiver's Wurst type still has it, so the translation supplies the
* arguments; a receiver of any other IM type gets none here and keeps getting them from that type.
*/
static List<ImTypeArgument> erasedReceiverTypeArguments(@Nullable Expr receiver, ImExpr translatedReceiver,
@Nullable StructureDef owner, ImTranslator t) {
if (receiver == null || owner == null || !(translatedReceiver.attrTyp() instanceof ImAnyType)
|| !(receiver.attrTyp().normalize() instanceof WurstTypeClassOrInterface receiverType)) {
return List.of();
}
List<WurstTypeClassOrInterface> types = new ArrayList<>();
types.add(receiverType);
types.addAll(receiverType.transitiveSupertypes());
for (WurstTypeClassOrInterface type : types) {
if (type.getDef() == owner && type.imTranslateType(t) instanceof ImClassType classType) {
List<ImTypeArgument> typeArguments = new ArrayList<>();
for (ImTypeArgument typeArgument : classType.getTypeArguments()) {
typeArguments.add(typeArgument.copy());
}
return typeArguments;
}
}
return List.of();
}

private static ImExpr translateTupleSelection(ImTranslator t, ImFunction f, ExprMemberVar mv) {
ImExpr left = mv.getLeft().imTranslateExpr(t, f);
WParameter tupleParam = (WParameter) mv.attrNameDef();
Expand Down Expand Up @@ -680,6 +721,7 @@ && isCalledOnDynamicRef(e)
ImMethod method = t.getMethodFor((FuncDef) calledFunc);
ImTypeArguments typeArguments = getFunctionCallTypeArguments(
t, e.attrFunctionSignature(), e, method.getImplementation().getTypeVariables());
typeArguments.addAll(0, erasedReceiverTypeArguments(leftExpr, receiver, calledFunc.attrNearestClassOrInterface(), t));
Comment thread
Frotty marked this conversation as resolved.
Resolved
call = ImMethodCall(e, method, typeArguments, receiver, imArgs, false);
} else {
ImFunction calledImFunc = directFunc;
Expand Down Expand Up @@ -909,7 +951,10 @@ public static ImExpr translate(ExprDestroy s, ImTranslator t, ImFunction f) {

public static ImExpr destroyClass(ExprDestroy s, ImTranslator t, ImFunction f, StructureDef classDef) {
ImMethod destroyFunc = t.destroyMethod.getFor(classDef);
return ImMethodCall(s, destroyFunc, ImTypeArguments(), s.getDestroyedObj().imTranslateExpr(t, f), ImExprs(), false);
Expr destroyed = s.getDestroyedObj();
ImExpr receiver = destroyed.imTranslateExpr(t, f);
return ImMethodCall(s, destroyFunc, ImTypeArguments(erasedReceiverTypeArguments(destroyed, receiver, classDef, t)),
receiver, ImExprs(), false);

}

Expand Down Expand Up @@ -1023,11 +1068,11 @@ public static ImLExpr translateLvalue(LExpr e, ImTranslator t, ImFunction f) {
}
ImExpr index1 = implicitParam.imTranslateExpr(t, f);
ImExpr index2 = astElementWithIndexes.getIndexes().get(0).imTranslateExpr(t, f);
return JassIm.ImMemberAccess(e, index1, JassIm.ImTypeArguments(), v, JassIm.ImExprs(index2));
return memberAccess(e, implicitParam, index1, v, varDef, JassIm.ImExprs(index2), t);

} else {
ImExpr index = implicitParam.imTranslateExpr(t, f);
return JassIm.ImMemberAccess(e, index, JassIm.ImTypeArguments(), v, JassIm.ImExprs());
return memberAccess(e, implicitParam, index, v, varDef, JassIm.ImExprs(), t);
}
} else {
// direct var access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ public static ImStmt translate(StmtForFrom s, ImTranslator t, ImFunction f) {
} else {
// store from-expression in variable, so that it is only evaluated once
ImExpr iterationTargetTr = iterationTarget.imTranslateExpr(t, f);
ImVar fromVar = ImVar(s, iterationTargetTr.attrTyp(), "from", false);
// An old-generic target is erased to any; the variable takes the class its Wurst type has, so the
// hasNext and next calls on it get that class's type arguments.
ImType fromType = iterationTargetTr.attrTyp() instanceof ImAnyType
? iterationTarget.attrTyp().imTranslateType(t)
: iterationTargetTr.attrTyp();
ImVar fromVar = ImVar(s, fromType, "from", false);
f.getLocals().add(fromVar);
result.add(ImSet(s, ImVarAccess(fromVar), iterationTargetTr));
fromTarget = JassIm.ImExprs(ImVarAccess(fromVar));
Expand Down Expand Up @@ -387,7 +392,8 @@ public static ImStmt translate(StmtSet s, ImTranslator t, ImFunction f) {
ImExpr receiver;
if (left.attrImplicitParameter() instanceof Expr) {
Expr implicit = (Expr) left.attrImplicitParameter();
receiver = JassIm.ImMemberAccess(left, implicit.imTranslateExpr(t, f), JassIm.ImTypeArguments(), receiverVar, JassIm.ImExprs());
receiver = ExprTranslation.memberAccess(left, implicit, implicit.imTranslateExpr(t, f), receiverVar, varDef,
JassIm.ImExprs(), t);
} else {
receiver = ImVarAccess(receiverVar);
}
Expand Down
Loading
Loading