From 58782333a94381f5ed781adc59472bc3940235e8 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 9 Jun 2002 05:19:13 +0000 Subject: [PATCH] give scopes types (static, params, locals for now) --- tools/qfcc/include/def.h | 9 ++++++++- tools/qfcc/source/def.c | 3 ++- tools/qfcc/source/expr.c | 3 ++- tools/qfcc/source/function.c | 2 +- tools/qfcc/source/qc-parse.y | 29 ++++++++++++++--------------- tools/qfcc/source/qfcc.c | 6 +++--- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/tools/qfcc/include/def.h b/tools/qfcc/include/def.h index 7f748e0bd..ac6085a24 100644 --- a/tools/qfcc/include/def.h +++ b/tools/qfcc/include/def.h @@ -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, diff --git a/tools/qfcc/source/def.c b/tools/qfcc/source/def.c index 9526b1a24..1d5de10d6 100644 --- a/tools/qfcc/source/def.c +++ b/tools/qfcc/source/def.c @@ -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; diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index bbb2097ed..f35a898f8 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -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 diff --git a/tools/qfcc/source/function.c b/tools/qfcc/source/function.c index e9825084b..9f14b21fb 100644 --- a/tools/qfcc/source/function.c +++ b/tools/qfcc/source/function.c @@ -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); diff --git a/tools/qfcc/source/qc-parse.y b/tools/qfcc/source/qc-parse.y index 02344823c..b8bd1f0cd 100644 --- a/tools/qfcc/source/qc-parse.y +++ b/tools/qfcc/source/qc-parse.y @@ -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) { diff --git a/tools/qfcc/source/qfcc.c b/tools/qfcc/source/qfcc.c index 71a6eba00..9b0b3f525 100644 --- a/tools/qfcc/source/qfcc.c +++ b/tools/qfcc/source/qfcc.c @@ -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);