aboutsummaryrefslogtreecommitdiff
path: root/src/types.cpp
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-04-27 14:37:15 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2022-04-27 14:37:15 +0200
commitc4e0d1efa1ec655bae9134b95a0fcd060cc7bbea (patch)
treec29bd0b78138e8d67aebe34ac689d13e32d9d15f /src/types.cpp
parent6e61abc7d06f22129f93110a9f652c3eec21f0c6 (diff)
parent9349dfba8fec53f52f77a0c8928e115ec93ff447 (diff)
Merge branch 'master' into xml
Diffstat (limited to 'src/types.cpp')
-rw-r--r--src/types.cpp289
1 files changed, 250 insertions, 39 deletions
diff --git a/src/types.cpp b/src/types.cpp
index 2b7ea93dc..b4dc17256 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -165,9 +165,8 @@ struct TypeUnion {
i16 tag_size;
bool is_polymorphic;
- bool is_poly_specialized : 1;
- bool no_nil : 1;
- bool maybe : 1;
+ bool is_poly_specialized;
+ UnionTypeKind kind;
};
struct TypeProc {
@@ -186,7 +185,6 @@ struct TypeProc {
bool c_vararg;
bool is_polymorphic;
bool is_poly_specialized;
- bool has_proc_default_values;
bool has_named_results;
bool diverging; // no return
bool return_by_pointer;
@@ -221,6 +219,7 @@ struct TypeProc {
ExactValue *max_value; \
i64 count; \
TokenKind op; \
+ bool is_sparse; \
}) \
TYPE_KIND(Slice, struct { Type *elem; }) \
TYPE_KIND(DynamicArray, struct { Type *elem; }) \
@@ -362,6 +361,7 @@ enum TypeInfoFlag : u32 {
enum : int {
MATRIX_ELEMENT_COUNT_MIN = 1,
MATRIX_ELEMENT_COUNT_MAX = 16,
+ MATRIX_ELEMENT_MAX_SIZE = MATRIX_ELEMENT_COUNT_MAX * (2 * 8), // complex128
};
@@ -391,6 +391,7 @@ struct Selection {
bool indirect; // Set if there was a pointer deref anywhere down the line
u8 swizzle_count; // maximum components = 4
u8 swizzle_indices; // 2 bits per component, representing which swizzle index
+ bool pseudo_field;
};
Selection empty_selection = {0};
@@ -683,6 +684,38 @@ gb_global Type *t_map_header = nullptr;
gb_global Type *t_equal_proc = nullptr;
gb_global Type *t_hasher_proc = nullptr;
+gb_global Type *t_objc_object = nullptr;
+gb_global Type *t_objc_selector = nullptr;
+gb_global Type *t_objc_class = nullptr;
+
+gb_global Type *t_objc_id = nullptr;
+gb_global Type *t_objc_SEL = nullptr;
+gb_global Type *t_objc_Class = nullptr;
+
+enum OdinAtomicMemoryOrder : i32 {
+ OdinAtomicMemoryOrder_relaxed = 0, // unordered
+ OdinAtomicMemoryOrder_consume = 1, // monotonic
+ OdinAtomicMemoryOrder_acquire = 2,
+ OdinAtomicMemoryOrder_release = 3,
+ OdinAtomicMemoryOrder_acq_rel = 4,
+ OdinAtomicMemoryOrder_seq_cst = 5,
+ OdinAtomicMemoryOrder_COUNT,
+};
+
+char const *OdinAtomicMemoryOrder_strings[OdinAtomicMemoryOrder_COUNT] = {
+ "Relaxed",
+ "Consume",
+ "Acquire",
+ "Release",
+ "Acq_Rel",
+ "Seq_Cst",
+};
+
+gb_global Type *t_atomic_memory_order = nullptr;
+
+
+
+
gb_global RecursiveMutex g_type_mutex;
struct TypePath;
@@ -690,7 +723,7 @@ struct TypePath;
i64 type_size_of (Type *t);
i64 type_align_of (Type *t);
i64 type_offset_of (Type *t, i32 index);
-gbString type_to_string (Type *type);
+gbString type_to_string (Type *type, bool shorthand=false);
i64 type_size_of_internal(Type *t, TypePath *path);
void init_map_internal_types(Type *type);
Type * bit_set_to_int(Type *t);
@@ -1253,6 +1286,13 @@ bool is_type_quaternion(Type *t) {
}
return false;
}
+bool is_type_complex_or_quaternion(Type *t) {
+ t = core_type(t);
+ if (t->kind == Type_Basic) {
+ return (t->Basic.flags & (BasicFlag_Complex|BasicFlag_Quaternion)) != 0;
+ }
+ return false;
+}
bool is_type_f16(Type *t) {
t = core_type(t);
if (t->kind == Type_Basic) {
@@ -1575,6 +1615,24 @@ Type *core_array_type(Type *t) {
}
}
+i32 type_math_rank(Type *t) {
+ i32 rank = 0;
+ for (;;) {
+ t = base_type(t);
+ switch (t->kind) {
+ case Type_Array:
+ rank += 1;
+ t = t->Array.elem;
+ break;
+ case Type_Matrix:
+ rank += 2;
+ t = t->Matrix.elem;
+ break;
+ default:
+ return rank;
+ }
+ }
+}
Type *base_complex_elem_type(Type *t) {
@@ -1627,7 +1685,7 @@ bool is_type_map(Type *t) {
bool is_type_union_maybe_pointer(Type *t) {
t = base_type(t);
- if (t->kind == Type_Union && t->Union.maybe) {
+ if (t->kind == Type_Union && t->Union.kind == UnionType_maybe) {
if (t->Union.variants.count == 1) {
Type *v = t->Union.variants[0];
return is_type_pointer(v) || is_type_multi_pointer(v);
@@ -1639,7 +1697,7 @@ bool is_type_union_maybe_pointer(Type *t) {
bool is_type_union_maybe_pointer_original_alignment(Type *t) {
t = base_type(t);
- if (t->kind == Type_Union && t->Union.maybe) {
+ if (t->kind == Type_Union && t->Union.kind == UnionType_maybe) {
if (t->Union.variants.count == 1) {
Type *v = t->Union.variants[0];
if (is_type_pointer(v) || is_type_multi_pointer(v)) {
@@ -2131,7 +2189,7 @@ bool type_has_nil(Type *t) {
case Type_Map:
return true;
case Type_Union:
- return !t->Union.no_nil;
+ return t->Union.kind != UnionType_no_nil;
case Type_Struct:
if (is_type_soa_struct(t)) {
switch (t->Struct.soa_kind) {
@@ -2160,6 +2218,17 @@ bool elem_type_can_be_constant(Type *t) {
return true;
}
+bool is_type_lock_free(Type *t) {
+ t = core_type(t);
+ if (t == t_invalid) {
+ return false;
+ }
+ i64 sz = type_size_of(t);
+ // TODO(bill): Figure this out correctly
+ return sz <= build_context.max_align;
+}
+
+
bool is_type_comparable(Type *t) {
t = base_type(t);
@@ -2296,7 +2365,7 @@ String lookup_subtype_polymorphic_field(Type *dst, Type *src) {
GB_ASSERT(is_type_struct(src) || is_type_union(src));
for_array(i, src->Struct.fields) {
Entity *f = src->Struct.fields[i];
- if (f->kind == Entity_Variable && f->flags & EntityFlag_Using) {
+ if (f->kind == Entity_Variable && f->flags & EntityFlags_IsSubtype) {
if (are_types_identical(dst, f->type)) {
return f->token.string;
}
@@ -2305,7 +2374,7 @@ String lookup_subtype_polymorphic_field(Type *dst, Type *src) {
return f->token.string;
}
}
- if (is_type_struct(f->type)) {
+ if ((f->flags & EntityFlag_Using) != 0 && is_type_struct(f->type)) {
String name = lookup_subtype_polymorphic_field(dst, f->type);
if (name.len > 0) {
return name;
@@ -2331,7 +2400,17 @@ Type *strip_type_aliasing(Type *x) {
return x;
}
+bool are_types_identical_internal(Type *x, Type *y, bool check_tuple_names);
+
bool are_types_identical(Type *x, Type *y) {
+ return are_types_identical_internal(x, y, false);
+}
+bool are_types_identical_unique_tuples(Type *x, Type *y) {
+ return are_types_identical_internal(x, y, true);
+}
+
+
+bool are_types_identical_internal(Type *x, Type *y, bool check_tuple_names) {
if (x == y) {
return true;
}
@@ -2407,7 +2486,7 @@ bool are_types_identical(Type *x, Type *y) {
if (y->kind == Type_Union) {
if (x->Union.variants.count == y->Union.variants.count &&
x->Union.custom_align == y->Union.custom_align &&
- x->Union.no_nil == y->Union.no_nil) {
+ x->Union.kind == y->Union.kind) {
// NOTE(bill): zeroth variant is nullptr
for_array(i, x->Union.variants) {
if (!are_types_identical(x->Union.variants[i], y->Union.variants[i])) {
@@ -2441,9 +2520,9 @@ bool are_types_identical(Type *x, Type *y) {
if (xf->token.string != yf->token.string) {
return false;
}
- bool xf_is_using = (xf->flags&EntityFlag_Using) != 0;
- bool yf_is_using = (yf->flags&EntityFlag_Using) != 0;
- if (xf_is_using ^ yf_is_using) {
+ u64 xf_flags = (xf->flags&EntityFlags_IsSubtype);
+ u64 yf_flags = (yf->flags&EntityFlags_IsSubtype);
+ if (xf_flags != yf_flags) {
return false;
}
}
@@ -2480,6 +2559,11 @@ bool are_types_identical(Type *x, Type *y) {
if (xe->kind != ye->kind || !are_types_identical(xe->type, ye->type)) {
return false;
}
+ if (check_tuple_names) {
+ if (xe->token.string != ye->token.string) {
+ return false;
+ }
+ }
if (xe->kind == Entity_Constant && !compare_exact_values(Token_CmpEq, xe->Constant.value, ye->Constant.value)) {
// NOTE(bill): This is needed for polymorphic procedures
return false;
@@ -2546,7 +2630,7 @@ i64 union_variant_index(Type *u, Type *v) {
for_array(i, u->Union.variants) {
Type *vt = u->Union.variants[i];
if (are_types_identical(v, vt)) {
- if (u->Union.no_nil) {
+ if (u->Union.kind == UnionType_no_nil) {
return cast(i64)(i+0);
} else {
return cast(i64)(i+1);
@@ -2570,6 +2654,17 @@ i64 union_tag_size(Type *u) {
// TODO(bill): Is this an okay approach?
i64 max_align = 1;
+
+ if (u->Union.variants.count < 1ull<<8) {
+ max_align = 1;
+ } else if (u->Union.variants.count < 1ull<<16) {
+ max_align = 2;
+ } else if (u->Union.variants.count < 1ull<<32) {
+ max_align = 4;
+ } else {
+ GB_PANIC("how many variants do you have?!");
+ }
+
for_array(i, u->Union.variants) {
Type *variant_type = u->Union.variants[i];
i64 align = type_align_of(variant_type);
@@ -2730,6 +2825,7 @@ Selection lookup_field_from_index(Type *type, i64 index) {
}
Entity *scope_lookup_current(Scope *s, String const &name);
+bool has_type_got_objc_class_attribute(Type *t);
Selection lookup_field_with_selection(Type *type_, String field_name, bool is_type, Selection sel, bool allow_blank_ident) {
GB_ASSERT(type_ != nullptr);
@@ -2742,9 +2838,40 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
bool is_ptr = type != type_;
sel.indirect = sel.indirect || is_ptr;
+ Type *original_type = type;
+
type = base_type(type);
if (is_type) {
+ if (has_type_got_objc_class_attribute(original_type) && original_type->kind == Type_Named) {
+ Entity *e = original_type->Named.type_name;
+ GB_ASSERT(e->kind == Entity_TypeName);
+ if (e->TypeName.objc_metadata) {
+ auto *md = e->TypeName.objc_metadata;
+ mutex_lock(md->mutex);
+ defer (mutex_unlock(md->mutex));
+ for (TypeNameObjCMetadataEntry const &entry : md->type_entries) {
+ GB_ASSERT(entry.entity->kind == Entity_Procedure);
+ if (entry.name == field_name) {
+ sel.entity = entry.entity;
+ sel.pseudo_field = true;
+ return sel;
+ }
+ }
+ }
+ if (type->kind == Type_Struct) {
+ for_array(i, type->Struct.fields) {
+ Entity *f = type->Struct.fields[i];
+ if (f->flags&EntityFlag_Using) {
+ sel = lookup_field_with_selection(f->type, field_name, is_type, sel, allow_blank_ident);
+ if (sel.entity) {
+ return sel;
+ }
+ }
+ }
+ }
+ }
+
if (is_type_enum(type)) {
// NOTE(bill): These may not have been added yet, so check in case
for_array(i, type->Enum.fields) {
@@ -2791,6 +2918,24 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
} else if (type->kind == Type_Union) {
} else if (type->kind == Type_Struct) {
+ if (has_type_got_objc_class_attribute(original_type) && original_type->kind == Type_Named) {
+ Entity *e = original_type->Named.type_name;
+ GB_ASSERT(e->kind == Entity_TypeName);
+ if (e->TypeName.objc_metadata) {
+ auto *md = e->TypeName.objc_metadata;
+ mutex_lock(md->mutex);
+ defer (mutex_unlock(md->mutex));
+ for (TypeNameObjCMetadataEntry const &entry : md->value_entries) {
+ GB_ASSERT(entry.entity->kind == Entity_Procedure);
+ if (entry.name == field_name) {
+ sel.entity = entry.entity;
+ sel.pseudo_field = true;
+ return sel;
+ }
+ }
+ }
+ }
+
for_array(i, type->Struct.fields) {
Entity *f = type->Struct.fields[i];
if (f->kind != Entity_Variable || (f->flags & EntityFlag_Field) == 0) {
@@ -3696,6 +3841,61 @@ i64 type_offset_of_from_selection(Type *type, Selection sel) {
return offset;
}
+isize check_is_assignable_to_using_subtype(Type *src, Type *dst, isize level = 0, bool src_is_ptr = false) {
+ Type *prev_src = src;
+ src = type_deref(src);
+ if (!src_is_ptr) {
+ src_is_ptr = src != prev_src;
+ }
+ src = base_type(src);
+
+ if (!is_type_struct(src)) {
+ return 0;
+ }
+
+ for_array(i, src->Struct.fields) {
+ Entity *f = src->Struct.fields[i];
+ if (f->kind != Entity_Variable || (f->flags&EntityFlags_IsSubtype) == 0) {
+ continue;
+ }
+
+ if (are_types_identical(f->type, dst)) {
+ return level+1;
+ }
+ if (src_is_ptr && is_type_pointer(dst)) {
+ if (are_types_identical(f->type, type_deref(dst))) {
+ return level+1;
+ }
+ }
+ isize nested_level = check_is_assignable_to_using_subtype(f->type, dst, level+1, src_is_ptr);
+ if (nested_level > 0) {
+ return nested_level;
+ }
+ }
+
+ return 0;
+}
+
+bool is_type_subtype_of(Type *src, Type *dst) {
+ if (are_types_identical(src, dst)) {
+ return true;
+ }
+
+ return 0 < check_is_assignable_to_using_subtype(src, dst, 0, is_type_pointer(src));
+}
+
+
+bool has_type_got_objc_class_attribute(Type *t) {
+ return t->kind == Type_Named && t->Named.type_name != nullptr && t->Named.type_name->TypeName.objc_class_name != "";
+}
+
+
+
+bool is_type_objc_object(Type *t) {
+ bool internal_check_is_assignable_to(Type *src, Type *dst);
+
+ return internal_check_is_assignable_to(t, t_objc_object);
+}
Type *get_struct_field_type(Type *t, isize index) {
t = base_type(type_deref(t));
@@ -3767,7 +3967,7 @@ Type *alloc_type_proc_from_types(Type **param_types, unsigned param_count, Type
-gbString write_type_to_string(gbString str, Type *type) {
+gbString write_type_to_string(gbString str, Type *type, bool shorthand=false) {
if (type == nullptr) {
return gb_string_appendc(str, "<no type>");
}
@@ -3808,6 +4008,9 @@ gbString write_type_to_string(gbString str, Type *type) {
break;
case Type_EnumeratedArray:
+ if (type->EnumeratedArray.is_sparse) {
+ str = gb_string_appendc(str, "#sparse");
+ }
str = gb_string_append_rune(str, '[');
str = write_type_to_string(str, type->EnumeratedArray.index);
str = gb_string_append_rune(str, ']');
@@ -3850,8 +4053,11 @@ gbString write_type_to_string(gbString str, Type *type) {
case Type_Union:
str = gb_string_appendc(str, "union");
- if (type->Union.no_nil != 0) str = gb_string_appendc(str, " #no_nil");
- if (type->Union.maybe != 0) str = gb_string_appendc(str, " #maybe");
+ switch (type->Union.kind) {
+ case UnionType_maybe: str = gb_string_appendc(str, " #maybe"); break;
+ case UnionType_no_nil: str = gb_string_appendc(str, " #no_nil"); break;
+ case UnionType_shared_nil: str = gb_string_appendc(str, " #shared_nil"); break;
+ }
if (type->Union.custom_align != 0) str = gb_string_append_fmt(str, " #align %d", cast(int)type->Union.custom_align);
str = gb_string_appendc(str, " {");
for_array(i, type->Union.variants) {
@@ -3879,15 +4085,21 @@ gbString write_type_to_string(gbString str, Type *type) {
if (type->Struct.is_raw_union) str = gb_string_appendc(str, " #raw_union");
if (type->Struct.custom_align != 0) str = gb_string_append_fmt(str, " #align %d", cast(int)type->Struct.custom_align);
str = gb_string_appendc(str, " {");
- for_array(i, type->Struct.fields) {
- Entity *f = type->Struct.fields[i];
- GB_ASSERT(f->kind == Entity_Variable);
- if (i > 0) {
- str = gb_string_appendc(str, ", ");
+
+
+ if (shorthand && type->Struct.fields.count > 16) {
+ str = gb_string_append_fmt(str, "%lld fields...", cast(long long)type->Struct.fields.count);
+ } else {
+ for_array(i, type->Struct.fields) {
+ Entity *f = type->Struct.fields[i];
+ GB_ASSERT(f->kind == Entity_Variable);
+ if (i > 0) {
+ str = gb_string_appendc(str, ", ");
+ }
+ str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
+ str = gb_string_appendc(str, ": ");
+ str = write_type_to_string(str, f->type);
}
- str = gb_string_append_length(str, f->token.string.text, f->token.string.len);
- str = gb_string_appendc(str, ": ");
- str = write_type_to_string(str, f->type);
}
str = gb_string_append_rune(str, '}');
} break;
@@ -3926,7 +4138,7 @@ gbString write_type_to_string(gbString str, Type *type) {
str = gb_string_appendc(str, " = ");
str = write_exact_value_to_string(str, var->Constant.value);
} else {
- str = gb_string_appendc(str, "=");
+ str = gb_string_appendc(str, " := ");
str = write_exact_value_to_string(str, var->Constant.value);
}
continue;
@@ -3954,14 +4166,10 @@ gbString write_type_to_string(gbString str, Type *type) {
str = gb_string_appendc(str, "typeid/");
str = write_type_to_string(str, var->type);
} else {
- if (var->kind == Entity_TypeName) {
- str = gb_string_appendc(str, "$");
- str = gb_string_append_length(str, name.text, name.len);
- str = gb_string_appendc(str, "=");
- str = write_type_to_string(str, var->type);
- } else {
- str = gb_string_appendc(str, "typeid");
- }
+ str = gb_string_appendc(str, "$");
+ str = gb_string_append_length(str, name.text, name.len);
+ str = gb_string_appendc(str, "=");
+ str = write_type_to_string(str, var->type);
}
}
}
@@ -4066,13 +4274,16 @@ gbString write_type_to_string(gbString str, Type *type) {
}
-gbString type_to_string(Type *type, gbAllocator allocator) {
- return write_type_to_string(gb_string_make(allocator, ""), type);
+gbString type_to_string(Type *type, gbAllocator allocator, bool shorthand=false) {
+ return write_type_to_string(gb_string_make(allocator, ""), type, shorthand);
}
-gbString type_to_string(Type *type) {
- return write_type_to_string(gb_string_make(heap_allocator(), ""), type);
+gbString type_to_string(Type *type, bool shorthand) {
+ return write_type_to_string(gb_string_make(heap_allocator(), ""), type, shorthand);
}
+gbString type_to_string_shorthand(Type *type) {
+ return type_to_string(type, true);
+}