enter.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. /* H I G H L E V E L S Y M B O L E N T R Y */
  8. /* $Id$ */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "debug.h"
  13. #include "alloc.h"
  14. #include "em_arith.h"
  15. #include "em_label.h"
  16. #include "em_code.h"
  17. #include "assert.h"
  18. #include "dbsymtab.h"
  19. #include "idf.h"
  20. #include "LLlex.h"
  21. #include "def.h"
  22. #include "type.h"
  23. #include "scope.h"
  24. #include "node.h"
  25. #include "main.h"
  26. #include "misc.h"
  27. #include "f_info.h"
  28. t_def *
  29. Enter(name, kind, type, pnam)
  30. char *name;
  31. t_type *type;
  32. {
  33. /* Enter a definition for "name" with kind "kind" and type
  34. "type" in the Current Scope. If it is a standard name, also
  35. put its number in the definition structure.
  36. */
  37. register t_def *df;
  38. df = define(str2idf(name, 0), CurrentScope, kind);
  39. df->df_type = type;
  40. if (pnam) df->df_value.df_stdname = pnam;
  41. #ifdef DBSYMTAB
  42. else if (options['g']) stb_string(df, kind);
  43. #endif /* DBSYMTAB */
  44. return df;
  45. }
  46. t_def *
  47. EnterType(name, type)
  48. char *name;
  49. t_type *type;
  50. {
  51. /* Enter a type definition for "name" and type
  52. "type" in the Current Scope.
  53. */
  54. return Enter(name, D_TYPE, type, 0);
  55. }
  56. EnterEnumList(Idlist, type)
  57. t_node *Idlist;
  58. register t_type *type;
  59. {
  60. /* Put a list of enumeration literals in the symbol table.
  61. They all have type "type".
  62. Also assign numbers to them, and link them together.
  63. We must link them together because an enumeration type may
  64. be exported, in which case its literals must also be exported.
  65. Thus, we need an easy way to get to them.
  66. */
  67. register t_def *df, *df1 = 0;
  68. register t_node *idlist = Idlist;
  69. type->enm_ncst = 0;
  70. for (; idlist; idlist = idlist->nd_NEXT) {
  71. df = define(idlist->nd_IDF, CurrentScope, D_ENUM);
  72. df->df_type = type;
  73. df->enm_val = (type->enm_ncst)++;
  74. if (! df1) {
  75. type->enm_enums = df;
  76. }
  77. else df1->enm_next = df;
  78. df1 = df;
  79. }
  80. FreeNode(Idlist);
  81. }
  82. EnterFieldList(Idlist, type, scope, addr)
  83. t_node *Idlist;
  84. register t_type *type;
  85. t_scope *scope;
  86. arith *addr;
  87. {
  88. /* Put a list of fields in the symbol table.
  89. They all have type "type", and are put in scope "scope".
  90. Mark them as QUALIFIED EXPORT, because that's exactly what
  91. fields are, you can get to them by qualifying them.
  92. */
  93. register t_def *df;
  94. register t_node *idlist = Idlist;
  95. for (; idlist; idlist = idlist->nd_NEXT) {
  96. df = define(idlist->nd_IDF, scope, D_FIELD);
  97. df->df_type = type;
  98. df->df_flags |= D_QEXPORTED;
  99. df->fld_off = align(*addr, type->tp_align);
  100. *addr = df->fld_off + type->tp_size;
  101. }
  102. FreeNode(Idlist);
  103. }
  104. EnterVarList(Idlist, type, local)
  105. t_node *Idlist;
  106. t_type *type;
  107. {
  108. /* Enter a list of identifiers representing variables into the
  109. name list. "type" represents the type of the variables.
  110. "local" is set if the variables are declared local to a
  111. procedure.
  112. */
  113. register t_def *df;
  114. register t_node *idlist = Idlist;
  115. register t_scopelist *sc = CurrVis;
  116. char buf[256];
  117. extern char *sprint();
  118. if (local) {
  119. /* Find the closest enclosing open scope. This
  120. is the procedure that we are dealing with
  121. */
  122. while (sc->sc_scope->sc_scopeclosed) sc = enclosing(sc);
  123. }
  124. for (; idlist; idlist = idlist->nd_RIGHT) {
  125. df = define(idlist->nd_LEFT->nd_IDF, CurrentScope, D_VARIABLE);
  126. df->df_type = type;
  127. if (idlist->nd_LEFT->nd_NEXT) {
  128. /* An address was supplied
  129. */
  130. register t_type *tp = idlist->nd_LEFT->nd_NEXT->nd_type;
  131. df->df_flags |= D_ADDRGIVEN | D_NOREG;
  132. if (tp != error_type && !(tp->tp_fund & T_CARDINAL)){
  133. node_error(idlist->nd_LEFT->nd_NEXT,
  134. "illegal type for address");
  135. }
  136. df->var_off = idlist->nd_LEFT->nd_NEXT->nd_INT;
  137. }
  138. else if (local) {
  139. /* subtract aligned size of variable to the offset,
  140. as the variable list exists only local to a
  141. procedure
  142. */
  143. sc->sc_scope->sc_off =
  144. -WA(align(type->tp_size - sc->sc_scope->sc_off,
  145. type->tp_align));
  146. df->var_off = sc->sc_scope->sc_off;
  147. }
  148. else {
  149. /* Global name, possibly external
  150. */
  151. if (sc->sc_scope->sc_definedby->df_flags & D_FOREIGN) {
  152. df->var_name = df->df_idf->id_text;
  153. }
  154. else {
  155. sprint(buf,"%s_%s", sc->sc_scope->sc_name,
  156. df->df_idf->id_text);
  157. df->var_name = Salloc(buf,
  158. (unsigned)(strlen(buf)+1));
  159. }
  160. df->df_flags |= D_NOREG;
  161. if (DefinitionModule) {
  162. df->df_flags |= D_USED | D_DEFINED;
  163. if (sc == Defined->mod_vis) {
  164. C_exa_dnam(df->var_name);
  165. }
  166. }
  167. else {
  168. C_ina_dnam(df->var_name);
  169. }
  170. }
  171. }
  172. FreeNode(Idlist);
  173. }
  174. EnterParamList(ppr, Idlist, type, VARp, off)
  175. t_param **ppr;
  176. t_node *Idlist;
  177. t_type *type;
  178. int VARp;
  179. arith *off;
  180. {
  181. /* Create (part of) a parameterlist of a procedure.
  182. "ids" indicates the list of identifiers, "tp" their type, and
  183. "VARp" indicates D_VARPAR or D_VALPAR.
  184. */
  185. register t_param *pr;
  186. register t_def *df;
  187. register t_node *idlist = Idlist;
  188. t_node *dummy = 0;
  189. static t_param *last;
  190. if (! idlist) {
  191. /* Can only happen when a procedure type is defined */
  192. dummy = Idlist = idlist = dot2leaf(Name);
  193. }
  194. for ( ; idlist; idlist = idlist->nd_NEXT) {
  195. pr = new_paramlist();
  196. pr->par_next = 0;
  197. if (!*ppr) *ppr = pr;
  198. else last->par_next = pr;
  199. last = pr;
  200. if (!DefinitionModule && idlist != dummy) {
  201. df = define(idlist->nd_IDF, CurrentScope, D_VARIABLE);
  202. df->var_off = *off;
  203. }
  204. else df = new_def();
  205. pr->par_def = df;
  206. df->df_type = type;
  207. df->df_flags |= VARp;
  208. if (IsConformantArray(type)) {
  209. /* we need room for the base address and a descriptor:
  210. arr_low and arr_high are set to their offset
  211. */
  212. type->arr_low = *off + pointer_size;
  213. type->arr_high = *off + pointer_size + word_size;
  214. *off += pointer_size + word_size + dword_size;
  215. }
  216. else if (VARp == D_VARPAR) {
  217. *off += pointer_size;
  218. }
  219. else {
  220. *off += WA(type->tp_size);
  221. }
  222. }
  223. FreeNode(Idlist);
  224. }
  225. STATIC t_def *DoImport();
  226. ImportEffects(idef, scope, flag)
  227. register t_def *idef;
  228. t_scope *scope;
  229. {
  230. /* Handle side effects of an import:
  231. - a module could have unqualified exports ???
  232. - importing an enumeration type also imports literals
  233. */
  234. register t_def *df = idef;
  235. register t_type *tp;
  236. while ((df->df_kind & D_IMPORTED) && df->imp_def != df) {
  237. /* The second condition could occur on some (erroneous and
  238. obscure) input such as:
  239. IMPLEMENTATION MODULE Test;
  240. FROM X IMPORT XType, XType;
  241. END Test.
  242. when X does not exist.
  243. */
  244. df = df->imp_def;
  245. }
  246. tp = BaseType(df->df_type);
  247. if (df->df_kind == D_TYPE && tp->tp_fund == T_ENUMERATION) {
  248. /* Also import all enumeration literals
  249. */
  250. for (df = tp->enm_enums; df; df = df->enm_next) {
  251. /* But be careful; we could have a situation where f.i.
  252. different subrange types of the enumeration type
  253. are imported. If the literal is already imported
  254. in some way, don't do it again; we don't want
  255. a multiple defined error message here.
  256. */
  257. t_def *df1;
  258. df->df_flags |= D_QEXPORTED;
  259. if ((!(df1 = lookup(df->df_idf, scope, D_IMPORT, 0)) ||
  260. df1 != df) &&
  261. ! DoImport(df, scope, flag|D_USED)) assert(0);
  262. /* don't complain when not used ... */
  263. }
  264. idef->df_flags |= D_USED; /* don't complain ... */
  265. }
  266. else if (df->df_kind == D_MODULE) {
  267. if (df->mod_vis == CurrVis) {
  268. error("cannot import current module \"%s\"",
  269. df->df_idf->id_text);
  270. return;
  271. }
  272. if (df->df_scope == GlobalScope) return;
  273. /* Also import all definitions that are exported from this
  274. module
  275. */
  276. for (df = df->mod_vis->sc_scope->sc_def;
  277. df;
  278. df = df->df_nextinscope) {
  279. if (df->df_flags & D_EXPORTED) {
  280. if (!DoImport(df, scope, D_IMP_BY_EXP|D_USED)){
  281. assert(0);
  282. }
  283. /* don't complain when these are not used */
  284. }
  285. }
  286. idef->df_flags |= D_USED; /* don't complain ... */
  287. }
  288. }
  289. STATIC t_def *
  290. DoImport(df, scope, flag)
  291. register t_def *df;
  292. t_scope *scope;
  293. {
  294. /* Definition "df" is imported to scope "scope".
  295. */
  296. register t_def *idef = define(df->df_idf, scope, D_IMPORT);
  297. idef->imp_def = df;
  298. idef->df_flags |= flag;
  299. ImportEffects(idef, scope, flag);
  300. return idef;
  301. }
  302. STATIC
  303. ForwModule(df, nd)
  304. register t_def *df;
  305. t_node *nd;
  306. {
  307. /* An import is done from a not yet defined module "df".
  308. We could also end up here for not found DEFINITION MODULES.
  309. Create a declaration and a scope for this module.
  310. */
  311. register t_scopelist *vis;
  312. if (df->df_scope != GlobalScope) {
  313. df->df_scope = enclosing(CurrVis)->sc_scope;
  314. df->df_kind = D_FORWMODULE;
  315. }
  316. open_scope(CLOSEDSCOPE);
  317. vis = CurrVis; /* The new scope, but watch out, it's "sc_encl"
  318. field is not set right. It must indicate the
  319. enclosing scope, but this must be done AFTER
  320. closing this one
  321. */
  322. close_scope(0);
  323. vis->sc_encl = enclosing(CurrVis);
  324. /* Here ! */
  325. df->for_vis = vis;
  326. df->for_node = nd;
  327. }
  328. STATIC t_def *
  329. ForwDef(ids, scope)
  330. register t_node *ids;
  331. t_scope *scope;
  332. {
  333. /* Enter a forward definition of "ids" in scope "scope",
  334. if it is not already defined.
  335. */
  336. register t_def *df;
  337. if (!(df = lookup(ids->nd_IDF, scope, 0, 0))) {
  338. df = define(ids->nd_IDF, scope, D_FORWARD);
  339. df->for_node = new_node();
  340. *(df->for_node) = *ids;
  341. df->for_node->nd_NEXT = 0;
  342. }
  343. return df;
  344. }
  345. EnterExportList(Idlist, qualified)
  346. t_node *Idlist;
  347. {
  348. /* From the current scope, the list of identifiers "ids" is
  349. exported. Note this fact. If the export is not qualified, make
  350. all the "ids" visible in the enclosing scope by defining them
  351. in this scope as "imported".
  352. */
  353. register t_node *idlist = Idlist;
  354. register t_def *df, *df1;
  355. for (;idlist; idlist = idlist->nd_NEXT) {
  356. df = lookup(idlist->nd_IDF, CurrentScope, 0, 0);
  357. if (!df) {
  358. /* undefined item in export list
  359. */
  360. node_error(idlist,
  361. "identifier \"%s\" not defined",
  362. idlist->nd_IDF->id_text);
  363. continue;
  364. }
  365. if (df->df_flags & (D_EXPORTED|D_QEXPORTED)) {
  366. node_error(idlist,
  367. "multiple occurrences of \"%s\" in export list",
  368. idlist->nd_IDF->id_text);
  369. continue;
  370. }
  371. df->df_flags |= qualified;
  372. if (qualified == D_EXPORTED) {
  373. /* Export, but not qualified.
  374. Find all imports of the module in which this export
  375. occurs, and export the current definition to it
  376. */
  377. df1 = CurrentScope->sc_definedby->df_idf->id_def;
  378. while (df1) {
  379. if ((df1->df_kind & D_IMPORTED) &&
  380. df1->imp_def == CurrentScope->sc_definedby) {
  381. if (! DoImport(df, df1->df_scope, D_IMP_BY_EXP)) assert(0);
  382. }
  383. df1 = df1->df_next;
  384. }
  385. /* Also handle the definition as if the enclosing
  386. scope imports it.
  387. */
  388. df1 = lookup(idlist->nd_IDF,
  389. enclosing(CurrVis)->sc_scope,
  390. D_IMPORTED,
  391. 0);
  392. if (df1) {
  393. /* It was already defined in the enclosing
  394. scope. There are two legal possibilities,
  395. which are examined below.
  396. */
  397. t_def *df2 = df;
  398. while (df2->df_kind & D_IMPORTED) {
  399. df2 = df2->imp_def;
  400. }
  401. if (df1->df_kind == D_PROCHEAD &&
  402. df2->df_kind == D_PROCEDURE) {
  403. df1->df_kind = D_IMPORT;
  404. df1->df_flags |= D_IMP_BY_EXP;
  405. df1->imp_def = df;
  406. continue;
  407. }
  408. if (df1->df_kind == D_HIDDEN &&
  409. df2->df_kind == D_TYPE) {
  410. DeclareType(idlist, df1, df2->df_type);
  411. df1->df_kind = D_TYPE;
  412. continue;
  413. }
  414. }
  415. if (! DoImport(df,enclosing(CurrVis)->sc_scope,D_IMP_BY_EXP)) assert(0);
  416. }
  417. }
  418. FreeNode(Idlist);
  419. }
  420. CheckForImports(df)
  421. t_def *df;
  422. {
  423. /* We have a definition for "df"; check all imports of
  424. it for side-effects
  425. */
  426. register t_def *df1 = df->df_idf->id_def;
  427. while (df1) {
  428. if (df1->df_kind & D_IMPORTED) {
  429. register t_def *df2 = df1->imp_def;
  430. while (df2->df_kind & D_IMPORTED) df2 = df2->imp_def;
  431. if (df2 == df) {
  432. ImportEffects(df1, df1->df_scope, 0);
  433. }
  434. }
  435. df1 = df1->df_next;
  436. }
  437. }
  438. EnterFromImportList(idlist, FromDef, FromId)
  439. register t_node *idlist;
  440. register t_def *FromDef;
  441. t_node *FromId;
  442. {
  443. /* Import the list Idlist from the module indicated by Fromdef.
  444. */
  445. t_scope *sc;
  446. register t_def *df;
  447. char *module_name = FromDef->df_idf->id_text;
  448. switch(FromDef->df_kind) {
  449. case D_ERROR:
  450. case D_FORWARD:
  451. /* The module from which the import was done
  452. is not yet declared. I'm not sure if I must
  453. accept this, but for the time being I will.
  454. We also end up here if some definition module could not
  455. be found.
  456. ???
  457. */
  458. ForwModule(FromDef, FromId);
  459. /* Fall through */
  460. case D_FORWMODULE:
  461. EnterImportList(idlist, 1, FromDef->for_vis->sc_scope);
  462. return;
  463. case D_MODULE:
  464. sc = FromDef->mod_vis->sc_scope;
  465. if (sc == CurrentScope) {
  466. node_error(FromId, "cannot import from current module \"%s\"", module_name);
  467. return;
  468. }
  469. break;
  470. default:
  471. node_error(FromId,"identifier \"%s\" does not represent a module",module_name);
  472. return;
  473. }
  474. for (; idlist; idlist = idlist->nd_NEXT) {
  475. if (! (df = lookup(idlist->nd_IDF, sc, 0, 0))) {
  476. if (! is_anon_idf(idlist->nd_IDF)) {
  477. node_error(idlist,
  478. "identifier \"%s\" not declared in module \"%s\"",
  479. idlist->nd_IDF->id_text,
  480. module_name);
  481. }
  482. df = define(idlist->nd_IDF,sc,D_ERROR);
  483. }
  484. else if (! (df->df_flags & (D_EXPORTED|D_QEXPORTED))) {
  485. node_error(idlist,
  486. "identifier \"%s\" not exported from module \"%s\"",
  487. idlist->nd_IDF->id_text,
  488. module_name);
  489. df->df_flags |= D_QEXPORTED;
  490. }
  491. if (! DoImport(df, CurrentScope, 0)) assert(0);
  492. }
  493. FreeNode(FromId);
  494. }
  495. EnterImportList(idlist, local, sc)
  496. register t_node *idlist;
  497. t_scope *sc;
  498. {
  499. /* Import "idlist" from scope "sc".
  500. If the import is not local, definition modules must be read
  501. for "idlist".
  502. */
  503. extern t_def *GetDefinitionModule();
  504. struct f_info f;
  505. f = file_info;
  506. for (; idlist; idlist = idlist->nd_NEXT) {
  507. if (! DoImport(local ?
  508. ForwDef(idlist, sc) :
  509. GetDefinitionModule(idlist->nd_IDF, 1),
  510. CurrentScope, 0)) assert(0);
  511. file_info = f;
  512. }
  513. }