1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 12:02:21 +02:00

Fixed CPP mangling for static fields

This commit is contained in:
Brian Fiete 2020-11-16 05:23:19 -08:00
parent e1a74ce81a
commit ca3308d959
4 changed files with 99 additions and 95 deletions

View file

@ -689,35 +689,13 @@ String BfGNUMangler::Mangle(BfMethodInstance* methodInst)
MangleContext mangleContext; MangleContext mangleContext;
mangleContext.mModule = methodInst->GetOwner()->mModule; mangleContext.mModule = methodInst->GetOwner()->mModule;
if (methodInst->mCallingConvention != BfCallingConvention_Unspecified) if (methodInst->mCallingConvention != BfCallingConvention_Unspecified)
mangleContext.mCCompat = true; mangleContext.mCCompat = true;
auto customAttributes = methodInst->GetCustomAttributes(); bool isCMangle = false;
if (customAttributes != NULL) HandleCustomAttributes(methodInst->GetCustomAttributes(), typeInst->mConstHolder, mangleContext.mModule, name, isCMangle, mangleContext.mCPPMangle);
{ if (isCMangle)
auto linkNameAttr = customAttributes->Get(typeInst->mModule->mCompiler->mLinkNameAttributeTypeDef); name += methodInst->mMethodDef->mName;
if (linkNameAttr != NULL) if (!name.IsEmpty())
{ return name;
if (linkNameAttr->mCtorArgs.size() == 1)
{
if (typeInst->mModule->TryGetConstString(typeInst->mConstHolder, linkNameAttr->mCtorArgs[0], name))
if (!name.IsWhitespace())
return name;
auto constant = typeInst->mConstHolder->GetConstant(linkNameAttr->mCtorArgs[0]);
if (constant != NULL)
{
if (constant->mInt32 == 1) // C
{
name += methodInst->mMethodDef->mName;
return name;
}
else if (constant->mInt32 == 2) // CPP
{
mangleContext.mCPPMangle = true;
}
}
}
}
}
bool mangledMethodIdx = false; bool mangledMethodIdx = false;
bool prefixLen = false; bool prefixLen = false;
@ -1024,13 +1002,14 @@ String BfGNUMangler::MangleMethodName(BfTypeInstance* type, const StringImpl& me
String BfGNUMangler::MangleStaticFieldName(BfTypeInstance* type, const StringImpl& fieldName) String BfGNUMangler::MangleStaticFieldName(BfTypeInstance* type, const StringImpl& fieldName)
{ {
MangleContext mangleContext;
mangleContext.mModule = type->mModule;
auto typeInst = type->ToTypeInstance(); auto typeInst = type->ToTypeInstance();
auto typeDef = typeInst->mTypeDef; auto typeDef = typeInst->mTypeDef;
if ((typeDef->IsGlobalsContainer()) && (typeDef->mNamespace.IsEmpty())) if ((typeDef->IsGlobalsContainer()) && (typeDef->mNamespace.IsEmpty()))
return fieldName; return fieldName;
MangleContext mangleContext;
mangleContext.mModule = type->mModule;
StringT<256> name = "_Z"; StringT<256> name = "_Z";
bool isNameOpen; bool isNameOpen;
MangleTypeInst(mangleContext, name, typeInst, NULL, &isNameOpen); MangleTypeInst(mangleContext, name, typeInst, NULL, &isNameOpen);
@ -1040,6 +1019,33 @@ String BfGNUMangler::MangleStaticFieldName(BfTypeInstance* type, const StringImp
return name; return name;
} }
String BfGNUMangler::Mangle(BfFieldInstance* fieldInstance)
{
StringT<256> name;
MangleContext mangleContext;
mangleContext.mModule = fieldInstance->mOwner->mModule;
bool isCMangle = false;
HandleCustomAttributes(fieldInstance->mCustomAttributes, fieldInstance->mOwner->mConstHolder, mangleContext.mModule, name, isCMangle, mangleContext.mCPPMangle);
if (isCMangle)
name += fieldInstance->GetFieldDef()->mName;
if (!name.IsEmpty())
return name;
auto typeInst = fieldInstance->mOwner->ToTypeInstance();
auto typeDef = typeInst->mTypeDef;
if ((typeDef->IsGlobalsContainer()) && (typeDef->mNamespace.IsEmpty()))
return fieldInstance->GetFieldDef()->mName;
name += "_Z";
bool isNameOpen;
MangleTypeInst(mangleContext, name, typeInst, NULL, &isNameOpen);
AddSizedString(name, fieldInstance->GetFieldDef()->mName);
if (isNameOpen)
name += "E";
return name;
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
BfModule* BfMSMangler::MangleContext::GetUnreifiedModule() BfModule* BfMSMangler::MangleContext::GetUnreifiedModule()
@ -1790,36 +1796,13 @@ void BfMSMangler::Mangle(StringImpl& name, bool is64Bit, BfMethodInstance* metho
mangleContext.mModule = methodInst->GetOwner()->mModule; mangleContext.mModule = methodInst->GetOwner()->mModule;
if (methodInst->mCallingConvention != BfCallingConvention_Unspecified) if (methodInst->mCallingConvention != BfCallingConvention_Unspecified)
mangleContext.mCCompat = true; mangleContext.mCCompat = true;
auto customAttributes = methodInst->GetCustomAttributes(); bool isCMangle = false;
if (customAttributes != NULL) HandleCustomAttributes(methodInst->GetCustomAttributes(), typeInst->mConstHolder, mangleContext.mModule, name, isCMangle, mangleContext.mCPPMangle);
{ if (isCMangle)
auto linkNameAttr = customAttributes->Get(typeInst->mModule->mCompiler->mLinkNameAttributeTypeDef); name += methodInst->mMethodDef->mName;
if (linkNameAttr != NULL) if (!name.IsEmpty())
{ return;
if (linkNameAttr->mCtorArgs.size() == 1)
{
if (typeInst->mModule->TryGetConstString(typeInst->mConstHolder, linkNameAttr->mCtorArgs[0], name))
if (!name.IsWhitespace())
return;
auto constant = typeInst->mConstHolder->GetConstant(linkNameAttr->mCtorArgs[0]);
if (constant != NULL)
{
if (constant->mInt32 == 1) // C
{
name += methodInst->mMethodDef->mName;
return;
}
else if (constant->mInt32 == 2) // CPP
{
mangleContext.mCPPMangle = true;
mangleContext.mCCompat = true;
}
}
}
}
}
name += '?'; name += '?';
if (methodInst->GetNumGenericArguments() != 0) if (methodInst->GetNumGenericArguments() != 0)
@ -2193,6 +2176,13 @@ void BfMSMangler::Mangle(StringImpl& name, bool is64Bit, BfFieldInstance* fieldI
MangleContext mangleContext; MangleContext mangleContext;
mangleContext.mIs64Bit = is64Bit; mangleContext.mIs64Bit = is64Bit;
mangleContext.mModule = fieldInstance->mOwner->mModule; mangleContext.mModule = fieldInstance->mOwner->mModule;
bool isCMangle = false;
HandleCustomAttributes(fieldInstance->mCustomAttributes, fieldInstance->mOwner->mConstHolder, mangleContext.mModule, name, isCMangle, mangleContext.mCPPMangle);
if (isCMangle)
name += fieldInstance->GetFieldDef()->mName;
if (!name.IsEmpty())
return;
name += '?'; name += '?';
AddStr(mangleContext, name, fieldDef->mName); AddStr(mangleContext, name, fieldDef->mName);
@ -2266,38 +2256,11 @@ void BfMangler::Mangle(StringImpl& outStr, MangleKind mangleKind, BfMethodInstan
} }
void BfMangler::Mangle(StringImpl& outStr, MangleKind mangleKind, BfFieldInstance* fieldInstance) void BfMangler::Mangle(StringImpl& outStr, MangleKind mangleKind, BfFieldInstance* fieldInstance)
{ {
if (fieldInstance->mCustomAttributes != NULL)
{
auto module = fieldInstance->mOwner->mModule;
auto linkNameAttr = fieldInstance->mCustomAttributes->Get(module->mCompiler->mLinkNameAttributeTypeDef);
if (linkNameAttr != NULL)
{
if (linkNameAttr->mCtorArgs.size() == 1)
{
if (module->TryGetConstString(fieldInstance->mOwner->mConstHolder, linkNameAttr->mCtorArgs[0], outStr))
if (!outStr.IsWhitespace())
return;
auto constant = fieldInstance->mOwner->mConstHolder->GetConstant(linkNameAttr->mCtorArgs[0]);
if (constant != NULL)
{
if (constant->mInt32 == 1) // C
{
outStr += fieldInstance->GetFieldDef()->mName;
return;
}
else if (constant->mInt32 == 2) // CPP
{
//mangleContext.mCPPMangle = true;
}
}
}
}
}
if (mangleKind == BfMangler::MangleKind_GNU) if (mangleKind == BfMangler::MangleKind_GNU)
outStr += BfGNUMangler::MangleStaticFieldName(fieldInstance->mOwner, fieldInstance->GetFieldDef()->mName); {
outStr += BfGNUMangler::Mangle(fieldInstance);
}
else else
BfMSMangler::Mangle(outStr, mangleKind == BfMangler::MangleKind_Microsoft_64, fieldInstance); BfMSMangler::Mangle(outStr, mangleKind == BfMangler::MangleKind_Microsoft_64, fieldInstance);
} }
@ -2318,3 +2281,33 @@ void BfMangler::MangleStaticFieldName(StringImpl& outStr, MangleKind mangleKind,
BfMSMangler::MangleStaticFieldName(outStr, mangleKind == BfMangler::MangleKind_Microsoft_64, type, fieldName, fieldType); BfMSMangler::MangleStaticFieldName(outStr, mangleKind == BfMangler::MangleKind_Microsoft_64, type, fieldName, fieldType);
} }
void BfMangler::HandleCustomAttributes(BfCustomAttributes* customAttributes, BfIRConstHolder* constHolder, BfModule* module, StringImpl& name, bool& isCMangle, bool& isCPPMangle)
{
if (customAttributes == NULL)
return;
auto linkNameAttr = customAttributes->Get(module->mCompiler->mLinkNameAttributeTypeDef);
if (linkNameAttr != NULL)
{
if (linkNameAttr->mCtorArgs.size() == 1)
{
if (module->TryGetConstString(constHolder, linkNameAttr->mCtorArgs[0], name))
if (!name.IsWhitespace())
return;
auto constant = constHolder->GetConstant(linkNameAttr->mCtorArgs[0]);
if (constant != NULL)
{
if (constant->mInt32 == 1) // C
{
isCMangle = true;
}
else if (constant->mInt32 == 2) // CPP
{
isCPPMangle = true;
}
}
}
}
}

View file

@ -8,6 +8,7 @@ NS_BF_BEGIN
class BfType; class BfType;
class BfFieldInstance; class BfFieldInstance;
class BfCustomAttributes;
class BfMangler class BfMangler
{ {
@ -83,6 +84,7 @@ public:
static void Mangle(StringImpl& outStr, MangleKind mangleKind, BfFieldInstance* fieldInstance); static void Mangle(StringImpl& outStr, MangleKind mangleKind, BfFieldInstance* fieldInstance);
static void MangleMethodName(StringImpl& outStr, MangleKind mangleKind, BfTypeInstance* type, const StringImpl& methodName); static void MangleMethodName(StringImpl& outStr, MangleKind mangleKind, BfTypeInstance* type, const StringImpl& methodName);
static void MangleStaticFieldName(StringImpl& outStr, MangleKind mangleKind, BfTypeInstance* owner, const StringImpl& fieldName, BfType* fieldType = NULL); static void MangleStaticFieldName(StringImpl& outStr, MangleKind mangleKind, BfTypeInstance* owner, const StringImpl& fieldName, BfType* fieldType = NULL);
static void HandleCustomAttributes(BfCustomAttributes* customAttributes, BfIRConstHolder* constHolder, BfModule* module, StringImpl& name, bool& isCMangle, bool& isCPPMangle);
}; };
class BfGNUMangler : public BfMangler class BfGNUMangler : public BfMangler
@ -123,8 +125,9 @@ public:
static void Mangle(MangleContext& mangleContext, StringImpl& name, BfType* type, BfType* postfixType = NULL); static void Mangle(MangleContext& mangleContext, StringImpl& name, BfType* type, BfType* postfixType = NULL);
static String Mangle(BfType* type, BfModule* module = NULL); static String Mangle(BfType* type, BfModule* module = NULL);
static String Mangle(BfMethodInstance* methodRef); static String Mangle(BfMethodInstance* methodRef);
static String MangleMethodName(BfTypeInstance* type, const StringImpl& methodName); static String MangleMethodName(BfTypeInstance* type, const StringImpl& methodName);
static String Mangle(BfFieldInstance* methodRef);
static String MangleStaticFieldName(BfTypeInstance* type, const StringImpl& fieldName); static String MangleStaticFieldName(BfTypeInstance* type, const StringImpl& fieldName);
}; };

View file

@ -2,12 +2,13 @@
#include <stdio.h> #include <stdio.h>
namespace Tests namespace Tests
{ {
struct Interop struct Interop
{ {
struct StructA struct StructA
{ {
int mA; int mA;
static int sVal;
int MethodA0(int arg0) int MethodA0(int arg0)
{ {
@ -287,6 +288,8 @@ namespace Tests
float mX; float mX;
}; };
}; };
int Interop::StructA::sVal = 1234;
} }
using namespace Tests; using namespace Tests;

View file

@ -9,6 +9,9 @@ namespace Tests
{ {
public int32 mA; public int32 mA;
[LinkName(.CPP)]
public static extern int32 sVal;
[LinkName(.CPP)] [LinkName(.CPP)]
public extern int32 MethodA0(int32 arg0) mut; public extern int32 MethodA0(int32 arg0) mut;
[LinkName(.CPP)] [LinkName(.CPP)]
@ -387,6 +390,8 @@ namespace Tests
//Console.WriteLine(str); //Console.WriteLine(str);
} }
Test.Assert(StructA.sVal == 1234);
StartTest("Func0"); StartTest("Func0");
Test.Assert(Func0(12, 34) == 3412); Test.Assert(Func0(12, 34) == 3412);
StartTest("Func0K"); StartTest("Func0K");