authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-10 15:57:57+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-10 15:57:57+02:00
logcb4c488cbd1e02b88ca8e7466a0945691f06d4fd
treed8a3fbabbcfbfa783a17e610f923270baefb155e
parent4cace8f7c342b7d1b1f415cc28f48d5c98bada72
signaturelock-open Commit is signed but in an unrecognized format.

translate-c support struct field alignment


5 files changed, 39 insertions(+), 3 deletions(-)

src-self-hosted/clang.zig+1
...@@ -765,6 +765,7 @@ pub extern fn ZigClangTagDecl_isThisDeclarationADefinition(self: *const ZigClang...@@ -765,6 +765,7 @@ pub extern fn ZigClangTagDecl_isThisDeclarationADefinition(self: *const ZigClang
765pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) *const struct_ZigClangEnumDecl;765pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) *const struct_ZigClangEnumDecl;
766pub extern fn ZigClangRecordDecl_getCanonicalDecl(record_decl: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangTagDecl;766pub extern fn ZigClangRecordDecl_getCanonicalDecl(record_decl: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangTagDecl;
767pub extern fn ZigClangFieldDecl_getCanonicalDecl(field_decl: ?*const struct_ZigClangFieldDecl) ?*const struct_ZigClangFieldDecl;767pub extern fn ZigClangFieldDecl_getCanonicalDecl(field_decl: ?*const struct_ZigClangFieldDecl) ?*const struct_ZigClangFieldDecl;
768pub extern fn ZigClangFieldDecl_getAlignedAttribute(field_decl: ?*const struct_ZigClangFieldDecl, *const ZigClangASTContext) c_uint;
768pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl;769pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl;
769pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl;770pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl;
770pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;771pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;
src-self-hosted/translate_c.zig+17-3
...@@ -632,7 +632,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {...@@ -632,7 +632,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
632 const align_expr = blk: {632 const align_expr = blk: {
633 const alignment = ZigClangVarDecl_getAlignedAttribute(var_decl, rp.c.clang_context);633 const alignment = ZigClangVarDecl_getAlignedAttribute(var_decl, rp.c.clang_context);
634 if (alignment != 0) {634 if (alignment != 0) {
635 _ = try appendToken(rp.c, .Keyword_linksection, "align");635 _ = try appendToken(rp.c, .Keyword_align, "align");
636 _ = try appendToken(rp.c, .LParen, "(");636 _ = try appendToken(rp.c, .LParen, "(");
637 // Clang reports the alignment in bits637 // Clang reports the alignment in bits
638 const expr = try transCreateNodeInt(rp.c, alignment / 8);638 const expr = try transCreateNodeInt(rp.c, alignment / 8);
...@@ -827,6 +827,20 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*...@@ -827,6 +827,20 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
827 else => |e| return e,827 else => |e| return e,
828 };828 };
829829
830 const align_expr = blk: {
831 const alignment = ZigClangFieldDecl_getAlignedAttribute(field_decl, rp.c.clang_context);
832 if (alignment != 0) {
833 _ = try appendToken(rp.c, .Keyword_align, "align");
834 _ = try appendToken(rp.c, .LParen, "(");
835 // Clang reports the alignment in bits
836 const expr = try transCreateNodeInt(rp.c, alignment / 8);
837 _ = try appendToken(rp.c, .RParen, ")");
838
839 break :blk expr;
840 }
841 break :blk null;
842 };
843
830 const field_node = try c.a().create(ast.Node.ContainerField);844 const field_node = try c.a().create(ast.Node.ContainerField);
831 field_node.* = .{845 field_node.* = .{
832 .doc_comments = null,846 .doc_comments = null,
...@@ -834,7 +848,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*...@@ -834,7 +848,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
834 .name_token = field_name,848 .name_token = field_name,
835 .type_expr = field_type,849 .type_expr = field_type,
836 .value_expr = null,850 .value_expr = null,
837 .align_expr = null,851 .align_expr = align_expr,
838 };852 };
839853
840 if (is_anon) {854 if (is_anon) {
...@@ -4607,7 +4621,7 @@ fn finishTransFnProto(...@@ -4607,7 +4621,7 @@ fn finishTransFnProto(
4607 if (fn_decl) |decl| {4621 if (fn_decl) |decl| {
4608 const alignment = ZigClangFunctionDecl_getAlignedAttribute(decl, rp.c.clang_context);4622 const alignment = ZigClangFunctionDecl_getAlignedAttribute(decl, rp.c.clang_context);
4609 if (alignment != 0) {4623 if (alignment != 0) {
4610 _ = try appendToken(rp.c, .Keyword_linksection, "align");4624 _ = try appendToken(rp.c, .Keyword_align, "align");
4611 _ = try appendToken(rp.c, .LParen, "(");4625 _ = try appendToken(rp.c, .LParen, "(");
4612 // Clang reports the alignment in bits4626 // Clang reports the alignment in bits
4613 const expr = try transCreateNodeInt(rp.c, alignment / 8);4627 const expr = try transCreateNodeInt(rp.c, alignment / 8);
src/zig_clang.cpp+10
...@@ -1615,6 +1615,16 @@ unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self,...@@ -1615,6 +1615,16 @@ unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self,
1615 return 0;1615 return 0;
1616}1616}
16171617
1618unsigned ZigClangFieldDecl_getAlignedAttribute(const struct ZigClangFieldDecl *self, const ZigClangASTContext* ctx) {
1619 auto casted_self = reinterpret_cast<const clang::FieldDecl *>(self);
1620 auto casted_ctx = const_cast<clang::ASTContext *>(reinterpret_cast<const clang::ASTContext *>(ctx));
1621 if (const clang::AlignedAttr *AA = casted_self->getAttr<clang::AlignedAttr>()) {
1622 return AA->getAlignment(*casted_ctx);
1623 }
1624 // Zero means no explicit alignment factor was specified
1625 return 0;
1626}
1627
1618unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx) {1628unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx) {
1619 auto casted_self = reinterpret_cast<const clang::FunctionDecl *>(self);1629 auto casted_self = reinterpret_cast<const clang::FunctionDecl *>(self);
1620 auto casted_ctx = const_cast<clang::ASTContext *>(reinterpret_cast<const clang::ASTContext *>(ctx));1630 auto casted_ctx = const_cast<clang::ASTContext *>(reinterpret_cast<const clang::ASTContext *>(ctx));
src/zig_clang.h+1
...@@ -865,6 +865,7 @@ ZIG_EXTERN_C const struct ZigClangVarDecl *ZigClangVarDecl_getCanonicalDecl(cons...@@ -865,6 +865,7 @@ ZIG_EXTERN_C const struct ZigClangVarDecl *ZigClangVarDecl_getCanonicalDecl(cons
865ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *self, size_t *len);865ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *self, size_t *len);
866ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);866ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);
867ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);867ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);
868ZIG_EXTERN_C unsigned ZigClangFieldDecl_getAlignedAttribute(const struct ZigClangFieldDecl *self, const ZigClangASTContext* ctx);
868869
869ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self);870ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self);
870871
test/translate_c.zig+10
...@@ -3,6 +3,16 @@ const std = @import("std");...@@ -3,6 +3,16 @@ const std = @import("std");
3const CrossTarget = std.zig.CrossTarget;3const CrossTarget = std.zig.CrossTarget;
44
5pub fn addCases(cases: *tests.TranslateCContext) void {5pub fn addCases(cases: *tests.TranslateCContext) void {
6 cases.add("struct with aligned fields",
7 \\struct foo {
8 \\ __attribute__((aligned(1))) short bar;
9 \\};
10 , &[_][]const u8{
11 \\pub const struct_foo = extern struct {
12 \\ bar: c_short align(1),
13 \\};
14 });
15
6 cases.add("structs with VLAs are rejected",16 cases.add("structs with VLAs are rejected",
7 \\struct foo { int x; int y[]; };17 \\struct foo { int x; int y[]; };
8 \\struct bar { int x; int y[0]; };18 \\struct bar { int x; int y[0]; };