@@ -1465,7 +1465,7 @@ SLFunc MapFuncDeclToWrapperFunc (SwiftClassName className, SLImportModules modul
14651465
14661466 GatherFunctionDeclarationGenerics ( funcDecl , genericDeclaration ) ;
14671467
1468- var instanceName = hasInstance ? new SLIdentifier ( GetUniqueNameForInstance ( parms ) ) : null ;
1468+ var instanceName = hasInstance ? new SLIdentifier ( GetUniqueNameForInstance ( usedNames ) ) : null ;
14691469 bool instanceIsAPointer = false ;
14701470 if ( hasInstance )
14711471 usedNames . Add ( instanceName . Name ) ;
@@ -1506,7 +1506,8 @@ SLFunc MapFuncDeclToWrapperFunc (SwiftClassName className, SLImportModules modul
15061506 // To mitigate this, all wrapper functions that return type struct need to have an extra
15071507 // parameter which is a reference to a struct.
15081508 if ( makeInOut ) {
1509- returnName = new SLIdentifier ( GetUniqueNameForReturn ( parms ) ) ;
1509+ returnName = new SLIdentifier ( GetUniqueNameForReturn ( usedNames ) ) ;
1510+ usedNames . Add ( returnName . Name ) ;
15101511
15111512 if ( funcDecl . HasThrows ) {
15121513 SLType slReturn = null ;
@@ -1645,14 +1646,16 @@ SLFunc MapFuncDeclToWrapperFunc (SwiftClassName className, SLImportModules modul
16451646 } else {
16461647 string exceptionCall = "setExceptionNotThrown" ;
16471648
1648- var temp = new SLIdentifier ( GetUniqueNameForFoo ( "temp" , parms ) ) ;
1649+ var temp = new SLIdentifier ( GetUniqueNameForFoo ( "temp" , usedNames ) ) ;
1650+ usedNames . Add ( temp . Name ) ;
16491651 var tempDecl = new SLDeclaration ( true , new SLBinding ( temp , new SLTry ( callSite ) ) , Visibility . None ) ;
16501652 doBlock . Add ( new SLLine ( tempDecl ) ) ;
16511653 doBlock . Add ( SLFunctionCall . FunctionCallLine ( exceptionCall ,
16521654 new SLArgument ( new SLIdentifier ( "value" ) , temp , true ) ,
16531655 new SLArgument ( new SLIdentifier ( "retval" ) , returnName , true ) ) ) ;
16541656 }
1655- var error = new SLIdentifier ( GetUniqueNameForFoo ( "error" , parms ) ) ;
1657+ var error = new SLIdentifier ( GetUniqueNameForFoo ( "error" , usedNames ) ) ;
1658+ usedNames . Add ( error . Name ) ;
16561659 var catcher = new SLCatch ( error . Name , null ) ;
16571660 catcher . Body . Add ( SLFunctionCall . FunctionCallLine ( "setExceptionThrown" ,
16581661 new SLArgument ( new SLIdentifier ( "err" ) , error , true ) ,
@@ -1826,7 +1829,7 @@ SLFunc MapTopLevelFuncToWrapperFunc (SLImportModules modules, FunctionDeclaratio
18261829 // To mitigate this, all wrapper functions that return type struct need to have an extra
18271830 // parameter which is a reference to a struct.
18281831 if ( makeInOut ) {
1829- returnName = new SLIdentifier ( GetUniqueNameForReturn ( parms ) ) ;
1832+ returnName = new SLIdentifier ( GetUniqueNameForReturn ( usedNames ) ) ;
18301833 usedNames . Add ( returnName . Name ) ;
18311834
18321835 if ( funcDecl . HasThrows ) {
@@ -1928,14 +1931,16 @@ SLFunc MapTopLevelFuncToWrapperFunc (SLImportModules modules, FunctionDeclaratio
19281931 } else {
19291932 string exceptionCall = "setExceptionNotThrown" ;
19301933
1931- var temp = new SLIdentifier ( GetUniqueNameForFoo ( "temp" , parms ) ) ;
1934+ var temp = new SLIdentifier ( GetUniqueNameForFoo ( "temp" , usedNames ) ) ;
1935+ usedNames . Add ( temp . Name ) ;
19321936 var tempDecl = new SLDeclaration ( true , new SLBinding ( temp , new SLTry ( callSite ) ) , Visibility . None ) ;
19331937 doBlock . Add ( new SLLine ( tempDecl ) ) ;
19341938 doBlock . Add ( SLFunctionCall . FunctionCallLine ( exceptionCall ,
19351939 new SLArgument ( new SLIdentifier ( "value" ) , temp , true ) ,
19361940 new SLArgument ( new SLIdentifier ( "retval" ) , returnName , true ) ) ) ;
19371941 }
1938- var error = new SLIdentifier ( GetUniqueNameForFoo ( "error" , parms ) ) ;
1942+ var error = new SLIdentifier ( GetUniqueNameForFoo ( "error" , usedNames ) ) ;
1943+ usedNames . Add ( error . Name ) ;
19391944 var catcher = new SLCatch ( error . Name , null ) ;
19401945 catcher . Body . Add ( SLFunctionCall . FunctionCallLine ( "setExceptionThrown" ,
19411946 new SLArgument ( new SLIdentifier ( "err" ) , error , true ) ,
@@ -2119,15 +2124,15 @@ DelegatedCommaListElemCollection<SLArgument> BuildArguments (BaseDeclaration con
21192124 SLImportModules modules , List < ICodeElement > preMarshalCode )
21202125 {
21212126 var retval = new DelegatedCommaListElemCollection < SLArgument > ( SLFunctionCall . WriteElement ) ;
2122- var uniqueNames = new List < SLParameter > ( ) ;
2123- uniqueNames . AddRange ( parms ) ;
2127+ var usedNames = new List < string > ( ) ;
2128+ usedNames . AddRange ( parms . Select ( p => p . PrivateName . Name ) ) ;
21242129 retval . AddRange ( parms . Select ( ( nt , i ) => {
21252130 bool parmNameIsRequired = original [ i ] . NameIsRequired ;
21262131 var originalTypeSpec = original [ i ] . TypeSpec . ReplaceName ( "Self" , substituteForSelf ) ;
21272132 if ( originalTypeSpec is ClosureTypeSpec ) {
21282133 var ct = ( ClosureTypeSpec ) originalTypeSpec ;
2129- var closureName = new SLIdentifier ( GetUniqueNameForFoo ( "clos" , uniqueNames ) ) ;
2130- uniqueNames . Add ( new SLParameter ( closureName , SLSimpleType . Bool ) ) ; // type doesn't matter here
2134+ var closureName = new SLIdentifier ( GetUniqueNameForFoo ( "clos" , usedNames ) ) ;
2135+ usedNames . Add ( closureName . Name ) ;
21312136
21322137 var origArgs = ct . Arguments as TupleTypeSpec ;
21332138 var ids = new List < SLNameTypePair > ( ) ;
@@ -2138,8 +2143,8 @@ DelegatedCommaListElemCollection<SLArgument> BuildArguments (BaseDeclaration con
21382143 }
21392144 var clargTypesAsTuple = ( SLTupleType ) clargTypes ;
21402145 for ( int j = 0 ; j < origArgsCount ; j ++ ) {
2141- SLIdentifier id = new SLIdentifier ( GetUniqueNameForFoo ( "arg" , uniqueNames ) ) ;
2142- uniqueNames . Add ( new SLParameter ( id , SLSimpleType . Bool ) ) ; // type doesn't matter
2146+ var id = new SLIdentifier ( GetUniqueNameForFoo ( "arg" , usedNames ) ) ;
2147+ usedNames . Add ( id . Name ) ;
21432148 ids . Add ( new SLNameTypePair ( id , clargTypesAsTuple . Elements [ j ] . TypeAnnotation ) ) ;
21442149 }
21452150 var clparms = new SLTupleType ( ids ) ;
@@ -2150,13 +2155,13 @@ DelegatedCommaListElemCollection<SLArgument> BuildArguments (BaseDeclaration con
21502155 bool hasArgs = ! ct . Arguments . IsEmptyTuple ;
21512156
21522157
2153- var funcPtrId = new SLIdentifier ( GetUniqueNameForFoo ( nt . PrivateName . Name + "Ptr" , uniqueNames ) ) ;
2158+ var funcPtrId = new SLIdentifier ( GetUniqueNameForFoo ( nt . PrivateName . Name + "Ptr" , usedNames ) ) ;
21542159 var wrappedClosType = nt . TypeAnnotation as SLFuncType ;
21552160 // this strips off the @escaping attribute, if any
21562161 var closTypeNoAttr = new SLFuncType ( wrappedClosType . ReturnType , wrappedClosType . Parameters ) ;
21572162
21582163 SLType funcPtrType = new SLBoundGenericType ( "UnsafeMutablePointer" , closTypeNoAttr ) ;
2159- uniqueNames . Add ( new SLParameter ( funcPtrId , funcPtrType ) ) ;
2164+ usedNames . Add ( funcPtrId . Name ) ;
21602165 var funcPtrBinding = new SLBinding ( funcPtrId ,
21612166 new SLFunctionCall ( $ "{ funcPtrType . ToString ( ) } .allocate",
21622167 false ,
@@ -2172,10 +2177,10 @@ DelegatedCommaListElemCollection<SLArgument> BuildArguments (BaseDeclaration con
21722177 SLIdentifier retvalId = null ;
21732178
21742179 if ( hasReturn ) {
2175- retvalPtrId = new SLIdentifier ( GetUniqueNameForFoo ( "retvalPtr" , uniqueNames ) ) ;
2176- uniqueNames . Add ( new SLParameter ( retvalPtrId , SLSimpleType . Bool ) ) ;
2177- retvalId = new SLIdentifier ( GetUniqueNameForFoo ( "retval" , uniqueNames ) ) ;
2178- uniqueNames . Add ( new SLParameter ( retvalId , SLSimpleType . Bool ) ) ;
2180+ retvalPtrId = new SLIdentifier ( GetUniqueNameForFoo ( "retvalPtr" , usedNames ) ) ;
2181+ usedNames . Add ( retvalPtrId . Name ) ;
2182+ retvalId = new SLIdentifier ( GetUniqueNameForFoo ( "retval" , usedNames ) ) ;
2183+ usedNames . Add ( retvalId . Name ) ;
21792184 var retvalBinding = new SLBinding ( retvalPtrId ,
21802185 new SLFunctionCall (
21812186 $ "UnsafeMutablePointer<{ clretType . ToString ( ) } >.allocate",
@@ -2184,8 +2189,8 @@ DelegatedCommaListElemCollection<SLArgument> BuildArguments (BaseDeclaration con
21842189 SLConstant . Val ( 1 ) , true ) ) ) ;
21852190 closureBody . Add ( new SLDeclaration ( true , retvalBinding , Visibility . None ) ) ;
21862191 }
2187- var argsPtrId = new SLIdentifier ( GetUniqueNameForFoo ( "argsPtr" , uniqueNames ) ) ;
2188- uniqueNames . Add ( new SLParameter ( argsPtrId , SLSimpleType . Bool ) ) ;
2192+ var argsPtrId = new SLIdentifier ( GetUniqueNameForFoo ( "argsPtr" , usedNames ) ) ;
2193+ usedNames . Add ( argsPtrId . Name ) ;
21892194 if ( hasArgs ) {
21902195 var argsBinding = new SLBinding ( argsPtrId ,
21912196 new SLFunctionCall (
@@ -2310,25 +2315,19 @@ public static SLBaseExpr BuildVariadicAdapter (SLBaseExpr callSite, FunctionDecl
23102315 }
23112316
23122317
2313- static string GetUniqueNameForInstance ( List < SLParameter > parms )
2318+ static string GetUniqueNameForInstance ( List < string > usedNames )
23142319 {
2315- return GetUniqueNameForFoo ( "this" , parms ) ;
2320+ return GetUniqueNameForFoo ( "this" , usedNames ) ;
23162321 }
23172322
2318- static string GetUniqueNameForReturn ( List < SLParameter > parms )
2323+ static string GetUniqueNameForReturn ( List < string > usedNames )
23192324 {
2320- return GetUniqueNameForFoo ( "retval" , parms ) ;
2325+ return GetUniqueNameForFoo ( "retval" , usedNames ) ;
23212326 }
23222327
2323- static string GetUniqueNameForFoo ( string foo , List < SLParameter > parms )
2328+ static string GetUniqueNameForFoo ( string foo , List < string > usedNames )
23242329 {
2325- int i = 0 ;
2326- string s = null ;
2327- do {
2328- s = String . Format ( "{0}{1}" , foo , i > 0 ? i . ToString ( ) : "" ) ;
2329- i ++ ;
2330- } while ( parms . Exists ( np => s == ( np . PublicName != null ? np . PublicName . Name : "" ) ) ) ;
2331- return s ;
2330+ return MarshalEngine . Uniqueify ( foo , usedNames ) ;
23322331 }
23332332
23342333 public static bool IsStructOrEnum ( TypeMapper t , ParameterItem item )
0 commit comments