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 @@ -2229,7 +2229,7 @@ private ImMethod specializeMethod(ImMethod m, GenericTypes generics) {
newM.setImplementation(genericNewOnly
? specializeMethodImplementation(m, generics)
: specializeFunction(newM.getImplementation(), generics));
adaptSubmethods(m.getSubMethods(), newM, generics);
adaptSubmethods(m, newM, generics);
return newM;
}

Expand Down Expand Up @@ -2280,18 +2280,76 @@ private ImFunction specializeClassFunction(ImFunction function, ImClass owningCl
return newImplementation;
}

private void adaptSubmethods(List<ImMethod> oldSubMethods, ImMethod newM, GenericTypes generics) {
/**
* The type arguments of the method {@code m} declares itself, which follow those of its class in the arguments
* it is specialised with ({@link #specializeMethod}). An override declares the same number, equal up to
* renaming, so they carry over to it unchanged whatever its class's type arguments are.
*/
private static GenericTypes methodTypeArguments(ImMethod m, GenericTypes generics) {
return generics.drop(m.getMethodClass().getTypeArguments().size());
}

private static GenericTypes concat(GenericTypes classTypeArguments, GenericTypes methodTypeArguments) {
List<ImTypeArgument> typeArguments = new ArrayList<>(classTypeArguments.getTypeArguments());
typeArguments.addAll(methodTypeArguments.getTypeArguments());
return new GenericTypes(typeArguments);
}

/** The methods {@link #specializeOwnTypeArgumentsOnErasedClass} made, by original and the method's own arguments. */
private final Table<ImMethod, GenericTypes, ImMethod> erasedClassMethodSpecialisations = HashBasedTable.create();

/**
* Lua: the method of the erased generic class which {@code method} belongs to, specialised for the type arguments
* {@code method} declares itself and nothing else. The class's own type variables stay as they are, erased as in
* every other method of the class, so the objects of the erased class reach the implementation for these method
* arguments. The overrides below it are specialised for the same method arguments.
*/
private ImMethod specializeOwnTypeArgumentsOnErasedClass(ImMethod method, GenericTypes methodGenerics) {
ImMethod known = erasedClassMethodSpecialisations.get(method, methodGenerics);
if (known != null) {
return known;
}
ImClass erasedClass = method.getMethodClass().getClassDef();
ImFunction function = method.getImplementation();
ImFunction newImplementation = function.copyWithRefs();
prog.getFunctions().add(newImplementation);
recordCopiedTypeVars(function.getTypeVariables(), newImplementation.getTypeVariables());
newImplementation.getTypeVariables().removeAll();
newImplementation.setName(function.getName() + "_specialized");
rewriteGenerics(newImplementation, methodGenerics, function.getTypeVariables());

ImMethod newM = method.copyWithRefs();
erasedClassMethodSpecialisations.put(method, methodGenerics, newM);
newM.setName(method.getName() + "_specialized_" + methodGenerics.makeName());
newM.setImplementation(newImplementation);
newM.setMethodClass(JassIm.ImClassType(erasedClass, JassIm.ImTypeArguments()));
erasedClass.getMethods().add(newM);
collectGenericNewUses(newImplementation);

newM.setSubMethods(new ArrayList<>());
for (ImMethod below : new ArrayList<>(method.getSubMethods())) {
newM.getSubMethods().add(isGenericType(below.getMethodClass())
? specializeOwnTypeArgumentsOnErasedClass(below, methodGenerics)
: specializeMethod(below, methodGenerics));
}
return newM;
}

private void adaptSubmethods(ImMethod m, ImMethod newM, GenericTypes generics) {
List<ImMethod> oldSubMethods = m.getSubMethods();
newM.setSubMethods(new ArrayList<>());
ImClassType newClassT = newM.getMethodClass();
ImClass newMClass = newClassT.getClassDef();
GenericTypes methodGenerics = methodTypeArguments(m, generics);
// Read from a copy: specialising a submethod can specialise its class, which binds that class's methods
// to the roots of the originals (attachSpecializedClassMethods), this list among them.
for (ImMethod subMethod : new ArrayList<>(oldSubMethods)) {
ImClassType subClassT = subMethod.getMethodClass();
ImClass subClass = subClassT.getClassDef();
if (isGenericType(subClassT)) {
if (genericNewOnly
&& subClass.getTypeVariables().size() == generics.getTypeArguments().size()) {
&& subClass.getTypeVariables().size() + methodGenerics.getTypeArguments().size()
== generics.getTypeArguments().size()) {
Comment thread
Frotty marked this conversation as resolved.
Resolved
ImMethod specializedSubMethod = specializeMethod(subMethod, generics);
if (allocatesFromSpecialisedClass(subClass, generics)) {
Comment thread
Frotty marked this conversation as resolved.
Resolved
// The objects of this instantiation come from the specialised class, which
Expand All @@ -2309,17 +2367,24 @@ private void adaptSubmethods(List<ImMethod> oldSubMethods, ImMethod newM, Generi
newM.getSubMethods().add(specializedSubMethod);
continue;
}
if (genericNewOnly && !methodGenerics.getTypeArguments().isEmpty()) {
// The class's type arguments are not known here (a plain interface gives none), but the method's
// own are, and the ordinary objects of the class stay erased: bind the implementation for them
// to the erased class. A specialised instantiation of the class gets its own below.
newM.getSubMethods().add(specializeOwnTypeArgumentsOnErasedClass(subMethod, methodGenerics));
}
onSpecializeClass(subClass, (subGenerics, specializedSubClass) -> {
if (specializedSubClass.isSubclassOf(newMClass)) {
ImMethod specializedSubMethod = specializeMethod(subMethod, subGenerics);
ImMethod specializedSubMethod = specializeMethod(subMethod, concat(subGenerics, methodGenerics));
newM.getSubMethods().add(specializedSubMethod);
}
});
}else {
specializeSuperClasses(subClass);
ImClassType newClassTspecialized = specializeType(newClassT);
if (subClass.isSubclassOf(newClassTspecialized.getClassDef())) {
newM.getSubMethods().add(subMethod);
newM.getSubMethods().add(methodGenerics.getTypeArguments().isEmpty()
? subMethod : specializeMethod(subMethod, methodGenerics));
Comment thread
Frotty marked this conversation as resolved.
Resolved
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,16 @@ public TypeParamDef getTypeParamDef(ImTypeVar tv) {
return typeVariableReverse.get(tv);
}

/** A new type variable for the same source parameter as {@code tv}, for a function which declares it again. */
public ImTypeVar copyTypeVar(ImTypeVar tv) {
ImTypeVar copy = JassIm.ImTypeVar(tv.getName());
TypeParamDef tp = typeVariableReverse.get(tv);
if (tp != null) {
typeVariableReverse.put(copy, tp);
}
return copy;
}

/**
* The signature node standing for one type class requirement.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import de.peeeq.wurstscript.ast.TypeExpr;
import de.peeeq.wurstscript.ast.TypeParamDef;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.attributes.names.DefLink;
import de.peeeq.wurstscript.attributes.names.FuncLink;
import de.peeeq.wurstscript.attributes.names.NameResolution;
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.translation.imtojass.ImAttrType;
import de.peeeq.wurstscript.types.VariableBinding;
Expand Down Expand Up @@ -180,25 +183,42 @@ private ImMethod methodOfItsOwn(ImClass imClass, WurstTypeClass classType, ImMet
* its own ({@code C<T:> extends Base<string>}). So the function calls the inherited one with this, as
* {@code super.m()} does, and the specialisation finds Base's type arguments from the type of this. It takes the
* inherited function's parameters with Base's type variables replaced by those arguments, and is vararg where that
* function is. The translator adds it to the class ({@link ImTranslator#bridgeFunction}).
* function is. Type parameters the implementation declares itself ({@code id<T:>}) are not Base's: the function
* declares a copy of each and passes them to the implementation, so a call through the interface specialises it
* with the call's type arguments, whatever Base's are. The translator adds it to the class
* ({@link ImTranslator#bridgeFunction}).
*/
private ImFunction implementationOfItsOwn(ImClass imClass, WurstTypeClass classType, FuncDef implementation,
ImFunction inherited, @org.eclipse.jdt.annotation.Nullable ClassOrInterface owner) {
List<ImTypeVar> ownerVariables = owner == null
? Collections.emptyList() : translator.getClassFor(owner).getTypeVariables();
List<ImTypeArgument> ownerArguments = owner == null
? Collections.emptyList() : typeArgumentsAsSeenFrom(classType, owner, ownerVariables);
List<ImTypeVar> variables = new java.util.ArrayList<>();
List<ImTypeArgument> substitutes = new java.util.ArrayList<>();
if (owner != null) {
List<ImTypeVar> ownerVariables = translator.getClassFor(owner).getTypeVariables();
variables.addAll(ownerVariables);
substitutes.addAll(typeArgumentsAsSeenFrom(classType, owner, ownerVariables));
}
// The implementation's own type parameters are not the class's: the function declares them again and passes
// them on, as a call through the interface gives them.
ImTypeVars ownVariables = JassIm.ImTypeVars();
ImTypeArguments forwarded = JassIm.ImTypeArguments();
for (ImTypeVar inheritedVariable : inherited.getTypeVariables()) {
ImTypeVar ownVariable = translator.copyTypeVar(inheritedVariable);
ownVariables.add(ownVariable);
variables.add(inheritedVariable);
substitutes.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(ownVariable), Collections.emptyMap()));
forwarded.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(ownVariable), Collections.emptyMap()));
}
ImVar thisVar = JassIm.ImVar(implementation, translator.selfType(imClass), "this", false);
ImVars parameters = JassIm.ImVars(thisVar);
ImExprs arguments = JassIm.ImExprs(JassIm.ImVarAccess(thisVar));
for (ImVar p : inherited.getParameters().subList(1, inherited.getParameters().size())) {
ImVar parameter = JassIm.ImVar(p.getTrace(),
ImAttrType.substituteType(p.getType(), ownerArguments, ownerVariables), p.getName(), false);
ImAttrType.substituteType(p.getType(), substitutes, variables), p.getName(), false);
parameters.add(parameter);
arguments.add(JassIm.ImVarAccess(parameter));
}
ImType returnType = ImAttrType.substituteType(inherited.getReturnType(), ownerArguments, ownerVariables);
ImExpr call = JassIm.ImFunctionCall(implementation, inherited, JassIm.ImTypeArguments(), arguments, false,
ImType returnType = ImAttrType.substituteType(inherited.getReturnType(), substitutes, variables);
ImExpr call = JassIm.ImFunctionCall(implementation, inherited, forwarded, arguments, false,
CallType.NORMAL);
ImStmts body = returnType instanceof ImVoid
? JassIm.ImStmts(call)
Expand All @@ -208,7 +228,7 @@ private ImFunction implementationOfItsOwn(ImClass imClass, WurstTypeClass classT
flags.add(FunctionFlagEnum.IS_VARARG);
}
return JassIm.ImFunction(implementation, imClass.getName() + "_" + implementation.getName(),
JassIm.ImTypeVars(), parameters, returnType, JassIm.ImVars(), body, flags);
ownVariables, parameters, returnType, JassIm.ImVars(), body, flags);
}

/**
Expand Down Expand Up @@ -247,10 +267,17 @@ private List<ImTypeArgument> typeArgumentsAsSeenFrom(WurstTypeClass classType, C

/**
* The default which another interface of the class gives {@code abstractMethod}: a method of an interface which
* is not generic, with the same name and parameter types and a body, the nearest first (the class's own
* interfaces before its superclass's, as in {@link #typeBindingOf}); null if there is none.
* is not generic, with the same name and a body, which could override the abstract method as the class sees it
* ({@link WurstValidator#canOverride}: its own type parameters, parameter and return types), the nearest first
* (the class's own interfaces before its superclass's, as in {@link #typeBindingOf}); null if there is none.
*/
private static @org.eclipse.jdt.annotation.Nullable FuncDef defaultOf(WurstTypeClass classType, FuncDef abstractMethod) {
DefLink bound = NameResolution.matchDefLinkReceiver(
FuncLink.create(abstractMethod, abstractMethod.getParent().attrNearestScope()).withConfigDef(), classType,
abstractMethod, false);
if (!(bound instanceof FuncLink abstractLink)) {
return null;
}
ArrayDeque<WurstTypeClassOrInterface> queue = new ArrayDeque<>();
queue.add(classType);
while (!queue.isEmpty()) {
Expand All @@ -259,7 +286,9 @@ private List<ImTypeArgument> typeArgumentsAsSeenFrom(WurstTypeClass classType, C
for (FuncDef candidate : i.getDef().getMethods()) {
if (candidate != abstractMethod && !candidate.attrHasEmptyBody()
&& candidate.getName().equals(abstractMethod.getName())
&& sameParameterTypes(candidate, abstractMethod)) {
&& WurstValidator.canOverride(
FuncLink.create(candidate, candidate.getParent().attrNearestScope()).withConfigDef(),
abstractLink, false)) {
return candidate;
}
}
Expand All @@ -277,18 +306,6 @@ && sameParameterTypes(candidate, abstractMethod)) {
return null;
}

private static boolean sameParameterTypes(FuncDef a, FuncDef b) {
if (a.getParameters().size() != b.getParameters().size()) {
return false;
}
for (int i = 0; i < a.getParameters().size(); i++) {
if (!a.getParameters().get(i).attrTyp().equalsType(b.getParameters().get(i).attrTyp(), a)) {
return false;
}
}
return true;
}

/**
* The type arguments a class gives this interface where it implements it: itself, through an interface which
* extends this one, or through a superclass (the instances are the subclasses of an implementing class too). The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.ast.Element;
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.translation.imtojass.ImAttrType;
import de.peeeq.wurstscript.types.*;

import java.util.ArrayList;
Expand Down Expand Up @@ -83,8 +84,25 @@ public static void addOverride(
return;
}

ImVars parameters = superMethodIm.getImplementation().getParameters().copy();
ImType rType = superMethodIm.getImplementation().getReturnType();
// The wrapper declares the method's own type parameters again and passes them on, as the bridge of
// InterfaceTranslator.implementationOfItsOwn does: a call through the interface specialises it with them.
ImFunction superImplementation = superMethodIm.getImplementation();
List<ImTypeVar> superTypeVars = superImplementation.getTypeVariables();
ImTypeVars typeVars = JassIm.ImTypeVars();
List<ImTypeArgument> ownTypeArgs = new ArrayList<>();
ImTypeArguments forwardedTypeArgs = JassIm.ImTypeArguments();
for (ImTypeVar superTypeVar : superTypeVars) {
ImTypeVar typeVar = tr.copyTypeVar(superTypeVar);
typeVars.add(typeVar);
ownTypeArgs.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(typeVar), Collections.emptyMap()));
forwardedTypeArgs.add(JassIm.ImTypeArgument(JassIm.ImTypeVarRef(typeVar), Collections.emptyMap()));
}
ImVars parameters = JassIm.ImVars();
for (ImVar p : superImplementation.getParameters()) {
parameters.add(JassIm.ImVar(p.getTrace(), ImAttrType.substituteType(p.getType(), ownTypeArgs, superTypeVars),
p.getName(), false));
}
ImType rType = ImAttrType.substituteType(superImplementation.getReturnType(), ownTypeArgs, superTypeVars);
ImVars locals = JassIm.ImVars();
ImStmts body = JassIm.ImStmts();

Expand All @@ -101,7 +119,7 @@ public static void addOverride(
arguments.add(arg);
}

ImExpr wrappedCall = JassIm.ImFunctionCall(e, subMethod.getImplementation(), JassIm.ImTypeArguments(), arguments, false, CallType.NORMAL);
ImExpr wrappedCall = JassIm.ImFunctionCall(e, subMethod.getImplementation(), forwardedTypeArgs, arguments, false, CallType.NORMAL);
if (rType instanceof ImVoid) {
body.add(wrappedCall);
} else {
Expand All @@ -114,7 +132,7 @@ public static void addOverride(

List<FunctionFlag> flags = Collections.emptyList();

ImFunction implementation = JassIm.ImFunction(e, subMethod.getName() + "_wrapper", JassIm.ImTypeVars(), parameters, rType, locals, body, flags);
ImFunction implementation = JassIm.ImFunction(e, subMethod.getName() + "_wrapper", typeVars, parameters, rType, locals, body, flags);
tr.getImProg().getFunctions().add(implementation);

ImMethod wrapperMethod = JassIm.ImMethod(e, subMethod.getMethodClass(), subMethod.getName() + "_wrapper", implementation, JassIm.ImMethods(), new ArrayList<>(), "", false);
Expand Down
Loading
Loading