Clean up redundant type parameter to initialize_def.

This commit is contained in:
Bill Currie 2018-10-15 22:11:20 +09:00
parent 1746ce3af5
commit ff5c1cd4c2
7 changed files with 49 additions and 62 deletions

View File

@ -237,13 +237,11 @@ void def_to_ddef (def_t *def, ddef_t *ddef, int aux);
For \a space and \a storage, see new_def().
\param sym The symbol for which to create and initialize a def.
\param type The type of the def. sym_t::type is set to this. If null,
the default type is used.
\param init If not null, the expressions to use to initialize the def.
\param space The space from which to allocate space for the def.
\param storage The storage class of the def.
*/
void initialize_def (struct symbol_s *sym, struct type_s *type,
void initialize_def (struct symbol_s *sym,
struct expr_s *init, struct defspace_s *space,
storage_class_t storage);

View File

@ -484,19 +484,14 @@ init_field_def (def_t *def, expr_t *init, storage_class_t storage)
}
void
initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
initialize_def (symbol_t *sym, expr_t *init, defspace_t *space,
storage_class_t storage)
{
symbol_t *check = symtab_lookup (current_symtab, sym->name);
reloc_t *relocs = 0;
if (!type) {
warning (0, "type for %s defaults to %s", sym->name,
type_default->name);
type = type_default;
}
if (check && check->table == current_symtab) {
if (check->sy_type != sy_var || check->type != type) {
if (check->sy_type != sy_var || check->type != sym->type) {
error (0, "%s redefined", sym->name);
} else {
// is var and same type
@ -514,7 +509,7 @@ initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
sym = check;
}
}
sym->type = type;
sym->sy_type = sy_var;
if (!sym->table)
symtab_addsymbol (current_symtab, sym);
// if (storage == sc_global && init && is_scalar (type)) {
@ -535,12 +530,13 @@ initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
sym->s.def = 0;
}
if (!sym->s.def) {
sym->s.def = new_def (sym->name, type, space, storage);
sym->s.def = new_def (sym->name, sym->type, space, storage);
reloc_attach_relocs (relocs, &sym->s.def->relocs);
}
if (type == &type_vector && options.code.vector_components)
if (sym->type == &type_vector && options.code.vector_components)
init_vector_components (sym, 0);
if (type->type == ev_field && storage != sc_local && storage != sc_param)
if (sym->type->type == ev_field && storage != sc_local
&& storage != sc_param)
init_field_def (sym->s.def, init, storage);
if (storage == sc_extern) {
if (init)
@ -553,14 +549,14 @@ initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
if (init->type == ex_error)
return;
if (init->type == ex_nil)
convert_nil (init, type);
if ((is_array (type) || is_struct (type)
|| type == &type_vector || type == &type_quaternion)
convert_nil (init, sym->type);
if ((is_array (sym->type) || is_struct (sym->type)
|| sym->type == &type_vector || sym->type == &type_quaternion)
&& init->type == ex_block && !init->e.block.result) {
init_elements (sym->s.def, init);
sym->s.def->initialized = 1;
} else {
if (!type_assignable (type, get_type (init))) {
if (!type_assignable (sym->type, get_type (init))) {
error (init, "type mismatch in initializer");
return;
}
@ -589,7 +585,7 @@ initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
v->v.string_val);
} else {
memcpy (D_POINTER (void, sym->s.def), &v->v,
type_size (type) * sizeof (pr_type_t));
type_size (sym->type) * sizeof (pr_type_t));
}
}
sym->s.def->initialized = sym->s.def->constant = 1;

View File

@ -2615,9 +2615,8 @@ super_expr (class_type_t *class_type)
sym = symtab_lookup (current_symtab, ".super");
if (!sym || sym->table != current_symtab) {
sym = new_symbol (".super");
initialize_def (sym, &type_obj_super, 0, current_symtab->space,
sc_local);
sym = new_symbol_type (".super", &type_obj_super);
initialize_def (sym, 0, current_symtab->space, sc_local);
}
super = new_symbol_expr (sym);

View File

@ -463,7 +463,7 @@ build_scope (symbol_t *fsym, symtab_t *parent)
if (fsym->type->t.func.num_params < 0) {
args = new_symbol_type (".args", &type_va_list);
initialize_def (args, args->type, 0, symtab->space, sc_param);
initialize_def (args, 0, symtab->space, sc_param);
}
for (p = fsym->params, i = 0; p; p = p->next) {
@ -476,14 +476,14 @@ build_scope (symbol_t *fsym, symtab_t *parent)
p->name = save_string ("");
}
param = new_symbol_type (p->name, p->type);
initialize_def (param, param->type, 0, symtab->space, sc_param);
initialize_def (param, 0, symtab->space, sc_param);
i++;
}
if (args) {
while (i < MAX_PARMS) {
param = new_symbol_type (va (".par%d", i), &type_param);
initialize_def (param, &type_param, 0, symtab->space, sc_param);
initialize_def (param, 0, symtab->space, sc_param);
i++;
}
}

