declar.g 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* $Header$ */
  2. /* DECLARATION SYNTAX PARSER */
  3. {
  4. #include "nobitfield.h"
  5. #include "debug.h"
  6. #include "arith.h"
  7. #include "LLlex.h"
  8. #include "idf.h"
  9. #include "type.h"
  10. #include "struct.h"
  11. #include "field.h"
  12. #include "decspecs.h"
  13. #include "def.h"
  14. #include "declarator.h"
  15. #include "label.h"
  16. #include "expr.h"
  17. #include "sizes.h"
  18. extern char options[];
  19. }
  20. /* 8 */
  21. declaration
  22. {struct decspecs Ds;}
  23. :
  24. {Ds = null_decspecs;}
  25. decl_specifiers(&Ds)
  26. init_declarator_list(&Ds)?
  27. ';'
  28. ;
  29. /* A `decl_specifiers' describes a sequence of a storage_class_specifier,
  30. an unsigned_specifier, a size_specifier and a simple type_specifier,
  31. which may occur in arbitrary order and each of which may be absent;
  32. at least one of them must be present, however, since the totally
  33. empty case has already be dealt with in `external_definition'.
  34. This means that something like:
  35. unsigned extern int short xx;
  36. is perfectly good C.
  37. On top of that, multiple occurrences of storage_class_specifiers,
  38. unsigned_specifiers and size_specifiers are errors, but a second
  39. type_specifier should end the decl_specifiers and be treated as
  40. the name to be declared (see the thin ice in RM11.1).
  41. Such a language is not easily expressed in a grammar; enumeration
  42. of the permutations is unattractive. We solve the problem by
  43. having a regular grammar for the "soft" items, handling the single
  44. occurrence of the type_specifier in the grammar (we have no choice),
  45. collecting all data in a `struct decspecs' and turning that data
  46. structure into what we want.
  47. The existence of declarations like
  48. short typedef yepp;
  49. makes all hope of writing a specific grammar for typedefs illusory.
  50. */
  51. decl_specifiers /* non-empty */ (struct decspecs *ds;)
  52. /* Reads a non-empty decl_specifiers and fills the struct
  53. decspecs *ds.
  54. */
  55. :
  56. [
  57. other_specifier(ds)+
  58. [%prefer /* the thin ice in R.M. 11.1 */
  59. single_type_specifier(ds) other_specifier(ds)*
  60. |
  61. empty
  62. ]
  63. |
  64. single_type_specifier(ds) other_specifier(ds)*
  65. ]
  66. {do_decspecs(ds);}
  67. ;
  68. /* 8.1 */
  69. other_specifier(struct decspecs *ds;):
  70. [
  71. [ AUTO | STATIC | EXTERN | TYPEDEF | REGISTER ]
  72. { if (ds->ds_sc_given)
  73. error("repeated storage class specifier");
  74. else {
  75. ds->ds_sc_given = 1;
  76. ds->ds_sc = DOT;
  77. }
  78. }
  79. |
  80. [ SHORT | LONG ]
  81. { if (ds->ds_size)
  82. error("repeated size specifier");
  83. else ds->ds_size = DOT;
  84. }
  85. |
  86. UNSIGNED
  87. { if (ds->ds_unsigned)
  88. error("unsigned specified twice");
  89. else ds->ds_unsigned = 1;
  90. }
  91. ]
  92. ;
  93. /* 8.2 */
  94. type_specifier(struct type **tpp;)
  95. /* Used in struct/union declarations and in casts; only the
  96. type is relevant.
  97. */
  98. {struct decspecs Ds; Ds = null_decspecs;}
  99. :
  100. decl_specifiers(&Ds)
  101. {
  102. if (Ds.ds_sc_given)
  103. error("storage class ignored");
  104. if (Ds.ds_sc == REGISTER)
  105. error("register ignored");
  106. }
  107. {*tpp = Ds.ds_type;}
  108. ;
  109. single_type_specifier(struct decspecs *ds;):
  110. [
  111. TYPE_IDENTIFIER /* this includes INT, CHAR, etc. */
  112. {idf2type(dot.tk_idf, &ds->ds_type);}
  113. |
  114. struct_or_union_specifier(&ds->ds_type)
  115. |
  116. enum_specifier(&ds->ds_type)
  117. ]
  118. ;
  119. /* 8.3 */
  120. init_declarator_list(struct decspecs *ds;):
  121. init_declarator(ds)
  122. [ ',' init_declarator(ds) ]*
  123. ;
  124. init_declarator(struct decspecs *ds;)
  125. {
  126. struct declarator Dc;
  127. struct expr *expr = (struct expr *) 0;
  128. }
  129. :
  130. {
  131. Dc = null_declarator;
  132. }
  133. [
  134. declarator(&Dc)
  135. {
  136. reject_params(&Dc);
  137. declare_idf(ds, &Dc, level);
  138. }
  139. initializer(Dc.dc_idf, &expr)?
  140. {
  141. code_declaration(Dc.dc_idf, expr, level, ds->ds_sc);
  142. free_expression(expr);
  143. }
  144. ]
  145. {remove_declarator(&Dc);}
  146. ;
  147. /*
  148. Functions yielding pointers to functions must be declared as, e.g.,
  149. int (*hehe(par1, par2))() char *par1, *par2; {}
  150. Since the function heading is read as a normal declarator,
  151. we just include the (formal) parameter list in the declarator
  152. description list dc.
  153. */
  154. declarator(struct declarator *dc;)
  155. {
  156. arith count;
  157. struct idstack_item *is = 0;
  158. }
  159. :
  160. [
  161. primary_declarator(dc)
  162. [%while(1) /* int i (M + 2) / 4;
  163. is a function, not an
  164. old-fashioned initialization.
  165. */
  166. '('
  167. formal_list(&is) ? /* semantic check later... */
  168. ')'
  169. {
  170. add_decl_unary(dc, FUNCTION, (arith)0, is);
  171. is = 0;
  172. }
  173. |
  174. arrayer(&count)
  175. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  176. ]*
  177. |
  178. '*' declarator(dc)
  179. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  180. ]
  181. ;
  182. primary_declarator(struct declarator *dc;) :
  183. [
  184. identifier(&dc->dc_idf)
  185. |
  186. '(' declarator(dc) ')'
  187. ]
  188. ;
  189. arrayer(arith *sizep;)
  190. { struct expr *expr; }
  191. :
  192. '['
  193. [
  194. constant_expression(&expr)
  195. {
  196. array_subscript(expr);
  197. *sizep = expr->VL_VALUE;
  198. free_expression(expr);
  199. }
  200. |
  201. empty
  202. { *sizep = (arith)-1; }
  203. ]
  204. ']'
  205. ;
  206. formal_list (struct idstack_item **is;)
  207. :
  208. formal(is) [ ',' formal(is) ]*
  209. ;
  210. formal(struct idstack_item **is;)
  211. {struct idf *idf; }
  212. :
  213. identifier(&idf)
  214. {
  215. struct idstack_item *new = new_idstack_item();
  216. new->is_idf = idf;
  217. new->next = *is;
  218. *is = new;
  219. }
  220. ;
  221. /* Change 2 */
  222. enum_specifier(struct type **tpp;)
  223. {
  224. struct idf *idf;
  225. arith l = (arith)0;
  226. }
  227. :
  228. ENUM
  229. [
  230. {declare_struct(ENUM, (struct idf *) 0, tpp);}
  231. enumerator_pack(*tpp, &l)
  232. |
  233. identifier(&idf)
  234. [
  235. {declare_struct(ENUM, idf, tpp);}
  236. enumerator_pack(*tpp, &l)
  237. |
  238. {apply_struct(ENUM, idf, tpp);}
  239. empty
  240. ]
  241. ]
  242. ;
  243. enumerator_pack(struct type *tp; arith *lp;) :
  244. '{'
  245. enumerator(tp, lp)
  246. [%while(AHEAD != '}') /* >>> conflict on ',' */
  247. ','
  248. enumerator(tp, lp)
  249. ]*
  250. ','? /* optional trailing comma */
  251. '}'
  252. {tp->tp_size = int_size;}
  253. /* fancy implementations that put small enums in 1 byte
  254. or so should start here.
  255. */
  256. ;
  257. enumerator(struct type *tp; arith *lp;)
  258. {
  259. struct idf *idf;
  260. struct expr *expr;
  261. }
  262. :
  263. identifier(&idf)
  264. [
  265. '='
  266. constant_expression(&expr)
  267. {
  268. *lp = expr->VL_VALUE;
  269. free_expression(expr);
  270. }
  271. ]?
  272. {declare_enum(tp, idf, (*lp)++);}
  273. ;
  274. /* 8.5 */
  275. struct_or_union_specifier(struct type **tpp;)
  276. {
  277. int fund;
  278. struct idf *idf;
  279. }
  280. :
  281. [ STRUCT | UNION ]
  282. {fund = DOT;}
  283. [
  284. {
  285. declare_struct(fund, (struct idf *)0, tpp);
  286. }
  287. struct_declaration_pack(*tpp)
  288. |
  289. identifier(&idf)
  290. [
  291. {
  292. declare_struct(fund, idf, tpp);
  293. (idf->id_struct->tg_busy)++;
  294. }
  295. struct_declaration_pack(*tpp)
  296. {
  297. (idf->id_struct->tg_busy)--;
  298. }
  299. |
  300. {apply_struct(fund, idf, tpp);}
  301. empty
  302. ]
  303. ]
  304. ;
  305. struct_declaration_pack(struct type *stp;)
  306. {
  307. struct sdef **sdefp = &stp->tp_sdef;
  308. arith size = (arith)0;
  309. }
  310. :
  311. /* The size is only filled in after the whole struct has
  312. been read, to prevent recursive definitions.
  313. */
  314. '{'
  315. struct_declaration(stp, &sdefp, &size)+
  316. '}'
  317. {stp->tp_size = align(size, stp->tp_align);}
  318. ;
  319. struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
  320. {struct type *tp;}
  321. :
  322. type_specifier(&tp)
  323. struct_declarator_list(tp, stp, sdefpp, szp)
  324. [ /* in some standard UNIX compilers the semicolon
  325. is optional, would you believe!
  326. */
  327. ';'
  328. |
  329. empty
  330. {warning("no semicolon after declarator");}
  331. ]
  332. ;
  333. struct_declarator_list(struct type *tp, *stp;
  334. struct sdef ***sdefpp; arith *szp;)
  335. :
  336. struct_declarator(tp, stp, sdefpp, szp)
  337. [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
  338. ;
  339. struct_declarator(struct type *tp; struct type *stp;
  340. struct sdef ***sdefpp; arith *szp;)
  341. {
  342. struct declarator Dc;
  343. struct field *fd = 0;
  344. }
  345. :
  346. {
  347. Dc = null_declarator;
  348. }
  349. [
  350. declarator(&Dc)
  351. {reject_params(&Dc);}
  352. bit_expression(&fd)?
  353. |
  354. {Dc.dc_idf = gen_idf();}
  355. bit_expression(&fd)
  356. ]
  357. {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
  358. {remove_declarator(&Dc);}
  359. ;
  360. bit_expression(struct field **fd;)
  361. { struct expr *expr; }
  362. :
  363. {
  364. *fd = new_field();
  365. }
  366. ':'
  367. constant_expression(&expr)
  368. {
  369. (*fd)->fd_width = expr->VL_VALUE;
  370. free_expression(expr);
  371. #ifdef NOBITFIELD
  372. error("bitfields are not implemented");
  373. #endif NOBITFIELD
  374. }
  375. ;
  376. /* 8.6 */
  377. initializer(struct idf *idf; struct expr **expp;) :
  378. [
  379. '='
  380. |
  381. empty
  382. {warning("old-fashioned initialization, insert =");}
  383. /* This causes trouble at declarator and at
  384. external_definition, q.v.
  385. */
  386. ]
  387. initial_value(expp)
  388. {
  389. if (idf->id_def->df_type->tp_fund == FUNCTION) {
  390. error("illegal initialization of function");
  391. free_expression(*expp);
  392. *expp = 0;
  393. }
  394. init_idf(idf);
  395. #ifdef DEBUG
  396. print_expr("initializer-expression", *expp);
  397. #endif DEBUG
  398. }
  399. ;
  400. /* 8.7 */
  401. cast(struct type **tpp;) {struct declarator Dc;} :
  402. {Dc = null_declarator;}
  403. '('
  404. type_specifier(tpp)
  405. abstract_declarator(&Dc)
  406. ')'
  407. {*tpp = declare_type(*tpp, &Dc);}
  408. {remove_declarator(&Dc);}
  409. ;
  410. /* This code is an abject copy of that of 'declarator', for lack of
  411. a two-level grammar.
  412. */
  413. abstract_declarator(struct declarator *dc;)
  414. {arith count;}
  415. :
  416. [
  417. primary_abstract_declarator(dc)
  418. [
  419. '(' ')'
  420. {add_decl_unary(dc, FUNCTION, (arith)0, NO_PARAMS);}
  421. |
  422. arrayer(&count)
  423. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  424. ]*
  425. |
  426. '*' abstract_declarator(dc)
  427. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  428. ]
  429. ;
  430. primary_abstract_declarator(struct declarator *dc;) :
  431. [%if (AHEAD == ')')
  432. empty
  433. |
  434. '(' abstract_declarator(dc) ')'
  435. ]
  436. ;
  437. empty:
  438. ;
  439. /* 8.8 */
  440. /* included in the IDENTIFIER/TYPE_IDENTIFIER mechanism */