[qfcc] Use separate symbol types for def and offset

I don't know why I thought it was a good idea to make sy_var context
dependent. Renaming sy_var to sy_def makes it a little easier to know to
use the def field, too.
This commit is contained in:
Bill Currie 2024-09-20 11:27:15 +09:00
parent 6ca06a577a
commit 34fc2108ea
17 changed files with 43 additions and 40 deletions

View file

@ -35,7 +35,8 @@
#endif
SY_TYPE(name) ///< just a name (referent tbd)
SY_TYPE(var) ///< symbol refers to a variable
SY_TYPE(offset) ///< symbol refers to a variable (struct member)
SY_TYPE(def) ///< symbol refers to a variable
SY_TYPE(const) ///< symbol refers to a constant
SY_TYPE(type) ///< symbol refers to a type
SY_TYPE(type_param) ///< symbol refers to a generic type parameter

View file

@ -69,8 +69,8 @@ typedef struct symbol_s {
struct param_s *params; ///< the parameters if a function
bool no_auto_init:1; ///< skip for non-designated initializers
union {
int offset; ///< sy_var (in a struct/union/macro)
struct def_s *def; ///< sy_var
int offset; ///< sy_offset
struct def_s *def; ///< sy_def
struct ex_value_s *value; ///< sy_const
const struct expr_s *expr; ///< sy_expr/sy_type_param
struct metafunc_s *metafunc;///< sy_func

View file

@ -861,7 +861,7 @@ emit_ivar_list_item (def_t *def, void *data, int index)
__FUNCTION__, index, ivar_data->count);
for (ivar_sym = ivar_data->ivars; ivar_sym; ivar_sym = ivar_sym->next) {
if (ivar_sym->sy_type != sy_var)
if (ivar_sym->sy_type != sy_offset)
continue;
if (!index--)
break;
@ -893,7 +893,7 @@ emit_ivars (symtab_t *ivars, const char *name)
if (ivars) {
ivar_data.ivars = ivars->symbols;
for (s = ivars->symbols; s; s = s->next)
if (s->sy_type == sy_var)
if (s->sy_type == sy_offset)
ivar_data.count++;
}
ivar_list_struct[1].type = array_type (&type_ivar, ivar_data.count);
@ -1979,7 +1979,7 @@ class_finish_ivar_scope (class_type_t *class_type, symtab_t *ivar_scope,
self_expr = cast_expr (class_ptr, self_expr);
}
for (sym = ivar_scope->symbols; sym; sym = sym->next) {
if (sym->sy_type != sy_var)
if (sym->sy_type != sy_offset)
continue;
sym->sy_type = sy_convert;
sym->convert.conv = class_dereference_ivar;

View file

@ -555,7 +555,7 @@ initialize_def (symbol_t *sym, const expr_t *init, defspace_t *space,
error (0, "%s shadows a parameter", sym->name);
}
if (check && check->table == symtab) {
if (check->sy_type != sy_var || !type_same (check->type, sym->type)) {
if (check->sy_type != sy_def || !type_same (check->type, sym->type)) {
error (0, "%s redefined", sym->name);
} else {
// is var and same type
@ -573,7 +573,7 @@ initialize_def (symbol_t *sym, const expr_t *init, defspace_t *space,
sym = check;
}
}
sym->sy_type = sy_var;
sym->sy_type = sy_def;
if (!sym->table)
symtab_addsymbol (symtab, sym);

View file

@ -147,13 +147,13 @@ print_struct (dstring_t *dstr, const type_t *t, int level, set_t *seen)
}
if (t->meta != ty_enum) {
for (sym = symtab->symbols; sym; sym = sym->next) {
if (sym->sy_type != sy_var) {
if (sym->sy_type != sy_offset) {
continue;
}
dot_print_type (dstr, sym->type, level, seen);
}
for (pnum = 0, sym = symtab->symbols; sym; sym = sym->next) {
if (sym->sy_type != sy_var) {
if (sym->sy_type != sy_offset) {
continue;
}
dasprintf (dstr, "%*st_%p:f%d -> \"t_%p\";\n", indent, "",
@ -173,7 +173,7 @@ print_struct (dstring_t *dstr, const type_t *t, int level, set_t *seen)
if (sym->sy_type == sy_const) {
val = sym->value->int_val;
} else {
if (sym->sy_type != sy_var) {
if (sym->sy_type != sy_offset) {
continue;
}
val = sym->offset;

View file

@ -964,7 +964,7 @@ is_constant (const expr_t *e)
}
if (e->type == ex_nil || e->type == ex_value || e->type == ex_labelref
|| (e->type == ex_symbol && e->symbol->sy_type == sy_const)
|| (e->type == ex_symbol && e->symbol->sy_type == sy_var
|| (e->type == ex_symbol && e->symbol->sy_type == sy_def
&& e->symbol->def->constant))
return 1;
return 0;
@ -977,7 +977,7 @@ is_variable (const expr_t *e)
e = e->alias.expr;
}
if (e->type == ex_def
|| (e->type == ex_symbol && e->symbol->sy_type == sy_var)
|| (e->type == ex_symbol && e->symbol->sy_type == sy_def)
|| e->type == ex_temp) {
return 1;
}
@ -1005,7 +1005,7 @@ constant_expr (const expr_t *e)
sym = e->symbol;
if (sym->sy_type == sy_const) {
value = sym->value;
} else if (sym->sy_type == sy_var && sym->def->constant) {
} else if (sym->sy_type == sy_def && sym->def->constant) {
//FIXME pointers and fields
internal_error (e, "what to do here?");
//memset (&value, 0, sizeof (value));
@ -1083,7 +1083,7 @@ expr_double (const expr_t *e)
if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_double)
return e->symbol->value->double_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var
if (e->type == ex_symbol && e->symbol->sy_type == sy_def
&& e->symbol->def->constant
&& is_double (e->symbol->def->type))
return D_DOUBLE (e->symbol->def);
@ -1100,7 +1100,7 @@ expr_float (const expr_t *e)
if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_float)
return e->symbol->value->float_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var
if (e->type == ex_symbol && e->symbol->sy_type == sy_def
&& e->symbol->def->constant
&& is_float (e->symbol->def->type))
return D_FLOAT (e->symbol->def);
@ -1130,7 +1130,7 @@ expr_vector (const expr_t *e)
if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_vector)
return e->symbol->value->vector_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var
if (e->type == ex_symbol && e->symbol->sy_type == sy_def
&& e->symbol->def->constant
&& e->symbol->def->type->type == ev_vector)
return D_VECTOR (e->symbol->def);
@ -1160,7 +1160,7 @@ expr_quaternion (const expr_t *e)
if (e->type == ex_symbol && e->symbol->sy_type == sy_const
&& e->symbol->type->type == ev_quaternion)
return e->symbol->value->quaternion_val;
if (e->type == ex_symbol && e->symbol->sy_type == sy_var
if (e->type == ex_symbol && e->symbol->sy_type == sy_def
&& e->symbol->def->constant
&& e->symbol->def->type->type == ev_quaternion)
return D_QUAT (e->symbol->def);
@ -1208,7 +1208,7 @@ expr_int (const expr_t *e)
|| is_enum (e->symbol->type))) {
return e->symbol->value->int_val;
}
if (e->type == ex_symbol && e->symbol->sy_type == sy_var
if (e->type == ex_symbol && e->symbol->sy_type == sy_def
&& e->symbol->def->constant
&& is_integral (e->symbol->def->type)) {
return D_INT (e->symbol->def);
@ -1253,7 +1253,7 @@ expr_uint (const expr_t *e)
&& e->symbol->type->type == ev_uint) {
return e->symbol->value->uint_val;
}
if (e->type == ex_symbol && e->symbol->sy_type == sy_var
if (e->type == ex_symbol && e->symbol->sy_type == sy_def
&& e->symbol->def->constant
&& is_integral (e->symbol->def->type)) {
return D_INT (e->symbol->def);
@ -2992,7 +2992,7 @@ address_expr (const expr_t *e1, const type_t *t)
}
break;
case ex_symbol:
if (e1->symbol->sy_type == sy_var) {
if (e1->symbol->sy_type == sy_def) {
auto def = e1->symbol->def;
auto type = def->type;
@ -3268,7 +3268,7 @@ think_expr (symbol_t *think_sym)
return new_symbol_expr (think_sym);
sym = symtab_lookup (current_symtab, "think");
if (sym && sym->sy_type == sy_var && sym->type
if (sym && sym->sy_type == sy_def && sym->type
&& sym->type->type == ev_field
&& sym->type->fldptr.type->type == ev_func) {
think_sym->type = sym->type->fldptr.type;

View file

@ -90,8 +90,9 @@ is_lvalue (const expr_t *expr)
case ex_symbol:
switch (expr->symbol->sy_type) {
case sy_name:
case sy_offset:
break;
case sy_var:
case sy_def:
return !expr->symbol->def->readonly;
case sy_const:
break;

View file

@ -187,7 +187,7 @@ get_designated_offset (const type_t *type, const designator_t *des)
static int
skip_field (symbol_t *field)
{
if (field->sy_type != sy_var) {
if (field->sy_type != sy_offset) {
return 1;
}
if (field->no_auto_init) {

View file

@ -1042,7 +1042,7 @@ create_param (symtab_t *parameters, symbol_t *param)
def->offset = defspace_alloc_aligned_highwater (space, size, alignment);
def->type = param->type;
param->def = def;
param->sy_type = sy_var;
param->sy_type = sy_def;
symtab_addsymbol (parameters, param);
if (is_vector(param->type) && options.code.vector_components)
init_vector_components (param, 0, parameters);

View file

@ -122,7 +122,7 @@ glsl_create_block (specifier_t spec, symbol_t *block_sym)
for (auto sym = block->members->symbols; sym; sym = sym->next) {
auto def = new_def (sym->name, nullptr, nullptr, spec.storage);
def->type = sym->type;
sym->sy_type = sy_var;
sym->sy_type = sy_def;
sym->def = def;
}
return block;
@ -132,7 +132,7 @@ void
glsl_finish_block (glsl_block_t *block)
{
for (auto sym = block->members->symbols; sym; sym = sym->next) {
if (sym->sy_type == sy_var) {
if (sym->sy_type == sy_def) {
//FIXME sc_extern isn't correct (problem with unsized arrays)
sym->def = new_def (sym->name, sym->type, block->space, sc_extern);
}

View file

@ -174,7 +174,7 @@ qfo_encode_struct (const type_t *type, defspace_t *space)
int size;
int offset;
sy = sy_var;
sy = sy_offset;
if (type->meta == ty_enum)
sy = sy_const;
if (!type->symtab) {

View file

@ -1745,7 +1745,7 @@ rua_macro_param (rua_macro_t *macro, const rua_tok_t *token, void *scanner)
return macro;
}
auto sym = new_symbol (token->text);
sym->sy_type = sy_var;
sym->sy_type = sy_offset;
sym->offset = macro->num_params++;
symtab_addsymbol (macro->params, sym);
if (token->token == -rua_ellipsis) {

View file

@ -619,7 +619,7 @@ use_type_name (specifier_t spec)
{
spec.sym = new_symbol (spec.sym->name);
spec.sym->type = spec.type;
spec.sym->sy_type = sy_var;
spec.sym->sy_type = sy_name;
symbol_t *s = symtab_addsymbol (current_symtab, spec.sym);
// a different symbol being returned means that this is a redefinition
// of that symbol in the same scope. However, typedefs to the same type
@ -1474,7 +1474,7 @@ component_decl
$1.sym = new_symbol (va (0, ".anonymous.%s",
$1.type->name + 4));
$1.sym->type = $1.type;
$1.sym->sy_type = sy_var;
$1.sym->sy_type = sy_offset;
$1.sym->visibility = vis_anonymous;
symtab_addsymbol (current_symtab, $1.sym);
if (!$1.sym->table) {
@ -2468,7 +2468,7 @@ ivar_decl
$1.sym = new_symbol (va (0, ".anonymous.%s",
$1.type->name + 4));
$1.sym->type = $1.type;
$1.sym->sy_type = sy_var;
$1.sym->sy_type = sy_offset;
$1.sym->visibility = vis_anonymous;
symtab_addsymbol (current_symtab, $1.sym);
if (!$1.sym->table) {

View file

@ -1537,7 +1537,7 @@ statement_return (sblock_t *sblock, const expr_t *e)
auto parameters = v6p ? current_func->locals : current_func->parameters;
int ind = 0;
for (auto p = parameters->symbols; p; p = p->next, ind++) {
if (p->sy_type != sy_var || !p->def->out_param) {
if (p->sy_type != sy_def || !p->def->out_param) {
continue;
}
auto type = p->def->type;
@ -2023,7 +2023,7 @@ expr_symbol (sblock_t *sblock, const expr_t *e, operand_t **op)
{
symbol_t *sym = e->symbol;
if (sym->sy_type == sy_var) {
if (sym->sy_type == sy_def) {
*op = def_operand (sym->def, sym->type, e);
} else if (sym->sy_type == sy_const) {
*op = value_operand (sym->value, e);

View file

@ -172,7 +172,7 @@ build_struct (int su, symbol_t *tag, symtab_t *symtab, const type_t *type,
return sym;
}
for (s = symtab->symbols; s; s = s->next) {
if (s->sy_type != sy_var)
if (s->sy_type != sy_offset)
continue;
if (is_class (s->type)) {
error (0, "statically allocated instance of class %s",
@ -201,7 +201,8 @@ build_struct (int su, symbol_t *tag, symtab_t *symtab, const type_t *type,
}
anonymous = s->type->symtab;
for (as = anonymous->symbols; as; as = as->next) {
if (as->visibility == vis_anonymous || as->sy_type!= sy_var) {
if (as->visibility == vis_anonymous
|| as->sy_type != sy_offset) {
continue;
}
if (Hash_Find (symtab->tab, as->name)) {
@ -353,7 +354,7 @@ make_structure (const char *name, int su, struct_def_t *defs,
strct = new_symtab (0, stab_struct);
while (defs->name) {
field = new_symbol_type (defs->name, defs->type);
field->sy_type = sy_var;
field->sy_type = sy_offset;
if (!symtab_addsymbol (strct, field))
internal_error (0, "duplicate symbol: %s", defs->name);
defs++;

View file

@ -239,7 +239,7 @@ make_symbol (const char *name, const type_t *type, defspace_t *space,
sym->def = new_def (name, type, space, storage);
reloc_attach_relocs (relocs, &sym->def->relocs);
}
sym->sy_type = sy_var;
sym->sy_type = sy_def;
return sym;
}
@ -302,7 +302,7 @@ declare_field (specifier_t spec, symtab_t *symtab)
symbol_t *s = spec.sym;
spec = default_type (spec, s);
s->type = find_type (append_type (s->type, spec.type));
s->sy_type = sy_var;
s->sy_type = sy_offset;
s->visibility = current_visibility;
symtab_addsymbol (current_symtab, s);
if (!s->table) {

View file

@ -307,7 +307,7 @@ value_store (pr_type_t *dst, const type_t *dstType, const expr_t *src)
memset (dst, 0, dstSize);
return;
}
if (src->type == ex_symbol && src->symbol->sy_type == sy_var) {
if (src->type == ex_symbol && src->symbol->sy_type == sy_def) {
// initialized global def treated as a constant
// from the tests in cast_expr, the def is known to be constant
def_t *def = src->symbol->def;