1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-07 19:18:19 +02:00

Added typeof_comptime const type

This commit is contained in:
Brian Fiete 2025-01-19 12:40:50 -08:00
parent a82cc0534d
commit c511773dad
10 changed files with 111 additions and 10 deletions

View file

@ -104,7 +104,7 @@ namespace System.Reflection
case (TypeCode)typeof(TypeCode).MaxValue + 9: //BfConstType_TypeOf
let argTypeId = Decode!<int32>(data);
args[argIdx] = Type.[Friend]GetType((.)argTypeId);
case (TypeCode)typeof(TypeCode).MaxValue + 18: // BfConstType_Box
case (TypeCode)typeof(TypeCode).MaxValue + 19: // BfConstType_Box
let boxedTypeId = Decode!<int32>(data);
var boxedType = Type.[Friend]GetType_(boxedTypeId);
int dataSize = boxedType.InstanceSize - boxedType.[Friend]mMemberDataOffset;
@ -216,7 +216,7 @@ namespace System.Reflection
case (TypeCode)typeof(TypeCode).MaxValue + 9: //BfConstType_TypeOf
let argTypeId = AttributeInfo.Decode!<int32>(mData);
args[argIdx] = Variant.Create(Type.[Friend]GetType((.)argTypeId));
case (TypeCode)typeof(TypeCode).MaxValue + 18: // BfConstType_Box
case (TypeCode)typeof(TypeCode).MaxValue + 19: // BfConstType_Box
let boxedTypeId = AttributeInfo.Decode!<int32>(mData);
var boxedType = Type.[Friend]GetType_(boxedTypeId);
int dataSize = boxedType.InstanceSize - boxedType.[Friend]mMemberDataOffset;

View file

