defmodule.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /* D E F I N I T I O N M O D U L E S */
  8. /* $Id$ */
  9. #include "debug.h"
  10. #include <assert.h>
  11. #include <em_arith.h>
  12. #include <em_label.h>
  13. #include <alloc.h>
  14. #include "idf.h"
  15. #include "input.h"
  16. #include "scope.h"
  17. #include "LLlex.h"
  18. #include "def.h"
  19. #include "Lpars.h"
  20. #include "f_info.h"
  21. #include "main.h"
  22. #include "node.h"
  23. #include "type.h"
  24. #include "misc.h"
  25. #ifdef DEBUG
  26. long sys_filesize();
  27. #endif
  28. t_idf *DefId;
  29. char *
  30. getwdir(fn)
  31. register char *fn;
  32. {
  33. register char *p;
  34. char *strrchr();
  35. while ((p = strrchr(fn,'/')) && *(p + 1) == '\0') {
  36. /* remove trailing /'s */
  37. *p = '\0';
  38. }
  39. if (p) {
  40. *p = '\0';
  41. fn = Salloc(fn, (unsigned) (p - &fn[0] + 1));
  42. *p = '/';
  43. return fn;
  44. }
  45. return "";
  46. }
  47. STATIC
  48. GetFile(name)
  49. char *name;
  50. {
  51. /* Try to find a file with basename "name" and extension ".def",
  52. in the directories mentioned in "DEFPATH".
  53. */
  54. char buf[15];
  55. char *strncpy(), *strcat();
  56. strncpy(buf, name, 10);
  57. buf[10] = '\0'; /* maximum length */
  58. strcat(buf, ".def");
  59. DEFPATH[0] = WorkingDir;
  60. if (! InsertFile(buf, DEFPATH, &(FileName))) {
  61. error("could not find a DEFINITION MODULE for \"%s\"", name);
  62. return 0;
  63. }
  64. WorkingDir = getwdir(FileName);
  65. LineNumber = 1;
  66. DO_DEBUG(options['F'], debug("File %s : %ld characters", FileName, sys_filesize(FileName)));
  67. return 1;
  68. }
  69. t_def *
  70. GetDefinitionModule(id, incr)
  71. register t_idf *id;
  72. {
  73. /* Return a pointer to the "def" structure of the definition
  74. module indicated by "id".
  75. We may have to read the definition module itself.
  76. Also increment level by "incr".
  77. */
  78. register t_def *df;
  79. static int level;
  80. t_scopelist *vis;
  81. char *fn = FileName;
  82. int ln = LineNumber;
  83. t_scope *newsc;
  84. level += incr;
  85. df = lookup(id, GlobalScope, D_IMPORTED, 0);
  86. if (!df) {
  87. /* Read definition module. Make an exception for SYSTEM.
  88. */
  89. extern int ForeignFlag;
  90. ForeignFlag = 0;
  91. DefId = id;
  92. open_scope(CLOSEDSCOPE);
  93. newsc = CurrentScope;
  94. vis = CurrVis;
  95. newsc->sc_defmodule = incr;
  96. if (!strcmp(id->id_text, "SYSTEM")) {
  97. do_SYSTEM();
  98. df = lookup(id, GlobalScope, D_IMPORTED, 0);
  99. }
  100. else {
  101. if (!is_anon_idf(id) && GetFile(id->id_text)) {
  102. char *f = FileName;
  103. DefModule();
  104. df = lookup(id, GlobalScope, D_IMPORTED, 0);
  105. if (level == 1 &&
  106. (df && !(df->df_flags & D_FOREIGN))) {
  107. /* The module is directly imported by
  108. the currently defined module, and
  109. is not foreign, so we have to
  110. remember its name because we have
  111. to call its initialization routine
  112. */
  113. static t_node *nd_end;
  114. register t_node *n;
  115. extern t_node *Modules;
  116. n = dot2leaf(Def);
  117. n->nd_def = newsc->sc_definedby;
  118. if (nd_end) nd_end->nd_NEXT = n;
  119. else Modules = n;
  120. nd_end = n;
  121. }
  122. free(f);
  123. }
  124. else {
  125. df = lookup(id, GlobalScope, D_IMPORTED, 0);
  126. newsc->sc_name = id->id_text;
  127. }
  128. }
  129. close_scope(SC_CHKFORW);
  130. if (! df) {
  131. df = MkDef(id, GlobalScope, D_ERROR);
  132. df->mod_vis = vis;
  133. newsc->sc_definedby = df;
  134. }
  135. }
  136. else if (df->df_flags & D_BUSY) {
  137. error("definition module \"%s\" depends on itself",
  138. id->id_text);
  139. }
  140. else if (df == Defined && level == 1) {
  141. error("cannot import from current module \"%s\"", id->id_text);
  142. df->df_kind = D_ERROR;
  143. }
  144. FileName = fn;
  145. LineNumber = ln;
  146. assert(df);
  147. level -= incr;
  148. return df;
  149. }