2012-03-17 Janne Blomqvist <jb@gcc.gnu.org>
[official-gcc.git] / gcc / c-parser.c
blob0d6f7a430563a8daa2b1ff9b3c690f7b0cef9ad6
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO:
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.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h" /* For rtl.h: needs enum reg_class. */
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-family/c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "ggc.h"
54 #include "c-family/c-common.h"
55 #include "c-family/c-objc.h"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
59 #include "plugin.h"
62 /* Initialization routine for this file. */
64 void
65 c_parse_init (void)
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
71 int mask = 0;
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);
77 mask |= D_CXXONLY;
78 if (!flag_isoc99)
79 mask |= D_C99;
80 if (flag_no_asm)
82 mask |= D_ASM | D_EXT;
83 if (!flag_isoc99)
84 mask |= D_EXT89;
86 if (!c_dialect_objc ())
87 mask |= D_OBJC | D_CXX_OBJC;
89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90 for (i = 0; i < num_c_common_reswords; i++)
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. */
94 if (c_common_reswords[i].disable & mask)
96 if (warn_cxx_compat
97 && (c_common_reswords[i].disable & D_CXXWARN))
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;
103 continue;
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
108 C_IS_RESERVED_WORD (id) = 1;
109 ridpointers [(int) c_common_reswords[i].rid] = id;
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.
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. */
124 /* The following local token type is used. */
126 /* A keyword. */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
129 /* More information about the type of a CPP_NAME token. */
130 typedef 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,
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
139 /* Not an identifier. */
140 C_ID_NONE
141 } c_id_kind;
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
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;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
158 location_t location;
159 /* The value associated with this token, if any. */
160 tree value;
161 } c_token;
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. */
166 typedef struct GTY(()) c_parser {
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;
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
183 /* Objective-C specific parser/lexer information. */
185 /* True if we are in a context where the Objective-C "PQ" keywords
186 are considered keywords. */
187 BOOL_BITFIELD objc_pq_context : 1;
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;
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;
198 /* Nonzero if we're processing a __transaction statement. The value
199 is 1 | TM_STMT_ATTR_*. */
200 unsigned int in_transaction : 4;
201 /* True if we are in a context where the Objective-C "Property attribute"
202 keywords are valid. */
203 BOOL_BITFIELD objc_property_attr_context : 1;
204 } c_parser;
207 /* The actual parser and external interface. ??? Does this need to be
208 garbage-collected? */
210 static GTY (()) c_parser *the_parser;
212 /* Read in and lex a single token, storing it in *TOKEN. */
214 static void
215 c_lex_one_token (c_parser *parser, c_token *token)
217 timevar_push (TV_LEX);
219 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
220 (parser->lex_untranslated_string
221 ? C_LEX_STRING_NO_TRANSLATE : 0));
222 token->id_kind = C_ID_NONE;
223 token->keyword = RID_MAX;
224 token->pragma_kind = PRAGMA_NONE;
226 switch (token->type)
228 case CPP_NAME:
230 tree decl;
232 bool objc_force_identifier = parser->objc_need_raw_identifier;
233 if (c_dialect_objc ())
234 parser->objc_need_raw_identifier = false;
236 if (C_IS_RESERVED_WORD (token->value))
238 enum rid rid_code = C_RID_CODE (token->value);
240 if (rid_code == RID_CXX_COMPAT_WARN)
242 warning_at (token->location,
243 OPT_Wc___compat,
244 "identifier %qE conflicts with C++ keyword",
245 token->value);
247 else if (rid_code >= RID_FIRST_ADDR_SPACE
248 && rid_code <= RID_LAST_ADDR_SPACE)
250 token->id_kind = C_ID_ADDRSPACE;
251 token->keyword = rid_code;
252 break;
254 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
256 /* We found an Objective-C "pq" keyword (in, out,
257 inout, bycopy, byref, oneway). They need special
258 care because the interpretation depends on the
259 context. */
260 if (parser->objc_pq_context)
262 token->type = CPP_KEYWORD;
263 token->keyword = rid_code;
264 break;
266 else if (parser->objc_could_be_foreach_context
267 && rid_code == RID_IN)
269 /* We are in Objective-C, inside a (potential)
270 foreach context (which means after having
271 parsed 'for (', but before having parsed ';'),
272 and we found 'in'. We consider it the keyword
273 which terminates the declaration at the
274 beginning of a foreach-statement. Note that
275 this means you can't use 'in' for anything else
276 in that context; in particular, in Objective-C
277 you can't use 'in' as the name of the running
278 variable in a C for loop. We could potentially
279 try to add code here to disambiguate, but it
280 seems a reasonable limitation. */
281 token->type = CPP_KEYWORD;
282 token->keyword = rid_code;
283 break;
285 /* Else, "pq" keywords outside of the "pq" context are
286 not keywords, and we fall through to the code for
287 normal tokens. */
289 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
291 /* We found an Objective-C "property attribute"
292 keyword (getter, setter, readonly, etc). These are
293 only valid in the property context. */
294 if (parser->objc_property_attr_context)
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
298 break;
300 /* Else they are not special keywords.
303 else if (c_dialect_objc ()
304 && (OBJC_IS_AT_KEYWORD (rid_code)
305 || OBJC_IS_CXX_KEYWORD (rid_code)))
307 /* We found one of the Objective-C "@" keywords (defs,
308 selector, synchronized, etc) or one of the
309 Objective-C "cxx" keywords (class, private,
310 protected, public, try, catch, throw) without a
311 preceding '@' sign. Do nothing and fall through to
312 the code for normal tokens (in C++ we would still
313 consider the CXX ones keywords, but not in C). */
316 else
318 token->type = CPP_KEYWORD;
319 token->keyword = rid_code;
320 break;
324 decl = lookup_name (token->value);
325 if (decl)
327 if (TREE_CODE (decl) == TYPE_DECL)
329 token->id_kind = C_ID_TYPENAME;
330 break;
333 else if (c_dialect_objc ())
335 tree objc_interface_decl = objc_is_class_name (token->value);
336 /* Objective-C class names are in the same namespace as
337 variables and typedefs, and hence are shadowed by local
338 declarations. */
339 if (objc_interface_decl
340 && (!objc_force_identifier || global_bindings_p ()))
342 token->value = objc_interface_decl;
343 token->id_kind = C_ID_CLASSNAME;
344 break;
347 token->id_kind = C_ID_ID;
349 break;
350 case CPP_AT_NAME:
351 /* This only happens in Objective-C; it must be a keyword. */
352 token->type = CPP_KEYWORD;
353 switch (C_RID_CODE (token->value))
355 /* Replace 'class' with '@class', 'private' with '@private',
356 etc. This prevents confusion with the C++ keyword
357 'class', and makes the tokens consistent with other
358 Objective-C 'AT' keywords. For example '@class' is
359 reported as RID_AT_CLASS which is consistent with
360 '@synchronized', which is reported as
361 RID_AT_SYNCHRONIZED.
363 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
364 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
365 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
366 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
367 case RID_THROW: token->keyword = RID_AT_THROW; break;
368 case RID_TRY: token->keyword = RID_AT_TRY; break;
369 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
370 default: token->keyword = C_RID_CODE (token->value);
372 break;
373 case CPP_COLON:
374 case CPP_COMMA:
375 case CPP_CLOSE_PAREN:
376 case CPP_SEMICOLON:
377 /* These tokens may affect the interpretation of any identifiers
378 following, if doing Objective-C. */
379 if (c_dialect_objc ())
380 parser->objc_need_raw_identifier = false;
381 break;
382 case CPP_PRAGMA:
383 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
384 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
385 token->value = NULL;
386 break;
387 default:
388 break;
390 timevar_pop (TV_LEX);
393 /* Return a pointer to the next token from PARSER, reading it in if
394 necessary. */
396 static inline c_token *
397 c_parser_peek_token (c_parser *parser)
399 if (parser->tokens_avail == 0)
401 c_lex_one_token (parser, &parser->tokens[0]);
402 parser->tokens_avail = 1;
404 return &parser->tokens[0];
407 /* Return true if the next token from PARSER has the indicated
408 TYPE. */
410 static inline bool
411 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
413 return c_parser_peek_token (parser)->type == type;
416 /* Return true if the next token from PARSER does not have the
417 indicated TYPE. */
419 static inline bool
420 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
422 return !c_parser_next_token_is (parser, type);
425 /* Return true if the next token from PARSER is the indicated
426 KEYWORD. */
428 static inline bool
429 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
431 return c_parser_peek_token (parser)->keyword == keyword;
434 /* Return a pointer to the next-but-one token from PARSER, reading it
435 in if necessary. The next token is already read in. */
437 static c_token *
438 c_parser_peek_2nd_token (c_parser *parser)
440 if (parser->tokens_avail >= 2)
441 return &parser->tokens[1];
442 gcc_assert (parser->tokens_avail == 1);
443 gcc_assert (parser->tokens[0].type != CPP_EOF);
444 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
445 c_lex_one_token (parser, &parser->tokens[1]);
446 parser->tokens_avail = 2;
447 return &parser->tokens[1];
450 /* Return true if TOKEN can start a type name,
451 false otherwise. */
452 static bool
453 c_token_starts_typename (c_token *token)
455 switch (token->type)
457 case CPP_NAME:
458 switch (token->id_kind)
460 case C_ID_ID:
461 return false;
462 case C_ID_ADDRSPACE:
463 return true;
464 case C_ID_TYPENAME:
465 return true;
466 case C_ID_CLASSNAME:
467 gcc_assert (c_dialect_objc ());
468 return true;
469 default:
470 gcc_unreachable ();
472 case CPP_KEYWORD:
473 switch (token->keyword)
475 case RID_UNSIGNED:
476 case RID_LONG:
477 case RID_INT128:
478 case RID_SHORT:
479 case RID_SIGNED:
480 case RID_COMPLEX:
481 case RID_INT:
482 case RID_CHAR:
483 case RID_FLOAT:
484 case RID_DOUBLE:
485 case RID_VOID:
486 case RID_DFLOAT32:
487 case RID_DFLOAT64:
488 case RID_DFLOAT128:
489 case RID_BOOL:
490 case RID_ENUM:
491 case RID_STRUCT:
492 case RID_UNION:
493 case RID_TYPEOF:
494 case RID_CONST:
495 case RID_VOLATILE:
496 case RID_RESTRICT:
497 case RID_ATTRIBUTE:
498 case RID_FRACT:
499 case RID_ACCUM:
500 case RID_SAT:
501 return true;
502 default:
503 return false;
505 case CPP_LESS:
506 if (c_dialect_objc ())
507 return true;
508 return false;
509 default:
510 return false;
514 enum c_lookahead_kind {
515 /* Always treat unknown identifiers as typenames. */
516 cla_prefer_type,
518 /* Could be parsing a nonabstract declarator. Only treat an identifier
519 as a typename if followed by another identifier or a star. */
520 cla_nonabstract_decl,
522 /* Never treat identifiers as typenames. */
523 cla_prefer_id
526 /* Return true if the next token from PARSER can start a type name,
527 false otherwise. LA specifies how to do lookahead in order to
528 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
530 static inline bool
531 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
533 c_token *token = c_parser_peek_token (parser);
534 if (c_token_starts_typename (token))
535 return true;
537 /* Try a bit harder to detect an unknown typename. */
538 if (la != cla_prefer_id
539 && token->type == CPP_NAME
540 && token->id_kind == C_ID_ID
542 /* Do not try too hard when we could have "object in array". */
543 && !parser->objc_could_be_foreach_context
545 && (la == cla_prefer_type
546 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
547 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
549 /* Only unknown identifiers. */
550 && !lookup_name (token->value))
551 return true;
553 return false;
556 /* Return true if TOKEN is a type qualifier, false otherwise. */
557 static bool
558 c_token_is_qualifier (c_token *token)
560 switch (token->type)
562 case CPP_NAME:
563 switch (token->id_kind)
565 case C_ID_ADDRSPACE:
566 return true;
567 default:
568 return false;
570 case CPP_KEYWORD:
571 switch (token->keyword)
573 case RID_CONST:
574 case RID_VOLATILE:
575 case RID_RESTRICT:
576 case RID_ATTRIBUTE:
577 return true;
578 default:
579 return false;
581 case CPP_LESS:
582 return false;
583 default:
584 gcc_unreachable ();
588 /* Return true if the next token from PARSER is a type qualifier,
589 false otherwise. */
590 static inline bool
591 c_parser_next_token_is_qualifier (c_parser *parser)
593 c_token *token = c_parser_peek_token (parser);
594 return c_token_is_qualifier (token);
597 /* Return true if TOKEN can start declaration specifiers, false
598 otherwise. */
599 static bool
600 c_token_starts_declspecs (c_token *token)
602 switch (token->type)
604 case CPP_NAME:
605 switch (token->id_kind)
607 case C_ID_ID:
608 return false;
609 case C_ID_ADDRSPACE:
610 return true;
611 case C_ID_TYPENAME:
612 return true;
613 case C_ID_CLASSNAME:
614 gcc_assert (c_dialect_objc ());
615 return true;
616 default:
617 gcc_unreachable ();
619 case CPP_KEYWORD:
620 switch (token->keyword)
622 case RID_STATIC:
623 case RID_EXTERN:
624 case RID_REGISTER:
625 case RID_TYPEDEF:
626 case RID_INLINE:
627 case RID_NORETURN:
628 case RID_AUTO:
629 case RID_THREAD:
630 case RID_UNSIGNED:
631 case RID_LONG:
632 case RID_INT128:
633 case RID_SHORT:
634 case RID_SIGNED:
635 case RID_COMPLEX:
636 case RID_INT:
637 case RID_CHAR:
638 case RID_FLOAT:
639 case RID_DOUBLE:
640 case RID_VOID:
641 case RID_DFLOAT32:
642 case RID_DFLOAT64:
643 case RID_DFLOAT128:
644 case RID_BOOL:
645 case RID_ENUM:
646 case RID_STRUCT:
647 case RID_UNION:
648 case RID_TYPEOF:
649 case RID_CONST:
650 case RID_VOLATILE:
651 case RID_RESTRICT:
652 case RID_ATTRIBUTE:
653 case RID_FRACT:
654 case RID_ACCUM:
655 case RID_SAT:
656 case RID_ALIGNAS:
657 return true;
658 default:
659 return false;
661 case CPP_LESS:
662 if (c_dialect_objc ())
663 return true;
664 return false;
665 default:
666 return false;
671 /* Return true if TOKEN can start declaration specifiers or a static
672 assertion, false otherwise. */
673 static bool
674 c_token_starts_declaration (c_token *token)
676 if (c_token_starts_declspecs (token)
677 || token->keyword == RID_STATIC_ASSERT)
678 return true;
679 else
680 return false;
683 /* Return true if the next token from PARSER can start declaration
684 specifiers, false otherwise. */
685 static inline bool
686 c_parser_next_token_starts_declspecs (c_parser *parser)
688 c_token *token = c_parser_peek_token (parser);
690 /* In Objective-C, a classname normally starts a declspecs unless it
691 is immediately followed by a dot. In that case, it is the
692 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
693 setter/getter on the class. c_token_starts_declspecs() can't
694 differentiate between the two cases because it only checks the
695 current token, so we have a special check here. */
696 if (c_dialect_objc ()
697 && token->type == CPP_NAME
698 && token->id_kind == C_ID_CLASSNAME
699 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
700 return false;
702 return c_token_starts_declspecs (token);
705 /* Return true if the next tokens from PARSER can start declaration
706 specifiers or a static assertion, false otherwise. */
707 static inline bool
708 c_parser_next_tokens_start_declaration (c_parser *parser)
710 c_token *token = c_parser_peek_token (parser);
712 /* Same as above. */
713 if (c_dialect_objc ()
714 && token->type == CPP_NAME
715 && token->id_kind == C_ID_CLASSNAME
716 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
717 return false;
719 /* Labels do not start declarations. */
720 if (token->type == CPP_NAME
721 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
722 return false;
724 if (c_token_starts_declaration (token))
725 return true;
727 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
728 return true;
730 return false;
733 /* Consume the next token from PARSER. */
735 static void
736 c_parser_consume_token (c_parser *parser)
738 gcc_assert (parser->tokens_avail >= 1);
739 gcc_assert (parser->tokens[0].type != CPP_EOF);
740 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
741 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
742 if (parser->tokens_avail == 2)
743 parser->tokens[0] = parser->tokens[1];
744 parser->tokens_avail--;
747 /* Expect the current token to be a #pragma. Consume it and remember
748 that we've begun parsing a pragma. */
750 static void
751 c_parser_consume_pragma (c_parser *parser)
753 gcc_assert (!parser->in_pragma);
754 gcc_assert (parser->tokens_avail >= 1);
755 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
756 if (parser->tokens_avail == 2)
757 parser->tokens[0] = parser->tokens[1];
758 parser->tokens_avail--;
759 parser->in_pragma = true;
762 /* Update the globals input_location and in_system_header from
763 TOKEN. */
764 static inline void
765 c_parser_set_source_position_from_token (c_token *token)
767 if (token->type != CPP_EOF)
769 input_location = token->location;
773 /* Issue a diagnostic of the form
774 FILE:LINE: MESSAGE before TOKEN
775 where TOKEN is the next token in the input stream of PARSER.
776 MESSAGE (specified by the caller) is usually of the form "expected
777 OTHER-TOKEN".
779 Do not issue a diagnostic if still recovering from an error.
781 ??? This is taken from the C++ parser, but building up messages in
782 this way is not i18n-friendly and some other approach should be
783 used. */
785 static void
786 c_parser_error (c_parser *parser, const char *gmsgid)
788 c_token *token = c_parser_peek_token (parser);
789 if (parser->error)
790 return;
791 parser->error = true;
792 if (!gmsgid)
793 return;
794 /* This diagnostic makes more sense if it is tagged to the line of
795 the token we just peeked at. */
796 c_parser_set_source_position_from_token (token);
797 c_parse_error (gmsgid,
798 /* Because c_parse_error does not understand
799 CPP_KEYWORD, keywords are treated like
800 identifiers. */
801 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
802 /* ??? The C parser does not save the cpp flags of a
803 token, we need to pass 0 here and we will not get
804 the source spelling of some tokens but rather the
805 canonical spelling. */
806 token->value, /*flags=*/0);
809 /* If the next token is of the indicated TYPE, consume it. Otherwise,
810 issue the error MSGID. If MSGID is NULL then a message has already
811 been produced and no message will be produced this time. Returns
812 true if found, false otherwise. */
814 static bool
815 c_parser_require (c_parser *parser,
816 enum cpp_ttype type,
817 const char *msgid)
819 if (c_parser_next_token_is (parser, type))
821 c_parser_consume_token (parser);
822 return true;
824 else
826 c_parser_error (parser, msgid);
827 return false;
831 /* If the next token is the indicated keyword, consume it. Otherwise,
832 issue the error MSGID. Returns true if found, false otherwise. */
834 static bool
835 c_parser_require_keyword (c_parser *parser,
836 enum rid keyword,
837 const char *msgid)
839 if (c_parser_next_token_is_keyword (parser, keyword))
841 c_parser_consume_token (parser);
842 return true;
844 else
846 c_parser_error (parser, msgid);
847 return false;
851 /* Like c_parser_require, except that tokens will be skipped until the
852 desired token is found. An error message is still produced if the
853 next token is not as expected. If MSGID is NULL then a message has
854 already been produced and no message will be produced this
855 time. */
857 static void
858 c_parser_skip_until_found (c_parser *parser,
859 enum cpp_ttype type,
860 const char *msgid)
862 unsigned nesting_depth = 0;
864 if (c_parser_require (parser, type, msgid))
865 return;
867 /* Skip tokens until the desired token is found. */
868 while (true)
870 /* Peek at the next token. */
871 c_token *token = c_parser_peek_token (parser);
872 /* If we've reached the token we want, consume it and stop. */
873 if (token->type == type && !nesting_depth)
875 c_parser_consume_token (parser);
876 break;
879 /* If we've run out of tokens, stop. */
880 if (token->type == CPP_EOF)
881 return;
882 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
883 return;
884 if (token->type == CPP_OPEN_BRACE
885 || token->type == CPP_OPEN_PAREN
886 || token->type == CPP_OPEN_SQUARE)
887 ++nesting_depth;
888 else if (token->type == CPP_CLOSE_BRACE
889 || token->type == CPP_CLOSE_PAREN
890 || token->type == CPP_CLOSE_SQUARE)
892 if (nesting_depth-- == 0)
893 break;
895 /* Consume this token. */
896 c_parser_consume_token (parser);
898 parser->error = false;
901 /* Skip tokens until the end of a parameter is found, but do not
902 consume the comma, semicolon or closing delimiter. */
904 static void
905 c_parser_skip_to_end_of_parameter (c_parser *parser)
907 unsigned nesting_depth = 0;
909 while (true)
911 c_token *token = c_parser_peek_token (parser);
912 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
913 && !nesting_depth)
914 break;
915 /* If we've run out of tokens, stop. */
916 if (token->type == CPP_EOF)
917 return;
918 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
919 return;
920 if (token->type == CPP_OPEN_BRACE
921 || token->type == CPP_OPEN_PAREN
922 || token->type == CPP_OPEN_SQUARE)
923 ++nesting_depth;
924 else if (token->type == CPP_CLOSE_BRACE
925 || token->type == CPP_CLOSE_PAREN
926 || token->type == CPP_CLOSE_SQUARE)
928 if (nesting_depth-- == 0)
929 break;
931 /* Consume this token. */
932 c_parser_consume_token (parser);
934 parser->error = false;
937 /* Expect to be at the end of the pragma directive and consume an
938 end of line marker. */
940 static void
941 c_parser_skip_to_pragma_eol (c_parser *parser)
943 gcc_assert (parser->in_pragma);
944 parser->in_pragma = false;
946 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
947 while (true)
949 c_token *token = c_parser_peek_token (parser);
950 if (token->type == CPP_EOF)
951 break;
952 if (token->type == CPP_PRAGMA_EOL)
954 c_parser_consume_token (parser);
955 break;
957 c_parser_consume_token (parser);
960 parser->error = false;
963 /* Skip tokens until we have consumed an entire block, or until we
964 have consumed a non-nested ';'. */
966 static void
967 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
969 unsigned nesting_depth = 0;
970 bool save_error = parser->error;
972 while (true)
974 c_token *token;
976 /* Peek at the next token. */
977 token = c_parser_peek_token (parser);
979 switch (token->type)
981 case CPP_EOF:
982 return;
984 case CPP_PRAGMA_EOL:
985 if (parser->in_pragma)
986 return;
987 break;
989 case CPP_SEMICOLON:
990 /* If the next token is a ';', we have reached the
991 end of the statement. */
992 if (!nesting_depth)
994 /* Consume the ';'. */
995 c_parser_consume_token (parser);
996 goto finished;
998 break;
1000 case CPP_CLOSE_BRACE:
1001 /* If the next token is a non-nested '}', then we have
1002 reached the end of the current block. */
1003 if (nesting_depth == 0 || --nesting_depth == 0)
1005 c_parser_consume_token (parser);
1006 goto finished;
1008 break;
1010 case CPP_OPEN_BRACE:
1011 /* If it the next token is a '{', then we are entering a new
1012 block. Consume the entire block. */
1013 ++nesting_depth;
1014 break;
1016 case CPP_PRAGMA:
1017 /* If we see a pragma, consume the whole thing at once. We
1018 have some safeguards against consuming pragmas willy-nilly.
1019 Normally, we'd expect to be here with parser->error set,
1020 which disables these safeguards. But it's possible to get
1021 here for secondary error recovery, after parser->error has
1022 been cleared. */
1023 c_parser_consume_pragma (parser);
1024 c_parser_skip_to_pragma_eol (parser);
1025 parser->error = save_error;
1026 continue;
1028 default:
1029 break;
1032 c_parser_consume_token (parser);
1035 finished:
1036 parser->error = false;
1039 /* CPP's options (initialized by c-opts.c). */
1040 extern cpp_options *cpp_opts;
1042 /* Save the warning flags which are controlled by __extension__. */
1044 static inline int
1045 disable_extension_diagnostics (void)
1047 int ret = (pedantic
1048 | (warn_pointer_arith << 1)
1049 | (warn_traditional << 2)
1050 | (flag_iso << 3)
1051 | (warn_long_long << 4)
1052 | (warn_cxx_compat << 5)
1053 | (warn_overlength_strings << 6));
1054 cpp_opts->cpp_pedantic = pedantic = 0;
1055 warn_pointer_arith = 0;
1056 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1057 flag_iso = 0;
1058 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1059 warn_cxx_compat = 0;
1060 warn_overlength_strings = 0;
1061 return ret;
1064 /* Restore the warning flags which are controlled by __extension__.
1065 FLAGS is the return value from disable_extension_diagnostics. */
1067 static inline void
1068 restore_extension_diagnostics (int flags)
1070 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1071 warn_pointer_arith = (flags >> 1) & 1;
1072 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1073 flag_iso = (flags >> 3) & 1;
1074 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1075 warn_cxx_compat = (flags >> 5) & 1;
1076 warn_overlength_strings = (flags >> 6) & 1;
1079 /* Possibly kinds of declarator to parse. */
1080 typedef enum c_dtr_syn {
1081 /* A normal declarator with an identifier. */
1082 C_DTR_NORMAL,
1083 /* An abstract declarator (maybe empty). */
1084 C_DTR_ABSTRACT,
1085 /* A parameter declarator: may be either, but after a type name does
1086 not redeclare a typedef name as an identifier if it can
1087 alternatively be interpreted as a typedef name; see DR#009,
1088 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1089 following DR#249. For example, given a typedef T, "int T" and
1090 "int *T" are valid parameter declarations redeclaring T, while
1091 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1092 abstract declarators rather than involving redundant parentheses;
1093 the same applies with attributes inside the parentheses before
1094 "T". */
1095 C_DTR_PARM
1096 } c_dtr_syn;
1098 /* The binary operation precedence levels, where 0 is a dummy lowest level
1099 used for the bottom of the stack. */
1100 enum c_parser_prec {
1101 PREC_NONE,
1102 PREC_LOGOR,
1103 PREC_LOGAND,
1104 PREC_BITOR,
1105 PREC_BITXOR,
1106 PREC_BITAND,
1107 PREC_EQ,
1108 PREC_REL,
1109 PREC_SHIFT,
1110 PREC_ADD,
1111 PREC_MULT,
1112 NUM_PRECS
1115 static void c_parser_external_declaration (c_parser *);
1116 static void c_parser_asm_definition (c_parser *);
1117 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1118 bool, bool, tree *);
1119 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1120 static void c_parser_static_assert_declaration (c_parser *);
1121 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1122 bool, enum c_lookahead_kind);
1123 static struct c_typespec c_parser_enum_specifier (c_parser *);
1124 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1125 static tree c_parser_struct_declaration (c_parser *);
1126 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1127 static tree c_parser_alignas_specifier (c_parser *);
1128 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1129 bool *);
1130 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1131 c_dtr_syn, bool *);
1132 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1133 bool,
1134 struct c_declarator *);
1135 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1136 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1137 tree);
1138 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1139 static tree c_parser_simple_asm_expr (c_parser *);
1140 static tree c_parser_attributes (c_parser *);
1141 static struct c_type_name *c_parser_type_name (c_parser *);
1142 static struct c_expr c_parser_initializer (c_parser *);
1143 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1144 static void c_parser_initelt (c_parser *, struct obstack *);
1145 static void c_parser_initval (c_parser *, struct c_expr *,
1146 struct obstack *);
1147 static tree c_parser_compound_statement (c_parser *);
1148 static void c_parser_compound_statement_nostart (c_parser *);
1149 static void c_parser_label (c_parser *);
1150 static void c_parser_statement (c_parser *);
1151 static void c_parser_statement_after_labels (c_parser *);
1152 static void c_parser_if_statement (c_parser *);
1153 static void c_parser_switch_statement (c_parser *);
1154 static void c_parser_while_statement (c_parser *);
1155 static void c_parser_do_statement (c_parser *);
1156 static void c_parser_for_statement (c_parser *);
1157 static tree c_parser_asm_statement (c_parser *);
1158 static tree c_parser_asm_operands (c_parser *, bool);
1159 static tree c_parser_asm_goto_operands (c_parser *);
1160 static tree c_parser_asm_clobbers (c_parser *);
1161 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1162 static struct c_expr c_parser_conditional_expression (c_parser *,
1163 struct c_expr *);
1164 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1165 enum c_parser_prec);
1166 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1167 static struct c_expr c_parser_unary_expression (c_parser *);
1168 static struct c_expr c_parser_sizeof_expression (c_parser *);
1169 static struct c_expr c_parser_alignof_expression (c_parser *);
1170 static struct c_expr c_parser_postfix_expression (c_parser *);
1171 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1172 struct c_type_name *,
1173 location_t);
1174 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1175 location_t loc,
1176 struct c_expr);
1177 static tree c_parser_transaction (c_parser *, enum rid);
1178 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1179 static tree c_parser_transaction_cancel (c_parser *);
1180 static struct c_expr c_parser_expression (c_parser *);
1181 static struct c_expr c_parser_expression_conv (c_parser *);
1182 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1183 VEC(tree,gc) **);
1184 static void c_parser_omp_construct (c_parser *);
1185 static void c_parser_omp_threadprivate (c_parser *);
1186 static void c_parser_omp_barrier (c_parser *);
1187 static void c_parser_omp_flush (c_parser *);
1188 static void c_parser_omp_taskwait (c_parser *);
1189 static void c_parser_omp_taskyield (c_parser *);
1191 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1192 static bool c_parser_pragma (c_parser *, enum pragma_context);
1194 /* These Objective-C parser functions are only ever called when
1195 compiling Objective-C. */
1196 static void c_parser_objc_class_definition (c_parser *, tree);
1197 static void c_parser_objc_class_instance_variables (c_parser *);
1198 static void c_parser_objc_class_declaration (c_parser *);
1199 static void c_parser_objc_alias_declaration (c_parser *);
1200 static void c_parser_objc_protocol_definition (c_parser *, tree);
1201 static bool c_parser_objc_method_type (c_parser *);
1202 static void c_parser_objc_method_definition (c_parser *);
1203 static void c_parser_objc_methodprotolist (c_parser *);
1204 static void c_parser_objc_methodproto (c_parser *);
1205 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1206 static tree c_parser_objc_type_name (c_parser *);
1207 static tree c_parser_objc_protocol_refs (c_parser *);
1208 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1209 static void c_parser_objc_synchronized_statement (c_parser *);
1210 static tree c_parser_objc_selector (c_parser *);
1211 static tree c_parser_objc_selector_arg (c_parser *);
1212 static tree c_parser_objc_receiver (c_parser *);
1213 static tree c_parser_objc_message_args (c_parser *);
1214 static tree c_parser_objc_keywordexpr (c_parser *);
1215 static void c_parser_objc_at_property_declaration (c_parser *);
1216 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1217 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1218 static bool c_parser_objc_diagnose_bad_element_prefix
1219 (c_parser *, struct c_declspecs *);
1221 /* Parse a translation unit (C90 6.7, C99 6.9).
1223 translation-unit:
1224 external-declarations
1226 external-declarations:
1227 external-declaration
1228 external-declarations external-declaration
1230 GNU extensions:
1232 translation-unit:
1233 empty
1236 static void
1237 c_parser_translation_unit (c_parser *parser)
1239 if (c_parser_next_token_is (parser, CPP_EOF))
1241 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1242 "ISO C forbids an empty translation unit");
1244 else
1246 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1247 mark_valid_location_for_stdc_pragma (false);
1250 ggc_collect ();
1251 c_parser_external_declaration (parser);
1252 obstack_free (&parser_obstack, obstack_position);
1254 while (c_parser_next_token_is_not (parser, CPP_EOF));
1258 /* Parse an external declaration (C90 6.7, C99 6.9).
1260 external-declaration:
1261 function-definition
1262 declaration
1264 GNU extensions:
1266 external-declaration:
1267 asm-definition
1269 __extension__ external-declaration
1271 Objective-C:
1273 external-declaration:
1274 objc-class-definition
1275 objc-class-declaration
1276 objc-alias-declaration
1277 objc-protocol-definition
1278 objc-method-definition
1279 @end
1282 static void
1283 c_parser_external_declaration (c_parser *parser)
1285 int ext;
1286 switch (c_parser_peek_token (parser)->type)
1288 case CPP_KEYWORD:
1289 switch (c_parser_peek_token (parser)->keyword)
1291 case RID_EXTENSION:
1292 ext = disable_extension_diagnostics ();
1293 c_parser_consume_token (parser);
1294 c_parser_external_declaration (parser);
1295 restore_extension_diagnostics (ext);
1296 break;
1297 case RID_ASM:
1298 c_parser_asm_definition (parser);
1299 break;
1300 case RID_AT_INTERFACE:
1301 case RID_AT_IMPLEMENTATION:
1302 gcc_assert (c_dialect_objc ());
1303 c_parser_objc_class_definition (parser, NULL_TREE);
1304 break;
1305 case RID_AT_CLASS:
1306 gcc_assert (c_dialect_objc ());
1307 c_parser_objc_class_declaration (parser);
1308 break;
1309 case RID_AT_ALIAS:
1310 gcc_assert (c_dialect_objc ());
1311 c_parser_objc_alias_declaration (parser);
1312 break;
1313 case RID_AT_PROTOCOL:
1314 gcc_assert (c_dialect_objc ());
1315 c_parser_objc_protocol_definition (parser, NULL_TREE);
1316 break;
1317 case RID_AT_PROPERTY:
1318 gcc_assert (c_dialect_objc ());
1319 c_parser_objc_at_property_declaration (parser);
1320 break;
1321 case RID_AT_SYNTHESIZE:
1322 gcc_assert (c_dialect_objc ());
1323 c_parser_objc_at_synthesize_declaration (parser);
1324 break;
1325 case RID_AT_DYNAMIC:
1326 gcc_assert (c_dialect_objc ());
1327 c_parser_objc_at_dynamic_declaration (parser);
1328 break;
1329 case RID_AT_END:
1330 gcc_assert (c_dialect_objc ());
1331 c_parser_consume_token (parser);
1332 objc_finish_implementation ();
1333 break;
1334 default:
1335 goto decl_or_fndef;
1337 break;
1338 case CPP_SEMICOLON:
1339 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1340 "ISO C does not allow extra %<;%> outside of a function");
1341 c_parser_consume_token (parser);
1342 break;
1343 case CPP_PRAGMA:
1344 mark_valid_location_for_stdc_pragma (true);
1345 c_parser_pragma (parser, pragma_external);
1346 mark_valid_location_for_stdc_pragma (false);
1347 break;
1348 case CPP_PLUS:
1349 case CPP_MINUS:
1350 if (c_dialect_objc ())
1352 c_parser_objc_method_definition (parser);
1353 break;
1355 /* Else fall through, and yield a syntax error trying to parse
1356 as a declaration or function definition. */
1357 default:
1358 decl_or_fndef:
1359 /* A declaration or a function definition (or, in Objective-C,
1360 an @interface or @protocol with prefix attributes). We can
1361 only tell which after parsing the declaration specifiers, if
1362 any, and the first declarator. */
1363 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1364 break;
1368 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1369 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1370 accepted; otherwise (old-style parameter declarations) only other
1371 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1372 assertion is accepted; otherwise (old-style parameter declarations)
1373 it is not. If NESTED is true, we are inside a function or parsing
1374 old-style parameter declarations; any functions encountered are
1375 nested functions and declaration specifiers are required; otherwise
1376 we are at top level and functions are normal functions and
1377 declaration specifiers may be optional. If EMPTY_OK is true, empty
1378 declarations are OK (subject to all other constraints); otherwise
1379 (old-style parameter declarations) they are diagnosed. If
1380 START_ATTR_OK is true, the declaration specifiers may start with
1381 attributes; otherwise they may not.
1382 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1383 declaration when parsing an Objective-C foreach statement.
1385 declaration:
1386 declaration-specifiers init-declarator-list[opt] ;
1387 static_assert-declaration
1389 function-definition:
1390 declaration-specifiers[opt] declarator declaration-list[opt]
1391 compound-statement
1393 declaration-list:
1394 declaration
1395 declaration-list declaration
1397 init-declarator-list:
1398 init-declarator
1399 init-declarator-list , init-declarator
1401 init-declarator:
1402 declarator simple-asm-expr[opt] attributes[opt]
1403 declarator simple-asm-expr[opt] attributes[opt] = initializer
1405 GNU extensions:
1407 nested-function-definition:
1408 declaration-specifiers declarator declaration-list[opt]
1409 compound-statement
1411 Objective-C:
1412 attributes objc-class-definition
1413 attributes objc-category-definition
1414 attributes objc-protocol-definition
1416 The simple-asm-expr and attributes are GNU extensions.
1418 This function does not handle __extension__; that is handled in its
1419 callers. ??? Following the old parser, __extension__ may start
1420 external declarations, declarations in functions and declarations
1421 at the start of "for" loops, but not old-style parameter
1422 declarations.
1424 C99 requires declaration specifiers in a function definition; the
1425 absence is diagnosed through the diagnosis of implicit int. In GNU
1426 C we also allow but diagnose declarations without declaration
1427 specifiers, but only at top level (elsewhere they conflict with
1428 other syntax).
1430 In Objective-C, declarations of the looping variable in a foreach
1431 statement are exceptionally terminated by 'in' (for example, 'for
1432 (NSObject *object in array) { ... }').
1434 OpenMP:
1436 declaration:
1437 threadprivate-directive */
1439 static void
1440 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1441 bool static_assert_ok, bool empty_ok,
1442 bool nested, bool start_attr_ok,
1443 tree *objc_foreach_object_declaration)
1445 struct c_declspecs *specs;
1446 tree prefix_attrs;
1447 tree all_prefix_attrs;
1448 bool diagnosed_no_specs = false;
1449 location_t here = c_parser_peek_token (parser)->location;
1451 if (static_assert_ok
1452 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1454 c_parser_static_assert_declaration (parser);
1455 return;
1457 specs = build_null_declspecs ();
1459 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1460 if (c_parser_peek_token (parser)->type == CPP_NAME
1461 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1462 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1463 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1464 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1466 error_at (here, "unknown type name %qE",
1467 c_parser_peek_token (parser)->value);
1469 /* Parse declspecs normally to get a correct pointer type, but avoid
1470 a further "fails to be a type name" error. Refuse nested functions
1471 since it is not how the user likely wants us to recover. */
1472 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1473 c_parser_peek_token (parser)->keyword = RID_VOID;
1474 c_parser_peek_token (parser)->value = error_mark_node;
1475 fndef_ok = !nested;
1478 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1479 if (parser->error)
1481 c_parser_skip_to_end_of_block_or_statement (parser);
1482 return;
1484 if (nested && !specs->declspecs_seen_p)
1486 c_parser_error (parser, "expected declaration specifiers");
1487 c_parser_skip_to_end_of_block_or_statement (parser);
1488 return;
1490 finish_declspecs (specs);
1491 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1493 if (empty_ok)
1494 shadow_tag (specs);
1495 else
1497 shadow_tag_warned (specs, 1);
1498 pedwarn (here, 0, "empty declaration");
1500 c_parser_consume_token (parser);
1501 return;
1504 /* Provide better error recovery. Note that a type name here is usually
1505 better diagnosed as a redeclaration. */
1506 if (empty_ok
1507 && specs->typespec_kind == ctsk_tagdef
1508 && c_parser_next_token_starts_declspecs (parser)
1509 && !c_parser_next_token_is (parser, CPP_NAME))
1511 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1512 parser->error = false;
1513 shadow_tag_warned (specs, 1);
1514 return;
1516 else if (c_dialect_objc ())
1518 /* Prefix attributes are an error on method decls. */
1519 switch (c_parser_peek_token (parser)->type)
1521 case CPP_PLUS:
1522 case CPP_MINUS:
1523 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1524 return;
1525 if (specs->attrs)
1527 warning_at (c_parser_peek_token (parser)->location,
1528 OPT_Wattributes,
1529 "prefix attributes are ignored for methods");
1530 specs->attrs = NULL_TREE;
1532 if (fndef_ok)
1533 c_parser_objc_method_definition (parser);
1534 else
1535 c_parser_objc_methodproto (parser);
1536 return;
1537 break;
1538 default:
1539 break;
1541 /* This is where we parse 'attributes @interface ...',
1542 'attributes @implementation ...', 'attributes @protocol ...'
1543 (where attributes could be, for example, __attribute__
1544 ((deprecated)).
1546 switch (c_parser_peek_token (parser)->keyword)
1548 case RID_AT_INTERFACE:
1550 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1551 return;
1552 c_parser_objc_class_definition (parser, specs->attrs);
1553 return;
1555 break;
1556 case RID_AT_IMPLEMENTATION:
1558 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1559 return;
1560 if (specs->attrs)
1562 warning_at (c_parser_peek_token (parser)->location,
1563 OPT_Wattributes,
1564 "prefix attributes are ignored for implementations");
1565 specs->attrs = NULL_TREE;
1567 c_parser_objc_class_definition (parser, NULL_TREE);
1568 return;
1570 break;
1571 case RID_AT_PROTOCOL:
1573 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1574 return;
1575 c_parser_objc_protocol_definition (parser, specs->attrs);
1576 return;
1578 break;
1579 case RID_AT_ALIAS:
1580 case RID_AT_CLASS:
1581 case RID_AT_END:
1582 case RID_AT_PROPERTY:
1583 if (specs->attrs)
1585 c_parser_error (parser, "unexpected attribute");
1586 specs->attrs = NULL;
1588 break;
1589 default:
1590 break;
1594 pending_xref_error ();
1595 prefix_attrs = specs->attrs;
1596 all_prefix_attrs = prefix_attrs;
1597 specs->attrs = NULL_TREE;
1598 while (true)
1600 struct c_declarator *declarator;
1601 bool dummy = false;
1602 timevar_id_t tv;
1603 tree fnbody;
1604 /* Declaring either one or more declarators (in which case we
1605 should diagnose if there were no declaration specifiers) or a
1606 function definition (in which case the diagnostic for
1607 implicit int suffices). */
1608 declarator = c_parser_declarator (parser,
1609 specs->typespec_kind != ctsk_none,
1610 C_DTR_NORMAL, &dummy);
1611 if (declarator == NULL)
1613 c_parser_skip_to_end_of_block_or_statement (parser);
1614 return;
1616 if (c_parser_next_token_is (parser, CPP_EQ)
1617 || c_parser_next_token_is (parser, CPP_COMMA)
1618 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1619 || c_parser_next_token_is_keyword (parser, RID_ASM)
1620 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1621 || c_parser_next_token_is_keyword (parser, RID_IN))
1623 tree asm_name = NULL_TREE;
1624 tree postfix_attrs = NULL_TREE;
1625 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1627 diagnosed_no_specs = true;
1628 pedwarn (here, 0, "data definition has no type or storage class");
1630 /* Having seen a data definition, there cannot now be a
1631 function definition. */
1632 fndef_ok = false;
1633 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1634 asm_name = c_parser_simple_asm_expr (parser);
1635 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1636 postfix_attrs = c_parser_attributes (parser);
1637 if (c_parser_next_token_is (parser, CPP_EQ))
1639 tree d;
1640 struct c_expr init;
1641 location_t init_loc;
1642 c_parser_consume_token (parser);
1643 /* The declaration of the variable is in effect while
1644 its initializer is parsed. */
1645 d = start_decl (declarator, specs, true,
1646 chainon (postfix_attrs, all_prefix_attrs));
1647 if (!d)
1648 d = error_mark_node;
1649 start_init (d, asm_name, global_bindings_p ());
1650 init_loc = c_parser_peek_token (parser)->location;
1651 init = c_parser_initializer (parser);
1652 finish_init ();
1653 if (d != error_mark_node)
1655 maybe_warn_string_init (TREE_TYPE (d), init);
1656 finish_decl (d, init_loc, init.value,
1657 init.original_type, asm_name);
1660 else
1662 tree d = start_decl (declarator, specs, false,
1663 chainon (postfix_attrs,
1664 all_prefix_attrs));
1665 if (d)
1666 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1667 NULL_TREE, asm_name);
1669 if (c_parser_next_token_is_keyword (parser, RID_IN))
1671 if (d)
1672 *objc_foreach_object_declaration = d;
1673 else
1674 *objc_foreach_object_declaration = error_mark_node;
1677 if (c_parser_next_token_is (parser, CPP_COMMA))
1679 c_parser_consume_token (parser);
1680 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1681 all_prefix_attrs = chainon (c_parser_attributes (parser),
1682 prefix_attrs);
1683 else
1684 all_prefix_attrs = prefix_attrs;
1685 continue;
1687 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1689 c_parser_consume_token (parser);
1690 return;
1692 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1694 /* This can only happen in Objective-C: we found the
1695 'in' that terminates the declaration inside an
1696 Objective-C foreach statement. Do not consume the
1697 token, so that the caller can use it to determine
1698 that this indeed is a foreach context. */
1699 return;
1701 else
1703 c_parser_error (parser, "expected %<,%> or %<;%>");
1704 c_parser_skip_to_end_of_block_or_statement (parser);
1705 return;
1708 else if (!fndef_ok)
1710 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1711 "%<asm%> or %<__attribute__%>");
1712 c_parser_skip_to_end_of_block_or_statement (parser);
1713 return;
1715 /* Function definition (nested or otherwise). */
1716 if (nested)
1718 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1719 c_push_function_context ();
1721 if (!start_function (specs, declarator, all_prefix_attrs))
1723 /* This can appear in many cases looking nothing like a
1724 function definition, so we don't give a more specific
1725 error suggesting there was one. */
1726 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1727 "or %<__attribute__%>");
1728 if (nested)
1729 c_pop_function_context ();
1730 break;
1733 if (DECL_DECLARED_INLINE_P (current_function_decl))
1734 tv = TV_PARSE_INLINE;
1735 else
1736 tv = TV_PARSE_FUNC;
1737 timevar_push (tv);
1739 /* Parse old-style parameter declarations. ??? Attributes are
1740 not allowed to start declaration specifiers here because of a
1741 syntax conflict between a function declaration with attribute
1742 suffix and a function definition with an attribute prefix on
1743 first old-style parameter declaration. Following the old
1744 parser, they are not accepted on subsequent old-style
1745 parameter declarations either. However, there is no
1746 ambiguity after the first declaration, nor indeed on the
1747 first as long as we don't allow postfix attributes after a
1748 declarator with a nonempty identifier list in a definition;
1749 and postfix attributes have never been accepted here in
1750 function definitions either. */
1751 while (c_parser_next_token_is_not (parser, CPP_EOF)
1752 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1753 c_parser_declaration_or_fndef (parser, false, false, false,
1754 true, false, NULL);
1755 store_parm_decls ();
1756 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1757 = c_parser_peek_token (parser)->location;
1758 fnbody = c_parser_compound_statement (parser);
1759 if (nested)
1761 tree decl = current_function_decl;
1762 /* Mark nested functions as needing static-chain initially.
1763 lower_nested_functions will recompute it but the
1764 DECL_STATIC_CHAIN flag is also used before that happens,
1765 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1766 DECL_STATIC_CHAIN (decl) = 1;
1767 add_stmt (fnbody);
1768 finish_function ();
1769 c_pop_function_context ();
1770 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1772 else
1774 add_stmt (fnbody);
1775 finish_function ();
1778 timevar_pop (tv);
1779 break;
1783 /* Parse an asm-definition (asm() outside a function body). This is a
1784 GNU extension.
1786 asm-definition:
1787 simple-asm-expr ;
1790 static void
1791 c_parser_asm_definition (c_parser *parser)
1793 tree asm_str = c_parser_simple_asm_expr (parser);
1794 if (asm_str)
1795 cgraph_add_asm_node (asm_str);
1796 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1799 /* Parse a static assertion (C11 6.7.10).
1801 static_assert-declaration:
1802 static_assert-declaration-no-semi ;
1805 static void
1806 c_parser_static_assert_declaration (c_parser *parser)
1808 c_parser_static_assert_declaration_no_semi (parser);
1809 if (parser->error
1810 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1811 c_parser_skip_to_end_of_block_or_statement (parser);
1814 /* Parse a static assertion (C11 6.7.10), without the trailing
1815 semicolon.
1817 static_assert-declaration-no-semi:
1818 _Static_assert ( constant-expression , string-literal )
1821 static void
1822 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1824 location_t assert_loc, value_loc;
1825 tree value;
1826 tree string;
1828 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1829 assert_loc = c_parser_peek_token (parser)->location;
1830 if (!flag_isoc11)
1832 if (flag_isoc99)
1833 pedwarn (assert_loc, OPT_pedantic,
1834 "ISO C99 does not support %<_Static_assert%>");
1835 else
1836 pedwarn (assert_loc, OPT_pedantic,
1837 "ISO C90 does not support %<_Static_assert%>");
1839 c_parser_consume_token (parser);
1840 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1841 return;
1842 value_loc = c_parser_peek_token (parser)->location;
1843 value = c_parser_expr_no_commas (parser, NULL).value;
1844 parser->lex_untranslated_string = true;
1845 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1847 parser->lex_untranslated_string = false;
1848 return;
1850 switch (c_parser_peek_token (parser)->type)
1852 case CPP_STRING:
1853 case CPP_STRING16:
1854 case CPP_STRING32:
1855 case CPP_WSTRING:
1856 case CPP_UTF8STRING:
1857 string = c_parser_peek_token (parser)->value;
1858 c_parser_consume_token (parser);
1859 parser->lex_untranslated_string = false;
1860 break;
1861 default:
1862 c_parser_error (parser, "expected string literal");
1863 parser->lex_untranslated_string = false;
1864 return;
1866 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1868 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1870 error_at (value_loc, "expression in static assertion is not an integer");
1871 return;
1873 if (TREE_CODE (value) != INTEGER_CST)
1875 value = c_fully_fold (value, false, NULL);
1876 if (TREE_CODE (value) == INTEGER_CST)
1877 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1878 "is not an integer constant expression");
1880 if (TREE_CODE (value) != INTEGER_CST)
1882 error_at (value_loc, "expression in static assertion is not constant");
1883 return;
1885 constant_expression_warning (value);
1886 if (integer_zerop (value))
1887 error_at (assert_loc, "static assertion failed: %E", string);
1890 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1891 6.7), adding them to SPECS (which may already include some).
1892 Storage class specifiers are accepted iff SCSPEC_OK; type
1893 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1894 the start iff START_ATTR_OK.
1896 declaration-specifiers:
1897 storage-class-specifier declaration-specifiers[opt]
1898 type-specifier declaration-specifiers[opt]
1899 type-qualifier declaration-specifiers[opt]
1900 function-specifier declaration-specifiers[opt]
1901 alignment-specifier declaration-specifiers[opt]
1903 Function specifiers (inline) are from C99, and are currently
1904 handled as storage class specifiers, as is __thread. Alignment
1905 specifiers are from C11.
1907 C90 6.5.1, C99 6.7.1:
1908 storage-class-specifier:
1909 typedef
1910 extern
1911 static
1912 auto
1913 register
1915 C99 6.7.4:
1916 function-specifier:
1917 inline
1918 _Noreturn
1920 (_Noreturn is new in C11.)
1922 C90 6.5.2, C99 6.7.2:
1923 type-specifier:
1924 void
1925 char
1926 short
1928 long
1929 float
1930 double
1931 signed
1932 unsigned
1933 _Bool
1934 _Complex
1935 [_Imaginary removed in C99 TC2]
1936 struct-or-union-specifier
1937 enum-specifier
1938 typedef-name
1940 (_Bool and _Complex are new in C99.)
1942 C90 6.5.3, C99 6.7.3:
1944 type-qualifier:
1945 const
1946 restrict
1947 volatile
1948 address-space-qualifier
1950 (restrict is new in C99.)
1952 GNU extensions:
1954 declaration-specifiers:
1955 attributes declaration-specifiers[opt]
1957 type-qualifier:
1958 address-space
1960 address-space:
1961 identifier recognized by the target
1963 storage-class-specifier:
1964 __thread
1966 type-specifier:
1967 typeof-specifier
1968 __int128
1969 _Decimal32
1970 _Decimal64
1971 _Decimal128
1972 _Fract
1973 _Accum
1974 _Sat
1976 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1977 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1979 Objective-C:
1981 type-specifier:
1982 class-name objc-protocol-refs[opt]
1983 typedef-name objc-protocol-refs
1984 objc-protocol-refs
1987 static void
1988 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1989 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1990 enum c_lookahead_kind la)
1992 bool attrs_ok = start_attr_ok;
1993 bool seen_type = specs->typespec_kind != ctsk_none;
1995 if (!typespec_ok)
1996 gcc_assert (la == cla_prefer_id);
1998 while (c_parser_next_token_is (parser, CPP_NAME)
1999 || c_parser_next_token_is (parser, CPP_KEYWORD)
2000 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2002 struct c_typespec t;
2003 tree attrs;
2004 tree align;
2005 location_t loc = c_parser_peek_token (parser)->location;
2007 /* If we cannot accept a type, exit if the next token must start
2008 one. Also, if we already have seen a tagged definition,
2009 a typename would be an error anyway and likely the user
2010 has simply forgotten a semicolon, so we exit. */
2011 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2012 && c_parser_next_tokens_start_typename (parser, la)
2013 && !c_parser_next_token_is_qualifier (parser))
2014 break;
2016 if (c_parser_next_token_is (parser, CPP_NAME))
2018 tree value = c_parser_peek_token (parser)->value;
2019 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
2021 if (kind == C_ID_ADDRSPACE)
2023 addr_space_t as
2024 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
2025 declspecs_add_addrspace (specs, as);
2026 c_parser_consume_token (parser);
2027 attrs_ok = true;
2028 continue;
2031 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2033 /* If we cannot accept a type, and the next token must start one,
2034 exit. Do the same if we already have seen a tagged definition,
2035 since it would be an error anyway and likely the user has simply
2036 forgotten a semicolon. */
2037 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2038 break;
2040 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2041 a C_ID_CLASSNAME. */
2042 c_parser_consume_token (parser);
2043 seen_type = true;
2044 attrs_ok = true;
2045 if (kind == C_ID_ID)
2047 error ("unknown type name %qE", value);
2048 t.kind = ctsk_typedef;
2049 t.spec = error_mark_node;
2051 else if (kind == C_ID_TYPENAME
2052 && (!c_dialect_objc ()
2053 || c_parser_next_token_is_not (parser, CPP_LESS)))
2055 t.kind = ctsk_typedef;
2056 /* For a typedef name, record the meaning, not the name.
2057 In case of 'foo foo, bar;'. */
2058 t.spec = lookup_name (value);
2060 else
2062 tree proto = NULL_TREE;
2063 gcc_assert (c_dialect_objc ());
2064 t.kind = ctsk_objc;
2065 if (c_parser_next_token_is (parser, CPP_LESS))
2066 proto = c_parser_objc_protocol_refs (parser);
2067 t.spec = objc_get_protocol_qualified_type (value, proto);
2069 t.expr = NULL_TREE;
2070 t.expr_const_operands = true;
2071 declspecs_add_type (loc, specs, t);
2072 continue;
2074 if (c_parser_next_token_is (parser, CPP_LESS))
2076 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2077 nisse@lysator.liu.se. */
2078 tree proto;
2079 gcc_assert (c_dialect_objc ());
2080 if (!typespec_ok || seen_type)
2081 break;
2082 proto = c_parser_objc_protocol_refs (parser);
2083 t.kind = ctsk_objc;
2084 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2085 t.expr = NULL_TREE;
2086 t.expr_const_operands = true;
2087 declspecs_add_type (loc, specs, t);
2088 continue;
2090 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2091 switch (c_parser_peek_token (parser)->keyword)
2093 case RID_STATIC:
2094 case RID_EXTERN:
2095 case RID_REGISTER:
2096 case RID_TYPEDEF:
2097 case RID_INLINE:
2098 case RID_NORETURN:
2099 case RID_AUTO:
2100 case RID_THREAD:
2101 if (!scspec_ok)
2102 goto out;
2103 attrs_ok = true;
2104 /* TODO: Distinguish between function specifiers (inline, noreturn)
2105 and storage class specifiers, either here or in
2106 declspecs_add_scspec. */
2107 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2108 c_parser_consume_token (parser);
2109 break;
2110 case RID_UNSIGNED:
2111 case RID_LONG:
2112 case RID_INT128:
2113 case RID_SHORT:
2114 case RID_SIGNED:
2115 case RID_COMPLEX:
2116 case RID_INT:
2117 case RID_CHAR:
2118 case RID_FLOAT:
2119 case RID_DOUBLE:
2120 case RID_VOID:
2121 case RID_DFLOAT32:
2122 case RID_DFLOAT64:
2123 case RID_DFLOAT128:
2124 case RID_BOOL:
2125 case RID_FRACT:
2126 case RID_ACCUM:
2127 case RID_SAT:
2128 if (!typespec_ok)
2129 goto out;
2130 attrs_ok = true;
2131 seen_type = true;
2132 if (c_dialect_objc ())
2133 parser->objc_need_raw_identifier = true;
2134 t.kind = ctsk_resword;
2135 t.spec = c_parser_peek_token (parser)->value;
2136 t.expr = NULL_TREE;
2137 t.expr_const_operands = true;
2138 declspecs_add_type (loc, specs, t);
2139 c_parser_consume_token (parser);
2140 break;
2141 case RID_ENUM:
2142 if (!typespec_ok)
2143 goto out;
2144 attrs_ok = true;
2145 seen_type = true;
2146 t = c_parser_enum_specifier (parser);
2147 declspecs_add_type (loc, specs, t);
2148 break;
2149 case RID_STRUCT:
2150 case RID_UNION:
2151 if (!typespec_ok)
2152 goto out;
2153 attrs_ok = true;
2154 seen_type = true;
2155 t = c_parser_struct_or_union_specifier (parser);
2156 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2157 declspecs_add_type (loc, specs, t);
2158 break;
2159 case RID_TYPEOF:
2160 /* ??? The old parser rejected typeof after other type
2161 specifiers, but is a syntax error the best way of
2162 handling this? */
2163 if (!typespec_ok || seen_type)
2164 goto out;
2165 attrs_ok = true;
2166 seen_type = true;
2167 t = c_parser_typeof_specifier (parser);
2168 declspecs_add_type (loc, specs, t);
2169 break;
2170 case RID_CONST:
2171 case RID_VOLATILE:
2172 case RID_RESTRICT:
2173 attrs_ok = true;
2174 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2175 c_parser_consume_token (parser);
2176 break;
2177 case RID_ATTRIBUTE:
2178 if (!attrs_ok)
2179 goto out;
2180 attrs = c_parser_attributes (parser);
2181 declspecs_add_attrs (specs, attrs);
2182 break;
2183 case RID_ALIGNAS:
2184 align = c_parser_alignas_specifier (parser);
2185 declspecs_add_alignas (specs, align);
2186 break;
2187 default:
2188 goto out;
2191 out: ;
2194 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2196 enum-specifier:
2197 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2198 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2199 enum attributes[opt] identifier
2201 The form with trailing comma is new in C99. The forms with
2202 attributes are GNU extensions. In GNU C, we accept any expression
2203 without commas in the syntax (assignment expressions, not just
2204 conditional expressions); assignment expressions will be diagnosed
2205 as non-constant.
2207 enumerator-list:
2208 enumerator
2209 enumerator-list , enumerator
2211 enumerator:
2212 enumeration-constant
2213 enumeration-constant = constant-expression
2216 static struct c_typespec
2217 c_parser_enum_specifier (c_parser *parser)
2219 struct c_typespec ret;
2220 tree attrs;
2221 tree ident = NULL_TREE;
2222 location_t enum_loc;
2223 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2224 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2225 enum_loc = c_parser_peek_token (parser)->location;
2226 c_parser_consume_token (parser);
2227 attrs = c_parser_attributes (parser);
2228 enum_loc = c_parser_peek_token (parser)->location;
2229 /* Set the location in case we create a decl now. */
2230 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2231 if (c_parser_next_token_is (parser, CPP_NAME))
2233 ident = c_parser_peek_token (parser)->value;
2234 ident_loc = c_parser_peek_token (parser)->location;
2235 enum_loc = ident_loc;
2236 c_parser_consume_token (parser);
2238 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2240 /* Parse an enum definition. */
2241 struct c_enum_contents the_enum;
2242 tree type;
2243 tree postfix_attrs;
2244 /* We chain the enumerators in reverse order, then put them in
2245 forward order at the end. */
2246 tree values;
2247 timevar_push (TV_PARSE_ENUM);
2248 type = start_enum (enum_loc, &the_enum, ident);
2249 values = NULL_TREE;
2250 c_parser_consume_token (parser);
2251 while (true)
2253 tree enum_id;
2254 tree enum_value;
2255 tree enum_decl;
2256 bool seen_comma;
2257 c_token *token;
2258 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2259 location_t decl_loc, value_loc;
2260 if (c_parser_next_token_is_not (parser, CPP_NAME))
2262 c_parser_error (parser, "expected identifier");
2263 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2264 values = error_mark_node;
2265 break;
2267 token = c_parser_peek_token (parser);
2268 enum_id = token->value;
2269 /* Set the location in case we create a decl now. */
2270 c_parser_set_source_position_from_token (token);
2271 decl_loc = value_loc = token->location;
2272 c_parser_consume_token (parser);
2273 if (c_parser_next_token_is (parser, CPP_EQ))
2275 c_parser_consume_token (parser);
2276 value_loc = c_parser_peek_token (parser)->location;
2277 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2279 else
2280 enum_value = NULL_TREE;
2281 enum_decl = build_enumerator (decl_loc, value_loc,
2282 &the_enum, enum_id, enum_value);
2283 TREE_CHAIN (enum_decl) = values;
2284 values = enum_decl;
2285 seen_comma = false;
2286 if (c_parser_next_token_is (parser, CPP_COMMA))
2288 comma_loc = c_parser_peek_token (parser)->location;
2289 seen_comma = true;
2290 c_parser_consume_token (parser);
2292 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2294 if (seen_comma && !flag_isoc99)
2295 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2296 c_parser_consume_token (parser);
2297 break;
2299 if (!seen_comma)
2301 c_parser_error (parser, "expected %<,%> or %<}%>");
2302 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2303 values = error_mark_node;
2304 break;
2307 postfix_attrs = c_parser_attributes (parser);
2308 ret.spec = finish_enum (type, nreverse (values),
2309 chainon (attrs, postfix_attrs));
2310 ret.kind = ctsk_tagdef;
2311 ret.expr = NULL_TREE;
2312 ret.expr_const_operands = true;
2313 timevar_pop (TV_PARSE_ENUM);
2314 return ret;
2316 else if (!ident)
2318 c_parser_error (parser, "expected %<{%>");
2319 ret.spec = error_mark_node;
2320 ret.kind = ctsk_tagref;
2321 ret.expr = NULL_TREE;
2322 ret.expr_const_operands = true;
2323 return ret;
2325 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2326 /* In ISO C, enumerated types can be referred to only if already
2327 defined. */
2328 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2330 gcc_assert (ident);
2331 pedwarn (enum_loc, OPT_pedantic,
2332 "ISO C forbids forward references to %<enum%> types");
2334 return ret;
2337 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2339 struct-or-union-specifier:
2340 struct-or-union attributes[opt] identifier[opt]
2341 { struct-contents } attributes[opt]
2342 struct-or-union attributes[opt] identifier
2344 struct-contents:
2345 struct-declaration-list
2347 struct-declaration-list:
2348 struct-declaration ;
2349 struct-declaration-list struct-declaration ;
2351 GNU extensions:
2353 struct-contents:
2354 empty
2355 struct-declaration
2356 struct-declaration-list struct-declaration
2358 struct-declaration-list:
2359 struct-declaration-list ;
2362 (Note that in the syntax here, unlike that in ISO C, the semicolons
2363 are included here rather than in struct-declaration, in order to
2364 describe the syntax with extra semicolons and missing semicolon at
2365 end.)
2367 Objective-C:
2369 struct-declaration-list:
2370 @defs ( class-name )
2372 (Note this does not include a trailing semicolon, but can be
2373 followed by further declarations, and gets a pedwarn-if-pedantic
2374 when followed by a semicolon.) */
2376 static struct c_typespec
2377 c_parser_struct_or_union_specifier (c_parser *parser)
2379 struct c_typespec ret;
2380 tree attrs;
2381 tree ident = NULL_TREE;
2382 location_t struct_loc;
2383 location_t ident_loc = UNKNOWN_LOCATION;
2384 enum tree_code code;
2385 switch (c_parser_peek_token (parser)->keyword)
2387 case RID_STRUCT:
2388 code = RECORD_TYPE;
2389 break;
2390 case RID_UNION:
2391 code = UNION_TYPE;
2392 break;
2393 default:
2394 gcc_unreachable ();
2396 struct_loc = c_parser_peek_token (parser)->location;
2397 c_parser_consume_token (parser);
2398 attrs = c_parser_attributes (parser);
2400 /* Set the location in case we create a decl now. */
2401 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2403 if (c_parser_next_token_is (parser, CPP_NAME))
2405 ident = c_parser_peek_token (parser)->value;
2406 ident_loc = c_parser_peek_token (parser)->location;
2407 struct_loc = ident_loc;
2408 c_parser_consume_token (parser);
2410 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2412 /* Parse a struct or union definition. Start the scope of the
2413 tag before parsing components. */
2414 struct c_struct_parse_info *struct_info;
2415 tree type = start_struct (struct_loc, code, ident, &struct_info);
2416 tree postfix_attrs;
2417 /* We chain the components in reverse order, then put them in
2418 forward order at the end. Each struct-declaration may
2419 declare multiple components (comma-separated), so we must use
2420 chainon to join them, although when parsing each
2421 struct-declaration we can use TREE_CHAIN directly.
2423 The theory behind all this is that there will be more
2424 semicolon separated fields than comma separated fields, and
2425 so we'll be minimizing the number of node traversals required
2426 by chainon. */
2427 tree contents;
2428 timevar_push (TV_PARSE_STRUCT);
2429 contents = NULL_TREE;
2430 c_parser_consume_token (parser);
2431 /* Handle the Objective-C @defs construct,
2432 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2433 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2435 tree name;
2436 gcc_assert (c_dialect_objc ());
2437 c_parser_consume_token (parser);
2438 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2439 goto end_at_defs;
2440 if (c_parser_next_token_is (parser, CPP_NAME)
2441 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2443 name = c_parser_peek_token (parser)->value;
2444 c_parser_consume_token (parser);
2446 else
2448 c_parser_error (parser, "expected class name");
2449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2450 goto end_at_defs;
2452 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2453 "expected %<)%>");
2454 contents = nreverse (objc_get_class_ivars (name));
2456 end_at_defs:
2457 /* Parse the struct-declarations and semicolons. Problems with
2458 semicolons are diagnosed here; empty structures are diagnosed
2459 elsewhere. */
2460 while (true)
2462 tree decls;
2463 /* Parse any stray semicolon. */
2464 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2466 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2467 "extra semicolon in struct or union specified");
2468 c_parser_consume_token (parser);
2469 continue;
2471 /* Stop if at the end of the struct or union contents. */
2472 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2474 c_parser_consume_token (parser);
2475 break;
2477 /* Accept #pragmas at struct scope. */
2478 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2480 c_parser_pragma (parser, pragma_external);
2481 continue;
2483 /* Parse some comma-separated declarations, but not the
2484 trailing semicolon if any. */
2485 decls = c_parser_struct_declaration (parser);
2486 contents = chainon (decls, contents);
2487 /* If no semicolon follows, either we have a parse error or
2488 are at the end of the struct or union and should
2489 pedwarn. */
2490 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2491 c_parser_consume_token (parser);
2492 else
2494 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2495 pedwarn (c_parser_peek_token (parser)->location, 0,
2496 "no semicolon at end of struct or union");
2497 else if (parser->error
2498 || !c_parser_next_token_starts_declspecs (parser))
2500 c_parser_error (parser, "expected %<;%>");
2501 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2502 break;
2505 /* If we come here, we have already emitted an error
2506 for an expected `;', identifier or `(', and we also
2507 recovered already. Go on with the next field. */
2510 postfix_attrs = c_parser_attributes (parser);
2511 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2512 chainon (attrs, postfix_attrs), struct_info);
2513 ret.kind = ctsk_tagdef;
2514 ret.expr = NULL_TREE;
2515 ret.expr_const_operands = true;
2516 timevar_pop (TV_PARSE_STRUCT);
2517 return ret;
2519 else if (!ident)
2521 c_parser_error (parser, "expected %<{%>");
2522 ret.spec = error_mark_node;
2523 ret.kind = ctsk_tagref;
2524 ret.expr = NULL_TREE;
2525 ret.expr_const_operands = true;
2526 return ret;
2528 ret = parser_xref_tag (ident_loc, code, ident);
2529 return ret;
2532 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2533 the trailing semicolon.
2535 struct-declaration:
2536 specifier-qualifier-list struct-declarator-list
2537 static_assert-declaration-no-semi
2539 specifier-qualifier-list:
2540 type-specifier specifier-qualifier-list[opt]
2541 type-qualifier specifier-qualifier-list[opt]
2542 attributes specifier-qualifier-list[opt]
2544 struct-declarator-list:
2545 struct-declarator
2546 struct-declarator-list , attributes[opt] struct-declarator
2548 struct-declarator:
2549 declarator attributes[opt]
2550 declarator[opt] : constant-expression attributes[opt]
2552 GNU extensions:
2554 struct-declaration:
2555 __extension__ struct-declaration
2556 specifier-qualifier-list
2558 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2559 of attributes where shown is a GNU extension. In GNU C, we accept
2560 any expression without commas in the syntax (assignment
2561 expressions, not just conditional expressions); assignment
2562 expressions will be diagnosed as non-constant. */
2564 static tree
2565 c_parser_struct_declaration (c_parser *parser)
2567 struct c_declspecs *specs;
2568 tree prefix_attrs;
2569 tree all_prefix_attrs;
2570 tree decls;
2571 location_t decl_loc;
2572 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2574 int ext;
2575 tree decl;
2576 ext = disable_extension_diagnostics ();
2577 c_parser_consume_token (parser);
2578 decl = c_parser_struct_declaration (parser);
2579 restore_extension_diagnostics (ext);
2580 return decl;
2582 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2584 c_parser_static_assert_declaration_no_semi (parser);
2585 return NULL_TREE;
2587 specs = build_null_declspecs ();
2588 decl_loc = c_parser_peek_token (parser)->location;
2589 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2590 if (parser->error)
2591 return NULL_TREE;
2592 if (!specs->declspecs_seen_p)
2594 c_parser_error (parser, "expected specifier-qualifier-list");
2595 return NULL_TREE;
2597 finish_declspecs (specs);
2598 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2599 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2601 tree ret;
2602 if (specs->typespec_kind == ctsk_none)
2604 pedwarn (decl_loc, OPT_pedantic,
2605 "ISO C forbids member declarations with no members");
2606 shadow_tag_warned (specs, pedantic);
2607 ret = NULL_TREE;
2609 else
2611 /* Support for unnamed structs or unions as members of
2612 structs or unions (which is [a] useful and [b] supports
2613 MS P-SDK). */
2614 tree attrs = NULL;
2616 ret = grokfield (c_parser_peek_token (parser)->location,
2617 build_id_declarator (NULL_TREE), specs,
2618 NULL_TREE, &attrs);
2619 if (ret)
2620 decl_attributes (&ret, attrs, 0);
2622 return ret;
2625 /* Provide better error recovery. Note that a type name here is valid,
2626 and will be treated as a field name. */
2627 if (specs->typespec_kind == ctsk_tagdef
2628 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2629 && c_parser_next_token_starts_declspecs (parser)
2630 && !c_parser_next_token_is (parser, CPP_NAME))
2632 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2633 parser->error = false;
2634 return NULL_TREE;
2637 pending_xref_error ();
2638 prefix_attrs = specs->attrs;
2639 all_prefix_attrs = prefix_attrs;
2640 specs->attrs = NULL_TREE;
2641 decls = NULL_TREE;
2642 while (true)
2644 /* Declaring one or more declarators or un-named bit-fields. */
2645 struct c_declarator *declarator;
2646 bool dummy = false;
2647 if (c_parser_next_token_is (parser, CPP_COLON))
2648 declarator = build_id_declarator (NULL_TREE);
2649 else
2650 declarator = c_parser_declarator (parser,
2651 specs->typespec_kind != ctsk_none,
2652 C_DTR_NORMAL, &dummy);
2653 if (declarator == NULL)
2655 c_parser_skip_to_end_of_block_or_statement (parser);
2656 break;
2658 if (c_parser_next_token_is (parser, CPP_COLON)
2659 || c_parser_next_token_is (parser, CPP_COMMA)
2660 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2661 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2662 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2664 tree postfix_attrs = NULL_TREE;
2665 tree width = NULL_TREE;
2666 tree d;
2667 if (c_parser_next_token_is (parser, CPP_COLON))
2669 c_parser_consume_token (parser);
2670 width = c_parser_expr_no_commas (parser, NULL).value;
2672 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2673 postfix_attrs = c_parser_attributes (parser);
2674 d = grokfield (c_parser_peek_token (parser)->location,
2675 declarator, specs, width, &all_prefix_attrs);
2676 decl_attributes (&d, chainon (postfix_attrs,
2677 all_prefix_attrs), 0);
2678 DECL_CHAIN (d) = decls;
2679 decls = d;
2680 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2681 all_prefix_attrs = chainon (c_parser_attributes (parser),
2682 prefix_attrs);
2683 else
2684 all_prefix_attrs = prefix_attrs;
2685 if (c_parser_next_token_is (parser, CPP_COMMA))
2686 c_parser_consume_token (parser);
2687 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2688 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2690 /* Semicolon consumed in caller. */
2691 break;
2693 else
2695 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2696 break;
2699 else
2701 c_parser_error (parser,
2702 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2703 "%<__attribute__%>");
2704 break;
2707 return decls;
2710 /* Parse a typeof specifier (a GNU extension).
2712 typeof-specifier:
2713 typeof ( expression )
2714 typeof ( type-name )
2717 static struct c_typespec
2718 c_parser_typeof_specifier (c_parser *parser)
2720 struct c_typespec ret;
2721 ret.kind = ctsk_typeof;
2722 ret.spec = error_mark_node;
2723 ret.expr = NULL_TREE;
2724 ret.expr_const_operands = true;
2725 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2726 c_parser_consume_token (parser);
2727 c_inhibit_evaluation_warnings++;
2728 in_typeof++;
2729 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2731 c_inhibit_evaluation_warnings--;
2732 in_typeof--;
2733 return ret;
2735 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2737 struct c_type_name *type = c_parser_type_name (parser);
2738 c_inhibit_evaluation_warnings--;
2739 in_typeof--;
2740 if (type != NULL)
2742 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2743 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2746 else
2748 bool was_vm;
2749 location_t here = c_parser_peek_token (parser)->location;
2750 struct c_expr expr = c_parser_expression (parser);
2751 c_inhibit_evaluation_warnings--;
2752 in_typeof--;
2753 if (TREE_CODE (expr.value) == COMPONENT_REF
2754 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2755 error_at (here, "%<typeof%> applied to a bit-field");
2756 mark_exp_read (expr.value);
2757 ret.spec = TREE_TYPE (expr.value);
2758 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2759 /* This is returned with the type so that when the type is
2760 evaluated, this can be evaluated. */
2761 if (was_vm)
2762 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2763 pop_maybe_used (was_vm);
2765 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2766 return ret;
2769 /* Parse an alignment-specifier.
2771 C11 6.7.5:
2773 alignment-specifier:
2774 _Alignas ( type-name )
2775 _Alignas ( constant-expression )
2778 static tree
2779 c_parser_alignas_specifier (c_parser * parser)
2781 tree ret = error_mark_node;
2782 location_t loc = c_parser_peek_token (parser)->location;
2783 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2784 c_parser_consume_token (parser);
2785 if (!flag_isoc11)
2787 if (flag_isoc99)
2788 pedwarn (loc, OPT_pedantic,
2789 "ISO C99 does not support %<_Alignas%>");
2790 else
2791 pedwarn (loc, OPT_pedantic,
2792 "ISO C90 does not support %<_Alignas%>");
2794 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2795 return ret;
2796 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2798 struct c_type_name *type = c_parser_type_name (parser);
2799 if (type != NULL)
2800 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2802 else
2803 ret = c_parser_expr_no_commas (parser, NULL).value;
2804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2805 return ret;
2808 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2809 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2810 be redeclared; otherwise it may not. KIND indicates which kind of
2811 declarator is wanted. Returns a valid declarator except in the
2812 case of a syntax error in which case NULL is returned. *SEEN_ID is
2813 set to true if an identifier being declared is seen; this is used
2814 to diagnose bad forms of abstract array declarators and to
2815 determine whether an identifier list is syntactically permitted.
2817 declarator:
2818 pointer[opt] direct-declarator
2820 direct-declarator:
2821 identifier
2822 ( attributes[opt] declarator )
2823 direct-declarator array-declarator
2824 direct-declarator ( parameter-type-list )
2825 direct-declarator ( identifier-list[opt] )
2827 pointer:
2828 * type-qualifier-list[opt]
2829 * type-qualifier-list[opt] pointer
2831 type-qualifier-list:
2832 type-qualifier
2833 attributes
2834 type-qualifier-list type-qualifier
2835 type-qualifier-list attributes
2837 parameter-type-list:
2838 parameter-list
2839 parameter-list , ...
2841 parameter-list:
2842 parameter-declaration
2843 parameter-list , parameter-declaration
2845 parameter-declaration:
2846 declaration-specifiers declarator attributes[opt]
2847 declaration-specifiers abstract-declarator[opt] attributes[opt]
2849 identifier-list:
2850 identifier
2851 identifier-list , identifier
2853 abstract-declarator:
2854 pointer
2855 pointer[opt] direct-abstract-declarator
2857 direct-abstract-declarator:
2858 ( attributes[opt] abstract-declarator )
2859 direct-abstract-declarator[opt] array-declarator
2860 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2862 GNU extensions:
2864 direct-declarator:
2865 direct-declarator ( parameter-forward-declarations
2866 parameter-type-list[opt] )
2868 direct-abstract-declarator:
2869 direct-abstract-declarator[opt] ( parameter-forward-declarations
2870 parameter-type-list[opt] )
2872 parameter-forward-declarations:
2873 parameter-list ;
2874 parameter-forward-declarations parameter-list ;
2876 The uses of attributes shown above are GNU extensions.
2878 Some forms of array declarator are not included in C99 in the
2879 syntax for abstract declarators; these are disallowed elsewhere.
2880 This may be a defect (DR#289).
2882 This function also accepts an omitted abstract declarator as being
2883 an abstract declarator, although not part of the formal syntax. */
2885 static struct c_declarator *
2886 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2887 bool *seen_id)
2889 /* Parse any initial pointer part. */
2890 if (c_parser_next_token_is (parser, CPP_MULT))
2892 struct c_declspecs *quals_attrs = build_null_declspecs ();
2893 struct c_declarator *inner;
2894 c_parser_consume_token (parser);
2895 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2896 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2897 if (inner == NULL)
2898 return NULL;
2899 else
2900 return make_pointer_declarator (quals_attrs, inner);
2902 /* Now we have a direct declarator, direct abstract declarator or
2903 nothing (which counts as a direct abstract declarator here). */
2904 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2907 /* Parse a direct declarator or direct abstract declarator; arguments
2908 as c_parser_declarator. */
2910 static struct c_declarator *
2911 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2912 bool *seen_id)
2914 /* The direct declarator must start with an identifier (possibly
2915 omitted) or a parenthesized declarator (possibly abstract). In
2916 an ordinary declarator, initial parentheses must start a
2917 parenthesized declarator. In an abstract declarator or parameter
2918 declarator, they could start a parenthesized declarator or a
2919 parameter list. To tell which, the open parenthesis and any
2920 following attributes must be read. If a declaration specifier
2921 follows, then it is a parameter list; if the specifier is a
2922 typedef name, there might be an ambiguity about redeclaring it,
2923 which is resolved in the direction of treating it as a typedef
2924 name. If a close parenthesis follows, it is also an empty
2925 parameter list, as the syntax does not permit empty abstract
2926 declarators. Otherwise, it is a parenthesized declarator (in
2927 which case the analysis may be repeated inside it, recursively).
2929 ??? There is an ambiguity in a parameter declaration "int
2930 (__attribute__((foo)) x)", where x is not a typedef name: it
2931 could be an abstract declarator for a function, or declare x with
2932 parentheses. The proper resolution of this ambiguity needs
2933 documenting. At present we follow an accident of the old
2934 parser's implementation, whereby the first parameter must have
2935 some declaration specifiers other than just attributes. Thus as
2936 a parameter declaration it is treated as a parenthesized
2937 parameter named x, and as an abstract declarator it is
2938 rejected.
2940 ??? Also following the old parser, attributes inside an empty
2941 parameter list are ignored, making it a list not yielding a
2942 prototype, rather than giving an error or making it have one
2943 parameter with implicit type int.
2945 ??? Also following the old parser, typedef names may be
2946 redeclared in declarators, but not Objective-C class names. */
2948 if (kind != C_DTR_ABSTRACT
2949 && c_parser_next_token_is (parser, CPP_NAME)
2950 && ((type_seen_p
2951 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2952 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2953 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2955 struct c_declarator *inner
2956 = build_id_declarator (c_parser_peek_token (parser)->value);
2957 *seen_id = true;
2958 inner->id_loc = c_parser_peek_token (parser)->location;
2959 c_parser_consume_token (parser);
2960 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2963 if (kind != C_DTR_NORMAL
2964 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2966 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2967 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2970 /* Either we are at the end of an abstract declarator, or we have
2971 parentheses. */
2973 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2975 tree attrs;
2976 struct c_declarator *inner;
2977 c_parser_consume_token (parser);
2978 attrs = c_parser_attributes (parser);
2979 if (kind != C_DTR_NORMAL
2980 && (c_parser_next_token_starts_declspecs (parser)
2981 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2983 struct c_arg_info *args
2984 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2985 attrs);
2986 if (args == NULL)
2987 return NULL;
2988 else
2990 inner
2991 = build_function_declarator (args,
2992 build_id_declarator (NULL_TREE));
2993 return c_parser_direct_declarator_inner (parser, *seen_id,
2994 inner);
2997 /* A parenthesized declarator. */
2998 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2999 if (inner != NULL && attrs != NULL)
3000 inner = build_attrs_declarator (attrs, inner);
3001 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3003 c_parser_consume_token (parser);
3004 if (inner == NULL)
3005 return NULL;
3006 else
3007 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3009 else
3011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3012 "expected %<)%>");
3013 return NULL;
3016 else
3018 if (kind == C_DTR_NORMAL)
3020 c_parser_error (parser, "expected identifier or %<(%>");
3021 return NULL;
3023 else
3024 return build_id_declarator (NULL_TREE);
3028 /* Parse part of a direct declarator or direct abstract declarator,
3029 given that some (in INNER) has already been parsed; ID_PRESENT is
3030 true if an identifier is present, false for an abstract
3031 declarator. */
3033 static struct c_declarator *
3034 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3035 struct c_declarator *inner)
3037 /* Parse a sequence of array declarators and parameter lists. */
3038 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3040 location_t brace_loc = c_parser_peek_token (parser)->location;
3041 struct c_declarator *declarator;
3042 struct c_declspecs *quals_attrs = build_null_declspecs ();
3043 bool static_seen;
3044 bool star_seen;
3045 tree dimen;
3046 c_parser_consume_token (parser);
3047 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3048 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3049 if (static_seen)
3050 c_parser_consume_token (parser);
3051 if (static_seen && !quals_attrs->declspecs_seen_p)
3052 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3053 if (!quals_attrs->declspecs_seen_p)
3054 quals_attrs = NULL;
3055 /* If "static" is present, there must be an array dimension.
3056 Otherwise, there may be a dimension, "*", or no
3057 dimension. */
3058 if (static_seen)
3060 star_seen = false;
3061 dimen = c_parser_expr_no_commas (parser, NULL).value;
3063 else
3065 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3067 dimen = NULL_TREE;
3068 star_seen = false;
3070 else if (c_parser_next_token_is (parser, CPP_MULT))
3072 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3074 dimen = NULL_TREE;
3075 star_seen = true;
3076 c_parser_consume_token (parser);
3078 else
3080 star_seen = false;
3081 dimen = c_parser_expr_no_commas (parser, NULL).value;
3084 else
3086 star_seen = false;
3087 dimen = c_parser_expr_no_commas (parser, NULL).value;
3090 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3091 c_parser_consume_token (parser);
3092 else
3094 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3095 "expected %<]%>");
3096 return NULL;
3098 if (dimen)
3099 mark_exp_read (dimen);
3100 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3101 static_seen, star_seen);
3102 if (declarator == NULL)
3103 return NULL;
3104 inner = set_array_declarator_inner (declarator, inner);
3105 return c_parser_direct_declarator_inner (parser, id_present, inner);
3107 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3109 tree attrs;
3110 struct c_arg_info *args;
3111 c_parser_consume_token (parser);
3112 attrs = c_parser_attributes (parser);
3113 args = c_parser_parms_declarator (parser, id_present, attrs);
3114 if (args == NULL)
3115 return NULL;
3116 else
3118 inner = build_function_declarator (args, inner);
3119 return c_parser_direct_declarator_inner (parser, id_present, inner);
3122 return inner;
3125 /* Parse a parameter list or identifier list, including the closing
3126 parenthesis but not the opening one. ATTRS are the attributes at
3127 the start of the list. ID_LIST_OK is true if an identifier list is
3128 acceptable; such a list must not have attributes at the start. */
3130 static struct c_arg_info *
3131 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3133 push_scope ();
3134 declare_parm_level ();
3135 /* If the list starts with an identifier, it is an identifier list.
3136 Otherwise, it is either a prototype list or an empty list. */
3137 if (id_list_ok
3138 && !attrs
3139 && c_parser_next_token_is (parser, CPP_NAME)
3140 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3142 /* Look ahead to detect typos in type names. */
3143 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3144 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3145 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3146 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3148 tree list = NULL_TREE, *nextp = &list;
3149 while (c_parser_next_token_is (parser, CPP_NAME)
3150 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3152 *nextp = build_tree_list (NULL_TREE,
3153 c_parser_peek_token (parser)->value);
3154 nextp = & TREE_CHAIN (*nextp);
3155 c_parser_consume_token (parser);
3156 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3157 break;
3158 c_parser_consume_token (parser);
3159 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3161 c_parser_error (parser, "expected identifier");
3162 break;
3165 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3167 struct c_arg_info *ret = build_arg_info ();
3168 ret->types = list;
3169 c_parser_consume_token (parser);
3170 pop_scope ();
3171 return ret;
3173 else
3175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3176 "expected %<)%>");
3177 pop_scope ();
3178 return NULL;
3181 else
3183 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3184 NULL);
3185 pop_scope ();
3186 return ret;
3190 /* Parse a parameter list (possibly empty), including the closing
3191 parenthesis but not the opening one. ATTRS are the attributes at
3192 the start of the list. EXPR is NULL or an expression that needs to
3193 be evaluated for the side effects of array size expressions in the
3194 parameters. */
3196 static struct c_arg_info *
3197 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3199 bool bad_parm = false;
3201 /* ??? Following the old parser, forward parameter declarations may
3202 use abstract declarators, and if no real parameter declarations
3203 follow the forward declarations then this is not diagnosed. Also
3204 note as above that attributes are ignored as the only contents of
3205 the parentheses, or as the only contents after forward
3206 declarations. */
3207 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3209 struct c_arg_info *ret = build_arg_info ();
3210 c_parser_consume_token (parser);
3211 return ret;
3213 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3215 struct c_arg_info *ret = build_arg_info ();
3217 if (flag_allow_parameterless_variadic_functions)
3219 /* F (...) is allowed. */
3220 ret->types = NULL_TREE;
3222 else
3224 /* Suppress -Wold-style-definition for this case. */
3225 ret->types = error_mark_node;
3226 error_at (c_parser_peek_token (parser)->location,
3227 "ISO C requires a named argument before %<...%>");
3229 c_parser_consume_token (parser);
3230 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3232 c_parser_consume_token (parser);
3233 return ret;
3235 else
3237 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3238 "expected %<)%>");
3239 return NULL;
3242 /* Nonempty list of parameters, either terminated with semicolon
3243 (forward declarations; recurse) or with close parenthesis (normal
3244 function) or with ", ... )" (variadic function). */
3245 while (true)
3247 /* Parse a parameter. */
3248 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3249 attrs = NULL_TREE;
3250 if (parm == NULL)
3251 bad_parm = true;
3252 else
3253 push_parm_decl (parm, &expr);
3254 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3256 tree new_attrs;
3257 c_parser_consume_token (parser);
3258 mark_forward_parm_decls ();
3259 new_attrs = c_parser_attributes (parser);
3260 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3262 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3264 c_parser_consume_token (parser);
3265 if (bad_parm)
3266 return NULL;
3267 else
3268 return get_parm_info (false, expr);
3270 if (!c_parser_require (parser, CPP_COMMA,
3271 "expected %<;%>, %<,%> or %<)%>"))
3273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3274 return NULL;
3276 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3278 c_parser_consume_token (parser);
3279 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3281 c_parser_consume_token (parser);
3282 if (bad_parm)
3283 return NULL;
3284 else
3285 return get_parm_info (true, expr);
3287 else
3289 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3290 "expected %<)%>");
3291 return NULL;
3297 /* Parse a parameter declaration. ATTRS are the attributes at the
3298 start of the declaration if it is the first parameter. */
3300 static struct c_parm *
3301 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3303 struct c_declspecs *specs;
3304 struct c_declarator *declarator;
3305 tree prefix_attrs;
3306 tree postfix_attrs = NULL_TREE;
3307 bool dummy = false;
3309 /* Accept #pragmas between parameter declarations. */
3310 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3311 c_parser_pragma (parser, pragma_external);
3313 if (!c_parser_next_token_starts_declspecs (parser))
3315 c_token *token = c_parser_peek_token (parser);
3316 if (parser->error)
3317 return NULL;
3318 c_parser_set_source_position_from_token (token);
3319 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3321 error ("unknown type name %qE", token->value);
3322 parser->error = true;
3324 /* ??? In some Objective-C cases '...' isn't applicable so there
3325 should be a different message. */
3326 else
3327 c_parser_error (parser,
3328 "expected declaration specifiers or %<...%>");
3329 c_parser_skip_to_end_of_parameter (parser);
3330 return NULL;
3332 specs = build_null_declspecs ();
3333 if (attrs)
3335 declspecs_add_attrs (specs, attrs);
3336 attrs = NULL_TREE;
3338 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3339 finish_declspecs (specs);
3340 pending_xref_error ();
3341 prefix_attrs = specs->attrs;
3342 specs->attrs = NULL_TREE;
3343 declarator = c_parser_declarator (parser,
3344 specs->typespec_kind != ctsk_none,
3345 C_DTR_PARM, &dummy);
3346 if (declarator == NULL)
3348 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3349 return NULL;
3351 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3352 postfix_attrs = c_parser_attributes (parser);
3353 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3354 declarator);
3357 /* Parse a string literal in an asm expression. It should not be
3358 translated, and wide string literals are an error although
3359 permitted by the syntax. This is a GNU extension.
3361 asm-string-literal:
3362 string-literal
3364 ??? At present, following the old parser, the caller needs to have
3365 set lex_untranslated_string to 1. It would be better to follow the
3366 C++ parser rather than using this kludge. */
3368 static tree
3369 c_parser_asm_string_literal (c_parser *parser)
3371 tree str;
3372 int save_flag = warn_overlength_strings;
3373 warn_overlength_strings = 0;
3374 if (c_parser_next_token_is (parser, CPP_STRING))
3376 str = c_parser_peek_token (parser)->value;
3377 c_parser_consume_token (parser);
3379 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3381 error_at (c_parser_peek_token (parser)->location,
3382 "wide string literal in %<asm%>");
3383 str = build_string (1, "");
3384 c_parser_consume_token (parser);
3386 else
3388 c_parser_error (parser, "expected string literal");
3389 str = NULL_TREE;
3391 warn_overlength_strings = save_flag;
3392 return str;
3395 /* Parse a simple asm expression. This is used in restricted
3396 contexts, where a full expression with inputs and outputs does not
3397 make sense. This is a GNU extension.
3399 simple-asm-expr:
3400 asm ( asm-string-literal )
3403 static tree
3404 c_parser_simple_asm_expr (c_parser *parser)
3406 tree str;
3407 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3408 /* ??? Follow the C++ parser rather than using the
3409 lex_untranslated_string kludge. */
3410 parser->lex_untranslated_string = true;
3411 c_parser_consume_token (parser);
3412 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3414 parser->lex_untranslated_string = false;
3415 return NULL_TREE;
3417 str = c_parser_asm_string_literal (parser);
3418 parser->lex_untranslated_string = false;
3419 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3421 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3422 return NULL_TREE;
3424 return str;
3427 static tree
3428 c_parser_attribute_any_word (c_parser *parser)
3430 tree attr_name = NULL_TREE;
3432 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3434 /* ??? See comment above about what keywords are accepted here. */
3435 bool ok;
3436 switch (c_parser_peek_token (parser)->keyword)
3438 case RID_STATIC:
3439 case RID_UNSIGNED:
3440 case RID_LONG:
3441 case RID_INT128:
3442 case RID_CONST:
3443 case RID_EXTERN:
3444 case RID_REGISTER:
3445 case RID_TYPEDEF:
3446 case RID_SHORT:
3447 case RID_INLINE:
3448 case RID_NORETURN:
3449 case RID_VOLATILE:
3450 case RID_SIGNED:
3451 case RID_AUTO:
3452 case RID_RESTRICT:
3453 case RID_COMPLEX:
3454 case RID_THREAD:
3455 case RID_INT:
3456 case RID_CHAR:
3457 case RID_FLOAT:
3458 case RID_DOUBLE:
3459 case RID_VOID:
3460 case RID_DFLOAT32:
3461 case RID_DFLOAT64:
3462 case RID_DFLOAT128:
3463 case RID_BOOL:
3464 case RID_FRACT:
3465 case RID_ACCUM:
3466 case RID_SAT:
3467 case RID_TRANSACTION_ATOMIC:
3468 case RID_TRANSACTION_CANCEL:
3469 ok = true;
3470 break;
3471 default:
3472 ok = false;
3473 break;
3475 if (!ok)
3476 return NULL_TREE;
3478 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3479 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3481 else if (c_parser_next_token_is (parser, CPP_NAME))
3482 attr_name = c_parser_peek_token (parser)->value;
3484 return attr_name;
3487 /* Parse (possibly empty) attributes. This is a GNU extension.
3489 attributes:
3490 empty
3491 attributes attribute
3493 attribute:
3494 __attribute__ ( ( attribute-list ) )
3496 attribute-list:
3497 attrib
3498 attribute_list , attrib
3500 attrib:
3501 empty
3502 any-word
3503 any-word ( identifier )
3504 any-word ( identifier , nonempty-expr-list )
3505 any-word ( expr-list )
3507 where the "identifier" must not be declared as a type, and
3508 "any-word" may be any identifier (including one declared as a
3509 type), a reserved word storage class specifier, type specifier or
3510 type qualifier. ??? This still leaves out most reserved keywords
3511 (following the old parser), shouldn't we include them, and why not
3512 allow identifiers declared as types to start the arguments? */
3514 static tree
3515 c_parser_attributes (c_parser *parser)
3517 tree attrs = NULL_TREE;
3518 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3520 /* ??? Follow the C++ parser rather than using the
3521 lex_untranslated_string kludge. */
3522 parser->lex_untranslated_string = true;
3523 c_parser_consume_token (parser);
3524 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3526 parser->lex_untranslated_string = false;
3527 return attrs;
3529 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3531 parser->lex_untranslated_string = false;
3532 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3533 return attrs;
3535 /* Parse the attribute list. */
3536 while (c_parser_next_token_is (parser, CPP_COMMA)
3537 || c_parser_next_token_is (parser, CPP_NAME)
3538 || c_parser_next_token_is (parser, CPP_KEYWORD))
3540 tree attr, attr_name, attr_args;
3541 VEC(tree,gc) *expr_list;
3542 if (c_parser_next_token_is (parser, CPP_COMMA))
3544 c_parser_consume_token (parser);
3545 continue;
3548 attr_name = c_parser_attribute_any_word (parser);
3549 if (attr_name == NULL)
3550 break;
3551 c_parser_consume_token (parser);
3552 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3554 attr = build_tree_list (attr_name, NULL_TREE);
3555 attrs = chainon (attrs, attr);
3556 continue;
3558 c_parser_consume_token (parser);
3559 /* Parse the attribute contents. If they start with an
3560 identifier which is followed by a comma or close
3561 parenthesis, then the arguments start with that
3562 identifier; otherwise they are an expression list.
3563 In objective-c the identifier may be a classname. */
3564 if (c_parser_next_token_is (parser, CPP_NAME)
3565 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3566 || (c_dialect_objc ()
3567 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3568 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3569 || (c_parser_peek_2nd_token (parser)->type
3570 == CPP_CLOSE_PAREN)))
3572 tree arg1 = c_parser_peek_token (parser)->value;
3573 c_parser_consume_token (parser);
3574 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3575 attr_args = build_tree_list (NULL_TREE, arg1);
3576 else
3578 tree tree_list;
3579 c_parser_consume_token (parser);
3580 expr_list = c_parser_expr_list (parser, false, true, NULL);
3581 tree_list = build_tree_list_vec (expr_list);
3582 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3583 release_tree_vector (expr_list);
3586 else
3588 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3589 attr_args = NULL_TREE;
3590 else
3592 expr_list = c_parser_expr_list (parser, false, true, NULL);
3593 attr_args = build_tree_list_vec (expr_list);
3594 release_tree_vector (expr_list);
3597 attr = build_tree_list (attr_name, attr_args);
3598 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3599 c_parser_consume_token (parser);
3600 else
3602 parser->lex_untranslated_string = false;
3603 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3604 "expected %<)%>");
3605 return attrs;
3607 attrs = chainon (attrs, attr);
3609 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3610 c_parser_consume_token (parser);
3611 else
3613 parser->lex_untranslated_string = false;
3614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3615 "expected %<)%>");
3616 return attrs;
3618 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3619 c_parser_consume_token (parser);
3620 else
3622 parser->lex_untranslated_string = false;
3623 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3624 "expected %<)%>");
3625 return attrs;
3627 parser->lex_untranslated_string = false;
3629 return attrs;
3632 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3634 type-name:
3635 specifier-qualifier-list abstract-declarator[opt]
3638 static struct c_type_name *
3639 c_parser_type_name (c_parser *parser)
3641 struct c_declspecs *specs = build_null_declspecs ();
3642 struct c_declarator *declarator;
3643 struct c_type_name *ret;
3644 bool dummy = false;
3645 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3646 if (!specs->declspecs_seen_p)
3648 c_parser_error (parser, "expected specifier-qualifier-list");
3649 return NULL;
3651 if (specs->type != error_mark_node)
3653 pending_xref_error ();
3654 finish_declspecs (specs);
3656 declarator = c_parser_declarator (parser,
3657 specs->typespec_kind != ctsk_none,
3658 C_DTR_ABSTRACT, &dummy);
3659 if (declarator == NULL)
3660 return NULL;
3661 ret = XOBNEW (&parser_obstack, struct c_type_name);
3662 ret->specs = specs;
3663 ret->declarator = declarator;
3664 return ret;
3667 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3669 initializer:
3670 assignment-expression
3671 { initializer-list }
3672 { initializer-list , }
3674 initializer-list:
3675 designation[opt] initializer
3676 initializer-list , designation[opt] initializer
3678 designation:
3679 designator-list =
3681 designator-list:
3682 designator
3683 designator-list designator
3685 designator:
3686 array-designator
3687 . identifier
3689 array-designator:
3690 [ constant-expression ]
3692 GNU extensions:
3694 initializer:
3697 designation:
3698 array-designator
3699 identifier :
3701 array-designator:
3702 [ constant-expression ... constant-expression ]
3704 Any expression without commas is accepted in the syntax for the
3705 constant-expressions, with non-constant expressions rejected later.
3707 This function is only used for top-level initializers; for nested
3708 ones, see c_parser_initval. */
3710 static struct c_expr
3711 c_parser_initializer (c_parser *parser)
3713 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3714 return c_parser_braced_init (parser, NULL_TREE, false);
3715 else
3717 struct c_expr ret;
3718 location_t loc = c_parser_peek_token (parser)->location;
3719 ret = c_parser_expr_no_commas (parser, NULL);
3720 if (TREE_CODE (ret.value) != STRING_CST
3721 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3722 ret = default_function_array_read_conversion (loc, ret);
3723 return ret;
3727 /* Parse a braced initializer list. TYPE is the type specified for a
3728 compound literal, and NULL_TREE for other initializers and for
3729 nested braced lists. NESTED_P is true for nested braced lists,
3730 false for the list of a compound literal or the list that is the
3731 top-level initializer in a declaration. */
3733 static struct c_expr
3734 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3736 struct c_expr ret;
3737 struct obstack braced_init_obstack;
3738 location_t brace_loc = c_parser_peek_token (parser)->location;
3739 gcc_obstack_init (&braced_init_obstack);
3740 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3741 c_parser_consume_token (parser);
3742 if (nested_p)
3743 push_init_level (0, &braced_init_obstack);
3744 else
3745 really_start_incremental_init (type);
3746 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3748 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3750 else
3752 /* Parse a non-empty initializer list, possibly with a trailing
3753 comma. */
3754 while (true)
3756 c_parser_initelt (parser, &braced_init_obstack);
3757 if (parser->error)
3758 break;
3759 if (c_parser_next_token_is (parser, CPP_COMMA))
3760 c_parser_consume_token (parser);
3761 else
3762 break;
3763 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3764 break;
3767 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3769 ret.value = error_mark_node;
3770 ret.original_code = ERROR_MARK;
3771 ret.original_type = NULL;
3772 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3773 pop_init_level (0, &braced_init_obstack);
3774 obstack_free (&braced_init_obstack, NULL);
3775 return ret;
3777 c_parser_consume_token (parser);
3778 ret = pop_init_level (0, &braced_init_obstack);
3779 obstack_free (&braced_init_obstack, NULL);
3780 return ret;
3783 /* Parse a nested initializer, including designators. */
3785 static void
3786 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3788 /* Parse any designator or designator list. A single array
3789 designator may have the subsequent "=" omitted in GNU C, but a
3790 longer list or a structure member designator may not. */
3791 if (c_parser_next_token_is (parser, CPP_NAME)
3792 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3794 /* Old-style structure member designator. */
3795 set_init_label (c_parser_peek_token (parser)->value,
3796 braced_init_obstack);
3797 /* Use the colon as the error location. */
3798 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3799 "obsolete use of designated initializer with %<:%>");
3800 c_parser_consume_token (parser);
3801 c_parser_consume_token (parser);
3803 else
3805 /* des_seen is 0 if there have been no designators, 1 if there
3806 has been a single array designator and 2 otherwise. */
3807 int des_seen = 0;
3808 /* Location of a designator. */
3809 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3810 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3811 || c_parser_next_token_is (parser, CPP_DOT))
3813 int des_prev = des_seen;
3814 if (!des_seen)
3815 des_loc = c_parser_peek_token (parser)->location;
3816 if (des_seen < 2)
3817 des_seen++;
3818 if (c_parser_next_token_is (parser, CPP_DOT))
3820 des_seen = 2;
3821 c_parser_consume_token (parser);
3822 if (c_parser_next_token_is (parser, CPP_NAME))
3824 set_init_label (c_parser_peek_token (parser)->value,
3825 braced_init_obstack);
3826 c_parser_consume_token (parser);
3828 else
3830 struct c_expr init;
3831 init.value = error_mark_node;
3832 init.original_code = ERROR_MARK;
3833 init.original_type = NULL;
3834 c_parser_error (parser, "expected identifier");
3835 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3836 process_init_element (init, false, braced_init_obstack);
3837 return;
3840 else
3842 tree first, second;
3843 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3844 /* ??? Following the old parser, [ objc-receiver
3845 objc-message-args ] is accepted as an initializer,
3846 being distinguished from a designator by what follows
3847 the first assignment expression inside the square
3848 brackets, but after a first array designator a
3849 subsequent square bracket is for Objective-C taken to
3850 start an expression, using the obsolete form of
3851 designated initializer without '=', rather than
3852 possibly being a second level of designation: in LALR
3853 terms, the '[' is shifted rather than reducing
3854 designator to designator-list. */
3855 if (des_prev == 1 && c_dialect_objc ())
3857 des_seen = des_prev;
3858 break;
3860 if (des_prev == 0 && c_dialect_objc ())
3862 /* This might be an array designator or an
3863 Objective-C message expression. If the former,
3864 continue parsing here; if the latter, parse the
3865 remainder of the initializer given the starting
3866 primary-expression. ??? It might make sense to
3867 distinguish when des_prev == 1 as well; see
3868 previous comment. */
3869 tree rec, args;
3870 struct c_expr mexpr;
3871 c_parser_consume_token (parser);
3872 if (c_parser_peek_token (parser)->type == CPP_NAME
3873 && ((c_parser_peek_token (parser)->id_kind
3874 == C_ID_TYPENAME)
3875 || (c_parser_peek_token (parser)->id_kind
3876 == C_ID_CLASSNAME)))
3878 /* Type name receiver. */
3879 tree id = c_parser_peek_token (parser)->value;
3880 c_parser_consume_token (parser);
3881 rec = objc_get_class_reference (id);
3882 goto parse_message_args;
3884 first = c_parser_expr_no_commas (parser, NULL).value;
3885 mark_exp_read (first);
3886 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3887 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3888 goto array_desig_after_first;
3889 /* Expression receiver. So far only one part
3890 without commas has been parsed; there might be
3891 more of the expression. */
3892 rec = first;
3893 while (c_parser_next_token_is (parser, CPP_COMMA))
3895 struct c_expr next;
3896 location_t comma_loc, exp_loc;
3897 comma_loc = c_parser_peek_token (parser)->location;
3898 c_parser_consume_token (parser);
3899 exp_loc = c_parser_peek_token (parser)->location;
3900 next = c_parser_expr_no_commas (parser, NULL);
3901 next = default_function_array_read_conversion (exp_loc,
3902 next);
3903 rec = build_compound_expr (comma_loc, rec, next.value);
3905 parse_message_args:
3906 /* Now parse the objc-message-args. */
3907 args = c_parser_objc_message_args (parser);
3908 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3909 "expected %<]%>");
3910 mexpr.value
3911 = objc_build_message_expr (rec, args);
3912 mexpr.original_code = ERROR_MARK;
3913 mexpr.original_type = NULL;
3914 /* Now parse and process the remainder of the
3915 initializer, starting with this message
3916 expression as a primary-expression. */
3917 c_parser_initval (parser, &mexpr, braced_init_obstack);
3918 return;
3920 c_parser_consume_token (parser);
3921 first = c_parser_expr_no_commas (parser, NULL).value;
3922 mark_exp_read (first);
3923 array_desig_after_first:
3924 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3926 ellipsis_loc = c_parser_peek_token (parser)->location;
3927 c_parser_consume_token (parser);
3928 second = c_parser_expr_no_commas (parser, NULL).value;
3929 mark_exp_read (second);
3931 else
3932 second = NULL_TREE;
3933 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3935 c_parser_consume_token (parser);
3936 set_init_index (first, second, braced_init_obstack);
3937 if (second)
3938 pedwarn (ellipsis_loc, OPT_pedantic,
3939 "ISO C forbids specifying range of elements to initialize");
3941 else
3942 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3943 "expected %<]%>");
3946 if (des_seen >= 1)
3948 if (c_parser_next_token_is (parser, CPP_EQ))
3950 if (!flag_isoc99)
3951 pedwarn (des_loc, OPT_pedantic,
3952 "ISO C90 forbids specifying subobject to initialize");
3953 c_parser_consume_token (parser);
3955 else
3957 if (des_seen == 1)
3958 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3959 "obsolete use of designated initializer without %<=%>");
3960 else
3962 struct c_expr init;
3963 init.value = error_mark_node;
3964 init.original_code = ERROR_MARK;
3965 init.original_type = NULL;
3966 c_parser_error (parser, "expected %<=%>");
3967 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3968 process_init_element (init, false, braced_init_obstack);
3969 return;
3974 c_parser_initval (parser, NULL, braced_init_obstack);
3977 /* Parse a nested initializer; as c_parser_initializer but parses
3978 initializers within braced lists, after any designators have been
3979 applied. If AFTER is not NULL then it is an Objective-C message
3980 expression which is the primary-expression starting the
3981 initializer. */
3983 static void
3984 c_parser_initval (c_parser *parser, struct c_expr *after,
3985 struct obstack * braced_init_obstack)
3987 struct c_expr init;
3988 gcc_assert (!after || c_dialect_objc ());
3989 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3990 init = c_parser_braced_init (parser, NULL_TREE, true);
3991 else
3993 location_t loc = c_parser_peek_token (parser)->location;
3994 init = c_parser_expr_no_commas (parser, after);
3995 if (init.value != NULL_TREE
3996 && TREE_CODE (init.value) != STRING_CST
3997 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3998 init = default_function_array_read_conversion (loc, init);
4000 process_init_element (init, false, braced_init_obstack);
4003 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4004 C99 6.8.2).
4006 compound-statement:
4007 { block-item-list[opt] }
4008 { label-declarations block-item-list }
4010 block-item-list:
4011 block-item
4012 block-item-list block-item
4014 block-item:
4015 nested-declaration
4016 statement
4018 nested-declaration:
4019 declaration
4021 GNU extensions:
4023 compound-statement:
4024 { label-declarations block-item-list }
4026 nested-declaration:
4027 __extension__ nested-declaration
4028 nested-function-definition
4030 label-declarations:
4031 label-declaration
4032 label-declarations label-declaration
4034 label-declaration:
4035 __label__ identifier-list ;
4037 Allowing the mixing of declarations and code is new in C99. The
4038 GNU syntax also permits (not shown above) labels at the end of
4039 compound statements, which yield an error. We don't allow labels
4040 on declarations; this might seem like a natural extension, but
4041 there would be a conflict between attributes on the label and
4042 prefix attributes on the declaration. ??? The syntax follows the
4043 old parser in requiring something after label declarations.
4044 Although they are erroneous if the labels declared aren't defined,
4045 is it useful for the syntax to be this way?
4047 OpenMP:
4049 block-item:
4050 openmp-directive
4052 openmp-directive:
4053 barrier-directive
4054 flush-directive */
4056 static tree
4057 c_parser_compound_statement (c_parser *parser)
4059 tree stmt;
4060 location_t brace_loc;
4061 brace_loc = c_parser_peek_token (parser)->location;
4062 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4064 /* Ensure a scope is entered and left anyway to avoid confusion
4065 if we have just prepared to enter a function body. */
4066 stmt = c_begin_compound_stmt (true);
4067 c_end_compound_stmt (brace_loc, stmt, true);
4068 return error_mark_node;
4070 stmt = c_begin_compound_stmt (true);
4071 c_parser_compound_statement_nostart (parser);
4072 return c_end_compound_stmt (brace_loc, stmt, true);
4075 /* Parse a compound statement except for the opening brace. This is
4076 used for parsing both compound statements and statement expressions
4077 (which follow different paths to handling the opening). */
4079 static void
4080 c_parser_compound_statement_nostart (c_parser *parser)
4082 bool last_stmt = false;
4083 bool last_label = false;
4084 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4085 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4086 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4088 c_parser_consume_token (parser);
4089 return;
4091 mark_valid_location_for_stdc_pragma (true);
4092 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4094 /* Read zero or more forward-declarations for labels that nested
4095 functions can jump to. */
4096 mark_valid_location_for_stdc_pragma (false);
4097 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4099 label_loc = c_parser_peek_token (parser)->location;
4100 c_parser_consume_token (parser);
4101 /* Any identifiers, including those declared as type names,
4102 are OK here. */
4103 while (true)
4105 tree label;
4106 if (c_parser_next_token_is_not (parser, CPP_NAME))
4108 c_parser_error (parser, "expected identifier");
4109 break;
4111 label
4112 = declare_label (c_parser_peek_token (parser)->value);
4113 C_DECLARED_LABEL_FLAG (label) = 1;
4114 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4115 c_parser_consume_token (parser);
4116 if (c_parser_next_token_is (parser, CPP_COMMA))
4117 c_parser_consume_token (parser);
4118 else
4119 break;
4121 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4123 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
4125 /* We must now have at least one statement, label or declaration. */
4126 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4128 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4129 c_parser_error (parser, "expected declaration or statement");
4130 c_parser_consume_token (parser);
4131 return;
4133 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4135 location_t loc = c_parser_peek_token (parser)->location;
4136 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4137 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4138 || (c_parser_next_token_is (parser, CPP_NAME)
4139 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4141 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4142 label_loc = c_parser_peek_2nd_token (parser)->location;
4143 else
4144 label_loc = c_parser_peek_token (parser)->location;
4145 last_label = true;
4146 last_stmt = false;
4147 mark_valid_location_for_stdc_pragma (false);
4148 c_parser_label (parser);
4150 else if (!last_label
4151 && c_parser_next_tokens_start_declaration (parser))
4153 last_label = false;
4154 mark_valid_location_for_stdc_pragma (false);
4155 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4156 if (last_stmt)
4157 pedwarn_c90 (loc,
4158 (pedantic && !flag_isoc99)
4159 ? OPT_pedantic
4160 : OPT_Wdeclaration_after_statement,
4161 "ISO C90 forbids mixed declarations and code");
4162 last_stmt = false;
4164 else if (!last_label
4165 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4167 /* __extension__ can start a declaration, but is also an
4168 unary operator that can start an expression. Consume all
4169 but the last of a possible series of __extension__ to
4170 determine which. */
4171 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4172 && (c_parser_peek_2nd_token (parser)->keyword
4173 == RID_EXTENSION))
4174 c_parser_consume_token (parser);
4175 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4177 int ext;
4178 ext = disable_extension_diagnostics ();
4179 c_parser_consume_token (parser);
4180 last_label = false;
4181 mark_valid_location_for_stdc_pragma (false);
4182 c_parser_declaration_or_fndef (parser, true, true, true, true,
4183 true, NULL);
4184 /* Following the old parser, __extension__ does not
4185 disable this diagnostic. */
4186 restore_extension_diagnostics (ext);
4187 if (last_stmt)
4188 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4189 ? OPT_pedantic
4190 : OPT_Wdeclaration_after_statement,
4191 "ISO C90 forbids mixed declarations and code");
4192 last_stmt = false;
4194 else
4195 goto statement;
4197 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4199 /* External pragmas, and some omp pragmas, are not associated
4200 with regular c code, and so are not to be considered statements
4201 syntactically. This ensures that the user doesn't put them
4202 places that would turn into syntax errors if the directive
4203 were ignored. */
4204 if (c_parser_pragma (parser, pragma_compound))
4205 last_label = false, last_stmt = true;
4207 else if (c_parser_next_token_is (parser, CPP_EOF))
4209 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4210 c_parser_error (parser, "expected declaration or statement");
4211 return;
4213 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4215 if (parser->in_if_block)
4217 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4218 error_at (loc, """expected %<}%> before %<else%>");
4219 return;
4221 else
4223 error_at (loc, "%<else%> without a previous %<if%>");
4224 c_parser_consume_token (parser);
4225 continue;
4228 else
4230 statement:
4231 last_label = false;
4232 last_stmt = true;
4233 mark_valid_location_for_stdc_pragma (false);
4234 c_parser_statement_after_labels (parser);
4237 parser->error = false;
4239 if (last_label)
4240 error_at (label_loc, "label at end of compound statement");
4241 c_parser_consume_token (parser);
4242 /* Restore the value we started with. */
4243 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4246 /* Parse a label (C90 6.6.1, C99 6.8.1).
4248 label:
4249 identifier : attributes[opt]
4250 case constant-expression :
4251 default :
4253 GNU extensions:
4255 label:
4256 case constant-expression ... constant-expression :
4258 The use of attributes on labels is a GNU extension. The syntax in
4259 GNU C accepts any expressions without commas, non-constant
4260 expressions being rejected later. */
4262 static void
4263 c_parser_label (c_parser *parser)
4265 location_t loc1 = c_parser_peek_token (parser)->location;
4266 tree label = NULL_TREE;
4267 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4269 tree exp1, exp2;
4270 c_parser_consume_token (parser);
4271 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4272 if (c_parser_next_token_is (parser, CPP_COLON))
4274 c_parser_consume_token (parser);
4275 label = do_case (loc1, exp1, NULL_TREE);
4277 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4279 c_parser_consume_token (parser);
4280 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4281 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4282 label = do_case (loc1, exp1, exp2);
4284 else
4285 c_parser_error (parser, "expected %<:%> or %<...%>");
4287 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4289 c_parser_consume_token (parser);
4290 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4291 label = do_case (loc1, NULL_TREE, NULL_TREE);
4293 else
4295 tree name = c_parser_peek_token (parser)->value;
4296 tree tlab;
4297 tree attrs;
4298 location_t loc2 = c_parser_peek_token (parser)->location;
4299 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4300 c_parser_consume_token (parser);
4301 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4302 c_parser_consume_token (parser);
4303 attrs = c_parser_attributes (parser);
4304 tlab = define_label (loc2, name);
4305 if (tlab)
4307 decl_attributes (&tlab, attrs, 0);
4308 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4311 if (label)
4313 if (c_parser_next_tokens_start_declaration (parser))
4315 error_at (c_parser_peek_token (parser)->location,
4316 "a label can only be part of a statement and "
4317 "a declaration is not a statement");
4318 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4319 /*static_assert_ok*/ true,
4320 /*nested*/ true, /*empty_ok*/ false,
4321 /*start_attr_ok*/ true, NULL);
4326 /* Parse a statement (C90 6.6, C99 6.8).
4328 statement:
4329 labeled-statement
4330 compound-statement
4331 expression-statement
4332 selection-statement
4333 iteration-statement
4334 jump-statement
4336 labeled-statement:
4337 label statement
4339 expression-statement:
4340 expression[opt] ;
4342 selection-statement:
4343 if-statement
4344 switch-statement
4346 iteration-statement:
4347 while-statement
4348 do-statement
4349 for-statement
4351 jump-statement:
4352 goto identifier ;
4353 continue ;
4354 break ;
4355 return expression[opt] ;
4357 GNU extensions:
4359 statement:
4360 asm-statement
4362 jump-statement:
4363 goto * expression ;
4365 Objective-C:
4367 statement:
4368 objc-throw-statement
4369 objc-try-catch-statement
4370 objc-synchronized-statement
4372 objc-throw-statement:
4373 @throw expression ;
4374 @throw ;
4376 OpenMP:
4378 statement:
4379 openmp-construct
4381 openmp-construct:
4382 parallel-construct
4383 for-construct
4384 sections-construct
4385 single-construct
4386 parallel-for-construct
4387 parallel-sections-construct
4388 master-construct
4389 critical-construct
4390 atomic-construct
4391 ordered-construct
4393 parallel-construct:
4394 parallel-directive structured-block
4396 for-construct:
4397 for-directive iteration-statement
4399 sections-construct:
4400 sections-directive section-scope
4402 single-construct:
4403 single-directive structured-block
4405 parallel-for-construct:
4406 parallel-for-directive iteration-statement
4408 parallel-sections-construct:
4409 parallel-sections-directive section-scope
4411 master-construct:
4412 master-directive structured-block
4414 critical-construct:
4415 critical-directive structured-block
4417 atomic-construct:
4418 atomic-directive expression-statement
4420 ordered-construct:
4421 ordered-directive structured-block
4423 Transactional Memory:
4425 statement:
4426 transaction-statement
4427 transaction-cancel-statement
4430 static void
4431 c_parser_statement (c_parser *parser)
4433 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4434 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4435 || (c_parser_next_token_is (parser, CPP_NAME)
4436 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4437 c_parser_label (parser);
4438 c_parser_statement_after_labels (parser);
4441 /* Parse a statement, other than a labeled statement. */
4443 static void
4444 c_parser_statement_after_labels (c_parser *parser)
4446 location_t loc = c_parser_peek_token (parser)->location;
4447 tree stmt = NULL_TREE;
4448 bool in_if_block = parser->in_if_block;
4449 parser->in_if_block = false;
4450 switch (c_parser_peek_token (parser)->type)
4452 case CPP_OPEN_BRACE:
4453 add_stmt (c_parser_compound_statement (parser));
4454 break;
4455 case CPP_KEYWORD:
4456 switch (c_parser_peek_token (parser)->keyword)
4458 case RID_IF:
4459 c_parser_if_statement (parser);
4460 break;
4461 case RID_SWITCH:
4462 c_parser_switch_statement (parser);
4463 break;
4464 case RID_WHILE:
4465 c_parser_while_statement (parser);
4466 break;
4467 case RID_DO:
4468 c_parser_do_statement (parser);
4469 break;
4470 case RID_FOR:
4471 c_parser_for_statement (parser);
4472 break;
4473 case RID_GOTO:
4474 c_parser_consume_token (parser);
4475 if (c_parser_next_token_is (parser, CPP_NAME))
4477 stmt = c_finish_goto_label (loc,
4478 c_parser_peek_token (parser)->value);
4479 c_parser_consume_token (parser);
4481 else if (c_parser_next_token_is (parser, CPP_MULT))
4483 tree val;
4485 c_parser_consume_token (parser);
4486 val = c_parser_expression (parser).value;
4487 mark_exp_read (val);
4488 stmt = c_finish_goto_ptr (loc, val);
4490 else
4491 c_parser_error (parser, "expected identifier or %<*%>");
4492 goto expect_semicolon;
4493 case RID_CONTINUE:
4494 c_parser_consume_token (parser);
4495 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4496 goto expect_semicolon;
4497 case RID_BREAK:
4498 c_parser_consume_token (parser);
4499 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4500 goto expect_semicolon;
4501 case RID_RETURN:
4502 c_parser_consume_token (parser);
4503 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4505 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4506 c_parser_consume_token (parser);
4508 else
4510 struct c_expr expr = c_parser_expression_conv (parser);
4511 mark_exp_read (expr.value);
4512 stmt = c_finish_return (loc, expr.value, expr.original_type);
4513 goto expect_semicolon;
4515 break;
4516 case RID_ASM:
4517 stmt = c_parser_asm_statement (parser);
4518 break;
4519 case RID_TRANSACTION_ATOMIC:
4520 case RID_TRANSACTION_RELAXED:
4521 stmt = c_parser_transaction (parser,
4522 c_parser_peek_token (parser)->keyword);
4523 break;
4524 case RID_TRANSACTION_CANCEL:
4525 stmt = c_parser_transaction_cancel (parser);
4526 goto expect_semicolon;
4527 case RID_AT_THROW:
4528 gcc_assert (c_dialect_objc ());
4529 c_parser_consume_token (parser);
4530 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4532 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4533 c_parser_consume_token (parser);
4535 else
4537 tree expr = c_parser_expression (parser).value;
4538 expr = c_fully_fold (expr, false, NULL);
4539 stmt = objc_build_throw_stmt (loc, expr);
4540 goto expect_semicolon;
4542 break;
4543 case RID_AT_TRY:
4544 gcc_assert (c_dialect_objc ());
4545 c_parser_objc_try_catch_finally_statement (parser);
4546 break;
4547 case RID_AT_SYNCHRONIZED:
4548 gcc_assert (c_dialect_objc ());
4549 c_parser_objc_synchronized_statement (parser);
4550 break;
4551 default:
4552 goto expr_stmt;
4554 break;
4555 case CPP_SEMICOLON:
4556 c_parser_consume_token (parser);
4557 break;
4558 case CPP_CLOSE_PAREN:
4559 case CPP_CLOSE_SQUARE:
4560 /* Avoid infinite loop in error recovery:
4561 c_parser_skip_until_found stops at a closing nesting
4562 delimiter without consuming it, but here we need to consume
4563 it to proceed further. */
4564 c_parser_error (parser, "expected statement");
4565 c_parser_consume_token (parser);
4566 break;
4567 case CPP_PRAGMA:
4568 c_parser_pragma (parser, pragma_stmt);
4569 break;
4570 default:
4571 expr_stmt:
4572 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4573 expect_semicolon:
4574 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4575 break;
4577 /* Two cases cannot and do not have line numbers associated: If stmt
4578 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4579 cannot hold line numbers. But that's OK because the statement
4580 will either be changed to a MODIFY_EXPR during gimplification of
4581 the statement expr, or discarded. If stmt was compound, but
4582 without new variables, we will have skipped the creation of a
4583 BIND and will have a bare STATEMENT_LIST. But that's OK because
4584 (recursively) all of the component statements should already have
4585 line numbers assigned. ??? Can we discard no-op statements
4586 earlier? */
4587 if (CAN_HAVE_LOCATION_P (stmt)
4588 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4589 SET_EXPR_LOCATION (stmt, loc);
4591 parser->in_if_block = in_if_block;
4594 /* Parse the condition from an if, do, while or for statements. */
4596 static tree
4597 c_parser_condition (c_parser *parser)
4599 location_t loc = c_parser_peek_token (parser)->location;
4600 tree cond;
4601 cond = c_parser_expression_conv (parser).value;
4602 cond = c_objc_common_truthvalue_conversion (loc, cond);
4603 cond = c_fully_fold (cond, false, NULL);
4604 if (warn_sequence_point)
4605 verify_sequence_points (cond);
4606 return cond;
4609 /* Parse a parenthesized condition from an if, do or while statement.
4611 condition:
4612 ( expression )
4614 static tree
4615 c_parser_paren_condition (c_parser *parser)
4617 tree cond;
4618 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4619 return error_mark_node;
4620 cond = c_parser_condition (parser);
4621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4622 return cond;
4625 /* Parse a statement which is a block in C99. */
4627 static tree
4628 c_parser_c99_block_statement (c_parser *parser)
4630 tree block = c_begin_compound_stmt (flag_isoc99);
4631 location_t loc = c_parser_peek_token (parser)->location;
4632 c_parser_statement (parser);
4633 return c_end_compound_stmt (loc, block, flag_isoc99);
4636 /* Parse the body of an if statement. This is just parsing a
4637 statement but (a) it is a block in C99, (b) we track whether the
4638 body is an if statement for the sake of -Wparentheses warnings, (c)
4639 we handle an empty body specially for the sake of -Wempty-body
4640 warnings, and (d) we call parser_compound_statement directly
4641 because c_parser_statement_after_labels resets
4642 parser->in_if_block. */
4644 static tree
4645 c_parser_if_body (c_parser *parser, bool *if_p)
4647 tree block = c_begin_compound_stmt (flag_isoc99);
4648 location_t body_loc = c_parser_peek_token (parser)->location;
4649 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4650 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4651 || (c_parser_next_token_is (parser, CPP_NAME)
4652 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4653 c_parser_label (parser);
4654 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4655 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4657 location_t loc = c_parser_peek_token (parser)->location;
4658 add_stmt (build_empty_stmt (loc));
4659 c_parser_consume_token (parser);
4660 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4661 warning_at (loc, OPT_Wempty_body,
4662 "suggest braces around empty body in an %<if%> statement");
4664 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4665 add_stmt (c_parser_compound_statement (parser));
4666 else
4667 c_parser_statement_after_labels (parser);
4668 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4671 /* Parse the else body of an if statement. This is just parsing a
4672 statement but (a) it is a block in C99, (b) we handle an empty body
4673 specially for the sake of -Wempty-body warnings. */
4675 static tree
4676 c_parser_else_body (c_parser *parser)
4678 location_t else_loc = c_parser_peek_token (parser)->location;
4679 tree block = c_begin_compound_stmt (flag_isoc99);
4680 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4681 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4682 || (c_parser_next_token_is (parser, CPP_NAME)
4683 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4684 c_parser_label (parser);
4685 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4687 location_t loc = c_parser_peek_token (parser)->location;
4688 warning_at (loc,
4689 OPT_Wempty_body,
4690 "suggest braces around empty body in an %<else%> statement");
4691 add_stmt (build_empty_stmt (loc));
4692 c_parser_consume_token (parser);
4694 else
4695 c_parser_statement_after_labels (parser);
4696 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4699 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4701 if-statement:
4702 if ( expression ) statement
4703 if ( expression ) statement else statement
4706 static void
4707 c_parser_if_statement (c_parser *parser)
4709 tree block;
4710 location_t loc;
4711 tree cond;
4712 bool first_if = false;
4713 tree first_body, second_body;
4714 bool in_if_block;
4716 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4717 c_parser_consume_token (parser);
4718 block = c_begin_compound_stmt (flag_isoc99);
4719 loc = c_parser_peek_token (parser)->location;
4720 cond = c_parser_paren_condition (parser);
4721 in_if_block = parser->in_if_block;
4722 parser->in_if_block = true;
4723 first_body = c_parser_if_body (parser, &first_if);
4724 parser->in_if_block = in_if_block;
4725 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4727 c_parser_consume_token (parser);
4728 second_body = c_parser_else_body (parser);
4730 else
4731 second_body = NULL_TREE;
4732 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4733 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4736 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4738 switch-statement:
4739 switch (expression) statement
4742 static void
4743 c_parser_switch_statement (c_parser *parser)
4745 tree block, expr, body, save_break;
4746 location_t switch_loc = c_parser_peek_token (parser)->location;
4747 location_t switch_cond_loc;
4748 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4749 c_parser_consume_token (parser);
4750 block = c_begin_compound_stmt (flag_isoc99);
4751 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4753 switch_cond_loc = c_parser_peek_token (parser)->location;
4754 expr = c_parser_expression (parser).value;
4755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4757 else
4759 switch_cond_loc = UNKNOWN_LOCATION;
4760 expr = error_mark_node;
4762 c_start_case (switch_loc, switch_cond_loc, expr);
4763 save_break = c_break_label;
4764 c_break_label = NULL_TREE;
4765 body = c_parser_c99_block_statement (parser);
4766 c_finish_case (body);
4767 if (c_break_label)
4769 location_t here = c_parser_peek_token (parser)->location;
4770 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4771 SET_EXPR_LOCATION (t, here);
4772 add_stmt (t);
4774 c_break_label = save_break;
4775 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4778 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4780 while-statement:
4781 while (expression) statement
4784 static void
4785 c_parser_while_statement (c_parser *parser)
4787 tree block, cond, body, save_break, save_cont;
4788 location_t loc;
4789 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4790 c_parser_consume_token (parser);
4791 block = c_begin_compound_stmt (flag_isoc99);
4792 loc = c_parser_peek_token (parser)->location;
4793 cond = c_parser_paren_condition (parser);
4794 save_break = c_break_label;
4795 c_break_label = NULL_TREE;
4796 save_cont = c_cont_label;
4797 c_cont_label = NULL_TREE;
4798 body = c_parser_c99_block_statement (parser);
4799 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4800 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4801 c_break_label = save_break;
4802 c_cont_label = save_cont;
4805 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4807 do-statement:
4808 do statement while ( expression ) ;
4811 static void
4812 c_parser_do_statement (c_parser *parser)
4814 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4815 location_t loc;
4816 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4817 c_parser_consume_token (parser);
4818 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4819 warning_at (c_parser_peek_token (parser)->location,
4820 OPT_Wempty_body,
4821 "suggest braces around empty body in %<do%> statement");
4822 block = c_begin_compound_stmt (flag_isoc99);
4823 loc = c_parser_peek_token (parser)->location;
4824 save_break = c_break_label;
4825 c_break_label = NULL_TREE;
4826 save_cont = c_cont_label;
4827 c_cont_label = NULL_TREE;
4828 body = c_parser_c99_block_statement (parser);
4829 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4830 new_break = c_break_label;
4831 c_break_label = save_break;
4832 new_cont = c_cont_label;
4833 c_cont_label = save_cont;
4834 cond = c_parser_paren_condition (parser);
4835 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4836 c_parser_skip_to_end_of_block_or_statement (parser);
4837 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4838 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4841 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4843 for-statement:
4844 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4845 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4847 The form with a declaration is new in C99.
4849 ??? In accordance with the old parser, the declaration may be a
4850 nested function, which is then rejected in check_for_loop_decls,
4851 but does it make any sense for this to be included in the grammar?
4852 Note in particular that the nested function does not include a
4853 trailing ';', whereas the "declaration" production includes one.
4854 Also, can we reject bad declarations earlier and cheaper than
4855 check_for_loop_decls?
4857 In Objective-C, there are two additional variants:
4859 foreach-statement:
4860 for ( expression in expresssion ) statement
4861 for ( declaration in expression ) statement
4863 This is inconsistent with C, because the second variant is allowed
4864 even if c99 is not enabled.
4866 The rest of the comment documents these Objective-C foreach-statement.
4868 Here is the canonical example of the first variant:
4869 for (object in array) { do something with object }
4870 we call the first expression ("object") the "object_expression" and
4871 the second expression ("array") the "collection_expression".
4872 object_expression must be an lvalue of type "id" (a generic Objective-C
4873 object) because the loop works by assigning to object_expression the
4874 various objects from the collection_expression. collection_expression
4875 must evaluate to something of type "id" which responds to the method
4876 countByEnumeratingWithState:objects:count:.
4878 The canonical example of the second variant is:
4879 for (id object in array) { do something with object }
4880 which is completely equivalent to
4882 id object;
4883 for (object in array) { do something with object }
4885 Note that initizializing 'object' in some way (eg, "for ((object =
4886 xxx) in array) { do something with object }") is possibly
4887 technically valid, but completely pointless as 'object' will be
4888 assigned to something else as soon as the loop starts. We should
4889 most likely reject it (TODO).
4891 The beginning of the Objective-C foreach-statement looks exactly
4892 like the beginning of the for-statement, and we can tell it is a
4893 foreach-statement only because the initial declaration or
4894 expression is terminated by 'in' instead of ';'.
4897 static void
4898 c_parser_for_statement (c_parser *parser)
4900 tree block, cond, incr, save_break, save_cont, body;
4901 /* The following are only used when parsing an ObjC foreach statement. */
4902 tree object_expression;
4903 /* Silence the bogus uninitialized warning. */
4904 tree collection_expression = NULL;
4905 location_t loc = c_parser_peek_token (parser)->location;
4906 location_t for_loc = c_parser_peek_token (parser)->location;
4907 bool is_foreach_statement = false;
4908 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4909 c_parser_consume_token (parser);
4910 /* Open a compound statement in Objective-C as well, just in case this is
4911 as foreach expression. */
4912 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4913 cond = error_mark_node;
4914 incr = error_mark_node;
4915 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4917 /* Parse the initialization declaration or expression. */
4918 object_expression = error_mark_node;
4919 parser->objc_could_be_foreach_context = c_dialect_objc ();
4920 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4922 parser->objc_could_be_foreach_context = false;
4923 c_parser_consume_token (parser);
4924 c_finish_expr_stmt (loc, NULL_TREE);
4926 else if (c_parser_next_tokens_start_declaration (parser))
4928 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4929 &object_expression);
4930 parser->objc_could_be_foreach_context = false;
4932 if (c_parser_next_token_is_keyword (parser, RID_IN))
4934 c_parser_consume_token (parser);
4935 is_foreach_statement = true;
4936 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4937 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4939 else
4940 check_for_loop_decls (for_loc, flag_isoc99);
4942 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4944 /* __extension__ can start a declaration, but is also an
4945 unary operator that can start an expression. Consume all
4946 but the last of a possible series of __extension__ to
4947 determine which. */
4948 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4949 && (c_parser_peek_2nd_token (parser)->keyword
4950 == RID_EXTENSION))
4951 c_parser_consume_token (parser);
4952 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4954 int ext;
4955 ext = disable_extension_diagnostics ();
4956 c_parser_consume_token (parser);
4957 c_parser_declaration_or_fndef (parser, true, true, true, true,
4958 true, &object_expression);
4959 parser->objc_could_be_foreach_context = false;
4961 restore_extension_diagnostics (ext);
4962 if (c_parser_next_token_is_keyword (parser, RID_IN))
4964 c_parser_consume_token (parser);
4965 is_foreach_statement = true;
4966 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4967 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4969 else
4970 check_for_loop_decls (for_loc, flag_isoc99);
4972 else
4973 goto init_expr;
4975 else
4977 init_expr:
4979 tree init_expression;
4980 init_expression = c_parser_expression (parser).value;
4981 parser->objc_could_be_foreach_context = false;
4982 if (c_parser_next_token_is_keyword (parser, RID_IN))
4984 c_parser_consume_token (parser);
4985 is_foreach_statement = true;
4986 if (! lvalue_p (init_expression))
4987 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4988 object_expression = c_fully_fold (init_expression, false, NULL);
4990 else
4992 c_finish_expr_stmt (loc, init_expression);
4993 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4997 /* Parse the loop condition. In the case of a foreach
4998 statement, there is no loop condition. */
4999 gcc_assert (!parser->objc_could_be_foreach_context);
5000 if (!is_foreach_statement)
5002 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5004 c_parser_consume_token (parser);
5005 cond = NULL_TREE;
5007 else
5009 cond = c_parser_condition (parser);
5010 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5013 /* Parse the increment expression (the third expression in a
5014 for-statement). In the case of a foreach-statement, this is
5015 the expression that follows the 'in'. */
5016 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5018 if (is_foreach_statement)
5020 c_parser_error (parser, "missing collection in fast enumeration");
5021 collection_expression = error_mark_node;
5023 else
5024 incr = c_process_expr_stmt (loc, NULL_TREE);
5026 else
5028 if (is_foreach_statement)
5029 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5030 false, NULL);
5031 else
5032 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5036 save_break = c_break_label;
5037 c_break_label = NULL_TREE;
5038 save_cont = c_cont_label;
5039 c_cont_label = NULL_TREE;
5040 body = c_parser_c99_block_statement (parser);
5041 if (is_foreach_statement)
5042 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5043 else
5044 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5045 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5046 c_break_label = save_break;
5047 c_cont_label = save_cont;
5050 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5051 statement with inputs, outputs, clobbers, and volatile tag
5052 allowed.
5054 asm-statement:
5055 asm type-qualifier[opt] ( asm-argument ) ;
5056 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5058 asm-argument:
5059 asm-string-literal
5060 asm-string-literal : asm-operands[opt]
5061 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5062 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5064 asm-goto-argument:
5065 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5066 : asm-goto-operands
5068 Qualifiers other than volatile are accepted in the syntax but
5069 warned for. */
5071 static tree
5072 c_parser_asm_statement (c_parser *parser)
5074 tree quals, str, outputs, inputs, clobbers, labels, ret;
5075 bool simple, is_goto;
5076 location_t asm_loc = c_parser_peek_token (parser)->location;
5077 int section, nsections;
5079 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5080 c_parser_consume_token (parser);
5081 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5083 quals = c_parser_peek_token (parser)->value;
5084 c_parser_consume_token (parser);
5086 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5087 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5089 warning_at (c_parser_peek_token (parser)->location,
5091 "%E qualifier ignored on asm",
5092 c_parser_peek_token (parser)->value);
5093 quals = NULL_TREE;
5094 c_parser_consume_token (parser);
5096 else
5097 quals = NULL_TREE;
5099 is_goto = false;
5100 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5102 c_parser_consume_token (parser);
5103 is_goto = true;
5106 /* ??? Follow the C++ parser rather than using the
5107 lex_untranslated_string kludge. */
5108 parser->lex_untranslated_string = true;
5109 ret = NULL;
5111 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5112 goto error;
5114 str = c_parser_asm_string_literal (parser);
5115 if (str == NULL_TREE)
5116 goto error_close_paren;
5118 simple = true;
5119 outputs = NULL_TREE;
5120 inputs = NULL_TREE;
5121 clobbers = NULL_TREE;
5122 labels = NULL_TREE;
5124 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5125 goto done_asm;
5127 /* Parse each colon-delimited section of operands. */
5128 nsections = 3 + is_goto;
5129 for (section = 0; section < nsections; ++section)
5131 if (!c_parser_require (parser, CPP_COLON,
5132 is_goto
5133 ? "expected %<:%>"
5134 : "expected %<:%> or %<)%>"))
5135 goto error_close_paren;
5137 /* Once past any colon, we're no longer a simple asm. */
5138 simple = false;
5140 if ((!c_parser_next_token_is (parser, CPP_COLON)
5141 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5142 || section == 3)
5143 switch (section)
5145 case 0:
5146 /* For asm goto, we don't allow output operands, but reserve
5147 the slot for a future extension that does allow them. */
5148 if (!is_goto)
5149 outputs = c_parser_asm_operands (parser, false);
5150 break;
5151 case 1:
5152 inputs = c_parser_asm_operands (parser, true);
5153 break;
5154 case 2:
5155 clobbers = c_parser_asm_clobbers (parser);
5156 break;
5157 case 3:
5158 labels = c_parser_asm_goto_operands (parser);
5159 break;
5160 default:
5161 gcc_unreachable ();
5164 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5165 goto done_asm;
5168 done_asm:
5169 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5171 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5172 goto error;
5175 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5176 c_parser_skip_to_end_of_block_or_statement (parser);
5178 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5179 clobbers, labels, simple));
5181 error:
5182 parser->lex_untranslated_string = false;
5183 return ret;
5185 error_close_paren:
5186 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5187 goto error;
5190 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5191 not outputs), apply the default conversion of functions and arrays
5192 to pointers.
5194 asm-operands:
5195 asm-operand
5196 asm-operands , asm-operand
5198 asm-operand:
5199 asm-string-literal ( expression )
5200 [ identifier ] asm-string-literal ( expression )
5203 static tree
5204 c_parser_asm_operands (c_parser *parser, bool convert_p)
5206 tree list = NULL_TREE;
5207 location_t loc;
5208 while (true)
5210 tree name, str;
5211 struct c_expr expr;
5212 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5214 c_parser_consume_token (parser);
5215 if (c_parser_next_token_is (parser, CPP_NAME))
5217 tree id = c_parser_peek_token (parser)->value;
5218 c_parser_consume_token (parser);
5219 name = build_string (IDENTIFIER_LENGTH (id),
5220 IDENTIFIER_POINTER (id));
5222 else
5224 c_parser_error (parser, "expected identifier");
5225 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5226 return NULL_TREE;
5228 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5229 "expected %<]%>");
5231 else
5232 name = NULL_TREE;
5233 str = c_parser_asm_string_literal (parser);
5234 if (str == NULL_TREE)
5235 return NULL_TREE;
5236 parser->lex_untranslated_string = false;
5237 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5239 parser->lex_untranslated_string = true;
5240 return NULL_TREE;
5242 loc = c_parser_peek_token (parser)->location;
5243 expr = c_parser_expression (parser);
5244 mark_exp_read (expr.value);
5245 if (convert_p)
5246 expr = default_function_array_conversion (loc, expr);
5247 expr.value = c_fully_fold (expr.value, false, NULL);
5248 parser->lex_untranslated_string = true;
5249 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5252 return NULL_TREE;
5254 list = chainon (list, build_tree_list (build_tree_list (name, str),
5255 expr.value));
5256 if (c_parser_next_token_is (parser, CPP_COMMA))
5257 c_parser_consume_token (parser);
5258 else
5259 break;
5261 return list;
5264 /* Parse asm clobbers, a GNU extension.
5266 asm-clobbers:
5267 asm-string-literal
5268 asm-clobbers , asm-string-literal
5271 static tree
5272 c_parser_asm_clobbers (c_parser *parser)
5274 tree list = NULL_TREE;
5275 while (true)
5277 tree str = c_parser_asm_string_literal (parser);
5278 if (str)
5279 list = tree_cons (NULL_TREE, str, list);
5280 else
5281 return NULL_TREE;
5282 if (c_parser_next_token_is (parser, CPP_COMMA))
5283 c_parser_consume_token (parser);
5284 else
5285 break;
5287 return list;
5290 /* Parse asm goto labels, a GNU extension.
5292 asm-goto-operands:
5293 identifier
5294 asm-goto-operands , identifier
5297 static tree
5298 c_parser_asm_goto_operands (c_parser *parser)
5300 tree list = NULL_TREE;
5301 while (true)
5303 tree name, label;
5305 if (c_parser_next_token_is (parser, CPP_NAME))
5307 c_token *tok = c_parser_peek_token (parser);
5308 name = tok->value;
5309 label = lookup_label_for_goto (tok->location, name);
5310 c_parser_consume_token (parser);
5311 TREE_USED (label) = 1;
5313 else
5315 c_parser_error (parser, "expected identifier");
5316 return NULL_TREE;
5319 name = build_string (IDENTIFIER_LENGTH (name),
5320 IDENTIFIER_POINTER (name));
5321 list = tree_cons (name, label, list);
5322 if (c_parser_next_token_is (parser, CPP_COMMA))
5323 c_parser_consume_token (parser);
5324 else
5325 return nreverse (list);
5329 /* Parse an expression other than a compound expression; that is, an
5330 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5331 NULL then it is an Objective-C message expression which is the
5332 primary-expression starting the expression as an initializer.
5334 assignment-expression:
5335 conditional-expression
5336 unary-expression assignment-operator assignment-expression
5338 assignment-operator: one of
5339 = *= /= %= += -= <<= >>= &= ^= |=
5341 In GNU C we accept any conditional expression on the LHS and
5342 diagnose the invalid lvalue rather than producing a syntax
5343 error. */
5345 static struct c_expr
5346 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5348 struct c_expr lhs, rhs, ret;
5349 enum tree_code code;
5350 location_t op_location, exp_location;
5351 gcc_assert (!after || c_dialect_objc ());
5352 lhs = c_parser_conditional_expression (parser, after);
5353 op_location = c_parser_peek_token (parser)->location;
5354 switch (c_parser_peek_token (parser)->type)
5356 case CPP_EQ:
5357 code = NOP_EXPR;
5358 break;
5359 case CPP_MULT_EQ:
5360 code = MULT_EXPR;
5361 break;
5362 case CPP_DIV_EQ:
5363 code = TRUNC_DIV_EXPR;
5364 break;
5365 case CPP_MOD_EQ:
5366 code = TRUNC_MOD_EXPR;
5367 break;
5368 case CPP_PLUS_EQ:
5369 code = PLUS_EXPR;
5370 break;
5371 case CPP_MINUS_EQ:
5372 code = MINUS_EXPR;
5373 break;
5374 case CPP_LSHIFT_EQ:
5375 code = LSHIFT_EXPR;
5376 break;
5377 case CPP_RSHIFT_EQ:
5378 code = RSHIFT_EXPR;
5379 break;
5380 case CPP_AND_EQ:
5381 code = BIT_AND_EXPR;
5382 break;
5383 case CPP_XOR_EQ:
5384 code = BIT_XOR_EXPR;
5385 break;
5386 case CPP_OR_EQ:
5387 code = BIT_IOR_EXPR;
5388 break;
5389 default:
5390 return lhs;
5392 c_parser_consume_token (parser);
5393 exp_location = c_parser_peek_token (parser)->location;
5394 rhs = c_parser_expr_no_commas (parser, NULL);
5395 rhs = default_function_array_read_conversion (exp_location, rhs);
5396 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5397 code, exp_location, rhs.value,
5398 rhs.original_type);
5399 if (code == NOP_EXPR)
5400 ret.original_code = MODIFY_EXPR;
5401 else
5403 TREE_NO_WARNING (ret.value) = 1;
5404 ret.original_code = ERROR_MARK;
5406 ret.original_type = NULL;
5407 return ret;
5410 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5411 is not NULL then it is an Objective-C message expression which is
5412 the primary-expression starting the expression as an initializer.
5414 conditional-expression:
5415 logical-OR-expression
5416 logical-OR-expression ? expression : conditional-expression
5418 GNU extensions:
5420 conditional-expression:
5421 logical-OR-expression ? : conditional-expression
5424 static struct c_expr
5425 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5427 struct c_expr cond, exp1, exp2, ret;
5428 location_t cond_loc, colon_loc, middle_loc;
5430 gcc_assert (!after || c_dialect_objc ());
5432 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5434 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5435 return cond;
5436 cond_loc = c_parser_peek_token (parser)->location;
5437 cond = default_function_array_read_conversion (cond_loc, cond);
5438 c_parser_consume_token (parser);
5439 if (c_parser_next_token_is (parser, CPP_COLON))
5441 tree eptype = NULL_TREE;
5443 middle_loc = c_parser_peek_token (parser)->location;
5444 pedwarn (middle_loc, OPT_pedantic,
5445 "ISO C forbids omitting the middle term of a ?: expression");
5446 warn_for_omitted_condop (middle_loc, cond.value);
5447 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5449 eptype = TREE_TYPE (cond.value);
5450 cond.value = TREE_OPERAND (cond.value, 0);
5452 /* Make sure first operand is calculated only once. */
5453 exp1.value = c_save_expr (default_conversion (cond.value));
5454 if (eptype)
5455 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5456 exp1.original_type = NULL;
5457 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5458 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5460 else
5462 cond.value
5463 = c_objc_common_truthvalue_conversion
5464 (cond_loc, default_conversion (cond.value));
5465 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5466 exp1 = c_parser_expression_conv (parser);
5467 mark_exp_read (exp1.value);
5468 c_inhibit_evaluation_warnings +=
5469 ((cond.value == truthvalue_true_node)
5470 - (cond.value == truthvalue_false_node));
5473 colon_loc = c_parser_peek_token (parser)->location;
5474 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5476 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5477 ret.value = error_mark_node;
5478 ret.original_code = ERROR_MARK;
5479 ret.original_type = NULL;
5480 return ret;
5483 location_t exp2_loc = c_parser_peek_token (parser)->location;
5484 exp2 = c_parser_conditional_expression (parser, NULL);
5485 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5487 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5488 ret.value = build_conditional_expr (colon_loc, cond.value,
5489 cond.original_code == C_MAYBE_CONST_EXPR,
5490 exp1.value, exp1.original_type,
5491 exp2.value, exp2.original_type);
5492 ret.original_code = ERROR_MARK;
5493 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5494 ret.original_type = NULL;
5495 else
5497 tree t1, t2;
5499 /* If both sides are enum type, the default conversion will have
5500 made the type of the result be an integer type. We want to
5501 remember the enum types we started with. */
5502 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5503 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5504 ret.original_type = ((t1 != error_mark_node
5505 && t2 != error_mark_node
5506 && (TYPE_MAIN_VARIANT (t1)
5507 == TYPE_MAIN_VARIANT (t2)))
5508 ? t1
5509 : NULL);
5511 return ret;
5514 /* Parse a binary expression; that is, a logical-OR-expression (C90
5515 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5516 an Objective-C message expression which is the primary-expression
5517 starting the expression as an initializer. PREC is the starting
5518 precedence, usually PREC_NONE.
5520 multiplicative-expression:
5521 cast-expression
5522 multiplicative-expression * cast-expression
5523 multiplicative-expression / cast-expression
5524 multiplicative-expression % cast-expression
5526 additive-expression:
5527 multiplicative-expression
5528 additive-expression + multiplicative-expression
5529 additive-expression - multiplicative-expression
5531 shift-expression:
5532 additive-expression
5533 shift-expression << additive-expression
5534 shift-expression >> additive-expression
5536 relational-expression:
5537 shift-expression
5538 relational-expression < shift-expression
5539 relational-expression > shift-expression
5540 relational-expression <= shift-expression
5541 relational-expression >= shift-expression
5543 equality-expression:
5544 relational-expression
5545 equality-expression == relational-expression
5546 equality-expression != relational-expression
5548 AND-expression:
5549 equality-expression
5550 AND-expression & equality-expression
5552 exclusive-OR-expression:
5553 AND-expression
5554 exclusive-OR-expression ^ AND-expression
5556 inclusive-OR-expression:
5557 exclusive-OR-expression
5558 inclusive-OR-expression | exclusive-OR-expression
5560 logical-AND-expression:
5561 inclusive-OR-expression
5562 logical-AND-expression && inclusive-OR-expression
5564 logical-OR-expression:
5565 logical-AND-expression
5566 logical-OR-expression || logical-AND-expression
5569 static struct c_expr
5570 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5571 enum c_parser_prec prec)
5573 /* A binary expression is parsed using operator-precedence parsing,
5574 with the operands being cast expressions. All the binary
5575 operators are left-associative. Thus a binary expression is of
5576 form:
5578 E0 op1 E1 op2 E2 ...
5580 which we represent on a stack. On the stack, the precedence
5581 levels are strictly increasing. When a new operator is
5582 encountered of higher precedence than that at the top of the
5583 stack, it is pushed; its LHS is the top expression, and its RHS
5584 is everything parsed until it is popped. When a new operator is
5585 encountered with precedence less than or equal to that at the top
5586 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5587 by the result of the operation until the operator at the top of
5588 the stack has lower precedence than the new operator or there is
5589 only one element on the stack; then the top expression is the LHS
5590 of the new operator. In the case of logical AND and OR
5591 expressions, we also need to adjust c_inhibit_evaluation_warnings
5592 as appropriate when the operators are pushed and popped. */
5594 struct {
5595 /* The expression at this stack level. */
5596 struct c_expr expr;
5597 /* The precedence of the operator on its left, PREC_NONE at the
5598 bottom of the stack. */
5599 enum c_parser_prec prec;
5600 /* The operation on its left. */
5601 enum tree_code op;
5602 /* The source location of this operation. */
5603 location_t loc;
5604 } stack[NUM_PRECS];
5605 int sp;
5606 /* Location of the binary operator. */
5607 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5608 #define POP \
5609 do { \
5610 switch (stack[sp].op) \
5612 case TRUTH_ANDIF_EXPR: \
5613 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5614 == truthvalue_false_node); \
5615 break; \
5616 case TRUTH_ORIF_EXPR: \
5617 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5618 == truthvalue_true_node); \
5619 break; \
5620 default: \
5621 break; \
5623 stack[sp - 1].expr \
5624 = default_function_array_read_conversion (stack[sp - 1].loc, \
5625 stack[sp - 1].expr); \
5626 stack[sp].expr \
5627 = default_function_array_read_conversion (stack[sp].loc, \
5628 stack[sp].expr); \
5629 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5630 stack[sp].op, \
5631 stack[sp - 1].expr, \
5632 stack[sp].expr); \
5633 sp--; \
5634 } while (0)
5635 gcc_assert (!after || c_dialect_objc ());
5636 stack[0].loc = c_parser_peek_token (parser)->location;
5637 stack[0].expr = c_parser_cast_expression (parser, after);
5638 stack[0].prec = prec;
5639 sp = 0;
5640 while (true)
5642 enum c_parser_prec oprec;
5643 enum tree_code ocode;
5644 if (parser->error)
5645 goto out;
5646 switch (c_parser_peek_token (parser)->type)
5648 case CPP_MULT:
5649 oprec = PREC_MULT;
5650 ocode = MULT_EXPR;
5651 break;
5652 case CPP_DIV:
5653 oprec = PREC_MULT;
5654 ocode = TRUNC_DIV_EXPR;
5655 break;
5656 case CPP_MOD:
5657 oprec = PREC_MULT;
5658 ocode = TRUNC_MOD_EXPR;
5659 break;
5660 case CPP_PLUS:
5661 oprec = PREC_ADD;
5662 ocode = PLUS_EXPR;
5663 break;
5664 case CPP_MINUS:
5665 oprec = PREC_ADD;
5666 ocode = MINUS_EXPR;
5667 break;
5668 case CPP_LSHIFT:
5669 oprec = PREC_SHIFT;
5670 ocode = LSHIFT_EXPR;
5671 break;
5672 case CPP_RSHIFT:
5673 oprec = PREC_SHIFT;
5674 ocode = RSHIFT_EXPR;
5675 break;
5676 case CPP_LESS:
5677 oprec = PREC_REL;
5678 ocode = LT_EXPR;
5679 break;
5680 case CPP_GREATER:
5681 oprec = PREC_REL;
5682 ocode = GT_EXPR;
5683 break;
5684 case CPP_LESS_EQ:
5685 oprec = PREC_REL;
5686 ocode = LE_EXPR;
5687 break;
5688 case CPP_GREATER_EQ:
5689 oprec = PREC_REL;
5690 ocode = GE_EXPR;
5691 break;
5692 case CPP_EQ_EQ:
5693 oprec = PREC_EQ;
5694 ocode = EQ_EXPR;
5695 break;
5696 case CPP_NOT_EQ:
5697 oprec = PREC_EQ;
5698 ocode = NE_EXPR;
5699 break;
5700 case CPP_AND:
5701 oprec = PREC_BITAND;
5702 ocode = BIT_AND_EXPR;
5703 break;
5704 case CPP_XOR:
5705 oprec = PREC_BITXOR;
5706 ocode = BIT_XOR_EXPR;
5707 break;
5708 case CPP_OR:
5709 oprec = PREC_BITOR;
5710 ocode = BIT_IOR_EXPR;
5711 break;
5712 case CPP_AND_AND:
5713 oprec = PREC_LOGAND;
5714 ocode = TRUTH_ANDIF_EXPR;
5715 break;
5716 case CPP_OR_OR:
5717 oprec = PREC_LOGOR;
5718 ocode = TRUTH_ORIF_EXPR;
5719 break;
5720 default:
5721 /* Not a binary operator, so end of the binary
5722 expression. */
5723 goto out;
5725 binary_loc = c_parser_peek_token (parser)->location;
5726 while (oprec <= stack[sp].prec)
5728 if (sp == 0)
5729 goto out;
5730 POP;
5732 c_parser_consume_token (parser);
5733 switch (ocode)
5735 case TRUTH_ANDIF_EXPR:
5736 stack[sp].expr
5737 = default_function_array_read_conversion (stack[sp].loc,
5738 stack[sp].expr);
5739 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5740 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5741 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5742 == truthvalue_false_node);
5743 break;
5744 case TRUTH_ORIF_EXPR:
5745 stack[sp].expr
5746 = default_function_array_read_conversion (stack[sp].loc,
5747 stack[sp].expr);
5748 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5749 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5750 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5751 == truthvalue_true_node);
5752 break;
5753 default:
5754 break;
5756 sp++;
5757 stack[sp].loc = binary_loc;
5758 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5759 stack[sp].prec = oprec;
5760 stack[sp].op = ocode;
5761 stack[sp].loc = binary_loc;
5763 out:
5764 while (sp > 0)
5765 POP;
5766 return stack[0].expr;
5767 #undef POP
5770 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5771 NULL then it is an Objective-C message expression which is the
5772 primary-expression starting the expression as an initializer.
5774 cast-expression:
5775 unary-expression
5776 ( type-name ) unary-expression
5779 static struct c_expr
5780 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5782 location_t cast_loc = c_parser_peek_token (parser)->location;
5783 gcc_assert (!after || c_dialect_objc ());
5784 if (after)
5785 return c_parser_postfix_expression_after_primary (parser,
5786 cast_loc, *after);
5787 /* If the expression begins with a parenthesized type name, it may
5788 be either a cast or a compound literal; we need to see whether
5789 the next character is '{' to tell the difference. If not, it is
5790 an unary expression. Full detection of unknown typenames here
5791 would require a 3-token lookahead. */
5792 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5793 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5795 struct c_type_name *type_name;
5796 struct c_expr ret;
5797 struct c_expr expr;
5798 c_parser_consume_token (parser);
5799 type_name = c_parser_type_name (parser);
5800 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5801 if (type_name == NULL)
5803 ret.value = error_mark_node;
5804 ret.original_code = ERROR_MARK;
5805 ret.original_type = NULL;
5806 return ret;
5809 /* Save casted types in the function's used types hash table. */
5810 used_types_insert (type_name->specs->type);
5812 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5813 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5814 cast_loc);
5816 location_t expr_loc = c_parser_peek_token (parser)->location;
5817 expr = c_parser_cast_expression (parser, NULL);
5818 expr = default_function_array_read_conversion (expr_loc, expr);
5820 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5821 ret.original_code = ERROR_MARK;
5822 ret.original_type = NULL;
5823 return ret;
5825 else
5826 return c_parser_unary_expression (parser);
5829 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5831 unary-expression:
5832 postfix-expression
5833 ++ unary-expression
5834 -- unary-expression
5835 unary-operator cast-expression
5836 sizeof unary-expression
5837 sizeof ( type-name )
5839 unary-operator: one of
5840 & * + - ~ !
5842 GNU extensions:
5844 unary-expression:
5845 __alignof__ unary-expression
5846 __alignof__ ( type-name )
5847 && identifier
5849 (C11 permits _Alignof with type names only.)
5851 unary-operator: one of
5852 __extension__ __real__ __imag__
5854 Transactional Memory:
5856 unary-expression:
5857 transaction-expression
5859 In addition, the GNU syntax treats ++ and -- as unary operators, so
5860 they may be applied to cast expressions with errors for non-lvalues
5861 given later. */
5863 static struct c_expr
5864 c_parser_unary_expression (c_parser *parser)
5866 int ext;
5867 struct c_expr ret, op;
5868 location_t op_loc = c_parser_peek_token (parser)->location;
5869 location_t exp_loc;
5870 ret.original_code = ERROR_MARK;
5871 ret.original_type = NULL;
5872 switch (c_parser_peek_token (parser)->type)
5874 case CPP_PLUS_PLUS:
5875 c_parser_consume_token (parser);
5876 exp_loc = c_parser_peek_token (parser)->location;
5877 op = c_parser_cast_expression (parser, NULL);
5878 op = default_function_array_read_conversion (exp_loc, op);
5879 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5880 case CPP_MINUS_MINUS:
5881 c_parser_consume_token (parser);
5882 exp_loc = c_parser_peek_token (parser)->location;
5883 op = c_parser_cast_expression (parser, NULL);
5884 op = default_function_array_read_conversion (exp_loc, op);
5885 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5886 case CPP_AND:
5887 c_parser_consume_token (parser);
5888 op = c_parser_cast_expression (parser, NULL);
5889 mark_exp_read (op.value);
5890 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5891 case CPP_MULT:
5892 c_parser_consume_token (parser);
5893 exp_loc = c_parser_peek_token (parser)->location;
5894 op = c_parser_cast_expression (parser, NULL);
5895 op = default_function_array_read_conversion (exp_loc, op);
5896 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5897 return ret;
5898 case CPP_PLUS:
5899 if (!c_dialect_objc () && !in_system_header)
5900 warning_at (op_loc,
5901 OPT_Wtraditional,
5902 "traditional C rejects the unary plus operator");
5903 c_parser_consume_token (parser);
5904 exp_loc = c_parser_peek_token (parser)->location;
5905 op = c_parser_cast_expression (parser, NULL);
5906 op = default_function_array_read_conversion (exp_loc, op);
5907 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5908 case CPP_MINUS:
5909 c_parser_consume_token (parser);
5910 exp_loc = c_parser_peek_token (parser)->location;
5911 op = c_parser_cast_expression (parser, NULL);
5912 op = default_function_array_read_conversion (exp_loc, op);
5913 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5914 case CPP_COMPL:
5915 c_parser_consume_token (parser);
5916 exp_loc = c_parser_peek_token (parser)->location;
5917 op = c_parser_cast_expression (parser, NULL);
5918 op = default_function_array_read_conversion (exp_loc, op);
5919 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5920 case CPP_NOT:
5921 c_parser_consume_token (parser);
5922 exp_loc = c_parser_peek_token (parser)->location;
5923 op = c_parser_cast_expression (parser, NULL);
5924 op = default_function_array_read_conversion (exp_loc, op);
5925 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5926 case CPP_AND_AND:
5927 /* Refer to the address of a label as a pointer. */
5928 c_parser_consume_token (parser);
5929 if (c_parser_next_token_is (parser, CPP_NAME))
5931 ret.value = finish_label_address_expr
5932 (c_parser_peek_token (parser)->value, op_loc);
5933 c_parser_consume_token (parser);
5935 else
5937 c_parser_error (parser, "expected identifier");
5938 ret.value = error_mark_node;
5940 return ret;
5941 case CPP_KEYWORD:
5942 switch (c_parser_peek_token (parser)->keyword)
5944 case RID_SIZEOF:
5945 return c_parser_sizeof_expression (parser);
5946 case RID_ALIGNOF:
5947 return c_parser_alignof_expression (parser);
5948 case RID_EXTENSION:
5949 c_parser_consume_token (parser);
5950 ext = disable_extension_diagnostics ();
5951 ret = c_parser_cast_expression (parser, NULL);
5952 restore_extension_diagnostics (ext);
5953 return ret;
5954 case RID_REALPART:
5955 c_parser_consume_token (parser);
5956 exp_loc = c_parser_peek_token (parser)->location;
5957 op = c_parser_cast_expression (parser, NULL);
5958 op = default_function_array_conversion (exp_loc, op);
5959 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5960 case RID_IMAGPART:
5961 c_parser_consume_token (parser);
5962 exp_loc = c_parser_peek_token (parser)->location;
5963 op = c_parser_cast_expression (parser, NULL);
5964 op = default_function_array_conversion (exp_loc, op);
5965 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5966 case RID_TRANSACTION_ATOMIC:
5967 case RID_TRANSACTION_RELAXED:
5968 return c_parser_transaction_expression (parser,
5969 c_parser_peek_token (parser)->keyword);
5970 default:
5971 return c_parser_postfix_expression (parser);
5973 default:
5974 return c_parser_postfix_expression (parser);
5978 /* Parse a sizeof expression. */
5980 static struct c_expr
5981 c_parser_sizeof_expression (c_parser *parser)
5983 struct c_expr expr;
5984 location_t expr_loc;
5985 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5986 c_parser_consume_token (parser);
5987 c_inhibit_evaluation_warnings++;
5988 in_sizeof++;
5989 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5990 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5992 /* Either sizeof ( type-name ) or sizeof unary-expression
5993 starting with a compound literal. */
5994 struct c_type_name *type_name;
5995 c_parser_consume_token (parser);
5996 expr_loc = c_parser_peek_token (parser)->location;
5997 type_name = c_parser_type_name (parser);
5998 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5999 if (type_name == NULL)
6001 struct c_expr ret;
6002 c_inhibit_evaluation_warnings--;
6003 in_sizeof--;
6004 ret.value = error_mark_node;
6005 ret.original_code = ERROR_MARK;
6006 ret.original_type = NULL;
6007 return ret;
6009 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6011 expr = c_parser_postfix_expression_after_paren_type (parser,
6012 type_name,
6013 expr_loc);
6014 goto sizeof_expr;
6016 /* sizeof ( type-name ). */
6017 c_inhibit_evaluation_warnings--;
6018 in_sizeof--;
6019 return c_expr_sizeof_type (expr_loc, type_name);
6021 else
6023 expr_loc = c_parser_peek_token (parser)->location;
6024 expr = c_parser_unary_expression (parser);
6025 sizeof_expr:
6026 c_inhibit_evaluation_warnings--;
6027 in_sizeof--;
6028 mark_exp_read (expr.value);
6029 if (TREE_CODE (expr.value) == COMPONENT_REF
6030 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6031 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6032 return c_expr_sizeof_expr (expr_loc, expr);
6036 /* Parse an alignof expression. */
6038 static struct c_expr
6039 c_parser_alignof_expression (c_parser *parser)
6041 struct c_expr expr;
6042 location_t loc = c_parser_peek_token (parser)->location;
6043 tree alignof_spelling = c_parser_peek_token (parser)->value;
6044 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6045 /* A diagnostic is not required for the use of this identifier in
6046 the implementation namespace; only diagnose it for the C11
6047 spelling because of existing code using the other spellings. */
6048 if (!flag_isoc11
6049 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6051 if (flag_isoc99)
6052 pedwarn (loc, OPT_pedantic, "ISO C99 does not support %qE",
6053 alignof_spelling);
6054 else
6055 pedwarn (loc, OPT_pedantic, "ISO C90 does not support %qE",
6056 alignof_spelling);
6058 c_parser_consume_token (parser);
6059 c_inhibit_evaluation_warnings++;
6060 in_alignof++;
6061 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6062 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6064 /* Either __alignof__ ( type-name ) or __alignof__
6065 unary-expression starting with a compound literal. */
6066 location_t loc;
6067 struct c_type_name *type_name;
6068 struct c_expr ret;
6069 c_parser_consume_token (parser);
6070 loc = c_parser_peek_token (parser)->location;
6071 type_name = c_parser_type_name (parser);
6072 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6073 if (type_name == NULL)
6075 struct c_expr ret;
6076 c_inhibit_evaluation_warnings--;
6077 in_alignof--;
6078 ret.value = error_mark_node;
6079 ret.original_code = ERROR_MARK;
6080 ret.original_type = NULL;
6081 return ret;
6083 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6085 expr = c_parser_postfix_expression_after_paren_type (parser,
6086 type_name,
6087 loc);
6088 goto alignof_expr;
6090 /* alignof ( type-name ). */
6091 c_inhibit_evaluation_warnings--;
6092 in_alignof--;
6093 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6094 ret.original_code = ERROR_MARK;
6095 ret.original_type = NULL;
6096 return ret;
6098 else
6100 struct c_expr ret;
6101 expr = c_parser_unary_expression (parser);
6102 alignof_expr:
6103 mark_exp_read (expr.value);
6104 c_inhibit_evaluation_warnings--;
6105 in_alignof--;
6106 pedwarn (loc, OPT_pedantic, "ISO C does not allow %<%E (expression)%>",
6107 alignof_spelling);
6108 ret.value = c_alignof_expr (loc, expr.value);
6109 ret.original_code = ERROR_MARK;
6110 ret.original_type = NULL;
6111 return ret;
6115 /* Helper function to read arguments of builtins which are interfaces
6116 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6117 others. The name of the builtin is passed using BNAME parameter.
6118 Function returns true if there were no errors while parsing and
6119 stores the arguments in CEXPR_LIST. */
6120 static bool
6121 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6122 VEC(c_expr_t,gc) **ret_cexpr_list)
6124 location_t loc = c_parser_peek_token (parser)->location;
6125 VEC (c_expr_t,gc) *cexpr_list;
6126 c_expr_t expr;
6128 *ret_cexpr_list = NULL;
6129 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6131 error_at (loc, "cannot take address of %qs", bname);
6132 return false;
6135 c_parser_consume_token (parser);
6137 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6139 c_parser_consume_token (parser);
6140 return true;
6143 expr = c_parser_expr_no_commas (parser, NULL);
6144 cexpr_list = VEC_alloc (c_expr_t, gc, 1);
6145 C_EXPR_APPEND (cexpr_list, expr);
6146 while (c_parser_next_token_is (parser, CPP_COMMA))
6148 c_parser_consume_token (parser);
6149 expr = c_parser_expr_no_commas (parser, NULL);
6150 C_EXPR_APPEND (cexpr_list, expr);
6153 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6154 return false;
6156 *ret_cexpr_list = cexpr_list;
6157 return true;
6161 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6163 postfix-expression:
6164 primary-expression
6165 postfix-expression [ expression ]
6166 postfix-expression ( argument-expression-list[opt] )
6167 postfix-expression . identifier
6168 postfix-expression -> identifier
6169 postfix-expression ++
6170 postfix-expression --
6171 ( type-name ) { initializer-list }
6172 ( type-name ) { initializer-list , }
6174 argument-expression-list:
6175 argument-expression
6176 argument-expression-list , argument-expression
6178 primary-expression:
6179 identifier
6180 constant
6181 string-literal
6182 ( expression )
6184 GNU extensions:
6186 primary-expression:
6187 __func__
6188 (treated as a keyword in GNU C)
6189 __FUNCTION__
6190 __PRETTY_FUNCTION__
6191 ( compound-statement )
6192 __builtin_va_arg ( assignment-expression , type-name )
6193 __builtin_offsetof ( type-name , offsetof-member-designator )
6194 __builtin_choose_expr ( assignment-expression ,
6195 assignment-expression ,
6196 assignment-expression )
6197 __builtin_types_compatible_p ( type-name , type-name )
6198 __builtin_complex ( assignment-expression , assignment-expression )
6199 __builtin_shuffle ( assignment-expression , assignment-expression )
6200 __builtin_shuffle ( assignment-expression ,
6201 assignment-expression ,
6202 assignment-expression, )
6204 offsetof-member-designator:
6205 identifier
6206 offsetof-member-designator . identifier
6207 offsetof-member-designator [ expression ]
6209 Objective-C:
6211 primary-expression:
6212 [ objc-receiver objc-message-args ]
6213 @selector ( objc-selector-arg )
6214 @protocol ( identifier )
6215 @encode ( type-name )
6216 objc-string-literal
6217 Classname . identifier
6220 static struct c_expr
6221 c_parser_postfix_expression (c_parser *parser)
6223 struct c_expr expr, e1;
6224 struct c_type_name *t1, *t2;
6225 location_t loc = c_parser_peek_token (parser)->location;;
6226 expr.original_code = ERROR_MARK;
6227 expr.original_type = NULL;
6228 switch (c_parser_peek_token (parser)->type)
6230 case CPP_NUMBER:
6231 expr.value = c_parser_peek_token (parser)->value;
6232 loc = c_parser_peek_token (parser)->location;
6233 c_parser_consume_token (parser);
6234 if (TREE_CODE (expr.value) == FIXED_CST
6235 && !targetm.fixed_point_supported_p ())
6237 error_at (loc, "fixed-point types not supported for this target");
6238 expr.value = error_mark_node;
6240 break;
6241 case CPP_CHAR:
6242 case CPP_CHAR16:
6243 case CPP_CHAR32:
6244 case CPP_WCHAR:
6245 expr.value = c_parser_peek_token (parser)->value;
6246 c_parser_consume_token (parser);
6247 break;
6248 case CPP_STRING:
6249 case CPP_STRING16:
6250 case CPP_STRING32:
6251 case CPP_WSTRING:
6252 case CPP_UTF8STRING:
6253 expr.value = c_parser_peek_token (parser)->value;
6254 expr.original_code = STRING_CST;
6255 c_parser_consume_token (parser);
6256 break;
6257 case CPP_OBJC_STRING:
6258 gcc_assert (c_dialect_objc ());
6259 expr.value
6260 = objc_build_string_object (c_parser_peek_token (parser)->value);
6261 c_parser_consume_token (parser);
6262 break;
6263 case CPP_NAME:
6264 switch (c_parser_peek_token (parser)->id_kind)
6266 case C_ID_ID:
6268 tree id = c_parser_peek_token (parser)->value;
6269 c_parser_consume_token (parser);
6270 expr.value = build_external_ref (loc, id,
6271 (c_parser_peek_token (parser)->type
6272 == CPP_OPEN_PAREN),
6273 &expr.original_type);
6274 break;
6276 case C_ID_CLASSNAME:
6278 /* Here we parse the Objective-C 2.0 Class.name dot
6279 syntax. */
6280 tree class_name = c_parser_peek_token (parser)->value;
6281 tree component;
6282 c_parser_consume_token (parser);
6283 gcc_assert (c_dialect_objc ());
6284 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6286 expr.value = error_mark_node;
6287 break;
6289 if (c_parser_next_token_is_not (parser, CPP_NAME))
6291 c_parser_error (parser, "expected identifier");
6292 expr.value = error_mark_node;
6293 break;
6295 component = c_parser_peek_token (parser)->value;
6296 c_parser_consume_token (parser);
6297 expr.value = objc_build_class_component_ref (class_name,
6298 component);
6299 break;
6301 default:
6302 c_parser_error (parser, "expected expression");
6303 expr.value = error_mark_node;
6304 break;
6306 break;
6307 case CPP_OPEN_PAREN:
6308 /* A parenthesized expression, statement expression or compound
6309 literal. */
6310 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6312 /* A statement expression. */
6313 tree stmt;
6314 location_t brace_loc;
6315 c_parser_consume_token (parser);
6316 brace_loc = c_parser_peek_token (parser)->location;
6317 c_parser_consume_token (parser);
6318 if (!building_stmt_list_p ())
6320 error_at (loc, "braced-group within expression allowed "
6321 "only inside a function");
6322 parser->error = true;
6323 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6324 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6325 expr.value = error_mark_node;
6326 break;
6328 stmt = c_begin_stmt_expr ();
6329 c_parser_compound_statement_nostart (parser);
6330 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6331 "expected %<)%>");
6332 pedwarn (loc, OPT_pedantic,
6333 "ISO C forbids braced-groups within expressions");
6334 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6335 mark_exp_read (expr.value);
6337 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6339 /* A compound literal. ??? Can we actually get here rather
6340 than going directly to
6341 c_parser_postfix_expression_after_paren_type from
6342 elsewhere? */
6343 location_t loc;
6344 struct c_type_name *type_name;
6345 c_parser_consume_token (parser);
6346 loc = c_parser_peek_token (parser)->location;
6347 type_name = c_parser_type_name (parser);
6348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6349 "expected %<)%>");
6350 if (type_name == NULL)
6352 expr.value = error_mark_node;
6354 else
6355 expr = c_parser_postfix_expression_after_paren_type (parser,
6356 type_name,
6357 loc);
6359 else
6361 /* A parenthesized expression. */
6362 c_parser_consume_token (parser);
6363 expr = c_parser_expression (parser);
6364 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6365 TREE_NO_WARNING (expr.value) = 1;
6366 if (expr.original_code != C_MAYBE_CONST_EXPR)
6367 expr.original_code = ERROR_MARK;
6368 /* Don't change EXPR.ORIGINAL_TYPE. */
6369 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6370 "expected %<)%>");
6372 break;
6373 case CPP_KEYWORD:
6374 switch (c_parser_peek_token (parser)->keyword)
6376 case RID_FUNCTION_NAME:
6377 case RID_PRETTY_FUNCTION_NAME:
6378 case RID_C99_FUNCTION_NAME:
6379 expr.value = fname_decl (loc,
6380 c_parser_peek_token (parser)->keyword,
6381 c_parser_peek_token (parser)->value);
6382 c_parser_consume_token (parser);
6383 break;
6384 case RID_VA_ARG:
6385 c_parser_consume_token (parser);
6386 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6388 expr.value = error_mark_node;
6389 break;
6391 e1 = c_parser_expr_no_commas (parser, NULL);
6392 mark_exp_read (e1.value);
6393 e1.value = c_fully_fold (e1.value, false, NULL);
6394 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6397 expr.value = error_mark_node;
6398 break;
6400 loc = c_parser_peek_token (parser)->location;
6401 t1 = c_parser_type_name (parser);
6402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6403 "expected %<)%>");
6404 if (t1 == NULL)
6406 expr.value = error_mark_node;
6408 else
6410 tree type_expr = NULL_TREE;
6411 expr.value = c_build_va_arg (loc, e1.value,
6412 groktypename (t1, &type_expr, NULL));
6413 if (type_expr)
6415 expr.value = build2 (C_MAYBE_CONST_EXPR,
6416 TREE_TYPE (expr.value), type_expr,
6417 expr.value);
6418 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6421 break;
6422 case RID_OFFSETOF:
6423 c_parser_consume_token (parser);
6424 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6426 expr.value = error_mark_node;
6427 break;
6429 t1 = c_parser_type_name (parser);
6430 if (t1 == NULL)
6431 parser->error = true;
6432 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6433 gcc_assert (parser->error);
6434 if (parser->error)
6436 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6437 expr.value = error_mark_node;
6438 break;
6442 tree type = groktypename (t1, NULL, NULL);
6443 tree offsetof_ref;
6444 if (type == error_mark_node)
6445 offsetof_ref = error_mark_node;
6446 else
6448 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6449 SET_EXPR_LOCATION (offsetof_ref, loc);
6451 /* Parse the second argument to __builtin_offsetof. We
6452 must have one identifier, and beyond that we want to
6453 accept sub structure and sub array references. */
6454 if (c_parser_next_token_is (parser, CPP_NAME))
6456 offsetof_ref = build_component_ref
6457 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6458 c_parser_consume_token (parser);
6459 while (c_parser_next_token_is (parser, CPP_DOT)
6460 || c_parser_next_token_is (parser,
6461 CPP_OPEN_SQUARE)
6462 || c_parser_next_token_is (parser,
6463 CPP_DEREF))
6465 if (c_parser_next_token_is (parser, CPP_DEREF))
6467 loc = c_parser_peek_token (parser)->location;
6468 offsetof_ref = build_array_ref (loc,
6469 offsetof_ref,
6470 integer_zero_node);
6471 goto do_dot;
6473 else if (c_parser_next_token_is (parser, CPP_DOT))
6475 do_dot:
6476 c_parser_consume_token (parser);
6477 if (c_parser_next_token_is_not (parser,
6478 CPP_NAME))
6480 c_parser_error (parser, "expected identifier");
6481 break;
6483 offsetof_ref = build_component_ref
6484 (loc, offsetof_ref,
6485 c_parser_peek_token (parser)->value);
6486 c_parser_consume_token (parser);
6488 else
6490 tree idx;
6491 loc = c_parser_peek_token (parser)->location;
6492 c_parser_consume_token (parser);
6493 idx = c_parser_expression (parser).value;
6494 idx = c_fully_fold (idx, false, NULL);
6495 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6496 "expected %<]%>");
6497 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6501 else
6502 c_parser_error (parser, "expected identifier");
6503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6504 "expected %<)%>");
6505 expr.value = fold_offsetof (offsetof_ref);
6507 break;
6508 case RID_CHOOSE_EXPR:
6510 VEC (c_expr_t, gc) *cexpr_list;
6511 c_expr_t *e1_p, *e2_p, *e3_p;
6512 tree c;
6514 c_parser_consume_token (parser);
6515 if (!c_parser_get_builtin_args (parser,
6516 "__builtin_choose_expr",
6517 &cexpr_list))
6519 expr.value = error_mark_node;
6520 break;
6523 if (VEC_length (c_expr_t, cexpr_list) != 3)
6525 error_at (loc, "wrong number of arguments to "
6526 "%<__builtin_choose_expr%>");
6527 expr.value = error_mark_node;
6528 break;
6531 e1_p = VEC_index (c_expr_t, cexpr_list, 0);
6532 e2_p = VEC_index (c_expr_t, cexpr_list, 1);
6533 e3_p = VEC_index (c_expr_t, cexpr_list, 2);
6535 c = e1_p->value;
6536 mark_exp_read (e2_p->value);
6537 mark_exp_read (e3_p->value);
6538 if (TREE_CODE (c) != INTEGER_CST
6539 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6540 error_at (loc,
6541 "first argument to %<__builtin_choose_expr%> not"
6542 " a constant");
6543 constant_expression_warning (c);
6544 expr = integer_zerop (c) ? *e3_p : *e2_p;
6545 break;
6547 case RID_TYPES_COMPATIBLE_P:
6548 c_parser_consume_token (parser);
6549 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6551 expr.value = error_mark_node;
6552 break;
6554 t1 = c_parser_type_name (parser);
6555 if (t1 == NULL)
6557 expr.value = error_mark_node;
6558 break;
6560 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6562 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6563 expr.value = error_mark_node;
6564 break;
6566 t2 = c_parser_type_name (parser);
6567 if (t2 == NULL)
6569 expr.value = error_mark_node;
6570 break;
6572 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6573 "expected %<)%>");
6575 tree e1, e2;
6576 e1 = groktypename (t1, NULL, NULL);
6577 e2 = groktypename (t2, NULL, NULL);
6578 if (e1 == error_mark_node || e2 == error_mark_node)
6580 expr.value = error_mark_node;
6581 break;
6584 e1 = TYPE_MAIN_VARIANT (e1);
6585 e2 = TYPE_MAIN_VARIANT (e2);
6587 expr.value
6588 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6590 break;
6591 case RID_BUILTIN_COMPLEX:
6593 VEC(c_expr_t, gc) *cexpr_list;
6594 c_expr_t *e1_p, *e2_p;
6596 c_parser_consume_token (parser);
6597 if (!c_parser_get_builtin_args (parser,
6598 "__builtin_complex",
6599 &cexpr_list))
6601 expr.value = error_mark_node;
6602 break;
6605 if (VEC_length (c_expr_t, cexpr_list) != 2)
6607 error_at (loc, "wrong number of arguments to "
6608 "%<__builtin_complex%>");
6609 expr.value = error_mark_node;
6610 break;
6613 e1_p = VEC_index (c_expr_t, cexpr_list, 0);
6614 e2_p = VEC_index (c_expr_t, cexpr_list, 1);
6616 mark_exp_read (e1_p->value);
6617 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6618 e1_p->value = convert (TREE_TYPE (e1_p->value),
6619 TREE_OPERAND (e1_p->value, 0));
6620 mark_exp_read (e2_p->value);
6621 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6622 e2_p->value = convert (TREE_TYPE (e2_p->value),
6623 TREE_OPERAND (e2_p->value, 0));
6624 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6625 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6626 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6627 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6629 error_at (loc, "%<__builtin_complex%> operand "
6630 "not of real binary floating-point type");
6631 expr.value = error_mark_node;
6632 break;
6634 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6635 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6637 error_at (loc,
6638 "%<__builtin_complex%> operands of different types");
6639 expr.value = error_mark_node;
6640 break;
6642 if (!flag_isoc99)
6643 pedwarn (loc, OPT_pedantic,
6644 "ISO C90 does not support complex types");
6645 expr.value = build2 (COMPLEX_EXPR,
6646 build_complex_type
6647 (TYPE_MAIN_VARIANT
6648 (TREE_TYPE (e1_p->value))),
6649 e1_p->value, e2_p->value);
6650 break;
6652 case RID_BUILTIN_SHUFFLE:
6654 VEC(c_expr_t,gc) *cexpr_list;
6655 unsigned int i;
6656 c_expr_t *p;
6658 c_parser_consume_token (parser);
6659 if (!c_parser_get_builtin_args (parser,
6660 "__builtin_shuffle",
6661 &cexpr_list))
6663 expr.value = error_mark_node;
6664 break;
6667 FOR_EACH_VEC_ELT (c_expr_t, cexpr_list, i, p)
6668 mark_exp_read (p->value);
6670 if (VEC_length (c_expr_t, cexpr_list) == 2)
6671 expr.value =
6672 c_build_vec_perm_expr
6673 (loc, VEC_index (c_expr_t, cexpr_list, 0)->value,
6674 NULL_TREE, VEC_index (c_expr_t, cexpr_list, 1)->value);
6676 else if (VEC_length (c_expr_t, cexpr_list) == 3)
6677 expr.value =
6678 c_build_vec_perm_expr
6679 (loc, VEC_index (c_expr_t, cexpr_list, 0)->value,
6680 VEC_index (c_expr_t, cexpr_list, 1)->value,
6681 VEC_index (c_expr_t, cexpr_list, 2)->value);
6682 else
6684 error_at (loc, "wrong number of arguments to "
6685 "%<__builtin_shuffle%>");
6686 expr.value = error_mark_node;
6688 break;
6690 case RID_AT_SELECTOR:
6691 gcc_assert (c_dialect_objc ());
6692 c_parser_consume_token (parser);
6693 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6695 expr.value = error_mark_node;
6696 break;
6699 tree sel = c_parser_objc_selector_arg (parser);
6700 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6701 "expected %<)%>");
6702 expr.value = objc_build_selector_expr (loc, sel);
6704 break;
6705 case RID_AT_PROTOCOL:
6706 gcc_assert (c_dialect_objc ());
6707 c_parser_consume_token (parser);
6708 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6710 expr.value = error_mark_node;
6711 break;
6713 if (c_parser_next_token_is_not (parser, CPP_NAME))
6715 c_parser_error (parser, "expected identifier");
6716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6717 expr.value = error_mark_node;
6718 break;
6721 tree id = c_parser_peek_token (parser)->value;
6722 c_parser_consume_token (parser);
6723 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6724 "expected %<)%>");
6725 expr.value = objc_build_protocol_expr (id);
6727 break;
6728 case RID_AT_ENCODE:
6729 /* Extension to support C-structures in the archiver. */
6730 gcc_assert (c_dialect_objc ());
6731 c_parser_consume_token (parser);
6732 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6734 expr.value = error_mark_node;
6735 break;
6737 t1 = c_parser_type_name (parser);
6738 if (t1 == NULL)
6740 expr.value = error_mark_node;
6741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6742 break;
6744 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6745 "expected %<)%>");
6747 tree type = groktypename (t1, NULL, NULL);
6748 expr.value = objc_build_encode_expr (type);
6750 break;
6751 default:
6752 c_parser_error (parser, "expected expression");
6753 expr.value = error_mark_node;
6754 break;
6756 break;
6757 case CPP_OPEN_SQUARE:
6758 if (c_dialect_objc ())
6760 tree receiver, args;
6761 c_parser_consume_token (parser);
6762 receiver = c_parser_objc_receiver (parser);
6763 args = c_parser_objc_message_args (parser);
6764 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6765 "expected %<]%>");
6766 expr.value = objc_build_message_expr (receiver, args);
6767 break;
6769 /* Else fall through to report error. */
6770 default:
6771 c_parser_error (parser, "expected expression");
6772 expr.value = error_mark_node;
6773 break;
6775 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6778 /* Parse a postfix expression after a parenthesized type name: the
6779 brace-enclosed initializer of a compound literal, possibly followed
6780 by some postfix operators. This is separate because it is not
6781 possible to tell until after the type name whether a cast
6782 expression has a cast or a compound literal, or whether the operand
6783 of sizeof is a parenthesized type name or starts with a compound
6784 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6785 location of the first token after the parentheses around the type
6786 name. */
6788 static struct c_expr
6789 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6790 struct c_type_name *type_name,
6791 location_t type_loc)
6793 tree type;
6794 struct c_expr init;
6795 bool non_const;
6796 struct c_expr expr;
6797 location_t start_loc;
6798 tree type_expr = NULL_TREE;
6799 bool type_expr_const = true;
6800 check_compound_literal_type (type_loc, type_name);
6801 start_init (NULL_TREE, NULL, 0);
6802 type = groktypename (type_name, &type_expr, &type_expr_const);
6803 start_loc = c_parser_peek_token (parser)->location;
6804 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6806 error_at (type_loc, "compound literal has variable size");
6807 type = error_mark_node;
6809 init = c_parser_braced_init (parser, type, false);
6810 finish_init ();
6811 maybe_warn_string_init (type, init);
6813 if (type != error_mark_node
6814 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6815 && current_function_decl)
6817 error ("compound literal qualified by address-space qualifier");
6818 type = error_mark_node;
6821 if (!flag_isoc99)
6822 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6823 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6824 ? CONSTRUCTOR_NON_CONST (init.value)
6825 : init.original_code == C_MAYBE_CONST_EXPR);
6826 non_const |= !type_expr_const;
6827 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6828 expr.original_code = ERROR_MARK;
6829 expr.original_type = NULL;
6830 if (type_expr)
6832 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6834 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6835 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6837 else
6839 gcc_assert (!non_const);
6840 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6841 type_expr, expr.value);
6844 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6847 /* Parse a postfix expression after the initial primary or compound
6848 literal; that is, parse a series of postfix operators.
6850 EXPR_LOC is the location of the primary expression. */
6852 static struct c_expr
6853 c_parser_postfix_expression_after_primary (c_parser *parser,
6854 location_t expr_loc,
6855 struct c_expr expr)
6857 struct c_expr orig_expr;
6858 tree ident, idx;
6859 VEC(tree,gc) *exprlist;
6860 VEC(tree,gc) *origtypes;
6861 while (true)
6863 location_t op_loc = c_parser_peek_token (parser)->location;
6864 switch (c_parser_peek_token (parser)->type)
6866 case CPP_OPEN_SQUARE:
6867 /* Array reference. */
6868 c_parser_consume_token (parser);
6869 idx = c_parser_expression (parser).value;
6870 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6871 "expected %<]%>");
6872 expr.value = build_array_ref (op_loc, expr.value, idx);
6873 expr.original_code = ERROR_MARK;
6874 expr.original_type = NULL;
6875 break;
6876 case CPP_OPEN_PAREN:
6877 /* Function call. */
6878 c_parser_consume_token (parser);
6879 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6880 exprlist = NULL;
6881 else
6882 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6883 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6884 "expected %<)%>");
6885 orig_expr = expr;
6886 mark_exp_read (expr.value);
6887 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6888 "(" after the FUNCNAME, which is what we have now. */
6889 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6890 origtypes);
6891 expr.original_code = ERROR_MARK;
6892 if (TREE_CODE (expr.value) == INTEGER_CST
6893 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6894 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6895 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6896 expr.original_code = C_MAYBE_CONST_EXPR;
6897 expr.original_type = NULL;
6898 if (exprlist != NULL)
6900 release_tree_vector (exprlist);
6901 release_tree_vector (origtypes);
6903 break;
6904 case CPP_DOT:
6905 /* Structure element reference. */
6906 c_parser_consume_token (parser);
6907 expr = default_function_array_conversion (expr_loc, expr);
6908 if (c_parser_next_token_is (parser, CPP_NAME))
6909 ident = c_parser_peek_token (parser)->value;
6910 else
6912 c_parser_error (parser, "expected identifier");
6913 expr.value = error_mark_node;
6914 expr.original_code = ERROR_MARK;
6915 expr.original_type = NULL;
6916 return expr;
6918 c_parser_consume_token (parser);
6919 expr.value = build_component_ref (op_loc, expr.value, ident);
6920 expr.original_code = ERROR_MARK;
6921 if (TREE_CODE (expr.value) != COMPONENT_REF)
6922 expr.original_type = NULL;
6923 else
6925 /* Remember the original type of a bitfield. */
6926 tree field = TREE_OPERAND (expr.value, 1);
6927 if (TREE_CODE (field) != FIELD_DECL)
6928 expr.original_type = NULL;
6929 else
6930 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6932 break;
6933 case CPP_DEREF:
6934 /* Structure element reference. */
6935 c_parser_consume_token (parser);
6936 expr = default_function_array_conversion (expr_loc, expr);
6937 if (c_parser_next_token_is (parser, CPP_NAME))
6938 ident = c_parser_peek_token (parser)->value;
6939 else
6941 c_parser_error (parser, "expected identifier");
6942 expr.value = error_mark_node;
6943 expr.original_code = ERROR_MARK;
6944 expr.original_type = NULL;
6945 return expr;
6947 c_parser_consume_token (parser);
6948 expr.value = build_component_ref (op_loc,
6949 build_indirect_ref (op_loc,
6950 expr.value,
6951 RO_ARROW),
6952 ident);
6953 expr.original_code = ERROR_MARK;
6954 if (TREE_CODE (expr.value) != COMPONENT_REF)
6955 expr.original_type = NULL;
6956 else
6958 /* Remember the original type of a bitfield. */
6959 tree field = TREE_OPERAND (expr.value, 1);
6960 if (TREE_CODE (field) != FIELD_DECL)
6961 expr.original_type = NULL;
6962 else
6963 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6965 break;
6966 case CPP_PLUS_PLUS:
6967 /* Postincrement. */
6968 c_parser_consume_token (parser);
6969 expr = default_function_array_read_conversion (expr_loc, expr);
6970 expr.value = build_unary_op (op_loc,
6971 POSTINCREMENT_EXPR, expr.value, 0);
6972 expr.original_code = ERROR_MARK;
6973 expr.original_type = NULL;
6974 break;
6975 case CPP_MINUS_MINUS:
6976 /* Postdecrement. */
6977 c_parser_consume_token (parser);
6978 expr = default_function_array_read_conversion (expr_loc, expr);
6979 expr.value = build_unary_op (op_loc,
6980 POSTDECREMENT_EXPR, expr.value, 0);
6981 expr.original_code = ERROR_MARK;
6982 expr.original_type = NULL;
6983 break;
6984 default:
6985 return expr;
6990 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6992 expression:
6993 assignment-expression
6994 expression , assignment-expression
6997 static struct c_expr
6998 c_parser_expression (c_parser *parser)
7000 struct c_expr expr;
7001 expr = c_parser_expr_no_commas (parser, NULL);
7002 while (c_parser_next_token_is (parser, CPP_COMMA))
7004 struct c_expr next;
7005 tree lhsval;
7006 location_t loc = c_parser_peek_token (parser)->location;
7007 location_t expr_loc;
7008 c_parser_consume_token (parser);
7009 expr_loc = c_parser_peek_token (parser)->location;
7010 lhsval = expr.value;
7011 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7012 lhsval = TREE_OPERAND (lhsval, 1);
7013 if (DECL_P (lhsval) || handled_component_p (lhsval))
7014 mark_exp_read (lhsval);
7015 next = c_parser_expr_no_commas (parser, NULL);
7016 next = default_function_array_conversion (expr_loc, next);
7017 expr.value = build_compound_expr (loc, expr.value, next.value);
7018 expr.original_code = COMPOUND_EXPR;
7019 expr.original_type = next.original_type;
7021 return expr;
7024 /* Parse an expression and convert functions or arrays to
7025 pointers. */
7027 static struct c_expr
7028 c_parser_expression_conv (c_parser *parser)
7030 struct c_expr expr;
7031 location_t loc = c_parser_peek_token (parser)->location;
7032 expr = c_parser_expression (parser);
7033 expr = default_function_array_conversion (loc, expr);
7034 return expr;
7037 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7038 functions and arrays to pointers. If FOLD_P, fold the expressions.
7040 nonempty-expr-list:
7041 assignment-expression
7042 nonempty-expr-list , assignment-expression
7045 static VEC(tree,gc) *
7046 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7047 VEC(tree,gc) **p_orig_types)
7049 VEC(tree,gc) *ret;
7050 VEC(tree,gc) *orig_types;
7051 struct c_expr expr;
7052 location_t loc = c_parser_peek_token (parser)->location;
7054 ret = make_tree_vector ();
7055 if (p_orig_types == NULL)
7056 orig_types = NULL;
7057 else
7058 orig_types = make_tree_vector ();
7060 expr = c_parser_expr_no_commas (parser, NULL);
7061 if (convert_p)
7062 expr = default_function_array_read_conversion (loc, expr);
7063 if (fold_p)
7064 expr.value = c_fully_fold (expr.value, false, NULL);
7065 VEC_quick_push (tree, ret, expr.value);
7066 if (orig_types != NULL)
7067 VEC_quick_push (tree, orig_types, expr.original_type);
7068 while (c_parser_next_token_is (parser, CPP_COMMA))
7070 c_parser_consume_token (parser);
7071 loc = c_parser_peek_token (parser)->location;
7072 expr = c_parser_expr_no_commas (parser, NULL);
7073 if (convert_p)
7074 expr = default_function_array_read_conversion (loc, expr);
7075 if (fold_p)
7076 expr.value = c_fully_fold (expr.value, false, NULL);
7077 VEC_safe_push (tree, gc, ret, expr.value);
7078 if (orig_types != NULL)
7079 VEC_safe_push (tree, gc, orig_types, expr.original_type);
7081 if (orig_types != NULL)
7082 *p_orig_types = orig_types;
7083 return ret;
7086 /* Parse Objective-C-specific constructs. */
7088 /* Parse an objc-class-definition.
7090 objc-class-definition:
7091 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7092 objc-class-instance-variables[opt] objc-methodprotolist @end
7093 @implementation identifier objc-superclass[opt]
7094 objc-class-instance-variables[opt]
7095 @interface identifier ( identifier ) objc-protocol-refs[opt]
7096 objc-methodprotolist @end
7097 @interface identifier ( ) objc-protocol-refs[opt]
7098 objc-methodprotolist @end
7099 @implementation identifier ( identifier )
7101 objc-superclass:
7102 : identifier
7104 "@interface identifier (" must start "@interface identifier (
7105 identifier ) ...": objc-methodprotolist in the first production may
7106 not start with a parenthesized identifier as a declarator of a data
7107 definition with no declaration specifiers if the objc-superclass,
7108 objc-protocol-refs and objc-class-instance-variables are omitted. */
7110 static void
7111 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7113 bool iface_p;
7114 tree id1;
7115 tree superclass;
7116 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7117 iface_p = true;
7118 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7119 iface_p = false;
7120 else
7121 gcc_unreachable ();
7123 c_parser_consume_token (parser);
7124 if (c_parser_next_token_is_not (parser, CPP_NAME))
7126 c_parser_error (parser, "expected identifier");
7127 return;
7129 id1 = c_parser_peek_token (parser)->value;
7130 c_parser_consume_token (parser);
7131 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7133 /* We have a category or class extension. */
7134 tree id2;
7135 tree proto = NULL_TREE;
7136 c_parser_consume_token (parser);
7137 if (c_parser_next_token_is_not (parser, CPP_NAME))
7139 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7141 /* We have a class extension. */
7142 id2 = NULL_TREE;
7144 else
7146 c_parser_error (parser, "expected identifier or %<)%>");
7147 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7148 return;
7151 else
7153 id2 = c_parser_peek_token (parser)->value;
7154 c_parser_consume_token (parser);
7156 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7157 if (!iface_p)
7159 objc_start_category_implementation (id1, id2);
7160 return;
7162 if (c_parser_next_token_is (parser, CPP_LESS))
7163 proto = c_parser_objc_protocol_refs (parser);
7164 objc_start_category_interface (id1, id2, proto, attributes);
7165 c_parser_objc_methodprotolist (parser);
7166 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7167 objc_finish_interface ();
7168 return;
7170 if (c_parser_next_token_is (parser, CPP_COLON))
7172 c_parser_consume_token (parser);
7173 if (c_parser_next_token_is_not (parser, CPP_NAME))
7175 c_parser_error (parser, "expected identifier");
7176 return;
7178 superclass = c_parser_peek_token (parser)->value;
7179 c_parser_consume_token (parser);
7181 else
7182 superclass = NULL_TREE;
7183 if (iface_p)
7185 tree proto = NULL_TREE;
7186 if (c_parser_next_token_is (parser, CPP_LESS))
7187 proto = c_parser_objc_protocol_refs (parser);
7188 objc_start_class_interface (id1, superclass, proto, attributes);
7190 else
7191 objc_start_class_implementation (id1, superclass);
7192 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7193 c_parser_objc_class_instance_variables (parser);
7194 if (iface_p)
7196 objc_continue_interface ();
7197 c_parser_objc_methodprotolist (parser);
7198 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7199 objc_finish_interface ();
7201 else
7203 objc_continue_implementation ();
7204 return;
7208 /* Parse objc-class-instance-variables.
7210 objc-class-instance-variables:
7211 { objc-instance-variable-decl-list[opt] }
7213 objc-instance-variable-decl-list:
7214 objc-visibility-spec
7215 objc-instance-variable-decl ;
7217 objc-instance-variable-decl-list objc-visibility-spec
7218 objc-instance-variable-decl-list objc-instance-variable-decl ;
7219 objc-instance-variable-decl-list ;
7221 objc-visibility-spec:
7222 @private
7223 @protected
7224 @public
7226 objc-instance-variable-decl:
7227 struct-declaration
7230 static void
7231 c_parser_objc_class_instance_variables (c_parser *parser)
7233 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7234 c_parser_consume_token (parser);
7235 while (c_parser_next_token_is_not (parser, CPP_EOF))
7237 tree decls;
7238 /* Parse any stray semicolon. */
7239 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7241 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7242 "extra semicolon");
7243 c_parser_consume_token (parser);
7244 continue;
7246 /* Stop if at the end of the instance variables. */
7247 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7249 c_parser_consume_token (parser);
7250 break;
7252 /* Parse any objc-visibility-spec. */
7253 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7255 c_parser_consume_token (parser);
7256 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7257 continue;
7259 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7261 c_parser_consume_token (parser);
7262 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7263 continue;
7265 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7267 c_parser_consume_token (parser);
7268 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7269 continue;
7271 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7273 c_parser_consume_token (parser);
7274 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7275 continue;
7277 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7279 c_parser_pragma (parser, pragma_external);
7280 continue;
7283 /* Parse some comma-separated declarations. */
7284 decls = c_parser_struct_declaration (parser);
7285 if (decls == NULL)
7287 /* There is a syntax error. We want to skip the offending
7288 tokens up to the next ';' (included) or '}'
7289 (excluded). */
7291 /* First, skip manually a ')' or ']'. This is because they
7292 reduce the nesting level, so c_parser_skip_until_found()
7293 wouldn't be able to skip past them. */
7294 c_token *token = c_parser_peek_token (parser);
7295 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7296 c_parser_consume_token (parser);
7298 /* Then, do the standard skipping. */
7299 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7301 /* We hopefully recovered. Start normal parsing again. */
7302 parser->error = false;
7303 continue;
7305 else
7307 /* Comma-separated instance variables are chained together
7308 in reverse order; add them one by one. */
7309 tree ivar = nreverse (decls);
7310 for (; ivar; ivar = DECL_CHAIN (ivar))
7311 objc_add_instance_variable (copy_node (ivar));
7313 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7317 /* Parse an objc-class-declaration.
7319 objc-class-declaration:
7320 @class identifier-list ;
7323 static void
7324 c_parser_objc_class_declaration (c_parser *parser)
7326 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7327 c_parser_consume_token (parser);
7328 /* Any identifiers, including those declared as type names, are OK
7329 here. */
7330 while (true)
7332 tree id;
7333 if (c_parser_next_token_is_not (parser, CPP_NAME))
7335 c_parser_error (parser, "expected identifier");
7336 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7337 parser->error = false;
7338 return;
7340 id = c_parser_peek_token (parser)->value;
7341 objc_declare_class (id);
7342 c_parser_consume_token (parser);
7343 if (c_parser_next_token_is (parser, CPP_COMMA))
7344 c_parser_consume_token (parser);
7345 else
7346 break;
7348 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7351 /* Parse an objc-alias-declaration.
7353 objc-alias-declaration:
7354 @compatibility_alias identifier identifier ;
7357 static void
7358 c_parser_objc_alias_declaration (c_parser *parser)
7360 tree id1, id2;
7361 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7362 c_parser_consume_token (parser);
7363 if (c_parser_next_token_is_not (parser, CPP_NAME))
7365 c_parser_error (parser, "expected identifier");
7366 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7367 return;
7369 id1 = c_parser_peek_token (parser)->value;
7370 c_parser_consume_token (parser);
7371 if (c_parser_next_token_is_not (parser, CPP_NAME))
7373 c_parser_error (parser, "expected identifier");
7374 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7375 return;
7377 id2 = c_parser_peek_token (parser)->value;
7378 c_parser_consume_token (parser);
7379 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7380 objc_declare_alias (id1, id2);
7383 /* Parse an objc-protocol-definition.
7385 objc-protocol-definition:
7386 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7387 @protocol identifier-list ;
7389 "@protocol identifier ;" should be resolved as "@protocol
7390 identifier-list ;": objc-methodprotolist may not start with a
7391 semicolon in the first alternative if objc-protocol-refs are
7392 omitted. */
7394 static void
7395 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7397 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7399 c_parser_consume_token (parser);
7400 if (c_parser_next_token_is_not (parser, CPP_NAME))
7402 c_parser_error (parser, "expected identifier");
7403 return;
7405 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7406 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7408 /* Any identifiers, including those declared as type names, are
7409 OK here. */
7410 while (true)
7412 tree id;
7413 if (c_parser_next_token_is_not (parser, CPP_NAME))
7415 c_parser_error (parser, "expected identifier");
7416 break;
7418 id = c_parser_peek_token (parser)->value;
7419 objc_declare_protocol (id, attributes);
7420 c_parser_consume_token (parser);
7421 if (c_parser_next_token_is (parser, CPP_COMMA))
7422 c_parser_consume_token (parser);
7423 else
7424 break;
7426 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7428 else
7430 tree id = c_parser_peek_token (parser)->value;
7431 tree proto = NULL_TREE;
7432 c_parser_consume_token (parser);
7433 if (c_parser_next_token_is (parser, CPP_LESS))
7434 proto = c_parser_objc_protocol_refs (parser);
7435 parser->objc_pq_context = true;
7436 objc_start_protocol (id, proto, attributes);
7437 c_parser_objc_methodprotolist (parser);
7438 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7439 parser->objc_pq_context = false;
7440 objc_finish_interface ();
7444 /* Parse an objc-method-type.
7446 objc-method-type:
7450 Return true if it is a class method (+) and false if it is
7451 an instance method (-).
7453 static inline bool
7454 c_parser_objc_method_type (c_parser *parser)
7456 switch (c_parser_peek_token (parser)->type)
7458 case CPP_PLUS:
7459 c_parser_consume_token (parser);
7460 return true;
7461 case CPP_MINUS:
7462 c_parser_consume_token (parser);
7463 return false;
7464 default:
7465 gcc_unreachable ();
7469 /* Parse an objc-method-definition.
7471 objc-method-definition:
7472 objc-method-type objc-method-decl ;[opt] compound-statement
7475 static void
7476 c_parser_objc_method_definition (c_parser *parser)
7478 bool is_class_method = c_parser_objc_method_type (parser);
7479 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7480 parser->objc_pq_context = true;
7481 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7482 &expr);
7483 if (decl == error_mark_node)
7484 return; /* Bail here. */
7486 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7488 c_parser_consume_token (parser);
7489 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7490 "extra semicolon in method definition specified");
7493 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7495 c_parser_error (parser, "expected %<{%>");
7496 return;
7499 parser->objc_pq_context = false;
7500 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7502 add_stmt (c_parser_compound_statement (parser));
7503 objc_finish_method_definition (current_function_decl);
7505 else
7507 /* This code is executed when we find a method definition
7508 outside of an @implementation context (or invalid for other
7509 reasons). Parse the method (to keep going) but do not emit
7510 any code.
7512 c_parser_compound_statement (parser);
7516 /* Parse an objc-methodprotolist.
7518 objc-methodprotolist:
7519 empty
7520 objc-methodprotolist objc-methodproto
7521 objc-methodprotolist declaration
7522 objc-methodprotolist ;
7523 @optional
7524 @required
7526 The declaration is a data definition, which may be missing
7527 declaration specifiers under the same rules and diagnostics as
7528 other data definitions outside functions, and the stray semicolon
7529 is diagnosed the same way as a stray semicolon outside a
7530 function. */
7532 static void
7533 c_parser_objc_methodprotolist (c_parser *parser)
7535 while (true)
7537 /* The list is terminated by @end. */
7538 switch (c_parser_peek_token (parser)->type)
7540 case CPP_SEMICOLON:
7541 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7542 "ISO C does not allow extra %<;%> outside of a function");
7543 c_parser_consume_token (parser);
7544 break;
7545 case CPP_PLUS:
7546 case CPP_MINUS:
7547 c_parser_objc_methodproto (parser);
7548 break;
7549 case CPP_PRAGMA:
7550 c_parser_pragma (parser, pragma_external);
7551 break;
7552 case CPP_EOF:
7553 return;
7554 default:
7555 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7556 return;
7557 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7558 c_parser_objc_at_property_declaration (parser);
7559 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7561 objc_set_method_opt (true);
7562 c_parser_consume_token (parser);
7564 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7566 objc_set_method_opt (false);
7567 c_parser_consume_token (parser);
7569 else
7570 c_parser_declaration_or_fndef (parser, false, false, true,
7571 false, true, NULL);
7572 break;
7577 /* Parse an objc-methodproto.
7579 objc-methodproto:
7580 objc-method-type objc-method-decl ;
7583 static void
7584 c_parser_objc_methodproto (c_parser *parser)
7586 bool is_class_method = c_parser_objc_method_type (parser);
7587 tree decl, attributes = NULL_TREE;
7589 /* Remember protocol qualifiers in prototypes. */
7590 parser->objc_pq_context = true;
7591 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7592 NULL);
7593 /* Forget protocol qualifiers now. */
7594 parser->objc_pq_context = false;
7596 /* Do not allow the presence of attributes to hide an erroneous
7597 method implementation in the interface section. */
7598 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7600 c_parser_error (parser, "expected %<;%>");
7601 return;
7604 if (decl != error_mark_node)
7605 objc_add_method_declaration (is_class_method, decl, attributes);
7607 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7610 /* If we are at a position that method attributes may be present, check that
7611 there are not any parsed already (a syntax error) and then collect any
7612 specified at the current location. Finally, if new attributes were present,
7613 check that the next token is legal ( ';' for decls and '{' for defs). */
7615 static bool
7616 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7618 bool bad = false;
7619 if (*attributes)
7621 c_parser_error (parser,
7622 "method attributes must be specified at the end only");
7623 *attributes = NULL_TREE;
7624 bad = true;
7627 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7628 *attributes = c_parser_attributes (parser);
7630 /* If there were no attributes here, just report any earlier error. */
7631 if (*attributes == NULL_TREE || bad)
7632 return bad;
7634 /* If the attributes are followed by a ; or {, then just report any earlier
7635 error. */
7636 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7637 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7638 return bad;
7640 /* We've got attributes, but not at the end. */
7641 c_parser_error (parser,
7642 "expected %<;%> or %<{%> after method attribute definition");
7643 return true;
7646 /* Parse an objc-method-decl.
7648 objc-method-decl:
7649 ( objc-type-name ) objc-selector
7650 objc-selector
7651 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7652 objc-keyword-selector objc-optparmlist
7653 attributes
7655 objc-keyword-selector:
7656 objc-keyword-decl
7657 objc-keyword-selector objc-keyword-decl
7659 objc-keyword-decl:
7660 objc-selector : ( objc-type-name ) identifier
7661 objc-selector : identifier
7662 : ( objc-type-name ) identifier
7663 : identifier
7665 objc-optparmlist:
7666 objc-optparms objc-optellipsis
7668 objc-optparms:
7669 empty
7670 objc-opt-parms , parameter-declaration
7672 objc-optellipsis:
7673 empty
7674 , ...
7677 static tree
7678 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7679 tree *attributes, tree *expr)
7681 tree type = NULL_TREE;
7682 tree sel;
7683 tree parms = NULL_TREE;
7684 bool ellipsis = false;
7685 bool attr_err = false;
7687 *attributes = NULL_TREE;
7688 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7690 c_parser_consume_token (parser);
7691 type = c_parser_objc_type_name (parser);
7692 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7694 sel = c_parser_objc_selector (parser);
7695 /* If there is no selector, or a colon follows, we have an
7696 objc-keyword-selector. If there is a selector, and a colon does
7697 not follow, that selector ends the objc-method-decl. */
7698 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7700 tree tsel = sel;
7701 tree list = NULL_TREE;
7702 while (true)
7704 tree atype = NULL_TREE, id, keyworddecl;
7705 tree param_attr = NULL_TREE;
7706 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7707 break;
7708 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7710 c_parser_consume_token (parser);
7711 atype = c_parser_objc_type_name (parser);
7712 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7713 "expected %<)%>");
7715 /* New ObjC allows attributes on method parameters. */
7716 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7717 param_attr = c_parser_attributes (parser);
7718 if (c_parser_next_token_is_not (parser, CPP_NAME))
7720 c_parser_error (parser, "expected identifier");
7721 return error_mark_node;
7723 id = c_parser_peek_token (parser)->value;
7724 c_parser_consume_token (parser);
7725 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7726 list = chainon (list, keyworddecl);
7727 tsel = c_parser_objc_selector (parser);
7728 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7729 break;
7732 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7734 /* Parse the optional parameter list. Optional Objective-C
7735 method parameters follow the C syntax, and may include '...'
7736 to denote a variable number of arguments. */
7737 parms = make_node (TREE_LIST);
7738 while (c_parser_next_token_is (parser, CPP_COMMA))
7740 struct c_parm *parm;
7741 c_parser_consume_token (parser);
7742 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7744 ellipsis = true;
7745 c_parser_consume_token (parser);
7746 attr_err |= c_parser_objc_maybe_method_attributes
7747 (parser, attributes) ;
7748 break;
7750 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7751 if (parm == NULL)
7752 break;
7753 parms = chainon (parms,
7754 build_tree_list (NULL_TREE, grokparm (parm, expr)));
7756 sel = list;
7758 else
7759 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7761 if (sel == NULL)
7763 c_parser_error (parser, "objective-c method declaration is expected");
7764 return error_mark_node;
7767 if (attr_err)
7768 return error_mark_node;
7770 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7773 /* Parse an objc-type-name.
7775 objc-type-name:
7776 objc-type-qualifiers[opt] type-name
7777 objc-type-qualifiers[opt]
7779 objc-type-qualifiers:
7780 objc-type-qualifier
7781 objc-type-qualifiers objc-type-qualifier
7783 objc-type-qualifier: one of
7784 in out inout bycopy byref oneway
7787 static tree
7788 c_parser_objc_type_name (c_parser *parser)
7790 tree quals = NULL_TREE;
7791 struct c_type_name *type_name = NULL;
7792 tree type = NULL_TREE;
7793 while (true)
7795 c_token *token = c_parser_peek_token (parser);
7796 if (token->type == CPP_KEYWORD
7797 && (token->keyword == RID_IN
7798 || token->keyword == RID_OUT
7799 || token->keyword == RID_INOUT
7800 || token->keyword == RID_BYCOPY
7801 || token->keyword == RID_BYREF
7802 || token->keyword == RID_ONEWAY))
7804 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7805 c_parser_consume_token (parser);
7807 else
7808 break;
7810 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7811 type_name = c_parser_type_name (parser);
7812 if (type_name)
7813 type = groktypename (type_name, NULL, NULL);
7815 /* If the type is unknown, and error has already been produced and
7816 we need to recover from the error. In that case, use NULL_TREE
7817 for the type, as if no type had been specified; this will use the
7818 default type ('id') which is good for error recovery. */
7819 if (type == error_mark_node)
7820 type = NULL_TREE;
7822 return build_tree_list (quals, type);
7825 /* Parse objc-protocol-refs.
7827 objc-protocol-refs:
7828 < identifier-list >
7831 static tree
7832 c_parser_objc_protocol_refs (c_parser *parser)
7834 tree list = NULL_TREE;
7835 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7836 c_parser_consume_token (parser);
7837 /* Any identifiers, including those declared as type names, are OK
7838 here. */
7839 while (true)
7841 tree id;
7842 if (c_parser_next_token_is_not (parser, CPP_NAME))
7844 c_parser_error (parser, "expected identifier");
7845 break;
7847 id = c_parser_peek_token (parser)->value;
7848 list = chainon (list, build_tree_list (NULL_TREE, id));
7849 c_parser_consume_token (parser);
7850 if (c_parser_next_token_is (parser, CPP_COMMA))
7851 c_parser_consume_token (parser);
7852 else
7853 break;
7855 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7856 return list;
7859 /* Parse an objc-try-catch-finally-statement.
7861 objc-try-catch-finally-statement:
7862 @try compound-statement objc-catch-list[opt]
7863 @try compound-statement objc-catch-list[opt] @finally compound-statement
7865 objc-catch-list:
7866 @catch ( objc-catch-parameter-declaration ) compound-statement
7867 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7869 objc-catch-parameter-declaration:
7870 parameter-declaration
7871 '...'
7873 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7875 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7876 for C++. Keep them in sync. */
7878 static void
7879 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7881 location_t location;
7882 tree stmt;
7884 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7885 c_parser_consume_token (parser);
7886 location = c_parser_peek_token (parser)->location;
7887 objc_maybe_warn_exceptions (location);
7888 stmt = c_parser_compound_statement (parser);
7889 objc_begin_try_stmt (location, stmt);
7891 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7893 struct c_parm *parm;
7894 tree parameter_declaration = error_mark_node;
7895 bool seen_open_paren = false;
7897 c_parser_consume_token (parser);
7898 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7899 seen_open_paren = true;
7900 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7902 /* We have "@catch (...)" (where the '...' are literally
7903 what is in the code). Skip the '...'.
7904 parameter_declaration is set to NULL_TREE, and
7905 objc_being_catch_clauses() knows that that means
7906 '...'. */
7907 c_parser_consume_token (parser);
7908 parameter_declaration = NULL_TREE;
7910 else
7912 /* We have "@catch (NSException *exception)" or something
7913 like that. Parse the parameter declaration. */
7914 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7915 if (parm == NULL)
7916 parameter_declaration = error_mark_node;
7917 else
7918 parameter_declaration = grokparm (parm, NULL);
7920 if (seen_open_paren)
7921 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7922 else
7924 /* If there was no open parenthesis, we are recovering from
7925 an error, and we are trying to figure out what mistake
7926 the user has made. */
7928 /* If there is an immediate closing parenthesis, the user
7929 probably forgot the opening one (ie, they typed "@catch
7930 NSException *e)". Parse the closing parenthesis and keep
7931 going. */
7932 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7933 c_parser_consume_token (parser);
7935 /* If these is no immediate closing parenthesis, the user
7936 probably doesn't know that parenthesis are required at
7937 all (ie, they typed "@catch NSException *e"). So, just
7938 forget about the closing parenthesis and keep going. */
7940 objc_begin_catch_clause (parameter_declaration);
7941 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7942 c_parser_compound_statement_nostart (parser);
7943 objc_finish_catch_clause ();
7945 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7947 c_parser_consume_token (parser);
7948 location = c_parser_peek_token (parser)->location;
7949 stmt = c_parser_compound_statement (parser);
7950 objc_build_finally_clause (location, stmt);
7952 objc_finish_try_stmt ();
7955 /* Parse an objc-synchronized-statement.
7957 objc-synchronized-statement:
7958 @synchronized ( expression ) compound-statement
7961 static void
7962 c_parser_objc_synchronized_statement (c_parser *parser)
7964 location_t loc;
7965 tree expr, stmt;
7966 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7967 c_parser_consume_token (parser);
7968 loc = c_parser_peek_token (parser)->location;
7969 objc_maybe_warn_exceptions (loc);
7970 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7972 expr = c_parser_expression (parser).value;
7973 expr = c_fully_fold (expr, false, NULL);
7974 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7976 else
7977 expr = error_mark_node;
7978 stmt = c_parser_compound_statement (parser);
7979 objc_build_synchronized (loc, expr, stmt);
7982 /* Parse an objc-selector; return NULL_TREE without an error if the
7983 next token is not an objc-selector.
7985 objc-selector:
7986 identifier
7987 one of
7988 enum struct union if else while do for switch case default
7989 break continue return goto asm sizeof typeof __alignof
7990 unsigned long const short volatile signed restrict _Complex
7991 in out inout bycopy byref oneway int char float double void _Bool
7993 ??? Why this selection of keywords but not, for example, storage
7994 class specifiers? */
7996 static tree
7997 c_parser_objc_selector (c_parser *parser)
7999 c_token *token = c_parser_peek_token (parser);
8000 tree value = token->value;
8001 if (token->type == CPP_NAME)
8003 c_parser_consume_token (parser);
8004 return value;
8006 if (token->type != CPP_KEYWORD)
8007 return NULL_TREE;
8008 switch (token->keyword)
8010 case RID_ENUM:
8011 case RID_STRUCT:
8012 case RID_UNION:
8013 case RID_IF:
8014 case RID_ELSE:
8015 case RID_WHILE:
8016 case RID_DO:
8017 case RID_FOR:
8018 case RID_SWITCH:
8019 case RID_CASE:
8020 case RID_DEFAULT:
8021 case RID_BREAK:
8022 case RID_CONTINUE:
8023 case RID_RETURN:
8024 case RID_GOTO:
8025 case RID_ASM:
8026 case RID_SIZEOF:
8027 case RID_TYPEOF:
8028 case RID_ALIGNOF:
8029 case RID_UNSIGNED:
8030 case RID_LONG:
8031 case RID_INT128:
8032 case RID_CONST:
8033 case RID_SHORT:
8034 case RID_VOLATILE:
8035 case RID_SIGNED:
8036 case RID_RESTRICT:
8037 case RID_COMPLEX:
8038 case RID_IN:
8039 case RID_OUT:
8040 case RID_INOUT:
8041 case RID_BYCOPY:
8042 case RID_BYREF:
8043 case RID_ONEWAY:
8044 case RID_INT:
8045 case RID_CHAR:
8046 case RID_FLOAT:
8047 case RID_DOUBLE:
8048 case RID_VOID:
8049 case RID_BOOL:
8050 c_parser_consume_token (parser);
8051 return value;
8052 default:
8053 return NULL_TREE;
8057 /* Parse an objc-selector-arg.
8059 objc-selector-arg:
8060 objc-selector
8061 objc-keywordname-list
8063 objc-keywordname-list:
8064 objc-keywordname
8065 objc-keywordname-list objc-keywordname
8067 objc-keywordname:
8068 objc-selector :
8072 static tree
8073 c_parser_objc_selector_arg (c_parser *parser)
8075 tree sel = c_parser_objc_selector (parser);
8076 tree list = NULL_TREE;
8077 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8078 return sel;
8079 while (true)
8081 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8082 return list;
8083 list = chainon (list, build_tree_list (sel, NULL_TREE));
8084 sel = c_parser_objc_selector (parser);
8085 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8086 break;
8088 return list;
8091 /* Parse an objc-receiver.
8093 objc-receiver:
8094 expression
8095 class-name
8096 type-name
8099 static tree
8100 c_parser_objc_receiver (c_parser *parser)
8102 if (c_parser_peek_token (parser)->type == CPP_NAME
8103 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8104 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8106 tree id = c_parser_peek_token (parser)->value;
8107 c_parser_consume_token (parser);
8108 return objc_get_class_reference (id);
8110 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8113 /* Parse objc-message-args.
8115 objc-message-args:
8116 objc-selector
8117 objc-keywordarg-list
8119 objc-keywordarg-list:
8120 objc-keywordarg
8121 objc-keywordarg-list objc-keywordarg
8123 objc-keywordarg:
8124 objc-selector : objc-keywordexpr
8125 : objc-keywordexpr
8128 static tree
8129 c_parser_objc_message_args (c_parser *parser)
8131 tree sel = c_parser_objc_selector (parser);
8132 tree list = NULL_TREE;
8133 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8134 return sel;
8135 while (true)
8137 tree keywordexpr;
8138 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8139 return error_mark_node;
8140 keywordexpr = c_parser_objc_keywordexpr (parser);
8141 list = chainon (list, build_tree_list (sel, keywordexpr));
8142 sel = c_parser_objc_selector (parser);
8143 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8144 break;
8146 return list;
8149 /* Parse an objc-keywordexpr.
8151 objc-keywordexpr:
8152 nonempty-expr-list
8155 static tree
8156 c_parser_objc_keywordexpr (c_parser *parser)
8158 tree ret;
8159 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
8160 if (VEC_length (tree, expr_list) == 1)
8162 /* Just return the expression, remove a level of
8163 indirection. */
8164 ret = VEC_index (tree, expr_list, 0);
8166 else
8168 /* We have a comma expression, we will collapse later. */
8169 ret = build_tree_list_vec (expr_list);
8171 release_tree_vector (expr_list);
8172 return ret;
8175 /* A check, needed in several places, that ObjC interface, implementation or
8176 method definitions are not prefixed by incorrect items. */
8177 static bool
8178 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8179 struct c_declspecs *specs)
8181 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8182 || specs->typespec_kind != ctsk_none)
8184 c_parser_error (parser,
8185 "no type or storage class may be specified here,");
8186 c_parser_skip_to_end_of_block_or_statement (parser);
8187 return true;
8189 return false;
8192 /* Parse an Objective-C @property declaration. The syntax is:
8194 objc-property-declaration:
8195 '@property' objc-property-attributes[opt] struct-declaration ;
8197 objc-property-attributes:
8198 '(' objc-property-attribute-list ')'
8200 objc-property-attribute-list:
8201 objc-property-attribute
8202 objc-property-attribute-list, objc-property-attribute
8204 objc-property-attribute
8205 'getter' = identifier
8206 'setter' = identifier
8207 'readonly'
8208 'readwrite'
8209 'assign'
8210 'retain'
8211 'copy'
8212 'nonatomic'
8214 For example:
8215 @property NSString *name;
8216 @property (readonly) id object;
8217 @property (retain, nonatomic, getter=getTheName) id name;
8218 @property int a, b, c;
8220 PS: This function is identical to cp_parser_objc_at_propery_declaration
8221 for C++. Keep them in sync. */
8222 static void
8223 c_parser_objc_at_property_declaration (c_parser *parser)
8225 /* The following variables hold the attributes of the properties as
8226 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8227 seen. When we see an attribute, we set them to 'true' (if they
8228 are boolean properties) or to the identifier (if they have an
8229 argument, ie, for getter and setter). Note that here we only
8230 parse the list of attributes, check the syntax and accumulate the
8231 attributes that we find. objc_add_property_declaration() will
8232 then process the information. */
8233 bool property_assign = false;
8234 bool property_copy = false;
8235 tree property_getter_ident = NULL_TREE;
8236 bool property_nonatomic = false;
8237 bool property_readonly = false;
8238 bool property_readwrite = false;
8239 bool property_retain = false;
8240 tree property_setter_ident = NULL_TREE;
8242 /* 'properties' is the list of properties that we read. Usually a
8243 single one, but maybe more (eg, in "@property int a, b, c;" there
8244 are three). */
8245 tree properties;
8246 location_t loc;
8248 loc = c_parser_peek_token (parser)->location;
8249 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8251 c_parser_consume_token (parser); /* Eat '@property'. */
8253 /* Parse the optional attribute list... */
8254 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8256 /* Eat the '(' */
8257 c_parser_consume_token (parser);
8259 /* Property attribute keywords are valid now. */
8260 parser->objc_property_attr_context = true;
8262 while (true)
8264 bool syntax_error = false;
8265 c_token *token = c_parser_peek_token (parser);
8266 enum rid keyword;
8268 if (token->type != CPP_KEYWORD)
8270 if (token->type == CPP_CLOSE_PAREN)
8271 c_parser_error (parser, "expected identifier");
8272 else
8274 c_parser_consume_token (parser);
8275 c_parser_error (parser, "unknown property attribute");
8277 break;
8279 keyword = token->keyword;
8280 c_parser_consume_token (parser);
8281 switch (keyword)
8283 case RID_ASSIGN: property_assign = true; break;
8284 case RID_COPY: property_copy = true; break;
8285 case RID_NONATOMIC: property_nonatomic = true; break;
8286 case RID_READONLY: property_readonly = true; break;
8287 case RID_READWRITE: property_readwrite = true; break;
8288 case RID_RETAIN: property_retain = true; break;
8290 case RID_GETTER:
8291 case RID_SETTER:
8292 if (c_parser_next_token_is_not (parser, CPP_EQ))
8294 if (keyword == RID_GETTER)
8295 c_parser_error (parser,
8296 "missing %<=%> (after %<getter%> attribute)");
8297 else
8298 c_parser_error (parser,
8299 "missing %<=%> (after %<setter%> attribute)");
8300 syntax_error = true;
8301 break;
8303 c_parser_consume_token (parser); /* eat the = */
8304 if (c_parser_next_token_is_not (parser, CPP_NAME))
8306 c_parser_error (parser, "expected identifier");
8307 syntax_error = true;
8308 break;
8310 if (keyword == RID_SETTER)
8312 if (property_setter_ident != NULL_TREE)
8313 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8314 else
8315 property_setter_ident = c_parser_peek_token (parser)->value;
8316 c_parser_consume_token (parser);
8317 if (c_parser_next_token_is_not (parser, CPP_COLON))
8318 c_parser_error (parser, "setter name must terminate with %<:%>");
8319 else
8320 c_parser_consume_token (parser);
8322 else
8324 if (property_getter_ident != NULL_TREE)
8325 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8326 else
8327 property_getter_ident = c_parser_peek_token (parser)->value;
8328 c_parser_consume_token (parser);
8330 break;
8331 default:
8332 c_parser_error (parser, "unknown property attribute");
8333 syntax_error = true;
8334 break;
8337 if (syntax_error)
8338 break;
8340 if (c_parser_next_token_is (parser, CPP_COMMA))
8341 c_parser_consume_token (parser);
8342 else
8343 break;
8345 parser->objc_property_attr_context = false;
8346 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8348 /* ... and the property declaration(s). */
8349 properties = c_parser_struct_declaration (parser);
8351 if (properties == error_mark_node)
8353 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8354 parser->error = false;
8355 return;
8358 if (properties == NULL_TREE)
8359 c_parser_error (parser, "expected identifier");
8360 else
8362 /* Comma-separated properties are chained together in
8363 reverse order; add them one by one. */
8364 properties = nreverse (properties);
8366 for (; properties; properties = TREE_CHAIN (properties))
8367 objc_add_property_declaration (loc, copy_node (properties),
8368 property_readonly, property_readwrite,
8369 property_assign, property_retain,
8370 property_copy, property_nonatomic,
8371 property_getter_ident, property_setter_ident);
8374 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8375 parser->error = false;
8378 /* Parse an Objective-C @synthesize declaration. The syntax is:
8380 objc-synthesize-declaration:
8381 @synthesize objc-synthesize-identifier-list ;
8383 objc-synthesize-identifier-list:
8384 objc-synthesize-identifier
8385 objc-synthesize-identifier-list, objc-synthesize-identifier
8387 objc-synthesize-identifier
8388 identifier
8389 identifier = identifier
8391 For example:
8392 @synthesize MyProperty;
8393 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8395 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8396 for C++. Keep them in sync.
8398 static void
8399 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8401 tree list = NULL_TREE;
8402 location_t loc;
8403 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8404 loc = c_parser_peek_token (parser)->location;
8406 c_parser_consume_token (parser);
8407 while (true)
8409 tree property, ivar;
8410 if (c_parser_next_token_is_not (parser, CPP_NAME))
8412 c_parser_error (parser, "expected identifier");
8413 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8414 /* Once we find the semicolon, we can resume normal parsing.
8415 We have to reset parser->error manually because
8416 c_parser_skip_until_found() won't reset it for us if the
8417 next token is precisely a semicolon. */
8418 parser->error = false;
8419 return;
8421 property = c_parser_peek_token (parser)->value;
8422 c_parser_consume_token (parser);
8423 if (c_parser_next_token_is (parser, CPP_EQ))
8425 c_parser_consume_token (parser);
8426 if (c_parser_next_token_is_not (parser, CPP_NAME))
8428 c_parser_error (parser, "expected identifier");
8429 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8430 parser->error = false;
8431 return;
8433 ivar = c_parser_peek_token (parser)->value;
8434 c_parser_consume_token (parser);
8436 else
8437 ivar = NULL_TREE;
8438 list = chainon (list, build_tree_list (ivar, property));
8439 if (c_parser_next_token_is (parser, CPP_COMMA))
8440 c_parser_consume_token (parser);
8441 else
8442 break;
8444 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8445 objc_add_synthesize_declaration (loc, list);
8448 /* Parse an Objective-C @dynamic declaration. The syntax is:
8450 objc-dynamic-declaration:
8451 @dynamic identifier-list ;
8453 For example:
8454 @dynamic MyProperty;
8455 @dynamic MyProperty, AnotherProperty;
8457 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8458 for C++. Keep them in sync.
8460 static void
8461 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8463 tree list = NULL_TREE;
8464 location_t loc;
8465 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8466 loc = c_parser_peek_token (parser)->location;
8468 c_parser_consume_token (parser);
8469 while (true)
8471 tree property;
8472 if (c_parser_next_token_is_not (parser, CPP_NAME))
8474 c_parser_error (parser, "expected identifier");
8475 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8476 parser->error = false;
8477 return;
8479 property = c_parser_peek_token (parser)->value;
8480 list = chainon (list, build_tree_list (NULL_TREE, property));
8481 c_parser_consume_token (parser);
8482 if (c_parser_next_token_is (parser, CPP_COMMA))
8483 c_parser_consume_token (parser);
8484 else
8485 break;
8487 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8488 objc_add_dynamic_declaration (loc, list);
8492 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8493 should be considered, statements. ALLOW_STMT is true if we're within
8494 the context of a function and such pragmas are to be allowed. Returns
8495 true if we actually parsed such a pragma. */
8497 static bool
8498 c_parser_pragma (c_parser *parser, enum pragma_context context)
8500 unsigned int id;
8502 id = c_parser_peek_token (parser)->pragma_kind;
8503 gcc_assert (id != PRAGMA_NONE);
8505 switch (id)
8507 case PRAGMA_OMP_BARRIER:
8508 if (context != pragma_compound)
8510 if (context == pragma_stmt)
8511 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8512 "used in compound statements");
8513 goto bad_stmt;
8515 c_parser_omp_barrier (parser);
8516 return false;
8518 case PRAGMA_OMP_FLUSH:
8519 if (context != pragma_compound)
8521 if (context == pragma_stmt)
8522 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8523 "used in compound statements");
8524 goto bad_stmt;
8526 c_parser_omp_flush (parser);
8527 return false;
8529 case PRAGMA_OMP_TASKWAIT:
8530 if (context != pragma_compound)
8532 if (context == pragma_stmt)
8533 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8534 "used in compound statements");
8535 goto bad_stmt;
8537 c_parser_omp_taskwait (parser);
8538 return false;
8540 case PRAGMA_OMP_TASKYIELD:
8541 if (context != pragma_compound)
8543 if (context == pragma_stmt)
8544 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
8545 "used in compound statements");
8546 goto bad_stmt;
8548 c_parser_omp_taskyield (parser);
8549 return false;
8551 case PRAGMA_OMP_THREADPRIVATE:
8552 c_parser_omp_threadprivate (parser);
8553 return false;
8555 case PRAGMA_OMP_SECTION:
8556 error_at (c_parser_peek_token (parser)->location,
8557 "%<#pragma omp section%> may only be used in "
8558 "%<#pragma omp sections%> construct");
8559 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8560 return false;
8562 case PRAGMA_GCC_PCH_PREPROCESS:
8563 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8564 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8565 return false;
8567 default:
8568 if (id < PRAGMA_FIRST_EXTERNAL)
8570 if (context == pragma_external)
8572 bad_stmt:
8573 c_parser_error (parser, "expected declaration specifiers");
8574 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8575 return false;
8577 c_parser_omp_construct (parser);
8578 return true;
8580 break;
8583 c_parser_consume_pragma (parser);
8584 c_invoke_pragma_handler (id);
8586 /* Skip to EOL, but suppress any error message. Those will have been
8587 generated by the handler routine through calling error, as opposed
8588 to calling c_parser_error. */
8589 parser->error = true;
8590 c_parser_skip_to_pragma_eol (parser);
8592 return false;
8595 /* The interface the pragma parsers have to the lexer. */
8597 enum cpp_ttype
8598 pragma_lex (tree *value)
8600 c_token *tok = c_parser_peek_token (the_parser);
8601 enum cpp_ttype ret = tok->type;
8603 *value = tok->value;
8604 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8605 ret = CPP_EOF;
8606 else
8608 if (ret == CPP_KEYWORD)
8609 ret = CPP_NAME;
8610 c_parser_consume_token (the_parser);
8613 return ret;
8616 static void
8617 c_parser_pragma_pch_preprocess (c_parser *parser)
8619 tree name = NULL;
8621 c_parser_consume_pragma (parser);
8622 if (c_parser_next_token_is (parser, CPP_STRING))
8624 name = c_parser_peek_token (parser)->value;
8625 c_parser_consume_token (parser);
8627 else
8628 c_parser_error (parser, "expected string literal");
8629 c_parser_skip_to_pragma_eol (parser);
8631 if (name)
8632 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8635 /* OpenMP 2.5 parsing routines. */
8637 /* Returns name of the next clause.
8638 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8639 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8640 returned and the token is consumed. */
8642 static pragma_omp_clause
8643 c_parser_omp_clause_name (c_parser *parser)
8645 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8647 if (c_parser_next_token_is_keyword (parser, RID_IF))
8648 result = PRAGMA_OMP_CLAUSE_IF;
8649 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8650 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8651 else if (c_parser_next_token_is (parser, CPP_NAME))
8653 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8655 switch (p[0])
8657 case 'c':
8658 if (!strcmp ("collapse", p))
8659 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8660 else if (!strcmp ("copyin", p))
8661 result = PRAGMA_OMP_CLAUSE_COPYIN;
8662 else if (!strcmp ("copyprivate", p))
8663 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8664 break;
8665 case 'f':
8666 if (!strcmp ("final", p))
8667 result = PRAGMA_OMP_CLAUSE_FINAL;
8668 else if (!strcmp ("firstprivate", p))
8669 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8670 break;
8671 case 'l':
8672 if (!strcmp ("lastprivate", p))
8673 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8674 break;
8675 case 'm':
8676 if (!strcmp ("mergeable", p))
8677 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
8678 break;
8679 case 'n':
8680 if (!strcmp ("nowait", p))
8681 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8682 else if (!strcmp ("num_threads", p))
8683 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8684 break;
8685 case 'o':
8686 if (!strcmp ("ordered", p))
8687 result = PRAGMA_OMP_CLAUSE_ORDERED;
8688 break;
8689 case 'p':
8690 if (!strcmp ("private", p))
8691 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8692 break;
8693 case 'r':
8694 if (!strcmp ("reduction", p))
8695 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8696 break;
8697 case 's':
8698 if (!strcmp ("schedule", p))
8699 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8700 else if (!strcmp ("shared", p))
8701 result = PRAGMA_OMP_CLAUSE_SHARED;
8702 break;
8703 case 'u':
8704 if (!strcmp ("untied", p))
8705 result = PRAGMA_OMP_CLAUSE_UNTIED;
8706 break;
8710 if (result != PRAGMA_OMP_CLAUSE_NONE)
8711 c_parser_consume_token (parser);
8713 return result;
8716 /* Validate that a clause of the given type does not already exist. */
8718 static void
8719 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8720 const char *name)
8722 tree c;
8724 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8725 if (OMP_CLAUSE_CODE (c) == code)
8727 location_t loc = OMP_CLAUSE_LOCATION (c);
8728 error_at (loc, "too many %qs clauses", name);
8729 break;
8733 /* OpenMP 2.5:
8734 variable-list:
8735 identifier
8736 variable-list , identifier
8738 If KIND is nonzero, create the appropriate node and install the
8739 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8740 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8742 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8743 return the list created. */
8745 static tree
8746 c_parser_omp_variable_list (c_parser *parser,
8747 location_t clause_loc,
8748 enum omp_clause_code kind,
8749 tree list)
8751 if (c_parser_next_token_is_not (parser, CPP_NAME)
8752 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8753 c_parser_error (parser, "expected identifier");
8755 while (c_parser_next_token_is (parser, CPP_NAME)
8756 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8758 tree t = lookup_name (c_parser_peek_token (parser)->value);
8760 if (t == NULL_TREE)
8761 undeclared_variable (c_parser_peek_token (parser)->location,
8762 c_parser_peek_token (parser)->value);
8763 else if (t == error_mark_node)
8765 else if (kind != 0)
8767 tree u = build_omp_clause (clause_loc, kind);
8768 OMP_CLAUSE_DECL (u) = t;
8769 OMP_CLAUSE_CHAIN (u) = list;
8770 list = u;
8772 else
8773 list = tree_cons (t, NULL_TREE, list);
8775 c_parser_consume_token (parser);
8777 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8778 break;
8780 c_parser_consume_token (parser);
8783 return list;
8786 /* Similarly, but expect leading and trailing parenthesis. This is a very
8787 common case for omp clauses. */
8789 static tree
8790 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8791 tree list)
8793 /* The clauses location. */
8794 location_t loc = c_parser_peek_token (parser)->location;
8796 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8798 list = c_parser_omp_variable_list (parser, loc, kind, list);
8799 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8801 return list;
8804 /* OpenMP 3.0:
8805 collapse ( constant-expression ) */
8807 static tree
8808 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8810 tree c, num = error_mark_node;
8811 HOST_WIDE_INT n;
8812 location_t loc;
8814 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8816 loc = c_parser_peek_token (parser)->location;
8817 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8819 num = c_parser_expr_no_commas (parser, NULL).value;
8820 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8822 if (num == error_mark_node)
8823 return list;
8824 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8825 || !host_integerp (num, 0)
8826 || (n = tree_low_cst (num, 0)) <= 0
8827 || (int) n != n)
8829 error_at (loc,
8830 "collapse argument needs positive constant integer expression");
8831 return list;
8833 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8834 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8835 OMP_CLAUSE_CHAIN (c) = list;
8836 return c;
8839 /* OpenMP 2.5:
8840 copyin ( variable-list ) */
8842 static tree
8843 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8845 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8848 /* OpenMP 2.5:
8849 copyprivate ( variable-list ) */
8851 static tree
8852 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8854 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8857 /* OpenMP 2.5:
8858 default ( shared | none ) */
8860 static tree
8861 c_parser_omp_clause_default (c_parser *parser, tree list)
8863 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8864 location_t loc = c_parser_peek_token (parser)->location;
8865 tree c;
8867 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8868 return list;
8869 if (c_parser_next_token_is (parser, CPP_NAME))
8871 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8873 switch (p[0])
8875 case 'n':
8876 if (strcmp ("none", p) != 0)
8877 goto invalid_kind;
8878 kind = OMP_CLAUSE_DEFAULT_NONE;
8879 break;
8881 case 's':
8882 if (strcmp ("shared", p) != 0)
8883 goto invalid_kind;
8884 kind = OMP_CLAUSE_DEFAULT_SHARED;
8885 break;
8887 default:
8888 goto invalid_kind;
8891 c_parser_consume_token (parser);
8893 else
8895 invalid_kind:
8896 c_parser_error (parser, "expected %<none%> or %<shared%>");
8898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8900 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8901 return list;
8903 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8904 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8905 OMP_CLAUSE_CHAIN (c) = list;
8906 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8908 return c;
8911 /* OpenMP 2.5:
8912 firstprivate ( variable-list ) */
8914 static tree
8915 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8917 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8920 /* OpenMP 3.1:
8921 final ( expression ) */
8923 static tree
8924 c_parser_omp_clause_final (c_parser *parser, tree list)
8926 location_t loc = c_parser_peek_token (parser)->location;
8927 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8929 tree t = c_parser_paren_condition (parser);
8930 tree c;
8932 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
8934 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
8935 OMP_CLAUSE_FINAL_EXPR (c) = t;
8936 OMP_CLAUSE_CHAIN (c) = list;
8937 list = c;
8939 else
8940 c_parser_error (parser, "expected %<(%>");
8942 return list;
8945 /* OpenMP 2.5:
8946 if ( expression ) */
8948 static tree
8949 c_parser_omp_clause_if (c_parser *parser, tree list)
8951 location_t loc = c_parser_peek_token (parser)->location;
8952 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8954 tree t = c_parser_paren_condition (parser);
8955 tree c;
8957 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8959 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8960 OMP_CLAUSE_IF_EXPR (c) = t;
8961 OMP_CLAUSE_CHAIN (c) = list;
8962 list = c;
8964 else
8965 c_parser_error (parser, "expected %<(%>");
8967 return list;
8970 /* OpenMP 2.5:
8971 lastprivate ( variable-list ) */
8973 static tree
8974 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8976 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8979 /* OpenMP 3.1:
8980 mergeable */
8982 static tree
8983 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8985 tree c;
8987 /* FIXME: Should we allow duplicates? */
8988 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
8990 c = build_omp_clause (c_parser_peek_token (parser)->location,
8991 OMP_CLAUSE_MERGEABLE);
8992 OMP_CLAUSE_CHAIN (c) = list;
8994 return c;
8997 /* OpenMP 2.5:
8998 nowait */
9000 static tree
9001 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9003 tree c;
9004 location_t loc = c_parser_peek_token (parser)->location;
9006 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9008 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9009 OMP_CLAUSE_CHAIN (c) = list;
9010 return c;
9013 /* OpenMP 2.5:
9014 num_threads ( expression ) */
9016 static tree
9017 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9019 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9020 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9022 location_t expr_loc = c_parser_peek_token (parser)->location;
9023 tree c, t = c_parser_expression (parser).value;
9024 mark_exp_read (t);
9025 t = c_fully_fold (t, false, NULL);
9027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9029 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9031 c_parser_error (parser, "expected integer expression");
9032 return list;
9035 /* Attempt to statically determine when the number isn't positive. */
9036 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9037 build_int_cst (TREE_TYPE (t), 0));
9038 if (CAN_HAVE_LOCATION_P (c))
9039 SET_EXPR_LOCATION (c, expr_loc);
9040 if (c == boolean_true_node)
9042 warning_at (expr_loc, 0,
9043 "%<num_threads%> value must be positive");
9044 t = integer_one_node;
9047 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9049 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9050 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9051 OMP_CLAUSE_CHAIN (c) = list;
9052 list = c;
9055 return list;
9058 /* OpenMP 2.5:
9059 ordered */
9061 static tree
9062 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9064 tree c;
9066 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9068 c = build_omp_clause (c_parser_peek_token (parser)->location,
9069 OMP_CLAUSE_ORDERED);
9070 OMP_CLAUSE_CHAIN (c) = list;
9072 return c;
9075 /* OpenMP 2.5:
9076 private ( variable-list ) */
9078 static tree
9079 c_parser_omp_clause_private (c_parser *parser, tree list)
9081 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9084 /* OpenMP 2.5:
9085 reduction ( reduction-operator : variable-list )
9087 reduction-operator:
9088 One of: + * - & ^ | && ||
9090 OpenMP 3.1:
9092 reduction-operator:
9093 One of: + * - & ^ | && || max min */
9095 static tree
9096 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9098 location_t clause_loc = c_parser_peek_token (parser)->location;
9099 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9101 enum tree_code code;
9103 switch (c_parser_peek_token (parser)->type)
9105 case CPP_PLUS:
9106 code = PLUS_EXPR;
9107 break;
9108 case CPP_MULT:
9109 code = MULT_EXPR;
9110 break;
9111 case CPP_MINUS:
9112 code = MINUS_EXPR;
9113 break;
9114 case CPP_AND:
9115 code = BIT_AND_EXPR;
9116 break;
9117 case CPP_XOR:
9118 code = BIT_XOR_EXPR;
9119 break;
9120 case CPP_OR:
9121 code = BIT_IOR_EXPR;
9122 break;
9123 case CPP_AND_AND:
9124 code = TRUTH_ANDIF_EXPR;
9125 break;
9126 case CPP_OR_OR:
9127 code = TRUTH_ORIF_EXPR;
9128 break;
9129 case CPP_NAME:
9131 const char *p
9132 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9133 if (strcmp (p, "min") == 0)
9135 code = MIN_EXPR;
9136 break;
9138 if (strcmp (p, "max") == 0)
9140 code = MAX_EXPR;
9141 break;
9144 /* FALLTHRU */
9145 default:
9146 c_parser_error (parser,
9147 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9148 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9150 return list;
9152 c_parser_consume_token (parser);
9153 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9155 tree nl, c;
9157 nl = c_parser_omp_variable_list (parser, clause_loc,
9158 OMP_CLAUSE_REDUCTION, list);
9159 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9160 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9162 list = nl;
9164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9166 return list;
9169 /* OpenMP 2.5:
9170 schedule ( schedule-kind )
9171 schedule ( schedule-kind , expression )
9173 schedule-kind:
9174 static | dynamic | guided | runtime | auto
9177 static tree
9178 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9180 tree c, t;
9181 location_t loc = c_parser_peek_token (parser)->location;
9183 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9184 return list;
9186 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9188 if (c_parser_next_token_is (parser, CPP_NAME))
9190 tree kind = c_parser_peek_token (parser)->value;
9191 const char *p = IDENTIFIER_POINTER (kind);
9193 switch (p[0])
9195 case 'd':
9196 if (strcmp ("dynamic", p) != 0)
9197 goto invalid_kind;
9198 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9199 break;
9201 case 'g':
9202 if (strcmp ("guided", p) != 0)
9203 goto invalid_kind;
9204 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9205 break;
9207 case 'r':
9208 if (strcmp ("runtime", p) != 0)
9209 goto invalid_kind;
9210 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9211 break;
9213 default:
9214 goto invalid_kind;
9217 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9218 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9219 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9220 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9221 else
9222 goto invalid_kind;
9224 c_parser_consume_token (parser);
9225 if (c_parser_next_token_is (parser, CPP_COMMA))
9227 location_t here;
9228 c_parser_consume_token (parser);
9230 here = c_parser_peek_token (parser)->location;
9231 t = c_parser_expr_no_commas (parser, NULL).value;
9232 mark_exp_read (t);
9233 t = c_fully_fold (t, false, NULL);
9235 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9236 error_at (here, "schedule %<runtime%> does not take "
9237 "a %<chunk_size%> parameter");
9238 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9239 error_at (here,
9240 "schedule %<auto%> does not take "
9241 "a %<chunk_size%> parameter");
9242 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9243 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9244 else
9245 c_parser_error (parser, "expected integer expression");
9247 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9249 else
9250 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9251 "expected %<,%> or %<)%>");
9253 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9254 OMP_CLAUSE_CHAIN (c) = list;
9255 return c;
9257 invalid_kind:
9258 c_parser_error (parser, "invalid schedule kind");
9259 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9260 return list;
9263 /* OpenMP 2.5:
9264 shared ( variable-list ) */
9266 static tree
9267 c_parser_omp_clause_shared (c_parser *parser, tree list)
9269 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9272 /* OpenMP 3.0:
9273 untied */
9275 static tree
9276 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9278 tree c;
9280 /* FIXME: Should we allow duplicates? */
9281 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9283 c = build_omp_clause (c_parser_peek_token (parser)->location,
9284 OMP_CLAUSE_UNTIED);
9285 OMP_CLAUSE_CHAIN (c) = list;
9287 return c;
9290 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9291 is a bitmask in MASK. Return the list of clauses found; the result
9292 of clause default goes in *pdefault. */
9294 static tree
9295 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9296 const char *where)
9298 tree clauses = NULL;
9299 bool first = true;
9301 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9303 location_t here;
9304 pragma_omp_clause c_kind;
9305 const char *c_name;
9306 tree prev = clauses;
9308 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9309 c_parser_consume_token (parser);
9311 first = false;
9312 here = c_parser_peek_token (parser)->location;
9313 c_kind = c_parser_omp_clause_name (parser);
9315 switch (c_kind)
9317 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9318 clauses = c_parser_omp_clause_collapse (parser, clauses);
9319 c_name = "collapse";
9320 break;
9321 case PRAGMA_OMP_CLAUSE_COPYIN:
9322 clauses = c_parser_omp_clause_copyin (parser, clauses);
9323 c_name = "copyin";
9324 break;
9325 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9326 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9327 c_name = "copyprivate";
9328 break;
9329 case PRAGMA_OMP_CLAUSE_DEFAULT:
9330 clauses = c_parser_omp_clause_default (parser, clauses);
9331 c_name = "default";
9332 break;
9333 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9334 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9335 c_name = "firstprivate";
9336 break;
9337 case PRAGMA_OMP_CLAUSE_FINAL:
9338 clauses = c_parser_omp_clause_final (parser, clauses);
9339 c_name = "final";
9340 break;
9341 case PRAGMA_OMP_CLAUSE_IF:
9342 clauses = c_parser_omp_clause_if (parser, clauses);
9343 c_name = "if";
9344 break;
9345 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9346 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9347 c_name = "lastprivate";
9348 break;
9349 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9350 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9351 c_name = "mergeable";
9352 break;
9353 case PRAGMA_OMP_CLAUSE_NOWAIT:
9354 clauses = c_parser_omp_clause_nowait (parser, clauses);
9355 c_name = "nowait";
9356 break;
9357 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9358 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9359 c_name = "num_threads";
9360 break;
9361 case PRAGMA_OMP_CLAUSE_ORDERED:
9362 clauses = c_parser_omp_clause_ordered (parser, clauses);
9363 c_name = "ordered";
9364 break;
9365 case PRAGMA_OMP_CLAUSE_PRIVATE:
9366 clauses = c_parser_omp_clause_private (parser, clauses);
9367 c_name = "private";
9368 break;
9369 case PRAGMA_OMP_CLAUSE_REDUCTION:
9370 clauses = c_parser_omp_clause_reduction (parser, clauses);
9371 c_name = "reduction";
9372 break;
9373 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9374 clauses = c_parser_omp_clause_schedule (parser, clauses);
9375 c_name = "schedule";
9376 break;
9377 case PRAGMA_OMP_CLAUSE_SHARED:
9378 clauses = c_parser_omp_clause_shared (parser, clauses);
9379 c_name = "shared";
9380 break;
9381 case PRAGMA_OMP_CLAUSE_UNTIED:
9382 clauses = c_parser_omp_clause_untied (parser, clauses);
9383 c_name = "untied";
9384 break;
9385 default:
9386 c_parser_error (parser, "expected %<#pragma omp%> clause");
9387 goto saw_error;
9390 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9392 /* Remove the invalid clause(s) from the list to avoid
9393 confusing the rest of the compiler. */
9394 clauses = prev;
9395 error_at (here, "%qs is not valid for %qs", c_name, where);
9399 saw_error:
9400 c_parser_skip_to_pragma_eol (parser);
9402 return c_finish_omp_clauses (clauses);
9405 /* OpenMP 2.5:
9406 structured-block:
9407 statement
9409 In practice, we're also interested in adding the statement to an
9410 outer node. So it is convenient if we work around the fact that
9411 c_parser_statement calls add_stmt. */
9413 static tree
9414 c_parser_omp_structured_block (c_parser *parser)
9416 tree stmt = push_stmt_list ();
9417 c_parser_statement (parser);
9418 return pop_stmt_list (stmt);
9421 /* OpenMP 2.5:
9422 # pragma omp atomic new-line
9423 expression-stmt
9425 expression-stmt:
9426 x binop= expr | x++ | ++x | x-- | --x
9427 binop:
9428 +, *, -, /, &, ^, |, <<, >>
9430 where x is an lvalue expression with scalar type.
9432 OpenMP 3.1:
9433 # pragma omp atomic new-line
9434 update-stmt
9436 # pragma omp atomic read new-line
9437 read-stmt
9439 # pragma omp atomic write new-line
9440 write-stmt
9442 # pragma omp atomic update new-line
9443 update-stmt
9445 # pragma omp atomic capture new-line
9446 capture-stmt
9448 # pragma omp atomic capture new-line
9449 capture-block
9451 read-stmt:
9452 v = x
9453 write-stmt:
9454 x = expr
9455 update-stmt:
9456 expression-stmt | x = x binop expr
9457 capture-stmt:
9458 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9459 capture-block:
9460 { v = x; update-stmt; } | { update-stmt; v = x; }
9462 where x and v are lvalue expressions with scalar type.
9464 LOC is the location of the #pragma token. */
9466 static void
9467 c_parser_omp_atomic (location_t loc, c_parser *parser)
9469 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9470 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9471 tree stmt, orig_lhs;
9472 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9473 struct c_expr rhs_expr;
9474 bool structured_block = false;
9476 if (c_parser_next_token_is (parser, CPP_NAME))
9478 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9480 if (!strcmp (p, "read"))
9481 code = OMP_ATOMIC_READ;
9482 else if (!strcmp (p, "write"))
9483 code = NOP_EXPR;
9484 else if (!strcmp (p, "update"))
9485 code = OMP_ATOMIC;
9486 else if (!strcmp (p, "capture"))
9487 code = OMP_ATOMIC_CAPTURE_NEW;
9488 else
9489 p = NULL;
9490 if (p)
9491 c_parser_consume_token (parser);
9493 c_parser_skip_to_pragma_eol (parser);
9495 switch (code)
9497 case OMP_ATOMIC_READ:
9498 case NOP_EXPR: /* atomic write */
9499 v = c_parser_unary_expression (parser).value;
9500 v = c_fully_fold (v, false, NULL);
9501 if (v == error_mark_node)
9502 goto saw_error;
9503 loc = c_parser_peek_token (parser)->location;
9504 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9505 goto saw_error;
9506 if (code == NOP_EXPR)
9507 lhs = c_parser_expression (parser).value;
9508 else
9509 lhs = c_parser_unary_expression (parser).value;
9510 lhs = c_fully_fold (lhs, false, NULL);
9511 if (lhs == error_mark_node)
9512 goto saw_error;
9513 if (code == NOP_EXPR)
9515 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
9516 opcode. */
9517 code = OMP_ATOMIC;
9518 rhs = lhs;
9519 lhs = v;
9520 v = NULL_TREE;
9522 goto done;
9523 case OMP_ATOMIC_CAPTURE_NEW:
9524 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9526 c_parser_consume_token (parser);
9527 structured_block = true;
9529 else
9531 v = c_parser_unary_expression (parser).value;
9532 v = c_fully_fold (v, false, NULL);
9533 if (v == error_mark_node)
9534 goto saw_error;
9535 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9536 goto saw_error;
9538 break;
9539 default:
9540 break;
9543 /* For structured_block case we don't know yet whether
9544 old or new x should be captured. */
9545 restart:
9546 lhs = c_parser_unary_expression (parser).value;
9547 lhs = c_fully_fold (lhs, false, NULL);
9548 orig_lhs = lhs;
9549 switch (TREE_CODE (lhs))
9551 case ERROR_MARK:
9552 saw_error:
9553 c_parser_skip_to_end_of_block_or_statement (parser);
9554 if (structured_block)
9556 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9557 c_parser_consume_token (parser);
9558 else if (code == OMP_ATOMIC_CAPTURE_NEW)
9560 c_parser_skip_to_end_of_block_or_statement (parser);
9561 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9562 c_parser_consume_token (parser);
9565 return;
9567 case POSTINCREMENT_EXPR:
9568 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9569 code = OMP_ATOMIC_CAPTURE_OLD;
9570 /* FALLTHROUGH */
9571 case PREINCREMENT_EXPR:
9572 lhs = TREE_OPERAND (lhs, 0);
9573 opcode = PLUS_EXPR;
9574 rhs = integer_one_node;
9575 break;
9577 case POSTDECREMENT_EXPR:
9578 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9579 code = OMP_ATOMIC_CAPTURE_OLD;
9580 /* FALLTHROUGH */
9581 case PREDECREMENT_EXPR:
9582 lhs = TREE_OPERAND (lhs, 0);
9583 opcode = MINUS_EXPR;
9584 rhs = integer_one_node;
9585 break;
9587 case COMPOUND_EXPR:
9588 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9589 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9590 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9591 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9592 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9593 (TREE_OPERAND (lhs, 1), 0), 0)))
9594 == BOOLEAN_TYPE)
9595 /* Undo effects of boolean_increment for post {in,de}crement. */
9596 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9597 /* FALLTHRU */
9598 case MODIFY_EXPR:
9599 if (TREE_CODE (lhs) == MODIFY_EXPR
9600 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9602 /* Undo effects of boolean_increment. */
9603 if (integer_onep (TREE_OPERAND (lhs, 1)))
9605 /* This is pre or post increment. */
9606 rhs = TREE_OPERAND (lhs, 1);
9607 lhs = TREE_OPERAND (lhs, 0);
9608 opcode = NOP_EXPR;
9609 if (code == OMP_ATOMIC_CAPTURE_NEW
9610 && !structured_block
9611 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9612 code = OMP_ATOMIC_CAPTURE_OLD;
9613 break;
9615 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9616 && TREE_OPERAND (lhs, 0)
9617 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9619 /* This is pre or post decrement. */
9620 rhs = TREE_OPERAND (lhs, 1);
9621 lhs = TREE_OPERAND (lhs, 0);
9622 opcode = NOP_EXPR;
9623 if (code == OMP_ATOMIC_CAPTURE_NEW
9624 && !structured_block
9625 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9626 code = OMP_ATOMIC_CAPTURE_OLD;
9627 break;
9630 /* FALLTHRU */
9631 default:
9632 switch (c_parser_peek_token (parser)->type)
9634 case CPP_MULT_EQ:
9635 opcode = MULT_EXPR;
9636 break;
9637 case CPP_DIV_EQ:
9638 opcode = TRUNC_DIV_EXPR;
9639 break;
9640 case CPP_PLUS_EQ:
9641 opcode = PLUS_EXPR;
9642 break;
9643 case CPP_MINUS_EQ:
9644 opcode = MINUS_EXPR;
9645 break;
9646 case CPP_LSHIFT_EQ:
9647 opcode = LSHIFT_EXPR;
9648 break;
9649 case CPP_RSHIFT_EQ:
9650 opcode = RSHIFT_EXPR;
9651 break;
9652 case CPP_AND_EQ:
9653 opcode = BIT_AND_EXPR;
9654 break;
9655 case CPP_OR_EQ:
9656 opcode = BIT_IOR_EXPR;
9657 break;
9658 case CPP_XOR_EQ:
9659 opcode = BIT_XOR_EXPR;
9660 break;
9661 case CPP_EQ:
9662 if (structured_block || code == OMP_ATOMIC)
9664 location_t aloc = c_parser_peek_token (parser)->location;
9665 location_t rhs_loc;
9666 enum c_parser_prec oprec = PREC_NONE;
9668 c_parser_consume_token (parser);
9669 rhs1 = c_parser_unary_expression (parser).value;
9670 rhs1 = c_fully_fold (rhs1, false, NULL);
9671 if (rhs1 == error_mark_node)
9672 goto saw_error;
9673 switch (c_parser_peek_token (parser)->type)
9675 case CPP_SEMICOLON:
9676 if (code == OMP_ATOMIC_CAPTURE_NEW)
9678 code = OMP_ATOMIC_CAPTURE_OLD;
9679 v = lhs;
9680 lhs = NULL_TREE;
9681 lhs1 = rhs1;
9682 rhs1 = NULL_TREE;
9683 c_parser_consume_token (parser);
9684 goto restart;
9686 c_parser_error (parser,
9687 "invalid form of %<#pragma omp atomic%>");
9688 goto saw_error;
9689 case CPP_MULT:
9690 opcode = MULT_EXPR;
9691 oprec = PREC_MULT;
9692 break;
9693 case CPP_DIV:
9694 opcode = TRUNC_DIV_EXPR;
9695 oprec = PREC_MULT;
9696 break;
9697 case CPP_PLUS:
9698 opcode = PLUS_EXPR;
9699 oprec = PREC_ADD;
9700 break;
9701 case CPP_MINUS:
9702 opcode = MINUS_EXPR;
9703 oprec = PREC_ADD;
9704 break;
9705 case CPP_LSHIFT:
9706 opcode = LSHIFT_EXPR;
9707 oprec = PREC_SHIFT;
9708 break;
9709 case CPP_RSHIFT:
9710 opcode = RSHIFT_EXPR;
9711 oprec = PREC_SHIFT;
9712 break;
9713 case CPP_AND:
9714 opcode = BIT_AND_EXPR;
9715 oprec = PREC_BITAND;
9716 break;
9717 case CPP_OR:
9718 opcode = BIT_IOR_EXPR;
9719 oprec = PREC_BITOR;
9720 break;
9721 case CPP_XOR:
9722 opcode = BIT_XOR_EXPR;
9723 oprec = PREC_BITXOR;
9724 break;
9725 default:
9726 c_parser_error (parser,
9727 "invalid operator for %<#pragma omp atomic%>");
9728 goto saw_error;
9730 loc = aloc;
9731 c_parser_consume_token (parser);
9732 rhs_loc = c_parser_peek_token (parser)->location;
9733 if (commutative_tree_code (opcode))
9734 oprec = (enum c_parser_prec) (oprec - 1);
9735 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
9736 rhs_expr = default_function_array_read_conversion (rhs_loc,
9737 rhs_expr);
9738 rhs = rhs_expr.value;
9739 rhs = c_fully_fold (rhs, false, NULL);
9740 goto stmt_done;
9742 /* FALLTHROUGH */
9743 default:
9744 c_parser_error (parser,
9745 "invalid operator for %<#pragma omp atomic%>");
9746 goto saw_error;
9749 /* Arrange to pass the location of the assignment operator to
9750 c_finish_omp_atomic. */
9751 loc = c_parser_peek_token (parser)->location;
9752 c_parser_consume_token (parser);
9754 location_t rhs_loc = c_parser_peek_token (parser)->location;
9755 rhs_expr = c_parser_expression (parser);
9756 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9758 rhs = rhs_expr.value;
9759 rhs = c_fully_fold (rhs, false, NULL);
9760 break;
9762 stmt_done:
9763 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
9765 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
9766 goto saw_error;
9767 v = c_parser_unary_expression (parser).value;
9768 v = c_fully_fold (v, false, NULL);
9769 if (v == error_mark_node)
9770 goto saw_error;
9771 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9772 goto saw_error;
9773 lhs1 = c_parser_unary_expression (parser).value;
9774 lhs1 = c_fully_fold (lhs1, false, NULL);
9775 if (lhs1 == error_mark_node)
9776 goto saw_error;
9778 if (structured_block)
9780 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9781 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
9783 done:
9784 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
9785 if (stmt != error_mark_node)
9786 add_stmt (stmt);
9788 if (!structured_block)
9789 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9793 /* OpenMP 2.5:
9794 # pragma omp barrier new-line
9797 static void
9798 c_parser_omp_barrier (c_parser *parser)
9800 location_t loc = c_parser_peek_token (parser)->location;
9801 c_parser_consume_pragma (parser);
9802 c_parser_skip_to_pragma_eol (parser);
9804 c_finish_omp_barrier (loc);
9807 /* OpenMP 2.5:
9808 # pragma omp critical [(name)] new-line
9809 structured-block
9811 LOC is the location of the #pragma itself. */
9813 static tree
9814 c_parser_omp_critical (location_t loc, c_parser *parser)
9816 tree stmt, name = NULL;
9818 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9820 c_parser_consume_token (parser);
9821 if (c_parser_next_token_is (parser, CPP_NAME))
9823 name = c_parser_peek_token (parser)->value;
9824 c_parser_consume_token (parser);
9825 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9827 else
9828 c_parser_error (parser, "expected identifier");
9830 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9831 c_parser_error (parser, "expected %<(%> or end of line");
9832 c_parser_skip_to_pragma_eol (parser);
9834 stmt = c_parser_omp_structured_block (parser);
9835 return c_finish_omp_critical (loc, stmt, name);
9838 /* OpenMP 2.5:
9839 # pragma omp flush flush-vars[opt] new-line
9841 flush-vars:
9842 ( variable-list ) */
9844 static void
9845 c_parser_omp_flush (c_parser *parser)
9847 location_t loc = c_parser_peek_token (parser)->location;
9848 c_parser_consume_pragma (parser);
9849 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9850 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9851 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9852 c_parser_error (parser, "expected %<(%> or end of line");
9853 c_parser_skip_to_pragma_eol (parser);
9855 c_finish_omp_flush (loc);
9858 /* Parse the restricted form of the for statement allowed by OpenMP.
9859 The real trick here is to determine the loop control variable early
9860 so that we can push a new decl if necessary to make it private.
9861 LOC is the location of the OMP in "#pragma omp". */
9863 static tree
9864 c_parser_omp_for_loop (location_t loc,
9865 c_parser *parser, tree clauses, tree *par_clauses)
9867 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9868 tree declv, condv, incrv, initv, ret = NULL;
9869 bool fail = false, open_brace_parsed = false;
9870 int i, collapse = 1, nbraces = 0;
9871 location_t for_loc;
9872 VEC(tree,gc) *for_block = make_tree_vector ();
9874 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9875 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9876 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9878 gcc_assert (collapse >= 1);
9880 declv = make_tree_vec (collapse);
9881 initv = make_tree_vec (collapse);
9882 condv = make_tree_vec (collapse);
9883 incrv = make_tree_vec (collapse);
9885 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9887 c_parser_error (parser, "for statement expected");
9888 return NULL;
9890 for_loc = c_parser_peek_token (parser)->location;
9891 c_parser_consume_token (parser);
9893 for (i = 0; i < collapse; i++)
9895 int bracecount = 0;
9897 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9898 goto pop_scopes;
9900 /* Parse the initialization declaration or expression. */
9901 if (c_parser_next_tokens_start_declaration (parser))
9903 if (i > 0)
9904 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9905 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9906 decl = check_for_loop_decls (for_loc, flag_isoc99);
9907 if (decl == NULL)
9908 goto error_init;
9909 if (DECL_INITIAL (decl) == error_mark_node)
9910 decl = error_mark_node;
9911 init = decl;
9913 else if (c_parser_next_token_is (parser, CPP_NAME)
9914 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9916 struct c_expr decl_exp;
9917 struct c_expr init_exp;
9918 location_t init_loc;
9920 decl_exp = c_parser_postfix_expression (parser);
9921 decl = decl_exp.value;
9923 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9925 init_loc = c_parser_peek_token (parser)->location;
9926 init_exp = c_parser_expr_no_commas (parser, NULL);
9927 init_exp = default_function_array_read_conversion (init_loc,
9928 init_exp);
9929 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9930 NOP_EXPR, init_loc, init_exp.value,
9931 init_exp.original_type);
9932 init = c_process_expr_stmt (init_loc, init);
9934 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9936 else
9938 error_init:
9939 c_parser_error (parser,
9940 "expected iteration declaration or initialization");
9941 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9942 "expected %<)%>");
9943 fail = true;
9944 goto parse_next;
9947 /* Parse the loop condition. */
9948 cond = NULL_TREE;
9949 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9951 location_t cond_loc = c_parser_peek_token (parser)->location;
9952 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
9953 PREC_NONE);
9955 cond = cond_expr.value;
9956 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9957 cond = c_fully_fold (cond, false, NULL);
9958 switch (cond_expr.original_code)
9960 case GT_EXPR:
9961 case GE_EXPR:
9962 case LT_EXPR:
9963 case LE_EXPR:
9964 break;
9965 default:
9966 /* Can't be cond = error_mark_node, because we want to preserve
9967 the location until c_finish_omp_for. */
9968 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9969 break;
9971 protected_set_expr_location (cond, cond_loc);
9973 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9975 /* Parse the increment expression. */
9976 incr = NULL_TREE;
9977 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9979 location_t incr_loc = c_parser_peek_token (parser)->location;
9981 incr = c_process_expr_stmt (incr_loc,
9982 c_parser_expression (parser).value);
9984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9986 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9987 fail = true;
9988 else
9990 TREE_VEC_ELT (declv, i) = decl;
9991 TREE_VEC_ELT (initv, i) = init;
9992 TREE_VEC_ELT (condv, i) = cond;
9993 TREE_VEC_ELT (incrv, i) = incr;
9996 parse_next:
9997 if (i == collapse - 1)
9998 break;
10000 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10001 in between the collapsed for loops to be still considered perfectly
10002 nested. Hopefully the final version clarifies this.
10003 For now handle (multiple) {'s and empty statements. */
10006 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10008 c_parser_consume_token (parser);
10009 break;
10011 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10013 c_parser_consume_token (parser);
10014 bracecount++;
10016 else if (bracecount
10017 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10018 c_parser_consume_token (parser);
10019 else
10021 c_parser_error (parser, "not enough perfectly nested loops");
10022 if (bracecount)
10024 open_brace_parsed = true;
10025 bracecount--;
10027 fail = true;
10028 collapse = 0;
10029 break;
10032 while (1);
10034 nbraces += bracecount;
10037 save_break = c_break_label;
10038 c_break_label = size_one_node;
10039 save_cont = c_cont_label;
10040 c_cont_label = NULL_TREE;
10041 body = push_stmt_list ();
10043 if (open_brace_parsed)
10045 location_t here = c_parser_peek_token (parser)->location;
10046 stmt = c_begin_compound_stmt (true);
10047 c_parser_compound_statement_nostart (parser);
10048 add_stmt (c_end_compound_stmt (here, stmt, true));
10050 else
10051 add_stmt (c_parser_c99_block_statement (parser));
10052 if (c_cont_label)
10054 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10055 SET_EXPR_LOCATION (t, loc);
10056 add_stmt (t);
10059 body = pop_stmt_list (body);
10060 c_break_label = save_break;
10061 c_cont_label = save_cont;
10063 while (nbraces)
10065 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10067 c_parser_consume_token (parser);
10068 nbraces--;
10070 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10071 c_parser_consume_token (parser);
10072 else
10074 c_parser_error (parser, "collapsed loops not perfectly nested");
10075 while (nbraces)
10077 location_t here = c_parser_peek_token (parser)->location;
10078 stmt = c_begin_compound_stmt (true);
10079 add_stmt (body);
10080 c_parser_compound_statement_nostart (parser);
10081 body = c_end_compound_stmt (here, stmt, true);
10082 nbraces--;
10084 goto pop_scopes;
10088 /* Only bother calling c_finish_omp_for if we haven't already generated
10089 an error from the initialization parsing. */
10090 if (!fail)
10092 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10093 if (stmt)
10095 if (par_clauses != NULL)
10097 tree *c;
10098 for (c = par_clauses; *c ; )
10099 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10100 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10101 c = &OMP_CLAUSE_CHAIN (*c);
10102 else
10104 for (i = 0; i < collapse; i++)
10105 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10106 break;
10107 if (i == collapse)
10108 c = &OMP_CLAUSE_CHAIN (*c);
10109 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10111 error_at (loc,
10112 "iteration variable %qD should not be firstprivate",
10113 OMP_CLAUSE_DECL (*c));
10114 *c = OMP_CLAUSE_CHAIN (*c);
10116 else
10118 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10119 change it to shared (decl) in
10120 OMP_PARALLEL_CLAUSES. */
10121 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10122 OMP_CLAUSE_LASTPRIVATE);
10123 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10124 OMP_CLAUSE_CHAIN (l) = clauses;
10125 clauses = l;
10126 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10130 OMP_FOR_CLAUSES (stmt) = clauses;
10132 ret = stmt;
10134 pop_scopes:
10135 while (!VEC_empty (tree, for_block))
10137 /* FIXME diagnostics: LOC below should be the actual location of
10138 this particular for block. We need to build a list of
10139 locations to go along with FOR_BLOCK. */
10140 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
10141 add_stmt (stmt);
10143 release_tree_vector (for_block);
10144 return ret;
10147 /* OpenMP 2.5:
10148 #pragma omp for for-clause[optseq] new-line
10149 for-loop
10151 LOC is the location of the #pragma token.
10154 #define OMP_FOR_CLAUSE_MASK \
10155 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10156 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10157 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10158 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10159 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10160 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10161 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10162 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10164 static tree
10165 c_parser_omp_for (location_t loc, c_parser *parser)
10167 tree block, clauses, ret;
10169 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10170 "#pragma omp for");
10172 block = c_begin_compound_stmt (true);
10173 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10174 block = c_end_compound_stmt (loc, block, true);
10175 add_stmt (block);
10177 return ret;
10180 /* OpenMP 2.5:
10181 # pragma omp master new-line
10182 structured-block
10184 LOC is the location of the #pragma token.
10187 static tree
10188 c_parser_omp_master (location_t loc, c_parser *parser)
10190 c_parser_skip_to_pragma_eol (parser);
10191 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10194 /* OpenMP 2.5:
10195 # pragma omp ordered new-line
10196 structured-block
10198 LOC is the location of the #pragma itself.
10201 static tree
10202 c_parser_omp_ordered (location_t loc, c_parser *parser)
10204 c_parser_skip_to_pragma_eol (parser);
10205 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10208 /* OpenMP 2.5:
10210 section-scope:
10211 { section-sequence }
10213 section-sequence:
10214 section-directive[opt] structured-block
10215 section-sequence section-directive structured-block
10217 SECTIONS_LOC is the location of the #pragma omp sections. */
10219 static tree
10220 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10222 tree stmt, substmt;
10223 bool error_suppress = false;
10224 location_t loc;
10226 loc = c_parser_peek_token (parser)->location;
10227 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10229 /* Avoid skipping until the end of the block. */
10230 parser->error = false;
10231 return NULL_TREE;
10234 stmt = push_stmt_list ();
10236 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10238 substmt = push_stmt_list ();
10240 while (1)
10242 c_parser_statement (parser);
10244 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10245 break;
10246 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10247 break;
10248 if (c_parser_next_token_is (parser, CPP_EOF))
10249 break;
10252 substmt = pop_stmt_list (substmt);
10253 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10254 SET_EXPR_LOCATION (substmt, loc);
10255 add_stmt (substmt);
10258 while (1)
10260 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10261 break;
10262 if (c_parser_next_token_is (parser, CPP_EOF))
10263 break;
10265 loc = c_parser_peek_token (parser)->location;
10266 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10268 c_parser_consume_pragma (parser);
10269 c_parser_skip_to_pragma_eol (parser);
10270 error_suppress = false;
10272 else if (!error_suppress)
10274 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10275 error_suppress = true;
10278 substmt = c_parser_omp_structured_block (parser);
10279 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10280 SET_EXPR_LOCATION (substmt, loc);
10281 add_stmt (substmt);
10283 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10284 "expected %<#pragma omp section%> or %<}%>");
10286 substmt = pop_stmt_list (stmt);
10288 stmt = make_node (OMP_SECTIONS);
10289 SET_EXPR_LOCATION (stmt, sections_loc);
10290 TREE_TYPE (stmt) = void_type_node;
10291 OMP_SECTIONS_BODY (stmt) = substmt;
10293 return add_stmt (stmt);
10296 /* OpenMP 2.5:
10297 # pragma omp sections sections-clause[optseq] newline
10298 sections-scope
10300 LOC is the location of the #pragma token.
10303 #define OMP_SECTIONS_CLAUSE_MASK \
10304 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10305 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10306 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10307 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10308 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10310 static tree
10311 c_parser_omp_sections (location_t loc, c_parser *parser)
10313 tree block, clauses, ret;
10315 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10316 "#pragma omp sections");
10318 block = c_begin_compound_stmt (true);
10319 ret = c_parser_omp_sections_scope (loc, parser);
10320 if (ret)
10321 OMP_SECTIONS_CLAUSES (ret) = clauses;
10322 block = c_end_compound_stmt (loc, block, true);
10323 add_stmt (block);
10325 return ret;
10328 /* OpenMP 2.5:
10329 # pragma parallel parallel-clause new-line
10330 # pragma parallel for parallel-for-clause new-line
10331 # pragma parallel sections parallel-sections-clause new-line
10333 LOC is the location of the #pragma token.
10336 #define OMP_PARALLEL_CLAUSE_MASK \
10337 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10338 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10339 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10340 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10341 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10342 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10343 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10344 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10346 static tree
10347 c_parser_omp_parallel (location_t loc, c_parser *parser)
10349 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10350 const char *p_name = "#pragma omp parallel";
10351 tree stmt, clauses, par_clause, ws_clause, block;
10352 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10354 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10356 c_parser_consume_token (parser);
10357 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10358 p_name = "#pragma omp parallel for";
10359 mask |= OMP_FOR_CLAUSE_MASK;
10360 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10362 else if (c_parser_next_token_is (parser, CPP_NAME))
10364 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10365 if (strcmp (p, "sections") == 0)
10367 c_parser_consume_token (parser);
10368 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10369 p_name = "#pragma omp parallel sections";
10370 mask |= OMP_SECTIONS_CLAUSE_MASK;
10371 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10375 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10377 switch (p_kind)
10379 case PRAGMA_OMP_PARALLEL:
10380 block = c_begin_omp_parallel ();
10381 c_parser_statement (parser);
10382 stmt = c_finish_omp_parallel (loc, clauses, block);
10383 break;
10385 case PRAGMA_OMP_PARALLEL_FOR:
10386 block = c_begin_omp_parallel ();
10387 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10388 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10389 stmt = c_finish_omp_parallel (loc, par_clause, block);
10390 OMP_PARALLEL_COMBINED (stmt) = 1;
10391 break;
10393 case PRAGMA_OMP_PARALLEL_SECTIONS:
10394 block = c_begin_omp_parallel ();
10395 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10396 stmt = c_parser_omp_sections_scope (loc, parser);
10397 if (stmt)
10398 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10399 stmt = c_finish_omp_parallel (loc, par_clause, block);
10400 OMP_PARALLEL_COMBINED (stmt) = 1;
10401 break;
10403 default:
10404 gcc_unreachable ();
10407 return stmt;
10410 /* OpenMP 2.5:
10411 # pragma omp single single-clause[optseq] new-line
10412 structured-block
10414 LOC is the location of the #pragma.
10417 #define OMP_SINGLE_CLAUSE_MASK \
10418 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10419 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10420 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10421 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10423 static tree
10424 c_parser_omp_single (location_t loc, c_parser *parser)
10426 tree stmt = make_node (OMP_SINGLE);
10427 SET_EXPR_LOCATION (stmt, loc);
10428 TREE_TYPE (stmt) = void_type_node;
10430 OMP_SINGLE_CLAUSES (stmt)
10431 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10432 "#pragma omp single");
10433 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10435 return add_stmt (stmt);
10438 /* OpenMP 3.0:
10439 # pragma omp task task-clause[optseq] new-line
10441 LOC is the location of the #pragma.
10444 #define OMP_TASK_CLAUSE_MASK \
10445 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10446 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10447 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10448 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10449 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10450 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10451 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10452 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10454 static tree
10455 c_parser_omp_task (location_t loc, c_parser *parser)
10457 tree clauses, block;
10459 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10460 "#pragma omp task");
10462 block = c_begin_omp_task ();
10463 c_parser_statement (parser);
10464 return c_finish_omp_task (loc, clauses, block);
10467 /* OpenMP 3.0:
10468 # pragma omp taskwait new-line
10471 static void
10472 c_parser_omp_taskwait (c_parser *parser)
10474 location_t loc = c_parser_peek_token (parser)->location;
10475 c_parser_consume_pragma (parser);
10476 c_parser_skip_to_pragma_eol (parser);
10478 c_finish_omp_taskwait (loc);
10481 /* OpenMP 3.1:
10482 # pragma omp taskyield new-line
10485 static void
10486 c_parser_omp_taskyield (c_parser *parser)
10488 location_t loc = c_parser_peek_token (parser)->location;
10489 c_parser_consume_pragma (parser);
10490 c_parser_skip_to_pragma_eol (parser);
10492 c_finish_omp_taskyield (loc);
10495 /* Main entry point to parsing most OpenMP pragmas. */
10497 static void
10498 c_parser_omp_construct (c_parser *parser)
10500 enum pragma_kind p_kind;
10501 location_t loc;
10502 tree stmt;
10504 loc = c_parser_peek_token (parser)->location;
10505 p_kind = c_parser_peek_token (parser)->pragma_kind;
10506 c_parser_consume_pragma (parser);
10508 switch (p_kind)
10510 case PRAGMA_OMP_ATOMIC:
10511 c_parser_omp_atomic (loc, parser);
10512 return;
10513 case PRAGMA_OMP_CRITICAL:
10514 stmt = c_parser_omp_critical (loc, parser);
10515 break;
10516 case PRAGMA_OMP_FOR:
10517 stmt = c_parser_omp_for (loc, parser);
10518 break;
10519 case PRAGMA_OMP_MASTER:
10520 stmt = c_parser_omp_master (loc, parser);
10521 break;
10522 case PRAGMA_OMP_ORDERED:
10523 stmt = c_parser_omp_ordered (loc, parser);
10524 break;
10525 case PRAGMA_OMP_PARALLEL:
10526 stmt = c_parser_omp_parallel (loc, parser);
10527 break;
10528 case PRAGMA_OMP_SECTIONS:
10529 stmt = c_parser_omp_sections (loc, parser);
10530 break;
10531 case PRAGMA_OMP_SINGLE:
10532 stmt = c_parser_omp_single (loc, parser);
10533 break;
10534 case PRAGMA_OMP_TASK:
10535 stmt = c_parser_omp_task (loc, parser);
10536 break;
10537 default:
10538 gcc_unreachable ();
10541 if (stmt)
10542 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
10546 /* OpenMP 2.5:
10547 # pragma omp threadprivate (variable-list) */
10549 static void
10550 c_parser_omp_threadprivate (c_parser *parser)
10552 tree vars, t;
10553 location_t loc;
10555 c_parser_consume_pragma (parser);
10556 loc = c_parser_peek_token (parser)->location;
10557 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10559 /* Mark every variable in VARS to be assigned thread local storage. */
10560 for (t = vars; t; t = TREE_CHAIN (t))
10562 tree v = TREE_PURPOSE (t);
10564 /* FIXME diagnostics: Ideally we should keep individual
10565 locations for all the variables in the var list to make the
10566 following errors more precise. Perhaps
10567 c_parser_omp_var_list_parens() should construct a list of
10568 locations to go along with the var list. */
10570 /* If V had already been marked threadprivate, it doesn't matter
10571 whether it had been used prior to this point. */
10572 if (TREE_CODE (v) != VAR_DECL)
10573 error_at (loc, "%qD is not a variable", v);
10574 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
10575 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
10576 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
10577 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
10578 else if (TREE_TYPE (v) == error_mark_node)
10580 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
10581 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
10582 else
10584 if (! DECL_THREAD_LOCAL_P (v))
10586 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
10587 /* If rtl has been already set for this var, call
10588 make_decl_rtl once again, so that encode_section_info
10589 has a chance to look at the new decl flags. */
10590 if (DECL_RTL_SET_P (v))
10591 make_decl_rtl (v);
10593 C_DECL_THREADPRIVATE_P (v) = 1;
10597 c_parser_skip_to_pragma_eol (parser);
10600 /* Parse a transaction attribute (GCC Extension).
10602 transaction-attribute:
10603 attributes
10604 [ [ any-word ] ]
10606 The transactional memory language description is written for C++,
10607 and uses the C++0x attribute syntax. For compatibility, allow the
10608 bracket style for transactions in C as well. */
10610 static tree
10611 c_parser_transaction_attributes (c_parser *parser)
10613 tree attr_name, attr = NULL;
10615 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10616 return c_parser_attributes (parser);
10618 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10619 return NULL_TREE;
10620 c_parser_consume_token (parser);
10621 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
10622 goto error1;
10624 attr_name = c_parser_attribute_any_word (parser);
10625 if (attr_name)
10627 c_parser_consume_token (parser);
10628 attr = build_tree_list (attr_name, NULL_TREE);
10630 else
10631 c_parser_error (parser, "expected identifier");
10633 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10634 error1:
10635 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10636 return attr;
10639 /* Parse a __transaction_atomic or __transaction_relaxed statement
10640 (GCC Extension).
10642 transaction-statement:
10643 __transaction_atomic transaction-attribute[opt] compound-statement
10644 __transaction_relaxed compound-statement
10646 Note that the only valid attribute is: "outer".
10649 static tree
10650 c_parser_transaction (c_parser *parser, enum rid keyword)
10652 unsigned int old_in = parser->in_transaction;
10653 unsigned int this_in = 1, new_in;
10654 location_t loc = c_parser_peek_token (parser)->location;
10655 tree stmt, attrs;
10657 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10658 || keyword == RID_TRANSACTION_RELAXED)
10659 && c_parser_next_token_is_keyword (parser, keyword));
10660 c_parser_consume_token (parser);
10662 if (keyword == RID_TRANSACTION_RELAXED)
10663 this_in |= TM_STMT_ATTR_RELAXED;
10664 else
10666 attrs = c_parser_transaction_attributes (parser);
10667 if (attrs)
10668 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
10671 /* Keep track if we're in the lexical scope of an outer transaction. */
10672 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
10674 parser->in_transaction = new_in;
10675 stmt = c_parser_compound_statement (parser);
10676 parser->in_transaction = old_in;
10678 if (flag_tm)
10679 stmt = c_finish_transaction (loc, stmt, this_in);
10680 else
10681 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10682 "%<__transaction_atomic%> without transactional memory support enabled"
10683 : "%<__transaction_relaxed %> "
10684 "without transactional memory support enabled"));
10686 return stmt;
10689 /* Parse a __transaction_atomic or __transaction_relaxed expression
10690 (GCC Extension).
10692 transaction-expression:
10693 __transaction_atomic ( expression )
10694 __transaction_relaxed ( expression )
10697 static struct c_expr
10698 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
10700 struct c_expr ret;
10701 unsigned int old_in = parser->in_transaction;
10702 unsigned int this_in = 1;
10703 location_t loc = c_parser_peek_token (parser)->location;
10704 tree attrs;
10706 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10707 || keyword == RID_TRANSACTION_RELAXED)
10708 && c_parser_next_token_is_keyword (parser, keyword));
10709 c_parser_consume_token (parser);
10711 if (keyword == RID_TRANSACTION_RELAXED)
10712 this_in |= TM_STMT_ATTR_RELAXED;
10713 else
10715 attrs = c_parser_transaction_attributes (parser);
10716 if (attrs)
10717 this_in |= parse_tm_stmt_attr (attrs, 0);
10720 parser->in_transaction = this_in;
10721 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10723 tree expr = c_parser_expression (parser).value;
10724 ret.original_type = TREE_TYPE (expr);
10725 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
10726 if (this_in & TM_STMT_ATTR_RELAXED)
10727 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
10728 SET_EXPR_LOCATION (ret.value, loc);
10729 ret.original_code = TRANSACTION_EXPR;
10730 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10733 goto error;
10736 else
10738 error:
10739 ret.value = error_mark_node;
10740 ret.original_code = ERROR_MARK;
10741 ret.original_type = NULL;
10743 parser->in_transaction = old_in;
10745 if (!flag_tm)
10746 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10747 "%<__transaction_atomic%> without transactional memory support enabled"
10748 : "%<__transaction_relaxed %> "
10749 "without transactional memory support enabled"));
10751 return ret;
10754 /* Parse a __transaction_cancel statement (GCC Extension).
10756 transaction-cancel-statement:
10757 __transaction_cancel transaction-attribute[opt] ;
10759 Note that the only valid attribute is "outer".
10762 static tree
10763 c_parser_transaction_cancel(c_parser *parser)
10765 location_t loc = c_parser_peek_token (parser)->location;
10766 tree attrs;
10767 bool is_outer = false;
10769 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
10770 c_parser_consume_token (parser);
10772 attrs = c_parser_transaction_attributes (parser);
10773 if (attrs)
10774 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
10776 if (!flag_tm)
10778 error_at (loc, "%<__transaction_cancel%> without "
10779 "transactional memory support enabled");
10780 goto ret_error;
10782 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
10784 error_at (loc, "%<__transaction_cancel%> within a "
10785 "%<__transaction_relaxed%>");
10786 goto ret_error;
10788 else if (is_outer)
10790 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
10791 && !is_tm_may_cancel_outer (current_function_decl))
10793 error_at (loc, "outer %<__transaction_cancel%> not "
10794 "within outer %<__transaction_atomic%>");
10795 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
10796 goto ret_error;
10799 else if (parser->in_transaction == 0)
10801 error_at (loc, "%<__transaction_cancel%> not within "
10802 "%<__transaction_atomic%>");
10803 goto ret_error;
10806 return add_stmt (build_tm_abort_call (loc, is_outer));
10808 ret_error:
10809 return build1 (NOP_EXPR, void_type_node, error_mark_node);
10812 /* Parse a single source file. */
10814 void
10815 c_parse_file (void)
10817 /* Use local storage to begin. If the first token is a pragma, parse it.
10818 If it is #pragma GCC pch_preprocess, then this will load a PCH file
10819 which will cause garbage collection. */
10820 c_parser tparser;
10822 memset (&tparser, 0, sizeof tparser);
10823 the_parser = &tparser;
10825 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
10826 c_parser_pragma_pch_preprocess (&tparser);
10828 the_parser = ggc_alloc_c_parser ();
10829 *the_parser = tparser;
10831 /* Initialize EH, if we've been told to do so. */
10832 if (flag_exceptions)
10833 using_eh_for_cleanups ();
10835 c_parser_translation_unit (the_parser);
10836 the_parser = NULL;
10839 #include "gt-c-parser.h"