replace.c 5.6 KB

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