authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-14 18:56:30-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-14 19:08:59-04:00
log3f776af3fa58b47d15a81b17c15e5b0e1c5ccf28
treebdcbd8dcf83e452a6ef10de8e74899494fa4c21c
parent639c3811288b65173b3d9706b8e2001ee2419233
signaturelock-open Commit is signed but in an unrecognized format.

fix alignment of structs

closes #1248 closes #1052 closes #1154

8 files changed, 524 insertions(+), 324 deletions(-)

src/all_types.hpp+25-22
......@@ -1012,13 +1012,13 @@ enum PtrLen {
10121012
10131013struct ZigTypePointer {
10141014 ZigType *child_type;
1015 ZigType *slice_parent;
10151016 PtrLen ptr_len;
1016 bool is_const;
1017 bool is_volatile;
1018 uint32_t alignment;
1017 uint32_t explicit_alignment; // 0 means use ABI alignment
10191018 uint32_t bit_offset;
10201019 uint32_t unaligned_bit_count;
1021 ZigType *slice_parent;
1020 bool is_const;
1021 bool is_volatile;
10221022};
10231023
10241024struct ZigTypeInt {
......@@ -1046,32 +1046,35 @@ struct TypeStructField {
10461046 size_t unaligned_bit_count;
10471047 AstNode *decl_node;
10481048};
1049
1050enum ResolveStatus {
1051 ResolveStatusUnstarted,
1052 ResolveStatusInvalid,
1053 ResolveStatusZeroBitsKnown,
1054 ResolveStatusAlignmentKnown,
1055 ResolveStatusSizeKnown,
1056};
1057
10491058struct ZigTypeStruct {
10501059 AstNode *decl_node;
1051 ContainerLayout layout;
1052 uint32_t src_field_count;
1053 uint32_t gen_field_count;
10541060 TypeStructField *fields;
1055 uint64_t size_bytes;
1056 bool is_invalid; // true if any fields are invalid
1057 bool is_slice;
10581061 ScopeDecls *decls_scope;
1062 uint64_t size_bytes;
1063 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
10591064
1060 // set this flag temporarily to detect infinite loops
1061 bool embedded_in_current;
1062 bool reported_infinite_err;
1063 // whether we've finished resolving it
1064 bool complete;
1065 uint32_t src_field_count;
1066 uint32_t gen_field_count;
1067
1068 uint32_t abi_alignment; // known after ResolveStatusAlignmentKnown
1069 ContainerLayout layout;
1070 ResolveStatus resolve_status;
10651071
1072 bool is_slice;
1073 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1074 bool reported_infinite_err;
10661075 // whether any of the fields require comptime
1067 // the value is not valid until zero_bits_known == true
1076 // known after ResolveStatusZeroBitsKnown
10681077 bool requires_comptime;
1069
1070 bool zero_bits_loop_flag;
1071 bool zero_bits_known;
1072 uint32_t abi_alignment; // also figured out with zero_bits pass
1073
1074 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
10751078};
10761079
10771080struct ZigTypeOptional {
src/analyze.cpp+218-169
......@@ -23,6 +23,7 @@ static Error resolve_enum_type(CodeGen *g, ZigType *enum_type);
2323static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);
2424
2525static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);
26static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);
2627static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type);
2728static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);
2829static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
......@@ -254,18 +255,42 @@ AstNode *type_decl_node(ZigType *type_entry) {
254255 zig_unreachable();
255256}
256257
257bool type_is_complete(ZigType *type_entry) {
258bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
258259 switch (type_entry->id) {
259260 case ZigTypeIdInvalid:
260261 zig_unreachable();
261262 case ZigTypeIdStruct:
262 return type_entry->data.structure.complete;
263 return type_entry->data.structure.resolve_status >= status;
263264 case ZigTypeIdEnum:
264 return type_entry->data.enumeration.complete;
265 switch (status) {
266 case ResolveStatusUnstarted:
267 return true;
268 case ResolveStatusInvalid:
269 zig_unreachable();
270 case ResolveStatusZeroBitsKnown:
271 return type_entry->data.enumeration.zero_bits_known;
272 case ResolveStatusAlignmentKnown:
273 return type_entry->data.enumeration.zero_bits_known;
274 case ResolveStatusSizeKnown:
275 return type_entry->data.enumeration.complete;
276 }
277 zig_unreachable();
265278 case ZigTypeIdUnion:
266 return type_entry->data.unionation.complete;
279 switch (status) {
280 case ResolveStatusUnstarted:
281 return true;
282 case ResolveStatusInvalid:
283 zig_unreachable();
284 case ResolveStatusZeroBitsKnown:
285 return type_entry->data.unionation.zero_bits_known;
286 case ResolveStatusAlignmentKnown:
287 return type_entry->data.unionation.zero_bits_known;
288 case ResolveStatusSizeKnown:
289 return type_entry->data.unionation.complete;
290 }
291 zig_unreachable();
267292 case ZigTypeIdOpaque:
268 return false;
293 return status < ResolveStatusSizeKnown;
269294 case ZigTypeIdMetaType:
270295 case ZigTypeIdVoid:
271296 case ZigTypeIdBool:
......@@ -291,43 +316,10 @@ bool type_is_complete(ZigType *type_entry) {
291316 zig_unreachable();
292317}
293318
294bool type_has_zero_bits_known(ZigType *type_entry) {
295 switch (type_entry->id) {
296 case ZigTypeIdInvalid:
297 zig_unreachable();
298 case ZigTypeIdStruct:
299 return type_entry->data.structure.zero_bits_known;
300 case ZigTypeIdEnum:
301 return type_entry->data.enumeration.zero_bits_known;
302 case ZigTypeIdUnion:
303 return type_entry->data.unionation.zero_bits_known;
304 case ZigTypeIdMetaType:
305 case ZigTypeIdVoid:
306 case ZigTypeIdBool:
307 case ZigTypeIdUnreachable:
308 case ZigTypeIdInt:
309 case ZigTypeIdFloat:
310 case ZigTypeIdPointer:
311 case ZigTypeIdArray:
312 case ZigTypeIdComptimeFloat:
313 case ZigTypeIdComptimeInt:
314 case ZigTypeIdUndefined:
315 case ZigTypeIdNull:
316 case ZigTypeIdOptional:
317 case ZigTypeIdErrorUnion:
318 case ZigTypeIdErrorSet:
319 case ZigTypeIdFn:
320 case ZigTypeIdNamespace:
321 case ZigTypeIdBoundFn:
322 case ZigTypeIdArgTuple:
323 case ZigTypeIdOpaque:
324 case ZigTypeIdPromise:
325 return true;
326 }
327 zig_unreachable();
319bool type_is_complete(ZigType *type_entry) {
320 return type_is_resolved(type_entry, ResolveStatusSizeKnown);
328321}
329322
330
331323uint64_t type_size(CodeGen *g, ZigType *type_entry) {
332324 assert(type_is_complete(type_entry));
333325
......@@ -376,7 +368,7 @@ uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {
376368
377369Result<bool> type_is_copyable(CodeGen *g, ZigType *type_entry) {
378370 Error err;
379 if ((err = type_ensure_zero_bits_known(g, type_entry)))
371 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
380372 return err;
381373
382374 if (!type_has_bits(type_entry))
......@@ -431,10 +423,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
431423 assert(!type_is_invalid(child_type));
432424 assert(ptr_len == PtrLenSingle || child_type->id != ZigTypeIdOpaque);
433425
426 if (byte_alignment != 0) {
427 uint32_t abi_alignment = get_abi_alignment(g, child_type);
428 if (byte_alignment == abi_alignment)
429 byte_alignment = 0;
430 }
431
434432 TypeId type_id = {};
435433 ZigType **parent_pointer = nullptr;
436 uint32_t abi_alignment = get_abi_alignment(g, child_type);
437 if (unaligned_bit_count != 0 || is_volatile || byte_alignment != abi_alignment || ptr_len != PtrLenSingle) {
434 if (unaligned_bit_count != 0 || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle) {
438435 type_id.id = ZigTypeIdPointer;
439436 type_id.data.pointer.child_type = child_type;
440437 type_id.data.pointer.is_const = is_const;
......@@ -451,12 +448,12 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
451448 assert(bit_offset == 0);
452449 parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)];
453450 if (*parent_pointer) {
454 assert((*parent_pointer)->data.pointer.alignment == byte_alignment);
451 assert((*parent_pointer)->data.pointer.explicit_alignment == 0);
455452 return *parent_pointer;
456453 }
457454 }
458455
459 assertNoError(type_ensure_zero_bits_known(g, child_type));
456 assert(type_is_resolved(child_type, ResolveStatusZeroBitsKnown));
460457
461458 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);
462459 entry->is_copyable = true;
......@@ -465,11 +462,14 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
465462 const char *const_str = is_const ? "const " : "";
466463 const char *volatile_str = is_volatile ? "volatile " : "";
467464 buf_resize(&entry->name, 0);
468 if (unaligned_bit_count == 0 && byte_alignment == abi_alignment) {
465 if (unaligned_bit_count == 0 && byte_alignment == 0) {
469466 buf_appendf(&entry->name, "%s%s%s%s", star_str, const_str, volatile_str, buf_ptr(&child_type->name));
470467 } else if (unaligned_bit_count == 0) {
471468 buf_appendf(&entry->name, "%salign(%" PRIu32 ") %s%s%s", star_str, byte_alignment,
472469 const_str, volatile_str, buf_ptr(&child_type->name));
470 } else if (byte_alignment == 0) {
471 buf_appendf(&entry->name, "%salign(:%" PRIu32 ":%" PRIu32 ") %s%s%s", star_str,
472 bit_offset, bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));
473473 } else {
474474 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s", star_str, byte_alignment,
475475 bit_offset, bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));
......@@ -480,8 +480,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
480480 entry->zero_bits = !type_has_bits(child_type);
481481
482482 if (!entry->zero_bits) {
483 assert(byte_alignment > 0);
484 if (is_const || is_volatile || unaligned_bit_count != 0 || byte_alignment != abi_alignment ||
483 if (is_const || is_volatile || unaligned_bit_count != 0 || byte_alignment != 0 ||
485484 ptr_len != PtrLenSingle)
486485 {
487486 ZigType *peer_type = get_pointer_to_type(g, child_type, false);
......@@ -505,7 +504,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
505504 entry->data.pointer.child_type = child_type;
506505 entry->data.pointer.is_const = is_const;
507506 entry->data.pointer.is_volatile = is_volatile;
508 entry->data.pointer.alignment = byte_alignment;
507 entry->data.pointer.explicit_alignment = byte_alignment;
509508 entry->data.pointer.bit_offset = bit_offset;
510509 entry->data.pointer.unaligned_bit_count = unaligned_bit_count;
511510
......@@ -518,8 +517,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
518517}
519518
520519ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const) {
521 return get_pointer_to_type_extra(g, child_type, is_const, false, PtrLenSingle,
522 get_abi_alignment(g, child_type), 0, 0);
520 return get_pointer_to_type_extra(g, child_type, is_const, false, PtrLenSingle, 0, 0, 0);
523521}
524522
525523ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {
......@@ -800,8 +798,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
800798 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
801799 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);
802800
803 assert(type_has_zero_bits_known(pointer_type->data.pointer.child_type));
804 if (pointer_type->data.pointer.child_type->zero_bits) {
801 if (!type_has_bits(pointer_type->data.pointer.child_type)) {
805802 entry->data.structure.gen_field_count = 1;
806803 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;
807804 entry->data.structure.fields[slice_len_index].gen_index = 0;
......@@ -826,20 +823,18 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
826823 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
827824
828825 ZigType *child_type = ptr_type->data.pointer.child_type;
829 uint32_t abi_alignment = get_abi_alignment(g, child_type);
830826 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
831 ptr_type->data.pointer.alignment != abi_alignment)
827 ptr_type->data.pointer.explicit_alignment != 0)
832828 {
833829 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,
834 PtrLenUnknown, abi_alignment, 0, 0);
830 PtrLenUnknown, 0, 0, 0);
835831 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
836832
837833 slice_type_common_init(g, ptr_type, entry);
838834
839835 entry->type_ref = peer_slice_type->type_ref;
840836 entry->di_type = peer_slice_type->di_type;
841 entry->data.structure.complete = true;
842 entry->data.structure.zero_bits_known = true;
837 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
843838 entry->data.structure.abi_alignment = peer_slice_type->data.structure.abi_alignment;
844839
845840 *parent_pointer = entry;
......@@ -851,15 +846,15 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
851846 if (is_slice(child_type)) {
852847 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
853848 assert(child_ptr_type->id == ZigTypeIdPointer);
854 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
855849 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
856 child_ptr_type->data.pointer.alignment != get_abi_alignment(g, grand_child_type))
850 child_ptr_type->data.pointer.explicit_alignment != 0)
857851 {
852 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
858853 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,
859 PtrLenUnknown, get_abi_alignment(g, grand_child_type), 0, 0);
854 PtrLenUnknown, 0, 0, 0);
860855 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
861856 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,
862 PtrLenUnknown, get_abi_alignment(g, bland_child_slice), 0, 0);
857 PtrLenUnknown, 0, 0, 0);
863858 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
864859
865860 entry->type_ref = peer_slice_type->type_ref;
......@@ -961,8 +956,7 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
961956 }
962957
963958
964 entry->data.structure.complete = true;
965 entry->data.structure.zero_bits_known = true;
959 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
966960
967961 *parent_pointer = entry;
968962 return entry;
......@@ -1367,7 +1361,7 @@ static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_
13671361
13681362static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **out_buffer) {
13691363 ZigType *ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
1370 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0);
1364 PtrLenUnknown, 0, 0, 0);
13711365 ZigType *str_type = get_slice_type(g, ptr_type);
13721366 IrInstruction *instr = analyze_const_value(g, scope, node, str_type, nullptr);
13731367 if (type_is_invalid(instr->value.type))
......@@ -1576,7 +1570,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
15761570 return g->builtin_types.entry_invalid;
15771571 }
15781572 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
1579 if ((err = type_ensure_zero_bits_known(g, type_entry)))
1573 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
15801574 return g->builtin_types.entry_invalid;
15811575 if (!type_has_bits(type_entry)) {
15821576 add_node_error(g, param_node->data.param_decl.type,
......@@ -1624,7 +1618,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
16241618 case ZigTypeIdUnion:
16251619 case ZigTypeIdFn:
16261620 case ZigTypeIdPromise:
1627 if ((err = type_ensure_zero_bits_known(g, type_entry)))
1621 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
16281622 return g->builtin_types.entry_invalid;
16291623 if (type_requires_comptime(type_entry)) {
16301624 add_node_error(g, param_node->data.param_decl.type,
......@@ -1714,7 +1708,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
17141708 case ZigTypeIdUnion:
17151709 case ZigTypeIdFn:
17161710 case ZigTypeIdPromise:
1717 if ((err = type_ensure_zero_bits_known(g, fn_type_id.return_type)))
1711 if ((err = type_resolve(g, fn_type_id.return_type, ResolveStatusZeroBitsKnown)))
17181712 return g->builtin_types.entry_invalid;
17191713 if (type_requires_comptime(fn_type_id.return_type)) {
17201714 return get_generic_fn_type(g, &fn_type_id);
......@@ -1740,7 +1734,7 @@ bool type_is_invalid(ZigType *type_entry) {
17401734 case ZigTypeIdInvalid:
17411735 return true;
17421736 case ZigTypeIdStruct:
1743 return type_entry->data.structure.is_invalid;
1737 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;
17441738 case ZigTypeIdEnum:
17451739 return type_entry->data.enumeration.is_invalid;
17461740 case ZigTypeIdUnion:
......@@ -1855,8 +1849,7 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
18551849
18561850 struct_type->data.structure.src_field_count = field_count;
18571851 struct_type->data.structure.gen_field_count = 0;
1858 struct_type->data.structure.zero_bits_known = true;
1859 struct_type->data.structure.complete = true;
1852 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
18601853 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
18611854 struct_type->data.structure.fields_by_name.init(field_count);
18621855
......@@ -1928,26 +1921,29 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
19281921static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
19291922 assert(struct_type->id == ZigTypeIdStruct);
19301923
1931 if (struct_type->data.structure.complete)
1924 Error err;
1925
1926 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
1927 return ErrorSemanticAnalyzeFail;
1928 if (struct_type->data.structure.resolve_status >= ResolveStatusSizeKnown)
19321929 return ErrorNone;
19331930
1934 Error err;
1935 if ((err = resolve_struct_zero_bits(g, struct_type)))
1931 if ((err = resolve_struct_alignment(g, struct_type)))
19361932 return err;
19371933
19381934 AstNode *decl_node = struct_type->data.structure.decl_node;
19391935
1940 if (struct_type->data.structure.embedded_in_current) {
1941 struct_type->data.structure.is_invalid = true;
1942 if (!struct_type->data.structure.reported_infinite_err) {
1943 struct_type->data.structure.reported_infinite_err = true;
1936 if (struct_type->data.structure.resolve_loop_flag) {
1937 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
1938 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
19441939 add_node_error(g, decl_node,
1945 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));
1940 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));
19461941 }
19471942 return ErrorSemanticAnalyzeFail;
19481943 }
19491944
1950 assert(!struct_type->data.structure.zero_bits_loop_flag);
1945 struct_type->data.structure.resolve_loop_flag = true;
1946
19511947 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
19521948 assert(decl_node->type == NodeTypeContainerDecl);
19531949
......@@ -1956,9 +1952,6 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
19561952 size_t gen_field_count = struct_type->data.structure.gen_field_count;
19571953 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);
19581954
1959 // this field should be set to true only during the recursive calls to resolve_struct_type
1960 struct_type->data.structure.embedded_in_current = true;
1961
19621955 Scope *scope = &struct_type->data.structure.decls_scope->base;
19631956
19641957 size_t gen_field_index = 0;
......@@ -1972,7 +1965,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
19721965 ZigType *field_type = type_struct_field->type_entry;
19731966
19741967 if ((err = ensure_complete_type(g, field_type))) {
1975 struct_type->data.structure.is_invalid = true;
1968 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
19761969 break;
19771970 }
19781971
......@@ -1982,7 +1975,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
19821975 add_node_error(g, field_source_node,
19831976 buf_sprintf("extern structs cannot contain fields of type '%s'",
19841977 buf_ptr(&field_type->name)));
1985 struct_type->data.structure.is_invalid = true;
1978 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
19861979 break;
19871980 }
19881981 }
......@@ -1998,7 +1991,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
19981991 add_node_error(g, field_source_node,
19991992 buf_sprintf("packed structs cannot contain fields of type '%s'",
20001993 buf_ptr(&field_type->name)));
2001 struct_type->data.structure.is_invalid = true;
1994 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
20021995 break;
20031996 }
20041997
......@@ -2049,12 +2042,13 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
20492042 gen_field_index += 1;
20502043 }
20512044
2052 struct_type->data.structure.embedded_in_current = false;
2053 struct_type->data.structure.complete = true;
2045 struct_type->data.structure.resolve_loop_flag = false;
20542046
2055 if (struct_type->data.structure.is_invalid)
2047 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
20562048 return ErrorSemanticAnalyzeFail;
20572049
2050 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
2051
20582052 if (struct_type->zero_bits) {
20592053 struct_type->type_ref = LLVMVoidType();
20602054
......@@ -2116,7 +2110,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
21162110
21172111 assert(field_type->type_ref);
21182112 assert(struct_type->type_ref);
2119 assert(struct_type->data.structure.complete);
2113 assert(struct_type->data.structure.resolve_status == ResolveStatusSizeKnown);
21202114 uint64_t debug_size_in_bits;
21212115 uint64_t debug_align_in_bits;
21222116 uint64_t debug_offset_in_bits;
......@@ -2570,30 +2564,18 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
25702564
25712565 Error err;
25722566
2573 if (struct_type->data.structure.is_invalid)
2567 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
25742568 return ErrorSemanticAnalyzeFail;
2575
2576 if (struct_type->data.structure.zero_bits_known)
2569 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
25772570 return ErrorNone;
25782571
2579 if (struct_type->data.structure.zero_bits_loop_flag) {
2580 // If we get here it's due to recursion. This is a design flaw in the compiler,
2581 // we should be able to still figure out alignment, but here we give up and say that
2582 // the alignment is pointer width, then assert that the first field is within that
2583 // alignment
2584 struct_type->data.structure.zero_bits_known = true;
2585 struct_type->data.structure.zero_bits_loop_flag = false;
2586 if (struct_type->data.structure.abi_alignment == 0) {
2587 if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2588 struct_type->data.structure.abi_alignment = 1;
2589 } else {
2590 struct_type->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, LLVMPointerType(LLVMInt8Type(), 0));
2591 }
2592 }
2572 if (struct_type->data.structure.resolve_loop_flag) {
2573 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
2574 struct_type->data.structure.resolve_loop_flag = false;
25932575 return ErrorNone;
25942576 }
25952577
2596 struct_type->data.structure.zero_bits_loop_flag = true;
2578 struct_type->data.structure.resolve_loop_flag = true;
25972579
25982580 AstNode *decl_node = struct_type->data.structure.decl_node;
25992581 assert(decl_node->type == NodeTypeContainerDecl);
......@@ -2616,7 +2598,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26162598
26172599 if (field_node->data.struct_field.type == nullptr) {
26182600 add_node_error(g, field_node, buf_sprintf("struct field missing type"));
2619 struct_type->data.structure.is_invalid = true;
2601 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
26202602 continue;
26212603 }
26222604
......@@ -2625,7 +2607,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26252607 ErrorMsg *msg = add_node_error(g, field_node,
26262608 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));
26272609 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2628 struct_type->data.structure.is_invalid = true;
2610 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
26292611 continue;
26302612 }
26312613
......@@ -2639,8 +2621,8 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26392621 buf_sprintf("enums, not structs, support field assignment"));
26402622 }
26412623
2642 if ((err = type_ensure_zero_bits_known(g, field_type))) {
2643 struct_type->data.structure.is_invalid = true;
2624 if ((err = type_resolve(g, field_type, ResolveStatusZeroBitsKnown))) {
2625 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
26442626 continue;
26452627 }
26462628
......@@ -2651,36 +2633,87 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26512633 if (!type_has_bits(field_type))
26522634 continue;
26532635
2654 if (gen_field_index == 0) {
2655 if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2656 struct_type->data.structure.abi_alignment = 1;
2657 } else if (struct_type->data.structure.abi_alignment == 0) {
2658 // Alignment of structs is the alignment of the first field, for now.
2659 // TODO change this when we re-order struct fields (issue #168)
2660 struct_type->data.structure.abi_alignment = get_abi_alignment(g, field_type);
2661 assert(struct_type->data.structure.abi_alignment != 0);
2662 } else {
2663 // due to a design flaw in the compiler we assumed that alignment was
2664 // pointer width, so we assert that this wasn't violated.
2665 if (get_abi_alignment(g, field_type) > struct_type->data.structure.abi_alignment) {
2666 zig_panic("compiler design flaw: incorrect alignment assumption");
2667 }
2668 }
2669 }
2670
26712636 type_struct_field->gen_index = gen_field_index;
26722637 gen_field_index += 1;
26732638 }
26742639
2675 struct_type->data.structure.zero_bits_loop_flag = false;
2640 struct_type->data.structure.resolve_loop_flag = false;
26762641 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
26772642 struct_type->zero_bits = (gen_field_index == 0);
2678 struct_type->data.structure.zero_bits_known = true;
26792643
2680 if (struct_type->data.structure.is_invalid) {
2644 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2645 return ErrorSemanticAnalyzeFail;
2646
2647 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
2648 return ErrorNone;
2649}
2650
2651static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2652 assert(struct_type->id == ZigTypeIdStruct);
2653
2654 Error err;
2655
2656 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2657 return ErrorSemanticAnalyzeFail;
2658 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)
2659 return ErrorNone;
2660
2661 if ((err = resolve_struct_zero_bits(g, struct_type)))
2662 return err;
2663
2664 AstNode *decl_node = struct_type->data.structure.decl_node;
2665
2666 if (struct_type->data.structure.resolve_loop_flag) {
2667 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
2668 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2669 add_node_error(g, decl_node,
2670 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));
2671 }
26812672 return ErrorSemanticAnalyzeFail;
26822673 }
26832674
2675 struct_type->data.structure.resolve_loop_flag = true;
2676 assert(decl_node->type == NodeTypeContainerDecl);
2677 assert(struct_type->di_type);
2678
2679 if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2680 struct_type->data.structure.abi_alignment = 1;
2681 }
2682
2683 size_t field_count = struct_type->data.structure.src_field_count;
2684 for (size_t i = 0; i < field_count; i += 1) {
2685 TypeStructField *field = &struct_type->data.structure.fields[i];
2686
2687 // If this assertion trips, look up the call stack. Probably something is
2688 // calling type_resolve with ResolveStatusAlignmentKnown when it should only
2689 // be resolving ResolveStatusZeroBitsKnown
2690 assert(field->type_entry != nullptr);
2691
2692 if (!type_has_bits(field->type_entry))
2693 continue;
2694
2695 // alignment of structs is the alignment of the most-aligned field
2696 if (struct_type->data.structure.layout != ContainerLayoutPacked) {
2697 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
2698 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2699 break;
2700 }
2701
2702 uint32_t this_field_align = get_abi_alignment(g, field->type_entry);
2703 assert(this_field_align != 0);
2704 if (this_field_align > struct_type->data.structure.abi_alignment) {
2705 struct_type->data.structure.abi_alignment = this_field_align;
2706 }
2707 }
2708 }
2709
2710 struct_type->data.structure.resolve_loop_flag = false;
2711
2712 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
2713 return ErrorSemanticAnalyzeFail;
2714 }
2715
2716 struct_type->data.structure.resolve_status = ResolveStatusAlignmentKnown;
26842717 return ErrorNone;
26852718}
26862719
......@@ -2807,7 +2840,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28072840 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));
28082841 return ErrorSemanticAnalyzeFail;
28092842 }
2810 if ((err = type_ensure_zero_bits_known(g, enum_type))) {
2843 if ((err = type_resolve(g, enum_type, ResolveStatusAlignmentKnown))) {
28112844 assert(g->errors.length != 0);
28122845 return err;
28132846 }
......@@ -2848,7 +2881,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28482881 }
28492882 } else {
28502883 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2851 if ((err = type_ensure_zero_bits_known(g, field_type))) {
2884 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
28522885 union_type->data.unionation.is_invalid = true;
28532886 continue;
28542887 }
......@@ -3111,7 +3144,7 @@ static void typecheck_panic_fn(CodeGen *g, ZigFn *panic_fn) {
31113144 return wrong_panic_prototype(g, proto_node, fn_type);
31123145 }
31133146 ZigType *const_u8_ptr = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
3114 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0);
3147 PtrLenUnknown, 0, 0, 0);
31153148 ZigType *const_u8_slice = get_slice_type(g, const_u8_ptr);
31163149 if (fn_type_id->param_info[0].type != const_u8_slice) {
31173150 return wrong_panic_prototype(g, proto_node, fn_type);
......@@ -3801,7 +3834,7 @@ TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name) {
38013834
38023835TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {
38033836 assert(type_entry->id == ZigTypeIdStruct);
3804 assert(type_entry->data.structure.complete);
3837 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
38053838 if (type_entry->data.structure.src_field_count == 0)
38063839 return nullptr;
38073840 auto entry = type_entry->data.structure.fields_by_name.maybe_get(name);
......@@ -3956,9 +3989,12 @@ bool type_is_codegen_pointer(ZigType *type) {
39563989uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
39573990 ZigType *ptr_type = get_codegen_ptr_type(type);
39583991 if (ptr_type->id == ZigTypeIdPointer) {
3959 return ptr_type->data.pointer.alignment;
3992 return (ptr_type->data.pointer.explicit_alignment == 0) ?
3993 get_abi_alignment(g, ptr_type->data.pointer.child_type) : ptr_type->data.pointer.explicit_alignment;
39603994 } else if (ptr_type->id == ZigTypeIdFn) {
3961 return (ptr_type->data.fn.fn_type_id.alignment == 0) ? 1 : ptr_type->data.fn.fn_type_id.alignment;
3995 return (ptr_type->data.fn.fn_type_id.alignment == 0) ?
3996 LLVMABIAlignmentOfType(g->target_data_ref, ptr_type->data.fn.raw_type_ref) :
3997 ptr_type->data.fn.fn_type_id.alignment;
39623998 } else if (ptr_type->id == ZigTypeIdPromise) {
39633999 return get_coro_frame_align_bytes(g);
39644000 } else {
......@@ -5023,8 +5059,8 @@ bool fn_eval_eql(Scope *a, Scope *b) {
50235059
50245060bool type_has_bits(ZigType *type_entry) {
50255061 assert(type_entry);
5026 assert(type_entry->id != ZigTypeIdInvalid);
5027 assert(type_has_zero_bits_known(type_entry));
5062 assert(!type_is_invalid(type_entry));
5063 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
50285064 return !type_entry->zero_bits;
50295065}
50305066
......@@ -5045,10 +5081,10 @@ bool type_requires_comptime(ZigType *type_entry) {
50455081 case ZigTypeIdArray:
50465082 return type_requires_comptime(type_entry->data.array.child_type);
50475083 case ZigTypeIdStruct:
5048 assert(type_has_zero_bits_known(type_entry));
5084 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
50495085 return type_entry->data.structure.requires_comptime;
50505086 case ZigTypeIdUnion:
5051 assert(type_has_zero_bits_known(type_entry));
5087 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
50525088 return type_entry->data.unionation.requires_comptime;
50535089 case ZigTypeIdOptional:
50545090 return type_requires_comptime(type_entry->data.maybe.child_type);
......@@ -5124,7 +5160,7 @@ void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
51245160 const_val->special = ConstValSpecialStatic;
51255161 // TODO make this `[*]null u8` instead of `[*]u8`
51265162 const_val->type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
5127 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0);
5163 PtrLenUnknown, 0, 0, 0);
51285164 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
51295165 const_val->data.x_ptr.data.base_array.array_val = array_val;
51305166 const_val->data.x_ptr.data.base_array.elem_index = 0;
......@@ -5269,8 +5305,7 @@ void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *arr
52695305 assert(array_val->type->id == ZigTypeIdArray);
52705306
52715307 ZigType *ptr_type = get_pointer_to_type_extra(g, array_val->type->data.array.child_type,
5272 is_const, false, PtrLenUnknown, get_abi_alignment(g, array_val->type->data.array.child_type),
5273 0, 0);
5308 is_const, false, PtrLenUnknown, 0, 0, 0);
52745309
52755310 const_val->special = ConstValSpecialStatic;
52765311 const_val->type = get_slice_type(g, ptr_type);
......@@ -5295,7 +5330,7 @@ void init_const_ptr_array(CodeGen *g, ConstExprValue *const_val, ConstExprValue
52955330
52965331 const_val->special = ConstValSpecialStatic;
52975332 const_val->type = get_pointer_to_type_extra(g, child_type, is_const, false,
5298 ptr_len, get_abi_alignment(g, child_type), 0, 0);
5333 ptr_len, 0, 0, 0);
52995334 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
53005335 const_val->data.x_ptr.data.base_array.array_val = array_val;
53015336 const_val->data.x_ptr.data.base_array.elem_index = elem_index;
......@@ -5394,32 +5429,46 @@ ConstExprValue *create_const_vals(size_t count) {
53945429}
53955430
53965431Error ensure_complete_type(CodeGen *g, ZigType *type_entry) {
5397 if (type_is_invalid(type_entry))
5398 return ErrorSemanticAnalyzeFail;
5399 if (type_entry->id == ZigTypeIdStruct) {
5400 if (!type_entry->data.structure.complete)
5401 return resolve_struct_type(g, type_entry);
5402 } else if (type_entry->id == ZigTypeIdEnum) {
5403 if (!type_entry->data.enumeration.complete)
5404 return resolve_enum_type(g, type_entry);
5405 } else if (type_entry->id == ZigTypeIdUnion) {
5406 if (!type_entry->data.unionation.complete)
5407 return resolve_union_type(g, type_entry);
5408 }
5409 return ErrorNone;
5432 return type_resolve(g, type_entry, ResolveStatusSizeKnown);
54105433}
54115434
5412Error type_ensure_zero_bits_known(CodeGen *g, ZigType *type_entry) {
5413 if (type_is_invalid(type_entry))
5435Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
5436 if (type_is_invalid(ty))
54145437 return ErrorSemanticAnalyzeFail;
5415 if (type_entry->id == ZigTypeIdStruct) {
5416 return resolve_struct_zero_bits(g, type_entry);
5417 } else if (type_entry->id == ZigTypeIdEnum) {
5418 return resolve_enum_zero_bits(g, type_entry);
5419 } else if (type_entry->id == ZigTypeIdUnion) {
5420 return resolve_union_zero_bits(g, type_entry);
5438 switch (status) {
5439 case ResolveStatusUnstarted:
5440 return ErrorNone;
5441 case ResolveStatusInvalid:
5442 zig_unreachable();
5443 case ResolveStatusZeroBitsKnown:
5444 if (ty->id == ZigTypeIdStruct) {
5445 return resolve_struct_zero_bits(g, ty);
5446 } else if (ty->id == ZigTypeIdEnum) {
5447 return resolve_enum_zero_bits(g, ty);
5448 } else if (ty->id == ZigTypeIdUnion) {
5449 return resolve_union_zero_bits(g, ty);
5450 }
5451 return ErrorNone;
5452 case ResolveStatusAlignmentKnown:
5453 if (ty->id == ZigTypeIdStruct) {
5454 return resolve_struct_alignment(g, ty);
5455 } else if (ty->id == ZigTypeIdEnum) {
5456 return resolve_enum_zero_bits(g, ty);
5457 } else if (ty->id == ZigTypeIdUnion) {
5458 return resolve_union_zero_bits(g, ty);
5459 }
5460 return ErrorNone;
5461 case ResolveStatusSizeKnown:
5462 if (ty->id == ZigTypeIdStruct) {
5463 return resolve_struct_type(g, ty);
5464 } else if (ty->id == ZigTypeIdEnum) {
5465 return resolve_enum_type(g, ty);
5466 } else if (ty->id == ZigTypeIdUnion) {
5467 return resolve_union_type(g, ty);
5468 }
5469 return ErrorNone;
54215470 }
5422 return ErrorNone;
5471 zig_unreachable();
54235472}
54245473
54255474bool ir_get_var_is_comptime(ZigVar *var) {
......@@ -6262,7 +6311,7 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
62626311}
62636312
62646313uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {
6265 assertNoError(type_ensure_zero_bits_known(g, type_entry));
6314 assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown));
62666315 if (type_entry->zero_bits) return 0;
62676316
62686317 // We need to make this function work without requiring ensure_complete_type
src/analyze.hpp+2-2
......@@ -59,9 +59,9 @@ bool get_ptr_const(ZigType *type);
5959ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry);
6060ZigType *container_ref_type(ZigType *type_entry);
6161bool type_is_complete(ZigType *type_entry);
62bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
6263bool type_is_invalid(ZigType *type_entry);
6364bool type_is_global_error_set(ZigType *err_set_type);
64bool type_has_zero_bits_known(ZigType *type_entry);
6565void resolve_container_type(CodeGen *g, ZigType *type_entry);
6666ScopeDecls *get_container_scope(ZigType *type_entry);
6767TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);
......@@ -89,7 +89,7 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_cou
8989AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);
9090bool type_requires_comptime(ZigType *type_entry);
9191Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry);
92Error ATTRIBUTE_MUST_USE type_ensure_zero_bits_known(CodeGen *g, ZigType *type_entry);
92Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status);
9393void complete_enum(CodeGen *g, ZigType *enum_type);
9494bool ir_get_var_is_comptime(ZigVar *var);
9595bool const_values_equal(ConstExprValue *a, ConstExprValue *b);
src/codegen.cpp+11-8
......@@ -775,7 +775,8 @@ static LLVMValueRef gen_store_untyped(CodeGen *g, LLVMValueRef value, LLVMValueR
775775
776776static LLVMValueRef gen_store(CodeGen *g, LLVMValueRef value, LLVMValueRef ptr, ZigType *ptr_type) {
777777 assert(ptr_type->id == ZigTypeIdPointer);
778 return gen_store_untyped(g, value, ptr, ptr_type->data.pointer.alignment, ptr_type->data.pointer.is_volatile);
778 uint32_t alignment = get_ptr_align(g, ptr_type);
779 return gen_store_untyped(g, value, ptr, alignment, ptr_type->data.pointer.is_volatile);
779780}
780781
781782static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMValueRef ptr, uint32_t alignment, bool is_volatile,
......@@ -793,7 +794,8 @@ static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMValueRef ptr, uint32_t alig
793794
794795static LLVMValueRef gen_load(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_type, const char *name) {
795796 assert(ptr_type->id == ZigTypeIdPointer);
796 return gen_load_untyped(g, ptr, ptr_type->data.pointer.alignment, ptr_type->data.pointer.is_volatile, name);
797 uint32_t alignment = get_ptr_align(g, ptr_type);
798 return gen_load_untyped(g, ptr, alignment, ptr_type->data.pointer.is_volatile, name);
797799}
798800
799801static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, ZigType *type, ZigType *ptr_type) {
......@@ -1795,7 +1797,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
17951797
17961798 ZigType *usize = g->builtin_types.entry_usize;
17971799 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref);
1798 uint64_t align_bytes = ptr_type->data.pointer.alignment;
1800 uint64_t align_bytes = get_ptr_align(g, ptr_type);
17991801 assert(size_bytes > 0);
18001802 assert(align_bytes > 0);
18011803
......@@ -4084,7 +4086,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
40844086 LLVMValueRef ptr_val;
40854087
40864088 if (target_type->id == ZigTypeIdPointer) {
4087 align_bytes = target_type->data.pointer.alignment;
4089 align_bytes = get_ptr_align(g, target_type);
40884090 ptr_val = target_val;
40894091 } else if (target_type->id == ZigTypeIdFn) {
40904092 align_bytes = target_type->data.fn.fn_type_id.alignment;
......@@ -4092,7 +4094,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
40924094 } else if (target_type->id == ZigTypeIdOptional &&
40934095 target_type->data.maybe.child_type->id == ZigTypeIdPointer)
40944096 {
4095 align_bytes = target_type->data.maybe.child_type->data.pointer.alignment;
4097 align_bytes = get_ptr_align(g, target_type->data.maybe.child_type);
40964098 ptr_val = target_val;
40974099 } else if (target_type->id == ZigTypeIdOptional &&
40984100 target_type->data.maybe.child_type->id == ZigTypeIdFn)
......@@ -4105,7 +4107,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
41054107 zig_panic("TODO audit this function");
41064108 } else if (target_type->id == ZigTypeIdStruct && target_type->data.structure.is_slice) {
41074109 ZigType *slice_ptr_type = target_type->data.structure.fields[slice_ptr_index].type_entry;
4108 align_bytes = slice_ptr_type->data.pointer.alignment;
4110 align_bytes = get_ptr_align(g, slice_ptr_type);
41094111
41104112 size_t ptr_index = target_type->data.structure.fields[slice_ptr_index].gen_index;
41114113 LLVMValueRef ptr_val_ptr = LLVMBuildStructGEP(g->builder, target_val, (unsigned)ptr_index, "");
......@@ -4260,7 +4262,8 @@ static LLVMValueRef ir_render_memset(CodeGen *g, IrExecutable *executable, IrIns
42604262 LLVMValueRef is_volatile = ptr_type->data.pointer.is_volatile ?
42614263 LLVMConstAllOnes(LLVMInt1Type()) : LLVMConstNull(LLVMInt1Type());
42624264
4263 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), ptr_type->data.pointer.alignment, false);
4265 uint32_t alignment = get_ptr_align(g, ptr_type);
4266 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), alignment, false);
42644267
42654268 LLVMValueRef params[] = {
42664269 dest_ptr_casted,
......@@ -4293,7 +4296,7 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns
42934296 LLVMValueRef is_volatile = (dest_ptr_type->data.pointer.is_volatile || src_ptr_type->data.pointer.is_volatile) ?
42944297 LLVMConstAllOnes(LLVMInt1Type()) : LLVMConstNull(LLVMInt1Type());
42954298
4296 uint32_t min_align_bytes = min(src_ptr_type->data.pointer.alignment, dest_ptr_type->data.pointer.alignment);
4299 uint32_t min_align_bytes = min(get_ptr_align(g, src_ptr_type), get_ptr_align(g, dest_ptr_type));
42974300 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), min_align_bytes, false);
42984301
42994302 LLVMValueRef params[] = {
src/ir.cpp+257-119
......@@ -40,6 +40,7 @@ struct IrAnalyze {
4040
4141enum ConstCastResultId {
4242 ConstCastResultIdOk,
43 ConstCastResultIdInvalid,
4344 ConstCastResultIdErrSet,
4445 ConstCastResultIdErrSetGlobal,
4546 ConstCastResultIdPointerChild,
......@@ -7490,8 +7491,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
74907491 if (type_has_bits(return_type)) {
74917492 IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,
74927493 get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8,
7493 false, false, PtrLenUnknown, get_abi_alignment(irb->codegen, irb->codegen->builtin_types.entry_u8),
7494 0, 0));
7494 false, false, PtrLenUnknown, 0, 0, 0));
74957495 IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr);
74967496 IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, result_ptr);
74977497 IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len,
......@@ -7544,8 +7544,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
75447544 IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle);
75457545 IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,
75467546 get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8,
7547 false, false, PtrLenUnknown, get_abi_alignment(irb->codegen, irb->codegen->builtin_types.entry_u8),
7548 0, 0));
7547 false, false, PtrLenUnknown, 0, 0, 0));
75497548 IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, coro_mem_ptr_maybe);
75507549 IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false);
75517550 IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var);
......@@ -8516,6 +8515,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
85168515 ConstCastOnly result = {};
85178516 result.id = ConstCastResultIdOk;
85188517
8518 Error err;
8519
85198520 if (wanted_type == actual_type)
85208521 return result;
85218522
......@@ -8528,6 +8529,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
85288529 {
85298530 ConstCastOnly child = types_match_const_cast_only(ira,
85308531 wanted_type->data.maybe.child_type, actual_type, source_node, wanted_is_mutable);
8532 if (child.id == ConstCastResultIdInvalid)
8533 return child;
85318534 if (child.id != ConstCastResultIdOk) {
85328535 result.id = ConstCastResultIdNullWrapPtr;
85338536 result.data.null_wrap_ptr_child = allocate_nonzero<ConstCastOnly>(1);
......@@ -8544,7 +8547,6 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
85448547 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&
85458548 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile))
85468549 {
8547 assert(actual_type->data.pointer.alignment >= wanted_type->data.pointer.alignment);
85488550 return result;
85498551 }
85508552
......@@ -8552,6 +8554,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
85528554 if (wanted_type->id == ZigTypeIdPointer && actual_type->id == ZigTypeIdPointer) {
85538555 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
85548556 actual_type->data.pointer.child_type, source_node, !wanted_type->data.pointer.is_const);
8557 if (child.id == ConstCastResultIdInvalid)
8558 return child;
85558559 if (child.id != ConstCastResultIdOk) {
85568560 result.id = ConstCastResultIdPointerChild;
85578561 result.data.pointer_mismatch = allocate_nonzero<ConstCastPointerMismatch>(1);
......@@ -8560,12 +8564,20 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
85608564 result.data.pointer_mismatch->actual_child = actual_type->data.pointer.child_type;
85618565 return result;
85628566 }
8567 if ((err = type_resolve(g, actual_type->data.pointer.child_type, ResolveStatusAlignmentKnown))) {
8568 result.id = ConstCastResultIdInvalid;
8569 return result;
8570 }
8571 if ((err = type_resolve(g, wanted_type->data.pointer.child_type, ResolveStatusAlignmentKnown))) {
8572 result.id = ConstCastResultIdInvalid;
8573 return result;
8574 }
85638575 if ((actual_type->data.pointer.ptr_len == wanted_type->data.pointer.ptr_len) &&
85648576 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&
85658577 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile) &&
85668578 actual_type->data.pointer.bit_offset == wanted_type->data.pointer.bit_offset &&
85678579 actual_type->data.pointer.unaligned_bit_count == wanted_type->data.pointer.unaligned_bit_count &&
8568 actual_type->data.pointer.alignment >= wanted_type->data.pointer.alignment)
8580 get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, wanted_type))
85698581 {
85708582 return result;
85718583 }
......@@ -8575,14 +8587,24 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
85758587 if (is_slice(wanted_type) && is_slice(actual_type)) {
85768588 ZigType *actual_ptr_type = actual_type->data.structure.fields[slice_ptr_index].type_entry;
85778589 ZigType *wanted_ptr_type = wanted_type->data.structure.fields[slice_ptr_index].type_entry;
8590 if ((err = type_resolve(g, actual_ptr_type->data.pointer.child_type, ResolveStatusAlignmentKnown))) {
8591 result.id = ConstCastResultIdInvalid;
8592 return result;
8593 }
8594 if ((err = type_resolve(g, wanted_ptr_type->data.pointer.child_type, ResolveStatusAlignmentKnown))) {
8595 result.id = ConstCastResultIdInvalid;
8596 return result;
8597 }
85788598 if ((!actual_ptr_type->data.pointer.is_const || wanted_ptr_type->data.pointer.is_const) &&
85798599 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile) &&
85808600 actual_ptr_type->data.pointer.bit_offset == wanted_ptr_type->data.pointer.bit_offset &&
85818601 actual_ptr_type->data.pointer.unaligned_bit_count == wanted_ptr_type->data.pointer.unaligned_bit_count &&
8582 actual_ptr_type->data.pointer.alignment >= wanted_ptr_type->data.pointer.alignment)
8602 get_ptr_align(g, actual_ptr_type) >= get_ptr_align(g, wanted_ptr_type))
85838603 {
85848604 ConstCastOnly child = types_match_const_cast_only(ira, wanted_ptr_type->data.pointer.child_type,
85858605 actual_ptr_type->data.pointer.child_type, source_node, !wanted_ptr_type->data.pointer.is_const);
8606 if (child.id == ConstCastResultIdInvalid)
8607 return child;
85868608 if (child.id != ConstCastResultIdOk) {
85878609 result.id = ConstCastResultIdSliceChild;
85888610 result.data.slice_mismatch = allocate_nonzero<ConstCastSliceMismatch>(1);
......@@ -8598,6 +8620,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
85988620 if (wanted_type->id == ZigTypeIdOptional && actual_type->id == ZigTypeIdOptional) {
85998621 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.maybe.child_type,
86008622 actual_type->data.maybe.child_type, source_node, wanted_is_mutable);
8623 if (child.id == ConstCastResultIdInvalid)
8624 return child;
86018625 if (child.id != ConstCastResultIdOk) {
86028626 result.id = ConstCastResultIdOptionalChild;
86038627 result.data.optional = allocate_nonzero<ConstCastOptionalMismatch>(1);
......@@ -8612,6 +8636,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
86128636 if (wanted_type->id == ZigTypeIdErrorUnion && actual_type->id == ZigTypeIdErrorUnion) {
86138637 ConstCastOnly payload_child = types_match_const_cast_only(ira, wanted_type->data.error_union.payload_type,
86148638 actual_type->data.error_union.payload_type, source_node, wanted_is_mutable);
8639 if (payload_child.id == ConstCastResultIdInvalid)
8640 return payload_child;
86158641 if (payload_child.id != ConstCastResultIdOk) {
86168642 result.id = ConstCastResultIdErrorUnionPayload;
86178643 result.data.error_union_payload = allocate_nonzero<ConstCastErrUnionPayloadMismatch>(1);
......@@ -8622,6 +8648,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
86228648 }
86238649 ConstCastOnly error_set_child = types_match_const_cast_only(ira, wanted_type->data.error_union.err_set_type,
86248650 actual_type->data.error_union.err_set_type, source_node, wanted_is_mutable);
8651 if (error_set_child.id == ConstCastResultIdInvalid)
8652 return error_set_child;
86258653 if (error_set_child.id != ConstCastResultIdOk) {
86268654 result.id = ConstCastResultIdErrorUnionErrorSet;
86278655 result.data.error_union_error_set = allocate_nonzero<ConstCastErrUnionErrSetMismatch>(1);
......@@ -8709,6 +8737,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
87098737 {
87108738 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.fn.fn_type_id.return_type,
87118739 actual_type->data.fn.fn_type_id.return_type, source_node, false);
8740 if (child.id == ConstCastResultIdInvalid)
8741 return child;
87128742 if (child.id != ConstCastResultIdOk) {
87138743 result.id = ConstCastResultIdFnReturnType;
87148744 result.data.return_type = allocate_nonzero<ConstCastOnly>(1);
......@@ -8721,6 +8751,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
87218751 actual_type->data.fn.fn_type_id.async_allocator_type,
87228752 wanted_type->data.fn.fn_type_id.async_allocator_type,
87238753 source_node, false);
8754 if (child.id == ConstCastResultIdInvalid)
8755 return child;
87248756 if (child.id != ConstCastResultIdOk) {
87258757 result.id = ConstCastResultIdAsyncAllocatorType;
87268758 result.data.async_allocator_type = allocate_nonzero<ConstCastOnly>(1);
......@@ -8745,6 +8777,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
87458777
87468778 ConstCastOnly arg_child = types_match_const_cast_only(ira, actual_param_info->type,
87478779 expected_param_info->type, source_node, false);
8780 if (arg_child.id == ConstCastResultIdInvalid)
8781 return arg_child;
87488782 if (arg_child.id != ConstCastResultIdOk) {
87498783 result.id = ConstCastResultIdFnArg;
87508784 result.data.fn_arg.arg_index = i;
......@@ -9238,7 +9272,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
92389272 if (prev_type->id == ZigTypeIdEnum && cur_type->id == ZigTypeIdUnion &&
92399273 (cur_type->data.unionation.decl_node->data.container_decl.auto_enum || cur_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
92409274 {
9241 if ((err = type_ensure_zero_bits_known(ira->codegen, cur_type)))
9275 if ((err = type_resolve(ira->codegen, cur_type, ResolveStatusZeroBitsKnown)))
92429276 return ira->codegen->builtin_types.entry_invalid;
92439277 if (cur_type->data.unionation.tag_type == prev_type) {
92449278 continue;
......@@ -9248,7 +9282,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
92489282 if (cur_type->id == ZigTypeIdEnum && prev_type->id == ZigTypeIdUnion &&
92499283 (prev_type->data.unionation.decl_node->data.container_decl.auto_enum || prev_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
92509284 {
9251 if ((err = type_ensure_zero_bits_known(ira->codegen, prev_type)))
9285 if ((err = type_resolve(ira->codegen, prev_type, ResolveStatusZeroBitsKnown)))
92529286 return ira->codegen->builtin_types.entry_invalid;
92539287 if (prev_type->data.unionation.tag_type == cur_type) {
92549288 prev_inst = cur_inst;
......@@ -9274,8 +9308,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
92749308 ZigType *ptr_type = get_pointer_to_type_extra(
92759309 ira->codegen, prev_inst->value.type->data.array.child_type,
92769310 true, false, PtrLenUnknown,
9277 get_abi_alignment(ira->codegen, prev_inst->value.type->data.array.child_type),
9278 0, 0);
9311 0, 0, 0);
92799312 ZigType *slice_type = get_slice_type(ira->codegen, ptr_type);
92809313 if (err_set_type != nullptr) {
92819314 return get_error_union_type(ira->codegen, err_set_type, slice_type);
......@@ -9472,7 +9505,16 @@ static IrInstruction *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira,
94729505 IrInstruction *value, ZigType *wanted_type)
94739506{
94749507 assert(value->value.type->id == ZigTypeIdPointer);
9475 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, value->value.type->data.pointer.alignment);
9508
9509 Error err;
9510
9511 if ((err = type_resolve(ira->codegen, value->value.type->data.pointer.child_type,
9512 ResolveStatusAlignmentKnown)))
9513 {
9514 return ira->codegen->invalid_instruction;
9515 }
9516
9517 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, value->value.type));
94769518
94779519 if (instr_is_comptime(value)) {
94789520 ConstExprValue *pointee = ir_const_ptr_pointee(ira, &value->value, source_instr->source_node);
......@@ -9500,7 +9542,15 @@ static IrInstruction *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira,
95009542static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruction *source_instr,
95019543 IrInstruction *value, ZigType *wanted_type)
95029544{
9503 wanted_type = adjust_slice_align(ira->codegen, wanted_type, value->value.type->data.pointer.alignment);
9545 Error err;
9546
9547 if ((err = type_resolve(ira->codegen, value->value.type->data.pointer.child_type,
9548 ResolveStatusAlignmentKnown)))
9549 {
9550 return ira->codegen->invalid_instruction;
9551 }
9552
9553 wanted_type = adjust_slice_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, value->value.type));
95049554
95059555 if (instr_is_comptime(value)) {
95069556 ConstExprValue *pointee = ir_const_ptr_pointee(ira, &value->value, source_instr->source_node);
......@@ -9687,8 +9737,7 @@ static ZigType *ir_analyze_const_ptr(IrAnalyze *ira, IrInstruction *instruction,
96879737 ConstPtrMut ptr_mut, bool ptr_is_const, bool ptr_is_volatile)
96889738{
96899739 IrInstruction *const_instr = ir_get_const_ptr(ira, instruction, pointee,
9690 pointee_type, ptr_mut, ptr_is_const, ptr_is_volatile,
9691 get_abi_alignment(ira->codegen, pointee_type));
9740 pointee_type, ptr_mut, ptr_is_const, ptr_is_volatile, 0);
96929741 ir_link_new_instruction(const_instr, instruction);
96939742 return const_instr->value.type;
96949743}
......@@ -10005,20 +10054,24 @@ static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *so
1000510054static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *value,
1000610055 bool is_const, bool is_volatile)
1000710056{
10057 Error err;
10058
1000810059 if (type_is_invalid(value->value.type))
1000910060 return ira->codegen->invalid_instruction;
1001010061
10062 if ((err = type_resolve(ira->codegen, value->value.type, ResolveStatusZeroBitsKnown)))
10063 return ira->codegen->invalid_instruction;
10064
1001110065 if (instr_is_comptime(value)) {
1001210066 ConstExprValue *val = ir_resolve_const(ira, value, UndefOk);
1001310067 if (!val)
1001410068 return ira->codegen->invalid_instruction;
1001510069 return ir_get_const_ptr(ira, source_instruction, val, value->value.type,
10016 ConstPtrMutComptimeConst, is_const, is_volatile,
10017 get_abi_alignment(ira->codegen, value->value.type));
10070 ConstPtrMutComptimeConst, is_const, is_volatile, 0);
1001810071 }
1001910072
1002010073 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value->value.type,
10021 is_const, is_volatile, PtrLenSingle, get_abi_alignment(ira->codegen, value->value.type), 0, 0);
10074 is_const, is_volatile, PtrLenSingle, 0, 0, 0);
1002210075 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instruction->scope,
1002310076 source_instruction->source_node, value, is_const, is_volatile);
1002410077 new_instruction->value.type = ptr_type;
......@@ -10185,9 +10238,9 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
1018510238 return ira->codegen->invalid_instruction;
1018610239 TypeUnionField *union_field = find_union_field_by_tag(wanted_type, &val->data.x_enum_tag);
1018710240 assert(union_field != nullptr);
10188 if ((err = type_ensure_zero_bits_known(ira->codegen, union_field->type_entry)))
10241 if ((err = type_resolve(ira->codegen, union_field->type_entry, ResolveStatusZeroBitsKnown)))
1018910242 return ira->codegen->invalid_instruction;
10190 if (!union_field->type_entry->zero_bits) {
10243 if (type_has_bits(union_field->type_entry)) {
1019110244 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(
1019210245 union_field->enum_field->decl_index);
1019310246 ErrorMsg *msg = ir_add_error(ira, source_instr,
......@@ -10490,7 +10543,10 @@ static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *sou
1049010543 ZigType *wanted_type)
1049110544{
1049210545 assert(wanted_type->id == ZigTypeIdPointer);
10493 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, target->value.type->data.pointer.alignment);
10546 Error err;
10547 if ((err = type_resolve(ira->codegen, target->value.type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
10548 return ira->codegen->invalid_instruction;
10549 wanted_type = adjust_ptr_align(ira->codegen, wanted_type, get_ptr_align(ira->codegen, target->value.type));
1049410550 ZigType *array_type = wanted_type->data.pointer.child_type;
1049510551 assert(array_type->id == ZigTypeIdArray);
1049610552 assert(array_type->data.array.len == 1);
......@@ -10537,6 +10593,8 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1053710593 switch (cast_result->id) {
1053810594 case ConstCastResultIdOk:
1053910595 zig_unreachable();
10596 case ConstCastResultIdInvalid:
10597 zig_unreachable();
1054010598 case ConstCastResultIdOptionalChild: {
1054110599 ErrorMsg *msg = add_error_note(ira->codegen, parent_msg, source_node,
1054210600 buf_sprintf("optional type child '%s' cannot cast into optional type child '%s'",
......@@ -10636,6 +10694,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1063610694 // perfect match or non-const to const
1063710695 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, wanted_type, actual_type,
1063810696 source_node, false);
10697 if (const_cast_result.id == ConstCastResultIdInvalid)
10698 return ira->codegen->invalid_instruction;
1063910699 if (const_cast_result.id == ConstCastResultIdOk) {
1064010700 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop, false);
1064110701 }
......@@ -10751,13 +10811,19 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1075110811 wanted_type->data.pointer.ptr_len == PtrLenUnknown &&
1075210812 actual_type->id == ZigTypeIdPointer &&
1075310813 actual_type->data.pointer.ptr_len == PtrLenSingle &&
10754 actual_type->data.pointer.child_type->id == ZigTypeIdArray &&
10755 actual_type->data.pointer.alignment >= wanted_type->data.pointer.alignment &&
10756 types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
10757 actual_type->data.pointer.child_type->data.array.child_type, source_node,
10758 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
10814 actual_type->data.pointer.child_type->id == ZigTypeIdArray)
1075910815 {
10760 return ir_resolve_ptr_of_array_to_unknown_len_ptr(ira, source_instr, value, wanted_type);
10816 if ((err = type_resolve(ira->codegen, actual_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
10817 return ira->codegen->invalid_instruction;
10818 if ((err = type_resolve(ira->codegen, wanted_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
10819 return ira->codegen->invalid_instruction;
10820 if (get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, wanted_type) &&
10821 types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
10822 actual_type->data.pointer.child_type->data.array.child_type, source_node,
10823 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
10824 {
10825 return ir_resolve_ptr_of_array_to_unknown_len_ptr(ira, source_instr, value, wanted_type);
10826 }
1076110827 }
1076210828
1076310829 // *[N]T to []T
......@@ -10811,16 +10877,23 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1081110877 wanted_child_type->data.pointer.ptr_len == PtrLenUnknown &&
1081210878 actual_type->id == ZigTypeIdPointer &&
1081310879 actual_type->data.pointer.ptr_len == PtrLenSingle &&
10814 actual_type->data.pointer.child_type->id == ZigTypeIdArray &&
10815 actual_type->data.pointer.alignment >= wanted_child_type->data.pointer.alignment &&
10816 types_match_const_cast_only(ira, wanted_child_type->data.pointer.child_type,
10817 actual_type->data.pointer.child_type->data.array.child_type, source_node,
10818 !wanted_child_type->data.pointer.is_const).id == ConstCastResultIdOk)
10880 actual_type->data.pointer.child_type->id == ZigTypeIdArray)
1081910881 {
10820 IrInstruction *cast1 = ir_resolve_ptr_of_array_to_unknown_len_ptr(ira, source_instr, value, wanted_child_type);
10821 if (type_is_invalid(cast1->value.type))
10882 if ((err = type_resolve(ira->codegen, actual_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
1082210883 return ira->codegen->invalid_instruction;
10823 return ir_analyze_maybe_wrap(ira, source_instr, cast1, wanted_type);
10884 if ((err = type_resolve(ira->codegen, wanted_child_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
10885 return ira->codegen->invalid_instruction;
10886 if (get_ptr_align(ira->codegen, actual_type) >= get_ptr_align(ira->codegen, wanted_child_type) &&
10887 types_match_const_cast_only(ira, wanted_child_type->data.pointer.child_type,
10888 actual_type->data.pointer.child_type->data.array.child_type, source_node,
10889 !wanted_child_type->data.pointer.is_const).id == ConstCastResultIdOk)
10890 {
10891 IrInstruction *cast1 = ir_resolve_ptr_of_array_to_unknown_len_ptr(ira, source_instr, value,
10892 wanted_child_type);
10893 if (type_is_invalid(cast1->value.type))
10894 return ira->codegen->invalid_instruction;
10895 return ir_analyze_maybe_wrap(ira, source_instr, cast1, wanted_type);
10896 }
1082410897 }
1082510898 }
1082610899
......@@ -10963,7 +11036,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1096311036
1096411037 // cast from union to the enum type of the union
1096511038 if (actual_type->id == ZigTypeIdUnion && wanted_type->id == ZigTypeIdEnum) {
10966 if ((err = type_ensure_zero_bits_known(ira->codegen, actual_type)))
11039 if ((err = type_resolve(ira->codegen, actual_type, ResolveStatusZeroBitsKnown)))
1096711040 return ira->codegen->invalid_instruction;
1096811041
1096911042 if (actual_type->data.unionation.tag_type == wanted_type) {
......@@ -10976,7 +11049,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1097611049 (wanted_type->data.unionation.decl_node->data.container_decl.auto_enum ||
1097711050 wanted_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
1097811051 {
10979 if ((err = type_ensure_zero_bits_known(ira->codegen, wanted_type)))
11052 if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusZeroBitsKnown)))
1098011053 return ira->codegen->invalid_instruction;
1098111054
1098211055 if (wanted_type->data.unionation.tag_type == actual_type) {
......@@ -10990,7 +11063,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1099011063 if (union_type->data.unionation.decl_node->data.container_decl.auto_enum ||
1099111064 union_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr)
1099211065 {
10993 if ((err = type_ensure_zero_bits_known(ira->codegen, union_type)))
11066 if ((err = type_resolve(ira->codegen, union_type, ResolveStatusZeroBitsKnown)))
1099411067 return ira->codegen->invalid_instruction;
1099511068
1099611069 if (union_type->data.unionation.tag_type == actual_type) {
......@@ -11017,14 +11090,24 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1101711090 actual_type->data.pointer.child_type, source_node,
1101811091 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
1101911092 {
11020 if (wanted_type->data.pointer.alignment > actual_type->data.pointer.alignment) {
11093 if ((err = type_resolve(ira->codegen, wanted_type->data.pointer.child_type,
11094 ResolveStatusAlignmentKnown)))
11095 {
11096 return ira->codegen->invalid_instruction;
11097 }
11098 if ((err = type_resolve(ira->codegen, actual_type->data.pointer.child_type,
11099 ResolveStatusAlignmentKnown)))
11100 {
11101 return ira->codegen->invalid_instruction;
11102 }
11103 uint32_t wanted_align = get_ptr_align(ira->codegen, wanted_type);
11104 uint32_t actual_align = get_ptr_align(ira->codegen, actual_type);
11105 if (wanted_align > actual_align) {
1102111106 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("cast increases pointer alignment"));
1102211107 add_error_note(ira->codegen, msg, value->source_node,
11023 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&actual_type->name),
11024 actual_type->data.pointer.alignment));
11108 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&actual_type->name), actual_align));
1102511109 add_error_note(ira->codegen, msg, source_instr->source_node,
11026 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&wanted_type->name),
11027 wanted_type->data.pointer.alignment));
11110 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&wanted_type->name), wanted_align));
1102811111 return ira->codegen->invalid_instruction;
1102911112 }
1103011113 return ir_analyze_ptr_to_array(ira, source_instr, value, wanted_type);
......@@ -11036,7 +11119,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1103611119 types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
1103711120 actual_type, source_node, !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
1103811121 {
11039 if ((err = type_ensure_zero_bits_known(ira->codegen, actual_type))) {
11122 if ((err = type_resolve(ira->codegen, actual_type, ResolveStatusZeroBitsKnown))) {
1104011123 return ira->codegen->invalid_instruction;
1104111124 }
1104211125 if (!type_has_bits(actual_type)) {
......@@ -11282,8 +11365,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
1128211365 return nullptr;
1128311366
1128411367 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
11285 true, false, PtrLenUnknown,
11286 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8), 0, 0);
11368 true, false, PtrLenUnknown, 0, 0, 0);
1128711369 ZigType *str_type = get_slice_type(ira->codegen, ptr_type);
1128811370 IrInstruction *casted_value = ir_implicit_cast(ira, value, str_type);
1128911371 if (type_is_invalid(casted_value->value.type))
......@@ -11573,8 +11655,6 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op
1157311655 ZigType *resolved_type = ir_resolve_peer_types(ira, source_node, nullptr, instructions, 2);
1157411656 if (type_is_invalid(resolved_type))
1157511657 return resolved_type;
11576 if ((err = type_ensure_zero_bits_known(ira->codegen, resolved_type)))
11577 return resolved_type;
1157811658
1157911659 bool operator_allowed;
1158011660 switch (resolved_type->id) {
......@@ -11630,6 +11710,9 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op
1163011710 if (casted_op2 == ira->codegen->invalid_instruction)
1163111711 return ira->codegen->builtin_types.entry_invalid;
1163211712
11713 if ((err = type_resolve(ira->codegen, resolved_type, ResolveStatusZeroBitsKnown)))
11714 return resolved_type;
11715
1163311716 bool one_possible_value = !type_requires_comptime(resolved_type) && !type_has_bits(resolved_type);
1163411717 if (one_possible_value || (instr_is_comptime(casted_op1) && instr_is_comptime(casted_op2))) {
1163511718 ConstExprValue *op1_val = one_possible_value ? &casted_op1->value : ir_resolve_const(ira, casted_op1, UndefBad);
......@@ -12316,7 +12399,7 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc
1231612399 out_array_val = out_val;
1231712400 } else if (is_slice(op1_type) || is_slice(op2_type)) {
1231812401 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
12319 true, false, PtrLenUnknown, get_abi_alignment(ira->codegen, child_type), 0, 0);
12402 true, false, PtrLenUnknown, 0, 0, 0);
1232012403 result_type = get_slice_type(ira->codegen, ptr_type);
1232112404 out_array_val = create_const_vals(1);
1232212405 out_array_val->special = ConstValSpecialStatic;
......@@ -12337,8 +12420,7 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc
1233712420 new_len += 1; // null byte
1233812421
1233912422 // TODO make this `[*]null T` instead of `[*]T`
12340 result_type = get_pointer_to_type_extra(ira->codegen, child_type, true, false,
12341 PtrLenUnknown, get_abi_alignment(ira->codegen, child_type), 0, 0);
12423 result_type = get_pointer_to_type_extra(ira->codegen, child_type, true, false, PtrLenUnknown, 0, 0, 0);
1234212424
1234312425 out_array_val = create_const_vals(1);
1234412426 out_array_val->special = ConstValSpecialStatic;
......@@ -12563,7 +12645,7 @@ static ZigType *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstructionDec
1256312645 if (type_is_invalid(result_type)) {
1256412646 result_type = ira->codegen->builtin_types.entry_invalid;
1256512647 } else {
12566 if ((err = type_ensure_zero_bits_known(ira->codegen, result_type))) {
12648 if ((err = type_resolve(ira->codegen, result_type, ResolveStatusZeroBitsKnown))) {
1256712649 result_type = ira->codegen->builtin_types.entry_invalid;
1256812650 }
1256912651 }
......@@ -12631,6 +12713,11 @@ static ZigType *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstructionDec
1263112713 }
1263212714
1263312715 if (decl_var_instruction->align_value == nullptr) {
12716 if ((err = type_resolve(ira->codegen, result_type, ResolveStatusAlignmentKnown))) {
12717 var->value->type = ira->codegen->builtin_types.entry_invalid;
12718 decl_var_instruction->base.other = &decl_var_instruction->base;
12719 return ira->codegen->builtin_types.entry_void;
12720 }
1263412721 var->align_bytes = get_abi_alignment(ira->codegen, result_type);
1263512722 } else {
1263612723 if (!ir_resolve_align(ira, decl_var_instruction->align_value->other, &var->align_bytes)) {
......@@ -13100,7 +13187,6 @@ static ZigVar *get_fn_var_by_index(ZigFn *fn_entry, size_t index) {
1310013187static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1310113188 ZigVar *var)
1310213189{
13103 Error err;
1310413190 while (var->next_var != nullptr) {
1310513191 var = var->next_var;
1310613192 }
......@@ -13158,8 +13244,6 @@ no_mem_slot:
1315813244 instruction->scope, instruction->source_node, var);
1315913245 var_ptr_instruction->value.type = get_pointer_to_type_extra(ira->codegen, var->value->type,
1316013246 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0);
13161 if ((err = type_ensure_zero_bits_known(ira->codegen, var->value->type)))
13162 return ira->codegen->invalid_instruction;
1316313247
1316413248 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);
1316513249 var_ptr_instruction->value.data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack;
......@@ -13356,8 +13440,7 @@ static ZigType *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instr
1335613440 IrInstruction *casted_new_stack = nullptr;
1335713441 if (call_instruction->new_stack != nullptr) {
1335813442 ZigType *u8_ptr = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
13359 false, false, PtrLenUnknown,
13360 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8), 0, 0);
13443 false, false, PtrLenUnknown, 0, 0, 0);
1336113444 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
1336213445 IrInstruction *new_stack = call_instruction->new_stack->other;
1336313446 if (type_is_invalid(new_stack->value.type))
......@@ -13536,7 +13619,7 @@ static ZigType *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instr
1353613619 inst_fn_type_id.return_type = specified_return_type;
1353713620 }
1353813621
13539 if ((err = type_ensure_zero_bits_known(ira->codegen, specified_return_type)))
13622 if ((err = type_resolve(ira->codegen, specified_return_type, ResolveStatusZeroBitsKnown)))
1354013623 return ira->codegen->builtin_types.entry_invalid;
1354113624
1354213625 if (type_requires_comptime(specified_return_type)) {
......@@ -14212,7 +14295,7 @@ static ZigType *adjust_ptr_len(CodeGen *g, ZigType *ptr_type, PtrLen ptr_len) {
1421214295 ptr_type->data.pointer.child_type,
1421314296 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1421414297 ptr_len,
14215 ptr_type->data.pointer.alignment,
14298 ptr_type->data.pointer.explicit_alignment,
1421614299 ptr_type->data.pointer.bit_offset, ptr_type->data.pointer.unaligned_bit_count);
1421714300}
1421814301
......@@ -14264,7 +14347,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle
1426414347 return_type = get_pointer_to_type_extra(ira->codegen, child_type,
1426514348 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1426614349 elem_ptr_instruction->ptr_len,
14267 ptr_type->data.pointer.alignment, 0, 0);
14350 ptr_type->data.pointer.explicit_alignment, 0, 0);
1426814351 } else {
1426914352 uint64_t elem_val_scalar;
1427014353 if (!ir_resolve_usize(ira, elem_index, &elem_val_scalar))
......@@ -14336,7 +14419,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle
1433614419
1433714420 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);
1433814421 uint64_t abi_align = get_abi_alignment(ira->codegen, return_type->data.pointer.child_type);
14339 uint64_t ptr_align = return_type->data.pointer.alignment;
14422 uint64_t ptr_align = get_ptr_align(ira->codegen, return_type);
1434014423 if (instr_is_comptime(casted_elem_index)) {
1434114424 uint64_t index = bigint_as_unsigned(&casted_elem_index->value.data.x_bigint);
1434214425 if (array_type->id == ZigTypeIdArray) {
......@@ -14653,9 +14736,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1465314736 }
1465414737
1465514738 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,
14656 is_const, is_volatile,
14657 PtrLenSingle,
14658 get_abi_alignment(ira->codegen, field_type), 0, 0);
14739 is_const, is_volatile, PtrLenSingle, 0, 0, 0);
1465914740
1466014741 IrInstruction *result = ir_get_const(ira, source_instr);
1466114742 ConstExprValue *const_val = &result->value;
......@@ -14669,7 +14750,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1466914750
1467014751 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, container_ptr, field);
1467114752 result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,
14672 PtrLenSingle, get_abi_alignment(ira->codegen, field->type_entry), 0, 0);
14753 PtrLenSingle, 0, 0, 0);
1467314754 return result;
1467414755 } else {
1467514756 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,
......@@ -15002,9 +15083,14 @@ static ZigType *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstructionFi
1500215083 } else if (buf_eql_str(field_name, "alignment")) {
1500315084 bool ptr_is_const = true;
1500415085 bool ptr_is_volatile = false;
15086 if ((err = type_resolve(ira->codegen, child_type->data.pointer.child_type,
15087 ResolveStatusAlignmentKnown)))
15088 {
15089 return ira->codegen->builtin_types.entry_invalid;
15090 }
1500515091 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
1500615092 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,
15007 child_type->data.pointer.alignment, false),
15093 get_ptr_align(ira->codegen, child_type), false),
1500815094 ira->codegen->builtin_types.entry_num_lit_int,
1500915095 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
1501015096 } else {
......@@ -15461,7 +15547,7 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1546115547 IrInstructionSliceType *slice_type_instruction)
1546215548{
1546315549 Error err;
15464 uint32_t align_bytes;
15550 uint32_t align_bytes = 0;
1546515551 if (slice_type_instruction->align_value != nullptr) {
1546615552 if (!ir_resolve_align(ira, slice_type_instruction->align_value->other, &align_bytes))
1546715553 return ira->codegen->builtin_types.entry_invalid;
......@@ -15471,12 +15557,6 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1547115557 if (type_is_invalid(child_type))
1547215558 return ira->codegen->builtin_types.entry_invalid;
1547315559
15474 if (slice_type_instruction->align_value == nullptr) {
15475 if ((err = type_ensure_zero_bits_known(ira->codegen, child_type)))
15476 return ira->codegen->builtin_types.entry_invalid;
15477 align_bytes = get_abi_alignment(ira->codegen, child_type);
15478 }
15479
1548015560 bool is_const = slice_type_instruction->is_const;
1548115561 bool is_volatile = slice_type_instruction->is_volatile;
1548215562
......@@ -15511,7 +15591,7 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1551115591 case ZigTypeIdBoundFn:
1551215592 case ZigTypeIdPromise:
1551315593 {
15514 if ((err = type_ensure_zero_bits_known(ira->codegen, child_type)))
15594 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
1551515595 return ira->codegen->builtin_types.entry_invalid;
1551615596 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
1551715597 is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0);
......@@ -15751,9 +15831,7 @@ static ZigType *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
1575115831 }
1575215832 ZigType *child_type = type_entry->data.maybe.child_type;
1575315833 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, child_type,
15754 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
15755 PtrLenSingle,
15756 get_abi_alignment(ira->codegen, child_type), 0, 0);
15834 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile, PtrLenSingle, 0, 0, 0);
1575715835
1575815836 if (instr_is_comptime(value)) {
1575915837 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
......@@ -16123,7 +16201,7 @@ static ZigType *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1612316201 return tag_type;
1612416202 }
1612516203 case ZigTypeIdEnum: {
16126 if ((err = type_ensure_zero_bits_known(ira->codegen, target_type)))
16204 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
1612716205 return ira->codegen->builtin_types.entry_invalid;
1612816206 if (target_type->data.enumeration.src_field_count < 2) {
1612916207 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
......@@ -16352,7 +16430,7 @@ static ZigType *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrInstruc
1635216430 if (casted_field_value == ira->codegen->invalid_instruction)
1635316431 return ira->codegen->builtin_types.entry_invalid;
1635416432
16355 if ((err = type_ensure_zero_bits_known(ira->codegen, casted_field_value->value.type)))
16433 if ((err = type_resolve(ira->codegen, casted_field_value->value.type, ResolveStatusZeroBitsKnown)))
1635616434 return ira->codegen->builtin_types.entry_invalid;
1635716435
1635816436 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);
......@@ -16752,7 +16830,7 @@ static ZigType *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstructionErr
1675216830 return ira->codegen->builtin_types.entry_invalid;
1675316831
1675416832 ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
16755 true, false, PtrLenUnknown, get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8), 0, 0);
16833 true, false, PtrLenUnknown, 0, 0, 0);
1675616834 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
1675716835 if (casted_value->value.special == ConstValSpecialStatic) {
1675816836 ErrorTableEntry *err = casted_value->value.data.x_err_set;
......@@ -16779,7 +16857,7 @@ static ZigType *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstructi
1677916857 assert(target->value.type->id == ZigTypeIdEnum);
1678016858
1678116859 if (instr_is_comptime(target)) {
16782 if ((err = type_ensure_zero_bits_known(ira->codegen, target->value.type)))
16860 if ((err = type_resolve(ira->codegen, target->value.type, ResolveStatusZeroBitsKnown)))
1678316861 return ira->codegen->builtin_types.entry_invalid;
1678416862 TypeEnumField *field = find_enum_field_by_tag(target->value.type, &target->value.data.x_bigint);
1678516863 ConstExprValue *array_val = create_const_str_lit(ira->codegen, field->name);
......@@ -16794,8 +16872,7 @@ static ZigType *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstructi
1679416872 ZigType *u8_ptr_type = get_pointer_to_type_extra(
1679516873 ira->codegen, ira->codegen->builtin_types.entry_u8,
1679616874 true, false, PtrLenUnknown,
16797 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8),
16798 0, 0);
16875 0, 0, 0);
1679916876 result->value.type = get_slice_type(ira->codegen, u8_ptr_type);
1680016877 return result->value.type;
1680116878}
......@@ -17158,8 +17235,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
1715817235 ZigType *u8_ptr = get_pointer_to_type_extra(
1715917236 ira->codegen, ira->codegen->builtin_types.entry_u8,
1716017237 true, false, PtrLenUnknown,
17161 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8),
17162 0, 0);
17238 0, 0, 0);
1716317239 fn_def_fields[6].type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));
1716417240 if (fn_node->is_extern && buf_len(fn_node->lib_name) > 0) {
1716517241 fn_def_fields[6].data.x_optional = create_const_vals(1);
......@@ -17279,7 +17355,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
1727917355 ensure_field_index(result->type, "alignment", 3);
1728017356 fields[3].special = ConstValSpecialStatic;
1728117357 fields[3].type = get_int_type(ira->codegen, false, 29);
17282 bigint_init_unsigned(&fields[3].data.x_bigint, attrs_type->data.pointer.alignment);
17358 bigint_init_unsigned(&fields[3].data.x_bigint, get_ptr_align(ira->codegen, attrs_type));
1728317359 // child: type
1728417360 ensure_field_index(result->type, "child", 4);
1728517361 fields[4].special = ConstValSpecialStatic;
......@@ -18369,7 +18445,21 @@ static ZigType *ir_analyze_instruction_err_set_cast(IrAnalyze *ira, IrInstructio
1836918445 return dest_type;
1837018446}
1837118447
18448static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align) {
18449 Error err;
18450
18451 if (ty->id == ZigTypeIdPointer) {
18452 if ((err = type_resolve(ira->codegen, ty->data.pointer.child_type, ResolveStatusAlignmentKnown)))
18453 return err;
18454 }
18455
18456 *result_align = get_ptr_align(ira->codegen, ty);
18457 return ErrorNone;
18458}
18459
1837218460static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionFromBytes *instruction) {
18461 Error err;
18462
1837318463 ZigType *dest_child_type = ir_resolve_type(ira, instruction->dest_child_type->other);
1837418464 if (type_is_invalid(dest_child_type))
1837518465 return ira->codegen->builtin_types.entry_invalid;
......@@ -18384,15 +18474,23 @@ static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionF
1838418474 if (target->value.type->id == ZigTypeIdPointer) {
1838518475 src_ptr_const = target->value.type->data.pointer.is_const;
1838618476 src_ptr_volatile = target->value.type->data.pointer.is_volatile;
18387 src_ptr_align = target->value.type->data.pointer.alignment;
18477
18478 if ((err = resolve_ptr_align(ira, target->value.type, &src_ptr_align)))
18479 return ira->codegen->builtin_types.entry_invalid;
1838818480 } else if (is_slice(target->value.type)) {
1838918481 ZigType *src_ptr_type = target->value.type->data.structure.fields[slice_ptr_index].type_entry;
1839018482 src_ptr_const = src_ptr_type->data.pointer.is_const;
1839118483 src_ptr_volatile = src_ptr_type->data.pointer.is_volatile;
18392 src_ptr_align = src_ptr_type->data.pointer.alignment;
18484
18485 if ((err = resolve_ptr_align(ira, src_ptr_type, &src_ptr_align)))
18486 return ira->codegen->builtin_types.entry_invalid;
1839318487 } else {
1839418488 src_ptr_const = true;
1839518489 src_ptr_volatile = false;
18490
18491 if ((err = type_resolve(ira->codegen, target->value.type, ResolveStatusAlignmentKnown)))
18492 return ira->codegen->builtin_types.entry_invalid;
18493
1839618494 src_ptr_align = get_abi_alignment(ira->codegen, target->value.type);
1839718495 }
1839818496
......@@ -18450,6 +18548,8 @@ static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionF
1845018548}
1845118549
1845218550static ZigType *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToBytes *instruction) {
18551 Error err;
18552
1845318553 IrInstruction *target = instruction->target->other;
1845418554 if (type_is_invalid(target->value.type))
1845518555 return ira->codegen->builtin_types.entry_invalid;
......@@ -18462,9 +18562,13 @@ static ZigType *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToB
1846218562
1846318563 ZigType *src_ptr_type = target->value.type->data.structure.fields[slice_ptr_index].type_entry;
1846418564
18565 uint32_t alignment;
18566 if ((err = resolve_ptr_align(ira, src_ptr_type, &alignment)))
18567 return ira->codegen->builtin_types.entry_invalid;
18568
1846518569 ZigType *dest_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
1846618570 src_ptr_type->data.pointer.is_const, src_ptr_type->data.pointer.is_volatile, PtrLenUnknown,
18467 src_ptr_type->data.pointer.alignment, 0, 0);
18571 alignment, 0, 0);
1846818572 ZigType *dest_slice_type = get_slice_type(ira->codegen, dest_ptr_type);
1846918573
1847018574 IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_slice_type, CastOpResizeSlice, true);
......@@ -18622,6 +18726,8 @@ static ZigType *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstructionBoo
1862218726}
1862318727
1862418728static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemset *instruction) {
18729 Error err;
18730
1862518731 IrInstruction *dest_ptr = instruction->dest_ptr->other;
1862618732 if (type_is_invalid(dest_ptr->value.type))
1862718733 return ira->codegen->builtin_types.entry_invalid;
......@@ -18640,8 +18746,13 @@ static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemse
1864018746
1864118747 ZigType *usize = ira->codegen->builtin_types.entry_usize;
1864218748 ZigType *u8 = ira->codegen->builtin_types.entry_u8;
18643 uint32_t dest_align = (dest_uncasted_type->id == ZigTypeIdPointer) ?
18644 dest_uncasted_type->data.pointer.alignment : get_abi_alignment(ira->codegen, u8);
18749 uint32_t dest_align;
18750 if (dest_uncasted_type->id == ZigTypeIdPointer) {
18751 if ((err = resolve_ptr_align(ira, dest_uncasted_type, &dest_align)))
18752 return ira->codegen->builtin_types.entry_invalid;
18753 } else {
18754 dest_align = get_abi_alignment(ira->codegen, u8);
18755 }
1864518756 ZigType *u8_ptr = get_pointer_to_type_extra(ira->codegen, u8, false, dest_is_volatile,
1864618757 PtrLenUnknown, dest_align, 0, 0);
1864718758
......@@ -18714,6 +18825,8 @@ static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemse
1871418825}
1871518826
1871618827static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcpy *instruction) {
18828 Error err;
18829
1871718830 IrInstruction *dest_ptr = instruction->dest_ptr->other;
1871818831 if (type_is_invalid(dest_ptr->value.type))
1871918832 return ira->codegen->builtin_types.entry_invalid;
......@@ -18733,10 +18846,22 @@ static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcp
1873318846 dest_uncasted_type->data.pointer.is_volatile;
1873418847 bool src_is_volatile = (src_uncasted_type->id == ZigTypeIdPointer) &&
1873518848 src_uncasted_type->data.pointer.is_volatile;
18736 uint32_t dest_align = (dest_uncasted_type->id == ZigTypeIdPointer) ?
18737 dest_uncasted_type->data.pointer.alignment : get_abi_alignment(ira->codegen, u8);
18738 uint32_t src_align = (src_uncasted_type->id == ZigTypeIdPointer) ?
18739 src_uncasted_type->data.pointer.alignment : get_abi_alignment(ira->codegen, u8);
18849
18850 uint32_t dest_align;
18851 if (dest_uncasted_type->id == ZigTypeIdPointer) {
18852 if ((err = resolve_ptr_align(ira, dest_uncasted_type, &dest_align)))
18853 return ira->codegen->builtin_types.entry_invalid;
18854 } else {
18855 dest_align = get_abi_alignment(ira->codegen, u8);
18856 }
18857
18858 uint32_t src_align;
18859 if (src_uncasted_type->id == ZigTypeIdPointer) {
18860 if ((err = resolve_ptr_align(ira, src_uncasted_type, &src_align)))
18861 return ira->codegen->builtin_types.entry_invalid;
18862 } else {
18863 src_align = get_abi_alignment(ira->codegen, u8);
18864 }
1874018865
1874118866 ZigType *usize = ira->codegen->builtin_types.entry_usize;
1874218867 ZigType *u8_ptr_mut = get_pointer_to_type_extra(ira->codegen, u8, false, dest_is_volatile,
......@@ -18881,17 +19006,13 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
1888119006 ZigType *return_type;
1888219007
1888319008 if (array_type->id == ZigTypeIdArray) {
18884 uint32_t byte_alignment = ptr_type->data.pointer.alignment;
18885 if (array_type->data.array.len == 0 && byte_alignment == 0) {
18886 byte_alignment = get_abi_alignment(ira->codegen, array_type->data.array.child_type);
18887 }
1888819009 bool is_comptime_const = ptr_ptr->value.special == ConstValSpecialStatic &&
1888919010 ptr_ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst;
1889019011 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.array.child_type,
1889119012 ptr_type->data.pointer.is_const || is_comptime_const,
1889219013 ptr_type->data.pointer.is_volatile,
1889319014 PtrLenUnknown,
18894 byte_alignment, 0, 0);
19015 ptr_type->data.pointer.explicit_alignment, 0, 0);
1889519016 return_type = get_slice_type(ira->codegen, slice_ptr_type);
1889619017 } else if (array_type->id == ZigTypeIdPointer) {
1889719018 if (array_type->data.pointer.ptr_len == PtrLenSingle) {
......@@ -18901,7 +19022,7 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
1890119022 main_type->data.pointer.child_type,
1890219023 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
1890319024 PtrLenUnknown,
18904 array_type->data.pointer.alignment, 0, 0);
19025 array_type->data.pointer.explicit_alignment, 0, 0);
1890519026 return_type = get_slice_type(ira->codegen, slice_ptr_type);
1890619027 } else {
1890719028 ir_add_error(ira, &instruction->base, buf_sprintf("slice of single-item pointer"));
......@@ -18911,7 +19032,7 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
1891119032 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,
1891219033 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
1891319034 PtrLenUnknown,
18914 array_type->data.pointer.alignment, 0, 0);
19035 array_type->data.pointer.explicit_alignment, 0, 0);
1891519036 return_type = get_slice_type(ira->codegen, slice_ptr_type);
1891619037 if (!end) {
1891719038 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));
......@@ -19292,7 +19413,7 @@ static ZigType *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAli
1929219413 return ira->codegen->builtin_types.entry_invalid;
1929319414 ZigType *type_entry = ir_resolve_type(ira, type_value);
1929419415
19295 if ((err = type_ensure_zero_bits_known(ira->codegen, type_entry)))
19416 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusAlignmentKnown)))
1929619417 return ira->codegen->builtin_types.entry_invalid;
1929719418
1929819419 switch (type_entry->id) {
......@@ -19336,6 +19457,8 @@ static ZigType *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAli
1933619457}
1933719458
1933819459static ZigType *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {
19460 Error err;
19461
1933919462 IrInstruction *type_value = instruction->type_value->other;
1934019463 if (type_is_invalid(type_value->value.type))
1934119464 return ira->codegen->builtin_types.entry_invalid;
......@@ -19379,10 +19502,13 @@ static ZigType *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstruction
1937919502
1938019503 ZigType *expected_ptr_type;
1938119504 if (result_ptr->value.type->id == ZigTypeIdPointer) {
19505 uint32_t alignment;
19506 if ((err = resolve_ptr_align(ira, result_ptr->value.type, &alignment)))
19507 return ira->codegen->builtin_types.entry_invalid;
1938219508 expected_ptr_type = get_pointer_to_type_extra(ira->codegen, dest_type,
1938319509 false, result_ptr->value.type->data.pointer.is_volatile,
1938419510 PtrLenSingle,
19385 result_ptr->value.type->data.pointer.alignment, 0, 0);
19511 alignment, 0, 0);
1938619512 } else {
1938719513 expected_ptr_type = get_pointer_to_type(ira->codegen, dest_type, false);
1938819514 }
......@@ -19544,8 +19670,7 @@ static ZigType *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
1954419670 }
1954519671 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, payload_type,
1954619672 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
19547 PtrLenSingle,
19548 get_abi_alignment(ira->codegen, payload_type), 0, 0);
19673 PtrLenSingle, 0, 0, 0);
1954919674 if (instr_is_comptime(value)) {
1955019675 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
1955119676 if (!ptr_val)
......@@ -19624,7 +19749,7 @@ static ZigType *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnP
1962419749 ZigType *param_type = ir_resolve_type(ira, param_type_value);
1962519750 if (type_is_invalid(param_type))
1962619751 return ira->codegen->builtin_types.entry_invalid;
19627 if ((err = type_ensure_zero_bits_known(ira->codegen, param_type)))
19752 if ((err = type_resolve(ira->codegen, param_type, ResolveStatusZeroBitsKnown)))
1962819753 return ira->codegen->builtin_types.entry_invalid;
1962919754 if (type_requires_comptime(param_type)) {
1963019755 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
......@@ -19899,7 +20024,7 @@ static ZigType *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructionPanic
1989920024 }
1990020025
1990120026 ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
19902 true, false, PtrLenUnknown, get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8), 0, 0);
20027 true, false, PtrLenUnknown, 0, 0, 0);
1990320028 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
1990420029 IrInstruction *casted_msg = ir_implicit_cast(ira, msg, str_type);
1990520030 if (type_is_invalid(casted_msg->value.type))
......@@ -19912,6 +20037,8 @@ static ZigType *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructionPanic
1991220037}
1991320038
1991420039static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint32_t align_bytes, bool safety_check_on) {
20040 Error err;
20041
1991520042 ZigType *target_type = target->value.type;
1991620043 assert(!type_is_invalid(target_type));
1991720044
......@@ -19920,7 +20047,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
1992020047
1992120048 if (target_type->id == ZigTypeIdPointer) {
1992220049 result_type = adjust_ptr_align(ira->codegen, target_type, align_bytes);
19923 old_align_bytes = target_type->data.pointer.alignment;
20050 if ((err = resolve_ptr_align(ira, target_type, &old_align_bytes)))
20051 return ira->codegen->invalid_instruction;
1992420052 } else if (target_type->id == ZigTypeIdFn) {
1992520053 FnTypeId fn_type_id = target_type->data.fn.fn_type_id;
1992620054 old_align_bytes = fn_type_id.alignment;
......@@ -19930,7 +20058,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
1993020058 target_type->data.maybe.child_type->id == ZigTypeIdPointer)
1993120059 {
1993220060 ZigType *ptr_type = target_type->data.maybe.child_type;
19933 old_align_bytes = ptr_type->data.pointer.alignment;
20061 if ((err = resolve_ptr_align(ira, ptr_type, &old_align_bytes)))
20062 return ira->codegen->invalid_instruction;
1993420063 ZigType *better_ptr_type = adjust_ptr_align(ira->codegen, ptr_type, align_bytes);
1993520064
1993620065 result_type = get_optional_type(ira->codegen, better_ptr_type);
......@@ -19944,7 +20073,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
1994420073 result_type = get_optional_type(ira->codegen, fn_type);
1994520074 } else if (is_slice(target_type)) {
1994620075 ZigType *slice_ptr_type = target_type->data.structure.fields[slice_ptr_index].type_entry;
19947 old_align_bytes = slice_ptr_type->data.pointer.alignment;
20076 if ((err = resolve_ptr_align(ira, slice_ptr_type, &old_align_bytes)))
20077 return ira->codegen->invalid_instruction;
1994820078 ZigType *result_ptr_type = adjust_ptr_align(ira->codegen, slice_ptr_type, align_bytes);
1994920079 result_type = get_slice_type(ira->codegen, result_ptr_type);
1995020080 } else {
......@@ -20023,8 +20153,13 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr
2002320153 return dest_type;
2002420154 }
2002520155
20026 uint32_t src_align_bytes = get_ptr_align(ira->codegen, src_type);
20027 uint32_t dest_align_bytes = get_ptr_align(ira->codegen, dest_type);
20156 uint32_t src_align_bytes;
20157 if ((err = resolve_ptr_align(ira, src_type, &src_align_bytes)))
20158 return ira->codegen->builtin_types.entry_invalid;
20159
20160 uint32_t dest_align_bytes;
20161 if ((err = resolve_ptr_align(ira, dest_type, &dest_align_bytes)))
20162 return ira->codegen->builtin_types.entry_invalid;
2002820163
2002920164 if (dest_align_bytes > src_align_bytes) {
2003020165 ErrorMsg *msg = ir_add_error(ira, &instruction->base, buf_sprintf("cast increases pointer alignment"));
......@@ -20041,7 +20176,7 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr
2004120176
2004220177 // Keep the bigger alignment, it can only help-
2004320178 // unless the target is zero bits.
20044 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))
20179 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
2004520180 return ira->codegen->builtin_types.entry_invalid;
2004620181
2004720182 IrInstruction *result;
......@@ -20289,7 +20424,7 @@ static ZigType *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstructionI
2028920424 return ira->codegen->builtin_types.entry_invalid;
2029020425 }
2029120426
20292 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))
20427 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
2029320428 return ira->codegen->builtin_types.entry_invalid;
2029420429 if (!type_has_bits(dest_type)) {
2029520430 ir_add_error(ira, dest_type_value,
......@@ -20440,12 +20575,15 @@ static ZigType *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtr
2044020575 if (instruction->align_value != nullptr) {
2044120576 if (!ir_resolve_align(ira, instruction->align_value->other, &align_bytes))
2044220577 return ira->codegen->builtin_types.entry_invalid;
20578 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusAlignmentKnown)))
20579 return ira->codegen->builtin_types.entry_invalid;
2044320580 } else {
20444 if ((err = type_ensure_zero_bits_known(ira->codegen, child_type)))
20581 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
2044520582 return ira->codegen->builtin_types.entry_invalid;
20446 align_bytes = get_abi_alignment(ira->codegen, child_type);
20583 align_bytes = 0;
2044720584 }
2044820585
20586
2044920587 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
2045020588 out_val->data.x_type = get_pointer_to_type_extra(ira->codegen, child_type,
2045120589 instruction->is_const, instruction->is_volatile,
......@@ -21089,7 +21227,7 @@ static ZigType *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstruction
2108921227 return ira->codegen->builtin_types.entry_invalid;
2109021228 }
2109121229
21092 if ((err = type_ensure_zero_bits_known(ira->codegen, target->value.type)))
21230 if ((err = type_resolve(ira->codegen, target->value.type, ResolveStatusZeroBitsKnown)))
2109321231 return ira->codegen->builtin_types.entry_invalid;
2109421232
2109521233 ZigType *tag_type = target->value.type->data.enumeration.tag_int_type;
......@@ -21112,7 +21250,7 @@ static ZigType *ir_analyze_instruction_int_to_enum(IrAnalyze *ira, IrInstruction
2111221250 return ira->codegen->builtin_types.entry_invalid;
2111321251 }
2111421252
21115 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))
21253 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
2111621254 return ira->codegen->builtin_types.entry_invalid;
2111721255
2111821256 ZigType *tag_type = dest_type->data.enumeration.tag_int_type;
std/debug/index.zig+3-3
......@@ -916,7 +916,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
916916 } else {
917917 return error.MissingDebugInfo;
918918 };
919 const syms = @ptrCast([*]macho.nlist_64, hdr_base + symtab.symoff)[0..symtab.nsyms];
919 const syms = @ptrCast([*]macho.nlist_64, @alignCast(@alignOf(macho.nlist_64), hdr_base + symtab.symoff))[0..symtab.nsyms];
920920 const strings = @ptrCast([*]u8, hdr_base + symtab.stroff)[0..symtab.strsize];
921921
922922 const symbols_buf = try allocator.alloc(MachoSymbol, syms.len);
......@@ -1497,14 +1497,14 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
14971497 const segcmd = while (ncmd != 0) : (ncmd -= 1) {
14981498 const lc = @ptrCast(*const std.macho.load_command, ptr);
14991499 switch (lc.cmd) {
1500 std.macho.LC_SEGMENT_64 => break @ptrCast(*const std.macho.segment_command_64, ptr),
1500 std.macho.LC_SEGMENT_64 => break @ptrCast(*const std.macho.segment_command_64, @alignCast(@alignOf(std.macho.segment_command_64), ptr)),
15011501 else => {},
15021502 }
15031503 ptr += lc.cmdsize; // TODO https://github.com/ziglang/zig/issues/1403
15041504 } else {
15051505 return error.MissingDebugInfo;
15061506 };
1507 const sections = @alignCast(@alignOf(macho.section_64), @ptrCast([*]const macho.section_64, ptr + @sizeOf(std.macho.segment_command_64)))[0..segcmd.nsects];
1507 const sections = @ptrCast([*]const macho.section_64, @alignCast(@alignOf(macho.section_64), ptr + @sizeOf(std.macho.segment_command_64)))[0..segcmd.nsects];
15081508 for (sections) |*sect| {
15091509 if (sect.flags & macho.SECTION_TYPE == macho.S_REGULAR and
15101510 (sect.flags & macho.SECTION_ATTRIBUTES) & macho.S_ATTR_DEBUG == macho.S_ATTR_DEBUG)
test/cases/align.zig+7
......@@ -212,3 +212,10 @@ fn fnWithAlignedStack() i32 {
212212 @setAlignStack(256);
213213 return 1234;
214214}
215
216test "alignment of structs" {
217 assert(@alignOf(struct {
218 a: i32,
219 b: *i32,
220 }) == @alignOf(usize));
221}
test/compile_errors.zig+1-1
......@@ -3444,7 +3444,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
34443444 \\
34453445 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
34463446 ,
3447 ".tmp_source.zig:8:26: error: expected type '*const u3', found '*align(1:3:6) const u3'",
3447 ".tmp_source.zig:8:26: error: expected type '*const u3', found '*align(:3:6) const u3'",
34483448 );
34493449
34503450 cases.add(