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() {
59315931 return array_length(all_type_ids);
59325932}
59335933
5934size_t type_id_index(TypeTableEntryId id) {
5935 switch (id) {
5934size_t type_id_index(TypeTableEntry *entry) {
5935 switch (entry->id) {
59365936 case TypeTableEntryIdInvalid:
59375937 zig_unreachable();
59385938 case TypeTableEntryIdMetaType:
......@@ -5952,6 +5952,8 @@ size_t type_id_index(TypeTableEntryId id) {
59525952 case TypeTableEntryIdArray:
59535953 return 7;
59545954 case TypeTableEntryIdStruct:
5955 if (entry->data.structure.is_slice)
5956 return 25;
59555957 return 8;
59565958 case TypeTableEntryIdNumLitFloat:
59575959 return 9;
src/analyze.hpp+1-1
......@@ -174,7 +174,7 @@ void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
174174const char *type_id_name(TypeTableEntryId id);
175175TypeTableEntryId type_id_at_index(size_t index);
176176size_t type_id_len();
177size_t type_id_index(TypeTableEntryId id);
177size_t type_id_index(TypeTableEntry *entry);
178178TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);
179179bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);
180180LinkLib *create_link_lib(Buf *name);
src/codegen.cpp+4
......@@ -6345,6 +6345,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
63456345 const TypeTableEntryId id = type_id_at_index(i);
63466346 buf_appendf(contents, " %s,\n", type_id_name(id));
63476347 }
6348 buf_appendf(contents, " Slice,\n");
63486349 buf_appendf(contents, "};\n\n");
63496350 }
63506351 {
......@@ -6357,6 +6358,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
63576358 " Int: Int,\n"
63586359 " Float: Float,\n"
63596360 " Pointer: Pointer,\n"
6361 " Slice: Slice,\n"
63606362 " Array: Array,\n"
63616363 " Struct: Struct,\n"
63626364 " FloatLiteral: void,\n"
......@@ -6392,6 +6394,8 @@ static void define_builtin_compile_vars(CodeGen *g) {
63926394 " child: type,\n"
63936395 " };\n"
63946396 "\n"
6397 " pub const Slice = Pointer;\n"
6398 "\n"
63956399 " pub const Array = struct {\n"
63966400 " len: usize,\n"
63976401 " 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
1578515785
1578615786 Buf field_name = BUF_INIT;
1578715787 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);
1578915789 buf_deinit(&field_name);
15790 assert(entry != nullptr);
1579115790
15792 TldVar *tld = (TldVar *)entry->value;
15791 TldVar *tld = (TldVar *)entry;
1579315792 assert(tld->base.id == TldIdVar);
1579415793
1579515794 VariableTableEntry *var = tld->var;
......@@ -16071,6 +16070,38 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1607116070 enum_field_val->data.x_struct.fields = inner_fields;
1607216071 };
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
1607416105 ConstExprValue *result = nullptr;
1607516106 switch (type_entry->id)
1607616107 {
......@@ -16139,34 +16170,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1613916170 }
1614016171 case TypeTableEntryIdPointer:
1614116172 {
16142 result = create_const_vals(1);
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
16173 result = create_ptr_like_type_info("Pointer", type_entry);
1617016174 break;
1617116175 }
1617216176 case TypeTableEntryIdArray:
......@@ -16436,6 +16440,17 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1643616440 }
1643716441 case TypeTableEntryIdStruct:
1643816442 {
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
1643916454 result = create_const_vals(1);
1644016455 result->special = ConstValSpecialStatic;
1644116456 result->type = ir_type_info_get_type(ira, "Struct");
......@@ -16622,7 +16637,7 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
1662216637
1662316638 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1662416639 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
1662716642 ConstExprValue *payload = ir_make_type_info_value(ira, type_entry);
1662816643 out_val->data.x_union.payload = payload;
......@@ -16650,7 +16665,7 @@ static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
1665016665 TypeTableEntry *result_type = var_value->data.x_type;
1665116666
1665216667 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));
1665416669 return result_type;
1665516670}
1665616671
test/cases/type_info.zig+22-3
......@@ -25,7 +25,7 @@ test "type info: integer, floating point type info" {
2525 }
2626}
2727
28test "type info: pointer, array and nullable type info" {
28test "type info: pointer type info" {
2929 comptime {
3030 const u32_ptr_info = @typeInfo(&u32);
3131 assert(TypeId(u32_ptr_info) == TypeId.Pointer);
......@@ -33,12 +33,31 @@ test "type info: pointer, array and nullable type info" {
3333 assert(u32_ptr_info.Pointer.is_volatile == false);
3434 assert(u32_ptr_info.Pointer.alignment == 4);
3535 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 {
3752 const arr_info = @typeInfo([42]bool);
3853 assert(TypeId(arr_info) == TypeId.Array);
3954 assert(arr_info.Array.len == 42);
4055 assert(arr_info.Array.child == bool);
56 }
57}
4158
59test "type info: nullable type info" {
60 comptime {
4261 const null_info = @typeInfo(?void);
4362 assert(TypeId(null_info) == TypeId.Nullable);
4463 assert(null_info.Nullable.child == void);
......@@ -100,11 +119,11 @@ test "type info: union info" {
100119 assert(TypeId(typeinfo_info) == TypeId.Union);
101120 assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
102121 assert(typeinfo_info.Union.tag_type == TypeId);
103 assert(typeinfo_info.Union.fields.len == 25);
122 assert(typeinfo_info.Union.fields.len == 26);
104123 assert(typeinfo_info.Union.fields[4].enum_field != null);
105124 assert((??typeinfo_info.Union.fields[4].enum_field).value == 4);
106125 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
109128 const TestNoTagUnion = union {
110129 Foo: void,