declar.g 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 "struct.h"
  19. #include "field.h"
  20. #include "decspecs.h"
  21. #include "def.h"
  22. #include "declar.h"
  23. #include "label.h"
  24. #include "expr.h"
  25. #include "sizes.h"
  26. #include "level.h"
  27. #ifdef LINT
  28. #include "l_lint.h"
  29. #include "l_state.h"
  30. #endif LINT
  31. extern char options[];
  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. [ AUTO | STATIC | EXTERN | TYPEDEF | REGISTER ]
  85. { if (ds->ds_sc_given)
  86. error("repeated storage class specifier");
  87. ds->ds_sc_given = 1;
  88. ds->ds_sc = DOT;
  89. }
  90. |
  91. [ SHORT | LONG ]
  92. { if (ds->ds_size)
  93. error("repeated size specifier");
  94. ds->ds_size = DOT;
  95. }
  96. |
  97. UNSIGNED
  98. { if (ds->ds_unsigned)
  99. error("unsigned specified twice");
  100. ds->ds_unsigned = 1;
  101. }
  102. ;
  103. /* 8.2 */
  104. type_specifier(struct type **tpp;)
  105. /* Used in struct/union declarations and in casts; only the
  106. type is relevant.
  107. */
  108. {struct decspecs Ds; Ds = null_decspecs;}
  109. :
  110. decl_specifiers(&Ds)
  111. {
  112. if (Ds.ds_sc_given)
  113. error("storage class ignored");
  114. if (Ds.ds_sc == REGISTER)
  115. error("register ignored");
  116. }
  117. {*tpp = Ds.ds_type;}
  118. ;
  119. single_type_specifier(register struct decspecs *ds;):
  120. %default TYPE_IDENTIFIER /* this includes INT, CHAR, etc. */
  121. {idf2type(dot.tk_idf, &ds->ds_type);}
  122. |
  123. IDENTIFIER
  124. {
  125. error("%s is not a type identifier", dot.tk_idf->id_text);
  126. ds->ds_type = error_type;
  127. if (dot.tk_idf->id_def) {
  128. dot.tk_idf->id_def->df_type = error_type;
  129. dot.tk_idf->id_def->df_sc = TYPEDEF;
  130. }
  131. }
  132. |
  133. struct_or_union_specifier(&ds->ds_type)
  134. |
  135. enum_specifier(&ds->ds_type)
  136. ;
  137. /* 8.3 */
  138. init_declarator_list(struct decspecs *ds;):
  139. init_declarator(ds)
  140. [ ',' init_declarator(ds) ]*
  141. ;
  142. init_declarator(register struct decspecs *ds;)
  143. {
  144. struct declarator Dc;
  145. }
  146. :
  147. {
  148. Dc = null_declarator;
  149. }
  150. [
  151. declarator(&Dc)
  152. {
  153. reject_params(&Dc);
  154. declare_idf(ds, &Dc, level);
  155. #ifdef LINT
  156. lint_declare_idf(Dc.dc_idf, ds->ds_sc);
  157. #endif LINT
  158. }
  159. [
  160. initializer(Dc.dc_idf, ds->ds_sc)
  161. |
  162. { code_declaration(Dc.dc_idf, (struct expr *) 0, level, ds->ds_sc); }
  163. ]
  164. ]
  165. {
  166. #ifdef LINT
  167. add_auto(Dc.dc_idf);
  168. #endif LINT
  169. remove_declarator(&Dc);
  170. }
  171. ;
  172. /* 8.6: initializer */
  173. initializer(struct idf *idf; int sc;)
  174. {
  175. struct expr *expr = (struct expr *) 0;
  176. int globalflag = level == L_GLOBAL ||
  177. (level >= L_LOCAL && sc == STATIC);
  178. }
  179. :
  180. { if (idf->id_def->df_type->tp_fund == FUNCTION) {
  181. error("illegal initialization of function");
  182. idf->id_def->df_type->tp_fund = ERRONEOUS;
  183. }
  184. }
  185. [
  186. '='
  187. |
  188. empty
  189. {warning("old-fashioned initialization, insert =");}
  190. /* This causes trouble at declarator and at
  191. external_definition, q.v.
  192. */
  193. ]
  194. {
  195. #ifdef LINT
  196. lint_statement();
  197. #endif LINT
  198. if (globalflag) {
  199. struct expr ex;
  200. code_declaration(idf, &ex, level, sc);
  201. }
  202. }
  203. initial_value(globalflag ? &(idf->id_def->df_type) : (struct type **)0,
  204. &expr)
  205. { if (! globalflag) {
  206. if (idf->id_def->df_type->tp_fund == FUNCTION) {
  207. free_expression(expr);
  208. expr = 0;
  209. }
  210. #ifdef DEBUG
  211. print_expr("initializer-expression", expr);
  212. #endif DEBUG
  213. #ifdef LINT
  214. change_state(idf, SET);
  215. pre_lint_expr(expr, RVAL, USED);
  216. #endif LINT
  217. code_declaration(idf, expr, level, sc);
  218. }
  219. init_idf(idf);
  220. }
  221. ;
  222. /*
  223. Functions yielding pointers to functions must be declared as, e.g.,
  224. int (*hehe(par1, par2))() char *par1, *par2; {}
  225. Since the function heading is read as a normal declarator,
  226. we just include the (formal) parameter list in the declarator
  227. description list dc.
  228. */
  229. declarator(register struct declarator *dc;)
  230. {
  231. arith count;
  232. struct formal *fm = 0;
  233. }
  234. :
  235. primary_declarator(dc)
  236. [%while(1) /* int i (M + 2) / 4;
  237. is a function, not an
  238. old-fashioned initialization.
  239. */
  240. '('
  241. formal_list(&fm) ? /* semantic check later... */
  242. ')'
  243. {
  244. add_decl_unary(dc, FUNCTION, (arith)0, fm);
  245. fm = 0;
  246. }
  247. |
  248. arrayer(&count)
  249. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  250. ]*
  251. |
  252. '*' declarator(dc)
  253. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  254. ;
  255. primary_declarator(register struct declarator *dc;) :
  256. identifier(&dc->dc_idf)
  257. |
  258. '(' declarator(dc) ')'
  259. ;
  260. arrayer(arith *sizep;)
  261. { struct expr *expr; }
  262. :
  263. '['
  264. [
  265. constant_expression(&expr)
  266. {
  267. check_array_subscript(expr);
  268. *sizep = expr->VL_VALUE;
  269. free_expression(expr);
  270. }
  271. |
  272. empty
  273. { *sizep = (arith)-1; }
  274. ]
  275. ']'
  276. ;
  277. formal_list (struct formal **fmp;)
  278. :
  279. formal(fmp) [ ',' formal(fmp) ]*
  280. ;
  281. formal(struct formal **fmp;)
  282. {struct idf *idf; }
  283. :
  284. identifier(&idf)
  285. {
  286. register struct formal *new = new_formal();
  287. new->fm_idf = idf;
  288. new->next = *fmp;
  289. *fmp = new;
  290. }
  291. ;
  292. /* Change 2 */
  293. enum_specifier(register struct type **tpp;)
  294. {
  295. struct idf *idf;
  296. arith l = (arith)0;
  297. }
  298. :
  299. ENUM
  300. [
  301. {declare_struct(ENUM, (struct idf *) 0, tpp);}
  302. enumerator_pack(*tpp, &l)
  303. |
  304. identifier(&idf)
  305. [
  306. {declare_struct(ENUM, idf, tpp);}
  307. enumerator_pack(*tpp, &l)
  308. |
  309. {apply_struct(ENUM, idf, tpp);}
  310. empty
  311. ]
  312. ]
  313. ;
  314. enumerator_pack(register struct type *tp; arith *lp;) :
  315. '{'
  316. enumerator(tp, lp)
  317. [%while(AHEAD != '}') /* >>> conflict on ',' */
  318. ','
  319. enumerator(tp, lp)
  320. ]*
  321. ','? /* optional trailing comma */
  322. '}'
  323. {tp->tp_size = int_size;}
  324. /* fancy implementations that put small enums in 1 byte
  325. or so should start here.
  326. */
  327. ;
  328. enumerator(struct type *tp; arith *lp;)
  329. {
  330. struct idf *idf;
  331. struct expr *expr;
  332. }
  333. :
  334. identifier(&idf)
  335. [
  336. '='
  337. constant_expression(&expr)
  338. {
  339. *lp = expr->VL_VALUE;
  340. free_expression(expr);
  341. }
  342. ]?
  343. {declare_enum(tp, idf, (*lp)++);}
  344. ;
  345. /* 8.5 */
  346. struct_or_union_specifier(register struct type **tpp;)
  347. {
  348. int fund;
  349. struct idf *idfX;
  350. register struct idf *idf;
  351. }
  352. :
  353. [ STRUCT | UNION ]
  354. {fund = DOT;}
  355. [
  356. {
  357. declare_struct(fund, (struct idf *)0, tpp);
  358. }
  359. struct_declaration_pack(*tpp)
  360. |
  361. identifier(&idfX) { idf = idfX; }
  362. [
  363. {
  364. declare_struct(fund, idf, tpp);
  365. (idf->id_struct->tg_busy)++;
  366. }
  367. struct_declaration_pack(*tpp)
  368. {
  369. (idf->id_struct->tg_busy)--;
  370. }
  371. |
  372. {apply_struct(fund, idf, tpp);}
  373. empty
  374. ]
  375. ]
  376. ;
  377. struct_declaration_pack(register struct type *stp;)
  378. {
  379. struct sdef **sdefp = &stp->tp_sdef;
  380. arith size = (arith)0;
  381. }
  382. :
  383. /* The size is only filled in after the whole struct has
  384. been read, to prevent recursive definitions.
  385. */
  386. '{'
  387. struct_declaration(stp, &sdefp, &size)+
  388. '}'
  389. {stp->tp_size = align(size, stp->tp_align);}
  390. ;
  391. struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
  392. {struct type *tp;}
  393. :
  394. type_specifier(&tp)
  395. struct_declarator_list(tp, stp, sdefpp, szp)
  396. [ /* in some standard UNIX compilers the semicolon
  397. is optional, would you believe!
  398. */
  399. ';'
  400. |
  401. empty
  402. {warning("no semicolon after declarator");}
  403. ]
  404. ;
  405. struct_declarator_list(struct type *tp, *stp;
  406. struct sdef ***sdefpp; arith *szp;)
  407. :
  408. struct_declarator(tp, stp, sdefpp, szp)
  409. [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
  410. ;
  411. struct_declarator(struct type *tp; struct type *stp;
  412. struct sdef ***sdefpp; arith *szp;)
  413. {
  414. struct declarator Dc;
  415. struct field *fd = 0;
  416. }
  417. :
  418. {
  419. Dc = null_declarator;
  420. }
  421. [
  422. declarator(&Dc)
  423. {reject_params(&Dc);}
  424. bit_expression(&fd)?
  425. |
  426. {Dc.dc_idf = gen_idf();}
  427. bit_expression(&fd)
  428. ]
  429. {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
  430. {remove_declarator(&Dc);}
  431. ;
  432. bit_expression(struct field **fd;)
  433. { struct expr *expr; }
  434. :
  435. {
  436. *fd = new_field();
  437. }
  438. ':'
  439. constant_expression(&expr)
  440. {
  441. (*fd)->fd_width = expr->VL_VALUE;
  442. free_expression(expr);
  443. #ifdef NOBITFIELD
  444. error("bitfields are not implemented");
  445. #endif NOBITFIELD
  446. }
  447. ;
  448. /* 8.7 */
  449. cast(struct type **tpp;) {struct declarator Dc;} :
  450. {Dc = null_declarator;}
  451. '('
  452. type_specifier(tpp)
  453. abstract_declarator(&Dc)
  454. ')'
  455. {*tpp = declare_type(*tpp, &Dc);}
  456. {remove_declarator(&Dc);}
  457. ;
  458. /* This code is an abject copy of that of 'declarator', for lack of
  459. a two-level grammar.
  460. */
  461. abstract_declarator(register struct declarator *dc;)
  462. {arith count;}
  463. :
  464. primary_abstract_declarator(dc)
  465. [
  466. '(' ')'
  467. {add_decl_unary(dc, FUNCTION, (arith)0, NO_PARAMS);}
  468. |
  469. arrayer(&count)
  470. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  471. ]*
  472. |
  473. '*' abstract_declarator(dc)
  474. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  475. ;
  476. primary_abstract_declarator(struct declarator *dc;) :
  477. [%if (AHEAD == ')')
  478. empty
  479. |
  480. '(' abstract_declarator(dc) ')'
  481. ]
  482. ;
  483. empty:
  484. ;
  485. /* 8.8 */
  486. /* included in the IDENTIFIER/TYPE_IDENTIFIER mechanism */