idf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. /* $Id$ */
  6. /* IDENTIFIER FIDDLING & SYMBOL TABLE HANDLING */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "system.h"
  10. #include "lint.h"
  11. #include <em_reg.h>
  12. #include "debug.h"
  13. #include "idfsize.h"
  14. #include "botch_free.h"
  15. #include "nopp.h"
  16. #include "nparams.h"
  17. #include <alloc.h>
  18. #include "arith.h"
  19. #include "align.h"
  20. #include "LLlex.h"
  21. #include "level.h"
  22. #include "stack.h"
  23. #include "idf.h"
  24. #include "label.h"
  25. #include "def.h"
  26. #include "type.h"
  27. #include "proto.h"
  28. #include "struct.h"
  29. #include "declar.h"
  30. #include "decspecs.h"
  31. #include "sizes.h"
  32. #include "Lpars.h"
  33. #include "assert.h"
  34. #include "ch3.h"
  35. #include "code_c.h"
  36. #include "conversion.h"
  37. #include "print.h"
  38. #include "idf_loc.h"
  39. #include "struct_loc.h"
  40. #include "proto_loc.h"
  41. #include "util_loc.h"
  42. #include "error.h"
  43. #include "stab.h"
  44. #include "stack_loc.h"
  45. #include <symbol2str.h>
  46. extern char options[];
  47. #ifdef DEBUG
  48. #define IDF_DEBUG
  49. #endif
  50. #include <idf_pkg.body>
  51. struct idf *gen_idf()
  52. {
  53. /* A new idf is created out of nowhere, to serve as an
  54. anonymous name.
  55. */
  56. static int name_cnt;
  57. char *s = Malloc(strlen(dot.tk_file) + 50);
  58. sprint(s, "#%d in %s, line %u",
  59. ++name_cnt, dot.tk_file, dot.tk_line);
  60. s = Realloc(s, strlen(s)+1);
  61. return str2idf(s, 0);
  62. }
  63. int is_anon_idf(struct idf *idf)
  64. {
  65. return idf->id_text[0] == '#';
  66. }
  67. void declare_idf(struct decspecs *ds, struct declarator *dc, int lvl)
  68. {
  69. /* The identifier inside dc is declared on the level lvl, with
  70. properties deduced from the decspecs ds and the declarator
  71. dc.
  72. The level is given explicitly to be able to insert, e.g.,
  73. labels on the outermost level inside the function.
  74. This routine implements the rich semantics of C
  75. declarations.
  76. */
  77. struct idf *idf = dc->dc_idf;
  78. int sc = ds->ds_sc;
  79. /* This local copy is essential:
  80. char b(), c;
  81. makes b GLOBAL and c AUTO.
  82. */
  83. struct def *def = idf->id_def; /* may be NULL */
  84. struct type *type;
  85. struct stack_level *stl = stack_level_of(lvl);
  86. char formal_array = 0;
  87. /* determine the present type */
  88. if (ds->ds_type == 0) {
  89. /* at the L_FORMAL1 level there is no type specified yet
  90. */
  91. ASSERT(lvl == L_FORMAL1);
  92. type = int_type; /* may change at L_FORMAL2 */
  93. }
  94. else {
  95. /* combine the decspecs and the declarator into one type */
  96. type = declare_type(ds->ds_type, dc);
  97. if ( (type->tp_size <= (arith)0) &&
  98. actual_declaration(sc, type)) {
  99. if (type->tp_size == (arith) -1) {
  100. /* the type is not yet known,
  101. but it has to be:
  102. */
  103. if (type->tp_fund != VOID) {
  104. if (level != L_GLOBAL)
  105. error("unknown %s-type",
  106. symbol2str(type->tp_fund));
  107. } else error("void is not a complete type");
  108. }
  109. else strict("%s has size 0", idf->id_text);
  110. }
  111. }
  112. /* some additional work for formal definitions */
  113. if (lvl == L_FORMAL2) {
  114. switch (type->tp_fund) {
  115. case FUNCTION:
  116. warning("%s is a function; cannot be formal",
  117. idf->id_text);
  118. type = construct_type(POINTER, type, 0, (arith)0,
  119. NO_PROTO);
  120. break;
  121. case ARRAY: /* 3.7.1 */
  122. type = construct_type(POINTER, type->tp_up, 0, (arith)0,
  123. NO_PROTO);
  124. formal_array = 1;
  125. break;
  126. case FLOAT:
  127. case CHAR:
  128. case SHORT:
  129. /* The conversion is done in formal_cvt(). It is
  130. * not done when the type is float and there is a
  131. * prototype.
  132. */
  133. break;
  134. }
  135. }
  136. /* The tests on types, postponed from do_decspecs(), can now
  137. be performed.
  138. */
  139. /* update the storage class */
  140. if (type && type->tp_fund == FUNCTION) {
  141. if (lvl != L_GLOBAL) { /* 3.5.1 */
  142. if (sc == 0)
  143. sc = GLOBAL;
  144. else if (sc != EXTERN && sc != TYPEDEF) {
  145. error("illegal storage class %s for function with block-scope"
  146. , symbol2str(sc));
  147. ds->ds_sc = sc = EXTERN;
  148. }
  149. }
  150. else if (sc == 0)
  151. sc = GLOBAL;
  152. }
  153. else /* non-FUNCTION */
  154. if (sc == 0)
  155. sc = lvl == L_GLOBAL ? GLOBAL
  156. : lvl == L_FORMAL1 || lvl == L_FORMAL2 ? FORMAL
  157. : AUTO;
  158. #ifdef LINT
  159. check_hiding(idf, lvl, sc); /* of some idf by this idf */
  160. #endif /* LINT */
  161. if (def && lvl == L_LOCAL && def->df_level == L_FORMAL2) {
  162. error("%s redeclared", idf->id_text);
  163. }
  164. if (def &&
  165. ( def->df_level == lvl ||
  166. ( lvl != L_GLOBAL && def->df_level > lvl ) ||
  167. (lvl == L_GLOBAL
  168. && def->df_level == L_PROTO
  169. && def->next && def->next->df_level == L_GLOBAL)
  170. )) {
  171. /* There is already a declaration for idf on this
  172. level, or even more inside.
  173. The rules differ for different levels.
  174. */
  175. switch (lvl) {
  176. case L_GLOBAL:
  177. global_redecl(idf, sc, type);
  178. def->df_file = idf->id_file;
  179. def->df_line = idf->id_line;
  180. break;
  181. case L_FORMAL1: /* formal declaration */
  182. error("formal %s redeclared", idf->id_text);
  183. break;
  184. case L_FORMAL2: /* formal definition */
  185. default: /* local */
  186. if (sc != EXTERN) error("%s redeclared", idf->id_text);
  187. break;
  188. }
  189. }
  190. else /* the idf is unknown on this level */
  191. if (lvl == L_FORMAL2 && sc != ENUM && good_formal(def, idf)) {
  192. /* formal declaration, update only */
  193. def->df_type = type;
  194. def->df_formal_array = formal_array;
  195. def->df_sc = sc;
  196. def->df_level = L_FORMAL2; /* CJ */
  197. def->df_file = idf->id_file;
  198. def->df_line = idf->id_line;
  199. }
  200. else { /* fill in the def block */
  201. struct def *newdef = new_def();
  202. newdef->next = def;
  203. newdef->df_level = lvl;
  204. newdef->df_type = type;
  205. newdef->df_sc = sc;
  206. newdef->df_file = idf->id_file;
  207. newdef->df_line = idf->id_line;
  208. #ifdef LINT
  209. newdef->df_set = 0;
  210. newdef->df_firstbrace = 0;
  211. #endif /* LINT */
  212. /* link it into the name list in the proper place */
  213. idf->id_def = newdef;
  214. update_ahead(idf);
  215. stack_idf(idf, stl);
  216. /* We now calculate the address.
  217. Globals have names and don't get addresses, they
  218. get numbers instead (through data_label()).
  219. Formals are handled by declare_formals().
  220. So here we hand out local addresses only.
  221. */
  222. if (lvl >= L_LOCAL) {
  223. ASSERT(sc);
  224. switch (sc) {
  225. case REGISTER:
  226. case AUTO:
  227. if (type->tp_size == (arith)-1
  228. && type->tp_fund != ARRAY) {
  229. error("size of local %s unknown",
  230. idf->id_text);
  231. /** type = idf->id_def->df_type = int_type; **/
  232. }
  233. if (type->tp_size != (arith) -1) {
  234. newdef->df_address =
  235. NewLocal(type->tp_size,
  236. type->tp_align,
  237. regtype(type),
  238. sc);
  239. }
  240. break;
  241. case STATIC:
  242. newdef->df_address = (arith) data_label();
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. int actual_declaration(int sc, struct type *tp)
  249. {
  250. /* An actual_declaration needs space, right here and now.
  251. */
  252. int fund = tp->tp_fund;
  253. if (sc == ENUM || sc == TYPEDEF) /* virtual declarations */
  254. return 0;
  255. if (fund == FUNCTION || fund == ARRAY)
  256. /* allocation solved in other ways */
  257. return 0;
  258. if (sc == EXTERN && fund == VOID) {
  259. /* strange, but should be accepted */
  260. return 0;
  261. }
  262. /* to be allocated */
  263. return 1;
  264. }
  265. void global_redecl(struct idf *idf, int new_sc, struct type *tp)
  266. {
  267. /* A global identifier may be declared several times,
  268. provided the declarations do not conflict; they might
  269. conflict in type (or supplement each other in the case of
  270. an array) or they might conflict or supplement each other
  271. in storage class.
  272. */
  273. struct def *def = idf->id_def;
  274. while (def->df_level != L_GLOBAL) def = def->next;
  275. if (!equal_type(tp, def->df_type, 0, 1)) {
  276. error("redeclaration of %s with different type", idf->id_text);
  277. return;
  278. } else update_proto(tp, def->df_type);
  279. if (tp->tp_fund == ARRAY) {
  280. /* Multiple array declaration; this may be interesting */
  281. if (tp->tp_size < 0) { /* new decl has [] */
  282. /* nothing new */
  283. } else
  284. if (def->df_type->tp_size < 0) { /* old decl has [] */
  285. def->df_type = tp;
  286. }
  287. } if (tp->tp_fund == FUNCTION && new_sc == GLOBAL) {
  288. /* see 3.1.2.2 */
  289. new_sc = EXTERN;
  290. }
  291. /* Now we may be able to update the storage class.
  292. Clean out this mess as soon as we know all the possibilities
  293. for new_sc.
  294. For now we have:
  295. EXTERN: we have seen the word "extern"
  296. GLOBAL: the item was declared on the outer
  297. level, without either "extern" or
  298. "static".
  299. STATIC: we have seen the word "static"
  300. */
  301. switch (def->df_sc) { /* the old storage class */
  302. case EXTERN:
  303. switch (new_sc) { /* the new storage class */
  304. case STATIC:
  305. warning("%s redeclared static", idf->id_text);
  306. /* fallthrough */
  307. case GLOBAL:
  308. def->df_sc = new_sc;
  309. /* fallthrough */
  310. case EXTERN:
  311. break;
  312. default:
  313. crash("bad storage class");
  314. /*NOTREACHED*/
  315. }
  316. break;
  317. case GLOBAL:
  318. switch (new_sc) { /* the new storage class */
  319. case STATIC: /* linkage disagreement */
  320. warning("%s redeclared static", idf->id_text);
  321. def->df_sc = new_sc;
  322. /* fallthrough */
  323. case GLOBAL:
  324. case EXTERN:
  325. break;
  326. default:
  327. crash("bad storage class");
  328. /*NOTREACHED*/
  329. }
  330. break;
  331. case STATIC:
  332. switch (new_sc) { /* the new storage class */
  333. case GLOBAL: /* linkage disagreement */
  334. case EXTERN:
  335. warning("%s is already declared static", idf->id_text);
  336. /* fallthrough */
  337. case STATIC:
  338. break;
  339. default:
  340. crash("bad storage class");
  341. /*NOTREACHED*/
  342. }
  343. break;
  344. case ENUM:
  345. case TYPEDEF:
  346. error("illegal redeclaration of %s", idf->id_text);
  347. break;
  348. default:
  349. crash("bad storage class");
  350. /*NOTREACHED*/
  351. }
  352. }
  353. int good_formal(struct def *def, struct idf *idf)
  354. {
  355. /* Succeeds if def is a proper L_FORMAL1 definition and
  356. gives an error message otherwise.
  357. */
  358. if (!def || def->df_level != L_FORMAL1) { /* not in parameter list */
  359. if (!is_anon_idf(idf))
  360. error("%s not in parameter list", idf->id_text);
  361. return 0;
  362. }
  363. ASSERT(def->df_sc == FORMAL); /* CJ */
  364. return 1;
  365. }
  366. void declare_params(struct declarator *dc)
  367. {
  368. /* Declares the formal parameters if they exist.
  369. */
  370. struct formal *fm = dc->dc_formal;
  371. while (fm) {
  372. declare_parameter(fm->fm_idf);
  373. fm = fm->next;
  374. }
  375. }
  376. void idf_initialized(struct idf *idf)
  377. {
  378. /* The topmost definition of idf is set to initialized.
  379. */
  380. struct def *def = idf->id_def; /* the topmost */
  381. while (def->df_level <= L_PROTO) def = def->next;
  382. if (def->df_initialized)
  383. error("multiple initialization of %s", idf->id_text);
  384. if (def->df_sc == TYPEDEF) {
  385. error("typedef cannot be initialized");
  386. return;
  387. }
  388. def->df_initialized = 1;
  389. }
  390. void declare_parameter(struct idf *idf)
  391. {
  392. /* idf is declared as a formal.
  393. */
  394. add_def(idf, FORMAL, int_type, level);
  395. }
  396. void declare_enum(struct type *tp, struct idf *idf, arith l)
  397. {
  398. /* idf is declared as an enum constant with value l.
  399. */
  400. add_def(idf, ENUM, tp, level);
  401. idf->id_def->df_address = l;
  402. }
  403. void check_formals(struct idf *idf, struct declarator *dc)
  404. {
  405. struct formal *fm = dc->dc_formal;
  406. struct proto *pl = idf->id_def->df_type->tp_proto;
  407. struct decl_unary *du = dc->dc_decl_unary;
  408. if (!du) { /* error or typdef'ed function */
  409. error("illegal definition of %s", idf->id_text);
  410. return;
  411. }
  412. while (du
  413. && (du->du_fund != FUNCTION
  414. || du->next != (struct decl_unary *) 0)) {
  415. du = du->next;
  416. }
  417. if (!du) return; /* terrible error, signalled earlier */
  418. if (du->du_proto) return;
  419. if (pl) {
  420. /* Don't give a warning about an old-style definition,
  421. * since the arguments will be checked anyway.
  422. */
  423. if (pl->pl_flag & PL_ELLIPSIS) {
  424. if (!(du->du_proto) && !(pl->pl_flag & PL_ERRGIVEN))
  425. error("ellipsis terminator in previous declaration");
  426. pl = pl->next;
  427. }
  428. else if (pl->pl_flag & PL_VOID) {
  429. pl = pl->next; /* should be 0 */
  430. }
  431. while(fm && pl) {
  432. if (!equal_type(promoted_type(fm->fm_idf->id_def->df_type)
  433. , pl->pl_type, -1, 1)) {
  434. if (!(pl->pl_flag & PL_ERRGIVEN))
  435. error("incorrect type for parameter %s"
  436. , fm->fm_idf->id_text);
  437. pl->pl_flag |= PL_ERRGIVEN;
  438. }
  439. fm = fm->next;
  440. pl = pl->next;
  441. }
  442. if (pl || fm) {
  443. error("incorrect number of parameters");
  444. }
  445. } else { /* make a pseudo-prototype */
  446. struct proto *lpl = new_proto();
  447. if (!options['o'])
  448. warning("'%s' old-fashioned function definition"
  449. , dc->dc_idf->id_text);
  450. while (fm) {
  451. if (pl == 0) pl = lpl;
  452. else {
  453. lpl->next = new_proto();
  454. lpl = lpl->next;
  455. }
  456. lpl->pl_flag = PL_FORMAL;
  457. lpl->pl_idf = fm->fm_idf;
  458. lpl->pl_type =
  459. promoted_type(fm->fm_idf->id_def->df_type);
  460. fm = fm->next;
  461. }
  462. if (pl == 0) { /* make func(void) */
  463. pl = lpl;
  464. pl->pl_type = void_type;
  465. pl->pl_flag = PL_FORMAL | PL_VOID;
  466. }
  467. idf->id_def->df_type->tp_pseudoproto = pl;
  468. }
  469. free_formals(dc->dc_formal);
  470. dc->dc_formal = 0;
  471. }
  472. void declare_formals(struct idf *idf, arith *fp)
  473. {
  474. /* Declares those formals as int that haven't been declared
  475. by the user.
  476. An address is assigned to each formal parameter.
  477. The total size of the formals is returned in *fp;
  478. */
  479. struct stack_entry *se = stack_level_of(L_FORMAL1)->sl_entry;
  480. arith f_offset = (arith)0;
  481. int nparams = 0;
  482. int hasproto;
  483. struct def *df = idf->id_def;
  484. /* When one of the formals has the same name as the function,
  485. it hides the function def. Get it.
  486. */
  487. while (se) {
  488. if (se->se_idf == idf) {
  489. df = df->next;
  490. break;
  491. }
  492. se = se->next;
  493. }
  494. se = stack_level_of(L_FORMAL1)->sl_entry;
  495. hasproto = df->df_type->tp_proto != 0;
  496. #ifdef DEBUG
  497. if (options['t'])
  498. dumpidftab("start declare_formals", 0);
  499. #endif /* DEBUG */
  500. if (is_struct_or_union(df->df_type->tp_up->tp_fund)) {
  501. /* create space for address of return value */
  502. f_offset = pointer_size;
  503. }
  504. while (se) {
  505. df = se->se_idf->id_def;
  506. /* this stacklevel may also contain tags. ignore them */
  507. if (!df || df->df_level < L_FORMAL1 ) {
  508. se = se->next;
  509. continue;
  510. }
  511. df->df_address = f_offset;
  512. /* the alignment convention for parameters is: align on
  513. word boundaries, i.e. take care that the following
  514. parameter starts on a new word boundary.
  515. */
  516. if (! hasproto
  517. && df->df_type->tp_fund == FLOAT
  518. && df->df_type->tp_size != double_size) {
  519. f_offset = align(f_offset + double_size, (int) word_size);
  520. }
  521. else f_offset = align(f_offset + df->df_type->tp_size, (int) word_size);
  522. RegisterAccount(df->df_address, df->df_type->tp_size,
  523. regtype(df->df_type),
  524. df->df_sc);
  525. /* cvt int to char or short and double to float, if necessary
  526. */
  527. formal_cvt(hasproto, df);
  528. df->df_level = L_FORMAL2; /* CJ */
  529. if (nparams++ >= STDC_NPARAMS)
  530. strict("number of formal parameters exceeds ANSI limit");
  531. #ifdef DBSYMTAB
  532. if (options['g']) {
  533. stb_string(df, FORMAL, se->se_idf->id_text);
  534. }
  535. #endif /* DBSYMTAB */
  536. se = se->next;
  537. }
  538. *fp = f_offset;
  539. }
  540. int regtype(struct type *tp)
  541. {
  542. switch(tp->tp_fund) {
  543. case INT:
  544. case LONG:
  545. return reg_any;
  546. case FLOAT:
  547. case DOUBLE:
  548. case LNGDBL:
  549. return reg_float;
  550. case POINTER:
  551. return reg_pointer;
  552. }
  553. return -1;
  554. }
  555. void add_def(struct idf *idf, int sc, struct type *tp, int lvl)
  556. {
  557. /* The identifier idf is declared on level lvl with storage
  558. class sc and type tp, through a faked C declaration.
  559. This is probably the wrong way to structure the problem,
  560. but it will have to do for the time being.
  561. */
  562. struct decspecs Ds; struct declarator Dc;
  563. Ds = null_decspecs;
  564. Ds.ds_type = tp;
  565. Ds.ds_sc = sc;
  566. Dc = null_declarator;
  567. Dc.dc_idf = idf;
  568. declare_idf(&Ds, &Dc, lvl);
  569. }
  570. void update_ahead(struct idf *idf)
  571. {
  572. /* The tk_symb of the token ahead is updated in the light of new
  573. information about the identifier idf.
  574. */
  575. int tk_symb = AHEAD;
  576. if ( (tk_symb == IDENTIFIER || tk_symb == TYPE_IDENTIFIER) &&
  577. ahead.tk_idf == idf
  578. )
  579. AHEAD = idf->id_def && idf->id_def->df_sc == TYPEDEF ?
  580. TYPE_IDENTIFIER : IDENTIFIER;
  581. }
  582. void free_formals(struct formal *fm)
  583. {
  584. while (fm) {
  585. struct formal *tmp = fm->next;
  586. free_formal(fm);
  587. fm = tmp;
  588. }
  589. }