@@ -20,6 +20,8 @@ void populate_global_c_keywords() {
2020 }
2121}
2222
23+ DeclarationHashMap global_function_identifiers = 0 ;
24+
2325void compile_identifier_base (const String base , String * line ) {
2426 strf (line , "%.*s" , PRINT (base ));
2527
@@ -29,27 +31,53 @@ void compile_identifier_base(const String base, String* line) {
2931}
3032
3133void compile_identifier (const Identifier identifier , String * line ) {
34+ String result = { 0 };
35+
3236 if (!identifier .is_external ) {
3337 if (identifier .parent_scope && identifier .parent_scope -> id == NodeFunctionDeclaration ) {
3438 const Identifier parent_ident = identifier .parent_scope -> FunctionDeclaration .identifier ;
35- compile_identifier (parent_ident , line );
36- strf (line , "__" );
39+ compile_identifier (parent_ident , & result );
40+ strf (& result , "__" );
3741 }
3842
3943 if (identifier .parent_scope && identifier .parent_scope -> id == NodeStructType
4044 && !(identifier .parent_declaration -> id == NodeVariableDeclaration
4145 && !(identifier .parent_declaration -> type -> flags & fType ))) {
4246 const Identifier parent_ident = ((StructType * ) (void * ) identifier .parent_scope )-> parent -> identifier ;
43- compile_identifier (parent_ident , line );
44- strf (line , "__" );
47+ compile_identifier (parent_ident , & result );
48+ strf (& result , "__" );
4549 }
4650 }
4751
4852 // TODO: change `PRINT` macro to `FMT` or `STRFMT`
49- compile_identifier_base (identifier .base , line );
53+ compile_identifier_base (identifier .base , & result );
5054
5155 if (identifier .parent_declaration -> generics .type_arguments_stack .size && !identifier .is_external ) {
52- stringify_generics (line , last (identifier .parent_declaration -> generics .type_arguments_stack ),
56+ stringify_generics (& result , last (identifier .parent_declaration -> generics .type_arguments_stack ),
5357 StringifyAlphaNumeric );
5458 }
59+
60+ if (identifier .function_declaration_counter ) strf (& result , "%u" , identifier .function_declaration_counter );
61+
62+ if (!identifier .function_declaration_counter && identifier .parent_declaration -> id == NodeFunctionDeclaration ) {
63+ const size_t initial_size = result .size ;
64+ unsigned counter = 0 ;
65+ Declaration * * existing_declaration ;
66+
67+ while (((existing_declaration = get (global_function_identifiers , result )))
68+ && * existing_declaration != identifier .parent_declaration ) {
69+ result .size = initial_size ;
70+ strf (& result , "%u" , ++ counter );
71+ }
72+
73+ if (counter ) {
74+ identifier .parent_declaration -> identifier .function_declaration_counter = counter ;
75+ }
76+
77+ if (!existing_declaration ) {
78+ put (& global_function_identifiers , result , identifier .parent_declaration );
79+ }
80+ }
81+
82+ strf (line , "%.*s" , PRINT (result ));
5583}
0 commit comments