domacro.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. /* PREPROCESSOR: CONTROLLINE INTERPRETER */
  7. #include "interface.h"
  8. #include "arith.h"
  9. #include "LLlex.h"
  10. #include "Lpars.h"
  11. #include "debug.h"
  12. #include "idf.h"
  13. #include "input.h"
  14. #include "nopp.h"
  15. #ifndef NOPP
  16. #include "ifdepth.h"
  17. #include "botch_free.h"
  18. #include "nparams.h"
  19. #include "parbufsize.h"
  20. #include "textsize.h"
  21. #include "idfsize.h"
  22. #include "assert.h"
  23. #include <alloc.h>
  24. #include "class.h"
  25. #include "macro.h"
  26. IMPORT char *inctable[]; /* list of include directories */
  27. IMPORT char *getwdir();
  28. PRIVATE char ifstack[IFDEPTH]; /* if-stack: the content of an entry is */
  29. /* 1 if a corresponding ELSE has been */
  30. /* encountered. */
  31. PRIVATE int nestlevel = -1; /* initially no nesting level. */
  32. PRIVATE struct idf *
  33. GetIdentifier()
  34. {
  35. /* returns a pointer to the descriptor of the identifier that is
  36. read from the input stream. A null-pointer is returned if
  37. the input does not contain an identifier.
  38. The substitution of macros is disabled.
  39. */
  40. int tok;
  41. struct token tk;
  42. ReplaceMacros = 0;
  43. tok = GetToken(&tk);
  44. ReplaceMacros = 1;
  45. return tok == IDENTIFIER ? tk.tk_idf : (struct idf *)0;
  46. }
  47. /* domacro() is the control line interpreter. The '#' has already
  48. been read by the lexical analyzer by which domacro() is called.
  49. The token appearing directly after the '#' is obtained by calling
  50. the basic lexical analyzing function GetToken() and is interpreted
  51. to perform the action belonging to that token.
  52. An error message is produced when the token is not recognized,
  53. i.e. it is not one of "define" .. "undef" , integer or newline.
  54. */
  55. EXPORT
  56. domacro()
  57. {
  58. struct token tk; /* the token itself */
  59. EoiForNewline = 1;
  60. SkipEscNewline = 1;
  61. switch(GetToken(&tk)) { /* select control line action */
  62. case IDENTIFIER: /* is it a macro keyword? */
  63. switch (tk.tk_idf->id_resmac) {
  64. case K_DEFINE: /* "define" */
  65. do_define();
  66. break;
  67. case K_ELIF: /* "elif" */
  68. do_elif();
  69. break;
  70. case K_ELSE: /* "else" */
  71. do_else();
  72. break;
  73. case K_ENDIF: /* "endif" */
  74. do_endif();
  75. break;
  76. case K_IF: /* "if" */
  77. do_if();
  78. break;
  79. case K_IFDEF: /* "ifdef" */
  80. do_ifdef(1);
  81. break;
  82. case K_IFNDEF: /* "ifndef" */
  83. do_ifdef(0);
  84. break;
  85. case K_INCLUDE: /* "include" */
  86. do_include();
  87. break;
  88. case K_LINE: /* "line" */
  89. /* set LineNumber and FileName according to
  90. the arguments.
  91. */
  92. if (GetToken(&tk) != INTEGER) {
  93. lexerror("#line without linenumber");
  94. SkipRestOfLine();
  95. }
  96. else
  97. do_line((unsigned int)tk.tk_ival);
  98. break;
  99. case K_UNDEF: /* "undef" */
  100. do_undef();
  101. break;
  102. default:
  103. /* invalid word seen after the '#' */
  104. lexerror("%s: unknown control", tk.tk_idf->id_text);
  105. SkipRestOfLine();
  106. }
  107. break;
  108. case INTEGER: /* # <integer> [<filespecifier>]? */
  109. do_line((unsigned int)tk.tk_ival);
  110. break;
  111. case EOI: /* only `#' on this line: do nothing, ignore */
  112. break;
  113. default: /* invalid token following '#' */
  114. lexerror("illegal # line");
  115. SkipRestOfLine();
  116. }
  117. EoiForNewline = 0;
  118. SkipEscNewline = 0;
  119. }
  120. PRIVATE
  121. skip_block()
  122. {
  123. /* skip_block() skips the input from
  124. 1) a false #if, #ifdef, #ifndef or #elif until the
  125. corresponding #elif (resulting in true), #else or
  126. #endif is read.
  127. 2) a #else corresponding to a true #if, #ifdef,
  128. #ifndef or #elif until the corresponding #endif is
  129. seen.
  130. */
  131. register int ch;
  132. register int skiplevel = nestlevel; /* current nesting level */
  133. struct token tk;
  134. NoUnstack++;
  135. for (;;) {
  136. LoadChar(ch); /* read first character after newline */
  137. if (ch != '#') {
  138. if (ch == EOI) {
  139. NoUnstack--;
  140. return;
  141. }
  142. SkipRestOfLine();
  143. continue;
  144. }
  145. if (GetToken(&tk) != IDENTIFIER) {
  146. SkipRestOfLine();
  147. continue;
  148. }
  149. /* an IDENTIFIER: look for #if, #ifdef and #ifndef
  150. without interpreting them.
  151. Interpret #else, #elif and #endif if they occur
  152. on the same level.
  153. */
  154. switch(tk.tk_idf->id_resmac) {
  155. case K_IF:
  156. case K_IFDEF:
  157. case K_IFNDEF:
  158. push_if();
  159. break;
  160. case K_ELIF:
  161. if (nestlevel == skiplevel) {
  162. nestlevel--;
  163. push_if();
  164. if (ifexpr()) {
  165. NoUnstack--;
  166. return;
  167. }
  168. }
  169. break;
  170. case K_ELSE:
  171. ++(ifstack[nestlevel]);
  172. if (nestlevel == skiplevel) {
  173. SkipRestOfLine();
  174. NoUnstack--;
  175. return;
  176. }
  177. break;
  178. case K_ENDIF:
  179. ASSERT(nestlevel >= 0);
  180. if (nestlevel == skiplevel) {
  181. SkipRestOfLine();
  182. nestlevel--;
  183. NoUnstack--;
  184. return;
  185. }
  186. nestlevel--;
  187. break;
  188. }
  189. }
  190. }
  191. PRIVATE
  192. ifexpr()
  193. {
  194. /* ifexpr() returns whether the restricted constant
  195. expression following #if or #elif evaluates to true. This
  196. is done by calling the LLgen generated subparser for
  197. constant expressions. The result of this expression will
  198. be given in the extern long variable "ifval".
  199. */
  200. IMPORT arith ifval;
  201. int errors = err_occurred;
  202. ifval = (arith)0;
  203. AccDefined = 1;
  204. UnknownIdIsZero = 1;
  205. PushLex(); /* NEW parser */
  206. If_expr(); /* invoke constant expression parser */
  207. PopLex(); /* OLD parser */
  208. AccDefined = 0;
  209. UnknownIdIsZero = 0;
  210. return (errors == err_occurred) && (ifval != (arith)0);
  211. }
  212. PRIVATE
  213. do_include()
  214. {
  215. /* do_include() performs the inclusion of a file.
  216. */
  217. char *filenm;
  218. char *result;
  219. int tok;
  220. struct token tk;
  221. AccFileSpecifier = 1;
  222. if (((tok = GetToken(&tk)) == FILESPECIFIER) || tok == STRING)
  223. filenm = tk.tk_bts;
  224. else {
  225. lexerror("bad include syntax");
  226. filenm = (char *)0;
  227. }
  228. AccFileSpecifier = 0;
  229. SkipRestOfLine();
  230. inctable[0] = WorkingDir;
  231. if (filenm) {
  232. if (!InsertFile(filenm, &inctable[tok==FILESPECIFIER],&result)){
  233. lexerror("cannot find include file \"%s\"", filenm);
  234. }
  235. else {
  236. WorkingDir = getwdir(result);
  237. File_Inserted = 1;
  238. FileName = result;
  239. LineNumber = 0;
  240. }
  241. }
  242. }
  243. PRIVATE
  244. do_define()
  245. {
  246. /* do_define() interprets a #define control line.
  247. */
  248. struct idf *id; /* the #defined identifier's descriptor */
  249. int nformals = -1; /* keep track of the number of formals */
  250. char *formals[NPARAMS]; /* pointers to the names of the formals */
  251. char parbuf[PARBUFSIZE]; /* names of formals */
  252. char *repl_text; /* start of the replacement text */
  253. int length; /* length of the replacement text */
  254. register ch;
  255. char *get_text();
  256. /* read the #defined macro's name */
  257. if (!(id = GetIdentifier())) {
  258. lexerror("#define: illegal macro name");
  259. SkipRestOfLine();
  260. return;
  261. }
  262. /* there is a formal parameter list if the identifier is
  263. followed immediately by a '('.
  264. */
  265. LoadChar(ch);
  266. if (ch == '(') {
  267. if ((nformals = getparams(formals, parbuf)) == -1) {
  268. SkipRestOfLine();
  269. return; /* an error occurred */
  270. }
  271. LoadChar(ch);
  272. }
  273. /* read the replacement text if there is any */
  274. ch = skipspaces(ch); /* find first character of the text */
  275. ASSERT(ch != EOI);
  276. if (class(ch) == STNL) {
  277. /* Treat `#define something' as `#define something ""'
  278. */
  279. repl_text = "";
  280. length = 0;
  281. }
  282. else {
  283. PushBack();
  284. repl_text = get_text((nformals > 0) ? formals : 0, &length);
  285. }
  286. macro_def(id, repl_text, nformals, length, NOFLAG);
  287. LineNumber++;
  288. }
  289. PRIVATE
  290. push_if()
  291. {
  292. if (nestlevel >= IFDEPTH)
  293. fatal("too many nested #if/#ifdef/#ifndef");
  294. else
  295. ifstack[++nestlevel] = 0;
  296. }
  297. PRIVATE
  298. do_elif()
  299. {
  300. if (nestlevel < 0 || (ifstack[nestlevel])) {
  301. lexerror("#elif without corresponding #if");
  302. SkipRestOfLine();
  303. }
  304. else { /* restart at this level as if a #if is detected. */
  305. nestlevel--;
  306. push_if();
  307. skip_block();
  308. }
  309. }
  310. PRIVATE
  311. do_else()
  312. {
  313. SkipRestOfLine();
  314. if (nestlevel < 0 || (ifstack[nestlevel]))
  315. lexerror("#else without corresponding #if");
  316. else { /* mark this level as else-d */
  317. ++(ifstack[nestlevel]);
  318. skip_block();
  319. }
  320. }
  321. PRIVATE
  322. do_endif()
  323. {
  324. SkipRestOfLine();
  325. if (nestlevel < 0) {
  326. lexerror("#endif without corresponding #if");
  327. }
  328. else nestlevel--;
  329. }
  330. PRIVATE
  331. do_if()
  332. {
  333. push_if();
  334. if (!ifexpr()) /* a false #if/#elif expression */
  335. skip_block();
  336. }
  337. PRIVATE
  338. do_ifdef(how)
  339. {
  340. register struct idf *id;
  341. /* how == 1 : ifdef; how == 0 : ifndef
  342. */
  343. push_if();
  344. if (!(id = GetIdentifier()))
  345. lexerror("illegal #ifdef construction");
  346. /* The next test is a shorthand for:
  347. (how && !id->id_macro) || (!how && id->id_macro)
  348. */
  349. if (how ^ (id && id->id_macro != 0))
  350. skip_block();
  351. else
  352. SkipRestOfLine();
  353. }
  354. PRIVATE
  355. do_undef()
  356. {
  357. register struct idf *id;
  358. /* Forget a macro definition. */
  359. if (id = GetIdentifier()) {
  360. if (id->id_macro) { /* forget the macro */
  361. free_macro(id->id_macro);
  362. id->id_macro = (struct macro *) 0;
  363. } /* else: don't complain */
  364. }
  365. else
  366. lexerror("illegal #undef construction");
  367. SkipRestOfLine();
  368. }
  369. PRIVATE int
  370. getparams(buf, parbuf)
  371. char *buf[];
  372. char parbuf[];
  373. {
  374. /* getparams() reads the formal parameter list of a macro
  375. definition.
  376. The number of parameters is returned.
  377. As a formal parameter list is expected when calling this
  378. routine, -1 is returned if an error is detected, for
  379. example:
  380. #define one(1), where 1 is not an identifier.
  381. Note that the '(' has already been eaten.
  382. The names of the formal parameters are stored into parbuf.
  383. */
  384. register char **pbuf = &buf[0];
  385. register int c;
  386. register char *ptr = &parbuf[0];
  387. register char **pbuf2;
  388. LoadChar(c);
  389. c = skipspaces(c);
  390. if (c == ')') { /* no parameters: #define name() */
  391. *pbuf = (char *) 0;
  392. return 0;
  393. }
  394. for (;;) { /* eat the formal parameter list */
  395. if (class(c) != STIDF) { /* not an identifier */
  396. lexerror("#define: bad formal parameter");
  397. return -1;
  398. }
  399. *pbuf = ptr; /* name of the formal */
  400. *ptr++ = c;
  401. if (ptr >= &parbuf[PARBUFSIZE])
  402. fatal("formal parameter buffer overflow");
  403. do { /* eat the identifier name */
  404. LoadChar(c);
  405. *ptr++ = c;
  406. if (ptr >= &parbuf[PARBUFSIZE])
  407. fatal("formal parameter buffer overflow");
  408. } while (in_idf(c));
  409. *(ptr - 1) = '\0'; /* mark end of the name */
  410. /* Check if this formal parameter is already used.
  411. Usually, macros do not have many parameters, so ...
  412. */
  413. for (pbuf2 = pbuf - 1; pbuf2 >= &buf[0]; pbuf2--) {
  414. if (!strcmp(*pbuf2, *pbuf)) {
  415. warning("formal parameter \"%s\" already used",
  416. *pbuf);
  417. }
  418. }
  419. pbuf++;
  420. c = skipspaces(c);
  421. if (c == ')') { /* end of the formal parameter list */
  422. *pbuf = (char *) 0;
  423. return pbuf - buf;
  424. }
  425. if (c != ',') {
  426. lexerror("#define: bad formal parameter list");
  427. return -1;
  428. }
  429. LoadChar(c);
  430. c = skipspaces(c);
  431. }
  432. /*NOTREACHED*/
  433. }
  434. EXPORT
  435. macro_def(id, text, nformals, length, flags)
  436. register struct idf *id;
  437. char *text;
  438. {
  439. register struct macro *newdef = id->id_macro;
  440. /* macro_def() puts the contents and information of a macro
  441. definition into a structure and stores it into the symbol
  442. table entry belonging to the name of the macro.
  443. A warning is given if the definition overwrites another.
  444. */
  445. if (newdef) { /* is there a redefinition? */
  446. if (macroeq(newdef->mc_text, text))
  447. return;
  448. lexwarning("redefine \"%s\"", id->id_text);
  449. }
  450. else
  451. id->id_macro = newdef = new_macro();
  452. newdef->mc_text = text; /* replacement text */
  453. newdef->mc_nps = nformals; /* nr of formals */
  454. newdef->mc_length = length; /* length of repl. text */
  455. newdef->mc_flag = flags; /* special flags */
  456. newdef->mc_count = 0;
  457. }
  458. PRIVATE int
  459. find_name(nm, index)
  460. char *nm, *index[];
  461. {
  462. /* find_name() returns the index of "nm" in the namelist
  463. "index" if it can be found there. 0 is returned if it is
  464. not there.
  465. */
  466. register char **ip = &index[0];
  467. while (*ip)
  468. if (strcmp(nm, *ip++) == 0)
  469. return ip - &index[0];
  470. /* arrived here, nm is not in the name list. */
  471. return 0;
  472. }
  473. PRIVATE char *
  474. get_text(formals, length)
  475. char *formals[];
  476. int *length;
  477. {
  478. /* get_text() copies the replacement text of a macro
  479. definition with zero, one or more parameters, thereby
  480. substituting each formal parameter by a special character
  481. (non-ascii: 0200 & (order-number in the formal parameter
  482. list)) in order to substitute this character later by the
  483. actual parameter. The replacement text is copied into
  484. itself because the copied text will contain fewer or the
  485. same amount of characters. The length of the replacement
  486. text is returned.
  487. Implementation:
  488. finite automaton : we are only interested in
  489. identifiers, because they might be replaced by some actual
  490. parameter. Other tokens will not be seen as such.
  491. */
  492. register int c;
  493. register int text_size;
  494. char *text = Malloc(text_size = ITEXTSIZE);
  495. register int pos = 0;
  496. LoadChar(c);
  497. while ((c != EOI) && (class(c) != STNL)) {
  498. if (c == '\\') { /* check for "\\\n" */
  499. LoadChar(c);
  500. if (c == '\n') {
  501. /* More than one line is used for the
  502. replacement text.
  503. Replace "\\\n" by " ".
  504. */
  505. text[pos++] = ' ';
  506. ++LineNumber;
  507. LoadChar(c);
  508. }
  509. else
  510. text[pos++] = '\\';
  511. if (pos == text_size)
  512. text = Srealloc(text, text_size += RTEXTSIZE);
  513. }
  514. else
  515. if ( c == '/') {
  516. LoadChar(c);
  517. if (c == '*') {
  518. skipcomment();
  519. text[pos++] = ' ';
  520. LoadChar(c);
  521. }
  522. else
  523. text[pos++] = '/';
  524. if (pos == text_size)
  525. text = Srealloc(text, text_size += RTEXTSIZE);
  526. }
  527. else
  528. if (formals && class(c) == STIDF) {
  529. char id_buf[IDFSIZE + 1];
  530. register id_size = 0;
  531. register n;
  532. /* read identifier: it may be a formal parameter */
  533. id_buf[id_size++] = c;
  534. do {
  535. LoadChar(c);
  536. if (id_size <= IDFSIZE)
  537. id_buf[id_size++] = c;
  538. } while (in_idf(c));
  539. id_buf[--id_size] = '\0';
  540. if (n = find_name(id_buf, formals)) {
  541. /* construct the formal parameter mark */
  542. text[pos++] = FORMALP | (char) n;
  543. if (pos == text_size)
  544. text = Srealloc(text,
  545. text_size += RTEXTSIZE);
  546. }
  547. else {
  548. register char *ptr = &id_buf[0];
  549. while (pos + id_size >= text_size)
  550. text = Srealloc(text,
  551. text_size += RTEXTSIZE);
  552. while (text[pos++] = *ptr++) ;
  553. pos--;
  554. }
  555. }
  556. else {
  557. text[pos++] = c;
  558. if (pos == text_size)
  559. text = Srealloc(text, text_size += RTEXTSIZE);
  560. LoadChar(c);
  561. }
  562. }
  563. text[pos++] = '\0';
  564. *length = pos - 1;
  565. return text;
  566. }
  567. #define BLANK(ch) ((ch == ' ') || (ch == '\t'))
  568. /* macroeq() decides whether two macro replacement texts are
  569. identical. This version compares the texts, which occur
  570. as strings, without taking care of the leading and trailing
  571. blanks (spaces and tabs).
  572. */
  573. PRIVATE
  574. macroeq(s, t)
  575. register char *s, *t;
  576. {
  577. /* skip leading spaces */
  578. while (BLANK(*s)) s++;
  579. while (BLANK(*t)) t++;
  580. /* first non-blank encountered in both strings */
  581. /* The actual comparison loop: */
  582. while (*s && *s == *t)
  583. s++, t++;
  584. /* two cases are possible when arrived here: */
  585. if (*s == '\0') { /* *s == '\0' */
  586. while (BLANK(*t)) t++;
  587. return *t == '\0';
  588. }
  589. else { /* *s != *t */
  590. while (BLANK(*s)) s++;
  591. while (BLANK(*t)) t++;
  592. return (*s == '\0') && (*t == '\0');
  593. }
  594. }
  595. #else NOPP
  596. EXPORT
  597. domacro()
  598. {
  599. int tok;
  600. struct token tk;
  601. EoiForNewline = 1;
  602. SkipEscNewline = 1;
  603. if ((tok = GetToken(&tk)) == IDENTIFIER) {
  604. if (strcmp(tk.tk_idf->id_text, "line") != 0) {
  605. error("illegal # line");
  606. SkipRestOfLine();
  607. return;
  608. }
  609. tok = GetToken(&tk);
  610. }
  611. if (tok != INTEGER) {
  612. error("illegal # line");
  613. SkipRestOfLine();
  614. return;
  615. }
  616. do_line((unsigned int) tk.tk_ival);
  617. EoiForNewline = 0;
  618. SkipEscNewline = 0;
  619. }
  620. #endif NOPP
  621. PRIVATE
  622. SkipRestOfLine()
  623. {
  624. /* we do a PushBack because we don't want to skip the next line
  625. if the last character was a newline
  626. */
  627. PushBack();
  628. skipline();
  629. }
  630. PRIVATE
  631. do_line(l)
  632. unsigned int l;
  633. {
  634. struct token tk;
  635. LineNumber = l - 1; /* the number of the next input line */
  636. if (GetToken(&tk) == STRING) /* is there a filespecifier? */
  637. FileName = tk.tk_bts;
  638. SkipRestOfLine();
  639. }