PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/src/c-parse.y

https://github.com/ashwinraghav/Cqual
Happy | 1776 lines | 1591 code | 185 blank | 0 comment | 0 complexity | 0f39eb27db78aa557f833cee4c6ab941 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* This file was modified in 2000-2002 by David Gay, Jeff Foster, and
  2. Tachio Terauchi for cqual. The changes are Copyright (c) 2000-2002
  3. The Regents of the University of California.
  4. This file is distributed under the terms of the GNU General Public License
  5. (see below). */
  6. /* YACC parser for C syntax and for Objective C. -*-c-*-
  7. Copyright (C) 1987, 88, 89, 92-97, 1998 Free Software Foundation, Inc.
  8. This file is part of GNU CC.
  9. GNU CC is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING. If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA. */
  21. /* This file defines the grammar of C */
  22. /* To whomever it may concern: I have heard that such a thing was once
  23. written by AT&T, but I have never seen it. */
  24. %expect 50
  25. /* Added 4 conflicts from base system for USER_QUALIFIER */
  26. /* These are the 23 conflicts you should get in parse.output;
  27. the state numbers may vary if minor changes in the grammar are made.
  28. State 42 contains 1 shift/reduce conflict. (Two ways to parse ATTRIBUTE.)
  29. State 44 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  30. State 103 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  31. State 110 contains 1 shift/reduce conflict. (Two ways to parse ATTRIBUTE.)
  32. State 111 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  33. State 115 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  34. State 132 contains 1 shift/reduce conflict. (See comment at component_decl.)
  35. State 180 contains 1 shift/reduce conflict. (Two ways to parse ATTRIBUTE.)
  36. State 194 contains 2 shift/reduce conflict. (Four ways to parse this.)
  37. State 202 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  38. State 214 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  39. State 220 contains 1 shift/reduce conflict. (Two ways to recover from error.)
  40. State 304 contains 2 shift/reduce conflicts. (Four ways to parse this.)
  41. State 335 contains 2 shift/reduce conflicts. (Four ways to parse this.)
  42. State 347 contains 1 shift/reduce conflict. (Two ways to parse ATTRIBUTES.)
  43. State 352 contains 1 shift/reduce conflict. (Two ways to parse ATTRIBUTES.)
  44. State 383 contains 2 shift/reduce conflicts. (Four ways to parse this.)
  45. State 434 contains 2 shift/reduce conflicts. (Four ways to parse this.) */
  46. %{
  47. #include <stdio.h>
  48. #include <errno.h>
  49. #include <setjmp.h>
  50. #include "parser.h"
  51. #include "c-parse.h"
  52. #include "c-lex.h"
  53. #include "semantics.h"
  54. #include "input.h"
  55. #include "expr.h"
  56. #include "stmt.h"
  57. int yyparse(void) deletes;
  58. void yyerror();
  59. /* Like YYERROR but do call yyerror. */
  60. #define YYERROR1 { yyerror ("syntax error"); YYERROR; }
  61. /* Cause the `yydebug' variable to be defined. */
  62. #define YYDEBUG 1
  63. %}
  64. %start program
  65. /* All identifiers that are not reserved words
  66. and are not declared typedefs in the current block */
  67. %token IDENTIFIER
  68. /* All identifiers that are declared typedefs in the current block.
  69. In some contexts, they are treated just like IDENTIFIER,
  70. but they can also serve as typespecs in declarations. */
  71. %token TYPENAME
  72. /* A user-defined type qualifier */
  73. %token USER_QUALIFIER
  74. /* A type variable */
  75. %token TYPE_VARIABLE
  76. /* Reserved words that specify storage class.
  77. yylval contains an IDENTIFIER_NODE which indicates which one. */
  78. %token <u.itoken> SCSPEC
  79. /* Reserved words that specify type.
  80. yylval contains an IDENTIFIER_NODE which indicates which one. */
  81. %token <u.itoken> TYPESPEC
  82. /* Reserved words that qualify types/functions: "const" or "volatile",
  83. "deletes".
  84. yylval contains an IDENTIFIER_NODE which indicates which one. */
  85. %token <u.itoken> TYPE_QUAL FN_QUAL
  86. /* Character or numeric constants.
  87. yylval is the node for the constant. */
  88. %token CONSTANT
  89. /* String constants in raw form.
  90. yylval is a STRING_CST node. */
  91. %token STRING MAGIC_STRING
  92. /* "...", used for functions with variable arglists. */
  93. %token <u.itoken> ELLIPSIS
  94. /* the reserved words */
  95. /* SCO include files test "ASM", so use something else. */
  96. %token <u.itoken> SIZEOF ENUM STRUCT UNION IF ELSE WHILE DO FOR SWITCH CASE DEFAULT
  97. %token <u.itoken> BREAK CONTINUE RETURN GOTO ASM_KEYWORD TYPEOF ALIGNOF
  98. %token <u.itoken> ATTRIBUTE EXTENSION LABEL
  99. %token <u.itoken> REALPART IMAGPART VA_ARG
  100. %token <u.itoken> ASSERT_TYPE CHANGE_TYPE DEEP_RESTRICT
  101. /* Add precedence rules to solve dangling else s/r conflict */
  102. %nonassoc IF
  103. %nonassoc ELSE
  104. /* Define the operator tokens and their precedences.
  105. The value is an integer because, if used, it is the tree code
  106. to use in the expression made from the operator. */
  107. %right <u.itoken> ASSIGN '='
  108. %right <u.itoken> '?' ':'
  109. %left <u.itoken> OROR
  110. %left <u.itoken> ANDAND
  111. %left <u.itoken> '|'
  112. %left <u.itoken> '^'
  113. %left <u.itoken> '&'
  114. %left <u.itoken> EQCOMPARE
  115. %left <u.itoken> ARITHCOMPARE
  116. %left <u.itoken> LSHIFT RSHIFT
  117. %left <u.itoken> '+' '-'
  118. %left <u.itoken> '*' '/' '%'
  119. %right <u.itoken> UNARY PLUSPLUS MINUSMINUS
  120. %left HYPERUNARY
  121. %left <u.itoken> POINTSAT '.' '(' '['
  122. %type <u.asm_operand> asm_operand asm_operands nonnull_asm_operands
  123. %type <u.asm_stmt> maybeasm
  124. %type <u.attribute> maybe_attribute attributes attribute attribute_list attrib
  125. %type <u.constant> CONSTANT
  126. %type <u.decl> datadecl datadecls datadef decl decls extdef extdefs fndef
  127. %type <u.decl> initdecls nested_function notype_nested_function notype_initdecls
  128. %type <u.decl> old_style_parm_decls initdcl
  129. %type <u.decl> component_decl_list component_decl_list2 component_decl
  130. %type <u.decl> components component_declarator enumerator enumlist
  131. %type <u.decl> parmlist parmlist_1 parmlist_2 parms parm ellipsis_decl
  132. %type <u.decl> parmlist_or_identifiers identifiers notype_initdcl
  133. %type <u.decl> parmlist_or_identifiers_1 old_parameter
  134. %type <u.declarator> declarator after_type_declarator notype_declarator
  135. %type <u.declarator> absdcl absdcl1 parm_declarator
  136. %type <u.expr> cast_expr expr expr_no_commas exprlist init initlist_maybe_comma initlist1 initelt nonnull_exprlist primary string_component STRING string_list
  137. %type <u.expr> unary_expr xexpr
  138. %type <u.id_label> id_label maybe_label_decls label_decls label_decl
  139. %type <u.id_label> identifiers_or_typenames
  140. %type <idtoken> identifier IDENTIFIER TYPENAME USER_QUALIFIER TYPE_VARIABLE MAGIC_STRING
  141. %type <u.iexpr> if_prefix
  142. %type <u.istmt> stmt_or_labels simple_if stmt_or_label
  143. %type <u.itoken> unop extension '~' '!' compstmt_start '{' ';'
  144. %type <u.itoken> sizeof alignof
  145. %type <u.label> label
  146. %type <u.stmt> stmts xstmts compstmt_or_error compstmt
  147. %type <u.stmt> labeled_stmt stmt
  148. %type <u.cstmt> do_stmt_start
  149. %type <u.string> asm_clobbers string
  150. %type <u.telement> scspec type_qual type_spec
  151. %type <u.telement> declmods declmods_no_prefix_attr
  152. %type <u.telement> reserved_declspecs reserved_declspecs_no_prefix_attr
  153. %type <u.telement> typed_declspecs typed_declspecs_no_prefix_attr
  154. %type <u.telement> typed_typespecs reserved_typespecquals
  155. %type <u.telement> typespec typespecqual_reserved structsp
  156. %type <u.telement> nonempty_type_quals type_quals
  157. %type <u.telement> maybe_type_qual fn_qual fn_quals
  158. %type <u.type> typename
  159. %type <u.word> idword any_word tag
  160. %{
  161. /* Region in which to allocate parse structures. Idea: the AST user can set
  162. this to different regions at appropriate junctures depending on what's
  163. being done with the AST */
  164. region parse_region;
  165. /* We'll see this a LOT below */
  166. #define pr parse_region
  167. /* Number of statements (loosely speaking) and compound statements
  168. seen so far. */
  169. static int stmt_count;
  170. static int compstmt_count;
  171. /* List of types and structure classes of the current declaration. */
  172. static type_element current_declspecs = NULL;
  173. static attribute prefix_attributes = NULL;
  174. /* >0 if currently parsing an expression that will not be evaluated (argument
  175. to alignof, sizeof. Currently not typeof though that could be considered
  176. a bug) */
  177. int unevaluated_expression;
  178. #ifdef RC_ADJUST
  179. static size_t rc_adjust_yystype(void *x, int by)
  180. {
  181. struct yystype *p = x;
  182. RC_ADJUST_PREAMBLE;
  183. RC_ADJUST(p->u.ptr, by);
  184. RC_ADJUST(p->idtoken.location.filename, by);
  185. RC_ADJUST(p->idtoken.id.data, by);
  186. RC_ADJUST(p->idtoken.decl, by);
  187. return sizeof *p;
  188. }
  189. static void rc_update_yystype(struct yystype *old, struct yystype *new)
  190. {
  191. regionid base = regionidof(old);
  192. RC_UPDATE(base, old->u.ptr, new->u.ptr);
  193. RC_UPDATE(base, old->idtoken.location.filename, new->idtoken.location.filename);
  194. RC_UPDATE(base, old->idtoken.id.data, new->idtoken.id.data);
  195. RC_UPDATE(base, old->idtoken.decl, new->idtoken.decl);
  196. }
  197. #endif
  198. /* A stack of declspecs and attributes for use during parsing */
  199. typedef struct spec_stack *spec_stack;
  200. struct spec_stack {
  201. type_element parentptr declspecs;
  202. attribute parentptr attributes;
  203. spec_stack sameregion next;
  204. };
  205. /* Stack of saved values of current_declspecs and prefix_attributes. */
  206. /* In an ideal world, we would be able to eliminate most rc ops for
  207. declspec_stack and ds_region assignments. Seems tricky though. */
  208. static spec_stack declspec_stack;
  209. static region ds_region;
  210. /* Pop top entry of declspec_stack back into current_declspecs,
  211. prefix_attributes */
  212. static void pop_declspec_stack(void) deletes
  213. {
  214. current_declspecs = declspec_stack->declspecs;
  215. prefix_attributes = declspec_stack->attributes;
  216. declspec_stack = declspec_stack->next;
  217. if (!declspec_stack)
  218. deleteregion_ptr(&ds_region);
  219. }
  220. static void push_declspec_stack(void)
  221. {
  222. spec_stack news;
  223. if (!ds_region) ds_region = newsubregion(parse_region);
  224. news = ralloc(ds_region, struct spec_stack);
  225. news->declspecs = current_declspecs;
  226. news->attributes = prefix_attributes;
  227. news->next = declspec_stack;
  228. declspec_stack = news;
  229. }
  230. /* Tell yyparse how to print a token's value, if yydebug is set. */
  231. #define YYPRINT(FILE,YYCHAR,YYLVAL) yyprint(FILE,YYCHAR,YYLVAL)
  232. void yyprint();
  233. %}
  234. %%
  235. program: /* empty */
  236. { if (pedantic)
  237. pedwarn("ANSI C forbids an empty source file");
  238. the_program = NULL;
  239. }
  240. | extdefs
  241. {
  242. the_program = CAST(declaration, ast_reverse(CAST(node, $1)));
  243. }
  244. ;
  245. /* the reason for the strange actions in this rule
  246. is so that notype_initdecls when reached via datadef
  247. can find a valid list of type and sc specs in $0. */
  248. extdefs:
  249. { $<u.telement>$ = NULL; } extdef { $$ = $2; }
  250. | extdefs { $<u.telement>$ = NULL; } extdef
  251. { $$ = declaration_chain($3, $1); }
  252. ;
  253. extdef:
  254. fndef
  255. | datadef
  256. | ASM_KEYWORD '(' expr ')' ';'
  257. {
  258. $$ = CAST(declaration, new_asm_decl
  259. (pr, $1.location,
  260. new_asm_stmt(pr, $1.location, $3, NULL, NULL, NULL, NULL))); }
  261. | extension extdef
  262. { pedantic = $1.i;
  263. $$ = CAST(declaration, new_extension_decl(pr, $1.location, $2)); }
  264. ;
  265. datadef:
  266. setspecs notype_initdecls ';'
  267. { if (pedantic)
  268. error("ANSI C forbids data definition with no type or storage class");
  269. else if (!flag_traditional)
  270. warning("data definition has no type or storage class");
  271. $$ = CAST(declaration, new_data_decl(pr, $2->loc, NULL, NULL, $2));
  272. pop_declspec_stack(); }
  273. | declmods setspecs notype_initdecls ';'
  274. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  275. pop_declspec_stack(); }
  276. | typed_declspecs setspecs initdecls ';'
  277. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  278. pop_declspec_stack(); }
  279. | declmods ';'
  280. { pedwarn("empty declaration"); }
  281. | typed_declspecs setspecs ';'
  282. { shadow_tag($1);
  283. $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, NULL));
  284. pop_declspec_stack(); }
  285. | error ';' { $$ = new_error_decl(pr, last_location); }
  286. | error '}' { $$ = new_error_decl(pr, last_location); }
  287. | ';'
  288. { if (pedantic)
  289. pedwarn("ANSI C does not allow extra `;' outside of a function");
  290. $$ = NULL; }
  291. ;
  292. fndef:
  293. typed_declspecs setspecs declarator
  294. { if (!start_function(current_declspecs, $3,
  295. prefix_attributes, 0))
  296. YYERROR1; }
  297. old_style_parm_decls
  298. { store_parm_decls($5); }
  299. compstmt_or_error
  300. { $$ = finish_function($7);
  301. pop_declspec_stack(); }
  302. | typed_declspecs setspecs declarator error
  303. { $$ = new_error_decl(pr, last_location);
  304. pop_declspec_stack(); }
  305. | declmods setspecs notype_declarator
  306. { if (!start_function(current_declspecs, $3,
  307. prefix_attributes, 0))
  308. YYERROR1; }
  309. old_style_parm_decls
  310. { store_parm_decls($5); }
  311. compstmt_or_error
  312. { $$ = finish_function($7);
  313. pop_declspec_stack(); }
  314. | declmods setspecs notype_declarator error
  315. { $$ = new_error_decl(pr, last_location);
  316. pop_declspec_stack(); }
  317. | setspecs notype_declarator
  318. { if (!start_function(NULL, $2,
  319. prefix_attributes, 0))
  320. YYERROR1; }
  321. old_style_parm_decls
  322. { store_parm_decls($4); }
  323. compstmt_or_error
  324. { $$ = finish_function($6);
  325. pop_declspec_stack(); }
  326. | setspecs notype_declarator error
  327. { $$ = new_error_decl(pr, last_location);
  328. pop_declspec_stack(); }
  329. ;
  330. identifier:
  331. IDENTIFIER
  332. | TYPENAME
  333. ;
  334. id_label:
  335. identifier { $$ = new_id_label(pr, $1.location, $1.id); }
  336. ;
  337. idword:
  338. identifier { $$ = new_word(pr, $1.location, $1.id); }
  339. unop: '&'
  340. { $$ = $1; $$.i = kind_address_of; }
  341. | '-'
  342. { $$ = $1; $$.i = kind_unary_minus; }
  343. | '+'
  344. { $$ = $1; $$.i = kind_unary_plus; }
  345. | PLUSPLUS
  346. { $$ = $1; $$.i = kind_preincrement; }
  347. | MINUSMINUS
  348. { $$ = $1; $$.i = kind_predecrement; }
  349. | '~'
  350. { $$ = $1; $$.i = kind_bitnot; }
  351. | '!'
  352. { $$ = $1; $$.i = kind_not; }
  353. | REALPART
  354. { $$ = $1; $$.i = kind_realpart; }
  355. | IMAGPART
  356. { $$ = $1; $$.i = kind_imagpart; }
  357. ;
  358. expr: nonnull_exprlist
  359. { if ($1->next)
  360. $$ = make_comma($1->loc, $1);
  361. else
  362. $$ = $1; }
  363. ;
  364. exprlist:
  365. /* empty */
  366. { $$ = NULL; }
  367. | nonnull_exprlist
  368. ;
  369. nonnull_exprlist:
  370. expr_no_commas
  371. { $$ = $1; }
  372. | nonnull_exprlist ',' expr_no_commas
  373. { $$ = expression_chain($1, $3); }
  374. ;
  375. unary_expr:
  376. primary
  377. | '*' cast_expr %prec UNARY
  378. { $$ = make_dereference($1.location, $2); }
  379. /* __extension__ turns off -pedantic for following primary. */
  380. | extension cast_expr %prec UNARY
  381. { $$ = make_extension_expr($1.location, $2);
  382. pedantic = $1.i; }
  383. | unop cast_expr %prec UNARY
  384. { $$ = make_unary($1.location, $1.i, $2);
  385. #if 0
  386. overflow_warning($$);
  387. #endif
  388. }
  389. /* Refer to the address of a label as a pointer. */
  390. | ANDAND id_label
  391. {
  392. $$ = CAST(expression, make_label_address($1.location, $2));
  393. use_label($2);
  394. }
  395. | sizeof unary_expr %prec UNARY
  396. {
  397. #if 0
  398. if (TREE_CODE ($2) == COMPONENT_REF
  399. && DECL_C_BIT_FIELD (TREE_OPERAND ($2, 1)))
  400. error("`sizeof' applied to a bit-field");
  401. $$ = c_sizeof (TREE_TYPE ($2));
  402. #endif
  403. $$ = make_sizeof_expr($1.location, $2);
  404. unevaluated_expression--; }
  405. | sizeof '(' typename ')' %prec HYPERUNARY
  406. { $$ = make_sizeof_type($1.location, $3);
  407. unevaluated_expression--; }
  408. | alignof unary_expr %prec UNARY
  409. { $$ = make_alignof_expr($1.location, $2);
  410. unevaluated_expression--; }
  411. | alignof '(' typename ')' %prec HYPERUNARY
  412. { $$ = make_alignof_type($1.location, $3);
  413. unevaluated_expression--; }
  414. ;
  415. sizeof:
  416. SIZEOF { unevaluated_expression++; $$ = $1; }
  417. ;
  418. alignof:
  419. ALIGNOF { unevaluated_expression++; $$ = $1; }
  420. ;
  421. cast_expr:
  422. unary_expr
  423. | '(' typename ')' cast_expr %prec UNARY
  424. { $$ = make_cast($1.location, $2, $4); }
  425. | '(' typename ')' '{'
  426. {
  427. #if 0
  428. start_init (NULL, NULL, 0);
  429. $2 = groktypename ($2);
  430. really_start_incremental_init ($2);
  431. #endif
  432. }
  433. initlist_maybe_comma '}' %prec UNARY
  434. {
  435. $$ = CAST(expression, new_cast_list(pr, $1.location, $2, CAST(expression, new_init_list(pr, $6->loc, $6))));
  436. $$->type = $2->type;
  437. $$->lvalue = TRUE;
  438. /* XXX: Evil hack for foo((int[5]) {1, 2, 3}) */
  439. /* XXX: what does gcc do ? */
  440. /* XXX: why on earth does gcc consider this an lvalue ?
  441. (see cparser/tests/addr1.c) */
  442. if (type_array($$->type))
  443. $$->lvalue = TRUE;
  444. if (pedantic)
  445. pedwarn("ANSI C forbids constructor expressions");
  446. #if 0
  447. char *name;
  448. tree result = pop_init_level (0);
  449. tree type = $2;
  450. finish_init ();
  451. if (TYPE_NAME (type) != 0)
  452. {
  453. if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
  454. name = IDENTIFIER_POINTER (TYPE_NAME (type));
  455. else
  456. name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
  457. }
  458. else
  459. name = "";
  460. $$ = result;
  461. if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
  462. {
  463. int failure = complete_array_type (type, $$, 1);
  464. if (failure)
  465. abort ();
  466. }
  467. #endif
  468. }
  469. ;
  470. expr_no_commas:
  471. cast_expr
  472. | expr_no_commas '+' expr_no_commas
  473. { $$ = make_binary($2.location, kind_plus, $1, $3); }
  474. | expr_no_commas '-' expr_no_commas
  475. { $$ = make_binary($2.location, kind_minus, $1, $3); }
  476. | expr_no_commas '*' expr_no_commas
  477. { $$ = make_binary($2.location, kind_times, $1, $3); }
  478. | expr_no_commas '/' expr_no_commas
  479. { $$ = make_binary($2.location, kind_divide, $1, $3); }
  480. | expr_no_commas '%' expr_no_commas
  481. { $$ = make_binary($2.location, kind_modulo, $1, $3); }
  482. | expr_no_commas LSHIFT expr_no_commas
  483. { $$ = make_binary($2.location, kind_lshift, $1, $3); }
  484. | expr_no_commas RSHIFT expr_no_commas
  485. { $$ = make_binary($2.location, kind_rshift, $1, $3); }
  486. | expr_no_commas ARITHCOMPARE expr_no_commas
  487. { $$ = make_binary($2.location, $2.i, $1, $3); }
  488. | expr_no_commas EQCOMPARE expr_no_commas
  489. { $$ = make_binary($2.location, $2.i, $1, $3); }
  490. | expr_no_commas '&' expr_no_commas
  491. { $$ = make_binary($2.location, kind_bitand, $1, $3); }
  492. | expr_no_commas '|' expr_no_commas
  493. { $$ = make_binary($2.location, kind_bitor, $1, $3); }
  494. | expr_no_commas '^' expr_no_commas
  495. { $$ = make_binary($2.location, kind_bitxor, $1, $3); }
  496. | expr_no_commas ANDAND expr_no_commas
  497. { $$ = make_binary($2.location, kind_andand, $1, $3); }
  498. | expr_no_commas OROR expr_no_commas
  499. { $$ = make_binary($2.location, kind_oror, $1, $3); }
  500. | expr_no_commas '?' expr ':' expr_no_commas
  501. { $$ = make_conditional($2.location, $1, $3, $5); }
  502. | expr_no_commas '?'
  503. { if (pedantic)
  504. pedwarn("ANSI C forbids omitting the middle term of a ?: expression");
  505. }
  506. ':' expr_no_commas
  507. { $$ = make_conditional($2.location, $1, NULL, $5); }
  508. | expr_no_commas '=' expr_no_commas
  509. { $$ = make_assign($2.location, kind_assign, $1, $3); }
  510. | expr_no_commas ASSIGN expr_no_commas
  511. { $$ = make_assign($2.location, $2.i, $1, $3); }
  512. ;
  513. primary:
  514. IDENTIFIER
  515. {
  516. if (yychar == YYEMPTY)
  517. yychar = YYLEX;
  518. $$ = make_identifier($1.location, $1.id, yychar == '(');
  519. }
  520. | CONSTANT { $$ = CAST(expression, $1); }
  521. | string { $$ = CAST(expression, $1); }
  522. | '(' expr ')'
  523. { $$ = $2; }
  524. | '(' error ')'
  525. { $$ = make_error_expr(last_location); }
  526. | '('
  527. { if (current_function_decl == 0)
  528. {
  529. error("braced-group within expression allowed only inside a function");
  530. YYERROR;
  531. }
  532. push_label_level();
  533. }
  534. compstmt ')'
  535. {
  536. pop_label_level();
  537. if (pedantic)
  538. pedwarn("ANSI C forbids braced-groups within expressions");
  539. $$ = make_compound_expr($1.location, $3);
  540. }
  541. | primary '(' exprlist ')' %prec '.'
  542. { $$ = make_function_call($2.location, $1, $3); }
  543. | VA_ARG '(' expr_no_commas ',' typename ')'
  544. { $$ = make_va_arg($1.location, $3, $5); }
  545. | primary '[' expr ']' %prec '.'
  546. { $$ = make_array_ref($2.location, $1, $3); }
  547. | primary '.' identifier
  548. { $$ = make_field_ref($2.location, $1, $3.id, $3.location); }
  549. | primary POINTSAT identifier
  550. { $$ = make_field_ref($2.location, make_dereference($2.location, $1),
  551. $3.id, $3.location); }
  552. | primary PLUSPLUS
  553. { $$ = make_postincrement($2.location, $1); }
  554. | primary MINUSMINUS
  555. { $$ = make_postdecrement($2.location, $1); }
  556. ;
  557. /* Produces a STRING_CST with perhaps more STRING_CSTs chained onto it. */
  558. string:
  559. string_list { $$ = make_string($1->loc, $1); }
  560. string_list:
  561. string_component { $$ = $1; }
  562. | string_list string_component
  563. { $$ = expression_chain($1, $2); }
  564. ;
  565. string_component:
  566. STRING { $$ = CAST(expression, $1); }
  567. | MAGIC_STRING
  568. { $$ = make_identifier($1.location, $1.id, FALSE);
  569. }
  570. ;
  571. old_style_parm_decls:
  572. /* empty */ { $$ = NULL; }
  573. | datadecls
  574. | datadecls ellipsis_decl
  575. /* ... is used here to indicate a varargs function. */
  576. { if (pedantic)
  577. pedwarn("ANSI C does not permit use of `varargs.h'");
  578. $$ = declaration_chain($1, $2);
  579. }
  580. ;
  581. ellipsis_decl:
  582. ELLIPSIS
  583. { $$ = CAST(declaration, new_ellipsis_decl(pr, $1.location, NULL)); }
  584. | type_qual ELLIPSIS
  585. { $$ = CAST(declaration, new_ellipsis_decl(pr, $2.location, $1)); }
  586. | ELLIPSIS type_qual
  587. { $$ = CAST(declaration, new_ellipsis_decl(pr, $1.location, $2)); }
  588. | type_qual ELLIPSIS type_qual
  589. { $$ = CAST(declaration,
  590. new_ellipsis_decl(pr, $2.location,
  591. type_element_chain($1, $3))); }
  592. /* The following are analogous to decls and decl
  593. except that they do not allow nested functions.
  594. They are used for old-style parm decls. */
  595. datadecls:
  596. datadecl
  597. | errstmt { $$ = new_error_decl(pr, last_location); }
  598. | datadecls datadecl { $$ = declaration_chain($1, $2); }
  599. | datadecl errstmt { $$ = new_error_decl(pr, last_location); }
  600. ;
  601. /* We don't allow prefix attributes here because they cause reduce/reduce
  602. conflicts: we can't know whether we're parsing a function decl with
  603. attribute suffix, or function defn with attribute prefix on first old
  604. style parm. */
  605. datadecl:
  606. typed_declspecs_no_prefix_attr setspecs initdecls ';'
  607. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  608. pop_declspec_stack(); }
  609. | declmods_no_prefix_attr setspecs notype_initdecls ';'
  610. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  611. pop_declspec_stack(); }
  612. | typed_declspecs_no_prefix_attr setspecs ';'
  613. { shadow_tag_warned($1, 1);
  614. $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, NULL));
  615. pop_declspec_stack();
  616. pedwarn("empty declaration"); }
  617. | declmods_no_prefix_attr ';'
  618. { pedwarn("empty declaration");
  619. $$ = NULL; }
  620. ;
  621. /* This combination which saves a lineno before a decl
  622. is the normal thing to use, rather than decl itself.
  623. This is to avoid shift/reduce conflicts in contexts
  624. where statement labels are allowed. */
  625. decls:
  626. decl
  627. | errstmt { $$ = new_error_decl(pr, last_location); }
  628. | decls decl { $$ = declaration_chain($1, $2); }
  629. | decl errstmt { $$ = new_error_decl(pr, last_location); }
  630. ;
  631. /* records the type and storage class specs to use for processing
  632. the declarators that follow.
  633. Maintains a stack of outer-level values of current_declspecs,
  634. for the sake of parm declarations nested in function declarators. */
  635. setspecs: /* empty */
  636. {
  637. push_declspec_stack();
  638. pending_xref_error();
  639. split_type_elements($<u.telement>0,
  640. &current_declspecs, &prefix_attributes);
  641. }
  642. ;
  643. /* ??? Yuck. See after_type_declarator. */
  644. setattrs: /* empty */
  645. { prefix_attributes = attribute_chain(prefix_attributes,
  646. $<u.attribute>0);
  647. /* This syntax is broken as it will apply to all remaining
  648. declarations, not just the current declarator
  649. (this is broken in base GCC too) */
  650. /* XXX: Used extensively in the linux kernel. YUCK. */
  651. /*error("Unsupported attribute syntax");*/ }
  652. ;
  653. decl:
  654. typed_declspecs setspecs initdecls ';'
  655. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  656. pop_declspec_stack(); }
  657. | declmods setspecs notype_initdecls ';'
  658. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  659. pop_declspec_stack(); }
  660. | typed_declspecs setspecs nested_function
  661. { $$ = $3;
  662. pop_declspec_stack(); }
  663. | declmods setspecs notype_nested_function
  664. { $$ = $3;
  665. pop_declspec_stack(); }
  666. | typed_declspecs setspecs ';'
  667. { shadow_tag($1);
  668. $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, NULL));
  669. pop_declspec_stack(); }
  670. | declmods ';'
  671. { pedwarn("empty declaration"); }
  672. | extension decl
  673. { pedantic = $1.i;
  674. $$ = CAST(declaration, new_extension_decl(pr, $1.location, $2)); }
  675. ;
  676. /* Declspecs which contain at least one type specifier or typedef name.
  677. (Just `const' or `volatile' is not enough.)
  678. A typedef'd name following these is taken as a name to be declared. */
  679. typed_declspecs:
  680. typespec reserved_declspecs
  681. { $$ = $1; $1->next = CAST(node, $2); }
  682. | declmods typespec reserved_declspecs
  683. { $$ = type_element_chain($1, $2); $2->next = CAST(node, $3); }
  684. ;
  685. reserved_declspecs: /* empty */
  686. { $$ = NULL; }
  687. | reserved_declspecs typespecqual_reserved
  688. { $$ = type_element_chain($1, $2); }
  689. | reserved_declspecs scspec
  690. { if (extra_warnings)
  691. warning("`%s' is not at beginning of declaration",
  692. rid_name(CAST(rid, $2)));
  693. $$ = type_element_chain($1, $2); }
  694. | reserved_declspecs attributes
  695. { $$ = type_element_chain($1, CAST(type_element, $2)); }
  696. ;
  697. typed_declspecs_no_prefix_attr:
  698. typespec reserved_declspecs_no_prefix_attr
  699. { $$ = $1; $1->next = CAST(node, $2); }
  700. | declmods_no_prefix_attr typespec reserved_declspecs_no_prefix_attr
  701. { $$ = type_element_chain($1, $2); $2->next = CAST(node, $3); }
  702. ;
  703. reserved_declspecs_no_prefix_attr:
  704. /* empty */
  705. { $$ = NULL; }
  706. | reserved_declspecs_no_prefix_attr typespecqual_reserved
  707. { $$ = type_element_chain($1, $2); }
  708. | reserved_declspecs_no_prefix_attr scspec
  709. { if (extra_warnings)
  710. warning("`%s' is not at beginning of declaration",
  711. rid_name(CAST(rid, $2)));
  712. $$ = type_element_chain($1, $2); }
  713. ;
  714. /* List of just storage classes, type modifiers, and prefix attributes.
  715. A declaration can start with just this, but then it cannot be used
  716. to redeclare a typedef-name.
  717. Declspecs have a non-NULL TREE_VALUE, attributes do not. */
  718. declmods:
  719. declmods_no_prefix_attr
  720. | attributes { $$ = CAST(type_element, $1); }
  721. | declmods declmods_no_prefix_attr
  722. { $$ = type_element_chain($1, $2); }
  723. | declmods attributes
  724. { $$ = type_element_chain($1, CAST(type_element, $2)); }
  725. ;
  726. declmods_no_prefix_attr:
  727. type_qual
  728. | scspec
  729. | declmods_no_prefix_attr type_qual
  730. { $$ = type_element_chain($1, $2); }
  731. | declmods_no_prefix_attr scspec
  732. { if (extra_warnings /*&& TREE_STATIC ($1)*/)
  733. warning("`%s' is not at beginning of declaration",
  734. rid_name(CAST(rid, $2)));
  735. $$ = type_element_chain($1, $2); }
  736. ;
  737. /* Used instead of declspecs where storage classes are not allowed
  738. (that is, for typenames and structure components).
  739. Don't accept a typedef-name if anything but a modifier precedes it. */
  740. typed_typespecs:
  741. typespec reserved_typespecquals
  742. { $$ = $1; $1->next = CAST(node, $2); }
  743. | nonempty_type_quals typespec reserved_typespecquals
  744. { $$ = type_element_chain($1, $2); $2->next = CAST(node, $3); }
  745. ;
  746. reserved_typespecquals: /* empty */
  747. { $$ = NULL; }
  748. | reserved_typespecquals typespecqual_reserved
  749. { $$ = type_element_chain($1, $2); }
  750. ;
  751. /* A typespec (but not a type qualifier).
  752. Once we have seen one of these in a declaration,
  753. if a typedef name appears then it is being redeclared. */
  754. typespec: type_spec
  755. | structsp
  756. | TYPENAME
  757. { /* For a typedef name, record the meaning, not the name.
  758. In case of `foo foo, bar;'. */
  759. $$ = CAST(type_element, new_typename(pr, $1.location, $1.decl)); }
  760. | TYPE_VARIABLE
  761. { $$ = CAST(type_element, new_type_variable(pr, $1.location, $1.id)); }
  762. | TYPEOF '(' expr ')'
  763. { $$ = CAST(type_element, new_typeof_expr(pr, $1.location, $3)); }
  764. | TYPEOF '(' typename ')'
  765. { $$ = CAST(type_element, new_typeof_type(pr, $1.location, $3)); }
  766. ;
  767. /* A typespec that is a reserved word, or a type qualifier. */
  768. typespecqual_reserved: type_spec
  769. | type_qual
  770. | structsp
  771. ;
  772. initdecls:
  773. initdcl
  774. | initdecls ',' initdcl { $$ = declaration_chain($1, $3); }
  775. ;
  776. notype_initdecls:
  777. notype_initdcl { $$ = $1; }
  778. | notype_initdecls ',' initdcl { $$ = declaration_chain($1, $3); }
  779. ;
  780. maybeasm:
  781. /* empty */
  782. { $$ = NULL; }
  783. | ASM_KEYWORD '(' string ')'
  784. { $$ = new_asm_stmt(pr, $1.location, CAST(expression, $3),
  785. NULL, NULL, NULL, NULL); }
  786. ;
  787. initdcl:
  788. declarator maybeasm maybe_attribute '='
  789. { $<u.decl>$ = start_decl($1, $2, current_declspecs, 1,
  790. $3, prefix_attributes); }
  791. init
  792. /* Note how the declaration of the variable is in effect while its init is parsed! */
  793. { $$ = finish_decl($<u.decl>5, $6); }
  794. | declarator maybeasm maybe_attribute
  795. { declaration d = start_decl($1, $2, current_declspecs, 0,
  796. $3, prefix_attributes);
  797. $$ = finish_decl(d, NULL); }
  798. ;
  799. notype_initdcl:
  800. notype_declarator maybeasm maybe_attribute '='
  801. { $<u.decl>$ = start_decl($1, $2, current_declspecs, 1,
  802. $3, prefix_attributes); }
  803. init
  804. /* Note how the declaration of the variable is in effect while its init is parsed! */
  805. { $$ = finish_decl($<u.decl>5, $6); }
  806. | notype_declarator maybeasm maybe_attribute
  807. { declaration d = start_decl($1, $2, current_declspecs, 0,
  808. $3, prefix_attributes);
  809. $$ = finish_decl(d, NULL); }
  810. ;
  811. maybe_attribute:
  812. /* empty */
  813. { $$ = NULL; }
  814. | attributes
  815. { $$ = $1; }
  816. ;
  817. attributes:
  818. attribute
  819. { $$ = $1; }
  820. | attributes attribute
  821. { $$ = attribute_chain($1, $2); }
  822. ;
  823. attribute:
  824. ATTRIBUTE '(' '(' attribute_list ')' ')'
  825. { $$ = $4; }
  826. ;
  827. attribute_list:
  828. attrib
  829. { $$ = $1; }
  830. | attribute_list ',' attrib
  831. { $$ = attribute_chain($1, $3); }
  832. ;
  833. attrib:
  834. /* empty */
  835. { $$ = NULL; }
  836. | any_word
  837. { $$ = new_attribute(pr, $1->loc, $1, NULL, NULL); }
  838. | any_word '(' IDENTIFIER ')'
  839. { $$ = new_attribute
  840. (pr, $1->loc, $1, new_word(pr, $3.location, $3.id), NULL); }
  841. | any_word '(' IDENTIFIER ',' nonnull_exprlist ')'
  842. { $$ = new_attribute
  843. (pr, $2.location, $1, new_word(pr, $3.location, $3.id), $5);
  844. }
  845. | any_word '(' exprlist ')'
  846. { $$ = new_attribute(pr, $2.location, $1, NULL, $3); }
  847. ;
  848. /* This still leaves out most reserved keywords,
  849. shouldn't we include them? */
  850. any_word:
  851. idword
  852. | scspec { $$ = new_word(pr, $1->loc, str2cstring(pr, rid_name(CAST(rid, $1)))); }
  853. | type_spec { $$ = new_word(pr, $1->loc, str2cstring(pr, rid_name(CAST(rid, $1)))); }
  854. | type_qual { $$ = new_word(pr, $1->loc, str2cstring(pr, qualifier_name(CAST(qualifier, $1)->id))); }
  855. ;
  856. /* Initializers. `init' is the entry point. */
  857. init:
  858. expr_no_commas
  859. | '{'
  860. initlist_maybe_comma '}'
  861. { $$ = CAST(expression, new_init_list(pr, $1.location, $2));
  862. $$->type = error_type; }
  863. | error
  864. { $$ = make_error_expr(last_location); }
  865. ;
  866. /* `initlist_maybe_comma' is the guts of an initializer in braces. */
  867. initlist_maybe_comma:
  868. /* empty */
  869. { if (pedantic)
  870. pedwarn("ANSI C forbids empty initializer braces");
  871. $$ = NULL; }
  872. | initlist1 maybecomma { $$ = CAST(expression, ast_reverse(CAST(node, $1))); }
  873. ;
  874. initlist1:
  875. initelt
  876. | initlist1 ',' initelt { $$ = expression_chain($3, $1); }
  877. ;
  878. /* `initelt' is a single element of an initializer.
  879. It may use braces. */
  880. initelt:
  881. expr_no_commas { $$ = $1; }
  882. | '{' initlist_maybe_comma '}'
  883. { $$ = CAST(expression, new_init_list(pr, $1.location, $2)); }
  884. | error { $$ = make_error_expr(last_location); }
  885. /* These are for labeled elements. The syntax for an array element
  886. initializer conflicts with the syntax for an Objective-C message,
  887. so don't include these productions in the Objective-C grammar. */
  888. | '[' expr_no_commas ELLIPSIS expr_no_commas ']' '=' initelt
  889. { $$ = CAST(expression, new_init_index(pr, $1.location, $2, $4, $7)); }
  890. | '[' expr_no_commas ']' '=' initelt
  891. { $$ = CAST(expression, new_init_index(pr, $1.location, $2, NULL, $5)); }
  892. | '[' expr_no_commas ']' initelt
  893. { $$ = CAST(expression, new_init_index(pr, $1.location, $2, NULL, $4)); }
  894. | idword ':' initelt
  895. { $$ = CAST(expression, new_init_field(pr, $1->loc, $1, $3)); }
  896. | '.' idword '=' initelt
  897. { $$ = CAST(expression, new_init_field(pr, $1.location, $2, $4)); }
  898. ;
  899. nested_function:
  900. declarator
  901. { if (!start_function(current_declspecs, $1,
  902. prefix_attributes, 1))
  903. {
  904. YYERROR1;
  905. }
  906. }
  907. old_style_parm_decls
  908. { store_parm_decls($3); }
  909. /* This used to use compstmt_or_error.
  910. That caused a bug with input `f(g) int g {}',
  911. where the use of YYERROR1 above caused an error
  912. which then was handled by compstmt_or_error.
  913. There followed a repeated execution of that same rule,
  914. which called YYERROR1 again, and so on. */
  915. compstmt
  916. { $$ = finish_function($5); }
  917. ;
  918. notype_nested_function:
  919. notype_declarator
  920. { if (!start_function(current_declspecs, $1,
  921. prefix_attributes, 1))
  922. {
  923. YYERROR1;
  924. }
  925. }
  926. old_style_parm_decls
  927. { store_parm_decls($3); }
  928. /* This used to use compstmt_or_error.
  929. That caused a bug with input `f(g) int g {}',
  930. where the use of YYERROR1 above caused an error
  931. which then was handled by compstmt_or_error.
  932. There followed a repeated execution of that same rule,
  933. which called YYERROR1 again, and so on. */
  934. compstmt
  935. { $$ = finish_function($5); }
  936. ;
  937. /* Any kind of declarator (thus, all declarators allowed
  938. after an explicit typespec). */
  939. declarator:
  940. after_type_declarator
  941. | notype_declarator
  942. ;
  943. /* A declarator that is allowed only after an explicit typespec. */
  944. after_type_declarator:
  945. '(' after_type_declarator ')'
  946. { $$ = $2; }
  947. | after_type_declarator '(' parmlist_or_identifiers fn_quals %prec '.'
  948. { $$ = make_function_declarator($2.location, $1, $3, $4); }
  949. | after_type_declarator '[' expr ']' %prec '.'
  950. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, $3)); }
  951. | after_type_declarator '[' ']' %prec '.'
  952. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, NULL)); }
  953. | '*' type_quals after_type_declarator %prec UNARY
  954. { $$ = CAST(declarator, new_pointer_declarator(pr, $1.location, $3, $2)); }
  955. /* ??? Yuck. setattrs is a quick hack. We can't use
  956. prefix_attributes because $1 only applies to this
  957. declarator. We assume setspecs has already been done.
  958. setattrs also avoids 5 reduce/reduce conflicts (otherwise multiple
  959. attributes could be recognized here or in `attributes'). */
  960. | attributes setattrs after_type_declarator
  961. { $$ = $3; }
  962. | TYPENAME { $$ = CAST(declarator, new_identifier_declarator(pr, $1.location, $1.id)); }
  963. ;
  964. /* Kinds of declarator that can appear in a parameter list
  965. in addition to notype_declarator. This is like after_type_declarator
  966. but does not allow a typedef name in parentheses as an identifier
  967. (because it would conflict with a function with that typedef as arg). */
  968. parm_declarator:
  969. parm_declarator '(' parmlist_or_identifiers fn_quals %prec '.'
  970. { $$ = make_function_declarator($2.location, $1, $3, $4); }
  971. | parm_declarator '[' expr ']' %prec '.'
  972. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, $3)); }
  973. | parm_declarator '[' ']' %prec '.'
  974. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, NULL)); }
  975. | '*' type_quals parm_declarator %prec UNARY
  976. { $$ = CAST(declarator, new_pointer_declarator(pr, $1.location, $3, $2)); }
  977. /* ??? Yuck. setattrs is a quick hack. We can't use
  978. prefix_attributes because $1 only applies to this
  979. declarator. We assume setspecs has already been done.
  980. setattrs also avoids 5 reduce/reduce conflicts (otherwise multiple
  981. attributes could be recognized here or in `attributes'). */
  982. | attributes setattrs parm_declarator
  983. { $$ = $3; }
  984. | TYPENAME { $$ = CAST(declarator, new_identifier_declarator(pr, $1.location, $1.id)); }
  985. ;
  986. /* A declarator allowed whether or not there has been
  987. an explicit typespec. These cannot redeclare a typedef-name. */
  988. notype_declarator:
  989. notype_declarator '(' parmlist_or_identifiers fn_quals %prec '.'
  990. { $$ = make_function_declarator($2.location, $1, $3, $4); }
  991. | '(' notype_declarator ')'
  992. { $$ = $2; }
  993. | '*' type_quals notype_declarator %prec UNARY
  994. { $$ = CAST(declarator, new_pointer_declarator(pr, $1.location, $3, $2)); }
  995. | notype_declarator '[' expr ']' %prec '.'
  996. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, $3)); }
  997. | notype_declarator '[' ']' %prec '.'
  998. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, NULL)); }
  999. /* ??? Yuck. setattrs is a quick hack. We can't use
  1000. prefix_attributes because $1 only applies to this
  1001. declarator. We assume setspecs has already been done.
  1002. setattrs also avoids 5 reduce/reduce conflicts (otherwise multiple
  1003. attributes could be recognized here or in `attributes'). */
  1004. | attributes setattrs notype_declarator
  1005. { $$ = $3; }
  1006. | IDENTIFIER { $$ = CAST(declarator, new_identifier_declarator(pr, $1.location, $1.id)); }
  1007. ;
  1008. tag:
  1009. identifier { $$ = new_word(pr, $1.location, $1.id); }
  1010. ;
  1011. structsp:
  1012. STRUCT tag '{'
  1013. { $$ = start_struct($1.location, kind_struct_ref, $2);
  1014. /* Start scope of tag before parsing components. */
  1015. }
  1016. component_decl_list '}' maybe_attribute
  1017. { $$ = finish_struct($<u.telement>4, $5, $7); }
  1018. | STRUCT '{' component_decl_list '}' maybe_attribute
  1019. { $$ = finish_struct(start_struct($1.location, kind_struct_ref, NULL),
  1020. $3, $5);
  1021. }
  1022. | STRUCT tag
  1023. { $$ = xref_tag($1.location, kind_struct_ref, $2); }
  1024. | UNION tag '{'
  1025. { $$ = start_struct ($1.location, kind_union_ref, $2); }
  1026. component_decl_list '}' maybe_attribute
  1027. { $$ = finish_struct($<u.telement>4, $5, $7); }
  1028. | UNION '{' component_decl_list '}' maybe_attribute
  1029. { $$ = finish_struct(start_struct($1.location, kind_union_ref, NULL),
  1030. $3, $5);
  1031. }
  1032. | UNION tag
  1033. { $$ = xref_tag($1.location, kind_union_ref, $2); }
  1034. | ENUM tag '{'
  1035. { $$ = start_enum($1.location, $2); }
  1036. enumlist maybecomma_warn '}' maybe_attribute
  1037. { $$ = finish_enum($<u.telement>4, $5, $8); }
  1038. | ENUM '{'
  1039. { $$ = start_enum($1.location, NULL); }
  1040. enumlist maybecomma_warn '}' maybe_attribute
  1041. { $$ = finish_enum($<u.telement>3, $4, $7); }
  1042. | ENUM tag
  1043. { $$ = xref_tag($1.location, kind_enum_ref, $2); }
  1044. ;
  1045. maybecomma:
  1046. /* empty */
  1047. | ','
  1048. ;
  1049. maybecomma_warn:
  1050. /* empty */
  1051. | ','
  1052. { if (pedantic) pedwarn("comma at end of enumerator list"); }
  1053. ;
  1054. component_decl_list:
  1055. component_decl_list2
  1056. { $$ = $1; }
  1057. | component_decl_list2 component_decl
  1058. { $$ = declaration_chain($1, $2);
  1059. pedwarn("no semicolon at end of struct or union"); }
  1060. ;
  1061. component_decl_list2: /* empty */
  1062. { $$ = NULL; }
  1063. | component_decl_list2 component_decl ';'
  1064. { $$ = declaration_chain($1, $2); }
  1065. | component_decl_list2 ';'
  1066. { if (pedantic)
  1067. pedwarn("extra semicolon in struct or union specified");
  1068. $$ = $1; }
  1069. ;
  1070. /* There is a shift-reduce conflict here, because `components' may
  1071. start with a `typename'. It happens that shifting (the default resolution)
  1072. does the right thing, because it treats the `typename' as part of
  1073. a `typed_typespecs'.
  1074. It is possible that this same technique would allow the distinction
  1075. between `notype_initdecls' and `initdecls' to be eliminated.
  1076. But I am being cautious and not trying it. */
  1077. component_decl:
  1078. typed_typespecs setspecs components
  1079. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  1080. pop_declspec_stack(); }
  1081. | typed_typespecs setspecs
  1082. { if (pedantic)
  1083. pedwarn("ANSI C forbids member declarations with no members");
  1084. shadow_tag($1);
  1085. $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, NULL));
  1086. pop_declspec_stack(); }
  1087. | nonempty_type_quals setspecs components
  1088. { $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, $3));
  1089. pop_declspec_stack(); }
  1090. | nonempty_type_quals setspecs
  1091. { if (pedantic)
  1092. pedwarn("ANSI C forbids member declarations with no members");
  1093. shadow_tag($1);
  1094. $$ = CAST(declaration, new_data_decl(pr, $1->loc, current_declspecs, prefix_attributes, NULL));
  1095. pop_declspec_stack(); }
  1096. | error
  1097. { $$ = new_error_decl(pr, last_location); }
  1098. | extension component_decl
  1099. { pedantic = $<u.itoken>1.i;
  1100. $$ = CAST(declaration, new_extension_decl(pr, $1.location, $2)); }
  1101. ;
  1102. components:
  1103. component_declarator
  1104. | components ',' component_declarator
  1105. { $$ = declaration_chain($1, $3); }
  1106. ;
  1107. component_declarator:
  1108. declarator maybe_attribute
  1109. { $$ = make_field($1, NULL, current_declspecs,
  1110. $2, prefix_attributes); }
  1111. | declarator ':' expr_no_commas maybe_attribute
  1112. { $$ = make_field($1, $3, current_declspecs,
  1113. $4, prefix_attributes); }
  1114. | ':' expr_no_commas maybe_attribute
  1115. { $$ = make_field(NULL, $2, current_declspecs,
  1116. $3, prefix_attributes); }
  1117. ;
  1118. enumlist:
  1119. enumerator
  1120. | enumlist ',' enumerator
  1121. { $$ = declaration_chain($1, $3); }
  1122. | error
  1123. { $$ = NULL; }
  1124. ;
  1125. enumerator:
  1126. identifier
  1127. { $$ = make_enumerator($1.location, $1.id, NULL); }
  1128. | identifier '=' expr_no_commas
  1129. { $$ = make_enumerator($1.location, $1.id, $3); }
  1130. ;
  1131. typename:
  1132. typed_typespecs absdcl
  1133. { $$ = make_type($1, $2); }
  1134. | nonempty_type_quals absdcl
  1135. { $$ = make_type($1, $2); }
  1136. ;
  1137. absdcl: /* an abstract declarator */
  1138. /* empty */
  1139. { $$ = NULL; }
  1140. | absdcl1
  1141. ;
  1142. nonempty_type_quals:
  1143. type_qual
  1144. | nonempty_type_quals type_qual
  1145. { $$ = type_element_chain($1, $2); }
  1146. ;
  1147. type_quals:
  1148. /* empty */
  1149. { $$ = NULL; }
  1150. | type_quals type_qual
  1151. { $$ = type_element_chain($1, $2); }
  1152. ;
  1153. absdcl1: /* a nonempty abstract declarator */
  1154. '(' absdcl1 ')'
  1155. { $$ = $2; }
  1156. /* `(typedef)1' is `int'. */
  1157. | '*' type_quals absdcl1 %prec UNARY
  1158. { $$ = CAST(declarator, new_pointer_declarator(pr, $1.location, $3, $2)); }
  1159. | '*' type_quals %prec UNARY
  1160. { $$ = CAST(declarator, new_pointer_declarator(pr, $1.location, NULL, $2)); }
  1161. | absdcl1 '(' parmlist fn_quals %prec '.'
  1162. { $$ = make_function_declarator($2.location, $1, $3, $4); }
  1163. | absdcl1 '[' expr ']' %prec '.'
  1164. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, $3)); }
  1165. | absdcl1 '[' ']' %prec '.'
  1166. { $$ = CAST(declarator, new_array_declarator(pr, $2.location, $1, NULL)); }
  1167. | '(' parmlist fn_quals %prec '.'
  1168. { $$ = make_function_declarator($1.location, NULL, $2, $3); }
  1169. | '[' expr ']' %prec '.'
  1170. { $$ = CAST(declarator, new_array_declarator(pr, $1.location, NULL, $2)); }
  1171. | '[' ']' %prec '.'
  1172. { $$ = CAST(declarator, new_array_declarator(pr, $1.location, NULL, NULL)); }
  1173. /* ??? It appears we have to support attributes here, however
  1174. using prefix_attributes is wrong. */
  1175. ;
  1176. /* at least one statement, the first of which parses without error. */
  1177. /* stmts is used only after decls, so an invalid first statement
  1178. is actually regarded as an invalid decl and part of the decls. */
  1179. stmts:
  1180. stmt_or_labels
  1181. {
  1182. if (pedantic && $1.i)
  1183. pedwarn("ANSI C forbids label at end of compound statement");
  1184. /* Add an empty statement to last label if stand-alone */
  1185. if ($1.i)
  1186. {
  1187. statement last_label = CAST(statement, last_node(CAST(node, $1.stmt)));
  1188. chain_with_labels(last_label, CAST(statement, new_empty_stmt(pr, last_label->loc)));
  1189. }
  1190. $$ = $1.stmt;
  1191. }
  1192. ;
  1193. stmt_or_labels:
  1194. stmt_or_label
  1195. | stmt_or_labels stmt_or_label
  1196. { $$.i = $2.i; $$.stmt = chain_with_labels($1.stmt, $2.stmt); }
  1197. | stmt_or_labels errstmt
  1198. { $$.i = 0; $$.stmt = new_error_stmt(pr, last_location); }
  1199. ;
  1200. xstmts:
  1201. /* empty */ { $$ = NULL; }
  1202. | stmts
  1203. ;
  1204. errstmt: error ';'
  1205. ;
  1206. pushlevel: /* empty */
  1207. { pushlevel(FALSE); }
  1208. ;
  1209. /* Read zero or more forward-declarations for labels
  1210. that nested functions can jump to. */
  1211. maybe_label_decls:
  1212. /* empty */ { $$ = NULL; }
  1213. | label_decls
  1214. { if (pedantic)
  1215. pedwarn("ANSI C forbids label declarations");
  1216. $$ = $1; }
  1217. ;
  1218. label_decls:
  1219. label_decl
  1220. | label_decls label_decl { $$ = id_label_chain($1, $2); }
  1221. ;
  1222. label_decl:
  1223. LABEL identifiers_or_typenames ';'
  1224. { $$ = $2; }
  1225. ;
  1226. /* This is the body of a function definition.
  1227. It causes syntax errors to ignore to the next openbrace. */
  1228. compstmt_or_error:
  1229. compstmt
  1230. | error compstmt { $$ = $2; }
  1231. ;
  1232. compstmt_start: '{' { $$ = $1; compstmt_count++; }
  1233. compstmt: compstmt_start pushlevel '}'
  1234. { $$ = CAST(statement, new_compound_stmt(pr, $1.location, NULL, NULL, NULL, poplevel())); }
  1235. | compstmt_start pushlevel maybe_label_decls decls xstmts '}'
  1236. { $$ = CAST(statement, new_compound_stmt(pr, $1.location, $3, $4, $5, poplevel())); }
  1237. | compstmt_start pushlevel maybe_label_decls error '}'
  1238. { poplevel();
  1239. $$ = new_error_stmt(pr, last_location); }
  1240. | compstmt_start pushlevel maybe_label_decls stmts '}'
  1241. { $$ = CAST(statement, new_compound_stmt(pr, $1.location, $3, NULL, $4, poplevel())); }
  1242. ;
  1243. /* Value is number of statements counted as of the closeparen. */
  1244. simple_if:
  1245. if_prefix labeled_stmt
  1246. { $$.stmt = CAST(statement, new_if_stmt(pr, $1.location, $1.expr, $2, NULL));
  1247. $$.i = $1.i; }
  1248. | if_prefix error { $$.i = $1.i; $$.stmt = new_error_stmt(pr, last_location); }
  1249. ;
  1250. if_prefix:
  1251. IF '(' expr ')'
  1252. { $$.location = $1.location;
  1253. $$.i = stmt_count;
  1254. $$.expr = $3;
  1255. check_condition("if", $3); }
  1256. ;
  1257. /* This is a subroutine of stmt.
  1258. It is used twice, once for valid DO statements
  1259. and once for catching errors in parsing the end test. */
  1260. do_stmt_start:
  1261. DO
  1262. { stmt_count++;
  1263. compstmt_count++;
  1264. $<u.cstmt>$ = CAST(conditional_stmt,
  1265. new_dowhile_stmt(pr, $1.location, NULL, NULL));
  1266. push_loop(CAST(breakable_stmt, $<u.cstmt>$)); }
  1267. labeled_stmt WHILE
  1268. { $$ = $<u.cstmt>2;
  1269. $$->stmt = $3; }
  1270. ;
  1271. labeled_stmt:
  1272. stmt
  1273. { $$ = $1; }
  1274. | label labeled_stmt
  1275. { $$ = CAST(statement, new_labeled_stmt(pr, $1->loc, $1, $2)); }
  1276. ;
  1277. stmt_or_label:
  1278. stmt
  1279. { $$.i = 0; $$.stmt = $1; }
  1280. | label
  1281. { $$.i = 1; $$.stmt = CAST(statement, new_labeled_stmt(pr, $1->loc, $1, NULL)); }
  1282. ;
  1283. /* Parse a single real statement, not including any labels. */
  1284. stmt:
  1285. compstmt
  1286. { stmt_count++; $$ = $1; }
  1287. | expr ';'
  1288. { stmt_count++;
  1289. $$ = CAST(statement, new_expression_stmt(pr, $1->loc, $1)); }
  1290. | simple_if ELSE
  1291. { $1.i = stmt_count; }
  1292. labeled_stmt
  1293. { if (extra_warnings && stmt_count == $1.i)
  1294. warning("empty body in an else-statement");
  1295. $$ = $1.stmt;
  1296. CAST(if_stmt, $$)->stmt2 = $4;
  1297. }
  1298. | simple_if %prec IF
  1299. { /* This warning is here instead of in simple_if, because we
  1300. do not want a warning if an empty if is followed by an
  1301. else statement. Increment stmt_count so we don't
  1302. give a second error if this is a nested `if'. */
  1303. if (extra_warnings && stmt_count++ == $1.i)
  1304. warning_with_location ($1.stmt->loc,
  1305. "empty body in an if-statement");
  1306. $$ = $1.stmt; }
  1307. | simple_if ELSE error
  1308. { $$ = new_error_stmt(pr, last_location); }
  1309. | WHILE
  1310. { stmt_count++; }
  1311. '(' expr ')'
  1312. { check_condition("while", $4);
  1313. $<u.cstmt>$ = CAST(conditional_stmt,
  1314. new_while_stmt(pr, $1.location, $4, NULL));
  1315. /* The condition is not "in the loop" for break or continue */
  1316. push_loop(CAST(breakable_stmt, $<u.cstmt>$)); }
  1317. labeled_stmt
  1318. { $$ = CAST(statement, $<u.cstmt>6);
  1319. $<u.cstmt>6->stmt = $7;
  1320. pop_loop(); }
  1321. | do_stmt_start '(' expr ')' ';'
  1322. { $$ = CAST(statement, $1);
  1323. $1->condition = $3;
  1324. check_condition("do-while", $3);
  1325. /* Note that pop_loop should be before the expr to be consistent
  1326. with while, but GCC is inconsistent. See loop1.c */
  1327. pop_loop(); }
  1328. | do_stmt_start error
  1329. { $$ = new_error_stmt(pr, last_location);
  1330. pop_loop(); }
  1331. | FOR '(' xexpr ';' { stmt_count++; }
  1332. xexpr ';' { if ($6) check_condition("for", $6); }
  1333. xexpr ')'
  1334. { $<u.for_stmt>$ = new_for_stmt(pr, $1.location, $3, $6, $9, NULL);
  1335. push_loop(CAST(breakable_stmt, $<u.for_stmt>$)); }
  1336. labeled_stmt
  1337. { $$ = CAST(statement, $<u.for_stmt>11);
  1338. $<u.for_stmt>11->stmt = $12;
  1339. pop_loop(); }
  1340. | SWITCH '(' expr ')'
  1341. { stmt_count++; check_switch($3);
  1342. $<u.cstmt>$ = CAST(conditional_stmt,
  1343. new_switch_stmt(pr, $1.location, $3, NULL));
  1344. push_loop(CAST(breakable_stmt, $<u.cstmt>$)); }
  1345. labeled_stmt
  1346. { $$ = CAST(statement, $<u.cstmt>5);
  1347. $<u.cstmt>5->stmt = $6;
  1348. pop_loop(); }
  1349. | BREAK ';'
  1350. { stmt_count++;
  1351. $$ = CAST(statement, new_break_stmt(pr, $1.location));
  1352. check_break($$);
  1353. }
  1354. | CONTINUE ';'
  1355. { stmt_count++;
  1356. $$ = CAST(statement, new_continue_stmt(pr, $1.location));
  1357. check_continue($$);
  1358. }
  1359. | RETURN ';'
  1360. { stmt_count++;
  1361. $$ = CAST(statement, new_return_stmt(pr, $1.location, NULL));
  1362. check_void_return(); }
  1363. | RETURN expr ';'
  1364. { stmt_count++;
  1365. $$ = CAST(statement, new_return_stmt(pr, $1.location, $2));
  1366. check_return($2); }
  1367. | ASM_KEYWORD maybe_type_qual '(' expr ')' ';'
  1368. { stmt_count++;
  1369. $$ = CAST(statement, new_asm_stmt(pr, $1.location, $4, NULL,
  1370. NULL, NULL, $2)); }
  1371. /* This is the case with just output operands. */
  1372. | ASM_KEYWORD maybe_type_qual '(' expr ':' asm_operands ')' ';'
  1373. { stmt_count++;
  1374. $$ = CAST(statement, new_asm_stmt(pr, $1.location, $4, $6, NULL,
  1375. NULL, $2)); }
  1376. /* This is the case with input operands as well. */
  1377. | ASM_KEYWORD maybe_type_qual '(' expr ':' asm_operands ':' asm_operands ')' ';'
  1378. { stmt_count++;
  1379. $$ = CAST(statement, new_asm_stmt(pr, $1.location, $4, $6, $8, NULL, $2)); }
  1380. /* This is the case with clobbered registers as well. */
  1381. | ASM_KEYWORD maybe_type_qual '(' expr ':' asm_operands ':'
  1382. asm_operands ':' asm_clobbers ')' ';'
  1383. { stmt_count++;
  1384. $$ =

Large files files are truncated, but you can click here to view the full file