authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-08-08 20:43:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-08-08 20:43:38-07:00
log2ed949a6ae053262ee563b198a8758ba82f50e84
tree6bb367b942f9f4b319a9e1b452260e9a7725d6a3
parent0d5ecc4312f45f9288c4a349837f04b733405960

add zeroes value


11 files changed, 99 insertions(+), 14 deletions(-)

doc/langref.md+1-1
......@@ -151,7 +151,7 @@ GotoExpression = "goto" Symbol
151151
152152GroupedExpression = "(" Expression ")"
153153
154KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "type"
154KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "zeroes" | "error" | "type"
155155```
156156
157157## Operator Precedence
src/all_types.hpp+17-2
......@@ -62,11 +62,18 @@ struct ConstErrValue {
6262 ConstExprValue *payload;
6363};
6464
65enum ConstValSpecial {
66 ConstValSpecialOther,
67 ConstValSpecialUndef,
68 ConstValSpecialZeroes,
69};
70
6571struct ConstExprValue {
66 bool ok; // true if constant expression evalution worked
72 bool ok;
6773 bool depends_on_compile_var;
68 bool undef;
74 ConstValSpecial special;
6975
76 // populated if val_type == ConstValTypeOk
7077 union {
7178 BigNum x_bignum;
7279 bool x_bool;
......@@ -167,6 +174,7 @@ enum NodeType {
167174 NodeTypeBoolLiteral,
168175 NodeTypeNullLiteral,
169176 NodeTypeUndefinedLiteral,
177 NodeTypeZeroesLiteral,
170178 NodeTypeIfBoolExpr,
171179 NodeTypeIfVarExpr,
172180 NodeTypeWhileExpr,
......@@ -694,6 +702,12 @@ struct AstNodeUndefinedLiteral {
694702 Expr resolved_expr;
695703};
696704
705struct AstNodeZeroesLiteral {
706 // populated by semantic analyzer
707 StructValExprCodeGen resolved_struct_val_expr;
708 Expr resolved_expr;
709};
710
697711struct AstNodeSymbolExpr {
698712 Buf *symbol;
699713
......@@ -791,6 +805,7 @@ struct AstNode {
791805 AstNodeStructValueField struct_val_field;
792806 AstNodeNullLiteral null_literal;
793807 AstNodeUndefinedLiteral undefined_literal;
808 AstNodeZeroesLiteral zeroes_literal;
794809 AstNodeSymbolExpr symbol_expr;
795810 AstNodeBoolLiteral bool_literal;
796811 AstNodeBreakExpr break_expr;
src/analyze.cpp+23-2
......@@ -90,6 +90,7 @@ static AstNode *first_executing_node(AstNode *node) {
9090 case NodeTypeBoolLiteral:
9191 case NodeTypeNullLiteral:
9292 case NodeTypeUndefinedLiteral:
93 case NodeTypeZeroesLiteral:
9394 case NodeTypeIfBoolExpr:
9495 case NodeTypeIfVarExpr:
9596 case NodeTypeLabel:
......@@ -1849,6 +1850,7 @@ static void resolve_top_level_decl(CodeGen *g, AstNode *node, bool pointer_only)
18491850 case NodeTypeBoolLiteral:
18501851 case NodeTypeNullLiteral:
18511852 case NodeTypeUndefinedLiteral:
1853 case NodeTypeZeroesLiteral:
18521854 case NodeTypeSymbol:
18531855 case NodeTypePrefixOpExpr:
18541856 case NodeTypeIfBoolExpr:
......@@ -3937,7 +3939,19 @@ static TypeTableEntry *analyze_undefined_literal_expr(CodeGen *g, ImportTableEnt
39373939 ConstExprValue *const_val = &expr->const_val;
39383940
39393941 const_val->ok = true;
3940 const_val->undef = true;
3942 const_val->special = ConstValSpecialUndef;
3943
3944 return expected_type ? expected_type : g->builtin_types.entry_undef;
3945}
3946
3947static TypeTableEntry *analyze_zeroes_literal_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
3948 TypeTableEntry *expected_type, AstNode *node)
3949{
3950 Expr *expr = get_resolved_expr(node);
3951 ConstExprValue *const_val = &expr->const_val;
3952
3953 const_val->ok = true;
3954 const_val->special = ConstValSpecialZeroes;
39413955
39423956 return expected_type ? expected_type : g->builtin_types.entry_undef;
39433957}
......@@ -4252,7 +4266,7 @@ static TypeTableEntry *analyze_if_bool_expr(CodeGen *g, ImportTableEntry *import
42524266 }
42534267
42544268 ConstExprValue *cond_val = &get_resolved_expr(*cond)->const_val;
4255 if (cond_val->undef) {
4269 if (cond_val->special == ConstValSpecialUndef) {
42564270 add_node_error(g, first_executing_node(*cond), buf_sprintf("branch on undefined value"));
42574271 return cond_type;
42584272 }
......@@ -6504,6 +6518,9 @@ static TypeTableEntry *analyze_expression_pointer_only(CodeGen *g, ImportTableEn
65046518 case NodeTypeUndefinedLiteral:
65056519 return_type = analyze_undefined_literal_expr(g, import, context, expected_type, node);
65066520 break;
6521 case NodeTypeZeroesLiteral:
6522 return_type = analyze_zeroes_literal_expr(g, import, context, expected_type, node);
6523 break;
65076524 case NodeTypeSymbol:
65086525 return_type = analyze_symbol_expr(g, import, context, expected_type, node, pointer_only);
65096526 break;
......@@ -6771,6 +6788,7 @@ static void scan_decls(CodeGen *g, ImportTableEntry *import, BlockContext *conte
67716788 case NodeTypeBoolLiteral:
67726789 case NodeTypeNullLiteral:
67736790 case NodeTypeUndefinedLiteral:
6791 case NodeTypeZeroesLiteral:
67746792 case NodeTypeSymbol:
67756793 case NodeTypePrefixOpExpr:
67766794 case NodeTypeIfBoolExpr:
......@@ -7029,6 +7047,8 @@ Expr *get_resolved_expr(AstNode *node) {
70297047 return &node->data.null_literal.resolved_expr;
70307048 case NodeTypeUndefinedLiteral:
70317049 return &node->data.undefined_literal.resolved_expr;
7050 case NodeTypeZeroesLiteral:
7051 return &node->data.zeroes_literal.resolved_expr;
70327052 case NodeTypeGoto:
70337053 return &node->data.goto_expr.resolved_expr;
70347054 case NodeTypeBreak:
......@@ -7111,6 +7131,7 @@ static TopLevelDecl *get_as_top_level_decl(AstNode *node) {
71117131 case NodeTypeBoolLiteral:
71127132 case NodeTypeNullLiteral:
71137133 case NodeTypeUndefinedLiteral:
7134 case NodeTypeZeroesLiteral:
71147135 case NodeTypeLabel:
71157136 case NodeTypeGoto:
71167137 case NodeTypeBreak:
src/ast_render.cpp+4
......@@ -171,6 +171,8 @@ static const char *node_type_str(NodeType node_type) {
171171 return "NullLiteral";
172172 case NodeTypeUndefinedLiteral:
173173 return "UndefinedLiteral";
174 case NodeTypeZeroesLiteral:
175 return "ZeroesLiteral";
174176 case NodeTypeIfBoolExpr:
175177 return "IfBoolExpr";
176178 case NodeTypeIfVarExpr:
......@@ -591,6 +593,8 @@ static void render_node(AstRender *ar, AstNode *node) {
591593 zig_panic("TODO");
592594 case NodeTypeUndefinedLiteral:
593595 zig_panic("TODO");
596 case NodeTypeZeroesLiteral:
597 zig_panic("TODO");
594598 case NodeTypeIfBoolExpr:
595599 zig_panic("TODO");
596600 case NodeTypeIfVarExpr:
src/codegen.cpp+17-5
......@@ -3141,11 +3141,15 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
31413141 }
31423142
31433143 bool have_init_expr = false;
3144 bool want_zeroes = false;
31443145 if (var_decl->expr) {
31453146 ConstExprValue *const_val = &get_resolved_expr(var_decl->expr)->const_val;
3146 if (!const_val->ok || !const_val->undef) {
3147 if (!const_val->ok || const_val->special == ConstValSpecialOther) {
31473148 have_init_expr = true;
31483149 }
3150 if (const_val->ok && const_val->special == ConstValSpecialZeroes) {
3151 want_zeroes = true;
3152 }
31493153 }
31503154 if (have_init_expr) {
31513155 TypeTableEntry *expr_type = get_expr_type(var_decl->expr);
......@@ -3204,7 +3208,8 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
32043208 }
32053209 }
32063210 }
3207 if (!ignore_uninit && want_debug_safety(g, source_node)) {
3211 bool want_safe = want_debug_safety(g, source_node);
3212 if (!ignore_uninit && (want_safe || want_zeroes)) {
32083213 TypeTableEntry *usize = g->builtin_types.entry_usize;
32093214 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, variable->type->type_ref);
32103215 uint64_t align_bytes = get_memcpy_align(g, variable->type);
......@@ -3212,7 +3217,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
32123217 // memset uninitialized memory to 0xa
32133218 set_debug_source_node(g, source_node);
32143219 LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
3215 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
3220 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), want_zeroes ? 0x00 : 0xaa, false);
32163221 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, variable->value_ref, ptr_u8, "");
32173222 LLVMValueRef byte_count = LLVMConstInt(usize->type_ref, size_bytes, false);
32183223 LLVMValueRef align_in_bytes = LLVMConstInt(LLVMInt32Type(), align_bytes, false);
......@@ -3535,6 +3540,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
35353540 case NodeTypeCharLiteral:
35363541 case NodeTypeNullLiteral:
35373542 case NodeTypeUndefinedLiteral:
3543 case NodeTypeZeroesLiteral:
35383544 case NodeTypeErrorType:
35393545 case NodeTypeTypeLiteral:
35403546 case NodeTypeArrayType:
......@@ -3562,8 +3568,14 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
35623568static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstExprValue *const_val) {
35633569 assert(const_val->ok);
35643570
3565 if (const_val->undef) {
3566 return LLVMGetUndef(type_entry->type_ref);
3571 switch (const_val->special) {
3572 case ConstValSpecialUndef:
3573 return LLVMGetUndef(type_entry->type_ref);
3574 case ConstValSpecialZeroes:
3575 return LLVMConstNull(type_entry->type_ref);
3576 case ConstValSpecialOther:
3577 break;
3578
35673579 }
35683580
35693581 switch (type_entry->id) {
src/eval.cpp+4-3
......@@ -545,7 +545,7 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
545545 ConstExprValue *const_val, TypeTableEntry *new_type)
546546{
547547 const_val->depends_on_compile_var = other_val->depends_on_compile_var;
548 const_val->undef = other_val->undef;
548 const_val->special = other_val->special;
549549
550550 assert(other_val != const_val);
551551 switch (cast_op) {
......@@ -572,7 +572,7 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
572572 const_val->data.x_ptr.ptr = ptr_val;
573573 const_val->data.x_ptr.len = 1;
574574 const_val->ok = true;
575 const_val->undef = other_val->undef;
575 const_val->special = other_val->special;
576576 const_val->depends_on_compile_var = other_val->depends_on_compile_var;
577577 } else {
578578 zig_panic("TODO");
......@@ -608,7 +608,7 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
608608
609609 const_val->data.x_maybe = ptr_parent;
610610 const_val->ok = true;
611 const_val->undef = other_val->undef;
611 const_val->special = other_val->special;
612612 const_val->depends_on_compile_var = other_val->depends_on_compile_var;
613613 } else {
614614 zig_panic("TODO");
......@@ -1277,6 +1277,7 @@ static bool eval_expr(EvalFn *ef, AstNode *node, ConstExprValue *out) {
12771277 case NodeTypeSliceExpr:
12781278 case NodeTypeNullLiteral:
12791279 case NodeTypeUndefinedLiteral:
1280 case NodeTypeZeroesLiteral:
12801281 case NodeTypeIfVarExpr:
12811282 case NodeTypeSwitchExpr:
12821283 case NodeTypeSwitchProng:
src/parser.cpp+11-1
......@@ -606,7 +606,7 @@ static AstNode *ast_parse_asm_expr(ParseContext *pc, int *token_index, bool mand
606606
607607/*
608608PrimaryExpression = "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | FnProto | AsmExpression | ("error" "." "Symbol")
609KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "type"
609KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "zeroes" | "error" | "type"
610610*/
611611static AstNode *ast_parse_primary_expr(ParseContext *pc, int *token_index, bool mandatory) {
612612 Token *token = &pc->tokens->at(*token_index);
......@@ -654,6 +654,10 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, int *token_index, bool
654654 AstNode *node = ast_create_node(pc, NodeTypeUndefinedLiteral, token);
655655 *token_index += 1;
656656 return node;
657 } else if (token->id == TokenIdKeywordZeroes) {
658 AstNode *node = ast_create_node(pc, NodeTypeZeroesLiteral, token);
659 *token_index += 1;
660 return node;
657661 } else if (token->id == TokenIdKeywordType) {
658662 AstNode *node = ast_create_node(pc, NodeTypeTypeLiteral, token);
659663 *token_index += 1;
......@@ -2539,6 +2543,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
25392543 case NodeTypeUndefinedLiteral:
25402544 // none
25412545 break;
2546 case NodeTypeZeroesLiteral:
2547 // none
2548 break;
25422549 case NodeTypeIfBoolExpr:
25432550 visit_field(&node->data.if_bool_expr.condition, visit, context);
25442551 visit_field(&node->data.if_bool_expr.then_block, visit, context);
......@@ -2813,6 +2820,9 @@ AstNode *ast_clone_subtree_special(AstNode *old_node, uint32_t *next_node_index,
28132820 case NodeTypeUndefinedLiteral:
28142821 // none
28152822 break;
2823 case NodeTypeZeroesLiteral:
2824 // none
2825 break;
28162826 case NodeTypeIfBoolExpr:
28172827 clone_subtree_field(&new_node->data.if_bool_expr.condition, old_node->data.if_bool_expr.condition, next_node_index);
28182828 clone_subtree_field(&new_node->data.if_bool_expr.then_block, old_node->data.if_bool_expr.then_block, next_node_index);
src/tokenizer.cpp+2
......@@ -137,6 +137,7 @@ static const struct ZigKeyword zig_keywords[] = {
137137 {"var", TokenIdKeywordVar},
138138 {"volatile", TokenIdKeywordVolatile},
139139 {"while", TokenIdKeywordWhile},
140 {"zeroes", TokenIdKeywordZeroes},
140141};
141142
142143bool is_zig_keyword(Buf *buf) {
......@@ -1467,6 +1468,7 @@ const char * token_name(TokenId id) {
14671468 case TokenIdKeywordNoAlias: return "noalias";
14681469 case TokenIdKeywordSwitch: return "switch";
14691470 case TokenIdKeywordUndefined: return "undefined";
1471 case TokenIdKeywordZeroes: return "zeroes";
14701472 case TokenIdKeywordError: return "error";
14711473 case TokenIdKeywordType: return "type";
14721474 case TokenIdKeywordInline: return "inline";
src/tokenizer.hpp+1
......@@ -40,6 +40,7 @@ enum TokenId {
4040 TokenIdKeywordNoAlias,
4141 TokenIdKeywordSwitch,
4242 TokenIdKeywordUndefined,
43 TokenIdKeywordZeroes,
4344 TokenIdKeywordError,
4445 TokenIdKeywordType,
4546 TokenIdKeywordInline,
test/cases/zeroes.zig created+18
......@@ -0,0 +1,18 @@
1const assert = @import("std").debug.assert;
2
3struct Foo {
4 a: f32,
5 b: i32,
6 c: bool,
7 d: ?i32,
8}
9
10#attribute("test")
11fn initializing_a_struct_with_zeroes() {
12 const foo: Foo = zeroes;
13 assert(foo.a == 0.0);
14 assert(foo.b == 0);
15 assert(foo.c == false);
16 assert(if (const x ?= foo.d) false else true);
17}
18
test/self_hosted.zig+1
......@@ -4,6 +4,7 @@ const str = std.str;
44const cstr = std.cstr;
55const other = @import("other.zig");
66const cases_return_type_type = @import("cases/return_type_type.zig");
7const test_zeroes = @import("cases/zeroes.zig");
78
89// normal comment
910/// this is a documentation comment