declar.g 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. /* $Header$ */
  6. /* DECLARATION SYNTAX PARSER */
  7. {
  8. #include "lint.h"
  9. #include "dbsymtab.h"
  10. #include <alloc.h>
  11. #include "nobitfield.h"
  12. #include "debug.h"
  13. #include <flt_arith.h>
  14. #include "arith.h"
  15. #include "LLlex.h"
  16. #include "label.h"
  17. #include "code.h"
  18. #include "idf.h"
  19. #include "type.h"
  20. #include "proto.h"
  21. #include "struct.h"
  22. #include "field.h"
  23. #include "decspecs.h"
  24. #include "def.h"
  25. #include "declar.h"
  26. #include "label.h"
  27. #include "expr.h"
  28. #include "sizes.h"
  29. #include "level.h"
  30. #ifdef LINT
  31. #include "l_lint.h"
  32. #include "l_state.h"
  33. #endif LINT
  34. extern char options[];
  35. }
  36. /* 3.5 */
  37. declaration
  38. {struct decspecs Ds;}
  39. :
  40. {Ds = null_decspecs;}
  41. decl_specifiers(&Ds)
  42. init_declarator_list(&Ds)?
  43. ';'
  44. ;
  45. /* A `decl_specifiers' describes a sequence of a storage_class_specifier,
  46. an unsigned_specifier, a size_specifier and a simple type_specifier,
  47. which may occur in arbitrary order and each of which may be absent;
  48. at least one of them must be present, however, since the totally
  49. empty case has already be dealt with in `external_definition'.
  50. This means that something like:
  51. unsigned extern int short xx;
  52. is perfectly legal C.
  53. On top of that, multiple occurrences of storage_class_specifiers,
  54. unsigned_specifiers and size_specifiers are errors, but a second
  55. type_specifier should end the decl_specifiers and be treated as
  56. the name to be declared.
  57. Such a language is not easily expressed in a grammar; enumeration
  58. of the permutations is unattractive. We solve the problem by
  59. having a regular grammar for the "soft" items, handling the single
  60. occurrence of the type_specifier in the grammar (we have no choice),
  61. collecting all data in a `struct decspecs' and turning that data
  62. structure into what we want.
  63. The existence of declarations like
  64. short typedef yepp;
  65. makes all hope of writing a specific grammar for typedefs illusory.
  66. */
  67. /* Accept a single declaration specifier. Then accept zero or more
  68. declaration specifiers. There can be a conflict on both
  69. TYPE_IDENTIFIER and IDENTIFIER.
  70. The following rule is used:
  71. When we see a TYPE_IDENTIFIER, we accept it if no type-specifier was
  72. given, and it is not directly followed by an identifier. If a
  73. type-specifier was given, it is taken as the identifier being
  74. declared. If it is followed by an identifier, we assume that an
  75. error has been made, (e.g. unsigned typedeffed_int x;) and that
  76. this will be detected later on.
  77. When we see an IDENTIFIER, directly followed by another IDENTIFIER,
  78. we assume that a typing mistake has been made, and we accept it as
  79. an erroneous type-identifier.
  80. */
  81. decl_specifiers /* non-empty */ (register struct decspecs *ds;)
  82. /* Reads a non-empty decl_specifiers and fills the struct
  83. decspecs *ds.
  84. */
  85. :
  86. single_decl_specifier(ds)
  87. [ %while( (DOT==TYPE_IDENTIFIER
  88. && ds->ds_size == 0
  89. && ds->ds_unsigned == 0
  90. && ds->ds_type == (struct type *)0)
  91. || AHEAD == IDENTIFIER) /* always an error */
  92. single_decl_specifier(ds)
  93. ]*
  94. {do_decspecs(ds);}
  95. ;
  96. single_decl_specifier /* non_empty */ (register struct decspecs *ds;)
  97. :
  98. [ AUTO | STATIC | EXTERN | TYPEDEF | REGISTER ]
  99. { if (ds->ds_sc_given)
  100. error("repeated storage class specifier");
  101. ds->ds_sc_given = 1;
  102. ds->ds_sc = DOT;
  103. }
  104. |
  105. VOLATILE
  106. { if (ds->ds_typequal & TQ_VOLATILE)
  107. error("repeated type qualifier");
  108. ds->ds_typequal |= TQ_VOLATILE;
  109. }
  110. |
  111. CONST
  112. { if (ds->ds_typequal & TQ_CONST)
  113. error("repeated type qualifier");
  114. ds->ds_typequal |= TQ_CONST;
  115. }
  116. |
  117. [ SHORT | LONG ]
  118. { if (ds->ds_size)
  119. error("repeated size specifier");
  120. ds->ds_size = DOT;
  121. }
  122. |
  123. [ SIGNED | UNSIGNED ]
  124. { if (ds->ds_unsigned != 0)
  125. error("repeated sign specifier");
  126. ds->ds_unsigned = DOT;
  127. }
  128. |
  129. [ VOID | CHAR | INT | FLOAT | DOUBLE ]
  130. {
  131. idf2type(dot.tk_idf, &ds->ds_type);
  132. ds->ds_typedef = 0;
  133. }
  134. |
  135. %default TYPE_IDENTIFIER
  136. {
  137. idf2type(dot.tk_idf, &ds->ds_type);
  138. ds->ds_typedef = 1;
  139. }
  140. |
  141. IDENTIFIER
  142. {
  143. error("%s is not a type identifier", dot.tk_idf->id_text);
  144. ds->ds_type = error_type;
  145. if (dot.tk_idf->id_def) {
  146. dot.tk_idf->id_def->df_type = error_type;
  147. dot.tk_idf->id_def->df_sc = TYPEDEF;
  148. }
  149. }
  150. |
  151. struct_or_union_specifier(&ds->ds_type)
  152. |
  153. enum_specifier(&ds->ds_type)
  154. ;
  155. /* 3.5.2 */
  156. type_specifier(struct type **tpp;)
  157. /* Used in struct/union declarations and in casts; only the
  158. type is relevant.
  159. */
  160. {struct decspecs Ds; Ds = null_decspecs;}
  161. :
  162. decl_specifiers(&Ds)
  163. {
  164. if (Ds.ds_sc_given)
  165. error("storage class ignored");
  166. if (Ds.ds_sc == REGISTER)
  167. error("register ignored");
  168. }
  169. {*tpp = Ds.ds_type;}
  170. ;
  171. /* 3.5 */
  172. init_declarator_list(struct decspecs *ds;):
  173. init_declarator(ds)
  174. [ ',' init_declarator(ds) ]*
  175. ;
  176. init_declarator(register struct decspecs *ds;)
  177. {
  178. struct declarator Dc;
  179. }
  180. :
  181. {
  182. Dc = null_declarator;
  183. }
  184. [
  185. declarator(&Dc)
  186. {
  187. reject_params(&Dc);
  188. declare_idf(ds, &Dc, level);
  189. #ifdef LINT
  190. lint_declare_idf(Dc.dc_idf, ds->ds_sc);
  191. #endif LINT
  192. }
  193. [
  194. initializer(Dc.dc_idf, ds->ds_sc)
  195. |
  196. { code_declaration(Dc.dc_idf, (struct expr *) 0, level, ds->ds_sc); }
  197. ]
  198. ]
  199. {
  200. #ifdef LINT
  201. add_auto(Dc.dc_idf);
  202. #endif LINT
  203. remove_declarator(&Dc);
  204. }
  205. ;
  206. /* 3.5.7: initializer */
  207. initializer(struct idf *idf; int sc;)
  208. {
  209. struct expr *expr = (struct expr *) 0;
  210. int fund = idf->id_def->df_type->tp_fund;
  211. int autoagg = (level >= L_LOCAL
  212. && sc != STATIC
  213. && ( fund == STRUCT
  214. || fund == UNION
  215. || fund == ARRAY));
  216. int globalflag = level == L_GLOBAL
  217. || (level >= L_LOCAL && sc == STATIC);
  218. }
  219. :
  220. { if (idf->id_def->df_type->tp_fund == FUNCTION) {
  221. error("illegal initialization of function");
  222. idf->id_def->df_type->tp_fund = ERRONEOUS;
  223. }
  224. if (level == L_FORMAL2)
  225. warning("illegal initialization of formal parameter (ignored)");
  226. }
  227. '='
  228. {
  229. if (AHEAD != '{' && AHEAD != STRING ) autoagg = 0;
  230. #ifdef LINT
  231. lint_statement();
  232. #endif LINT
  233. if (globalflag) {
  234. struct expr ex;
  235. code_declaration(idf, &ex, level, sc);
  236. }
  237. else if (autoagg)
  238. loc_init((struct expr *) 0, idf);
  239. }
  240. initial_value((globalflag || autoagg) ?
  241. &(idf->id_def->df_type)
  242. : (struct type **)0,
  243. &expr)
  244. { if (! globalflag) {
  245. if (idf->id_def->df_type->tp_fund == FUNCTION) {
  246. free_expression(expr);
  247. expr = 0;
  248. }
  249. #ifdef DEBUG
  250. print_expr("initializer-expression", expr);
  251. #endif DEBUG
  252. #ifdef LINT
  253. change_state(idf, SET);
  254. #endif LINT
  255. if (autoagg)
  256. loc_init((struct expr *) 0, idf);
  257. else code_declaration(idf, expr, level, sc);
  258. }
  259. init_idf(idf);
  260. }
  261. ;
  262. /*
  263. Functions yielding pointers to functions must be declared as, e.g.,
  264. int (*hehe(par1, par2))() char *par1, *par2; {}
  265. Since the function heading is read as a normal declarator,
  266. we just include the (formal) parameter list in the declarator
  267. description list dc.
  268. */
  269. /* 3.5.4 */
  270. declarator(register struct declarator *dc;)
  271. { struct formal *fm = NO_PARAMS;
  272. struct proto *pl = NO_PROTO;
  273. arith count;
  274. int qual;
  275. }
  276. :
  277. primary_declarator(dc)
  278. [/*%while(1)*/
  279. '('
  280. [ %if (DOT != IDENTIFIER && DOT != ')')
  281. parameter_type_list(&pl)
  282. |
  283. formal_list(&fm)
  284. |
  285. /* empty */
  286. ]
  287. ')'
  288. { add_decl_unary(dc, FUNCTION, 0, (arith)0, fm, pl);
  289. fm = NO_PARAMS;
  290. }
  291. |
  292. arrayer(&count)
  293. {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
  294. ]*
  295. |
  296. pointer(&qual) declarator(dc)
  297. {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
  298. ;
  299. primary_declarator(register struct declarator *dc;) :
  300. identifier(&dc->dc_idf)
  301. |
  302. '(' declarator(dc) ')'
  303. ;
  304. arrayer(arith *sizep;)
  305. { struct expr *expr; }
  306. :
  307. '['
  308. { *sizep = (arith)-1; }
  309. [
  310. constant_expression(&expr)
  311. {
  312. check_array_subscript(expr);
  313. *sizep = expr->VL_VALUE;
  314. free_expression(expr);
  315. }
  316. ]?
  317. ']'
  318. ;
  319. formal_list (struct formal **fmp;)
  320. :
  321. formal(fmp) [ %persistent ',' formal(fmp) ]*
  322. ;
  323. formal(struct formal **fmp;)
  324. {struct idf *idf; }
  325. :
  326. identifier(&idf)
  327. {
  328. register struct formal *new = new_formal();
  329. new->fm_idf = idf;
  330. new->next = *fmp;
  331. *fmp = new;
  332. }
  333. ;
  334. /* Change 2 */
  335. enum_specifier(register struct type **tpp;)
  336. {
  337. struct idf *idf;
  338. arith l = (arith)0;
  339. }
  340. :
  341. ENUM
  342. [
  343. {declare_struct(ENUM, (struct idf *) 0, tpp);}
  344. enumerator_pack(*tpp, &l)
  345. |
  346. identifier(&idf)
  347. [
  348. {declare_struct(ENUM, idf, tpp);}
  349. enumerator_pack(*tpp, &l)
  350. {
  351. #ifdef DBSYMTAB
  352. if (options['g']) {
  353. stb_tag(idf->id_enum, idf->id_text);
  354. }
  355. #endif /*DBSYMTAB */
  356. }
  357. |
  358. {apply_struct(ENUM, idf, tpp);}
  359. /* empty */
  360. ]
  361. ]
  362. ;
  363. enumerator_pack(register struct type *tp; arith *lp;) :
  364. '{'
  365. enumerator(tp, lp)
  366. [%while (AHEAD != '}')
  367. ','
  368. enumerator(tp, lp)
  369. ]*
  370. [
  371. ',' {warning("unexpected trailing comma in enumerator pack");}
  372. ]?
  373. '}'
  374. {tp->tp_size = int_size;}
  375. /* fancy implementations that put small enums in 1 byte
  376. or so should start here.
  377. */
  378. ;
  379. enumerator(struct type *tp; arith *lp;)
  380. {
  381. struct idf *idf;
  382. struct expr *expr;
  383. }
  384. :
  385. identifier(&idf)
  386. [
  387. '='
  388. constant_expression(&expr)
  389. {
  390. *lp = expr->VL_VALUE;
  391. free_expression(expr);
  392. }
  393. ]?
  394. {declare_enum(tp, idf, (*lp)++);}
  395. ;
  396. /* 8.5 */
  397. struct_or_union_specifier(register struct type **tpp;)
  398. {
  399. int fund;
  400. struct idf *idfX;
  401. register struct idf *idf;
  402. }
  403. :
  404. [ STRUCT | UNION ]
  405. {fund = DOT;}
  406. [
  407. {
  408. declare_struct(fund, (struct idf *)0, tpp);
  409. }
  410. struct_declaration_pack(*tpp)
  411. |
  412. identifier(&idfX) { idf = idfX; }
  413. [
  414. {
  415. declare_struct(fund, idf, tpp);
  416. (idf->id_struct->tg_busy)++;
  417. }
  418. struct_declaration_pack(*tpp)
  419. {
  420. (idf->id_struct->tg_busy)--;
  421. #ifdef DBSYMTAB
  422. if (options['g']) {
  423. stb_tag(idf->id_struct, idf->id_text);
  424. }
  425. #endif /*DBSYMTAB */
  426. }
  427. |
  428. {
  429. /* a ';' means an empty declaration (probably)
  430. * this means that we have to declare a new
  431. * structure. (yegh)
  432. */
  433. if (DOT == ';') declare_struct(fund, idf, tpp);
  434. else apply_struct(fund, idf, tpp);
  435. }
  436. /* empty */
  437. ]
  438. ]
  439. ;
  440. struct_declaration_pack(register struct type *stp;)
  441. {
  442. struct sdef **sdefp = &stp->tp_sdef;
  443. arith size = (arith)0;
  444. }
  445. :
  446. /* The size is only filled in after the whole struct has
  447. been read, to prevent recursive definitions.
  448. */
  449. '{'
  450. struct_declaration(stp, &sdefp, &size)+
  451. '}'
  452. {stp->tp_size = align(size, stp->tp_align);}
  453. ;
  454. struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
  455. {struct type *tp;}
  456. :
  457. type_specifier(&tp) struct_declarator_list(tp, stp, sdefpp, szp) ';'
  458. ;
  459. struct_declarator_list(struct type *tp, *stp;
  460. struct sdef ***sdefpp; arith *szp;)
  461. :
  462. struct_declarator(tp, stp, sdefpp, szp)
  463. [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
  464. ;
  465. struct_declarator(struct type *tp; struct type *stp;
  466. struct sdef ***sdefpp; arith *szp;)
  467. {
  468. struct declarator Dc;
  469. struct field *fd = 0;
  470. }
  471. :
  472. {
  473. Dc = null_declarator;
  474. }
  475. [
  476. declarator(&Dc)
  477. {reject_params(&Dc);}
  478. bit_expression(&fd)?
  479. |
  480. {Dc.dc_idf = gen_idf();}
  481. bit_expression(&fd)
  482. ]
  483. {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
  484. {remove_declarator(&Dc);}
  485. ;
  486. bit_expression(struct field **fd;)
  487. { struct expr *expr; }
  488. :
  489. {
  490. *fd = new_field();
  491. }
  492. ':'
  493. constant_expression(&expr)
  494. {
  495. (*fd)->fd_width = expr->VL_VALUE;
  496. free_expression(expr);
  497. #ifdef NOBITFIELD
  498. error("bitfields are not implemented");
  499. #endif NOBITFIELD
  500. }
  501. ;
  502. /* 8.7 */
  503. cast(struct type **tpp;)
  504. {struct declarator Dc;}
  505. :
  506. {Dc = null_declarator;}
  507. '('
  508. type_specifier(tpp)
  509. abstract_declarator(&Dc)
  510. ')'
  511. {*tpp = declare_type(*tpp, &Dc);}
  512. {remove_declarator(&Dc);}
  513. ;
  514. /* This code is an abject copy of that of 'declarator', for lack of
  515. a two-level grammar.
  516. */
  517. abstract_declarator(register struct declarator *dc;)
  518. { struct proto *pl = NO_PROTO;
  519. arith count;
  520. int qual;
  521. }
  522. :
  523. primary_abstract_declarator(dc)
  524. [
  525. '('
  526. [
  527. parameter_type_list(&pl)
  528. |
  529. /* empty */
  530. ]
  531. ')'
  532. {add_decl_unary(dc, FUNCTION, 0, (arith)0, NO_PARAMS, pl);}
  533. |
  534. arrayer(&count)
  535. {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
  536. ]*
  537. |
  538. pointer(&qual) abstract_declarator(dc)
  539. {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
  540. ;
  541. %first first_of_parameter_type_list, parameter_type_list;
  542. primary_abstract_declarator(struct declarator *dc;)
  543. :
  544. [%if (AHEAD == ')' || first_of_parameter_type_list(AHEAD))
  545. /* empty */
  546. |
  547. '(' abstract_declarator(dc) ')'
  548. ]
  549. ;
  550. parameter_type_list(struct proto **plp;)
  551. { int save_level; }
  552. :
  553. { if (level > L_PROTO) {
  554. save_level = level;
  555. level = L_PROTO;
  556. } else level--;
  557. }
  558. parameter_decl_list(plp)
  559. [
  560. ',' ELLIPSIS
  561. { register struct proto *new = new_proto();
  562. new->next = *plp;
  563. new->pl_flag = PL_ELLIPSIS;
  564. *plp = new;
  565. }
  566. ]?
  567. { check_for_void(*plp);
  568. if (level == L_PROTO)
  569. level = save_level;
  570. else level++;
  571. }
  572. ;
  573. parameter_decl_list(struct proto **plp;)
  574. :
  575. parameter_decl(plp)
  576. [ %while (AHEAD != ELLIPSIS)
  577. %persistent
  578. ',' parameter_decl(plp)
  579. ]*
  580. ;
  581. parameter_decl(struct proto **plp;)
  582. { register struct proto *new = new_proto();
  583. struct declarator Dc;
  584. struct decspecs Ds;
  585. }
  586. :
  587. { Dc = null_declarator;
  588. Ds = null_decspecs;
  589. }
  590. decl_specifiers(&Ds)
  591. parameter_declarator(&Dc)
  592. { add_proto(new, &Ds, &Dc, level);
  593. new->next = *plp;
  594. *plp = new;
  595. }
  596. ;
  597. /* This is weird. Due to the LR structure of the ANSI C grammar
  598. we have to duplicate the actions of 'declarator' and
  599. 'abstract_declarator'. Calling these separate, as in
  600. parameter_decl:
  601. decl_specifiers
  602. [
  603. declarator
  604. |
  605. abstract_declarator
  606. ]
  607. gives us a conflict on the terminals '(' and '*'. E.i. on
  608. some input, it is impossible to decide which rule we take.
  609. Combining the two declarators into one common declarator
  610. is out of the question, since this results in an empty
  611. string for the non-terminal 'declarator'.
  612. So we combine the two only for the use of parameter_decl,
  613. since this is the only place where they don't give
  614. conflicts. However, this makes the grammar messy.
  615. */
  616. parameter_declarator(register struct declarator *dc;)
  617. { struct formal *fm = NO_PARAMS;
  618. struct proto *pl = NO_PROTO;
  619. arith count;
  620. int qual;
  621. }
  622. :
  623. primary_parameter_declarator(dc)
  624. [
  625. '('
  626. [ %if (DOT != IDENTIFIER)
  627. parameter_type_list(&pl)
  628. |
  629. formal_list(&fm)
  630. ]?
  631. ')'
  632. { add_decl_unary(dc, FUNCTION, 0, (arith)0, fm, pl);
  633. reject_params(dc);
  634. }
  635. |
  636. arrayer(&count)
  637. {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
  638. ]*
  639. |
  640. pointer(&qual) parameter_declarator(dc)
  641. {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
  642. ;
  643. primary_parameter_declarator(register struct declarator *dc;)
  644. :
  645. [%if (AHEAD == ')' || first_of_parameter_type_list(AHEAD)
  646. && (AHEAD != IDENTIFIER))
  647. /* empty */
  648. |
  649. identifier(&dc->dc_idf)
  650. |
  651. '(' parameter_declarator(dc) ')'
  652. ]
  653. ;
  654. pointer(int *qual;)
  655. :
  656. '*' type_qualifier_list(qual)
  657. ;
  658. /* Type qualifiers may come in three flavours:
  659. volatile, const, const volatile.
  660. These all have different semantic properties:
  661. volatile:
  662. means that the object can be modified
  663. without prior knowledge of the implementation.
  664. const:
  665. means that the object can not be modified; thus
  666. it's illegal to use this as a l-value.
  667. const volatile:
  668. means that the object can be modified without
  669. prior knowledge of the implementation, but may
  670. not be used as a l-value.
  671. */
  672. /* 3.5.4 */
  673. type_qualifier_list(int *qual;)
  674. :
  675. { *qual = 0; }
  676. [
  677. VOLATILE
  678. { if (*qual & TQ_VOLATILE)
  679. error("repeated type qualifier");
  680. *qual |= TQ_VOLATILE;
  681. }
  682. |
  683. CONST
  684. { if (*qual & TQ_CONST)
  685. error("repeated type qualifier");
  686. *qual |= TQ_CONST;
  687. }
  688. ]*
  689. ;
  690. empty:
  691. ;