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 @@ -35,4 +35,8 @@ public IlConstHandle ConvertOriginFrameType(ILconstInt i) {
return new IlConstHandle("frameType", i.getVal());
}

public IlConstHandle BlzGetFrameByName(ILconstString name, ILconstInt createContext) {
return new IlConstHandle("framehandle", new FrameHandle());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public ILconstObject allocate(ImClassType clazz, Element trace) {
objectIdCounter++;
ILconstObject res = new ILconstObject(clazz, objectIdCounter, trace);
indexToObject.put(objectIdCounter, res);
System.out.println("alloc objId=" + objectIdCounter + " type=" + clazz + " trace=" + trace);
WLogger.trace("alloc objId=" + objectIdCounter + " type=" + clazz + " trace=" + trace);
return res;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.google.common.collect.*;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.ast.PackageOrGlobal;
import de.peeeq.wurstscript.ast.WPackage;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.translation.imtojass.ImAttrType;
import de.peeeq.wurstscript.translation.imtojass.TypeRewriteMatcher;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

import java.util.*;
Expand Down Expand Up @@ -675,6 +678,10 @@ private ImClass specializeClass(ImClass c, GenericTypes generics) {
private void createSpecializedGlobals(ImClass originalClass, GenericTypes generics, List<ImTypeVar> typeVars) {
String key = gKey(generics);

// Collect "insert specialized init right after original init" operations per parent ImStmts
// Using identity maps because IM nodes use identity semantics for parent/ownership.
Map<ImStmts, IdentityHashMap<ImStmt, List<ImStmt>>> insertsByParent = new IdentityHashMap<>();

for (Map.Entry<ImVar, ImClass> entry : globalToClass.entrySet()) {
ImVar originalGlobal = entry.getKey();
ImClass owningClass = entry.getValue();
Expand All @@ -694,17 +701,89 @@ private void createSpecializedGlobals(ImClass originalClass, GenericTypes generi
originalGlobal.getIsBJ()
);

// Create + register global
translator.addGlobal(specializedGlobal);
specializedGlobals.put(originalGlobal, key, specializedGlobal);
dbg("Created specialized global: " + specializedName + " type=" + specializedType);

// If original has init(s), create corresponding specialized init(s) and schedule insertion
List<ImSet> originalInits = prog.getGlobalInits().get(originalGlobal);
if (originalInits != null && !originalInits.isEmpty()) {
ImExpr initRhs = originalInits.getFirst().getRight().copy();
initRhs = specializeNullInitializer(initRhs, specializedType);
translator.addGlobalWithInitalizer(specializedGlobal, initRhs);
} else {
translator.addGlobal(specializedGlobal);

ImSet firstOrig = originalInits.getFirst();
if (!(firstOrig.getParent() instanceof ImStmts parentStmts)) {
throw new CompileError(originalGlobal,
"Initializer for global " + originalGlobal.getName() + " is not inside ImStmts.");
}
// ensure all original init sets share the same parent statement list
for (ImSet s : originalInits) {
if (s.getParent() != parentStmts) {
throw new CompileError(originalGlobal,
"Initializer statements for global " + originalGlobal.getName() + " are not in the same ImStmts.");
}
}

// Helper: rebuild LHS as ImLExpr for specialized global
java.util.function.Function<ImLExpr, ImLExpr> specializeLhs = (ImLExpr lhs) -> {
if (lhs instanceof ImVarAccess va) {
if (va.getVar() == originalGlobal) {
return JassIm.ImVarAccess(specializedGlobal);
}
return (ImLExpr) va.copy();
}
if (lhs instanceof ImVarArrayAccess aa) {
if (aa.getVar() == originalGlobal) {
return JassIm.ImVarArrayAccess(
aa.getTrace(),
specializedGlobal,
aa.getIndexes().copy()
);
}
return (ImLExpr) aa.copy();
}
throw new CompileError(originalGlobal,
"Unsupported initializer LHS for global " + originalGlobal.getName() + ": " + lhs.getClass().getSimpleName());
};

List<ImSet> specializedInitsForMap = new ArrayList<>(originalInits.size());

// Create specialized init sets and schedule: insert each right after its corresponding original init set
for (ImSet origSet : originalInits) {
ImExpr rhs = origSet.getRight().copy();
rhs = specializeNullInitializer(rhs, specializedType);

ImLExpr newLeft = specializeLhs.apply(origSet.getLeft());
ImSet specSet = JassIm.ImSet(originalGlobal.attrTrace(), newLeft, rhs);

// schedule insertion right after origSet in its parent ImStmts
IdentityHashMap<ImStmt, List<ImStmt>> byStmt =
insertsByParent.computeIfAbsent(parentStmts, k -> new IdentityHashMap<>());
byStmt.computeIfAbsent(origSet, k -> new ArrayList<>(1)).add(specSet);

// keep prog.getGlobalInits consistent, but do NOT reuse the tree-attached node elsewhere
specializedInitsForMap.add((ImSet) specSet.copy());
}

prog.getGlobalInits().put(specializedGlobal, specializedInitsForMap);
}
}

specializedGlobals.put(originalGlobal, key, specializedGlobal);
dbg("Created specialized global: " + specializedName + " type=" + specializedType);
// Perform insertions after the loop (so indices/state remain stable during collection)
for (Map.Entry<ImStmts, IdentityHashMap<ImStmt, List<ImStmt>>> e : insertsByParent.entrySet()) {
ImStmts parent = e.getKey();
IdentityHashMap<ImStmt, List<ImStmt>> toInsertAfter = e.getValue();

ListIterator<ImStmt> it = parent.listIterator();
while (it.hasNext()) {
ImStmt curr = it.next();
List<ImStmt> ins = toInsertAfter.get(curr);
if (ins != null) {
// add in order, immediately after the original init
for (ImStmt s : ins) {
it.add(s);
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static de.peeeq.wurstscript.utils.Utils.string;

public class BugTests extends WurstScriptTest {
private static final String TEST_DIR = "./testscripts/concept/";
public static final String TEST_DIR = "./testscripts/concept/";

@Test
public void localsInOndestroy() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

import static tests.wurstscript.tests.BugTests.TEST_DIR;

public class GenericsWithTypeclassesTests extends WurstScriptTest {


Expand Down Expand Up @@ -2013,5 +2018,10 @@ public void genericClassWithStaticMemberArray() {
);
}

@Test
public void fullArrayListTest() throws IOException {
testAssertOkFileWithStdLib(new File(TEST_DIR + "arrayList.wurst"), true);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class StdLib {
/**
* version to use for the tests
*/
private final static String version = "6107c40e64fa646a016e8c446026f2f5cf3f2a1e";
private final static String version = "e6463189f754b8794e59ba9d4ac1a91977c8aaac";

/**
* flag so that initialization in only done once
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,15 @@ protected WurstModel testScript(String name, boolean executeProg, String prog) {
private void testWithInliningAndOptimizations(String name, boolean executeProg, boolean executeTests, WurstGui gui,
WurstCompilerJassImpl compiler, WurstModel model, boolean executeProgOnlyAfterTransforms, RunArgs runArgs) throws Error {
// test with inlining and local optimization
currentTestEnv = "With Inlining and Optimizations";
setCurrentTestEnv("With Inlining and Optimizations");
compiler.setRunArgs(runArgs.with("-inline", "-localOptimizations"));
translateAndTest(name + "_inlopt", executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms);
}

private void testWithInliningAndOptimizationsAndStacktraces(String name, boolean executeProg, boolean executeTests, WurstGui gui,
WurstCompilerJassImpl compiler, WurstModel model, boolean executeProgOnlyAfterTransforms, RunArgs runArgs) throws Error {
// test with inlining and local optimization
currentTestEnv = "With Inlining, Optimizations and Stacktraces";
setCurrentTestEnv("With Inlining, Optimizations and Stacktraces");
compiler.setRunArgs(runArgs.with("-inline", "-localOptimizations", "-stacktraces"));
translateAndTest(name + "_stacktraceinlopt", executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms);
}
Expand All @@ -416,15 +416,15 @@ private void testWithInlining(String name, boolean executeProg, boolean executeT
, WurstCompilerJassImpl compiler, WurstModel model, boolean executeProgOnlyAfterTransforms
, RunArgs runArgs) throws Error {
// test with inlining
currentTestEnv = "With Inlining";
setCurrentTestEnv("With Inlining");
compiler.setRunArgs(runArgs.with("-inline"));
translateAndTest(name + "_inl", executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms);
}

private void testWithLocalOptimizations(String name, boolean executeProg, boolean executeTests, WurstGui gui,
WurstCompilerJassImpl compiler, WurstModel model, boolean executeProgOnlyAfterTransforms, RunArgs runArgs) throws Error {
// test with local optimization
currentTestEnv = "With Local Optimizations";
setCurrentTestEnv("With Local Optimizations");
compiler.setRunArgs(runArgs.with("-localOptimizations"));
translateAndTest(name + "_opt", executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms);
}
Expand All @@ -434,7 +434,7 @@ private void testWithoutInliningAndOptimization(String name, boolean executeProg
throws Error {
compiler.setRunArgs(runArgs);
// test without inlining and optimization
currentTestEnv = "No opts";
setCurrentTestEnv("No opts");
translateAndTest(name + "_no_opts", executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms);
}

Expand Down Expand Up @@ -526,7 +526,7 @@ private void translateAndTest(String name, boolean executeProg,
if (executeProg) {
WLogger.info("Executing imProg before jass transformation");
String currentEnv = currentTestEnv;
currentTestEnv = "ImProg before jass transformation";
setCurrentTestEnv("ImProg before jass transformation");
executeImProg(gui, imProg);
currentTestEnv = currentEnv;
}
Expand All @@ -545,7 +545,7 @@ private void translateAndTest(String name, boolean executeProg,
if (executeProg) {
WLogger.info("Executing imProg after jass transformation");
String currentEnv = currentTestEnv;
currentTestEnv += "-ImProg";
setCurrentTestEnv(currentTestEnv + "-ImProg");
executeImProg(gui, imProg);
currentTestEnv = currentEnv;
}
Expand All @@ -563,7 +563,7 @@ private void translateAndTest(String name, boolean executeProg,

if (executeProg) {
String currentEnv = currentTestEnv;
currentTestEnv += "-JassProg";
setCurrentTestEnv(currentTestEnv + "-JassProg");
executeJassProg(prog);
currentTestEnv = currentEnv;
}
Expand Down Expand Up @@ -613,7 +613,16 @@ private void runPjass(File outputFile) throws Error {
}
}

public static String currentTestEnv = "";
private static String currentTestEnv = "";

public static String getCurrentTestEnv() {
return currentTestEnv;
}

public static void setCurrentTestEnv(String env) {
currentTestEnv = env;
System.out.println("Current test environment: " + currentTestEnv);
}

private void executeImProg(WurstGui gui, ImProg imProg) throws TestFailException {
try {
Expand Down
Loading
Loading