PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/cyclone/banshee/cparser/c-parse.y

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