authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-09 09:27:14-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-05-09 09:27:14-04:00
log116914ab3e72491c863ca8a062bd8fa184c01e04
tree84572f9ee727bf371bc55c0b232691b8e1ac9484
parent670c9f9b741651f8b9873356a9e24da07c3ed355
parent2a74aa206781b56d3aae5c0e8a94c75f0d73ac51
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1000 from zig-lang/slice-type-info

Added Slice as its own type info in userland

5 files changed, 79 insertions(+), 39 deletions(-)

src/analyze.cpp+4-2
...@@ -5931,8 +5931,8 @@ size_t type_id_len() {...@@ -5931,8 +5931,8 @@ size_t type_id_len() {
5931 return array_length(all_type_ids);5931 return array_length(all_type_ids);
5932}5932}
59335933
5934size_t type_id_index(TypeTableEntryId id) {5934size_t type_id_index(TypeTableEntry *entry) {
5935 switch (id) {5935 switch (entry->id) {
5936 case TypeTableEntryIdInvalid:5936 case TypeTableEntryIdInvalid:
5937 zig_unreachable();5937 zig_unreachable();
5938 case TypeTableEntryIdMetaType:5938 case TypeTableEntryIdMetaType:
...@@ -5952,6 +5952,8 @@ size_t type_id_index(TypeTableEntryId id) {...@@ -5952,6 +5952,8 @@ size_t type_id_index(TypeTableEntryId id) {
5952 case TypeTableEntryIdArray:5952 case TypeTableEntryIdArray:
5953 return 7;5953 return 7;
5954 case TypeTableEntryIdStruct:5954 case TypeTableEntryIdStruct:
5955 if (entry->data.structure.is_slice)
5956 return 25;
5955 return 8;5957 return 8;
5956 case TypeTableEntryIdNumLitFloat:5958 case TypeTableEntryIdNumLitFloat:
5957 return 9;5959 return 9;
src/analyze.hpp+1-1
...@@ -174,7 +174,7 @@ void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);...@@ -174,7 +174,7 @@ void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
174const char *type_id_name(TypeTableEntryId id);174const char *type_id_name(TypeTableEntryId id);
175TypeTableEntryId type_id_at_index(size_t index);175TypeTableEntryId type_id_at_index(size_t index);
176size_t type_id_len();176size_t type_id_len();
177size_t type_id_index(TypeTableEntryId id);177size_t type_id_index(TypeTableEntry *entry);
178TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);178TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);
179bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);179bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);
180LinkLib *create_link_lib(Buf *name);180LinkLib *create_link_lib(Buf *name);
src/codegen.cpp+4
...@@ -6345,6 +6345,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6345,6 +6345,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6345 const TypeTableEntryId id = type_id_at_index(i);6345 const TypeTableEntryId id = type_id_at_index(i);
6346 buf_appendf(contents, " %s,\n", type_id_name(id));6346 buf_appendf(contents, " %s,\n", type_id_name(id));
6347 }6347 }
6348 buf_appendf(contents, " Slice,\n");
6348 buf_appendf(contents, "};\n\n");6349 buf_appendf(contents, "};\n\n");
6349 }6350 }
6350 {6351 {
...@@ -6357,6 +6358,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6357,6 +6358,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6357 " Int: Int,\n"6358 " Int: Int,\n"
6358 " Float: Float,\n"6359 " Float: Float,\n"
6359 " Pointer: Pointer,\n"6360 " Pointer: Pointer,\n"
6361 " Slice: Slice,\n"
6360 " Array: Array,\n"6362 " Array: Array,\n"
6361 " Struct: Struct,\n"6363 " Struct: Struct,\n"
6362 " FloatLiteral: void,\n"6364 " FloatLiteral: void,\n"
...@@ -6392,6 +6394,8 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6392,6 +6394,8 @@ static void define_builtin_compile_vars(CodeGen *g) {
6392 " child: type,\n"6394 " child: type,\n"
6393 " };\n"6395 " };\n"
6394 "\n"6396 "\n"
6397 " pub const Slice = Pointer;\n"
6398 "\n"
6395 " pub const Array = struct {\n"6399 " pub const Array = struct {\n"
6396 " len: usize,\n"6400 " len: usize,\n"
6397 " child: type,\n"6401 " child: type,\n"
src/ir.cpp+48-33
...@@ -15785,11 +15785,10 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na...@@ -15785,11 +15785,10 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na
1578515785
15786 Buf field_name = BUF_INIT;15786 Buf field_name = BUF_INIT;
15787 buf_init_from_str(&field_name, type_name);15787 buf_init_from_str(&field_name, type_name);
15788 auto entry = type_info_scope->decl_table.maybe_get(&field_name);15788 auto entry = type_info_scope->decl_table.get(&field_name);
15789 buf_deinit(&field_name);15789 buf_deinit(&field_name);
15790 assert(entry != nullptr);
1579115790
15792 TldVar *tld = (TldVar *)entry->value;15791 TldVar *tld = (TldVar *)entry;
15793 assert(tld->base.id == TldIdVar);15792 assert(tld->base.id == TldIdVar);
1579415793
15795 VariableTableEntry *var = tld->var;15794 VariableTableEntry *var = tld->var;
...@@ -16071,6 +16070,38 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16071,6 +16070,38 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16071 enum_field_val->data.x_struct.fields = inner_fields;16070 enum_field_val->data.x_struct.fields = inner_fields;
16072 };16071 };
1607316072
16073 const auto create_ptr_like_type_info = [ira](const char *name, TypeTableEntry *ptr_type_entry) {
16074 ConstExprValue *result = create_const_vals(1);
16075 result->special = ConstValSpecialStatic;
16076 result->type = ir_type_info_get_type(ira, name);
16077
16078 ConstExprValue *fields = create_const_vals(4);
16079 result->data.x_struct.fields = fields;
16080
16081 // is_const: bool
16082 ensure_field_index(result->type, "is_const", 0);
16083 fields[0].special = ConstValSpecialStatic;
16084 fields[0].type = ira->codegen->builtin_types.entry_bool;
16085 fields[0].data.x_bool = ptr_type_entry->data.pointer.is_const;
16086 // is_volatile: bool
16087 ensure_field_index(result->type, "is_volatile", 1);
16088 fields[1].special = ConstValSpecialStatic;
16089 fields[1].type = ira->codegen->builtin_types.entry_bool;
16090 fields[1].data.x_bool = ptr_type_entry->data.pointer.is_volatile;
16091 // alignment: u32
16092 ensure_field_index(result->type, "alignment", 2);
16093 fields[2].special = ConstValSpecialStatic;
16094 fields[2].type = ira->codegen->builtin_types.entry_u32;
16095 bigint_init_unsigned(&fields[2].data.x_bigint, ptr_type_entry->data.pointer.alignment);
16096 // child: type
16097 ensure_field_index(result->type, "child", 3);
16098 fields[3].special = ConstValSpecialStatic;
16099 fields[3].type = ira->codegen->builtin_types.entry_type;
16100 fields[3].data.x_type = ptr_type_entry->data.pointer.child_type;
16101
16102 return result;
16103 };
16104
16074 ConstExprValue *result = nullptr;16105 ConstExprValue *result = nullptr;
16075 switch (type_entry->id)16106 switch (type_entry->id)
16076 {16107 {
...@@ -16139,34 +16170,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16139,34 +16170,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16139 }16170 }
16140 case TypeTableEntryIdPointer:16171 case TypeTableEntryIdPointer:
16141 {16172 {
16142 result = create_const_vals(1);16173 result = create_ptr_like_type_info("Pointer", type_entry);
16143 result->special = ConstValSpecialStatic;
16144 result->type = ir_type_info_get_type(ira, "Pointer");
16145
16146 ConstExprValue *fields = create_const_vals(4);
16147 result->data.x_struct.fields = fields;
16148
16149 // is_const: bool
16150 ensure_field_index(result->type, "is_const", 0);
16151 fields[0].special = ConstValSpecialStatic;
16152 fields[0].type = ira->codegen->builtin_types.entry_bool;
16153 fields[0].data.x_bool = type_entry->data.pointer.is_const;
16154 // is_volatile: bool
16155 ensure_field_index(result->type, "is_volatile", 1);
16156 fields[1].special = ConstValSpecialStatic;
16157 fields[1].type = ira->codegen->builtin_types.entry_bool;
16158 fields[1].data.x_bool = type_entry->data.pointer.is_volatile;
16159 // alignment: u32
16160 ensure_field_index(result->type, "alignment", 2);
16161 fields[2].special = ConstValSpecialStatic;
16162 fields[2].type = ira->codegen->builtin_types.entry_u32;
16163 bigint_init_unsigned(&fields[2].data.x_bigint, type_entry->data.pointer.alignment);
16164 // child: type
16165 ensure_field_index(result->type, "child", 3);
16166 fields[3].special = ConstValSpecialStatic;
16167 fields[3].type = ira->codegen->builtin_types.entry_type;
16168 fields[3].data.x_type = type_entry->data.pointer.child_type;
16169
16170 break;16174 break;
16171 }16175 }
16172 case TypeTableEntryIdArray:16176 case TypeTableEntryIdArray:
...@@ -16436,6 +16440,17 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16436,6 +16440,17 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16436 }16440 }
16437 case TypeTableEntryIdStruct:16441 case TypeTableEntryIdStruct:
16438 {16442 {
16443 if (type_entry->data.structure.is_slice) {
16444 Buf ptr_field_name = BUF_INIT;
16445 buf_init_from_str(&ptr_field_name, "ptr");
16446 TypeTableEntry *ptr_type = type_entry->data.structure.fields_by_name.get(&ptr_field_name)->type_entry;
16447 ensure_complete_type(ira->codegen, ptr_type);
16448 buf_deinit(&ptr_field_name);
16449
16450 result = create_ptr_like_type_info("Slice", ptr_type);
16451 break;
16452 }
16453
16439 result = create_const_vals(1);16454 result = create_const_vals(1);
16440 result->special = ConstValSpecialStatic;16455 result->special = ConstValSpecialStatic;
16441 result->type = ir_type_info_get_type(ira, "Struct");16456 result->type = ir_type_info_get_type(ira, "Struct");
...@@ -16622,7 +16637,7 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,...@@ -16622,7 +16637,7 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
1662216637
16623 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);16638 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
16624 out_val->type = result_type;16639 out_val->type = result_type;
16625 bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry->id));16640 bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry));
1662616641
16627 ConstExprValue *payload = ir_make_type_info_value(ira, type_entry);16642 ConstExprValue *payload = ir_make_type_info_value(ira, type_entry);
16628 out_val->data.x_union.payload = payload;16643 out_val->data.x_union.payload = payload;
...@@ -16650,7 +16665,7 @@ static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,...@@ -16650,7 +16665,7 @@ static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
16650 TypeTableEntry *result_type = var_value->data.x_type;16665 TypeTableEntry *result_type = var_value->data.x_type;
1665116666
16652 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);16667 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
16653 bigint_init_unsigned(&out_val->data.x_enum_tag, type_id_index(type_entry->id));16668 bigint_init_unsigned(&out_val->data.x_enum_tag, type_id_index(type_entry));
16654 return result_type;16669 return result_type;
16655}16670}
1665616671
test/cases/type_info.zig+22-3
...@@ -25,7 +25,7 @@ test "type info: integer, floating point type info" {...@@ -25,7 +25,7 @@ test "type info: integer, floating point type info" {
25 }25 }
26}26}
2727
28test "type info: pointer, array and nullable type info" {28test "type info: pointer type info" {
29 comptime {29 comptime {
30 const u32_ptr_info = @typeInfo(&u32);30 const u32_ptr_info = @typeInfo(&u32);
31 assert(TypeId(u32_ptr_info) == TypeId.Pointer);31 assert(TypeId(u32_ptr_info) == TypeId.Pointer);
...@@ -33,12 +33,31 @@ test "type info: pointer, array and nullable type info" {...@@ -33,12 +33,31 @@ test "type info: pointer, array and nullable type info" {
33 assert(u32_ptr_info.Pointer.is_volatile == false);33 assert(u32_ptr_info.Pointer.is_volatile == false);
34 assert(u32_ptr_info.Pointer.alignment == 4);34 assert(u32_ptr_info.Pointer.alignment == 4);
35 assert(u32_ptr_info.Pointer.child == u32);35 assert(u32_ptr_info.Pointer.child == u32);
36 }
37}
38
39test "type info: slice type info" {
40 comptime {
41 const u32_slice_info = @typeInfo([]u32);
42 assert(TypeId(u32_slice_info) == TypeId.Slice);
43 assert(u32_slice_info.Slice.is_const == false);
44 assert(u32_slice_info.Slice.is_volatile == false);
45 assert(u32_slice_info.Slice.alignment == 4);
46 assert(u32_slice_info.Slice.child == u32);
47 }
48}
3649
50test "type info: array type info" {
51 comptime {
37 const arr_info = @typeInfo([42]bool);52 const arr_info = @typeInfo([42]bool);
38 assert(TypeId(arr_info) == TypeId.Array);53 assert(TypeId(arr_info) == TypeId.Array);
39 assert(arr_info.Array.len == 42);54 assert(arr_info.Array.len == 42);
40 assert(arr_info.Array.child == bool);55 assert(arr_info.Array.child == bool);
56 }
57}
4158
59test "type info: nullable type info" {
60 comptime {
42 const null_info = @typeInfo(?void);61 const null_info = @typeInfo(?void);
43 assert(TypeId(null_info) == TypeId.Nullable);62 assert(TypeId(null_info) == TypeId.Nullable);
44 assert(null_info.Nullable.child == void);63 assert(null_info.Nullable.child == void);
...@@ -100,11 +119,11 @@ test "type info: union info" {...@@ -100,11 +119,11 @@ test "type info: union info" {
100 assert(TypeId(typeinfo_info) == TypeId.Union);119 assert(TypeId(typeinfo_info) == TypeId.Union);
101 assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);120 assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
102 assert(typeinfo_info.Union.tag_type == TypeId);121 assert(typeinfo_info.Union.tag_type == TypeId);
103 assert(typeinfo_info.Union.fields.len == 25);122 assert(typeinfo_info.Union.fields.len == 26);
104 assert(typeinfo_info.Union.fields[4].enum_field != null);123 assert(typeinfo_info.Union.fields[4].enum_field != null);
105 assert((??typeinfo_info.Union.fields[4].enum_field).value == 4);124 assert((??typeinfo_info.Union.fields[4].enum_field).value == 4);
106 assert(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));125 assert(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int));
107 assert(typeinfo_info.Union.defs.len == 20);126 assert(typeinfo_info.Union.defs.len == 21);
108127
109 const TestNoTagUnion = union {128 const TestNoTagUnion = union {
110 Foo: void,129 Foo: void,