Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pointer reference to sibling union field on FieldDef #7755

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ struct FieldDef : public Definition {
flexbuffer(false),
presence(kDefault),
nested_flatbuffer(nullptr),
padding(0) {}
padding(0),
sibling_union_field(nullptr){}

Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
const Parser &parser) const;
Expand Down Expand Up @@ -342,6 +343,12 @@ struct FieldDef : public Definition {

StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
size_t padding; // Bytes to always pad after this field.

// sibling_union_field is always set to nullptr. The only exception is
// when FieldDef is a union field or an union type field. Therefore,
// sibling_union_field on a union field points to the union type field
// and vice-versa.
FieldDef *sibling_union_field;
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
};

struct StructDef : public Definition {
Expand Down
8 changes: 8 additions & 0 deletions src/idl_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,14 @@ CheckedError Parser::ParseField(StructDef &struct_def) {
FieldDef *field;
ECHECK(AddField(struct_def, name, type, &field));

if (typefield) {
dbaileychess marked this conversation as resolved.
Show resolved Hide resolved
// We preserve the relation between the typefield
// and field, so we can easily map it in the code
// generators.
typefield->sibling_union_field = field;
field->sibling_union_field = typefield;
}

if (token_ == '=') {
NEXT();
ECHECK(ParseSingleValue(&field->name, field->value, true));
Expand Down