lookup.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* L O O K U P R O U T I N E S */
  2. #include <alloc.h>
  3. #include <em_arith.h>
  4. #include <em_label.h>
  5. #include <assert.h>
  6. #include "LLlex.h"
  7. #include "def.h"
  8. #include "idf.h"
  9. #include "misc.h"
  10. #include "node.h"
  11. #include "scope.h"
  12. #include "type.h"
  13. remove_def(df)
  14. register struct def *df;
  15. {
  16. struct idf *id= df->df_idf;
  17. struct def *df1 = id->id_def;
  18. if( df1 == df ) id->id_def = df->df_next;
  19. else {
  20. while( df1 && df1->df_next != df ) df1 = df1->df_next;
  21. df1->df_next = df->df_next;
  22. }
  23. free_def(df);
  24. }
  25. struct def *
  26. lookup(id, scope, inuse)
  27. register struct idf *id;
  28. struct scope *scope;
  29. long inuse;
  30. {
  31. /* Look up a definition of an identifier in scope "scope".
  32. Make the "def" list self-organizing.
  33. Return a pointer to its "def" structure if it exists,
  34. otherwise return 0.
  35. */
  36. register struct def *df, *df1;
  37. /* Look in the chain of definitions of this "id" for one with scope
  38. "scope".
  39. */
  40. for( df = id->id_def, df1 = 0;
  41. df && df->df_scope != scope;
  42. df1 = df, df = df->df_next ) { /* nothing */ }
  43. if( df ) {
  44. /* Found it
  45. */
  46. if( df1) {
  47. /* Put the definition in front
  48. */
  49. df1->df_next = df->df_next;
  50. df->df_next = id->id_def;
  51. id->id_def = df;
  52. }
  53. while( df->df_kind & inuse ) {
  54. assert(df->usd_def != 0);
  55. df=df->usd_def;
  56. }
  57. }
  58. return df;
  59. }
  60. struct def *
  61. lookfor(id, vis, give_error)
  62. register struct node *id;
  63. struct scopelist *vis;
  64. {
  65. /* Look for an identifier in the visibility range started by "vis".
  66. If it is not defined create a dummy definition and
  67. if give_error is set, give an error message.
  68. */
  69. register struct def *df, *tmp_df;
  70. register struct scopelist *sc = vis;
  71. while( sc ) {
  72. df = lookup(id->nd_IDF, sc->sc_scope, D_INUSE);
  73. if( df ) {
  74. while( vis->sc_scope->sc_level >
  75. sc->sc_scope->sc_level ) {
  76. if( tmp_df = define(id->nd_IDF, vis->sc_scope,
  77. D_INUSE))
  78. tmp_df->usd_def = df;
  79. vis = nextvisible(vis);
  80. }
  81. /* Since the scope-level of standard procedures is the
  82. * same as for the user-defined procedures, the procedure
  83. * must be marked as used. Not doing so would mean that
  84. * such a procedure could redefined after usage.
  85. */
  86. if( (vis->sc_scope == GlobalScope) &&
  87. !lookup(id->nd_IDF, GlobalScope, D_INUSE) ) {
  88. if( tmp_df = define(id->nd_IDF, vis->sc_scope,
  89. D_INUSE))
  90. tmp_df->usd_def = df;
  91. }
  92. return df;
  93. }
  94. sc = nextvisible(sc);
  95. }
  96. if( give_error ) id_not_declared(id);
  97. df = MkDef(id->nd_IDF, vis->sc_scope, D_ERROR);
  98. return df;
  99. }