In gcc/testsuite/: 2010-09-30 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / c-parser.c
blob564077449e465499299445ac3965ab5cd4b195f9
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
4 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 "toplev.h"
54 #include "ggc.h"
55 #include "c-family/c-common.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 /* The following flag is needed to contextualize Objective-C lexical
189 analysis. In some cases (e.g., 'int NSObject;'), it is
190 undesirable to bind an identifier to an Objective-C class, even
191 if a class with that name exists. */
192 BOOL_BITFIELD objc_need_raw_identifier : 1;
193 } c_parser;
196 /* The actual parser and external interface. ??? Does this need to be
197 garbage-collected? */
199 static GTY (()) c_parser *the_parser;
201 /* Read in and lex a single token, storing it in *TOKEN. */
203 static void
204 c_lex_one_token (c_parser *parser, c_token *token)
206 timevar_push (TV_LEX);
208 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
209 (parser->lex_untranslated_string
210 ? C_LEX_STRING_NO_TRANSLATE : 0));
211 token->id_kind = C_ID_NONE;
212 token->keyword = RID_MAX;
213 token->pragma_kind = PRAGMA_NONE;
215 switch (token->type)
217 case CPP_NAME:
219 tree decl;
221 bool objc_force_identifier = parser->objc_need_raw_identifier;
222 if (c_dialect_objc ())
223 parser->objc_need_raw_identifier = false;
225 if (C_IS_RESERVED_WORD (token->value))
227 enum rid rid_code = C_RID_CODE (token->value);
229 if (rid_code == RID_CXX_COMPAT_WARN)
231 warning_at (token->location,
232 OPT_Wc___compat,
233 "identifier %qE conflicts with C++ keyword",
234 token->value);
236 else if (rid_code >= RID_FIRST_ADDR_SPACE
237 && rid_code <= RID_LAST_ADDR_SPACE)
239 token->id_kind = C_ID_ADDRSPACE;
240 token->keyword = rid_code;
241 break;
243 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
245 /* We found an Objective-C "pq" keyword (in, out,
246 inout, bycopy, byref, oneway). They need special
247 care because the interpretation depends on the
248 context.
250 if (parser->objc_pq_context)
252 token->type = CPP_KEYWORD;
253 token->keyword = rid_code;
254 break;
256 /* Else, "pq" keywords outside of the "pq" context are
257 not keywords, and we fall through to the code for
258 normal tokens.
261 else if (c_dialect_objc ()
262 && (OBJC_IS_AT_KEYWORD (rid_code)
263 || OBJC_IS_CXX_KEYWORD (rid_code)))
265 /* We found one of the Objective-C "@" keywords (defs,
266 selector, synchronized, etc) or one of the
267 Objective-C "cxx" keywords (class, private,
268 protected, public, try, catch, throw) without a
269 preceding '@' sign. Do nothing and fall through to
270 the code for normal tokens (in C++ we would still
271 consider the CXX ones keywords, but not in C).
275 else
277 token->type = CPP_KEYWORD;
278 token->keyword = rid_code;
279 break;
283 decl = lookup_name (token->value);
284 if (decl)
286 if (TREE_CODE (decl) == TYPE_DECL)
288 token->id_kind = C_ID_TYPENAME;
289 break;
292 else if (c_dialect_objc ())
294 tree objc_interface_decl = objc_is_class_name (token->value);
295 /* Objective-C class names are in the same namespace as
296 variables and typedefs, and hence are shadowed by local
297 declarations. */
298 if (objc_interface_decl
299 && (global_bindings_p ()
300 || (!objc_force_identifier && !decl)))
302 token->value = objc_interface_decl;
303 token->id_kind = C_ID_CLASSNAME;
304 break;
307 token->id_kind = C_ID_ID;
309 break;
310 case CPP_AT_NAME:
311 /* This only happens in Objective-C; it must be a keyword. */
312 token->type = CPP_KEYWORD;
313 switch (C_RID_CODE (token->value))
315 /* Replace 'class' with '@class', 'private' with '@private',
316 etc. This prevents confusion with the C++ keyword
317 'class', and makes the tokens consistent with other
318 Objective-C 'AT' keywords. For example '@class' is
319 reported as RID_AT_CLASS which is consistent with
320 '@synchronized', which is reported as
321 RID_AT_SYNCHRONIZED.
323 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
324 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
325 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
326 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
327 case RID_THROW: token->keyword = RID_AT_THROW; break;
328 case RID_TRY: token->keyword = RID_AT_TRY; break;
329 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
330 default: token->keyword = C_RID_CODE (token->value);
332 break;
333 case CPP_COLON:
334 case CPP_COMMA:
335 case CPP_CLOSE_PAREN:
336 case CPP_SEMICOLON:
337 /* These tokens may affect the interpretation of any identifiers
338 following, if doing Objective-C. */
339 if (c_dialect_objc ())
340 parser->objc_need_raw_identifier = false;
341 break;
342 case CPP_PRAGMA:
343 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
344 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
345 token->value = NULL;
346 break;
347 default:
348 break;
350 timevar_pop (TV_LEX);
353 /* Return a pointer to the next token from PARSER, reading it in if
354 necessary. */
356 static inline c_token *
357 c_parser_peek_token (c_parser *parser)
359 if (parser->tokens_avail == 0)
361 c_lex_one_token (parser, &parser->tokens[0]);
362 parser->tokens_avail = 1;
364 return &parser->tokens[0];
367 /* Return true if the next token from PARSER has the indicated
368 TYPE. */
370 static inline bool
371 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
373 return c_parser_peek_token (parser)->type == type;
376 /* Return true if the next token from PARSER does not have the
377 indicated TYPE. */
379 static inline bool
380 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
382 return !c_parser_next_token_is (parser, type);
385 /* Return true if the next token from PARSER is the indicated
386 KEYWORD. */
388 static inline bool
389 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
391 return c_parser_peek_token (parser)->keyword == keyword;
394 /* Return true if TOKEN can start a type name,
395 false otherwise. */
396 static bool
397 c_token_starts_typename (c_token *token)
399 switch (token->type)
401 case CPP_NAME:
402 switch (token->id_kind)
404 case C_ID_ID:
405 return false;
406 case C_ID_ADDRSPACE:
407 return true;
408 case C_ID_TYPENAME:
409 return true;
410 case C_ID_CLASSNAME:
411 gcc_assert (c_dialect_objc ());
412 return true;
413 default:
414 gcc_unreachable ();
416 case CPP_KEYWORD:
417 switch (token->keyword)
419 case RID_UNSIGNED:
420 case RID_LONG:
421 case RID_INT128:
422 case RID_SHORT:
423 case RID_SIGNED:
424 case RID_COMPLEX:
425 case RID_INT:
426 case RID_CHAR:
427 case RID_FLOAT:
428 case RID_DOUBLE:
429 case RID_VOID:
430 case RID_DFLOAT32:
431 case RID_DFLOAT64:
432 case RID_DFLOAT128:
433 case RID_BOOL:
434 case RID_ENUM:
435 case RID_STRUCT:
436 case RID_UNION:
437 case RID_TYPEOF:
438 case RID_CONST:
439 case RID_VOLATILE:
440 case RID_RESTRICT:
441 case RID_ATTRIBUTE:
442 case RID_FRACT:
443 case RID_ACCUM:
444 case RID_SAT:
445 return true;
446 default:
447 return false;
449 case CPP_LESS:
450 if (c_dialect_objc ())
451 return true;
452 return false;
453 default:
454 return false;
458 /* Return true if the next token from PARSER can start a type name,
459 false otherwise. */
460 static inline bool
461 c_parser_next_token_starts_typename (c_parser *parser)
463 c_token *token = c_parser_peek_token (parser);
464 return c_token_starts_typename (token);
467 /* Return true if TOKEN can start declaration specifiers, false
468 otherwise. */
469 static bool
470 c_token_starts_declspecs (c_token *token)
472 switch (token->type)
474 case CPP_NAME:
475 switch (token->id_kind)
477 case C_ID_ID:
478 return false;
479 case C_ID_ADDRSPACE:
480 return true;
481 case C_ID_TYPENAME:
482 return true;
483 case C_ID_CLASSNAME:
484 gcc_assert (c_dialect_objc ());
485 return true;
486 default:
487 gcc_unreachable ();
489 case CPP_KEYWORD:
490 switch (token->keyword)
492 case RID_STATIC:
493 case RID_EXTERN:
494 case RID_REGISTER:
495 case RID_TYPEDEF:
496 case RID_INLINE:
497 case RID_AUTO:
498 case RID_THREAD:
499 case RID_UNSIGNED:
500 case RID_LONG:
501 case RID_INT128:
502 case RID_SHORT:
503 case RID_SIGNED:
504 case RID_COMPLEX:
505 case RID_INT:
506 case RID_CHAR:
507 case RID_FLOAT:
508 case RID_DOUBLE:
509 case RID_VOID:
510 case RID_DFLOAT32:
511 case RID_DFLOAT64:
512 case RID_DFLOAT128:
513 case RID_BOOL:
514 case RID_ENUM:
515 case RID_STRUCT:
516 case RID_UNION:
517 case RID_TYPEOF:
518 case RID_CONST:
519 case RID_VOLATILE:
520 case RID_RESTRICT:
521 case RID_ATTRIBUTE:
522 case RID_FRACT:
523 case RID_ACCUM:
524 case RID_SAT:
525 return true;
526 default:
527 return false;
529 case CPP_LESS:
530 if (c_dialect_objc ())
531 return true;
532 return false;
533 default:
534 return false;
539 /* Return true if TOKEN can start declaration specifiers or a static
540 assertion, false otherwise. */
541 static bool
542 c_token_starts_declaration (c_token *token)
544 if (c_token_starts_declspecs (token)
545 || token->keyword == RID_STATIC_ASSERT)
546 return true;
547 else
548 return false;
551 /* Return true if the next token from PARSER can start declaration
552 specifiers, false otherwise. */
553 static inline bool
554 c_parser_next_token_starts_declspecs (c_parser *parser)
556 c_token *token = c_parser_peek_token (parser);
557 return c_token_starts_declspecs (token);
560 /* Return true if the next token from PARSER can start declaration
561 specifiers or a static assertion, false otherwise. */
562 static inline bool
563 c_parser_next_token_starts_declaration (c_parser *parser)
565 c_token *token = c_parser_peek_token (parser);
566 return c_token_starts_declaration (token);
569 /* Return a pointer to the next-but-one token from PARSER, reading it
570 in if necessary. The next token is already read in. */
572 static c_token *
573 c_parser_peek_2nd_token (c_parser *parser)
575 if (parser->tokens_avail >= 2)
576 return &parser->tokens[1];
577 gcc_assert (parser->tokens_avail == 1);
578 gcc_assert (parser->tokens[0].type != CPP_EOF);
579 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
580 c_lex_one_token (parser, &parser->tokens[1]);
581 parser->tokens_avail = 2;
582 return &parser->tokens[1];
585 /* Consume the next token from PARSER. */
587 static void
588 c_parser_consume_token (c_parser *parser)
590 gcc_assert (parser->tokens_avail >= 1);
591 gcc_assert (parser->tokens[0].type != CPP_EOF);
592 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
593 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
594 if (parser->tokens_avail == 2)
595 parser->tokens[0] = parser->tokens[1];
596 parser->tokens_avail--;
599 /* Expect the current token to be a #pragma. Consume it and remember
600 that we've begun parsing a pragma. */
602 static void
603 c_parser_consume_pragma (c_parser *parser)
605 gcc_assert (!parser->in_pragma);
606 gcc_assert (parser->tokens_avail >= 1);
607 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
608 if (parser->tokens_avail == 2)
609 parser->tokens[0] = parser->tokens[1];
610 parser->tokens_avail--;
611 parser->in_pragma = true;
614 /* Update the globals input_location and in_system_header from
615 TOKEN. */
616 static inline void
617 c_parser_set_source_position_from_token (c_token *token)
619 if (token->type != CPP_EOF)
621 input_location = token->location;
625 /* Issue a diagnostic of the form
626 FILE:LINE: MESSAGE before TOKEN
627 where TOKEN is the next token in the input stream of PARSER.
628 MESSAGE (specified by the caller) is usually of the form "expected
629 OTHER-TOKEN".
631 Do not issue a diagnostic if still recovering from an error.
633 ??? This is taken from the C++ parser, but building up messages in
634 this way is not i18n-friendly and some other approach should be
635 used. */
637 static void
638 c_parser_error (c_parser *parser, const char *gmsgid)
640 c_token *token = c_parser_peek_token (parser);
641 if (parser->error)
642 return;
643 parser->error = true;
644 if (!gmsgid)
645 return;
646 /* This diagnostic makes more sense if it is tagged to the line of
647 the token we just peeked at. */
648 c_parser_set_source_position_from_token (token);
649 c_parse_error (gmsgid,
650 /* Because c_parse_error does not understand
651 CPP_KEYWORD, keywords are treated like
652 identifiers. */
653 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
654 /* ??? The C parser does not save the cpp flags of a
655 token, we need to pass 0 here and we will not get
656 the source spelling of some tokens but rather the
657 canonical spelling. */
658 token->value, /*flags=*/0);
661 /* If the next token is of the indicated TYPE, consume it. Otherwise,
662 issue the error MSGID. If MSGID is NULL then a message has already
663 been produced and no message will be produced this time. Returns
664 true if found, false otherwise. */
666 static bool
667 c_parser_require (c_parser *parser,
668 enum cpp_ttype type,
669 const char *msgid)
671 if (c_parser_next_token_is (parser, type))
673 c_parser_consume_token (parser);
674 return true;
676 else
678 c_parser_error (parser, msgid);
679 return false;
683 /* If the next token is the indicated keyword, consume it. Otherwise,
684 issue the error MSGID. Returns true if found, false otherwise. */
686 static bool
687 c_parser_require_keyword (c_parser *parser,
688 enum rid keyword,
689 const char *msgid)
691 if (c_parser_next_token_is_keyword (parser, keyword))
693 c_parser_consume_token (parser);
694 return true;
696 else
698 c_parser_error (parser, msgid);
699 return false;
703 /* Like c_parser_require, except that tokens will be skipped until the
704 desired token is found. An error message is still produced if the
705 next token is not as expected. If MSGID is NULL then a message has
706 already been produced and no message will be produced this
707 time. */
709 static void
710 c_parser_skip_until_found (c_parser *parser,
711 enum cpp_ttype type,
712 const char *msgid)
714 unsigned nesting_depth = 0;
716 if (c_parser_require (parser, type, msgid))
717 return;
719 /* Skip tokens until the desired token is found. */
720 while (true)
722 /* Peek at the next token. */
723 c_token *token = c_parser_peek_token (parser);
724 /* If we've reached the token we want, consume it and stop. */
725 if (token->type == type && !nesting_depth)
727 c_parser_consume_token (parser);
728 break;
731 /* If we've run out of tokens, stop. */
732 if (token->type == CPP_EOF)
733 return;
734 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
735 return;
736 if (token->type == CPP_OPEN_BRACE
737 || token->type == CPP_OPEN_PAREN
738 || token->type == CPP_OPEN_SQUARE)
739 ++nesting_depth;
740 else if (token->type == CPP_CLOSE_BRACE
741 || token->type == CPP_CLOSE_PAREN
742 || token->type == CPP_CLOSE_SQUARE)
744 if (nesting_depth-- == 0)
745 break;
747 /* Consume this token. */
748 c_parser_consume_token (parser);
750 parser->error = false;
753 /* Skip tokens until the end of a parameter is found, but do not
754 consume the comma, semicolon or closing delimiter. */
756 static void
757 c_parser_skip_to_end_of_parameter (c_parser *parser)
759 unsigned nesting_depth = 0;
761 while (true)
763 c_token *token = c_parser_peek_token (parser);
764 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
765 && !nesting_depth)
766 break;
767 /* If we've run out of tokens, stop. */
768 if (token->type == CPP_EOF)
769 return;
770 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
771 return;
772 if (token->type == CPP_OPEN_BRACE
773 || token->type == CPP_OPEN_PAREN
774 || token->type == CPP_OPEN_SQUARE)
775 ++nesting_depth;
776 else if (token->type == CPP_CLOSE_BRACE
777 || token->type == CPP_CLOSE_PAREN
778 || token->type == CPP_CLOSE_SQUARE)
780 if (nesting_depth-- == 0)
781 break;
783 /* Consume this token. */
784 c_parser_consume_token (parser);
786 parser->error = false;
789 /* Expect to be at the end of the pragma directive and consume an
790 end of line marker. */
792 static void
793 c_parser_skip_to_pragma_eol (c_parser *parser)
795 gcc_assert (parser->in_pragma);
796 parser->in_pragma = false;
798 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
799 while (true)
801 c_token *token = c_parser_peek_token (parser);
802 if (token->type == CPP_EOF)
803 break;
804 if (token->type == CPP_PRAGMA_EOL)
806 c_parser_consume_token (parser);
807 break;
809 c_parser_consume_token (parser);
812 parser->error = false;
815 /* Skip tokens until we have consumed an entire block, or until we
816 have consumed a non-nested ';'. */
818 static void
819 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
821 unsigned nesting_depth = 0;
822 bool save_error = parser->error;
824 while (true)
826 c_token *token;
828 /* Peek at the next token. */
829 token = c_parser_peek_token (parser);
831 switch (token->type)
833 case CPP_EOF:
834 return;
836 case CPP_PRAGMA_EOL:
837 if (parser->in_pragma)
838 return;
839 break;
841 case CPP_SEMICOLON:
842 /* If the next token is a ';', we have reached the
843 end of the statement. */
844 if (!nesting_depth)
846 /* Consume the ';'. */
847 c_parser_consume_token (parser);
848 goto finished;
850 break;
852 case CPP_CLOSE_BRACE:
853 /* If the next token is a non-nested '}', then we have
854 reached the end of the current block. */
855 if (nesting_depth == 0 || --nesting_depth == 0)
857 c_parser_consume_token (parser);
858 goto finished;
860 break;
862 case CPP_OPEN_BRACE:
863 /* If it the next token is a '{', then we are entering a new
864 block. Consume the entire block. */
865 ++nesting_depth;
866 break;
868 case CPP_PRAGMA:
869 /* If we see a pragma, consume the whole thing at once. We
870 have some safeguards against consuming pragmas willy-nilly.
871 Normally, we'd expect to be here with parser->error set,
872 which disables these safeguards. But it's possible to get
873 here for secondary error recovery, after parser->error has
874 been cleared. */
875 c_parser_consume_pragma (parser);
876 c_parser_skip_to_pragma_eol (parser);
877 parser->error = save_error;
878 continue;
880 default:
881 break;
884 c_parser_consume_token (parser);
887 finished:
888 parser->error = false;
891 /* CPP's options (initialized by c-opts.c). */
892 extern cpp_options *cpp_opts;
894 /* Save the warning flags which are controlled by __extension__. */
896 static inline int
897 disable_extension_diagnostics (void)
899 int ret = (pedantic
900 | (warn_pointer_arith << 1)
901 | (warn_traditional << 2)
902 | (flag_iso << 3)
903 | (warn_long_long << 4)
904 | (warn_cxx_compat << 5));
905 cpp_opts->cpp_pedantic = pedantic = 0;
906 warn_pointer_arith = 0;
907 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
908 flag_iso = 0;
909 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
910 warn_cxx_compat = 0;
911 return ret;
914 /* Restore the warning flags which are controlled by __extension__.
915 FLAGS is the return value from disable_extension_diagnostics. */
917 static inline void
918 restore_extension_diagnostics (int flags)
920 cpp_opts->cpp_pedantic = pedantic = flags & 1;
921 warn_pointer_arith = (flags >> 1) & 1;
922 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
923 flag_iso = (flags >> 3) & 1;
924 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
925 warn_cxx_compat = (flags >> 5) & 1;
928 /* Possibly kinds of declarator to parse. */
929 typedef enum c_dtr_syn {
930 /* A normal declarator with an identifier. */
931 C_DTR_NORMAL,
932 /* An abstract declarator (maybe empty). */
933 C_DTR_ABSTRACT,
934 /* A parameter declarator: may be either, but after a type name does
935 not redeclare a typedef name as an identifier if it can
936 alternatively be interpreted as a typedef name; see DR#009,
937 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
938 following DR#249. For example, given a typedef T, "int T" and
939 "int *T" are valid parameter declarations redeclaring T, while
940 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
941 abstract declarators rather than involving redundant parentheses;
942 the same applies with attributes inside the parentheses before
943 "T". */
944 C_DTR_PARM
945 } c_dtr_syn;
947 static void c_parser_external_declaration (c_parser *);
948 static void c_parser_asm_definition (c_parser *);
949 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
950 bool, bool);
951 static void c_parser_static_assert_declaration_no_semi (c_parser *);
952 static void c_parser_static_assert_declaration (c_parser *);
953 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
954 bool);
955 static struct c_typespec c_parser_enum_specifier (c_parser *);
956 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
957 static tree c_parser_struct_declaration (c_parser *);
958 static struct c_typespec c_parser_typeof_specifier (c_parser *);
959 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
960 bool *);
961 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
962 c_dtr_syn, bool *);
963 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
964 bool,
965 struct c_declarator *);
966 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
967 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
968 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
969 static tree c_parser_simple_asm_expr (c_parser *);
970 static tree c_parser_attributes (c_parser *);
971 static struct c_type_name *c_parser_type_name (c_parser *);
972 static struct c_expr c_parser_initializer (c_parser *);
973 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
974 static void c_parser_initelt (c_parser *, struct obstack *);
975 static void c_parser_initval (c_parser *, struct c_expr *,
976 struct obstack *);
977 static tree c_parser_compound_statement (c_parser *);
978 static void c_parser_compound_statement_nostart (c_parser *);
979 static void c_parser_label (c_parser *);
980 static void c_parser_statement (c_parser *);
981 static void c_parser_statement_after_labels (c_parser *);
982 static void c_parser_if_statement (c_parser *);
983 static void c_parser_switch_statement (c_parser *);
984 static void c_parser_while_statement (c_parser *);
985 static void c_parser_do_statement (c_parser *);
986 static void c_parser_for_statement (c_parser *);
987 static tree c_parser_asm_statement (c_parser *);
988 static tree c_parser_asm_operands (c_parser *, bool);
989 static tree c_parser_asm_goto_operands (c_parser *);
990 static tree c_parser_asm_clobbers (c_parser *);
991 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
992 static struct c_expr c_parser_conditional_expression (c_parser *,
993 struct c_expr *);
994 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
995 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
996 static struct c_expr c_parser_unary_expression (c_parser *);
997 static struct c_expr c_parser_sizeof_expression (c_parser *);
998 static struct c_expr c_parser_alignof_expression (c_parser *);
999 static struct c_expr c_parser_postfix_expression (c_parser *);
1000 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1001 struct c_type_name *,
1002 location_t);
1003 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1004 location_t loc,
1005 struct c_expr);
1006 static struct c_expr c_parser_expression (c_parser *);
1007 static struct c_expr c_parser_expression_conv (c_parser *);
1008 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1009 VEC(tree,gc) **);
1010 static void c_parser_omp_construct (c_parser *);
1011 static void c_parser_omp_threadprivate (c_parser *);
1012 static void c_parser_omp_barrier (c_parser *);
1013 static void c_parser_omp_flush (c_parser *);
1014 static void c_parser_omp_taskwait (c_parser *);
1016 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1017 static bool c_parser_pragma (c_parser *, enum pragma_context);
1019 /* These Objective-C parser functions are only ever called when
1020 compiling Objective-C. */
1021 static void c_parser_objc_class_definition (c_parser *, tree);
1022 static void c_parser_objc_class_instance_variables (c_parser *);
1023 static void c_parser_objc_class_declaration (c_parser *);
1024 static void c_parser_objc_alias_declaration (c_parser *);
1025 static void c_parser_objc_protocol_definition (c_parser *, tree);
1026 static enum tree_code c_parser_objc_method_type (c_parser *);
1027 static void c_parser_objc_method_definition (c_parser *);
1028 static void c_parser_objc_methodprotolist (c_parser *);
1029 static void c_parser_objc_methodproto (c_parser *);
1030 static tree c_parser_objc_method_decl (c_parser *, tree *);
1031 static tree c_parser_objc_type_name (c_parser *);
1032 static tree c_parser_objc_protocol_refs (c_parser *);
1033 static void c_parser_objc_try_catch_statement (c_parser *);
1034 static void c_parser_objc_synchronized_statement (c_parser *);
1035 static tree c_parser_objc_selector (c_parser *);
1036 static tree c_parser_objc_selector_arg (c_parser *);
1037 static tree c_parser_objc_receiver (c_parser *);
1038 static tree c_parser_objc_message_args (c_parser *);
1039 static tree c_parser_objc_keywordexpr (c_parser *);
1040 static bool c_parser_objc_diagnose_bad_element_prefix
1041 (c_parser *, struct c_declspecs *);
1043 /* Parse a translation unit (C90 6.7, C99 6.9).
1045 translation-unit:
1046 external-declarations
1048 external-declarations:
1049 external-declaration
1050 external-declarations external-declaration
1052 GNU extensions:
1054 translation-unit:
1055 empty
1058 static void
1059 c_parser_translation_unit (c_parser *parser)
1061 if (c_parser_next_token_is (parser, CPP_EOF))
1063 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1064 "ISO C forbids an empty translation unit");
1066 else
1068 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1069 mark_valid_location_for_stdc_pragma (false);
1072 ggc_collect ();
1073 c_parser_external_declaration (parser);
1074 obstack_free (&parser_obstack, obstack_position);
1076 while (c_parser_next_token_is_not (parser, CPP_EOF));
1080 /* Parse an external declaration (C90 6.7, C99 6.9).
1082 external-declaration:
1083 function-definition
1084 declaration
1086 GNU extensions:
1088 external-declaration:
1089 asm-definition
1091 __extension__ external-declaration
1093 Objective-C:
1095 external-declaration:
1096 objc-class-definition
1097 objc-class-declaration
1098 objc-alias-declaration
1099 objc-protocol-definition
1100 objc-method-definition
1101 @end
1104 static void
1105 c_parser_external_declaration (c_parser *parser)
1107 int ext;
1108 switch (c_parser_peek_token (parser)->type)
1110 case CPP_KEYWORD:
1111 switch (c_parser_peek_token (parser)->keyword)
1113 case RID_EXTENSION:
1114 ext = disable_extension_diagnostics ();
1115 c_parser_consume_token (parser);
1116 c_parser_external_declaration (parser);
1117 restore_extension_diagnostics (ext);
1118 break;
1119 case RID_ASM:
1120 c_parser_asm_definition (parser);
1121 break;
1122 case RID_AT_INTERFACE:
1123 case RID_AT_IMPLEMENTATION:
1124 gcc_assert (c_dialect_objc ());
1125 c_parser_objc_class_definition (parser, NULL_TREE);
1126 break;
1127 case RID_AT_CLASS:
1128 gcc_assert (c_dialect_objc ());
1129 c_parser_objc_class_declaration (parser);
1130 break;
1131 case RID_AT_ALIAS:
1132 gcc_assert (c_dialect_objc ());
1133 c_parser_objc_alias_declaration (parser);
1134 break;
1135 case RID_AT_PROTOCOL:
1136 gcc_assert (c_dialect_objc ());
1137 c_parser_objc_protocol_definition (parser, NULL_TREE);
1138 break;
1139 case RID_AT_END:
1140 gcc_assert (c_dialect_objc ());
1141 c_parser_consume_token (parser);
1142 objc_finish_implementation ();
1143 break;
1144 default:
1145 goto decl_or_fndef;
1147 break;
1148 case CPP_SEMICOLON:
1149 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1150 "ISO C does not allow extra %<;%> outside of a function");
1151 c_parser_consume_token (parser);
1152 break;
1153 case CPP_PRAGMA:
1154 mark_valid_location_for_stdc_pragma (true);
1155 c_parser_pragma (parser, pragma_external);
1156 mark_valid_location_for_stdc_pragma (false);
1157 break;
1158 case CPP_PLUS:
1159 case CPP_MINUS:
1160 if (c_dialect_objc ())
1162 c_parser_objc_method_definition (parser);
1163 break;
1165 /* Else fall through, and yield a syntax error trying to parse
1166 as a declaration or function definition. */
1167 default:
1168 decl_or_fndef:
1169 /* A declaration or a function definition (or, in Objective-C,
1170 an @interface or @protocol with prefix attributes). We can
1171 only tell which after parsing the declaration specifiers, if
1172 any, and the first declarator. */
1173 c_parser_declaration_or_fndef (parser, true, true, true, false, true);
1174 break;
1178 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1179 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1180 accepted; otherwise (old-style parameter declarations) only other
1181 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1182 assertion is accepted; otherwise (old-style parameter declarations)
1183 it is not. If NESTED is true, we are inside a function or parsing
1184 old-style parameter declarations; any functions encountered are
1185 nested functions and declaration specifiers are required; otherwise
1186 we are at top level and functions are normal functions and
1187 declaration specifiers may be optional. If EMPTY_OK is true, empty
1188 declarations are OK (subject to all other constraints); otherwise
1189 (old-style parameter declarations) they are diagnosed. If
1190 START_ATTR_OK is true, the declaration specifiers may start with
1191 attributes; otherwise they may not.
1193 declaration:
1194 declaration-specifiers init-declarator-list[opt] ;
1195 static_assert-declaration
1197 function-definition:
1198 declaration-specifiers[opt] declarator declaration-list[opt]
1199 compound-statement
1201 declaration-list:
1202 declaration
1203 declaration-list declaration
1205 init-declarator-list:
1206 init-declarator
1207 init-declarator-list , init-declarator
1209 init-declarator:
1210 declarator simple-asm-expr[opt] attributes[opt]
1211 declarator simple-asm-expr[opt] attributes[opt] = initializer
1213 GNU extensions:
1215 nested-function-definition:
1216 declaration-specifiers declarator declaration-list[opt]
1217 compound-statement
1219 Objective-C:
1220 attributes objc-class-definition
1221 attributes objc-category-definition
1222 attributes objc-protocol-definition
1224 The simple-asm-expr and attributes are GNU extensions.
1226 This function does not handle __extension__; that is handled in its
1227 callers. ??? Following the old parser, __extension__ may start
1228 external declarations, declarations in functions and declarations
1229 at the start of "for" loops, but not old-style parameter
1230 declarations.
1232 C99 requires declaration specifiers in a function definition; the
1233 absence is diagnosed through the diagnosis of implicit int. In GNU
1234 C we also allow but diagnose declarations without declaration
1235 specifiers, but only at top level (elsewhere they conflict with
1236 other syntax).
1238 OpenMP:
1240 declaration:
1241 threadprivate-directive */
1243 static void
1244 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1245 bool static_assert_ok, bool empty_ok,
1246 bool nested, bool start_attr_ok)
1248 struct c_declspecs *specs;
1249 tree prefix_attrs;
1250 tree all_prefix_attrs;
1251 bool diagnosed_no_specs = false;
1252 location_t here = c_parser_peek_token (parser)->location;
1254 if (static_assert_ok
1255 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1257 c_parser_static_assert_declaration (parser);
1258 return;
1260 specs = build_null_declspecs ();
1261 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1262 if (parser->error)
1264 c_parser_skip_to_end_of_block_or_statement (parser);
1265 return;
1267 if (nested && !specs->declspecs_seen_p)
1269 c_parser_error (parser, "expected declaration specifiers");
1270 c_parser_skip_to_end_of_block_or_statement (parser);
1271 return;
1273 finish_declspecs (specs);
1274 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1276 if (empty_ok)
1277 shadow_tag (specs);
1278 else
1280 shadow_tag_warned (specs, 1);
1281 pedwarn (here, 0, "empty declaration");
1283 c_parser_consume_token (parser);
1284 return;
1286 else if (c_dialect_objc ())
1288 /* Prefix attributes are an error on method decls. */
1289 switch (c_parser_peek_token (parser)->type)
1291 case CPP_PLUS:
1292 case CPP_MINUS:
1293 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1294 return;
1295 if (specs->attrs)
1297 warning_at (c_parser_peek_token (parser)->location,
1298 OPT_Wattributes,
1299 "prefix attributes are ignored for methods");
1300 specs->attrs = NULL_TREE;
1302 if (fndef_ok)
1303 c_parser_objc_method_definition (parser);
1304 else
1305 c_parser_objc_methodproto (parser);
1306 return;
1307 break;
1308 default:
1309 break;
1311 /* This is where we parse 'attributes @interface ...',
1312 'attributes @implementation ...', 'attributes @protocol ...'
1313 (where attributes could be, for example, __attribute__
1314 ((deprecated)).
1316 switch (c_parser_peek_token (parser)->keyword)
1318 case RID_AT_INTERFACE:
1320 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1321 return;
1322 c_parser_objc_class_definition (parser, specs->attrs);
1323 return;
1325 break;
1326 case RID_AT_IMPLEMENTATION:
1328 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1329 return;
1330 if (specs->attrs)
1332 warning_at (c_parser_peek_token (parser)->location,
1333 OPT_Wattributes,
1334 "prefix attributes are ignored for implementations");
1335 specs->attrs = NULL_TREE;
1337 c_parser_objc_class_definition (parser, NULL_TREE);
1338 return;
1340 break;
1341 case RID_AT_PROTOCOL:
1343 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1344 return;
1345 c_parser_objc_protocol_definition (parser, specs->attrs);
1346 return;
1348 break;
1349 default:
1350 break;
1354 pending_xref_error ();
1355 prefix_attrs = specs->attrs;
1356 all_prefix_attrs = prefix_attrs;
1357 specs->attrs = NULL_TREE;
1358 while (true)
1360 struct c_declarator *declarator;
1361 bool dummy = false;
1362 tree fnbody;
1363 /* Declaring either one or more declarators (in which case we
1364 should diagnose if there were no declaration specifiers) or a
1365 function definition (in which case the diagnostic for
1366 implicit int suffices). */
1367 declarator = c_parser_declarator (parser, specs->type_seen_p,
1368 C_DTR_NORMAL, &dummy);
1369 if (declarator == NULL)
1371 c_parser_skip_to_end_of_block_or_statement (parser);
1372 return;
1374 if (c_parser_next_token_is (parser, CPP_EQ)
1375 || c_parser_next_token_is (parser, CPP_COMMA)
1376 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1377 || c_parser_next_token_is_keyword (parser, RID_ASM)
1378 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1380 tree asm_name = NULL_TREE;
1381 tree postfix_attrs = NULL_TREE;
1382 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1384 diagnosed_no_specs = true;
1385 pedwarn (here, 0, "data definition has no type or storage class");
1387 /* Having seen a data definition, there cannot now be a
1388 function definition. */
1389 fndef_ok = false;
1390 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1391 asm_name = c_parser_simple_asm_expr (parser);
1392 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1393 postfix_attrs = c_parser_attributes (parser);
1394 if (c_parser_next_token_is (parser, CPP_EQ))
1396 tree d;
1397 struct c_expr init;
1398 location_t init_loc;
1399 c_parser_consume_token (parser);
1400 /* The declaration of the variable is in effect while
1401 its initializer is parsed. */
1402 d = start_decl (declarator, specs, true,
1403 chainon (postfix_attrs, all_prefix_attrs));
1404 if (!d)
1405 d = error_mark_node;
1406 start_init (d, asm_name, global_bindings_p ());
1407 init_loc = c_parser_peek_token (parser)->location;
1408 init = c_parser_initializer (parser);
1409 finish_init ();
1410 if (d != error_mark_node)
1412 maybe_warn_string_init (TREE_TYPE (d), init);
1413 finish_decl (d, init_loc, init.value,
1414 init.original_type, asm_name);
1417 else
1419 tree d = start_decl (declarator, specs, false,
1420 chainon (postfix_attrs,
1421 all_prefix_attrs));
1422 if (d)
1423 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1424 NULL_TREE, asm_name);
1426 if (c_parser_next_token_is (parser, CPP_COMMA))
1428 c_parser_consume_token (parser);
1429 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1430 all_prefix_attrs = chainon (c_parser_attributes (parser),
1431 prefix_attrs);
1432 else
1433 all_prefix_attrs = prefix_attrs;
1434 continue;
1436 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1438 c_parser_consume_token (parser);
1439 return;
1441 else
1443 c_parser_error (parser, "expected %<,%> or %<;%>");
1444 c_parser_skip_to_end_of_block_or_statement (parser);
1445 return;
1448 else if (!fndef_ok)
1450 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1451 "%<asm%> or %<__attribute__%>");
1452 c_parser_skip_to_end_of_block_or_statement (parser);
1453 return;
1455 /* Function definition (nested or otherwise). */
1456 if (nested)
1458 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1459 c_push_function_context ();
1461 if (!start_function (specs, declarator, all_prefix_attrs))
1463 /* This can appear in many cases looking nothing like a
1464 function definition, so we don't give a more specific
1465 error suggesting there was one. */
1466 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1467 "or %<__attribute__%>");
1468 if (nested)
1469 c_pop_function_context ();
1470 break;
1472 /* Parse old-style parameter declarations. ??? Attributes are
1473 not allowed to start declaration specifiers here because of a
1474 syntax conflict between a function declaration with attribute
1475 suffix and a function definition with an attribute prefix on
1476 first old-style parameter declaration. Following the old
1477 parser, they are not accepted on subsequent old-style
1478 parameter declarations either. However, there is no
1479 ambiguity after the first declaration, nor indeed on the
1480 first as long as we don't allow postfix attributes after a
1481 declarator with a nonempty identifier list in a definition;
1482 and postfix attributes have never been accepted here in
1483 function definitions either. */
1484 while (c_parser_next_token_is_not (parser, CPP_EOF)
1485 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1486 c_parser_declaration_or_fndef (parser, false, false, false,
1487 true, false);
1488 store_parm_decls ();
1489 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1490 = c_parser_peek_token (parser)->location;
1491 fnbody = c_parser_compound_statement (parser);
1492 if (nested)
1494 tree decl = current_function_decl;
1495 /* Mark nested functions as needing static-chain initially.
1496 lower_nested_functions will recompute it but the
1497 DECL_STATIC_CHAIN flag is also used before that happens,
1498 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1499 DECL_STATIC_CHAIN (decl) = 1;
1500 add_stmt (fnbody);
1501 finish_function ();
1502 c_pop_function_context ();
1503 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1505 else
1507 add_stmt (fnbody);
1508 finish_function ();
1510 break;
1514 /* Parse an asm-definition (asm() outside a function body). This is a
1515 GNU extension.
1517 asm-definition:
1518 simple-asm-expr ;
1521 static void
1522 c_parser_asm_definition (c_parser *parser)
1524 tree asm_str = c_parser_simple_asm_expr (parser);
1525 if (asm_str)
1526 cgraph_add_asm_node (asm_str);
1527 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1530 /* Parse a static assertion (C1X N1425 6.7.10).
1532 static_assert-declaration:
1533 static_assert-declaration-no-semi ;
1536 static void
1537 c_parser_static_assert_declaration (c_parser *parser)
1539 c_parser_static_assert_declaration_no_semi (parser);
1540 if (parser->error
1541 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1542 c_parser_skip_to_end_of_block_or_statement (parser);
1545 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1546 semicolon.
1548 static_assert-declaration-no-semi:
1549 _Static_assert ( constant-expression , string-literal )
1552 static void
1553 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1555 location_t assert_loc, value_loc;
1556 tree value;
1557 tree string;
1559 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1560 assert_loc = c_parser_peek_token (parser)->location;
1561 if (!flag_isoc1x)
1563 if (flag_isoc99)
1564 pedwarn (assert_loc, OPT_pedantic,
1565 "ISO C99 does not support %<_Static_assert%>");
1566 else
1567 pedwarn (assert_loc, OPT_pedantic,
1568 "ISO C90 does not support %<_Static_assert%>");
1570 c_parser_consume_token (parser);
1571 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1572 return;
1573 value_loc = c_parser_peek_token (parser)->location;
1574 value = c_parser_expr_no_commas (parser, NULL).value;
1575 parser->lex_untranslated_string = true;
1576 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1578 parser->lex_untranslated_string = false;
1579 return;
1581 switch (c_parser_peek_token (parser)->type)
1583 case CPP_STRING:
1584 case CPP_STRING16:
1585 case CPP_STRING32:
1586 case CPP_WSTRING:
1587 case CPP_UTF8STRING:
1588 string = c_parser_peek_token (parser)->value;
1589 c_parser_consume_token (parser);
1590 parser->lex_untranslated_string = false;
1591 break;
1592 default:
1593 c_parser_error (parser, "expected string literal");
1594 parser->lex_untranslated_string = false;
1595 return;
1597 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1599 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1601 error_at (value_loc, "expression in static assertion is not an integer");
1602 return;
1604 if (TREE_CODE (value) != INTEGER_CST)
1606 value = c_fully_fold (value, false, NULL);
1607 if (TREE_CODE (value) == INTEGER_CST)
1608 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1609 "is not an integer constant expression");
1611 if (TREE_CODE (value) != INTEGER_CST)
1613 error_at (value_loc, "expression in static assertion is not constant");
1614 return;
1616 constant_expression_warning (value);
1617 if (integer_zerop (value))
1618 error_at (assert_loc, "static assertion failed: %E", string);
1621 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1622 6.7), adding them to SPECS (which may already include some).
1623 Storage class specifiers are accepted iff SCSPEC_OK; type
1624 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1625 the start iff START_ATTR_OK.
1627 declaration-specifiers:
1628 storage-class-specifier declaration-specifiers[opt]
1629 type-specifier declaration-specifiers[opt]
1630 type-qualifier declaration-specifiers[opt]
1631 function-specifier declaration-specifiers[opt]
1633 Function specifiers (inline) are from C99, and are currently
1634 handled as storage class specifiers, as is __thread.
1636 C90 6.5.1, C99 6.7.1:
1637 storage-class-specifier:
1638 typedef
1639 extern
1640 static
1641 auto
1642 register
1644 C99 6.7.4:
1645 function-specifier:
1646 inline
1648 C90 6.5.2, C99 6.7.2:
1649 type-specifier:
1650 void
1651 char
1652 short
1654 long
1655 float
1656 double
1657 signed
1658 unsigned
1659 _Bool
1660 _Complex
1661 [_Imaginary removed in C99 TC2]
1662 struct-or-union-specifier
1663 enum-specifier
1664 typedef-name
1666 (_Bool and _Complex are new in C99.)
1668 C90 6.5.3, C99 6.7.3:
1670 type-qualifier:
1671 const
1672 restrict
1673 volatile
1674 address-space-qualifier
1676 (restrict is new in C99.)
1678 GNU extensions:
1680 declaration-specifiers:
1681 attributes declaration-specifiers[opt]
1683 type-qualifier:
1684 address-space
1686 address-space:
1687 identifier recognized by the target
1689 storage-class-specifier:
1690 __thread
1692 type-specifier:
1693 typeof-specifier
1694 __int128
1695 _Decimal32
1696 _Decimal64
1697 _Decimal128
1698 _Fract
1699 _Accum
1700 _Sat
1702 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1703 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1705 Objective-C:
1707 type-specifier:
1708 class-name objc-protocol-refs[opt]
1709 typedef-name objc-protocol-refs
1710 objc-protocol-refs
1713 static void
1714 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1715 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1717 bool attrs_ok = start_attr_ok;
1718 bool seen_type = specs->type_seen_p;
1719 while (c_parser_next_token_is (parser, CPP_NAME)
1720 || c_parser_next_token_is (parser, CPP_KEYWORD)
1721 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1723 struct c_typespec t;
1724 tree attrs;
1725 location_t loc = c_parser_peek_token (parser)->location;
1726 if (c_parser_next_token_is (parser, CPP_NAME))
1728 tree value = c_parser_peek_token (parser)->value;
1729 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1731 if (kind == C_ID_ADDRSPACE)
1733 addr_space_t as
1734 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1735 declspecs_add_addrspace (specs, as);
1736 c_parser_consume_token (parser);
1737 attrs_ok = true;
1738 continue;
1741 /* This finishes the specifiers unless a type name is OK, it
1742 is declared as a type name and a type name hasn't yet
1743 been seen. */
1744 if (!typespec_ok || seen_type
1745 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1746 break;
1747 c_parser_consume_token (parser);
1748 seen_type = true;
1749 attrs_ok = true;
1750 if (kind == C_ID_TYPENAME
1751 && (!c_dialect_objc ()
1752 || c_parser_next_token_is_not (parser, CPP_LESS)))
1754 t.kind = ctsk_typedef;
1755 /* For a typedef name, record the meaning, not the name.
1756 In case of 'foo foo, bar;'. */
1757 t.spec = lookup_name (value);
1758 t.expr = NULL_TREE;
1759 t.expr_const_operands = true;
1761 else
1763 tree proto = NULL_TREE;
1764 gcc_assert (c_dialect_objc ());
1765 t.kind = ctsk_objc;
1766 if (c_parser_next_token_is (parser, CPP_LESS))
1767 proto = c_parser_objc_protocol_refs (parser);
1768 t.spec = objc_get_protocol_qualified_type (value, proto);
1769 t.expr = NULL_TREE;
1770 t.expr_const_operands = true;
1772 declspecs_add_type (loc, specs, t);
1773 continue;
1775 if (c_parser_next_token_is (parser, CPP_LESS))
1777 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1778 nisse@lysator.liu.se. */
1779 tree proto;
1780 gcc_assert (c_dialect_objc ());
1781 if (!typespec_ok || seen_type)
1782 break;
1783 proto = c_parser_objc_protocol_refs (parser);
1784 t.kind = ctsk_objc;
1785 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1786 t.expr = NULL_TREE;
1787 t.expr_const_operands = true;
1788 declspecs_add_type (loc, specs, t);
1789 continue;
1791 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1792 switch (c_parser_peek_token (parser)->keyword)
1794 case RID_STATIC:
1795 case RID_EXTERN:
1796 case RID_REGISTER:
1797 case RID_TYPEDEF:
1798 case RID_INLINE:
1799 case RID_AUTO:
1800 case RID_THREAD:
1801 if (!scspec_ok)
1802 goto out;
1803 attrs_ok = true;
1804 /* TODO: Distinguish between function specifiers (inline)
1805 and storage class specifiers, either here or in
1806 declspecs_add_scspec. */
1807 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1808 c_parser_consume_token (parser);
1809 break;
1810 case RID_UNSIGNED:
1811 case RID_LONG:
1812 case RID_INT128:
1813 case RID_SHORT:
1814 case RID_SIGNED:
1815 case RID_COMPLEX:
1816 case RID_INT:
1817 case RID_CHAR:
1818 case RID_FLOAT:
1819 case RID_DOUBLE:
1820 case RID_VOID:
1821 case RID_DFLOAT32:
1822 case RID_DFLOAT64:
1823 case RID_DFLOAT128:
1824 case RID_BOOL:
1825 case RID_FRACT:
1826 case RID_ACCUM:
1827 case RID_SAT:
1828 if (!typespec_ok)
1829 goto out;
1830 attrs_ok = true;
1831 seen_type = true;
1832 if (c_dialect_objc ())
1833 parser->objc_need_raw_identifier = true;
1834 t.kind = ctsk_resword;
1835 t.spec = c_parser_peek_token (parser)->value;
1836 t.expr = NULL_TREE;
1837 t.expr_const_operands = true;
1838 declspecs_add_type (loc, specs, t);
1839 c_parser_consume_token (parser);
1840 break;
1841 case RID_ENUM:
1842 if (!typespec_ok)
1843 goto out;
1844 attrs_ok = true;
1845 seen_type = true;
1846 t = c_parser_enum_specifier (parser);
1847 declspecs_add_type (loc, specs, t);
1848 break;
1849 case RID_STRUCT:
1850 case RID_UNION:
1851 if (!typespec_ok)
1852 goto out;
1853 attrs_ok = true;
1854 seen_type = true;
1855 t = c_parser_struct_or_union_specifier (parser);
1856 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1857 declspecs_add_type (loc, specs, t);
1858 break;
1859 case RID_TYPEOF:
1860 /* ??? The old parser rejected typeof after other type
1861 specifiers, but is a syntax error the best way of
1862 handling this? */
1863 if (!typespec_ok || seen_type)
1864 goto out;
1865 attrs_ok = true;
1866 seen_type = true;
1867 t = c_parser_typeof_specifier (parser);
1868 declspecs_add_type (loc, specs, t);
1869 break;
1870 case RID_CONST:
1871 case RID_VOLATILE:
1872 case RID_RESTRICT:
1873 attrs_ok = true;
1874 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1875 c_parser_consume_token (parser);
1876 break;
1877 case RID_ATTRIBUTE:
1878 if (!attrs_ok)
1879 goto out;
1880 attrs = c_parser_attributes (parser);
1881 declspecs_add_attrs (specs, attrs);
1882 break;
1883 default:
1884 goto out;
1887 out: ;
1890 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1892 enum-specifier:
1893 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1894 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1895 enum attributes[opt] identifier
1897 The form with trailing comma is new in C99. The forms with
1898 attributes are GNU extensions. In GNU C, we accept any expression
1899 without commas in the syntax (assignment expressions, not just
1900 conditional expressions); assignment expressions will be diagnosed
1901 as non-constant.
1903 enumerator-list:
1904 enumerator
1905 enumerator-list , enumerator
1907 enumerator:
1908 enumeration-constant
1909 enumeration-constant = constant-expression
1912 static struct c_typespec
1913 c_parser_enum_specifier (c_parser *parser)
1915 struct c_typespec ret;
1916 tree attrs;
1917 tree ident = NULL_TREE;
1918 location_t enum_loc;
1919 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1920 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1921 enum_loc = c_parser_peek_token (parser)->location;
1922 c_parser_consume_token (parser);
1923 attrs = c_parser_attributes (parser);
1924 enum_loc = c_parser_peek_token (parser)->location;
1925 /* Set the location in case we create a decl now. */
1926 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1927 if (c_parser_next_token_is (parser, CPP_NAME))
1929 ident = c_parser_peek_token (parser)->value;
1930 ident_loc = c_parser_peek_token (parser)->location;
1931 enum_loc = ident_loc;
1932 c_parser_consume_token (parser);
1934 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1936 /* Parse an enum definition. */
1937 struct c_enum_contents the_enum;
1938 tree type = start_enum (enum_loc, &the_enum, ident);
1939 tree postfix_attrs;
1940 /* We chain the enumerators in reverse order, then put them in
1941 forward order at the end. */
1942 tree values = NULL_TREE;
1943 c_parser_consume_token (parser);
1944 while (true)
1946 tree enum_id;
1947 tree enum_value;
1948 tree enum_decl;
1949 bool seen_comma;
1950 c_token *token;
1951 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1952 location_t decl_loc, value_loc;
1953 if (c_parser_next_token_is_not (parser, CPP_NAME))
1955 c_parser_error (parser, "expected identifier");
1956 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1957 values = error_mark_node;
1958 break;
1960 token = c_parser_peek_token (parser);
1961 enum_id = token->value;
1962 /* Set the location in case we create a decl now. */
1963 c_parser_set_source_position_from_token (token);
1964 decl_loc = value_loc = token->location;
1965 c_parser_consume_token (parser);
1966 if (c_parser_next_token_is (parser, CPP_EQ))
1968 c_parser_consume_token (parser);
1969 value_loc = c_parser_peek_token (parser)->location;
1970 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1972 else
1973 enum_value = NULL_TREE;
1974 enum_decl = build_enumerator (decl_loc, value_loc,
1975 &the_enum, enum_id, enum_value);
1976 TREE_CHAIN (enum_decl) = values;
1977 values = enum_decl;
1978 seen_comma = false;
1979 if (c_parser_next_token_is (parser, CPP_COMMA))
1981 comma_loc = c_parser_peek_token (parser)->location;
1982 seen_comma = true;
1983 c_parser_consume_token (parser);
1985 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1987 if (seen_comma && !flag_isoc99)
1988 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1989 c_parser_consume_token (parser);
1990 break;
1992 if (!seen_comma)
1994 c_parser_error (parser, "expected %<,%> or %<}%>");
1995 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1996 values = error_mark_node;
1997 break;
2000 postfix_attrs = c_parser_attributes (parser);
2001 ret.spec = finish_enum (type, nreverse (values),
2002 chainon (attrs, postfix_attrs));
2003 ret.kind = ctsk_tagdef;
2004 ret.expr = NULL_TREE;
2005 ret.expr_const_operands = true;
2006 return ret;
2008 else if (!ident)
2010 c_parser_error (parser, "expected %<{%>");
2011 ret.spec = error_mark_node;
2012 ret.kind = ctsk_tagref;
2013 ret.expr = NULL_TREE;
2014 ret.expr_const_operands = true;
2015 return ret;
2017 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2018 /* In ISO C, enumerated types can be referred to only if already
2019 defined. */
2020 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2022 gcc_assert (ident);
2023 pedwarn (enum_loc, OPT_pedantic,
2024 "ISO C forbids forward references to %<enum%> types");
2026 return ret;
2029 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2031 struct-or-union-specifier:
2032 struct-or-union attributes[opt] identifier[opt]
2033 { struct-contents } attributes[opt]
2034 struct-or-union attributes[opt] identifier
2036 struct-contents:
2037 struct-declaration-list
2039 struct-declaration-list:
2040 struct-declaration ;
2041 struct-declaration-list struct-declaration ;
2043 GNU extensions:
2045 struct-contents:
2046 empty
2047 struct-declaration
2048 struct-declaration-list struct-declaration
2050 struct-declaration-list:
2051 struct-declaration-list ;
2054 (Note that in the syntax here, unlike that in ISO C, the semicolons
2055 are included here rather than in struct-declaration, in order to
2056 describe the syntax with extra semicolons and missing semicolon at
2057 end.)
2059 Objective-C:
2061 struct-declaration-list:
2062 @defs ( class-name )
2064 (Note this does not include a trailing semicolon, but can be
2065 followed by further declarations, and gets a pedwarn-if-pedantic
2066 when followed by a semicolon.) */
2068 static struct c_typespec
2069 c_parser_struct_or_union_specifier (c_parser *parser)
2071 struct c_typespec ret;
2072 tree attrs;
2073 tree ident = NULL_TREE;
2074 location_t struct_loc;
2075 location_t ident_loc = UNKNOWN_LOCATION;
2076 enum tree_code code;
2077 switch (c_parser_peek_token (parser)->keyword)
2079 case RID_STRUCT:
2080 code = RECORD_TYPE;
2081 break;
2082 case RID_UNION:
2083 code = UNION_TYPE;
2084 break;
2085 default:
2086 gcc_unreachable ();
2088 struct_loc = c_parser_peek_token (parser)->location;
2089 c_parser_consume_token (parser);
2090 attrs = c_parser_attributes (parser);
2092 /* Set the location in case we create a decl now. */
2093 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2095 if (c_parser_next_token_is (parser, CPP_NAME))
2097 ident = c_parser_peek_token (parser)->value;
2098 ident_loc = c_parser_peek_token (parser)->location;
2099 struct_loc = ident_loc;
2100 c_parser_consume_token (parser);
2102 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2104 /* Parse a struct or union definition. Start the scope of the
2105 tag before parsing components. */
2106 struct c_struct_parse_info *struct_info;
2107 tree type = start_struct (struct_loc, code, ident, &struct_info);
2108 tree postfix_attrs;
2109 /* We chain the components in reverse order, then put them in
2110 forward order at the end. Each struct-declaration may
2111 declare multiple components (comma-separated), so we must use
2112 chainon to join them, although when parsing each
2113 struct-declaration we can use TREE_CHAIN directly.
2115 The theory behind all this is that there will be more
2116 semicolon separated fields than comma separated fields, and
2117 so we'll be minimizing the number of node traversals required
2118 by chainon. */
2119 tree contents = NULL_TREE;
2120 c_parser_consume_token (parser);
2121 /* Handle the Objective-C @defs construct,
2122 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2123 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2125 tree name;
2126 gcc_assert (c_dialect_objc ());
2127 c_parser_consume_token (parser);
2128 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2129 goto end_at_defs;
2130 if (c_parser_next_token_is (parser, CPP_NAME)
2131 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2133 name = c_parser_peek_token (parser)->value;
2134 c_parser_consume_token (parser);
2136 else
2138 c_parser_error (parser, "expected class name");
2139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2140 goto end_at_defs;
2142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2143 "expected %<)%>");
2144 contents = nreverse (objc_get_class_ivars (name));
2146 end_at_defs:
2147 /* Parse the struct-declarations and semicolons. Problems with
2148 semicolons are diagnosed here; empty structures are diagnosed
2149 elsewhere. */
2150 while (true)
2152 tree decls;
2153 /* Parse any stray semicolon. */
2154 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2156 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2157 "extra semicolon in struct or union specified");
2158 c_parser_consume_token (parser);
2159 continue;
2161 /* Stop if at the end of the struct or union contents. */
2162 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2164 c_parser_consume_token (parser);
2165 break;
2167 /* Accept #pragmas at struct scope. */
2168 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2170 c_parser_pragma (parser, pragma_external);
2171 continue;
2173 /* Parse some comma-separated declarations, but not the
2174 trailing semicolon if any. */
2175 decls = c_parser_struct_declaration (parser);
2176 contents = chainon (decls, contents);
2177 /* If no semicolon follows, either we have a parse error or
2178 are at the end of the struct or union and should
2179 pedwarn. */
2180 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2181 c_parser_consume_token (parser);
2182 else
2184 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2185 pedwarn (c_parser_peek_token (parser)->location, 0,
2186 "no semicolon at end of struct or union");
2187 else
2189 c_parser_error (parser, "expected %<;%>");
2190 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2191 break;
2195 postfix_attrs = c_parser_attributes (parser);
2196 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2197 chainon (attrs, postfix_attrs), struct_info);
2198 ret.kind = ctsk_tagdef;
2199 ret.expr = NULL_TREE;
2200 ret.expr_const_operands = true;
2201 return ret;
2203 else if (!ident)
2205 c_parser_error (parser, "expected %<{%>");
2206 ret.spec = error_mark_node;
2207 ret.kind = ctsk_tagref;
2208 ret.expr = NULL_TREE;
2209 ret.expr_const_operands = true;
2210 return ret;
2212 ret = parser_xref_tag (ident_loc, code, ident);
2213 return ret;
2216 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2217 the trailing semicolon.
2219 struct-declaration:
2220 specifier-qualifier-list struct-declarator-list
2221 static_assert-declaration-no-semi
2223 specifier-qualifier-list:
2224 type-specifier specifier-qualifier-list[opt]
2225 type-qualifier specifier-qualifier-list[opt]
2226 attributes specifier-qualifier-list[opt]
2228 struct-declarator-list:
2229 struct-declarator
2230 struct-declarator-list , attributes[opt] struct-declarator
2232 struct-declarator:
2233 declarator attributes[opt]
2234 declarator[opt] : constant-expression attributes[opt]
2236 GNU extensions:
2238 struct-declaration:
2239 __extension__ struct-declaration
2240 specifier-qualifier-list
2242 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2243 of attributes where shown is a GNU extension. In GNU C, we accept
2244 any expression without commas in the syntax (assignment
2245 expressions, not just conditional expressions); assignment
2246 expressions will be diagnosed as non-constant. */
2248 static tree
2249 c_parser_struct_declaration (c_parser *parser)
2251 struct c_declspecs *specs;
2252 tree prefix_attrs;
2253 tree all_prefix_attrs;
2254 tree decls;
2255 location_t decl_loc;
2256 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2258 int ext;
2259 tree decl;
2260 ext = disable_extension_diagnostics ();
2261 c_parser_consume_token (parser);
2262 decl = c_parser_struct_declaration (parser);
2263 restore_extension_diagnostics (ext);
2264 return decl;
2266 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2268 c_parser_static_assert_declaration_no_semi (parser);
2269 return NULL_TREE;
2271 specs = build_null_declspecs ();
2272 decl_loc = c_parser_peek_token (parser)->location;
2273 c_parser_declspecs (parser, specs, false, true, true);
2274 if (parser->error)
2275 return NULL_TREE;
2276 if (!specs->declspecs_seen_p)
2278 c_parser_error (parser, "expected specifier-qualifier-list");
2279 return NULL_TREE;
2281 finish_declspecs (specs);
2282 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2284 tree ret;
2285 if (!specs->type_seen_p)
2287 pedwarn (decl_loc, OPT_pedantic,
2288 "ISO C forbids member declarations with no members");
2289 shadow_tag_warned (specs, pedantic);
2290 ret = NULL_TREE;
2292 else
2294 /* Support for unnamed structs or unions as members of
2295 structs or unions (which is [a] useful and [b] supports
2296 MS P-SDK). */
2297 tree attrs = NULL;
2299 ret = grokfield (c_parser_peek_token (parser)->location,
2300 build_id_declarator (NULL_TREE), specs,
2301 NULL_TREE, &attrs);
2302 if (ret)
2303 decl_attributes (&ret, attrs, 0);
2305 return ret;
2307 pending_xref_error ();
2308 prefix_attrs = specs->attrs;
2309 all_prefix_attrs = prefix_attrs;
2310 specs->attrs = NULL_TREE;
2311 decls = NULL_TREE;
2312 while (true)
2314 /* Declaring one or more declarators or un-named bit-fields. */
2315 struct c_declarator *declarator;
2316 bool dummy = false;
2317 if (c_parser_next_token_is (parser, CPP_COLON))
2318 declarator = build_id_declarator (NULL_TREE);
2319 else
2320 declarator = c_parser_declarator (parser, specs->type_seen_p,
2321 C_DTR_NORMAL, &dummy);
2322 if (declarator == NULL)
2324 c_parser_skip_to_end_of_block_or_statement (parser);
2325 break;
2327 if (c_parser_next_token_is (parser, CPP_COLON)
2328 || c_parser_next_token_is (parser, CPP_COMMA)
2329 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2330 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2331 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2333 tree postfix_attrs = NULL_TREE;
2334 tree width = NULL_TREE;
2335 tree d;
2336 if (c_parser_next_token_is (parser, CPP_COLON))
2338 c_parser_consume_token (parser);
2339 width = c_parser_expr_no_commas (parser, NULL).value;
2341 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2342 postfix_attrs = c_parser_attributes (parser);
2343 d = grokfield (c_parser_peek_token (parser)->location,
2344 declarator, specs, width, &all_prefix_attrs);
2345 decl_attributes (&d, chainon (postfix_attrs,
2346 all_prefix_attrs), 0);
2347 DECL_CHAIN (d) = decls;
2348 decls = d;
2349 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2350 all_prefix_attrs = chainon (c_parser_attributes (parser),
2351 prefix_attrs);
2352 else
2353 all_prefix_attrs = prefix_attrs;
2354 if (c_parser_next_token_is (parser, CPP_COMMA))
2355 c_parser_consume_token (parser);
2356 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2357 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2359 /* Semicolon consumed in caller. */
2360 break;
2362 else
2364 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2365 break;
2368 else
2370 c_parser_error (parser,
2371 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2372 "%<__attribute__%>");
2373 break;
2376 return decls;
2379 /* Parse a typeof specifier (a GNU extension).
2381 typeof-specifier:
2382 typeof ( expression )
2383 typeof ( type-name )
2386 static struct c_typespec
2387 c_parser_typeof_specifier (c_parser *parser)
2389 struct c_typespec ret;
2390 ret.kind = ctsk_typeof;
2391 ret.spec = error_mark_node;
2392 ret.expr = NULL_TREE;
2393 ret.expr_const_operands = true;
2394 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2395 c_parser_consume_token (parser);
2396 c_inhibit_evaluation_warnings++;
2397 in_typeof++;
2398 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2400 c_inhibit_evaluation_warnings--;
2401 in_typeof--;
2402 return ret;
2404 if (c_parser_next_token_starts_typename (parser))
2406 struct c_type_name *type = c_parser_type_name (parser);
2407 c_inhibit_evaluation_warnings--;
2408 in_typeof--;
2409 if (type != NULL)
2411 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2412 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2415 else
2417 bool was_vm;
2418 location_t here = c_parser_peek_token (parser)->location;
2419 struct c_expr expr = c_parser_expression (parser);
2420 c_inhibit_evaluation_warnings--;
2421 in_typeof--;
2422 if (TREE_CODE (expr.value) == COMPONENT_REF
2423 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2424 error_at (here, "%<typeof%> applied to a bit-field");
2425 mark_exp_read (expr.value);
2426 ret.spec = TREE_TYPE (expr.value);
2427 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2428 /* This is returned with the type so that when the type is
2429 evaluated, this can be evaluated. */
2430 if (was_vm)
2431 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2432 pop_maybe_used (was_vm);
2434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2435 return ret;
2438 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2439 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2440 be redeclared; otherwise it may not. KIND indicates which kind of
2441 declarator is wanted. Returns a valid declarator except in the
2442 case of a syntax error in which case NULL is returned. *SEEN_ID is
2443 set to true if an identifier being declared is seen; this is used
2444 to diagnose bad forms of abstract array declarators and to
2445 determine whether an identifier list is syntactically permitted.
2447 declarator:
2448 pointer[opt] direct-declarator
2450 direct-declarator:
2451 identifier
2452 ( attributes[opt] declarator )
2453 direct-declarator array-declarator
2454 direct-declarator ( parameter-type-list )
2455 direct-declarator ( identifier-list[opt] )
2457 pointer:
2458 * type-qualifier-list[opt]
2459 * type-qualifier-list[opt] pointer
2461 type-qualifier-list:
2462 type-qualifier
2463 attributes
2464 type-qualifier-list type-qualifier
2465 type-qualifier-list attributes
2467 parameter-type-list:
2468 parameter-list
2469 parameter-list , ...
2471 parameter-list:
2472 parameter-declaration
2473 parameter-list , parameter-declaration
2475 parameter-declaration:
2476 declaration-specifiers declarator attributes[opt]
2477 declaration-specifiers abstract-declarator[opt] attributes[opt]
2479 identifier-list:
2480 identifier
2481 identifier-list , identifier
2483 abstract-declarator:
2484 pointer
2485 pointer[opt] direct-abstract-declarator
2487 direct-abstract-declarator:
2488 ( attributes[opt] abstract-declarator )
2489 direct-abstract-declarator[opt] array-declarator
2490 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2492 GNU extensions:
2494 direct-declarator:
2495 direct-declarator ( parameter-forward-declarations
2496 parameter-type-list[opt] )
2498 direct-abstract-declarator:
2499 direct-abstract-declarator[opt] ( parameter-forward-declarations
2500 parameter-type-list[opt] )
2502 parameter-forward-declarations:
2503 parameter-list ;
2504 parameter-forward-declarations parameter-list ;
2506 The uses of attributes shown above are GNU extensions.
2508 Some forms of array declarator are not included in C99 in the
2509 syntax for abstract declarators; these are disallowed elsewhere.
2510 This may be a defect (DR#289).
2512 This function also accepts an omitted abstract declarator as being
2513 an abstract declarator, although not part of the formal syntax. */
2515 static struct c_declarator *
2516 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2517 bool *seen_id)
2519 /* Parse any initial pointer part. */
2520 if (c_parser_next_token_is (parser, CPP_MULT))
2522 struct c_declspecs *quals_attrs = build_null_declspecs ();
2523 struct c_declarator *inner;
2524 c_parser_consume_token (parser);
2525 c_parser_declspecs (parser, quals_attrs, false, false, true);
2526 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2527 if (inner == NULL)
2528 return NULL;
2529 else
2530 return make_pointer_declarator (quals_attrs, inner);
2532 /* Now we have a direct declarator, direct abstract declarator or
2533 nothing (which counts as a direct abstract declarator here). */
2534 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2537 /* Parse a direct declarator or direct abstract declarator; arguments
2538 as c_parser_declarator. */
2540 static struct c_declarator *
2541 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2542 bool *seen_id)
2544 /* The direct declarator must start with an identifier (possibly
2545 omitted) or a parenthesized declarator (possibly abstract). In
2546 an ordinary declarator, initial parentheses must start a
2547 parenthesized declarator. In an abstract declarator or parameter
2548 declarator, they could start a parenthesized declarator or a
2549 parameter list. To tell which, the open parenthesis and any
2550 following attributes must be read. If a declaration specifier
2551 follows, then it is a parameter list; if the specifier is a
2552 typedef name, there might be an ambiguity about redeclaring it,
2553 which is resolved in the direction of treating it as a typedef
2554 name. If a close parenthesis follows, it is also an empty
2555 parameter list, as the syntax does not permit empty abstract
2556 declarators. Otherwise, it is a parenthesized declarator (in
2557 which case the analysis may be repeated inside it, recursively).
2559 ??? There is an ambiguity in a parameter declaration "int
2560 (__attribute__((foo)) x)", where x is not a typedef name: it
2561 could be an abstract declarator for a function, or declare x with
2562 parentheses. The proper resolution of this ambiguity needs
2563 documenting. At present we follow an accident of the old
2564 parser's implementation, whereby the first parameter must have
2565 some declaration specifiers other than just attributes. Thus as
2566 a parameter declaration it is treated as a parenthesized
2567 parameter named x, and as an abstract declarator it is
2568 rejected.
2570 ??? Also following the old parser, attributes inside an empty
2571 parameter list are ignored, making it a list not yielding a
2572 prototype, rather than giving an error or making it have one
2573 parameter with implicit type int.
2575 ??? Also following the old parser, typedef names may be
2576 redeclared in declarators, but not Objective-C class names. */
2578 if (kind != C_DTR_ABSTRACT
2579 && c_parser_next_token_is (parser, CPP_NAME)
2580 && ((type_seen_p
2581 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2582 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2583 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2585 struct c_declarator *inner
2586 = build_id_declarator (c_parser_peek_token (parser)->value);
2587 *seen_id = true;
2588 inner->id_loc = c_parser_peek_token (parser)->location;
2589 c_parser_consume_token (parser);
2590 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2593 if (kind != C_DTR_NORMAL
2594 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2596 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2597 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2600 /* Either we are at the end of an abstract declarator, or we have
2601 parentheses. */
2603 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2605 tree attrs;
2606 struct c_declarator *inner;
2607 c_parser_consume_token (parser);
2608 attrs = c_parser_attributes (parser);
2609 if (kind != C_DTR_NORMAL
2610 && (c_parser_next_token_starts_declspecs (parser)
2611 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2613 struct c_arg_info *args
2614 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2615 attrs);
2616 if (args == NULL)
2617 return NULL;
2618 else
2620 inner
2621 = build_function_declarator (args,
2622 build_id_declarator (NULL_TREE));
2623 return c_parser_direct_declarator_inner (parser, *seen_id,
2624 inner);
2627 /* A parenthesized declarator. */
2628 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2629 if (inner != NULL && attrs != NULL)
2630 inner = build_attrs_declarator (attrs, inner);
2631 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2633 c_parser_consume_token (parser);
2634 if (inner == NULL)
2635 return NULL;
2636 else
2637 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2639 else
2641 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2642 "expected %<)%>");
2643 return NULL;
2646 else
2648 if (kind == C_DTR_NORMAL)
2650 c_parser_error (parser, "expected identifier or %<(%>");
2651 return NULL;
2653 else
2654 return build_id_declarator (NULL_TREE);
2658 /* Parse part of a direct declarator or direct abstract declarator,
2659 given that some (in INNER) has already been parsed; ID_PRESENT is
2660 true if an identifier is present, false for an abstract
2661 declarator. */
2663 static struct c_declarator *
2664 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2665 struct c_declarator *inner)
2667 /* Parse a sequence of array declarators and parameter lists. */
2668 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2670 location_t brace_loc = c_parser_peek_token (parser)->location;
2671 struct c_declarator *declarator;
2672 struct c_declspecs *quals_attrs = build_null_declspecs ();
2673 bool static_seen;
2674 bool star_seen;
2675 tree dimen;
2676 c_parser_consume_token (parser);
2677 c_parser_declspecs (parser, quals_attrs, false, false, true);
2678 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2679 if (static_seen)
2680 c_parser_consume_token (parser);
2681 if (static_seen && !quals_attrs->declspecs_seen_p)
2682 c_parser_declspecs (parser, quals_attrs, false, false, true);
2683 if (!quals_attrs->declspecs_seen_p)
2684 quals_attrs = NULL;
2685 /* If "static" is present, there must be an array dimension.
2686 Otherwise, there may be a dimension, "*", or no
2687 dimension. */
2688 if (static_seen)
2690 star_seen = false;
2691 dimen = c_parser_expr_no_commas (parser, NULL).value;
2693 else
2695 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2697 dimen = NULL_TREE;
2698 star_seen = false;
2700 else if (c_parser_next_token_is (parser, CPP_MULT))
2702 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2704 dimen = NULL_TREE;
2705 star_seen = true;
2706 c_parser_consume_token (parser);
2708 else
2710 star_seen = false;
2711 dimen = c_parser_expr_no_commas (parser, NULL).value;
2714 else
2716 star_seen = false;
2717 dimen = c_parser_expr_no_commas (parser, NULL).value;
2720 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2721 c_parser_consume_token (parser);
2722 else
2724 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2725 "expected %<]%>");
2726 return NULL;
2728 if (dimen)
2729 mark_exp_read (dimen);
2730 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2731 static_seen, star_seen);
2732 if (declarator == NULL)
2733 return NULL;
2734 inner = set_array_declarator_inner (declarator, inner);
2735 return c_parser_direct_declarator_inner (parser, id_present, inner);
2737 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2739 tree attrs;
2740 struct c_arg_info *args;
2741 c_parser_consume_token (parser);
2742 attrs = c_parser_attributes (parser);
2743 args = c_parser_parms_declarator (parser, id_present, attrs);
2744 if (args == NULL)
2745 return NULL;
2746 else
2748 inner = build_function_declarator (args, inner);
2749 return c_parser_direct_declarator_inner (parser, id_present, inner);
2752 return inner;
2755 /* Parse a parameter list or identifier list, including the closing
2756 parenthesis but not the opening one. ATTRS are the attributes at
2757 the start of the list. ID_LIST_OK is true if an identifier list is
2758 acceptable; such a list must not have attributes at the start. */
2760 static struct c_arg_info *
2761 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2763 push_scope ();
2764 declare_parm_level ();
2765 /* If the list starts with an identifier, it is an identifier list.
2766 Otherwise, it is either a prototype list or an empty list. */
2767 if (id_list_ok
2768 && !attrs
2769 && c_parser_next_token_is (parser, CPP_NAME)
2770 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2772 tree list = NULL_TREE, *nextp = &list;
2773 while (c_parser_next_token_is (parser, CPP_NAME)
2774 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2776 *nextp = build_tree_list (NULL_TREE,
2777 c_parser_peek_token (parser)->value);
2778 nextp = & TREE_CHAIN (*nextp);
2779 c_parser_consume_token (parser);
2780 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2781 break;
2782 c_parser_consume_token (parser);
2783 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2785 c_parser_error (parser, "expected identifier");
2786 break;
2789 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2791 struct c_arg_info *ret = build_arg_info ();
2792 ret->types = list;
2793 c_parser_consume_token (parser);
2794 pop_scope ();
2795 return ret;
2797 else
2799 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2800 "expected %<)%>");
2801 pop_scope ();
2802 return NULL;
2805 else
2807 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2808 pop_scope ();
2809 return ret;
2813 /* Parse a parameter list (possibly empty), including the closing
2814 parenthesis but not the opening one. ATTRS are the attributes at
2815 the start of the list. */
2817 static struct c_arg_info *
2818 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2820 bool bad_parm = false;
2821 /* ??? Following the old parser, forward parameter declarations may
2822 use abstract declarators, and if no real parameter declarations
2823 follow the forward declarations then this is not diagnosed. Also
2824 note as above that attributes are ignored as the only contents of
2825 the parentheses, or as the only contents after forward
2826 declarations. */
2827 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2829 struct c_arg_info *ret = build_arg_info ();
2830 c_parser_consume_token (parser);
2831 return ret;
2833 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2835 struct c_arg_info *ret = build_arg_info ();
2836 /* Suppress -Wold-style-definition for this case. */
2837 ret->types = error_mark_node;
2838 error_at (c_parser_peek_token (parser)->location,
2839 "ISO C requires a named argument before %<...%>");
2840 c_parser_consume_token (parser);
2841 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2843 c_parser_consume_token (parser);
2844 return ret;
2846 else
2848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2849 "expected %<)%>");
2850 return NULL;
2853 /* Nonempty list of parameters, either terminated with semicolon
2854 (forward declarations; recurse) or with close parenthesis (normal
2855 function) or with ", ... )" (variadic function). */
2856 while (true)
2858 /* Parse a parameter. */
2859 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2860 attrs = NULL_TREE;
2861 if (parm == NULL)
2862 bad_parm = true;
2863 else
2864 push_parm_decl (parm);
2865 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2867 tree new_attrs;
2868 c_parser_consume_token (parser);
2869 mark_forward_parm_decls ();
2870 new_attrs = c_parser_attributes (parser);
2871 return c_parser_parms_list_declarator (parser, new_attrs);
2873 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2875 c_parser_consume_token (parser);
2876 if (bad_parm)
2878 get_pending_sizes ();
2879 return NULL;
2881 else
2882 return get_parm_info (false);
2884 if (!c_parser_require (parser, CPP_COMMA,
2885 "expected %<;%>, %<,%> or %<)%>"))
2887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2888 get_pending_sizes ();
2889 return NULL;
2891 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2893 c_parser_consume_token (parser);
2894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2896 c_parser_consume_token (parser);
2897 if (bad_parm)
2899 get_pending_sizes ();
2900 return NULL;
2902 else
2903 return get_parm_info (true);
2905 else
2907 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2908 "expected %<)%>");
2909 get_pending_sizes ();
2910 return NULL;
2916 /* Parse a parameter declaration. ATTRS are the attributes at the
2917 start of the declaration if it is the first parameter. */
2919 static struct c_parm *
2920 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2922 struct c_declspecs *specs;
2923 struct c_declarator *declarator;
2924 tree prefix_attrs;
2925 tree postfix_attrs = NULL_TREE;
2926 bool dummy = false;
2927 if (!c_parser_next_token_starts_declspecs (parser))
2929 c_token *token = c_parser_peek_token (parser);
2930 if (parser->error)
2931 return NULL;
2932 c_parser_set_source_position_from_token (token);
2933 if (token->type == CPP_NAME
2934 && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
2935 && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
2937 error ("unknown type name %qE", token->value);
2938 parser->error = true;
2940 /* ??? In some Objective-C cases '...' isn't applicable so there
2941 should be a different message. */
2942 else
2943 c_parser_error (parser,
2944 "expected declaration specifiers or %<...%>");
2945 c_parser_skip_to_end_of_parameter (parser);
2946 return NULL;
2948 specs = build_null_declspecs ();
2949 if (attrs)
2951 declspecs_add_attrs (specs, attrs);
2952 attrs = NULL_TREE;
2954 c_parser_declspecs (parser, specs, true, true, true);
2955 finish_declspecs (specs);
2956 pending_xref_error ();
2957 prefix_attrs = specs->attrs;
2958 specs->attrs = NULL_TREE;
2959 declarator = c_parser_declarator (parser, specs->type_seen_p,
2960 C_DTR_PARM, &dummy);
2961 if (declarator == NULL)
2963 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2964 return NULL;
2966 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2967 postfix_attrs = c_parser_attributes (parser);
2968 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2969 declarator);
2972 /* Parse a string literal in an asm expression. It should not be
2973 translated, and wide string literals are an error although
2974 permitted by the syntax. This is a GNU extension.
2976 asm-string-literal:
2977 string-literal
2979 ??? At present, following the old parser, the caller needs to have
2980 set lex_untranslated_string to 1. It would be better to follow the
2981 C++ parser rather than using this kludge. */
2983 static tree
2984 c_parser_asm_string_literal (c_parser *parser)
2986 tree str;
2987 if (c_parser_next_token_is (parser, CPP_STRING))
2989 str = c_parser_peek_token (parser)->value;
2990 c_parser_consume_token (parser);
2992 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2994 error_at (c_parser_peek_token (parser)->location,
2995 "wide string literal in %<asm%>");
2996 str = build_string (1, "");
2997 c_parser_consume_token (parser);
2999 else
3001 c_parser_error (parser, "expected string literal");
3002 str = NULL_TREE;
3004 return str;
3007 /* Parse a simple asm expression. This is used in restricted
3008 contexts, where a full expression with inputs and outputs does not
3009 make sense. This is a GNU extension.
3011 simple-asm-expr:
3012 asm ( asm-string-literal )
3015 static tree
3016 c_parser_simple_asm_expr (c_parser *parser)
3018 tree str;
3019 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3020 /* ??? Follow the C++ parser rather than using the
3021 lex_untranslated_string kludge. */
3022 parser->lex_untranslated_string = true;
3023 c_parser_consume_token (parser);
3024 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3026 parser->lex_untranslated_string = false;
3027 return NULL_TREE;
3029 str = c_parser_asm_string_literal (parser);
3030 parser->lex_untranslated_string = false;
3031 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3033 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3034 return NULL_TREE;
3036 return str;
3039 /* Parse (possibly empty) attributes. This is a GNU extension.
3041 attributes:
3042 empty
3043 attributes attribute
3045 attribute:
3046 __attribute__ ( ( attribute-list ) )
3048 attribute-list:
3049 attrib
3050 attribute_list , attrib
3052 attrib:
3053 empty
3054 any-word
3055 any-word ( identifier )
3056 any-word ( identifier , nonempty-expr-list )
3057 any-word ( expr-list )
3059 where the "identifier" must not be declared as a type, and
3060 "any-word" may be any identifier (including one declared as a
3061 type), a reserved word storage class specifier, type specifier or
3062 type qualifier. ??? This still leaves out most reserved keywords
3063 (following the old parser), shouldn't we include them, and why not
3064 allow identifiers declared as types to start the arguments? */
3066 static tree
3067 c_parser_attributes (c_parser *parser)
3069 tree attrs = NULL_TREE;
3070 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3072 /* ??? Follow the C++ parser rather than using the
3073 lex_untranslated_string kludge. */
3074 parser->lex_untranslated_string = true;
3075 c_parser_consume_token (parser);
3076 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3078 parser->lex_untranslated_string = false;
3079 return attrs;
3081 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3083 parser->lex_untranslated_string = false;
3084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3085 return attrs;
3087 /* Parse the attribute list. */
3088 while (c_parser_next_token_is (parser, CPP_COMMA)
3089 || c_parser_next_token_is (parser, CPP_NAME)
3090 || c_parser_next_token_is (parser, CPP_KEYWORD))
3092 tree attr, attr_name, attr_args;
3093 VEC(tree,gc) *expr_list;
3094 if (c_parser_next_token_is (parser, CPP_COMMA))
3096 c_parser_consume_token (parser);
3097 continue;
3099 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3101 /* ??? See comment above about what keywords are
3102 accepted here. */
3103 bool ok;
3104 switch (c_parser_peek_token (parser)->keyword)
3106 case RID_STATIC:
3107 case RID_UNSIGNED:
3108 case RID_LONG:
3109 case RID_INT128:
3110 case RID_CONST:
3111 case RID_EXTERN:
3112 case RID_REGISTER:
3113 case RID_TYPEDEF:
3114 case RID_SHORT:
3115 case RID_INLINE:
3116 case RID_VOLATILE:
3117 case RID_SIGNED:
3118 case RID_AUTO:
3119 case RID_RESTRICT:
3120 case RID_COMPLEX:
3121 case RID_THREAD:
3122 case RID_INT:
3123 case RID_CHAR:
3124 case RID_FLOAT:
3125 case RID_DOUBLE:
3126 case RID_VOID:
3127 case RID_DFLOAT32:
3128 case RID_DFLOAT64:
3129 case RID_DFLOAT128:
3130 case RID_BOOL:
3131 case RID_FRACT:
3132 case RID_ACCUM:
3133 case RID_SAT:
3134 ok = true;
3135 break;
3136 default:
3137 ok = false;
3138 break;
3140 if (!ok)
3141 break;
3142 /* Accept __attribute__((__const)) as __attribute__((const))
3143 etc. */
3144 attr_name
3145 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3147 else
3148 attr_name = c_parser_peek_token (parser)->value;
3149 c_parser_consume_token (parser);
3150 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3152 attr = build_tree_list (attr_name, NULL_TREE);
3153 attrs = chainon (attrs, attr);
3154 continue;
3156 c_parser_consume_token (parser);
3157 /* Parse the attribute contents. If they start with an
3158 identifier which is followed by a comma or close
3159 parenthesis, then the arguments start with that
3160 identifier; otherwise they are an expression list. */
3161 if (c_parser_next_token_is (parser, CPP_NAME)
3162 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3163 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3164 || (c_parser_peek_2nd_token (parser)->type
3165 == CPP_CLOSE_PAREN)))
3167 tree arg1 = c_parser_peek_token (parser)->value;
3168 c_parser_consume_token (parser);
3169 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3170 attr_args = build_tree_list (NULL_TREE, arg1);
3171 else
3173 tree tree_list;
3174 c_parser_consume_token (parser);
3175 expr_list = c_parser_expr_list (parser, false, true, NULL);
3176 tree_list = build_tree_list_vec (expr_list);
3177 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3178 release_tree_vector (expr_list);
3181 else
3183 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3184 attr_args = NULL_TREE;
3185 else
3187 expr_list = c_parser_expr_list (parser, false, true, NULL);
3188 attr_args = build_tree_list_vec (expr_list);
3189 release_tree_vector (expr_list);
3192 attr = build_tree_list (attr_name, attr_args);
3193 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3194 c_parser_consume_token (parser);
3195 else
3197 parser->lex_untranslated_string = false;
3198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3199 "expected %<)%>");
3200 return attrs;
3202 attrs = chainon (attrs, attr);
3204 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3205 c_parser_consume_token (parser);
3206 else
3208 parser->lex_untranslated_string = false;
3209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3210 "expected %<)%>");
3211 return attrs;
3213 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3214 c_parser_consume_token (parser);
3215 else
3217 parser->lex_untranslated_string = false;
3218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3219 "expected %<)%>");
3220 return attrs;
3222 parser->lex_untranslated_string = false;
3224 return attrs;
3227 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3229 type-name:
3230 specifier-qualifier-list abstract-declarator[opt]
3233 static struct c_type_name *
3234 c_parser_type_name (c_parser *parser)
3236 struct c_declspecs *specs = build_null_declspecs ();
3237 struct c_declarator *declarator;
3238 struct c_type_name *ret;
3239 bool dummy = false;
3240 c_parser_declspecs (parser, specs, false, true, true);
3241 if (!specs->declspecs_seen_p)
3243 c_parser_error (parser, "expected specifier-qualifier-list");
3244 return NULL;
3246 pending_xref_error ();
3247 finish_declspecs (specs);
3248 declarator = c_parser_declarator (parser, specs->type_seen_p,
3249 C_DTR_ABSTRACT, &dummy);
3250 if (declarator == NULL)
3251 return NULL;
3252 ret = XOBNEW (&parser_obstack, struct c_type_name);
3253 ret->specs = specs;
3254 ret->declarator = declarator;
3255 return ret;
3258 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3260 initializer:
3261 assignment-expression
3262 { initializer-list }
3263 { initializer-list , }
3265 initializer-list:
3266 designation[opt] initializer
3267 initializer-list , designation[opt] initializer
3269 designation:
3270 designator-list =
3272 designator-list:
3273 designator
3274 designator-list designator
3276 designator:
3277 array-designator
3278 . identifier
3280 array-designator:
3281 [ constant-expression ]
3283 GNU extensions:
3285 initializer:
3288 designation:
3289 array-designator
3290 identifier :
3292 array-designator:
3293 [ constant-expression ... constant-expression ]
3295 Any expression without commas is accepted in the syntax for the
3296 constant-expressions, with non-constant expressions rejected later.
3298 This function is only used for top-level initializers; for nested
3299 ones, see c_parser_initval. */
3301 static struct c_expr
3302 c_parser_initializer (c_parser *parser)
3304 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3305 return c_parser_braced_init (parser, NULL_TREE, false);
3306 else
3308 struct c_expr ret;
3309 location_t loc = c_parser_peek_token (parser)->location;
3310 ret = c_parser_expr_no_commas (parser, NULL);
3311 if (TREE_CODE (ret.value) != STRING_CST
3312 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3313 ret = default_function_array_read_conversion (loc, ret);
3314 return ret;
3318 /* Parse a braced initializer list. TYPE is the type specified for a
3319 compound literal, and NULL_TREE for other initializers and for
3320 nested braced lists. NESTED_P is true for nested braced lists,
3321 false for the list of a compound literal or the list that is the
3322 top-level initializer in a declaration. */
3324 static struct c_expr
3325 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3327 struct c_expr ret;
3328 struct obstack braced_init_obstack;
3329 location_t brace_loc = c_parser_peek_token (parser)->location;
3330 gcc_obstack_init (&braced_init_obstack);
3331 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3332 c_parser_consume_token (parser);
3333 if (nested_p)
3334 push_init_level (0, &braced_init_obstack);
3335 else
3336 really_start_incremental_init (type);
3337 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3339 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3341 else
3343 /* Parse a non-empty initializer list, possibly with a trailing
3344 comma. */
3345 while (true)
3347 c_parser_initelt (parser, &braced_init_obstack);
3348 if (parser->error)
3349 break;
3350 if (c_parser_next_token_is (parser, CPP_COMMA))
3351 c_parser_consume_token (parser);
3352 else
3353 break;
3354 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3355 break;
3358 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3360 ret.value = error_mark_node;
3361 ret.original_code = ERROR_MARK;
3362 ret.original_type = NULL;
3363 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3364 pop_init_level (0, &braced_init_obstack);
3365 obstack_free (&braced_init_obstack, NULL);
3366 return ret;
3368 c_parser_consume_token (parser);
3369 ret = pop_init_level (0, &braced_init_obstack);
3370 obstack_free (&braced_init_obstack, NULL);
3371 return ret;
3374 /* Parse a nested initializer, including designators. */
3376 static void
3377 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3379 /* Parse any designator or designator list. A single array
3380 designator may have the subsequent "=" omitted in GNU C, but a
3381 longer list or a structure member designator may not. */
3382 if (c_parser_next_token_is (parser, CPP_NAME)
3383 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3385 /* Old-style structure member designator. */
3386 set_init_label (c_parser_peek_token (parser)->value,
3387 braced_init_obstack);
3388 /* Use the colon as the error location. */
3389 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3390 "obsolete use of designated initializer with %<:%>");
3391 c_parser_consume_token (parser);
3392 c_parser_consume_token (parser);
3394 else
3396 /* des_seen is 0 if there have been no designators, 1 if there
3397 has been a single array designator and 2 otherwise. */
3398 int des_seen = 0;
3399 /* Location of a designator. */
3400 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3401 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3402 || c_parser_next_token_is (parser, CPP_DOT))
3404 int des_prev = des_seen;
3405 if (!des_seen)
3406 des_loc = c_parser_peek_token (parser)->location;
3407 if (des_seen < 2)
3408 des_seen++;
3409 if (c_parser_next_token_is (parser, CPP_DOT))
3411 des_seen = 2;
3412 c_parser_consume_token (parser);
3413 if (c_parser_next_token_is (parser, CPP_NAME))
3415 set_init_label (c_parser_peek_token (parser)->value,
3416 braced_init_obstack);
3417 c_parser_consume_token (parser);
3419 else
3421 struct c_expr init;
3422 init.value = error_mark_node;
3423 init.original_code = ERROR_MARK;
3424 init.original_type = NULL;
3425 c_parser_error (parser, "expected identifier");
3426 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3427 process_init_element (init, false, braced_init_obstack);
3428 return;
3431 else
3433 tree first, second;
3434 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3435 /* ??? Following the old parser, [ objc-receiver
3436 objc-message-args ] is accepted as an initializer,
3437 being distinguished from a designator by what follows
3438 the first assignment expression inside the square
3439 brackets, but after a first array designator a
3440 subsequent square bracket is for Objective-C taken to
3441 start an expression, using the obsolete form of
3442 designated initializer without '=', rather than
3443 possibly being a second level of designation: in LALR
3444 terms, the '[' is shifted rather than reducing
3445 designator to designator-list. */
3446 if (des_prev == 1 && c_dialect_objc ())
3448 des_seen = des_prev;
3449 break;
3451 if (des_prev == 0 && c_dialect_objc ())
3453 /* This might be an array designator or an
3454 Objective-C message expression. If the former,
3455 continue parsing here; if the latter, parse the
3456 remainder of the initializer given the starting
3457 primary-expression. ??? It might make sense to
3458 distinguish when des_prev == 1 as well; see
3459 previous comment. */
3460 tree rec, args;
3461 struct c_expr mexpr;
3462 c_parser_consume_token (parser);
3463 if (c_parser_peek_token (parser)->type == CPP_NAME
3464 && ((c_parser_peek_token (parser)->id_kind
3465 == C_ID_TYPENAME)
3466 || (c_parser_peek_token (parser)->id_kind
3467 == C_ID_CLASSNAME)))
3469 /* Type name receiver. */
3470 tree id = c_parser_peek_token (parser)->value;
3471 c_parser_consume_token (parser);
3472 rec = objc_get_class_reference (id);
3473 goto parse_message_args;
3475 first = c_parser_expr_no_commas (parser, NULL).value;
3476 mark_exp_read (first);
3477 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3478 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3479 goto array_desig_after_first;
3480 /* Expression receiver. So far only one part
3481 without commas has been parsed; there might be
3482 more of the expression. */
3483 rec = first;
3484 while (c_parser_next_token_is (parser, CPP_COMMA))
3486 struct c_expr next;
3487 location_t comma_loc, exp_loc;
3488 comma_loc = c_parser_peek_token (parser)->location;
3489 c_parser_consume_token (parser);
3490 exp_loc = c_parser_peek_token (parser)->location;
3491 next = c_parser_expr_no_commas (parser, NULL);
3492 next = default_function_array_read_conversion (exp_loc,
3493 next);
3494 rec = build_compound_expr (comma_loc, rec, next.value);
3496 parse_message_args:
3497 /* Now parse the objc-message-args. */
3498 args = c_parser_objc_message_args (parser);
3499 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3500 "expected %<]%>");
3501 mexpr.value
3502 = objc_build_message_expr (build_tree_list (rec, args));
3503 mexpr.original_code = ERROR_MARK;
3504 mexpr.original_type = NULL;
3505 /* Now parse and process the remainder of the
3506 initializer, starting with this message
3507 expression as a primary-expression. */
3508 c_parser_initval (parser, &mexpr, braced_init_obstack);
3509 return;
3511 c_parser_consume_token (parser);
3512 first = c_parser_expr_no_commas (parser, NULL).value;
3513 mark_exp_read (first);
3514 array_desig_after_first:
3515 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3517 ellipsis_loc = c_parser_peek_token (parser)->location;
3518 c_parser_consume_token (parser);
3519 second = c_parser_expr_no_commas (parser, NULL).value;
3520 mark_exp_read (second);
3522 else
3523 second = NULL_TREE;
3524 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3526 c_parser_consume_token (parser);
3527 set_init_index (first, second, braced_init_obstack);
3528 if (second)
3529 pedwarn (ellipsis_loc, OPT_pedantic,
3530 "ISO C forbids specifying range of elements to initialize");
3532 else
3533 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3534 "expected %<]%>");
3537 if (des_seen >= 1)
3539 if (c_parser_next_token_is (parser, CPP_EQ))
3541 if (!flag_isoc99)
3542 pedwarn (des_loc, OPT_pedantic,
3543 "ISO C90 forbids specifying subobject to initialize");
3544 c_parser_consume_token (parser);
3546 else
3548 if (des_seen == 1)
3549 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3550 "obsolete use of designated initializer without %<=%>");
3551 else
3553 struct c_expr init;
3554 init.value = error_mark_node;
3555 init.original_code = ERROR_MARK;
3556 init.original_type = NULL;
3557 c_parser_error (parser, "expected %<=%>");
3558 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3559 process_init_element (init, false, braced_init_obstack);
3560 return;
3565 c_parser_initval (parser, NULL, braced_init_obstack);
3568 /* Parse a nested initializer; as c_parser_initializer but parses
3569 initializers within braced lists, after any designators have been
3570 applied. If AFTER is not NULL then it is an Objective-C message
3571 expression which is the primary-expression starting the
3572 initializer. */
3574 static void
3575 c_parser_initval (c_parser *parser, struct c_expr *after,
3576 struct obstack * braced_init_obstack)
3578 struct c_expr init;
3579 gcc_assert (!after || c_dialect_objc ());
3580 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3581 init = c_parser_braced_init (parser, NULL_TREE, true);
3582 else
3584 location_t loc = c_parser_peek_token (parser)->location;
3585 init = c_parser_expr_no_commas (parser, after);
3586 if (init.value != NULL_TREE
3587 && TREE_CODE (init.value) != STRING_CST
3588 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3589 init = default_function_array_read_conversion (loc, init);
3591 process_init_element (init, false, braced_init_obstack);
3594 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3595 C99 6.8.2).
3597 compound-statement:
3598 { block-item-list[opt] }
3599 { label-declarations block-item-list }
3601 block-item-list:
3602 block-item
3603 block-item-list block-item
3605 block-item:
3606 nested-declaration
3607 statement
3609 nested-declaration:
3610 declaration
3612 GNU extensions:
3614 compound-statement:
3615 { label-declarations block-item-list }
3617 nested-declaration:
3618 __extension__ nested-declaration
3619 nested-function-definition
3621 label-declarations:
3622 label-declaration
3623 label-declarations label-declaration
3625 label-declaration:
3626 __label__ identifier-list ;
3628 Allowing the mixing of declarations and code is new in C99. The
3629 GNU syntax also permits (not shown above) labels at the end of
3630 compound statements, which yield an error. We don't allow labels
3631 on declarations; this might seem like a natural extension, but
3632 there would be a conflict between attributes on the label and
3633 prefix attributes on the declaration. ??? The syntax follows the
3634 old parser in requiring something after label declarations.
3635 Although they are erroneous if the labels declared aren't defined,
3636 is it useful for the syntax to be this way?
3638 OpenMP:
3640 block-item:
3641 openmp-directive
3643 openmp-directive:
3644 barrier-directive
3645 flush-directive */
3647 static tree
3648 c_parser_compound_statement (c_parser *parser)
3650 tree stmt;
3651 location_t brace_loc;
3652 brace_loc = c_parser_peek_token (parser)->location;
3653 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3655 /* Ensure a scope is entered and left anyway to avoid confusion
3656 if we have just prepared to enter a function body. */
3657 stmt = c_begin_compound_stmt (true);
3658 c_end_compound_stmt (brace_loc, stmt, true);
3659 return error_mark_node;
3661 stmt = c_begin_compound_stmt (true);
3662 c_parser_compound_statement_nostart (parser);
3663 return c_end_compound_stmt (brace_loc, stmt, true);
3666 /* Parse a compound statement except for the opening brace. This is
3667 used for parsing both compound statements and statement expressions
3668 (which follow different paths to handling the opening). */
3670 static void
3671 c_parser_compound_statement_nostart (c_parser *parser)
3673 bool last_stmt = false;
3674 bool last_label = false;
3675 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3676 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3677 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3679 c_parser_consume_token (parser);
3680 return;
3682 mark_valid_location_for_stdc_pragma (true);
3683 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3685 /* Read zero or more forward-declarations for labels that nested
3686 functions can jump to. */
3687 mark_valid_location_for_stdc_pragma (false);
3688 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3690 label_loc = c_parser_peek_token (parser)->location;
3691 c_parser_consume_token (parser);
3692 /* Any identifiers, including those declared as type names,
3693 are OK here. */
3694 while (true)
3696 tree label;
3697 if (c_parser_next_token_is_not (parser, CPP_NAME))
3699 c_parser_error (parser, "expected identifier");
3700 break;
3702 label
3703 = declare_label (c_parser_peek_token (parser)->value);
3704 C_DECLARED_LABEL_FLAG (label) = 1;
3705 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3706 c_parser_consume_token (parser);
3707 if (c_parser_next_token_is (parser, CPP_COMMA))
3708 c_parser_consume_token (parser);
3709 else
3710 break;
3712 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3714 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3716 /* We must now have at least one statement, label or declaration. */
3717 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3719 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3720 c_parser_error (parser, "expected declaration or statement");
3721 c_parser_consume_token (parser);
3722 return;
3724 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3726 location_t loc = c_parser_peek_token (parser)->location;
3727 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3728 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3729 || (c_parser_next_token_is (parser, CPP_NAME)
3730 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3732 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3733 label_loc = c_parser_peek_2nd_token (parser)->location;
3734 else
3735 label_loc = c_parser_peek_token (parser)->location;
3736 last_label = true;
3737 last_stmt = false;
3738 mark_valid_location_for_stdc_pragma (false);
3739 c_parser_label (parser);
3741 else if (!last_label
3742 && c_parser_next_token_starts_declaration (parser))
3744 last_label = false;
3745 mark_valid_location_for_stdc_pragma (false);
3746 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
3747 if (last_stmt)
3748 pedwarn_c90 (loc,
3749 (pedantic && !flag_isoc99)
3750 ? OPT_pedantic
3751 : OPT_Wdeclaration_after_statement,
3752 "ISO C90 forbids mixed declarations and code");
3753 last_stmt = false;
3755 else if (!last_label
3756 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3758 /* __extension__ can start a declaration, but is also an
3759 unary operator that can start an expression. Consume all
3760 but the last of a possible series of __extension__ to
3761 determine which. */
3762 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3763 && (c_parser_peek_2nd_token (parser)->keyword
3764 == RID_EXTENSION))
3765 c_parser_consume_token (parser);
3766 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
3768 int ext;
3769 ext = disable_extension_diagnostics ();
3770 c_parser_consume_token (parser);
3771 last_label = false;
3772 mark_valid_location_for_stdc_pragma (false);
3773 c_parser_declaration_or_fndef (parser, true, true, true, true,
3774 true);
3775 /* Following the old parser, __extension__ does not
3776 disable this diagnostic. */
3777 restore_extension_diagnostics (ext);
3778 if (last_stmt)
3779 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3780 ? OPT_pedantic
3781 : OPT_Wdeclaration_after_statement,
3782 "ISO C90 forbids mixed declarations and code");
3783 last_stmt = false;
3785 else
3786 goto statement;
3788 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3790 /* External pragmas, and some omp pragmas, are not associated
3791 with regular c code, and so are not to be considered statements
3792 syntactically. This ensures that the user doesn't put them
3793 places that would turn into syntax errors if the directive
3794 were ignored. */
3795 if (c_parser_pragma (parser, pragma_compound))
3796 last_label = false, last_stmt = true;
3798 else if (c_parser_next_token_is (parser, CPP_EOF))
3800 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3801 c_parser_error (parser, "expected declaration or statement");
3802 return;
3804 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3806 if (parser->in_if_block)
3808 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3809 error_at (loc, """expected %<}%> before %<else%>");
3810 return;
3812 else
3814 error_at (loc, "%<else%> without a previous %<if%>");
3815 c_parser_consume_token (parser);
3816 continue;
3819 else
3821 statement:
3822 last_label = false;
3823 last_stmt = true;
3824 mark_valid_location_for_stdc_pragma (false);
3825 c_parser_statement_after_labels (parser);
3828 parser->error = false;
3830 if (last_label)
3831 error_at (label_loc, "label at end of compound statement");
3832 c_parser_consume_token (parser);
3833 /* Restore the value we started with. */
3834 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3837 /* Parse a label (C90 6.6.1, C99 6.8.1).
3839 label:
3840 identifier : attributes[opt]
3841 case constant-expression :
3842 default :
3844 GNU extensions:
3846 label:
3847 case constant-expression ... constant-expression :
3849 The use of attributes on labels is a GNU extension. The syntax in
3850 GNU C accepts any expressions without commas, non-constant
3851 expressions being rejected later. */
3853 static void
3854 c_parser_label (c_parser *parser)
3856 location_t loc1 = c_parser_peek_token (parser)->location;
3857 tree label = NULL_TREE;
3858 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3860 tree exp1, exp2;
3861 c_parser_consume_token (parser);
3862 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3863 if (c_parser_next_token_is (parser, CPP_COLON))
3865 c_parser_consume_token (parser);
3866 label = do_case (loc1, exp1, NULL_TREE);
3868 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3870 c_parser_consume_token (parser);
3871 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3872 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3873 label = do_case (loc1, exp1, exp2);
3875 else
3876 c_parser_error (parser, "expected %<:%> or %<...%>");
3878 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3880 c_parser_consume_token (parser);
3881 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3882 label = do_case (loc1, NULL_TREE, NULL_TREE);
3884 else
3886 tree name = c_parser_peek_token (parser)->value;
3887 tree tlab;
3888 tree attrs;
3889 location_t loc2 = c_parser_peek_token (parser)->location;
3890 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3891 c_parser_consume_token (parser);
3892 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3893 c_parser_consume_token (parser);
3894 attrs = c_parser_attributes (parser);
3895 tlab = define_label (loc2, name);
3896 if (tlab)
3898 decl_attributes (&tlab, attrs, 0);
3899 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
3902 if (label)
3904 if (c_parser_next_token_starts_declaration (parser)
3905 && !(c_parser_next_token_is (parser, CPP_NAME)
3906 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3908 error_at (c_parser_peek_token (parser)->location,
3909 "a label can only be part of a statement and "
3910 "a declaration is not a statement");
3911 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3912 /*static_assert_ok*/ true,
3913 /*nested*/ true, /*empty_ok*/ false,
3914 /*start_attr_ok*/ true);
3919 /* Parse a statement (C90 6.6, C99 6.8).
3921 statement:
3922 labeled-statement
3923 compound-statement
3924 expression-statement
3925 selection-statement
3926 iteration-statement
3927 jump-statement
3929 labeled-statement:
3930 label statement
3932 expression-statement:
3933 expression[opt] ;
3935 selection-statement:
3936 if-statement
3937 switch-statement
3939 iteration-statement:
3940 while-statement
3941 do-statement
3942 for-statement
3944 jump-statement:
3945 goto identifier ;
3946 continue ;
3947 break ;
3948 return expression[opt] ;
3950 GNU extensions:
3952 statement:
3953 asm-statement
3955 jump-statement:
3956 goto * expression ;
3958 Objective-C:
3960 statement:
3961 objc-throw-statement
3962 objc-try-catch-statement
3963 objc-synchronized-statement
3965 objc-throw-statement:
3966 @throw expression ;
3967 @throw ;
3969 OpenMP:
3971 statement:
3972 openmp-construct
3974 openmp-construct:
3975 parallel-construct
3976 for-construct
3977 sections-construct
3978 single-construct
3979 parallel-for-construct
3980 parallel-sections-construct
3981 master-construct
3982 critical-construct
3983 atomic-construct
3984 ordered-construct
3986 parallel-construct:
3987 parallel-directive structured-block
3989 for-construct:
3990 for-directive iteration-statement
3992 sections-construct:
3993 sections-directive section-scope
3995 single-construct:
3996 single-directive structured-block
3998 parallel-for-construct:
3999 parallel-for-directive iteration-statement
4001 parallel-sections-construct:
4002 parallel-sections-directive section-scope
4004 master-construct:
4005 master-directive structured-block
4007 critical-construct:
4008 critical-directive structured-block
4010 atomic-construct:
4011 atomic-directive expression-statement
4013 ordered-construct:
4014 ordered-directive structured-block */
4016 static void
4017 c_parser_statement (c_parser *parser)
4019 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4020 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4021 || (c_parser_next_token_is (parser, CPP_NAME)
4022 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4023 c_parser_label (parser);
4024 c_parser_statement_after_labels (parser);
4027 /* Parse a statement, other than a labeled statement. */
4029 static void
4030 c_parser_statement_after_labels (c_parser *parser)
4032 location_t loc = c_parser_peek_token (parser)->location;
4033 tree stmt = NULL_TREE;
4034 bool in_if_block = parser->in_if_block;
4035 parser->in_if_block = false;
4036 switch (c_parser_peek_token (parser)->type)
4038 case CPP_OPEN_BRACE:
4039 add_stmt (c_parser_compound_statement (parser));
4040 break;
4041 case CPP_KEYWORD:
4042 switch (c_parser_peek_token (parser)->keyword)
4044 case RID_IF:
4045 c_parser_if_statement (parser);
4046 break;
4047 case RID_SWITCH:
4048 c_parser_switch_statement (parser);
4049 break;
4050 case RID_WHILE:
4051 c_parser_while_statement (parser);
4052 break;
4053 case RID_DO:
4054 c_parser_do_statement (parser);
4055 break;
4056 case RID_FOR:
4057 c_parser_for_statement (parser);
4058 break;
4059 case RID_GOTO:
4060 c_parser_consume_token (parser);
4061 if (c_parser_next_token_is (parser, CPP_NAME))
4063 stmt = c_finish_goto_label (loc,
4064 c_parser_peek_token (parser)->value);
4065 c_parser_consume_token (parser);
4067 else if (c_parser_next_token_is (parser, CPP_MULT))
4069 c_parser_consume_token (parser);
4070 stmt = c_finish_goto_ptr (loc,
4071 c_parser_expression (parser).value);
4073 else
4074 c_parser_error (parser, "expected identifier or %<*%>");
4075 goto expect_semicolon;
4076 case RID_CONTINUE:
4077 c_parser_consume_token (parser);
4078 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4079 goto expect_semicolon;
4080 case RID_BREAK:
4081 c_parser_consume_token (parser);
4082 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4083 goto expect_semicolon;
4084 case RID_RETURN:
4085 c_parser_consume_token (parser);
4086 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4088 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4089 c_parser_consume_token (parser);
4091 else
4093 struct c_expr expr = c_parser_expression_conv (parser);
4094 mark_exp_read (expr.value);
4095 stmt = c_finish_return (loc, expr.value, expr.original_type);
4096 goto expect_semicolon;
4098 break;
4099 case RID_ASM:
4100 stmt = c_parser_asm_statement (parser);
4101 break;
4102 case RID_AT_THROW:
4103 gcc_assert (c_dialect_objc ());
4104 c_parser_consume_token (parser);
4105 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4107 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4108 c_parser_consume_token (parser);
4110 else
4112 tree expr = c_parser_expression (parser).value;
4113 expr = c_fully_fold (expr, false, NULL);
4114 stmt = objc_build_throw_stmt (loc, expr);
4115 goto expect_semicolon;
4117 break;
4118 case RID_AT_TRY:
4119 gcc_assert (c_dialect_objc ());
4120 c_parser_objc_try_catch_statement (parser);
4121 break;
4122 case RID_AT_SYNCHRONIZED:
4123 gcc_assert (c_dialect_objc ());
4124 c_parser_objc_synchronized_statement (parser);
4125 break;
4126 default:
4127 goto expr_stmt;
4129 break;
4130 case CPP_SEMICOLON:
4131 c_parser_consume_token (parser);
4132 break;
4133 case CPP_CLOSE_PAREN:
4134 case CPP_CLOSE_SQUARE:
4135 /* Avoid infinite loop in error recovery:
4136 c_parser_skip_until_found stops at a closing nesting
4137 delimiter without consuming it, but here we need to consume
4138 it to proceed further. */
4139 c_parser_error (parser, "expected statement");
4140 c_parser_consume_token (parser);
4141 break;
4142 case CPP_PRAGMA:
4143 c_parser_pragma (parser, pragma_stmt);
4144 break;
4145 default:
4146 expr_stmt:
4147 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4148 expect_semicolon:
4149 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4150 break;
4152 /* Two cases cannot and do not have line numbers associated: If stmt
4153 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4154 cannot hold line numbers. But that's OK because the statement
4155 will either be changed to a MODIFY_EXPR during gimplification of
4156 the statement expr, or discarded. If stmt was compound, but
4157 without new variables, we will have skipped the creation of a
4158 BIND and will have a bare STATEMENT_LIST. But that's OK because
4159 (recursively) all of the component statements should already have
4160 line numbers assigned. ??? Can we discard no-op statements
4161 earlier? */
4162 if (CAN_HAVE_LOCATION_P (stmt)
4163 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4164 SET_EXPR_LOCATION (stmt, loc);
4166 parser->in_if_block = in_if_block;
4169 /* Parse the condition from an if, do, while or for statements. */
4171 static tree
4172 c_parser_condition (c_parser *parser)
4174 location_t loc = c_parser_peek_token (parser)->location;
4175 tree cond;
4176 cond = c_parser_expression_conv (parser).value;
4177 cond = c_objc_common_truthvalue_conversion (loc, cond);
4178 cond = c_fully_fold (cond, false, NULL);
4179 if (warn_sequence_point)
4180 verify_sequence_points (cond);
4181 return cond;
4184 /* Parse a parenthesized condition from an if, do or while statement.
4186 condition:
4187 ( expression )
4189 static tree
4190 c_parser_paren_condition (c_parser *parser)
4192 tree cond;
4193 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4194 return error_mark_node;
4195 cond = c_parser_condition (parser);
4196 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4197 return cond;
4200 /* Parse a statement which is a block in C99. */
4202 static tree
4203 c_parser_c99_block_statement (c_parser *parser)
4205 tree block = c_begin_compound_stmt (flag_isoc99);
4206 location_t loc = c_parser_peek_token (parser)->location;
4207 c_parser_statement (parser);
4208 return c_end_compound_stmt (loc, block, flag_isoc99);
4211 /* Parse the body of an if statement. This is just parsing a
4212 statement but (a) it is a block in C99, (b) we track whether the
4213 body is an if statement for the sake of -Wparentheses warnings, (c)
4214 we handle an empty body specially for the sake of -Wempty-body
4215 warnings, and (d) we call parser_compound_statement directly
4216 because c_parser_statement_after_labels resets
4217 parser->in_if_block. */
4219 static tree
4220 c_parser_if_body (c_parser *parser, bool *if_p)
4222 tree block = c_begin_compound_stmt (flag_isoc99);
4223 location_t body_loc = c_parser_peek_token (parser)->location;
4224 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4225 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4226 || (c_parser_next_token_is (parser, CPP_NAME)
4227 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4228 c_parser_label (parser);
4229 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4230 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4232 location_t loc = c_parser_peek_token (parser)->location;
4233 add_stmt (build_empty_stmt (loc));
4234 c_parser_consume_token (parser);
4235 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4236 warning_at (loc, OPT_Wempty_body,
4237 "suggest braces around empty body in an %<if%> statement");
4239 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4240 add_stmt (c_parser_compound_statement (parser));
4241 else
4242 c_parser_statement_after_labels (parser);
4243 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4246 /* Parse the else body of an if statement. This is just parsing a
4247 statement but (a) it is a block in C99, (b) we handle an empty body
4248 specially for the sake of -Wempty-body warnings. */
4250 static tree
4251 c_parser_else_body (c_parser *parser)
4253 location_t else_loc = c_parser_peek_token (parser)->location;
4254 tree block = c_begin_compound_stmt (flag_isoc99);
4255 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4256 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4257 || (c_parser_next_token_is (parser, CPP_NAME)
4258 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4259 c_parser_label (parser);
4260 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4262 location_t loc = c_parser_peek_token (parser)->location;
4263 warning_at (loc,
4264 OPT_Wempty_body,
4265 "suggest braces around empty body in an %<else%> statement");
4266 add_stmt (build_empty_stmt (loc));
4267 c_parser_consume_token (parser);
4269 else
4270 c_parser_statement_after_labels (parser);
4271 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4274 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4276 if-statement:
4277 if ( expression ) statement
4278 if ( expression ) statement else statement
4281 static void
4282 c_parser_if_statement (c_parser *parser)
4284 tree block;
4285 location_t loc;
4286 tree cond;
4287 bool first_if = false;
4288 tree first_body, second_body;
4289 bool in_if_block;
4291 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4292 c_parser_consume_token (parser);
4293 block = c_begin_compound_stmt (flag_isoc99);
4294 loc = c_parser_peek_token (parser)->location;
4295 cond = c_parser_paren_condition (parser);
4296 in_if_block = parser->in_if_block;
4297 parser->in_if_block = true;
4298 first_body = c_parser_if_body (parser, &first_if);
4299 parser->in_if_block = in_if_block;
4300 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4302 c_parser_consume_token (parser);
4303 second_body = c_parser_else_body (parser);
4305 else
4306 second_body = NULL_TREE;
4307 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4308 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4311 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4313 switch-statement:
4314 switch (expression) statement
4317 static void
4318 c_parser_switch_statement (c_parser *parser)
4320 tree block, expr, body, save_break;
4321 location_t switch_loc = c_parser_peek_token (parser)->location;
4322 location_t switch_cond_loc;
4323 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4324 c_parser_consume_token (parser);
4325 block = c_begin_compound_stmt (flag_isoc99);
4326 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4328 switch_cond_loc = c_parser_peek_token (parser)->location;
4329 expr = c_parser_expression (parser).value;
4330 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4332 else
4334 switch_cond_loc = UNKNOWN_LOCATION;
4335 expr = error_mark_node;
4337 c_start_case (switch_loc, switch_cond_loc, expr);
4338 save_break = c_break_label;
4339 c_break_label = NULL_TREE;
4340 body = c_parser_c99_block_statement (parser);
4341 c_finish_case (body);
4342 if (c_break_label)
4344 location_t here = c_parser_peek_token (parser)->location;
4345 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4346 SET_EXPR_LOCATION (t, here);
4347 add_stmt (t);
4349 c_break_label = save_break;
4350 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4353 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4355 while-statement:
4356 while (expression) statement
4359 static void
4360 c_parser_while_statement (c_parser *parser)
4362 tree block, cond, body, save_break, save_cont;
4363 location_t loc;
4364 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4365 c_parser_consume_token (parser);
4366 block = c_begin_compound_stmt (flag_isoc99);
4367 loc = c_parser_peek_token (parser)->location;
4368 cond = c_parser_paren_condition (parser);
4369 save_break = c_break_label;
4370 c_break_label = NULL_TREE;
4371 save_cont = c_cont_label;
4372 c_cont_label = NULL_TREE;
4373 body = c_parser_c99_block_statement (parser);
4374 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4375 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4376 c_break_label = save_break;
4377 c_cont_label = save_cont;
4380 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4382 do-statement:
4383 do statement while ( expression ) ;
4386 static void
4387 c_parser_do_statement (c_parser *parser)
4389 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4390 location_t loc;
4391 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4392 c_parser_consume_token (parser);
4393 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4394 warning_at (c_parser_peek_token (parser)->location,
4395 OPT_Wempty_body,
4396 "suggest braces around empty body in %<do%> statement");
4397 block = c_begin_compound_stmt (flag_isoc99);
4398 loc = c_parser_peek_token (parser)->location;
4399 save_break = c_break_label;
4400 c_break_label = NULL_TREE;
4401 save_cont = c_cont_label;
4402 c_cont_label = NULL_TREE;
4403 body = c_parser_c99_block_statement (parser);
4404 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4405 new_break = c_break_label;
4406 c_break_label = save_break;
4407 new_cont = c_cont_label;
4408 c_cont_label = save_cont;
4409 cond = c_parser_paren_condition (parser);
4410 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4411 c_parser_skip_to_end_of_block_or_statement (parser);
4412 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4413 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4416 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4418 for-statement:
4419 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4420 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4422 The form with a declaration is new in C99.
4424 ??? In accordance with the old parser, the declaration may be a
4425 nested function, which is then rejected in check_for_loop_decls,
4426 but does it make any sense for this to be included in the grammar?
4427 Note in particular that the nested function does not include a
4428 trailing ';', whereas the "declaration" production includes one.
4429 Also, can we reject bad declarations earlier and cheaper than
4430 check_for_loop_decls? */
4432 static void
4433 c_parser_for_statement (c_parser *parser)
4435 tree block, cond, incr, save_break, save_cont, body;
4436 location_t loc = c_parser_peek_token (parser)->location;
4437 location_t for_loc = c_parser_peek_token (parser)->location;
4438 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4439 c_parser_consume_token (parser);
4440 block = c_begin_compound_stmt (flag_isoc99);
4441 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4443 /* Parse the initialization declaration or expression. */
4444 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4446 c_parser_consume_token (parser);
4447 c_finish_expr_stmt (loc, NULL_TREE);
4449 else if (c_parser_next_token_starts_declaration (parser))
4451 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
4452 check_for_loop_decls (for_loc);
4454 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4456 /* __extension__ can start a declaration, but is also an
4457 unary operator that can start an expression. Consume all
4458 but the last of a possible series of __extension__ to
4459 determine which. */
4460 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4461 && (c_parser_peek_2nd_token (parser)->keyword
4462 == RID_EXTENSION))
4463 c_parser_consume_token (parser);
4464 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4466 int ext;
4467 ext = disable_extension_diagnostics ();
4468 c_parser_consume_token (parser);
4469 c_parser_declaration_or_fndef (parser, true, true, true, true,
4470 true);
4471 restore_extension_diagnostics (ext);
4472 check_for_loop_decls (for_loc);
4474 else
4475 goto init_expr;
4477 else
4479 init_expr:
4480 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
4481 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4483 /* Parse the loop condition. */
4484 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4486 c_parser_consume_token (parser);
4487 cond = NULL_TREE;
4489 else
4491 cond = c_parser_condition (parser);
4492 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4494 /* Parse the increment expression. */
4495 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4496 incr = c_process_expr_stmt (loc, NULL_TREE);
4497 else
4498 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4499 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4501 else
4503 cond = error_mark_node;
4504 incr = error_mark_node;
4506 save_break = c_break_label;
4507 c_break_label = NULL_TREE;
4508 save_cont = c_cont_label;
4509 c_cont_label = NULL_TREE;
4510 body = c_parser_c99_block_statement (parser);
4511 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4512 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4513 c_break_label = save_break;
4514 c_cont_label = save_cont;
4517 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4518 statement with inputs, outputs, clobbers, and volatile tag
4519 allowed.
4521 asm-statement:
4522 asm type-qualifier[opt] ( asm-argument ) ;
4523 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4525 asm-argument:
4526 asm-string-literal
4527 asm-string-literal : asm-operands[opt]
4528 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4529 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4531 asm-goto-argument:
4532 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4533 : asm-goto-operands
4535 Qualifiers other than volatile are accepted in the syntax but
4536 warned for. */
4538 static tree
4539 c_parser_asm_statement (c_parser *parser)
4541 tree quals, str, outputs, inputs, clobbers, labels, ret;
4542 bool simple, is_goto;
4543 location_t asm_loc = c_parser_peek_token (parser)->location;
4544 int section, nsections;
4546 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4547 c_parser_consume_token (parser);
4548 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4550 quals = c_parser_peek_token (parser)->value;
4551 c_parser_consume_token (parser);
4553 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4554 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4556 warning_at (c_parser_peek_token (parser)->location,
4558 "%E qualifier ignored on asm",
4559 c_parser_peek_token (parser)->value);
4560 quals = NULL_TREE;
4561 c_parser_consume_token (parser);
4563 else
4564 quals = NULL_TREE;
4566 is_goto = false;
4567 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4569 c_parser_consume_token (parser);
4570 is_goto = true;
4573 /* ??? Follow the C++ parser rather than using the
4574 lex_untranslated_string kludge. */
4575 parser->lex_untranslated_string = true;
4576 ret = NULL;
4578 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4579 goto error;
4581 str = c_parser_asm_string_literal (parser);
4582 if (str == NULL_TREE)
4583 goto error_close_paren;
4585 simple = true;
4586 outputs = NULL_TREE;
4587 inputs = NULL_TREE;
4588 clobbers = NULL_TREE;
4589 labels = NULL_TREE;
4591 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4592 goto done_asm;
4594 /* Parse each colon-delimited section of operands. */
4595 nsections = 3 + is_goto;
4596 for (section = 0; section < nsections; ++section)
4598 if (!c_parser_require (parser, CPP_COLON,
4599 is_goto
4600 ? "expected %<:%>"
4601 : "expected %<:%> or %<)%>"))
4602 goto error_close_paren;
4604 /* Once past any colon, we're no longer a simple asm. */
4605 simple = false;
4607 if ((!c_parser_next_token_is (parser, CPP_COLON)
4608 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4609 || section == 3)
4610 switch (section)
4612 case 0:
4613 /* For asm goto, we don't allow output operands, but reserve
4614 the slot for a future extension that does allow them. */
4615 if (!is_goto)
4616 outputs = c_parser_asm_operands (parser, false);
4617 break;
4618 case 1:
4619 inputs = c_parser_asm_operands (parser, true);
4620 break;
4621 case 2:
4622 clobbers = c_parser_asm_clobbers (parser);
4623 break;
4624 case 3:
4625 labels = c_parser_asm_goto_operands (parser);
4626 break;
4627 default:
4628 gcc_unreachable ();
4631 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4632 goto done_asm;
4635 done_asm:
4636 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4639 goto error;
4642 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4643 c_parser_skip_to_end_of_block_or_statement (parser);
4645 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4646 clobbers, labels, simple));
4648 error:
4649 parser->lex_untranslated_string = false;
4650 return ret;
4652 error_close_paren:
4653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4654 goto error;
4657 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4658 not outputs), apply the default conversion of functions and arrays
4659 to pointers.
4661 asm-operands:
4662 asm-operand
4663 asm-operands , asm-operand
4665 asm-operand:
4666 asm-string-literal ( expression )
4667 [ identifier ] asm-string-literal ( expression )
4670 static tree
4671 c_parser_asm_operands (c_parser *parser, bool convert_p)
4673 tree list = NULL_TREE;
4674 location_t loc;
4675 while (true)
4677 tree name, str;
4678 struct c_expr expr;
4679 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4681 c_parser_consume_token (parser);
4682 if (c_parser_next_token_is (parser, CPP_NAME))
4684 tree id = c_parser_peek_token (parser)->value;
4685 c_parser_consume_token (parser);
4686 name = build_string (IDENTIFIER_LENGTH (id),
4687 IDENTIFIER_POINTER (id));
4689 else
4691 c_parser_error (parser, "expected identifier");
4692 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4693 return NULL_TREE;
4695 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4696 "expected %<]%>");
4698 else
4699 name = NULL_TREE;
4700 str = c_parser_asm_string_literal (parser);
4701 if (str == NULL_TREE)
4702 return NULL_TREE;
4703 parser->lex_untranslated_string = false;
4704 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4706 parser->lex_untranslated_string = true;
4707 return NULL_TREE;
4709 loc = c_parser_peek_token (parser)->location;
4710 expr = c_parser_expression (parser);
4711 mark_exp_read (expr.value);
4712 if (convert_p)
4713 expr = default_function_array_conversion (loc, expr);
4714 expr.value = c_fully_fold (expr.value, false, NULL);
4715 parser->lex_untranslated_string = true;
4716 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4718 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4719 return NULL_TREE;
4721 list = chainon (list, build_tree_list (build_tree_list (name, str),
4722 expr.value));
4723 if (c_parser_next_token_is (parser, CPP_COMMA))
4724 c_parser_consume_token (parser);
4725 else
4726 break;
4728 return list;
4731 /* Parse asm clobbers, a GNU extension.
4733 asm-clobbers:
4734 asm-string-literal
4735 asm-clobbers , asm-string-literal
4738 static tree
4739 c_parser_asm_clobbers (c_parser *parser)
4741 tree list = NULL_TREE;
4742 while (true)
4744 tree str = c_parser_asm_string_literal (parser);
4745 if (str)
4746 list = tree_cons (NULL_TREE, str, list);
4747 else
4748 return NULL_TREE;
4749 if (c_parser_next_token_is (parser, CPP_COMMA))
4750 c_parser_consume_token (parser);
4751 else
4752 break;
4754 return list;
4757 /* Parse asm goto labels, a GNU extension.
4759 asm-goto-operands:
4760 identifier
4761 asm-goto-operands , identifier
4764 static tree
4765 c_parser_asm_goto_operands (c_parser *parser)
4767 tree list = NULL_TREE;
4768 while (true)
4770 tree name, label;
4772 if (c_parser_next_token_is (parser, CPP_NAME))
4774 c_token *tok = c_parser_peek_token (parser);
4775 name = tok->value;
4776 label = lookup_label_for_goto (tok->location, name);
4777 c_parser_consume_token (parser);
4778 TREE_USED (label) = 1;
4780 else
4782 c_parser_error (parser, "expected identifier");
4783 return NULL_TREE;
4786 name = build_string (IDENTIFIER_LENGTH (name),
4787 IDENTIFIER_POINTER (name));
4788 list = tree_cons (name, label, list);
4789 if (c_parser_next_token_is (parser, CPP_COMMA))
4790 c_parser_consume_token (parser);
4791 else
4792 return nreverse (list);
4796 /* Parse an expression other than a compound expression; that is, an
4797 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4798 NULL then it is an Objective-C message expression which is the
4799 primary-expression starting the expression as an initializer.
4801 assignment-expression:
4802 conditional-expression
4803 unary-expression assignment-operator assignment-expression
4805 assignment-operator: one of
4806 = *= /= %= += -= <<= >>= &= ^= |=
4808 In GNU C we accept any conditional expression on the LHS and
4809 diagnose the invalid lvalue rather than producing a syntax
4810 error. */
4812 static struct c_expr
4813 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4815 struct c_expr lhs, rhs, ret;
4816 enum tree_code code;
4817 location_t op_location, exp_location;
4818 gcc_assert (!after || c_dialect_objc ());
4819 lhs = c_parser_conditional_expression (parser, after);
4820 op_location = c_parser_peek_token (parser)->location;
4821 switch (c_parser_peek_token (parser)->type)
4823 case CPP_EQ:
4824 code = NOP_EXPR;
4825 break;
4826 case CPP_MULT_EQ:
4827 code = MULT_EXPR;
4828 break;
4829 case CPP_DIV_EQ:
4830 code = TRUNC_DIV_EXPR;
4831 break;
4832 case CPP_MOD_EQ:
4833 code = TRUNC_MOD_EXPR;
4834 break;
4835 case CPP_PLUS_EQ:
4836 code = PLUS_EXPR;
4837 break;
4838 case CPP_MINUS_EQ:
4839 code = MINUS_EXPR;
4840 break;
4841 case CPP_LSHIFT_EQ:
4842 code = LSHIFT_EXPR;
4843 break;
4844 case CPP_RSHIFT_EQ:
4845 code = RSHIFT_EXPR;
4846 break;
4847 case CPP_AND_EQ:
4848 code = BIT_AND_EXPR;
4849 break;
4850 case CPP_XOR_EQ:
4851 code = BIT_XOR_EXPR;
4852 break;
4853 case CPP_OR_EQ:
4854 code = BIT_IOR_EXPR;
4855 break;
4856 default:
4857 return lhs;
4859 c_parser_consume_token (parser);
4860 exp_location = c_parser_peek_token (parser)->location;
4861 rhs = c_parser_expr_no_commas (parser, NULL);
4862 rhs = default_function_array_read_conversion (exp_location, rhs);
4863 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
4864 code, exp_location, rhs.value,
4865 rhs.original_type);
4866 if (code == NOP_EXPR)
4867 ret.original_code = MODIFY_EXPR;
4868 else
4870 TREE_NO_WARNING (ret.value) = 1;
4871 ret.original_code = ERROR_MARK;
4873 ret.original_type = NULL;
4874 return ret;
4877 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4878 is not NULL then it is an Objective-C message expression which is
4879 the primary-expression starting the expression as an initializer.
4881 conditional-expression:
4882 logical-OR-expression
4883 logical-OR-expression ? expression : conditional-expression
4885 GNU extensions:
4887 conditional-expression:
4888 logical-OR-expression ? : conditional-expression
4891 static struct c_expr
4892 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4894 struct c_expr cond, exp1, exp2, ret;
4895 location_t cond_loc, colon_loc, middle_loc;
4897 gcc_assert (!after || c_dialect_objc ());
4899 cond = c_parser_binary_expression (parser, after);
4901 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4902 return cond;
4903 cond_loc = c_parser_peek_token (parser)->location;
4904 cond = default_function_array_read_conversion (cond_loc, cond);
4905 c_parser_consume_token (parser);
4906 if (c_parser_next_token_is (parser, CPP_COLON))
4908 tree eptype = NULL_TREE;
4910 middle_loc = c_parser_peek_token (parser)->location;
4911 pedwarn (middle_loc, OPT_pedantic,
4912 "ISO C forbids omitting the middle term of a ?: expression");
4913 warn_for_omitted_condop (middle_loc, cond.value);
4914 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4916 eptype = TREE_TYPE (cond.value);
4917 cond.value = TREE_OPERAND (cond.value, 0);
4919 /* Make sure first operand is calculated only once. */
4920 exp1.value = c_save_expr (default_conversion (cond.value));
4921 if (eptype)
4922 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
4923 exp1.original_type = NULL;
4924 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4925 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
4927 else
4929 cond.value
4930 = c_objc_common_truthvalue_conversion
4931 (cond_loc, default_conversion (cond.value));
4932 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
4933 exp1 = c_parser_expression_conv (parser);
4934 mark_exp_read (exp1.value);
4935 c_inhibit_evaluation_warnings +=
4936 ((cond.value == truthvalue_true_node)
4937 - (cond.value == truthvalue_false_node));
4940 colon_loc = c_parser_peek_token (parser)->location;
4941 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4943 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4944 ret.value = error_mark_node;
4945 ret.original_code = ERROR_MARK;
4946 ret.original_type = NULL;
4947 return ret;
4950 location_t exp2_loc = c_parser_peek_token (parser)->location;
4951 exp2 = c_parser_conditional_expression (parser, NULL);
4952 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
4954 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4955 ret.value = build_conditional_expr (colon_loc, cond.value,
4956 cond.original_code == C_MAYBE_CONST_EXPR,
4957 exp1.value, exp1.original_type,
4958 exp2.value, exp2.original_type);
4959 ret.original_code = ERROR_MARK;
4960 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4961 ret.original_type = NULL;
4962 else
4964 tree t1, t2;
4966 /* If both sides are enum type, the default conversion will have
4967 made the type of the result be an integer type. We want to
4968 remember the enum types we started with. */
4969 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4970 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4971 ret.original_type = ((t1 != error_mark_node
4972 && t2 != error_mark_node
4973 && (TYPE_MAIN_VARIANT (t1)
4974 == TYPE_MAIN_VARIANT (t2)))
4975 ? t1
4976 : NULL);
4978 return ret;
4981 /* Parse a binary expression; that is, a logical-OR-expression (C90
4982 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4983 an Objective-C message expression which is the primary-expression
4984 starting the expression as an initializer.
4986 multiplicative-expression:
4987 cast-expression
4988 multiplicative-expression * cast-expression
4989 multiplicative-expression / cast-expression
4990 multiplicative-expression % cast-expression
4992 additive-expression:
4993 multiplicative-expression
4994 additive-expression + multiplicative-expression
4995 additive-expression - multiplicative-expression
4997 shift-expression:
4998 additive-expression
4999 shift-expression << additive-expression
5000 shift-expression >> additive-expression
5002 relational-expression:
5003 shift-expression
5004 relational-expression < shift-expression
5005 relational-expression > shift-expression
5006 relational-expression <= shift-expression
5007 relational-expression >= shift-expression
5009 equality-expression:
5010 relational-expression
5011 equality-expression == relational-expression
5012 equality-expression != relational-expression
5014 AND-expression:
5015 equality-expression
5016 AND-expression & equality-expression
5018 exclusive-OR-expression:
5019 AND-expression
5020 exclusive-OR-expression ^ AND-expression
5022 inclusive-OR-expression:
5023 exclusive-OR-expression
5024 inclusive-OR-expression | exclusive-OR-expression
5026 logical-AND-expression:
5027 inclusive-OR-expression
5028 logical-AND-expression && inclusive-OR-expression
5030 logical-OR-expression:
5031 logical-AND-expression
5032 logical-OR-expression || logical-AND-expression
5035 static struct c_expr
5036 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5038 /* A binary expression is parsed using operator-precedence parsing,
5039 with the operands being cast expressions. All the binary
5040 operators are left-associative. Thus a binary expression is of
5041 form:
5043 E0 op1 E1 op2 E2 ...
5045 which we represent on a stack. On the stack, the precedence
5046 levels are strictly increasing. When a new operator is
5047 encountered of higher precedence than that at the top of the
5048 stack, it is pushed; its LHS is the top expression, and its RHS
5049 is everything parsed until it is popped. When a new operator is
5050 encountered with precedence less than or equal to that at the top
5051 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5052 by the result of the operation until the operator at the top of
5053 the stack has lower precedence than the new operator or there is
5054 only one element on the stack; then the top expression is the LHS
5055 of the new operator. In the case of logical AND and OR
5056 expressions, we also need to adjust c_inhibit_evaluation_warnings
5057 as appropriate when the operators are pushed and popped. */
5059 /* The precedence levels, where 0 is a dummy lowest level used for
5060 the bottom of the stack. */
5061 enum prec {
5062 PREC_NONE,
5063 PREC_LOGOR,
5064 PREC_LOGAND,
5065 PREC_BITOR,
5066 PREC_BITXOR,
5067 PREC_BITAND,
5068 PREC_EQ,
5069 PREC_REL,
5070 PREC_SHIFT,
5071 PREC_ADD,
5072 PREC_MULT,
5073 NUM_PRECS
5075 struct {
5076 /* The expression at this stack level. */
5077 struct c_expr expr;
5078 /* The precedence of the operator on its left, PREC_NONE at the
5079 bottom of the stack. */
5080 enum prec prec;
5081 /* The operation on its left. */
5082 enum tree_code op;
5083 /* The source location of this operation. */
5084 location_t loc;
5085 } stack[NUM_PRECS];
5086 int sp;
5087 /* Location of the binary operator. */
5088 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5089 #define POP \
5090 do { \
5091 switch (stack[sp].op) \
5093 case TRUTH_ANDIF_EXPR: \
5094 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5095 == truthvalue_false_node); \
5096 break; \
5097 case TRUTH_ORIF_EXPR: \
5098 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5099 == truthvalue_true_node); \
5100 break; \
5101 default: \
5102 break; \
5104 stack[sp - 1].expr \
5105 = default_function_array_read_conversion (stack[sp - 1].loc, \
5106 stack[sp - 1].expr); \
5107 stack[sp].expr \
5108 = default_function_array_read_conversion (stack[sp].loc, \
5109 stack[sp].expr); \
5110 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5111 stack[sp].op, \
5112 stack[sp - 1].expr, \
5113 stack[sp].expr); \
5114 sp--; \
5115 } while (0)
5116 gcc_assert (!after || c_dialect_objc ());
5117 stack[0].loc = c_parser_peek_token (parser)->location;
5118 stack[0].expr = c_parser_cast_expression (parser, after);
5119 stack[0].prec = PREC_NONE;
5120 sp = 0;
5121 while (true)
5123 enum prec oprec;
5124 enum tree_code ocode;
5125 if (parser->error)
5126 goto out;
5127 switch (c_parser_peek_token (parser)->type)
5129 case CPP_MULT:
5130 oprec = PREC_MULT;
5131 ocode = MULT_EXPR;
5132 break;
5133 case CPP_DIV:
5134 oprec = PREC_MULT;
5135 ocode = TRUNC_DIV_EXPR;
5136 break;
5137 case CPP_MOD:
5138 oprec = PREC_MULT;
5139 ocode = TRUNC_MOD_EXPR;
5140 break;
5141 case CPP_PLUS:
5142 oprec = PREC_ADD;
5143 ocode = PLUS_EXPR;
5144 break;
5145 case CPP_MINUS:
5146 oprec = PREC_ADD;
5147 ocode = MINUS_EXPR;
5148 break;
5149 case CPP_LSHIFT:
5150 oprec = PREC_SHIFT;
5151 ocode = LSHIFT_EXPR;
5152 break;
5153 case CPP_RSHIFT:
5154 oprec = PREC_SHIFT;
5155 ocode = RSHIFT_EXPR;
5156 break;
5157 case CPP_LESS:
5158 oprec = PREC_REL;
5159 ocode = LT_EXPR;
5160 break;
5161 case CPP_GREATER:
5162 oprec = PREC_REL;
5163 ocode = GT_EXPR;
5164 break;
5165 case CPP_LESS_EQ:
5166 oprec = PREC_REL;
5167 ocode = LE_EXPR;
5168 break;
5169 case CPP_GREATER_EQ:
5170 oprec = PREC_REL;
5171 ocode = GE_EXPR;
5172 break;
5173 case CPP_EQ_EQ:
5174 oprec = PREC_EQ;
5175 ocode = EQ_EXPR;
5176 break;
5177 case CPP_NOT_EQ:
5178 oprec = PREC_EQ;
5179 ocode = NE_EXPR;
5180 break;
5181 case CPP_AND:
5182 oprec = PREC_BITAND;
5183 ocode = BIT_AND_EXPR;
5184 break;
5185 case CPP_XOR:
5186 oprec = PREC_BITXOR;
5187 ocode = BIT_XOR_EXPR;
5188 break;
5189 case CPP_OR:
5190 oprec = PREC_BITOR;
5191 ocode = BIT_IOR_EXPR;
5192 break;
5193 case CPP_AND_AND:
5194 oprec = PREC_LOGAND;
5195 ocode = TRUTH_ANDIF_EXPR;
5196 break;
5197 case CPP_OR_OR:
5198 oprec = PREC_LOGOR;
5199 ocode = TRUTH_ORIF_EXPR;
5200 break;
5201 default:
5202 /* Not a binary operator, so end of the binary
5203 expression. */
5204 goto out;
5206 binary_loc = c_parser_peek_token (parser)->location;
5207 c_parser_consume_token (parser);
5208 while (oprec <= stack[sp].prec)
5209 POP;
5210 switch (ocode)
5212 case TRUTH_ANDIF_EXPR:
5213 stack[sp].expr
5214 = default_function_array_read_conversion (stack[sp].loc,
5215 stack[sp].expr);
5216 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5217 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5218 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5219 == truthvalue_false_node);
5220 break;
5221 case TRUTH_ORIF_EXPR:
5222 stack[sp].expr
5223 = default_function_array_read_conversion (stack[sp].loc,
5224 stack[sp].expr);
5225 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5226 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5227 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5228 == truthvalue_true_node);
5229 break;
5230 default:
5231 break;
5233 sp++;
5234 stack[sp].loc = binary_loc;
5235 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5236 stack[sp].prec = oprec;
5237 stack[sp].op = ocode;
5238 stack[sp].loc = binary_loc;
5240 out:
5241 while (sp > 0)
5242 POP;
5243 return stack[0].expr;
5244 #undef POP
5247 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5248 NULL then it is an Objective-C message expression which is the
5249 primary-expression starting the expression as an initializer.
5251 cast-expression:
5252 unary-expression
5253 ( type-name ) unary-expression
5256 static struct c_expr
5257 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5259 location_t cast_loc = c_parser_peek_token (parser)->location;
5260 gcc_assert (!after || c_dialect_objc ());
5261 if (after)
5262 return c_parser_postfix_expression_after_primary (parser,
5263 cast_loc, *after);
5264 /* If the expression begins with a parenthesized type name, it may
5265 be either a cast or a compound literal; we need to see whether
5266 the next character is '{' to tell the difference. If not, it is
5267 an unary expression. */
5268 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5269 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5271 struct c_type_name *type_name;
5272 struct c_expr ret;
5273 struct c_expr expr;
5274 c_parser_consume_token (parser);
5275 type_name = c_parser_type_name (parser);
5276 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5277 if (type_name == NULL)
5279 ret.value = error_mark_node;
5280 ret.original_code = ERROR_MARK;
5281 ret.original_type = NULL;
5282 return ret;
5285 /* Save casted types in the function's used types hash table. */
5286 used_types_insert (type_name->specs->type);
5288 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5289 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5290 cast_loc);
5292 location_t expr_loc = c_parser_peek_token (parser)->location;
5293 expr = c_parser_cast_expression (parser, NULL);
5294 expr = default_function_array_read_conversion (expr_loc, expr);
5296 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5297 ret.original_code = ERROR_MARK;
5298 ret.original_type = NULL;
5299 return ret;
5301 else
5302 return c_parser_unary_expression (parser);
5305 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5307 unary-expression:
5308 postfix-expression
5309 ++ unary-expression
5310 -- unary-expression
5311 unary-operator cast-expression
5312 sizeof unary-expression
5313 sizeof ( type-name )
5315 unary-operator: one of
5316 & * + - ~ !
5318 GNU extensions:
5320 unary-expression:
5321 __alignof__ unary-expression
5322 __alignof__ ( type-name )
5323 && identifier
5325 unary-operator: one of
5326 __extension__ __real__ __imag__
5328 In addition, the GNU syntax treats ++ and -- as unary operators, so
5329 they may be applied to cast expressions with errors for non-lvalues
5330 given later. */
5332 static struct c_expr
5333 c_parser_unary_expression (c_parser *parser)
5335 int ext;
5336 struct c_expr ret, op;
5337 location_t op_loc = c_parser_peek_token (parser)->location;
5338 location_t exp_loc;
5339 ret.original_code = ERROR_MARK;
5340 ret.original_type = NULL;
5341 switch (c_parser_peek_token (parser)->type)
5343 case CPP_PLUS_PLUS:
5344 c_parser_consume_token (parser);
5345 exp_loc = c_parser_peek_token (parser)->location;
5346 op = c_parser_cast_expression (parser, NULL);
5347 op = default_function_array_read_conversion (exp_loc, op);
5348 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5349 case CPP_MINUS_MINUS:
5350 c_parser_consume_token (parser);
5351 exp_loc = c_parser_peek_token (parser)->location;
5352 op = c_parser_cast_expression (parser, NULL);
5353 op = default_function_array_read_conversion (exp_loc, op);
5354 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5355 case CPP_AND:
5356 c_parser_consume_token (parser);
5357 op = c_parser_cast_expression (parser, NULL);
5358 mark_exp_read (op.value);
5359 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5360 case CPP_MULT:
5361 c_parser_consume_token (parser);
5362 exp_loc = c_parser_peek_token (parser)->location;
5363 op = c_parser_cast_expression (parser, NULL);
5364 op = default_function_array_read_conversion (exp_loc, op);
5365 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5366 return ret;
5367 case CPP_PLUS:
5368 if (!c_dialect_objc () && !in_system_header)
5369 warning_at (op_loc,
5370 OPT_Wtraditional,
5371 "traditional C rejects the unary plus operator");
5372 c_parser_consume_token (parser);
5373 exp_loc = c_parser_peek_token (parser)->location;
5374 op = c_parser_cast_expression (parser, NULL);
5375 op = default_function_array_read_conversion (exp_loc, op);
5376 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5377 case CPP_MINUS:
5378 c_parser_consume_token (parser);
5379 exp_loc = c_parser_peek_token (parser)->location;
5380 op = c_parser_cast_expression (parser, NULL);
5381 op = default_function_array_read_conversion (exp_loc, op);
5382 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5383 case CPP_COMPL:
5384 c_parser_consume_token (parser);
5385 exp_loc = c_parser_peek_token (parser)->location;
5386 op = c_parser_cast_expression (parser, NULL);
5387 op = default_function_array_read_conversion (exp_loc, op);
5388 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5389 case CPP_NOT:
5390 c_parser_consume_token (parser);
5391 exp_loc = c_parser_peek_token (parser)->location;
5392 op = c_parser_cast_expression (parser, NULL);
5393 op = default_function_array_read_conversion (exp_loc, op);
5394 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5395 case CPP_AND_AND:
5396 /* Refer to the address of a label as a pointer. */
5397 c_parser_consume_token (parser);
5398 if (c_parser_next_token_is (parser, CPP_NAME))
5400 ret.value = finish_label_address_expr
5401 (c_parser_peek_token (parser)->value, op_loc);
5402 c_parser_consume_token (parser);
5404 else
5406 c_parser_error (parser, "expected identifier");
5407 ret.value = error_mark_node;
5409 return ret;
5410 case CPP_KEYWORD:
5411 switch (c_parser_peek_token (parser)->keyword)
5413 case RID_SIZEOF:
5414 return c_parser_sizeof_expression (parser);
5415 case RID_ALIGNOF:
5416 return c_parser_alignof_expression (parser);
5417 case RID_EXTENSION:
5418 c_parser_consume_token (parser);
5419 ext = disable_extension_diagnostics ();
5420 ret = c_parser_cast_expression (parser, NULL);
5421 restore_extension_diagnostics (ext);
5422 return ret;
5423 case RID_REALPART:
5424 c_parser_consume_token (parser);
5425 exp_loc = c_parser_peek_token (parser)->location;
5426 op = c_parser_cast_expression (parser, NULL);
5427 op = default_function_array_conversion (exp_loc, op);
5428 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5429 case RID_IMAGPART:
5430 c_parser_consume_token (parser);
5431 exp_loc = c_parser_peek_token (parser)->location;
5432 op = c_parser_cast_expression (parser, NULL);
5433 op = default_function_array_conversion (exp_loc, op);
5434 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5435 default:
5436 return c_parser_postfix_expression (parser);
5438 default:
5439 return c_parser_postfix_expression (parser);
5443 /* Parse a sizeof expression. */
5445 static struct c_expr
5446 c_parser_sizeof_expression (c_parser *parser)
5448 struct c_expr expr;
5449 location_t expr_loc;
5450 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5451 c_parser_consume_token (parser);
5452 c_inhibit_evaluation_warnings++;
5453 in_sizeof++;
5454 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5455 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5457 /* Either sizeof ( type-name ) or sizeof unary-expression
5458 starting with a compound literal. */
5459 struct c_type_name *type_name;
5460 c_parser_consume_token (parser);
5461 expr_loc = c_parser_peek_token (parser)->location;
5462 type_name = c_parser_type_name (parser);
5463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5464 if (type_name == NULL)
5466 struct c_expr ret;
5467 c_inhibit_evaluation_warnings--;
5468 in_sizeof--;
5469 ret.value = error_mark_node;
5470 ret.original_code = ERROR_MARK;
5471 ret.original_type = NULL;
5472 return ret;
5474 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5476 expr = c_parser_postfix_expression_after_paren_type (parser,
5477 type_name,
5478 expr_loc);
5479 goto sizeof_expr;
5481 /* sizeof ( type-name ). */
5482 c_inhibit_evaluation_warnings--;
5483 in_sizeof--;
5484 return c_expr_sizeof_type (expr_loc, type_name);
5486 else
5488 expr_loc = c_parser_peek_token (parser)->location;
5489 expr = c_parser_unary_expression (parser);
5490 sizeof_expr:
5491 c_inhibit_evaluation_warnings--;
5492 in_sizeof--;
5493 mark_exp_read (expr.value);
5494 if (TREE_CODE (expr.value) == COMPONENT_REF
5495 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5496 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5497 return c_expr_sizeof_expr (expr_loc, expr);
5501 /* Parse an alignof expression. */
5503 static struct c_expr
5504 c_parser_alignof_expression (c_parser *parser)
5506 struct c_expr expr;
5507 location_t loc = c_parser_peek_token (parser)->location;
5508 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5509 c_parser_consume_token (parser);
5510 c_inhibit_evaluation_warnings++;
5511 in_alignof++;
5512 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5513 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5515 /* Either __alignof__ ( type-name ) or __alignof__
5516 unary-expression starting with a compound literal. */
5517 location_t loc;
5518 struct c_type_name *type_name;
5519 struct c_expr ret;
5520 c_parser_consume_token (parser);
5521 loc = c_parser_peek_token (parser)->location;
5522 type_name = c_parser_type_name (parser);
5523 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5524 if (type_name == NULL)
5526 struct c_expr ret;
5527 c_inhibit_evaluation_warnings--;
5528 in_alignof--;
5529 ret.value = error_mark_node;
5530 ret.original_code = ERROR_MARK;
5531 ret.original_type = NULL;
5532 return ret;
5534 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5536 expr = c_parser_postfix_expression_after_paren_type (parser,
5537 type_name,
5538 loc);
5539 goto alignof_expr;
5541 /* alignof ( type-name ). */
5542 c_inhibit_evaluation_warnings--;
5543 in_alignof--;
5544 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5545 ret.original_code = ERROR_MARK;
5546 ret.original_type = NULL;
5547 return ret;
5549 else
5551 struct c_expr ret;
5552 expr = c_parser_unary_expression (parser);
5553 alignof_expr:
5554 mark_exp_read (expr.value);
5555 c_inhibit_evaluation_warnings--;
5556 in_alignof--;
5557 ret.value = c_alignof_expr (loc, expr.value);
5558 ret.original_code = ERROR_MARK;
5559 ret.original_type = NULL;
5560 return ret;
5564 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5566 postfix-expression:
5567 primary-expression
5568 postfix-expression [ expression ]
5569 postfix-expression ( argument-expression-list[opt] )
5570 postfix-expression . identifier
5571 postfix-expression -> identifier
5572 postfix-expression ++
5573 postfix-expression --
5574 ( type-name ) { initializer-list }
5575 ( type-name ) { initializer-list , }
5577 argument-expression-list:
5578 argument-expression
5579 argument-expression-list , argument-expression
5581 primary-expression:
5582 identifier
5583 constant
5584 string-literal
5585 ( expression )
5587 GNU extensions:
5589 primary-expression:
5590 __func__
5591 (treated as a keyword in GNU C)
5592 __FUNCTION__
5593 __PRETTY_FUNCTION__
5594 ( compound-statement )
5595 __builtin_va_arg ( assignment-expression , type-name )
5596 __builtin_offsetof ( type-name , offsetof-member-designator )
5597 __builtin_choose_expr ( assignment-expression ,
5598 assignment-expression ,
5599 assignment-expression )
5600 __builtin_types_compatible_p ( type-name , type-name )
5602 offsetof-member-designator:
5603 identifier
5604 offsetof-member-designator . identifier
5605 offsetof-member-designator [ expression ]
5607 Objective-C:
5609 primary-expression:
5610 [ objc-receiver objc-message-args ]
5611 @selector ( objc-selector-arg )
5612 @protocol ( identifier )
5613 @encode ( type-name )
5614 objc-string-literal
5617 static struct c_expr
5618 c_parser_postfix_expression (c_parser *parser)
5620 struct c_expr expr, e1, e2, e3;
5621 struct c_type_name *t1, *t2;
5622 location_t loc = c_parser_peek_token (parser)->location;;
5623 expr.original_code = ERROR_MARK;
5624 expr.original_type = NULL;
5625 switch (c_parser_peek_token (parser)->type)
5627 case CPP_NUMBER:
5628 expr.value = c_parser_peek_token (parser)->value;
5629 loc = c_parser_peek_token (parser)->location;
5630 c_parser_consume_token (parser);
5631 if (TREE_CODE (expr.value) == FIXED_CST
5632 && !targetm.fixed_point_supported_p ())
5634 error_at (loc, "fixed-point types not supported for this target");
5635 expr.value = error_mark_node;
5637 break;
5638 case CPP_CHAR:
5639 case CPP_CHAR16:
5640 case CPP_CHAR32:
5641 case CPP_WCHAR:
5642 expr.value = c_parser_peek_token (parser)->value;
5643 c_parser_consume_token (parser);
5644 break;
5645 case CPP_STRING:
5646 case CPP_STRING16:
5647 case CPP_STRING32:
5648 case CPP_WSTRING:
5649 case CPP_UTF8STRING:
5650 expr.value = c_parser_peek_token (parser)->value;
5651 expr.original_code = STRING_CST;
5652 c_parser_consume_token (parser);
5653 break;
5654 case CPP_OBJC_STRING:
5655 gcc_assert (c_dialect_objc ());
5656 expr.value
5657 = objc_build_string_object (c_parser_peek_token (parser)->value);
5658 c_parser_consume_token (parser);
5659 break;
5660 case CPP_NAME:
5661 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5663 c_parser_error (parser, "expected expression");
5664 expr.value = error_mark_node;
5665 break;
5668 tree id = c_parser_peek_token (parser)->value;
5669 c_parser_consume_token (parser);
5670 expr.value = build_external_ref (loc, id,
5671 (c_parser_peek_token (parser)->type
5672 == CPP_OPEN_PAREN),
5673 &expr.original_type);
5675 break;
5676 case CPP_OPEN_PAREN:
5677 /* A parenthesized expression, statement expression or compound
5678 literal. */
5679 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5681 /* A statement expression. */
5682 tree stmt;
5683 location_t brace_loc;
5684 c_parser_consume_token (parser);
5685 brace_loc = c_parser_peek_token (parser)->location;
5686 c_parser_consume_token (parser);
5687 if (cur_stmt_list == NULL)
5689 error_at (loc, "braced-group within expression allowed "
5690 "only inside a function");
5691 parser->error = true;
5692 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5694 expr.value = error_mark_node;
5695 break;
5697 stmt = c_begin_stmt_expr ();
5698 c_parser_compound_statement_nostart (parser);
5699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5700 "expected %<)%>");
5701 pedwarn (loc, OPT_pedantic,
5702 "ISO C forbids braced-groups within expressions");
5703 expr.value = c_finish_stmt_expr (brace_loc, stmt);
5704 mark_exp_read (expr.value);
5706 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5708 /* A compound literal. ??? Can we actually get here rather
5709 than going directly to
5710 c_parser_postfix_expression_after_paren_type from
5711 elsewhere? */
5712 location_t loc;
5713 struct c_type_name *type_name;
5714 c_parser_consume_token (parser);
5715 loc = c_parser_peek_token (parser)->location;
5716 type_name = c_parser_type_name (parser);
5717 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5718 "expected %<)%>");
5719 if (type_name == NULL)
5721 expr.value = error_mark_node;
5723 else
5724 expr = c_parser_postfix_expression_after_paren_type (parser,
5725 type_name,
5726 loc);
5728 else
5730 /* A parenthesized expression. */
5731 c_parser_consume_token (parser);
5732 expr = c_parser_expression (parser);
5733 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5734 TREE_NO_WARNING (expr.value) = 1;
5735 if (expr.original_code != C_MAYBE_CONST_EXPR)
5736 expr.original_code = ERROR_MARK;
5737 /* Don't change EXPR.ORIGINAL_TYPE. */
5738 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5739 "expected %<)%>");
5741 break;
5742 case CPP_KEYWORD:
5743 switch (c_parser_peek_token (parser)->keyword)
5745 case RID_FUNCTION_NAME:
5746 case RID_PRETTY_FUNCTION_NAME:
5747 case RID_C99_FUNCTION_NAME:
5748 expr.value = fname_decl (loc,
5749 c_parser_peek_token (parser)->keyword,
5750 c_parser_peek_token (parser)->value);
5751 c_parser_consume_token (parser);
5752 break;
5753 case RID_VA_ARG:
5754 c_parser_consume_token (parser);
5755 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5757 expr.value = error_mark_node;
5758 break;
5760 e1 = c_parser_expr_no_commas (parser, NULL);
5761 mark_exp_read (e1.value);
5762 e1.value = c_fully_fold (e1.value, false, NULL);
5763 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5765 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5766 expr.value = error_mark_node;
5767 break;
5769 loc = c_parser_peek_token (parser)->location;
5770 t1 = c_parser_type_name (parser);
5771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5772 "expected %<)%>");
5773 if (t1 == NULL)
5775 expr.value = error_mark_node;
5777 else
5779 tree type_expr = NULL_TREE;
5780 expr.value = c_build_va_arg (loc, e1.value,
5781 groktypename (t1, &type_expr, NULL));
5782 if (type_expr)
5784 expr.value = build2 (C_MAYBE_CONST_EXPR,
5785 TREE_TYPE (expr.value), type_expr,
5786 expr.value);
5787 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5790 break;
5791 case RID_OFFSETOF:
5792 c_parser_consume_token (parser);
5793 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5795 expr.value = error_mark_node;
5796 break;
5798 t1 = c_parser_type_name (parser);
5799 if (t1 == NULL)
5801 expr.value = error_mark_node;
5802 break;
5804 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5807 expr.value = error_mark_node;
5808 break;
5811 tree type = groktypename (t1, NULL, NULL);
5812 tree offsetof_ref;
5813 if (type == error_mark_node)
5814 offsetof_ref = error_mark_node;
5815 else
5817 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5818 SET_EXPR_LOCATION (offsetof_ref, loc);
5820 /* Parse the second argument to __builtin_offsetof. We
5821 must have one identifier, and beyond that we want to
5822 accept sub structure and sub array references. */
5823 if (c_parser_next_token_is (parser, CPP_NAME))
5825 offsetof_ref = build_component_ref
5826 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
5827 c_parser_consume_token (parser);
5828 while (c_parser_next_token_is (parser, CPP_DOT)
5829 || c_parser_next_token_is (parser,
5830 CPP_OPEN_SQUARE)
5831 || c_parser_next_token_is (parser,
5832 CPP_DEREF))
5834 if (c_parser_next_token_is (parser, CPP_DEREF))
5836 loc = c_parser_peek_token (parser)->location;
5837 offsetof_ref = build_array_ref (loc,
5838 offsetof_ref,
5839 integer_zero_node);
5840 goto do_dot;
5842 else if (c_parser_next_token_is (parser, CPP_DOT))
5844 do_dot:
5845 c_parser_consume_token (parser);
5846 if (c_parser_next_token_is_not (parser,
5847 CPP_NAME))
5849 c_parser_error (parser, "expected identifier");
5850 break;
5852 offsetof_ref = build_component_ref
5853 (loc, offsetof_ref,
5854 c_parser_peek_token (parser)->value);
5855 c_parser_consume_token (parser);
5857 else
5859 tree idx;
5860 loc = c_parser_peek_token (parser)->location;
5861 c_parser_consume_token (parser);
5862 idx = c_parser_expression (parser).value;
5863 idx = c_fully_fold (idx, false, NULL);
5864 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5865 "expected %<]%>");
5866 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
5870 else
5871 c_parser_error (parser, "expected identifier");
5872 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5873 "expected %<)%>");
5874 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5876 break;
5877 case RID_CHOOSE_EXPR:
5878 c_parser_consume_token (parser);
5879 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5881 expr.value = error_mark_node;
5882 break;
5884 loc = c_parser_peek_token (parser)->location;
5885 e1 = c_parser_expr_no_commas (parser, NULL);
5886 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5888 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5889 expr.value = error_mark_node;
5890 break;
5892 e2 = c_parser_expr_no_commas (parser, NULL);
5893 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5895 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5896 expr.value = error_mark_node;
5897 break;
5899 e3 = c_parser_expr_no_commas (parser, NULL);
5900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5901 "expected %<)%>");
5903 tree c;
5905 c = e1.value;
5906 mark_exp_read (e2.value);
5907 mark_exp_read (e3.value);
5908 if (TREE_CODE (c) != INTEGER_CST
5909 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
5910 error_at (loc,
5911 "first argument to %<__builtin_choose_expr%> not"
5912 " a constant");
5913 constant_expression_warning (c);
5914 expr = integer_zerop (c) ? e3 : e2;
5916 break;
5917 case RID_TYPES_COMPATIBLE_P:
5918 c_parser_consume_token (parser);
5919 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5921 expr.value = error_mark_node;
5922 break;
5924 t1 = c_parser_type_name (parser);
5925 if (t1 == NULL)
5927 expr.value = error_mark_node;
5928 break;
5930 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5932 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5933 expr.value = error_mark_node;
5934 break;
5936 t2 = c_parser_type_name (parser);
5937 if (t2 == NULL)
5939 expr.value = error_mark_node;
5940 break;
5942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5943 "expected %<)%>");
5945 tree e1, e2;
5947 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5948 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
5950 expr.value
5951 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
5953 break;
5954 case RID_AT_SELECTOR:
5955 gcc_assert (c_dialect_objc ());
5956 c_parser_consume_token (parser);
5957 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5959 expr.value = error_mark_node;
5960 break;
5963 tree sel = c_parser_objc_selector_arg (parser);
5964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5965 "expected %<)%>");
5966 expr.value = objc_build_selector_expr (loc, sel);
5968 break;
5969 case RID_AT_PROTOCOL:
5970 gcc_assert (c_dialect_objc ());
5971 c_parser_consume_token (parser);
5972 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5974 expr.value = error_mark_node;
5975 break;
5977 if (c_parser_next_token_is_not (parser, CPP_NAME))
5979 c_parser_error (parser, "expected identifier");
5980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5981 expr.value = error_mark_node;
5982 break;
5985 tree id = c_parser_peek_token (parser)->value;
5986 c_parser_consume_token (parser);
5987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5988 "expected %<)%>");
5989 expr.value = objc_build_protocol_expr (id);
5991 break;
5992 case RID_AT_ENCODE:
5993 /* Extension to support C-structures in the archiver. */
5994 gcc_assert (c_dialect_objc ());
5995 c_parser_consume_token (parser);
5996 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5998 expr.value = error_mark_node;
5999 break;
6001 t1 = c_parser_type_name (parser);
6002 if (t1 == NULL)
6004 expr.value = error_mark_node;
6005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6006 break;
6008 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6009 "expected %<)%>");
6011 tree type = groktypename (t1, NULL, NULL);
6012 expr.value = objc_build_encode_expr (type);
6014 break;
6015 default:
6016 c_parser_error (parser, "expected expression");
6017 expr.value = error_mark_node;
6018 break;
6020 break;
6021 case CPP_OPEN_SQUARE:
6022 if (c_dialect_objc ())
6024 tree receiver, args;
6025 c_parser_consume_token (parser);
6026 receiver = c_parser_objc_receiver (parser);
6027 args = c_parser_objc_message_args (parser);
6028 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6029 "expected %<]%>");
6030 expr.value = objc_build_message_expr (build_tree_list (receiver,
6031 args));
6032 break;
6034 /* Else fall through to report error. */
6035 default:
6036 c_parser_error (parser, "expected expression");
6037 expr.value = error_mark_node;
6038 break;
6040 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6043 /* Parse a postfix expression after a parenthesized type name: the
6044 brace-enclosed initializer of a compound literal, possibly followed
6045 by some postfix operators. This is separate because it is not
6046 possible to tell until after the type name whether a cast
6047 expression has a cast or a compound literal, or whether the operand
6048 of sizeof is a parenthesized type name or starts with a compound
6049 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6050 location of the first token after the parentheses around the type
6051 name. */
6053 static struct c_expr
6054 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6055 struct c_type_name *type_name,
6056 location_t type_loc)
6058 tree type;
6059 struct c_expr init;
6060 bool non_const;
6061 struct c_expr expr;
6062 location_t start_loc;
6063 tree type_expr = NULL_TREE;
6064 bool type_expr_const = true;
6065 check_compound_literal_type (type_loc, type_name);
6066 start_init (NULL_TREE, NULL, 0);
6067 type = groktypename (type_name, &type_expr, &type_expr_const);
6068 start_loc = c_parser_peek_token (parser)->location;
6069 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6071 error_at (type_loc, "compound literal has variable size");
6072 type = error_mark_node;
6074 init = c_parser_braced_init (parser, type, false);
6075 finish_init ();
6076 maybe_warn_string_init (type, init);
6078 if (type != error_mark_node
6079 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6080 && current_function_decl)
6082 error ("compound literal qualified by address-space qualifier");
6083 type = error_mark_node;
6086 if (!flag_isoc99)
6087 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6088 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6089 ? CONSTRUCTOR_NON_CONST (init.value)
6090 : init.original_code == C_MAYBE_CONST_EXPR);
6091 non_const |= !type_expr_const;
6092 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6093 expr.original_code = ERROR_MARK;
6094 expr.original_type = NULL;
6095 if (type_expr)
6097 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6099 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6100 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6102 else
6104 gcc_assert (!non_const);
6105 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6106 type_expr, expr.value);
6109 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6112 /* Parse a postfix expression after the initial primary or compound
6113 literal; that is, parse a series of postfix operators.
6115 EXPR_LOC is the location of the primary expression. */
6117 static struct c_expr
6118 c_parser_postfix_expression_after_primary (c_parser *parser,
6119 location_t expr_loc,
6120 struct c_expr expr)
6122 struct c_expr orig_expr;
6123 tree ident, idx;
6124 VEC(tree,gc) *exprlist;
6125 VEC(tree,gc) *origtypes;
6126 while (true)
6128 location_t op_loc = c_parser_peek_token (parser)->location;
6129 switch (c_parser_peek_token (parser)->type)
6131 case CPP_OPEN_SQUARE:
6132 /* Array reference. */
6133 c_parser_consume_token (parser);
6134 idx = c_parser_expression (parser).value;
6135 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6136 "expected %<]%>");
6137 expr.value = build_array_ref (op_loc, expr.value, idx);
6138 expr.original_code = ERROR_MARK;
6139 expr.original_type = NULL;
6140 break;
6141 case CPP_OPEN_PAREN:
6142 /* Function call. */
6143 c_parser_consume_token (parser);
6144 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6145 exprlist = NULL;
6146 else
6147 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6149 "expected %<)%>");
6150 orig_expr = expr;
6151 mark_exp_read (expr.value);
6152 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6153 "(" after the FUNCNAME, which is what we have now. */
6154 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6155 origtypes);
6156 expr.original_code = ERROR_MARK;
6157 if (TREE_CODE (expr.value) == INTEGER_CST
6158 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6159 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6160 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6161 expr.original_code = C_MAYBE_CONST_EXPR;
6162 expr.original_type = NULL;
6163 if (exprlist != NULL)
6165 release_tree_vector (exprlist);
6166 release_tree_vector (origtypes);
6168 break;
6169 case CPP_DOT:
6170 /* Structure element reference. */
6171 c_parser_consume_token (parser);
6172 expr = default_function_array_conversion (expr_loc, expr);
6173 if (c_parser_next_token_is (parser, CPP_NAME))
6174 ident = c_parser_peek_token (parser)->value;
6175 else
6177 c_parser_error (parser, "expected identifier");
6178 expr.value = error_mark_node;
6179 expr.original_code = ERROR_MARK;
6180 expr.original_type = NULL;
6181 return expr;
6183 c_parser_consume_token (parser);
6184 expr.value = build_component_ref (op_loc, expr.value, ident);
6185 expr.original_code = ERROR_MARK;
6186 if (TREE_CODE (expr.value) != COMPONENT_REF)
6187 expr.original_type = NULL;
6188 else
6190 /* Remember the original type of a bitfield. */
6191 tree field = TREE_OPERAND (expr.value, 1);
6192 if (TREE_CODE (field) != FIELD_DECL)
6193 expr.original_type = NULL;
6194 else
6195 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6197 break;
6198 case CPP_DEREF:
6199 /* Structure element reference. */
6200 c_parser_consume_token (parser);
6201 expr = default_function_array_conversion (expr_loc, expr);
6202 if (c_parser_next_token_is (parser, CPP_NAME))
6203 ident = c_parser_peek_token (parser)->value;
6204 else
6206 c_parser_error (parser, "expected identifier");
6207 expr.value = error_mark_node;
6208 expr.original_code = ERROR_MARK;
6209 expr.original_type = NULL;
6210 return expr;
6212 c_parser_consume_token (parser);
6213 expr.value = build_component_ref (op_loc,
6214 build_indirect_ref (op_loc,
6215 expr.value,
6216 RO_ARROW),
6217 ident);
6218 expr.original_code = ERROR_MARK;
6219 if (TREE_CODE (expr.value) != COMPONENT_REF)
6220 expr.original_type = NULL;
6221 else
6223 /* Remember the original type of a bitfield. */
6224 tree field = TREE_OPERAND (expr.value, 1);
6225 if (TREE_CODE (field) != FIELD_DECL)
6226 expr.original_type = NULL;
6227 else
6228 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6230 break;
6231 case CPP_PLUS_PLUS:
6232 /* Postincrement. */
6233 c_parser_consume_token (parser);
6234 expr = default_function_array_read_conversion (expr_loc, expr);
6235 expr.value = build_unary_op (op_loc,
6236 POSTINCREMENT_EXPR, expr.value, 0);
6237 expr.original_code = ERROR_MARK;
6238 expr.original_type = NULL;
6239 break;
6240 case CPP_MINUS_MINUS:
6241 /* Postdecrement. */
6242 c_parser_consume_token (parser);
6243 expr = default_function_array_read_conversion (expr_loc, expr);
6244 expr.value = build_unary_op (op_loc,
6245 POSTDECREMENT_EXPR, expr.value, 0);
6246 expr.original_code = ERROR_MARK;
6247 expr.original_type = NULL;
6248 break;
6249 default:
6250 return expr;
6255 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6257 expression:
6258 assignment-expression
6259 expression , assignment-expression
6262 static struct c_expr
6263 c_parser_expression (c_parser *parser)
6265 struct c_expr expr;
6266 expr = c_parser_expr_no_commas (parser, NULL);
6267 while (c_parser_next_token_is (parser, CPP_COMMA))
6269 struct c_expr next;
6270 tree lhsval;
6271 location_t loc = c_parser_peek_token (parser)->location;
6272 location_t expr_loc;
6273 c_parser_consume_token (parser);
6274 expr_loc = c_parser_peek_token (parser)->location;
6275 lhsval = expr.value;
6276 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6277 lhsval = TREE_OPERAND (lhsval, 1);
6278 if (DECL_P (lhsval) || handled_component_p (lhsval))
6279 mark_exp_read (lhsval);
6280 next = c_parser_expr_no_commas (parser, NULL);
6281 next = default_function_array_conversion (expr_loc, next);
6282 expr.value = build_compound_expr (loc, expr.value, next.value);
6283 expr.original_code = COMPOUND_EXPR;
6284 expr.original_type = next.original_type;
6286 return expr;
6289 /* Parse an expression and convert functions or arrays to
6290 pointers. */
6292 static struct c_expr
6293 c_parser_expression_conv (c_parser *parser)
6295 struct c_expr expr;
6296 location_t loc = c_parser_peek_token (parser)->location;
6297 expr = c_parser_expression (parser);
6298 expr = default_function_array_conversion (loc, expr);
6299 return expr;
6302 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6303 functions and arrays to pointers. If FOLD_P, fold the expressions.
6305 nonempty-expr-list:
6306 assignment-expression
6307 nonempty-expr-list , assignment-expression
6310 static VEC(tree,gc) *
6311 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6312 VEC(tree,gc) **p_orig_types)
6314 VEC(tree,gc) *ret;
6315 VEC(tree,gc) *orig_types;
6316 struct c_expr expr;
6317 location_t loc = c_parser_peek_token (parser)->location;
6319 ret = make_tree_vector ();
6320 if (p_orig_types == NULL)
6321 orig_types = NULL;
6322 else
6323 orig_types = make_tree_vector ();
6325 expr = c_parser_expr_no_commas (parser, NULL);
6326 if (convert_p)
6327 expr = default_function_array_read_conversion (loc, expr);
6328 if (fold_p)
6329 expr.value = c_fully_fold (expr.value, false, NULL);
6330 VEC_quick_push (tree, ret, expr.value);
6331 if (orig_types != NULL)
6332 VEC_quick_push (tree, orig_types, expr.original_type);
6333 while (c_parser_next_token_is (parser, CPP_COMMA))
6335 c_parser_consume_token (parser);
6336 loc = c_parser_peek_token (parser)->location;
6337 expr = c_parser_expr_no_commas (parser, NULL);
6338 if (convert_p)
6339 expr = default_function_array_read_conversion (loc, expr);
6340 if (fold_p)
6341 expr.value = c_fully_fold (expr.value, false, NULL);
6342 VEC_safe_push (tree, gc, ret, expr.value);
6343 if (orig_types != NULL)
6344 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6346 if (orig_types != NULL)
6347 *p_orig_types = orig_types;
6348 return ret;
6351 /* Parse Objective-C-specific constructs. */
6353 /* Parse an objc-class-definition.
6355 objc-class-definition:
6356 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6357 objc-class-instance-variables[opt] objc-methodprotolist @end
6358 @implementation identifier objc-superclass[opt]
6359 objc-class-instance-variables[opt]
6360 @interface identifier ( identifier ) objc-protocol-refs[opt]
6361 objc-methodprotolist @end
6362 @implementation identifier ( identifier )
6364 objc-superclass:
6365 : identifier
6367 "@interface identifier (" must start "@interface identifier (
6368 identifier ) ...": objc-methodprotolist in the first production may
6369 not start with a parenthesized identifier as a declarator of a data
6370 definition with no declaration specifiers if the objc-superclass,
6371 objc-protocol-refs and objc-class-instance-variables are omitted. */
6373 static void
6374 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6376 bool iface_p;
6377 tree id1;
6378 tree superclass;
6379 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6380 iface_p = true;
6381 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6382 iface_p = false;
6383 else
6384 gcc_unreachable ();
6386 c_parser_consume_token (parser);
6387 if (c_parser_next_token_is_not (parser, CPP_NAME))
6389 c_parser_error (parser, "expected identifier");
6390 return;
6392 id1 = c_parser_peek_token (parser)->value;
6393 c_parser_consume_token (parser);
6394 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6396 tree id2;
6397 tree proto = NULL_TREE;
6398 c_parser_consume_token (parser);
6399 if (c_parser_next_token_is_not (parser, CPP_NAME))
6401 c_parser_error (parser, "expected identifier");
6402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6403 return;
6405 id2 = c_parser_peek_token (parser)->value;
6406 c_parser_consume_token (parser);
6407 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6408 if (!iface_p)
6410 objc_start_category_implementation (id1, id2);
6411 return;
6413 if (c_parser_next_token_is (parser, CPP_LESS))
6414 proto = c_parser_objc_protocol_refs (parser);
6415 objc_start_category_interface (id1, id2, proto, attributes);
6416 c_parser_objc_methodprotolist (parser);
6417 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6418 objc_finish_interface ();
6419 return;
6421 if (c_parser_next_token_is (parser, CPP_COLON))
6423 c_parser_consume_token (parser);
6424 if (c_parser_next_token_is_not (parser, CPP_NAME))
6426 c_parser_error (parser, "expected identifier");
6427 return;
6429 superclass = c_parser_peek_token (parser)->value;
6430 c_parser_consume_token (parser);
6432 else
6433 superclass = NULL_TREE;
6434 if (iface_p)
6436 tree proto = NULL_TREE;
6437 if (c_parser_next_token_is (parser, CPP_LESS))
6438 proto = c_parser_objc_protocol_refs (parser);
6439 objc_start_class_interface (id1, superclass, proto, attributes);
6441 else
6442 objc_start_class_implementation (id1, superclass);
6443 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6444 c_parser_objc_class_instance_variables (parser);
6445 if (iface_p)
6447 objc_continue_interface ();
6448 c_parser_objc_methodprotolist (parser);
6449 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6450 objc_finish_interface ();
6452 else
6454 objc_continue_implementation ();
6455 return;
6459 /* Parse objc-class-instance-variables.
6461 objc-class-instance-variables:
6462 { objc-instance-variable-decl-list[opt] }
6464 objc-instance-variable-decl-list:
6465 objc-visibility-spec
6466 objc-instance-variable-decl ;
6468 objc-instance-variable-decl-list objc-visibility-spec
6469 objc-instance-variable-decl-list objc-instance-variable-decl ;
6470 objc-instance-variable-decl-list ;
6472 objc-visibility-spec:
6473 @private
6474 @protected
6475 @public
6477 objc-instance-variable-decl:
6478 struct-declaration
6481 static void
6482 c_parser_objc_class_instance_variables (c_parser *parser)
6484 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6485 c_parser_consume_token (parser);
6486 while (c_parser_next_token_is_not (parser, CPP_EOF))
6488 tree decls;
6489 /* Parse any stray semicolon. */
6490 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6492 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6493 "extra semicolon in struct or union specified");
6494 c_parser_consume_token (parser);
6495 continue;
6497 /* Stop if at the end of the instance variables. */
6498 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6500 c_parser_consume_token (parser);
6501 break;
6503 /* Parse any objc-visibility-spec. */
6504 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6506 c_parser_consume_token (parser);
6507 objc_set_visibility (2);
6508 continue;
6510 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6512 c_parser_consume_token (parser);
6513 objc_set_visibility (0);
6514 continue;
6516 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6518 c_parser_consume_token (parser);
6519 objc_set_visibility (1);
6520 continue;
6522 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6524 c_parser_pragma (parser, pragma_external);
6525 continue;
6528 /* Parse some comma-separated declarations. */
6529 decls = c_parser_struct_declaration (parser);
6531 /* Comma-separated instance variables are chained together in
6532 reverse order; add them one by one. */
6533 tree ivar = nreverse (decls);
6534 for (; ivar; ivar = DECL_CHAIN (ivar))
6535 objc_add_instance_variable (copy_node (ivar));
6537 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6541 /* Parse an objc-class-declaration.
6543 objc-class-declaration:
6544 @class identifier-list ;
6547 static void
6548 c_parser_objc_class_declaration (c_parser *parser)
6550 tree list = NULL_TREE;
6551 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6552 c_parser_consume_token (parser);
6553 /* Any identifiers, including those declared as type names, are OK
6554 here. */
6555 while (true)
6557 tree id;
6558 if (c_parser_next_token_is_not (parser, CPP_NAME))
6560 c_parser_error (parser, "expected identifier");
6561 break;
6563 id = c_parser_peek_token (parser)->value;
6564 list = chainon (list, build_tree_list (NULL_TREE, id));
6565 c_parser_consume_token (parser);
6566 if (c_parser_next_token_is (parser, CPP_COMMA))
6567 c_parser_consume_token (parser);
6568 else
6569 break;
6571 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6572 objc_declare_class (list);
6575 /* Parse an objc-alias-declaration.
6577 objc-alias-declaration:
6578 @compatibility_alias identifier identifier ;
6581 static void
6582 c_parser_objc_alias_declaration (c_parser *parser)
6584 tree id1, id2;
6585 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6586 c_parser_consume_token (parser);
6587 if (c_parser_next_token_is_not (parser, CPP_NAME))
6589 c_parser_error (parser, "expected identifier");
6590 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6591 return;
6593 id1 = c_parser_peek_token (parser)->value;
6594 c_parser_consume_token (parser);
6595 if (c_parser_next_token_is_not (parser, CPP_NAME))
6597 c_parser_error (parser, "expected identifier");
6598 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6599 return;
6601 id2 = c_parser_peek_token (parser)->value;
6602 c_parser_consume_token (parser);
6603 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6604 objc_declare_alias (id1, id2);
6607 /* Parse an objc-protocol-definition.
6609 objc-protocol-definition:
6610 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6611 @protocol identifier-list ;
6613 "@protocol identifier ;" should be resolved as "@protocol
6614 identifier-list ;": objc-methodprotolist may not start with a
6615 semicolon in the first alternative if objc-protocol-refs are
6616 omitted. */
6618 static void
6619 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
6621 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6623 c_parser_consume_token (parser);
6624 if (c_parser_next_token_is_not (parser, CPP_NAME))
6626 c_parser_error (parser, "expected identifier");
6627 return;
6629 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6630 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6632 tree list = NULL_TREE;
6633 /* Any identifiers, including those declared as type names, are
6634 OK here. */
6635 while (true)
6637 tree id;
6638 if (c_parser_next_token_is_not (parser, CPP_NAME))
6640 c_parser_error (parser, "expected identifier");
6641 break;
6643 id = c_parser_peek_token (parser)->value;
6644 list = chainon (list, build_tree_list (NULL_TREE, id));
6645 c_parser_consume_token (parser);
6646 if (c_parser_next_token_is (parser, CPP_COMMA))
6647 c_parser_consume_token (parser);
6648 else
6649 break;
6651 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6652 objc_declare_protocols (list);
6654 else
6656 tree id = c_parser_peek_token (parser)->value;
6657 tree proto = NULL_TREE;
6658 c_parser_consume_token (parser);
6659 if (c_parser_next_token_is (parser, CPP_LESS))
6660 proto = c_parser_objc_protocol_refs (parser);
6661 parser->objc_pq_context = true;
6662 objc_start_protocol (id, proto, attributes);
6663 c_parser_objc_methodprotolist (parser);
6664 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6665 parser->objc_pq_context = false;
6666 objc_finish_interface ();
6670 /* Parse an objc-method-type.
6672 objc-method-type:
6677 static enum tree_code
6678 c_parser_objc_method_type (c_parser *parser)
6680 switch (c_parser_peek_token (parser)->type)
6682 case CPP_PLUS:
6683 c_parser_consume_token (parser);
6684 return PLUS_EXPR;
6685 case CPP_MINUS:
6686 c_parser_consume_token (parser);
6687 return MINUS_EXPR;
6688 default:
6689 gcc_unreachable ();
6693 /* Parse an objc-method-definition.
6695 objc-method-definition:
6696 objc-method-type objc-method-decl ;[opt] compound-statement
6699 static void
6700 c_parser_objc_method_definition (c_parser *parser)
6702 enum tree_code type = c_parser_objc_method_type (parser);
6703 tree decl, attributes = NULL_TREE;
6704 objc_set_method_type (type);
6705 parser->objc_pq_context = true;
6706 decl = c_parser_objc_method_decl (parser, &attributes);
6707 if (decl == error_mark_node)
6708 return; /* Bail here. */
6710 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6712 c_parser_consume_token (parser);
6713 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6714 "extra semicolon in method definition specified");
6717 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6719 c_parser_error (parser, "expected %<{%>");
6720 return;
6723 parser->objc_pq_context = false;
6724 if (objc_start_method_definition (decl, attributes))
6726 add_stmt (c_parser_compound_statement (parser));
6727 objc_finish_method_definition (current_function_decl);
6729 else
6731 /* This code is executed when we find a method definition
6732 outside of an @implementation context. Parse the method (to
6733 keep going) but do not emit any code.
6735 c_parser_compound_statement (parser);
6739 /* Parse an objc-methodprotolist.
6741 objc-methodprotolist:
6742 empty
6743 objc-methodprotolist objc-methodproto
6744 objc-methodprotolist declaration
6745 objc-methodprotolist ;
6747 The declaration is a data definition, which may be missing
6748 declaration specifiers under the same rules and diagnostics as
6749 other data definitions outside functions, and the stray semicolon
6750 is diagnosed the same way as a stray semicolon outside a
6751 function. */
6753 static void
6754 c_parser_objc_methodprotolist (c_parser *parser)
6756 while (true)
6758 /* The list is terminated by @end. */
6759 switch (c_parser_peek_token (parser)->type)
6761 case CPP_SEMICOLON:
6762 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6763 "ISO C does not allow extra %<;%> outside of a function");
6764 c_parser_consume_token (parser);
6765 break;
6766 case CPP_PLUS:
6767 case CPP_MINUS:
6768 c_parser_objc_methodproto (parser);
6769 break;
6770 case CPP_PRAGMA:
6771 c_parser_pragma (parser, pragma_external);
6772 break;
6773 case CPP_EOF:
6774 return;
6775 default:
6776 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6777 return;
6778 c_parser_declaration_or_fndef (parser, false, false, true,
6779 false, true);
6780 break;
6785 /* Parse an objc-methodproto.
6787 objc-methodproto:
6788 objc-method-type objc-method-decl ;
6791 static void
6792 c_parser_objc_methodproto (c_parser *parser)
6794 enum tree_code type = c_parser_objc_method_type (parser);
6795 tree decl, attributes = NULL_TREE;
6796 objc_set_method_type (type);
6797 /* Remember protocol qualifiers in prototypes. */
6798 parser->objc_pq_context = true;
6799 decl = c_parser_objc_method_decl (parser, &attributes);
6800 /* Forget protocol qualifiers now. */
6801 parser->objc_pq_context = false;
6803 /* Do not allow the presence of attributes to hide an erroneous
6804 method implementation in the interface section. */
6805 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
6807 c_parser_error (parser, "expected %<;%>");
6808 return;
6811 if (decl != error_mark_node)
6812 objc_add_method_declaration (decl, attributes);
6814 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6817 /* If we are at a position that method attributes may be present, check that
6818 there are not any parsed already (a syntax error) and then collect any
6819 specified at the current location. Finally, if new attributes were present,
6820 check that the next token is legal ( ';' for decls and '{' for defs). */
6822 static bool
6823 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
6825 bool bad = false;
6826 if (*attributes)
6828 c_parser_error (parser,
6829 "method attributes must be specified at the end only");
6830 *attributes = NULL_TREE;
6831 bad = true;
6834 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
6835 *attributes = c_parser_attributes (parser);
6837 /* If there were no attributes here, just report any earlier error. */
6838 if (*attributes == NULL_TREE || bad)
6839 return bad;
6841 /* If the attributes are followed by a ; or {, then just report any earlier
6842 error. */
6843 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
6844 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6845 return bad;
6847 /* We've got attributes, but not at the end. */
6848 c_parser_error (parser,
6849 "expected %<;%> or %<{%> after method attribute definition");
6850 return true;
6853 /* Parse an objc-method-decl.
6855 objc-method-decl:
6856 ( objc-type-name ) objc-selector
6857 objc-selector
6858 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6859 objc-keyword-selector objc-optparmlist
6860 attributes
6862 objc-keyword-selector:
6863 objc-keyword-decl
6864 objc-keyword-selector objc-keyword-decl
6866 objc-keyword-decl:
6867 objc-selector : ( objc-type-name ) identifier
6868 objc-selector : identifier
6869 : ( objc-type-name ) identifier
6870 : identifier
6872 objc-optparmlist:
6873 objc-optparms objc-optellipsis
6875 objc-optparms:
6876 empty
6877 objc-opt-parms , parameter-declaration
6879 objc-optellipsis:
6880 empty
6881 , ...
6884 static tree
6885 c_parser_objc_method_decl (c_parser *parser, tree *attributes)
6887 tree type = NULL_TREE;
6888 tree sel;
6889 tree parms = NULL_TREE;
6890 bool ellipsis = false;
6891 bool attr_err = false;
6893 *attributes = NULL_TREE;
6894 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6896 c_parser_consume_token (parser);
6897 type = c_parser_objc_type_name (parser);
6898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6900 sel = c_parser_objc_selector (parser);
6901 /* If there is no selector, or a colon follows, we have an
6902 objc-keyword-selector. If there is a selector, and a colon does
6903 not follow, that selector ends the objc-method-decl. */
6904 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6906 tree tsel = sel;
6907 tree list = NULL_TREE;
6908 while (true)
6910 tree atype = NULL_TREE, id, keyworddecl;
6911 tree param_attr = NULL_TREE;
6912 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6913 break;
6914 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6916 c_parser_consume_token (parser);
6917 atype = c_parser_objc_type_name (parser);
6918 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6919 "expected %<)%>");
6921 /* New ObjC allows attributes on method parameters. */
6922 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
6923 param_attr = c_parser_attributes (parser);
6924 if (c_parser_next_token_is_not (parser, CPP_NAME))
6926 c_parser_error (parser, "expected identifier");
6927 return error_mark_node;
6929 id = c_parser_peek_token (parser)->value;
6930 c_parser_consume_token (parser);
6931 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
6932 list = chainon (list, keyworddecl);
6933 tsel = c_parser_objc_selector (parser);
6934 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6935 break;
6938 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
6940 /* Parse the optional parameter list. Optional Objective-C
6941 method parameters follow the C syntax, and may include '...'
6942 to denote a variable number of arguments. */
6943 parms = make_node (TREE_LIST);
6944 while (c_parser_next_token_is (parser, CPP_COMMA))
6946 struct c_parm *parm;
6947 c_parser_consume_token (parser);
6948 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6950 ellipsis = true;
6951 c_parser_consume_token (parser);
6952 attr_err |= c_parser_objc_maybe_method_attributes
6953 (parser, attributes) ;
6954 break;
6956 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6957 if (parm == NULL)
6958 break;
6959 parms = chainon (parms,
6960 build_tree_list (NULL_TREE, grokparm (parm)));
6962 sel = list;
6964 else
6965 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
6967 if (sel == NULL)
6969 c_parser_error (parser, "objective-c method declaration is expected");
6970 return error_mark_node;
6973 if (attr_err)
6974 return error_mark_node;
6976 return objc_build_method_signature (type, sel, parms, ellipsis);
6979 /* Parse an objc-type-name.
6981 objc-type-name:
6982 objc-type-qualifiers[opt] type-name
6983 objc-type-qualifiers[opt]
6985 objc-type-qualifiers:
6986 objc-type-qualifier
6987 objc-type-qualifiers objc-type-qualifier
6989 objc-type-qualifier: one of
6990 in out inout bycopy byref oneway
6993 static tree
6994 c_parser_objc_type_name (c_parser *parser)
6996 tree quals = NULL_TREE;
6997 struct c_type_name *type_name = NULL;
6998 tree type = NULL_TREE;
6999 while (true)
7001 c_token *token = c_parser_peek_token (parser);
7002 if (token->type == CPP_KEYWORD
7003 && (token->keyword == RID_IN
7004 || token->keyword == RID_OUT
7005 || token->keyword == RID_INOUT
7006 || token->keyword == RID_BYCOPY
7007 || token->keyword == RID_BYREF
7008 || token->keyword == RID_ONEWAY))
7010 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
7011 c_parser_consume_token (parser);
7013 else
7014 break;
7016 if (c_parser_next_token_starts_typename (parser))
7017 type_name = c_parser_type_name (parser);
7018 if (type_name)
7019 type = groktypename (type_name, NULL, NULL);
7020 return build_tree_list (quals, type);
7023 /* Parse objc-protocol-refs.
7025 objc-protocol-refs:
7026 < identifier-list >
7029 static tree
7030 c_parser_objc_protocol_refs (c_parser *parser)
7032 tree list = NULL_TREE;
7033 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7034 c_parser_consume_token (parser);
7035 /* Any identifiers, including those declared as type names, are OK
7036 here. */
7037 while (true)
7039 tree id;
7040 if (c_parser_next_token_is_not (parser, CPP_NAME))
7042 c_parser_error (parser, "expected identifier");
7043 break;
7045 id = c_parser_peek_token (parser)->value;
7046 list = chainon (list, build_tree_list (NULL_TREE, id));
7047 c_parser_consume_token (parser);
7048 if (c_parser_next_token_is (parser, CPP_COMMA))
7049 c_parser_consume_token (parser);
7050 else
7051 break;
7053 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7054 return list;
7057 /* Parse an objc-try-catch-statement.
7059 objc-try-catch-statement:
7060 @try compound-statement objc-catch-list[opt]
7061 @try compound-statement objc-catch-list[opt] @finally compound-statement
7063 objc-catch-list:
7064 @catch ( parameter-declaration ) compound-statement
7065 objc-catch-list @catch ( parameter-declaration ) compound-statement
7068 static void
7069 c_parser_objc_try_catch_statement (c_parser *parser)
7071 location_t loc;
7072 tree stmt;
7073 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7074 c_parser_consume_token (parser);
7075 loc = c_parser_peek_token (parser)->location;
7076 stmt = c_parser_compound_statement (parser);
7077 objc_begin_try_stmt (loc, stmt);
7078 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7080 struct c_parm *parm;
7081 c_parser_consume_token (parser);
7082 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7083 break;
7084 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7085 if (parm == NULL)
7087 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7088 break;
7090 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7091 objc_begin_catch_clause (grokparm (parm));
7092 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7093 c_parser_compound_statement_nostart (parser);
7094 objc_finish_catch_clause ();
7096 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7098 location_t finloc;
7099 tree finstmt;
7100 c_parser_consume_token (parser);
7101 finloc = c_parser_peek_token (parser)->location;
7102 finstmt = c_parser_compound_statement (parser);
7103 objc_build_finally_clause (finloc, finstmt);
7105 objc_finish_try_stmt ();
7108 /* Parse an objc-synchronized-statement.
7110 objc-synchronized-statement:
7111 @synchronized ( expression ) compound-statement
7114 static void
7115 c_parser_objc_synchronized_statement (c_parser *parser)
7117 location_t loc;
7118 tree expr, stmt;
7119 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7120 c_parser_consume_token (parser);
7121 loc = c_parser_peek_token (parser)->location;
7122 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7124 expr = c_parser_expression (parser).value;
7125 expr = c_fully_fold (expr, false, NULL);
7126 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7128 else
7129 expr = error_mark_node;
7130 stmt = c_parser_compound_statement (parser);
7131 objc_build_synchronized (loc, expr, stmt);
7134 /* Parse an objc-selector; return NULL_TREE without an error if the
7135 next token is not an objc-selector.
7137 objc-selector:
7138 identifier
7139 one of
7140 enum struct union if else while do for switch case default
7141 break continue return goto asm sizeof typeof __alignof
7142 unsigned long const short volatile signed restrict _Complex
7143 in out inout bycopy byref oneway int char float double void _Bool
7145 ??? Why this selection of keywords but not, for example, storage
7146 class specifiers? */
7148 static tree
7149 c_parser_objc_selector (c_parser *parser)
7151 c_token *token = c_parser_peek_token (parser);
7152 tree value = token->value;
7153 if (token->type == CPP_NAME)
7155 c_parser_consume_token (parser);
7156 return value;
7158 if (token->type != CPP_KEYWORD)
7159 return NULL_TREE;
7160 switch (token->keyword)
7162 case RID_ENUM:
7163 case RID_STRUCT:
7164 case RID_UNION:
7165 case RID_IF:
7166 case RID_ELSE:
7167 case RID_WHILE:
7168 case RID_DO:
7169 case RID_FOR:
7170 case RID_SWITCH:
7171 case RID_CASE:
7172 case RID_DEFAULT:
7173 case RID_BREAK:
7174 case RID_CONTINUE:
7175 case RID_RETURN:
7176 case RID_GOTO:
7177 case RID_ASM:
7178 case RID_SIZEOF:
7179 case RID_TYPEOF:
7180 case RID_ALIGNOF:
7181 case RID_UNSIGNED:
7182 case RID_LONG:
7183 case RID_INT128:
7184 case RID_CONST:
7185 case RID_SHORT:
7186 case RID_VOLATILE:
7187 case RID_SIGNED:
7188 case RID_RESTRICT:
7189 case RID_COMPLEX:
7190 case RID_IN:
7191 case RID_OUT:
7192 case RID_INOUT:
7193 case RID_BYCOPY:
7194 case RID_BYREF:
7195 case RID_ONEWAY:
7196 case RID_INT:
7197 case RID_CHAR:
7198 case RID_FLOAT:
7199 case RID_DOUBLE:
7200 case RID_VOID:
7201 case RID_BOOL:
7202 c_parser_consume_token (parser);
7203 return value;
7204 default:
7205 return NULL_TREE;
7209 /* Parse an objc-selector-arg.
7211 objc-selector-arg:
7212 objc-selector
7213 objc-keywordname-list
7215 objc-keywordname-list:
7216 objc-keywordname
7217 objc-keywordname-list objc-keywordname
7219 objc-keywordname:
7220 objc-selector :
7224 static tree
7225 c_parser_objc_selector_arg (c_parser *parser)
7227 tree sel = c_parser_objc_selector (parser);
7228 tree list = NULL_TREE;
7229 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7230 return sel;
7231 while (true)
7233 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7234 return list;
7235 list = chainon (list, build_tree_list (sel, NULL_TREE));
7236 sel = c_parser_objc_selector (parser);
7237 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7238 break;
7240 return list;
7243 /* Parse an objc-receiver.
7245 objc-receiver:
7246 expression
7247 class-name
7248 type-name
7251 static tree
7252 c_parser_objc_receiver (c_parser *parser)
7254 if (c_parser_peek_token (parser)->type == CPP_NAME
7255 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7256 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7258 tree id = c_parser_peek_token (parser)->value;
7259 c_parser_consume_token (parser);
7260 return objc_get_class_reference (id);
7262 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7265 /* Parse objc-message-args.
7267 objc-message-args:
7268 objc-selector
7269 objc-keywordarg-list
7271 objc-keywordarg-list:
7272 objc-keywordarg
7273 objc-keywordarg-list objc-keywordarg
7275 objc-keywordarg:
7276 objc-selector : objc-keywordexpr
7277 : objc-keywordexpr
7280 static tree
7281 c_parser_objc_message_args (c_parser *parser)
7283 tree sel = c_parser_objc_selector (parser);
7284 tree list = NULL_TREE;
7285 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7286 return sel;
7287 while (true)
7289 tree keywordexpr;
7290 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7291 return error_mark_node;
7292 keywordexpr = c_parser_objc_keywordexpr (parser);
7293 list = chainon (list, build_tree_list (sel, keywordexpr));
7294 sel = c_parser_objc_selector (parser);
7295 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7296 break;
7298 return list;
7301 /* Parse an objc-keywordexpr.
7303 objc-keywordexpr:
7304 nonempty-expr-list
7307 static tree
7308 c_parser_objc_keywordexpr (c_parser *parser)
7310 tree ret;
7311 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7312 if (VEC_length (tree, expr_list) == 1)
7314 /* Just return the expression, remove a level of
7315 indirection. */
7316 ret = VEC_index (tree, expr_list, 0);
7318 else
7320 /* We have a comma expression, we will collapse later. */
7321 ret = build_tree_list_vec (expr_list);
7323 release_tree_vector (expr_list);
7324 return ret;
7327 /* A check, needed in several places, that ObjC interface, implementation or
7328 method definitions are not prefixed by incorrect items. */
7329 static bool
7330 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7331 struct c_declspecs *specs)
7333 if (!specs->declspecs_seen_p || specs->type_seen_p || specs->non_sc_seen_p)
7335 c_parser_error (parser,
7336 "no type or storage class may be specified here,");
7337 c_parser_skip_to_end_of_block_or_statement (parser);
7338 return true;
7340 return false;
7343 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
7344 should be considered, statements. ALLOW_STMT is true if we're within
7345 the context of a function and such pragmas are to be allowed. Returns
7346 true if we actually parsed such a pragma. */
7348 static bool
7349 c_parser_pragma (c_parser *parser, enum pragma_context context)
7351 unsigned int id;
7353 id = c_parser_peek_token (parser)->pragma_kind;
7354 gcc_assert (id != PRAGMA_NONE);
7356 switch (id)
7358 case PRAGMA_OMP_BARRIER:
7359 if (context != pragma_compound)
7361 if (context == pragma_stmt)
7362 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7363 "used in compound statements");
7364 goto bad_stmt;
7366 c_parser_omp_barrier (parser);
7367 return false;
7369 case PRAGMA_OMP_FLUSH:
7370 if (context != pragma_compound)
7372 if (context == pragma_stmt)
7373 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7374 "used in compound statements");
7375 goto bad_stmt;
7377 c_parser_omp_flush (parser);
7378 return false;
7380 case PRAGMA_OMP_TASKWAIT:
7381 if (context != pragma_compound)
7383 if (context == pragma_stmt)
7384 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7385 "used in compound statements");
7386 goto bad_stmt;
7388 c_parser_omp_taskwait (parser);
7389 return false;
7391 case PRAGMA_OMP_THREADPRIVATE:
7392 c_parser_omp_threadprivate (parser);
7393 return false;
7395 case PRAGMA_OMP_SECTION:
7396 error_at (c_parser_peek_token (parser)->location,
7397 "%<#pragma omp section%> may only be used in "
7398 "%<#pragma omp sections%> construct");
7399 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7400 return false;
7402 case PRAGMA_GCC_PCH_PREPROCESS:
7403 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7404 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7405 return false;
7407 default:
7408 if (id < PRAGMA_FIRST_EXTERNAL)
7410 if (context == pragma_external)
7412 bad_stmt:
7413 c_parser_error (parser, "expected declaration specifiers");
7414 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7415 return false;
7417 c_parser_omp_construct (parser);
7418 return true;
7420 break;
7423 c_parser_consume_pragma (parser);
7424 c_invoke_pragma_handler (id);
7426 /* Skip to EOL, but suppress any error message. Those will have been
7427 generated by the handler routine through calling error, as opposed
7428 to calling c_parser_error. */
7429 parser->error = true;
7430 c_parser_skip_to_pragma_eol (parser);
7432 return false;
7435 /* The interface the pragma parsers have to the lexer. */
7437 enum cpp_ttype
7438 pragma_lex (tree *value)
7440 c_token *tok = c_parser_peek_token (the_parser);
7441 enum cpp_ttype ret = tok->type;
7443 *value = tok->value;
7444 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
7445 ret = CPP_EOF;
7446 else
7448 if (ret == CPP_KEYWORD)
7449 ret = CPP_NAME;
7450 c_parser_consume_token (the_parser);
7453 return ret;
7456 static void
7457 c_parser_pragma_pch_preprocess (c_parser *parser)
7459 tree name = NULL;
7461 c_parser_consume_pragma (parser);
7462 if (c_parser_next_token_is (parser, CPP_STRING))
7464 name = c_parser_peek_token (parser)->value;
7465 c_parser_consume_token (parser);
7467 else
7468 c_parser_error (parser, "expected string literal");
7469 c_parser_skip_to_pragma_eol (parser);
7471 if (name)
7472 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
7475 /* OpenMP 2.5 parsing routines. */
7477 /* Returns name of the next clause.
7478 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
7479 the token is not consumed. Otherwise appropriate pragma_omp_clause is
7480 returned and the token is consumed. */
7482 static pragma_omp_clause
7483 c_parser_omp_clause_name (c_parser *parser)
7485 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7487 if (c_parser_next_token_is_keyword (parser, RID_IF))
7488 result = PRAGMA_OMP_CLAUSE_IF;
7489 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7490 result = PRAGMA_OMP_CLAUSE_DEFAULT;
7491 else if (c_parser_next_token_is (parser, CPP_NAME))
7493 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7495 switch (p[0])
7497 case 'c':
7498 if (!strcmp ("collapse", p))
7499 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7500 else if (!strcmp ("copyin", p))
7501 result = PRAGMA_OMP_CLAUSE_COPYIN;
7502 else if (!strcmp ("copyprivate", p))
7503 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7504 break;
7505 case 'f':
7506 if (!strcmp ("firstprivate", p))
7507 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7508 break;
7509 case 'l':
7510 if (!strcmp ("lastprivate", p))
7511 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7512 break;
7513 case 'n':
7514 if (!strcmp ("nowait", p))
7515 result = PRAGMA_OMP_CLAUSE_NOWAIT;
7516 else if (!strcmp ("num_threads", p))
7517 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7518 break;
7519 case 'o':
7520 if (!strcmp ("ordered", p))
7521 result = PRAGMA_OMP_CLAUSE_ORDERED;
7522 break;
7523 case 'p':
7524 if (!strcmp ("private", p))
7525 result = PRAGMA_OMP_CLAUSE_PRIVATE;
7526 break;
7527 case 'r':
7528 if (!strcmp ("reduction", p))
7529 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7530 break;
7531 case 's':
7532 if (!strcmp ("schedule", p))
7533 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7534 else if (!strcmp ("shared", p))
7535 result = PRAGMA_OMP_CLAUSE_SHARED;
7536 break;
7537 case 'u':
7538 if (!strcmp ("untied", p))
7539 result = PRAGMA_OMP_CLAUSE_UNTIED;
7540 break;
7544 if (result != PRAGMA_OMP_CLAUSE_NONE)
7545 c_parser_consume_token (parser);
7547 return result;
7550 /* Validate that a clause of the given type does not already exist. */
7552 static void
7553 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7554 const char *name)
7556 tree c;
7558 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7559 if (OMP_CLAUSE_CODE (c) == code)
7561 location_t loc = OMP_CLAUSE_LOCATION (c);
7562 error_at (loc, "too many %qs clauses", name);
7563 break;
7567 /* OpenMP 2.5:
7568 variable-list:
7569 identifier
7570 variable-list , identifier
7572 If KIND is nonzero, create the appropriate node and install the
7573 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7574 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
7576 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7577 return the list created. */
7579 static tree
7580 c_parser_omp_variable_list (c_parser *parser,
7581 location_t clause_loc,
7582 enum omp_clause_code kind,
7583 tree list)
7585 if (c_parser_next_token_is_not (parser, CPP_NAME)
7586 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7587 c_parser_error (parser, "expected identifier");
7589 while (c_parser_next_token_is (parser, CPP_NAME)
7590 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7592 tree t = lookup_name (c_parser_peek_token (parser)->value);
7594 if (t == NULL_TREE)
7595 undeclared_variable (c_parser_peek_token (parser)->location,
7596 c_parser_peek_token (parser)->value);
7597 else if (t == error_mark_node)
7599 else if (kind != 0)
7601 tree u = build_omp_clause (clause_loc, kind);
7602 OMP_CLAUSE_DECL (u) = t;
7603 OMP_CLAUSE_CHAIN (u) = list;
7604 list = u;
7606 else
7607 list = tree_cons (t, NULL_TREE, list);
7609 c_parser_consume_token (parser);
7611 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7612 break;
7614 c_parser_consume_token (parser);
7617 return list;
7620 /* Similarly, but expect leading and trailing parenthesis. This is a very
7621 common case for omp clauses. */
7623 static tree
7624 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7625 tree list)
7627 /* The clauses location. */
7628 location_t loc = c_parser_peek_token (parser)->location;
7630 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7632 list = c_parser_omp_variable_list (parser, loc, kind, list);
7633 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7635 return list;
7638 /* OpenMP 3.0:
7639 collapse ( constant-expression ) */
7641 static tree
7642 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7644 tree c, num = error_mark_node;
7645 HOST_WIDE_INT n;
7646 location_t loc;
7648 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7650 loc = c_parser_peek_token (parser)->location;
7651 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7653 num = c_parser_expr_no_commas (parser, NULL).value;
7654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7656 if (num == error_mark_node)
7657 return list;
7658 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7659 || !host_integerp (num, 0)
7660 || (n = tree_low_cst (num, 0)) <= 0
7661 || (int) n != n)
7663 error_at (loc,
7664 "collapse argument needs positive constant integer expression");
7665 return list;
7667 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
7668 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7669 OMP_CLAUSE_CHAIN (c) = list;
7670 return c;
7673 /* OpenMP 2.5:
7674 copyin ( variable-list ) */
7676 static tree
7677 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7679 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7682 /* OpenMP 2.5:
7683 copyprivate ( variable-list ) */
7685 static tree
7686 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7688 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7691 /* OpenMP 2.5:
7692 default ( shared | none ) */
7694 static tree
7695 c_parser_omp_clause_default (c_parser *parser, tree list)
7697 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7698 location_t loc = c_parser_peek_token (parser)->location;
7699 tree c;
7701 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7702 return list;
7703 if (c_parser_next_token_is (parser, CPP_NAME))
7705 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7707 switch (p[0])
7709 case 'n':
7710 if (strcmp ("none", p) != 0)
7711 goto invalid_kind;
7712 kind = OMP_CLAUSE_DEFAULT_NONE;
7713 break;
7715 case 's':
7716 if (strcmp ("shared", p) != 0)
7717 goto invalid_kind;
7718 kind = OMP_CLAUSE_DEFAULT_SHARED;
7719 break;
7721 default:
7722 goto invalid_kind;
7725 c_parser_consume_token (parser);
7727 else
7729 invalid_kind:
7730 c_parser_error (parser, "expected %<none%> or %<shared%>");
7732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7734 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7735 return list;
7737 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7738 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
7739 OMP_CLAUSE_CHAIN (c) = list;
7740 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7742 return c;
7745 /* OpenMP 2.5:
7746 firstprivate ( variable-list ) */
7748 static tree
7749 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7751 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7754 /* OpenMP 2.5:
7755 if ( expression ) */
7757 static tree
7758 c_parser_omp_clause_if (c_parser *parser, tree list)
7760 location_t loc = c_parser_peek_token (parser)->location;
7761 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7763 tree t = c_parser_paren_condition (parser);
7764 tree c;
7766 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7768 c = build_omp_clause (loc, OMP_CLAUSE_IF);
7769 OMP_CLAUSE_IF_EXPR (c) = t;
7770 OMP_CLAUSE_CHAIN (c) = list;
7771 list = c;
7773 else
7774 c_parser_error (parser, "expected %<(%>");
7776 return list;
7779 /* OpenMP 2.5:
7780 lastprivate ( variable-list ) */
7782 static tree
7783 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7785 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7788 /* OpenMP 2.5:
7789 nowait */
7791 static tree
7792 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7794 tree c;
7795 location_t loc = c_parser_peek_token (parser)->location;
7797 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7799 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
7800 OMP_CLAUSE_CHAIN (c) = list;
7801 return c;
7804 /* OpenMP 2.5:
7805 num_threads ( expression ) */
7807 static tree
7808 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7810 location_t num_threads_loc = c_parser_peek_token (parser)->location;
7811 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7813 location_t expr_loc = c_parser_peek_token (parser)->location;
7814 tree c, t = c_parser_expression (parser).value;
7815 t = c_fully_fold (t, false, NULL);
7817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7819 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7821 c_parser_error (parser, "expected integer expression");
7822 return list;
7825 /* Attempt to statically determine when the number isn't positive. */
7826 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
7827 build_int_cst (TREE_TYPE (t), 0));
7828 if (CAN_HAVE_LOCATION_P (c))
7829 SET_EXPR_LOCATION (c, expr_loc);
7830 if (c == boolean_true_node)
7832 warning_at (expr_loc, 0,
7833 "%<num_threads%> value must be positive");
7834 t = integer_one_node;
7837 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7839 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
7840 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7841 OMP_CLAUSE_CHAIN (c) = list;
7842 list = c;
7845 return list;
7848 /* OpenMP 2.5:
7849 ordered */
7851 static tree
7852 c_parser_omp_clause_ordered (c_parser *parser, tree list)
7854 tree c;
7856 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7858 c = build_omp_clause (c_parser_peek_token (parser)->location,
7859 OMP_CLAUSE_ORDERED);
7860 OMP_CLAUSE_CHAIN (c) = list;
7862 return c;
7865 /* OpenMP 2.5:
7866 private ( variable-list ) */
7868 static tree
7869 c_parser_omp_clause_private (c_parser *parser, tree list)
7871 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7874 /* OpenMP 2.5:
7875 reduction ( reduction-operator : variable-list )
7877 reduction-operator:
7878 One of: + * - & ^ | && || */
7880 static tree
7881 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7883 location_t clause_loc = c_parser_peek_token (parser)->location;
7884 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7886 enum tree_code code;
7888 switch (c_parser_peek_token (parser)->type)
7890 case CPP_PLUS:
7891 code = PLUS_EXPR;
7892 break;
7893 case CPP_MULT:
7894 code = MULT_EXPR;
7895 break;
7896 case CPP_MINUS:
7897 code = MINUS_EXPR;
7898 break;
7899 case CPP_AND:
7900 code = BIT_AND_EXPR;
7901 break;
7902 case CPP_XOR:
7903 code = BIT_XOR_EXPR;
7904 break;
7905 case CPP_OR:
7906 code = BIT_IOR_EXPR;
7907 break;
7908 case CPP_AND_AND:
7909 code = TRUTH_ANDIF_EXPR;
7910 break;
7911 case CPP_OR_OR:
7912 code = TRUTH_ORIF_EXPR;
7913 break;
7914 default:
7915 c_parser_error (parser,
7916 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7917 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7918 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7919 return list;
7921 c_parser_consume_token (parser);
7922 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7924 tree nl, c;
7926 nl = c_parser_omp_variable_list (parser, clause_loc,
7927 OMP_CLAUSE_REDUCTION, list);
7928 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7929 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7931 list = nl;
7933 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7935 return list;
7938 /* OpenMP 2.5:
7939 schedule ( schedule-kind )
7940 schedule ( schedule-kind , expression )
7942 schedule-kind:
7943 static | dynamic | guided | runtime | auto
7946 static tree
7947 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7949 tree c, t;
7950 location_t loc = c_parser_peek_token (parser)->location;
7952 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7953 return list;
7955 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
7957 if (c_parser_next_token_is (parser, CPP_NAME))
7959 tree kind = c_parser_peek_token (parser)->value;
7960 const char *p = IDENTIFIER_POINTER (kind);
7962 switch (p[0])
7964 case 'd':
7965 if (strcmp ("dynamic", p) != 0)
7966 goto invalid_kind;
7967 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7968 break;
7970 case 'g':
7971 if (strcmp ("guided", p) != 0)
7972 goto invalid_kind;
7973 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7974 break;
7976 case 'r':
7977 if (strcmp ("runtime", p) != 0)
7978 goto invalid_kind;
7979 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7980 break;
7982 default:
7983 goto invalid_kind;
7986 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7987 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7988 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7989 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7990 else
7991 goto invalid_kind;
7993 c_parser_consume_token (parser);
7994 if (c_parser_next_token_is (parser, CPP_COMMA))
7996 location_t here;
7997 c_parser_consume_token (parser);
7999 here = c_parser_peek_token (parser)->location;
8000 t = c_parser_expr_no_commas (parser, NULL).value;
8001 t = c_fully_fold (t, false, NULL);
8003 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8004 error_at (here, "schedule %<runtime%> does not take "
8005 "a %<chunk_size%> parameter");
8006 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8007 error_at (here,
8008 "schedule %<auto%> does not take "
8009 "a %<chunk_size%> parameter");
8010 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8011 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8012 else
8013 c_parser_error (parser, "expected integer expression");
8015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8017 else
8018 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8019 "expected %<,%> or %<)%>");
8021 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8022 OMP_CLAUSE_CHAIN (c) = list;
8023 return c;
8025 invalid_kind:
8026 c_parser_error (parser, "invalid schedule kind");
8027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8028 return list;
8031 /* OpenMP 2.5:
8032 shared ( variable-list ) */
8034 static tree
8035 c_parser_omp_clause_shared (c_parser *parser, tree list)
8037 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8040 /* OpenMP 3.0:
8041 untied */
8043 static tree
8044 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8046 tree c;
8048 /* FIXME: Should we allow duplicates? */
8049 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8051 c = build_omp_clause (c_parser_peek_token (parser)->location,
8052 OMP_CLAUSE_UNTIED);
8053 OMP_CLAUSE_CHAIN (c) = list;
8055 return c;
8058 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8059 is a bitmask in MASK. Return the list of clauses found; the result
8060 of clause default goes in *pdefault. */
8062 static tree
8063 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8064 const char *where)
8066 tree clauses = NULL;
8067 bool first = true;
8069 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8071 location_t here;
8072 pragma_omp_clause c_kind;
8073 const char *c_name;
8074 tree prev = clauses;
8076 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8077 c_parser_consume_token (parser);
8079 first = false;
8080 here = c_parser_peek_token (parser)->location;
8081 c_kind = c_parser_omp_clause_name (parser);
8083 switch (c_kind)
8085 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8086 clauses = c_parser_omp_clause_collapse (parser, clauses);
8087 c_name = "collapse";
8088 break;
8089 case PRAGMA_OMP_CLAUSE_COPYIN:
8090 clauses = c_parser_omp_clause_copyin (parser, clauses);
8091 c_name = "copyin";
8092 break;
8093 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8094 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8095 c_name = "copyprivate";
8096 break;
8097 case PRAGMA_OMP_CLAUSE_DEFAULT:
8098 clauses = c_parser_omp_clause_default (parser, clauses);
8099 c_name = "default";
8100 break;
8101 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8102 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8103 c_name = "firstprivate";
8104 break;
8105 case PRAGMA_OMP_CLAUSE_IF:
8106 clauses = c_parser_omp_clause_if (parser, clauses);
8107 c_name = "if";
8108 break;
8109 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8110 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8111 c_name = "lastprivate";
8112 break;
8113 case PRAGMA_OMP_CLAUSE_NOWAIT:
8114 clauses = c_parser_omp_clause_nowait (parser, clauses);
8115 c_name = "nowait";
8116 break;
8117 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8118 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8119 c_name = "num_threads";
8120 break;
8121 case PRAGMA_OMP_CLAUSE_ORDERED:
8122 clauses = c_parser_omp_clause_ordered (parser, clauses);
8123 c_name = "ordered";
8124 break;
8125 case PRAGMA_OMP_CLAUSE_PRIVATE:
8126 clauses = c_parser_omp_clause_private (parser, clauses);
8127 c_name = "private";
8128 break;
8129 case PRAGMA_OMP_CLAUSE_REDUCTION:
8130 clauses = c_parser_omp_clause_reduction (parser, clauses);
8131 c_name = "reduction";
8132 break;
8133 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8134 clauses = c_parser_omp_clause_schedule (parser, clauses);
8135 c_name = "schedule";
8136 break;
8137 case PRAGMA_OMP_CLAUSE_SHARED:
8138 clauses = c_parser_omp_clause_shared (parser, clauses);
8139 c_name = "shared";
8140 break;
8141 case PRAGMA_OMP_CLAUSE_UNTIED:
8142 clauses = c_parser_omp_clause_untied (parser, clauses);
8143 c_name = "untied";
8144 break;
8145 default:
8146 c_parser_error (parser, "expected %<#pragma omp%> clause");
8147 goto saw_error;
8150 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8152 /* Remove the invalid clause(s) from the list to avoid
8153 confusing the rest of the compiler. */
8154 clauses = prev;
8155 error_at (here, "%qs is not valid for %qs", c_name, where);
8159 saw_error:
8160 c_parser_skip_to_pragma_eol (parser);
8162 return c_finish_omp_clauses (clauses);
8165 /* OpenMP 2.5:
8166 structured-block:
8167 statement
8169 In practice, we're also interested in adding the statement to an
8170 outer node. So it is convenient if we work around the fact that
8171 c_parser_statement calls add_stmt. */
8173 static tree
8174 c_parser_omp_structured_block (c_parser *parser)
8176 tree stmt = push_stmt_list ();
8177 c_parser_statement (parser);
8178 return pop_stmt_list (stmt);
8181 /* OpenMP 2.5:
8182 # pragma omp atomic new-line
8183 expression-stmt
8185 expression-stmt:
8186 x binop= expr | x++ | ++x | x-- | --x
8187 binop:
8188 +, *, -, /, &, ^, |, <<, >>
8190 where x is an lvalue expression with scalar type.
8192 LOC is the location of the #pragma token. */
8194 static void
8195 c_parser_omp_atomic (location_t loc, c_parser *parser)
8197 tree lhs, rhs;
8198 tree stmt;
8199 enum tree_code code;
8200 struct c_expr rhs_expr;
8202 c_parser_skip_to_pragma_eol (parser);
8204 lhs = c_parser_unary_expression (parser).value;
8205 lhs = c_fully_fold (lhs, false, NULL);
8206 switch (TREE_CODE (lhs))
8208 case ERROR_MARK:
8209 saw_error:
8210 c_parser_skip_to_end_of_block_or_statement (parser);
8211 return;
8213 case PREINCREMENT_EXPR:
8214 case POSTINCREMENT_EXPR:
8215 lhs = TREE_OPERAND (lhs, 0);
8216 code = PLUS_EXPR;
8217 rhs = integer_one_node;
8218 break;
8220 case PREDECREMENT_EXPR:
8221 case POSTDECREMENT_EXPR:
8222 lhs = TREE_OPERAND (lhs, 0);
8223 code = MINUS_EXPR;
8224 rhs = integer_one_node;
8225 break;
8227 case COMPOUND_EXPR:
8228 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8229 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8230 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8231 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8232 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8233 (TREE_OPERAND (lhs, 1), 0), 0)))
8234 == BOOLEAN_TYPE)
8235 /* Undo effects of boolean_increment for post {in,de}crement. */
8236 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
8237 /* FALLTHRU */
8238 case MODIFY_EXPR:
8239 if (TREE_CODE (lhs) == MODIFY_EXPR
8240 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
8242 /* Undo effects of boolean_increment. */
8243 if (integer_onep (TREE_OPERAND (lhs, 1)))
8245 /* This is pre or post increment. */
8246 rhs = TREE_OPERAND (lhs, 1);
8247 lhs = TREE_OPERAND (lhs, 0);
8248 code = NOP_EXPR;
8249 break;
8251 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
8252 && TREE_OPERAND (lhs, 0)
8253 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
8255 /* This is pre or post decrement. */
8256 rhs = TREE_OPERAND (lhs, 1);
8257 lhs = TREE_OPERAND (lhs, 0);
8258 code = NOP_EXPR;
8259 break;
8262 /* FALLTHRU */
8263 default:
8264 switch (c_parser_peek_token (parser)->type)
8266 case CPP_MULT_EQ:
8267 code = MULT_EXPR;
8268 break;
8269 case CPP_DIV_EQ:
8270 code = TRUNC_DIV_EXPR;
8271 break;
8272 case CPP_PLUS_EQ:
8273 code = PLUS_EXPR;
8274 break;
8275 case CPP_MINUS_EQ:
8276 code = MINUS_EXPR;
8277 break;
8278 case CPP_LSHIFT_EQ:
8279 code = LSHIFT_EXPR;
8280 break;
8281 case CPP_RSHIFT_EQ:
8282 code = RSHIFT_EXPR;
8283 break;
8284 case CPP_AND_EQ:
8285 code = BIT_AND_EXPR;
8286 break;
8287 case CPP_OR_EQ:
8288 code = BIT_IOR_EXPR;
8289 break;
8290 case CPP_XOR_EQ:
8291 code = BIT_XOR_EXPR;
8292 break;
8293 default:
8294 c_parser_error (parser,
8295 "invalid operator for %<#pragma omp atomic%>");
8296 goto saw_error;
8299 c_parser_consume_token (parser);
8301 location_t rhs_loc = c_parser_peek_token (parser)->location;
8302 rhs_expr = c_parser_expression (parser);
8303 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
8305 rhs = rhs_expr.value;
8306 rhs = c_fully_fold (rhs, false, NULL);
8307 break;
8309 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
8310 if (stmt != error_mark_node)
8311 add_stmt (stmt);
8312 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8316 /* OpenMP 2.5:
8317 # pragma omp barrier new-line
8320 static void
8321 c_parser_omp_barrier (c_parser *parser)
8323 location_t loc = c_parser_peek_token (parser)->location;
8324 c_parser_consume_pragma (parser);
8325 c_parser_skip_to_pragma_eol (parser);
8327 c_finish_omp_barrier (loc);
8330 /* OpenMP 2.5:
8331 # pragma omp critical [(name)] new-line
8332 structured-block
8334 LOC is the location of the #pragma itself. */
8336 static tree
8337 c_parser_omp_critical (location_t loc, c_parser *parser)
8339 tree stmt, name = NULL;
8341 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8343 c_parser_consume_token (parser);
8344 if (c_parser_next_token_is (parser, CPP_NAME))
8346 name = c_parser_peek_token (parser)->value;
8347 c_parser_consume_token (parser);
8348 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8350 else
8351 c_parser_error (parser, "expected identifier");
8353 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8354 c_parser_error (parser, "expected %<(%> or end of line");
8355 c_parser_skip_to_pragma_eol (parser);
8357 stmt = c_parser_omp_structured_block (parser);
8358 return c_finish_omp_critical (loc, stmt, name);
8361 /* OpenMP 2.5:
8362 # pragma omp flush flush-vars[opt] new-line
8364 flush-vars:
8365 ( variable-list ) */
8367 static void
8368 c_parser_omp_flush (c_parser *parser)
8370 location_t loc = c_parser_peek_token (parser)->location;
8371 c_parser_consume_pragma (parser);
8372 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8373 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8374 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8375 c_parser_error (parser, "expected %<(%> or end of line");
8376 c_parser_skip_to_pragma_eol (parser);
8378 c_finish_omp_flush (loc);
8381 /* Parse the restricted form of the for statement allowed by OpenMP.
8382 The real trick here is to determine the loop control variable early
8383 so that we can push a new decl if necessary to make it private.
8384 LOC is the location of the OMP in "#pragma omp". */
8386 static tree
8387 c_parser_omp_for_loop (location_t loc,
8388 c_parser *parser, tree clauses, tree *par_clauses)
8390 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
8391 tree declv, condv, incrv, initv, ret = NULL;
8392 bool fail = false, open_brace_parsed = false;
8393 int i, collapse = 1, nbraces = 0;
8394 location_t for_loc;
8395 VEC(tree,gc) *for_block = make_tree_vector ();
8397 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8398 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8399 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8401 gcc_assert (collapse >= 1);
8403 declv = make_tree_vec (collapse);
8404 initv = make_tree_vec (collapse);
8405 condv = make_tree_vec (collapse);
8406 incrv = make_tree_vec (collapse);
8408 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8410 c_parser_error (parser, "for statement expected");
8411 return NULL;
8413 for_loc = c_parser_peek_token (parser)->location;
8414 c_parser_consume_token (parser);
8416 for (i = 0; i < collapse; i++)
8418 int bracecount = 0;
8420 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8421 goto pop_scopes;
8423 /* Parse the initialization declaration or expression. */
8424 if (c_parser_next_token_starts_declaration (parser))
8426 if (i > 0)
8427 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
8428 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
8429 decl = check_for_loop_decls (for_loc);
8430 if (decl == NULL)
8431 goto error_init;
8432 if (DECL_INITIAL (decl) == error_mark_node)
8433 decl = error_mark_node;
8434 init = decl;
8436 else if (c_parser_next_token_is (parser, CPP_NAME)
8437 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
8439 struct c_expr decl_exp;
8440 struct c_expr init_exp;
8441 location_t init_loc;
8443 decl_exp = c_parser_postfix_expression (parser);
8444 decl = decl_exp.value;
8446 c_parser_require (parser, CPP_EQ, "expected %<=%>");
8448 init_loc = c_parser_peek_token (parser)->location;
8449 init_exp = c_parser_expr_no_commas (parser, NULL);
8450 init_exp = default_function_array_read_conversion (init_loc,
8451 init_exp);
8452 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
8453 NOP_EXPR, init_loc, init_exp.value,
8454 init_exp.original_type);
8455 init = c_process_expr_stmt (init_loc, init);
8457 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8459 else
8461 error_init:
8462 c_parser_error (parser,
8463 "expected iteration declaration or initialization");
8464 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8465 "expected %<)%>");
8466 fail = true;
8467 goto parse_next;
8470 /* Parse the loop condition. */
8471 cond = NULL_TREE;
8472 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
8474 location_t cond_loc = c_parser_peek_token (parser)->location;
8475 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
8477 cond = cond_expr.value;
8478 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
8479 cond = c_fully_fold (cond, false, NULL);
8480 switch (cond_expr.original_code)
8482 case GT_EXPR:
8483 case GE_EXPR:
8484 case LT_EXPR:
8485 case LE_EXPR:
8486 break;
8487 default:
8488 /* Can't be cond = error_mark_node, because we want to preserve
8489 the location until c_finish_omp_for. */
8490 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
8491 break;
8493 protected_set_expr_location (cond, cond_loc);
8495 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8497 /* Parse the increment expression. */
8498 incr = NULL_TREE;
8499 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
8501 location_t incr_loc = c_parser_peek_token (parser)->location;
8503 incr = c_process_expr_stmt (incr_loc,
8504 c_parser_expression (parser).value);
8506 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8508 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
8509 fail = true;
8510 else
8512 TREE_VEC_ELT (declv, i) = decl;
8513 TREE_VEC_ELT (initv, i) = init;
8514 TREE_VEC_ELT (condv, i) = cond;
8515 TREE_VEC_ELT (incrv, i) = incr;
8518 parse_next:
8519 if (i == collapse - 1)
8520 break;
8522 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8523 in between the collapsed for loops to be still considered perfectly
8524 nested. Hopefully the final version clarifies this.
8525 For now handle (multiple) {'s and empty statements. */
8528 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8530 c_parser_consume_token (parser);
8531 break;
8533 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8535 c_parser_consume_token (parser);
8536 bracecount++;
8538 else if (bracecount
8539 && c_parser_next_token_is (parser, CPP_SEMICOLON))
8540 c_parser_consume_token (parser);
8541 else
8543 c_parser_error (parser, "not enough perfectly nested loops");
8544 if (bracecount)
8546 open_brace_parsed = true;
8547 bracecount--;
8549 fail = true;
8550 collapse = 0;
8551 break;
8554 while (1);
8556 nbraces += bracecount;
8559 save_break = c_break_label;
8560 c_break_label = size_one_node;
8561 save_cont = c_cont_label;
8562 c_cont_label = NULL_TREE;
8563 body = push_stmt_list ();
8565 if (open_brace_parsed)
8567 location_t here = c_parser_peek_token (parser)->location;
8568 stmt = c_begin_compound_stmt (true);
8569 c_parser_compound_statement_nostart (parser);
8570 add_stmt (c_end_compound_stmt (here, stmt, true));
8572 else
8573 add_stmt (c_parser_c99_block_statement (parser));
8574 if (c_cont_label)
8576 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8577 SET_EXPR_LOCATION (t, loc);
8578 add_stmt (t);
8581 body = pop_stmt_list (body);
8582 c_break_label = save_break;
8583 c_cont_label = save_cont;
8585 while (nbraces)
8587 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8589 c_parser_consume_token (parser);
8590 nbraces--;
8592 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8593 c_parser_consume_token (parser);
8594 else
8596 c_parser_error (parser, "collapsed loops not perfectly nested");
8597 while (nbraces)
8599 location_t here = c_parser_peek_token (parser)->location;
8600 stmt = c_begin_compound_stmt (true);
8601 add_stmt (body);
8602 c_parser_compound_statement_nostart (parser);
8603 body = c_end_compound_stmt (here, stmt, true);
8604 nbraces--;
8606 goto pop_scopes;
8610 /* Only bother calling c_finish_omp_for if we haven't already generated
8611 an error from the initialization parsing. */
8612 if (!fail)
8614 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8615 if (stmt)
8617 if (par_clauses != NULL)
8619 tree *c;
8620 for (c = par_clauses; *c ; )
8621 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8622 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8623 c = &OMP_CLAUSE_CHAIN (*c);
8624 else
8626 for (i = 0; i < collapse; i++)
8627 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8628 break;
8629 if (i == collapse)
8630 c = &OMP_CLAUSE_CHAIN (*c);
8631 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8633 error_at (loc,
8634 "iteration variable %qD should not be firstprivate",
8635 OMP_CLAUSE_DECL (*c));
8636 *c = OMP_CLAUSE_CHAIN (*c);
8638 else
8640 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8641 change it to shared (decl) in
8642 OMP_PARALLEL_CLAUSES. */
8643 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8644 OMP_CLAUSE_LASTPRIVATE);
8645 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8646 OMP_CLAUSE_CHAIN (l) = clauses;
8647 clauses = l;
8648 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8652 OMP_FOR_CLAUSES (stmt) = clauses;
8654 ret = stmt;
8656 pop_scopes:
8657 while (!VEC_empty (tree, for_block))
8659 /* FIXME diagnostics: LOC below should be the actual location of
8660 this particular for block. We need to build a list of
8661 locations to go along with FOR_BLOCK. */
8662 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
8663 add_stmt (stmt);
8665 release_tree_vector (for_block);
8666 return ret;
8669 /* OpenMP 2.5:
8670 #pragma omp for for-clause[optseq] new-line
8671 for-loop
8673 LOC is the location of the #pragma token.
8676 #define OMP_FOR_CLAUSE_MASK \
8677 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8678 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8679 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8680 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8681 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8682 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
8683 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
8684 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8686 static tree
8687 c_parser_omp_for (location_t loc, c_parser *parser)
8689 tree block, clauses, ret;
8691 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8692 "#pragma omp for");
8694 block = c_begin_compound_stmt (true);
8695 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8696 block = c_end_compound_stmt (loc, block, true);
8697 add_stmt (block);
8699 return ret;
8702 /* OpenMP 2.5:
8703 # pragma omp master new-line
8704 structured-block
8706 LOC is the location of the #pragma token.
8709 static tree
8710 c_parser_omp_master (location_t loc, c_parser *parser)
8712 c_parser_skip_to_pragma_eol (parser);
8713 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
8716 /* OpenMP 2.5:
8717 # pragma omp ordered new-line
8718 structured-block
8720 LOC is the location of the #pragma itself.
8723 static tree
8724 c_parser_omp_ordered (location_t loc, c_parser *parser)
8726 c_parser_skip_to_pragma_eol (parser);
8727 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
8730 /* OpenMP 2.5:
8732 section-scope:
8733 { section-sequence }
8735 section-sequence:
8736 section-directive[opt] structured-block
8737 section-sequence section-directive structured-block
8739 SECTIONS_LOC is the location of the #pragma omp sections. */
8741 static tree
8742 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
8744 tree stmt, substmt;
8745 bool error_suppress = false;
8746 location_t loc;
8748 loc = c_parser_peek_token (parser)->location;
8749 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8751 /* Avoid skipping until the end of the block. */
8752 parser->error = false;
8753 return NULL_TREE;
8756 stmt = push_stmt_list ();
8758 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8760 substmt = push_stmt_list ();
8762 while (1)
8764 c_parser_statement (parser);
8766 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8767 break;
8768 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8769 break;
8770 if (c_parser_next_token_is (parser, CPP_EOF))
8771 break;
8774 substmt = pop_stmt_list (substmt);
8775 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8776 SET_EXPR_LOCATION (substmt, loc);
8777 add_stmt (substmt);
8780 while (1)
8782 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8783 break;
8784 if (c_parser_next_token_is (parser, CPP_EOF))
8785 break;
8787 loc = c_parser_peek_token (parser)->location;
8788 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8790 c_parser_consume_pragma (parser);
8791 c_parser_skip_to_pragma_eol (parser);
8792 error_suppress = false;
8794 else if (!error_suppress)
8796 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8797 error_suppress = true;
8800 substmt = c_parser_omp_structured_block (parser);
8801 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8802 SET_EXPR_LOCATION (substmt, loc);
8803 add_stmt (substmt);
8805 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8806 "expected %<#pragma omp section%> or %<}%>");
8808 substmt = pop_stmt_list (stmt);
8810 stmt = make_node (OMP_SECTIONS);
8811 SET_EXPR_LOCATION (stmt, sections_loc);
8812 TREE_TYPE (stmt) = void_type_node;
8813 OMP_SECTIONS_BODY (stmt) = substmt;
8815 return add_stmt (stmt);
8818 /* OpenMP 2.5:
8819 # pragma omp sections sections-clause[optseq] newline
8820 sections-scope
8822 LOC is the location of the #pragma token.
8825 #define OMP_SECTIONS_CLAUSE_MASK \
8826 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8827 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8828 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8829 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8830 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8832 static tree
8833 c_parser_omp_sections (location_t loc, c_parser *parser)
8835 tree block, clauses, ret;
8837 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8838 "#pragma omp sections");
8840 block = c_begin_compound_stmt (true);
8841 ret = c_parser_omp_sections_scope (loc, parser);
8842 if (ret)
8843 OMP_SECTIONS_CLAUSES (ret) = clauses;
8844 block = c_end_compound_stmt (loc, block, true);
8845 add_stmt (block);
8847 return ret;
8850 /* OpenMP 2.5:
8851 # pragma parallel parallel-clause new-line
8852 # pragma parallel for parallel-for-clause new-line
8853 # pragma parallel sections parallel-sections-clause new-line
8855 LOC is the location of the #pragma token.
8858 #define OMP_PARALLEL_CLAUSE_MASK \
8859 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8860 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8861 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8862 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8863 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8864 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8865 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8866 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8868 static tree
8869 c_parser_omp_parallel (location_t loc, c_parser *parser)
8871 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8872 const char *p_name = "#pragma omp parallel";
8873 tree stmt, clauses, par_clause, ws_clause, block;
8874 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8876 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8878 c_parser_consume_token (parser);
8879 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8880 p_name = "#pragma omp parallel for";
8881 mask |= OMP_FOR_CLAUSE_MASK;
8882 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8884 else if (c_parser_next_token_is (parser, CPP_NAME))
8886 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8887 if (strcmp (p, "sections") == 0)
8889 c_parser_consume_token (parser);
8890 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8891 p_name = "#pragma omp parallel sections";
8892 mask |= OMP_SECTIONS_CLAUSE_MASK;
8893 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8897 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8899 switch (p_kind)
8901 case PRAGMA_OMP_PARALLEL:
8902 block = c_begin_omp_parallel ();
8903 c_parser_statement (parser);
8904 stmt = c_finish_omp_parallel (loc, clauses, block);
8905 break;
8907 case PRAGMA_OMP_PARALLEL_FOR:
8908 block = c_begin_omp_parallel ();
8909 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8910 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
8911 stmt = c_finish_omp_parallel (loc, par_clause, block);
8912 OMP_PARALLEL_COMBINED (stmt) = 1;
8913 break;
8915 case PRAGMA_OMP_PARALLEL_SECTIONS:
8916 block = c_begin_omp_parallel ();
8917 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8918 stmt = c_parser_omp_sections_scope (loc, parser);
8919 if (stmt)
8920 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8921 stmt = c_finish_omp_parallel (loc, par_clause, block);
8922 OMP_PARALLEL_COMBINED (stmt) = 1;
8923 break;
8925 default:
8926 gcc_unreachable ();
8929 return stmt;
8932 /* OpenMP 2.5:
8933 # pragma omp single single-clause[optseq] new-line
8934 structured-block
8936 LOC is the location of the #pragma.
8939 #define OMP_SINGLE_CLAUSE_MASK \
8940 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8941 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8942 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8943 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8945 static tree
8946 c_parser_omp_single (location_t loc, c_parser *parser)
8948 tree stmt = make_node (OMP_SINGLE);
8949 SET_EXPR_LOCATION (stmt, loc);
8950 TREE_TYPE (stmt) = void_type_node;
8952 OMP_SINGLE_CLAUSES (stmt)
8953 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8954 "#pragma omp single");
8955 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8957 return add_stmt (stmt);
8960 /* OpenMP 3.0:
8961 # pragma omp task task-clause[optseq] new-line
8963 LOC is the location of the #pragma.
8966 #define OMP_TASK_CLAUSE_MASK \
8967 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8968 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8969 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8970 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8971 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8972 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8974 static tree
8975 c_parser_omp_task (location_t loc, c_parser *parser)
8977 tree clauses, block;
8979 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8980 "#pragma omp task");
8982 block = c_begin_omp_task ();
8983 c_parser_statement (parser);
8984 return c_finish_omp_task (loc, clauses, block);
8987 /* OpenMP 3.0:
8988 # pragma omp taskwait new-line
8991 static void
8992 c_parser_omp_taskwait (c_parser *parser)
8994 location_t loc = c_parser_peek_token (parser)->location;
8995 c_parser_consume_pragma (parser);
8996 c_parser_skip_to_pragma_eol (parser);
8998 c_finish_omp_taskwait (loc);
9001 /* Main entry point to parsing most OpenMP pragmas. */
9003 static void
9004 c_parser_omp_construct (c_parser *parser)
9006 enum pragma_kind p_kind;
9007 location_t loc;
9008 tree stmt;
9010 loc = c_parser_peek_token (parser)->location;
9011 p_kind = c_parser_peek_token (parser)->pragma_kind;
9012 c_parser_consume_pragma (parser);
9014 switch (p_kind)
9016 case PRAGMA_OMP_ATOMIC:
9017 c_parser_omp_atomic (loc, parser);
9018 return;
9019 case PRAGMA_OMP_CRITICAL:
9020 stmt = c_parser_omp_critical (loc, parser);
9021 break;
9022 case PRAGMA_OMP_FOR:
9023 stmt = c_parser_omp_for (loc, parser);
9024 break;
9025 case PRAGMA_OMP_MASTER:
9026 stmt = c_parser_omp_master (loc, parser);
9027 break;
9028 case PRAGMA_OMP_ORDERED:
9029 stmt = c_parser_omp_ordered (loc, parser);
9030 break;
9031 case PRAGMA_OMP_PARALLEL:
9032 stmt = c_parser_omp_parallel (loc, parser);
9033 break;
9034 case PRAGMA_OMP_SECTIONS:
9035 stmt = c_parser_omp_sections (loc, parser);
9036 break;
9037 case PRAGMA_OMP_SINGLE:
9038 stmt = c_parser_omp_single (loc, parser);
9039 break;
9040 case PRAGMA_OMP_TASK:
9041 stmt = c_parser_omp_task (loc, parser);
9042 break;
9043 default:
9044 gcc_unreachable ();
9047 if (stmt)
9048 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9052 /* OpenMP 2.5:
9053 # pragma omp threadprivate (variable-list) */
9055 static void
9056 c_parser_omp_threadprivate (c_parser *parser)
9058 tree vars, t;
9059 location_t loc;
9061 c_parser_consume_pragma (parser);
9062 loc = c_parser_peek_token (parser)->location;
9063 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9065 /* Mark every variable in VARS to be assigned thread local storage. */
9066 for (t = vars; t; t = TREE_CHAIN (t))
9068 tree v = TREE_PURPOSE (t);
9070 /* FIXME diagnostics: Ideally we should keep individual
9071 locations for all the variables in the var list to make the
9072 following errors more precise. Perhaps
9073 c_parser_omp_var_list_parens() should construct a list of
9074 locations to go along with the var list. */
9076 /* If V had already been marked threadprivate, it doesn't matter
9077 whether it had been used prior to this point. */
9078 if (TREE_CODE (v) != VAR_DECL)
9079 error_at (loc, "%qD is not a variable", v);
9080 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9081 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9082 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9083 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9084 else if (TREE_TYPE (v) == error_mark_node)
9086 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9087 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9088 else
9090 if (! DECL_THREAD_LOCAL_P (v))
9092 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9093 /* If rtl has been already set for this var, call
9094 make_decl_rtl once again, so that encode_section_info
9095 has a chance to look at the new decl flags. */
9096 if (DECL_RTL_SET_P (v))
9097 make_decl_rtl (v);
9099 C_DECL_THREADPRIVATE_P (v) = 1;
9103 c_parser_skip_to_pragma_eol (parser);
9107 /* Parse a single source file. */
9109 void
9110 c_parse_file (void)
9112 /* Use local storage to begin. If the first token is a pragma, parse it.
9113 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9114 which will cause garbage collection. */
9115 c_parser tparser;
9117 memset (&tparser, 0, sizeof tparser);
9118 the_parser = &tparser;
9120 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9121 c_parser_pragma_pch_preprocess (&tparser);
9123 the_parser = ggc_alloc_c_parser ();
9124 *the_parser = tparser;
9126 /* Initialize EH, if we've been told to do so. */
9127 if (flag_exceptions)
9128 using_eh_for_cleanups ();
9130 c_parser_translation_unit (the_parser);
9131 the_parser = NULL;
9134 #include "gt-c-parser.h"