Fix class initialization.

This commit is contained in:
Bill Currie 2011-02-04 14:29:47 +09:00
parent 04b86fcdb7
commit acb3c0da11
4 changed files with 72 additions and 62 deletions

View file

@ -85,6 +85,7 @@ typedef struct protocollist_s {
extern struct type_s type_id; extern struct type_s type_id;
extern struct type_s type_Class; extern struct type_s type_Class;
extern struct type_s type_ClassPtr;
extern struct type_s type_Protocol; extern struct type_s type_Protocol;
extern struct type_s type_SEL; extern struct type_s type_SEL;
extern struct type_s type_IMP; extern struct type_s type_IMP;

View file

@ -70,9 +70,6 @@ static hashtab_t *category_hash;
static hashtab_t *protocol_hash; static hashtab_t *protocol_hash;
// these will be built up further // these will be built up further
type_t type_id = { ev_pointer, "id" };
type_t type_Class = { ev_pointer, "Class" };
type_t type_Protocol = { ev_invalid, "Protocol" };
type_t type_SEL = { ev_pointer, "SEL" }; type_t type_SEL = { ev_pointer, "SEL" };
type_t type_IMP = { ev_func, "IMP", ty_none, type_t type_IMP = { ev_func, "IMP", ty_none,
{{&type_id, -3, {&type_id, &type_SEL}}}}; {{&type_id, -3, {&type_id, &type_SEL}}}};
@ -88,9 +85,13 @@ type_t type_category;
type_t type_ivar; type_t type_ivar;
type_t type_module; type_t type_module;
type_t type_id = { ev_pointer, "id" };
type_t type_Class = { ev_invalid, "Class", ty_class};
type_t type_ClassPtr = { ev_pointer, 0, ty_none, {{&type_Class}}};
type_t type_Protocol = { ev_invalid, "Protocol", ty_class};
class_t class_id = {1, "id"}; class_t class_id = {1, "id"};
class_t class_Class = {1, "Class"}; class_t class_Class = {1, "Class"};
class_t class_Protocol = {1, "Protocl"}; class_t class_Protocol = {1, "Protocol"};
static struct_def_t sel_struct[] = { static struct_def_t sel_struct[] = {
{"sel_id", &type_string}, {"sel_id", &type_string},
@ -205,73 +206,83 @@ get_class_name (class_type_t *class_type, int pretty)
return "???"; return "???";
} }
void static void
class_init (void) init_objective_structs (void)
{ {
type_t *type;
symbol_t *sym;
class_id.type = &type_id;
class_Class.type = &type_Class;
class_Protocol.type = &type_Protocol;
class_Class.super_class = get_class (sym = new_symbol ("Object"), 1);
class_Class.methods = new_methodlist ();
symtab_addsymbol (pr.symtab, sym);
sym = new_symbol_type ("obj_module_t", &type_module);
sym->sy_type = sy_type;
symtab_addsymbol (pr.symtab, sym);
type_SEL.t.fldptr.type = make_structure (0, 's', sel_struct, 0)->type; type_SEL.t.fldptr.type = make_structure (0, 's', sel_struct, 0)->type;
chain_type (&type_SEL);
chain_type (&type_IMP);
make_structure (0, 's', method_struct, &type_Method); make_structure (0, 's', method_struct, &type_Method);
chain_type (&type_Method);
type = make_structure (0, 's', class_ivars, 0)->type;
type->ty = ty_class;
type_Class.t.fldptr.type = type;
class_Class.ivars = type->t.symtab;
type = make_structure (0, 's', protocol_ivars, &type_Protocol)->type;
type->ty = ty_class;
type->t.class = &class_Protocol;
class_Protocol.ivars = type->t.symtab;
type = make_structure (0, 's', id_ivars, 0)->type;
type->ty = ty_class;
type->t.class = &class_id;
type_id.t.fldptr.type = type;
class_id.ivars = type->t.symtab;
make_structure (0, 's', method_desc_struct, &type_method_description); make_structure (0, 's', method_desc_struct, &type_method_description);
chain_type (&type_method_description);
make_structure (0, 's', category_struct, &type_category); make_structure (0, 's', category_struct, &type_category);
chain_type (&type_category);
make_structure (0, 's', ivar_struct, &type_ivar); make_structure (0, 's', ivar_struct, &type_ivar);
make_structure (0, 's', super_struct, &type_Super);
chain_type (&type_IMP);
chain_type (&type_Super);
chain_type (&type_SEL);
chain_type (&type_Method);
chain_type (&type_Class);
chain_type (&type_Protocol);
chain_type (&type_id);
chain_type (&type_method_description);
chain_type (&type_category);
chain_type (&type_ivar); chain_type (&type_ivar);
type_supermsg.t.func.param_types[0] = pointer_type (&type_Super); make_structure (0, 's', super_struct, &type_Super);
chain_type (&type_supermsg); chain_type (&type_Super);
make_structure ("obj_module_s", 's', module_struct, &type_module); make_structure ("obj_module_s", 's', module_struct, &type_module);
chain_type (&type_module); chain_type (&type_module);
type_supermsg.t.func.param_types[0] = pointer_type (&type_Super);
chain_type (&type_supermsg);
type_obj_exec_class.t.func.param_types[0] = pointer_type (&type_module); type_obj_exec_class.t.func.param_types[0] = pointer_type (&type_module);
chain_type (&type_obj_exec_class); chain_type (&type_obj_exec_class);
} }
static void
init_classes (void)
{
symbol_t *sym;
type_Class.ty = ty_class;
type_Class.t.class = &class_Class;
chain_type (&type_Class);
sym = make_structure (0, 's', class_ivars, 0);
class_Class.ivars = sym->type->t.symtab;
class_Class.type = &type_Class;
class_Class.super_class = get_class (sym = new_symbol ("Object"), 1);
class_Class.methods = new_methodlist ();
symtab_addsymbol (pr.symtab, sym);
chain_type (&type_ClassPtr);
type_Protocol.ty = ty_class;
type_Protocol.t.class = &class_Protocol;
chain_type (&type_Protocol);
sym = make_structure (0, 's', protocol_ivars, &type_Protocol);
class_Protocol.ivars = sym->type->t.symtab;
class_Protocol.type = &type_Protocol;
type_id.ty = ty_class;
type_id.t.class = &class_id;
chain_type (&type_id);
sym = make_structure (0, 's', id_ivars, 0);
class_id.ivars = sym->type->t.symtab;
class_id.type = &type_id;
}
void
class_init (void)
{
symbol_t *sym;
init_classes ();
init_objective_structs ();
sym = new_symbol_type ("obj_module_t", &type_module);
sym->sy_type = sy_type;
symtab_addsymbol (pr.symtab, sym);
}
symbol_t * symbol_t *
class_symbol (class_type_t *class_type, int external) class_symbol (class_type_t *class_type, int external)
{ {
@ -287,7 +298,7 @@ class_symbol (class_type_t *class_type, int external)
break; break;
case ct_class: case ct_class:
name = va ("_OBJ_CLASS_%s", class_type->c.class->name); name = va ("_OBJ_CLASS_%s", class_type->c.class->name);
type = type_Class.t.fldptr.type; type = &type_Class;
break; break;
case ct_protocol: case ct_protocol:
return 0; // probably in error recovery return 0; // probably in error recovery
@ -313,7 +324,7 @@ get_class (symbol_t *sym, int create)
c = calloc (sizeof (class_t), 1); c = calloc (sizeof (class_t), 1);
if (sym) if (sym)
c->name = sym->name; c->name = sym->name;
new = *type_Class.t.fldptr.type; new = type_Class;
new.t.class = c; new.t.class = c;
c->type = find_type (&new); c->type = find_type (&new);
c->methods = new_methodlist (); c->methods = new_methodlist ();
@ -332,7 +343,7 @@ set_self_type (class_t *class, method_t *method)
if (method->instance) if (method->instance)
method->params->type = pointer_type (class->type); method->params->type = pointer_type (class->type);
else else
method->params->type = &type_Class; method->params->type = &type_ClassPtr;
} }
static void static void
@ -465,7 +476,7 @@ begin_class (class_t *class)
defspace_t *space; defspace_t *space;
sym = make_symbol (va ("_OBJ_METACLASS_%s", class->name), sym = make_symbol (va ("_OBJ_METACLASS_%s", class->name),
type_Class.t.fldptr.type, pr.far_data, st_static); &type_Class, pr.far_data, st_static);
meta_def = sym->s.def; meta_def = sym->s.def;
meta_def->initialized = meta_def->constant = meta_def->nosave = 1; meta_def->initialized = meta_def->constant = meta_def->nosave = 1;
space = meta_def->space; space = meta_def->space;
@ -475,10 +486,8 @@ begin_class (class_t *class)
EMIT_STRING (space, meta->super_class, class->super_class->name); EMIT_STRING (space, meta->super_class, class->super_class->name);
EMIT_STRING (space, meta->name, class->name); EMIT_STRING (space, meta->name, class->name);
meta->info = _PR_CLS_META; meta->info = _PR_CLS_META;
meta->instance_size = type_size (type_Class.t.fldptr.type); meta->instance_size = type_size (&type_Class);
EMIT_DEF (space, meta->ivars, EMIT_DEF (space, meta->ivars, emit_ivars (class_Class.ivars, "Class"));
emit_ivars (type_Class.t.fldptr.type->t.class->ivars,
"Class"));
current_class = &class->class_type; current_class = &class->class_type;
sym = class_symbol (current_class, 0); sym = class_symbol (current_class, 0);
class->def = def = sym->s.def; class->def = def = sym->s.def;

View file

@ -275,7 +275,7 @@ static keyword_t keywords[] = {
{"unsigned", TYPE, &type_integer, 0, PROG_VERSION, 0},//FIXME {"unsigned", TYPE, &type_integer, 0, PROG_VERSION, 0},//FIXME
{"function", TYPE, &type_function, 0, PROG_VERSION, 0}, {"function", TYPE, &type_function, 0, PROG_VERSION, 0},
{"id", TYPE, &type_id, 0, PROG_VERSION, 1}, {"id", TYPE, &type_id, 0, PROG_VERSION, 1},
{"Class", TYPE, &type_Class, 0, PROG_VERSION, 1}, {"Class", TYPE, &type_ClassPtr, 0, PROG_VERSION, 1},//FIXME
// {"Protocol", TYPE, &type_Protocol, 0, PROG_VERSION, 0}, // {"Protocol", TYPE, &type_Protocol, 0, PROG_VERSION, 0},
{"Method", TYPE, &type_Method, 0, PROG_VERSION, 1}, {"Method", TYPE, &type_Method, 0, PROG_VERSION, 1},
{"Super", TYPE, &type_Super, 0, PROG_VERSION, 1}, {"Super", TYPE, &type_Super, 0, PROG_VERSION, 1},

View file

@ -576,7 +576,7 @@ _encode_type (dstring_t *encoding, type_t *type, int level)
dasprintf (encoding, ":"); dasprintf (encoding, ":");
break; break;
} }
if (type == &type_Class) { if (type == &type_ClassPtr) {
dasprintf (encoding, "#"); dasprintf (encoding, "#");
break; break;
} }
@ -695,7 +695,7 @@ _parse_type (const char **str)
case '@': case '@':
return &type_id; return &type_id;
case '#': case '#':
return &type_Class; return &type_ClassPtr;
case ':': case ':':
return &type_SEL; return &type_SEL;
case '^': case '^':