lookup.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* L O O K U P R O U T I N E S */
  8. /* $Id$ */
  9. #include "debug.h"
  10. #include <em_arith.h>
  11. #include <em_label.h>
  12. #include <assert.h>
  13. #include "LLlex.h"
  14. #include "def.h"
  15. #include "idf.h"
  16. #include "scope.h"
  17. #include "node.h"
  18. #include "type.h"
  19. #include "misc.h"
  20. extern int pass_1;
  21. #ifdef DEBUG
  22. extern char options[];
  23. #endif
  24. t_def *
  25. lookup(id, scope, import, flags)
  26. register t_idf *id;
  27. t_scope *scope;
  28. {
  29. /* Look up a definition of an identifier in scope "scope".
  30. Make the "def" list self-organizing.
  31. Return a pointer to its "def" structure if it exists,
  32. otherwise return 0.
  33. */
  34. register t_def *df, *df1;
  35. /* Look in the chain of definitions of this "id" for one with scope
  36. "scope".
  37. */
  38. for (df = id->id_def, df1 = 0;
  39. df && df->df_scope != scope;
  40. df1 = df, df = df->df_next) { /* nothing */ }
  41. if (! df && import && scopeclosed(scope)) {
  42. for (df = id->id_def, df1 = 0;
  43. df && df->df_scope != PervasiveScope;
  44. df1 = df, df = df->df_next) { /* nothing */ }
  45. }
  46. if (df) {
  47. /* Found it
  48. */
  49. if (df1) {
  50. /* Put the definition in front
  51. */
  52. df1->df_next = df->df_next;
  53. df->df_next = id->id_def;
  54. id->id_def = df;
  55. }
  56. df->df_flags |= flags;
  57. while (df->df_kind & import) {
  58. assert(df->imp_def != 0);
  59. df = df->imp_def;
  60. }
  61. DO_DEBUG(options['S'], print("lookup %s, %x\n", id->id_text, df->df_kind));
  62. }
  63. return df;
  64. }
  65. t_def *
  66. lookfor(id, vis, message, flags)
  67. register t_node *id;
  68. register t_scopelist *vis;
  69. {
  70. /* Look for an identifier in the visibility range started by "vis".
  71. If it is not defined create a dummy definition and,
  72. if message is set, give an error message
  73. */
  74. register t_scopelist *sc;
  75. t_scopelist *sc1 = 0;
  76. t_def *df;
  77. for (sc = vis; sc; sc = nextvisible(sc)) {
  78. df = lookup(id->nd_IDF, sc->sc_scope, D_IMPORTED, flags);
  79. if (df) {
  80. if (message && df->df_kind == D_FORWARD) {
  81. if (! sc1) sc1 = sc;
  82. while (sc && sc->sc_scope != df->df_scope) {
  83. sc = enclosing(sc);
  84. }
  85. if (sc) continue;
  86. break;
  87. }
  88. if (pass_1 && message) {
  89. if (sc1) sc = sc1;
  90. while (vis->sc_scope->sc_level >
  91. sc->sc_scope->sc_level ||
  92. (sc1 &&
  93. vis->sc_scope->sc_level >=
  94. sc->sc_scope->sc_level)) {
  95. define( id->nd_IDF,
  96. vis->sc_scope,
  97. D_INUSE)-> imp_def = df;
  98. vis = enclosing(vis);
  99. }
  100. }
  101. return df;
  102. }
  103. }
  104. if (message) id_not_declared(id);
  105. return MkDef(id->nd_IDF, vis->sc_scope, D_ERROR);
  106. }