diff --git a/IDEHelper/Compiler/BfConstResolver.cpp b/IDEHelper/Compiler/BfConstResolver.cpp index 85352380..e6bcefb5 100644 --- a/IDEHelper/Compiler/BfConstResolver.cpp +++ b/IDEHelper/Compiler/BfConstResolver.cpp @@ -91,7 +91,7 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo return BfTypedValue(mModule->GetStringObjectValue(stringId), mResult.mType); } - return BfTypedValue(mModule->GetConstValue32(stringId), toType); + return BfTypedValue(mModule->mBfIRBuilder->CreateConst(BfTypeCode_StringId, stringId), toType); } } } diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index 1413f9d1..a69d665f 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -5389,6 +5389,8 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu if (!argValue) { argValue = mModule->GetTypedValueFromConstant(foreignConst, methodInstance->GetOwner()->mConstHolder, wantType); + if (!argValue) + mModule->Fail("Default parameter value failed", targetSrc); } } else diff --git a/IDEHelper/Compiler/BfIRBuilder.cpp b/IDEHelper/Compiler/BfIRBuilder.cpp index c31df9cd..5f2a6949 100644 --- a/IDEHelper/Compiler/BfIRBuilder.cpp +++ b/IDEHelper/Compiler/BfIRBuilder.cpp @@ -573,12 +573,12 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f } return CreateConstArray(constArray->mType, copiedVals); } - else if ((IsInt(fromConst->mTypeCode)) || (fromConst->mTypeCode == BfTypeCode_Boolean)) + else if ((IsInt(fromConst->mTypeCode)) || (fromConst->mTypeCode == BfTypeCode_Boolean) || (fromConst->mTypeCode == BfTypeCode_StringId)) { return CreateConst(fromConst->mTypeCode, fromConst->mUInt64); } else if ((fromConst->mTypeCode == BfTypeCode_Single) || (fromConst->mTypeCode == BfTypeCode_Double)) - { + { return CreateConst(fromConst->mTypeCode, fromConst->mDouble); } else if (fromConst->mTypeCode == BfTypeCode_NullPtr) diff --git a/IDEHelper/Compiler/BfIRBuilder.h b/IDEHelper/Compiler/BfIRBuilder.h index 24599442..2f81288d 100644 --- a/IDEHelper/Compiler/BfIRBuilder.h +++ b/IDEHelper/Compiler/BfIRBuilder.h @@ -81,8 +81,9 @@ enum BfTypeCode : uint8 { BfTypeCode_None, BfTypeCode_CharPtr, + BfTypeCode_StringId, BfTypeCode_Pointer, - BfTypeCode_NullPtr, + BfTypeCode_NullPtr, BfTypeCode_Self, BfTypeCode_Dot, BfTypeCode_Var, diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index a452c88c..8b36dbc9 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -1518,7 +1518,7 @@ int BfModule::GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHold constHolder = mBfIRBuilder; auto constant = constHolder->GetConstant(constantStr); - if (constant->mTypeCode == BfTypeCode_Int32) + if (constant->mTypeCode == BfTypeCode_StringId) { return constant->mInt32; } @@ -6075,7 +6075,7 @@ BfIRFunction BfModule::GetIntrinsic(BfMethodInstance* methodInstance, bool repor auto constant = methodOwner->mConstHolder->GetConstant(customAttribute.mCtorArgs[0]); String error; - if ((constant != NULL) && (BfIRConstHolder::IsInt(constant->mTypeCode))) + if ((constant != NULL) && (constant->mTypeCode = BfTypeCode_StringId)) { int stringId = constant->mInt32; auto entry = mContext->mStringObjectIdMap[stringId]; @@ -9335,7 +9335,7 @@ void BfModule::CurrentAddToConstHolder(BfIRValue& irVal) int stringPoolIdx = GetStringPoolIdx(irVal, mBfIRBuilder); if (stringPoolIdx != -1) { - irVal = mCurTypeInstance->GetOrCreateConstHolder()->CreateConst(BfTypeCode_Int32, stringPoolIdx); + irVal = mCurTypeInstance->GetOrCreateConstHolder()->CreateConst(BfTypeCode_StringId, stringPoolIdx); return; } @@ -9374,56 +9374,70 @@ void BfModule::ClearConstData() } BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType) -{ - - bool isString = false; - isString = (wantType->IsObject()) && (wantType->ToTypeInstance()->mTypeDef == mCompiler->mStringTypeDef); - if (wantType->IsPointer()) - isString = true; - - if (!isString) +{ + switch (constant->mTypeCode) { - switch (constant->mTypeCode) + case BfTypeCode_StringId: + case BfTypeCode_Boolean: + case BfTypeCode_Int8: + case BfTypeCode_UInt8: + case BfTypeCode_Int16: + case BfTypeCode_UInt16: + case BfTypeCode_Int32: + case BfTypeCode_UInt32: + case BfTypeCode_Int64: + case BfTypeCode_UInt64: + case BfTypeCode_IntPtr: + case BfTypeCode_UIntPtr: + case BfTypeCode_IntUnknown: + case BfTypeCode_UIntUnknown: + case BfTypeCode_Char8: + case BfTypeCode_Char16: + case BfTypeCode_Char32: + case BfTypeCode_Single: + case BfTypeCode_Double: { - case BfTypeCode_Boolean: - case BfTypeCode_Int8: - case BfTypeCode_UInt8: - case BfTypeCode_Int16: - case BfTypeCode_UInt16: - case BfTypeCode_Int32: - case BfTypeCode_UInt32: - case BfTypeCode_Int64: - case BfTypeCode_UInt64: - case BfTypeCode_IntPtr: - case BfTypeCode_UIntPtr: - case BfTypeCode_IntUnknown: - case BfTypeCode_UIntUnknown: - case BfTypeCode_Char8: - case BfTypeCode_Char16: - case BfTypeCode_Char32: - case BfTypeCode_Single: - case BfTypeCode_Double: + auto constVal = mBfIRBuilder->CreateConst(constant, constHolder); + BfTypedValue typedValue; + + if (constant->mTypeCode == BfTypeCode_StringId) + { + if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) || + ((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8)))) + { + typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, wantType), wantType); + return typedValue; + } + + auto stringType = ResolveTypeDef(mCompiler->mStringTypeDef); + typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, stringType), stringType); + } + + if (!typedValue) { auto constVal = mBfIRBuilder->CreateConst(constant, constHolder); - auto typedValue = BfTypedValue(constVal, GetPrimitiveType(constant->mTypeCode)); - if (typedValue.mType == wantType) - return typedValue; - if (wantType->IsTypedPrimitive()) - { - if (typedValue.mType == wantType->GetUnderlyingType()) - { - typedValue.mType = wantType; - return typedValue; - } - } - auto castedTypedValue = Cast(NULL, typedValue, wantType, (BfCastFlags)(BfCastFlags_SilentFail | BfCastFlags_Explicit)); - BF_ASSERT(castedTypedValue); - return castedTypedValue; + typedValue = BfTypedValue(constVal, GetPrimitiveType(constant->mTypeCode)); } - break; - default: break; + + if (typedValue.mType == wantType) + return typedValue; + if (wantType->IsTypedPrimitive()) + { + if (typedValue.mType == wantType->GetUnderlyingType()) + { + typedValue.mType = wantType; + return typedValue; + } + } + auto castedTypedValue = Cast(NULL, typedValue, wantType, (BfCastFlags)(BfCastFlags_SilentFail | BfCastFlags_Explicit)); + if (!castedTypedValue) + return BfTypedValue(); + return castedTypedValue; } + break; + default: break; } + BfIRValue irValue = ConstantToCurrent(constant, constHolder, wantType); BF_ASSERT(irValue); if (!irValue) @@ -9438,17 +9452,19 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con return GetDefaultValue(wantType); } - bool isString = false; - isString = (wantType->IsObject()) && (wantType->ToTypeInstance()->mTypeDef == mCompiler->mStringTypeDef); - if (((wantType->IsPointer()) || (isString)) && (mBfIRBuilder->IsInt(constant->mTypeCode))) - { - const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString; - BfIRValue stringObjConst = GetStringObjectValue(str); - if (wantType->IsObject()) - return stringObjConst; - return GetStringCharPtr(stringObjConst); - } - + if (constant->mTypeCode == BfTypeCode_StringId) + { + if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) || + ((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8)))) + { + const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString; + BfIRValue stringObjConst = GetStringObjectValue(str); + if (wantType->IsPointer()) + return GetStringCharPtr(stringObjConst); + return stringObjConst; + } + } + if (constant->mConstType == BfConstType_Array) { auto elementType = wantType->GetUnderlyingType(); diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index fe2424d2..7e0b249d 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -4536,6 +4536,9 @@ BfPrimitiveType* BfModule::GetPrimitiveType(BfTypeCode typeCode) case BfTypeCode_UIntUnknown: primType = (BfPrimitiveType*)ResolveTypeDef(mSystem->mTypeUIntUnknown); break; + case BfTypeCode_StringId: + BF_FATAL("Invalid use of StringId"); + break; default: break; } mContext->mPrimitiveTypes[typeCode] = primType; diff --git a/IDEHelper/Tests/src/Operators.bf b/IDEHelper/Tests/src/Operators.bf index 6c3dbd53..3b5d0ff2 100644 --- a/IDEHelper/Tests/src/Operators.bf +++ b/IDEHelper/Tests/src/Operators.bf @@ -231,6 +231,13 @@ namespace Tests { return val.mA; } + + public static implicit operator Self(int val) + { + IntStruct sVal; + sVal.mA = val; + return sVal; + } } [Test] @@ -244,6 +251,12 @@ namespace Tests const String cStrD = "D"; const char8* cStrPD = "D"; + public static void TestDefaults(StringView sv = "ABC", IntStruct intStruct = 123) + { + Test.Assert(sv == "ABC"); + Test.Assert(intStruct.mA == 123); + } + [Test] public static void TestStringOp() { @@ -254,6 +267,8 @@ namespace Tests const char8* cStr3 = "A" + "B"; const char8* cStr4 = cStr1 + "C" + cStrPD; Test.Assert(StringView(cStr4) == "ABCD"); + + TestDefaults(); } } }