give scopes types (static, params, locals for now)

This commit is contained in:
Bill Currie 2002-06-09 05:19:13 +00:00
parent 1e57351be1
commit 58782333a9
6 changed files with 30 additions and 22 deletions

View file

@ -72,8 +72,15 @@ typedef struct defspace_s {
int (*grow) (struct defspace_s *space);
} defspace_t;
typedef enum {
sc_static,
sc_params,
sc_local,
} scope_type;
typedef struct scope_s {
struct scope_s *next;
scope_type type;
defspace_t *space;
def_t *head;
def_t **tail;
@ -85,7 +92,7 @@ extern def_t def_ret, def_parms[MAX_PARMS];
extern def_t def_void;
extern def_t def_function;
scope_t *new_scope (defspace_t *space, scope_t *parent);
scope_t *new_scope (scope_type type, defspace_t *space, scope_t *parent);
defspace_t *new_defspace (void);
def_t *get_def (struct type_s *type, const char *name, scope_t *scope,

View file

@ -116,11 +116,12 @@ new_defspace (void)
}
scope_t *
new_scope (defspace_t *space, scope_t *parent)
new_scope (scope_type type, defspace_t *space, scope_t *parent)
{
scope_t *scope;
ALLOC (1024, scope_t, scopes, scope);
scope->type = type;
scope->space = space;
scope->parent = parent;
scope->tail = &scope->head;

View file

@ -326,7 +326,8 @@ check_initialized (expr_t *e)
{
if (options.warnings.uninited_variable) {
if (e->type == ex_def
&& !(e->e.def->type->type == ev_func && !e->e.def->scope->parent)
&& !(e->e.def->type->type == ev_func
&& e->e.def->scope->type == sc_static)
&& !e->e.def->initialized) {
warning (e, "%s may be used uninitialized", e->e.def->name);
e->e.def->initialized = 1; // only warn once

View file

@ -123,7 +123,7 @@ build_scope (function_t *f, def_t *func, param_t *params)
param_t *p;
def_t *argv = 0;
f->scope = new_scope (new_defspace (), pr.scope);
f->scope = new_scope (sc_params, new_defspace (), pr.scope);
if (func->type->num_parms < 0) {
def = get_def (&type_integer, ".argc", f->scope, 1);

View file

@ -340,7 +340,8 @@ def_item
: def_name opt_initializer
{
$$ = $1;
if ($$ && !$$->scope->parent && $$->type->type != ev_func)
if ($$ && $$->scope->type == sc_static
&& $$->type->type != ev_func)
def_initialized ($$);
}
;
@ -348,17 +349,14 @@ def_item
def_name
: NAME
{
if (current_scope->parent) {
scope_t *scope = current_scope->parent;
if (!scope->parent && scope->parent->parent) {
def_t *def = get_def (0, $1, scope, 0);
if (def) {
scope = def->scope;
if (scope->parent && !scope->parent->parent) {
warning (0, "local %s shadows param %s", $1,
def->name);
}
}
if (current_scope->type == sc_local
&& current_scope->parent->type == sc_params) {
scope_t *scope;
def_t *def = get_def (0, $1, scope, 0);
if (def) {
scope = def->scope;
if (scope->type == sc_params)
warning (0, "local %s shadows param %s", $1, def->name);
}
}
$$ = get_def (current_type, $1, current_scope, 1);
@ -375,7 +373,7 @@ opt_initializer
var_initializer
: '=' expr
{
if (current_scope->parent) {
if (current_scope->type == sc_local) {
append_expr (local_expr,
assign_expr (new_def_expr (current_def), $2));
def_initialized (current_def);
@ -505,7 +503,8 @@ end_function
statement_block
: '{'
{
scope_t *scope = new_scope (current_scope->space, current_scope);
scope_t *scope = new_scope (sc_local, current_scope->space,
current_scope);
current_scope = scope;
}
statements '}'
@ -1332,7 +1331,7 @@ static void
scan_scope (hashtab_t *tab, scope_t *scope)
{
def_t *def;
if (scope->parent && scope->parent->parent)
if (scope->type == sc_local)
scan_scope (tab, scope->parent);
for (def = scope->head; def; def = def->def_next) {
if (def->name && !def->removed) {

View file

@ -110,7 +110,7 @@ InitData (void)
pr.globals = new_defspace ();
pr.globals->data = calloc (65536, sizeof (pr_type_t));
pr.scope = new_scope (pr.globals, 0);
pr.scope = new_scope (sc_static, pr.globals, 0);
current_scope = pr.scope;
numglobaldefs = 1;
@ -136,7 +136,7 @@ WriteData (int crc)
fields = calloc (pr.scope->num_defs, sizeof (ddef_t));
for (def = pr.scope->head; def; def = def->def_next) {
if (def->scope->parent)
if (def->scope->type != sc_static || !def->name)
continue;
if (def->type->type == ev_func) {
} else if (def->type->type == ev_field) {
@ -165,7 +165,7 @@ WriteData (int crc)
printf ("%6i strofs\n", pr.strofs);
printf ("%6i statements\n", pr.num_statements);
printf ("%6i functions\n", pr.num_functions);
printf ("%6i global defs\n", pr.scope->num_defs);
printf ("%6i global defs\n", numglobaldefs);
printf ("%6i locals size (%s)\n", num_localdefs, big_function);
printf ("%6i fielddefs\n", numfielddefs);
printf ("%6i globals\n", pr.globals->size);