diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-08-23 11:19:27 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-23 11:19:27 -0400 |
| commit | ec2f9ef4e8be5995ab652dde59b12ee340a9e28d (patch) | |
| tree | 1f91f64575dcf8e9184f74d804021d0fadcb8a24 /src | |
| parent | 9322eee80aadd6d9f80ebc11d29d09898237a14a (diff) | |
| parent | 43587af01acba98d9c09c4e0cf20343be8d7c81b (diff) | |
| download | zig-ec2f9ef4e8be5995ab652dde59b12ee340a9e28d.tar.gz zig-ec2f9ef4e8be5995ab652dde59b12ee340a9e28d.zip | |
Merge pull request #3114 from Tetralux/align-on-struct-fields
parsing and rendering of align(N) on struct fields
Diffstat (limited to 'src')
| -rw-r--r-- | src/all_types.hpp | 2 | ||||
| -rw-r--r-- | src/analyze.cpp | 16 | ||||
| -rw-r--r-- | src/parser.cpp | 8 |
3 files changed, 21 insertions, 5 deletions
diff --git a/src/all_types.hpp b/src/all_types.hpp index 559ebe8cda..2394bcb73b 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -849,6 +849,8 @@ struct AstNodeStructField { Buf *name; AstNode *type; AstNode *value; + // populated if the "align(A)" is present + AstNode *align_expr; }; struct AstNodeStringLiteral { diff --git a/src/analyze.cpp b/src/analyze.cpp index 6397a16818..35c598ab97 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2255,9 +2255,21 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) { if (field->gen_index == SIZE_MAX) continue; - size_t this_field_align; - if (packed) { + uint32_t this_field_align; + + // TODO: Sets the field alignment in the type, but doesn't do anything + // to actually make that happen yet. + AstNode *align_expr = field->decl_node->data.struct_field.align_expr; + if (align_expr != nullptr) { + if (!analyze_const_align(g, &struct_type->data.structure.decls_scope->base, align_expr, &this_field_align)) { + field->type_entry = g->builtin_types.entry_invalid; + } else { + field->align = this_field_align; + } + } else if (packed) { // TODO: https://github.com/ziglang/zig/issues/1512 + // TODO: Validate requested alignment is possible, given packed, + // and given other field alignments. this_field_align = 1; // TODO If we have no type_entry for the field, we've already failed to // compile the program correctly. This stage1 compiler needs a deeper diff --git a/src/parser.cpp b/src/parser.cpp index 1e7e36d0bd..6cd6c2f045 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -782,24 +782,26 @@ static AstNode *ast_parse_var_decl(ParseContext *pc) { return res; } -// ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)? +// ContainerField <- IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)? static AstNode *ast_parse_container_field(ParseContext *pc) { Token *identifier = eat_token_if(pc, TokenIdSymbol); if (identifier == nullptr) return nullptr; AstNode *type_expr = nullptr; - if (eat_token_if(pc, TokenIdColon) != nullptr) + if (eat_token_if(pc, TokenIdColon) != nullptr) { type_expr = ast_expect(pc, ast_parse_type_expr); + } + AstNode *align_expr = ast_parse_byte_align(pc); AstNode *expr = nullptr; if (eat_token_if(pc, TokenIdEq) != nullptr) expr = ast_expect(pc, ast_parse_expr); - AstNode *res = ast_create_node(pc, NodeTypeStructField, identifier); res->data.struct_field.name = token_buf(identifier); res->data.struct_field.type = type_expr; res->data.struct_field.value = expr; + res->data.struct_field.align_expr = align_expr; return res; } |
