ack/lang/m2/comp/def.c

280 lines
6.4 KiB
C
Raw Normal View History

1986-03-26 15:11:02 +00:00
/* D E F I N I T I O N M E C H A N I S M */
static char *RcsId = "$Header$";
#include <alloc.h>
#include <em_arith.h>
#include <em_label.h>
1986-03-29 01:04:49 +00:00
#include <assert.h>
1986-03-26 15:11:02 +00:00
#include "Lpars.h"
#include "def.h"
1986-04-02 17:34:21 +00:00
#include "type.h"
1986-03-26 15:11:02 +00:00
#include "idf.h"
#include "main.h"
#include "scope.h"
1986-04-06 17:42:56 +00:00
#include "LLlex.h"
#include "node.h"
1986-03-26 22:46:48 +00:00
#include "debug.h"
1986-03-26 15:11:02 +00:00
struct def *h_def; /* Pointer to free list of def structures */
1986-03-27 17:37:41 +00:00
static struct def illegal_def =
1986-03-29 01:04:49 +00:00
{0, 0, 0, -20 /* Illegal scope */, D_ERROR};
1986-03-27 17:37:41 +00:00
struct def *ill_df = &illegal_def;
1986-03-26 15:11:02 +00:00
struct def *
define(id, scope, kind)
register struct idf *id;
1986-04-03 00:44:39 +00:00
struct scope *scope;
1986-03-26 15:11:02 +00:00
{
/* Declare an identifier in a scope, but first check if it
already has been defined. If so, error message.
*/
1986-03-27 17:37:41 +00:00
register struct def *df;
1986-03-29 01:04:49 +00:00
register struct scope *sc;
1986-03-26 15:11:02 +00:00
1986-04-03 00:44:39 +00:00
DO_DEBUG(debug(4,"Defining identifier %s in scope %d", id->id_text, scope->sc_scope));
df = lookup(id, scope->sc_scope);
1986-03-26 17:53:13 +00:00
if ( /* Already in this scope */
df
|| /* A closed scope, and id defined in the pervasive scope */
1986-03-27 17:37:41 +00:00
( CurrentScope == scope
&&
1986-04-03 00:44:39 +00:00
scopeclosed(CurrentScope)
1986-03-27 17:37:41 +00:00
&&
(df = lookup(id, 0)))
1986-03-26 17:53:13 +00:00
) {
1986-03-26 15:11:02 +00:00
switch(df->df_kind) {
case D_PROCHEAD:
if (kind == D_PROCEDURE) {
df->df_kind = D_PROCEDURE;
return df;
}
break;
case D_HIDDEN:
1986-04-03 17:41:26 +00:00
if (kind == D_TYPE && !DefinitionModule) {
1986-03-26 15:11:02 +00:00
df->df_kind = D_HTYPE;
return df;
}
break;
1986-03-27 17:37:41 +00:00
case D_ERROR:
1986-03-26 15:11:02 +00:00
case D_ISEXPORTED:
df->df_kind = kind;
return df;
}
1986-04-02 17:34:21 +00:00
if (kind != D_ERROR) {
1986-04-06 17:42:56 +00:00
error("identifier \"%s\" already declared", id->id_text);
1986-04-02 17:34:21 +00:00
}
1986-03-26 15:11:02 +00:00
return df;
}
df = new_def();
df->df_idf = id;
1986-04-03 00:44:39 +00:00
df->df_scope = scope->sc_scope;
1986-03-26 15:11:02 +00:00
df->df_kind = kind;
df->next = id->id_def;
id->id_def = df;
1986-03-29 01:04:49 +00:00
/* enter the definition in the list of definitions in this scope */
1986-04-03 00:44:39 +00:00
df->df_nextinscope = scope->sc_def;
scope->sc_def = df;
1986-03-26 15:11:02 +00:00
return df;
}
struct def *
lookup(id, scope)
register struct idf *id;
{
/* 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.
*/
register struct def *df, *df1;
df1 = 0;
df = id->id_def;
1986-03-27 17:37:41 +00:00
DO_DEBUG(debug(4,"Looking for identifier %s in scope %d", id->id_text, scope));
1986-03-26 15:11:02 +00:00
while (df) {
1986-03-26 17:53:13 +00:00
if (df->df_scope == scope) {
1986-03-29 01:04:49 +00:00
if (df->df_kind == D_IMPORT) {
1986-04-02 17:34:21 +00:00
df = df->imp_def;
1986-03-29 01:04:49 +00:00
assert(df != 0);
return df;
}
1986-03-26 15:11:02 +00:00
if (df1) {
df1->next = df->next;
df->next = id->id_def;
id->id_def = df;
}
return df;
}
1986-03-26 22:46:48 +00:00
df1 = df;
1986-03-26 15:11:02 +00:00
df = df->next;
}
return 0;
}
1986-03-29 01:04:49 +00:00
Export(ids, qualified)
1986-04-06 17:42:56 +00:00
register struct node *ids;
1986-03-29 01:04:49 +00:00
{
1986-04-02 17:34:21 +00:00
/* From the current scope, the list of identifiers "ids" is
exported. Note this fact. If the export is not qualified, make
all the "ids" visible in the enclosing scope by defining them
in this scope as "imported".
*/
1986-03-29 01:04:49 +00:00
register struct def *df;
while (ids) {
1986-04-06 17:42:56 +00:00
df = define(ids->nd_IDF, CurrentScope, D_ISEXPORTED);
1986-03-29 01:04:49 +00:00
if (qualified) {
df->df_flags |= D_QEXPORTED;
}
else {
df->df_flags |= D_EXPORTED;
1986-04-06 17:42:56 +00:00
df = define(ids->nd_IDF, enclosing(CurrentScope),
1986-03-29 01:04:49 +00:00
D_IMPORT);
}
ids = ids->next;
}
}
1986-04-06 17:42:56 +00:00
Import(ids, idn, local)
register struct node *ids;
struct node *idn;
1986-03-29 01:04:49 +00:00
{
1986-04-02 17:34:21 +00:00
/* "ids" is a list of imported identifiers.
1986-04-06 17:42:56 +00:00
If "idn" is a null-pointer, the identifiers are imported from
the enclosing scope. Otherwise they are imported from the module
indicated by "idn", which must be visible in the enclosing
scope. An exception must be made for imports of the
Compilation Unit.
1986-04-02 17:34:21 +00:00
This case is indicated by the value 0 of the flag "local".
1986-04-06 17:42:56 +00:00
In this case, if "idn" is a null pointer, the "ids" identifiers
1986-04-02 17:34:21 +00:00
are all module identifiers. Their Definition Modules must be
1986-04-06 17:42:56 +00:00
read. Otherwise "idn" is a module identifier whose Definition
1986-04-02 17:34:21 +00:00
Module must be read. "ids" then represents a list of
identifiers defined in this module.
*/
1986-03-29 01:04:49 +00:00
register struct def *df;
1986-04-06 17:42:56 +00:00
register struct idf *id = 0;
1986-03-29 01:04:49 +00:00
int scope;
int kind;
1986-04-02 17:34:21 +00:00
int imp_kind;
#define FROM_MODULE 0
#define FROM_ENCLOSING 1
1986-04-03 00:44:39 +00:00
struct def *lookfor(), *GetDefinitionModule();
1986-03-29 01:04:49 +00:00
1986-04-06 17:42:56 +00:00
if (idn) id = idn->nd_IDF;
1986-04-03 00:44:39 +00:00
kind = D_IMPORT;
scope = enclosing(CurrentScope)->sc_scope;
if (!id) imp_kind = FROM_ENCLOSING;
else {
imp_kind = FROM_MODULE;
if (local) df = lookfor(id, enclosing(CurrentScope), 1);
else df = GetDefinitionModule(id);
if (df->df_kind != D_MODULE) {
/* enter all "ids" with type D_ERROR */
kind = D_ERROR;
if (df->df_kind != D_ERROR) {
1986-04-06 17:42:56 +00:00
node_error(idn, "identifier \"%s\" does not represent a module", id->id_text);
1986-03-29 01:04:49 +00:00
}
}
1986-04-03 00:44:39 +00:00
else scope = df->mod_scope;
}
while (ids) {
if (imp_kind == FROM_MODULE) {
1986-04-06 17:42:56 +00:00
if (!(df = lookup(ids->nd_IDF, scope))) {
node_error(ids, "identifier \"%s\" not declared in qualifying module",
ids->nd_IDF->id_text);
1986-04-03 00:44:39 +00:00
df = ill_df;
}
else
if (!(df->df_flags&(D_EXPORTED|D_QEXPORTED))) {
1986-04-06 17:42:56 +00:00
node_error(ids,"identifier \"%s\" not exported from qualifying module",
ids->nd_IDF->id_text);
1986-04-02 17:34:21 +00:00
}
1986-04-03 00:44:39 +00:00
}
else {
if (local) {
1986-04-06 17:42:56 +00:00
df = lookfor(ids->nd_IDF,
1986-04-03 00:44:39 +00:00
enclosing(CurrentScope), 0);
1986-04-06 17:42:56 +00:00
} else df = GetDefinitionModule(ids->nd_IDF);
1986-04-03 00:44:39 +00:00
if (df->df_kind == D_ERROR) {
1986-04-06 17:42:56 +00:00
node_error(ids, "identifier \"%s\" not visible in enclosing scope",
ids->nd_IDF->id_text);
1986-03-29 01:04:49 +00:00
}
}
1986-04-06 17:42:56 +00:00
define(ids->nd_IDF, CurrentScope, kind)->imp_def = df;
1986-04-03 00:44:39 +00:00
if (df->df_kind == D_TYPE &&
df->df_type->tp_fund == ENUMERATION) {
/* Also import all enumeration literals */
exprt_literals(df->df_type->enm_enums,
CurrentScope);
}
ids = ids->next;
1986-03-29 01:04:49 +00:00
}
}
1986-04-02 17:34:21 +00:00
exprt_literals(df, toscope)
register struct def *df;
register struct scope *toscope;
{
/* A list of enumeration literals is exported. This is implemented
as an import from the scope "toscope".
*/
while (df) {
1986-04-03 00:44:39 +00:00
define(df->df_idf, toscope, D_IMPORT)->imp_def = df;
1986-04-02 17:34:21 +00:00
df = df->enm_next;
}
}
1986-04-03 17:41:26 +00:00
RemImports(pdf)
struct def **pdf;
{
/* Remove all imports from a definition module. This is
neccesary because the implementation module might import
them again.
*/
register struct def *df = *pdf, *df1 = 0;
while (df) {
if (df->df_kind == D_IMPORT) {
RemFromId(df);
if (df1) {
df1->df_nextinscope = df->df_nextinscope;
free_def(df);
df = df1->df_nextinscope;
}
else {
*pdf = df->df_nextinscope;
free_def(df);
df = *pdf;
}
}
else {
df1 = df;
df = df->df_nextinscope;
}
}
}
RemFromId(df)
struct def *df;
{
/* Remove definition "df" from the definition list
*/
register struct idf *id = df->df_idf;
register struct def *df1;
if (id->id_def == df) id->id_def = df->next;
else {
df1 = id->id_def;
while (df1->next != df) {
assert(df1->next != 0);
df1 = df1->next;
}
df1->next = df->next;
}
}