From 5cd95d47b212c10c56b6c51172fe707fdb1f82c1 Mon Sep 17 00:00:00 2001 From: ceriel Date: Fri, 30 Oct 1987 09:19:23 +0000 Subject: [PATCH] Now detects use before declaration, f.i. in VAR k: REAL; REAL : INTEGER; --- lang/m2/comp/Version.c | 2 +- lang/m2/comp/def.H | 4 +++- lang/m2/comp/def.c | 10 ++++++++-- lang/m2/comp/lookup.c | 21 ++++++++++++++++----- lang/m2/comp/scope.C | 2 +- lang/m2/comp/walk.c | 4 ++-- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lang/m2/comp/Version.c b/lang/m2/comp/Version.c index ff5e11d27..1fa3d2ab1 100644 --- a/lang/m2/comp/Version.c +++ b/lang/m2/comp/Version.c @@ -1 +1 @@ -static char Version[] = "ACK Modula-2 compiler Version 0.24"; +static char Version[] = "ACK Modula-2 compiler Version 0.25"; diff --git a/lang/m2/comp/def.H b/lang/m2/comp/def.H index abe0901c9..b9c49038f 100644 --- a/lang/m2/comp/def.H +++ b/lang/m2/comp/def.H @@ -98,9 +98,11 @@ struct def { /* list of definitions for a name */ #define D_ERROR 0x4000 /* a compiler generated definition for an undefined variable */ +#define D_INUSE 0x8000 /* identification in this scope (like D_IMPORT) + */ #define D_VALUE (D_PROCEDURE|D_VARIABLE|D_FIELD|D_ENUM|D_CONST|D_PROCHEAD) #define D_ISTYPE (D_HIDDEN|D_TYPE|D_FTYPE) -#define D_IMPORTED (D_IMPORT) +#define D_IMPORTED (D_IMPORT|D_INUSE) #define is_type(dfx) ((dfx)->df_kind & D_ISTYPE) unsigned short df_flags; #define D_NOREG 0x01 /* set if it may not reside in a register */ diff --git a/lang/m2/comp/def.c b/lang/m2/comp/def.c index 613af6bab..1c3e633cd 100644 --- a/lang/m2/comp/def.c +++ b/lang/m2/comp/def.c @@ -96,16 +96,22 @@ define(id, scope, kind) */ register t_def *df; - df = lookup(id, scope, 1, 0); + df = lookup(id, scope, 2, 0); if ( /* Already in this scope */ df || /* A closed scope, and id defined in the pervasive scope */ ( scopeclosed(scope) && - (df = lookup(id, PervasiveScope, 1, 0))) + (df = lookup(id, PervasiveScope, 2, 0))) ) { switch(df->df_kind) { + case D_INUSE: + if (kind != D_INUSE) { + error("identifier \"%s\" already used; may not be redefined in this scope", df->df_idf->id_text); + } + return df; + case D_HIDDEN: /* An opaque type. We may now have found the definition of this type. diff --git a/lang/m2/comp/lookup.c b/lang/m2/comp/lookup.c index 6656ea1c4..8375aacb4 100644 --- a/lang/m2/comp/lookup.c +++ b/lang/m2/comp/lookup.c @@ -23,6 +23,8 @@ #include "type.h" #include "misc.h" +extern int pass_1; + t_def * lookup(id, scope, import, flags) register t_idf *id; @@ -55,6 +57,7 @@ lookup(id, scope, import, flags) df->df_flags |= flags; if (import) { while (df->df_kind & D_IMPORTED) { + if (df->df_kind == D_INUSE && import != 1) break; assert(df->imp_def != 0); df = df->imp_def; } @@ -66,23 +69,31 @@ lookup(id, scope, import, flags) t_def * lookfor(id, vis, message, flags) register t_node *id; - t_scopelist *vis; + register t_scopelist *vis; { /* Look for an identifier in the visibility range started by "vis". If it is not defined create a dummy definition and, if message is set, give an error message */ - register t_def *df; register t_scopelist *sc = vis; + t_def *df; while (sc) { df = lookup(id->nd_IDF, sc->sc_scope, 1, flags); - if (df) return df; + if (df) { + if (pass_1 && + message && + sc->sc_scope->sc_level < vis->sc_scope->sc_level && + ! scopeclosed(vis->sc_scope)) { + define(id->nd_IDF, vis->sc_scope, D_INUSE)-> + imp_def = df; + } + return df; + } sc = nextvisible(sc); } if (message) id_not_declared(id); - df = MkDef(id->nd_IDF, vis->sc_scope, D_ERROR); - return df; + return MkDef(id->nd_IDF, vis->sc_scope, D_ERROR); } diff --git a/lang/m2/comp/scope.C b/lang/m2/comp/scope.C index 70eb52d8f..4e92c4049 100644 --- a/lang/m2/comp/scope.C +++ b/lang/m2/comp/scope.C @@ -188,7 +188,7 @@ Reverse(pdf) from this list. */ register t_def *df, *df1; -#define INTERESTING D_MODULE|D_PROCEDURE|D_PROCHEAD|D_VARIABLE +#define INTERESTING (D_MODULE|D_PROCEDURE|D_PROCHEAD|D_VARIABLE|D_IMPORTED) df = 0; df1 = *pdf; diff --git a/lang/m2/comp/walk.c b/lang/m2/comp/walk.c index 49e8e3cbc..46b3c68ce 100644 --- a/lang/m2/comp/walk.c +++ b/lang/m2/comp/walk.c @@ -860,8 +860,8 @@ UseWarnings(df) register t_def *df1 = df->imp_def; df1->df_flags |= df->df_flags & (D_USED|D_DEFINED); - if (df->df_kind == D_IMPORT && - !(df->df_flags & D_IMP_BY_EXP)) { + if (df->df_kind == D_INUSE) return; + if ( !(df->df_flags & D_IMP_BY_EXP)) { if (! (df->df_flags & (D_USED | D_DEFINED))) { node_warning( df->df_scope->sc_end,