#include "runtime.h"
#define OBJC_VERSION 8
#define PROTOCOL_VERSION 2
static struct objc_list *__objc_module_list = 0;
static struct objc_list *unclaimed_proto_list = 0;
static struct objc_list *uninitialized_statics = 0;
objc_mutex_t __objc_runtime_mutex = 0;
int __objc_runtime_threads_alive = 1;
static void init_check_module_version (Module_t);
static void __objc_init_protocols (struct objc_protocol_list *protos);
static void __objc_class_add_protocols (Class, struct objc_protocol_list *);
void (*_objc_load_callback) (Class class, Category *category);
BOOL __objc_dangling_categories = NO;
extern SEL
__sel_register_typed_name (const char *name, const char *types,
struct objc_selector *orig, BOOL is_const);
static void objc_send_load (void);
static void __objc_create_classes_tree (Module_t module);
static void __objc_call_callback (Module_t module);
static BOOL class_is_subclass_of_class (Class class, Class superclass);
typedef struct objc_class_tree {
Class class;
struct objc_list *subclasses;
} objc_class_tree;
static struct objc_list *__objc_class_tree_list = NULL;
static cache_ptr __objc_load_methods = NULL;
static Class class_superclass_of_class (Class class)
{
char *super_class_name;
if (CLS_ISRESOLV (class))
return class->super_class;
super_class_name = (char *)class->super_class;
if (super_class_name == NULL)
return Nil;
return objc_lookup_class (super_class_name);
}
static objc_class_tree *
create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
{
Class superclass = bottom_class->super_class ?
objc_lookup_class ((char *) bottom_class->super_class)
: Nil;
objc_class_tree *tree, *prev;
DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
DEBUG_PRINTF ("bottom_class = %s, upper = %s\n",
(bottom_class ? bottom_class->name : NULL),
(upper ? upper->name : NULL));
tree = prev = objc_calloc (1, sizeof (objc_class_tree));
prev->class = bottom_class;
while (superclass != upper)
{
tree = objc_calloc (1, sizeof (objc_class_tree));
tree->class = superclass;
tree->subclasses = list_cons (prev, tree->subclasses);
superclass = class_superclass_of_class (superclass);
prev = tree;
}
return tree;
}
static objc_class_tree *
__objc_tree_insert_class (objc_class_tree *tree, Class class)
{
DEBUG_PRINTF ("__objc_tree_insert_class: tree = %x, class = %s\n",
tree, class->name);
if (tree == NULL)
return create_tree_of_subclasses_inherited_from (class, NULL);
else if (class == tree->class)
{
DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name);
return tree;
}
else if (class_superclass_of_class (class) == tree->class)
{
struct objc_list *list = tree->subclasses;
objc_class_tree *node;
while (list)
{
if (((objc_class_tree *) list->head)->class == class)
{
DEBUG_PRINTF ("2. class %s was previously inserted\n",
class->name);
return tree;
}
list = list->tail;
}
node = objc_calloc (1, sizeof (objc_class_tree));
node->class = class;
tree->subclasses = list_cons (node, tree->subclasses);
DEBUG_PRINTF ("3. class %s inserted\n", class->name);
return tree;
}
else
{
struct objc_list *subclasses = tree->subclasses;
if (! class_is_subclass_of_class (class, tree->class))
return NULL;
for (; subclasses != NULL; subclasses = subclasses->tail)
{
Class aClass = ((objc_class_tree *) (subclasses->head))->class;
if (class_is_subclass_of_class (class, aClass))
{
subclasses->head
= __objc_tree_insert_class (subclasses->head, class);
DEBUG_PRINTF ("4. class %s inserted\n", class->name);
return tree;
}
}
{
objc_class_tree *new_tree
= create_tree_of_subclasses_inherited_from (class, tree->class);
tree->subclasses = list_cons (new_tree, tree->subclasses);
DEBUG_PRINTF ("5. class %s inserted\n", class->name);
return tree;
}
}
}
static void
objc_tree_insert_class (Class class)
{
struct objc_list *list_node;
objc_class_tree *tree;
list_node = __objc_class_tree_list;
while (list_node)
{
tree = __objc_tree_insert_class (list_node->head, class);
if (tree)
{
list_node->head = tree;
break;
}
else
list_node = list_node->tail;
}
if (! list_node)
{
__objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
__objc_class_tree_list->head = __objc_tree_insert_class (NULL, class);
}
}
static void
objc_preorder_traverse (objc_class_tree *tree,
int level,
void (*function) (objc_class_tree *, int))
{
struct objc_list *node;
(*function) (tree, level);
for (node = tree->subclasses; node; node = node->tail)
objc_preorder_traverse (node->head, level + 1, function);
}
static void
objc_postorder_traverse (objc_class_tree *tree,
int level,
void (*function) (objc_class_tree *, int))
{
struct objc_list *node;
for (node = tree->subclasses; node; node = node->tail)
objc_postorder_traverse (node->head, level + 1, function);
(*function) (tree, level);
}
#ifdef DEBUG
static void
__objc_tree_print (objc_class_tree *tree, int level)
{
int i;
for (i = 0; i < level; i++)
printf (" ");
printf ("%s\n", tree->class->name);
}
#endif
static void
__objc_send_message_in_list (MethodList_t method_list, Class class, SEL op)
{
int i;
if (! method_list)
return;
__objc_send_message_in_list (method_list->method_next, class, op);
for (i = 0; i < method_list->method_count; i++)
{
Method_t mth = &method_list->method_list[i];
if (mth->method_name && sel_eq (mth->method_name, op)
&& ! objc_hash_is_key_in_hash (__objc_load_methods, mth->method_imp))
{
objc_hash_add (&__objc_load_methods,
mth->method_imp,
mth->method_imp);
DEBUG_PRINTF ("sending +load in class: %s\n", class->name);
(*mth->method_imp) ((id)class, mth->method_name);
break;
}
}
}
static void
__objc_send_load (objc_class_tree *tree,
int level __attribute__ ((__unused__)))
{
static SEL load_sel = 0;
Class class = tree->class;
MethodList_t method_list = class->class_pointer->methods;
if (! load_sel)
load_sel = sel_register_name ("load");
__objc_send_message_in_list (method_list, class, load_sel);
}
static void
__objc_destroy_class_tree_node (objc_class_tree *tree,
int level __attribute__ ((__unused__)))
{
objc_free (tree);
}
static BOOL
class_is_subclass_of_class (Class class, Class superclass)
{
for (; class != Nil;)
{
if (class == superclass)
return YES;
class = class_superclass_of_class (class);
}
return NO;
}
static struct objc_list *unresolved_classes = 0;
extern void __objc_force_linking (void);
void
__objc_force_linking (void)
{
extern void __objc_linking (void);
__objc_linking ();
}
static void
objc_init_statics (void)
{
struct objc_list **cell = &uninitialized_statics;
struct objc_static_instances **statics_in_module;
objc_mutex_lock (__objc_runtime_mutex);
while (*cell)
{
int module_initialized = 1;
for (statics_in_module = (*cell)->head;
*statics_in_module; statics_in_module++)
{
struct objc_static_instances *statics = *statics_in_module;
Class class = objc_lookup_class (statics->class_name);
if (! class)
module_initialized = 0;
else if (class != statics->instances[0]->class_pointer)
{
id *inst;
for (inst = &statics->instances[0]; *inst; inst++)
{
(*inst)->class_pointer = class;
}
}
}
if (module_initialized)
{
struct objc_list *this = *cell;
*cell = this->tail;
objc_free (this);
}
else
cell = &(*cell)->tail;
}
objc_mutex_unlock (__objc_runtime_mutex);
}
void
__objc_exec_class (Module_t module)
{
static BOOL previous_constructors = 0;
static struct objc_list *unclaimed_categories = 0;
Symtab_t symtab = module->symtab;
struct objc_static_instances **statics
= symtab->defs[symtab->cls_def_cnt + symtab->cat_def_cnt];
struct objc_list **cell;
SEL selectors = symtab->refs;
int i;
DEBUG_PRINTF ("received module: %s\n", module->name);
init_check_module_version (module);
if (! previous_constructors)
{
__objc_init_thread_system ();
__objc_runtime_threads_alive = 1;
__objc_runtime_mutex = objc_mutex_allocate ();
__objc_init_selector_tables ();
__objc_init_class_tables ();
__objc_init_dispatch_tables ();
__objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
__objc_load_methods = objc_hash_new (128,
(hash_func_type)objc_hash_ptr,
objc_compare_ptrs);
previous_constructors = 1;
}
objc_mutex_lock (__objc_runtime_mutex);
__objc_module_list = list_cons (module, __objc_module_list);
if (selectors)
{
for (i = 0; selectors[i].sel_id; ++i)
{
const char *name, *type;
name = (char *) selectors[i].sel_id;
type = (char *) selectors[i].sel_types;
__sel_register_typed_name (name, type,
(struct objc_selector *) &(selectors[i]),
YES);
}
}
DEBUG_PRINTF ("gathering selectors from module: %s\n", module->name);
for (i = 0; i < symtab->cls_def_cnt; ++i)
{
Class class = (Class) symtab->defs[i];
const char *superclass = (char *) class->super_class;
assert (CLS_ISCLASS (class));
assert (CLS_ISMETA (class->class_pointer));
DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name);
class->subclass_list = NULL;
__objc_add_class_to_hash (class);
__objc_register_selectors_from_class (class);
__objc_register_selectors_from_class ((Class) class->class_pointer);
__objc_install_premature_dtable (class);
__objc_install_premature_dtable (class->class_pointer);
__objc_register_instance_methods_to_class (class);
if (class->protocols)
__objc_init_protocols (class->protocols);
if (superclass && ! objc_lookup_class (superclass))
unresolved_classes = list_cons (class, unresolved_classes);
}
for (i = 0; i < symtab->cat_def_cnt; ++i)
{
Category_t category = symtab->defs[i + symtab->cls_def_cnt];
Class class = objc_lookup_class (category->class_name);
if (class)
{
DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
module->name,
class->name);
if (category->instance_methods)
class_add_method_list (class, category->instance_methods);
if (category->class_methods)
class_add_method_list ((Class) class->class_pointer,
category->class_methods);
if (category->protocols)
{
__objc_init_protocols (category->protocols);
__objc_class_add_protocols (class, category->protocols);
}
__objc_register_instance_methods_to_class (class);
}
else
{
unclaimed_categories = list_cons (category, unclaimed_categories);
}
}
if (statics)
uninitialized_statics = list_cons (statics, uninitialized_statics);
if (uninitialized_statics)
objc_init_statics ();
for (cell = &unclaimed_categories; *cell; )
{
Category_t category = (*cell)->head;
Class class = objc_lookup_class (category->class_name);
if (class)
{
DEBUG_PRINTF ("attaching stored categories to object: %s\n",
class->name);
list_remove_head (cell);
if (category->instance_methods)
class_add_method_list (class, category->instance_methods);
if (category->class_methods)
class_add_method_list ((Class) class->class_pointer,
category->class_methods);
if (category->protocols)
{
__objc_init_protocols (category->protocols);
__objc_class_add_protocols (class, category->protocols);
}
__objc_register_instance_methods_to_class (class);
}
else
cell = &(*cell)->tail;
}
if (unclaimed_proto_list && objc_lookup_class ("Protocol"))
{
list_mapcar (unclaimed_proto_list,
(void (*) (void *))__objc_init_protocols);
list_free (unclaimed_proto_list);
unclaimed_proto_list = 0;
}
objc_send_load ();
objc_mutex_unlock (__objc_runtime_mutex);
}
static void
objc_send_load (void)
{
if (! __objc_module_list)
return;
if (unresolved_classes)
{
Class class = unresolved_classes->head;
while (objc_lookup_class ((char *) class->super_class))
{
list_remove_head (&unresolved_classes);
if (unresolved_classes)
class = unresolved_classes->head;
else
break;
}
if (unresolved_classes)
return;
}
if (! objc_lookup_class ("NXConstantString") ||
! objc_lookup_class ("Object"))
return;
list_mapcar (__objc_module_list,
(void (*) (void *)) __objc_create_classes_tree);
while (__objc_class_tree_list)
{
#ifdef DEBUG
objc_preorder_traverse (__objc_class_tree_list->head,
0, __objc_tree_print);
#endif
objc_preorder_traverse (__objc_class_tree_list->head,
0, __objc_send_load);
objc_postorder_traverse (__objc_class_tree_list->head,
0, __objc_destroy_class_tree_node);
list_remove_head (&__objc_class_tree_list);
}
list_mapcar (__objc_module_list, (void (*) (void *)) __objc_call_callback);
list_free (__objc_module_list);
__objc_module_list = NULL;
}
static void
__objc_create_classes_tree (Module_t module)
{
Symtab_t symtab = module->symtab;
int i;
for (i = 0; i < symtab->cls_def_cnt; i++)
{
Class class = (Class) symtab->defs[i];
objc_tree_insert_class (class);
}
}
static void
__objc_call_callback (Module_t module)
{
Symtab_t symtab = module->symtab;
int i;
for (i = 0; i < symtab->cls_def_cnt; i++)
{
Class class = (Class) symtab->defs[i];
if (_objc_load_callback)
_objc_load_callback (class, 0);
}
for (i = 0; i < symtab->cat_def_cnt; i++)
{
Category_t category = symtab->defs[i + symtab->cls_def_cnt];
Class class = objc_lookup_class (category->class_name);
if (_objc_load_callback)
_objc_load_callback (class, category);
}
}
static void
init_check_module_version (Module_t module)
{
if ((module->version != OBJC_VERSION) || (module->size != sizeof (Module)))
{
int code;
if (module->version > OBJC_VERSION)
code = OBJC_ERR_OBJC_VERSION;
else if (module->version < OBJC_VERSION)
code = OBJC_ERR_GCC_VERSION;
else
code = OBJC_ERR_MODULE_SIZE;
objc_error (nil, code, "Module %s version %d doesn't match runtime %d\n",
module->name, (int)module->version, OBJC_VERSION);
}
}
static void
__objc_init_protocols (struct objc_protocol_list *protos)
{
size_t i;
static Class proto_class = 0;
if (! protos)
return;
objc_mutex_lock (__objc_runtime_mutex);
if (! proto_class)
proto_class = objc_lookup_class ("Protocol");
if (! proto_class)
{
unclaimed_proto_list = list_cons (protos, unclaimed_proto_list);
objc_mutex_unlock (__objc_runtime_mutex);
return;
}
#if 0
assert (protos->next == 0);
#endif
for (i = 0; i < protos->count; i++)
{
struct objc_protocol *aProto = protos->list[i];
if (((size_t)aProto->class_pointer) == PROTOCOL_VERSION)
{
aProto->class_pointer = proto_class;
__objc_init_protocols (aProto->protocol_list);
}
else if (protos->list[i]->class_pointer != proto_class)
{
objc_error (nil, OBJC_ERR_PROTOCOL_VERSION,
"Version %d doesn't match runtime protocol version %d\n",
(int) ((char *) protos->list[i]->class_pointer
- (char *) 0),
PROTOCOL_VERSION);
}
}
objc_mutex_unlock (__objc_runtime_mutex);
}
static void
__objc_class_add_protocols (Class class, struct objc_protocol_list *protos)
{
if (! protos)
return;
protos->next = class->protocols;
class->protocols = protos;
}