2009-04-16 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / c-parser.c
blob9b3ace515a9b7dd5df97a0d5493f42aa2982a62f
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
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"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "langhooks.h"
47 #include "input.h"
48 #include "cpplib.h"
49 #include "timevar.h"
50 #include "c-pragma.h"
51 #include "c-tree.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "toplev.h"
55 #include "ggc.h"
56 #include "c-common.h"
57 #include "vec.h"
58 #include "target.h"
59 #include "cgraph.h"
60 #include "plugin.h"
63 /* Initialization routine for this file. */
65 void
66 c_parse_init (void)
68 /* The only initialization required is of the reserved word
69 identifiers. */
70 unsigned int i;
71 tree id;
72 int mask = 0;
74 mask |= D_CXXONLY;
75 if (!flag_isoc99)
76 mask |= D_C99;
77 if (flag_no_asm)
79 mask |= D_ASM | D_EXT;
80 if (!flag_isoc99)
81 mask |= D_EXT89;
83 if (!c_dialect_objc ())
84 mask |= D_OBJC | D_CXX_OBJC;
86 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
87 for (i = 0; i < num_c_common_reswords; i++)
89 /* If a keyword is disabled, do not enter it into the table
90 and so create a canonical spelling that isn't a keyword. */
91 if (c_common_reswords[i].disable & mask)
93 if (warn_cxx_compat
94 && (c_common_reswords[i].disable & D_CXXWARN))
96 id = get_identifier (c_common_reswords[i].word);
97 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
98 C_IS_RESERVED_WORD (id) = 1;
100 continue;
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, c_common_reswords[i].rid);
105 C_IS_RESERVED_WORD (id) = 1;
106 ridpointers [(int) c_common_reswords[i].rid] = id;
110 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
111 and the C parser. Unlike the C++ lexer, the parser structure
112 stores the lexer information instead of using a separate structure.
113 Identifiers are separated into ordinary identifiers, type names,
114 keywords and some other Objective-C types of identifiers, and some
115 look-ahead is maintained.
117 ??? It might be a good idea to lex the whole file up front (as for
118 C++). It would then be possible to share more of the C and C++
119 lexer code, if desired. */
121 /* The following local token type is used. */
123 /* A keyword. */
124 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
126 /* More information about the type of a CPP_NAME token. */
127 typedef enum c_id_kind {
128 /* An ordinary identifier. */
129 C_ID_ID,
130 /* An identifier declared as a typedef name. */
131 C_ID_TYPENAME,
132 /* An identifier declared as an Objective-C class name. */
133 C_ID_CLASSNAME,
134 /* Not an identifier. */
135 C_ID_NONE
136 } c_id_kind;
138 /* A single C token after string literal concatenation and conversion
139 of preprocessing tokens to tokens. */
140 typedef struct c_token GTY (())
142 /* The kind of token. */
143 ENUM_BITFIELD (cpp_ttype) type : 8;
144 /* If this token is a CPP_NAME, this value indicates whether also
145 declared as some kind of type. Otherwise, it is C_ID_NONE. */
146 ENUM_BITFIELD (c_id_kind) id_kind : 8;
147 /* If this token is a keyword, this value indicates which keyword.
148 Otherwise, this value is RID_MAX. */
149 ENUM_BITFIELD (rid) keyword : 8;
150 /* If this token is a CPP_PRAGMA, this indicates the pragma that
151 was seen. Otherwise it is PRAGMA_NONE. */
152 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
153 /* The value associated with this token, if any. */
154 tree value;
155 /* The location at which this token was found. */
156 location_t location;
157 } c_token;
159 /* A parser structure recording information about the state and
160 context of parsing. Includes lexer information with up to two
161 tokens of look-ahead; more are not needed for C. */
162 typedef struct c_parser GTY(())
164 /* The look-ahead tokens. */
165 c_token tokens[2];
166 /* How many look-ahead tokens are available (0, 1 or 2). */
167 short tokens_avail;
168 /* True if a syntax error is being recovered from; false otherwise.
169 c_parser_error sets this flag. It should clear this flag when
170 enough tokens have been consumed to recover from the error. */
171 BOOL_BITFIELD error : 1;
172 /* True if we're processing a pragma, and shouldn't automatically
173 consume CPP_PRAGMA_EOL. */
174 BOOL_BITFIELD in_pragma : 1;
175 /* True if we're parsing the outermost block of an if statement. */
176 BOOL_BITFIELD in_if_block : 1;
177 /* True if we want to lex an untranslated string. */
178 BOOL_BITFIELD lex_untranslated_string : 1;
179 /* Objective-C specific parser/lexer information. */
180 BOOL_BITFIELD objc_pq_context : 1;
181 /* The following flag is needed to contextualize Objective-C lexical
182 analysis. In some cases (e.g., 'int NSObject;'), it is
183 undesirable to bind an identifier to an Objective-C class, even
184 if a class with that name exists. */
185 BOOL_BITFIELD objc_need_raw_identifier : 1;
186 } c_parser;
189 /* The actual parser and external interface. ??? Does this need to be
190 garbage-collected? */
192 static GTY (()) c_parser *the_parser;
195 /* Read in and lex a single token, storing it in *TOKEN. */
197 static void
198 c_lex_one_token (c_parser *parser, c_token *token)
200 timevar_push (TV_LEX);
202 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
203 (parser->lex_untranslated_string
204 ? C_LEX_STRING_NO_TRANSLATE : 0));
205 token->id_kind = C_ID_NONE;
206 token->keyword = RID_MAX;
207 token->pragma_kind = PRAGMA_NONE;
209 switch (token->type)
211 case CPP_NAME:
213 tree decl;
215 bool objc_force_identifier = parser->objc_need_raw_identifier;
216 if (c_dialect_objc ())
217 parser->objc_need_raw_identifier = false;
219 if (C_IS_RESERVED_WORD (token->value))
221 enum rid rid_code = C_RID_CODE (token->value);
223 if (rid_code == RID_CXX_COMPAT_WARN)
225 warning_at (token->location,
226 OPT_Wc___compat,
227 "identifier %qs conflicts with C++ keyword",
228 IDENTIFIER_POINTER (token->value));
230 else if (c_dialect_objc ())
232 if (!objc_is_reserved_word (token->value)
233 && (!OBJC_IS_PQ_KEYWORD (rid_code)
234 || parser->objc_pq_context))
236 /* Return the canonical spelling for this keyword. */
237 token->value = ridpointers[(int) rid_code];
238 token->type = CPP_KEYWORD;
239 token->keyword = rid_code;
240 break;
243 else
245 token->type = CPP_KEYWORD;
246 token->keyword = rid_code;
247 break;
251 decl = lookup_name (token->value);
252 if (decl)
254 if (TREE_CODE (decl) == TYPE_DECL)
256 token->id_kind = C_ID_TYPENAME;
257 break;
260 else if (c_dialect_objc ())
262 tree objc_interface_decl = objc_is_class_name (token->value);
263 /* Objective-C class names are in the same namespace as
264 variables and typedefs, and hence are shadowed by local
265 declarations. */
266 if (objc_interface_decl
267 && (global_bindings_p ()
268 || (!objc_force_identifier && !decl)))
270 token->value = objc_interface_decl;
271 token->id_kind = C_ID_CLASSNAME;
272 break;
275 token->id_kind = C_ID_ID;
277 break;
278 case CPP_AT_NAME:
279 /* This only happens in Objective-C; it must be a keyword. */
280 token->type = CPP_KEYWORD;
281 token->keyword = C_RID_CODE (token->value);
282 break;
283 case CPP_COLON:
284 case CPP_COMMA:
285 case CPP_CLOSE_PAREN:
286 case CPP_SEMICOLON:
287 /* These tokens may affect the interpretation of any identifiers
288 following, if doing Objective-C. */
289 if (c_dialect_objc ())
290 parser->objc_need_raw_identifier = false;
291 break;
292 case CPP_PRAGMA:
293 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
294 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
295 token->value = NULL;
296 break;
297 default:
298 break;
300 timevar_pop (TV_LEX);
303 /* Return a pointer to the next token from PARSER, reading it in if
304 necessary. */
306 static inline c_token *
307 c_parser_peek_token (c_parser *parser)
309 if (parser->tokens_avail == 0)
311 c_lex_one_token (parser, &parser->tokens[0]);
312 parser->tokens_avail = 1;
314 return &parser->tokens[0];
317 /* Return true if the next token from PARSER has the indicated
318 TYPE. */
320 static inline bool
321 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
323 return c_parser_peek_token (parser)->type == type;
326 /* Return true if the next token from PARSER does not have the
327 indicated TYPE. */
329 static inline bool
330 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
332 return !c_parser_next_token_is (parser, type);
335 /* Return true if the next token from PARSER is the indicated
336 KEYWORD. */
338 static inline bool
339 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
341 return c_parser_peek_token (parser)->keyword == keyword;
344 /* Return true if TOKEN can start a type name,
345 false otherwise. */
346 static bool
347 c_token_starts_typename (c_token *token)
349 switch (token->type)
351 case CPP_NAME:
352 switch (token->id_kind)
354 case C_ID_ID:
355 return false;
356 case C_ID_TYPENAME:
357 return true;
358 case C_ID_CLASSNAME:
359 gcc_assert (c_dialect_objc ());
360 return true;
361 default:
362 gcc_unreachable ();
364 case CPP_KEYWORD:
365 switch (token->keyword)
367 case RID_UNSIGNED:
368 case RID_LONG:
369 case RID_SHORT:
370 case RID_SIGNED:
371 case RID_COMPLEX:
372 case RID_INT:
373 case RID_CHAR:
374 case RID_FLOAT:
375 case RID_DOUBLE:
376 case RID_VOID:
377 case RID_DFLOAT32:
378 case RID_DFLOAT64:
379 case RID_DFLOAT128:
380 case RID_BOOL:
381 case RID_ENUM:
382 case RID_STRUCT:
383 case RID_UNION:
384 case RID_TYPEOF:
385 case RID_CONST:
386 case RID_VOLATILE:
387 case RID_RESTRICT:
388 case RID_ATTRIBUTE:
389 case RID_FRACT:
390 case RID_ACCUM:
391 case RID_SAT:
392 return true;
393 default:
394 return false;
396 case CPP_LESS:
397 if (c_dialect_objc ())
398 return true;
399 return false;
400 default:
401 return false;
405 /* Return true if the next token from PARSER can start a type name,
406 false otherwise. */
407 static inline bool
408 c_parser_next_token_starts_typename (c_parser *parser)
410 c_token *token = c_parser_peek_token (parser);
411 return c_token_starts_typename (token);
414 /* Return true if TOKEN can start declaration specifiers, false
415 otherwise. */
416 static bool
417 c_token_starts_declspecs (c_token *token)
419 switch (token->type)
421 case CPP_NAME:
422 switch (token->id_kind)
424 case C_ID_ID:
425 return false;
426 case C_ID_TYPENAME:
427 return true;
428 case C_ID_CLASSNAME:
429 gcc_assert (c_dialect_objc ());
430 return true;
431 default:
432 gcc_unreachable ();
434 case CPP_KEYWORD:
435 switch (token->keyword)
437 case RID_STATIC:
438 case RID_EXTERN:
439 case RID_REGISTER:
440 case RID_TYPEDEF:
441 case RID_INLINE:
442 case RID_AUTO:
443 case RID_THREAD:
444 case RID_UNSIGNED:
445 case RID_LONG:
446 case RID_SHORT:
447 case RID_SIGNED:
448 case RID_COMPLEX:
449 case RID_INT:
450 case RID_CHAR:
451 case RID_FLOAT:
452 case RID_DOUBLE:
453 case RID_VOID:
454 case RID_DFLOAT32:
455 case RID_DFLOAT64:
456 case RID_DFLOAT128:
457 case RID_BOOL:
458 case RID_ENUM:
459 case RID_STRUCT:
460 case RID_UNION:
461 case RID_TYPEOF:
462 case RID_CONST:
463 case RID_VOLATILE:
464 case RID_RESTRICT:
465 case RID_ATTRIBUTE:
466 case RID_FRACT:
467 case RID_ACCUM:
468 case RID_SAT:
469 return true;
470 default:
471 return false;
473 case CPP_LESS:
474 if (c_dialect_objc ())
475 return true;
476 return false;
477 default:
478 return false;
482 /* Return true if the next token from PARSER can start declaration
483 specifiers, false otherwise. */
484 static inline bool
485 c_parser_next_token_starts_declspecs (c_parser *parser)
487 c_token *token = c_parser_peek_token (parser);
488 return c_token_starts_declspecs (token);
491 /* Return a pointer to the next-but-one token from PARSER, reading it
492 in if necessary. The next token is already read in. */
494 static c_token *
495 c_parser_peek_2nd_token (c_parser *parser)
497 if (parser->tokens_avail >= 2)
498 return &parser->tokens[1];
499 gcc_assert (parser->tokens_avail == 1);
500 gcc_assert (parser->tokens[0].type != CPP_EOF);
501 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
502 c_lex_one_token (parser, &parser->tokens[1]);
503 parser->tokens_avail = 2;
504 return &parser->tokens[1];
507 /* Consume the next token from PARSER. */
509 static void
510 c_parser_consume_token (c_parser *parser)
512 gcc_assert (parser->tokens_avail >= 1);
513 gcc_assert (parser->tokens[0].type != CPP_EOF);
514 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
515 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
516 if (parser->tokens_avail == 2)
517 parser->tokens[0] = parser->tokens[1];
518 parser->tokens_avail--;
521 /* Expect the current token to be a #pragma. Consume it and remember
522 that we've begun parsing a pragma. */
524 static void
525 c_parser_consume_pragma (c_parser *parser)
527 gcc_assert (!parser->in_pragma);
528 gcc_assert (parser->tokens_avail >= 1);
529 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
530 if (parser->tokens_avail == 2)
531 parser->tokens[0] = parser->tokens[1];
532 parser->tokens_avail--;
533 parser->in_pragma = true;
536 /* Update the globals input_location and in_system_header from
537 TOKEN. */
538 static inline void
539 c_parser_set_source_position_from_token (c_token *token)
541 if (token->type != CPP_EOF)
543 input_location = token->location;
547 /* Issue a diagnostic of the form
548 FILE:LINE: MESSAGE before TOKEN
549 where TOKEN is the next token in the input stream of PARSER.
550 MESSAGE (specified by the caller) is usually of the form "expected
551 OTHER-TOKEN".
553 Do not issue a diagnostic if still recovering from an error.
555 ??? This is taken from the C++ parser, but building up messages in
556 this way is not i18n-friendly and some other approach should be
557 used. */
559 static void
560 c_parser_error (c_parser *parser, const char *gmsgid)
562 c_token *token = c_parser_peek_token (parser);
563 if (parser->error)
564 return;
565 parser->error = true;
566 if (!gmsgid)
567 return;
568 /* This diagnostic makes more sense if it is tagged to the line of
569 the token we just peeked at. */
570 c_parser_set_source_position_from_token (token);
571 c_parse_error (gmsgid,
572 /* Because c_parse_error does not understand
573 CPP_KEYWORD, keywords are treated like
574 identifiers. */
575 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
576 token->value);
579 /* If the next token is of the indicated TYPE, consume it. Otherwise,
580 issue the error MSGID. If MSGID is NULL then a message has already
581 been produced and no message will be produced this time. Returns
582 true if found, false otherwise. */
584 static bool
585 c_parser_require (c_parser *parser,
586 enum cpp_ttype type,
587 const char *msgid)
589 if (c_parser_next_token_is (parser, type))
591 c_parser_consume_token (parser);
592 return true;
594 else
596 c_parser_error (parser, msgid);
597 return false;
601 /* If the next token is the indicated keyword, consume it. Otherwise,
602 issue the error MSGID. Returns true if found, false otherwise. */
604 static bool
605 c_parser_require_keyword (c_parser *parser,
606 enum rid keyword,
607 const char *msgid)
609 if (c_parser_next_token_is_keyword (parser, keyword))
611 c_parser_consume_token (parser);
612 return true;
614 else
616 c_parser_error (parser, msgid);
617 return false;
621 /* Like c_parser_require, except that tokens will be skipped until the
622 desired token is found. An error message is still produced if the
623 next token is not as expected. If MSGID is NULL then a message has
624 already been produced and no message will be produced this
625 time. */
627 static void
628 c_parser_skip_until_found (c_parser *parser,
629 enum cpp_ttype type,
630 const char *msgid)
632 unsigned nesting_depth = 0;
634 if (c_parser_require (parser, type, msgid))
635 return;
637 /* Skip tokens until the desired token is found. */
638 while (true)
640 /* Peek at the next token. */
641 c_token *token = c_parser_peek_token (parser);
642 /* If we've reached the token we want, consume it and stop. */
643 if (token->type == type && !nesting_depth)
645 c_parser_consume_token (parser);
646 break;
649 /* If we've run out of tokens, stop. */
650 if (token->type == CPP_EOF)
651 return;
652 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
653 return;
654 if (token->type == CPP_OPEN_BRACE
655 || token->type == CPP_OPEN_PAREN
656 || token->type == CPP_OPEN_SQUARE)
657 ++nesting_depth;
658 else if (token->type == CPP_CLOSE_BRACE
659 || token->type == CPP_CLOSE_PAREN
660 || token->type == CPP_CLOSE_SQUARE)
662 if (nesting_depth-- == 0)
663 break;
665 /* Consume this token. */
666 c_parser_consume_token (parser);
668 parser->error = false;
671 /* Skip tokens until the end of a parameter is found, but do not
672 consume the comma, semicolon or closing delimiter. */
674 static void
675 c_parser_skip_to_end_of_parameter (c_parser *parser)
677 unsigned nesting_depth = 0;
679 while (true)
681 c_token *token = c_parser_peek_token (parser);
682 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
683 && !nesting_depth)
684 break;
685 /* If we've run out of tokens, stop. */
686 if (token->type == CPP_EOF)
687 return;
688 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
689 return;
690 if (token->type == CPP_OPEN_BRACE
691 || token->type == CPP_OPEN_PAREN
692 || token->type == CPP_OPEN_SQUARE)
693 ++nesting_depth;
694 else if (token->type == CPP_CLOSE_BRACE
695 || token->type == CPP_CLOSE_PAREN
696 || token->type == CPP_CLOSE_SQUARE)
698 if (nesting_depth-- == 0)
699 break;
701 /* Consume this token. */
702 c_parser_consume_token (parser);
704 parser->error = false;
707 /* Expect to be at the end of the pragma directive and consume an
708 end of line marker. */
710 static void
711 c_parser_skip_to_pragma_eol (c_parser *parser)
713 gcc_assert (parser->in_pragma);
714 parser->in_pragma = false;
716 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
717 while (true)
719 c_token *token = c_parser_peek_token (parser);
720 if (token->type == CPP_EOF)
721 break;
722 if (token->type == CPP_PRAGMA_EOL)
724 c_parser_consume_token (parser);
725 break;
727 c_parser_consume_token (parser);
730 parser->error = false;
733 /* Skip tokens until we have consumed an entire block, or until we
734 have consumed a non-nested ';'. */
736 static void
737 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
739 unsigned nesting_depth = 0;
740 bool save_error = parser->error;
742 while (true)
744 c_token *token;
746 /* Peek at the next token. */
747 token = c_parser_peek_token (parser);
749 switch (token->type)
751 case CPP_EOF:
752 return;
754 case CPP_PRAGMA_EOL:
755 if (parser->in_pragma)
756 return;
757 break;
759 case CPP_SEMICOLON:
760 /* If the next token is a ';', we have reached the
761 end of the statement. */
762 if (!nesting_depth)
764 /* Consume the ';'. */
765 c_parser_consume_token (parser);
766 goto finished;
768 break;
770 case CPP_CLOSE_BRACE:
771 /* If the next token is a non-nested '}', then we have
772 reached the end of the current block. */
773 if (nesting_depth == 0 || --nesting_depth == 0)
775 c_parser_consume_token (parser);
776 goto finished;
778 break;
780 case CPP_OPEN_BRACE:
781 /* If it the next token is a '{', then we are entering a new
782 block. Consume the entire block. */
783 ++nesting_depth;
784 break;
786 case CPP_PRAGMA:
787 /* If we see a pragma, consume the whole thing at once. We
788 have some safeguards against consuming pragmas willy-nilly.
789 Normally, we'd expect to be here with parser->error set,
790 which disables these safeguards. But it's possible to get
791 here for secondary error recovery, after parser->error has
792 been cleared. */
793 c_parser_consume_pragma (parser);
794 c_parser_skip_to_pragma_eol (parser);
795 parser->error = save_error;
796 continue;
798 default:
799 break;
802 c_parser_consume_token (parser);
805 finished:
806 parser->error = false;
809 /* CPP's options (initialized by c-opts.c). */
810 extern cpp_options *cpp_opts;
812 /* Save the warning flags which are controlled by __extension__. */
814 static inline int
815 disable_extension_diagnostics (void)
817 int ret = (pedantic
818 | (warn_pointer_arith << 1)
819 | (warn_traditional << 2)
820 | (flag_iso << 3)
821 | (warn_long_long << 4)
822 | (cpp_opts->warn_long_long << 5));
823 cpp_opts->pedantic = pedantic = 0;
824 warn_pointer_arith = 0;
825 cpp_opts->warn_traditional = warn_traditional = 0;
826 flag_iso = 0;
827 warn_long_long = 0;
828 cpp_opts->warn_long_long = 0;
829 return ret;
832 /* Restore the warning flags which are controlled by __extension__.
833 FLAGS is the return value from disable_extension_diagnostics. */
835 static inline void
836 restore_extension_diagnostics (int flags)
838 cpp_opts->pedantic = pedantic = flags & 1;
839 warn_pointer_arith = (flags >> 1) & 1;
840 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
841 flag_iso = (flags >> 3) & 1;
842 warn_long_long = (flags >> 4) & 1;
843 cpp_opts->warn_long_long = (flags >> 5) & 1;
846 /* Possibly kinds of declarator to parse. */
847 typedef enum c_dtr_syn {
848 /* A normal declarator with an identifier. */
849 C_DTR_NORMAL,
850 /* An abstract declarator (maybe empty). */
851 C_DTR_ABSTRACT,
852 /* A parameter declarator: may be either, but after a type name does
853 not redeclare a typedef name as an identifier if it can
854 alternatively be interpreted as a typedef name; see DR#009,
855 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
856 following DR#249. For example, given a typedef T, "int T" and
857 "int *T" are valid parameter declarations redeclaring T, while
858 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
859 abstract declarators rather than involving redundant parentheses;
860 the same applies with attributes inside the parentheses before
861 "T". */
862 C_DTR_PARM
863 } c_dtr_syn;
865 static void c_parser_external_declaration (c_parser *);
866 static void c_parser_asm_definition (c_parser *);
867 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
868 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
869 bool);
870 static struct c_typespec c_parser_enum_specifier (c_parser *);
871 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
872 static tree c_parser_struct_declaration (c_parser *);
873 static struct c_typespec c_parser_typeof_specifier (c_parser *);
874 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
875 bool *);
876 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
877 c_dtr_syn, bool *);
878 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
879 bool,
880 struct c_declarator *);
881 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
882 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
883 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
884 static tree c_parser_simple_asm_expr (c_parser *);
885 static tree c_parser_attributes (c_parser *);
886 static struct c_type_name *c_parser_type_name (c_parser *);
887 static struct c_expr c_parser_initializer (c_parser *);
888 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
889 static void c_parser_initelt (c_parser *);
890 static void c_parser_initval (c_parser *, struct c_expr *);
891 static tree c_parser_compound_statement (c_parser *);
892 static void c_parser_compound_statement_nostart (c_parser *);
893 static void c_parser_label (c_parser *);
894 static void c_parser_statement (c_parser *);
895 static void c_parser_statement_after_labels (c_parser *);
896 static void c_parser_if_statement (c_parser *);
897 static void c_parser_switch_statement (c_parser *);
898 static void c_parser_while_statement (c_parser *);
899 static void c_parser_do_statement (c_parser *);
900 static void c_parser_for_statement (c_parser *);
901 static tree c_parser_asm_statement (c_parser *);
902 static tree c_parser_asm_operands (c_parser *, bool);
903 static tree c_parser_asm_clobbers (c_parser *);
904 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
905 static struct c_expr c_parser_conditional_expression (c_parser *,
906 struct c_expr *);
907 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
908 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
909 static struct c_expr c_parser_unary_expression (c_parser *);
910 static struct c_expr c_parser_sizeof_expression (c_parser *);
911 static struct c_expr c_parser_alignof_expression (c_parser *);
912 static struct c_expr c_parser_postfix_expression (c_parser *);
913 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
914 struct c_type_name *);
915 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
916 struct c_expr);
917 static struct c_expr c_parser_expression (c_parser *);
918 static struct c_expr c_parser_expression_conv (c_parser *);
919 static tree c_parser_expr_list (c_parser *, bool, bool);
920 static void c_parser_omp_construct (c_parser *);
921 static void c_parser_omp_threadprivate (c_parser *);
922 static void c_parser_omp_barrier (c_parser *);
923 static void c_parser_omp_flush (c_parser *);
924 static void c_parser_omp_taskwait (c_parser *);
926 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
927 static bool c_parser_pragma (c_parser *, enum pragma_context);
929 /* These Objective-C parser functions are only ever called when
930 compiling Objective-C. */
931 static void c_parser_objc_class_definition (c_parser *);
932 static void c_parser_objc_class_instance_variables (c_parser *);
933 static void c_parser_objc_class_declaration (c_parser *);
934 static void c_parser_objc_alias_declaration (c_parser *);
935 static void c_parser_objc_protocol_definition (c_parser *);
936 static enum tree_code c_parser_objc_method_type (c_parser *);
937 static void c_parser_objc_method_definition (c_parser *);
938 static void c_parser_objc_methodprotolist (c_parser *);
939 static void c_parser_objc_methodproto (c_parser *);
940 static tree c_parser_objc_method_decl (c_parser *);
941 static tree c_parser_objc_type_name (c_parser *);
942 static tree c_parser_objc_protocol_refs (c_parser *);
943 static void c_parser_objc_try_catch_statement (c_parser *);
944 static void c_parser_objc_synchronized_statement (c_parser *);
945 static tree c_parser_objc_selector (c_parser *);
946 static tree c_parser_objc_selector_arg (c_parser *);
947 static tree c_parser_objc_receiver (c_parser *);
948 static tree c_parser_objc_message_args (c_parser *);
949 static tree c_parser_objc_keywordexpr (c_parser *);
951 /* Parse a translation unit (C90 6.7, C99 6.9).
953 translation-unit:
954 external-declarations
956 external-declarations:
957 external-declaration
958 external-declarations external-declaration
960 GNU extensions:
962 translation-unit:
963 empty
966 static void
967 c_parser_translation_unit (c_parser *parser)
969 if (c_parser_next_token_is (parser, CPP_EOF))
971 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
972 "ISO C forbids an empty translation unit");
974 else
976 void *obstack_position = obstack_alloc (&parser_obstack, 0);
979 ggc_collect ();
980 c_parser_external_declaration (parser);
981 obstack_free (&parser_obstack, obstack_position);
983 while (c_parser_next_token_is_not (parser, CPP_EOF));
987 /* Parse an external declaration (C90 6.7, C99 6.9).
989 external-declaration:
990 function-definition
991 declaration
993 GNU extensions:
995 external-declaration:
996 asm-definition
998 __extension__ external-declaration
1000 Objective-C:
1002 external-declaration:
1003 objc-class-definition
1004 objc-class-declaration
1005 objc-alias-declaration
1006 objc-protocol-definition
1007 objc-method-definition
1008 @end
1011 static void
1012 c_parser_external_declaration (c_parser *parser)
1014 int ext;
1015 switch (c_parser_peek_token (parser)->type)
1017 case CPP_KEYWORD:
1018 switch (c_parser_peek_token (parser)->keyword)
1020 case RID_EXTENSION:
1021 ext = disable_extension_diagnostics ();
1022 c_parser_consume_token (parser);
1023 c_parser_external_declaration (parser);
1024 restore_extension_diagnostics (ext);
1025 break;
1026 case RID_ASM:
1027 c_parser_asm_definition (parser);
1028 break;
1029 case RID_AT_INTERFACE:
1030 case RID_AT_IMPLEMENTATION:
1031 gcc_assert (c_dialect_objc ());
1032 c_parser_objc_class_definition (parser);
1033 break;
1034 case RID_CLASS:
1035 gcc_assert (c_dialect_objc ());
1036 c_parser_objc_class_declaration (parser);
1037 break;
1038 case RID_AT_ALIAS:
1039 gcc_assert (c_dialect_objc ());
1040 c_parser_objc_alias_declaration (parser);
1041 break;
1042 case RID_AT_PROTOCOL:
1043 gcc_assert (c_dialect_objc ());
1044 c_parser_objc_protocol_definition (parser);
1045 break;
1046 case RID_AT_END:
1047 gcc_assert (c_dialect_objc ());
1048 c_parser_consume_token (parser);
1049 objc_finish_implementation ();
1050 break;
1051 default:
1052 goto decl_or_fndef;
1054 break;
1055 case CPP_SEMICOLON:
1056 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1057 "ISO C does not allow extra %<;%> outside of a function");
1058 c_parser_consume_token (parser);
1059 break;
1060 case CPP_PRAGMA:
1061 c_parser_pragma (parser, pragma_external);
1062 break;
1063 case CPP_PLUS:
1064 case CPP_MINUS:
1065 if (c_dialect_objc ())
1067 c_parser_objc_method_definition (parser);
1068 break;
1070 /* Else fall through, and yield a syntax error trying to parse
1071 as a declaration or function definition. */
1072 default:
1073 decl_or_fndef:
1074 /* A declaration or a function definition. We can only tell
1075 which after parsing the declaration specifiers, if any, and
1076 the first declarator. */
1077 c_parser_declaration_or_fndef (parser, true, true, false, true);
1078 break;
1083 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1084 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1085 accepted; otherwise (old-style parameter declarations) only other
1086 declarations are accepted. If NESTED is true, we are inside a
1087 function or parsing old-style parameter declarations; any functions
1088 encountered are nested functions and declaration specifiers are
1089 required; otherwise we are at top level and functions are normal
1090 functions and declaration specifiers may be optional. If EMPTY_OK
1091 is true, empty declarations are OK (subject to all other
1092 constraints); otherwise (old-style parameter declarations) they are
1093 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1094 may start with attributes; otherwise they may not.
1096 declaration:
1097 declaration-specifiers init-declarator-list[opt] ;
1099 function-definition:
1100 declaration-specifiers[opt] declarator declaration-list[opt]
1101 compound-statement
1103 declaration-list:
1104 declaration
1105 declaration-list declaration
1107 init-declarator-list:
1108 init-declarator
1109 init-declarator-list , init-declarator
1111 init-declarator:
1112 declarator simple-asm-expr[opt] attributes[opt]
1113 declarator simple-asm-expr[opt] attributes[opt] = initializer
1115 GNU extensions:
1117 nested-function-definition:
1118 declaration-specifiers declarator declaration-list[opt]
1119 compound-statement
1121 The simple-asm-expr and attributes are GNU extensions.
1123 This function does not handle __extension__; that is handled in its
1124 callers. ??? Following the old parser, __extension__ may start
1125 external declarations, declarations in functions and declarations
1126 at the start of "for" loops, but not old-style parameter
1127 declarations.
1129 C99 requires declaration specifiers in a function definition; the
1130 absence is diagnosed through the diagnosis of implicit int. In GNU
1131 C we also allow but diagnose declarations without declaration
1132 specifiers, but only at top level (elsewhere they conflict with
1133 other syntax).
1135 OpenMP:
1137 declaration:
1138 threadprivate-directive */
1140 static void
1141 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1142 bool nested, bool start_attr_ok)
1144 struct c_declspecs *specs;
1145 tree prefix_attrs;
1146 tree all_prefix_attrs;
1147 bool diagnosed_no_specs = false;
1148 location_t here = c_parser_peek_token (parser)->location;
1150 specs = build_null_declspecs ();
1151 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1152 if (parser->error)
1154 c_parser_skip_to_end_of_block_or_statement (parser);
1155 return;
1157 if (nested && !specs->declspecs_seen_p)
1159 c_parser_error (parser, "expected declaration specifiers");
1160 c_parser_skip_to_end_of_block_or_statement (parser);
1161 return;
1163 finish_declspecs (specs);
1164 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1166 if (empty_ok)
1167 shadow_tag (specs);
1168 else
1170 shadow_tag_warned (specs, 1);
1171 pedwarn (here, 0, "empty declaration");
1173 c_parser_consume_token (parser);
1174 return;
1176 pending_xref_error ();
1177 prefix_attrs = specs->attrs;
1178 all_prefix_attrs = prefix_attrs;
1179 specs->attrs = NULL_TREE;
1180 while (true)
1182 struct c_declarator *declarator;
1183 bool dummy = false;
1184 tree fnbody;
1185 /* Declaring either one or more declarators (in which case we
1186 should diagnose if there were no declaration specifiers) or a
1187 function definition (in which case the diagnostic for
1188 implicit int suffices). */
1189 declarator = c_parser_declarator (parser, specs->type_seen_p,
1190 C_DTR_NORMAL, &dummy);
1191 if (declarator == NULL)
1193 c_parser_skip_to_end_of_block_or_statement (parser);
1194 return;
1196 if (c_parser_next_token_is (parser, CPP_EQ)
1197 || c_parser_next_token_is (parser, CPP_COMMA)
1198 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1199 || c_parser_next_token_is_keyword (parser, RID_ASM)
1200 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1202 tree asm_name = NULL_TREE;
1203 tree postfix_attrs = NULL_TREE;
1204 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1206 diagnosed_no_specs = true;
1207 pedwarn (here, 0, "data definition has no type or storage class");
1209 /* Having seen a data definition, there cannot now be a
1210 function definition. */
1211 fndef_ok = false;
1212 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1213 asm_name = c_parser_simple_asm_expr (parser);
1214 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1215 postfix_attrs = c_parser_attributes (parser);
1216 if (c_parser_next_token_is (parser, CPP_EQ))
1218 tree d;
1219 struct c_expr init;
1220 c_parser_consume_token (parser);
1221 /* The declaration of the variable is in effect while
1222 its initializer is parsed. */
1223 d = start_decl (declarator, specs, true,
1224 chainon (postfix_attrs, all_prefix_attrs));
1225 if (!d)
1226 d = error_mark_node;
1227 start_init (d, asm_name, global_bindings_p ());
1228 init = c_parser_initializer (parser);
1229 finish_init ();
1230 if (d != error_mark_node)
1232 maybe_warn_string_init (TREE_TYPE (d), init);
1233 finish_decl (d, init.value, asm_name);
1236 else
1238 tree d = start_decl (declarator, specs, false,
1239 chainon (postfix_attrs,
1240 all_prefix_attrs));
1241 if (d)
1242 finish_decl (d, NULL_TREE, asm_name);
1244 if (c_parser_next_token_is (parser, CPP_COMMA))
1246 c_parser_consume_token (parser);
1247 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1248 all_prefix_attrs = chainon (c_parser_attributes (parser),
1249 prefix_attrs);
1250 else
1251 all_prefix_attrs = prefix_attrs;
1252 continue;
1254 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1256 c_parser_consume_token (parser);
1257 return;
1259 else
1261 c_parser_error (parser, "expected %<,%> or %<;%>");
1262 c_parser_skip_to_end_of_block_or_statement (parser);
1263 return;
1266 else if (!fndef_ok)
1268 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1269 "%<asm%> or %<__attribute__%>");
1270 c_parser_skip_to_end_of_block_or_statement (parser);
1271 return;
1273 /* Function definition (nested or otherwise). */
1274 if (nested)
1276 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1277 c_push_function_context ();
1279 if (!start_function (specs, declarator, all_prefix_attrs))
1281 /* This can appear in many cases looking nothing like a
1282 function definition, so we don't give a more specific
1283 error suggesting there was one. */
1284 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1285 "or %<__attribute__%>");
1286 if (nested)
1287 c_pop_function_context ();
1288 break;
1290 /* Parse old-style parameter declarations. ??? Attributes are
1291 not allowed to start declaration specifiers here because of a
1292 syntax conflict between a function declaration with attribute
1293 suffix and a function definition with an attribute prefix on
1294 first old-style parameter declaration. Following the old
1295 parser, they are not accepted on subsequent old-style
1296 parameter declarations either. However, there is no
1297 ambiguity after the first declaration, nor indeed on the
1298 first as long as we don't allow postfix attributes after a
1299 declarator with a nonempty identifier list in a definition;
1300 and postfix attributes have never been accepted here in
1301 function definitions either. */
1302 while (c_parser_next_token_is_not (parser, CPP_EOF)
1303 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1304 c_parser_declaration_or_fndef (parser, false, false, true, false);
1305 store_parm_decls ();
1306 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1307 = c_parser_peek_token (parser)->location;
1308 fnbody = c_parser_compound_statement (parser);
1309 if (nested)
1311 tree decl = current_function_decl;
1312 add_stmt (fnbody);
1313 finish_function ();
1314 c_pop_function_context ();
1315 add_stmt (build_stmt (DECL_EXPR, decl));
1317 else
1319 add_stmt (fnbody);
1320 finish_function ();
1322 break;
1326 /* Parse an asm-definition (asm() outside a function body). This is a
1327 GNU extension.
1329 asm-definition:
1330 simple-asm-expr ;
1333 static void
1334 c_parser_asm_definition (c_parser *parser)
1336 tree asm_str = c_parser_simple_asm_expr (parser);
1337 if (asm_str)
1338 cgraph_add_asm_node (asm_str);
1339 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1342 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1343 6.7), adding them to SPECS (which may already include some).
1344 Storage class specifiers are accepted iff SCSPEC_OK; type
1345 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1346 the start iff START_ATTR_OK.
1348 declaration-specifiers:
1349 storage-class-specifier declaration-specifiers[opt]
1350 type-specifier declaration-specifiers[opt]
1351 type-qualifier declaration-specifiers[opt]
1352 function-specifier declaration-specifiers[opt]
1354 Function specifiers (inline) are from C99, and are currently
1355 handled as storage class specifiers, as is __thread.
1357 C90 6.5.1, C99 6.7.1:
1358 storage-class-specifier:
1359 typedef
1360 extern
1361 static
1362 auto
1363 register
1365 C99 6.7.4:
1366 function-specifier:
1367 inline
1369 C90 6.5.2, C99 6.7.2:
1370 type-specifier:
1371 void
1372 char
1373 short
1375 long
1376 float
1377 double
1378 signed
1379 unsigned
1380 _Bool
1381 _Complex
1382 [_Imaginary removed in C99 TC2]
1383 struct-or-union-specifier
1384 enum-specifier
1385 typedef-name
1387 (_Bool and _Complex are new in C99.)
1389 C90 6.5.3, C99 6.7.3:
1391 type-qualifier:
1392 const
1393 restrict
1394 volatile
1396 (restrict is new in C99.)
1398 GNU extensions:
1400 declaration-specifiers:
1401 attributes declaration-specifiers[opt]
1403 storage-class-specifier:
1404 __thread
1406 type-specifier:
1407 typeof-specifier
1408 _Decimal32
1409 _Decimal64
1410 _Decimal128
1411 _Fract
1412 _Accum
1413 _Sat
1415 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1416 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1418 Objective-C:
1420 type-specifier:
1421 class-name objc-protocol-refs[opt]
1422 typedef-name objc-protocol-refs
1423 objc-protocol-refs
1426 static void
1427 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1428 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1430 bool attrs_ok = start_attr_ok;
1431 bool seen_type = specs->type_seen_p;
1432 while (c_parser_next_token_is (parser, CPP_NAME)
1433 || c_parser_next_token_is (parser, CPP_KEYWORD)
1434 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1436 struct c_typespec t;
1437 tree attrs;
1438 if (c_parser_next_token_is (parser, CPP_NAME))
1440 tree value = c_parser_peek_token (parser)->value;
1441 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1442 /* This finishes the specifiers unless a type name is OK, it
1443 is declared as a type name and a type name hasn't yet
1444 been seen. */
1445 if (!typespec_ok || seen_type
1446 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1447 break;
1448 c_parser_consume_token (parser);
1449 seen_type = true;
1450 attrs_ok = true;
1451 if (kind == C_ID_TYPENAME
1452 && (!c_dialect_objc ()
1453 || c_parser_next_token_is_not (parser, CPP_LESS)))
1455 t.kind = ctsk_typedef;
1456 /* For a typedef name, record the meaning, not the name.
1457 In case of 'foo foo, bar;'. */
1458 t.spec = lookup_name (value);
1459 t.expr = NULL_TREE;
1460 t.expr_const_operands = true;
1462 else
1464 tree proto = NULL_TREE;
1465 gcc_assert (c_dialect_objc ());
1466 t.kind = ctsk_objc;
1467 if (c_parser_next_token_is (parser, CPP_LESS))
1468 proto = c_parser_objc_protocol_refs (parser);
1469 t.spec = objc_get_protocol_qualified_type (value, proto);
1470 t.expr = NULL_TREE;
1471 t.expr_const_operands = true;
1473 declspecs_add_type (specs, t);
1474 continue;
1476 if (c_parser_next_token_is (parser, CPP_LESS))
1478 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1479 nisse@lysator.liu.se. */
1480 tree proto;
1481 gcc_assert (c_dialect_objc ());
1482 if (!typespec_ok || seen_type)
1483 break;
1484 proto = c_parser_objc_protocol_refs (parser);
1485 t.kind = ctsk_objc;
1486 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1487 t.expr = NULL_TREE;
1488 t.expr_const_operands = true;
1489 declspecs_add_type (specs, t);
1490 continue;
1492 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1493 switch (c_parser_peek_token (parser)->keyword)
1495 case RID_STATIC:
1496 case RID_EXTERN:
1497 case RID_REGISTER:
1498 case RID_TYPEDEF:
1499 case RID_INLINE:
1500 case RID_AUTO:
1501 case RID_THREAD:
1502 if (!scspec_ok)
1503 goto out;
1504 attrs_ok = true;
1505 /* TODO: Distinguish between function specifiers (inline)
1506 and storage class specifiers, either here or in
1507 declspecs_add_scspec. */
1508 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1509 c_parser_consume_token (parser);
1510 break;
1511 case RID_UNSIGNED:
1512 case RID_LONG:
1513 case RID_SHORT:
1514 case RID_SIGNED:
1515 case RID_COMPLEX:
1516 case RID_INT:
1517 case RID_CHAR:
1518 case RID_FLOAT:
1519 case RID_DOUBLE:
1520 case RID_VOID:
1521 case RID_DFLOAT32:
1522 case RID_DFLOAT64:
1523 case RID_DFLOAT128:
1524 case RID_BOOL:
1525 case RID_FRACT:
1526 case RID_ACCUM:
1527 case RID_SAT:
1528 if (!typespec_ok)
1529 goto out;
1530 attrs_ok = true;
1531 seen_type = true;
1532 if (c_dialect_objc ())
1533 parser->objc_need_raw_identifier = true;
1534 t.kind = ctsk_resword;
1535 t.spec = c_parser_peek_token (parser)->value;
1536 t.expr = NULL_TREE;
1537 t.expr_const_operands = true;
1538 declspecs_add_type (specs, t);
1539 c_parser_consume_token (parser);
1540 break;
1541 case RID_ENUM:
1542 if (!typespec_ok)
1543 goto out;
1544 attrs_ok = true;
1545 seen_type = true;
1546 t = c_parser_enum_specifier (parser);
1547 declspecs_add_type (specs, t);
1548 break;
1549 case RID_STRUCT:
1550 case RID_UNION:
1551 if (!typespec_ok)
1552 goto out;
1553 attrs_ok = true;
1554 seen_type = true;
1555 t = c_parser_struct_or_union_specifier (parser);
1556 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1557 declspecs_add_type (specs, t);
1558 break;
1559 case RID_TYPEOF:
1560 /* ??? The old parser rejected typeof after other type
1561 specifiers, but is a syntax error the best way of
1562 handling this? */
1563 if (!typespec_ok || seen_type)
1564 goto out;
1565 attrs_ok = true;
1566 seen_type = true;
1567 t = c_parser_typeof_specifier (parser);
1568 declspecs_add_type (specs, t);
1569 break;
1570 case RID_CONST:
1571 case RID_VOLATILE:
1572 case RID_RESTRICT:
1573 attrs_ok = true;
1574 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1575 c_parser_consume_token (parser);
1576 break;
1577 case RID_ATTRIBUTE:
1578 if (!attrs_ok)
1579 goto out;
1580 attrs = c_parser_attributes (parser);
1581 declspecs_add_attrs (specs, attrs);
1582 break;
1583 default:
1584 goto out;
1587 out: ;
1590 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1592 enum-specifier:
1593 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1594 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1595 enum attributes[opt] identifier
1597 The form with trailing comma is new in C99. The forms with
1598 attributes are GNU extensions. In GNU C, we accept any expression
1599 without commas in the syntax (assignment expressions, not just
1600 conditional expressions); assignment expressions will be diagnosed
1601 as non-constant.
1603 enumerator-list:
1604 enumerator
1605 enumerator-list , enumerator
1607 enumerator:
1608 enumeration-constant
1609 enumeration-constant = constant-expression
1612 static struct c_typespec
1613 c_parser_enum_specifier (c_parser *parser)
1615 struct c_typespec ret;
1616 tree attrs;
1617 tree ident = NULL_TREE;
1618 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1619 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1620 c_parser_consume_token (parser);
1621 attrs = c_parser_attributes (parser);
1622 /* Set the location in case we create a decl now. */
1623 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1624 if (c_parser_next_token_is (parser, CPP_NAME))
1626 ident = c_parser_peek_token (parser)->value;
1627 ident_loc = c_parser_peek_token (parser)->location;
1628 c_parser_consume_token (parser);
1630 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1632 /* Parse an enum definition. */
1633 struct c_enum_contents the_enum;
1634 tree type = start_enum (&the_enum, ident);
1635 tree postfix_attrs;
1636 /* We chain the enumerators in reverse order, then put them in
1637 forward order at the end. */
1638 tree values = NULL_TREE;
1639 c_parser_consume_token (parser);
1640 while (true)
1642 tree enum_id;
1643 tree enum_value;
1644 tree enum_decl;
1645 bool seen_comma;
1646 c_token *token;
1647 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1648 location_t value_loc;
1649 if (c_parser_next_token_is_not (parser, CPP_NAME))
1651 c_parser_error (parser, "expected identifier");
1652 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1653 values = error_mark_node;
1654 break;
1656 token = c_parser_peek_token (parser);
1657 enum_id = token->value;
1658 /* Set the location in case we create a decl now. */
1659 c_parser_set_source_position_from_token (token);
1660 value_loc = token->location;
1661 c_parser_consume_token (parser);
1662 if (c_parser_next_token_is (parser, CPP_EQ))
1664 c_parser_consume_token (parser);
1665 value_loc = c_parser_peek_token (parser)->location;
1666 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1668 else
1669 enum_value = NULL_TREE;
1670 enum_decl = build_enumerator (&the_enum, enum_id, enum_value,
1671 value_loc);
1672 TREE_CHAIN (enum_decl) = values;
1673 values = enum_decl;
1674 seen_comma = false;
1675 if (c_parser_next_token_is (parser, CPP_COMMA))
1677 comma_loc = c_parser_peek_token (parser)->location;
1678 seen_comma = true;
1679 c_parser_consume_token (parser);
1681 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1683 if (seen_comma && !flag_isoc99)
1684 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1685 c_parser_consume_token (parser);
1686 break;
1688 if (!seen_comma)
1690 c_parser_error (parser, "expected %<,%> or %<}%>");
1691 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1692 values = error_mark_node;
1693 break;
1696 postfix_attrs = c_parser_attributes (parser);
1697 ret.spec = finish_enum (type, nreverse (values),
1698 chainon (attrs, postfix_attrs));
1699 ret.kind = ctsk_tagdef;
1700 ret.expr = NULL_TREE;
1701 ret.expr_const_operands = true;
1702 return ret;
1704 else if (!ident)
1706 c_parser_error (parser, "expected %<{%>");
1707 ret.spec = error_mark_node;
1708 ret.kind = ctsk_tagref;
1709 ret.expr = NULL_TREE;
1710 ret.expr_const_operands = true;
1711 return ret;
1713 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1714 /* In ISO C, enumerated types can be referred to only if already
1715 defined. */
1716 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1718 gcc_assert (ident);
1719 pedwarn (ident_loc, OPT_pedantic,
1720 "ISO C forbids forward references to %<enum%> types");
1722 return ret;
1725 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1727 struct-or-union-specifier:
1728 struct-or-union attributes[opt] identifier[opt]
1729 { struct-contents } attributes[opt]
1730 struct-or-union attributes[opt] identifier
1732 struct-contents:
1733 struct-declaration-list
1735 struct-declaration-list:
1736 struct-declaration ;
1737 struct-declaration-list struct-declaration ;
1739 GNU extensions:
1741 struct-contents:
1742 empty
1743 struct-declaration
1744 struct-declaration-list struct-declaration
1746 struct-declaration-list:
1747 struct-declaration-list ;
1750 (Note that in the syntax here, unlike that in ISO C, the semicolons
1751 are included here rather than in struct-declaration, in order to
1752 describe the syntax with extra semicolons and missing semicolon at
1753 end.)
1755 Objective-C:
1757 struct-declaration-list:
1758 @defs ( class-name )
1760 (Note this does not include a trailing semicolon, but can be
1761 followed by further declarations, and gets a pedwarn-if-pedantic
1762 when followed by a semicolon.) */
1764 static struct c_typespec
1765 c_parser_struct_or_union_specifier (c_parser *parser)
1767 struct c_typespec ret;
1768 tree attrs;
1769 tree ident = NULL_TREE;
1770 enum tree_code code;
1771 switch (c_parser_peek_token (parser)->keyword)
1773 case RID_STRUCT:
1774 code = RECORD_TYPE;
1775 break;
1776 case RID_UNION:
1777 code = UNION_TYPE;
1778 break;
1779 default:
1780 gcc_unreachable ();
1782 c_parser_consume_token (parser);
1783 attrs = c_parser_attributes (parser);
1784 /* Set the location in case we create a decl now. */
1785 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1786 if (c_parser_next_token_is (parser, CPP_NAME))
1788 ident = c_parser_peek_token (parser)->value;
1789 c_parser_consume_token (parser);
1791 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1793 /* Parse a struct or union definition. Start the scope of the
1794 tag before parsing components. */
1795 tree type = start_struct (code, ident);
1796 tree postfix_attrs;
1797 /* We chain the components in reverse order, then put them in
1798 forward order at the end. Each struct-declaration may
1799 declare multiple components (comma-separated), so we must use
1800 chainon to join them, although when parsing each
1801 struct-declaration we can use TREE_CHAIN directly.
1803 The theory behind all this is that there will be more
1804 semicolon separated fields than comma separated fields, and
1805 so we'll be minimizing the number of node traversals required
1806 by chainon. */
1807 tree contents = NULL_TREE;
1808 c_parser_consume_token (parser);
1809 /* Handle the Objective-C @defs construct,
1810 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1811 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1813 tree name;
1814 gcc_assert (c_dialect_objc ());
1815 c_parser_consume_token (parser);
1816 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1817 goto end_at_defs;
1818 if (c_parser_next_token_is (parser, CPP_NAME)
1819 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1821 name = c_parser_peek_token (parser)->value;
1822 c_parser_consume_token (parser);
1824 else
1826 c_parser_error (parser, "expected class name");
1827 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1828 goto end_at_defs;
1830 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1831 "expected %<)%>");
1832 contents = nreverse (objc_get_class_ivars (name));
1834 end_at_defs:
1835 /* Parse the struct-declarations and semicolons. Problems with
1836 semicolons are diagnosed here; empty structures are diagnosed
1837 elsewhere. */
1838 while (true)
1840 tree decls;
1841 /* Parse any stray semicolon. */
1842 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1844 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1845 "extra semicolon in struct or union specified");
1846 c_parser_consume_token (parser);
1847 continue;
1849 /* Stop if at the end of the struct or union contents. */
1850 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1852 c_parser_consume_token (parser);
1853 break;
1855 /* Accept #pragmas at struct scope. */
1856 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1858 c_parser_pragma (parser, pragma_external);
1859 continue;
1861 /* Parse some comma-separated declarations, but not the
1862 trailing semicolon if any. */
1863 decls = c_parser_struct_declaration (parser);
1864 contents = chainon (decls, contents);
1865 /* If no semicolon follows, either we have a parse error or
1866 are at the end of the struct or union and should
1867 pedwarn. */
1868 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1869 c_parser_consume_token (parser);
1870 else
1872 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1873 pedwarn (c_parser_peek_token (parser)->location, 0,
1874 "no semicolon at end of struct or union");
1875 else
1877 c_parser_error (parser, "expected %<;%>");
1878 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1879 break;
1883 postfix_attrs = c_parser_attributes (parser);
1884 ret.spec = finish_struct (type, nreverse (contents),
1885 chainon (attrs, postfix_attrs));
1886 ret.kind = ctsk_tagdef;
1887 ret.expr = NULL_TREE;
1888 ret.expr_const_operands = true;
1889 return ret;
1891 else if (!ident)
1893 c_parser_error (parser, "expected %<{%>");
1894 ret.spec = error_mark_node;
1895 ret.kind = ctsk_tagref;
1896 ret.expr = NULL_TREE;
1897 ret.expr_const_operands = true;
1898 return ret;
1900 ret = parser_xref_tag (code, ident);
1901 return ret;
1904 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1905 the trailing semicolon.
1907 struct-declaration:
1908 specifier-qualifier-list struct-declarator-list
1910 specifier-qualifier-list:
1911 type-specifier specifier-qualifier-list[opt]
1912 type-qualifier specifier-qualifier-list[opt]
1913 attributes specifier-qualifier-list[opt]
1915 struct-declarator-list:
1916 struct-declarator
1917 struct-declarator-list , attributes[opt] struct-declarator
1919 struct-declarator:
1920 declarator attributes[opt]
1921 declarator[opt] : constant-expression attributes[opt]
1923 GNU extensions:
1925 struct-declaration:
1926 __extension__ struct-declaration
1927 specifier-qualifier-list
1929 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1930 of attributes where shown is a GNU extension. In GNU C, we accept
1931 any expression without commas in the syntax (assignment
1932 expressions, not just conditional expressions); assignment
1933 expressions will be diagnosed as non-constant. */
1935 static tree
1936 c_parser_struct_declaration (c_parser *parser)
1938 struct c_declspecs *specs;
1939 tree prefix_attrs;
1940 tree all_prefix_attrs;
1941 tree decls;
1942 location_t decl_loc;
1943 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1945 int ext;
1946 tree decl;
1947 ext = disable_extension_diagnostics ();
1948 c_parser_consume_token (parser);
1949 decl = c_parser_struct_declaration (parser);
1950 restore_extension_diagnostics (ext);
1951 return decl;
1953 specs = build_null_declspecs ();
1954 decl_loc = c_parser_peek_token (parser)->location;
1955 c_parser_declspecs (parser, specs, false, true, true);
1956 if (parser->error)
1957 return NULL_TREE;
1958 if (!specs->declspecs_seen_p)
1960 c_parser_error (parser, "expected specifier-qualifier-list");
1961 return NULL_TREE;
1963 finish_declspecs (specs);
1964 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1966 tree ret;
1967 if (!specs->type_seen_p)
1969 pedwarn (decl_loc, OPT_pedantic,
1970 "ISO C forbids member declarations with no members");
1971 shadow_tag_warned (specs, pedantic);
1972 ret = NULL_TREE;
1974 else
1976 /* Support for unnamed structs or unions as members of
1977 structs or unions (which is [a] useful and [b] supports
1978 MS P-SDK). */
1979 tree attrs = NULL;
1981 ret = grokfield (c_parser_peek_token (parser)->location,
1982 build_id_declarator (NULL_TREE), specs,
1983 NULL_TREE, &attrs);
1984 if (ret)
1985 decl_attributes (&ret, attrs, 0);
1987 return ret;
1989 pending_xref_error ();
1990 prefix_attrs = specs->attrs;
1991 all_prefix_attrs = prefix_attrs;
1992 specs->attrs = NULL_TREE;
1993 decls = NULL_TREE;
1994 while (true)
1996 /* Declaring one or more declarators or un-named bit-fields. */
1997 struct c_declarator *declarator;
1998 bool dummy = false;
1999 if (c_parser_next_token_is (parser, CPP_COLON))
2000 declarator = build_id_declarator (NULL_TREE);
2001 else
2002 declarator = c_parser_declarator (parser, specs->type_seen_p,
2003 C_DTR_NORMAL, &dummy);
2004 if (declarator == NULL)
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2007 break;
2009 if (c_parser_next_token_is (parser, CPP_COLON)
2010 || c_parser_next_token_is (parser, CPP_COMMA)
2011 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2012 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2013 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2015 tree postfix_attrs = NULL_TREE;
2016 tree width = NULL_TREE;
2017 tree d;
2018 if (c_parser_next_token_is (parser, CPP_COLON))
2020 c_parser_consume_token (parser);
2021 width = c_parser_expr_no_commas (parser, NULL).value;
2023 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2024 postfix_attrs = c_parser_attributes (parser);
2025 d = grokfield (c_parser_peek_token (parser)->location,
2026 declarator, specs, width, &all_prefix_attrs);
2027 decl_attributes (&d, chainon (postfix_attrs,
2028 all_prefix_attrs), 0);
2029 TREE_CHAIN (d) = decls;
2030 decls = d;
2031 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2032 all_prefix_attrs = chainon (c_parser_attributes (parser),
2033 prefix_attrs);
2034 else
2035 all_prefix_attrs = prefix_attrs;
2036 if (c_parser_next_token_is (parser, CPP_COMMA))
2037 c_parser_consume_token (parser);
2038 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2039 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2041 /* Semicolon consumed in caller. */
2042 break;
2044 else
2046 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2047 break;
2050 else
2052 c_parser_error (parser,
2053 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2054 "%<__attribute__%>");
2055 break;
2058 return decls;
2061 /* Parse a typeof specifier (a GNU extension).
2063 typeof-specifier:
2064 typeof ( expression )
2065 typeof ( type-name )
2068 static struct c_typespec
2069 c_parser_typeof_specifier (c_parser *parser)
2071 struct c_typespec ret;
2072 ret.kind = ctsk_typeof;
2073 ret.spec = error_mark_node;
2074 ret.expr = NULL_TREE;
2075 ret.expr_const_operands = true;
2076 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2077 c_parser_consume_token (parser);
2078 skip_evaluation++;
2079 in_typeof++;
2080 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2082 skip_evaluation--;
2083 in_typeof--;
2084 return ret;
2086 if (c_parser_next_token_starts_typename (parser))
2088 struct c_type_name *type = c_parser_type_name (parser);
2089 skip_evaluation--;
2090 in_typeof--;
2091 if (type != NULL)
2093 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2094 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2097 else
2099 bool was_vm;
2100 location_t here = c_parser_peek_token (parser)->location;
2101 struct c_expr expr = c_parser_expression (parser);
2102 skip_evaluation--;
2103 in_typeof--;
2104 if (TREE_CODE (expr.value) == COMPONENT_REF
2105 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2106 error_at (here, "%<typeof%> applied to a bit-field");
2107 ret.spec = TREE_TYPE (expr.value);
2108 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2109 /* This is returned with the type so that when the type is
2110 evaluated, this can be evaluated. */
2111 if (was_vm)
2112 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2113 pop_maybe_used (was_vm);
2115 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2116 return ret;
2119 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2120 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2121 be redeclared; otherwise it may not. KIND indicates which kind of
2122 declarator is wanted. Returns a valid declarator except in the
2123 case of a syntax error in which case NULL is returned. *SEEN_ID is
2124 set to true if an identifier being declared is seen; this is used
2125 to diagnose bad forms of abstract array declarators and to
2126 determine whether an identifier list is syntactically permitted.
2128 declarator:
2129 pointer[opt] direct-declarator
2131 direct-declarator:
2132 identifier
2133 ( attributes[opt] declarator )
2134 direct-declarator array-declarator
2135 direct-declarator ( parameter-type-list )
2136 direct-declarator ( identifier-list[opt] )
2138 pointer:
2139 * type-qualifier-list[opt]
2140 * type-qualifier-list[opt] pointer
2142 type-qualifier-list:
2143 type-qualifier
2144 attributes
2145 type-qualifier-list type-qualifier
2146 type-qualifier-list attributes
2148 parameter-type-list:
2149 parameter-list
2150 parameter-list , ...
2152 parameter-list:
2153 parameter-declaration
2154 parameter-list , parameter-declaration
2156 parameter-declaration:
2157 declaration-specifiers declarator attributes[opt]
2158 declaration-specifiers abstract-declarator[opt] attributes[opt]
2160 identifier-list:
2161 identifier
2162 identifier-list , identifier
2164 abstract-declarator:
2165 pointer
2166 pointer[opt] direct-abstract-declarator
2168 direct-abstract-declarator:
2169 ( attributes[opt] abstract-declarator )
2170 direct-abstract-declarator[opt] array-declarator
2171 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2173 GNU extensions:
2175 direct-declarator:
2176 direct-declarator ( parameter-forward-declarations
2177 parameter-type-list[opt] )
2179 direct-abstract-declarator:
2180 direct-abstract-declarator[opt] ( parameter-forward-declarations
2181 parameter-type-list[opt] )
2183 parameter-forward-declarations:
2184 parameter-list ;
2185 parameter-forward-declarations parameter-list ;
2187 The uses of attributes shown above are GNU extensions.
2189 Some forms of array declarator are not included in C99 in the
2190 syntax for abstract declarators; these are disallowed elsewhere.
2191 This may be a defect (DR#289).
2193 This function also accepts an omitted abstract declarator as being
2194 an abstract declarator, although not part of the formal syntax. */
2196 static struct c_declarator *
2197 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2198 bool *seen_id)
2200 /* Parse any initial pointer part. */
2201 if (c_parser_next_token_is (parser, CPP_MULT))
2203 struct c_declspecs *quals_attrs = build_null_declspecs ();
2204 struct c_declarator *inner;
2205 c_parser_consume_token (parser);
2206 c_parser_declspecs (parser, quals_attrs, false, false, true);
2207 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2208 if (inner == NULL)
2209 return NULL;
2210 else
2211 return make_pointer_declarator (quals_attrs, inner);
2213 /* Now we have a direct declarator, direct abstract declarator or
2214 nothing (which counts as a direct abstract declarator here). */
2215 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2218 /* Parse a direct declarator or direct abstract declarator; arguments
2219 as c_parser_declarator. */
2221 static struct c_declarator *
2222 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2223 bool *seen_id)
2225 /* The direct declarator must start with an identifier (possibly
2226 omitted) or a parenthesized declarator (possibly abstract). In
2227 an ordinary declarator, initial parentheses must start a
2228 parenthesized declarator. In an abstract declarator or parameter
2229 declarator, they could start a parenthesized declarator or a
2230 parameter list. To tell which, the open parenthesis and any
2231 following attributes must be read. If a declaration specifier
2232 follows, then it is a parameter list; if the specifier is a
2233 typedef name, there might be an ambiguity about redeclaring it,
2234 which is resolved in the direction of treating it as a typedef
2235 name. If a close parenthesis follows, it is also an empty
2236 parameter list, as the syntax does not permit empty abstract
2237 declarators. Otherwise, it is a parenthesized declarator (in
2238 which case the analysis may be repeated inside it, recursively).
2240 ??? There is an ambiguity in a parameter declaration "int
2241 (__attribute__((foo)) x)", where x is not a typedef name: it
2242 could be an abstract declarator for a function, or declare x with
2243 parentheses. The proper resolution of this ambiguity needs
2244 documenting. At present we follow an accident of the old
2245 parser's implementation, whereby the first parameter must have
2246 some declaration specifiers other than just attributes. Thus as
2247 a parameter declaration it is treated as a parenthesized
2248 parameter named x, and as an abstract declarator it is
2249 rejected.
2251 ??? Also following the old parser, attributes inside an empty
2252 parameter list are ignored, making it a list not yielding a
2253 prototype, rather than giving an error or making it have one
2254 parameter with implicit type int.
2256 ??? Also following the old parser, typedef names may be
2257 redeclared in declarators, but not Objective-C class names. */
2259 if (kind != C_DTR_ABSTRACT
2260 && c_parser_next_token_is (parser, CPP_NAME)
2261 && ((type_seen_p
2262 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2263 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2265 struct c_declarator *inner
2266 = build_id_declarator (c_parser_peek_token (parser)->value);
2267 *seen_id = true;
2268 inner->id_loc = c_parser_peek_token (parser)->location;
2269 c_parser_consume_token (parser);
2270 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2273 if (kind != C_DTR_NORMAL
2274 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2276 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2277 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2280 /* Either we are at the end of an abstract declarator, or we have
2281 parentheses. */
2283 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2285 tree attrs;
2286 struct c_declarator *inner;
2287 c_parser_consume_token (parser);
2288 attrs = c_parser_attributes (parser);
2289 if (kind != C_DTR_NORMAL
2290 && (c_parser_next_token_starts_declspecs (parser)
2291 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2293 struct c_arg_info *args
2294 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2295 attrs);
2296 if (args == NULL)
2297 return NULL;
2298 else
2300 inner
2301 = build_function_declarator (args,
2302 build_id_declarator (NULL_TREE));
2303 return c_parser_direct_declarator_inner (parser, *seen_id,
2304 inner);
2307 /* A parenthesized declarator. */
2308 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2309 if (inner != NULL && attrs != NULL)
2310 inner = build_attrs_declarator (attrs, inner);
2311 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2313 c_parser_consume_token (parser);
2314 if (inner == NULL)
2315 return NULL;
2316 else
2317 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2319 else
2321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2322 "expected %<)%>");
2323 return NULL;
2326 else
2328 if (kind == C_DTR_NORMAL)
2330 c_parser_error (parser, "expected identifier or %<(%>");
2331 return NULL;
2333 else
2334 return build_id_declarator (NULL_TREE);
2338 /* Parse part of a direct declarator or direct abstract declarator,
2339 given that some (in INNER) has already been parsed; ID_PRESENT is
2340 true if an identifier is present, false for an abstract
2341 declarator. */
2343 static struct c_declarator *
2344 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2345 struct c_declarator *inner)
2347 /* Parse a sequence of array declarators and parameter lists. */
2348 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2350 struct c_declarator *declarator;
2351 struct c_declspecs *quals_attrs = build_null_declspecs ();
2352 bool static_seen;
2353 bool star_seen;
2354 tree dimen;
2355 c_parser_consume_token (parser);
2356 c_parser_declspecs (parser, quals_attrs, false, false, true);
2357 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2358 if (static_seen)
2359 c_parser_consume_token (parser);
2360 if (static_seen && !quals_attrs->declspecs_seen_p)
2361 c_parser_declspecs (parser, quals_attrs, false, false, true);
2362 if (!quals_attrs->declspecs_seen_p)
2363 quals_attrs = NULL;
2364 /* If "static" is present, there must be an array dimension.
2365 Otherwise, there may be a dimension, "*", or no
2366 dimension. */
2367 if (static_seen)
2369 star_seen = false;
2370 dimen = c_parser_expr_no_commas (parser, NULL).value;
2372 else
2374 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2376 dimen = NULL_TREE;
2377 star_seen = false;
2379 else if (c_parser_next_token_is (parser, CPP_MULT))
2381 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2383 dimen = NULL_TREE;
2384 star_seen = true;
2385 c_parser_consume_token (parser);
2387 else
2389 star_seen = false;
2390 dimen = c_parser_expr_no_commas (parser, NULL).value;
2393 else
2395 star_seen = false;
2396 dimen = c_parser_expr_no_commas (parser, NULL).value;
2399 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2400 c_parser_consume_token (parser);
2401 else
2403 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2404 "expected %<]%>");
2405 return NULL;
2407 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2408 star_seen);
2409 if (declarator == NULL)
2410 return NULL;
2411 inner = set_array_declarator_inner (declarator, inner);
2412 return c_parser_direct_declarator_inner (parser, id_present, inner);
2414 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2416 tree attrs;
2417 struct c_arg_info *args;
2418 c_parser_consume_token (parser);
2419 attrs = c_parser_attributes (parser);
2420 args = c_parser_parms_declarator (parser, id_present, attrs);
2421 if (args == NULL)
2422 return NULL;
2423 else
2425 inner = build_function_declarator (args, inner);
2426 return c_parser_direct_declarator_inner (parser, id_present, inner);
2429 return inner;
2432 /* Parse a parameter list or identifier list, including the closing
2433 parenthesis but not the opening one. ATTRS are the attributes at
2434 the start of the list. ID_LIST_OK is true if an identifier list is
2435 acceptable; such a list must not have attributes at the start. */
2437 static struct c_arg_info *
2438 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2440 push_scope ();
2441 declare_parm_level ();
2442 /* If the list starts with an identifier, it is an identifier list.
2443 Otherwise, it is either a prototype list or an empty list. */
2444 if (id_list_ok
2445 && !attrs
2446 && c_parser_next_token_is (parser, CPP_NAME)
2447 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2449 tree list = NULL_TREE, *nextp = &list;
2450 while (c_parser_next_token_is (parser, CPP_NAME)
2451 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2453 *nextp = build_tree_list (NULL_TREE,
2454 c_parser_peek_token (parser)->value);
2455 nextp = & TREE_CHAIN (*nextp);
2456 c_parser_consume_token (parser);
2457 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2458 break;
2459 c_parser_consume_token (parser);
2460 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2462 c_parser_error (parser, "expected identifier");
2463 break;
2466 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2468 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2469 ret->parms = 0;
2470 ret->tags = 0;
2471 ret->types = list;
2472 ret->others = 0;
2473 ret->pending_sizes = 0;
2474 ret->had_vla_unspec = 0;
2475 c_parser_consume_token (parser);
2476 pop_scope ();
2477 return ret;
2479 else
2481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2482 "expected %<)%>");
2483 pop_scope ();
2484 return NULL;
2487 else
2489 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2490 pop_scope ();
2491 return ret;
2495 /* Parse a parameter list (possibly empty), including the closing
2496 parenthesis but not the opening one. ATTRS are the attributes at
2497 the start of the list. */
2499 static struct c_arg_info *
2500 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2502 bool good_parm = false;
2503 /* ??? Following the old parser, forward parameter declarations may
2504 use abstract declarators, and if no real parameter declarations
2505 follow the forward declarations then this is not diagnosed. Also
2506 note as above that attributes are ignored as the only contents of
2507 the parentheses, or as the only contents after forward
2508 declarations. */
2509 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2511 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2512 ret->parms = 0;
2513 ret->tags = 0;
2514 ret->types = 0;
2515 ret->others = 0;
2516 ret->pending_sizes = 0;
2517 ret->had_vla_unspec = 0;
2518 c_parser_consume_token (parser);
2519 return ret;
2521 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2523 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2524 ret->parms = 0;
2525 ret->tags = 0;
2526 ret->others = 0;
2527 ret->pending_sizes = 0;
2528 ret->had_vla_unspec = 0;
2529 /* Suppress -Wold-style-definition for this case. */
2530 ret->types = error_mark_node;
2531 error_at (c_parser_peek_token (parser)->location,
2532 "ISO C requires a named argument before %<...%>");
2533 c_parser_consume_token (parser);
2534 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2536 c_parser_consume_token (parser);
2537 return ret;
2539 else
2541 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2542 "expected %<)%>");
2543 return NULL;
2546 /* Nonempty list of parameters, either terminated with semicolon
2547 (forward declarations; recurse) or with close parenthesis (normal
2548 function) or with ", ... )" (variadic function). */
2549 while (true)
2551 /* Parse a parameter. */
2552 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2553 attrs = NULL_TREE;
2554 if (parm != NULL)
2556 good_parm = true;
2557 push_parm_decl (parm);
2559 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2561 tree new_attrs;
2562 c_parser_consume_token (parser);
2563 mark_forward_parm_decls ();
2564 new_attrs = c_parser_attributes (parser);
2565 return c_parser_parms_list_declarator (parser, new_attrs);
2567 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2569 c_parser_consume_token (parser);
2570 if (good_parm)
2571 return get_parm_info (false);
2572 else
2574 struct c_arg_info *ret
2575 = XOBNEW (&parser_obstack, struct c_arg_info);
2576 ret->parms = 0;
2577 ret->tags = 0;
2578 ret->types = 0;
2579 ret->others = 0;
2580 ret->pending_sizes = 0;
2581 ret->had_vla_unspec = 0;
2582 return ret;
2585 if (!c_parser_require (parser, CPP_COMMA,
2586 "expected %<;%>, %<,%> or %<)%>"))
2588 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2589 get_pending_sizes ();
2590 return NULL;
2592 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2594 c_parser_consume_token (parser);
2595 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2597 c_parser_consume_token (parser);
2598 if (good_parm)
2599 return get_parm_info (true);
2600 else
2602 struct c_arg_info *ret
2603 = XOBNEW (&parser_obstack, struct c_arg_info);
2604 ret->parms = 0;
2605 ret->tags = 0;
2606 ret->types = 0;
2607 ret->others = 0;
2608 ret->pending_sizes = 0;
2609 ret->had_vla_unspec = 0;
2610 return ret;
2613 else
2615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2616 "expected %<)%>");
2617 get_pending_sizes ();
2618 return NULL;
2624 /* Parse a parameter declaration. ATTRS are the attributes at the
2625 start of the declaration if it is the first parameter. */
2627 static struct c_parm *
2628 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2630 struct c_declspecs *specs;
2631 struct c_declarator *declarator;
2632 tree prefix_attrs;
2633 tree postfix_attrs = NULL_TREE;
2634 bool dummy = false;
2635 if (!c_parser_next_token_starts_declspecs (parser))
2637 /* ??? In some Objective-C cases '...' isn't applicable so there
2638 should be a different message. */
2639 c_parser_error (parser,
2640 "expected declaration specifiers or %<...%>");
2641 c_parser_skip_to_end_of_parameter (parser);
2642 return NULL;
2644 specs = build_null_declspecs ();
2645 if (attrs)
2647 declspecs_add_attrs (specs, attrs);
2648 attrs = NULL_TREE;
2650 c_parser_declspecs (parser, specs, true, true, true);
2651 finish_declspecs (specs);
2652 pending_xref_error ();
2653 prefix_attrs = specs->attrs;
2654 specs->attrs = NULL_TREE;
2655 declarator = c_parser_declarator (parser, specs->type_seen_p,
2656 C_DTR_PARM, &dummy);
2657 if (declarator == NULL)
2659 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2660 return NULL;
2662 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2663 postfix_attrs = c_parser_attributes (parser);
2664 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2665 declarator);
2668 /* Parse a string literal in an asm expression. It should not be
2669 translated, and wide string literals are an error although
2670 permitted by the syntax. This is a GNU extension.
2672 asm-string-literal:
2673 string-literal
2675 ??? At present, following the old parser, the caller needs to have
2676 set lex_untranslated_string to 1. It would be better to follow the
2677 C++ parser rather than using this kludge. */
2679 static tree
2680 c_parser_asm_string_literal (c_parser *parser)
2682 tree str;
2683 if (c_parser_next_token_is (parser, CPP_STRING))
2685 str = c_parser_peek_token (parser)->value;
2686 c_parser_consume_token (parser);
2688 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2690 error_at (c_parser_peek_token (parser)->location,
2691 "wide string literal in %<asm%>");
2692 str = build_string (1, "");
2693 c_parser_consume_token (parser);
2695 else
2697 c_parser_error (parser, "expected string literal");
2698 str = NULL_TREE;
2700 return str;
2703 /* Parse a simple asm expression. This is used in restricted
2704 contexts, where a full expression with inputs and outputs does not
2705 make sense. This is a GNU extension.
2707 simple-asm-expr:
2708 asm ( asm-string-literal )
2711 static tree
2712 c_parser_simple_asm_expr (c_parser *parser)
2714 tree str;
2715 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2716 /* ??? Follow the C++ parser rather than using the
2717 lex_untranslated_string kludge. */
2718 parser->lex_untranslated_string = true;
2719 c_parser_consume_token (parser);
2720 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2722 parser->lex_untranslated_string = false;
2723 return NULL_TREE;
2725 str = c_parser_asm_string_literal (parser);
2726 parser->lex_untranslated_string = false;
2727 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2729 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2730 return NULL_TREE;
2732 return str;
2735 /* Parse (possibly empty) attributes. This is a GNU extension.
2737 attributes:
2738 empty
2739 attributes attribute
2741 attribute:
2742 __attribute__ ( ( attribute-list ) )
2744 attribute-list:
2745 attrib
2746 attribute_list , attrib
2748 attrib:
2749 empty
2750 any-word
2751 any-word ( identifier )
2752 any-word ( identifier , nonempty-expr-list )
2753 any-word ( expr-list )
2755 where the "identifier" must not be declared as a type, and
2756 "any-word" may be any identifier (including one declared as a
2757 type), a reserved word storage class specifier, type specifier or
2758 type qualifier. ??? This still leaves out most reserved keywords
2759 (following the old parser), shouldn't we include them, and why not
2760 allow identifiers declared as types to start the arguments? */
2762 static tree
2763 c_parser_attributes (c_parser *parser)
2765 tree attrs = NULL_TREE;
2766 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2768 /* ??? Follow the C++ parser rather than using the
2769 lex_untranslated_string kludge. */
2770 parser->lex_untranslated_string = true;
2771 c_parser_consume_token (parser);
2772 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2774 parser->lex_untranslated_string = false;
2775 return attrs;
2777 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2779 parser->lex_untranslated_string = false;
2780 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2781 return attrs;
2783 /* Parse the attribute list. */
2784 while (c_parser_next_token_is (parser, CPP_COMMA)
2785 || c_parser_next_token_is (parser, CPP_NAME)
2786 || c_parser_next_token_is (parser, CPP_KEYWORD))
2788 tree attr, attr_name, attr_args;
2789 if (c_parser_next_token_is (parser, CPP_COMMA))
2791 c_parser_consume_token (parser);
2792 continue;
2794 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2796 /* ??? See comment above about what keywords are
2797 accepted here. */
2798 bool ok;
2799 switch (c_parser_peek_token (parser)->keyword)
2801 case RID_STATIC:
2802 case RID_UNSIGNED:
2803 case RID_LONG:
2804 case RID_CONST:
2805 case RID_EXTERN:
2806 case RID_REGISTER:
2807 case RID_TYPEDEF:
2808 case RID_SHORT:
2809 case RID_INLINE:
2810 case RID_VOLATILE:
2811 case RID_SIGNED:
2812 case RID_AUTO:
2813 case RID_RESTRICT:
2814 case RID_COMPLEX:
2815 case RID_THREAD:
2816 case RID_INT:
2817 case RID_CHAR:
2818 case RID_FLOAT:
2819 case RID_DOUBLE:
2820 case RID_VOID:
2821 case RID_DFLOAT32:
2822 case RID_DFLOAT64:
2823 case RID_DFLOAT128:
2824 case RID_BOOL:
2825 case RID_FRACT:
2826 case RID_ACCUM:
2827 case RID_SAT:
2828 ok = true;
2829 break;
2830 default:
2831 ok = false;
2832 break;
2834 if (!ok)
2835 break;
2836 /* Accept __attribute__((__const)) as __attribute__((const))
2837 etc. */
2838 attr_name
2839 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
2841 else
2842 attr_name = c_parser_peek_token (parser)->value;
2843 c_parser_consume_token (parser);
2844 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2846 attr = build_tree_list (attr_name, NULL_TREE);
2847 attrs = chainon (attrs, attr);
2848 continue;
2850 c_parser_consume_token (parser);
2851 /* Parse the attribute contents. If they start with an
2852 identifier which is followed by a comma or close
2853 parenthesis, then the arguments start with that
2854 identifier; otherwise they are an expression list. */
2855 if (c_parser_next_token_is (parser, CPP_NAME)
2856 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2857 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2858 || (c_parser_peek_2nd_token (parser)->type
2859 == CPP_CLOSE_PAREN)))
2861 tree arg1 = c_parser_peek_token (parser)->value;
2862 c_parser_consume_token (parser);
2863 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2864 attr_args = build_tree_list (NULL_TREE, arg1);
2865 else
2867 c_parser_consume_token (parser);
2868 attr_args = tree_cons (NULL_TREE, arg1,
2869 c_parser_expr_list (parser, false,
2870 true));
2873 else
2875 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2876 attr_args = NULL_TREE;
2877 else
2878 attr_args = c_parser_expr_list (parser, false, true);
2880 attr = build_tree_list (attr_name, attr_args);
2881 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2882 c_parser_consume_token (parser);
2883 else
2885 parser->lex_untranslated_string = false;
2886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2887 "expected %<)%>");
2888 return attrs;
2890 attrs = chainon (attrs, attr);
2892 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2893 c_parser_consume_token (parser);
2894 else
2896 parser->lex_untranslated_string = false;
2897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2898 "expected %<)%>");
2899 return attrs;
2901 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2902 c_parser_consume_token (parser);
2903 else
2905 parser->lex_untranslated_string = false;
2906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2907 "expected %<)%>");
2908 return attrs;
2910 parser->lex_untranslated_string = false;
2912 return attrs;
2915 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2917 type-name:
2918 specifier-qualifier-list abstract-declarator[opt]
2921 static struct c_type_name *
2922 c_parser_type_name (c_parser *parser)
2924 struct c_declspecs *specs = build_null_declspecs ();
2925 struct c_declarator *declarator;
2926 struct c_type_name *ret;
2927 bool dummy = false;
2928 c_parser_declspecs (parser, specs, false, true, true);
2929 if (!specs->declspecs_seen_p)
2931 c_parser_error (parser, "expected specifier-qualifier-list");
2932 return NULL;
2934 pending_xref_error ();
2935 finish_declspecs (specs);
2936 declarator = c_parser_declarator (parser, specs->type_seen_p,
2937 C_DTR_ABSTRACT, &dummy);
2938 if (declarator == NULL)
2939 return NULL;
2940 ret = XOBNEW (&parser_obstack, struct c_type_name);
2941 ret->specs = specs;
2942 ret->declarator = declarator;
2943 return ret;
2946 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2948 initializer:
2949 assignment-expression
2950 { initializer-list }
2951 { initializer-list , }
2953 initializer-list:
2954 designation[opt] initializer
2955 initializer-list , designation[opt] initializer
2957 designation:
2958 designator-list =
2960 designator-list:
2961 designator
2962 designator-list designator
2964 designator:
2965 array-designator
2966 . identifier
2968 array-designator:
2969 [ constant-expression ]
2971 GNU extensions:
2973 initializer:
2976 designation:
2977 array-designator
2978 identifier :
2980 array-designator:
2981 [ constant-expression ... constant-expression ]
2983 Any expression without commas is accepted in the syntax for the
2984 constant-expressions, with non-constant expressions rejected later.
2986 This function is only used for top-level initializers; for nested
2987 ones, see c_parser_initval. */
2989 static struct c_expr
2990 c_parser_initializer (c_parser *parser)
2992 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2993 return c_parser_braced_init (parser, NULL_TREE, false);
2994 else
2996 struct c_expr ret;
2997 ret = c_parser_expr_no_commas (parser, NULL);
2998 if (TREE_CODE (ret.value) != STRING_CST
2999 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3000 ret = default_function_array_conversion (ret);
3001 return ret;
3005 /* Parse a braced initializer list. TYPE is the type specified for a
3006 compound literal, and NULL_TREE for other initializers and for
3007 nested braced lists. NESTED_P is true for nested braced lists,
3008 false for the list of a compound literal or the list that is the
3009 top-level initializer in a declaration. */
3011 static struct c_expr
3012 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3014 location_t brace_loc = c_parser_peek_token (parser)->location;
3015 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3016 c_parser_consume_token (parser);
3017 if (nested_p)
3018 push_init_level (0);
3019 else
3020 really_start_incremental_init (type);
3021 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3023 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3025 else
3027 /* Parse a non-empty initializer list, possibly with a trailing
3028 comma. */
3029 while (true)
3031 c_parser_initelt (parser);
3032 if (parser->error)
3033 break;
3034 if (c_parser_next_token_is (parser, CPP_COMMA))
3035 c_parser_consume_token (parser);
3036 else
3037 break;
3038 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3039 break;
3042 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3044 struct c_expr ret;
3045 ret.value = error_mark_node;
3046 ret.original_code = ERROR_MARK;
3047 ret.original_type = NULL;
3048 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3049 pop_init_level (0);
3050 return ret;
3052 c_parser_consume_token (parser);
3053 return pop_init_level (0);
3056 /* Parse a nested initializer, including designators. */
3058 static void
3059 c_parser_initelt (c_parser *parser)
3061 /* Parse any designator or designator list. A single array
3062 designator may have the subsequent "=" omitted in GNU C, but a
3063 longer list or a structure member designator may not. */
3064 if (c_parser_next_token_is (parser, CPP_NAME)
3065 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3067 /* Old-style structure member designator. */
3068 set_init_label (c_parser_peek_token (parser)->value);
3069 /* Use the colon as the error location. */
3070 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3071 "obsolete use of designated initializer with %<:%>");
3072 c_parser_consume_token (parser);
3073 c_parser_consume_token (parser);
3075 else
3077 /* des_seen is 0 if there have been no designators, 1 if there
3078 has been a single array designator and 2 otherwise. */
3079 int des_seen = 0;
3080 /* Location of a designator. */
3081 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3082 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3083 || c_parser_next_token_is (parser, CPP_DOT))
3085 int des_prev = des_seen;
3086 if (!des_seen)
3087 des_loc = c_parser_peek_token (parser)->location;
3088 if (des_seen < 2)
3089 des_seen++;
3090 if (c_parser_next_token_is (parser, CPP_DOT))
3092 des_seen = 2;
3093 c_parser_consume_token (parser);
3094 if (c_parser_next_token_is (parser, CPP_NAME))
3096 set_init_label (c_parser_peek_token (parser)->value);
3097 c_parser_consume_token (parser);
3099 else
3101 struct c_expr init;
3102 init.value = error_mark_node;
3103 init.original_code = ERROR_MARK;
3104 init.original_type = NULL;
3105 c_parser_error (parser, "expected identifier");
3106 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3107 process_init_element (init, false);
3108 return;
3111 else
3113 tree first, second;
3114 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3115 /* ??? Following the old parser, [ objc-receiver
3116 objc-message-args ] is accepted as an initializer,
3117 being distinguished from a designator by what follows
3118 the first assignment expression inside the square
3119 brackets, but after a first array designator a
3120 subsequent square bracket is for Objective-C taken to
3121 start an expression, using the obsolete form of
3122 designated initializer without '=', rather than
3123 possibly being a second level of designation: in LALR
3124 terms, the '[' is shifted rather than reducing
3125 designator to designator-list. */
3126 if (des_prev == 1 && c_dialect_objc ())
3128 des_seen = des_prev;
3129 break;
3131 if (des_prev == 0 && c_dialect_objc ())
3133 /* This might be an array designator or an
3134 Objective-C message expression. If the former,
3135 continue parsing here; if the latter, parse the
3136 remainder of the initializer given the starting
3137 primary-expression. ??? It might make sense to
3138 distinguish when des_prev == 1 as well; see
3139 previous comment. */
3140 tree rec, args;
3141 struct c_expr mexpr;
3142 c_parser_consume_token (parser);
3143 if (c_parser_peek_token (parser)->type == CPP_NAME
3144 && ((c_parser_peek_token (parser)->id_kind
3145 == C_ID_TYPENAME)
3146 || (c_parser_peek_token (parser)->id_kind
3147 == C_ID_CLASSNAME)))
3149 /* Type name receiver. */
3150 tree id = c_parser_peek_token (parser)->value;
3151 c_parser_consume_token (parser);
3152 rec = objc_get_class_reference (id);
3153 goto parse_message_args;
3155 first = c_parser_expr_no_commas (parser, NULL).value;
3156 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3157 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3158 goto array_desig_after_first;
3159 /* Expression receiver. So far only one part
3160 without commas has been parsed; there might be
3161 more of the expression. */
3162 rec = first;
3163 while (c_parser_next_token_is (parser, CPP_COMMA))
3165 struct c_expr next;
3166 c_parser_consume_token (parser);
3167 next = c_parser_expr_no_commas (parser, NULL);
3168 next = default_function_array_conversion (next);
3169 rec = build_compound_expr (rec, next.value);
3171 parse_message_args:
3172 /* Now parse the objc-message-args. */
3173 args = c_parser_objc_message_args (parser);
3174 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3175 "expected %<]%>");
3176 mexpr.value
3177 = objc_build_message_expr (build_tree_list (rec, args));
3178 mexpr.original_code = ERROR_MARK;
3179 mexpr.original_type = NULL;
3180 /* Now parse and process the remainder of the
3181 initializer, starting with this message
3182 expression as a primary-expression. */
3183 c_parser_initval (parser, &mexpr);
3184 return;
3186 c_parser_consume_token (parser);
3187 first = c_parser_expr_no_commas (parser, NULL).value;
3188 array_desig_after_first:
3189 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3191 ellipsis_loc = c_parser_peek_token (parser)->location;
3192 c_parser_consume_token (parser);
3193 second = c_parser_expr_no_commas (parser, NULL).value;
3195 else
3196 second = NULL_TREE;
3197 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3199 c_parser_consume_token (parser);
3200 set_init_index (first, second);
3201 if (second)
3202 pedwarn (ellipsis_loc, OPT_pedantic,
3203 "ISO C forbids specifying range of elements to initialize");
3205 else
3206 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3207 "expected %<]%>");
3210 if (des_seen >= 1)
3212 if (c_parser_next_token_is (parser, CPP_EQ))
3214 if (!flag_isoc99)
3215 pedwarn (des_loc, OPT_pedantic,
3216 "ISO C90 forbids specifying subobject to initialize");
3217 c_parser_consume_token (parser);
3219 else
3221 if (des_seen == 1)
3222 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3223 "obsolete use of designated initializer without %<=%>");
3224 else
3226 struct c_expr init;
3227 init.value = error_mark_node;
3228 init.original_code = ERROR_MARK;
3229 init.original_type = NULL;
3230 c_parser_error (parser, "expected %<=%>");
3231 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3232 process_init_element (init, false);
3233 return;
3238 c_parser_initval (parser, NULL);
3241 /* Parse a nested initializer; as c_parser_initializer but parses
3242 initializers within braced lists, after any designators have been
3243 applied. If AFTER is not NULL then it is an Objective-C message
3244 expression which is the primary-expression starting the
3245 initializer. */
3247 static void
3248 c_parser_initval (c_parser *parser, struct c_expr *after)
3250 struct c_expr init;
3251 gcc_assert (!after || c_dialect_objc ());
3252 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3253 init = c_parser_braced_init (parser, NULL_TREE, true);
3254 else
3256 init = c_parser_expr_no_commas (parser, after);
3257 if (init.value != NULL_TREE
3258 && TREE_CODE (init.value) != STRING_CST
3259 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3260 init = default_function_array_conversion (init);
3262 process_init_element (init, false);
3265 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3266 C99 6.8.2).
3268 compound-statement:
3269 { block-item-list[opt] }
3270 { label-declarations block-item-list }
3272 block-item-list:
3273 block-item
3274 block-item-list block-item
3276 block-item:
3277 nested-declaration
3278 statement
3280 nested-declaration:
3281 declaration
3283 GNU extensions:
3285 compound-statement:
3286 { label-declarations block-item-list }
3288 nested-declaration:
3289 __extension__ nested-declaration
3290 nested-function-definition
3292 label-declarations:
3293 label-declaration
3294 label-declarations label-declaration
3296 label-declaration:
3297 __label__ identifier-list ;
3299 Allowing the mixing of declarations and code is new in C99. The
3300 GNU syntax also permits (not shown above) labels at the end of
3301 compound statements, which yield an error. We don't allow labels
3302 on declarations; this might seem like a natural extension, but
3303 there would be a conflict between attributes on the label and
3304 prefix attributes on the declaration. ??? The syntax follows the
3305 old parser in requiring something after label declarations.
3306 Although they are erroneous if the labels declared aren't defined,
3307 is it useful for the syntax to be this way?
3309 OpenMP:
3311 block-item:
3312 openmp-directive
3314 openmp-directive:
3315 barrier-directive
3316 flush-directive */
3318 static tree
3319 c_parser_compound_statement (c_parser *parser)
3321 tree stmt;
3322 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3324 /* Ensure a scope is entered and left anyway to avoid confusion
3325 if we have just prepared to enter a function body. */
3326 stmt = c_begin_compound_stmt (true);
3327 c_end_compound_stmt (stmt, true);
3328 return error_mark_node;
3330 stmt = c_begin_compound_stmt (true);
3331 c_parser_compound_statement_nostart (parser);
3332 return c_end_compound_stmt (stmt, true);
3335 /* Parse a compound statement except for the opening brace. This is
3336 used for parsing both compound statements and statement expressions
3337 (which follow different paths to handling the opening). */
3339 static void
3340 c_parser_compound_statement_nostart (c_parser *parser)
3342 bool last_stmt = false;
3343 bool last_label = false;
3344 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3345 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3347 c_parser_consume_token (parser);
3348 return;
3350 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3352 location_t err_loc = c_parser_peek_token (parser)->location;
3353 /* Read zero or more forward-declarations for labels that nested
3354 functions can jump to. */
3355 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3357 c_parser_consume_token (parser);
3358 /* Any identifiers, including those declared as type names,
3359 are OK here. */
3360 while (true)
3362 tree label;
3363 if (c_parser_next_token_is_not (parser, CPP_NAME))
3365 c_parser_error (parser, "expected identifier");
3366 break;
3368 label
3369 = declare_label (c_parser_peek_token (parser)->value);
3370 C_DECLARED_LABEL_FLAG (label) = 1;
3371 add_stmt (build_stmt (DECL_EXPR, label));
3372 c_parser_consume_token (parser);
3373 if (c_parser_next_token_is (parser, CPP_COMMA))
3374 c_parser_consume_token (parser);
3375 else
3376 break;
3378 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3380 pedwarn (err_loc, OPT_pedantic, "ISO C forbids label declarations");
3382 /* We must now have at least one statement, label or declaration. */
3383 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3385 c_parser_error (parser, "expected declaration or statement");
3386 c_parser_consume_token (parser);
3387 return;
3389 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3391 location_t loc = c_parser_peek_token (parser)->location;
3392 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3393 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3394 || (c_parser_next_token_is (parser, CPP_NAME)
3395 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3397 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3398 label_loc = c_parser_peek_2nd_token (parser)->location;
3399 else
3400 label_loc = c_parser_peek_token (parser)->location;
3401 last_label = true;
3402 last_stmt = false;
3403 c_parser_label (parser);
3405 else if (!last_label
3406 && c_parser_next_token_starts_declspecs (parser))
3408 last_label = false;
3409 c_parser_declaration_or_fndef (parser, true, true, true, true);
3410 if (last_stmt)
3411 pedwarn_c90 (loc,
3412 (pedantic && !flag_isoc99)
3413 ? OPT_pedantic
3414 : OPT_Wdeclaration_after_statement,
3415 "ISO C90 forbids mixed declarations and code");
3416 last_stmt = false;
3418 else if (!last_label
3419 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3421 /* __extension__ can start a declaration, but is also an
3422 unary operator that can start an expression. Consume all
3423 but the last of a possible series of __extension__ to
3424 determine which. */
3425 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3426 && (c_parser_peek_2nd_token (parser)->keyword
3427 == RID_EXTENSION))
3428 c_parser_consume_token (parser);
3429 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3431 int ext;
3432 ext = disable_extension_diagnostics ();
3433 c_parser_consume_token (parser);
3434 last_label = false;
3435 c_parser_declaration_or_fndef (parser, true, true, true, true);
3436 /* Following the old parser, __extension__ does not
3437 disable this diagnostic. */
3438 restore_extension_diagnostics (ext);
3439 if (last_stmt)
3440 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3441 ? OPT_pedantic
3442 : OPT_Wdeclaration_after_statement,
3443 "ISO C90 forbids mixed declarations and code");
3444 last_stmt = false;
3446 else
3447 goto statement;
3449 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3451 /* External pragmas, and some omp pragmas, are not associated
3452 with regular c code, and so are not to be considered statements
3453 syntactically. This ensures that the user doesn't put them
3454 places that would turn into syntax errors if the directive
3455 were ignored. */
3456 if (c_parser_pragma (parser, pragma_compound))
3457 last_label = false, last_stmt = true;
3459 else if (c_parser_next_token_is (parser, CPP_EOF))
3461 c_parser_error (parser, "expected declaration or statement");
3462 return;
3464 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3466 if (parser->in_if_block)
3468 error_at (loc, """expected %<}%> before %<else%>");
3469 return;
3471 else
3473 error_at (loc, "%<else%> without a previous %<if%>");
3474 c_parser_consume_token (parser);
3475 continue;
3478 else
3480 statement:
3481 last_label = false;
3482 last_stmt = true;
3483 c_parser_statement_after_labels (parser);
3486 parser->error = false;
3488 if (last_label)
3489 error_at (label_loc, "label at end of compound statement");
3490 c_parser_consume_token (parser);
3493 /* Parse a label (C90 6.6.1, C99 6.8.1).
3495 label:
3496 identifier : attributes[opt]
3497 case constant-expression :
3498 default :
3500 GNU extensions:
3502 label:
3503 case constant-expression ... constant-expression :
3505 The use of attributes on labels is a GNU extension. The syntax in
3506 GNU C accepts any expressions without commas, non-constant
3507 expressions being rejected later. */
3509 static void
3510 c_parser_label (c_parser *parser)
3512 location_t loc1 = c_parser_peek_token (parser)->location;
3513 tree label = NULL_TREE;
3514 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3516 tree exp1, exp2;
3517 c_parser_consume_token (parser);
3518 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3519 if (c_parser_next_token_is (parser, CPP_COLON))
3521 c_parser_consume_token (parser);
3522 label = do_case (exp1, NULL_TREE);
3524 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3526 c_parser_consume_token (parser);
3527 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3528 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3529 label = do_case (exp1, exp2);
3531 else
3532 c_parser_error (parser, "expected %<:%> or %<...%>");
3534 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3536 c_parser_consume_token (parser);
3537 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3538 label = do_case (NULL_TREE, NULL_TREE);
3540 else
3542 tree name = c_parser_peek_token (parser)->value;
3543 tree tlab;
3544 tree attrs;
3545 location_t loc2 = c_parser_peek_token (parser)->location;
3546 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3547 c_parser_consume_token (parser);
3548 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3549 c_parser_consume_token (parser);
3550 attrs = c_parser_attributes (parser);
3551 tlab = define_label (loc2, name);
3552 if (tlab)
3554 decl_attributes (&tlab, attrs, 0);
3555 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3558 if (label)
3560 SET_EXPR_LOCATION (label, loc1);
3561 if (c_parser_next_token_starts_declspecs (parser)
3562 && !(c_parser_next_token_is (parser, CPP_NAME)
3563 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3565 error_at (c_parser_peek_token (parser)->location,
3566 "a label can only be part of a statement and "
3567 "a declaration is not a statement");
3568 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3569 /*nested*/ true, /*empty_ok*/ false,
3570 /*start_attr_ok*/ true);
3575 /* Parse a statement (C90 6.6, C99 6.8).
3577 statement:
3578 labeled-statement
3579 compound-statement
3580 expression-statement
3581 selection-statement
3582 iteration-statement
3583 jump-statement
3585 labeled-statement:
3586 label statement
3588 expression-statement:
3589 expression[opt] ;
3591 selection-statement:
3592 if-statement
3593 switch-statement
3595 iteration-statement:
3596 while-statement
3597 do-statement
3598 for-statement
3600 jump-statement:
3601 goto identifier ;
3602 continue ;
3603 break ;
3604 return expression[opt] ;
3606 GNU extensions:
3608 statement:
3609 asm-statement
3611 jump-statement:
3612 goto * expression ;
3614 Objective-C:
3616 statement:
3617 objc-throw-statement
3618 objc-try-catch-statement
3619 objc-synchronized-statement
3621 objc-throw-statement:
3622 @throw expression ;
3623 @throw ;
3625 OpenMP:
3627 statement:
3628 openmp-construct
3630 openmp-construct:
3631 parallel-construct
3632 for-construct
3633 sections-construct
3634 single-construct
3635 parallel-for-construct
3636 parallel-sections-construct
3637 master-construct
3638 critical-construct
3639 atomic-construct
3640 ordered-construct
3642 parallel-construct:
3643 parallel-directive structured-block
3645 for-construct:
3646 for-directive iteration-statement
3648 sections-construct:
3649 sections-directive section-scope
3651 single-construct:
3652 single-directive structured-block
3654 parallel-for-construct:
3655 parallel-for-directive iteration-statement
3657 parallel-sections-construct:
3658 parallel-sections-directive section-scope
3660 master-construct:
3661 master-directive structured-block
3663 critical-construct:
3664 critical-directive structured-block
3666 atomic-construct:
3667 atomic-directive expression-statement
3669 ordered-construct:
3670 ordered-directive structured-block */
3672 static void
3673 c_parser_statement (c_parser *parser)
3675 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3676 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3677 || (c_parser_next_token_is (parser, CPP_NAME)
3678 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3679 c_parser_label (parser);
3680 c_parser_statement_after_labels (parser);
3683 /* Parse a statement, other than a labeled statement. */
3685 static void
3686 c_parser_statement_after_labels (c_parser *parser)
3688 location_t loc = c_parser_peek_token (parser)->location;
3689 tree stmt = NULL_TREE;
3690 bool in_if_block = parser->in_if_block;
3691 parser->in_if_block = false;
3692 switch (c_parser_peek_token (parser)->type)
3694 case CPP_OPEN_BRACE:
3695 add_stmt (c_parser_compound_statement (parser));
3696 break;
3697 case CPP_KEYWORD:
3698 switch (c_parser_peek_token (parser)->keyword)
3700 case RID_IF:
3701 c_parser_if_statement (parser);
3702 break;
3703 case RID_SWITCH:
3704 c_parser_switch_statement (parser);
3705 break;
3706 case RID_WHILE:
3707 c_parser_while_statement (parser);
3708 break;
3709 case RID_DO:
3710 c_parser_do_statement (parser);
3711 break;
3712 case RID_FOR:
3713 c_parser_for_statement (parser);
3714 break;
3715 case RID_GOTO:
3716 c_parser_consume_token (parser);
3717 if (c_parser_next_token_is (parser, CPP_NAME))
3719 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3720 c_parser_consume_token (parser);
3722 else if (c_parser_next_token_is (parser, CPP_MULT))
3724 c_parser_consume_token (parser);
3725 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3727 else
3728 c_parser_error (parser, "expected identifier or %<*%>");
3729 goto expect_semicolon;
3730 case RID_CONTINUE:
3731 c_parser_consume_token (parser);
3732 stmt = c_finish_bc_stmt (&c_cont_label, false);
3733 goto expect_semicolon;
3734 case RID_BREAK:
3735 c_parser_consume_token (parser);
3736 stmt = c_finish_bc_stmt (&c_break_label, true);
3737 goto expect_semicolon;
3738 case RID_RETURN:
3739 c_parser_consume_token (parser);
3740 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3742 stmt = c_finish_return (NULL_TREE);
3743 c_parser_consume_token (parser);
3745 else
3747 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3748 goto expect_semicolon;
3750 break;
3751 case RID_ASM:
3752 stmt = c_parser_asm_statement (parser);
3753 break;
3754 case RID_THROW:
3755 gcc_assert (c_dialect_objc ());
3756 c_parser_consume_token (parser);
3757 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3759 stmt = objc_build_throw_stmt (NULL_TREE);
3760 c_parser_consume_token (parser);
3762 else
3764 tree expr = c_parser_expression (parser).value;
3765 expr = c_fully_fold (expr, false, NULL);
3766 stmt = objc_build_throw_stmt (expr);
3767 goto expect_semicolon;
3769 break;
3770 case RID_TRY:
3771 gcc_assert (c_dialect_objc ());
3772 c_parser_objc_try_catch_statement (parser);
3773 break;
3774 case RID_AT_SYNCHRONIZED:
3775 gcc_assert (c_dialect_objc ());
3776 c_parser_objc_synchronized_statement (parser);
3777 break;
3778 default:
3779 goto expr_stmt;
3781 break;
3782 case CPP_SEMICOLON:
3783 c_parser_consume_token (parser);
3784 break;
3785 case CPP_CLOSE_PAREN:
3786 case CPP_CLOSE_SQUARE:
3787 /* Avoid infinite loop in error recovery:
3788 c_parser_skip_until_found stops at a closing nesting
3789 delimiter without consuming it, but here we need to consume
3790 it to proceed further. */
3791 c_parser_error (parser, "expected statement");
3792 c_parser_consume_token (parser);
3793 break;
3794 case CPP_PRAGMA:
3795 c_parser_pragma (parser, pragma_stmt);
3796 break;
3797 default:
3798 expr_stmt:
3799 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3800 expect_semicolon:
3801 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3802 break;
3804 /* Two cases cannot and do not have line numbers associated: If stmt
3805 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3806 cannot hold line numbers. But that's OK because the statement
3807 will either be changed to a MODIFY_EXPR during gimplification of
3808 the statement expr, or discarded. If stmt was compound, but
3809 without new variables, we will have skipped the creation of a
3810 BIND and will have a bare STATEMENT_LIST. But that's OK because
3811 (recursively) all of the component statements should already have
3812 line numbers assigned. ??? Can we discard no-op statements
3813 earlier? */
3814 protected_set_expr_location (stmt, loc);
3816 parser->in_if_block = in_if_block;
3819 /* Parse the condition from an if, do, while or for statements. */
3821 static tree
3822 c_parser_condition (c_parser *parser)
3824 location_t loc;
3825 tree cond;
3826 loc = c_parser_peek_token (parser)->location;
3827 cond = c_parser_expression_conv (parser).value;
3828 cond = c_objc_common_truthvalue_conversion (loc, cond);
3829 cond = c_fully_fold (cond, false, NULL);
3830 protected_set_expr_location (cond, loc);
3831 if (warn_sequence_point)
3832 verify_sequence_points (cond);
3833 return cond;
3836 /* Parse a parenthesized condition from an if, do or while statement.
3838 condition:
3839 ( expression )
3841 static tree
3842 c_parser_paren_condition (c_parser *parser)
3844 tree cond;
3845 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3846 return error_mark_node;
3847 cond = c_parser_condition (parser);
3848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3849 return cond;
3852 /* Parse a statement which is a block in C99. */
3854 static tree
3855 c_parser_c99_block_statement (c_parser *parser)
3857 tree block = c_begin_compound_stmt (flag_isoc99);
3858 c_parser_statement (parser);
3859 return c_end_compound_stmt (block, flag_isoc99);
3862 /* Parse the body of an if statement. This is just parsing a
3863 statement but (a) it is a block in C99, (b) we track whether the
3864 body is an if statement for the sake of -Wparentheses warnings, (c)
3865 we handle an empty body specially for the sake of -Wempty-body
3866 warnings, and (d) we call parser_compound_statement directly
3867 because c_parser_statement_after_labels resets
3868 parser->in_if_block. */
3870 static tree
3871 c_parser_if_body (c_parser *parser, bool *if_p)
3873 tree block = c_begin_compound_stmt (flag_isoc99);
3874 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3875 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3876 || (c_parser_next_token_is (parser, CPP_NAME)
3877 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3878 c_parser_label (parser);
3879 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3880 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3882 location_t loc = c_parser_peek_token (parser)->location;
3883 add_stmt (build_empty_stmt ());
3884 c_parser_consume_token (parser);
3885 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
3886 warning_at (loc, OPT_Wempty_body,
3887 "suggest braces around empty body in an %<if%> statement");
3889 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3890 add_stmt (c_parser_compound_statement (parser));
3891 else
3892 c_parser_statement_after_labels (parser);
3893 return c_end_compound_stmt (block, flag_isoc99);
3896 /* Parse the else body of an if statement. This is just parsing a
3897 statement but (a) it is a block in C99, (b) we handle an empty body
3898 specially for the sake of -Wempty-body warnings. */
3900 static tree
3901 c_parser_else_body (c_parser *parser)
3903 tree block = c_begin_compound_stmt (flag_isoc99);
3904 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3905 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3906 || (c_parser_next_token_is (parser, CPP_NAME)
3907 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3908 c_parser_label (parser);
3909 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3911 warning_at (c_parser_peek_token (parser)->location,
3912 OPT_Wempty_body,
3913 "suggest braces around empty body in an %<else%> statement");
3914 add_stmt (build_empty_stmt ());
3915 c_parser_consume_token (parser);
3917 else
3918 c_parser_statement_after_labels (parser);
3919 return c_end_compound_stmt (block, flag_isoc99);
3922 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3924 if-statement:
3925 if ( expression ) statement
3926 if ( expression ) statement else statement
3929 static void
3930 c_parser_if_statement (c_parser *parser)
3932 tree block;
3933 location_t loc;
3934 tree cond;
3935 bool first_if = false;
3936 tree first_body, second_body;
3937 bool in_if_block;
3939 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3940 c_parser_consume_token (parser);
3941 block = c_begin_compound_stmt (flag_isoc99);
3942 loc = c_parser_peek_token (parser)->location;
3943 cond = c_parser_paren_condition (parser);
3944 in_if_block = parser->in_if_block;
3945 parser->in_if_block = true;
3946 first_body = c_parser_if_body (parser, &first_if);
3947 parser->in_if_block = in_if_block;
3948 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3950 c_parser_consume_token (parser);
3951 second_body = c_parser_else_body (parser);
3953 else
3954 second_body = NULL_TREE;
3955 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3956 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3959 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3961 switch-statement:
3962 switch (expression) statement
3965 static void
3966 c_parser_switch_statement (c_parser *parser)
3968 tree block, expr, body, save_break;
3969 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3970 c_parser_consume_token (parser);
3971 block = c_begin_compound_stmt (flag_isoc99);
3972 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3974 expr = c_parser_expression (parser).value;
3975 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3977 else
3978 expr = error_mark_node;
3979 c_start_case (expr);
3980 save_break = c_break_label;
3981 c_break_label = NULL_TREE;
3982 body = c_parser_c99_block_statement (parser);
3983 c_finish_case (body);
3984 if (c_break_label)
3985 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3986 c_break_label = save_break;
3987 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3990 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3992 while-statement:
3993 while (expression) statement
3996 static void
3997 c_parser_while_statement (c_parser *parser)
3999 tree block, cond, body, save_break, save_cont;
4000 location_t loc;
4001 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4002 c_parser_consume_token (parser);
4003 block = c_begin_compound_stmt (flag_isoc99);
4004 loc = c_parser_peek_token (parser)->location;
4005 cond = c_parser_paren_condition (parser);
4006 save_break = c_break_label;
4007 c_break_label = NULL_TREE;
4008 save_cont = c_cont_label;
4009 c_cont_label = NULL_TREE;
4010 body = c_parser_c99_block_statement (parser);
4011 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4012 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4013 c_break_label = save_break;
4014 c_cont_label = save_cont;
4017 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4019 do-statement:
4020 do statement while ( expression ) ;
4023 static void
4024 c_parser_do_statement (c_parser *parser)
4026 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4027 location_t loc;
4028 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4029 c_parser_consume_token (parser);
4030 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4031 warning_at (c_parser_peek_token (parser)->location,
4032 OPT_Wempty_body,
4033 "suggest braces around empty body in %<do%> statement");
4034 block = c_begin_compound_stmt (flag_isoc99);
4035 loc = c_parser_peek_token (parser)->location;
4036 save_break = c_break_label;
4037 c_break_label = NULL_TREE;
4038 save_cont = c_cont_label;
4039 c_cont_label = NULL_TREE;
4040 body = c_parser_c99_block_statement (parser);
4041 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4042 new_break = c_break_label;
4043 c_break_label = save_break;
4044 new_cont = c_cont_label;
4045 c_cont_label = save_cont;
4046 cond = c_parser_paren_condition (parser);
4047 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4048 c_parser_skip_to_end_of_block_or_statement (parser);
4049 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4050 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4053 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4055 for-statement:
4056 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4057 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4059 The form with a declaration is new in C99.
4061 ??? In accordance with the old parser, the declaration may be a
4062 nested function, which is then rejected in check_for_loop_decls,
4063 but does it make any sense for this to be included in the grammar?
4064 Note in particular that the nested function does not include a
4065 trailing ';', whereas the "declaration" production includes one.
4066 Also, can we reject bad declarations earlier and cheaper than
4067 check_for_loop_decls? */
4069 static void
4070 c_parser_for_statement (c_parser *parser)
4072 tree block, cond, incr, save_break, save_cont, body;
4073 location_t loc;
4074 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4075 loc = c_parser_peek_token (parser)->location;
4076 c_parser_consume_token (parser);
4077 block = c_begin_compound_stmt (flag_isoc99);
4078 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4080 /* Parse the initialization declaration or expression. */
4081 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4083 c_parser_consume_token (parser);
4084 c_finish_expr_stmt (NULL_TREE);
4086 else if (c_parser_next_token_starts_declspecs (parser))
4088 c_parser_declaration_or_fndef (parser, true, true, true, true);
4089 check_for_loop_decls ();
4091 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4093 /* __extension__ can start a declaration, but is also an
4094 unary operator that can start an expression. Consume all
4095 but the last of a possible series of __extension__ to
4096 determine which. */
4097 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4098 && (c_parser_peek_2nd_token (parser)->keyword
4099 == RID_EXTENSION))
4100 c_parser_consume_token (parser);
4101 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4103 int ext;
4104 ext = disable_extension_diagnostics ();
4105 c_parser_consume_token (parser);
4106 c_parser_declaration_or_fndef (parser, true, true, true, true);
4107 restore_extension_diagnostics (ext);
4108 check_for_loop_decls ();
4110 else
4111 goto init_expr;
4113 else
4115 init_expr:
4116 c_finish_expr_stmt (c_parser_expression (parser).value);
4117 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4119 /* Parse the loop condition. */
4120 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4122 c_parser_consume_token (parser);
4123 cond = NULL_TREE;
4125 else
4127 cond = c_parser_condition (parser);
4128 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4130 /* Parse the increment expression. */
4131 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4132 incr = c_process_expr_stmt (NULL_TREE);
4133 else
4134 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4135 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4137 else
4139 cond = error_mark_node;
4140 incr = error_mark_node;
4142 save_break = c_break_label;
4143 c_break_label = NULL_TREE;
4144 save_cont = c_cont_label;
4145 c_cont_label = NULL_TREE;
4146 body = c_parser_c99_block_statement (parser);
4147 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4148 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4149 c_break_label = save_break;
4150 c_cont_label = save_cont;
4153 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4154 statement with inputs, outputs, clobbers, and volatile tag
4155 allowed.
4157 asm-statement:
4158 asm type-qualifier[opt] ( asm-argument ) ;
4160 asm-argument:
4161 asm-string-literal
4162 asm-string-literal : asm-operands[opt]
4163 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4164 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4166 Qualifiers other than volatile are accepted in the syntax but
4167 warned for. */
4169 static tree
4170 c_parser_asm_statement (c_parser *parser)
4172 tree quals, str, outputs, inputs, clobbers, ret;
4173 bool simple;
4174 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4175 c_parser_consume_token (parser);
4176 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4178 quals = c_parser_peek_token (parser)->value;
4179 c_parser_consume_token (parser);
4181 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4182 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4184 warning_at (c_parser_peek_token (parser)->location,
4186 "%E qualifier ignored on asm",
4187 c_parser_peek_token (parser)->value);
4188 quals = NULL_TREE;
4189 c_parser_consume_token (parser);
4191 else
4192 quals = NULL_TREE;
4193 /* ??? Follow the C++ parser rather than using the
4194 lex_untranslated_string kludge. */
4195 parser->lex_untranslated_string = true;
4196 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4198 parser->lex_untranslated_string = false;
4199 return NULL_TREE;
4201 str = c_parser_asm_string_literal (parser);
4202 if (str == NULL_TREE)
4204 parser->lex_untranslated_string = false;
4205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4206 return NULL_TREE;
4208 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4210 simple = true;
4211 outputs = NULL_TREE;
4212 inputs = NULL_TREE;
4213 clobbers = NULL_TREE;
4214 goto done_asm;
4216 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4218 parser->lex_untranslated_string = false;
4219 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4220 return NULL_TREE;
4222 simple = false;
4223 /* Parse outputs. */
4224 if (c_parser_next_token_is (parser, CPP_COLON)
4225 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4226 outputs = NULL_TREE;
4227 else
4228 outputs = c_parser_asm_operands (parser, false);
4229 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4231 inputs = NULL_TREE;
4232 clobbers = NULL_TREE;
4233 goto done_asm;
4235 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4237 parser->lex_untranslated_string = false;
4238 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4239 return NULL_TREE;
4241 /* Parse inputs. */
4242 if (c_parser_next_token_is (parser, CPP_COLON)
4243 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4244 inputs = NULL_TREE;
4245 else
4246 inputs = c_parser_asm_operands (parser, true);
4247 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4249 clobbers = NULL_TREE;
4250 goto done_asm;
4252 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4254 parser->lex_untranslated_string = false;
4255 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4256 return NULL_TREE;
4258 /* Parse clobbers. */
4259 clobbers = c_parser_asm_clobbers (parser);
4260 done_asm:
4261 parser->lex_untranslated_string = false;
4262 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4265 return NULL_TREE;
4267 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4268 c_parser_skip_to_end_of_block_or_statement (parser);
4269 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4270 clobbers, simple));
4271 return ret;
4274 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4275 not outputs), apply the default conversion of functions and arrays
4276 to pointers.
4278 asm-operands:
4279 asm-operand
4280 asm-operands , asm-operand
4282 asm-operand:
4283 asm-string-literal ( expression )
4284 [ identifier ] asm-string-literal ( expression )
4287 static tree
4288 c_parser_asm_operands (c_parser *parser, bool convert_p)
4290 tree list = NULL_TREE;
4291 while (true)
4293 tree name, str;
4294 struct c_expr expr;
4295 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4297 c_parser_consume_token (parser);
4298 if (c_parser_next_token_is (parser, CPP_NAME))
4300 tree id = c_parser_peek_token (parser)->value;
4301 c_parser_consume_token (parser);
4302 name = build_string (IDENTIFIER_LENGTH (id),
4303 IDENTIFIER_POINTER (id));
4305 else
4307 c_parser_error (parser, "expected identifier");
4308 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4309 return NULL_TREE;
4311 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4312 "expected %<]%>");
4314 else
4315 name = NULL_TREE;
4316 str = c_parser_asm_string_literal (parser);
4317 if (str == NULL_TREE)
4318 return NULL_TREE;
4319 parser->lex_untranslated_string = false;
4320 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4322 parser->lex_untranslated_string = true;
4323 return NULL_TREE;
4325 expr = c_parser_expression (parser);
4326 if (convert_p)
4327 expr = default_function_array_conversion (expr);
4328 expr.value = c_fully_fold (expr.value, false, NULL);
4329 parser->lex_untranslated_string = true;
4330 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4332 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4333 return NULL_TREE;
4335 list = chainon (list, build_tree_list (build_tree_list (name, str),
4336 expr.value));
4337 if (c_parser_next_token_is (parser, CPP_COMMA))
4338 c_parser_consume_token (parser);
4339 else
4340 break;
4342 return list;
4345 /* Parse asm clobbers, a GNU extension.
4347 asm-clobbers:
4348 asm-string-literal
4349 asm-clobbers , asm-string-literal
4352 static tree
4353 c_parser_asm_clobbers (c_parser *parser)
4355 tree list = NULL_TREE;
4356 while (true)
4358 tree str = c_parser_asm_string_literal (parser);
4359 if (str)
4360 list = tree_cons (NULL_TREE, str, list);
4361 else
4362 return NULL_TREE;
4363 if (c_parser_next_token_is (parser, CPP_COMMA))
4364 c_parser_consume_token (parser);
4365 else
4366 break;
4368 return list;
4371 /* Parse an expression other than a compound expression; that is, an
4372 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4373 NULL then it is an Objective-C message expression which is the
4374 primary-expression starting the expression as an initializer.
4376 assignment-expression:
4377 conditional-expression
4378 unary-expression assignment-operator assignment-expression
4380 assignment-operator: one of
4381 = *= /= %= += -= <<= >>= &= ^= |=
4383 In GNU C we accept any conditional expression on the LHS and
4384 diagnose the invalid lvalue rather than producing a syntax
4385 error. */
4387 static struct c_expr
4388 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4390 struct c_expr lhs, rhs, ret;
4391 enum tree_code code;
4392 location_t op_location;
4393 gcc_assert (!after || c_dialect_objc ());
4394 lhs = c_parser_conditional_expression (parser, after);
4395 op_location = c_parser_peek_token (parser)->location;
4396 switch (c_parser_peek_token (parser)->type)
4398 case CPP_EQ:
4399 code = NOP_EXPR;
4400 break;
4401 case CPP_MULT_EQ:
4402 code = MULT_EXPR;
4403 break;
4404 case CPP_DIV_EQ:
4405 code = TRUNC_DIV_EXPR;
4406 break;
4407 case CPP_MOD_EQ:
4408 code = TRUNC_MOD_EXPR;
4409 break;
4410 case CPP_PLUS_EQ:
4411 code = PLUS_EXPR;
4412 break;
4413 case CPP_MINUS_EQ:
4414 code = MINUS_EXPR;
4415 break;
4416 case CPP_LSHIFT_EQ:
4417 code = LSHIFT_EXPR;
4418 break;
4419 case CPP_RSHIFT_EQ:
4420 code = RSHIFT_EXPR;
4421 break;
4422 case CPP_AND_EQ:
4423 code = BIT_AND_EXPR;
4424 break;
4425 case CPP_XOR_EQ:
4426 code = BIT_XOR_EXPR;
4427 break;
4428 case CPP_OR_EQ:
4429 code = BIT_IOR_EXPR;
4430 break;
4431 default:
4432 return lhs;
4434 c_parser_consume_token (parser);
4435 rhs = c_parser_expr_no_commas (parser, NULL);
4436 rhs = default_function_array_conversion (rhs);
4437 ret.value = build_modify_expr (op_location, lhs.value, code, rhs.value);
4438 if (code == NOP_EXPR)
4439 ret.original_code = MODIFY_EXPR;
4440 else
4442 TREE_NO_WARNING (ret.value) = 1;
4443 ret.original_code = ERROR_MARK;
4445 ret.original_type = NULL;
4446 return ret;
4449 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4450 is not NULL then it is an Objective-C message expression which is
4451 the primary-expression starting the expression as an initializer.
4453 conditional-expression:
4454 logical-OR-expression
4455 logical-OR-expression ? expression : conditional-expression
4457 GNU extensions:
4459 conditional-expression:
4460 logical-OR-expression ? : conditional-expression
4463 static struct c_expr
4464 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4466 struct c_expr cond, exp1, exp2, ret;
4467 location_t cond_loc;
4469 gcc_assert (!after || c_dialect_objc ());
4471 cond_loc = c_parser_peek_token (parser)->location;
4472 cond = c_parser_binary_expression (parser, after);
4473 protected_set_expr_location (cond.value, cond_loc);
4475 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4476 return cond;
4477 cond = default_function_array_conversion (cond);
4478 c_parser_consume_token (parser);
4479 if (c_parser_next_token_is (parser, CPP_COLON))
4481 tree eptype = NULL_TREE;
4482 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4483 "ISO C forbids omitting the middle term of a ?: expression");
4484 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4486 eptype = TREE_TYPE (cond.value);
4487 cond.value = TREE_OPERAND (cond.value, 0);
4489 /* Make sure first operand is calculated only once. */
4490 exp1.value = c_save_expr (default_conversion (cond.value));
4491 if (eptype)
4492 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
4493 exp1.original_type = NULL;
4494 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4495 skip_evaluation += cond.value == truthvalue_true_node;
4497 else
4499 cond.value
4500 = c_objc_common_truthvalue_conversion
4501 (cond_loc, default_conversion (cond.value));
4502 skip_evaluation += cond.value == truthvalue_false_node;
4503 exp1 = c_parser_expression_conv (parser);
4504 skip_evaluation += ((cond.value == truthvalue_true_node)
4505 - (cond.value == truthvalue_false_node));
4507 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4509 skip_evaluation -= cond.value == truthvalue_true_node;
4510 ret.value = error_mark_node;
4511 ret.original_code = ERROR_MARK;
4512 ret.original_type = NULL;
4513 return ret;
4515 exp2 = c_parser_conditional_expression (parser, NULL);
4516 exp2 = default_function_array_conversion (exp2);
4517 skip_evaluation -= cond.value == truthvalue_true_node;
4518 ret.value = build_conditional_expr (cond.value,
4519 cond.original_code == C_MAYBE_CONST_EXPR,
4520 exp1.value, exp2.value);
4521 ret.original_code = ERROR_MARK;
4522 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4523 ret.original_type = NULL;
4524 else
4526 tree t1, t2;
4528 /* If both sides are enum type, the default conversion will have
4529 made the type of the result be an integer type. We want to
4530 remember the enum types we started with. */
4531 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4532 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4533 ret.original_type = ((t1 != error_mark_node
4534 && t2 != error_mark_node
4535 && (TYPE_MAIN_VARIANT (t1)
4536 == TYPE_MAIN_VARIANT (t2)))
4537 ? t1
4538 : NULL);
4540 return ret;
4543 /* Parse a binary expression; that is, a logical-OR-expression (C90
4544 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4545 an Objective-C message expression which is the primary-expression
4546 starting the expression as an initializer.
4548 multiplicative-expression:
4549 cast-expression
4550 multiplicative-expression * cast-expression
4551 multiplicative-expression / cast-expression
4552 multiplicative-expression % cast-expression
4554 additive-expression:
4555 multiplicative-expression
4556 additive-expression + multiplicative-expression
4557 additive-expression - multiplicative-expression
4559 shift-expression:
4560 additive-expression
4561 shift-expression << additive-expression
4562 shift-expression >> additive-expression
4564 relational-expression:
4565 shift-expression
4566 relational-expression < shift-expression
4567 relational-expression > shift-expression
4568 relational-expression <= shift-expression
4569 relational-expression >= shift-expression
4571 equality-expression:
4572 relational-expression
4573 equality-expression == relational-expression
4574 equality-expression != relational-expression
4576 AND-expression:
4577 equality-expression
4578 AND-expression & equality-expression
4580 exclusive-OR-expression:
4581 AND-expression
4582 exclusive-OR-expression ^ AND-expression
4584 inclusive-OR-expression:
4585 exclusive-OR-expression
4586 inclusive-OR-expression | exclusive-OR-expression
4588 logical-AND-expression:
4589 inclusive-OR-expression
4590 logical-AND-expression && inclusive-OR-expression
4592 logical-OR-expression:
4593 logical-AND-expression
4594 logical-OR-expression || logical-AND-expression
4597 static struct c_expr
4598 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4600 /* A binary expression is parsed using operator-precedence parsing,
4601 with the operands being cast expressions. All the binary
4602 operators are left-associative. Thus a binary expression is of
4603 form:
4605 E0 op1 E1 op2 E2 ...
4607 which we represent on a stack. On the stack, the precedence
4608 levels are strictly increasing. When a new operator is
4609 encountered of higher precedence than that at the top of the
4610 stack, it is pushed; its LHS is the top expression, and its RHS
4611 is everything parsed until it is popped. When a new operator is
4612 encountered with precedence less than or equal to that at the top
4613 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4614 by the result of the operation until the operator at the top of
4615 the stack has lower precedence than the new operator or there is
4616 only one element on the stack; then the top expression is the LHS
4617 of the new operator. In the case of logical AND and OR
4618 expressions, we also need to adjust skip_evaluation as
4619 appropriate when the operators are pushed and popped. */
4621 /* The precedence levels, where 0 is a dummy lowest level used for
4622 the bottom of the stack. */
4623 enum prec {
4624 PREC_NONE,
4625 PREC_LOGOR,
4626 PREC_LOGAND,
4627 PREC_BITOR,
4628 PREC_BITXOR,
4629 PREC_BITAND,
4630 PREC_EQ,
4631 PREC_REL,
4632 PREC_SHIFT,
4633 PREC_ADD,
4634 PREC_MULT,
4635 NUM_PRECS
4637 struct {
4638 /* The expression at this stack level. */
4639 struct c_expr expr;
4640 /* The precedence of the operator on its left, PREC_NONE at the
4641 bottom of the stack. */
4642 enum prec prec;
4643 /* The operation on its left. */
4644 enum tree_code op;
4645 } stack[NUM_PRECS];
4646 int sp;
4647 /* Location of the binary operator. */
4648 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4649 #define POP \
4650 do { \
4651 switch (stack[sp].op) \
4653 case TRUTH_ANDIF_EXPR: \
4654 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4655 break; \
4656 case TRUTH_ORIF_EXPR: \
4657 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4658 break; \
4659 default: \
4660 break; \
4662 stack[sp - 1].expr \
4663 = default_function_array_conversion (stack[sp - 1].expr); \
4664 stack[sp].expr \
4665 = default_function_array_conversion (stack[sp].expr); \
4666 stack[sp - 1].expr = parser_build_binary_op (binary_loc, \
4667 stack[sp].op, \
4668 stack[sp - 1].expr, \
4669 stack[sp].expr); \
4670 sp--; \
4671 } while (0)
4672 gcc_assert (!after || c_dialect_objc ());
4673 stack[0].expr = c_parser_cast_expression (parser, after);
4674 stack[0].prec = PREC_NONE;
4675 sp = 0;
4676 while (true)
4678 enum prec oprec;
4679 enum tree_code ocode;
4680 if (parser->error)
4681 goto out;
4682 switch (c_parser_peek_token (parser)->type)
4684 case CPP_MULT:
4685 oprec = PREC_MULT;
4686 ocode = MULT_EXPR;
4687 break;
4688 case CPP_DIV:
4689 oprec = PREC_MULT;
4690 ocode = TRUNC_DIV_EXPR;
4691 break;
4692 case CPP_MOD:
4693 oprec = PREC_MULT;
4694 ocode = TRUNC_MOD_EXPR;
4695 break;
4696 case CPP_PLUS:
4697 oprec = PREC_ADD;
4698 ocode = PLUS_EXPR;
4699 break;
4700 case CPP_MINUS:
4701 oprec = PREC_ADD;
4702 ocode = MINUS_EXPR;
4703 break;
4704 case CPP_LSHIFT:
4705 oprec = PREC_SHIFT;
4706 ocode = LSHIFT_EXPR;
4707 break;
4708 case CPP_RSHIFT:
4709 oprec = PREC_SHIFT;
4710 ocode = RSHIFT_EXPR;
4711 break;
4712 case CPP_LESS:
4713 oprec = PREC_REL;
4714 ocode = LT_EXPR;
4715 break;
4716 case CPP_GREATER:
4717 oprec = PREC_REL;
4718 ocode = GT_EXPR;
4719 break;
4720 case CPP_LESS_EQ:
4721 oprec = PREC_REL;
4722 ocode = LE_EXPR;
4723 break;
4724 case CPP_GREATER_EQ:
4725 oprec = PREC_REL;
4726 ocode = GE_EXPR;
4727 break;
4728 case CPP_EQ_EQ:
4729 oprec = PREC_EQ;
4730 ocode = EQ_EXPR;
4731 break;
4732 case CPP_NOT_EQ:
4733 oprec = PREC_EQ;
4734 ocode = NE_EXPR;
4735 break;
4736 case CPP_AND:
4737 oprec = PREC_BITAND;
4738 ocode = BIT_AND_EXPR;
4739 break;
4740 case CPP_XOR:
4741 oprec = PREC_BITXOR;
4742 ocode = BIT_XOR_EXPR;
4743 break;
4744 case CPP_OR:
4745 oprec = PREC_BITOR;
4746 ocode = BIT_IOR_EXPR;
4747 break;
4748 case CPP_AND_AND:
4749 oprec = PREC_LOGAND;
4750 ocode = TRUTH_ANDIF_EXPR;
4751 break;
4752 case CPP_OR_OR:
4753 oprec = PREC_LOGOR;
4754 ocode = TRUTH_ORIF_EXPR;
4755 break;
4756 default:
4757 /* Not a binary operator, so end of the binary
4758 expression. */
4759 goto out;
4761 binary_loc = c_parser_peek_token (parser)->location;
4762 c_parser_consume_token (parser);
4763 while (oprec <= stack[sp].prec)
4764 POP;
4765 switch (ocode)
4767 case TRUTH_ANDIF_EXPR:
4768 stack[sp].expr
4769 = default_function_array_conversion (stack[sp].expr);
4770 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4771 (binary_loc, default_conversion (stack[sp].expr.value));
4772 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4773 break;
4774 case TRUTH_ORIF_EXPR:
4775 stack[sp].expr
4776 = default_function_array_conversion (stack[sp].expr);
4777 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4778 (binary_loc, default_conversion (stack[sp].expr.value));
4779 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4780 break;
4781 default:
4782 break;
4784 sp++;
4785 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4786 stack[sp].prec = oprec;
4787 stack[sp].op = ocode;
4789 out:
4790 while (sp > 0)
4791 POP;
4792 return stack[0].expr;
4793 #undef POP
4796 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4797 NULL then it is an Objective-C message expression which is the
4798 primary-expression starting the expression as an initializer.
4800 cast-expression:
4801 unary-expression
4802 ( type-name ) unary-expression
4805 static struct c_expr
4806 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4808 gcc_assert (!after || c_dialect_objc ());
4809 if (after)
4810 return c_parser_postfix_expression_after_primary (parser, *after);
4811 /* If the expression begins with a parenthesized type name, it may
4812 be either a cast or a compound literal; we need to see whether
4813 the next character is '{' to tell the difference. If not, it is
4814 an unary expression. */
4815 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4816 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4818 struct c_type_name *type_name;
4819 struct c_expr ret;
4820 struct c_expr expr;
4821 c_parser_consume_token (parser);
4822 type_name = c_parser_type_name (parser);
4823 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4824 if (type_name == NULL)
4826 ret.value = error_mark_node;
4827 ret.original_code = ERROR_MARK;
4828 ret.original_type = NULL;
4829 return ret;
4832 /* Save casted types in the function's used types hash table. */
4833 used_types_insert (type_name->specs->type);
4835 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4836 return c_parser_postfix_expression_after_paren_type (parser,
4837 type_name);
4838 expr = c_parser_cast_expression (parser, NULL);
4839 expr = default_function_array_conversion (expr);
4840 ret.value = c_cast_expr (type_name, expr.value);
4841 ret.original_code = ERROR_MARK;
4842 ret.original_type = NULL;
4843 return ret;
4845 else
4846 return c_parser_unary_expression (parser);
4849 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4851 unary-expression:
4852 postfix-expression
4853 ++ unary-expression
4854 -- unary-expression
4855 unary-operator cast-expression
4856 sizeof unary-expression
4857 sizeof ( type-name )
4859 unary-operator: one of
4860 & * + - ~ !
4862 GNU extensions:
4864 unary-expression:
4865 __alignof__ unary-expression
4866 __alignof__ ( type-name )
4867 && identifier
4869 unary-operator: one of
4870 __extension__ __real__ __imag__
4872 In addition, the GNU syntax treats ++ and -- as unary operators, so
4873 they may be applied to cast expressions with errors for non-lvalues
4874 given later. */
4876 static struct c_expr
4877 c_parser_unary_expression (c_parser *parser)
4879 int ext;
4880 struct c_expr ret, op;
4881 location_t loc = c_parser_peek_token (parser)->location;
4882 ret.original_code = ERROR_MARK;
4883 ret.original_type = NULL;
4884 switch (c_parser_peek_token (parser)->type)
4886 case CPP_PLUS_PLUS:
4887 c_parser_consume_token (parser);
4888 op = c_parser_cast_expression (parser, NULL);
4889 op = default_function_array_conversion (op);
4890 return parser_build_unary_op (PREINCREMENT_EXPR, op, loc);
4891 case CPP_MINUS_MINUS:
4892 c_parser_consume_token (parser);
4893 op = c_parser_cast_expression (parser, NULL);
4894 op = default_function_array_conversion (op);
4895 return parser_build_unary_op (PREDECREMENT_EXPR, op, loc);
4896 case CPP_AND:
4897 c_parser_consume_token (parser);
4898 return parser_build_unary_op (ADDR_EXPR,
4899 c_parser_cast_expression (parser, NULL),
4900 loc);
4901 case CPP_MULT:
4902 c_parser_consume_token (parser);
4903 op = c_parser_cast_expression (parser, NULL);
4904 op = default_function_array_conversion (op);
4905 ret.value = build_indirect_ref (loc, op.value, "unary *");
4906 return ret;
4907 case CPP_PLUS:
4908 if (!c_dialect_objc () && !in_system_header)
4909 warning_at (c_parser_peek_token (parser)->location,
4910 OPT_Wtraditional,
4911 "traditional C rejects the unary plus operator");
4912 c_parser_consume_token (parser);
4913 op = c_parser_cast_expression (parser, NULL);
4914 op = default_function_array_conversion (op);
4915 return parser_build_unary_op (CONVERT_EXPR, op, loc);
4916 case CPP_MINUS:
4917 c_parser_consume_token (parser);
4918 op = c_parser_cast_expression (parser, NULL);
4919 op = default_function_array_conversion (op);
4920 return parser_build_unary_op (NEGATE_EXPR, op, loc);
4921 case CPP_COMPL:
4922 c_parser_consume_token (parser);
4923 op = c_parser_cast_expression (parser, NULL);
4924 op = default_function_array_conversion (op);
4925 return parser_build_unary_op (BIT_NOT_EXPR, op, loc);
4926 case CPP_NOT:
4927 c_parser_consume_token (parser);
4928 op = c_parser_cast_expression (parser, NULL);
4929 op = default_function_array_conversion (op);
4930 return parser_build_unary_op (TRUTH_NOT_EXPR, op, loc);
4931 case CPP_AND_AND:
4932 /* Refer to the address of a label as a pointer. */
4933 c_parser_consume_token (parser);
4934 if (c_parser_next_token_is (parser, CPP_NAME))
4936 ret.value = finish_label_address_expr
4937 (c_parser_peek_token (parser)->value, loc);
4938 c_parser_consume_token (parser);
4940 else
4942 c_parser_error (parser, "expected identifier");
4943 ret.value = error_mark_node;
4945 return ret;
4946 case CPP_KEYWORD:
4947 switch (c_parser_peek_token (parser)->keyword)
4949 case RID_SIZEOF:
4950 return c_parser_sizeof_expression (parser);
4951 case RID_ALIGNOF:
4952 return c_parser_alignof_expression (parser);
4953 case RID_EXTENSION:
4954 c_parser_consume_token (parser);
4955 ext = disable_extension_diagnostics ();
4956 ret = c_parser_cast_expression (parser, NULL);
4957 restore_extension_diagnostics (ext);
4958 return ret;
4959 case RID_REALPART:
4960 c_parser_consume_token (parser);
4961 op = c_parser_cast_expression (parser, NULL);
4962 op = default_function_array_conversion (op);
4963 return parser_build_unary_op (REALPART_EXPR, op, loc);
4964 case RID_IMAGPART:
4965 c_parser_consume_token (parser);
4966 op = c_parser_cast_expression (parser, NULL);
4967 op = default_function_array_conversion (op);
4968 return parser_build_unary_op (IMAGPART_EXPR, op, loc);
4969 default:
4970 return c_parser_postfix_expression (parser);
4972 default:
4973 return c_parser_postfix_expression (parser);
4977 /* Parse a sizeof expression. */
4979 static struct c_expr
4980 c_parser_sizeof_expression (c_parser *parser)
4982 struct c_expr expr;
4983 location_t expr_loc;
4984 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4985 c_parser_consume_token (parser);
4986 skip_evaluation++;
4987 in_sizeof++;
4988 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4989 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4991 /* Either sizeof ( type-name ) or sizeof unary-expression
4992 starting with a compound literal. */
4993 struct c_type_name *type_name;
4994 c_parser_consume_token (parser);
4995 expr_loc = c_parser_peek_token (parser)->location;
4996 type_name = c_parser_type_name (parser);
4997 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4998 if (type_name == NULL)
5000 struct c_expr ret;
5001 skip_evaluation--;
5002 in_sizeof--;
5003 ret.value = error_mark_node;
5004 ret.original_code = ERROR_MARK;
5005 ret.original_type = NULL;
5006 return ret;
5008 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5010 expr = c_parser_postfix_expression_after_paren_type (parser,
5011 type_name);
5012 goto sizeof_expr;
5014 /* sizeof ( type-name ). */
5015 skip_evaluation--;
5016 in_sizeof--;
5017 return c_expr_sizeof_type (type_name);
5019 else
5021 expr_loc = c_parser_peek_token (parser)->location;
5022 expr = c_parser_unary_expression (parser);
5023 sizeof_expr:
5024 skip_evaluation--;
5025 in_sizeof--;
5026 if (TREE_CODE (expr.value) == COMPONENT_REF
5027 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5028 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5029 return c_expr_sizeof_expr (expr);
5033 /* Parse an alignof expression. */
5035 static struct c_expr
5036 c_parser_alignof_expression (c_parser *parser)
5038 struct c_expr expr;
5039 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5040 c_parser_consume_token (parser);
5041 skip_evaluation++;
5042 in_alignof++;
5043 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5044 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5046 /* Either __alignof__ ( type-name ) or __alignof__
5047 unary-expression starting with a compound literal. */
5048 struct c_type_name *type_name;
5049 struct c_expr ret;
5050 c_parser_consume_token (parser);
5051 type_name = c_parser_type_name (parser);
5052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5053 if (type_name == NULL)
5055 struct c_expr ret;
5056 skip_evaluation--;
5057 in_alignof--;
5058 ret.value = error_mark_node;
5059 ret.original_code = ERROR_MARK;
5060 ret.original_type = NULL;
5061 return ret;
5063 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5065 expr = c_parser_postfix_expression_after_paren_type (parser,
5066 type_name);
5067 goto alignof_expr;
5069 /* alignof ( type-name ). */
5070 skip_evaluation--;
5071 in_alignof--;
5072 ret.value = c_alignof (groktypename (type_name, NULL, NULL));
5073 ret.original_code = ERROR_MARK;
5074 ret.original_type = NULL;
5075 return ret;
5077 else
5079 struct c_expr ret;
5080 expr = c_parser_unary_expression (parser);
5081 alignof_expr:
5082 skip_evaluation--;
5083 in_alignof--;
5084 ret.value = c_alignof_expr (expr.value);
5085 ret.original_code = ERROR_MARK;
5086 ret.original_type = NULL;
5087 return ret;
5091 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5093 postfix-expression:
5094 primary-expression
5095 postfix-expression [ expression ]
5096 postfix-expression ( argument-expression-list[opt] )
5097 postfix-expression . identifier
5098 postfix-expression -> identifier
5099 postfix-expression ++
5100 postfix-expression --
5101 ( type-name ) { initializer-list }
5102 ( type-name ) { initializer-list , }
5104 argument-expression-list:
5105 argument-expression
5106 argument-expression-list , argument-expression
5108 primary-expression:
5109 identifier
5110 constant
5111 string-literal
5112 ( expression )
5114 GNU extensions:
5116 primary-expression:
5117 __func__
5118 (treated as a keyword in GNU C)
5119 __FUNCTION__
5120 __PRETTY_FUNCTION__
5121 ( compound-statement )
5122 __builtin_va_arg ( assignment-expression , type-name )
5123 __builtin_offsetof ( type-name , offsetof-member-designator )
5124 __builtin_choose_expr ( assignment-expression ,
5125 assignment-expression ,
5126 assignment-expression )
5127 __builtin_types_compatible_p ( type-name , type-name )
5129 offsetof-member-designator:
5130 identifier
5131 offsetof-member-designator . identifier
5132 offsetof-member-designator [ expression ]
5134 Objective-C:
5136 primary-expression:
5137 [ objc-receiver objc-message-args ]
5138 @selector ( objc-selector-arg )
5139 @protocol ( identifier )
5140 @encode ( type-name )
5141 objc-string-literal
5144 static struct c_expr
5145 c_parser_postfix_expression (c_parser *parser)
5147 struct c_expr expr, e1, e2, e3;
5148 struct c_type_name *t1, *t2;
5149 location_t loc;
5150 expr.original_code = ERROR_MARK;
5151 expr.original_type = NULL;
5152 switch (c_parser_peek_token (parser)->type)
5154 case CPP_NUMBER:
5155 expr.value = c_parser_peek_token (parser)->value;
5156 loc = c_parser_peek_token (parser)->location;
5157 c_parser_consume_token (parser);
5158 if (TREE_CODE (expr.value) == FIXED_CST
5159 && !targetm.fixed_point_supported_p ())
5161 error_at (loc, "fixed-point types not supported for this target");
5162 expr.value = error_mark_node;
5164 break;
5165 case CPP_CHAR:
5166 case CPP_CHAR16:
5167 case CPP_CHAR32:
5168 case CPP_WCHAR:
5169 expr.value = c_parser_peek_token (parser)->value;
5170 c_parser_consume_token (parser);
5171 break;
5172 case CPP_STRING:
5173 case CPP_STRING16:
5174 case CPP_STRING32:
5175 case CPP_WSTRING:
5176 expr.value = c_parser_peek_token (parser)->value;
5177 expr.original_code = STRING_CST;
5178 c_parser_consume_token (parser);
5179 break;
5180 case CPP_OBJC_STRING:
5181 gcc_assert (c_dialect_objc ());
5182 expr.value
5183 = objc_build_string_object (c_parser_peek_token (parser)->value);
5184 c_parser_consume_token (parser);
5185 break;
5186 case CPP_NAME:
5187 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5189 c_parser_error (parser, "expected expression");
5190 expr.value = error_mark_node;
5191 break;
5194 tree id = c_parser_peek_token (parser)->value;
5195 location_t loc = c_parser_peek_token (parser)->location;
5196 c_parser_consume_token (parser);
5197 expr.value = build_external_ref (id,
5198 (c_parser_peek_token (parser)->type
5199 == CPP_OPEN_PAREN), loc,
5200 &expr.original_type);
5202 break;
5203 case CPP_OPEN_PAREN:
5204 /* A parenthesized expression, statement expression or compound
5205 literal. */
5206 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5208 /* A statement expression. */
5209 tree stmt;
5210 location_t here = c_parser_peek_token (parser)->location;
5211 c_parser_consume_token (parser);
5212 c_parser_consume_token (parser);
5213 if (cur_stmt_list == NULL)
5215 error_at (here, "braced-group within expression allowed "
5216 "only inside a function");
5217 parser->error = true;
5218 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5219 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5220 expr.value = error_mark_node;
5221 break;
5223 stmt = c_begin_stmt_expr ();
5224 c_parser_compound_statement_nostart (parser);
5225 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5226 "expected %<)%>");
5227 pedwarn (here, OPT_pedantic,
5228 "ISO C forbids braced-groups within expressions");
5229 expr.value = c_finish_stmt_expr (stmt);
5231 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5233 /* A compound literal. ??? Can we actually get here rather
5234 than going directly to
5235 c_parser_postfix_expression_after_paren_type from
5236 elsewhere? */
5237 struct c_type_name *type_name;
5238 c_parser_consume_token (parser);
5239 type_name = c_parser_type_name (parser);
5240 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5241 "expected %<)%>");
5242 if (type_name == NULL)
5244 expr.value = error_mark_node;
5246 else
5247 expr = c_parser_postfix_expression_after_paren_type (parser,
5248 type_name);
5250 else
5252 /* A parenthesized expression. */
5253 c_parser_consume_token (parser);
5254 expr = c_parser_expression (parser);
5255 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5256 TREE_NO_WARNING (expr.value) = 1;
5257 if (expr.original_code != C_MAYBE_CONST_EXPR)
5258 expr.original_code = ERROR_MARK;
5259 /* Don't change EXPR.ORIGINAL_TYPE. */
5260 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5261 "expected %<)%>");
5263 break;
5264 case CPP_KEYWORD:
5265 switch (c_parser_peek_token (parser)->keyword)
5267 case RID_FUNCTION_NAME:
5268 case RID_PRETTY_FUNCTION_NAME:
5269 case RID_C99_FUNCTION_NAME:
5270 expr.value = fname_decl (c_parser_peek_token (parser)->location,
5271 c_parser_peek_token (parser)->keyword,
5272 c_parser_peek_token (parser)->value);
5273 c_parser_consume_token (parser);
5274 break;
5275 case RID_VA_ARG:
5276 c_parser_consume_token (parser);
5277 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5279 expr.value = error_mark_node;
5280 break;
5282 e1 = c_parser_expr_no_commas (parser, NULL);
5283 e1.value = c_fully_fold (e1.value, false, NULL);
5284 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5287 expr.value = error_mark_node;
5288 break;
5290 t1 = c_parser_type_name (parser);
5291 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5292 "expected %<)%>");
5293 if (t1 == NULL)
5295 expr.value = error_mark_node;
5297 else
5299 tree type_expr = NULL_TREE;
5300 expr.value = build_va_arg (e1.value, groktypename (t1,
5301 &type_expr,
5302 NULL));
5303 if (type_expr)
5305 expr.value = build2 (C_MAYBE_CONST_EXPR,
5306 TREE_TYPE (expr.value), type_expr,
5307 expr.value);
5308 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5311 break;
5312 case RID_OFFSETOF:
5313 c_parser_consume_token (parser);
5314 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5316 expr.value = error_mark_node;
5317 break;
5319 t1 = c_parser_type_name (parser);
5320 if (t1 == NULL)
5322 expr.value = error_mark_node;
5323 break;
5325 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5327 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5328 expr.value = error_mark_node;
5329 break;
5332 tree type = groktypename (t1, NULL, NULL);
5333 tree offsetof_ref;
5334 if (type == error_mark_node)
5335 offsetof_ref = error_mark_node;
5336 else
5337 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5338 /* Parse the second argument to __builtin_offsetof. We
5339 must have one identifier, and beyond that we want to
5340 accept sub structure and sub array references. */
5341 if (c_parser_next_token_is (parser, CPP_NAME))
5343 offsetof_ref = build_component_ref
5344 (offsetof_ref, c_parser_peek_token (parser)->value);
5345 c_parser_consume_token (parser);
5346 while (c_parser_next_token_is (parser, CPP_DOT)
5347 || c_parser_next_token_is (parser,
5348 CPP_OPEN_SQUARE)
5349 || c_parser_next_token_is (parser,
5350 CPP_DEREF))
5352 if (c_parser_next_token_is (parser, CPP_DEREF))
5354 loc = c_parser_peek_token (parser)->location;
5355 offsetof_ref = build_array_ref (offsetof_ref,
5356 integer_zero_node,
5357 loc);
5358 goto do_dot;
5360 else if (c_parser_next_token_is (parser, CPP_DOT))
5362 do_dot:
5363 c_parser_consume_token (parser);
5364 if (c_parser_next_token_is_not (parser,
5365 CPP_NAME))
5367 c_parser_error (parser, "expected identifier");
5368 break;
5370 offsetof_ref = build_component_ref
5371 (offsetof_ref,
5372 c_parser_peek_token (parser)->value);
5373 c_parser_consume_token (parser);
5375 else
5377 tree idx;
5378 loc = c_parser_peek_token (parser)->location;
5379 c_parser_consume_token (parser);
5380 idx = c_parser_expression (parser).value;
5381 idx = c_fully_fold (idx, false, NULL);
5382 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5383 "expected %<]%>");
5384 offsetof_ref = build_array_ref (offsetof_ref, idx, loc);
5388 else
5389 c_parser_error (parser, "expected identifier");
5390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5391 "expected %<)%>");
5392 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5394 break;
5395 case RID_CHOOSE_EXPR:
5396 c_parser_consume_token (parser);
5397 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5399 expr.value = error_mark_node;
5400 break;
5402 loc = c_parser_peek_token (parser)->location;
5403 e1 = c_parser_expr_no_commas (parser, NULL);
5404 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5407 expr.value = error_mark_node;
5408 break;
5410 e2 = c_parser_expr_no_commas (parser, NULL);
5411 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5414 expr.value = error_mark_node;
5415 break;
5417 e3 = c_parser_expr_no_commas (parser, NULL);
5418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5419 "expected %<)%>");
5421 tree c;
5423 c = e1.value;
5424 if (TREE_CODE (c) != INTEGER_CST
5425 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
5426 error_at (loc,
5427 "first argument to %<__builtin_choose_expr%> not"
5428 " a constant");
5429 constant_expression_warning (c);
5430 expr = integer_zerop (c) ? e3 : e2;
5432 break;
5433 case RID_TYPES_COMPATIBLE_P:
5434 c_parser_consume_token (parser);
5435 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5437 expr.value = error_mark_node;
5438 break;
5440 t1 = c_parser_type_name (parser);
5441 if (t1 == NULL)
5443 expr.value = error_mark_node;
5444 break;
5446 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5449 expr.value = error_mark_node;
5450 break;
5452 t2 = c_parser_type_name (parser);
5453 if (t2 == NULL)
5455 expr.value = error_mark_node;
5456 break;
5458 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5459 "expected %<)%>");
5461 tree e1, e2;
5463 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5464 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
5466 expr.value = comptypes (e1, e2)
5467 ? build_int_cst (NULL_TREE, 1)
5468 : build_int_cst (NULL_TREE, 0);
5470 break;
5471 case RID_AT_SELECTOR:
5472 gcc_assert (c_dialect_objc ());
5473 c_parser_consume_token (parser);
5474 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5476 expr.value = error_mark_node;
5477 break;
5480 tree sel = c_parser_objc_selector_arg (parser);
5481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5482 "expected %<)%>");
5483 expr.value = objc_build_selector_expr (sel);
5485 break;
5486 case RID_AT_PROTOCOL:
5487 gcc_assert (c_dialect_objc ());
5488 c_parser_consume_token (parser);
5489 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5491 expr.value = error_mark_node;
5492 break;
5494 if (c_parser_next_token_is_not (parser, CPP_NAME))
5496 c_parser_error (parser, "expected identifier");
5497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5498 expr.value = error_mark_node;
5499 break;
5502 tree id = c_parser_peek_token (parser)->value;
5503 c_parser_consume_token (parser);
5504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5505 "expected %<)%>");
5506 expr.value = objc_build_protocol_expr (id);
5508 break;
5509 case RID_AT_ENCODE:
5510 /* Extension to support C-structures in the archiver. */
5511 gcc_assert (c_dialect_objc ());
5512 c_parser_consume_token (parser);
5513 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5515 expr.value = error_mark_node;
5516 break;
5518 t1 = c_parser_type_name (parser);
5519 if (t1 == NULL)
5521 expr.value = error_mark_node;
5522 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5523 break;
5525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5526 "expected %<)%>");
5528 tree type = groktypename (t1, NULL, NULL);
5529 expr.value = objc_build_encode_expr (type);
5531 break;
5532 default:
5533 c_parser_error (parser, "expected expression");
5534 expr.value = error_mark_node;
5535 break;
5537 break;
5538 case CPP_OPEN_SQUARE:
5539 if (c_dialect_objc ())
5541 tree receiver, args;
5542 c_parser_consume_token (parser);
5543 receiver = c_parser_objc_receiver (parser);
5544 args = c_parser_objc_message_args (parser);
5545 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5546 "expected %<]%>");
5547 expr.value = objc_build_message_expr (build_tree_list (receiver,
5548 args));
5549 break;
5551 /* Else fall through to report error. */
5552 default:
5553 c_parser_error (parser, "expected expression");
5554 expr.value = error_mark_node;
5555 break;
5557 return c_parser_postfix_expression_after_primary (parser, expr);
5560 /* Parse a postfix expression after a parenthesized type name: the
5561 brace-enclosed initializer of a compound literal, possibly followed
5562 by some postfix operators. This is separate because it is not
5563 possible to tell until after the type name whether a cast
5564 expression has a cast or a compound literal, or whether the operand
5565 of sizeof is a parenthesized type name or starts with a compound
5566 literal. */
5568 static struct c_expr
5569 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5570 struct c_type_name *type_name)
5572 tree type;
5573 struct c_expr init;
5574 bool non_const;
5575 struct c_expr expr;
5576 location_t start_loc;
5577 tree type_expr = NULL_TREE;
5578 bool type_expr_const = true;
5579 start_init (NULL_TREE, NULL, 0);
5580 type = groktypename (type_name, &type_expr, &type_expr_const);
5581 start_loc = c_parser_peek_token (parser)->location;
5582 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5584 error_at (start_loc, "compound literal has variable size");
5585 type = error_mark_node;
5587 init = c_parser_braced_init (parser, type, false);
5588 finish_init ();
5589 maybe_warn_string_init (type, init);
5591 if (!flag_isoc99)
5592 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
5593 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5594 ? CONSTRUCTOR_NON_CONST (init.value)
5595 : init.original_code == C_MAYBE_CONST_EXPR);
5596 non_const |= !type_expr_const;
5597 expr.value = build_compound_literal (type, init.value, non_const);
5598 expr.original_code = ERROR_MARK;
5599 expr.original_type = NULL;
5600 if (type_expr)
5602 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5604 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
5605 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
5607 else
5609 gcc_assert (!non_const);
5610 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
5611 type_expr, expr.value);
5614 return c_parser_postfix_expression_after_primary (parser, expr);
5617 /* Parse a postfix expression after the initial primary or compound
5618 literal; that is, parse a series of postfix operators. */
5620 static struct c_expr
5621 c_parser_postfix_expression_after_primary (c_parser *parser,
5622 struct c_expr expr)
5624 struct c_expr orig_expr;
5625 tree ident, idx, exprlist;
5626 location_t loc = c_parser_peek_token (parser)->location;
5627 while (true)
5629 switch (c_parser_peek_token (parser)->type)
5631 case CPP_OPEN_SQUARE:
5632 /* Array reference. */
5633 loc = c_parser_peek_token (parser)->location;
5634 c_parser_consume_token (parser);
5635 idx = c_parser_expression (parser).value;
5636 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5637 "expected %<]%>");
5638 expr.value = build_array_ref (expr.value, idx, loc);
5639 expr.original_code = ERROR_MARK;
5640 expr.original_type = NULL;
5641 break;
5642 case CPP_OPEN_PAREN:
5643 /* Function call. */
5644 c_parser_consume_token (parser);
5645 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5646 exprlist = NULL_TREE;
5647 else
5648 exprlist = c_parser_expr_list (parser, true, false);
5649 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5650 "expected %<)%>");
5651 orig_expr = expr;
5652 expr.value = build_function_call (expr.value, exprlist);
5653 expr.original_code = ERROR_MARK;
5654 if (TREE_CODE (expr.value) == INTEGER_CST
5655 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
5656 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
5657 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
5658 expr.original_code = C_MAYBE_CONST_EXPR;
5659 expr.original_type = NULL;
5660 break;
5661 case CPP_DOT:
5662 /* Structure element reference. */
5663 c_parser_consume_token (parser);
5664 expr = default_function_array_conversion (expr);
5665 if (c_parser_next_token_is (parser, CPP_NAME))
5666 ident = c_parser_peek_token (parser)->value;
5667 else
5669 c_parser_error (parser, "expected identifier");
5670 expr.value = error_mark_node;
5671 expr.original_code = ERROR_MARK;
5672 expr.original_type = NULL;
5673 return expr;
5675 c_parser_consume_token (parser);
5676 expr.value = build_component_ref (expr.value, ident);
5677 expr.original_code = ERROR_MARK;
5678 if (TREE_CODE (expr.value) != COMPONENT_REF)
5679 expr.original_type = NULL;
5680 else
5682 /* Remember the original type of a bitfield. */
5683 tree field = TREE_OPERAND (expr.value, 1);
5684 if (TREE_CODE (field) != FIELD_DECL)
5685 expr.original_type = NULL;
5686 else
5687 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5689 break;
5690 case CPP_DEREF:
5691 /* Structure element reference. */
5692 c_parser_consume_token (parser);
5693 expr = default_function_array_conversion (expr);
5694 if (c_parser_next_token_is (parser, CPP_NAME))
5695 ident = c_parser_peek_token (parser)->value;
5696 else
5698 c_parser_error (parser, "expected identifier");
5699 expr.value = error_mark_node;
5700 expr.original_code = ERROR_MARK;
5701 expr.original_type = NULL;
5702 return expr;
5704 c_parser_consume_token (parser);
5705 expr.value = build_component_ref (build_indirect_ref (loc,
5706 expr.value,
5707 "->"),
5708 ident);
5709 expr.original_code = ERROR_MARK;
5710 if (TREE_CODE (expr.value) != COMPONENT_REF)
5711 expr.original_type = NULL;
5712 else
5714 /* Remember the original type of a bitfield. */
5715 tree field = TREE_OPERAND (expr.value, 1);
5716 if (TREE_CODE (field) != FIELD_DECL)
5717 expr.original_type = NULL;
5718 else
5719 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5721 break;
5722 case CPP_PLUS_PLUS:
5723 /* Postincrement. */
5724 c_parser_consume_token (parser);
5725 expr = default_function_array_conversion (expr);
5726 expr.value = build_unary_op (loc,
5727 POSTINCREMENT_EXPR, expr.value, 0);
5728 expr.original_code = ERROR_MARK;
5729 expr.original_type = NULL;
5730 break;
5731 case CPP_MINUS_MINUS:
5732 /* Postdecrement. */
5733 c_parser_consume_token (parser);
5734 expr = default_function_array_conversion (expr);
5735 expr.value = build_unary_op (loc,
5736 POSTDECREMENT_EXPR, expr.value, 0);
5737 expr.original_code = ERROR_MARK;
5738 expr.original_type = NULL;
5739 break;
5740 default:
5741 return expr;
5746 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5748 expression:
5749 assignment-expression
5750 expression , assignment-expression
5753 static struct c_expr
5754 c_parser_expression (c_parser *parser)
5756 struct c_expr expr;
5757 expr = c_parser_expr_no_commas (parser, NULL);
5758 while (c_parser_next_token_is (parser, CPP_COMMA))
5760 struct c_expr next;
5761 c_parser_consume_token (parser);
5762 next = c_parser_expr_no_commas (parser, NULL);
5763 next = default_function_array_conversion (next);
5764 expr.value = build_compound_expr (expr.value, next.value);
5765 expr.original_code = COMPOUND_EXPR;
5766 expr.original_type = NULL;
5768 return expr;
5771 /* Parse an expression and convert functions or arrays to
5772 pointers. */
5774 static struct c_expr
5775 c_parser_expression_conv (c_parser *parser)
5777 struct c_expr expr;
5778 expr = c_parser_expression (parser);
5779 expr = default_function_array_conversion (expr);
5780 return expr;
5783 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5784 functions and arrays to pointers. If FOLD_P, fold the expressions.
5786 nonempty-expr-list:
5787 assignment-expression
5788 nonempty-expr-list , assignment-expression
5791 static tree
5792 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p)
5794 struct c_expr expr;
5795 tree ret, cur;
5796 expr = c_parser_expr_no_commas (parser, NULL);
5797 if (convert_p)
5798 expr = default_function_array_conversion (expr);
5799 if (fold_p)
5800 expr.value = c_fully_fold (expr.value, false, NULL);
5801 ret = cur = build_tree_list (NULL_TREE, expr.value);
5802 while (c_parser_next_token_is (parser, CPP_COMMA))
5804 c_parser_consume_token (parser);
5805 expr = c_parser_expr_no_commas (parser, NULL);
5806 if (convert_p)
5807 expr = default_function_array_conversion (expr);
5808 if (fold_p)
5809 expr.value = c_fully_fold (expr.value, false, NULL);
5810 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5812 return ret;
5816 /* Parse Objective-C-specific constructs. */
5818 /* Parse an objc-class-definition.
5820 objc-class-definition:
5821 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5822 objc-class-instance-variables[opt] objc-methodprotolist @end
5823 @implementation identifier objc-superclass[opt]
5824 objc-class-instance-variables[opt]
5825 @interface identifier ( identifier ) objc-protocol-refs[opt]
5826 objc-methodprotolist @end
5827 @implementation identifier ( identifier )
5829 objc-superclass:
5830 : identifier
5832 "@interface identifier (" must start "@interface identifier (
5833 identifier ) ...": objc-methodprotolist in the first production may
5834 not start with a parenthesized identifier as a declarator of a data
5835 definition with no declaration specifiers if the objc-superclass,
5836 objc-protocol-refs and objc-class-instance-variables are omitted. */
5838 static void
5839 c_parser_objc_class_definition (c_parser *parser)
5841 bool iface_p;
5842 tree id1;
5843 tree superclass;
5844 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5845 iface_p = true;
5846 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5847 iface_p = false;
5848 else
5849 gcc_unreachable ();
5850 c_parser_consume_token (parser);
5851 if (c_parser_next_token_is_not (parser, CPP_NAME))
5853 c_parser_error (parser, "expected identifier");
5854 return;
5856 id1 = c_parser_peek_token (parser)->value;
5857 c_parser_consume_token (parser);
5858 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5860 tree id2;
5861 tree proto = NULL_TREE;
5862 c_parser_consume_token (parser);
5863 if (c_parser_next_token_is_not (parser, CPP_NAME))
5865 c_parser_error (parser, "expected identifier");
5866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5867 return;
5869 id2 = c_parser_peek_token (parser)->value;
5870 c_parser_consume_token (parser);
5871 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5872 if (!iface_p)
5874 objc_start_category_implementation (id1, id2);
5875 return;
5877 if (c_parser_next_token_is (parser, CPP_LESS))
5878 proto = c_parser_objc_protocol_refs (parser);
5879 objc_start_category_interface (id1, id2, proto);
5880 c_parser_objc_methodprotolist (parser);
5881 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5882 objc_finish_interface ();
5883 return;
5885 if (c_parser_next_token_is (parser, CPP_COLON))
5887 c_parser_consume_token (parser);
5888 if (c_parser_next_token_is_not (parser, CPP_NAME))
5890 c_parser_error (parser, "expected identifier");
5891 return;
5893 superclass = c_parser_peek_token (parser)->value;
5894 c_parser_consume_token (parser);
5896 else
5897 superclass = NULL_TREE;
5898 if (iface_p)
5900 tree proto = NULL_TREE;
5901 if (c_parser_next_token_is (parser, CPP_LESS))
5902 proto = c_parser_objc_protocol_refs (parser);
5903 objc_start_class_interface (id1, superclass, proto);
5905 else
5906 objc_start_class_implementation (id1, superclass);
5907 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5908 c_parser_objc_class_instance_variables (parser);
5909 if (iface_p)
5911 objc_continue_interface ();
5912 c_parser_objc_methodprotolist (parser);
5913 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5914 objc_finish_interface ();
5916 else
5918 objc_continue_implementation ();
5919 return;
5923 /* Parse objc-class-instance-variables.
5925 objc-class-instance-variables:
5926 { objc-instance-variable-decl-list[opt] }
5928 objc-instance-variable-decl-list:
5929 objc-visibility-spec
5930 objc-instance-variable-decl ;
5932 objc-instance-variable-decl-list objc-visibility-spec
5933 objc-instance-variable-decl-list objc-instance-variable-decl ;
5934 objc-instance-variable-decl-list ;
5936 objc-visibility-spec:
5937 @private
5938 @protected
5939 @public
5941 objc-instance-variable-decl:
5942 struct-declaration
5945 static void
5946 c_parser_objc_class_instance_variables (c_parser *parser)
5948 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5949 c_parser_consume_token (parser);
5950 while (c_parser_next_token_is_not (parser, CPP_EOF))
5952 tree decls;
5953 /* Parse any stray semicolon. */
5954 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5956 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
5957 "extra semicolon in struct or union specified");
5958 c_parser_consume_token (parser);
5959 continue;
5961 /* Stop if at the end of the instance variables. */
5962 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5964 c_parser_consume_token (parser);
5965 break;
5967 /* Parse any objc-visibility-spec. */
5968 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
5970 c_parser_consume_token (parser);
5971 objc_set_visibility (2);
5972 continue;
5974 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
5976 c_parser_consume_token (parser);
5977 objc_set_visibility (0);
5978 continue;
5980 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
5982 c_parser_consume_token (parser);
5983 objc_set_visibility (1);
5984 continue;
5986 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5988 c_parser_pragma (parser, pragma_external);
5989 continue;
5992 /* Parse some comma-separated declarations. */
5993 decls = c_parser_struct_declaration (parser);
5995 /* Comma-separated instance variables are chained together in
5996 reverse order; add them one by one. */
5997 tree ivar = nreverse (decls);
5998 for (; ivar; ivar = TREE_CHAIN (ivar))
5999 objc_add_instance_variable (copy_node (ivar));
6001 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6005 /* Parse an objc-class-declaration.
6007 objc-class-declaration:
6008 @class identifier-list ;
6011 static void
6012 c_parser_objc_class_declaration (c_parser *parser)
6014 tree list = NULL_TREE;
6015 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
6016 c_parser_consume_token (parser);
6017 /* Any identifiers, including those declared as type names, are OK
6018 here. */
6019 while (true)
6021 tree id;
6022 if (c_parser_next_token_is_not (parser, CPP_NAME))
6024 c_parser_error (parser, "expected identifier");
6025 break;
6027 id = c_parser_peek_token (parser)->value;
6028 list = chainon (list, build_tree_list (NULL_TREE, id));
6029 c_parser_consume_token (parser);
6030 if (c_parser_next_token_is (parser, CPP_COMMA))
6031 c_parser_consume_token (parser);
6032 else
6033 break;
6035 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6036 objc_declare_class (list);
6039 /* Parse an objc-alias-declaration.
6041 objc-alias-declaration:
6042 @compatibility_alias identifier identifier ;
6045 static void
6046 c_parser_objc_alias_declaration (c_parser *parser)
6048 tree id1, id2;
6049 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6050 c_parser_consume_token (parser);
6051 if (c_parser_next_token_is_not (parser, CPP_NAME))
6053 c_parser_error (parser, "expected identifier");
6054 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6055 return;
6057 id1 = c_parser_peek_token (parser)->value;
6058 c_parser_consume_token (parser);
6059 if (c_parser_next_token_is_not (parser, CPP_NAME))
6061 c_parser_error (parser, "expected identifier");
6062 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6063 return;
6065 id2 = c_parser_peek_token (parser)->value;
6066 c_parser_consume_token (parser);
6067 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6068 objc_declare_alias (id1, id2);
6071 /* Parse an objc-protocol-definition.
6073 objc-protocol-definition:
6074 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6075 @protocol identifier-list ;
6077 "@protocol identifier ;" should be resolved as "@protocol
6078 identifier-list ;": objc-methodprotolist may not start with a
6079 semicolon in the first alternative if objc-protocol-refs are
6080 omitted. */
6082 static void
6083 c_parser_objc_protocol_definition (c_parser *parser)
6085 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6086 c_parser_consume_token (parser);
6087 if (c_parser_next_token_is_not (parser, CPP_NAME))
6089 c_parser_error (parser, "expected identifier");
6090 return;
6092 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6093 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6095 tree list = NULL_TREE;
6096 /* Any identifiers, including those declared as type names, are
6097 OK here. */
6098 while (true)
6100 tree id;
6101 if (c_parser_next_token_is_not (parser, CPP_NAME))
6103 c_parser_error (parser, "expected identifier");
6104 break;
6106 id = c_parser_peek_token (parser)->value;
6107 list = chainon (list, build_tree_list (NULL_TREE, id));
6108 c_parser_consume_token (parser);
6109 if (c_parser_next_token_is (parser, CPP_COMMA))
6110 c_parser_consume_token (parser);
6111 else
6112 break;
6114 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6115 objc_declare_protocols (list);
6117 else
6119 tree id = c_parser_peek_token (parser)->value;
6120 tree proto = NULL_TREE;
6121 c_parser_consume_token (parser);
6122 if (c_parser_next_token_is (parser, CPP_LESS))
6123 proto = c_parser_objc_protocol_refs (parser);
6124 parser->objc_pq_context = true;
6125 objc_start_protocol (id, proto);
6126 c_parser_objc_methodprotolist (parser);
6127 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6128 parser->objc_pq_context = false;
6129 objc_finish_interface ();
6133 /* Parse an objc-method-type.
6135 objc-method-type:
6140 static enum tree_code
6141 c_parser_objc_method_type (c_parser *parser)
6143 switch (c_parser_peek_token (parser)->type)
6145 case CPP_PLUS:
6146 c_parser_consume_token (parser);
6147 return PLUS_EXPR;
6148 case CPP_MINUS:
6149 c_parser_consume_token (parser);
6150 return MINUS_EXPR;
6151 default:
6152 gcc_unreachable ();
6156 /* Parse an objc-method-definition.
6158 objc-method-definition:
6159 objc-method-type objc-method-decl ;[opt] compound-statement
6162 static void
6163 c_parser_objc_method_definition (c_parser *parser)
6165 enum tree_code type = c_parser_objc_method_type (parser);
6166 tree decl;
6167 objc_set_method_type (type);
6168 parser->objc_pq_context = true;
6169 decl = c_parser_objc_method_decl (parser);
6170 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6172 c_parser_consume_token (parser);
6173 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6174 "extra semicolon in method definition specified");
6176 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6178 c_parser_error (parser, "expected %<{%>");
6179 return;
6181 parser->objc_pq_context = false;
6182 objc_start_method_definition (decl);
6183 add_stmt (c_parser_compound_statement (parser));
6184 objc_finish_method_definition (current_function_decl);
6187 /* Parse an objc-methodprotolist.
6189 objc-methodprotolist:
6190 empty
6191 objc-methodprotolist objc-methodproto
6192 objc-methodprotolist declaration
6193 objc-methodprotolist ;
6195 The declaration is a data definition, which may be missing
6196 declaration specifiers under the same rules and diagnostics as
6197 other data definitions outside functions, and the stray semicolon
6198 is diagnosed the same way as a stray semicolon outside a
6199 function. */
6201 static void
6202 c_parser_objc_methodprotolist (c_parser *parser)
6204 while (true)
6206 /* The list is terminated by @end. */
6207 switch (c_parser_peek_token (parser)->type)
6209 case CPP_SEMICOLON:
6210 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6211 "ISO C does not allow extra %<;%> outside of a function");
6212 c_parser_consume_token (parser);
6213 break;
6214 case CPP_PLUS:
6215 case CPP_MINUS:
6216 c_parser_objc_methodproto (parser);
6217 break;
6218 case CPP_PRAGMA:
6219 c_parser_pragma (parser, pragma_external);
6220 break;
6221 case CPP_EOF:
6222 return;
6223 default:
6224 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6225 return;
6226 c_parser_declaration_or_fndef (parser, false, true, false, true);
6227 break;
6232 /* Parse an objc-methodproto.
6234 objc-methodproto:
6235 objc-method-type objc-method-decl ;
6238 static void
6239 c_parser_objc_methodproto (c_parser *parser)
6241 enum tree_code type = c_parser_objc_method_type (parser);
6242 tree decl;
6243 objc_set_method_type (type);
6244 /* Remember protocol qualifiers in prototypes. */
6245 parser->objc_pq_context = true;
6246 decl = c_parser_objc_method_decl (parser);
6247 /* Forget protocol qualifiers here. */
6248 parser->objc_pq_context = false;
6249 objc_add_method_declaration (decl);
6250 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6253 /* Parse an objc-method-decl.
6255 objc-method-decl:
6256 ( objc-type-name ) objc-selector
6257 objc-selector
6258 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6259 objc-keyword-selector objc-optparmlist
6261 objc-keyword-selector:
6262 objc-keyword-decl
6263 objc-keyword-selector objc-keyword-decl
6265 objc-keyword-decl:
6266 objc-selector : ( objc-type-name ) identifier
6267 objc-selector : identifier
6268 : ( objc-type-name ) identifier
6269 : identifier
6271 objc-optparmlist:
6272 objc-optparms objc-optellipsis
6274 objc-optparms:
6275 empty
6276 objc-opt-parms , parameter-declaration
6278 objc-optellipsis:
6279 empty
6280 , ...
6283 static tree
6284 c_parser_objc_method_decl (c_parser *parser)
6286 tree type = NULL_TREE;
6287 tree sel;
6288 tree parms = NULL_TREE;
6289 bool ellipsis = false;
6291 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6293 c_parser_consume_token (parser);
6294 type = c_parser_objc_type_name (parser);
6295 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6297 sel = c_parser_objc_selector (parser);
6298 /* If there is no selector, or a colon follows, we have an
6299 objc-keyword-selector. If there is a selector, and a colon does
6300 not follow, that selector ends the objc-method-decl. */
6301 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6303 tree tsel = sel;
6304 tree list = NULL_TREE;
6305 while (true)
6307 tree atype = NULL_TREE, id, keyworddecl;
6308 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6309 break;
6310 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6312 c_parser_consume_token (parser);
6313 atype = c_parser_objc_type_name (parser);
6314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6315 "expected %<)%>");
6317 if (c_parser_next_token_is_not (parser, CPP_NAME))
6319 c_parser_error (parser, "expected identifier");
6320 return error_mark_node;
6322 id = c_parser_peek_token (parser)->value;
6323 c_parser_consume_token (parser);
6324 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6325 list = chainon (list, keyworddecl);
6326 tsel = c_parser_objc_selector (parser);
6327 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6328 break;
6330 /* Parse the optional parameter list. Optional Objective-C
6331 method parameters follow the C syntax, and may include '...'
6332 to denote a variable number of arguments. */
6333 parms = make_node (TREE_LIST);
6334 while (c_parser_next_token_is (parser, CPP_COMMA))
6336 struct c_parm *parm;
6337 c_parser_consume_token (parser);
6338 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6340 ellipsis = true;
6341 c_parser_consume_token (parser);
6342 break;
6344 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6345 if (parm == NULL)
6346 break;
6347 parms = chainon (parms,
6348 build_tree_list (NULL_TREE, grokparm (parm)));
6350 sel = list;
6352 return objc_build_method_signature (type, sel, parms, ellipsis);
6355 /* Parse an objc-type-name.
6357 objc-type-name:
6358 objc-type-qualifiers[opt] type-name
6359 objc-type-qualifiers[opt]
6361 objc-type-qualifiers:
6362 objc-type-qualifier
6363 objc-type-qualifiers objc-type-qualifier
6365 objc-type-qualifier: one of
6366 in out inout bycopy byref oneway
6369 static tree
6370 c_parser_objc_type_name (c_parser *parser)
6372 tree quals = NULL_TREE;
6373 struct c_type_name *type_name = NULL;
6374 tree type = NULL_TREE;
6375 while (true)
6377 c_token *token = c_parser_peek_token (parser);
6378 if (token->type == CPP_KEYWORD
6379 && (token->keyword == RID_IN
6380 || token->keyword == RID_OUT
6381 || token->keyword == RID_INOUT
6382 || token->keyword == RID_BYCOPY
6383 || token->keyword == RID_BYREF
6384 || token->keyword == RID_ONEWAY))
6386 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6387 c_parser_consume_token (parser);
6389 else
6390 break;
6392 if (c_parser_next_token_starts_typename (parser))
6393 type_name = c_parser_type_name (parser);
6394 if (type_name)
6395 type = groktypename (type_name, NULL, NULL);
6396 return build_tree_list (quals, type);
6399 /* Parse objc-protocol-refs.
6401 objc-protocol-refs:
6402 < identifier-list >
6405 static tree
6406 c_parser_objc_protocol_refs (c_parser *parser)
6408 tree list = NULL_TREE;
6409 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6410 c_parser_consume_token (parser);
6411 /* Any identifiers, including those declared as type names, are OK
6412 here. */
6413 while (true)
6415 tree id;
6416 if (c_parser_next_token_is_not (parser, CPP_NAME))
6418 c_parser_error (parser, "expected identifier");
6419 break;
6421 id = c_parser_peek_token (parser)->value;
6422 list = chainon (list, build_tree_list (NULL_TREE, id));
6423 c_parser_consume_token (parser);
6424 if (c_parser_next_token_is (parser, CPP_COMMA))
6425 c_parser_consume_token (parser);
6426 else
6427 break;
6429 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6430 return list;
6433 /* Parse an objc-try-catch-statement.
6435 objc-try-catch-statement:
6436 @try compound-statement objc-catch-list[opt]
6437 @try compound-statement objc-catch-list[opt] @finally compound-statement
6439 objc-catch-list:
6440 @catch ( parameter-declaration ) compound-statement
6441 objc-catch-list @catch ( parameter-declaration ) compound-statement
6444 static void
6445 c_parser_objc_try_catch_statement (c_parser *parser)
6447 location_t loc;
6448 tree stmt;
6449 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6450 c_parser_consume_token (parser);
6451 loc = c_parser_peek_token (parser)->location;
6452 stmt = c_parser_compound_statement (parser);
6453 objc_begin_try_stmt (loc, stmt);
6454 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6456 struct c_parm *parm;
6457 c_parser_consume_token (parser);
6458 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6459 break;
6460 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6461 if (parm == NULL)
6463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6464 break;
6466 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6467 objc_begin_catch_clause (grokparm (parm));
6468 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6469 c_parser_compound_statement_nostart (parser);
6470 objc_finish_catch_clause ();
6472 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6474 location_t finloc;
6475 tree finstmt;
6476 c_parser_consume_token (parser);
6477 finloc = c_parser_peek_token (parser)->location;
6478 finstmt = c_parser_compound_statement (parser);
6479 objc_build_finally_clause (finloc, finstmt);
6481 objc_finish_try_stmt ();
6484 /* Parse an objc-synchronized-statement.
6486 objc-synchronized-statement:
6487 @synchronized ( expression ) compound-statement
6490 static void
6491 c_parser_objc_synchronized_statement (c_parser *parser)
6493 location_t loc;
6494 tree expr, stmt;
6495 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6496 c_parser_consume_token (parser);
6497 loc = c_parser_peek_token (parser)->location;
6498 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6500 expr = c_parser_expression (parser).value;
6501 expr = c_fully_fold (expr, false, NULL);
6502 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6504 else
6505 expr = error_mark_node;
6506 stmt = c_parser_compound_statement (parser);
6507 objc_build_synchronized (loc, expr, stmt);
6510 /* Parse an objc-selector; return NULL_TREE without an error if the
6511 next token is not an objc-selector.
6513 objc-selector:
6514 identifier
6515 one of
6516 enum struct union if else while do for switch case default
6517 break continue return goto asm sizeof typeof __alignof
6518 unsigned long const short volatile signed restrict _Complex
6519 in out inout bycopy byref oneway int char float double void _Bool
6521 ??? Why this selection of keywords but not, for example, storage
6522 class specifiers? */
6524 static tree
6525 c_parser_objc_selector (c_parser *parser)
6527 c_token *token = c_parser_peek_token (parser);
6528 tree value = token->value;
6529 if (token->type == CPP_NAME)
6531 c_parser_consume_token (parser);
6532 return value;
6534 if (token->type != CPP_KEYWORD)
6535 return NULL_TREE;
6536 switch (token->keyword)
6538 case RID_ENUM:
6539 case RID_STRUCT:
6540 case RID_UNION:
6541 case RID_IF:
6542 case RID_ELSE:
6543 case RID_WHILE:
6544 case RID_DO:
6545 case RID_FOR:
6546 case RID_SWITCH:
6547 case RID_CASE:
6548 case RID_DEFAULT:
6549 case RID_BREAK:
6550 case RID_CONTINUE:
6551 case RID_RETURN:
6552 case RID_GOTO:
6553 case RID_ASM:
6554 case RID_SIZEOF:
6555 case RID_TYPEOF:
6556 case RID_ALIGNOF:
6557 case RID_UNSIGNED:
6558 case RID_LONG:
6559 case RID_CONST:
6560 case RID_SHORT:
6561 case RID_VOLATILE:
6562 case RID_SIGNED:
6563 case RID_RESTRICT:
6564 case RID_COMPLEX:
6565 case RID_IN:
6566 case RID_OUT:
6567 case RID_INOUT:
6568 case RID_BYCOPY:
6569 case RID_BYREF:
6570 case RID_ONEWAY:
6571 case RID_INT:
6572 case RID_CHAR:
6573 case RID_FLOAT:
6574 case RID_DOUBLE:
6575 case RID_VOID:
6576 case RID_BOOL:
6577 c_parser_consume_token (parser);
6578 return value;
6579 default:
6580 return NULL_TREE;
6584 /* Parse an objc-selector-arg.
6586 objc-selector-arg:
6587 objc-selector
6588 objc-keywordname-list
6590 objc-keywordname-list:
6591 objc-keywordname
6592 objc-keywordname-list objc-keywordname
6594 objc-keywordname:
6595 objc-selector :
6599 static tree
6600 c_parser_objc_selector_arg (c_parser *parser)
6602 tree sel = c_parser_objc_selector (parser);
6603 tree list = NULL_TREE;
6604 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6605 return sel;
6606 while (true)
6608 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6609 return list;
6610 list = chainon (list, build_tree_list (sel, NULL_TREE));
6611 sel = c_parser_objc_selector (parser);
6612 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6613 break;
6615 return list;
6618 /* Parse an objc-receiver.
6620 objc-receiver:
6621 expression
6622 class-name
6623 type-name
6626 static tree
6627 c_parser_objc_receiver (c_parser *parser)
6629 if (c_parser_peek_token (parser)->type == CPP_NAME
6630 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6631 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6633 tree id = c_parser_peek_token (parser)->value;
6634 c_parser_consume_token (parser);
6635 return objc_get_class_reference (id);
6637 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
6640 /* Parse objc-message-args.
6642 objc-message-args:
6643 objc-selector
6644 objc-keywordarg-list
6646 objc-keywordarg-list:
6647 objc-keywordarg
6648 objc-keywordarg-list objc-keywordarg
6650 objc-keywordarg:
6651 objc-selector : objc-keywordexpr
6652 : objc-keywordexpr
6655 static tree
6656 c_parser_objc_message_args (c_parser *parser)
6658 tree sel = c_parser_objc_selector (parser);
6659 tree list = NULL_TREE;
6660 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6661 return sel;
6662 while (true)
6664 tree keywordexpr;
6665 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6666 return list;
6667 keywordexpr = c_parser_objc_keywordexpr (parser);
6668 list = chainon (list, build_tree_list (sel, keywordexpr));
6669 sel = c_parser_objc_selector (parser);
6670 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6671 break;
6673 return list;
6676 /* Parse an objc-keywordexpr.
6678 objc-keywordexpr:
6679 nonempty-expr-list
6682 static tree
6683 c_parser_objc_keywordexpr (c_parser *parser)
6685 tree list = c_parser_expr_list (parser, true, true);
6686 if (TREE_CHAIN (list) == NULL_TREE)
6688 /* Just return the expression, remove a level of
6689 indirection. */
6690 return TREE_VALUE (list);
6692 else
6694 /* We have a comma expression, we will collapse later. */
6695 return list;
6700 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6701 should be considered, statements. ALLOW_STMT is true if we're within
6702 the context of a function and such pragmas are to be allowed. Returns
6703 true if we actually parsed such a pragma. */
6705 static bool
6706 c_parser_pragma (c_parser *parser, enum pragma_context context)
6708 unsigned int id;
6710 id = c_parser_peek_token (parser)->pragma_kind;
6711 gcc_assert (id != PRAGMA_NONE);
6713 switch (id)
6715 case PRAGMA_OMP_BARRIER:
6716 if (context != pragma_compound)
6718 if (context == pragma_stmt)
6719 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6720 "used in compound statements");
6721 goto bad_stmt;
6723 c_parser_omp_barrier (parser);
6724 return false;
6726 case PRAGMA_OMP_FLUSH:
6727 if (context != pragma_compound)
6729 if (context == pragma_stmt)
6730 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6731 "used in compound statements");
6732 goto bad_stmt;
6734 c_parser_omp_flush (parser);
6735 return false;
6737 case PRAGMA_OMP_TASKWAIT:
6738 if (context != pragma_compound)
6740 if (context == pragma_stmt)
6741 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
6742 "used in compound statements");
6743 goto bad_stmt;
6745 c_parser_omp_taskwait (parser);
6746 return false;
6748 case PRAGMA_OMP_THREADPRIVATE:
6749 c_parser_omp_threadprivate (parser);
6750 return false;
6752 case PRAGMA_OMP_SECTION:
6753 error_at (c_parser_peek_token (parser)->location,
6754 "%<#pragma omp section%> may only be used in "
6755 "%<#pragma omp sections%> construct");
6756 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6757 return false;
6759 case PRAGMA_GCC_PCH_PREPROCESS:
6760 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6761 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6762 return false;
6764 default:
6765 if (id < PRAGMA_FIRST_EXTERNAL)
6767 if (context == pragma_external)
6769 bad_stmt:
6770 c_parser_error (parser, "expected declaration specifiers");
6771 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6772 return false;
6774 c_parser_omp_construct (parser);
6775 return true;
6777 break;
6780 c_parser_consume_pragma (parser);
6781 c_invoke_pragma_handler (id);
6783 /* Skip to EOL, but suppress any error message. Those will have been
6784 generated by the handler routine through calling error, as opposed
6785 to calling c_parser_error. */
6786 parser->error = true;
6787 c_parser_skip_to_pragma_eol (parser);
6789 return false;
6792 /* The interface the pragma parsers have to the lexer. */
6794 enum cpp_ttype
6795 pragma_lex (tree *value)
6797 c_token *tok = c_parser_peek_token (the_parser);
6798 enum cpp_ttype ret = tok->type;
6800 *value = tok->value;
6801 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6802 ret = CPP_EOF;
6803 else
6805 if (ret == CPP_KEYWORD)
6806 ret = CPP_NAME;
6807 c_parser_consume_token (the_parser);
6810 return ret;
6813 static void
6814 c_parser_pragma_pch_preprocess (c_parser *parser)
6816 tree name = NULL;
6818 c_parser_consume_pragma (parser);
6819 if (c_parser_next_token_is (parser, CPP_STRING))
6821 name = c_parser_peek_token (parser)->value;
6822 c_parser_consume_token (parser);
6824 else
6825 c_parser_error (parser, "expected string literal");
6826 c_parser_skip_to_pragma_eol (parser);
6828 if (name)
6829 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6832 /* OpenMP 2.5 parsing routines. */
6834 /* Returns name of the next clause.
6835 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6836 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6837 returned and the token is consumed. */
6839 static pragma_omp_clause
6840 c_parser_omp_clause_name (c_parser *parser)
6842 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6844 if (c_parser_next_token_is_keyword (parser, RID_IF))
6845 result = PRAGMA_OMP_CLAUSE_IF;
6846 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6847 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6848 else if (c_parser_next_token_is (parser, CPP_NAME))
6850 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6852 switch (p[0])
6854 case 'c':
6855 if (!strcmp ("collapse", p))
6856 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
6857 else if (!strcmp ("copyin", p))
6858 result = PRAGMA_OMP_CLAUSE_COPYIN;
6859 else if (!strcmp ("copyprivate", p))
6860 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6861 break;
6862 case 'f':
6863 if (!strcmp ("firstprivate", p))
6864 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6865 break;
6866 case 'l':
6867 if (!strcmp ("lastprivate", p))
6868 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6869 break;
6870 case 'n':
6871 if (!strcmp ("nowait", p))
6872 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6873 else if (!strcmp ("num_threads", p))
6874 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6875 break;
6876 case 'o':
6877 if (!strcmp ("ordered", p))
6878 result = PRAGMA_OMP_CLAUSE_ORDERED;
6879 break;
6880 case 'p':
6881 if (!strcmp ("private", p))
6882 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6883 break;
6884 case 'r':
6885 if (!strcmp ("reduction", p))
6886 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6887 break;
6888 case 's':
6889 if (!strcmp ("schedule", p))
6890 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6891 else if (!strcmp ("shared", p))
6892 result = PRAGMA_OMP_CLAUSE_SHARED;
6893 break;
6894 case 'u':
6895 if (!strcmp ("untied", p))
6896 result = PRAGMA_OMP_CLAUSE_UNTIED;
6897 break;
6901 if (result != PRAGMA_OMP_CLAUSE_NONE)
6902 c_parser_consume_token (parser);
6904 return result;
6907 /* Validate that a clause of the given type does not already exist. */
6909 static void
6910 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
6911 const char *name)
6913 tree c;
6915 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6916 if (OMP_CLAUSE_CODE (c) == code)
6918 error ("too many %qs clauses", name);
6919 break;
6923 /* OpenMP 2.5:
6924 variable-list:
6925 identifier
6926 variable-list , identifier
6928 If KIND is nonzero, create the appropriate node and install the decl
6929 in OMP_CLAUSE_DECL and add the node to the head of the list.
6931 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6932 return the list created. */
6934 static tree
6935 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6936 tree list)
6938 if (c_parser_next_token_is_not (parser, CPP_NAME)
6939 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6940 c_parser_error (parser, "expected identifier");
6942 while (c_parser_next_token_is (parser, CPP_NAME)
6943 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6945 tree t = lookup_name (c_parser_peek_token (parser)->value);
6947 if (t == NULL_TREE)
6948 undeclared_variable (c_parser_peek_token (parser)->value,
6949 c_parser_peek_token (parser)->location);
6950 else if (t == error_mark_node)
6952 else if (kind != 0)
6954 tree u = build_omp_clause (kind);
6955 OMP_CLAUSE_DECL (u) = t;
6956 OMP_CLAUSE_CHAIN (u) = list;
6957 list = u;
6959 else
6960 list = tree_cons (t, NULL_TREE, list);
6962 c_parser_consume_token (parser);
6964 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6965 break;
6967 c_parser_consume_token (parser);
6970 return list;
6973 /* Similarly, but expect leading and trailing parenthesis. This is a very
6974 common case for omp clauses. */
6976 static tree
6977 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
6978 tree list)
6980 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6982 list = c_parser_omp_variable_list (parser, kind, list);
6983 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6985 return list;
6988 /* OpenMP 3.0:
6989 collapse ( constant-expression ) */
6991 static tree
6992 c_parser_omp_clause_collapse (c_parser *parser, tree list)
6994 tree c, num = error_mark_node;
6995 HOST_WIDE_INT n;
6996 location_t loc;
6998 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7000 loc = c_parser_peek_token (parser)->location;
7001 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7003 num = c_parser_expr_no_commas (parser, NULL).value;
7004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7006 if (num == error_mark_node)
7007 return list;
7008 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7009 || !host_integerp (num, 0)
7010 || (n = tree_low_cst (num, 0)) <= 0
7011 || (int) n != n)
7013 error_at (loc,
7014 "collapse argument needs positive constant integer expression");
7015 return list;
7017 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
7018 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7019 OMP_CLAUSE_CHAIN (c) = list;
7020 return c;
7023 /* OpenMP 2.5:
7024 copyin ( variable-list ) */
7026 static tree
7027 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7029 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7032 /* OpenMP 2.5:
7033 copyprivate ( variable-list ) */
7035 static tree
7036 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7038 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7041 /* OpenMP 2.5:
7042 default ( shared | none ) */
7044 static tree
7045 c_parser_omp_clause_default (c_parser *parser, tree list)
7047 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7048 tree c;
7050 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7051 return list;
7052 if (c_parser_next_token_is (parser, CPP_NAME))
7054 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7056 switch (p[0])
7058 case 'n':
7059 if (strcmp ("none", p) != 0)
7060 goto invalid_kind;
7061 kind = OMP_CLAUSE_DEFAULT_NONE;
7062 break;
7064 case 's':
7065 if (strcmp ("shared", p) != 0)
7066 goto invalid_kind;
7067 kind = OMP_CLAUSE_DEFAULT_SHARED;
7068 break;
7070 default:
7071 goto invalid_kind;
7074 c_parser_consume_token (parser);
7076 else
7078 invalid_kind:
7079 c_parser_error (parser, "expected %<none%> or %<shared%>");
7081 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7083 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7084 return list;
7086 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7087 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
7088 OMP_CLAUSE_CHAIN (c) = list;
7089 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7091 return c;
7094 /* OpenMP 2.5:
7095 firstprivate ( variable-list ) */
7097 static tree
7098 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7100 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7103 /* OpenMP 2.5:
7104 if ( expression ) */
7106 static tree
7107 c_parser_omp_clause_if (c_parser *parser, tree list)
7109 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7111 tree t = c_parser_paren_condition (parser);
7112 tree c;
7114 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7116 c = build_omp_clause (OMP_CLAUSE_IF);
7117 OMP_CLAUSE_IF_EXPR (c) = t;
7118 OMP_CLAUSE_CHAIN (c) = list;
7119 list = c;
7121 else
7122 c_parser_error (parser, "expected %<(%>");
7124 return list;
7127 /* OpenMP 2.5:
7128 lastprivate ( variable-list ) */
7130 static tree
7131 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7133 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7136 /* OpenMP 2.5:
7137 nowait */
7139 static tree
7140 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7142 tree c;
7144 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7146 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
7147 OMP_CLAUSE_CHAIN (c) = list;
7148 return c;
7151 /* OpenMP 2.5:
7152 num_threads ( expression ) */
7154 static tree
7155 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7157 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7159 location_t expr_loc = c_parser_peek_token (parser)->location;
7160 tree c, t = c_parser_expression (parser).value;
7161 t = c_fully_fold (t, false, NULL);
7163 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7165 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7167 c_parser_error (parser, "expected integer expression");
7168 return list;
7171 /* Attempt to statically determine when the number isn't positive. */
7172 c = fold_build2 (LE_EXPR, boolean_type_node, t,
7173 build_int_cst (TREE_TYPE (t), 0));
7174 if (c == boolean_true_node)
7176 warning_at (expr_loc, 0,
7177 "%<num_threads%> value must be positive");
7178 t = integer_one_node;
7181 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7183 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
7184 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7185 OMP_CLAUSE_CHAIN (c) = list;
7186 list = c;
7189 return list;
7192 /* OpenMP 2.5:
7193 ordered */
7195 static tree
7196 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7198 tree c;
7200 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7202 c = build_omp_clause (OMP_CLAUSE_ORDERED);
7203 OMP_CLAUSE_CHAIN (c) = list;
7204 return c;
7207 /* OpenMP 2.5:
7208 private ( variable-list ) */
7210 static tree
7211 c_parser_omp_clause_private (c_parser *parser, tree list)
7213 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7216 /* OpenMP 2.5:
7217 reduction ( reduction-operator : variable-list )
7219 reduction-operator:
7220 One of: + * - & ^ | && || */
7222 static tree
7223 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7225 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7227 enum tree_code code;
7229 switch (c_parser_peek_token (parser)->type)
7231 case CPP_PLUS:
7232 code = PLUS_EXPR;
7233 break;
7234 case CPP_MULT:
7235 code = MULT_EXPR;
7236 break;
7237 case CPP_MINUS:
7238 code = MINUS_EXPR;
7239 break;
7240 case CPP_AND:
7241 code = BIT_AND_EXPR;
7242 break;
7243 case CPP_XOR:
7244 code = BIT_XOR_EXPR;
7245 break;
7246 case CPP_OR:
7247 code = BIT_IOR_EXPR;
7248 break;
7249 case CPP_AND_AND:
7250 code = TRUTH_ANDIF_EXPR;
7251 break;
7252 case CPP_OR_OR:
7253 code = TRUTH_ORIF_EXPR;
7254 break;
7255 default:
7256 c_parser_error (parser,
7257 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7258 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7259 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7260 return list;
7262 c_parser_consume_token (parser);
7263 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7265 tree nl, c;
7267 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
7268 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7269 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7271 list = nl;
7273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7275 return list;
7278 /* OpenMP 2.5:
7279 schedule ( schedule-kind )
7280 schedule ( schedule-kind , expression )
7282 schedule-kind:
7283 static | dynamic | guided | runtime | auto
7286 static tree
7287 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7289 tree c, t;
7291 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7292 return list;
7294 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7296 if (c_parser_next_token_is (parser, CPP_NAME))
7298 tree kind = c_parser_peek_token (parser)->value;
7299 const char *p = IDENTIFIER_POINTER (kind);
7301 switch (p[0])
7303 case 'd':
7304 if (strcmp ("dynamic", p) != 0)
7305 goto invalid_kind;
7306 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7307 break;
7309 case 'g':
7310 if (strcmp ("guided", p) != 0)
7311 goto invalid_kind;
7312 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7313 break;
7315 case 'r':
7316 if (strcmp ("runtime", p) != 0)
7317 goto invalid_kind;
7318 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7319 break;
7321 default:
7322 goto invalid_kind;
7325 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7326 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7327 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7328 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7329 else
7330 goto invalid_kind;
7332 c_parser_consume_token (parser);
7333 if (c_parser_next_token_is (parser, CPP_COMMA))
7335 location_t here;
7336 c_parser_consume_token (parser);
7338 here = c_parser_peek_token (parser)->location;
7339 t = c_parser_expr_no_commas (parser, NULL).value;
7340 t = c_fully_fold (t, false, NULL);
7342 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7343 error_at (here, "schedule %<runtime%> does not take "
7344 "a %<chunk_size%> parameter");
7345 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7346 error_at (here,
7347 "schedule %<auto%> does not take "
7348 "a %<chunk_size%> parameter");
7349 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7350 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7351 else
7352 c_parser_error (parser, "expected integer expression");
7354 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7356 else
7357 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7358 "expected %<,%> or %<)%>");
7360 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7361 OMP_CLAUSE_CHAIN (c) = list;
7362 return c;
7364 invalid_kind:
7365 c_parser_error (parser, "invalid schedule kind");
7366 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7367 return list;
7370 /* OpenMP 2.5:
7371 shared ( variable-list ) */
7373 static tree
7374 c_parser_omp_clause_shared (c_parser *parser, tree list)
7376 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7379 /* OpenMP 3.0:
7380 untied */
7382 static tree
7383 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7385 tree c;
7387 /* FIXME: Should we allow duplicates? */
7388 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7390 c = build_omp_clause (OMP_CLAUSE_UNTIED);
7391 OMP_CLAUSE_CHAIN (c) = list;
7392 return c;
7395 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7396 is a bitmask in MASK. Return the list of clauses found; the result
7397 of clause default goes in *pdefault. */
7399 static tree
7400 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7401 const char *where)
7403 tree clauses = NULL;
7404 bool first = true;
7406 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7408 location_t here;
7409 pragma_omp_clause c_kind;
7410 const char *c_name;
7411 tree prev = clauses;
7413 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7414 c_parser_consume_token (parser);
7416 first = false;
7417 here = c_parser_peek_token (parser)->location;
7418 c_kind = c_parser_omp_clause_name (parser);
7420 switch (c_kind)
7422 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7423 clauses = c_parser_omp_clause_collapse (parser, clauses);
7424 c_name = "collapse";
7425 break;
7426 case PRAGMA_OMP_CLAUSE_COPYIN:
7427 clauses = c_parser_omp_clause_copyin (parser, clauses);
7428 c_name = "copyin";
7429 break;
7430 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7431 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7432 c_name = "copyprivate";
7433 break;
7434 case PRAGMA_OMP_CLAUSE_DEFAULT:
7435 clauses = c_parser_omp_clause_default (parser, clauses);
7436 c_name = "default";
7437 break;
7438 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7439 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7440 c_name = "firstprivate";
7441 break;
7442 case PRAGMA_OMP_CLAUSE_IF:
7443 clauses = c_parser_omp_clause_if (parser, clauses);
7444 c_name = "if";
7445 break;
7446 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7447 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7448 c_name = "lastprivate";
7449 break;
7450 case PRAGMA_OMP_CLAUSE_NOWAIT:
7451 clauses = c_parser_omp_clause_nowait (parser, clauses);
7452 c_name = "nowait";
7453 break;
7454 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7455 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7456 c_name = "num_threads";
7457 break;
7458 case PRAGMA_OMP_CLAUSE_ORDERED:
7459 clauses = c_parser_omp_clause_ordered (parser, clauses);
7460 c_name = "ordered";
7461 break;
7462 case PRAGMA_OMP_CLAUSE_PRIVATE:
7463 clauses = c_parser_omp_clause_private (parser, clauses);
7464 c_name = "private";
7465 break;
7466 case PRAGMA_OMP_CLAUSE_REDUCTION:
7467 clauses = c_parser_omp_clause_reduction (parser, clauses);
7468 c_name = "reduction";
7469 break;
7470 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7471 clauses = c_parser_omp_clause_schedule (parser, clauses);
7472 c_name = "schedule";
7473 break;
7474 case PRAGMA_OMP_CLAUSE_SHARED:
7475 clauses = c_parser_omp_clause_shared (parser, clauses);
7476 c_name = "shared";
7477 break;
7478 case PRAGMA_OMP_CLAUSE_UNTIED:
7479 clauses = c_parser_omp_clause_untied (parser, clauses);
7480 c_name = "untied";
7481 break;
7482 default:
7483 c_parser_error (parser, "expected %<#pragma omp%> clause");
7484 goto saw_error;
7487 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7489 /* Remove the invalid clause(s) from the list to avoid
7490 confusing the rest of the compiler. */
7491 clauses = prev;
7492 error_at (here, "%qs is not valid for %qs", c_name, where);
7496 saw_error:
7497 c_parser_skip_to_pragma_eol (parser);
7499 return c_finish_omp_clauses (clauses);
7502 /* OpenMP 2.5:
7503 structured-block:
7504 statement
7506 In practice, we're also interested in adding the statement to an
7507 outer node. So it is convenient if we work around the fact that
7508 c_parser_statement calls add_stmt. */
7510 static tree
7511 c_parser_omp_structured_block (c_parser *parser)
7513 tree stmt = push_stmt_list ();
7514 c_parser_statement (parser);
7515 return pop_stmt_list (stmt);
7518 /* OpenMP 2.5:
7519 # pragma omp atomic new-line
7520 expression-stmt
7522 expression-stmt:
7523 x binop= expr | x++ | ++x | x-- | --x
7524 binop:
7525 +, *, -, /, &, ^, |, <<, >>
7527 where x is an lvalue expression with scalar type. */
7529 static void
7530 c_parser_omp_atomic (c_parser *parser)
7532 tree lhs, rhs;
7533 tree stmt;
7534 enum tree_code code;
7535 struct c_expr rhs_expr;
7537 c_parser_skip_to_pragma_eol (parser);
7539 lhs = c_parser_unary_expression (parser).value;
7540 lhs = c_fully_fold (lhs, false, NULL);
7541 switch (TREE_CODE (lhs))
7543 case ERROR_MARK:
7544 saw_error:
7545 c_parser_skip_to_end_of_block_or_statement (parser);
7546 return;
7548 case PREINCREMENT_EXPR:
7549 case POSTINCREMENT_EXPR:
7550 lhs = TREE_OPERAND (lhs, 0);
7551 code = PLUS_EXPR;
7552 rhs = integer_one_node;
7553 break;
7555 case PREDECREMENT_EXPR:
7556 case POSTDECREMENT_EXPR:
7557 lhs = TREE_OPERAND (lhs, 0);
7558 code = MINUS_EXPR;
7559 rhs = integer_one_node;
7560 break;
7562 default:
7563 switch (c_parser_peek_token (parser)->type)
7565 case CPP_MULT_EQ:
7566 code = MULT_EXPR;
7567 break;
7568 case CPP_DIV_EQ:
7569 code = TRUNC_DIV_EXPR;
7570 break;
7571 case CPP_PLUS_EQ:
7572 code = PLUS_EXPR;
7573 break;
7574 case CPP_MINUS_EQ:
7575 code = MINUS_EXPR;
7576 break;
7577 case CPP_LSHIFT_EQ:
7578 code = LSHIFT_EXPR;
7579 break;
7580 case CPP_RSHIFT_EQ:
7581 code = RSHIFT_EXPR;
7582 break;
7583 case CPP_AND_EQ:
7584 code = BIT_AND_EXPR;
7585 break;
7586 case CPP_OR_EQ:
7587 code = BIT_IOR_EXPR;
7588 break;
7589 case CPP_XOR_EQ:
7590 code = BIT_XOR_EXPR;
7591 break;
7592 default:
7593 c_parser_error (parser,
7594 "invalid operator for %<#pragma omp atomic%>");
7595 goto saw_error;
7598 c_parser_consume_token (parser);
7599 rhs_expr = c_parser_expression (parser);
7600 rhs_expr = default_function_array_conversion (rhs_expr);
7601 rhs = rhs_expr.value;
7602 rhs = c_fully_fold (rhs, false, NULL);
7603 break;
7605 stmt = c_finish_omp_atomic (code, lhs, rhs);
7606 if (stmt != error_mark_node)
7607 add_stmt (stmt);
7608 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7612 /* OpenMP 2.5:
7613 # pragma omp barrier new-line
7616 static void
7617 c_parser_omp_barrier (c_parser *parser)
7619 c_parser_consume_pragma (parser);
7620 c_parser_skip_to_pragma_eol (parser);
7622 c_finish_omp_barrier ();
7625 /* OpenMP 2.5:
7626 # pragma omp critical [(name)] new-line
7627 structured-block
7630 static tree
7631 c_parser_omp_critical (c_parser *parser)
7633 tree stmt, name = NULL;
7635 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7637 c_parser_consume_token (parser);
7638 if (c_parser_next_token_is (parser, CPP_NAME))
7640 name = c_parser_peek_token (parser)->value;
7641 c_parser_consume_token (parser);
7642 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7644 else
7645 c_parser_error (parser, "expected identifier");
7647 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7648 c_parser_error (parser, "expected %<(%> or end of line");
7649 c_parser_skip_to_pragma_eol (parser);
7651 stmt = c_parser_omp_structured_block (parser);
7652 return c_finish_omp_critical (stmt, name);
7655 /* OpenMP 2.5:
7656 # pragma omp flush flush-vars[opt] new-line
7658 flush-vars:
7659 ( variable-list ) */
7661 static void
7662 c_parser_omp_flush (c_parser *parser)
7664 c_parser_consume_pragma (parser);
7665 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7666 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
7667 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7668 c_parser_error (parser, "expected %<(%> or end of line");
7669 c_parser_skip_to_pragma_eol (parser);
7671 c_finish_omp_flush ();
7674 /* Parse the restricted form of the for statement allowed by OpenMP.
7675 The real trick here is to determine the loop control variable early
7676 so that we can push a new decl if necessary to make it private. */
7678 static tree
7679 c_parser_omp_for_loop (c_parser *parser, tree clauses, tree *par_clauses)
7681 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7682 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
7683 location_t loc;
7684 bool fail = false, open_brace_parsed = false;
7685 int i, collapse = 1, nbraces = 0;
7687 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
7688 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
7689 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
7691 gcc_assert (collapse >= 1);
7693 declv = make_tree_vec (collapse);
7694 initv = make_tree_vec (collapse);
7695 condv = make_tree_vec (collapse);
7696 incrv = make_tree_vec (collapse);
7698 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7700 c_parser_error (parser, "for statement expected");
7701 return NULL;
7703 loc = c_parser_peek_token (parser)->location;
7704 c_parser_consume_token (parser);
7706 for (i = 0; i < collapse; i++)
7708 int bracecount = 0;
7710 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7711 goto pop_scopes;
7713 /* Parse the initialization declaration or expression. */
7714 if (c_parser_next_token_starts_declspecs (parser))
7716 if (i > 0)
7717 for_block
7718 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
7719 c_parser_declaration_or_fndef (parser, true, true, true, true);
7720 decl = check_for_loop_decls ();
7721 if (decl == NULL)
7722 goto error_init;
7723 if (DECL_INITIAL (decl) == error_mark_node)
7724 decl = error_mark_node;
7725 init = decl;
7727 else if (c_parser_next_token_is (parser, CPP_NAME)
7728 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7730 struct c_expr init_exp;
7731 location_t init_loc;
7733 decl = c_parser_postfix_expression (parser).value;
7735 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7736 init_loc = c_parser_peek_token (parser)->location;
7738 init_exp = c_parser_expr_no_commas (parser, NULL);
7739 init_exp = default_function_array_conversion (init_exp);
7740 init = build_modify_expr (init_loc,
7741 decl, NOP_EXPR, init_exp.value);
7742 init = c_process_expr_stmt (init);
7744 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7746 else
7748 error_init:
7749 c_parser_error (parser,
7750 "expected iteration declaration or initialization");
7751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7752 "expected %<)%>");
7753 fail = true;
7754 goto parse_next;
7757 /* Parse the loop condition. */
7758 cond = NULL_TREE;
7759 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7761 location_t cond_loc = c_parser_peek_token (parser)->location;
7762 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
7764 cond = cond_expr.value;
7765 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
7766 cond = c_fully_fold (cond, false, NULL);
7767 switch (cond_expr.original_code)
7769 case GT_EXPR:
7770 case GE_EXPR:
7771 case LT_EXPR:
7772 case LE_EXPR:
7773 break;
7774 default:
7775 /* Can't be cond = error_mark_node, because we want to preserve
7776 the location until c_finish_omp_for. */
7777 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
7778 break;
7780 protected_set_expr_location (cond, cond_loc);
7782 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7784 /* Parse the increment expression. */
7785 incr = NULL_TREE;
7786 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7788 location_t incr_loc = c_parser_peek_token (parser)->location;
7790 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7791 protected_set_expr_location (incr, incr_loc);
7793 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7795 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
7796 fail = true;
7797 else
7799 TREE_VEC_ELT (declv, i) = decl;
7800 TREE_VEC_ELT (initv, i) = init;
7801 TREE_VEC_ELT (condv, i) = cond;
7802 TREE_VEC_ELT (incrv, i) = incr;
7805 parse_next:
7806 if (i == collapse - 1)
7807 break;
7809 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
7810 in between the collapsed for loops to be still considered perfectly
7811 nested. Hopefully the final version clarifies this.
7812 For now handle (multiple) {'s and empty statements. */
7815 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7817 c_parser_consume_token (parser);
7818 break;
7820 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7822 c_parser_consume_token (parser);
7823 bracecount++;
7825 else if (bracecount
7826 && c_parser_next_token_is (parser, CPP_SEMICOLON))
7827 c_parser_consume_token (parser);
7828 else
7830 c_parser_error (parser, "not enough perfectly nested loops");
7831 if (bracecount)
7833 open_brace_parsed = true;
7834 bracecount--;
7836 fail = true;
7837 collapse = 0;
7838 break;
7841 while (1);
7843 nbraces += bracecount;
7846 save_break = c_break_label;
7847 c_break_label = size_one_node;
7848 save_cont = c_cont_label;
7849 c_cont_label = NULL_TREE;
7850 body = push_stmt_list ();
7852 if (open_brace_parsed)
7854 stmt = c_begin_compound_stmt (true);
7855 c_parser_compound_statement_nostart (parser);
7856 add_stmt (c_end_compound_stmt (stmt, true));
7858 else
7859 add_stmt (c_parser_c99_block_statement (parser));
7860 if (c_cont_label)
7861 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7863 body = pop_stmt_list (body);
7864 c_break_label = save_break;
7865 c_cont_label = save_cont;
7867 while (nbraces)
7869 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7871 c_parser_consume_token (parser);
7872 nbraces--;
7874 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7875 c_parser_consume_token (parser);
7876 else
7878 c_parser_error (parser, "collapsed loops not perfectly nested");
7879 while (nbraces)
7881 stmt = c_begin_compound_stmt (true);
7882 add_stmt (body);
7883 c_parser_compound_statement_nostart (parser);
7884 body = c_end_compound_stmt (stmt, true);
7885 nbraces--;
7887 goto pop_scopes;
7891 /* Only bother calling c_finish_omp_for if we haven't already generated
7892 an error from the initialization parsing. */
7893 if (!fail)
7895 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
7896 if (stmt)
7898 if (par_clauses != NULL)
7900 tree *c;
7901 for (c = par_clauses; *c ; )
7902 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
7903 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
7904 c = &OMP_CLAUSE_CHAIN (*c);
7905 else
7907 for (i = 0; i < collapse; i++)
7908 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
7909 break;
7910 if (i == collapse)
7911 c = &OMP_CLAUSE_CHAIN (*c);
7912 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
7914 error_at (loc,
7915 "iteration variable %qD should not be firstprivate",
7916 OMP_CLAUSE_DECL (*c));
7917 *c = OMP_CLAUSE_CHAIN (*c);
7919 else
7921 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
7922 change it to shared (decl) in
7923 OMP_PARALLEL_CLAUSES. */
7924 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
7925 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
7926 OMP_CLAUSE_CHAIN (l) = clauses;
7927 clauses = l;
7928 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
7932 OMP_FOR_CLAUSES (stmt) = clauses;
7934 ret = stmt;
7936 pop_scopes:
7937 while (for_block)
7939 stmt = c_end_compound_stmt (TREE_VALUE (for_block), true);
7940 add_stmt (stmt);
7941 for_block = TREE_CHAIN (for_block);
7943 return ret;
7946 /* OpenMP 2.5:
7947 #pragma omp for for-clause[optseq] new-line
7948 for-loop
7951 #define OMP_FOR_CLAUSE_MASK \
7952 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7953 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7954 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7955 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7956 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7957 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7958 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
7959 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7961 static tree
7962 c_parser_omp_for (c_parser *parser)
7964 tree block, clauses, ret;
7966 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7967 "#pragma omp for");
7969 block = c_begin_compound_stmt (true);
7970 ret = c_parser_omp_for_loop (parser, clauses, NULL);
7971 block = c_end_compound_stmt (block, true);
7972 add_stmt (block);
7974 return ret;
7977 /* OpenMP 2.5:
7978 # pragma omp master new-line
7979 structured-block
7982 static tree
7983 c_parser_omp_master (c_parser *parser)
7985 c_parser_skip_to_pragma_eol (parser);
7986 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7989 /* OpenMP 2.5:
7990 # pragma omp ordered new-line
7991 structured-block
7994 static tree
7995 c_parser_omp_ordered (c_parser *parser)
7997 c_parser_skip_to_pragma_eol (parser);
7998 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
8001 /* OpenMP 2.5:
8003 section-scope:
8004 { section-sequence }
8006 section-sequence:
8007 section-directive[opt] structured-block
8008 section-sequence section-directive structured-block */
8010 static tree
8011 c_parser_omp_sections_scope (c_parser *parser)
8013 tree stmt, substmt;
8014 bool error_suppress = false;
8015 location_t loc;
8017 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8019 /* Avoid skipping until the end of the block. */
8020 parser->error = false;
8021 return NULL_TREE;
8024 stmt = push_stmt_list ();
8026 loc = c_parser_peek_token (parser)->location;
8027 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8029 substmt = push_stmt_list ();
8031 while (1)
8033 c_parser_statement (parser);
8035 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8036 break;
8037 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8038 break;
8039 if (c_parser_next_token_is (parser, CPP_EOF))
8040 break;
8043 substmt = pop_stmt_list (substmt);
8044 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8045 SET_EXPR_LOCATION (substmt, loc);
8046 add_stmt (substmt);
8049 while (1)
8051 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8052 break;
8053 if (c_parser_next_token_is (parser, CPP_EOF))
8054 break;
8056 loc = c_parser_peek_token (parser)->location;
8057 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8059 c_parser_consume_pragma (parser);
8060 c_parser_skip_to_pragma_eol (parser);
8061 error_suppress = false;
8063 else if (!error_suppress)
8065 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8066 error_suppress = true;
8069 substmt = c_parser_omp_structured_block (parser);
8070 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8071 SET_EXPR_LOCATION (substmt, loc);
8072 add_stmt (substmt);
8074 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8075 "expected %<#pragma omp section%> or %<}%>");
8077 substmt = pop_stmt_list (stmt);
8079 stmt = make_node (OMP_SECTIONS);
8080 TREE_TYPE (stmt) = void_type_node;
8081 OMP_SECTIONS_BODY (stmt) = substmt;
8083 return add_stmt (stmt);
8086 /* OpenMP 2.5:
8087 # pragma omp sections sections-clause[optseq] newline
8088 sections-scope
8091 #define OMP_SECTIONS_CLAUSE_MASK \
8092 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8093 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8094 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8095 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8096 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8098 static tree
8099 c_parser_omp_sections (c_parser *parser)
8101 tree block, clauses, ret;
8103 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8104 "#pragma omp sections");
8106 block = c_begin_compound_stmt (true);
8107 ret = c_parser_omp_sections_scope (parser);
8108 if (ret)
8109 OMP_SECTIONS_CLAUSES (ret) = clauses;
8110 block = c_end_compound_stmt (block, true);
8111 add_stmt (block);
8113 return ret;
8116 /* OpenMP 2.5:
8117 # pragma parallel parallel-clause new-line
8118 # pragma parallel for parallel-for-clause new-line
8119 # pragma parallel sections parallel-sections-clause new-line
8122 #define OMP_PARALLEL_CLAUSE_MASK \
8123 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8124 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8125 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8126 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8127 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8128 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8129 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8130 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8132 static tree
8133 c_parser_omp_parallel (c_parser *parser)
8135 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8136 const char *p_name = "#pragma omp parallel";
8137 tree stmt, clauses, par_clause, ws_clause, block;
8138 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8140 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8142 c_parser_consume_token (parser);
8143 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8144 p_name = "#pragma omp parallel for";
8145 mask |= OMP_FOR_CLAUSE_MASK;
8146 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8148 else if (c_parser_next_token_is (parser, CPP_NAME))
8150 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8151 if (strcmp (p, "sections") == 0)
8153 c_parser_consume_token (parser);
8154 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8155 p_name = "#pragma omp parallel sections";
8156 mask |= OMP_SECTIONS_CLAUSE_MASK;
8157 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8161 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8163 switch (p_kind)
8165 case PRAGMA_OMP_PARALLEL:
8166 block = c_begin_omp_parallel ();
8167 c_parser_statement (parser);
8168 stmt = c_finish_omp_parallel (clauses, block);
8169 break;
8171 case PRAGMA_OMP_PARALLEL_FOR:
8172 block = c_begin_omp_parallel ();
8173 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8174 c_parser_omp_for_loop (parser, ws_clause, &par_clause);
8175 stmt = c_finish_omp_parallel (par_clause, block);
8176 OMP_PARALLEL_COMBINED (stmt) = 1;
8177 break;
8179 case PRAGMA_OMP_PARALLEL_SECTIONS:
8180 block = c_begin_omp_parallel ();
8181 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8182 stmt = c_parser_omp_sections_scope (parser);
8183 if (stmt)
8184 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8185 stmt = c_finish_omp_parallel (par_clause, block);
8186 OMP_PARALLEL_COMBINED (stmt) = 1;
8187 break;
8189 default:
8190 gcc_unreachable ();
8193 return stmt;
8196 /* OpenMP 2.5:
8197 # pragma omp single single-clause[optseq] new-line
8198 structured-block
8201 #define OMP_SINGLE_CLAUSE_MASK \
8202 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8203 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8204 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8205 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8207 static tree
8208 c_parser_omp_single (c_parser *parser)
8210 tree stmt = make_node (OMP_SINGLE);
8211 TREE_TYPE (stmt) = void_type_node;
8213 OMP_SINGLE_CLAUSES (stmt)
8214 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8215 "#pragma omp single");
8216 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8218 return add_stmt (stmt);
8221 /* OpenMP 3.0:
8222 # pragma omp task task-clause[optseq] new-line
8225 #define OMP_TASK_CLAUSE_MASK \
8226 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8227 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8228 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8229 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8230 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8231 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8233 static tree
8234 c_parser_omp_task (c_parser *parser)
8236 tree clauses, block;
8238 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8239 "#pragma omp task");
8241 block = c_begin_omp_task ();
8242 c_parser_statement (parser);
8243 return c_finish_omp_task (clauses, block);
8246 /* OpenMP 3.0:
8247 # pragma omp taskwait new-line
8250 static void
8251 c_parser_omp_taskwait (c_parser *parser)
8253 c_parser_consume_pragma (parser);
8254 c_parser_skip_to_pragma_eol (parser);
8256 c_finish_omp_taskwait ();
8259 /* Main entry point to parsing most OpenMP pragmas. */
8261 static void
8262 c_parser_omp_construct (c_parser *parser)
8264 enum pragma_kind p_kind;
8265 location_t loc;
8266 tree stmt;
8268 loc = c_parser_peek_token (parser)->location;
8269 p_kind = c_parser_peek_token (parser)->pragma_kind;
8270 c_parser_consume_pragma (parser);
8272 /* For all constructs below except #pragma omp atomic
8273 MUST_NOT_THROW catch handlers are needed when exceptions
8274 are enabled. */
8275 if (p_kind != PRAGMA_OMP_ATOMIC)
8276 c_maybe_initialize_eh ();
8278 switch (p_kind)
8280 case PRAGMA_OMP_ATOMIC:
8281 c_parser_omp_atomic (parser);
8282 return;
8283 case PRAGMA_OMP_CRITICAL:
8284 stmt = c_parser_omp_critical (parser);
8285 break;
8286 case PRAGMA_OMP_FOR:
8287 stmt = c_parser_omp_for (parser);
8288 break;
8289 case PRAGMA_OMP_MASTER:
8290 stmt = c_parser_omp_master (parser);
8291 break;
8292 case PRAGMA_OMP_ORDERED:
8293 stmt = c_parser_omp_ordered (parser);
8294 break;
8295 case PRAGMA_OMP_PARALLEL:
8296 stmt = c_parser_omp_parallel (parser);
8297 break;
8298 case PRAGMA_OMP_SECTIONS:
8299 stmt = c_parser_omp_sections (parser);
8300 break;
8301 case PRAGMA_OMP_SINGLE:
8302 stmt = c_parser_omp_single (parser);
8303 break;
8304 case PRAGMA_OMP_TASK:
8305 stmt = c_parser_omp_task (parser);
8306 break;
8307 default:
8308 gcc_unreachable ();
8311 if (stmt)
8312 SET_EXPR_LOCATION (stmt, loc);
8316 /* OpenMP 2.5:
8317 # pragma omp threadprivate (variable-list) */
8319 static void
8320 c_parser_omp_threadprivate (c_parser *parser)
8322 tree vars, t;
8324 c_parser_consume_pragma (parser);
8325 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8327 /* Mark every variable in VARS to be assigned thread local storage. */
8328 for (t = vars; t; t = TREE_CHAIN (t))
8330 tree v = TREE_PURPOSE (t);
8332 /* If V had already been marked threadprivate, it doesn't matter
8333 whether it had been used prior to this point. */
8334 if (TREE_CODE (v) != VAR_DECL)
8335 error ("%qD is not a variable", v);
8336 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8337 error ("%qE declared %<threadprivate%> after first use", v);
8338 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8339 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8340 else if (TREE_TYPE (v) == error_mark_node)
8342 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8343 error ("%<threadprivate%> %qE has incomplete type", v);
8344 else
8346 if (! DECL_THREAD_LOCAL_P (v))
8348 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8349 /* If rtl has been already set for this var, call
8350 make_decl_rtl once again, so that encode_section_info
8351 has a chance to look at the new decl flags. */
8352 if (DECL_RTL_SET_P (v))
8353 make_decl_rtl (v);
8355 C_DECL_THREADPRIVATE_P (v) = 1;
8359 c_parser_skip_to_pragma_eol (parser);
8363 /* Parse a single source file. */
8365 void
8366 c_parse_file (void)
8368 /* Use local storage to begin. If the first token is a pragma, parse it.
8369 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8370 which will cause garbage collection. */
8371 c_parser tparser;
8373 memset (&tparser, 0, sizeof tparser);
8374 the_parser = &tparser;
8376 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8377 c_parser_pragma_pch_preprocess (&tparser);
8379 the_parser = GGC_NEW (c_parser);
8380 *the_parser = tparser;
8382 c_parser_translation_unit (the_parser);
8383 the_parser = NULL;
8386 #include "gt-c-parser.h"