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