scope.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* S C O P E M E C H A N I S M */
  2. #include "debug.h"
  3. #include <alloc.h>
  4. #include <assert.h>
  5. #include <em_arith.h>
  6. #include <em_label.h>
  7. #include "LLlex.h"
  8. #include "def.h"
  9. #include "idf.h"
  10. #include "misc.h"
  11. #include "node.h"
  12. #include "scope.h"
  13. #include "type.h"
  14. struct scope *GlobalScope, *PervasiveScope, *BlockScope;
  15. struct scopelist *CurrVis;
  16. extern int proclevel; /* declared in declar.g */
  17. static int sccount;
  18. InitScope()
  19. {
  20. register struct scope *sc = new_scope();
  21. register struct scopelist *ls = new_scopelist();
  22. sc->sc_level = proclevel;
  23. PervasiveScope = sc;
  24. ls->sc_scope = PervasiveScope;
  25. ls->sc_count = ++sccount;
  26. CurrVis = ls;
  27. }
  28. open_scope()
  29. {
  30. register struct scope *sc = new_scope();
  31. register struct scopelist *ls = new_scopelist();
  32. sc->sc_level = proclevel;
  33. ls->sc_scope = sc;
  34. ls->next = CurrVis;
  35. ls->sc_count = ++sccount;
  36. CurrVis = ls;
  37. }
  38. close_scope(doclean)
  39. {
  40. /* When this procedure is called, the next visible scope is equal to
  41. the statically enclosing scope
  42. */
  43. register struct def *df;
  44. assert(CurrentScope != 0);
  45. df = CurrentScope->sc_def;
  46. if (doclean) while (df) {
  47. struct def *next = df->df_nextinscope;
  48. if (! (df->df_flags & (D_VARPAR|D_VALPAR))) remove_def(df);
  49. df = next;
  50. }
  51. CurrVis = CurrVis->next;
  52. }
  53. Forward(nd, tp)
  54. register struct node *nd;
  55. register struct type *tp;
  56. {
  57. /* Enter a forward reference into the current scope. This is
  58. * used in pointertypes.
  59. */
  60. register struct def *df = define(nd->nd_IDF, CurrentScope, D_FORWTYPE);
  61. register struct forwtype *fw_type = new_forwtype();
  62. fw_type->f_next = df->df_fortype;
  63. df->df_fortype = fw_type;
  64. fw_type->f_node = nd;
  65. fw_type->f_type = tp;
  66. }
  67. chk_prog_params()
  68. {
  69. /* the program parameters must be global variables of some file type */
  70. register struct def *df = CurrentScope->sc_def;
  71. while( df ) {
  72. if( df->df_kind & D_PARAMETER ) {
  73. if( !is_anon_idf(df->df_idf) ) {
  74. if( df->df_type == error_type )
  75. error("program parameter \"%s\" must be a global variable",
  76. df->df_idf->id_text);
  77. else if( df->df_type->tp_fund != T_FILE )
  78. error("program parameter \"%s\" must have a file type",
  79. df->df_idf->id_text);
  80. df->df_kind = D_VARIABLE;
  81. }
  82. else df->df_kind = D_ERROR;
  83. }
  84. df = df->df_nextinscope;
  85. }
  86. }
  87. chk_directives()
  88. {
  89. /* check if all forward declarations are defined */
  90. register struct def *df = CurrentScope->sc_def;
  91. while( df ) {
  92. if( df->df_kind == D_FWPROCEDURE )
  93. error("procedure \"%s\" not defined", df->df_idf->id_text);
  94. else if( df->df_kind == D_FWFUNCTION )
  95. error("function \"%s\" not defined", df->df_idf->id_text);
  96. df = df->df_nextinscope;
  97. }
  98. }