View File

@ -252,6 +252,17 @@ spec_merge (specifier_t spec, specifier_t new)
return spec;
}
static specifier_t
default_type (specifier_t spec, symbol_t *sym)
{
if (!spec.type) {
spec.type = type_default;
warning (0, "type defaults to '%s' in declaration of '%s'",
type_default->name, sym->name);
}
return spec;
}
%}
%expect 0
@ -382,48 +393,35 @@ external_decl_list
external_decl
: var_decl
{
specifier_t spec = $<spec>0;
type_t *type;
if (!spec.type)
spec.type = type_default;
type = find_type (append_type ($1->type, spec.type));
specifier_t spec = default_type ($<spec>0, $1);
$1->type = find_type (append_type ($1->type, spec.type));
if (spec.is_typedef) {
$1->type = type;
$1->sy_type = sy_type;
symtab_addsymbol (current_symtab, $1);
} else {
initialize_def ($1, type, 0, current_symtab->space,
spec.storage);
initialize_def ($1, 0, current_symtab->space, spec.storage);
if ($1->s.def)
$1->s.def->nosave |= spec.nosave;
}
}
| var_decl var_initializer
{
specifier_t spec = $<spec>0;
type_t *type;
specifier_t spec = default_type ($<spec>0, $1);
if (!spec.type)
spec.type = type_default;
type = find_type (append_type ($1->type, spec.type));
$1->type = find_type (append_type ($1->type, spec.type));
if (spec.is_typedef) {
error (0, "typedef %s is initialized", $1->name);
$1->type = type;
$1->sy_type = sy_type;
symtab_addsymbol (current_symtab, $1);
} else {
initialize_def ($1, type, $2, current_symtab->space,
spec.storage);
initialize_def ($1, $2, current_symtab->space, spec.storage);
if ($1->s.def)
$1->s.def->nosave |= spec.nosave;
}
}
| function_decl
{
specifier_t spec = $<spec>0;
if (!spec.type)
spec.type = type_default;
specifier_t spec = default_type ($<spec>0, $1);
$1->type = find_type (append_type ($1->type, spec.type));
if (spec.is_typedef) {
$1->sy_type = sy_type;
@ -903,19 +901,16 @@ decl
: function_decl {}
| var_decl opt_initializer
{
specifier_t spec = $<spec>0;
type_t *type;
storage_class_t sc = $<spec>0.storage;
specifier_t spec = default_type ($<spec>0, $1);
storage_class_t sc = spec.storage;
struct defspace_s *space = current_symtab->space;
if (!spec.type)
spec.type = type_default;
if (sc == sc_static)
space = pr.near_data;
type = find_type (append_type ($1->type, spec.type));
initialize_def ($1, type, $2, space, sc);
$1->type = find_type (append_type ($1->type, spec.type));
initialize_def ($1, $2, space, sc);
if ($1->s.def)
$1->s.def->nosave |= $<spec>0.nosave;
$1->s.def->nosave |= spec.nosave;
}
;
@ -974,8 +969,7 @@ non_code_func
{
symbol_t *sym = $<symbol>0;
specifier_t spec = $<spec>-1;
initialize_def (sym, sym->type, $2, current_symtab->space,
spec.storage);
initialize_def (sym, $2, current_symtab->space, spec.storage);
if (sym->s.def)
sym->s.def->nosave |= spec.nosave;
}
@ -988,8 +982,7 @@ non_code_func
if (sym->sy_type == sy_func)
make_function (sym, 0, sym->table->space, spec.storage);
} else {
initialize_def (sym, sym->type, 0, current_symtab->space,
spec.storage);
initialize_def (sym, 0, current_symtab->space, spec.storage);
if (sym->s.def)
sym->s.def->nosave |= spec.nosave;
}

View File

@ -207,8 +207,8 @@ declarations
{
while ($3) {
symbol_t *next = $3->next;
initialize_def ($3, $5, 0, current_symtab->space,
current_storage);
$3->type = $5;
initialize_def ($3, 0, current_symtab->space, current_storage);
$3 = next;
}
}

View File

@ -345,9 +345,10 @@ build_switch (expr_t *sw, case_node_t *tree, int op, expr_t *sw_val,
tree->labels[i]->e.label.used++;
append_expr (table_init, address_expr (tree->labels[i], 0, 0));
}
table_sym = new_symbol (table_name);
initialize_def (table_sym, array_type (&type_integer, high - low + 1),
table_init, pr.near_data, sc_static);
table_sym = new_symbol_type (table_name,
array_type (&type_integer,
high - low + 1));
initialize_def (table_sym, table_init, pr.near_data, sc_static);
table_expr = new_symbol_expr (table_sym);
if (tree->left) {