From ed80a8d88bd28becd5532bc3a43b7e090f025a1d Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Wed, 24 Nov 2021 13:27:39 -0800 Subject: [PATCH] Fixed comptime emission for enums --- IDEHelper/Compiler/BfDefBuilder.cpp | 10 ++++++++-- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 10 ++++++++-- IDEHelper/Tests/src/Reflection2.bf | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/IDEHelper/Compiler/BfDefBuilder.cpp b/IDEHelper/Compiler/BfDefBuilder.cpp index 92e122f8..6dafe761 100644 --- a/IDEHelper/Compiler/BfDefBuilder.cpp +++ b/IDEHelper/Compiler/BfDefBuilder.cpp @@ -1926,6 +1926,7 @@ void BfDefBuilder::FinishTypeDef(bool wantsToString) BfMethodDef* staticMarkMethod = NULL; BfMethodDef* dynamicCastMethod = NULL; BfMethodDef* toStringMethod = NULL; + BfMethodDef* getUnderlyingMethod = NULL; bool needsEqualsMethod = ((mCurTypeDef->mTypeCode == BfTypeCode_Struct) || (mCurTypeDef->mTypeCode == BfTypeCode_Enum)) && (!mCurTypeDef->mIsStatic); BfMethodDef* equalsOpMethod = NULL; BfMethodDef* equalsMethod = NULL; @@ -2061,9 +2062,14 @@ void BfDefBuilder::FinishTypeDef(bool wantsToString) if (method->mName == BF_METHODNAME_DYNAMICCAST) _SetMethod(dynamicCastMethod, method); if (method->mName == BF_METHODNAME_TO_STRING) - _SetMethod(toStringMethod, method); + _SetMethod(toStringMethod, method); } } + else if (method->mMethodType == BfMethodType_PropertyGetter) + { + if (method->mName == BF_METHODNAME_ENUM_GETUNDERLYING) + _SetMethod(getUnderlyingMethod, method); + } else if ((method->mMethodType == BfMethodType_Operator) && (method->mIsStatic) && (method->mParams.size() == 2)) @@ -2247,7 +2253,7 @@ void BfDefBuilder::FinishTypeDef(bool wantsToString) if (toStringMethod != NULL) wantsToString = false; - if ((mCurTypeDef->mTypeCode == BfTypeCode_Enum) && (!isPayloadEnum)) + if ((mCurTypeDef->mTypeCode == BfTypeCode_Enum) && (!isPayloadEnum) && (getUnderlyingMethod == NULL)) { auto methodDef = new BfMethodDef(); mCurTypeDef->mMethods.push_back(methodDef); diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 9fb03ea8..99248138 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -2108,7 +2108,13 @@ void BfModule::UpdateCEEmit(CeEmitContext* ceEmitContext, BfTypeInstance* typeIn typeInstance->mTypeDef->ClearOldMemberSets(); - FinishCEParseContext(refNode, typeInstance, &ceParseContext); + FinishCEParseContext(refNode, typeInstance, &ceParseContext); + + if (typeInstance->mTypeDef->mEmitParent != NULL) + { + // Remove generated fields like the 'underlying type' enum field + typeInstance->mFieldInstances.Resize(typeInstance->mTypeDef->mEmitParent->mFields.mSize); + } } void BfModule::HandleCEAttributes(CeEmitContext* ceEmitContext, BfTypeInstance* typeInstance, BfCustomAttributes* customAttributes, HashSet foundAttributes) @@ -4000,7 +4006,7 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy } if (hadNewMembers) - { + { DoPopulateType(resolvedTypeRef, populateType); return; } diff --git a/IDEHelper/Tests/src/Reflection2.bf b/IDEHelper/Tests/src/Reflection2.bf index 75be6732..c9ab725d 100644 --- a/IDEHelper/Tests/src/Reflection2.bf +++ b/IDEHelper/Tests/src/Reflection2.bf @@ -6,11 +6,25 @@ namespace Tests { class Reflection2 { + public typealias RemovePtr = comptype(RemovePtr(typeof(T))); + + [Comptime] + public static Type RemovePtr(Type type) + { + if (type.IsPointer) + return type.UnderlyingType; + + return type; + } + [Test] public static void TestBasics() { const Type t = typeof(StringView); int fieldCount = t.FieldCount; + + Test.Assert(typeof(RemovePtr) == typeof(int32)); + Test.Assert(typeof(RemovePtr) == typeof(uint32)); } } }