1986-03-26 15:11:02 +00:00
|
|
|
/* S C O P E M E C H A N I S M */
|
|
|
|
|
|
|
|
static char *RcsId = "$Header$";
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <alloc.h>
|
1986-03-27 17:37:41 +00:00
|
|
|
#include <em_arith.h>
|
|
|
|
#include <em_label.h>
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "idf.h"
|
1986-03-26 15:11:02 +00:00
|
|
|
#include "scope.h"
|
1986-03-27 17:37:41 +00:00
|
|
|
#include "type.h"
|
|
|
|
#include "def.h"
|
1986-03-26 17:53:13 +00:00
|
|
|
#include "debug.h"
|
1986-03-26 15:11:02 +00:00
|
|
|
|
|
|
|
static int maxscope; /* maximum assigned scope number */
|
|
|
|
|
1986-04-03 00:44:39 +00:00
|
|
|
struct scope *CurrentScope, *GlobalScope;
|
1986-03-26 15:11:02 +00:00
|
|
|
|
|
|
|
/* STATICALLOCDEF "scope" */
|
|
|
|
|
1986-04-03 00:44:39 +00:00
|
|
|
open_scope(scopetype, scope)
|
1986-03-26 15:11:02 +00:00
|
|
|
{
|
1986-04-03 00:44:39 +00:00
|
|
|
/* Open a scope that is either open (automatic imports) or closed.
|
|
|
|
A closed scope is handled by adding an extra entry to the list
|
|
|
|
with scope number 0. This has two purposes: it makes scope 0
|
|
|
|
visible, and it marks the end of a visibility list.
|
|
|
|
Scope 0 is the pervasive scope, the one that is always visible.
|
|
|
|
A disadvantage of this method is that we cannot open scope 0
|
|
|
|
explicitly.
|
|
|
|
*/
|
1986-03-26 15:11:02 +00:00
|
|
|
register struct scope *sc = new_scope();
|
|
|
|
register struct scope *sc1;
|
|
|
|
|
1986-04-03 00:44:39 +00:00
|
|
|
sc->sc_scope = scope == 0 ? ++maxscope : scope;
|
|
|
|
sc->sc_forw = 0; sc->sc_def = 0;
|
1986-03-26 15:11:02 +00:00
|
|
|
assert(scopetype == OPENSCOPE || scopetype == CLOSEDSCOPE);
|
1986-04-03 00:44:39 +00:00
|
|
|
DO_DEBUG(debug(1, "Opening a %s scope",
|
|
|
|
scopetype == OPENSCOPE ? "open" : "closed"));
|
|
|
|
sc1 = CurrentScope;
|
1986-03-26 15:11:02 +00:00
|
|
|
if (scopetype == CLOSEDSCOPE) {
|
|
|
|
sc1 = new_scope();
|
1986-03-27 17:37:41 +00:00
|
|
|
sc1->sc_scope = 0; /* Pervasive scope nr */
|
1986-04-03 00:44:39 +00:00
|
|
|
sc1->sc_forw = 0; sc1->sc_def = 0;
|
|
|
|
sc1->next = CurrentScope;
|
1986-03-26 15:11:02 +00:00
|
|
|
}
|
|
|
|
sc->next = sc1;
|
1986-04-03 00:44:39 +00:00
|
|
|
CurrentScope = sc;
|
1986-03-26 15:11:02 +00:00
|
|
|
}
|
|
|
|
|
1986-03-27 17:37:41 +00:00
|
|
|
static rem_forwards();
|
|
|
|
|
1986-03-26 15:11:02 +00:00
|
|
|
close_scope()
|
|
|
|
{
|
1986-04-03 00:44:39 +00:00
|
|
|
register struct scope *sc = CurrentScope;
|
1986-03-26 15:11:02 +00:00
|
|
|
|
|
|
|
assert(sc != 0);
|
1986-03-26 17:53:13 +00:00
|
|
|
DO_DEBUG(debug(1, "Closing a scope"));
|
1986-03-27 17:37:41 +00:00
|
|
|
if (sc->sc_forw) rem_forwards(sc->sc_forw);
|
1986-03-26 15:11:02 +00:00
|
|
|
if (sc->next && (sc->next->sc_scope == 0)) {
|
|
|
|
struct scope *sc1 = sc;
|
|
|
|
|
|
|
|
sc = sc->next;
|
|
|
|
free_scope(sc1);
|
|
|
|
}
|
1986-04-03 00:44:39 +00:00
|
|
|
CurrentScope = sc->next;
|
1986-03-26 15:11:02 +00:00
|
|
|
free_scope(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
init_scope()
|
|
|
|
{
|
|
|
|
register struct scope *sc = new_scope();
|
|
|
|
|
|
|
|
sc->sc_scope = 0;
|
1986-04-03 00:44:39 +00:00
|
|
|
sc->sc_forw = 0;
|
|
|
|
sc->sc_def = 0;
|
|
|
|
CurrentScope = sc;
|
1986-03-27 17:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
uniq_scope()
|
|
|
|
{
|
|
|
|
return ++maxscope;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct forwards {
|
|
|
|
struct forwards *next;
|
|
|
|
struct token fo_tok;
|
|
|
|
struct type **fo_ptyp;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* STATICALLOCDEF "forwards" */
|
|
|
|
|
|
|
|
Forward(tk, ptp)
|
|
|
|
struct token *tk;
|
|
|
|
struct type **ptp;
|
|
|
|
{
|
1986-04-03 00:44:39 +00:00
|
|
|
/* Enter a forward reference into a list belonging to the
|
|
|
|
current scope. This is used for POINTER declarations, which
|
|
|
|
may have forward references that must howewer be declared in the
|
|
|
|
same scope.
|
|
|
|
*/
|
1986-03-27 17:37:41 +00:00
|
|
|
register struct forwards *f = new_forwards();
|
|
|
|
|
|
|
|
f->fo_tok = *tk;
|
|
|
|
f->fo_ptyp = ptp;
|
1986-04-03 00:44:39 +00:00
|
|
|
f->next = CurrentScope->sc_forw;
|
|
|
|
CurrentScope->sc_forw = f;
|
1986-03-27 17:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
rem_forwards(fo)
|
|
|
|
struct forwards *fo;
|
|
|
|
{
|
1986-04-03 00:44:39 +00:00
|
|
|
/* When closing a scope, all forward references must be resolved
|
|
|
|
*/
|
1986-03-27 17:37:41 +00:00
|
|
|
register struct forwards *f;
|
|
|
|
struct token savetok;
|
|
|
|
register struct def *df;
|
|
|
|
struct def *lookfor();
|
|
|
|
|
|
|
|
savetok = dot;
|
|
|
|
while (f = fo) {
|
|
|
|
dot = f->fo_tok;
|
1986-04-03 00:44:39 +00:00
|
|
|
df = lookfor(dot.TOK_IDF, CurrentScope, 1);
|
1986-03-27 17:37:41 +00:00
|
|
|
if (!(df->df_kind & (D_TYPE | D_HTYPE | D_ERROR))) {
|
1986-04-03 00:44:39 +00:00
|
|
|
error("identifier \"%s\" not a type",
|
|
|
|
df->df_idf->id_text);
|
1986-03-27 17:37:41 +00:00
|
|
|
}
|
|
|
|
*(f->fo_ptyp) = df->df_type;
|
|
|
|
fo = f->next;
|
|
|
|
free_forwards(f);
|
|
|
|
}
|
|
|
|
dot = savetok;
|
1986-03-26 15:11:02 +00:00
|
|
|
}
|