aboutsummaryrefslogtreecommitdiff
path: root/src/types.cpp
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2022-02-15 15:48:56 +0000
committerGitHub <noreply@github.com>2022-02-15 15:48:56 +0000
commit9e98494fff550c84a6d9c73032f3c9556f9b4c43 (patch)
tree3448b7b6dd725433a57e16348eb2d7e688e565ad /src/types.cpp
parent86d334282cf039860ddd9e894fbb552c1111ca4f (diff)
parent803648be894659409beb3c2b5e76b7b491ddf7c5 (diff)
Merge pull request #1482 from odin-lang/objc-intrinsics
Improve support Objective-C code through intrinsics and Metal API
Diffstat (limited to 'src/types.cpp')
-rw-r--r--src/types.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/types.cpp b/src/types.cpp
index 9ee6ba359..78958146b 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -393,6 +393,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};
@@ -685,6 +686,16 @@ 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;
+
+
+
gb_global RecursiveMutex g_type_mutex;
struct TypePath;
@@ -2772,6 +2783,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);
@@ -2784,9 +2796,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) {
@@ -2833,6 +2876,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) {
@@ -3738,6 +3799,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&EntityFlag_Using) == 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));