1987-04-29 10:22:07 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*
|
|
|
|
* Author: Ceriel J.H. Jacobs
|
|
|
|
*/
|
|
|
|
|
1986-06-10 13:18:52 +00:00
|
|
|
/* L O O K U P R O U T I N E S */
|
|
|
|
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1987-04-29 10:22:07 +00:00
|
|
|
|
2019-03-01 17:39:25 +00:00
|
|
|
#include "parameters.h"
|
1986-06-10 13:18:52 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#include <em_arith.h>
|
|
|
|
#include <em_label.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2019-03-01 17:39:25 +00:00
|
|
|
#include "lookup.h"
|
1987-07-30 13:37:39 +00:00
|
|
|
#include "LLlex.h"
|
1986-06-10 13:18:52 +00:00
|
|
|
#include "def.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "scope.h"
|
|
|
|
#include "node.h"
|
1986-09-25 19:39:06 +00:00
|
|
|
#include "type.h"
|
1986-10-22 15:38:24 +00:00
|
|
|
#include "misc.h"
|
1986-06-10 13:18:52 +00:00
|
|
|
|
1987-10-30 09:19:23 +00:00
|
|
|
extern int pass_1;
|
1988-09-20 13:31:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
extern char options[];
|
|
|
|
#endif
|
1987-10-30 09:19:23 +00:00
|
|
|
|
2019-03-01 17:39:25 +00:00
|
|
|
t_def *lookup(register t_idf *id, t_scope *scope, int import, int flags)
|
1986-06-10 13:18:52 +00:00
|
|
|
{
|
|
|
|
/* Look up a definition of an identifier in scope "scope".
|
|
|
|
Make the "def" list self-organizing.
|
|
|
|
Return a pointer to its "def" structure if it exists,
|
|
|
|
otherwise return 0.
|
|
|
|
*/
|
1987-09-23 16:39:43 +00:00
|
|
|
register t_def *df, *df1;
|
1986-06-20 14:36:49 +00:00
|
|
|
|
|
|
|
/* 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;
|
1987-07-16 19:51:40 +00:00
|
|
|
df1 = df, df = df->df_next) { /* nothing */ }
|
1986-06-10 13:18:52 +00:00
|
|
|
|
1987-11-11 13:10:08 +00:00
|
|
|
if (! df && import && scopeclosed(scope)) {
|
|
|
|
for (df = id->id_def, df1 = 0;
|
|
|
|
df && df->df_scope != PervasiveScope;
|
|
|
|
df1 = df, df = df->df_next) { /* nothing */ }
|
|
|
|
}
|
|
|
|
|
1986-06-20 14:36:49 +00:00
|
|
|
if (df) {
|
|
|
|
/* Found it
|
|
|
|
*/
|
|
|
|
if (df1) {
|
|
|
|
/* Put the definition in front
|
|
|
|
*/
|
1987-07-16 19:51:40 +00:00
|
|
|
df1->df_next = df->df_next;
|
|
|
|
df->df_next = id->id_def;
|
1986-06-20 14:36:49 +00:00
|
|
|
id->id_def = df;
|
|
|
|
}
|
1987-10-21 11:29:52 +00:00
|
|
|
df->df_flags |= flags;
|
1987-11-11 13:10:08 +00:00
|
|
|
while (df->df_kind & import) {
|
|
|
|
assert(df->imp_def != 0);
|
|
|
|
df = df->imp_def;
|
1986-06-10 13:18:52 +00:00
|
|
|
}
|
1988-09-20 13:31:57 +00:00
|
|
|
DO_DEBUG(options['S'], print("lookup %s, %x\n", id->id_text, df->df_kind));
|
1986-06-10 13:18:52 +00:00
|
|
|
}
|
1986-06-20 14:36:49 +00:00
|
|
|
return df;
|
1986-06-10 13:18:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 17:39:25 +00:00
|
|
|
t_def *lookfor(register t_node *id, register t_scopelist *vis, int message, int flags)
|
1986-06-10 13:18:52 +00:00
|
|
|
{
|
|
|
|
/* Look for an identifier in the visibility range started by "vis".
|
|
|
|
If it is not defined create a dummy definition and,
|
1987-10-21 11:29:52 +00:00
|
|
|
if message is set, give an error message
|
1986-06-10 13:18:52 +00:00
|
|
|
*/
|
1988-09-20 13:31:57 +00:00
|
|
|
register t_scopelist *sc;
|
|
|
|
t_scopelist *sc1 = 0;
|
1987-10-30 09:19:23 +00:00
|
|
|
t_def *df;
|
1986-06-10 13:18:52 +00:00
|
|
|
|
1988-09-20 13:31:57 +00:00
|
|
|
for (sc = vis; sc; sc = nextvisible(sc)) {
|
1987-11-11 13:10:08 +00:00
|
|
|
df = lookup(id->nd_IDF, sc->sc_scope, D_IMPORTED, flags);
|
1987-10-30 09:19:23 +00:00
|
|
|
if (df) {
|
1988-09-20 13:31:57 +00:00
|
|
|
if (message && df->df_kind == D_FORWARD) {
|
|
|
|
if (! sc1) sc1 = sc;
|
|
|
|
while (sc && sc->sc_scope != df->df_scope) {
|
|
|
|
sc = enclosing(sc);
|
|
|
|
}
|
|
|
|
if (sc) continue;
|
|
|
|
break;
|
|
|
|
}
|
1987-11-11 13:10:08 +00:00
|
|
|
if (pass_1 && message) {
|
1988-09-20 13:31:57 +00:00
|
|
|
if (sc1) sc = sc1;
|
1987-11-11 13:10:08 +00:00
|
|
|
while (vis->sc_scope->sc_level >
|
1988-09-20 13:31:57 +00:00
|
|
|
sc->sc_scope->sc_level ||
|
|
|
|
(sc1 &&
|
|
|
|
vis->sc_scope->sc_level >=
|
|
|
|
sc->sc_scope->sc_level)) {
|
1987-11-11 13:10:08 +00:00
|
|
|
define( id->nd_IDF,
|
|
|
|
vis->sc_scope,
|
|
|
|
D_INUSE)-> imp_def = df;
|
1988-09-20 13:31:57 +00:00
|
|
|
vis = enclosing(vis);
|
1987-11-11 13:10:08 +00:00
|
|
|
}
|
1987-10-30 09:19:23 +00:00
|
|
|
}
|
|
|
|
return df;
|
|
|
|
}
|
1986-06-10 13:18:52 +00:00
|
|
|
}
|
|
|
|
|
1987-10-21 11:29:52 +00:00
|
|
|
if (message) id_not_declared(id);
|
1986-06-10 13:18:52 +00:00
|
|
|
|
1987-10-30 09:19:23 +00:00
|
|
|
return MkDef(id->nd_IDF, vis->sc_scope, D_ERROR);
|
1986-06-10 13:18:52 +00:00
|
|
|
}
|