authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-06 11:09:14-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-06 11:09:14-05:00
log63f636e7b775bc8d2881104248f9579a089c4240
tree25e7432ccd228f0507aa3cb8665ad66762e29db6
parenta08b65720b7a6b78ff59b36fff1b3a6910483609
signaturelock-open Commit is signed but in an unrecognized format.

limit integer types to maximum bit width of 65535

closes #1541

14 files changed, 105 insertions(+), 145 deletions(-)

doc/langref.html.in+7-4
...@@ -8,6 +8,7 @@...@@ -8,6 +8,7 @@
8 body{8 body{
9 background-color:#111;9 background-color:#111;
10 color: #bbb;10 color: #bbb;
11 font-family: sans-serif;
11 }12 }
12 a {13 a {
13 color: #88f;14 color: #88f;
...@@ -467,9 +468,10 @@ pub fn main() void {...@@ -467,9 +468,10 @@ pub fn main() void {
467 <p>468 <p>
468 In addition to the integer types above, arbitrary bit-width integers can be referenced by using469 In addition to the integer types above, arbitrary bit-width integers can be referenced by using
469 an identifier of <code>i</code> or </code>u</code> followed by digits. For example, the identifier470 an identifier of <code>i</code> or </code>u</code> followed by digits. For example, the identifier
470 {#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer.471 {#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer. The maximum allowed bit-width of an
472 integer type is {#syntax#}65535{#endsyntax#}.
471 </p>473 </p>
472 {#see_also|Integers|Floats|void|Errors#}474 {#see_also|Integers|Floats|void|Errors|@IntType#}
473 {#header_close#}475 {#header_close#}
474 {#header_open|Primitive Values#}476 {#header_open|Primitive Values#}
475 <div class="table-wrapper">477 <div class="table-wrapper">
...@@ -5814,9 +5816,10 @@ fn add(a: i32, b: i32) i32 { return a + b; }...@@ -5814,9 +5816,10 @@ fn add(a: i32, b: i32) i32 { return a + b; }
5814 {#header_close#}5816 {#header_close#}
58155817
5816 {#header_open|@IntType#}5818 {#header_open|@IntType#}
5817 <pre>{#syntax#}@IntType(comptime is_signed: bool, comptime bit_count: u32) type{#endsyntax#}</pre>5819 <pre>{#syntax#}@IntType(comptime is_signed: bool, comptime bit_count: u16) type{#endsyntax#}</pre>
5818 <p>5820 <p>
5819 This function returns an integer type with the given signness and bit count.5821 This function returns an integer type with the given signness and bit count. The maximum
5822 bit count for an integer type is {#syntax#}65535{#endsyntax#}.
5820 </p>5823 </p>
5821 {#header_close#}5824 {#header_close#}
5822 {#header_open|@memberCount#}5825 {#header_open|@memberCount#}
src/all_types.hpp-1
...@@ -1222,7 +1222,6 @@ struct ZigType {...@@ -1222,7 +1222,6 @@ struct ZigType {
1222 ZigLLVMDIType *di_type;1222 ZigLLVMDIType *di_type;
12231223
1224 bool zero_bits; // this is denormalized data1224 bool zero_bits; // this is denormalized data
1225 bool is_copyable;
1226 bool gen_h_loop_flag;1225 bool gen_h_loop_flag;
12271226
1228 union {1227 union {
src/analyze.cpp+24-44
...@@ -367,23 +367,6 @@ uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {...@@ -367,23 +367,6 @@ uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {
367 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);367 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);
368}368}
369369
370Result<bool> type_is_copyable(CodeGen *g, ZigType *type_entry) {
371 Error err;
372 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
373 return err;
374
375 if (!type_has_bits(type_entry))
376 return true;
377
378 if (!handle_is_ptr(type_entry))
379 return true;
380
381 if ((err = ensure_complete_type(g, type_entry)))
382 return err;
383
384 return type_entry->is_copyable;
385}
386
387static bool is_slice(ZigType *type) {370static bool is_slice(ZigType *type) {
388 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;371 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
389}372}
...@@ -465,7 +448,6 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons...@@ -465,7 +448,6 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
465 assert(type_is_resolved(child_type, ResolveStatusZeroBitsKnown));448 assert(type_is_resolved(child_type, ResolveStatusZeroBitsKnown));
466449
467 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);450 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);
468 entry->is_copyable = true;
469451
470 const char *star_str = ptr_len == PtrLenSingle ? "*" : "[*]";452 const char *star_str = ptr_len == PtrLenSingle ? "*" : "[*]";
471 const char *const_str = is_const ? "const " : "";453 const char *const_str = is_const ? "const " : "";
...@@ -581,7 +563,6 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {...@@ -581,7 +563,6 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
581563
582 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);564 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);
583 assert(child_type->type_ref || child_type->zero_bits);565 assert(child_type->type_ref || child_type->zero_bits);
584 entry->is_copyable = type_is_copyable(g, child_type).unwrap();
585566
586 buf_resize(&entry->name, 0);567 buf_resize(&entry->name, 0);
587 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));568 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
...@@ -671,7 +652,6 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa...@@ -671,7 +652,6 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
671 }652 }
672653
673 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);654 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);
674 entry->is_copyable = true;
675 assert(payload_type->di_type);655 assert(payload_type->di_type);
676 assert(type_is_complete(payload_type));656 assert(type_is_complete(payload_type));
677657
...@@ -766,7 +746,6 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {...@@ -766,7 +746,6 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
766746
767 ZigType *entry = new_type_table_entry(ZigTypeIdArray);747 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
768 entry->zero_bits = (array_size == 0) || child_type->zero_bits;748 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
769 entry->is_copyable = false;
770749
771 buf_resize(&entry->name, 0);750 buf_resize(&entry->name, 0);
772 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));751 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));
...@@ -831,7 +810,6 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {...@@ -831,7 +810,6 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
831 }810 }
832811
833 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);812 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
834 entry->is_copyable = true;
835813
836 // replace the & with [] to go from a ptr type name to a slice type name814 // replace the & with [] to go from a ptr type name to a slice type name
837 buf_resize(&entry->name, 0);815 buf_resize(&entry->name, 0);
...@@ -986,7 +964,6 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c...@@ -986,7 +964,6 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
986 ImportTableEntry *import = scope ? get_scope_import(scope) : nullptr;964 ImportTableEntry *import = scope ? get_scope_import(scope) : nullptr;
987 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;965 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
988966
989 entry->is_copyable = false;
990 entry->type_ref = LLVMInt8Type();967 entry->type_ref = LLVMInt8Type();
991 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,968 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
992 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),969 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
...@@ -1005,7 +982,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {...@@ -1005,7 +982,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {
1005 return fn_type->data.fn.bound_fn_parent;982 return fn_type->data.fn.bound_fn_parent;
1006983
1007 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);984 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);
1008 bound_fn_type->is_copyable = false;
1009 bound_fn_type->data.bound_fn.fn_type = fn_type;985 bound_fn_type->data.bound_fn.fn_type = fn_type;
1010 bound_fn_type->zero_bits = true;986 bound_fn_type->zero_bits = true;
1011987
...@@ -1105,7 +1081,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1105,7 +1081,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1105 }1081 }
11061082
1107 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);1083 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1108 fn_type->is_copyable = true;
1109 fn_type->data.fn.fn_type_id = *fn_type_id;1084 fn_type->data.fn.fn_type_id = *fn_type_id;
11101085
1111 bool skip_debug_info = false;1086 bool skip_debug_info = false;
...@@ -1318,7 +1293,6 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {...@@ -1318,7 +1293,6 @@ ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
13181293
1319ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {1294ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1320 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);1295 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1321 fn_type->is_copyable = false;
1322 buf_resize(&fn_type->name, 0);1296 buf_resize(&fn_type->name, 0);
1323 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {1297 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
1324 const char *async_allocator_type_str = (fn_type->data.fn.fn_type_id.async_allocator_type == nullptr) ?1298 const char *async_allocator_type_str = (fn_type->data.fn.fn_type_id.async_allocator_type == nullptr) ?
...@@ -1526,7 +1500,6 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {...@@ -1526,7 +1500,6 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
1526 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);1500 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
1527 buf_resize(&err_set_type->name, 0);1501 buf_resize(&err_set_type->name, 0);
1528 buf_appendf(&err_set_type->name, "@typeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name));1502 buf_appendf(&err_set_type->name, "@typeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name));
1529 err_set_type->is_copyable = true;
1530 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;1503 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
1531 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;1504 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
1532 err_set_type->data.error_set.err_count = 0;1505 err_set_type->data.error_set.err_count = 0;
...@@ -2846,7 +2819,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2846,7 +2819,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2846 tag_type = new_type_table_entry(ZigTypeIdEnum);2819 tag_type = new_type_table_entry(ZigTypeIdEnum);
2847 buf_resize(&tag_type->name, 0);2820 buf_resize(&tag_type->name, 0);
2848 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));2821 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));
2849 tag_type->is_copyable = true;
2850 tag_type->type_ref = tag_int_type->type_ref;2822 tag_type->type_ref = tag_int_type->type_ref;
2851 tag_type->zero_bits = tag_int_type->zero_bits;2823 tag_type->zero_bits = tag_int_type->zero_bits;
28522824
...@@ -3366,10 +3338,10 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {...@@ -3366,10 +3338,10 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
3366 }3338 }
33673339
3368 {3340 {
3369 ZigType *type = get_primitive_type(g, tld->name);3341 ZigType *type;
3370 if (type != nullptr) {3342 if (get_primitive_type(g, tld->name, &type) != ErrorPrimitiveTypeNotFound) {
3371 add_node_error(g, tld->source_node,3343 add_node_error(g, tld->source_node,
3372 buf_sprintf("declaration shadows type '%s'", buf_ptr(&type->name)));3344 buf_sprintf("declaration shadows primitive type '%s'", buf_ptr(tld->name)));
3373 }3345 }
3374 }3346 }
3375}3347}
...@@ -3613,10 +3585,10 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf...@@ -3613,10 +3585,10 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
3613 add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));3585 add_error_note(g, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
3614 variable_entry->value->type = g->builtin_types.entry_invalid;3586 variable_entry->value->type = g->builtin_types.entry_invalid;
3615 } else {3587 } else {
3616 ZigType *type = get_primitive_type(g, name);3588 ZigType *type;
3617 if (type != nullptr) {3589 if (get_primitive_type(g, name, &type) != ErrorPrimitiveTypeNotFound) {
3618 add_node_error(g, source_node,3590 add_node_error(g, source_node,
3619 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));3591 buf_sprintf("variable shadows primitive type '%s'", buf_ptr(name)));
3620 variable_entry->value->type = g->builtin_types.entry_invalid;3592 variable_entry->value->type = g->builtin_types.entry_invalid;
3621 } else {3593 } else {
3622 Scope *search_scope = nullptr;3594 Scope *search_scope = nullptr;
...@@ -4435,6 +4407,7 @@ void semantic_analyze(CodeGen *g) {...@@ -4435,6 +4407,7 @@ void semantic_analyze(CodeGen *g) {
4435}4407}
44364408
4437ZigType *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {4409ZigType *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
4410 assert(size_in_bits <= 65535);
4438 TypeId type_id = {};4411 TypeId type_id = {};
4439 type_id.id = ZigTypeIdInt;4412 type_id.id = ZigTypeIdInt;
4440 type_id.data.integer.is_signed = is_signed;4413 type_id.data.integer.is_signed = is_signed;
...@@ -4523,7 +4496,7 @@ Buf *get_linux_libc_lib_path(const char *o_file) {...@@ -4523,7 +4496,7 @@ Buf *get_linux_libc_lib_path(const char *o_file) {
4523 Termination term;4496 Termination term;
4524 Buf *out_stderr = buf_alloc();4497 Buf *out_stderr = buf_alloc();
4525 Buf *out_stdout = buf_alloc();4498 Buf *out_stdout = buf_alloc();
4526 int err;4499 Error err;
4527 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {4500 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
4528 zig_panic("unable to determine libc lib path: executing C compiler: %s", err_str(err));4501 zig_panic("unable to determine libc lib path: executing C compiler: %s", err_str(err));
4529 }4502 }
...@@ -4552,7 +4525,7 @@ Buf *get_linux_libc_include_path(void) {...@@ -4552,7 +4525,7 @@ Buf *get_linux_libc_include_path(void) {
4552 Termination term;4525 Termination term;
4553 Buf *out_stderr = buf_alloc();4526 Buf *out_stderr = buf_alloc();
4554 Buf *out_stdout = buf_alloc();4527 Buf *out_stdout = buf_alloc();
4555 int err;4528 Error err;
4556 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {4529 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
4557 zig_panic("unable to determine libc include path: executing C compiler: %s", err_str(err));4530 zig_panic("unable to determine libc include path: executing C compiler: %s", err_str(err));
4558 }4531 }
...@@ -5977,8 +5950,8 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {...@@ -5977,8 +5950,8 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
5977}5950}
59785951
5979ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {5952ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
5953 assert(size_in_bits <= 65535);
5980 ZigType *entry = new_type_table_entry(ZigTypeIdInt);5954 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
5981 entry->is_copyable = true;
5982 entry->type_ref = (size_in_bits == 0) ? LLVMVoidType() : LLVMIntType(size_in_bits);5955 entry->type_ref = (size_in_bits == 0) ? LLVMVoidType() : LLVMIntType(size_in_bits);
5983 entry->zero_bits = (size_in_bits == 0);5956 entry->zero_bits = (size_in_bits == 0);
59845957
...@@ -6455,7 +6428,10 @@ bool fn_type_can_fail(FnTypeId *fn_type_id) {...@@ -6455,7 +6428,10 @@ bool fn_type_can_fail(FnTypeId *fn_type_id) {
6455 return type_can_fail(fn_type_id->return_type) || fn_type_id->cc == CallingConventionAsync;6428 return type_can_fail(fn_type_id->return_type) || fn_type_id->cc == CallingConventionAsync;
6456}6429}
64576430
6458ZigType *get_primitive_type(CodeGen *g, Buf *name) {6431// ErrorNone - result pointer has the type
6432// ErrorOverflow - an integer primitive type has too large a bit width
6433// ErrorPrimitiveTypeNotFound - result pointer unchanged
6434Error get_primitive_type(CodeGen *g, Buf *name, ZigType **result) {
6459 if (buf_len(name) >= 2) {6435 if (buf_len(name) >= 2) {
6460 uint8_t first_c = buf_ptr(name)[0];6436 uint8_t first_c = buf_ptr(name)[0];
6461 if (first_c == 'i' || first_c == 'u') {6437 if (first_c == 'i' || first_c == 'u') {
...@@ -6466,18 +6442,22 @@ ZigType *get_primitive_type(CodeGen *g, Buf *name) {...@@ -6466,18 +6442,22 @@ ZigType *get_primitive_type(CodeGen *g, Buf *name) {
6466 }6442 }
6467 }6443 }
6468 bool is_signed = (first_c == 'i');6444 bool is_signed = (first_c == 'i');
6469 uint32_t bit_count = atoi(buf_ptr(name) + 1);6445 unsigned long int bit_count = strtoul(buf_ptr(name) + 1, nullptr, 10);
6470 return get_int_type(g, is_signed, bit_count);6446 // strtoul returns ULONG_MAX on errors, so this comparison catches that as well.
6447 if (bit_count >= 65536) return ErrorOverflow;
6448 *result = get_int_type(g, is_signed, bit_count);
6449 return ErrorNone;
6471 }6450 }
6472 }6451 }
64736452
6474not_integer:6453not_integer:
64756454
6476 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);6455 auto primitive_table_entry = g->primitive_type_table.maybe_get(name);
6477 if (primitive_table_entry != nullptr) {6456 if (primitive_table_entry == nullptr)
6478 return primitive_table_entry->value;6457 return ErrorPrimitiveTypeNotFound;
6479 }6458
6480 return nullptr;6459 *result = primitive_table_entry->value;
6460 return ErrorNone;
6481}6461}
64826462
6483Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents) {6463Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents) {
src/analyze.hpp+1-3
...@@ -9,7 +9,6 @@...@@ -9,7 +9,6 @@
9#define ZIG_ANALYZE_HPP9#define ZIG_ANALYZE_HPP
1010
11#include "all_types.hpp"11#include "all_types.hpp"
12#include "result.hpp"
1312
14void semantic_analyze(CodeGen *g);13void semantic_analyze(CodeGen *g);
15ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);14ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
...@@ -180,7 +179,6 @@ ZigTypeId type_id_at_index(size_t index);...@@ -180,7 +179,6 @@ ZigTypeId type_id_at_index(size_t index);
180size_t type_id_len();179size_t type_id_len();
181size_t type_id_index(ZigType *entry);180size_t type_id_index(ZigType *entry);
182ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);181ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);
183Result<bool> type_is_copyable(CodeGen *g, ZigType *type_entry);
184LinkLib *create_link_lib(Buf *name);182LinkLib *create_link_lib(Buf *name);
185LinkLib *add_link_lib(CodeGen *codegen, Buf *lib);183LinkLib *add_link_lib(CodeGen *codegen, Buf *lib);
186184
...@@ -204,7 +202,7 @@ bool type_can_fail(ZigType *type_entry);...@@ -204,7 +202,7 @@ bool type_can_fail(ZigType *type_entry);
204bool fn_eval_cacheable(Scope *scope, ZigType *return_type);202bool fn_eval_cacheable(Scope *scope, ZigType *return_type);
205AstNode *type_decl_node(ZigType *type_entry);203AstNode *type_decl_node(ZigType *type_entry);
206204
207ZigType *get_primitive_type(CodeGen *g, Buf *name);205Error get_primitive_type(CodeGen *g, Buf *name, ZigType **result);
208206
209bool calling_convention_allows_zig_types(CallingConvention cc);207bool calling_convention_allows_zig_types(CallingConvention cc);
210const char *calling_convention_name(CallingConvention cc);208const char *calling_convention_name(CallingConvention cc);
src/codegen.cpp+3-3
...@@ -7326,7 +7326,7 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {...@@ -7326,7 +7326,7 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
7326 import->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));7326 import->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
73277327
7328 ZigList<ErrorMsg *> errors = {0};7328 ZigList<ErrorMsg *> errors = {0};
7329 int err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);7329 Error err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);
73307330
7331 if (err == ErrorCCompileErrors && errors.length > 0) {7331 if (err == ErrorCCompileErrors && errors.length > 0) {
7332 for (size_t i = 0; i < errors.length; i += 1) {7332 for (size_t i = 0; i < errors.length; i += 1) {
...@@ -7446,7 +7446,7 @@ static void gen_root_source(CodeGen *g) {...@@ -7446,7 +7446,7 @@ static void gen_root_source(CodeGen *g) {
7446 return;7446 return;
74477447
7448 Buf *source_code = buf_alloc();7448 Buf *source_code = buf_alloc();
7449 int err;7449 Error err;
7450 // No need for using the caching system for this file fetch because it is handled7450 // No need for using the caching system for this file fetch because it is handled
7451 // separately.7451 // separately.
7452 if ((err = os_fetch_file_path(resolved_path, source_code, true))) {7452 if ((err = os_fetch_file_path(resolved_path, source_code, true))) {
...@@ -7514,7 +7514,7 @@ void codegen_add_assembly(CodeGen *g, Buf *path) {...@@ -7514,7 +7514,7 @@ void codegen_add_assembly(CodeGen *g, Buf *path) {
75147514
7515static void gen_global_asm(CodeGen *g) {7515static void gen_global_asm(CodeGen *g) {
7516 Buf contents = BUF_INIT;7516 Buf contents = BUF_INIT;
7517 int err;7517 Error err;
7518 for (size_t i = 0; i < g->assembly_files.length; i += 1) {7518 for (size_t i = 0; i < g->assembly_files.length; i += 1) {
7519 Buf *asm_file = g->assembly_files.at(i);7519 Buf *asm_file = g->assembly_files.at(i);
7520 // No need to use the caching system for these fetches because they7520 // No need to use the caching system for these fetches because they
src/error.cpp+3-2
...@@ -7,8 +7,8 @@...@@ -7,8 +7,8 @@
77
8#include "error.hpp"8#include "error.hpp"
99
10const char *err_str(int err) {10const char *err_str(Error err) {
11 switch ((enum Error)err) {11 switch (err) {
12 case ErrorNone: return "(no error)";12 case ErrorNone: return "(no error)";
13 case ErrorNoMem: return "out of memory";13 case ErrorNoMem: return "out of memory";
14 case ErrorInvalidFormat: return "invalid format";14 case ErrorInvalidFormat: return "invalid format";
...@@ -32,6 +32,7 @@ const char *err_str(int err) {...@@ -32,6 +32,7 @@ const char *err_str(int err) {
32 case ErrorUnsupportedOperatingSystem: return "unsupported operating system";32 case ErrorUnsupportedOperatingSystem: return "unsupported operating system";
33 case ErrorSharingViolation: return "sharing violation";33 case ErrorSharingViolation: return "sharing violation";
34 case ErrorPipeBusy: return "pipe busy";34 case ErrorPipeBusy: return "pipe busy";
35 case ErrorPrimitiveTypeNotFound: return "primitive type not found";
35 }36 }
36 return "(invalid error)";37 return "(invalid error)";
37}38}
src/error.hpp+8-1
...@@ -8,6 +8,8 @@...@@ -8,6 +8,8 @@
8#ifndef ERROR_HPP8#ifndef ERROR_HPP
9#define ERROR_HPP9#define ERROR_HPP
1010
11#include <assert.h>
12
11enum Error {13enum Error {
12 ErrorNone,14 ErrorNone,
13 ErrorNoMem,15 ErrorNoMem,
...@@ -32,8 +34,13 @@ enum Error {...@@ -32,8 +34,13 @@ enum Error {
32 ErrorUnsupportedOperatingSystem,34 ErrorUnsupportedOperatingSystem,
33 ErrorSharingViolation,35 ErrorSharingViolation,
34 ErrorPipeBusy,36 ErrorPipeBusy,
37 ErrorPrimitiveTypeNotFound,
35};38};
3639
37const char *err_str(int err);40const char *err_str(Error err);
41
42static inline void assertNoError(Error err) {
43 assert(err == ErrorNone);
44}
3845
39#endif46#endif
src/ir.cpp+16-12
...@@ -3055,10 +3055,10 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s...@@ -3055,10 +3055,10 @@ static ZigVar *create_local_var(CodeGen *codegen, AstNode *node, Scope *parent_s
3055 add_error_note(codegen, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));3055 add_error_note(codegen, msg, existing_var->decl_node, buf_sprintf("previous declaration is here"));
3056 variable_entry->value->type = codegen->builtin_types.entry_invalid;3056 variable_entry->value->type = codegen->builtin_types.entry_invalid;
3057 } else {3057 } else {
3058 ZigType *type = get_primitive_type(codegen, name);3058 ZigType *type;
3059 if (type != nullptr) {3059 if (get_primitive_type(codegen, name, &type) != ErrorPrimitiveTypeNotFound) {
3060 add_node_error(codegen, node,3060 add_node_error(codegen, node,
3061 buf_sprintf("variable shadows type '%s'", buf_ptr(&type->name)));3061 buf_sprintf("variable shadows primitive type '%s'", buf_ptr(name)));
3062 variable_entry->value->type = codegen->builtin_types.entry_invalid;3062 variable_entry->value->type = codegen->builtin_types.entry_invalid;
3063 } else {3063 } else {
3064 Tld *tld = find_decl(codegen, parent_scope, name);3064 Tld *tld = find_decl(codegen, parent_scope, name);
...@@ -3488,6 +3488,7 @@ static IrInstruction *ir_gen_null_literal(IrBuilder *irb, Scope *scope, AstNode...@@ -3488,6 +3488,7 @@ static IrInstruction *ir_gen_null_literal(IrBuilder *irb, Scope *scope, AstNode
3488}3488}
34893489
3490static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {3490static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
3491 Error err;
3491 assert(node->type == NodeTypeSymbol);3492 assert(node->type == NodeTypeSymbol);
34923493
3493 Buf *variable_name = node->data.symbol_expr.symbol;3494 Buf *variable_name = node->data.symbol_expr.symbol;
...@@ -3501,8 +3502,15 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,...@@ -3501,8 +3502,15 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
3501 return &const_instruction->base;3502 return &const_instruction->base;
3502 }3503 }
35033504
3504 ZigType *primitive_type = get_primitive_type(irb->codegen, variable_name);3505 ZigType *primitive_type;
3505 if (primitive_type != nullptr) {3506 if ((err = get_primitive_type(irb->codegen, variable_name, &primitive_type))) {
3507 if (err == ErrorOverflow) {
3508 add_node_error(irb->codegen, node,
3509 buf_sprintf("primitive integer type '%s' exceeds maximum bit width of 65535",
3510 buf_ptr(variable_name)));
3511 return irb->codegen->invalid_instruction;
3512 }
3513 } else {
3506 IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_type);3514 IrInstruction *value = ir_build_const_type(irb, scope, node, primitive_type);
3507 if (lval == LValPtr) {3515 if (lval == LValPtr) {
3508 return ir_build_ref(irb, scope, node, value, false, false);3516 return ir_build_ref(irb, scope, node, value, false, false);
...@@ -6302,7 +6310,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp...@@ -6302,7 +6310,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
6302 }6310 }
6303 }6311 }
63046312
6305 err_set_type->is_copyable = true;
6306 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;6313 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
6307 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;6314 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
6308 err_set_type->data.error_set.err_count = count;6315 err_set_type->data.error_set.err_count = count;
...@@ -6341,7 +6348,6 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN...@@ -6341,7 +6348,6 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN
6341 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);6348 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
6342 buf_resize(&err_set_type->name, 0);6349 buf_resize(&err_set_type->name, 0);
6343 buf_appendf(&err_set_type->name, "error.{%s}", buf_ptr(&err_entry->name));6350 buf_appendf(&err_set_type->name, "error.{%s}", buf_ptr(&err_entry->name));
6344 err_set_type->is_copyable = true;
6345 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;6351 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
6346 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;6352 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
6347 err_set_type->data.error_set.err_count = 1;6353 err_set_type->data.error_set.err_count = 1;
...@@ -6362,7 +6368,6 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A...@@ -6362,7 +6368,6 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
6362 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error set", node);6368 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error set", node);
6363 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);6369 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
6364 buf_init_from_buf(&err_set_type->name, type_name);6370 buf_init_from_buf(&err_set_type->name, type_name);
6365 err_set_type->is_copyable = true;
6366 err_set_type->data.error_set.err_count = err_count;6371 err_set_type->data.error_set.err_count = err_count;
6367 err_set_type->type_ref = irb->codegen->builtin_types.entry_global_error_set->type_ref;6372 err_set_type->type_ref = irb->codegen->builtin_types.entry_global_error_set->type_ref;
6368 err_set_type->di_type = irb->codegen->builtin_types.entry_global_error_set->di_type;6373 err_set_type->di_type = irb->codegen->builtin_types.entry_global_error_set->di_type;
...@@ -8208,7 +8213,6 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp...@@ -8208,7 +8213,6 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
8208 }8213 }
8209 free(errors);8214 free(errors);
82108215
8211 err_set_type->is_copyable = true;
8212 err_set_type->type_ref = ira->codegen->builtin_types.entry_global_error_set->type_ref;8216 err_set_type->type_ref = ira->codegen->builtin_types.entry_global_error_set->type_ref;
8213 err_set_type->di_type = ira->codegen->builtin_types.entry_global_error_set->di_type;8217 err_set_type->di_type = ira->codegen->builtin_types.entry_global_error_set->di_type;
8214 err_set_type->data.error_set.err_count = intersection_list.length;8218 err_set_type->data.error_set.err_count = intersection_list.length;
...@@ -17650,7 +17654,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct...@@ -17650,7 +17654,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1765017654
17651 ZigList<ErrorMsg *> errors = {0};17655 ZigList<ErrorMsg *> errors = {0};
1765217656
17653 int err;17657 Error err;
17654 if ((err = parse_h_buf(child_import, &errors, &cimport_scope->buf, ira->codegen, node))) {17658 if ((err = parse_h_buf(child_import, &errors, &cimport_scope->buf, ira->codegen, node))) {
17655 if (err != ErrorCCompileErrors) {17659 if (err != ErrorCCompileErrors) {
17656 ir_add_error_node(ira, node, buf_sprintf("C import failed: %s", err_str(err)));17660 ir_add_error_node(ira, node, buf_sprintf("C import failed: %s", err_str(err)));
...@@ -17766,7 +17770,7 @@ static IrInstruction *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstru...@@ -17766,7 +17770,7 @@ static IrInstruction *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstru
1776617770
17767 // load from file system into const expr17771 // load from file system into const expr
17768 Buf *file_contents = buf_alloc();17772 Buf *file_contents = buf_alloc();
17769 int err;17773 Error err;
17770 if ((err = file_fetch(ira->codegen, file_path, file_contents))) {17774 if ((err = file_fetch(ira->codegen, file_path, file_contents))) {
17771 if (err == ErrorFileNotFound) {17775 if (err == ErrorFileNotFound) {
17772 ir_add_error(ira, instruction->name, buf_sprintf("unable to find '%s'", buf_ptr(file_path)));17776 ir_add_error(ira, instruction->name, buf_sprintf("unable to find '%s'", buf_ptr(file_path)));
...@@ -18252,7 +18256,7 @@ static IrInstruction *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruct...@@ -18252,7 +18256,7 @@ static IrInstruction *ir_analyze_instruction_int_type(IrAnalyze *ira, IrInstruct
1825218256
18253 IrInstruction *bit_count_value = instruction->bit_count->child;18257 IrInstruction *bit_count_value = instruction->bit_count->child;
18254 uint64_t bit_count;18258 uint64_t bit_count;
18255 if (!ir_resolve_unsigned(ira, bit_count_value, ira->codegen->builtin_types.entry_u32, &bit_count))18259 if (!ir_resolve_unsigned(ira, bit_count_value, ira->codegen->builtin_types.entry_u16, &bit_count))
18256 return ira->codegen->invalid_instruction;18260 return ira->codegen->invalid_instruction;
1825718261
18258 return ir_const_type(ira, &instruction->base, get_int_type(ira->codegen, is_signed, (uint32_t)bit_count));18262 return ir_const_type(ira, &instruction->base, get_int_type(ira->codegen, is_signed, (uint32_t)bit_count));
src/os.cpp+16-16
...@@ -793,7 +793,7 @@ Error os_file_exists(Buf *full_path, bool *result) {...@@ -793,7 +793,7 @@ Error os_file_exists(Buf *full_path, bool *result) {
793}793}
794794
795#if defined(ZIG_OS_POSIX)795#if defined(ZIG_OS_POSIX)
796static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,796static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
797 Termination *term, Buf *out_stderr, Buf *out_stdout)797 Termination *term, Buf *out_stderr, Buf *out_stdout)
798{798{
799 int stdin_pipe[2];799 int stdin_pipe[2];
...@@ -872,7 +872,7 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -872,7 +872,7 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,
872// LocalFree(messageBuffer);872// LocalFree(messageBuffer);
873//}873//}
874874
875static int os_exec_process_windows(const char *exe, ZigList<const char *> &args,875static Error os_exec_process_windows(const char *exe, ZigList<const char *> &args,
876 Termination *term, Buf *out_stderr, Buf *out_stdout)876 Termination *term, Buf *out_stderr, Buf *out_stdout)
877{877{
878 Buf command_line = BUF_INIT;878 Buf command_line = BUF_INIT;
...@@ -983,7 +983,7 @@ static int os_exec_process_windows(const char *exe, ZigList<const char *> &args,...@@ -983,7 +983,7 @@ static int os_exec_process_windows(const char *exe, ZigList<const char *> &args,
983 CloseHandle(piProcInfo.hProcess);983 CloseHandle(piProcInfo.hProcess);
984 CloseHandle(piProcInfo.hThread);984 CloseHandle(piProcInfo.hThread);
985985
986 return 0;986 return ErrorNone;
987}987}
988#endif988#endif
989989
...@@ -1003,7 +1003,7 @@ Error os_execv(const char *exe, const char **argv) {...@@ -1003,7 +1003,7 @@ Error os_execv(const char *exe, const char **argv) {
1003#endif1003#endif
1004}1004}
10051005
1006int os_exec_process(const char *exe, ZigList<const char *> &args,1006Error os_exec_process(const char *exe, ZigList<const char *> &args,
1007 Termination *term, Buf *out_stderr, Buf *out_stdout)1007 Termination *term, Buf *out_stderr, Buf *out_stdout)
1008{1008{
1009#if defined(ZIG_OS_WINDOWS)1009#if defined(ZIG_OS_WINDOWS)
...@@ -1027,7 +1027,7 @@ void os_write_file(Buf *full_path, Buf *contents) {...@@ -1027,7 +1027,7 @@ void os_write_file(Buf *full_path, Buf *contents) {
1027 zig_panic("close failed");1027 zig_panic("close failed");
1028}1028}
10291029
1030int os_copy_file(Buf *src_path, Buf *dest_path) {1030Error os_copy_file(Buf *src_path, Buf *dest_path) {
1031 FILE *src_f = fopen(buf_ptr(src_path), "rb");1031 FILE *src_f = fopen(buf_ptr(src_path), "rb");
1032 if (!src_f) {1032 if (!src_f) {
1033 int err = errno;1033 int err = errno;
...@@ -1074,7 +1074,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) {...@@ -1074,7 +1074,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) {
1074 if (feof(src_f)) {1074 if (feof(src_f)) {
1075 fclose(src_f);1075 fclose(src_f);
1076 fclose(dest_f);1076 fclose(dest_f);
1077 return 0;1077 return ErrorNone;
1078 }1078 }
1079 }1079 }
1080}1080}
...@@ -1197,7 +1197,7 @@ bool os_stderr_tty(void) {...@@ -1197,7 +1197,7 @@ bool os_stderr_tty(void) {
1197}1197}
11981198
1199#if defined(ZIG_OS_POSIX)1199#if defined(ZIG_OS_POSIX)
1200static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) {1200static Error os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
1201 const char *tmp_dir = getenv("TMPDIR");1201 const char *tmp_dir = getenv("TMPDIR");
1202 if (!tmp_dir) {1202 if (!tmp_dir) {
1203 tmp_dir = P_tmpdir;1203 tmp_dir = P_tmpdir;
...@@ -1221,12 +1221,12 @@ static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_pat...@@ -1221,12 +1221,12 @@ static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_pat
1221 if (fclose(f))1221 if (fclose(f))
1222 zig_panic("close failed");1222 zig_panic("close failed");
12231223
1224 return 0;1224 return ErrorNone;
1225}1225}
1226#endif1226#endif
12271227
1228#if defined(ZIG_OS_WINDOWS)1228#if defined(ZIG_OS_WINDOWS)
1229static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) {1229static Error os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
1230 char tmp_dir[MAX_PATH + 1];1230 char tmp_dir[MAX_PATH + 1];
1231 if (GetTempPath(MAX_PATH, tmp_dir) == 0) {1231 if (GetTempPath(MAX_PATH, tmp_dir) == 0) {
1232 zig_panic("GetTempPath failed");1232 zig_panic("GetTempPath failed");
...@@ -1255,11 +1255,11 @@ static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_p...@@ -1255,11 +1255,11 @@ static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_p
1255 if (fclose(f)) {1255 if (fclose(f)) {
1256 zig_panic("fclose failed");1256 zig_panic("fclose failed");
1257 }1257 }
1258 return 0;1258 return ErrorNone;
1259}1259}
1260#endif1260#endif
12611261
1262int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {1262Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
1263#if defined(ZIG_OS_WINDOWS)1263#if defined(ZIG_OS_WINDOWS)
1264 return os_buf_to_tmp_file_windows(contents, suffix, out_tmp_path);1264 return os_buf_to_tmp_file_windows(contents, suffix, out_tmp_path);
1265#elif defined(ZIG_OS_POSIX)1265#elif defined(ZIG_OS_POSIX)
...@@ -1269,17 +1269,17 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {...@@ -1269,17 +1269,17 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
1269#endif1269#endif
1270}1270}
12711271
1272int os_delete_file(Buf *path) {1272Error os_delete_file(Buf *path) {
1273 if (remove(buf_ptr(path))) {1273 if (remove(buf_ptr(path))) {
1274 return ErrorFileSystem;1274 return ErrorFileSystem;
1275 } else {1275 } else {
1276 return 0;1276 return ErrorNone;
1277 }1277 }
1278}1278}
12791279
1280int os_rename(Buf *src_path, Buf *dest_path) {1280Error os_rename(Buf *src_path, Buf *dest_path) {
1281 if (buf_eql_buf(src_path, dest_path)) {1281 if (buf_eql_buf(src_path, dest_path)) {
1282 return 0;1282 return ErrorNone;
1283 }1283 }
1284#if defined(ZIG_OS_WINDOWS)1284#if defined(ZIG_OS_WINDOWS)
1285 if (!MoveFileExA(buf_ptr(src_path), buf_ptr(dest_path), MOVEFILE_REPLACE_EXISTING)) {1285 if (!MoveFileExA(buf_ptr(src_path), buf_ptr(dest_path), MOVEFILE_REPLACE_EXISTING)) {
...@@ -1290,7 +1290,7 @@ int os_rename(Buf *src_path, Buf *dest_path) {...@@ -1290,7 +1290,7 @@ int os_rename(Buf *src_path, Buf *dest_path) {
1290 return ErrorFileSystem;1290 return ErrorFileSystem;
1291 }1291 }
1292#endif1292#endif
1293 return 0;1293 return ErrorNone;
1294}1294}
12951295
1296double os_get_time(void) {1296double os_get_time(void) {
src/os.hpp+5-6
...@@ -13,7 +13,6 @@...@@ -13,7 +13,6 @@
13#include "error.hpp"13#include "error.hpp"
14#include "zig_llvm.h"14#include "zig_llvm.h"
15#include "windows_sdk.h"15#include "windows_sdk.h"
16#include "result.hpp"
1716
18#include <stdio.h>17#include <stdio.h>
19#include <inttypes.h>18#include <inttypes.h>
...@@ -85,7 +84,7 @@ struct OsTimeStamp {...@@ -85,7 +84,7 @@ struct OsTimeStamp {
85int os_init(void);84int os_init(void);
8685
87void os_spawn_process(const char *exe, ZigList<const char *> &args, Termination *term);86void os_spawn_process(const char *exe, ZigList<const char *> &args, Termination *term);
88int os_exec_process(const char *exe, ZigList<const char *> &args,87Error os_exec_process(const char *exe, ZigList<const char *> &args,
89 Termination *term, Buf *out_stderr, Buf *out_stdout);88 Termination *term, Buf *out_stderr, Buf *out_stdout);
90Error os_execv(const char *exe, const char **argv);89Error os_execv(const char *exe, const char **argv);
9190
...@@ -109,7 +108,7 @@ Error ATTRIBUTE_MUST_USE os_file_overwrite(OsFile file, Buf *contents);...@@ -109,7 +108,7 @@ Error ATTRIBUTE_MUST_USE os_file_overwrite(OsFile file, Buf *contents);
109void os_file_close(OsFile file);108void os_file_close(OsFile file);
110109
111void os_write_file(Buf *full_path, Buf *contents);110void os_write_file(Buf *full_path, Buf *contents);
112int os_copy_file(Buf *src_path, Buf *dest_path);111Error os_copy_file(Buf *src_path, Buf *dest_path);
113112
114Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);113Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
115Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);114Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
...@@ -119,12 +118,12 @@ Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);...@@ -119,12 +118,12 @@ Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
119bool os_stderr_tty(void);118bool os_stderr_tty(void);
120void os_stderr_set_color(TermColor color);119void os_stderr_set_color(TermColor color);
121120
122int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);121Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);
123int os_delete_file(Buf *path);122Error os_delete_file(Buf *path);
124123
125Error ATTRIBUTE_MUST_USE os_file_exists(Buf *full_path, bool *result);124Error ATTRIBUTE_MUST_USE os_file_exists(Buf *full_path, bool *result);
126125
127int os_rename(Buf *src_path, Buf *dest_path);126Error os_rename(Buf *src_path, Buf *dest_path);
128double os_get_time(void);127double os_get_time(void);
129128
130bool os_is_sep(uint8_t c);129bool os_is_sep(uint8_t c);
src/result.hpp deleted-36
...@@ -1,36 +0,0 @@
1/*
2 * Copyright (c) 2018 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_RESULT_HPP
9#define ZIG_RESULT_HPP
10
11#include "error.hpp"
12
13#include <assert.h>
14
15static inline void assertNoError(Error err) {
16 assert(err == ErrorNone);
17}
18
19template<typename T>
20struct Result {
21 T data;
22 Error err;
23
24 Result(T x) : data(x), err(ErrorNone) {}
25
26 Result(Error err) : err(err) {
27 assert(err != ErrorNone);
28 }
29
30 T unwrap() {
31 assert(err == ErrorNone);
32 return data;
33 }
34};
35
36#endif
src/translate_c.cpp+6-5
...@@ -436,7 +436,8 @@ static AstNode *get_global(Context *c, Buf *name) {...@@ -436,7 +436,8 @@ static AstNode *get_global(Context *c, Buf *name) {
436 if (entry)436 if (entry)
437 return entry->value;437 return entry->value;
438 }438 }
439 if (get_primitive_type(c->codegen, name) != nullptr) {439 ZigType *type;
440 if (get_primitive_type(c->codegen, name, &type) != ErrorPrimitiveTypeNotFound) {
440 return trans_create_node_symbol(c, name);441 return trans_create_node_symbol(c, name);
441 }442 }
442 return nullptr;443 return nullptr;
...@@ -4682,10 +4683,10 @@ static void process_preprocessor_entities(Context *c, ASTUnit &unit) {...@@ -4682,10 +4683,10 @@ static void process_preprocessor_entities(Context *c, ASTUnit &unit) {
4682 }4683 }
4683}4684}
46844685
4685int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,4686Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
4686 CodeGen *codegen, AstNode *source_node)4687 CodeGen *codegen, AstNode *source_node)
4687{4688{
4688 int err;4689 Error err;
4689 Buf tmp_file_path = BUF_INIT;4690 Buf tmp_file_path = BUF_INIT;
4690 if ((err = os_buf_to_tmp_file(source, buf_create_from_str(".h"), &tmp_file_path))) {4691 if ((err = os_buf_to_tmp_file(source, buf_create_from_str(".h"), &tmp_file_path))) {
4691 return err;4692 return err;
...@@ -4698,7 +4699,7 @@ int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *sour...@@ -4698,7 +4699,7 @@ int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *sour
4698 return err;4699 return err;
4699}4700}
47004701
4701int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,4702Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
4702 CodeGen *codegen, AstNode *source_node)4703 CodeGen *codegen, AstNode *source_node)
4703{4704{
4704 Context context = {0};4705 Context context = {0};
...@@ -4865,5 +4866,5 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch...@@ -4865,5 +4866,5 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
48654866
4866 import->root = c->root;4867 import->root = c->root;
48674868
4868 return 0;4869 return ErrorNone;
4869}4870}
src/translate_c.hpp+2-2
...@@ -11,10 +11,10 @@...@@ -11,10 +11,10 @@
1111
12#include "all_types.hpp"12#include "all_types.hpp"
1313
14int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,14Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const char *target_file,
15 CodeGen *codegen, AstNode *source_node);15 CodeGen *codegen, AstNode *source_node);
1616
17int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,17Error parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
18 CodeGen *codegen, AstNode *source_node);18 CodeGen *codegen, AstNode *source_node);
1919
20#endif20#endif
test/compile_errors.zig+14-10
...@@ -1,6 +1,19 @@...@@ -1,6 +1,19 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "exceeded maximum bit width of integer",
6 \\export fn entry1() void {
7 \\ const T = @IntType(false, 65536);
8 \\}
9 \\export fn entry2() void {
10 \\ var x: i65536 = 1;
11 \\}
12 ,
13 ".tmp_source.zig:2:31: error: integer value 65536 cannot be implicitly casted to type 'u16'",
14 ".tmp_source.zig:5:12: error: primitive integer type 'i65536' exceeds maximum bit width of 65535",
15 );
16
4 cases.add(17 cases.add(
5 "Panic declared with wrong type signature in tests",18 "Panic declared with wrong type signature in tests",
6 \\test "" {}19 \\test "" {}
...@@ -534,15 +547,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -534,15 +547,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
534 ".tmp_source.zig:1:9: error: parameter of type 'fn(var)var' must be declared comptime",547 ".tmp_source.zig:1:9: error: parameter of type 'fn(var)var' must be declared comptime",
535 );548 );
536549
537 cases.add(
538 "bit count of @IntType too large",
539 \\comptime {
540 \\ _ = @IntType(false, @import("std").math.maxInt(u32) + 1);
541 \\}
542 ,
543 ".tmp_source.zig:2:57: error: integer value 4294967296 cannot be implicitly casted to type 'u32'",
544 );
545
546 cases.add(550 cases.add(
547 "optional pointer to void in extern struct",551 "optional pointer to void in extern struct",
548 \\const Foo = extern struct.{552 \\const Foo = extern struct.{
...@@ -4278,7 +4282,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -4278,7 +4282,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
4278 \\ const a: u16 = 300;4282 \\ const a: u16 = 300;
4279 \\}4283 \\}
4280 ,4284 ,
4281 ".tmp_source.zig:1:1: error: declaration shadows type 'u16'",4285 ".tmp_source.zig:1:1: error: declaration shadows primitive type 'u16'",
4282 );4286 );
42834287
4284 cases.add(4288 cases.add(