2002-05-08 21:24:24 +00:00
|
|
|
/*
|
|
|
|
method.c
|
|
|
|
|
|
|
|
QC method support code
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
Copyright (C) 2002 Bill Currie
|
2002-05-08 21:24:24 +00:00
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/5/7
|
|
|
|
|
|
|
|
This program 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
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2002-05-08 21:24:24 +00:00
|
|
|
#include "QF/dstring.h"
|
2002-05-09 20:12:28 +00:00
|
|
|
#include "QF/hash.h"
|
2002-05-15 23:24:19 +00:00
|
|
|
#include "QF/pr_obj.h"
|
|
|
|
#include "QF/va.h"
|
2002-05-08 21:24:24 +00:00
|
|
|
|
|
|
|
#include "qfcc.h"
|
|
|
|
|
2002-06-01 05:30:16 +00:00
|
|
|
#include "expr.h"
|
2002-05-08 21:24:24 +00:00
|
|
|
#include "class.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "def.h"
|
2002-07-13 06:09:03 +00:00
|
|
|
#include "emit.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "immediate.h"
|
2002-05-08 21:24:24 +00:00
|
|
|
#include "method.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "reloc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2002-05-15 23:24:19 +00:00
|
|
|
#include "struct.h"
|
2002-05-08 21:24:24 +00:00
|
|
|
#include "type.h"
|
|
|
|
|
|
|
|
method_t *
|
|
|
|
new_method (type_t *ret_type, param_t *selector, param_t *opt_parms)
|
|
|
|
{
|
|
|
|
method_t *meth = malloc (sizeof (method_t));
|
2002-05-22 05:03:36 +00:00
|
|
|
param_t *cmd = new_param (0, &type_SEL, "_cmd");
|
2002-05-08 21:24:24 +00:00
|
|
|
param_t *self = new_param (0, &type_id, "self");
|
2002-05-16 20:20:23 +00:00
|
|
|
dstring_t *name = dstring_newstr ();
|
|
|
|
dstring_t *types = dstring_newstr ();
|
2002-05-08 21:24:24 +00:00
|
|
|
|
|
|
|
opt_parms = reverse_params (opt_parms);
|
|
|
|
selector = _reverse_params (selector, opt_parms);
|
|
|
|
cmd->next = selector;
|
|
|
|
self->next = cmd;
|
|
|
|
|
|
|
|
meth->next = 0;
|
|
|
|
meth->instance = 0;
|
|
|
|
meth->selector = selector;
|
|
|
|
meth->params = self;
|
|
|
|
meth->type = parse_params (ret_type, meth->params);
|
2002-05-16 20:20:23 +00:00
|
|
|
|
|
|
|
selector_name (name, (keywordarg_t *)selector);
|
|
|
|
selector_types (types, (keywordarg_t *)selector);
|
|
|
|
meth->name = name->str;
|
|
|
|
meth->types = types->str;
|
|
|
|
free (name);
|
|
|
|
free (types);
|
|
|
|
|
2002-05-08 21:24:24 +00:00
|
|
|
//print_type (meth->type); puts ("");
|
|
|
|
meth->def = 0;
|
|
|
|
return meth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
add_method (methodlist_t *methodlist, method_t *method)
|
|
|
|
{
|
|
|
|
if (method->next) {
|
|
|
|
error (0, "add_method: method loop detected");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
*methodlist->tail = method;
|
|
|
|
methodlist->tail = &method->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
def_t *
|
2002-11-14 18:17:43 +00:00
|
|
|
method_def (class_type_t *class_type, method_t *method)
|
2002-05-08 21:24:24 +00:00
|
|
|
{
|
|
|
|
dstring_t *str = dstring_newstr ();
|
2002-05-09 20:12:28 +00:00
|
|
|
def_t *def;
|
|
|
|
char *s;
|
2002-11-14 18:17:43 +00:00
|
|
|
const char *class_name;
|
|
|
|
const char *category_name = "";
|
|
|
|
|
|
|
|
if (class_type->is_class) {
|
|
|
|
class_name = class_type->c.class->name;
|
|
|
|
} else {
|
|
|
|
class_name = class_type->c.category->class->name;
|
|
|
|
category_name = class_type->c.category->name;
|
|
|
|
}
|
2002-05-09 20:12:28 +00:00
|
|
|
|
|
|
|
dsprintf (str, "_%c_%s_%s_%s",
|
|
|
|
method->instance ? 'i' : 'c',
|
2002-11-14 18:17:43 +00:00
|
|
|
class_name,
|
|
|
|
category_name,
|
2002-05-16 20:20:23 +00:00
|
|
|
method->name);
|
2002-05-21 23:02:56 +00:00
|
|
|
str->str[--str->size - 1] = 0;
|
2002-05-09 20:12:28 +00:00
|
|
|
for (s = str->str; *s; s++)
|
|
|
|
if (*s == ':')
|
|
|
|
*s = '_';
|
2002-05-21 23:02:56 +00:00
|
|
|
//printf ("%s %s %s %ld\n", method->name, method->types, str->str, str->size);
|
2002-06-28 17:59:32 +00:00
|
|
|
def = get_def (method->type, str->str, pr.scope, st_static);
|
2002-05-09 20:12:28 +00:00
|
|
|
dstring_delete (str);
|
|
|
|
return def;
|
2002-05-08 21:24:24 +00:00
|
|
|
}
|
2002-05-08 23:12:49 +00:00
|
|
|
|
2002-05-10 00:00:23 +00:00
|
|
|
methodlist_t *
|
|
|
|
new_methodlist (void)
|
|
|
|
{
|
|
|
|
methodlist_t *l = malloc (sizeof (methodlist_t));
|
|
|
|
l->head = 0;
|
|
|
|
l->tail = &l->head;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
copy_methods (methodlist_t *dst, methodlist_t *src)
|
|
|
|
{
|
|
|
|
method_t *s, *d;
|
|
|
|
|
2002-08-13 21:17:20 +00:00
|
|
|
for (s = src->head; s; s = s->next) {
|
2002-05-10 00:00:23 +00:00
|
|
|
d = malloc (sizeof (method_t));
|
|
|
|
*d = *s;
|
|
|
|
d->next = 0;
|
|
|
|
add_method (dst, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-15 23:24:19 +00:00
|
|
|
int
|
|
|
|
method_compare (method_t *m1, method_t *m2)
|
|
|
|
{
|
2002-05-16 20:48:41 +00:00
|
|
|
if (m1->instance != m2->instance)
|
|
|
|
return 0;
|
|
|
|
return strcmp (m1->name, m2->name) == 0
|
|
|
|
&& strcmp (m1->types, m2->types) == 0;
|
2002-05-15 23:24:19 +00:00
|
|
|
}
|
|
|
|
|
2002-05-08 23:12:49 +00:00
|
|
|
keywordarg_t *
|
|
|
|
new_keywordarg (const char *selector, struct expr_s *expr)
|
|
|
|
{
|
|
|
|
keywordarg_t *k = malloc (sizeof (keywordarg_t));
|
|
|
|
|
|
|
|
k->next = 0;
|
|
|
|
k->selector = selector;
|
|
|
|
k->expr = expr;
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2002-08-20 02:09:34 +00:00
|
|
|
keywordarg_t *
|
|
|
|
copy_keywordargs (const keywordarg_t *kwargs)
|
|
|
|
{
|
|
|
|
keywordarg_t *n_kwargs = 0, **kw = &n_kwargs;
|
|
|
|
|
|
|
|
while (kwargs) {
|
|
|
|
*kw = new_keywordarg (kwargs->selector, kwargs->expr);
|
|
|
|
kwargs = kwargs->next;
|
|
|
|
kw = &(*kw)->next;
|
|
|
|
}
|
|
|
|
return n_kwargs;
|
|
|
|
}
|
|
|
|
|
2002-05-08 23:12:49 +00:00
|
|
|
expr_t *
|
2002-05-16 20:20:23 +00:00
|
|
|
send_message (int super)
|
2002-05-08 23:12:49 +00:00
|
|
|
{
|
2002-09-11 16:21:26 +00:00
|
|
|
return new_def_expr (get_def (&type_IMP,
|
|
|
|
super ? "obj_msgSend_super" : "obj_msgSend",
|
|
|
|
pr.scope, st_extern));
|
2002-05-08 23:12:49 +00:00
|
|
|
}
|
2002-05-09 20:12:28 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
selector_name (dstring_t *sel_id, keywordarg_t *selector)
|
|
|
|
{
|
|
|
|
dstring_clearstr (sel_id);
|
|
|
|
while (selector && selector->selector) {
|
|
|
|
dstring_appendstr (sel_id, selector->selector);
|
|
|
|
dstring_appendstr (sel_id, ":");
|
|
|
|
selector = selector->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
selector_types (dstring_t *sel_types, keywordarg_t *selector)
|
|
|
|
{
|
2002-05-15 23:24:19 +00:00
|
|
|
dstring_clearstr (sel_types);
|
2002-05-09 20:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
string_t sel_id;
|
|
|
|
string_t sel_types;
|
|
|
|
def_t *def;
|
|
|
|
} sel_def_t;
|
|
|
|
|
|
|
|
static hashtab_t *sel_def_hash;
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
sel_def_get_hash (void *_sel_def, void *unused)
|
|
|
|
{
|
|
|
|
sel_def_t *sel_def = (sel_def_t*)_sel_def;
|
|
|
|
unsigned long hash;
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
hash = Hash_String (G_GETSTR (sel_def->sel_id))
|
|
|
|
^ Hash_String (G_GETSTR (sel_def->sel_types));
|
2002-05-09 20:12:28 +00:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sel_def_compare (void *_sd1, void *_sd2, void *unused)
|
|
|
|
{
|
|
|
|
sel_def_t *sd1 = (sel_def_t*)_sd1;
|
|
|
|
sel_def_t *sd2 = (sel_def_t*)_sd2;
|
|
|
|
int cmp;
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
cmp = strcmp (G_GETSTR (sd1->sel_id), G_GETSTR (sd2->sel_id)) == 0;
|
2002-05-09 20:12:28 +00:00
|
|
|
if (cmp)
|
2002-07-05 20:02:10 +00:00
|
|
|
cmp = strcmp (G_GETSTR (sd1->sel_types),
|
|
|
|
G_GETSTR (sd2->sel_types)) == 0;
|
2002-05-09 20:12:28 +00:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
def_t *
|
2002-07-13 06:09:03 +00:00
|
|
|
selector_def (const char *sel_id, const char *sel_types)
|
2002-05-09 20:12:28 +00:00
|
|
|
{
|
2002-07-13 06:09:03 +00:00
|
|
|
sel_def_t _sel_def = {ReuseString (sel_id), ReuseString (sel_types), 0};
|
2002-05-09 20:12:28 +00:00
|
|
|
sel_def_t *sel_def = &_sel_def;
|
|
|
|
|
|
|
|
if (!sel_def_hash) {
|
|
|
|
sel_def_hash = Hash_NewTable (1021, 0, 0, 0);
|
|
|
|
Hash_SetHashCompare (sel_def_hash, sel_def_get_hash, sel_def_compare);
|
|
|
|
}
|
|
|
|
sel_def = Hash_FindElement (sel_def_hash, sel_def);
|
|
|
|
if (sel_def)
|
|
|
|
return sel_def->def;
|
|
|
|
sel_def = malloc (sizeof (sel_def_t));
|
2002-06-09 04:30:02 +00:00
|
|
|
sel_def->def = new_def (type_SEL.aux_type, ".imm", pr.scope);
|
2002-05-16 21:57:03 +00:00
|
|
|
sel_def->def->initialized = sel_def->def->constant = 1;
|
2002-06-10 20:54:22 +00:00
|
|
|
sel_def->def->ofs = new_location (type_SEL.aux_type, pr.near_data);
|
2002-07-13 06:09:03 +00:00
|
|
|
EMIT_STRING (G_INT (sel_def->def->ofs), sel_id);
|
|
|
|
EMIT_STRING (G_INT (sel_def->def->ofs + 1), sel_types);
|
|
|
|
sel_def->sel_id = G_INT (sel_def->def->ofs);
|
|
|
|
sel_def->sel_types = G_INT (sel_def->def->ofs + 1);
|
2002-05-09 20:12:28 +00:00
|
|
|
Hash_AddElement (sel_def_hash, sel_def);
|
|
|
|
return sel_def->def;
|
|
|
|
}
|
2002-05-15 23:24:19 +00:00
|
|
|
|
2002-07-13 06:09:03 +00:00
|
|
|
def_t *
|
2002-05-15 23:24:19 +00:00
|
|
|
emit_methods (methodlist_t *_methods, const char *name, int instance)
|
|
|
|
{
|
|
|
|
const char *type = instance ? "INSTANCE" : "CLASS";
|
|
|
|
method_t *method;
|
|
|
|
int i, count;
|
|
|
|
def_t *methods_def;
|
|
|
|
pr_method_list_t *methods;
|
|
|
|
type_t *method_list;
|
|
|
|
|
2002-05-16 21:57:03 +00:00
|
|
|
if (!_methods)
|
|
|
|
return 0;
|
2002-05-15 23:24:19 +00:00
|
|
|
for (count = 0, method = _methods->head; method; method = method->next)
|
2002-05-21 22:51:46 +00:00
|
|
|
if (!method->instance == !instance) {
|
|
|
|
if (!method->def) {
|
|
|
|
warning (0, "method %s not implemented", method->name);
|
|
|
|
}
|
2002-05-15 23:24:19 +00:00
|
|
|
count++;
|
2002-05-21 22:51:46 +00:00
|
|
|
}
|
2002-05-16 21:57:03 +00:00
|
|
|
if (!count)
|
|
|
|
return 0;
|
2002-05-15 23:24:19 +00:00
|
|
|
method_list = new_struct (0);
|
|
|
|
new_struct_field (method_list, &type_pointer, "method_next", vis_public);
|
|
|
|
new_struct_field (method_list, &type_integer, "method_count", vis_public);
|
|
|
|
for (i = 0; i < count; i++)
|
2002-05-22 05:03:36 +00:00
|
|
|
new_struct_field (method_list, type_Method.aux_type, 0, vis_public);
|
2002-06-09 04:30:02 +00:00
|
|
|
methods_def = get_def (method_list, va ("_OBJ_%s_METHODS_%s", type, name),
|
2002-06-28 17:59:32 +00:00
|
|
|
pr.scope, st_static);
|
2002-05-16 21:57:03 +00:00
|
|
|
methods_def->initialized = methods_def->constant = 1;
|
2002-05-15 23:24:19 +00:00
|
|
|
methods = &G_STRUCT (pr_method_list_t, methods_def->ofs);
|
|
|
|
methods->method_next = 0;
|
|
|
|
methods->method_count = count;
|
|
|
|
for (i = 0, method = _methods->head; method; method = method->next) {
|
2002-05-21 22:51:46 +00:00
|
|
|
if (!method->instance != !instance || !method->def)
|
2002-05-15 23:24:19 +00:00
|
|
|
continue;
|
2002-07-13 06:09:03 +00:00
|
|
|
EMIT_STRING (methods->method_list[i].method_name.sel_id, method->name);
|
|
|
|
EMIT_STRING (methods->method_list[i].method_name.sel_types, method->types);
|
|
|
|
EMIT_STRING (methods->method_list[i].method_types, method->types);
|
2002-05-31 06:00:11 +00:00
|
|
|
methods->method_list[i].method_imp = G_FUNCTION (method->def->ofs);
|
2002-06-21 20:46:56 +00:00
|
|
|
if (method->func) {
|
2002-07-13 06:09:03 +00:00
|
|
|
reloc_def_func (method->func,
|
|
|
|
POINTER_OFS (&methods->method_list[i].method_imp));
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
2002-05-31 06:00:11 +00:00
|
|
|
i++;
|
2002-05-15 23:24:19 +00:00
|
|
|
}
|
2002-07-13 06:09:03 +00:00
|
|
|
return methods_def;
|
2002-05-15 23:24:19 +00:00
|
|
|
}
|
2002-06-28 16:00:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
clear_selectors (void)
|
|
|
|
{
|
|
|
|
if (sel_def_hash)
|
|
|
|
Hash_FlushTable (sel_def_hash);
|
|
|
|
}
|