domacro.c 16 KB

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