0
Fork 0
mirror of https://codeberg.org/ziglang/zig synced 2026-11-18 07:05:05 +00:00
zig/src/zig_clang.cpp

3479 lines
198 KiB

2020-01-23 17:22:13 -05:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2019-07-17 17:45:24 -04:00
2021-08-16 01:11:10 -07:00
2019-07-17 17:45:24 -04:00
2019-07-17 17:45:24 -04:00
2020-01-23 14:35:12 -05:00
2020-01-23 14:35:12 -05:00
2020-01-23 14:35:12 -05:00
2020-01-23 14:35:12 -05:00
2020-01-23 14:35:12 -05:00
2020-01-23 14:35:12 -05:00
2020-01-23 14:35:12 -05:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2019-07-17 17:45:24 -04:00
2019-07-17 17:45:24 -04:00
2019-07-17 17:45:24 -04:00
2019-07-17 17:45:24 -04:00
2020-01-23 17:22:13 -05:00
2021-08-16 01:11:10 -07:00
2019-07-17 17:45:24 -04:00
2021-08-16 01:11:10 -07:00
2019-07-17 17:45:24 -04:00
2019-07-17 17:45:24 -04:00
2020-01-23 17:22:13 -05:00
2021-08-16 01:11:10 -07:00
2019-07-17 17:45:24 -04:00
2021-08-16 01:11:10 -07:00
2019-07-17 17:45:24 -04:00
2019-07-17 17:45:24 -04:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2021-08-16 01:11:10 -07:00
2019-05-15 11:57:30 -04:00
2019-05-15 11:57:30 -04:00
2019-05-15 11:57:30 -04:00
2019-05-26 15:43:13 -07:00
2019-02-28 09:55:27 -05:00
2019-05-26 15:43:13 -07:00
2020-01-05 17:47:29 +01:00
2019-12-19 01:38:42 +02:00
2020-01-02 11:33:26 +01:00
translate-c: packed struct implies align(1) on every field Superceeds PR #12735 (now supporting all packed structs in GNU C) Fixes issue #12733 This stops translating C packed struct as a Zig packed struct. Instead use a regular `extern struct` with `align(1)`. This is because (as @Vexu explained) Zig packed structs are really just integers (not structs). Alignment issue is more complicated. I think @ifreund was the first to notice it in his comment on PR #12735 Justification of my interpretion of the C(lang) behavior comes from a careful reading of the GCC docs for type & variable attributes: (clang emulates gnu's packed attribute here) The final line of the documentation for __attribute__ ((aligned)) [on types] says: > When used on a struct, or struct member, *the aligned attribute can only increase the alignment*; in order to decrease it, the packed attribute must be specified as well. This implies that GCC uses the `packed` attribute for alignment purposes in addition to eliminating padding. The documentation for __attribute__((packed)) [on types], states: > This attribute, attached to a struct, union, or C++ class type definition, specifies that each of its members (other than zero-width bit-fields) is placed to minimize the memory required. **This is equivalent to specifying the packed attribute on each of the members**. The key is resolving this indirection, and looking at the documentation for __attribute__((packed)) [on fields (wierdly under "variables" section)]: > The packed attribute specifies that a **structure member should have the smallest possible alignment** — one bit for a bit-field and one byte otherwise, unless a larger value is specified with the aligned attribute. The attribute does not apply to non-member objects. Furthermore, alignment is the only effect of the packed attribute mentioned in the GCC docs (for "common" architecture). Based on this, it seems safe to completely substitute C 'packed' with Zig 'align(1)'. Target-specific or undocumented behavior potentially changes this. Unfortunately, the current implementation of `translate-c` translates as `packed struct` without alignment info. Because Zig packed structs are really integers (as mentioned above), they are the wrong interpretation and we should be using 'extern struct'. Running `translate-c` on the following code: ```c struct foo { char a; int b; } __attribute__((packed)); struct bar { char a; int b; short c; __attribute__((aligned(8))) long d; } __attribute__((packed)); ``` Previously used a 'packed struct' (which was not FFI-safe on stage1). After applying this change, the translated structures have align(1) explicitly applied to all of their fields AS EXPECTED (unless explicitly overriden). This makes Zig behavior for `tranlsate-c` consistent with clang/GCC. Here is the newly produced (correct) output for the above example: ```zig pub const struct_foo = extern struct { a: u8 align(1), b: c_int align(1), }; pub const struct_bar = extern struct { a: u8 align(1), b: c_int align(1), c: c_short align(1), d: c_long align(8), }; ``` Also note for reference: Since the last stable release (0.9.1), there was a change in the language spec related to the alignment of packed structures. The docs for Zig 0.9.1 read: > Packed structs have 1-byte alignment. So the old behavior of translate-c (not specifying any alignment) was possibly correct back then. However the current docs read: > Packed structs have the same alignment as their backing integer Suggsestive both to the change to an integer-backed representation which is incompatible with C's notation.
2022-09-04 17:16:26 -07:00
2020-01-02 12:13:05 +01:00
2020-01-02 12:13:05 +01:00
translate-c: packed struct implies align(1) on every field Superceeds PR #12735 (now supporting all packed structs in GNU C) Fixes issue #12733 This stops translating C packed struct as a Zig packed struct. Instead use a regular `extern struct` with `align(1)`. This is because (as @Vexu explained) Zig packed structs are really just integers (not structs). Alignment issue is more complicated. I think @ifreund was the first to notice it in his comment on PR #12735 Justification of my interpretion of the C(lang) behavior comes from a careful reading of the GCC docs for type & variable attributes: (clang emulates gnu's packed attribute here) The final line of the documentation for __attribute__ ((aligned)) [on types] says: > When used on a struct, or struct member, *the aligned attribute can only increase the alignment*; in order to decrease it, the packed attribute must be specified as well. This implies that GCC uses the `packed` attribute for alignment purposes in addition to eliminating padding. The documentation for __attribute__((packed)) [on types], states: > This attribute, attached to a struct, union, or C++ class type definition, specifies that each of its members (other than zero-width bit-fields) is placed to minimize the memory required. **This is equivalent to specifying the packed attribute on each of the members**. The key is resolving this indirection, and looking at the documentation for __attribute__((packed)) [on fields (wierdly under "variables" section)]: > The packed attribute specifies that a **structure member should have the smallest possible alignment** — one bit for a bit-field and one byte otherwise, unless a larger value is specified with the aligned attribute. The attribute does not apply to non-member objects. Furthermore, alignment is the only effect of the packed attribute mentioned in the GCC docs (for "common" architecture). Based on this, it seems safe to completely substitute C 'packed' with Zig 'align(1)'. Target-specific or undocumented behavior potentially changes this. Unfortunately, the current implementation of `translate-c` translates as `packed struct` without alignment info. Because Zig packed structs are really integers (as mentioned above), they are the wrong interpretation and we should be using 'extern struct'. Running `translate-c` on the following code: ```c struct foo { char a; int b; } __attribute__((packed)); struct bar { char a; int b; short c; __attribute__((aligned(8))) long d; } __attribute__((packed)); ``` Previously used a 'packed struct' (which was not FFI-safe on stage1). After applying this change, the translated structures have align(1) explicitly applied to all of their fields AS EXPECTED (unless explicitly overriden). This makes Zig behavior for `tranlsate-c` consistent with clang/GCC. Here is the newly produced (correct) output for the above example: ```zig pub const struct_foo = extern struct { a: u8 align(1), b: c_int align(1), }; pub const struct_bar = extern struct { a: u8 align(1), b: c_int align(1), c: c_short align(1), d: c_long align(8), }; ``` Also note for reference: Since the last stable release (0.9.1), there was a change in the language spec related to the alignment of packed structures. The docs for Zig 0.9.1 read: > Packed structs have 1-byte alignment. So the old behavior of translate-c (not specifying any alignment) was possibly correct back then. However the current docs read: > Packed structs have the same alignment as their backing integer Suggsestive both to the change to an integer-backed representation which is incompatible with C's notation.
2022-09-04 17:16:26 -07:00
2020-01-02 11:33:26 +01:00
2020-01-05 20:02:28 +01:00
2020-01-05 11:39:01 +01:00
2020-03-08 12:07:26 +02:00
2019-12-13 14:55:36 +02:00
2019-12-13 14:55:36 +02:00
2019-10-12 16:25:51 +02:00
2019-07-17 17:45:24 -04:00
2019-10-12 16:25:51 +02:00
2019-05-26 17:36:47 -07:00
2019-12-13 14:55:36 +02:00
2019-05-15 11:57:30 -04:00
2019-05-26 15:43:13 -07:00
2019-05-15 11:57:30 -04:00
2019-05-16 12:55:10 -04:00
2019-12-17 11:15:41 +02:00
2019-05-16 12:55:10 -04:00
2019-05-16 12:55:10 -04:00
2019-05-16 12:55:10 -04:00
2019-05-26 19:14:50 -07:00
2019-05-27 14:38:09 -07:00
2019-05-26 19:14:50 -07:00
2019-05-27 14:38:09 -07:00
2019-05-26 19:14:50 -07:00
2019-05-27 14:38:09 -07:00
2019-05-27 19:20:23 -07:00
2019-05-27 19:20:23 -07:00
2021-02-23 23:25:11 -08:00
2019-06-08 15:54:15 -07:00
2019-06-08 15:54:15 -07:00
2019-06-10 23:06:54 -07:00
2019-06-10 23:06:54 -07:00
2019-06-23 17:17:21 -07:00
2020-01-05 17:47:29 +01:00