ack/lang/m2/comp/enter.c

433 lines
11 KiB
C
Raw Normal View History

1986-06-10 13:18:52 +00:00
/* H I G H L E V E L S Y M B O L E N T R Y */
1986-03-26 17:53:13 +00:00
1986-05-01 19:06:53 +00:00
#include "debug.h"
1986-03-26 17:53:13 +00:00
1986-03-26 22:46:48 +00:00
#include <alloc.h>
1986-03-26 17:53:13 +00:00
#include <em_arith.h>
#include <em_label.h>
1986-07-08 14:59:02 +00:00
#include <em_code.h>
1986-04-07 17:40:38 +00:00
#include <assert.h>
1986-05-01 19:06:53 +00:00
1986-03-26 17:53:13 +00:00
#include "idf.h"
#include "def.h"
#include "type.h"
#include "scope.h"
1986-04-06 17:42:56 +00:00
#include "LLlex.h"
#include "node.h"
1986-04-18 17:53:47 +00:00
#include "main.h"
1986-03-26 17:53:13 +00:00
1986-03-26 22:46:48 +00:00
struct def *
1986-03-26 17:53:13 +00:00
Enter(name, kind, type, pnam)
char *name;
struct type *type;
{
1986-04-07 17:40:38 +00:00
/* Enter a definition for "name" with kind "kind" and type
"type" in the Current Scope. If it is a standard name, also
put its number in the definition structure.
*/
1986-06-10 13:18:52 +00:00
register struct def *df;
1986-03-26 17:53:13 +00:00
1986-06-10 13:18:52 +00:00
df = define(str2idf(name, 0), CurrentScope, kind);
1986-03-26 17:53:13 +00:00
df->df_type = type;
1986-06-10 13:18:52 +00:00
if (pnam) df->df_value.df_stdname = pnam;
1986-03-26 22:46:48 +00:00
return df;
}
1986-06-10 13:18:52 +00:00
EnterEnumList(Idlist, type)
struct node *Idlist;
register struct type *type;
1986-03-26 22:46:48 +00:00
{
1986-06-10 13:18:52 +00:00
/* Put a list of enumeration literals in the symbol table.
They all have type "type".
Also assign numbers to them, and link them together.
We must link them together because an enumeration type may
be exported, in which case its literals must also be exported.
Thus, we need an easy way to get to them.
1986-04-07 17:40:38 +00:00
*/
1986-03-26 22:46:48 +00:00
register struct def *df;
1986-06-10 13:18:52 +00:00
register struct node *idlist = Idlist;
1986-03-26 22:46:48 +00:00
1986-06-10 13:18:52 +00:00
type->enm_ncst = 0;
for (; idlist; idlist = idlist->next) {
df = define(idlist->nd_IDF, CurrentScope, D_ENUM);
1986-03-26 22:46:48 +00:00
df->df_type = type;
1986-06-10 13:18:52 +00:00
df->enm_val = (type->enm_ncst)++;
df->enm_next = type->enm_enums;
type->enm_enums = df;
}
FreeNode(Idlist);
}
1986-05-21 18:32:20 +00:00
1986-06-10 13:18:52 +00:00
EnterFieldList(Idlist, type, scope, addr)
struct node *Idlist;
register struct type *type;
struct scope *scope;
arith *addr;
{
/* Put a list of fields in the symbol table.
They all have type "type", and are put in scope "scope".
Mark them as QUALIFIED EXPORT, because that's exactly what
fields are, you can get to them by qualifying them.
*/
register struct def *df;
register struct node *idlist = Idlist;
1986-04-22 22:36:16 +00:00
1986-06-10 13:18:52 +00:00
for (; idlist; idlist = idlist->next) {
df = define(idlist->nd_IDF, scope, D_FIELD);
df->df_type = type;
df->df_flags |= D_QEXPORTED;
df->fld_off = align(*addr, type->tp_align);
*addr = df->fld_off + type->tp_size;
1986-03-27 17:37:41 +00:00
}
1986-06-10 13:18:52 +00:00
FreeNode(Idlist);
1986-03-27 17:37:41 +00:00
}
1986-06-10 13:18:52 +00:00
EnterVarList(Idlist, type, local)
struct node *Idlist;
1986-04-15 17:51:53 +00:00
struct type *type;
{
1986-04-21 17:27:06 +00:00
/* Enter a list of identifiers representing variables into the
name list. "type" represents the type of the variables.
"local" is set if the variables are declared local to a
1986-05-16 17:15:36 +00:00
procedure.
1986-04-21 17:27:06 +00:00
*/
1986-04-15 17:51:53 +00:00
register struct def *df;
1986-06-10 13:18:52 +00:00
register struct node *idlist = Idlist;
1986-04-28 18:06:58 +00:00
register struct scopelist *sc;
1986-04-21 17:27:06 +00:00
char buf[256];
1986-06-06 02:22:09 +00:00
extern char *sprint();
1986-04-15 17:51:53 +00:00
1986-04-28 18:06:58 +00:00
sc = CurrVis;
1986-04-22 22:36:16 +00:00
1986-04-15 17:51:53 +00:00
if (local) {
/* Find the closest enclosing open scope. This
is the procedure that we are dealing with
*/
1986-04-28 18:06:58 +00:00
while (sc->sc_scope->sc_scopeclosed) sc = enclosing(sc);
1986-04-15 17:51:53 +00:00
}
1986-06-10 13:18:52 +00:00
for (; idlist; idlist = idlist->nd_right) {
df = define(idlist->nd_IDF, CurrentScope, D_VARIABLE);
1986-04-15 17:51:53 +00:00
df->df_type = type;
1986-06-10 13:18:52 +00:00
if (idlist->nd_left) {
1986-04-21 17:27:06 +00:00
/* An address was supplied
*/
1986-04-15 17:51:53 +00:00
df->var_addrgiven = 1;
1986-06-17 12:04:05 +00:00
df->df_flags |= D_NOREG;
1986-06-10 13:18:52 +00:00
if (idlist->nd_left->nd_type != card_type) {
1986-10-06 20:36:30 +00:00
node_error(idlist->nd_left,
"Illegal type for address");
1986-04-15 17:51:53 +00:00
}
1986-06-10 13:18:52 +00:00
df->var_off = idlist->nd_left->nd_INT;
1986-04-15 17:51:53 +00:00
}
else if (local) {
1986-04-22 22:36:16 +00:00
/* subtract aligned size of variable to the offset,
as the variable list exists only local to a
procedure
1986-04-15 17:51:53 +00:00
*/
1986-04-28 18:06:58 +00:00
sc->sc_scope->sc_off =
1986-06-10 13:18:52 +00:00
-WA(align(type->tp_size - sc->sc_scope->sc_off,
type->tp_align));
1986-04-28 18:06:58 +00:00
df->var_off = sc->sc_scope->sc_off;
1986-04-15 17:51:53 +00:00
}
1986-04-21 17:27:06 +00:00
else {
1986-04-22 22:36:16 +00:00
/* Global name, possibly external
*/
1986-04-28 18:06:58 +00:00
sprint(buf,"%s_%s", sc->sc_scope->sc_name,
1986-04-21 17:27:06 +00:00
df->df_idf->id_text);
1986-06-06 02:22:09 +00:00
df->var_name = Salloc(buf, (unsigned)(strlen(buf)+1));
1986-06-17 12:04:05 +00:00
df->df_flags |= D_NOREG;
1986-05-16 17:15:36 +00:00
1986-04-21 17:27:06 +00:00
if (DefinitionModule) {
1986-06-17 12:04:05 +00:00
if (sc == Defined->mod_vis) {
C_exa_dnam(df->var_name);
}
1986-04-21 17:27:06 +00:00
}
else {
C_ina_dnam(df->var_name);
}
1986-04-18 17:53:47 +00:00
}
1986-06-10 13:18:52 +00:00
}
FreeNode(Idlist);
}
EnterParamList(ppr, Idlist, type, VARp, off)
struct paramlist **ppr;
1986-10-06 20:36:30 +00:00
struct node *Idlist;
1986-06-10 13:18:52 +00:00
struct type *type;
int VARp;
arith *off;
{
/* Create (part of) a parameterlist of a procedure.
"ids" indicates the list of identifiers, "tp" their type, and
"VARp" indicates D_VARPAR or D_VALPAR.
*/
register struct paramlist *pr;
register struct def *df;
register struct node *idlist = Idlist;
1986-08-26 14:33:24 +00:00
struct node *dummy = 0;
1986-06-17 12:04:05 +00:00
static struct paramlist *last;
1986-06-10 13:18:52 +00:00
1986-08-26 14:33:24 +00:00
if (! idlist) {
1986-09-25 19:39:06 +00:00
/* Can only happen when a procedure type is defined */
1986-08-26 14:33:24 +00:00
dummy = Idlist = idlist = MkLeaf(Name, &dot);
}
1986-06-10 13:18:52 +00:00
for ( ; idlist; idlist = idlist->next) {
pr = new_paramlist();
1986-06-17 12:04:05 +00:00
pr->next = 0;
1986-10-06 20:36:30 +00:00
if (!*ppr) *ppr = pr;
1986-06-17 12:04:05 +00:00
else last->next = pr;
last = pr;
1986-09-25 19:39:06 +00:00
if (!DefinitionModule && idlist != dummy) {
1986-08-26 14:33:24 +00:00
df = define(idlist->nd_IDF, CurrentScope, D_VARIABLE);
df->var_off = *off;
}
1986-10-06 20:36:30 +00:00
else df = new_def();
1986-06-10 13:18:52 +00:00
pr->par_def = df;
df->df_type = type;
df->df_flags = VARp;
1986-08-26 14:33:24 +00:00
1986-06-10 13:18:52 +00:00
if (IsConformantArray(type)) {
/* we need room for the base address and a descriptor
*/
*off += pointer_size + 3 * word_size;
}
else if (VARp == D_VARPAR) {
*off += pointer_size;
}
else {
*off += WA(type->tp_size);
}
}
FreeNode(Idlist);
}
1986-05-16 17:15:36 +00:00
1986-06-17 12:04:05 +00:00
STATIC
1986-06-10 13:18:52 +00:00
DoImport(df, scope)
register struct def *df;
struct scope *scope;
{
/* Definition "df" is imported to scope "scope".
Handle the case that it is an enumeration type or a module.
*/
define(df->df_idf, scope, D_IMPORT)->imp_def = df;
if (df->df_kind == D_TYPE && df->df_type->tp_fund == T_ENUMERATION) {
/* Also import all enumeration literals
*/
1986-09-25 19:39:06 +00:00
for (df = df->df_type->enm_enums; df; df = df->enm_next) {
1986-06-10 13:18:52 +00:00
define(df->df_idf, scope, D_IMPORT)->imp_def = df;
}
}
else if (df->df_kind == D_MODULE) {
/* Also import all definitions that are exported from this
module
*/
1986-09-25 19:39:06 +00:00
for (df = df->mod_vis->sc_scope->sc_def;
df;
df = df->df_nextinscope) {
1986-06-10 13:18:52 +00:00
if (df->df_flags & D_EXPORTED) {
define(df->df_idf,scope,D_IMPORT)->imp_def = df;
}
}
1986-04-15 17:51:53 +00:00
}
}
1986-06-17 12:04:05 +00:00
STATIC struct scopelist *
1986-06-10 13:18:52 +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-06-10 13:18:52 +00:00
df->df_scope = enclosing(CurrVis)->sc_scope;
df->df_kind = D_FORWMODULE;
open_scope(CLOSEDSCOPE);
vis = CurrVis; /* The new scope, but watch out, it's "sc_encl"
field is not set right. It must indicate the
enclosing scope, but this must be done AFTER
closing this one
*/
close_scope(0);
vis->sc_encl = enclosing(CurrVis);
/* Here ! */
1986-10-06 20:36:30 +00:00
df->for_vis = vis;
df->for_node = MkLeaf(Name, &(idn->nd_token));
1986-06-10 13:18:52 +00:00
return vis;
}
1986-06-17 12:04:05 +00:00
STATIC struct def *
1986-06-10 13:18:52 +00:00
ForwDef(ids, scope)
register struct node *ids;
struct scope *scope;
1986-03-27 17:37:41 +00:00
{
1986-06-10 13:18:52 +00:00
/* Enter a forward definition of "ids" in scope "scope",
if it is not already defined.
1986-04-02 17:34:21 +00:00
*/
1986-06-10 13:18:52 +00:00
register struct def *df;
if (!(df = lookup(ids->nd_IDF, scope))) {
df = define(ids->nd_IDF, scope, D_FORWARD);
df->for_node = MkLeaf(Name, &(ids->nd_token));
1986-03-27 17:37:41 +00:00
}
1986-06-10 13:18:52 +00:00
return df;
}
1986-04-18 17:53:47 +00:00
1986-06-10 13:18:52 +00:00
EnterExportList(Idlist, qualified)
struct node *Idlist;
{
/* 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".
*/
register struct node *idlist = Idlist;
register struct def *df, *df1;
for (;idlist; idlist = idlist->next) {
df = lookup(idlist->nd_IDF, CurrentScope);
if (!df) {
/* undefined item in export list
*/
1986-10-06 20:36:30 +00:00
node_error(idlist,
"identifier \"%s\" not defined",
idlist->nd_IDF->id_text);
1986-06-10 13:18:52 +00:00
continue;
}
if (df->df_flags & (D_EXPORTED|D_QEXPORTED)) {
1986-10-06 20:36:30 +00:00
node_error(idlist,
"multiple occurrences of \"%s\" in export list",
idlist->nd_IDF->id_text);
1986-06-10 13:18:52 +00:00
}
df->df_flags |= qualified;
if (qualified == D_EXPORTED) {
/* Export, but not qualified.
Find all imports of the module in which this export
occurs, and export the current definition to it
*/
1986-10-06 20:36:30 +00:00
df1 = CurrentScope->sc_definedby->df_idf->id_def;
while (df1) {
if (df1->df_kind == D_IMPORT &&
df1->imp_def == CurrentScope->sc_definedby) {
DoImport(df, df1->df_scope);
1986-06-10 13:18:52 +00:00
}
1986-10-06 20:36:30 +00:00
df1 = df1->next;
1986-06-10 13:18:52 +00:00
}
/* Also handle the definition as if the enclosing
scope imports it.
*/
df1 = lookup(idlist->nd_IDF,
enclosing(CurrVis)->sc_scope);
if (df1) {
/* It was already defined in the enclosing
scope. There are two legal possibilities,
which are examined below.
*/
1986-09-25 19:39:06 +00:00
if (df1->df_kind == D_PROCHEAD &&
df->df_kind == D_PROCEDURE) {
1986-06-10 13:18:52 +00:00
df1->df_kind = D_IMPORT;
df1->imp_def = df;
continue;
}
1986-09-25 19:39:06 +00:00
if (df1->df_kind == D_HIDDEN &&
df->df_kind == D_TYPE) {
if (df->df_type->tp_fund != T_POINTER) {
1986-10-06 20:36:30 +00:00
node_error(idlist,
"opaque type \"%s\" is not a pointer type",
df->df_idf->id_text);
1986-09-25 19:39:06 +00:00
}
assert(df1->df_type->next == NULLTYPE);
df1->df_kind = D_TYPE;
df1->df_type->next = df->df_type;
continue;
}
1986-06-10 13:18:52 +00:00
}
1986-04-18 17:53:47 +00:00
1986-06-10 13:18:52 +00:00
DoImport(df, enclosing(CurrVis)->sc_scope);
}
}
FreeNode(Idlist);
}
1986-08-26 14:33:24 +00:00
EnterFromImportList(Idlist, FromDef)
1986-06-10 13:18:52 +00:00
struct node *Idlist;
1986-08-26 14:33:24 +00:00
register struct def *FromDef;
1986-06-10 13:18:52 +00:00
{
1986-08-26 14:33:24 +00:00
/* Import the list Idlist from the module indicated by Fromdef.
1986-06-10 13:18:52 +00:00
*/
register struct node *idlist = Idlist;
1986-08-26 14:33:24 +00:00
register struct scopelist *vis;
1986-06-10 13:18:52 +00:00
register struct def *df;
int forwflag = 0;
1986-08-26 14:33:24 +00:00
switch(FromDef->df_kind) {
case D_ERROR:
/* 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.
???
*/
vis = ForwModule(FromDef, FromDef->df_idf);
forwflag = 1;
break;
case D_FORWMODULE:
vis = FromDef->for_vis;
break;
case D_MODULE:
vis = FromDef->mod_vis;
break;
default:
1986-10-06 20:36:30 +00:00
error("identifier \"%s\" does not represent a module",
FromDef->df_idf->id_text);
1986-08-26 14:33:24 +00:00
break;
1986-06-10 13:18:52 +00:00
}
for (; idlist; idlist = idlist->next) {
1986-10-06 20:36:30 +00:00
if (forwflag) df = ForwDef(idlist, vis->sc_scope);
else if (! (df = lookup(idlist->nd_IDF, vis->sc_scope))) {
node_error(idlist,
"identifier \"%s\" not declared in qualifying module",
idlist->nd_IDF->id_text);
1986-06-10 13:18:52 +00:00
df = define(idlist->nd_IDF,vis->sc_scope,D_ERROR);
}
1986-10-06 20:36:30 +00:00
else if (! (df->df_flags & (D_EXPORTED|D_QEXPORTED))) {
node_error(idlist,
"identifier \"%s\" not exported from qualifying module",
idlist->nd_IDF->id_text);
1986-06-10 13:18:52 +00:00
df->df_flags |= D_QEXPORTED;
}
DoImport(df, CurrentScope);
}
FreeNode(Idlist);
}
EnterImportList(Idlist, local)
struct node *Idlist;
{
/* Import "Idlist" from the enclosing scope.
An exception must be made for imports of the compilation unit.
In this case, definition modules must be read for "Idlist".
This case is indicated by the value 0 of the "local" flag.
*/
register struct node *idlist = Idlist;
1986-10-06 20:36:30 +00:00
struct scope *sc = enclosing(CurrVis)->sc_scope;
1986-06-17 12:04:05 +00:00
extern struct def *GetDefinitionModule();
1986-06-10 13:18:52 +00:00
for (; idlist; idlist = idlist->next) {
1986-10-06 20:36:30 +00:00
DoImport(local ?
ForwDef(idlist, sc) :
GetDefinitionModule(idlist->nd_IDF) ,
CurrentScope);
1986-06-10 13:18:52 +00:00
}
FreeNode(Idlist);
1986-03-26 17:53:13 +00:00
}