authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-08 18:39:52-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-08 18:39:52-05:00
log6504c5098ead851b369af0525c11ad2ea4cee8a2
tree3eaa22acec3003b3dfc5965d9a475c63afbd23ce
parentd5e788072d8b207334c0eab0f1289317a55c9344
signature Commit is signed but in an unrecognized format.

tuple detection does not require AST node


6 files changed, 42 insertions(+), 23 deletions(-)

src/all_types.hpp+8-2
......@@ -1308,6 +1308,13 @@ struct RootStruct {
13081308 ZigLLVMDIFile *di_file;
13091309};
13101310
1311enum StructSpecial {
1312 StructSpecialNone,
1313 StructSpecialSlice,
1314 StructSpecialInferredTuple,
1315 StructSpecialInferredStruct,
1316};
1317
13111318struct ZigTypeStruct {
13121319 AstNode *decl_node;
13131320 TypeStructField **fields;
......@@ -1323,13 +1330,12 @@ struct ZigTypeStruct {
13231330 ContainerLayout layout;
13241331 ResolveStatus resolve_status;
13251332
1326 bool is_slice;
1333 StructSpecial special;
13271334 // whether any of the fields require comptime
13281335 // known after ResolveStatusZeroBitsKnown
13291336 bool requires_comptime;
13301337 bool resolve_loop_flag_zero_bits;
13311338 bool resolve_loop_flag_other;
1332 bool is_inferred;
13331339};
13341340
13351341struct ZigTypeOptional {
src/analyze.cpp+12-6
......@@ -420,7 +420,7 @@ uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {
420420}
421421
422422static bool is_slice(ZigType *type) {
423 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
423 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialSlice;
424424}
425425
426426ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x) {
......@@ -832,7 +832,7 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
832832
833833 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
834834 entry->data.structure.layout = ContainerLayoutAuto;
835 entry->data.structure.is_slice = true;
835 entry->data.structure.special = StructSpecialSlice;
836836 entry->data.structure.src_field_count = element_count;
837837 entry->data.structure.gen_field_count = element_count;
838838 entry->data.structure.fields = alloc_type_struct_fields(element_count);
......@@ -2752,7 +2752,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27522752 } else if (decl_node->type == NodeTypeContainerInitExpr) {
27532753 field_count = struct_type->data.structure.src_field_count;
27542754
2755 src_assert(struct_type->data.structure.is_inferred, decl_node);
2755 src_assert(is_anon_container(struct_type), decl_node);
27562756 src_assert(field_count == 0 || struct_type->data.structure.fields != nullptr, decl_node);
27572757 } else zig_unreachable();
27582758
......@@ -4256,7 +4256,7 @@ bool is_container(ZigType *type_entry) {
42564256 case ZigTypeIdInvalid:
42574257 zig_unreachable();
42584258 case ZigTypeIdStruct:
4259 return !type_entry->data.structure.is_slice;
4259 return type_entry->data.structure.special != StructSpecialSlice;
42604260 case ZigTypeIdEnum:
42614261 case ZigTypeIdUnion:
42624262 return true;
......@@ -7391,7 +7391,7 @@ size_t type_id_index(ZigType *entry) {
73917391 case ZigTypeIdArray:
73927392 return 7;
73937393 case ZigTypeIdStruct:
7394 if (entry->data.structure.is_slice)
7394 if (entry->data.structure.special == StructSpecialSlice)
73957395 return 6;
73967396 return 8;
73977397 case ZigTypeIdComptimeFloat:
......@@ -9077,7 +9077,7 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r
90779077 assert(type->llvm_di_type != nullptr);
90789078 return;
90799079 case ZigTypeIdStruct:
9080 if (type->data.structure.is_slice)
9080 if (type->data.structure.special == StructSpecialSlice)
90819081 return resolve_llvm_types_slice(g, type, wanted_resolve_status);
90829082 else
90839083 return resolve_llvm_types_struct(g, type, wanted_resolve_status, nullptr);
......@@ -9229,3 +9229,9 @@ void IrExecutable::src() {
92299229 it->source_node->src();
92309230 }
92319231}
9232
9233bool is_anon_container(ZigType *ty) {
9234 return ty->id == ZigTypeIdStruct && (
9235 ty->data.structure.special == StructSpecialInferredTuple ||
9236 ty->data.structure.special == StructSpecialInferredStruct);
9237}
src/analyze.hpp+1
......@@ -278,4 +278,5 @@ IrInstruction *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node,
278278Error analyze_import(CodeGen *codegen, ZigType *source_import, Buf *import_target_str,
279279 ZigType **out_import, Buf **out_import_target_path, Buf *out_full_path);
280280ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry);
281bool is_anon_container(ZigType *ty);
281282#endif
src/codegen.cpp+7-5
......@@ -2998,9 +2998,9 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29982998
29992999 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
30003000 assert(wanted_type->id == ZigTypeIdStruct);
3001 assert(wanted_type->data.structure.is_slice);
3001 assert(wanted_type->data.structure.special == StructSpecialSlice);
30023002 assert(actual_type->id == ZigTypeIdStruct);
3003 assert(actual_type->data.structure.is_slice);
3003 assert(actual_type->data.structure.special == StructSpecialSlice);
30043004
30053005 ZigType *actual_pointer_type = actual_type->data.structure.fields[0]->type_entry;
30063006 ZigType *actual_child_type = actual_pointer_type->data.pointer.child_type;
......@@ -3751,7 +3751,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
37513751 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");
37523752 } else if (array_type->id == ZigTypeIdStruct) {
37533753 LLVMValueRef array_ptr = get_handle_value(g, array_ptr_ptr, array_type, array_ptr_type);
3754 assert(array_type->data.structure.is_slice);
3754 assert(array_type->data.structure.special == StructSpecialSlice);
37553755
37563756 ZigType *ptr_type = array_type->data.structure.fields[slice_ptr_index]->type_entry;
37573757 if (!type_has_bits(ptr_type)) {
......@@ -5028,7 +5028,9 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
50285028 {
50295029 align_bytes = target_type->data.maybe.child_type->data.fn.fn_type_id.alignment;
50305030 ptr_val = target_val;
5031 } else if (target_type->id == ZigTypeIdStruct && target_type->data.structure.is_slice) {
5031 } else if (target_type->id == ZigTypeIdStruct &&
5032 target_type->data.structure.special == StructSpecialSlice)
5033 {
50325034 ZigType *slice_ptr_type = target_type->data.structure.fields[slice_ptr_index]->type_entry;
50335035 align_bytes = get_ptr_align(g, slice_ptr_type);
50345036
......@@ -5290,7 +5292,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
52905292
52915293 return tmp_struct_ptr;
52925294 } else if (array_type->id == ZigTypeIdStruct) {
5293 assert(array_type->data.structure.is_slice);
5295 assert(array_type->data.structure.special == StructSpecialSlice);
52945296 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
52955297 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);
52965298 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(tmp_struct_ptr))) == LLVMStructTypeKind);
src/dump_analysis.cpp+1-1
......@@ -744,7 +744,7 @@ static void anal_dump_type(AnalDumpCtx *ctx, ZigType *ty) {
744744 case ZigTypeIdEnumLiteral:
745745 break;
746746 case ZigTypeIdStruct: {
747 if (ty->data.structure.is_slice) {
747 if (ty->data.structure.special == StructSpecialSlice) {
748748 jw_object_field(jw, "len");
749749 jw_int(jw, 2);
750750 anal_dump_pointer_attrs(ctx, ty->data.structure.fields[slice_ptr_index]->type_entry);
src/ir.cpp+13-9
......@@ -724,14 +724,11 @@ static bool is_opt_err_set(ZigType *ty) {
724724}
725725
726726static bool is_tuple(ZigType *type) {
727 return type->id == ZigTypeIdStruct && type->data.structure.decl_node != nullptr &&
728 type->data.structure.decl_node->type == NodeTypeContainerInitExpr &&
729 (type->data.structure.decl_node->data.container_init_expr.kind == ContainerInitKindArray ||
730 type->data.structure.decl_node->data.container_init_expr.entries.length == 0);
727 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialInferredTuple;
731728}
732729
733730static bool is_slice(ZigType *type) {
734 return type->id == ZigTypeIdStruct && type->data.structure.is_slice;
731 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialSlice;
735732}
736733
737734static bool slice_is_const(ZigType *type) {
......@@ -13928,7 +13925,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1392813925 }
1392913926
1393013927 // cast from inferred struct type to array, union, or struct
13931 if (actual_type->id == ZigTypeIdStruct && actual_type->data.structure.is_inferred) {
13928 if (is_anon_container(actual_type)) {
1393213929 AstNode *decl_node = actual_type->data.structure.decl_node;
1393313930 ir_assert(decl_node->type == NodeTypeContainerInitExpr, source_instr);
1393413931 ContainerInitKind init_kind = decl_node->data.container_init_expr.kind;
......@@ -17047,10 +17044,17 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira,
1704717044 Buf *name = get_anon_type_name(ira->codegen, nullptr, container_string(ContainerKindStruct),
1704817045 instruction->base.scope, instruction->base.source_node, bare_name);
1704917046
17047 StructSpecial struct_special = StructSpecialInferredStruct;
17048 if (instruction->base.source_node->type == NodeTypeContainerInitExpr &&
17049 instruction->base.source_node->data.container_init_expr.kind == ContainerInitKindArray)
17050 {
17051 struct_special = StructSpecialInferredTuple;
17052 }
17053
1705017054 ZigType *inferred_struct_type = get_partial_container_type(ira->codegen,
1705117055 instruction->base.scope, ContainerKindStruct, instruction->base.source_node,
1705217056 buf_ptr(name), bare_name, ContainerLayoutAuto);
17053 inferred_struct_type->data.structure.is_inferred = true;
17057 inferred_struct_type->data.structure.special = struct_special;
1705417058 inferred_struct_type->data.structure.resolve_status = ResolveStatusBeingInferred;
1705517059 implicit_elem_type = inferred_struct_type;
1705617060 }
......@@ -19570,7 +19574,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
1957019574 bool is_const = struct_ptr->value->type->data.pointer.is_const;
1957119575 bool is_volatile = struct_ptr->value->type->data.pointer.is_volatile;
1957219576 ZigType *ptr_type;
19573 if (struct_type->data.structure.is_inferred) {
19577 if (is_anon_container(struct_type)) {
1957419578 ptr_type = get_pointer_to_type_extra(ira->codegen, field_type,
1957519579 is_const, is_volatile, PtrLenSingle, 0, 0, 0, false);
1957619580 } else {
......@@ -22816,7 +22820,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2281622820 }
2281722821 case ZigTypeIdStruct:
2281822822 {
22819 if (type_entry->data.structure.is_slice) {
22823 if (type_entry->data.structure.special == StructSpecialSlice) {
2282022824 result = create_ptr_like_type_info(ira, type_entry);
2282122825 if (result == nullptr)
2282222826 return ErrorSemanticAnalyzeFail;