ack/lang/pc/comp/lookup.c

101 lines
2.1 KiB
C
Raw Normal View History

1988-10-26 15:21:11 +00:00
/* L O O K U P R O U T I N E S */
1989-05-03 10:30:22 +00:00
#include <alloc.h>
1988-10-26 15:21:11 +00:00
#include <em_arith.h>
#include <em_label.h>
1989-05-03 10:30:22 +00:00
#include <assert.h>
1988-10-26 15:21:11 +00:00
#include "parameters.h"
1988-10-26 15:21:11 +00:00
#include "LLlex.h"
#include "def.h"
#include "idf.h"
#include "misc.h"
#include "node.h"
#include "scope.h"
#include "type.h"
#include "lookup.h"
1988-10-26 15:21:11 +00:00
void remove_def(register struct def *df)
1989-05-03 10:30:22 +00:00
{
struct idf *id= df->df_idf;
struct def *df1 = id->id_def;
if( df1 == df ) id->id_def = df->df_next;
else {
while( df1 && df1->df_next != df ) df1 = df1->df_next;
df1->df_next = df->df_next;
}
free_def(df);
1989-05-03 10:30:22 +00:00
}
struct def *lookup(register struct idf *id, struct scope *scope, long inuse)
1988-10-26 15:21:11 +00:00
{
1988-10-26 15:21:11 +00:00
register struct def *df, *df1;
/* Look in the chain of definitions of this "id" for one with scope
"scope".
*/
for( df = id->id_def, df1 = 0;
df && df->df_scope != scope;
df1 = df, df = df->df_next ) { /* nothing */ }
1989-05-03 10:30:22 +00:00
if( df ) {
/* Found it
1988-10-26 15:21:11 +00:00
*/
1989-05-03 10:30:22 +00:00
if( df1) {
/* Put the definition in front
*/
df1->df_next = df->df_next;
df->df_next = id->id_def;
id->id_def = df;
}
while( df->df_kind & inuse ) {
assert(df->usd_def != 0);
df=df->usd_def;
}
1988-10-26 15:21:11 +00:00
}
1989-05-03 10:30:22 +00:00
1988-10-26 15:21:11 +00:00
return df;
}
struct def *lookfor(register struct node *id, struct scopelist *vis, int give_error)
1988-10-26 15:21:11 +00:00
{
1989-05-03 10:30:22 +00:00
register struct def *df, *tmp_df;
1988-10-26 15:21:11 +00:00
register struct scopelist *sc = vis;
while( sc ) {
1989-05-03 10:30:22 +00:00
df = lookup(id->nd_IDF, sc->sc_scope, D_INUSE);
if( df ) {
while( vis->sc_scope->sc_level >
sc->sc_scope->sc_level ) {
if( (tmp_df = define(id->nd_IDF, vis->sc_scope,
D_INUSE)) )
1989-05-03 10:30:22 +00:00
tmp_df->usd_def = df;
vis = nextvisible(vis);
}
/* Since the scope-level of standard procedures is the
* same as for the user-defined procedures, the procedure
* must be marked as used. Not doing so would mean that
* such a procedure could redefined after usage.
*/
if( (vis->sc_scope == GlobalScope) &&
!lookup(id->nd_IDF, GlobalScope, D_INUSE) ) {
if( (tmp_df = define(id->nd_IDF, vis->sc_scope,
D_INUSE)) )
1989-05-03 10:30:22 +00:00
tmp_df->usd_def = df;
}
return df;
}
1988-10-26 15:21:11 +00:00
sc = nextvisible(sc);
}
if( give_error ) id_not_declared(id);
df = MkDef(id->nd_IDF, vis->sc_scope, D_ERROR);
return df;
}