authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-02 12:13:05+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-02 12:33:19-05:00
logd908ca4823e51e8a9a413a3afac540e9e5295826
tree06c4d32cd0bab4f9e5d0f424cabaf9d0966e0387
parent8e89bdfe991ebf643c6f35064a2e72de18feff9b
signature Commit is signed but in an unrecognized format.

Translate align attribute


5 files changed, 72 insertions(+), 6 deletions(-)

src-self-hosted/clang.zig+2
...@@ -765,6 +765,8 @@ pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigC...@@ -765,6 +765,8 @@ pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigC
765pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;765pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;
766pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarDecl) ?*const struct_ZigClangVarDecl;766pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarDecl) ?*const struct_ZigClangVarDecl;
767pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8;767pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8;
768pub extern fn ZigClangFunctionDecl_getAlignedAttribute(self: *const ZigClangFunctionDecl, *const ZigClangASTContext) c_uint;
769pub extern fn ZigClangVarDecl_getAlignedAttribute(self: *const ZigClangVarDecl, *const ZigClangASTContext) c_uint;
768pub extern fn ZigClangRecordDecl_getDefinition(self: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangRecordDecl;770pub extern fn ZigClangRecordDecl_getDefinition(self: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangRecordDecl;
769pub extern fn ZigClangEnumDecl_getDefinition(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangEnumDecl;771pub extern fn ZigClangEnumDecl_getDefinition(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangEnumDecl;
770pub extern fn ZigClangRecordDecl_getLocation(self: ?*const struct_ZigClangRecordDecl) struct_ZigClangSourceLocation;772pub extern fn ZigClangRecordDecl_getLocation(self: ?*const struct_ZigClangRecordDecl) struct_ZigClangSourceLocation;
src-self-hosted/translate_c.zig+32-2
...@@ -597,6 +597,20 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {...@@ -597,6 +597,20 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
597 break :blk null;597 break :blk null;
598 };598 };
599599
600 const align_expr = blk: {
601 const alignment = ZigClangVarDecl_getAlignedAttribute(var_decl, rp.c.clang_context);
602 if (alignment != 0) {
603 _ = try appendToken(rp.c, .Keyword_linksection, "align");
604 _ = try appendToken(rp.c, .LParen, "(");
605 // Clang reports the alignment in bits
606 const expr = try transCreateNodeInt(rp.c, alignment / 8);
607 _ = try appendToken(rp.c, .RParen, ")");
608
609 break :blk expr;
610 }
611 break :blk null;
612 };
613
600 const node = try c.a().create(ast.Node.VarDecl);614 const node = try c.a().create(ast.Node.VarDecl);
601 node.* = ast.Node.VarDecl{615 node.* = ast.Node.VarDecl{
602 .doc_comments = null,616 .doc_comments = null,
...@@ -609,7 +623,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {...@@ -609,7 +623,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
609 .extern_export_token = extern_tok,623 .extern_export_token = extern_tok,
610 .lib_name = null,624 .lib_name = null,
611 .type_node = type_node,625 .type_node = type_node,
612 .align_node = null,626 .align_node = align_expr,
613 .section_node = linksection_expr,627 .section_node = linksection_expr,
614 .init_node = init_node,628 .init_node = init_node,
615 .semicolon_token = try appendToken(c, .Semicolon, ";"),629 .semicolon_token = try appendToken(c, .Semicolon, ";"),
...@@ -4142,6 +4156,22 @@ fn finishTransFnProto(...@@ -4142,6 +4156,22 @@ fn finishTransFnProto(
4142 break :blk null;4156 break :blk null;
4143 };4157 };
41444158
4159 const align_expr = blk: {
4160 if (fn_decl) |decl| {
4161 const alignment = ZigClangFunctionDecl_getAlignedAttribute(decl, rp.c.clang_context);
4162 if (alignment != 0) {
4163 _ = try appendToken(rp.c, .Keyword_linksection, "align");
4164 _ = try appendToken(rp.c, .LParen, "(");
4165 // Clang reports the alignment in bits
4166 const expr = try transCreateNodeInt(rp.c, alignment / 8);
4167 _ = try appendToken(rp.c, .RParen, ")");
4168
4169 break :blk expr;
4170 }
4171 }
4172 break :blk null;
4173 };
4174
4145 const return_type_node = blk: {4175 const return_type_node = blk: {
4146 if (ZigClangFunctionType_getNoReturnAttr(fn_ty)) {4176 if (ZigClangFunctionType_getNoReturnAttr(fn_ty)) {
4147 break :blk try transCreateNodeIdentifier(rp.c, "noreturn");4177 break :blk try transCreateNodeIdentifier(rp.c, "noreturn");
...@@ -4175,7 +4205,7 @@ fn finishTransFnProto(...@@ -4175,7 +4205,7 @@ fn finishTransFnProto(
4175 .cc_token = cc_tok,4205 .cc_token = cc_tok,
4176 .body_node = null,4206 .body_node = null,
4177 .lib_name = null,4207 .lib_name = null,
4178 .align_expr = null,4208 .align_expr = align_expr,
4179 .section_expr = linksection_expr,4209 .section_expr = linksection_expr,
4180 };4210 };
4181 return fn_proto;4211 return fn_proto;
src/zig_clang.cpp+20
...@@ -1592,6 +1592,26 @@ const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *se...@@ -1592,6 +1592,26 @@ const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *se
1592 return nullptr;1592 return nullptr;
1593}1593}
15941594
1595unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx) {
1596 auto casted_self = reinterpret_cast<const clang::VarDecl *>(self);
1597 auto casted_ctx = const_cast<clang::ASTContext *>(reinterpret_cast<const clang::ASTContext *>(ctx));
1598 if (const clang::AlignedAttr *AA = casted_self->getAttr<clang::AlignedAttr>()) {
1599 return AA->getAlignment(*casted_ctx);
1600 }
1601 // Zero means no explicit alignment factor was specified
1602 return 0;
1603}
1604
1605unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx) {
1606 auto casted_self = reinterpret_cast<const clang::FunctionDecl *>(self);
1607 auto casted_ctx = const_cast<clang::ASTContext *>(reinterpret_cast<const clang::ASTContext *>(ctx));
1608 if (const clang::AlignedAttr *AA = casted_self->getAttr<clang::AlignedAttr>()) {
1609 return AA->getAlignment(*casted_ctx);
1610 }
1611 // Zero means no explicit alignment factor was specified
1612 return 0;
1613}
1614
1595const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *zig_record_decl) {1615const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *zig_record_decl) {
1596 const clang::RecordDecl *record_decl = reinterpret_cast<const clang::RecordDecl *>(zig_record_decl);1616 const clang::RecordDecl *record_decl = reinterpret_cast<const clang::RecordDecl *>(zig_record_decl);
1597 const clang::RecordDecl *definition = record_decl->getDefinition();1617 const clang::RecordDecl *definition = record_decl->getDefinition();
src/zig_clang.h+2
...@@ -859,6 +859,8 @@ ZIG_EXTERN_C const struct ZigClangTypedefNameDecl *ZigClangTypedefNameDecl_getCa...@@ -859,6 +859,8 @@ ZIG_EXTERN_C const struct ZigClangTypedefNameDecl *ZigClangTypedefNameDecl_getCa
859ZIG_EXTERN_C const struct ZigClangFunctionDecl *ZigClangFunctionDecl_getCanonicalDecl(const ZigClangFunctionDecl *self);859ZIG_EXTERN_C const struct ZigClangFunctionDecl *ZigClangFunctionDecl_getCanonicalDecl(const ZigClangFunctionDecl *self);
860ZIG_EXTERN_C const struct ZigClangVarDecl *ZigClangVarDecl_getCanonicalDecl(const ZigClangVarDecl *self);860ZIG_EXTERN_C const struct ZigClangVarDecl *ZigClangVarDecl_getCanonicalDecl(const ZigClangVarDecl *self);
861ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *self, size_t *len);861ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigClangVarDecl *self, size_t *len);
862ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);
863ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);
862864
863ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *);865ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *);
864ZIG_EXTERN_C const struct ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const struct ZigClangEnumDecl *);866ZIG_EXTERN_C const struct ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const struct ZigClangEnumDecl *);
test/translate_c.zig+16-4
...@@ -17,14 +17,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -17,14 +17,26 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
17 \\}17 \\}
18 });18 });
1919
20 cases.add("align() attribute",
21 \\__attribute__ ((aligned(128)))
22 \\extern char my_array[16];
23 \\__attribute__ ((aligned(128)))
24 \\void my_fn(void) { }
25 , &[_][]const u8{
26 \\pub extern var my_array: [16]u8 align(128);
27 \\pub export fn my_fn() align(128) void {}
28 });
29
20 cases.add("linksection() attribute",30 cases.add("linksection() attribute",
21 \\__attribute__ ((__section__(".data")))31 \\// Use the "segment,section" format to make this test pass when
32 \\// targeting the mach-o binary format
33 \\__attribute__ ((__section__("NEAR,.data")))
22 \\extern char my_array[16];34 \\extern char my_array[16];
23 \\__attribute__ ((__section__(".data")))35 \\__attribute__ ((__section__("NEAR,.data")))
24 \\void my_fn(void) { }36 \\void my_fn(void) { }
25 , &[_][]const u8{37 , &[_][]const u8{
26 \\pub extern var my_array: [16]u8 linksection(".data");38 \\pub extern var my_array: [16]u8 linksection("NEAR,.data");
27 \\pub export fn my_fn() linksection(".data") void {}39 \\pub export fn my_fn() linksection("NEAR,.data") void {}
28 });40 });
2941
30 cases.add("simple function prototypes",42 cases.add("simple function prototypes",