2010-10-29 Paolo Bonzini <bonzini@gnu.org>
[official-gcc.git] / gcc / c-parser.c
blob932ab31e649b883fd37737c2ee1fbf303601f6b0
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 /* True if we are parsing a (potential) Objective-C foreach
189 statement. This is set to true after we parsed 'for (' and while
190 we wait for 'in' or ';' to decide if it's a standard C for loop or an
191 Objective-C foreach loop. */
192 BOOL_BITFIELD objc_could_be_foreach_context : 1;
193 /* The following flag is needed to contextualize Objective-C lexical
194 analysis. In some cases (e.g., 'int NSObject;'), it is
195 undesirable to bind an identifier to an Objective-C class, even
196 if a class with that name exists. */
197 BOOL_BITFIELD objc_need_raw_identifier : 1;
198 /* True if we are in a context where the Objective-C "Property attribute"
199 keywords are valid. */
200 BOOL_BITFIELD objc_property_attr_context : 1;
201 } c_parser;
204 /* The actual parser and external interface. ??? Does this need to be
205 garbage-collected? */
207 static GTY (()) c_parser *the_parser;
209 /* Read in and lex a single token, storing it in *TOKEN. */
211 static void
212 c_lex_one_token (c_parser *parser, c_token *token)
214 timevar_push (TV_LEX);
216 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
217 (parser->lex_untranslated_string
218 ? C_LEX_STRING_NO_TRANSLATE : 0));
219 token->id_kind = C_ID_NONE;
220 token->keyword = RID_MAX;
221 token->pragma_kind = PRAGMA_NONE;
223 switch (token->type)
225 case CPP_NAME:
227 tree decl;
229 bool objc_force_identifier = parser->objc_need_raw_identifier;
230 if (c_dialect_objc ())
231 parser->objc_need_raw_identifier = false;
233 if (C_IS_RESERVED_WORD (token->value))
235 enum rid rid_code = C_RID_CODE (token->value);
237 if (rid_code == RID_CXX_COMPAT_WARN)
239 warning_at (token->location,
240 OPT_Wc___compat,
241 "identifier %qE conflicts with C++ keyword",
242 token->value);
244 else if (rid_code >= RID_FIRST_ADDR_SPACE
245 && rid_code <= RID_LAST_ADDR_SPACE)
247 token->id_kind = C_ID_ADDRSPACE;
248 token->keyword = rid_code;
249 break;
251 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
253 /* We found an Objective-C "pq" keyword (in, out,
254 inout, bycopy, byref, oneway). They need special
255 care because the interpretation depends on the
256 context.
258 if (parser->objc_pq_context)
260 token->type = CPP_KEYWORD;
261 token->keyword = rid_code;
262 break;
264 else if (parser->objc_could_be_foreach_context
265 && rid_code == RID_IN)
267 /* We are in Objective-C, inside a (potential)
268 foreach context (which means after having
269 parsed 'for (', but before having parsed ';'),
270 and we found 'in'. We consider it the keyword
271 which terminates the declaration at the
272 beginning of a foreach-statement. Note that
273 this means you can't use 'in' for anything else
274 in that context; in particular, in Objective-C
275 you can't use 'in' as the name of the running
276 variable in a C for loop. We could potentially
277 try to add code here to disambiguate, but it
278 seems a reasonable limitation.
280 token->type = CPP_KEYWORD;
281 token->keyword = rid_code;
282 break;
284 /* Else, "pq" keywords outside of the "pq" context are
285 not keywords, and we fall through to the code for
286 normal tokens.
289 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
291 /* We found an Objective-C "property attribute" keyword
292 (readonly, copies, getter, setter, ivar). These are
293 only valid in the property context. */
294 if (parser->objc_property_attr_context)
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
298 break;
300 /* Else they are not special keywords.
303 else if (c_dialect_objc ()
304 && (OBJC_IS_AT_KEYWORD (rid_code)
305 || OBJC_IS_CXX_KEYWORD (rid_code)))
307 /* We found one of the Objective-C "@" keywords (defs,
308 selector, synchronized, etc) or one of the
309 Objective-C "cxx" keywords (class, private,
310 protected, public, try, catch, throw) without a
311 preceding '@' sign. Do nothing and fall through to
312 the code for normal tokens (in C++ we would still
313 consider the CXX ones keywords, but not in C).
317 else
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
325 decl = lookup_name (token->value);
326 if (decl)
328 if (TREE_CODE (decl) == TYPE_DECL)
330 token->id_kind = C_ID_TYPENAME;
331 break;
334 else if (c_dialect_objc ())
336 tree objc_interface_decl = objc_is_class_name (token->value);
337 /* Objective-C class names are in the same namespace as
338 variables and typedefs, and hence are shadowed by local
339 declarations. */
340 if (objc_interface_decl
341 && (global_bindings_p ()
342 || (!objc_force_identifier && !decl)))
344 token->value = objc_interface_decl;
345 token->id_kind = C_ID_CLASSNAME;
346 break;
349 token->id_kind = C_ID_ID;
351 break;
352 case CPP_AT_NAME:
353 /* This only happens in Objective-C; it must be a keyword. */
354 token->type = CPP_KEYWORD;
355 switch (C_RID_CODE (token->value))
357 /* Replace 'class' with '@class', 'private' with '@private',
358 etc. This prevents confusion with the C++ keyword
359 'class', and makes the tokens consistent with other
360 Objective-C 'AT' keywords. For example '@class' is
361 reported as RID_AT_CLASS which is consistent with
362 '@synchronized', which is reported as
363 RID_AT_SYNCHRONIZED.
365 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
366 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
367 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
368 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
369 case RID_THROW: token->keyword = RID_AT_THROW; break;
370 case RID_TRY: token->keyword = RID_AT_TRY; break;
371 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
372 default: token->keyword = C_RID_CODE (token->value);
374 break;
375 case CPP_COLON:
376 case CPP_COMMA:
377 case CPP_CLOSE_PAREN:
378 case CPP_SEMICOLON:
379 /* These tokens may affect the interpretation of any identifiers
380 following, if doing Objective-C. */
381 if (c_dialect_objc ())
382 parser->objc_need_raw_identifier = false;
383 break;
384 case CPP_PRAGMA:
385 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
386 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
387 token->value = NULL;
388 break;
389 default:
390 break;
392 timevar_pop (TV_LEX);
395 /* Return a pointer to the next token from PARSER, reading it in if
396 necessary. */
398 static inline c_token *
399 c_parser_peek_token (c_parser *parser)
401 if (parser->tokens_avail == 0)
403 c_lex_one_token (parser, &parser->tokens[0]);
404 parser->tokens_avail = 1;
406 return &parser->tokens[0];
409 /* Return true if the next token from PARSER has the indicated
410 TYPE. */
412 static inline bool
413 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
415 return c_parser_peek_token (parser)->type == type;
418 /* Return true if the next token from PARSER does not have the
419 indicated TYPE. */
421 static inline bool
422 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
424 return !c_parser_next_token_is (parser, type);
427 /* Return true if the next token from PARSER is the indicated
428 KEYWORD. */
430 static inline bool
431 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
433 return c_parser_peek_token (parser)->keyword == keyword;
436 /* Return true if TOKEN can start a type name,
437 false otherwise. */
438 static bool
439 c_token_starts_typename (c_token *token)
441 switch (token->type)
443 case CPP_NAME:
444 switch (token->id_kind)
446 case C_ID_ID:
447 return false;
448 case C_ID_ADDRSPACE:
449 return true;
450 case C_ID_TYPENAME:
451 return true;
452 case C_ID_CLASSNAME:
453 gcc_assert (c_dialect_objc ());
454 return true;
455 default:
456 gcc_unreachable ();
458 case CPP_KEYWORD:
459 switch (token->keyword)
461 case RID_UNSIGNED:
462 case RID_LONG:
463 case RID_INT128:
464 case RID_SHORT:
465 case RID_SIGNED:
466 case RID_COMPLEX:
467 case RID_INT:
468 case RID_CHAR:
469 case RID_FLOAT:
470 case RID_DOUBLE:
471 case RID_VOID:
472 case RID_DFLOAT32:
473 case RID_DFLOAT64:
474 case RID_DFLOAT128:
475 case RID_BOOL:
476 case RID_ENUM:
477 case RID_STRUCT:
478 case RID_UNION:
479 case RID_TYPEOF:
480 case RID_CONST:
481 case RID_VOLATILE:
482 case RID_RESTRICT:
483 case RID_ATTRIBUTE:
484 case RID_FRACT:
485 case RID_ACCUM:
486 case RID_SAT:
487 return true;
488 default:
489 return false;
491 case CPP_LESS:
492 if (c_dialect_objc ())
493 return true;
494 return false;
495 default:
496 return false;
500 /* Return true if the next token from PARSER can start a type name,
501 false otherwise. */
502 static inline bool
503 c_parser_next_token_starts_typename (c_parser *parser)
505 c_token *token = c_parser_peek_token (parser);
506 return c_token_starts_typename (token);
509 /* Return true if TOKEN can start declaration specifiers, false
510 otherwise. */
511 static bool
512 c_token_starts_declspecs (c_token *token)
514 switch (token->type)
516 case CPP_NAME:
517 switch (token->id_kind)
519 case C_ID_ID:
520 return false;
521 case C_ID_ADDRSPACE:
522 return true;
523 case C_ID_TYPENAME:
524 return true;
525 case C_ID_CLASSNAME:
526 gcc_assert (c_dialect_objc ());
527 return true;
528 default:
529 gcc_unreachable ();
531 case CPP_KEYWORD:
532 switch (token->keyword)
534 case RID_STATIC:
535 case RID_EXTERN:
536 case RID_REGISTER:
537 case RID_TYPEDEF:
538 case RID_INLINE:
539 case RID_AUTO:
540 case RID_THREAD:
541 case RID_UNSIGNED:
542 case RID_LONG:
543 case RID_INT128:
544 case RID_SHORT:
545 case RID_SIGNED:
546 case RID_COMPLEX:
547 case RID_INT:
548 case RID_CHAR:
549 case RID_FLOAT:
550 case RID_DOUBLE:
551 case RID_VOID:
552 case RID_DFLOAT32:
553 case RID_DFLOAT64:
554 case RID_DFLOAT128:
555 case RID_BOOL:
556 case RID_ENUM:
557 case RID_STRUCT:
558 case RID_UNION:
559 case RID_TYPEOF:
560 case RID_CONST:
561 case RID_VOLATILE:
562 case RID_RESTRICT:
563 case RID_ATTRIBUTE:
564 case RID_FRACT:
565 case RID_ACCUM:
566 case RID_SAT:
567 return true;
568 default:
569 return false;
571 case CPP_LESS:
572 if (c_dialect_objc ())
573 return true;
574 return false;
575 default:
576 return false;
581 /* Return true if TOKEN can start declaration specifiers or a static
582 assertion, false otherwise. */
583 static bool
584 c_token_starts_declaration (c_token *token)
586 if (c_token_starts_declspecs (token)
587 || token->keyword == RID_STATIC_ASSERT)
588 return true;
589 else
590 return false;
593 static c_token *c_parser_peek_2nd_token (c_parser *parser);
595 /* Return true if the next token from PARSER can start declaration
596 specifiers, false otherwise. */
597 static inline bool
598 c_parser_next_token_starts_declspecs (c_parser *parser)
600 c_token *token = c_parser_peek_token (parser);
601 return c_token_starts_declspecs (token);
604 /* Return true if the next token from PARSER can start declaration
605 specifiers or a static assertion, false otherwise. */
606 static inline bool
607 c_parser_next_token_starts_declaration (c_parser *parser)
609 c_token *token = c_parser_peek_token (parser);
610 return c_token_starts_declaration (token);
613 /* Return a pointer to the next-but-one token from PARSER, reading it
614 in if necessary. The next token is already read in. */
616 static c_token *
617 c_parser_peek_2nd_token (c_parser *parser)
619 if (parser->tokens_avail >= 2)
620 return &parser->tokens[1];
621 gcc_assert (parser->tokens_avail == 1);
622 gcc_assert (parser->tokens[0].type != CPP_EOF);
623 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
624 c_lex_one_token (parser, &parser->tokens[1]);
625 parser->tokens_avail = 2;
626 return &parser->tokens[1];
629 /* Consume the next token from PARSER. */
631 static void
632 c_parser_consume_token (c_parser *parser)
634 gcc_assert (parser->tokens_avail >= 1);
635 gcc_assert (parser->tokens[0].type != CPP_EOF);
636 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
637 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
638 if (parser->tokens_avail == 2)
639 parser->tokens[0] = parser->tokens[1];
640 parser->tokens_avail--;
643 /* Expect the current token to be a #pragma. Consume it and remember
644 that we've begun parsing a pragma. */
646 static void
647 c_parser_consume_pragma (c_parser *parser)
649 gcc_assert (!parser->in_pragma);
650 gcc_assert (parser->tokens_avail >= 1);
651 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
652 if (parser->tokens_avail == 2)
653 parser->tokens[0] = parser->tokens[1];
654 parser->tokens_avail--;
655 parser->in_pragma = true;
658 /* Update the globals input_location and in_system_header from
659 TOKEN. */
660 static inline void
661 c_parser_set_source_position_from_token (c_token *token)
663 if (token->type != CPP_EOF)
665 input_location = token->location;
669 /* Issue a diagnostic of the form
670 FILE:LINE: MESSAGE before TOKEN
671 where TOKEN is the next token in the input stream of PARSER.
672 MESSAGE (specified by the caller) is usually of the form "expected
673 OTHER-TOKEN".
675 Do not issue a diagnostic if still recovering from an error.
677 ??? This is taken from the C++ parser, but building up messages in
678 this way is not i18n-friendly and some other approach should be
679 used. */
681 static void
682 c_parser_error (c_parser *parser, const char *gmsgid)
684 c_token *token = c_parser_peek_token (parser);
685 if (parser->error)
686 return;
687 parser->error = true;
688 if (!gmsgid)
689 return;
690 /* This diagnostic makes more sense if it is tagged to the line of
691 the token we just peeked at. */
692 c_parser_set_source_position_from_token (token);
693 c_parse_error (gmsgid,
694 /* Because c_parse_error does not understand
695 CPP_KEYWORD, keywords are treated like
696 identifiers. */
697 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
698 /* ??? The C parser does not save the cpp flags of a
699 token, we need to pass 0 here and we will not get
700 the source spelling of some tokens but rather the
701 canonical spelling. */
702 token->value, /*flags=*/0);
705 /* If the next token is of the indicated TYPE, consume it. Otherwise,
706 issue the error MSGID. If MSGID is NULL then a message has already
707 been produced and no message will be produced this time. Returns
708 true if found, false otherwise. */
710 static bool
711 c_parser_require (c_parser *parser,
712 enum cpp_ttype type,
713 const char *msgid)
715 if (c_parser_next_token_is (parser, type))
717 c_parser_consume_token (parser);
718 return true;
720 else
722 c_parser_error (parser, msgid);
723 return false;
727 /* If the next token is the indicated keyword, consume it. Otherwise,
728 issue the error MSGID. Returns true if found, false otherwise. */
730 static bool
731 c_parser_require_keyword (c_parser *parser,
732 enum rid keyword,
733 const char *msgid)
735 if (c_parser_next_token_is_keyword (parser, keyword))
737 c_parser_consume_token (parser);
738 return true;
740 else
742 c_parser_error (parser, msgid);
743 return false;
747 /* Like c_parser_require, except that tokens will be skipped until the
748 desired token is found. An error message is still produced if the
749 next token is not as expected. If MSGID is NULL then a message has
750 already been produced and no message will be produced this
751 time. */
753 static void
754 c_parser_skip_until_found (c_parser *parser,
755 enum cpp_ttype type,
756 const char *msgid)
758 unsigned nesting_depth = 0;
760 if (c_parser_require (parser, type, msgid))
761 return;
763 /* Skip tokens until the desired token is found. */
764 while (true)
766 /* Peek at the next token. */
767 c_token *token = c_parser_peek_token (parser);
768 /* If we've reached the token we want, consume it and stop. */
769 if (token->type == type && !nesting_depth)
771 c_parser_consume_token (parser);
772 break;
775 /* If we've run out of tokens, stop. */
776 if (token->type == CPP_EOF)
777 return;
778 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
779 return;
780 if (token->type == CPP_OPEN_BRACE
781 || token->type == CPP_OPEN_PAREN
782 || token->type == CPP_OPEN_SQUARE)
783 ++nesting_depth;
784 else if (token->type == CPP_CLOSE_BRACE
785 || token->type == CPP_CLOSE_PAREN
786 || token->type == CPP_CLOSE_SQUARE)
788 if (nesting_depth-- == 0)
789 break;
791 /* Consume this token. */
792 c_parser_consume_token (parser);
794 parser->error = false;
797 /* Skip tokens until the end of a parameter is found, but do not
798 consume the comma, semicolon or closing delimiter. */
800 static void
801 c_parser_skip_to_end_of_parameter (c_parser *parser)
803 unsigned nesting_depth = 0;
805 while (true)
807 c_token *token = c_parser_peek_token (parser);
808 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
809 && !nesting_depth)
810 break;
811 /* If we've run out of tokens, stop. */
812 if (token->type == CPP_EOF)
813 return;
814 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
815 return;
816 if (token->type == CPP_OPEN_BRACE
817 || token->type == CPP_OPEN_PAREN
818 || token->type == CPP_OPEN_SQUARE)
819 ++nesting_depth;
820 else if (token->type == CPP_CLOSE_BRACE
821 || token->type == CPP_CLOSE_PAREN
822 || token->type == CPP_CLOSE_SQUARE)
824 if (nesting_depth-- == 0)
825 break;
827 /* Consume this token. */
828 c_parser_consume_token (parser);
830 parser->error = false;
833 /* Expect to be at the end of the pragma directive and consume an
834 end of line marker. */
836 static void
837 c_parser_skip_to_pragma_eol (c_parser *parser)
839 gcc_assert (parser->in_pragma);
840 parser->in_pragma = false;
842 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
843 while (true)
845 c_token *token = c_parser_peek_token (parser);
846 if (token->type == CPP_EOF)
847 break;
848 if (token->type == CPP_PRAGMA_EOL)
850 c_parser_consume_token (parser);
851 break;
853 c_parser_consume_token (parser);
856 parser->error = false;
859 /* Skip tokens until we have consumed an entire block, or until we
860 have consumed a non-nested ';'. */
862 static void
863 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
865 unsigned nesting_depth = 0;
866 bool save_error = parser->error;
868 while (true)
870 c_token *token;
872 /* Peek at the next token. */
873 token = c_parser_peek_token (parser);
875 switch (token->type)
877 case CPP_EOF:
878 return;
880 case CPP_PRAGMA_EOL:
881 if (parser->in_pragma)
882 return;
883 break;
885 case CPP_SEMICOLON:
886 /* If the next token is a ';', we have reached the
887 end of the statement. */
888 if (!nesting_depth)
890 /* Consume the ';'. */
891 c_parser_consume_token (parser);
892 goto finished;
894 break;
896 case CPP_CLOSE_BRACE:
897 /* If the next token is a non-nested '}', then we have
898 reached the end of the current block. */
899 if (nesting_depth == 0 || --nesting_depth == 0)
901 c_parser_consume_token (parser);
902 goto finished;
904 break;
906 case CPP_OPEN_BRACE:
907 /* If it the next token is a '{', then we are entering a new
908 block. Consume the entire block. */
909 ++nesting_depth;
910 break;
912 case CPP_PRAGMA:
913 /* If we see a pragma, consume the whole thing at once. We
914 have some safeguards against consuming pragmas willy-nilly.
915 Normally, we'd expect to be here with parser->error set,
916 which disables these safeguards. But it's possible to get
917 here for secondary error recovery, after parser->error has
918 been cleared. */
919 c_parser_consume_pragma (parser);
920 c_parser_skip_to_pragma_eol (parser);
921 parser->error = save_error;
922 continue;
924 default:
925 break;
928 c_parser_consume_token (parser);
931 finished:
932 parser->error = false;
935 /* CPP's options (initialized by c-opts.c). */
936 extern cpp_options *cpp_opts;
938 /* Save the warning flags which are controlled by __extension__. */
940 static inline int
941 disable_extension_diagnostics (void)
943 int ret = (pedantic
944 | (warn_pointer_arith << 1)
945 | (warn_traditional << 2)
946 | (flag_iso << 3)
947 | (warn_long_long << 4)
948 | (warn_cxx_compat << 5));
949 cpp_opts->cpp_pedantic = pedantic = 0;
950 warn_pointer_arith = 0;
951 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
952 flag_iso = 0;
953 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
954 warn_cxx_compat = 0;
955 return ret;
958 /* Restore the warning flags which are controlled by __extension__.
959 FLAGS is the return value from disable_extension_diagnostics. */
961 static inline void
962 restore_extension_diagnostics (int flags)
964 cpp_opts->cpp_pedantic = pedantic = flags & 1;
965 warn_pointer_arith = (flags >> 1) & 1;
966 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
967 flag_iso = (flags >> 3) & 1;
968 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
969 warn_cxx_compat = (flags >> 5) & 1;
972 /* Possibly kinds of declarator to parse. */
973 typedef enum c_dtr_syn {
974 /* A normal declarator with an identifier. */
975 C_DTR_NORMAL,
976 /* An abstract declarator (maybe empty). */
977 C_DTR_ABSTRACT,
978 /* A parameter declarator: may be either, but after a type name does
979 not redeclare a typedef name as an identifier if it can
980 alternatively be interpreted as a typedef name; see DR#009,
981 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
982 following DR#249. For example, given a typedef T, "int T" and
983 "int *T" are valid parameter declarations redeclaring T, while
984 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
985 abstract declarators rather than involving redundant parentheses;
986 the same applies with attributes inside the parentheses before
987 "T". */
988 C_DTR_PARM
989 } c_dtr_syn;
991 static void c_parser_external_declaration (c_parser *);
992 static void c_parser_asm_definition (c_parser *);
993 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
994 bool, bool, tree *);
995 static void c_parser_static_assert_declaration_no_semi (c_parser *);
996 static void c_parser_static_assert_declaration (c_parser *);
997 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
998 bool);
999 static struct c_typespec c_parser_enum_specifier (c_parser *);
1000 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1001 static tree c_parser_struct_declaration (c_parser *);
1002 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1003 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1004 bool *);
1005 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1006 c_dtr_syn, bool *);
1007 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1008 bool,
1009 struct c_declarator *);
1010 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1011 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
1012 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1013 static tree c_parser_simple_asm_expr (c_parser *);
1014 static tree c_parser_attributes (c_parser *);
1015 static struct c_type_name *c_parser_type_name (c_parser *);
1016 static struct c_expr c_parser_initializer (c_parser *);
1017 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1018 static void c_parser_initelt (c_parser *, struct obstack *);
1019 static void c_parser_initval (c_parser *, struct c_expr *,
1020 struct obstack *);
1021 static tree c_parser_compound_statement (c_parser *);
1022 static void c_parser_compound_statement_nostart (c_parser *);
1023 static void c_parser_label (c_parser *);
1024 static void c_parser_statement (c_parser *);
1025 static void c_parser_statement_after_labels (c_parser *);
1026 static void c_parser_if_statement (c_parser *);
1027 static void c_parser_switch_statement (c_parser *);
1028 static void c_parser_while_statement (c_parser *);
1029 static void c_parser_do_statement (c_parser *);
1030 static void c_parser_for_statement (c_parser *);
1031 static tree c_parser_asm_statement (c_parser *);
1032 static tree c_parser_asm_operands (c_parser *, bool);
1033 static tree c_parser_asm_goto_operands (c_parser *);
1034 static tree c_parser_asm_clobbers (c_parser *);
1035 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1036 static struct c_expr c_parser_conditional_expression (c_parser *,
1037 struct c_expr *);
1038 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1039 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1040 static struct c_expr c_parser_unary_expression (c_parser *);
1041 static struct c_expr c_parser_sizeof_expression (c_parser *);
1042 static struct c_expr c_parser_alignof_expression (c_parser *);
1043 static struct c_expr c_parser_postfix_expression (c_parser *);
1044 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1045 struct c_type_name *,
1046 location_t);
1047 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1048 location_t loc,
1049 struct c_expr);
1050 static struct c_expr c_parser_expression (c_parser *);
1051 static struct c_expr c_parser_expression_conv (c_parser *);
1052 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1053 VEC(tree,gc) **);
1054 static void c_parser_omp_construct (c_parser *);
1055 static void c_parser_omp_threadprivate (c_parser *);
1056 static void c_parser_omp_barrier (c_parser *);
1057 static void c_parser_omp_flush (c_parser *);
1058 static void c_parser_omp_taskwait (c_parser *);
1060 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1061 static bool c_parser_pragma (c_parser *, enum pragma_context);
1063 /* These Objective-C parser functions are only ever called when
1064 compiling Objective-C. */
1065 static void c_parser_objc_class_definition (c_parser *, tree);
1066 static void c_parser_objc_class_instance_variables (c_parser *);
1067 static void c_parser_objc_class_declaration (c_parser *);
1068 static void c_parser_objc_alias_declaration (c_parser *);
1069 static void c_parser_objc_protocol_definition (c_parser *, tree);
1070 static bool c_parser_objc_method_type (c_parser *);
1071 static void c_parser_objc_method_definition (c_parser *);
1072 static void c_parser_objc_methodprotolist (c_parser *);
1073 static void c_parser_objc_methodproto (c_parser *);
1074 static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
1075 static tree c_parser_objc_type_name (c_parser *);
1076 static tree c_parser_objc_protocol_refs (c_parser *);
1077 static void c_parser_objc_try_catch_statement (c_parser *);
1078 static void c_parser_objc_synchronized_statement (c_parser *);
1079 static tree c_parser_objc_selector (c_parser *);
1080 static tree c_parser_objc_selector_arg (c_parser *);
1081 static tree c_parser_objc_receiver (c_parser *);
1082 static tree c_parser_objc_message_args (c_parser *);
1083 static tree c_parser_objc_keywordexpr (c_parser *);
1084 static void c_parser_objc_at_property_declaration (c_parser *);
1085 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1086 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1087 static bool c_parser_objc_diagnose_bad_element_prefix
1088 (c_parser *, struct c_declspecs *);
1090 /* Parse a translation unit (C90 6.7, C99 6.9).
1092 translation-unit:
1093 external-declarations
1095 external-declarations:
1096 external-declaration
1097 external-declarations external-declaration
1099 GNU extensions:
1101 translation-unit:
1102 empty
1105 static void
1106 c_parser_translation_unit (c_parser *parser)
1108 if (c_parser_next_token_is (parser, CPP_EOF))
1110 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1111 "ISO C forbids an empty translation unit");
1113 else
1115 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1116 mark_valid_location_for_stdc_pragma (false);
1119 ggc_collect ();
1120 c_parser_external_declaration (parser);
1121 obstack_free (&parser_obstack, obstack_position);
1123 while (c_parser_next_token_is_not (parser, CPP_EOF));
1127 /* Parse an external declaration (C90 6.7, C99 6.9).
1129 external-declaration:
1130 function-definition
1131 declaration
1133 GNU extensions:
1135 external-declaration:
1136 asm-definition
1138 __extension__ external-declaration
1140 Objective-C:
1142 external-declaration:
1143 objc-class-definition
1144 objc-class-declaration
1145 objc-alias-declaration
1146 objc-protocol-definition
1147 objc-method-definition
1148 @end
1151 static void
1152 c_parser_external_declaration (c_parser *parser)
1154 int ext;
1155 switch (c_parser_peek_token (parser)->type)
1157 case CPP_KEYWORD:
1158 switch (c_parser_peek_token (parser)->keyword)
1160 case RID_EXTENSION:
1161 ext = disable_extension_diagnostics ();
1162 c_parser_consume_token (parser);
1163 c_parser_external_declaration (parser);
1164 restore_extension_diagnostics (ext);
1165 break;
1166 case RID_ASM:
1167 c_parser_asm_definition (parser);
1168 break;
1169 case RID_AT_INTERFACE:
1170 case RID_AT_IMPLEMENTATION:
1171 gcc_assert (c_dialect_objc ());
1172 c_parser_objc_class_definition (parser, NULL_TREE);
1173 break;
1174 case RID_AT_CLASS:
1175 gcc_assert (c_dialect_objc ());
1176 c_parser_objc_class_declaration (parser);
1177 break;
1178 case RID_AT_ALIAS:
1179 gcc_assert (c_dialect_objc ());
1180 c_parser_objc_alias_declaration (parser);
1181 break;
1182 case RID_AT_PROTOCOL:
1183 gcc_assert (c_dialect_objc ());
1184 c_parser_objc_protocol_definition (parser, NULL_TREE);
1185 break;
1186 case RID_AT_PROPERTY:
1187 gcc_assert (c_dialect_objc ());
1188 c_parser_objc_at_property_declaration (parser);
1189 break;
1190 case RID_AT_SYNTHESIZE:
1191 gcc_assert (c_dialect_objc ());
1192 c_parser_objc_at_synthesize_declaration (parser);
1193 break;
1194 case RID_AT_DYNAMIC:
1195 gcc_assert (c_dialect_objc ());
1196 c_parser_objc_at_dynamic_declaration (parser);
1197 break;
1198 case RID_AT_END:
1199 gcc_assert (c_dialect_objc ());
1200 c_parser_consume_token (parser);
1201 objc_finish_implementation ();
1202 break;
1203 default:
1204 goto decl_or_fndef;
1206 break;
1207 case CPP_SEMICOLON:
1208 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1209 "ISO C does not allow extra %<;%> outside of a function");
1210 c_parser_consume_token (parser);
1211 break;
1212 case CPP_PRAGMA:
1213 mark_valid_location_for_stdc_pragma (true);
1214 c_parser_pragma (parser, pragma_external);
1215 mark_valid_location_for_stdc_pragma (false);
1216 break;
1217 case CPP_PLUS:
1218 case CPP_MINUS:
1219 if (c_dialect_objc ())
1221 c_parser_objc_method_definition (parser);
1222 break;
1224 /* Else fall through, and yield a syntax error trying to parse
1225 as a declaration or function definition. */
1226 default:
1227 decl_or_fndef:
1228 /* A declaration or a function definition (or, in Objective-C,
1229 an @interface or @protocol with prefix attributes). We can
1230 only tell which after parsing the declaration specifiers, if
1231 any, and the first declarator. */
1232 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1233 break;
1237 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1238 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1239 accepted; otherwise (old-style parameter declarations) only other
1240 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1241 assertion is accepted; otherwise (old-style parameter declarations)
1242 it is not. If NESTED is true, we are inside a function or parsing
1243 old-style parameter declarations; any functions encountered are
1244 nested functions and declaration specifiers are required; otherwise
1245 we are at top level and functions are normal functions and
1246 declaration specifiers may be optional. If EMPTY_OK is true, empty
1247 declarations are OK (subject to all other constraints); otherwise
1248 (old-style parameter declarations) they are diagnosed. If
1249 START_ATTR_OK is true, the declaration specifiers may start with
1250 attributes; otherwise they may not.
1251 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1252 declaration when parsing an Objective-C foreach statement.
1254 declaration:
1255 declaration-specifiers init-declarator-list[opt] ;
1256 static_assert-declaration
1258 function-definition:
1259 declaration-specifiers[opt] declarator declaration-list[opt]
1260 compound-statement
1262 declaration-list:
1263 declaration
1264 declaration-list declaration
1266 init-declarator-list:
1267 init-declarator
1268 init-declarator-list , init-declarator
1270 init-declarator:
1271 declarator simple-asm-expr[opt] attributes[opt]
1272 declarator simple-asm-expr[opt] attributes[opt] = initializer
1274 GNU extensions:
1276 nested-function-definition:
1277 declaration-specifiers declarator declaration-list[opt]
1278 compound-statement
1280 Objective-C:
1281 attributes objc-class-definition
1282 attributes objc-category-definition
1283 attributes objc-protocol-definition
1285 The simple-asm-expr and attributes are GNU extensions.
1287 This function does not handle __extension__; that is handled in its
1288 callers. ??? Following the old parser, __extension__ may start
1289 external declarations, declarations in functions and declarations
1290 at the start of "for" loops, but not old-style parameter
1291 declarations.
1293 C99 requires declaration specifiers in a function definition; the
1294 absence is diagnosed through the diagnosis of implicit int. In GNU
1295 C we also allow but diagnose declarations without declaration
1296 specifiers, but only at top level (elsewhere they conflict with
1297 other syntax).
1299 In Objective-C, declarations of the looping variable in a foreach
1300 statement are exceptionally terminated by 'in' (for example, 'for
1301 (NSObject *object in array) { ... }').
1303 OpenMP:
1305 declaration:
1306 threadprivate-directive */
1308 static void
1309 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1310 bool static_assert_ok, bool empty_ok,
1311 bool nested, bool start_attr_ok,
1312 tree *objc_foreach_object_declaration)
1314 struct c_declspecs *specs;
1315 tree prefix_attrs;
1316 tree all_prefix_attrs;
1317 bool diagnosed_no_specs = false;
1318 location_t here = c_parser_peek_token (parser)->location;
1320 if (static_assert_ok
1321 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1323 c_parser_static_assert_declaration (parser);
1324 return;
1326 specs = build_null_declspecs ();
1327 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1328 if (parser->error)
1330 c_parser_skip_to_end_of_block_or_statement (parser);
1331 return;
1333 if (nested && !specs->declspecs_seen_p)
1335 c_parser_error (parser, "expected declaration specifiers");
1336 c_parser_skip_to_end_of_block_or_statement (parser);
1337 return;
1339 finish_declspecs (specs);
1340 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1342 if (empty_ok)
1343 shadow_tag (specs);
1344 else
1346 shadow_tag_warned (specs, 1);
1347 pedwarn (here, 0, "empty declaration");
1349 c_parser_consume_token (parser);
1350 return;
1352 else if (c_dialect_objc ())
1354 /* Prefix attributes are an error on method decls. */
1355 switch (c_parser_peek_token (parser)->type)
1357 case CPP_PLUS:
1358 case CPP_MINUS:
1359 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1360 return;
1361 if (specs->attrs)
1363 warning_at (c_parser_peek_token (parser)->location,
1364 OPT_Wattributes,
1365 "prefix attributes are ignored for methods");
1366 specs->attrs = NULL_TREE;
1368 if (fndef_ok)
1369 c_parser_objc_method_definition (parser);
1370 else
1371 c_parser_objc_methodproto (parser);
1372 return;
1373 break;
1374 default:
1375 break;
1377 /* This is where we parse 'attributes @interface ...',
1378 'attributes @implementation ...', 'attributes @protocol ...'
1379 (where attributes could be, for example, __attribute__
1380 ((deprecated)).
1382 switch (c_parser_peek_token (parser)->keyword)
1384 case RID_AT_INTERFACE:
1386 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1387 return;
1388 c_parser_objc_class_definition (parser, specs->attrs);
1389 return;
1391 break;
1392 case RID_AT_IMPLEMENTATION:
1394 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1395 return;
1396 if (specs->attrs)
1398 warning_at (c_parser_peek_token (parser)->location,
1399 OPT_Wattributes,
1400 "prefix attributes are ignored for implementations");
1401 specs->attrs = NULL_TREE;
1403 c_parser_objc_class_definition (parser, NULL_TREE);
1404 return;
1406 break;
1407 case RID_AT_PROTOCOL:
1409 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1410 return;
1411 c_parser_objc_protocol_definition (parser, specs->attrs);
1412 return;
1414 break;
1415 case RID_AT_ALIAS:
1416 case RID_AT_CLASS:
1417 case RID_AT_END:
1418 case RID_AT_PROPERTY:
1419 if (specs->attrs)
1421 c_parser_error (parser,
1422 "attributes may not be specified before" );
1423 specs->attrs = NULL;
1425 break;
1426 default:
1427 break;
1431 pending_xref_error ();
1432 prefix_attrs = specs->attrs;
1433 all_prefix_attrs = prefix_attrs;
1434 specs->attrs = NULL_TREE;
1435 while (true)
1437 struct c_declarator *declarator;
1438 bool dummy = false;
1439 tree fnbody;
1440 /* Declaring either one or more declarators (in which case we
1441 should diagnose if there were no declaration specifiers) or a
1442 function definition (in which case the diagnostic for
1443 implicit int suffices). */
1444 declarator = c_parser_declarator (parser, specs->type_seen_p,
1445 C_DTR_NORMAL, &dummy);
1446 if (declarator == NULL)
1448 c_parser_skip_to_end_of_block_or_statement (parser);
1449 return;
1451 if (c_parser_next_token_is (parser, CPP_EQ)
1452 || c_parser_next_token_is (parser, CPP_COMMA)
1453 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1454 || c_parser_next_token_is_keyword (parser, RID_ASM)
1455 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1456 || c_parser_next_token_is_keyword (parser, RID_IN))
1458 tree asm_name = NULL_TREE;
1459 tree postfix_attrs = NULL_TREE;
1460 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1462 diagnosed_no_specs = true;
1463 pedwarn (here, 0, "data definition has no type or storage class");
1465 /* Having seen a data definition, there cannot now be a
1466 function definition. */
1467 fndef_ok = false;
1468 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1469 asm_name = c_parser_simple_asm_expr (parser);
1470 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1471 postfix_attrs = c_parser_attributes (parser);
1472 if (c_parser_next_token_is (parser, CPP_EQ))
1474 tree d;
1475 struct c_expr init;
1476 location_t init_loc;
1477 c_parser_consume_token (parser);
1478 /* The declaration of the variable is in effect while
1479 its initializer is parsed. */
1480 d = start_decl (declarator, specs, true,
1481 chainon (postfix_attrs, all_prefix_attrs));
1482 if (!d)
1483 d = error_mark_node;
1484 start_init (d, asm_name, global_bindings_p ());
1485 init_loc = c_parser_peek_token (parser)->location;
1486 init = c_parser_initializer (parser);
1487 finish_init ();
1488 if (d != error_mark_node)
1490 maybe_warn_string_init (TREE_TYPE (d), init);
1491 finish_decl (d, init_loc, init.value,
1492 init.original_type, asm_name);
1495 else
1497 tree d = start_decl (declarator, specs, false,
1498 chainon (postfix_attrs,
1499 all_prefix_attrs));
1500 if (d)
1501 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1502 NULL_TREE, asm_name);
1504 if (c_parser_next_token_is_keyword (parser, RID_IN))
1506 if (d)
1507 *objc_foreach_object_declaration = d;
1508 else
1509 *objc_foreach_object_declaration = error_mark_node;
1512 if (c_parser_next_token_is (parser, CPP_COMMA))
1514 c_parser_consume_token (parser);
1515 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1516 all_prefix_attrs = chainon (c_parser_attributes (parser),
1517 prefix_attrs);
1518 else
1519 all_prefix_attrs = prefix_attrs;
1520 continue;
1522 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1524 c_parser_consume_token (parser);
1525 return;
1527 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1529 /* This can only happen in Objective-C: we found the
1530 'in' that terminates the declaration inside an
1531 Objective-C foreach statement. Do not consume the
1532 token, so that the caller can use it to determine
1533 that this indeed is a foreach context. */
1534 return;
1536 else
1538 c_parser_error (parser, "expected %<,%> or %<;%>");
1539 c_parser_skip_to_end_of_block_or_statement (parser);
1540 return;
1543 else if (!fndef_ok)
1545 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1546 "%<asm%> or %<__attribute__%>");
1547 c_parser_skip_to_end_of_block_or_statement (parser);
1548 return;
1550 /* Function definition (nested or otherwise). */
1551 if (nested)
1553 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1554 c_push_function_context ();
1556 if (!start_function (specs, declarator, all_prefix_attrs))
1558 /* This can appear in many cases looking nothing like a
1559 function definition, so we don't give a more specific
1560 error suggesting there was one. */
1561 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1562 "or %<__attribute__%>");
1563 if (nested)
1564 c_pop_function_context ();
1565 break;
1567 /* Parse old-style parameter declarations. ??? Attributes are
1568 not allowed to start declaration specifiers here because of a
1569 syntax conflict between a function declaration with attribute
1570 suffix and a function definition with an attribute prefix on
1571 first old-style parameter declaration. Following the old
1572 parser, they are not accepted on subsequent old-style
1573 parameter declarations either. However, there is no
1574 ambiguity after the first declaration, nor indeed on the
1575 first as long as we don't allow postfix attributes after a
1576 declarator with a nonempty identifier list in a definition;
1577 and postfix attributes have never been accepted here in
1578 function definitions either. */
1579 while (c_parser_next_token_is_not (parser, CPP_EOF)
1580 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1581 c_parser_declaration_or_fndef (parser, false, false, false,
1582 true, false, NULL);
1583 store_parm_decls ();
1584 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1585 = c_parser_peek_token (parser)->location;
1586 fnbody = c_parser_compound_statement (parser);
1587 if (nested)
1589 tree decl = current_function_decl;
1590 /* Mark nested functions as needing static-chain initially.
1591 lower_nested_functions will recompute it but the
1592 DECL_STATIC_CHAIN flag is also used before that happens,
1593 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1594 DECL_STATIC_CHAIN (decl) = 1;
1595 add_stmt (fnbody);
1596 finish_function ();
1597 c_pop_function_context ();
1598 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1600 else
1602 add_stmt (fnbody);
1603 finish_function ();
1605 break;
1609 /* Parse an asm-definition (asm() outside a function body). This is a
1610 GNU extension.
1612 asm-definition:
1613 simple-asm-expr ;
1616 static void
1617 c_parser_asm_definition (c_parser *parser)
1619 tree asm_str = c_parser_simple_asm_expr (parser);
1620 if (asm_str)
1621 cgraph_add_asm_node (asm_str);
1622 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1625 /* Parse a static assertion (C1X N1425 6.7.10).
1627 static_assert-declaration:
1628 static_assert-declaration-no-semi ;
1631 static void
1632 c_parser_static_assert_declaration (c_parser *parser)
1634 c_parser_static_assert_declaration_no_semi (parser);
1635 if (parser->error
1636 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1637 c_parser_skip_to_end_of_block_or_statement (parser);
1640 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1641 semicolon.
1643 static_assert-declaration-no-semi:
1644 _Static_assert ( constant-expression , string-literal )
1647 static void
1648 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1650 location_t assert_loc, value_loc;
1651 tree value;
1652 tree string;
1654 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1655 assert_loc = c_parser_peek_token (parser)->location;
1656 if (!flag_isoc1x)
1658 if (flag_isoc99)
1659 pedwarn (assert_loc, OPT_pedantic,
1660 "ISO C99 does not support %<_Static_assert%>");
1661 else
1662 pedwarn (assert_loc, OPT_pedantic,
1663 "ISO C90 does not support %<_Static_assert%>");
1665 c_parser_consume_token (parser);
1666 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1667 return;
1668 value_loc = c_parser_peek_token (parser)->location;
1669 value = c_parser_expr_no_commas (parser, NULL).value;
1670 parser->lex_untranslated_string = true;
1671 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1673 parser->lex_untranslated_string = false;
1674 return;
1676 switch (c_parser_peek_token (parser)->type)
1678 case CPP_STRING:
1679 case CPP_STRING16:
1680 case CPP_STRING32:
1681 case CPP_WSTRING:
1682 case CPP_UTF8STRING:
1683 string = c_parser_peek_token (parser)->value;
1684 c_parser_consume_token (parser);
1685 parser->lex_untranslated_string = false;
1686 break;
1687 default:
1688 c_parser_error (parser, "expected string literal");
1689 parser->lex_untranslated_string = false;
1690 return;
1692 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1694 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1696 error_at (value_loc, "expression in static assertion is not an integer");
1697 return;
1699 if (TREE_CODE (value) != INTEGER_CST)
1701 value = c_fully_fold (value, false, NULL);
1702 if (TREE_CODE (value) == INTEGER_CST)
1703 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1704 "is not an integer constant expression");
1706 if (TREE_CODE (value) != INTEGER_CST)
1708 error_at (value_loc, "expression in static assertion is not constant");
1709 return;
1711 constant_expression_warning (value);
1712 if (integer_zerop (value))
1713 error_at (assert_loc, "static assertion failed: %E", string);
1716 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1717 6.7), adding them to SPECS (which may already include some).
1718 Storage class specifiers are accepted iff SCSPEC_OK; type
1719 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1720 the start iff START_ATTR_OK.
1722 declaration-specifiers:
1723 storage-class-specifier declaration-specifiers[opt]
1724 type-specifier declaration-specifiers[opt]
1725 type-qualifier declaration-specifiers[opt]
1726 function-specifier declaration-specifiers[opt]
1728 Function specifiers (inline) are from C99, and are currently
1729 handled as storage class specifiers, as is __thread.
1731 C90 6.5.1, C99 6.7.1:
1732 storage-class-specifier:
1733 typedef
1734 extern
1735 static
1736 auto
1737 register
1739 C99 6.7.4:
1740 function-specifier:
1741 inline
1743 C90 6.5.2, C99 6.7.2:
1744 type-specifier:
1745 void
1746 char
1747 short
1749 long
1750 float
1751 double
1752 signed
1753 unsigned
1754 _Bool
1755 _Complex
1756 [_Imaginary removed in C99 TC2]
1757 struct-or-union-specifier
1758 enum-specifier
1759 typedef-name
1761 (_Bool and _Complex are new in C99.)
1763 C90 6.5.3, C99 6.7.3:
1765 type-qualifier:
1766 const
1767 restrict
1768 volatile
1769 address-space-qualifier
1771 (restrict is new in C99.)
1773 GNU extensions:
1775 declaration-specifiers:
1776 attributes declaration-specifiers[opt]
1778 type-qualifier:
1779 address-space
1781 address-space:
1782 identifier recognized by the target
1784 storage-class-specifier:
1785 __thread
1787 type-specifier:
1788 typeof-specifier
1789 __int128
1790 _Decimal32
1791 _Decimal64
1792 _Decimal128
1793 _Fract
1794 _Accum
1795 _Sat
1797 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1798 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1800 Objective-C:
1802 type-specifier:
1803 class-name objc-protocol-refs[opt]
1804 typedef-name objc-protocol-refs
1805 objc-protocol-refs
1808 static void
1809 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1810 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1812 bool attrs_ok = start_attr_ok;
1813 bool seen_type = specs->type_seen_p;
1814 while (c_parser_next_token_is (parser, CPP_NAME)
1815 || c_parser_next_token_is (parser, CPP_KEYWORD)
1816 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1818 struct c_typespec t;
1819 tree attrs;
1820 location_t loc = c_parser_peek_token (parser)->location;
1821 if (c_parser_next_token_is (parser, CPP_NAME))
1823 tree value = c_parser_peek_token (parser)->value;
1824 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1826 if (kind == C_ID_ADDRSPACE)
1828 addr_space_t as
1829 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1830 declspecs_add_addrspace (specs, as);
1831 c_parser_consume_token (parser);
1832 attrs_ok = true;
1833 continue;
1836 /* This finishes the specifiers unless a type name is OK, it
1837 is declared as a type name and a type name hasn't yet
1838 been seen. */
1839 if (!typespec_ok || seen_type
1840 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1841 break;
1842 c_parser_consume_token (parser);
1843 seen_type = true;
1844 attrs_ok = true;
1845 if (kind == C_ID_TYPENAME
1846 && (!c_dialect_objc ()
1847 || c_parser_next_token_is_not (parser, CPP_LESS)))
1849 t.kind = ctsk_typedef;
1850 /* For a typedef name, record the meaning, not the name.
1851 In case of 'foo foo, bar;'. */
1852 t.spec = lookup_name (value);
1853 t.expr = NULL_TREE;
1854 t.expr_const_operands = true;
1856 else
1858 tree proto = NULL_TREE;
1859 gcc_assert (c_dialect_objc ());
1860 t.kind = ctsk_objc;
1861 if (c_parser_next_token_is (parser, CPP_LESS))
1862 proto = c_parser_objc_protocol_refs (parser);
1863 t.spec = objc_get_protocol_qualified_type (value, proto);
1864 t.expr = NULL_TREE;
1865 t.expr_const_operands = true;
1867 declspecs_add_type (loc, specs, t);
1868 continue;
1870 if (c_parser_next_token_is (parser, CPP_LESS))
1872 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1873 nisse@lysator.liu.se. */
1874 tree proto;
1875 gcc_assert (c_dialect_objc ());
1876 if (!typespec_ok || seen_type)
1877 break;
1878 proto = c_parser_objc_protocol_refs (parser);
1879 t.kind = ctsk_objc;
1880 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1881 t.expr = NULL_TREE;
1882 t.expr_const_operands = true;
1883 declspecs_add_type (loc, specs, t);
1884 continue;
1886 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1887 switch (c_parser_peek_token (parser)->keyword)
1889 case RID_STATIC:
1890 case RID_EXTERN:
1891 case RID_REGISTER:
1892 case RID_TYPEDEF:
1893 case RID_INLINE:
1894 case RID_AUTO:
1895 case RID_THREAD:
1896 if (!scspec_ok)
1897 goto out;
1898 attrs_ok = true;
1899 /* TODO: Distinguish between function specifiers (inline)
1900 and storage class specifiers, either here or in
1901 declspecs_add_scspec. */
1902 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1903 c_parser_consume_token (parser);
1904 break;
1905 case RID_UNSIGNED:
1906 case RID_LONG:
1907 case RID_INT128:
1908 case RID_SHORT:
1909 case RID_SIGNED:
1910 case RID_COMPLEX:
1911 case RID_INT:
1912 case RID_CHAR:
1913 case RID_FLOAT:
1914 case RID_DOUBLE:
1915 case RID_VOID:
1916 case RID_DFLOAT32:
1917 case RID_DFLOAT64:
1918 case RID_DFLOAT128:
1919 case RID_BOOL:
1920 case RID_FRACT:
1921 case RID_ACCUM:
1922 case RID_SAT:
1923 if (!typespec_ok)
1924 goto out;
1925 attrs_ok = true;
1926 seen_type = true;
1927 if (c_dialect_objc ())
1928 parser->objc_need_raw_identifier = true;
1929 t.kind = ctsk_resword;
1930 t.spec = c_parser_peek_token (parser)->value;
1931 t.expr = NULL_TREE;
1932 t.expr_const_operands = true;
1933 declspecs_add_type (loc, specs, t);
1934 c_parser_consume_token (parser);
1935 break;
1936 case RID_ENUM:
1937 if (!typespec_ok)
1938 goto out;
1939 attrs_ok = true;
1940 seen_type = true;
1941 t = c_parser_enum_specifier (parser);
1942 declspecs_add_type (loc, specs, t);
1943 break;
1944 case RID_STRUCT:
1945 case RID_UNION:
1946 if (!typespec_ok)
1947 goto out;
1948 attrs_ok = true;
1949 seen_type = true;
1950 t = c_parser_struct_or_union_specifier (parser);
1951 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1952 declspecs_add_type (loc, specs, t);
1953 break;
1954 case RID_TYPEOF:
1955 /* ??? The old parser rejected typeof after other type
1956 specifiers, but is a syntax error the best way of
1957 handling this? */
1958 if (!typespec_ok || seen_type)
1959 goto out;
1960 attrs_ok = true;
1961 seen_type = true;
1962 t = c_parser_typeof_specifier (parser);
1963 declspecs_add_type (loc, specs, t);
1964 break;
1965 case RID_CONST:
1966 case RID_VOLATILE:
1967 case RID_RESTRICT:
1968 attrs_ok = true;
1969 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1970 c_parser_consume_token (parser);
1971 break;
1972 case RID_ATTRIBUTE:
1973 if (!attrs_ok)
1974 goto out;
1975 attrs = c_parser_attributes (parser);
1976 declspecs_add_attrs (specs, attrs);
1977 break;
1978 default:
1979 goto out;
1982 out: ;
1985 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1987 enum-specifier:
1988 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1989 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1990 enum attributes[opt] identifier
1992 The form with trailing comma is new in C99. The forms with
1993 attributes are GNU extensions. In GNU C, we accept any expression
1994 without commas in the syntax (assignment expressions, not just
1995 conditional expressions); assignment expressions will be diagnosed
1996 as non-constant.
1998 enumerator-list:
1999 enumerator
2000 enumerator-list , enumerator
2002 enumerator:
2003 enumeration-constant
2004 enumeration-constant = constant-expression
2007 static struct c_typespec
2008 c_parser_enum_specifier (c_parser *parser)
2010 struct c_typespec ret;
2011 tree attrs;
2012 tree ident = NULL_TREE;
2013 location_t enum_loc;
2014 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2015 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2016 enum_loc = c_parser_peek_token (parser)->location;
2017 c_parser_consume_token (parser);
2018 attrs = c_parser_attributes (parser);
2019 enum_loc = c_parser_peek_token (parser)->location;
2020 /* Set the location in case we create a decl now. */
2021 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2022 if (c_parser_next_token_is (parser, CPP_NAME))
2024 ident = c_parser_peek_token (parser)->value;
2025 ident_loc = c_parser_peek_token (parser)->location;
2026 enum_loc = ident_loc;
2027 c_parser_consume_token (parser);
2029 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2031 /* Parse an enum definition. */
2032 struct c_enum_contents the_enum;
2033 tree type = start_enum (enum_loc, &the_enum, ident);
2034 tree postfix_attrs;
2035 /* We chain the enumerators in reverse order, then put them in
2036 forward order at the end. */
2037 tree values = NULL_TREE;
2038 c_parser_consume_token (parser);
2039 while (true)
2041 tree enum_id;
2042 tree enum_value;
2043 tree enum_decl;
2044 bool seen_comma;
2045 c_token *token;
2046 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2047 location_t decl_loc, value_loc;
2048 if (c_parser_next_token_is_not (parser, CPP_NAME))
2050 c_parser_error (parser, "expected identifier");
2051 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2052 values = error_mark_node;
2053 break;
2055 token = c_parser_peek_token (parser);
2056 enum_id = token->value;
2057 /* Set the location in case we create a decl now. */
2058 c_parser_set_source_position_from_token (token);
2059 decl_loc = value_loc = token->location;
2060 c_parser_consume_token (parser);
2061 if (c_parser_next_token_is (parser, CPP_EQ))
2063 c_parser_consume_token (parser);
2064 value_loc = c_parser_peek_token (parser)->location;
2065 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2067 else
2068 enum_value = NULL_TREE;
2069 enum_decl = build_enumerator (decl_loc, value_loc,
2070 &the_enum, enum_id, enum_value);
2071 TREE_CHAIN (enum_decl) = values;
2072 values = enum_decl;
2073 seen_comma = false;
2074 if (c_parser_next_token_is (parser, CPP_COMMA))
2076 comma_loc = c_parser_peek_token (parser)->location;
2077 seen_comma = true;
2078 c_parser_consume_token (parser);
2080 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2082 if (seen_comma && !flag_isoc99)
2083 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2084 c_parser_consume_token (parser);
2085 break;
2087 if (!seen_comma)
2089 c_parser_error (parser, "expected %<,%> or %<}%>");
2090 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2091 values = error_mark_node;
2092 break;
2095 postfix_attrs = c_parser_attributes (parser);
2096 ret.spec = finish_enum (type, nreverse (values),
2097 chainon (attrs, postfix_attrs));
2098 ret.kind = ctsk_tagdef;
2099 ret.expr = NULL_TREE;
2100 ret.expr_const_operands = true;
2101 return ret;
2103 else if (!ident)
2105 c_parser_error (parser, "expected %<{%>");
2106 ret.spec = error_mark_node;
2107 ret.kind = ctsk_tagref;
2108 ret.expr = NULL_TREE;
2109 ret.expr_const_operands = true;
2110 return ret;
2112 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2113 /* In ISO C, enumerated types can be referred to only if already
2114 defined. */
2115 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2117 gcc_assert (ident);
2118 pedwarn (enum_loc, OPT_pedantic,
2119 "ISO C forbids forward references to %<enum%> types");
2121 return ret;
2124 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2126 struct-or-union-specifier:
2127 struct-or-union attributes[opt] identifier[opt]
2128 { struct-contents } attributes[opt]
2129 struct-or-union attributes[opt] identifier
2131 struct-contents:
2132 struct-declaration-list
2134 struct-declaration-list:
2135 struct-declaration ;
2136 struct-declaration-list struct-declaration ;
2138 GNU extensions:
2140 struct-contents:
2141 empty
2142 struct-declaration
2143 struct-declaration-list struct-declaration
2145 struct-declaration-list:
2146 struct-declaration-list ;
2149 (Note that in the syntax here, unlike that in ISO C, the semicolons
2150 are included here rather than in struct-declaration, in order to
2151 describe the syntax with extra semicolons and missing semicolon at
2152 end.)
2154 Objective-C:
2156 struct-declaration-list:
2157 @defs ( class-name )
2159 (Note this does not include a trailing semicolon, but can be
2160 followed by further declarations, and gets a pedwarn-if-pedantic
2161 when followed by a semicolon.) */
2163 static struct c_typespec
2164 c_parser_struct_or_union_specifier (c_parser *parser)
2166 struct c_typespec ret;
2167 tree attrs;
2168 tree ident = NULL_TREE;
2169 location_t struct_loc;
2170 location_t ident_loc = UNKNOWN_LOCATION;
2171 enum tree_code code;
2172 switch (c_parser_peek_token (parser)->keyword)
2174 case RID_STRUCT:
2175 code = RECORD_TYPE;
2176 break;
2177 case RID_UNION:
2178 code = UNION_TYPE;
2179 break;
2180 default:
2181 gcc_unreachable ();
2183 struct_loc = c_parser_peek_token (parser)->location;
2184 c_parser_consume_token (parser);
2185 attrs = c_parser_attributes (parser);
2187 /* Set the location in case we create a decl now. */
2188 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2190 if (c_parser_next_token_is (parser, CPP_NAME))
2192 ident = c_parser_peek_token (parser)->value;
2193 ident_loc = c_parser_peek_token (parser)->location;
2194 struct_loc = ident_loc;
2195 c_parser_consume_token (parser);
2197 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2199 /* Parse a struct or union definition. Start the scope of the
2200 tag before parsing components. */
2201 struct c_struct_parse_info *struct_info;
2202 tree type = start_struct (struct_loc, code, ident, &struct_info);
2203 tree postfix_attrs;
2204 /* We chain the components in reverse order, then put them in
2205 forward order at the end. Each struct-declaration may
2206 declare multiple components (comma-separated), so we must use
2207 chainon to join them, although when parsing each
2208 struct-declaration we can use TREE_CHAIN directly.
2210 The theory behind all this is that there will be more
2211 semicolon separated fields than comma separated fields, and
2212 so we'll be minimizing the number of node traversals required
2213 by chainon. */
2214 tree contents = NULL_TREE;
2215 c_parser_consume_token (parser);
2216 /* Handle the Objective-C @defs construct,
2217 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2218 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2220 tree name;
2221 gcc_assert (c_dialect_objc ());
2222 c_parser_consume_token (parser);
2223 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2224 goto end_at_defs;
2225 if (c_parser_next_token_is (parser, CPP_NAME)
2226 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2228 name = c_parser_peek_token (parser)->value;
2229 c_parser_consume_token (parser);
2231 else
2233 c_parser_error (parser, "expected class name");
2234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2235 goto end_at_defs;
2237 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2238 "expected %<)%>");
2239 contents = nreverse (objc_get_class_ivars (name));
2241 end_at_defs:
2242 /* Parse the struct-declarations and semicolons. Problems with
2243 semicolons are diagnosed here; empty structures are diagnosed
2244 elsewhere. */
2245 while (true)
2247 tree decls;
2248 /* Parse any stray semicolon. */
2249 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2251 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2252 "extra semicolon in struct or union specified");
2253 c_parser_consume_token (parser);
2254 continue;
2256 /* Stop if at the end of the struct or union contents. */
2257 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2259 c_parser_consume_token (parser);
2260 break;
2262 /* Accept #pragmas at struct scope. */
2263 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2265 c_parser_pragma (parser, pragma_external);
2266 continue;
2268 /* Parse some comma-separated declarations, but not the
2269 trailing semicolon if any. */
2270 decls = c_parser_struct_declaration (parser);
2271 contents = chainon (decls, contents);
2272 /* If no semicolon follows, either we have a parse error or
2273 are at the end of the struct or union and should
2274 pedwarn. */
2275 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2276 c_parser_consume_token (parser);
2277 else
2279 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2280 pedwarn (c_parser_peek_token (parser)->location, 0,
2281 "no semicolon at end of struct or union");
2282 else
2284 c_parser_error (parser, "expected %<;%>");
2285 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2286 break;
2290 postfix_attrs = c_parser_attributes (parser);
2291 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2292 chainon (attrs, postfix_attrs), struct_info);
2293 ret.kind = ctsk_tagdef;
2294 ret.expr = NULL_TREE;
2295 ret.expr_const_operands = true;
2296 return ret;
2298 else if (!ident)
2300 c_parser_error (parser, "expected %<{%>");
2301 ret.spec = error_mark_node;
2302 ret.kind = ctsk_tagref;
2303 ret.expr = NULL_TREE;
2304 ret.expr_const_operands = true;
2305 return ret;
2307 ret = parser_xref_tag (ident_loc, code, ident);
2308 return ret;
2311 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2312 the trailing semicolon.
2314 struct-declaration:
2315 specifier-qualifier-list struct-declarator-list
2316 static_assert-declaration-no-semi
2318 specifier-qualifier-list:
2319 type-specifier specifier-qualifier-list[opt]
2320 type-qualifier specifier-qualifier-list[opt]
2321 attributes specifier-qualifier-list[opt]
2323 struct-declarator-list:
2324 struct-declarator
2325 struct-declarator-list , attributes[opt] struct-declarator
2327 struct-declarator:
2328 declarator attributes[opt]
2329 declarator[opt] : constant-expression attributes[opt]
2331 GNU extensions:
2333 struct-declaration:
2334 __extension__ struct-declaration
2335 specifier-qualifier-list
2337 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2338 of attributes where shown is a GNU extension. In GNU C, we accept
2339 any expression without commas in the syntax (assignment
2340 expressions, not just conditional expressions); assignment
2341 expressions will be diagnosed as non-constant. */
2343 static tree
2344 c_parser_struct_declaration (c_parser *parser)
2346 struct c_declspecs *specs;
2347 tree prefix_attrs;
2348 tree all_prefix_attrs;
2349 tree decls;
2350 location_t decl_loc;
2351 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2353 int ext;
2354 tree decl;
2355 ext = disable_extension_diagnostics ();
2356 c_parser_consume_token (parser);
2357 decl = c_parser_struct_declaration (parser);
2358 restore_extension_diagnostics (ext);
2359 return decl;
2361 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2363 c_parser_static_assert_declaration_no_semi (parser);
2364 return NULL_TREE;
2366 specs = build_null_declspecs ();
2367 decl_loc = c_parser_peek_token (parser)->location;
2368 c_parser_declspecs (parser, specs, false, true, true);
2369 if (parser->error)
2370 return NULL_TREE;
2371 if (!specs->declspecs_seen_p)
2373 c_parser_error (parser, "expected specifier-qualifier-list");
2374 return NULL_TREE;
2376 finish_declspecs (specs);
2377 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2379 tree ret;
2380 if (!specs->type_seen_p)
2382 pedwarn (decl_loc, OPT_pedantic,
2383 "ISO C forbids member declarations with no members");
2384 shadow_tag_warned (specs, pedantic);
2385 ret = NULL_TREE;
2387 else
2389 /* Support for unnamed structs or unions as members of
2390 structs or unions (which is [a] useful and [b] supports
2391 MS P-SDK). */
2392 tree attrs = NULL;
2394 ret = grokfield (c_parser_peek_token (parser)->location,
2395 build_id_declarator (NULL_TREE), specs,
2396 NULL_TREE, &attrs);
2397 if (ret)
2398 decl_attributes (&ret, attrs, 0);
2400 return ret;
2402 pending_xref_error ();
2403 prefix_attrs = specs->attrs;
2404 all_prefix_attrs = prefix_attrs;
2405 specs->attrs = NULL_TREE;
2406 decls = NULL_TREE;
2407 while (true)
2409 /* Declaring one or more declarators or un-named bit-fields. */
2410 struct c_declarator *declarator;
2411 bool dummy = false;
2412 if (c_parser_next_token_is (parser, CPP_COLON))
2413 declarator = build_id_declarator (NULL_TREE);
2414 else
2415 declarator = c_parser_declarator (parser, specs->type_seen_p,
2416 C_DTR_NORMAL, &dummy);
2417 if (declarator == NULL)
2419 c_parser_skip_to_end_of_block_or_statement (parser);
2420 break;
2422 if (c_parser_next_token_is (parser, CPP_COLON)
2423 || c_parser_next_token_is (parser, CPP_COMMA)
2424 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2425 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2426 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2428 tree postfix_attrs = NULL_TREE;
2429 tree width = NULL_TREE;
2430 tree d;
2431 if (c_parser_next_token_is (parser, CPP_COLON))
2433 c_parser_consume_token (parser);
2434 width = c_parser_expr_no_commas (parser, NULL).value;
2436 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2437 postfix_attrs = c_parser_attributes (parser);
2438 d = grokfield (c_parser_peek_token (parser)->location,
2439 declarator, specs, width, &all_prefix_attrs);
2440 decl_attributes (&d, chainon (postfix_attrs,
2441 all_prefix_attrs), 0);
2442 DECL_CHAIN (d) = decls;
2443 decls = d;
2444 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2445 all_prefix_attrs = chainon (c_parser_attributes (parser),
2446 prefix_attrs);
2447 else
2448 all_prefix_attrs = prefix_attrs;
2449 if (c_parser_next_token_is (parser, CPP_COMMA))
2450 c_parser_consume_token (parser);
2451 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2452 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2454 /* Semicolon consumed in caller. */
2455 break;
2457 else
2459 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2460 break;
2463 else
2465 c_parser_error (parser,
2466 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2467 "%<__attribute__%>");
2468 break;
2471 return decls;
2474 /* Parse a typeof specifier (a GNU extension).
2476 typeof-specifier:
2477 typeof ( expression )
2478 typeof ( type-name )
2481 static struct c_typespec
2482 c_parser_typeof_specifier (c_parser *parser)
2484 struct c_typespec ret;
2485 ret.kind = ctsk_typeof;
2486 ret.spec = error_mark_node;
2487 ret.expr = NULL_TREE;
2488 ret.expr_const_operands = true;
2489 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2490 c_parser_consume_token (parser);
2491 c_inhibit_evaluation_warnings++;
2492 in_typeof++;
2493 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2495 c_inhibit_evaluation_warnings--;
2496 in_typeof--;
2497 return ret;
2499 if (c_parser_next_token_starts_typename (parser))
2501 struct c_type_name *type = c_parser_type_name (parser);
2502 c_inhibit_evaluation_warnings--;
2503 in_typeof--;
2504 if (type != NULL)
2506 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2507 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2510 else
2512 bool was_vm;
2513 location_t here = c_parser_peek_token (parser)->location;
2514 struct c_expr expr = c_parser_expression (parser);
2515 c_inhibit_evaluation_warnings--;
2516 in_typeof--;
2517 if (TREE_CODE (expr.value) == COMPONENT_REF
2518 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2519 error_at (here, "%<typeof%> applied to a bit-field");
2520 mark_exp_read (expr.value);
2521 ret.spec = TREE_TYPE (expr.value);
2522 if (c_dialect_objc()
2523 && ret.spec != error_mark_node
2524 && lookup_attribute ("objc_volatilized", TYPE_ATTRIBUTES (ret.spec)))
2525 ret.spec = build_qualified_type
2526 (ret.spec, (TYPE_QUALS (ret.spec) & ~TYPE_QUAL_VOLATILE));
2527 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2528 /* This is returned with the type so that when the type is
2529 evaluated, this can be evaluated. */
2530 if (was_vm)
2531 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2532 pop_maybe_used (was_vm);
2534 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2535 return ret;
2538 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2539 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2540 be redeclared; otherwise it may not. KIND indicates which kind of
2541 declarator is wanted. Returns a valid declarator except in the
2542 case of a syntax error in which case NULL is returned. *SEEN_ID is
2543 set to true if an identifier being declared is seen; this is used
2544 to diagnose bad forms of abstract array declarators and to
2545 determine whether an identifier list is syntactically permitted.
2547 declarator:
2548 pointer[opt] direct-declarator
2550 direct-declarator:
2551 identifier
2552 ( attributes[opt] declarator )
2553 direct-declarator array-declarator
2554 direct-declarator ( parameter-type-list )
2555 direct-declarator ( identifier-list[opt] )
2557 pointer:
2558 * type-qualifier-list[opt]
2559 * type-qualifier-list[opt] pointer
2561 type-qualifier-list:
2562 type-qualifier
2563 attributes
2564 type-qualifier-list type-qualifier
2565 type-qualifier-list attributes
2567 parameter-type-list:
2568 parameter-list
2569 parameter-list , ...
2571 parameter-list:
2572 parameter-declaration
2573 parameter-list , parameter-declaration
2575 parameter-declaration:
2576 declaration-specifiers declarator attributes[opt]
2577 declaration-specifiers abstract-declarator[opt] attributes[opt]
2579 identifier-list:
2580 identifier
2581 identifier-list , identifier
2583 abstract-declarator:
2584 pointer
2585 pointer[opt] direct-abstract-declarator
2587 direct-abstract-declarator:
2588 ( attributes[opt] abstract-declarator )
2589 direct-abstract-declarator[opt] array-declarator
2590 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2592 GNU extensions:
2594 direct-declarator:
2595 direct-declarator ( parameter-forward-declarations
2596 parameter-type-list[opt] )
2598 direct-abstract-declarator:
2599 direct-abstract-declarator[opt] ( parameter-forward-declarations
2600 parameter-type-list[opt] )
2602 parameter-forward-declarations:
2603 parameter-list ;
2604 parameter-forward-declarations parameter-list ;
2606 The uses of attributes shown above are GNU extensions.
2608 Some forms of array declarator are not included in C99 in the
2609 syntax for abstract declarators; these are disallowed elsewhere.
2610 This may be a defect (DR#289).
2612 This function also accepts an omitted abstract declarator as being
2613 an abstract declarator, although not part of the formal syntax. */
2615 static struct c_declarator *
2616 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2617 bool *seen_id)
2619 /* Parse any initial pointer part. */
2620 if (c_parser_next_token_is (parser, CPP_MULT))
2622 struct c_declspecs *quals_attrs = build_null_declspecs ();
2623 struct c_declarator *inner;
2624 c_parser_consume_token (parser);
2625 c_parser_declspecs (parser, quals_attrs, false, false, true);
2626 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2627 if (inner == NULL)
2628 return NULL;
2629 else
2630 return make_pointer_declarator (quals_attrs, inner);
2632 /* Now we have a direct declarator, direct abstract declarator or
2633 nothing (which counts as a direct abstract declarator here). */
2634 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2637 /* Parse a direct declarator or direct abstract declarator; arguments
2638 as c_parser_declarator. */
2640 static struct c_declarator *
2641 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2642 bool *seen_id)
2644 /* The direct declarator must start with an identifier (possibly
2645 omitted) or a parenthesized declarator (possibly abstract). In
2646 an ordinary declarator, initial parentheses must start a
2647 parenthesized declarator. In an abstract declarator or parameter
2648 declarator, they could start a parenthesized declarator or a
2649 parameter list. To tell which, the open parenthesis and any
2650 following attributes must be read. If a declaration specifier
2651 follows, then it is a parameter list; if the specifier is a
2652 typedef name, there might be an ambiguity about redeclaring it,
2653 which is resolved in the direction of treating it as a typedef
2654 name. If a close parenthesis follows, it is also an empty
2655 parameter list, as the syntax does not permit empty abstract
2656 declarators. Otherwise, it is a parenthesized declarator (in
2657 which case the analysis may be repeated inside it, recursively).
2659 ??? There is an ambiguity in a parameter declaration "int
2660 (__attribute__((foo)) x)", where x is not a typedef name: it
2661 could be an abstract declarator for a function, or declare x with
2662 parentheses. The proper resolution of this ambiguity needs
2663 documenting. At present we follow an accident of the old
2664 parser's implementation, whereby the first parameter must have
2665 some declaration specifiers other than just attributes. Thus as
2666 a parameter declaration it is treated as a parenthesized
2667 parameter named x, and as an abstract declarator it is
2668 rejected.
2670 ??? Also following the old parser, attributes inside an empty
2671 parameter list are ignored, making it a list not yielding a
2672 prototype, rather than giving an error or making it have one
2673 parameter with implicit type int.
2675 ??? Also following the old parser, typedef names may be
2676 redeclared in declarators, but not Objective-C class names. */
2678 if (kind != C_DTR_ABSTRACT
2679 && c_parser_next_token_is (parser, CPP_NAME)
2680 && ((type_seen_p
2681 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2682 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2683 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2685 struct c_declarator *inner
2686 = build_id_declarator (c_parser_peek_token (parser)->value);
2687 *seen_id = true;
2688 inner->id_loc = c_parser_peek_token (parser)->location;
2689 c_parser_consume_token (parser);
2690 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2693 if (kind != C_DTR_NORMAL
2694 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2696 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2697 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2700 /* Either we are at the end of an abstract declarator, or we have
2701 parentheses. */
2703 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2705 tree attrs;
2706 struct c_declarator *inner;
2707 c_parser_consume_token (parser);
2708 attrs = c_parser_attributes (parser);
2709 if (kind != C_DTR_NORMAL
2710 && (c_parser_next_token_starts_declspecs (parser)
2711 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2713 struct c_arg_info *args
2714 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2715 attrs);
2716 if (args == NULL)
2717 return NULL;
2718 else
2720 inner
2721 = build_function_declarator (args,
2722 build_id_declarator (NULL_TREE));
2723 return c_parser_direct_declarator_inner (parser, *seen_id,
2724 inner);
2727 /* A parenthesized declarator. */
2728 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2729 if (inner != NULL && attrs != NULL)
2730 inner = build_attrs_declarator (attrs, inner);
2731 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2733 c_parser_consume_token (parser);
2734 if (inner == NULL)
2735 return NULL;
2736 else
2737 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2739 else
2741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2742 "expected %<)%>");
2743 return NULL;
2746 else
2748 if (kind == C_DTR_NORMAL)
2750 c_parser_error (parser, "expected identifier or %<(%>");
2751 return NULL;
2753 else
2754 return build_id_declarator (NULL_TREE);
2758 /* Parse part of a direct declarator or direct abstract declarator,
2759 given that some (in INNER) has already been parsed; ID_PRESENT is
2760 true if an identifier is present, false for an abstract
2761 declarator. */
2763 static struct c_declarator *
2764 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2765 struct c_declarator *inner)
2767 /* Parse a sequence of array declarators and parameter lists. */
2768 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2770 location_t brace_loc = c_parser_peek_token (parser)->location;
2771 struct c_declarator *declarator;
2772 struct c_declspecs *quals_attrs = build_null_declspecs ();
2773 bool static_seen;
2774 bool star_seen;
2775 tree dimen;
2776 c_parser_consume_token (parser);
2777 c_parser_declspecs (parser, quals_attrs, false, false, true);
2778 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2779 if (static_seen)
2780 c_parser_consume_token (parser);
2781 if (static_seen && !quals_attrs->declspecs_seen_p)
2782 c_parser_declspecs (parser, quals_attrs, false, false, true);
2783 if (!quals_attrs->declspecs_seen_p)
2784 quals_attrs = NULL;
2785 /* If "static" is present, there must be an array dimension.
2786 Otherwise, there may be a dimension, "*", or no
2787 dimension. */
2788 if (static_seen)
2790 star_seen = false;
2791 dimen = c_parser_expr_no_commas (parser, NULL).value;
2793 else
2795 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2797 dimen = NULL_TREE;
2798 star_seen = false;
2800 else if (c_parser_next_token_is (parser, CPP_MULT))
2802 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2804 dimen = NULL_TREE;
2805 star_seen = true;
2806 c_parser_consume_token (parser);
2808 else
2810 star_seen = false;
2811 dimen = c_parser_expr_no_commas (parser, NULL).value;
2814 else
2816 star_seen = false;
2817 dimen = c_parser_expr_no_commas (parser, NULL).value;
2820 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2821 c_parser_consume_token (parser);
2822 else
2824 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2825 "expected %<]%>");
2826 return NULL;
2828 if (dimen)
2829 mark_exp_read (dimen);
2830 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2831 static_seen, star_seen);
2832 if (declarator == NULL)
2833 return NULL;
2834 inner = set_array_declarator_inner (declarator, inner);
2835 return c_parser_direct_declarator_inner (parser, id_present, inner);
2837 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2839 tree attrs;
2840 struct c_arg_info *args;
2841 c_parser_consume_token (parser);
2842 attrs = c_parser_attributes (parser);
2843 args = c_parser_parms_declarator (parser, id_present, attrs);
2844 if (args == NULL)
2845 return NULL;
2846 else
2848 inner = build_function_declarator (args, inner);
2849 return c_parser_direct_declarator_inner (parser, id_present, inner);
2852 return inner;
2855 /* Parse a parameter list or identifier list, including the closing
2856 parenthesis but not the opening one. ATTRS are the attributes at
2857 the start of the list. ID_LIST_OK is true if an identifier list is
2858 acceptable; such a list must not have attributes at the start. */
2860 static struct c_arg_info *
2861 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2863 push_scope ();
2864 declare_parm_level ();
2865 /* If the list starts with an identifier, it is an identifier list.
2866 Otherwise, it is either a prototype list or an empty list. */
2867 if (id_list_ok
2868 && !attrs
2869 && c_parser_next_token_is (parser, CPP_NAME)
2870 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2872 tree list = NULL_TREE, *nextp = &list;
2873 while (c_parser_next_token_is (parser, CPP_NAME)
2874 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2876 *nextp = build_tree_list (NULL_TREE,
2877 c_parser_peek_token (parser)->value);
2878 nextp = & TREE_CHAIN (*nextp);
2879 c_parser_consume_token (parser);
2880 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2881 break;
2882 c_parser_consume_token (parser);
2883 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2885 c_parser_error (parser, "expected identifier");
2886 break;
2889 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2891 struct c_arg_info *ret = build_arg_info ();
2892 ret->types = list;
2893 c_parser_consume_token (parser);
2894 pop_scope ();
2895 return ret;
2897 else
2899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2900 "expected %<)%>");
2901 pop_scope ();
2902 return NULL;
2905 else
2907 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2908 pop_scope ();
2909 return ret;
2913 /* Parse a parameter list (possibly empty), including the closing
2914 parenthesis but not the opening one. ATTRS are the attributes at
2915 the start of the list. */
2917 static struct c_arg_info *
2918 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2920 bool bad_parm = false;
2921 /* ??? Following the old parser, forward parameter declarations may
2922 use abstract declarators, and if no real parameter declarations
2923 follow the forward declarations then this is not diagnosed. Also
2924 note as above that attributes are ignored as the only contents of
2925 the parentheses, or as the only contents after forward
2926 declarations. */
2927 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2929 struct c_arg_info *ret = build_arg_info ();
2930 c_parser_consume_token (parser);
2931 return ret;
2933 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2935 struct c_arg_info *ret = build_arg_info ();
2936 /* Suppress -Wold-style-definition for this case. */
2937 ret->types = error_mark_node;
2938 error_at (c_parser_peek_token (parser)->location,
2939 "ISO C requires a named argument before %<...%>");
2940 c_parser_consume_token (parser);
2941 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2943 c_parser_consume_token (parser);
2944 return ret;
2946 else
2948 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2949 "expected %<)%>");
2950 return NULL;
2953 /* Nonempty list of parameters, either terminated with semicolon
2954 (forward declarations; recurse) or with close parenthesis (normal
2955 function) or with ", ... )" (variadic function). */
2956 while (true)
2958 /* Parse a parameter. */
2959 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2960 attrs = NULL_TREE;
2961 if (parm == NULL)
2962 bad_parm = true;
2963 else
2964 push_parm_decl (parm);
2965 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2967 tree new_attrs;
2968 c_parser_consume_token (parser);
2969 mark_forward_parm_decls ();
2970 new_attrs = c_parser_attributes (parser);
2971 return c_parser_parms_list_declarator (parser, new_attrs);
2973 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2975 c_parser_consume_token (parser);
2976 if (bad_parm)
2978 get_pending_sizes ();
2979 return NULL;
2981 else
2982 return get_parm_info (false);
2984 if (!c_parser_require (parser, CPP_COMMA,
2985 "expected %<;%>, %<,%> or %<)%>"))
2987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2988 get_pending_sizes ();
2989 return NULL;
2991 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2993 c_parser_consume_token (parser);
2994 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2996 c_parser_consume_token (parser);
2997 if (bad_parm)
2999 get_pending_sizes ();
3000 return NULL;
3002 else
3003 return get_parm_info (true);
3005 else
3007 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3008 "expected %<)%>");
3009 get_pending_sizes ();
3010 return NULL;
3016 /* Parse a parameter declaration. ATTRS are the attributes at the
3017 start of the declaration if it is the first parameter. */
3019 static struct c_parm *
3020 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3022 struct c_declspecs *specs;
3023 struct c_declarator *declarator;
3024 tree prefix_attrs;
3025 tree postfix_attrs = NULL_TREE;
3026 bool dummy = false;
3027 if (!c_parser_next_token_starts_declspecs (parser))
3029 c_token *token = c_parser_peek_token (parser);
3030 if (parser->error)
3031 return NULL;
3032 c_parser_set_source_position_from_token (token);
3033 if (token->type == CPP_NAME
3034 && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
3035 && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
3037 error ("unknown type name %qE", token->value);
3038 parser->error = true;
3040 /* ??? In some Objective-C cases '...' isn't applicable so there
3041 should be a different message. */
3042 else
3043 c_parser_error (parser,
3044 "expected declaration specifiers or %<...%>");
3045 c_parser_skip_to_end_of_parameter (parser);
3046 return NULL;
3048 specs = build_null_declspecs ();
3049 if (attrs)
3051 declspecs_add_attrs (specs, attrs);
3052 attrs = NULL_TREE;
3054 c_parser_declspecs (parser, specs, true, true, true);
3055 finish_declspecs (specs);
3056 pending_xref_error ();
3057 prefix_attrs = specs->attrs;
3058 specs->attrs = NULL_TREE;
3059 declarator = c_parser_declarator (parser, specs->type_seen_p,
3060 C_DTR_PARM, &dummy);
3061 if (declarator == NULL)
3063 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3064 return NULL;
3066 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3067 postfix_attrs = c_parser_attributes (parser);
3068 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3069 declarator);
3072 /* Parse a string literal in an asm expression. It should not be
3073 translated, and wide string literals are an error although
3074 permitted by the syntax. This is a GNU extension.
3076 asm-string-literal:
3077 string-literal
3079 ??? At present, following the old parser, the caller needs to have
3080 set lex_untranslated_string to 1. It would be better to follow the
3081 C++ parser rather than using this kludge. */
3083 static tree
3084 c_parser_asm_string_literal (c_parser *parser)
3086 tree str;
3087 if (c_parser_next_token_is (parser, CPP_STRING))
3089 str = c_parser_peek_token (parser)->value;
3090 c_parser_consume_token (parser);
3092 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3094 error_at (c_parser_peek_token (parser)->location,
3095 "wide string literal in %<asm%>");
3096 str = build_string (1, "");
3097 c_parser_consume_token (parser);
3099 else
3101 c_parser_error (parser, "expected string literal");
3102 str = NULL_TREE;
3104 return str;
3107 /* Parse a simple asm expression. This is used in restricted
3108 contexts, where a full expression with inputs and outputs does not
3109 make sense. This is a GNU extension.
3111 simple-asm-expr:
3112 asm ( asm-string-literal )
3115 static tree
3116 c_parser_simple_asm_expr (c_parser *parser)
3118 tree str;
3119 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3120 /* ??? Follow the C++ parser rather than using the
3121 lex_untranslated_string kludge. */
3122 parser->lex_untranslated_string = true;
3123 c_parser_consume_token (parser);
3124 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3126 parser->lex_untranslated_string = false;
3127 return NULL_TREE;
3129 str = c_parser_asm_string_literal (parser);
3130 parser->lex_untranslated_string = false;
3131 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3134 return NULL_TREE;
3136 return str;
3139 /* Parse (possibly empty) attributes. This is a GNU extension.
3141 attributes:
3142 empty
3143 attributes attribute
3145 attribute:
3146 __attribute__ ( ( attribute-list ) )
3148 attribute-list:
3149 attrib
3150 attribute_list , attrib
3152 attrib:
3153 empty
3154 any-word
3155 any-word ( identifier )
3156 any-word ( identifier , nonempty-expr-list )
3157 any-word ( expr-list )
3159 where the "identifier" must not be declared as a type, and
3160 "any-word" may be any identifier (including one declared as a
3161 type), a reserved word storage class specifier, type specifier or
3162 type qualifier. ??? This still leaves out most reserved keywords
3163 (following the old parser), shouldn't we include them, and why not
3164 allow identifiers declared as types to start the arguments? */
3166 static tree
3167 c_parser_attributes (c_parser *parser)
3169 tree attrs = NULL_TREE;
3170 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3172 /* ??? Follow the C++ parser rather than using the
3173 lex_untranslated_string kludge. */
3174 parser->lex_untranslated_string = true;
3175 c_parser_consume_token (parser);
3176 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3178 parser->lex_untranslated_string = false;
3179 return attrs;
3181 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3183 parser->lex_untranslated_string = false;
3184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3185 return attrs;
3187 /* Parse the attribute list. */
3188 while (c_parser_next_token_is (parser, CPP_COMMA)
3189 || c_parser_next_token_is (parser, CPP_NAME)
3190 || c_parser_next_token_is (parser, CPP_KEYWORD))
3192 tree attr, attr_name, attr_args;
3193 VEC(tree,gc) *expr_list;
3194 if (c_parser_next_token_is (parser, CPP_COMMA))
3196 c_parser_consume_token (parser);
3197 continue;
3199 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3201 /* ??? See comment above about what keywords are
3202 accepted here. */
3203 bool ok;
3204 switch (c_parser_peek_token (parser)->keyword)
3206 case RID_STATIC:
3207 case RID_UNSIGNED:
3208 case RID_LONG:
3209 case RID_INT128:
3210 case RID_CONST:
3211 case RID_EXTERN:
3212 case RID_REGISTER:
3213 case RID_TYPEDEF:
3214 case RID_SHORT:
3215 case RID_INLINE:
3216 case RID_VOLATILE:
3217 case RID_SIGNED:
3218 case RID_AUTO:
3219 case RID_RESTRICT:
3220 case RID_COMPLEX:
3221 case RID_THREAD:
3222 case RID_INT:
3223 case RID_CHAR:
3224 case RID_FLOAT:
3225 case RID_DOUBLE:
3226 case RID_VOID:
3227 case RID_DFLOAT32:
3228 case RID_DFLOAT64:
3229 case RID_DFLOAT128:
3230 case RID_BOOL:
3231 case RID_FRACT:
3232 case RID_ACCUM:
3233 case RID_SAT:
3234 ok = true;
3235 break;
3236 default:
3237 ok = false;
3238 break;
3240 if (!ok)
3241 break;
3242 /* Accept __attribute__((__const)) as __attribute__((const))
3243 etc. */
3244 attr_name
3245 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3247 else
3248 attr_name = c_parser_peek_token (parser)->value;
3249 c_parser_consume_token (parser);
3250 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3252 attr = build_tree_list (attr_name, NULL_TREE);
3253 attrs = chainon (attrs, attr);
3254 continue;
3256 c_parser_consume_token (parser);
3257 /* Parse the attribute contents. If they start with an
3258 identifier which is followed by a comma or close
3259 parenthesis, then the arguments start with that
3260 identifier; otherwise they are an expression list. */
3261 if (c_parser_next_token_is (parser, CPP_NAME)
3262 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3263 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3264 || (c_parser_peek_2nd_token (parser)->type
3265 == CPP_CLOSE_PAREN)))
3267 tree arg1 = c_parser_peek_token (parser)->value;
3268 c_parser_consume_token (parser);
3269 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3270 attr_args = build_tree_list (NULL_TREE, arg1);
3271 else
3273 tree tree_list;
3274 c_parser_consume_token (parser);
3275 expr_list = c_parser_expr_list (parser, false, true, NULL);
3276 tree_list = build_tree_list_vec (expr_list);
3277 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3278 release_tree_vector (expr_list);
3281 else
3283 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3284 attr_args = NULL_TREE;
3285 else
3287 expr_list = c_parser_expr_list (parser, false, true, NULL);
3288 attr_args = build_tree_list_vec (expr_list);
3289 release_tree_vector (expr_list);
3292 attr = build_tree_list (attr_name, attr_args);
3293 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3294 c_parser_consume_token (parser);
3295 else
3297 parser->lex_untranslated_string = false;
3298 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3299 "expected %<)%>");
3300 return attrs;
3302 attrs = chainon (attrs, attr);
3304 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3305 c_parser_consume_token (parser);
3306 else
3308 parser->lex_untranslated_string = false;
3309 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3310 "expected %<)%>");
3311 return attrs;
3313 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3314 c_parser_consume_token (parser);
3315 else
3317 parser->lex_untranslated_string = false;
3318 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3319 "expected %<)%>");
3320 return attrs;
3322 parser->lex_untranslated_string = false;
3324 return attrs;
3327 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3329 type-name:
3330 specifier-qualifier-list abstract-declarator[opt]
3333 static struct c_type_name *
3334 c_parser_type_name (c_parser *parser)
3336 struct c_declspecs *specs = build_null_declspecs ();
3337 struct c_declarator *declarator;
3338 struct c_type_name *ret;
3339 bool dummy = false;
3340 c_parser_declspecs (parser, specs, false, true, true);
3341 if (!specs->declspecs_seen_p)
3343 c_parser_error (parser, "expected specifier-qualifier-list");
3344 return NULL;
3346 pending_xref_error ();
3347 finish_declspecs (specs);
3348 declarator = c_parser_declarator (parser, specs->type_seen_p,
3349 C_DTR_ABSTRACT, &dummy);
3350 if (declarator == NULL)
3351 return NULL;
3352 ret = XOBNEW (&parser_obstack, struct c_type_name);
3353 ret->specs = specs;
3354 ret->declarator = declarator;
3355 return ret;
3358 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3360 initializer:
3361 assignment-expression
3362 { initializer-list }
3363 { initializer-list , }
3365 initializer-list:
3366 designation[opt] initializer
3367 initializer-list , designation[opt] initializer
3369 designation:
3370 designator-list =
3372 designator-list:
3373 designator
3374 designator-list designator
3376 designator:
3377 array-designator
3378 . identifier
3380 array-designator:
3381 [ constant-expression ]
3383 GNU extensions:
3385 initializer:
3388 designation:
3389 array-designator
3390 identifier :
3392 array-designator:
3393 [ constant-expression ... constant-expression ]
3395 Any expression without commas is accepted in the syntax for the
3396 constant-expressions, with non-constant expressions rejected later.
3398 This function is only used for top-level initializers; for nested
3399 ones, see c_parser_initval. */
3401 static struct c_expr
3402 c_parser_initializer (c_parser *parser)
3404 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3405 return c_parser_braced_init (parser, NULL_TREE, false);
3406 else
3408 struct c_expr ret;
3409 location_t loc = c_parser_peek_token (parser)->location;
3410 ret = c_parser_expr_no_commas (parser, NULL);
3411 if (TREE_CODE (ret.value) != STRING_CST
3412 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3413 ret = default_function_array_read_conversion (loc, ret);
3414 return ret;
3418 /* Parse a braced initializer list. TYPE is the type specified for a
3419 compound literal, and NULL_TREE for other initializers and for
3420 nested braced lists. NESTED_P is true for nested braced lists,
3421 false for the list of a compound literal or the list that is the
3422 top-level initializer in a declaration. */
3424 static struct c_expr
3425 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3427 struct c_expr ret;
3428 struct obstack braced_init_obstack;
3429 location_t brace_loc = c_parser_peek_token (parser)->location;
3430 gcc_obstack_init (&braced_init_obstack);
3431 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3432 c_parser_consume_token (parser);
3433 if (nested_p)
3434 push_init_level (0, &braced_init_obstack);
3435 else
3436 really_start_incremental_init (type);
3437 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3439 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3441 else
3443 /* Parse a non-empty initializer list, possibly with a trailing
3444 comma. */
3445 while (true)
3447 c_parser_initelt (parser, &braced_init_obstack);
3448 if (parser->error)
3449 break;
3450 if (c_parser_next_token_is (parser, CPP_COMMA))
3451 c_parser_consume_token (parser);
3452 else
3453 break;
3454 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3455 break;
3458 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3460 ret.value = error_mark_node;
3461 ret.original_code = ERROR_MARK;
3462 ret.original_type = NULL;
3463 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3464 pop_init_level (0, &braced_init_obstack);
3465 obstack_free (&braced_init_obstack, NULL);
3466 return ret;
3468 c_parser_consume_token (parser);
3469 ret = pop_init_level (0, &braced_init_obstack);
3470 obstack_free (&braced_init_obstack, NULL);
3471 return ret;
3474 /* Parse a nested initializer, including designators. */
3476 static void
3477 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3479 /* Parse any designator or designator list. A single array
3480 designator may have the subsequent "=" omitted in GNU C, but a
3481 longer list or a structure member designator may not. */
3482 if (c_parser_next_token_is (parser, CPP_NAME)
3483 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3485 /* Old-style structure member designator. */
3486 set_init_label (c_parser_peek_token (parser)->value,
3487 braced_init_obstack);
3488 /* Use the colon as the error location. */
3489 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3490 "obsolete use of designated initializer with %<:%>");
3491 c_parser_consume_token (parser);
3492 c_parser_consume_token (parser);
3494 else
3496 /* des_seen is 0 if there have been no designators, 1 if there
3497 has been a single array designator and 2 otherwise. */
3498 int des_seen = 0;
3499 /* Location of a designator. */
3500 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3501 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3502 || c_parser_next_token_is (parser, CPP_DOT))
3504 int des_prev = des_seen;
3505 if (!des_seen)
3506 des_loc = c_parser_peek_token (parser)->location;
3507 if (des_seen < 2)
3508 des_seen++;
3509 if (c_parser_next_token_is (parser, CPP_DOT))
3511 des_seen = 2;
3512 c_parser_consume_token (parser);
3513 if (c_parser_next_token_is (parser, CPP_NAME))
3515 set_init_label (c_parser_peek_token (parser)->value,
3516 braced_init_obstack);
3517 c_parser_consume_token (parser);
3519 else
3521 struct c_expr init;
3522 init.value = error_mark_node;
3523 init.original_code = ERROR_MARK;
3524 init.original_type = NULL;
3525 c_parser_error (parser, "expected identifier");
3526 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3527 process_init_element (init, false, braced_init_obstack);
3528 return;
3531 else
3533 tree first, second;
3534 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3535 /* ??? Following the old parser, [ objc-receiver
3536 objc-message-args ] is accepted as an initializer,
3537 being distinguished from a designator by what follows
3538 the first assignment expression inside the square
3539 brackets, but after a first array designator a
3540 subsequent square bracket is for Objective-C taken to
3541 start an expression, using the obsolete form of
3542 designated initializer without '=', rather than
3543 possibly being a second level of designation: in LALR
3544 terms, the '[' is shifted rather than reducing
3545 designator to designator-list. */
3546 if (des_prev == 1 && c_dialect_objc ())
3548 des_seen = des_prev;
3549 break;
3551 if (des_prev == 0 && c_dialect_objc ())
3553 /* This might be an array designator or an
3554 Objective-C message expression. If the former,
3555 continue parsing here; if the latter, parse the
3556 remainder of the initializer given the starting
3557 primary-expression. ??? It might make sense to
3558 distinguish when des_prev == 1 as well; see
3559 previous comment. */
3560 tree rec, args;
3561 struct c_expr mexpr;
3562 c_parser_consume_token (parser);
3563 if (c_parser_peek_token (parser)->type == CPP_NAME
3564 && ((c_parser_peek_token (parser)->id_kind
3565 == C_ID_TYPENAME)
3566 || (c_parser_peek_token (parser)->id_kind
3567 == C_ID_CLASSNAME)))
3569 /* Type name receiver. */
3570 tree id = c_parser_peek_token (parser)->value;
3571 c_parser_consume_token (parser);
3572 rec = objc_get_class_reference (id);
3573 goto parse_message_args;
3575 first = c_parser_expr_no_commas (parser, NULL).value;
3576 mark_exp_read (first);
3577 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3578 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3579 goto array_desig_after_first;
3580 /* Expression receiver. So far only one part
3581 without commas has been parsed; there might be
3582 more of the expression. */
3583 rec = first;
3584 while (c_parser_next_token_is (parser, CPP_COMMA))
3586 struct c_expr next;
3587 location_t comma_loc, exp_loc;
3588 comma_loc = c_parser_peek_token (parser)->location;
3589 c_parser_consume_token (parser);
3590 exp_loc = c_parser_peek_token (parser)->location;
3591 next = c_parser_expr_no_commas (parser, NULL);
3592 next = default_function_array_read_conversion (exp_loc,
3593 next);
3594 rec = build_compound_expr (comma_loc, rec, next.value);
3596 parse_message_args:
3597 /* Now parse the objc-message-args. */
3598 args = c_parser_objc_message_args (parser);
3599 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3600 "expected %<]%>");
3601 mexpr.value
3602 = objc_build_message_expr (build_tree_list (rec, args));
3603 mexpr.original_code = ERROR_MARK;
3604 mexpr.original_type = NULL;
3605 /* Now parse and process the remainder of the
3606 initializer, starting with this message
3607 expression as a primary-expression. */
3608 c_parser_initval (parser, &mexpr, braced_init_obstack);
3609 return;
3611 c_parser_consume_token (parser);
3612 first = c_parser_expr_no_commas (parser, NULL).value;
3613 mark_exp_read (first);
3614 array_desig_after_first:
3615 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3617 ellipsis_loc = c_parser_peek_token (parser)->location;
3618 c_parser_consume_token (parser);
3619 second = c_parser_expr_no_commas (parser, NULL).value;
3620 mark_exp_read (second);
3622 else
3623 second = NULL_TREE;
3624 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3626 c_parser_consume_token (parser);
3627 set_init_index (first, second, braced_init_obstack);
3628 if (second)
3629 pedwarn (ellipsis_loc, OPT_pedantic,
3630 "ISO C forbids specifying range of elements to initialize");
3632 else
3633 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3634 "expected %<]%>");
3637 if (des_seen >= 1)
3639 if (c_parser_next_token_is (parser, CPP_EQ))
3641 if (!flag_isoc99)
3642 pedwarn (des_loc, OPT_pedantic,
3643 "ISO C90 forbids specifying subobject to initialize");
3644 c_parser_consume_token (parser);
3646 else
3648 if (des_seen == 1)
3649 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3650 "obsolete use of designated initializer without %<=%>");
3651 else
3653 struct c_expr init;
3654 init.value = error_mark_node;
3655 init.original_code = ERROR_MARK;
3656 init.original_type = NULL;
3657 c_parser_error (parser, "expected %<=%>");
3658 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3659 process_init_element (init, false, braced_init_obstack);
3660 return;
3665 c_parser_initval (parser, NULL, braced_init_obstack);
3668 /* Parse a nested initializer; as c_parser_initializer but parses
3669 initializers within braced lists, after any designators have been
3670 applied. If AFTER is not NULL then it is an Objective-C message
3671 expression which is the primary-expression starting the
3672 initializer. */
3674 static void
3675 c_parser_initval (c_parser *parser, struct c_expr *after,
3676 struct obstack * braced_init_obstack)
3678 struct c_expr init;
3679 gcc_assert (!after || c_dialect_objc ());
3680 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3681 init = c_parser_braced_init (parser, NULL_TREE, true);
3682 else
3684 location_t loc = c_parser_peek_token (parser)->location;
3685 init = c_parser_expr_no_commas (parser, after);
3686 if (init.value != NULL_TREE
3687 && TREE_CODE (init.value) != STRING_CST
3688 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3689 init = default_function_array_read_conversion (loc, init);
3691 process_init_element (init, false, braced_init_obstack);
3694 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3695 C99 6.8.2).
3697 compound-statement:
3698 { block-item-list[opt] }
3699 { label-declarations block-item-list }
3701 block-item-list:
3702 block-item
3703 block-item-list block-item
3705 block-item:
3706 nested-declaration
3707 statement
3709 nested-declaration:
3710 declaration
3712 GNU extensions:
3714 compound-statement:
3715 { label-declarations block-item-list }
3717 nested-declaration:
3718 __extension__ nested-declaration
3719 nested-function-definition
3721 label-declarations:
3722 label-declaration
3723 label-declarations label-declaration
3725 label-declaration:
3726 __label__ identifier-list ;
3728 Allowing the mixing of declarations and code is new in C99. The
3729 GNU syntax also permits (not shown above) labels at the end of
3730 compound statements, which yield an error. We don't allow labels
3731 on declarations; this might seem like a natural extension, but
3732 there would be a conflict between attributes on the label and
3733 prefix attributes on the declaration. ??? The syntax follows the
3734 old parser in requiring something after label declarations.
3735 Although they are erroneous if the labels declared aren't defined,
3736 is it useful for the syntax to be this way?
3738 OpenMP:
3740 block-item:
3741 openmp-directive
3743 openmp-directive:
3744 barrier-directive
3745 flush-directive */
3747 static tree
3748 c_parser_compound_statement (c_parser *parser)
3750 tree stmt;
3751 location_t brace_loc;
3752 brace_loc = c_parser_peek_token (parser)->location;
3753 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3755 /* Ensure a scope is entered and left anyway to avoid confusion
3756 if we have just prepared to enter a function body. */
3757 stmt = c_begin_compound_stmt (true);
3758 c_end_compound_stmt (brace_loc, stmt, true);
3759 return error_mark_node;
3761 stmt = c_begin_compound_stmt (true);
3762 c_parser_compound_statement_nostart (parser);
3763 return c_end_compound_stmt (brace_loc, stmt, true);
3766 /* Parse a compound statement except for the opening brace. This is
3767 used for parsing both compound statements and statement expressions
3768 (which follow different paths to handling the opening). */
3770 static void
3771 c_parser_compound_statement_nostart (c_parser *parser)
3773 bool last_stmt = false;
3774 bool last_label = false;
3775 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3776 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3777 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3779 c_parser_consume_token (parser);
3780 return;
3782 mark_valid_location_for_stdc_pragma (true);
3783 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3785 /* Read zero or more forward-declarations for labels that nested
3786 functions can jump to. */
3787 mark_valid_location_for_stdc_pragma (false);
3788 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3790 label_loc = c_parser_peek_token (parser)->location;
3791 c_parser_consume_token (parser);
3792 /* Any identifiers, including those declared as type names,
3793 are OK here. */
3794 while (true)
3796 tree label;
3797 if (c_parser_next_token_is_not (parser, CPP_NAME))
3799 c_parser_error (parser, "expected identifier");
3800 break;
3802 label
3803 = declare_label (c_parser_peek_token (parser)->value);
3804 C_DECLARED_LABEL_FLAG (label) = 1;
3805 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3806 c_parser_consume_token (parser);
3807 if (c_parser_next_token_is (parser, CPP_COMMA))
3808 c_parser_consume_token (parser);
3809 else
3810 break;
3812 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3814 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3816 /* We must now have at least one statement, label or declaration. */
3817 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3819 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3820 c_parser_error (parser, "expected declaration or statement");
3821 c_parser_consume_token (parser);
3822 return;
3824 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3826 location_t loc = c_parser_peek_token (parser)->location;
3827 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3828 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3829 || (c_parser_next_token_is (parser, CPP_NAME)
3830 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3832 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3833 label_loc = c_parser_peek_2nd_token (parser)->location;
3834 else
3835 label_loc = c_parser_peek_token (parser)->location;
3836 last_label = true;
3837 last_stmt = false;
3838 mark_valid_location_for_stdc_pragma (false);
3839 c_parser_label (parser);
3841 else if (!last_label
3842 && c_parser_next_token_starts_declaration (parser))
3844 last_label = false;
3845 mark_valid_location_for_stdc_pragma (false);
3846 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
3847 if (last_stmt)
3848 pedwarn_c90 (loc,
3849 (pedantic && !flag_isoc99)
3850 ? OPT_pedantic
3851 : OPT_Wdeclaration_after_statement,
3852 "ISO C90 forbids mixed declarations and code");
3853 last_stmt = false;
3855 else if (!last_label
3856 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3858 /* __extension__ can start a declaration, but is also an
3859 unary operator that can start an expression. Consume all
3860 but the last of a possible series of __extension__ to
3861 determine which. */
3862 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3863 && (c_parser_peek_2nd_token (parser)->keyword
3864 == RID_EXTENSION))
3865 c_parser_consume_token (parser);
3866 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
3868 int ext;
3869 ext = disable_extension_diagnostics ();
3870 c_parser_consume_token (parser);
3871 last_label = false;
3872 mark_valid_location_for_stdc_pragma (false);
3873 c_parser_declaration_or_fndef (parser, true, true, true, true,
3874 true, NULL);
3875 /* Following the old parser, __extension__ does not
3876 disable this diagnostic. */
3877 restore_extension_diagnostics (ext);
3878 if (last_stmt)
3879 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3880 ? OPT_pedantic
3881 : OPT_Wdeclaration_after_statement,
3882 "ISO C90 forbids mixed declarations and code");
3883 last_stmt = false;
3885 else
3886 goto statement;
3888 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3890 /* External pragmas, and some omp pragmas, are not associated
3891 with regular c code, and so are not to be considered statements
3892 syntactically. This ensures that the user doesn't put them
3893 places that would turn into syntax errors if the directive
3894 were ignored. */
3895 if (c_parser_pragma (parser, pragma_compound))
3896 last_label = false, last_stmt = true;
3898 else if (c_parser_next_token_is (parser, CPP_EOF))
3900 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3901 c_parser_error (parser, "expected declaration or statement");
3902 return;
3904 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3906 if (parser->in_if_block)
3908 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3909 error_at (loc, """expected %<}%> before %<else%>");
3910 return;
3912 else
3914 error_at (loc, "%<else%> without a previous %<if%>");
3915 c_parser_consume_token (parser);
3916 continue;
3919 else
3921 statement:
3922 last_label = false;
3923 last_stmt = true;
3924 mark_valid_location_for_stdc_pragma (false);
3925 c_parser_statement_after_labels (parser);
3928 parser->error = false;
3930 if (last_label)
3931 error_at (label_loc, "label at end of compound statement");
3932 c_parser_consume_token (parser);
3933 /* Restore the value we started with. */
3934 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3937 /* Parse a label (C90 6.6.1, C99 6.8.1).
3939 label:
3940 identifier : attributes[opt]
3941 case constant-expression :
3942 default :
3944 GNU extensions:
3946 label:
3947 case constant-expression ... constant-expression :
3949 The use of attributes on labels is a GNU extension. The syntax in
3950 GNU C accepts any expressions without commas, non-constant
3951 expressions being rejected later. */
3953 static void
3954 c_parser_label (c_parser *parser)
3956 location_t loc1 = c_parser_peek_token (parser)->location;
3957 tree label = NULL_TREE;
3958 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3960 tree exp1, exp2;
3961 c_parser_consume_token (parser);
3962 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3963 if (c_parser_next_token_is (parser, CPP_COLON))
3965 c_parser_consume_token (parser);
3966 label = do_case (loc1, exp1, NULL_TREE);
3968 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3970 c_parser_consume_token (parser);
3971 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3972 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3973 label = do_case (loc1, exp1, exp2);
3975 else
3976 c_parser_error (parser, "expected %<:%> or %<...%>");
3978 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3980 c_parser_consume_token (parser);
3981 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3982 label = do_case (loc1, NULL_TREE, NULL_TREE);
3984 else
3986 tree name = c_parser_peek_token (parser)->value;
3987 tree tlab;
3988 tree attrs;
3989 location_t loc2 = c_parser_peek_token (parser)->location;
3990 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3991 c_parser_consume_token (parser);
3992 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3993 c_parser_consume_token (parser);
3994 attrs = c_parser_attributes (parser);
3995 tlab = define_label (loc2, name);
3996 if (tlab)
3998 decl_attributes (&tlab, attrs, 0);
3999 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4002 if (label)
4004 if (c_parser_next_token_starts_declaration (parser)
4005 && !(c_parser_next_token_is (parser, CPP_NAME)
4006 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4008 error_at (c_parser_peek_token (parser)->location,
4009 "a label can only be part of a statement and "
4010 "a declaration is not a statement");
4011 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4012 /*static_assert_ok*/ true,
4013 /*nested*/ true, /*empty_ok*/ false,
4014 /*start_attr_ok*/ true, NULL);
4019 /* Parse a statement (C90 6.6, C99 6.8).
4021 statement:
4022 labeled-statement
4023 compound-statement
4024 expression-statement
4025 selection-statement
4026 iteration-statement
4027 jump-statement
4029 labeled-statement:
4030 label statement
4032 expression-statement:
4033 expression[opt] ;
4035 selection-statement:
4036 if-statement
4037 switch-statement
4039 iteration-statement:
4040 while-statement
4041 do-statement
4042 for-statement
4044 jump-statement:
4045 goto identifier ;
4046 continue ;
4047 break ;
4048 return expression[opt] ;
4050 GNU extensions:
4052 statement:
4053 asm-statement
4055 jump-statement:
4056 goto * expression ;
4058 Objective-C:
4060 statement:
4061 objc-throw-statement
4062 objc-try-catch-statement
4063 objc-synchronized-statement
4065 objc-throw-statement:
4066 @throw expression ;
4067 @throw ;
4069 OpenMP:
4071 statement:
4072 openmp-construct
4074 openmp-construct:
4075 parallel-construct
4076 for-construct
4077 sections-construct
4078 single-construct
4079 parallel-for-construct
4080 parallel-sections-construct
4081 master-construct
4082 critical-construct
4083 atomic-construct
4084 ordered-construct
4086 parallel-construct:
4087 parallel-directive structured-block
4089 for-construct:
4090 for-directive iteration-statement
4092 sections-construct:
4093 sections-directive section-scope
4095 single-construct:
4096 single-directive structured-block
4098 parallel-for-construct:
4099 parallel-for-directive iteration-statement
4101 parallel-sections-construct:
4102 parallel-sections-directive section-scope
4104 master-construct:
4105 master-directive structured-block
4107 critical-construct:
4108 critical-directive structured-block
4110 atomic-construct:
4111 atomic-directive expression-statement
4113 ordered-construct:
4114 ordered-directive structured-block */
4116 static void
4117 c_parser_statement (c_parser *parser)
4119 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4120 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4121 || (c_parser_next_token_is (parser, CPP_NAME)
4122 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4123 c_parser_label (parser);
4124 c_parser_statement_after_labels (parser);
4127 /* Parse a statement, other than a labeled statement. */
4129 static void
4130 c_parser_statement_after_labels (c_parser *parser)
4132 location_t loc = c_parser_peek_token (parser)->location;
4133 tree stmt = NULL_TREE;
4134 bool in_if_block = parser->in_if_block;
4135 parser->in_if_block = false;
4136 switch (c_parser_peek_token (parser)->type)
4138 case CPP_OPEN_BRACE:
4139 add_stmt (c_parser_compound_statement (parser));
4140 break;
4141 case CPP_KEYWORD:
4142 switch (c_parser_peek_token (parser)->keyword)
4144 case RID_IF:
4145 c_parser_if_statement (parser);
4146 break;
4147 case RID_SWITCH:
4148 c_parser_switch_statement (parser);
4149 break;
4150 case RID_WHILE:
4151 c_parser_while_statement (parser);
4152 break;
4153 case RID_DO:
4154 c_parser_do_statement (parser);
4155 break;
4156 case RID_FOR:
4157 c_parser_for_statement (parser);
4158 break;
4159 case RID_GOTO:
4160 c_parser_consume_token (parser);
4161 if (c_parser_next_token_is (parser, CPP_NAME))
4163 stmt = c_finish_goto_label (loc,
4164 c_parser_peek_token (parser)->value);
4165 c_parser_consume_token (parser);
4167 else if (c_parser_next_token_is (parser, CPP_MULT))
4169 tree val;
4171 c_parser_consume_token (parser);
4172 val = c_parser_expression (parser).value;
4173 mark_exp_read (val);
4174 stmt = c_finish_goto_ptr (loc, val);
4176 else
4177 c_parser_error (parser, "expected identifier or %<*%>");
4178 goto expect_semicolon;
4179 case RID_CONTINUE:
4180 c_parser_consume_token (parser);
4181 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4182 goto expect_semicolon;
4183 case RID_BREAK:
4184 c_parser_consume_token (parser);
4185 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4186 goto expect_semicolon;
4187 case RID_RETURN:
4188 c_parser_consume_token (parser);
4189 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4191 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4192 c_parser_consume_token (parser);
4194 else
4196 struct c_expr expr = c_parser_expression_conv (parser);
4197 mark_exp_read (expr.value);
4198 stmt = c_finish_return (loc, expr.value, expr.original_type);
4199 goto expect_semicolon;
4201 break;
4202 case RID_ASM:
4203 stmt = c_parser_asm_statement (parser);
4204 break;
4205 case RID_AT_THROW:
4206 gcc_assert (c_dialect_objc ());
4207 c_parser_consume_token (parser);
4208 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4210 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4211 c_parser_consume_token (parser);
4213 else
4215 tree expr = c_parser_expression (parser).value;
4216 expr = c_fully_fold (expr, false, NULL);
4217 stmt = objc_build_throw_stmt (loc, expr);
4218 goto expect_semicolon;
4220 break;
4221 case RID_AT_TRY:
4222 gcc_assert (c_dialect_objc ());
4223 c_parser_objc_try_catch_statement (parser);
4224 break;
4225 case RID_AT_SYNCHRONIZED:
4226 gcc_assert (c_dialect_objc ());
4227 c_parser_objc_synchronized_statement (parser);
4228 break;
4229 default:
4230 goto expr_stmt;
4232 break;
4233 case CPP_SEMICOLON:
4234 c_parser_consume_token (parser);
4235 break;
4236 case CPP_CLOSE_PAREN:
4237 case CPP_CLOSE_SQUARE:
4238 /* Avoid infinite loop in error recovery:
4239 c_parser_skip_until_found stops at a closing nesting
4240 delimiter without consuming it, but here we need to consume
4241 it to proceed further. */
4242 c_parser_error (parser, "expected statement");
4243 c_parser_consume_token (parser);
4244 break;
4245 case CPP_PRAGMA:
4246 c_parser_pragma (parser, pragma_stmt);
4247 break;
4248 default:
4249 expr_stmt:
4250 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4251 expect_semicolon:
4252 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4253 break;
4255 /* Two cases cannot and do not have line numbers associated: If stmt
4256 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4257 cannot hold line numbers. But that's OK because the statement
4258 will either be changed to a MODIFY_EXPR during gimplification of
4259 the statement expr, or discarded. If stmt was compound, but
4260 without new variables, we will have skipped the creation of a
4261 BIND and will have a bare STATEMENT_LIST. But that's OK because
4262 (recursively) all of the component statements should already have
4263 line numbers assigned. ??? Can we discard no-op statements
4264 earlier? */
4265 if (CAN_HAVE_LOCATION_P (stmt)
4266 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4267 SET_EXPR_LOCATION (stmt, loc);
4269 parser->in_if_block = in_if_block;
4272 /* Parse the condition from an if, do, while or for statements. */
4274 static tree
4275 c_parser_condition (c_parser *parser)
4277 location_t loc = c_parser_peek_token (parser)->location;
4278 tree cond;
4279 cond = c_parser_expression_conv (parser).value;
4280 cond = c_objc_common_truthvalue_conversion (loc, cond);
4281 cond = c_fully_fold (cond, false, NULL);
4282 if (warn_sequence_point)
4283 verify_sequence_points (cond);
4284 return cond;
4287 /* Parse a parenthesized condition from an if, do or while statement.
4289 condition:
4290 ( expression )
4292 static tree
4293 c_parser_paren_condition (c_parser *parser)
4295 tree cond;
4296 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4297 return error_mark_node;
4298 cond = c_parser_condition (parser);
4299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4300 return cond;
4303 /* Parse a statement which is a block in C99. */
4305 static tree
4306 c_parser_c99_block_statement (c_parser *parser)
4308 tree block = c_begin_compound_stmt (flag_isoc99);
4309 location_t loc = c_parser_peek_token (parser)->location;
4310 c_parser_statement (parser);
4311 return c_end_compound_stmt (loc, block, flag_isoc99);
4314 /* Parse the body of an if statement. This is just parsing a
4315 statement but (a) it is a block in C99, (b) we track whether the
4316 body is an if statement for the sake of -Wparentheses warnings, (c)
4317 we handle an empty body specially for the sake of -Wempty-body
4318 warnings, and (d) we call parser_compound_statement directly
4319 because c_parser_statement_after_labels resets
4320 parser->in_if_block. */
4322 static tree
4323 c_parser_if_body (c_parser *parser, bool *if_p)
4325 tree block = c_begin_compound_stmt (flag_isoc99);
4326 location_t body_loc = c_parser_peek_token (parser)->location;
4327 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4328 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4329 || (c_parser_next_token_is (parser, CPP_NAME)
4330 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4331 c_parser_label (parser);
4332 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4333 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4335 location_t loc = c_parser_peek_token (parser)->location;
4336 add_stmt (build_empty_stmt (loc));
4337 c_parser_consume_token (parser);
4338 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4339 warning_at (loc, OPT_Wempty_body,
4340 "suggest braces around empty body in an %<if%> statement");
4342 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4343 add_stmt (c_parser_compound_statement (parser));
4344 else
4345 c_parser_statement_after_labels (parser);
4346 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4349 /* Parse the else body of an if statement. This is just parsing a
4350 statement but (a) it is a block in C99, (b) we handle an empty body
4351 specially for the sake of -Wempty-body warnings. */
4353 static tree
4354 c_parser_else_body (c_parser *parser)
4356 location_t else_loc = c_parser_peek_token (parser)->location;
4357 tree block = c_begin_compound_stmt (flag_isoc99);
4358 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4359 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4360 || (c_parser_next_token_is (parser, CPP_NAME)
4361 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4362 c_parser_label (parser);
4363 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4365 location_t loc = c_parser_peek_token (parser)->location;
4366 warning_at (loc,
4367 OPT_Wempty_body,
4368 "suggest braces around empty body in an %<else%> statement");
4369 add_stmt (build_empty_stmt (loc));
4370 c_parser_consume_token (parser);
4372 else
4373 c_parser_statement_after_labels (parser);
4374 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4377 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4379 if-statement:
4380 if ( expression ) statement
4381 if ( expression ) statement else statement
4384 static void
4385 c_parser_if_statement (c_parser *parser)
4387 tree block;
4388 location_t loc;
4389 tree cond;
4390 bool first_if = false;
4391 tree first_body, second_body;
4392 bool in_if_block;
4394 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4395 c_parser_consume_token (parser);
4396 block = c_begin_compound_stmt (flag_isoc99);
4397 loc = c_parser_peek_token (parser)->location;
4398 cond = c_parser_paren_condition (parser);
4399 in_if_block = parser->in_if_block;
4400 parser->in_if_block = true;
4401 first_body = c_parser_if_body (parser, &first_if);
4402 parser->in_if_block = in_if_block;
4403 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4405 c_parser_consume_token (parser);
4406 second_body = c_parser_else_body (parser);
4408 else
4409 second_body = NULL_TREE;
4410 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4411 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4414 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4416 switch-statement:
4417 switch (expression) statement
4420 static void
4421 c_parser_switch_statement (c_parser *parser)
4423 tree block, expr, body, save_break;
4424 location_t switch_loc = c_parser_peek_token (parser)->location;
4425 location_t switch_cond_loc;
4426 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4427 c_parser_consume_token (parser);
4428 block = c_begin_compound_stmt (flag_isoc99);
4429 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4431 switch_cond_loc = c_parser_peek_token (parser)->location;
4432 expr = c_parser_expression (parser).value;
4433 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4435 else
4437 switch_cond_loc = UNKNOWN_LOCATION;
4438 expr = error_mark_node;
4440 c_start_case (switch_loc, switch_cond_loc, expr);
4441 save_break = c_break_label;
4442 c_break_label = NULL_TREE;
4443 body = c_parser_c99_block_statement (parser);
4444 c_finish_case (body);
4445 if (c_break_label)
4447 location_t here = c_parser_peek_token (parser)->location;
4448 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4449 SET_EXPR_LOCATION (t, here);
4450 add_stmt (t);
4452 c_break_label = save_break;
4453 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4456 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4458 while-statement:
4459 while (expression) statement
4462 static void
4463 c_parser_while_statement (c_parser *parser)
4465 tree block, cond, body, save_break, save_cont;
4466 location_t loc;
4467 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4468 c_parser_consume_token (parser);
4469 block = c_begin_compound_stmt (flag_isoc99);
4470 loc = c_parser_peek_token (parser)->location;
4471 cond = c_parser_paren_condition (parser);
4472 save_break = c_break_label;
4473 c_break_label = NULL_TREE;
4474 save_cont = c_cont_label;
4475 c_cont_label = NULL_TREE;
4476 body = c_parser_c99_block_statement (parser);
4477 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4478 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4479 c_break_label = save_break;
4480 c_cont_label = save_cont;
4483 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4485 do-statement:
4486 do statement while ( expression ) ;
4489 static void
4490 c_parser_do_statement (c_parser *parser)
4492 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4493 location_t loc;
4494 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4495 c_parser_consume_token (parser);
4496 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4497 warning_at (c_parser_peek_token (parser)->location,
4498 OPT_Wempty_body,
4499 "suggest braces around empty body in %<do%> statement");
4500 block = c_begin_compound_stmt (flag_isoc99);
4501 loc = c_parser_peek_token (parser)->location;
4502 save_break = c_break_label;
4503 c_break_label = NULL_TREE;
4504 save_cont = c_cont_label;
4505 c_cont_label = NULL_TREE;
4506 body = c_parser_c99_block_statement (parser);
4507 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4508 new_break = c_break_label;
4509 c_break_label = save_break;
4510 new_cont = c_cont_label;
4511 c_cont_label = save_cont;
4512 cond = c_parser_paren_condition (parser);
4513 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4514 c_parser_skip_to_end_of_block_or_statement (parser);
4515 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4516 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4519 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4521 for-statement:
4522 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4523 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4525 The form with a declaration is new in C99.
4527 ??? In accordance with the old parser, the declaration may be a
4528 nested function, which is then rejected in check_for_loop_decls,
4529 but does it make any sense for this to be included in the grammar?
4530 Note in particular that the nested function does not include a
4531 trailing ';', whereas the "declaration" production includes one.
4532 Also, can we reject bad declarations earlier and cheaper than
4533 check_for_loop_decls?
4535 In Objective-C, there are two additional variants:
4537 foreach-statement:
4538 for ( expression in expresssion ) statement
4539 for ( declaration in expression ) statement
4541 This is inconsistent with C, because the second variant is allowed
4542 even if c99 is not enabled.
4544 The rest of the comment documents these Objective-C foreach-statement.
4546 Here is the canonical example of the first variant:
4547 for (object in array) { do something with object }
4548 we call the first expression ("object") the "object_expression" and
4549 the second expression ("array") the "collection_expression".
4550 object_expression must be an lvalue of type "id" (a generic Objective-C
4551 object) because the loop works by assigning to object_expression the
4552 various objects from the collection_expression. collection_expression
4553 must evaluate to something of type "id" which responds to the method
4554 countByEnumeratingWithState:objects:count:.
4556 The canonical example of the second variant is:
4557 for (id object in array) { do something with object }
4558 which is completely equivalent to
4560 id object;
4561 for (object in array) { do something with object }
4563 Note that initizializing 'object' in some way (eg, "for ((object =
4564 xxx) in array) { do something with object }") is possibly
4565 technically valid, but completely pointless as 'object' will be
4566 assigned to something else as soon as the loop starts. We should
4567 most likely reject it (TODO).
4569 The beginning of the Objective-C foreach-statement looks exactly
4570 like the beginning of the for-statement, and we can tell it is a
4571 foreach-statement only because the initial declaration or
4572 expression is terminated by 'in' instead of ';'.
4575 static void
4576 c_parser_for_statement (c_parser *parser)
4578 tree block, cond, incr, save_break, save_cont, body;
4579 /* The following are only used when parsing an ObjC foreach statement. */
4580 tree object_expression, collection_expression;
4581 location_t loc = c_parser_peek_token (parser)->location;
4582 location_t for_loc = c_parser_peek_token (parser)->location;
4583 bool is_foreach_statement = false;
4584 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4585 c_parser_consume_token (parser);
4586 /* Open a compound statement in Objective-C as well, just in case this is
4587 as foreach expression. */
4588 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4589 cond = error_mark_node;
4590 incr = error_mark_node;
4591 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4593 /* Parse the initialization declaration or expression. */
4594 object_expression = error_mark_node;
4595 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4597 c_parser_consume_token (parser);
4598 c_finish_expr_stmt (loc, NULL_TREE);
4600 else if (c_parser_next_token_starts_declaration (parser))
4602 parser->objc_could_be_foreach_context = true;
4603 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4604 &object_expression);
4605 parser->objc_could_be_foreach_context = false;
4607 if (c_parser_next_token_is_keyword (parser, RID_IN))
4609 c_parser_consume_token (parser);
4610 is_foreach_statement = true;
4611 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4612 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4614 else
4615 check_for_loop_decls (for_loc, flag_isoc99);
4617 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4619 /* __extension__ can start a declaration, but is also an
4620 unary operator that can start an expression. Consume all
4621 but the last of a possible series of __extension__ to
4622 determine which. */
4623 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4624 && (c_parser_peek_2nd_token (parser)->keyword
4625 == RID_EXTENSION))
4626 c_parser_consume_token (parser);
4627 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4629 int ext;
4630 ext = disable_extension_diagnostics ();
4631 c_parser_consume_token (parser);
4632 parser->objc_could_be_foreach_context = true;
4633 c_parser_declaration_or_fndef (parser, true, true, true, true,
4634 true, &object_expression);
4635 parser->objc_could_be_foreach_context = false;
4637 restore_extension_diagnostics (ext);
4638 if (c_parser_next_token_is_keyword (parser, RID_IN))
4640 c_parser_consume_token (parser);
4641 is_foreach_statement = true;
4642 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4643 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4645 else
4646 check_for_loop_decls (for_loc, flag_isoc99);
4648 else
4649 goto init_expr;
4651 else
4653 init_expr:
4655 tree init_expression;
4656 parser->objc_could_be_foreach_context = true;
4657 init_expression = c_parser_expression (parser).value;
4658 parser->objc_could_be_foreach_context = false;
4659 if (c_parser_next_token_is_keyword (parser, RID_IN))
4661 c_parser_consume_token (parser);
4662 is_foreach_statement = true;
4663 if (! lvalue_p (init_expression))
4664 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4665 object_expression = c_process_expr_stmt (loc, init_expression);
4668 else
4670 c_finish_expr_stmt (loc, init_expression);
4671 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4675 /* Parse the loop condition. In the case of a foreach
4676 statement, there is no loop condition. */
4677 if (!is_foreach_statement)
4679 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4681 c_parser_consume_token (parser);
4682 cond = NULL_TREE;
4684 else
4686 cond = c_parser_condition (parser);
4687 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4690 /* Parse the increment expression (the third expression in a
4691 for-statement). In the case of a foreach-statement, this is
4692 the expression that follows the 'in'. */
4693 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4695 if (is_foreach_statement)
4697 c_parser_error (parser, "missing collection in fast enumeration");
4698 collection_expression = error_mark_node;
4700 else
4701 incr = c_process_expr_stmt (loc, NULL_TREE);
4703 else
4705 if (is_foreach_statement)
4706 collection_expression = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4707 else
4708 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4712 save_break = c_break_label;
4713 c_break_label = NULL_TREE;
4714 save_cont = c_cont_label;
4715 c_cont_label = NULL_TREE;
4716 body = c_parser_c99_block_statement (parser);
4717 if (is_foreach_statement)
4718 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4719 else
4720 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4721 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4722 c_break_label = save_break;
4723 c_cont_label = save_cont;
4726 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4727 statement with inputs, outputs, clobbers, and volatile tag
4728 allowed.
4730 asm-statement:
4731 asm type-qualifier[opt] ( asm-argument ) ;
4732 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4734 asm-argument:
4735 asm-string-literal
4736 asm-string-literal : asm-operands[opt]
4737 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4738 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4740 asm-goto-argument:
4741 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4742 : asm-goto-operands
4744 Qualifiers other than volatile are accepted in the syntax but
4745 warned for. */
4747 static tree
4748 c_parser_asm_statement (c_parser *parser)
4750 tree quals, str, outputs, inputs, clobbers, labels, ret;
4751 bool simple, is_goto;
4752 location_t asm_loc = c_parser_peek_token (parser)->location;
4753 int section, nsections;
4755 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4756 c_parser_consume_token (parser);
4757 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4759 quals = c_parser_peek_token (parser)->value;
4760 c_parser_consume_token (parser);
4762 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4763 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4765 warning_at (c_parser_peek_token (parser)->location,
4767 "%E qualifier ignored on asm",
4768 c_parser_peek_token (parser)->value);
4769 quals = NULL_TREE;
4770 c_parser_consume_token (parser);
4772 else
4773 quals = NULL_TREE;
4775 is_goto = false;
4776 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4778 c_parser_consume_token (parser);
4779 is_goto = true;
4782 /* ??? Follow the C++ parser rather than using the
4783 lex_untranslated_string kludge. */
4784 parser->lex_untranslated_string = true;
4785 ret = NULL;
4787 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4788 goto error;
4790 str = c_parser_asm_string_literal (parser);
4791 if (str == NULL_TREE)
4792 goto error_close_paren;
4794 simple = true;
4795 outputs = NULL_TREE;
4796 inputs = NULL_TREE;
4797 clobbers = NULL_TREE;
4798 labels = NULL_TREE;
4800 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4801 goto done_asm;
4803 /* Parse each colon-delimited section of operands. */
4804 nsections = 3 + is_goto;
4805 for (section = 0; section < nsections; ++section)
4807 if (!c_parser_require (parser, CPP_COLON,
4808 is_goto
4809 ? "expected %<:%>"
4810 : "expected %<:%> or %<)%>"))
4811 goto error_close_paren;
4813 /* Once past any colon, we're no longer a simple asm. */
4814 simple = false;
4816 if ((!c_parser_next_token_is (parser, CPP_COLON)
4817 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4818 || section == 3)
4819 switch (section)
4821 case 0:
4822 /* For asm goto, we don't allow output operands, but reserve
4823 the slot for a future extension that does allow them. */
4824 if (!is_goto)
4825 outputs = c_parser_asm_operands (parser, false);
4826 break;
4827 case 1:
4828 inputs = c_parser_asm_operands (parser, true);
4829 break;
4830 case 2:
4831 clobbers = c_parser_asm_clobbers (parser);
4832 break;
4833 case 3:
4834 labels = c_parser_asm_goto_operands (parser);
4835 break;
4836 default:
4837 gcc_unreachable ();
4840 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4841 goto done_asm;
4844 done_asm:
4845 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4847 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4848 goto error;
4851 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4852 c_parser_skip_to_end_of_block_or_statement (parser);
4854 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4855 clobbers, labels, simple));
4857 error:
4858 parser->lex_untranslated_string = false;
4859 return ret;
4861 error_close_paren:
4862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4863 goto error;
4866 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4867 not outputs), apply the default conversion of functions and arrays
4868 to pointers.
4870 asm-operands:
4871 asm-operand
4872 asm-operands , asm-operand
4874 asm-operand:
4875 asm-string-literal ( expression )
4876 [ identifier ] asm-string-literal ( expression )
4879 static tree
4880 c_parser_asm_operands (c_parser *parser, bool convert_p)
4882 tree list = NULL_TREE;
4883 location_t loc;
4884 while (true)
4886 tree name, str;
4887 struct c_expr expr;
4888 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4890 c_parser_consume_token (parser);
4891 if (c_parser_next_token_is (parser, CPP_NAME))
4893 tree id = c_parser_peek_token (parser)->value;
4894 c_parser_consume_token (parser);
4895 name = build_string (IDENTIFIER_LENGTH (id),
4896 IDENTIFIER_POINTER (id));
4898 else
4900 c_parser_error (parser, "expected identifier");
4901 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4902 return NULL_TREE;
4904 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4905 "expected %<]%>");
4907 else
4908 name = NULL_TREE;
4909 str = c_parser_asm_string_literal (parser);
4910 if (str == NULL_TREE)
4911 return NULL_TREE;
4912 parser->lex_untranslated_string = false;
4913 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4915 parser->lex_untranslated_string = true;
4916 return NULL_TREE;
4918 loc = c_parser_peek_token (parser)->location;
4919 expr = c_parser_expression (parser);
4920 mark_exp_read (expr.value);
4921 if (convert_p)
4922 expr = default_function_array_conversion (loc, expr);
4923 expr.value = c_fully_fold (expr.value, false, NULL);
4924 parser->lex_untranslated_string = true;
4925 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4927 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4928 return NULL_TREE;
4930 list = chainon (list, build_tree_list (build_tree_list (name, str),
4931 expr.value));
4932 if (c_parser_next_token_is (parser, CPP_COMMA))
4933 c_parser_consume_token (parser);
4934 else
4935 break;
4937 return list;
4940 /* Parse asm clobbers, a GNU extension.
4942 asm-clobbers:
4943 asm-string-literal
4944 asm-clobbers , asm-string-literal
4947 static tree
4948 c_parser_asm_clobbers (c_parser *parser)
4950 tree list = NULL_TREE;
4951 while (true)
4953 tree str = c_parser_asm_string_literal (parser);
4954 if (str)
4955 list = tree_cons (NULL_TREE, str, list);
4956 else
4957 return NULL_TREE;
4958 if (c_parser_next_token_is (parser, CPP_COMMA))
4959 c_parser_consume_token (parser);
4960 else
4961 break;
4963 return list;
4966 /* Parse asm goto labels, a GNU extension.
4968 asm-goto-operands:
4969 identifier
4970 asm-goto-operands , identifier
4973 static tree
4974 c_parser_asm_goto_operands (c_parser *parser)
4976 tree list = NULL_TREE;
4977 while (true)
4979 tree name, label;
4981 if (c_parser_next_token_is (parser, CPP_NAME))
4983 c_token *tok = c_parser_peek_token (parser);
4984 name = tok->value;
4985 label = lookup_label_for_goto (tok->location, name);
4986 c_parser_consume_token (parser);
4987 TREE_USED (label) = 1;
4989 else
4991 c_parser_error (parser, "expected identifier");
4992 return NULL_TREE;
4995 name = build_string (IDENTIFIER_LENGTH (name),
4996 IDENTIFIER_POINTER (name));
4997 list = tree_cons (name, label, list);
4998 if (c_parser_next_token_is (parser, CPP_COMMA))
4999 c_parser_consume_token (parser);
5000 else
5001 return nreverse (list);
5005 /* Parse an expression other than a compound expression; that is, an
5006 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5007 NULL then it is an Objective-C message expression which is the
5008 primary-expression starting the expression as an initializer.
5010 assignment-expression:
5011 conditional-expression
5012 unary-expression assignment-operator assignment-expression
5014 assignment-operator: one of
5015 = *= /= %= += -= <<= >>= &= ^= |=
5017 In GNU C we accept any conditional expression on the LHS and
5018 diagnose the invalid lvalue rather than producing a syntax
5019 error. */
5021 static struct c_expr
5022 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5024 struct c_expr lhs, rhs, ret;
5025 enum tree_code code;
5026 location_t op_location, exp_location;
5027 gcc_assert (!after || c_dialect_objc ());
5028 lhs = c_parser_conditional_expression (parser, after);
5029 op_location = c_parser_peek_token (parser)->location;
5030 switch (c_parser_peek_token (parser)->type)
5032 case CPP_EQ:
5033 code = NOP_EXPR;
5034 break;
5035 case CPP_MULT_EQ:
5036 code = MULT_EXPR;
5037 break;
5038 case CPP_DIV_EQ:
5039 code = TRUNC_DIV_EXPR;
5040 break;
5041 case CPP_MOD_EQ:
5042 code = TRUNC_MOD_EXPR;
5043 break;
5044 case CPP_PLUS_EQ:
5045 code = PLUS_EXPR;
5046 break;
5047 case CPP_MINUS_EQ:
5048 code = MINUS_EXPR;
5049 break;
5050 case CPP_LSHIFT_EQ:
5051 code = LSHIFT_EXPR;
5052 break;
5053 case CPP_RSHIFT_EQ:
5054 code = RSHIFT_EXPR;
5055 break;
5056 case CPP_AND_EQ:
5057 code = BIT_AND_EXPR;
5058 break;
5059 case CPP_XOR_EQ:
5060 code = BIT_XOR_EXPR;
5061 break;
5062 case CPP_OR_EQ:
5063 code = BIT_IOR_EXPR;
5064 break;
5065 default:
5066 return lhs;
5068 c_parser_consume_token (parser);
5069 exp_location = c_parser_peek_token (parser)->location;
5070 rhs = c_parser_expr_no_commas (parser, NULL);
5071 rhs = default_function_array_read_conversion (exp_location, rhs);
5072 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5073 code, exp_location, rhs.value,
5074 rhs.original_type);
5075 if (code == NOP_EXPR)
5076 ret.original_code = MODIFY_EXPR;
5077 else
5079 TREE_NO_WARNING (ret.value) = 1;
5080 ret.original_code = ERROR_MARK;
5082 ret.original_type = NULL;
5083 return ret;
5086 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5087 is not NULL then it is an Objective-C message expression which is
5088 the primary-expression starting the expression as an initializer.
5090 conditional-expression:
5091 logical-OR-expression
5092 logical-OR-expression ? expression : conditional-expression
5094 GNU extensions:
5096 conditional-expression:
5097 logical-OR-expression ? : conditional-expression
5100 static struct c_expr
5101 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5103 struct c_expr cond, exp1, exp2, ret;
5104 location_t cond_loc, colon_loc, middle_loc;
5106 gcc_assert (!after || c_dialect_objc ());
5108 cond = c_parser_binary_expression (parser, after);
5110 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5111 return cond;
5112 cond_loc = c_parser_peek_token (parser)->location;
5113 cond = default_function_array_read_conversion (cond_loc, cond);
5114 c_parser_consume_token (parser);
5115 if (c_parser_next_token_is (parser, CPP_COLON))
5117 tree eptype = NULL_TREE;
5119 middle_loc = c_parser_peek_token (parser)->location;
5120 pedwarn (middle_loc, OPT_pedantic,
5121 "ISO C forbids omitting the middle term of a ?: expression");
5122 warn_for_omitted_condop (middle_loc, cond.value);
5123 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5125 eptype = TREE_TYPE (cond.value);
5126 cond.value = TREE_OPERAND (cond.value, 0);
5128 /* Make sure first operand is calculated only once. */
5129 exp1.value = c_save_expr (default_conversion (cond.value));
5130 if (eptype)
5131 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5132 exp1.original_type = NULL;
5133 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5134 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5136 else
5138 cond.value
5139 = c_objc_common_truthvalue_conversion
5140 (cond_loc, default_conversion (cond.value));
5141 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5142 exp1 = c_parser_expression_conv (parser);
5143 mark_exp_read (exp1.value);
5144 c_inhibit_evaluation_warnings +=
5145 ((cond.value == truthvalue_true_node)
5146 - (cond.value == truthvalue_false_node));
5149 colon_loc = c_parser_peek_token (parser)->location;
5150 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5152 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5153 ret.value = error_mark_node;
5154 ret.original_code = ERROR_MARK;
5155 ret.original_type = NULL;
5156 return ret;
5159 location_t exp2_loc = c_parser_peek_token (parser)->location;
5160 exp2 = c_parser_conditional_expression (parser, NULL);
5161 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5163 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5164 ret.value = build_conditional_expr (colon_loc, cond.value,
5165 cond.original_code == C_MAYBE_CONST_EXPR,
5166 exp1.value, exp1.original_type,
5167 exp2.value, exp2.original_type);
5168 ret.original_code = ERROR_MARK;
5169 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5170 ret.original_type = NULL;
5171 else
5173 tree t1, t2;
5175 /* If both sides are enum type, the default conversion will have
5176 made the type of the result be an integer type. We want to
5177 remember the enum types we started with. */
5178 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5179 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5180 ret.original_type = ((t1 != error_mark_node
5181 && t2 != error_mark_node
5182 && (TYPE_MAIN_VARIANT (t1)
5183 == TYPE_MAIN_VARIANT (t2)))
5184 ? t1
5185 : NULL);
5187 return ret;
5190 /* Parse a binary expression; that is, a logical-OR-expression (C90
5191 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5192 an Objective-C message expression which is the primary-expression
5193 starting the expression as an initializer.
5195 multiplicative-expression:
5196 cast-expression
5197 multiplicative-expression * cast-expression
5198 multiplicative-expression / cast-expression
5199 multiplicative-expression % cast-expression
5201 additive-expression:
5202 multiplicative-expression
5203 additive-expression + multiplicative-expression
5204 additive-expression - multiplicative-expression
5206 shift-expression:
5207 additive-expression
5208 shift-expression << additive-expression
5209 shift-expression >> additive-expression
5211 relational-expression:
5212 shift-expression
5213 relational-expression < shift-expression
5214 relational-expression > shift-expression
5215 relational-expression <= shift-expression
5216 relational-expression >= shift-expression
5218 equality-expression:
5219 relational-expression
5220 equality-expression == relational-expression
5221 equality-expression != relational-expression
5223 AND-expression:
5224 equality-expression
5225 AND-expression & equality-expression
5227 exclusive-OR-expression:
5228 AND-expression
5229 exclusive-OR-expression ^ AND-expression
5231 inclusive-OR-expression:
5232 exclusive-OR-expression
5233 inclusive-OR-expression | exclusive-OR-expression
5235 logical-AND-expression:
5236 inclusive-OR-expression
5237 logical-AND-expression && inclusive-OR-expression
5239 logical-OR-expression:
5240 logical-AND-expression
5241 logical-OR-expression || logical-AND-expression
5244 static struct c_expr
5245 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5247 /* A binary expression is parsed using operator-precedence parsing,
5248 with the operands being cast expressions. All the binary
5249 operators are left-associative. Thus a binary expression is of
5250 form:
5252 E0 op1 E1 op2 E2 ...
5254 which we represent on a stack. On the stack, the precedence
5255 levels are strictly increasing. When a new operator is
5256 encountered of higher precedence than that at the top of the
5257 stack, it is pushed; its LHS is the top expression, and its RHS
5258 is everything parsed until it is popped. When a new operator is
5259 encountered with precedence less than or equal to that at the top
5260 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5261 by the result of the operation until the operator at the top of
5262 the stack has lower precedence than the new operator or there is
5263 only one element on the stack; then the top expression is the LHS
5264 of the new operator. In the case of logical AND and OR
5265 expressions, we also need to adjust c_inhibit_evaluation_warnings
5266 as appropriate when the operators are pushed and popped. */
5268 /* The precedence levels, where 0 is a dummy lowest level used for
5269 the bottom of the stack. */
5270 enum prec {
5271 PREC_NONE,
5272 PREC_LOGOR,
5273 PREC_LOGAND,
5274 PREC_BITOR,
5275 PREC_BITXOR,
5276 PREC_BITAND,
5277 PREC_EQ,
5278 PREC_REL,
5279 PREC_SHIFT,
5280 PREC_ADD,
5281 PREC_MULT,
5282 NUM_PRECS
5284 struct {
5285 /* The expression at this stack level. */
5286 struct c_expr expr;
5287 /* The precedence of the operator on its left, PREC_NONE at the
5288 bottom of the stack. */
5289 enum prec prec;
5290 /* The operation on its left. */
5291 enum tree_code op;
5292 /* The source location of this operation. */
5293 location_t loc;
5294 } stack[NUM_PRECS];
5295 int sp;
5296 /* Location of the binary operator. */
5297 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5298 #define POP \
5299 do { \
5300 switch (stack[sp].op) \
5302 case TRUTH_ANDIF_EXPR: \
5303 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5304 == truthvalue_false_node); \
5305 break; \
5306 case TRUTH_ORIF_EXPR: \
5307 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5308 == truthvalue_true_node); \
5309 break; \
5310 default: \
5311 break; \
5313 stack[sp - 1].expr \
5314 = default_function_array_read_conversion (stack[sp - 1].loc, \
5315 stack[sp - 1].expr); \
5316 stack[sp].expr \
5317 = default_function_array_read_conversion (stack[sp].loc, \
5318 stack[sp].expr); \
5319 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5320 stack[sp].op, \
5321 stack[sp - 1].expr, \
5322 stack[sp].expr); \
5323 sp--; \
5324 } while (0)
5325 gcc_assert (!after || c_dialect_objc ());
5326 stack[0].loc = c_parser_peek_token (parser)->location;
5327 stack[0].expr = c_parser_cast_expression (parser, after);
5328 stack[0].prec = PREC_NONE;
5329 sp = 0;
5330 while (true)
5332 enum prec oprec;
5333 enum tree_code ocode;
5334 if (parser->error)
5335 goto out;
5336 switch (c_parser_peek_token (parser)->type)
5338 case CPP_MULT:
5339 oprec = PREC_MULT;
5340 ocode = MULT_EXPR;
5341 break;
5342 case CPP_DIV:
5343 oprec = PREC_MULT;
5344 ocode = TRUNC_DIV_EXPR;
5345 break;
5346 case CPP_MOD:
5347 oprec = PREC_MULT;
5348 ocode = TRUNC_MOD_EXPR;
5349 break;
5350 case CPP_PLUS:
5351 oprec = PREC_ADD;
5352 ocode = PLUS_EXPR;
5353 break;
5354 case CPP_MINUS:
5355 oprec = PREC_ADD;
5356 ocode = MINUS_EXPR;
5357 break;
5358 case CPP_LSHIFT:
5359 oprec = PREC_SHIFT;
5360 ocode = LSHIFT_EXPR;
5361 break;
5362 case CPP_RSHIFT:
5363 oprec = PREC_SHIFT;
5364 ocode = RSHIFT_EXPR;
5365 break;
5366 case CPP_LESS:
5367 oprec = PREC_REL;
5368 ocode = LT_EXPR;
5369 break;
5370 case CPP_GREATER:
5371 oprec = PREC_REL;
5372 ocode = GT_EXPR;
5373 break;
5374 case CPP_LESS_EQ:
5375 oprec = PREC_REL;
5376 ocode = LE_EXPR;
5377 break;
5378 case CPP_GREATER_EQ:
5379 oprec = PREC_REL;
5380 ocode = GE_EXPR;
5381 break;
5382 case CPP_EQ_EQ:
5383 oprec = PREC_EQ;
5384 ocode = EQ_EXPR;
5385 break;
5386 case CPP_NOT_EQ:
5387 oprec = PREC_EQ;
5388 ocode = NE_EXPR;
5389 break;
5390 case CPP_AND:
5391 oprec = PREC_BITAND;
5392 ocode = BIT_AND_EXPR;
5393 break;
5394 case CPP_XOR:
5395 oprec = PREC_BITXOR;
5396 ocode = BIT_XOR_EXPR;
5397 break;
5398 case CPP_OR:
5399 oprec = PREC_BITOR;
5400 ocode = BIT_IOR_EXPR;
5401 break;
5402 case CPP_AND_AND:
5403 oprec = PREC_LOGAND;
5404 ocode = TRUTH_ANDIF_EXPR;
5405 break;
5406 case CPP_OR_OR:
5407 oprec = PREC_LOGOR;
5408 ocode = TRUTH_ORIF_EXPR;
5409 break;
5410 default:
5411 /* Not a binary operator, so end of the binary
5412 expression. */
5413 goto out;
5415 binary_loc = c_parser_peek_token (parser)->location;
5416 c_parser_consume_token (parser);
5417 while (oprec <= stack[sp].prec)
5418 POP;
5419 switch (ocode)
5421 case TRUTH_ANDIF_EXPR:
5422 stack[sp].expr
5423 = default_function_array_read_conversion (stack[sp].loc,
5424 stack[sp].expr);
5425 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5426 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5427 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5428 == truthvalue_false_node);
5429 break;
5430 case TRUTH_ORIF_EXPR:
5431 stack[sp].expr
5432 = default_function_array_read_conversion (stack[sp].loc,
5433 stack[sp].expr);
5434 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5435 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5436 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5437 == truthvalue_true_node);
5438 break;
5439 default:
5440 break;
5442 sp++;
5443 stack[sp].loc = binary_loc;
5444 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5445 stack[sp].prec = oprec;
5446 stack[sp].op = ocode;
5447 stack[sp].loc = binary_loc;
5449 out:
5450 while (sp > 0)
5451 POP;
5452 return stack[0].expr;
5453 #undef POP
5456 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5457 NULL then it is an Objective-C message expression which is the
5458 primary-expression starting the expression as an initializer.
5460 cast-expression:
5461 unary-expression
5462 ( type-name ) unary-expression
5465 static struct c_expr
5466 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5468 location_t cast_loc = c_parser_peek_token (parser)->location;
5469 gcc_assert (!after || c_dialect_objc ());
5470 if (after)
5471 return c_parser_postfix_expression_after_primary (parser,
5472 cast_loc, *after);
5473 /* If the expression begins with a parenthesized type name, it may
5474 be either a cast or a compound literal; we need to see whether
5475 the next character is '{' to tell the difference. If not, it is
5476 an unary expression. */
5477 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5478 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5480 struct c_type_name *type_name;
5481 struct c_expr ret;
5482 struct c_expr expr;
5483 c_parser_consume_token (parser);
5484 type_name = c_parser_type_name (parser);
5485 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5486 if (type_name == NULL)
5488 ret.value = error_mark_node;
5489 ret.original_code = ERROR_MARK;
5490 ret.original_type = NULL;
5491 return ret;
5494 /* Save casted types in the function's used types hash table. */
5495 used_types_insert (type_name->specs->type);
5497 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5498 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5499 cast_loc);
5501 location_t expr_loc = c_parser_peek_token (parser)->location;
5502 expr = c_parser_cast_expression (parser, NULL);
5503 expr = default_function_array_read_conversion (expr_loc, expr);
5505 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5506 ret.original_code = ERROR_MARK;
5507 ret.original_type = NULL;
5508 return ret;
5510 else
5511 return c_parser_unary_expression (parser);
5514 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5516 unary-expression:
5517 postfix-expression
5518 ++ unary-expression
5519 -- unary-expression
5520 unary-operator cast-expression
5521 sizeof unary-expression
5522 sizeof ( type-name )
5524 unary-operator: one of
5525 & * + - ~ !
5527 GNU extensions:
5529 unary-expression:
5530 __alignof__ unary-expression
5531 __alignof__ ( type-name )
5532 && identifier
5534 unary-operator: one of
5535 __extension__ __real__ __imag__
5537 In addition, the GNU syntax treats ++ and -- as unary operators, so
5538 they may be applied to cast expressions with errors for non-lvalues
5539 given later. */
5541 static struct c_expr
5542 c_parser_unary_expression (c_parser *parser)
5544 int ext;
5545 struct c_expr ret, op;
5546 location_t op_loc = c_parser_peek_token (parser)->location;
5547 location_t exp_loc;
5548 ret.original_code = ERROR_MARK;
5549 ret.original_type = NULL;
5550 switch (c_parser_peek_token (parser)->type)
5552 case CPP_PLUS_PLUS:
5553 c_parser_consume_token (parser);
5554 exp_loc = c_parser_peek_token (parser)->location;
5555 op = c_parser_cast_expression (parser, NULL);
5556 op = default_function_array_read_conversion (exp_loc, op);
5557 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5558 case CPP_MINUS_MINUS:
5559 c_parser_consume_token (parser);
5560 exp_loc = c_parser_peek_token (parser)->location;
5561 op = c_parser_cast_expression (parser, NULL);
5562 op = default_function_array_read_conversion (exp_loc, op);
5563 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5564 case CPP_AND:
5565 c_parser_consume_token (parser);
5566 op = c_parser_cast_expression (parser, NULL);
5567 mark_exp_read (op.value);
5568 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5569 case CPP_MULT:
5570 c_parser_consume_token (parser);
5571 exp_loc = c_parser_peek_token (parser)->location;
5572 op = c_parser_cast_expression (parser, NULL);
5573 op = default_function_array_read_conversion (exp_loc, op);
5574 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5575 return ret;
5576 case CPP_PLUS:
5577 if (!c_dialect_objc () && !in_system_header)
5578 warning_at (op_loc,
5579 OPT_Wtraditional,
5580 "traditional C rejects the unary plus operator");
5581 c_parser_consume_token (parser);
5582 exp_loc = c_parser_peek_token (parser)->location;
5583 op = c_parser_cast_expression (parser, NULL);
5584 op = default_function_array_read_conversion (exp_loc, op);
5585 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5586 case CPP_MINUS:
5587 c_parser_consume_token (parser);
5588 exp_loc = c_parser_peek_token (parser)->location;
5589 op = c_parser_cast_expression (parser, NULL);
5590 op = default_function_array_read_conversion (exp_loc, op);
5591 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5592 case CPP_COMPL:
5593 c_parser_consume_token (parser);
5594 exp_loc = c_parser_peek_token (parser)->location;
5595 op = c_parser_cast_expression (parser, NULL);
5596 op = default_function_array_read_conversion (exp_loc, op);
5597 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5598 case CPP_NOT:
5599 c_parser_consume_token (parser);
5600 exp_loc = c_parser_peek_token (parser)->location;
5601 op = c_parser_cast_expression (parser, NULL);
5602 op = default_function_array_read_conversion (exp_loc, op);
5603 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5604 case CPP_AND_AND:
5605 /* Refer to the address of a label as a pointer. */
5606 c_parser_consume_token (parser);
5607 if (c_parser_next_token_is (parser, CPP_NAME))
5609 ret.value = finish_label_address_expr
5610 (c_parser_peek_token (parser)->value, op_loc);
5611 c_parser_consume_token (parser);
5613 else
5615 c_parser_error (parser, "expected identifier");
5616 ret.value = error_mark_node;
5618 return ret;
5619 case CPP_KEYWORD:
5620 switch (c_parser_peek_token (parser)->keyword)
5622 case RID_SIZEOF:
5623 return c_parser_sizeof_expression (parser);
5624 case RID_ALIGNOF:
5625 return c_parser_alignof_expression (parser);
5626 case RID_EXTENSION:
5627 c_parser_consume_token (parser);
5628 ext = disable_extension_diagnostics ();
5629 ret = c_parser_cast_expression (parser, NULL);
5630 restore_extension_diagnostics (ext);
5631 return ret;
5632 case RID_REALPART:
5633 c_parser_consume_token (parser);
5634 exp_loc = c_parser_peek_token (parser)->location;
5635 op = c_parser_cast_expression (parser, NULL);
5636 op = default_function_array_conversion (exp_loc, op);
5637 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5638 case RID_IMAGPART:
5639 c_parser_consume_token (parser);
5640 exp_loc = c_parser_peek_token (parser)->location;
5641 op = c_parser_cast_expression (parser, NULL);
5642 op = default_function_array_conversion (exp_loc, op);
5643 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5644 default:
5645 return c_parser_postfix_expression (parser);
5647 default:
5648 return c_parser_postfix_expression (parser);
5652 /* Parse a sizeof expression. */
5654 static struct c_expr
5655 c_parser_sizeof_expression (c_parser *parser)
5657 struct c_expr expr;
5658 location_t expr_loc;
5659 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5660 c_parser_consume_token (parser);
5661 c_inhibit_evaluation_warnings++;
5662 in_sizeof++;
5663 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5664 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5666 /* Either sizeof ( type-name ) or sizeof unary-expression
5667 starting with a compound literal. */
5668 struct c_type_name *type_name;
5669 c_parser_consume_token (parser);
5670 expr_loc = c_parser_peek_token (parser)->location;
5671 type_name = c_parser_type_name (parser);
5672 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5673 if (type_name == NULL)
5675 struct c_expr ret;
5676 c_inhibit_evaluation_warnings--;
5677 in_sizeof--;
5678 ret.value = error_mark_node;
5679 ret.original_code = ERROR_MARK;
5680 ret.original_type = NULL;
5681 return ret;
5683 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5685 expr = c_parser_postfix_expression_after_paren_type (parser,
5686 type_name,
5687 expr_loc);
5688 goto sizeof_expr;
5690 /* sizeof ( type-name ). */
5691 c_inhibit_evaluation_warnings--;
5692 in_sizeof--;
5693 return c_expr_sizeof_type (expr_loc, type_name);
5695 else
5697 expr_loc = c_parser_peek_token (parser)->location;
5698 expr = c_parser_unary_expression (parser);
5699 sizeof_expr:
5700 c_inhibit_evaluation_warnings--;
5701 in_sizeof--;
5702 mark_exp_read (expr.value);
5703 if (TREE_CODE (expr.value) == COMPONENT_REF
5704 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5705 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5706 return c_expr_sizeof_expr (expr_loc, expr);
5710 /* Parse an alignof expression. */
5712 static struct c_expr
5713 c_parser_alignof_expression (c_parser *parser)
5715 struct c_expr expr;
5716 location_t loc = c_parser_peek_token (parser)->location;
5717 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5718 c_parser_consume_token (parser);
5719 c_inhibit_evaluation_warnings++;
5720 in_alignof++;
5721 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5722 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5724 /* Either __alignof__ ( type-name ) or __alignof__
5725 unary-expression starting with a compound literal. */
5726 location_t loc;
5727 struct c_type_name *type_name;
5728 struct c_expr ret;
5729 c_parser_consume_token (parser);
5730 loc = c_parser_peek_token (parser)->location;
5731 type_name = c_parser_type_name (parser);
5732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5733 if (type_name == NULL)
5735 struct c_expr ret;
5736 c_inhibit_evaluation_warnings--;
5737 in_alignof--;
5738 ret.value = error_mark_node;
5739 ret.original_code = ERROR_MARK;
5740 ret.original_type = NULL;
5741 return ret;
5743 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5745 expr = c_parser_postfix_expression_after_paren_type (parser,
5746 type_name,
5747 loc);
5748 goto alignof_expr;
5750 /* alignof ( type-name ). */
5751 c_inhibit_evaluation_warnings--;
5752 in_alignof--;
5753 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5754 ret.original_code = ERROR_MARK;
5755 ret.original_type = NULL;
5756 return ret;
5758 else
5760 struct c_expr ret;
5761 expr = c_parser_unary_expression (parser);
5762 alignof_expr:
5763 mark_exp_read (expr.value);
5764 c_inhibit_evaluation_warnings--;
5765 in_alignof--;
5766 ret.value = c_alignof_expr (loc, expr.value);
5767 ret.original_code = ERROR_MARK;
5768 ret.original_type = NULL;
5769 return ret;
5773 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5775 postfix-expression:
5776 primary-expression
5777 postfix-expression [ expression ]
5778 postfix-expression ( argument-expression-list[opt] )
5779 postfix-expression . identifier
5780 postfix-expression -> identifier
5781 postfix-expression ++
5782 postfix-expression --
5783 ( type-name ) { initializer-list }
5784 ( type-name ) { initializer-list , }
5786 argument-expression-list:
5787 argument-expression
5788 argument-expression-list , argument-expression
5790 primary-expression:
5791 identifier
5792 constant
5793 string-literal
5794 ( expression )
5796 GNU extensions:
5798 primary-expression:
5799 __func__
5800 (treated as a keyword in GNU C)
5801 __FUNCTION__
5802 __PRETTY_FUNCTION__
5803 ( compound-statement )
5804 __builtin_va_arg ( assignment-expression , type-name )
5805 __builtin_offsetof ( type-name , offsetof-member-designator )
5806 __builtin_choose_expr ( assignment-expression ,
5807 assignment-expression ,
5808 assignment-expression )
5809 __builtin_types_compatible_p ( type-name , type-name )
5811 offsetof-member-designator:
5812 identifier
5813 offsetof-member-designator . identifier
5814 offsetof-member-designator [ expression ]
5816 Objective-C:
5818 primary-expression:
5819 [ objc-receiver objc-message-args ]
5820 @selector ( objc-selector-arg )
5821 @protocol ( identifier )
5822 @encode ( type-name )
5823 objc-string-literal
5826 static struct c_expr
5827 c_parser_postfix_expression (c_parser *parser)
5829 struct c_expr expr, e1, e2, e3;
5830 struct c_type_name *t1, *t2;
5831 location_t loc = c_parser_peek_token (parser)->location;;
5832 expr.original_code = ERROR_MARK;
5833 expr.original_type = NULL;
5834 switch (c_parser_peek_token (parser)->type)
5836 case CPP_NUMBER:
5837 expr.value = c_parser_peek_token (parser)->value;
5838 loc = c_parser_peek_token (parser)->location;
5839 c_parser_consume_token (parser);
5840 if (TREE_CODE (expr.value) == FIXED_CST
5841 && !targetm.fixed_point_supported_p ())
5843 error_at (loc, "fixed-point types not supported for this target");
5844 expr.value = error_mark_node;
5846 break;
5847 case CPP_CHAR:
5848 case CPP_CHAR16:
5849 case CPP_CHAR32:
5850 case CPP_WCHAR:
5851 expr.value = c_parser_peek_token (parser)->value;
5852 c_parser_consume_token (parser);
5853 break;
5854 case CPP_STRING:
5855 case CPP_STRING16:
5856 case CPP_STRING32:
5857 case CPP_WSTRING:
5858 case CPP_UTF8STRING:
5859 expr.value = c_parser_peek_token (parser)->value;
5860 expr.original_code = STRING_CST;
5861 c_parser_consume_token (parser);
5862 break;
5863 case CPP_OBJC_STRING:
5864 gcc_assert (c_dialect_objc ());
5865 expr.value
5866 = objc_build_string_object (c_parser_peek_token (parser)->value);
5867 c_parser_consume_token (parser);
5868 break;
5869 case CPP_NAME:
5870 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5872 c_parser_error (parser, "expected expression");
5873 expr.value = error_mark_node;
5874 break;
5877 tree id = c_parser_peek_token (parser)->value;
5878 c_parser_consume_token (parser);
5879 expr.value = build_external_ref (loc, id,
5880 (c_parser_peek_token (parser)->type
5881 == CPP_OPEN_PAREN),
5882 &expr.original_type);
5884 break;
5885 case CPP_OPEN_PAREN:
5886 /* A parenthesized expression, statement expression or compound
5887 literal. */
5888 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5890 /* A statement expression. */
5891 tree stmt;
5892 location_t brace_loc;
5893 c_parser_consume_token (parser);
5894 brace_loc = c_parser_peek_token (parser)->location;
5895 c_parser_consume_token (parser);
5896 if (cur_stmt_list == NULL)
5898 error_at (loc, "braced-group within expression allowed "
5899 "only inside a function");
5900 parser->error = true;
5901 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5903 expr.value = error_mark_node;
5904 break;
5906 stmt = c_begin_stmt_expr ();
5907 c_parser_compound_statement_nostart (parser);
5908 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5909 "expected %<)%>");
5910 pedwarn (loc, OPT_pedantic,
5911 "ISO C forbids braced-groups within expressions");
5912 expr.value = c_finish_stmt_expr (brace_loc, stmt);
5913 mark_exp_read (expr.value);
5915 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5917 /* A compound literal. ??? Can we actually get here rather
5918 than going directly to
5919 c_parser_postfix_expression_after_paren_type from
5920 elsewhere? */
5921 location_t loc;
5922 struct c_type_name *type_name;
5923 c_parser_consume_token (parser);
5924 loc = c_parser_peek_token (parser)->location;
5925 type_name = c_parser_type_name (parser);
5926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5927 "expected %<)%>");
5928 if (type_name == NULL)
5930 expr.value = error_mark_node;
5932 else
5933 expr = c_parser_postfix_expression_after_paren_type (parser,
5934 type_name,
5935 loc);
5937 else
5939 /* A parenthesized expression. */
5940 c_parser_consume_token (parser);
5941 expr = c_parser_expression (parser);
5942 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5943 TREE_NO_WARNING (expr.value) = 1;
5944 if (expr.original_code != C_MAYBE_CONST_EXPR)
5945 expr.original_code = ERROR_MARK;
5946 /* Don't change EXPR.ORIGINAL_TYPE. */
5947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5948 "expected %<)%>");
5950 break;
5951 case CPP_KEYWORD:
5952 switch (c_parser_peek_token (parser)->keyword)
5954 case RID_FUNCTION_NAME:
5955 case RID_PRETTY_FUNCTION_NAME:
5956 case RID_C99_FUNCTION_NAME:
5957 expr.value = fname_decl (loc,
5958 c_parser_peek_token (parser)->keyword,
5959 c_parser_peek_token (parser)->value);
5960 c_parser_consume_token (parser);
5961 break;
5962 case RID_VA_ARG:
5963 c_parser_consume_token (parser);
5964 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5966 expr.value = error_mark_node;
5967 break;
5969 e1 = c_parser_expr_no_commas (parser, NULL);
5970 mark_exp_read (e1.value);
5971 e1.value = c_fully_fold (e1.value, false, NULL);
5972 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5974 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5975 expr.value = error_mark_node;
5976 break;
5978 loc = c_parser_peek_token (parser)->location;
5979 t1 = c_parser_type_name (parser);
5980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5981 "expected %<)%>");
5982 if (t1 == NULL)
5984 expr.value = error_mark_node;
5986 else
5988 tree type_expr = NULL_TREE;
5989 expr.value = c_build_va_arg (loc, e1.value,
5990 groktypename (t1, &type_expr, NULL));
5991 if (type_expr)
5993 expr.value = build2 (C_MAYBE_CONST_EXPR,
5994 TREE_TYPE (expr.value), type_expr,
5995 expr.value);
5996 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5999 break;
6000 case RID_OFFSETOF:
6001 c_parser_consume_token (parser);
6002 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6004 expr.value = error_mark_node;
6005 break;
6007 t1 = c_parser_type_name (parser);
6008 if (t1 == NULL)
6010 expr.value = error_mark_node;
6011 break;
6013 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6016 expr.value = error_mark_node;
6017 break;
6020 tree type = groktypename (t1, NULL, NULL);
6021 tree offsetof_ref;
6022 if (type == error_mark_node)
6023 offsetof_ref = error_mark_node;
6024 else
6026 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6027 SET_EXPR_LOCATION (offsetof_ref, loc);
6029 /* Parse the second argument to __builtin_offsetof. We
6030 must have one identifier, and beyond that we want to
6031 accept sub structure and sub array references. */
6032 if (c_parser_next_token_is (parser, CPP_NAME))
6034 offsetof_ref = build_component_ref
6035 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6036 c_parser_consume_token (parser);
6037 while (c_parser_next_token_is (parser, CPP_DOT)
6038 || c_parser_next_token_is (parser,
6039 CPP_OPEN_SQUARE)
6040 || c_parser_next_token_is (parser,
6041 CPP_DEREF))
6043 if (c_parser_next_token_is (parser, CPP_DEREF))
6045 loc = c_parser_peek_token (parser)->location;
6046 offsetof_ref = build_array_ref (loc,
6047 offsetof_ref,
6048 integer_zero_node);
6049 goto do_dot;
6051 else if (c_parser_next_token_is (parser, CPP_DOT))
6053 do_dot:
6054 c_parser_consume_token (parser);
6055 if (c_parser_next_token_is_not (parser,
6056 CPP_NAME))
6058 c_parser_error (parser, "expected identifier");
6059 break;
6061 offsetof_ref = build_component_ref
6062 (loc, offsetof_ref,
6063 c_parser_peek_token (parser)->value);
6064 c_parser_consume_token (parser);
6066 else
6068 tree idx;
6069 loc = c_parser_peek_token (parser)->location;
6070 c_parser_consume_token (parser);
6071 idx = c_parser_expression (parser).value;
6072 idx = c_fully_fold (idx, false, NULL);
6073 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6074 "expected %<]%>");
6075 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6079 else
6080 c_parser_error (parser, "expected identifier");
6081 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6082 "expected %<)%>");
6083 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6085 break;
6086 case RID_CHOOSE_EXPR:
6087 c_parser_consume_token (parser);
6088 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6090 expr.value = error_mark_node;
6091 break;
6093 loc = c_parser_peek_token (parser)->location;
6094 e1 = c_parser_expr_no_commas (parser, NULL);
6095 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6098 expr.value = error_mark_node;
6099 break;
6101 e2 = c_parser_expr_no_commas (parser, NULL);
6102 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6105 expr.value = error_mark_node;
6106 break;
6108 e3 = c_parser_expr_no_commas (parser, NULL);
6109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6110 "expected %<)%>");
6112 tree c;
6114 c = e1.value;
6115 mark_exp_read (e2.value);
6116 mark_exp_read (e3.value);
6117 if (TREE_CODE (c) != INTEGER_CST
6118 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6119 error_at (loc,
6120 "first argument to %<__builtin_choose_expr%> not"
6121 " a constant");
6122 constant_expression_warning (c);
6123 expr = integer_zerop (c) ? e3 : e2;
6125 break;
6126 case RID_TYPES_COMPATIBLE_P:
6127 c_parser_consume_token (parser);
6128 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6130 expr.value = error_mark_node;
6131 break;
6133 t1 = c_parser_type_name (parser);
6134 if (t1 == NULL)
6136 expr.value = error_mark_node;
6137 break;
6139 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6142 expr.value = error_mark_node;
6143 break;
6145 t2 = c_parser_type_name (parser);
6146 if (t2 == NULL)
6148 expr.value = error_mark_node;
6149 break;
6151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6152 "expected %<)%>");
6154 tree e1, e2;
6156 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6157 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6159 expr.value
6160 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6162 break;
6163 case RID_AT_SELECTOR:
6164 gcc_assert (c_dialect_objc ());
6165 c_parser_consume_token (parser);
6166 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6168 expr.value = error_mark_node;
6169 break;
6172 tree sel = c_parser_objc_selector_arg (parser);
6173 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6174 "expected %<)%>");
6175 expr.value = objc_build_selector_expr (loc, sel);
6177 break;
6178 case RID_AT_PROTOCOL:
6179 gcc_assert (c_dialect_objc ());
6180 c_parser_consume_token (parser);
6181 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6183 expr.value = error_mark_node;
6184 break;
6186 if (c_parser_next_token_is_not (parser, CPP_NAME))
6188 c_parser_error (parser, "expected identifier");
6189 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6190 expr.value = error_mark_node;
6191 break;
6194 tree id = c_parser_peek_token (parser)->value;
6195 c_parser_consume_token (parser);
6196 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6197 "expected %<)%>");
6198 expr.value = objc_build_protocol_expr (id);
6200 break;
6201 case RID_AT_ENCODE:
6202 /* Extension to support C-structures in the archiver. */
6203 gcc_assert (c_dialect_objc ());
6204 c_parser_consume_token (parser);
6205 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6207 expr.value = error_mark_node;
6208 break;
6210 t1 = c_parser_type_name (parser);
6211 if (t1 == NULL)
6213 expr.value = error_mark_node;
6214 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6215 break;
6217 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6218 "expected %<)%>");
6220 tree type = groktypename (t1, NULL, NULL);
6221 expr.value = objc_build_encode_expr (type);
6223 break;
6224 default:
6225 c_parser_error (parser, "expected expression");
6226 expr.value = error_mark_node;
6227 break;
6229 break;
6230 case CPP_OPEN_SQUARE:
6231 if (c_dialect_objc ())
6233 tree receiver, args;
6234 c_parser_consume_token (parser);
6235 receiver = c_parser_objc_receiver (parser);
6236 args = c_parser_objc_message_args (parser);
6237 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6238 "expected %<]%>");
6239 expr.value = objc_build_message_expr (build_tree_list (receiver,
6240 args));
6241 break;
6243 /* Else fall through to report error. */
6244 default:
6245 c_parser_error (parser, "expected expression");
6246 expr.value = error_mark_node;
6247 break;
6249 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6252 /* Parse a postfix expression after a parenthesized type name: the
6253 brace-enclosed initializer of a compound literal, possibly followed
6254 by some postfix operators. This is separate because it is not
6255 possible to tell until after the type name whether a cast
6256 expression has a cast or a compound literal, or whether the operand
6257 of sizeof is a parenthesized type name or starts with a compound
6258 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6259 location of the first token after the parentheses around the type
6260 name. */
6262 static struct c_expr
6263 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6264 struct c_type_name *type_name,
6265 location_t type_loc)
6267 tree type;
6268 struct c_expr init;
6269 bool non_const;
6270 struct c_expr expr;
6271 location_t start_loc;
6272 tree type_expr = NULL_TREE;
6273 bool type_expr_const = true;
6274 check_compound_literal_type (type_loc, type_name);
6275 start_init (NULL_TREE, NULL, 0);
6276 type = groktypename (type_name, &type_expr, &type_expr_const);
6277 start_loc = c_parser_peek_token (parser)->location;
6278 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6280 error_at (type_loc, "compound literal has variable size");
6281 type = error_mark_node;
6283 init = c_parser_braced_init (parser, type, false);
6284 finish_init ();
6285 maybe_warn_string_init (type, init);
6287 if (type != error_mark_node
6288 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6289 && current_function_decl)
6291 error ("compound literal qualified by address-space qualifier");
6292 type = error_mark_node;
6295 if (!flag_isoc99)
6296 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6297 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6298 ? CONSTRUCTOR_NON_CONST (init.value)
6299 : init.original_code == C_MAYBE_CONST_EXPR);
6300 non_const |= !type_expr_const;
6301 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6302 expr.original_code = ERROR_MARK;
6303 expr.original_type = NULL;
6304 if (type_expr)
6306 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6308 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6309 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6311 else
6313 gcc_assert (!non_const);
6314 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6315 type_expr, expr.value);
6318 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6321 /* Parse a postfix expression after the initial primary or compound
6322 literal; that is, parse a series of postfix operators.
6324 EXPR_LOC is the location of the primary expression. */
6326 static struct c_expr
6327 c_parser_postfix_expression_after_primary (c_parser *parser,
6328 location_t expr_loc,
6329 struct c_expr expr)
6331 struct c_expr orig_expr;
6332 tree ident, idx;
6333 VEC(tree,gc) *exprlist;
6334 VEC(tree,gc) *origtypes;
6335 while (true)
6337 location_t op_loc = c_parser_peek_token (parser)->location;
6338 switch (c_parser_peek_token (parser)->type)
6340 case CPP_OPEN_SQUARE:
6341 /* Array reference. */
6342 c_parser_consume_token (parser);
6343 idx = c_parser_expression (parser).value;
6344 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6345 "expected %<]%>");
6346 expr.value = build_array_ref (op_loc, expr.value, idx);
6347 expr.original_code = ERROR_MARK;
6348 expr.original_type = NULL;
6349 break;
6350 case CPP_OPEN_PAREN:
6351 /* Function call. */
6352 c_parser_consume_token (parser);
6353 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6354 exprlist = NULL;
6355 else
6356 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6357 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6358 "expected %<)%>");
6359 orig_expr = expr;
6360 mark_exp_read (expr.value);
6361 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6362 "(" after the FUNCNAME, which is what we have now. */
6363 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6364 origtypes);
6365 expr.original_code = ERROR_MARK;
6366 if (TREE_CODE (expr.value) == INTEGER_CST
6367 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6368 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6369 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6370 expr.original_code = C_MAYBE_CONST_EXPR;
6371 expr.original_type = NULL;
6372 if (exprlist != NULL)
6374 release_tree_vector (exprlist);
6375 release_tree_vector (origtypes);
6377 break;
6378 case CPP_DOT:
6379 /* Structure element reference. */
6380 c_parser_consume_token (parser);
6381 expr = default_function_array_conversion (expr_loc, expr);
6382 if (c_parser_next_token_is (parser, CPP_NAME))
6383 ident = c_parser_peek_token (parser)->value;
6384 else
6386 c_parser_error (parser, "expected identifier");
6387 expr.value = error_mark_node;
6388 expr.original_code = ERROR_MARK;
6389 expr.original_type = NULL;
6390 return expr;
6392 c_parser_consume_token (parser);
6393 expr.value = build_component_ref (op_loc, expr.value, ident);
6394 expr.original_code = ERROR_MARK;
6395 if (TREE_CODE (expr.value) != COMPONENT_REF)
6396 expr.original_type = NULL;
6397 else
6399 /* Remember the original type of a bitfield. */
6400 tree field = TREE_OPERAND (expr.value, 1);
6401 if (TREE_CODE (field) != FIELD_DECL)
6402 expr.original_type = NULL;
6403 else
6404 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6406 break;
6407 case CPP_DEREF:
6408 /* Structure element reference. */
6409 c_parser_consume_token (parser);
6410 expr = default_function_array_conversion (expr_loc, expr);
6411 if (c_parser_next_token_is (parser, CPP_NAME))
6412 ident = c_parser_peek_token (parser)->value;
6413 else
6415 c_parser_error (parser, "expected identifier");
6416 expr.value = error_mark_node;
6417 expr.original_code = ERROR_MARK;
6418 expr.original_type = NULL;
6419 return expr;
6421 c_parser_consume_token (parser);
6422 expr.value = build_component_ref (op_loc,
6423 build_indirect_ref (op_loc,
6424 expr.value,
6425 RO_ARROW),
6426 ident);
6427 expr.original_code = ERROR_MARK;
6428 if (TREE_CODE (expr.value) != COMPONENT_REF)
6429 expr.original_type = NULL;
6430 else
6432 /* Remember the original type of a bitfield. */
6433 tree field = TREE_OPERAND (expr.value, 1);
6434 if (TREE_CODE (field) != FIELD_DECL)
6435 expr.original_type = NULL;
6436 else
6437 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6439 break;
6440 case CPP_PLUS_PLUS:
6441 /* Postincrement. */
6442 c_parser_consume_token (parser);
6443 expr = default_function_array_read_conversion (expr_loc, expr);
6444 expr.value = build_unary_op (op_loc,
6445 POSTINCREMENT_EXPR, expr.value, 0);
6446 expr.original_code = ERROR_MARK;
6447 expr.original_type = NULL;
6448 break;
6449 case CPP_MINUS_MINUS:
6450 /* Postdecrement. */
6451 c_parser_consume_token (parser);
6452 expr = default_function_array_read_conversion (expr_loc, expr);
6453 expr.value = build_unary_op (op_loc,
6454 POSTDECREMENT_EXPR, expr.value, 0);
6455 expr.original_code = ERROR_MARK;
6456 expr.original_type = NULL;
6457 break;
6458 default:
6459 return expr;
6464 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6466 expression:
6467 assignment-expression
6468 expression , assignment-expression
6471 static struct c_expr
6472 c_parser_expression (c_parser *parser)
6474 struct c_expr expr;
6475 expr = c_parser_expr_no_commas (parser, NULL);
6476 while (c_parser_next_token_is (parser, CPP_COMMA))
6478 struct c_expr next;
6479 tree lhsval;
6480 location_t loc = c_parser_peek_token (parser)->location;
6481 location_t expr_loc;
6482 c_parser_consume_token (parser);
6483 expr_loc = c_parser_peek_token (parser)->location;
6484 lhsval = expr.value;
6485 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6486 lhsval = TREE_OPERAND (lhsval, 1);
6487 if (DECL_P (lhsval) || handled_component_p (lhsval))
6488 mark_exp_read (lhsval);
6489 next = c_parser_expr_no_commas (parser, NULL);
6490 next = default_function_array_conversion (expr_loc, next);
6491 expr.value = build_compound_expr (loc, expr.value, next.value);
6492 expr.original_code = COMPOUND_EXPR;
6493 expr.original_type = next.original_type;
6495 return expr;
6498 /* Parse an expression and convert functions or arrays to
6499 pointers. */
6501 static struct c_expr
6502 c_parser_expression_conv (c_parser *parser)
6504 struct c_expr expr;
6505 location_t loc = c_parser_peek_token (parser)->location;
6506 expr = c_parser_expression (parser);
6507 expr = default_function_array_conversion (loc, expr);
6508 return expr;
6511 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6512 functions and arrays to pointers. If FOLD_P, fold the expressions.
6514 nonempty-expr-list:
6515 assignment-expression
6516 nonempty-expr-list , assignment-expression
6519 static VEC(tree,gc) *
6520 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6521 VEC(tree,gc) **p_orig_types)
6523 VEC(tree,gc) *ret;
6524 VEC(tree,gc) *orig_types;
6525 struct c_expr expr;
6526 location_t loc = c_parser_peek_token (parser)->location;
6528 ret = make_tree_vector ();
6529 if (p_orig_types == NULL)
6530 orig_types = NULL;
6531 else
6532 orig_types = make_tree_vector ();
6534 expr = c_parser_expr_no_commas (parser, NULL);
6535 if (convert_p)
6536 expr = default_function_array_read_conversion (loc, expr);
6537 if (fold_p)
6538 expr.value = c_fully_fold (expr.value, false, NULL);
6539 VEC_quick_push (tree, ret, expr.value);
6540 if (orig_types != NULL)
6541 VEC_quick_push (tree, orig_types, expr.original_type);
6542 while (c_parser_next_token_is (parser, CPP_COMMA))
6544 c_parser_consume_token (parser);
6545 loc = c_parser_peek_token (parser)->location;
6546 expr = c_parser_expr_no_commas (parser, NULL);
6547 if (convert_p)
6548 expr = default_function_array_read_conversion (loc, expr);
6549 if (fold_p)
6550 expr.value = c_fully_fold (expr.value, false, NULL);
6551 VEC_safe_push (tree, gc, ret, expr.value);
6552 if (orig_types != NULL)
6553 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6555 if (orig_types != NULL)
6556 *p_orig_types = orig_types;
6557 return ret;
6560 /* Parse Objective-C-specific constructs. */
6562 /* Parse an objc-class-definition.
6564 objc-class-definition:
6565 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6566 objc-class-instance-variables[opt] objc-methodprotolist @end
6567 @implementation identifier objc-superclass[opt]
6568 objc-class-instance-variables[opt]
6569 @interface identifier ( identifier ) objc-protocol-refs[opt]
6570 objc-methodprotolist @end
6571 @implementation identifier ( identifier )
6573 objc-superclass:
6574 : identifier
6576 "@interface identifier (" must start "@interface identifier (
6577 identifier ) ...": objc-methodprotolist in the first production may
6578 not start with a parenthesized identifier as a declarator of a data
6579 definition with no declaration specifiers if the objc-superclass,
6580 objc-protocol-refs and objc-class-instance-variables are omitted. */
6582 static void
6583 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6585 bool iface_p;
6586 tree id1;
6587 tree superclass;
6588 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6589 iface_p = true;
6590 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6591 iface_p = false;
6592 else
6593 gcc_unreachable ();
6595 c_parser_consume_token (parser);
6596 if (c_parser_next_token_is_not (parser, CPP_NAME))
6598 c_parser_error (parser, "expected identifier");
6599 return;
6601 id1 = c_parser_peek_token (parser)->value;
6602 c_parser_consume_token (parser);
6603 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6605 tree id2;
6606 tree proto = NULL_TREE;
6607 c_parser_consume_token (parser);
6608 if (c_parser_next_token_is_not (parser, CPP_NAME))
6610 c_parser_error (parser, "expected identifier");
6611 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6612 return;
6614 id2 = c_parser_peek_token (parser)->value;
6615 c_parser_consume_token (parser);
6616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6617 if (!iface_p)
6619 objc_start_category_implementation (id1, id2);
6620 return;
6622 if (c_parser_next_token_is (parser, CPP_LESS))
6623 proto = c_parser_objc_protocol_refs (parser);
6624 objc_start_category_interface (id1, id2, proto, attributes);
6625 c_parser_objc_methodprotolist (parser);
6626 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6627 objc_finish_interface ();
6628 return;
6630 if (c_parser_next_token_is (parser, CPP_COLON))
6632 c_parser_consume_token (parser);
6633 if (c_parser_next_token_is_not (parser, CPP_NAME))
6635 c_parser_error (parser, "expected identifier");
6636 return;
6638 superclass = c_parser_peek_token (parser)->value;
6639 c_parser_consume_token (parser);
6641 else
6642 superclass = NULL_TREE;
6643 if (iface_p)
6645 tree proto = NULL_TREE;
6646 if (c_parser_next_token_is (parser, CPP_LESS))
6647 proto = c_parser_objc_protocol_refs (parser);
6648 objc_start_class_interface (id1, superclass, proto, attributes);
6650 else
6651 objc_start_class_implementation (id1, superclass);
6652 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6653 c_parser_objc_class_instance_variables (parser);
6654 if (iface_p)
6656 objc_continue_interface ();
6657 c_parser_objc_methodprotolist (parser);
6658 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6659 objc_finish_interface ();
6661 else
6663 objc_continue_implementation ();
6664 return;
6668 /* Parse objc-class-instance-variables.
6670 objc-class-instance-variables:
6671 { objc-instance-variable-decl-list[opt] }
6673 objc-instance-variable-decl-list:
6674 objc-visibility-spec
6675 objc-instance-variable-decl ;
6677 objc-instance-variable-decl-list objc-visibility-spec
6678 objc-instance-variable-decl-list objc-instance-variable-decl ;
6679 objc-instance-variable-decl-list ;
6681 objc-visibility-spec:
6682 @private
6683 @protected
6684 @public
6686 objc-instance-variable-decl:
6687 struct-declaration
6690 static void
6691 c_parser_objc_class_instance_variables (c_parser *parser)
6693 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6694 c_parser_consume_token (parser);
6695 while (c_parser_next_token_is_not (parser, CPP_EOF))
6697 tree decls;
6698 /* Parse any stray semicolon. */
6699 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6701 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6702 "extra semicolon in struct or union specified");
6703 c_parser_consume_token (parser);
6704 continue;
6706 /* Stop if at the end of the instance variables. */
6707 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6709 c_parser_consume_token (parser);
6710 break;
6712 /* Parse any objc-visibility-spec. */
6713 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6715 c_parser_consume_token (parser);
6716 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6717 continue;
6719 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6721 c_parser_consume_token (parser);
6722 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6723 continue;
6725 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6727 c_parser_consume_token (parser);
6728 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6729 continue;
6731 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6733 c_parser_consume_token (parser);
6734 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6735 continue;
6737 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6739 c_parser_pragma (parser, pragma_external);
6740 continue;
6743 /* Parse some comma-separated declarations. */
6744 decls = c_parser_struct_declaration (parser);
6746 /* Comma-separated instance variables are chained together in
6747 reverse order; add them one by one. */
6748 tree ivar = nreverse (decls);
6749 for (; ivar; ivar = DECL_CHAIN (ivar))
6750 objc_add_instance_variable (copy_node (ivar));
6752 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6756 /* Parse an objc-class-declaration.
6758 objc-class-declaration:
6759 @class identifier-list ;
6762 static void
6763 c_parser_objc_class_declaration (c_parser *parser)
6765 tree list = NULL_TREE;
6766 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6767 c_parser_consume_token (parser);
6768 /* Any identifiers, including those declared as type names, are OK
6769 here. */
6770 while (true)
6772 tree id;
6773 if (c_parser_next_token_is_not (parser, CPP_NAME))
6775 c_parser_error (parser, "expected identifier");
6776 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6777 parser->error = false;
6778 return;
6780 id = c_parser_peek_token (parser)->value;
6781 list = chainon (list, build_tree_list (NULL_TREE, id));
6782 c_parser_consume_token (parser);
6783 if (c_parser_next_token_is (parser, CPP_COMMA))
6784 c_parser_consume_token (parser);
6785 else
6786 break;
6788 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6789 objc_declare_class (list);
6792 /* Parse an objc-alias-declaration.
6794 objc-alias-declaration:
6795 @compatibility_alias identifier identifier ;
6798 static void
6799 c_parser_objc_alias_declaration (c_parser *parser)
6801 tree id1, id2;
6802 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6803 c_parser_consume_token (parser);
6804 if (c_parser_next_token_is_not (parser, CPP_NAME))
6806 c_parser_error (parser, "expected identifier");
6807 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6808 return;
6810 id1 = c_parser_peek_token (parser)->value;
6811 c_parser_consume_token (parser);
6812 if (c_parser_next_token_is_not (parser, CPP_NAME))
6814 c_parser_error (parser, "expected identifier");
6815 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6816 return;
6818 id2 = c_parser_peek_token (parser)->value;
6819 c_parser_consume_token (parser);
6820 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6821 objc_declare_alias (id1, id2);
6824 /* Parse an objc-protocol-definition.
6826 objc-protocol-definition:
6827 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6828 @protocol identifier-list ;
6830 "@protocol identifier ;" should be resolved as "@protocol
6831 identifier-list ;": objc-methodprotolist may not start with a
6832 semicolon in the first alternative if objc-protocol-refs are
6833 omitted. */
6835 static void
6836 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
6838 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6840 c_parser_consume_token (parser);
6841 if (c_parser_next_token_is_not (parser, CPP_NAME))
6843 c_parser_error (parser, "expected identifier");
6844 return;
6846 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6847 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6849 tree list = NULL_TREE;
6850 /* Any identifiers, including those declared as type names, are
6851 OK here. */
6852 while (true)
6854 tree id;
6855 if (c_parser_next_token_is_not (parser, CPP_NAME))
6857 c_parser_error (parser, "expected identifier");
6858 break;
6860 id = c_parser_peek_token (parser)->value;
6861 list = chainon (list, build_tree_list (NULL_TREE, id));
6862 c_parser_consume_token (parser);
6863 if (c_parser_next_token_is (parser, CPP_COMMA))
6864 c_parser_consume_token (parser);
6865 else
6866 break;
6868 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6869 objc_declare_protocols (list);
6871 else
6873 tree id = c_parser_peek_token (parser)->value;
6874 tree proto = NULL_TREE;
6875 c_parser_consume_token (parser);
6876 if (c_parser_next_token_is (parser, CPP_LESS))
6877 proto = c_parser_objc_protocol_refs (parser);
6878 parser->objc_pq_context = true;
6879 objc_start_protocol (id, proto, attributes);
6880 c_parser_objc_methodprotolist (parser);
6881 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6882 parser->objc_pq_context = false;
6883 objc_finish_interface ();
6887 /* Parse an objc-method-type.
6889 objc-method-type:
6893 Return true if it is a class method (+) and false if it is
6894 an instance method (-).
6896 static inline bool
6897 c_parser_objc_method_type (c_parser *parser)
6899 switch (c_parser_peek_token (parser)->type)
6901 case CPP_PLUS:
6902 c_parser_consume_token (parser);
6903 return true;
6904 case CPP_MINUS:
6905 c_parser_consume_token (parser);
6906 return false;
6907 default:
6908 gcc_unreachable ();
6912 /* Parse an objc-method-definition.
6914 objc-method-definition:
6915 objc-method-type objc-method-decl ;[opt] compound-statement
6918 static void
6919 c_parser_objc_method_definition (c_parser *parser)
6921 bool is_class_method = c_parser_objc_method_type (parser);
6922 tree decl, attributes = NULL_TREE;
6923 parser->objc_pq_context = true;
6924 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
6925 if (decl == error_mark_node)
6926 return; /* Bail here. */
6928 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6930 c_parser_consume_token (parser);
6931 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6932 "extra semicolon in method definition specified");
6935 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6937 c_parser_error (parser, "expected %<{%>");
6938 return;
6941 parser->objc_pq_context = false;
6942 if (objc_start_method_definition (is_class_method, decl, attributes))
6944 add_stmt (c_parser_compound_statement (parser));
6945 objc_finish_method_definition (current_function_decl);
6947 else
6949 /* This code is executed when we find a method definition
6950 outside of an @implementation context (or invalid for other
6951 reasons). Parse the method (to keep going) but do not emit
6952 any code.
6954 c_parser_compound_statement (parser);
6958 /* Parse an objc-methodprotolist.
6960 objc-methodprotolist:
6961 empty
6962 objc-methodprotolist objc-methodproto
6963 objc-methodprotolist declaration
6964 objc-methodprotolist ;
6965 @optional
6966 @required
6968 The declaration is a data definition, which may be missing
6969 declaration specifiers under the same rules and diagnostics as
6970 other data definitions outside functions, and the stray semicolon
6971 is diagnosed the same way as a stray semicolon outside a
6972 function. */
6974 static void
6975 c_parser_objc_methodprotolist (c_parser *parser)
6977 while (true)
6979 /* The list is terminated by @end. */
6980 switch (c_parser_peek_token (parser)->type)
6982 case CPP_SEMICOLON:
6983 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6984 "ISO C does not allow extra %<;%> outside of a function");
6985 c_parser_consume_token (parser);
6986 break;
6987 case CPP_PLUS:
6988 case CPP_MINUS:
6989 c_parser_objc_methodproto (parser);
6990 break;
6991 case CPP_PRAGMA:
6992 c_parser_pragma (parser, pragma_external);
6993 break;
6994 case CPP_EOF:
6995 return;
6996 default:
6997 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6998 return;
6999 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7000 c_parser_objc_at_property_declaration (parser);
7001 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7003 objc_set_method_opt (true);
7004 c_parser_consume_token (parser);
7006 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7008 objc_set_method_opt (false);
7009 c_parser_consume_token (parser);
7011 else
7012 c_parser_declaration_or_fndef (parser, false, false, true,
7013 false, true, NULL);
7014 break;
7019 /* Parse an objc-methodproto.
7021 objc-methodproto:
7022 objc-method-type objc-method-decl ;
7025 static void
7026 c_parser_objc_methodproto (c_parser *parser)
7028 bool is_class_method = c_parser_objc_method_type (parser);
7029 tree decl, attributes = NULL_TREE;
7031 /* Remember protocol qualifiers in prototypes. */
7032 parser->objc_pq_context = true;
7033 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7034 /* Forget protocol qualifiers now. */
7035 parser->objc_pq_context = false;
7037 /* Do not allow the presence of attributes to hide an erroneous
7038 method implementation in the interface section. */
7039 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7041 c_parser_error (parser, "expected %<;%>");
7042 return;
7045 if (decl != error_mark_node)
7046 objc_add_method_declaration (is_class_method, decl, attributes);
7048 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7051 /* If we are at a position that method attributes may be present, check that
7052 there are not any parsed already (a syntax error) and then collect any
7053 specified at the current location. Finally, if new attributes were present,
7054 check that the next token is legal ( ';' for decls and '{' for defs). */
7056 static bool
7057 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7059 bool bad = false;
7060 if (*attributes)
7062 c_parser_error (parser,
7063 "method attributes must be specified at the end only");
7064 *attributes = NULL_TREE;
7065 bad = true;
7068 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7069 *attributes = c_parser_attributes (parser);
7071 /* If there were no attributes here, just report any earlier error. */
7072 if (*attributes == NULL_TREE || bad)
7073 return bad;
7075 /* If the attributes are followed by a ; or {, then just report any earlier
7076 error. */
7077 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7078 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7079 return bad;
7081 /* We've got attributes, but not at the end. */
7082 c_parser_error (parser,
7083 "expected %<;%> or %<{%> after method attribute definition");
7084 return true;
7087 /* Parse an objc-method-decl.
7089 objc-method-decl:
7090 ( objc-type-name ) objc-selector
7091 objc-selector
7092 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7093 objc-keyword-selector objc-optparmlist
7094 attributes
7096 objc-keyword-selector:
7097 objc-keyword-decl
7098 objc-keyword-selector objc-keyword-decl
7100 objc-keyword-decl:
7101 objc-selector : ( objc-type-name ) identifier
7102 objc-selector : identifier
7103 : ( objc-type-name ) identifier
7104 : identifier
7106 objc-optparmlist:
7107 objc-optparms objc-optellipsis
7109 objc-optparms:
7110 empty
7111 objc-opt-parms , parameter-declaration
7113 objc-optellipsis:
7114 empty
7115 , ...
7118 static tree
7119 c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
7121 tree type = NULL_TREE;
7122 tree sel;
7123 tree parms = NULL_TREE;
7124 bool ellipsis = false;
7125 bool attr_err = false;
7127 *attributes = NULL_TREE;
7128 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7130 c_parser_consume_token (parser);
7131 type = c_parser_objc_type_name (parser);
7132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7134 sel = c_parser_objc_selector (parser);
7135 /* If there is no selector, or a colon follows, we have an
7136 objc-keyword-selector. If there is a selector, and a colon does
7137 not follow, that selector ends the objc-method-decl. */
7138 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7140 tree tsel = sel;
7141 tree list = NULL_TREE;
7142 while (true)
7144 tree atype = NULL_TREE, id, keyworddecl;
7145 tree param_attr = NULL_TREE;
7146 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7147 break;
7148 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7150 c_parser_consume_token (parser);
7151 atype = c_parser_objc_type_name (parser);
7152 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7153 "expected %<)%>");
7155 /* New ObjC allows attributes on method parameters. */
7156 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7157 param_attr = c_parser_attributes (parser);
7158 if (c_parser_next_token_is_not (parser, CPP_NAME))
7160 c_parser_error (parser, "expected identifier");
7161 return error_mark_node;
7163 id = c_parser_peek_token (parser)->value;
7164 c_parser_consume_token (parser);
7165 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7166 list = chainon (list, keyworddecl);
7167 tsel = c_parser_objc_selector (parser);
7168 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7169 break;
7172 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7174 /* Parse the optional parameter list. Optional Objective-C
7175 method parameters follow the C syntax, and may include '...'
7176 to denote a variable number of arguments. */
7177 parms = make_node (TREE_LIST);
7178 while (c_parser_next_token_is (parser, CPP_COMMA))
7180 struct c_parm *parm;
7181 c_parser_consume_token (parser);
7182 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7184 ellipsis = true;
7185 c_parser_consume_token (parser);
7186 attr_err |= c_parser_objc_maybe_method_attributes
7187 (parser, attributes) ;
7188 break;
7190 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7191 if (parm == NULL)
7192 break;
7193 parms = chainon (parms,
7194 build_tree_list (NULL_TREE, grokparm (parm)));
7196 sel = list;
7198 else
7199 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7201 if (sel == NULL)
7203 c_parser_error (parser, "objective-c method declaration is expected");
7204 return error_mark_node;
7207 if (attr_err)
7208 return error_mark_node;
7210 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7213 /* Parse an objc-type-name.
7215 objc-type-name:
7216 objc-type-qualifiers[opt] type-name
7217 objc-type-qualifiers[opt]
7219 objc-type-qualifiers:
7220 objc-type-qualifier
7221 objc-type-qualifiers objc-type-qualifier
7223 objc-type-qualifier: one of
7224 in out inout bycopy byref oneway
7227 static tree
7228 c_parser_objc_type_name (c_parser *parser)
7230 tree quals = NULL_TREE;
7231 struct c_type_name *type_name = NULL;
7232 tree type = NULL_TREE;
7233 while (true)
7235 c_token *token = c_parser_peek_token (parser);
7236 if (token->type == CPP_KEYWORD
7237 && (token->keyword == RID_IN
7238 || token->keyword == RID_OUT
7239 || token->keyword == RID_INOUT
7240 || token->keyword == RID_BYCOPY
7241 || token->keyword == RID_BYREF
7242 || token->keyword == RID_ONEWAY))
7244 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7245 c_parser_consume_token (parser);
7247 else
7248 break;
7250 if (c_parser_next_token_starts_typename (parser))
7251 type_name = c_parser_type_name (parser);
7252 if (type_name)
7253 type = groktypename (type_name, NULL, NULL);
7254 return build_tree_list (quals, type);
7257 /* Parse objc-protocol-refs.
7259 objc-protocol-refs:
7260 < identifier-list >
7263 static tree
7264 c_parser_objc_protocol_refs (c_parser *parser)
7266 tree list = NULL_TREE;
7267 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7268 c_parser_consume_token (parser);
7269 /* Any identifiers, including those declared as type names, are OK
7270 here. */
7271 while (true)
7273 tree id;
7274 if (c_parser_next_token_is_not (parser, CPP_NAME))
7276 c_parser_error (parser, "expected identifier");
7277 break;
7279 id = c_parser_peek_token (parser)->value;
7280 list = chainon (list, build_tree_list (NULL_TREE, id));
7281 c_parser_consume_token (parser);
7282 if (c_parser_next_token_is (parser, CPP_COMMA))
7283 c_parser_consume_token (parser);
7284 else
7285 break;
7287 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7288 return list;
7291 /* Parse an objc-try-catch-statement.
7293 objc-try-catch-statement:
7294 @try compound-statement objc-catch-list[opt]
7295 @try compound-statement objc-catch-list[opt] @finally compound-statement
7297 objc-catch-list:
7298 @catch ( parameter-declaration ) compound-statement
7299 objc-catch-list @catch ( parameter-declaration ) compound-statement
7302 static void
7303 c_parser_objc_try_catch_statement (c_parser *parser)
7305 location_t loc;
7306 tree stmt;
7307 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7308 c_parser_consume_token (parser);
7309 loc = c_parser_peek_token (parser)->location;
7310 stmt = c_parser_compound_statement (parser);
7311 objc_begin_try_stmt (loc, stmt);
7312 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7314 struct c_parm *parm;
7315 c_parser_consume_token (parser);
7316 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7317 break;
7318 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7319 if (parm == NULL)
7321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7322 break;
7324 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7325 objc_begin_catch_clause (grokparm (parm));
7326 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7327 c_parser_compound_statement_nostart (parser);
7328 objc_finish_catch_clause ();
7330 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7332 location_t finloc;
7333 tree finstmt;
7334 c_parser_consume_token (parser);
7335 finloc = c_parser_peek_token (parser)->location;
7336 finstmt = c_parser_compound_statement (parser);
7337 objc_build_finally_clause (finloc, finstmt);
7339 objc_finish_try_stmt ();
7342 /* Parse an objc-synchronized-statement.
7344 objc-synchronized-statement:
7345 @synchronized ( expression ) compound-statement
7348 static void
7349 c_parser_objc_synchronized_statement (c_parser *parser)
7351 location_t loc;
7352 tree expr, stmt;
7353 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7354 c_parser_consume_token (parser);
7355 loc = c_parser_peek_token (parser)->location;
7356 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7358 expr = c_parser_expression (parser).value;
7359 expr = c_fully_fold (expr, false, NULL);
7360 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7362 else
7363 expr = error_mark_node;
7364 stmt = c_parser_compound_statement (parser);
7365 objc_build_synchronized (loc, expr, stmt);
7368 /* Parse an objc-selector; return NULL_TREE without an error if the
7369 next token is not an objc-selector.
7371 objc-selector:
7372 identifier
7373 one of
7374 enum struct union if else while do for switch case default
7375 break continue return goto asm sizeof typeof __alignof
7376 unsigned long const short volatile signed restrict _Complex
7377 in out inout bycopy byref oneway int char float double void _Bool
7379 ??? Why this selection of keywords but not, for example, storage
7380 class specifiers? */
7382 static tree
7383 c_parser_objc_selector (c_parser *parser)
7385 c_token *token = c_parser_peek_token (parser);
7386 tree value = token->value;
7387 if (token->type == CPP_NAME)
7389 c_parser_consume_token (parser);
7390 return value;
7392 if (token->type != CPP_KEYWORD)
7393 return NULL_TREE;
7394 switch (token->keyword)
7396 case RID_ENUM:
7397 case RID_STRUCT:
7398 case RID_UNION:
7399 case RID_IF:
7400 case RID_ELSE:
7401 case RID_WHILE:
7402 case RID_DO:
7403 case RID_FOR:
7404 case RID_SWITCH:
7405 case RID_CASE:
7406 case RID_DEFAULT:
7407 case RID_BREAK:
7408 case RID_CONTINUE:
7409 case RID_RETURN:
7410 case RID_GOTO:
7411 case RID_ASM:
7412 case RID_SIZEOF:
7413 case RID_TYPEOF:
7414 case RID_ALIGNOF:
7415 case RID_UNSIGNED:
7416 case RID_LONG:
7417 case RID_INT128:
7418 case RID_CONST:
7419 case RID_SHORT:
7420 case RID_VOLATILE:
7421 case RID_SIGNED:
7422 case RID_RESTRICT:
7423 case RID_COMPLEX:
7424 case RID_IN:
7425 case RID_OUT:
7426 case RID_INOUT:
7427 case RID_BYCOPY:
7428 case RID_BYREF:
7429 case RID_ONEWAY:
7430 case RID_INT:
7431 case RID_CHAR:
7432 case RID_FLOAT:
7433 case RID_DOUBLE:
7434 case RID_VOID:
7435 case RID_BOOL:
7436 c_parser_consume_token (parser);
7437 return value;
7438 default:
7439 return NULL_TREE;
7443 /* Parse an objc-selector-arg.
7445 objc-selector-arg:
7446 objc-selector
7447 objc-keywordname-list
7449 objc-keywordname-list:
7450 objc-keywordname
7451 objc-keywordname-list objc-keywordname
7453 objc-keywordname:
7454 objc-selector :
7458 static tree
7459 c_parser_objc_selector_arg (c_parser *parser)
7461 tree sel = c_parser_objc_selector (parser);
7462 tree list = NULL_TREE;
7463 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7464 return sel;
7465 while (true)
7467 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7468 return list;
7469 list = chainon (list, build_tree_list (sel, NULL_TREE));
7470 sel = c_parser_objc_selector (parser);
7471 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7472 break;
7474 return list;
7477 /* Parse an objc-receiver.
7479 objc-receiver:
7480 expression
7481 class-name
7482 type-name
7485 static tree
7486 c_parser_objc_receiver (c_parser *parser)
7488 if (c_parser_peek_token (parser)->type == CPP_NAME
7489 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7490 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7492 tree id = c_parser_peek_token (parser)->value;
7493 c_parser_consume_token (parser);
7494 return objc_get_class_reference (id);
7496 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7499 /* Parse objc-message-args.
7501 objc-message-args:
7502 objc-selector
7503 objc-keywordarg-list
7505 objc-keywordarg-list:
7506 objc-keywordarg
7507 objc-keywordarg-list objc-keywordarg
7509 objc-keywordarg:
7510 objc-selector : objc-keywordexpr
7511 : objc-keywordexpr
7514 static tree
7515 c_parser_objc_message_args (c_parser *parser)
7517 tree sel = c_parser_objc_selector (parser);
7518 tree list = NULL_TREE;
7519 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7520 return sel;
7521 while (true)
7523 tree keywordexpr;
7524 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7525 return error_mark_node;
7526 keywordexpr = c_parser_objc_keywordexpr (parser);
7527 list = chainon (list, build_tree_list (sel, keywordexpr));
7528 sel = c_parser_objc_selector (parser);
7529 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7530 break;
7532 return list;
7535 /* Parse an objc-keywordexpr.
7537 objc-keywordexpr:
7538 nonempty-expr-list
7541 static tree
7542 c_parser_objc_keywordexpr (c_parser *parser)
7544 tree ret;
7545 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7546 if (VEC_length (tree, expr_list) == 1)
7548 /* Just return the expression, remove a level of
7549 indirection. */
7550 ret = VEC_index (tree, expr_list, 0);
7552 else
7554 /* We have a comma expression, we will collapse later. */
7555 ret = build_tree_list_vec (expr_list);
7557 release_tree_vector (expr_list);
7558 return ret;
7561 /* A check, needed in several places, that ObjC interface, implementation or
7562 method definitions are not prefixed by incorrect items. */
7563 static bool
7564 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7565 struct c_declspecs *specs)
7567 if (!specs->declspecs_seen_p || specs->type_seen_p || specs->non_sc_seen_p)
7569 c_parser_error (parser,
7570 "no type or storage class may be specified here,");
7571 c_parser_skip_to_end_of_block_or_statement (parser);
7572 return true;
7574 return false;
7577 /* Parse an Objective-C @property declaration. The syntax is:
7579 objc-property-declaration:
7580 '@property' objc-property-attributes[opt] struct-declaration ;
7582 objc-property-attributes:
7583 '(' objc-property-attribute-list ')'
7585 objc-property-attribute-list:
7586 objc-property-attribute
7587 objc-property-attribute-list, objc-property-attribute
7589 objc-property-attribute
7590 'getter' = identifier
7591 'setter' = identifier
7592 'readonly'
7593 'readwrite'
7594 'assign'
7595 'retain'
7596 'copy'
7597 'nonatomic'
7599 For example:
7600 @property NSString *name;
7601 @property (readonly) id object;
7602 @property (retain, nonatomic, getter=getTheName) id name;
7603 @property int a, b, c;
7605 PS: This function is identical to cp_parser_objc_at_propery_declaration
7606 for C++. Keep them in sync. */
7607 static void
7608 c_parser_objc_at_property_declaration (c_parser *parser)
7610 /* The following variables hold the attributes of the properties as
7611 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7612 seen. When we see an attribute, we set them to 'true' (if they
7613 are boolean properties) or to the identifier (if they have an
7614 argument, ie, for getter and setter). Note that here we only
7615 parse the list of attributes, check the syntax and accumulate the
7616 attributes that we find. objc_add_property_declaration() will
7617 then process the information. */
7618 bool property_assign = false;
7619 bool property_copy = false;
7620 tree property_getter_ident = NULL_TREE;
7621 bool property_nonatomic = false;
7622 bool property_readonly = false;
7623 bool property_readwrite = false;
7624 bool property_retain = false;
7625 tree property_setter_ident = NULL_TREE;
7626 /* The following two will be removed once @synthesize is
7627 implemented. */
7628 bool property_copies = false;
7629 tree property_ivar_ident = NULL_TREE;
7631 /* 'properties' is the list of properties that we read. Usually a
7632 single one, but maybe more (eg, in "@property int a, b, c;" there
7633 are three). */
7634 tree properties;
7635 location_t loc;
7637 loc = c_parser_peek_token (parser)->location;
7638 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7640 c_parser_consume_token (parser); /* Eat '@property'. */
7642 /* Parse the optional attribute list... */
7643 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7645 /* Eat the '(' */
7646 c_parser_consume_token (parser);
7648 /* Property attribute keywords are valid now. */
7649 parser->objc_property_attr_context = true;
7651 while (true)
7653 bool syntax_error = false;
7654 c_token *token = c_parser_peek_token (parser);
7655 enum rid keyword;
7657 if (token->type != CPP_KEYWORD)
7659 if (token->type == CPP_CLOSE_PAREN)
7660 c_parser_error (parser, "expected identifier");
7661 else
7663 c_parser_consume_token (parser);
7664 c_parser_error (parser, "unknown property attribute");
7666 break;
7668 keyword = token->keyword;
7669 c_parser_consume_token (parser);
7670 switch (keyword)
7672 case RID_ASSIGN: property_assign = true; break;
7673 case RID_COPIES: property_copies = true; break;
7674 case RID_COPY: property_copy = true; break;
7675 case RID_NONATOMIC: property_nonatomic = true; break;
7676 case RID_READONLY: property_readonly = true; break;
7677 case RID_READWRITE: property_readwrite = true; break;
7678 case RID_RETAIN: property_retain = true; break;
7680 case RID_GETTER:
7681 case RID_SETTER:
7682 case RID_IVAR:
7683 if (c_parser_next_token_is_not (parser, CPP_EQ))
7685 c_parser_error (parser,
7686 "getter/setter/ivar attribute must be followed by %<=%>");
7687 syntax_error = true;
7688 break;
7690 c_parser_consume_token (parser); /* eat the = */
7691 if (c_parser_next_token_is_not (parser, CPP_NAME))
7693 c_parser_error (parser, "expected identifier");
7694 syntax_error = true;
7695 break;
7697 if (keyword == RID_SETTER)
7699 if (property_setter_ident != NULL_TREE)
7700 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7701 else
7702 property_setter_ident = c_parser_peek_token (parser)->value;
7703 c_parser_consume_token (parser);
7704 if (c_parser_next_token_is_not (parser, CPP_COLON))
7705 c_parser_error (parser, "setter name must terminate with %<:%>");
7706 else
7707 c_parser_consume_token (parser);
7709 else if (keyword == RID_GETTER)
7711 if (property_getter_ident != NULL_TREE)
7712 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
7713 else
7714 property_getter_ident = c_parser_peek_token (parser)->value;
7715 c_parser_consume_token (parser);
7717 else /* RID_IVAR, this case will go away. */
7719 if (property_ivar_ident != NULL_TREE)
7720 c_parser_error (parser, "the %<ivar%> attribute may only be specified once");
7721 else
7722 property_ivar_ident = c_parser_peek_token (parser)->value;
7723 c_parser_consume_token (parser);
7725 break;
7726 default:
7727 c_parser_error (parser, "unknown property attribute");
7728 syntax_error = true;
7729 break;
7732 if (syntax_error)
7733 break;
7735 if (c_parser_next_token_is (parser, CPP_COMMA))
7736 c_parser_consume_token (parser);
7737 else
7738 break;
7740 parser->objc_property_attr_context = false;
7741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7743 /* ... and the property declaration(s). */
7744 properties = c_parser_struct_declaration (parser);
7746 if (properties == error_mark_node)
7748 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7749 parser->error = false;
7750 return;
7753 if (properties == NULL_TREE)
7754 c_parser_error (parser, "expected identifier");
7755 else
7757 /* Comma-separated properties are chained together in
7758 reverse order; add them one by one. */
7759 properties = nreverse (properties);
7761 for (; properties; properties = TREE_CHAIN (properties))
7762 objc_add_property_declaration (loc, copy_node (properties),
7763 property_readonly, property_readwrite,
7764 property_assign, property_retain,
7765 property_copy, property_nonatomic,
7766 property_getter_ident, property_setter_ident,
7767 /* The following two will be removed. */
7768 property_copies, property_ivar_ident);
7771 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7772 parser->error = false;
7775 /* Parse an Objective-C @synthesize declaration. The syntax is:
7777 objc-synthesize-declaration:
7778 @synthesize objc-synthesize-identifier-list ;
7780 objc-synthesize-identifier-list:
7781 objc-synthesize-identifier
7782 objc-synthesize-identifier-list, objc-synthesize-identifier
7784 objc-synthesize-identifier
7785 identifier
7786 identifier = identifier
7788 For example:
7789 @synthesize MyProperty;
7790 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
7792 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
7793 for C++. Keep them in sync.
7795 static void
7796 c_parser_objc_at_synthesize_declaration (c_parser *parser)
7798 tree list = NULL_TREE;
7799 location_t loc;
7800 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
7801 loc = c_parser_peek_token (parser)->location;
7803 c_parser_consume_token (parser);
7804 while (true)
7806 tree property, ivar;
7807 if (c_parser_next_token_is_not (parser, CPP_NAME))
7809 c_parser_error (parser, "expected identifier");
7810 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7811 /* Once we find the semicolon, we can resume normal parsing.
7812 We have to reset parser->error manually because
7813 c_parser_skip_until_found() won't reset it for us if the
7814 next token is precisely a semicolon. */
7815 parser->error = false;
7816 return;
7818 property = c_parser_peek_token (parser)->value;
7819 c_parser_consume_token (parser);
7820 if (c_parser_next_token_is (parser, CPP_EQ))
7822 c_parser_consume_token (parser);
7823 if (c_parser_next_token_is_not (parser, CPP_NAME))
7825 c_parser_error (parser, "expected identifier");
7826 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7827 parser->error = false;
7828 return;
7830 ivar = c_parser_peek_token (parser)->value;
7831 c_parser_consume_token (parser);
7833 else
7834 ivar = NULL_TREE;
7835 list = chainon (list, build_tree_list (ivar, property));
7836 if (c_parser_next_token_is (parser, CPP_COMMA))
7837 c_parser_consume_token (parser);
7838 else
7839 break;
7841 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7842 objc_add_synthesize_declaration (loc, list);
7845 /* Parse an Objective-C @dynamic declaration. The syntax is:
7847 objc-dynamic-declaration:
7848 @dynamic identifier-list ;
7850 For example:
7851 @dynamic MyProperty;
7852 @dynamic MyProperty, AnotherProperty;
7854 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
7855 for C++. Keep them in sync.
7857 static void
7858 c_parser_objc_at_dynamic_declaration (c_parser *parser)
7860 tree list = NULL_TREE;
7861 location_t loc;
7862 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
7863 loc = c_parser_peek_token (parser)->location;
7865 c_parser_consume_token (parser);
7866 while (true)
7868 tree property;
7869 if (c_parser_next_token_is_not (parser, CPP_NAME))
7871 c_parser_error (parser, "expected identifier");
7872 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7873 parser->error = false;
7874 return;
7876 property = c_parser_peek_token (parser)->value;
7877 list = chainon (list, build_tree_list (NULL_TREE, property));
7878 c_parser_consume_token (parser);
7879 if (c_parser_next_token_is (parser, CPP_COMMA))
7880 c_parser_consume_token (parser);
7881 else
7882 break;
7884 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7885 objc_add_dynamic_declaration (loc, list);
7889 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
7890 should be considered, statements. ALLOW_STMT is true if we're within
7891 the context of a function and such pragmas are to be allowed. Returns
7892 true if we actually parsed such a pragma. */
7894 static bool
7895 c_parser_pragma (c_parser *parser, enum pragma_context context)
7897 unsigned int id;
7899 id = c_parser_peek_token (parser)->pragma_kind;
7900 gcc_assert (id != PRAGMA_NONE);
7902 switch (id)
7904 case PRAGMA_OMP_BARRIER:
7905 if (context != pragma_compound)
7907 if (context == pragma_stmt)
7908 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7909 "used in compound statements");
7910 goto bad_stmt;
7912 c_parser_omp_barrier (parser);
7913 return false;
7915 case PRAGMA_OMP_FLUSH:
7916 if (context != pragma_compound)
7918 if (context == pragma_stmt)
7919 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7920 "used in compound statements");
7921 goto bad_stmt;
7923 c_parser_omp_flush (parser);
7924 return false;
7926 case PRAGMA_OMP_TASKWAIT:
7927 if (context != pragma_compound)
7929 if (context == pragma_stmt)
7930 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7931 "used in compound statements");
7932 goto bad_stmt;
7934 c_parser_omp_taskwait (parser);
7935 return false;
7937 case PRAGMA_OMP_THREADPRIVATE:
7938 c_parser_omp_threadprivate (parser);
7939 return false;
7941 case PRAGMA_OMP_SECTION:
7942 error_at (c_parser_peek_token (parser)->location,
7943 "%<#pragma omp section%> may only be used in "
7944 "%<#pragma omp sections%> construct");
7945 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7946 return false;
7948 case PRAGMA_GCC_PCH_PREPROCESS:
7949 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7950 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7951 return false;
7953 default:
7954 if (id < PRAGMA_FIRST_EXTERNAL)
7956 if (context == pragma_external)
7958 bad_stmt:
7959 c_parser_error (parser, "expected declaration specifiers");
7960 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7961 return false;
7963 c_parser_omp_construct (parser);
7964 return true;
7966 break;
7969 c_parser_consume_pragma (parser);
7970 c_invoke_pragma_handler (id);
7972 /* Skip to EOL, but suppress any error message. Those will have been
7973 generated by the handler routine through calling error, as opposed
7974 to calling c_parser_error. */
7975 parser->error = true;
7976 c_parser_skip_to_pragma_eol (parser);
7978 return false;
7981 /* The interface the pragma parsers have to the lexer. */
7983 enum cpp_ttype
7984 pragma_lex (tree *value)
7986 c_token *tok = c_parser_peek_token (the_parser);
7987 enum cpp_ttype ret = tok->type;
7989 *value = tok->value;
7990 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
7991 ret = CPP_EOF;
7992 else
7994 if (ret == CPP_KEYWORD)
7995 ret = CPP_NAME;
7996 c_parser_consume_token (the_parser);
7999 return ret;
8002 static void
8003 c_parser_pragma_pch_preprocess (c_parser *parser)
8005 tree name = NULL;
8007 c_parser_consume_pragma (parser);
8008 if (c_parser_next_token_is (parser, CPP_STRING))
8010 name = c_parser_peek_token (parser)->value;
8011 c_parser_consume_token (parser);
8013 else
8014 c_parser_error (parser, "expected string literal");
8015 c_parser_skip_to_pragma_eol (parser);
8017 if (name)
8018 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8021 /* OpenMP 2.5 parsing routines. */
8023 /* Returns name of the next clause.
8024 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8025 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8026 returned and the token is consumed. */
8028 static pragma_omp_clause
8029 c_parser_omp_clause_name (c_parser *parser)
8031 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8033 if (c_parser_next_token_is_keyword (parser, RID_IF))
8034 result = PRAGMA_OMP_CLAUSE_IF;
8035 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8036 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8037 else if (c_parser_next_token_is (parser, CPP_NAME))
8039 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8041 switch (p[0])
8043 case 'c':
8044 if (!strcmp ("collapse", p))
8045 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8046 else if (!strcmp ("copyin", p))
8047 result = PRAGMA_OMP_CLAUSE_COPYIN;
8048 else if (!strcmp ("copyprivate", p))
8049 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8050 break;
8051 case 'f':
8052 if (!strcmp ("firstprivate", p))
8053 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8054 break;
8055 case 'l':
8056 if (!strcmp ("lastprivate", p))
8057 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8058 break;
8059 case 'n':
8060 if (!strcmp ("nowait", p))
8061 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8062 else if (!strcmp ("num_threads", p))
8063 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8064 break;
8065 case 'o':
8066 if (!strcmp ("ordered", p))
8067 result = PRAGMA_OMP_CLAUSE_ORDERED;
8068 break;
8069 case 'p':
8070 if (!strcmp ("private", p))
8071 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8072 break;
8073 case 'r':
8074 if (!strcmp ("reduction", p))
8075 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8076 break;
8077 case 's':
8078 if (!strcmp ("schedule", p))
8079 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8080 else if (!strcmp ("shared", p))
8081 result = PRAGMA_OMP_CLAUSE_SHARED;
8082 break;
8083 case 'u':
8084 if (!strcmp ("untied", p))
8085 result = PRAGMA_OMP_CLAUSE_UNTIED;
8086 break;
8090 if (result != PRAGMA_OMP_CLAUSE_NONE)
8091 c_parser_consume_token (parser);
8093 return result;
8096 /* Validate that a clause of the given type does not already exist. */
8098 static void
8099 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8100 const char *name)
8102 tree c;
8104 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8105 if (OMP_CLAUSE_CODE (c) == code)
8107 location_t loc = OMP_CLAUSE_LOCATION (c);
8108 error_at (loc, "too many %qs clauses", name);
8109 break;
8113 /* OpenMP 2.5:
8114 variable-list:
8115 identifier
8116 variable-list , identifier
8118 If KIND is nonzero, create the appropriate node and install the
8119 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8120 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8122 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8123 return the list created. */
8125 static tree
8126 c_parser_omp_variable_list (c_parser *parser,
8127 location_t clause_loc,
8128 enum omp_clause_code kind,
8129 tree list)
8131 if (c_parser_next_token_is_not (parser, CPP_NAME)
8132 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8133 c_parser_error (parser, "expected identifier");
8135 while (c_parser_next_token_is (parser, CPP_NAME)
8136 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8138 tree t = lookup_name (c_parser_peek_token (parser)->value);
8140 if (t == NULL_TREE)
8141 undeclared_variable (c_parser_peek_token (parser)->location,
8142 c_parser_peek_token (parser)->value);
8143 else if (t == error_mark_node)
8145 else if (kind != 0)
8147 tree u = build_omp_clause (clause_loc, kind);
8148 OMP_CLAUSE_DECL (u) = t;
8149 OMP_CLAUSE_CHAIN (u) = list;
8150 list = u;
8152 else
8153 list = tree_cons (t, NULL_TREE, list);
8155 c_parser_consume_token (parser);
8157 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8158 break;
8160 c_parser_consume_token (parser);
8163 return list;
8166 /* Similarly, but expect leading and trailing parenthesis. This is a very
8167 common case for omp clauses. */
8169 static tree
8170 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8171 tree list)
8173 /* The clauses location. */
8174 location_t loc = c_parser_peek_token (parser)->location;
8176 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8178 list = c_parser_omp_variable_list (parser, loc, kind, list);
8179 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8181 return list;
8184 /* OpenMP 3.0:
8185 collapse ( constant-expression ) */
8187 static tree
8188 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8190 tree c, num = error_mark_node;
8191 HOST_WIDE_INT n;
8192 location_t loc;
8194 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8196 loc = c_parser_peek_token (parser)->location;
8197 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8199 num = c_parser_expr_no_commas (parser, NULL).value;
8200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8202 if (num == error_mark_node)
8203 return list;
8204 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8205 || !host_integerp (num, 0)
8206 || (n = tree_low_cst (num, 0)) <= 0
8207 || (int) n != n)
8209 error_at (loc,
8210 "collapse argument needs positive constant integer expression");
8211 return list;
8213 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8214 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8215 OMP_CLAUSE_CHAIN (c) = list;
8216 return c;
8219 /* OpenMP 2.5:
8220 copyin ( variable-list ) */
8222 static tree
8223 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8225 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8228 /* OpenMP 2.5:
8229 copyprivate ( variable-list ) */
8231 static tree
8232 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8234 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8237 /* OpenMP 2.5:
8238 default ( shared | none ) */
8240 static tree
8241 c_parser_omp_clause_default (c_parser *parser, tree list)
8243 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8244 location_t loc = c_parser_peek_token (parser)->location;
8245 tree c;
8247 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8248 return list;
8249 if (c_parser_next_token_is (parser, CPP_NAME))
8251 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8253 switch (p[0])
8255 case 'n':
8256 if (strcmp ("none", p) != 0)
8257 goto invalid_kind;
8258 kind = OMP_CLAUSE_DEFAULT_NONE;
8259 break;
8261 case 's':
8262 if (strcmp ("shared", p) != 0)
8263 goto invalid_kind;
8264 kind = OMP_CLAUSE_DEFAULT_SHARED;
8265 break;
8267 default:
8268 goto invalid_kind;
8271 c_parser_consume_token (parser);
8273 else
8275 invalid_kind:
8276 c_parser_error (parser, "expected %<none%> or %<shared%>");
8278 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8280 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8281 return list;
8283 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8284 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8285 OMP_CLAUSE_CHAIN (c) = list;
8286 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8288 return c;
8291 /* OpenMP 2.5:
8292 firstprivate ( variable-list ) */
8294 static tree
8295 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8297 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8300 /* OpenMP 2.5:
8301 if ( expression ) */
8303 static tree
8304 c_parser_omp_clause_if (c_parser *parser, tree list)
8306 location_t loc = c_parser_peek_token (parser)->location;
8307 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8309 tree t = c_parser_paren_condition (parser);
8310 tree c;
8312 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8314 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8315 OMP_CLAUSE_IF_EXPR (c) = t;
8316 OMP_CLAUSE_CHAIN (c) = list;
8317 list = c;
8319 else
8320 c_parser_error (parser, "expected %<(%>");
8322 return list;
8325 /* OpenMP 2.5:
8326 lastprivate ( variable-list ) */
8328 static tree
8329 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8331 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8334 /* OpenMP 2.5:
8335 nowait */
8337 static tree
8338 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8340 tree c;
8341 location_t loc = c_parser_peek_token (parser)->location;
8343 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8345 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8346 OMP_CLAUSE_CHAIN (c) = list;
8347 return c;
8350 /* OpenMP 2.5:
8351 num_threads ( expression ) */
8353 static tree
8354 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8356 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8357 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8359 location_t expr_loc = c_parser_peek_token (parser)->location;
8360 tree c, t = c_parser_expression (parser).value;
8361 t = c_fully_fold (t, false, NULL);
8363 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8365 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8367 c_parser_error (parser, "expected integer expression");
8368 return list;
8371 /* Attempt to statically determine when the number isn't positive. */
8372 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8373 build_int_cst (TREE_TYPE (t), 0));
8374 if (CAN_HAVE_LOCATION_P (c))
8375 SET_EXPR_LOCATION (c, expr_loc);
8376 if (c == boolean_true_node)
8378 warning_at (expr_loc, 0,
8379 "%<num_threads%> value must be positive");
8380 t = integer_one_node;
8383 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8385 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8386 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8387 OMP_CLAUSE_CHAIN (c) = list;
8388 list = c;
8391 return list;
8394 /* OpenMP 2.5:
8395 ordered */
8397 static tree
8398 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8400 tree c;
8402 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8404 c = build_omp_clause (c_parser_peek_token (parser)->location,
8405 OMP_CLAUSE_ORDERED);
8406 OMP_CLAUSE_CHAIN (c) = list;
8408 return c;
8411 /* OpenMP 2.5:
8412 private ( variable-list ) */
8414 static tree
8415 c_parser_omp_clause_private (c_parser *parser, tree list)
8417 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8420 /* OpenMP 2.5:
8421 reduction ( reduction-operator : variable-list )
8423 reduction-operator:
8424 One of: + * - & ^ | && || */
8426 static tree
8427 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8429 location_t clause_loc = c_parser_peek_token (parser)->location;
8430 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8432 enum tree_code code;
8434 switch (c_parser_peek_token (parser)->type)
8436 case CPP_PLUS:
8437 code = PLUS_EXPR;
8438 break;
8439 case CPP_MULT:
8440 code = MULT_EXPR;
8441 break;
8442 case CPP_MINUS:
8443 code = MINUS_EXPR;
8444 break;
8445 case CPP_AND:
8446 code = BIT_AND_EXPR;
8447 break;
8448 case CPP_XOR:
8449 code = BIT_XOR_EXPR;
8450 break;
8451 case CPP_OR:
8452 code = BIT_IOR_EXPR;
8453 break;
8454 case CPP_AND_AND:
8455 code = TRUTH_ANDIF_EXPR;
8456 break;
8457 case CPP_OR_OR:
8458 code = TRUTH_ORIF_EXPR;
8459 break;
8460 default:
8461 c_parser_error (parser,
8462 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8463 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8464 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8465 return list;
8467 c_parser_consume_token (parser);
8468 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8470 tree nl, c;
8472 nl = c_parser_omp_variable_list (parser, clause_loc,
8473 OMP_CLAUSE_REDUCTION, list);
8474 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8475 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8477 list = nl;
8479 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8481 return list;
8484 /* OpenMP 2.5:
8485 schedule ( schedule-kind )
8486 schedule ( schedule-kind , expression )
8488 schedule-kind:
8489 static | dynamic | guided | runtime | auto
8492 static tree
8493 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8495 tree c, t;
8496 location_t loc = c_parser_peek_token (parser)->location;
8498 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8499 return list;
8501 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8503 if (c_parser_next_token_is (parser, CPP_NAME))
8505 tree kind = c_parser_peek_token (parser)->value;
8506 const char *p = IDENTIFIER_POINTER (kind);
8508 switch (p[0])
8510 case 'd':
8511 if (strcmp ("dynamic", p) != 0)
8512 goto invalid_kind;
8513 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8514 break;
8516 case 'g':
8517 if (strcmp ("guided", p) != 0)
8518 goto invalid_kind;
8519 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8520 break;
8522 case 'r':
8523 if (strcmp ("runtime", p) != 0)
8524 goto invalid_kind;
8525 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8526 break;
8528 default:
8529 goto invalid_kind;
8532 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8533 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8534 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8535 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8536 else
8537 goto invalid_kind;
8539 c_parser_consume_token (parser);
8540 if (c_parser_next_token_is (parser, CPP_COMMA))
8542 location_t here;
8543 c_parser_consume_token (parser);
8545 here = c_parser_peek_token (parser)->location;
8546 t = c_parser_expr_no_commas (parser, NULL).value;
8547 t = c_fully_fold (t, false, NULL);
8549 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8550 error_at (here, "schedule %<runtime%> does not take "
8551 "a %<chunk_size%> parameter");
8552 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8553 error_at (here,
8554 "schedule %<auto%> does not take "
8555 "a %<chunk_size%> parameter");
8556 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8557 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8558 else
8559 c_parser_error (parser, "expected integer expression");
8561 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8563 else
8564 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8565 "expected %<,%> or %<)%>");
8567 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8568 OMP_CLAUSE_CHAIN (c) = list;
8569 return c;
8571 invalid_kind:
8572 c_parser_error (parser, "invalid schedule kind");
8573 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8574 return list;
8577 /* OpenMP 2.5:
8578 shared ( variable-list ) */
8580 static tree
8581 c_parser_omp_clause_shared (c_parser *parser, tree list)
8583 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8586 /* OpenMP 3.0:
8587 untied */
8589 static tree
8590 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8592 tree c;
8594 /* FIXME: Should we allow duplicates? */
8595 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8597 c = build_omp_clause (c_parser_peek_token (parser)->location,
8598 OMP_CLAUSE_UNTIED);
8599 OMP_CLAUSE_CHAIN (c) = list;
8601 return c;
8604 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8605 is a bitmask in MASK. Return the list of clauses found; the result
8606 of clause default goes in *pdefault. */
8608 static tree
8609 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8610 const char *where)
8612 tree clauses = NULL;
8613 bool first = true;
8615 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8617 location_t here;
8618 pragma_omp_clause c_kind;
8619 const char *c_name;
8620 tree prev = clauses;
8622 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8623 c_parser_consume_token (parser);
8625 first = false;
8626 here = c_parser_peek_token (parser)->location;
8627 c_kind = c_parser_omp_clause_name (parser);
8629 switch (c_kind)
8631 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8632 clauses = c_parser_omp_clause_collapse (parser, clauses);
8633 c_name = "collapse";
8634 break;
8635 case PRAGMA_OMP_CLAUSE_COPYIN:
8636 clauses = c_parser_omp_clause_copyin (parser, clauses);
8637 c_name = "copyin";
8638 break;
8639 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8640 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8641 c_name = "copyprivate";
8642 break;
8643 case PRAGMA_OMP_CLAUSE_DEFAULT:
8644 clauses = c_parser_omp_clause_default (parser, clauses);
8645 c_name = "default";
8646 break;
8647 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8648 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8649 c_name = "firstprivate";
8650 break;
8651 case PRAGMA_OMP_CLAUSE_IF:
8652 clauses = c_parser_omp_clause_if (parser, clauses);
8653 c_name = "if";
8654 break;
8655 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8656 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8657 c_name = "lastprivate";
8658 break;
8659 case PRAGMA_OMP_CLAUSE_NOWAIT:
8660 clauses = c_parser_omp_clause_nowait (parser, clauses);
8661 c_name = "nowait";
8662 break;
8663 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8664 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8665 c_name = "num_threads";
8666 break;
8667 case PRAGMA_OMP_CLAUSE_ORDERED:
8668 clauses = c_parser_omp_clause_ordered (parser, clauses);
8669 c_name = "ordered";
8670 break;
8671 case PRAGMA_OMP_CLAUSE_PRIVATE:
8672 clauses = c_parser_omp_clause_private (parser, clauses);
8673 c_name = "private";
8674 break;
8675 case PRAGMA_OMP_CLAUSE_REDUCTION:
8676 clauses = c_parser_omp_clause_reduction (parser, clauses);
8677 c_name = "reduction";
8678 break;
8679 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8680 clauses = c_parser_omp_clause_schedule (parser, clauses);
8681 c_name = "schedule";
8682 break;
8683 case PRAGMA_OMP_CLAUSE_SHARED:
8684 clauses = c_parser_omp_clause_shared (parser, clauses);
8685 c_name = "shared";
8686 break;
8687 case PRAGMA_OMP_CLAUSE_UNTIED:
8688 clauses = c_parser_omp_clause_untied (parser, clauses);
8689 c_name = "untied";
8690 break;
8691 default:
8692 c_parser_error (parser, "expected %<#pragma omp%> clause");
8693 goto saw_error;
8696 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8698 /* Remove the invalid clause(s) from the list to avoid
8699 confusing the rest of the compiler. */
8700 clauses = prev;
8701 error_at (here, "%qs is not valid for %qs", c_name, where);
8705 saw_error:
8706 c_parser_skip_to_pragma_eol (parser);
8708 return c_finish_omp_clauses (clauses);
8711 /* OpenMP 2.5:
8712 structured-block:
8713 statement
8715 In practice, we're also interested in adding the statement to an
8716 outer node. So it is convenient if we work around the fact that
8717 c_parser_statement calls add_stmt. */
8719 static tree
8720 c_parser_omp_structured_block (c_parser *parser)
8722 tree stmt = push_stmt_list ();
8723 c_parser_statement (parser);
8724 return pop_stmt_list (stmt);
8727 /* OpenMP 2.5:
8728 # pragma omp atomic new-line
8729 expression-stmt
8731 expression-stmt:
8732 x binop= expr | x++ | ++x | x-- | --x
8733 binop:
8734 +, *, -, /, &, ^, |, <<, >>
8736 where x is an lvalue expression with scalar type.
8738 LOC is the location of the #pragma token. */
8740 static void
8741 c_parser_omp_atomic (location_t loc, c_parser *parser)
8743 tree lhs, rhs;
8744 tree stmt;
8745 enum tree_code code;
8746 struct c_expr rhs_expr;
8748 c_parser_skip_to_pragma_eol (parser);
8750 lhs = c_parser_unary_expression (parser).value;
8751 lhs = c_fully_fold (lhs, false, NULL);
8752 switch (TREE_CODE (lhs))
8754 case ERROR_MARK:
8755 saw_error:
8756 c_parser_skip_to_end_of_block_or_statement (parser);
8757 return;
8759 case PREINCREMENT_EXPR:
8760 case POSTINCREMENT_EXPR:
8761 lhs = TREE_OPERAND (lhs, 0);
8762 code = PLUS_EXPR;
8763 rhs = integer_one_node;
8764 break;
8766 case PREDECREMENT_EXPR:
8767 case POSTDECREMENT_EXPR:
8768 lhs = TREE_OPERAND (lhs, 0);
8769 code = MINUS_EXPR;
8770 rhs = integer_one_node;
8771 break;
8773 case COMPOUND_EXPR:
8774 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
8775 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
8776 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
8777 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
8778 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
8779 (TREE_OPERAND (lhs, 1), 0), 0)))
8780 == BOOLEAN_TYPE)
8781 /* Undo effects of boolean_increment for post {in,de}crement. */
8782 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
8783 /* FALLTHRU */
8784 case MODIFY_EXPR:
8785 if (TREE_CODE (lhs) == MODIFY_EXPR
8786 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
8788 /* Undo effects of boolean_increment. */
8789 if (integer_onep (TREE_OPERAND (lhs, 1)))
8791 /* This is pre or post increment. */
8792 rhs = TREE_OPERAND (lhs, 1);
8793 lhs = TREE_OPERAND (lhs, 0);
8794 code = NOP_EXPR;
8795 break;
8797 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
8798 && TREE_OPERAND (lhs, 0)
8799 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
8801 /* This is pre or post decrement. */
8802 rhs = TREE_OPERAND (lhs, 1);
8803 lhs = TREE_OPERAND (lhs, 0);
8804 code = NOP_EXPR;
8805 break;
8808 /* FALLTHRU */
8809 default:
8810 switch (c_parser_peek_token (parser)->type)
8812 case CPP_MULT_EQ:
8813 code = MULT_EXPR;
8814 break;
8815 case CPP_DIV_EQ:
8816 code = TRUNC_DIV_EXPR;
8817 break;
8818 case CPP_PLUS_EQ:
8819 code = PLUS_EXPR;
8820 break;
8821 case CPP_MINUS_EQ:
8822 code = MINUS_EXPR;
8823 break;
8824 case CPP_LSHIFT_EQ:
8825 code = LSHIFT_EXPR;
8826 break;
8827 case CPP_RSHIFT_EQ:
8828 code = RSHIFT_EXPR;
8829 break;
8830 case CPP_AND_EQ:
8831 code = BIT_AND_EXPR;
8832 break;
8833 case CPP_OR_EQ:
8834 code = BIT_IOR_EXPR;
8835 break;
8836 case CPP_XOR_EQ:
8837 code = BIT_XOR_EXPR;
8838 break;
8839 default:
8840 c_parser_error (parser,
8841 "invalid operator for %<#pragma omp atomic%>");
8842 goto saw_error;
8845 c_parser_consume_token (parser);
8847 location_t rhs_loc = c_parser_peek_token (parser)->location;
8848 rhs_expr = c_parser_expression (parser);
8849 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
8851 rhs = rhs_expr.value;
8852 rhs = c_fully_fold (rhs, false, NULL);
8853 break;
8855 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
8856 if (stmt != error_mark_node)
8857 add_stmt (stmt);
8858 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8862 /* OpenMP 2.5:
8863 # pragma omp barrier new-line
8866 static void
8867 c_parser_omp_barrier (c_parser *parser)
8869 location_t loc = c_parser_peek_token (parser)->location;
8870 c_parser_consume_pragma (parser);
8871 c_parser_skip_to_pragma_eol (parser);
8873 c_finish_omp_barrier (loc);
8876 /* OpenMP 2.5:
8877 # pragma omp critical [(name)] new-line
8878 structured-block
8880 LOC is the location of the #pragma itself. */
8882 static tree
8883 c_parser_omp_critical (location_t loc, c_parser *parser)
8885 tree stmt, name = NULL;
8887 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8889 c_parser_consume_token (parser);
8890 if (c_parser_next_token_is (parser, CPP_NAME))
8892 name = c_parser_peek_token (parser)->value;
8893 c_parser_consume_token (parser);
8894 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8896 else
8897 c_parser_error (parser, "expected identifier");
8899 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8900 c_parser_error (parser, "expected %<(%> or end of line");
8901 c_parser_skip_to_pragma_eol (parser);
8903 stmt = c_parser_omp_structured_block (parser);
8904 return c_finish_omp_critical (loc, stmt, name);
8907 /* OpenMP 2.5:
8908 # pragma omp flush flush-vars[opt] new-line
8910 flush-vars:
8911 ( variable-list ) */
8913 static void
8914 c_parser_omp_flush (c_parser *parser)
8916 location_t loc = c_parser_peek_token (parser)->location;
8917 c_parser_consume_pragma (parser);
8918 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8919 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8920 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8921 c_parser_error (parser, "expected %<(%> or end of line");
8922 c_parser_skip_to_pragma_eol (parser);
8924 c_finish_omp_flush (loc);
8927 /* Parse the restricted form of the for statement allowed by OpenMP.
8928 The real trick here is to determine the loop control variable early
8929 so that we can push a new decl if necessary to make it private.
8930 LOC is the location of the OMP in "#pragma omp". */
8932 static tree
8933 c_parser_omp_for_loop (location_t loc,
8934 c_parser *parser, tree clauses, tree *par_clauses)
8936 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
8937 tree declv, condv, incrv, initv, ret = NULL;
8938 bool fail = false, open_brace_parsed = false;
8939 int i, collapse = 1, nbraces = 0;
8940 location_t for_loc;
8941 VEC(tree,gc) *for_block = make_tree_vector ();
8943 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8944 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8945 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8947 gcc_assert (collapse >= 1);
8949 declv = make_tree_vec (collapse);
8950 initv = make_tree_vec (collapse);
8951 condv = make_tree_vec (collapse);
8952 incrv = make_tree_vec (collapse);
8954 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8956 c_parser_error (parser, "for statement expected");
8957 return NULL;
8959 for_loc = c_parser_peek_token (parser)->location;
8960 c_parser_consume_token (parser);
8962 for (i = 0; i < collapse; i++)
8964 int bracecount = 0;
8966 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8967 goto pop_scopes;
8969 /* Parse the initialization declaration or expression. */
8970 if (c_parser_next_token_starts_declaration (parser))
8972 if (i > 0)
8973 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
8974 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
8975 decl = check_for_loop_decls (for_loc, flag_isoc99);
8976 if (decl == NULL)
8977 goto error_init;
8978 if (DECL_INITIAL (decl) == error_mark_node)
8979 decl = error_mark_node;
8980 init = decl;
8982 else if (c_parser_next_token_is (parser, CPP_NAME)
8983 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
8985 struct c_expr decl_exp;
8986 struct c_expr init_exp;
8987 location_t init_loc;
8989 decl_exp = c_parser_postfix_expression (parser);
8990 decl = decl_exp.value;
8992 c_parser_require (parser, CPP_EQ, "expected %<=%>");
8994 init_loc = c_parser_peek_token (parser)->location;
8995 init_exp = c_parser_expr_no_commas (parser, NULL);
8996 init_exp = default_function_array_read_conversion (init_loc,
8997 init_exp);
8998 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
8999 NOP_EXPR, init_loc, init_exp.value,
9000 init_exp.original_type);
9001 init = c_process_expr_stmt (init_loc, init);
9003 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9005 else
9007 error_init:
9008 c_parser_error (parser,
9009 "expected iteration declaration or initialization");
9010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9011 "expected %<)%>");
9012 fail = true;
9013 goto parse_next;
9016 /* Parse the loop condition. */
9017 cond = NULL_TREE;
9018 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9020 location_t cond_loc = c_parser_peek_token (parser)->location;
9021 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9023 cond = cond_expr.value;
9024 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9025 cond = c_fully_fold (cond, false, NULL);
9026 switch (cond_expr.original_code)
9028 case GT_EXPR:
9029 case GE_EXPR:
9030 case LT_EXPR:
9031 case LE_EXPR:
9032 break;
9033 default:
9034 /* Can't be cond = error_mark_node, because we want to preserve
9035 the location until c_finish_omp_for. */
9036 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9037 break;
9039 protected_set_expr_location (cond, cond_loc);
9041 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9043 /* Parse the increment expression. */
9044 incr = NULL_TREE;
9045 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9047 location_t incr_loc = c_parser_peek_token (parser)->location;
9049 incr = c_process_expr_stmt (incr_loc,
9050 c_parser_expression (parser).value);
9052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9054 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9055 fail = true;
9056 else
9058 TREE_VEC_ELT (declv, i) = decl;
9059 TREE_VEC_ELT (initv, i) = init;
9060 TREE_VEC_ELT (condv, i) = cond;
9061 TREE_VEC_ELT (incrv, i) = incr;
9064 parse_next:
9065 if (i == collapse - 1)
9066 break;
9068 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9069 in between the collapsed for loops to be still considered perfectly
9070 nested. Hopefully the final version clarifies this.
9071 For now handle (multiple) {'s and empty statements. */
9074 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9076 c_parser_consume_token (parser);
9077 break;
9079 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9081 c_parser_consume_token (parser);
9082 bracecount++;
9084 else if (bracecount
9085 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9086 c_parser_consume_token (parser);
9087 else
9089 c_parser_error (parser, "not enough perfectly nested loops");
9090 if (bracecount)
9092 open_brace_parsed = true;
9093 bracecount--;
9095 fail = true;
9096 collapse = 0;
9097 break;
9100 while (1);
9102 nbraces += bracecount;
9105 save_break = c_break_label;
9106 c_break_label = size_one_node;
9107 save_cont = c_cont_label;
9108 c_cont_label = NULL_TREE;
9109 body = push_stmt_list ();
9111 if (open_brace_parsed)
9113 location_t here = c_parser_peek_token (parser)->location;
9114 stmt = c_begin_compound_stmt (true);
9115 c_parser_compound_statement_nostart (parser);
9116 add_stmt (c_end_compound_stmt (here, stmt, true));
9118 else
9119 add_stmt (c_parser_c99_block_statement (parser));
9120 if (c_cont_label)
9122 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9123 SET_EXPR_LOCATION (t, loc);
9124 add_stmt (t);
9127 body = pop_stmt_list (body);
9128 c_break_label = save_break;
9129 c_cont_label = save_cont;
9131 while (nbraces)
9133 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9135 c_parser_consume_token (parser);
9136 nbraces--;
9138 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9139 c_parser_consume_token (parser);
9140 else
9142 c_parser_error (parser, "collapsed loops not perfectly nested");
9143 while (nbraces)
9145 location_t here = c_parser_peek_token (parser)->location;
9146 stmt = c_begin_compound_stmt (true);
9147 add_stmt (body);
9148 c_parser_compound_statement_nostart (parser);
9149 body = c_end_compound_stmt (here, stmt, true);
9150 nbraces--;
9152 goto pop_scopes;
9156 /* Only bother calling c_finish_omp_for if we haven't already generated
9157 an error from the initialization parsing. */
9158 if (!fail)
9160 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9161 if (stmt)
9163 if (par_clauses != NULL)
9165 tree *c;
9166 for (c = par_clauses; *c ; )
9167 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9168 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9169 c = &OMP_CLAUSE_CHAIN (*c);
9170 else
9172 for (i = 0; i < collapse; i++)
9173 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9174 break;
9175 if (i == collapse)
9176 c = &OMP_CLAUSE_CHAIN (*c);
9177 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9179 error_at (loc,
9180 "iteration variable %qD should not be firstprivate",
9181 OMP_CLAUSE_DECL (*c));
9182 *c = OMP_CLAUSE_CHAIN (*c);
9184 else
9186 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9187 change it to shared (decl) in
9188 OMP_PARALLEL_CLAUSES. */
9189 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9190 OMP_CLAUSE_LASTPRIVATE);
9191 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9192 OMP_CLAUSE_CHAIN (l) = clauses;
9193 clauses = l;
9194 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9198 OMP_FOR_CLAUSES (stmt) = clauses;
9200 ret = stmt;
9202 pop_scopes:
9203 while (!VEC_empty (tree, for_block))
9205 /* FIXME diagnostics: LOC below should be the actual location of
9206 this particular for block. We need to build a list of
9207 locations to go along with FOR_BLOCK. */
9208 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9209 add_stmt (stmt);
9211 release_tree_vector (for_block);
9212 return ret;
9215 /* OpenMP 2.5:
9216 #pragma omp for for-clause[optseq] new-line
9217 for-loop
9219 LOC is the location of the #pragma token.
9222 #define OMP_FOR_CLAUSE_MASK \
9223 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9224 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9225 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9226 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9227 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9228 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9229 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9230 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9232 static tree
9233 c_parser_omp_for (location_t loc, c_parser *parser)
9235 tree block, clauses, ret;
9237 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9238 "#pragma omp for");
9240 block = c_begin_compound_stmt (true);
9241 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9242 block = c_end_compound_stmt (loc, block, true);
9243 add_stmt (block);
9245 return ret;
9248 /* OpenMP 2.5:
9249 # pragma omp master new-line
9250 structured-block
9252 LOC is the location of the #pragma token.
9255 static tree
9256 c_parser_omp_master (location_t loc, c_parser *parser)
9258 c_parser_skip_to_pragma_eol (parser);
9259 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9262 /* OpenMP 2.5:
9263 # pragma omp ordered new-line
9264 structured-block
9266 LOC is the location of the #pragma itself.
9269 static tree
9270 c_parser_omp_ordered (location_t loc, c_parser *parser)
9272 c_parser_skip_to_pragma_eol (parser);
9273 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9276 /* OpenMP 2.5:
9278 section-scope:
9279 { section-sequence }
9281 section-sequence:
9282 section-directive[opt] structured-block
9283 section-sequence section-directive structured-block
9285 SECTIONS_LOC is the location of the #pragma omp sections. */
9287 static tree
9288 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9290 tree stmt, substmt;
9291 bool error_suppress = false;
9292 location_t loc;
9294 loc = c_parser_peek_token (parser)->location;
9295 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9297 /* Avoid skipping until the end of the block. */
9298 parser->error = false;
9299 return NULL_TREE;
9302 stmt = push_stmt_list ();
9304 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9306 substmt = push_stmt_list ();
9308 while (1)
9310 c_parser_statement (parser);
9312 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9313 break;
9314 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9315 break;
9316 if (c_parser_next_token_is (parser, CPP_EOF))
9317 break;
9320 substmt = pop_stmt_list (substmt);
9321 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9322 SET_EXPR_LOCATION (substmt, loc);
9323 add_stmt (substmt);
9326 while (1)
9328 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9329 break;
9330 if (c_parser_next_token_is (parser, CPP_EOF))
9331 break;
9333 loc = c_parser_peek_token (parser)->location;
9334 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9336 c_parser_consume_pragma (parser);
9337 c_parser_skip_to_pragma_eol (parser);
9338 error_suppress = false;
9340 else if (!error_suppress)
9342 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9343 error_suppress = true;
9346 substmt = c_parser_omp_structured_block (parser);
9347 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9348 SET_EXPR_LOCATION (substmt, loc);
9349 add_stmt (substmt);
9351 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9352 "expected %<#pragma omp section%> or %<}%>");
9354 substmt = pop_stmt_list (stmt);
9356 stmt = make_node (OMP_SECTIONS);
9357 SET_EXPR_LOCATION (stmt, sections_loc);
9358 TREE_TYPE (stmt) = void_type_node;
9359 OMP_SECTIONS_BODY (stmt) = substmt;
9361 return add_stmt (stmt);
9364 /* OpenMP 2.5:
9365 # pragma omp sections sections-clause[optseq] newline
9366 sections-scope
9368 LOC is the location of the #pragma token.
9371 #define OMP_SECTIONS_CLAUSE_MASK \
9372 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9373 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9374 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9375 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9376 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9378 static tree
9379 c_parser_omp_sections (location_t loc, c_parser *parser)
9381 tree block, clauses, ret;
9383 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9384 "#pragma omp sections");
9386 block = c_begin_compound_stmt (true);
9387 ret = c_parser_omp_sections_scope (loc, parser);
9388 if (ret)
9389 OMP_SECTIONS_CLAUSES (ret) = clauses;
9390 block = c_end_compound_stmt (loc, block, true);
9391 add_stmt (block);
9393 return ret;
9396 /* OpenMP 2.5:
9397 # pragma parallel parallel-clause new-line
9398 # pragma parallel for parallel-for-clause new-line
9399 # pragma parallel sections parallel-sections-clause new-line
9401 LOC is the location of the #pragma token.
9404 #define OMP_PARALLEL_CLAUSE_MASK \
9405 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9406 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9407 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9408 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9409 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9410 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9411 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9412 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9414 static tree
9415 c_parser_omp_parallel (location_t loc, c_parser *parser)
9417 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9418 const char *p_name = "#pragma omp parallel";
9419 tree stmt, clauses, par_clause, ws_clause, block;
9420 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9422 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9424 c_parser_consume_token (parser);
9425 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9426 p_name = "#pragma omp parallel for";
9427 mask |= OMP_FOR_CLAUSE_MASK;
9428 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9430 else if (c_parser_next_token_is (parser, CPP_NAME))
9432 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9433 if (strcmp (p, "sections") == 0)
9435 c_parser_consume_token (parser);
9436 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9437 p_name = "#pragma omp parallel sections";
9438 mask |= OMP_SECTIONS_CLAUSE_MASK;
9439 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9443 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9445 switch (p_kind)
9447 case PRAGMA_OMP_PARALLEL:
9448 block = c_begin_omp_parallel ();
9449 c_parser_statement (parser);
9450 stmt = c_finish_omp_parallel (loc, clauses, block);
9451 break;
9453 case PRAGMA_OMP_PARALLEL_FOR:
9454 block = c_begin_omp_parallel ();
9455 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9456 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9457 stmt = c_finish_omp_parallel (loc, par_clause, block);
9458 OMP_PARALLEL_COMBINED (stmt) = 1;
9459 break;
9461 case PRAGMA_OMP_PARALLEL_SECTIONS:
9462 block = c_begin_omp_parallel ();
9463 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9464 stmt = c_parser_omp_sections_scope (loc, parser);
9465 if (stmt)
9466 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9467 stmt = c_finish_omp_parallel (loc, par_clause, block);
9468 OMP_PARALLEL_COMBINED (stmt) = 1;
9469 break;
9471 default:
9472 gcc_unreachable ();
9475 return stmt;
9478 /* OpenMP 2.5:
9479 # pragma omp single single-clause[optseq] new-line
9480 structured-block
9482 LOC is the location of the #pragma.
9485 #define OMP_SINGLE_CLAUSE_MASK \
9486 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9487 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9488 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9489 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9491 static tree
9492 c_parser_omp_single (location_t loc, c_parser *parser)
9494 tree stmt = make_node (OMP_SINGLE);
9495 SET_EXPR_LOCATION (stmt, loc);
9496 TREE_TYPE (stmt) = void_type_node;
9498 OMP_SINGLE_CLAUSES (stmt)
9499 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9500 "#pragma omp single");
9501 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9503 return add_stmt (stmt);
9506 /* OpenMP 3.0:
9507 # pragma omp task task-clause[optseq] new-line
9509 LOC is the location of the #pragma.
9512 #define OMP_TASK_CLAUSE_MASK \
9513 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9514 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9515 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9516 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9517 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9518 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9520 static tree
9521 c_parser_omp_task (location_t loc, c_parser *parser)
9523 tree clauses, block;
9525 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9526 "#pragma omp task");
9528 block = c_begin_omp_task ();
9529 c_parser_statement (parser);
9530 return c_finish_omp_task (loc, clauses, block);
9533 /* OpenMP 3.0:
9534 # pragma omp taskwait new-line
9537 static void
9538 c_parser_omp_taskwait (c_parser *parser)
9540 location_t loc = c_parser_peek_token (parser)->location;
9541 c_parser_consume_pragma (parser);
9542 c_parser_skip_to_pragma_eol (parser);
9544 c_finish_omp_taskwait (loc);
9547 /* Main entry point to parsing most OpenMP pragmas. */
9549 static void
9550 c_parser_omp_construct (c_parser *parser)
9552 enum pragma_kind p_kind;
9553 location_t loc;
9554 tree stmt;
9556 loc = c_parser_peek_token (parser)->location;
9557 p_kind = c_parser_peek_token (parser)->pragma_kind;
9558 c_parser_consume_pragma (parser);
9560 switch (p_kind)
9562 case PRAGMA_OMP_ATOMIC:
9563 c_parser_omp_atomic (loc, parser);
9564 return;
9565 case PRAGMA_OMP_CRITICAL:
9566 stmt = c_parser_omp_critical (loc, parser);
9567 break;
9568 case PRAGMA_OMP_FOR:
9569 stmt = c_parser_omp_for (loc, parser);
9570 break;
9571 case PRAGMA_OMP_MASTER:
9572 stmt = c_parser_omp_master (loc, parser);
9573 break;
9574 case PRAGMA_OMP_ORDERED:
9575 stmt = c_parser_omp_ordered (loc, parser);
9576 break;
9577 case PRAGMA_OMP_PARALLEL:
9578 stmt = c_parser_omp_parallel (loc, parser);
9579 break;
9580 case PRAGMA_OMP_SECTIONS:
9581 stmt = c_parser_omp_sections (loc, parser);
9582 break;
9583 case PRAGMA_OMP_SINGLE:
9584 stmt = c_parser_omp_single (loc, parser);
9585 break;
9586 case PRAGMA_OMP_TASK:
9587 stmt = c_parser_omp_task (loc, parser);
9588 break;
9589 default:
9590 gcc_unreachable ();
9593 if (stmt)
9594 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9598 /* OpenMP 2.5:
9599 # pragma omp threadprivate (variable-list) */
9601 static void
9602 c_parser_omp_threadprivate (c_parser *parser)
9604 tree vars, t;
9605 location_t loc;
9607 c_parser_consume_pragma (parser);
9608 loc = c_parser_peek_token (parser)->location;
9609 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9611 /* Mark every variable in VARS to be assigned thread local storage. */
9612 for (t = vars; t; t = TREE_CHAIN (t))
9614 tree v = TREE_PURPOSE (t);
9616 /* FIXME diagnostics: Ideally we should keep individual
9617 locations for all the variables in the var list to make the
9618 following errors more precise. Perhaps
9619 c_parser_omp_var_list_parens() should construct a list of
9620 locations to go along with the var list. */
9622 /* If V had already been marked threadprivate, it doesn't matter
9623 whether it had been used prior to this point. */
9624 if (TREE_CODE (v) != VAR_DECL)
9625 error_at (loc, "%qD is not a variable", v);
9626 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9627 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9628 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9629 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9630 else if (TREE_TYPE (v) == error_mark_node)
9632 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9633 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9634 else
9636 if (! DECL_THREAD_LOCAL_P (v))
9638 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9639 /* If rtl has been already set for this var, call
9640 make_decl_rtl once again, so that encode_section_info
9641 has a chance to look at the new decl flags. */
9642 if (DECL_RTL_SET_P (v))
9643 make_decl_rtl (v);
9645 C_DECL_THREADPRIVATE_P (v) = 1;
9649 c_parser_skip_to_pragma_eol (parser);
9653 /* Parse a single source file. */
9655 void
9656 c_parse_file (void)
9658 /* Use local storage to begin. If the first token is a pragma, parse it.
9659 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9660 which will cause garbage collection. */
9661 c_parser tparser;
9663 memset (&tparser, 0, sizeof tparser);
9664 the_parser = &tparser;
9666 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9667 c_parser_pragma_pch_preprocess (&tparser);
9669 the_parser = ggc_alloc_c_parser ();
9670 *the_parser = tparser;
9672 /* Initialize EH, if we've been told to do so. */
9673 if (flag_exceptions)
9674 using_eh_for_cleanups ();
9676 c_parser_translation_unit (the_parser);
9677 the_parser = NULL;
9680 #include "gt-c-parser.h"