diff --git a/IDEHelper/Compiler/BfConstResolver.cpp b/IDEHelper/Compiler/BfConstResolver.cpp index 727bbdda..64e24e7d 100644 --- a/IDEHelper/Compiler/BfConstResolver.cpp +++ b/IDEHelper/Compiler/BfConstResolver.cpp @@ -35,7 +35,7 @@ BfConstResolver::BfConstResolver(BfModule* bfModule) : BfExprEvaluator(bfModule) } BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfConstResolveFlags flags) -{ +{ mBfEvalExprFlags = (BfEvalExprFlags)(mBfEvalExprFlags | BfEvalExprFlags_Comptime); // Handle the 'int[?] val = .(1, 2, 3)' case @@ -137,11 +137,11 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo int stringId = mModule->GetStringPoolIdx(mResult.mValue); if (stringId != -1) { - if ((flags & BfConstResolveFlag_RemapFromStringId) != 0) + if ((flags & BfConstResolveFlag_ActualizeValues) != 0) { prevIgnoreWrites.Restore(); mModule->mBfIRBuilder->PopulateType(mResult.mType); - return BfTypedValue(mModule->GetStringObjectValue(stringId), mResult.mType); + return BfTypedValue(mModule->GetStringObjectValue(stringId, false, true), mResult.mType); } return BfTypedValue(mModule->mBfIRBuilder->CreateConst(BfTypeCode_StringId, stringId), toType); @@ -232,7 +232,9 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo }*/ mModule->FixIntUnknown(mResult); - mModule->FixValueActualization(mResult); + + if ((flags & BfConstResolveFlag_NoActualizeValues) == 0) + mModule->FixValueActualization(mResult, !prevIgnoreWrites.mPrevVal || ((flags & BfConstResolveFlag_ActualizeValues) != 0)); return mResult; } diff --git a/IDEHelper/Compiler/BfConstResolver.h b/IDEHelper/Compiler/BfConstResolver.h index 53f0dafd..6d1d02ff 100644 --- a/IDEHelper/Compiler/BfConstResolver.h +++ b/IDEHelper/Compiler/BfConstResolver.h @@ -15,9 +15,10 @@ enum BfConstResolveFlags BfConstResolveFlag_ExplicitCast = 1, BfConstResolveFlag_NoCast = 2, BfConstResolveFlag_AllowSoftFail = 4, - BfConstResolveFlag_RemapFromStringId = 8, - BfConstResolveFlag_ArrayInitSize = 0x10, - BfConstResolveFlag_AllowGlobalVariable = 0x20, + BfConstResolveFlag_ActualizeValues = 8, + BfConstResolveFlag_NoActualizeValues = 0x10, + BfConstResolveFlag_ArrayInitSize = 0x20, + BfConstResolveFlag_AllowGlobalVariable = 0x40, }; class BfConstResolver : public BfExprEvaluator diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index 770a8908..bc57a481 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -1654,8 +1654,11 @@ String* BfModule::GetStringPoolString(BfIRValue constantStr, BfIRConstHolder * c return NULL; } -BfIRValue BfModule::GetStringCharPtr(int stringId) +BfIRValue BfModule::GetStringCharPtr(int stringId, bool force) { + if ((mBfIRBuilder->mIgnoreWrites) && (!force)) + return mBfIRBuilder->CreateConst(BfTypeCode_StringId, stringId); + BfIRValue* irValue = NULL; if (!mStringCharPtrPool.TryAdd(stringId, NULL, &irValue)) return *irValue; @@ -1670,13 +1673,13 @@ BfIRValue BfModule::GetStringCharPtr(int stringId) return strCharPtrConst; } -BfIRValue BfModule::GetStringCharPtr(BfIRValue strValue) +BfIRValue BfModule::GetStringCharPtr(BfIRValue strValue, bool force) { if (strValue.IsConst()) { int stringId = GetStringPoolIdx(strValue); BF_ASSERT(stringId != -1); - return GetStringCharPtr(stringId); + return GetStringCharPtr(stringId, force); } BfIRValue charPtrPtr = mBfIRBuilder->CreateInBoundsGEP(strValue, 0, 1); @@ -1684,27 +1687,32 @@ BfIRValue BfModule::GetStringCharPtr(BfIRValue strValue) return charPtr; } -BfIRValue BfModule::GetStringCharPtr(const StringImpl& str) +BfIRValue BfModule::GetStringCharPtr(const StringImpl& str, bool force) { - return GetStringCharPtr(GetStringObjectValue(str)); + return GetStringCharPtr(GetStringObjectValue(str, force), force); } -BfIRValue BfModule::GetStringObjectValue(int strId) +BfIRValue BfModule::GetStringObjectValue(int strId, bool define, bool force) { BfIRValue* objValue; if (mStringObjectPool.TryGetValue(strId, &objValue)) return *objValue; auto stringPoolEntry = mContext->mStringObjectIdMap[strId]; - return GetStringObjectValue(stringPoolEntry.mString, true); + return GetStringObjectValue(stringPoolEntry.mString, define, force); } -BfIRValue BfModule::GetStringObjectValue(const StringImpl& str, bool define) +BfIRValue BfModule::GetStringObjectValue(const StringImpl& str, bool define, bool force) { auto stringType = ResolveTypeDef(mCompiler->mStringTypeDef, define ? BfPopulateType_Data : BfPopulateType_Declaration); mBfIRBuilder->PopulateType(stringType); - int strId = mContext->GetStringLiteralId(str); + int strId = mContext->GetStringLiteralId(str); + + if ((mBfIRBuilder->mIgnoreWrites) && (!force)) + { + return mBfIRBuilder->CreateConst(BfTypeCode_StringId, strId); + } BfIRValue* irValuePtr = NULL; if (!mStringObjectPool.TryAdd(strId, NULL, &irValuePtr)) @@ -5900,8 +5908,8 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin if (needsTypeNames) { - typeNameConst = GetStringObjectValue(typeDef->mName->mString, true); - namespaceConst = GetStringObjectValue(typeDef->mNamespace.ToString(), true); + typeNameConst = GetStringObjectValue(typeDef->mName->mString, !mIsComptimeModule); + namespaceConst = GetStringObjectValue(typeDef->mNamespace.ToString(), !mIsComptimeModule); } else { @@ -6041,7 +6049,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin *orderedIdPtr = (int)usedStringIdMap.size() - 1; } - GetStringObjectValue(stringId); + GetStringObjectValue(stringId, true, true); PUSH_INT8(0xFF); // String PUSH_INT32(*orderedIdPtr); argIdx++; @@ -6061,7 +6069,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin *orderedIdPtr = (int)usedStringIdMap.size() - 1; } - GetStringObjectValue(stringId); + GetStringObjectValue(stringId, true, true); PUSH_INT8(0xFF); // String PUSH_INT32(*orderedIdPtr); argIdx++; @@ -6225,7 +6233,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin BfType* payloadType = typeInstance->GetUnionInnerType(); if (!payloadType->IsValuelessType()) { - BfIRValue payloadNameConst = GetStringObjectValue("$payload", true); + BfIRValue payloadNameConst = GetStringObjectValue("$payload", !mIsComptimeModule); SizedArray payloadFieldVals = { emptyValueType, @@ -6240,7 +6248,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin } BfType* dscrType = typeInstance->GetDiscriminatorType(); - BfIRValue dscrNameConst = GetStringObjectValue("$discriminator", true); + BfIRValue dscrNameConst = GetStringObjectValue("$discriminator", !mIsComptimeModule); SizedArray dscrFieldVals = { emptyValueType, @@ -6262,7 +6270,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin BfFieldInstance* fieldInstance = &typeInstance->mFieldInstances[fieldIdx]; BfFieldDef* fieldDef = fieldInstance->GetFieldDef(); - BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, true); + BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, !mIsComptimeModule); int typeId = 0; auto fieldType = fieldInstance->GetResolvedType(); @@ -6538,7 +6546,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin funcVal = mBfIRBuilder->CreateBitCast(moduleMethodInstance.mFunc, voidPtrIRType); } - BfIRValue methodNameConst = GetStringObjectValue(methodDef->mName, true); + BfIRValue methodNameConst = GetStringObjectValue(methodDef->mName, !mIsComptimeModule); BfMethodFlags methodFlags = defaultMethod->GetMethodFlags(); @@ -6561,7 +6569,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary& usedStrin if (defaultMethod->GetParamIsSplat(paramIdx)) paramFlags = (ParamFlags)(paramFlags | ParamFlag_Splat); - BfIRValue paramNameConst = GetStringObjectValue(paramName, true); + BfIRValue paramNameConst = GetStringObjectValue(paramName, !mIsComptimeModule); SizedArray paramDataVals = { @@ -10621,17 +10629,18 @@ BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConst auto constVal = mBfIRBuilder->CreateConst(constant, constHolder); BfTypedValue typedValue; + bool allowUnactualized = mBfIRBuilder->mIgnoreWrites; if (constant->mTypeCode == BfTypeCode_StringId) { if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) || ((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8)))) { - typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, wantType), wantType); + typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, wantType, allowUnactualized), wantType); return typedValue; } auto stringType = ResolveTypeDef(mCompiler->mStringTypeDef); - typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, stringType), stringType); + typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, stringType, allowUnactualized), stringType); } if (!typedValue) @@ -10675,7 +10684,27 @@ BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConst return BfTypedValue(irValue, wantType, false); } -BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowStringId) +bool BfModule::HasUnactializedConstant(BfConstant* constant, BfIRConstHolder* constHolder) +{ + if ((constant->mConstType == BfConstType_TypeOf) || (constant->mConstType == BfConstType_TypeOf_WithData)) + return true; + if (constant->mTypeCode == BfTypeCode_StringId) + return true; + + if (constant->mConstType == BfConstType_Agg) + { + auto constArray = (BfConstantAgg*)constant; + for (auto val : constArray->mValues) + { + if (HasUnactializedConstant(constHolder->GetConstant(val), constHolder)) + return true; + } + } + + return false; +} + +BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowUnactualized) { if (constant->mTypeCode == BfTypeCode_NullPtr) { @@ -10690,20 +10719,27 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con if (constant->mTypeCode == BfTypeCode_StringId) { - if (!allowStringId) + if (!allowUnactualized) { if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) || ((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8)))) { const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString; - BfIRValue stringObjConst = GetStringObjectValue(str); + BfIRValue stringObjConst = GetStringObjectValue(str, false, true); if (wantType->IsPointer()) - return GetStringCharPtr(stringObjConst); + return GetStringCharPtr(stringObjConst, true); return stringObjConst; } } } + if (constant->mConstType == Beefy::BfConstType_TypeOf) + { + auto constTypeOf = (BfTypeOf_Const*)constant; + AddDependency(constTypeOf->mType, mCurTypeInstance, BfDependencyMap::DependencyFlag_ExprTypeReference); + return CreateTypeDataRef(constTypeOf->mType); + } + if (constant->mConstType == BfConstType_Agg) { auto constArray = (BfConstantAgg*)constant; @@ -11012,8 +11048,8 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri auto& fieldTypeInst = checkTypeInst->mFieldInstances[bestField->mIdx]; if (assignExpr->mRight != NULL) - { - BfTypedValue result = constResolver.Resolve(assignExpr->mRight, fieldTypeInst.mResolvedType); + { + BfTypedValue result = constResolver.Resolve(assignExpr->mRight, fieldTypeInst.mResolvedType, BfConstResolveFlag_NoActualizeValues); if (result) { CurrentAddToConstHolder(result.mValue); @@ -14049,8 +14085,10 @@ void BfModule::DoLocalVariableDebugInfo(BfLocalVariable* localVarDef, bool doAli if (mBfIRBuilder->HasDebugLocation()) { if ((isConstant) && (!didConstToMem)) - { - localVarDef->mDbgDeclareInst = mBfIRBuilder->DbgInsertValueIntrinsic(localVarDef->mConstValue, diVariable); + { + BfTypedValue result(localVarDef->mConstValue, localVarDef->mResolvedType); + FixValueActualization(result); + localVarDef->mDbgDeclareInst = mBfIRBuilder->DbgInsertValueIntrinsic(result.mValue, diVariable); } else { @@ -18139,7 +18177,7 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup) auto methodDeclaration = methodDef->GetMethodDeclaration(); if ((methodDef->mHasComptime) && (!mIsComptimeModule)) - mBfIRBuilder->mIgnoreWrites = true; + mBfIRBuilder->mIgnoreWrites = true; if ((methodInstance->mIsReified) && (methodInstance->mVirtualTableIdx != -1)) { diff --git a/IDEHelper/Compiler/BfModule.h b/IDEHelper/Compiler/BfModule.h index fedc977d..881944c0 100644 --- a/IDEHelper/Compiler/BfModule.h +++ b/IDEHelper/Compiler/BfModule.h @@ -1538,12 +1538,12 @@ public: BfIRValue CreateStringCharPtr(const StringImpl& str, int stringId, bool define); int GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHolder = NULL); String* GetStringPoolString(BfIRValue constantStr, BfIRConstHolder* constHolder = NULL); - BfIRValue GetStringCharPtr(int stringId); - BfIRValue GetStringCharPtr(BfIRValue strValue); - BfIRValue GetStringCharPtr(const StringImpl& str); - BfIRValue GetStringObjectValue(int idx); - BfIRValue GetStringObjectValue(const StringImpl& str, bool define = false); - BfIRValue CreateGlobalConstValue(const StringImpl& name, BfIRValue constant, BfIRType type, bool external); + BfIRValue GetStringCharPtr(int stringId, bool force = false); + BfIRValue GetStringCharPtr(BfIRValue strValue, bool force = false); + BfIRValue GetStringCharPtr(const StringImpl& str, bool force = false); + BfIRValue GetStringObjectValue(int idx, bool define, bool force); + BfIRValue GetStringObjectValue(const StringImpl& str, bool define = false, bool force = false); + BfIRValue CreateGlobalConstValue(const StringImpl& name, BfIRValue constant, BfIRType type, bool external); void VariantToString(StringImpl& str, const BfVariant& variant); StringT<128> TypeToString(BfType* resolvedType, Array* genericMethodParamNameOverrides = NULL); StringT<128> TypeToString(BfType* resolvedType, BfTypeNameFlags typeNameFlags, Array* genericMethodParamNameOverrides = NULL); @@ -1553,8 +1553,9 @@ public: void pm(BfMethodInstance* type); void CurrentAddToConstHolder(BfIRValue& irVal); void ClearConstData(); - BfTypedValue GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType); - BfIRValue ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowStringId = false); + bool HasUnactializedConstant(BfConstant* constant, BfIRConstHolder* constHolder); + BfTypedValue GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType); + BfIRValue ConstantToCurrent(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType, bool allowUnactualized = false); void ValidateCustomAttributes(BfCustomAttributes* customAttributes, BfAttributeTargets attrTarget); void GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttributeDirective* attributesDirective, BfAttributeTargets attrType, bool allowNonConstArgs = false, BfCaptureInfo* captureInfo = NULL); BfCustomAttributes* GetCustomAttributes(BfAttributeDirective* attributesDirective, BfAttributeTargets attrType, bool allowNonConstArgs = false, BfCaptureInfo* captureInfo = NULL); @@ -1771,8 +1772,8 @@ public: BfMethodRefType* CreateMethodRefType(BfMethodInstance* methodInstance, bool mustAlreadyExist = false); BfType* FixIntUnknown(BfType* type); void FixIntUnknown(BfTypedValue& typedVal, BfType* matchType = NULL); - void FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs); - void FixValueActualization(BfTypedValue& typedVal); + void FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs); + void FixValueActualization(BfTypedValue& typedVal, bool force = false); bool TypeEquals(BfTypedValue& val, BfType* type); BfTypeDef* ResolveGenericInstanceDef(BfGenericInstanceTypeRef* genericTypeRef, BfType** outType = NULL, BfResolveTypeRefFlags resolveFlags = BfResolveTypeRefFlag_None); BfType* ResolveType(BfType* lookupType, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = BfResolveTypeRefFlag_None); diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 129a15b4..7f3de22b 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -6439,19 +6439,16 @@ void BfModule::FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs) FixIntUnknown(rhs); } -void BfModule::FixValueActualization(BfTypedValue& typedVal) +void BfModule::FixValueActualization(BfTypedValue& typedVal, bool force) { if (!typedVal.mValue.IsConst()) return; - if (mBfIRBuilder->mIgnoreWrites) + if ((mBfIRBuilder->mIgnoreWrites) && (!force)) return; auto constant = mBfIRBuilder->GetConstant(typedVal.mValue); - if (constant->mConstType == BfConstType_TypeOf) - { - auto constTypeOf = (BfTypeOf_Const*)constant; - AddDependency(constTypeOf->mType, mCurTypeInstance, BfDependencyMap::DependencyFlag_ExprTypeReference); - typedVal.mValue = CreateTypeDataRef(constTypeOf->mType); - } + if (!HasUnactializedConstant(constant, mBfIRBuilder)) + return; + typedVal.mValue = ConstantToCurrent(constant, mBfIRBuilder, typedVal.mType, false); } BfTypeInstance* BfModule::GetPrimitiveStructType(BfTypeCode typeCode) diff --git a/IDEHelper/Compiler/BfStmtEvaluator.cpp b/IDEHelper/Compiler/BfStmtEvaluator.cpp index 556d9105..e04eaeeb 100644 --- a/IDEHelper/Compiler/BfStmtEvaluator.cpp +++ b/IDEHelper/Compiler/BfStmtEvaluator.cpp @@ -1654,7 +1654,7 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD if (isConst) { BfConstResolver constResolver(this); - initValue = constResolver.Resolve(varDecl->mInitializer, resolvedType, BfConstResolveFlag_RemapFromStringId); + initValue = constResolver.Resolve(varDecl->mInitializer, resolvedType, BfConstResolveFlag_ActualizeValues); if (!initValue) initValue = GetDefaultTypedValue(resolvedType); }