2011-01-24 23:54:02 +00:00
|
|
|
/*
|
|
|
|
symtab.c
|
|
|
|
|
|
|
|
Symbol table management.
|
|
|
|
|
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2011/01/05
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-01-05 10:09:49 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-12-06 11:52:53 +00:00
|
|
|
#include "QF/alloc.h"
|
2011-01-05 10:09:49 +00:00
|
|
|
#include "QF/hash.h"
|
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qfcc/include/class.h"
|
|
|
|
#include "tools/qfcc/include/def.h"
|
|
|
|
#include "tools/qfcc/include/defspace.h"
|
|
|
|
#include "tools/qfcc/include/diagnostic.h"
|
|
|
|
#include "tools/qfcc/include/function.h"
|
|
|
|
#include "tools/qfcc/include/qfcc.h"
|
|
|
|
#include "tools/qfcc/include/reloc.h"
|
2023-02-08 02:18:42 +00:00
|
|
|
#include "tools/qfcc/include/shared.h"
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qfcc/include/strpool.h"
|
|
|
|
#include "tools/qfcc/include/symtab.h"
|
|
|
|
#include "tools/qfcc/include/type.h"
|
2011-01-05 10:09:49 +00:00
|
|
|
|
2013-03-08 13:16:31 +00:00
|
|
|
static symtab_t *symtabs_freelist;
|
|
|
|
static symbol_t *symbols_freelist;
|
2011-01-05 10:09:49 +00:00
|
|
|
|
2022-07-26 12:14:28 +00:00
|
|
|
static const char * const sy_type_names[] = {
|
2019-06-05 22:01:44 +00:00
|
|
|
"sy_name",
|
2012-12-05 10:45:16 +00:00
|
|
|
"sy_var",
|
|
|
|
"sy_const",
|
|
|
|
"sy_type",
|
|
|
|
"sy_expr",
|
|
|
|
"sy_func",
|
|
|
|
"sy_class",
|
2020-03-05 09:45:47 +00:00
|
|
|
"sy_convert",
|
2012-12-05 10:45:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *
|
|
|
|
symtype_str (sy_type_e type)
|
|
|
|
{
|
2012-12-21 12:53:13 +00:00
|
|
|
if (type > sy_class)
|
2012-12-05 10:45:16 +00:00
|
|
|
return "<invalid sy_type>";
|
|
|
|
return sy_type_names[type];
|
|
|
|
}
|
|
|
|
|
2011-01-13 05:48:38 +00:00
|
|
|
symbol_t *
|
|
|
|
new_symbol (const char *name)
|
|
|
|
{
|
|
|
|
symbol_t *symbol;
|
|
|
|
ALLOC (256, symbol_t, symbols, symbol);
|
2022-01-19 12:32:01 +00:00
|
|
|
if (name) {
|
|
|
|
symbol->name = save_string (name);
|
|
|
|
}
|
2011-01-13 05:48:38 +00:00
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2011-01-17 03:04:41 +00:00
|
|
|
symbol_t *
|
|
|
|
new_symbol_type (const char *name, type_t *type)
|
|
|
|
{
|
|
|
|
symbol_t *symbol;
|
|
|
|
symbol = new_symbol (name);
|
|
|
|
symbol->type = type;
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2011-01-05 10:09:49 +00:00
|
|
|
static const char *
|
2012-07-18 13:34:37 +00:00
|
|
|
sym_getkey (const void *k, void *unused)
|
2011-01-05 10:09:49 +00:00
|
|
|
{
|
|
|
|
return ((symbol_t *) k)->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
symtab_t *
|
|
|
|
new_symtab (symtab_t *parent, stab_type_e type)
|
|
|
|
{
|
|
|
|
symtab_t *symtab;
|
|
|
|
int tabsize = 63;
|
|
|
|
|
|
|
|
ALLOC (16, symtab_t, symtabs, symtab);
|
|
|
|
|
|
|
|
symtab->parent = parent;
|
|
|
|
symtab->type = type;
|
|
|
|
if (symtab->type == stab_global)
|
|
|
|
tabsize = 1023;
|
2020-03-25 06:43:16 +00:00
|
|
|
symtab->tab = Hash_NewTable (tabsize, sym_getkey, 0, 0, 0);
|
2011-01-05 10:09:49 +00:00
|
|
|
symtab->symtail = &symtab->symbols;
|
|
|
|
return symtab;
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol_t *
|
|
|
|
symtab_lookup (symtab_t *symtab, const char *name)
|
|
|
|
{
|
|
|
|
symbol_t *symbol;
|
2012-11-09 05:36:19 +00:00
|
|
|
do {
|
2011-01-05 10:09:49 +00:00
|
|
|
if ((symbol = Hash_Find (symtab->tab, name)))
|
|
|
|
return symbol;
|
|
|
|
symtab = symtab->parent;
|
2012-11-09 05:36:19 +00:00
|
|
|
} while (symtab);
|
2011-01-05 10:09:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-13 05:48:38 +00:00
|
|
|
symbol_t *
|
|
|
|
symtab_addsymbol (symtab_t *symtab, symbol_t *symbol)
|
2011-01-05 10:09:49 +00:00
|
|
|
{
|
2011-01-13 05:48:38 +00:00
|
|
|
symbol_t *s;
|
2011-02-06 07:56:19 +00:00
|
|
|
if (symbol->table)
|
|
|
|
internal_error (0, "symbol '%s' is already in another symbol table",
|
|
|
|
symbol->name);
|
2011-01-13 05:48:38 +00:00
|
|
|
if ((s = Hash_Find (symtab->tab, symbol->name)))
|
|
|
|
return s;
|
2011-01-05 10:09:49 +00:00
|
|
|
Hash_Add (symtab->tab, symbol);
|
2011-01-13 05:48:38 +00:00
|
|
|
|
2011-01-05 10:09:49 +00:00
|
|
|
symbol->next = *symtab->symtail;
|
|
|
|
*symtab->symtail = symbol;
|
|
|
|
symtab->symtail = &symbol->next;
|
|
|
|
|
2011-01-13 05:48:38 +00:00
|
|
|
symbol->table = symtab;
|
2011-01-05 10:09:49 +00:00
|
|
|
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol_t *
|
2011-01-13 05:48:38 +00:00
|
|
|
symtab_removesymbol (symtab_t *symtab, symbol_t *symbol)
|
2011-01-05 10:09:49 +00:00
|
|
|
{
|
2011-01-13 05:48:38 +00:00
|
|
|
symbol_t **s;
|
|
|
|
|
|
|
|
if (!(symbol = Hash_Del (symtab->tab, symbol->name)))
|
|
|
|
return 0;
|
|
|
|
for (s = &symtab->symbols; *s && *s != symbol; s = & (*s)->next)
|
|
|
|
;
|
|
|
|
if (!*s)
|
2020-03-04 07:31:28 +00:00
|
|
|
internal_error (0, "attempt to remove symbol not in symtab");
|
2011-01-13 05:48:38 +00:00
|
|
|
*s = (*s)->next;
|
|
|
|
if (symtab->symtail == &symbol->next)
|
|
|
|
symtab->symtail = s;
|
|
|
|
symbol->next = 0;
|
|
|
|
symbol->table = 0;
|
2011-01-05 10:09:49 +00:00
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol_t *
|
2011-01-13 05:48:38 +00:00
|
|
|
copy_symbol (symbol_t *symbol)
|
2011-01-05 10:09:49 +00:00
|
|
|
{
|
2011-01-13 05:48:38 +00:00
|
|
|
symbol_t *sym = new_symbol (symbol->name);
|
2020-03-05 09:45:47 +00:00
|
|
|
sym->visibility = symbol->visibility;
|
2011-01-13 05:48:38 +00:00
|
|
|
sym->type = symbol->type;
|
|
|
|
sym->params = copy_params (symbol->params);
|
2011-01-17 13:33:33 +00:00
|
|
|
sym->sy_type = symbol->sy_type;
|
|
|
|
sym->s = symbol->s;
|
2011-01-13 05:48:38 +00:00
|
|
|
return sym;
|
2011-01-05 10:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
symtab_t *
|
2023-02-11 05:38:37 +00:00
|
|
|
symtab_flat_copy (symtab_t *symtab, symtab_t *parent, stab_type_e type)
|
2011-01-05 10:09:49 +00:00
|
|
|
{
|
|
|
|
symtab_t *newtab;
|
|
|
|
symbol_t *newsym;
|
|
|
|
symbol_t *symbol;
|
|
|
|
|
2023-02-11 05:38:37 +00:00
|
|
|
newtab = new_symtab (parent, type);
|
2012-11-09 05:36:19 +00:00
|
|
|
do {
|
2011-01-05 10:09:49 +00:00
|
|
|
for (symbol = symtab->symbols; symbol; symbol = symbol->next) {
|
2020-03-04 16:46:56 +00:00
|
|
|
if (symbol->visibility == vis_anonymous
|
|
|
|
|| Hash_Find (newtab->tab, symbol->name))
|
2011-01-05 10:09:49 +00:00
|
|
|
continue;
|
2011-01-13 05:48:38 +00:00
|
|
|
newsym = copy_symbol (symbol);
|
|
|
|
symtab_addsymbol (newtab, newsym);
|
2011-01-05 10:09:49 +00:00
|
|
|
}
|
|
|
|
symtab = symtab->parent;
|
|
|
|
// Set the tail pointer so symbols in ancestor tables come before
|
|
|
|
// those in decendent tables.
|
|
|
|
newtab->symtail = &newtab->symbols;
|
2012-11-09 05:36:19 +00:00
|
|
|
} while (symtab);
|
2011-01-05 10:09:49 +00:00
|
|
|
// Reset the tail pointer so any symbols added to newtab come after
|
|
|
|
// those copied from the input symbol table chain.
|
|
|
|
for (symbol = newtab->symbols; symbol && symbol->next;
|
|
|
|
symbol = symbol->next)
|
|
|
|
;
|
|
|
|
newtab->symtail = symbol ? &symbol->next : &newtab->symbols;
|
|
|
|
return newtab;
|
|
|
|
}
|
2011-01-24 06:41:43 +00:00
|
|
|
|
|
|
|
symbol_t *
|
|
|
|
make_symbol (const char *name, type_t *type, defspace_t *space,
|
|
|
|
storage_class_t storage)
|
|
|
|
{
|
|
|
|
symbol_t *sym;
|
2011-02-15 01:47:16 +00:00
|
|
|
struct reloc_s *relocs = 0;
|
2011-01-24 06:41:43 +00:00
|
|
|
|
2012-12-02 01:11:30 +00:00
|
|
|
if (storage != sc_extern && storage != sc_global && storage != sc_static)
|
2011-01-24 23:29:00 +00:00
|
|
|
internal_error (0, "invalid storage class for %s", __FUNCTION__);
|
2012-12-02 01:11:30 +00:00
|
|
|
if (storage != sc_extern && !space)
|
2011-01-24 23:29:00 +00:00
|
|
|
internal_error (0, "null space for non-external storage");
|
2011-01-24 06:41:43 +00:00
|
|
|
sym = symtab_lookup (pr.symtab, name);
|
|
|
|
if (!sym) {
|
|
|
|
sym = new_symbol_type (name, type);
|
|
|
|
}
|
|
|
|
if (sym->type != type) {
|
2011-02-15 03:08:03 +00:00
|
|
|
if (is_array (sym->type) && is_array (type)
|
|
|
|
&& !sym->type->t.array.size) {
|
|
|
|
sym->type = type;
|
|
|
|
} else {
|
|
|
|
error (0, "%s redefined", name);
|
|
|
|
sym = new_symbol_type (name, type);
|
|
|
|
}
|
2011-01-24 06:41:43 +00:00
|
|
|
}
|
2012-12-02 01:11:30 +00:00
|
|
|
if (sym->s.def && sym->s.def->external && storage != sc_extern) {
|
2011-03-09 01:52:29 +00:00
|
|
|
//FIXME this really is not the right way
|
2011-02-15 01:47:16 +00:00
|
|
|
relocs = sym->s.def->relocs;
|
2011-01-24 06:41:43 +00:00
|
|
|
free_def (sym->s.def);
|
|
|
|
sym->s.def = 0;
|
|
|
|
}
|
2011-02-15 01:47:16 +00:00
|
|
|
if (!sym->s.def) {
|
2011-01-24 06:41:43 +00:00
|
|
|
sym->s.def = new_def (name, type, space, storage);
|
2012-11-12 07:14:09 +00:00
|
|
|
reloc_attach_relocs (relocs, &sym->s.def->relocs);
|
2011-02-15 01:47:16 +00:00
|
|
|
}
|
2019-06-05 22:01:44 +00:00
|
|
|
sym->sy_type = sy_var;
|
2011-01-24 06:41:43 +00:00
|
|
|
return sym;
|
|
|
|
}
|
2023-02-08 02:18:42 +00:00
|
|
|
|
|
|
|
symbol_t *
|
|
|
|
declare_symbol (specifier_t spec, expr_t *init, symtab_t *symtab)
|
|
|
|
{
|
|
|
|
symbol_t *s = spec.sym;
|
|
|
|
defspace_t *space = symtab->space;
|
|
|
|
|
|
|
|
if (s->table) {
|
|
|
|
// due to the way declarations work, we need a new symbol at all times.
|
|
|
|
// redelcarations will be checked later
|
|
|
|
s = new_symbol (s->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
spec = default_type (spec, s);
|
|
|
|
if (!spec.storage) {
|
|
|
|
spec.storage = current_storage;
|
|
|
|
}
|
|
|
|
if (spec.storage == sc_static) {
|
|
|
|
space = pr.near_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME is_function is bad (this whole implementation of handling
|
|
|
|
//function prototypes is bad)
|
|
|
|
if (spec.is_function && is_func (spec.type)) {
|
|
|
|
set_func_type_attrs (spec.type, spec);
|
|
|
|
}
|
|
|
|
s->type = spec.type;
|
|
|
|
|
|
|
|
if (spec.is_typedef) {
|
|
|
|
if (init) {
|
|
|
|
error (0, "typedef %s is initialized", s->name);
|
|
|
|
}
|
|
|
|
s->sy_type = sy_type;
|
|
|
|
s->type = find_type (s->type);
|
|
|
|
s->type = find_type (alias_type (s->type, s->type, s->name));
|
|
|
|
symtab_addsymbol (symtab, s);
|
|
|
|
} else {
|
|
|
|
if (spec.is_function && is_func (s->type)) {
|
|
|
|
if (init) {
|
|
|
|
error (0, "function %s is initialized", s->name);
|
|
|
|
}
|
|
|
|
s->type = find_type (s->type);
|
|
|
|
s = function_symbol (s, spec.is_overload, 1);
|
|
|
|
} else {
|
|
|
|
s->type = find_type (s->type);
|
|
|
|
initialize_def (s, init, space, spec.storage, symtab);
|
|
|
|
if (s->s.def) {
|
|
|
|
s->s.def->nosave |= spec.nosave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|