|
4 | 4 | import de.peeeq.wurstscript.jassIm.ImFunction; |
5 | 5 | import de.peeeq.wurstscript.jassIm.ImVar; |
6 | 6 | import de.peeeq.wurstscript.translation.imtranslation.FunctionFlagEnum; |
| 7 | +import de.peeeq.wurstscript.types.WurstType; |
| 8 | +import de.peeeq.wurstscript.types.WurstTypeArray; |
| 9 | +import de.peeeq.wurstscript.types.WurstTypeTuple; |
| 10 | +import org.eclipse.jdt.annotation.Nullable; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
7 | 16 |
|
8 | 17 | /** Metadata for names which are part of the Warcraft III-facing API. */ |
9 | 18 | public final class NamePreservation { |
@@ -33,29 +42,73 @@ public static void preserve(ImFunction function) { |
33 | 42 | * name-based side table. The marker remains attached to the AST definition and is copied to |
34 | 43 | * the corresponding IM variable through its trace. |
35 | 44 | */ |
36 | | - public static void preserve(GlobalVarDef variable) { |
37 | | - if (!variable.hasAnnotation(ANNOTATION)) { |
38 | | - Annotation marker = Ast.Annotation(variable.getSource(), |
39 | | - Ast.Identifier(variable.getSource(), ANNOTATION.substring(1)), Ast.Arguments()); |
40 | | - variable.getModifiers().add(marker); |
| 45 | + public static @Nullable Annotation preserve(GlobalVarDef variable) { |
| 46 | + if (variable.hasAnnotation(ANNOTATION)) { |
| 47 | + return null; |
41 | 48 | } |
| 49 | + Annotation marker = Ast.Annotation(variable.getSource(), |
| 50 | + Ast.Identifier(variable.getSource(), ANNOTATION.substring(1)), Ast.Arguments()); |
| 51 | + variable.getModifiers().add(marker); |
| 52 | + return marker; |
42 | 53 | } |
43 | 54 |
|
44 | 55 | /** |
45 | | - * Finds globals by their emitted runtime name, without consulting lexical name resolution. |
46 | | - * This is needed for native APIs such as TriggerRegisterVariableEvent whose string argument |
47 | | - * refers to the generated global name rather than a source-level variable access. |
| 56 | + * Resolves globals by their emitted runtime name, without consulting lexical name resolution. |
| 57 | + * The index is scoped to one validation run; the preservation marker itself remains attached to |
| 58 | + * the AST definition and is copied to the corresponding IM variables through their trace. |
48 | 59 | */ |
49 | | - public static void preserveGlobalWithRuntimeName(WurstModel model, String runtimeName) { |
| 60 | + public static RuntimeNameIndex indexGlobals(WurstModel model) { |
| 61 | + RuntimeNameIndex result = new RuntimeNameIndex(); |
50 | 62 | model.accept(new Element.DefaultVisitor() { |
51 | 63 | @Override |
52 | 64 | public void visit(GlobalVarDef variable) { |
53 | 65 | super.visit(variable); |
54 | | - if (runtimeName(variable).equals(runtimeName)) { |
55 | | - preserve(variable); |
56 | | - } |
| 66 | + String name = runtimeName(variable); |
| 67 | + result.add(name, variable); |
| 68 | + addTupleComponentNames(result, name, variable.attrTyp(), variable); |
57 | 69 | } |
58 | 70 | }); |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + private static void addTupleComponentNames(RuntimeNameIndex index, String name, WurstType type, |
| 75 | + GlobalVarDef variable) { |
| 76 | + if (type instanceof WurstTypeArray array) { |
| 77 | + type = array.getBaseType(); |
| 78 | + } |
| 79 | + if (!(type instanceof WurstTypeTuple tuple)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + for (WParameter parameter : tuple.getTupleDef().getParameters()) { |
| 83 | + String componentName = name + "_" + parameter.getName(); |
| 84 | + index.add(componentName, variable); |
| 85 | + addTupleComponentNames(index, componentName, parameter.attrTyp(), variable); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static final class RuntimeNameIndex { |
| 90 | + private final Map<String, List<GlobalVarDef>> globalsByName = new LinkedHashMap<>(); |
| 91 | + private final Map<GlobalVarDef, Annotation> syntheticMarkers = new LinkedHashMap<>(); |
| 92 | + |
| 93 | + private void add(String name, GlobalVarDef variable) { |
| 94 | + globalsByName.computeIfAbsent(name, ignored -> new ArrayList<>()).add(variable); |
| 95 | + } |
| 96 | + |
| 97 | + public void preserve(String runtimeName) { |
| 98 | + for (GlobalVarDef variable : globalsByName.getOrDefault(runtimeName, List.of())) { |
| 99 | + Annotation marker = NamePreservation.preserve(variable); |
| 100 | + if (marker != null) { |
| 101 | + syntheticMarkers.put(variable, marker); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void clearSyntheticMarkers() { |
| 107 | + for (Map.Entry<GlobalVarDef, Annotation> entry : syntheticMarkers.entrySet()) { |
| 108 | + entry.getKey().getModifiers().remove(entry.getValue()); |
| 109 | + } |
| 110 | + syntheticMarkers.clear(); |
| 111 | + } |
59 | 112 | } |
60 | 113 |
|
61 | 114 | private static String runtimeName(GlobalVarDef variable) { |
|
0 commit comments