aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-06-06 00:39:39 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-06-06 00:39:39 -0400
commitd3693dca73dfc726aed32908691437abe614e5cf (patch)
tree167e2d8c24c6a653f05723716b796949a0eb5585 /src
parent76c8efd56c84c189a52d3dc559fff109d5d34ce4 (diff)
downloadzig-d3693dca73dfc726aed32908691437abe614e5cf.tar.gz
zig-d3693dca73dfc726aed32908691437abe614e5cf.zip
Pointer Reform: update @typeInfo
* add assertion for trying to do @typeInfo on global error set * remove TypeInfo.Slice * add TypeInfo.Pointer.Size with possible values - One - Many - Slice See #770
Diffstat (limited to 'src')
-rw-r--r--src/analyze.cpp2
-rw-r--r--src/codegen.cpp11
-rw-r--r--src/ir.cpp80
3 files changed, 58 insertions, 35 deletions
diff --git a/src/analyze.cpp b/src/analyze.cpp
index 15f08aa3fe..93373f6ec2 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -5981,7 +5981,7 @@ size_t type_id_index(TypeTableEntry *entry) {
return 7;
case TypeTableEntryIdStruct:
if (entry->data.structure.is_slice)
- return 25;
+ return 6;
return 8;
case TypeTableEntryIdComptimeFloat:
return 9;
diff --git a/src/codegen.cpp b/src/codegen.cpp
index a977c34daf..7f95f335d1 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -6481,7 +6481,6 @@ static void define_builtin_compile_vars(CodeGen *g) {
const TypeTableEntryId id = type_id_at_index(i);
buf_appendf(contents, " %s,\n", type_id_name(id));
}
- buf_appendf(contents, " Slice,\n");
buf_appendf(contents, "};\n\n");
}
{
@@ -6494,7 +6493,6 @@ static void define_builtin_compile_vars(CodeGen *g) {
" Int: Int,\n"
" Float: Float,\n"
" Pointer: Pointer,\n"
- " Slice: Slice,\n"
" Array: Array,\n"
" Struct: Struct,\n"
" ComptimeFloat: void,\n"
@@ -6524,13 +6522,18 @@ static void define_builtin_compile_vars(CodeGen *g) {
" };\n"
"\n"
" pub const Pointer = struct {\n"
+ " size: Size,\n"
" is_const: bool,\n"
" is_volatile: bool,\n"
" alignment: u32,\n"
" child: type,\n"
- " };\n"
"\n"
- " pub const Slice = Pointer;\n"
+ " pub const Size = enum {\n"
+ " One,\n"
+ " Many,\n"
+ " Slice,\n"
+ " };\n"
+ " };\n"
"\n"
" pub const Array = struct {\n"
" len: usize,\n"
diff --git a/src/ir.cpp b/src/ir.cpp
index a6686aae76..3486e8c047 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -16222,8 +16222,7 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop
return true;
}
-static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry)
-{
+static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry) {
assert(type_entry != nullptr);
assert(!type_is_invalid(type_entry));
@@ -16248,38 +16247,67 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
enum_field_val->data.x_struct.fields = inner_fields;
};
- const auto create_ptr_like_type_info = [ira](const char *name, TypeTableEntry *ptr_type_entry) {
+ const auto create_ptr_like_type_info = [ira](TypeTableEntry *ptr_type_entry) {
+ TypeTableEntry *attrs_type;
+ uint32_t size_enum_index;
+ if (is_slice(ptr_type_entry)) {
+ attrs_type = ptr_type_entry->data.structure.fields[slice_ptr_index].type_entry;
+ size_enum_index = 2;
+ } else if (ptr_type_entry->id == TypeTableEntryIdPointer) {
+ attrs_type = ptr_type_entry;
+ size_enum_index = (ptr_type_entry->data.pointer.ptr_len == PtrLenSingle) ? 0 : 1;
+ } else {
+ zig_unreachable();
+ }
+
+ TypeTableEntry *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer");
+ ensure_complete_type(ira->codegen, type_info_pointer_type);
+ assert(!type_is_invalid(type_info_pointer_type));
+
ConstExprValue *result = create_const_vals(1);
result->special = ConstValSpecialStatic;
- result->type = ir_type_info_get_type(ira, name);
+ result->type = type_info_pointer_type;
- ConstExprValue *fields = create_const_vals(4);
+ ConstExprValue *fields = create_const_vals(5);
result->data.x_struct.fields = fields;
- // is_const: bool
- ensure_field_index(result->type, "is_const", 0);
+ // size: Size
+ ensure_field_index(result->type, "size", 0);
+ TypeTableEntry *type_info_pointer_size_type = ir_type_info_get_type(ira, "Size", type_info_pointer_type);
+ ensure_complete_type(ira->codegen, type_info_pointer_size_type);
+ assert(!type_is_invalid(type_info_pointer_size_type));
fields[0].special = ConstValSpecialStatic;
- fields[0].type = ira->codegen->builtin_types.entry_bool;
- fields[0].data.x_bool = ptr_type_entry->data.pointer.is_const;
- // is_volatile: bool
- ensure_field_index(result->type, "is_volatile", 1);
+ fields[0].type = type_info_pointer_size_type;
+ bigint_init_unsigned(&fields[0].data.x_enum_tag, size_enum_index);
+
+ // is_const: bool
+ ensure_field_index(result->type, "is_const", 1);
fields[1].special = ConstValSpecialStatic;
fields[1].type = ira->codegen->builtin_types.entry_bool;
- fields[1].data.x_bool = ptr_type_entry->data.pointer.is_volatile;
- // alignment: u32
- ensure_field_index(result->type, "alignment", 2);
+ fields[1].data.x_bool = attrs_type->data.pointer.is_const;
+ // is_volatile: bool
+ ensure_field_index(result->type, "is_volatile", 2);
fields[2].special = ConstValSpecialStatic;
- fields[2].type = ira->codegen->builtin_types.entry_u32;
- bigint_init_unsigned(&fields[2].data.x_bigint, ptr_type_entry->data.pointer.alignment);
- // child: type
- ensure_field_index(result->type, "child", 3);
+ fields[2].type = ira->codegen->builtin_types.entry_bool;
+ fields[2].data.x_bool = attrs_type->data.pointer.is_volatile;
+ // alignment: u32
+ ensure_field_index(result->type, "alignment", 3);
fields[3].special = ConstValSpecialStatic;
- fields[3].type = ira->codegen->builtin_types.entry_type;
- fields[3].data.x_type = ptr_type_entry->data.pointer.child_type;
+ fields[3].type = ira->codegen->builtin_types.entry_u32;
+ bigint_init_unsigned(&fields[3].data.x_bigint, attrs_type->data.pointer.alignment);
+ // child: type
+ ensure_field_index(result->type, "child", 4);
+ fields[4].special = ConstValSpecialStatic;
+ fields[4].type = ira->codegen->builtin_types.entry_type;
+ fields[4].data.x_type = attrs_type->data.pointer.child_type;
return result;
};
+ if (type_entry == ira->codegen->builtin_types.entry_global_error_set) {
+ zig_panic("TODO implement @typeInfo for global error set");
+ }
+
ConstExprValue *result = nullptr;
switch (type_entry->id)
{
@@ -16348,7 +16376,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
}
case TypeTableEntryIdPointer:
{
- result = create_ptr_like_type_info("Pointer", type_entry);
+ result = create_ptr_like_type_info(type_entry);
break;
}
case TypeTableEntryIdArray:
@@ -16621,15 +16649,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
case TypeTableEntryIdStruct:
{
if (type_entry->data.structure.is_slice) {
- Buf ptr_field_name = BUF_INIT;
- buf_init_from_str(&ptr_field_name, "ptr");
- TypeTableEntry *ptr_type = type_entry->data.structure.fields_by_name.get(&ptr_field_name)->type_entry;
- ensure_complete_type(ira->codegen, ptr_type);
- if (type_is_invalid(ptr_type))
- return nullptr;
- buf_deinit(&ptr_field_name);
-
- result = create_ptr_like_type_info("Slice", ptr_type);
+ result = create_ptr_like_type_info(type_entry);
break;
}