domacro.c 15 KB

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