scope.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. InitScope()
  18. {
  19. register struct scope *sc = new_scope();
  20. register struct scopelist *ls = new_scopelist();
  21. sc->sc_def = 0;
  22. sc->sc_level = proclevel;
  23. PervasiveScope = sc;
  24. ls->next = 0;
  25. ls->sc_scope = PervasiveScope;
  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. CurrVis = ls;
  36. }
  37. close_scope()
  38. {
  39. /* When this procedure is called, the next visible scope is equal to
  40. the statically enclosing scope
  41. */
  42. assert(CurrentScope != 0);
  43. CurrVis = CurrVis->next;
  44. }
  45. Forward(nd, tp)
  46. register struct node *nd;
  47. register struct type *tp;
  48. {
  49. /* Enter a forward reference into the current scope. This is
  50. * used in pointertypes.
  51. */
  52. register struct def *df = define(nd->nd_IDF, CurrentScope, D_FORWTYPE);
  53. register struct forwtype *fw_type = new_forwtype();
  54. fw_type->f_next = df->df_fortype;
  55. df->df_fortype = fw_type;
  56. fw_type->f_node = nd;
  57. fw_type->f_type = tp;
  58. }
  59. STATIC
  60. chk_prog_params()
  61. {
  62. /* the program parameters must be global variables of some file type */
  63. register struct def *df = CurrentScope->sc_def;
  64. while( df ) {
  65. if( df->df_kind & D_PARAMETER ) {
  66. if( !is_anon_idf(df->df_idf) ) {
  67. if( df->df_type == error_type )
  68. error("program parameter \"%s\" must be a global variable",
  69. df->df_idf->id_text);
  70. else if( df->df_type->tp_fund != T_FILE )
  71. error("program parameter \"%s\" must have a file type",
  72. df->df_idf->id_text);
  73. df->df_kind = D_VARIABLE;
  74. }
  75. else df->df_kind = D_ERROR;
  76. }
  77. df = df->df_nextinscope;
  78. }
  79. }
  80. STATIC
  81. chk_directives()
  82. {
  83. /* check if all forward declarations are defined */
  84. register struct def *df = CurrentScope->sc_def;
  85. while( df ) {
  86. if( df->df_kind == D_FWPROCEDURE )
  87. error("procedure \"%s\" not defined", df->df_idf->id_text);
  88. else if( df->df_kind == D_FWFUNCTION )
  89. error("function \"%s\" not defined", df->df_idf->id_text);
  90. df = df->df_nextinscope;
  91. }
  92. }