1717package struct ThunkNameRegistry {
1818 /// Maps base names such as "swiftjava_Module_Type_method_a_b_c" to the number of times we've seen them.
1919 /// This is used to de-duplicate symbols as we emit them.
20- private var registry : [ ImportedFunc : Int ] = [ : ]
20+ private var registry : [ ImportedFunc : String ] = [ : ]
21+ private var duplicateNames : [ String : Int ] = [ : ]
2122
2223 package init ( ) { }
2324
2425 package mutating func functionThunkName(
2526 module: String , decl: ImportedFunc ,
2627 file: String = #fileID, line: UInt = #line) -> String {
27- let emittedCount = self . registry [ decl, default: 0 ]
28- defer { self . registry [ decl] = emittedCount + 1 }
28+ if let existingName = self . registry [ decl] {
29+ return existingName
30+ }
2931
3032 let params = decl. effectiveParameters ( paramPassingStyle: . swiftThunkSelf)
3133 var paramsPart = " "
@@ -42,10 +44,18 @@ package struct ThunkNameRegistry {
4244 " swiftjava_ \( module) _ \( decl. baseIdentifier) \( paramsPart) "
4345 }
4446
45- if emittedCount == 0 {
46- return name // first occurrence of a name we keep as-is
47- } else {
48- return " \( name) $ \( emittedCount) "
49- }
47+ let emittedCount = self . duplicateNames [ name, default: 0 ]
48+ defer { self . duplicateNames [ name] = emittedCount + 1 }
49+
50+ let deduplicatedName =
51+ if emittedCount == 0 {
52+ name // first occurrence of a name we keep as-is
53+ } else {
54+ " \( name) $ \( emittedCount) "
55+ }
56+
57+ // Store the name we assigned to this specific decl.
58+ self . registry [ decl] = deduplicatedName
59+ return deduplicatedName
5060 }
5161}
0 commit comments