replace.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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: MACRO-TEXT REPLACEMENT ROUTINES */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "nopp.h"
  11. #ifndef NOPP
  12. #include "debug.h" /* UF */
  13. #include "pathlength.h" /* UF */
  14. #include "strsize.h" /* UF */
  15. #include <alloc.h>
  16. #include "idf.h"
  17. #include "input.h"
  18. #include "macro.h"
  19. #include "arith.h"
  20. #include "LLlex.h"
  21. #include "class.h"
  22. #include "assert.h"
  23. #include "interface.h"
  24. #include "static.h"
  25. char *strcpy(), *strcat();
  26. char *long2str();
  27. extern int InputLevel;
  28. PRIVATE struct mlist *ReplaceList; /* list of currently active macros */
  29. PRIVATE macro_func();
  30. PRIVATE char *macro2buffer();
  31. extern char **getactuals();
  32. EXPORT int
  33. replace(idef)
  34. register struct idf *idef;
  35. {
  36. /* replace() is called by the lexical analyzer to perform
  37. macro replacement. "idef" is the description of the
  38. identifier which leads to the replacement. If the
  39. optional actual parameters of the macro are OK, the text
  40. of the macro is prepared to serve as an input buffer,
  41. which is pushed onto the input stack.
  42. replace() returns 1 if the replacement succeeded and 0 if
  43. some error has occurred.
  44. */
  45. register struct macro *mac = idef->id_macro;
  46. register struct mlist *repl;
  47. register int c;
  48. char **actpars;
  49. char *reptext;
  50. int size;
  51. if (mac->mc_flag & NOREPLACE) {
  52. lexwarning("macro %s is recursive", idef->id_text);
  53. return 0;
  54. }
  55. if (mac->mc_nps != -1) { /* with parameter list */
  56. if (mac->mc_flag & FUNC) {
  57. /* must be "defined".
  58. Unfortunately, the next assertion
  59. will not compile ...
  60. ASSERT( ! strcmp("defined", idef->id_text));
  61. */
  62. if (! AccDefined)
  63. return 0;
  64. }
  65. if (++mac->mc_count > 100) {
  66. /* 100 must be some number in Parameters */
  67. lexwarning("macro %s is assumed recursive",
  68. idef->id_text);
  69. return 0;
  70. }
  71. LoadChar(c);
  72. c = skipspaces(c,! (mac->mc_flag & FUNC));
  73. if (c != '(') { /* no replacement if no () */
  74. PushBack();
  75. if (! (mac->mc_flag & FUNC)) {
  76. lexerror("(warning) macro %s needs arguments",
  77. idef->id_text);
  78. return 0;
  79. }
  80. }
  81. if (mac->mc_flag & FUNC) {
  82. struct idf *param;
  83. extern struct idf *GetIdentifier();
  84. UnknownIdIsZero = 0;
  85. param = GetIdentifier();
  86. UnknownIdIsZero = 1;
  87. if (c == '(') {
  88. LoadChar(c);
  89. c = skipspaces(c,0);
  90. if (c != ')') error(") missing");
  91. }
  92. if (! param) {
  93. error("identifier missing");
  94. }
  95. repl = new_mlist();
  96. if (param && param->id_macro)
  97. reptext = "1";
  98. else
  99. reptext = "0";
  100. InsertText(reptext, 1);
  101. InputLevel++;
  102. repl->m_level = InputLevel;
  103. repl->next = ReplaceList;
  104. ReplaceList = repl;
  105. repl->m_mac = mac;
  106. return 1;
  107. }
  108. actpars = getactuals(idef); /* get act.param. list */
  109. }
  110. repl = new_mlist();
  111. repl->m_mac = mac;
  112. if (mac->mc_flag & FUNC) /* this macro leads to special action */
  113. macro_func(idef);
  114. if (mac->mc_nps <= 0) {
  115. reptext = mac->mc_text;
  116. size = mac->mc_length;
  117. mac->mc_flag |= NOREPLACE;
  118. }
  119. else {
  120. reptext = macro2buffer(idef, actpars, &size); /* create input buffer */
  121. repl->m_repl = reptext;
  122. }
  123. InsertText(reptext, size);
  124. InputLevel++;
  125. repl->m_level = InputLevel;
  126. repl->next = ReplaceList;
  127. ReplaceList = repl;
  128. return 1;
  129. }
  130. GSTATIC char FilNamBuf[PATHLENGTH];
  131. PRIVATE
  132. macro_func(idef)
  133. register struct idf *idef;
  134. {
  135. /* macro_func() performs the special actions needed with some
  136. macros. These macros are __FILE__ and __LINE__ which
  137. replacement texts must be evaluated at the time they are
  138. used.
  139. */
  140. register struct macro *mac = idef->id_macro;
  141. switch (idef->id_text[2]) { /* This switch is very blunt... */
  142. case 'F' : /* __FILE__ */
  143. FilNamBuf[0] = '"';
  144. strcpy(&FilNamBuf[1], FileName);
  145. strcat(FilNamBuf, "\"");
  146. mac->mc_text = FilNamBuf;
  147. mac->mc_length = strlen(FilNamBuf);
  148. break;
  149. case 'L' : /* __LINE__ */
  150. mac->mc_text = long2str((long)LineNumber, 10);
  151. mac->mc_length = 1;
  152. break;
  153. default :
  154. crash("(macro_func)");
  155. /*NOTREACHED*/
  156. }
  157. }
  158. PRIVATE char *
  159. macro2buffer(idef, actpars, siztext)
  160. struct idf *idef;
  161. char **actpars;
  162. int *siztext;
  163. {
  164. /* Macro2buffer() turns the macro replacement text, as it is
  165. stored, into an input buffer, while each occurrence of the
  166. non-ascii formal parameter mark is replaced by its
  167. corresponding actual parameter specified in the actual
  168. parameter list actpars. A pointer to the beginning of the
  169. constructed text is returned, while *siztext is filled
  170. with its length.
  171. If there are no parameters, this function behaves
  172. the same as strcpy().
  173. */
  174. register int size = 8;
  175. register char *text = Malloc(size);
  176. register int pos = 0;
  177. register char *ptr = idef->id_macro->mc_text;
  178. while (*ptr) {
  179. if (*ptr & FORMALP) { /* non-asc formal param. mark */
  180. register int n = *ptr++ & 0177;
  181. register char *p;
  182. ASSERT(n != 0);
  183. /* copy the text of the actual parameter
  184. into the replacement text
  185. */
  186. for (p = actpars[n - 1]; *p; p++) {
  187. text[pos++] = *p;
  188. if (pos == size)
  189. text = Srealloc(text, size += RSTRSIZE);
  190. }
  191. }
  192. else {
  193. text[pos++] = *ptr++;
  194. if (pos == size)
  195. text = Srealloc(text, size += RSTRSIZE);
  196. }
  197. }
  198. text[pos] = '\0';
  199. *siztext = pos;
  200. return text;
  201. }
  202. EXPORT
  203. DoUnstack()
  204. {
  205. Unstacked = 1;
  206. }
  207. EXPORT
  208. EnableMacros()
  209. {
  210. register struct mlist *p = ReplaceList, *prev = 0;
  211. ASSERT(Unstacked > 0);
  212. while (p) {
  213. struct mlist *nxt = p->next;
  214. if (p->m_level > InputLevel) {
  215. p->m_mac->mc_flag &= ~NOREPLACE;
  216. if (p->m_mac->mc_count) p->m_mac->mc_count--;
  217. if (p->m_repl) free(p->m_repl);
  218. if (! prev) ReplaceList = nxt;
  219. else prev->next = nxt;
  220. free_mlist(p);
  221. }
  222. else prev = p;
  223. p = nxt;
  224. }
  225. Unstacked = 0;
  226. }
  227. #endif /* NOPP */