Skip to content

Conversation

@bahnwaerter
Copy link
Member

No description provided.

// Flat unnamed (anonymous) struct or union by adding the field(s) to to the parent struct or union.
fNames.addAll(su.getFieldNames());
fTypes.addAll(Arrays.asList(su.getFieldTypes()));
bitFieldWidths.addAll(su.getBitFieldWidths());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the idea to flatten anonymous structs into a parent struct; and I guess, similarly, to flatten anonymous unions into a parent union.

But does it also make sense to flatten an anonymous struct into a parent union, or vice versa? A struct is a kind of product type (basically a tuple, but the fields are named), whereas a union is a sum type (only one of the values is present, not all). It seems to me that this information is lost by flattening one into the other, isn't it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, after I thought more about this, I think that the should not be flattened for this reason. At least the sizeof computation could be fixed for that reason. I guess the better solution would be to just have a recursive lookup in CStructOrUnion instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I was thinking in a similar direction: structs and unions could have special handling for anonymous fields (let's be careful to avoid "name clashes" because they all have the field name ""), and then the field lookup should recurse into these anonymous fields.

Relatedly, is it also possible in C to have an anonymous field but where the struct type is named? (and perhaps declared separately?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, @maul-esel. So far, anonymous unions are not supported correctly.

I was thinking in a similar direction: structs and unions could have special handling for anonymous fields (let's be careful to avoid "name clashes" because they all have the field name ""), and then the field lookup should recurse into these anonymous fields.

That would be indeed a great conceptual idea. Unfortunately, our representation of anonymous structs and unions is not created exactly like you described it. Declaration nodes with empty names are not stored as valid declarations. This means that anonymous structs and unions cannot be members of a parent type. We could add more hacks but I wanted to keep the changes as simple as possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just suggest to have a separate member in CStructOrUnion for anonymous fields and to adapt the methods (recursive lookup in anonymous fields, somehow return the fields in getters).

@maul-esel
Copy link
Contributor

maul-esel commented Nov 14, 2025

Thanks for looking into this issue 👍

I don't fully understand the conceptual approach yet, see comment above.

Also, does your fix work if there are multiple anonymous structs / unions inside a parent struct / union? Please add some tests for this.

@maul-esel maul-esel marked this pull request as draft December 7, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants