1987-03-10 17:51:10 +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".
|
|
|
|
*/
|
1986-03-10 13:07:55 +00:00
|
|
|
/* $Header$ */
|
|
|
|
/* PREPROCESSOR: MACRO-TEXT REPLACEMENT ROUTINES */
|
|
|
|
|
|
|
|
#include "nopp.h"
|
|
|
|
|
|
|
|
#ifndef NOPP
|
|
|
|
#include "debug.h" /* UF */
|
|
|
|
#include "pathlength.h" /* UF */
|
|
|
|
#include "strsize.h" /* UF */
|
1987-01-24 00:25:56 +00:00
|
|
|
#include <alloc.h>
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "idf.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "macro.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "class.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "interface.h"
|
1986-09-15 08:47:53 +00:00
|
|
|
#include "static.h"
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-03-26 16:58:43 +00:00
|
|
|
char *strcpy(), *strcat();
|
|
|
|
char *long2str();
|
1989-07-14 09:51:02 +00:00
|
|
|
extern int InputLevel;
|
1986-03-26 16:58:43 +00:00
|
|
|
|
1987-02-09 23:19:42 +00:00
|
|
|
PRIVATE struct mlist *ReplaceList; /* list of currently active macros */
|
1986-12-04 16:29:44 +00:00
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
EXPORT int
|
|
|
|
replace(idef)
|
1986-12-04 16:29:44 +00:00
|
|
|
register struct idf *idef;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
/* replace() is called by the lexical analyzer to perform
|
|
|
|
macro replacement. "idef" is the description of the
|
|
|
|
identifier which leads to the replacement. If the
|
|
|
|
optional actual parameters of the macro are OK, the text
|
|
|
|
of the macro is prepared to serve as an input buffer,
|
|
|
|
which is pushed onto the input stack.
|
|
|
|
replace() returns 1 if the replacement succeeded and 0 if
|
|
|
|
some error has occurred.
|
|
|
|
*/
|
1986-12-04 16:29:44 +00:00
|
|
|
register struct macro *mac = idef->id_macro;
|
1987-02-09 23:19:42 +00:00
|
|
|
register struct mlist *repl;
|
|
|
|
register int c;
|
1986-03-10 13:07:55 +00:00
|
|
|
char **actpars, **getactuals();
|
|
|
|
char *reptext, *macro2buffer();
|
|
|
|
int size;
|
|
|
|
|
1986-12-04 16:29:44 +00:00
|
|
|
if (mac->mc_flag & NOREPLACE) {
|
|
|
|
lexwarning("macro %s is recursive", idef->id_text);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (mac->mc_nps != -1) { /* with parameter list */
|
|
|
|
if (mac->mc_flag & FUNC) {
|
1986-12-01 10:00:23 +00:00
|
|
|
/* must be "defined".
|
|
|
|
Unfortunately, the next assertion
|
|
|
|
will not compile ...
|
|
|
|
ASSERT( ! strcmp("defined", idef->id_text));
|
|
|
|
*/
|
|
|
|
if (! AccDefined)
|
|
|
|
return 0;
|
|
|
|
}
|
1986-12-04 16:29:44 +00:00
|
|
|
if (++mac->mc_count > 100) {
|
|
|
|
/* 100 must be some number in Parameters */
|
|
|
|
lexwarning("macro %s is assumed recursive",
|
|
|
|
idef->id_text);
|
|
|
|
return 0;
|
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
LoadChar(c);
|
1989-10-25 14:23:08 +00:00
|
|
|
c = skipspaces(c,! (mac->mc_flag & FUNC));
|
1986-03-10 13:07:55 +00:00
|
|
|
if (c != '(') { /* no replacement if no () */
|
|
|
|
PushBack();
|
1989-10-25 14:23:08 +00:00
|
|
|
if (! (mac->mc_flag & FUNC)) {
|
|
|
|
lexerror("(warning) macro %s needs arguments",
|
|
|
|
idef->id_text);
|
|
|
|
return 0;
|
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
1986-12-04 16:29:44 +00:00
|
|
|
if (mac->mc_flag & FUNC) {
|
1989-10-25 14:23:08 +00:00
|
|
|
struct idf *param;
|
|
|
|
extern struct idf *GetIdentifier();
|
1986-12-01 10:00:23 +00:00
|
|
|
|
1989-10-25 14:23:08 +00:00
|
|
|
UnknownIdIsZero = 0;
|
|
|
|
param = GetIdentifier();
|
|
|
|
UnknownIdIsZero = 1;
|
|
|
|
if (c == '(') {
|
|
|
|
LoadChar(c);
|
|
|
|
c = skipspaces(c,0);
|
|
|
|
if (c != ')') error(") missing");
|
|
|
|
}
|
|
|
|
if (! param) {
|
|
|
|
error("identifier missing");
|
|
|
|
}
|
1987-02-09 23:19:42 +00:00
|
|
|
repl = new_mlist();
|
1989-10-25 14:23:08 +00:00
|
|
|
if (param && param->id_macro)
|
1986-12-04 16:29:44 +00:00
|
|
|
reptext = "1";
|
1986-12-01 10:00:23 +00:00
|
|
|
else
|
1986-12-04 16:29:44 +00:00
|
|
|
reptext = "0";
|
|
|
|
InsertText(reptext, 1);
|
1989-07-14 09:51:02 +00:00
|
|
|
InputLevel++;
|
|
|
|
repl->m_level = InputLevel;
|
1987-02-09 23:19:42 +00:00
|
|
|
repl->next = ReplaceList;
|
|
|
|
ReplaceList = repl;
|
|
|
|
repl->m_mac = mac;
|
1986-12-01 10:00:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1989-10-25 14:23:08 +00:00
|
|
|
actpars = getactuals(idef); /* get act.param. list */
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
1987-02-09 23:19:42 +00:00
|
|
|
repl = new_mlist();
|
|
|
|
repl->m_mac = mac;
|
1986-12-04 16:29:44 +00:00
|
|
|
if (mac->mc_flag & FUNC) /* this macro leads to special action */
|
1986-03-10 13:07:55 +00:00
|
|
|
macro_func(idef);
|
1987-02-09 23:19:42 +00:00
|
|
|
if (mac->mc_nps <= 0) {
|
|
|
|
reptext = mac->mc_text;
|
|
|
|
size = mac->mc_length;
|
1986-12-04 16:29:44 +00:00
|
|
|
mac->mc_flag |= NOREPLACE;
|
1987-02-09 23:19:42 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
reptext = macro2buffer(idef, actpars, &size); /* create input buffer */
|
|
|
|
repl->m_repl = reptext;
|
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
InsertText(reptext, size);
|
1989-07-14 09:51:02 +00:00
|
|
|
InputLevel++;
|
|
|
|
repl->m_level = InputLevel;
|
1987-02-09 23:19:42 +00:00
|
|
|
repl->next = ReplaceList;
|
|
|
|
ReplaceList = repl;
|
1986-03-10 13:07:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1986-09-17 12:31:45 +00:00
|
|
|
GSTATIC char FilNamBuf[PATHLENGTH];
|
1986-09-15 08:47:53 +00:00
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
PRIVATE
|
|
|
|
macro_func(idef)
|
1986-12-04 16:29:44 +00:00
|
|
|
register struct idf *idef;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
/* macro_func() performs the special actions needed with some
|
|
|
|
macros. These macros are __FILE__ and __LINE__ which
|
|
|
|
replacement texts must be evaluated at the time they are
|
|
|
|
used.
|
|
|
|
*/
|
1986-12-04 16:29:44 +00:00
|
|
|
register struct macro *mac = idef->id_macro;
|
|
|
|
|
1986-09-29 14:01:34 +00:00
|
|
|
switch (idef->id_text[2]) { /* This switch is very blunt... */
|
1986-03-10 13:07:55 +00:00
|
|
|
case 'F' : /* __FILE__ */
|
|
|
|
FilNamBuf[0] = '"';
|
|
|
|
strcpy(&FilNamBuf[1], FileName);
|
|
|
|
strcat(FilNamBuf, "\"");
|
1986-12-04 16:29:44 +00:00
|
|
|
mac->mc_text = FilNamBuf;
|
|
|
|
mac->mc_length = strlen(FilNamBuf);
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
|
|
|
case 'L' : /* __LINE__ */
|
1986-12-04 16:29:44 +00:00
|
|
|
mac->mc_text = long2str((long)LineNumber, 10);
|
|
|
|
mac->mc_length = 1;
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
|
|
|
default :
|
1986-04-04 11:31:03 +00:00
|
|
|
crash("(macro_func)");
|
1988-09-16 23:19:50 +00:00
|
|
|
/*NOTREACHED*/
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE char *
|
|
|
|
macro2buffer(idef, actpars, siztext)
|
|
|
|
struct idf *idef;
|
|
|
|
char **actpars;
|
|
|
|
int *siztext;
|
|
|
|
{
|
|
|
|
/* Macro2buffer() turns the macro replacement text, as it is
|
|
|
|
stored, into an input buffer, while each occurrence of the
|
|
|
|
non-ascii formal parameter mark is replaced by its
|
|
|
|
corresponding actual parameter specified in the actual
|
|
|
|
parameter list actpars. A pointer to the beginning of the
|
|
|
|
constructed text is returned, while *siztext is filled
|
|
|
|
with its length.
|
|
|
|
If there are no parameters, this function behaves
|
|
|
|
the same as strcpy().
|
|
|
|
*/
|
|
|
|
register int size = 8;
|
|
|
|
register char *text = Malloc(size);
|
1986-12-04 16:29:44 +00:00
|
|
|
register int pos = 0;
|
1986-03-10 13:07:55 +00:00
|
|
|
register char *ptr = idef->id_macro->mc_text;
|
|
|
|
|
|
|
|
while (*ptr) {
|
|
|
|
if (*ptr & FORMALP) { /* non-asc formal param. mark */
|
|
|
|
register int n = *ptr++ & 0177;
|
|
|
|
register char *p;
|
|
|
|
|
|
|
|
ASSERT(n != 0);
|
|
|
|
/* copy the text of the actual parameter
|
|
|
|
into the replacement text
|
|
|
|
*/
|
1987-01-14 09:58:51 +00:00
|
|
|
for (p = actpars[n - 1]; *p; p++) {
|
1986-03-10 13:07:55 +00:00
|
|
|
text[pos++] = *p;
|
1986-04-04 11:31:03 +00:00
|
|
|
if (pos == size)
|
1986-09-29 14:01:34 +00:00
|
|
|
text = Srealloc(text, size += RSTRSIZE);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
text[pos++] = *ptr++;
|
1986-04-04 11:31:03 +00:00
|
|
|
if (pos == size)
|
1986-03-10 13:07:55 +00:00
|
|
|
text = Srealloc(text, size += RSTRSIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text[pos] = '\0';
|
|
|
|
*siztext = pos;
|
|
|
|
return text;
|
|
|
|
}
|
1986-12-04 16:29:44 +00:00
|
|
|
|
|
|
|
EXPORT
|
|
|
|
DoUnstack()
|
|
|
|
{
|
1989-07-14 09:51:02 +00:00
|
|
|
Unstacked = 1;
|
1986-12-04 16:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT
|
|
|
|
EnableMacros()
|
|
|
|
{
|
1987-10-20 09:36:34 +00:00
|
|
|
register struct mlist *p = ReplaceList, *prev = 0;
|
1986-12-04 16:29:44 +00:00
|
|
|
|
|
|
|
ASSERT(Unstacked > 0);
|
1987-10-20 09:36:34 +00:00
|
|
|
while (p) {
|
1987-02-09 23:19:42 +00:00
|
|
|
struct mlist *nxt = p->next;
|
|
|
|
|
1989-07-14 09:51:02 +00:00
|
|
|
if (p->m_level > InputLevel) {
|
1987-10-20 09:36:34 +00:00
|
|
|
p->m_mac->mc_flag &= ~NOREPLACE;
|
|
|
|
if (p->m_mac->mc_count) p->m_mac->mc_count--;
|
|
|
|
if (p->m_repl) free(p->m_repl);
|
|
|
|
if (! prev) ReplaceList = nxt;
|
|
|
|
else prev->next = nxt;
|
|
|
|
free_mlist(p);
|
|
|
|
}
|
|
|
|
else prev = p;
|
1987-02-09 23:19:42 +00:00
|
|
|
p = nxt;
|
1986-12-04 16:29:44 +00:00
|
|
|
}
|
1987-10-20 09:36:34 +00:00
|
|
|
Unstacked = 0;
|
1986-12-04 16:29:44 +00:00
|
|
|
}
|
1991-12-17 14:11:15 +00:00
|
|
|
#endif /* NOPP */
|