authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-15 17:29:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-15 17:29:44-07:00
log431d8f946fc1340475bd6d849d74a741fd119251
tree13f42f86e534e3917bcfd7dbfe5a8a96668fb2ef
parent423ee0689be4f93b2613a5e42e7411887a9a46ad

implicit casting from constant size array to string

closes #36

5 files changed, 98 insertions(+), 86 deletions(-)

example/hello_world/hello.zig+1-2
......@@ -3,7 +3,6 @@ export executable "hello";
33use "std.zig";
44
55pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
6 // TODO implicit coercion from array to string
7 print_str("Hello, world!\n" as string);
6 print_str("Hello, world!\n");
87 return 0;
98}
src/analyze.cpp+31-17
......@@ -618,7 +618,7 @@ static bool num_lit_fits_in_other_type(CodeGen *g, TypeTableEntry *literal_type,
618618 zig_unreachable();
619619}
620620
621static TypeTableEntry *resolve_type_compatibility(CodeGen *g, AstNode *node,
621static TypeTableEntry *resolve_type_compatibility(CodeGen *g, BlockContext *context, AstNode *node,
622622 TypeTableEntry *expected_type, TypeTableEntry *actual_type)
623623{
624624 if (expected_type == nullptr)
......@@ -642,8 +642,21 @@ static TypeTableEntry *resolve_type_compatibility(CodeGen *g, AstNode *node,
642642 expected_type->data.integral.is_signed == actual_type->data.integral.is_signed &&
643643 expected_type->size_in_bits > actual_type->size_in_bits)
644644 {
645 node->codegen_node->expr_node.cast_type = expected_type;
645 node->codegen_node->expr_node.implicit_cast.type = expected_type;
646646 node->codegen_node->expr_node.implicit_cast.op = CastOpIntWidenOrShorten;
647 node->codegen_node->expr_node.implicit_cast.source_node = node;
648 return expected_type;
649 }
650
651 // implicit constant sized array to string conversion
652 if (expected_type == g->builtin_types.entry_string &&
653 actual_type->id == TypeTableEntryIdArray &&
654 actual_type->data.array.child_type == g->builtin_types.entry_u8)
655 {
656 node->codegen_node->expr_node.implicit_cast.type = expected_type;
657 node->codegen_node->expr_node.implicit_cast.op = CastOpArrayToString;
658 node->codegen_node->expr_node.implicit_cast.source_node = node;
659 context->cast_expr_alloca_list.append(&node->codegen_node->expr_node.implicit_cast);
647660 return expected_type;
648661 }
649662
......@@ -655,7 +668,8 @@ static TypeTableEntry *resolve_type_compatibility(CodeGen *g, AstNode *node,
655668 return g->builtin_types.entry_invalid;
656669}
657670
658static TypeTableEntry *resolve_peer_type_compatibility(CodeGen *g, AstNode *parent_node,
671static TypeTableEntry *resolve_peer_type_compatibility(CodeGen *g, BlockContext *block_context,
672 AstNode *parent_node,
659673 AstNode *child1, AstNode *child2,
660674 TypeTableEntry *type1, TypeTableEntry *type2)
661675{
......@@ -668,8 +682,8 @@ static TypeTableEntry *resolve_peer_type_compatibility(CodeGen *g, AstNode *pare
668682 return parent_type;
669683 }
670684
671 resolve_type_compatibility(g, child1, parent_type, type1);
672 resolve_type_compatibility(g, child2, parent_type, type2);
685 resolve_type_compatibility(g, block_context, child1, parent_type, type1);
686 resolve_type_compatibility(g, block_context, child2, parent_type, type2);
673687
674688 return parent_type;
675689}
......@@ -874,6 +888,8 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
874888 }
875889
876890 CastNode *cast_node = &node->codegen_node->data.cast_node;
891 cast_node->source_node = node;
892 cast_node->type = wanted_type;
877893
878894 // special casing this for now, TODO think about casting and do a general solution
879895 if (wanted_type == g->builtin_types.entry_isize &&
......@@ -891,7 +907,7 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B
891907 actual_type->data.array.child_type == g->builtin_types.entry_u8)
892908 {
893909 cast_node->op = CastOpArrayToString;
894 context->cast_expr_alloca_list.append(node);
910 context->cast_expr_alloca_list.append(cast_node);
895911 return wanted_type;
896912 } else if (actual_type->id == TypeTableEntryIdNumberLiteral &&
897913 num_lit_fits_in_other_type(g, actual_type, wanted_type))
......@@ -1022,10 +1038,8 @@ static TypeTableEntry *analyze_bin_op_expr(CodeGen *g, ImportTableEntry *import,
10221038 }
10231039 case BinOpTypeBoolOr:
10241040 case BinOpTypeBoolAnd:
1025 analyze_expression(g, import, context, g->builtin_types.entry_bool,
1026 node->data.bin_op_expr.op1);
1027 analyze_expression(g, import, context, g->builtin_types.entry_bool,
1028 node->data.bin_op_expr.op2);
1041 analyze_expression(g, import, context, g->builtin_types.entry_bool, node->data.bin_op_expr.op1);
1042 analyze_expression(g, import, context, g->builtin_types.entry_bool, node->data.bin_op_expr.op2);
10291043 return g->builtin_types.entry_bool;
10301044 case BinOpTypeCmpEq:
10311045 case BinOpTypeCmpNotEq:
......@@ -1188,8 +1202,8 @@ static VariableTableEntry *analyze_variable_declaration(CodeGen *g, ImportTableE
11881202 return nullptr;
11891203}
11901204
1191static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
1192 TypeTableEntry *expected_type, AstNode *node)
1205static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry *import,
1206 BlockContext *block_context, TypeTableEntry *expected_type, AstNode *node)
11931207{
11941208 TypeTableEntry *num_lit_type = g->num_lit_types[node->data.number_literal.kind];
11951209 if (node->data.number_literal.overflow) {
......@@ -1199,7 +1213,7 @@ static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry
11991213 } else if (expected_type) {
12001214 NumberLiteralNode *codegen_num_lit = &node->codegen_node->data.num_lit_node;
12011215 assert(!codegen_num_lit->resolved_type);
1202 codegen_num_lit->resolved_type = resolve_type_compatibility(g, node, expected_type, num_lit_type);
1216 codegen_num_lit->resolved_type = resolve_type_compatibility(g, block_context, node, expected_type, num_lit_type);
12031217 return codegen_num_lit->resolved_type;
12041218 } else {
12051219 return num_lit_type;
......@@ -1260,7 +1274,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
12601274 actual_return_type = g->builtin_types.entry_invalid;
12611275 }
12621276
1263 resolve_type_compatibility(g, node, expected_return_type, actual_return_type);
1277 resolve_type_compatibility(g, context, node, expected_return_type, actual_return_type);
12641278 } else {
12651279 add_node_error(g, node, buf_sprintf("return expression outside function definition"));
12661280 }
......@@ -1453,14 +1467,14 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
14531467 else_type = analyze_expression(g, import, context, expected_type, node->data.if_expr.else_node);
14541468 } else {
14551469 else_type = g->builtin_types.entry_void;
1456 else_type = resolve_type_compatibility(g, node, expected_type, else_type);
1470 else_type = resolve_type_compatibility(g, context, node, expected_type, else_type);
14571471 }
14581472
14591473
14601474 if (expected_type) {
14611475 return_type = (then_type->id == TypeTableEntryIdUnreachable) ? else_type : then_type;
14621476 } else {
1463 return_type = resolve_peer_type_compatibility(g, node,
1477 return_type = resolve_peer_type_compatibility(g, context, node,
14641478 node->data.if_expr.then_block, node->data.if_expr.else_node,
14651479 then_type, else_type);
14661480 }
......@@ -1482,7 +1496,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
14821496 zig_unreachable();
14831497 }
14841498 assert(return_type);
1485 resolve_type_compatibility(g, node, expected_type, return_type);
1499 resolve_type_compatibility(g, context, node, expected_type, return_type);
14861500
14871501 node->codegen_node->expr_node.type_entry = return_type;
14881502 node->codegen_node->expr_node.block_context = context;
src/analyze.hpp+4-2
......@@ -17,6 +17,7 @@ struct FnTableEntry;
1717struct BlockContext;
1818struct TypeTableEntry;
1919struct VariableTableEntry;
20struct CastNode;
2021
2122struct TypeTableEntryPointer {
2223 TypeTableEntry *pointer_child;
......@@ -213,7 +214,7 @@ struct BlockContext {
213214 FnTableEntry *fn_entry; // null at the module scope
214215 BlockContext *parent; // null when this is the root
215216 HashMap<Buf *, VariableTableEntry *, buf_hash, buf_eql_buf> variable_table;
216 ZigList<AstNode *> cast_expr_alloca_list;
217 ZigList<CastNode *> cast_expr_alloca_list;
217218 LLVMZigDIScope *di_scope;
218219};
219220
......@@ -261,6 +262,8 @@ struct CastNode {
261262 // if op is CastOpArrayToString, this will be a pointer to
262263 // the string struct on the stack
263264 LLVMValueRef ptr;
265 TypeTableEntry *type;
266 AstNode *source_node;
264267};
265268
266269struct ExprNode {
......@@ -270,7 +273,6 @@ struct ExprNode {
270273 BlockContext *block_context;
271274
272275 // may be null for no cast
273 TypeTableEntry *cast_type;
274276 CastNode implicit_cast;
275277};
276278
src/codegen.cpp+4-7
......@@ -1039,7 +1039,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
10391039 assert(node->codegen_node);
10401040
10411041 TypeTableEntry *actual_type = node->codegen_node->expr_node.type_entry;
1042 TypeTableEntry *cast_type = node->codegen_node->expr_node.cast_type;
1042 TypeTableEntry *cast_type = node->codegen_node->expr_node.implicit_cast.type;
10431043
10441044 return cast_type ? gen_bare_cast(g, node, val, actual_type, cast_type,
10451045 &node->codegen_node->expr_node.implicit_cast) : val;
......@@ -1248,12 +1248,9 @@ static void do_code_gen(CodeGen *g) {
12481248
12491249 // allocate structs which are the result of casts
12501250 for (int cea_i = 0; cea_i < block_context->cast_expr_alloca_list.length; cea_i += 1) {
1251 AstNode *cast_expr_node = block_context->cast_expr_alloca_list.at(cea_i);
1252 assert(cast_expr_node->type == NodeTypeCastExpr);
1253 CastNode *cast_codegen = &cast_expr_node->codegen_node->data.cast_node;
1254 TypeTableEntry *type_entry = get_type_for_type_node(g, cast_expr_node->data.cast_expr.type);
1255 add_debug_source_node(g, cast_expr_node);
1256 cast_codegen->ptr = LLVMBuildAlloca(g->builder, type_entry->type_ref, "");
1251 CastNode *cast_node = block_context->cast_expr_alloca_list.at(cea_i);
1252 add_debug_source_node(g, cast_node->source_node);
1253 cast_node->ptr = LLVMBuildAlloca(g->builder, cast_node->type->type_ref, "");
12571254 }
12581255 }
12591256
test/run_tests.cpp+58-58
......@@ -121,7 +121,7 @@ static void add_compiling_test_cases(void) {
121121 }
122122
123123 fn this_is_a_function() -> unreachable {
124 print_str("OK\n" as string);
124 print_str("OK\n");
125125 exit(0);
126126 }
127127 )SOURCE", "OK\n");
......@@ -137,7 +137,7 @@ static void add_compiling_test_cases(void) {
137137 /// this is a documentation comment
138138 /// doc comment line 2
139139 pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
140 print_str(/* mid-line comment /* nested */ */ "OK\n" as string);
140 print_str(/* mid-line comment /* nested */ */ "OK\n");
141141 return 0;
142142 }
143143 )SOURCE", "OK\n");
......@@ -185,17 +185,17 @@ static void add_compiling_test_cases(void) {
185185
186186 pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
187187 if 1 != 0 {
188 print_str("1 is true\n" as string);
188 print_str("1 is true\n");
189189 } else {
190 print_str("1 is false\n" as string);
190 print_str("1 is false\n");
191191 }
192192 if 0 != 0 {
193 print_str("0 is true\n" as string);
193 print_str("0 is true\n");
194194 } else if 1 - 1 != 0 {
195 print_str("1 - 1 is true\n" as string);
195 print_str("1 - 1 is true\n");
196196 }
197197 if !(0 != 0) {
198 print_str("!0 is true\n" as string);
198 print_str("!0 is true\n");
199199 }
200200 return 0;
201201 }
......@@ -210,7 +210,7 @@ static void add_compiling_test_cases(void) {
210210
211211 pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
212212 if add(22, 11) == 33 {
213 print_str("pass\n" as string);
213 print_str("pass\n");
214214 }
215215 return 0;
216216 }
......@@ -223,7 +223,7 @@ static void add_compiling_test_cases(void) {
223223 if a == 0 {
224224 goto done;
225225 }
226 print_str("loop\n" as string);
226 print_str("loop\n");
227227 loop(a - 1);
228228
229229 done:
......@@ -243,7 +243,7 @@ pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
243243 const a : i32 = 1;
244244 const b = 2 as i32;
245245 if (a + b == 3) {
246 print_str("OK\n" as string);
246 print_str("OK\n");
247247 }
248248 return 0;
249249}
......@@ -253,10 +253,10 @@ pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
253253use "std.zig";
254254
255255pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
256 if (true) { print_str("OK 1\n" as string); }
257 if (false) { print_str("BAD 1\n" as string); }
258 if (!true) { print_str("BAD 2\n" as string); }
259 if (!false) { print_str("OK 2\n" as string); }
256 if (true) { print_str("OK 1\n"); }
257 if (false) { print_str("BAD 1\n"); }
258 if (!true) { print_str("BAD 2\n"); }
259 if (!false) { print_str("OK 2\n"); }
260260 return 0;
261261}
262262 )SOURCE", "OK 1\nOK 2\n");
......@@ -267,14 +267,14 @@ use "std.zig";
267267pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
268268 if (true) {
269269 const no_conflict : i32 = 5;
270 if (no_conflict == 5) { print_str("OK 1\n" as string); }
270 if (no_conflict == 5) { print_str("OK 1\n"); }
271271 }
272272
273273 const c = {
274274 const no_conflict = 10 as i32;
275275 no_conflict
276276 };
277 if (c == 10) { print_str("OK 2\n" as string); }
277 if (c == 10) { print_str("OK 2\n"); }
278278 return 0;
279279}
280280 )SOURCE", "OK 1\nOK 2\n");
......@@ -290,7 +290,7 @@ pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
290290fn void_fun(a : i32, b : void, c : i32) {
291291 const v = b;
292292 const vv : void = if (a == 1) {v} else {};
293 if (a + c == 3) { print_str("OK\n" as string); }
293 if (a + c == 3) { print_str("OK\n"); }
294294 return vv;
295295}
296296 )SOURCE", "OK\n");
......@@ -300,14 +300,14 @@ use "std.zig";
300300
301301pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
302302 var zero : i32;
303 if (zero == 0) { print_str("zero\n" as string); }
303 if (zero == 0) { print_str("zero\n"); }
304304
305305 var i = 0 as i32;
306306loop_start:
307307 if i == 3 {
308308 goto done;
309309 }
310 print_str("loop\n" as string);
310 print_str("loop\n");
311311 i = i + 1;
312312 goto loop_start;
313313done:
......@@ -346,7 +346,7 @@ loop_2_start:
346346loop_2_end:
347347
348348 if accumulator == 15 {
349 print_str("OK\n" as string);
349 print_str("OK\n");
350350 }
351351
352352 return 0;
......@@ -358,7 +358,7 @@ loop_2_end:
358358use "std.zig";
359359
360360export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
361 print_str("Hello, world!\n" as string);
361 print_str("Hello, world!\n");
362362 return 0;
363363}
364364 )SOURCE", "Hello, world!\n");
......@@ -368,20 +368,20 @@ export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
368368use "std.zig";
369369
370370export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
371 if false || false || false { print_str("BAD 1\n" as string); }
372 if true && true && false { print_str("BAD 2\n" as string); }
373 if 1 | 2 | 4 != 7 { print_str("BAD 3\n" as string); }
374 if 3 ^ 6 ^ 8 != 13 { print_str("BAD 4\n" as string); }
375 if 7 & 14 & 28 != 4 { print_str("BAD 5\n" as string); }
376 if 9 << 1 << 2 != 9 << 3 { print_str("BAD 6\n" as string); }
377 if 90 >> 1 >> 2 != 90 >> 3 { print_str("BAD 7\n" as string); }
378 if 100 - 1 + 1000 != 1099 { print_str("BAD 8\n" as string); }
379 if 5 * 4 / 2 % 3 != 1 { print_str("BAD 9\n" as string); }
380 if 5 as i32 as i32 != 5 { print_str("BAD 10\n" as string); }
381 if !!false { print_str("BAD 11\n" as string); }
382 if 7 != --7 { print_str("BAD 12\n" as string); }
383
384 print_str("OK\n" as string);
371 if false || false || false { print_str("BAD 1\n"); }
372 if true && true && false { print_str("BAD 2\n"); }
373 if 1 | 2 | 4 != 7 { print_str("BAD 3\n"); }
374 if 3 ^ 6 ^ 8 != 13 { print_str("BAD 4\n"); }
375 if 7 & 14 & 28 != 4 { print_str("BAD 5\n"); }
376 if 9 << 1 << 2 != 9 << 3 { print_str("BAD 6\n"); }
377 if 90 >> 1 >> 2 != 90 >> 3 { print_str("BAD 7\n"); }
378 if 100 - 1 + 1000 != 1099 { print_str("BAD 8\n"); }
379 if 5 * 4 / 2 % 3 != 1 { print_str("BAD 9\n"); }
380 if 5 as i32 as i32 != 5 { print_str("BAD 10\n"); }
381 if !!false { print_str("BAD 11\n"); }
382 if 7 != --7 { print_str("BAD 12\n"); }
383
384 print_str("OK\n");
385385 return 0;
386386}
387387 )SOURCE", "OK\n");
......@@ -390,19 +390,19 @@ export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
390390use "std.zig";
391391
392392export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
393 if true || { print_str("BAD 1\n" as string); false } {
394 print_str("OK 1\n" as string);
393 if true || { print_str("BAD 1\n"); false } {
394 print_str("OK 1\n");
395395 }
396 if false || { print_str("OK 2\n" as string); false } {
397 print_str("BAD 2\n" as string);
396 if false || { print_str("OK 2\n"); false } {
397 print_str("BAD 2\n");
398398 }
399399
400 if true && { print_str("OK 3\n" as string); false } {
401 print_str("BAD 3\n" as string);
400 if true && { print_str("OK 3\n"); false } {
401 print_str("BAD 3\n");
402402 }
403 if false && { print_str("BAD 4\n" as string); false } {
403 if false && { print_str("BAD 4\n"); false } {
404404 } else {
405 print_str("OK 4\n" as string);
405 print_str("OK 4\n");
406406 }
407407
408408 return 0;
......@@ -414,20 +414,20 @@ use "std.zig";
414414
415415export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
416416 var i : i32 = 0;
417 i += 5; if i != 5 { print_str("BAD +=\n" as string); }
418 i -= 2; if i != 3 { print_str("BAD -=\n" as string); }
419 i *= 20; if i != 60 { print_str("BAD *=\n" as string); }
420 i /= 3; if i != 20 { print_str("BAD /=\n" as string); }
421 i %= 11; if i != 9 { print_str("BAD %=\n" as string); }
422 i <<= 1; if i != 18 { print_str("BAD <<=\n" as string); }
423 i >>= 2; if i != 4 { print_str("BAD >>=\n" as string); }
417 i += 5; if i != 5 { print_str("BAD +=\n"); }
418 i -= 2; if i != 3 { print_str("BAD -=\n"); }
419 i *= 20; if i != 60 { print_str("BAD *=\n"); }
420 i /= 3; if i != 20 { print_str("BAD /=\n"); }
421 i %= 11; if i != 9 { print_str("BAD %=\n"); }
422 i <<= 1; if i != 18 { print_str("BAD <<=\n"); }
423 i >>= 2; if i != 4 { print_str("BAD >>=\n"); }
424424 i = 6;
425 i &= 5; if i != 4 { print_str("BAD &=\n" as string); }
426 i ^= 6; if i != 2 { print_str("BAD ^=\n" as string); }
425 i &= 5; if i != 4 { print_str("BAD &=\n"); }
426 i ^= 6; if i != 2 { print_str("BAD ^=\n"); }
427427 i = 6;
428 i |= 3; if i != 7 { print_str("BAD |=\n" as string); }
428 i |= 3; if i != 7 { print_str("BAD |=\n"); }
429429
430 print_str("OK\n" as string);
430 print_str("OK\n");
431431 return 0;
432432}
433433 )SOURCE", "OK\n");
......@@ -578,7 +578,7 @@ struct Foo {
578578}
579579fn test_foo(foo : Foo) {
580580 if foo.b {
581 print_str("OK\n" as string);
581 print_str("OK\n");
582582 }
583583}
584584 )SOURCE", "OK\n");
......@@ -590,10 +590,10 @@ const g1 : i32 = 1233 + 1;
590590var g2 : i32;
591591
592592export fn main(argc : isize, argv : &&u8, env : &&u8) -> i32 {
593 if g2 != 0 { print_str("BAD\n" as string); }
593 if g2 != 0 { print_str("BAD\n"); }
594594 g2 = g1;
595 if g2 != 1234 { print_str("BAD\n" as string); }
596 print_str("OK\n" as string);
595 if g2 != 1234 { print_str("BAD\n"); }
596 print_str("OK\n");
597597 return 0;
598598}
599599 )SOURCE", "OK\n");