def.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 E C H A N I S M */
  8. /* $Id$ */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "debug.h"
  12. #include <alloc.h>
  13. #include <em_arith.h>
  14. #include <em_label.h>
  15. #include <em_code.h>
  16. #include <assert.h>
  17. #include "LLlex.h"
  18. #include "main.h"
  19. #include "def.h"
  20. #include "type.h"
  21. #include "idf.h"
  22. #include "scope.h"
  23. #include "node.h"
  24. #include "Lpars.h"
  25. #include "warning.h"
  26. extern char *sprint();
  27. STATIC
  28. internal(c)
  29. register char *c;
  30. {
  31. if (options['x']) {
  32. C_exp(c);
  33. }
  34. else C_inp(c);
  35. }
  36. STATIC
  37. DefInFront(df)
  38. register t_def *df;
  39. {
  40. /* Put definition "df" in front of the list of definitions
  41. in its scope.
  42. This is neccessary because in some cases the order in this
  43. list is important.
  44. */
  45. register t_def *df1 = df->df_scope->sc_def;
  46. if (df1 != df) {
  47. /* Definition "df" is not in front of the list
  48. */
  49. while (df1) {
  50. /* Find definition "df"
  51. */
  52. if (df1->df_nextinscope == df) {
  53. /* It already was in the list. Remove it
  54. */
  55. df1->df_nextinscope = df->df_nextinscope;
  56. break;
  57. }
  58. df1 = df1->df_nextinscope;
  59. }
  60. /* Now put it in front
  61. */
  62. df->df_nextinscope = df->df_scope->sc_def;
  63. df->df_scope->sc_def = df;
  64. }
  65. }
  66. t_def *
  67. MkDef(id, scope, kind)
  68. register t_idf *id;
  69. register t_scope *scope;
  70. {
  71. /* Create a new definition structure in scope "scope", with
  72. id "id" and kind "kind".
  73. */
  74. register t_def *df;
  75. df = new_def();
  76. df->df_idf = id;
  77. df->df_scope = scope;
  78. df->df_kind = kind;
  79. df->df_next = id->id_def;
  80. id->id_def = df;
  81. if (kind == D_ERROR || kind == D_FORWARD) df->df_type = error_type;
  82. if (kind & (D_TYPE|D_PROCEDURE|D_CONST)) {
  83. df->df_flags = D_DEFINED;
  84. }
  85. /* enter the definition in the list of definitions in this scope
  86. */
  87. df->df_nextinscope = scope->sc_def;
  88. scope->sc_def = df;
  89. return df;
  90. }
  91. t_def *
  92. define(id, scope, kind)
  93. register t_idf *id;
  94. register t_scope *scope;
  95. int kind;
  96. {
  97. /* Declare an identifier in a scope, but first check if it
  98. already has been defined.
  99. If so, then check for the cases in which this is legal,
  100. and otherwise give an error message.
  101. */
  102. register t_def *df;
  103. DO_DEBUG(options['S'], print("define %s, %x\n", id->id_text, kind));
  104. df = lookup(id, scope, D_IMPORT, 0);
  105. if ( /* Already in this scope */
  106. df
  107. ) {
  108. switch(df->df_kind) {
  109. case D_INUSE:
  110. if (kind != D_INUSE && kind != D_ERROR) {
  111. error("identifier \"%s\" already used; may not be redefined in this scope", df->df_idf->id_text);
  112. df->df_kind = D_ERROR;
  113. break;
  114. }
  115. return df;
  116. case D_HIDDEN:
  117. /* An opaque type. We may now have found the
  118. definition of this type.
  119. */
  120. if (kind == D_TYPE && df->df_scope == CurrentScope &&
  121. !DefinitionModule) {
  122. df->df_kind = D_TYPE;
  123. return df;
  124. }
  125. break;
  126. case D_FORWMODULE:
  127. /* A forward reference to a module. We may have found
  128. another one, or we may have found the definition
  129. for this module.
  130. */
  131. if (kind & (D_FORWMODULE|D_FORWARD)) {
  132. return df;
  133. }
  134. if (kind == D_MODULE) {
  135. FreeNode(df->for_node);
  136. df->mod_vis = df->for_vis;
  137. df->df_kind = kind;
  138. DefInFront(df);
  139. return df;
  140. }
  141. break;
  142. case D_TYPE:
  143. if (kind == D_FORWTYPE) return df;
  144. break;
  145. case D_FORWTYPE:
  146. if (kind & (D_FORWTYPE|D_TYPE)) return df;
  147. error("identifier \"%s\" must be a type", id->id_text);
  148. df->df_kind = D_ERROR;
  149. break;
  150. case D_FORWARD:
  151. /* A forward reference, for which we may now have
  152. found a definition.
  153. */
  154. if (! (kind & (D_FORWARD | D_FORWMODULE))) {
  155. FreeNode(df->for_node);
  156. }
  157. df->df_kind = D_ERROR; /* avoiding error message */
  158. break;
  159. }
  160. if (kind != D_ERROR && df->df_kind != D_ERROR) {
  161. /* Avoid spurious error messages
  162. */
  163. error("identifier \"%s\" already declared",
  164. id->id_text);
  165. }
  166. if (df->df_scope == scope || df->df_kind == D_ERROR) {
  167. df->df_kind = kind;
  168. if (kind & (D_TYPE|D_PROCEDURE|D_CONST)) {
  169. df->df_flags = D_DEFINED;
  170. }
  171. return df;
  172. }
  173. }
  174. return MkDef(id, scope, kind);
  175. }
  176. end_definition_list(pdf)
  177. register t_def **pdf;
  178. {
  179. /* Remove all imports from a definition module. This is
  180. neccesary because the implementation module might import
  181. them again.
  182. Also, mark all other definitions "QUALIFIED EXPORT".
  183. */
  184. register t_def *df;
  185. while (df = *pdf) {
  186. if (df->df_kind & D_IMPORTED) {
  187. if (! (df->df_flags & D_USED)) {
  188. warning(W_ORDINARY, "identifier \"%s\" imported but not used", df->df_idf->id_text);
  189. }
  190. RemoveFromIdList(df);
  191. *pdf = df->df_nextinscope;
  192. free_def(df);
  193. }
  194. else {
  195. df->df_flags |= D_QEXPORTED;
  196. pdf = &(df->df_nextinscope);
  197. }
  198. }
  199. }
  200. RemoveFromIdList(df)
  201. register t_def *df;
  202. {
  203. /* Remove definition "df" from the definition list
  204. */
  205. register t_idf *id = df->df_idf;
  206. register t_def *df1;
  207. if ((df1 = id->id_def) == df) id->id_def = df->df_next;
  208. else {
  209. while (df1->df_next != df) {
  210. assert(df1->df_next != 0);
  211. df1 = df1->df_next;
  212. }
  213. df1->df_next = df->df_next;
  214. }
  215. }
  216. t_def *
  217. DeclProc(type, id)
  218. register t_idf *id;
  219. {
  220. /* A procedure is declared, either in a definition or a program
  221. module. Create a def structure for it (if neccessary).
  222. Also create a name for it.
  223. */
  224. register t_def *df;
  225. register t_scope *scope;
  226. static int nmcount;
  227. char buf[256];
  228. assert(type & (D_PROCEDURE | D_PROCHEAD));
  229. if (type == D_PROCHEAD) {
  230. /* In a definition module
  231. */
  232. df = define(id, CurrentScope, type);
  233. df->for_node = dot2leaf(Name);
  234. df->df_flags |= D_USED | D_DEFINED;
  235. if (CurrentScope->sc_definedby->df_flags & D_FOREIGN) {
  236. df->prc_name = id->id_text;
  237. }
  238. else {
  239. sprint(buf,"%s_%s",CurrentScope->sc_name,id->id_text);
  240. df->prc_name = Salloc(buf, (unsigned) (strlen(buf)+1));
  241. }
  242. if (CurrVis == Defined->mod_vis) {
  243. /* The current module will define this routine.
  244. make sure the name is exported.
  245. */
  246. C_exp(df->prc_name);
  247. }
  248. }
  249. else {
  250. df = lookup(id, CurrentScope, D_IMPORTED, 0);
  251. if (df && df->df_kind == D_PROCHEAD) {
  252. /* C_exp already generated when we saw the definition
  253. in the definition module
  254. */
  255. DefInFront(df);
  256. }
  257. else {
  258. df = define(id, CurrentScope, type);
  259. sprint(buf,"_%d_%s",++nmcount,id->id_text);
  260. df->prc_name = Salloc(buf, (unsigned)(strlen(buf)+1));
  261. internal(buf);
  262. df->df_flags |= D_DEFINED;
  263. }
  264. open_scope(OPENSCOPE);
  265. scope = CurrentScope;
  266. scope->sc_name = df->prc_name;
  267. scope->sc_definedby = df;
  268. }
  269. df->prc_vis = CurrVis;
  270. return df;
  271. }
  272. EndProc(df, id)
  273. register t_def *df;
  274. t_idf *id;
  275. {
  276. /* The end of a procedure declaration.
  277. Check that the closing identifier matches the name of the
  278. procedure, close the scope, and check that a function
  279. procedure has at least one RETURN statement.
  280. */
  281. extern int return_occurred;
  282. match_id(id, df->df_idf);
  283. close_scope(SC_CHKFORW|SC_REVERSE);
  284. if (! return_occurred && ResultType(df->df_type)) {
  285. error("function procedure %s does not return a value",
  286. df->df_idf->id_text);
  287. }
  288. }
  289. t_def *
  290. DefineLocalModule(id)
  291. t_idf *id;
  292. {
  293. /* Create a definition for a local module. Also give it
  294. a name to be used for code generation.
  295. */
  296. register t_def *df = define(id, CurrentScope, D_MODULE);
  297. register t_scope *sc;
  298. static int modulecount = 0;
  299. char buf[256];
  300. extern int proclevel;
  301. sprint(buf, "_%d%s_", ++modulecount, id->id_text);
  302. if (!df->mod_vis) {
  303. /* We never saw the name of this module before. Create a
  304. scope for it.
  305. */
  306. open_scope(CLOSEDSCOPE);
  307. df->mod_vis = CurrVis;
  308. }
  309. CurrVis = df->mod_vis;
  310. sc = CurrentScope;
  311. sc->sc_level = proclevel;
  312. sc->sc_definedby = df;
  313. sc->sc_name = Salloc(buf, (unsigned) (strlen(buf) + 1));
  314. /* Create a type for it
  315. */
  316. df->df_type = standard_type(T_RECORD, 1, (arith) 0);
  317. df->df_type->rec_scope = sc;
  318. /* Generate code that indicates that the initialization procedure
  319. for this module is local.
  320. */
  321. internal(buf);
  322. return df;
  323. }
  324. CheckWithDef(df, tp)
  325. register t_def *df;
  326. t_type *tp;
  327. {
  328. /* Check the header of a procedure declaration against a
  329. possible earlier definition in the definition module.
  330. */
  331. if (df->df_kind == D_PROCHEAD &&
  332. df->df_type &&
  333. df->df_type != error_type) {
  334. /* We already saw a definition of this type
  335. in the definition module.
  336. */
  337. if (!TstProcEquiv(tp, df->df_type)) {
  338. error("inconsistent procedure declaration for \"%s\"",
  339. df->df_idf->id_text);
  340. }
  341. FreeType(df->df_type);
  342. df->df_kind = D_PROCEDURE;
  343. }
  344. df->df_type = tp;
  345. }
  346. #ifdef DEBUG
  347. PrDef(df)
  348. register t_def *df;
  349. {
  350. print("n: %s, k: %d\n", df->df_idf->id_text, df->df_kind);
  351. }
  352. #endif /* DEBUG */