From 3513f9580a484a8a8e58ac7f93b55e865fa0a26e Mon Sep 17 00:00:00 2001 From: Jon Haslam Date: Tue, 23 Apr 2024 14:56:09 +0100 Subject: [PATCH] TypeGraphv2: correctly handle bitfields in code generation (#495) Currently in TypeGraph when generating inst::Field objects in the generated source we use the `sizeof` operator to construct the static and exclusive size. As you can't use sizeof() on a bitfield this generates invalid code. This fix special cases bit fields and sets the static and exclusive size to 0 as there size will be rolled up in the parent object. --- oi/CodeGen.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/oi/CodeGen.cpp b/oi/CodeGen.cpp index 3cd6ec9..c7c680e 100644 --- a/oi/CodeGen.cpp +++ b/oi/CodeGen.cpp @@ -822,17 +822,24 @@ void CodeGen::genClassTreeBuilderInstructions(const Class& c, code += std::to_string(numFields); code += "> fields{\n"; index = 0; + for (const auto& m : c.members) { ++index; if (m.name.starts_with(AddPadding::MemberPrefix)) continue; std::string fullName = c.name() + "::" + m.name; + bool isbitField = m.bitsize; bool isPrimitive = dynamic_cast(&m.type()); - code += " inst::Field{sizeof("; - code += fullName; - code += "), "; - code += std::to_string(calculateExclusiveSize(m.type())); - code += ",\""; + + if (!isbitField) { + code += " inst::Field{sizeof("; + code += fullName; + code += "), "; + code += std::to_string(calculateExclusiveSize(m.type())); + } else { + code += " inst::Field{0, 0"; + } + code += ", \""; code += m.inputName; code += "\", member_"; code += std::to_string(index);