declar.g 8.9 KB

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