idf.c 15 KB

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