| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include <assert.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #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, tid_t tid, sym_t **new)
- {
- sym_t *sym;
- sym = sym_get(st, name);
- if (sym) {
- /* if there is no type info, accept anything */
- if (!sym->tid)
- sym->tid = tid;
- /* otherwise, if specified, it must match */
- else if (tid && (sym->tid != tid))
- return -EINVAL;
- goto found;
- }
- st->sym = realloc(st->sym, ++st->len * sizeof(*st->sym));
- assert(st->sym);
- sym = &st->sym[st->len-1];
- sym->name = name;
- sym->tid = tid;
- found:
- if (new)
- *new = sym;
- return 0;
- }
- void sym_dump(sym_t *sym, FILE *fp)
- {
- type_dump_cdecl(type_info(sym->tid), 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);
- }
- }
|