From 82c1125a6803c6792435a67c6c5612a6fc8f797c Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Thu, 21 Nov 2024 16:29:40 -0500 Subject: [PATCH] Default ctor fix with emitted ctors --- IDEHelper/Compiler/BfAst.h | 3 ++- IDEHelper/Compiler/BfModule.cpp | 2 +- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 17 ++++++++++++++ IDEHelper/Tests/src/Comptime.bf | 30 ++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/IDEHelper/Compiler/BfAst.h b/IDEHelper/Compiler/BfAst.h index eec1d0b0..7229c45f 100644 --- a/IDEHelper/Compiler/BfAst.h +++ b/IDEHelper/Compiler/BfAst.h @@ -52,7 +52,8 @@ class BfPassInstance; enum BfProtection : uint8 { - BfProtection_Hidden, + BfProtection_Disable, + BfProtection_Hidden, BfProtection_Private, BfProtection_Internal, BfProtection_Protected, diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index e571c4f4..dd14d598 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -26000,7 +26000,7 @@ void BfModule::CheckOverridenMethod(BfMethodInstance* methodInstance, BfMethodIn auto prevProtection = methodOverriden->mMethodDef->mProtection; 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); BfAstNode* protectionRefNode = NULL; if (auto propertyMethodDeclaration = methodDef->GetPropertyMethodDeclaration()) diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 91ef3306..34a7cb02 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -6238,9 +6238,20 @@ void BfModule::DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance) if (typeInstance->mTypeOptionsIdx >= 0) typeOptions = mSystem->GetTypeOptions(typeInstance->mTypeOptionsIdx); + BfMethodDef* defaultCtor = NULL; + bool hasExplicitCtors = false; + // Generate all methods. Pass 0 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]; // 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)); } + if ((defaultCtor != NULL) && (hasExplicitCtors)) + { + // This can happen if we emit another ctor + defaultCtor->mProtection = BfProtection_Hidden; + } + if (typeInstance == mContext->mBfObjectType) { BF_ASSERT(typeInstance->mInterfaceMethodTable.size() == 0); diff --git a/IDEHelper/Tests/src/Comptime.bf b/IDEHelper/Tests/src/Comptime.bf index 9b3cb52d..da08ce75 100644 --- a/IDEHelper/Tests/src/Comptime.bf +++ b/IDEHelper/Tests/src/Comptime.bf @@ -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] public static void TestBasics() { @@ -613,6 +640,9 @@ namespace Tests public int mA = 123; """> genClass = scope .(); Test.Assert(genClass.mA == 123); + + DefaultCtorTest dct = .(); + Test.Assert(dct.mA == 123); } } }