lookup.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* L O O K U P R O U T I N E S */
  2. #include <em_arith.h>
  3. #include <em_label.h>
  4. #include "LLlex.h"
  5. #include "def.h"
  6. #include "idf.h"
  7. #include "misc.h"
  8. #include "node.h"
  9. #include "scope.h"
  10. #include "type.h"
  11. struct def *
  12. lookup(id, scope)
  13. register struct idf *id;
  14. struct scope *scope;
  15. {
  16. /* Look up a definition of an identifier in scope "scope".
  17. Make the "def" list self-organizing.
  18. Return a pointer to its "def" structure if it exists,
  19. otherwise return 0.
  20. */
  21. register struct def *df, *df1;
  22. /* Look in the chain of definitions of this "id" for one with scope
  23. "scope".
  24. */
  25. for( df = id->id_def, df1 = 0;
  26. df && df->df_scope != scope;
  27. df1 = df, df = df->df_next ) { /* nothing */ }
  28. if( df && df1 ) {
  29. /* Put the definition in front
  30. */
  31. df1->df_next = df->df_next;
  32. df->df_next = id->id_def;
  33. id->id_def = df;
  34. }
  35. return df;
  36. }
  37. struct def *
  38. lookfor(id, vis, give_error)
  39. register struct node *id;
  40. struct scopelist *vis;
  41. {
  42. /* Look for an identifier in the visibility range started by "vis".
  43. If it is not defined create a dummy definition and
  44. if give_error is set, give an error message.
  45. */
  46. register struct def *df;
  47. register struct scopelist *sc = vis;
  48. while( sc ) {
  49. df = lookup(id->nd_IDF, sc->sc_scope);
  50. if( df ) return df;
  51. sc = nextvisible(sc);
  52. }
  53. if( give_error ) id_not_declared(id);
  54. df = MkDef(id->nd_IDF, vis->sc_scope, D_ERROR);
  55. return df;
  56. }