]> gcc.gnu.org Git - gcc.git/blame - gcc/c-parser.c
tree-ssa-dce.c (mark_last_stmt_necessary): New function.
[gcc.git] / gcc / c-parser.c
CommitLineData
27bf414c
JM
1/* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
ebfbbdc5 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
5df27e4a 4 Free Software Foundation, Inc.
27bf414c
JM
5
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
8
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License as published by the Free
9dcd6f09 13Software Foundation; either version 3, or (at your option) any later
27bf414c
JM
14version.
15
16GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17WARRANTY; without even the implied warranty of MERCHANTABILITY or
18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19for more details.
20
21You should have received a copy of the GNU General Public License
9dcd6f09
NC
22along with GCC; see the file COPYING3. If not see
23<http://www.gnu.org/licenses/>. */
27bf414c
JM
24
25/* TODO:
26
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
30
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
33
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
36
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
39
40#include "config.h"
41#include "system.h"
42#include "coretypes.h"
f4ce02c5 43#include "tm.h" /* For rtl.h: needs enum reg_class. */
27bf414c
JM
44#include "tree.h"
45#include "langhooks.h"
46#include "input.h"
47#include "cpplib.h"
48#include "timevar.h"
49#include "c-pragma.h"
50#include "c-tree.h"
51#include "flags.h"
52#include "output.h"
53#include "toplev.h"
54#include "ggc.h"
55#include "c-common.h"
bc4071dd
RH
56#include "vec.h"
57#include "target.h"
474eccc6 58#include "cgraph.h"
68a607d8 59#include "plugin.h"
27bf414c
JM
60
61\f
27bf414c
JM
62/* Initialization routine for this file. */
63
64void
65c_parse_init (void)
66{
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
eea1139b 71 int mask = 0;
27bf414c 72
36c5e70a
BE
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
76
eea1139b
ILT
77 mask |= D_CXXONLY;
78 if (!flag_isoc99)
79 mask |= D_C99;
80 if (flag_no_asm)
81 {
82 mask |= D_ASM | D_EXT;
83 if (!flag_isoc99)
84 mask |= D_EXT89;
85 }
27bf414c 86 if (!c_dialect_objc ())
eea1139b 87 mask |= D_OBJC | D_CXX_OBJC;
27bf414c
JM
88
89 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
eea1139b 90 for (i = 0; i < num_c_common_reswords; i++)
27bf414c
JM
91 {
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
eea1139b
ILT
94 if (c_common_reswords[i].disable & mask)
95 {
96 if (warn_cxx_compat
97 && (c_common_reswords[i].disable & D_CXXWARN))
98 {
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
102 }
103 continue;
104 }
27bf414c 105
eea1139b
ILT
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
27bf414c 108 C_IS_RESERVED_WORD (id) = 1;
eea1139b 109 ridpointers [(int) c_common_reswords[i].rid] = id;
27bf414c
JM
110 }
111}
112\f
113/* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
119
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
123
124/* The following local token type is used. */
125
126/* A keyword. */
127#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
128
27bf414c
JM
129/* More information about the type of a CPP_NAME token. */
130typedef enum c_id_kind {
131 /* An ordinary identifier. */
132 C_ID_ID,
133 /* An identifier declared as a typedef name. */
134 C_ID_TYPENAME,
135 /* An identifier declared as an Objective-C class name. */
136 C_ID_CLASSNAME,
36c5e70a
BE
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
27bf414c
JM
139 /* Not an identifier. */
140 C_ID_NONE
141} c_id_kind;
142
143/* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
d1b38208 145typedef struct GTY (()) c_token {
27bf414c
JM
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
bc4071dd
RH
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
13fa1171 156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
27bf414c
JM
157 /* The location at which this token was found. */
158 location_t location;
8415f317
NF
159 /* The value associated with this token, if any. */
160 tree value;
27bf414c
JM
161} c_token;
162
163/* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
d1b38208 166typedef struct GTY(()) c_parser {
27bf414c
JM
167 /* The look-ahead tokens. */
168 c_token tokens[2];
169 /* How many look-ahead tokens are available (0, 1 or 2). */
170 short tokens_avail;
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
bc4071dd
RH
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
b4b56033
MLI
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
46c2514e
TT
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
0bacb8c7
TT
182 /* Objective-C specific parser/lexer information. */
183 BOOL_BITFIELD objc_pq_context : 1;
184 /* The following flag is needed to contextualize Objective-C lexical
185 analysis. In some cases (e.g., 'int NSObject;'), it is
186 undesirable to bind an identifier to an Objective-C class, even
187 if a class with that name exists. */
188 BOOL_BITFIELD objc_need_raw_identifier : 1;
27bf414c
JM
189} c_parser;
190
bc4071dd
RH
191
192/* The actual parser and external interface. ??? Does this need to be
193 garbage-collected? */
194
195static GTY (()) c_parser *the_parser;
196
197
27bf414c
JM
198/* Read in and lex a single token, storing it in *TOKEN. */
199
200static void
0bacb8c7 201c_lex_one_token (c_parser *parser, c_token *token)
27bf414c
JM
202{
203 timevar_push (TV_LEX);
bc4071dd 204
46c2514e
TT
205 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
206 (parser->lex_untranslated_string
207 ? C_LEX_STRING_NO_TRANSLATE : 0));
bc4071dd
RH
208 token->id_kind = C_ID_NONE;
209 token->keyword = RID_MAX;
210 token->pragma_kind = PRAGMA_NONE;
bc4071dd 211
27bf414c
JM
212 switch (token->type)
213 {
214 case CPP_NAME:
27bf414c
JM
215 {
216 tree decl;
217
0bacb8c7
TT
218 bool objc_force_identifier = parser->objc_need_raw_identifier;
219 if (c_dialect_objc ())
220 parser->objc_need_raw_identifier = false;
27bf414c
JM
221
222 if (C_IS_RESERVED_WORD (token->value))
223 {
224 enum rid rid_code = C_RID_CODE (token->value);
225
eea1139b
ILT
226 if (rid_code == RID_CXX_COMPAT_WARN)
227 {
3ba09659
AH
228 warning_at (token->location,
229 OPT_Wc___compat,
88388a52
JM
230 "identifier %qE conflicts with C++ keyword",
231 token->value);
eea1139b 232 }
36c5e70a
BE
233 else if (rid_code >= RID_FIRST_ADDR_SPACE
234 && rid_code <= RID_LAST_ADDR_SPACE)
235 {
236 token->id_kind = C_ID_ADDRSPACE;
237 token->keyword = rid_code;
238 break;
239 }
eea1139b 240 else if (c_dialect_objc ())
27bf414c 241 {
eea1139b 242 if (!objc_is_reserved_word (token->value)
0bacb8c7
TT
243 && (!OBJC_IS_PQ_KEYWORD (rid_code)
244 || parser->objc_pq_context))
27bf414c
JM
245 {
246 /* Return the canonical spelling for this keyword. */
247 token->value = ridpointers[(int) rid_code];
248 token->type = CPP_KEYWORD;
249 token->keyword = rid_code;
250 break;
251 }
252 }
253 else
254 {
27bf414c
JM
255 token->type = CPP_KEYWORD;
256 token->keyword = rid_code;
257 break;
258 }
259 }
260
261 decl = lookup_name (token->value);
262 if (decl)
263 {
264 if (TREE_CODE (decl) == TYPE_DECL)
265 {
266 token->id_kind = C_ID_TYPENAME;
267 break;
268 }
269 }
270 else if (c_dialect_objc ())
271 {
272 tree objc_interface_decl = objc_is_class_name (token->value);
273 /* Objective-C class names are in the same namespace as
274 variables and typedefs, and hence are shadowed by local
275 declarations. */
276 if (objc_interface_decl
277 && (global_bindings_p ()
278 || (!objc_force_identifier && !decl)))
279 {
280 token->value = objc_interface_decl;
281 token->id_kind = C_ID_CLASSNAME;
282 break;
283 }
284 }
bc4071dd 285 token->id_kind = C_ID_ID;
27bf414c 286 }
27bf414c
JM
287 break;
288 case CPP_AT_NAME:
289 /* This only happens in Objective-C; it must be a keyword. */
290 token->type = CPP_KEYWORD;
27bf414c
JM
291 token->keyword = C_RID_CODE (token->value);
292 break;
293 case CPP_COLON:
294 case CPP_COMMA:
295 case CPP_CLOSE_PAREN:
296 case CPP_SEMICOLON:
297 /* These tokens may affect the interpretation of any identifiers
298 following, if doing Objective-C. */
0bacb8c7
TT
299 if (c_dialect_objc ())
300 parser->objc_need_raw_identifier = false;
bc4071dd
RH
301 break;
302 case CPP_PRAGMA:
303 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
d75d71e0 304 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
bc4071dd 305 token->value = NULL;
27bf414c
JM
306 break;
307 default:
27bf414c
JM
308 break;
309 }
310 timevar_pop (TV_LEX);
311}
312
313/* Return a pointer to the next token from PARSER, reading it in if
314 necessary. */
315
316static inline c_token *
317c_parser_peek_token (c_parser *parser)
318{
319 if (parser->tokens_avail == 0)
320 {
0bacb8c7 321 c_lex_one_token (parser, &parser->tokens[0]);
27bf414c
JM
322 parser->tokens_avail = 1;
323 }
324 return &parser->tokens[0];
325}
326
327/* Return true if the next token from PARSER has the indicated
328 TYPE. */
329
330static inline bool
331c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
332{
333 return c_parser_peek_token (parser)->type == type;
334}
335
336/* Return true if the next token from PARSER does not have the
337 indicated TYPE. */
338
339static inline bool
340c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
341{
342 return !c_parser_next_token_is (parser, type);
343}
344
345/* Return true if the next token from PARSER is the indicated
346 KEYWORD. */
347
348static inline bool
349c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
350{
dbc518f0 351 return c_parser_peek_token (parser)->keyword == keyword;
27bf414c
JM
352}
353
354/* Return true if TOKEN can start a type name,
355 false otherwise. */
356static bool
357c_token_starts_typename (c_token *token)
358{
359 switch (token->type)
360 {
361 case CPP_NAME:
362 switch (token->id_kind)
363 {
364 case C_ID_ID:
365 return false;
36c5e70a
BE
366 case C_ID_ADDRSPACE:
367 return true;
27bf414c
JM
368 case C_ID_TYPENAME:
369 return true;
370 case C_ID_CLASSNAME:
371 gcc_assert (c_dialect_objc ());
372 return true;
373 default:
374 gcc_unreachable ();
375 }
376 case CPP_KEYWORD:
377 switch (token->keyword)
378 {
379 case RID_UNSIGNED:
380 case RID_LONG:
a6766312 381 case RID_INT128:
27bf414c
JM
382 case RID_SHORT:
383 case RID_SIGNED:
384 case RID_COMPLEX:
385 case RID_INT:
386 case RID_CHAR:
387 case RID_FLOAT:
388 case RID_DOUBLE:
389 case RID_VOID:
9a8ce21f
JG
390 case RID_DFLOAT32:
391 case RID_DFLOAT64:
392 case RID_DFLOAT128:
27bf414c
JM
393 case RID_BOOL:
394 case RID_ENUM:
395 case RID_STRUCT:
396 case RID_UNION:
397 case RID_TYPEOF:
398 case RID_CONST:
399 case RID_VOLATILE:
400 case RID_RESTRICT:
401 case RID_ATTRIBUTE:
ab22c1fa
CF
402 case RID_FRACT:
403 case RID_ACCUM:
404 case RID_SAT:
27bf414c
JM
405 return true;
406 default:
407 return false;
408 }
409 case CPP_LESS:
410 if (c_dialect_objc ())
411 return true;
412 return false;
413 default:
414 return false;
415 }
416}
417
418/* Return true if the next token from PARSER can start a type name,
419 false otherwise. */
420static inline bool
421c_parser_next_token_starts_typename (c_parser *parser)
422{
423 c_token *token = c_parser_peek_token (parser);
424 return c_token_starts_typename (token);
425}
426
427/* Return true if TOKEN can start declaration specifiers, false
428 otherwise. */
429static bool
430c_token_starts_declspecs (c_token *token)
431{
432 switch (token->type)
433 {
434 case CPP_NAME:
435 switch (token->id_kind)
436 {
437 case C_ID_ID:
438 return false;
36c5e70a
BE
439 case C_ID_ADDRSPACE:
440 return true;
27bf414c
JM
441 case C_ID_TYPENAME:
442 return true;
443 case C_ID_CLASSNAME:
444 gcc_assert (c_dialect_objc ());
445 return true;
446 default:
447 gcc_unreachable ();
448 }
449 case CPP_KEYWORD:
450 switch (token->keyword)
451 {
452 case RID_STATIC:
453 case RID_EXTERN:
454 case RID_REGISTER:
455 case RID_TYPEDEF:
456 case RID_INLINE:
457 case RID_AUTO:
458 case RID_THREAD:
459 case RID_UNSIGNED:
460 case RID_LONG:
a6766312 461 case RID_INT128:
27bf414c
JM
462 case RID_SHORT:
463 case RID_SIGNED:
464 case RID_COMPLEX:
465 case RID_INT:
466 case RID_CHAR:
467 case RID_FLOAT:
468 case RID_DOUBLE:
469 case RID_VOID:
9a8ce21f
JG
470 case RID_DFLOAT32:
471 case RID_DFLOAT64:
472 case RID_DFLOAT128:
27bf414c
JM
473 case RID_BOOL:
474 case RID_ENUM:
475 case RID_STRUCT:
476 case RID_UNION:
477 case RID_TYPEOF:
478 case RID_CONST:
479 case RID_VOLATILE:
480 case RID_RESTRICT:
481 case RID_ATTRIBUTE:
ab22c1fa
CF
482 case RID_FRACT:
483 case RID_ACCUM:
484 case RID_SAT:
27bf414c
JM
485 return true;
486 default:
487 return false;
488 }
489 case CPP_LESS:
490 if (c_dialect_objc ())
491 return true;
492 return false;
493 default:
494 return false;
495 }
496}
497
32912286
JM
498
499/* Return true if TOKEN can start declaration specifiers or a static
500 assertion, false otherwise. */
501static bool
502c_token_starts_declaration (c_token *token)
503{
504 if (c_token_starts_declspecs (token)
505 || token->keyword == RID_STATIC_ASSERT)
506 return true;
507 else
508 return false;
509}
510
27bf414c
JM
511/* Return true if the next token from PARSER can start declaration
512 specifiers, false otherwise. */
513static inline bool
514c_parser_next_token_starts_declspecs (c_parser *parser)
515{
516 c_token *token = c_parser_peek_token (parser);
517 return c_token_starts_declspecs (token);
518}
519
32912286
JM
520/* Return true if the next token from PARSER can start declaration
521 specifiers or a static assertion, false otherwise. */
522static inline bool
523c_parser_next_token_starts_declaration (c_parser *parser)
524{
525 c_token *token = c_parser_peek_token (parser);
526 return c_token_starts_declaration (token);
527}
528
27bf414c
JM
529/* Return a pointer to the next-but-one token from PARSER, reading it
530 in if necessary. The next token is already read in. */
531
532static c_token *
533c_parser_peek_2nd_token (c_parser *parser)
534{
535 if (parser->tokens_avail >= 2)
536 return &parser->tokens[1];
537 gcc_assert (parser->tokens_avail == 1);
538 gcc_assert (parser->tokens[0].type != CPP_EOF);
bc4071dd 539 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
0bacb8c7 540 c_lex_one_token (parser, &parser->tokens[1]);
27bf414c
JM
541 parser->tokens_avail = 2;
542 return &parser->tokens[1];
543}
544
545/* Consume the next token from PARSER. */
546
547static void
548c_parser_consume_token (c_parser *parser)
549{
bc4071dd
RH
550 gcc_assert (parser->tokens_avail >= 1);
551 gcc_assert (parser->tokens[0].type != CPP_EOF);
552 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
553 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
27bf414c
JM
554 if (parser->tokens_avail == 2)
555 parser->tokens[0] = parser->tokens[1];
27bf414c
JM
556 parser->tokens_avail--;
557}
558
bc4071dd
RH
559/* Expect the current token to be a #pragma. Consume it and remember
560 that we've begun parsing a pragma. */
561
562static void
563c_parser_consume_pragma (c_parser *parser)
564{
565 gcc_assert (!parser->in_pragma);
566 gcc_assert (parser->tokens_avail >= 1);
567 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
568 if (parser->tokens_avail == 2)
569 parser->tokens[0] = parser->tokens[1];
570 parser->tokens_avail--;
571 parser->in_pragma = true;
572}
573
27bf414c
JM
574/* Update the globals input_location and in_system_header from
575 TOKEN. */
576static inline void
577c_parser_set_source_position_from_token (c_token *token)
578{
579 if (token->type != CPP_EOF)
580 {
581 input_location = token->location;
27bf414c
JM
582 }
583}
584
27bf414c
JM
585/* Issue a diagnostic of the form
586 FILE:LINE: MESSAGE before TOKEN
587 where TOKEN is the next token in the input stream of PARSER.
588 MESSAGE (specified by the caller) is usually of the form "expected
589 OTHER-TOKEN".
590
591 Do not issue a diagnostic if still recovering from an error.
592
593 ??? This is taken from the C++ parser, but building up messages in
594 this way is not i18n-friendly and some other approach should be
595 used. */
596
597static void
4b794eaf 598c_parser_error (c_parser *parser, const char *gmsgid)
27bf414c
JM
599{
600 c_token *token = c_parser_peek_token (parser);
601 if (parser->error)
602 return;
603 parser->error = true;
4b794eaf 604 if (!gmsgid)
27bf414c
JM
605 return;
606 /* This diagnostic makes more sense if it is tagged to the line of
607 the token we just peeked at. */
608 c_parser_set_source_position_from_token (token);
4b794eaf 609 c_parse_error (gmsgid,
27bf414c
JM
610 /* Because c_parse_error does not understand
611 CPP_KEYWORD, keywords are treated like
612 identifiers. */
613 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
cfc93532
MLI
614 /* ??? The C parser does not save the cpp flags of a
615 token, we need to pass 0 here and we will not get
616 the source spelling of some tokens but rather the
617 canonical spelling. */
618 token->value, /*flags=*/0);
27bf414c
JM
619}
620
621/* If the next token is of the indicated TYPE, consume it. Otherwise,
622 issue the error MSGID. If MSGID is NULL then a message has already
623 been produced and no message will be produced this time. Returns
624 true if found, false otherwise. */
625
626static bool
627c_parser_require (c_parser *parser,
628 enum cpp_ttype type,
629 const char *msgid)
630{
631 if (c_parser_next_token_is (parser, type))
632 {
633 c_parser_consume_token (parser);
634 return true;
635 }
636 else
637 {
638 c_parser_error (parser, msgid);
639 return false;
640 }
641}
642
643/* If the next token is the indicated keyword, consume it. Otherwise,
644 issue the error MSGID. Returns true if found, false otherwise. */
645
646static bool
647c_parser_require_keyword (c_parser *parser,
648 enum rid keyword,
649 const char *msgid)
650{
651 if (c_parser_next_token_is_keyword (parser, keyword))
652 {
653 c_parser_consume_token (parser);
654 return true;
655 }
656 else
657 {
658 c_parser_error (parser, msgid);
659 return false;
660 }
661}
662
663/* Like c_parser_require, except that tokens will be skipped until the
664 desired token is found. An error message is still produced if the
665 next token is not as expected. If MSGID is NULL then a message has
666 already been produced and no message will be produced this
667 time. */
668
669static void
670c_parser_skip_until_found (c_parser *parser,
671 enum cpp_ttype type,
672 const char *msgid)
673{
674 unsigned nesting_depth = 0;
675
676 if (c_parser_require (parser, type, msgid))
677 return;
678
679 /* Skip tokens until the desired token is found. */
680 while (true)
681 {
682 /* Peek at the next token. */
683 c_token *token = c_parser_peek_token (parser);
684 /* If we've reached the token we want, consume it and stop. */
685 if (token->type == type && !nesting_depth)
686 {
687 c_parser_consume_token (parser);
688 break;
689 }
bc4071dd 690
27bf414c
JM
691 /* If we've run out of tokens, stop. */
692 if (token->type == CPP_EOF)
693 return;
bc4071dd
RH
694 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
695 return;
27bf414c
JM
696 if (token->type == CPP_OPEN_BRACE
697 || token->type == CPP_OPEN_PAREN
698 || token->type == CPP_OPEN_SQUARE)
699 ++nesting_depth;
700 else if (token->type == CPP_CLOSE_BRACE
701 || token->type == CPP_CLOSE_PAREN
702 || token->type == CPP_CLOSE_SQUARE)
703 {
704 if (nesting_depth-- == 0)
705 break;
706 }
707 /* Consume this token. */
708 c_parser_consume_token (parser);
709 }
710 parser->error = false;
711}
712
713/* Skip tokens until the end of a parameter is found, but do not
714 consume the comma, semicolon or closing delimiter. */
715
716static void
717c_parser_skip_to_end_of_parameter (c_parser *parser)
718{
719 unsigned nesting_depth = 0;
720
721 while (true)
722 {
723 c_token *token = c_parser_peek_token (parser);
724 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
725 && !nesting_depth)
726 break;
727 /* If we've run out of tokens, stop. */
728 if (token->type == CPP_EOF)
729 return;
bc4071dd
RH
730 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
731 return;
27bf414c
JM
732 if (token->type == CPP_OPEN_BRACE
733 || token->type == CPP_OPEN_PAREN
734 || token->type == CPP_OPEN_SQUARE)
735 ++nesting_depth;
736 else if (token->type == CPP_CLOSE_BRACE
737 || token->type == CPP_CLOSE_PAREN
738 || token->type == CPP_CLOSE_SQUARE)
739 {
740 if (nesting_depth-- == 0)
741 break;
742 }
743 /* Consume this token. */
744 c_parser_consume_token (parser);
745 }
746 parser->error = false;
747}
748
bc4071dd
RH
749/* Expect to be at the end of the pragma directive and consume an
750 end of line marker. */
751
752static void
753c_parser_skip_to_pragma_eol (c_parser *parser)
754{
755 gcc_assert (parser->in_pragma);
756 parser->in_pragma = false;
757
758 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
759 while (true)
760 {
761 c_token *token = c_parser_peek_token (parser);
762 if (token->type == CPP_EOF)
763 break;
764 if (token->type == CPP_PRAGMA_EOL)
765 {
766 c_parser_consume_token (parser);
767 break;
768 }
769 c_parser_consume_token (parser);
770 }
771
772 parser->error = false;
773}
27bf414c 774
2a83cc52
RH
775/* Skip tokens until we have consumed an entire block, or until we
776 have consumed a non-nested ';'. */
777
778static void
779c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
780{
781 unsigned nesting_depth = 0;
782 bool save_error = parser->error;
783
784 while (true)
785 {
786 c_token *token;
787
788 /* Peek at the next token. */
789 token = c_parser_peek_token (parser);
790
791 switch (token->type)
792 {
793 case CPP_EOF:
794 return;
795
796 case CPP_PRAGMA_EOL:
797 if (parser->in_pragma)
798 return;
799 break;
800
801 case CPP_SEMICOLON:
802 /* If the next token is a ';', we have reached the
803 end of the statement. */
804 if (!nesting_depth)
805 {
806 /* Consume the ';'. */
807 c_parser_consume_token (parser);
808 goto finished;
809 }
810 break;
811
812 case CPP_CLOSE_BRACE:
813 /* If the next token is a non-nested '}', then we have
814 reached the end of the current block. */
815 if (nesting_depth == 0 || --nesting_depth == 0)
816 {
817 c_parser_consume_token (parser);
818 goto finished;
819 }
820 break;
821
822 case CPP_OPEN_BRACE:
823 /* If it the next token is a '{', then we are entering a new
824 block. Consume the entire block. */
825 ++nesting_depth;
826 break;
827
828 case CPP_PRAGMA:
829 /* If we see a pragma, consume the whole thing at once. We
830 have some safeguards against consuming pragmas willy-nilly.
831 Normally, we'd expect to be here with parser->error set,
832 which disables these safeguards. But it's possible to get
833 here for secondary error recovery, after parser->error has
834 been cleared. */
835 c_parser_consume_pragma (parser);
836 c_parser_skip_to_pragma_eol (parser);
837 parser->error = save_error;
838 continue;
9e33de05
RS
839
840 default:
841 break;
2a83cc52
RH
842 }
843
844 c_parser_consume_token (parser);
845 }
846
847 finished:
848 parser->error = false;
849}
850
d2e796ad
MLI
851/* CPP's options (initialized by c-opts.c). */
852extern cpp_options *cpp_opts;
853
27bf414c
JM
854/* Save the warning flags which are controlled by __extension__. */
855
856static inline int
857disable_extension_diagnostics (void)
858{
859 int ret = (pedantic
860 | (warn_pointer_arith << 1)
861 | (warn_traditional << 2)
d2e796ad 862 | (flag_iso << 3)
24b97832
ILT
863 | (warn_long_long << 4)
864 | (warn_cxx_compat << 5));
d2e796ad 865 cpp_opts->pedantic = pedantic = 0;
27bf414c 866 warn_pointer_arith = 0;
d2e796ad 867 cpp_opts->warn_traditional = warn_traditional = 0;
27bf414c 868 flag_iso = 0;
9c650d90 869 cpp_opts->warn_long_long = warn_long_long = 0;
24b97832 870 warn_cxx_compat = 0;
27bf414c
JM
871 return ret;
872}
873
874/* Restore the warning flags which are controlled by __extension__.
875 FLAGS is the return value from disable_extension_diagnostics. */
876
877static inline void
878restore_extension_diagnostics (int flags)
879{
d2e796ad 880 cpp_opts->pedantic = pedantic = flags & 1;
27bf414c 881 warn_pointer_arith = (flags >> 1) & 1;
d2e796ad 882 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
27bf414c 883 flag_iso = (flags >> 3) & 1;
9c650d90 884 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
24b97832 885 warn_cxx_compat = (flags >> 5) & 1;
27bf414c
JM
886}
887
888/* Possibly kinds of declarator to parse. */
889typedef enum c_dtr_syn {
890 /* A normal declarator with an identifier. */
891 C_DTR_NORMAL,
892 /* An abstract declarator (maybe empty). */
893 C_DTR_ABSTRACT,
894 /* A parameter declarator: may be either, but after a type name does
895 not redeclare a typedef name as an identifier if it can
896 alternatively be interpreted as a typedef name; see DR#009,
897 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
898 following DR#249. For example, given a typedef T, "int T" and
899 "int *T" are valid parameter declarations redeclaring T, while
900 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
901 abstract declarators rather than involving redundant parentheses;
902 the same applies with attributes inside the parentheses before
903 "T". */
904 C_DTR_PARM
905} c_dtr_syn;
906
907static void c_parser_external_declaration (c_parser *);
908static void c_parser_asm_definition (c_parser *);
32912286
JM
909static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
910 bool, bool);
911static void c_parser_static_assert_declaration_no_semi (c_parser *);
912static void c_parser_static_assert_declaration (c_parser *);
27bf414c
JM
913static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
914 bool);
915static struct c_typespec c_parser_enum_specifier (c_parser *);
916static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
917static tree c_parser_struct_declaration (c_parser *);
918static struct c_typespec c_parser_typeof_specifier (c_parser *);
919static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
920 bool *);
921static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
922 c_dtr_syn, bool *);
923static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
924 bool,
925 struct c_declarator *);
926static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
927static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
928static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
929static tree c_parser_simple_asm_expr (c_parser *);
930static tree c_parser_attributes (c_parser *);
931static struct c_type_name *c_parser_type_name (c_parser *);
932static struct c_expr c_parser_initializer (c_parser *);
933static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
a1e3b3d9
LB
934static void c_parser_initelt (c_parser *, struct obstack *);
935static void c_parser_initval (c_parser *, struct c_expr *,
936 struct obstack *);
27bf414c
JM
937static tree c_parser_compound_statement (c_parser *);
938static void c_parser_compound_statement_nostart (c_parser *);
939static void c_parser_label (c_parser *);
940static void c_parser_statement (c_parser *);
941static void c_parser_statement_after_labels (c_parser *);
942static void c_parser_if_statement (c_parser *);
943static void c_parser_switch_statement (c_parser *);
944static void c_parser_while_statement (c_parser *);
945static void c_parser_do_statement (c_parser *);
946static void c_parser_for_statement (c_parser *);
947static tree c_parser_asm_statement (c_parser *);
46bdb9cf 948static tree c_parser_asm_operands (c_parser *, bool);
1c384bf1 949static tree c_parser_asm_goto_operands (c_parser *);
27bf414c
JM
950static tree c_parser_asm_clobbers (c_parser *);
951static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
952static struct c_expr c_parser_conditional_expression (c_parser *,
953 struct c_expr *);
954static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
955static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
956static struct c_expr c_parser_unary_expression (c_parser *);
957static struct c_expr c_parser_sizeof_expression (c_parser *);
958static struct c_expr c_parser_alignof_expression (c_parser *);
959static struct c_expr c_parser_postfix_expression (c_parser *);
960static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
24b97832
ILT
961 struct c_type_name *,
962 location_t);
27bf414c 963static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
c2255bc4 964 location_t loc,
27bf414c
JM
965 struct c_expr);
966static struct c_expr c_parser_expression (c_parser *);
46bdb9cf 967static struct c_expr c_parser_expression_conv (c_parser *);
bbbbb16a
ILT
968static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
969 VEC(tree,gc) **);
953ff289
DN
970static void c_parser_omp_construct (c_parser *);
971static void c_parser_omp_threadprivate (c_parser *);
972static void c_parser_omp_barrier (c_parser *);
973static void c_parser_omp_flush (c_parser *);
a68ab351 974static void c_parser_omp_taskwait (c_parser *);
27bf414c 975
bc4071dd
RH
976enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
977static bool c_parser_pragma (c_parser *, enum pragma_context);
978
27bf414c
JM
979/* These Objective-C parser functions are only ever called when
980 compiling Objective-C. */
981static void c_parser_objc_class_definition (c_parser *);
982static void c_parser_objc_class_instance_variables (c_parser *);
983static void c_parser_objc_class_declaration (c_parser *);
984static void c_parser_objc_alias_declaration (c_parser *);
985static void c_parser_objc_protocol_definition (c_parser *);
986static enum tree_code c_parser_objc_method_type (c_parser *);
987static void c_parser_objc_method_definition (c_parser *);
988static void c_parser_objc_methodprotolist (c_parser *);
989static void c_parser_objc_methodproto (c_parser *);
990static tree c_parser_objc_method_decl (c_parser *);
991static tree c_parser_objc_type_name (c_parser *);
992static tree c_parser_objc_protocol_refs (c_parser *);
993static void c_parser_objc_try_catch_statement (c_parser *);
994static void c_parser_objc_synchronized_statement (c_parser *);
995static tree c_parser_objc_selector (c_parser *);
996static tree c_parser_objc_selector_arg (c_parser *);
997static tree c_parser_objc_receiver (c_parser *);
998static tree c_parser_objc_message_args (c_parser *);
999static tree c_parser_objc_keywordexpr (c_parser *);
1000
1001/* Parse a translation unit (C90 6.7, C99 6.9).
1002
1003 translation-unit:
1004 external-declarations
1005
1006 external-declarations:
1007 external-declaration
1008 external-declarations external-declaration
1009
1010 GNU extensions:
1011
1012 translation-unit:
1013 empty
1014*/
1015
1016static void
1017c_parser_translation_unit (c_parser *parser)
1018{
1019 if (c_parser_next_token_is (parser, CPP_EOF))
1020 {
b8698a0f 1021 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 1022 "ISO C forbids an empty translation unit");
27bf414c
JM
1023 }
1024 else
1025 {
1026 void *obstack_position = obstack_alloc (&parser_obstack, 0);
6ec637a4 1027 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
1028 do
1029 {
1030 ggc_collect ();
1031 c_parser_external_declaration (parser);
1032 obstack_free (&parser_obstack, obstack_position);
1033 }
1034 while (c_parser_next_token_is_not (parser, CPP_EOF));
1035 }
1036}
1037
1038/* Parse an external declaration (C90 6.7, C99 6.9).
1039
1040 external-declaration:
1041 function-definition
1042 declaration
1043
1044 GNU extensions:
1045
1046 external-declaration:
1047 asm-definition
1048 ;
1049 __extension__ external-declaration
1050
1051 Objective-C:
1052
1053 external-declaration:
1054 objc-class-definition
1055 objc-class-declaration
1056 objc-alias-declaration
1057 objc-protocol-definition
1058 objc-method-definition
1059 @end
1060*/
1061
1062static void
1063c_parser_external_declaration (c_parser *parser)
1064{
1065 int ext;
1066 switch (c_parser_peek_token (parser)->type)
1067 {
1068 case CPP_KEYWORD:
1069 switch (c_parser_peek_token (parser)->keyword)
1070 {
1071 case RID_EXTENSION:
1072 ext = disable_extension_diagnostics ();
1073 c_parser_consume_token (parser);
1074 c_parser_external_declaration (parser);
1075 restore_extension_diagnostics (ext);
1076 break;
1077 case RID_ASM:
1078 c_parser_asm_definition (parser);
1079 break;
1080 case RID_AT_INTERFACE:
1081 case RID_AT_IMPLEMENTATION:
1082 gcc_assert (c_dialect_objc ());
1083 c_parser_objc_class_definition (parser);
1084 break;
eea1139b 1085 case RID_CLASS:
27bf414c
JM
1086 gcc_assert (c_dialect_objc ());
1087 c_parser_objc_class_declaration (parser);
1088 break;
1089 case RID_AT_ALIAS:
1090 gcc_assert (c_dialect_objc ());
1091 c_parser_objc_alias_declaration (parser);
1092 break;
1093 case RID_AT_PROTOCOL:
1094 gcc_assert (c_dialect_objc ());
1095 c_parser_objc_protocol_definition (parser);
1096 break;
1097 case RID_AT_END:
1098 gcc_assert (c_dialect_objc ());
1099 c_parser_consume_token (parser);
1100 objc_finish_implementation ();
1101 break;
1102 default:
1103 goto decl_or_fndef;
1104 }
1105 break;
1106 case CPP_SEMICOLON:
b8698a0f 1107 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 1108 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
1109 c_parser_consume_token (parser);
1110 break;
bc4071dd 1111 case CPP_PRAGMA:
6ec637a4 1112 mark_valid_location_for_stdc_pragma (true);
bc4071dd 1113 c_parser_pragma (parser, pragma_external);
6ec637a4 1114 mark_valid_location_for_stdc_pragma (false);
bc4071dd 1115 break;
27bf414c
JM
1116 case CPP_PLUS:
1117 case CPP_MINUS:
1118 if (c_dialect_objc ())
1119 {
1120 c_parser_objc_method_definition (parser);
1121 break;
1122 }
1123 /* Else fall through, and yield a syntax error trying to parse
1124 as a declaration or function definition. */
1125 default:
1126 decl_or_fndef:
1127 /* A declaration or a function definition. We can only tell
1128 which after parsing the declaration specifiers, if any, and
1129 the first declarator. */
32912286 1130 c_parser_declaration_or_fndef (parser, true, true, true, false, true);
27bf414c
JM
1131 break;
1132 }
1133}
1134
bc4071dd 1135
27bf414c
JM
1136/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1137 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1138 accepted; otherwise (old-style parameter declarations) only other
32912286
JM
1139 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1140 assertion is accepted; otherwise (old-style parameter declarations)
1141 it is not. If NESTED is true, we are inside a function or parsing
1142 old-style parameter declarations; any functions encountered are
1143 nested functions and declaration specifiers are required; otherwise
1144 we are at top level and functions are normal functions and
1145 declaration specifiers may be optional. If EMPTY_OK is true, empty
1146 declarations are OK (subject to all other constraints); otherwise
1147 (old-style parameter declarations) they are diagnosed. If
1148 START_ATTR_OK is true, the declaration specifiers may start with
1149 attributes; otherwise they may not.
27bf414c
JM
1150
1151 declaration:
1152 declaration-specifiers init-declarator-list[opt] ;
32912286 1153 static_assert-declaration
27bf414c
JM
1154
1155 function-definition:
1156 declaration-specifiers[opt] declarator declaration-list[opt]
1157 compound-statement
1158
1159 declaration-list:
1160 declaration
1161 declaration-list declaration
1162
1163 init-declarator-list:
1164 init-declarator
1165 init-declarator-list , init-declarator
1166
1167 init-declarator:
1168 declarator simple-asm-expr[opt] attributes[opt]
1169 declarator simple-asm-expr[opt] attributes[opt] = initializer
1170
1171 GNU extensions:
1172
1173 nested-function-definition:
1174 declaration-specifiers declarator declaration-list[opt]
1175 compound-statement
1176
1177 The simple-asm-expr and attributes are GNU extensions.
1178
1179 This function does not handle __extension__; that is handled in its
1180 callers. ??? Following the old parser, __extension__ may start
1181 external declarations, declarations in functions and declarations
1182 at the start of "for" loops, but not old-style parameter
1183 declarations.
1184
1185 C99 requires declaration specifiers in a function definition; the
1186 absence is diagnosed through the diagnosis of implicit int. In GNU
1187 C we also allow but diagnose declarations without declaration
1188 specifiers, but only at top level (elsewhere they conflict with
953ff289 1189 other syntax).
b8698a0f 1190
953ff289 1191 OpenMP:
b8698a0f 1192
953ff289
DN
1193 declaration:
1194 threadprivate-directive */
27bf414c
JM
1195
1196static void
32912286
JM
1197c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1198 bool static_assert_ok, bool empty_ok,
27bf414c
JM
1199 bool nested, bool start_attr_ok)
1200{
1201 struct c_declspecs *specs;
1202 tree prefix_attrs;
1203 tree all_prefix_attrs;
1204 bool diagnosed_no_specs = false;
c7412148 1205 location_t here = c_parser_peek_token (parser)->location;
bc4071dd 1206
32912286
JM
1207 if (static_assert_ok
1208 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1209 {
1210 c_parser_static_assert_declaration (parser);
1211 return;
1212 }
27bf414c
JM
1213 specs = build_null_declspecs ();
1214 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1215 if (parser->error)
1216 {
1217 c_parser_skip_to_end_of_block_or_statement (parser);
1218 return;
1219 }
1220 if (nested && !specs->declspecs_seen_p)
1221 {
1222 c_parser_error (parser, "expected declaration specifiers");
1223 c_parser_skip_to_end_of_block_or_statement (parser);
1224 return;
1225 }
1226 finish_declspecs (specs);
1227 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1228 {
1229 if (empty_ok)
1230 shadow_tag (specs);
1231 else
1232 {
1233 shadow_tag_warned (specs, 1);
509c9d60 1234 pedwarn (here, 0, "empty declaration");
27bf414c
JM
1235 }
1236 c_parser_consume_token (parser);
1237 return;
1238 }
1239 pending_xref_error ();
1240 prefix_attrs = specs->attrs;
1241 all_prefix_attrs = prefix_attrs;
1242 specs->attrs = NULL_TREE;
1243 while (true)
1244 {
1245 struct c_declarator *declarator;
1246 bool dummy = false;
1247 tree fnbody;
1248 /* Declaring either one or more declarators (in which case we
1249 should diagnose if there were no declaration specifiers) or a
1250 function definition (in which case the diagnostic for
1251 implicit int suffices). */
1252 declarator = c_parser_declarator (parser, specs->type_seen_p,
1253 C_DTR_NORMAL, &dummy);
1254 if (declarator == NULL)
1255 {
1256 c_parser_skip_to_end_of_block_or_statement (parser);
1257 return;
1258 }
1259 if (c_parser_next_token_is (parser, CPP_EQ)
1260 || c_parser_next_token_is (parser, CPP_COMMA)
1261 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1262 || c_parser_next_token_is_keyword (parser, RID_ASM)
1263 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1264 {
1265 tree asm_name = NULL_TREE;
1266 tree postfix_attrs = NULL_TREE;
1267 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1268 {
1269 diagnosed_no_specs = true;
509c9d60 1270 pedwarn (here, 0, "data definition has no type or storage class");
27bf414c
JM
1271 }
1272 /* Having seen a data definition, there cannot now be a
1273 function definition. */
1274 fndef_ok = false;
1275 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1276 asm_name = c_parser_simple_asm_expr (parser);
1277 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1278 postfix_attrs = c_parser_attributes (parser);
1279 if (c_parser_next_token_is (parser, CPP_EQ))
1280 {
1281 tree d;
1282 struct c_expr init;
c2255bc4 1283 location_t init_loc;
27bf414c
JM
1284 c_parser_consume_token (parser);
1285 /* The declaration of the variable is in effect while
1286 its initializer is parsed. */
1287 d = start_decl (declarator, specs, true,
1288 chainon (postfix_attrs, all_prefix_attrs));
1289 if (!d)
1290 d = error_mark_node;
1291 start_init (d, asm_name, global_bindings_p ());
c2255bc4 1292 init_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1293 init = c_parser_initializer (parser);
1294 finish_init ();
1295 if (d != error_mark_node)
1296 {
1297 maybe_warn_string_init (TREE_TYPE (d), init);
c2255bc4
AH
1298 finish_decl (d, init_loc, init.value,
1299 init.original_type, asm_name);
27bf414c
JM
1300 }
1301 }
1302 else
1303 {
1304 tree d = start_decl (declarator, specs, false,
1305 chainon (postfix_attrs,
1306 all_prefix_attrs));
1307 if (d)
c2255bc4
AH
1308 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1309 NULL_TREE, asm_name);
27bf414c
JM
1310 }
1311 if (c_parser_next_token_is (parser, CPP_COMMA))
1312 {
1313 c_parser_consume_token (parser);
1314 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1315 all_prefix_attrs = chainon (c_parser_attributes (parser),
1316 prefix_attrs);
1317 else
1318 all_prefix_attrs = prefix_attrs;
1319 continue;
1320 }
1321 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1322 {
1323 c_parser_consume_token (parser);
1324 return;
1325 }
1326 else
1327 {
1328 c_parser_error (parser, "expected %<,%> or %<;%>");
1329 c_parser_skip_to_end_of_block_or_statement (parser);
1330 return;
1331 }
1332 }
1333 else if (!fndef_ok)
1334 {
1335 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1336 "%<asm%> or %<__attribute__%>");
1337 c_parser_skip_to_end_of_block_or_statement (parser);
1338 return;
1339 }
1340 /* Function definition (nested or otherwise). */
1341 if (nested)
1342 {
509c9d60 1343 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
d2784db4 1344 c_push_function_context ();
27bf414c
JM
1345 }
1346 if (!start_function (specs, declarator, all_prefix_attrs))
1347 {
1348 /* This can appear in many cases looking nothing like a
1349 function definition, so we don't give a more specific
1350 error suggesting there was one. */
1351 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1352 "or %<__attribute__%>");
1353 if (nested)
d2784db4 1354 c_pop_function_context ();
27bf414c
JM
1355 break;
1356 }
1357 /* Parse old-style parameter declarations. ??? Attributes are
1358 not allowed to start declaration specifiers here because of a
1359 syntax conflict between a function declaration with attribute
1360 suffix and a function definition with an attribute prefix on
1361 first old-style parameter declaration. Following the old
1362 parser, they are not accepted on subsequent old-style
1363 parameter declarations either. However, there is no
1364 ambiguity after the first declaration, nor indeed on the
1365 first as long as we don't allow postfix attributes after a
1366 declarator with a nonempty identifier list in a definition;
1367 and postfix attributes have never been accepted here in
1368 function definitions either. */
1369 while (c_parser_next_token_is_not (parser, CPP_EOF)
1370 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
32912286
JM
1371 c_parser_declaration_or_fndef (parser, false, false, false,
1372 true, false);
27bf414c 1373 store_parm_decls ();
1751ecd6
AH
1374 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1375 = c_parser_peek_token (parser)->location;
27bf414c
JM
1376 fnbody = c_parser_compound_statement (parser);
1377 if (nested)
1378 {
1379 tree decl = current_function_decl;
9f62cb92
JJ
1380 /* Mark nested functions as needing static-chain initially.
1381 lower_nested_functions will recompute it but the
1382 DECL_STATIC_CHAIN flag is also used before that happens,
1383 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1384 DECL_STATIC_CHAIN (decl) = 1;
27bf414c
JM
1385 add_stmt (fnbody);
1386 finish_function ();
d2784db4 1387 c_pop_function_context ();
c2255bc4 1388 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
27bf414c
JM
1389 }
1390 else
1391 {
1392 add_stmt (fnbody);
1393 finish_function ();
1394 }
1395 break;
1396 }
1397}
1398
1399/* Parse an asm-definition (asm() outside a function body). This is a
1400 GNU extension.
1401
1402 asm-definition:
1403 simple-asm-expr ;
1404*/
1405
1406static void
1407c_parser_asm_definition (c_parser *parser)
1408{
1409 tree asm_str = c_parser_simple_asm_expr (parser);
27bf414c 1410 if (asm_str)
474eccc6 1411 cgraph_add_asm_node (asm_str);
27bf414c
JM
1412 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1413}
1414
32912286
JM
1415/* Parse a static assertion (C1X N1425 6.7.10).
1416
1417 static_assert-declaration:
1418 static_assert-declaration-no-semi ;
1419*/
1420
1421static void
1422c_parser_static_assert_declaration (c_parser *parser)
1423{
1424 c_parser_static_assert_declaration_no_semi (parser);
1425 if (parser->error
1426 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1427 c_parser_skip_to_end_of_block_or_statement (parser);
1428}
1429
1430/* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1431 semicolon.
1432
1433 static_assert-declaration-no-semi:
1434 _Static_assert ( constant-expression , string-literal )
1435*/
1436
1437static void
1438c_parser_static_assert_declaration_no_semi (c_parser *parser)
1439{
1440 location_t assert_loc, value_loc;
1441 tree value;
1442 tree string;
1443
1444 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1445 assert_loc = c_parser_peek_token (parser)->location;
1446 if (!flag_isoc1x)
1447 {
1448 if (flag_isoc99)
1449 pedwarn (assert_loc, OPT_pedantic,
1450 "ISO C99 does not support %<_Static_assert%>");
1451 else
1452 pedwarn (assert_loc, OPT_pedantic,
1453 "ISO C90 does not support %<_Static_assert%>");
1454 }
1455 c_parser_consume_token (parser);
1456 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1457 return;
1458 value_loc = c_parser_peek_token (parser)->location;
1459 value = c_parser_expr_no_commas (parser, NULL).value;
1460 parser->lex_untranslated_string = true;
1461 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1462 {
1463 parser->lex_untranslated_string = false;
1464 return;
1465 }
1466 switch (c_parser_peek_token (parser)->type)
1467 {
1468 case CPP_STRING:
1469 case CPP_STRING16:
1470 case CPP_STRING32:
1471 case CPP_WSTRING:
1472 case CPP_UTF8STRING:
1473 string = c_parser_peek_token (parser)->value;
1474 c_parser_consume_token (parser);
1475 parser->lex_untranslated_string = false;
1476 break;
1477 default:
1478 c_parser_error (parser, "expected string literal");
1479 parser->lex_untranslated_string = false;
1480 return;
1481 }
1482 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1483
1484 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1485 {
1486 error_at (value_loc, "expression in static assertion is not an integer");
1487 return;
1488 }
1489 if (TREE_CODE (value) != INTEGER_CST)
1490 {
1491 value = c_fully_fold (value, false, NULL);
1492 if (TREE_CODE (value) == INTEGER_CST)
1493 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1494 "is not an integer constant expression");
1495 }
1496 if (TREE_CODE (value) != INTEGER_CST)
1497 {
1498 error_at (value_loc, "expression in static assertion is not constant");
1499 return;
1500 }
1501 constant_expression_warning (value);
1502 if (integer_zerop (value))
1503 error_at (assert_loc, "static assertion failed: %E", string);
1504}
1505
27bf414c
JM
1506/* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1507 6.7), adding them to SPECS (which may already include some).
1508 Storage class specifiers are accepted iff SCSPEC_OK; type
1509 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1510 the start iff START_ATTR_OK.
1511
1512 declaration-specifiers:
1513 storage-class-specifier declaration-specifiers[opt]
1514 type-specifier declaration-specifiers[opt]
1515 type-qualifier declaration-specifiers[opt]
1516 function-specifier declaration-specifiers[opt]
1517
1518 Function specifiers (inline) are from C99, and are currently
1519 handled as storage class specifiers, as is __thread.
1520
1521 C90 6.5.1, C99 6.7.1:
1522 storage-class-specifier:
1523 typedef
1524 extern
1525 static
1526 auto
1527 register
1528
1529 C99 6.7.4:
1530 function-specifier:
1531 inline
1532
1533 C90 6.5.2, C99 6.7.2:
1534 type-specifier:
1535 void
1536 char
1537 short
1538 int
1539 long
1540 float
1541 double
1542 signed
1543 unsigned
1544 _Bool
1545 _Complex
1546 [_Imaginary removed in C99 TC2]
1547 struct-or-union-specifier
1548 enum-specifier
1549 typedef-name
1550
1551 (_Bool and _Complex are new in C99.)
1552
1553 C90 6.5.3, C99 6.7.3:
1554
1555 type-qualifier:
1556 const
1557 restrict
1558 volatile
36c5e70a 1559 address-space-qualifier
27bf414c
JM
1560
1561 (restrict is new in C99.)
1562
1563 GNU extensions:
1564
1565 declaration-specifiers:
1566 attributes declaration-specifiers[opt]
1567
36c5e70a
BE
1568 type-qualifier:
1569 address-space
1570
1571 address-space:
1572 identifier recognized by the target
1573
27bf414c
JM
1574 storage-class-specifier:
1575 __thread
1576
1577 type-specifier:
1578 typeof-specifier
a6766312 1579 __int128
9a8ce21f
JG
1580 _Decimal32
1581 _Decimal64
1582 _Decimal128
ab22c1fa
CF
1583 _Fract
1584 _Accum
1585 _Sat
1586
1587 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1588 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
27bf414c
JM
1589
1590 Objective-C:
1591
1592 type-specifier:
1593 class-name objc-protocol-refs[opt]
1594 typedef-name objc-protocol-refs
1595 objc-protocol-refs
1596*/
1597
1598static void
1599c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1600 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1601{
1602 bool attrs_ok = start_attr_ok;
1603 bool seen_type = specs->type_seen_p;
1604 while (c_parser_next_token_is (parser, CPP_NAME)
1605 || c_parser_next_token_is (parser, CPP_KEYWORD)
1606 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1607 {
1608 struct c_typespec t;
1609 tree attrs;
dc491a25 1610 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1611 if (c_parser_next_token_is (parser, CPP_NAME))
1612 {
1613 tree value = c_parser_peek_token (parser)->value;
1614 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
36c5e70a
BE
1615
1616 if (kind == C_ID_ADDRSPACE)
1617 {
1618 addr_space_t as
1619 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1620 declspecs_add_addrspace (specs, as);
1621 c_parser_consume_token (parser);
1622 attrs_ok = true;
1623 continue;
1624 }
1625
27bf414c
JM
1626 /* This finishes the specifiers unless a type name is OK, it
1627 is declared as a type name and a type name hasn't yet
1628 been seen. */
1629 if (!typespec_ok || seen_type
1630 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1631 break;
1632 c_parser_consume_token (parser);
1633 seen_type = true;
1634 attrs_ok = true;
1635 if (kind == C_ID_TYPENAME
1636 && (!c_dialect_objc ()
1637 || c_parser_next_token_is_not (parser, CPP_LESS)))
1638 {
1639 t.kind = ctsk_typedef;
1640 /* For a typedef name, record the meaning, not the name.
1641 In case of 'foo foo, bar;'. */
1642 t.spec = lookup_name (value);
928c19bb
JM
1643 t.expr = NULL_TREE;
1644 t.expr_const_operands = true;
27bf414c
JM
1645 }
1646 else
1647 {
1648 tree proto = NULL_TREE;
1649 gcc_assert (c_dialect_objc ());
1650 t.kind = ctsk_objc;
1651 if (c_parser_next_token_is (parser, CPP_LESS))
1652 proto = c_parser_objc_protocol_refs (parser);
1653 t.spec = objc_get_protocol_qualified_type (value, proto);
928c19bb
JM
1654 t.expr = NULL_TREE;
1655 t.expr_const_operands = true;
27bf414c 1656 }
dc491a25 1657 declspecs_add_type (loc, specs, t);
27bf414c
JM
1658 continue;
1659 }
1660 if (c_parser_next_token_is (parser, CPP_LESS))
1661 {
1662 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1663 nisse@lysator.liu.se. */
1664 tree proto;
1665 gcc_assert (c_dialect_objc ());
1666 if (!typespec_ok || seen_type)
1667 break;
1668 proto = c_parser_objc_protocol_refs (parser);
1669 t.kind = ctsk_objc;
1670 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
928c19bb
JM
1671 t.expr = NULL_TREE;
1672 t.expr_const_operands = true;
dc491a25 1673 declspecs_add_type (loc, specs, t);
27bf414c
JM
1674 continue;
1675 }
1676 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1677 switch (c_parser_peek_token (parser)->keyword)
1678 {
1679 case RID_STATIC:
1680 case RID_EXTERN:
1681 case RID_REGISTER:
1682 case RID_TYPEDEF:
1683 case RID_INLINE:
1684 case RID_AUTO:
1685 case RID_THREAD:
1686 if (!scspec_ok)
1687 goto out;
1688 attrs_ok = true;
1689 /* TODO: Distinguish between function specifiers (inline)
1690 and storage class specifiers, either here or in
1691 declspecs_add_scspec. */
1692 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1693 c_parser_consume_token (parser);
1694 break;
1695 case RID_UNSIGNED:
1696 case RID_LONG:
a6766312 1697 case RID_INT128:
27bf414c
JM
1698 case RID_SHORT:
1699 case RID_SIGNED:
1700 case RID_COMPLEX:
1701 case RID_INT:
1702 case RID_CHAR:
1703 case RID_FLOAT:
1704 case RID_DOUBLE:
1705 case RID_VOID:
9a8ce21f
JG
1706 case RID_DFLOAT32:
1707 case RID_DFLOAT64:
1708 case RID_DFLOAT128:
27bf414c 1709 case RID_BOOL:
ab22c1fa
CF
1710 case RID_FRACT:
1711 case RID_ACCUM:
1712 case RID_SAT:
27bf414c
JM
1713 if (!typespec_ok)
1714 goto out;
1715 attrs_ok = true;
1716 seen_type = true;
0bacb8c7
TT
1717 if (c_dialect_objc ())
1718 parser->objc_need_raw_identifier = true;
27bf414c
JM
1719 t.kind = ctsk_resword;
1720 t.spec = c_parser_peek_token (parser)->value;
928c19bb
JM
1721 t.expr = NULL_TREE;
1722 t.expr_const_operands = true;
dc491a25 1723 declspecs_add_type (loc, specs, t);
27bf414c
JM
1724 c_parser_consume_token (parser);
1725 break;
1726 case RID_ENUM:
1727 if (!typespec_ok)
1728 goto out;
1729 attrs_ok = true;
1730 seen_type = true;
1731 t = c_parser_enum_specifier (parser);
dc491a25 1732 declspecs_add_type (loc, specs, t);
27bf414c
JM
1733 break;
1734 case RID_STRUCT:
1735 case RID_UNION:
1736 if (!typespec_ok)
1737 goto out;
1738 attrs_ok = true;
1739 seen_type = true;
1740 t = c_parser_struct_or_union_specifier (parser);
dab71827 1741 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
dc491a25 1742 declspecs_add_type (loc, specs, t);
27bf414c
JM
1743 break;
1744 case RID_TYPEOF:
1745 /* ??? The old parser rejected typeof after other type
1746 specifiers, but is a syntax error the best way of
1747 handling this? */
1748 if (!typespec_ok || seen_type)
1749 goto out;
1750 attrs_ok = true;
1751 seen_type = true;
1752 t = c_parser_typeof_specifier (parser);
dc491a25 1753 declspecs_add_type (loc, specs, t);
27bf414c
JM
1754 break;
1755 case RID_CONST:
1756 case RID_VOLATILE:
1757 case RID_RESTRICT:
1758 attrs_ok = true;
1759 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1760 c_parser_consume_token (parser);
1761 break;
1762 case RID_ATTRIBUTE:
1763 if (!attrs_ok)
1764 goto out;
1765 attrs = c_parser_attributes (parser);
1766 declspecs_add_attrs (specs, attrs);
1767 break;
1768 default:
1769 goto out;
1770 }
1771 }
1772 out: ;
1773}
1774
1775/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1776
1777 enum-specifier:
1778 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1779 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1780 enum attributes[opt] identifier
1781
1782 The form with trailing comma is new in C99. The forms with
1783 attributes are GNU extensions. In GNU C, we accept any expression
1784 without commas in the syntax (assignment expressions, not just
1785 conditional expressions); assignment expressions will be diagnosed
1786 as non-constant.
1787
1788 enumerator-list:
1789 enumerator
1790 enumerator-list , enumerator
1791
1792 enumerator:
1793 enumeration-constant
1794 enumeration-constant = constant-expression
1795*/
1796
1797static struct c_typespec
1798c_parser_enum_specifier (c_parser *parser)
1799{
1800 struct c_typespec ret;
1801 tree attrs;
1802 tree ident = NULL_TREE;
24b97832 1803 location_t enum_loc;
922f2908 1804 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c 1805 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
24b97832 1806 enum_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1807 c_parser_consume_token (parser);
1808 attrs = c_parser_attributes (parser);
c2255bc4 1809 enum_loc = c_parser_peek_token (parser)->location;
5af28c74
TT
1810 /* Set the location in case we create a decl now. */
1811 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
27bf414c
JM
1812 if (c_parser_next_token_is (parser, CPP_NAME))
1813 {
1814 ident = c_parser_peek_token (parser)->value;
c7412148 1815 ident_loc = c_parser_peek_token (parser)->location;
24b97832 1816 enum_loc = ident_loc;
27bf414c
JM
1817 c_parser_consume_token (parser);
1818 }
1819 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1820 {
1821 /* Parse an enum definition. */
7114359f 1822 struct c_enum_contents the_enum;
c2255bc4 1823 tree type = start_enum (enum_loc, &the_enum, ident);
27bf414c
JM
1824 tree postfix_attrs;
1825 /* We chain the enumerators in reverse order, then put them in
1826 forward order at the end. */
1827 tree values = NULL_TREE;
1828 c_parser_consume_token (parser);
1829 while (true)
1830 {
1831 tree enum_id;
1832 tree enum_value;
1833 tree enum_decl;
1834 bool seen_comma;
5af28c74 1835 c_token *token;
922f2908 1836 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
85790e66 1837 location_t value_loc;
27bf414c
JM
1838 if (c_parser_next_token_is_not (parser, CPP_NAME))
1839 {
1840 c_parser_error (parser, "expected identifier");
1841 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1842 values = error_mark_node;
1843 break;
1844 }
5af28c74
TT
1845 token = c_parser_peek_token (parser);
1846 enum_id = token->value;
1847 /* Set the location in case we create a decl now. */
1848 c_parser_set_source_position_from_token (token);
85790e66 1849 value_loc = token->location;
27bf414c
JM
1850 c_parser_consume_token (parser);
1851 if (c_parser_next_token_is (parser, CPP_EQ))
1852 {
1853 c_parser_consume_token (parser);
85790e66 1854 value_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1855 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1856 }
1857 else
1858 enum_value = NULL_TREE;
c2255bc4
AH
1859 enum_decl = build_enumerator (value_loc,
1860 &the_enum, enum_id, enum_value);
27bf414c
JM
1861 TREE_CHAIN (enum_decl) = values;
1862 values = enum_decl;
1863 seen_comma = false;
1864 if (c_parser_next_token_is (parser, CPP_COMMA))
1865 {
c7412148 1866 comma_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1867 seen_comma = true;
1868 c_parser_consume_token (parser);
1869 }
1870 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1871 {
fcf73884 1872 if (seen_comma && !flag_isoc99)
509c9d60 1873 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
27bf414c
JM
1874 c_parser_consume_token (parser);
1875 break;
1876 }
1877 if (!seen_comma)
1878 {
1879 c_parser_error (parser, "expected %<,%> or %<}%>");
1880 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1881 values = error_mark_node;
1882 break;
1883 }
1884 }
1885 postfix_attrs = c_parser_attributes (parser);
1886 ret.spec = finish_enum (type, nreverse (values),
1887 chainon (attrs, postfix_attrs));
1888 ret.kind = ctsk_tagdef;
928c19bb
JM
1889 ret.expr = NULL_TREE;
1890 ret.expr_const_operands = true;
27bf414c
JM
1891 return ret;
1892 }
1893 else if (!ident)
1894 {
1895 c_parser_error (parser, "expected %<{%>");
1896 ret.spec = error_mark_node;
1897 ret.kind = ctsk_tagref;
928c19bb
JM
1898 ret.expr = NULL_TREE;
1899 ret.expr_const_operands = true;
27bf414c
JM
1900 return ret;
1901 }
c2255bc4 1902 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
27bf414c
JM
1903 /* In ISO C, enumerated types can be referred to only if already
1904 defined. */
1905 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
c7412148
TT
1906 {
1907 gcc_assert (ident);
c2255bc4 1908 pedwarn (enum_loc, OPT_pedantic,
509c9d60 1909 "ISO C forbids forward references to %<enum%> types");
c7412148 1910 }
27bf414c
JM
1911 return ret;
1912}
1913
1914/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1915
1916 struct-or-union-specifier:
1917 struct-or-union attributes[opt] identifier[opt]
1918 { struct-contents } attributes[opt]
1919 struct-or-union attributes[opt] identifier
1920
1921 struct-contents:
1922 struct-declaration-list
1923
1924 struct-declaration-list:
1925 struct-declaration ;
1926 struct-declaration-list struct-declaration ;
1927
1928 GNU extensions:
1929
1930 struct-contents:
1931 empty
1932 struct-declaration
1933 struct-declaration-list struct-declaration
1934
1935 struct-declaration-list:
1936 struct-declaration-list ;
1937 ;
1938
1939 (Note that in the syntax here, unlike that in ISO C, the semicolons
1940 are included here rather than in struct-declaration, in order to
1941 describe the syntax with extra semicolons and missing semicolon at
1942 end.)
1943
1944 Objective-C:
1945
1946 struct-declaration-list:
1947 @defs ( class-name )
1948
1949 (Note this does not include a trailing semicolon, but can be
1950 followed by further declarations, and gets a pedwarn-if-pedantic
1951 when followed by a semicolon.) */
1952
1953static struct c_typespec
1954c_parser_struct_or_union_specifier (c_parser *parser)
1955{
1956 struct c_typespec ret;
1957 tree attrs;
1958 tree ident = NULL_TREE;
24b97832
ILT
1959 location_t struct_loc;
1960 location_t ident_loc = UNKNOWN_LOCATION;
27bf414c
JM
1961 enum tree_code code;
1962 switch (c_parser_peek_token (parser)->keyword)
1963 {
1964 case RID_STRUCT:
1965 code = RECORD_TYPE;
1966 break;
1967 case RID_UNION:
1968 code = UNION_TYPE;
1969 break;
1970 default:
1971 gcc_unreachable ();
1972 }
24b97832 1973 struct_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
1974 c_parser_consume_token (parser);
1975 attrs = c_parser_attributes (parser);
c2255bc4 1976
5af28c74
TT
1977 /* Set the location in case we create a decl now. */
1978 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
c2255bc4 1979
27bf414c
JM
1980 if (c_parser_next_token_is (parser, CPP_NAME))
1981 {
1982 ident = c_parser_peek_token (parser)->value;
24b97832
ILT
1983 ident_loc = c_parser_peek_token (parser)->location;
1984 struct_loc = ident_loc;
27bf414c
JM
1985 c_parser_consume_token (parser);
1986 }
1987 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1988 {
1989 /* Parse a struct or union definition. Start the scope of the
1990 tag before parsing components. */
dc491a25
ILT
1991 struct c_struct_parse_info *struct_info;
1992 tree type = start_struct (struct_loc, code, ident, &struct_info);
27bf414c
JM
1993 tree postfix_attrs;
1994 /* We chain the components in reverse order, then put them in
1995 forward order at the end. Each struct-declaration may
1996 declare multiple components (comma-separated), so we must use
1997 chainon to join them, although when parsing each
1998 struct-declaration we can use TREE_CHAIN directly.
1999
2000 The theory behind all this is that there will be more
2001 semicolon separated fields than comma separated fields, and
2002 so we'll be minimizing the number of node traversals required
2003 by chainon. */
2004 tree contents = NULL_TREE;
2005 c_parser_consume_token (parser);
2006 /* Handle the Objective-C @defs construct,
2007 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2008 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2009 {
2010 tree name;
2011 gcc_assert (c_dialect_objc ());
2012 c_parser_consume_token (parser);
2013 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2014 goto end_at_defs;
2015 if (c_parser_next_token_is (parser, CPP_NAME)
2016 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2017 {
2018 name = c_parser_peek_token (parser)->value;
2019 c_parser_consume_token (parser);
2020 }
2021 else
2022 {
2023 c_parser_error (parser, "expected class name");
2024 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2025 goto end_at_defs;
2026 }
2027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2028 "expected %<)%>");
2029 contents = nreverse (objc_get_class_ivars (name));
2030 }
2031 end_at_defs:
2032 /* Parse the struct-declarations and semicolons. Problems with
2033 semicolons are diagnosed here; empty structures are diagnosed
2034 elsewhere. */
2035 while (true)
2036 {
2037 tree decls;
2038 /* Parse any stray semicolon. */
2039 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2040 {
b8698a0f 2041 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 2042 "extra semicolon in struct or union specified");
27bf414c
JM
2043 c_parser_consume_token (parser);
2044 continue;
2045 }
2046 /* Stop if at the end of the struct or union contents. */
2047 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2048 {
2049 c_parser_consume_token (parser);
2050 break;
2051 }
bc4071dd
RH
2052 /* Accept #pragmas at struct scope. */
2053 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2054 {
2055 c_parser_pragma (parser, pragma_external);
2056 continue;
2057 }
27bf414c
JM
2058 /* Parse some comma-separated declarations, but not the
2059 trailing semicolon if any. */
2060 decls = c_parser_struct_declaration (parser);
2061 contents = chainon (decls, contents);
2062 /* If no semicolon follows, either we have a parse error or
2063 are at the end of the struct or union and should
2064 pedwarn. */
2065 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2066 c_parser_consume_token (parser);
2067 else
2068 {
2069 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
b8698a0f 2070 pedwarn (c_parser_peek_token (parser)->location, 0,
509c9d60 2071 "no semicolon at end of struct or union");
27bf414c
JM
2072 else
2073 {
2074 c_parser_error (parser, "expected %<;%>");
2075 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2076 break;
2077 }
2078 }
2079 }
2080 postfix_attrs = c_parser_attributes (parser);
c2255bc4 2081 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
dc491a25 2082 chainon (attrs, postfix_attrs), struct_info);
27bf414c 2083 ret.kind = ctsk_tagdef;
928c19bb
JM
2084 ret.expr = NULL_TREE;
2085 ret.expr_const_operands = true;
27bf414c
JM
2086 return ret;
2087 }
2088 else if (!ident)
2089 {
2090 c_parser_error (parser, "expected %<{%>");
2091 ret.spec = error_mark_node;
2092 ret.kind = ctsk_tagref;
928c19bb
JM
2093 ret.expr = NULL_TREE;
2094 ret.expr_const_operands = true;
67c2939d 2095 return ret;
27bf414c 2096 }
c2255bc4 2097 ret = parser_xref_tag (ident_loc, code, ident);
27bf414c
JM
2098 return ret;
2099}
2100
2101/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2102 the trailing semicolon.
2103
2104 struct-declaration:
2105 specifier-qualifier-list struct-declarator-list
32912286 2106 static_assert-declaration-no-semi
27bf414c
JM
2107
2108 specifier-qualifier-list:
2109 type-specifier specifier-qualifier-list[opt]
2110 type-qualifier specifier-qualifier-list[opt]
2111 attributes specifier-qualifier-list[opt]
2112
2113 struct-declarator-list:
2114 struct-declarator
2115 struct-declarator-list , attributes[opt] struct-declarator
2116
2117 struct-declarator:
2118 declarator attributes[opt]
2119 declarator[opt] : constant-expression attributes[opt]
2120
2121 GNU extensions:
2122
2123 struct-declaration:
2124 __extension__ struct-declaration
2125 specifier-qualifier-list
2126
2127 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2128 of attributes where shown is a GNU extension. In GNU C, we accept
2129 any expression without commas in the syntax (assignment
2130 expressions, not just conditional expressions); assignment
2131 expressions will be diagnosed as non-constant. */
2132
2133static tree
2134c_parser_struct_declaration (c_parser *parser)
2135{
2136 struct c_declspecs *specs;
2137 tree prefix_attrs;
2138 tree all_prefix_attrs;
2139 tree decls;
c7412148 2140 location_t decl_loc;
27bf414c
JM
2141 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2142 {
2143 int ext;
2144 tree decl;
2145 ext = disable_extension_diagnostics ();
2146 c_parser_consume_token (parser);
2147 decl = c_parser_struct_declaration (parser);
2148 restore_extension_diagnostics (ext);
2149 return decl;
2150 }
32912286
JM
2151 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2152 {
2153 c_parser_static_assert_declaration_no_semi (parser);
2154 return NULL_TREE;
2155 }
27bf414c 2156 specs = build_null_declspecs ();
c7412148 2157 decl_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2158 c_parser_declspecs (parser, specs, false, true, true);
2159 if (parser->error)
67c2939d 2160 return NULL_TREE;
27bf414c
JM
2161 if (!specs->declspecs_seen_p)
2162 {
2163 c_parser_error (parser, "expected specifier-qualifier-list");
2164 return NULL_TREE;
2165 }
2166 finish_declspecs (specs);
2167 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2168 {
2169 tree ret;
2170 if (!specs->type_seen_p)
2171 {
b8698a0f 2172 pedwarn (decl_loc, OPT_pedantic,
509c9d60 2173 "ISO C forbids member declarations with no members");
27bf414c
JM
2174 shadow_tag_warned (specs, pedantic);
2175 ret = NULL_TREE;
2176 }
2177 else
2178 {
2179 /* Support for unnamed structs or unions as members of
2180 structs or unions (which is [a] useful and [b] supports
2181 MS P-SDK). */
b9baeecd 2182 tree attrs = NULL;
3d10ed6c
AH
2183
2184 ret = grokfield (c_parser_peek_token (parser)->location,
2185 build_id_declarator (NULL_TREE), specs,
b9baeecd 2186 NULL_TREE, &attrs);
0ad7e054
RS
2187 if (ret)
2188 decl_attributes (&ret, attrs, 0);
27bf414c
JM
2189 }
2190 return ret;
2191 }
2192 pending_xref_error ();
2193 prefix_attrs = specs->attrs;
2194 all_prefix_attrs = prefix_attrs;
2195 specs->attrs = NULL_TREE;
2196 decls = NULL_TREE;
2197 while (true)
2198 {
2199 /* Declaring one or more declarators or un-named bit-fields. */
2200 struct c_declarator *declarator;
2201 bool dummy = false;
2202 if (c_parser_next_token_is (parser, CPP_COLON))
2203 declarator = build_id_declarator (NULL_TREE);
2204 else
2205 declarator = c_parser_declarator (parser, specs->type_seen_p,
2206 C_DTR_NORMAL, &dummy);
2207 if (declarator == NULL)
2208 {
2209 c_parser_skip_to_end_of_block_or_statement (parser);
2210 break;
2211 }
2212 if (c_parser_next_token_is (parser, CPP_COLON)
2213 || c_parser_next_token_is (parser, CPP_COMMA)
2214 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2215 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2216 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2217 {
2218 tree postfix_attrs = NULL_TREE;
2219 tree width = NULL_TREE;
2220 tree d;
2221 if (c_parser_next_token_is (parser, CPP_COLON))
2222 {
2223 c_parser_consume_token (parser);
2224 width = c_parser_expr_no_commas (parser, NULL).value;
2225 }
2226 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2227 postfix_attrs = c_parser_attributes (parser);
3d10ed6c
AH
2228 d = grokfield (c_parser_peek_token (parser)->location,
2229 declarator, specs, width, &all_prefix_attrs);
27bf414c
JM
2230 decl_attributes (&d, chainon (postfix_attrs,
2231 all_prefix_attrs), 0);
2232 TREE_CHAIN (d) = decls;
2233 decls = d;
2234 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2235 all_prefix_attrs = chainon (c_parser_attributes (parser),
2236 prefix_attrs);
2237 else
2238 all_prefix_attrs = prefix_attrs;
2239 if (c_parser_next_token_is (parser, CPP_COMMA))
2240 c_parser_consume_token (parser);
2241 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2242 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2243 {
2244 /* Semicolon consumed in caller. */
2245 break;
2246 }
2247 else
2248 {
2249 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2250 break;
2251 }
2252 }
2253 else
2254 {
2255 c_parser_error (parser,
2256 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2257 "%<__attribute__%>");
2258 break;
2259 }
2260 }
2261 return decls;
2262}
2263
2264/* Parse a typeof specifier (a GNU extension).
2265
2266 typeof-specifier:
2267 typeof ( expression )
2268 typeof ( type-name )
2269*/
2270
2271static struct c_typespec
2272c_parser_typeof_specifier (c_parser *parser)
2273{
2274 struct c_typespec ret;
2275 ret.kind = ctsk_typeof;
2276 ret.spec = error_mark_node;
928c19bb
JM
2277 ret.expr = NULL_TREE;
2278 ret.expr_const_operands = true;
27bf414c
JM
2279 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2280 c_parser_consume_token (parser);
7d882b83 2281 c_inhibit_evaluation_warnings++;
27bf414c
JM
2282 in_typeof++;
2283 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2284 {
7d882b83 2285 c_inhibit_evaluation_warnings--;
27bf414c
JM
2286 in_typeof--;
2287 return ret;
2288 }
2289 if (c_parser_next_token_starts_typename (parser))
2290 {
2291 struct c_type_name *type = c_parser_type_name (parser);
7d882b83 2292 c_inhibit_evaluation_warnings--;
27bf414c
JM
2293 in_typeof--;
2294 if (type != NULL)
2295 {
928c19bb 2296 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
27bf414c
JM
2297 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2298 }
2299 }
2300 else
2301 {
52ffd86e 2302 bool was_vm;
c7412148 2303 location_t here = c_parser_peek_token (parser)->location;
27bf414c 2304 struct c_expr expr = c_parser_expression (parser);
7d882b83 2305 c_inhibit_evaluation_warnings--;
27bf414c
JM
2306 in_typeof--;
2307 if (TREE_CODE (expr.value) == COMPONENT_REF
2308 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 2309 error_at (here, "%<typeof%> applied to a bit-field");
ebfbbdc5 2310 mark_exp_read (expr.value);
27bf414c 2311 ret.spec = TREE_TYPE (expr.value);
52ffd86e 2312 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
928c19bb
JM
2313 /* This is returned with the type so that when the type is
2314 evaluated, this can be evaluated. */
2315 if (was_vm)
2316 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
52ffd86e 2317 pop_maybe_used (was_vm);
27bf414c
JM
2318 }
2319 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2320 return ret;
2321}
2322
2323/* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2324 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2325 be redeclared; otherwise it may not. KIND indicates which kind of
2326 declarator is wanted. Returns a valid declarator except in the
2327 case of a syntax error in which case NULL is returned. *SEEN_ID is
2328 set to true if an identifier being declared is seen; this is used
2329 to diagnose bad forms of abstract array declarators and to
2330 determine whether an identifier list is syntactically permitted.
2331
2332 declarator:
2333 pointer[opt] direct-declarator
2334
2335 direct-declarator:
2336 identifier
2337 ( attributes[opt] declarator )
2338 direct-declarator array-declarator
2339 direct-declarator ( parameter-type-list )
2340 direct-declarator ( identifier-list[opt] )
2341
2342 pointer:
2343 * type-qualifier-list[opt]
2344 * type-qualifier-list[opt] pointer
2345
2346 type-qualifier-list:
2347 type-qualifier
2348 attributes
2349 type-qualifier-list type-qualifier
2350 type-qualifier-list attributes
2351
2352 parameter-type-list:
2353 parameter-list
2354 parameter-list , ...
2355
2356 parameter-list:
2357 parameter-declaration
2358 parameter-list , parameter-declaration
2359
2360 parameter-declaration:
2361 declaration-specifiers declarator attributes[opt]
2362 declaration-specifiers abstract-declarator[opt] attributes[opt]
2363
2364 identifier-list:
2365 identifier
2366 identifier-list , identifier
2367
2368 abstract-declarator:
2369 pointer
2370 pointer[opt] direct-abstract-declarator
2371
2372 direct-abstract-declarator:
2373 ( attributes[opt] abstract-declarator )
2374 direct-abstract-declarator[opt] array-declarator
2375 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2376
2377 GNU extensions:
2378
2379 direct-declarator:
2380 direct-declarator ( parameter-forward-declarations
2381 parameter-type-list[opt] )
2382
2383 direct-abstract-declarator:
c22cacf3 2384 direct-abstract-declarator[opt] ( parameter-forward-declarations
27bf414c
JM
2385 parameter-type-list[opt] )
2386
2387 parameter-forward-declarations:
2388 parameter-list ;
2389 parameter-forward-declarations parameter-list ;
2390
2391 The uses of attributes shown above are GNU extensions.
2392
2393 Some forms of array declarator are not included in C99 in the
2394 syntax for abstract declarators; these are disallowed elsewhere.
2395 This may be a defect (DR#289).
2396
2397 This function also accepts an omitted abstract declarator as being
2398 an abstract declarator, although not part of the formal syntax. */
2399
2400static struct c_declarator *
2401c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2402 bool *seen_id)
2403{
2404 /* Parse any initial pointer part. */
2405 if (c_parser_next_token_is (parser, CPP_MULT))
2406 {
2407 struct c_declspecs *quals_attrs = build_null_declspecs ();
2408 struct c_declarator *inner;
2409 c_parser_consume_token (parser);
2410 c_parser_declspecs (parser, quals_attrs, false, false, true);
2411 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2412 if (inner == NULL)
2413 return NULL;
2414 else
2415 return make_pointer_declarator (quals_attrs, inner);
2416 }
2417 /* Now we have a direct declarator, direct abstract declarator or
2418 nothing (which counts as a direct abstract declarator here). */
2419 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2420}
2421
2422/* Parse a direct declarator or direct abstract declarator; arguments
2423 as c_parser_declarator. */
2424
2425static struct c_declarator *
2426c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2427 bool *seen_id)
2428{
2429 /* The direct declarator must start with an identifier (possibly
2430 omitted) or a parenthesized declarator (possibly abstract). In
2431 an ordinary declarator, initial parentheses must start a
2432 parenthesized declarator. In an abstract declarator or parameter
2433 declarator, they could start a parenthesized declarator or a
2434 parameter list. To tell which, the open parenthesis and any
2435 following attributes must be read. If a declaration specifier
2436 follows, then it is a parameter list; if the specifier is a
2437 typedef name, there might be an ambiguity about redeclaring it,
2438 which is resolved in the direction of treating it as a typedef
2439 name. If a close parenthesis follows, it is also an empty
2440 parameter list, as the syntax does not permit empty abstract
0fa2e4df 2441 declarators. Otherwise, it is a parenthesized declarator (in
27bf414c
JM
2442 which case the analysis may be repeated inside it, recursively).
2443
2444 ??? There is an ambiguity in a parameter declaration "int
2445 (__attribute__((foo)) x)", where x is not a typedef name: it
2446 could be an abstract declarator for a function, or declare x with
2447 parentheses. The proper resolution of this ambiguity needs
2448 documenting. At present we follow an accident of the old
2449 parser's implementation, whereby the first parameter must have
2450 some declaration specifiers other than just attributes. Thus as
0fa2e4df 2451 a parameter declaration it is treated as a parenthesized
27bf414c
JM
2452 parameter named x, and as an abstract declarator it is
2453 rejected.
2454
2455 ??? Also following the old parser, attributes inside an empty
2456 parameter list are ignored, making it a list not yielding a
2457 prototype, rather than giving an error or making it have one
2458 parameter with implicit type int.
2459
2460 ??? Also following the old parser, typedef names may be
2461 redeclared in declarators, but not Objective-C class names. */
2462
2463 if (kind != C_DTR_ABSTRACT
2464 && c_parser_next_token_is (parser, CPP_NAME)
2465 && ((type_seen_p
2466 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2467 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2468 {
2469 struct c_declarator *inner
2470 = build_id_declarator (c_parser_peek_token (parser)->value);
2471 *seen_id = true;
6037d88d 2472 inner->id_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2473 c_parser_consume_token (parser);
2474 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2475 }
2476
2477 if (kind != C_DTR_NORMAL
2478 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2479 {
2480 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2481 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2482 }
2483
2484 /* Either we are at the end of an abstract declarator, or we have
2485 parentheses. */
2486
2487 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2488 {
2489 tree attrs;
2490 struct c_declarator *inner;
2491 c_parser_consume_token (parser);
2492 attrs = c_parser_attributes (parser);
2493 if (kind != C_DTR_NORMAL
2494 && (c_parser_next_token_starts_declspecs (parser)
2495 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2496 {
2497 struct c_arg_info *args
2498 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2499 attrs);
2500 if (args == NULL)
2501 return NULL;
2502 else
2503 {
2504 inner
2505 = build_function_declarator (args,
2506 build_id_declarator (NULL_TREE));
2507 return c_parser_direct_declarator_inner (parser, *seen_id,
2508 inner);
2509 }
2510 }
2511 /* A parenthesized declarator. */
2512 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2513 if (inner != NULL && attrs != NULL)
2514 inner = build_attrs_declarator (attrs, inner);
2515 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2516 {
2517 c_parser_consume_token (parser);
2518 if (inner == NULL)
2519 return NULL;
2520 else
2521 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2522 }
2523 else
2524 {
2525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2526 "expected %<)%>");
2527 return NULL;
2528 }
2529 }
2530 else
2531 {
2532 if (kind == C_DTR_NORMAL)
2533 {
2534 c_parser_error (parser, "expected identifier or %<(%>");
2535 return NULL;
2536 }
2537 else
2538 return build_id_declarator (NULL_TREE);
2539 }
2540}
2541
2542/* Parse part of a direct declarator or direct abstract declarator,
2543 given that some (in INNER) has already been parsed; ID_PRESENT is
2544 true if an identifier is present, false for an abstract
2545 declarator. */
2546
2547static struct c_declarator *
2548c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2549 struct c_declarator *inner)
2550{
2551 /* Parse a sequence of array declarators and parameter lists. */
2552 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2553 {
b8698a0f 2554 location_t brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
2555 struct c_declarator *declarator;
2556 struct c_declspecs *quals_attrs = build_null_declspecs ();
2557 bool static_seen;
2558 bool star_seen;
2559 tree dimen;
2560 c_parser_consume_token (parser);
2561 c_parser_declspecs (parser, quals_attrs, false, false, true);
2562 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2563 if (static_seen)
2564 c_parser_consume_token (parser);
2565 if (static_seen && !quals_attrs->declspecs_seen_p)
2566 c_parser_declspecs (parser, quals_attrs, false, false, true);
2567 if (!quals_attrs->declspecs_seen_p)
2568 quals_attrs = NULL;
2569 /* If "static" is present, there must be an array dimension.
2570 Otherwise, there may be a dimension, "*", or no
2571 dimension. */
2572 if (static_seen)
2573 {
2574 star_seen = false;
2575 dimen = c_parser_expr_no_commas (parser, NULL).value;
2576 }
2577 else
2578 {
2579 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2580 {
2581 dimen = NULL_TREE;
2582 star_seen = false;
2583 }
2584 else if (c_parser_next_token_is (parser, CPP_MULT))
2585 {
2586 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2587 {
2588 dimen = NULL_TREE;
2589 star_seen = true;
2590 c_parser_consume_token (parser);
2591 }
2592 else
2593 {
2594 star_seen = false;
2595 dimen = c_parser_expr_no_commas (parser, NULL).value;
2596 }
2597 }
2598 else
2599 {
2600 star_seen = false;
2601 dimen = c_parser_expr_no_commas (parser, NULL).value;
2602 }
2603 }
2604 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2605 c_parser_consume_token (parser);
2606 else
2607 {
2608 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2609 "expected %<]%>");
2610 return NULL;
2611 }
97e3c923
JJ
2612 if (dimen)
2613 mark_exp_read (dimen);
c2255bc4
AH
2614 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2615 static_seen, star_seen);
52ffd86e
MS
2616 if (declarator == NULL)
2617 return NULL;
6ac0194d 2618 inner = set_array_declarator_inner (declarator, inner);
27bf414c
JM
2619 return c_parser_direct_declarator_inner (parser, id_present, inner);
2620 }
2621 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2622 {
2623 tree attrs;
2624 struct c_arg_info *args;
2625 c_parser_consume_token (parser);
2626 attrs = c_parser_attributes (parser);
2627 args = c_parser_parms_declarator (parser, id_present, attrs);
2628 if (args == NULL)
2629 return NULL;
2630 else
2631 {
2632 inner = build_function_declarator (args, inner);
2633 return c_parser_direct_declarator_inner (parser, id_present, inner);
2634 }
2635 }
2636 return inner;
2637}
2638
2639/* Parse a parameter list or identifier list, including the closing
2640 parenthesis but not the opening one. ATTRS are the attributes at
2641 the start of the list. ID_LIST_OK is true if an identifier list is
2642 acceptable; such a list must not have attributes at the start. */
2643
2644static struct c_arg_info *
2645c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2646{
2647 push_scope ();
2648 declare_parm_level ();
2649 /* If the list starts with an identifier, it is an identifier list.
2650 Otherwise, it is either a prototype list or an empty list. */
2651 if (id_list_ok
2652 && !attrs
2653 && c_parser_next_token_is (parser, CPP_NAME)
2654 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2655 {
7fa3585c 2656 tree list = NULL_TREE, *nextp = &list;
27bf414c
JM
2657 while (c_parser_next_token_is (parser, CPP_NAME)
2658 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2659 {
7fa3585c
GK
2660 *nextp = build_tree_list (NULL_TREE,
2661 c_parser_peek_token (parser)->value);
2662 nextp = & TREE_CHAIN (*nextp);
27bf414c
JM
2663 c_parser_consume_token (parser);
2664 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2665 break;
2666 c_parser_consume_token (parser);
2667 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2668 {
2669 c_parser_error (parser, "expected identifier");
2670 break;
2671 }
2672 }
2673 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2674 {
2675 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2676 ret->parms = 0;
2677 ret->tags = 0;
2678 ret->types = list;
2679 ret->others = 0;
3542a5c0 2680 ret->pending_sizes = 0;
6a1f8611 2681 ret->had_vla_unspec = 0;
27bf414c
JM
2682 c_parser_consume_token (parser);
2683 pop_scope ();
2684 return ret;
2685 }
2686 else
2687 {
2688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2689 "expected %<)%>");
2690 pop_scope ();
2691 return NULL;
2692 }
2693 }
2694 else
2695 {
2696 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2697 pop_scope ();
2698 return ret;
2699 }
2700}
2701
2702/* Parse a parameter list (possibly empty), including the closing
2703 parenthesis but not the opening one. ATTRS are the attributes at
2704 the start of the list. */
2705
2706static struct c_arg_info *
2707c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2708{
2709 bool good_parm = false;
2710 /* ??? Following the old parser, forward parameter declarations may
2711 use abstract declarators, and if no real parameter declarations
2712 follow the forward declarations then this is not diagnosed. Also
2713 note as above that attributes are ignored as the only contents of
2714 the parentheses, or as the only contents after forward
2715 declarations. */
2716 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2717 {
2718 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2719 ret->parms = 0;
2720 ret->tags = 0;
2721 ret->types = 0;
2722 ret->others = 0;
3542a5c0 2723 ret->pending_sizes = 0;
6a1f8611 2724 ret->had_vla_unspec = 0;
27bf414c
JM
2725 c_parser_consume_token (parser);
2726 return ret;
2727 }
2728 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2729 {
2730 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2731 ret->parms = 0;
2732 ret->tags = 0;
2733 ret->others = 0;
3542a5c0 2734 ret->pending_sizes = 0;
6a1f8611 2735 ret->had_vla_unspec = 0;
27bf414c
JM
2736 /* Suppress -Wold-style-definition for this case. */
2737 ret->types = error_mark_node;
3ba09659
AH
2738 error_at (c_parser_peek_token (parser)->location,
2739 "ISO C requires a named argument before %<...%>");
27bf414c
JM
2740 c_parser_consume_token (parser);
2741 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2742 {
2743 c_parser_consume_token (parser);
2744 return ret;
2745 }
2746 else
2747 {
2748 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2749 "expected %<)%>");
2750 return NULL;
2751 }
2752 }
2753 /* Nonempty list of parameters, either terminated with semicolon
2754 (forward declarations; recurse) or with close parenthesis (normal
2755 function) or with ", ... )" (variadic function). */
2756 while (true)
2757 {
2758 /* Parse a parameter. */
2759 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2760 attrs = NULL_TREE;
2761 if (parm != NULL)
2762 {
2763 good_parm = true;
2764 push_parm_decl (parm);
2765 }
2766 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2767 {
2768 tree new_attrs;
2769 c_parser_consume_token (parser);
91d975b8 2770 mark_forward_parm_decls ();
27bf414c
JM
2771 new_attrs = c_parser_attributes (parser);
2772 return c_parser_parms_list_declarator (parser, new_attrs);
2773 }
2774 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2775 {
2776 c_parser_consume_token (parser);
2777 if (good_parm)
2778 return get_parm_info (false);
2779 else
2780 {
2781 struct c_arg_info *ret
2782 = XOBNEW (&parser_obstack, struct c_arg_info);
2783 ret->parms = 0;
2784 ret->tags = 0;
2785 ret->types = 0;
2786 ret->others = 0;
3542a5c0 2787 ret->pending_sizes = 0;
6a1f8611 2788 ret->had_vla_unspec = 0;
27bf414c
JM
2789 return ret;
2790 }
2791 }
2792 if (!c_parser_require (parser, CPP_COMMA,
2793 "expected %<;%>, %<,%> or %<)%>"))
2794 {
2795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
f007042e 2796 get_pending_sizes ();
27bf414c
JM
2797 return NULL;
2798 }
2799 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2800 {
2801 c_parser_consume_token (parser);
2802 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2803 {
2804 c_parser_consume_token (parser);
2805 if (good_parm)
2806 return get_parm_info (true);
2807 else
2808 {
2809 struct c_arg_info *ret
2810 = XOBNEW (&parser_obstack, struct c_arg_info);
2811 ret->parms = 0;
2812 ret->tags = 0;
2813 ret->types = 0;
2814 ret->others = 0;
3542a5c0 2815 ret->pending_sizes = 0;
6a1f8611 2816 ret->had_vla_unspec = 0;
27bf414c
JM
2817 return ret;
2818 }
2819 }
2820 else
2821 {
2822 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2823 "expected %<)%>");
f007042e 2824 get_pending_sizes ();
27bf414c
JM
2825 return NULL;
2826 }
2827 }
2828 }
2829}
2830
2831/* Parse a parameter declaration. ATTRS are the attributes at the
2832 start of the declaration if it is the first parameter. */
2833
2834static struct c_parm *
2835c_parser_parameter_declaration (c_parser *parser, tree attrs)
2836{
2837 struct c_declspecs *specs;
2838 struct c_declarator *declarator;
2839 tree prefix_attrs;
2840 tree postfix_attrs = NULL_TREE;
2841 bool dummy = false;
2842 if (!c_parser_next_token_starts_declspecs (parser))
2843 {
2844 /* ??? In some Objective-C cases '...' isn't applicable so there
2845 should be a different message. */
2846 c_parser_error (parser,
2847 "expected declaration specifiers or %<...%>");
2848 c_parser_skip_to_end_of_parameter (parser);
2849 return NULL;
2850 }
2851 specs = build_null_declspecs ();
2852 if (attrs)
2853 {
2854 declspecs_add_attrs (specs, attrs);
2855 attrs = NULL_TREE;
2856 }
2857 c_parser_declspecs (parser, specs, true, true, true);
2858 finish_declspecs (specs);
2859 pending_xref_error ();
2860 prefix_attrs = specs->attrs;
2861 specs->attrs = NULL_TREE;
2862 declarator = c_parser_declarator (parser, specs->type_seen_p,
2863 C_DTR_PARM, &dummy);
2864 if (declarator == NULL)
2865 {
2866 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2867 return NULL;
2868 }
2869 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2870 postfix_attrs = c_parser_attributes (parser);
2871 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2872 declarator);
2873}
2874
2875/* Parse a string literal in an asm expression. It should not be
2876 translated, and wide string literals are an error although
2877 permitted by the syntax. This is a GNU extension.
2878
2879 asm-string-literal:
2880 string-literal
2881
2882 ??? At present, following the old parser, the caller needs to have
46c2514e
TT
2883 set lex_untranslated_string to 1. It would be better to follow the
2884 C++ parser rather than using this kludge. */
27bf414c
JM
2885
2886static tree
2887c_parser_asm_string_literal (c_parser *parser)
2888{
2889 tree str;
2890 if (c_parser_next_token_is (parser, CPP_STRING))
2891 {
2892 str = c_parser_peek_token (parser)->value;
2893 c_parser_consume_token (parser);
2894 }
2895 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2896 {
3ba09659
AH
2897 error_at (c_parser_peek_token (parser)->location,
2898 "wide string literal in %<asm%>");
27bf414c
JM
2899 str = build_string (1, "");
2900 c_parser_consume_token (parser);
2901 }
2902 else
2903 {
2904 c_parser_error (parser, "expected string literal");
2905 str = NULL_TREE;
2906 }
2907 return str;
2908}
2909
2910/* Parse a simple asm expression. This is used in restricted
2911 contexts, where a full expression with inputs and outputs does not
2912 make sense. This is a GNU extension.
2913
2914 simple-asm-expr:
2915 asm ( asm-string-literal )
2916*/
2917
2918static tree
2919c_parser_simple_asm_expr (c_parser *parser)
2920{
2921 tree str;
2922 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2923 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
2924 lex_untranslated_string kludge. */
2925 parser->lex_untranslated_string = true;
27bf414c
JM
2926 c_parser_consume_token (parser);
2927 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2928 {
46c2514e 2929 parser->lex_untranslated_string = false;
27bf414c
JM
2930 return NULL_TREE;
2931 }
2932 str = c_parser_asm_string_literal (parser);
46c2514e 2933 parser->lex_untranslated_string = false;
27bf414c
JM
2934 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2935 {
2936 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2937 return NULL_TREE;
2938 }
2939 return str;
2940}
2941
2942/* Parse (possibly empty) attributes. This is a GNU extension.
2943
2944 attributes:
2945 empty
2946 attributes attribute
2947
2948 attribute:
2949 __attribute__ ( ( attribute-list ) )
2950
2951 attribute-list:
2952 attrib
2953 attribute_list , attrib
2954
2955 attrib:
2956 empty
2957 any-word
2958 any-word ( identifier )
2959 any-word ( identifier , nonempty-expr-list )
2960 any-word ( expr-list )
2961
2962 where the "identifier" must not be declared as a type, and
2963 "any-word" may be any identifier (including one declared as a
2964 type), a reserved word storage class specifier, type specifier or
2965 type qualifier. ??? This still leaves out most reserved keywords
2966 (following the old parser), shouldn't we include them, and why not
2967 allow identifiers declared as types to start the arguments? */
2968
2969static tree
2970c_parser_attributes (c_parser *parser)
2971{
2972 tree attrs = NULL_TREE;
2973 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2974 {
2975 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
2976 lex_untranslated_string kludge. */
2977 parser->lex_untranslated_string = true;
27bf414c
JM
2978 c_parser_consume_token (parser);
2979 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2980 {
46c2514e 2981 parser->lex_untranslated_string = false;
27bf414c
JM
2982 return attrs;
2983 }
2984 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2985 {
46c2514e 2986 parser->lex_untranslated_string = false;
27bf414c
JM
2987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2988 return attrs;
2989 }
2990 /* Parse the attribute list. */
2991 while (c_parser_next_token_is (parser, CPP_COMMA)
2992 || c_parser_next_token_is (parser, CPP_NAME)
2993 || c_parser_next_token_is (parser, CPP_KEYWORD))
2994 {
2995 tree attr, attr_name, attr_args;
bbbbb16a 2996 VEC(tree,gc) *expr_list;
27bf414c
JM
2997 if (c_parser_next_token_is (parser, CPP_COMMA))
2998 {
2999 c_parser_consume_token (parser);
3000 continue;
3001 }
3002 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3003 {
3004 /* ??? See comment above about what keywords are
3005 accepted here. */
3006 bool ok;
3007 switch (c_parser_peek_token (parser)->keyword)
3008 {
3009 case RID_STATIC:
3010 case RID_UNSIGNED:
3011 case RID_LONG:
a6766312 3012 case RID_INT128:
27bf414c
JM
3013 case RID_CONST:
3014 case RID_EXTERN:
3015 case RID_REGISTER:
3016 case RID_TYPEDEF:
3017 case RID_SHORT:
3018 case RID_INLINE:
3019 case RID_VOLATILE:
3020 case RID_SIGNED:
3021 case RID_AUTO:
3022 case RID_RESTRICT:
3023 case RID_COMPLEX:
3024 case RID_THREAD:
3025 case RID_INT:
3026 case RID_CHAR:
3027 case RID_FLOAT:
3028 case RID_DOUBLE:
3029 case RID_VOID:
9a8ce21f
JG
3030 case RID_DFLOAT32:
3031 case RID_DFLOAT64:
3032 case RID_DFLOAT128:
27bf414c 3033 case RID_BOOL:
ab22c1fa
CF
3034 case RID_FRACT:
3035 case RID_ACCUM:
3036 case RID_SAT:
27bf414c
JM
3037 ok = true;
3038 break;
3039 default:
3040 ok = false;
3041 break;
3042 }
3043 if (!ok)
3044 break;
5dc4a7f4
JJ
3045 /* Accept __attribute__((__const)) as __attribute__((const))
3046 etc. */
3047 attr_name
3048 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
27bf414c 3049 }
5dc4a7f4
JJ
3050 else
3051 attr_name = c_parser_peek_token (parser)->value;
27bf414c
JM
3052 c_parser_consume_token (parser);
3053 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3054 {
3055 attr = build_tree_list (attr_name, NULL_TREE);
3056 attrs = chainon (attrs, attr);
3057 continue;
3058 }
3059 c_parser_consume_token (parser);
3060 /* Parse the attribute contents. If they start with an
3061 identifier which is followed by a comma or close
3062 parenthesis, then the arguments start with that
3063 identifier; otherwise they are an expression list. */
3064 if (c_parser_next_token_is (parser, CPP_NAME)
3065 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3066 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3067 || (c_parser_peek_2nd_token (parser)->type
3068 == CPP_CLOSE_PAREN)))
3069 {
3070 tree arg1 = c_parser_peek_token (parser)->value;
3071 c_parser_consume_token (parser);
3072 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3073 attr_args = build_tree_list (NULL_TREE, arg1);
3074 else
3075 {
bbbbb16a 3076 tree tree_list;
27bf414c 3077 c_parser_consume_token (parser);
bbbbb16a 3078 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898 3079 tree_list = build_tree_list_vec (expr_list);
bbbbb16a 3080 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
c166b898 3081 release_tree_vector (expr_list);
27bf414c
JM
3082 }
3083 }
3084 else
3085 {
3086 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3087 attr_args = NULL_TREE;
3088 else
bbbbb16a
ILT
3089 {
3090 expr_list = c_parser_expr_list (parser, false, true, NULL);
c166b898
ILT
3091 attr_args = build_tree_list_vec (expr_list);
3092 release_tree_vector (expr_list);
bbbbb16a 3093 }
27bf414c
JM
3094 }
3095 attr = build_tree_list (attr_name, attr_args);
3096 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3097 c_parser_consume_token (parser);
3098 else
3099 {
46c2514e 3100 parser->lex_untranslated_string = false;
27bf414c
JM
3101 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3102 "expected %<)%>");
3103 return attrs;
3104 }
3105 attrs = chainon (attrs, attr);
3106 }
3107 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3108 c_parser_consume_token (parser);
3109 else
3110 {
46c2514e 3111 parser->lex_untranslated_string = false;
27bf414c
JM
3112 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3113 "expected %<)%>");
3114 return attrs;
3115 }
3116 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3117 c_parser_consume_token (parser);
3118 else
3119 {
46c2514e 3120 parser->lex_untranslated_string = false;
27bf414c
JM
3121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3122 "expected %<)%>");
3123 return attrs;
3124 }
46c2514e 3125 parser->lex_untranslated_string = false;
27bf414c
JM
3126 }
3127 return attrs;
3128}
3129
3130/* Parse a type name (C90 6.5.5, C99 6.7.6).
3131
3132 type-name:
3133 specifier-qualifier-list abstract-declarator[opt]
3134*/
3135
3136static struct c_type_name *
3137c_parser_type_name (c_parser *parser)
3138{
3139 struct c_declspecs *specs = build_null_declspecs ();
3140 struct c_declarator *declarator;
3141 struct c_type_name *ret;
3142 bool dummy = false;
3143 c_parser_declspecs (parser, specs, false, true, true);
3144 if (!specs->declspecs_seen_p)
3145 {
3146 c_parser_error (parser, "expected specifier-qualifier-list");
3147 return NULL;
3148 }
3149 pending_xref_error ();
3150 finish_declspecs (specs);
3151 declarator = c_parser_declarator (parser, specs->type_seen_p,
3152 C_DTR_ABSTRACT, &dummy);
3153 if (declarator == NULL)
3154 return NULL;
3155 ret = XOBNEW (&parser_obstack, struct c_type_name);
3156 ret->specs = specs;
3157 ret->declarator = declarator;
3158 return ret;
3159}
3160
3161/* Parse an initializer (C90 6.5.7, C99 6.7.8).
3162
3163 initializer:
3164 assignment-expression
3165 { initializer-list }
3166 { initializer-list , }
3167
3168 initializer-list:
3169 designation[opt] initializer
3170 initializer-list , designation[opt] initializer
3171
3172 designation:
3173 designator-list =
3174
3175 designator-list:
3176 designator
3177 designator-list designator
3178
3179 designator:
3180 array-designator
3181 . identifier
3182
3183 array-designator:
3184 [ constant-expression ]
3185
3186 GNU extensions:
3187
3188 initializer:
3189 { }
3190
3191 designation:
3192 array-designator
3193 identifier :
3194
3195 array-designator:
3196 [ constant-expression ... constant-expression ]
3197
3198 Any expression without commas is accepted in the syntax for the
3199 constant-expressions, with non-constant expressions rejected later.
3200
3201 This function is only used for top-level initializers; for nested
3202 ones, see c_parser_initval. */
3203
3204static struct c_expr
3205c_parser_initializer (c_parser *parser)
3206{
3207 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3208 return c_parser_braced_init (parser, NULL_TREE, false);
3209 else
46bdb9cf
JM
3210 {
3211 struct c_expr ret;
c2255bc4 3212 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3213 ret = c_parser_expr_no_commas (parser, NULL);
3214 if (TREE_CODE (ret.value) != STRING_CST
3215 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
ebfbbdc5 3216 ret = default_function_array_read_conversion (loc, ret);
46bdb9cf
JM
3217 return ret;
3218 }
27bf414c
JM
3219}
3220
3221/* Parse a braced initializer list. TYPE is the type specified for a
3222 compound literal, and NULL_TREE for other initializers and for
3223 nested braced lists. NESTED_P is true for nested braced lists,
3224 false for the list of a compound literal or the list that is the
3225 top-level initializer in a declaration. */
3226
3227static struct c_expr
3228c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3229{
a1e3b3d9
LB
3230 struct c_expr ret;
3231 struct obstack braced_init_obstack;
c7412148 3232 location_t brace_loc = c_parser_peek_token (parser)->location;
a1e3b3d9 3233 gcc_obstack_init (&braced_init_obstack);
27bf414c
JM
3234 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3235 c_parser_consume_token (parser);
3236 if (nested_p)
a1e3b3d9 3237 push_init_level (0, &braced_init_obstack);
27bf414c
JM
3238 else
3239 really_start_incremental_init (type);
3240 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3241 {
509c9d60 3242 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
27bf414c
JM
3243 }
3244 else
3245 {
3246 /* Parse a non-empty initializer list, possibly with a trailing
3247 comma. */
3248 while (true)
3249 {
a1e3b3d9 3250 c_parser_initelt (parser, &braced_init_obstack);
27bf414c
JM
3251 if (parser->error)
3252 break;
3253 if (c_parser_next_token_is (parser, CPP_COMMA))
3254 c_parser_consume_token (parser);
3255 else
3256 break;
3257 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3258 break;
3259 }
3260 }
3261 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3262 {
27bf414c
JM
3263 ret.value = error_mark_node;
3264 ret.original_code = ERROR_MARK;
6866c6e8 3265 ret.original_type = NULL;
27bf414c 3266 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
a1e3b3d9
LB
3267 pop_init_level (0, &braced_init_obstack);
3268 obstack_free (&braced_init_obstack, NULL);
27bf414c
JM
3269 return ret;
3270 }
3271 c_parser_consume_token (parser);
a1e3b3d9
LB
3272 ret = pop_init_level (0, &braced_init_obstack);
3273 obstack_free (&braced_init_obstack, NULL);
3274 return ret;
27bf414c
JM
3275}
3276
3277/* Parse a nested initializer, including designators. */
3278
3279static void
a1e3b3d9 3280c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
27bf414c
JM
3281{
3282 /* Parse any designator or designator list. A single array
3283 designator may have the subsequent "=" omitted in GNU C, but a
3284 longer list or a structure member designator may not. */
3285 if (c_parser_next_token_is (parser, CPP_NAME)
3286 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3287 {
3288 /* Old-style structure member designator. */
a1e3b3d9
LB
3289 set_init_label (c_parser_peek_token (parser)->value,
3290 braced_init_obstack);
fcf73884 3291 /* Use the colon as the error location. */
b8698a0f 3292 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
509c9d60 3293 "obsolete use of designated initializer with %<:%>");
27bf414c
JM
3294 c_parser_consume_token (parser);
3295 c_parser_consume_token (parser);
3296 }
3297 else
3298 {
3299 /* des_seen is 0 if there have been no designators, 1 if there
3300 has been a single array designator and 2 otherwise. */
3301 int des_seen = 0;
c7412148 3302 /* Location of a designator. */
922f2908 3303 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3304 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3305 || c_parser_next_token_is (parser, CPP_DOT))
3306 {
3307 int des_prev = des_seen;
c7412148
TT
3308 if (!des_seen)
3309 des_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3310 if (des_seen < 2)
3311 des_seen++;
3312 if (c_parser_next_token_is (parser, CPP_DOT))
3313 {
3314 des_seen = 2;
3315 c_parser_consume_token (parser);
3316 if (c_parser_next_token_is (parser, CPP_NAME))
3317 {
a1e3b3d9
LB
3318 set_init_label (c_parser_peek_token (parser)->value,
3319 braced_init_obstack);
27bf414c
JM
3320 c_parser_consume_token (parser);
3321 }
3322 else
3323 {
3324 struct c_expr init;
3325 init.value = error_mark_node;
3326 init.original_code = ERROR_MARK;
6866c6e8 3327 init.original_type = NULL;
27bf414c
JM
3328 c_parser_error (parser, "expected identifier");
3329 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
a1e3b3d9 3330 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
3331 return;
3332 }
3333 }
3334 else
3335 {
3336 tree first, second;
922f2908 3337 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3338 /* ??? Following the old parser, [ objc-receiver
3339 objc-message-args ] is accepted as an initializer,
3340 being distinguished from a designator by what follows
3341 the first assignment expression inside the square
3342 brackets, but after a first array designator a
3343 subsequent square bracket is for Objective-C taken to
3344 start an expression, using the obsolete form of
3345 designated initializer without '=', rather than
3346 possibly being a second level of designation: in LALR
3347 terms, the '[' is shifted rather than reducing
3348 designator to designator-list. */
3349 if (des_prev == 1 && c_dialect_objc ())
3350 {
3351 des_seen = des_prev;
3352 break;
3353 }
3354 if (des_prev == 0 && c_dialect_objc ())
3355 {
3356 /* This might be an array designator or an
3357 Objective-C message expression. If the former,
3358 continue parsing here; if the latter, parse the
3359 remainder of the initializer given the starting
3360 primary-expression. ??? It might make sense to
3361 distinguish when des_prev == 1 as well; see
3362 previous comment. */
3363 tree rec, args;
3364 struct c_expr mexpr;
3365 c_parser_consume_token (parser);
3366 if (c_parser_peek_token (parser)->type == CPP_NAME
3367 && ((c_parser_peek_token (parser)->id_kind
3368 == C_ID_TYPENAME)
3369 || (c_parser_peek_token (parser)->id_kind
3370 == C_ID_CLASSNAME)))
3371 {
3372 /* Type name receiver. */
3373 tree id = c_parser_peek_token (parser)->value;
3374 c_parser_consume_token (parser);
3375 rec = objc_get_class_reference (id);
3376 goto parse_message_args;
3377 }
3378 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 3379 mark_exp_read (first);
27bf414c
JM
3380 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3381 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3382 goto array_desig_after_first;
3383 /* Expression receiver. So far only one part
3384 without commas has been parsed; there might be
3385 more of the expression. */
3386 rec = first;
3387 while (c_parser_next_token_is (parser, CPP_COMMA))
3388 {
f2a71bbc 3389 struct c_expr next;
c2255bc4
AH
3390 location_t comma_loc, exp_loc;
3391 comma_loc = c_parser_peek_token (parser)->location;
27bf414c 3392 c_parser_consume_token (parser);
c2255bc4 3393 exp_loc = c_parser_peek_token (parser)->location;
f2a71bbc 3394 next = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5
JJ
3395 next = default_function_array_read_conversion (exp_loc,
3396 next);
c2255bc4 3397 rec = build_compound_expr (comma_loc, rec, next.value);
27bf414c
JM
3398 }
3399 parse_message_args:
3400 /* Now parse the objc-message-args. */
3401 args = c_parser_objc_message_args (parser);
3402 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3403 "expected %<]%>");
3404 mexpr.value
3405 = objc_build_message_expr (build_tree_list (rec, args));
3406 mexpr.original_code = ERROR_MARK;
6866c6e8 3407 mexpr.original_type = NULL;
27bf414c
JM
3408 /* Now parse and process the remainder of the
3409 initializer, starting with this message
3410 expression as a primary-expression. */
a1e3b3d9 3411 c_parser_initval (parser, &mexpr, braced_init_obstack);
27bf414c
JM
3412 return;
3413 }
3414 c_parser_consume_token (parser);
3415 first = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 3416 mark_exp_read (first);
27bf414c
JM
3417 array_desig_after_first:
3418 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3419 {
c7412148 3420 ellipsis_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3421 c_parser_consume_token (parser);
3422 second = c_parser_expr_no_commas (parser, NULL).value;
ebfbbdc5 3423 mark_exp_read (second);
27bf414c
JM
3424 }
3425 else
3426 second = NULL_TREE;
3427 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3428 {
3429 c_parser_consume_token (parser);
a1e3b3d9 3430 set_init_index (first, second, braced_init_obstack);
fcf73884 3431 if (second)
b8698a0f 3432 pedwarn (ellipsis_loc, OPT_pedantic,
509c9d60 3433 "ISO C forbids specifying range of elements to initialize");
27bf414c
JM
3434 }
3435 else
3436 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3437 "expected %<]%>");
3438 }
3439 }
3440 if (des_seen >= 1)
3441 {
3442 if (c_parser_next_token_is (parser, CPP_EQ))
3443 {
fcf73884 3444 if (!flag_isoc99)
b8698a0f 3445 pedwarn (des_loc, OPT_pedantic,
509c9d60 3446 "ISO C90 forbids specifying subobject to initialize");
27bf414c
JM
3447 c_parser_consume_token (parser);
3448 }
3449 else
3450 {
3451 if (des_seen == 1)
b8698a0f 3452 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 3453 "obsolete use of designated initializer without %<=%>");
27bf414c
JM
3454 else
3455 {
3456 struct c_expr init;
3457 init.value = error_mark_node;
3458 init.original_code = ERROR_MARK;
6866c6e8 3459 init.original_type = NULL;
27bf414c
JM
3460 c_parser_error (parser, "expected %<=%>");
3461 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
a1e3b3d9 3462 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
3463 return;
3464 }
3465 }
3466 }
3467 }
a1e3b3d9 3468 c_parser_initval (parser, NULL, braced_init_obstack);
27bf414c
JM
3469}
3470
3471/* Parse a nested initializer; as c_parser_initializer but parses
3472 initializers within braced lists, after any designators have been
3473 applied. If AFTER is not NULL then it is an Objective-C message
3474 expression which is the primary-expression starting the
3475 initializer. */
3476
3477static void
a1e3b3d9
LB
3478c_parser_initval (c_parser *parser, struct c_expr *after,
3479 struct obstack * braced_init_obstack)
27bf414c
JM
3480{
3481 struct c_expr init;
3482 gcc_assert (!after || c_dialect_objc ());
3483 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3484 init = c_parser_braced_init (parser, NULL_TREE, true);
3485 else
46bdb9cf 3486 {
c2255bc4 3487 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf
JM
3488 init = c_parser_expr_no_commas (parser, after);
3489 if (init.value != NULL_TREE
3490 && TREE_CODE (init.value) != STRING_CST
3491 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
ebfbbdc5 3492 init = default_function_array_read_conversion (loc, init);
46bdb9cf 3493 }
a1e3b3d9 3494 process_init_element (init, false, braced_init_obstack);
27bf414c
JM
3495}
3496
3497/* Parse a compound statement (possibly a function body) (C90 6.6.2,
3498 C99 6.8.2).
3499
3500 compound-statement:
3501 { block-item-list[opt] }
3502 { label-declarations block-item-list }
3503
3504 block-item-list:
3505 block-item
3506 block-item-list block-item
3507
3508 block-item:
3509 nested-declaration
3510 statement
3511
3512 nested-declaration:
3513 declaration
3514
3515 GNU extensions:
3516
3517 compound-statement:
3518 { label-declarations block-item-list }
3519
3520 nested-declaration:
3521 __extension__ nested-declaration
3522 nested-function-definition
3523
3524 label-declarations:
3525 label-declaration
3526 label-declarations label-declaration
3527
3528 label-declaration:
3529 __label__ identifier-list ;
3530
3531 Allowing the mixing of declarations and code is new in C99. The
3532 GNU syntax also permits (not shown above) labels at the end of
3533 compound statements, which yield an error. We don't allow labels
3534 on declarations; this might seem like a natural extension, but
3535 there would be a conflict between attributes on the label and
3536 prefix attributes on the declaration. ??? The syntax follows the
3537 old parser in requiring something after label declarations.
3538 Although they are erroneous if the labels declared aren't defined,
953ff289 3539 is it useful for the syntax to be this way?
b8698a0f 3540
953ff289 3541 OpenMP:
b8698a0f 3542
953ff289
DN
3543 block-item:
3544 openmp-directive
3545
3546 openmp-directive:
3547 barrier-directive
3548 flush-directive */
27bf414c
JM
3549
3550static tree
3551c_parser_compound_statement (c_parser *parser)
3552{
3553 tree stmt;
c2255bc4
AH
3554 location_t brace_loc;
3555 brace_loc = c_parser_peek_token (parser)->location;
27bf414c 3556 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5600f233
JM
3557 {
3558 /* Ensure a scope is entered and left anyway to avoid confusion
3559 if we have just prepared to enter a function body. */
3560 stmt = c_begin_compound_stmt (true);
c2255bc4 3561 c_end_compound_stmt (brace_loc, stmt, true);
5600f233
JM
3562 return error_mark_node;
3563 }
27bf414c
JM
3564 stmt = c_begin_compound_stmt (true);
3565 c_parser_compound_statement_nostart (parser);
c2255bc4 3566 return c_end_compound_stmt (brace_loc, stmt, true);
27bf414c
JM
3567}
3568
3569/* Parse a compound statement except for the opening brace. This is
3570 used for parsing both compound statements and statement expressions
3571 (which follow different paths to handling the opening). */
3572
3573static void
3574c_parser_compound_statement_nostart (c_parser *parser)
3575{
3576 bool last_stmt = false;
3577 bool last_label = false;
6ec637a4 3578 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3ba09659 3579 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
3580 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3581 {
3582 c_parser_consume_token (parser);
3583 return;
3584 }
6ec637a4 3585 mark_valid_location_for_stdc_pragma (true);
27bf414c
JM
3586 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3587 {
3588 /* Read zero or more forward-declarations for labels that nested
3589 functions can jump to. */
6ec637a4 3590 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3591 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3592 {
c2255bc4 3593 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3594 c_parser_consume_token (parser);
3595 /* Any identifiers, including those declared as type names,
3596 are OK here. */
3597 while (true)
3598 {
3599 tree label;
3600 if (c_parser_next_token_is_not (parser, CPP_NAME))
3601 {
3602 c_parser_error (parser, "expected identifier");
3603 break;
3604 }
3605 label
3606 = declare_label (c_parser_peek_token (parser)->value);
3607 C_DECLARED_LABEL_FLAG (label) = 1;
c2255bc4 3608 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
27bf414c
JM
3609 c_parser_consume_token (parser);
3610 if (c_parser_next_token_is (parser, CPP_COMMA))
3611 c_parser_consume_token (parser);
3612 else
3613 break;
3614 }
3615 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3616 }
c2255bc4 3617 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
27bf414c
JM
3618 }
3619 /* We must now have at least one statement, label or declaration. */
3620 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3621 {
6ec637a4 3622 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3623 c_parser_error (parser, "expected declaration or statement");
3624 c_parser_consume_token (parser);
3625 return;
3626 }
3627 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3628 {
3629 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3630 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3631 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3632 || (c_parser_next_token_is (parser, CPP_NAME)
3633 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3634 {
c7412148
TT
3635 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3636 label_loc = c_parser_peek_2nd_token (parser)->location;
3637 else
3638 label_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
3639 last_label = true;
3640 last_stmt = false;
6ec637a4 3641 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3642 c_parser_label (parser);
3643 }
3644 else if (!last_label
32912286 3645 && c_parser_next_token_starts_declaration (parser))
27bf414c
JM
3646 {
3647 last_label = false;
6ec637a4 3648 mark_valid_location_for_stdc_pragma (false);
32912286 3649 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
fcf73884 3650 if (last_stmt)
b8698a0f 3651 pedwarn_c90 (loc,
509c9d60 3652 (pedantic && !flag_isoc99)
fcf73884
MLI
3653 ? OPT_pedantic
3654 : OPT_Wdeclaration_after_statement,
509c9d60 3655 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3656 last_stmt = false;
3657 }
3658 else if (!last_label
3659 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3660 {
3661 /* __extension__ can start a declaration, but is also an
3662 unary operator that can start an expression. Consume all
3663 but the last of a possible series of __extension__ to
3664 determine which. */
3665 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3666 && (c_parser_peek_2nd_token (parser)->keyword
3667 == RID_EXTENSION))
3668 c_parser_consume_token (parser);
32912286 3669 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
3670 {
3671 int ext;
3672 ext = disable_extension_diagnostics ();
3673 c_parser_consume_token (parser);
3674 last_label = false;
6ec637a4 3675 mark_valid_location_for_stdc_pragma (false);
32912286
JM
3676 c_parser_declaration_or_fndef (parser, true, true, true, true,
3677 true);
27bf414c
JM
3678 /* Following the old parser, __extension__ does not
3679 disable this diagnostic. */
3680 restore_extension_diagnostics (ext);
fcf73884 3681 if (last_stmt)
509c9d60 3682 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
fcf73884
MLI
3683 ? OPT_pedantic
3684 : OPT_Wdeclaration_after_statement,
509c9d60 3685 "ISO C90 forbids mixed declarations and code");
27bf414c
JM
3686 last_stmt = false;
3687 }
3688 else
3689 goto statement;
3690 }
bc4071dd
RH
3691 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3692 {
3693 /* External pragmas, and some omp pragmas, are not associated
3694 with regular c code, and so are not to be considered statements
3695 syntactically. This ensures that the user doesn't put them
3696 places that would turn into syntax errors if the directive
3697 were ignored. */
3698 if (c_parser_pragma (parser, pragma_compound))
3699 last_label = false, last_stmt = true;
3700 }
3701 else if (c_parser_next_token_is (parser, CPP_EOF))
3702 {
6ec637a4 3703 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
bc4071dd
RH
3704 c_parser_error (parser, "expected declaration or statement");
3705 return;
3706 }
b4b56033
MLI
3707 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3708 {
b8698a0f 3709 if (parser->in_if_block)
b4b56033 3710 {
6ec637a4 3711 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3ba09659 3712 error_at (loc, """expected %<}%> before %<else%>");
b4b56033
MLI
3713 return;
3714 }
b8698a0f 3715 else
b4b56033 3716 {
3ba09659 3717 error_at (loc, "%<else%> without a previous %<if%>");
b4b56033
MLI
3718 c_parser_consume_token (parser);
3719 continue;
3720 }
3721 }
27bf414c
JM
3722 else
3723 {
3724 statement:
3725 last_label = false;
3726 last_stmt = true;
6ec637a4 3727 mark_valid_location_for_stdc_pragma (false);
27bf414c
JM
3728 c_parser_statement_after_labels (parser);
3729 }
2c14ae9a
VR
3730
3731 parser->error = false;
27bf414c
JM
3732 }
3733 if (last_label)
3ba09659 3734 error_at (label_loc, "label at end of compound statement");
27bf414c 3735 c_parser_consume_token (parser);
6ec637a4
JJ
3736 /* Restore the value we started with. */
3737 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
27bf414c
JM
3738}
3739
3740/* Parse a label (C90 6.6.1, C99 6.8.1).
3741
3742 label:
3743 identifier : attributes[opt]
3744 case constant-expression :
3745 default :
3746
3747 GNU extensions:
3748
3749 label:
3750 case constant-expression ... constant-expression :
3751
3752 The use of attributes on labels is a GNU extension. The syntax in
3753 GNU C accepts any expressions without commas, non-constant
3754 expressions being rejected later. */
3755
3756static void
3757c_parser_label (c_parser *parser)
3758{
3759 location_t loc1 = c_parser_peek_token (parser)->location;
3760 tree label = NULL_TREE;
3761 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3762 {
3763 tree exp1, exp2;
3764 c_parser_consume_token (parser);
3765 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3766 if (c_parser_next_token_is (parser, CPP_COLON))
3767 {
3768 c_parser_consume_token (parser);
c2255bc4 3769 label = do_case (loc1, exp1, NULL_TREE);
27bf414c
JM
3770 }
3771 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3772 {
3773 c_parser_consume_token (parser);
3774 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3775 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 3776 label = do_case (loc1, exp1, exp2);
27bf414c
JM
3777 }
3778 else
3779 c_parser_error (parser, "expected %<:%> or %<...%>");
3780 }
3781 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3782 {
3783 c_parser_consume_token (parser);
3784 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
c2255bc4 3785 label = do_case (loc1, NULL_TREE, NULL_TREE);
27bf414c
JM
3786 }
3787 else
3788 {
3789 tree name = c_parser_peek_token (parser)->value;
3790 tree tlab;
27bf414c 3791 tree attrs;
c7412148 3792 location_t loc2 = c_parser_peek_token (parser)->location;
27bf414c
JM
3793 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3794 c_parser_consume_token (parser);
3795 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
27bf414c
JM
3796 c_parser_consume_token (parser);
3797 attrs = c_parser_attributes (parser);
3798 tlab = define_label (loc2, name);
3799 if (tlab)
3800 {
3801 decl_attributes (&tlab, attrs, 0);
c2255bc4 3802 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
27bf414c
JM
3803 }
3804 }
3805 if (label)
3d57f0f0 3806 {
32912286 3807 if (c_parser_next_token_starts_declaration (parser)
3d57f0f0
MLI
3808 && !(c_parser_next_token_is (parser, CPP_NAME)
3809 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3810 {
3ba09659
AH
3811 error_at (c_parser_peek_token (parser)->location,
3812 "a label can only be part of a statement and "
3813 "a declaration is not a statement");
b8698a0f 3814 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
32912286 3815 /*static_assert_ok*/ true,
3d57f0f0
MLI
3816 /*nested*/ true, /*empty_ok*/ false,
3817 /*start_attr_ok*/ true);
3818 }
3819 }
27bf414c
JM
3820}
3821
3822/* Parse a statement (C90 6.6, C99 6.8).
3823
3824 statement:
3825 labeled-statement
3826 compound-statement
3827 expression-statement
3828 selection-statement
3829 iteration-statement
3830 jump-statement
3831
3832 labeled-statement:
3833 label statement
3834
3835 expression-statement:
3836 expression[opt] ;
3837
3838 selection-statement:
3839 if-statement
3840 switch-statement
3841
3842 iteration-statement:
3843 while-statement
3844 do-statement
3845 for-statement
3846
3847 jump-statement:
3848 goto identifier ;
3849 continue ;
3850 break ;
3851 return expression[opt] ;
3852
3853 GNU extensions:
3854
3855 statement:
3856 asm-statement
3857
3858 jump-statement:
3859 goto * expression ;
3860
3861 Objective-C:
3862
3863 statement:
3864 objc-throw-statement
3865 objc-try-catch-statement
3866 objc-synchronized-statement
3867
3868 objc-throw-statement:
3869 @throw expression ;
3870 @throw ;
953ff289
DN
3871
3872 OpenMP:
3873
3874 statement:
3875 openmp-construct
3876
3877 openmp-construct:
3878 parallel-construct
3879 for-construct
3880 sections-construct
3881 single-construct
3882 parallel-for-construct
3883 parallel-sections-construct
3884 master-construct
3885 critical-construct
3886 atomic-construct
3887 ordered-construct
3888
3889 parallel-construct:
3890 parallel-directive structured-block
3891
3892 for-construct:
3893 for-directive iteration-statement
3894
3895 sections-construct:
3896 sections-directive section-scope
3897
3898 single-construct:
3899 single-directive structured-block
3900
3901 parallel-for-construct:
3902 parallel-for-directive iteration-statement
3903
3904 parallel-sections-construct:
3905 parallel-sections-directive section-scope
3906
3907 master-construct:
3908 master-directive structured-block
3909
3910 critical-construct:
3911 critical-directive structured-block
3912
3913 atomic-construct:
3914 atomic-directive expression-statement
3915
3916 ordered-construct:
3917 ordered-directive structured-block */
27bf414c
JM
3918
3919static void
3920c_parser_statement (c_parser *parser)
3921{
3922 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3923 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3924 || (c_parser_next_token_is (parser, CPP_NAME)
3925 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3926 c_parser_label (parser);
3927 c_parser_statement_after_labels (parser);
3928}
3929
3930/* Parse a statement, other than a labeled statement. */
3931
3932static void
3933c_parser_statement_after_labels (c_parser *parser)
3934{
3935 location_t loc = c_parser_peek_token (parser)->location;
3936 tree stmt = NULL_TREE;
b4b56033
MLI
3937 bool in_if_block = parser->in_if_block;
3938 parser->in_if_block = false;
27bf414c
JM
3939 switch (c_parser_peek_token (parser)->type)
3940 {
3941 case CPP_OPEN_BRACE:
3942 add_stmt (c_parser_compound_statement (parser));
3943 break;
3944 case CPP_KEYWORD:
3945 switch (c_parser_peek_token (parser)->keyword)
3946 {
3947 case RID_IF:
3948 c_parser_if_statement (parser);
3949 break;
3950 case RID_SWITCH:
3951 c_parser_switch_statement (parser);
3952 break;
3953 case RID_WHILE:
3954 c_parser_while_statement (parser);
3955 break;
3956 case RID_DO:
3957 c_parser_do_statement (parser);
3958 break;
3959 case RID_FOR:
3960 c_parser_for_statement (parser);
3961 break;
3962 case RID_GOTO:
3963 c_parser_consume_token (parser);
3964 if (c_parser_next_token_is (parser, CPP_NAME))
3965 {
c2255bc4
AH
3966 stmt = c_finish_goto_label (loc,
3967 c_parser_peek_token (parser)->value);
27bf414c
JM
3968 c_parser_consume_token (parser);
3969 }
3970 else if (c_parser_next_token_is (parser, CPP_MULT))
3971 {
3972 c_parser_consume_token (parser);
c2255bc4
AH
3973 stmt = c_finish_goto_ptr (loc,
3974 c_parser_expression (parser).value);
27bf414c
JM
3975 }
3976 else
3977 c_parser_error (parser, "expected identifier or %<*%>");
3978 goto expect_semicolon;
3979 case RID_CONTINUE:
3980 c_parser_consume_token (parser);
c2255bc4 3981 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
27bf414c
JM
3982 goto expect_semicolon;
3983 case RID_BREAK:
3984 c_parser_consume_token (parser);
c2255bc4 3985 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
27bf414c
JM
3986 goto expect_semicolon;
3987 case RID_RETURN:
3988 c_parser_consume_token (parser);
3989 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3990 {
c2255bc4 3991 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
27bf414c
JM
3992 c_parser_consume_token (parser);
3993 }
3994 else
3995 {
bbbbb16a 3996 struct c_expr expr = c_parser_expression_conv (parser);
ebfbbdc5 3997 mark_exp_read (expr.value);
c2255bc4 3998 stmt = c_finish_return (loc, expr.value, expr.original_type);
27bf414c
JM
3999 goto expect_semicolon;
4000 }
4001 break;
4002 case RID_ASM:
4003 stmt = c_parser_asm_statement (parser);
4004 break;
eea1139b 4005 case RID_THROW:
27bf414c
JM
4006 gcc_assert (c_dialect_objc ());
4007 c_parser_consume_token (parser);
4008 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4009 {
c2255bc4 4010 stmt = objc_build_throw_stmt (loc, NULL_TREE);
27bf414c
JM
4011 c_parser_consume_token (parser);
4012 }
4013 else
4014 {
928c19bb
JM
4015 tree expr = c_parser_expression (parser).value;
4016 expr = c_fully_fold (expr, false, NULL);
c2255bc4 4017 stmt = objc_build_throw_stmt (loc, expr);
27bf414c
JM
4018 goto expect_semicolon;
4019 }
4020 break;
eea1139b 4021 case RID_TRY:
27bf414c
JM
4022 gcc_assert (c_dialect_objc ());
4023 c_parser_objc_try_catch_statement (parser);
4024 break;
4025 case RID_AT_SYNCHRONIZED:
4026 gcc_assert (c_dialect_objc ());
4027 c_parser_objc_synchronized_statement (parser);
4028 break;
4029 default:
4030 goto expr_stmt;
4031 }
4032 break;
4033 case CPP_SEMICOLON:
4034 c_parser_consume_token (parser);
4035 break;
4036 case CPP_CLOSE_PAREN:
4037 case CPP_CLOSE_SQUARE:
4038 /* Avoid infinite loop in error recovery:
4039 c_parser_skip_until_found stops at a closing nesting
4040 delimiter without consuming it, but here we need to consume
4041 it to proceed further. */
4042 c_parser_error (parser, "expected statement");
4043 c_parser_consume_token (parser);
4044 break;
bc4071dd
RH
4045 case CPP_PRAGMA:
4046 c_parser_pragma (parser, pragma_stmt);
4047 break;
27bf414c
JM
4048 default:
4049 expr_stmt:
c2255bc4 4050 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
27bf414c
JM
4051 expect_semicolon:
4052 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4053 break;
4054 }
4055 /* Two cases cannot and do not have line numbers associated: If stmt
4056 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4057 cannot hold line numbers. But that's OK because the statement
4058 will either be changed to a MODIFY_EXPR during gimplification of
4059 the statement expr, or discarded. If stmt was compound, but
4060 without new variables, we will have skipped the creation of a
4061 BIND and will have a bare STATEMENT_LIST. But that's OK because
4062 (recursively) all of the component statements should already have
4063 line numbers assigned. ??? Can we discard no-op statements
4064 earlier? */
c2255bc4
AH
4065 if (CAN_HAVE_LOCATION_P (stmt)
4066 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4067 SET_EXPR_LOCATION (stmt, loc);
b4b56033
MLI
4068
4069 parser->in_if_block = in_if_block;
27bf414c
JM
4070}
4071
ca085fd7
MLI
4072/* Parse the condition from an if, do, while or for statements. */
4073
4074static tree
4075c_parser_condition (c_parser *parser)
4076{
c2255bc4 4077 location_t loc = c_parser_peek_token (parser)->location;
ca085fd7 4078 tree cond;
928c19bb
JM
4079 cond = c_parser_expression_conv (parser).value;
4080 cond = c_objc_common_truthvalue_conversion (loc, cond);
4081 cond = c_fully_fold (cond, false, NULL);
ca085fd7
MLI
4082 if (warn_sequence_point)
4083 verify_sequence_points (cond);
4084 return cond;
4085}
4086
27bf414c
JM
4087/* Parse a parenthesized condition from an if, do or while statement.
4088
4089 condition:
4090 ( expression )
4091*/
4092static tree
4093c_parser_paren_condition (c_parser *parser)
4094{
27bf414c
JM
4095 tree cond;
4096 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4097 return error_mark_node;
ca085fd7 4098 cond = c_parser_condition (parser);
27bf414c
JM
4099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4100 return cond;
4101}
4102
4103/* Parse a statement which is a block in C99. */
4104
4105static tree
4106c_parser_c99_block_statement (c_parser *parser)
4107{
4108 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 4109 location_t loc = c_parser_peek_token (parser)->location;
27bf414c 4110 c_parser_statement (parser);
c2255bc4 4111 return c_end_compound_stmt (loc, block, flag_isoc99);
27bf414c
JM
4112}
4113
b4b56033
MLI
4114/* Parse the body of an if statement. This is just parsing a
4115 statement but (a) it is a block in C99, (b) we track whether the
4116 body is an if statement for the sake of -Wparentheses warnings, (c)
4117 we handle an empty body specially for the sake of -Wempty-body
4118 warnings, and (d) we call parser_compound_statement directly
4119 because c_parser_statement_after_labels resets
4120 parser->in_if_block. */
27bf414c
JM
4121
4122static tree
4123c_parser_if_body (c_parser *parser, bool *if_p)
4124{
4125 tree block = c_begin_compound_stmt (flag_isoc99);
c2255bc4 4126 location_t body_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4127 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4128 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4129 || (c_parser_next_token_is (parser, CPP_NAME)
4130 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4131 c_parser_label (parser);
4132 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
62e00e94 4133 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
b4b56033 4134 {
626c34b5 4135 location_t loc = c_parser_peek_token (parser)->location;
c2255bc4 4136 add_stmt (build_empty_stmt (loc));
b4b56033 4137 c_parser_consume_token (parser);
626c34b5
PB
4138 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4139 warning_at (loc, OPT_Wempty_body,
4140 "suggest braces around empty body in an %<if%> statement");
b4b56033
MLI
4141 }
4142 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4143 add_stmt (c_parser_compound_statement (parser));
4144 else
4145 c_parser_statement_after_labels (parser);
c2255bc4 4146 return c_end_compound_stmt (body_loc, block, flag_isoc99);
b4b56033
MLI
4147}
4148
4149/* Parse the else body of an if statement. This is just parsing a
4150 statement but (a) it is a block in C99, (b) we handle an empty body
4151 specially for the sake of -Wempty-body warnings. */
4152
4153static tree
4154c_parser_else_body (c_parser *parser)
4155{
c2255bc4 4156 location_t else_loc = c_parser_peek_token (parser)->location;
b4b56033
MLI
4157 tree block = c_begin_compound_stmt (flag_isoc99);
4158 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4159 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4160 || (c_parser_next_token_is (parser, CPP_NAME)
4161 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4162 c_parser_label (parser);
4163 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4164 {
c2255bc4
AH
4165 location_t loc = c_parser_peek_token (parser)->location;
4166 warning_at (loc,
626c34b5
PB
4167 OPT_Wempty_body,
4168 "suggest braces around empty body in an %<else%> statement");
c2255bc4 4169 add_stmt (build_empty_stmt (loc));
b4b56033
MLI
4170 c_parser_consume_token (parser);
4171 }
b8698a0f 4172 else
b4b56033 4173 c_parser_statement_after_labels (parser);
c2255bc4 4174 return c_end_compound_stmt (else_loc, block, flag_isoc99);
27bf414c
JM
4175}
4176
4177/* Parse an if statement (C90 6.6.4, C99 6.8.4).
4178
4179 if-statement:
4180 if ( expression ) statement
4181 if ( expression ) statement else statement
4182*/
4183
4184static void
4185c_parser_if_statement (c_parser *parser)
4186{
4187 tree block;
4188 location_t loc;
4189 tree cond;
b4b56033 4190 bool first_if = false;
27bf414c 4191 tree first_body, second_body;
b4b56033
MLI
4192 bool in_if_block;
4193
27bf414c
JM
4194 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4195 c_parser_consume_token (parser);
4196 block = c_begin_compound_stmt (flag_isoc99);
4197 loc = c_parser_peek_token (parser)->location;
4198 cond = c_parser_paren_condition (parser);
b4b56033
MLI
4199 in_if_block = parser->in_if_block;
4200 parser->in_if_block = true;
27bf414c 4201 first_body = c_parser_if_body (parser, &first_if);
b4b56033 4202 parser->in_if_block = in_if_block;
27bf414c
JM
4203 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4204 {
4205 c_parser_consume_token (parser);
b4b56033 4206 second_body = c_parser_else_body (parser);
27bf414c
JM
4207 }
4208 else
4209 second_body = NULL_TREE;
4210 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
c2255bc4 4211 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4212}
4213
4214/* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4215
4216 switch-statement:
4217 switch (expression) statement
4218*/
4219
4220static void
4221c_parser_switch_statement (c_parser *parser)
4222{
4223 tree block, expr, body, save_break;
c2255bc4
AH
4224 location_t switch_loc = c_parser_peek_token (parser)->location;
4225 location_t switch_cond_loc;
27bf414c
JM
4226 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4227 c_parser_consume_token (parser);
4228 block = c_begin_compound_stmt (flag_isoc99);
4229 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4230 {
c2255bc4 4231 switch_cond_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4232 expr = c_parser_expression (parser).value;
4233 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4234 }
4235 else
c2255bc4
AH
4236 {
4237 switch_cond_loc = UNKNOWN_LOCATION;
4238 expr = error_mark_node;
4239 }
4240 c_start_case (switch_loc, switch_cond_loc, expr);
27bf414c
JM
4241 save_break = c_break_label;
4242 c_break_label = NULL_TREE;
4243 body = c_parser_c99_block_statement (parser);
4244 c_finish_case (body);
4245 if (c_break_label)
c2255bc4
AH
4246 {
4247 location_t here = c_parser_peek_token (parser)->location;
4248 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4249 SET_EXPR_LOCATION (t, here);
4250 add_stmt (t);
4251 }
27bf414c 4252 c_break_label = save_break;
c2255bc4 4253 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
27bf414c
JM
4254}
4255
4256/* Parse a while statement (C90 6.6.5, C99 6.8.5).
4257
4258 while-statement:
4259 while (expression) statement
4260*/
4261
4262static void
4263c_parser_while_statement (c_parser *parser)
4264{
4265 tree block, cond, body, save_break, save_cont;
4266 location_t loc;
4267 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4268 c_parser_consume_token (parser);
4269 block = c_begin_compound_stmt (flag_isoc99);
4270 loc = c_parser_peek_token (parser)->location;
4271 cond = c_parser_paren_condition (parser);
4272 save_break = c_break_label;
4273 c_break_label = NULL_TREE;
4274 save_cont = c_cont_label;
4275 c_cont_label = NULL_TREE;
4276 body = c_parser_c99_block_statement (parser);
4277 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
c2255bc4 4278 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4279 c_break_label = save_break;
4280 c_cont_label = save_cont;
4281}
4282
4283/* Parse a do statement (C90 6.6.5, C99 6.8.5).
4284
4285 do-statement:
4286 do statement while ( expression ) ;
4287*/
4288
4289static void
4290c_parser_do_statement (c_parser *parser)
4291{
4292 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4293 location_t loc;
4294 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4295 c_parser_consume_token (parser);
62e00e94 4296 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3ba09659
AH
4297 warning_at (c_parser_peek_token (parser)->location,
4298 OPT_Wempty_body,
4299 "suggest braces around empty body in %<do%> statement");
27bf414c
JM
4300 block = c_begin_compound_stmt (flag_isoc99);
4301 loc = c_parser_peek_token (parser)->location;
4302 save_break = c_break_label;
4303 c_break_label = NULL_TREE;
4304 save_cont = c_cont_label;
4305 c_cont_label = NULL_TREE;
4306 body = c_parser_c99_block_statement (parser);
4307 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4308 new_break = c_break_label;
4309 c_break_label = save_break;
4310 new_cont = c_cont_label;
4311 c_cont_label = save_cont;
4312 cond = c_parser_paren_condition (parser);
4313 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4314 c_parser_skip_to_end_of_block_or_statement (parser);
4315 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
c2255bc4 4316 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4317}
4318
4319/* Parse a for statement (C90 6.6.5, C99 6.8.5).
4320
4321 for-statement:
4322 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4323 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4324
4325 The form with a declaration is new in C99.
4326
4327 ??? In accordance with the old parser, the declaration may be a
4328 nested function, which is then rejected in check_for_loop_decls,
4329 but does it make any sense for this to be included in the grammar?
4330 Note in particular that the nested function does not include a
4331 trailing ';', whereas the "declaration" production includes one.
4332 Also, can we reject bad declarations earlier and cheaper than
4333 check_for_loop_decls? */
4334
4335static void
4336c_parser_for_statement (c_parser *parser)
4337{
4338 tree block, cond, incr, save_break, save_cont, body;
c2255bc4
AH
4339 location_t loc = c_parser_peek_token (parser)->location;
4340 location_t for_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4341 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4342 c_parser_consume_token (parser);
4343 block = c_begin_compound_stmt (flag_isoc99);
4344 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4345 {
4346 /* Parse the initialization declaration or expression. */
4347 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4348 {
4349 c_parser_consume_token (parser);
c2255bc4 4350 c_finish_expr_stmt (loc, NULL_TREE);
27bf414c 4351 }
32912286 4352 else if (c_parser_next_token_starts_declaration (parser))
27bf414c 4353 {
32912286 4354 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
c2255bc4 4355 check_for_loop_decls (for_loc);
27bf414c
JM
4356 }
4357 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4358 {
4359 /* __extension__ can start a declaration, but is also an
4360 unary operator that can start an expression. Consume all
4361 but the last of a possible series of __extension__ to
4362 determine which. */
4363 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4364 && (c_parser_peek_2nd_token (parser)->keyword
4365 == RID_EXTENSION))
4366 c_parser_consume_token (parser);
32912286 4367 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
27bf414c
JM
4368 {
4369 int ext;
4370 ext = disable_extension_diagnostics ();
4371 c_parser_consume_token (parser);
32912286
JM
4372 c_parser_declaration_or_fndef (parser, true, true, true, true,
4373 true);
27bf414c 4374 restore_extension_diagnostics (ext);
c2255bc4 4375 check_for_loop_decls (for_loc);
27bf414c
JM
4376 }
4377 else
4378 goto init_expr;
4379 }
4380 else
4381 {
4382 init_expr:
c2255bc4 4383 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
27bf414c
JM
4384 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4385 }
4386 /* Parse the loop condition. */
27bf414c
JM
4387 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4388 {
4389 c_parser_consume_token (parser);
4390 cond = NULL_TREE;
4391 }
4392 else
4393 {
ca085fd7 4394 cond = c_parser_condition (parser);
27bf414c
JM
4395 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4396 }
4397 /* Parse the increment expression. */
4398 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
c2255bc4 4399 incr = c_process_expr_stmt (loc, NULL_TREE);
27bf414c 4400 else
c2255bc4 4401 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
27bf414c
JM
4402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4403 }
4404 else
4405 {
4406 cond = error_mark_node;
4407 incr = error_mark_node;
4408 }
4409 save_break = c_break_label;
4410 c_break_label = NULL_TREE;
4411 save_cont = c_cont_label;
4412 c_cont_label = NULL_TREE;
4413 body = c_parser_c99_block_statement (parser);
4414 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
c2255bc4 4415 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
27bf414c
JM
4416 c_break_label = save_break;
4417 c_cont_label = save_cont;
4418}
4419
4420/* Parse an asm statement, a GNU extension. This is a full-blown asm
4421 statement with inputs, outputs, clobbers, and volatile tag
4422 allowed.
4423
4424 asm-statement:
4425 asm type-qualifier[opt] ( asm-argument ) ;
1c384bf1 4426 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
27bf414c
JM
4427
4428 asm-argument:
4429 asm-string-literal
4430 asm-string-literal : asm-operands[opt]
4431 asm-string-literal : asm-operands[opt] : asm-operands[opt]
1c384bf1
RH
4432 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4433
4434 asm-goto-argument:
4435 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4436 : asm-goto-operands
27bf414c
JM
4437
4438 Qualifiers other than volatile are accepted in the syntax but
4439 warned for. */
4440
4441static tree
4442c_parser_asm_statement (c_parser *parser)
4443{
1c384bf1
RH
4444 tree quals, str, outputs, inputs, clobbers, labels, ret;
4445 bool simple, is_goto;
c2255bc4 4446 location_t asm_loc = c_parser_peek_token (parser)->location;
1c384bf1
RH
4447 int section, nsections;
4448
27bf414c
JM
4449 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4450 c_parser_consume_token (parser);
4451 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4452 {
4453 quals = c_parser_peek_token (parser)->value;
4454 c_parser_consume_token (parser);
4455 }
4456 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4457 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4458 {
3ba09659
AH
4459 warning_at (c_parser_peek_token (parser)->location,
4460 0,
4461 "%E qualifier ignored on asm",
4462 c_parser_peek_token (parser)->value);
27bf414c
JM
4463 quals = NULL_TREE;
4464 c_parser_consume_token (parser);
4465 }
4466 else
4467 quals = NULL_TREE;
1c384bf1
RH
4468
4469 is_goto = false;
4470 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4471 {
4472 c_parser_consume_token (parser);
4473 is_goto = true;
4474 }
4475
27bf414c 4476 /* ??? Follow the C++ parser rather than using the
46c2514e
TT
4477 lex_untranslated_string kludge. */
4478 parser->lex_untranslated_string = true;
1c384bf1
RH
4479 ret = NULL;
4480
27bf414c 4481 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1c384bf1
RH
4482 goto error;
4483
27bf414c 4484 str = c_parser_asm_string_literal (parser);
b85eb797 4485 if (str == NULL_TREE)
1c384bf1
RH
4486 goto error_close_paren;
4487
4488 simple = true;
4489 outputs = NULL_TREE;
4490 inputs = NULL_TREE;
4491 clobbers = NULL_TREE;
4492 labels = NULL_TREE;
4493
4494 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4495 goto done_asm;
4496
4497 /* Parse each colon-delimited section of operands. */
4498 nsections = 3 + is_goto;
4499 for (section = 0; section < nsections; ++section)
b85eb797 4500 {
1c384bf1
RH
4501 if (!c_parser_require (parser, CPP_COLON,
4502 is_goto
4503 ? "expected %<:%>"
4504 : "expected %<:%> or %<)%>"))
4505 goto error_close_paren;
4506
4507 /* Once past any colon, we're no longer a simple asm. */
4508 simple = false;
4509
4510 if ((!c_parser_next_token_is (parser, CPP_COLON)
4511 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4512 || section == 3)
4513 switch (section)
4514 {
4515 case 0:
4516 /* For asm goto, we don't allow output operands, but reserve
4517 the slot for a future extension that does allow them. */
4518 if (!is_goto)
4519 outputs = c_parser_asm_operands (parser, false);
4520 break;
4521 case 1:
4522 inputs = c_parser_asm_operands (parser, true);
4523 break;
4524 case 2:
4525 clobbers = c_parser_asm_clobbers (parser);
4526 break;
4527 case 3:
4528 labels = c_parser_asm_goto_operands (parser);
4529 break;
4530 default:
4531 gcc_unreachable ();
4532 }
4533
4534 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4535 goto done_asm;
27bf414c 4536 }
1c384bf1 4537
27bf414c 4538 done_asm:
27bf414c
JM
4539 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4540 {
4541 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1c384bf1 4542 goto error;
27bf414c 4543 }
1c384bf1 4544
27bf414c
JM
4545 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4546 c_parser_skip_to_end_of_block_or_statement (parser);
1c384bf1 4547
c2255bc4 4548 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
1c384bf1
RH
4549 clobbers, labels, simple));
4550
4551 error:
4552 parser->lex_untranslated_string = false;
27bf414c 4553 return ret;
1c384bf1
RH
4554
4555 error_close_paren:
4556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4557 goto error;
27bf414c
JM
4558}
4559
46bdb9cf
JM
4560/* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4561 not outputs), apply the default conversion of functions and arrays
4562 to pointers.
27bf414c
JM
4563
4564 asm-operands:
4565 asm-operand
4566 asm-operands , asm-operand
4567
4568 asm-operand:
4569 asm-string-literal ( expression )
4570 [ identifier ] asm-string-literal ( expression )
4571*/
4572
4573static tree
46bdb9cf 4574c_parser_asm_operands (c_parser *parser, bool convert_p)
27bf414c
JM
4575{
4576 tree list = NULL_TREE;
c2255bc4 4577 location_t loc;
27bf414c
JM
4578 while (true)
4579 {
f2a71bbc
JM
4580 tree name, str;
4581 struct c_expr expr;
27bf414c
JM
4582 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4583 {
4584 c_parser_consume_token (parser);
4585 if (c_parser_next_token_is (parser, CPP_NAME))
4586 {
4587 tree id = c_parser_peek_token (parser)->value;
4588 c_parser_consume_token (parser);
4589 name = build_string (IDENTIFIER_LENGTH (id),
4590 IDENTIFIER_POINTER (id));
4591 }
4592 else
4593 {
4594 c_parser_error (parser, "expected identifier");
4595 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4596 return NULL_TREE;
4597 }
4598 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4599 "expected %<]%>");
4600 }
4601 else
4602 name = NULL_TREE;
4603 str = c_parser_asm_string_literal (parser);
4604 if (str == NULL_TREE)
4605 return NULL_TREE;
46c2514e 4606 parser->lex_untranslated_string = false;
27bf414c
JM
4607 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4608 {
46c2514e 4609 parser->lex_untranslated_string = true;
27bf414c
JM
4610 return NULL_TREE;
4611 }
c2255bc4 4612 loc = c_parser_peek_token (parser)->location;
f2a71bbc 4613 expr = c_parser_expression (parser);
ebfbbdc5 4614 mark_exp_read (expr.value);
46bdb9cf 4615 if (convert_p)
c2255bc4 4616 expr = default_function_array_conversion (loc, expr);
928c19bb 4617 expr.value = c_fully_fold (expr.value, false, NULL);
46c2514e 4618 parser->lex_untranslated_string = true;
27bf414c
JM
4619 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4620 {
4621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4622 return NULL_TREE;
4623 }
4624 list = chainon (list, build_tree_list (build_tree_list (name, str),
f2a71bbc 4625 expr.value));
27bf414c
JM
4626 if (c_parser_next_token_is (parser, CPP_COMMA))
4627 c_parser_consume_token (parser);
4628 else
4629 break;
4630 }
4631 return list;
4632}
4633
4634/* Parse asm clobbers, a GNU extension.
4635
4636 asm-clobbers:
4637 asm-string-literal
4638 asm-clobbers , asm-string-literal
4639*/
4640
4641static tree
4642c_parser_asm_clobbers (c_parser *parser)
4643{
4644 tree list = NULL_TREE;
4645 while (true)
4646 {
4647 tree str = c_parser_asm_string_literal (parser);
4648 if (str)
4649 list = tree_cons (NULL_TREE, str, list);
4650 else
4651 return NULL_TREE;
4652 if (c_parser_next_token_is (parser, CPP_COMMA))
4653 c_parser_consume_token (parser);
4654 else
4655 break;
4656 }
4657 return list;
4658}
4659
1c384bf1 4660/* Parse asm goto labels, a GNU extension.
b8698a0f 4661
1c384bf1
RH
4662 asm-goto-operands:
4663 identifier
4664 asm-goto-operands , identifier
4665*/
4666
4667static tree
4668c_parser_asm_goto_operands (c_parser *parser)
4669{
4670 tree list = NULL_TREE;
4671 while (true)
4672 {
4673 tree name, label;
4674
4675 if (c_parser_next_token_is (parser, CPP_NAME))
4676 {
4677 c_token *tok = c_parser_peek_token (parser);
4678 name = tok->value;
4679 label = lookup_label_for_goto (tok->location, name);
4680 c_parser_consume_token (parser);
4681 TREE_USED (label) = 1;
4682 }
4683 else
4684 {
4685 c_parser_error (parser, "expected identifier");
4686 return NULL_TREE;
4687 }
4688
4689 name = build_string (IDENTIFIER_LENGTH (name),
4690 IDENTIFIER_POINTER (name));
4691 list = tree_cons (name, label, list);
4692 if (c_parser_next_token_is (parser, CPP_COMMA))
4693 c_parser_consume_token (parser);
4694 else
4695 return nreverse (list);
4696 }
4697}
4698
27bf414c
JM
4699/* Parse an expression other than a compound expression; that is, an
4700 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4701 NULL then it is an Objective-C message expression which is the
4702 primary-expression starting the expression as an initializer.
4703
4704 assignment-expression:
4705 conditional-expression
4706 unary-expression assignment-operator assignment-expression
4707
4708 assignment-operator: one of
4709 = *= /= %= += -= <<= >>= &= ^= |=
4710
4711 In GNU C we accept any conditional expression on the LHS and
4712 diagnose the invalid lvalue rather than producing a syntax
4713 error. */
4714
4715static struct c_expr
4716c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4717{
4718 struct c_expr lhs, rhs, ret;
4719 enum tree_code code;
c2255bc4 4720 location_t op_location, exp_location;
27bf414c
JM
4721 gcc_assert (!after || c_dialect_objc ());
4722 lhs = c_parser_conditional_expression (parser, after);
c9f9eb5d 4723 op_location = c_parser_peek_token (parser)->location;
27bf414c
JM
4724 switch (c_parser_peek_token (parser)->type)
4725 {
4726 case CPP_EQ:
4727 code = NOP_EXPR;
4728 break;
4729 case CPP_MULT_EQ:
4730 code = MULT_EXPR;
4731 break;
4732 case CPP_DIV_EQ:
4733 code = TRUNC_DIV_EXPR;
4734 break;
4735 case CPP_MOD_EQ:
4736 code = TRUNC_MOD_EXPR;
4737 break;
4738 case CPP_PLUS_EQ:
4739 code = PLUS_EXPR;
4740 break;
4741 case CPP_MINUS_EQ:
4742 code = MINUS_EXPR;
4743 break;
4744 case CPP_LSHIFT_EQ:
4745 code = LSHIFT_EXPR;
4746 break;
4747 case CPP_RSHIFT_EQ:
4748 code = RSHIFT_EXPR;
4749 break;
4750 case CPP_AND_EQ:
4751 code = BIT_AND_EXPR;
4752 break;
4753 case CPP_XOR_EQ:
4754 code = BIT_XOR_EXPR;
4755 break;
4756 case CPP_OR_EQ:
4757 code = BIT_IOR_EXPR;
4758 break;
4759 default:
4760 return lhs;
4761 }
4762 c_parser_consume_token (parser);
c2255bc4 4763 exp_location = c_parser_peek_token (parser)->location;
27bf414c 4764 rhs = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5 4765 rhs = default_function_array_read_conversion (exp_location, rhs);
32e8bb8e 4766 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
c2255bc4
AH
4767 code, exp_location, rhs.value,
4768 rhs.original_type);
27bf414c
JM
4769 if (code == NOP_EXPR)
4770 ret.original_code = MODIFY_EXPR;
4771 else
4772 {
4773 TREE_NO_WARNING (ret.value) = 1;
4774 ret.original_code = ERROR_MARK;
4775 }
6866c6e8 4776 ret.original_type = NULL;
27bf414c
JM
4777 return ret;
4778}
4779
4780/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4781 is not NULL then it is an Objective-C message expression which is
4782 the primary-expression starting the expression as an initializer.
4783
4784 conditional-expression:
4785 logical-OR-expression
4786 logical-OR-expression ? expression : conditional-expression
4787
4788 GNU extensions:
4789
4790 conditional-expression:
4791 logical-OR-expression ? : conditional-expression
4792*/
4793
4794static struct c_expr
4795c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4796{
4797 struct c_expr cond, exp1, exp2, ret;
744aa42f 4798 location_t cond_loc, colon_loc;
ba47d38d 4799
27bf414c 4800 gcc_assert (!after || c_dialect_objc ());
ba47d38d 4801
27bf414c 4802 cond = c_parser_binary_expression (parser, after);
ba47d38d 4803
27bf414c
JM
4804 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4805 return cond;
c2255bc4 4806 cond_loc = c_parser_peek_token (parser)->location;
ebfbbdc5 4807 cond = default_function_array_read_conversion (cond_loc, cond);
27bf414c
JM
4808 c_parser_consume_token (parser);
4809 if (c_parser_next_token_is (parser, CPP_COLON))
4810 {
8ce94e44 4811 tree eptype = NULL_TREE;
b8698a0f 4812 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 4813 "ISO C forbids omitting the middle term of a ?: expression");
8ce94e44
JM
4814 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4815 {
4816 eptype = TREE_TYPE (cond.value);
4817 cond.value = TREE_OPERAND (cond.value, 0);
4818 }
27bf414c 4819 /* Make sure first operand is calculated only once. */
928c19bb 4820 exp1.value = c_save_expr (default_conversion (cond.value));
8ce94e44
JM
4821 if (eptype)
4822 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6866c6e8 4823 exp1.original_type = NULL;
ba47d38d 4824 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
7d882b83 4825 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
27bf414c
JM
4826 }
4827 else
4828 {
4829 cond.value
85498824 4830 = c_objc_common_truthvalue_conversion
ba47d38d 4831 (cond_loc, default_conversion (cond.value));
7d882b83 4832 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
46bdb9cf 4833 exp1 = c_parser_expression_conv (parser);
ebfbbdc5 4834 mark_exp_read (exp1.value);
7d882b83
ILT
4835 c_inhibit_evaluation_warnings +=
4836 ((cond.value == truthvalue_true_node)
4837 - (cond.value == truthvalue_false_node));
27bf414c 4838 }
744aa42f
ILT
4839
4840 colon_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
4841 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4842 {
7d882b83 4843 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
27bf414c
JM
4844 ret.value = error_mark_node;
4845 ret.original_code = ERROR_MARK;
6866c6e8 4846 ret.original_type = NULL;
27bf414c
JM
4847 return ret;
4848 }
c2255bc4
AH
4849 {
4850 location_t exp2_loc = c_parser_peek_token (parser)->location;
4851 exp2 = c_parser_conditional_expression (parser, NULL);
ebfbbdc5 4852 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
c2255bc4 4853 }
7d882b83 4854 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
744aa42f 4855 ret.value = build_conditional_expr (colon_loc, cond.value,
928c19bb 4856 cond.original_code == C_MAYBE_CONST_EXPR,
d130ae11
ILT
4857 exp1.value, exp1.original_type,
4858 exp2.value, exp2.original_type);
27bf414c 4859 ret.original_code = ERROR_MARK;
6866c6e8
ILT
4860 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4861 ret.original_type = NULL;
4862 else
4863 {
4864 tree t1, t2;
4865
4866 /* If both sides are enum type, the default conversion will have
4867 made the type of the result be an integer type. We want to
4868 remember the enum types we started with. */
4869 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4870 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4871 ret.original_type = ((t1 != error_mark_node
4872 && t2 != error_mark_node
4873 && (TYPE_MAIN_VARIANT (t1)
4874 == TYPE_MAIN_VARIANT (t2)))
4875 ? t1
4876 : NULL);
4877 }
27bf414c
JM
4878 return ret;
4879}
4880
4881/* Parse a binary expression; that is, a logical-OR-expression (C90
4882 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4883 an Objective-C message expression which is the primary-expression
4884 starting the expression as an initializer.
4885
4886 multiplicative-expression:
4887 cast-expression
4888 multiplicative-expression * cast-expression
4889 multiplicative-expression / cast-expression
4890 multiplicative-expression % cast-expression
4891
4892 additive-expression:
4893 multiplicative-expression
4894 additive-expression + multiplicative-expression
4895 additive-expression - multiplicative-expression
4896
4897 shift-expression:
4898 additive-expression
4899 shift-expression << additive-expression
4900 shift-expression >> additive-expression
4901
4902 relational-expression:
4903 shift-expression
4904 relational-expression < shift-expression
4905 relational-expression > shift-expression
4906 relational-expression <= shift-expression
4907 relational-expression >= shift-expression
4908
4909 equality-expression:
4910 relational-expression
4911 equality-expression == relational-expression
4912 equality-expression != relational-expression
4913
4914 AND-expression:
4915 equality-expression
4916 AND-expression & equality-expression
4917
4918 exclusive-OR-expression:
4919 AND-expression
4920 exclusive-OR-expression ^ AND-expression
4921
4922 inclusive-OR-expression:
4923 exclusive-OR-expression
4924 inclusive-OR-expression | exclusive-OR-expression
4925
4926 logical-AND-expression:
4927 inclusive-OR-expression
4928 logical-AND-expression && inclusive-OR-expression
4929
4930 logical-OR-expression:
4931 logical-AND-expression
4932 logical-OR-expression || logical-AND-expression
4933*/
4934
4935static struct c_expr
4936c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4937{
4938 /* A binary expression is parsed using operator-precedence parsing,
4939 with the operands being cast expressions. All the binary
4940 operators are left-associative. Thus a binary expression is of
4941 form:
4942
4943 E0 op1 E1 op2 E2 ...
4944
4945 which we represent on a stack. On the stack, the precedence
4946 levels are strictly increasing. When a new operator is
4947 encountered of higher precedence than that at the top of the
4948 stack, it is pushed; its LHS is the top expression, and its RHS
4949 is everything parsed until it is popped. When a new operator is
4950 encountered with precedence less than or equal to that at the top
4951 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4952 by the result of the operation until the operator at the top of
4953 the stack has lower precedence than the new operator or there is
4954 only one element on the stack; then the top expression is the LHS
4955 of the new operator. In the case of logical AND and OR
7d882b83
ILT
4956 expressions, we also need to adjust c_inhibit_evaluation_warnings
4957 as appropriate when the operators are pushed and popped. */
27bf414c
JM
4958
4959 /* The precedence levels, where 0 is a dummy lowest level used for
4960 the bottom of the stack. */
4961 enum prec {
4962 PREC_NONE,
4963 PREC_LOGOR,
4964 PREC_LOGAND,
4965 PREC_BITOR,
4966 PREC_BITXOR,
4967 PREC_BITAND,
4968 PREC_EQ,
4969 PREC_REL,
4970 PREC_SHIFT,
4971 PREC_ADD,
4972 PREC_MULT,
4973 NUM_PRECS
4974 };
4975 struct {
4976 /* The expression at this stack level. */
4977 struct c_expr expr;
4978 /* The precedence of the operator on its left, PREC_NONE at the
4979 bottom of the stack. */
4980 enum prec prec;
4981 /* The operation on its left. */
4982 enum tree_code op;
ca80e52b
EB
4983 /* The source location of this operation. */
4984 location_t loc;
27bf414c
JM
4985 } stack[NUM_PRECS];
4986 int sp;
ba47d38d 4987 /* Location of the binary operator. */
1f6d0c60 4988 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
27bf414c
JM
4989#define POP \
4990 do { \
4991 switch (stack[sp].op) \
4992 { \
4993 case TRUTH_ANDIF_EXPR: \
7d882b83
ILT
4994 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4995 == truthvalue_false_node); \
27bf414c
JM
4996 break; \
4997 case TRUTH_ORIF_EXPR: \
7d882b83
ILT
4998 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4999 == truthvalue_true_node); \
27bf414c
JM
5000 break; \
5001 default: \
5002 break; \
5003 } \
f2a71bbc 5004 stack[sp - 1].expr \
ebfbbdc5
JJ
5005 = default_function_array_read_conversion (stack[sp - 1].loc, \
5006 stack[sp - 1].expr); \
f2a71bbc 5007 stack[sp].expr \
ebfbbdc5
JJ
5008 = default_function_array_read_conversion (stack[sp].loc, \
5009 stack[sp].expr); \
ca80e52b 5010 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
ba47d38d 5011 stack[sp].op, \
27bf414c
JM
5012 stack[sp - 1].expr, \
5013 stack[sp].expr); \
5014 sp--; \
5015 } while (0)
5016 gcc_assert (!after || c_dialect_objc ());
ca80e52b 5017 stack[0].loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5018 stack[0].expr = c_parser_cast_expression (parser, after);
5019 stack[0].prec = PREC_NONE;
5020 sp = 0;
5021 while (true)
5022 {
5023 enum prec oprec;
5024 enum tree_code ocode;
5025 if (parser->error)
5026 goto out;
5027 switch (c_parser_peek_token (parser)->type)
5028 {
5029 case CPP_MULT:
5030 oprec = PREC_MULT;
5031 ocode = MULT_EXPR;
5032 break;
5033 case CPP_DIV:
5034 oprec = PREC_MULT;
5035 ocode = TRUNC_DIV_EXPR;
5036 break;
5037 case CPP_MOD:
5038 oprec = PREC_MULT;
5039 ocode = TRUNC_MOD_EXPR;
5040 break;
5041 case CPP_PLUS:
5042 oprec = PREC_ADD;
5043 ocode = PLUS_EXPR;
5044 break;
5045 case CPP_MINUS:
5046 oprec = PREC_ADD;
5047 ocode = MINUS_EXPR;
5048 break;
5049 case CPP_LSHIFT:
5050 oprec = PREC_SHIFT;
5051 ocode = LSHIFT_EXPR;
5052 break;
5053 case CPP_RSHIFT:
5054 oprec = PREC_SHIFT;
5055 ocode = RSHIFT_EXPR;
5056 break;
5057 case CPP_LESS:
5058 oprec = PREC_REL;
5059 ocode = LT_EXPR;
5060 break;
5061 case CPP_GREATER:
5062 oprec = PREC_REL;
5063 ocode = GT_EXPR;
5064 break;
5065 case CPP_LESS_EQ:
5066 oprec = PREC_REL;
5067 ocode = LE_EXPR;
5068 break;
5069 case CPP_GREATER_EQ:
5070 oprec = PREC_REL;
5071 ocode = GE_EXPR;
5072 break;
5073 case CPP_EQ_EQ:
5074 oprec = PREC_EQ;
5075 ocode = EQ_EXPR;
5076 break;
5077 case CPP_NOT_EQ:
5078 oprec = PREC_EQ;
5079 ocode = NE_EXPR;
5080 break;
5081 case CPP_AND:
5082 oprec = PREC_BITAND;
5083 ocode = BIT_AND_EXPR;
5084 break;
5085 case CPP_XOR:
5086 oprec = PREC_BITXOR;
5087 ocode = BIT_XOR_EXPR;
5088 break;
5089 case CPP_OR:
5090 oprec = PREC_BITOR;
5091 ocode = BIT_IOR_EXPR;
5092 break;
5093 case CPP_AND_AND:
5094 oprec = PREC_LOGAND;
5095 ocode = TRUTH_ANDIF_EXPR;
5096 break;
5097 case CPP_OR_OR:
5098 oprec = PREC_LOGOR;
5099 ocode = TRUTH_ORIF_EXPR;
5100 break;
5101 default:
5102 /* Not a binary operator, so end of the binary
5103 expression. */
5104 goto out;
5105 }
ba47d38d 5106 binary_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5107 c_parser_consume_token (parser);
5108 while (oprec <= stack[sp].prec)
5109 POP;
5110 switch (ocode)
5111 {
5112 case TRUTH_ANDIF_EXPR:
f2a71bbc 5113 stack[sp].expr
ebfbbdc5
JJ
5114 = default_function_array_read_conversion (stack[sp].loc,
5115 stack[sp].expr);
85498824 5116 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 5117 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
5118 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5119 == truthvalue_false_node);
27bf414c
JM
5120 break;
5121 case TRUTH_ORIF_EXPR:
f2a71bbc 5122 stack[sp].expr
ebfbbdc5
JJ
5123 = default_function_array_read_conversion (stack[sp].loc,
5124 stack[sp].expr);
85498824 5125 stack[sp].expr.value = c_objc_common_truthvalue_conversion
ca80e52b 5126 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7d882b83
ILT
5127 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5128 == truthvalue_true_node);
27bf414c
JM
5129 break;
5130 default:
5131 break;
5132 }
5133 sp++;
ca80e52b 5134 stack[sp].loc = binary_loc;
27bf414c
JM
5135 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5136 stack[sp].prec = oprec;
5137 stack[sp].op = ocode;
c2255bc4 5138 stack[sp].loc = binary_loc;
27bf414c
JM
5139 }
5140 out:
5141 while (sp > 0)
5142 POP;
5143 return stack[0].expr;
5144#undef POP
5145}
5146
5147/* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5148 NULL then it is an Objective-C message expression which is the
5149 primary-expression starting the expression as an initializer.
5150
5151 cast-expression:
5152 unary-expression
5153 ( type-name ) unary-expression
5154*/
5155
5156static struct c_expr
5157c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5158{
c2255bc4 5159 location_t cast_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5160 gcc_assert (!after || c_dialect_objc ());
5161 if (after)
c2255bc4
AH
5162 return c_parser_postfix_expression_after_primary (parser,
5163 cast_loc, *after);
27bf414c
JM
5164 /* If the expression begins with a parenthesized type name, it may
5165 be either a cast or a compound literal; we need to see whether
5166 the next character is '{' to tell the difference. If not, it is
5167 an unary expression. */
5168 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5169 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5170 {
5171 struct c_type_name *type_name;
5172 struct c_expr ret;
f2a71bbc 5173 struct c_expr expr;
27bf414c
JM
5174 c_parser_consume_token (parser);
5175 type_name = c_parser_type_name (parser);
5176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5177 if (type_name == NULL)
5178 {
5179 ret.value = error_mark_node;
5180 ret.original_code = ERROR_MARK;
6866c6e8 5181 ret.original_type = NULL;
27bf414c
JM
5182 return ret;
5183 }
33c9159e
AH
5184
5185 /* Save casted types in the function's used types hash table. */
8d8d1a28 5186 used_types_insert (type_name->specs->type);
33c9159e 5187
27bf414c 5188 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
24b97832 5189 return c_parser_postfix_expression_after_paren_type (parser, type_name,
c2255bc4
AH
5190 cast_loc);
5191 {
5192 location_t expr_loc = c_parser_peek_token (parser)->location;
5193 expr = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5194 expr = default_function_array_read_conversion (expr_loc, expr);
c2255bc4
AH
5195 }
5196 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
27bf414c 5197 ret.original_code = ERROR_MARK;
6866c6e8 5198 ret.original_type = NULL;
27bf414c
JM
5199 return ret;
5200 }
5201 else
5202 return c_parser_unary_expression (parser);
5203}
5204
5205/* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5206
5207 unary-expression:
5208 postfix-expression
5209 ++ unary-expression
5210 -- unary-expression
5211 unary-operator cast-expression
5212 sizeof unary-expression
5213 sizeof ( type-name )
5214
5215 unary-operator: one of
5216 & * + - ~ !
5217
5218 GNU extensions:
5219
5220 unary-expression:
5221 __alignof__ unary-expression
5222 __alignof__ ( type-name )
5223 && identifier
5224
5225 unary-operator: one of
5226 __extension__ __real__ __imag__
5227
5228 In addition, the GNU syntax treats ++ and -- as unary operators, so
5229 they may be applied to cast expressions with errors for non-lvalues
5230 given later. */
5231
5232static struct c_expr
5233c_parser_unary_expression (c_parser *parser)
5234{
5235 int ext;
46bdb9cf 5236 struct c_expr ret, op;
c2255bc4
AH
5237 location_t op_loc = c_parser_peek_token (parser)->location;
5238 location_t exp_loc;
6866c6e8
ILT
5239 ret.original_code = ERROR_MARK;
5240 ret.original_type = NULL;
27bf414c
JM
5241 switch (c_parser_peek_token (parser)->type)
5242 {
5243 case CPP_PLUS_PLUS:
5244 c_parser_consume_token (parser);
c2255bc4 5245 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5246 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5247 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5248 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
27bf414c
JM
5249 case CPP_MINUS_MINUS:
5250 c_parser_consume_token (parser);
c2255bc4 5251 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5252 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5253 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5254 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
27bf414c
JM
5255 case CPP_AND:
5256 c_parser_consume_token (parser);
ebfbbdc5
JJ
5257 op = c_parser_cast_expression (parser, NULL);
5258 mark_exp_read (op.value);
5259 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
27bf414c
JM
5260 case CPP_MULT:
5261 c_parser_consume_token (parser);
c2255bc4 5262 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5263 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5264 op = default_function_array_read_conversion (exp_loc, op);
dd865ef6 5265 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
27bf414c
JM
5266 return ret;
5267 case CPP_PLUS:
44c21c7f 5268 if (!c_dialect_objc () && !in_system_header)
c2255bc4 5269 warning_at (op_loc,
3ba09659
AH
5270 OPT_Wtraditional,
5271 "traditional C rejects the unary plus operator");
c7412148 5272 c_parser_consume_token (parser);
c2255bc4 5273 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5274 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5275 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5276 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
27bf414c
JM
5277 case CPP_MINUS:
5278 c_parser_consume_token (parser);
c2255bc4 5279 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5280 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5281 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5282 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
27bf414c
JM
5283 case CPP_COMPL:
5284 c_parser_consume_token (parser);
c2255bc4 5285 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5286 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5287 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5288 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
27bf414c
JM
5289 case CPP_NOT:
5290 c_parser_consume_token (parser);
c2255bc4 5291 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5292 op = c_parser_cast_expression (parser, NULL);
ebfbbdc5 5293 op = default_function_array_read_conversion (exp_loc, op);
c2255bc4 5294 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
27bf414c
JM
5295 case CPP_AND_AND:
5296 /* Refer to the address of a label as a pointer. */
5297 c_parser_consume_token (parser);
5298 if (c_parser_next_token_is (parser, CPP_NAME))
5299 {
5300 ret.value = finish_label_address_expr
c2255bc4 5301 (c_parser_peek_token (parser)->value, op_loc);
27bf414c 5302 c_parser_consume_token (parser);
27bf414c
JM
5303 }
5304 else
5305 {
5306 c_parser_error (parser, "expected identifier");
5307 ret.value = error_mark_node;
27bf414c 5308 }
43f6dfd3 5309 return ret;
27bf414c
JM
5310 case CPP_KEYWORD:
5311 switch (c_parser_peek_token (parser)->keyword)
5312 {
5313 case RID_SIZEOF:
5314 return c_parser_sizeof_expression (parser);
5315 case RID_ALIGNOF:
5316 return c_parser_alignof_expression (parser);
5317 case RID_EXTENSION:
5318 c_parser_consume_token (parser);
5319 ext = disable_extension_diagnostics ();
5320 ret = c_parser_cast_expression (parser, NULL);
5321 restore_extension_diagnostics (ext);
5322 return ret;
5323 case RID_REALPART:
5324 c_parser_consume_token (parser);
c2255bc4 5325 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5326 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5327 op = default_function_array_conversion (exp_loc, op);
5328 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
27bf414c
JM
5329 case RID_IMAGPART:
5330 c_parser_consume_token (parser);
c2255bc4 5331 exp_loc = c_parser_peek_token (parser)->location;
46bdb9cf 5332 op = c_parser_cast_expression (parser, NULL);
c2255bc4
AH
5333 op = default_function_array_conversion (exp_loc, op);
5334 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
27bf414c
JM
5335 default:
5336 return c_parser_postfix_expression (parser);
5337 }
5338 default:
5339 return c_parser_postfix_expression (parser);
5340 }
5341}
5342
5343/* Parse a sizeof expression. */
5344
5345static struct c_expr
5346c_parser_sizeof_expression (c_parser *parser)
5347{
5348 struct c_expr expr;
c7412148 5349 location_t expr_loc;
27bf414c
JM
5350 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5351 c_parser_consume_token (parser);
7d882b83 5352 c_inhibit_evaluation_warnings++;
27bf414c
JM
5353 in_sizeof++;
5354 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5355 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5356 {
5357 /* Either sizeof ( type-name ) or sizeof unary-expression
5358 starting with a compound literal. */
5359 struct c_type_name *type_name;
5360 c_parser_consume_token (parser);
c7412148 5361 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5362 type_name = c_parser_type_name (parser);
5363 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5364 if (type_name == NULL)
5365 {
5366 struct c_expr ret;
7d882b83 5367 c_inhibit_evaluation_warnings--;
27bf414c
JM
5368 in_sizeof--;
5369 ret.value = error_mark_node;
5370 ret.original_code = ERROR_MARK;
6866c6e8 5371 ret.original_type = NULL;
27bf414c
JM
5372 return ret;
5373 }
5374 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5375 {
5376 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5377 type_name,
5378 expr_loc);
27bf414c
JM
5379 goto sizeof_expr;
5380 }
5381 /* sizeof ( type-name ). */
7d882b83 5382 c_inhibit_evaluation_warnings--;
27bf414c 5383 in_sizeof--;
c2255bc4 5384 return c_expr_sizeof_type (expr_loc, type_name);
27bf414c
JM
5385 }
5386 else
5387 {
c7412148 5388 expr_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5389 expr = c_parser_unary_expression (parser);
5390 sizeof_expr:
7d882b83 5391 c_inhibit_evaluation_warnings--;
27bf414c 5392 in_sizeof--;
ebfbbdc5 5393 mark_exp_read (expr.value);
27bf414c
JM
5394 if (TREE_CODE (expr.value) == COMPONENT_REF
5395 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3ba09659 5396 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
c2255bc4 5397 return c_expr_sizeof_expr (expr_loc, expr);
27bf414c
JM
5398 }
5399}
5400
5401/* Parse an alignof expression. */
5402
5403static struct c_expr
5404c_parser_alignof_expression (c_parser *parser)
5405{
5406 struct c_expr expr;
c2255bc4 5407 location_t loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5408 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5409 c_parser_consume_token (parser);
7d882b83 5410 c_inhibit_evaluation_warnings++;
27bf414c
JM
5411 in_alignof++;
5412 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5413 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5414 {
5415 /* Either __alignof__ ( type-name ) or __alignof__
5416 unary-expression starting with a compound literal. */
24b97832 5417 location_t loc;
27bf414c
JM
5418 struct c_type_name *type_name;
5419 struct c_expr ret;
5420 c_parser_consume_token (parser);
24b97832 5421 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5422 type_name = c_parser_type_name (parser);
5423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5424 if (type_name == NULL)
5425 {
5426 struct c_expr ret;
7d882b83 5427 c_inhibit_evaluation_warnings--;
27bf414c
JM
5428 in_alignof--;
5429 ret.value = error_mark_node;
5430 ret.original_code = ERROR_MARK;
6866c6e8 5431 ret.original_type = NULL;
27bf414c
JM
5432 return ret;
5433 }
5434 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5435 {
5436 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5437 type_name,
5438 loc);
27bf414c
JM
5439 goto alignof_expr;
5440 }
5441 /* alignof ( type-name ). */
7d882b83 5442 c_inhibit_evaluation_warnings--;
27bf414c 5443 in_alignof--;
c2255bc4 5444 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
27bf414c 5445 ret.original_code = ERROR_MARK;
6866c6e8 5446 ret.original_type = NULL;
27bf414c
JM
5447 return ret;
5448 }
5449 else
5450 {
5451 struct c_expr ret;
5452 expr = c_parser_unary_expression (parser);
5453 alignof_expr:
ebfbbdc5 5454 mark_exp_read (expr.value);
7d882b83 5455 c_inhibit_evaluation_warnings--;
27bf414c 5456 in_alignof--;
c2255bc4 5457 ret.value = c_alignof_expr (loc, expr.value);
27bf414c 5458 ret.original_code = ERROR_MARK;
6866c6e8 5459 ret.original_type = NULL;
27bf414c
JM
5460 return ret;
5461 }
5462}
5463
5464/* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5465
5466 postfix-expression:
5467 primary-expression
5468 postfix-expression [ expression ]
5469 postfix-expression ( argument-expression-list[opt] )
5470 postfix-expression . identifier
5471 postfix-expression -> identifier
5472 postfix-expression ++
5473 postfix-expression --
5474 ( type-name ) { initializer-list }
5475 ( type-name ) { initializer-list , }
5476
5477 argument-expression-list:
5478 argument-expression
5479 argument-expression-list , argument-expression
5480
5481 primary-expression:
5482 identifier
5483 constant
5484 string-literal
5485 ( expression )
5486
5487 GNU extensions:
5488
5489 primary-expression:
5490 __func__
5491 (treated as a keyword in GNU C)
5492 __FUNCTION__
5493 __PRETTY_FUNCTION__
5494 ( compound-statement )
5495 __builtin_va_arg ( assignment-expression , type-name )
5496 __builtin_offsetof ( type-name , offsetof-member-designator )
5497 __builtin_choose_expr ( assignment-expression ,
5498 assignment-expression ,
5499 assignment-expression )
5500 __builtin_types_compatible_p ( type-name , type-name )
5501
5502 offsetof-member-designator:
5503 identifier
5504 offsetof-member-designator . identifier
5505 offsetof-member-designator [ expression ]
5506
5507 Objective-C:
5508
5509 primary-expression:
5510 [ objc-receiver objc-message-args ]
5511 @selector ( objc-selector-arg )
5512 @protocol ( identifier )
5513 @encode ( type-name )
5514 objc-string-literal
5515*/
5516
5517static struct c_expr
5518c_parser_postfix_expression (c_parser *parser)
5519{
5520 struct c_expr expr, e1, e2, e3;
5521 struct c_type_name *t1, *t2;
c2255bc4 5522 location_t loc = c_parser_peek_token (parser)->location;;
6866c6e8
ILT
5523 expr.original_code = ERROR_MARK;
5524 expr.original_type = NULL;
27bf414c
JM
5525 switch (c_parser_peek_token (parser)->type)
5526 {
5527 case CPP_NUMBER:
754ccf7c 5528 expr.value = c_parser_peek_token (parser)->value;
754ccf7c
JJ
5529 loc = c_parser_peek_token (parser)->location;
5530 c_parser_consume_token (parser);
5531 if (TREE_CODE (expr.value) == FIXED_CST
5532 && !targetm.fixed_point_supported_p ())
5533 {
5534 error_at (loc, "fixed-point types not supported for this target");
5535 expr.value = error_mark_node;
5536 }
5537 break;
27bf414c 5538 case CPP_CHAR:
b6baa67d
KVH
5539 case CPP_CHAR16:
5540 case CPP_CHAR32:
27bf414c
JM
5541 case CPP_WCHAR:
5542 expr.value = c_parser_peek_token (parser)->value;
27bf414c
JM
5543 c_parser_consume_token (parser);
5544 break;
5545 case CPP_STRING:
b6baa67d
KVH
5546 case CPP_STRING16:
5547 case CPP_STRING32:
27bf414c 5548 case CPP_WSTRING:
2c6e3f55 5549 case CPP_UTF8STRING:
27bf414c
JM
5550 expr.value = c_parser_peek_token (parser)->value;
5551 expr.original_code = STRING_CST;
5552 c_parser_consume_token (parser);
5553 break;
5554 case CPP_OBJC_STRING:
5555 gcc_assert (c_dialect_objc ());
5556 expr.value
5557 = objc_build_string_object (c_parser_peek_token (parser)->value);
27bf414c
JM
5558 c_parser_consume_token (parser);
5559 break;
5560 case CPP_NAME:
5561 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5562 {
5563 c_parser_error (parser, "expected expression");
5564 expr.value = error_mark_node;
27bf414c
JM
5565 break;
5566 }
5567 {
5568 tree id = c_parser_peek_token (parser)->value;
5569 c_parser_consume_token (parser);
c2255bc4 5570 expr.value = build_external_ref (loc, id,
27bf414c 5571 (c_parser_peek_token (parser)->type
c2255bc4 5572 == CPP_OPEN_PAREN),
6866c6e8 5573 &expr.original_type);
27bf414c
JM
5574 }
5575 break;
5576 case CPP_OPEN_PAREN:
5577 /* A parenthesized expression, statement expression or compound
5578 literal. */
5579 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5580 {
5581 /* A statement expression. */
5582 tree stmt;
c2255bc4 5583 location_t brace_loc;
27bf414c 5584 c_parser_consume_token (parser);
c2255bc4 5585 brace_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5586 c_parser_consume_token (parser);
5587 if (cur_stmt_list == NULL)
5588 {
c2255bc4 5589 error_at (loc, "braced-group within expression allowed "
3ba09659 5590 "only inside a function");
27bf414c
JM
5591 parser->error = true;
5592 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5594 expr.value = error_mark_node;
27bf414c
JM
5595 break;
5596 }
5597 stmt = c_begin_stmt_expr ();
5598 c_parser_compound_statement_nostart (parser);
5599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5600 "expected %<)%>");
b8698a0f 5601 pedwarn (loc, OPT_pedantic,
509c9d60 5602 "ISO C forbids braced-groups within expressions");
c2255bc4 5603 expr.value = c_finish_stmt_expr (brace_loc, stmt);
82c3c067 5604 mark_exp_read (expr.value);
27bf414c
JM
5605 }
5606 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5607 {
5608 /* A compound literal. ??? Can we actually get here rather
5609 than going directly to
5610 c_parser_postfix_expression_after_paren_type from
5611 elsewhere? */
24b97832 5612 location_t loc;
27bf414c
JM
5613 struct c_type_name *type_name;
5614 c_parser_consume_token (parser);
24b97832 5615 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5616 type_name = c_parser_type_name (parser);
5617 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5618 "expected %<)%>");
5619 if (type_name == NULL)
5620 {
5621 expr.value = error_mark_node;
27bf414c
JM
5622 }
5623 else
5624 expr = c_parser_postfix_expression_after_paren_type (parser,
24b97832
ILT
5625 type_name,
5626 loc);
27bf414c
JM
5627 }
5628 else
5629 {
5630 /* A parenthesized expression. */
5631 c_parser_consume_token (parser);
5632 expr = c_parser_expression (parser);
5633 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5634 TREE_NO_WARNING (expr.value) = 1;
928c19bb
JM
5635 if (expr.original_code != C_MAYBE_CONST_EXPR)
5636 expr.original_code = ERROR_MARK;
6866c6e8 5637 /* Don't change EXPR.ORIGINAL_TYPE. */
27bf414c
JM
5638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5639 "expected %<)%>");
5640 }
5641 break;
5642 case CPP_KEYWORD:
5643 switch (c_parser_peek_token (parser)->keyword)
5644 {
5645 case RID_FUNCTION_NAME:
5646 case RID_PRETTY_FUNCTION_NAME:
5647 case RID_C99_FUNCTION_NAME:
c2255bc4 5648 expr.value = fname_decl (loc,
3ba09659 5649 c_parser_peek_token (parser)->keyword,
27bf414c 5650 c_parser_peek_token (parser)->value);
27bf414c
JM
5651 c_parser_consume_token (parser);
5652 break;
5653 case RID_VA_ARG:
5654 c_parser_consume_token (parser);
5655 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5656 {
5657 expr.value = error_mark_node;
27bf414c
JM
5658 break;
5659 }
5660 e1 = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5 5661 mark_exp_read (e1.value);
928c19bb 5662 e1.value = c_fully_fold (e1.value, false, NULL);
27bf414c
JM
5663 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5664 {
5665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5666 expr.value = error_mark_node;
27bf414c
JM
5667 break;
5668 }
72b5577d 5669 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5670 t1 = c_parser_type_name (parser);
5671 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5672 "expected %<)%>");
5673 if (t1 == NULL)
5674 {
5675 expr.value = error_mark_node;
27bf414c
JM
5676 }
5677 else
5678 {
928c19bb 5679 tree type_expr = NULL_TREE;
c2255bc4
AH
5680 expr.value = c_build_va_arg (loc, e1.value,
5681 groktypename (t1, &type_expr, NULL));
928c19bb
JM
5682 if (type_expr)
5683 {
5684 expr.value = build2 (C_MAYBE_CONST_EXPR,
5685 TREE_TYPE (expr.value), type_expr,
5686 expr.value);
5687 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5688 }
27bf414c
JM
5689 }
5690 break;
5691 case RID_OFFSETOF:
5692 c_parser_consume_token (parser);
5693 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5694 {
5695 expr.value = error_mark_node;
27bf414c
JM
5696 break;
5697 }
5698 t1 = c_parser_type_name (parser);
5699 if (t1 == NULL)
5700 {
5701 expr.value = error_mark_node;
27bf414c
JM
5702 break;
5703 }
5704 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5705 {
5706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5707 expr.value = error_mark_node;
27bf414c
JM
5708 break;
5709 }
5710 {
928c19bb 5711 tree type = groktypename (t1, NULL, NULL);
27bf414c
JM
5712 tree offsetof_ref;
5713 if (type == error_mark_node)
5714 offsetof_ref = error_mark_node;
5715 else
c2255bc4
AH
5716 {
5717 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5718 SET_EXPR_LOCATION (offsetof_ref, loc);
5719 }
27bf414c
JM
5720 /* Parse the second argument to __builtin_offsetof. We
5721 must have one identifier, and beyond that we want to
5722 accept sub structure and sub array references. */
5723 if (c_parser_next_token_is (parser, CPP_NAME))
5724 {
5725 offsetof_ref = build_component_ref
c2255bc4 5726 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
27bf414c
JM
5727 c_parser_consume_token (parser);
5728 while (c_parser_next_token_is (parser, CPP_DOT)
5729 || c_parser_next_token_is (parser,
634b5df5
JJ
5730 CPP_OPEN_SQUARE)
5731 || c_parser_next_token_is (parser,
5732 CPP_DEREF))
27bf414c 5733 {
634b5df5
JJ
5734 if (c_parser_next_token_is (parser, CPP_DEREF))
5735 {
5736 loc = c_parser_peek_token (parser)->location;
c2255bc4
AH
5737 offsetof_ref = build_array_ref (loc,
5738 offsetof_ref,
5739 integer_zero_node);
634b5df5
JJ
5740 goto do_dot;
5741 }
5742 else if (c_parser_next_token_is (parser, CPP_DOT))
27bf414c 5743 {
634b5df5 5744 do_dot:
27bf414c
JM
5745 c_parser_consume_token (parser);
5746 if (c_parser_next_token_is_not (parser,
5747 CPP_NAME))
5748 {
5749 c_parser_error (parser, "expected identifier");
5750 break;
5751 }
5752 offsetof_ref = build_component_ref
c2255bc4 5753 (loc, offsetof_ref,
27bf414c
JM
5754 c_parser_peek_token (parser)->value);
5755 c_parser_consume_token (parser);
5756 }
5757 else
5758 {
5759 tree idx;
6a3799eb 5760 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5761 c_parser_consume_token (parser);
5762 idx = c_parser_expression (parser).value;
928c19bb 5763 idx = c_fully_fold (idx, false, NULL);
27bf414c
JM
5764 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5765 "expected %<]%>");
c2255bc4 5766 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
27bf414c
JM
5767 }
5768 }
5769 }
5770 else
5771 c_parser_error (parser, "expected identifier");
5772 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5773 "expected %<)%>");
6d4d7b0e 5774 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
27bf414c
JM
5775 }
5776 break;
5777 case RID_CHOOSE_EXPR:
5778 c_parser_consume_token (parser);
5779 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5780 {
5781 expr.value = error_mark_node;
27bf414c
JM
5782 break;
5783 }
c7412148 5784 loc = c_parser_peek_token (parser)->location;
27bf414c
JM
5785 e1 = c_parser_expr_no_commas (parser, NULL);
5786 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5787 {
5788 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5789 expr.value = error_mark_node;
27bf414c
JM
5790 break;
5791 }
5792 e2 = c_parser_expr_no_commas (parser, NULL);
5793 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5794 {
5795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5796 expr.value = error_mark_node;
27bf414c
JM
5797 break;
5798 }
5799 e3 = c_parser_expr_no_commas (parser, NULL);
5800 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5801 "expected %<)%>");
5802 {
5803 tree c;
5804
4d84fe7c 5805 c = e1.value;
ebfbbdc5
JJ
5806 mark_exp_read (e2.value);
5807 mark_exp_read (e3.value);
928c19bb
JM
5808 if (TREE_CODE (c) != INTEGER_CST
5809 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
3ba09659
AH
5810 error_at (loc,
5811 "first argument to %<__builtin_choose_expr%> not"
5812 " a constant");
928c19bb 5813 constant_expression_warning (c);
27bf414c
JM
5814 expr = integer_zerop (c) ? e3 : e2;
5815 }
5816 break;
5817 case RID_TYPES_COMPATIBLE_P:
5818 c_parser_consume_token (parser);
5819 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5820 {
5821 expr.value = error_mark_node;
27bf414c
JM
5822 break;
5823 }
5824 t1 = c_parser_type_name (parser);
5825 if (t1 == NULL)
5826 {
5827 expr.value = error_mark_node;
27bf414c
JM
5828 break;
5829 }
5830 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5831 {
5832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5833 expr.value = error_mark_node;
27bf414c
JM
5834 break;
5835 }
5836 t2 = c_parser_type_name (parser);
5837 if (t2 == NULL)
5838 {
5839 expr.value = error_mark_node;
27bf414c
JM
5840 break;
5841 }
5842 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5843 "expected %<)%>");
5844 {
5845 tree e1, e2;
5846
928c19bb
JM
5847 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5848 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
27bf414c
JM
5849
5850 expr.value = comptypes (e1, e2)
5851 ? build_int_cst (NULL_TREE, 1)
5852 : build_int_cst (NULL_TREE, 0);
27bf414c
JM
5853 }
5854 break;
5855 case RID_AT_SELECTOR:
5856 gcc_assert (c_dialect_objc ());
5857 c_parser_consume_token (parser);
5858 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5859 {
5860 expr.value = error_mark_node;
27bf414c
JM
5861 break;
5862 }
5863 {
5864 tree sel = c_parser_objc_selector_arg (parser);
5865 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5866 "expected %<)%>");
c2255bc4 5867 expr.value = objc_build_selector_expr (loc, sel);
27bf414c
JM
5868 }
5869 break;
5870 case RID_AT_PROTOCOL:
5871 gcc_assert (c_dialect_objc ());
5872 c_parser_consume_token (parser);
5873 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5874 {
5875 expr.value = error_mark_node;
27bf414c
JM
5876 break;
5877 }
5878 if (c_parser_next_token_is_not (parser, CPP_NAME))
5879 {
5880 c_parser_error (parser, "expected identifier");
5881 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5882 expr.value = error_mark_node;
27bf414c
JM
5883 break;
5884 }
5885 {
5886 tree id = c_parser_peek_token (parser)->value;
5887 c_parser_consume_token (parser);
5888 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5889 "expected %<)%>");
5890 expr.value = objc_build_protocol_expr (id);
27bf414c
JM
5891 }
5892 break;
5893 case RID_AT_ENCODE:
5894 /* Extension to support C-structures in the archiver. */
5895 gcc_assert (c_dialect_objc ());
5896 c_parser_consume_token (parser);
5897 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5898 {
5899 expr.value = error_mark_node;
27bf414c
JM
5900 break;
5901 }
5902 t1 = c_parser_type_name (parser);
5903 if (t1 == NULL)
5904 {
5905 expr.value = error_mark_node;
27bf414c
JM
5906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5907 break;
5908 }
5909 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5910 "expected %<)%>");
5911 {
928c19bb 5912 tree type = groktypename (t1, NULL, NULL);
27bf414c 5913 expr.value = objc_build_encode_expr (type);
27bf414c
JM
5914 }
5915 break;
5916 default:
5917 c_parser_error (parser, "expected expression");
5918 expr.value = error_mark_node;
27bf414c
JM
5919 break;
5920 }
5921 break;
5922 case CPP_OPEN_SQUARE:
5923 if (c_dialect_objc ())
5924 {
5925 tree receiver, args;
5926 c_parser_consume_token (parser);
5927 receiver = c_parser_objc_receiver (parser);
5928 args = c_parser_objc_message_args (parser);
5929 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5930 "expected %<]%>");
5931 expr.value = objc_build_message_expr (build_tree_list (receiver,
5932 args));
27bf414c
JM
5933 break;
5934 }
5935 /* Else fall through to report error. */
5936 default:
5937 c_parser_error (parser, "expected expression");
5938 expr.value = error_mark_node;
27bf414c
JM
5939 break;
5940 }
c2255bc4 5941 return c_parser_postfix_expression_after_primary (parser, loc, expr);
27bf414c
JM
5942}
5943
5944/* Parse a postfix expression after a parenthesized type name: the
5945 brace-enclosed initializer of a compound literal, possibly followed
5946 by some postfix operators. This is separate because it is not
5947 possible to tell until after the type name whether a cast
5948 expression has a cast or a compound literal, or whether the operand
5949 of sizeof is a parenthesized type name or starts with a compound
24b97832
ILT
5950 literal. TYPE_LOC is the location where TYPE_NAME starts--the
5951 location of the first token after the parentheses around the type
5952 name. */
27bf414c
JM
5953
5954static struct c_expr
5955c_parser_postfix_expression_after_paren_type (c_parser *parser,
24b97832
ILT
5956 struct c_type_name *type_name,
5957 location_t type_loc)
27bf414c
JM
5958{
5959 tree type;
5960 struct c_expr init;
928c19bb 5961 bool non_const;
27bf414c 5962 struct c_expr expr;
c7412148 5963 location_t start_loc;
928c19bb
JM
5964 tree type_expr = NULL_TREE;
5965 bool type_expr_const = true;
c2255bc4 5966 check_compound_literal_type (type_loc, type_name);
27bf414c 5967 start_init (NULL_TREE, NULL, 0);
928c19bb 5968 type = groktypename (type_name, &type_expr, &type_expr_const);
c7412148 5969 start_loc = c_parser_peek_token (parser)->location;
85cad37c 5970 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
27bf414c 5971 {
24b97832 5972 error_at (type_loc, "compound literal has variable size");
27bf414c
JM
5973 type = error_mark_node;
5974 }
5975 init = c_parser_braced_init (parser, type, false);
5976 finish_init ();
5977 maybe_warn_string_init (type, init);
5978
36c5e70a
BE
5979 if (type != error_mark_node
5980 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
5981 && current_function_decl)
5982 {
5983 error ("compound literal qualified by address-space qualifier");
5984 type = error_mark_node;
5985 }
5986
fcf73884 5987 if (!flag_isoc99)
509c9d60 5988 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
928c19bb
JM
5989 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5990 ? CONSTRUCTOR_NON_CONST (init.value)
5991 : init.original_code == C_MAYBE_CONST_EXPR);
5992 non_const |= !type_expr_const;
c2255bc4 5993 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
27bf414c 5994 expr.original_code = ERROR_MARK;
6866c6e8 5995 expr.original_type = NULL;
928c19bb
JM
5996 if (type_expr)
5997 {
5998 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5999 {
6000 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6001 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6002 }
6003 else
6004 {
6005 gcc_assert (!non_const);
6006 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6007 type_expr, expr.value);
6008 }
6009 }
c2255bc4 6010 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
27bf414c
JM
6011}
6012
6013/* Parse a postfix expression after the initial primary or compound
c2255bc4
AH
6014 literal; that is, parse a series of postfix operators.
6015
6016 EXPR_LOC is the location of the primary expression. */
27bf414c
JM
6017
6018static struct c_expr
6019c_parser_postfix_expression_after_primary (c_parser *parser,
c2255bc4 6020 location_t expr_loc,
27bf414c
JM
6021 struct c_expr expr)
6022{
928c19bb 6023 struct c_expr orig_expr;
bbbbb16a
ILT
6024 tree ident, idx;
6025 VEC(tree,gc) *exprlist;
6026 VEC(tree,gc) *origtypes;
27bf414c
JM
6027 while (true)
6028 {
c2255bc4 6029 location_t op_loc = c_parser_peek_token (parser)->location;
27bf414c
JM
6030 switch (c_parser_peek_token (parser)->type)
6031 {
6032 case CPP_OPEN_SQUARE:
6033 /* Array reference. */
6034 c_parser_consume_token (parser);
6035 idx = c_parser_expression (parser).value;
6036 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6037 "expected %<]%>");
c2255bc4 6038 expr.value = build_array_ref (op_loc, expr.value, idx);
27bf414c 6039 expr.original_code = ERROR_MARK;
6866c6e8 6040 expr.original_type = NULL;
27bf414c
JM
6041 break;
6042 case CPP_OPEN_PAREN:
6043 /* Function call. */
6044 c_parser_consume_token (parser);
6045 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
bbbbb16a 6046 exprlist = NULL;
27bf414c 6047 else
bbbbb16a 6048 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
27bf414c
JM
6049 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6050 "expected %<)%>");
928c19bb 6051 orig_expr = expr;
ebfbbdc5 6052 mark_exp_read (expr.value);
c2255bc4
AH
6053 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6054 "(" after the FUNCNAME, which is what we have now. */
6055 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
bbbbb16a 6056 origtypes);
27bf414c 6057 expr.original_code = ERROR_MARK;
928c19bb
JM
6058 if (TREE_CODE (expr.value) == INTEGER_CST
6059 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6060 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6061 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6062 expr.original_code = C_MAYBE_CONST_EXPR;
6866c6e8 6063 expr.original_type = NULL;
bbbbb16a
ILT
6064 if (exprlist != NULL)
6065 {
c166b898
ILT
6066 release_tree_vector (exprlist);
6067 release_tree_vector (origtypes);
bbbbb16a 6068 }
27bf414c
JM
6069 break;
6070 case CPP_DOT:
6071 /* Structure element reference. */
6072 c_parser_consume_token (parser);
c2255bc4 6073 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
6074 if (c_parser_next_token_is (parser, CPP_NAME))
6075 ident = c_parser_peek_token (parser)->value;
6076 else
6077 {
6078 c_parser_error (parser, "expected identifier");
6079 expr.value = error_mark_node;
6080 expr.original_code = ERROR_MARK;
6866c6e8 6081 expr.original_type = NULL;
27bf414c
JM
6082 return expr;
6083 }
6084 c_parser_consume_token (parser);
c2255bc4 6085 expr.value = build_component_ref (op_loc, expr.value, ident);
27bf414c 6086 expr.original_code = ERROR_MARK;
6866c6e8
ILT
6087 if (TREE_CODE (expr.value) != COMPONENT_REF)
6088 expr.original_type = NULL;
6089 else
6090 {
6091 /* Remember the original type of a bitfield. */
6092 tree field = TREE_OPERAND (expr.value, 1);
6093 if (TREE_CODE (field) != FIELD_DECL)
6094 expr.original_type = NULL;
6095 else
6096 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6097 }
27bf414c
JM
6098 break;
6099 case CPP_DEREF:
6100 /* Structure element reference. */
6101 c_parser_consume_token (parser);
c2255bc4 6102 expr = default_function_array_conversion (expr_loc, expr);
27bf414c
JM
6103 if (c_parser_next_token_is (parser, CPP_NAME))
6104 ident = c_parser_peek_token (parser)->value;
6105 else
6106 {
6107 c_parser_error (parser, "expected identifier");
6108 expr.value = error_mark_node;
6109 expr.original_code = ERROR_MARK;
6866c6e8 6110 expr.original_type = NULL;
27bf414c
JM
6111 return expr;
6112 }
6113 c_parser_consume_token (parser);
c2255bc4
AH
6114 expr.value = build_component_ref (op_loc,
6115 build_indirect_ref (op_loc,
c9f9eb5d 6116 expr.value,
dd865ef6 6117 RO_ARROW),
6a3799eb 6118 ident);
27bf414c 6119 expr.original_code = ERROR_MARK;
6866c6e8
ILT
6120 if (TREE_CODE (expr.value) != COMPONENT_REF)
6121 expr.original_type = NULL;
6122 else
6123 {
6124 /* Remember the original type of a bitfield. */
6125 tree field = TREE_OPERAND (expr.value, 1);
6126 if (TREE_CODE (field) != FIELD_DECL)
6127 expr.original_type = NULL;
6128 else
6129 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6130 }
27bf414c
JM
6131 break;
6132 case CPP_PLUS_PLUS:
6133 /* Postincrement. */
6134 c_parser_consume_token (parser);
ebfbbdc5 6135 expr = default_function_array_read_conversion (expr_loc, expr);
c2255bc4 6136 expr.value = build_unary_op (op_loc,
c9f9eb5d 6137 POSTINCREMENT_EXPR, expr.value, 0);
27bf414c 6138 expr.original_code = ERROR_MARK;
6866c6e8 6139 expr.original_type = NULL;
27bf414c
JM
6140 break;
6141 case CPP_MINUS_MINUS:
6142 /* Postdecrement. */
6143 c_parser_consume_token (parser);
ebfbbdc5 6144 expr = default_function_array_read_conversion (expr_loc, expr);
c2255bc4 6145 expr.value = build_unary_op (op_loc,
c9f9eb5d 6146 POSTDECREMENT_EXPR, expr.value, 0);
27bf414c 6147 expr.original_code = ERROR_MARK;
6866c6e8 6148 expr.original_type = NULL;
27bf414c
JM
6149 break;
6150 default:
6151 return expr;
6152 }
6153 }
6154}
6155
6156/* Parse an expression (C90 6.3.17, C99 6.5.17).
6157
6158 expression:
6159 assignment-expression
6160 expression , assignment-expression
6161*/
6162
6163static struct c_expr
6164c_parser_expression (c_parser *parser)
6165{
6166 struct c_expr expr;
6167 expr = c_parser_expr_no_commas (parser, NULL);
6168 while (c_parser_next_token_is (parser, CPP_COMMA))
6169 {
6170 struct c_expr next;
056928b2 6171 tree lhsval;
c2255bc4
AH
6172 location_t loc = c_parser_peek_token (parser)->location;
6173 location_t expr_loc;
27bf414c 6174 c_parser_consume_token (parser);
c2255bc4 6175 expr_loc = c_parser_peek_token (parser)->location;
056928b2
JJ
6176 lhsval = expr.value;
6177 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6178 lhsval = TREE_OPERAND (lhsval, 1);
6179 if (DECL_P (lhsval) || handled_component_p (lhsval))
6180 mark_exp_read (lhsval);
27bf414c 6181 next = c_parser_expr_no_commas (parser, NULL);
c2255bc4
AH
6182 next = default_function_array_conversion (expr_loc, next);
6183 expr.value = build_compound_expr (loc, expr.value, next.value);
27bf414c 6184 expr.original_code = COMPOUND_EXPR;
81f40b79 6185 expr.original_type = next.original_type;
27bf414c
JM
6186 }
6187 return expr;
6188}
6189
46bdb9cf
JM
6190/* Parse an expression and convert functions or arrays to
6191 pointers. */
6192
6193static struct c_expr
6194c_parser_expression_conv (c_parser *parser)
6195{
6196 struct c_expr expr;
c2255bc4 6197 location_t loc = c_parser_peek_token (parser)->location;
46bdb9cf 6198 expr = c_parser_expression (parser);
c2255bc4 6199 expr = default_function_array_conversion (loc, expr);
46bdb9cf
JM
6200 return expr;
6201}
6202
6203/* Parse a non-empty list of expressions. If CONVERT_P, convert
928c19bb 6204 functions and arrays to pointers. If FOLD_P, fold the expressions.
27bf414c
JM
6205
6206 nonempty-expr-list:
6207 assignment-expression
6208 nonempty-expr-list , assignment-expression
6209*/
6210
bbbbb16a
ILT
6211static VEC(tree,gc) *
6212c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6213 VEC(tree,gc) **p_orig_types)
27bf414c 6214{
bbbbb16a
ILT
6215 VEC(tree,gc) *ret;
6216 VEC(tree,gc) *orig_types;
27bf414c 6217 struct c_expr expr;
c2255bc4 6218 location_t loc = c_parser_peek_token (parser)->location;
bbbbb16a 6219
c166b898 6220 ret = make_tree_vector ();
bbbbb16a
ILT
6221 if (p_orig_types == NULL)
6222 orig_types = NULL;
6223 else
c166b898 6224 orig_types = make_tree_vector ();
bbbbb16a 6225
27bf414c 6226 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 6227 if (convert_p)
ebfbbdc5 6228 expr = default_function_array_read_conversion (loc, expr);
928c19bb
JM
6229 if (fold_p)
6230 expr.value = c_fully_fold (expr.value, false, NULL);
bbbbb16a
ILT
6231 VEC_quick_push (tree, ret, expr.value);
6232 if (orig_types != NULL)
6233 VEC_quick_push (tree, orig_types, expr.original_type);
27bf414c
JM
6234 while (c_parser_next_token_is (parser, CPP_COMMA))
6235 {
6236 c_parser_consume_token (parser);
c2255bc4 6237 loc = c_parser_peek_token (parser)->location;
27bf414c 6238 expr = c_parser_expr_no_commas (parser, NULL);
46bdb9cf 6239 if (convert_p)
ebfbbdc5 6240 expr = default_function_array_read_conversion (loc, expr);
928c19bb
JM
6241 if (fold_p)
6242 expr.value = c_fully_fold (expr.value, false, NULL);
bbbbb16a
ILT
6243 VEC_safe_push (tree, gc, ret, expr.value);
6244 if (orig_types != NULL)
6245 VEC_safe_push (tree, gc, orig_types, expr.original_type);
27bf414c 6246 }
bbbbb16a
ILT
6247 if (orig_types != NULL)
6248 *p_orig_types = orig_types;
27bf414c
JM
6249 return ret;
6250}
27bf414c
JM
6251\f
6252/* Parse Objective-C-specific constructs. */
6253
6254/* Parse an objc-class-definition.
6255
6256 objc-class-definition:
6257 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6258 objc-class-instance-variables[opt] objc-methodprotolist @end
6259 @implementation identifier objc-superclass[opt]
6260 objc-class-instance-variables[opt]
6261 @interface identifier ( identifier ) objc-protocol-refs[opt]
6262 objc-methodprotolist @end
6263 @implementation identifier ( identifier )
6264
6265 objc-superclass:
6266 : identifier
6267
6268 "@interface identifier (" must start "@interface identifier (
6269 identifier ) ...": objc-methodprotolist in the first production may
0fa2e4df 6270 not start with a parenthesized identifier as a declarator of a data
27bf414c
JM
6271 definition with no declaration specifiers if the objc-superclass,
6272 objc-protocol-refs and objc-class-instance-variables are omitted. */
6273
6274static void
6275c_parser_objc_class_definition (c_parser *parser)
6276{
6277 bool iface_p;
6278 tree id1;
6279 tree superclass;
6280 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6281 iface_p = true;
6282 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6283 iface_p = false;
6284 else
6285 gcc_unreachable ();
6286 c_parser_consume_token (parser);
6287 if (c_parser_next_token_is_not (parser, CPP_NAME))
6288 {
6289 c_parser_error (parser, "expected identifier");
6290 return;
6291 }
6292 id1 = c_parser_peek_token (parser)->value;
6293 c_parser_consume_token (parser);
6294 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6295 {
6296 tree id2;
6297 tree proto = NULL_TREE;
6298 c_parser_consume_token (parser);
6299 if (c_parser_next_token_is_not (parser, CPP_NAME))
6300 {
6301 c_parser_error (parser, "expected identifier");
6302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6303 return;
6304 }
6305 id2 = c_parser_peek_token (parser)->value;
6306 c_parser_consume_token (parser);
6307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6308 if (!iface_p)
6309 {
6310 objc_start_category_implementation (id1, id2);
6311 return;
6312 }
6313 if (c_parser_next_token_is (parser, CPP_LESS))
6314 proto = c_parser_objc_protocol_refs (parser);
6315 objc_start_category_interface (id1, id2, proto);
6316 c_parser_objc_methodprotolist (parser);
6317 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6318 objc_finish_interface ();
6319 return;
6320 }
6321 if (c_parser_next_token_is (parser, CPP_COLON))
6322 {
6323 c_parser_consume_token (parser);
6324 if (c_parser_next_token_is_not (parser, CPP_NAME))
6325 {
6326 c_parser_error (parser, "expected identifier");
6327 return;
6328 }
6329 superclass = c_parser_peek_token (parser)->value;
6330 c_parser_consume_token (parser);
6331 }
6332 else
6333 superclass = NULL_TREE;
6334 if (iface_p)
6335 {
6336 tree proto = NULL_TREE;
6337 if (c_parser_next_token_is (parser, CPP_LESS))
6338 proto = c_parser_objc_protocol_refs (parser);
6339 objc_start_class_interface (id1, superclass, proto);
6340 }
6341 else
6342 objc_start_class_implementation (id1, superclass);
6343 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6344 c_parser_objc_class_instance_variables (parser);
6345 if (iface_p)
6346 {
6347 objc_continue_interface ();
6348 c_parser_objc_methodprotolist (parser);
6349 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6350 objc_finish_interface ();
6351 }
6352 else
6353 {
6354 objc_continue_implementation ();
6355 return;
6356 }
6357}
6358
6359/* Parse objc-class-instance-variables.
6360
6361 objc-class-instance-variables:
6362 { objc-instance-variable-decl-list[opt] }
6363
6364 objc-instance-variable-decl-list:
6365 objc-visibility-spec
6366 objc-instance-variable-decl ;
6367 ;
6368 objc-instance-variable-decl-list objc-visibility-spec
6369 objc-instance-variable-decl-list objc-instance-variable-decl ;
6370 objc-instance-variable-decl-list ;
6371
6372 objc-visibility-spec:
6373 @private
6374 @protected
6375 @public
6376
6377 objc-instance-variable-decl:
6378 struct-declaration
6379*/
6380
6381static void
6382c_parser_objc_class_instance_variables (c_parser *parser)
6383{
6384 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6385 c_parser_consume_token (parser);
6386 while (c_parser_next_token_is_not (parser, CPP_EOF))
6387 {
6388 tree decls;
6389 /* Parse any stray semicolon. */
6390 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6391 {
b8698a0f 6392 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 6393 "extra semicolon in struct or union specified");
27bf414c
JM
6394 c_parser_consume_token (parser);
6395 continue;
6396 }
6397 /* Stop if at the end of the instance variables. */
6398 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6399 {
6400 c_parser_consume_token (parser);
6401 break;
6402 }
6403 /* Parse any objc-visibility-spec. */
eea1139b 6404 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
27bf414c
JM
6405 {
6406 c_parser_consume_token (parser);
6407 objc_set_visibility (2);
6408 continue;
6409 }
eea1139b 6410 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
27bf414c
JM
6411 {
6412 c_parser_consume_token (parser);
6413 objc_set_visibility (0);
6414 continue;
6415 }
eea1139b 6416 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
27bf414c
JM
6417 {
6418 c_parser_consume_token (parser);
6419 objc_set_visibility (1);
6420 continue;
6421 }
bc4071dd
RH
6422 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6423 {
6424 c_parser_pragma (parser, pragma_external);
6425 continue;
6426 }
6427
27bf414c
JM
6428 /* Parse some comma-separated declarations. */
6429 decls = c_parser_struct_declaration (parser);
6430 {
6431 /* Comma-separated instance variables are chained together in
6432 reverse order; add them one by one. */
6433 tree ivar = nreverse (decls);
6434 for (; ivar; ivar = TREE_CHAIN (ivar))
6435 objc_add_instance_variable (copy_node (ivar));
6436 }
6437 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6438 }
6439}
6440
6441/* Parse an objc-class-declaration.
6442
6443 objc-class-declaration:
6444 @class identifier-list ;
6445*/
6446
6447static void
6448c_parser_objc_class_declaration (c_parser *parser)
6449{
6450 tree list = NULL_TREE;
eea1139b 6451 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
27bf414c
JM
6452 c_parser_consume_token (parser);
6453 /* Any identifiers, including those declared as type names, are OK
6454 here. */
6455 while (true)
6456 {
6457 tree id;
6458 if (c_parser_next_token_is_not (parser, CPP_NAME))
6459 {
6460 c_parser_error (parser, "expected identifier");
6461 break;
6462 }
6463 id = c_parser_peek_token (parser)->value;
6464 list = chainon (list, build_tree_list (NULL_TREE, id));
6465 c_parser_consume_token (parser);
6466 if (c_parser_next_token_is (parser, CPP_COMMA))
6467 c_parser_consume_token (parser);
6468 else
6469 break;
6470 }
6471 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6472 objc_declare_class (list);
6473}
6474
6475/* Parse an objc-alias-declaration.
6476
6477 objc-alias-declaration:
6478 @compatibility_alias identifier identifier ;
6479*/
6480
6481static void
6482c_parser_objc_alias_declaration (c_parser *parser)
6483{
6484 tree id1, id2;
6485 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6486 c_parser_consume_token (parser);
6487 if (c_parser_next_token_is_not (parser, CPP_NAME))
6488 {
6489 c_parser_error (parser, "expected identifier");
6490 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6491 return;
6492 }
6493 id1 = c_parser_peek_token (parser)->value;
6494 c_parser_consume_token (parser);
6495 if (c_parser_next_token_is_not (parser, CPP_NAME))
6496 {
6497 c_parser_error (parser, "expected identifier");
6498 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6499 return;
6500 }
6501 id2 = c_parser_peek_token (parser)->value;
6502 c_parser_consume_token (parser);
6503 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6504 objc_declare_alias (id1, id2);
6505}
6506
6507/* Parse an objc-protocol-definition.
6508
6509 objc-protocol-definition:
6510 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6511 @protocol identifier-list ;
6512
6513 "@protocol identifier ;" should be resolved as "@protocol
6514 identifier-list ;": objc-methodprotolist may not start with a
6515 semicolon in the first alternative if objc-protocol-refs are
6516 omitted. */
6517
6518static void
6519c_parser_objc_protocol_definition (c_parser *parser)
6520{
6521 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6522 c_parser_consume_token (parser);
6523 if (c_parser_next_token_is_not (parser, CPP_NAME))
6524 {
6525 c_parser_error (parser, "expected identifier");
6526 return;
6527 }
6528 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6529 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6530 {
6531 tree list = NULL_TREE;
6532 /* Any identifiers, including those declared as type names, are
6533 OK here. */
6534 while (true)
6535 {
6536 tree id;
6537 if (c_parser_next_token_is_not (parser, CPP_NAME))
6538 {
6539 c_parser_error (parser, "expected identifier");
6540 break;
6541 }
6542 id = c_parser_peek_token (parser)->value;
6543 list = chainon (list, build_tree_list (NULL_TREE, id));
6544 c_parser_consume_token (parser);
6545 if (c_parser_next_token_is (parser, CPP_COMMA))
6546 c_parser_consume_token (parser);
6547 else
6548 break;
6549 }
6550 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6551 objc_declare_protocols (list);
6552 }
6553 else
6554 {
6555 tree id = c_parser_peek_token (parser)->value;
6556 tree proto = NULL_TREE;
6557 c_parser_consume_token (parser);
6558 if (c_parser_next_token_is (parser, CPP_LESS))
6559 proto = c_parser_objc_protocol_refs (parser);
0bacb8c7 6560 parser->objc_pq_context = true;
27bf414c
JM
6561 objc_start_protocol (id, proto);
6562 c_parser_objc_methodprotolist (parser);
6563 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
0bacb8c7 6564 parser->objc_pq_context = false;
27bf414c
JM
6565 objc_finish_interface ();
6566 }
6567}
6568
6569/* Parse an objc-method-type.
6570
6571 objc-method-type:
6572 +
6573 -
6574*/
6575
6576static enum tree_code
6577c_parser_objc_method_type (c_parser *parser)
6578{
6579 switch (c_parser_peek_token (parser)->type)
6580 {
6581 case CPP_PLUS:
6582 c_parser_consume_token (parser);
6583 return PLUS_EXPR;
6584 case CPP_MINUS:
6585 c_parser_consume_token (parser);
6586 return MINUS_EXPR;
6587 default:
6588 gcc_unreachable ();
6589 }
6590}
6591
6592/* Parse an objc-method-definition.
6593
6594 objc-method-definition:
6595 objc-method-type objc-method-decl ;[opt] compound-statement
6596*/
6597
6598static void
6599c_parser_objc_method_definition (c_parser *parser)
6600{
6601 enum tree_code type = c_parser_objc_method_type (parser);
6602 tree decl;
6603 objc_set_method_type (type);
0bacb8c7 6604 parser->objc_pq_context = true;
27bf414c
JM
6605 decl = c_parser_objc_method_decl (parser);
6606 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6607 {
6608 c_parser_consume_token (parser);
b8698a0f 6609 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 6610 "extra semicolon in method definition specified");
27bf414c 6611 }
8f078c08
AP
6612 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6613 {
6614 c_parser_error (parser, "expected %<{%>");
6615 return;
6616 }
0bacb8c7 6617 parser->objc_pq_context = false;
27bf414c
JM
6618 objc_start_method_definition (decl);
6619 add_stmt (c_parser_compound_statement (parser));
6620 objc_finish_method_definition (current_function_decl);
6621}
6622
6623/* Parse an objc-methodprotolist.
6624
6625 objc-methodprotolist:
6626 empty
6627 objc-methodprotolist objc-methodproto
6628 objc-methodprotolist declaration
6629 objc-methodprotolist ;
6630
6631 The declaration is a data definition, which may be missing
6632 declaration specifiers under the same rules and diagnostics as
6633 other data definitions outside functions, and the stray semicolon
6634 is diagnosed the same way as a stray semicolon outside a
6635 function. */
6636
6637static void
6638c_parser_objc_methodprotolist (c_parser *parser)
6639{
6640 while (true)
6641 {
6642 /* The list is terminated by @end. */
6643 switch (c_parser_peek_token (parser)->type)
6644 {
6645 case CPP_SEMICOLON:
b8698a0f 6646 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
509c9d60 6647 "ISO C does not allow extra %<;%> outside of a function");
27bf414c
JM
6648 c_parser_consume_token (parser);
6649 break;
6650 case CPP_PLUS:
6651 case CPP_MINUS:
6652 c_parser_objc_methodproto (parser);
6653 break;
b9b58168
RH
6654 case CPP_PRAGMA:
6655 c_parser_pragma (parser, pragma_external);
6656 break;
27bf414c
JM
6657 case CPP_EOF:
6658 return;
6659 default:
6660 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6661 return;
32912286
JM
6662 c_parser_declaration_or_fndef (parser, false, false, true,
6663 false, true);
27bf414c
JM
6664 break;
6665 }
6666 }
6667}
6668
6669/* Parse an objc-methodproto.
6670
6671 objc-methodproto:
6672 objc-method-type objc-method-decl ;
6673*/
6674
6675static void
6676c_parser_objc_methodproto (c_parser *parser)
6677{
6678 enum tree_code type = c_parser_objc_method_type (parser);
6679 tree decl;
6680 objc_set_method_type (type);
6681 /* Remember protocol qualifiers in prototypes. */
0bacb8c7 6682 parser->objc_pq_context = true;
27bf414c
JM
6683 decl = c_parser_objc_method_decl (parser);
6684 /* Forget protocol qualifiers here. */
0bacb8c7 6685 parser->objc_pq_context = false;
27bf414c
JM
6686 objc_add_method_declaration (decl);
6687 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6688}
6689
6690/* Parse an objc-method-decl.
6691
6692 objc-method-decl:
6693 ( objc-type-name ) objc-selector
6694 objc-selector
6695 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6696 objc-keyword-selector objc-optparmlist
6697
6698 objc-keyword-selector:
6699 objc-keyword-decl
6700 objc-keyword-selector objc-keyword-decl
6701
6702 objc-keyword-decl:
6703 objc-selector : ( objc-type-name ) identifier
6704 objc-selector : identifier
6705 : ( objc-type-name ) identifier
6706 : identifier
6707
6708 objc-optparmlist:
6709 objc-optparms objc-optellipsis
6710
6711 objc-optparms:
6712 empty
6713 objc-opt-parms , parameter-declaration
6714
6715 objc-optellipsis:
6716 empty
6717 , ...
6718*/
6719
6720static tree
6721c_parser_objc_method_decl (c_parser *parser)
6722{
6723 tree type = NULL_TREE;
6724 tree sel;
6725 tree parms = NULL_TREE;
dbb74365
RS
6726 bool ellipsis = false;
6727
27bf414c
JM
6728 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6729 {
6730 c_parser_consume_token (parser);
6731 type = c_parser_objc_type_name (parser);
6732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6733 }
6734 sel = c_parser_objc_selector (parser);
6735 /* If there is no selector, or a colon follows, we have an
6736 objc-keyword-selector. If there is a selector, and a colon does
6737 not follow, that selector ends the objc-method-decl. */
6738 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6739 {
6740 tree tsel = sel;
6741 tree list = NULL_TREE;
27bf414c
JM
6742 while (true)
6743 {
6744 tree atype = NULL_TREE, id, keyworddecl;
6745 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6746 break;
6747 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6748 {
6749 c_parser_consume_token (parser);
6750 atype = c_parser_objc_type_name (parser);
6751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6752 "expected %<)%>");
6753 }
6754 if (c_parser_next_token_is_not (parser, CPP_NAME))
6755 {
6756 c_parser_error (parser, "expected identifier");
6757 return error_mark_node;
6758 }
6759 id = c_parser_peek_token (parser)->value;
6760 c_parser_consume_token (parser);
6761 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6762 list = chainon (list, keyworddecl);
6763 tsel = c_parser_objc_selector (parser);
6764 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6765 break;
6766 }
6767 /* Parse the optional parameter list. Optional Objective-C
6768 method parameters follow the C syntax, and may include '...'
6769 to denote a variable number of arguments. */
6770 parms = make_node (TREE_LIST);
27bf414c
JM
6771 while (c_parser_next_token_is (parser, CPP_COMMA))
6772 {
6773 struct c_parm *parm;
6774 c_parser_consume_token (parser);
6775 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6776 {
6777 ellipsis = true;
6778 c_parser_consume_token (parser);
6779 break;
6780 }
6781 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6782 if (parm == NULL)
6783 break;
6784 parms = chainon (parms,
6785 build_tree_list (NULL_TREE, grokparm (parm)));
6786 }
27bf414c
JM
6787 sel = list;
6788 }
dbb74365 6789 return objc_build_method_signature (type, sel, parms, ellipsis);
27bf414c
JM
6790}
6791
6792/* Parse an objc-type-name.
6793
6794 objc-type-name:
6795 objc-type-qualifiers[opt] type-name
6796 objc-type-qualifiers[opt]
6797
6798 objc-type-qualifiers:
6799 objc-type-qualifier
6800 objc-type-qualifiers objc-type-qualifier
6801
6802 objc-type-qualifier: one of
6803 in out inout bycopy byref oneway
6804*/
6805
6806static tree
6807c_parser_objc_type_name (c_parser *parser)
6808{
6809 tree quals = NULL_TREE;
d75d71e0 6810 struct c_type_name *type_name = NULL;
27bf414c
JM
6811 tree type = NULL_TREE;
6812 while (true)
6813 {
6814 c_token *token = c_parser_peek_token (parser);
6815 if (token->type == CPP_KEYWORD
6816 && (token->keyword == RID_IN
6817 || token->keyword == RID_OUT
6818 || token->keyword == RID_INOUT
6819 || token->keyword == RID_BYCOPY
6820 || token->keyword == RID_BYREF
6821 || token->keyword == RID_ONEWAY))
6822 {
6823 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6824 c_parser_consume_token (parser);
6825 }
6826 else
6827 break;
6828 }
6829 if (c_parser_next_token_starts_typename (parser))
d75d71e0
ILT
6830 type_name = c_parser_type_name (parser);
6831 if (type_name)
928c19bb 6832 type = groktypename (type_name, NULL, NULL);
27bf414c
JM
6833 return build_tree_list (quals, type);
6834}
6835
6836/* Parse objc-protocol-refs.
6837
6838 objc-protocol-refs:
6839 < identifier-list >
6840*/
6841
6842static tree
6843c_parser_objc_protocol_refs (c_parser *parser)
6844{
6845 tree list = NULL_TREE;
6846 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6847 c_parser_consume_token (parser);
6848 /* Any identifiers, including those declared as type names, are OK
6849 here. */
6850 while (true)
6851 {
6852 tree id;
6853 if (c_parser_next_token_is_not (parser, CPP_NAME))
6854 {
6855 c_parser_error (parser, "expected identifier");
6856 break;
6857 }
6858 id = c_parser_peek_token (parser)->value;
6859 list = chainon (list, build_tree_list (NULL_TREE, id));
6860 c_parser_consume_token (parser);
6861 if (c_parser_next_token_is (parser, CPP_COMMA))
6862 c_parser_consume_token (parser);
6863 else
6864 break;
6865 }
6866 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6867 return list;
6868}
6869
6870/* Parse an objc-try-catch-statement.
6871
6872 objc-try-catch-statement:
6873 @try compound-statement objc-catch-list[opt]
6874 @try compound-statement objc-catch-list[opt] @finally compound-statement
6875
6876 objc-catch-list:
6877 @catch ( parameter-declaration ) compound-statement
6878 objc-catch-list @catch ( parameter-declaration ) compound-statement
6879*/
6880
6881static void
6882c_parser_objc_try_catch_statement (c_parser *parser)
6883{
6884 location_t loc;
6885 tree stmt;
eea1139b 6886 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
27bf414c
JM
6887 c_parser_consume_token (parser);
6888 loc = c_parser_peek_token (parser)->location;
6889 stmt = c_parser_compound_statement (parser);
6890 objc_begin_try_stmt (loc, stmt);
eea1139b 6891 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
27bf414c
JM
6892 {
6893 struct c_parm *parm;
6894 c_parser_consume_token (parser);
6895 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6896 break;
6897 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6898 if (parm == NULL)
6899 {
6900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6901 break;
6902 }
6903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6904 objc_begin_catch_clause (grokparm (parm));
6905 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6906 c_parser_compound_statement_nostart (parser);
6907 objc_finish_catch_clause ();
6908 }
6909 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6910 {
6911 location_t finloc;
6912 tree finstmt;
6913 c_parser_consume_token (parser);
6914 finloc = c_parser_peek_token (parser)->location;
6915 finstmt = c_parser_compound_statement (parser);
6916 objc_build_finally_clause (finloc, finstmt);
6917 }
6918 objc_finish_try_stmt ();
6919}
6920
6921/* Parse an objc-synchronized-statement.
6922
6923 objc-synchronized-statement:
6924 @synchronized ( expression ) compound-statement
6925*/
6926
6927static void
6928c_parser_objc_synchronized_statement (c_parser *parser)
6929{
6930 location_t loc;
6931 tree expr, stmt;
6932 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6933 c_parser_consume_token (parser);
6934 loc = c_parser_peek_token (parser)->location;
6935 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6936 {
6937 expr = c_parser_expression (parser).value;
928c19bb 6938 expr = c_fully_fold (expr, false, NULL);
27bf414c
JM
6939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6940 }
6941 else
6942 expr = error_mark_node;
6943 stmt = c_parser_compound_statement (parser);
6944 objc_build_synchronized (loc, expr, stmt);
6945}
6946
6947/* Parse an objc-selector; return NULL_TREE without an error if the
6948 next token is not an objc-selector.
6949
6950 objc-selector:
6951 identifier
6952 one of
6953 enum struct union if else while do for switch case default
6954 break continue return goto asm sizeof typeof __alignof
6955 unsigned long const short volatile signed restrict _Complex
6956 in out inout bycopy byref oneway int char float double void _Bool
6957
6958 ??? Why this selection of keywords but not, for example, storage
6959 class specifiers? */
6960
6961static tree
6962c_parser_objc_selector (c_parser *parser)
6963{
6964 c_token *token = c_parser_peek_token (parser);
6965 tree value = token->value;
6966 if (token->type == CPP_NAME)
6967 {
6968 c_parser_consume_token (parser);
6969 return value;
6970 }
6971 if (token->type != CPP_KEYWORD)
6972 return NULL_TREE;
6973 switch (token->keyword)
6974 {
6975 case RID_ENUM:
6976 case RID_STRUCT:
6977 case RID_UNION:
6978 case RID_IF:
6979 case RID_ELSE:
6980 case RID_WHILE:
6981 case RID_DO:
6982 case RID_FOR:
6983 case RID_SWITCH:
6984 case RID_CASE:
6985 case RID_DEFAULT:
6986 case RID_BREAK:
6987 case RID_CONTINUE:
6988 case RID_RETURN:
6989 case RID_GOTO:
6990 case RID_ASM:
6991 case RID_SIZEOF:
6992 case RID_TYPEOF:
6993 case RID_ALIGNOF:
6994 case RID_UNSIGNED:
6995 case RID_LONG:
a6766312 6996 case RID_INT128:
27bf414c
JM
6997 case RID_CONST:
6998 case RID_SHORT:
6999 case RID_VOLATILE:
7000 case RID_SIGNED:
7001 case RID_RESTRICT:
7002 case RID_COMPLEX:
7003 case RID_IN:
7004 case RID_OUT:
7005 case RID_INOUT:
7006 case RID_BYCOPY:
7007 case RID_BYREF:
7008 case RID_ONEWAY:
7009 case RID_INT:
7010 case RID_CHAR:
7011 case RID_FLOAT:
7012 case RID_DOUBLE:
7013 case RID_VOID:
7014 case RID_BOOL:
7015 c_parser_consume_token (parser);
7016 return value;
7017 default:
7018 return NULL_TREE;
7019 }
7020}
7021
7022/* Parse an objc-selector-arg.
7023
7024 objc-selector-arg:
7025 objc-selector
7026 objc-keywordname-list
7027
7028 objc-keywordname-list:
7029 objc-keywordname
7030 objc-keywordname-list objc-keywordname
7031
7032 objc-keywordname:
7033 objc-selector :
7034 :
7035*/
7036
7037static tree
7038c_parser_objc_selector_arg (c_parser *parser)
7039{
7040 tree sel = c_parser_objc_selector (parser);
7041 tree list = NULL_TREE;
7042 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7043 return sel;
7044 while (true)
7045 {
7046 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7047 return list;
7048 list = chainon (list, build_tree_list (sel, NULL_TREE));
7049 sel = c_parser_objc_selector (parser);
7050 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7051 break;
7052 }
7053 return list;
7054}
7055
7056/* Parse an objc-receiver.
7057
7058 objc-receiver:
7059 expression
7060 class-name
7061 type-name
7062*/
7063
7064static tree
7065c_parser_objc_receiver (c_parser *parser)
7066{
7067 if (c_parser_peek_token (parser)->type == CPP_NAME
7068 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7069 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7070 {
7071 tree id = c_parser_peek_token (parser)->value;
7072 c_parser_consume_token (parser);
7073 return objc_get_class_reference (id);
7074 }
928c19bb 7075 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
27bf414c
JM
7076}
7077
7078/* Parse objc-message-args.
7079
7080 objc-message-args:
7081 objc-selector
7082 objc-keywordarg-list
7083
7084 objc-keywordarg-list:
7085 objc-keywordarg
7086 objc-keywordarg-list objc-keywordarg
7087
7088 objc-keywordarg:
7089 objc-selector : objc-keywordexpr
7090 : objc-keywordexpr
7091*/
7092
7093static tree
7094c_parser_objc_message_args (c_parser *parser)
7095{
7096 tree sel = c_parser_objc_selector (parser);
7097 tree list = NULL_TREE;
7098 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7099 return sel;
7100 while (true)
7101 {
7102 tree keywordexpr;
7103 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
0a7d7dea 7104 return error_mark_node;
27bf414c
JM
7105 keywordexpr = c_parser_objc_keywordexpr (parser);
7106 list = chainon (list, build_tree_list (sel, keywordexpr));
7107 sel = c_parser_objc_selector (parser);
7108 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7109 break;
7110 }
7111 return list;
7112}
7113
7114/* Parse an objc-keywordexpr.
7115
7116 objc-keywordexpr:
7117 nonempty-expr-list
7118*/
7119
7120static tree
7121c_parser_objc_keywordexpr (c_parser *parser)
7122{
bbbbb16a
ILT
7123 tree ret;
7124 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7125 if (VEC_length (tree, expr_list) == 1)
27bf414c
JM
7126 {
7127 /* Just return the expression, remove a level of
7128 indirection. */
bbbbb16a 7129 ret = VEC_index (tree, expr_list, 0);
27bf414c
JM
7130 }
7131 else
7132 {
7133 /* We have a comma expression, we will collapse later. */
c166b898 7134 ret = build_tree_list_vec (expr_list);
27bf414c 7135 }
c166b898 7136 release_tree_vector (expr_list);
bbbbb16a 7137 return ret;
27bf414c
JM
7138}
7139
7140\f
953ff289
DN
7141/* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
7142 should be considered, statements. ALLOW_STMT is true if we're within
7143 the context of a function and such pragmas are to be allowed. Returns
7144 true if we actually parsed such a pragma. */
27bf414c 7145
bc4071dd 7146static bool
953ff289 7147c_parser_pragma (c_parser *parser, enum pragma_context context)
bc4071dd
RH
7148{
7149 unsigned int id;
7150
7151 id = c_parser_peek_token (parser)->pragma_kind;
7152 gcc_assert (id != PRAGMA_NONE);
7153
7154 switch (id)
7155 {
953ff289
DN
7156 case PRAGMA_OMP_BARRIER:
7157 if (context != pragma_compound)
7158 {
7159 if (context == pragma_stmt)
7160 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7161 "used in compound statements");
7162 goto bad_stmt;
7163 }
7164 c_parser_omp_barrier (parser);
7165 return false;
7166
7167 case PRAGMA_OMP_FLUSH:
7168 if (context != pragma_compound)
7169 {
7170 if (context == pragma_stmt)
7171 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7172 "used in compound statements");
7173 goto bad_stmt;
7174 }
7175 c_parser_omp_flush (parser);
7176 return false;
7177
a68ab351
JJ
7178 case PRAGMA_OMP_TASKWAIT:
7179 if (context != pragma_compound)
7180 {
7181 if (context == pragma_stmt)
7182 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7183 "used in compound statements");
7184 goto bad_stmt;
7185 }
7186 c_parser_omp_taskwait (parser);
7187 return false;
7188
953ff289
DN
7189 case PRAGMA_OMP_THREADPRIVATE:
7190 c_parser_omp_threadprivate (parser);
7191 return false;
7192
7193 case PRAGMA_OMP_SECTION:
3ba09659
AH
7194 error_at (c_parser_peek_token (parser)->location,
7195 "%<#pragma omp section%> may only be used in "
7196 "%<#pragma omp sections%> construct");
953ff289
DN
7197 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7198 return false;
7199
bc4071dd
RH
7200 case PRAGMA_GCC_PCH_PREPROCESS:
7201 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7202 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7203 return false;
7204
7205 default:
953ff289
DN
7206 if (id < PRAGMA_FIRST_EXTERNAL)
7207 {
7208 if (context == pragma_external)
7209 {
7210 bad_stmt:
7211 c_parser_error (parser, "expected declaration specifiers");
7212 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7213 return false;
7214 }
7215 c_parser_omp_construct (parser);
7216 return true;
7217 }
bc4071dd
RH
7218 break;
7219 }
7220
7221 c_parser_consume_pragma (parser);
7222 c_invoke_pragma_handler (id);
27bf414c 7223
b8698a0f 7224 /* Skip to EOL, but suppress any error message. Those will have been
bc4071dd
RH
7225 generated by the handler routine through calling error, as opposed
7226 to calling c_parser_error. */
7227 parser->error = true;
7228 c_parser_skip_to_pragma_eol (parser);
7229
7230 return false;
7231}
7232
7233/* The interface the pragma parsers have to the lexer. */
7234
7235enum cpp_ttype
7236pragma_lex (tree *value)
7237{
7238 c_token *tok = c_parser_peek_token (the_parser);
7239 enum cpp_ttype ret = tok->type;
7240
7241 *value = tok->value;
7242 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
7243 ret = CPP_EOF;
7244 else
7245 {
7246 if (ret == CPP_KEYWORD)
7247 ret = CPP_NAME;
7248 c_parser_consume_token (the_parser);
7249 }
7250
7251 return ret;
7252}
7253
7254static void
7255c_parser_pragma_pch_preprocess (c_parser *parser)
7256{
7257 tree name = NULL;
7258
7259 c_parser_consume_pragma (parser);
7260 if (c_parser_next_token_is (parser, CPP_STRING))
7261 {
7262 name = c_parser_peek_token (parser)->value;
7263 c_parser_consume_token (parser);
7264 }
7265 else
7266 c_parser_error (parser, "expected string literal");
7267 c_parser_skip_to_pragma_eol (parser);
7268
7269 if (name)
7270 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
7271}
953ff289
DN
7272\f
7273/* OpenMP 2.5 parsing routines. */
7274
7275/* Returns name of the next clause.
7276 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
7277 the token is not consumed. Otherwise appropriate pragma_omp_clause is
7278 returned and the token is consumed. */
7279
7280static pragma_omp_clause
7281c_parser_omp_clause_name (c_parser *parser)
7282{
7283 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7284
7285 if (c_parser_next_token_is_keyword (parser, RID_IF))
7286 result = PRAGMA_OMP_CLAUSE_IF;
7287 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7288 result = PRAGMA_OMP_CLAUSE_DEFAULT;
7289 else if (c_parser_next_token_is (parser, CPP_NAME))
7290 {
7291 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7292
7293 switch (p[0])
7294 {
7295 case 'c':
a68ab351
JJ
7296 if (!strcmp ("collapse", p))
7297 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7298 else if (!strcmp ("copyin", p))
953ff289
DN
7299 result = PRAGMA_OMP_CLAUSE_COPYIN;
7300 else if (!strcmp ("copyprivate", p))
7301 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7302 break;
7303 case 'f':
7304 if (!strcmp ("firstprivate", p))
7305 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7306 break;
7307 case 'l':
7308 if (!strcmp ("lastprivate", p))
7309 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7310 break;
7311 case 'n':
7312 if (!strcmp ("nowait", p))
7313 result = PRAGMA_OMP_CLAUSE_NOWAIT;
7314 else if (!strcmp ("num_threads", p))
7315 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7316 break;
7317 case 'o':
7318 if (!strcmp ("ordered", p))
7319 result = PRAGMA_OMP_CLAUSE_ORDERED;
7320 break;
7321 case 'p':
7322 if (!strcmp ("private", p))
7323 result = PRAGMA_OMP_CLAUSE_PRIVATE;
7324 break;
7325 case 'r':
7326 if (!strcmp ("reduction", p))
7327 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7328 break;
7329 case 's':
7330 if (!strcmp ("schedule", p))
7331 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7332 else if (!strcmp ("shared", p))
7333 result = PRAGMA_OMP_CLAUSE_SHARED;
7334 break;
a68ab351
JJ
7335 case 'u':
7336 if (!strcmp ("untied", p))
7337 result = PRAGMA_OMP_CLAUSE_UNTIED;
7338 break;
953ff289
DN
7339 }
7340 }
7341
7342 if (result != PRAGMA_OMP_CLAUSE_NONE)
7343 c_parser_consume_token (parser);
7344
7345 return result;
7346}
7347
7348/* Validate that a clause of the given type does not already exist. */
7349
7350static void
d75d71e0
ILT
7351check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7352 const char *name)
953ff289
DN
7353{
7354 tree c;
7355
7356 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
aaf46ef9 7357 if (OMP_CLAUSE_CODE (c) == code)
953ff289 7358 {
c2255bc4
AH
7359 location_t loc = OMP_CLAUSE_LOCATION (c);
7360 error_at (loc, "too many %qs clauses", name);
953ff289
DN
7361 break;
7362 }
7363}
7364
7365/* OpenMP 2.5:
7366 variable-list:
7367 identifier
7368 variable-list , identifier
7369
c2255bc4
AH
7370 If KIND is nonzero, create the appropriate node and install the
7371 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7372 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
953ff289
DN
7373
7374 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7375 return the list created. */
7376
7377static tree
c2255bc4
AH
7378c_parser_omp_variable_list (c_parser *parser,
7379 location_t clause_loc,
7380 enum omp_clause_code kind,
aaf46ef9 7381 tree list)
953ff289
DN
7382{
7383 if (c_parser_next_token_is_not (parser, CPP_NAME)
7384 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7385 c_parser_error (parser, "expected identifier");
7386
7387 while (c_parser_next_token_is (parser, CPP_NAME)
7388 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7389 {
7390 tree t = lookup_name (c_parser_peek_token (parser)->value);
7391
7392 if (t == NULL_TREE)
c2255bc4
AH
7393 undeclared_variable (c_parser_peek_token (parser)->location,
7394 c_parser_peek_token (parser)->value);
953ff289
DN
7395 else if (t == error_mark_node)
7396 ;
7397 else if (kind != 0)
7398 {
c2255bc4 7399 tree u = build_omp_clause (clause_loc, kind);
953ff289
DN
7400 OMP_CLAUSE_DECL (u) = t;
7401 OMP_CLAUSE_CHAIN (u) = list;
7402 list = u;
7403 }
7404 else
7405 list = tree_cons (t, NULL_TREE, list);
7406
7407 c_parser_consume_token (parser);
7408
7409 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7410 break;
7411
7412 c_parser_consume_token (parser);
7413 }
7414
7415 return list;
7416}
7417
7418/* Similarly, but expect leading and trailing parenthesis. This is a very
7419 common case for omp clauses. */
7420
7421static tree
d75d71e0
ILT
7422c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7423 tree list)
953ff289 7424{
c2255bc4
AH
7425 /* The clauses location. */
7426 location_t loc = c_parser_peek_token (parser)->location;
7427
953ff289
DN
7428 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7429 {
c2255bc4 7430 list = c_parser_omp_variable_list (parser, loc, kind, list);
953ff289
DN
7431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7432 }
7433 return list;
7434}
7435
a68ab351
JJ
7436/* OpenMP 3.0:
7437 collapse ( constant-expression ) */
7438
7439static tree
7440c_parser_omp_clause_collapse (c_parser *parser, tree list)
7441{
7442 tree c, num = error_mark_node;
7443 HOST_WIDE_INT n;
7444 location_t loc;
7445
7446 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7447
7448 loc = c_parser_peek_token (parser)->location;
7449 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7450 {
7451 num = c_parser_expr_no_commas (parser, NULL).value;
7452 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7453 }
7454 if (num == error_mark_node)
7455 return list;
7456 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7457 || !host_integerp (num, 0)
7458 || (n = tree_low_cst (num, 0)) <= 0
7459 || (int) n != n)
7460 {
3ba09659
AH
7461 error_at (loc,
7462 "collapse argument needs positive constant integer expression");
a68ab351
JJ
7463 return list;
7464 }
c2255bc4 7465 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
a68ab351
JJ
7466 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7467 OMP_CLAUSE_CHAIN (c) = list;
7468 return c;
7469}
7470
953ff289
DN
7471/* OpenMP 2.5:
7472 copyin ( variable-list ) */
7473
7474static tree
7475c_parser_omp_clause_copyin (c_parser *parser, tree list)
7476{
7477 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7478}
7479
7480/* OpenMP 2.5:
7481 copyprivate ( variable-list ) */
7482
7483static tree
7484c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7485{
7486 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7487}
7488
7489/* OpenMP 2.5:
7490 default ( shared | none ) */
7491
7492static tree
7493c_parser_omp_clause_default (c_parser *parser, tree list)
7494{
7495 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
c2255bc4 7496 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7497 tree c;
7498
7499 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7500 return list;
7501 if (c_parser_next_token_is (parser, CPP_NAME))
7502 {
7503 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7504
7505 switch (p[0])
7506 {
7507 case 'n':
7508 if (strcmp ("none", p) != 0)
7509 goto invalid_kind;
7510 kind = OMP_CLAUSE_DEFAULT_NONE;
7511 break;
7512
7513 case 's':
7514 if (strcmp ("shared", p) != 0)
7515 goto invalid_kind;
7516 kind = OMP_CLAUSE_DEFAULT_SHARED;
7517 break;
7518
7519 default:
7520 goto invalid_kind;
7521 }
7522
7523 c_parser_consume_token (parser);
7524 }
7525 else
7526 {
7527 invalid_kind:
7528 c_parser_error (parser, "expected %<none%> or %<shared%>");
7529 }
7530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7531
7532 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7533 return list;
7534
7535 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
c2255bc4 7536 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
953ff289
DN
7537 OMP_CLAUSE_CHAIN (c) = list;
7538 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7539
7540 return c;
7541}
7542
7543/* OpenMP 2.5:
7544 firstprivate ( variable-list ) */
7545
7546static tree
7547c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7548{
7549 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7550}
7551
7552/* OpenMP 2.5:
7553 if ( expression ) */
7554
7555static tree
7556c_parser_omp_clause_if (c_parser *parser, tree list)
7557{
c2255bc4 7558 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7559 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7560 {
7561 tree t = c_parser_paren_condition (parser);
7562 tree c;
7563
7564 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7565
c2255bc4 7566 c = build_omp_clause (loc, OMP_CLAUSE_IF);
953ff289
DN
7567 OMP_CLAUSE_IF_EXPR (c) = t;
7568 OMP_CLAUSE_CHAIN (c) = list;
7569 list = c;
7570 }
7571 else
7572 c_parser_error (parser, "expected %<(%>");
7573
7574 return list;
7575}
7576
7577/* OpenMP 2.5:
7578 lastprivate ( variable-list ) */
7579
7580static tree
7581c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7582{
7583 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7584}
7585
7586/* OpenMP 2.5:
7587 nowait */
7588
7589static tree
7590c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7591{
7592 tree c;
c2255bc4 7593 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7594
7595 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7596
c2255bc4 7597 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
953ff289
DN
7598 OMP_CLAUSE_CHAIN (c) = list;
7599 return c;
7600}
7601
7602/* OpenMP 2.5:
7603 num_threads ( expression ) */
7604
7605static tree
7606c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7607{
c2255bc4 7608 location_t num_threads_loc = c_parser_peek_token (parser)->location;
953ff289
DN
7609 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7610 {
c7412148 7611 location_t expr_loc = c_parser_peek_token (parser)->location;
953ff289 7612 tree c, t = c_parser_expression (parser).value;
928c19bb 7613 t = c_fully_fold (t, false, NULL);
953ff289
DN
7614
7615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7616
7617 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7618 {
7619 c_parser_error (parser, "expected integer expression");
7620 return list;
7621 }
7622
7623 /* Attempt to statically determine when the number isn't positive. */
db3927fb 7624 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
953ff289 7625 build_int_cst (TREE_TYPE (t), 0));
c2255bc4
AH
7626 if (CAN_HAVE_LOCATION_P (c))
7627 SET_EXPR_LOCATION (c, expr_loc);
953ff289
DN
7628 if (c == boolean_true_node)
7629 {
3ba09659
AH
7630 warning_at (expr_loc, 0,
7631 "%<num_threads%> value must be positive");
953ff289
DN
7632 t = integer_one_node;
7633 }
7634
7635 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7636
c2255bc4 7637 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
953ff289
DN
7638 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7639 OMP_CLAUSE_CHAIN (c) = list;
7640 list = c;
7641 }
7642
7643 return list;
7644}
7645
7646/* OpenMP 2.5:
7647 ordered */
7648
7649static tree
c2255bc4 7650c_parser_omp_clause_ordered (c_parser *parser, tree list)
953ff289
DN
7651{
7652 tree c;
7653
7654 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7655
c2255bc4
AH
7656 c = build_omp_clause (c_parser_peek_token (parser)->location,
7657 OMP_CLAUSE_ORDERED);
953ff289 7658 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 7659
953ff289
DN
7660 return c;
7661}
7662
7663/* OpenMP 2.5:
7664 private ( variable-list ) */
7665
7666static tree
7667c_parser_omp_clause_private (c_parser *parser, tree list)
7668{
7669 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7670}
7671
7672/* OpenMP 2.5:
7673 reduction ( reduction-operator : variable-list )
7674
7675 reduction-operator:
7676 One of: + * - & ^ | && || */
7677
7678static tree
7679c_parser_omp_clause_reduction (c_parser *parser, tree list)
7680{
c2255bc4 7681 location_t clause_loc = c_parser_peek_token (parser)->location;
953ff289
DN
7682 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7683 {
7684 enum tree_code code;
7685
7686 switch (c_parser_peek_token (parser)->type)
7687 {
7688 case CPP_PLUS:
7689 code = PLUS_EXPR;
7690 break;
7691 case CPP_MULT:
7692 code = MULT_EXPR;
7693 break;
7694 case CPP_MINUS:
7695 code = MINUS_EXPR;
7696 break;
7697 case CPP_AND:
7698 code = BIT_AND_EXPR;
7699 break;
7700 case CPP_XOR:
7701 code = BIT_XOR_EXPR;
7702 break;
7703 case CPP_OR:
7704 code = BIT_IOR_EXPR;
7705 break;
7706 case CPP_AND_AND:
7707 code = TRUTH_ANDIF_EXPR;
7708 break;
7709 case CPP_OR_OR:
7710 code = TRUTH_ORIF_EXPR;
7711 break;
7712 default:
7713 c_parser_error (parser,
7714 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7715 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7717 return list;
7718 }
7719 c_parser_consume_token (parser);
7720 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7721 {
7722 tree nl, c;
7723
c2255bc4
AH
7724 nl = c_parser_omp_variable_list (parser, clause_loc,
7725 OMP_CLAUSE_REDUCTION, list);
953ff289
DN
7726 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7727 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7728
7729 list = nl;
7730 }
7731 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7732 }
7733 return list;
7734}
7735
7736/* OpenMP 2.5:
7737 schedule ( schedule-kind )
7738 schedule ( schedule-kind , expression )
7739
7740 schedule-kind:
a68ab351 7741 static | dynamic | guided | runtime | auto
953ff289
DN
7742*/
7743
7744static tree
7745c_parser_omp_clause_schedule (c_parser *parser, tree list)
7746{
7747 tree c, t;
c2255bc4 7748 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
7749
7750 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7751 return list;
7752
c2255bc4 7753 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
953ff289
DN
7754
7755 if (c_parser_next_token_is (parser, CPP_NAME))
7756 {
7757 tree kind = c_parser_peek_token (parser)->value;
7758 const char *p = IDENTIFIER_POINTER (kind);
7759
7760 switch (p[0])
7761 {
7762 case 'd':
7763 if (strcmp ("dynamic", p) != 0)
7764 goto invalid_kind;
7765 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7766 break;
7767
7768 case 'g':
7769 if (strcmp ("guided", p) != 0)
7770 goto invalid_kind;
7771 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7772 break;
7773
7774 case 'r':
7775 if (strcmp ("runtime", p) != 0)
7776 goto invalid_kind;
7777 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7778 break;
7779
7780 default:
7781 goto invalid_kind;
7782 }
7783 }
7784 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7785 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
a68ab351
JJ
7786 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7787 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
953ff289
DN
7788 else
7789 goto invalid_kind;
7790
7791 c_parser_consume_token (parser);
7792 if (c_parser_next_token_is (parser, CPP_COMMA))
7793 {
c7412148 7794 location_t here;
953ff289
DN
7795 c_parser_consume_token (parser);
7796
c7412148 7797 here = c_parser_peek_token (parser)->location;
953ff289 7798 t = c_parser_expr_no_commas (parser, NULL).value;
928c19bb 7799 t = c_fully_fold (t, false, NULL);
953ff289
DN
7800
7801 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
3ba09659
AH
7802 error_at (here, "schedule %<runtime%> does not take "
7803 "a %<chunk_size%> parameter");
a68ab351 7804 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
3ba09659
AH
7805 error_at (here,
7806 "schedule %<auto%> does not take "
7807 "a %<chunk_size%> parameter");
953ff289
DN
7808 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7809 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7810 else
7811 c_parser_error (parser, "expected integer expression");
7812
7813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7814 }
7815 else
7816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7817 "expected %<,%> or %<)%>");
7818
7819 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7820 OMP_CLAUSE_CHAIN (c) = list;
7821 return c;
7822
7823 invalid_kind:
7824 c_parser_error (parser, "invalid schedule kind");
7825 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7826 return list;
7827}
7828
7829/* OpenMP 2.5:
7830 shared ( variable-list ) */
7831
7832static tree
7833c_parser_omp_clause_shared (c_parser *parser, tree list)
7834{
7835 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7836}
7837
a68ab351
JJ
7838/* OpenMP 3.0:
7839 untied */
7840
7841static tree
7842c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7843{
7844 tree c;
7845
7846 /* FIXME: Should we allow duplicates? */
7847 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7848
c2255bc4
AH
7849 c = build_omp_clause (c_parser_peek_token (parser)->location,
7850 OMP_CLAUSE_UNTIED);
a68ab351 7851 OMP_CLAUSE_CHAIN (c) = list;
c2255bc4 7852
a68ab351
JJ
7853 return c;
7854}
7855
953ff289
DN
7856/* Parse all OpenMP clauses. The set clauses allowed by the directive
7857 is a bitmask in MASK. Return the list of clauses found; the result
7858 of clause default goes in *pdefault. */
7859
7860static tree
7861c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7862 const char *where)
7863{
7864 tree clauses = NULL;
8085ca15 7865 bool first = true;
953ff289
DN
7866
7867 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7868 {
8085ca15
JJ
7869 location_t here;
7870 pragma_omp_clause c_kind;
953ff289
DN
7871 const char *c_name;
7872 tree prev = clauses;
7873
8085ca15
JJ
7874 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7875 c_parser_consume_token (parser);
7876
7877 first = false;
7878 here = c_parser_peek_token (parser)->location;
7879 c_kind = c_parser_omp_clause_name (parser);
7880
953ff289
DN
7881 switch (c_kind)
7882 {
a68ab351
JJ
7883 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7884 clauses = c_parser_omp_clause_collapse (parser, clauses);
7885 c_name = "collapse";
7886 break;
953ff289
DN
7887 case PRAGMA_OMP_CLAUSE_COPYIN:
7888 clauses = c_parser_omp_clause_copyin (parser, clauses);
7889 c_name = "copyin";
7890 break;
7891 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7892 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7893 c_name = "copyprivate";
7894 break;
7895 case PRAGMA_OMP_CLAUSE_DEFAULT:
7896 clauses = c_parser_omp_clause_default (parser, clauses);
7897 c_name = "default";
7898 break;
7899 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7900 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7901 c_name = "firstprivate";
7902 break;
7903 case PRAGMA_OMP_CLAUSE_IF:
7904 clauses = c_parser_omp_clause_if (parser, clauses);
7905 c_name = "if";
7906 break;
7907 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7908 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7909 c_name = "lastprivate";
7910 break;
7911 case PRAGMA_OMP_CLAUSE_NOWAIT:
7912 clauses = c_parser_omp_clause_nowait (parser, clauses);
7913 c_name = "nowait";
7914 break;
7915 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7916 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7917 c_name = "num_threads";
7918 break;
7919 case PRAGMA_OMP_CLAUSE_ORDERED:
7920 clauses = c_parser_omp_clause_ordered (parser, clauses);
7921 c_name = "ordered";
7922 break;
7923 case PRAGMA_OMP_CLAUSE_PRIVATE:
7924 clauses = c_parser_omp_clause_private (parser, clauses);
7925 c_name = "private";
7926 break;
7927 case PRAGMA_OMP_CLAUSE_REDUCTION:
7928 clauses = c_parser_omp_clause_reduction (parser, clauses);
7929 c_name = "reduction";
7930 break;
7931 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7932 clauses = c_parser_omp_clause_schedule (parser, clauses);
7933 c_name = "schedule";
7934 break;
7935 case PRAGMA_OMP_CLAUSE_SHARED:
7936 clauses = c_parser_omp_clause_shared (parser, clauses);
7937 c_name = "shared";
7938 break;
a68ab351
JJ
7939 case PRAGMA_OMP_CLAUSE_UNTIED:
7940 clauses = c_parser_omp_clause_untied (parser, clauses);
7941 c_name = "untied";
7942 break;
953ff289
DN
7943 default:
7944 c_parser_error (parser, "expected %<#pragma omp%> clause");
7945 goto saw_error;
7946 }
7947
7948 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7949 {
7950 /* Remove the invalid clause(s) from the list to avoid
7951 confusing the rest of the compiler. */
7952 clauses = prev;
3ba09659 7953 error_at (here, "%qs is not valid for %qs", c_name, where);
953ff289
DN
7954 }
7955 }
7956
7957 saw_error:
7958 c_parser_skip_to_pragma_eol (parser);
7959
7960 return c_finish_omp_clauses (clauses);
7961}
7962
7963/* OpenMP 2.5:
7964 structured-block:
7965 statement
7966
7967 In practice, we're also interested in adding the statement to an
7968 outer node. So it is convenient if we work around the fact that
7969 c_parser_statement calls add_stmt. */
7970
7971static tree
7972c_parser_omp_structured_block (c_parser *parser)
7973{
7974 tree stmt = push_stmt_list ();
7975 c_parser_statement (parser);
7976 return pop_stmt_list (stmt);
7977}
7978
7979/* OpenMP 2.5:
7980 # pragma omp atomic new-line
7981 expression-stmt
7982
7983 expression-stmt:
7984 x binop= expr | x++ | ++x | x-- | --x
7985 binop:
7986 +, *, -, /, &, ^, |, <<, >>
7987
b8698a0f 7988 where x is an lvalue expression with scalar type.
c2255bc4
AH
7989
7990 LOC is the location of the #pragma token. */
953ff289
DN
7991
7992static void
c2255bc4 7993c_parser_omp_atomic (location_t loc, c_parser *parser)
953ff289
DN
7994{
7995 tree lhs, rhs;
fe89d797 7996 tree stmt;
953ff289 7997 enum tree_code code;
79addd1f 7998 struct c_expr rhs_expr;
953ff289
DN
7999
8000 c_parser_skip_to_pragma_eol (parser);
8001
8002 lhs = c_parser_unary_expression (parser).value;
928c19bb 8003 lhs = c_fully_fold (lhs, false, NULL);
953ff289
DN
8004 switch (TREE_CODE (lhs))
8005 {
8006 case ERROR_MARK:
8007 saw_error:
8008 c_parser_skip_to_end_of_block_or_statement (parser);
8009 return;
8010
8011 case PREINCREMENT_EXPR:
8012 case POSTINCREMENT_EXPR:
8013 lhs = TREE_OPERAND (lhs, 0);
8014 code = PLUS_EXPR;
8015 rhs = integer_one_node;
8016 break;
8017
8018 case PREDECREMENT_EXPR:
8019 case POSTDECREMENT_EXPR:
8020 lhs = TREE_OPERAND (lhs, 0);
8021 code = MINUS_EXPR;
8022 rhs = integer_one_node;
8023 break;
8024
8025 default:
8026 switch (c_parser_peek_token (parser)->type)
8027 {
8028 case CPP_MULT_EQ:
8029 code = MULT_EXPR;
8030 break;
8031 case CPP_DIV_EQ:
8032 code = TRUNC_DIV_EXPR;
8033 break;
8034 case CPP_PLUS_EQ:
8035 code = PLUS_EXPR;
8036 break;
8037 case CPP_MINUS_EQ:
8038 code = MINUS_EXPR;
8039 break;
8040 case CPP_LSHIFT_EQ:
8041 code = LSHIFT_EXPR;
8042 break;
8043 case CPP_RSHIFT_EQ:
8044 code = RSHIFT_EXPR;
8045 break;
8046 case CPP_AND_EQ:
8047 code = BIT_AND_EXPR;
8048 break;
8049 case CPP_OR_EQ:
8050 code = BIT_IOR_EXPR;
8051 break;
8052 case CPP_XOR_EQ:
8053 code = BIT_XOR_EXPR;
8054 break;
8055 default:
8056 c_parser_error (parser,
8057 "invalid operator for %<#pragma omp atomic%>");
8058 goto saw_error;
8059 }
8060
8061 c_parser_consume_token (parser);
c2255bc4
AH
8062 {
8063 location_t rhs_loc = c_parser_peek_token (parser)->location;
8064 rhs_expr = c_parser_expression (parser);
ebfbbdc5 8065 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
c2255bc4 8066 }
79addd1f 8067 rhs = rhs_expr.value;
928c19bb 8068 rhs = c_fully_fold (rhs, false, NULL);
953ff289
DN
8069 break;
8070 }
c2255bc4 8071 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
fe89d797
MM
8072 if (stmt != error_mark_node)
8073 add_stmt (stmt);
953ff289
DN
8074 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8075}
8076
8077
8078/* OpenMP 2.5:
8079 # pragma omp barrier new-line
8080*/
8081
8082static void
8083c_parser_omp_barrier (c_parser *parser)
8084{
c2255bc4 8085 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8086 c_parser_consume_pragma (parser);
8087 c_parser_skip_to_pragma_eol (parser);
8088
c2255bc4 8089 c_finish_omp_barrier (loc);
953ff289
DN
8090}
8091
8092/* OpenMP 2.5:
8093 # pragma omp critical [(name)] new-line
8094 structured-block
c2255bc4
AH
8095
8096 LOC is the location of the #pragma itself. */
953ff289
DN
8097
8098static tree
c2255bc4 8099c_parser_omp_critical (location_t loc, c_parser *parser)
953ff289
DN
8100{
8101 tree stmt, name = NULL;
8102
8103 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8104 {
8105 c_parser_consume_token (parser);
8106 if (c_parser_next_token_is (parser, CPP_NAME))
8107 {
8108 name = c_parser_peek_token (parser)->value;
8109 c_parser_consume_token (parser);
8110 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8111 }
8112 else
8113 c_parser_error (parser, "expected identifier");
8114 }
8115 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8116 c_parser_error (parser, "expected %<(%> or end of line");
8117 c_parser_skip_to_pragma_eol (parser);
8118
8119 stmt = c_parser_omp_structured_block (parser);
c2255bc4 8120 return c_finish_omp_critical (loc, stmt, name);
953ff289
DN
8121}
8122
8123/* OpenMP 2.5:
8124 # pragma omp flush flush-vars[opt] new-line
8125
8126 flush-vars:
8127 ( variable-list ) */
8128
8129static void
8130c_parser_omp_flush (c_parser *parser)
8131{
c2255bc4 8132 location_t loc = c_parser_peek_token (parser)->location;
953ff289
DN
8133 c_parser_consume_pragma (parser);
8134 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
d75d71e0 8135 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289
DN
8136 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8137 c_parser_error (parser, "expected %<(%> or end of line");
8138 c_parser_skip_to_pragma_eol (parser);
8139
c2255bc4 8140 c_finish_omp_flush (loc);
953ff289
DN
8141}
8142
fa10beec 8143/* Parse the restricted form of the for statement allowed by OpenMP.
953ff289 8144 The real trick here is to determine the loop control variable early
c2255bc4
AH
8145 so that we can push a new decl if necessary to make it private.
8146 LOC is the location of the OMP in "#pragma omp". */
953ff289
DN
8147
8148static tree
c2255bc4
AH
8149c_parser_omp_for_loop (location_t loc,
8150 c_parser *parser, tree clauses, tree *par_clauses)
953ff289 8151{
a68ab351
JJ
8152 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
8153 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
a68ab351
JJ
8154 bool fail = false, open_brace_parsed = false;
8155 int i, collapse = 1, nbraces = 0;
c2255bc4 8156 location_t for_loc;
a68ab351
JJ
8157
8158 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8159 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8160 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8161
8162 gcc_assert (collapse >= 1);
8163
8164 declv = make_tree_vec (collapse);
8165 initv = make_tree_vec (collapse);
8166 condv = make_tree_vec (collapse);
8167 incrv = make_tree_vec (collapse);
953ff289
DN
8168
8169 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8170 {
8171 c_parser_error (parser, "for statement expected");
8172 return NULL;
8173 }
c2255bc4 8174 for_loc = c_parser_peek_token (parser)->location;
953ff289
DN
8175 c_parser_consume_token (parser);
8176
a68ab351 8177 for (i = 0; i < collapse; i++)
953ff289 8178 {
a68ab351 8179 int bracecount = 0;
953ff289 8180
a68ab351
JJ
8181 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8182 goto pop_scopes;
953ff289 8183
a68ab351 8184 /* Parse the initialization declaration or expression. */
32912286 8185 if (c_parser_next_token_starts_declaration (parser))
a68ab351
JJ
8186 {
8187 if (i > 0)
8188 for_block
8189 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
32912286 8190 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
c2255bc4 8191 decl = check_for_loop_decls (for_loc);
a68ab351
JJ
8192 if (decl == NULL)
8193 goto error_init;
8194 if (DECL_INITIAL (decl) == error_mark_node)
8195 decl = error_mark_node;
8196 init = decl;
8197 }
8198 else if (c_parser_next_token_is (parser, CPP_NAME)
8199 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
8200 {
32e8bb8e 8201 struct c_expr decl_exp;
a68ab351 8202 struct c_expr init_exp;
c9f9eb5d 8203 location_t init_loc;
a68ab351 8204
32e8bb8e
ILT
8205 decl_exp = c_parser_postfix_expression (parser);
8206 decl = decl_exp.value;
a68ab351
JJ
8207
8208 c_parser_require (parser, CPP_EQ, "expected %<=%>");
8209
c2255bc4 8210 init_loc = c_parser_peek_token (parser)->location;
a68ab351 8211 init_exp = c_parser_expr_no_commas (parser, NULL);
ebfbbdc5
JJ
8212 init_exp = default_function_array_read_conversion (init_loc,
8213 init_exp);
32e8bb8e 8214 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
c2255bc4 8215 NOP_EXPR, init_loc, init_exp.value,
bbbbb16a 8216 init_exp.original_type);
c2255bc4 8217 init = c_process_expr_stmt (init_loc, init);
953ff289 8218
a68ab351
JJ
8219 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8220 }
8221 else
8222 {
8223 error_init:
8224 c_parser_error (parser,
8225 "expected iteration declaration or initialization");
8226 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8227 "expected %<)%>");
8228 fail = true;
8229 goto parse_next;
8230 }
8231
8232 /* Parse the loop condition. */
8233 cond = NULL_TREE;
8234 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
8235 {
3ba09659 8236 location_t cond_loc = c_parser_peek_token (parser)->location;
c5cdb03f 8237 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
3ba09659 8238
c5cdb03f 8239 cond = cond_expr.value;
3ba09659 8240 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
928c19bb 8241 cond = c_fully_fold (cond, false, NULL);
c5cdb03f
JJ
8242 switch (cond_expr.original_code)
8243 {
8244 case GT_EXPR:
8245 case GE_EXPR:
8246 case LT_EXPR:
8247 case LE_EXPR:
8248 break;
8249 default:
8250 /* Can't be cond = error_mark_node, because we want to preserve
8251 the location until c_finish_omp_for. */
8252 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
8253 break;
8254 }
c9f9eb5d 8255 protected_set_expr_location (cond, cond_loc);
a68ab351 8256 }
953ff289 8257 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
953ff289 8258
a68ab351
JJ
8259 /* Parse the increment expression. */
8260 incr = NULL_TREE;
8261 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
c9f9eb5d
AH
8262 {
8263 location_t incr_loc = c_parser_peek_token (parser)->location;
8264
c2255bc4
AH
8265 incr = c_process_expr_stmt (incr_loc,
8266 c_parser_expression (parser).value);
c9f9eb5d 8267 }
a68ab351 8268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
953ff289 8269
a68ab351
JJ
8270 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
8271 fail = true;
8272 else
8273 {
8274 TREE_VEC_ELT (declv, i) = decl;
8275 TREE_VEC_ELT (initv, i) = init;
8276 TREE_VEC_ELT (condv, i) = cond;
8277 TREE_VEC_ELT (incrv, i) = incr;
8278 }
8279
8280 parse_next:
8281 if (i == collapse - 1)
8282 break;
8283
8284 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8285 in between the collapsed for loops to be still considered perfectly
8286 nested. Hopefully the final version clarifies this.
8287 For now handle (multiple) {'s and empty statements. */
8288 do
8289 {
8290 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8291 {
8292 c_parser_consume_token (parser);
8293 break;
8294 }
8295 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8296 {
8297 c_parser_consume_token (parser);
8298 bracecount++;
8299 }
8300 else if (bracecount
8301 && c_parser_next_token_is (parser, CPP_SEMICOLON))
8302 c_parser_consume_token (parser);
8303 else
8304 {
8305 c_parser_error (parser, "not enough perfectly nested loops");
8306 if (bracecount)
8307 {
8308 open_brace_parsed = true;
8309 bracecount--;
8310 }
8311 fail = true;
8312 collapse = 0;
8313 break;
8314 }
8315 }
8316 while (1);
8317
8318 nbraces += bracecount;
8319 }
953ff289 8320
953ff289
DN
8321 save_break = c_break_label;
8322 c_break_label = size_one_node;
8323 save_cont = c_cont_label;
8324 c_cont_label = NULL_TREE;
8325 body = push_stmt_list ();
8326
a68ab351
JJ
8327 if (open_brace_parsed)
8328 {
c2255bc4 8329 location_t here = c_parser_peek_token (parser)->location;
a68ab351
JJ
8330 stmt = c_begin_compound_stmt (true);
8331 c_parser_compound_statement_nostart (parser);
c2255bc4 8332 add_stmt (c_end_compound_stmt (here, stmt, true));
a68ab351
JJ
8333 }
8334 else
8335 add_stmt (c_parser_c99_block_statement (parser));
953ff289 8336 if (c_cont_label)
c2255bc4
AH
8337 {
8338 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8339 SET_EXPR_LOCATION (t, loc);
8340 add_stmt (t);
8341 }
953ff289
DN
8342
8343 body = pop_stmt_list (body);
8344 c_break_label = save_break;
8345 c_cont_label = save_cont;
8346
a68ab351
JJ
8347 while (nbraces)
8348 {
8349 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8350 {
8351 c_parser_consume_token (parser);
8352 nbraces--;
8353 }
8354 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8355 c_parser_consume_token (parser);
8356 else
8357 {
8358 c_parser_error (parser, "collapsed loops not perfectly nested");
8359 while (nbraces)
8360 {
c2255bc4 8361 location_t here = c_parser_peek_token (parser)->location;
a68ab351
JJ
8362 stmt = c_begin_compound_stmt (true);
8363 add_stmt (body);
8364 c_parser_compound_statement_nostart (parser);
c2255bc4 8365 body = c_end_compound_stmt (here, stmt, true);
a68ab351
JJ
8366 nbraces--;
8367 }
8368 goto pop_scopes;
8369 }
8370 }
8371
61c3a446 8372 /* Only bother calling c_finish_omp_for if we haven't already generated
953ff289 8373 an error from the initialization parsing. */
a68ab351
JJ
8374 if (!fail)
8375 {
8376 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8377 if (stmt)
8378 {
8379 if (par_clauses != NULL)
8380 {
8381 tree *c;
8382 for (c = par_clauses; *c ; )
8383 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8384 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8385 c = &OMP_CLAUSE_CHAIN (*c);
8386 else
8387 {
8388 for (i = 0; i < collapse; i++)
8389 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8390 break;
8391 if (i == collapse)
8392 c = &OMP_CLAUSE_CHAIN (*c);
8393 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8394 {
3ba09659
AH
8395 error_at (loc,
8396 "iteration variable %qD should not be firstprivate",
8397 OMP_CLAUSE_DECL (*c));
a68ab351
JJ
8398 *c = OMP_CLAUSE_CHAIN (*c);
8399 }
8400 else
8401 {
8402 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8403 change it to shared (decl) in
8404 OMP_PARALLEL_CLAUSES. */
c2255bc4
AH
8405 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8406 OMP_CLAUSE_LASTPRIVATE);
a68ab351
JJ
8407 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8408 OMP_CLAUSE_CHAIN (l) = clauses;
8409 clauses = l;
8410 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8411 }
8412 }
8413 }
8414 OMP_FOR_CLAUSES (stmt) = clauses;
8415 }
8416 ret = stmt;
8417 }
8418pop_scopes:
8419 while (for_block)
8420 {
c2255bc4
AH
8421 /* FIXME diagnostics: LOC below should be the actual location of
8422 this particular for block. We need to build a list of
8423 locations to go along with FOR_BLOCK. */
8424 stmt = c_end_compound_stmt (loc, TREE_VALUE (for_block), true);
a68ab351
JJ
8425 add_stmt (stmt);
8426 for_block = TREE_CHAIN (for_block);
8427 }
8428 return ret;
953ff289
DN
8429}
8430
8431/* OpenMP 2.5:
8432 #pragma omp for for-clause[optseq] new-line
8433 for-loop
c2255bc4
AH
8434
8435 LOC is the location of the #pragma token.
953ff289
DN
8436*/
8437
8438#define OMP_FOR_CLAUSE_MASK \
8439 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8440 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8441 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8442 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8443 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8444 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
a68ab351 8445 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
953ff289
DN
8446 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8447
8448static tree
c2255bc4 8449c_parser_omp_for (location_t loc, c_parser *parser)
953ff289
DN
8450{
8451 tree block, clauses, ret;
8452
8453 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8454 "#pragma omp for");
8455
8456 block = c_begin_compound_stmt (true);
c2255bc4
AH
8457 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8458 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
8459 add_stmt (block);
8460
8461 return ret;
8462}
8463
8464/* OpenMP 2.5:
8465 # pragma omp master new-line
8466 structured-block
c2255bc4
AH
8467
8468 LOC is the location of the #pragma token.
953ff289
DN
8469*/
8470
8471static tree
c2255bc4 8472c_parser_omp_master (location_t loc, c_parser *parser)
953ff289
DN
8473{
8474 c_parser_skip_to_pragma_eol (parser);
c2255bc4 8475 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
953ff289
DN
8476}
8477
8478/* OpenMP 2.5:
8479 # pragma omp ordered new-line
8480 structured-block
c2255bc4
AH
8481
8482 LOC is the location of the #pragma itself.
953ff289
DN
8483*/
8484
8485static tree
c2255bc4 8486c_parser_omp_ordered (location_t loc, c_parser *parser)
953ff289
DN
8487{
8488 c_parser_skip_to_pragma_eol (parser);
c2255bc4 8489 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
953ff289
DN
8490}
8491
8492/* OpenMP 2.5:
8493
8494 section-scope:
8495 { section-sequence }
8496
8497 section-sequence:
8498 section-directive[opt] structured-block
b8698a0f 8499 section-sequence section-directive structured-block
c2255bc4
AH
8500
8501 SECTIONS_LOC is the location of the #pragma omp sections. */
953ff289
DN
8502
8503static tree
c2255bc4 8504c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
953ff289
DN
8505{
8506 tree stmt, substmt;
8507 bool error_suppress = false;
8508 location_t loc;
8509
c2255bc4 8510 loc = c_parser_peek_token (parser)->location;
953ff289
DN
8511 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8512 {
8513 /* Avoid skipping until the end of the block. */
8514 parser->error = false;
8515 return NULL_TREE;
8516 }
8517
8518 stmt = push_stmt_list ();
8519
953ff289
DN
8520 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8521 {
8522 substmt = push_stmt_list ();
8523
8524 while (1)
8525 {
8526 c_parser_statement (parser);
8527
8528 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8529 break;
8530 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8531 break;
8532 if (c_parser_next_token_is (parser, CPP_EOF))
8533 break;
8534 }
8535
8536 substmt = pop_stmt_list (substmt);
8537 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8538 SET_EXPR_LOCATION (substmt, loc);
8539 add_stmt (substmt);
8540 }
8541
8542 while (1)
8543 {
8544 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8545 break;
8546 if (c_parser_next_token_is (parser, CPP_EOF))
8547 break;
8548
8549 loc = c_parser_peek_token (parser)->location;
8550 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8551 {
8552 c_parser_consume_pragma (parser);
8553 c_parser_skip_to_pragma_eol (parser);
8554 error_suppress = false;
8555 }
8556 else if (!error_suppress)
8557 {
3ba09659 8558 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
953ff289
DN
8559 error_suppress = true;
8560 }
8561
8562 substmt = c_parser_omp_structured_block (parser);
8563 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8564 SET_EXPR_LOCATION (substmt, loc);
8565 add_stmt (substmt);
8566 }
8567 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8568 "expected %<#pragma omp section%> or %<}%>");
8569
8570 substmt = pop_stmt_list (stmt);
8571
8572 stmt = make_node (OMP_SECTIONS);
c2255bc4 8573 SET_EXPR_LOCATION (stmt, sections_loc);
953ff289
DN
8574 TREE_TYPE (stmt) = void_type_node;
8575 OMP_SECTIONS_BODY (stmt) = substmt;
8576
8577 return add_stmt (stmt);
8578}
8579
8580/* OpenMP 2.5:
8581 # pragma omp sections sections-clause[optseq] newline
8582 sections-scope
c2255bc4
AH
8583
8584 LOC is the location of the #pragma token.
953ff289
DN
8585*/
8586
8587#define OMP_SECTIONS_CLAUSE_MASK \
8588 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8589 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8590 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8591 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8592 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8593
8594static tree
c2255bc4 8595c_parser_omp_sections (location_t loc, c_parser *parser)
953ff289
DN
8596{
8597 tree block, clauses, ret;
8598
8599 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8600 "#pragma omp sections");
8601
8602 block = c_begin_compound_stmt (true);
c2255bc4 8603 ret = c_parser_omp_sections_scope (loc, parser);
953ff289
DN
8604 if (ret)
8605 OMP_SECTIONS_CLAUSES (ret) = clauses;
c2255bc4 8606 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
8607 add_stmt (block);
8608
8609 return ret;
8610}
8611
8612/* OpenMP 2.5:
8613 # pragma parallel parallel-clause new-line
8614 # pragma parallel for parallel-for-clause new-line
8615 # pragma parallel sections parallel-sections-clause new-line
c2255bc4
AH
8616
8617 LOC is the location of the #pragma token.
953ff289
DN
8618*/
8619
8620#define OMP_PARALLEL_CLAUSE_MASK \
8621 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8622 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8623 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8624 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8625 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8626 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8627 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8628 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8629
8630static tree
c2255bc4 8631c_parser_omp_parallel (location_t loc, c_parser *parser)
953ff289
DN
8632{
8633 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8634 const char *p_name = "#pragma omp parallel";
8635 tree stmt, clauses, par_clause, ws_clause, block;
8636 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8637
8638 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8639 {
8640 c_parser_consume_token (parser);
8641 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8642 p_name = "#pragma omp parallel for";
8643 mask |= OMP_FOR_CLAUSE_MASK;
8644 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8645 }
8646 else if (c_parser_next_token_is (parser, CPP_NAME))
8647 {
8648 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8649 if (strcmp (p, "sections") == 0)
8650 {
8651 c_parser_consume_token (parser);
8652 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8653 p_name = "#pragma omp parallel sections";
8654 mask |= OMP_SECTIONS_CLAUSE_MASK;
8655 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8656 }
8657 }
8658
8659 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8660
8661 switch (p_kind)
8662 {
8663 case PRAGMA_OMP_PARALLEL:
8664 block = c_begin_omp_parallel ();
8665 c_parser_statement (parser);
c2255bc4 8666 stmt = c_finish_omp_parallel (loc, clauses, block);
953ff289
DN
8667 break;
8668
8669 case PRAGMA_OMP_PARALLEL_FOR:
8670 block = c_begin_omp_parallel ();
c2255bc4
AH
8671 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8672 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
8673 stmt = c_finish_omp_parallel (loc, par_clause, block);
761041be 8674 OMP_PARALLEL_COMBINED (stmt) = 1;
953ff289
DN
8675 break;
8676
8677 case PRAGMA_OMP_PARALLEL_SECTIONS:
8678 block = c_begin_omp_parallel ();
c2255bc4
AH
8679 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8680 stmt = c_parser_omp_sections_scope (loc, parser);
953ff289
DN
8681 if (stmt)
8682 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
c2255bc4 8683 stmt = c_finish_omp_parallel (loc, par_clause, block);
761041be 8684 OMP_PARALLEL_COMBINED (stmt) = 1;
953ff289
DN
8685 break;
8686
8687 default:
8688 gcc_unreachable ();
8689 }
8690
8691 return stmt;
8692}
8693
8694/* OpenMP 2.5:
8695 # pragma omp single single-clause[optseq] new-line
8696 structured-block
c2255bc4
AH
8697
8698 LOC is the location of the #pragma.
953ff289
DN
8699*/
8700
8701#define OMP_SINGLE_CLAUSE_MASK \
8702 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8703 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8704 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8705 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8706
8707static tree
c2255bc4 8708c_parser_omp_single (location_t loc, c_parser *parser)
953ff289
DN
8709{
8710 tree stmt = make_node (OMP_SINGLE);
c2255bc4 8711 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
8712 TREE_TYPE (stmt) = void_type_node;
8713
8714 OMP_SINGLE_CLAUSES (stmt)
8715 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8716 "#pragma omp single");
8717 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8718
8719 return add_stmt (stmt);
8720}
8721
a68ab351
JJ
8722/* OpenMP 3.0:
8723 # pragma omp task task-clause[optseq] new-line
c2255bc4
AH
8724
8725 LOC is the location of the #pragma.
a68ab351
JJ
8726*/
8727
8728#define OMP_TASK_CLAUSE_MASK \
8729 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8730 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8731 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8732 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8733 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8734 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8735
8736static tree
c2255bc4 8737c_parser_omp_task (location_t loc, c_parser *parser)
a68ab351
JJ
8738{
8739 tree clauses, block;
8740
8741 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8742 "#pragma omp task");
8743
8744 block = c_begin_omp_task ();
8745 c_parser_statement (parser);
c2255bc4 8746 return c_finish_omp_task (loc, clauses, block);
a68ab351
JJ
8747}
8748
8749/* OpenMP 3.0:
8750 # pragma omp taskwait new-line
8751*/
8752
8753static void
8754c_parser_omp_taskwait (c_parser *parser)
8755{
c2255bc4 8756 location_t loc = c_parser_peek_token (parser)->location;
a68ab351
JJ
8757 c_parser_consume_pragma (parser);
8758 c_parser_skip_to_pragma_eol (parser);
8759
c2255bc4 8760 c_finish_omp_taskwait (loc);
a68ab351 8761}
953ff289
DN
8762
8763/* Main entry point to parsing most OpenMP pragmas. */
8764
8765static void
8766c_parser_omp_construct (c_parser *parser)
8767{
8768 enum pragma_kind p_kind;
8769 location_t loc;
8770 tree stmt;
8771
8772 loc = c_parser_peek_token (parser)->location;
8773 p_kind = c_parser_peek_token (parser)->pragma_kind;
8774 c_parser_consume_pragma (parser);
8775
8776 switch (p_kind)
8777 {
8778 case PRAGMA_OMP_ATOMIC:
c2255bc4 8779 c_parser_omp_atomic (loc, parser);
953ff289
DN
8780 return;
8781 case PRAGMA_OMP_CRITICAL:
c2255bc4 8782 stmt = c_parser_omp_critical (loc, parser);
953ff289
DN
8783 break;
8784 case PRAGMA_OMP_FOR:
c2255bc4 8785 stmt = c_parser_omp_for (loc, parser);
953ff289
DN
8786 break;
8787 case PRAGMA_OMP_MASTER:
c2255bc4 8788 stmt = c_parser_omp_master (loc, parser);
953ff289
DN
8789 break;
8790 case PRAGMA_OMP_ORDERED:
c2255bc4 8791 stmt = c_parser_omp_ordered (loc, parser);
953ff289
DN
8792 break;
8793 case PRAGMA_OMP_PARALLEL:
c2255bc4 8794 stmt = c_parser_omp_parallel (loc, parser);
953ff289
DN
8795 break;
8796 case PRAGMA_OMP_SECTIONS:
c2255bc4 8797 stmt = c_parser_omp_sections (loc, parser);
953ff289
DN
8798 break;
8799 case PRAGMA_OMP_SINGLE:
c2255bc4 8800 stmt = c_parser_omp_single (loc, parser);
953ff289 8801 break;
a68ab351 8802 case PRAGMA_OMP_TASK:
c2255bc4 8803 stmt = c_parser_omp_task (loc, parser);
a68ab351 8804 break;
953ff289
DN
8805 default:
8806 gcc_unreachable ();
8807 }
8808
8809 if (stmt)
c2255bc4 8810 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
953ff289
DN
8811}
8812
8813
8814/* OpenMP 2.5:
8815 # pragma omp threadprivate (variable-list) */
8816
8817static void
8818c_parser_omp_threadprivate (c_parser *parser)
8819{
8820 tree vars, t;
c2255bc4 8821 location_t loc;
953ff289
DN
8822
8823 c_parser_consume_pragma (parser);
c2255bc4 8824 loc = c_parser_peek_token (parser)->location;
d75d71e0 8825 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
953ff289 8826
953ff289
DN
8827 /* Mark every variable in VARS to be assigned thread local storage. */
8828 for (t = vars; t; t = TREE_CHAIN (t))
8829 {
8830 tree v = TREE_PURPOSE (t);
8831
c2255bc4
AH
8832 /* FIXME diagnostics: Ideally we should keep individual
8833 locations for all the variables in the var list to make the
8834 following errors more precise. Perhaps
8835 c_parser_omp_var_list_parens() should construct a list of
8836 locations to go along with the var list. */
8837
953ff289
DN
8838 /* If V had already been marked threadprivate, it doesn't matter
8839 whether it had been used prior to this point. */
5df27e4a 8840 if (TREE_CODE (v) != VAR_DECL)
c2255bc4 8841 error_at (loc, "%qD is not a variable", v);
5df27e4a 8842 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
c2255bc4 8843 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
953ff289 8844 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
c2255bc4 8845 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
5df27e4a
JJ
8846 else if (TREE_TYPE (v) == error_mark_node)
8847 ;
953ff289 8848 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
c2255bc4 8849 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
953ff289
DN
8850 else
8851 {
8852 if (! DECL_THREAD_LOCAL_P (v))
8853 {
8854 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8855 /* If rtl has been already set for this var, call
8856 make_decl_rtl once again, so that encode_section_info
8857 has a chance to look at the new decl flags. */
8858 if (DECL_RTL_SET_P (v))
8859 make_decl_rtl (v);
8860 }
8861 C_DECL_THREADPRIVATE_P (v) = 1;
8862 }
8863 }
8864
8865 c_parser_skip_to_pragma_eol (parser);
8866}
8867
bc4071dd 8868\f
27bf414c
JM
8869/* Parse a single source file. */
8870
8871void
8872c_parse_file (void)
8873{
bc4071dd
RH
8874 /* Use local storage to begin. If the first token is a pragma, parse it.
8875 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8876 which will cause garbage collection. */
8877 c_parser tparser;
8878
8879 memset (&tparser, 0, sizeof tparser);
8880 the_parser = &tparser;
8881
8882 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8883 c_parser_pragma_pch_preprocess (&tparser);
8884
8885 the_parser = GGC_NEW (c_parser);
8886 *the_parser = tparser;
8887
f9417da1
RG
8888 /* Initialize EH, if we've been told to do so. */
8889 if (flag_exceptions)
1d65f45c 8890 using_eh_for_cleanups ();
f9417da1 8891
27bf414c
JM
8892 c_parser_translation_unit (the_parser);
8893 the_parser = NULL;
8894}
8895
8896#include "gt-c-parser.h"
This page took 2.397309 seconds and 5 git commands to generate.