authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-15 17:11:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-15 17:11:57-07:00
log423ee0689be4f93b2613a5e42e7411887a9a46ad
tree3e5e1c02c7e108968e699eb5d7c4844b1c9cd9a4
parent8a570c458bcf62bc3d882f8d253f75ec6d3fd719

add implicit casting support


5 files changed, 200 insertions(+), 84 deletions(-)

README.md+2
......@@ -39,6 +39,8 @@ compromises backward compatibility.
3939 provide a tag or sha1).
4040 * Include documentation generator.
4141 * Shebang line OK so language can be used for "scripting" as well.
42 * No null pointer. Convenient syntax for dealing with a maybe type so that
43 null pointer is not missed.
4244
4345### Current Status
4446
src/analyze.cpp+126-65
......@@ -551,22 +551,127 @@ static TypeTableEntry *get_return_type(BlockContext *context) {
551551 return return_type_node->codegen_node->data.type_node.entry;
552552}
553553
554static void check_type_compatibility(CodeGen *g, AstNode *node,
554static TypeTableEntry *determine_peer_type_compatibility(CodeGen *g, AstNode *node,
555 TypeTableEntry *type1, TypeTableEntry *type2)
556{
557 if (type1->id == TypeTableEntryIdInvalid ||
558 type2->id == TypeTableEntryIdInvalid)
559 {
560 return type1;
561 } else if (type1->id == TypeTableEntryIdUnreachable) {
562 return type2;
563 } else if (type2->id == TypeTableEntryIdUnreachable) {
564 return type1;
565 } else if (type1->id == TypeTableEntryIdInt &&
566 type2->id == TypeTableEntryIdInt &&
567 type1->data.integral.is_signed == type2->data.integral.is_signed)
568 {
569 return (type1->size_in_bits > type2->size_in_bits) ? type1 : type2;
570 } else if (type1->id == TypeTableEntryIdFloat &&
571 type2->id == TypeTableEntryIdFloat)
572 {
573 return (type1->size_in_bits > type2->size_in_bits) ? type1 : type2;
574 } else if (type1->id == TypeTableEntryIdArray &&
575 type2->id == TypeTableEntryIdArray &&
576 type1 == type2)
577 {
578 return type1;
579 } else if (type1 == type2) {
580 return type1;
581 }
582
583 add_node_error(g, node,
584 buf_sprintf("ambiguous expression type: '%s' vs '%s'",
585 buf_ptr(&type1->name), buf_ptr(&type2->name)));
586
587 return g->builtin_types.entry_invalid;
588}
589
590static bool num_lit_fits_in_other_type(CodeGen *g, TypeTableEntry *literal_type, TypeTableEntry *other_type) {
591 NumLit num_lit = literal_type->data.num_lit.kind;
592 uint64_t lit_size_in_bits = num_lit_bit_count(num_lit);
593
594 switch (other_type->id) {
595 case TypeTableEntryIdInvalid:
596 case TypeTableEntryIdNumberLiteral:
597 zig_unreachable();
598 case TypeTableEntryIdVoid:
599 case TypeTableEntryIdBool:
600 case TypeTableEntryIdUnreachable:
601 case TypeTableEntryIdPointer:
602 case TypeTableEntryIdArray:
603 case TypeTableEntryIdStruct:
604 return false;
605 case TypeTableEntryIdInt:
606 if (is_num_lit_unsigned(num_lit)) {
607 return lit_size_in_bits <= other_type->size_in_bits;
608 } else {
609 return false;
610 }
611 case TypeTableEntryIdFloat:
612 if (is_num_lit_float(num_lit)) {
613 return lit_size_in_bits <= other_type->size_in_bits;
614 } else {
615 return false;
616 }
617 }
618 zig_unreachable();
619}
620
621static TypeTableEntry *resolve_type_compatibility(CodeGen *g, AstNode *node,
555622 TypeTableEntry *expected_type, TypeTableEntry *actual_type)
556623{
557624 if (expected_type == nullptr)
558 return; // anything will do
625 return actual_type; // anything will do
559626 if (expected_type == actual_type)
560 return; // match
627 return expected_type; // match
561628 if (expected_type->id == TypeTableEntryIdInvalid || actual_type->id == TypeTableEntryIdInvalid)
562 return; // already complained
629 return expected_type; // already complained
563630 if (actual_type->id == TypeTableEntryIdUnreachable)
564 return; // sorry toots; gotta run. good luck with that expected type.
631 return actual_type; // sorry toots; gotta run. good luck with that expected type.
632
633 if (actual_type->id == TypeTableEntryIdNumberLiteral &&
634 num_lit_fits_in_other_type(g, actual_type, expected_type))
635 {
636 return expected_type;
637 }
638
639 // implicit widening conversion
640 if (expected_type->id == TypeTableEntryIdInt &&
641 actual_type->id == TypeTableEntryIdInt &&
642 expected_type->data.integral.is_signed == actual_type->data.integral.is_signed &&
643 expected_type->size_in_bits > actual_type->size_in_bits)
644 {
645 node->codegen_node->expr_node.cast_type = expected_type;
646 node->codegen_node->expr_node.implicit_cast.op = CastOpIntWidenOrShorten;
647 return expected_type;
648 }
565649
566650 add_node_error(g, node,
567651 buf_sprintf("expected type '%s', got '%s'",
568652 buf_ptr(&expected_type->name),
569653 buf_ptr(&actual_type->name)));
654
655 return g->builtin_types.entry_invalid;
656}
657
658static TypeTableEntry *resolve_peer_type_compatibility(CodeGen *g, AstNode *parent_node,
659 AstNode *child1, AstNode *child2,
660 TypeTableEntry *type1, TypeTableEntry *type2)
661{
662 assert(type1);
663 assert(type2);
664
665 TypeTableEntry *parent_type = determine_peer_type_compatibility(g, parent_node, type1, type2);
666
667 if (parent_type->id == TypeTableEntryIdInvalid) {
668 return parent_type;
669 }
670
671 resolve_type_compatibility(g, child1, parent_type, type1);
672 resolve_type_compatibility(g, child2, parent_type, type2);
673
674 return parent_type;
570675}
571676
572677BlockContext *new_block_context(AstNode *node, BlockContext *parent) {
......@@ -624,39 +729,6 @@ static void get_struct_field(TypeTableEntry *struct_type, Buf *name, TypeStructF
624729 *out_i = -1;
625730}
626731
627static bool num_lit_fits_in_other_type(CodeGen *g, TypeTableEntry *literal_type, TypeTableEntry *other_type) {
628 NumLit num_lit = literal_type->data.num_lit.kind;
629 uint64_t lit_size_in_bits = num_lit_bit_count(num_lit);
630
631 switch (other_type->id) {
632 case TypeTableEntryIdInvalid:
633 case TypeTableEntryIdNumberLiteral:
634 zig_unreachable();
635 case TypeTableEntryIdVoid:
636 case TypeTableEntryIdBool:
637 case TypeTableEntryIdUnreachable:
638 case TypeTableEntryIdPointer:
639 case TypeTableEntryIdArray:
640 case TypeTableEntryIdStruct:
641 return false;
642 case TypeTableEntryIdInt:
643 if (is_num_lit_unsigned(num_lit)) {
644
645 return lit_size_in_bits <= other_type->size_in_bits;
646 } else {
647 return false;
648 }
649 case TypeTableEntryIdFloat:
650 if (is_num_lit_float(num_lit)) {
651 return lit_size_in_bits <= other_type->size_in_bits;
652 } else {
653 return false;
654 }
655 }
656 zig_unreachable();
657}
658
659
660732static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
661733 AstNode *node)
662734{
......@@ -1125,19 +1197,10 @@ static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry
11251197 buf_sprintf("number literal too large to be represented in any type"));
11261198 return g->builtin_types.entry_invalid;
11271199 } else if (expected_type) {
1128 if (expected_type->id == TypeTableEntryIdInvalid) {
1129 return g->builtin_types.entry_invalid;
1130 } else if (num_lit_fits_in_other_type(g, num_lit_type, expected_type)) {
1131 NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
1132 assert(!codegen_num_lit->resolved_type);
1133 codegen_num_lit->resolved_type = expected_type;
1134
1135 return expected_type;
1136 } else {
1137 add_node_error(g, node, buf_sprintf("expected type '%s', got '%s'",
1138 buf_ptr(&expected_type->name), buf_ptr(&num_lit_type->name)));
1139 return g->builtin_types.entry_invalid;
1140 }
1200 NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
1201 assert(!codegen_num_lit->resolved_type);
1202 codegen_num_lit->resolved_type = resolve_type_compatibility(g, node, expected_type, num_lit_type);
1203 return codegen_num_lit->resolved_type;
11411204 } else {
11421205 return num_lit_type;
11431206 }
......@@ -1154,6 +1217,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
11541217 BlockContext *child_context = new_block_context(node, context);
11551218 node->codegen_node->data.block_node.block_context = child_context;
11561219 return_type = g->builtin_types.entry_void;
1220
11571221 for (int i = 0; i < node->data.block.statements.length; i += 1) {
11581222 AstNode *child = node->data.block.statements.at(i);
11591223 if (child->type == NodeTypeLabel) {
......@@ -1172,7 +1236,9 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
11721236 add_node_error(g, first_executing_node(child), buf_sprintf("unreachable code"));
11731237 break;
11741238 }
1175 return_type = analyze_expression(g, import, child_context, nullptr, child);
1239 bool is_last = (i == node->data.block.statements.length - 1);
1240 TypeTableEntry *passed_expected_type = is_last ? expected_type : nullptr;
1241 return_type = analyze_expression(g, import, child_context, passed_expected_type, child);
11761242 }
11771243 break;
11781244 }
......@@ -1194,7 +1260,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
11941260 actual_return_type = g->builtin_types.entry_invalid;
11951261 }
11961262
1197 check_type_compatibility(g, node, expected_return_type, actual_return_type);
1263 resolve_type_compatibility(g, node, expected_return_type, actual_return_type);
11981264 } else {
11991265 add_node_error(g, node, buf_sprintf("return expression outside function definition"));
12001266 }
......@@ -1387,22 +1453,17 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
13871453 else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node);
13881454 } else {
13891455 else_type = g->builtin_types.entry_void;
1456 else_type = resolve_type_compatibility(g, node, expected_type, else_type);
13901457 }
13911458
13921459
1393 TypeTableEntry *primary_type;
1394 TypeTableEntry *other_type;
1395 if (then_type->id == TypeTableEntryIdUnreachable) {
1396 primary_type = else_type;
1397 other_type = then_type;
1460 if (expected_type) {
1461 return_type = (then_type->id == TypeTableEntryIdUnreachable) ? else_type : then_type;
13981462 } else {
1399 primary_type = then_type;
1400 other_type = else_type;
1463 return_type = resolve_peer_type_compatibility(g, node,
1464 node->data.if_expr.then_block, node->data.if_expr.else_node,
1465 then_type, else_type);
14011466 }
1402
1403 check_type_compatibility(g, node, primary_type, other_type);
1404 check_type_compatibility(g, node, expected_type, other_type);
1405 return_type = primary_type;
14061467 break;
14071468 }
14081469 case NodeTypeDirective:
......@@ -1421,7 +1482,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
14211482 zig_unreachable();
14221483 }
14231484 assert(return_type);
1424 check_type_compatibility(g, node, expected_type, return_type);
1485 resolve_type_compatibility(g, node, expected_type, return_type);
14251486
14261487 node->codegen_node->expr_node.type_entry = return_type;
14271488 node->codegen_node->expr_node.block_context = context;
src/analyze.hpp+12-6
......@@ -145,6 +145,7 @@ struct CodeGen {
145145 TypeTableEntry *entry_bool;
146146 TypeTableEntry *entry_u8;
147147 TypeTableEntry *entry_u64;
148 TypeTableEntry *entry_i8;
148149 TypeTableEntry *entry_i32;
149150 TypeTableEntry *entry_i64;
150151 TypeTableEntry *entry_isize;
......@@ -230,12 +231,6 @@ struct FnDefNode {
230231 bool skip;
231232};
232233
233struct ExprNode {
234 TypeTableEntry *type_entry;
235 // the context in which this expression is evaluated.
236 // for blocks, this points to the containing scope, not the block's own scope for its children.
237 BlockContext *block_context;
238};
239234
240235struct AssignNode {
241236 VariableTableEntry *var_entry;
......@@ -268,6 +263,17 @@ struct CastNode {
268263 LLVMValueRef ptr;
269264};
270265
266struct ExprNode {
267 TypeTableEntry *type_entry;
268 // the context in which this expression is evaluated.
269 // for blocks, this points to the containing scope, not the block's own scope for its children.
270 BlockContext *block_context;
271
272 // may be null for no cast
273 TypeTableEntry *cast_type;
274 CastNode implicit_cast;
275};
276
271277struct NumberLiteralNode {
272278 TypeTableEntry *resolved_type;
273279};
src/codegen.cpp+49-11
......@@ -274,16 +274,9 @@ static LLVMValueRef gen_prefix_op_expr(CodeGen *g, AstNode *node) {
274274 zig_unreachable();
275275}
276276
277static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
278 assert(node->type == NodeTypeCastExpr);
279
280 LLVMValueRef expr_val = gen_expr(g, node->data.cast_expr.expr);
281
282 TypeTableEntry *actual_type = get_expr_type(node->data.cast_expr.expr);
283 TypeTableEntry *wanted_type = get_expr_type(node);
284
285 CastNode *cast_node = &node->codegen_node->data.cast_node;
286
277static LLVMValueRef gen_bare_cast(CodeGen *g, AstNode *node, LLVMValueRef expr_val,
278 TypeTableEntry *actual_type, TypeTableEntry *wanted_type, CastNode *cast_node)
279{
287280 switch (cast_node->op) {
288281 case CastOpNothing:
289282 return expr_val;
......@@ -327,6 +320,20 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
327320 zig_unreachable();
328321}
329322
323static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
324 assert(node->type == NodeTypeCastExpr);
325
326 LLVMValueRef expr_val = gen_expr(g, node->data.cast_expr.expr);
327
328 TypeTableEntry *actual_type = get_expr_type(node->data.cast_expr.expr);
329 TypeTableEntry *wanted_type = get_expr_type(node);
330
331 CastNode *cast_node = &node->codegen_node->data.cast_node;
332
333 return gen_bare_cast(g, node, expr_val, actual_type, wanted_type, cast_node);
334
335}
336
330337static LLVMValueRef gen_arithmetic_bin_op(CodeGen *g,
331338 LLVMValueRef val1, LLVMValueRef val2,
332339 TypeTableEntry *op1_type, TypeTableEntry *op2_type,
......@@ -871,7 +878,7 @@ static LLVMValueRef gen_asm_expr(CodeGen *g, AstNode *node) {
871878 return LLVMBuildCall(g->builder, asm_fn, param_values, input_and_output_count, "");
872879}
873880
874static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
881static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
875882 switch (node->type) {
876883 case NodeTypeBinOpExpr:
877884 return gen_bin_op_expr(g, node);
......@@ -1022,6 +1029,22 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
10221029 zig_unreachable();
10231030}
10241031
1032static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
1033 LLVMValueRef val = gen_expr_no_cast(g, node);
1034
1035 if (node->type == NodeTypeVoid) {
1036 return val;
1037 }
1038
1039 assert(node->codegen_node);
1040
1041 TypeTableEntry *actual_type = node->codegen_node->expr_node.type_entry;
1042 TypeTableEntry *cast_type = node->codegen_node->expr_node.cast_type;
1043
1044 return cast_type ? gen_bare_cast(g, node, val, actual_type, cast_type,
1045 &node->codegen_node->expr_node.implicit_cast) : val;
1046}
1047
10251048static void build_label_blocks(CodeGen *g, AstNode *block_node) {
10261049 assert(block_node->type == NodeTypeBlock);
10271050 for (int i = 0; i < block_node->data.block.statements.length; i += 1) {
......@@ -1322,6 +1345,19 @@ static void define_builtin_types(CodeGen *g) {
13221345 g->builtin_types.entry_u64 = entry;
13231346 }
13241347 g->builtin_types.entry_c_string_literal = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
1348 {
1349 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
1350 entry->type_ref = LLVMInt8Type();
1351 buf_init_from_str(&entry->name, "i8");
1352 entry->size_in_bits = 8;
1353 entry->align_in_bits = 8;
1354 entry->data.integral.is_signed = true;
1355 entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
1356 entry->size_in_bits, entry->align_in_bits,
1357 LLVMZigEncoding_DW_ATE_signed());
1358 g->type_table.put(&entry->name, entry);
1359 g->builtin_types.entry_i8 = entry;
1360 }
13251361 {
13261362 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
13271363 entry->type_ref = LLVMInt32Type();
......@@ -1951,6 +1987,8 @@ void codegen_link(CodeGen *g, const char *out_file) {
19511987 fprintf(stderr, "ld failed with return code %d\n", return_code);
19521988 fprintf(stderr, "%s\n", buf_ptr(&ld_stderr));
19531989 exit(1);
1990 } else if (buf_len(&ld_stderr)) {
1991 fprintf(stderr, "%s\n", buf_ptr(&ld_stderr));
19541992 }
19551993
19561994 if (g->out_type == OutTypeLib) {
test/run_tests.cpp+11-2
......@@ -56,7 +56,7 @@ static TestCase *add_simple_case(const char *case_name, const char *source, cons
5656 test_case->compiler_args.append(tmp_exe_path);
5757 test_case->compiler_args.append("--release");
5858 test_case->compiler_args.append("--strip");
59 test_case->compiler_args.append("--verbose");
59 //test_case->compiler_args.append("--verbose");
6060 test_case->compiler_args.append("--color");
6161 test_case->compiler_args.append("on");
6262
......@@ -86,7 +86,7 @@ static TestCase *add_compile_fail_case(const char *case_name, const char *source
8686 test_case->compiler_args.append(tmp_exe_path);
8787 test_case->compiler_args.append("--release");
8888 test_case->compiler_args.append("--strip");
89 test_case->compiler_args.append("--verbose");
89 //test_case->compiler_args.append("--verbose");
9090
9191 test_cases.append(test_case);
9292
......@@ -788,6 +788,15 @@ fn f() {
788788 x = 1;
789789}
790790 )SOURCE", 1, ".tmp_source.zig:4:5: error: cannot assign to constant variable");
791
792
793 add_compile_fail_case("missing else clause", R"SOURCE(
794fn f() {
795 const x : i32 = if true { 1 };
796 const y = if true { 1 as i32 };
797}
798 )SOURCE", 2, ".tmp_source.zig:3:21: error: expected type 'i32', got 'void'",
799 ".tmp_source.zig:4:15: error: ambiguous expression type: 'i32' vs 'void'");
791800}
792801
793802static void print_compiler_invocation(TestCase *test_case) {