-
Notifications
You must be signed in to change notification settings - Fork 47
Add C translation support for anonymous structs and unions #758
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
base: dev
Are you sure you want to change the base?
Add C translation support for anonymous structs and unions #758
Conversation
…ent struct or union
| // 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()); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
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. |
No description provided.