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

Default ctor fix with emitted ctors

This commit is contained in:
Brian Fiete 2024-11-21 16:29:40 -05:00
parent 049118bb32
commit 82c1125a68
4 changed files with 50 additions and 2 deletions

View file

@ -52,6 +52,7 @@ class BfPassInstance;
enum BfProtection : uint8 enum BfProtection : uint8
{ {
BfProtection_Disable,
BfProtection_Hidden, BfProtection_Hidden,
BfProtection_Private, BfProtection_Private,
BfProtection_Internal, BfProtection_Internal,

View file

@ -26000,7 +26000,7 @@ void BfModule::CheckOverridenMethod(BfMethodInstance* methodInstance, BfMethodIn
auto prevProtection = methodOverriden->mMethodDef->mProtection; auto prevProtection = methodOverriden->mMethodDef->mProtection;
if ((methodDef->mProtection != prevProtection) && (methodDef->mMethodType != BfMethodType_Dtor)) if ((methodDef->mProtection != prevProtection) && (methodDef->mMethodType != BfMethodType_Dtor))
{ {
const char* protectionNames[] = { "hidden", "private", "internal", "protected", "protected internal", "public" }; const char* protectionNames[] = { "disabled", "hidden", "private", "internal", "protected", "protected internal", "public" };
BF_STATIC_ASSERT(BF_ARRAY_COUNT(protectionNames) == BfProtection_COUNT); BF_STATIC_ASSERT(BF_ARRAY_COUNT(protectionNames) == BfProtection_COUNT);
BfAstNode* protectionRefNode = NULL; BfAstNode* protectionRefNode = NULL;
if (auto propertyMethodDeclaration = methodDef->GetPropertyMethodDeclaration()) if (auto propertyMethodDeclaration = methodDef->GetPropertyMethodDeclaration())

View file

@ -6238,9 +6238,20 @@ void BfModule::DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance)
if (typeInstance->mTypeOptionsIdx >= 0) if (typeInstance->mTypeOptionsIdx >= 0)
typeOptions = mSystem->GetTypeOptions(typeInstance->mTypeOptionsIdx); typeOptions = mSystem->GetTypeOptions(typeInstance->mTypeOptionsIdx);
BfMethodDef* defaultCtor = NULL;
bool hasExplicitCtors = false;
// Generate all methods. Pass 0 // Generate all methods. Pass 0
for (auto methodDef : typeDef->mMethods) for (auto methodDef : typeDef->mMethods)
{ {
if (methodDef->mMethodType == BfMethodType_Ctor)
{
if (methodDef->mMethodDeclaration == NULL)
defaultCtor = methodDef;
else
hasExplicitCtors = true;
}
auto methodInstanceGroup = &typeInstance->mMethodInstanceGroups[methodDef->mIdx]; auto methodInstanceGroup = &typeInstance->mMethodInstanceGroups[methodDef->mIdx];
// Don't set these pointers during resolve pass because they may become invalid if it's just a temporary autocomplete method // Don't set these pointers during resolve pass because they may become invalid if it's just a temporary autocomplete method
@ -6272,6 +6283,12 @@ void BfModule::DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance)
//BF_ASSERT((methodInstanceGroup->mOnDemandKind == BfMethodOnDemandKind_NotSet) || (methodInstanceGroup->mOnDemandKind == BfMethodOnDemandKind_AlwaysInclude)); //BF_ASSERT((methodInstanceGroup->mOnDemandKind == BfMethodOnDemandKind_NotSet) || (methodInstanceGroup->mOnDemandKind == BfMethodOnDemandKind_AlwaysInclude));
} }
if ((defaultCtor != NULL) && (hasExplicitCtors))
{
// This can happen if we emit another ctor
defaultCtor->mProtection = BfProtection_Hidden;
}
if (typeInstance == mContext->mBfObjectType) if (typeInstance == mContext->mBfObjectType)
{ {
BF_ASSERT(typeInstance->mInterfaceMethodTable.size() == 0); BF_ASSERT(typeInstance->mInterfaceMethodTable.size() == 0);

View file

@ -524,6 +524,33 @@ namespace Tests
} }
} }
struct Float3 : this(float x, float y, float z = 0)
{
}
struct Pos3f : Float3
{
[OnCompile(.TypeInit), Comptime]
static void TypeInit()
{
Compiler.EmitTypeBody(typeof(Self),
"""
public this(float x, float y, float z) : base(x, y, z) {}
""");
}
}
struct DefaultCtorTest
{
public int mA;
[OnCompile(.TypeInit), Comptime]
static void InitType()
{
Compiler.EmitTypeBody(typeof(Self), "public this() { mA = 123; }");
}
}
[Test] [Test]
public static void TestBasics() public static void TestBasics()
{ {
@ -613,6 +640,9 @@ namespace Tests
public int mA = 123; public int mA = 123;
"""> genClass = scope .(); """> genClass = scope .();
Test.Assert(genClass.mA == 123); Test.Assert(genClass.mA == 123);
DefaultCtorTest dct = .();
Test.Assert(dct.mA == 123);
} }
} }
} }