-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
translate-c: Allow translating packed C structs iff ABI-sized
This relaxes the restriction added in c7884af and fixes #12733 Right now, the compiler only considers packed structs to be C compatible (extern) if the structs are "ABI sized". See also b83c037 for details on the extern restrictions on `packed struct` This is an issue on all platforms, but is of particular importance on aarch64-macos (seeissue for details) Before this commit, a type like `` struct example { int val; } __attribute__((__packed__)); `` would fail to translate and turn into `` pub const example = @CompileError("cannot translate packed record union"); `` This fixes this issue for ABI-sized structs, making the translate-c rules consistent with the regular compiler. However, we still reject packed structs that are not "ABI-sized", giving the same `@compileError` message So `` struct example { long a, b, c } __attribute__((__packed__)); `` will still fail (in the sense that it will emit a `@compileError`) **Implementation details**: For the purposes of translate-c, a packed struct is considered "ABI-sized" when sizeof(target_struct) <= sizeof(void*) Unfortunately, right now the translate-c `*Context` structure (on the Zig side) doesn't directly have any information on the target we're translating into. However, clang has this target information, and keeps it in clang::ASTContext. I added a helper function to extract the `clang::TargetInfo` from this, then query the pointer size. This is potentially controversial, since it technically makes the output target-dependent. The decision of whether to accept/reject a packed struct varies depending on the target's pointer size. However, the translator already asks for (integer) field offsets elsewhere, so I'm pretty sure target info already impacts translation. We also add getSize() and getDataSize() functions to *clang.RecordDecl. These return padded and unpadded sizes respectively. For a packed struct, they should be the same. The overall effect is 4 more functions added to zig_clang.cpp, in order to extract target info & query record sizes.
- Loading branch information
Showing
4 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters