declar.g 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* $Header$ */
  2. /* DECLARATION SYNTAX PARSER */
  3. {
  4. #include <alloc.h>
  5. #include "nobitfield.h"
  6. #include "debug.h"
  7. #include "arith.h"
  8. #include "LLlex.h"
  9. #include "idf.h"
  10. #include "type.h"
  11. #include "struct.h"
  12. #include "field.h"
  13. #include "decspecs.h"
  14. #include "def.h"
  15. #include "declar.h"
  16. #include "label.h"
  17. #include "expr.h"
  18. #include "sizes.h"
  19. extern char options[];
  20. }
  21. /* 8 */
  22. declaration
  23. {struct decspecs Ds;}
  24. :
  25. {Ds = null_decspecs;}
  26. decl_specifiers(&Ds)
  27. init_declarator_list(&Ds)?
  28. ';'
  29. ;
  30. /* A `decl_specifiers' describes a sequence of a storage_class_specifier,
  31. an unsigned_specifier, a size_specifier and a simple type_specifier,
  32. which may occur in arbitrary order and each of which may be absent;
  33. at least one of them must be present, however, since the totally
  34. empty case has already be dealt with in `external_definition'.
  35. This means that something like:
  36. unsigned extern int short xx;
  37. is perfectly good C.
  38. On top of that, multiple occurrences of storage_class_specifiers,
  39. unsigned_specifiers and size_specifiers are errors, but a second
  40. type_specifier should end the decl_specifiers and be treated as
  41. the name to be declared (see the thin ice in RM11.1).
  42. Such a language is not easily expressed in a grammar; enumeration
  43. of the permutations is unattractive. We solve the problem by
  44. having a regular grammar for the "soft" items, handling the single
  45. occurrence of the type_specifier in the grammar (we have no choice),
  46. collecting all data in a `struct decspecs' and turning that data
  47. structure into what we want.
  48. The existence of declarations like
  49. short typedef yepp;
  50. makes all hope of writing a specific grammar for typedefs illusory.
  51. */
  52. decl_specifiers /* non-empty */ (struct decspecs *ds;)
  53. /* Reads a non-empty decl_specifiers and fills the struct
  54. decspecs *ds.
  55. */
  56. :
  57. [
  58. other_specifier(ds)+
  59. [%prefer /* the thin ice in R.M. 11.1 */
  60. single_type_specifier(ds) other_specifier(ds)*
  61. |
  62. empty
  63. ]
  64. |
  65. single_type_specifier(ds) other_specifier(ds)*
  66. ]
  67. {do_decspecs(ds);}
  68. ;
  69. /* 8.1 */
  70. other_specifier(struct decspecs *ds;):
  71. [
  72. [ AUTO | STATIC | EXTERN | TYPEDEF | REGISTER ]
  73. { if (ds->ds_sc_given)
  74. error("repeated storage class specifier");
  75. else {
  76. ds->ds_sc_given = 1;
  77. ds->ds_sc = DOT;
  78. }
  79. }
  80. |
  81. [ SHORT | LONG ]
  82. { if (ds->ds_size)
  83. error("repeated size specifier");
  84. else ds->ds_size = DOT;
  85. }
  86. |
  87. UNSIGNED
  88. { if (ds->ds_unsigned)
  89. error("unsigned specified twice");
  90. else ds->ds_unsigned = 1;
  91. }
  92. ]
  93. ;
  94. /* 8.2 */
  95. type_specifier(struct type **tpp;)
  96. /* Used in struct/union declarations and in casts; only the
  97. type is relevant.
  98. */
  99. {struct decspecs Ds; Ds = null_decspecs;}
  100. :
  101. decl_specifiers(&Ds)
  102. {
  103. if (Ds.ds_sc_given)
  104. error("storage class ignored");
  105. if (Ds.ds_sc == REGISTER)
  106. error("register ignored");
  107. }
  108. {*tpp = Ds.ds_type;}
  109. ;
  110. single_type_specifier(struct decspecs *ds;):
  111. [
  112. TYPE_IDENTIFIER /* this includes INT, CHAR, etc. */
  113. {idf2type(dot.tk_idf, &ds->ds_type);}
  114. |
  115. struct_or_union_specifier(&ds->ds_type)
  116. |
  117. enum_specifier(&ds->ds_type)
  118. ]
  119. ;
  120. /* 8.3 */
  121. init_declarator_list(struct decspecs *ds;):
  122. init_declarator(ds)
  123. [ ',' init_declarator(ds) ]*
  124. ;
  125. init_declarator(struct decspecs *ds;)
  126. {
  127. struct declarator Dc;
  128. struct expr *expr = (struct expr *) 0;
  129. }
  130. :
  131. {
  132. Dc = null_declarator;
  133. }
  134. [
  135. declarator(&Dc)
  136. {
  137. reject_params(&Dc);
  138. declare_idf(ds, &Dc, level);
  139. }
  140. initializer(Dc.dc_idf, &expr)?
  141. {
  142. code_declaration(Dc.dc_idf, expr, level, ds->ds_sc);
  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 formal *fm = 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(&fm) ? /* semantic check later... */
  168. ')'
  169. {
  170. add_decl_unary(dc, FUNCTION, (arith)0, fm);
  171. fm = 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. check_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 formal **fmp;)
  207. :
  208. formal(fmp) [ ',' formal(fmp) ]*
  209. ;
  210. formal(struct formal **fmp;)
  211. {struct idf *idf; }
  212. :
  213. identifier(&idf)
  214. {
  215. struct formal *new = new_formal();
  216. new->fm_idf = idf;
  217. new->next = *fmp;
  218. *fmp = 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 */