replace.c 5.3 KB

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