domacro.c 16 KB

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