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 {...@@ -1012,13 +1012,13 @@ enum PtrLen {
10121012
1013struct ZigTypePointer {1013struct ZigTypePointer {
1014 ZigType *child_type;1014 ZigType *child_type;
1015 ZigType *slice_parent;
1015 PtrLen ptr_len;1016 PtrLen ptr_len;
1016 bool is_const;1017 uint32_t explicit_alignment; // 0 means use ABI alignment
1017 bool is_volatile;
1018 uint32_t alignment;
1019 uint32_t bit_offset;1018 uint32_t bit_offset;
1020 uint32_t unaligned_bit_count;1019 uint32_t unaligned_bit_count;
1021 ZigType *slice_parent;1020 bool is_const;
1021 bool is_volatile;
1022};1022};
10231023
1024struct ZigTypeInt {1024struct ZigTypeInt {
...@@ -1046,32 +1046,35 @@ struct TypeStructField {...@@ -1046,32 +1046,35 @@ struct TypeStructField {
1046 size_t unaligned_bit_count;1046 size_t unaligned_bit_count;
1047 AstNode *decl_node;1047 AstNode *decl_node;
1048};1048};
1049
1050enum ResolveStatus {
1051 ResolveStatusUnstarted,
1052 ResolveStatusInvalid,
1053 ResolveStatusZeroBitsKnown,
1054 ResolveStatusAlignmentKnown,
1055 ResolveStatusSizeKnown,
1056};
1057
1049struct ZigTypeStruct {1058struct ZigTypeStruct {
1050 AstNode *decl_node;1059 AstNode *decl_node;
1051 ContainerLayout layout;
1052 uint32_t src_field_count;
1053 uint32_t gen_field_count;
1054 TypeStructField *fields;1060 TypeStructField *fields;
1055 uint64_t size_bytes;
1056 bool is_invalid; // true if any fields are invalid
1057 bool is_slice;
1058 ScopeDecls *decls_scope;1061 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 loops1065 uint32_t src_field_count;
1061 bool embedded_in_current;1066 uint32_t gen_field_count;
1062 bool reported_infinite_err;1067
1063 // whether we've finished resolving it1068 uint32_t abi_alignment; // known after ResolveStatusAlignmentKnown
1064 bool complete;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;
1066 // whether any of the fields require comptime1075 // whether any of the fields require comptime
1067 // the value is not valid until zero_bits_known == true1076 // known after ResolveStatusZeroBitsKnown
1068 bool requires_comptime;1077 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;
1075};1078};
10761079
1077struct ZigTypeOptional {1080struct ZigTypeOptional {
src/analyze.cpp+218-169
...@@ -23,6 +23,7 @@ static Error resolve_enum_type(CodeGen *g, ZigType *enum_type);...@@ -23,6 +23,7 @@ static Error resolve_enum_type(CodeGen *g, ZigType *enum_type);
23static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);23static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);
2424
25static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);25static 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);
26static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type);27static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type);
27static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);28static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);
28static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);29static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
...@@ -254,18 +255,42 @@ AstNode *type_decl_node(ZigType *type_entry) {...@@ -254,18 +255,42 @@ AstNode *type_decl_node(ZigType *type_entry) {
254 zig_unreachable();255 zig_unreachable();
255}256}
256257
257bool type_is_complete(ZigType *type_entry) {258bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
258 switch (type_entry->id) {259 switch (type_entry->id) {
259 case ZigTypeIdInvalid:260 case ZigTypeIdInvalid:
260 zig_unreachable();261 zig_unreachable();
261 case ZigTypeIdStruct:262 case ZigTypeIdStruct:
262 return type_entry->data.structure.complete;263 return type_entry->data.structure.resolve_status >= status;
263 case ZigTypeIdEnum:264 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();
265 case ZigTypeIdUnion:278 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();
267 case ZigTypeIdOpaque:292 case ZigTypeIdOpaque:
268 return false;293 return status < ResolveStatusSizeKnown;
269 case ZigTypeIdMetaType:294 case ZigTypeIdMetaType:
270 case ZigTypeIdVoid:295 case ZigTypeIdVoid:
271 case ZigTypeIdBool:296 case ZigTypeIdBool:
...@@ -291,43 +316,10 @@ bool type_is_complete(ZigType *type_entry) {...@@ -291,43 +316,10 @@ bool type_is_complete(ZigType *type_entry) {
291 zig_unreachable();316 zig_unreachable();
292}317}
293318
294bool type_has_zero_bits_known(ZigType *type_entry) {319bool type_is_complete(ZigType *type_entry) {
295 switch (type_entry->id) {320 return type_is_resolved(type_entry, ResolveStatusSizeKnown);
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();
328}321}
329322
330
331uint64_t type_size(CodeGen *g, ZigType *type_entry) {323uint64_t type_size(CodeGen *g, ZigType *type_entry) {
332 assert(type_is_complete(type_entry));324 assert(type_is_complete(type_entry));
333325
...@@ -376,7 +368,7 @@ uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {...@@ -376,7 +368,7 @@ uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {
376368
377Result<bool> type_is_copyable(CodeGen *g, ZigType *type_entry) {369Result<bool> type_is_copyable(CodeGen *g, ZigType *type_entry) {
378 Error err;370 Error err;
379 if ((err = type_ensure_zero_bits_known(g, type_entry)))371 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
380 return err;372 return err;
381373
382 if (!type_has_bits(type_entry))374 if (!type_has_bits(type_entry))
...@@ -431,10 +423,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons...@@ -431,10 +423,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
431 assert(!type_is_invalid(child_type));423 assert(!type_is_invalid(child_type));
432 assert(ptr_len == PtrLenSingle || child_type->id != ZigTypeIdOpaque);424 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
434 TypeId type_id = {};432 TypeId type_id = {};
435 ZigType **parent_pointer = nullptr;433 ZigType **parent_pointer = nullptr;
436 uint32_t abi_alignment = get_abi_alignment(g, child_type);434 if (unaligned_bit_count != 0 || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle) {
437 if (unaligned_bit_count != 0 || is_volatile || byte_alignment != abi_alignment || ptr_len != PtrLenSingle) {
438 type_id.id = ZigTypeIdPointer;435 type_id.id = ZigTypeIdPointer;
439 type_id.data.pointer.child_type = child_type;436 type_id.data.pointer.child_type = child_type;
440 type_id.data.pointer.is_const = is_const;437 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...@@ -451,12 +448,12 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
451 assert(bit_offset == 0);448 assert(bit_offset == 0);
452 parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)];449 parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)];
453 if (*parent_pointer) {450 if (*parent_pointer) {
454 assert((*parent_pointer)->data.pointer.alignment == byte_alignment);451 assert((*parent_pointer)->data.pointer.explicit_alignment == 0);
455 return *parent_pointer;452 return *parent_pointer;
456 }453 }
457 }454 }
458455
459 assertNoError(type_ensure_zero_bits_known(g, child_type));456 assert(type_is_resolved(child_type, ResolveStatusZeroBitsKnown));
460457
461 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);458 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);
462 entry->is_copyable = true;459 entry->is_copyable = true;
...@@ -465,11 +462,14 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons...@@ -465,11 +462,14 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
465 const char *const_str = is_const ? "const " : "";462 const char *const_str = is_const ? "const " : "";
466 const char *volatile_str = is_volatile ? "volatile " : "";463 const char *volatile_str = is_volatile ? "volatile " : "";
467 buf_resize(&entry->name, 0);464 buf_resize(&entry->name, 0);
468 if (unaligned_bit_count == 0 && byte_alignment == abi_alignment) {465 if (unaligned_bit_count == 0 && byte_alignment == 0) {
469 buf_appendf(&entry->name, "%s%s%s%s", star_str, const_str, volatile_str, buf_ptr(&child_type->name));466 buf_appendf(&entry->name, "%s%s%s%s", star_str, const_str, volatile_str, buf_ptr(&child_type->name));
470 } else if (unaligned_bit_count == 0) {467 } else if (unaligned_bit_count == 0) {
471 buf_appendf(&entry->name, "%salign(%" PRIu32 ") %s%s%s", star_str, byte_alignment,468 buf_appendf(&entry->name, "%salign(%" PRIu32 ") %s%s%s", star_str, byte_alignment,
472 const_str, volatile_str, buf_ptr(&child_type->name));469 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));
473 } else {473 } else {
474 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s", star_str, byte_alignment,474 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s", star_str, byte_alignment,
475 bit_offset, bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));475 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...@@ -480,8 +480,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
480 entry->zero_bits = !type_has_bits(child_type);480 entry->zero_bits = !type_has_bits(child_type);
481481
482 if (!entry->zero_bits) {482 if (!entry->zero_bits) {
483 assert(byte_alignment > 0);483 if (is_const || is_volatile || unaligned_bit_count != 0 || byte_alignment != 0 ||
484 if (is_const || is_volatile || unaligned_bit_count != 0 || byte_alignment != abi_alignment ||
485 ptr_len != PtrLenSingle)484 ptr_len != PtrLenSingle)
486 {485 {
487 ZigType *peer_type = get_pointer_to_type(g, child_type, false);486 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...@@ -505,7 +504,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
505 entry->data.pointer.child_type = child_type;504 entry->data.pointer.child_type = child_type;
506 entry->data.pointer.is_const = is_const;505 entry->data.pointer.is_const = is_const;
507 entry->data.pointer.is_volatile = is_volatile;506 entry->data.pointer.is_volatile = is_volatile;
508 entry->data.pointer.alignment = byte_alignment;507 entry->data.pointer.explicit_alignment = byte_alignment;
509 entry->data.pointer.bit_offset = bit_offset;508 entry->data.pointer.bit_offset = bit_offset;
510 entry->data.pointer.unaligned_bit_count = unaligned_bit_count;509 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...@@ -518,8 +517,7 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
518}517}
519518
520ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const) {519ZigType *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,520 return get_pointer_to_type_extra(g, child_type, is_const, false, PtrLenSingle, 0, 0, 0);
522 get_abi_alignment(g, child_type), 0, 0);
523}521}
524522
525ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {523ZigType *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...@@ -800,8 +798,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
800 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);798 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
801 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);799 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));801 if (!type_has_bits(pointer_type->data.pointer.child_type)) {
804 if (pointer_type->data.pointer.child_type->zero_bits) {
805 entry->data.structure.gen_field_count = 1;802 entry->data.structure.gen_field_count = 1;
806 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;803 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;
807 entry->data.structure.fields[slice_len_index].gen_index = 0;804 entry->data.structure.fields[slice_len_index].gen_index = 0;
...@@ -826,20 +823,18 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {...@@ -826,20 +823,18 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
826 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);823 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
827824
828 ZigType *child_type = ptr_type->data.pointer.child_type;825 ZigType *child_type = ptr_type->data.pointer.child_type;
829 uint32_t abi_alignment = get_abi_alignment(g, child_type);
830 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||826 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)
832 {828 {
833 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,829 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);
835 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);831 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
836832
837 slice_type_common_init(g, ptr_type, entry);833 slice_type_common_init(g, ptr_type, entry);
838834
839 entry->type_ref = peer_slice_type->type_ref;835 entry->type_ref = peer_slice_type->type_ref;
840 entry->di_type = peer_slice_type->di_type;836 entry->di_type = peer_slice_type->di_type;
841 entry->data.structure.complete = true;837 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
842 entry->data.structure.zero_bits_known = true;
843 entry->data.structure.abi_alignment = peer_slice_type->data.structure.abi_alignment;838 entry->data.structure.abi_alignment = peer_slice_type->data.structure.abi_alignment;
844839
845 *parent_pointer = entry;840 *parent_pointer = entry;
...@@ -851,15 +846,15 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {...@@ -851,15 +846,15 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
851 if (is_slice(child_type)) {846 if (is_slice(child_type)) {
852 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;847 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
853 assert(child_ptr_type->id == ZigTypeIdPointer);848 assert(child_ptr_type->id == ZigTypeIdPointer);
854 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
855 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||849 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)
857 {851 {
852 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
858 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,853 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);
860 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);855 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
861 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,856 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);
863 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);858 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
864859
865 entry->type_ref = peer_slice_type->type_ref;860 entry->type_ref = peer_slice_type->type_ref;
...@@ -961,8 +956,7 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {...@@ -961,8 +956,7 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
961 }956 }
962957
963958
964 entry->data.structure.complete = true;959 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
965 entry->data.structure.zero_bits_known = true;
966960
967 *parent_pointer = entry;961 *parent_pointer = entry;
968 return entry;962 return entry;
...@@ -1367,7 +1361,7 @@ static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_...@@ -1367,7 +1361,7 @@ static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_
13671361
1368static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **out_buffer) {1362static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **out_buffer) {
1369 ZigType *ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,1363 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);
1371 ZigType *str_type = get_slice_type(g, ptr_type);1365 ZigType *str_type = get_slice_type(g, ptr_type);
1372 IrInstruction *instr = analyze_const_value(g, scope, node, str_type, nullptr);1366 IrInstruction *instr = analyze_const_value(g, scope, node, str_type, nullptr);
1373 if (type_is_invalid(instr->value.type))1367 if (type_is_invalid(instr->value.type))
...@@ -1576,7 +1570,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc...@@ -1576,7 +1570,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
1576 return g->builtin_types.entry_invalid;1570 return g->builtin_types.entry_invalid;
1577 }1571 }
1578 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {1572 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)))
1580 return g->builtin_types.entry_invalid;1574 return g->builtin_types.entry_invalid;
1581 if (!type_has_bits(type_entry)) {1575 if (!type_has_bits(type_entry)) {
1582 add_node_error(g, param_node->data.param_decl.type,1576 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...@@ -1624,7 +1618,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
1624 case ZigTypeIdUnion:1618 case ZigTypeIdUnion:
1625 case ZigTypeIdFn:1619 case ZigTypeIdFn:
1626 case ZigTypeIdPromise:1620 case ZigTypeIdPromise:
1627 if ((err = type_ensure_zero_bits_known(g, type_entry)))1621 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
1628 return g->builtin_types.entry_invalid;1622 return g->builtin_types.entry_invalid;
1629 if (type_requires_comptime(type_entry)) {1623 if (type_requires_comptime(type_entry)) {
1630 add_node_error(g, param_node->data.param_decl.type,1624 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...@@ -1714,7 +1708,7 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
1714 case ZigTypeIdUnion:1708 case ZigTypeIdUnion:
1715 case ZigTypeIdFn:1709 case ZigTypeIdFn:
1716 case ZigTypeIdPromise:1710 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)))
1718 return g->builtin_types.entry_invalid;1712 return g->builtin_types.entry_invalid;
1719 if (type_requires_comptime(fn_type_id.return_type)) {1713 if (type_requires_comptime(fn_type_id.return_type)) {
1720 return get_generic_fn_type(g, &fn_type_id);1714 return get_generic_fn_type(g, &fn_type_id);
...@@ -1740,7 +1734,7 @@ bool type_is_invalid(ZigType *type_entry) {...@@ -1740,7 +1734,7 @@ bool type_is_invalid(ZigType *type_entry) {
1740 case ZigTypeIdInvalid:1734 case ZigTypeIdInvalid:
1741 return true;1735 return true;
1742 case ZigTypeIdStruct:1736 case ZigTypeIdStruct:
1743 return type_entry->data.structure.is_invalid;1737 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;
1744 case ZigTypeIdEnum:1738 case ZigTypeIdEnum:
1745 return type_entry->data.enumeration.is_invalid;1739 return type_entry->data.enumeration.is_invalid;
1746 case ZigTypeIdUnion:1740 case ZigTypeIdUnion:
...@@ -1855,8 +1849,7 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na...@@ -1855,8 +1849,7 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
18551849
1856 struct_type->data.structure.src_field_count = field_count;1850 struct_type->data.structure.src_field_count = field_count;
1857 struct_type->data.structure.gen_field_count = 0;1851 struct_type->data.structure.gen_field_count = 0;
1858 struct_type->data.structure.zero_bits_known = true;1852 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1859 struct_type->data.structure.complete = true;
1860 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);1853 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
1861 struct_type->data.structure.fields_by_name.init(field_count);1854 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...@@ -1928,26 +1921,29 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
1928static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {1921static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1929 assert(struct_type->id == ZigTypeIdStruct);1922 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)
1932 return ErrorNone;1929 return ErrorNone;
19331930
1934 Error err;1931 if ((err = resolve_struct_alignment(g, struct_type)))
1935 if ((err = resolve_struct_zero_bits(g, struct_type)))
1936 return err;1932 return err;
19371933
1938 AstNode *decl_node = struct_type->data.structure.decl_node;1934 AstNode *decl_node = struct_type->data.structure.decl_node;
19391935
1940 if (struct_type->data.structure.embedded_in_current) {1936 if (struct_type->data.structure.resolve_loop_flag) {
1941 struct_type->data.structure.is_invalid = true;1937 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
1942 if (!struct_type->data.structure.reported_infinite_err) {1938 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1943 struct_type->data.structure.reported_infinite_err = true;
1944 add_node_error(g, decl_node,1939 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)));
1946 }1941 }
1947 return ErrorSemanticAnalyzeFail;1942 return ErrorSemanticAnalyzeFail;
1948 }1943 }
19491944
1950 assert(!struct_type->data.structure.zero_bits_loop_flag);1945 struct_type->data.structure.resolve_loop_flag = true;
1946
1951 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);1947 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
1952 assert(decl_node->type == NodeTypeContainerDecl);1948 assert(decl_node->type == NodeTypeContainerDecl);
19531949
...@@ -1956,9 +1952,6 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -1956,9 +1952,6 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1956 size_t gen_field_count = struct_type->data.structure.gen_field_count;1952 size_t gen_field_count = struct_type->data.structure.gen_field_count;
1957 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);1953 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
1962 Scope *scope = &struct_type->data.structure.decls_scope->base;1955 Scope *scope = &struct_type->data.structure.decls_scope->base;
19631956
1964 size_t gen_field_index = 0;1957 size_t gen_field_index = 0;
...@@ -1972,7 +1965,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -1972,7 +1965,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1972 ZigType *field_type = type_struct_field->type_entry;1965 ZigType *field_type = type_struct_field->type_entry;
19731966
1974 if ((err = ensure_complete_type(g, field_type))) {1967 if ((err = ensure_complete_type(g, field_type))) {
1975 struct_type->data.structure.is_invalid = true;1968 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1976 break;1969 break;
1977 }1970 }
19781971
...@@ -1982,7 +1975,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -1982,7 +1975,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1982 add_node_error(g, field_source_node,1975 add_node_error(g, field_source_node,
1983 buf_sprintf("extern structs cannot contain fields of type '%s'",1976 buf_sprintf("extern structs cannot contain fields of type '%s'",
1984 buf_ptr(&field_type->name)));1977 buf_ptr(&field_type->name)));
1985 struct_type->data.structure.is_invalid = true;1978 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1986 break;1979 break;
1987 }1980 }
1988 }1981 }
...@@ -1998,7 +1991,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -1998,7 +1991,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1998 add_node_error(g, field_source_node,1991 add_node_error(g, field_source_node,
1999 buf_sprintf("packed structs cannot contain fields of type '%s'",1992 buf_sprintf("packed structs cannot contain fields of type '%s'",
2000 buf_ptr(&field_type->name)));1993 buf_ptr(&field_type->name)));
2001 struct_type->data.structure.is_invalid = true;1994 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2002 break;1995 break;
2003 }1996 }
20041997
...@@ -2049,12 +2042,13 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -2049,12 +2042,13 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
2049 gen_field_index += 1;2042 gen_field_index += 1;
2050 }2043 }
20512044
2052 struct_type->data.structure.embedded_in_current = false;2045 struct_type->data.structure.resolve_loop_flag = false;
2053 struct_type->data.structure.complete = true;
20542046
2055 if (struct_type->data.structure.is_invalid)2047 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2056 return ErrorSemanticAnalyzeFail;2048 return ErrorSemanticAnalyzeFail;
20572049
2050 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
2051
2058 if (struct_type->zero_bits) {2052 if (struct_type->zero_bits) {
2059 struct_type->type_ref = LLVMVoidType();2053 struct_type->type_ref = LLVMVoidType();
20602054
...@@ -2116,7 +2110,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -2116,7 +2110,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
21162110
2117 assert(field_type->type_ref);2111 assert(field_type->type_ref);
2118 assert(struct_type->type_ref);2112 assert(struct_type->type_ref);
2119 assert(struct_type->data.structure.complete);2113 assert(struct_type->data.structure.resolve_status == ResolveStatusSizeKnown);
2120 uint64_t debug_size_in_bits;2114 uint64_t debug_size_in_bits;
2121 uint64_t debug_align_in_bits;2115 uint64_t debug_align_in_bits;
2122 uint64_t debug_offset_in_bits;2116 uint64_t debug_offset_in_bits;
...@@ -2570,30 +2564,18 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2570,30 +2564,18 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
25702564
2571 Error err;2565 Error err;
25722566
2573 if (struct_type->data.structure.is_invalid)2567 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2574 return ErrorSemanticAnalyzeFail;2568 return ErrorSemanticAnalyzeFail;
25752569 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
2576 if (struct_type->data.structure.zero_bits_known)
2577 return ErrorNone;2570 return ErrorNone;
25782571
2579 if (struct_type->data.structure.zero_bits_loop_flag) {2572 if (struct_type->data.structure.resolve_loop_flag) {
2580 // If we get here it's due to recursion. This is a design flaw in the compiler,2573 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
2581 // we should be able to still figure out alignment, but here we give up and say that2574 struct_type->data.structure.resolve_loop_flag = false;
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 }
2593 return ErrorNone;2575 return ErrorNone;
2594 }2576 }
25952577
2596 struct_type->data.structure.zero_bits_loop_flag = true;2578 struct_type->data.structure.resolve_loop_flag = true;
25972579
2598 AstNode *decl_node = struct_type->data.structure.decl_node;2580 AstNode *decl_node = struct_type->data.structure.decl_node;
2599 assert(decl_node->type == NodeTypeContainerDecl);2581 assert(decl_node->type == NodeTypeContainerDecl);
...@@ -2616,7 +2598,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2616,7 +2598,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26162598
2617 if (field_node->data.struct_field.type == nullptr) {2599 if (field_node->data.struct_field.type == nullptr) {
2618 add_node_error(g, field_node, buf_sprintf("struct field missing type"));2600 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;
2620 continue;2602 continue;
2621 }2603 }
26222604
...@@ -2625,7 +2607,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2625,7 +2607,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2625 ErrorMsg *msg = add_node_error(g, field_node,2607 ErrorMsg *msg = add_node_error(g, field_node,
2626 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));2608 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));
2627 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));2609 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;
2629 continue;2611 continue;
2630 }2612 }
26312613
...@@ -2639,8 +2621,8 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2639,8 +2621,8 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2639 buf_sprintf("enums, not structs, support field assignment"));2621 buf_sprintf("enums, not structs, support field assignment"));
2640 }2622 }
26412623
2642 if ((err = type_ensure_zero_bits_known(g, field_type))) {2624 if ((err = type_resolve(g, field_type, ResolveStatusZeroBitsKnown))) {
2643 struct_type->data.structure.is_invalid = true;2625 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2644 continue;2626 continue;
2645 }2627 }
26462628
...@@ -2651,36 +2633,87 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2651,36 +2633,87 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2651 if (!type_has_bits(field_type))2633 if (!type_has_bits(field_type))
2652 continue;2634 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
2671 type_struct_field->gen_index = gen_field_index;2636 type_struct_field->gen_index = gen_field_index;
2672 gen_field_index += 1;2637 gen_field_index += 1;
2673 }2638 }
26742639
2675 struct_type->data.structure.zero_bits_loop_flag = false;2640 struct_type->data.structure.resolve_loop_flag = false;
2676 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;2641 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
2677 struct_type->zero_bits = (gen_field_index == 0);2642 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 }
2681 return ErrorSemanticAnalyzeFail;2672 return ErrorSemanticAnalyzeFail;
2682 }2673 }
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;
2684 return ErrorNone;2717 return ErrorNone;
2685}2718}
26862719
...@@ -2807,7 +2840,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2807,7 +2840,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2807 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));2840 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));
2808 return ErrorSemanticAnalyzeFail;2841 return ErrorSemanticAnalyzeFail;
2809 }2842 }
2810 if ((err = type_ensure_zero_bits_known(g, enum_type))) {2843 if ((err = type_resolve(g, enum_type, ResolveStatusAlignmentKnown))) {
2811 assert(g->errors.length != 0);2844 assert(g->errors.length != 0);
2812 return err;2845 return err;
2813 }2846 }
...@@ -2848,7 +2881,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2848,7 +2881,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2848 }2881 }
2849 } else {2882 } else {
2850 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);2883 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))) {
2852 union_type->data.unionation.is_invalid = true;2885 union_type->data.unionation.is_invalid = true;
2853 continue;2886 continue;
2854 }2887 }
...@@ -3111,7 +3144,7 @@ static void typecheck_panic_fn(CodeGen *g, ZigFn *panic_fn) {...@@ -3111,7 +3144,7 @@ static void typecheck_panic_fn(CodeGen *g, ZigFn *panic_fn) {
3111 return wrong_panic_prototype(g, proto_node, fn_type);3144 return wrong_panic_prototype(g, proto_node, fn_type);
3112 }3145 }
3113 ZigType *const_u8_ptr = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,3146 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);
3115 ZigType *const_u8_slice = get_slice_type(g, const_u8_ptr);3148 ZigType *const_u8_slice = get_slice_type(g, const_u8_ptr);
3116 if (fn_type_id->param_info[0].type != const_u8_slice) {3149 if (fn_type_id->param_info[0].type != const_u8_slice) {
3117 return wrong_panic_prototype(g, proto_node, fn_type);3150 return wrong_panic_prototype(g, proto_node, fn_type);
...@@ -3801,7 +3834,7 @@ TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name) {...@@ -3801,7 +3834,7 @@ TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name) {
38013834
3802TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {3835TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {
3803 assert(type_entry->id == ZigTypeIdStruct);3836 assert(type_entry->id == ZigTypeIdStruct);
3804 assert(type_entry->data.structure.complete);3837 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
3805 if (type_entry->data.structure.src_field_count == 0)3838 if (type_entry->data.structure.src_field_count == 0)
3806 return nullptr;3839 return nullptr;
3807 auto entry = type_entry->data.structure.fields_by_name.maybe_get(name);3840 auto entry = type_entry->data.structure.fields_by_name.maybe_get(name);
...@@ -3956,9 +3989,12 @@ bool type_is_codegen_pointer(ZigType *type) {...@@ -3956,9 +3989,12 @@ bool type_is_codegen_pointer(ZigType *type) {
3956uint32_t get_ptr_align(CodeGen *g, ZigType *type) {3989uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
3957 ZigType *ptr_type = get_codegen_ptr_type(type);3990 ZigType *ptr_type = get_codegen_ptr_type(type);
3958 if (ptr_type->id == ZigTypeIdPointer) {3991 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;
3960 } else if (ptr_type->id == ZigTypeIdFn) {3994 } 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;
3962 } else if (ptr_type->id == ZigTypeIdPromise) {3998 } else if (ptr_type->id == ZigTypeIdPromise) {
3963 return get_coro_frame_align_bytes(g);3999 return get_coro_frame_align_bytes(g);
3964 } else {4000 } else {
...@@ -5023,8 +5059,8 @@ bool fn_eval_eql(Scope *a, Scope *b) {...@@ -5023,8 +5059,8 @@ bool fn_eval_eql(Scope *a, Scope *b) {
50235059
5024bool type_has_bits(ZigType *type_entry) {5060bool type_has_bits(ZigType *type_entry) {
5025 assert(type_entry);5061 assert(type_entry);
5026 assert(type_entry->id != ZigTypeIdInvalid);5062 assert(!type_is_invalid(type_entry));
5027 assert(type_has_zero_bits_known(type_entry));5063 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5028 return !type_entry->zero_bits;5064 return !type_entry->zero_bits;
5029}5065}
50305066
...@@ -5045,10 +5081,10 @@ bool type_requires_comptime(ZigType *type_entry) {...@@ -5045,10 +5081,10 @@ bool type_requires_comptime(ZigType *type_entry) {
5045 case ZigTypeIdArray:5081 case ZigTypeIdArray:
5046 return type_requires_comptime(type_entry->data.array.child_type);5082 return type_requires_comptime(type_entry->data.array.child_type);
5047 case ZigTypeIdStruct:5083 case ZigTypeIdStruct:
5048 assert(type_has_zero_bits_known(type_entry));5084 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5049 return type_entry->data.structure.requires_comptime;5085 return type_entry->data.structure.requires_comptime;
5050 case ZigTypeIdUnion:5086 case ZigTypeIdUnion:
5051 assert(type_has_zero_bits_known(type_entry));5087 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5052 return type_entry->data.unionation.requires_comptime;5088 return type_entry->data.unionation.requires_comptime;
5053 case ZigTypeIdOptional:5089 case ZigTypeIdOptional:
5054 return type_requires_comptime(type_entry->data.maybe.child_type);5090 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) {...@@ -5124,7 +5160,7 @@ void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
5124 const_val->special = ConstValSpecialStatic;5160 const_val->special = ConstValSpecialStatic;
5125 // TODO make this `[*]null u8` instead of `[*]u8`5161 // TODO make this `[*]null u8` instead of `[*]u8`
5126 const_val->type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,5162 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);
5128 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;5164 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
5129 const_val->data.x_ptr.data.base_array.array_val = array_val;5165 const_val->data.x_ptr.data.base_array.array_val = array_val;
5130 const_val->data.x_ptr.data.base_array.elem_index = 0;5166 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...@@ -5269,8 +5305,7 @@ void init_const_slice(CodeGen *g, ConstExprValue *const_val, ConstExprValue *arr
5269 assert(array_val->type->id == ZigTypeIdArray);5305 assert(array_val->type->id == ZigTypeIdArray);
52705306
5271 ZigType *ptr_type = get_pointer_to_type_extra(g, array_val->type->data.array.child_type,5307 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),5308 is_const, false, PtrLenUnknown, 0, 0, 0);
5273 0, 0);
52745309
5275 const_val->special = ConstValSpecialStatic;5310 const_val->special = ConstValSpecialStatic;
5276 const_val->type = get_slice_type(g, ptr_type);5311 const_val->type = get_slice_type(g, ptr_type);
...@@ -5295,7 +5330,7 @@ void init_const_ptr_array(CodeGen *g, ConstExprValue *const_val, ConstExprValue...@@ -5295,7 +5330,7 @@ void init_const_ptr_array(CodeGen *g, ConstExprValue *const_val, ConstExprValue
52955330
5296 const_val->special = ConstValSpecialStatic;5331 const_val->special = ConstValSpecialStatic;
5297 const_val->type = get_pointer_to_type_extra(g, child_type, is_const, false,5332 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);
5299 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;5334 const_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
5300 const_val->data.x_ptr.data.base_array.array_val = array_val;5335 const_val->data.x_ptr.data.base_array.array_val = array_val;
5301 const_val->data.x_ptr.data.base_array.elem_index = elem_index;5336 const_val->data.x_ptr.data.base_array.elem_index = elem_index;
...@@ -5394,32 +5429,46 @@ ConstExprValue *create_const_vals(size_t count) {...@@ -5394,32 +5429,46 @@ ConstExprValue *create_const_vals(size_t count) {
5394}5429}
53955430
5396Error ensure_complete_type(CodeGen *g, ZigType *type_entry) {5431Error ensure_complete_type(CodeGen *g, ZigType *type_entry) {
5397 if (type_is_invalid(type_entry))5432 return type_resolve(g, type_entry, ResolveStatusSizeKnown);
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;
5410}5433}
54115434
5412Error type_ensure_zero_bits_known(CodeGen *g, ZigType *type_entry) {5435Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
5413 if (type_is_invalid(type_entry))5436 if (type_is_invalid(ty))
5414 return ErrorSemanticAnalyzeFail;5437 return ErrorSemanticAnalyzeFail;
5415 if (type_entry->id == ZigTypeIdStruct) {5438 switch (status) {
5416 return resolve_struct_zero_bits(g, type_entry);5439 case ResolveStatusUnstarted:
5417 } else if (type_entry->id == ZigTypeIdEnum) {5440 return ErrorNone;
5418 return resolve_enum_zero_bits(g, type_entry);5441 case ResolveStatusInvalid:
5419 } else if (type_entry->id == ZigTypeIdUnion) {5442 zig_unreachable();
5420 return resolve_union_zero_bits(g, type_entry);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;
5421 }5470 }
5422 return ErrorNone;5471 zig_unreachable();
5423}5472}
54245473
5425bool ir_get_var_is_comptime(ZigVar *var) {5474bool ir_get_var_is_comptime(ZigVar *var) {
...@@ -6262,7 +6311,7 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {...@@ -6262,7 +6311,7 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
6262}6311}
62636312
6264uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {6313uint32_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));
6266 if (type_entry->zero_bits) return 0;6315 if (type_entry->zero_bits) return 0;
62676316
6268 // We need to make this function work without requiring ensure_complete_type6317 // 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);...@@ -59,9 +59,9 @@ bool get_ptr_const(ZigType *type);
59ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry);59ZigType *validate_var_type(CodeGen *g, AstNode *source_node, ZigType *type_entry);
60ZigType *container_ref_type(ZigType *type_entry);60ZigType *container_ref_type(ZigType *type_entry);
61bool type_is_complete(ZigType *type_entry);61bool type_is_complete(ZigType *type_entry);
62bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
62bool type_is_invalid(ZigType *type_entry);63bool type_is_invalid(ZigType *type_entry);
63bool type_is_global_error_set(ZigType *err_set_type);64bool type_is_global_error_set(ZigType *err_set_type);
64bool type_has_zero_bits_known(ZigType *type_entry);
65void resolve_container_type(CodeGen *g, ZigType *type_entry);65void resolve_container_type(CodeGen *g, ZigType *type_entry);
66ScopeDecls *get_container_scope(ZigType *type_entry);66ScopeDecls *get_container_scope(ZigType *type_entry);
67TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);67TypeStructField *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...@@ -89,7 +89,7 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_cou
89AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);89AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);
90bool type_requires_comptime(ZigType *type_entry);90bool type_requires_comptime(ZigType *type_entry);
91Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry);91Error 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);
93void complete_enum(CodeGen *g, ZigType *enum_type);93void complete_enum(CodeGen *g, ZigType *enum_type);
94bool ir_get_var_is_comptime(ZigVar *var);94bool ir_get_var_is_comptime(ZigVar *var);
95bool const_values_equal(ConstExprValue *a, ConstExprValue *b);95bool 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...@@ -775,7 +775,8 @@ static LLVMValueRef gen_store_untyped(CodeGen *g, LLVMValueRef value, LLVMValueR
775775
776static LLVMValueRef gen_store(CodeGen *g, LLVMValueRef value, LLVMValueRef ptr, ZigType *ptr_type) {776static LLVMValueRef gen_store(CodeGen *g, LLVMValueRef value, LLVMValueRef ptr, ZigType *ptr_type) {
777 assert(ptr_type->id == ZigTypeIdPointer);777 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);
779}780}
780781
781static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMValueRef ptr, uint32_t alignment, bool is_volatile,782static 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...@@ -793,7 +794,8 @@ static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMValueRef ptr, uint32_t alig
793794
794static LLVMValueRef gen_load(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_type, const char *name) {795static LLVMValueRef gen_load(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_type, const char *name) {
795 assert(ptr_type->id == ZigTypeIdPointer);796 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);
797}799}
798800
799static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, ZigType *type, ZigType *ptr_type) {801static 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...@@ -1795,7 +1797,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
17951797
1796 ZigType *usize = g->builtin_types.entry_usize;1798 ZigType *usize = g->builtin_types.entry_usize;
1797 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref);1799 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);
1799 assert(size_bytes > 0);1801 assert(size_bytes > 0);
1800 assert(align_bytes > 0);1802 assert(align_bytes > 0);
18011803
...@@ -4084,7 +4086,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I...@@ -4084,7 +4086,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
4084 LLVMValueRef ptr_val;4086 LLVMValueRef ptr_val;
40854087
4086 if (target_type->id == ZigTypeIdPointer) {4088 if (target_type->id == ZigTypeIdPointer) {
4087 align_bytes = target_type->data.pointer.alignment;4089 align_bytes = get_ptr_align(g, target_type);
4088 ptr_val = target_val;4090 ptr_val = target_val;
4089 } else if (target_type->id == ZigTypeIdFn) {4091 } else if (target_type->id == ZigTypeIdFn) {
4090 align_bytes = target_type->data.fn.fn_type_id.alignment;4092 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...@@ -4092,7 +4094,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
4092 } else if (target_type->id == ZigTypeIdOptional &&4094 } else if (target_type->id == ZigTypeIdOptional &&
4093 target_type->data.maybe.child_type->id == ZigTypeIdPointer)4095 target_type->data.maybe.child_type->id == ZigTypeIdPointer)
4094 {4096 {
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);
4096 ptr_val = target_val;4098 ptr_val = target_val;
4097 } else if (target_type->id == ZigTypeIdOptional &&4099 } else if (target_type->id == ZigTypeIdOptional &&
4098 target_type->data.maybe.child_type->id == ZigTypeIdFn)4100 target_type->data.maybe.child_type->id == ZigTypeIdFn)
...@@ -4105,7 +4107,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I...@@ -4105,7 +4107,7 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
4105 zig_panic("TODO audit this function");4107 zig_panic("TODO audit this function");
4106 } else if (target_type->id == ZigTypeIdStruct && target_type->data.structure.is_slice) {4108 } else if (target_type->id == ZigTypeIdStruct && target_type->data.structure.is_slice) {
4107 ZigType *slice_ptr_type = target_type->data.structure.fields[slice_ptr_index].type_entry;4109 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
4110 size_t ptr_index = target_type->data.structure.fields[slice_ptr_index].gen_index;4112 size_t ptr_index = target_type->data.structure.fields[slice_ptr_index].gen_index;
4111 LLVMValueRef ptr_val_ptr = LLVMBuildStructGEP(g->builder, target_val, (unsigned)ptr_index, "");4113 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...@@ -4260,7 +4262,8 @@ static LLVMValueRef ir_render_memset(CodeGen *g, IrExecutable *executable, IrIns
4260 LLVMValueRef is_volatile = ptr_type->data.pointer.is_volatile ?4262 LLVMValueRef is_volatile = ptr_type->data.pointer.is_volatile ?
4261 LLVMConstAllOnes(LLVMInt1Type()) : LLVMConstNull(LLVMInt1Type());4263 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
4265 LLVMValueRef params[] = {4268 LLVMValueRef params[] = {
4266 dest_ptr_casted,4269 dest_ptr_casted,
...@@ -4293,7 +4296,7 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns...@@ -4293,7 +4296,7 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns
4293 LLVMValueRef is_volatile = (dest_ptr_type->data.pointer.is_volatile || src_ptr_type->data.pointer.is_volatile) ?4296 LLVMValueRef is_volatile = (dest_ptr_type->data.pointer.is_volatile || src_ptr_type->data.pointer.is_volatile) ?
4294 LLVMConstAllOnes(LLVMInt1Type()) : LLVMConstNull(LLVMInt1Type());4297 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));
4297 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), min_align_bytes, false);4300 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), min_align_bytes, false);
42984301
4299 LLVMValueRef params[] = {4302 LLVMValueRef params[] = {
src/ir.cpp+257-119
...@@ -40,6 +40,7 @@ struct IrAnalyze {...@@ -40,6 +40,7 @@ struct IrAnalyze {
4040
41enum ConstCastResultId {41enum ConstCastResultId {
42 ConstCastResultIdOk,42 ConstCastResultIdOk,
43 ConstCastResultIdInvalid,
43 ConstCastResultIdErrSet,44 ConstCastResultIdErrSet,
44 ConstCastResultIdErrSetGlobal,45 ConstCastResultIdErrSetGlobal,
45 ConstCastResultIdPointerChild,46 ConstCastResultIdPointerChild,
...@@ -7490,8 +7491,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec...@@ -7490,8 +7491,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
7490 if (type_has_bits(return_type)) {7491 if (type_has_bits(return_type)) {
7491 IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,7492 IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,
7492 get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8,7493 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 false, false, PtrLenUnknown, 0, 0, 0));
7494 0, 0));
7495 IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr);7495 IrInstruction *result_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr);
7496 IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, result_ptr);7496 IrInstruction *result_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, result_ptr);
7497 IrInstruction *return_value_ptr_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len,7497 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...@@ -7544,8 +7544,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
7544 IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle);7544 IrInstruction *coro_mem_ptr_maybe = ir_build_coro_free(irb, scope, node, coro_id, irb->exec->coro_handle);
7545 IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,7545 IrInstruction *u8_ptr_type_unknown_len = ir_build_const_type(irb, scope, node,
7546 get_pointer_to_type_extra(irb->codegen, irb->codegen->builtin_types.entry_u8,7546 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),7547 false, false, PtrLenUnknown, 0, 0, 0));
7548 0, 0));
7549 IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, coro_mem_ptr_maybe);7548 IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type_unknown_len, coro_mem_ptr_maybe);
7550 IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false);7549 IrInstruction *coro_mem_ptr_ref = ir_build_ref(irb, scope, node, coro_mem_ptr, true, false);
7551 IrInstruction *coro_size_ptr = ir_build_var_ptr(irb, scope, node, coro_size_var);7550 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...@@ -8516,6 +8515,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8516 ConstCastOnly result = {};8515 ConstCastOnly result = {};
8517 result.id = ConstCastResultIdOk;8516 result.id = ConstCastResultIdOk;
85188517
8518 Error err;
8519
8519 if (wanted_type == actual_type)8520 if (wanted_type == actual_type)
8520 return result;8521 return result;
85218522
...@@ -8528,6 +8529,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8528,6 +8529,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8528 {8529 {
8529 ConstCastOnly child = types_match_const_cast_only(ira,8530 ConstCastOnly child = types_match_const_cast_only(ira,
8530 wanted_type->data.maybe.child_type, actual_type, source_node, wanted_is_mutable);8531 wanted_type->data.maybe.child_type, actual_type, source_node, wanted_is_mutable);
8532 if (child.id == ConstCastResultIdInvalid)
8533 return child;
8531 if (child.id != ConstCastResultIdOk) {8534 if (child.id != ConstCastResultIdOk) {
8532 result.id = ConstCastResultIdNullWrapPtr;8535 result.id = ConstCastResultIdNullWrapPtr;
8533 result.data.null_wrap_ptr_child = allocate_nonzero<ConstCastOnly>(1);8536 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...@@ -8544,7 +8547,6 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8544 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&8547 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&
8545 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile))8548 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile))
8546 {8549 {
8547 assert(actual_type->data.pointer.alignment >= wanted_type->data.pointer.alignment);
8548 return result;8550 return result;
8549 }8551 }
85508552
...@@ -8552,6 +8554,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8552,6 +8554,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8552 if (wanted_type->id == ZigTypeIdPointer && actual_type->id == ZigTypeIdPointer) {8554 if (wanted_type->id == ZigTypeIdPointer && actual_type->id == ZigTypeIdPointer) {
8553 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,8555 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
8554 actual_type->data.pointer.child_type, source_node, !wanted_type->data.pointer.is_const);8556 actual_type->data.pointer.child_type, source_node, !wanted_type->data.pointer.is_const);
8557 if (child.id == ConstCastResultIdInvalid)
8558 return child;
8555 if (child.id != ConstCastResultIdOk) {8559 if (child.id != ConstCastResultIdOk) {
8556 result.id = ConstCastResultIdPointerChild;8560 result.id = ConstCastResultIdPointerChild;
8557 result.data.pointer_mismatch = allocate_nonzero<ConstCastPointerMismatch>(1);8561 result.data.pointer_mismatch = allocate_nonzero<ConstCastPointerMismatch>(1);
...@@ -8560,12 +8564,20 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8560,12 +8564,20 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8560 result.data.pointer_mismatch->actual_child = actual_type->data.pointer.child_type;8564 result.data.pointer_mismatch->actual_child = actual_type->data.pointer.child_type;
8561 return result;8565 return result;
8562 }8566 }
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 }
8563 if ((actual_type->data.pointer.ptr_len == wanted_type->data.pointer.ptr_len) &&8575 if ((actual_type->data.pointer.ptr_len == wanted_type->data.pointer.ptr_len) &&
8564 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&8576 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&
8565 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile) &&8577 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile) &&
8566 actual_type->data.pointer.bit_offset == wanted_type->data.pointer.bit_offset &&8578 actual_type->data.pointer.bit_offset == wanted_type->data.pointer.bit_offset &&
8567 actual_type->data.pointer.unaligned_bit_count == wanted_type->data.pointer.unaligned_bit_count &&8579 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))
8569 {8581 {
8570 return result;8582 return result;
8571 }8583 }
...@@ -8575,14 +8587,24 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8575,14 +8587,24 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8575 if (is_slice(wanted_type) && is_slice(actual_type)) {8587 if (is_slice(wanted_type) && is_slice(actual_type)) {
8576 ZigType *actual_ptr_type = actual_type->data.structure.fields[slice_ptr_index].type_entry;8588 ZigType *actual_ptr_type = actual_type->data.structure.fields[slice_ptr_index].type_entry;
8577 ZigType *wanted_ptr_type = wanted_type->data.structure.fields[slice_ptr_index].type_entry;8589 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 }
8578 if ((!actual_ptr_type->data.pointer.is_const || wanted_ptr_type->data.pointer.is_const) &&8598 if ((!actual_ptr_type->data.pointer.is_const || wanted_ptr_type->data.pointer.is_const) &&
8579 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile) &&8599 (!actual_ptr_type->data.pointer.is_volatile || wanted_ptr_type->data.pointer.is_volatile) &&
8580 actual_ptr_type->data.pointer.bit_offset == wanted_ptr_type->data.pointer.bit_offset &&8600 actual_ptr_type->data.pointer.bit_offset == wanted_ptr_type->data.pointer.bit_offset &&
8581 actual_ptr_type->data.pointer.unaligned_bit_count == wanted_ptr_type->data.pointer.unaligned_bit_count &&8601 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))
8583 {8603 {
8584 ConstCastOnly child = types_match_const_cast_only(ira, wanted_ptr_type->data.pointer.child_type,8604 ConstCastOnly child = types_match_const_cast_only(ira, wanted_ptr_type->data.pointer.child_type,
8585 actual_ptr_type->data.pointer.child_type, source_node, !wanted_ptr_type->data.pointer.is_const);8605 actual_ptr_type->data.pointer.child_type, source_node, !wanted_ptr_type->data.pointer.is_const);
8606 if (child.id == ConstCastResultIdInvalid)
8607 return child;
8586 if (child.id != ConstCastResultIdOk) {8608 if (child.id != ConstCastResultIdOk) {
8587 result.id = ConstCastResultIdSliceChild;8609 result.id = ConstCastResultIdSliceChild;
8588 result.data.slice_mismatch = allocate_nonzero<ConstCastSliceMismatch>(1);8610 result.data.slice_mismatch = allocate_nonzero<ConstCastSliceMismatch>(1);
...@@ -8598,6 +8620,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8598,6 +8620,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8598 if (wanted_type->id == ZigTypeIdOptional && actual_type->id == ZigTypeIdOptional) {8620 if (wanted_type->id == ZigTypeIdOptional && actual_type->id == ZigTypeIdOptional) {
8599 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.maybe.child_type,8621 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.maybe.child_type,
8600 actual_type->data.maybe.child_type, source_node, wanted_is_mutable);8622 actual_type->data.maybe.child_type, source_node, wanted_is_mutable);
8623 if (child.id == ConstCastResultIdInvalid)
8624 return child;
8601 if (child.id != ConstCastResultIdOk) {8625 if (child.id != ConstCastResultIdOk) {
8602 result.id = ConstCastResultIdOptionalChild;8626 result.id = ConstCastResultIdOptionalChild;
8603 result.data.optional = allocate_nonzero<ConstCastOptionalMismatch>(1);8627 result.data.optional = allocate_nonzero<ConstCastOptionalMismatch>(1);
...@@ -8612,6 +8636,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8612,6 +8636,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8612 if (wanted_type->id == ZigTypeIdErrorUnion && actual_type->id == ZigTypeIdErrorUnion) {8636 if (wanted_type->id == ZigTypeIdErrorUnion && actual_type->id == ZigTypeIdErrorUnion) {
8613 ConstCastOnly payload_child = types_match_const_cast_only(ira, wanted_type->data.error_union.payload_type,8637 ConstCastOnly payload_child = types_match_const_cast_only(ira, wanted_type->data.error_union.payload_type,
8614 actual_type->data.error_union.payload_type, source_node, wanted_is_mutable);8638 actual_type->data.error_union.payload_type, source_node, wanted_is_mutable);
8639 if (payload_child.id == ConstCastResultIdInvalid)
8640 return payload_child;
8615 if (payload_child.id != ConstCastResultIdOk) {8641 if (payload_child.id != ConstCastResultIdOk) {
8616 result.id = ConstCastResultIdErrorUnionPayload;8642 result.id = ConstCastResultIdErrorUnionPayload;
8617 result.data.error_union_payload = allocate_nonzero<ConstCastErrUnionPayloadMismatch>(1);8643 result.data.error_union_payload = allocate_nonzero<ConstCastErrUnionPayloadMismatch>(1);
...@@ -8622,6 +8648,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8622,6 +8648,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8622 }8648 }
8623 ConstCastOnly error_set_child = types_match_const_cast_only(ira, wanted_type->data.error_union.err_set_type,8649 ConstCastOnly error_set_child = types_match_const_cast_only(ira, wanted_type->data.error_union.err_set_type,
8624 actual_type->data.error_union.err_set_type, source_node, wanted_is_mutable);8650 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;
8625 if (error_set_child.id != ConstCastResultIdOk) {8653 if (error_set_child.id != ConstCastResultIdOk) {
8626 result.id = ConstCastResultIdErrorUnionErrorSet;8654 result.id = ConstCastResultIdErrorUnionErrorSet;
8627 result.data.error_union_error_set = allocate_nonzero<ConstCastErrUnionErrSetMismatch>(1);8655 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...@@ -8709,6 +8737,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8709 {8737 {
8710 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.fn.fn_type_id.return_type,8738 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.fn.fn_type_id.return_type,
8711 actual_type->data.fn.fn_type_id.return_type, source_node, false);8739 actual_type->data.fn.fn_type_id.return_type, source_node, false);
8740 if (child.id == ConstCastResultIdInvalid)
8741 return child;
8712 if (child.id != ConstCastResultIdOk) {8742 if (child.id != ConstCastResultIdOk) {
8713 result.id = ConstCastResultIdFnReturnType;8743 result.id = ConstCastResultIdFnReturnType;
8714 result.data.return_type = allocate_nonzero<ConstCastOnly>(1);8744 result.data.return_type = allocate_nonzero<ConstCastOnly>(1);
...@@ -8721,6 +8751,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8721,6 +8751,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8721 actual_type->data.fn.fn_type_id.async_allocator_type,8751 actual_type->data.fn.fn_type_id.async_allocator_type,
8722 wanted_type->data.fn.fn_type_id.async_allocator_type,8752 wanted_type->data.fn.fn_type_id.async_allocator_type,
8723 source_node, false);8753 source_node, false);
8754 if (child.id == ConstCastResultIdInvalid)
8755 return child;
8724 if (child.id != ConstCastResultIdOk) {8756 if (child.id != ConstCastResultIdOk) {
8725 result.id = ConstCastResultIdAsyncAllocatorType;8757 result.id = ConstCastResultIdAsyncAllocatorType;
8726 result.data.async_allocator_type = allocate_nonzero<ConstCastOnly>(1);8758 result.data.async_allocator_type = allocate_nonzero<ConstCastOnly>(1);
...@@ -8745,6 +8777,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8745,6 +8777,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
87458777
8746 ConstCastOnly arg_child = types_match_const_cast_only(ira, actual_param_info->type,8778 ConstCastOnly arg_child = types_match_const_cast_only(ira, actual_param_info->type,
8747 expected_param_info->type, source_node, false);8779 expected_param_info->type, source_node, false);
8780 if (arg_child.id == ConstCastResultIdInvalid)
8781 return arg_child;
8748 if (arg_child.id != ConstCastResultIdOk) {8782 if (arg_child.id != ConstCastResultIdOk) {
8749 result.id = ConstCastResultIdFnArg;8783 result.id = ConstCastResultIdFnArg;
8750 result.data.fn_arg.arg_index = i;8784 result.data.fn_arg.arg_index = i;
...@@ -9238,7 +9272,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -9238,7 +9272,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
9238 if (prev_type->id == ZigTypeIdEnum && cur_type->id == ZigTypeIdUnion &&9272 if (prev_type->id == ZigTypeIdEnum && cur_type->id == ZigTypeIdUnion &&
9239 (cur_type->data.unionation.decl_node->data.container_decl.auto_enum || cur_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))9273 (cur_type->data.unionation.decl_node->data.container_decl.auto_enum || cur_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
9240 {9274 {
9241 if ((err = type_ensure_zero_bits_known(ira->codegen, cur_type)))9275 if ((err = type_resolve(ira->codegen, cur_type, ResolveStatusZeroBitsKnown)))
9242 return ira->codegen->builtin_types.entry_invalid;9276 return ira->codegen->builtin_types.entry_invalid;
9243 if (cur_type->data.unionation.tag_type == prev_type) {9277 if (cur_type->data.unionation.tag_type == prev_type) {
9244 continue;9278 continue;
...@@ -9248,7 +9282,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -9248,7 +9282,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
9248 if (cur_type->id == ZigTypeIdEnum && prev_type->id == ZigTypeIdUnion &&9282 if (cur_type->id == ZigTypeIdEnum && prev_type->id == ZigTypeIdUnion &&
9249 (prev_type->data.unionation.decl_node->data.container_decl.auto_enum || prev_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))9283 (prev_type->data.unionation.decl_node->data.container_decl.auto_enum || prev_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
9250 {9284 {
9251 if ((err = type_ensure_zero_bits_known(ira->codegen, prev_type)))9285 if ((err = type_resolve(ira->codegen, prev_type, ResolveStatusZeroBitsKnown)))
9252 return ira->codegen->builtin_types.entry_invalid;9286 return ira->codegen->builtin_types.entry_invalid;
9253 if (prev_type->data.unionation.tag_type == cur_type) {9287 if (prev_type->data.unionation.tag_type == cur_type) {
9254 prev_inst = cur_inst;9288 prev_inst = cur_inst;
...@@ -9274,8 +9308,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -9274,8 +9308,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
9274 ZigType *ptr_type = get_pointer_to_type_extra(9308 ZigType *ptr_type = get_pointer_to_type_extra(
9275 ira->codegen, prev_inst->value.type->data.array.child_type,9309 ira->codegen, prev_inst->value.type->data.array.child_type,
9276 true, false, PtrLenUnknown,9310 true, false, PtrLenUnknown,
9277 get_abi_alignment(ira->codegen, prev_inst->value.type->data.array.child_type),9311 0, 0, 0);
9278 0, 0);
9279 ZigType *slice_type = get_slice_type(ira->codegen, ptr_type);9312 ZigType *slice_type = get_slice_type(ira->codegen, ptr_type);
9280 if (err_set_type != nullptr) {9313 if (err_set_type != nullptr) {
9281 return get_error_union_type(ira->codegen, err_set_type, slice_type);9314 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,...@@ -9472,7 +9505,16 @@ static IrInstruction *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira,
9472 IrInstruction *value, ZigType *wanted_type)9505 IrInstruction *value, ZigType *wanted_type)
9473{9506{
9474 assert(value->value.type->id == ZigTypeIdPointer);9507 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
9477 if (instr_is_comptime(value)) {9519 if (instr_is_comptime(value)) {
9478 ConstExprValue *pointee = ir_const_ptr_pointee(ira, &value->value, source_instr->source_node);9520 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,...@@ -9500,7 +9542,15 @@ static IrInstruction *ir_resolve_ptr_of_array_to_unknown_len_ptr(IrAnalyze *ira,
9500static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruction *source_instr,9542static IrInstruction *ir_resolve_ptr_of_array_to_slice(IrAnalyze *ira, IrInstruction *source_instr,
9501 IrInstruction *value, ZigType *wanted_type)9543 IrInstruction *value, ZigType *wanted_type)
9502{9544{
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
9505 if (instr_is_comptime(value)) {9555 if (instr_is_comptime(value)) {
9506 ConstExprValue *pointee = ir_const_ptr_pointee(ira, &value->value, source_instr->source_node);9556 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,...@@ -9687,8 +9737,7 @@ static ZigType *ir_analyze_const_ptr(IrAnalyze *ira, IrInstruction *instruction,
9687 ConstPtrMut ptr_mut, bool ptr_is_const, bool ptr_is_volatile)9737 ConstPtrMut ptr_mut, bool ptr_is_const, bool ptr_is_volatile)
9688{9738{
9689 IrInstruction *const_instr = ir_get_const_ptr(ira, instruction, pointee,9739 IrInstruction *const_instr = ir_get_const_ptr(ira, instruction, pointee,
9690 pointee_type, ptr_mut, ptr_is_const, ptr_is_volatile,9740 pointee_type, ptr_mut, ptr_is_const, ptr_is_volatile, 0);
9691 get_abi_alignment(ira->codegen, pointee_type));
9692 ir_link_new_instruction(const_instr, instruction);9741 ir_link_new_instruction(const_instr, instruction);
9693 return const_instr->value.type;9742 return const_instr->value.type;
9694}9743}
...@@ -10005,20 +10054,24 @@ static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *so...@@ -10005,20 +10054,24 @@ static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *so
10005static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *value,10054static IrInstruction *ir_get_ref(IrAnalyze *ira, IrInstruction *source_instruction, IrInstruction *value,
10006 bool is_const, bool is_volatile)10055 bool is_const, bool is_volatile)
10007{10056{
10057 Error err;
10058
10008 if (type_is_invalid(value->value.type))10059 if (type_is_invalid(value->value.type))
10009 return ira->codegen->invalid_instruction;10060 return ira->codegen->invalid_instruction;
1001010061
10062 if ((err = type_resolve(ira->codegen, value->value.type, ResolveStatusZeroBitsKnown)))
10063 return ira->codegen->invalid_instruction;
10064
10011 if (instr_is_comptime(value)) {10065 if (instr_is_comptime(value)) {
10012 ConstExprValue *val = ir_resolve_const(ira, value, UndefOk);10066 ConstExprValue *val = ir_resolve_const(ira, value, UndefOk);
10013 if (!val)10067 if (!val)
10014 return ira->codegen->invalid_instruction;10068 return ira->codegen->invalid_instruction;
10015 return ir_get_const_ptr(ira, source_instruction, val, value->value.type,10069 return ir_get_const_ptr(ira, source_instruction, val, value->value.type,
10016 ConstPtrMutComptimeConst, is_const, is_volatile,10070 ConstPtrMutComptimeConst, is_const, is_volatile, 0);
10017 get_abi_alignment(ira->codegen, value->value.type));
10018 }10071 }
1001910072
10020 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, value->value.type,10073 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);
10022 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instruction->scope,10075 IrInstruction *new_instruction = ir_build_ref(&ira->new_irb, source_instruction->scope,
10023 source_instruction->source_node, value, is_const, is_volatile);10076 source_instruction->source_node, value, is_const, is_volatile);
10024 new_instruction->value.type = ptr_type;10077 new_instruction->value.type = ptr_type;
...@@ -10185,9 +10238,9 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so...@@ -10185,9 +10238,9 @@ static IrInstruction *ir_analyze_enum_to_union(IrAnalyze *ira, IrInstruction *so
10185 return ira->codegen->invalid_instruction;10238 return ira->codegen->invalid_instruction;
10186 TypeUnionField *union_field = find_union_field_by_tag(wanted_type, &val->data.x_enum_tag);10239 TypeUnionField *union_field = find_union_field_by_tag(wanted_type, &val->data.x_enum_tag);
10187 assert(union_field != nullptr);10240 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)))
10189 return ira->codegen->invalid_instruction;10242 return ira->codegen->invalid_instruction;
10190 if (!union_field->type_entry->zero_bits) {10243 if (type_has_bits(union_field->type_entry)) {
10191 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(10244 AstNode *field_node = wanted_type->data.unionation.decl_node->data.container_decl.fields.at(
10192 union_field->enum_field->decl_index);10245 union_field->enum_field->decl_index);
10193 ErrorMsg *msg = ir_add_error(ira, source_instr,10246 ErrorMsg *msg = ir_add_error(ira, source_instr,
...@@ -10490,7 +10543,10 @@ static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *sou...@@ -10490,7 +10543,10 @@ static IrInstruction *ir_analyze_ptr_to_array(IrAnalyze *ira, IrInstruction *sou
10490 ZigType *wanted_type)10543 ZigType *wanted_type)
10491{10544{
10492 assert(wanted_type->id == ZigTypeIdPointer);10545 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));
10494 ZigType *array_type = wanted_type->data.pointer.child_type;10550 ZigType *array_type = wanted_type->data.pointer.child_type;
10495 assert(array_type->id == ZigTypeIdArray);10551 assert(array_type->id == ZigTypeIdArray);
10496 assert(array_type->data.array.len == 1);10552 assert(array_type->data.array.len == 1);
...@@ -10537,6 +10593,8 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa...@@ -10537,6 +10593,8 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
10537 switch (cast_result->id) {10593 switch (cast_result->id) {
10538 case ConstCastResultIdOk:10594 case ConstCastResultIdOk:
10539 zig_unreachable();10595 zig_unreachable();
10596 case ConstCastResultIdInvalid:
10597 zig_unreachable();
10540 case ConstCastResultIdOptionalChild: {10598 case ConstCastResultIdOptionalChild: {
10541 ErrorMsg *msg = add_error_note(ira->codegen, parent_msg, source_node,10599 ErrorMsg *msg = add_error_note(ira->codegen, parent_msg, source_node,
10542 buf_sprintf("optional type child '%s' cannot cast into optional type child '%s'",10600 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...@@ -10636,6 +10694,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
10636 // perfect match or non-const to const10694 // perfect match or non-const to const
10637 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, wanted_type, actual_type,10695 ConstCastOnly const_cast_result = types_match_const_cast_only(ira, wanted_type, actual_type,
10638 source_node, false);10696 source_node, false);
10697 if (const_cast_result.id == ConstCastResultIdInvalid)
10698 return ira->codegen->invalid_instruction;
10639 if (const_cast_result.id == ConstCastResultIdOk) {10699 if (const_cast_result.id == ConstCastResultIdOk) {
10640 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop, false);10700 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop, false);
10641 }10701 }
...@@ -10751,13 +10811,19 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -10751,13 +10811,19 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
10751 wanted_type->data.pointer.ptr_len == PtrLenUnknown &&10811 wanted_type->data.pointer.ptr_len == PtrLenUnknown &&
10752 actual_type->id == ZigTypeIdPointer &&10812 actual_type->id == ZigTypeIdPointer &&
10753 actual_type->data.pointer.ptr_len == PtrLenSingle &&10813 actual_type->data.pointer.ptr_len == PtrLenSingle &&
10754 actual_type->data.pointer.child_type->id == ZigTypeIdArray &&10814 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)
10759 {10815 {
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 }
10761 }10827 }
1076210828
10763 // *[N]T to []T10829 // *[N]T to []T
...@@ -10811,16 +10877,23 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -10811,16 +10877,23 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
10811 wanted_child_type->data.pointer.ptr_len == PtrLenUnknown &&10877 wanted_child_type->data.pointer.ptr_len == PtrLenUnknown &&
10812 actual_type->id == ZigTypeIdPointer &&10878 actual_type->id == ZigTypeIdPointer &&
10813 actual_type->data.pointer.ptr_len == PtrLenSingle &&10879 actual_type->data.pointer.ptr_len == PtrLenSingle &&
10814 actual_type->data.pointer.child_type->id == ZigTypeIdArray &&10880 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)
10819 {10881 {
10820 IrInstruction *cast1 = ir_resolve_ptr_of_array_to_unknown_len_ptr(ira, source_instr, value, wanted_child_type);10882 if ((err = type_resolve(ira->codegen, actual_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
10821 if (type_is_invalid(cast1->value.type))
10822 return ira->codegen->invalid_instruction;10883 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 }
10824 }10897 }
10825 }10898 }
1082610899
...@@ -10963,7 +11036,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -10963,7 +11036,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1096311036
10964 // cast from union to the enum type of the union11037 // cast from union to the enum type of the union
10965 if (actual_type->id == ZigTypeIdUnion && wanted_type->id == ZigTypeIdEnum) {11038 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)))
10967 return ira->codegen->invalid_instruction;11040 return ira->codegen->invalid_instruction;
1096811041
10969 if (actual_type->data.unionation.tag_type == wanted_type) {11042 if (actual_type->data.unionation.tag_type == wanted_type) {
...@@ -10976,7 +11049,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -10976,7 +11049,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
10976 (wanted_type->data.unionation.decl_node->data.container_decl.auto_enum ||11049 (wanted_type->data.unionation.decl_node->data.container_decl.auto_enum ||
10977 wanted_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))11050 wanted_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
10978 {11051 {
10979 if ((err = type_ensure_zero_bits_known(ira->codegen, wanted_type)))11052 if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusZeroBitsKnown)))
10980 return ira->codegen->invalid_instruction;11053 return ira->codegen->invalid_instruction;
1098111054
10982 if (wanted_type->data.unionation.tag_type == actual_type) {11055 if (wanted_type->data.unionation.tag_type == actual_type) {
...@@ -10990,7 +11063,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -10990,7 +11063,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
10990 if (union_type->data.unionation.decl_node->data.container_decl.auto_enum ||11063 if (union_type->data.unionation.decl_node->data.container_decl.auto_enum ||
10991 union_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr)11064 union_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr)
10992 {11065 {
10993 if ((err = type_ensure_zero_bits_known(ira->codegen, union_type)))11066 if ((err = type_resolve(ira->codegen, union_type, ResolveStatusZeroBitsKnown)))
10994 return ira->codegen->invalid_instruction;11067 return ira->codegen->invalid_instruction;
1099511068
10996 if (union_type->data.unionation.tag_type == actual_type) {11069 if (union_type->data.unionation.tag_type == actual_type) {
...@@ -11017,14 +11090,24 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -11017,14 +11090,24 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
11017 actual_type->data.pointer.child_type, source_node,11090 actual_type->data.pointer.child_type, source_node,
11018 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)11091 !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
11019 {11092 {
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) {
11021 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("cast increases pointer alignment"));11106 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("cast increases pointer alignment"));
11022 add_error_note(ira->codegen, msg, value->source_node,11107 add_error_note(ira->codegen, msg, value->source_node,
11023 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&actual_type->name),11108 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&actual_type->name), actual_align));
11024 actual_type->data.pointer.alignment));
11025 add_error_note(ira->codegen, msg, source_instr->source_node,11109 add_error_note(ira->codegen, msg, source_instr->source_node,
11026 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&wanted_type->name),11110 buf_sprintf("'%s' has alignment %" PRIu32, buf_ptr(&wanted_type->name), wanted_align));
11027 wanted_type->data.pointer.alignment));
11028 return ira->codegen->invalid_instruction;11111 return ira->codegen->invalid_instruction;
11029 }11112 }
11030 return ir_analyze_ptr_to_array(ira, source_instr, value, wanted_type);11113 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...@@ -11036,7 +11119,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
11036 types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,11119 types_match_const_cast_only(ira, wanted_type->data.pointer.child_type,
11037 actual_type, source_node, !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)11120 actual_type, source_node, !wanted_type->data.pointer.is_const).id == ConstCastResultIdOk)
11038 {11121 {
11039 if ((err = type_ensure_zero_bits_known(ira->codegen, actual_type))) {11122 if ((err = type_resolve(ira->codegen, actual_type, ResolveStatusZeroBitsKnown))) {
11040 return ira->codegen->invalid_instruction;11123 return ira->codegen->invalid_instruction;
11041 }11124 }
11042 if (!type_has_bits(actual_type)) {11125 if (!type_has_bits(actual_type)) {
...@@ -11282,8 +11365,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {...@@ -11282,8 +11365,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
11282 return nullptr;11365 return nullptr;
1128311366
11284 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,11367 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
11285 true, false, PtrLenUnknown,11368 true, false, PtrLenUnknown, 0, 0, 0);
11286 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8), 0, 0);
11287 ZigType *str_type = get_slice_type(ira->codegen, ptr_type);11369 ZigType *str_type = get_slice_type(ira->codegen, ptr_type);
11288 IrInstruction *casted_value = ir_implicit_cast(ira, value, str_type);11370 IrInstruction *casted_value = ir_implicit_cast(ira, value, str_type);
11289 if (type_is_invalid(casted_value->value.type))11371 if (type_is_invalid(casted_value->value.type))
...@@ -11573,8 +11655,6 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op...@@ -11573,8 +11655,6 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op
11573 ZigType *resolved_type = ir_resolve_peer_types(ira, source_node, nullptr, instructions, 2);11655 ZigType *resolved_type = ir_resolve_peer_types(ira, source_node, nullptr, instructions, 2);
11574 if (type_is_invalid(resolved_type))11656 if (type_is_invalid(resolved_type))
11575 return resolved_type;11657 return resolved_type;
11576 if ((err = type_ensure_zero_bits_known(ira->codegen, resolved_type)))
11577 return resolved_type;
1157811658
11579 bool operator_allowed;11659 bool operator_allowed;
11580 switch (resolved_type->id) {11660 switch (resolved_type->id) {
...@@ -11630,6 +11710,9 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op...@@ -11630,6 +11710,9 @@ static ZigType *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *bin_op
11630 if (casted_op2 == ira->codegen->invalid_instruction)11710 if (casted_op2 == ira->codegen->invalid_instruction)
11631 return ira->codegen->builtin_types.entry_invalid;11711 return ira->codegen->builtin_types.entry_invalid;
1163211712
11713 if ((err = type_resolve(ira->codegen, resolved_type, ResolveStatusZeroBitsKnown)))
11714 return resolved_type;
11715
11633 bool one_possible_value = !type_requires_comptime(resolved_type) && !type_has_bits(resolved_type);11716 bool one_possible_value = !type_requires_comptime(resolved_type) && !type_has_bits(resolved_type);
11634 if (one_possible_value || (instr_is_comptime(casted_op1) && instr_is_comptime(casted_op2))) {11717 if (one_possible_value || (instr_is_comptime(casted_op1) && instr_is_comptime(casted_op2))) {
11635 ConstExprValue *op1_val = one_possible_value ? &casted_op1->value : ir_resolve_const(ira, casted_op1, UndefBad);11718 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...@@ -12316,7 +12399,7 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc
12316 out_array_val = out_val;12399 out_array_val = out_val;
12317 } else if (is_slice(op1_type) || is_slice(op2_type)) {12400 } else if (is_slice(op1_type) || is_slice(op2_type)) {
12318 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,12401 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);
12320 result_type = get_slice_type(ira->codegen, ptr_type);12403 result_type = get_slice_type(ira->codegen, ptr_type);
12321 out_array_val = create_const_vals(1);12404 out_array_val = create_const_vals(1);
12322 out_array_val->special = ConstValSpecialStatic;12405 out_array_val->special = ConstValSpecialStatic;
...@@ -12337,8 +12420,7 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc...@@ -12337,8 +12420,7 @@ static ZigType *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruc
12337 new_len += 1; // null byte12420 new_len += 1; // null byte
1233812421
12339 // TODO make this `[*]null T` instead of `[*]T`12422 // TODO make this `[*]null T` instead of `[*]T`
12340 result_type = get_pointer_to_type_extra(ira->codegen, child_type, true, false,12423 result_type = get_pointer_to_type_extra(ira->codegen, child_type, true, false, PtrLenUnknown, 0, 0, 0);
12341 PtrLenUnknown, get_abi_alignment(ira->codegen, child_type), 0, 0);
1234212424
12343 out_array_val = create_const_vals(1);12425 out_array_val = create_const_vals(1);
12344 out_array_val->special = ConstValSpecialStatic;12426 out_array_val->special = ConstValSpecialStatic;
...@@ -12563,7 +12645,7 @@ static ZigType *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstructionDec...@@ -12563,7 +12645,7 @@ static ZigType *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstructionDec
12563 if (type_is_invalid(result_type)) {12645 if (type_is_invalid(result_type)) {
12564 result_type = ira->codegen->builtin_types.entry_invalid;12646 result_type = ira->codegen->builtin_types.entry_invalid;
12565 } else {12647 } else {
12566 if ((err = type_ensure_zero_bits_known(ira->codegen, result_type))) {12648 if ((err = type_resolve(ira->codegen, result_type, ResolveStatusZeroBitsKnown))) {
12567 result_type = ira->codegen->builtin_types.entry_invalid;12649 result_type = ira->codegen->builtin_types.entry_invalid;
12568 }12650 }
12569 }12651 }
...@@ -12631,6 +12713,11 @@ static ZigType *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstructionDec...@@ -12631,6 +12713,11 @@ static ZigType *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstructionDec
12631 }12713 }
1263212714
12633 if (decl_var_instruction->align_value == nullptr) {12715 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 }
12634 var->align_bytes = get_abi_alignment(ira->codegen, result_type);12721 var->align_bytes = get_abi_alignment(ira->codegen, result_type);
12635 } else {12722 } else {
12636 if (!ir_resolve_align(ira, decl_var_instruction->align_value->other, &var->align_bytes)) {12723 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) {...@@ -13100,7 +13187,6 @@ static ZigVar *get_fn_var_by_index(ZigFn *fn_entry, size_t index) {
13100static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,13187static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
13101 ZigVar *var)13188 ZigVar *var)
13102{13189{
13103 Error err;
13104 while (var->next_var != nullptr) {13190 while (var->next_var != nullptr) {
13105 var = var->next_var;13191 var = var->next_var;
13106 }13192 }
...@@ -13158,8 +13244,6 @@ no_mem_slot:...@@ -13158,8 +13244,6 @@ no_mem_slot:
13158 instruction->scope, instruction->source_node, var);13244 instruction->scope, instruction->source_node, var);
13159 var_ptr_instruction->value.type = get_pointer_to_type_extra(ira->codegen, var->value->type,13245 var_ptr_instruction->value.type = get_pointer_to_type_extra(ira->codegen, var->value->type,
13160 var->src_is_const, is_volatile, PtrLenSingle, var->align_bytes, 0, 0);13246 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
13164 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);13248 bool in_fn_scope = (scope_fn_entry(var->parent_scope) != nullptr);
13165 var_ptr_instruction->value.data.rh_ptr = in_fn_scope ? RuntimeHintPtrStack : RuntimeHintPtrNonStack;13249 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...@@ -13356,8 +13440,7 @@ static ZigType *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instr
13356 IrInstruction *casted_new_stack = nullptr;13440 IrInstruction *casted_new_stack = nullptr;
13357 if (call_instruction->new_stack != nullptr) {13441 if (call_instruction->new_stack != nullptr) {
13358 ZigType *u8_ptr = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,13442 ZigType *u8_ptr = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
13359 false, false, PtrLenUnknown,13443 false, false, PtrLenUnknown, 0, 0, 0);
13360 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8), 0, 0);
13361 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);13444 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
13362 IrInstruction *new_stack = call_instruction->new_stack->other;13445 IrInstruction *new_stack = call_instruction->new_stack->other;
13363 if (type_is_invalid(new_stack->value.type))13446 if (type_is_invalid(new_stack->value.type))
...@@ -13536,7 +13619,7 @@ static ZigType *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instr...@@ -13536,7 +13619,7 @@ static ZigType *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call_instr
13536 inst_fn_type_id.return_type = specified_return_type;13619 inst_fn_type_id.return_type = specified_return_type;
13537 }13620 }
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)))
13540 return ira->codegen->builtin_types.entry_invalid;13623 return ira->codegen->builtin_types.entry_invalid;
1354113624
13542 if (type_requires_comptime(specified_return_type)) {13625 if (type_requires_comptime(specified_return_type)) {
...@@ -14212,7 +14295,7 @@ static ZigType *adjust_ptr_len(CodeGen *g, ZigType *ptr_type, PtrLen ptr_len) {...@@ -14212,7 +14295,7 @@ static ZigType *adjust_ptr_len(CodeGen *g, ZigType *ptr_type, PtrLen ptr_len) {
14212 ptr_type->data.pointer.child_type,14295 ptr_type->data.pointer.child_type,
14213 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,14296 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
14214 ptr_len,14297 ptr_len,
14215 ptr_type->data.pointer.alignment,14298 ptr_type->data.pointer.explicit_alignment,
14216 ptr_type->data.pointer.bit_offset, ptr_type->data.pointer.unaligned_bit_count);14299 ptr_type->data.pointer.bit_offset, ptr_type->data.pointer.unaligned_bit_count);
14217}14300}
1421814301
...@@ -14264,7 +14347,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle...@@ -14264,7 +14347,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle
14264 return_type = get_pointer_to_type_extra(ira->codegen, child_type,14347 return_type = get_pointer_to_type_extra(ira->codegen, child_type,
14265 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,14348 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
14266 elem_ptr_instruction->ptr_len,14349 elem_ptr_instruction->ptr_len,
14267 ptr_type->data.pointer.alignment, 0, 0);14350 ptr_type->data.pointer.explicit_alignment, 0, 0);
14268 } else {14351 } else {
14269 uint64_t elem_val_scalar;14352 uint64_t elem_val_scalar;
14270 if (!ir_resolve_usize(ira, elem_index, &elem_val_scalar))14353 if (!ir_resolve_usize(ira, elem_index, &elem_val_scalar))
...@@ -14336,7 +14419,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle...@@ -14336,7 +14419,7 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle
1433614419
14337 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);14420 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);
14338 uint64_t abi_align = get_abi_alignment(ira->codegen, return_type->data.pointer.child_type);14421 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);
14340 if (instr_is_comptime(casted_elem_index)) {14423 if (instr_is_comptime(casted_elem_index)) {
14341 uint64_t index = bigint_as_unsigned(&casted_elem_index->value.data.x_bigint);14424 uint64_t index = bigint_as_unsigned(&casted_elem_index->value.data.x_bigint);
14342 if (array_type->id == ZigTypeIdArray) {14425 if (array_type->id == ZigTypeIdArray) {
...@@ -14653,9 +14736,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -14653,9 +14736,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
14653 }14736 }
1465414737
14655 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,14738 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,
14656 is_const, is_volatile,14739 is_const, is_volatile, PtrLenSingle, 0, 0, 0);
14657 PtrLenSingle,
14658 get_abi_alignment(ira->codegen, field_type), 0, 0);
1465914740
14660 IrInstruction *result = ir_get_const(ira, source_instr);14741 IrInstruction *result = ir_get_const(ira, source_instr);
14661 ConstExprValue *const_val = &result->value;14742 ConstExprValue *const_val = &result->value;
...@@ -14669,7 +14750,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -14669,7 +14750,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1466914750
14670 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, container_ptr, field);14751 IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, container_ptr, field);
14671 result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,14752 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);
14673 return result;14754 return result;
14674 } else {14755 } else {
14675 return ir_analyze_container_member_access_inner(ira, bare_type, field_name,14756 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...@@ -15002,9 +15083,14 @@ static ZigType *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstructionFi
15002 } else if (buf_eql_str(field_name, "alignment")) {15083 } else if (buf_eql_str(field_name, "alignment")) {
15003 bool ptr_is_const = true;15084 bool ptr_is_const = true;
15004 bool ptr_is_volatile = false;15085 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 }
15005 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,15091 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
15006 create_const_unsigned_negative(ira->codegen->builtin_types.entry_num_lit_int,15092 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),
15008 ira->codegen->builtin_types.entry_num_lit_int,15094 ira->codegen->builtin_types.entry_num_lit_int,
15009 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);15095 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
15010 } else {15096 } else {
...@@ -15461,7 +15547,7 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -15461,7 +15547,7 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,
15461 IrInstructionSliceType *slice_type_instruction)15547 IrInstructionSliceType *slice_type_instruction)
15462{15548{
15463 Error err;15549 Error err;
15464 uint32_t align_bytes;15550 uint32_t align_bytes = 0;
15465 if (slice_type_instruction->align_value != nullptr) {15551 if (slice_type_instruction->align_value != nullptr) {
15466 if (!ir_resolve_align(ira, slice_type_instruction->align_value->other, &align_bytes))15552 if (!ir_resolve_align(ira, slice_type_instruction->align_value->other, &align_bytes))
15467 return ira->codegen->builtin_types.entry_invalid;15553 return ira->codegen->builtin_types.entry_invalid;
...@@ -15471,12 +15557,6 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -15471,12 +15557,6 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,
15471 if (type_is_invalid(child_type))15557 if (type_is_invalid(child_type))
15472 return ira->codegen->builtin_types.entry_invalid;15558 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
15480 bool is_const = slice_type_instruction->is_const;15560 bool is_const = slice_type_instruction->is_const;
15481 bool is_volatile = slice_type_instruction->is_volatile;15561 bool is_volatile = slice_type_instruction->is_volatile;
1548215562
...@@ -15511,7 +15591,7 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -15511,7 +15591,7 @@ static ZigType *ir_analyze_instruction_slice_type(IrAnalyze *ira,
15511 case ZigTypeIdBoundFn:15591 case ZigTypeIdBoundFn:
15512 case ZigTypeIdPromise:15592 case ZigTypeIdPromise:
15513 {15593 {
15514 if ((err = type_ensure_zero_bits_known(ira->codegen, child_type)))15594 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
15515 return ira->codegen->builtin_types.entry_invalid;15595 return ira->codegen->builtin_types.entry_invalid;
15516 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,15596 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
15517 is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0);15597 is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0);
...@@ -15751,9 +15831,7 @@ static ZigType *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,...@@ -15751,9 +15831,7 @@ static ZigType *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
15751 }15831 }
15752 ZigType *child_type = type_entry->data.maybe.child_type;15832 ZigType *child_type = type_entry->data.maybe.child_type;
15753 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, child_type,15833 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,15834 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile, PtrLenSingle, 0, 0, 0);
15755 PtrLenSingle,
15756 get_abi_alignment(ira->codegen, child_type), 0, 0);
1575715835
15758 if (instr_is_comptime(value)) {15836 if (instr_is_comptime(value)) {
15759 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);15837 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
...@@ -16123,7 +16201,7 @@ static ZigType *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -16123,7 +16201,7 @@ static ZigType *ir_analyze_instruction_switch_target(IrAnalyze *ira,
16123 return tag_type;16201 return tag_type;
16124 }16202 }
16125 case ZigTypeIdEnum: {16203 case ZigTypeIdEnum: {
16126 if ((err = type_ensure_zero_bits_known(ira->codegen, target_type)))16204 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
16127 return ira->codegen->builtin_types.entry_invalid;16205 return ira->codegen->builtin_types.entry_invalid;
16128 if (target_type->data.enumeration.src_field_count < 2) {16206 if (target_type->data.enumeration.src_field_count < 2) {
16129 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];16207 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
...@@ -16352,7 +16430,7 @@ static ZigType *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrInstruc...@@ -16352,7 +16430,7 @@ static ZigType *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrInstruc
16352 if (casted_field_value == ira->codegen->invalid_instruction)16430 if (casted_field_value == ira->codegen->invalid_instruction)
16353 return ira->codegen->builtin_types.entry_invalid;16431 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)))
16356 return ira->codegen->builtin_types.entry_invalid;16434 return ira->codegen->builtin_types.entry_invalid;
1635716435
16358 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);16436 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...@@ -16752,7 +16830,7 @@ static ZigType *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstructionErr
16752 return ira->codegen->builtin_types.entry_invalid;16830 return ira->codegen->builtin_types.entry_invalid;
1675316831
16754 ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,16832 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);
16756 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);16834 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
16757 if (casted_value->value.special == ConstValSpecialStatic) {16835 if (casted_value->value.special == ConstValSpecialStatic) {
16758 ErrorTableEntry *err = casted_value->value.data.x_err_set;16836 ErrorTableEntry *err = casted_value->value.data.x_err_set;
...@@ -16779,7 +16857,7 @@ static ZigType *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstructi...@@ -16779,7 +16857,7 @@ static ZigType *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstructi
16779 assert(target->value.type->id == ZigTypeIdEnum);16857 assert(target->value.type->id == ZigTypeIdEnum);
1678016858
16781 if (instr_is_comptime(target)) {16859 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)))
16783 return ira->codegen->builtin_types.entry_invalid;16861 return ira->codegen->builtin_types.entry_invalid;
16784 TypeEnumField *field = find_enum_field_by_tag(target->value.type, &target->value.data.x_bigint);16862 TypeEnumField *field = find_enum_field_by_tag(target->value.type, &target->value.data.x_bigint);
16785 ConstExprValue *array_val = create_const_str_lit(ira->codegen, field->name);16863 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...@@ -16794,8 +16872,7 @@ static ZigType *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstructi
16794 ZigType *u8_ptr_type = get_pointer_to_type_extra(16872 ZigType *u8_ptr_type = get_pointer_to_type_extra(
16795 ira->codegen, ira->codegen->builtin_types.entry_u8,16873 ira->codegen, ira->codegen->builtin_types.entry_u8,
16796 true, false, PtrLenUnknown,16874 true, false, PtrLenUnknown,
16797 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8),16875 0, 0, 0);
16798 0, 0);
16799 result->value.type = get_slice_type(ira->codegen, u8_ptr_type);16876 result->value.type = get_slice_type(ira->codegen, u8_ptr_type);
16800 return result->value.type;16877 return result->value.type;
16801}16878}
...@@ -17158,8 +17235,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco...@@ -17158,8 +17235,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
17158 ZigType *u8_ptr = get_pointer_to_type_extra(17235 ZigType *u8_ptr = get_pointer_to_type_extra(
17159 ira->codegen, ira->codegen->builtin_types.entry_u8,17236 ira->codegen, ira->codegen->builtin_types.entry_u8,
17160 true, false, PtrLenUnknown,17237 true, false, PtrLenUnknown,
17161 get_abi_alignment(ira->codegen, ira->codegen->builtin_types.entry_u8),17238 0, 0, 0);
17162 0, 0);
17163 fn_def_fields[6].type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));17239 fn_def_fields[6].type = get_optional_type(ira->codegen, get_slice_type(ira->codegen, u8_ptr));
17164 if (fn_node->is_extern && buf_len(fn_node->lib_name) > 0) {17240 if (fn_node->is_extern && buf_len(fn_node->lib_name) > 0) {
17165 fn_def_fields[6].data.x_optional = create_const_vals(1);17241 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...@@ -17279,7 +17355,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
17279 ensure_field_index(result->type, "alignment", 3);17355 ensure_field_index(result->type, "alignment", 3);
17280 fields[3].special = ConstValSpecialStatic;17356 fields[3].special = ConstValSpecialStatic;
17281 fields[3].type = get_int_type(ira->codegen, false, 29);17357 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));
17283 // child: type17359 // child: type
17284 ensure_field_index(result->type, "child", 4);17360 ensure_field_index(result->type, "child", 4);
17285 fields[4].special = ConstValSpecialStatic;17361 fields[4].special = ConstValSpecialStatic;
...@@ -18369,7 +18445,21 @@ static ZigType *ir_analyze_instruction_err_set_cast(IrAnalyze *ira, IrInstructio...@@ -18369,7 +18445,21 @@ static ZigType *ir_analyze_instruction_err_set_cast(IrAnalyze *ira, IrInstructio
18369 return dest_type;18445 return dest_type;
18370}18446}
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
18372static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionFromBytes *instruction) {18460static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionFromBytes *instruction) {
18461 Error err;
18462
18373 ZigType *dest_child_type = ir_resolve_type(ira, instruction->dest_child_type->other);18463 ZigType *dest_child_type = ir_resolve_type(ira, instruction->dest_child_type->other);
18374 if (type_is_invalid(dest_child_type))18464 if (type_is_invalid(dest_child_type))
18375 return ira->codegen->builtin_types.entry_invalid;18465 return ira->codegen->builtin_types.entry_invalid;
...@@ -18384,15 +18474,23 @@ static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionF...@@ -18384,15 +18474,23 @@ static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionF
18384 if (target->value.type->id == ZigTypeIdPointer) {18474 if (target->value.type->id == ZigTypeIdPointer) {
18385 src_ptr_const = target->value.type->data.pointer.is_const;18475 src_ptr_const = target->value.type->data.pointer.is_const;
18386 src_ptr_volatile = target->value.type->data.pointer.is_volatile;18476 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;
18388 } else if (is_slice(target->value.type)) {18480 } else if (is_slice(target->value.type)) {
18389 ZigType *src_ptr_type = target->value.type->data.structure.fields[slice_ptr_index].type_entry;18481 ZigType *src_ptr_type = target->value.type->data.structure.fields[slice_ptr_index].type_entry;
18390 src_ptr_const = src_ptr_type->data.pointer.is_const;18482 src_ptr_const = src_ptr_type->data.pointer.is_const;
18391 src_ptr_volatile = src_ptr_type->data.pointer.is_volatile;18483 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;
18393 } else {18487 } else {
18394 src_ptr_const = true;18488 src_ptr_const = true;
18395 src_ptr_volatile = false;18489 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
18396 src_ptr_align = get_abi_alignment(ira->codegen, target->value.type);18494 src_ptr_align = get_abi_alignment(ira->codegen, target->value.type);
18397 }18495 }
1839818496
...@@ -18450,6 +18548,8 @@ static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionF...@@ -18450,6 +18548,8 @@ static ZigType *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstructionF
18450}18548}
1845118549
18452static ZigType *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToBytes *instruction) {18550static ZigType *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToBytes *instruction) {
18551 Error err;
18552
18453 IrInstruction *target = instruction->target->other;18553 IrInstruction *target = instruction->target->other;
18454 if (type_is_invalid(target->value.type))18554 if (type_is_invalid(target->value.type))
18455 return ira->codegen->builtin_types.entry_invalid;18555 return ira->codegen->builtin_types.entry_invalid;
...@@ -18462,9 +18562,13 @@ static ZigType *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToB...@@ -18462,9 +18562,13 @@ static ZigType *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToB
1846218562
18463 ZigType *src_ptr_type = target->value.type->data.structure.fields[slice_ptr_index].type_entry;18563 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
18465 ZigType *dest_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,18569 ZigType *dest_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,
18466 src_ptr_type->data.pointer.is_const, src_ptr_type->data.pointer.is_volatile, PtrLenUnknown,18570 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);
18468 ZigType *dest_slice_type = get_slice_type(ira->codegen, dest_ptr_type);18572 ZigType *dest_slice_type = get_slice_type(ira->codegen, dest_ptr_type);
1846918573
18470 IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_slice_type, CastOpResizeSlice, true);18574 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...@@ -18622,6 +18726,8 @@ static ZigType *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstructionBoo
18622}18726}
1862318727
18624static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemset *instruction) {18728static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemset *instruction) {
18729 Error err;
18730
18625 IrInstruction *dest_ptr = instruction->dest_ptr->other;18731 IrInstruction *dest_ptr = instruction->dest_ptr->other;
18626 if (type_is_invalid(dest_ptr->value.type))18732 if (type_is_invalid(dest_ptr->value.type))
18627 return ira->codegen->builtin_types.entry_invalid;18733 return ira->codegen->builtin_types.entry_invalid;
...@@ -18640,8 +18746,13 @@ static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemse...@@ -18640,8 +18746,13 @@ static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemse
1864018746
18641 ZigType *usize = ira->codegen->builtin_types.entry_usize;18747 ZigType *usize = ira->codegen->builtin_types.entry_usize;
18642 ZigType *u8 = ira->codegen->builtin_types.entry_u8;18748 ZigType *u8 = ira->codegen->builtin_types.entry_u8;
18643 uint32_t dest_align = (dest_uncasted_type->id == ZigTypeIdPointer) ?18749 uint32_t dest_align;
18644 dest_uncasted_type->data.pointer.alignment : get_abi_alignment(ira->codegen, u8);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 }
18645 ZigType *u8_ptr = get_pointer_to_type_extra(ira->codegen, u8, false, dest_is_volatile,18756 ZigType *u8_ptr = get_pointer_to_type_extra(ira->codegen, u8, false, dest_is_volatile,
18646 PtrLenUnknown, dest_align, 0, 0);18757 PtrLenUnknown, dest_align, 0, 0);
1864718758
...@@ -18714,6 +18825,8 @@ static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemse...@@ -18714,6 +18825,8 @@ static ZigType *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructionMemse
18714}18825}
1871518826
18716static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcpy *instruction) {18827static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcpy *instruction) {
18828 Error err;
18829
18717 IrInstruction *dest_ptr = instruction->dest_ptr->other;18830 IrInstruction *dest_ptr = instruction->dest_ptr->other;
18718 if (type_is_invalid(dest_ptr->value.type))18831 if (type_is_invalid(dest_ptr->value.type))
18719 return ira->codegen->builtin_types.entry_invalid;18832 return ira->codegen->builtin_types.entry_invalid;
...@@ -18733,10 +18846,22 @@ static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcp...@@ -18733,10 +18846,22 @@ static ZigType *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructionMemcp
18733 dest_uncasted_type->data.pointer.is_volatile;18846 dest_uncasted_type->data.pointer.is_volatile;
18734 bool src_is_volatile = (src_uncasted_type->id == ZigTypeIdPointer) &&18847 bool src_is_volatile = (src_uncasted_type->id == ZigTypeIdPointer) &&
18735 src_uncasted_type->data.pointer.is_volatile;18848 src_uncasted_type->data.pointer.is_volatile;
18736 uint32_t dest_align = (dest_uncasted_type->id == ZigTypeIdPointer) ?18849
18737 dest_uncasted_type->data.pointer.alignment : get_abi_alignment(ira->codegen, u8);18850 uint32_t dest_align;
18738 uint32_t src_align = (src_uncasted_type->id == ZigTypeIdPointer) ?18851 if (dest_uncasted_type->id == ZigTypeIdPointer) {
18739 src_uncasted_type->data.pointer.alignment : get_abi_alignment(ira->codegen, u8);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
18741 ZigType *usize = ira->codegen->builtin_types.entry_usize;18866 ZigType *usize = ira->codegen->builtin_types.entry_usize;
18742 ZigType *u8_ptr_mut = get_pointer_to_type_extra(ira->codegen, u8, false, dest_is_volatile,18867 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...@@ -18881,17 +19006,13 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
18881 ZigType *return_type;19006 ZigType *return_type;
1888219007
18883 if (array_type->id == ZigTypeIdArray) {19008 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 }
18888 bool is_comptime_const = ptr_ptr->value.special == ConstValSpecialStatic &&19009 bool is_comptime_const = ptr_ptr->value.special == ConstValSpecialStatic &&
18889 ptr_ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst;19010 ptr_ptr->value.data.x_ptr.mut == ConstPtrMutComptimeConst;
18890 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.array.child_type,19011 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.array.child_type,
18891 ptr_type->data.pointer.is_const || is_comptime_const,19012 ptr_type->data.pointer.is_const || is_comptime_const,
18892 ptr_type->data.pointer.is_volatile,19013 ptr_type->data.pointer.is_volatile,
18893 PtrLenUnknown,19014 PtrLenUnknown,
18894 byte_alignment, 0, 0);19015 ptr_type->data.pointer.explicit_alignment, 0, 0);
18895 return_type = get_slice_type(ira->codegen, slice_ptr_type);19016 return_type = get_slice_type(ira->codegen, slice_ptr_type);
18896 } else if (array_type->id == ZigTypeIdPointer) {19017 } else if (array_type->id == ZigTypeIdPointer) {
18897 if (array_type->data.pointer.ptr_len == PtrLenSingle) {19018 if (array_type->data.pointer.ptr_len == PtrLenSingle) {
...@@ -18901,7 +19022,7 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice...@@ -18901,7 +19022,7 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
18901 main_type->data.pointer.child_type,19022 main_type->data.pointer.child_type,
18902 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,19023 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
18903 PtrLenUnknown,19024 PtrLenUnknown,
18904 array_type->data.pointer.alignment, 0, 0);19025 array_type->data.pointer.explicit_alignment, 0, 0);
18905 return_type = get_slice_type(ira->codegen, slice_ptr_type);19026 return_type = get_slice_type(ira->codegen, slice_ptr_type);
18906 } else {19027 } else {
18907 ir_add_error(ira, &instruction->base, buf_sprintf("slice of single-item pointer"));19028 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...@@ -18911,7 +19032,7 @@ static ZigType *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructionSlice
18911 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,19032 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,
18912 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,19033 array_type->data.pointer.is_const, array_type->data.pointer.is_volatile,
18913 PtrLenUnknown,19034 PtrLenUnknown,
18914 array_type->data.pointer.alignment, 0, 0);19035 array_type->data.pointer.explicit_alignment, 0, 0);
18915 return_type = get_slice_type(ira->codegen, slice_ptr_type);19036 return_type = get_slice_type(ira->codegen, slice_ptr_type);
18916 if (!end) {19037 if (!end) {
18917 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));19038 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...@@ -19292,7 +19413,7 @@ static ZigType *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAli
19292 return ira->codegen->builtin_types.entry_invalid;19413 return ira->codegen->builtin_types.entry_invalid;
19293 ZigType *type_entry = ir_resolve_type(ira, type_value);19414 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)))
19296 return ira->codegen->builtin_types.entry_invalid;19417 return ira->codegen->builtin_types.entry_invalid;
1929719418
19298 switch (type_entry->id) {19419 switch (type_entry->id) {
...@@ -19336,6 +19457,8 @@ static ZigType *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAli...@@ -19336,6 +19457,8 @@ static ZigType *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAli
19336}19457}
1933719458
19338static ZigType *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {19459static ZigType *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {
19460 Error err;
19461
19339 IrInstruction *type_value = instruction->type_value->other;19462 IrInstruction *type_value = instruction->type_value->other;
19340 if (type_is_invalid(type_value->value.type))19463 if (type_is_invalid(type_value->value.type))
19341 return ira->codegen->builtin_types.entry_invalid;19464 return ira->codegen->builtin_types.entry_invalid;
...@@ -19379,10 +19502,13 @@ static ZigType *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstruction...@@ -19379,10 +19502,13 @@ static ZigType *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstruction
1937919502
19380 ZigType *expected_ptr_type;19503 ZigType *expected_ptr_type;
19381 if (result_ptr->value.type->id == ZigTypeIdPointer) {19504 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;
19382 expected_ptr_type = get_pointer_to_type_extra(ira->codegen, dest_type,19508 expected_ptr_type = get_pointer_to_type_extra(ira->codegen, dest_type,
19383 false, result_ptr->value.type->data.pointer.is_volatile,19509 false, result_ptr->value.type->data.pointer.is_volatile,
19384 PtrLenSingle,19510 PtrLenSingle,
19385 result_ptr->value.type->data.pointer.alignment, 0, 0);19511 alignment, 0, 0);
19386 } else {19512 } else {
19387 expected_ptr_type = get_pointer_to_type(ira->codegen, dest_type, false);19513 expected_ptr_type = get_pointer_to_type(ira->codegen, dest_type, false);
19388 }19514 }
...@@ -19544,8 +19670,7 @@ static ZigType *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,...@@ -19544,8 +19670,7 @@ static ZigType *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
19544 }19670 }
19545 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, payload_type,19671 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, payload_type,
19546 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,19672 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
19547 PtrLenSingle,19673 PtrLenSingle, 0, 0, 0);
19548 get_abi_alignment(ira->codegen, payload_type), 0, 0);
19549 if (instr_is_comptime(value)) {19674 if (instr_is_comptime(value)) {
19550 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);19675 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
19551 if (!ptr_val)19676 if (!ptr_val)
...@@ -19624,7 +19749,7 @@ static ZigType *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnP...@@ -19624,7 +19749,7 @@ static ZigType *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnP
19624 ZigType *param_type = ir_resolve_type(ira, param_type_value);19749 ZigType *param_type = ir_resolve_type(ira, param_type_value);
19625 if (type_is_invalid(param_type))19750 if (type_is_invalid(param_type))
19626 return ira->codegen->builtin_types.entry_invalid;19751 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)))
19628 return ira->codegen->builtin_types.entry_invalid;19753 return ira->codegen->builtin_types.entry_invalid;
19629 if (type_requires_comptime(param_type)) {19754 if (type_requires_comptime(param_type)) {
19630 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {19755 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
...@@ -19899,7 +20024,7 @@ static ZigType *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructionPanic...@@ -19899,7 +20024,7 @@ static ZigType *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructionPanic
19899 }20024 }
1990020025
19901 ZigType *u8_ptr_type = get_pointer_to_type_extra(ira->codegen, ira->codegen->builtin_types.entry_u8,20026 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);
19903 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);20028 ZigType *str_type = get_slice_type(ira->codegen, u8_ptr_type);
19904 IrInstruction *casted_msg = ir_implicit_cast(ira, msg, str_type);20029 IrInstruction *casted_msg = ir_implicit_cast(ira, msg, str_type);
19905 if (type_is_invalid(casted_msg->value.type))20030 if (type_is_invalid(casted_msg->value.type))
...@@ -19912,6 +20037,8 @@ static ZigType *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructionPanic...@@ -19912,6 +20037,8 @@ static ZigType *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructionPanic
19912}20037}
1991320038
19914static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint32_t align_bytes, bool safety_check_on) {20039static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint32_t align_bytes, bool safety_check_on) {
20040 Error err;
20041
19915 ZigType *target_type = target->value.type;20042 ZigType *target_type = target->value.type;
19916 assert(!type_is_invalid(target_type));20043 assert(!type_is_invalid(target_type));
1991720044
...@@ -19920,7 +20047,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3...@@ -19920,7 +20047,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
1992020047
19921 if (target_type->id == ZigTypeIdPointer) {20048 if (target_type->id == ZigTypeIdPointer) {
19922 result_type = adjust_ptr_align(ira->codegen, target_type, align_bytes);20049 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;
19924 } else if (target_type->id == ZigTypeIdFn) {20052 } else if (target_type->id == ZigTypeIdFn) {
19925 FnTypeId fn_type_id = target_type->data.fn.fn_type_id;20053 FnTypeId fn_type_id = target_type->data.fn.fn_type_id;
19926 old_align_bytes = fn_type_id.alignment;20054 old_align_bytes = fn_type_id.alignment;
...@@ -19930,7 +20058,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3...@@ -19930,7 +20058,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
19930 target_type->data.maybe.child_type->id == ZigTypeIdPointer)20058 target_type->data.maybe.child_type->id == ZigTypeIdPointer)
19931 {20059 {
19932 ZigType *ptr_type = target_type->data.maybe.child_type;20060 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;
19934 ZigType *better_ptr_type = adjust_ptr_align(ira->codegen, ptr_type, align_bytes);20063 ZigType *better_ptr_type = adjust_ptr_align(ira->codegen, ptr_type, align_bytes);
1993520064
19936 result_type = get_optional_type(ira->codegen, better_ptr_type);20065 result_type = get_optional_type(ira->codegen, better_ptr_type);
...@@ -19944,7 +20073,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3...@@ -19944,7 +20073,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
19944 result_type = get_optional_type(ira->codegen, fn_type);20073 result_type = get_optional_type(ira->codegen, fn_type);
19945 } else if (is_slice(target_type)) {20074 } else if (is_slice(target_type)) {
19946 ZigType *slice_ptr_type = target_type->data.structure.fields[slice_ptr_index].type_entry;20075 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;
19948 ZigType *result_ptr_type = adjust_ptr_align(ira->codegen, slice_ptr_type, align_bytes);20078 ZigType *result_ptr_type = adjust_ptr_align(ira->codegen, slice_ptr_type, align_bytes);
19949 result_type = get_slice_type(ira->codegen, result_ptr_type);20079 result_type = get_slice_type(ira->codegen, result_ptr_type);
19950 } else {20080 } else {
...@@ -20023,8 +20153,13 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr...@@ -20023,8 +20153,13 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr
20023 return dest_type;20153 return dest_type;
20024 }20154 }
2002520155
20026 uint32_t src_align_bytes = get_ptr_align(ira->codegen, src_type);20156 uint32_t src_align_bytes;
20027 uint32_t dest_align_bytes = get_ptr_align(ira->codegen, dest_type);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
20029 if (dest_align_bytes > src_align_bytes) {20164 if (dest_align_bytes > src_align_bytes) {
20030 ErrorMsg *msg = ir_add_error(ira, &instruction->base, buf_sprintf("cast increases pointer alignment"));20165 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...@@ -20041,7 +20176,7 @@ static ZigType *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtr
2004120176
20042 // Keep the bigger alignment, it can only help-20177 // Keep the bigger alignment, it can only help-
20043 // unless the target is zero bits.20178 // 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)))
20045 return ira->codegen->builtin_types.entry_invalid;20180 return ira->codegen->builtin_types.entry_invalid;
2004620181
20047 IrInstruction *result;20182 IrInstruction *result;
...@@ -20289,7 +20424,7 @@ static ZigType *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstructionI...@@ -20289,7 +20424,7 @@ static ZigType *ir_analyze_instruction_int_to_ptr(IrAnalyze *ira, IrInstructionI
20289 return ira->codegen->builtin_types.entry_invalid;20424 return ira->codegen->builtin_types.entry_invalid;
20290 }20425 }
2029120426
20292 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))20427 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
20293 return ira->codegen->builtin_types.entry_invalid;20428 return ira->codegen->builtin_types.entry_invalid;
20294 if (!type_has_bits(dest_type)) {20429 if (!type_has_bits(dest_type)) {
20295 ir_add_error(ira, dest_type_value,20430 ir_add_error(ira, dest_type_value,
...@@ -20440,12 +20575,15 @@ static ZigType *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtr...@@ -20440,12 +20575,15 @@ static ZigType *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstructionPtr
20440 if (instruction->align_value != nullptr) {20575 if (instruction->align_value != nullptr) {
20441 if (!ir_resolve_align(ira, instruction->align_value->other, &align_bytes))20576 if (!ir_resolve_align(ira, instruction->align_value->other, &align_bytes))
20442 return ira->codegen->builtin_types.entry_invalid;20577 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;
20443 } else {20580 } else {
20444 if ((err = type_ensure_zero_bits_known(ira->codegen, child_type)))20581 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
20445 return ira->codegen->builtin_types.entry_invalid;20582 return ira->codegen->builtin_types.entry_invalid;
20446 align_bytes = get_abi_alignment(ira->codegen, child_type);20583 align_bytes = 0;
20447 }20584 }
2044820585
20586
20449 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);20587 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
20450 out_val->data.x_type = get_pointer_to_type_extra(ira->codegen, child_type,20588 out_val->data.x_type = get_pointer_to_type_extra(ira->codegen, child_type,
20451 instruction->is_const, instruction->is_volatile,20589 instruction->is_const, instruction->is_volatile,
...@@ -21089,7 +21227,7 @@ static ZigType *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstruction...@@ -21089,7 +21227,7 @@ static ZigType *ir_analyze_instruction_enum_to_int(IrAnalyze *ira, IrInstruction
21089 return ira->codegen->builtin_types.entry_invalid;21227 return ira->codegen->builtin_types.entry_invalid;
21090 }21228 }
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)))
21093 return ira->codegen->builtin_types.entry_invalid;21231 return ira->codegen->builtin_types.entry_invalid;
2109421232
21095 ZigType *tag_type = target->value.type->data.enumeration.tag_int_type;21233 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...@@ -21112,7 +21250,7 @@ static ZigType *ir_analyze_instruction_int_to_enum(IrAnalyze *ira, IrInstruction
21112 return ira->codegen->builtin_types.entry_invalid;21250 return ira->codegen->builtin_types.entry_invalid;
21113 }21251 }
2111421252
21115 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))21253 if ((err = type_resolve(ira->codegen, dest_type, ResolveStatusZeroBitsKnown)))
21116 return ira->codegen->builtin_types.entry_invalid;21254 return ira->codegen->builtin_types.entry_invalid;
2111721255
21118 ZigType *tag_type = dest_type->data.enumeration.tag_int_type;21256 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 {...@@ -916,7 +916,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo {
916 } else {916 } else {
917 return error.MissingDebugInfo;917 return error.MissingDebugInfo;
918 };918 };
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];
920 const strings = @ptrCast([*]u8, hdr_base + symtab.stroff)[0..symtab.strsize];920 const strings = @ptrCast([*]u8, hdr_base + symtab.stroff)[0..symtab.strsize];
921921
922 const symbols_buf = try allocator.alloc(MachoSymbol, syms.len);922 const symbols_buf = try allocator.alloc(MachoSymbol, syms.len);
...@@ -1497,14 +1497,14 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u...@@ -1497,14 +1497,14 @@ fn getLineNumberInfoMacOs(di: *DebugInfo, symbol: MachoSymbol, target_address: u
1497 const segcmd = while (ncmd != 0) : (ncmd -= 1) {1497 const segcmd = while (ncmd != 0) : (ncmd -= 1) {
1498 const lc = @ptrCast(*const std.macho.load_command, ptr);1498 const lc = @ptrCast(*const std.macho.load_command, ptr);
1499 switch (lc.cmd) {1499 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)),
1501 else => {},1501 else => {},
1502 }1502 }
1503 ptr += lc.cmdsize; // TODO https://github.com/ziglang/zig/issues/14031503 ptr += lc.cmdsize; // TODO https://github.com/ziglang/zig/issues/1403
1504 } else {1504 } else {
1505 return error.MissingDebugInfo;1505 return error.MissingDebugInfo;
1506 };1506 };
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];
1508 for (sections) |*sect| {1508 for (sections) |*sect| {
1509 if (sect.flags & macho.SECTION_TYPE == macho.S_REGULAR and1509 if (sect.flags & macho.SECTION_TYPE == macho.S_REGULAR and
1510 (sect.flags & macho.SECTION_ATTRIBUTES) & macho.S_ATTR_DEBUG == macho.S_ATTR_DEBUG)1510 (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 {...@@ -212,3 +212,10 @@ fn fnWithAlignedStack() i32 {
212 @setAlignStack(256);212 @setAlignStack(256);
213 return 1234;213 return 1234;
214}214}
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 {...@@ -3444,7 +3444,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
3444 \\3444 \\
3445 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }3445 \\export fn entry() usize { return @sizeOf(@typeOf(foo)); }
3446 ,3446 ,
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'",
3448 );3448 );
34493449
3450 cases.add(3450 cases.add(