beginnings of structure code. committed because cvs diff doesn't work on

new files
This commit is contained in:
Bill Currie 2001-12-08 00:09:11 +00:00
parent 1767ad435f
commit 3293c5e85f
2 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,12 @@
typedef struct struct_field_s {
struct struct_field_s *next;
const char *name;
struct type_s *type;
int offset;
} struct_field_t;
struct_field_t *new_struct_field (struct type_s *strct, struct type_s *type,
const char *name);
struct_field_t *struct_find_field (struct type_s *strct, const char *name);
struct type_s *new_struct (const char *name);
struct type_s *find_struct (const char *name);

View file

@ -0,0 +1,85 @@
#include <QF/hash.h>
#include "qfcc.h"
#include "struct.h"
typedef struct {
const char *name;
type_t *type;
} struct_t;
static hashtab_t *structs;
static const char *
structs_get_key (void *s, void *unused)
{
return ((struct_t *) s)->name;
}
static const char *
struct_field_get_key (void *f, void *unused)
{
return ((struct_field_t *) f)->name;
}
struct_field_t *
new_struct_field (type_t *strct, type_t *type, const char *name)
{
struct_field_t *field;
if (!strct)
return 0;
field = malloc (sizeof (struct_field_t));
field->name = name;
field->type = type;
field->offset = strct->num_parms;
strct->num_parms += pr_type_size[type->type];
field->next = 0;
*strct->struct_tail = field;
strct->struct_tail = &field->next;
return 0;
}
struct_field_t *
struct_find_field (type_t *strct, const char *name)
{
if (!structs)
return 0;
return Hash_Find (strct->struct_fields, name);
}
type_t *
new_struct (const char *name)
{
struct_t *strct;
if (!structs) {
structs = Hash_NewTable (16381, structs_get_key, 0, 0);
}
strct = (struct_t *) Hash_Find (structs, name);
if (strct) {
error (0, "duplicate struct definition");
return 0;
}
strct = malloc (sizeof (struct_t));
strct->name = name;
strct->type = calloc (1, sizeof (type_t));
strct->type->type = ev_struct;
strct->type->struct_tail = &strct->type->struct_head;
strct->type->struct_fields = Hash_NewTable (61, struct_field_get_key, 0, 0);
Hash_Add (structs, strct);
return strct->type;
}
type_t *
find_struct (const char *name)
{
struct_t *strct;
if (!structs)
return 0;
strct = (struct_t *) Hash_Find (structs, name);
if (strct)
return strct->type;
return 0;
}