declar.g 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. #endif LINT
  216. code_declaration(idf, expr, level, sc);
  217. }
  218. init_idf(idf);
  219. }
  220. ;
  221. /*
  222. Functions yielding pointers to functions must be declared as, e.g.,
  223. int (*hehe(par1, par2))() char *par1, *par2; {}
  224. Since the function heading is read as a normal declarator,
  225. we just include the (formal) parameter list in the declarator
  226. description list dc.
  227. */
  228. declarator(register struct declarator *dc;)
  229. {
  230. arith count;
  231. struct formal *fm = 0;
  232. }
  233. :
  234. primary_declarator(dc)
  235. [%while(1) /* int i (M + 2) / 4;
  236. is a function, not an
  237. old-fashioned initialization.
  238. */
  239. '('
  240. formal_list(&fm) ? /* semantic check later... */
  241. ')'
  242. {
  243. add_decl_unary(dc, FUNCTION, (arith)0, fm);
  244. fm = 0;
  245. }
  246. |
  247. arrayer(&count)
  248. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  249. ]*
  250. |
  251. '*' declarator(dc)
  252. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  253. ;
  254. primary_declarator(register struct declarator *dc;) :
  255. identifier(&dc->dc_idf)
  256. |
  257. '(' declarator(dc) ')'
  258. ;
  259. arrayer(arith *sizep;)
  260. { struct expr *expr; }
  261. :
  262. '['
  263. [
  264. constant_expression(&expr)
  265. {
  266. check_array_subscript(expr);
  267. *sizep = expr->VL_VALUE;
  268. free_expression(expr);
  269. }
  270. |
  271. empty
  272. { *sizep = (arith)-1; }
  273. ]
  274. ']'
  275. ;
  276. formal_list (struct formal **fmp;)
  277. :
  278. formal(fmp) [ ',' formal(fmp) ]*
  279. ;
  280. formal(struct formal **fmp;)
  281. {struct idf *idf; }
  282. :
  283. identifier(&idf)
  284. {
  285. register struct formal *new = new_formal();
  286. new->fm_idf = idf;
  287. new->next = *fmp;
  288. *fmp = new;
  289. }
  290. ;
  291. /* Change 2 */
  292. enum_specifier(register struct type **tpp;)
  293. {
  294. struct idf *idf;
  295. arith l = (arith)0;
  296. }
  297. :
  298. ENUM
  299. [
  300. {declare_struct(ENUM, (struct idf *) 0, tpp);}
  301. enumerator_pack(*tpp, &l)
  302. |
  303. identifier(&idf)
  304. [
  305. {declare_struct(ENUM, idf, tpp);}
  306. enumerator_pack(*tpp, &l)
  307. |
  308. {apply_struct(ENUM, idf, tpp);}
  309. empty
  310. ]
  311. ]
  312. ;
  313. enumerator_pack(register struct type *tp; arith *lp;) :
  314. '{'
  315. enumerator(tp, lp)
  316. [%while(AHEAD != '}') /* >>> conflict on ',' */
  317. ','
  318. enumerator(tp, lp)
  319. ]*
  320. ','? /* optional trailing comma */
  321. '}'
  322. {tp->tp_size = int_size;}
  323. /* fancy implementations that put small enums in 1 byte
  324. or so should start here.
  325. */
  326. ;
  327. enumerator(struct type *tp; arith *lp;)
  328. {
  329. struct idf *idf;
  330. struct expr *expr;
  331. }
  332. :
  333. identifier(&idf)
  334. [
  335. '='
  336. constant_expression(&expr)
  337. {
  338. *lp = expr->VL_VALUE;
  339. free_expression(expr);
  340. }
  341. ]?
  342. {declare_enum(tp, idf, (*lp)++);}
  343. ;
  344. /* 8.5 */
  345. struct_or_union_specifier(register struct type **tpp;)
  346. {
  347. int fund;
  348. struct idf *idfX;
  349. register struct idf *idf;
  350. }
  351. :
  352. [ STRUCT | UNION ]
  353. {fund = DOT;}
  354. [
  355. {
  356. declare_struct(fund, (struct idf *)0, tpp);
  357. }
  358. struct_declaration_pack(*tpp)
  359. |
  360. identifier(&idfX) { idf = idfX; }
  361. [
  362. {
  363. declare_struct(fund, idf, tpp);
  364. (idf->id_struct->tg_busy)++;
  365. }
  366. struct_declaration_pack(*tpp)
  367. {
  368. (idf->id_struct->tg_busy)--;
  369. }
  370. |
  371. {apply_struct(fund, idf, tpp);}
  372. empty
  373. ]
  374. ]
  375. ;
  376. struct_declaration_pack(register struct type *stp;)
  377. {
  378. struct sdef **sdefp = &stp->tp_sdef;
  379. arith size = (arith)0;
  380. }
  381. :
  382. /* The size is only filled in after the whole struct has
  383. been read, to prevent recursive definitions.
  384. */
  385. '{'
  386. struct_declaration(stp, &sdefp, &size)+
  387. '}'
  388. {stp->tp_size = align(size, stp->tp_align);}
  389. ;
  390. struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
  391. {struct type *tp;}
  392. :
  393. type_specifier(&tp)
  394. struct_declarator_list(tp, stp, sdefpp, szp)
  395. [ /* in some standard UNIX compilers the semicolon
  396. is optional, would you believe!
  397. */
  398. ';'
  399. |
  400. empty
  401. {warning("no semicolon after declarator");}
  402. ]
  403. ;
  404. struct_declarator_list(struct type *tp, *stp;
  405. struct sdef ***sdefpp; arith *szp;)
  406. :
  407. struct_declarator(tp, stp, sdefpp, szp)
  408. [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
  409. ;
  410. struct_declarator(struct type *tp; struct type *stp;
  411. struct sdef ***sdefpp; arith *szp;)
  412. {
  413. struct declarator Dc;
  414. struct field *fd = 0;
  415. }
  416. :
  417. {
  418. Dc = null_declarator;
  419. }
  420. [
  421. declarator(&Dc)
  422. {reject_params(&Dc);}
  423. bit_expression(&fd)?
  424. |
  425. {Dc.dc_idf = gen_idf();}
  426. bit_expression(&fd)
  427. ]
  428. {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
  429. {remove_declarator(&Dc);}
  430. ;
  431. bit_expression(struct field **fd;)
  432. { struct expr *expr; }
  433. :
  434. {
  435. *fd = new_field();
  436. }
  437. ':'
  438. constant_expression(&expr)
  439. {
  440. (*fd)->fd_width = expr->VL_VALUE;
  441. free_expression(expr);
  442. #ifdef NOBITFIELD
  443. error("bitfields are not implemented");
  444. #endif NOBITFIELD
  445. }
  446. ;
  447. /* 8.7 */
  448. cast(struct type **tpp;) {struct declarator Dc;} :
  449. {Dc = null_declarator;}
  450. '('
  451. type_specifier(tpp)
  452. abstract_declarator(&Dc)
  453. ')'
  454. {*tpp = declare_type(*tpp, &Dc);}
  455. {remove_declarator(&Dc);}
  456. ;
  457. /* This code is an abject copy of that of 'declarator', for lack of
  458. a two-level grammar.
  459. */
  460. abstract_declarator(register struct declarator *dc;)
  461. {arith count;}
  462. :
  463. primary_abstract_declarator(dc)
  464. [
  465. '(' ')'
  466. {add_decl_unary(dc, FUNCTION, (arith)0, NO_PARAMS);}
  467. |
  468. arrayer(&count)
  469. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  470. ]*
  471. |
  472. '*' abstract_declarator(dc)
  473. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  474. ;
  475. primary_abstract_declarator(struct declarator *dc;) :
  476. [%if (AHEAD == ')')
  477. empty
  478. |
  479. '(' abstract_declarator(dc) ')'
  480. ]
  481. ;
  482. empty:
  483. ;
  484. /* 8.8 */
  485. /* included in the IDENTIFIER/TYPE_IDENTIFIER mechanism */