Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
Contributed by Kresten Krab Thorup and Dennis Glatting.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version.
GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
GNU CC; see the file COPYING. If not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
GCC to produce an executable, this does not cause the resulting executable
to be covered by the GNU General Public License. This exception does not
however invalidate any other reasons why the executable file might be
covered by the GNU General Public License. */
#include "runtime.h"
#include "sarray.h"
static cache_ptr __objc_class_hash = 0;
objc_lookup_class if the runtime is not able to find the class.
This may e.g. try to load in the class using dynamic loading */
Class (*_objc_lookup_class)(const char* name) = 0;
BOOL __objc_class_links_resolved = NO;
#define CLASS_HASH_SIZE 32
void __objc_init_class_tables()
{
if(__objc_class_hash)
return;
objc_mutex_lock(__objc_runtime_mutex);
__objc_class_hash
= hash_new (CLASS_HASH_SIZE,
(hash_func_type) hash_string,
(compare_func_type) compare_strings);
objc_mutex_unlock(__objc_runtime_mutex);
}
class a number, unless it's already known */
void
__objc_add_class_to_hash(Class class)
{
Class h_class;
objc_mutex_lock(__objc_runtime_mutex);
assert(__objc_class_hash);
assert(CLS_ISCLASS(class));
h_class = hash_value_for_key (__objc_class_hash, class->name);
if (!h_class)
{
number. */
static unsigned int class_number = 1;
CLS_SETNUMBER(class, class_number);
CLS_SETNUMBER(class->class_pointer, class_number);
++class_number;
hash_add (&__objc_class_hash, class->name, class);
}
objc_mutex_unlock(__objc_runtime_mutex);
}
identify a known class, the hook _objc_lookup_class is called. If
this fails, nil is returned */
Class objc_lookup_class (const char* name)
{
Class class;
objc_mutex_lock(__objc_runtime_mutex);
assert (__objc_class_hash);
class = hash_value_for_key (__objc_class_hash, name);
objc_mutex_unlock(__objc_runtime_mutex);
if (class)
return class;
if (_objc_lookup_class)
return (*_objc_lookup_class)(name);
else
return 0;
}
identify a known class, the hook _objc_lookup_class is called. If
this fails, an error message is issued and the system aborts */
Class
objc_get_class (const char *name)
{
Class class;
objc_mutex_lock(__objc_runtime_mutex);
assert (__objc_class_hash);
class = hash_value_for_key (__objc_class_hash, name);
objc_mutex_unlock(__objc_runtime_mutex);
if (class)
return class;
if (_objc_lookup_class)
class = (*_objc_lookup_class)(name);
if(class)
return class;
objc_error(nil, OBJC_ERR_BAD_CLASS,
"objc runtime: cannot find class %s\n", name);
return 0;
}
MetaClass
objc_get_meta_class(const char *name)
{
return objc_get_class(name)->class_pointer;
}
executable. Pass *ENUM_STATE == NULL to start the enumeration. The
function will return 0 when there are no more classes.
For example:
id class;
void *es = NULL;
while ((class = objc_next_class(&es)))
... do something with class;
*/
Class
objc_next_class(void **enum_state)
{
objc_mutex_lock(__objc_runtime_mutex);
assert(__objc_class_hash);
*(node_ptr*)enum_state =
hash_next(__objc_class_hash, *(node_ptr*)enum_state);
objc_mutex_unlock(__objc_runtime_mutex);
if (*(node_ptr*)enum_state)
return (*(node_ptr*)enum_state)->value;
return (Class)0;
}
can be sure of is that the class_pointer for class objects point
to the right meta class objects */
void __objc_resolve_class_links()
{
node_ptr node;
Class object_class = objc_get_class ("Object");
assert(object_class);
objc_mutex_lock(__objc_runtime_mutex);
for (node = hash_next (__objc_class_hash, NULL); node;
node = hash_next (__objc_class_hash, node))
{
Class class1 = node->value;
assert (CLS_ISCLASS(class1));
assert (CLS_ISMETA(class1->class_pointer));
class1->class_pointer->class_pointer = object_class->class_pointer;
if (!(CLS_ISRESOLV(class1)))
{
CLS_SETRESOLV(class1);
CLS_SETRESOLV(class1->class_pointer);
if(class1->super_class)
{
Class a_super_class
= objc_get_class ((char *) class1->super_class);
assert (a_super_class);
DEBUG_PRINTF ("making class connections for: %s\n",
class1->name);
class1->sibling_class = a_super_class->subclass_list;
a_super_class->subclass_list = class1;
if (a_super_class->class_pointer)
{
class1->class_pointer->sibling_class
= a_super_class->class_pointer->subclass_list;
a_super_class->class_pointer->subclass_list
= class1->class_pointer;
}
}
else
{
class1->class_pointer->sibling_class
= object_class->subclass_list;
object_class->subclass_list = class1->class_pointer;
}
}
}
for (node = hash_next (__objc_class_hash, NULL); node;
node = hash_next (__objc_class_hash, node))
{
Class class1 = node->value;
Class sub_class;
for (sub_class = class1->subclass_list; sub_class;
sub_class = sub_class->sibling_class)
{
sub_class->super_class = class1;
if(CLS_ISCLASS(sub_class))
sub_class->class_pointer->super_class = class1->class_pointer;
}
}
objc_mutex_unlock(__objc_runtime_mutex);
}
#define CLASSOF(c) ((c)->class_pointer)
Class
class_pose_as (Class impostor, Class super_class)
{
node_ptr node;
Class class1;
if (!CLS_ISRESOLV (impostor))
__objc_resolve_class_links ();
assert (impostor);
assert (super_class);
assert (impostor->super_class == super_class);
assert (CLS_ISCLASS (impostor));
assert (CLS_ISCLASS (super_class));
assert (impostor->instance_size == super_class->instance_size);
{
Class *subclass = &(super_class->subclass_list);
while (*subclass)
{
Class nextSub = (*subclass)->sibling_class;
if (*subclass != impostor)
{
Class sub = *subclass;
sub->sibling_class = impostor->subclass_list;
sub->super_class = impostor;
impostor->subclass_list = sub;
the top of the meta class hierarchy chain. (root
meta-class objects inherit their class object) If that is
the case... don't mess with the meta-meta class. */
if (CLS_ISCLASS (sub))
{
CLASSOF (sub)->sibling_class =
CLASSOF (impostor)->subclass_list;
CLASSOF (sub)->super_class = CLASSOF (impostor);
CLASSOF (impostor)->subclass_list = CLASSOF (sub);
}
}
*subclass = nextSub;
}
super_class->subclass_list = impostor;
CLASSOF (super_class)->subclass_list = CLASSOF (impostor);
impostor->sibling_class = 0;
CLASSOF (impostor)->sibling_class = 0;
}
assert (impostor->super_class == super_class);
assert (CLASSOF (impostor)->super_class == CLASSOF (super_class));
what the keys of the hashtable is, change all values that are
superclass into impostor. */
objc_mutex_lock(__objc_runtime_mutex);
for (node = hash_next (__objc_class_hash, NULL); node;
node = hash_next (__objc_class_hash, node))
{
class1 = (Class)node->value;
if (class1 == super_class)
{
node->value = impostor;
}
}
objc_mutex_unlock(__objc_runtime_mutex);
__objc_update_dispatch_table_for_class (CLASSOF (impostor));
__objc_update_dispatch_table_for_class (impostor);
return impostor;
}