@ -962,11 +962,21 @@ void BeIRCodeGen::Read(BeValue*& beValue)
}
else if (constType == BfConstType_TypeOf)
{
CMD_PARAM(BeType*, type);
CMD_PARAM(BeType*, type);
beValue = mReflectDataMap[type];
BF_ASSERT(beValue != NULL);
return;
}
else if (constType == BfConstType_TypeOf_Comptime)
{
CMD_PARAM(BeType*, typeType);
CMD_PARAM(int, bfTypeId);
auto beConst = mBeModule->mAlloc.Alloc<BeTypeOfConstant>();
beConst->mType = typeType;
beConst->mBfTypeId = bfTypeId;
beValue = beConst;
return;
}
else if (constType == BfConstType_TypeOf_WithData)
{
CMD_PARAM(BeType*, type);

View file

@ -1457,6 +1457,13 @@ void BeDumpContext::ToString(StringImpl& str, BeValue* value, bool showType, boo
return;
}
if (auto constant = BeValueDynCast<BeTypeOfConstant>(value))
{
ToString(str, constant->GetType());
str += StrFormat(" typeof(%d)", constant->mBfTypeId);
return;
}
if (auto constant = BeValueDynCast<BeStringConstant>(value))
{
ToString(str, constant->GetType());

View file

@ -439,6 +439,20 @@ public:
}
};
class BeTypeOfConstant : public BeConstant
{
public:
BE_VALUE_TYPE(BeTypeOfConstant, BeConstant);
int mBfTypeId;
virtual void HashContent(BeHashContext& hashCtx) override
{
hashCtx.Mixin(TypeId);
hashCtx.Mixin(mBfTypeId);
}
};
class BeStringConstant : public BeConstant
{
public:

View file

@ -406,6 +406,11 @@ String BfIRConstHolder::ToString(BfIRValue irValue)
auto typeofConst = (BfTypeOf_Const*)constant;
return "typeof " + mModule->TypeToString(typeofConst->mType);
}
else if (constant->mConstType == BfConstType_TypeOf_Comptime)
{
auto typeofConst = (BfTypeOf_Const*)constant;
return "typeof_comptime " + mModule->TypeToString(typeofConst->mType);
}
else if (constant->mConstType == BfConstType_TypeOf_WithData)
{
auto typeofConst = (BfTypeOf_WithData_Const*)constant;
@ -687,8 +692,8 @@ int BfIRConstHolder::CheckConstEquality(BfIRValue lhs, BfIRValue rhs)
}
}
if (((constLHS->mConstType == BfConstType_TypeOf) || (constLHS->mConstType == BfConstType_TypeOf_WithData)) &&
((constRHS->mConstType == BfConstType_TypeOf) || (constRHS->mConstType == BfConstType_TypeOf_WithData)))
if (((constLHS->mConstType == BfConstType_TypeOf) || (constLHS->mConstType == BfConstType_TypeOf_WithData) || (constLHS->mConstType == BfConstType_TypeOf_Comptime)) &&
((constRHS->mConstType == BfConstType_TypeOf) || (constRHS->mConstType == BfConstType_TypeOf_WithData) || (constRHS->mConstType == BfConstType_TypeOf_Comptime)))
{
auto typeOfLHS = (BfTypeOf_Const*)constLHS;
auto typeOfRHS = (BfTypeOf_Const*)constRHS;
@ -892,6 +897,11 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
auto typeOf = (BfTypeOf_Const*)fromConst;
return CreateTypeOf(typeOf->mType);
}
else if (fromConst->mConstType == BfConstType_TypeOf_Comptime)
{
auto typeOf = (BfTypeOf_Const*)fromConst;
return CreateTypeOfComptime(typeOf->mType);
}
else if (fromConst->mConstType == BfConstType_TypeOf_WithData)
{
auto typeOf = (BfTypeOf_WithData_Const*)fromConst;
@ -1039,6 +1049,7 @@ BfIRValue BfIRConstHolder::CreateConstAgg(BfIRType type, const BfSizedArray<BfIR
for (auto& val : values)
{
BF_ASSERT(val);
//BF_ASSERT(val.IsConst());
}
#endif
@ -1137,6 +1148,18 @@ BfIRValue BfIRConstHolder::CreateConstBox(BfIRValue val, BfIRType type)
return castedVal;
}
BfIRValue BfIRConstHolder::CreateTypeOfComptime(BfType* type)
{
BfTypeOf_Const* typeOf = mTempAlloc.Alloc<BfTypeOf_Const>();
typeOf->mConstType = BfConstType_TypeOf_Comptime;
typeOf->mType = type;
auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(typeOf));
#ifdef CHECK_CONSTHOLDER
irValue.mHolder = this;
#endif
return irValue;
}
BfIRValue BfIRConstHolder::CreateTypeOf(BfType* type)
{
BfTypeOf_Const* typeOf = mTempAlloc.Alloc<BfTypeOf_Const>();
@ -2530,6 +2553,14 @@ void BfIRBuilder::Write(const BfIRValue& irValue)
Write(MapType(typeofConst->mType, BfIRPopulateType_Identity));
}
break;
case (int)BfConstType_TypeOf_Comptime:
{
auto typeType = mModule->ResolveTypeDef(mModule->mCompiler->mTypeTypeDef);
auto typeofConst = (BfTypeOf_Const*)constant;
Write(MapType(typeType, BfIRPopulateType_Identity));
Write(typeofConst->mType->mTypeId);
}
break;
case (int)BfConstType_Undef:
{
auto undefConst = (BfConstantUndef*)constant;

View file

@ -134,6 +134,7 @@ enum BfConstType
BfConstType_IntToPtr,
BfConstType_TypeOf,
BfConstType_TypeOf_WithData,
BfConstType_TypeOf_Comptime,
BfConstType_AggZero,
BfConstType_Agg,
BfConstType_AggCE,
@ -981,6 +982,7 @@ public:
BfIRValue CreateConstBitCast(BfIRValue val, BfIRType type);
BfIRValue CreateConstBox(BfIRValue val, BfIRType type);
BfIRValue CreateTypeOf(BfType* type);
BfIRValue CreateTypeOfComptime(BfType* type);
BfIRValue CreateTypeOf(BfType* type, BfIRValue typeData);
BfIRValue GetUndefConstValue(BfIRType type);
BfIRValue CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);

View file

