diff --git a/src/all_types.hpp b/src/all_types.hpp index 559ebe8cda..48323e58ad 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -47,6 +47,19 @@ struct ResultLocPeer; struct ResultLocPeerParent; struct ResultLocBitCast; +enum PtrLen { + PtrLenUnknown, + PtrLenSingle, + PtrLenC, +}; + +enum UndefAllowed { + UndefOk, + UndefBad, + LazyOkNoUndef, + LazyOk, +}; + enum X64CABIClass { X64CABIClass_Unknown, X64CABIClass_MEMORY, @@ -69,9 +82,9 @@ struct IrExecutable { IrExecutable *source_exec; IrAnalyze *analysis; Scope *begin_scope; + ErrorMsg *first_err_trace_msg; ZigList tld_list; - bool invalid; bool is_inline; bool is_generic_instantiation; bool need_err_code_spill; @@ -255,6 +268,7 @@ enum ConstValSpecial { ConstValSpecialRuntime, ConstValSpecialStatic, ConstValSpecialUndef, + ConstValSpecialLazy, }; enum RuntimeHintErrorUnion { @@ -291,6 +305,82 @@ struct ConstGlobalRefs { uint32_t align; }; +enum LazyValueId { + LazyValueIdInvalid, + LazyValueIdAlignOf, + LazyValueIdPtrType, + LazyValueIdOptType, + LazyValueIdSliceType, + LazyValueIdFnType, + LazyValueIdErrUnionType, +}; + +struct LazyValue { + LazyValueId id; +}; + +struct LazyValueAlignOf { + LazyValue base; + + IrAnalyze *ira; + IrInstruction *target_type; +}; + +struct LazyValueSliceType { + LazyValue base; + + IrAnalyze *ira; + IrInstruction *elem_type; + IrInstruction *align_inst; // can be null + + bool is_const; + bool is_volatile; + bool is_allowzero; +}; + +struct LazyValuePtrType { + LazyValue base; + + IrAnalyze *ira; + IrInstruction *elem_type; + IrInstruction *align_inst; // can be null + + PtrLen ptr_len; + uint32_t bit_offset_in_host; + + uint32_t host_int_bytes; + bool is_const; + bool is_volatile; + bool is_allowzero; +}; + +struct LazyValueOptType { + LazyValue base; + + IrAnalyze *ira; + IrInstruction *payload_type; +}; + +struct LazyValueFnType { + LazyValue base; + + IrAnalyze *ira; + AstNode *proto_node; + IrInstruction **param_types; + IrInstruction *align_inst; // can be null + IrInstruction *return_type; + + bool is_generic; +}; + +struct LazyValueErrUnionType { + LazyValue base; + + IrAnalyze *ira; + IrInstruction *err_set_type; + IrInstruction *payload_type; +}; + struct ConstExprValue { ZigType *type; ConstValSpecial special; @@ -318,6 +408,7 @@ struct ConstExprValue { ConstPtrValue x_ptr; ConstArgTuple x_arg_tuple; Buf *x_enum_literal; + LazyValue *x_lazy; // populated if special == ConstValSpecialRuntime RuntimeHintErrorUnion rh_error_union; @@ -364,6 +455,7 @@ enum TldResolution { TldResolutionUnresolved, TldResolutionResolving, TldResolutionInvalid, + TldResolutionOkLazy, TldResolutionOk, }; @@ -420,10 +512,12 @@ struct TypeEnumField { struct TypeUnionField { Buf *name; + ZigType *type_entry; // available after ResolveStatusSizeKnown + ConstExprValue *type_val; // available after ResolveStatusZeroBitsKnown TypeEnumField *enum_field; - ZigType *type_entry; AstNode *decl_node; uint32_t gen_index; + uint32_t align; }; enum NodeType { @@ -849,6 +943,8 @@ struct AstNodeStructField { Buf *name; AstNode *type; AstNode *value; + // populated if the "align(A)" is present + AstNode *align_expr; }; struct AstNodeStringLiteral { @@ -944,6 +1040,7 @@ struct AstNodeEnumLiteral { struct AstNode { enum NodeType type; + bool already_traced_this_node; size_t line; size_t column; ZigType *owner; @@ -1039,12 +1136,6 @@ struct FnTypeId { uint32_t fn_type_id_hash(FnTypeId*); bool fn_type_id_eql(FnTypeId *a, FnTypeId *b); -enum PtrLen { - PtrLenUnknown, - PtrLenSingle, - PtrLenC, -}; - struct ZigTypePointer { ZigType *child_type; ZigType *slice_parent; @@ -1055,6 +1146,7 @@ struct ZigTypePointer { bool is_const; bool is_volatile; bool allow_zero; + bool resolve_loop_flag_zero_bits; }; struct ZigTypeInt { @@ -1073,7 +1165,8 @@ struct ZigTypeArray { struct TypeStructField { Buf *name; - ZigType *type_entry; + ZigType *type_entry; // available after ResolveStatusSizeKnown + ConstExprValue *type_val; // available after ResolveStatusZeroBitsKnown size_t src_index; size_t gen_index; size_t offset; // byte offset from beginning of struct @@ -1129,15 +1222,16 @@ struct ZigTypeStruct { ResolveStatus resolve_status; bool is_slice; - bool resolve_loop_flag; // set this flag temporarily to detect infinite loops - bool reported_infinite_err; // whether any of the fields require comptime // known after ResolveStatusZeroBitsKnown bool requires_comptime; + bool resolve_loop_flag_zero_bits; + bool resolve_loop_flag_other; }; struct ZigTypeOptional { ZigType *child_type; + ResolveStatus resolve_status; }; struct ZigTypeErrorUnion { @@ -1155,26 +1249,20 @@ struct ZigTypeErrorSet { struct ZigTypeEnum { AstNode *decl_node; - ContainerLayout layout; - uint32_t src_field_count; TypeEnumField *fields; - bool is_invalid; // true if any fields are invalid ZigType *tag_int_type; ScopeDecls *decls_scope; - // set this flag temporarily to detect infinite loops - bool embedded_in_current; - bool reported_infinite_err; - // whether we've finished resolving it - bool complete; - - bool zero_bits_loop_flag; - bool zero_bits_known; - LLVMValueRef name_function; HashMap fields_by_name; + uint32_t src_field_count; + + ContainerLayout layout; + ResolveStatus resolve_status; + + bool resolve_loop_flag; }; uint32_t type_ptr_hash(const ZigType *ptr); @@ -1187,7 +1275,7 @@ struct ZigTypeUnion { HashMap fields_by_name; ZigType *tag_type; // always an enum or null LLVMTypeRef union_llvm_type; - ZigType *most_aligned_union_member; + TypeUnionField *most_aligned_union_member; size_t gen_union_index; size_t gen_tag_index; size_t union_abi_size; @@ -1199,11 +1287,11 @@ struct ZigTypeUnion { ResolveStatus resolve_status; bool have_explicit_tag_type; - bool resolve_loop_flag; // set this flag temporarily to detect infinite loops - bool reported_infinite_err; // whether any of the fields require comptime // the value is not valid until zero_bits_known == true bool requires_comptime; + bool resolve_loop_flag_zero_bits; + bool resolve_loop_flag_other; }; struct FnGenParamInfo { @@ -1715,6 +1803,7 @@ struct CodeGen { //////////////////////////// Runtime State LLVMModuleRef module; ZigList errors; + ErrorMsg *trace_err; LLVMBuilderRef builder; ZigLLVMDIBuilder *dbuilder; ZigLLVMDICompileUnit *compile_unit; @@ -1767,7 +1856,6 @@ struct CodeGen { ZigList resolve_queue; size_t resolve_queue_index; ZigList timing_events; - ZigList tld_ref_source_node_stack; ZigList inline_fns; ZigList test_fns; ZigList errors_by_index; @@ -1852,7 +1940,6 @@ struct CodeGen { ZigFn *main_fn; ZigFn *panic_fn; TldFn *panic_tld_fn; - AstNode *root_export_decl; WantPIC want_pic; WantStackCheck want_stack_check; @@ -1940,7 +2027,7 @@ struct CodeGen { Buf *zig_lib_dir; Buf *zig_std_dir; Buf *dynamic_linker_path; - Buf *version_script_path; + Buf *version_script_path; const char **llvm_argv; size_t llvm_argv_len; @@ -3657,13 +3744,13 @@ enum ResultLocId { ResultLocIdBitCast, }; -// Additions to this struct may need to be handled in +// Additions to this struct may need to be handled in // ir_reset_result struct ResultLoc { ResultLocId id; bool written; bool allow_write_through_const; - IrInstruction *resolved_loc; // result ptr + IrInstruction *resolved_loc; // result ptr IrInstruction *source_instruction; IrInstruction *gen_instruction; // value to store to the result loc ZigType *implicit_elem_type; diff --git a/src/analyze.cpp b/src/analyze.cpp index ab994d07e8..9ae7e99547 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -20,7 +20,7 @@ static const size_t default_backward_branch_quota = 1000; -static Error resolve_struct_type(CodeGen *g, ZigType *struct_type); +static Error ATTRIBUTE_MUST_USE resolve_struct_type(CodeGen *g, ZigType *struct_type); static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type); static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type); @@ -59,13 +59,15 @@ ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg) { root_struct->source_code, root_struct->line_offsets, msg); g->errors.append(err); + g->trace_err = err; return err; } -ErrorMsg *add_node_error(CodeGen *g, const AstNode *node, Buf *msg) { +ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) { Token fake_token; fake_token.start_line = node->line; fake_token.start_column = node->column; + node->already_traced_this_node = true; return add_token_error(g, node->owner, &fake_token, msg); } @@ -271,6 +273,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) { return type_entry->data.structure.resolve_status >= status; case ZigTypeIdUnion: return type_entry->data.unionation.resolve_status >= status; + case ZigTypeIdEnum: + return type_entry->data.enumeration.resolve_status >= status; case ZigTypeIdFnFrame: switch (status) { case ResolveStatusInvalid: @@ -285,32 +289,28 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) { case ResolveStatusLLVMFull: return type_entry->llvm_type != nullptr; } - case ZigTypeIdEnum: - switch (status) { - case ResolveStatusUnstarted: - return true; - case ResolveStatusInvalid: - zig_unreachable(); - case ResolveStatusZeroBitsKnown: - return type_entry->data.enumeration.zero_bits_known; - case ResolveStatusAlignmentKnown: - return type_entry->data.enumeration.zero_bits_known; - case ResolveStatusSizeKnown: - return type_entry->data.enumeration.complete; - case ResolveStatusLLVMFwdDecl: - case ResolveStatusLLVMFull: - return type_entry->llvm_di_type != nullptr; - } - zig_unreachable(); case ZigTypeIdOpaque: return status < ResolveStatusSizeKnown; + case ZigTypeIdPointer: + switch (status) { + case ResolveStatusInvalid: + zig_unreachable(); + case ResolveStatusUnstarted: + return true; + case ResolveStatusZeroBitsKnown: + case ResolveStatusAlignmentKnown: + case ResolveStatusSizeKnown: + return type_entry->abi_size != SIZE_MAX; + case ResolveStatusLLVMFwdDecl: + case ResolveStatusLLVMFull: + return type_entry->llvm_type != nullptr; + } case ZigTypeIdMetaType: case ZigTypeIdVoid: case ZigTypeIdBool: case ZigTypeIdUnreachable: case ZigTypeIdInt: case ZigTypeIdFloat: - case ZigTypeIdPointer: case ZigTypeIdArray: case ZigTypeIdComptimeFloat: case ZigTypeIdComptimeInt: @@ -460,8 +460,6 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons } } - assert(type_is_resolved(child_type, ResolveStatusZeroBitsKnown)); - ZigType *entry = new_type_table_entry(ZigTypeIdPointer); const char *star_str = ptr_len_to_star_str(ptr_len); @@ -491,17 +489,21 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons buf_ptr(&child_type->name)); } - assert(child_type->id != ZigTypeIdInvalid); - - if (type_has_bits(child_type)) { - entry->abi_size = g->builtin_types.entry_usize->abi_size; - entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits; - entry->abi_align = g->builtin_types.entry_usize->abi_align; + if (type_is_resolved(child_type, ResolveStatusZeroBitsKnown)) { + if (type_has_bits(child_type)) { + entry->abi_size = g->builtin_types.entry_usize->abi_size; + entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits; + entry->abi_align = g->builtin_types.entry_usize->abi_align; + } else { + assert(byte_alignment == 0); + entry->abi_size = 0; + entry->size_in_bits = 0; + entry->abi_align = 0; + } } else { - assert(byte_alignment == 0); - entry->abi_size = 0; - entry->size_in_bits = 0; - entry->abi_align = 0; + entry->abi_size = SIZE_MAX; + entry->size_in_bits = SIZE_MAX; + entry->abi_align = UINT32_MAX; } entry->data.pointer.ptr_len = ptr_len; @@ -564,6 +566,7 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) { } entry->data.maybe.child_type = child_type; + entry->data.maybe.resolve_status = ResolveStatusSizeKnown; child_type->optional_parent = entry; return entry; @@ -865,7 +868,7 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { return table_entry->value; } if (fn_type_id->return_type != nullptr) { - if ((err = ensure_complete_type(g, fn_type_id->return_type))) + if ((err = type_resolve(g, fn_type_id->return_type, ResolveStatusSizeKnown))) return g->builtin_types.entry_invalid; assert(fn_type_id->return_type->id != ZigTypeIdOpaque); } else { @@ -960,31 +963,276 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind return entry; } -ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, Buf *type_name) { +ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, + Buf *type_name, UndefAllowed undef) +{ size_t backward_branch_count = 0; size_t backward_branch_quota = default_backward_branch_quota; return ir_eval_const_value(g, scope, node, type_entry, &backward_branch_count, &backward_branch_quota, - nullptr, nullptr, node, type_name, nullptr, nullptr); + nullptr, nullptr, node, type_name, nullptr, nullptr, undef); +} + +static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, ZigType *parent_type, + ConstExprValue *parent_type_val, bool *is_zero_bits) +{ + Error err; + if (type_val->special != ConstValSpecialLazy) { + assert(type_val->special == ConstValSpecialStatic); + if ((type_val->data.x_type->id == ZigTypeIdStruct && + type_val->data.x_type->data.structure.resolve_loop_flag_zero_bits) || + (type_val->data.x_type->id == ZigTypeIdUnion && + type_val->data.x_type->data.unionation.resolve_loop_flag_zero_bits) || + type_val->data.x_type->id == ZigTypeIdPointer) + { + // Does a struct/union which contains a pointer field to itself have bits? Yes. + *is_zero_bits = false; + return ErrorNone; + } + if ((err = type_resolve(g, type_val->data.x_type, ResolveStatusZeroBitsKnown))) + return err; + *is_zero_bits = (type_val->data.x_type->abi_size == 0); + return ErrorNone; + } + switch (type_val->data.x_lazy->id) { + case LazyValueIdInvalid: + case LazyValueIdAlignOf: + zig_unreachable(); + case LazyValueIdPtrType: { + LazyValuePtrType *lazy_ptr_type = reinterpret_cast(type_val->data.x_lazy); + + if (parent_type_val == &lazy_ptr_type->elem_type->value) { + // Does a struct which contains a pointer field to itself have bits? Yes. + *is_zero_bits = false; + return ErrorNone; + } else { + if (parent_type_val == nullptr) { + parent_type_val = type_val; + } + return type_val_resolve_zero_bits(g, &lazy_ptr_type->elem_type->value, parent_type, + parent_type_val, is_zero_bits); + } + } + case LazyValueIdOptType: + case LazyValueIdSliceType: + case LazyValueIdErrUnionType: + *is_zero_bits = false; + return ErrorNone; + case LazyValueIdFnType: { + LazyValueFnType *lazy_fn_type = reinterpret_cast(type_val->data.x_lazy); + *is_zero_bits = lazy_fn_type->is_generic; + return ErrorNone; + } + } + zig_unreachable(); +} + +Error type_val_resolve_is_opaque_type(CodeGen *g, ConstExprValue *type_val, bool *is_opaque_type) { + if (type_val->special != ConstValSpecialLazy) { + assert(type_val->special == ConstValSpecialStatic); + *is_opaque_type = (type_val->data.x_type->id == ZigTypeIdOpaque); + return ErrorNone; + } + switch (type_val->data.x_lazy->id) { + case LazyValueIdInvalid: + case LazyValueIdAlignOf: + zig_unreachable(); + case LazyValueIdSliceType: + case LazyValueIdPtrType: + case LazyValueIdFnType: + case LazyValueIdOptType: + case LazyValueIdErrUnionType: + *is_opaque_type = false; + return ErrorNone; + } + zig_unreachable(); +} + +static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue *type_val) { + if (type_val->special != ConstValSpecialLazy) { + return type_requires_comptime(g, type_val->data.x_type); + } + switch (type_val->data.x_lazy->id) { + case LazyValueIdInvalid: + case LazyValueIdAlignOf: + zig_unreachable(); + case LazyValueIdSliceType: { + LazyValueSliceType *lazy_slice_type = reinterpret_cast(type_val->data.x_lazy); + return type_val_resolve_requires_comptime(g, &lazy_slice_type->elem_type->value); + } + case LazyValueIdPtrType: { + LazyValuePtrType *lazy_ptr_type = reinterpret_cast(type_val->data.x_lazy); + return type_val_resolve_requires_comptime(g, &lazy_ptr_type->elem_type->value); + } + case LazyValueIdOptType: { + LazyValueOptType *lazy_opt_type = reinterpret_cast(type_val->data.x_lazy); + return type_val_resolve_requires_comptime(g, &lazy_opt_type->payload_type->value); + } + case LazyValueIdFnType: { + LazyValueFnType *lazy_fn_type = reinterpret_cast(type_val->data.x_lazy); + if (lazy_fn_type->is_generic) + return ReqCompTimeYes; + switch (type_val_resolve_requires_comptime(g, &lazy_fn_type->return_type->value)) { + case ReqCompTimeInvalid: + return ReqCompTimeInvalid; + case ReqCompTimeYes: + return ReqCompTimeYes; + case ReqCompTimeNo: + break; + } + size_t param_count = lazy_fn_type->proto_node->data.fn_proto.params.length; + for (size_t i = 0; i < param_count; i += 1) { + AstNode *param_node = lazy_fn_type->proto_node->data.fn_proto.params.at(i); + bool param_is_var_args = param_node->data.param_decl.is_var_args; + if (param_is_var_args) break; + switch (type_val_resolve_requires_comptime(g, &lazy_fn_type->param_types[i]->value)) { + case ReqCompTimeInvalid: + return ReqCompTimeInvalid; + case ReqCompTimeYes: + return ReqCompTimeYes; + case ReqCompTimeNo: + break; + } + } + return ReqCompTimeNo; + } + case LazyValueIdErrUnionType: { + LazyValueErrUnionType *lazy_err_union_type = + reinterpret_cast(type_val->data.x_lazy); + return type_val_resolve_requires_comptime(g, &lazy_err_union_type->payload_type->value); + } + } + zig_unreachable(); +} + +static Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ConstExprValue *type_val, + size_t *abi_size, size_t *size_in_bits) +{ + Error err; + +start_over: + if (type_val->special != ConstValSpecialLazy) { + assert(type_val->special == ConstValSpecialStatic); + ZigType *ty = type_val->data.x_type; + if ((err = type_resolve(g, ty, ResolveStatusSizeKnown))) + return err; + *abi_size = ty->abi_size; + *size_in_bits = ty->size_in_bits; + return ErrorNone; + } + switch (type_val->data.x_lazy->id) { + case LazyValueIdInvalid: + case LazyValueIdAlignOf: + zig_unreachable(); + case LazyValueIdSliceType: + *abi_size = g->builtin_types.entry_usize->abi_size * 2; + *size_in_bits = g->builtin_types.entry_usize->size_in_bits * 2; + return ErrorNone; + case LazyValueIdPtrType: + case LazyValueIdFnType: + *abi_size = g->builtin_types.entry_usize->abi_size; + *size_in_bits = g->builtin_types.entry_usize->size_in_bits; + return ErrorNone; + case LazyValueIdOptType: + case LazyValueIdErrUnionType: + if ((err = ir_resolve_lazy(g, source_node, type_val))) + return err; + goto start_over; + } + zig_unreachable(); +} + +Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t *abi_align) { + Error err; + if (type_val->special != ConstValSpecialLazy) { + assert(type_val->special == ConstValSpecialStatic); + ZigType *ty = type_val->data.x_type; + if (ty->id == ZigTypeIdPointer) { + *abi_align = g->builtin_types.entry_usize->abi_align; + return ErrorNone; + } + if ((err = type_resolve(g, ty, ResolveStatusAlignmentKnown))) + return err; + *abi_align = ty->abi_align; + return ErrorNone; + } + switch (type_val->data.x_lazy->id) { + case LazyValueIdInvalid: + case LazyValueIdAlignOf: + zig_unreachable(); + case LazyValueIdSliceType: + case LazyValueIdPtrType: + case LazyValueIdFnType: + *abi_align = g->builtin_types.entry_usize->abi_align; + return ErrorNone; + case LazyValueIdOptType: { + LazyValueOptType *lazy_opt_type = reinterpret_cast(type_val->data.x_lazy); + return type_val_resolve_abi_align(g, &lazy_opt_type->payload_type->value, abi_align); + } + case LazyValueIdErrUnionType: { + LazyValueErrUnionType *lazy_err_union_type = + reinterpret_cast(type_val->data.x_lazy); + uint32_t payload_abi_align; + if ((err = type_val_resolve_abi_align(g, &lazy_err_union_type->payload_type->value, + &payload_abi_align))) + { + return err; + } + *abi_align = (payload_abi_align > g->err_tag_type->abi_align) ? + payload_abi_align : g->err_tag_type->abi_align; + return ErrorNone; + } + } + zig_unreachable(); +} + +static OnePossibleValue type_val_resolve_has_one_possible_value(CodeGen *g, ConstExprValue *type_val) { + if (type_val->special != ConstValSpecialLazy) { + return type_has_one_possible_value(g, type_val->data.x_type); + } + switch (type_val->data.x_lazy->id) { + case LazyValueIdInvalid: + case LazyValueIdAlignOf: + zig_unreachable(); + case LazyValueIdSliceType: // it has the len field + case LazyValueIdOptType: // it has the optional bit + case LazyValueIdFnType: + return OnePossibleValueNo; + case LazyValueIdPtrType: { + Error err; + bool zero_bits; + if ((err = type_val_resolve_zero_bits(g, type_val, nullptr, nullptr, &zero_bits))) { + return OnePossibleValueInvalid; + } + if (zero_bits) { + return OnePossibleValueYes; + } else { + return OnePossibleValueNo; + } + } + case LazyValueIdErrUnionType: { + LazyValueErrUnionType *lazy_err_union_type = + reinterpret_cast(type_val->data.x_lazy); + switch (type_val_resolve_has_one_possible_value(g, &lazy_err_union_type->err_set_type->value)) { + case OnePossibleValueInvalid: + return OnePossibleValueInvalid; + case OnePossibleValueNo: + return OnePossibleValueNo; + case OnePossibleValueYes: + return type_val_resolve_has_one_possible_value(g, &lazy_err_union_type->payload_type->value); + } + } + } + zig_unreachable(); } ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) { - ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr); + ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, + nullptr, UndefBad); if (type_is_invalid(result->type)) return g->builtin_types.entry_invalid; - - assert(result->special != ConstValSpecialRuntime); - // Reject undefined as valid `type` type even though the specification - // allows it to be casted to anything. - // See also ir_resolve_type() - if (result->special == ConstValSpecialUndef) { - add_node_error(g, node, - buf_sprintf("expected type 'type', found '%s'", - buf_ptr(&g->builtin_types.entry_undef->name))); - return g->builtin_types.entry_invalid; - } - - assert(result->data.x_type != nullptr); + src_assert(result->special == ConstValSpecialStatic, node); + src_assert(result->data.x_type != nullptr, node); return result->data.x_type; } @@ -1032,11 +1280,12 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_cou } static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_t *result) { - ConstExprValue *align_result = analyze_const_value(g, scope, node, get_align_amt_type(g), nullptr); + ConstExprValue *align_result = analyze_const_value(g, scope, node, get_align_amt_type(g), + nullptr, UndefBad); if (type_is_invalid(align_result->type)) return false; - uint32_t align_bytes = bigint_as_unsigned(&align_result->data.x_bigint); + uint32_t align_bytes = bigint_as_u32(&align_result->data.x_bigint); if (align_bytes == 0) { add_node_error(g, node, buf_sprintf("alignment must be >= 1")); return false; @@ -1054,7 +1303,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf ** ZigType *ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false, PtrLenUnknown, 0, 0, 0, false); ZigType *str_type = get_slice_type(g, ptr_type); - ConstExprValue *result_val = analyze_const_value(g, scope, node, str_type, nullptr); + ConstExprValue *result_val = analyze_const_value(g, scope, node, str_type, nullptr, UndefBad); if (type_is_invalid(result_val->type)) return false; @@ -1068,7 +1317,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf ** return true; } expand_undef_array(g, array_val); - size_t len = bigint_as_unsigned(&len_field->data.x_bigint); + size_t len = bigint_as_usize(&len_field->data.x_bigint); Buf *result = buf_alloc(); buf_resize(result, len); for (size_t i = 0; i < len; i += 1) { @@ -1078,7 +1327,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf ** add_node_error(g, node, buf_sprintf("use of undefined value")); return false; } - uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint); + uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint); assert(big_c <= UINT8_MAX); uint8_t c = (uint8_t)big_c; buf_ptr(result)[i] = c; @@ -1404,7 +1653,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc add_node_error(g, proto_node, buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447")); return g->builtin_types.entry_invalid; - //return get_generic_fn_type(g, &fn_type_id); } ZigType *specified_return_type = analyze_type_expr(g, child_scope, fn_proto->return_type); @@ -1423,14 +1671,17 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc } if (!calling_convention_allows_zig_types(fn_type_id.cc) && - fn_type_id.return_type->id != ZigTypeIdVoid && - !type_allowed_in_extern(g, fn_type_id.return_type)) + fn_type_id.return_type->id != ZigTypeIdVoid) { - add_node_error(g, fn_proto->return_type, - buf_sprintf("return type '%s' not allowed in function with calling convention '%s'", - buf_ptr(&fn_type_id.return_type->name), - calling_convention_name(fn_type_id.cc))); - return g->builtin_types.entry_invalid; + if ((err = type_resolve(g, fn_type_id.return_type, ResolveStatusSizeKnown))) + return g->builtin_types.entry_invalid; + if (!type_allowed_in_extern(g, fn_type_id.return_type)) { + add_node_error(g, fn_proto->return_type, + buf_sprintf("return type '%s' not allowed in function with calling convention '%s'", + buf_ptr(&fn_type_id.return_type->name), + calling_convention_name(fn_type_id.cc))); + return g->builtin_types.entry_invalid; + } } switch (fn_type_id.return_type->id) { @@ -1490,7 +1741,7 @@ bool type_is_invalid(ZigType *type_entry) { case ZigTypeIdUnion: return type_entry->data.unionation.resolve_status == ResolveStatusInvalid; case ZigTypeIdEnum: - return type_entry->data.enumeration.is_invalid; + return type_entry->data.enumeration.resolve_status == ResolveStatusInvalid; default: return false; } @@ -1584,6 +1835,17 @@ static size_t get_abi_size_bytes(size_t size_in_bits, size_t pointer_size_bytes) return align_forward(store_size_bytes, abi_align); } +ZigType *resolve_struct_field_type(CodeGen *g, TypeStructField *struct_field) { + Error err; + if (struct_field->type_entry == nullptr) { + if ((err = ir_resolve_lazy(g, struct_field->decl_node, struct_field->type_val))) { + return nullptr; + } + struct_field->type_entry = struct_field->type_val->data.x_type; + } + return struct_field->type_entry; +} + static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { assert(struct_type->id == ZigTypeIdStruct); @@ -1599,12 +1861,11 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { AstNode *decl_node = struct_type->data.structure.decl_node; - if (struct_type->data.structure.resolve_loop_flag) { + if (struct_type->data.structure.resolve_loop_flag_other) { if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) { struct_type->data.structure.resolve_status = ResolveStatusInvalid; - ErrorMsg *msg = add_node_error(g, decl_node, - buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name))); - emit_error_notes_for_ref_stack(g, msg); + add_node_error(g, decl_node, + buf_sprintf("struct '%s' depends on itself", buf_ptr(&struct_type->name))); } return ErrorSemanticAnalyzeFail; } @@ -1615,20 +1876,10 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { size_t field_count = struct_type->data.structure.src_field_count; bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked); - struct_type->data.structure.resolve_loop_flag = true; + struct_type->data.structure.resolve_loop_flag_other = true; uint32_t *host_int_bytes = packed ? allocate(struct_type->data.structure.gen_field_count) : nullptr; - // Resolve sizes of all the field types. Done before the offset loop because the offset - // loop has to look ahead. - for (size_t i = 0; i < field_count; i += 1) { - TypeStructField *field = &struct_type->data.structure.fields[i]; - if ((err = type_resolve(g, field->type_entry, ResolveStatusSizeKnown))) { - struct_type->data.structure.resolve_status = ResolveStatusInvalid; - return ErrorSemanticAnalyzeFail; - } - } - size_t packed_bits_offset = 0; size_t next_offset = 0; size_t first_packed_bits_offset_misalign = SIZE_MAX; @@ -1641,13 +1892,25 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { TypeStructField *field = &struct_type->data.structure.fields[i]; if (field->gen_index == SIZE_MAX) continue; - ZigType *field_type = field->type_entry; - assert(field_type != nullptr); field->gen_index = gen_field_index; field->offset = next_offset; if (packed) { + ZigType *field_type = resolve_struct_field_type(g, field); + if (field_type == nullptr) { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return err; + } + if ((err = type_resolve(g, field->type_entry, ResolveStatusSizeKnown))) { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return err; + } + if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field->type_entry, field->decl_node))) { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return err; + } + size_t field_size_in_bits = type_size_bits(g, field_type); size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits; @@ -1683,6 +1946,15 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { } packed_bits_offset = next_packed_bits_offset; } else { + size_t field_abi_size; + size_t field_size_in_bits; + if ((err = type_val_resolve_abi_size(g, field->decl_node, field->type_val, + &field_abi_size, &field_size_in_bits))) + { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return err; + } + gen_field_index += 1; size_t next_src_field_index = i + 1; for (; next_src_field_index < field_count; next_src_field_index += 1) { @@ -1690,9 +1962,9 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { break; } } - size_t next_abi_align = (next_src_field_index == field_count) ? - abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align; - next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, next_abi_align); + size_t next_align = (next_src_field_index == field_count) ? + abi_align : struct_type->data.structure.fields[next_src_field_index].align; + next_offset = next_field_offset(next_offset, abi_align, field_abi_size, next_align); size_in_bits = next_offset * 8; } } @@ -1708,9 +1980,39 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) { struct_type->size_in_bits = size_in_bits; struct_type->data.structure.resolve_status = ResolveStatusSizeKnown; struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index; - struct_type->data.structure.resolve_loop_flag = false; + struct_type->data.structure.resolve_loop_flag_other = false; struct_type->data.structure.host_int_bytes = host_int_bytes; + + // Resolve types for fields + if (!packed) { + for (size_t i = 0; i < field_count; i += 1) { + TypeStructField *field = &struct_type->data.structure.fields[i]; + ZigType *field_type = resolve_struct_field_type(g, field); + if (field_type == nullptr) { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return err; + } + + if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return err; + } + + if (struct_type->data.structure.layout == ContainerLayoutExtern && + !type_allowed_in_extern(g, field_type)) + { + add_node_error(g, field->decl_node, + buf_sprintf("extern structs cannot contain fields of type '%s'", + buf_ptr(&field_type->name))); + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return ErrorSemanticAnalyzeFail; + } + + } + } + + return ErrorNone; } @@ -1728,22 +2030,21 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) { if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown) return ErrorNone; - if (union_type->data.unionation.resolve_loop_flag) { - if (!union_type->data.unionation.reported_infinite_err) { - AstNode *decl_node = union_type->data.unionation.decl_node; - union_type->data.unionation.reported_infinite_err = true; + AstNode *decl_node = union_type->data.structure.decl_node; + + if (union_type->data.unionation.resolve_loop_flag_other) { + if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; - ErrorMsg *msg = add_node_error(g, decl_node, - buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name))); - emit_error_notes_for_ref_stack(g, msg); + add_node_error(g, decl_node, + buf_sprintf("union '%s' depends on itself", buf_ptr(&union_type->name))); } return ErrorSemanticAnalyzeFail; } // set temporary flag - union_type->data.unionation.resolve_loop_flag = true; + union_type->data.unionation.resolve_loop_flag_other = true; - ZigType *most_aligned_union_member = nullptr; + TypeUnionField *most_aligned_union_member = nullptr; uint32_t field_count = union_type->data.unionation.src_field_count; bool packed = union_type->data.unionation.layout == ContainerLayoutPacked; @@ -1752,34 +2053,44 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) { if (field->gen_index == UINT32_MAX) continue; - size_t this_field_align; - if (packed) { - // TODO: https://github.com/ziglang/zig/issues/1512 - this_field_align = 1; - // This is the same hack as resolve_struct_alignment. See the comment there. - } else if (field->type_entry == nullptr) { - this_field_align = g->builtin_types.entry_usize->abi_align; - } else { + AstNode *align_expr = field->decl_node->data.struct_field.align_expr; + if (align_expr != nullptr) { + if (!analyze_const_align(g, &union_type->data.unionation.decls_scope->base, align_expr, + &field->align)) + { + union_type->data.unionation.resolve_status = ResolveStatusInvalid; + return err; + } + add_node_error(g, field->decl_node, + buf_create_from_str("TODO implement field alignment syntax for unions. https://github.com/ziglang/zig/issues/3125")); + } else if (packed) { + field->align = 1; + } else if (field->type_entry != nullptr) { if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; - return ErrorSemanticAnalyzeFail; + return err; + } + field->align = field->type_entry->abi_align; + } else { + if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) { + if (g->trace_err != nullptr) { + g->trace_err = add_error_note(g, g->trace_err, field->decl_node, + buf_create_from_str("while checking this field")); + } + union_type->data.unionation.resolve_status = ResolveStatusInvalid; + return err; } - if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) return ErrorSemanticAnalyzeFail; - - this_field_align = field->type_entry->abi_align; } - if (most_aligned_union_member == nullptr || - this_field_align > most_aligned_union_member->abi_align) - { - most_aligned_union_member = field->type_entry; + if (most_aligned_union_member == nullptr || field->align > most_aligned_union_member->align) { + most_aligned_union_member = field; } } // unset temporary flag - union_type->data.unionation.resolve_loop_flag = false; + union_type->data.unionation.resolve_loop_flag_other = false; union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown; union_type->data.unionation.most_aligned_union_member = most_aligned_union_member; @@ -1793,18 +2104,18 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) { union_type->abi_align = tag_type->abi_align; union_type->data.unionation.gen_tag_index = SIZE_MAX; union_type->data.unionation.gen_union_index = SIZE_MAX; - } else if (tag_type->abi_align > most_aligned_union_member->abi_align) { + } else if (tag_type->abi_align > most_aligned_union_member->align) { union_type->abi_align = tag_type->abi_align; union_type->data.unionation.gen_tag_index = 0; union_type->data.unionation.gen_union_index = 1; } else { - union_type->abi_align = most_aligned_union_member->abi_align; + union_type->abi_align = most_aligned_union_member->align; union_type->data.unionation.gen_union_index = 0; union_type->data.unionation.gen_tag_index = 1; } } else { assert(most_aligned_union_member != nullptr); - union_type->abi_align = most_aligned_union_member->abi_align; + union_type->abi_align = most_aligned_union_member->align; union_type->data.unionation.gen_union_index = SIZE_MAX; union_type->data.unionation.gen_tag_index = SIZE_MAX; } @@ -1812,6 +2123,17 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) { return ErrorNone; } +ZigType *resolve_union_field_type(CodeGen *g, TypeUnionField *union_field) { + Error err; + if (union_field->type_entry == nullptr) { + if ((err = ir_resolve_lazy(g, union_field->decl_node, union_field->type_val))) { + return nullptr; + } + union_field->type_entry = union_field->type_val->data.x_type; + } + return union_field->type_entry; +} + static Error resolve_union_type(CodeGen *g, ZigType *union_type) { assert(union_type->id == ZigTypeIdUnion); @@ -1831,30 +2153,32 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) { assert(decl_node->type == NodeTypeContainerDecl); uint32_t field_count = union_type->data.unionation.src_field_count; - ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member; + TypeUnionField *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member; assert(union_type->data.unionation.fields); size_t union_abi_size = 0; size_t union_size_in_bits = 0; - if (union_type->data.unionation.resolve_loop_flag) { - if (!union_type->data.unionation.reported_infinite_err) { - union_type->data.unionation.reported_infinite_err = true; + if (union_type->data.unionation.resolve_loop_flag_other) { + if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; - ErrorMsg *msg = add_node_error(g, decl_node, - buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name))); - emit_error_notes_for_ref_stack(g, msg); + add_node_error(g, decl_node, + buf_sprintf("union '%s' depends on itself", buf_ptr(&union_type->name))); } return ErrorSemanticAnalyzeFail; } // set temporary flag - union_type->data.unionation.resolve_loop_flag = true; + union_type->data.unionation.resolve_loop_flag_other = true; for (uint32_t i = 0; i < field_count; i += 1) { TypeUnionField *union_field = &union_type->data.unionation.fields[i]; - ZigType *field_type = union_field->type_entry; + ZigType *field_type = resolve_union_field_type(g, union_field); + if (field_type == nullptr) { + union_type->data.unionation.resolve_status = ResolveStatusInvalid; + return ErrorSemanticAnalyzeFail; + } if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; @@ -1874,11 +2198,11 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) { // The union itself for now has to be treated as being independently aligned. // See https://github.com/ziglang/zig/issues/2166. if (most_aligned_union_member != nullptr) { - union_abi_size = align_forward(union_abi_size, most_aligned_union_member->abi_align); + union_abi_size = align_forward(union_abi_size, most_aligned_union_member->align); } // unset temporary flag - union_type->data.unionation.resolve_loop_flag = false; + union_type->data.unionation.resolve_loop_flag_other = false; union_type->data.unionation.resolve_status = ResolveStatusSizeKnown; union_type->data.unionation.union_abi_size = union_abi_size; @@ -1897,7 +2221,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) { field_sizes[union_type->data.unionation.gen_tag_index] = tag_type->abi_size; field_aligns[union_type->data.unionation.gen_tag_index] = tag_type->abi_align; field_sizes[union_type->data.unionation.gen_union_index] = union_abi_size; - field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->abi_align; + field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->align; size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[1]); union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], union_type->abi_align); union_type->size_in_bits = union_type->abi_size * 8; @@ -1925,25 +2249,26 @@ static bool type_is_valid_extern_enum_tag(CodeGen *g, ZigType *ty) { static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { assert(enum_type->id == ZigTypeIdEnum); - if (enum_type->data.enumeration.is_invalid) + if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid) return ErrorSemanticAnalyzeFail; - - if (enum_type->data.enumeration.zero_bits_known) + if (enum_type->data.enumeration.resolve_status >= ResolveStatusZeroBitsKnown) return ErrorNone; - if (enum_type->data.enumeration.zero_bits_loop_flag) { - ErrorMsg *msg = add_node_error(g, enum_type->data.enumeration.decl_node, - buf_sprintf("'%s' depends on itself", buf_ptr(&enum_type->name))); - emit_error_notes_for_ref_stack(g, msg); - enum_type->data.enumeration.is_invalid = true; - return ErrorSemanticAnalyzeFail; - } - - enum_type->data.enumeration.zero_bits_loop_flag = true; - AstNode *decl_node = enum_type->data.enumeration.decl_node; assert(decl_node->type == NodeTypeContainerDecl); + if (enum_type->data.enumeration.resolve_loop_flag) { + if (enum_type->data.enumeration.resolve_status != ResolveStatusInvalid) { + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; + add_node_error(g, decl_node, + buf_sprintf("enum '%s' depends on itself", + buf_ptr(&enum_type->name))); + } + return ErrorSemanticAnalyzeFail; + } + + enum_type->data.enumeration.resolve_loop_flag = true; + assert(!enum_type->data.enumeration.fields); uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length; if (field_count == 0) { @@ -1951,9 +2276,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { enum_type->data.enumeration.src_field_count = field_count; enum_type->data.enumeration.fields = nullptr; - enum_type->data.enumeration.is_invalid = true; - enum_type->data.enumeration.zero_bits_loop_flag = false; - enum_type->data.enumeration.zero_bits_known = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; } @@ -1982,14 +2305,14 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { if (decl_node->data.container_decl.init_arg_expr != nullptr) { ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr); if (type_is_invalid(wanted_tag_int_type)) { - enum_type->data.enumeration.is_invalid = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; } else if (wanted_tag_int_type->id != ZigTypeIdInt) { - enum_type->data.enumeration.is_invalid = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; add_node_error(g, decl_node->data.container_decl.init_arg_expr, buf_sprintf("expected integer, found '%s'", buf_ptr(&wanted_tag_int_type->name))); } else if (enum_type->data.enumeration.layout == ContainerLayoutExtern && !type_is_valid_extern_enum_tag(g, wanted_tag_int_type)) { - enum_type->data.enumeration.is_invalid = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; ErrorMsg *msg = add_node_error(g, decl_node->data.container_decl.init_arg_expr, buf_sprintf("'%s' is not a valid tag type for an extern enum", buf_ptr(&wanted_tag_int_type->name))); @@ -2022,6 +2345,11 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { buf_sprintf("structs and unions, not enums, support field types")); add_error_note(g, msg, decl_node, buf_sprintf("consider 'union(enum)' here")); + } else if (field_node->data.struct_field.align_expr != nullptr) { + ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.align_expr, + buf_sprintf("structs and unions, not enums, support field alignment")); + add_error_note(g, msg, decl_node, + buf_sprintf("consider 'union(enum)' here")); } auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field); @@ -2029,7 +2357,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { ErrorMsg *msg = add_node_error(g, field_node, buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name))); add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); - enum_type->data.enumeration.is_invalid = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; continue; } @@ -2037,9 +2365,10 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { if (tag_value != nullptr) { // A user-specified value is available - ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr); + ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, + nullptr, UndefBad); if (type_is_invalid(result->type)) { - enum_type->data.enumeration.is_invalid = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; continue; } @@ -2060,7 +2389,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { if (!bigint_fits_in_bits(&type_enum_field->value, tag_int_type->size_in_bits, tag_int_type->data.integral.is_signed)) { - enum_type->data.enumeration.is_invalid = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; Buf *val_buf = buf_alloc(); bigint_append_buf(val_buf, &type_enum_field->value, 10); @@ -2075,7 +2404,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { // Make sure the value is unique auto entry = occupied_tag_values.put_unique(type_enum_field->value, field_node); if (entry != nullptr) { - enum_type->data.enumeration.is_invalid = true; + enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; Buf *val_buf = buf_alloc(); bigint_append_buf(val_buf, &type_enum_field->value, 10); @@ -2089,13 +2418,12 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { last_enum_field = type_enum_field; } - enum_type->data.enumeration.zero_bits_loop_flag = false; - enum_type->data.enumeration.zero_bits_known = true; - enum_type->data.enumeration.complete = true; - - if (enum_type->data.enumeration.is_invalid) + if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid) return ErrorSemanticAnalyzeFail; + enum_type->data.enumeration.resolve_loop_flag = false; + enum_type->data.enumeration.resolve_status = ResolveStatusSizeKnown; + return ErrorNone; } @@ -2112,16 +2440,17 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { AstNode *decl_node = struct_type->data.structure.decl_node; assert(decl_node->type == NodeTypeContainerDecl); - if (struct_type->data.structure.resolve_loop_flag) { - // TODO This is a problem. I believe it can be solved with lazy values. - struct_type->size_in_bits = SIZE_MAX; - struct_type->abi_size = SIZE_MAX; - struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown; - struct_type->data.structure.resolve_loop_flag = false; - return ErrorNone; + if (struct_type->data.structure.resolve_loop_flag_zero_bits) { + if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + add_node_error(g, decl_node, + buf_sprintf("struct '%s' depends on itself", + buf_ptr(&struct_type->name))); + } + return ErrorSemanticAnalyzeFail; } - struct_type->data.structure.resolve_loop_flag = true; + struct_type->data.structure.resolve_loop_flag_zero_bits = true; assert(!struct_type->data.structure.fields); size_t field_count = decl_node->data.container_decl.fields.length; @@ -2153,58 +2482,60 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) { return ErrorSemanticAnalyzeFail; } - ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type); - type_struct_field->type_entry = field_type; - if (type_is_invalid(field_type)) { + ConstExprValue *field_type_val = analyze_const_value(g, scope, + field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, LazyOkNoUndef); + if (type_is_invalid(field_type_val->type)) { struct_type->data.structure.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; } + assert(field_type_val->special != ConstValSpecialRuntime); + type_struct_field->type_val = field_type_val; if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) return ErrorSemanticAnalyzeFail; - if (struct_type->data.structure.layout == ContainerLayoutExtern && - !type_allowed_in_extern(g, field_type)) - { - add_node_error(g, field_node, - buf_sprintf("extern structs cannot contain fields of type '%s'", - buf_ptr(&field_type->name))); + bool field_is_opaque_type; + if ((err = type_val_resolve_is_opaque_type(g, field_type_val, &field_is_opaque_type))) { struct_type->data.structure.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; - } else if (struct_type->data.structure.layout == ContainerLayoutPacked) { - if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_node))) { - struct_type->data.structure.resolve_status = ResolveStatusInvalid; - return ErrorSemanticAnalyzeFail; - } } - - type_struct_field->src_index = i; - type_struct_field->gen_index = SIZE_MAX; - - if (field_type->id == ZigTypeIdOpaque) { + if (field_is_opaque_type) { add_node_error(g, field_node->data.struct_field.type, buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs")); struct_type->data.structure.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; } - switch (type_requires_comptime(g, field_type)) { + + type_struct_field->src_index = i; + type_struct_field->gen_index = SIZE_MAX; + + switch (type_val_resolve_requires_comptime(g, field_type_val)) { case ReqCompTimeYes: struct_type->data.structure.requires_comptime = true; break; case ReqCompTimeInvalid: + if (g->trace_err != nullptr) { + g->trace_err = add_error_note(g, g->trace_err, field_node, + buf_create_from_str("while checking this field")); + } struct_type->data.structure.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; case ReqCompTimeNo: break; } - if (!type_has_bits(field_type)) + bool field_is_zero_bits; + if ((err = type_val_resolve_zero_bits(g, field_type_val, struct_type, nullptr, &field_is_zero_bits))) { + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return ErrorSemanticAnalyzeFail; + } + if (field_is_zero_bits) continue; type_struct_field->gen_index = gen_field_index; gen_field_index += 1; } - struct_type->data.structure.resolve_loop_flag = false; + struct_type->data.structure.resolve_loop_flag_zero_bits = false; struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index; if (gen_field_index != 0) { struct_type->abi_size = SIZE_MAX; @@ -2234,17 +2565,16 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) { AstNode *decl_node = struct_type->data.structure.decl_node; - if (struct_type->data.structure.resolve_loop_flag) { + if (struct_type->data.structure.resolve_loop_flag_other) { if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) { struct_type->data.structure.resolve_status = ResolveStatusInvalid; - ErrorMsg *msg = add_node_error(g, decl_node, - buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name))); - emit_error_notes_for_ref_stack(g, msg); + add_node_error(g, decl_node, + buf_sprintf("struct '%s' depends on itself", buf_ptr(&struct_type->name))); } return ErrorSemanticAnalyzeFail; } - struct_type->data.structure.resolve_loop_flag = true; + struct_type->data.structure.resolve_loop_flag_other = true; assert(decl_node->type == NodeTypeContainerDecl); size_t field_count = struct_type->data.structure.src_field_count; @@ -2255,35 +2585,35 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) { if (field->gen_index == SIZE_MAX) continue; - size_t this_field_align; - if (packed) { - // TODO: https://github.com/ziglang/zig/issues/1512 - this_field_align = 1; - // TODO If we have no type_entry for the field, we've already failed to - // compile the program correctly. This stage1 compiler needs a deeper - // reworking to make this correct, or we can ignore the problem - // and make sure it is fixed in stage2. This workaround is for when - // there is a false positive of a dependency loop, of alignment depending - // on itself. When this false positive happens we assume a pointer-aligned - // field, which is usually fine but could be incorrectly over-aligned or - // even under-aligned. See https://github.com/ziglang/zig/issues/1512 - } else if (field->type_entry == nullptr) { - this_field_align = g->builtin_types.entry_usize->abi_align; - } else { - if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) { + AstNode *align_expr = field->decl_node->data.struct_field.align_expr; + if (align_expr != nullptr) { + if (!analyze_const_align(g, &struct_type->data.structure.decls_scope->base, align_expr, + &field->align)) + { struct_type->data.structure.resolve_status = ResolveStatusInvalid; - return ErrorSemanticAnalyzeFail; + return err; } - this_field_align = field->type_entry->abi_align; + } else if (packed) { + field->align = 1; + } else { + if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) { + if (g->trace_err != nullptr) { + g->trace_err = add_error_note(g, g->trace_err, field->decl_node, + buf_create_from_str("while checking this field")); + } + struct_type->data.structure.resolve_status = ResolveStatusInvalid; + return err; + } + if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) + return ErrorSemanticAnalyzeFail; } - // TODO: https://github.com/ziglang/zig/issues/1512 - if (this_field_align > struct_type->abi_align) { - struct_type->abi_align = this_field_align; + if (field->align > struct_type->abi_align) { + struct_type->abi_align = field->align; } } - struct_type->data.structure.resolve_loop_flag = false; + struct_type->data.structure.resolve_loop_flag_other = false; if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) { return ErrorSemanticAnalyzeFail; @@ -2304,23 +2634,21 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown) return ErrorNone; - if (union_type->data.unionation.resolve_loop_flag) { - // If we get here it's due to recursion. From this we conclude that the struct is - // not zero bits. - // TODO actually it could still be zero bits. Here we should continue analyzing - // the union from the next field index. - union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown; - union_type->data.unionation.resolve_loop_flag = false; - union_type->abi_size = SIZE_MAX; - union_type->size_in_bits = SIZE_MAX; - return ErrorNone; - } - - union_type->data.unionation.resolve_loop_flag = true; - AstNode *decl_node = union_type->data.unionation.decl_node; assert(decl_node->type == NodeTypeContainerDecl); + if (union_type->data.unionation.resolve_loop_flag_zero_bits) { + if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) { + union_type->data.unionation.resolve_status = ResolveStatusInvalid; + add_node_error(g, decl_node, + buf_sprintf("union '%s' depends on itself", + buf_ptr(&union_type->name))); + } + return ErrorSemanticAnalyzeFail; + } + + union_type->data.unionation.resolve_loop_flag_zero_bits = true; + assert(union_type->data.unionation.fields == nullptr); uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length; if (field_count == 0) { @@ -2380,14 +2708,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { tag_type->size_in_bits = tag_int_type->size_in_bits; tag_type->data.enumeration.tag_int_type = tag_int_type; - tag_type->data.enumeration.zero_bits_known = true; + tag_type->data.enumeration.resolve_status = ResolveStatusSizeKnown; tag_type->data.enumeration.decl_node = decl_node; tag_type->data.enumeration.layout = ContainerLayoutAuto; tag_type->data.enumeration.src_field_count = field_count; tag_type->data.enumeration.fields = allocate(field_count); tag_type->data.enumeration.fields_by_name.init(field_count); tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope; - tag_type->data.enumeration.complete = true; } else if (enum_type_node != nullptr) { ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node); if (type_is_invalid(enum_type)) { @@ -2429,49 +2756,68 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { return ErrorSemanticAnalyzeFail; } - ZigType *field_type; + bool field_is_zero_bits; if (field_node->data.struct_field.type == nullptr) { - if (decl_node->data.container_decl.auto_enum || decl_node->data.container_decl.init_arg_expr != nullptr) { - field_type = g->builtin_types.entry_void; + if (decl_node->data.container_decl.auto_enum || + decl_node->data.container_decl.init_arg_expr != nullptr) + { + union_field->type_entry = g->builtin_types.entry_void; + field_is_zero_bits = true; } else { add_node_error(g, field_node, buf_sprintf("union field missing type")); union_type->data.unionation.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; } } else { - field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type); - if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) { + ConstExprValue *field_type_val = analyze_const_value(g, scope, + field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, LazyOkNoUndef); + if (type_is_invalid(field_type_val->type)) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; } + assert(field_type_val->special != ConstValSpecialRuntime); + union_field->type_val = field_type_val; if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) return ErrorSemanticAnalyzeFail; - } - union_field->type_entry = field_type; - if (field_type->id == ZigTypeIdOpaque) { - add_node_error(g, field_node->data.struct_field.type, - buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions")); - union_type->data.unionation.resolve_status = ResolveStatusInvalid; - return ErrorSemanticAnalyzeFail; - } - - switch (type_requires_comptime(g, field_type)) { - case ReqCompTimeInvalid: + bool field_is_opaque_type; + if ((err = type_val_resolve_is_opaque_type(g, field_type_val, &field_is_opaque_type))) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; - case ReqCompTimeYes: - union_type->data.unionation.requires_comptime = true; - break; - case ReqCompTimeNo: - break; + } + if (field_is_opaque_type) { + add_node_error(g, field_node->data.struct_field.type, + buf_create_from_str( + "opaque types have unknown size and therefore cannot be directly embedded in unions")); + union_type->data.unionation.resolve_status = ResolveStatusInvalid; + return ErrorSemanticAnalyzeFail; + } + + switch (type_val_resolve_requires_comptime(g, field_type_val)) { + case ReqCompTimeInvalid: + if (g->trace_err != nullptr) { + g->trace_err = add_error_note(g, g->trace_err, field_node, + buf_create_from_str("while checking this field")); + } + union_type->data.unionation.resolve_status = ResolveStatusInvalid; + return ErrorSemanticAnalyzeFail; + case ReqCompTimeYes: + union_type->data.unionation.requires_comptime = true; + break; + case ReqCompTimeNo: + break; + } + + if ((err = type_val_resolve_zero_bits(g, field_type_val, union_type, nullptr, &field_is_zero_bits))) { + union_type->data.unionation.resolve_status = ResolveStatusInvalid; + return ErrorSemanticAnalyzeFail; + } } if (field_node->data.struct_field.value != nullptr && !decl_node->data.container_decl.auto_enum) { ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.value, - buf_sprintf("non-enum union field assignment")); - add_error_note(g, msg, decl_node, - buf_sprintf("consider 'union(enum)' here")); + buf_create_from_str("untagged union field assignment")); + add_error_note(g, msg, decl_node, buf_create_from_str("consider 'union(enum)' here")); } if (create_enum_type) { @@ -2489,7 +2835,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { // In a second pass we will fill in the unspecified ones. if (tag_value != nullptr) { ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type; - ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr); + ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, + nullptr, UndefBad); if (type_is_invalid(result->type)) { union_type->data.unionation.resolve_status = ResolveStatusInvalid; return ErrorSemanticAnalyzeFail; @@ -2530,7 +2877,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { } assert(union_field->enum_field != nullptr); - if (!type_has_bits(field_type)) + if (field_is_zero_bits) continue; union_field->gen_index = gen_field_index; @@ -2607,7 +2954,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { return ErrorSemanticAnalyzeFail; } - union_type->data.unionation.resolve_loop_flag = false; + union_type->data.unionation.resolve_loop_flag_zero_bits = false; union_type->data.unionation.gen_field_count = gen_field_index; bool zero_bits = gen_field_index == 0 && (field_count < 2 || !src_have_tag); @@ -2664,6 +3011,8 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node) { fn_entry->body_node = (proto_node->data.fn_proto.fn_def_node == nullptr) ? nullptr : proto_node->data.fn_proto.fn_def_node->data.fn_def.body; + fn_entry->analyzed_executable.source_node = fn_entry->body_node; + return fn_entry; } @@ -2690,7 +3039,7 @@ void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn) { fake_decl->data.symbol_expr.symbol = tld_fn->base.name; // call this for the side effects of casting to panic_fn_type - analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr); + analyze_const_value(g, tld_fn->base.parent_scope, fake_decl, panic_fn_type, nullptr, UndefBad); } ZigType *get_test_fn_type(CodeGen *g) { @@ -2836,7 +3185,8 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) { static void resolve_decl_comptime(CodeGen *g, TldCompTime *tld_comptime) { assert(tld_comptime->base.source_node->type == NodeTypeCompTime); AstNode *expr_node = tld_comptime->base.source_node->data.comptime_expr.expr; - analyze_const_value(g, tld_comptime->base.parent_scope, expr_node, g->builtin_types.entry_void, nullptr); + analyze_const_value(g, tld_comptime->base.parent_scope, expr_node, g->builtin_types.entry_void, + nullptr, UndefBad); } static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) { @@ -2931,7 +3281,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) { Tld *tld = get_container_scope(g->compile_var_import)->decl_table.get(name); - resolve_top_level_decl(g, tld, tld->source_node); + resolve_top_level_decl(g, tld, tld->source_node, false); assert(tld->id == TldIdVar); TldVar *tld_var = (TldVar *)tld; tld_var->var->const_value = value; @@ -3173,7 +3523,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf return variable_entry; } -static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { +static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) { AstNode *source_node = tld_var->base.source_node; AstNodeVariableDeclaration *var_decl = &source_node->data.variable_declaration; @@ -3185,9 +3535,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { ZigType *explicit_type = nullptr; if (var_decl->type) { if (tld_var->analyzing_type) { - ErrorMsg *msg = add_node_error(g, var_decl->type, + add_node_error(g, var_decl->type, buf_sprintf("type of '%s' depends on itself", buf_ptr(tld_var->base.name))); - emit_error_notes_for_ref_stack(g, msg); explicit_type = g->builtin_types.entry_invalid; } else { tld_var->analyzing_type = true; @@ -3205,7 +3554,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { if (explicit_type && explicit_type->id == ZigTypeIdInvalid) { implicit_type = explicit_type; } else if (var_decl->expr) { - init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol); + init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, + var_decl->symbol, allow_lazy ? LazyOk : UndefOk); assert(init_value); implicit_type = init_value->type; @@ -3360,7 +3710,8 @@ static void preview_use_decl(CodeGen *g, TldUsingNamespace *using_namespace, Sco using_namespace->base.resolution = TldResolutionResolving; assert(using_namespace->base.source_node->type == NodeTypeUsingNamespace); ConstExprValue *result = analyze_const_value(g, &dest_decls_scope->base, - using_namespace->base.source_node->data.using_namespace.expr, g->builtin_types.entry_type, nullptr); + using_namespace->base.source_node->data.using_namespace.expr, g->builtin_types.entry_type, + nullptr, UndefBad); using_namespace->using_namespace_value = result; if (type_is_invalid(result->type)) { @@ -3380,51 +3731,61 @@ static void preview_use_decl(CodeGen *g, TldUsingNamespace *using_namespace, Sco } } -void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) { - if (tld->resolution != TldResolutionUnresolved) +void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy) { + bool want_resolve_lazy = tld->resolution == TldResolutionOkLazy && !allow_lazy; + if (tld->resolution != TldResolutionUnresolved && !want_resolve_lazy) return; - assert(tld->resolution != TldResolutionResolving); tld->resolution = TldResolutionResolving; - g->tld_ref_source_node_stack.append(source_node); switch (tld->id) { - case TldIdVar: - { - TldVar *tld_var = (TldVar *)tld; - resolve_decl_var(g, tld_var); - break; - } - case TldIdFn: - { - TldFn *tld_fn = (TldFn *)tld; - resolve_decl_fn(g, tld_fn); - break; - } - case TldIdContainer: - { - TldContainer *tld_container = (TldContainer *)tld; - resolve_decl_container(g, tld_container); - break; - } - case TldIdCompTime: - { - TldCompTime *tld_comptime = (TldCompTime *)tld; - resolve_decl_comptime(g, tld_comptime); - break; + case TldIdVar: { + TldVar *tld_var = (TldVar *)tld; + if (want_resolve_lazy) { + ir_resolve_lazy(g, source_node, tld_var->var->const_value); + } else { + resolve_decl_var(g, tld_var, allow_lazy); } + tld->resolution = allow_lazy ? TldResolutionOkLazy : TldResolutionOk; + break; + } + case TldIdFn: { + TldFn *tld_fn = (TldFn *)tld; + resolve_decl_fn(g, tld_fn); + + tld->resolution = TldResolutionOk; + break; + } + case TldIdContainer: { + TldContainer *tld_container = (TldContainer *)tld; + resolve_decl_container(g, tld_container); + + tld->resolution = TldResolutionOk; + break; + } + case TldIdCompTime: { + TldCompTime *tld_comptime = (TldCompTime *)tld; + resolve_decl_comptime(g, tld_comptime); + + tld->resolution = TldResolutionOk; + break; + } case TldIdUsingNamespace: { TldUsingNamespace *tld_using_namespace = (TldUsingNamespace *)tld; assert(tld_using_namespace->base.parent_scope->id == ScopeIdDecls); ScopeDecls *dest_decls_scope = (ScopeDecls *)tld_using_namespace->base.parent_scope; preview_use_decl(g, tld_using_namespace, dest_decls_scope); resolve_use_decl(g, tld_using_namespace, dest_decls_scope); + + tld->resolution = TldResolutionOk; break; } } - tld->resolution = TldResolutionOk; - g->tld_ref_source_node_stack.pop(); + if (g->trace_err != nullptr && source_node != nullptr && !source_node->already_traced_this_node) { + g->trace_err = add_error_note(g, g->trace_err, source_node, buf_create_from_str("referenced here")); + source_node->already_traced_this_node = true; + } } Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name) { @@ -3550,7 +3911,7 @@ TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) } TypeEnumField *find_enum_field_by_tag(ZigType *enum_type, const BigInt *tag) { - assert(enum_type->data.enumeration.zero_bits_known); + assert(type_is_resolved(enum_type, ResolveStatusZeroBitsKnown)); for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) { TypeEnumField *field = &enum_type->data.enumeration.fields[i]; if (bigint_cmp(&field->value, tag) == CmpEQ) { @@ -3619,43 +3980,6 @@ ZigType *container_ref_type(ZigType *type_entry) { type_entry->data.pointer.child_type : type_entry; } -Error resolve_container_type(CodeGen *g, ZigType *type_entry) { - switch (type_entry->id) { - case ZigTypeIdStruct: - return resolve_struct_type(g, type_entry); - case ZigTypeIdEnum: - return resolve_enum_zero_bits(g, type_entry); - case ZigTypeIdUnion: - return resolve_union_type(g, type_entry); - case ZigTypeIdPointer: - case ZigTypeIdMetaType: - case ZigTypeIdVoid: - case ZigTypeIdBool: - case ZigTypeIdUnreachable: - case ZigTypeIdInt: - case ZigTypeIdFloat: - case ZigTypeIdArray: - case ZigTypeIdComptimeFloat: - case ZigTypeIdComptimeInt: - case ZigTypeIdEnumLiteral: - case ZigTypeIdUndefined: - case ZigTypeIdNull: - case ZigTypeIdOptional: - case ZigTypeIdErrorUnion: - case ZigTypeIdErrorSet: - case ZigTypeIdFn: - case ZigTypeIdBoundFn: - case ZigTypeIdInvalid: - case ZigTypeIdArgTuple: - case ZigTypeIdOpaque: - case ZigTypeIdVector: - case ZigTypeIdFnFrame: - case ZigTypeIdAnyFrame: - zig_unreachable(); - } - zig_unreachable(); -} - ZigType *get_src_ptr_type(ZigType *type) { if (type->id == ZigTypeIdPointer) return type; if (type->id == ZigTypeIdFn) return type; @@ -3791,6 +4115,13 @@ static void resolve_async_fn_frame(CodeGen *g, ZigFn *fn) { ZigType *frame_type = get_fn_frame_type(g, fn); Error err; if ((err = type_resolve(g, frame_type, ResolveStatusSizeKnown))) { + if (g->trace_err != nullptr && frame_type->data.frame.resolve_loop_src_node != nullptr && + !frame_type->data.frame.reported_loop_err) + { + frame_type->data.frame.reported_loop_err = true; + g->trace_err = add_error_note(g, g->trace_err, frame_type->data.frame.resolve_loop_src_node, + buf_sprintf("when analyzing type '%s' here", buf_ptr(&frame_type->name))); + } fn->anal_state = FnAnalStateInvalid; return; } @@ -3906,7 +4237,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) { &fn->analyzed_executable, fn_type_id->return_type, return_type_node); fn->src_implicit_return_type = block_return_type; - if (type_is_invalid(block_return_type) || fn->analyzed_executable.invalid) { + if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) { assert(g->errors.length > 0); fn->anal_state = FnAnalStateInvalid; return; @@ -3990,7 +4321,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) { assert(!fn_type->data.fn.is_generic); ir_gen_fn(g, fn_table_entry); - if (fn_table_entry->ir_executable.invalid) { + if (fn_table_entry->ir_executable.first_err_trace_msg != nullptr) { fn_table_entry->anal_state = FnAnalStateInvalid; return; } @@ -4128,12 +4459,14 @@ void semantic_analyze(CodeGen *g) { { for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) { Tld *tld = g->resolve_queue.at(g->resolve_queue_index); + g->trace_err = nullptr; AstNode *source_node = nullptr; - resolve_top_level_decl(g, tld, source_node); + resolve_top_level_decl(g, tld, source_node, false); } for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) { ZigFn *fn_entry = g->fn_defs.at(g->fn_defs_index); + g->trace_err = nullptr; analyze_fn_body(g, fn_entry); } } @@ -4145,6 +4478,7 @@ void semantic_analyze(CodeGen *g) { // second pass over functions for detecting async for (g->fn_defs_index = 0; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) { ZigFn *fn = g->fn_defs.at(g->fn_defs_index); + g->trace_err = nullptr; analyze_fn_async(g, fn, true); if (fn_is_async(fn) && fn->non_async_node != nullptr) { ErrorMsg *msg = add_node_error(g, fn->proto_node, @@ -4790,7 +5124,10 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) { case ZigTypeIdStruct: for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) { TypeStructField *field = &type_entry->data.structure.fields[i]; - switch (type_has_one_possible_value(g, field->type_entry)) { + OnePossibleValue opv = (field->type_entry != nullptr) ? + type_has_one_possible_value(g, field->type_entry) : + type_val_resolve_has_one_possible_value(g, field->type_val); + switch (opv) { case OnePossibleValueInvalid: return OnePossibleValueInvalid; case OnePossibleValueNo: @@ -4816,18 +5153,19 @@ OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry) { case ZigTypeIdUnion: if (type_entry->data.unionation.src_field_count > 1) return OnePossibleValueNo; - return type_has_one_possible_value(g, type_entry->data.unionation.fields[0].type_entry); + TypeUnionField *only_field = &type_entry->data.unionation.fields[0]; + if (only_field->type_entry != nullptr) { + return type_has_one_possible_value(g, only_field->type_entry); + } + return type_val_resolve_has_one_possible_value(g, only_field->type_val); } zig_unreachable(); } -ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) { +ReqCompTime type_requires_comptime(CodeGen *g, ZigType *ty) { Error err; - if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown))) - return ReqCompTimeInvalid; - switch (type_entry->id) { + switch (ty->id) { case ZigTypeIdInvalid: - case ZigTypeIdOpaque: zig_unreachable(); case ZigTypeIdComptimeFloat: case ZigTypeIdComptimeInt: @@ -4839,23 +5177,36 @@ ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry) { case ZigTypeIdArgTuple: return ReqCompTimeYes; case ZigTypeIdArray: - return type_requires_comptime(g, type_entry->data.array.child_type); + return type_requires_comptime(g, ty->data.array.child_type); case ZigTypeIdStruct: - return type_entry->data.structure.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo; + if (ty->data.structure.resolve_loop_flag_zero_bits) { + // Does a struct which contains a pointer field to itself require comptime? No. + return ReqCompTimeNo; + } + if ((err = type_resolve(g, ty, ResolveStatusZeroBitsKnown))) + return ReqCompTimeInvalid; + return ty->data.structure.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo; case ZigTypeIdUnion: - return type_entry->data.unionation.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo; + if (ty->data.unionation.resolve_loop_flag_zero_bits) { + // Does a union which contains a pointer field to itself require comptime? No. + return ReqCompTimeNo; + } + if ((err = type_resolve(g, ty, ResolveStatusZeroBitsKnown))) + return ReqCompTimeInvalid; + return ty->data.unionation.requires_comptime ? ReqCompTimeYes : ReqCompTimeNo; case ZigTypeIdOptional: - return type_requires_comptime(g, type_entry->data.maybe.child_type); + return type_requires_comptime(g, ty->data.maybe.child_type); case ZigTypeIdErrorUnion: - return type_requires_comptime(g, type_entry->data.error_union.payload_type); + return type_requires_comptime(g, ty->data.error_union.payload_type); case ZigTypeIdPointer: - if (type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) { + if (ty->data.pointer.child_type->id == ZigTypeIdOpaque) { return ReqCompTimeNo; } else { - return type_requires_comptime(g, type_entry->data.pointer.child_type); + return type_requires_comptime(g, ty->data.pointer.child_type); } case ZigTypeIdFn: - return type_entry->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo; + return ty->data.fn.is_generic ? ReqCompTimeYes : ReqCompTimeNo; + case ZigTypeIdOpaque: case ZigTypeIdEnum: case ZigTypeIdErrorSet: case ZigTypeIdBool: @@ -5143,34 +5494,6 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_ } -void init_const_undefined(CodeGen *g, ConstExprValue *const_val) { - Error err; - ZigType *wanted_type = const_val->type; - if (wanted_type->id == ZigTypeIdArray) { - const_val->special = ConstValSpecialStatic; - const_val->data.x_array.special = ConstArraySpecialUndef; - } else if (wanted_type->id == ZigTypeIdStruct) { - if ((err = ensure_complete_type(g, wanted_type))) { - return; - } - - const_val->special = ConstValSpecialStatic; - size_t field_count = wanted_type->data.structure.src_field_count; - const_val->data.x_struct.fields = create_const_vals(field_count); - for (size_t i = 0; i < field_count; i += 1) { - ConstExprValue *field_val = &const_val->data.x_struct.fields[i]; - field_val->type = wanted_type->data.structure.fields[i].type_entry; - assert(field_val->type); - init_const_undefined(g, field_val); - field_val->parent.id = ConstParentIdStruct; - field_val->parent.data.p_struct.struct_val = const_val; - field_val->parent.data.p_struct.field_index = i; - } - } else { - const_val->special = ConstValSpecialUndef; - } -} - ConstExprValue *create_const_vals(size_t count) { ConstGlobalRefs *global_refs = allocate(count); ConstExprValue *vals = allocate(count); @@ -5180,10 +5503,6 @@ ConstExprValue *create_const_vals(size_t count) { return vals; } -Error ensure_complete_type(CodeGen *g, ZigType *type_entry) { - return type_resolve(g, type_entry, ResolveStatusSizeKnown); -} - static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) { if (orig_fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) return orig_fn_type; @@ -5197,27 +5516,6 @@ static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) { return fn_type; } -static void emit_error_notes_for_type_loop(CodeGen *g, ErrorMsg *msg, ZigType *stop_type, - ZigType *ty, AstNode *src_node) -{ - ErrorMsg *note = add_error_note(g, msg, src_node, - buf_sprintf("when analyzing type '%s' here", buf_ptr(&ty->name))); - if (ty == stop_type) - return; - switch (ty->id) { - case ZigTypeIdFnFrame: { - ty->data.frame.reported_loop_err = true; - ZigType *depending_type = ty->data.frame.resolve_loop_type; - if (depending_type == nullptr) - return; - emit_error_notes_for_type_loop(g, note, stop_type, - depending_type, ty->data.frame.resolve_loop_src_node); - } - default: - return; - } -} - static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) { Error err; @@ -5229,14 +5527,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) { if (frame_type->data.frame.resolve_loop_type != nullptr) { if (!frame_type->data.frame.reported_loop_err) { - frame_type->data.frame.reported_loop_err = true; - ErrorMsg *msg = add_node_error(g, fn->proto_node, + add_node_error(g, fn->proto_node, buf_sprintf("'%s' depends on itself", buf_ptr(&frame_type->name))); - emit_error_notes_for_type_loop(g, msg, - frame_type, - frame_type->data.frame.resolve_loop_type, - frame_type->data.frame.resolve_loop_src_node); - emit_error_notes_for_ref_stack(g, msg); } return ErrorSemanticAnalyzeFail; } @@ -5252,11 +5544,9 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) { return ErrorSemanticAnalyzeFail; break; case FnAnalStateProbing: { - ErrorMsg *msg = add_node_error(g, fn->proto_node, + add_node_error(g, fn->proto_node, buf_sprintf("cannot resolve '%s': function not fully analyzed yet", buf_ptr(&frame_type->name))); - ir_add_analysis_trace(fn->ir_executable.analysis, msg, - buf_sprintf("depends on its own frame here")); return ErrorSemanticAnalyzeFail; } } @@ -5327,10 +5617,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) { if (callee->anal_state == FnAnalStateProbing) { ErrorMsg *msg = add_node_error(g, fn->proto_node, buf_sprintf("unable to determine async function frame of '%s'", buf_ptr(&fn->symbol_name))); - ErrorMsg *note = add_error_note(g, msg, call->base.source_node, + g->trace_err = add_error_note(g, msg, call->base.source_node, buf_sprintf("analysis of function '%s' depends on the frame", buf_ptr(&callee->symbol_name))); - ir_add_analysis_trace(callee->ir_executable.analysis, note, - buf_sprintf("depends on the frame here")); return ErrorSemanticAnalyzeFail; } @@ -5442,6 +5730,37 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) { return ErrorNone; } +static Error resolve_pointer_zero_bits(CodeGen *g, ZigType *ty) { + Error err; + + if (ty->abi_size != SIZE_MAX) + return ErrorNone; + + if (ty->data.pointer.resolve_loop_flag_zero_bits) { + ty->abi_size = g->builtin_types.entry_usize->abi_size; + ty->size_in_bits = g->builtin_types.entry_usize->size_in_bits; + ty->abi_align = g->builtin_types.entry_usize->abi_align; + return ErrorNone; + } + ty->data.pointer.resolve_loop_flag_zero_bits = true; + + ZigType *elem_type = ty->data.pointer.child_type; + + if ((err = type_resolve(g, elem_type, ResolveStatusZeroBitsKnown))) + return err; + + if (type_has_bits(elem_type)) { + ty->abi_size = g->builtin_types.entry_usize->abi_size; + ty->size_in_bits = g->builtin_types.entry_usize->size_in_bits; + ty->abi_align = g->builtin_types.entry_usize->abi_align; + } else { + ty->abi_size = 0; + ty->size_in_bits = 0; + ty->abi_align = 0; + } + return ErrorNone; +} + Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) { if (type_is_invalid(ty)) return ErrorSemanticAnalyzeFail; @@ -5451,36 +5770,48 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) { case ResolveStatusInvalid: zig_unreachable(); case ResolveStatusZeroBitsKnown: - if (ty->id == ZigTypeIdStruct) { - return resolve_struct_zero_bits(g, ty); - } else if (ty->id == ZigTypeIdEnum) { - return resolve_enum_zero_bits(g, ty); - } else if (ty->id == ZigTypeIdUnion) { - return resolve_union_zero_bits(g, ty); + switch (ty->id) { + case ZigTypeIdStruct: + return resolve_struct_zero_bits(g, ty); + case ZigTypeIdEnum: + return resolve_enum_zero_bits(g, ty); + case ZigTypeIdUnion: + return resolve_union_zero_bits(g, ty); + case ZigTypeIdPointer: + return resolve_pointer_zero_bits(g, ty); + default: + return ErrorNone; } - return ErrorNone; case ResolveStatusAlignmentKnown: - if (ty->id == ZigTypeIdStruct) { - return resolve_struct_alignment(g, ty); - } else if (ty->id == ZigTypeIdEnum) { - return resolve_enum_zero_bits(g, ty); - } else if (ty->id == ZigTypeIdUnion) { - return resolve_union_alignment(g, ty); - } else if (ty->id == ZigTypeIdFnFrame) { - return resolve_async_frame(g, ty); + switch (ty->id) { + case ZigTypeIdStruct: + return resolve_struct_alignment(g, ty); + case ZigTypeIdEnum: + return resolve_enum_zero_bits(g, ty); + case ZigTypeIdUnion: + return resolve_union_alignment(g, ty); + case ZigTypeIdFnFrame: + return resolve_async_frame(g, ty); + case ZigTypeIdPointer: + return resolve_pointer_zero_bits(g, ty); + default: + return ErrorNone; } - return ErrorNone; case ResolveStatusSizeKnown: - if (ty->id == ZigTypeIdStruct) { - return resolve_struct_type(g, ty); - } else if (ty->id == ZigTypeIdEnum) { - return resolve_enum_zero_bits(g, ty); - } else if (ty->id == ZigTypeIdUnion) { - return resolve_union_type(g, ty); - } else if (ty->id == ZigTypeIdFnFrame) { - return resolve_async_frame(g, ty); + switch (ty->id) { + case ZigTypeIdStruct: + return resolve_struct_type(g, ty); + case ZigTypeIdEnum: + return resolve_enum_zero_bits(g, ty); + case ZigTypeIdUnion: + return resolve_union_type(g, ty); + case ZigTypeIdFnFrame: + return resolve_async_frame(g, ty); + case ZigTypeIdPointer: + return resolve_pointer_zero_bits(g, ty); + default: + return ErrorNone; } - return ErrorNone; case ResolveStatusLLVMFwdDecl: case ResolveStatusLLVMFull: resolve_llvm_types(g, ty, status); @@ -5855,6 +6186,9 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) { case ConstValSpecialRuntime: buf_appendf(buf, "(runtime value)"); return; + case ConstValSpecialLazy: + buf_appendf(buf, "(lazy value)"); + return; case ConstValSpecialUndef: buf_appendf(buf, "undefined"); return; @@ -5976,7 +6310,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) { { if (is_slice(type_entry)) { ConstExprValue *len_val = &const_val->data.x_struct.fields[slice_len_index]; - size_t len = bigint_as_unsigned(&len_val->data.x_bigint); + size_t len = bigint_as_usize(&len_val->data.x_bigint); ConstExprValue *ptr_val = &const_val->data.x_struct.fields[slice_ptr_index]; if (ptr_val->special == ConstValSpecialUndef) { @@ -6051,12 +6385,12 @@ ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) { entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type); entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type); - if (size_in_bits >= 128) { + if (size_in_bits >= 128 && entry->abi_align < 16) { // Override the incorrect alignment reported by LLVM. Clang does this as well. // On x86_64 there are some instructions like CMPXCHG16B which require this. // On all targets, integers 128 bits and above have ABI alignment of 16. + // However for some targets, LLVM incorrectly reports this as 8. // See: https://github.com/ziglang/zig/issues/2987 - assert(entry->abi_align == 8); // if this trips we can remove the workaround entry->abi_align = 16; } } @@ -6229,6 +6563,40 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) { zig_unreachable(); } +static void init_const_undefined(CodeGen *g, ConstExprValue *const_val) { + Error err; + ZigType *wanted_type = const_val->type; + if (wanted_type->id == ZigTypeIdArray) { + const_val->special = ConstValSpecialStatic; + const_val->data.x_array.special = ConstArraySpecialUndef; + } else if (wanted_type->id == ZigTypeIdStruct) { + if ((err = type_resolve(g, wanted_type, ResolveStatusZeroBitsKnown))) { + return; + } + + const_val->special = ConstValSpecialStatic; + size_t field_count = wanted_type->data.structure.src_field_count; + const_val->data.x_struct.fields = create_const_vals(field_count); + for (size_t i = 0; i < field_count; i += 1) { + ConstExprValue *field_val = &const_val->data.x_struct.fields[i]; + field_val->type = wanted_type->data.structure.fields[i].type_entry; + assert(field_val->type); + init_const_undefined(g, field_val); + field_val->parent.id = ConstParentIdStruct; + field_val->parent.data.p_struct.struct_val = const_val; + field_val->parent.data.p_struct.field_index = i; + } + } else { + const_val->special = ConstValSpecialUndef; + } +} + +void expand_undef_struct(CodeGen *g, ConstExprValue *const_val) { + if (const_val->special == ConstValSpecialUndef) { + init_const_undefined(g, const_val); + } +} + // Canonicalize the array value as ConstArraySpecialNone void expand_undef_array(CodeGen *g, ConstExprValue *const_val) { size_t elem_count; @@ -6492,7 +6860,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) { ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) { Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name)); - resolve_top_level_decl(codegen, tld, nullptr); + resolve_top_level_decl(codegen, tld, nullptr, false); assert(tld->id == TldIdVar); TldVar *tld_var = (TldVar *)tld; ConstExprValue *var_value = tld_var->var->const_value; @@ -6707,19 +7075,6 @@ bool ptr_allows_addr_zero(ZigType *ptr_type) { return false; } -void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg) { - size_t i = g->tld_ref_source_node_stack.length; - for (;;) { - if (i == 0) - break; - i -= 1; - AstNode *source_node = g->tld_ref_source_node_stack.at(i); - if (source_node) { - msg = add_error_note(g, msg, source_node, buf_sprintf("referenced here")); - } - } -} - Buf *type_bare_name(ZigType *type_entry) { if (is_slice(type_entry)) { return &type_entry->name; @@ -7122,10 +7477,9 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS struct_type->data.structure.resolve_status = ResolveStatusLLVMFull; } -static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) { - assert(!enum_type->data.enumeration.is_invalid); - assert(enum_type->data.enumeration.complete); - if (enum_type->llvm_di_type != nullptr) return; +static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatus wanted_resolve_status) { + assert(enum_type->data.enumeration.resolve_status >= ResolveStatusSizeKnown); + if (enum_type->data.enumeration.resolve_status >= wanted_resolve_status) return; Scope *scope = &enum_type->data.enumeration.decls_scope->base; ZigType *import = get_scope_import(scope); @@ -7146,6 +7500,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) { debug_align_in_bits, ZigLLVM_DIFlags_Zero, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, ""); + enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull; return; } @@ -7178,14 +7533,16 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) { get_llvm_di_type(g, tag_int_type), ""); enum_type->llvm_di_type = tag_di_type; + enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull; } static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) { if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return; - ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member; + TypeUnionField *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member; ZigType *tag_type = union_type->data.unionation.tag_type; - if (most_aligned_union_member == nullptr) { + uint32_t gen_field_count = union_type->data.unionation.gen_field_count; + if (gen_field_count == 0) { union_type->llvm_type = get_llvm_type(g, tag_type); union_type->llvm_di_type = get_llvm_di_type(g, tag_type); union_type->data.unionation.resolve_status = ResolveStatusLLVMFull; @@ -7209,7 +7566,6 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return; } - uint32_t gen_field_count = union_type->data.unionation.gen_field_count; ZigLLVMDIType **union_inner_di_types = allocate(gen_field_count); uint32_t field_count = union_type->data.unionation.src_field_count; for (uint32_t i = 0; i < field_count; i += 1) { @@ -7236,17 +7592,17 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta if (tag_type == nullptr || !type_has_bits(tag_type)) { assert(most_aligned_union_member != nullptr); - size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size; + size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->type_entry->abi_size; if (padding_bytes > 0) { ZigType *u8_type = get_int_type(g, false, 8); ZigType *padding_array = get_array_type(g, u8_type, padding_bytes); LLVMTypeRef union_element_types[] = { - most_aligned_union_member->llvm_type, + most_aligned_union_member->type_entry->llvm_type, get_llvm_type(g, padding_array), }; LLVMStructSetBody(union_type->llvm_type, union_element_types, 2, false); } else { - LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->llvm_type, 1, false); + LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->type_entry->llvm_type, 1, false); } union_type->data.unionation.union_llvm_type = union_type->llvm_type; union_type->data.unionation.gen_tag_index = SIZE_MAX; @@ -7257,7 +7613,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name), import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), union_type->data.unionation.union_abi_size * 8, - most_aligned_union_member->abi_align * 8, + most_aligned_union_member->align * 8, ZigLLVM_DIFlags_Zero, union_inner_di_types, gen_field_count, 0, ""); @@ -7268,14 +7624,14 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta } LLVMTypeRef union_type_ref; - size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size; + size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->type_entry->abi_size; if (padding_bytes == 0) { - union_type_ref = get_llvm_type(g, most_aligned_union_member); + union_type_ref = get_llvm_type(g, most_aligned_union_member->type_entry); } else { ZigType *u8_type = get_int_type(g, false, 8); ZigType *padding_array = get_array_type(g, u8_type, padding_bytes); LLVMTypeRef union_element_types[] = { - get_llvm_type(g, most_aligned_union_member), + get_llvm_type(g, most_aligned_union_member->type_entry), get_llvm_type(g, padding_array), }; union_type_ref = LLVMStructType(union_element_types, 2, false); @@ -7291,7 +7647,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder, ZigLLVMTypeToScope(union_type->llvm_di_type), "AnonUnion", import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), - most_aligned_union_member->size_in_bits, 8*most_aligned_union_member->abi_align, + most_aligned_union_member->type_entry->size_in_bits, 8*most_aligned_union_member->align, ZigLLVM_DIFlags_Zero, union_inner_di_types, gen_field_count, 0, ""); uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type, @@ -7302,8 +7658,8 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(union_type->llvm_di_type), "payload", import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1), - most_aligned_union_member->size_in_bits, - 8*most_aligned_union_member->abi_align, + most_aligned_union_member->type_entry->size_in_bits, + 8*most_aligned_union_member->align, union_offset_in_bits, ZigLLVM_DIFlags_Zero, union_di_type); @@ -7340,6 +7696,9 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) { if (type->llvm_di_type != nullptr) return; + if (resolve_pointer_zero_bits(g, type) != ErrorNone) + zig_unreachable(); + if (!type_has_bits(type)) { type->llvm_type = g->builtin_types.entry_void->llvm_type; type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type; @@ -7406,8 +7765,11 @@ static void resolve_llvm_types_integer(CodeGen *g, ZigType *type) { type->llvm_type = LLVMIntType(type->size_in_bits); } -static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) { - if (type->llvm_di_type != nullptr) return; +static void resolve_llvm_types_optional(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) { + assert(type->id == ZigTypeIdOptional); + assert(type->data.maybe.resolve_status != ResolveStatusInvalid); + assert(type->data.maybe.resolve_status >= ResolveStatusSizeKnown); + if (type->data.maybe.resolve_status >= wanted_resolve_status) return; LLVMTypeRef bool_llvm_type = get_llvm_type(g, g->builtin_types.entry_bool); ZigLLVMDIType *bool_llvm_di_type = get_llvm_di_type(g, g->builtin_types.entry_bool); @@ -7416,30 +7778,41 @@ static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) { if (!type_has_bits(child_type)) { type->llvm_type = bool_llvm_type; type->llvm_di_type = bool_llvm_di_type; + type->data.maybe.resolve_status = ResolveStatusLLVMFull; return; } + if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) { + type->llvm_type = get_llvm_type(g, child_type); + type->llvm_di_type = get_llvm_di_type(g, child_type); + type->data.maybe.resolve_status = ResolveStatusLLVMFull; + return; + } + + ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); + ZigLLVMDIFile *di_file = nullptr; + unsigned line = 0; + + if (type->data.maybe.resolve_status < ResolveStatusLLVMFwdDecl) { + type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&type->name)); + unsigned dwarf_kind = ZigLLVMTag_DW_structure_type(); + type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, + dwarf_kind, buf_ptr(&type->name), + compile_unit_scope, di_file, line); + + type->data.maybe.resolve_status = ResolveStatusLLVMFwdDecl; + if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return; + } + LLVMTypeRef child_llvm_type = get_llvm_type(g, child_type); ZigLLVMDIType *child_llvm_di_type = get_llvm_di_type(g, child_type); - - if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) { - type->llvm_type = child_llvm_type; - type->llvm_di_type = child_llvm_di_type; - return; - } + if (type->data.maybe.resolve_status >= wanted_resolve_status) return; LLVMTypeRef elem_types[] = { get_llvm_type(g, child_type), LLVMInt1Type(), }; - type->llvm_type = LLVMStructType(elem_types, 2, false); - - ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit); - ZigLLVMDIFile *di_file = nullptr; - unsigned line = 0; - type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder, - ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name), - compile_unit_scope, di_file, line); + LLVMStructSetBody(type->llvm_type, elem_types, 2, false); uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_llvm_type); uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_llvm_type); @@ -7474,6 +7847,7 @@ static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) { ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type); type->llvm_di_type = replacement_di_type; + type->data.maybe.resolve_status = ResolveStatusLLVMFull; } static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) { @@ -7909,7 +8283,7 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r else return resolve_llvm_types_struct(g, type, wanted_resolve_status, nullptr); case ZigTypeIdEnum: - return resolve_llvm_types_enum(g, type); + return resolve_llvm_types_enum(g, type, wanted_resolve_status); case ZigTypeIdUnion: return resolve_llvm_types_union(g, type, wanted_resolve_status); case ZigTypeIdPointer: @@ -7917,7 +8291,7 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r case ZigTypeIdInt: return resolve_llvm_types_integer(g, type); case ZigTypeIdOptional: - return resolve_llvm_types_optional(g, type); + return resolve_llvm_types_optional(g, type, wanted_resolve_status); case ZigTypeIdErrorUnion: return resolve_llvm_types_error_union(g, type); case ZigTypeIdArray: diff --git a/src/analyze.hpp b/src/analyze.hpp index 5752c74751..ebfd11f514 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -11,10 +11,9 @@ #include "all_types.hpp" void semantic_analyze(CodeGen *g); -ErrorMsg *add_node_error(CodeGen *g, const AstNode *node, Buf *msg); +ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg); ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg); ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node, Buf *msg); -void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg); ZigType *new_type_table_entry(ZigTypeId id); ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn); ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const); @@ -59,7 +58,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Bu ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope); Tld *find_decl(CodeGen *g, Scope *scope, Buf *name); Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name); -void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node); +void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy); ZigType *get_src_ptr_type(ZigType *type); ZigType *get_codegen_ptr_type(ZigType *type); @@ -71,7 +70,6 @@ bool type_is_complete(ZigType *type_entry); bool type_is_resolved(ZigType *type_entry, ResolveStatus status); bool type_is_invalid(ZigType *type_entry); bool type_is_global_error_set(ZigType *err_set_type); -Error resolve_container_type(CodeGen *g, ZigType *type_entry); ScopeDecls *get_container_scope(ZigType *type_entry); TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name); TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name); @@ -95,7 +93,6 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node); ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value); void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc); AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index); -Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry); Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status); void complete_enum(CodeGen *g, ZigType *enum_type); bool ir_get_var_is_comptime(ZigVar *var); @@ -169,12 +166,11 @@ ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t void init_const_arg_tuple(CodeGen *g, ConstExprValue *const_val, size_t arg_index_start, size_t arg_index_end); ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_t arg_index_end); -void init_const_undefined(CodeGen *g, ConstExprValue *const_val); - ConstExprValue *create_const_vals(size_t count); ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits); void expand_undef_array(CodeGen *g, ConstExprValue *const_val); +void expand_undef_struct(CodeGen *g, ConstExprValue *const_val); void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value); const char *type_id_name(ZigTypeId id); @@ -244,9 +240,14 @@ void add_cc_args(CodeGen *g, ZigList &args, const char *out_dep_pa void src_assert(bool ok, AstNode *source_node); bool is_container(ZigType *type_entry); -ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, Buf *type_name); +ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, + Buf *type_name, UndefAllowed undef); void resolve_llvm_types_fn(CodeGen *g, ZigFn *fn); bool fn_is_async(ZigFn *fn); +Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t *abi_align); +ZigType *resolve_union_field_type(CodeGen *g, TypeUnionField *union_field); +ZigType *resolve_struct_field_type(CodeGen *g, TypeStructField *struct_field); + #endif diff --git a/src/bigint.cpp b/src/bigint.cpp index da53a2b129..b227b3b4f7 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -15,6 +15,8 @@ #include #include +static uint64_t bigint_as_unsigned(const BigInt *bigint); + static void bigint_normalize(BigInt *dest) { const uint64_t *digits = bigint_ptr(dest); @@ -1660,7 +1662,7 @@ size_t bigint_clz(const BigInt *bi, size_t bit_count) { return count; } -uint64_t bigint_as_unsigned(const BigInt *bigint) { +static uint64_t bigint_as_unsigned(const BigInt *bigint) { assert(!bigint->is_negative); if (bigint->digit_count == 0) { return 0; @@ -1671,6 +1673,25 @@ uint64_t bigint_as_unsigned(const BigInt *bigint) { } } +uint64_t bigint_as_u64(const BigInt *bigint) +{ + return bigint_as_unsigned(bigint); +} + +uint32_t bigint_as_u32(const BigInt *bigint) { + uint64_t value64 = bigint_as_unsigned(bigint); + uint32_t value32 = (uint32_t)value64; + assert (value64 == value32); + return value32; +} + +size_t bigint_as_usize(const BigInt *bigint) { + uint64_t value64 = bigint_as_unsigned(bigint); + size_t valueUsize = (size_t)value64; + assert (value64 == valueUsize); + return valueUsize; +} + int64_t bigint_as_signed(const BigInt *bigint) { if (bigint->digit_count == 0) { return 0; diff --git a/src/bigint.hpp b/src/bigint.hpp index 48b222a227..276ccd64f9 100644 --- a/src/bigint.hpp +++ b/src/bigint.hpp @@ -36,7 +36,10 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op); void bigint_init_data(BigInt *dest, const uint64_t *digits, size_t digit_count, bool is_negative); // panics if number won't fit -uint64_t bigint_as_unsigned(const BigInt *bigint); +uint64_t bigint_as_u64(const BigInt *bigint); +uint32_t bigint_as_u32(const BigInt *bigint); +size_t bigint_as_usize(const BigInt *bigint); + int64_t bigint_as_signed(const BigInt *bigint); static inline const uint64_t *bigint_ptr(const BigInt *bigint) { diff --git a/src/codegen.cpp b/src/codegen.cpp index d758d34192..e869201294 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2872,7 +2872,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in eval_min_max_value_int(g, int_type, &biggest_possible_err_val, true); if (bigint_fits_in_bits(&biggest_possible_err_val, 64, false) && - bigint_as_unsigned(&biggest_possible_err_val) < g->errors_by_index.length) + bigint_as_usize(&biggest_possible_err_val) < g->errors_by_index.length) { ok_bit = neq_zero_bit; } else { @@ -3052,8 +3052,10 @@ static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, IrExecutable *ex IrInstructionPtrOfArrayToSlice *instruction) { ZigType *actual_type = instruction->operand->value.type; - LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand); - assert(expr_val); + ZigType *slice_type = instruction->base.value.type; + ZigType *slice_ptr_type = slice_type->data.structure.fields[slice_ptr_index].type_entry; + size_t ptr_index = slice_type->data.structure.fields[slice_ptr_index].gen_index; + size_t len_index = slice_type->data.structure.fields[slice_len_index].gen_index; LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc); @@ -3061,15 +3063,21 @@ static LLVMValueRef ir_render_ptr_of_array_to_slice(CodeGen *g, IrExecutable *ex ZigType *array_type = actual_type->data.pointer.child_type; assert(array_type->id == ZigTypeIdArray); - LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, result_loc, slice_ptr_index, ""); - LLVMValueRef indices[] = { - LLVMConstNull(g->builtin_types.entry_usize->llvm_type), - LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false), - }; - LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, ""); - gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false); + if (type_has_bits(actual_type)) { + LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, result_loc, ptr_index, ""); + LLVMValueRef indices[] = { + LLVMConstNull(g->builtin_types.entry_usize->llvm_type), + LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false), + }; + LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand); + LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, ""); + gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false); + } else if (ir_want_runtime_safety(g, &instruction->base)) { + LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, result_loc, ptr_index, ""); + gen_undef_init(g, slice_ptr_type->abi_align, slice_ptr_type, ptr_field_ptr); + } - LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, result_loc, slice_len_index, ""); + LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, result_loc, len_index, ""); LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, array_type->data.array.len, false); gen_store_untyped(g, len_value, len_field_ptr, 0, false); @@ -3386,6 +3394,8 @@ static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) { static bool value_is_all_undef(ConstExprValue *const_val) { switch (const_val->special) { + case ConstValSpecialLazy: + zig_unreachable(); case ConstValSpecialRuntime: return false; case ConstValSpecialUndef: @@ -3525,6 +3535,8 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir } static LLVMValueRef ir_render_var_ptr(CodeGen *g, IrExecutable *executable, IrInstructionVarPtr *instruction) { + if (instruction->base.value.special != ConstValSpecialRuntime) + return ir_llvm_value(g, &instruction->base); ZigVar *var = instruction->var; if (type_has_bits(var->var_type)) { assert(var->value_ref); @@ -6041,6 +6053,7 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, ConstExprValue *const_val) { switch (const_val->special) { + case ConstValSpecialLazy: case ConstValSpecialRuntime: zig_unreachable(); case ConstValSpecialUndef: @@ -6300,6 +6313,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c assert(type_has_bits(type_entry)); switch (const_val->special) { + case ConstValSpecialLazy: + zig_unreachable(); case ConstValSpecialRuntime: zig_unreachable(); case ConstValSpecialUndef: @@ -6563,7 +6578,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c uint64_t pad_bytes = type_entry->data.unionation.union_abi_size - field_type_bytes; LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, ""); make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_value->type, correctly_typed_value) || - payload_value->type != type_entry->data.unionation.most_aligned_union_member; + payload_value->type != type_entry->data.unionation.most_aligned_union_member->type_entry; { if (pad_bytes == 0) { @@ -8905,7 +8920,7 @@ static void gen_root_source(CodeGen *g) { } Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic")); assert(panic_tld != nullptr); - resolve_top_level_decl(g, panic_tld, nullptr); + resolve_top_level_decl(g, panic_tld, nullptr, false); } diff --git a/src/ir.cpp b/src/ir.cpp index cdddce1b18..52cf69de82 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -152,11 +152,6 @@ struct ConstCastBadAllowsZero { }; -enum UndefAllowed { - UndefOk, - UndefBad, -}; - static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope); static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *scope, LVal lval, ResultLoc *result_loc); @@ -234,6 +229,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c } case ConstPtrSpecialBaseStruct: { ConstExprValue *struct_val = const_val->data.x_ptr.data.base_struct.struct_val; + expand_undef_struct(g, struct_val); result = &struct_val->data.x_struct.fields[const_val->data.x_ptr.data.base_struct.field_index]; break; } @@ -402,7 +398,7 @@ static void ir_ref_var(ZigVar *var) { ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) { ConstExprValue *result = ir_eval_const_value(ira->codegen, scope, node, ira->codegen->builtin_types.entry_type, ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr, nullptr, - node, nullptr, ira->new_irb.exec, nullptr); + node, nullptr, ira->new_irb.exec, nullptr, UndefBad); if (type_is_invalid(result->type)) return ira->codegen->builtin_types.entry_invalid; @@ -5766,7 +5762,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode buf_sprintf("value %s too large for u32 bit offset", buf_ptr(val_buf))); return irb->codegen->invalid_instruction; } - bit_offset_start = bigint_as_unsigned(node->data.pointer_type.bit_offset_start); + bit_offset_start = bigint_as_u32(node->data.pointer_type.bit_offset_start); } uint32_t host_int_bytes = 0; @@ -5778,7 +5774,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode buf_sprintf("value %s too large for u32 byte count", buf_ptr(val_buf))); return irb->codegen->invalid_instruction; } - host_int_bytes = bigint_as_unsigned(node->data.pointer_type.host_int_bytes); + host_int_bytes = bigint_as_u32(node->data.pointer_type.host_int_bytes); } if (host_int_bytes != 0 && bit_offset_start >= host_int_bytes * 8) { @@ -6858,7 +6854,7 @@ static IrInstruction *ir_gen_asm_expr(IrBuilder *irb, Scope *scope, AstNode *nod uint32_t len = asm_token.end - asm_token.start - 2; add_node_error(irb->codegen, node, - buf_sprintf("could not find '%.*s' in the inputs or outputs.", + buf_sprintf("could not find '%.*s' in the inputs or outputs", len, ptr)); return irb->codegen->invalid_instruction; } @@ -8111,7 +8107,12 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc ir_build_reset_result(irb, scope, node, result_loc); } IrInstruction *result = ir_gen_node_raw(irb, node, scope, lval, result_loc); - irb->exec->invalid = irb->exec->invalid || (result == irb->codegen->invalid_instruction); + if (result == irb->codegen->invalid_instruction) { + if (irb->exec->first_err_trace_msg == nullptr) { + irb->exec->first_err_trace_msg = irb->codegen->trace_err; + } + src_assert(irb->exec->first_err_trace_msg != nullptr, node); + } return result; } @@ -8119,21 +8120,20 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope) { return ir_gen_node_extra(irb, node, scope, LValNone, nullptr); } -static void invalidate_exec(IrExecutable *exec) { - if (exec->invalid) +static void invalidate_exec(IrExecutable *exec, ErrorMsg *msg) { + if (exec->first_err_trace_msg != nullptr) return; - exec->invalid = true; + exec->first_err_trace_msg = msg; for (size_t i = 0; i < exec->tld_list.length; i += 1) { exec->tld_list.items[i]->resolution = TldResolutionInvalid; } if (exec->source_exec != nullptr) - invalidate_exec(exec->source_exec); + invalidate_exec(exec->source_exec, msg); } - bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) { assert(node->owner); @@ -8151,8 +8151,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValNone, nullptr); assert(result); - if (irb->exec->invalid) + if (irb->exec->first_err_trace_msg != nullptr) { + codegen->trace_err = irb->exec->first_err_trace_msg; return false; + } if (!instr_is_unreachable(result)) { ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->source_node, result)); @@ -8181,15 +8183,9 @@ static void ir_add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, Error ir_add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1); } -void ir_add_analysis_trace(IrAnalyze *ira, ErrorMsg *err_msg, Buf *text) { - IrInstruction *old_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index); - add_error_note(ira->codegen, err_msg, old_instruction->source_node, text); - ir_add_call_stack_errors(ira->codegen, ira->new_irb.exec, err_msg, 10); -} - static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) { - invalidate_exec(exec); ErrorMsg *err_msg = add_node_error(codegen, source_node, msg); + invalidate_exec(exec, err_msg); if (exec->parent_exec) { ir_add_call_stack_errors(codegen, exec, err_msg, 10); } @@ -8223,6 +8219,7 @@ static Error eval_comptime_ptr_reinterpret(IrAnalyze *ira, CodeGen *codegen, Ast { Error err; assert(ptr_val->type->id == ZigTypeIdPointer); + assert(ptr_val->special == ConstValSpecialStatic); ConstExprValue tmp = {}; tmp.special = ConstValSpecialStatic; tmp.type = ptr_val->type->data.pointer.child_type; @@ -9016,7 +9013,8 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc } ConstExprValue *const_val = ir_resolve_const(ira, instruction, UndefBad); - assert(const_val != nullptr); + if (const_val == nullptr) + return false; bool const_val_is_int = (const_val->type->id == ZigTypeIdInt || const_val->type->id == ZigTypeIdComptimeInt); bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat); @@ -9376,6 +9374,14 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted result.id = ConstCastResultIdInvalid; return result; } + if ((err = type_resolve(g, wanted_type, ResolveStatusZeroBitsKnown))) { + result.id = ConstCastResultIdInvalid; + return result; + } + if ((err = type_resolve(g, actual_type, ResolveStatusZeroBitsKnown))) { + result.id = ConstCastResultIdInvalid; + return result; + } bool ptr_lens_equal = actual_ptr_type->data.pointer.ptr_len == wanted_ptr_type->data.pointer.ptr_len; if ((ptr_lens_equal || wanted_is_c_ptr || actual_is_c_ptr) && type_has_bits(wanted_type) == type_has_bits(actual_type) && @@ -10634,7 +10640,7 @@ static void ir_finish_bb(IrAnalyze *ira) { static IrInstruction *ir_unreach_error(IrAnalyze *ira) { ira->old_bb_index = SIZE_MAX; - ira->new_irb.exec->invalid = true; + assert(ira->new_irb.exec->first_err_trace_msg != nullptr); return ira->codegen->unreach_instruction; } @@ -10722,32 +10728,57 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio return const_instr; } -static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) { - switch (value->value.special) { - case ConstValSpecialStatic: - return &value->value; - case ConstValSpecialRuntime: - if (!type_has_bits(value->value.type)) { - return &value->value; - } - ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression")); - return nullptr; - case ConstValSpecialUndef: - if (undef_allowed == UndefOk) { - return &value->value; - } else { - ir_add_error(ira, value, buf_sprintf("use of undefined value here causes undefined behavior")); - return nullptr; - } +static Error ir_resolve_const_val(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, + ConstExprValue *val, UndefAllowed undef_allowed) +{ + Error err; + for (;;) { + switch (val->special) { + case ConstValSpecialStatic: + return ErrorNone; + case ConstValSpecialRuntime: + if (!type_has_bits(val->type)) + return ErrorNone; + + exec_add_error_node(codegen, exec, source_node, + buf_sprintf("unable to evaluate constant expression")); + return ErrorSemanticAnalyzeFail; + case ConstValSpecialUndef: + if (undef_allowed == UndefOk || undef_allowed == LazyOk) + return ErrorNone; + + exec_add_error_node(codegen, exec, source_node, + buf_sprintf("use of undefined value here causes undefined behavior")); + return ErrorSemanticAnalyzeFail; + case ConstValSpecialLazy: + if (undef_allowed == LazyOk || undef_allowed == LazyOkNoUndef) + return ErrorNone; + + if ((err = ir_resolve_lazy(codegen, source_node, val))) + return err; + + continue; + } } - zig_unreachable(); +} + +static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) { + Error err; + if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, value->source_node, + &value->value, undef_allowed))) + { + return nullptr; + } + return &value->value; } ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node, ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota, ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name, - IrExecutable *parent_exec, AstNode *expected_type_source_node) + IrExecutable *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef_allowed) { + Error err; + if (expected_type != nullptr && type_is_invalid(expected_type)) return &codegen->invalid_instruction->value; @@ -10761,8 +10792,10 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod ir_executable->begin_scope = scope; ir_gen(codegen, node, scope, ir_executable); - if (ir_executable->invalid) + if (ir_executable->first_err_trace_msg != nullptr) { + codegen->trace_err = ir_executable->first_err_trace_msg; return &codegen->invalid_instruction->value; + } if (codegen->verbose_ir) { fprintf(stderr, "\nSource: "); @@ -10783,8 +10816,9 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod analyzed_executable->backward_branch_quota = backward_branch_quota; analyzed_executable->begin_scope = scope; ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable, expected_type, expected_type_source_node); - if (type_is_invalid(result_type)) + if (type_is_invalid(result_type)) { return &codegen->invalid_instruction->value; + } if (codegen->verbose_ir) { fprintf(stderr, "{ // (analyzed)\n"); @@ -10792,7 +10826,14 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod fprintf(stderr, "}\n"); } - return ir_exec_const_result(codegen, analyzed_executable); + ConstExprValue *result = ir_exec_const_result(codegen, analyzed_executable); + if (type_is_invalid(result->type)) + return &codegen->invalid_instruction->value; + + if ((err = ir_resolve_const_val(codegen, analyzed_executable, node, result, undef_allowed))) + return &codegen->invalid_instruction->value; + + return result; } static ErrorTableEntry *ir_resolve_error(IrAnalyze *ira, IrInstruction *err_value) { @@ -10813,22 +10854,43 @@ static ErrorTableEntry *ir_resolve_error(IrAnalyze *ira, IrInstruction *err_valu return const_val->data.x_err_set; } -static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) { +static ZigType *ir_resolve_const_type(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, + ConstExprValue *val) +{ + Error err; + if ((err = ir_resolve_const_val(codegen, exec, source_node, val, UndefBad))) + return codegen->builtin_types.entry_invalid; + + assert(val->data.x_type != nullptr); + return val->data.x_type; +} + +static ConstExprValue *ir_resolve_type_lazy(IrAnalyze *ira, IrInstruction *type_value) { if (type_is_invalid(type_value->value.type)) - return ira->codegen->builtin_types.entry_invalid; + return nullptr; if (type_value->value.type->id != ZigTypeIdMetaType) { ir_add_error(ira, type_value, buf_sprintf("expected type 'type', found '%s'", buf_ptr(&type_value->value.type->name))); - return ira->codegen->builtin_types.entry_invalid; + return nullptr; } - ConstExprValue *const_val = ir_resolve_const(ira, type_value, UndefBad); - if (!const_val) + Error err; + if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, type_value->source_node, + &type_value->value, LazyOk))) + { + return nullptr; + } + + return &type_value->value; +} + +static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) { + ConstExprValue *val = ir_resolve_type_lazy(ira, type_value); + if (val == nullptr) return ira->codegen->builtin_types.entry_invalid; - assert(const_val->data.x_type != nullptr); - return const_val->data.x_type; + return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, val); } static ZigType *ir_resolve_int_type(IrAnalyze *ira, IrInstruction *type_value) { @@ -11026,14 +11088,21 @@ static IrInstruction *ir_analyze_err_set_cast(IrAnalyze *ira, IrInstruction *sou } static IrInstruction *ir_analyze_frame_ptr_to_anyframe(IrAnalyze *ira, IrInstruction *source_instr, - IrInstruction *value, ZigType *wanted_type) + IrInstruction *frame_ptr, ZigType *wanted_type) { - if (instr_is_comptime(value)) { - zig_panic("TODO comptime frame pointer"); + if (instr_is_comptime(frame_ptr)) { + ConstExprValue *ptr_val = ir_resolve_const(ira, frame_ptr, UndefBad); + if (ptr_val == nullptr) + return ira->codegen->invalid_instruction; + + ir_assert(ptr_val->type->id == ZigTypeIdPointer, source_instr); + if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) { + zig_panic("TODO comptime frame pointer"); + } } IrInstruction *result = ir_build_cast(&ira->new_irb, source_instr->scope, source_instr->source_node, - wanted_type, value, CastOpBitCast); + wanted_type, frame_ptr, CastOpBitCast); result->value.type = wanted_type; return result; } @@ -11152,6 +11221,9 @@ static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instructi ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value->value.type, is_const, is_volatile, PtrLenSingle, 0, 0, 0, false); + if ((err = type_resolve(ira->codegen, ptr_type, ResolveStatusZeroBitsKnown))) + return ira->codegen->invalid_instruction; + IrInstruction *result_loc; if (type_has_bits(ptr_type) && !handle_is_ptr(value->value.type)) { result_loc = ir_resolve_result(ira, source_instruction, no_result_loc(), value->value.type, nullptr, true, @@ -11322,7 +11394,7 @@ static IrInstruction *ir_analyze_undefined_to_anything(IrAnalyze *ira, IrInstruc IrInstruction *target, ZigType *wanted_type) { IrInstruction *result = ir_const(ira, source_instr, wanted_type); - init_const_undefined(ira->codegen, &result->value); + result->value.special = ConstValSpecialUndef; return result; } @@ -11345,10 +11417,13 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so return ira->codegen->invalid_instruction; TypeUnionField *union_field = find_union_field_by_tag(wanted_type, &val->data.x_enum_tag); assert(union_field != nullptr); - if ((err = type_resolve(ira->codegen, union_field->type_entry, ResolveStatusZeroBitsKnown))) + ZigType *field_type = resolve_union_field_type(ira->codegen, union_field); + if (field_type == nullptr) + return ira->codegen->invalid_instruction; + if ((err = type_resolve(ira->codegen, field_type, ResolveStatusZeroBitsKnown))) return ira->codegen->invalid_instruction; - switch (type_has_one_possible_value(ira->codegen, union_field->type_entry)) { + switch (type_has_one_possible_value(ira->codegen, field_type)) { case OnePossibleValueInvalid: return ira->codegen->invalid_instruction; case OnePossibleValueNo: { @@ -11357,7 +11432,7 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("cast to union '%s' must initialize '%s' field '%s'", buf_ptr(&wanted_type->name), - buf_ptr(&union_field->type_entry->name), + buf_ptr(&field_type->name), buf_ptr(union_field->name))); add_error_note(ira->codegen, msg, field_node, buf_sprintf("field '%s' declared here", buf_ptr(union_field->name))); @@ -11373,7 +11448,7 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so bigint_init_bigint(&result->value.data.x_union.tag, &val->data.x_enum_tag); result->value.data.x_union.payload = create_const_vals(1); result->value.data.x_union.payload->special = ConstValSpecialStatic; - result->value.data.x_union.payload->type = union_field->type_entry; + result->value.data.x_union.payload->type = field_type; return result; } @@ -11390,12 +11465,17 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so buf_ptr(&wanted_type->name))); for (uint32_t i = 0; i < wanted_type->data.unionation.src_field_count; i += 1) { TypeUnionField *union_field = &wanted_type->data.unionation.fields[i]; - if (type_has_bits(union_field->type_entry)) { + ZigType *field_type = resolve_union_field_type(ira->codegen, union_field); + if (field_type == nullptr) + return ira->codegen->invalid_instruction; + if ((err = type_resolve(ira->codegen, field_type, ResolveStatusZeroBitsKnown))) + return ira->codegen->invalid_instruction; + if (type_has_bits(field_type)) { AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(i); add_error_note(ira->codegen, msg, field_node, buf_sprintf("field '%s' has type '%s'", buf_ptr(union_field->name), - buf_ptr(&union_field->type_entry->name))); + buf_ptr(&field_type->name))); } } return ira->codegen->invalid_instruction; @@ -11461,7 +11541,7 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour ZigType *actual_type = target->value.type; - if ((err = ensure_complete_type(ira->codegen, wanted_type))) + if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; if (actual_type != wanted_type->data.enumeration.tag_int_type) { @@ -11550,7 +11630,7 @@ static IrInstruction *ir_analyze_int_to_err(IrAnalyze *ira, IrInstruction *sourc return ira->codegen->invalid_instruction; } - size_t index = bigint_as_unsigned(&val->data.x_bigint); + size_t index = bigint_as_usize(&val->data.x_bigint); result->value.data.x_err_set = ira->codegen->errors_by_index.at(index); return result; } else { @@ -12508,6 +12588,29 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc return ir_build_load_ptr_gen(ira, source_instruction, ptr, child_type, result_loc_inst); } +static bool ir_resolve_const_align(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, + ConstExprValue *const_val, uint32_t *out) +{ + Error err; + if ((err = ir_resolve_const_val(codegen, exec, source_node, const_val, UndefBad))) + return false; + + uint32_t align_bytes = bigint_as_u32(&const_val->data.x_bigint); + if (align_bytes == 0) { + exec_add_error_node(codegen, exec, source_node, buf_sprintf("alignment must be >= 1")); + return false; + } + + if (!is_power_of_2(align_bytes)) { + exec_add_error_node(codegen, exec, source_node, + buf_sprintf("alignment value %" PRIu32 " is not a power of 2", align_bytes)); + return false; + } + + *out = align_bytes; + return true; +} + static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) { if (type_is_invalid(value->value.type)) return false; @@ -12516,23 +12619,8 @@ static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out if (type_is_invalid(casted_value->value.type)) return false; - ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad); - if (!const_val) - return false; - - uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint); - if (align_bytes == 0) { - ir_add_error(ira, value, buf_sprintf("alignment must be >= 1")); - return false; - } - - if (!is_power_of_2(align_bytes)) { - ir_add_error(ira, value, buf_sprintf("alignment value %" PRIu32 " is not a power of 2", align_bytes)); - return false; - } - - *out = align_bytes; - return true; + return ir_resolve_const_align(ira->codegen, ira->new_irb.exec, value->source_node, + &casted_value->value, out); } static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *int_type, uint64_t *out) { @@ -12547,7 +12635,7 @@ static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *i if (!const_val) return false; - *out = bigint_as_unsigned(&const_val->data.x_bigint); + *out = bigint_as_u64(&const_val->data.x_bigint); return true; } @@ -12595,7 +12683,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic if (!const_val) return false; - *out = (AtomicOrder)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (AtomicOrder)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12615,7 +12703,7 @@ static bool ir_resolve_atomic_rmw_op(IrAnalyze *ira, IrInstruction *value, Atomi if (!const_val) return false; - *out = (AtomicRmwOp)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (AtomicRmwOp)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12635,7 +12723,7 @@ static bool ir_resolve_global_linkage(IrAnalyze *ira, IrInstruction *value, Glob if (!const_val) return false; - *out = (GlobalLinkageId)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (GlobalLinkageId)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12655,7 +12743,7 @@ static bool ir_resolve_float_mode(IrAnalyze *ira, IrInstruction *value, FloatMod if (!const_val) return false; - *out = (FloatMode)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (FloatMode)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12684,7 +12772,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) { return array_val->data.x_array.data.s_buf; } expand_undef_array(ira->codegen, array_val); - size_t len = bigint_as_unsigned(&len_field->data.x_bigint); + size_t len = bigint_as_usize(&len_field->data.x_bigint); Buf *result = buf_alloc(); buf_resize(result, len); for (size_t i = 0; i < len; i += 1) { @@ -12694,7 +12782,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) { ir_add_error(ira, casted_value, buf_sprintf("use of undefined value")); return nullptr; } - uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint); + uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint); assert(big_c <= UINT8_MAX); uint8_t c = (uint8_t)big_c; buf_ptr(result)[i] = c; @@ -12719,7 +12807,9 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio if (type_is_invalid(operand->value.type)) return ir_unreach_error(ira); - if (!instr_is_comptime(operand) && handle_is_ptr(ira->explicit_return_type)) { + if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr && + handle_is_ptr(ira->explicit_return_type)) + { // result location mechanism took care of it. IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope, instruction->base.source_node, nullptr); @@ -13576,21 +13666,34 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp if (type_is_invalid(casted_op2->value.type)) return ira->codegen->invalid_instruction; - if (op1->value.special == ConstValSpecialUndef || casted_op2->value.special == ConstValSpecialUndef) { - IrInstruction *result = ir_const(ira, &instruction->base, op1->value.type); - result->value.special = ConstValSpecialUndef; - return result; + // If either operand is undef, result is undef. + ConstExprValue *op1_val = nullptr; + ConstExprValue *op2_val = nullptr; + if (instr_is_comptime(op1)) { + op1_val = ir_resolve_const(ira, op1, UndefOk); + if (op1_val == nullptr) + return ira->codegen->invalid_instruction; + if (op1_val->special == ConstValSpecialUndef) + return ir_const_undef(ira, &instruction->base, op1->value.type); } - if (casted_op2->value.special == ConstValSpecialStatic && op1->value.special == ConstValSpecialStatic && + if (instr_is_comptime(casted_op2)) { + op2_val = ir_resolve_const(ira, casted_op2, UndefOk); + if (op2_val == nullptr) + return ira->codegen->invalid_instruction; + if (op2_val->special == ConstValSpecialUndef) + return ir_const_undef(ira, &instruction->base, op1->value.type); + } + + if (op2_val != nullptr && op1_val != nullptr && (op1->value.data.x_ptr.special == ConstPtrSpecialHardCodedAddr || op1->value.data.x_ptr.special == ConstPtrSpecialNull)) { - uint64_t start_addr = (op1->value.data.x_ptr.special == ConstPtrSpecialNull) ? - 0 : op1->value.data.x_ptr.data.hard_coded_addr.addr; + uint64_t start_addr = (op1_val->data.x_ptr.special == ConstPtrSpecialNull) ? + 0 : op1_val->data.x_ptr.data.hard_coded_addr.addr; uint64_t elem_offset; if (!ir_resolve_usize(ira, casted_op2, &elem_offset)) return ira->codegen->invalid_instruction; - ZigType *elem_type = op1->value.type->data.pointer.child_type; + ZigType *elem_type = op1_val->type->data.pointer.child_type; if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; uint64_t byte_offset = type_size(ira->codegen, elem_type) * elem_offset; @@ -13602,7 +13705,7 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp } else { zig_unreachable(); } - IrInstruction *result = ir_const(ira, &instruction->base, op1->value.type); + IrInstruction *result = ir_const(ira, &instruction->base, op1_val->type); result->value.data.x_ptr.special = ConstPtrSpecialHardCodedAddr; result->value.data.x_ptr.mut = ConstPtrMutRuntimeVar; result->value.data.x_ptr.data.hard_coded_addr.addr = new_addr; @@ -13829,7 +13932,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i op1_array_val = ptr_val->data.x_ptr.data.base_array.array_val; op1_array_index = ptr_val->data.x_ptr.data.base_array.elem_index; ConstExprValue *len_val = &op1_val->data.x_struct.fields[slice_len_index]; - op1_array_end = op1_array_index + bigint_as_unsigned(&len_val->data.x_bigint); + op1_array_end = op1_array_index + bigint_as_usize(&len_val->data.x_bigint); } else { ir_add_error(ira, op1, buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name))); @@ -13862,7 +13965,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i op2_array_val = ptr_val->data.x_ptr.data.base_array.array_val; op2_array_index = ptr_val->data.x_ptr.data.base_array.elem_index; ConstExprValue *len_val = &op2_val->data.x_struct.fields[slice_len_index]; - op2_array_end = op2_array_index + bigint_as_unsigned(&len_val->data.x_bigint); + op2_array_end = op2_array_index + bigint_as_usize(&len_val->data.x_bigint); } else { ir_add_error(ira, op2, buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name))); @@ -14171,9 +14274,13 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, } break; case ReqCompTimeNo: - if (init_val != nullptr) { - if (init_val->special == ConstValSpecialStatic && - init_val->type->id == ZigTypeIdFn && + if (init_val != nullptr && value_is_comptime(init_val)) { + if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, + decl_var_instruction->base.source_node, init_val, UndefOk))) + { + result_type = ira->codegen->builtin_types.entry_invalid; + } else if (init_val->type->id == ZigTypeIdFn && + init_val->special != ConstValSpecialUndef && init_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr && init_val->data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways) { @@ -14231,7 +14338,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, } } - if (init_val != nullptr && init_val->special != ConstValSpecialRuntime) { + if (init_val != nullptr && value_is_comptime(init_val)) { // Resolve ConstPtrMutInfer if (var->gen_is_const) { var_ptr->value.data.x_ptr.mut = ConstPtrMutComptimeConst; @@ -14242,6 +14349,10 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, // since it's a comptime val there are no instructions for it. // we memcpy the init value here IrInstruction *deref = ir_get_deref(ira, var_ptr, var_ptr, nullptr); + if (type_is_invalid(deref->value.type)) { + var->var_type = ira->codegen->builtin_types.entry_invalid; + return ira->codegen->invalid_instruction; + } // If this assertion trips, something is wrong with the IR instructions, because // we expected the above deref to return a constant value, but it created a runtime // instruction. @@ -14250,7 +14361,7 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira, ir_analyze_store_ptr(ira, var_ptr, var_ptr, deref, false); } - if (var_ptr->value.special == ConstValSpecialStatic && var->mem_slot_index != SIZE_MAX) { + if (instr_is_comptime(var_ptr) && var->mem_slot_index != SIZE_MAX) { assert(var->mem_slot_index < ira->exec_context.mem_slot_list.length); ConstExprValue *mem_slot = ira->exec_context.mem_slot_list.at(var->mem_slot_index); copy_const_val(mem_slot, init_val, !is_comptime_var || var->gen_is_const); @@ -14515,28 +14626,23 @@ static IrInstruction *ir_analyze_instruction_error_return_trace(IrAnalyze *ira, static IrInstruction *ir_analyze_instruction_error_union(IrAnalyze *ira, IrInstructionErrorUnion *instruction) { - Error err; + IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type); + result->value.special = ConstValSpecialLazy; - ZigType *err_set_type = ir_resolve_type(ira, instruction->err_set->child); - if (type_is_invalid(err_set_type)) + LazyValueErrUnionType *lazy_err_union_type = allocate(1); + lazy_err_union_type->ira = ira; + result->value.data.x_lazy = &lazy_err_union_type->base; + lazy_err_union_type->base.id = LazyValueIdErrUnionType; + + lazy_err_union_type->err_set_type = instruction->err_set->child; + if (ir_resolve_type_lazy(ira, lazy_err_union_type->err_set_type) == nullptr) return ira->codegen->invalid_instruction; - ZigType *payload_type = ir_resolve_type(ira, instruction->payload->child); - if (type_is_invalid(payload_type)) + lazy_err_union_type->payload_type = instruction->payload->child; + if (ir_resolve_type_lazy(ira, lazy_err_union_type->payload_type) == nullptr) return ira->codegen->invalid_instruction; - if (err_set_type->id != ZigTypeIdErrorSet) { - ir_add_error(ira, instruction->err_set->child, - buf_sprintf("expected error set type, found type '%s'", - buf_ptr(&err_set_type->name))); - return ira->codegen->invalid_instruction; - } - - if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown))) - return ira->codegen->invalid_instruction; - ZigType *result_type = get_error_union_type(ira->codegen, err_set_type, payload_type); - - return ir_const_type(ira, &instruction->base, result_type); + return result; } static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_inst, ZigType *var_type, @@ -14555,6 +14661,10 @@ static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_in if ((err = type_resolve(ira->codegen, var_type, ResolveStatusZeroBitsKnown))) return ira->codegen->invalid_instruction; + if (align != 0) { + if ((err = type_resolve(ira->codegen, var_type, ResolveStatusAlignmentKnown))) + return ira->codegen->invalid_instruction; + } assert(result->base.value.data.x_ptr.special != ConstPtrSpecialInvalid); pointee->type = var_type; @@ -15174,23 +15284,25 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, bool comptime_var_mem = ir_get_var_is_comptime(var); bool linkage_makes_it_runtime = var->decl_node->data.variable_declaration.is_extern; - bool is_const = var->src_is_const; bool is_volatile = false; + IrInstruction *result = ir_build_var_ptr(&ira->new_irb, + instruction->scope, instruction->source_node, var); + result->value.type = get_pointer_to_type_extra(ira->codegen, var->var_type, + var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0, false); + if (linkage_makes_it_runtime) goto no_mem_slot; - if (var->const_value->special == ConstValSpecialStatic) { + if (value_is_comptime(var->const_value)) { mem_slot = var->const_value; - } else { - if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) { - // find the relevant exec_context - assert(var->owner_exec != nullptr); - assert(var->owner_exec->analysis != nullptr); - IrExecContext *exec_context = &var->owner_exec->analysis->exec_context; - assert(var->mem_slot_index < exec_context->mem_slot_list.length); - mem_slot = exec_context->mem_slot_list.at(var->mem_slot_index); - } + } else if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) { + // find the relevant exec_context + assert(var->owner_exec != nullptr); + assert(var->owner_exec->analysis != nullptr); + IrExecContext *exec_context = &var->owner_exec->analysis->exec_context; + assert(var->mem_slot_index < exec_context->mem_slot_list.length); + mem_slot = exec_context->mem_slot_list.at(var->mem_slot_index); } if (mem_slot != nullptr) { @@ -15198,6 +15310,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, case ConstValSpecialRuntime: goto no_mem_slot; case ConstValSpecialStatic: // fallthrough + case ConstValSpecialLazy: // fallthrough case ConstValSpecialUndef: { ConstPtrMut ptr_mut; if (comptime_var_mem) { @@ -15208,8 +15321,11 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, assert(!comptime_var_mem); ptr_mut = ConstPtrMutRuntimeVar; } - return ir_get_const_ptr(ira, instruction, mem_slot, var->var_type, - ptr_mut, is_const, is_volatile, var->align_bytes); + result->value.special = ConstValSpecialStatic; + result->value.data.x_ptr.mut = ptr_mut; + result->value.data.x_ptr.special = ConstPtrSpecialRef; + result->value.data.x_ptr.data.ref.pointee = mem_slot; + return result; } } zig_unreachable(); @@ -15217,15 +15333,10 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, no_mem_slot: - IrInstruction *var_ptr_instruction = ir_build_var_ptr(&ira->new_irb, - instruction->scope, instruction->source_node, var); - var_ptr_instruction->value.type = get_pointer_to_type_extra(ira->codegen, var->var_type, - var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0, false); - bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr); - var_ptr_instruction->value.data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack; + result->value.data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack; - return var_ptr_instruction; + return result; } // This function is called when a comptime value becomes accessible at runtime. @@ -15489,7 +15600,8 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c AstNode *body_node = fn_entry->body_node; result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type, ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry, - nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node); + nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node, + UndefOk); if (inferred_err_set_type != nullptr) { inferred_err_set_type->data.error_set.infer_fn = nullptr; @@ -15513,8 +15625,9 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c ira->codegen->memoized_fn_eval_table.put(exec_scope, result); } - if (type_is_invalid(result->type)) + if (type_is_invalid(result->type)) { return ira->codegen->invalid_instruction; + } } IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type); @@ -15685,7 +15798,8 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c ConstExprValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen), ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, - nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr); + nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, + nullptr, UndefBad); IrInstructionConst *const_instruction = ir_create_instruction(&ira->new_irb, impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr); copy_const_val(&const_instruction->base.value, align_result, true); @@ -16066,51 +16180,20 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source zig_unreachable(); } -static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) { - Error err; - IrInstruction *value = un_op_instruction->value->child; - ZigType *type_entry = ir_resolve_type(ira, value); - if (type_is_invalid(type_entry)) - return ira->codegen->invalid_instruction; - if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown))) +static IrInstruction *ir_analyze_optional_type(IrAnalyze *ira, IrInstructionUnOp *instruction) { + IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type); + result->value.special = ConstValSpecialLazy; + + LazyValueOptType *lazy_opt_type = allocate(1); + lazy_opt_type->ira = ira; + result->value.data.x_lazy = &lazy_opt_type->base; + lazy_opt_type->base.id = LazyValueIdOptType; + + lazy_opt_type->payload_type = instruction->value->child; + if (ir_resolve_type_lazy(ira, lazy_opt_type->payload_type) == nullptr) return ira->codegen->invalid_instruction; - switch (type_entry->id) { - case ZigTypeIdInvalid: - zig_unreachable(); - case ZigTypeIdMetaType: - case ZigTypeIdVoid: - case ZigTypeIdBool: - case ZigTypeIdInt: - case ZigTypeIdVector: - case ZigTypeIdFloat: - case ZigTypeIdPointer: - case ZigTypeIdArray: - case ZigTypeIdStruct: - case ZigTypeIdComptimeFloat: - case ZigTypeIdComptimeInt: - case ZigTypeIdEnumLiteral: - case ZigTypeIdUndefined: - case ZigTypeIdNull: - case ZigTypeIdOptional: - case ZigTypeIdErrorUnion: - case ZigTypeIdErrorSet: - case ZigTypeIdEnum: - case ZigTypeIdUnion: - case ZigTypeIdFn: - case ZigTypeIdBoundFn: - case ZigTypeIdArgTuple: - case ZigTypeIdFnFrame: - case ZigTypeIdAnyFrame: - return ir_const_type(ira, &un_op_instruction->base, get_optional_type(ira->codegen, type_entry)); - - case ZigTypeIdUnreachable: - case ZigTypeIdOpaque: - ir_add_error_node(ira, un_op_instruction->base.source_node, - buf_sprintf("type '%s' not optional", buf_ptr(&type_entry->name))); - return ira->codegen->invalid_instruction; - } - zig_unreachable(); + return result; } static ErrorMsg *ir_eval_negation_scalar(IrAnalyze *ira, IrInstruction *source_instr, ZigType *scalar_type, @@ -16727,14 +16810,14 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct return ira->codegen->invalid_instruction; bool safety_check_on = elem_ptr_instruction->safety_check_on; - if ((err = ensure_complete_type(ira->codegen, return_type->data.pointer.child_type))) + if ((err = type_resolve(ira->codegen, return_type->data.pointer.child_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type); uint64_t abi_align = get_abi_alignment(ira->codegen, return_type->data.pointer.child_type); uint64_t ptr_align = get_ptr_align(ira->codegen, return_type); if (instr_is_comptime(casted_elem_index)) { - uint64_t index = bigint_as_unsigned(&casted_elem_index->value.data.x_bigint); + uint64_t index = bigint_as_u64(&casted_elem_index->value.data.x_bigint); if (array_type->id == ZigTypeIdArray) { uint64_t array_len = array_type->data.array.len; if (index >= array_len) { @@ -16896,7 +16979,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct ConstExprValue *len_field = &array_ptr_val->data.x_struct.fields[slice_len_index]; IrInstruction *result = ir_const(ira, &elem_ptr_instruction->base, return_type); ConstExprValue *out_val = &result->value; - uint64_t slice_len = bigint_as_unsigned(&len_field->data.x_bigint); + uint64_t slice_len = bigint_as_u64(&len_field->data.x_bigint); if (index >= slice_len) { ir_add_error_node(ira, elem_ptr_instruction->base.source_node, buf_sprintf("index %" ZIG_PRI_u64 " outside slice of size %" ZIG_PRI_u64, @@ -17005,7 +17088,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira, auto entry = container_scope->decl_table.maybe_get(field_name); Tld *tld = entry ? entry->value : nullptr; if (tld && tld->id == TldIdFn) { - resolve_top_level_decl(ira->codegen, tld, source_instr->source_node); + resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false); if (tld->resolution == TldResolutionInvalid) return ira->codegen->invalid_instruction; TldFn *tld_fn = (TldFn *)tld; @@ -17038,27 +17121,31 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira, static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction *source_instr, TypeStructField *field, IrInstruction *struct_ptr, ZigType *struct_type, bool initializing) { - switch (type_has_one_possible_value(ira->codegen, field->type_entry)) { + Error err; + ZigType *field_type = resolve_struct_field_type(ira->codegen, field); + if (field_type == nullptr) + return ira->codegen->invalid_instruction; + switch (type_has_one_possible_value(ira->codegen, field_type)) { case OnePossibleValueInvalid: return ira->codegen->invalid_instruction; case OnePossibleValueYes: { - IrInstruction *elem = ir_const(ira, source_instr, field->type_entry); + IrInstruction *elem = ir_const(ira, source_instr, field_type); return ir_get_ref(ira, source_instr, elem, false, false); } case OnePossibleValueNo: break; } + if ((err = type_resolve(ira->codegen, struct_type, ResolveStatusAlignmentKnown))) + return ira->codegen->invalid_instruction; assert(struct_ptr->value.type->id == ZigTypeIdPointer); - bool is_packed = (struct_type->data.structure.layout == ContainerLayoutPacked); - uint32_t align_bytes = is_packed ? 1 : get_abi_alignment(ira->codegen, field->type_entry); uint32_t ptr_bit_offset = struct_ptr->value.type->data.pointer.bit_offset_in_host; uint32_t ptr_host_int_bytes = struct_ptr->value.type->data.pointer.host_int_bytes; uint32_t host_int_bytes_for_result_type = (ptr_host_int_bytes == 0) ? get_host_int_bytes(ira->codegen, struct_type, field) : ptr_host_int_bytes; bool is_const = struct_ptr->value.type->data.pointer.is_const; bool is_volatile = struct_ptr->value.type->data.pointer.is_volatile; - ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry, - is_const, is_volatile, PtrLenSingle, align_bytes, + ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type, + is_const, is_volatile, PtrLenSingle, field->align, (uint32_t)(ptr_bit_offset + field->bit_offset_in_host), (uint32_t)host_int_bytes_for_result_type, false); if (instr_is_comptime(struct_ptr)) { @@ -17113,7 +17200,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ Error err; ZigType *bare_type = container_ref_type(container_type); - if ((err = ensure_complete_type(ira->codegen, bare_type))) + if ((err = type_resolve(ira->codegen, bare_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; assert(container_ptr->value.type->id == ZigTypeIdPointer); @@ -17243,23 +17330,22 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name, } static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) { - ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected")); - emit_error_notes_for_ref_stack(ira->codegen, msg); + ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected")); return ira->codegen->invalid_instruction; } static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) { - resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node); - if (tld->resolution == TldResolutionInvalid) + resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node, true); + if (tld->resolution == TldResolutionInvalid) { return ira->codegen->invalid_instruction; + } switch (tld->id) { case TldIdContainer: case TldIdCompTime: case TldIdUsingNamespace: zig_unreachable(); - case TldIdVar: - { + case TldIdVar: { TldVar *tld_var = (TldVar *)tld; ZigVar *var = tld_var->var; if (var == nullptr) { @@ -17271,8 +17357,7 @@ static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_ return ir_get_var_ptr(ira, source_instruction, var); } - case TldIdFn: - { + case TldIdFn: { TldFn *tld_fn = (TldFn *)tld; ZigFn *fn_entry = tld_fn->fn_entry; assert(fn_entry->type_entry); @@ -17396,7 +17481,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc return ira->codegen->invalid_instruction; } else if (is_container(child_type)) { if (child_type->id == ZigTypeIdEnum) { - if ((err = ensure_complete_type(ira->codegen, child_type))) + if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; TypeEnumField *field = find_enum_type_field(child_type, field_name); @@ -17425,7 +17510,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr || child_type->data.unionation.decl_node->data.container_decl.auto_enum)) { - if ((err = ensure_complete_type(ira->codegen, child_type))) + if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; TypeUnionField *field = find_union_type_field(child_type, field_name); if (field) { @@ -17844,65 +17929,29 @@ static IrInstruction *ir_analyze_instruction_any_frame_type(IrAnalyze *ira, static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira, IrInstructionSliceType *slice_type_instruction) { - Error err; - uint32_t align_bytes = 0; + IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type); + result->value.special = ConstValSpecialLazy; + + LazyValueSliceType *lazy_slice_type = allocate(1); + lazy_slice_type->ira = ira; + result->value.data.x_lazy = &lazy_slice_type->base; + lazy_slice_type->base.id = LazyValueIdSliceType; + if (slice_type_instruction->align_value != nullptr) { - if (!ir_resolve_align(ira, slice_type_instruction->align_value->child, &align_bytes)) + lazy_slice_type->align_inst = slice_type_instruction->align_value->child; + if (ir_resolve_const(ira, lazy_slice_type->align_inst, LazyOk) == nullptr) return ira->codegen->invalid_instruction; } - ZigType *child_type = ir_resolve_type(ira, slice_type_instruction->child_type->child); - if (type_is_invalid(child_type)) + lazy_slice_type->elem_type = slice_type_instruction->child_type->child; + if (ir_resolve_type_lazy(ira, lazy_slice_type->elem_type) == nullptr) return ira->codegen->invalid_instruction; - bool is_const = slice_type_instruction->is_const; - bool is_volatile = slice_type_instruction->is_volatile; - bool is_allow_zero = slice_type_instruction->is_allow_zero; + lazy_slice_type->is_const = slice_type_instruction->is_const; + lazy_slice_type->is_volatile = slice_type_instruction->is_volatile; + lazy_slice_type->is_allowzero = slice_type_instruction->is_allow_zero; - switch (child_type->id) { - case ZigTypeIdInvalid: // handled above - zig_unreachable(); - case ZigTypeIdUnreachable: - case ZigTypeIdUndefined: - case ZigTypeIdNull: - case ZigTypeIdArgTuple: - case ZigTypeIdOpaque: - ir_add_error_node(ira, slice_type_instruction->base.source_node, - buf_sprintf("slice of type '%s' not allowed", buf_ptr(&child_type->name))); - return ira->codegen->invalid_instruction; - case ZigTypeIdMetaType: - case ZigTypeIdVoid: - case ZigTypeIdBool: - case ZigTypeIdInt: - case ZigTypeIdFloat: - case ZigTypeIdPointer: - case ZigTypeIdArray: - case ZigTypeIdStruct: - case ZigTypeIdComptimeFloat: - case ZigTypeIdComptimeInt: - case ZigTypeIdEnumLiteral: - case ZigTypeIdOptional: - case ZigTypeIdErrorUnion: - case ZigTypeIdErrorSet: - case ZigTypeIdEnum: - case ZigTypeIdUnion: - case ZigTypeIdFn: - case ZigTypeIdBoundFn: - case ZigTypeIdVector: - case ZigTypeIdFnFrame: - case ZigTypeIdAnyFrame: - { - ResolveStatus needed_status = (align_bytes == 0) ? - ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown; - if ((err = type_resolve(ira->codegen, child_type, needed_status))) - return ira->codegen->invalid_instruction; - ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type, - is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0, is_allow_zero); - ZigType *result_type = get_slice_type(ira->codegen, slice_ptr_type); - return ir_const_type(ira, &slice_type_instruction->base, result_type); - } - } - zig_unreachable(); + return result; } static IrInstruction *ir_analyze_instruction_global_asm(IrAnalyze *ira, IrInstructionGlobalAsm *instruction) { @@ -18008,7 +18057,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira, case ZigTypeIdFnFrame: case ZigTypeIdAnyFrame: { - if ((err = ensure_complete_type(ira->codegen, child_type))) + if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; ZigType *result_type = get_array_type(ira->codegen, child_type, size); return ir_const_type(ira, &array_type_instruction->base, result_type); @@ -18024,7 +18073,7 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira, IrInstruction *type_value = size_of_instruction->type_value->child; ZigType *type_entry = ir_resolve_type(ira, type_value); - if ((err = ensure_complete_type(ira->codegen, type_entry))) + if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; switch (type_entry->id) { @@ -18529,7 +18578,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira, if (pointee_val->special == ConstValSpecialRuntime) pointee_val = nullptr; } - if ((err = ensure_complete_type(ira->codegen, target_type))) + if ((err = type_resolve(ira->codegen, target_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; switch (target_type->id) { @@ -19070,7 +19119,7 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc Scope *analyze_scope = &get_container_scope(container_type)->base; // memoize it field->init_val = analyze_const_value(ira->codegen, analyze_scope, init_node, - field->type_entry, nullptr); + field->type_entry, nullptr, UndefOk); } if (type_is_invalid(field->init_val->type)) return ira->codegen->invalid_instruction; @@ -19276,8 +19325,7 @@ static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira, if (!msg_buf) return ira->codegen->invalid_instruction; - ErrorMsg *msg = ir_add_error(ira, &instruction->base, msg_buf); - emit_error_notes_for_ref_stack(ira->codegen, msg); + ir_add_error(ira, &instruction->base, msg_buf); return ira->codegen->invalid_instruction; } @@ -19319,7 +19367,10 @@ static IrInstruction *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruct ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8, true, false, PtrLenUnknown, 0, 0, 0, false); ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type); - if (casted_value->value.special == ConstValSpecialStatic) { + if (instr_is_comptime(casted_value)) { + ConstExprValue *val = ir_resolve_const(ira, casted_value, UndefBad); + if (val == nullptr) + return ira->codegen->invalid_instruction; ErrorTableEntry *err = casted_value->value.data.x_err_set; if (!err->cached_error_name_val) { ConstExprValue *array_val = create_const_str_lit(ira->codegen, &err->name); @@ -19391,7 +19442,7 @@ static IrInstruction *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira, return ira->codegen->invalid_instruction; } - if ((err = ensure_complete_type(ira->codegen, container_type))) + if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; TypeStructField *field = find_struct_type_field(container_type, field_name); @@ -19470,7 +19521,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira, return nullptr; Error err; - if ((err = ensure_complete_type(ira->codegen, container_type))) + if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown))) return nullptr; Buf *field_name = ir_resolve_str(ira, field_name_value); @@ -19574,11 +19625,9 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig ZigVar *var = tld->var; - if ((err = ensure_complete_type(ira->codegen, var->const_value->type))) - return ira->codegen->builtin_types.entry_invalid; - assert(var->const_value->type->id == ZigTypeIdMetaType); - return var->const_value->data.x_type; + + return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, nullptr, var->const_value); } static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr, ConstExprValue *out_val, @@ -19594,15 +19643,15 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr ensure_field_index(type_info_declaration_type, "data", 2); ZigType *type_info_declaration_data_type = ir_type_info_get_type(ira, "Data", type_info_declaration_type); - if ((err = ensure_complete_type(ira->codegen, type_info_declaration_data_type))) + if ((err = type_resolve(ira->codegen, type_info_declaration_data_type, ResolveStatusSizeKnown))) return err; ZigType *type_info_fn_decl_type = ir_type_info_get_type(ira, "FnDecl", type_info_declaration_data_type); - if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_type))) + if ((err = type_resolve(ira->codegen, type_info_fn_decl_type, ResolveStatusSizeKnown))) return err; ZigType *type_info_fn_decl_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_decl_type); - if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_inline_type))) + if ((err = type_resolve(ira->codegen, type_info_fn_decl_inline_type, ResolveStatusSizeKnown))) return err; // Loop through our declarations once to figure out how many declarations we will generate info for. @@ -19613,7 +19662,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr while ((curr_entry = decl_it.next()) != nullptr) { // If the declaration is unresolved, force it to be resolved again. if (curr_entry->value->resolution == TldResolutionUnresolved) { - resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node); + resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false); if (curr_entry->value->resolution != TldResolutionOk) { return ErrorSemanticAnalyzeFail; } @@ -19673,7 +19722,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr case TldIdVar: { ZigVar *var = ((TldVar *)curr_entry->value)->var; - if ((err = ensure_complete_type(ira->codegen, var->const_value->type))) + if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown))) return ErrorSemanticAnalyzeFail; if (var->const_value->type->id == ZigTypeIdMetaType) { @@ -19799,7 +19848,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr case TldIdContainer: { ZigType *type_entry = ((TldContainer *)curr_entry->value)->type_entry; - if ((err = ensure_complete_type(ira->codegen, type_entry))) + if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown))) return ErrorSemanticAnalyzeFail; // This is a type. @@ -19855,7 +19904,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty return nullptr; ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr); - assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_type)); + assertNoError(type_resolve(ira->codegen, type_info_pointer_type, ResolveStatusSizeKnown)); ConstExprValue *result = create_const_vals(1); result->special = ConstValSpecialStatic; @@ -19867,7 +19916,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty // size: Size ensure_field_index(result->type, "size", 0); ZigType *type_info_pointer_size_type = ir_type_info_get_type(ira, "Size", type_info_pointer_type); - assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_size_type)); + assertNoError(type_resolve(ira->codegen, type_info_pointer_size_type, ResolveStatusSizeKnown)); fields[0].special = ConstValSpecialStatic; fields[0].type = type_info_pointer_size_type; bigint_init_unsigned(&fields[0].data.x_enum_tag, size_enum_index); @@ -20345,6 +20394,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr inner_fields[1].special = ConstValSpecialStatic; inner_fields[1].type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int); + ZigType *field_type = resolve_struct_field_type(ira->codegen, struct_field); + if (field_type == nullptr) + return ErrorSemanticAnalyzeFail; + if ((err = type_resolve(ira->codegen, field_type, ResolveStatusZeroBitsKnown))) + return err; if (!type_has_bits(struct_field->type_entry)) { inner_fields[1].data.x_optional = nullptr; } else { @@ -20581,7 +20635,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct ZigType *void_type = ira->codegen->builtin_types.entry_void; ConstExprValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type, ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr, - &cimport_scope->buf, block_node, nullptr, nullptr, nullptr); + &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, UndefBad); if (type_is_invalid(cimport_result->type)) return ira->codegen->invalid_instruction; @@ -21181,7 +21235,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru ConstExprValue *len_val = &val->data.x_struct.fields[slice_len_index]; if (value_is_comptime(len_val)) { - known_len = bigint_as_unsigned(&len_val->data.x_bigint); + known_len = bigint_as_u64(&len_val->data.x_bigint); have_known_len = true; } } @@ -21482,64 +21536,75 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio return ira->codegen->invalid_instruction; // TODO test this at comptime with u8 and non-u8 types - if (casted_dest_ptr->value.special == ConstValSpecialStatic && - casted_byte->value.special == ConstValSpecialStatic && - casted_count->value.special == ConstValSpecialStatic && - casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr && - casted_dest_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) + if (instr_is_comptime(casted_dest_ptr) && + instr_is_comptime(casted_byte) && + instr_is_comptime(casted_count)) { - ConstExprValue *dest_ptr_val = &casted_dest_ptr->value; - - ConstExprValue *dest_elements; - size_t start; - size_t bound_end; - switch (dest_ptr_val->data.x_ptr.special) { - case ConstPtrSpecialInvalid: - case ConstPtrSpecialDiscard: - zig_unreachable(); - case ConstPtrSpecialRef: - dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee; - start = 0; - bound_end = 1; - break; - case ConstPtrSpecialBaseArray: - { - ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val; - expand_undef_array(ira->codegen, array_val); - dest_elements = array_val->data.x_array.data.s_none.elements; - start = dest_ptr_val->data.x_ptr.data.base_array.elem_index; - bound_end = array_val->type->data.array.len; - break; - } - case ConstPtrSpecialBaseStruct: - zig_panic("TODO memset on const inner struct"); - case ConstPtrSpecialBaseErrorUnionCode: - zig_panic("TODO memset on const inner error union code"); - case ConstPtrSpecialBaseErrorUnionPayload: - zig_panic("TODO memset on const inner error union payload"); - case ConstPtrSpecialBaseOptionalPayload: - zig_panic("TODO memset on const inner optional payload"); - case ConstPtrSpecialHardCodedAddr: - zig_unreachable(); - case ConstPtrSpecialFunction: - zig_panic("TODO memset on ptr cast from function"); - case ConstPtrSpecialNull: - zig_panic("TODO memset on null ptr"); - } - - size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint); - size_t end = start + count; - if (end > bound_end) { - ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access")); + ConstExprValue *dest_ptr_val = ir_resolve_const(ira, casted_dest_ptr, UndefBad); + if (dest_ptr_val == nullptr) return ira->codegen->invalid_instruction; - } - ConstExprValue *byte_val = &casted_byte->value; - for (size_t i = start; i < end; i += 1) { - copy_const_val(&dest_elements[i], byte_val, true); - } + ConstExprValue *byte_val = ir_resolve_const(ira, casted_byte, UndefOk); + if (byte_val == nullptr) + return ira->codegen->invalid_instruction; - return ir_const_void(ira, &instruction->base); + ConstExprValue *count_val = ir_resolve_const(ira, casted_count, UndefBad); + if (count_val == nullptr) + return ira->codegen->invalid_instruction; + + if (casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr && + casted_dest_ptr->value.data.x_ptr.mut != ConstPtrMutRuntimeVar) + { + ConstExprValue *dest_elements; + size_t start; + size_t bound_end; + switch (dest_ptr_val->data.x_ptr.special) { + case ConstPtrSpecialInvalid: + case ConstPtrSpecialDiscard: + zig_unreachable(); + case ConstPtrSpecialRef: + dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee; + start = 0; + bound_end = 1; + break; + case ConstPtrSpecialBaseArray: + { + ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val; + expand_undef_array(ira->codegen, array_val); + dest_elements = array_val->data.x_array.data.s_none.elements; + start = dest_ptr_val->data.x_ptr.data.base_array.elem_index; + bound_end = array_val->type->data.array.len; + break; + } + case ConstPtrSpecialBaseStruct: + zig_panic("TODO memset on const inner struct"); + case ConstPtrSpecialBaseErrorUnionCode: + zig_panic("TODO memset on const inner error union code"); + case ConstPtrSpecialBaseErrorUnionPayload: + zig_panic("TODO memset on const inner error union payload"); + case ConstPtrSpecialBaseOptionalPayload: + zig_panic("TODO memset on const inner optional payload"); + case ConstPtrSpecialHardCodedAddr: + zig_unreachable(); + case ConstPtrSpecialFunction: + zig_panic("TODO memset on ptr cast from function"); + case ConstPtrSpecialNull: + zig_panic("TODO memset on null ptr"); + } + + size_t count = bigint_as_usize(&count_val->data.x_bigint); + size_t end = start + count; + if (end > bound_end) { + ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access")); + return ira->codegen->invalid_instruction; + } + + for (size_t i = start; i < end; i += 1) { + copy_const_val(&dest_elements[i], byte_val, true); + } + + return ir_const_void(ira, &instruction->base); + } } IrInstruction *result = ir_build_memset(&ira->new_irb, instruction->base.scope, instruction->base.source_node, @@ -21607,107 +21672,118 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio // TODO test this at comptime with u8 and non-u8 types // TODO test with dest ptr being a global runtime variable - if (casted_dest_ptr->value.special == ConstValSpecialStatic && - casted_src_ptr->value.special == ConstValSpecialStatic && - casted_count->value.special == ConstValSpecialStatic && - casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr) + if (instr_is_comptime(casted_dest_ptr) && + instr_is_comptime(casted_src_ptr) && + instr_is_comptime(casted_count)) { - size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint); - - ConstExprValue *dest_ptr_val = &casted_dest_ptr->value; - ConstExprValue *dest_elements; - size_t dest_start; - size_t dest_end; - switch (dest_ptr_val->data.x_ptr.special) { - case ConstPtrSpecialInvalid: - case ConstPtrSpecialDiscard: - zig_unreachable(); - case ConstPtrSpecialRef: - dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee; - dest_start = 0; - dest_end = 1; - break; - case ConstPtrSpecialBaseArray: - { - ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val; - expand_undef_array(ira->codegen, array_val); - dest_elements = array_val->data.x_array.data.s_none.elements; - dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index; - dest_end = array_val->type->data.array.len; - break; - } - case ConstPtrSpecialBaseStruct: - zig_panic("TODO memcpy on const inner struct"); - case ConstPtrSpecialBaseErrorUnionCode: - zig_panic("TODO memcpy on const inner error union code"); - case ConstPtrSpecialBaseErrorUnionPayload: - zig_panic("TODO memcpy on const inner error union payload"); - case ConstPtrSpecialBaseOptionalPayload: - zig_panic("TODO memcpy on const inner optional payload"); - case ConstPtrSpecialHardCodedAddr: - zig_unreachable(); - case ConstPtrSpecialFunction: - zig_panic("TODO memcpy on ptr cast from function"); - case ConstPtrSpecialNull: - zig_panic("TODO memcpy on null ptr"); - } - - if (dest_start + count > dest_end) { - ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access")); + ConstExprValue *dest_ptr_val = ir_resolve_const(ira, casted_dest_ptr, UndefBad); + if (dest_ptr_val == nullptr) return ira->codegen->invalid_instruction; - } - ConstExprValue *src_ptr_val = &casted_src_ptr->value; - ConstExprValue *src_elements; - size_t src_start; - size_t src_end; - - switch (src_ptr_val->data.x_ptr.special) { - case ConstPtrSpecialInvalid: - case ConstPtrSpecialDiscard: - zig_unreachable(); - case ConstPtrSpecialRef: - src_elements = src_ptr_val->data.x_ptr.data.ref.pointee; - src_start = 0; - src_end = 1; - break; - case ConstPtrSpecialBaseArray: - { - ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val; - expand_undef_array(ira->codegen, array_val); - src_elements = array_val->data.x_array.data.s_none.elements; - src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index; - src_end = array_val->type->data.array.len; - break; - } - case ConstPtrSpecialBaseStruct: - zig_panic("TODO memcpy on const inner struct"); - case ConstPtrSpecialBaseErrorUnionCode: - zig_panic("TODO memcpy on const inner error union code"); - case ConstPtrSpecialBaseErrorUnionPayload: - zig_panic("TODO memcpy on const inner error union payload"); - case ConstPtrSpecialBaseOptionalPayload: - zig_panic("TODO memcpy on const inner optional payload"); - case ConstPtrSpecialHardCodedAddr: - zig_unreachable(); - case ConstPtrSpecialFunction: - zig_panic("TODO memcpy on ptr cast from function"); - case ConstPtrSpecialNull: - zig_panic("TODO memcpy on null ptr"); - } - - if (src_start + count > src_end) { - ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access")); + ConstExprValue *src_ptr_val = ir_resolve_const(ira, casted_src_ptr, UndefBad); + if (src_ptr_val == nullptr) return ira->codegen->invalid_instruction; + + ConstExprValue *count_val = ir_resolve_const(ira, casted_count, UndefBad); + if (count_val == nullptr) + return ira->codegen->invalid_instruction; + + if (dest_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) { + size_t count = bigint_as_usize(&count_val->data.x_bigint); + + ConstExprValue *dest_elements; + size_t dest_start; + size_t dest_end; + switch (dest_ptr_val->data.x_ptr.special) { + case ConstPtrSpecialInvalid: + case ConstPtrSpecialDiscard: + zig_unreachable(); + case ConstPtrSpecialRef: + dest_elements = dest_ptr_val->data.x_ptr.data.ref.pointee; + dest_start = 0; + dest_end = 1; + break; + case ConstPtrSpecialBaseArray: + { + ConstExprValue *array_val = dest_ptr_val->data.x_ptr.data.base_array.array_val; + expand_undef_array(ira->codegen, array_val); + dest_elements = array_val->data.x_array.data.s_none.elements; + dest_start = dest_ptr_val->data.x_ptr.data.base_array.elem_index; + dest_end = array_val->type->data.array.len; + break; + } + case ConstPtrSpecialBaseStruct: + zig_panic("TODO memcpy on const inner struct"); + case ConstPtrSpecialBaseErrorUnionCode: + zig_panic("TODO memcpy on const inner error union code"); + case ConstPtrSpecialBaseErrorUnionPayload: + zig_panic("TODO memcpy on const inner error union payload"); + case ConstPtrSpecialBaseOptionalPayload: + zig_panic("TODO memcpy on const inner optional payload"); + case ConstPtrSpecialHardCodedAddr: + zig_unreachable(); + case ConstPtrSpecialFunction: + zig_panic("TODO memcpy on ptr cast from function"); + case ConstPtrSpecialNull: + zig_panic("TODO memcpy on null ptr"); + } + + if (dest_start + count > dest_end) { + ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access")); + return ira->codegen->invalid_instruction; + } + + ConstExprValue *src_elements; + size_t src_start; + size_t src_end; + + switch (src_ptr_val->data.x_ptr.special) { + case ConstPtrSpecialInvalid: + case ConstPtrSpecialDiscard: + zig_unreachable(); + case ConstPtrSpecialRef: + src_elements = src_ptr_val->data.x_ptr.data.ref.pointee; + src_start = 0; + src_end = 1; + break; + case ConstPtrSpecialBaseArray: + { + ConstExprValue *array_val = src_ptr_val->data.x_ptr.data.base_array.array_val; + expand_undef_array(ira->codegen, array_val); + src_elements = array_val->data.x_array.data.s_none.elements; + src_start = src_ptr_val->data.x_ptr.data.base_array.elem_index; + src_end = array_val->type->data.array.len; + break; + } + case ConstPtrSpecialBaseStruct: + zig_panic("TODO memcpy on const inner struct"); + case ConstPtrSpecialBaseErrorUnionCode: + zig_panic("TODO memcpy on const inner error union code"); + case ConstPtrSpecialBaseErrorUnionPayload: + zig_panic("TODO memcpy on const inner error union payload"); + case ConstPtrSpecialBaseOptionalPayload: + zig_panic("TODO memcpy on const inner optional payload"); + case ConstPtrSpecialHardCodedAddr: + zig_unreachable(); + case ConstPtrSpecialFunction: + zig_panic("TODO memcpy on ptr cast from function"); + case ConstPtrSpecialNull: + zig_panic("TODO memcpy on null ptr"); + } + + if (src_start + count > src_end) { + ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds pointer access")); + return ira->codegen->invalid_instruction; + } + + // TODO check for noalias violations - this should be generalized to work for any function + + for (size_t i = 0; i < count; i += 1) { + copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true); + } + + return ir_const_void(ira, &instruction->base); } - - // TODO check for noalias violations - this should be generalized to work for any function - - for (size_t i = 0; i < count; i += 1) { - copy_const_val(&dest_elements[dest_start + i], &src_elements[src_start + i], true); - } - - return ir_const_void(ira, &instruction->base); } IrInstruction *result = ir_build_memcpy(&ira->new_irb, instruction->base.scope, instruction->base.source_node, @@ -21877,6 +21953,11 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction if (slice_ptr == nullptr) return ira->codegen->invalid_instruction; + if (slice_ptr->special == ConstValSpecialUndef) { + ir_add_error(ira, &instruction->base, buf_sprintf("slice of undefined")); + return ira->codegen->invalid_instruction; + } + parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index]; if (parent_ptr->special == ConstValSpecialUndef) { ir_add_error(ira, &instruction->base, buf_sprintf("slice of undefined")); @@ -21897,7 +21978,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction case ConstPtrSpecialBaseArray: array_val = parent_ptr->data.x_ptr.data.base_array.array_val; abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index; - rel_end = bigint_as_unsigned(&len_val->data.x_bigint); + rel_end = bigint_as_usize(&len_val->data.x_bigint); break; case ConstPtrSpecialBaseStruct: zig_panic("TODO slice const inner struct"); @@ -21910,7 +21991,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction case ConstPtrSpecialHardCodedAddr: array_val = nullptr; abs_offset = 0; - rel_end = bigint_as_unsigned(&len_val->data.x_bigint); + rel_end = bigint_as_usize(&len_val->data.x_bigint); break; case ConstPtrSpecialFunction: zig_panic("TODO slice of slice cast from function"); @@ -21921,7 +22002,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction zig_unreachable(); } - uint64_t start_scalar = bigint_as_unsigned(&casted_start->value.data.x_bigint); + uint64_t start_scalar = bigint_as_u64(&casted_start->value.data.x_bigint); if (!ptr_is_undef && start_scalar > rel_end) { ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice")); return ira->codegen->invalid_instruction; @@ -21929,7 +22010,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction uint64_t end_scalar; if (end) { - end_scalar = bigint_as_unsigned(&end->value.data.x_bigint); + end_scalar = bigint_as_u64(&end->value.data.x_bigint); } else { end_scalar = rel_end; } @@ -22021,7 +22102,7 @@ static IrInstruction *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInst return ira->codegen->invalid_instruction; ZigType *container_type = ir_resolve_type(ira, container); - if ((err = ensure_complete_type(ira->codegen, container_type))) + if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; uint64_t result; @@ -22057,7 +22138,7 @@ static IrInstruction *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInstr if (type_is_invalid(container_type)) return ira->codegen->invalid_instruction; - if ((err = ensure_complete_type(ira->codegen, container_type))) + if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; @@ -22100,7 +22181,7 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr if (type_is_invalid(container_type)) return ira->codegen->invalid_instruction; - if ((err = ensure_complete_type(ira->codegen, container_type))) + if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; uint64_t member_index; @@ -22249,53 +22330,24 @@ static IrInstruction *ir_analyze_instruction_frame_size(IrAnalyze *ira, IrInstru } static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) { - Error err; - IrInstruction *type_value = instruction->type_value->child; - if (type_is_invalid(type_value->value.type)) - return ira->codegen->invalid_instruction; - ZigType *type_entry = ir_resolve_type(ira, type_value); + // Here we create a lazy value in order to avoid resolving the alignment of the type + // immediately. This avoids false positive dependency loops such as: + // const Node = struct { + // field: []align(@alignOf(Node)) Node, + // }; + IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int); + result->value.special = ConstValSpecialLazy; - if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusAlignmentKnown))) + LazyValueAlignOf *lazy_align_of = allocate(1); + lazy_align_of->ira = ira; + result->value.data.x_lazy = &lazy_align_of->base; + lazy_align_of->base.id = LazyValueIdAlignOf; + + lazy_align_of->target_type = instruction->type_value->child; + if (ir_resolve_type_lazy(ira, lazy_align_of->target_type) == nullptr) return ira->codegen->invalid_instruction; - switch (type_entry->id) { - case ZigTypeIdInvalid: - zig_unreachable(); - case ZigTypeIdMetaType: - case ZigTypeIdUnreachable: - case ZigTypeIdComptimeFloat: - case ZigTypeIdComptimeInt: - case ZigTypeIdEnumLiteral: - case ZigTypeIdUndefined: - case ZigTypeIdNull: - case ZigTypeIdBoundFn: - case ZigTypeIdArgTuple: - case ZigTypeIdVoid: - case ZigTypeIdOpaque: - ir_add_error(ira, instruction->type_value, - buf_sprintf("no align available for type '%s'", buf_ptr(&type_entry->name))); - return ira->codegen->invalid_instruction; - case ZigTypeIdBool: - case ZigTypeIdInt: - case ZigTypeIdFloat: - case ZigTypeIdPointer: - case ZigTypeIdArray: - case ZigTypeIdStruct: - case ZigTypeIdOptional: - case ZigTypeIdErrorUnion: - case ZigTypeIdErrorSet: - case ZigTypeIdEnum: - case ZigTypeIdUnion: - case ZigTypeIdFn: - case ZigTypeIdVector: - case ZigTypeIdFnFrame: - case ZigTypeIdAnyFrame: - { - uint64_t align_in_bytes = get_abi_alignment(ira->codegen, type_entry); - return ir_const_unsigned(ira, &instruction->base, align_in_bytes); - } - } - zig_unreachable(); + return result; } static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) { @@ -22359,13 +22411,26 @@ static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstr if (type_is_invalid(casted_result_ptr->value.type)) return ira->codegen->invalid_instruction; - if (casted_op1->value.special == ConstValSpecialStatic && - casted_op2->value.special == ConstValSpecialStatic && - casted_result_ptr->value.special == ConstValSpecialStatic) + if (instr_is_comptime(casted_op1) && + instr_is_comptime(casted_op2) && + instr_is_comptime(casted_result_ptr)) { - BigInt *op1_bigint = &casted_op1->value.data.x_bigint; - BigInt *op2_bigint = &casted_op2->value.data.x_bigint; - ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, &casted_result_ptr->value, casted_result_ptr->source_node); + ConstExprValue *op1_val = ir_resolve_const(ira, casted_op1, UndefBad); + if (op1_val == nullptr) + return ira->codegen->invalid_instruction; + + ConstExprValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad); + if (op2_val == nullptr) + return ira->codegen->invalid_instruction; + + ConstExprValue *result_val = ir_resolve_const(ira, casted_result_ptr, UndefBad); + if (result_val == nullptr) + return ira->codegen->invalid_instruction; + + BigInt *op1_bigint = &op1_val->data.x_bigint; + BigInt *op2_bigint = &op2_val->data.x_bigint; + ConstExprValue *pointee_val = const_ptr_pointee(ira, ira->codegen, result_val, + casted_result_ptr->source_node); if (pointee_val == nullptr) return ira->codegen->invalid_instruction; BigInt *dest_bigint = &pointee_val->data.x_bigint; @@ -22754,84 +22819,64 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct AstNode *proto_node = instruction->base.source_node; assert(proto_node->type == NodeTypeFnProto); + IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type); + result->value.special = ConstValSpecialLazy; + + LazyValueFnType *lazy_fn_type = allocate(1); + lazy_fn_type->ira = ira; + result->value.data.x_lazy = &lazy_fn_type->base; + lazy_fn_type->base.id = LazyValueIdFnType; + if (proto_node->data.fn_proto.auto_err_set) { ir_add_error(ira, &instruction->base, buf_sprintf("inferring error set of return type valid only for function definitions")); return ira->codegen->invalid_instruction; } - FnTypeId fn_type_id = {0}; - init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length); + size_t param_count = proto_node->data.fn_proto.params.length; + lazy_fn_type->proto_node = proto_node; + lazy_fn_type->param_types = allocate(param_count); - for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) { - AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index); + for (size_t param_index = 0; param_index < param_count; param_index += 1) { + AstNode *param_node = proto_node->data.fn_proto.params.at(param_index); assert(param_node->type == NodeTypeParamDecl); bool param_is_var_args = param_node->data.param_decl.is_var_args; if (param_is_var_args) { - if (fn_type_id.cc == CallingConventionC) { - fn_type_id.param_count = fn_type_id.next_param_index; - continue; - } else if (fn_type_id.cc == CallingConventionUnspecified) { - return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id)); + if (proto_node->data.fn_proto.cc == CallingConventionC) { + break; + } else if (proto_node->data.fn_proto.cc == CallingConventionUnspecified) { + lazy_fn_type->is_generic = true; + return result; } else { zig_unreachable(); } } - FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index]; - param_info->is_noalias = param_node->data.param_decl.is_noalias; - if (instruction->param_types[fn_type_id.next_param_index] == nullptr) { - param_info->type = nullptr; - return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id)); - } else { - IrInstruction *param_type_value = instruction->param_types[fn_type_id.next_param_index]->child; - if (type_is_invalid(param_type_value->value.type)) - return ira->codegen->invalid_instruction; - ZigType *param_type = ir_resolve_type(ira, param_type_value); - switch (type_requires_comptime(ira->codegen, param_type)) { - case ReqCompTimeYes: - if (!calling_convention_allows_zig_types(fn_type_id.cc)) { - ir_add_error(ira, param_type_value, - buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'", - buf_ptr(¶m_type->name), calling_convention_name(fn_type_id.cc))); - return ira->codegen->invalid_instruction; - } - param_info->type = param_type; - fn_type_id.next_param_index += 1; - return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id)); - case ReqCompTimeInvalid: - return ira->codegen->invalid_instruction; - case ReqCompTimeNo: - break; - } - if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) { - ir_add_error(ira, param_type_value, - buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'", - buf_ptr(¶m_type->name), calling_convention_name(fn_type_id.cc))); - return ira->codegen->invalid_instruction; - } - param_info->type = param_type; + if (instruction->param_types[param_index] == nullptr) { + lazy_fn_type->is_generic = true; + return result; } + IrInstruction *param_type_value = instruction->param_types[param_index]->child; + if (type_is_invalid(param_type_value->value.type)) + return ira->codegen->invalid_instruction; + if (ir_resolve_const(ira, param_type_value, LazyOk) == nullptr) + return ira->codegen->invalid_instruction; + lazy_fn_type->param_types[param_index] = param_type_value; } if (instruction->align_value != nullptr) { - if (!ir_resolve_align(ira, instruction->align_value->child, &fn_type_id.alignment)) + lazy_fn_type->align_inst = instruction->align_value->child; + if (ir_resolve_const(ira, lazy_fn_type->align_inst, LazyOk) == nullptr) return ira->codegen->invalid_instruction; } - IrInstruction *return_type_value = instruction->return_type->child; - fn_type_id.return_type = ir_resolve_type(ira, return_type_value); - if (type_is_invalid(fn_type_id.return_type)) - return ira->codegen->invalid_instruction; - if (fn_type_id.return_type->id == ZigTypeIdOpaque) { - ir_add_error(ira, instruction->return_type, - buf_sprintf("return type cannot be opaque")); - return ira->codegen->invalid_instruction; - } + lazy_fn_type->return_type = instruction->return_type->child; + if (ir_resolve_const(ira, lazy_fn_type->return_type, LazyOk) == nullptr) + return ira->codegen->invalid_instruction; - return ir_const_type(ira, &instruction->base, get_fn_type(ira->codegen, &fn_type_id)); + return result; } static IrInstruction *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) { @@ -23218,7 +23263,7 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_ if (!val) return ira->codegen->invalid_instruction; - if (val->special == ConstValSpecialStatic) { + if (value_is_comptime(val) && val->special != ConstValSpecialUndef) { bool is_addr_zero = val->data.x_ptr.special == ConstPtrSpecialNull || (val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr && val->data.x_ptr.data.hard_coded_addr.addr == 0); @@ -23258,6 +23303,12 @@ static IrInstruction *ir_analyze_ptr_cast(IrAnalyze *ira, IrInstruction *source_ IrInstruction *casted_ptr = ir_build_ptr_cast_gen(ira, source_instr, dest_type, ptr, safety_check_on); + if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown))) + return ira->codegen->invalid_instruction; + + if ((err = type_resolve(ira->codegen, src_type, ResolveStatusZeroBitsKnown))) + return ira->codegen->invalid_instruction; + if (type_has_bits(dest_type) && !type_has_bits(src_type)) { ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("'%s' and '%s' do not have the same in-memory representation", @@ -23309,8 +23360,10 @@ static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExp } static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val) { - if (val->special == ConstValSpecialUndef) + if (val->special == ConstValSpecialUndef) { + expand_undef_struct(codegen, val); val->special = ConstValSpecialStatic; + } assert(val->special == ConstValSpecialStatic); switch (val->type->id) { case ZigTypeIdInvalid: @@ -23500,7 +23553,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou BigInt bn; bigint_read_twos_complement(&bn, buf, codegen->builtin_types.entry_usize->data.integral.bit_count, codegen->is_big_endian, false); - val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn); + val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_usize(&bn); return ErrorNone; } case ZigTypeIdArray: @@ -23693,7 +23746,7 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc if (!val) return ira->codegen->invalid_instruction; - uint64_t addr = bigint_as_unsigned(&val->data.x_bigint); + uint64_t addr = bigint_as_u64(&val->data.x_bigint); if (!ptr_allows_addr_zero(ptr_type) && addr == 0) { ir_add_error(ira, source_instr, buf_sprintf("pointer type '%s' does not allow address zero", buf_ptr(&ptr_type->name))); @@ -23746,8 +23799,9 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira, IrInstructionDeclRef *instruction) { IrInstruction *ref_instruction = ir_analyze_decl_ref(ira, &instruction->base, instruction->tld); - if (type_is_invalid(ref_instruction->value.type)) + if (type_is_invalid(ref_instruction->value.type)) { return ira->codegen->invalid_instruction; + } if (instruction->lval == LValPtr) { return ref_instruction; @@ -23795,53 +23849,32 @@ static IrInstruction *ir_analyze_instruction_ptr_to_int(IrAnalyze *ira, IrInstru } static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtrType *instruction) { - Error err; - ZigType *child_type = ir_resolve_type(ira, instruction->child_type->child); - if (type_is_invalid(child_type)) + IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type); + result->value.special = ConstValSpecialLazy; + + LazyValuePtrType *lazy_ptr_type = allocate(1); + lazy_ptr_type->ira = ira; + result->value.data.x_lazy = &lazy_ptr_type->base; + lazy_ptr_type->base.id = LazyValueIdPtrType; + + lazy_ptr_type->elem_type = instruction->child_type->child; + if (ir_resolve_type_lazy(ira, lazy_ptr_type->elem_type) == nullptr) return ira->codegen->invalid_instruction; - if (child_type->id == ZigTypeIdUnreachable) { - ir_add_error(ira, &instruction->base, buf_sprintf("pointer to noreturn not allowed")); - return ira->codegen->invalid_instruction; - } else if (child_type->id == ZigTypeIdOpaque && instruction->ptr_len == PtrLenUnknown) { - ir_add_error(ira, &instruction->base, buf_sprintf("unknown-length pointer to opaque")); - return ira->codegen->invalid_instruction; - } else if (instruction->ptr_len == PtrLenC) { - if (!type_allowed_in_extern(ira->codegen, child_type)) { - ir_add_error(ira, &instruction->base, - buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'", buf_ptr(&child_type->name))); - return ira->codegen->invalid_instruction; - } else if (child_type->id == ZigTypeIdOpaque) { - ir_add_error(ira, &instruction->base, buf_sprintf("C pointers cannot point opaque types")); - return ira->codegen->invalid_instruction; - } else if (instruction->is_allow_zero) { - ir_add_error(ira, &instruction->base, buf_sprintf("C pointers always allow address zero")); - return ira->codegen->invalid_instruction; - } - } - - uint32_t align_bytes; if (instruction->align_value != nullptr) { - if (!ir_resolve_align(ira, instruction->align_value->child, &align_bytes)) + lazy_ptr_type->align_inst = instruction->align_value->child; + if (ir_resolve_const(ira, lazy_ptr_type->align_inst, LazyOk) == nullptr) return ira->codegen->invalid_instruction; - if ((err = type_resolve(ira->codegen, child_type, ResolveStatusAlignmentKnown))) - return ira->codegen->invalid_instruction; - if (!type_has_bits(child_type)) { - align_bytes = 0; - } - } else { - if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown))) - return ira->codegen->invalid_instruction; - align_bytes = 0; } - bool allow_zero = instruction->is_allow_zero || instruction->ptr_len == PtrLenC; + lazy_ptr_type->ptr_len = instruction->ptr_len; + lazy_ptr_type->is_const = instruction->is_const; + lazy_ptr_type->is_volatile = instruction->is_volatile; + lazy_ptr_type->is_allowzero = instruction->is_allow_zero; + lazy_ptr_type->bit_offset_in_host = instruction->bit_offset_start; + lazy_ptr_type->host_int_bytes = instruction->host_int_bytes; - ZigType *result_type = get_pointer_to_type_extra(ira->codegen, child_type, - instruction->is_const, instruction->is_volatile, - instruction->ptr_len, align_bytes, - instruction->bit_offset_start, instruction->host_int_bytes, allow_zero); - return ir_const_type(ira, &instruction->base, result_type); + return result; } static IrInstruction *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstructionAlignCast *instruction) { @@ -23954,7 +23987,7 @@ static IrInstruction *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruct return ira->codegen->invalid_instruction; if (enum_type->id == ZigTypeIdEnum) { - if ((err = ensure_complete_type(ira->codegen, enum_type))) + if ((err = type_resolve(ira->codegen, enum_type, ResolveStatusSizeKnown))) return ira->codegen->invalid_instruction; return ir_const_type(ira, &instruction->base, enum_type->data.enumeration.tag_int_type); @@ -25100,7 +25133,7 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_exec, ZigType *expected_type, AstNode *expected_type_source_node) { - assert(!old_exec->invalid); + assert(old_exec->first_err_trace_msg == nullptr); assert(expected_type == nullptr || !type_is_invalid(expected_type)); IrAnalyze *ira = allocate(1); @@ -25147,6 +25180,18 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_ old_instruction->child = new_instruction; if (type_is_invalid(new_instruction->value.type)) { + if (new_exec->first_err_trace_msg != nullptr) { + ira->codegen->trace_err = new_exec->first_err_trace_msg; + } else { + new_exec->first_err_trace_msg = ira->codegen->trace_err; + } + if (new_exec->first_err_trace_msg != nullptr && + !old_instruction->source_node->already_traced_this_node) + { + old_instruction->source_node->already_traced_this_node = true; + new_exec->first_err_trace_msg = add_error_note(ira->codegen, new_exec->first_err_trace_msg, + old_instruction->source_node, buf_create_from_str("referenced here")); + } return ira->codegen->builtin_types.entry_invalid; } @@ -25158,7 +25203,15 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_ ira->instruction_index += 1; } - if (new_exec->invalid) { + if (new_exec->first_err_trace_msg != nullptr) { + codegen->trace_err = new_exec->first_err_trace_msg; + if (codegen->trace_err != nullptr && new_exec->source_node != nullptr && + !new_exec->source_node->already_traced_this_node) + { + new_exec->source_node->already_traced_this_node = true; + codegen->trace_err = add_error_note(codegen, codegen->trace_err, + new_exec->source_node, buf_create_from_str("referenced here")); + } return ira->codegen->builtin_types.entry_invalid; } else if (ira->src_implicit_return_type_list.length == 0) { return codegen->builtin_types.entry_unreachable; @@ -25354,3 +25407,336 @@ bool ir_has_side_effects(IrInstruction *instruction) { } zig_unreachable(); } + +static ZigType *ir_resolve_lazy_fn_type(IrAnalyze *ira, AstNode *source_node, LazyValueFnType *lazy_fn_type) { + Error err; + AstNode *proto_node = lazy_fn_type->proto_node; + + FnTypeId fn_type_id = {0}; + init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length); + + for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) { + AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index); + assert(param_node->type == NodeTypeParamDecl); + + bool param_is_var_args = param_node->data.param_decl.is_var_args; + if (param_is_var_args) { + if (fn_type_id.cc == CallingConventionC) { + fn_type_id.param_count = fn_type_id.next_param_index; + continue; + } else if (fn_type_id.cc == CallingConventionUnspecified) { + return get_generic_fn_type(ira->codegen, &fn_type_id); + } else { + zig_unreachable(); + } + } + FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index]; + param_info->is_noalias = param_node->data.param_decl.is_noalias; + + if (lazy_fn_type->param_types[fn_type_id.next_param_index] == nullptr) { + param_info->type = nullptr; + return get_generic_fn_type(ira->codegen, &fn_type_id); + } else { + IrInstruction *param_type_inst = lazy_fn_type->param_types[fn_type_id.next_param_index]; + ZigType *param_type = ir_resolve_type(ira, param_type_inst); + if (type_is_invalid(param_type)) + return nullptr; + switch (type_requires_comptime(ira->codegen, param_type)) { + case ReqCompTimeYes: + if (!calling_convention_allows_zig_types(fn_type_id.cc)) { + ir_add_error(ira, param_type_inst, + buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'", + buf_ptr(¶m_type->name), calling_convention_name(fn_type_id.cc))); + return nullptr; + } + param_info->type = param_type; + fn_type_id.next_param_index += 1; + return get_generic_fn_type(ira->codegen, &fn_type_id); + case ReqCompTimeInvalid: + return nullptr; + case ReqCompTimeNo: + break; + } + if (!calling_convention_allows_zig_types(fn_type_id.cc)) { + if ((err = type_resolve(ira->codegen, param_type, ResolveStatusZeroBitsKnown))) + return nullptr; + if (!type_has_bits(param_type)) { + ir_add_error(ira, param_type_inst, + buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'", + buf_ptr(¶m_type->name), calling_convention_name(fn_type_id.cc))); + return nullptr; + } + } + param_info->type = param_type; + } + } + + if (lazy_fn_type->align_inst != nullptr) { + if (!ir_resolve_align(ira, lazy_fn_type->align_inst, &fn_type_id.alignment)) + return nullptr; + } + + fn_type_id.return_type = ir_resolve_type(ira, lazy_fn_type->return_type); + if (type_is_invalid(fn_type_id.return_type)) + return nullptr; + if (fn_type_id.return_type->id == ZigTypeIdOpaque) { + ir_add_error(ira, lazy_fn_type->return_type, buf_create_from_str("return type cannot be opaque")); + return nullptr; + } + + return get_fn_type(ira->codegen, &fn_type_id); +} + +static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) { + Error err; + if (val->special != ConstValSpecialLazy) + return ErrorNone; + switch (val->data.x_lazy->id) { + case LazyValueIdInvalid: + zig_unreachable(); + case LazyValueIdAlignOf: { + LazyValueAlignOf *lazy_align_of = reinterpret_cast(val->data.x_lazy); + IrAnalyze *ira = lazy_align_of->ira; + + if (lazy_align_of->target_type->value.special == ConstValSpecialStatic) { + switch (lazy_align_of->target_type->value.data.x_type->id) { + case ZigTypeIdInvalid: + zig_unreachable(); + case ZigTypeIdMetaType: + case ZigTypeIdUnreachable: + case ZigTypeIdComptimeFloat: + case ZigTypeIdComptimeInt: + case ZigTypeIdEnumLiteral: + case ZigTypeIdUndefined: + case ZigTypeIdNull: + case ZigTypeIdBoundFn: + case ZigTypeIdArgTuple: + case ZigTypeIdVoid: + case ZigTypeIdOpaque: + ir_add_error(ira, lazy_align_of->target_type, + buf_sprintf("no align available for type '%s'", + buf_ptr(&lazy_align_of->target_type->value.data.x_type->name))); + return ErrorSemanticAnalyzeFail; + case ZigTypeIdBool: + case ZigTypeIdInt: + case ZigTypeIdFloat: + case ZigTypeIdPointer: + case ZigTypeIdArray: + case ZigTypeIdStruct: + case ZigTypeIdOptional: + case ZigTypeIdErrorUnion: + case ZigTypeIdErrorSet: + case ZigTypeIdEnum: + case ZigTypeIdUnion: + case ZigTypeIdFn: + case ZigTypeIdVector: + case ZigTypeIdFnFrame: + case ZigTypeIdAnyFrame: + break; + } + } + + uint32_t align_in_bytes; + if ((err = type_val_resolve_abi_align(ira->codegen, &lazy_align_of->target_type->value, + &align_in_bytes))) + { + return err; + } + + val->special = ConstValSpecialStatic; + assert(val->type->id == ZigTypeIdComptimeInt); + bigint_init_unsigned(&val->data.x_bigint, align_in_bytes); + return ErrorNone; + } + case LazyValueIdSliceType: { + LazyValueSliceType *lazy_slice_type = reinterpret_cast(val->data.x_lazy); + IrAnalyze *ira = lazy_slice_type->ira; + + uint32_t align_bytes = 0; + if (lazy_slice_type->align_inst != nullptr) { + if (!ir_resolve_align(ira, lazy_slice_type->align_inst, &align_bytes)) + return ErrorSemanticAnalyzeFail; + } + ZigType *elem_type = ir_resolve_type(ira, lazy_slice_type->elem_type); + if (type_is_invalid(elem_type)) + return ErrorSemanticAnalyzeFail; + + switch (elem_type->id) { + case ZigTypeIdInvalid: // handled above + zig_unreachable(); + case ZigTypeIdUnreachable: + case ZigTypeIdUndefined: + case ZigTypeIdNull: + case ZigTypeIdArgTuple: + case ZigTypeIdOpaque: + ir_add_error(ira, lazy_slice_type->elem_type, + buf_sprintf("slice of type '%s' not allowed", buf_ptr(&elem_type->name))); + return ErrorSemanticAnalyzeFail; + case ZigTypeIdMetaType: + case ZigTypeIdVoid: + case ZigTypeIdBool: + case ZigTypeIdInt: + case ZigTypeIdFloat: + case ZigTypeIdPointer: + case ZigTypeIdArray: + case ZigTypeIdStruct: + case ZigTypeIdComptimeFloat: + case ZigTypeIdComptimeInt: + case ZigTypeIdEnumLiteral: + case ZigTypeIdOptional: + case ZigTypeIdErrorUnion: + case ZigTypeIdErrorSet: + case ZigTypeIdEnum: + case ZigTypeIdUnion: + case ZigTypeIdFn: + case ZigTypeIdBoundFn: + case ZigTypeIdVector: + case ZigTypeIdFnFrame: + case ZigTypeIdAnyFrame: + break; + } + + ResolveStatus needed_status = (align_bytes == 0) ? + ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown; + if ((err = type_resolve(ira->codegen, elem_type, needed_status))) + return err; + ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, elem_type, + lazy_slice_type->is_const, lazy_slice_type->is_volatile, PtrLenUnknown, align_bytes, + 0, 0, lazy_slice_type->is_allowzero); + val->special = ConstValSpecialStatic; + assert(val->type->id == ZigTypeIdMetaType); + val->data.x_type = get_slice_type(ira->codegen, slice_ptr_type); + return ErrorNone; + } + case LazyValueIdPtrType: { + LazyValuePtrType *lazy_ptr_type = reinterpret_cast(val->data.x_lazy); + IrAnalyze *ira = lazy_ptr_type->ira; + + uint32_t align_bytes = 0; + if (lazy_ptr_type->align_inst != nullptr) { + if (!ir_resolve_align(ira, lazy_ptr_type->align_inst, &align_bytes)) + return ErrorSemanticAnalyzeFail; + } + ZigType *elem_type = ir_resolve_type(ira, lazy_ptr_type->elem_type); + if (type_is_invalid(elem_type)) + return ErrorSemanticAnalyzeFail; + + if (elem_type->id == ZigTypeIdUnreachable) { + ir_add_error(ira, lazy_ptr_type->elem_type, + buf_create_from_str("pointer to noreturn not allowed")); + return ErrorSemanticAnalyzeFail; + } else if (elem_type->id == ZigTypeIdOpaque && lazy_ptr_type->ptr_len == PtrLenUnknown) { + ir_add_error(ira, lazy_ptr_type->elem_type, + buf_create_from_str("unknown-length pointer to opaque")); + return ErrorSemanticAnalyzeFail; + } else if (lazy_ptr_type->ptr_len == PtrLenC) { + if (!type_allowed_in_extern(ira->codegen, elem_type)) { + ir_add_error(ira, lazy_ptr_type->elem_type, + buf_sprintf("C pointers cannot point to non-C-ABI-compatible type '%s'", + buf_ptr(&elem_type->name))); + return ErrorSemanticAnalyzeFail; + } else if (elem_type->id == ZigTypeIdOpaque) { + ir_add_error(ira, lazy_ptr_type->elem_type, + buf_sprintf("C pointers cannot point opaque types")); + return ErrorSemanticAnalyzeFail; + } else if (lazy_ptr_type->is_allowzero) { + ir_add_error(ira, lazy_ptr_type->elem_type, + buf_sprintf("C pointers always allow address zero")); + return ErrorSemanticAnalyzeFail; + } + } + + if (align_bytes != 0) { + if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusAlignmentKnown))) + return err; + if (!type_has_bits(elem_type)) + align_bytes = 0; + } + bool allow_zero = lazy_ptr_type->is_allowzero || lazy_ptr_type->ptr_len == PtrLenC; + assert(val->type->id == ZigTypeIdMetaType); + val->data.x_type = get_pointer_to_type_extra(ira->codegen, elem_type, + lazy_ptr_type->is_const, lazy_ptr_type->is_volatile, lazy_ptr_type->ptr_len, align_bytes, + lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes, + allow_zero); + val->special = ConstValSpecialStatic; + return ErrorNone; + } + case LazyValueIdOptType: { + LazyValueOptType *lazy_opt_type = reinterpret_cast(val->data.x_lazy); + IrAnalyze *ira = lazy_opt_type->ira; + + ZigType *payload_type = ir_resolve_type(ira, lazy_opt_type->payload_type); + if (type_is_invalid(payload_type)) + return ErrorSemanticAnalyzeFail; + + if (payload_type->id == ZigTypeIdOpaque || payload_type->id == ZigTypeIdUnreachable) { + ir_add_error(ira, lazy_opt_type->payload_type, + buf_sprintf("type '%s' cannot be optional", buf_ptr(&payload_type->name))); + return ErrorSemanticAnalyzeFail; + } + + if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown))) + return err; + + assert(val->type->id == ZigTypeIdMetaType); + val->data.x_type = get_optional_type(ira->codegen, payload_type); + val->special = ConstValSpecialStatic; + return ErrorNone; + } + case LazyValueIdFnType: { + LazyValueFnType *lazy_fn_type = reinterpret_cast(val->data.x_lazy); + ZigType *fn_type = ir_resolve_lazy_fn_type(lazy_fn_type->ira, source_node, lazy_fn_type); + if (fn_type == nullptr) + return ErrorSemanticAnalyzeFail; + val->special = ConstValSpecialStatic; + assert(val->type->id == ZigTypeIdMetaType); + val->data.x_type = fn_type; + return ErrorNone; + } + case LazyValueIdErrUnionType: { + LazyValueErrUnionType *lazy_err_union_type = + reinterpret_cast(val->data.x_lazy); + IrAnalyze *ira = lazy_err_union_type->ira; + + ZigType *err_set_type = ir_resolve_type(ira, lazy_err_union_type->err_set_type); + if (type_is_invalid(err_set_type)) + return ErrorSemanticAnalyzeFail; + + ZigType *payload_type = ir_resolve_type(ira, lazy_err_union_type->payload_type); + if (type_is_invalid(payload_type)) + return ErrorSemanticAnalyzeFail; + + if (err_set_type->id != ZigTypeIdErrorSet) { + ir_add_error(ira, lazy_err_union_type->err_set_type, + buf_sprintf("expected error set type, found type '%s'", + buf_ptr(&err_set_type->name))); + return ErrorSemanticAnalyzeFail; + } + + if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown))) + return ErrorSemanticAnalyzeFail; + + assert(val->type->id == ZigTypeIdMetaType); + val->data.x_type = get_error_union_type(ira->codegen, err_set_type, payload_type); + val->special = ConstValSpecialStatic; + return ErrorNone; + } + } + zig_unreachable(); +} + +Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) { + Error err; + if ((err = ir_resolve_lazy_raw(source_node, val))) { + if (codegen->trace_err != nullptr && !source_node->already_traced_this_node) { + source_node->already_traced_this_node = true; + codegen->trace_err = add_error_note(codegen, codegen->trace_err, source_node, + buf_create_from_str("referenced here")); + } + return err; + } + if (type_is_invalid(val->type)) { + return ErrorSemanticAnalyzeFail; + } + return ErrorNone; +} diff --git a/src/ir.hpp b/src/ir.hpp index 3761c5a97d..3923ea28e8 100644 --- a/src/ir.hpp +++ b/src/ir.hpp @@ -16,7 +16,9 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry); ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node, ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota, ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name, - IrExecutable *parent_exec, AstNode *expected_type_source_node); + IrExecutable *parent_exec, AstNode *expected_type_source_node, UndefAllowed undef); + +Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val); ZigType *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable, ZigType *expected_type, AstNode *expected_type_source_node); @@ -28,6 +30,4 @@ ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprVal AstNode *source_node); const char *float_op_to_name(BuiltinFnId op, bool llvm_name); -void ir_add_analysis_trace(IrAnalyze *ira, ErrorMsg *err_msg, Buf *text); - #endif diff --git a/src/os.cpp b/src/os.cpp index 5fa70bd260..6c1a2581df 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1125,29 +1125,27 @@ Error os_get_cwd(Buf *out_cwd) { #endif } -#if defined(ZIG_OS_WINDOWS) #define is_wprefix(s, prefix) \ (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0) -static bool is_stderr_cyg_pty(void) { - HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); - if (stderr_handle == INVALID_HANDLE_VALUE) - return false; - - int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH; - FILE_NAME_INFO *nameinfo; - WCHAR *p = NULL; +bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { +#if defined(ZIG_OS_WINDOWS) + HANDLE handle = (HANDLE)_get_osfhandle(fd); // Cygwin/msys's pty is a pipe. - if (GetFileType(stderr_handle) != FILE_TYPE_PIPE) { - return 0; + if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) != FILE_TYPE_PIPE) { + return false; } - nameinfo = (FILE_NAME_INFO *)allocate(size); + + int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH; + WCHAR *p = NULL; + + FILE_NAME_INFO *nameinfo = (FILE_NAME_INFO *)allocate(size); if (nameinfo == NULL) { - return 0; + return false; } // Check the name of the pipe: // '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master' - if (GetFileInformationByHandleEx(stderr_handle, FileNameInfo, nameinfo, size)) { + if (GetFileInformationByHandleEx(handle, FileNameInfo, nameinfo, size)) { nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0'; p = nameinfo->FileName; if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */ @@ -1180,12 +1178,14 @@ static bool is_stderr_cyg_pty(void) { } free(nameinfo); return (p != NULL); -} +#else + return false; #endif +} bool os_stderr_tty(void) { #if defined(ZIG_OS_WINDOWS) - return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty(); + return _isatty(fileno(stderr)) != 0 || os_is_cygwin_pty(fileno(stderr)); #elif defined(ZIG_OS_POSIX) return isatty(STDERR_FILENO) != 0; #else @@ -1486,7 +1486,7 @@ WORD original_console_attributes = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BL void os_stderr_set_color(TermColor color) { #if defined(ZIG_OS_WINDOWS) - if (is_stderr_cyg_pty()) { + if (os_stderr_tty()) { set_color_posix(color); return; } diff --git a/src/os.hpp b/src/os.hpp index c8135e9844..7354528c34 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -11,6 +11,7 @@ #include "list.hpp" #include "buffer.hpp" #include "error.hpp" +#include "target.hpp" #include "zig_llvm.h" #include "windows_sdk.h" @@ -88,6 +89,11 @@ struct Termination { #define OsFile int #endif +#if defined(ZIG_OS_WINDOWS) +#undef fileno +#define fileno _fileno +#endif + struct OsTimeStamp { uint64_t sec; uint64_t nsec; @@ -152,6 +158,8 @@ Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type); Error ATTRIBUTE_MUST_USE os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type); +bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd); + Error ATTRIBUTE_MUST_USE os_self_exe_shared_libs(ZigList &paths); #endif diff --git a/src/parser.cpp b/src/parser.cpp index 1e7e36d0bd..6cd6c2f045 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -782,24 +782,26 @@ static AstNode *ast_parse_var_decl(ParseContext *pc) { return res; } -// ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)? +// ContainerField <- IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)? static AstNode *ast_parse_container_field(ParseContext *pc) { Token *identifier = eat_token_if(pc, TokenIdSymbol); if (identifier == nullptr) return nullptr; AstNode *type_expr = nullptr; - if (eat_token_if(pc, TokenIdColon) != nullptr) + if (eat_token_if(pc, TokenIdColon) != nullptr) { type_expr = ast_expect(pc, ast_parse_type_expr); + } + AstNode *align_expr = ast_parse_byte_align(pc); AstNode *expr = nullptr; if (eat_token_if(pc, TokenIdEq) != nullptr) expr = ast_expect(pc, ast_parse_expr); - AstNode *res = ast_create_node(pc, NodeTypeStructField, identifier); res->data.struct_field.name = token_buf(identifier); res->data.struct_field.type = type_expr; res->data.struct_field.value = expr; + res->data.struct_field.align_expr = align_expr; return res; } diff --git a/src/target.cpp b/src/target.cpp index 8b533f4af6..ca66107bb7 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -504,6 +504,16 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) { return ErrorNone; } +static ZigLLVM_EnvironmentType target_get_win32_abi() { + FILE* files[] = { stdin, stdout, stderr, nullptr }; + for (int i = 0; files[i] != nullptr; i++) { + if (os_is_cygwin_pty(fileno(files[i]))) { + return ZigLLVM_GNU; + } + } + return ZigLLVM_MSVC; +} + void get_native_target(ZigTarget *target) { // first zero initialize *target = {}; @@ -518,6 +528,9 @@ void get_native_target(ZigTarget *target) { &target->abi, &oformat); target->os = get_zig_os_type(os_type); + if (target->os == OsWindows) { + target->abi = target_get_win32_abi(); + } target->is_native = true; if (target->abi == ZigLLVM_UnknownEnvironment) { target->abi = target_default_abi(target->arch, target->os); @@ -1651,7 +1664,7 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { return ZigLLVM_GNU; case OsUefi: case OsWindows: - return ZigLLVM_MSVC; + return ZigLLVM_MSVC; case OsLinux: case OsWASI: case OsEmscripten: diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 84f3f2c0ec..9c9effcb10 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -841,7 +841,7 @@ void tokenize(Buf *buf, Tokenization *out) { case TokenizeStateSawAmpersand: switch (c) { case '&': - tokenize_error(&t, "`&&` is invalid. Note that `and` is boolean AND."); + tokenize_error(&t, "`&&` is invalid. Note that `and` is boolean AND"); break; case '=': set_token_id(&t, t.cur_tok, TokenIdBitAndEq); diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 96be3344d9..4fe601aa8a 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -15,9 +15,9 @@ #include "zig_llvm.h" -#if __GNUC__ >= 8 +#if __GNUC__ >= 9 #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wclass-memaccess" +#pragma GCC diagnostic ignored "-Winit-list-lifetime" #endif #include @@ -50,7 +50,7 @@ #include -#if __GNUC__ >= 8 +#if __GNUC__ >= 9 #pragma GCC diagnostic pop #endif diff --git a/std/array_list.zig b/std/array_list.zig index ee282145bd..a527d818d6 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -6,20 +6,23 @@ const mem = std.mem; const Allocator = mem.Allocator; pub fn ArrayList(comptime T: type) type { - return AlignedArrayList(T, @alignOf(T)); + return AlignedArrayList(T, null); } -pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { +pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { return struct { const Self = @This(); /// Use toSlice instead of slicing this directly, because if you don't /// specify the end position of the slice, this will potentially give /// you uninitialized memory. - items: []align(A) T, + items: Slice, len: usize, allocator: *Allocator, + pub const Slice = if (alignment) |a| ([]align(a) T) else []T; + pub const SliceConst = if (alignment) |a| ([]align(a) const T) else []const T; + /// Deinitialize with `deinit` or use `toOwnedSlice`. pub fn init(allocator: *Allocator) Self { return Self{ @@ -33,11 +36,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { self.allocator.free(self.items); } - pub fn toSlice(self: Self) []align(A) T { + pub fn toSlice(self: Self) Slice { return self.items[0..self.len]; } - pub fn toSliceConst(self: Self) []align(A) const T { + pub fn toSliceConst(self: Self) SliceConst { return self.items[0..self.len]; } @@ -69,7 +72,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { /// ArrayList takes ownership of the passed in slice. The slice must have been /// allocated with `allocator`. /// Deinitialize with `deinit` or use `toOwnedSlice`. - pub fn fromOwnedSlice(allocator: *Allocator, slice: []align(A) T) Self { + pub fn fromOwnedSlice(allocator: *Allocator, slice: Slice) Self { return Self{ .items = slice, .len = slice.len, @@ -78,7 +81,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { } /// The caller owns the returned memory. ArrayList becomes empty. - pub fn toOwnedSlice(self: *Self) []align(A) T { + pub fn toOwnedSlice(self: *Self) Slice { const allocator = self.allocator; const result = allocator.shrink(self.items, self.len); self.* = init(allocator); @@ -93,7 +96,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { self.items[n] = item; } - pub fn insertSlice(self: *Self, n: usize, items: []align(A) const T) !void { + pub fn insertSlice(self: *Self, n: usize, items: SliceConst) !void { try self.ensureCapacity(self.len + items.len); self.len += items.len; @@ -141,7 +144,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { return self.swapRemove(i); } - pub fn appendSlice(self: *Self, items: []align(A) const T) !void { + pub fn appendSlice(self: *Self, items: SliceConst) !void { try self.ensureCapacity(self.len + items.len); mem.copy(T, self.items[self.len..], items); self.len += items.len; diff --git a/std/crypto/throughput_test.zig b/std/crypto/benchmark.zig similarity index 91% rename from std/crypto/throughput_test.zig rename to std/crypto/benchmark.zig index aee06571a0..ae5087a847 100644 --- a/std/crypto/throughput_test.zig +++ b/std/crypto/benchmark.zig @@ -1,8 +1,10 @@ +// zig run benchmark.zig --release-fast --override-std-dir .. + const builtin = @import("builtin"); -const std = @import("std"); +const std = @import("../std.zig"); const time = std.time; const Timer = time.Timer; -const crypto = @import("../crypto.zig"); +const crypto = std.crypto; const KiB = 1024; const MiB = 1024 * KiB; @@ -14,7 +16,7 @@ const Crypto = struct { name: []const u8, }; -const hashes = []Crypto{ +const hashes = [_]Crypto{ Crypto{ .ty = crypto.Md5, .name = "md5" }, Crypto{ .ty = crypto.Sha1, .name = "sha1" }, Crypto{ .ty = crypto.Sha256, .name = "sha256" }, @@ -45,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: var, comptime bytes: comptime_int) !u64 { return throughput; } -const macs = []Crypto{ +const macs = [_]Crypto{ Crypto{ .ty = crypto.Poly1305, .name = "poly1305" }, Crypto{ .ty = crypto.HmacMd5, .name = "hmac-md5" }, Crypto{ .ty = crypto.HmacSha1, .name = "hmac-sha1" }, @@ -75,7 +77,7 @@ pub fn benchmarkMac(comptime Mac: var, comptime bytes: comptime_int) !u64 { return throughput; } -const exchanges = []Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }}; +const exchanges = [_]Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }}; pub fn benchmarkKeyExchange(comptime DhKeyExchange: var, comptime exchange_count: comptime_int) !u64 { std.debug.assert(DhKeyExchange.minimum_key_length >= DhKeyExchange.secret_length); @@ -135,13 +137,16 @@ pub fn main() !void { var buffer: [1024]u8 = undefined; var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]); - const args = try std.os.argsAlloc(&fixed.allocator); + const args = try std.process.argsAlloc(&fixed.allocator); var filter: ?[]u8 = ""; var i: usize = 1; while (i < args.len) : (i += 1) { - if (std.mem.eql(u8, args[i], "--seed")) { + if (std.mem.eql(u8, args[i], "--mode")) { + try stdout.print("{}\n", builtin.mode); + return; + } else if (std.mem.eql(u8, args[i], "--seed")) { i += 1; if (i == args.len) { usage(); diff --git a/std/crypto/blake2.zig b/std/crypto/blake2.zig index 2a2236dfd7..6bb2764b92 100644 --- a/std/crypto/blake2.zig +++ b/std/crypto/blake2.zig @@ -269,8 +269,8 @@ pub const Blake2b512 = Blake2b(512); fn Blake2b(comptime out_len: usize) type { return struct { const Self = @This(); - const block_length = 128; - const digest_length = out_len / 8; + pub const block_length = 128; + pub const digest_length = out_len / 8; const iv = [8]u64{ 0x6a09e667f3bcc908, diff --git a/std/crypto/sha2.zig b/std/crypto/sha2.zig index c1f7bd228d..b40a39d579 100644 --- a/std/crypto/sha2.zig +++ b/std/crypto/sha2.zig @@ -420,8 +420,8 @@ pub const Sha512 = Sha2_64(Sha512Params); fn Sha2_64(comptime params: Sha2Params64) type { return struct { const Self = @This(); - const block_length = 128; - const digest_length = params.out_len / 8; + pub const block_length = 128; + pub const digest_length = params.out_len / 8; s: [8]u64, // Streaming Cache diff --git a/std/event/fs.zig b/std/event/fs.zig index 1e979d55b3..4490e1deae 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -719,6 +719,16 @@ pub const WatchEventId = enum { Delete, }; +fn eqlString(a: []const u16, b: []const u16) bool { + if (a.len != b.len) return false; + if (a.ptr == b.ptr) return true; + return mem.compare(u16, a, b) == .Equal; +} + +fn hashString(s: []const u16) u32 { + return @truncate(u32, std.hash.Wyhash.hash(0, @sliceToBytes(s))); +} + //pub const WatchEventError = error{ // UserResourceLimitReached, // SystemResources, @@ -736,7 +746,7 @@ pub const WatchEventId = enum { // file_table: FileTable, // table_lock: event.Lock, // -// const FileTable = std.AutoHashMap([]const u8, *Put); +// const FileTable = std.StringHashmap(*Put); // const Put = struct { // putter: anyframe, // value_ptr: *V, @@ -755,8 +765,8 @@ pub const WatchEventId = enum { // all_putters: std.atomic.Queue(anyframe), // ref_count: std.atomic.Int(usize), // -// const DirTable = std.AutoHashMap([]const u8, *Dir); -// const FileTable = std.AutoHashMap([]const u16, V); +// const DirTable = std.StringHashMap(*Dir); +// const FileTable = std.HashMap([]const u16, V, hashString, eqlString); // // const Dir = struct { // putter: anyframe, @@ -772,7 +782,7 @@ pub const WatchEventId = enum { // table_lock: event.Lock, // // const WdTable = std.AutoHashMap(i32, Dir); -// const FileTable = std.AutoHashMap([]const u8, V); +// const FileTable = std.StringHashMap(V); // // const Dir = struct { // dirname: []const u8, @@ -780,7 +790,7 @@ pub const WatchEventId = enum { // }; // }; // -// const FileToHandle = std.AutoHashMap([]const u8, anyframe); +// const FileToHandle = std.StringHashMap(anyframe); // // const Self = @This(); // diff --git a/std/hash/auto_hash.zig b/std/hash/auto_hash.zig index 251f7e798a..d34fc2719a 100644 --- a/std/hash/auto_hash.zig +++ b/std/hash/auto_hash.zig @@ -1,11 +1,79 @@ const std = @import("std"); const builtin = @import("builtin"); +const assert = std.debug.assert; const mem = std.mem; const meta = std.meta; +/// Describes how pointer types should be hashed. +pub const HashStrategy = enum { + /// Do not follow pointers, only hash their value. + Shallow, + + /// Follow pointers, hash the pointee content. + /// Only dereferences one level, ie. it is changed into .Shallow when a + /// pointer type is encountered. + Deep, + + /// Follow pointers, hash the pointee content. + /// Dereferences all pointers encountered. + /// Assumes no cycle. + DeepRecursive, +}; + +/// Helper function to hash a pointer and mutate the strategy if needed. +pub fn hashPointer(hasher: var, key: var, comptime strat: HashStrategy) void { + const info = @typeInfo(@typeOf(key)); + + switch (info.Pointer.size) { + builtin.TypeInfo.Pointer.Size.One => switch (strat) { + .Shallow => hash(hasher, @ptrToInt(key), .Shallow), + .Deep => hash(hasher, key.*, .Shallow), + .DeepRecursive => hash(hasher, key.*, .DeepRecursive), + }, + + builtin.TypeInfo.Pointer.Size.Slice => switch (strat) { + .Shallow => { + hashPointer(hasher, key.ptr, .Shallow); + hash(hasher, key.len, .Shallow); + }, + .Deep => hashArray(hasher, key, .Shallow), + .DeepRecursive => hashArray(hasher, key, .DeepRecursive), + }, + + builtin.TypeInfo.Pointer.Size.Many, + builtin.TypeInfo.Pointer.Size.C, + => switch (strat) { + .Shallow => hash(hasher, @ptrToInt(key), .Shallow), + else => @compileError( + \\ unknown-length pointers and C pointers cannot be hashed deeply. + \\ Consider providing your own hash function. + ), + }, + } +} + +/// Helper function to hash a set of contiguous objects, from an array or slice. +pub fn hashArray(hasher: var, key: var, comptime strat: HashStrategy) void { + switch (strat) { + .Shallow => { + // TODO detect via a trait when Key has no padding bits to + // hash it as an array of bytes. + // Otherwise, hash every element. + for (key) |element| { + hash(hasher, element, .Shallow); + } + }, + else => { + for (key) |element| { + hash(hasher, element, strat); + } + }, + } +} + /// Provides generic hashing for any eligible type. -/// Only hashes `key` itself, pointers are not followed. -pub fn autoHash(hasher: var, key: var) void { +/// Strategy is provided to determine if pointers should be followed or not. +pub fn hash(hasher: var, key: var, comptime strat: HashStrategy) void { const Key = @typeOf(key); switch (@typeInfo(Key)) { .NoReturn, @@ -26,35 +94,18 @@ pub fn autoHash(hasher: var, key: var) void { // TODO Check if the situation is better after #561 is resolved. .Int => @inlineCall(hasher.update, std.mem.asBytes(&key)), - .Float => |info| autoHash(hasher, @bitCast(@IntType(false, info.bits), key)), + .Float => |info| hash(hasher, @bitCast(@IntType(false, info.bits), key), strat), - .Bool => autoHash(hasher, @boolToInt(key)), - .Enum => autoHash(hasher, @enumToInt(key)), - .ErrorSet => autoHash(hasher, @errorToInt(key)), - .AnyFrame, .Fn => autoHash(hasher, @ptrToInt(key)), + .Bool => hash(hasher, @boolToInt(key), strat), + .Enum => hash(hasher, @enumToInt(key), strat), + .ErrorSet => hash(hasher, @errorToInt(key), strat), + .AnyFrame, .Fn => hash(hasher, @ptrToInt(key), strat), - .Pointer => |info| switch (info.size) { - builtin.TypeInfo.Pointer.Size.One, - builtin.TypeInfo.Pointer.Size.Many, - builtin.TypeInfo.Pointer.Size.C, - => autoHash(hasher, @ptrToInt(key)), + .Pointer => @inlineCall(hashPointer, hasher, key, strat), - builtin.TypeInfo.Pointer.Size.Slice => { - autoHash(hasher, key.ptr); - autoHash(hasher, key.len); - }, - }, + .Optional => if (key) |k| hash(hasher, k, strat), - .Optional => if (key) |k| autoHash(hasher, k), - - .Array => { - // TODO detect via a trait when Key has no padding bits to - // hash it as an array of bytes. - // Otherwise, hash every element. - for (key) |element| { - autoHash(hasher, element); - } - }, + .Array => hashArray(hasher, key, strat), .Vector => |info| { if (info.child.bit_count % 8 == 0) { @@ -67,7 +118,7 @@ pub fn autoHash(hasher: var, key: var) void { const array: [info.len]info.child = key; comptime var i: u32 = 0; inline while (i < info.len) : (i += 1) { - autoHash(hasher, array[i]); + hash(hasher, array[i], strat); } } }, @@ -79,19 +130,19 @@ pub fn autoHash(hasher: var, key: var) void { inline for (info.fields) |field| { // We reuse the hash of the previous field as the seed for the // next one so that they're dependant. - autoHash(hasher, @field(key, field.name)); + hash(hasher, @field(key, field.name), strat); } }, .Union => |info| blk: { if (info.tag_type) |tag_type| { const tag = meta.activeTag(key); - const s = autoHash(hasher, tag); + const s = hash(hasher, tag, strat); inline for (info.fields) |field| { const enum_field = field.enum_field.?; if (enum_field.value == @enumToInt(tag)) { - autoHash(hasher, @field(key, enum_field.name)); - // TODO use a labelled break when it does not crash the compiler. + hash(hasher, @field(key, enum_field.name), strat); + // TODO use a labelled break when it does not crash the compiler. cf #2908 // break :blk; return; } @@ -102,25 +153,86 @@ pub fn autoHash(hasher: var, key: var) void { .ErrorUnion => blk: { const payload = key catch |err| { - autoHash(hasher, err); + hash(hasher, err, strat); break :blk; }; - autoHash(hasher, payload); + hash(hasher, payload, strat); }, } } +/// Provides generic hashing for any eligible type. +/// Only hashes `key` itself, pointers are not followed. +/// Slices are rejected to avoid ambiguity on the user's intention. +pub fn autoHash(hasher: var, key: var) void { + const Key = @typeOf(key); + if (comptime meta.trait.isSlice(Key)) { + comptime assert(@hasDecl(std, "StringHashMap")); // detect when the following message needs updated + const extra_help = if (Key == []const u8) + " Consider std.StringHashMap for hashing the contents of []const u8." + else + ""; + + @compileError("std.auto_hash.autoHash does not allow slices (here " ++ @typeName(Key) ++ + ") because the intent is unclear. Consider using std.auto_hash.hash or providing your own hash function instead." ++ + extra_help); + } + + hash(hasher, key, .Shallow); +} + const testing = std.testing; const Wyhash = std.hash.Wyhash; -fn testAutoHash(key: var) u64 { +fn testHash(key: var) u64 { // Any hash could be used here, for testing autoHash. var hasher = Wyhash.init(0); - autoHash(&hasher, key); + hash(&hasher, key, .Shallow); return hasher.final(); } -test "autoHash slice" { +fn testHashShallow(key: var) u64 { + // Any hash could be used here, for testing autoHash. + var hasher = Wyhash.init(0); + hash(&hasher, key, .Shallow); + return hasher.final(); +} + +fn testHashDeep(key: var) u64 { + // Any hash could be used here, for testing autoHash. + var hasher = Wyhash.init(0); + hash(&hasher, key, .Deep); + return hasher.final(); +} + +fn testHashDeepRecursive(key: var) u64 { + // Any hash could be used here, for testing autoHash. + var hasher = Wyhash.init(0); + hash(&hasher, key, .DeepRecursive); + return hasher.final(); +} + +test "hash pointer" { + const array = [_]u32{ 123, 123, 123 }; + const a = &array[0]; + const b = &array[1]; + const c = &array[2]; + const d = a; + + testing.expect(testHashShallow(a) == testHashShallow(d)); + testing.expect(testHashShallow(a) != testHashShallow(c)); + testing.expect(testHashShallow(a) != testHashShallow(b)); + + testing.expect(testHashDeep(a) == testHashDeep(a)); + testing.expect(testHashDeep(a) == testHashDeep(c)); + testing.expect(testHashDeep(a) == testHashDeep(b)); + + testing.expect(testHashDeepRecursive(a) == testHashDeepRecursive(a)); + testing.expect(testHashDeepRecursive(a) == testHashDeepRecursive(c)); + testing.expect(testHashDeepRecursive(a) == testHashDeepRecursive(b)); +} + +test "hash slice shallow" { // Allocate one array dynamically so that we're assured it is not merged // with the other by the optimization passes. const array1 = try std.heap.direct_allocator.create([6]u32); @@ -130,23 +242,78 @@ test "autoHash slice" { const a = array1[0..]; const b = array2[0..]; const c = array1[0..3]; - testing.expect(testAutoHash(a) == testAutoHash(a)); - testing.expect(testAutoHash(a) != testAutoHash(array1)); - testing.expect(testAutoHash(a) != testAutoHash(b)); - testing.expect(testAutoHash(a) != testAutoHash(c)); + testing.expect(testHashShallow(a) == testHashShallow(a)); + testing.expect(testHashShallow(a) != testHashShallow(array1)); + testing.expect(testHashShallow(a) != testHashShallow(b)); + testing.expect(testHashShallow(a) != testHashShallow(c)); } -test "testAutoHash optional" { +test "hash slice deep" { + // Allocate one array dynamically so that we're assured it is not merged + // with the other by the optimization passes. + const array1 = try std.heap.direct_allocator.create([6]u32); + defer std.heap.direct_allocator.destroy(array1); + array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 }; + const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 }; + const a = array1[0..]; + const b = array2[0..]; + const c = array1[0..3]; + testing.expect(testHashDeep(a) == testHashDeep(a)); + testing.expect(testHashDeep(a) == testHashDeep(array1)); + testing.expect(testHashDeep(a) == testHashDeep(b)); + testing.expect(testHashDeep(a) != testHashDeep(c)); +} + +test "hash struct deep" { + const Foo = struct { + a: u32, + b: f64, + c: *bool, + + const Self = @This(); + + pub fn init(allocator: *mem.Allocator, a_: u32, b_: f64, c_: bool) !Self { + const ptr = try allocator.create(bool); + ptr.* = c_; + return Self{ .a = a_, .b = b_, .c = ptr }; + } + }; + + const allocator = std.heap.direct_allocator; + const foo = try Foo.init(allocator, 123, 1.0, true); + const bar = try Foo.init(allocator, 123, 1.0, true); + const baz = try Foo.init(allocator, 123, 1.0, false); + defer allocator.destroy(foo.c); + defer allocator.destroy(bar.c); + defer allocator.destroy(baz.c); + + testing.expect(testHashDeep(foo) == testHashDeep(bar)); + testing.expect(testHashDeep(foo) != testHashDeep(baz)); + testing.expect(testHashDeep(bar) != testHashDeep(baz)); + + var hasher = Wyhash.init(0); + const h = testHashDeep(foo); + autoHash(&hasher, foo.a); + autoHash(&hasher, foo.b); + autoHash(&hasher, foo.c.*); + testing.expectEqual(h, hasher.final()); + + const h2 = testHashDeepRecursive(&foo); + testing.expect(h2 != testHashDeep(&foo)); + testing.expect(h2 == testHashDeep(foo)); +} + +test "testHash optional" { const a: ?u32 = 123; const b: ?u32 = null; - testing.expectEqual(testAutoHash(a), testAutoHash(u32(123))); - testing.expect(testAutoHash(a) != testAutoHash(b)); - testing.expectEqual(testAutoHash(b), 0); + testing.expectEqual(testHash(a), testHash(u32(123))); + testing.expect(testHash(a) != testHash(b)); + testing.expectEqual(testHash(b), 0); } -test "testAutoHash array" { +test "testHash array" { const a = [_]u32{ 1, 2, 3 }; - const h = testAutoHash(a); + const h = testHash(a); var hasher = Wyhash.init(0); autoHash(&hasher, u32(1)); autoHash(&hasher, u32(2)); @@ -154,14 +321,14 @@ test "testAutoHash array" { testing.expectEqual(h, hasher.final()); } -test "testAutoHash struct" { +test "testHash struct" { const Foo = struct { a: u32 = 1, b: u32 = 2, c: u32 = 3, }; const f = Foo{}; - const h = testAutoHash(f); + const h = testHash(f); var hasher = Wyhash.init(0); autoHash(&hasher, u32(1)); autoHash(&hasher, u32(2)); @@ -169,7 +336,7 @@ test "testAutoHash struct" { testing.expectEqual(h, hasher.final()); } -test "testAutoHash union" { +test "testHash union" { const Foo = union(enum) { A: u32, B: f32, @@ -179,24 +346,24 @@ test "testAutoHash union" { const a = Foo{ .A = 18 }; var b = Foo{ .B = 12.34 }; const c = Foo{ .C = 18 }; - testing.expect(testAutoHash(a) == testAutoHash(a)); - testing.expect(testAutoHash(a) != testAutoHash(b)); - testing.expect(testAutoHash(a) != testAutoHash(c)); + testing.expect(testHash(a) == testHash(a)); + testing.expect(testHash(a) != testHash(b)); + testing.expect(testHash(a) != testHash(c)); b = Foo{ .A = 18 }; - testing.expect(testAutoHash(a) == testAutoHash(b)); + testing.expect(testHash(a) == testHash(b)); } -test "testAutoHash vector" { +test "testHash vector" { const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 }; const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 }; const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 }; - testing.expect(testAutoHash(a) == testAutoHash(a)); - testing.expect(testAutoHash(a) != testAutoHash(b)); - testing.expect(testAutoHash(a) != testAutoHash(c)); + testing.expect(testHash(a) == testHash(a)); + testing.expect(testHash(a) != testHash(b)); + testing.expect(testHash(a) != testHash(c)); } -test "testAutoHash error union" { +test "testHash error union" { const Errors = error{Test}; const Foo = struct { a: u32 = 1, @@ -205,7 +372,7 @@ test "testAutoHash error union" { }; const f = Foo{}; const g: Errors!Foo = Errors.Test; - testing.expect(testAutoHash(f) != testAutoHash(g)); - testing.expect(testAutoHash(f) == testAutoHash(Foo{})); - testing.expect(testAutoHash(g) == testAutoHash(Errors.Test)); + testing.expect(testHash(f) != testHash(g)); + testing.expect(testHash(f) == testHash(Foo{})); + testing.expect(testHash(g) == testHash(Errors.Test)); } diff --git a/std/hash/benchmark.zig b/std/hash/benchmark.zig new file mode 100644 index 0000000000..d110684a8e --- /dev/null +++ b/std/hash/benchmark.zig @@ -0,0 +1,273 @@ +// zig run benchmark.zig --release-fast --override-std-dir .. + +const builtin = @import("builtin"); +const std = @import("std"); +const time = std.time; +const Timer = time.Timer; +const hash = std.hash; + +const KiB = 1024; +const MiB = 1024 * KiB; +const GiB = 1024 * MiB; + +var prng = std.rand.DefaultPrng.init(0); + +const Hash = struct { + ty: type, + name: []const u8, + has_iterative_api: bool = true, + init_u8s: ?[]const u8 = null, + init_u64: ?u64 = null, +}; + +const siphash_key = "0123456789abcdef"; + +const hashes = [_]Hash{ + Hash{ + .ty = hash.Wyhash, + .name = "wyhash", + .init_u64 = 0, + }, + Hash{ + .ty = hash.SipHash64(1, 3), + .name = "siphash(1,3)", + .init_u8s = siphash_key, + }, + Hash{ + .ty = hash.SipHash64(2, 4), + .name = "siphash(2,4)", + .init_u8s = siphash_key, + }, + Hash{ + .ty = hash.Fnv1a_64, + .name = "fnv1a", + }, + Hash{ + .ty = hash.Adler32, + .name = "adler32", + }, + Hash{ + .ty = hash.crc.Crc32WithPoly(hash.crc.Polynomial.IEEE), + .name = "crc32-slicing-by-8", + }, + Hash{ + .ty = hash.crc.Crc32SmallWithPoly(hash.crc.Polynomial.IEEE), + .name = "crc32-half-byte-lookup", + }, + Hash{ + .ty = hash.CityHash32, + .name = "cityhash-32", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.CityHash64, + .name = "cityhash-64", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.Murmur2_32, + .name = "murmur2-32", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.Murmur2_64, + .name = "murmur2-64", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.Murmur3_32, + .name = "murmur3-32", + .has_iterative_api = false, + }, +}; + +const Result = struct { + hash: u64, + throughput: u64, +}; + +const block_size: usize = 8 * 8192; + +pub fn benchmarkHash(comptime H: var, bytes: usize) !Result { + var h = blk: { + if (H.init_u8s) |init| { + break :blk H.ty.init(init); + } + if (H.init_u64) |init| { + break :blk H.ty.init(init); + } + break :blk H.ty.init(); + }; + + var block: [block_size]u8 = undefined; + prng.random.bytes(block[0..]); + + var offset: usize = 0; + var timer = try Timer.start(); + const start = timer.lap(); + while (offset < bytes) : (offset += block.len) { + h.update(block[0..]); + } + const end = timer.read(); + + const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; + const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s); + + return Result{ + .hash = h.final(), + .throughput = throughput, + }; +} + +pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !Result { + const key_count = bytes / key_size; + var block: [block_size]u8 = undefined; + prng.random.bytes(block[0..]); + + var i: usize = 0; + var timer = try Timer.start(); + const start = timer.lap(); + + var sum: u64 = 0; + while (i < key_count) : (i += 1) { + const small_key = block[0..key_size]; + sum +%= blk: { + if (H.init_u8s) |init| { + break :blk H.ty.hash(init, small_key); + } + if (H.init_u64) |init| { + break :blk H.ty.hash(init, small_key); + } + break :blk H.ty.hash(small_key); + }; + } + const end = timer.read(); + + const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; + const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s); + + return Result{ + .hash = sum, + .throughput = throughput, + }; +} + +fn usage() void { + std.debug.warn( + \\throughput_test [options] + \\ + \\Options: + \\ --filter [test-name] + \\ --seed [int] + \\ --count [int] + \\ --key-size [int] + \\ --iterative-only + \\ --help + \\ + ); +} + +fn mode(comptime x: comptime_int) comptime_int { + return if (builtin.mode == builtin.Mode.Debug) x / 64 else x; +} + +// TODO(#1358): Replace with builtin formatted padding when available. +fn printPad(stdout: var, s: []const u8) !void { + var i: usize = 0; + while (i < 12 - s.len) : (i += 1) { + try stdout.print(" "); + } + try stdout.print("{}", s); +} + +pub fn main() !void { + var stdout_file = try std.io.getStdOut(); + var stdout_out_stream = stdout_file.outStream(); + const stdout = &stdout_out_stream.stream; + + var buffer: [1024]u8 = undefined; + var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]); + const args = try std.process.argsAlloc(&fixed.allocator); + + var filter: ?[]u8 = ""; + var count: usize = mode(128 * MiB); + var key_size: usize = 32; + var seed: u32 = 0; + var test_iterative_only = false; + + var i: usize = 1; + while (i < args.len) : (i += 1) { + if (std.mem.eql(u8, args[i], "--mode")) { + try stdout.print("{}\n", builtin.mode); + return; + } else if (std.mem.eql(u8, args[i], "--seed")) { + i += 1; + if (i == args.len) { + usage(); + std.os.exit(1); + } + + seed = try std.fmt.parseUnsigned(u32, args[i], 10); + // we seed later + } else if (std.mem.eql(u8, args[i], "--filter")) { + i += 1; + if (i == args.len) { + usage(); + std.os.exit(1); + } + + filter = args[i]; + } else if (std.mem.eql(u8, args[i], "--count")) { + i += 1; + if (i == args.len) { + usage(); + std.os.exit(1); + } + + const c = try std.fmt.parseUnsigned(usize, args[i], 10); + count = c * MiB; + } else if (std.mem.eql(u8, args[i], "--key-size")) { + i += 1; + if (i == args.len) { + usage(); + std.os.exit(1); + } + + key_size = try std.fmt.parseUnsigned(usize, args[i], 10); + if (key_size > block_size) { + try stdout.print("key_size cannot exceed block size of {}\n", block_size); + std.os.exit(1); + } + } else if (std.mem.eql(u8, args[i], "--iterative-only")) { + test_iterative_only = true; + } else if (std.mem.eql(u8, args[i], "--help")) { + usage(); + return; + } else { + usage(); + std.os.exit(1); + } + } + + inline for (hashes) |H| { + if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { + if (!test_iterative_only or H.has_iterative_api) { + try stdout.print("{}\n", H.name); + + // Always reseed prior to every call so we are hashing the same buffer contents. + // This allows easier comparison between different implementations. + if (H.has_iterative_api) { + prng.seed(seed); + const result = try benchmarkHash(H, count); + try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); + } + + if (!test_iterative_only) { + prng.seed(seed); + const result_small = try benchmarkHashSmallKeys(H, key_size, count); + try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash); + } + } + } + } +} diff --git a/std/hash/crc.zig b/std/hash/crc.zig index 53b4262c93..cdcaf55610 100644 --- a/std/hash/crc.zig +++ b/std/hash/crc.zig @@ -10,9 +10,9 @@ const debug = std.debug; const testing = std.testing; pub const Polynomial = struct { - const IEEE = 0xedb88320; - const Castagnoli = 0x82f63b78; - const Koopman = 0xeb31d82e; + pub const IEEE = 0xedb88320; + pub const Castagnoli = 0x82f63b78; + pub const Koopman = 0xeb31d82e; }; // IEEE is by far the most common CRC and so is aliased by default. diff --git a/std/hash/siphash.zig b/std/hash/siphash.zig index 8e83d67897..3d67ba685b 100644 --- a/std/hash/siphash.zig +++ b/std/hash/siphash.zig @@ -21,7 +21,7 @@ pub fn SipHash128(comptime c_rounds: usize, comptime d_rounds: usize) type { return SipHash(u128, c_rounds, d_rounds); } -fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type { +fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type { assert(T == u64 or T == u128); assert(c_rounds > 0 and d_rounds > 0); @@ -34,10 +34,6 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) v1: u64, v2: u64, v3: u64, - - // streaming cache - buf: [8]u8, - buf_len: usize, msg_len: u8, pub fn init(key: []const u8) Self { @@ -51,9 +47,6 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) .v1 = k1 ^ 0x646f72616e646f6d, .v2 = k0 ^ 0x6c7967656e657261, .v3 = k1 ^ 0x7465646279746573, - - .buf = undefined, - .buf_len = 0, .msg_len = 0, }; @@ -64,73 +57,66 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) return d; } - pub fn update(d: *Self, b: []const u8) void { + pub fn update(self: *Self, b: []const u8) void { + std.debug.assert(b.len % 8 == 0); + var off: usize = 0; - - // Partial from previous. - if (d.buf_len != 0 and d.buf_len + b.len > 8) { - off += 8 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); - d.round(d.buf[0..]); - d.buf_len = 0; + while (off < b.len) : (off += 8) { + @inlineCall(self.round, b[off .. off + 8]); } - // Full middle blocks. - while (off + 8 <= b.len) : (off += 8) { - d.round(b[off .. off + 8]); - } - - // Remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); - d.msg_len +%= @truncate(u8, b.len); + self.msg_len +%= @truncate(u8, b.len); } - pub fn final(d: *Self) T { - // Padding - mem.set(u8, d.buf[d.buf_len..], 0); - d.buf[7] = d.msg_len; - d.round(d.buf[0..]); + pub fn final(self: *Self, b: []const u8) T { + std.debug.assert(b.len < 8); + + self.msg_len +%= @truncate(u8, b.len); + + var buf = [_]u8{0} ** 8; + mem.copy(u8, buf[0..], b[0..]); + buf[7] = self.msg_len; + self.round(buf[0..]); if (T == u128) { - d.v2 ^= 0xee; + self.v2 ^= 0xee; } else { - d.v2 ^= 0xff; + self.v2 ^= 0xff; } comptime var i: usize = 0; inline while (i < d_rounds) : (i += 1) { - @inlineCall(sipRound, d); + @inlineCall(sipRound, self); } - const b1 = d.v0 ^ d.v1 ^ d.v2 ^ d.v3; + const b1 = self.v0 ^ self.v1 ^ self.v2 ^ self.v3; if (T == u64) { return b1; } - d.v1 ^= 0xdd; + self.v1 ^= 0xdd; comptime var j: usize = 0; inline while (j < d_rounds) : (j += 1) { - @inlineCall(sipRound, d); + @inlineCall(sipRound, self); } - const b2 = d.v0 ^ d.v1 ^ d.v2 ^ d.v3; + const b2 = self.v0 ^ self.v1 ^ self.v2 ^ self.v3; return (u128(b2) << 64) | b1; } - fn round(d: *Self, b: []const u8) void { + fn round(self: *Self, b: []const u8) void { assert(b.len == 8); const m = mem.readIntSliceLittle(u64, b[0..]); - d.v3 ^= m; + self.v3 ^= m; comptime var i: usize = 0; inline while (i < c_rounds) : (i += 1) { - @inlineCall(sipRound, d); + @inlineCall(sipRound, self); } - d.v0 ^= m; + self.v0 ^= m; } fn sipRound(d: *Self) void { @@ -151,9 +137,61 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) } pub fn hash(key: []const u8, input: []const u8) T { + const aligned_len = input.len - (input.len % 8); + var c = Self.init(key); - c.update(input); - return c.final(); + @inlineCall(c.update, input[0..aligned_len]); + return @inlineCall(c.final, input[aligned_len..]); + } + }; +} + +pub fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type { + assert(T == u64 or T == u128); + assert(c_rounds > 0 and d_rounds > 0); + + return struct { + const State = SipHashStateless(T, c_rounds, d_rounds); + const Self = @This(); + const digest_size = 64; + const block_size = 64; + + state: State, + buf: [8]u8, + buf_len: usize, + + pub fn init(key: []const u8) Self { + return Self{ + .state = State.init(key), + .buf = undefined, + .buf_len = 0, + }; + } + + pub fn update(self: *Self, b: []const u8) void { + var off: usize = 0; + + if (self.buf_len != 0 and self.buf_len + b.len >= 8) { + off += 8 - self.buf_len; + mem.copy(u8, self.buf[self.buf_len..], b[0..off]); + self.state.update(self.buf[0..]); + self.buf_len = 0; + } + + const remain_len = b.len - off; + const aligned_len = remain_len - (remain_len % 8); + self.state.update(b[off .. off + aligned_len]); + + mem.copy(u8, self.buf[self.buf_len..], b[off + aligned_len ..]); + self.buf_len += @intCast(u8, b[off + aligned_len ..].len); + } + + pub fn final(self: *Self) T { + return self.state.final(self.buf[0..self.buf_len]); + } + + pub fn hash(key: []const u8, input: []const u8) T { + return State.hash(key, input); } }; } @@ -237,7 +275,7 @@ test "siphash64-2-4 sanity" { buffer[i] = @intCast(u8, i); const expected = mem.readIntLittle(u64, &vector); - testing.expect(siphash.hash(test_key, buffer[0..i]) == expected); + testing.expectEqual(siphash.hash(test_key, buffer[0..i]), expected); } } @@ -316,6 +354,30 @@ test "siphash128-2-4 sanity" { buffer[i] = @intCast(u8, i); const expected = mem.readIntLittle(u128, &vector); - testing.expect(siphash.hash(test_key, buffer[0..i]) == expected); + testing.expectEqual(siphash.hash(test_key, buffer[0..i]), expected); + } +} + +test "iterative non-divisible update" { + var buf: [1024]u8 = undefined; + for (buf) |*e, i| { + e.* = @truncate(u8, i); + } + + const key = "0x128dad08f12307"; + const Siphash = SipHash64(2, 4); + + var end: usize = 9; + while (end < buf.len) : (end += 9) { + const non_iterative_hash = Siphash.hash(key, buf[0..end]); + + var wy = Siphash.init(key); + var i: usize = 0; + while (i < end) : (i += 7) { + wy.update(buf[i..std.math.min(i + 7, end)]); + } + const iterative_hash = wy.final(); + + std.testing.expectEqual(iterative_hash, non_iterative_hash); } } diff --git a/std/hash/throughput_test.zig b/std/hash/throughput_test.zig deleted file mode 100644 index 4b7e8ef344..0000000000 --- a/std/hash/throughput_test.zig +++ /dev/null @@ -1,148 +0,0 @@ -const builtin = @import("builtin"); -const std = @import("std"); -const time = std.time; -const Timer = time.Timer; -const hash = std.hash; - -const KiB = 1024; -const MiB = 1024 * KiB; -const GiB = 1024 * MiB; - -var prng = std.rand.DefaultPrng.init(0); - -const Hash = struct { - ty: type, - name: []const u8, - init_u8s: ?[]const u8 = null, - init_u64: ?u64 = null, -}; - -const siphash_key = "0123456789abcdef"; - -const hashes = [_]Hash{ - Hash{ .ty = hash.Wyhash, .name = "wyhash", .init_u64 = 0 }, - Hash{ .ty = hash.SipHash64(1, 3), .name = "siphash(1,3)", .init_u8s = siphash_key }, - Hash{ .ty = hash.SipHash64(2, 4), .name = "siphash(2,4)", .init_u8s = siphash_key }, - Hash{ .ty = hash.Fnv1a_64, .name = "fnv1a" }, - Hash{ .ty = hash.Crc32, .name = "crc32" }, -}; - -const Result = struct { - hash: u64, - throughput: u64, -}; - -pub fn benchmarkHash(comptime H: var, bytes: usize) !Result { - var h = blk: { - if (H.init_u8s) |init| { - break :blk H.ty.init(init); - } - if (H.init_u64) |init| { - break :blk H.ty.init(init); - } - break :blk H.ty.init(); - }; - - var block: [8192]u8 = undefined; - prng.random.bytes(block[0..]); - - var offset: usize = 0; - var timer = try Timer.start(); - const start = timer.lap(); - while (offset < bytes) : (offset += block.len) { - h.update(block[0..]); - } - const end = timer.read(); - - const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; - const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s); - - return Result{ - .hash = h.final(), - .throughput = throughput, - }; -} - -fn usage() void { - std.debug.warn( - \\throughput_test [options] - \\ - \\Options: - \\ --filter [test-name] - \\ --seed [int] - \\ --count [int] - \\ --help - \\ - ); -} - -fn mode(comptime x: comptime_int) comptime_int { - return if (builtin.mode == builtin.Mode.Debug) x / 64 else x; -} - -// TODO(#1358): Replace with builtin formatted padding when available. -fn printPad(stdout: var, s: []const u8) !void { - var i: usize = 0; - while (i < 12 - s.len) : (i += 1) { - try stdout.print(" "); - } - try stdout.print("{}", s); -} - -pub fn main() !void { - var stdout_file = try std.io.getStdOut(); - var stdout_out_stream = stdout_file.outStream(); - const stdout = &stdout_out_stream.stream; - - var buffer: [1024]u8 = undefined; - var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]); - const args = try std.process.argsAlloc(&fixed.allocator); - - var filter: ?[]u8 = ""; - var count: usize = mode(128 * MiB); - - var i: usize = 1; - while (i < args.len) : (i += 1) { - if (std.mem.eql(u8, args[i], "--seed")) { - i += 1; - if (i == args.len) { - usage(); - std.os.exit(1); - } - - const seed = try std.fmt.parseUnsigned(u32, args[i], 10); - prng.seed(seed); - } else if (std.mem.eql(u8, args[i], "--filter")) { - i += 1; - if (i == args.len) { - usage(); - std.os.exit(1); - } - - filter = args[i]; - } else if (std.mem.eql(u8, args[i], "--count")) { - i += 1; - if (i == args.len) { - usage(); - std.os.exit(1); - } - - const c = try std.fmt.parseUnsigned(u32, args[i], 10); - count = c * MiB; - } else if (std.mem.eql(u8, args[i], "--help")) { - usage(); - return; - } else { - usage(); - std.os.exit(1); - } - } - - inline for (hashes) |H| { - if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { - const result = try benchmarkHash(H, count); - try printPad(stdout, H.name); - try stdout.print(": {:4} MiB/s [{:16}]\n", result.throughput / (1 * MiB), result.hash); - } - } -} diff --git a/std/hash/wyhash.zig b/std/hash/wyhash.zig index dfa5156cad..7e35ccc6d2 100644 --- a/std/hash/wyhash.zig +++ b/std/hash/wyhash.zig @@ -10,7 +10,8 @@ const primes = [_]u64{ }; fn read_bytes(comptime bytes: u8, data: []const u8) u64 { - return mem.readVarInt(u64, data[0..bytes], .Little); + const T = @IntType(false, 8 * bytes); + return mem.readIntSliceLittle(T, data[0..bytes]); } fn read_8bytes_swapped(data: []const u8) u64 { @@ -31,18 +32,21 @@ fn mix1(a: u64, b: u64, seed: u64) u64 { return mum(a ^ seed ^ primes[2], b ^ seed ^ primes[3]); } -pub const Wyhash = struct { +// Wyhash version which does not store internal state for handling partial buffers. +// This is needed so that we can maximize the speed for the short key case, which will +// use the non-iterative api which the public Wyhash exposes. +const WyhashStateless = struct { seed: u64, msg_len: usize, - pub fn init(seed: u64) Wyhash { - return Wyhash{ + pub fn init(seed: u64) WyhashStateless { + return WyhashStateless{ .seed = seed, .msg_len = 0, }; } - fn round(self: *Wyhash, b: []const u8) void { + fn round(self: *WyhashStateless, b: []const u8) void { std.debug.assert(b.len == 32); self.seed = mix0( @@ -56,12 +60,25 @@ pub const Wyhash = struct { ); } - fn partial(self: *Wyhash, b: []const u8) void { - const rem_key = b; - const rem_len = b.len; + pub fn update(self: *WyhashStateless, b: []const u8) void { + std.debug.assert(b.len % 32 == 0); - var seed = self.seed; - seed = switch (@intCast(u5, rem_len)) { + var off: usize = 0; + while (off < b.len) : (off += 32) { + @inlineCall(self.round, b[off .. off + 32]); + } + + self.msg_len += b.len; + } + + pub fn final(self: *WyhashStateless, b: []const u8) u64 { + std.debug.assert(b.len < 32); + + const seed = self.seed; + const rem_len = @intCast(u5, b.len); + const rem_key = b[0..rem_len]; + + self.seed = switch (rem_len) { 0 => seed, 1 => mix0(read_bytes(1, rem_key), primes[4], seed), 2 => mix0(read_bytes(2, rem_key), primes[4], seed), @@ -95,34 +112,70 @@ pub const Wyhash = struct { 30 => mix0(read_8bytes_swapped(rem_key), read_8bytes_swapped(rem_key[8..]), seed) ^ mix1(read_8bytes_swapped(rem_key[16..]), (read_bytes(4, rem_key[24..]) << 16) | read_bytes(2, rem_key[28..]), seed), 31 => mix0(read_8bytes_swapped(rem_key), read_8bytes_swapped(rem_key[8..]), seed) ^ mix1(read_8bytes_swapped(rem_key[16..]), (read_bytes(4, rem_key[24..]) << 24) | (read_bytes(2, rem_key[28..]) << 8) | read_bytes(1, rem_key[30..]), seed), }; - self.seed = seed; + + self.msg_len += b.len; + return mum(self.seed ^ self.msg_len, primes[4]); + } + + pub fn hash(seed: u64, input: []const u8) u64 { + const aligned_len = input.len - (input.len % 32); + + var c = WyhashStateless.init(seed); + @inlineCall(c.update, input[0..aligned_len]); + return @inlineCall(c.final, input[aligned_len..]); + } +}; + +/// Fast non-cryptographic 64bit hash function. +/// See https://github.com/wangyi-fudan/wyhash +pub const Wyhash = struct { + state: WyhashStateless, + + buf: [32]u8, + buf_len: usize, + + pub fn init(seed: u64) Wyhash { + return Wyhash{ + .state = WyhashStateless.init(seed), + .buf = undefined, + .buf_len = 0, + }; } pub fn update(self: *Wyhash, b: []const u8) void { var off: usize = 0; - // Full middle blocks. - while (off + 32 <= b.len) : (off += 32) { - @inlineCall(self.round, b[off .. off + 32]); + if (self.buf_len != 0 and self.buf_len + b.len >= 32) { + off += 32 - self.buf_len; + mem.copy(u8, self.buf[self.buf_len..], b[0..off]); + self.state.update(self.buf[0..]); + self.buf_len = 0; } - self.partial(b[off..]); - self.msg_len += b.len; + const remain_len = b.len - off; + const aligned_len = remain_len - (remain_len % 32); + self.state.update(b[off .. off + aligned_len]); + + mem.copy(u8, self.buf[self.buf_len..], b[off + aligned_len ..]); + self.buf_len += @intCast(u8, b[off + aligned_len ..].len); } pub fn final(self: *Wyhash) u64 { - return mum(self.seed ^ self.msg_len, primes[4]); + const seed = self.state.seed; + const rem_len = @intCast(u5, self.buf_len); + const rem_key = self.buf[0..self.buf_len]; + + return self.state.final(rem_key); } pub fn hash(seed: u64, input: []const u8) u64 { - var c = Wyhash.init(seed); - c.update(input); - return c.final(); + return WyhashStateless.hash(seed, input); } }; +const expectEqual = std.testing.expectEqual; + test "test vectors" { - const expectEqual = std.testing.expectEqual; const hash = Wyhash.hash; expectEqual(hash(0, ""), 0x0); @@ -133,3 +186,46 @@ test "test vectors" { expectEqual(hash(5, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0x602a1894d3bbfe7f); expectEqual(hash(6, "12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 0x829e9c148b75970e); } + +test "test vectors streaming" { + var wh = Wyhash.init(5); + for ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") |e| { + wh.update(mem.asBytes(&e)); + } + expectEqual(wh.final(), 0x602a1894d3bbfe7f); + + const pattern = "1234567890"; + const count = 8; + const result = 0x829e9c148b75970e; + expectEqual(Wyhash.hash(6, pattern ** 8), result); + + wh = Wyhash.init(6); + var i: u32 = 0; + while (i < count) : (i += 1) { + wh.update(pattern); + } + expectEqual(wh.final(), result); +} + +test "iterative non-divisible update" { + var buf: [8192]u8 = undefined; + for (buf) |*e, i| { + e.* = @truncate(u8, i); + } + + const seed = 0x128dad08f; + + var end: usize = 32; + while (end < buf.len) : (end += 32) { + const non_iterative_hash = Wyhash.hash(seed, buf[0..end]); + + var wy = Wyhash.init(seed); + var i: usize = 0; + while (i < end) : (i += 33) { + wy.update(buf[i..std.math.min(i + 33, end)]); + } + const iterative_hash = wy.final(); + + std.testing.expectEqual(iterative_hash, non_iterative_hash); + } +} diff --git a/std/hash_map.zig b/std/hash_map.zig index ab3c4c248d..5a852d4302 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -17,6 +17,21 @@ pub fn AutoHashMap(comptime K: type, comptime V: type) type { return HashMap(K, V, getAutoHashFn(K), getAutoEqlFn(K)); } +/// Builtin hashmap for strings as keys. +pub fn StringHashMap(comptime V: type) type { + return HashMap([]const u8, V, hashString, eqlString); +} + +pub fn eqlString(a: []const u8, b: []const u8) bool { + if (a.len != b.len) return false; + if (a.ptr == b.ptr) return true; + return mem.compare(u8, a, b) == .Equal; +} + +pub fn hashString(s: []const u8) u32 { + return @truncate(u32, std.hash.Wyhash.hash(0, s)); +} + pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u32, comptime eql: fn (a: K, b: K) bool) type { return struct { entries: []Entry, diff --git a/std/http/headers.zig b/std/http/headers.zig index c588f2d055..67624b5b11 100644 --- a/std/http/headers.zig +++ b/std/http/headers.zig @@ -102,19 +102,9 @@ test "HeaderEntry" { testing.expectEqualSlices(u8, "x", e.value); } -fn stringEql(a: []const u8, b: []const u8) bool { - if (a.len != b.len) return false; - if (a.ptr == b.ptr) return true; - return mem.compare(u8, a, b) == .Equal; -} - -fn stringHash(s: []const u8) u32 { - return @truncate(u32, std.hash.Wyhash.hash(0, s)); -} - const HeaderList = std.ArrayList(HeaderEntry); const HeaderIndexList = std.ArrayList(usize); -const HeaderIndex = std.HashMap([]const u8, HeaderIndexList, stringHash, stringEql); +const HeaderIndex = std.StringHashMap(HeaderIndexList); pub const Headers = struct { // the owned header field name is stored in the index as part of the key diff --git a/std/os/windows.zig b/std/os/windows.zig index 7c1761a4b8..5b07de6b3b 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -65,7 +65,7 @@ pub const CreateFileError = error{ InvalidUtf8, /// On Windows, file paths cannot contain these characters: - /// '/', '*', '?', '"', '<', '>', '|' + /// '*', '?', '"', '<', '>', '|', and '/' (when the ABI is not GNU) BadPathName, Unexpected, @@ -836,11 +836,10 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16) // > converting the name to an NT-style name, except when using the "\\?\" // > prefix as detailed in the following sections. // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation - // Because we want the larger maximum path length for absolute paths, we - // disallow forward slashes in zig std lib file functions on Windows. for (s) |byte| { switch (byte) { - '/', '*', '?', '"', '<', '>', '|' => return error.BadPathName, + '*', '?', '"', '<', '>', '|' => return error.BadPathName, + '/' => if (builtin.abi == .msvc) return error.BadPathName, else => {}, } } diff --git a/std/std.zig b/std/std.zig index e48586d873..18bd550eb5 100644 --- a/std/std.zig +++ b/std/std.zig @@ -17,6 +17,7 @@ pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList; pub const StaticallyInitializedMutex = @import("statically_initialized_mutex.zig").StaticallyInitializedMutex; pub const SegmentedList = @import("segmented_list.zig").SegmentedList; pub const SpinLock = @import("spinlock.zig").SpinLock; +pub const StringHashMap = @import("hash_map.zig").StringHashMap; pub const ChildProcess = @import("child_process.zig").ChildProcess; pub const TailQueue = @import("linked_list.zig").TailQueue; pub const Thread = @import("thread.zig").Thread; diff --git a/std/zig/ast.zig b/std/zig/ast.zig index e5781da035..c2f369ff4a 100644 --- a/std/zig/ast.zig +++ b/std/zig/ast.zig @@ -761,6 +761,7 @@ pub const Node = struct { name_token: TokenIndex, type_expr: ?*Node, value_expr: ?*Node, + align_expr: ?*Node, pub fn iterate(self: *ContainerField, index: usize) ?*Node { var i = index; diff --git a/std/zig/parse.zig b/std/zig/parse.zig index 0a2fbb4fa1..0511b6dce0 100644 --- a/std/zig/parse.zig +++ b/std/zig/parse.zig @@ -380,16 +380,18 @@ fn parseVarDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { return &node.base; } -/// ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)? +/// ContainerField <- IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)? fn parseContainerField(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { const name_token = eatToken(it, .Identifier) orelse return null; - const type_expr = if (eatToken(it, .Colon)) |_| - try expectNode(arena, it, tree, parseTypeExpr, AstError{ + var align_expr: ?*Node = null; + var type_expr: ?*Node = null; + if (eatToken(it, .Colon)) |_| { + type_expr = try expectNode(arena, it, tree, parseTypeExpr, AstError{ .ExpectedTypeExpr = AstError.ExpectedTypeExpr{ .token = it.index }, - }) - else - null; + }); + align_expr = try parseByteAlign(arena, it, tree); + } const value_expr = if (eatToken(it, .Equal)) |_| try expectNode(arena, it, tree, parseExpr, AstError{ @@ -406,6 +408,7 @@ fn parseContainerField(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No .name_token = name_token, .type_expr = type_expr, .value_expr = value_expr, + .align_expr = align_expr, }; return &node.base; } diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 871195280f..c601e6e8bf 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -166,6 +166,15 @@ test "zig fmt: doc comments on param decl" { ); } +test "zig fmt: aligned struct field" { + try testCanonical( + \\pub const S = struct { + \\ f: i32 align(32), + \\}; + \\ + ); +} + test "zig fmt: preserve space between async fn definitions" { try testCanonical( \\async fn a() void {} @@ -201,6 +210,103 @@ test "zig fmt: comment to disable/enable zig fmt" { ); } +test "zig fmt: line comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\// Test + \\const e = f; + ); +} + +test "zig fmt: doc comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\/// test + \\const e = f; + ); +} + +test "zig fmt: line and doc comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\// test 1 + \\/// test 2 + \\const e = f; + ); +} + +test "zig fmt: doc and line comment following 'zig fmt: off'" { + try testCanonical( + \\// zig fmt: off + \\/// test 1 + \\// test 2 + \\const e = f; + ); +} + +test "zig fmt: alternating 'zig fmt: off' and 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\// zig fmt: on + \\// zig fmt: off + \\const e = f; + \\// zig fmt: off + \\// zig fmt: on + \\// zig fmt: off + \\const a = b; + \\// zig fmt: on + \\const c = d; + \\// zig fmt: on + \\ + ); +} + +test "zig fmt: line comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\// test + \\const e = f; + \\ + ); +} + +test "zig fmt: doc comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\/// test + \\const e = f; + \\ + ); +} + +test "zig fmt: line and doc comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\// test1 + \\/// test2 + \\const e = f; + \\ + ); +} + +test "zig fmt: doc and line comment following 'zig fmt: on'" { + try testCanonical( + \\// zig fmt: off + \\const e = f; + \\// zig fmt: on + \\/// test1 + \\// test2 + \\const e = f; + \\ + ); +} + test "zig fmt: pointer of unknown length" { try testCanonical( \\fn foo(ptr: [*]u8) void {} @@ -2269,7 +2375,6 @@ test "zig fmt: if type expr" { \\ ); } - test "zig fmt: file ends with struct field" { try testTransform( \\a: bool @@ -2279,6 +2384,20 @@ test "zig fmt: file ends with struct field" { ); } +test "zig fmt: comment after empty comment" { + try testTransform( + \\const x = true; // + \\// + \\// + \\//a + \\ + , + \\const x = true; + \\//a + \\ + ); +} + test "zig fmt: comments at several places in struct init" { try testTransform( \\var bar = Bar{ diff --git a/std/zig/render.zig b/std/zig/render.zig index 035526ed11..5695e8801f 100644 --- a/std/zig/render.zig +++ b/std/zig/render.zig @@ -89,41 +89,98 @@ fn renderRoot( var it = tree.root_node.decls.iterator(0); while (true) { var decl = (it.next() orelse return).*; - // look for zig fmt: off comment - var start_token_index = decl.firstToken(); - zig_fmt_loop: while (start_token_index != 0) { - start_token_index -= 1; - const start_token = tree.tokens.at(start_token_index); - switch (start_token.id) { + + // This loop does the following: + // + // - Iterates through line/doc comment tokens that precedes the current + // decl. + // - Figures out the first token index (`copy_start_token_index`) which + // hasn't been copied to the output stream yet. + // - Detects `zig fmt: (off|on)` in the line comment tokens, and + // determines whether the current decl should be reformatted or not. + // + var token_index = decl.firstToken(); + var fmt_active = true; + var found_fmt_directive = false; + + var copy_start_token_index = token_index; + + while (token_index != 0) { + token_index -= 1; + const token = tree.tokens.at(token_index); + switch (token.id) { Token.Id.LineComment => {}, - Token.Id.DocComment => continue, + Token.Id.DocComment => { + copy_start_token_index = token_index; + continue; + }, else => break, } - if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(start_token)[2..], " "), "zig fmt: off")) { - var end_token_index = start_token_index; - while (true) { - end_token_index += 1; - const end_token = tree.tokens.at(end_token_index); - switch (end_token.id) { + + if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(token)[2..], " "), "zig fmt: off")) { + if (!found_fmt_directive) { + fmt_active = false; + found_fmt_directive = true; + } + } else if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(token)[2..], " "), "zig fmt: on")) { + if (!found_fmt_directive) { + fmt_active = true; + found_fmt_directive = true; + } + } + } + + if (!fmt_active) { + // Reformatting is disabled for the current decl and possibly some + // more decls that follow. + // Find the next `decl` for which reformatting is re-enabled. + token_index = decl.firstToken(); + + while (!fmt_active) { + decl = (it.next() orelse { + // If there's no next reformatted `decl`, just copy the + // remaining input tokens and bail out. + const start = tree.tokens.at(copy_start_token_index).start; + try copyFixingWhitespace(stream, tree.source[start..]); + return; + }).*; + var decl_first_token_index = decl.firstToken(); + + while (token_index < decl_first_token_index) : (token_index += 1) { + const token = tree.tokens.at(token_index); + switch (token.id) { Token.Id.LineComment => {}, - Token.Id.Eof => { - const start = tree.tokens.at(start_token_index + 1).start; - try copyFixingWhitespace(stream, tree.source[start..]); - return; - }, + Token.Id.Eof => unreachable, else => continue, } - if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(end_token)[2..], " "), "zig fmt: on")) { - const start = tree.tokens.at(start_token_index + 1).start; - try copyFixingWhitespace(stream, tree.source[start..end_token.end]); - try stream.writeByte('\n'); - while (tree.tokens.at(decl.firstToken()).start < end_token.end) { - decl = (it.next() orelse return).*; - } - break :zig_fmt_loop; + if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(token)[2..], " "), "zig fmt: on")) { + fmt_active = true; + } else if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(token)[2..], " "), "zig fmt: off")) { + fmt_active = false; } } } + + // Found the next `decl` for which reformatting is enabled. Copy + // the input tokens before the `decl` that haven't been copied yet. + var copy_end_token_index = decl.firstToken(); + token_index = copy_end_token_index; + while (token_index != 0) { + token_index -= 1; + const token = tree.tokens.at(token_index); + switch (token.id) { + Token.Id.LineComment => {}, + Token.Id.DocComment => { + copy_end_token_index = token_index; + continue; + }, + else => break, + } + } + + const start = tree.tokens.at(copy_start_token_index).start; + const end = tree.tokens.at(copy_end_token_index).start; + try copyFixingWhitespace(stream, tree.source[start..end]); } try renderTopLevelDecl(allocator, stream, tree, 0, &start_col, decl); @@ -206,7 +263,20 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i } else if (field.type_expr != null and field.value_expr == null) { try renderToken(tree, stream, field.name_token, indent, start_col, Space.None); // name try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // : - return renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type, + + if (field.align_expr) |align_value_expr| { + try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Space); // type + const lparen_token = tree.prevToken(align_value_expr.firstToken()); + const align_kw = tree.prevToken(lparen_token); + const rparen_token = tree.nextToken(align_value_expr.lastToken()); + try renderToken(tree, stream, align_kw, indent, start_col, Space.None); // align + try renderToken(tree, stream, lparen_token, indent, start_col, Space.None); // ( + try renderExpression(allocator, stream, tree, indent, start_col, align_value_expr, Space.None); // alignment + try renderToken(tree, stream, rparen_token, indent, start_col, Space.Comma); // ) + } else { + try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type, + } + } else if (field.type_expr == null and field.value_expr != null) { try renderToken(tree, stream, field.name_token, indent, start_col, Space.Space); // name try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // = @@ -1924,15 +1994,24 @@ fn renderTokenOffset( } } - const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2; - if (comment_is_empty) { - switch (space) { - Space.Newline => { - try stream.writeByte('\n'); - start_col.* = 0; - return; - }, - else => {}, + while (true) { + const comment_is_empty = mem.trimRight(u8, tree.tokenSlicePtr(next_token), " ").len == 2; + if (comment_is_empty) { + switch (space) { + Space.Newline => { + offset += 1; + token = next_token; + next_token = tree.tokens.at(token_index + offset); + if (next_token.id != .LineComment) { + try stream.writeByte('\n'); + start_col.* = 0; + return; + } + }, + else => break, + } + } else { + break; } } diff --git a/test/compile_errors.zig b/test/compile_errors.zig index a6a1d0219b..812b236716 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,51 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "struct depends on itself via optional field", + \\const LhsExpr = struct { + \\ rhsExpr: ?AstObject, + \\}; + \\const AstObject = union { + \\ lhsExpr: LhsExpr, + \\}; + \\export fn entry() void { + \\ const lhsExpr = LhsExpr{ .rhsExpr = null }; + \\ const obj = AstObject{ .lhsExpr = lhsExpr }; + \\} + , + "tmp.zig:1:17: error: struct 'LhsExpr' depends on itself", + "tmp.zig:5:5: note: while checking this field", + "tmp.zig:2:5: note: while checking this field", + ); + + cases.add( + "alignment of enum field specified", + \\const Number = enum { + \\ a, + \\ b align(i32), + \\}; + \\export fn entry1() void { + \\ var x: Number = undefined; + \\} + , + "tmp.zig:3:13: error: structs and unions, not enums, support field alignment", + "tmp.zig:1:16: note: consider 'union(enum)' here", + ); + + cases.add( + "bad alignment type", + \\export fn entry1() void { + \\ var x: []align(true) i32 = undefined; + \\} + \\export fn entry2() void { + \\ var x: *align(f64(12.34)) i32 = undefined; + \\} + , + "tmp.zig:2:20: error: expected type 'u29', found 'bool'", + "tmp.zig:5:22: error: fractional component prevents float value 12.340000 from being casted to type 'u29'", + ); + cases.addCase(x: { var tc = cases.create("variable in inline assembly template cannot be found", \\export fn entry() void { @@ -10,7 +55,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ : [bar] "=r" (-> usize) \\ ); \\} - , "tmp.zig:2:14: error: could not find 'foo' in the inputs or outputs."); + , "tmp.zig:2:14: error: could not find 'foo' in the inputs or outputs"); tc.target = tests.Target{ .Cross = tests.CrossTarget{ .arch = .x86_64, @@ -53,8 +98,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:8:1: error: '@Frame(rangeSum)' depends on itself", - "tmp.zig:15:33: note: when analyzing type '@Frame(rangeSumIndirect)' here", - "tmp.zig:26:25: note: when analyzing type '@Frame(rangeSum)' here", + "tmp.zig:15:33: note: when analyzing type '@Frame(rangeSum)' here", + "tmp.zig:26:25: note: when analyzing type '@Frame(rangeSumIndirect)' here", ); cases.add( @@ -245,7 +290,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { , "tmp.zig:4:1: error: unable to determine async function frame of 'amain'", "tmp.zig:5:10: note: analysis of function 'other' depends on the frame", - "tmp.zig:8:13: note: depends on the frame here", + "tmp.zig:8:13: note: referenced here", ); cases.add( @@ -258,7 +303,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:4:1: error: cannot resolve '@Frame(amain)': function not fully analyzed yet", - "tmp.zig:5:13: note: depends on its own frame here", + "tmp.zig:5:13: note: referenced here", ); cases.add( @@ -404,7 +449,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ const foo: Foo = undefined; \\} , - "tmp.zig:2:8: error: expected type 'type', found '(undefined)'", + "tmp.zig:2:8: error: use of undefined value here causes undefined behavior", ); cases.add( @@ -470,7 +515,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ); cases.add( - "Generic function where return type is self-referenced", + "generic function where return type is self-referenced", \\fn Foo(comptime T: type) Foo(T) { \\ return struct{ x: T }; \\} @@ -481,7 +526,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:1:29: error: evaluation exceeded 1000 backwards branches", - "tmp.zig:1:29: note: called from here", + "tmp.zig:5:18: note: referenced here", ); cases.add( @@ -645,7 +690,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\const A = struct { a : A, }; \\export fn entry() usize { return @sizeOf(A); } , - "tmp.zig:1:11: error: struct 'A' contains itself", + "tmp.zig:1:11: error: struct 'A' depends on itself", ); cases.add( @@ -655,7 +700,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\const C = struct { a : A, }; \\export fn entry() usize { return @sizeOf(A); } , - "tmp.zig:1:11: error: struct 'A' contains itself", + "tmp.zig:1:11: error: struct 'A' depends on itself", ); cases.add( @@ -670,7 +715,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ return @sizeOf(@typeOf(foo.x)); \\} , - "tmp.zig:1:13: error: struct 'Foo' contains itself", + "tmp.zig:1:13: error: struct 'Foo' depends on itself", "tmp.zig:8:28: note: referenced here", ); @@ -689,7 +734,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:7:9: error: dependency loop detected", - "tmp.zig:2:19: note: called from here", + "tmp.zig:2:19: note: referenced here", "tmp.zig:10:21: note: referenced here", ); @@ -703,7 +748,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var s: Foo = Foo.E; \\} , - "tmp.zig:1:17: error: 'Foo' depends on itself", + "tmp.zig:1:17: error: enum 'Foo' depends on itself", ); cases.add( @@ -866,7 +911,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { break :x tc; }); - cases.addTest( + cases.add( "export generic function", \\export fn foo(num: var) i32 { \\ return 0; @@ -875,17 +920,17 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:1:15: error: parameter of type 'var' not allowed in function with calling convention 'ccc'", ); - cases.addTest( + cases.add( "C pointer to c_void", \\export fn a() void { \\ var x: *c_void = undefined; \\ var y: [*c]c_void = x; \\} , - "tmp.zig:3:12: error: C pointers cannot point opaque types", + "tmp.zig:3:16: error: C pointers cannot point opaque types", ); - cases.addTest( + cases.add( "directly embedding opaque type in struct and union", \\const O = @OpaqueType(); \\const Foo = struct { @@ -906,7 +951,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:7:10: error: opaque types have unknown size and therefore cannot be directly embedded in unions", ); - cases.addTest( + cases.add( "implicit cast between C pointer and Zig pointer - bad const/align/child", \\export fn a() void { \\ var x: [*c]u8 = undefined; @@ -942,7 +987,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:23:22: error: expected type '[*c]u32', found '*u8'", ); - cases.addTest( + cases.add( "implicit casting null c pointer to zig pointer", \\comptime { \\ var c_ptr: [*c]u8 = 0; @@ -952,7 +997,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:24: error: null pointer casted to type '*u8'", ); - cases.addTest( + cases.add( "implicit casting undefined c pointer to zig pointer", \\comptime { \\ var c_ptr: [*c]u8 = undefined; @@ -962,7 +1007,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:24: error: use of undefined value here causes undefined behavior", ); - cases.addTest( + cases.add( "implicit casting C pointers which would mess up null semantics", \\export fn entry() void { \\ var slice: []const u8 = "aoeu"; @@ -987,7 +1032,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:13:35: note: mutable '[*c]const u8' allows illegal null values stored to type '[*]u8'", ); - cases.addTest( + cases.add( "implicit casting too big integers to C pointers", \\export fn a() void { \\ var ptr: [*c]u8 = (1 << 64) + 1; @@ -1001,14 +1046,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:6:23: error: integer type 'u65' too big for implicit @intToPtr to type '[*c]u8'", ); - cases.addTest( + cases.add( "C pointer pointing to non C ABI compatible type or has align attr", \\const Foo = struct {}; \\export fn a() void { \\ const T = [*c]Foo; \\} , - "tmp.zig:3:15: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'", + "tmp.zig:3:19: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'", ); cases.addCase(x: { @@ -1029,7 +1074,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { break :x tc; }); - cases.addTest( + cases.add( "assign to invalid dereference", \\export fn entry() void { \\ 'a'.* = 1; @@ -1038,7 +1083,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:8: error: attempt to dereference non-pointer type 'comptime_int'", ); - cases.addTest( + cases.add( "take slice of invalid dereference", \\export fn entry() void { \\ const x = 'a'.*[0..]; @@ -1047,7 +1092,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:18: error: attempt to dereference non-pointer type 'comptime_int'", ); - cases.addTest( + cases.add( "@truncate undefined value", \\export fn entry() void { \\ var z = @truncate(u8, u16(undefined)); @@ -1091,7 +1136,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ return 5678; \\} , - "tmp.zig:2:12: error: `&&` is invalid. Note that `and` is boolean AND.", + "tmp.zig:2:12: error: `&&` is invalid. Note that `and` is boolean AND", ); cases.add( @@ -1935,7 +1980,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "unknown length pointer to opaque", \\export const T = [*]@OpaqueType(); , - "tmp.zig:1:18: error: unknown-length pointer to opaque", + "tmp.zig:1:21: error: unknown-length pointer to opaque", ); cases.add( @@ -2924,7 +2969,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\fn a() *noreturn {} \\export fn entry() void { _ = a(); } , - "tmp.zig:1:8: error: pointer to noreturn not allowed", + "tmp.zig:1:9: error: pointer to noreturn not allowed", ); cases.add( @@ -3596,7 +3641,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ); cases.add( - "non constant expression in array size outside function", + "non constant expression in array size", \\const Foo = struct { \\ y: [get()]u8, \\}; @@ -3606,8 +3651,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(Foo)); } , "tmp.zig:5:25: error: unable to evaluate constant expression", - "tmp.zig:2:12: note: called from here", - "tmp.zig:2:8: note: called from here", + "tmp.zig:2:12: note: referenced here", ); cases.add( @@ -3701,7 +3745,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(y)); } , "tmp.zig:3:14: error: division by zero", - "tmp.zig:1:14: note: called from here", + "tmp.zig:1:14: note: referenced here", ); cases.add( @@ -4133,7 +4177,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(seventh_fib_number)); } , "tmp.zig:3:21: error: evaluation exceeded 1000 backwards branches", - "tmp.zig:3:21: note: called from here", + "tmp.zig:1:37: note: referenced here", + "tmp.zig:6:50: note: referenced here", ); cases.add( @@ -4174,7 +4219,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(a)); } , "tmp.zig:6:26: error: unable to evaluate constant expression", - "tmp.zig:4:17: note: called from here", + "tmp.zig:4:17: note: referenced here", ); cases.add( @@ -4257,7 +4302,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(y)); } , "tmp.zig:3:12: error: negation caused overflow", - "tmp.zig:1:14: note: called from here", + "tmp.zig:1:14: note: referenced here", ); cases.add( @@ -4270,7 +4315,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(y)); } , "tmp.zig:3:14: error: operation caused overflow", - "tmp.zig:1:14: note: called from here", + "tmp.zig:1:14: note: referenced here", ); cases.add( @@ -4283,7 +4328,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(y)); } , "tmp.zig:3:14: error: operation caused overflow", - "tmp.zig:1:14: note: called from here", + "tmp.zig:1:14: note: referenced here", ); cases.add( @@ -4296,7 +4341,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\export fn entry() usize { return @sizeOf(@typeOf(y)); } , "tmp.zig:3:14: error: operation caused overflow", - "tmp.zig:1:14: note: called from here", + "tmp.zig:1:14: note: referenced here", ); cases.add( @@ -4388,7 +4433,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:3:7: error: unable to evaluate constant expression", - "tmp.zig:16:19: note: called from here", + "tmp.zig:16:19: note: referenced here", ); cases.add( @@ -4717,7 +4762,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:10:14: error: unable to evaluate constant expression", - "tmp.zig:6:20: note: called from here", + "tmp.zig:6:20: note: referenced here", ); cases.add( @@ -5864,7 +5909,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\} , "tmp.zig:4:25: error: aoeu", - "tmp.zig:1:36: note: called from here", + "tmp.zig:1:36: note: referenced here", "tmp.zig:12:20: note: referenced here", ); @@ -5939,7 +5984,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ var x: MultipleChoice = undefined; \\} , - "tmp.zig:2:14: error: non-enum union field assignment", + "tmp.zig:2:14: error: untagged union field assignment", "tmp.zig:1:24: note: consider 'union(enum)' here", ); diff --git a/test/stage1/behavior.zig b/test/stage1/behavior.zig index 6ec1521048..f7152971a4 100644 --- a/test/stage1/behavior.zig +++ b/test/stage1/behavior.zig @@ -15,6 +15,7 @@ comptime { _ = @import("behavior/bugs/1111.zig"); _ = @import("behavior/bugs/1120.zig"); _ = @import("behavior/bugs/1277.zig"); + _ = @import("behavior/bugs/1310.zig"); _ = @import("behavior/bugs/1322.zig"); _ = @import("behavior/bugs/1381.zig"); _ = @import("behavior/bugs/1421.zig"); @@ -22,15 +23,18 @@ comptime { _ = @import("behavior/bugs/1486.zig"); _ = @import("behavior/bugs/1500.zig"); _ = @import("behavior/bugs/1607.zig"); + _ = @import("behavior/bugs/1735.zig"); _ = @import("behavior/bugs/1851.zig"); _ = @import("behavior/bugs/1914.zig"); _ = @import("behavior/bugs/2006.zig"); _ = @import("behavior/bugs/2114.zig"); _ = @import("behavior/bugs/2346.zig"); _ = @import("behavior/bugs/2578.zig"); + _ = @import("behavior/bugs/3112.zig"); _ = @import("behavior/bugs/394.zig"); _ = @import("behavior/bugs/421.zig"); _ = @import("behavior/bugs/529.zig"); + _ = @import("behavior/bugs/624.zig"); _ = @import("behavior/bugs/655.zig"); _ = @import("behavior/bugs/656.zig"); _ = @import("behavior/bugs/679.zig"); diff --git a/test/stage1/behavior/align.zig b/test/stage1/behavior/align.zig index f607ac59d2..677ae85b68 100644 --- a/test/stage1/behavior/align.zig +++ b/test/stage1/behavior/align.zig @@ -290,3 +290,18 @@ test "read 128-bit field from default aligned struct in global memory" { expect((@ptrToInt(&default_aligned_global.badguy) % 16) == 0); expect(12 == default_aligned_global.badguy); } + +test "struct field explicit alignment" { + const S = struct { + const Node = struct { + next: *Node, + massive_byte: u8 align(64), + }; + }; + + var node: S.Node = undefined; + node.massive_byte = 100; + expect(node.massive_byte == 100); + comptime expect(@typeOf(&node.massive_byte) == *align(64) u8); + expect(@ptrToInt(&node.massive_byte) % 64 == 0); +} diff --git a/test/stage1/behavior/array.zig b/test/stage1/behavior/array.zig index 9349af573c..462977066e 100644 --- a/test/stage1/behavior/array.zig +++ b/test/stage1/behavior/array.zig @@ -292,3 +292,9 @@ test "read/write through global variable array of struct fields initialized via }; S.doTheTest(); } + +test "implicit cast zero sized array ptr to slice" { + var b = ""; + const c: []const u8 = &b; + expect(c.len == 0); +} diff --git a/test/stage1/behavior/bugs/1310.zig b/test/stage1/behavior/bugs/1310.zig new file mode 100644 index 0000000000..f0f696e4bc --- /dev/null +++ b/test/stage1/behavior/bugs/1310.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const expect = std.testing.expect; + +pub const VM = ?[*]const struct_InvocationTable_; +pub const struct_InvocationTable_ = extern struct { + GetVM: ?extern fn (?[*]VM) c_int, +}; + +pub const struct_VM_ = extern struct { + functions: ?[*]const struct_InvocationTable_, +}; + +//excised output from stdlib.h etc + +pub const InvocationTable_ = struct_InvocationTable_; +pub const VM_ = struct_VM_; + +extern fn agent_callback(_vm: [*]VM, options: [*]u8) i32 { + return 11; +} + +test "fixed" { + expect(agent_callback(undefined, undefined) == 11); +} diff --git a/test/stage1/behavior/bugs/1735.zig b/test/stage1/behavior/bugs/1735.zig new file mode 100644 index 0000000000..e757b5157c --- /dev/null +++ b/test/stage1/behavior/bugs/1735.zig @@ -0,0 +1,46 @@ +const std = @import("std"); + +const mystruct = struct { + pending: ?listofstructs, +}; +pub fn TailQueue(comptime T: type) type { + return struct { + const Self = @This(); + + pub const Node = struct { + prev: ?*Node, + next: ?*Node, + data: T, + }; + + first: ?*Node, + last: ?*Node, + len: usize, + + pub fn init() Self { + return Self{ + .first = null, + .last = null, + .len = 0, + }; + } + }; +} +const listofstructs = TailQueue(mystruct); + +const a = struct { + const Self = @This(); + + foo: listofstructs, + + pub fn init() Self { + return Self{ + .foo = listofstructs.init(), + }; + } +}; + +test "intialization" { + var t = a.init(); + std.testing.expect(t.foo.len == 0); +} diff --git a/test/stage1/behavior/bugs/3112.zig b/test/stage1/behavior/bugs/3112.zig new file mode 100644 index 0000000000..1dfe359d4a --- /dev/null +++ b/test/stage1/behavior/bugs/3112.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const expect = std.testing.expect; + +const State = struct { + const Self = @This(); + enter: fn (previous: ?Self) void, +}; + +fn prev(p: ?State) void { + expect(p == null); +} + +test "zig test crash" { + var global: State = undefined; + global.enter = prev; + global.enter(null); +} diff --git a/test/stage1/behavior/bugs/624.zig b/test/stage1/behavior/bugs/624.zig new file mode 100644 index 0000000000..1eee889a0d --- /dev/null +++ b/test/stage1/behavior/bugs/624.zig @@ -0,0 +1,23 @@ +const std = @import("std"); +const expect = std.testing.expect; + +const TestContext = struct { + server_context: *ListenerContext, +}; + +const ListenerContext = struct { + context_alloc: *ContextAllocator, +}; + +const ContextAllocator = MemoryPool(TestContext); + +fn MemoryPool(comptime T: type) type { + return struct { + n: usize, + }; +} + +test "foo" { + var allocator = ContextAllocator{ .n = 10 }; + expect(allocator.n == 10); +} diff --git a/test/stage1/behavior/error.zig b/test/stage1/behavior/error.zig index 264f140c9d..fefd95a850 100644 --- a/test/stage1/behavior/error.zig +++ b/test/stage1/behavior/error.zig @@ -375,3 +375,23 @@ test "implicit cast to optional to error union to return result loc" { S.entry(); //comptime S.entry(); TODO } + +test "function pointer with return type that is error union with payload which is pointer of parent struct" { + const S = struct { + const Foo = struct { + fun: fn (a: i32) (anyerror!*Foo), + }; + + const Err = error{UnspecifiedErr}; + + fn bar(a: i32) anyerror!*Foo { + return Err.UnspecifiedErr; + } + + fn doTheTest() void { + var x = Foo{ .fun = bar }; + expectError(error.UnspecifiedErr, x.fun(1)); + } + }; + S.doTheTest(); +} diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index ab58f2ed08..c122b18e0a 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -706,3 +706,18 @@ test "result location zero sized array inside struct field implicit cast to slic var foo = E{ .entries = [_]u32{} }; expect(foo.entries.len == 0); } + +var global_foo: *i32 = undefined; + +test "global variable assignment with optional unwrapping with var initialized to undefined" { + const S = struct { + var data: i32 = 1234; + fn foo() ?*i32 { + return &data; + } + }; + global_foo = S.foo() orelse { + @panic("bad"); + }; + expect(global_foo.* == 1234); +} diff --git a/test/stage1/behavior/optional.zig b/test/stage1/behavior/optional.zig index ee3cb4aef9..8cc90d10a4 100644 --- a/test/stage1/behavior/optional.zig +++ b/test/stage1/behavior/optional.zig @@ -100,3 +100,22 @@ test "nested orelse" { S.entry(); comptime S.entry(); } + +test "self-referential struct through a slice of optional" { + const S = struct { + const Node = struct { + children: []?Node, + data: ?u8, + + fn new() Node { + return Node{ + .children = undefined, + .data = null, + }; + } + }; + }; + + var n = S.Node.new(); + expect(n.data == null); +} diff --git a/tools/process_headers.zig b/tools/process_headers.zig index 805fc3d5f7..7bd7ca08d2 100644 --- a/tools/process_headers.zig +++ b/tools/process_headers.zig @@ -504,12 +504,9 @@ const Contents = struct { } }; -comptime { - @compileError("the behavior of std.AutoHashMap changed and []const u8 will be treated as a pointer. will need to update the hash maps to actually do some kind of hashing on the slices."); -} -const HashToContents = std.AutoHashMap([]const u8, Contents); +const HashToContents = std.StringHashMap(Contents); const TargetToHash = std.HashMap(DestTarget, []const u8, DestTarget.hash, DestTarget.eql); -const PathTable = std.AutoHashMap([]const u8, *TargetToHash); +const PathTable = std.StringHashMap(*TargetToHash); const LibCVendor = enum { musl, diff --git a/tools/update_glibc.zig b/tools/update_glibc.zig index 9928183b7b..1bca274124 100644 --- a/tools/update_glibc.zig +++ b/tools/update_glibc.zig @@ -118,7 +118,7 @@ const FunctionSet = struct { list: std.ArrayList(VersionedFn), fn_vers_list: FnVersionList, }; -const FnVersionList = std.AutoHashMap([]const u8, std.ArrayList(usize)); +const FnVersionList = std.StringHashMap(std.ArrayList(usize)); const VersionedFn = struct { ver: []const u8, // example: "GLIBC_2.15" @@ -140,8 +140,8 @@ pub fn main() !void { const prefix = try fs.path.join(allocator, [_][]const u8{ in_glibc_dir, "sysdeps", "unix", "sysv", "linux" }); const glibc_out_dir = try fs.path.join(allocator, [_][]const u8{ zig_src_dir, "libc", "glibc" }); - var global_fn_set = std.AutoHashMap([]const u8, Function).init(allocator); - var global_ver_set = std.AutoHashMap([]const u8, usize).init(allocator); + var global_fn_set = std.StringHashMap(Function).init(allocator); + var global_ver_set = std.StringHashMap(usize).init(allocator); var target_functions = std.AutoHashMap(usize, FunctionSet).init(allocator); for (abi_lists) |*abi_list| {