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
765765pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) *const struct_ZigClangEnumDecl;
766766pub extern fn ZigClangRecordDecl_getCanonicalDecl(record_decl: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangTagDecl;
767767pub 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;
768769pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl;
769770pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl;
770771pub 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 {
632632 const align_expr = blk: {
633633 const alignment = ZigClangVarDecl_getAlignedAttribute(var_decl, rp.c.clang_context);
634634 if (alignment != 0) {
635 _ = try appendToken(rp.c, .Keyword_linksection, "align");
635 _ = try appendToken(rp.c, .Keyword_align, "align");
636636 _ = try appendToken(rp.c, .LParen, "(");
637637 // Clang reports the alignment in bits
638638 const expr = try transCreateNodeInt(rp.c, alignment / 8);
......@@ -827,6 +827,20 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
827827 else => |e| return e,
828828 };
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
830844 const field_node = try c.a().create(ast.Node.ContainerField);
831845 field_node.* = .{
832846 .doc_comments = null,
......@@ -834,7 +848,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
834848 .name_token = field_name,
835849 .type_expr = field_type,
836850 .value_expr = null,
837 .align_expr = null,
851 .align_expr = align_expr,
838852 };
839853
840854 if (is_anon) {
......@@ -4607,7 +4621,7 @@ fn finishTransFnProto(
46074621 if (fn_decl) |decl| {
46084622 const alignment = ZigClangFunctionDecl_getAlignedAttribute(decl, rp.c.clang_context);
46094623 if (alignment != 0) {
4610 _ = try appendToken(rp.c, .Keyword_linksection, "align");
4624 _ = try appendToken(rp.c, .Keyword_align, "align");
46114625 _ = try appendToken(rp.c, .LParen, "(");
46124626 // Clang reports the alignment in bits
46134627 const expr = try transCreateNodeInt(rp.c, alignment / 8);
src/zig_clang.cpp+10
......@@ -1615,6 +1615,16 @@ unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self,
16151615 return 0;
16161616}
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
16181628unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx) {
16191629 auto casted_self = reinterpret_cast<const clang::FunctionDecl *>(self);
16201630 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
865865ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *self, size_t *len);
866866ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);
867867ZIG_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
869870ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self);
870871
test/translate_c.zig+10
......@@ -3,6 +3,16 @@ const std = @import("std");
33const CrossTarget = std.zig.CrossTarget;
44
55pub 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
616 cases.add("structs with VLAs are rejected",
717 \\struct foo { int x; int y[]; };
818 \\struct bar { int x; int y[0]; };