authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-26 03:39:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-26 03:39:18-04:00
log7c53230a61568411e1375e1230df1ceaf1e462de
tree5a92be90eaaf6db480c95ba42e32a4f430fd1da7
parent5c04730534ea7933855429c5fc5dc7b22eba7bc2

introduce copyable concept

closes #103

13 files changed, 115 insertions(+), 65 deletions(-)

src/all_types.hpp+1
......@@ -1037,6 +1037,7 @@ struct TypeTableEntry {
10371037 ZigLLVMDIType *di_type;
10381038
10391039 bool zero_bits;
1040 bool is_copyable;
10401041
10411042 union {
10421043 TypeTableEntryPointer pointer;
src/analyze.cpp+30
......@@ -300,6 +300,18 @@ uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry) {
300300 return LLVMSizeOfTypeInBits(g->target_data_ref, canon_type->type_ref);
301301}
302302
303static bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry) {
304 type_ensure_zero_bits_known(g, type_entry);
305 if (!type_has_bits(type_entry))
306 return true;
307
308 if (!handle_is_ptr(type_entry))
309 return true;
310
311 ensure_complete_type(g, type_entry);
312 return type_entry->is_copyable;
313}
314
303315static bool is_slice(TypeTableEntry *type) {
304316 return type->id == TypeTableEntryIdStruct && type->data.structure.is_slice;
305317}
......@@ -336,6 +348,7 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type
336348 type_ensure_zero_bits_known(g, child_type);
337349
338350 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdPointer);
351 entry->is_copyable = true;
339352
340353 const char *const_str = is_const ? "const " : "";
341354 const char *volatile_str = is_volatile ? "volatile " : "";
......@@ -392,6 +405,7 @@ TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type) {
392405 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdMaybe);
393406 assert(child_type->type_ref);
394407 assert(child_type->di_type);
408 entry->is_copyable = type_is_copyable(g, child_type);
395409
396410 buf_resize(&entry->name, 0);
397411 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
......@@ -471,6 +485,7 @@ TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type) {
471485 return child_type->error_parent;
472486
473487 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdErrorUnion);
488 entry->is_copyable = true;
474489 assert(child_type->type_ref);
475490 assert(child_type->di_type);
476491 ensure_complete_type(g, child_type);
......@@ -557,6 +572,7 @@ TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t
557572
558573 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdArray);
559574 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
575 entry->is_copyable = false;
560576
561577 buf_resize(&entry->name, 0);
562578 buf_appendf(&entry->name, "[%" PRIu64 "]%s", array_size, buf_ptr(&child_type->name));
......@@ -614,6 +630,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
614630 } else if (is_const) {
615631 TypeTableEntry *var_peer = get_slice_type(g, child_type, false);
616632 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdStruct);
633 entry->is_copyable = true;
617634
618635 buf_resize(&entry->name, 0);
619636 buf_appendf(&entry->name, "[]const %s", buf_ptr(&child_type->name));
......@@ -629,6 +646,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
629646 return entry;
630647 } else {
631648 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdStruct);
649 entry->is_copyable = true;
632650
633651 // If the child type is []const T then we need to make sure the type ref
634652 // and debug info is the same as if the child type were []T.
......@@ -752,6 +770,7 @@ TypeTableEntry *get_typedecl_type(CodeGen *g, const char *name, TypeTableEntry *
752770
753771 buf_init_from_str(&entry->name, name);
754772
773 entry->is_copyable = type_is_copyable(g, child_type);
755774 entry->type_ref = child_type->type_ref;
756775 entry->di_type = child_type->di_type;
757776 entry->zero_bits = child_type->zero_bits;
......@@ -768,6 +787,7 @@ TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry) {
768787 return fn_type->data.fn.bound_fn_parent;
769788
770789 TypeTableEntry *bound_fn_type = new_type_table_entry(TypeTableEntryIdBoundFn);
790 bound_fn_type->is_copyable = false;
771791 bound_fn_type->data.bound_fn.fn_type = fn_type;
772792 bound_fn_type->zero_bits = true;
773793
......@@ -786,6 +806,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
786806 ensure_complete_type(g, fn_type_id->return_type);
787807
788808 TypeTableEntry *fn_type = new_type_table_entry(TypeTableEntryIdFn);
809 fn_type->is_copyable = true;
789810 fn_type->data.fn.fn_type_id = *fn_type_id;
790811
791812 if (fn_type_id->is_cold) {
......@@ -972,6 +993,7 @@ TypeTableEntry *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
972993
973994static TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
974995 TypeTableEntry *fn_type = new_type_table_entry(TypeTableEntryIdFn);
996 fn_type->is_copyable = false;
975997 buf_init_from_str(&fn_type->name, "fn(");
976998 size_t i = 0;
977999 for (; i < fn_type_id->next_param_index; i += 1) {
......@@ -1078,6 +1100,12 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
10781100 case TypeTableEntryIdFn:
10791101 case TypeTableEntryIdTypeDecl:
10801102 case TypeTableEntryIdEnumTag:
1103 ensure_complete_type(g, type_entry);
1104 if (!fn_type_id.is_extern && !type_is_copyable(g, type_entry)) {
1105 add_node_error(g, param_node->data.param_decl.type,
1106 buf_sprintf("type '%s' is not copyable; cannot pass by value", buf_ptr(&type_entry->name)));
1107 return g->builtin_types.entry_invalid;
1108 }
10811109 break;
10821110 }
10831111 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
......@@ -1159,6 +1187,7 @@ static TypeTableEntry *create_enum_tag_type(CodeGen *g, TypeTableEntry *enum_typ
11591187 buf_resize(&entry->name, 0);
11601188 buf_appendf(&entry->name, "@enumTagType(%s)", buf_ptr(&enum_type->name));
11611189
1190 entry->is_copyable = true;
11621191 entry->data.enum_tag.enum_type = enum_type;
11631192 entry->data.enum_tag.int_type = int_type;
11641193 entry->type_ref = int_type->type_ref;
......@@ -4007,6 +4036,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
40074036
40084037TypeTableEntry *make_int_type(CodeGen *g, bool is_signed, size_t size_in_bits) {
40094038 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdInt);
4039 entry->is_copyable = true;
40104040 entry->type_ref = LLVMIntType(size_in_bits);
40114041
40124042 const char u_or_i = is_signed ? 'i' : 'u';
src/codegen.cpp+18-3
......@@ -261,6 +261,13 @@ static void addLLVMArgAttr(LLVMValueRef arg_val, unsigned param_index, const cha
261261 return addLLVMAttr(arg_val, param_index + 1, attr_name);
262262}
263263
264static void addLLVMCallsiteAttr(LLVMValueRef call_instr, unsigned param_index, const char *attr_name) {
265 unsigned kind_id = LLVMGetEnumAttributeKindForName(attr_name, strlen(attr_name));
266 assert(kind_id != 0);
267 LLVMAttributeRef llvm_attr = LLVMCreateEnumAttribute(LLVMGetGlobalContext(), kind_id, 0);
268 LLVMAddCallSiteAttribute(call_instr, param_index + 1, llvm_attr);
269}
270
264271static Buf *get_mangled_name(CodeGen *g, Buf *original_name, bool external_linkage) {
265272 if (external_linkage || g->external_symbol_names.maybe_get(original_name) == nullptr) {
266273 return original_name;
......@@ -1578,11 +1585,12 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
15781585 fn_type = instruction->fn_ref->value.type;
15791586 }
15801587
1581 TypeTableEntry *src_return_type = fn_type->data.fn.fn_type_id.return_type;
1588 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
1589 TypeTableEntry *src_return_type = fn_type_id->return_type;
15821590 bool ret_has_bits = type_has_bits(src_return_type);
15831591 bool first_arg_ret = ret_has_bits && handle_is_ptr(src_return_type);
15841592 size_t actual_param_count = instruction->arg_count + (first_arg_ret ? 1 : 0);
1585 bool is_var_args = fn_type->data.fn.fn_type_id.is_var_args;
1593 bool is_var_args = fn_type_id->is_var_args;
15861594 LLVMValueRef *gen_param_values = allocate<LLVMValueRef>(actual_param_count);
15871595 size_t gen_param_index = 0;
15881596 if (first_arg_ret) {
......@@ -1603,6 +1611,13 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
16031611 LLVMValueRef result = ZigLLVMBuildCall(g->builder, fn_val,
16041612 gen_param_values, gen_param_index, fn_type->data.fn.calling_convention, "");
16051613
1614 for (size_t param_i = 0; param_i < fn_type_id->param_count; param_i += 1) {
1615 FnGenParamInfo *gen_info = &fn_type->data.fn.gen_param_info[param_i];
1616 if (gen_info->is_byval) {
1617 addLLVMCallsiteAttr(result, gen_info->gen_index, "byval");
1618 }
1619 }
1620
16061621 if (src_return_type->id == TypeTableEntryIdUnreachable) {
16071622 return LLVMBuildUnreachable(g->builder);
16081623 } else if (!ret_has_bits) {
......@@ -3376,7 +3391,7 @@ static void do_code_gen(CodeGen *g) {
33763391 addLLVMArgAttr(fn_val, gen_index, "nonnull");
33773392 }
33783393 if (is_byval) {
3379 // TODO add byval attr?
3394 addLLVMArgAttr(fn_val, gen_index, "byval");
33803395 }
33813396 }
33823397
src/ir.cpp+1-1
......@@ -9905,7 +9905,6 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
99059905 case TypeTableEntryIdNumLitInt:
99069906 case TypeTableEntryIdBoundFn:
99079907 case TypeTableEntryIdMetaType:
9908 case TypeTableEntryIdFn:
99099908 case TypeTableEntryIdNamespace:
99109909 case TypeTableEntryIdArgTuple:
99119910 ir_add_error_node(ira, size_of_instruction->base.source_node,
......@@ -9925,6 +9924,7 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
99259924 case TypeTableEntryIdEnum:
99269925 case TypeTableEntryIdUnion:
99279926 case TypeTableEntryIdEnumTag:
9927 case TypeTableEntryIdFn:
99289928 {
99299929 uint64_t size_in_bytes = type_size(ira->codegen, type_entry);
99309930 ConstExprValue *out_val = ir_build_const_from(ira, &size_of_instruction->base);
std/list.zig+2-2
......@@ -34,9 +34,9 @@ pub fn List(comptime T: type) -> type{
3434 return l.items[0...l.len];
3535 }
3636
37 pub fn append(l: &Self, item: T) -> %void {
37 pub fn append(l: &Self, item: &const T) -> %void {
3838 const new_item_ptr = %return l.addOne();
39 *new_item_ptr = item;
39 *new_item_ptr = *item;
4040 }
4141
4242 pub fn resize(l: &Self, new_len: usize) -> %void {
test/cases/enum.zig+4-4
......@@ -48,15 +48,15 @@ test "constantEnumWithPayload" {
4848 shouldBeNotEmpty(full);
4949}
5050
51fn shouldBeEmpty(x: AnEnumWithPayload) {
52 switch (x) {
51fn shouldBeEmpty(x: &const AnEnumWithPayload) {
52 switch (*x) {
5353 AnEnumWithPayload.Empty => {},
5454 else => @unreachable(),
5555 }
5656}
5757
58fn shouldBeNotEmpty(x: AnEnumWithPayload) {
59 switch (x) {
58fn shouldBeNotEmpty(x: &const AnEnumWithPayload) {
59 switch (*x) {
6060 AnEnumWithPayload.Empty => @unreachable(),
6161 else => {},
6262 }
test/cases/incomplete_struct_param_tld.zig created+32
......@@ -0,0 +1,32 @@
1const assert = @import("std").debug.assert;
2
3const A = struct {
4 b: B,
5};
6
7const B = struct {
8 c: C,
9};
10
11const C = struct {
12 x: i32,
13
14 fn d(c: &const C) -> i32 {
15 return c.x;
16 }
17};
18
19fn foo(a: &const A) -> i32 {
20 return a.b.c.d();
21}
22
23test "incomplete struct param top level declaration" {
24 const a = A {
25 .b = B {
26 .c = C {
27 .x = 13,
28 },
29 },
30 };
31 assert(foo(a) == 13);
32}
test/cases/misc.zig+4-4
......@@ -338,8 +338,8 @@ const Test3Point = struct {
338338};
339339const test3_foo = Test3Foo.Three{Test3Point {.x = 3, .y = 4}};
340340const test3_bar = Test3Foo.Two{13};
341fn test3_1(f: Test3Foo) {
342 switch (f) {
341fn test3_1(f: &const Test3Foo) {
342 switch (*f) {
343343 Test3Foo.Three => |pt| {
344344 assert(pt.x == 3);
345345 assert(pt.y == 4);
......@@ -347,8 +347,8 @@ fn test3_1(f: Test3Foo) {
347347 else => @unreachable(),
348348 }
349349}
350fn test3_2(f: Test3Foo) {
351 switch (f) {
350fn test3_2(f: &const Test3Foo) {
351 switch (*f) {
352352 Test3Foo.Two => |x| {
353353 assert(x == 13);
354354 },
test/cases/null.zig+2-2
......@@ -62,8 +62,8 @@ fn foo(x: ?i32) -> ?bool {
6262test "ifVarMaybePointer" {
6363 assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
6464}
65fn shouldBeAPlus1(p: Particle) -> u64 {
66 var maybe_particle: ?Particle = p;
65fn shouldBeAPlus1(p: &const Particle) -> u64 {
66 var maybe_particle: ?Particle = *p;
6767 if (const *particle ?= maybe_particle) {
6868 particle.a += 1;
6969 }
test/cases/struct.zig+4-4
......@@ -53,10 +53,10 @@ const StructFoo = struct {
5353 b : bool,
5454 c : f32,
5555};
56fn testFoo(foo : StructFoo) {
56fn testFoo(foo: &const StructFoo) {
5757 assert(foo.b);
5858}
59fn testMutation(foo : &StructFoo) {
59fn testMutation(foo: &StructFoo) {
6060 foo.c = 100;
6161}
6262
......@@ -110,7 +110,7 @@ const Foo = struct {
110110
111111fn aFunc() -> i32 { 13 }
112112
113fn callStructField(foo: Foo) -> i32 {
113fn callStructField(foo: &const Foo) -> i32 {
114114 return foo.ptr();
115115}
116116
......@@ -123,7 +123,7 @@ test "storeMemberFunctionInVariable" {
123123}
124124const MemberFnTestFoo = struct {
125125 x: i32,
126 fn member(foo: MemberFnTestFoo) -> i32 { foo.x }
126 fn member(foo: &const MemberFnTestFoo) -> i32 { foo.x }
127127};
128128
129129
test/cases/switch.zig+2-2
......@@ -92,8 +92,8 @@ const SwitchProngWithVarEnum = enum {
9292 Two: f32,
9393 Meh,
9494};
95fn switchProngWithVarFn(a: SwitchProngWithVarEnum) {
96 switch(a) {
95fn switchProngWithVarFn(a: &const SwitchProngWithVarEnum) {
96 switch(*a) {
9797 SwitchProngWithVarEnum.One => |x| {
9898 if (x != 13) @unreachable();
9999 },
test/run_tests.cpp+14-43
......@@ -512,42 +512,6 @@ export fn main(argc: c_int, argv: &&u8) -> c_int {
512512 )SOURCE", "3.25\n3\n3.00\n-0.40\n");
513513
514514
515 add_simple_case("incomplete struct parameter top level decl", R"SOURCE(
516const io = @import("std").io;
517const A = struct {
518 b: B,
519};
520
521const B = struct {
522 c: C,
523};
524
525const C = struct {
526 x: i32,
527
528 fn d(c: &const C) {
529 %%io.stdout.printf("OK\n");
530 }
531};
532
533fn foo(a: A) {
534 a.b.c.d();
535}
536
537pub fn main(args: [][]u8) -> %void {
538 const a = A {
539 .b = B {
540 .c = C {
541 .x = 13,
542 },
543 },
544 };
545 foo(a);
546}
547
548 )SOURCE", "OK\n");
549
550
551515 add_simple_case("same named methods in incomplete struct", R"SOURCE(
552516const io = @import("std").io;
553517
......@@ -1037,9 +1001,10 @@ export fn entry() -> usize { @sizeOf(@typeOf(a)) }
10371001 )SOURCE", 1, ".tmp_source.zig:4:11: error: expected array or C string literal, found 'usize'");
10381002
10391003 add_compile_fail_case("non compile time array concatenation", R"SOURCE(
1040fn f(s: [10]u8) -> []u8 {
1004fn f() -> []u8 {
10411005 s ++ "foo"
10421006}
1007var s: [10]u8 = undefined;
10431008export fn entry() -> usize { @sizeOf(@typeOf(f)) }
10441009 )SOURCE", 1, ".tmp_source.zig:3:5: error: unable to evaluate constant expression");
10451010
......@@ -1071,10 +1036,10 @@ const Foo = struct {
10711036 a: i32,
10721037 b: i32,
10731038
1074 fn member_a(foo: Foo) -> i32 {
1039 fn member_a(foo: &const Foo) -> i32 {
10751040 return foo.a;
10761041 }
1077 fn member_b(foo: Foo) -> i32 {
1042 fn member_b(foo: &const Foo) -> i32 {
10781043 return foo.b;
10791044 }
10801045};
......@@ -1085,7 +1050,7 @@ const members = []member_fn_type {
10851050 Foo.member_b,
10861051};
10871052
1088fn f(foo: Foo, index: usize) {
1053fn f(foo: &const Foo, index: usize) {
10891054 const result = members[index]();
10901055}
10911056
......@@ -1335,15 +1300,15 @@ const EnumWithData = enum {
13351300 One,
13361301 Two: i32,
13371302};
1338fn bad_eql_2(a: EnumWithData, b: EnumWithData) -> bool {
1339 a == b
1303fn bad_eql_2(a: &const EnumWithData, b: &const EnumWithData) -> bool {
1304 *a == *b
13401305}
13411306
13421307export fn entry1() -> usize { @sizeOf(@typeOf(bad_eql_1)) }
13431308export fn entry2() -> usize { @sizeOf(@typeOf(bad_eql_2)) }
13441309 )SOURCE", 2,
13451310 ".tmp_source.zig:3:7: error: operator not allowed for type '[]u8'",
1346 ".tmp_source.zig:10:7: error: operator not allowed for type 'EnumWithData'");
1311 ".tmp_source.zig:10:8: error: operator not allowed for type 'EnumWithData'");
13471312
13481313 add_compile_fail_case("non-const switch number literal", R"SOURCE(
13491314export fn foo() {
......@@ -1845,6 +1810,12 @@ export fn bar() {}
18451810pub const baz = 1234;
18461811 )SOURCE");
18471812 }
1813
1814 add_compile_fail_case("pass non-copyable type by value to function", R"SOURCE(
1815const Point = struct { x: i32, y: i32, };
1816fn foo(p: Point) { }
1817export fn entry() -> usize { @sizeOf(@typeOf(foo)) }
1818 )SOURCE", 1, ".tmp_source.zig:3:11: error: type 'Point' is not copyable; cannot pass by value");
18481819}
18491820
18501821//////////////////////////////////////////////////////////////////////////////
test/self_hosted.zig+1
......@@ -15,6 +15,7 @@ const test_generics = @import("cases/generics.zig");
1515const test_goto = @import("cases/goto.zig");
1616const test_if = @import("cases/if.zig");
1717const test_import = @import("cases/import.zig");
18const test_incomplete_struct_param_tld = @import("cases/incomplete_struct_param_tld.zig");
1819const test_ir_block_deps = @import("cases/ir_block_deps.zig");
1920const test_math = @import("cases/math.zig");
2021const test_misc = @import("cases/misc.zig");