handle duplicate function names/overloaded methods

This commit is contained in:
Volker Berlin 2019-07-15 21:16:47 +02:00
parent 815cd2eec9
commit 0f0928f4a8
2 changed files with 17 additions and 2 deletions

View File

@ -27,4 +27,6 @@ class Function {
int typeId = -1; // -1 not init, use an invalid value to fail hard if it used without init int typeId = -1; // -1 not init, use an invalid value to fail hard if it used without init
final StringBuilder output = new StringBuilder(); final StringBuilder output = new StringBuilder();
String name;
} }

View File

@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -70,6 +71,8 @@ public class TextModuleWriter extends ModuleWriter {
private Map<String, Function> functions = new LinkedHashMap<>(); private Map<String, Function> functions = new LinkedHashMap<>();
private final Set<String> functionNames = new HashSet<>();
private int inset; private int inset;
private boolean isImport; private boolean isImport;
@ -234,7 +237,17 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Nonnull @Nonnull
private String normalizeName( FunctionName name ) { private String normalizeName( FunctionName name ) {
return normalizeName( name.fullName ); Function function = getFunction( name );
if( function.name == null ) {
String base;
String str = base = normalizeName( name.fullName );
for( int i = 1; functionNames.contains( str ); i++ ) {
str = base + '.' + i;
}
functionNames.add( str );
function.name = str;
}
return function.name;
} }
/** /**
@ -417,7 +430,7 @@ public class TextModuleWriter extends ModuleWriter {
*/ */
@Override @Override
protected void writeGlobalAccess( boolean load, FunctionName name, AnyType type ) throws IOException { protected void writeGlobalAccess( boolean load, FunctionName name, AnyType type ) throws IOException {
String fullName = normalizeName( name ); String fullName = normalizeName( name.fullName );
if( !globals.contains( fullName ) ) { if( !globals.contains( fullName ) ) {
// declare global variable if not already declared. // declare global variable if not already declared.
output.append( "\n " ); output.append( "\n " );