qse/ase/stx/class.c

169 lines
4.4 KiB
C
Raw Normal View History

2005-05-22 15:03:20 +00:00
/*
2005-10-02 15:45:09 +00:00
* $Id: class.c,v 1.27 2005-10-02 15:45:09 bacon Exp $
2005-05-22 15:03:20 +00:00
*/
#include <xp/stx/class.h>
#include <xp/stx/symbol.h>
#include <xp/stx/object.h>
2005-07-19 12:08:04 +00:00
#include <xp/stx/dict.h>
2005-05-22 15:03:20 +00:00
#include <xp/stx/misc.h>
2005-06-08 16:00:51 +00:00
xp_word_t xp_stx_new_class (xp_stx_t* stx, const xp_char_t* name)
2005-05-22 15:03:20 +00:00
{
2005-06-08 16:00:51 +00:00
xp_word_t meta, class;
xp_word_t class_name;
2005-05-22 15:03:20 +00:00
2005-07-05 09:02:13 +00:00
meta = xp_stx_alloc_word_object (
stx, XP_NULL, XP_STX_METACLASS_SIZE, XP_NULL, 0);
2005-05-22 15:03:20 +00:00
XP_STX_CLASS(stx,meta) = stx->class_metaclass;
2005-05-25 16:44:05 +00:00
/* the spec of the metaclass must be the spec of its
* instance. so the XP_STX_CLASS_SIZE is set */
2005-07-19 12:08:04 +00:00
XP_STX_WORD_AT(stx,meta,XP_STX_METACLASS_SPEC) =
2005-07-05 04:29:31 +00:00
XP_STX_TO_SMALLINT((XP_STX_CLASS_SIZE << XP_STX_SPEC_INDEXABLE_BITS) | XP_STX_SPEC_NOT_INDEXABLE);
2005-05-22 15:03:20 +00:00
2005-05-25 16:44:05 +00:00
/* the spec of the class is set later in __create_builtin_classes */
2005-07-05 09:02:13 +00:00
class = xp_stx_alloc_word_object (
stx, XP_NULL, XP_STX_CLASS_SIZE, XP_NULL, 0);
2005-05-22 15:03:20 +00:00
XP_STX_CLASS(stx,class) = meta;
class_name = xp_stx_new_symbol (stx, name);
2005-07-19 12:08:04 +00:00
XP_STX_WORD_AT(stx,class,XP_STX_CLASS_NAME) = class_name;
2005-05-22 15:03:20 +00:00
2005-07-19 12:08:04 +00:00
xp_stx_dict_put (stx, stx->smalltalk, class_name, class);
2005-05-22 15:03:20 +00:00
return class;
}
2005-06-08 16:00:51 +00:00
xp_word_t xp_stx_lookup_class (xp_stx_t* stx, const xp_char_t* name)
2005-05-22 15:03:20 +00:00
{
2005-07-19 12:08:04 +00:00
xp_word_t assoc, meta, value;
2005-05-22 15:03:20 +00:00
2005-07-19 12:08:04 +00:00
assoc = xp_stx_dict_lookup (stx, stx->smalltalk, name);
2005-07-19 15:52:19 +00:00
if (assoc == stx->nil) {
return stx->nil;
}
2005-05-22 16:26:58 +00:00
2005-07-19 12:08:04 +00:00
value = XP_STX_WORD_AT(stx,assoc,XP_STX_ASSOCIATION_VALUE);
2005-05-22 16:26:58 +00:00
meta = XP_STX_CLASS(stx,value);
if (XP_STX_CLASS(stx,meta) != stx->class_metaclass) return stx->nil;
2005-05-22 15:24:57 +00:00
return value;
2005-05-22 15:03:20 +00:00
}
2005-06-29 16:01:32 +00:00
int xp_stx_get_instance_variable_index (
xp_stx_t* stx, xp_word_t class_index,
const xp_char_t* name, xp_word_t* index)
{
2005-07-03 16:37:01 +00:00
xp_word_t index_super = 0;
2005-06-29 16:01:32 +00:00
xp_stx_class_t* class_obj;
2005-06-30 12:07:02 +00:00
xp_stx_char_object_t* string;
2005-06-29 16:01:32 +00:00
2005-07-07 07:45:05 +00:00
class_obj = (xp_stx_class_t*)XP_STX_OBJECT(stx, class_index);
2005-06-29 16:01:32 +00:00
xp_assert (class_obj != XP_NULL);
if (class_obj->superclass != stx->nil) {
if (xp_stx_get_instance_variable_index (
stx, class_obj->superclass, name, &index_super) == 0) {
*index = index_super;
return 0;
}
}
if (class_obj->header.class == stx->class_metaclass) {
/* metaclass */
/* TODO: can a metaclas have instance variables? */
*index = index_super;
}
else {
2005-06-30 15:11:00 +00:00
if (class_obj->variables == stx->nil) *index = 0;
else {
2005-06-30 12:07:02 +00:00
string = XP_STX_CHAR_OBJECT(stx, class_obj->variables);
2005-07-03 16:37:01 +00:00
if (xp_stx_strword(string->data, name, index) != XP_NULL) {
2005-06-30 12:07:02 +00:00
*index += index_super;
return 0;
}
}
2005-06-30 15:11:00 +00:00
*index += index_super;
2005-06-29 16:01:32 +00:00
}
return -1;
}
2005-07-03 16:37:01 +00:00
xp_word_t xp_stx_lookup_class_variable (
xp_stx_t* stx, xp_word_t class_index, const xp_char_t* name)
{
xp_stx_class_t* class_obj;
2005-07-07 07:45:05 +00:00
class_obj = (xp_stx_class_t*)XP_STX_OBJECT(stx, class_index);
2005-07-03 16:37:01 +00:00
xp_assert (class_obj != XP_NULL);
if (class_obj->superclass != stx->nil) {
xp_word_t tmp;
tmp = xp_stx_lookup_class_variable (
stx, class_obj->superclass, name);
if (tmp != stx->nil) return tmp;
}
/* TODO: can a metaclas have class variables? */
if (class_obj->header.class != stx->class_metaclass &&
2005-07-04 08:37:25 +00:00
class_obj->class_variables != stx->nil) {
2005-07-19 12:08:04 +00:00
if (xp_stx_dict_lookup(stx,
class_obj->class_variables,name) != stx->nil) return class_index;
2005-07-03 16:37:01 +00:00
}
return stx->nil;
}
2005-09-11 15:15:35 +00:00
2005-10-02 15:45:09 +00:00
xp_word_t xp_stx_lookup_method (xp_stx_t* stx,
xp_word_t class_index, const xp_char_t* name, xp_bool_t from_super)
2005-09-11 15:15:35 +00:00
{
xp_stx_class_t* class_obj;
class_obj = (xp_stx_class_t*)XP_STX_OBJECT(stx, class_index);
xp_assert (class_obj != XP_NULL);
2005-10-02 10:44:49 +00:00
#if 0
2005-09-11 15:15:35 +00:00
if (class_obj->header.class != stx->class_metaclass &&
2005-09-11 15:43:14 +00:00
class_obj->methods != stx->nil) {
xp_word_t assoc;
2005-10-02 10:44:49 +00:00
assoc = xp_stx_dict_lookup(stx, class_obj->methods, name);
2005-09-11 15:43:14 +00:00
if (assoc != stx->nil) {
xp_assert (XP_STX_CLASS(stx,assoc) == stx->class_association);
return XP_STX_WORD_AT(stx, assoc, XP_STX_ASSOCIATION_VALUE);
}
2005-09-11 15:15:35 +00:00
}
if (class_obj->superclass != stx->nil) {
xp_word_t tmp;
tmp = xp_stx_lookup_method (
stx, class_obj->superclass, name);
if (tmp != stx->nil) return tmp;
}
2005-10-02 10:44:49 +00:00
#endif
while (class_index != stx->nil) {
class_obj = (xp_stx_class_t*)XP_STX_OBJECT(stx, class_index);
2005-10-02 15:45:09 +00:00
2005-10-02 10:44:49 +00:00
xp_assert (class_obj != XP_NULL);
2005-10-02 15:45:09 +00:00
xp_assert (
class_obj->header.class == stx->class_metaclass ||
XP_STX_CLASS(stx,class_obj->header.class) == stx->class_metaclass);
2005-10-02 10:44:49 +00:00
2005-10-02 15:45:09 +00:00
if (from_super) {
from_super = xp_false;
}
else if (class_obj->methods != stx->nil) {
2005-10-02 10:44:49 +00:00
xp_word_t assoc;
assoc = xp_stx_dict_lookup(stx, class_obj->methods, name);
if (assoc != stx->nil) {
xp_assert (XP_STX_CLASS(stx,assoc) == stx->class_association);
return XP_STX_WORD_AT(stx, assoc, XP_STX_ASSOCIATION_VALUE);
}
}
class_index = class_obj->superclass;
}
2005-09-11 15:15:35 +00:00
return stx->nil;
}
2005-10-02 10:44:49 +00:00