idf.c 15 KB

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