1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Fixed comptime const evals within comptime

This commit is contained in:
Brian Fiete 2022-02-02 07:27:03 -05:00
parent 1ee32434a5
commit 6c3944170d
5 changed files with 47 additions and 13 deletions

View file

@ -267,7 +267,7 @@ namespace System
static extern void Comptime_EmitAddInterface(int32 typeId, int32 ifaceTypeId); static extern void Comptime_EmitAddInterface(int32 typeId, int32 ifaceTypeId);
static extern void Comptime_EmitMethodEntry(int64 methodHandle, StringView text); static extern void Comptime_EmitMethodEntry(int64 methodHandle, StringView text);
static extern void Comptime_EmitMethodExit(int64 methodHandle, StringView text); static extern void Comptime_EmitMethodExit(int64 methodHandle, StringView text);
static extern void Comptime_EmitMixin(String text); static extern void Comptime_EmitMixin(StringView text);
[Comptime(OnlyFromComptime=true)] [Comptime(OnlyFromComptime=true)]
public static MethodBuilder CreateMethod(Type owner, StringView methodName, Type returnType, MethodFlags methodFlags) public static MethodBuilder CreateMethod(Type owner, StringView methodName, Type returnType, MethodFlags methodFlags)
@ -302,14 +302,14 @@ namespace System
} }
[Comptime(ConstEval=true)] [Comptime(ConstEval=true)]
public static void Mixin(String text) public static void Mixin(StringView text)
{ {
if (Compiler.IsComptime) if (Compiler.IsComptime)
Comptime_EmitMixin(text); Comptime_EmitMixin(text);
} }
[Comptime] [Comptime]
public static void MixinRoot(String text) public static void MixinRoot(StringView text)
{ {
if (Compiler.IsComptime) if (Compiler.IsComptime)
Comptime_EmitMixin(text); Comptime_EmitMixin(text);

View file

@ -3066,6 +3066,11 @@ bool BfExprEvaluator::IsComptime()
return (mModule->mIsComptimeModule) || ((mBfEvalExprFlags & BfEvalExprFlags_Comptime) != 0); return (mModule->mIsComptimeModule) || ((mBfEvalExprFlags & BfEvalExprFlags_Comptime) != 0);
} }
bool BfExprEvaluator::IsConstEval()
{
return ((mBfEvalExprFlags & BfEvalExprFlags_Comptime) != 0);
}
bool BfExprEvaluator::IsComptimeEntry() bool BfExprEvaluator::IsComptimeEntry()
{ {
if (mModule->mIsComptimeModule) if (mModule->mIsComptimeModule)
@ -3741,7 +3746,7 @@ void BfExprEvaluator::Visit(BfLiteralExpression* literalExpr)
void BfExprEvaluator::Visit(BfStringInterpolationExpression* stringInterpolationExpression) void BfExprEvaluator::Visit(BfStringInterpolationExpression* stringInterpolationExpression)
{ {
if (IsComptimeEntry()) if (IsConstEval())
{ {
mModule->Fail("Const evaluation of string interpolation not allowed", stringInterpolationExpression); mModule->Fail("Const evaluation of string interpolation not allowed", stringInterpolationExpression);
} }
@ -4267,6 +4272,7 @@ BfTypedValue BfExprEvaluator::LookupIdentifier(BfIdentifierNode* identifierNode,
void BfExprEvaluator::Visit(BfIdentifierNode* identifierNode) void BfExprEvaluator::Visit(BfIdentifierNode* identifierNode)
{ {
auto autoComplete = GetAutoComplete(); auto autoComplete = GetAutoComplete();
if (autoComplete != NULL) if (autoComplete != NULL)
autoComplete->CheckIdentifier(identifierNode, true); autoComplete->CheckIdentifier(identifierNode, true);
@ -4276,7 +4282,11 @@ void BfExprEvaluator::Visit(BfIdentifierNode* identifierNode)
mModule->CheckTypeRefFixit(identifierNode); mModule->CheckTypeRefFixit(identifierNode);
if ((autoComplete != NULL) && (autoComplete->CheckFixit(identifierNode))) if ((autoComplete != NULL) && (autoComplete->CheckFixit(identifierNode)))
{ {
if (mModule->mCurMethodInstance != NULL) if ((mModule->mCurMethodState != NULL) && (mModule->mCurMethodState->mClosureState != NULL) && (mModule->mCurMethodState->mClosureState->mCapturing))
{
// During this phase we don't have lambda and local method params available so they would result in erroneous fixits
}
else if (mModule->mCurMethodInstance != NULL)
{ {
BfType* fieldType = mExpectingType; BfType* fieldType = mExpectingType;
if (fieldType == NULL) if (fieldType == NULL)
@ -5751,6 +5761,8 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, BfMethodInstance*
} }
} }
if (methodInstance->mReturnType->IsValuelessType())
return BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), returnType);
return mModule->GetDefaultTypedValue(returnType, true, BfDefaultValueKind_Undef); return mModule->GetDefaultTypedValue(returnType, true, BfDefaultValueKind_Undef);
} }
@ -6920,7 +6932,7 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu
if (methodDef->mMethodType == BfMethodType_Extension) if (methodDef->mMethodType == BfMethodType_Extension)
numElements++; numElements++;
if (IsComptimeEntry()) if (IsConstEval())
{ {
if ((wantType->IsArray()) || (wantType->IsInstanceOf(mModule->mCompiler->mSpanTypeDef))) if ((wantType->IsArray()) || (wantType->IsInstanceOf(mModule->mCompiler->mSpanTypeDef)))
{ {
@ -7410,7 +7422,7 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu
else else
{ {
// We need to make a temp and get the addr of that // We need to make a temp and get the addr of that
if ((!wantsSplat) && (!argValue.IsValuelessType()) && (!argValue.IsAddr()) && (!IsComptimeEntry())) if ((!wantsSplat) && (!argValue.IsValuelessType()) && (!argValue.IsAddr()) && (!IsConstEval()))
{ {
argValue = mModule->MakeAddressable(argValue); argValue = mModule->MakeAddressable(argValue);
} }
@ -7430,7 +7442,7 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu
{ {
if (argValue) if (argValue)
{ {
if (IsComptimeEntry()) if (IsConstEval())
{ {
auto constant = mModule->mBfIRBuilder->GetConstant(expandedParamsArray.mValue); auto constant = mModule->mBfIRBuilder->GetConstant(expandedParamsArray.mValue);
BF_ASSERT(constant->mConstType == BfConstType_Agg); BF_ASSERT(constant->mConstType == BfConstType_Agg);
@ -8008,7 +8020,7 @@ BfTypedValue BfExprEvaluator::CheckEnumCreation(BfAstNode* targetSrc, BfTypeInst
BfIRValue enumValue; BfIRValue enumValue;
BfTypedValue result; BfTypedValue result;
bool wantConst = IsComptimeEntry(); bool wantConst = IsConstEval();
if (wantConst) if (wantConst)
{ {
@ -15158,7 +15170,7 @@ BfTypedValue BfExprEvaluator::MakeCallableTarget(BfAstNode* targetSrc, BfTypedVa
if ((target.mType->IsStruct()) && (!target.IsAddr())) if ((target.mType->IsStruct()) && (!target.IsAddr()))
{ {
if (IsComptimeEntry()) if (IsConstEval())
return target; return target;
target = mModule->MakeAddressable(target); target = mModule->MakeAddressable(target);
} }

View file

@ -410,6 +410,7 @@ public:
virtual bool CheckAllowValue(const BfTypedValue& typedValue, BfAstNode* refNode); virtual bool CheckAllowValue(const BfTypedValue& typedValue, BfAstNode* refNode);
BfAutoComplete* GetAutoComplete(); BfAutoComplete* GetAutoComplete();
bool IsComptime(); bool IsComptime();
bool IsConstEval();
bool IsComptimeEntry(); bool IsComptimeEntry();
int GetStructRetIdx(BfMethodInstance* methodInstance, bool forceStatic = false); int GetStructRetIdx(BfMethodInstance* methodInstance, bool forceStatic = false);
BfTypedValue SetupNullConditional(BfTypedValue target, BfTokenNode* dotToken); BfTypedValue SetupNullConditional(BfTypedValue target, BfTokenNode* dotToken);

View file

@ -5078,11 +5078,19 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurModule->mCurMethodInstance, mCallerMethodInstance); SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurModule->mCurMethodInstance, mCallerMethodInstance);
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurModule->mCurTypeInstance, mCallerTypeInstance); SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurModule->mCurTypeInstance, mCallerTypeInstance);
int32 strInstAddr = *(int32*)((uint8*)stackPtr + 0); // int32 strInstAddr = *(int32*)((uint8*)stackPtr + 0);
// String emitStr;
// if (!GetStringFromAddr(strInstAddr, emitStr))
// {
// _Fail("Invalid String");
// return false;
// }
addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr);
String emitStr; String emitStr;
if (!GetStringFromAddr(strInstAddr, emitStr)) if (!GetStringFromStringView(strViewPtr, emitStr))
{ {
_Fail("Invalid String"); _Fail("Invalid StringView");
return false; return false;
} }

View file

@ -424,6 +424,15 @@ namespace Tests
} }
} }
[Comptime]
static int GetMixinVal()
{
int a = 23;
Compiler.Mixin("a += 100;");
Compiler.MixinRoot(scope String("b += 200;"));
return a;
}
[Test] [Test]
public static void TestBasics() public static void TestBasics()
{ {
@ -493,6 +502,10 @@ namespace Tests
GetTupleField<decltype(tuple), const 1>.Type tupType1; GetTupleField<decltype(tuple), const 1>.Type tupType1;
Test.Assert(typeof(decltype(tupType0)) == typeof(int16)); Test.Assert(typeof(decltype(tupType0)) == typeof(int16));
Test.Assert(typeof(decltype(tupType1)) == typeof(float)); Test.Assert(typeof(decltype(tupType1)) == typeof(float));
int b = 34;
Test.Assert(GetMixinVal() == 123);
Test.Assert(b == 234);
} }
} }
} }