ack/lang/m2/comp/defmodule.c

167 lines
3.4 KiB
C
Raw Normal View History

/*
* (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-04-03 00:44:39 +00:00
/* D E F I N I T I O N M O D U L E S */
1994-06-24 14:02:31 +00:00
/* $Id$ */
1986-05-01 19:06:53 +00:00
#include "debug.h"
1986-04-04 13:47:04 +00:00
1986-04-03 00:44:39 +00:00
#include <assert.h>
#include <em_arith.h>
#include <em_label.h>
#include <alloc.h>
1986-04-21 17:27:06 +00:00
1986-04-03 00:44:39 +00:00
#include "idf.h"
#include "input.h"
#include "scope.h"
#include "LLlex.h"
#include "def.h"
1986-10-06 20:36:30 +00:00
#include "Lpars.h"
1986-04-03 00:44:39 +00:00
#include "f_info.h"
1986-04-21 17:27:06 +00:00
#include "main.h"
1986-10-06 20:36:30 +00:00
#include "node.h"
1986-11-05 14:33:00 +00:00
#include "type.h"
#include "misc.h"
1986-04-21 17:27:06 +00:00
1986-04-08 18:15:46 +00:00
#ifdef DEBUG
long sys_filesize();
#endif
1986-04-03 00:44:39 +00:00
t_idf *DefId;
1986-12-09 17:41:06 +00:00
char *
getwdir(fn)
1987-05-18 15:57:33 +00:00
register char *fn;
{
register char *p;
char *strrchr();
while ((p = strrchr(fn,'/')) && *(p + 1) == '\0') {
/* remove trailing /'s */
*p = '\0';
}
if (p) {
*p = '\0';
1987-05-18 15:57:33 +00:00
fn = Salloc(fn, (unsigned) (p - &fn[0] + 1));
*p = '/';
return fn;
}
return "";
}
1986-11-05 14:33:00 +00:00
STATIC
1986-04-03 00:44:39 +00:00
GetFile(name)
char *name;
{
/* Try to find a file with basename "name" and extension ".def",
in the directories mentioned in "DEFPATH".
*/
1986-10-06 20:36:30 +00:00
char buf[15];
1987-05-18 15:57:33 +00:00
char *strncpy(), *strcat();
1986-04-03 00:44:39 +00:00
1986-10-06 20:36:30 +00:00
strncpy(buf, name, 10);
1986-06-06 02:22:09 +00:00
buf[10] = '\0'; /* maximum length */
strcat(buf, ".def");
DEFPATH[0] = WorkingDir;
1986-04-03 00:44:39 +00:00
if (! InsertFile(buf, DEFPATH, &(FileName))) {
1986-11-26 16:40:45 +00:00
error("could not find a DEFINITION MODULE for \"%s\"", name);
1986-11-05 14:33:00 +00:00
return 0;
1986-04-03 00:44:39 +00:00
}
WorkingDir = getwdir(FileName);
1986-04-03 00:44:39 +00:00
LineNumber = 1;
1986-06-26 09:39:36 +00:00
DO_DEBUG(options['F'], debug("File %s : %ld characters", FileName, sys_filesize(FileName)));
1986-11-05 14:33:00 +00:00
return 1;
1986-04-03 00:44:39 +00:00
}
t_def *
1986-10-06 20:36:30 +00:00
GetDefinitionModule(id, incr)
register t_idf *id;
1986-04-03 00:44:39 +00:00
{
/* Return a pointer to the "def" structure of the definition
module indicated by "id".
We may have to read the definition module itself.
1986-10-06 20:36:30 +00:00
Also increment level by "incr".
1986-04-03 00:44:39 +00:00
*/
register t_def *df;
1986-05-28 18:36:51 +00:00
static int level;
t_scopelist *vis;
char *fn = FileName;
int ln = LineNumber;
1988-10-25 17:43:19 +00:00
t_scope *newsc;
1986-04-03 00:44:39 +00:00
1986-10-06 20:36:30 +00:00
level += incr;
df = lookup(id, GlobalScope, D_IMPORTED, 0);
1986-04-03 00:44:39 +00:00
if (!df) {
/* Read definition module. Make an exception for SYSTEM.
*/
extern int ForeignFlag;
ForeignFlag = 0;
DefId = id;
open_scope(CLOSEDSCOPE);
1988-10-25 17:43:19 +00:00
newsc = CurrentScope;
1988-10-13 15:43:23 +00:00
vis = CurrVis;
1988-10-25 17:43:19 +00:00
newsc->sc_defmodule = incr;
1986-04-03 00:44:39 +00:00
if (!strcmp(id->id_text, "SYSTEM")) {
do_SYSTEM();
df = lookup(id, GlobalScope, D_IMPORTED, 0);
1986-04-03 00:44:39 +00:00
}
else {
if (!is_anon_idf(id) && GetFile(id->id_text)) {
1993-01-19 15:33:35 +00:00
char *f = FileName;
1986-11-05 14:33:00 +00:00
DefModule();
df = lookup(id, GlobalScope, D_IMPORTED, 0);
if (level == 1 &&
(df && !(df->df_flags & D_FOREIGN))) {
1986-11-05 14:33:00 +00:00
/* The module is directly imported by
the currently defined module, and
is not foreign, so we have to
remember its name because we have
to call its initialization routine
1986-11-05 14:33:00 +00:00
*/
static t_node *nd_end;
register t_node *n;
extern t_node *Modules;
1986-10-06 20:36:30 +00:00
1988-03-23 17:44:25 +00:00
n = dot2leaf(Def);
n->nd_def = newsc->sc_definedby;
if (nd_end) nd_end->nd_NEXT = n;
1986-11-05 14:33:00 +00:00
else Modules = n;
nd_end = n;
}
1993-01-19 15:33:35 +00:00
free(f);
1986-05-28 18:36:51 +00:00
}
else {
df = lookup(id, GlobalScope, D_IMPORTED, 0);
newsc->sc_name = id->id_text;
}
1986-04-03 00:44:39 +00:00
}
close_scope(SC_CHKFORW);
1986-11-05 14:33:00 +00:00
if (! df) {
df = MkDef(id, GlobalScope, D_ERROR);
df->mod_vis = vis;
newsc->sc_definedby = df;
1986-11-05 14:33:00 +00:00
}
1986-04-03 00:44:39 +00:00
}
else if (df->df_flags & D_BUSY) {
error("definition module \"%s\" depends on itself",
id->id_text);
}
else if (df == Defined && level == 1) {
1987-07-13 11:49:32 +00:00
error("cannot import from current module \"%s\"", id->id_text);
df->df_kind = D_ERROR;
}
FileName = fn;
LineNumber = ln;
1986-11-05 14:33:00 +00:00
assert(df);
1986-10-06 20:36:30 +00:00
level -= incr;
1986-04-03 00:44:39 +00:00
return df;
}