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 @@ -5,6 +5,7 @@
import de.peeeq.wurstscript.ast.ClassDef;
import de.peeeq.wurstscript.ast.ConstructorDef;
import de.peeeq.wurstscript.ast.FunctionDefinition;
import de.peeeq.wurstscript.jassIm.ImAlloc;
import de.peeeq.wurstscript.jassIm.ImBoolVal;
import de.peeeq.wurstscript.jassIm.ImClass;
import de.peeeq.wurstscript.jassIm.ImClassType;
Expand Down Expand Up @@ -96,7 +97,8 @@ public static int removeUnreachableFunctions(ImTranslator trans) {

/**
* The same before the generics are specialised. A generic function calls through its type arguments, so what the
* type arguments in reachable code bind is reachable too: the implementations a type class binding names, and
* type arguments in reachable code, and those a constructed class gives its superclasses, bind is reachable too:
* the implementations a type class binding names, and
* the function which constructs a class given as a type argument (the specialisation of {@code wurstNewInstance}
* calls it).
*
Expand Down Expand Up @@ -191,6 +193,7 @@ private static final class Reachability extends ImFunction.DefaultVisitor {
private final boolean followTypeArguments;
private final Set<ImFunction> reachable;
private final Set<ImClass> constructedClasses = new ReferenceOpenHashSet<>();
private final Set<ImClass> classesWithFollowedSuperclasses = new ReferenceOpenHashSet<>();
private final Set<ImMethod> usedMethods = new ReferenceOpenHashSet<>();
private final ArrayDeque<ImFunction> work = new ArrayDeque<>();
private final Set<ImVar> globals;
Expand Down Expand Up @@ -406,6 +409,31 @@ public void visit(ImTypeArgument argument) {
}
}

@Override
public void visit(ImAlloc alloc) {
super.visit(alloc);
if (followTypeArguments) {
followSuperclassTypeArguments(alloc.getClazz().getClassDef());
}
}

/**
* The type arguments a constructed class gives its superclasses, which are in no body: given
* {@code class StateLoader implements Loader<State>}, the inherited methods of {@code Loader<State>} construct
* {@code State} and dispatch through the bindings of its type argument, as the specialisation makes them do.
*/
private void followSuperclassTypeArguments(ImClass imClass) {
if (!classesWithFollowedSuperclasses.add(imClass)) {
return;
}
for (ImClassType superType : imClass.getSuperClasses()) {
for (ImTypeArgument argument : superType.getTypeArguments()) {
followTypeArgument(argument);
}
followSuperclassTypeArguments(superType.getClassDef());
}
}

/**
* What a type argument binds: the implementations its type class binding names, and the construction of each
* class its type names. The type is a reference, which the visitor does not enter, so the type arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,14 +850,18 @@ private boolean allocatesFromSpecialisedClass(ImClass clazz, GenericTypes generi

/**
* Whether a class which extends this instantiation extends the specialised class
* ({@link #collectSpecialisedSuperclasses}): when its objects come from the specialised class, unless it is an
* interface, which has no code to inherit and whose identity matters only to an {@code instanceof}
* ({@link #collectSpecialisedSuperclasses}): when its objects come from the specialised class. An interface only
* when one of its default methods needs the type argument ({@link #classNeedsIdentity}): a default method is code
* the implementing class inherits, and the erased one cannot dispatch a bound or reach a static for the
* implementing class's instantiation. Otherwise an interface's identity matters only to an {@code instanceof}
* ({@link #needsRuntimeTypeSpecialization}).
*/
private boolean inheritsFromSpecialisedClass(ImClass clazz, GenericTypes generics) {
return !(clazz.attrTrace() instanceof InterfaceDef)
&& (allocatesFromSpecialisedClass(clazz, generics)
|| needsRuntimeTypeSpecialization(clazz, generics, new HashSet<>()));
if (clazz.attrTrace() instanceof InterfaceDef) {
return !generics.containsTypeVariable() && classNeedsIdentity(clazz);
}
return allocatesFromSpecialisedClass(clazz, generics)
|| needsRuntimeTypeSpecialization(clazz, generics, new HashSet<>());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public void moduleGenericRootDispatchIsIdenticalAcrossCompilations() throws IOEx
assertEquals(Files.toString(lua, Charsets.UTF_8), firstLua);
}

/** A bounded interface default, inherited by some implementing classes and overridden by others, emits one script. */
@Test
public void interfaceDefaultWithBoundIsIdenticalAcrossCompilations() throws IOException {
String name = "interfaceDefaultWithBound";
String[] source = GenericsTests.overriddenBoundedDefaultSource();
testNamed(name).testLua(true).luaOnly(false).executeProg().lines(source);
File jass = new File("test-output/DeterministicChecks_" + name + "_no_opts.j");
File lua = new File("test-output/lua/DeterministicChecks_" + name + ".lua");
String firstJass = Files.toString(jass, Charsets.UTF_8);
String firstLua = Files.toString(lua, Charsets.UTF_8);
testNamed(name).testLua(true).luaOnly(false).executeProg().lines(source);
assertEquals(Files.toString(jass, Charsets.UTF_8), firstJass);
assertEquals(Files.toString(lua, Charsets.UTF_8), firstLua);
}

@Test
public void simple() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,44 @@ public void genericConstructionOnFreshlyConstructedReceiver() {
);
}

/**
* wurstNewInstance in an interface's default method constructs each implementing class's type argument, which only
* the implementing class's superclass edge names. The interpreter runs the translated program only.
*/
@Test
public void genericConstructionInAnInterfaceDefaultMethod() {
test().testLua(true).luaOnly(false).executeProgOnlyAfterTransforms().executeProg().lines(
"package MagicFunctions",
" @compilerintrinsic function wurstNewInstance<T:>() returns T",
" return null",
"endpackage",
"",
"package FieldIterationTest",
" import MagicFunctions",
" native testSuccess()",
"",
" class State",
" int value = 7",
"",
" class Other",
" int value = 8",
"",
" interface Loader<T:>",
" function load() returns T",
" return wurstNewInstance<T>()",
"",
" class StateLoader implements Loader<State>",
" class OtherLoader implements Loader<Other>",
"",
" init",
" Loader<State> a = new StateLoader()",
" Loader<Other> b = new OtherLoader()",
" if a.load().value == 7 and b.load().value == 8",
" testSuccess()",
"endpackage"
);
}

/**
* wurstNewInstance in the constructor of a generic class, in an ondestroy, and behind a super call, on Jass and Lua.
* On Lua each reaches a copy specialised for its instantiation (EliminateGenerics.allocatesFromSpecialisedClass).
Expand Down
Loading
Loading