#include #include #include #include #include #include "sym.h" sym_t *sym_get(symtab_t *st, const char *name) { size_t i; for (i = 0; i < st->len; i++) { if (!strcmp(st->sym[i]->name, name)) return st->sym[i]; } return NULL; } int sym_add(symtab_t *st, const char *name, type_t *type, sym_t **new) { sym_t *sym; sym = sym_get(st, name); if (sym) { /* if there is no type info, accept anything */ if (!sym->type) sym->type = type; /* otherwise, if specified, it must match */ else if (type && !type_equal(sym->type, type)) return -EINVAL; goto found; } st->sym = realloc(st->sym, ++st->len * sizeof(*st->sym)); assert(st->sym); st->sym[st->len - 1] = calloc(1, sizeof(sym_t)); sym = st->sym[st->len - 1]; sym->st = st; sym->name = name; sym->type = type; found: if (new) *new = sym; return 0; } void sym_dump(sym_t *sym, FILE *fp) { type_dump(sym->type, fp); fputc(' ', fp); fputs(sym->name, fp); } void symtab_dump(symtab_t *st, FILE *fp) { size_t i; for (i = 0; i < st->len; i++) { sym_dump(st->sym[i], fp); fputc('\n', fp); } }