77import de .peeeq .wurstscript .types .WurstType ;
88import de .peeeq .wurstscript .types .WurstTypeArray ;
99import de .peeeq .wurstscript .types .WurstTypeTuple ;
10- import org .eclipse .jdt .annotation .Nullable ;
1110
1211import java .util .ArrayList ;
1312import java .util .LinkedHashMap ;
1817public final class NamePreservation {
1918
2019 public static final String ANNOTATION = "@preserveName" ;
20+ private static final String SYNTHETIC_MARKER = "__wurst_trve_preserve_name" ;
2121
2222 private NamePreservation () {
2323 }
@@ -42,14 +42,14 @@ public static void preserve(ImFunction function) {
4242 * name-based side table. The marker remains attached to the AST definition and is copied to
4343 * the corresponding IM variable through its trace.
4444 */
45- public static @ Nullable Annotation preserve (GlobalVarDef variable ) {
45+ public static void preserve (GlobalVarDef variable ) {
4646 if (variable .hasAnnotation (ANNOTATION )) {
47- return null ;
47+ return ;
4848 }
4949 Annotation marker = Ast .Annotation (variable .getSource (),
50- Ast .Identifier (variable .getSource (), ANNOTATION .substring (1 )), Ast .Arguments ());
50+ Ast .Identifier (variable .getSource (), ANNOTATION .substring (1 )),
51+ Ast .Arguments (Ast .ExprStringVal (variable .getSource (), SYNTHETIC_MARKER )));
5152 variable .getModifiers ().add (marker );
52- return marker ;
5353 }
5454
5555 /**
@@ -71,6 +71,21 @@ public void visit(GlobalVarDef variable) {
7171 return result ;
7272 }
7373
74+ /** Removes markers synthesized for TRVE during an earlier validation run. */
75+ public static void clearSyntheticMarkers (WurstModel model ) {
76+ model .accept (new Element .DefaultVisitor () {
77+ @ Override
78+ public void visit (GlobalVarDef variable ) {
79+ super .visit (variable );
80+ variable .getModifiers ().removeIf (modifier -> modifier instanceof Annotation annotation
81+ && annotation .getAnnotationType ().equalsIgnoreCase (ANNOTATION )
82+ && annotation .getArgs ().size () == 1
83+ && annotation .getArgs ().get (0 ) instanceof ExprStringVal value
84+ && value .getValS ().equals (SYNTHETIC_MARKER ));
85+ }
86+ });
87+ }
88+
7489 private static void addTupleComponentNames (RuntimeNameIndex index , String name , WurstType type ,
7590 GlobalVarDef variable ) {
7691 if (type instanceof WurstTypeArray array ) {
@@ -88,26 +103,15 @@ private static void addTupleComponentNames(RuntimeNameIndex index, String name,
88103
89104 public static final class RuntimeNameIndex {
90105 private final Map <String , List <GlobalVarDef >> globalsByName = new LinkedHashMap <>();
91- private final Map <GlobalVarDef , Annotation > syntheticMarkers = new LinkedHashMap <>();
92106
93107 private void add (String name , GlobalVarDef variable ) {
94108 globalsByName .computeIfAbsent (name , ignored -> new ArrayList <>()).add (variable );
95109 }
96110
97111 public void preserve (String runtimeName ) {
98112 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 ());
113+ NamePreservation .preserve (variable );
109114 }
110- syntheticMarkers .clear ();
111115 }
112116 }
113117
0 commit comments