From 4edfa03ffcdab22ec617c6387611407c6d23f696 Mon Sep 17 00:00:00 2001 From: Romain BOULLARD Date: Sat, 9 May 2026 12:23:35 +0200 Subject: [PATCH] native_type and native_inline on fields --- src/idl_gen_cpp.cpp | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 51bb82049..02de41604 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -987,7 +987,13 @@ class CppGenerator : public BaseGenerator { case BASE_TYPE_STRUCT: { auto type_name = WrapInNameSpace(*type.struct_def); if (IsStruct(type)) { - auto native_type = type.struct_def->attributes.Lookup("native_type"); + // Field-level native_type takes priority over struct-level + // native_type + auto field_native_type = field.attributes.Lookup("native_type"); + auto native_type = + field_native_type + ? field_native_type + : type.struct_def->attributes.Lookup("native_type"); if (native_type) { type_name = native_type->constant; } @@ -3516,10 +3522,21 @@ class CppGenerator : public BaseGenerator { case BASE_TYPE_STRUCT: { if (IsStruct(type)) { const auto& struct_attrs = type.struct_def->attributes; - const auto native_type = struct_attrs.Lookup("native_type"); + // Field-level native_type takes priority over struct-level + // native_type + auto field_native_type = afield.attributes.Lookup("native_type"); + const auto native_type = field_native_type + ? field_native_type + : struct_attrs.Lookup("native_type"); if (native_type) { std::string unpack_call = "::flatbuffers::UnPack"; - const auto pack_name = struct_attrs.Lookup("native_type_pack_name"); + // Field-level native_type_pack_name takes priority over + // struct-level + auto field_pack_name = + afield.attributes.Lookup("native_type_pack_name"); + const auto pack_name = + field_pack_name ? field_pack_name + : struct_attrs.Lookup("native_type_pack_name"); if (pack_name) { unpack_call += pack_name->constant; } @@ -3918,11 +3935,22 @@ class CppGenerator : public BaseGenerator { case BASE_TYPE_STRUCT: { if (IsStruct(field.value.type)) { const auto& struct_attribs = field.value.type.struct_def->attributes; - const auto native_type = struct_attribs.Lookup("native_type"); + // Field-level native_type takes priority over struct-level + // native_type + auto field_native_type = field.attributes.Lookup("native_type"); + const auto native_type = field_native_type + ? field_native_type + : struct_attribs.Lookup("native_type"); if (native_type && field.native_inline) { code += "::flatbuffers::Pack"; + // Field-level native_type_pack_name takes priority over + // struct-level + auto field_pack_name = + field.attributes.Lookup("native_type_pack_name"); const auto pack_name = - struct_attribs.Lookup("native_type_pack_name"); + field_pack_name + ? field_pack_name + : struct_attribs.Lookup("native_type_pack_name"); if (pack_name) { code += pack_name->constant; }