scope.C 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* S C O P E M E C H A N I S M */
  8. /* $Id$ */
  9. #include "debug.h"
  10. #include <assert.h>
  11. #include <alloc.h>
  12. #include <em_arith.h>
  13. #include <em_label.h>
  14. #include "LLlex.h"
  15. #include "idf.h"
  16. #include "scope.h"
  17. #include "type.h"
  18. #include "def.h"
  19. #include "node.h"
  20. t_scope *PervasiveScope;
  21. t_scopelist *CurrVis, *GlobalVis;
  22. extern int proclevel;
  23. extern char options[];
  24. /* STATICALLOCDEF "scope" 10 */
  25. /* STATICALLOCDEF "scopelist" 10 */
  26. static int sc_count;
  27. open_scope(scopetype)
  28. {
  29. /* Open a scope that is either open (automatic imports) or closed.
  30. */
  31. register t_scope *sc = new_scope();
  32. register t_scopelist *ls = new_scopelist();
  33. assert(scopetype == OPENSCOPE || scopetype == CLOSEDSCOPE);
  34. sc->sc_scopeclosed = scopetype == CLOSEDSCOPE;
  35. sc->sc_level = proclevel;
  36. ls->sc_scope = sc;
  37. ls->sc_encl = CurrVis;
  38. if (! sc->sc_scopeclosed) {
  39. ls->sc_next = ls->sc_encl;
  40. }
  41. ls->sc_count = sc_count++;
  42. CurrVis = ls;
  43. }
  44. t_scope *
  45. open_and_close_scope(scopetype)
  46. {
  47. t_scope *sc;
  48. open_scope(scopetype);
  49. sc = CurrentScope;
  50. close_scope(0);
  51. return sc;
  52. }
  53. InitScope()
  54. {
  55. register t_scope *sc = new_scope();
  56. register t_scopelist *ls = new_scopelist();
  57. sc->sc_level = proclevel;
  58. PervasiveScope = sc;
  59. ls->sc_scope = PervasiveScope;
  60. CurrVis = ls;
  61. }
  62. STATIC
  63. chk_proc(df)
  64. register t_def *df;
  65. {
  66. /* Called at scope closing. Check all definitions, and if one
  67. is a D_PROCHEAD, the procedure was not defined.
  68. Also check that hidden types are defined.
  69. */
  70. while (df) {
  71. if (df->df_kind == D_HIDDEN) {
  72. error("hidden type \"%s\" not declared",
  73. df->df_idf->id_text);
  74. }
  75. else if (df->df_kind == D_PROCHEAD) {
  76. /* A not defined procedure
  77. */
  78. error("procedure \"%s\" not defined",
  79. df->df_idf->id_text);
  80. FreeNode(df->for_node);
  81. }
  82. df = df->df_nextinscope;
  83. }
  84. }
  85. STATIC
  86. chk_forw(pdf)
  87. t_def **pdf;
  88. {
  89. /* Called at scope close. Look for all forward definitions and
  90. if the scope was a closed scope, give an error message for
  91. them, and otherwise move them to the enclosing scope.
  92. */
  93. register t_def *df;
  94. while (df = *pdf) {
  95. if (df->df_kind == D_FORWTYPE) {
  96. pdf = &df->df_nextinscope;
  97. ForceForwardTypeDef(df); /* removes df */
  98. continue;
  99. }
  100. if (df->df_kind & (D_FORWARD|D_FORWMODULE)) {
  101. /* These definitions must be found in
  102. the enclosing closed scope, which of course
  103. may be the scope that is now closed!
  104. */
  105. if (scopeclosed(CurrentScope)) {
  106. /* Indeed, the scope was a closed
  107. scope, so give error message
  108. */
  109. node_error(df->for_node, "identifier \"%s\" not declared",
  110. df->df_idf->id_text);
  111. }
  112. else {
  113. /* This scope was an open scope.
  114. Maybe the definitions are in the
  115. enclosing scope?
  116. */
  117. register t_scopelist *ls =
  118. nextvisible(CurrVis);
  119. register t_def *df1 = lookup(df->df_idf, ls->sc_scope, 0, 0);
  120. *pdf = df->df_nextinscope;
  121. if (! df1) {
  122. if (df->df_kind == D_FORWMODULE) {
  123. df->for_vis->sc_next = ls;
  124. }
  125. df->df_nextinscope = ls->sc_scope->sc_def;
  126. ls->sc_scope->sc_def = df;
  127. df->df_scope = ls->sc_scope;
  128. continue;
  129. }
  130. /* leave it like this ??? */
  131. }
  132. FreeNode(df->for_node);
  133. }
  134. pdf = &df->df_nextinscope;
  135. }
  136. }
  137. Reverse(pdf)
  138. t_def **pdf;
  139. {
  140. /* Reverse the order in the list of definitions in a scope.
  141. This is neccesary because this list is built in reverse.
  142. Also, while we're at it, remove uninteresting definitions
  143. from this list.
  144. */
  145. register t_def *df, *df1;
  146. #define INTERESTING (D_MODULE|D_PROCEDURE|D_PROCHEAD|D_VARIABLE|D_IMPORTED|D_TYPE|D_CONST|D_FIELD)
  147. df = 0;
  148. df1 = *pdf;
  149. while (df1) {
  150. if (df1->df_kind & INTERESTING) {
  151. t_def *prev = df;
  152. df = df1;
  153. df1 = df1->df_nextinscope;
  154. df->df_nextinscope = prev;
  155. }
  156. else df1 = df1->df_nextinscope;
  157. }
  158. *pdf = df;
  159. }
  160. close_scope(flag)
  161. register int flag;
  162. {
  163. /* Close a scope. If "flag" is set, check for forward declarations,
  164. either POINTER declarations, or EXPORTs, or forward references
  165. to MODULES
  166. */
  167. register t_scope *sc = CurrentScope;
  168. assert(sc != 0);
  169. FreeNode(sc->sc_end);
  170. sc->sc_end = dot2leaf(Link);
  171. if (flag) {
  172. DO_DEBUG(options['S'],(print("List of definitions in currently ended scope:\n"), DumpScope(sc->sc_def)));
  173. if (flag & SC_CHKPROC) chk_proc(sc->sc_def);
  174. if (flag & SC_CHKFORW) chk_forw(&(sc->sc_def));
  175. if (flag & SC_REVERSE) Reverse(&(sc->sc_def));
  176. }
  177. CurrVis = enclosing(CurrVis);
  178. }
  179. #ifdef DEBUG
  180. DumpScope(df)
  181. register t_def *df;
  182. {
  183. while (df) {
  184. PrDef(df);
  185. df = df->df_nextinscope;
  186. }
  187. }
  188. #endif