Browse Source

Fixes to "valueless" crepr structs

Brian Fiete 5 months ago
parent
commit
5c11c2271e

+ 8 - 1
IDEHelper/Compiler/BfIRBuilder.cpp

@@ -3784,7 +3784,14 @@ void BfIRBuilder::CreateTypeDefinition_Data(BfModule* populateModule, BfTypeInst
 	SizedArray<BfIRType, 256> irFieldTypes;
 	SizedArray<BfIRType, 256> irFieldTypes;
 	if ((!typeInstance->IsTypedPrimitive()) && (typeInstance->mBaseType != NULL))
 	if ((!typeInstance->IsTypedPrimitive()) && (typeInstance->mBaseType != NULL))
 	{
 	{
-		irFieldTypes.push_back(MapTypeInst(typeInstance->mBaseType, BfIRPopulateType_Eventually_Full));
+		if (typeInstance->mBaseType->IsValuelessCReprType())
+		{
+			// Fake an empty type to avoid having a one-byte base type
+			auto valueTypeType = mModule->ResolveTypeDef(mModule->mCompiler->mValueTypeTypeDef);
+			irFieldTypes.push_back(MapType(valueTypeType, BfIRPopulateType_Eventually_Full));
+		}
+		else
+			irFieldTypes.push_back(MapTypeInst(typeInstance->mBaseType, BfIRPopulateType_Eventually_Full));
 	}
 	}
 
 
 	SizedArray<BfIRMDNode, 256> diFieldTypes;
 	SizedArray<BfIRMDNode, 256> diFieldTypes;

+ 14 - 0
IDEHelper/Compiler/BfResolvedTypeUtils.cpp

@@ -2880,6 +2880,20 @@ bool BfTypeInstance::IsValuelessCReprType()
 	return true;
 	return true;
 }
 }
 
 
+BfTypeInstance* BfTypeInstance::GetBaseType(bool remapValuelessCRepr)
+{
+	if (!remapValuelessCRepr)
+		return mBaseType;
+	auto checkType = mBaseType;
+	while (checkType != NULL)
+	{
+		if (!checkType->IsValuelessCReprType())
+			break;
+		checkType = checkType->mBaseType;
+	}
+	return checkType;
+}
+
 bool BfTypeInstance::IsIRFuncUsed(BfIRFunction func)
 bool BfTypeInstance::IsIRFuncUsed(BfIRFunction func)
 {
 {
 	for (auto& group : mMethodInstanceGroups)
 	for (auto& group : mMethodInstanceGroups)

+ 1 - 0
IDEHelper/Compiler/BfResolvedTypeUtils.h

@@ -2176,6 +2176,7 @@ public:
 	virtual bool CanBeValuelessType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
 	virtual bool CanBeValuelessType() override { return (mTypeDef->mTypeCode == BfTypeCode_Struct) || (mTypeDef->mTypeCode == BfTypeCode_Enum); }
 	virtual bool IsValuelessType() override;
 	virtual bool IsValuelessType() override;
 	virtual bool IsValuelessCReprType();
 	virtual bool IsValuelessCReprType();
+	virtual BfTypeInstance* GetBaseType(bool remapValuelessCRepr = false);
 	virtual bool HasPackingHoles() override { return mHasPackingHoles; }
 	virtual bool HasPackingHoles() override { return mHasPackingHoles; }
 	virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef) override;
 	virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfTypeDef* activeTypeDef) override;
 	virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProject* curProject) override;
 	virtual bool IsTypeMemberAccessible(BfTypeDef* declaringTypeDef, BfProject* curProject) override;

+ 6 - 4
IDEHelper/Compiler/CeMachine.cpp

@@ -4492,10 +4492,11 @@ bool CeContext::WriteConstant(BfModule* module, addr_ce addr, BfConstant* consta
 			auto typeInst = type->ToTypeInstance();
 			auto typeInst = type->ToTypeInstance();
 			int idx = 0;
 			int idx = 0;
 
 
-			if (typeInst->mBaseType != NULL)
+			auto baseType = typeInst->GetBaseType(true);
+			if (baseType != NULL)
 			{
 			{
 				auto baseConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[0]);
 				auto baseConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[0]);
-				if (!WriteConstant(module, addr, baseConstant, typeInst->mBaseType))
+				if (!WriteConstant(module, addr, baseConstant, baseType))
 					return false;
 					return false;
 			}
 			}
 
 
@@ -5034,9 +5035,10 @@ BfIRValue CeContext::CreateConstant(BfModule* module, uint8* ptr, BfType* bfType
 			return irBuilder->CreateConstNull(irBuilder->MapType(typeInst));
 			return irBuilder->CreateConstNull(irBuilder->MapType(typeInst));
 		}
 		}
 
 
-		if (typeInst->mBaseType != NULL)
+		auto baseType = typeInst->GetBaseType(true);
+		if (baseType != NULL)
 		{
 		{
-			auto result = CreateConstant(module, instData, typeInst->mBaseType);
+			auto result = CreateConstant(module, instData, baseType);
 			if (!result)
 			if (!result)
 				return BfIRValue();
 				return BfIRValue();
 			fieldVals.Add(result);
 			fieldVals.Add(result);