@ -5620,7 +5620,7 @@ BfIRValue BfModule::CreateClassVDataExtGlobal(BfTypeInstance* declTypeInst, BfTy
return globalVariable;
}
BfIRValue BfModule::CreateTypeDataRef(BfType* type)
BfIRValue BfModule::CreateTypeDataRef(BfType* type, bool forceConstant)
{
if (mBfIRBuilder->mIgnoreWrites)
{
@ -5629,6 +5629,9 @@ BfIRValue BfModule::CreateTypeDataRef(BfType* type)
if (mIsComptimeModule)
{
if (forceConstant)
return mBfIRBuilder->CreateTypeOfComptime(type);
auto typeTypeDef = ResolveTypeDef(mCompiler->mTypeTypeDef);
auto typeTypeInst = typeTypeDef->ToTypeInstance();
return mBfIRBuilder->Comptime_GetReflectType(type->mTypeId, mBfIRBuilder->MapType(typeTypeInst));
@ -12119,7 +12122,7 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con
auto constTypeOf = (BfTypeOf_Const*)constant;
if (mCurTypeInstance != NULL)
AddDependency(constTypeOf->mType, mCurTypeInstance, BfDependencyMap::DependencyFlag_ExprTypeReference);
return CreateTypeDataRef(constTypeOf->mType);
return CreateTypeDataRef(constTypeOf->mType, true);
}
if (constant->mConstType == BfConstType_PtrToInt)

View file

@ -2090,7 +2090,7 @@ public:
BfIRValue CreateClassVDataGlobal(BfTypeInstance* typeInstance, int* outNumElements = NULL, String* outMangledName = NULL);
BfIRValue GetClassVDataPtr(BfTypeInstance* typeInstance);
BfIRValue CreateClassVDataExtGlobal(BfTypeInstance* declTypeInst, BfTypeInstance* implTypeInst, int startVirtIdx);
BfIRValue CreateTypeDataRef(BfType* type);
BfIRValue CreateTypeDataRef(BfType* type, bool forceConstant = false);
void EncodeAttributeData(BfTypeInstance* typeInstance, BfType* argType, BfIRValue arg, SizedArrayImpl<uint8>& data, Dictionary<int, int>& usedStringIdMap);
BfIRValue CreateFieldData(BfFieldInstance* fieldInstance, int customAttrIdx);
void CreateSlotOfs(BfTypeInstance* typeInstance);

View file

@ -1885,6 +1885,17 @@ CeOperand CeBuilder::GetOperand(BeValue* value, bool allowAlloca, bool allowImme
// return GetOperand(callInst->mInlineResult);
}
break;
case BeTypeOfConstant::TypeId:
{
auto beTypeOf = (BeTypeOfConstant*)value;
auto ptrType = mCeMachine->GetBeContext()->GetVoidPtrType();
CeOperand result = FrameAlloc(ptrType);
Emit(CeOp_GetReflectType);
EmitFrameOffset(result);
Emit((int32)beTypeOf->mBfTypeId);
return result;
}
break;
}
CeOperand* operandPtr = NULL;

View file

@ -551,6 +551,28 @@ namespace Tests
}
}
struct StructD
{
[Comptime]
public static Span<Type> GetTypes()
{
List<Type> list = scope .();
list.Add(typeof(StructA));
list.Add(typeof(EnumA));
return list;
}
}
struct StructE
{
[Comptime]
public static int GetSizes()
{
const let typeList = StructD.GetTypes();
return typeList[0].InstanceSize + typeList[1].InstanceSize;
}
}
[Test]
public static void TestBasics()
{
@ -604,8 +626,6 @@ namespace Tests
Test.Assert(typeof(decltype(f)) == typeof(float));
Test.Assert(ClassB<const 3>.cTimesTen == 30);
DictWrapper<Dictionary<int, float>> dictWrap = scope .();
dictWrap.[Friend]mValue.Add(1, 2.3f);
dictWrap.[Friend]mValue.Add(2, 3.4f);
@ -643,6 +663,9 @@ namespace Tests
DefaultCtorTest dct = .();
Test.Assert(dct.mA == 123);
const int typeSizes = StructE.GetSizes();
Test.Assert(typeSizes == 25);
}
}
}