declar.g 14 KB

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