authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-09 12:34:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-09 12:34:03-07:00
log745c325d0f498406f229e532753e5d5712e824d4
treeaf0bd93ca81361b65c54c9da83a0b7cecc8097bd
parent56908dcb9dd7bbfae7c22b6312752eb576a227c2

support variable declarations in structs

See #22

5 files changed, 24 insertions(+), 9 deletions(-)

doc/langref.md+1-1
......@@ -17,7 +17,7 @@ VariableDeclaration = ("var" | "const") "Symbol" option(":" TypeExpr) "=" Expres
1717
1818ContainerDecl = ("struct" | "enum" | "union") "Symbol" option(ParamDeclList) "{" many(StructMember) "}"
1919
20StructMember = many(Directive) option(VisibleMod) (StructField | FnDef)
20StructMember = many(Directive) option(VisibleMod) (StructField | FnDef | GlobalVarDecl)
2121
2222StructField = "Symbol" option(":" Expression) ",")
2323
src/all_types.hpp+1-1
......@@ -604,7 +604,7 @@ struct AstNodeStructDecl {
604604 ZigList<AstNode *> generic_params;
605605 bool generic_params_is_var_args; // always an error but it can happen from parsing
606606 ZigList<AstNode *> fields;
607 ZigList<AstNode *> fns;
607 ZigList<AstNode *> decls;
608608
609609 // populated by semantic analyzer
610610 BlockContext *block_context;
src/analyze.cpp+3-3
......@@ -1580,8 +1580,8 @@ static void scan_struct_decl(CodeGen *g, ImportTableEntry *import, BlockContext
15801580 node->data.struct_decl.type_entry = container_type;
15811581
15821582 // handle the member function definitions independently
1583 for (int i = 0; i < node->data.struct_decl.fns.length; i += 1) {
1584 AstNode *child_node = node->data.struct_decl.fns.at(i);
1583 for (int i = 0; i < node->data.struct_decl.decls.length; i += 1) {
1584 AstNode *child_node = node->data.struct_decl.decls.at(i);
15851585 get_as_top_level_decl(child_node)->parent_decl = node;
15861586 BlockContext *child_context = get_container_block_context(container_type);
15871587 scan_decls(g, import, child_context, child_node);
......@@ -1669,7 +1669,7 @@ static void resolve_top_level_decl(CodeGen *g, AstNode *node, bool pointer_only)
16691669 case NodeTypeVariableDeclaration:
16701670 {
16711671 AstNodeVariableDeclaration *variable_declaration = &node->data.variable_declaration;
1672 VariableTableEntry *var = analyze_variable_declaration_raw(g, import, import->block_context,
1672 VariableTableEntry *var = analyze_variable_declaration_raw(g, import, node->block_context,
16731673 node, variable_declaration, false, node, false);
16741674
16751675 g->global_vars.append(var);
src/parser.cpp+11-4
......@@ -2605,7 +2605,7 @@ static AstNode *ast_parse_use(ParseContext *pc, int *token_index,
26052605
26062606/*
26072607ContainerDecl = ("struct" | "enum" | "union") "Symbol" option(ParamDeclList) "{" many(StructMember) "}"
2608StructMember: many(Directive) option(VisibleMod) (StructField | FnDef)
2608StructMember = many(Directive) option(VisibleMod) (StructField | FnDef | GlobalVarDecl)
26092609StructField : "Symbol" option(":" Expression) ",")
26102610*/
26112611static AstNode *ast_parse_container_decl(ParseContext *pc, int *token_index,
......@@ -2664,7 +2664,14 @@ static AstNode *ast_parse_container_decl(ParseContext *pc, int *token_index,
26642664
26652665 AstNode *fn_def_node = ast_parse_fn_def(pc, token_index, false, directive_list, visib_mod);
26662666 if (fn_def_node) {
2667 node->data.struct_decl.fns.append(fn_def_node);
2667 node->data.struct_decl.decls.append(fn_def_node);
2668 continue;
2669 }
2670
2671 AstNode *var_decl_node = ast_parse_variable_declaration_expr(pc, token_index, false, directive_list, visib_mod);
2672 if (var_decl_node) {
2673 ast_eat_token(pc, token_index, TokenIdSemicolon);
2674 node->data.struct_decl.decls.append(var_decl_node);
26682675 continue;
26692676 }
26702677
......@@ -3035,7 +3042,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
30353042 break;
30363043 case NodeTypeStructDecl:
30373044 visit_node_list(&node->data.struct_decl.fields, visit, context);
3038 visit_node_list(&node->data.struct_decl.fns, visit, context);
3045 visit_node_list(&node->data.struct_decl.decls, visit, context);
30393046 visit_node_list(node->data.struct_decl.top_level_decl.directives, visit, context);
30403047 break;
30413048 case NodeTypeStructField:
......@@ -3278,7 +3285,7 @@ AstNode *ast_clone_subtree(AstNode *old_node, uint32_t *next_node_index) {
32783285 case NodeTypeStructDecl:
32793286 clone_subtree_list(&new_node->data.struct_decl.fields, &old_node->data.struct_decl.fields,
32803287 next_node_index);
3281 clone_subtree_list(&new_node->data.struct_decl.fns, &old_node->data.struct_decl.fns,
3288 clone_subtree_list(&new_node->data.struct_decl.decls, &old_node->data.struct_decl.decls,
32823289 next_node_index);
32833290 clone_subtree_list_ptr(&new_node->data.struct_decl.top_level_decl.directives,
32843291 old_node->data.struct_decl.top_level_decl.directives, next_node_index);
test/self_hosted.zig+8
......@@ -1640,3 +1640,11 @@ fn truncate() {
16401640fn test_truncate(x: u32) -> u8 {
16411641 @truncate(u8, x)
16421642}
1643
1644#attribute("test")
1645fn const_decls_in_struct() {
1646 assert(GenericDataThing(3).count_plus_one == 4);
1647}
1648struct GenericDataThing(count: isize) {
1649 const count_plus_one = count + 1;
1650}