replace.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* $Header$ */
  2. /* PREPROCESSOR: MACRO-TEXT REPLACEMENT ROUTINES */
  3. #include "nopp.h"
  4. #ifndef NOPP
  5. #include "debug.h" /* UF */
  6. #include "pathlength.h" /* UF */
  7. #include "strsize.h" /* UF */
  8. #include <alloc.h>
  9. #include "idf.h"
  10. #include "input.h"
  11. #include "macro.h"
  12. #include "arith.h"
  13. #include "LLlex.h"
  14. #include "class.h"
  15. #include "assert.h"
  16. #include "interface.h"
  17. #include "static.h"
  18. char *strcpy(), *strcat();
  19. char *long2str();
  20. PRIVATE struct macro *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. int size;
  39. if (mac->mc_flag & NOREPLACE) {
  40. lexwarning("macro %s is recursive", idef->id_text);
  41. return 0;
  42. }
  43. if (mac->mc_nps != -1) { /* with parameter list */
  44. if (mac->mc_flag & FUNC) {
  45. /* must be "defined".
  46. Unfortunately, the next assertion
  47. will not compile ...
  48. ASSERT( ! strcmp("defined", idef->id_text));
  49. */
  50. if (! AccDefined)
  51. return 0;
  52. }
  53. if (++mac->mc_count > 100) {
  54. /* 100 must be some number in Parameters */
  55. lexwarning("macro %s is assumed recursive",
  56. idef->id_text);
  57. return 0;
  58. }
  59. LoadChar(c);
  60. c = skipspaces(c);
  61. if (c != '(') { /* no replacement if no () */
  62. lexerror("(warning) macro %s needs arguments",
  63. idef->id_text);
  64. PushBack();
  65. return 0;
  66. }
  67. actpars = getactuals(idef); /* get act.param. list */
  68. if (mac->mc_flag & FUNC) {
  69. struct idf *param = str2idf(*actpars);
  70. if (param->id_macro)
  71. reptext = "1";
  72. else
  73. reptext = "0";
  74. InsertText(reptext, 1);
  75. mac->next = ReplaceList;
  76. ReplaceList = mac;
  77. return 1;
  78. }
  79. }
  80. if (mac->mc_flag & FUNC) /* this macro leads to special action */
  81. macro_func(idef);
  82. if (mac->mc_nps <= 0)
  83. mac->mc_flag |= NOREPLACE;
  84. reptext = macro2buffer(idef, actpars, &size); /* create input buffer */
  85. InsertText(reptext, size);
  86. mac->next = ReplaceList;
  87. ReplaceList = mac;
  88. return 1;
  89. }
  90. GSTATIC char FilNamBuf[PATHLENGTH];
  91. PRIVATE
  92. macro_func(idef)
  93. register struct idf *idef;
  94. {
  95. /* macro_func() performs the special actions needed with some
  96. macros. These macros are __FILE__ and __LINE__ which
  97. replacement texts must be evaluated at the time they are
  98. used.
  99. */
  100. register struct macro *mac = idef->id_macro;
  101. switch (idef->id_text[2]) { /* This switch is very blunt... */
  102. case 'F' : /* __FILE__ */
  103. FilNamBuf[0] = '"';
  104. strcpy(&FilNamBuf[1], FileName);
  105. strcat(FilNamBuf, "\"");
  106. mac->mc_text = FilNamBuf;
  107. mac->mc_length = strlen(FilNamBuf);
  108. break;
  109. case 'L' : /* __LINE__ */
  110. mac->mc_text = long2str((long)LineNumber, 10);
  111. mac->mc_length = 1;
  112. break;
  113. default :
  114. crash("(macro_func)");
  115. }
  116. }
  117. PRIVATE char *
  118. macro2buffer(idef, actpars, siztext)
  119. struct idf *idef;
  120. char **actpars;
  121. int *siztext;
  122. {
  123. /* Macro2buffer() turns the macro replacement text, as it is
  124. stored, into an input buffer, while each occurrence of the
  125. non-ascii formal parameter mark is replaced by its
  126. corresponding actual parameter specified in the actual
  127. parameter list actpars. A pointer to the beginning of the
  128. constructed text is returned, while *siztext is filled
  129. with its length.
  130. If there are no parameters, this function behaves
  131. the same as strcpy().
  132. */
  133. register int size = 8;
  134. register char *text = Malloc(size);
  135. register int pos = 0;
  136. register char *ptr = idef->id_macro->mc_text;
  137. while (*ptr) {
  138. if (*ptr & FORMALP) { /* non-asc formal param. mark */
  139. register int n = *ptr++ & 0177;
  140. register char *p;
  141. ASSERT(n != 0);
  142. /* copy the text of the actual parameter
  143. into the replacement text
  144. */
  145. for (p = actpars[n - 1]; *p; p++) {
  146. text[pos++] = *p;
  147. if (pos == size)
  148. text = Srealloc(text, size += RSTRSIZE);
  149. }
  150. }
  151. else {
  152. text[pos++] = *ptr++;
  153. if (pos == size)
  154. text = Srealloc(text, size += RSTRSIZE);
  155. }
  156. }
  157. text[pos] = '\0';
  158. *siztext = pos;
  159. return text;
  160. }
  161. EXPORT
  162. DoUnstack()
  163. {
  164. Unstacked++;
  165. }
  166. EXPORT
  167. EnableMacros()
  168. {
  169. register struct macro *p = ReplaceList;
  170. ASSERT(Unstacked > 0);
  171. while (Unstacked > 0) {
  172. ASSERT(p != 0);
  173. p->mc_flag &= ~NOREPLACE;
  174. p->mc_count = 0;
  175. p = p->next;
  176. Unstacked--;
  177. }
  178. ReplaceList = p;
  179. }
  180. #endif NOPP