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:
parent
e1a74ce81a
commit
ca3308d959
4 changed files with 99 additions and 95 deletions
|
@ -690,34 +690,12 @@ String BfGNUMangler::Mangle(BfMethodInstance* methodInst)
|
||||||
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);
|
|
||||||
if (linkNameAttr != NULL)
|
|
||||||
{
|
|
||||||
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;
|
name += methodInst->mMethodDef->mName;
|
||||||
|
if (!name.IsEmpty())
|
||||||
return name;
|
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,35 +1796,12 @@ 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);
|
|
||||||
if (linkNameAttr != NULL)
|
|
||||||
{
|
|
||||||
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;
|
name += methodInst->mMethodDef->mName;
|
||||||
|
if (!name.IsEmpty())
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
else if (constant->mInt32 == 2) // CPP
|
|
||||||
{
|
|
||||||
mangleContext.mCPPMangle = true;
|
|
||||||
mangleContext.mCCompat = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
name += '?';
|
name += '?';
|
||||||
|
|
||||||
|
@ -2194,6 +2177,13 @@ void BfMSMangler::Mangle(StringImpl& name, bool is64Bit, BfFieldInstance* fieldI
|
||||||
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);
|
||||||
Mangle(mangleContext, name, fieldInstance->mOwner, true);
|
Mangle(mangleContext, name, fieldInstance->mOwner, true);
|
||||||
|
@ -2267,37 +2257,10 @@ 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
@ -125,6 +127,7 @@ public:
|
||||||
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ namespace Tests
|
||||||
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;
|
||||||
|
|
|
@ -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");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue