Skip to content

Commit b94a89b

Browse files
committed
Give unique names to exported inlined functions
This way a precise exception is thrown when the birdge tries calling into a wrong version of the library with exported symbols. Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 7d784d2 commit b94a89b

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/Generator/Passes/SymbolsCodeGenerator.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,27 @@ private string GetExporting()
7070
"__declspec(dllexport) " : string.Empty;
7171
}
7272

73-
private string GetWrapper(Module module)
73+
private string GetWrapper(Function function)
7474
{
75-
var symbolsLibraryName = new StringBuilder(module.SymbolsLibraryName);
76-
for (int i = 0; i < symbolsLibraryName.Length; i++)
77-
if (!char.IsLetterOrDigit(symbolsLibraryName[i]))
78-
symbolsLibraryName[i] = '_';
79-
return $"{symbolsLibraryName}{++functionCount}";
75+
if (function is Method method && (method.IsConstructor || method.IsDestructor))
76+
{
77+
var nameBuilder = new StringBuilder(method.USR);
78+
for (int i = 0; i < nameBuilder.Length; i++)
79+
if (!char.IsLetterOrDigit(nameBuilder[i]) &&
80+
nameBuilder[i] != '_')
81+
{
82+
if (nameBuilder[i] == '&')
83+
nameBuilder.Insert(i + 1, '_');
84+
nameBuilder[i] = '_';
85+
}
86+
string @class = function.Namespace.Name;
87+
nameBuilder.Replace("c__S_", string.Empty).Replace(
88+
$"{@class}_F_", $"{@class}_").TrimUnderscores();
89+
if (function.IsOperator)
90+
nameBuilder.Append(function.OperatorKind);
91+
return nameBuilder.ToString();
92+
}
93+
return $"_{functionCount++}";
8094
}
8195

8296
private static string GetDerivedType(string @namespace, string wrapper)
@@ -86,7 +100,7 @@ private static string GetDerivedType(string @namespace, string wrapper)
86100

87101
private void WrapConstructor(Method method)
88102
{
89-
string wrapper = GetWrapper(method.TranslationUnit.Module);
103+
string wrapper = GetWrapper(method);
90104
if (Options.CheckSymbols)
91105
{
92106
method.Mangled = wrapper;
@@ -144,7 +158,7 @@ private string CastIfRVReference(Parameter p)
144158

145159
private void WrapDestructor(Method method)
146160
{
147-
string wrapper = GetWrapper(method.TranslationUnit.Module);
161+
string wrapper = GetWrapper(method);
148162
if (Options.CheckSymbols)
149163
{
150164
method.Mangled = wrapper;
@@ -178,7 +192,7 @@ private void WrapDestructor(Method method)
178192

179193
private void TakeFunctionAddress(Function function)
180194
{
181-
string wrapper = GetWrapper(function.TranslationUnit.Module);
195+
string wrapper = GetWrapper(function);
182196
string @namespace = function.Namespace.Visit(cppTypePrinter);
183197
if (function.Access == AccessSpecifier.Protected)
184198
{

0 commit comments

Comments
 (0)