| author | |
| committer | |
| log | 5a8822c714e4ee2d442e76f36213d119530f0fea |
| tree | 45c71c52e8f7304efc2f32cda40b3983d8b76e5e |
| parent | 28c5a8f2cab193858717594fc91df3369980e18c |
6 files changed, 135 insertions(+), 66 deletions(-)
doc/langref.md+2-3| ... | ... | @@ -142,7 +142,7 @@ FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen) |
| 142 | 142 | |
| 143 | 143 | ArrayAccessExpression : token(LBracket) Expression token(RBracket) |
| 144 | 144 | |
| 145 | PrefixOp : token(Not) | token(Dash) | token(Tilde) | |
| 145 | PrefixOp : token(Not) | token(Dash) | token(Tilde) | (token(Ampersand) option(token(Const))) | |
| 146 | 146 | |
| 147 | 147 | PrimaryExpression : token(Number) | token(String) | KeywordLiteral | GroupedExpression | token(Symbol) | Goto | BlockExpression |
| 148 | 148 | |
| ... | ... | @@ -157,8 +157,7 @@ KeywordLiteral : token(Unreachable) | token(Void) | token(True) | token(False) |
| 157 | 157 | |
| 158 | 158 | ``` |
| 159 | 159 | x() x[] x.y |
| 160 | &x | |
| 161 | !x -x ~x | |
| 160 | !x -x ~x &x &const x | |
| 162 | 161 | as |
| 163 | 162 | * / % |
| 164 | 163 | + - |
src/analyze.cpp+70-31| ... | ... | @@ -987,6 +987,50 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B |
| 987 | 987 | } |
| 988 | 988 | } |
| 989 | 989 | |
| 990 | enum LValPurpose { | |
| 991 | LValPurposeAssign, | |
| 992 | LValPurposeAddressOf, | |
| 993 | }; | |
| 994 | ||
| 995 | static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, BlockContext *block_context, | |
| 996 | AstNode *lhs_node, LValPurpose purpose, bool is_ptr_const) | |
| 997 | { | |
| 998 | TypeTableEntry *expected_rhs_type = nullptr; | |
| 999 | if (lhs_node->type == NodeTypeSymbol) { | |
| 1000 | Buf *name = &lhs_node->data.symbol; | |
| 1001 | VariableTableEntry *var = find_variable(block_context, name); | |
| 1002 | if (var) { | |
| 1003 | if (purpose == LValPurposeAssign && var->is_const) { | |
| 1004 | add_node_error(g, lhs_node, | |
| 1005 | buf_sprintf("cannot assign to constant")); | |
| 1006 | } else if (purpose == LValPurposeAddressOf && var->is_const && !is_ptr_const) { | |
| 1007 | add_node_error(g, lhs_node, | |
| 1008 | buf_sprintf("must use &const to get address of constant")); | |
| 1009 | } else { | |
| 1010 | expected_rhs_type = var->type; | |
| 1011 | } | |
| 1012 | } else { | |
| 1013 | add_node_error(g, lhs_node, | |
| 1014 | buf_sprintf("use of undeclared identifier '%s'", buf_ptr(name))); | |
| 1015 | } | |
| 1016 | } else if (lhs_node->type == NodeTypeArrayAccessExpr) { | |
| 1017 | expected_rhs_type = analyze_array_access_expr(g, import, block_context, lhs_node); | |
| 1018 | } else if (lhs_node->type == NodeTypeFieldAccessExpr) { | |
| 1019 | alloc_codegen_node(lhs_node); | |
| 1020 | expected_rhs_type = analyze_field_access_expr(g, import, block_context, lhs_node); | |
| 1021 | } else { | |
| 1022 | if (purpose == LValPurposeAssign) { | |
| 1023 | add_node_error(g, lhs_node, | |
| 1024 | buf_sprintf("assignment target must be variable, field, or array element")); | |
| 1025 | } else if (purpose == LValPurposeAddressOf) { | |
| 1026 | add_node_error(g, lhs_node, | |
| 1027 | buf_sprintf("addressof target must be variable, field, or array element")); | |
| 1028 | } | |
| 1029 | expected_rhs_type = g->builtin_types.entry_invalid; | |
| 1030 | } | |
| 1031 | return expected_rhs_type; | |
| 1032 | } | |
| 1033 | ||
| 990 | 1034 | static TypeTableEntry *analyze_bin_op_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context, |
| 991 | 1035 | TypeTableEntry *expected_type, AstNode *node) |
| 992 | 1036 | { |
| ... | ... | @@ -1006,38 +1050,17 @@ static TypeTableEntry *analyze_bin_op_expr(CodeGen *g, ImportTableEntry *import, |
| 1006 | 1050 | case BinOpTypeAssignBoolOr: |
| 1007 | 1051 | { |
| 1008 | 1052 | AstNode *lhs_node = node->data.bin_op_expr.op1; |
| 1009 | TypeTableEntry *expected_rhs_type = nullptr; | |
| 1010 | if (lhs_node->type == NodeTypeSymbol) { | |
| 1011 | Buf *name = &lhs_node->data.symbol; | |
| 1012 | VariableTableEntry *var = find_variable(context, name); | |
| 1013 | if (var) { | |
| 1014 | if (var->is_const) { | |
| 1015 | add_node_error(g, lhs_node, | |
| 1016 | buf_sprintf("cannot assign to constant variable")); | |
| 1017 | } else { | |
| 1018 | if (!is_op_allowed(var->type, node->data.bin_op_expr.bin_op)) { | |
| 1019 | if (var->type->id != TypeTableEntryIdInvalid) { | |
| 1020 | add_node_error(g, lhs_node, | |
| 1021 | buf_sprintf("operator not allowed for type '%s'", | |
| 1022 | buf_ptr(&var->type->name))); | |
| 1023 | } | |
| 1024 | } else { | |
| 1025 | expected_rhs_type = var->type; | |
| 1026 | } | |
| 1027 | } | |
| 1028 | } else { | |
| 1053 | ||
| 1054 | TypeTableEntry *expected_rhs_type = analyze_lvalue(g, import, context, lhs_node, | |
| 1055 | LValPurposeAssign, false); | |
| 1056 | if (!is_op_allowed(expected_rhs_type, node->data.bin_op_expr.bin_op)) { | |
| 1057 | if (expected_rhs_type->id != TypeTableEntryIdInvalid) { | |
| 1029 | 1058 | add_node_error(g, lhs_node, |
| 1030 | buf_sprintf("use of undeclared identifier '%s'", buf_ptr(name))); | |
| 1059 | buf_sprintf("operator not allowed for type '%s'", | |
| 1060 | buf_ptr(&expected_rhs_type->name))); | |
| 1031 | 1061 | } |
| 1032 | } else if (lhs_node->type == NodeTypeArrayAccessExpr) { | |
| 1033 | expected_rhs_type = analyze_array_access_expr(g, import, context, lhs_node); | |
| 1034 | } else if (lhs_node->type == NodeTypeFieldAccessExpr) { | |
| 1035 | alloc_codegen_node(lhs_node); | |
| 1036 | expected_rhs_type = analyze_field_access_expr(g, import, context, lhs_node); | |
| 1037 | } else { | |
| 1038 | add_node_error(g, lhs_node, | |
| 1039 | buf_sprintf("assignment target must be variable, field, or array element")); | |
| 1040 | 1062 | } |
| 1063 | ||
| 1041 | 1064 | analyze_expression(g, import, context, expected_rhs_type, node->data.bin_op_expr.op2); |
| 1042 | 1065 | return g->builtin_types.entry_void; |
| 1043 | 1066 | } |
| ... | ... | @@ -1388,6 +1411,8 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, |
| 1388 | 1411 | break; |
| 1389 | 1412 | case NodeTypePrefixOpExpr: |
| 1390 | 1413 | switch (node->data.prefix_op_expr.prefix_op) { |
| 1414 | case PrefixOpInvalid: | |
| 1415 | zig_unreachable(); | |
| 1391 | 1416 | case PrefixOpBoolNot: |
| 1392 | 1417 | analyze_expression(g, import, context, g->builtin_types.entry_bool, |
| 1393 | 1418 | node->data.prefix_op_expr.primary_expr); |
| ... | ... | @@ -1407,8 +1432,22 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, |
| 1407 | 1432 | return_type = g->builtin_types.entry_i32; |
| 1408 | 1433 | break; |
| 1409 | 1434 | } |
| 1410 | case PrefixOpInvalid: | |
| 1411 | zig_unreachable(); | |
| 1435 | case PrefixOpAddressOf: | |
| 1436 | case PrefixOpConstAddressOf: | |
| 1437 | { | |
| 1438 | bool is_const = (node->data.prefix_op_expr.prefix_op == PrefixOpConstAddressOf); | |
| 1439 | ||
| 1440 | TypeTableEntry *child_type = analyze_lvalue(g, import, context, | |
| 1441 | node->data.prefix_op_expr.primary_expr, LValPurposeAddressOf, is_const); | |
| 1442 | ||
| 1443 | if (child_type->id == TypeTableEntryIdInvalid) { | |
| 1444 | return_type = g->builtin_types.entry_invalid; | |
| 1445 | break; | |
| 1446 | } | |
| 1447 | ||
| 1448 | return_type = get_pointer_to_type(g, child_type, is_const); | |
| 1449 | break; | |
| 1450 | } | |
| 1412 | 1451 | } |
| 1413 | 1452 | break; |
| 1414 | 1453 | case NodeTypeIfExpr: |
src/codegen.cpp+56-29| ... | ... | @@ -206,7 +206,7 @@ static LLVMValueRef gen_array_ptr(CodeGen *g, AstNode *node) { |
| 206 | 206 | return LLVMBuildInBoundsGEP(g->builder, array_ref_value, indices, 2, ""); |
| 207 | 207 | } |
| 208 | 208 | |
| 209 | static LLVMValueRef gen_field_ptr(CodeGen *g, AstNode *node) { | |
| 209 | static LLVMValueRef gen_field_ptr(CodeGen *g, AstNode *node, TypeTableEntry **out_type_entry) { | |
| 210 | 210 | assert(node->type == NodeTypeFieldAccessExpr); |
| 211 | 211 | |
| 212 | 212 | LLVMValueRef struct_ptr = gen_expr(g, node->data.field_access_expr.struct_expr); |
| ... | ... | @@ -217,6 +217,8 @@ static LLVMValueRef gen_field_ptr(CodeGen *g, AstNode *node) { |
| 217 | 217 | |
| 218 | 218 | assert(codegen_field_access->field_index >= 0); |
| 219 | 219 | |
| 220 | *out_type_entry = codegen_field_access->type_struct_field->type_entry; | |
| 221 | ||
| 220 | 222 | add_debug_source_node(g, node); |
| 221 | 223 | return LLVMBuildStructGEP(g->builder, struct_ptr, codegen_field_access->field_index, ""); |
| 222 | 224 | } |
| ... | ... | @@ -243,34 +245,78 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node) { |
| 243 | 245 | zig_panic("gen_field_access_expr bad array field"); |
| 244 | 246 | } |
| 245 | 247 | } else if (struct_type->id == TypeTableEntryIdStruct) { |
| 246 | LLVMValueRef ptr = gen_field_ptr(g, node); | |
| 248 | TypeTableEntry *type_entry; | |
| 249 | LLVMValueRef ptr = gen_field_ptr(g, node, &type_entry); | |
| 247 | 250 | return LLVMBuildLoad(g->builder, ptr, ""); |
| 248 | 251 | } else { |
| 249 | 252 | zig_panic("gen_field_access_expr bad struct type"); |
| 250 | 253 | } |
| 251 | 254 | } |
| 252 | 255 | |
| 256 | static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *parent_node, AstNode *node, | |
| 257 | TypeTableEntry **out_type_entry) | |
| 258 | { | |
| 259 | LLVMValueRef target_ref; | |
| 260 | ||
| 261 | if (node->type == NodeTypeSymbol) { | |
| 262 | VariableTableEntry *var = find_variable(parent_node->codegen_node->expr_node.block_context, | |
| 263 | &node->data.symbol); | |
| 264 | ||
| 265 | // semantic checking ensures no variables are constant | |
| 266 | assert(!var->is_const); | |
| 267 | ||
| 268 | *out_type_entry = var->type; | |
| 269 | target_ref = var->value_ref; | |
| 270 | } else if (node->type == NodeTypeArrayAccessExpr) { | |
| 271 | TypeTableEntry *array_type = get_expr_type(node->data.array_access_expr.array_ref_expr); | |
| 272 | assert(array_type->id == TypeTableEntryIdArray); | |
| 273 | *out_type_entry = array_type->data.array.child_type; | |
| 274 | target_ref = gen_array_ptr(g, node); | |
| 275 | } else if (node->type == NodeTypeFieldAccessExpr) { | |
| 276 | target_ref = gen_field_ptr(g, node, out_type_entry); | |
| 277 | } else { | |
| 278 | zig_panic("bad assign target"); | |
| 279 | } | |
| 280 | ||
| 281 | return target_ref; | |
| 282 | } | |
| 283 | ||
| 253 | 284 | static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) { |
| 254 | 285 | assert(node->type == NodeTypePrefixOpExpr); |
| 255 | 286 | assert(node->data.prefix_op_expr.primary_expr); |
| 256 | 287 | |
| 257 | LLVMValueRef expr = gen_expr(g, node->data.prefix_op_expr.primary_expr); | |
| 288 | AstNode *expr_node = node->data.prefix_op_expr.primary_expr; | |
| 258 | 289 | |
| 259 | 290 | switch (node->data.prefix_op_expr.prefix_op) { |
| 291 | case PrefixOpInvalid: | |
| 292 | zig_unreachable(); | |
| 260 | 293 | case PrefixOpNegation: |
| 261 | add_debug_source_node(g, node); | |
| 262 | return LLVMBuildNeg(g->builder, expr, ""); | |
| 294 | { | |
| 295 | LLVMValueRef expr = gen_expr(g, expr_node); | |
| 296 | add_debug_source_node(g, node); | |
| 297 | return LLVMBuildNeg(g->builder, expr, ""); | |
| 298 | } | |
| 263 | 299 | case PrefixOpBoolNot: |
| 264 | 300 | { |
| 301 | LLVMValueRef expr = gen_expr(g, expr_node); | |
| 265 | 302 | LLVMValueRef zero = LLVMConstNull(LLVMTypeOf(expr)); |
| 266 | 303 | add_debug_source_node(g, node); |
| 267 | 304 | return LLVMBuildICmp(g->builder, LLVMIntEQ, expr, zero, ""); |
| 268 | 305 | } |
| 269 | 306 | case PrefixOpBinNot: |
| 270 | add_debug_source_node(g, node); | |
| 271 | return LLVMBuildNot(g->builder, expr, ""); | |
| 272 | case PrefixOpInvalid: | |
| 273 | zig_unreachable(); | |
| 307 | { | |
| 308 | LLVMValueRef expr = gen_expr(g, expr_node); | |
| 309 | add_debug_source_node(g, node); | |
| 310 | return LLVMBuildNot(g->builder, expr, ""); | |
| 311 | } | |
| 312 | case PrefixOpAddressOf: | |
| 313 | case PrefixOpConstAddressOf: | |
| 314 | { | |
| 315 | add_debug_source_node(g, node); | |
| 316 | TypeTableEntry *lvalue_type; | |
| 317 | return gen_lvalue(g, node, expr_node, &lvalue_type); | |
| 318 | } | |
| 319 | ||
| 274 | 320 | } |
| 275 | 321 | zig_unreachable(); |
| 276 | 322 | } |
| ... | ... | @@ -571,33 +617,14 @@ static LLVMValueRef gen_bool_or_expr(CodeGen *g, AstNode *expr_node) { |
| 571 | 617 | return phi; |
| 572 | 618 | } |
| 573 | 619 | |
| 574 | ||
| 575 | 620 | static LLVMValueRef gen_assign_expr(CodeGen *g, AstNode *node) { |
| 576 | 621 | assert(node->type == NodeTypeBinOpExpr); |
| 577 | 622 | |
| 578 | 623 | AstNode *lhs_node = node->data.bin_op_expr.op1; |
| 579 | 624 | |
| 580 | LLVMValueRef target_ref; | |
| 581 | 625 | TypeTableEntry *op1_type; |
| 582 | if (lhs_node->type == NodeTypeSymbol) { | |
| 583 | VariableTableEntry *var = find_variable(node->codegen_node->expr_node.block_context, | |
| 584 | &lhs_node->data.symbol); | |
| 585 | ||
| 586 | // semantic checking ensures no variables are constant | |
| 587 | assert(!var->is_const); | |
| 626 | LLVMValueRef target_ref = gen_lvalue(g, node, lhs_node, &op1_type); | |
| 588 | 627 | |
| 589 | op1_type = var->type; | |
| 590 | target_ref = var->value_ref; | |
| 591 | } else if (lhs_node->type == NodeTypeArrayAccessExpr) { | |
| 592 | TypeTableEntry *array_type = get_expr_type(lhs_node->data.array_access_expr.array_ref_expr); | |
| 593 | assert(array_type->id == TypeTableEntryIdArray); | |
| 594 | op1_type = array_type->data.array.child_type; | |
| 595 | target_ref = gen_array_ptr(g, lhs_node); | |
| 596 | } else if (lhs_node->type == NodeTypeFieldAccessExpr) { | |
| 597 | target_ref = gen_field_ptr(g, lhs_node); | |
| 598 | } else { | |
| 599 | zig_panic("bad assign target"); | |
| 600 | } | |
| 601 | 628 | LLVMValueRef value = gen_expr(g, node->data.bin_op_expr.op2); |
| 602 | 629 | |
| 603 | 630 | if (node->data.bin_op_expr.bin_op == BinOpTypeAssign) { |
src/parser.cpp+2| ... | ... | @@ -58,6 +58,8 @@ static const char *prefix_op_str(PrefixOp prefix_op) { |
| 58 | 58 | case PrefixOpNegation: return "-"; |
| 59 | 59 | case PrefixOpBoolNot: return "!"; |
| 60 | 60 | case PrefixOpBinNot: return "~"; |
| 61 | case PrefixOpAddressOf: return "&"; | |
| 62 | case PrefixOpConstAddressOf: return "&const"; | |
| 61 | 63 | } |
| 62 | 64 | zig_unreachable(); |
| 63 | 65 | } |
src/parser.hpp+2| ... | ... | @@ -198,6 +198,8 @@ enum PrefixOp { |
| 198 | 198 | PrefixOpBoolNot, |
| 199 | 199 | PrefixOpBinNot, |
| 200 | 200 | PrefixOpNegation, |
| 201 | PrefixOpAddressOf, | |
| 202 | PrefixOpConstAddressOf, | |
| 201 | 203 | }; |
| 202 | 204 | |
| 203 | 205 | struct AstNodePrefixOpExpr { |
test/run_tests.cpp+3-3| ... | ... | @@ -566,7 +566,7 @@ use "std.zig"; |
| 566 | 566 | |
| 567 | 567 | export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 { |
| 568 | 568 | var foo : Foo; |
| 569 | foo.a = foo.a + 1; | |
| 569 | foo.a += 1; | |
| 570 | 570 | foo.b = foo.a == 1; |
| 571 | 571 | test_foo(foo); |
| 572 | 572 | return 0; |
| ... | ... | @@ -749,7 +749,7 @@ fn f() { |
| 749 | 749 | const a = 3; |
| 750 | 750 | a = 4; |
| 751 | 751 | } |
| 752 | )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant variable"); | |
| 752 | )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant"); | |
| 753 | 753 | |
| 754 | 754 | add_compile_fail_case("use of undeclared identifier", R"SOURCE( |
| 755 | 755 | fn f() { |
| ... | ... | @@ -787,7 +787,7 @@ const x : i32 = 99; |
| 787 | 787 | fn f() { |
| 788 | 788 | x = 1; |
| 789 | 789 | } |
| 790 | )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant variable"); | |
| 790 | )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant"); | |
| 791 | 791 | |
| 792 | 792 | |
| 793 | 793 | add_compile_fail_case("missing else clause", R"SOURCE( |