ack/lang/m2/comp/def.c

502 lines
11 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 */
1986-05-01 19:06:53 +00:00
#ifndef NORCSID
1986-03-26 15:11:02 +00:00
static char *RcsId = "$Header$";
1986-05-01 19:06:53 +00:00
#endif
#include "debug.h"
1986-03-26 15:11:02 +00:00
#include <alloc.h>
#include <em_arith.h>
#include <em_label.h>
1986-03-29 01:04:49 +00:00
#include <assert.h>
1986-04-21 17:27:06 +00:00
1986-04-08 18:15:46 +00:00
#include "main.h"
1986-03-26 15:11:02 +00:00
#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 "scope.h"
1986-04-06 17:42:56 +00:00
#include "LLlex.h"
#include "node.h"
1986-04-21 17:27:06 +00:00
1986-03-26 15:11:02 +00:00
struct def *h_def; /* Pointer to free list of def structures */
1986-05-01 19:06:53 +00:00
struct def *ill_df;
1986-03-27 17:37:41 +00:00
1986-04-18 17:53:47 +00:00
struct def *
MkDef(id, scope, kind)
struct idf *id;
struct scope *scope;
{
/* Create a new definition structure in scope "scope", with
id "id" and kind "kind".
*/
register struct def *df;
df = new_def();
1986-04-28 18:06:58 +00:00
clear((char *) df, sizeof (*df));
1986-04-18 17:53:47 +00:00
df->df_idf = id;
df->df_scope = scope;
df->df_kind = kind;
df->next = id->id_def;
id->id_def = df;
/* enter the definition in the list of definitions in this scope
*/
df->df_nextinscope = scope->sc_def;
scope->sc_def = df;
return df;
}
1986-05-01 19:06:53 +00:00
InitDef()
{
/* Initialize this module. Easy, the only thing to be initialized
is "illegal_def".
*/
struct idf *gen_anon_idf();
ill_df = MkDef(gen_anon_idf(), CurrentScope, D_ERROR);
}
1986-03-26 15:11:02 +00:00
struct def *
define(id, scope, kind)
register struct idf *id;
1986-04-08 18:15:46 +00:00
register 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-26 15:11:02 +00:00
1986-04-15 17:51:53 +00:00
df = lookup(id, 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-04-28 18:06:58 +00:00
(
1986-04-15 17:51:53 +00:00
scopeclosed(scope)
1986-03-27 17:37:41 +00:00
&&
1986-04-15 17:51:53 +00:00
(df = lookup(id, PervasiveScope)))
1986-03-26 17:53:13 +00:00
) {
1986-03-26 15:11:02 +00:00
switch(df->df_kind) {
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-04-28 18:06:58 +00:00
1986-04-11 11:57:19 +00:00
case D_FORWMODULE:
1986-04-12 02:21:24 +00:00
if (kind == D_FORWMODULE) {
return df;
}
1986-04-28 18:06:58 +00:00
1986-04-12 02:21:24 +00:00
if (kind == D_MODULE) {
FreeNode(df->for_node);
1986-04-28 18:06:58 +00:00
df->mod_vis = df->for_vis;
1986-04-11 11:57:19 +00:00
df->df_kind = kind;
return df;
}
break;
1986-04-28 18:06:58 +00:00
1986-04-11 11:57:19 +00:00
case D_FORWARD:
1986-04-12 02:21:24 +00:00
if (kind != D_FORWARD) {
FreeNode(df->for_node);
}
1986-04-28 18:06:58 +00:00
df->df_kind = kind;
return df;
1986-04-12 02:21:24 +00:00
case D_ERROR:
1986-03-26 15:11:02 +00:00
df->df_kind = kind;
return df;
}
1986-04-28 18:06:58 +00:00
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-04-28 18:06:58 +00:00
1986-03-26 15:11:02 +00:00
return df;
}
1986-04-28 18:06:58 +00:00
1986-04-18 17:53:47 +00:00
return MkDef(id, scope, kind);
1986-03-26 15:11:02 +00:00
}
struct def *
lookup(id, scope)
register struct idf *id;
1986-04-15 17:51:53 +00:00
struct scope *scope;
1986-03-26 15:11:02 +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.
*/
register struct def *df, *df1;
1986-04-12 02:21:24 +00:00
struct def *retval;
1986-03-26 15:11:02 +00:00
df1 = 0;
df = id->id_def;
while (df) {
1986-03-26 17:53:13 +00:00
if (df->df_scope == scope) {
1986-04-12 02:21:24 +00:00
retval = df;
1986-03-29 01:04:49 +00:00
if (df->df_kind == D_IMPORT) {
1986-04-12 02:21:24 +00:00
retval = df->imp_def;
assert(retval != 0);
1986-04-11 11:57:19 +00:00
}
1986-03-26 15:11:02 +00:00
if (df1) {
df1->next = df->next;
df->next = id->id_def;
id->id_def = df;
}
1986-04-12 02:21:24 +00:00
return retval;
1986-03-26 15:11:02 +00:00
}
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
1986-04-28 18:06:58 +00:00
DoImport(df, scope)
struct def *df;
struct scope *scope;
{
register struct def *df1;
if (df->df_kind == D_TYPE && df->df_type->tp_fund == T_ENUMERATION) {
/* Also import all enumeration literals
*/
df1 = df->df_type->enm_enums;
while (df1) {
define(df1->df_idf, scope, D_IMPORT)->imp_def = df1;
df1 = df1->enm_next;
}
}
else if (df->df_kind == D_MODULE) {
/* Also import all definitions that are exported from this
module
*/
df1 = df->mod_vis->sc_scope->sc_def;
while (df1) {
if (df1->df_flags & D_EXPORTED) {
define(df1->df_idf, scope, D_IMPORT)->imp_def = df1;
}
df1 = df1->df_nextinscope;
}
}
}
Export(ids, qualified, moddef)
1986-04-06 17:42:56 +00:00
register struct node *ids;
1986-04-28 18:06:58 +00:00
struct def *moddef;
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-04-11 11:57:19 +00:00
register struct def *df, *df1;
1986-04-28 18:06:58 +00:00
register struct def *impmod;
1986-03-29 01:04:49 +00:00
1986-04-28 18:06:58 +00:00
for (;ids; ids = ids->next) {
1986-04-15 17:51:53 +00:00
df = lookup(ids->nd_IDF, CurrentScope);
1986-04-28 18:06:58 +00:00
if (!df) {
/* undefined item in export list
*/
node_error(ids, "identifier \"%s\" not defined", ids->nd_IDF->id_text);
continue;
}
if (df->df_flags & (D_EXPORTED|D_QEXPORTED)) {
1986-04-12 02:21:24 +00:00
node_error(ids, "Identifier \"%s\" occurs more than once in export list",
df->df_idf->id_text);
}
1986-04-28 18:06:58 +00:00
1986-03-29 01:04:49 +00:00
if (qualified) {
df->df_flags |= D_QEXPORTED;
}
else {
1986-04-28 18:06:58 +00:00
/* Export, but not qualified.
Find all imports of the module in which this export
occurs, and export the current definition to it
*/
impmod = moddef->df_idf->id_def;
while (impmod) {
if (impmod->df_kind == D_IMPORT &&
impmod->imp_def == moddef) {
DoImport(df, impmod->df_scope);
}
impmod = impmod->next;
}
1986-03-29 01:04:49 +00:00
df->df_flags |= D_EXPORTED;
1986-04-28 18:06:58 +00:00
df1 = lookup(ids->nd_IDF, enclosing(CurrVis)->sc_scope);
if (df1 && df1->df_kind == D_PROCHEAD) {
if (df->df_kind == D_PROCEDURE) {
df1->df_kind = D_IMPORT;
df1->imp_def = df;
continue;
}
1986-04-11 11:57:19 +00:00
}
1986-04-28 18:06:58 +00:00
else if (df1 && df1->df_kind == D_HIDDEN) {
if (df->df_kind == D_TYPE) {
if (df->df_type->tp_fund != T_POINTER) {
error("Opaque type \"%s\" is not a pointer type", df->df_idf->id_text);
}
df->df_kind = D_HTYPE;
df1->df_kind = D_IMPORT;
df1->imp_def = df;
continue;
}
1986-04-11 11:57:19 +00:00
}
1986-04-28 18:06:58 +00:00
df1 = define(ids->nd_IDF,
enclosing(CurrVis)->sc_scope,
D_IMPORT);
1986-04-11 11:57:19 +00:00
df1->imp_def = df;
1986-04-28 18:06:58 +00:00
DoImport(df, enclosing(CurrVis)->sc_scope);
1986-03-29 01:04:49 +00:00
}
}
}
1986-04-28 18:06:58 +00:00
static struct scopelist *
1986-04-15 17:51:53 +00:00
ForwModule(df, idn)
register struct def *df;
struct node *idn;
{
/* An import is done from a not yet defined module "idn".
Create a declaration and a scope for this module.
*/
1986-04-28 18:06:58 +00:00
struct scopelist *vis;
1986-04-15 17:51:53 +00:00
1986-04-28 18:06:58 +00:00
df->df_scope = enclosing(CurrVis)->sc_scope;
1986-04-15 17:51:53 +00:00
df->df_kind = D_FORWMODULE;
open_scope(CLOSEDSCOPE);
1986-04-28 18:06:58 +00:00
vis = CurrVis; /* The new scope, but watch out, it's "sc_encl"
1986-04-15 17:51:53 +00:00
field is not set right. It must indicate the
enclosing scope, but this must be done AFTER
closing this one
*/
1986-04-28 18:06:58 +00:00
df->for_vis = vis;
1986-04-15 17:51:53 +00:00
df->for_node = MkNode(Name, NULLNODE, NULLNODE, &(idn->nd_token));
close_scope(0);
1986-04-28 18:06:58 +00:00
vis->sc_encl = enclosing(CurrVis);
1986-04-15 17:51:53 +00:00
/* Here ! */
1986-04-28 18:06:58 +00:00
return vis;
1986-04-15 17:51:53 +00:00
}
static struct def *
ForwDef(ids, scope)
register struct node *ids;
struct scope *scope;
{
/* Enter a forward definition of "ids" in scope "scope",
if it is not already defined.
*/
register struct def *df;
if (!(df = lookup(ids->nd_IDF, scope))) {
df = define(ids->nd_IDF, scope, D_FORWARD);
df->for_node = MkNode(Name,NULLNODE,NULLNODE,&(ids->nd_token));
}
return df;
}
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-28 18:06:58 +00:00
struct scopelist *vis = enclosing(CurrVis);
1986-04-15 17:51:53 +00:00
int forwflag = 0;
1986-04-02 17:34:21 +00:00
#define FROM_MODULE 0
#define FROM_ENCLOSING 1
1986-04-15 17:51:53 +00:00
int imp_kind = FROM_ENCLOSING;
1986-04-03 00:44:39 +00:00
struct def *lookfor(), *GetDefinitionModule();
1986-03-29 01:04:49 +00:00
1986-04-15 17:51:53 +00:00
if (idn) {
1986-04-03 00:44:39 +00:00
imp_kind = FROM_MODULE;
1986-04-11 11:57:19 +00:00
if (local) {
1986-04-28 18:06:58 +00:00
df = lookfor(idn, vis, 0);
1986-04-15 17:51:53 +00:00
switch(df->df_kind) {
case D_ERROR:
1986-04-11 11:57:19 +00:00
/* The module from which the import was done
is not yet declared. I'm not sure if I must
accept this, but for the time being I will.
???
*/
1986-04-28 18:06:58 +00:00
vis = ForwModule(df, idn);
1986-04-15 17:51:53 +00:00
forwflag = 1;
break;
case D_FORWMODULE:
1986-04-28 18:06:58 +00:00
vis = df->for_vis;
1986-04-15 17:51:53 +00:00
break;
case D_MODULE:
1986-04-28 18:06:58 +00:00
vis = df->mod_vis;
1986-04-15 17:51:53 +00:00
break;
default:
1986-04-12 02:21:24 +00:00
node_error(idn, "identifier \"%s\" does not represent a module",
idn->nd_IDF->id_text);
1986-04-15 17:51:53 +00:00
break;
1986-03-29 01:04:49 +00:00
}
}
1986-04-28 18:06:58 +00:00
else vis = GetDefinitionModule(idn->nd_IDF)->mod_vis;
1986-04-15 17:51:53 +00:00
1986-04-12 02:21:24 +00:00
FreeNode(idn);
1986-04-03 00:44:39 +00:00
}
1986-04-12 02:21:24 +00:00
idn = ids;
1986-04-03 00:44:39 +00:00
while (ids) {
if (imp_kind == FROM_MODULE) {
1986-04-15 17:51:53 +00:00
if (forwflag) {
1986-04-28 18:06:58 +00:00
df = ForwDef(ids, vis->sc_scope);
1986-04-11 11:57:19 +00:00
}
1986-04-28 18:06:58 +00:00
else if (!(df = lookup(ids->nd_IDF, vis->sc_scope))) {
1986-04-06 17:42:56 +00:00
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;
}
1986-04-11 11:57:19 +00:00
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 {
1986-04-28 18:06:58 +00:00
if (local) df = ForwDef(ids, vis->sc_scope);
1986-04-15 17:51:53 +00:00
else df = GetDefinitionModule(ids->nd_IDF);
1986-03-29 01:04:49 +00:00
}
1986-04-15 17:51:53 +00:00
1986-05-01 19:06:53 +00:00
define(ids->nd_IDF,CurrentScope,D_IMPORT)->imp_def = df;
1986-04-28 18:06:58 +00:00
DoImport(df, CurrentScope);
1986-05-01 19:06:53 +00:00
1986-04-03 00:44:39 +00:00
ids = ids->next;
1986-03-29 01:04:49 +00:00
}
1986-04-15 17:51:53 +00:00
1986-04-12 02:21:24 +00:00
FreeNode(idn);
1986-03-29 01:04:49 +00:00
}
1986-04-02 17:34:21 +00:00
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.
*/
1986-04-12 02:21:24 +00:00
register struct def *df = *pdf;
1986-04-03 17:41:26 +00:00
while (df) {
if (df->df_kind == D_IMPORT) {
RemFromId(df);
1986-04-12 02:21:24 +00:00
*pdf = df->df_nextinscope;
free_def(df);
1986-04-03 17:41:26 +00:00
}
else {
1986-04-12 02:21:24 +00:00
pdf = &(df->df_nextinscope);
1986-04-03 17:41:26 +00:00
}
1986-04-12 02:21:24 +00:00
df = *pdf;
1986-04-03 17:41:26 +00:00
}
}
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;
}
}
1986-04-15 17:51:53 +00:00
1986-04-21 17:27:06 +00:00
struct def *
DeclProc(type)
{
/* A procedure is declared, either in a definition or a program
module. Create a def structure for it (if neccessary)
*/
register struct def *df;
extern char *sprint(), *Malloc(), *strcpy();
static int nmcount = 0;
char buf[256];
assert(type & (D_PROCEDURE | D_PROCHEAD));
if (type == D_PROCHEAD) {
/* In a definition module
*/
df = define(dot.TOK_IDF, CurrentScope, type);
df->for_node = MkNode(Name, NULLNODE, NULLNODE, &dot);
sprint(buf,"%s_%s",CurrentScope->sc_name,df->df_idf->id_text);
df->for_name = Malloc((unsigned) (strlen(buf)+1));
strcpy(df->for_name, buf);
C_exp(df->for_name);
}
else {
df = lookup(dot.TOK_IDF, CurrentScope);
if (df && df->df_kind == D_PROCHEAD) {
/* C_exp already generated when we saw the definition
in the definition module
*/
1986-04-22 22:36:16 +00:00
df->df_kind = D_PROCEDURE;
open_scope(OPENSCOPE);
CurrentScope->sc_name = df->for_name;
1986-04-28 18:06:58 +00:00
df->prc_vis = CurrVis;
1986-04-21 17:27:06 +00:00
}
else {
df = define(dot.TOK_IDF, CurrentScope, type);
1986-04-28 18:06:58 +00:00
if (CurrVis != Defined->mod_vis) {
1986-04-21 17:27:06 +00:00
sprint(buf, "_%d_%s", ++nmcount,
df->df_idf->id_text);
}
1986-05-01 19:06:53 +00:00
else sprint(buf, "%s_%s",CurrentScope->sc_name,
df->df_idf->id_text);
1986-04-22 22:36:16 +00:00
open_scope(OPENSCOPE);
1986-04-28 18:06:58 +00:00
df->prc_vis = CurrVis;
1986-04-22 22:36:16 +00:00
CurrentScope->sc_name = Malloc((unsigned)(strlen(buf)+1));
strcpy(CurrentScope->sc_name, buf);
1986-04-21 17:27:06 +00:00
C_inp(buf);
}
}
return df;
}
1986-04-23 22:12:22 +00:00
InitProc(nd, df)
struct node *nd;
struct def *df;
{
/* Create an initialization procedure for a module.
*/
df->mod_body = nd;
/* Keep it this way, or really create a procedure out of it??? */
}
1986-04-15 17:51:53 +00:00
#ifdef DEBUG
PrDef(df)
register struct def *df;
{
1986-05-01 19:06:53 +00:00
print("n: %s, k: %d\n", df->df_idf->id_text, df->df_kind);
1986-04-15 17:51:53 +00:00
}
#endif DEBUG