@@ -27,6 +27,7 @@ public class LuaTranslator {
2727 private final Set <String > usedNames = new HashSet <>(Arrays .asList (
2828 // reserved function names
2929 "print" , "tostring" , "error" ,
30+ "main" , "config" ,
3031 // keywords:
3132 "and" ,
3233 "break" ,
@@ -74,8 +75,10 @@ public LuaVariable initFor(ImVar a) {
7475 @ Override
7576 public LuaFunction initFor (ImFunction a ) {
7677 String name = a .getName ();
77- if (!a .isExtern () && !a .isBj () && !a .isNative ()) {
78+ if (!a .isExtern () && !a .isBj () && !a .isNative () && ! isFixedEntryPoint ( name ) ) {
7879 name = uniqueName (name );
80+ } else if (isFixedEntryPoint (name )) {
81+ usedNames .add (name );
7982 }
8083
8184 LuaFunction lf = LuaAst .LuaFunction (name , LuaAst .LuaParams (), LuaAst .LuaStatements ());
@@ -178,6 +181,7 @@ public LuaCompilationUnit translate() {
178181
179182
180183 normalizeMethodNames ();
184+ normalizeFieldNames ();
181185
182186// NormalizeNames.normalizeNames(prog);
183187
@@ -214,6 +218,10 @@ public LuaCompilationUnit translate() {
214218 return luaModel ;
215219 }
216220
221+ private boolean isFixedEntryPoint (String name ) {
222+ return "main" .equals (name ) || "config" .equals (name );
223+ }
224+
217225 private void collectPredefinedNames () {
218226 for (ImFunction function : prog .getFunctions ()) {
219227 if (function .isBj () || function .isExtern () || function .isNative ()) {
@@ -258,6 +266,42 @@ private void normalizeMethodNames() {
258266 }
259267 }
260268
269+ private void normalizeFieldNames () {
270+ for (ImClass c : prog .getClasses ()) {
271+ Set <String > methodNames = new HashSet <>();
272+ collectMethodNames (c , methodNames , new HashSet <>());
273+ if (methodNames .isEmpty ()) {
274+ continue ;
275+ }
276+ Set <String > reserved = new HashSet <>(methodNames );
277+ for (ImVar field : c .getFields ()) {
278+ if (reserved .contains (field .getName ())) {
279+ String base = field .getName () + "_field" ;
280+ String candidate = base ;
281+ int i = 1 ;
282+ while (reserved .contains (candidate )) {
283+ candidate = base + i ++;
284+ }
285+ field .setName (candidate );
286+ }
287+ reserved .add (field .getName ());
288+ }
289+ }
290+ }
291+
292+ private void collectMethodNames (ImClass c , Set <String > methodNames , Set <ImClass > visited ) {
293+ if (visited .contains (c )) {
294+ return ;
295+ }
296+ visited .add (c );
297+ for (ImMethod method : c .getMethods ()) {
298+ methodNames .add (method .getName ());
299+ }
300+ for (ImClassType sc : c .getSuperClasses ()) {
301+ collectMethodNames (sc .getClassDef (), methodNames , visited );
302+ }
303+ }
304+
261305 private void createStringConcatFunction () {
262306 String [] code = {
263307 "if x then" ,
0 commit comments