From e6a918600bb9671cb6ad6de29f294fac3b29713f Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Tue, 9 Feb 2021 16:24:02 -0800 Subject: [PATCH] Fixed string literal issues with import --- IDEHelper/Compiler/BfModule.cpp | 70 ++++++++++++++++--------------- IDEHelper/Tests/src/Reflection.bf | 66 ++++++++++++++--------------- 2 files changed, 70 insertions(+), 66 deletions(-) diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index fd7dd99e..0f294eb0 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -5969,11 +5969,6 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin // Fields BfType* reflectFieldDataType = ResolveTypeDef(mCompiler->mReflectFieldDataDef); BfIRValue emptyValueType = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance()->mBaseType), SizedArray()); - - if (typeInstance->mTypeDef->mName->ToString() == "TestProgram") - { - NOP; - } auto _HandleCustomAttrs = [&](BfCustomAttributes* customAttributes) { @@ -11001,11 +10996,6 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri continue; } - if (mModuleName == "BeefTest_TestProgram") - { - NOP; - } - if ((mIsReified) && (attrTypeInst->mAttributeData != NULL) && ((attrTypeInst->mAttributeData->mFlags & BfAttributeFlag_ReflectAttribute) != 0)) { // Reify attribute @@ -18376,7 +18366,36 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup) } BfLogSysM("ProcessMethod %p Unspecialized: %d Module: %p IRFunction: %d Reified: %d Incomplete:%d\n", methodInstance, mCurTypeInstance->IsUnspecializedType(), this, methodInstance->mIRFunction.mId, methodInstance->mIsReified, mIncompleteMethodCount); - + + int importStrNum = -1; + auto importKind = methodInstance->GetImportKind(); + if ((!mCompiler->mIsResolveOnly) && + ((importKind == BfImportKind_Import_Static) || (importKind == BfImportKind_Import_Dynamic))) + { + if (auto customAttr = methodInstance->GetCustomAttributes()->Get(mCompiler->mImportAttributeTypeDef)) + { + if (customAttr->mCtorArgs.size() == 1) + { + auto fileNameArg = customAttr->mCtorArgs[0]; + auto constant = mCurTypeInstance->mConstHolder->GetConstant(fileNameArg); + if (constant != NULL) + { + if (!constant->IsNull()) + importStrNum = constant->mInt32; + } + else + { + importStrNum = GetStringPoolIdx(fileNameArg, mCurTypeInstance->mConstHolder); + } + if (importStrNum != -1) + { + if (!mStringPoolRefs.Contains(importStrNum)) + mStringPoolRefs.Add(importStrNum); + } + } + } + } + if (methodInstance->GetImportCallKind() != BfImportCallKind_None) { if (mBfIRBuilder->mIgnoreWrites) @@ -19558,34 +19577,19 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup) } bool isDllImport = false; - if (methodInstance->GetImportKind() == BfImportKind_Import_Static) + if ((importKind == BfImportKind_Import_Static) || (importKind == BfImportKind_Import_Dynamic)) { - for (auto customAttr : methodInstance->GetCustomAttributes()->mAttributes) - { - if (customAttr.mType->mTypeDef->mFullName.ToString() == "System.ImportAttribute") + if (importStrNum != -1) + { + if (importKind == BfImportKind_Import_Static) { - if (customAttr.mCtorArgs.size() == 1) + if (!mImportFileNames.Contains(importStrNum)) { - auto fileNameArg = customAttr.mCtorArgs[0]; - int strNum = 0; - auto constant = mCurTypeInstance->mConstHolder->GetConstant(fileNameArg); - if (constant != NULL) - { - if (constant->IsNull()) - continue; // Invalid - strNum = constant->mInt32; - } - else - { - strNum = GetStringPoolIdx(fileNameArg, mCurTypeInstance->mConstHolder); - } - if (!mImportFileNames.Contains(strNum)) - mImportFileNames.Add(strNum); + mImportFileNames.Add(importStrNum); } } + } - - //mImportFileNames } else if (methodInstance->GetImportKind() == BfImportKind_Import_Dynamic) { diff --git a/IDEHelper/Tests/src/Reflection.bf b/IDEHelper/Tests/src/Reflection.bf index 85ccaa1a..89dcc0f1 100644 --- a/IDEHelper/Tests/src/Reflection.bf +++ b/IDEHelper/Tests/src/Reflection.bf @@ -239,6 +239,32 @@ namespace Tests switch (methodIdx) { case 0: + Test.Assert(methodInfo.Name == "__BfCtor"); + Test.Assert(methodInfo.IsConstructor); + case 1: + Test.Assert(methodInfo.Name == "__BfStaticCtor"); + Test.Assert(methodInfo.IsConstructor); + case 2: + Test.Assert(methodInfo.Name == "GetA"); + var result = methodInfo.Invoke(ca, 123).Get(); + Test.Assert(result.Get() == 1123); + result.Dispose(); + result = methodInfo.Invoke(ca2, 123).Get(); + Test.Assert(result.Get() == 2123); + result.Dispose(); + result = methodInfo.Invoke(.Create(ca2), .Create(123)).Get(); + Test.Assert(result.Get() == 2123); + result.Dispose(); + case 3: + Test.Assert(methodInfo.Name == "MemberMethodA"); + var result = methodInfo.Invoke(ca, 100, (int32)20, 3.0f).Get(); + Test.Assert(result.Get() == 123); + result.Dispose(); + + result = methodInfo.Invoke(.Create(ca), .Create(100), .Create((int32)20), .Create(3.0f)).Get(); + Test.Assert(result.Get() == 123); + result.Dispose(); + case 4: StructA sa = .() { mA = 1, mB = 2 }; Test.Assert(methodInfo.Name == "StaticMethodA"); @@ -291,7 +317,7 @@ namespace Tests let attrC = methodInfo.GetCustomAttribute().Get(); Test.Assert(attrC.mA == 71); Test.Assert(attrC.mB == 72); - case 1: + case 5: Test.Assert(methodInfo.Name == "StaticMethodB"); var fieldA = typeInfo.GetField("mA").Value; @@ -337,33 +363,6 @@ namespace Tests res.Dispose(); fieldSAV.Dispose(); fieldSStrV.Dispose(); - - case 2: - Test.Assert(methodInfo.Name == "MemberMethodA"); - var result = methodInfo.Invoke(ca, 100, (int32)20, 3.0f).Get(); - Test.Assert(result.Get() == 123); - result.Dispose(); - - result = methodInfo.Invoke(.Create(ca), .Create(100), .Create((int32)20), .Create(3.0f)).Get(); - Test.Assert(result.Get() == 123); - result.Dispose(); - case 3: - Test.Assert(methodInfo.Name == "GetA"); - var result = methodInfo.Invoke(ca, 123).Get(); - Test.Assert(result.Get() == 1123); - result.Dispose(); - result = methodInfo.Invoke(ca2, 123).Get(); - Test.Assert(result.Get() == 2123); - result.Dispose(); - result = methodInfo.Invoke(.Create(ca2), .Create(123)).Get(); - Test.Assert(result.Get() == 2123); - result.Dispose(); - case 4: - Test.Assert(methodInfo.Name == "__BfStaticCtor"); - Test.Assert(methodInfo.IsConstructor); - case 5: - Test.Assert(methodInfo.Name == "__BfCtor"); - Test.Assert(methodInfo.IsConstructor); case 6: Test.FatalError(); // Shouldn't have any more } @@ -445,6 +444,8 @@ namespace Tests switch (methodIdx) { case 0: + Test.Assert(methodInfo.Name == "__BfCtor"); + case 1: Test.Assert(methodInfo.Name == "GetA"); var result = methodInfo.Invoke(sa, 34).Get(); @@ -464,7 +465,7 @@ namespace Tests result = methodInfo.Invoke(.Create(&sa), .Create(34)); Test.Assert(result.Get() == 1234); result.Dispose(); - case 1: + case 2: Test.Assert(methodInfo.Name == "GetB"); var result = methodInfo.Invoke(sa, 34).Get(); @@ -489,16 +490,15 @@ namespace Tests Test.Assert(sa.mB == 91); result.Dispose(); - case 2: - Test.Assert(methodInfo.Name == "MethodA0"); case 3: - Test.Assert(methodInfo.Name == "MethodA1"); + Test.Assert(methodInfo.Name == "MethodA0"); case 4: - Test.Assert(methodInfo.Name == "__BfCtor"); + Test.Assert(methodInfo.Name == "MethodA1"); case 5: Test.Assert(methodInfo.Name == "__Equals"); case 6: Test.Assert(methodInfo.Name == "__StrictEquals"); + default: Test.FatalError(); // Shouldn't have any more }