* config/mips/mips.h (ISA_HAS_DMUL3, ISA_HAS_BADDU, ISA_HAS_BBIT,
[official-gcc.git] / gcc / c-parser.c
blob2b64c86953adcfc2c56efeba71e7f5638eb822ba
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
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"
62 /* Initialization routine for this file. */
64 void
65 c_parse_init (void)
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
71 int mask = 0;
73 mask |= D_CXXONLY;
74 if (!flag_isoc99)
75 mask |= D_C99;
76 if (flag_no_asm)
78 mask |= D_ASM | D_EXT;
79 if (!flag_isoc99)
80 mask |= D_EXT89;
82 if (!c_dialect_objc ())
83 mask |= D_OBJC | D_CXX_OBJC;
85 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
86 for (i = 0; i < num_c_common_reswords; i++)
88 /* If a keyword is disabled, do not enter it into the table
89 and so create a canonical spelling that isn't a keyword. */
90 if (c_common_reswords[i].disable & mask)
92 if (warn_cxx_compat
93 && (c_common_reswords[i].disable & D_CXXWARN))
95 id = get_identifier (c_common_reswords[i].word);
96 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
97 C_IS_RESERVED_WORD (id) = 1;
99 continue;
102 id = get_identifier (c_common_reswords[i].word);
103 C_SET_RID_CODE (id, c_common_reswords[i].rid);
104 C_IS_RESERVED_WORD (id) = 1;
105 ridpointers [(int) c_common_reswords[i].rid] = id;
109 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
110 and the C parser. Unlike the C++ lexer, the parser structure
111 stores the lexer information instead of using a separate structure.
112 Identifiers are separated into ordinary identifiers, type names,
113 keywords and some other Objective-C types of identifiers, and some
114 look-ahead is maintained.
116 ??? It might be a good idea to lex the whole file up front (as for
117 C++). It would then be possible to share more of the C and C++
118 lexer code, if desired. */
120 /* The following local token type is used. */
122 /* A keyword. */
123 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
125 /* More information about the type of a CPP_NAME token. */
126 typedef enum c_id_kind {
127 /* An ordinary identifier. */
128 C_ID_ID,
129 /* An identifier declared as a typedef name. */
130 C_ID_TYPENAME,
131 /* An identifier declared as an Objective-C class name. */
132 C_ID_CLASSNAME,
133 /* Not an identifier. */
134 C_ID_NONE
135 } c_id_kind;
137 /* A single C token after string literal concatenation and conversion
138 of preprocessing tokens to tokens. */
139 typedef struct c_token GTY (())
141 /* The kind of token. */
142 ENUM_BITFIELD (cpp_ttype) type : 8;
143 /* If this token is a CPP_NAME, this value indicates whether also
144 declared as some kind of type. Otherwise, it is C_ID_NONE. */
145 ENUM_BITFIELD (c_id_kind) id_kind : 8;
146 /* If this token is a keyword, this value indicates which keyword.
147 Otherwise, this value is RID_MAX. */
148 ENUM_BITFIELD (rid) keyword : 8;
149 /* If this token is a CPP_PRAGMA, this indicates the pragma that
150 was seen. Otherwise it is PRAGMA_NONE. */
151 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
152 /* The value associated with this token, if any. */
153 tree value;
154 /* The location at which this token was found. */
155 location_t location;
156 } c_token;
158 /* A parser structure recording information about the state and
159 context of parsing. Includes lexer information with up to two
160 tokens of look-ahead; more are not needed for C. */
161 typedef struct c_parser GTY(())
163 /* The look-ahead tokens. */
164 c_token tokens[2];
165 /* How many look-ahead tokens are available (0, 1 or 2). */
166 short tokens_avail;
167 /* True if a syntax error is being recovered from; false otherwise.
168 c_parser_error sets this flag. It should clear this flag when
169 enough tokens have been consumed to recover from the error. */
170 BOOL_BITFIELD error : 1;
171 /* True if we're processing a pragma, and shouldn't automatically
172 consume CPP_PRAGMA_EOL. */
173 BOOL_BITFIELD in_pragma : 1;
174 /* True if we're parsing the outermost block of an if statement. */
175 BOOL_BITFIELD in_if_block : 1;
176 /* True if we want to lex an untranslated string. */
177 BOOL_BITFIELD lex_untranslated_string : 1;
178 /* Objective-C specific parser/lexer information. */
179 BOOL_BITFIELD objc_pq_context : 1;
180 /* The following flag is needed to contextualize Objective-C lexical
181 analysis. In some cases (e.g., 'int NSObject;'), it is
182 undesirable to bind an identifier to an Objective-C class, even
183 if a class with that name exists. */
184 BOOL_BITFIELD objc_need_raw_identifier : 1;
185 } c_parser;
188 /* The actual parser and external interface. ??? Does this need to be
189 garbage-collected? */
191 static GTY (()) c_parser *the_parser;
194 /* Read in and lex a single token, storing it in *TOKEN. */
196 static void
197 c_lex_one_token (c_parser *parser, c_token *token)
199 timevar_push (TV_LEX);
201 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
202 (parser->lex_untranslated_string
203 ? C_LEX_STRING_NO_TRANSLATE : 0));
204 token->id_kind = C_ID_NONE;
205 token->keyword = RID_MAX;
206 token->pragma_kind = PRAGMA_NONE;
208 switch (token->type)
210 case CPP_NAME:
212 tree decl;
214 bool objc_force_identifier = parser->objc_need_raw_identifier;
215 if (c_dialect_objc ())
216 parser->objc_need_raw_identifier = false;
218 if (C_IS_RESERVED_WORD (token->value))
220 enum rid rid_code = C_RID_CODE (token->value);
222 if (rid_code == RID_CXX_COMPAT_WARN)
224 warning_at (token->location,
225 OPT_Wc___compat,
226 "identifier %qs conflicts with C++ keyword",
227 IDENTIFIER_POINTER (token->value));
229 else if (c_dialect_objc ())
231 if (!objc_is_reserved_word (token->value)
232 && (!OBJC_IS_PQ_KEYWORD (rid_code)
233 || parser->objc_pq_context))
235 /* Return the canonical spelling for this keyword. */
236 token->value = ridpointers[(int) rid_code];
237 token->type = CPP_KEYWORD;
238 token->keyword = rid_code;
239 break;
242 else
244 token->type = CPP_KEYWORD;
245 token->keyword = rid_code;
246 break;
250 decl = lookup_name (token->value);
251 if (decl)
253 if (TREE_CODE (decl) == TYPE_DECL)
255 token->id_kind = C_ID_TYPENAME;
256 break;
259 else if (c_dialect_objc ())
261 tree objc_interface_decl = objc_is_class_name (token->value);
262 /* Objective-C class names are in the same namespace as
263 variables and typedefs, and hence are shadowed by local
264 declarations. */
265 if (objc_interface_decl
266 && (global_bindings_p ()
267 || (!objc_force_identifier && !decl)))
269 token->value = objc_interface_decl;
270 token->id_kind = C_ID_CLASSNAME;
271 break;
274 token->id_kind = C_ID_ID;
276 break;
277 case CPP_AT_NAME:
278 /* This only happens in Objective-C; it must be a keyword. */
279 token->type = CPP_KEYWORD;
280 token->keyword = C_RID_CODE (token->value);
281 break;
282 case CPP_COLON:
283 case CPP_COMMA:
284 case CPP_CLOSE_PAREN:
285 case CPP_SEMICOLON:
286 /* These tokens may affect the interpretation of any identifiers
287 following, if doing Objective-C. */
288 if (c_dialect_objc ())
289 parser->objc_need_raw_identifier = false;
290 break;
291 case CPP_PRAGMA:
292 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
293 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
294 token->value = NULL;
295 break;
296 default:
297 break;
299 timevar_pop (TV_LEX);
302 /* Return a pointer to the next token from PARSER, reading it in if
303 necessary. */
305 static inline c_token *
306 c_parser_peek_token (c_parser *parser)
308 if (parser->tokens_avail == 0)
310 c_lex_one_token (parser, &parser->tokens[0]);
311 parser->tokens_avail = 1;
313 return &parser->tokens[0];
316 /* Return true if the next token from PARSER has the indicated
317 TYPE. */
319 static inline bool
320 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
322 return c_parser_peek_token (parser)->type == type;
325 /* Return true if the next token from PARSER does not have the
326 indicated TYPE. */
328 static inline bool
329 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
331 return !c_parser_next_token_is (parser, type);
334 /* Return true if the next token from PARSER is the indicated
335 KEYWORD. */
337 static inline bool
338 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
340 return c_parser_peek_token (parser)->keyword == keyword;
343 /* Return true if TOKEN can start a type name,
344 false otherwise. */
345 static bool
346 c_token_starts_typename (c_token *token)
348 switch (token->type)
350 case CPP_NAME:
351 switch (token->id_kind)
353 case C_ID_ID:
354 return false;
355 case C_ID_TYPENAME:
356 return true;
357 case C_ID_CLASSNAME:
358 gcc_assert (c_dialect_objc ());
359 return true;
360 default:
361 gcc_unreachable ();
363 case CPP_KEYWORD:
364 switch (token->keyword)
366 case RID_UNSIGNED:
367 case RID_LONG:
368 case RID_SHORT:
369 case RID_SIGNED:
370 case RID_COMPLEX:
371 case RID_INT:
372 case RID_CHAR:
373 case RID_FLOAT:
374 case RID_DOUBLE:
375 case RID_VOID:
376 case RID_DFLOAT32:
377 case RID_DFLOAT64:
378 case RID_DFLOAT128:
379 case RID_BOOL:
380 case RID_ENUM:
381 case RID_STRUCT:
382 case RID_UNION:
383 case RID_TYPEOF:
384 case RID_CONST:
385 case RID_VOLATILE:
386 case RID_RESTRICT:
387 case RID_ATTRIBUTE:
388 case RID_FRACT:
389 case RID_ACCUM:
390 case RID_SAT:
391 return true;
392 default:
393 return false;
395 case CPP_LESS:
396 if (c_dialect_objc ())
397 return true;
398 return false;
399 default:
400 return false;
404 /* Return true if the next token from PARSER can start a type name,
405 false otherwise. */
406 static inline bool
407 c_parser_next_token_starts_typename (c_parser *parser)
409 c_token *token = c_parser_peek_token (parser);
410 return c_token_starts_typename (token);
413 /* Return true if TOKEN can start declaration specifiers, false
414 otherwise. */
415 static bool
416 c_token_starts_declspecs (c_token *token)
418 switch (token->type)
420 case CPP_NAME:
421 switch (token->id_kind)
423 case C_ID_ID:
424 return false;
425 case C_ID_TYPENAME:
426 return true;
427 case C_ID_CLASSNAME:
428 gcc_assert (c_dialect_objc ());
429 return true;
430 default:
431 gcc_unreachable ();
433 case CPP_KEYWORD:
434 switch (token->keyword)
436 case RID_STATIC:
437 case RID_EXTERN:
438 case RID_REGISTER:
439 case RID_TYPEDEF:
440 case RID_INLINE:
441 case RID_AUTO:
442 case RID_THREAD:
443 case RID_UNSIGNED:
444 case RID_LONG:
445 case RID_SHORT:
446 case RID_SIGNED:
447 case RID_COMPLEX:
448 case RID_INT:
449 case RID_CHAR:
450 case RID_FLOAT:
451 case RID_DOUBLE:
452 case RID_VOID:
453 case RID_DFLOAT32:
454 case RID_DFLOAT64:
455 case RID_DFLOAT128:
456 case RID_BOOL:
457 case RID_ENUM:
458 case RID_STRUCT:
459 case RID_UNION:
460 case RID_TYPEOF:
461 case RID_CONST:
462 case RID_VOLATILE:
463 case RID_RESTRICT:
464 case RID_ATTRIBUTE:
465 case RID_FRACT:
466 case RID_ACCUM:
467 case RID_SAT:
468 return true;
469 default:
470 return false;
472 case CPP_LESS:
473 if (c_dialect_objc ())
474 return true;
475 return false;
476 default:
477 return false;
481 /* Return true if the next token from PARSER can start declaration
482 specifiers, false otherwise. */
483 static inline bool
484 c_parser_next_token_starts_declspecs (c_parser *parser)
486 c_token *token = c_parser_peek_token (parser);
487 return c_token_starts_declspecs (token);
490 /* Return a pointer to the next-but-one token from PARSER, reading it
491 in if necessary. The next token is already read in. */
493 static c_token *
494 c_parser_peek_2nd_token (c_parser *parser)
496 if (parser->tokens_avail >= 2)
497 return &parser->tokens[1];
498 gcc_assert (parser->tokens_avail == 1);
499 gcc_assert (parser->tokens[0].type != CPP_EOF);
500 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
501 c_lex_one_token (parser, &parser->tokens[1]);
502 parser->tokens_avail = 2;
503 return &parser->tokens[1];
506 /* Consume the next token from PARSER. */
508 static void
509 c_parser_consume_token (c_parser *parser)
511 gcc_assert (parser->tokens_avail >= 1);
512 gcc_assert (parser->tokens[0].type != CPP_EOF);
513 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
514 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
515 if (parser->tokens_avail == 2)
516 parser->tokens[0] = parser->tokens[1];
517 parser->tokens_avail--;
520 /* Expect the current token to be a #pragma. Consume it and remember
521 that we've begun parsing a pragma. */
523 static void
524 c_parser_consume_pragma (c_parser *parser)
526 gcc_assert (!parser->in_pragma);
527 gcc_assert (parser->tokens_avail >= 1);
528 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
529 if (parser->tokens_avail == 2)
530 parser->tokens[0] = parser->tokens[1];
531 parser->tokens_avail--;
532 parser->in_pragma = true;
535 /* Update the globals input_location and in_system_header from
536 TOKEN. */
537 static inline void
538 c_parser_set_source_position_from_token (c_token *token)
540 if (token->type != CPP_EOF)
542 input_location = token->location;
546 /* Issue a diagnostic of the form
547 FILE:LINE: MESSAGE before TOKEN
548 where TOKEN is the next token in the input stream of PARSER.
549 MESSAGE (specified by the caller) is usually of the form "expected
550 OTHER-TOKEN".
552 Do not issue a diagnostic if still recovering from an error.
554 ??? This is taken from the C++ parser, but building up messages in
555 this way is not i18n-friendly and some other approach should be
556 used. */
558 static void
559 c_parser_error (c_parser *parser, const char *gmsgid)
561 c_token *token = c_parser_peek_token (parser);
562 if (parser->error)
563 return;
564 parser->error = true;
565 if (!gmsgid)
566 return;
567 /* This diagnostic makes more sense if it is tagged to the line of
568 the token we just peeked at. */
569 c_parser_set_source_position_from_token (token);
570 c_parse_error (gmsgid,
571 /* Because c_parse_error does not understand
572 CPP_KEYWORD, keywords are treated like
573 identifiers. */
574 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
575 token->value);
578 /* If the next token is of the indicated TYPE, consume it. Otherwise,
579 issue the error MSGID. If MSGID is NULL then a message has already
580 been produced and no message will be produced this time. Returns
581 true if found, false otherwise. */
583 static bool
584 c_parser_require (c_parser *parser,
585 enum cpp_ttype type,
586 const char *msgid)
588 if (c_parser_next_token_is (parser, type))
590 c_parser_consume_token (parser);
591 return true;
593 else
595 c_parser_error (parser, msgid);
596 return false;
600 /* If the next token is the indicated keyword, consume it. Otherwise,
601 issue the error MSGID. Returns true if found, false otherwise. */
603 static bool
604 c_parser_require_keyword (c_parser *parser,
605 enum rid keyword,
606 const char *msgid)
608 if (c_parser_next_token_is_keyword (parser, keyword))
610 c_parser_consume_token (parser);
611 return true;
613 else
615 c_parser_error (parser, msgid);
616 return false;
620 /* Like c_parser_require, except that tokens will be skipped until the
621 desired token is found. An error message is still produced if the
622 next token is not as expected. If MSGID is NULL then a message has
623 already been produced and no message will be produced this
624 time. */
626 static void
627 c_parser_skip_until_found (c_parser *parser,
628 enum cpp_ttype type,
629 const char *msgid)
631 unsigned nesting_depth = 0;
633 if (c_parser_require (parser, type, msgid))
634 return;
636 /* Skip tokens until the desired token is found. */
637 while (true)
639 /* Peek at the next token. */
640 c_token *token = c_parser_peek_token (parser);
641 /* If we've reached the token we want, consume it and stop. */
642 if (token->type == type && !nesting_depth)
644 c_parser_consume_token (parser);
645 break;
648 /* If we've run out of tokens, stop. */
649 if (token->type == CPP_EOF)
650 return;
651 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
652 return;
653 if (token->type == CPP_OPEN_BRACE
654 || token->type == CPP_OPEN_PAREN
655 || token->type == CPP_OPEN_SQUARE)
656 ++nesting_depth;
657 else if (token->type == CPP_CLOSE_BRACE
658 || token->type == CPP_CLOSE_PAREN
659 || token->type == CPP_CLOSE_SQUARE)
661 if (nesting_depth-- == 0)
662 break;
664 /* Consume this token. */
665 c_parser_consume_token (parser);
667 parser->error = false;
670 /* Skip tokens until the end of a parameter is found, but do not
671 consume the comma, semicolon or closing delimiter. */
673 static void
674 c_parser_skip_to_end_of_parameter (c_parser *parser)
676 unsigned nesting_depth = 0;
678 while (true)
680 c_token *token = c_parser_peek_token (parser);
681 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
682 && !nesting_depth)
683 break;
684 /* If we've run out of tokens, stop. */
685 if (token->type == CPP_EOF)
686 return;
687 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
688 return;
689 if (token->type == CPP_OPEN_BRACE
690 || token->type == CPP_OPEN_PAREN
691 || token->type == CPP_OPEN_SQUARE)
692 ++nesting_depth;
693 else if (token->type == CPP_CLOSE_BRACE
694 || token->type == CPP_CLOSE_PAREN
695 || token->type == CPP_CLOSE_SQUARE)
697 if (nesting_depth-- == 0)
698 break;
700 /* Consume this token. */
701 c_parser_consume_token (parser);
703 parser->error = false;
706 /* Expect to be at the end of the pragma directive and consume an
707 end of line marker. */
709 static void
710 c_parser_skip_to_pragma_eol (c_parser *parser)
712 gcc_assert (parser->in_pragma);
713 parser->in_pragma = false;
715 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
716 while (true)
718 c_token *token = c_parser_peek_token (parser);
719 if (token->type == CPP_EOF)
720 break;
721 if (token->type == CPP_PRAGMA_EOL)
723 c_parser_consume_token (parser);
724 break;
726 c_parser_consume_token (parser);
729 parser->error = false;
732 /* Skip tokens until we have consumed an entire block, or until we
733 have consumed a non-nested ';'. */
735 static void
736 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
738 unsigned nesting_depth = 0;
739 bool save_error = parser->error;
741 while (true)
743 c_token *token;
745 /* Peek at the next token. */
746 token = c_parser_peek_token (parser);
748 switch (token->type)
750 case CPP_EOF:
751 return;
753 case CPP_PRAGMA_EOL:
754 if (parser->in_pragma)
755 return;
756 break;
758 case CPP_SEMICOLON:
759 /* If the next token is a ';', we have reached the
760 end of the statement. */
761 if (!nesting_depth)
763 /* Consume the ';'. */
764 c_parser_consume_token (parser);
765 goto finished;
767 break;
769 case CPP_CLOSE_BRACE:
770 /* If the next token is a non-nested '}', then we have
771 reached the end of the current block. */
772 if (nesting_depth == 0 || --nesting_depth == 0)
774 c_parser_consume_token (parser);
775 goto finished;
777 break;
779 case CPP_OPEN_BRACE:
780 /* If it the next token is a '{', then we are entering a new
781 block. Consume the entire block. */
782 ++nesting_depth;
783 break;
785 case CPP_PRAGMA:
786 /* If we see a pragma, consume the whole thing at once. We
787 have some safeguards against consuming pragmas willy-nilly.
788 Normally, we'd expect to be here with parser->error set,
789 which disables these safeguards. But it's possible to get
790 here for secondary error recovery, after parser->error has
791 been cleared. */
792 c_parser_consume_pragma (parser);
793 c_parser_skip_to_pragma_eol (parser);
794 parser->error = save_error;
795 continue;
797 default:
798 break;
801 c_parser_consume_token (parser);
804 finished:
805 parser->error = false;
808 /* CPP's options (initialized by c-opts.c). */
809 extern cpp_options *cpp_opts;
811 /* Save the warning flags which are controlled by __extension__. */
813 static inline int
814 disable_extension_diagnostics (void)
816 int ret = (pedantic
817 | (warn_pointer_arith << 1)
818 | (warn_traditional << 2)
819 | (flag_iso << 3)
820 | (warn_long_long << 4)
821 | (cpp_opts->warn_long_long << 5));
822 cpp_opts->pedantic = pedantic = 0;
823 warn_pointer_arith = 0;
824 cpp_opts->warn_traditional = warn_traditional = 0;
825 flag_iso = 0;
826 warn_long_long = 0;
827 cpp_opts->warn_long_long = 0;
828 return ret;
831 /* Restore the warning flags which are controlled by __extension__.
832 FLAGS is the return value from disable_extension_diagnostics. */
834 static inline void
835 restore_extension_diagnostics (int flags)
837 cpp_opts->pedantic = pedantic = flags & 1;
838 warn_pointer_arith = (flags >> 1) & 1;
839 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
840 flag_iso = (flags >> 3) & 1;
841 warn_long_long = (flags >> 4) & 1;
842 cpp_opts->warn_long_long = (flags >> 5) & 1;
845 /* Possibly kinds of declarator to parse. */
846 typedef enum c_dtr_syn {
847 /* A normal declarator with an identifier. */
848 C_DTR_NORMAL,
849 /* An abstract declarator (maybe empty). */
850 C_DTR_ABSTRACT,
851 /* A parameter declarator: may be either, but after a type name does
852 not redeclare a typedef name as an identifier if it can
853 alternatively be interpreted as a typedef name; see DR#009,
854 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
855 following DR#249. For example, given a typedef T, "int T" and
856 "int *T" are valid parameter declarations redeclaring T, while
857 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
858 abstract declarators rather than involving redundant parentheses;
859 the same applies with attributes inside the parentheses before
860 "T". */
861 C_DTR_PARM
862 } c_dtr_syn;
864 static void c_parser_external_declaration (c_parser *);
865 static void c_parser_asm_definition (c_parser *);
866 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
867 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
868 bool);
869 static struct c_typespec c_parser_enum_specifier (c_parser *);
870 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
871 static tree c_parser_struct_declaration (c_parser *);
872 static struct c_typespec c_parser_typeof_specifier (c_parser *);
873 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
874 bool *);
875 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
876 c_dtr_syn, bool *);
877 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
878 bool,
879 struct c_declarator *);
880 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
881 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
882 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
883 static tree c_parser_simple_asm_expr (c_parser *);
884 static tree c_parser_attributes (c_parser *);
885 static struct c_type_name *c_parser_type_name (c_parser *);
886 static struct c_expr c_parser_initializer (c_parser *);
887 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
888 static void c_parser_initelt (c_parser *);
889 static void c_parser_initval (c_parser *, struct c_expr *);
890 static tree c_parser_compound_statement (c_parser *);
891 static void c_parser_compound_statement_nostart (c_parser *);
892 static void c_parser_label (c_parser *);
893 static void c_parser_statement (c_parser *);
894 static void c_parser_statement_after_labels (c_parser *);
895 static void c_parser_if_statement (c_parser *);
896 static void c_parser_switch_statement (c_parser *);
897 static void c_parser_while_statement (c_parser *);
898 static void c_parser_do_statement (c_parser *);
899 static void c_parser_for_statement (c_parser *);
900 static tree c_parser_asm_statement (c_parser *);
901 static tree c_parser_asm_operands (c_parser *, bool);
902 static tree c_parser_asm_clobbers (c_parser *);
903 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
904 static struct c_expr c_parser_conditional_expression (c_parser *,
905 struct c_expr *);
906 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
907 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
908 static struct c_expr c_parser_unary_expression (c_parser *);
909 static struct c_expr c_parser_sizeof_expression (c_parser *);
910 static struct c_expr c_parser_alignof_expression (c_parser *);
911 static struct c_expr c_parser_postfix_expression (c_parser *);
912 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
913 struct c_type_name *);
914 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
915 struct c_expr);
916 static struct c_expr c_parser_expression (c_parser *);
917 static struct c_expr c_parser_expression_conv (c_parser *);
918 static tree c_parser_expr_list (c_parser *, bool);
919 static void c_parser_omp_construct (c_parser *);
920 static void c_parser_omp_threadprivate (c_parser *);
921 static void c_parser_omp_barrier (c_parser *);
922 static void c_parser_omp_flush (c_parser *);
923 static void c_parser_omp_taskwait (c_parser *);
925 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
926 static bool c_parser_pragma (c_parser *, enum pragma_context);
928 /* These Objective-C parser functions are only ever called when
929 compiling Objective-C. */
930 static void c_parser_objc_class_definition (c_parser *);
931 static void c_parser_objc_class_instance_variables (c_parser *);
932 static void c_parser_objc_class_declaration (c_parser *);
933 static void c_parser_objc_alias_declaration (c_parser *);
934 static void c_parser_objc_protocol_definition (c_parser *);
935 static enum tree_code c_parser_objc_method_type (c_parser *);
936 static void c_parser_objc_method_definition (c_parser *);
937 static void c_parser_objc_methodprotolist (c_parser *);
938 static void c_parser_objc_methodproto (c_parser *);
939 static tree c_parser_objc_method_decl (c_parser *);
940 static tree c_parser_objc_type_name (c_parser *);
941 static tree c_parser_objc_protocol_refs (c_parser *);
942 static void c_parser_objc_try_catch_statement (c_parser *);
943 static void c_parser_objc_synchronized_statement (c_parser *);
944 static tree c_parser_objc_selector (c_parser *);
945 static tree c_parser_objc_selector_arg (c_parser *);
946 static tree c_parser_objc_receiver (c_parser *);
947 static tree c_parser_objc_message_args (c_parser *);
948 static tree c_parser_objc_keywordexpr (c_parser *);
950 /* Parse a translation unit (C90 6.7, C99 6.9).
952 translation-unit:
953 external-declarations
955 external-declarations:
956 external-declaration
957 external-declarations external-declaration
959 GNU extensions:
961 translation-unit:
962 empty
965 static void
966 c_parser_translation_unit (c_parser *parser)
968 if (c_parser_next_token_is (parser, CPP_EOF))
970 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
971 "ISO C forbids an empty translation unit");
973 else
975 void *obstack_position = obstack_alloc (&parser_obstack, 0);
978 ggc_collect ();
979 c_parser_external_declaration (parser);
980 obstack_free (&parser_obstack, obstack_position);
982 while (c_parser_next_token_is_not (parser, CPP_EOF));
986 /* Parse an external declaration (C90 6.7, C99 6.9).
988 external-declaration:
989 function-definition
990 declaration
992 GNU extensions:
994 external-declaration:
995 asm-definition
997 __extension__ external-declaration
999 Objective-C:
1001 external-declaration:
1002 objc-class-definition
1003 objc-class-declaration
1004 objc-alias-declaration
1005 objc-protocol-definition
1006 objc-method-definition
1007 @end
1010 static void
1011 c_parser_external_declaration (c_parser *parser)
1013 int ext;
1014 switch (c_parser_peek_token (parser)->type)
1016 case CPP_KEYWORD:
1017 switch (c_parser_peek_token (parser)->keyword)
1019 case RID_EXTENSION:
1020 ext = disable_extension_diagnostics ();
1021 c_parser_consume_token (parser);
1022 c_parser_external_declaration (parser);
1023 restore_extension_diagnostics (ext);
1024 break;
1025 case RID_ASM:
1026 c_parser_asm_definition (parser);
1027 break;
1028 case RID_AT_INTERFACE:
1029 case RID_AT_IMPLEMENTATION:
1030 gcc_assert (c_dialect_objc ());
1031 c_parser_objc_class_definition (parser);
1032 break;
1033 case RID_CLASS:
1034 gcc_assert (c_dialect_objc ());
1035 c_parser_objc_class_declaration (parser);
1036 break;
1037 case RID_AT_ALIAS:
1038 gcc_assert (c_dialect_objc ());
1039 c_parser_objc_alias_declaration (parser);
1040 break;
1041 case RID_AT_PROTOCOL:
1042 gcc_assert (c_dialect_objc ());
1043 c_parser_objc_protocol_definition (parser);
1044 break;
1045 case RID_AT_END:
1046 gcc_assert (c_dialect_objc ());
1047 c_parser_consume_token (parser);
1048 objc_finish_implementation ();
1049 break;
1050 default:
1051 goto decl_or_fndef;
1053 break;
1054 case CPP_SEMICOLON:
1055 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1056 "ISO C does not allow extra %<;%> outside of a function");
1057 c_parser_consume_token (parser);
1058 break;
1059 case CPP_PRAGMA:
1060 c_parser_pragma (parser, pragma_external);
1061 break;
1062 case CPP_PLUS:
1063 case CPP_MINUS:
1064 if (c_dialect_objc ())
1066 c_parser_objc_method_definition (parser);
1067 break;
1069 /* Else fall through, and yield a syntax error trying to parse
1070 as a declaration or function definition. */
1071 default:
1072 decl_or_fndef:
1073 /* A declaration or a function definition. We can only tell
1074 which after parsing the declaration specifiers, if any, and
1075 the first declarator. */
1076 c_parser_declaration_or_fndef (parser, true, true, false, true);
1077 break;
1082 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1083 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1084 accepted; otherwise (old-style parameter declarations) only other
1085 declarations are accepted. If NESTED is true, we are inside a
1086 function or parsing old-style parameter declarations; any functions
1087 encountered are nested functions and declaration specifiers are
1088 required; otherwise we are at top level and functions are normal
1089 functions and declaration specifiers may be optional. If EMPTY_OK
1090 is true, empty declarations are OK (subject to all other
1091 constraints); otherwise (old-style parameter declarations) they are
1092 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1093 may start with attributes; otherwise they may not.
1095 declaration:
1096 declaration-specifiers init-declarator-list[opt] ;
1098 function-definition:
1099 declaration-specifiers[opt] declarator declaration-list[opt]
1100 compound-statement
1102 declaration-list:
1103 declaration
1104 declaration-list declaration
1106 init-declarator-list:
1107 init-declarator
1108 init-declarator-list , init-declarator
1110 init-declarator:
1111 declarator simple-asm-expr[opt] attributes[opt]
1112 declarator simple-asm-expr[opt] attributes[opt] = initializer
1114 GNU extensions:
1116 nested-function-definition:
1117 declaration-specifiers declarator declaration-list[opt]
1118 compound-statement
1120 The simple-asm-expr and attributes are GNU extensions.
1122 This function does not handle __extension__; that is handled in its
1123 callers. ??? Following the old parser, __extension__ may start
1124 external declarations, declarations in functions and declarations
1125 at the start of "for" loops, but not old-style parameter
1126 declarations.
1128 C99 requires declaration specifiers in a function definition; the
1129 absence is diagnosed through the diagnosis of implicit int. In GNU
1130 C we also allow but diagnose declarations without declaration
1131 specifiers, but only at top level (elsewhere they conflict with
1132 other syntax).
1134 OpenMP:
1136 declaration:
1137 threadprivate-directive */
1139 static void
1140 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1141 bool nested, bool start_attr_ok)
1143 struct c_declspecs *specs;
1144 tree prefix_attrs;
1145 tree all_prefix_attrs;
1146 bool diagnosed_no_specs = false;
1147 location_t here = c_parser_peek_token (parser)->location;
1149 specs = build_null_declspecs ();
1150 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1151 if (parser->error)
1153 c_parser_skip_to_end_of_block_or_statement (parser);
1154 return;
1156 if (nested && !specs->declspecs_seen_p)
1158 c_parser_error (parser, "expected declaration specifiers");
1159 c_parser_skip_to_end_of_block_or_statement (parser);
1160 return;
1162 finish_declspecs (specs);
1163 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1165 if (empty_ok)
1166 shadow_tag (specs);
1167 else
1169 shadow_tag_warned (specs, 1);
1170 pedwarn (here, 0, "empty declaration");
1172 c_parser_consume_token (parser);
1173 return;
1175 pending_xref_error ();
1176 prefix_attrs = specs->attrs;
1177 all_prefix_attrs = prefix_attrs;
1178 specs->attrs = NULL_TREE;
1179 while (true)
1181 struct c_declarator *declarator;
1182 bool dummy = false;
1183 tree fnbody;
1184 /* Declaring either one or more declarators (in which case we
1185 should diagnose if there were no declaration specifiers) or a
1186 function definition (in which case the diagnostic for
1187 implicit int suffices). */
1188 declarator = c_parser_declarator (parser, specs->type_seen_p,
1189 C_DTR_NORMAL, &dummy);
1190 if (declarator == NULL)
1192 c_parser_skip_to_end_of_block_or_statement (parser);
1193 return;
1195 if (c_parser_next_token_is (parser, CPP_EQ)
1196 || c_parser_next_token_is (parser, CPP_COMMA)
1197 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1198 || c_parser_next_token_is_keyword (parser, RID_ASM)
1199 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1201 tree asm_name = NULL_TREE;
1202 tree postfix_attrs = NULL_TREE;
1203 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1205 diagnosed_no_specs = true;
1206 pedwarn (here, 0, "data definition has no type or storage class");
1208 /* Having seen a data definition, there cannot now be a
1209 function definition. */
1210 fndef_ok = false;
1211 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1212 asm_name = c_parser_simple_asm_expr (parser);
1213 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1214 postfix_attrs = c_parser_attributes (parser);
1215 if (c_parser_next_token_is (parser, CPP_EQ))
1217 tree d;
1218 struct c_expr init;
1219 c_parser_consume_token (parser);
1220 /* The declaration of the variable is in effect while
1221 its initializer is parsed. */
1222 d = start_decl (declarator, specs, true,
1223 chainon (postfix_attrs, all_prefix_attrs));
1224 if (!d)
1225 d = error_mark_node;
1226 start_init (d, asm_name, global_bindings_p ());
1227 init = c_parser_initializer (parser);
1228 finish_init ();
1229 if (d != error_mark_node)
1231 maybe_warn_string_init (TREE_TYPE (d), init);
1232 finish_decl (d, init.value, asm_name);
1235 else
1237 tree d = start_decl (declarator, specs, false,
1238 chainon (postfix_attrs,
1239 all_prefix_attrs));
1240 if (d)
1241 finish_decl (d, NULL_TREE, asm_name);
1243 if (c_parser_next_token_is (parser, CPP_COMMA))
1245 c_parser_consume_token (parser);
1246 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1247 all_prefix_attrs = chainon (c_parser_attributes (parser),
1248 prefix_attrs);
1249 else
1250 all_prefix_attrs = prefix_attrs;
1251 continue;
1253 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1255 c_parser_consume_token (parser);
1256 return;
1258 else
1260 c_parser_error (parser, "expected %<,%> or %<;%>");
1261 c_parser_skip_to_end_of_block_or_statement (parser);
1262 return;
1265 else if (!fndef_ok)
1267 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1268 "%<asm%> or %<__attribute__%>");
1269 c_parser_skip_to_end_of_block_or_statement (parser);
1270 return;
1272 /* Function definition (nested or otherwise). */
1273 if (nested)
1275 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1276 c_push_function_context ();
1278 if (!start_function (specs, declarator, all_prefix_attrs))
1280 /* This can appear in many cases looking nothing like a
1281 function definition, so we don't give a more specific
1282 error suggesting there was one. */
1283 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1284 "or %<__attribute__%>");
1285 if (nested)
1286 c_pop_function_context ();
1287 break;
1289 /* Parse old-style parameter declarations. ??? Attributes are
1290 not allowed to start declaration specifiers here because of a
1291 syntax conflict between a function declaration with attribute
1292 suffix and a function definition with an attribute prefix on
1293 first old-style parameter declaration. Following the old
1294 parser, they are not accepted on subsequent old-style
1295 parameter declarations either. However, there is no
1296 ambiguity after the first declaration, nor indeed on the
1297 first as long as we don't allow postfix attributes after a
1298 declarator with a nonempty identifier list in a definition;
1299 and postfix attributes have never been accepted here in
1300 function definitions either. */
1301 while (c_parser_next_token_is_not (parser, CPP_EOF)
1302 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1303 c_parser_declaration_or_fndef (parser, false, false, true, false);
1304 store_parm_decls ();
1305 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1306 = c_parser_peek_token (parser)->location;
1307 fnbody = c_parser_compound_statement (parser);
1308 if (nested)
1310 tree decl = current_function_decl;
1311 add_stmt (fnbody);
1312 finish_function ();
1313 c_pop_function_context ();
1314 add_stmt (build_stmt (DECL_EXPR, decl));
1316 else
1318 add_stmt (fnbody);
1319 finish_function ();
1321 break;
1325 /* Parse an asm-definition (asm() outside a function body). This is a
1326 GNU extension.
1328 asm-definition:
1329 simple-asm-expr ;
1332 static void
1333 c_parser_asm_definition (c_parser *parser)
1335 tree asm_str = c_parser_simple_asm_expr (parser);
1336 if (asm_str)
1337 cgraph_add_asm_node (asm_str);
1338 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1341 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1342 6.7), adding them to SPECS (which may already include some).
1343 Storage class specifiers are accepted iff SCSPEC_OK; type
1344 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1345 the start iff START_ATTR_OK.
1347 declaration-specifiers:
1348 storage-class-specifier declaration-specifiers[opt]
1349 type-specifier declaration-specifiers[opt]
1350 type-qualifier declaration-specifiers[opt]
1351 function-specifier declaration-specifiers[opt]
1353 Function specifiers (inline) are from C99, and are currently
1354 handled as storage class specifiers, as is __thread.
1356 C90 6.5.1, C99 6.7.1:
1357 storage-class-specifier:
1358 typedef
1359 extern
1360 static
1361 auto
1362 register
1364 C99 6.7.4:
1365 function-specifier:
1366 inline
1368 C90 6.5.2, C99 6.7.2:
1369 type-specifier:
1370 void
1371 char
1372 short
1374 long
1375 float
1376 double
1377 signed
1378 unsigned
1379 _Bool
1380 _Complex
1381 [_Imaginary removed in C99 TC2]
1382 struct-or-union-specifier
1383 enum-specifier
1384 typedef-name
1386 (_Bool and _Complex are new in C99.)
1388 C90 6.5.3, C99 6.7.3:
1390 type-qualifier:
1391 const
1392 restrict
1393 volatile
1395 (restrict is new in C99.)
1397 GNU extensions:
1399 declaration-specifiers:
1400 attributes declaration-specifiers[opt]
1402 storage-class-specifier:
1403 __thread
1405 type-specifier:
1406 typeof-specifier
1407 _Decimal32
1408 _Decimal64
1409 _Decimal128
1410 _Fract
1411 _Accum
1412 _Sat
1414 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1415 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1417 Objective-C:
1419 type-specifier:
1420 class-name objc-protocol-refs[opt]
1421 typedef-name objc-protocol-refs
1422 objc-protocol-refs
1425 static void
1426 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1427 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1429 bool attrs_ok = start_attr_ok;
1430 bool seen_type = specs->type_seen_p;
1431 while (c_parser_next_token_is (parser, CPP_NAME)
1432 || c_parser_next_token_is (parser, CPP_KEYWORD)
1433 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1435 struct c_typespec t;
1436 tree attrs;
1437 if (c_parser_next_token_is (parser, CPP_NAME))
1439 tree value = c_parser_peek_token (parser)->value;
1440 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1441 /* This finishes the specifiers unless a type name is OK, it
1442 is declared as a type name and a type name hasn't yet
1443 been seen. */
1444 if (!typespec_ok || seen_type
1445 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1446 break;
1447 c_parser_consume_token (parser);
1448 seen_type = true;
1449 attrs_ok = true;
1450 if (kind == C_ID_TYPENAME
1451 && (!c_dialect_objc ()
1452 || c_parser_next_token_is_not (parser, CPP_LESS)))
1454 t.kind = ctsk_typedef;
1455 /* For a typedef name, record the meaning, not the name.
1456 In case of 'foo foo, bar;'. */
1457 t.spec = lookup_name (value);
1459 else
1461 tree proto = NULL_TREE;
1462 gcc_assert (c_dialect_objc ());
1463 t.kind = ctsk_objc;
1464 if (c_parser_next_token_is (parser, CPP_LESS))
1465 proto = c_parser_objc_protocol_refs (parser);
1466 t.spec = objc_get_protocol_qualified_type (value, proto);
1468 declspecs_add_type (specs, t);
1469 continue;
1471 if (c_parser_next_token_is (parser, CPP_LESS))
1473 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1474 nisse@lysator.liu.se. */
1475 tree proto;
1476 gcc_assert (c_dialect_objc ());
1477 if (!typespec_ok || seen_type)
1478 break;
1479 proto = c_parser_objc_protocol_refs (parser);
1480 t.kind = ctsk_objc;
1481 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1482 declspecs_add_type (specs, t);
1483 continue;
1485 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1486 switch (c_parser_peek_token (parser)->keyword)
1488 case RID_STATIC:
1489 case RID_EXTERN:
1490 case RID_REGISTER:
1491 case RID_TYPEDEF:
1492 case RID_INLINE:
1493 case RID_AUTO:
1494 case RID_THREAD:
1495 if (!scspec_ok)
1496 goto out;
1497 attrs_ok = true;
1498 /* TODO: Distinguish between function specifiers (inline)
1499 and storage class specifiers, either here or in
1500 declspecs_add_scspec. */
1501 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1502 c_parser_consume_token (parser);
1503 break;
1504 case RID_UNSIGNED:
1505 case RID_LONG:
1506 case RID_SHORT:
1507 case RID_SIGNED:
1508 case RID_COMPLEX:
1509 case RID_INT:
1510 case RID_CHAR:
1511 case RID_FLOAT:
1512 case RID_DOUBLE:
1513 case RID_VOID:
1514 case RID_DFLOAT32:
1515 case RID_DFLOAT64:
1516 case RID_DFLOAT128:
1517 case RID_BOOL:
1518 case RID_FRACT:
1519 case RID_ACCUM:
1520 case RID_SAT:
1521 if (!typespec_ok)
1522 goto out;
1523 attrs_ok = true;
1524 seen_type = true;
1525 if (c_dialect_objc ())
1526 parser->objc_need_raw_identifier = true;
1527 t.kind = ctsk_resword;
1528 t.spec = c_parser_peek_token (parser)->value;
1529 declspecs_add_type (specs, t);
1530 c_parser_consume_token (parser);
1531 break;
1532 case RID_ENUM:
1533 if (!typespec_ok)
1534 goto out;
1535 attrs_ok = true;
1536 seen_type = true;
1537 t = c_parser_enum_specifier (parser);
1538 declspecs_add_type (specs, t);
1539 break;
1540 case RID_STRUCT:
1541 case RID_UNION:
1542 if (!typespec_ok)
1543 goto out;
1544 attrs_ok = true;
1545 seen_type = true;
1546 t = c_parser_struct_or_union_specifier (parser);
1547 declspecs_add_type (specs, t);
1548 break;
1549 case RID_TYPEOF:
1550 /* ??? The old parser rejected typeof after other type
1551 specifiers, but is a syntax error the best way of
1552 handling this? */
1553 if (!typespec_ok || seen_type)
1554 goto out;
1555 attrs_ok = true;
1556 seen_type = true;
1557 t = c_parser_typeof_specifier (parser);
1558 declspecs_add_type (specs, t);
1559 break;
1560 case RID_CONST:
1561 case RID_VOLATILE:
1562 case RID_RESTRICT:
1563 attrs_ok = true;
1564 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1565 c_parser_consume_token (parser);
1566 break;
1567 case RID_ATTRIBUTE:
1568 if (!attrs_ok)
1569 goto out;
1570 attrs = c_parser_attributes (parser);
1571 declspecs_add_attrs (specs, attrs);
1572 break;
1573 default:
1574 goto out;
1577 out: ;
1580 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1582 enum-specifier:
1583 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1584 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1585 enum attributes[opt] identifier
1587 The form with trailing comma is new in C99. The forms with
1588 attributes are GNU extensions. In GNU C, we accept any expression
1589 without commas in the syntax (assignment expressions, not just
1590 conditional expressions); assignment expressions will be diagnosed
1591 as non-constant.
1593 enumerator-list:
1594 enumerator
1595 enumerator-list , enumerator
1597 enumerator:
1598 enumeration-constant
1599 enumeration-constant = constant-expression
1602 static struct c_typespec
1603 c_parser_enum_specifier (c_parser *parser)
1605 struct c_typespec ret;
1606 tree attrs;
1607 tree ident = NULL_TREE;
1608 location_t ident_loc;
1609 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1610 c_parser_consume_token (parser);
1611 attrs = c_parser_attributes (parser);
1612 /* Set the location in case we create a decl now. */
1613 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1614 if (c_parser_next_token_is (parser, CPP_NAME))
1616 ident = c_parser_peek_token (parser)->value;
1617 ident_loc = c_parser_peek_token (parser)->location;
1618 c_parser_consume_token (parser);
1620 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1622 /* Parse an enum definition. */
1623 struct c_enum_contents the_enum;
1624 tree type = start_enum (&the_enum, ident);
1625 tree postfix_attrs;
1626 /* We chain the enumerators in reverse order, then put them in
1627 forward order at the end. */
1628 tree values = NULL_TREE;
1629 c_parser_consume_token (parser);
1630 while (true)
1632 tree enum_id;
1633 tree enum_value;
1634 tree enum_decl;
1635 bool seen_comma;
1636 c_token *token;
1637 location_t comma_loc;
1638 location_t value_loc;
1639 if (c_parser_next_token_is_not (parser, CPP_NAME))
1641 c_parser_error (parser, "expected identifier");
1642 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1643 values = error_mark_node;
1644 break;
1646 token = c_parser_peek_token (parser);
1647 enum_id = token->value;
1648 /* Set the location in case we create a decl now. */
1649 c_parser_set_source_position_from_token (token);
1650 value_loc = token->location;
1651 c_parser_consume_token (parser);
1652 if (c_parser_next_token_is (parser, CPP_EQ))
1654 c_parser_consume_token (parser);
1655 value_loc = c_parser_peek_token (parser)->location;
1656 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1658 else
1659 enum_value = NULL_TREE;
1660 enum_decl = build_enumerator (&the_enum, enum_id, enum_value,
1661 value_loc);
1662 TREE_CHAIN (enum_decl) = values;
1663 values = enum_decl;
1664 seen_comma = false;
1665 if (c_parser_next_token_is (parser, CPP_COMMA))
1667 comma_loc = c_parser_peek_token (parser)->location;
1668 seen_comma = true;
1669 c_parser_consume_token (parser);
1671 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1673 if (seen_comma && !flag_isoc99)
1674 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1675 c_parser_consume_token (parser);
1676 break;
1678 if (!seen_comma)
1680 c_parser_error (parser, "expected %<,%> or %<}%>");
1681 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1682 values = error_mark_node;
1683 break;
1686 postfix_attrs = c_parser_attributes (parser);
1687 ret.spec = finish_enum (type, nreverse (values),
1688 chainon (attrs, postfix_attrs));
1689 ret.kind = ctsk_tagdef;
1690 return ret;
1692 else if (!ident)
1694 c_parser_error (parser, "expected %<{%>");
1695 ret.spec = error_mark_node;
1696 ret.kind = ctsk_tagref;
1697 return ret;
1699 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1700 /* In ISO C, enumerated types can be referred to only if already
1701 defined. */
1702 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1704 gcc_assert (ident);
1705 pedwarn (ident_loc, OPT_pedantic,
1706 "ISO C forbids forward references to %<enum%> types");
1708 return ret;
1711 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1713 struct-or-union-specifier:
1714 struct-or-union attributes[opt] identifier[opt]
1715 { struct-contents } attributes[opt]
1716 struct-or-union attributes[opt] identifier
1718 struct-contents:
1719 struct-declaration-list
1721 struct-declaration-list:
1722 struct-declaration ;
1723 struct-declaration-list struct-declaration ;
1725 GNU extensions:
1727 struct-contents:
1728 empty
1729 struct-declaration
1730 struct-declaration-list struct-declaration
1732 struct-declaration-list:
1733 struct-declaration-list ;
1736 (Note that in the syntax here, unlike that in ISO C, the semicolons
1737 are included here rather than in struct-declaration, in order to
1738 describe the syntax with extra semicolons and missing semicolon at
1739 end.)
1741 Objective-C:
1743 struct-declaration-list:
1744 @defs ( class-name )
1746 (Note this does not include a trailing semicolon, but can be
1747 followed by further declarations, and gets a pedwarn-if-pedantic
1748 when followed by a semicolon.) */
1750 static struct c_typespec
1751 c_parser_struct_or_union_specifier (c_parser *parser)
1753 struct c_typespec ret;
1754 tree attrs;
1755 tree ident = NULL_TREE;
1756 enum tree_code code;
1757 switch (c_parser_peek_token (parser)->keyword)
1759 case RID_STRUCT:
1760 code = RECORD_TYPE;
1761 break;
1762 case RID_UNION:
1763 code = UNION_TYPE;
1764 break;
1765 default:
1766 gcc_unreachable ();
1768 c_parser_consume_token (parser);
1769 attrs = c_parser_attributes (parser);
1770 /* Set the location in case we create a decl now. */
1771 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1772 if (c_parser_next_token_is (parser, CPP_NAME))
1774 ident = c_parser_peek_token (parser)->value;
1775 c_parser_consume_token (parser);
1777 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1779 /* Parse a struct or union definition. Start the scope of the
1780 tag before parsing components. */
1781 tree type = start_struct (code, ident);
1782 tree postfix_attrs;
1783 /* We chain the components in reverse order, then put them in
1784 forward order at the end. Each struct-declaration may
1785 declare multiple components (comma-separated), so we must use
1786 chainon to join them, although when parsing each
1787 struct-declaration we can use TREE_CHAIN directly.
1789 The theory behind all this is that there will be more
1790 semicolon separated fields than comma separated fields, and
1791 so we'll be minimizing the number of node traversals required
1792 by chainon. */
1793 tree contents = NULL_TREE;
1794 c_parser_consume_token (parser);
1795 /* Handle the Objective-C @defs construct,
1796 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1797 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1799 tree name;
1800 gcc_assert (c_dialect_objc ());
1801 c_parser_consume_token (parser);
1802 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1803 goto end_at_defs;
1804 if (c_parser_next_token_is (parser, CPP_NAME)
1805 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1807 name = c_parser_peek_token (parser)->value;
1808 c_parser_consume_token (parser);
1810 else
1812 c_parser_error (parser, "expected class name");
1813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1814 goto end_at_defs;
1816 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1817 "expected %<)%>");
1818 contents = nreverse (objc_get_class_ivars (name));
1820 end_at_defs:
1821 /* Parse the struct-declarations and semicolons. Problems with
1822 semicolons are diagnosed here; empty structures are diagnosed
1823 elsewhere. */
1824 while (true)
1826 tree decls;
1827 /* Parse any stray semicolon. */
1828 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1830 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1831 "extra semicolon in struct or union specified");
1832 c_parser_consume_token (parser);
1833 continue;
1835 /* Stop if at the end of the struct or union contents. */
1836 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1838 c_parser_consume_token (parser);
1839 break;
1841 /* Accept #pragmas at struct scope. */
1842 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1844 c_parser_pragma (parser, pragma_external);
1845 continue;
1847 /* Parse some comma-separated declarations, but not the
1848 trailing semicolon if any. */
1849 decls = c_parser_struct_declaration (parser);
1850 contents = chainon (decls, contents);
1851 /* If no semicolon follows, either we have a parse error or
1852 are at the end of the struct or union and should
1853 pedwarn. */
1854 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1855 c_parser_consume_token (parser);
1856 else
1858 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1859 pedwarn (c_parser_peek_token (parser)->location, 0,
1860 "no semicolon at end of struct or union");
1861 else
1863 c_parser_error (parser, "expected %<;%>");
1864 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1865 break;
1869 postfix_attrs = c_parser_attributes (parser);
1870 ret.spec = finish_struct (type, nreverse (contents),
1871 chainon (attrs, postfix_attrs));
1872 ret.kind = ctsk_tagdef;
1873 return ret;
1875 else if (!ident)
1877 c_parser_error (parser, "expected %<{%>");
1878 ret.spec = error_mark_node;
1879 ret.kind = ctsk_tagref;
1880 return ret;
1882 ret = parser_xref_tag (code, ident);
1883 return ret;
1886 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1887 the trailing semicolon.
1889 struct-declaration:
1890 specifier-qualifier-list struct-declarator-list
1892 specifier-qualifier-list:
1893 type-specifier specifier-qualifier-list[opt]
1894 type-qualifier specifier-qualifier-list[opt]
1895 attributes specifier-qualifier-list[opt]
1897 struct-declarator-list:
1898 struct-declarator
1899 struct-declarator-list , attributes[opt] struct-declarator
1901 struct-declarator:
1902 declarator attributes[opt]
1903 declarator[opt] : constant-expression attributes[opt]
1905 GNU extensions:
1907 struct-declaration:
1908 __extension__ struct-declaration
1909 specifier-qualifier-list
1911 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1912 of attributes where shown is a GNU extension. In GNU C, we accept
1913 any expression without commas in the syntax (assignment
1914 expressions, not just conditional expressions); assignment
1915 expressions will be diagnosed as non-constant. */
1917 static tree
1918 c_parser_struct_declaration (c_parser *parser)
1920 struct c_declspecs *specs;
1921 tree prefix_attrs;
1922 tree all_prefix_attrs;
1923 tree decls;
1924 location_t decl_loc;
1925 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1927 int ext;
1928 tree decl;
1929 ext = disable_extension_diagnostics ();
1930 c_parser_consume_token (parser);
1931 decl = c_parser_struct_declaration (parser);
1932 restore_extension_diagnostics (ext);
1933 return decl;
1935 specs = build_null_declspecs ();
1936 decl_loc = c_parser_peek_token (parser)->location;
1937 c_parser_declspecs (parser, specs, false, true, true);
1938 if (parser->error)
1939 return NULL_TREE;
1940 if (!specs->declspecs_seen_p)
1942 c_parser_error (parser, "expected specifier-qualifier-list");
1943 return NULL_TREE;
1945 finish_declspecs (specs);
1946 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1948 tree ret;
1949 if (!specs->type_seen_p)
1951 pedwarn (decl_loc, OPT_pedantic,
1952 "ISO C forbids member declarations with no members");
1953 shadow_tag_warned (specs, pedantic);
1954 ret = NULL_TREE;
1956 else
1958 /* Support for unnamed structs or unions as members of
1959 structs or unions (which is [a] useful and [b] supports
1960 MS P-SDK). */
1961 tree attrs = NULL;
1963 ret = grokfield (c_parser_peek_token (parser)->location,
1964 build_id_declarator (NULL_TREE), specs,
1965 NULL_TREE, &attrs);
1966 if (ret)
1967 decl_attributes (&ret, attrs, 0);
1969 return ret;
1971 pending_xref_error ();
1972 prefix_attrs = specs->attrs;
1973 all_prefix_attrs = prefix_attrs;
1974 specs->attrs = NULL_TREE;
1975 decls = NULL_TREE;
1976 while (true)
1978 /* Declaring one or more declarators or un-named bit-fields. */
1979 struct c_declarator *declarator;
1980 bool dummy = false;
1981 if (c_parser_next_token_is (parser, CPP_COLON))
1982 declarator = build_id_declarator (NULL_TREE);
1983 else
1984 declarator = c_parser_declarator (parser, specs->type_seen_p,
1985 C_DTR_NORMAL, &dummy);
1986 if (declarator == NULL)
1988 c_parser_skip_to_end_of_block_or_statement (parser);
1989 break;
1991 if (c_parser_next_token_is (parser, CPP_COLON)
1992 || c_parser_next_token_is (parser, CPP_COMMA)
1993 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1994 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1995 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1997 tree postfix_attrs = NULL_TREE;
1998 tree width = NULL_TREE;
1999 tree d;
2000 if (c_parser_next_token_is (parser, CPP_COLON))
2002 c_parser_consume_token (parser);
2003 width = c_parser_expr_no_commas (parser, NULL).value;
2005 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2006 postfix_attrs = c_parser_attributes (parser);
2007 d = grokfield (c_parser_peek_token (parser)->location,
2008 declarator, specs, width, &all_prefix_attrs);
2009 decl_attributes (&d, chainon (postfix_attrs,
2010 all_prefix_attrs), 0);
2011 TREE_CHAIN (d) = decls;
2012 decls = d;
2013 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2014 all_prefix_attrs = chainon (c_parser_attributes (parser),
2015 prefix_attrs);
2016 else
2017 all_prefix_attrs = prefix_attrs;
2018 if (c_parser_next_token_is (parser, CPP_COMMA))
2019 c_parser_consume_token (parser);
2020 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2021 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2023 /* Semicolon consumed in caller. */
2024 break;
2026 else
2028 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2029 break;
2032 else
2034 c_parser_error (parser,
2035 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2036 "%<__attribute__%>");
2037 break;
2040 return decls;
2043 /* Parse a typeof specifier (a GNU extension).
2045 typeof-specifier:
2046 typeof ( expression )
2047 typeof ( type-name )
2050 static struct c_typespec
2051 c_parser_typeof_specifier (c_parser *parser)
2053 struct c_typespec ret;
2054 ret.kind = ctsk_typeof;
2055 ret.spec = error_mark_node;
2056 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2057 c_parser_consume_token (parser);
2058 skip_evaluation++;
2059 in_typeof++;
2060 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2062 skip_evaluation--;
2063 in_typeof--;
2064 return ret;
2066 if (c_parser_next_token_starts_typename (parser))
2068 struct c_type_name *type = c_parser_type_name (parser);
2069 skip_evaluation--;
2070 in_typeof--;
2071 if (type != NULL)
2073 ret.spec = groktypename (type);
2074 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2077 else
2079 bool was_vm;
2080 location_t here = c_parser_peek_token (parser)->location;
2081 struct c_expr expr = c_parser_expression (parser);
2082 skip_evaluation--;
2083 in_typeof--;
2084 if (TREE_CODE (expr.value) == COMPONENT_REF
2085 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2086 error_at (here, "%<typeof%> applied to a bit-field");
2087 ret.spec = TREE_TYPE (expr.value);
2088 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2089 /* This should be returned with the type so that when the type
2090 is evaluated, this can be evaluated. For now, we avoid
2091 evaluation when the context might. */
2092 if (!skip_evaluation && was_vm)
2094 tree e = expr.value;
2096 /* If the expression is not of a type to which we cannot assign a line
2097 number, wrap the thing in a no-op NOP_EXPR. */
2098 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2099 e = build1 (NOP_EXPR, void_type_node, e);
2101 if (CAN_HAVE_LOCATION_P (e))
2102 SET_EXPR_LOCATION (e, here);
2104 add_stmt (e);
2106 pop_maybe_used (was_vm);
2108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2109 return ret;
2112 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2113 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2114 be redeclared; otherwise it may not. KIND indicates which kind of
2115 declarator is wanted. Returns a valid declarator except in the
2116 case of a syntax error in which case NULL is returned. *SEEN_ID is
2117 set to true if an identifier being declared is seen; this is used
2118 to diagnose bad forms of abstract array declarators and to
2119 determine whether an identifier list is syntactically permitted.
2121 declarator:
2122 pointer[opt] direct-declarator
2124 direct-declarator:
2125 identifier
2126 ( attributes[opt] declarator )
2127 direct-declarator array-declarator
2128 direct-declarator ( parameter-type-list )
2129 direct-declarator ( identifier-list[opt] )
2131 pointer:
2132 * type-qualifier-list[opt]
2133 * type-qualifier-list[opt] pointer
2135 type-qualifier-list:
2136 type-qualifier
2137 attributes
2138 type-qualifier-list type-qualifier
2139 type-qualifier-list attributes
2141 parameter-type-list:
2142 parameter-list
2143 parameter-list , ...
2145 parameter-list:
2146 parameter-declaration
2147 parameter-list , parameter-declaration
2149 parameter-declaration:
2150 declaration-specifiers declarator attributes[opt]
2151 declaration-specifiers abstract-declarator[opt] attributes[opt]
2153 identifier-list:
2154 identifier
2155 identifier-list , identifier
2157 abstract-declarator:
2158 pointer
2159 pointer[opt] direct-abstract-declarator
2161 direct-abstract-declarator:
2162 ( attributes[opt] abstract-declarator )
2163 direct-abstract-declarator[opt] array-declarator
2164 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2166 GNU extensions:
2168 direct-declarator:
2169 direct-declarator ( parameter-forward-declarations
2170 parameter-type-list[opt] )
2172 direct-abstract-declarator:
2173 direct-abstract-declarator[opt] ( parameter-forward-declarations
2174 parameter-type-list[opt] )
2176 parameter-forward-declarations:
2177 parameter-list ;
2178 parameter-forward-declarations parameter-list ;
2180 The uses of attributes shown above are GNU extensions.
2182 Some forms of array declarator are not included in C99 in the
2183 syntax for abstract declarators; these are disallowed elsewhere.
2184 This may be a defect (DR#289).
2186 This function also accepts an omitted abstract declarator as being
2187 an abstract declarator, although not part of the formal syntax. */
2189 static struct c_declarator *
2190 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2191 bool *seen_id)
2193 /* Parse any initial pointer part. */
2194 if (c_parser_next_token_is (parser, CPP_MULT))
2196 struct c_declspecs *quals_attrs = build_null_declspecs ();
2197 struct c_declarator *inner;
2198 c_parser_consume_token (parser);
2199 c_parser_declspecs (parser, quals_attrs, false, false, true);
2200 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2201 if (inner == NULL)
2202 return NULL;
2203 else
2204 return make_pointer_declarator (quals_attrs, inner);
2206 /* Now we have a direct declarator, direct abstract declarator or
2207 nothing (which counts as a direct abstract declarator here). */
2208 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2211 /* Parse a direct declarator or direct abstract declarator; arguments
2212 as c_parser_declarator. */
2214 static struct c_declarator *
2215 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2216 bool *seen_id)
2218 /* The direct declarator must start with an identifier (possibly
2219 omitted) or a parenthesized declarator (possibly abstract). In
2220 an ordinary declarator, initial parentheses must start a
2221 parenthesized declarator. In an abstract declarator or parameter
2222 declarator, they could start a parenthesized declarator or a
2223 parameter list. To tell which, the open parenthesis and any
2224 following attributes must be read. If a declaration specifier
2225 follows, then it is a parameter list; if the specifier is a
2226 typedef name, there might be an ambiguity about redeclaring it,
2227 which is resolved in the direction of treating it as a typedef
2228 name. If a close parenthesis follows, it is also an empty
2229 parameter list, as the syntax does not permit empty abstract
2230 declarators. Otherwise, it is a parenthesized declarator (in
2231 which case the analysis may be repeated inside it, recursively).
2233 ??? There is an ambiguity in a parameter declaration "int
2234 (__attribute__((foo)) x)", where x is not a typedef name: it
2235 could be an abstract declarator for a function, or declare x with
2236 parentheses. The proper resolution of this ambiguity needs
2237 documenting. At present we follow an accident of the old
2238 parser's implementation, whereby the first parameter must have
2239 some declaration specifiers other than just attributes. Thus as
2240 a parameter declaration it is treated as a parenthesized
2241 parameter named x, and as an abstract declarator it is
2242 rejected.
2244 ??? Also following the old parser, attributes inside an empty
2245 parameter list are ignored, making it a list not yielding a
2246 prototype, rather than giving an error or making it have one
2247 parameter with implicit type int.
2249 ??? Also following the old parser, typedef names may be
2250 redeclared in declarators, but not Objective-C class names. */
2252 if (kind != C_DTR_ABSTRACT
2253 && c_parser_next_token_is (parser, CPP_NAME)
2254 && ((type_seen_p
2255 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2256 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2258 struct c_declarator *inner
2259 = build_id_declarator (c_parser_peek_token (parser)->value);
2260 *seen_id = true;
2261 inner->id_loc = c_parser_peek_token (parser)->location;
2262 c_parser_consume_token (parser);
2263 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2266 if (kind != C_DTR_NORMAL
2267 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2269 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2270 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2273 /* Either we are at the end of an abstract declarator, or we have
2274 parentheses. */
2276 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2278 tree attrs;
2279 struct c_declarator *inner;
2280 c_parser_consume_token (parser);
2281 attrs = c_parser_attributes (parser);
2282 if (kind != C_DTR_NORMAL
2283 && (c_parser_next_token_starts_declspecs (parser)
2284 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2286 struct c_arg_info *args
2287 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2288 attrs);
2289 if (args == NULL)
2290 return NULL;
2291 else
2293 inner
2294 = build_function_declarator (args,
2295 build_id_declarator (NULL_TREE));
2296 return c_parser_direct_declarator_inner (parser, *seen_id,
2297 inner);
2300 /* A parenthesized declarator. */
2301 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2302 if (inner != NULL && attrs != NULL)
2303 inner = build_attrs_declarator (attrs, inner);
2304 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2306 c_parser_consume_token (parser);
2307 if (inner == NULL)
2308 return NULL;
2309 else
2310 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2312 else
2314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2315 "expected %<)%>");
2316 return NULL;
2319 else
2321 if (kind == C_DTR_NORMAL)
2323 c_parser_error (parser, "expected identifier or %<(%>");
2324 return NULL;
2326 else
2327 return build_id_declarator (NULL_TREE);
2331 /* Parse part of a direct declarator or direct abstract declarator,
2332 given that some (in INNER) has already been parsed; ID_PRESENT is
2333 true if an identifier is present, false for an abstract
2334 declarator. */
2336 static struct c_declarator *
2337 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2338 struct c_declarator *inner)
2340 /* Parse a sequence of array declarators and parameter lists. */
2341 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2343 struct c_declarator *declarator;
2344 struct c_declspecs *quals_attrs = build_null_declspecs ();
2345 bool static_seen;
2346 bool star_seen;
2347 tree dimen;
2348 c_parser_consume_token (parser);
2349 c_parser_declspecs (parser, quals_attrs, false, false, true);
2350 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2351 if (static_seen)
2352 c_parser_consume_token (parser);
2353 if (static_seen && !quals_attrs->declspecs_seen_p)
2354 c_parser_declspecs (parser, quals_attrs, false, false, true);
2355 if (!quals_attrs->declspecs_seen_p)
2356 quals_attrs = NULL;
2357 /* If "static" is present, there must be an array dimension.
2358 Otherwise, there may be a dimension, "*", or no
2359 dimension. */
2360 if (static_seen)
2362 star_seen = false;
2363 dimen = c_parser_expr_no_commas (parser, NULL).value;
2365 else
2367 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2369 dimen = NULL_TREE;
2370 star_seen = false;
2372 else if (c_parser_next_token_is (parser, CPP_MULT))
2374 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2376 dimen = NULL_TREE;
2377 star_seen = true;
2378 c_parser_consume_token (parser);
2380 else
2382 star_seen = false;
2383 dimen = c_parser_expr_no_commas (parser, NULL).value;
2386 else
2388 star_seen = false;
2389 dimen = c_parser_expr_no_commas (parser, NULL).value;
2392 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2393 c_parser_consume_token (parser);
2394 else
2396 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2397 "expected %<]%>");
2398 return NULL;
2400 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2401 star_seen);
2402 if (declarator == NULL)
2403 return NULL;
2404 inner = set_array_declarator_inner (declarator, inner);
2405 return c_parser_direct_declarator_inner (parser, id_present, inner);
2407 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2409 tree attrs;
2410 struct c_arg_info *args;
2411 c_parser_consume_token (parser);
2412 attrs = c_parser_attributes (parser);
2413 args = c_parser_parms_declarator (parser, id_present, attrs);
2414 if (args == NULL)
2415 return NULL;
2416 else
2418 inner = build_function_declarator (args, inner);
2419 return c_parser_direct_declarator_inner (parser, id_present, inner);
2422 return inner;
2425 /* Parse a parameter list or identifier list, including the closing
2426 parenthesis but not the opening one. ATTRS are the attributes at
2427 the start of the list. ID_LIST_OK is true if an identifier list is
2428 acceptable; such a list must not have attributes at the start. */
2430 static struct c_arg_info *
2431 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2433 push_scope ();
2434 declare_parm_level ();
2435 /* If the list starts with an identifier, it is an identifier list.
2436 Otherwise, it is either a prototype list or an empty list. */
2437 if (id_list_ok
2438 && !attrs
2439 && c_parser_next_token_is (parser, CPP_NAME)
2440 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2442 tree list = NULL_TREE, *nextp = &list;
2443 while (c_parser_next_token_is (parser, CPP_NAME)
2444 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2446 *nextp = build_tree_list (NULL_TREE,
2447 c_parser_peek_token (parser)->value);
2448 nextp = & TREE_CHAIN (*nextp);
2449 c_parser_consume_token (parser);
2450 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2451 break;
2452 c_parser_consume_token (parser);
2453 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2455 c_parser_error (parser, "expected identifier");
2456 break;
2459 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2461 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2462 ret->parms = 0;
2463 ret->tags = 0;
2464 ret->types = list;
2465 ret->others = 0;
2466 ret->pending_sizes = 0;
2467 ret->had_vla_unspec = 0;
2468 c_parser_consume_token (parser);
2469 pop_scope ();
2470 return ret;
2472 else
2474 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2475 "expected %<)%>");
2476 pop_scope ();
2477 return NULL;
2480 else
2482 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2483 pop_scope ();
2484 return ret;
2488 /* Parse a parameter list (possibly empty), including the closing
2489 parenthesis but not the opening one. ATTRS are the attributes at
2490 the start of the list. */
2492 static struct c_arg_info *
2493 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2495 bool good_parm = false;
2496 /* ??? Following the old parser, forward parameter declarations may
2497 use abstract declarators, and if no real parameter declarations
2498 follow the forward declarations then this is not diagnosed. Also
2499 note as above that attributes are ignored as the only contents of
2500 the parentheses, or as the only contents after forward
2501 declarations. */
2502 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2504 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2505 ret->parms = 0;
2506 ret->tags = 0;
2507 ret->types = 0;
2508 ret->others = 0;
2509 ret->pending_sizes = 0;
2510 ret->had_vla_unspec = 0;
2511 c_parser_consume_token (parser);
2512 return ret;
2514 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2516 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2517 ret->parms = 0;
2518 ret->tags = 0;
2519 ret->others = 0;
2520 ret->pending_sizes = 0;
2521 ret->had_vla_unspec = 0;
2522 /* Suppress -Wold-style-definition for this case. */
2523 ret->types = error_mark_node;
2524 error_at (c_parser_peek_token (parser)->location,
2525 "ISO C requires a named argument before %<...%>");
2526 c_parser_consume_token (parser);
2527 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2529 c_parser_consume_token (parser);
2530 return ret;
2532 else
2534 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2535 "expected %<)%>");
2536 return NULL;
2539 /* Nonempty list of parameters, either terminated with semicolon
2540 (forward declarations; recurse) or with close parenthesis (normal
2541 function) or with ", ... )" (variadic function). */
2542 while (true)
2544 /* Parse a parameter. */
2545 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2546 attrs = NULL_TREE;
2547 if (parm != NULL)
2549 good_parm = true;
2550 push_parm_decl (parm);
2552 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2554 tree new_attrs;
2555 c_parser_consume_token (parser);
2556 mark_forward_parm_decls ();
2557 new_attrs = c_parser_attributes (parser);
2558 return c_parser_parms_list_declarator (parser, new_attrs);
2560 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2562 c_parser_consume_token (parser);
2563 if (good_parm)
2564 return get_parm_info (false);
2565 else
2567 struct c_arg_info *ret
2568 = XOBNEW (&parser_obstack, struct c_arg_info);
2569 ret->parms = 0;
2570 ret->tags = 0;
2571 ret->types = 0;
2572 ret->others = 0;
2573 ret->pending_sizes = 0;
2574 ret->had_vla_unspec = 0;
2575 return ret;
2578 if (!c_parser_require (parser, CPP_COMMA,
2579 "expected %<;%>, %<,%> or %<)%>"))
2581 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2582 return NULL;
2584 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2586 c_parser_consume_token (parser);
2587 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2589 c_parser_consume_token (parser);
2590 if (good_parm)
2591 return get_parm_info (true);
2592 else
2594 struct c_arg_info *ret
2595 = XOBNEW (&parser_obstack, struct c_arg_info);
2596 ret->parms = 0;
2597 ret->tags = 0;
2598 ret->types = 0;
2599 ret->others = 0;
2600 ret->pending_sizes = 0;
2601 ret->had_vla_unspec = 0;
2602 return ret;
2605 else
2607 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2608 "expected %<)%>");
2609 return NULL;
2615 /* Parse a parameter declaration. ATTRS are the attributes at the
2616 start of the declaration if it is the first parameter. */
2618 static struct c_parm *
2619 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2621 struct c_declspecs *specs;
2622 struct c_declarator *declarator;
2623 tree prefix_attrs;
2624 tree postfix_attrs = NULL_TREE;
2625 bool dummy = false;
2626 if (!c_parser_next_token_starts_declspecs (parser))
2628 /* ??? In some Objective-C cases '...' isn't applicable so there
2629 should be a different message. */
2630 c_parser_error (parser,
2631 "expected declaration specifiers or %<...%>");
2632 c_parser_skip_to_end_of_parameter (parser);
2633 return NULL;
2635 specs = build_null_declspecs ();
2636 if (attrs)
2638 declspecs_add_attrs (specs, attrs);
2639 attrs = NULL_TREE;
2641 c_parser_declspecs (parser, specs, true, true, true);
2642 finish_declspecs (specs);
2643 pending_xref_error ();
2644 prefix_attrs = specs->attrs;
2645 specs->attrs = NULL_TREE;
2646 declarator = c_parser_declarator (parser, specs->type_seen_p,
2647 C_DTR_PARM, &dummy);
2648 if (declarator == NULL)
2650 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2651 return NULL;
2653 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2654 postfix_attrs = c_parser_attributes (parser);
2655 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2656 declarator);
2659 /* Parse a string literal in an asm expression. It should not be
2660 translated, and wide string literals are an error although
2661 permitted by the syntax. This is a GNU extension.
2663 asm-string-literal:
2664 string-literal
2666 ??? At present, following the old parser, the caller needs to have
2667 set lex_untranslated_string to 1. It would be better to follow the
2668 C++ parser rather than using this kludge. */
2670 static tree
2671 c_parser_asm_string_literal (c_parser *parser)
2673 tree str;
2674 if (c_parser_next_token_is (parser, CPP_STRING))
2676 str = c_parser_peek_token (parser)->value;
2677 c_parser_consume_token (parser);
2679 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2681 error_at (c_parser_peek_token (parser)->location,
2682 "wide string literal in %<asm%>");
2683 str = build_string (1, "");
2684 c_parser_consume_token (parser);
2686 else
2688 c_parser_error (parser, "expected string literal");
2689 str = NULL_TREE;
2691 return str;
2694 /* Parse a simple asm expression. This is used in restricted
2695 contexts, where a full expression with inputs and outputs does not
2696 make sense. This is a GNU extension.
2698 simple-asm-expr:
2699 asm ( asm-string-literal )
2702 static tree
2703 c_parser_simple_asm_expr (c_parser *parser)
2705 tree str;
2706 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2707 /* ??? Follow the C++ parser rather than using the
2708 lex_untranslated_string kludge. */
2709 parser->lex_untranslated_string = true;
2710 c_parser_consume_token (parser);
2711 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2713 parser->lex_untranslated_string = false;
2714 return NULL_TREE;
2716 str = c_parser_asm_string_literal (parser);
2717 parser->lex_untranslated_string = false;
2718 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2720 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2721 return NULL_TREE;
2723 return str;
2726 /* Parse (possibly empty) attributes. This is a GNU extension.
2728 attributes:
2729 empty
2730 attributes attribute
2732 attribute:
2733 __attribute__ ( ( attribute-list ) )
2735 attribute-list:
2736 attrib
2737 attribute_list , attrib
2739 attrib:
2740 empty
2741 any-word
2742 any-word ( identifier )
2743 any-word ( identifier , nonempty-expr-list )
2744 any-word ( expr-list )
2746 where the "identifier" must not be declared as a type, and
2747 "any-word" may be any identifier (including one declared as a
2748 type), a reserved word storage class specifier, type specifier or
2749 type qualifier. ??? This still leaves out most reserved keywords
2750 (following the old parser), shouldn't we include them, and why not
2751 allow identifiers declared as types to start the arguments? */
2753 static tree
2754 c_parser_attributes (c_parser *parser)
2756 tree attrs = NULL_TREE;
2757 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2759 /* ??? Follow the C++ parser rather than using the
2760 lex_untranslated_string kludge. */
2761 parser->lex_untranslated_string = true;
2762 c_parser_consume_token (parser);
2763 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2765 parser->lex_untranslated_string = false;
2766 return attrs;
2768 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2770 parser->lex_untranslated_string = false;
2771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2772 return attrs;
2774 /* Parse the attribute list. */
2775 while (c_parser_next_token_is (parser, CPP_COMMA)
2776 || c_parser_next_token_is (parser, CPP_NAME)
2777 || c_parser_next_token_is (parser, CPP_KEYWORD))
2779 tree attr, attr_name, attr_args;
2780 if (c_parser_next_token_is (parser, CPP_COMMA))
2782 c_parser_consume_token (parser);
2783 continue;
2785 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2787 /* ??? See comment above about what keywords are
2788 accepted here. */
2789 bool ok;
2790 switch (c_parser_peek_token (parser)->keyword)
2792 case RID_STATIC:
2793 case RID_UNSIGNED:
2794 case RID_LONG:
2795 case RID_CONST:
2796 case RID_EXTERN:
2797 case RID_REGISTER:
2798 case RID_TYPEDEF:
2799 case RID_SHORT:
2800 case RID_INLINE:
2801 case RID_VOLATILE:
2802 case RID_SIGNED:
2803 case RID_AUTO:
2804 case RID_RESTRICT:
2805 case RID_COMPLEX:
2806 case RID_THREAD:
2807 case RID_INT:
2808 case RID_CHAR:
2809 case RID_FLOAT:
2810 case RID_DOUBLE:
2811 case RID_VOID:
2812 case RID_DFLOAT32:
2813 case RID_DFLOAT64:
2814 case RID_DFLOAT128:
2815 case RID_BOOL:
2816 case RID_FRACT:
2817 case RID_ACCUM:
2818 case RID_SAT:
2819 ok = true;
2820 break;
2821 default:
2822 ok = false;
2823 break;
2825 if (!ok)
2826 break;
2827 /* Accept __attribute__((__const)) as __attribute__((const))
2828 etc. */
2829 attr_name
2830 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
2832 else
2833 attr_name = c_parser_peek_token (parser)->value;
2834 c_parser_consume_token (parser);
2835 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2837 attr = build_tree_list (attr_name, NULL_TREE);
2838 attrs = chainon (attrs, attr);
2839 continue;
2841 c_parser_consume_token (parser);
2842 /* Parse the attribute contents. If they start with an
2843 identifier which is followed by a comma or close
2844 parenthesis, then the arguments start with that
2845 identifier; otherwise they are an expression list. */
2846 if (c_parser_next_token_is (parser, CPP_NAME)
2847 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2848 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2849 || (c_parser_peek_2nd_token (parser)->type
2850 == CPP_CLOSE_PAREN)))
2852 tree arg1 = c_parser_peek_token (parser)->value;
2853 c_parser_consume_token (parser);
2854 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2855 attr_args = build_tree_list (NULL_TREE, arg1);
2856 else
2858 c_parser_consume_token (parser);
2859 attr_args = tree_cons (NULL_TREE, arg1,
2860 c_parser_expr_list (parser, false));
2863 else
2865 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2866 attr_args = NULL_TREE;
2867 else
2868 attr_args = c_parser_expr_list (parser, false);
2870 attr = build_tree_list (attr_name, attr_args);
2871 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2872 c_parser_consume_token (parser);
2873 else
2875 parser->lex_untranslated_string = false;
2876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2877 "expected %<)%>");
2878 return attrs;
2880 attrs = chainon (attrs, attr);
2882 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2883 c_parser_consume_token (parser);
2884 else
2886 parser->lex_untranslated_string = false;
2887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2888 "expected %<)%>");
2889 return attrs;
2891 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2892 c_parser_consume_token (parser);
2893 else
2895 parser->lex_untranslated_string = false;
2896 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2897 "expected %<)%>");
2898 return attrs;
2900 parser->lex_untranslated_string = false;
2902 return attrs;
2905 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2907 type-name:
2908 specifier-qualifier-list abstract-declarator[opt]
2911 static struct c_type_name *
2912 c_parser_type_name (c_parser *parser)
2914 struct c_declspecs *specs = build_null_declspecs ();
2915 struct c_declarator *declarator;
2916 struct c_type_name *ret;
2917 bool dummy = false;
2918 c_parser_declspecs (parser, specs, false, true, true);
2919 if (!specs->declspecs_seen_p)
2921 c_parser_error (parser, "expected specifier-qualifier-list");
2922 return NULL;
2924 pending_xref_error ();
2925 finish_declspecs (specs);
2926 declarator = c_parser_declarator (parser, specs->type_seen_p,
2927 C_DTR_ABSTRACT, &dummy);
2928 if (declarator == NULL)
2929 return NULL;
2930 ret = XOBNEW (&parser_obstack, struct c_type_name);
2931 ret->specs = specs;
2932 ret->declarator = declarator;
2933 return ret;
2936 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2938 initializer:
2939 assignment-expression
2940 { initializer-list }
2941 { initializer-list , }
2943 initializer-list:
2944 designation[opt] initializer
2945 initializer-list , designation[opt] initializer
2947 designation:
2948 designator-list =
2950 designator-list:
2951 designator
2952 designator-list designator
2954 designator:
2955 array-designator
2956 . identifier
2958 array-designator:
2959 [ constant-expression ]
2961 GNU extensions:
2963 initializer:
2966 designation:
2967 array-designator
2968 identifier :
2970 array-designator:
2971 [ constant-expression ... constant-expression ]
2973 Any expression without commas is accepted in the syntax for the
2974 constant-expressions, with non-constant expressions rejected later.
2976 This function is only used for top-level initializers; for nested
2977 ones, see c_parser_initval. */
2979 static struct c_expr
2980 c_parser_initializer (c_parser *parser)
2982 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2983 return c_parser_braced_init (parser, NULL_TREE, false);
2984 else
2986 struct c_expr ret;
2987 ret = c_parser_expr_no_commas (parser, NULL);
2988 if (TREE_CODE (ret.value) != STRING_CST
2989 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
2990 ret = default_function_array_conversion (ret);
2991 return ret;
2995 /* Parse a braced initializer list. TYPE is the type specified for a
2996 compound literal, and NULL_TREE for other initializers and for
2997 nested braced lists. NESTED_P is true for nested braced lists,
2998 false for the list of a compound literal or the list that is the
2999 top-level initializer in a declaration. */
3001 static struct c_expr
3002 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3004 location_t brace_loc = c_parser_peek_token (parser)->location;
3005 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3006 c_parser_consume_token (parser);
3007 if (nested_p)
3008 push_init_level (0);
3009 else
3010 really_start_incremental_init (type);
3011 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3013 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3015 else
3017 /* Parse a non-empty initializer list, possibly with a trailing
3018 comma. */
3019 while (true)
3021 c_parser_initelt (parser);
3022 if (parser->error)
3023 break;
3024 if (c_parser_next_token_is (parser, CPP_COMMA))
3025 c_parser_consume_token (parser);
3026 else
3027 break;
3028 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3029 break;
3032 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3034 struct c_expr ret;
3035 ret.value = error_mark_node;
3036 ret.original_code = ERROR_MARK;
3037 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3038 return ret;
3040 c_parser_consume_token (parser);
3041 return pop_init_level (0);
3044 /* Parse a nested initializer, including designators. */
3046 static void
3047 c_parser_initelt (c_parser *parser)
3049 /* Parse any designator or designator list. A single array
3050 designator may have the subsequent "=" omitted in GNU C, but a
3051 longer list or a structure member designator may not. */
3052 if (c_parser_next_token_is (parser, CPP_NAME)
3053 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3055 /* Old-style structure member designator. */
3056 set_init_label (c_parser_peek_token (parser)->value);
3057 /* Use the colon as the error location. */
3058 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3059 "obsolete use of designated initializer with %<:%>");
3060 c_parser_consume_token (parser);
3061 c_parser_consume_token (parser);
3063 else
3065 /* des_seen is 0 if there have been no designators, 1 if there
3066 has been a single array designator and 2 otherwise. */
3067 int des_seen = 0;
3068 /* Location of a designator. */
3069 location_t des_loc;
3070 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3071 || c_parser_next_token_is (parser, CPP_DOT))
3073 int des_prev = des_seen;
3074 if (!des_seen)
3075 des_loc = c_parser_peek_token (parser)->location;
3076 if (des_seen < 2)
3077 des_seen++;
3078 if (c_parser_next_token_is (parser, CPP_DOT))
3080 des_seen = 2;
3081 c_parser_consume_token (parser);
3082 if (c_parser_next_token_is (parser, CPP_NAME))
3084 set_init_label (c_parser_peek_token (parser)->value);
3085 c_parser_consume_token (parser);
3087 else
3089 struct c_expr init;
3090 init.value = error_mark_node;
3091 init.original_code = ERROR_MARK;
3092 c_parser_error (parser, "expected identifier");
3093 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3094 process_init_element (init);
3095 return;
3098 else
3100 tree first, second;
3101 location_t ellipsis_loc;
3102 /* ??? Following the old parser, [ objc-receiver
3103 objc-message-args ] is accepted as an initializer,
3104 being distinguished from a designator by what follows
3105 the first assignment expression inside the square
3106 brackets, but after a first array designator a
3107 subsequent square bracket is for Objective-C taken to
3108 start an expression, using the obsolete form of
3109 designated initializer without '=', rather than
3110 possibly being a second level of designation: in LALR
3111 terms, the '[' is shifted rather than reducing
3112 designator to designator-list. */
3113 if (des_prev == 1 && c_dialect_objc ())
3115 des_seen = des_prev;
3116 break;
3118 if (des_prev == 0 && c_dialect_objc ())
3120 /* This might be an array designator or an
3121 Objective-C message expression. If the former,
3122 continue parsing here; if the latter, parse the
3123 remainder of the initializer given the starting
3124 primary-expression. ??? It might make sense to
3125 distinguish when des_prev == 1 as well; see
3126 previous comment. */
3127 tree rec, args;
3128 struct c_expr mexpr;
3129 c_parser_consume_token (parser);
3130 if (c_parser_peek_token (parser)->type == CPP_NAME
3131 && ((c_parser_peek_token (parser)->id_kind
3132 == C_ID_TYPENAME)
3133 || (c_parser_peek_token (parser)->id_kind
3134 == C_ID_CLASSNAME)))
3136 /* Type name receiver. */
3137 tree id = c_parser_peek_token (parser)->value;
3138 c_parser_consume_token (parser);
3139 rec = objc_get_class_reference (id);
3140 goto parse_message_args;
3142 first = c_parser_expr_no_commas (parser, NULL).value;
3143 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3144 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3145 goto array_desig_after_first;
3146 /* Expression receiver. So far only one part
3147 without commas has been parsed; there might be
3148 more of the expression. */
3149 rec = first;
3150 while (c_parser_next_token_is (parser, CPP_COMMA))
3152 struct c_expr next;
3153 c_parser_consume_token (parser);
3154 next = c_parser_expr_no_commas (parser, NULL);
3155 next = default_function_array_conversion (next);
3156 rec = build_compound_expr (rec, next.value);
3158 parse_message_args:
3159 /* Now parse the objc-message-args. */
3160 args = c_parser_objc_message_args (parser);
3161 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3162 "expected %<]%>");
3163 mexpr.value
3164 = objc_build_message_expr (build_tree_list (rec, args));
3165 mexpr.original_code = ERROR_MARK;
3166 /* Now parse and process the remainder of the
3167 initializer, starting with this message
3168 expression as a primary-expression. */
3169 c_parser_initval (parser, &mexpr);
3170 return;
3172 c_parser_consume_token (parser);
3173 first = c_parser_expr_no_commas (parser, NULL).value;
3174 array_desig_after_first:
3175 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3177 ellipsis_loc = c_parser_peek_token (parser)->location;
3178 c_parser_consume_token (parser);
3179 second = c_parser_expr_no_commas (parser, NULL).value;
3181 else
3182 second = NULL_TREE;
3183 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3185 c_parser_consume_token (parser);
3186 set_init_index (first, second);
3187 if (second)
3188 pedwarn (ellipsis_loc, OPT_pedantic,
3189 "ISO C forbids specifying range of elements to initialize");
3191 else
3192 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3193 "expected %<]%>");
3196 if (des_seen >= 1)
3198 if (c_parser_next_token_is (parser, CPP_EQ))
3200 if (!flag_isoc99)
3201 pedwarn (des_loc, OPT_pedantic,
3202 "ISO C90 forbids specifying subobject to initialize");
3203 c_parser_consume_token (parser);
3205 else
3207 if (des_seen == 1)
3208 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3209 "obsolete use of designated initializer without %<=%>");
3210 else
3212 struct c_expr init;
3213 init.value = error_mark_node;
3214 init.original_code = ERROR_MARK;
3215 c_parser_error (parser, "expected %<=%>");
3216 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3217 process_init_element (init);
3218 return;
3223 c_parser_initval (parser, NULL);
3226 /* Parse a nested initializer; as c_parser_initializer but parses
3227 initializers within braced lists, after any designators have been
3228 applied. If AFTER is not NULL then it is an Objective-C message
3229 expression which is the primary-expression starting the
3230 initializer. */
3232 static void
3233 c_parser_initval (c_parser *parser, struct c_expr *after)
3235 struct c_expr init;
3236 gcc_assert (!after || c_dialect_objc ());
3237 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3238 init = c_parser_braced_init (parser, NULL_TREE, true);
3239 else
3241 init = c_parser_expr_no_commas (parser, after);
3242 if (init.value != NULL_TREE
3243 && TREE_CODE (init.value) != STRING_CST
3244 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3245 init = default_function_array_conversion (init);
3247 process_init_element (init);
3250 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3251 C99 6.8.2).
3253 compound-statement:
3254 { block-item-list[opt] }
3255 { label-declarations block-item-list }
3257 block-item-list:
3258 block-item
3259 block-item-list block-item
3261 block-item:
3262 nested-declaration
3263 statement
3265 nested-declaration:
3266 declaration
3268 GNU extensions:
3270 compound-statement:
3271 { label-declarations block-item-list }
3273 nested-declaration:
3274 __extension__ nested-declaration
3275 nested-function-definition
3277 label-declarations:
3278 label-declaration
3279 label-declarations label-declaration
3281 label-declaration:
3282 __label__ identifier-list ;
3284 Allowing the mixing of declarations and code is new in C99. The
3285 GNU syntax also permits (not shown above) labels at the end of
3286 compound statements, which yield an error. We don't allow labels
3287 on declarations; this might seem like a natural extension, but
3288 there would be a conflict between attributes on the label and
3289 prefix attributes on the declaration. ??? The syntax follows the
3290 old parser in requiring something after label declarations.
3291 Although they are erroneous if the labels declared aren't defined,
3292 is it useful for the syntax to be this way?
3294 OpenMP:
3296 block-item:
3297 openmp-directive
3299 openmp-directive:
3300 barrier-directive
3301 flush-directive */
3303 static tree
3304 c_parser_compound_statement (c_parser *parser)
3306 tree stmt;
3307 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3308 return error_mark_node;
3309 stmt = c_begin_compound_stmt (true);
3310 c_parser_compound_statement_nostart (parser);
3311 return c_end_compound_stmt (stmt, true);
3314 /* Parse a compound statement except for the opening brace. This is
3315 used for parsing both compound statements and statement expressions
3316 (which follow different paths to handling the opening). */
3318 static void
3319 c_parser_compound_statement_nostart (c_parser *parser)
3321 bool last_stmt = false;
3322 bool last_label = false;
3323 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3324 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3326 c_parser_consume_token (parser);
3327 return;
3329 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3331 location_t err_loc = c_parser_peek_token (parser)->location;
3332 /* Read zero or more forward-declarations for labels that nested
3333 functions can jump to. */
3334 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3336 c_parser_consume_token (parser);
3337 /* Any identifiers, including those declared as type names,
3338 are OK here. */
3339 while (true)
3341 tree label;
3342 if (c_parser_next_token_is_not (parser, CPP_NAME))
3344 c_parser_error (parser, "expected identifier");
3345 break;
3347 label
3348 = declare_label (c_parser_peek_token (parser)->value);
3349 C_DECLARED_LABEL_FLAG (label) = 1;
3350 add_stmt (build_stmt (DECL_EXPR, label));
3351 c_parser_consume_token (parser);
3352 if (c_parser_next_token_is (parser, CPP_COMMA))
3353 c_parser_consume_token (parser);
3354 else
3355 break;
3357 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3359 pedwarn (err_loc, OPT_pedantic, "ISO C forbids label declarations");
3361 /* We must now have at least one statement, label or declaration. */
3362 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3364 c_parser_error (parser, "expected declaration or statement");
3365 c_parser_consume_token (parser);
3366 return;
3368 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3370 location_t loc = c_parser_peek_token (parser)->location;
3371 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3372 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3373 || (c_parser_next_token_is (parser, CPP_NAME)
3374 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3376 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3377 label_loc = c_parser_peek_2nd_token (parser)->location;
3378 else
3379 label_loc = c_parser_peek_token (parser)->location;
3380 last_label = true;
3381 last_stmt = false;
3382 c_parser_label (parser);
3384 else if (!last_label
3385 && c_parser_next_token_starts_declspecs (parser))
3387 last_label = false;
3388 c_parser_declaration_or_fndef (parser, true, true, true, true);
3389 if (last_stmt)
3390 pedwarn_c90 (loc,
3391 (pedantic && !flag_isoc99)
3392 ? OPT_pedantic
3393 : OPT_Wdeclaration_after_statement,
3394 "ISO C90 forbids mixed declarations and code");
3395 last_stmt = false;
3397 else if (!last_label
3398 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3400 /* __extension__ can start a declaration, but is also an
3401 unary operator that can start an expression. Consume all
3402 but the last of a possible series of __extension__ to
3403 determine which. */
3404 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3405 && (c_parser_peek_2nd_token (parser)->keyword
3406 == RID_EXTENSION))
3407 c_parser_consume_token (parser);
3408 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3410 int ext;
3411 ext = disable_extension_diagnostics ();
3412 c_parser_consume_token (parser);
3413 last_label = false;
3414 c_parser_declaration_or_fndef (parser, true, true, true, true);
3415 /* Following the old parser, __extension__ does not
3416 disable this diagnostic. */
3417 restore_extension_diagnostics (ext);
3418 if (last_stmt)
3419 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3420 ? OPT_pedantic
3421 : OPT_Wdeclaration_after_statement,
3422 "ISO C90 forbids mixed declarations and code");
3423 last_stmt = false;
3425 else
3426 goto statement;
3428 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3430 /* External pragmas, and some omp pragmas, are not associated
3431 with regular c code, and so are not to be considered statements
3432 syntactically. This ensures that the user doesn't put them
3433 places that would turn into syntax errors if the directive
3434 were ignored. */
3435 if (c_parser_pragma (parser, pragma_compound))
3436 last_label = false, last_stmt = true;
3438 else if (c_parser_next_token_is (parser, CPP_EOF))
3440 c_parser_error (parser, "expected declaration or statement");
3441 return;
3443 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3445 if (parser->in_if_block)
3447 error_at (loc, """expected %<}%> before %<else%>");
3448 return;
3450 else
3452 error_at (loc, "%<else%> without a previous %<if%>");
3453 c_parser_consume_token (parser);
3454 continue;
3457 else
3459 statement:
3460 last_label = false;
3461 last_stmt = true;
3462 c_parser_statement_after_labels (parser);
3465 parser->error = false;
3467 if (last_label)
3468 error_at (label_loc, "label at end of compound statement");
3469 c_parser_consume_token (parser);
3472 /* Parse a label (C90 6.6.1, C99 6.8.1).
3474 label:
3475 identifier : attributes[opt]
3476 case constant-expression :
3477 default :
3479 GNU extensions:
3481 label:
3482 case constant-expression ... constant-expression :
3484 The use of attributes on labels is a GNU extension. The syntax in
3485 GNU C accepts any expressions without commas, non-constant
3486 expressions being rejected later. */
3488 static void
3489 c_parser_label (c_parser *parser)
3491 location_t loc1 = c_parser_peek_token (parser)->location;
3492 tree label = NULL_TREE;
3493 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3495 tree exp1, exp2;
3496 c_parser_consume_token (parser);
3497 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3498 if (c_parser_next_token_is (parser, CPP_COLON))
3500 c_parser_consume_token (parser);
3501 label = do_case (exp1, NULL_TREE);
3503 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3505 c_parser_consume_token (parser);
3506 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3507 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3508 label = do_case (exp1, exp2);
3510 else
3511 c_parser_error (parser, "expected %<:%> or %<...%>");
3513 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3515 c_parser_consume_token (parser);
3516 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3517 label = do_case (NULL_TREE, NULL_TREE);
3519 else
3521 tree name = c_parser_peek_token (parser)->value;
3522 tree tlab;
3523 tree attrs;
3524 location_t loc2 = c_parser_peek_token (parser)->location;
3525 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3526 c_parser_consume_token (parser);
3527 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3528 c_parser_consume_token (parser);
3529 attrs = c_parser_attributes (parser);
3530 tlab = define_label (loc2, name);
3531 if (tlab)
3533 decl_attributes (&tlab, attrs, 0);
3534 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3537 if (label)
3539 SET_EXPR_LOCATION (label, loc1);
3540 if (c_parser_next_token_starts_declspecs (parser)
3541 && !(c_parser_next_token_is (parser, CPP_NAME)
3542 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3544 error_at (c_parser_peek_token (parser)->location,
3545 "a label can only be part of a statement and "
3546 "a declaration is not a statement");
3547 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3548 /*nested*/ true, /*empty_ok*/ false,
3549 /*start_attr_ok*/ true);
3554 /* Parse a statement (C90 6.6, C99 6.8).
3556 statement:
3557 labeled-statement
3558 compound-statement
3559 expression-statement
3560 selection-statement
3561 iteration-statement
3562 jump-statement
3564 labeled-statement:
3565 label statement
3567 expression-statement:
3568 expression[opt] ;
3570 selection-statement:
3571 if-statement
3572 switch-statement
3574 iteration-statement:
3575 while-statement
3576 do-statement
3577 for-statement
3579 jump-statement:
3580 goto identifier ;
3581 continue ;
3582 break ;
3583 return expression[opt] ;
3585 GNU extensions:
3587 statement:
3588 asm-statement
3590 jump-statement:
3591 goto * expression ;
3593 Objective-C:
3595 statement:
3596 objc-throw-statement
3597 objc-try-catch-statement
3598 objc-synchronized-statement
3600 objc-throw-statement:
3601 @throw expression ;
3602 @throw ;
3604 OpenMP:
3606 statement:
3607 openmp-construct
3609 openmp-construct:
3610 parallel-construct
3611 for-construct
3612 sections-construct
3613 single-construct
3614 parallel-for-construct
3615 parallel-sections-construct
3616 master-construct
3617 critical-construct
3618 atomic-construct
3619 ordered-construct
3621 parallel-construct:
3622 parallel-directive structured-block
3624 for-construct:
3625 for-directive iteration-statement
3627 sections-construct:
3628 sections-directive section-scope
3630 single-construct:
3631 single-directive structured-block
3633 parallel-for-construct:
3634 parallel-for-directive iteration-statement
3636 parallel-sections-construct:
3637 parallel-sections-directive section-scope
3639 master-construct:
3640 master-directive structured-block
3642 critical-construct:
3643 critical-directive structured-block
3645 atomic-construct:
3646 atomic-directive expression-statement
3648 ordered-construct:
3649 ordered-directive structured-block */
3651 static void
3652 c_parser_statement (c_parser *parser)
3654 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3655 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3656 || (c_parser_next_token_is (parser, CPP_NAME)
3657 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3658 c_parser_label (parser);
3659 c_parser_statement_after_labels (parser);
3662 /* Parse a statement, other than a labeled statement. */
3664 static void
3665 c_parser_statement_after_labels (c_parser *parser)
3667 location_t loc = c_parser_peek_token (parser)->location;
3668 tree stmt = NULL_TREE;
3669 bool in_if_block = parser->in_if_block;
3670 parser->in_if_block = false;
3671 switch (c_parser_peek_token (parser)->type)
3673 case CPP_OPEN_BRACE:
3674 add_stmt (c_parser_compound_statement (parser));
3675 break;
3676 case CPP_KEYWORD:
3677 switch (c_parser_peek_token (parser)->keyword)
3679 case RID_IF:
3680 c_parser_if_statement (parser);
3681 break;
3682 case RID_SWITCH:
3683 c_parser_switch_statement (parser);
3684 break;
3685 case RID_WHILE:
3686 c_parser_while_statement (parser);
3687 break;
3688 case RID_DO:
3689 c_parser_do_statement (parser);
3690 break;
3691 case RID_FOR:
3692 c_parser_for_statement (parser);
3693 break;
3694 case RID_GOTO:
3695 c_parser_consume_token (parser);
3696 if (c_parser_next_token_is (parser, CPP_NAME))
3698 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3699 c_parser_consume_token (parser);
3701 else if (c_parser_next_token_is (parser, CPP_MULT))
3703 c_parser_consume_token (parser);
3704 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3706 else
3707 c_parser_error (parser, "expected identifier or %<*%>");
3708 goto expect_semicolon;
3709 case RID_CONTINUE:
3710 c_parser_consume_token (parser);
3711 stmt = c_finish_bc_stmt (&c_cont_label, false);
3712 goto expect_semicolon;
3713 case RID_BREAK:
3714 c_parser_consume_token (parser);
3715 stmt = c_finish_bc_stmt (&c_break_label, true);
3716 goto expect_semicolon;
3717 case RID_RETURN:
3718 c_parser_consume_token (parser);
3719 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3721 stmt = c_finish_return (NULL_TREE);
3722 c_parser_consume_token (parser);
3724 else
3726 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3727 goto expect_semicolon;
3729 break;
3730 case RID_ASM:
3731 stmt = c_parser_asm_statement (parser);
3732 break;
3733 case RID_THROW:
3734 gcc_assert (c_dialect_objc ());
3735 c_parser_consume_token (parser);
3736 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3738 stmt = objc_build_throw_stmt (NULL_TREE);
3739 c_parser_consume_token (parser);
3741 else
3743 stmt
3744 = objc_build_throw_stmt (c_parser_expression (parser).value);
3745 goto expect_semicolon;
3747 break;
3748 case RID_TRY:
3749 gcc_assert (c_dialect_objc ());
3750 c_parser_objc_try_catch_statement (parser);
3751 break;
3752 case RID_AT_SYNCHRONIZED:
3753 gcc_assert (c_dialect_objc ());
3754 c_parser_objc_synchronized_statement (parser);
3755 break;
3756 default:
3757 goto expr_stmt;
3759 break;
3760 case CPP_SEMICOLON:
3761 c_parser_consume_token (parser);
3762 break;
3763 case CPP_CLOSE_PAREN:
3764 case CPP_CLOSE_SQUARE:
3765 /* Avoid infinite loop in error recovery:
3766 c_parser_skip_until_found stops at a closing nesting
3767 delimiter without consuming it, but here we need to consume
3768 it to proceed further. */
3769 c_parser_error (parser, "expected statement");
3770 c_parser_consume_token (parser);
3771 break;
3772 case CPP_PRAGMA:
3773 c_parser_pragma (parser, pragma_stmt);
3774 break;
3775 default:
3776 expr_stmt:
3777 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3778 expect_semicolon:
3779 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3780 break;
3782 /* Two cases cannot and do not have line numbers associated: If stmt
3783 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3784 cannot hold line numbers. But that's OK because the statement
3785 will either be changed to a MODIFY_EXPR during gimplification of
3786 the statement expr, or discarded. If stmt was compound, but
3787 without new variables, we will have skipped the creation of a
3788 BIND and will have a bare STATEMENT_LIST. But that's OK because
3789 (recursively) all of the component statements should already have
3790 line numbers assigned. ??? Can we discard no-op statements
3791 earlier? */
3792 if (stmt && CAN_HAVE_LOCATION_P (stmt))
3793 SET_EXPR_LOCATION (stmt, loc);
3795 parser->in_if_block = in_if_block;
3798 /* Parse the condition from an if, do, while or for statements. */
3800 static tree
3801 c_parser_condition (c_parser *parser)
3803 location_t loc;
3804 tree cond;
3805 loc = c_parser_peek_token (parser)->location;
3806 cond = c_objc_common_truthvalue_conversion
3807 (loc, c_parser_expression_conv (parser).value);
3808 if (CAN_HAVE_LOCATION_P (cond))
3809 SET_EXPR_LOCATION (cond, loc);
3810 if (warn_sequence_point)
3811 verify_sequence_points (cond);
3812 return cond;
3815 /* Parse a parenthesized condition from an if, do or while statement.
3817 condition:
3818 ( expression )
3820 static tree
3821 c_parser_paren_condition (c_parser *parser)
3823 tree cond;
3824 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3825 return error_mark_node;
3826 cond = c_parser_condition (parser);
3827 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3828 return cond;
3831 /* Parse a statement which is a block in C99. */
3833 static tree
3834 c_parser_c99_block_statement (c_parser *parser)
3836 tree block = c_begin_compound_stmt (flag_isoc99);
3837 c_parser_statement (parser);
3838 return c_end_compound_stmt (block, flag_isoc99);
3841 /* Parse the body of an if statement. This is just parsing a
3842 statement but (a) it is a block in C99, (b) we track whether the
3843 body is an if statement for the sake of -Wparentheses warnings, (c)
3844 we handle an empty body specially for the sake of -Wempty-body
3845 warnings, and (d) we call parser_compound_statement directly
3846 because c_parser_statement_after_labels resets
3847 parser->in_if_block. */
3849 static tree
3850 c_parser_if_body (c_parser *parser, bool *if_p)
3852 tree block = c_begin_compound_stmt (flag_isoc99);
3853 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3854 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3855 || (c_parser_next_token_is (parser, CPP_NAME)
3856 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3857 c_parser_label (parser);
3858 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3859 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3861 add_stmt (build_empty_stmt ());
3862 c_parser_consume_token (parser);
3864 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3865 add_stmt (c_parser_compound_statement (parser));
3866 else
3867 c_parser_statement_after_labels (parser);
3868 return c_end_compound_stmt (block, flag_isoc99);
3871 /* Parse the else body of an if statement. This is just parsing a
3872 statement but (a) it is a block in C99, (b) we handle an empty body
3873 specially for the sake of -Wempty-body warnings. */
3875 static tree
3876 c_parser_else_body (c_parser *parser)
3878 tree block = c_begin_compound_stmt (flag_isoc99);
3879 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3880 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3881 || (c_parser_next_token_is (parser, CPP_NAME)
3882 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3883 c_parser_label (parser);
3884 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3886 add_stmt (build_empty_stmt ());
3887 c_parser_consume_token (parser);
3889 else
3890 c_parser_statement_after_labels (parser);
3891 return c_end_compound_stmt (block, flag_isoc99);
3894 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3896 if-statement:
3897 if ( expression ) statement
3898 if ( expression ) statement else statement
3901 static void
3902 c_parser_if_statement (c_parser *parser)
3904 tree block;
3905 location_t loc;
3906 tree cond;
3907 bool first_if = false;
3908 tree first_body, second_body;
3909 bool in_if_block;
3911 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3912 c_parser_consume_token (parser);
3913 block = c_begin_compound_stmt (flag_isoc99);
3914 loc = c_parser_peek_token (parser)->location;
3915 cond = c_parser_paren_condition (parser);
3916 in_if_block = parser->in_if_block;
3917 parser->in_if_block = true;
3918 first_body = c_parser_if_body (parser, &first_if);
3919 parser->in_if_block = in_if_block;
3920 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3922 c_parser_consume_token (parser);
3923 second_body = c_parser_else_body (parser);
3925 else
3926 second_body = NULL_TREE;
3927 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3928 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3931 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3933 switch-statement:
3934 switch (expression) statement
3937 static void
3938 c_parser_switch_statement (c_parser *parser)
3940 tree block, expr, body, save_break;
3941 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3942 c_parser_consume_token (parser);
3943 block = c_begin_compound_stmt (flag_isoc99);
3944 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3946 expr = c_parser_expression (parser).value;
3947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3949 else
3950 expr = error_mark_node;
3951 c_start_case (expr);
3952 save_break = c_break_label;
3953 c_break_label = NULL_TREE;
3954 body = c_parser_c99_block_statement (parser);
3955 c_finish_case (body);
3956 if (c_break_label)
3957 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3958 c_break_label = save_break;
3959 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3962 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3964 while-statement:
3965 while (expression) statement
3968 static void
3969 c_parser_while_statement (c_parser *parser)
3971 tree block, cond, body, save_break, save_cont;
3972 location_t loc;
3973 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3974 c_parser_consume_token (parser);
3975 block = c_begin_compound_stmt (flag_isoc99);
3976 loc = c_parser_peek_token (parser)->location;
3977 cond = c_parser_paren_condition (parser);
3978 save_break = c_break_label;
3979 c_break_label = NULL_TREE;
3980 save_cont = c_cont_label;
3981 c_cont_label = NULL_TREE;
3982 body = c_parser_c99_block_statement (parser);
3983 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3984 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3985 c_break_label = save_break;
3986 c_cont_label = save_cont;
3989 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3991 do-statement:
3992 do statement while ( expression ) ;
3995 static void
3996 c_parser_do_statement (c_parser *parser)
3998 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3999 location_t loc;
4000 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4001 c_parser_consume_token (parser);
4002 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4003 warning_at (c_parser_peek_token (parser)->location,
4004 OPT_Wempty_body,
4005 "suggest braces around empty body in %<do%> statement");
4006 block = c_begin_compound_stmt (flag_isoc99);
4007 loc = c_parser_peek_token (parser)->location;
4008 save_break = c_break_label;
4009 c_break_label = NULL_TREE;
4010 save_cont = c_cont_label;
4011 c_cont_label = NULL_TREE;
4012 body = c_parser_c99_block_statement (parser);
4013 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4014 new_break = c_break_label;
4015 c_break_label = save_break;
4016 new_cont = c_cont_label;
4017 c_cont_label = save_cont;
4018 cond = c_parser_paren_condition (parser);
4019 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4020 c_parser_skip_to_end_of_block_or_statement (parser);
4021 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4022 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4025 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4027 for-statement:
4028 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4029 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4031 The form with a declaration is new in C99.
4033 ??? In accordance with the old parser, the declaration may be a
4034 nested function, which is then rejected in check_for_loop_decls,
4035 but does it make any sense for this to be included in the grammar?
4036 Note in particular that the nested function does not include a
4037 trailing ';', whereas the "declaration" production includes one.
4038 Also, can we reject bad declarations earlier and cheaper than
4039 check_for_loop_decls? */
4041 static void
4042 c_parser_for_statement (c_parser *parser)
4044 tree block, cond, incr, save_break, save_cont, body;
4045 location_t loc;
4046 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4047 loc = c_parser_peek_token (parser)->location;
4048 c_parser_consume_token (parser);
4049 block = c_begin_compound_stmt (flag_isoc99);
4050 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4052 /* Parse the initialization declaration or expression. */
4053 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4055 c_parser_consume_token (parser);
4056 c_finish_expr_stmt (NULL_TREE);
4058 else if (c_parser_next_token_starts_declspecs (parser))
4060 c_parser_declaration_or_fndef (parser, true, true, true, true);
4061 check_for_loop_decls ();
4063 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4065 /* __extension__ can start a declaration, but is also an
4066 unary operator that can start an expression. Consume all
4067 but the last of a possible series of __extension__ to
4068 determine which. */
4069 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4070 && (c_parser_peek_2nd_token (parser)->keyword
4071 == RID_EXTENSION))
4072 c_parser_consume_token (parser);
4073 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4075 int ext;
4076 ext = disable_extension_diagnostics ();
4077 c_parser_consume_token (parser);
4078 c_parser_declaration_or_fndef (parser, true, true, true, true);
4079 restore_extension_diagnostics (ext);
4080 check_for_loop_decls ();
4082 else
4083 goto init_expr;
4085 else
4087 init_expr:
4088 c_finish_expr_stmt (c_parser_expression (parser).value);
4089 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4091 /* Parse the loop condition. */
4092 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4094 c_parser_consume_token (parser);
4095 cond = NULL_TREE;
4097 else
4099 cond = c_parser_condition (parser);
4100 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4102 /* Parse the increment expression. */
4103 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4104 incr = c_process_expr_stmt (NULL_TREE);
4105 else
4106 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4107 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4109 else
4111 cond = error_mark_node;
4112 incr = error_mark_node;
4114 save_break = c_break_label;
4115 c_break_label = NULL_TREE;
4116 save_cont = c_cont_label;
4117 c_cont_label = NULL_TREE;
4118 body = c_parser_c99_block_statement (parser);
4119 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4120 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4121 c_break_label = save_break;
4122 c_cont_label = save_cont;
4125 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4126 statement with inputs, outputs, clobbers, and volatile tag
4127 allowed.
4129 asm-statement:
4130 asm type-qualifier[opt] ( asm-argument ) ;
4132 asm-argument:
4133 asm-string-literal
4134 asm-string-literal : asm-operands[opt]
4135 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4136 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4138 Qualifiers other than volatile are accepted in the syntax but
4139 warned for. */
4141 static tree
4142 c_parser_asm_statement (c_parser *parser)
4144 tree quals, str, outputs, inputs, clobbers, ret;
4145 bool simple;
4146 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4147 c_parser_consume_token (parser);
4148 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4150 quals = c_parser_peek_token (parser)->value;
4151 c_parser_consume_token (parser);
4153 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4154 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4156 warning_at (c_parser_peek_token (parser)->location,
4158 "%E qualifier ignored on asm",
4159 c_parser_peek_token (parser)->value);
4160 quals = NULL_TREE;
4161 c_parser_consume_token (parser);
4163 else
4164 quals = NULL_TREE;
4165 /* ??? Follow the C++ parser rather than using the
4166 lex_untranslated_string kludge. */
4167 parser->lex_untranslated_string = true;
4168 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4170 parser->lex_untranslated_string = false;
4171 return NULL_TREE;
4173 str = c_parser_asm_string_literal (parser);
4174 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4176 simple = true;
4177 outputs = NULL_TREE;
4178 inputs = NULL_TREE;
4179 clobbers = NULL_TREE;
4180 goto done_asm;
4182 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4184 parser->lex_untranslated_string = false;
4185 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4186 return NULL_TREE;
4188 simple = false;
4189 /* Parse outputs. */
4190 if (c_parser_next_token_is (parser, CPP_COLON)
4191 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4192 outputs = NULL_TREE;
4193 else
4194 outputs = c_parser_asm_operands (parser, false);
4195 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4197 inputs = NULL_TREE;
4198 clobbers = NULL_TREE;
4199 goto done_asm;
4201 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4203 parser->lex_untranslated_string = false;
4204 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4205 return NULL_TREE;
4207 /* Parse inputs. */
4208 if (c_parser_next_token_is (parser, CPP_COLON)
4209 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4210 inputs = NULL_TREE;
4211 else
4212 inputs = c_parser_asm_operands (parser, true);
4213 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4215 clobbers = NULL_TREE;
4216 goto done_asm;
4218 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4220 parser->lex_untranslated_string = false;
4221 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4222 return NULL_TREE;
4224 /* Parse clobbers. */
4225 clobbers = c_parser_asm_clobbers (parser);
4226 done_asm:
4227 parser->lex_untranslated_string = false;
4228 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4230 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4231 return NULL_TREE;
4233 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4234 c_parser_skip_to_end_of_block_or_statement (parser);
4235 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4236 clobbers, simple));
4237 return ret;
4240 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4241 not outputs), apply the default conversion of functions and arrays
4242 to pointers.
4244 asm-operands:
4245 asm-operand
4246 asm-operands , asm-operand
4248 asm-operand:
4249 asm-string-literal ( expression )
4250 [ identifier ] asm-string-literal ( expression )
4253 static tree
4254 c_parser_asm_operands (c_parser *parser, bool convert_p)
4256 tree list = NULL_TREE;
4257 while (true)
4259 tree name, str;
4260 struct c_expr expr;
4261 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4263 c_parser_consume_token (parser);
4264 if (c_parser_next_token_is (parser, CPP_NAME))
4266 tree id = c_parser_peek_token (parser)->value;
4267 c_parser_consume_token (parser);
4268 name = build_string (IDENTIFIER_LENGTH (id),
4269 IDENTIFIER_POINTER (id));
4271 else
4273 c_parser_error (parser, "expected identifier");
4274 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4275 return NULL_TREE;
4277 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4278 "expected %<]%>");
4280 else
4281 name = NULL_TREE;
4282 str = c_parser_asm_string_literal (parser);
4283 if (str == NULL_TREE)
4284 return NULL_TREE;
4285 parser->lex_untranslated_string = false;
4286 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4288 parser->lex_untranslated_string = true;
4289 return NULL_TREE;
4291 expr = c_parser_expression (parser);
4292 if (convert_p)
4293 expr = default_function_array_conversion (expr);
4294 parser->lex_untranslated_string = true;
4295 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4297 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4298 return NULL_TREE;
4300 list = chainon (list, build_tree_list (build_tree_list (name, str),
4301 expr.value));
4302 if (c_parser_next_token_is (parser, CPP_COMMA))
4303 c_parser_consume_token (parser);
4304 else
4305 break;
4307 return list;
4310 /* Parse asm clobbers, a GNU extension.
4312 asm-clobbers:
4313 asm-string-literal
4314 asm-clobbers , asm-string-literal
4317 static tree
4318 c_parser_asm_clobbers (c_parser *parser)
4320 tree list = NULL_TREE;
4321 while (true)
4323 tree str = c_parser_asm_string_literal (parser);
4324 if (str)
4325 list = tree_cons (NULL_TREE, str, list);
4326 else
4327 return NULL_TREE;
4328 if (c_parser_next_token_is (parser, CPP_COMMA))
4329 c_parser_consume_token (parser);
4330 else
4331 break;
4333 return list;
4336 /* Parse an expression other than a compound expression; that is, an
4337 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4338 NULL then it is an Objective-C message expression which is the
4339 primary-expression starting the expression as an initializer.
4341 assignment-expression:
4342 conditional-expression
4343 unary-expression assignment-operator assignment-expression
4345 assignment-operator: one of
4346 = *= /= %= += -= <<= >>= &= ^= |=
4348 In GNU C we accept any conditional expression on the LHS and
4349 diagnose the invalid lvalue rather than producing a syntax
4350 error. */
4352 static struct c_expr
4353 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4355 struct c_expr lhs, rhs, ret;
4356 enum tree_code code;
4357 gcc_assert (!after || c_dialect_objc ());
4358 lhs = c_parser_conditional_expression (parser, after);
4359 switch (c_parser_peek_token (parser)->type)
4361 case CPP_EQ:
4362 code = NOP_EXPR;
4363 break;
4364 case CPP_MULT_EQ:
4365 code = MULT_EXPR;
4366 break;
4367 case CPP_DIV_EQ:
4368 code = TRUNC_DIV_EXPR;
4369 break;
4370 case CPP_MOD_EQ:
4371 code = TRUNC_MOD_EXPR;
4372 break;
4373 case CPP_PLUS_EQ:
4374 code = PLUS_EXPR;
4375 break;
4376 case CPP_MINUS_EQ:
4377 code = MINUS_EXPR;
4378 break;
4379 case CPP_LSHIFT_EQ:
4380 code = LSHIFT_EXPR;
4381 break;
4382 case CPP_RSHIFT_EQ:
4383 code = RSHIFT_EXPR;
4384 break;
4385 case CPP_AND_EQ:
4386 code = BIT_AND_EXPR;
4387 break;
4388 case CPP_XOR_EQ:
4389 code = BIT_XOR_EXPR;
4390 break;
4391 case CPP_OR_EQ:
4392 code = BIT_IOR_EXPR;
4393 break;
4394 default:
4395 return lhs;
4397 c_parser_consume_token (parser);
4398 rhs = c_parser_expr_no_commas (parser, NULL);
4399 rhs = default_function_array_conversion (rhs);
4400 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4401 if (code == NOP_EXPR)
4402 ret.original_code = MODIFY_EXPR;
4403 else
4405 TREE_NO_WARNING (ret.value) = 1;
4406 ret.original_code = ERROR_MARK;
4408 return ret;
4411 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4412 is not NULL then it is an Objective-C message expression which is
4413 the primary-expression starting the expression as an initializer.
4415 conditional-expression:
4416 logical-OR-expression
4417 logical-OR-expression ? expression : conditional-expression
4419 GNU extensions:
4421 conditional-expression:
4422 logical-OR-expression ? : conditional-expression
4425 static struct c_expr
4426 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4428 struct c_expr cond, exp1, exp2, ret;
4429 location_t cond_loc;
4431 gcc_assert (!after || c_dialect_objc ());
4433 cond_loc = c_parser_peek_token (parser)->location;
4434 cond = c_parser_binary_expression (parser, after);
4436 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4437 return cond;
4438 cond = default_function_array_conversion (cond);
4439 c_parser_consume_token (parser);
4440 if (c_parser_next_token_is (parser, CPP_COLON))
4442 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4443 "ISO C forbids omitting the middle term of a ?: expression");
4444 /* Make sure first operand is calculated only once. */
4445 exp1.value = save_expr (default_conversion (cond.value));
4446 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4447 skip_evaluation += cond.value == truthvalue_true_node;
4449 else
4451 cond.value
4452 = c_objc_common_truthvalue_conversion
4453 (cond_loc, default_conversion (cond.value));
4454 skip_evaluation += cond.value == truthvalue_false_node;
4455 exp1 = c_parser_expression_conv (parser);
4456 skip_evaluation += ((cond.value == truthvalue_true_node)
4457 - (cond.value == truthvalue_false_node));
4459 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4461 skip_evaluation -= cond.value == truthvalue_true_node;
4462 ret.value = error_mark_node;
4463 ret.original_code = ERROR_MARK;
4464 return ret;
4466 exp2 = c_parser_conditional_expression (parser, NULL);
4467 exp2 = default_function_array_conversion (exp2);
4468 skip_evaluation -= cond.value == truthvalue_true_node;
4469 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4470 ret.original_code = ERROR_MARK;
4471 return ret;
4474 /* Parse a binary expression; that is, a logical-OR-expression (C90
4475 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4476 an Objective-C message expression which is the primary-expression
4477 starting the expression as an initializer.
4479 multiplicative-expression:
4480 cast-expression
4481 multiplicative-expression * cast-expression
4482 multiplicative-expression / cast-expression
4483 multiplicative-expression % cast-expression
4485 additive-expression:
4486 multiplicative-expression
4487 additive-expression + multiplicative-expression
4488 additive-expression - multiplicative-expression
4490 shift-expression:
4491 additive-expression
4492 shift-expression << additive-expression
4493 shift-expression >> additive-expression
4495 relational-expression:
4496 shift-expression
4497 relational-expression < shift-expression
4498 relational-expression > shift-expression
4499 relational-expression <= shift-expression
4500 relational-expression >= shift-expression
4502 equality-expression:
4503 relational-expression
4504 equality-expression == relational-expression
4505 equality-expression != relational-expression
4507 AND-expression:
4508 equality-expression
4509 AND-expression & equality-expression
4511 exclusive-OR-expression:
4512 AND-expression
4513 exclusive-OR-expression ^ AND-expression
4515 inclusive-OR-expression:
4516 exclusive-OR-expression
4517 inclusive-OR-expression | exclusive-OR-expression
4519 logical-AND-expression:
4520 inclusive-OR-expression
4521 logical-AND-expression && inclusive-OR-expression
4523 logical-OR-expression:
4524 logical-AND-expression
4525 logical-OR-expression || logical-AND-expression
4528 static struct c_expr
4529 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4531 /* A binary expression is parsed using operator-precedence parsing,
4532 with the operands being cast expressions. All the binary
4533 operators are left-associative. Thus a binary expression is of
4534 form:
4536 E0 op1 E1 op2 E2 ...
4538 which we represent on a stack. On the stack, the precedence
4539 levels are strictly increasing. When a new operator is
4540 encountered of higher precedence than that at the top of the
4541 stack, it is pushed; its LHS is the top expression, and its RHS
4542 is everything parsed until it is popped. When a new operator is
4543 encountered with precedence less than or equal to that at the top
4544 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4545 by the result of the operation until the operator at the top of
4546 the stack has lower precedence than the new operator or there is
4547 only one element on the stack; then the top expression is the LHS
4548 of the new operator. In the case of logical AND and OR
4549 expressions, we also need to adjust skip_evaluation as
4550 appropriate when the operators are pushed and popped. */
4552 /* The precedence levels, where 0 is a dummy lowest level used for
4553 the bottom of the stack. */
4554 enum prec {
4555 PREC_NONE,
4556 PREC_LOGOR,
4557 PREC_LOGAND,
4558 PREC_BITOR,
4559 PREC_BITXOR,
4560 PREC_BITAND,
4561 PREC_EQ,
4562 PREC_REL,
4563 PREC_SHIFT,
4564 PREC_ADD,
4565 PREC_MULT,
4566 NUM_PRECS
4568 struct {
4569 /* The expression at this stack level. */
4570 struct c_expr expr;
4571 /* The precedence of the operator on its left, PREC_NONE at the
4572 bottom of the stack. */
4573 enum prec prec;
4574 /* The operation on its left. */
4575 enum tree_code op;
4576 } stack[NUM_PRECS];
4577 int sp;
4578 /* Location of the binary operator. */
4579 location_t binary_loc;
4580 #define POP \
4581 do { \
4582 switch (stack[sp].op) \
4584 case TRUTH_ANDIF_EXPR: \
4585 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4586 break; \
4587 case TRUTH_ORIF_EXPR: \
4588 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4589 break; \
4590 default: \
4591 break; \
4593 stack[sp - 1].expr \
4594 = default_function_array_conversion (stack[sp - 1].expr); \
4595 stack[sp].expr \
4596 = default_function_array_conversion (stack[sp].expr); \
4597 stack[sp - 1].expr = parser_build_binary_op (binary_loc, \
4598 stack[sp].op, \
4599 stack[sp - 1].expr, \
4600 stack[sp].expr); \
4601 sp--; \
4602 } while (0)
4603 gcc_assert (!after || c_dialect_objc ());
4604 stack[0].expr = c_parser_cast_expression (parser, after);
4605 stack[0].prec = PREC_NONE;
4606 sp = 0;
4607 while (true)
4609 enum prec oprec;
4610 enum tree_code ocode;
4611 if (parser->error)
4612 goto out;
4613 switch (c_parser_peek_token (parser)->type)
4615 case CPP_MULT:
4616 oprec = PREC_MULT;
4617 ocode = MULT_EXPR;
4618 break;
4619 case CPP_DIV:
4620 oprec = PREC_MULT;
4621 ocode = TRUNC_DIV_EXPR;
4622 break;
4623 case CPP_MOD:
4624 oprec = PREC_MULT;
4625 ocode = TRUNC_MOD_EXPR;
4626 break;
4627 case CPP_PLUS:
4628 oprec = PREC_ADD;
4629 ocode = PLUS_EXPR;
4630 break;
4631 case CPP_MINUS:
4632 oprec = PREC_ADD;
4633 ocode = MINUS_EXPR;
4634 break;
4635 case CPP_LSHIFT:
4636 oprec = PREC_SHIFT;
4637 ocode = LSHIFT_EXPR;
4638 break;
4639 case CPP_RSHIFT:
4640 oprec = PREC_SHIFT;
4641 ocode = RSHIFT_EXPR;
4642 break;
4643 case CPP_LESS:
4644 oprec = PREC_REL;
4645 ocode = LT_EXPR;
4646 break;
4647 case CPP_GREATER:
4648 oprec = PREC_REL;
4649 ocode = GT_EXPR;
4650 break;
4651 case CPP_LESS_EQ:
4652 oprec = PREC_REL;
4653 ocode = LE_EXPR;
4654 break;
4655 case CPP_GREATER_EQ:
4656 oprec = PREC_REL;
4657 ocode = GE_EXPR;
4658 break;
4659 case CPP_EQ_EQ:
4660 oprec = PREC_EQ;
4661 ocode = EQ_EXPR;
4662 break;
4663 case CPP_NOT_EQ:
4664 oprec = PREC_EQ;
4665 ocode = NE_EXPR;
4666 break;
4667 case CPP_AND:
4668 oprec = PREC_BITAND;
4669 ocode = BIT_AND_EXPR;
4670 break;
4671 case CPP_XOR:
4672 oprec = PREC_BITXOR;
4673 ocode = BIT_XOR_EXPR;
4674 break;
4675 case CPP_OR:
4676 oprec = PREC_BITOR;
4677 ocode = BIT_IOR_EXPR;
4678 break;
4679 case CPP_AND_AND:
4680 oprec = PREC_LOGAND;
4681 ocode = TRUTH_ANDIF_EXPR;
4682 break;
4683 case CPP_OR_OR:
4684 oprec = PREC_LOGOR;
4685 ocode = TRUTH_ORIF_EXPR;
4686 break;
4687 default:
4688 /* Not a binary operator, so end of the binary
4689 expression. */
4690 goto out;
4692 binary_loc = c_parser_peek_token (parser)->location;
4693 c_parser_consume_token (parser);
4694 while (oprec <= stack[sp].prec)
4695 POP;
4696 switch (ocode)
4698 case TRUTH_ANDIF_EXPR:
4699 stack[sp].expr
4700 = default_function_array_conversion (stack[sp].expr);
4701 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4702 (binary_loc, default_conversion (stack[sp].expr.value));
4703 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4704 break;
4705 case TRUTH_ORIF_EXPR:
4706 stack[sp].expr
4707 = default_function_array_conversion (stack[sp].expr);
4708 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4709 (binary_loc, default_conversion (stack[sp].expr.value));
4710 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4711 break;
4712 default:
4713 break;
4715 sp++;
4716 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4717 stack[sp].prec = oprec;
4718 stack[sp].op = ocode;
4720 out:
4721 while (sp > 0)
4722 POP;
4723 return stack[0].expr;
4724 #undef POP
4727 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4728 NULL then it is an Objective-C message expression which is the
4729 primary-expression starting the expression as an initializer.
4731 cast-expression:
4732 unary-expression
4733 ( type-name ) unary-expression
4736 static struct c_expr
4737 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4739 gcc_assert (!after || c_dialect_objc ());
4740 if (after)
4741 return c_parser_postfix_expression_after_primary (parser, *after);
4742 /* If the expression begins with a parenthesized type name, it may
4743 be either a cast or a compound literal; we need to see whether
4744 the next character is '{' to tell the difference. If not, it is
4745 an unary expression. */
4746 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4747 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4749 struct c_type_name *type_name;
4750 struct c_expr ret;
4751 struct c_expr expr;
4752 c_parser_consume_token (parser);
4753 type_name = c_parser_type_name (parser);
4754 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4755 if (type_name == NULL)
4757 ret.value = error_mark_node;
4758 ret.original_code = ERROR_MARK;
4759 return ret;
4762 /* Save casted types in the function's used types hash table. */
4763 used_types_insert (type_name->specs->type);
4765 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4766 return c_parser_postfix_expression_after_paren_type (parser,
4767 type_name);
4768 expr = c_parser_cast_expression (parser, NULL);
4769 expr = default_function_array_conversion (expr);
4770 ret.value = c_cast_expr (type_name, expr.value);
4771 ret.original_code = ERROR_MARK;
4772 return ret;
4774 else
4775 return c_parser_unary_expression (parser);
4778 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4780 unary-expression:
4781 postfix-expression
4782 ++ unary-expression
4783 -- unary-expression
4784 unary-operator cast-expression
4785 sizeof unary-expression
4786 sizeof ( type-name )
4788 unary-operator: one of
4789 & * + - ~ !
4791 GNU extensions:
4793 unary-expression:
4794 __alignof__ unary-expression
4795 __alignof__ ( type-name )
4796 && identifier
4798 unary-operator: one of
4799 __extension__ __real__ __imag__
4801 In addition, the GNU syntax treats ++ and -- as unary operators, so
4802 they may be applied to cast expressions with errors for non-lvalues
4803 given later. */
4805 static struct c_expr
4806 c_parser_unary_expression (c_parser *parser)
4808 int ext;
4809 struct c_expr ret, op;
4810 location_t loc = c_parser_peek_token (parser)->location;
4811 switch (c_parser_peek_token (parser)->type)
4813 case CPP_PLUS_PLUS:
4814 c_parser_consume_token (parser);
4815 op = c_parser_cast_expression (parser, NULL);
4816 op = default_function_array_conversion (op);
4817 return parser_build_unary_op (PREINCREMENT_EXPR, op, loc);
4818 case CPP_MINUS_MINUS:
4819 c_parser_consume_token (parser);
4820 op = c_parser_cast_expression (parser, NULL);
4821 op = default_function_array_conversion (op);
4822 return parser_build_unary_op (PREDECREMENT_EXPR, op, loc);
4823 case CPP_AND:
4824 c_parser_consume_token (parser);
4825 return parser_build_unary_op (ADDR_EXPR,
4826 c_parser_cast_expression (parser, NULL),
4827 loc);
4828 case CPP_MULT:
4829 c_parser_consume_token (parser);
4830 op = c_parser_cast_expression (parser, NULL);
4831 op = default_function_array_conversion (op);
4832 ret.value = build_indirect_ref (op.value, "unary *", loc);
4833 ret.original_code = ERROR_MARK;
4834 return ret;
4835 case CPP_PLUS:
4836 if (!c_dialect_objc () && !in_system_header)
4837 warning_at (c_parser_peek_token (parser)->location,
4838 OPT_Wtraditional,
4839 "traditional C rejects the unary plus operator");
4840 c_parser_consume_token (parser);
4841 op = c_parser_cast_expression (parser, NULL);
4842 op = default_function_array_conversion (op);
4843 return parser_build_unary_op (CONVERT_EXPR, op, loc);
4844 case CPP_MINUS:
4845 c_parser_consume_token (parser);
4846 op = c_parser_cast_expression (parser, NULL);
4847 op = default_function_array_conversion (op);
4848 return parser_build_unary_op (NEGATE_EXPR, op, loc);
4849 case CPP_COMPL:
4850 c_parser_consume_token (parser);
4851 op = c_parser_cast_expression (parser, NULL);
4852 op = default_function_array_conversion (op);
4853 return parser_build_unary_op (BIT_NOT_EXPR, op, loc);
4854 case CPP_NOT:
4855 c_parser_consume_token (parser);
4856 op = c_parser_cast_expression (parser, NULL);
4857 op = default_function_array_conversion (op);
4858 return parser_build_unary_op (TRUTH_NOT_EXPR, op, loc);
4859 case CPP_AND_AND:
4860 /* Refer to the address of a label as a pointer. */
4861 c_parser_consume_token (parser);
4862 if (c_parser_next_token_is (parser, CPP_NAME))
4864 ret.value = finish_label_address_expr
4865 (c_parser_peek_token (parser)->value, loc);
4866 c_parser_consume_token (parser);
4868 else
4870 c_parser_error (parser, "expected identifier");
4871 ret.value = error_mark_node;
4873 ret.original_code = ERROR_MARK;
4874 return ret;
4875 case CPP_KEYWORD:
4876 switch (c_parser_peek_token (parser)->keyword)
4878 case RID_SIZEOF:
4879 return c_parser_sizeof_expression (parser);
4880 case RID_ALIGNOF:
4881 return c_parser_alignof_expression (parser);
4882 case RID_EXTENSION:
4883 c_parser_consume_token (parser);
4884 ext = disable_extension_diagnostics ();
4885 ret = c_parser_cast_expression (parser, NULL);
4886 restore_extension_diagnostics (ext);
4887 return ret;
4888 case RID_REALPART:
4889 c_parser_consume_token (parser);
4890 op = c_parser_cast_expression (parser, NULL);
4891 op = default_function_array_conversion (op);
4892 return parser_build_unary_op (REALPART_EXPR, op, loc);
4893 case RID_IMAGPART:
4894 c_parser_consume_token (parser);
4895 op = c_parser_cast_expression (parser, NULL);
4896 op = default_function_array_conversion (op);
4897 return parser_build_unary_op (IMAGPART_EXPR, op, loc);
4898 default:
4899 return c_parser_postfix_expression (parser);
4901 default:
4902 return c_parser_postfix_expression (parser);
4906 /* Parse a sizeof expression. */
4908 static struct c_expr
4909 c_parser_sizeof_expression (c_parser *parser)
4911 struct c_expr expr;
4912 location_t expr_loc;
4913 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4914 c_parser_consume_token (parser);
4915 skip_evaluation++;
4916 in_sizeof++;
4917 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4918 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4920 /* Either sizeof ( type-name ) or sizeof unary-expression
4921 starting with a compound literal. */
4922 struct c_type_name *type_name;
4923 c_parser_consume_token (parser);
4924 expr_loc = c_parser_peek_token (parser)->location;
4925 type_name = c_parser_type_name (parser);
4926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4927 if (type_name == NULL)
4929 struct c_expr ret;
4930 skip_evaluation--;
4931 in_sizeof--;
4932 ret.value = error_mark_node;
4933 ret.original_code = ERROR_MARK;
4934 return ret;
4936 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4938 expr = c_parser_postfix_expression_after_paren_type (parser,
4939 type_name);
4940 goto sizeof_expr;
4942 /* sizeof ( type-name ). */
4943 skip_evaluation--;
4944 in_sizeof--;
4945 if (type_name->declarator->kind == cdk_array
4946 && type_name->declarator->u.array.vla_unspec_p)
4948 /* C99 6.7.5.2p4 */
4949 error_at (expr_loc,
4950 "%<[*]%> not allowed in other than a declaration");
4952 return c_expr_sizeof_type (type_name);
4954 else
4956 expr_loc = c_parser_peek_token (parser)->location;
4957 expr = c_parser_unary_expression (parser);
4958 sizeof_expr:
4959 skip_evaluation--;
4960 in_sizeof--;
4961 if (TREE_CODE (expr.value) == COMPONENT_REF
4962 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4963 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
4964 return c_expr_sizeof_expr (expr);
4968 /* Parse an alignof expression. */
4970 static struct c_expr
4971 c_parser_alignof_expression (c_parser *parser)
4973 struct c_expr expr;
4974 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4975 c_parser_consume_token (parser);
4976 skip_evaluation++;
4977 in_alignof++;
4978 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4979 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4981 /* Either __alignof__ ( type-name ) or __alignof__
4982 unary-expression starting with a compound literal. */
4983 struct c_type_name *type_name;
4984 struct c_expr ret;
4985 c_parser_consume_token (parser);
4986 type_name = c_parser_type_name (parser);
4987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4988 if (type_name == NULL)
4990 struct c_expr ret;
4991 skip_evaluation--;
4992 in_alignof--;
4993 ret.value = error_mark_node;
4994 ret.original_code = ERROR_MARK;
4995 return ret;
4997 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4999 expr = c_parser_postfix_expression_after_paren_type (parser,
5000 type_name);
5001 goto alignof_expr;
5003 /* alignof ( type-name ). */
5004 skip_evaluation--;
5005 in_alignof--;
5006 ret.value = c_alignof (groktypename (type_name));
5007 ret.original_code = ERROR_MARK;
5008 return ret;
5010 else
5012 struct c_expr ret;
5013 expr = c_parser_unary_expression (parser);
5014 alignof_expr:
5015 skip_evaluation--;
5016 in_alignof--;
5017 ret.value = c_alignof_expr (expr.value);
5018 ret.original_code = ERROR_MARK;
5019 return ret;
5023 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5025 postfix-expression:
5026 primary-expression
5027 postfix-expression [ expression ]
5028 postfix-expression ( argument-expression-list[opt] )
5029 postfix-expression . identifier
5030 postfix-expression -> identifier
5031 postfix-expression ++
5032 postfix-expression --
5033 ( type-name ) { initializer-list }
5034 ( type-name ) { initializer-list , }
5036 argument-expression-list:
5037 argument-expression
5038 argument-expression-list , argument-expression
5040 primary-expression:
5041 identifier
5042 constant
5043 string-literal
5044 ( expression )
5046 GNU extensions:
5048 primary-expression:
5049 __func__
5050 (treated as a keyword in GNU C)
5051 __FUNCTION__
5052 __PRETTY_FUNCTION__
5053 ( compound-statement )
5054 __builtin_va_arg ( assignment-expression , type-name )
5055 __builtin_offsetof ( type-name , offsetof-member-designator )
5056 __builtin_choose_expr ( assignment-expression ,
5057 assignment-expression ,
5058 assignment-expression )
5059 __builtin_types_compatible_p ( type-name , type-name )
5061 offsetof-member-designator:
5062 identifier
5063 offsetof-member-designator . identifier
5064 offsetof-member-designator [ expression ]
5066 Objective-C:
5068 primary-expression:
5069 [ objc-receiver objc-message-args ]
5070 @selector ( objc-selector-arg )
5071 @protocol ( identifier )
5072 @encode ( type-name )
5073 objc-string-literal
5076 static struct c_expr
5077 c_parser_postfix_expression (c_parser *parser)
5079 struct c_expr expr, e1, e2, e3;
5080 struct c_type_name *t1, *t2;
5081 location_t loc;
5082 switch (c_parser_peek_token (parser)->type)
5084 case CPP_NUMBER:
5085 case CPP_CHAR:
5086 case CPP_CHAR16:
5087 case CPP_CHAR32:
5088 case CPP_WCHAR:
5089 expr.value = c_parser_peek_token (parser)->value;
5090 expr.original_code = ERROR_MARK;
5091 c_parser_consume_token (parser);
5092 break;
5093 case CPP_STRING:
5094 case CPP_STRING16:
5095 case CPP_STRING32:
5096 case CPP_WSTRING:
5097 expr.value = c_parser_peek_token (parser)->value;
5098 expr.original_code = STRING_CST;
5099 c_parser_consume_token (parser);
5100 break;
5101 case CPP_OBJC_STRING:
5102 gcc_assert (c_dialect_objc ());
5103 expr.value
5104 = objc_build_string_object (c_parser_peek_token (parser)->value);
5105 expr.original_code = ERROR_MARK;
5106 c_parser_consume_token (parser);
5107 break;
5108 case CPP_NAME:
5109 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5111 c_parser_error (parser, "expected expression");
5112 expr.value = error_mark_node;
5113 expr.original_code = ERROR_MARK;
5114 break;
5117 tree id = c_parser_peek_token (parser)->value;
5118 location_t loc = c_parser_peek_token (parser)->location;
5119 c_parser_consume_token (parser);
5120 expr.value = build_external_ref (id,
5121 (c_parser_peek_token (parser)->type
5122 == CPP_OPEN_PAREN), loc);
5123 expr.original_code = ERROR_MARK;
5125 break;
5126 case CPP_OPEN_PAREN:
5127 /* A parenthesized expression, statement expression or compound
5128 literal. */
5129 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5131 /* A statement expression. */
5132 tree stmt;
5133 location_t here = c_parser_peek_token (parser)->location;
5134 c_parser_consume_token (parser);
5135 c_parser_consume_token (parser);
5136 if (cur_stmt_list == NULL)
5138 error_at (here, "braced-group within expression allowed "
5139 "only inside a function");
5140 parser->error = true;
5141 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5143 expr.value = error_mark_node;
5144 expr.original_code = ERROR_MARK;
5145 break;
5147 stmt = c_begin_stmt_expr ();
5148 c_parser_compound_statement_nostart (parser);
5149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5150 "expected %<)%>");
5151 pedwarn (here, OPT_pedantic,
5152 "ISO C forbids braced-groups within expressions");
5153 expr.value = c_finish_stmt_expr (stmt);
5154 expr.original_code = ERROR_MARK;
5156 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5158 /* A compound literal. ??? Can we actually get here rather
5159 than going directly to
5160 c_parser_postfix_expression_after_paren_type from
5161 elsewhere? */
5162 struct c_type_name *type_name;
5163 c_parser_consume_token (parser);
5164 type_name = c_parser_type_name (parser);
5165 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5166 "expected %<)%>");
5167 if (type_name == NULL)
5169 expr.value = error_mark_node;
5170 expr.original_code = ERROR_MARK;
5172 else
5173 expr = c_parser_postfix_expression_after_paren_type (parser,
5174 type_name);
5176 else
5178 /* A parenthesized expression. */
5179 c_parser_consume_token (parser);
5180 expr = c_parser_expression (parser);
5181 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5182 TREE_NO_WARNING (expr.value) = 1;
5183 expr.original_code = ERROR_MARK;
5184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5185 "expected %<)%>");
5187 break;
5188 case CPP_KEYWORD:
5189 switch (c_parser_peek_token (parser)->keyword)
5191 case RID_FUNCTION_NAME:
5192 case RID_PRETTY_FUNCTION_NAME:
5193 case RID_C99_FUNCTION_NAME:
5194 expr.value = fname_decl (c_parser_peek_token (parser)->location,
5195 c_parser_peek_token (parser)->keyword,
5196 c_parser_peek_token (parser)->value);
5197 expr.original_code = ERROR_MARK;
5198 c_parser_consume_token (parser);
5199 break;
5200 case RID_VA_ARG:
5201 c_parser_consume_token (parser);
5202 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5204 expr.value = error_mark_node;
5205 expr.original_code = ERROR_MARK;
5206 break;
5208 e1 = c_parser_expr_no_commas (parser, NULL);
5209 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5211 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5212 expr.value = error_mark_node;
5213 expr.original_code = ERROR_MARK;
5214 break;
5216 t1 = c_parser_type_name (parser);
5217 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5218 "expected %<)%>");
5219 if (t1 == NULL)
5221 expr.value = error_mark_node;
5222 expr.original_code = ERROR_MARK;
5224 else
5226 expr.value = build_va_arg (e1.value, groktypename (t1));
5227 expr.original_code = ERROR_MARK;
5229 break;
5230 case RID_OFFSETOF:
5231 c_parser_consume_token (parser);
5232 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5234 expr.value = error_mark_node;
5235 expr.original_code = ERROR_MARK;
5236 break;
5238 t1 = c_parser_type_name (parser);
5239 if (t1 == NULL)
5241 expr.value = error_mark_node;
5242 expr.original_code = ERROR_MARK;
5243 break;
5245 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5247 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5248 expr.value = error_mark_node;
5249 expr.original_code = ERROR_MARK;
5250 break;
5253 tree type = groktypename (t1);
5254 tree offsetof_ref;
5255 if (type == error_mark_node)
5256 offsetof_ref = error_mark_node;
5257 else
5258 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5259 /* Parse the second argument to __builtin_offsetof. We
5260 must have one identifier, and beyond that we want to
5261 accept sub structure and sub array references. */
5262 if (c_parser_next_token_is (parser, CPP_NAME))
5264 offsetof_ref = build_component_ref
5265 (offsetof_ref, c_parser_peek_token (parser)->value);
5266 c_parser_consume_token (parser);
5267 while (c_parser_next_token_is (parser, CPP_DOT)
5268 || c_parser_next_token_is (parser,
5269 CPP_OPEN_SQUARE))
5271 if (c_parser_next_token_is (parser, CPP_DOT))
5273 c_parser_consume_token (parser);
5274 if (c_parser_next_token_is_not (parser,
5275 CPP_NAME))
5277 c_parser_error (parser, "expected identifier");
5278 break;
5280 offsetof_ref = build_component_ref
5281 (offsetof_ref,
5282 c_parser_peek_token (parser)->value);
5283 c_parser_consume_token (parser);
5285 else
5287 tree idx;
5288 loc = c_parser_peek_token (parser)->location;
5289 c_parser_consume_token (parser);
5290 idx = c_parser_expression (parser).value;
5291 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5292 "expected %<]%>");
5293 offsetof_ref = build_array_ref (offsetof_ref, idx, loc);
5297 else
5298 c_parser_error (parser, "expected identifier");
5299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5300 "expected %<)%>");
5301 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5302 expr.original_code = ERROR_MARK;
5304 break;
5305 case RID_CHOOSE_EXPR:
5306 c_parser_consume_token (parser);
5307 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5309 expr.value = error_mark_node;
5310 expr.original_code = ERROR_MARK;
5311 break;
5313 loc = c_parser_peek_token (parser)->location;
5314 e1 = c_parser_expr_no_commas (parser, NULL);
5315 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5317 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5318 expr.value = error_mark_node;
5319 expr.original_code = ERROR_MARK;
5320 break;
5322 e2 = c_parser_expr_no_commas (parser, NULL);
5323 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5326 expr.value = error_mark_node;
5327 expr.original_code = ERROR_MARK;
5328 break;
5330 e3 = c_parser_expr_no_commas (parser, NULL);
5331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5332 "expected %<)%>");
5334 tree c;
5336 c = fold (e1.value);
5337 if (TREE_CODE (c) != INTEGER_CST)
5338 error_at (loc,
5339 "first argument to %<__builtin_choose_expr%> not"
5340 " a constant");
5341 expr = integer_zerop (c) ? e3 : e2;
5343 break;
5344 case RID_TYPES_COMPATIBLE_P:
5345 c_parser_consume_token (parser);
5346 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5348 expr.value = error_mark_node;
5349 expr.original_code = ERROR_MARK;
5350 break;
5352 t1 = c_parser_type_name (parser);
5353 if (t1 == NULL)
5355 expr.value = error_mark_node;
5356 expr.original_code = ERROR_MARK;
5357 break;
5359 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5362 expr.value = error_mark_node;
5363 expr.original_code = ERROR_MARK;
5364 break;
5366 t2 = c_parser_type_name (parser);
5367 if (t2 == NULL)
5369 expr.value = error_mark_node;
5370 expr.original_code = ERROR_MARK;
5371 break;
5373 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5374 "expected %<)%>");
5376 tree e1, e2;
5378 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5379 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5381 expr.value = comptypes (e1, e2)
5382 ? build_int_cst (NULL_TREE, 1)
5383 : build_int_cst (NULL_TREE, 0);
5384 expr.original_code = ERROR_MARK;
5386 break;
5387 case RID_AT_SELECTOR:
5388 gcc_assert (c_dialect_objc ());
5389 c_parser_consume_token (parser);
5390 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5392 expr.value = error_mark_node;
5393 expr.original_code = ERROR_MARK;
5394 break;
5397 tree sel = c_parser_objc_selector_arg (parser);
5398 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5399 "expected %<)%>");
5400 expr.value = objc_build_selector_expr (sel);
5401 expr.original_code = ERROR_MARK;
5403 break;
5404 case RID_AT_PROTOCOL:
5405 gcc_assert (c_dialect_objc ());
5406 c_parser_consume_token (parser);
5407 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5409 expr.value = error_mark_node;
5410 expr.original_code = ERROR_MARK;
5411 break;
5413 if (c_parser_next_token_is_not (parser, CPP_NAME))
5415 c_parser_error (parser, "expected identifier");
5416 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5417 expr.value = error_mark_node;
5418 expr.original_code = ERROR_MARK;
5419 break;
5422 tree id = c_parser_peek_token (parser)->value;
5423 c_parser_consume_token (parser);
5424 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5425 "expected %<)%>");
5426 expr.value = objc_build_protocol_expr (id);
5427 expr.original_code = ERROR_MARK;
5429 break;
5430 case RID_AT_ENCODE:
5431 /* Extension to support C-structures in the archiver. */
5432 gcc_assert (c_dialect_objc ());
5433 c_parser_consume_token (parser);
5434 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5436 expr.value = error_mark_node;
5437 expr.original_code = ERROR_MARK;
5438 break;
5440 t1 = c_parser_type_name (parser);
5441 if (t1 == NULL)
5443 expr.value = error_mark_node;
5444 expr.original_code = ERROR_MARK;
5445 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5446 break;
5448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5449 "expected %<)%>");
5451 tree type = groktypename (t1);
5452 expr.value = objc_build_encode_expr (type);
5453 expr.original_code = ERROR_MARK;
5455 break;
5456 default:
5457 c_parser_error (parser, "expected expression");
5458 expr.value = error_mark_node;
5459 expr.original_code = ERROR_MARK;
5460 break;
5462 break;
5463 case CPP_OPEN_SQUARE:
5464 if (c_dialect_objc ())
5466 tree receiver, args;
5467 c_parser_consume_token (parser);
5468 receiver = c_parser_objc_receiver (parser);
5469 args = c_parser_objc_message_args (parser);
5470 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5471 "expected %<]%>");
5472 expr.value = objc_build_message_expr (build_tree_list (receiver,
5473 args));
5474 expr.original_code = ERROR_MARK;
5475 break;
5477 /* Else fall through to report error. */
5478 default:
5479 c_parser_error (parser, "expected expression");
5480 expr.value = error_mark_node;
5481 expr.original_code = ERROR_MARK;
5482 break;
5484 return c_parser_postfix_expression_after_primary (parser, expr);
5487 /* Parse a postfix expression after a parenthesized type name: the
5488 brace-enclosed initializer of a compound literal, possibly followed
5489 by some postfix operators. This is separate because it is not
5490 possible to tell until after the type name whether a cast
5491 expression has a cast or a compound literal, or whether the operand
5492 of sizeof is a parenthesized type name or starts with a compound
5493 literal. */
5495 static struct c_expr
5496 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5497 struct c_type_name *type_name)
5499 tree type;
5500 struct c_expr init;
5501 struct c_expr expr;
5502 location_t start_loc;
5503 start_init (NULL_TREE, NULL, 0);
5504 type = groktypename (type_name);
5505 start_loc = c_parser_peek_token (parser)->location;
5506 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5508 error_at (start_loc, "compound literal has variable size");
5509 type = error_mark_node;
5511 init = c_parser_braced_init (parser, type, false);
5512 finish_init ();
5513 maybe_warn_string_init (type, init);
5515 if (!flag_isoc99)
5516 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
5517 expr.value = build_compound_literal (type, init.value);
5518 expr.original_code = ERROR_MARK;
5519 return c_parser_postfix_expression_after_primary (parser, expr);
5522 /* Parse a postfix expression after the initial primary or compound
5523 literal; that is, parse a series of postfix operators. */
5525 static struct c_expr
5526 c_parser_postfix_expression_after_primary (c_parser *parser,
5527 struct c_expr expr)
5529 tree ident, idx, exprlist;
5530 location_t loc = c_parser_peek_token (parser)->location;
5531 while (true)
5533 switch (c_parser_peek_token (parser)->type)
5535 case CPP_OPEN_SQUARE:
5536 /* Array reference. */
5537 loc = c_parser_peek_token (parser)->location;
5538 c_parser_consume_token (parser);
5539 idx = c_parser_expression (parser).value;
5540 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5541 "expected %<]%>");
5542 expr.value = build_array_ref (expr.value, idx, loc);
5543 expr.original_code = ERROR_MARK;
5544 break;
5545 case CPP_OPEN_PAREN:
5546 /* Function call. */
5547 c_parser_consume_token (parser);
5548 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5549 exprlist = NULL_TREE;
5550 else
5551 exprlist = c_parser_expr_list (parser, true);
5552 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5553 "expected %<)%>");
5554 expr.value = build_function_call (expr.value, exprlist);
5555 expr.original_code = ERROR_MARK;
5556 if (warn_disallowed_functions)
5557 warn_if_disallowed_function_p (expr.value);
5558 break;
5559 case CPP_DOT:
5560 /* Structure element reference. */
5561 c_parser_consume_token (parser);
5562 expr = default_function_array_conversion (expr);
5563 if (c_parser_next_token_is (parser, CPP_NAME))
5564 ident = c_parser_peek_token (parser)->value;
5565 else
5567 c_parser_error (parser, "expected identifier");
5568 expr.value = error_mark_node;
5569 expr.original_code = ERROR_MARK;
5570 return expr;
5572 c_parser_consume_token (parser);
5573 expr.value = build_component_ref (expr.value, ident);
5574 expr.original_code = ERROR_MARK;
5575 break;
5576 case CPP_DEREF:
5577 /* Structure element reference. */
5578 c_parser_consume_token (parser);
5579 expr = default_function_array_conversion (expr);
5580 if (c_parser_next_token_is (parser, CPP_NAME))
5581 ident = c_parser_peek_token (parser)->value;
5582 else
5584 c_parser_error (parser, "expected identifier");
5585 expr.value = error_mark_node;
5586 expr.original_code = ERROR_MARK;
5587 return expr;
5589 c_parser_consume_token (parser);
5590 expr.value = build_component_ref (build_indirect_ref (expr.value,
5591 "->", loc),
5592 ident);
5593 expr.original_code = ERROR_MARK;
5594 break;
5595 case CPP_PLUS_PLUS:
5596 /* Postincrement. */
5597 c_parser_consume_token (parser);
5598 expr = default_function_array_conversion (expr);
5599 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5600 expr.original_code = ERROR_MARK;
5601 break;
5602 case CPP_MINUS_MINUS:
5603 /* Postdecrement. */
5604 c_parser_consume_token (parser);
5605 expr = default_function_array_conversion (expr);
5606 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5607 expr.original_code = ERROR_MARK;
5608 break;
5609 default:
5610 return expr;
5615 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5617 expression:
5618 assignment-expression
5619 expression , assignment-expression
5622 static struct c_expr
5623 c_parser_expression (c_parser *parser)
5625 struct c_expr expr;
5626 expr = c_parser_expr_no_commas (parser, NULL);
5627 while (c_parser_next_token_is (parser, CPP_COMMA))
5629 struct c_expr next;
5630 c_parser_consume_token (parser);
5631 next = c_parser_expr_no_commas (parser, NULL);
5632 next = default_function_array_conversion (next);
5633 expr.value = build_compound_expr (expr.value, next.value);
5634 expr.original_code = COMPOUND_EXPR;
5636 return expr;
5639 /* Parse an expression and convert functions or arrays to
5640 pointers. */
5642 static struct c_expr
5643 c_parser_expression_conv (c_parser *parser)
5645 struct c_expr expr;
5646 expr = c_parser_expression (parser);
5647 expr = default_function_array_conversion (expr);
5648 return expr;
5651 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5652 functions and arrays to pointers.
5654 nonempty-expr-list:
5655 assignment-expression
5656 nonempty-expr-list , assignment-expression
5659 static tree
5660 c_parser_expr_list (c_parser *parser, bool convert_p)
5662 struct c_expr expr;
5663 tree ret, cur;
5664 expr = c_parser_expr_no_commas (parser, NULL);
5665 if (convert_p)
5666 expr = default_function_array_conversion (expr);
5667 ret = cur = build_tree_list (NULL_TREE, expr.value);
5668 while (c_parser_next_token_is (parser, CPP_COMMA))
5670 c_parser_consume_token (parser);
5671 expr = c_parser_expr_no_commas (parser, NULL);
5672 if (convert_p)
5673 expr = default_function_array_conversion (expr);
5674 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5676 return ret;
5680 /* Parse Objective-C-specific constructs. */
5682 /* Parse an objc-class-definition.
5684 objc-class-definition:
5685 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5686 objc-class-instance-variables[opt] objc-methodprotolist @end
5687 @implementation identifier objc-superclass[opt]
5688 objc-class-instance-variables[opt]
5689 @interface identifier ( identifier ) objc-protocol-refs[opt]
5690 objc-methodprotolist @end
5691 @implementation identifier ( identifier )
5693 objc-superclass:
5694 : identifier
5696 "@interface identifier (" must start "@interface identifier (
5697 identifier ) ...": objc-methodprotolist in the first production may
5698 not start with a parenthesized identifier as a declarator of a data
5699 definition with no declaration specifiers if the objc-superclass,
5700 objc-protocol-refs and objc-class-instance-variables are omitted. */
5702 static void
5703 c_parser_objc_class_definition (c_parser *parser)
5705 bool iface_p;
5706 tree id1;
5707 tree superclass;
5708 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5709 iface_p = true;
5710 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5711 iface_p = false;
5712 else
5713 gcc_unreachable ();
5714 c_parser_consume_token (parser);
5715 if (c_parser_next_token_is_not (parser, CPP_NAME))
5717 c_parser_error (parser, "expected identifier");
5718 return;
5720 id1 = c_parser_peek_token (parser)->value;
5721 c_parser_consume_token (parser);
5722 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5724 tree id2;
5725 tree proto = NULL_TREE;
5726 c_parser_consume_token (parser);
5727 if (c_parser_next_token_is_not (parser, CPP_NAME))
5729 c_parser_error (parser, "expected identifier");
5730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5731 return;
5733 id2 = c_parser_peek_token (parser)->value;
5734 c_parser_consume_token (parser);
5735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5736 if (!iface_p)
5738 objc_start_category_implementation (id1, id2);
5739 return;
5741 if (c_parser_next_token_is (parser, CPP_LESS))
5742 proto = c_parser_objc_protocol_refs (parser);
5743 objc_start_category_interface (id1, id2, proto);
5744 c_parser_objc_methodprotolist (parser);
5745 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5746 objc_finish_interface ();
5747 return;
5749 if (c_parser_next_token_is (parser, CPP_COLON))
5751 c_parser_consume_token (parser);
5752 if (c_parser_next_token_is_not (parser, CPP_NAME))
5754 c_parser_error (parser, "expected identifier");
5755 return;
5757 superclass = c_parser_peek_token (parser)->value;
5758 c_parser_consume_token (parser);
5760 else
5761 superclass = NULL_TREE;
5762 if (iface_p)
5764 tree proto = NULL_TREE;
5765 if (c_parser_next_token_is (parser, CPP_LESS))
5766 proto = c_parser_objc_protocol_refs (parser);
5767 objc_start_class_interface (id1, superclass, proto);
5769 else
5770 objc_start_class_implementation (id1, superclass);
5771 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5772 c_parser_objc_class_instance_variables (parser);
5773 if (iface_p)
5775 objc_continue_interface ();
5776 c_parser_objc_methodprotolist (parser);
5777 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5778 objc_finish_interface ();
5780 else
5782 objc_continue_implementation ();
5783 return;
5787 /* Parse objc-class-instance-variables.
5789 objc-class-instance-variables:
5790 { objc-instance-variable-decl-list[opt] }
5792 objc-instance-variable-decl-list:
5793 objc-visibility-spec
5794 objc-instance-variable-decl ;
5796 objc-instance-variable-decl-list objc-visibility-spec
5797 objc-instance-variable-decl-list objc-instance-variable-decl ;
5798 objc-instance-variable-decl-list ;
5800 objc-visibility-spec:
5801 @private
5802 @protected
5803 @public
5805 objc-instance-variable-decl:
5806 struct-declaration
5809 static void
5810 c_parser_objc_class_instance_variables (c_parser *parser)
5812 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5813 c_parser_consume_token (parser);
5814 while (c_parser_next_token_is_not (parser, CPP_EOF))
5816 tree decls;
5817 /* Parse any stray semicolon. */
5818 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5820 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
5821 "extra semicolon in struct or union specified");
5822 c_parser_consume_token (parser);
5823 continue;
5825 /* Stop if at the end of the instance variables. */
5826 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5828 c_parser_consume_token (parser);
5829 break;
5831 /* Parse any objc-visibility-spec. */
5832 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
5834 c_parser_consume_token (parser);
5835 objc_set_visibility (2);
5836 continue;
5838 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
5840 c_parser_consume_token (parser);
5841 objc_set_visibility (0);
5842 continue;
5844 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
5846 c_parser_consume_token (parser);
5847 objc_set_visibility (1);
5848 continue;
5850 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5852 c_parser_pragma (parser, pragma_external);
5853 continue;
5856 /* Parse some comma-separated declarations. */
5857 decls = c_parser_struct_declaration (parser);
5859 /* Comma-separated instance variables are chained together in
5860 reverse order; add them one by one. */
5861 tree ivar = nreverse (decls);
5862 for (; ivar; ivar = TREE_CHAIN (ivar))
5863 objc_add_instance_variable (copy_node (ivar));
5865 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5869 /* Parse an objc-class-declaration.
5871 objc-class-declaration:
5872 @class identifier-list ;
5875 static void
5876 c_parser_objc_class_declaration (c_parser *parser)
5878 tree list = NULL_TREE;
5879 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
5880 c_parser_consume_token (parser);
5881 /* Any identifiers, including those declared as type names, are OK
5882 here. */
5883 while (true)
5885 tree id;
5886 if (c_parser_next_token_is_not (parser, CPP_NAME))
5888 c_parser_error (parser, "expected identifier");
5889 break;
5891 id = c_parser_peek_token (parser)->value;
5892 list = chainon (list, build_tree_list (NULL_TREE, id));
5893 c_parser_consume_token (parser);
5894 if (c_parser_next_token_is (parser, CPP_COMMA))
5895 c_parser_consume_token (parser);
5896 else
5897 break;
5899 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5900 objc_declare_class (list);
5903 /* Parse an objc-alias-declaration.
5905 objc-alias-declaration:
5906 @compatibility_alias identifier identifier ;
5909 static void
5910 c_parser_objc_alias_declaration (c_parser *parser)
5912 tree id1, id2;
5913 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5914 c_parser_consume_token (parser);
5915 if (c_parser_next_token_is_not (parser, CPP_NAME))
5917 c_parser_error (parser, "expected identifier");
5918 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5919 return;
5921 id1 = c_parser_peek_token (parser)->value;
5922 c_parser_consume_token (parser);
5923 if (c_parser_next_token_is_not (parser, CPP_NAME))
5925 c_parser_error (parser, "expected identifier");
5926 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5927 return;
5929 id2 = c_parser_peek_token (parser)->value;
5930 c_parser_consume_token (parser);
5931 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5932 objc_declare_alias (id1, id2);
5935 /* Parse an objc-protocol-definition.
5937 objc-protocol-definition:
5938 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5939 @protocol identifier-list ;
5941 "@protocol identifier ;" should be resolved as "@protocol
5942 identifier-list ;": objc-methodprotolist may not start with a
5943 semicolon in the first alternative if objc-protocol-refs are
5944 omitted. */
5946 static void
5947 c_parser_objc_protocol_definition (c_parser *parser)
5949 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5950 c_parser_consume_token (parser);
5951 if (c_parser_next_token_is_not (parser, CPP_NAME))
5953 c_parser_error (parser, "expected identifier");
5954 return;
5956 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5957 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5959 tree list = NULL_TREE;
5960 /* Any identifiers, including those declared as type names, are
5961 OK here. */
5962 while (true)
5964 tree id;
5965 if (c_parser_next_token_is_not (parser, CPP_NAME))
5967 c_parser_error (parser, "expected identifier");
5968 break;
5970 id = c_parser_peek_token (parser)->value;
5971 list = chainon (list, build_tree_list (NULL_TREE, id));
5972 c_parser_consume_token (parser);
5973 if (c_parser_next_token_is (parser, CPP_COMMA))
5974 c_parser_consume_token (parser);
5975 else
5976 break;
5978 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5979 objc_declare_protocols (list);
5981 else
5983 tree id = c_parser_peek_token (parser)->value;
5984 tree proto = NULL_TREE;
5985 c_parser_consume_token (parser);
5986 if (c_parser_next_token_is (parser, CPP_LESS))
5987 proto = c_parser_objc_protocol_refs (parser);
5988 parser->objc_pq_context = true;
5989 objc_start_protocol (id, proto);
5990 c_parser_objc_methodprotolist (parser);
5991 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5992 parser->objc_pq_context = false;
5993 objc_finish_interface ();
5997 /* Parse an objc-method-type.
5999 objc-method-type:
6004 static enum tree_code
6005 c_parser_objc_method_type (c_parser *parser)
6007 switch (c_parser_peek_token (parser)->type)
6009 case CPP_PLUS:
6010 c_parser_consume_token (parser);
6011 return PLUS_EXPR;
6012 case CPP_MINUS:
6013 c_parser_consume_token (parser);
6014 return MINUS_EXPR;
6015 default:
6016 gcc_unreachable ();
6020 /* Parse an objc-method-definition.
6022 objc-method-definition:
6023 objc-method-type objc-method-decl ;[opt] compound-statement
6026 static void
6027 c_parser_objc_method_definition (c_parser *parser)
6029 enum tree_code type = c_parser_objc_method_type (parser);
6030 tree decl;
6031 objc_set_method_type (type);
6032 parser->objc_pq_context = true;
6033 decl = c_parser_objc_method_decl (parser);
6034 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6036 c_parser_consume_token (parser);
6037 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6038 "extra semicolon in method definition specified");
6040 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6042 c_parser_error (parser, "expected %<{%>");
6043 return;
6045 parser->objc_pq_context = false;
6046 objc_start_method_definition (decl);
6047 add_stmt (c_parser_compound_statement (parser));
6048 objc_finish_method_definition (current_function_decl);
6051 /* Parse an objc-methodprotolist.
6053 objc-methodprotolist:
6054 empty
6055 objc-methodprotolist objc-methodproto
6056 objc-methodprotolist declaration
6057 objc-methodprotolist ;
6059 The declaration is a data definition, which may be missing
6060 declaration specifiers under the same rules and diagnostics as
6061 other data definitions outside functions, and the stray semicolon
6062 is diagnosed the same way as a stray semicolon outside a
6063 function. */
6065 static void
6066 c_parser_objc_methodprotolist (c_parser *parser)
6068 while (true)
6070 /* The list is terminated by @end. */
6071 switch (c_parser_peek_token (parser)->type)
6073 case CPP_SEMICOLON:
6074 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6075 "ISO C does not allow extra %<;%> outside of a function");
6076 c_parser_consume_token (parser);
6077 break;
6078 case CPP_PLUS:
6079 case CPP_MINUS:
6080 c_parser_objc_methodproto (parser);
6081 break;
6082 case CPP_PRAGMA:
6083 c_parser_pragma (parser, pragma_external);
6084 break;
6085 case CPP_EOF:
6086 return;
6087 default:
6088 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6089 return;
6090 c_parser_declaration_or_fndef (parser, false, true, false, true);
6091 break;
6096 /* Parse an objc-methodproto.
6098 objc-methodproto:
6099 objc-method-type objc-method-decl ;
6102 static void
6103 c_parser_objc_methodproto (c_parser *parser)
6105 enum tree_code type = c_parser_objc_method_type (parser);
6106 tree decl;
6107 objc_set_method_type (type);
6108 /* Remember protocol qualifiers in prototypes. */
6109 parser->objc_pq_context = true;
6110 decl = c_parser_objc_method_decl (parser);
6111 /* Forget protocol qualifiers here. */
6112 parser->objc_pq_context = false;
6113 objc_add_method_declaration (decl);
6114 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6117 /* Parse an objc-method-decl.
6119 objc-method-decl:
6120 ( objc-type-name ) objc-selector
6121 objc-selector
6122 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6123 objc-keyword-selector objc-optparmlist
6125 objc-keyword-selector:
6126 objc-keyword-decl
6127 objc-keyword-selector objc-keyword-decl
6129 objc-keyword-decl:
6130 objc-selector : ( objc-type-name ) identifier
6131 objc-selector : identifier
6132 : ( objc-type-name ) identifier
6133 : identifier
6135 objc-optparmlist:
6136 objc-optparms objc-optellipsis
6138 objc-optparms:
6139 empty
6140 objc-opt-parms , parameter-declaration
6142 objc-optellipsis:
6143 empty
6144 , ...
6147 static tree
6148 c_parser_objc_method_decl (c_parser *parser)
6150 tree type = NULL_TREE;
6151 tree sel;
6152 tree parms = NULL_TREE;
6153 bool ellipsis = false;
6155 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6157 c_parser_consume_token (parser);
6158 type = c_parser_objc_type_name (parser);
6159 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6161 sel = c_parser_objc_selector (parser);
6162 /* If there is no selector, or a colon follows, we have an
6163 objc-keyword-selector. If there is a selector, and a colon does
6164 not follow, that selector ends the objc-method-decl. */
6165 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6167 tree tsel = sel;
6168 tree list = NULL_TREE;
6169 while (true)
6171 tree atype = NULL_TREE, id, keyworddecl;
6172 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6173 break;
6174 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6176 c_parser_consume_token (parser);
6177 atype = c_parser_objc_type_name (parser);
6178 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6179 "expected %<)%>");
6181 if (c_parser_next_token_is_not (parser, CPP_NAME))
6183 c_parser_error (parser, "expected identifier");
6184 return error_mark_node;
6186 id = c_parser_peek_token (parser)->value;
6187 c_parser_consume_token (parser);
6188 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6189 list = chainon (list, keyworddecl);
6190 tsel = c_parser_objc_selector (parser);
6191 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6192 break;
6194 /* Parse the optional parameter list. Optional Objective-C
6195 method parameters follow the C syntax, and may include '...'
6196 to denote a variable number of arguments. */
6197 parms = make_node (TREE_LIST);
6198 while (c_parser_next_token_is (parser, CPP_COMMA))
6200 struct c_parm *parm;
6201 c_parser_consume_token (parser);
6202 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6204 ellipsis = true;
6205 c_parser_consume_token (parser);
6206 break;
6208 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6209 if (parm == NULL)
6210 break;
6211 parms = chainon (parms,
6212 build_tree_list (NULL_TREE, grokparm (parm)));
6214 sel = list;
6216 return objc_build_method_signature (type, sel, parms, ellipsis);
6219 /* Parse an objc-type-name.
6221 objc-type-name:
6222 objc-type-qualifiers[opt] type-name
6223 objc-type-qualifiers[opt]
6225 objc-type-qualifiers:
6226 objc-type-qualifier
6227 objc-type-qualifiers objc-type-qualifier
6229 objc-type-qualifier: one of
6230 in out inout bycopy byref oneway
6233 static tree
6234 c_parser_objc_type_name (c_parser *parser)
6236 tree quals = NULL_TREE;
6237 struct c_type_name *type_name = NULL;
6238 tree type = NULL_TREE;
6239 while (true)
6241 c_token *token = c_parser_peek_token (parser);
6242 if (token->type == CPP_KEYWORD
6243 && (token->keyword == RID_IN
6244 || token->keyword == RID_OUT
6245 || token->keyword == RID_INOUT
6246 || token->keyword == RID_BYCOPY
6247 || token->keyword == RID_BYREF
6248 || token->keyword == RID_ONEWAY))
6250 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6251 c_parser_consume_token (parser);
6253 else
6254 break;
6256 if (c_parser_next_token_starts_typename (parser))
6257 type_name = c_parser_type_name (parser);
6258 if (type_name)
6259 type = groktypename (type_name);
6260 return build_tree_list (quals, type);
6263 /* Parse objc-protocol-refs.
6265 objc-protocol-refs:
6266 < identifier-list >
6269 static tree
6270 c_parser_objc_protocol_refs (c_parser *parser)
6272 tree list = NULL_TREE;
6273 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6274 c_parser_consume_token (parser);
6275 /* Any identifiers, including those declared as type names, are OK
6276 here. */
6277 while (true)
6279 tree id;
6280 if (c_parser_next_token_is_not (parser, CPP_NAME))
6282 c_parser_error (parser, "expected identifier");
6283 break;
6285 id = c_parser_peek_token (parser)->value;
6286 list = chainon (list, build_tree_list (NULL_TREE, id));
6287 c_parser_consume_token (parser);
6288 if (c_parser_next_token_is (parser, CPP_COMMA))
6289 c_parser_consume_token (parser);
6290 else
6291 break;
6293 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6294 return list;
6297 /* Parse an objc-try-catch-statement.
6299 objc-try-catch-statement:
6300 @try compound-statement objc-catch-list[opt]
6301 @try compound-statement objc-catch-list[opt] @finally compound-statement
6303 objc-catch-list:
6304 @catch ( parameter-declaration ) compound-statement
6305 objc-catch-list @catch ( parameter-declaration ) compound-statement
6308 static void
6309 c_parser_objc_try_catch_statement (c_parser *parser)
6311 location_t loc;
6312 tree stmt;
6313 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6314 c_parser_consume_token (parser);
6315 loc = c_parser_peek_token (parser)->location;
6316 stmt = c_parser_compound_statement (parser);
6317 objc_begin_try_stmt (loc, stmt);
6318 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6320 struct c_parm *parm;
6321 c_parser_consume_token (parser);
6322 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6323 break;
6324 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6325 if (parm == NULL)
6327 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6328 break;
6330 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6331 objc_begin_catch_clause (grokparm (parm));
6332 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6333 c_parser_compound_statement_nostart (parser);
6334 objc_finish_catch_clause ();
6336 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6338 location_t finloc;
6339 tree finstmt;
6340 c_parser_consume_token (parser);
6341 finloc = c_parser_peek_token (parser)->location;
6342 finstmt = c_parser_compound_statement (parser);
6343 objc_build_finally_clause (finloc, finstmt);
6345 objc_finish_try_stmt ();
6348 /* Parse an objc-synchronized-statement.
6350 objc-synchronized-statement:
6351 @synchronized ( expression ) compound-statement
6354 static void
6355 c_parser_objc_synchronized_statement (c_parser *parser)
6357 location_t loc;
6358 tree expr, stmt;
6359 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6360 c_parser_consume_token (parser);
6361 loc = c_parser_peek_token (parser)->location;
6362 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6364 expr = c_parser_expression (parser).value;
6365 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6367 else
6368 expr = error_mark_node;
6369 stmt = c_parser_compound_statement (parser);
6370 objc_build_synchronized (loc, expr, stmt);
6373 /* Parse an objc-selector; return NULL_TREE without an error if the
6374 next token is not an objc-selector.
6376 objc-selector:
6377 identifier
6378 one of
6379 enum struct union if else while do for switch case default
6380 break continue return goto asm sizeof typeof __alignof
6381 unsigned long const short volatile signed restrict _Complex
6382 in out inout bycopy byref oneway int char float double void _Bool
6384 ??? Why this selection of keywords but not, for example, storage
6385 class specifiers? */
6387 static tree
6388 c_parser_objc_selector (c_parser *parser)
6390 c_token *token = c_parser_peek_token (parser);
6391 tree value = token->value;
6392 if (token->type == CPP_NAME)
6394 c_parser_consume_token (parser);
6395 return value;
6397 if (token->type != CPP_KEYWORD)
6398 return NULL_TREE;
6399 switch (token->keyword)
6401 case RID_ENUM:
6402 case RID_STRUCT:
6403 case RID_UNION:
6404 case RID_IF:
6405 case RID_ELSE:
6406 case RID_WHILE:
6407 case RID_DO:
6408 case RID_FOR:
6409 case RID_SWITCH:
6410 case RID_CASE:
6411 case RID_DEFAULT:
6412 case RID_BREAK:
6413 case RID_CONTINUE:
6414 case RID_RETURN:
6415 case RID_GOTO:
6416 case RID_ASM:
6417 case RID_SIZEOF:
6418 case RID_TYPEOF:
6419 case RID_ALIGNOF:
6420 case RID_UNSIGNED:
6421 case RID_LONG:
6422 case RID_CONST:
6423 case RID_SHORT:
6424 case RID_VOLATILE:
6425 case RID_SIGNED:
6426 case RID_RESTRICT:
6427 case RID_COMPLEX:
6428 case RID_IN:
6429 case RID_OUT:
6430 case RID_INOUT:
6431 case RID_BYCOPY:
6432 case RID_BYREF:
6433 case RID_ONEWAY:
6434 case RID_INT:
6435 case RID_CHAR:
6436 case RID_FLOAT:
6437 case RID_DOUBLE:
6438 case RID_VOID:
6439 case RID_BOOL:
6440 c_parser_consume_token (parser);
6441 return value;
6442 default:
6443 return NULL_TREE;
6447 /* Parse an objc-selector-arg.
6449 objc-selector-arg:
6450 objc-selector
6451 objc-keywordname-list
6453 objc-keywordname-list:
6454 objc-keywordname
6455 objc-keywordname-list objc-keywordname
6457 objc-keywordname:
6458 objc-selector :
6462 static tree
6463 c_parser_objc_selector_arg (c_parser *parser)
6465 tree sel = c_parser_objc_selector (parser);
6466 tree list = NULL_TREE;
6467 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6468 return sel;
6469 while (true)
6471 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6472 return list;
6473 list = chainon (list, build_tree_list (sel, NULL_TREE));
6474 sel = c_parser_objc_selector (parser);
6475 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6476 break;
6478 return list;
6481 /* Parse an objc-receiver.
6483 objc-receiver:
6484 expression
6485 class-name
6486 type-name
6489 static tree
6490 c_parser_objc_receiver (c_parser *parser)
6492 if (c_parser_peek_token (parser)->type == CPP_NAME
6493 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6494 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6496 tree id = c_parser_peek_token (parser)->value;
6497 c_parser_consume_token (parser);
6498 return objc_get_class_reference (id);
6500 return c_parser_expression (parser).value;
6503 /* Parse objc-message-args.
6505 objc-message-args:
6506 objc-selector
6507 objc-keywordarg-list
6509 objc-keywordarg-list:
6510 objc-keywordarg
6511 objc-keywordarg-list objc-keywordarg
6513 objc-keywordarg:
6514 objc-selector : objc-keywordexpr
6515 : objc-keywordexpr
6518 static tree
6519 c_parser_objc_message_args (c_parser *parser)
6521 tree sel = c_parser_objc_selector (parser);
6522 tree list = NULL_TREE;
6523 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6524 return sel;
6525 while (true)
6527 tree keywordexpr;
6528 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6529 return list;
6530 keywordexpr = c_parser_objc_keywordexpr (parser);
6531 list = chainon (list, build_tree_list (sel, keywordexpr));
6532 sel = c_parser_objc_selector (parser);
6533 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6534 break;
6536 return list;
6539 /* Parse an objc-keywordexpr.
6541 objc-keywordexpr:
6542 nonempty-expr-list
6545 static tree
6546 c_parser_objc_keywordexpr (c_parser *parser)
6548 tree list = c_parser_expr_list (parser, true);
6549 if (TREE_CHAIN (list) == NULL_TREE)
6551 /* Just return the expression, remove a level of
6552 indirection. */
6553 return TREE_VALUE (list);
6555 else
6557 /* We have a comma expression, we will collapse later. */
6558 return list;
6563 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6564 should be considered, statements. ALLOW_STMT is true if we're within
6565 the context of a function and such pragmas are to be allowed. Returns
6566 true if we actually parsed such a pragma. */
6568 static bool
6569 c_parser_pragma (c_parser *parser, enum pragma_context context)
6571 unsigned int id;
6573 id = c_parser_peek_token (parser)->pragma_kind;
6574 gcc_assert (id != PRAGMA_NONE);
6576 switch (id)
6578 case PRAGMA_OMP_BARRIER:
6579 if (context != pragma_compound)
6581 if (context == pragma_stmt)
6582 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6583 "used in compound statements");
6584 goto bad_stmt;
6586 c_parser_omp_barrier (parser);
6587 return false;
6589 case PRAGMA_OMP_FLUSH:
6590 if (context != pragma_compound)
6592 if (context == pragma_stmt)
6593 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6594 "used in compound statements");
6595 goto bad_stmt;
6597 c_parser_omp_flush (parser);
6598 return false;
6600 case PRAGMA_OMP_TASKWAIT:
6601 if (context != pragma_compound)
6603 if (context == pragma_stmt)
6604 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
6605 "used in compound statements");
6606 goto bad_stmt;
6608 c_parser_omp_taskwait (parser);
6609 return false;
6611 case PRAGMA_OMP_THREADPRIVATE:
6612 c_parser_omp_threadprivate (parser);
6613 return false;
6615 case PRAGMA_OMP_SECTION:
6616 error_at (c_parser_peek_token (parser)->location,
6617 "%<#pragma omp section%> may only be used in "
6618 "%<#pragma omp sections%> construct");
6619 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6620 return false;
6622 case PRAGMA_GCC_PCH_PREPROCESS:
6623 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6624 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6625 return false;
6627 default:
6628 if (id < PRAGMA_FIRST_EXTERNAL)
6630 if (context == pragma_external)
6632 bad_stmt:
6633 c_parser_error (parser, "expected declaration specifiers");
6634 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6635 return false;
6637 c_parser_omp_construct (parser);
6638 return true;
6640 break;
6643 c_parser_consume_pragma (parser);
6644 c_invoke_pragma_handler (id);
6646 /* Skip to EOL, but suppress any error message. Those will have been
6647 generated by the handler routine through calling error, as opposed
6648 to calling c_parser_error. */
6649 parser->error = true;
6650 c_parser_skip_to_pragma_eol (parser);
6652 return false;
6655 /* The interface the pragma parsers have to the lexer. */
6657 enum cpp_ttype
6658 pragma_lex (tree *value)
6660 c_token *tok = c_parser_peek_token (the_parser);
6661 enum cpp_ttype ret = tok->type;
6663 *value = tok->value;
6664 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6665 ret = CPP_EOF;
6666 else
6668 if (ret == CPP_KEYWORD)
6669 ret = CPP_NAME;
6670 c_parser_consume_token (the_parser);
6673 return ret;
6676 static void
6677 c_parser_pragma_pch_preprocess (c_parser *parser)
6679 tree name = NULL;
6681 c_parser_consume_pragma (parser);
6682 if (c_parser_next_token_is (parser, CPP_STRING))
6684 name = c_parser_peek_token (parser)->value;
6685 c_parser_consume_token (parser);
6687 else
6688 c_parser_error (parser, "expected string literal");
6689 c_parser_skip_to_pragma_eol (parser);
6691 if (name)
6692 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6695 /* OpenMP 2.5 parsing routines. */
6697 /* Returns name of the next clause.
6698 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6699 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6700 returned and the token is consumed. */
6702 static pragma_omp_clause
6703 c_parser_omp_clause_name (c_parser *parser)
6705 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6707 if (c_parser_next_token_is_keyword (parser, RID_IF))
6708 result = PRAGMA_OMP_CLAUSE_IF;
6709 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6710 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6711 else if (c_parser_next_token_is (parser, CPP_NAME))
6713 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6715 switch (p[0])
6717 case 'c':
6718 if (!strcmp ("collapse", p))
6719 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
6720 else if (!strcmp ("copyin", p))
6721 result = PRAGMA_OMP_CLAUSE_COPYIN;
6722 else if (!strcmp ("copyprivate", p))
6723 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6724 break;
6725 case 'f':
6726 if (!strcmp ("firstprivate", p))
6727 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6728 break;
6729 case 'l':
6730 if (!strcmp ("lastprivate", p))
6731 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6732 break;
6733 case 'n':
6734 if (!strcmp ("nowait", p))
6735 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6736 else if (!strcmp ("num_threads", p))
6737 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6738 break;
6739 case 'o':
6740 if (!strcmp ("ordered", p))
6741 result = PRAGMA_OMP_CLAUSE_ORDERED;
6742 break;
6743 case 'p':
6744 if (!strcmp ("private", p))
6745 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6746 break;
6747 case 'r':
6748 if (!strcmp ("reduction", p))
6749 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6750 break;
6751 case 's':
6752 if (!strcmp ("schedule", p))
6753 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6754 else if (!strcmp ("shared", p))
6755 result = PRAGMA_OMP_CLAUSE_SHARED;
6756 break;
6757 case 'u':
6758 if (!strcmp ("untied", p))
6759 result = PRAGMA_OMP_CLAUSE_UNTIED;
6760 break;
6764 if (result != PRAGMA_OMP_CLAUSE_NONE)
6765 c_parser_consume_token (parser);
6767 return result;
6770 /* Validate that a clause of the given type does not already exist. */
6772 static void
6773 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
6774 const char *name)
6776 tree c;
6778 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6779 if (OMP_CLAUSE_CODE (c) == code)
6781 error ("too many %qs clauses", name);
6782 break;
6786 /* OpenMP 2.5:
6787 variable-list:
6788 identifier
6789 variable-list , identifier
6791 If KIND is nonzero, create the appropriate node and install the decl
6792 in OMP_CLAUSE_DECL and add the node to the head of the list.
6794 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6795 return the list created. */
6797 static tree
6798 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6799 tree list)
6801 if (c_parser_next_token_is_not (parser, CPP_NAME)
6802 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6803 c_parser_error (parser, "expected identifier");
6805 while (c_parser_next_token_is (parser, CPP_NAME)
6806 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6808 tree t = lookup_name (c_parser_peek_token (parser)->value);
6810 if (t == NULL_TREE)
6811 undeclared_variable (c_parser_peek_token (parser)->value,
6812 c_parser_peek_token (parser)->location);
6813 else if (t == error_mark_node)
6815 else if (kind != 0)
6817 tree u = build_omp_clause (kind);
6818 OMP_CLAUSE_DECL (u) = t;
6819 OMP_CLAUSE_CHAIN (u) = list;
6820 list = u;
6822 else
6823 list = tree_cons (t, NULL_TREE, list);
6825 c_parser_consume_token (parser);
6827 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6828 break;
6830 c_parser_consume_token (parser);
6833 return list;
6836 /* Similarly, but expect leading and trailing parenthesis. This is a very
6837 common case for omp clauses. */
6839 static tree
6840 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
6841 tree list)
6843 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6845 list = c_parser_omp_variable_list (parser, kind, list);
6846 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6848 return list;
6851 /* OpenMP 3.0:
6852 collapse ( constant-expression ) */
6854 static tree
6855 c_parser_omp_clause_collapse (c_parser *parser, tree list)
6857 tree c, num = error_mark_node;
6858 HOST_WIDE_INT n;
6859 location_t loc;
6861 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
6863 loc = c_parser_peek_token (parser)->location;
6864 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6866 num = c_parser_expr_no_commas (parser, NULL).value;
6867 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6869 if (num == error_mark_node)
6870 return list;
6871 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
6872 || !host_integerp (num, 0)
6873 || (n = tree_low_cst (num, 0)) <= 0
6874 || (int) n != n)
6876 error_at (loc,
6877 "collapse argument needs positive constant integer expression");
6878 return list;
6880 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
6881 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
6882 OMP_CLAUSE_CHAIN (c) = list;
6883 return c;
6886 /* OpenMP 2.5:
6887 copyin ( variable-list ) */
6889 static tree
6890 c_parser_omp_clause_copyin (c_parser *parser, tree list)
6892 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
6895 /* OpenMP 2.5:
6896 copyprivate ( variable-list ) */
6898 static tree
6899 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
6901 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
6904 /* OpenMP 2.5:
6905 default ( shared | none ) */
6907 static tree
6908 c_parser_omp_clause_default (c_parser *parser, tree list)
6910 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
6911 tree c;
6913 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6914 return list;
6915 if (c_parser_next_token_is (parser, CPP_NAME))
6917 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6919 switch (p[0])
6921 case 'n':
6922 if (strcmp ("none", p) != 0)
6923 goto invalid_kind;
6924 kind = OMP_CLAUSE_DEFAULT_NONE;
6925 break;
6927 case 's':
6928 if (strcmp ("shared", p) != 0)
6929 goto invalid_kind;
6930 kind = OMP_CLAUSE_DEFAULT_SHARED;
6931 break;
6933 default:
6934 goto invalid_kind;
6937 c_parser_consume_token (parser);
6939 else
6941 invalid_kind:
6942 c_parser_error (parser, "expected %<none%> or %<shared%>");
6944 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6946 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6947 return list;
6949 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
6950 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
6951 OMP_CLAUSE_CHAIN (c) = list;
6952 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
6954 return c;
6957 /* OpenMP 2.5:
6958 firstprivate ( variable-list ) */
6960 static tree
6961 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
6963 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
6966 /* OpenMP 2.5:
6967 if ( expression ) */
6969 static tree
6970 c_parser_omp_clause_if (c_parser *parser, tree list)
6972 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6974 tree t = c_parser_paren_condition (parser);
6975 tree c;
6977 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
6979 c = build_omp_clause (OMP_CLAUSE_IF);
6980 OMP_CLAUSE_IF_EXPR (c) = t;
6981 OMP_CLAUSE_CHAIN (c) = list;
6982 list = c;
6984 else
6985 c_parser_error (parser, "expected %<(%>");
6987 return list;
6990 /* OpenMP 2.5:
6991 lastprivate ( variable-list ) */
6993 static tree
6994 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
6996 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
6999 /* OpenMP 2.5:
7000 nowait */
7002 static tree
7003 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7005 tree c;
7007 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7009 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
7010 OMP_CLAUSE_CHAIN (c) = list;
7011 return c;
7014 /* OpenMP 2.5:
7015 num_threads ( expression ) */
7017 static tree
7018 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7020 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7022 location_t expr_loc = c_parser_peek_token (parser)->location;
7023 tree c, t = c_parser_expression (parser).value;
7025 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7027 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7029 c_parser_error (parser, "expected integer expression");
7030 return list;
7033 /* Attempt to statically determine when the number isn't positive. */
7034 c = fold_build2 (LE_EXPR, boolean_type_node, t,
7035 build_int_cst (TREE_TYPE (t), 0));
7036 if (c == boolean_true_node)
7038 warning_at (expr_loc, 0,
7039 "%<num_threads%> value must be positive");
7040 t = integer_one_node;
7043 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7045 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
7046 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7047 OMP_CLAUSE_CHAIN (c) = list;
7048 list = c;
7051 return list;
7054 /* OpenMP 2.5:
7055 ordered */
7057 static tree
7058 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7060 tree c;
7062 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7064 c = build_omp_clause (OMP_CLAUSE_ORDERED);
7065 OMP_CLAUSE_CHAIN (c) = list;
7066 return c;
7069 /* OpenMP 2.5:
7070 private ( variable-list ) */
7072 static tree
7073 c_parser_omp_clause_private (c_parser *parser, tree list)
7075 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7078 /* OpenMP 2.5:
7079 reduction ( reduction-operator : variable-list )
7081 reduction-operator:
7082 One of: + * - & ^ | && || */
7084 static tree
7085 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7087 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7089 enum tree_code code;
7091 switch (c_parser_peek_token (parser)->type)
7093 case CPP_PLUS:
7094 code = PLUS_EXPR;
7095 break;
7096 case CPP_MULT:
7097 code = MULT_EXPR;
7098 break;
7099 case CPP_MINUS:
7100 code = MINUS_EXPR;
7101 break;
7102 case CPP_AND:
7103 code = BIT_AND_EXPR;
7104 break;
7105 case CPP_XOR:
7106 code = BIT_XOR_EXPR;
7107 break;
7108 case CPP_OR:
7109 code = BIT_IOR_EXPR;
7110 break;
7111 case CPP_AND_AND:
7112 code = TRUTH_ANDIF_EXPR;
7113 break;
7114 case CPP_OR_OR:
7115 code = TRUTH_ORIF_EXPR;
7116 break;
7117 default:
7118 c_parser_error (parser,
7119 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7120 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7122 return list;
7124 c_parser_consume_token (parser);
7125 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7127 tree nl, c;
7129 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
7130 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7131 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7133 list = nl;
7135 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7137 return list;
7140 /* OpenMP 2.5:
7141 schedule ( schedule-kind )
7142 schedule ( schedule-kind , expression )
7144 schedule-kind:
7145 static | dynamic | guided | runtime | auto
7148 static tree
7149 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7151 tree c, t;
7153 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7154 return list;
7156 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7158 if (c_parser_next_token_is (parser, CPP_NAME))
7160 tree kind = c_parser_peek_token (parser)->value;
7161 const char *p = IDENTIFIER_POINTER (kind);
7163 switch (p[0])
7165 case 'd':
7166 if (strcmp ("dynamic", p) != 0)
7167 goto invalid_kind;
7168 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7169 break;
7171 case 'g':
7172 if (strcmp ("guided", p) != 0)
7173 goto invalid_kind;
7174 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7175 break;
7177 case 'r':
7178 if (strcmp ("runtime", p) != 0)
7179 goto invalid_kind;
7180 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7181 break;
7183 default:
7184 goto invalid_kind;
7187 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7188 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7189 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7190 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7191 else
7192 goto invalid_kind;
7194 c_parser_consume_token (parser);
7195 if (c_parser_next_token_is (parser, CPP_COMMA))
7197 location_t here;
7198 c_parser_consume_token (parser);
7200 here = c_parser_peek_token (parser)->location;
7201 t = c_parser_expr_no_commas (parser, NULL).value;
7203 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7204 error_at (here, "schedule %<runtime%> does not take "
7205 "a %<chunk_size%> parameter");
7206 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7207 error_at (here,
7208 "schedule %<auto%> does not take "
7209 "a %<chunk_size%> parameter");
7210 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7211 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7212 else
7213 c_parser_error (parser, "expected integer expression");
7215 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7217 else
7218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7219 "expected %<,%> or %<)%>");
7221 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7222 OMP_CLAUSE_CHAIN (c) = list;
7223 return c;
7225 invalid_kind:
7226 c_parser_error (parser, "invalid schedule kind");
7227 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7228 return list;
7231 /* OpenMP 2.5:
7232 shared ( variable-list ) */
7234 static tree
7235 c_parser_omp_clause_shared (c_parser *parser, tree list)
7237 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7240 /* OpenMP 3.0:
7241 untied */
7243 static tree
7244 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7246 tree c;
7248 /* FIXME: Should we allow duplicates? */
7249 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7251 c = build_omp_clause (OMP_CLAUSE_UNTIED);
7252 OMP_CLAUSE_CHAIN (c) = list;
7253 return c;
7256 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7257 is a bitmask in MASK. Return the list of clauses found; the result
7258 of clause default goes in *pdefault. */
7260 static tree
7261 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7262 const char *where)
7264 tree clauses = NULL;
7265 bool first = true;
7267 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7269 location_t here;
7270 pragma_omp_clause c_kind;
7271 const char *c_name;
7272 tree prev = clauses;
7274 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7275 c_parser_consume_token (parser);
7277 first = false;
7278 here = c_parser_peek_token (parser)->location;
7279 c_kind = c_parser_omp_clause_name (parser);
7281 switch (c_kind)
7283 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7284 clauses = c_parser_omp_clause_collapse (parser, clauses);
7285 c_name = "collapse";
7286 break;
7287 case PRAGMA_OMP_CLAUSE_COPYIN:
7288 clauses = c_parser_omp_clause_copyin (parser, clauses);
7289 c_name = "copyin";
7290 break;
7291 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7292 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7293 c_name = "copyprivate";
7294 break;
7295 case PRAGMA_OMP_CLAUSE_DEFAULT:
7296 clauses = c_parser_omp_clause_default (parser, clauses);
7297 c_name = "default";
7298 break;
7299 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7300 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7301 c_name = "firstprivate";
7302 break;
7303 case PRAGMA_OMP_CLAUSE_IF:
7304 clauses = c_parser_omp_clause_if (parser, clauses);
7305 c_name = "if";
7306 break;
7307 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7308 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7309 c_name = "lastprivate";
7310 break;
7311 case PRAGMA_OMP_CLAUSE_NOWAIT:
7312 clauses = c_parser_omp_clause_nowait (parser, clauses);
7313 c_name = "nowait";
7314 break;
7315 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7316 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7317 c_name = "num_threads";
7318 break;
7319 case PRAGMA_OMP_CLAUSE_ORDERED:
7320 clauses = c_parser_omp_clause_ordered (parser, clauses);
7321 c_name = "ordered";
7322 break;
7323 case PRAGMA_OMP_CLAUSE_PRIVATE:
7324 clauses = c_parser_omp_clause_private (parser, clauses);
7325 c_name = "private";
7326 break;
7327 case PRAGMA_OMP_CLAUSE_REDUCTION:
7328 clauses = c_parser_omp_clause_reduction (parser, clauses);
7329 c_name = "reduction";
7330 break;
7331 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7332 clauses = c_parser_omp_clause_schedule (parser, clauses);
7333 c_name = "schedule";
7334 break;
7335 case PRAGMA_OMP_CLAUSE_SHARED:
7336 clauses = c_parser_omp_clause_shared (parser, clauses);
7337 c_name = "shared";
7338 break;
7339 case PRAGMA_OMP_CLAUSE_UNTIED:
7340 clauses = c_parser_omp_clause_untied (parser, clauses);
7341 c_name = "untied";
7342 break;
7343 default:
7344 c_parser_error (parser, "expected %<#pragma omp%> clause");
7345 goto saw_error;
7348 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7350 /* Remove the invalid clause(s) from the list to avoid
7351 confusing the rest of the compiler. */
7352 clauses = prev;
7353 error_at (here, "%qs is not valid for %qs", c_name, where);
7357 saw_error:
7358 c_parser_skip_to_pragma_eol (parser);
7360 return c_finish_omp_clauses (clauses);
7363 /* OpenMP 2.5:
7364 structured-block:
7365 statement
7367 In practice, we're also interested in adding the statement to an
7368 outer node. So it is convenient if we work around the fact that
7369 c_parser_statement calls add_stmt. */
7371 static tree
7372 c_parser_omp_structured_block (c_parser *parser)
7374 tree stmt = push_stmt_list ();
7375 c_parser_statement (parser);
7376 return pop_stmt_list (stmt);
7379 /* OpenMP 2.5:
7380 # pragma omp atomic new-line
7381 expression-stmt
7383 expression-stmt:
7384 x binop= expr | x++ | ++x | x-- | --x
7385 binop:
7386 +, *, -, /, &, ^, |, <<, >>
7388 where x is an lvalue expression with scalar type. */
7390 static void
7391 c_parser_omp_atomic (c_parser *parser)
7393 tree lhs, rhs;
7394 tree stmt;
7395 enum tree_code code;
7396 struct c_expr rhs_expr;
7398 c_parser_skip_to_pragma_eol (parser);
7400 lhs = c_parser_unary_expression (parser).value;
7401 switch (TREE_CODE (lhs))
7403 case ERROR_MARK:
7404 saw_error:
7405 c_parser_skip_to_end_of_block_or_statement (parser);
7406 return;
7408 case PREINCREMENT_EXPR:
7409 case POSTINCREMENT_EXPR:
7410 lhs = TREE_OPERAND (lhs, 0);
7411 code = PLUS_EXPR;
7412 rhs = integer_one_node;
7413 break;
7415 case PREDECREMENT_EXPR:
7416 case POSTDECREMENT_EXPR:
7417 lhs = TREE_OPERAND (lhs, 0);
7418 code = MINUS_EXPR;
7419 rhs = integer_one_node;
7420 break;
7422 default:
7423 switch (c_parser_peek_token (parser)->type)
7425 case CPP_MULT_EQ:
7426 code = MULT_EXPR;
7427 break;
7428 case CPP_DIV_EQ:
7429 code = TRUNC_DIV_EXPR;
7430 break;
7431 case CPP_PLUS_EQ:
7432 code = PLUS_EXPR;
7433 break;
7434 case CPP_MINUS_EQ:
7435 code = MINUS_EXPR;
7436 break;
7437 case CPP_LSHIFT_EQ:
7438 code = LSHIFT_EXPR;
7439 break;
7440 case CPP_RSHIFT_EQ:
7441 code = RSHIFT_EXPR;
7442 break;
7443 case CPP_AND_EQ:
7444 code = BIT_AND_EXPR;
7445 break;
7446 case CPP_OR_EQ:
7447 code = BIT_IOR_EXPR;
7448 break;
7449 case CPP_XOR_EQ:
7450 code = BIT_XOR_EXPR;
7451 break;
7452 default:
7453 c_parser_error (parser,
7454 "invalid operator for %<#pragma omp atomic%>");
7455 goto saw_error;
7458 c_parser_consume_token (parser);
7459 rhs_expr = c_parser_expression (parser);
7460 rhs_expr = default_function_array_conversion (rhs_expr);
7461 rhs = rhs_expr.value;
7462 break;
7464 stmt = c_finish_omp_atomic (code, lhs, rhs);
7465 if (stmt != error_mark_node)
7466 add_stmt (stmt);
7467 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7471 /* OpenMP 2.5:
7472 # pragma omp barrier new-line
7475 static void
7476 c_parser_omp_barrier (c_parser *parser)
7478 c_parser_consume_pragma (parser);
7479 c_parser_skip_to_pragma_eol (parser);
7481 c_finish_omp_barrier ();
7484 /* OpenMP 2.5:
7485 # pragma omp critical [(name)] new-line
7486 structured-block
7489 static tree
7490 c_parser_omp_critical (c_parser *parser)
7492 tree stmt, name = NULL;
7494 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7496 c_parser_consume_token (parser);
7497 if (c_parser_next_token_is (parser, CPP_NAME))
7499 name = c_parser_peek_token (parser)->value;
7500 c_parser_consume_token (parser);
7501 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7503 else
7504 c_parser_error (parser, "expected identifier");
7506 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7507 c_parser_error (parser, "expected %<(%> or end of line");
7508 c_parser_skip_to_pragma_eol (parser);
7510 stmt = c_parser_omp_structured_block (parser);
7511 return c_finish_omp_critical (stmt, name);
7514 /* OpenMP 2.5:
7515 # pragma omp flush flush-vars[opt] new-line
7517 flush-vars:
7518 ( variable-list ) */
7520 static void
7521 c_parser_omp_flush (c_parser *parser)
7523 c_parser_consume_pragma (parser);
7524 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7525 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
7526 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7527 c_parser_error (parser, "expected %<(%> or end of line");
7528 c_parser_skip_to_pragma_eol (parser);
7530 c_finish_omp_flush ();
7533 /* Parse the restricted form of the for statement allowed by OpenMP.
7534 The real trick here is to determine the loop control variable early
7535 so that we can push a new decl if necessary to make it private. */
7537 static tree
7538 c_parser_omp_for_loop (c_parser *parser, tree clauses, tree *par_clauses)
7540 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7541 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
7542 location_t loc;
7543 bool fail = false, open_brace_parsed = false;
7544 int i, collapse = 1, nbraces = 0;
7546 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
7547 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
7548 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
7550 gcc_assert (collapse >= 1);
7552 declv = make_tree_vec (collapse);
7553 initv = make_tree_vec (collapse);
7554 condv = make_tree_vec (collapse);
7555 incrv = make_tree_vec (collapse);
7557 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7559 c_parser_error (parser, "for statement expected");
7560 return NULL;
7562 loc = c_parser_peek_token (parser)->location;
7563 c_parser_consume_token (parser);
7565 for (i = 0; i < collapse; i++)
7567 int bracecount = 0;
7569 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7570 goto pop_scopes;
7572 /* Parse the initialization declaration or expression. */
7573 if (c_parser_next_token_starts_declspecs (parser))
7575 if (i > 0)
7576 for_block
7577 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
7578 c_parser_declaration_or_fndef (parser, true, true, true, true);
7579 decl = check_for_loop_decls ();
7580 if (decl == NULL)
7581 goto error_init;
7582 if (DECL_INITIAL (decl) == error_mark_node)
7583 decl = error_mark_node;
7584 init = decl;
7586 else if (c_parser_next_token_is (parser, CPP_NAME)
7587 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7589 struct c_expr init_exp;
7591 decl = c_parser_postfix_expression (parser).value;
7593 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7595 init_exp = c_parser_expr_no_commas (parser, NULL);
7596 init_exp = default_function_array_conversion (init_exp);
7597 init = build_modify_expr (decl, NOP_EXPR, init_exp.value);
7598 init = c_process_expr_stmt (init);
7600 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7602 else
7604 error_init:
7605 c_parser_error (parser,
7606 "expected iteration declaration or initialization");
7607 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7608 "expected %<)%>");
7609 fail = true;
7610 goto parse_next;
7613 /* Parse the loop condition. */
7614 cond = NULL_TREE;
7615 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7617 location_t cond_loc = c_parser_peek_token (parser)->location;
7619 cond = c_parser_expression_conv (parser).value;
7620 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
7621 if (CAN_HAVE_LOCATION_P (cond))
7622 SET_EXPR_LOCATION (cond, cond_loc);
7624 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7626 /* Parse the increment expression. */
7627 incr = NULL_TREE;
7628 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7629 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7632 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
7633 fail = true;
7634 else
7636 TREE_VEC_ELT (declv, i) = decl;
7637 TREE_VEC_ELT (initv, i) = init;
7638 TREE_VEC_ELT (condv, i) = cond;
7639 TREE_VEC_ELT (incrv, i) = incr;
7642 parse_next:
7643 if (i == collapse - 1)
7644 break;
7646 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
7647 in between the collapsed for loops to be still considered perfectly
7648 nested. Hopefully the final version clarifies this.
7649 For now handle (multiple) {'s and empty statements. */
7652 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7654 c_parser_consume_token (parser);
7655 break;
7657 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7659 c_parser_consume_token (parser);
7660 bracecount++;
7662 else if (bracecount
7663 && c_parser_next_token_is (parser, CPP_SEMICOLON))
7664 c_parser_consume_token (parser);
7665 else
7667 c_parser_error (parser, "not enough perfectly nested loops");
7668 if (bracecount)
7670 open_brace_parsed = true;
7671 bracecount--;
7673 fail = true;
7674 collapse = 0;
7675 break;
7678 while (1);
7680 nbraces += bracecount;
7683 save_break = c_break_label;
7684 c_break_label = size_one_node;
7685 save_cont = c_cont_label;
7686 c_cont_label = NULL_TREE;
7687 body = push_stmt_list ();
7689 if (open_brace_parsed)
7691 stmt = c_begin_compound_stmt (true);
7692 c_parser_compound_statement_nostart (parser);
7693 add_stmt (c_end_compound_stmt (stmt, true));
7695 else
7696 add_stmt (c_parser_c99_block_statement (parser));
7697 if (c_cont_label)
7698 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7700 body = pop_stmt_list (body);
7701 c_break_label = save_break;
7702 c_cont_label = save_cont;
7704 while (nbraces)
7706 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7708 c_parser_consume_token (parser);
7709 nbraces--;
7711 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7712 c_parser_consume_token (parser);
7713 else
7715 c_parser_error (parser, "collapsed loops not perfectly nested");
7716 while (nbraces)
7718 stmt = c_begin_compound_stmt (true);
7719 add_stmt (body);
7720 c_parser_compound_statement_nostart (parser);
7721 body = c_end_compound_stmt (stmt, true);
7722 nbraces--;
7724 goto pop_scopes;
7728 /* Only bother calling c_finish_omp_for if we haven't already generated
7729 an error from the initialization parsing. */
7730 if (!fail)
7732 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
7733 if (stmt)
7735 if (par_clauses != NULL)
7737 tree *c;
7738 for (c = par_clauses; *c ; )
7739 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
7740 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
7741 c = &OMP_CLAUSE_CHAIN (*c);
7742 else
7744 for (i = 0; i < collapse; i++)
7745 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
7746 break;
7747 if (i == collapse)
7748 c = &OMP_CLAUSE_CHAIN (*c);
7749 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
7751 error_at (loc,
7752 "iteration variable %qD should not be firstprivate",
7753 OMP_CLAUSE_DECL (*c));
7754 *c = OMP_CLAUSE_CHAIN (*c);
7756 else
7758 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
7759 change it to shared (decl) in
7760 OMP_PARALLEL_CLAUSES. */
7761 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
7762 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
7763 OMP_CLAUSE_CHAIN (l) = clauses;
7764 clauses = l;
7765 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
7769 OMP_FOR_CLAUSES (stmt) = clauses;
7771 ret = stmt;
7773 pop_scopes:
7774 while (for_block)
7776 stmt = c_end_compound_stmt (TREE_VALUE (for_block), true);
7777 add_stmt (stmt);
7778 for_block = TREE_CHAIN (for_block);
7780 return ret;
7783 /* OpenMP 2.5:
7784 #pragma omp for for-clause[optseq] new-line
7785 for-loop
7788 #define OMP_FOR_CLAUSE_MASK \
7789 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7790 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7791 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7792 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7793 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7794 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7795 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
7796 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7798 static tree
7799 c_parser_omp_for (c_parser *parser)
7801 tree block, clauses, ret;
7803 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7804 "#pragma omp for");
7806 block = c_begin_compound_stmt (true);
7807 ret = c_parser_omp_for_loop (parser, clauses, NULL);
7808 block = c_end_compound_stmt (block, true);
7809 add_stmt (block);
7811 return ret;
7814 /* OpenMP 2.5:
7815 # pragma omp master new-line
7816 structured-block
7819 static tree
7820 c_parser_omp_master (c_parser *parser)
7822 c_parser_skip_to_pragma_eol (parser);
7823 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7826 /* OpenMP 2.5:
7827 # pragma omp ordered new-line
7828 structured-block
7831 static tree
7832 c_parser_omp_ordered (c_parser *parser)
7834 c_parser_skip_to_pragma_eol (parser);
7835 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
7838 /* OpenMP 2.5:
7840 section-scope:
7841 { section-sequence }
7843 section-sequence:
7844 section-directive[opt] structured-block
7845 section-sequence section-directive structured-block */
7847 static tree
7848 c_parser_omp_sections_scope (c_parser *parser)
7850 tree stmt, substmt;
7851 bool error_suppress = false;
7852 location_t loc;
7854 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7856 /* Avoid skipping until the end of the block. */
7857 parser->error = false;
7858 return NULL_TREE;
7861 stmt = push_stmt_list ();
7863 loc = c_parser_peek_token (parser)->location;
7864 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
7866 substmt = push_stmt_list ();
7868 while (1)
7870 c_parser_statement (parser);
7872 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7873 break;
7874 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7875 break;
7876 if (c_parser_next_token_is (parser, CPP_EOF))
7877 break;
7880 substmt = pop_stmt_list (substmt);
7881 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7882 SET_EXPR_LOCATION (substmt, loc);
7883 add_stmt (substmt);
7886 while (1)
7888 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7889 break;
7890 if (c_parser_next_token_is (parser, CPP_EOF))
7891 break;
7893 loc = c_parser_peek_token (parser)->location;
7894 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7896 c_parser_consume_pragma (parser);
7897 c_parser_skip_to_pragma_eol (parser);
7898 error_suppress = false;
7900 else if (!error_suppress)
7902 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
7903 error_suppress = true;
7906 substmt = c_parser_omp_structured_block (parser);
7907 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7908 SET_EXPR_LOCATION (substmt, loc);
7909 add_stmt (substmt);
7911 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
7912 "expected %<#pragma omp section%> or %<}%>");
7914 substmt = pop_stmt_list (stmt);
7916 stmt = make_node (OMP_SECTIONS);
7917 TREE_TYPE (stmt) = void_type_node;
7918 OMP_SECTIONS_BODY (stmt) = substmt;
7920 return add_stmt (stmt);
7923 /* OpenMP 2.5:
7924 # pragma omp sections sections-clause[optseq] newline
7925 sections-scope
7928 #define OMP_SECTIONS_CLAUSE_MASK \
7929 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7930 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7931 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7932 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7933 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7935 static tree
7936 c_parser_omp_sections (c_parser *parser)
7938 tree block, clauses, ret;
7940 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
7941 "#pragma omp sections");
7943 block = c_begin_compound_stmt (true);
7944 ret = c_parser_omp_sections_scope (parser);
7945 if (ret)
7946 OMP_SECTIONS_CLAUSES (ret) = clauses;
7947 block = c_end_compound_stmt (block, true);
7948 add_stmt (block);
7950 return ret;
7953 /* OpenMP 2.5:
7954 # pragma parallel parallel-clause new-line
7955 # pragma parallel for parallel-for-clause new-line
7956 # pragma parallel sections parallel-sections-clause new-line
7959 #define OMP_PARALLEL_CLAUSE_MASK \
7960 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7961 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7962 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7963 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7964 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7965 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7966 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7967 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7969 static tree
7970 c_parser_omp_parallel (c_parser *parser)
7972 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
7973 const char *p_name = "#pragma omp parallel";
7974 tree stmt, clauses, par_clause, ws_clause, block;
7975 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
7977 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7979 c_parser_consume_token (parser);
7980 p_kind = PRAGMA_OMP_PARALLEL_FOR;
7981 p_name = "#pragma omp parallel for";
7982 mask |= OMP_FOR_CLAUSE_MASK;
7983 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7985 else if (c_parser_next_token_is (parser, CPP_NAME))
7987 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7988 if (strcmp (p, "sections") == 0)
7990 c_parser_consume_token (parser);
7991 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
7992 p_name = "#pragma omp parallel sections";
7993 mask |= OMP_SECTIONS_CLAUSE_MASK;
7994 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7998 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8000 switch (p_kind)
8002 case PRAGMA_OMP_PARALLEL:
8003 block = c_begin_omp_parallel ();
8004 c_parser_statement (parser);
8005 stmt = c_finish_omp_parallel (clauses, block);
8006 break;
8008 case PRAGMA_OMP_PARALLEL_FOR:
8009 block = c_begin_omp_parallel ();
8010 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8011 c_parser_omp_for_loop (parser, ws_clause, &par_clause);
8012 stmt = c_finish_omp_parallel (par_clause, block);
8013 OMP_PARALLEL_COMBINED (stmt) = 1;
8014 break;
8016 case PRAGMA_OMP_PARALLEL_SECTIONS:
8017 block = c_begin_omp_parallel ();
8018 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8019 stmt = c_parser_omp_sections_scope (parser);
8020 if (stmt)
8021 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8022 stmt = c_finish_omp_parallel (par_clause, block);
8023 OMP_PARALLEL_COMBINED (stmt) = 1;
8024 break;
8026 default:
8027 gcc_unreachable ();
8030 return stmt;
8033 /* OpenMP 2.5:
8034 # pragma omp single single-clause[optseq] new-line
8035 structured-block
8038 #define OMP_SINGLE_CLAUSE_MASK \
8039 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8040 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8041 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8042 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8044 static tree
8045 c_parser_omp_single (c_parser *parser)
8047 tree stmt = make_node (OMP_SINGLE);
8048 TREE_TYPE (stmt) = void_type_node;
8050 OMP_SINGLE_CLAUSES (stmt)
8051 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8052 "#pragma omp single");
8053 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8055 return add_stmt (stmt);
8058 /* OpenMP 3.0:
8059 # pragma omp task task-clause[optseq] new-line
8062 #define OMP_TASK_CLAUSE_MASK \
8063 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8064 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8065 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8066 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8067 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8068 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8070 static tree
8071 c_parser_omp_task (c_parser *parser)
8073 tree clauses, block;
8075 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8076 "#pragma omp task");
8078 block = c_begin_omp_task ();
8079 c_parser_statement (parser);
8080 return c_finish_omp_task (clauses, block);
8083 /* OpenMP 3.0:
8084 # pragma omp taskwait new-line
8087 static void
8088 c_parser_omp_taskwait (c_parser *parser)
8090 c_parser_consume_pragma (parser);
8091 c_parser_skip_to_pragma_eol (parser);
8093 c_finish_omp_taskwait ();
8096 /* Main entry point to parsing most OpenMP pragmas. */
8098 static void
8099 c_parser_omp_construct (c_parser *parser)
8101 enum pragma_kind p_kind;
8102 location_t loc;
8103 tree stmt;
8105 loc = c_parser_peek_token (parser)->location;
8106 p_kind = c_parser_peek_token (parser)->pragma_kind;
8107 c_parser_consume_pragma (parser);
8109 /* For all constructs below except #pragma omp atomic
8110 MUST_NOT_THROW catch handlers are needed when exceptions
8111 are enabled. */
8112 if (p_kind != PRAGMA_OMP_ATOMIC)
8113 c_maybe_initialize_eh ();
8115 switch (p_kind)
8117 case PRAGMA_OMP_ATOMIC:
8118 c_parser_omp_atomic (parser);
8119 return;
8120 case PRAGMA_OMP_CRITICAL:
8121 stmt = c_parser_omp_critical (parser);
8122 break;
8123 case PRAGMA_OMP_FOR:
8124 stmt = c_parser_omp_for (parser);
8125 break;
8126 case PRAGMA_OMP_MASTER:
8127 stmt = c_parser_omp_master (parser);
8128 break;
8129 case PRAGMA_OMP_ORDERED:
8130 stmt = c_parser_omp_ordered (parser);
8131 break;
8132 case PRAGMA_OMP_PARALLEL:
8133 stmt = c_parser_omp_parallel (parser);
8134 break;
8135 case PRAGMA_OMP_SECTIONS:
8136 stmt = c_parser_omp_sections (parser);
8137 break;
8138 case PRAGMA_OMP_SINGLE:
8139 stmt = c_parser_omp_single (parser);
8140 break;
8141 case PRAGMA_OMP_TASK:
8142 stmt = c_parser_omp_task (parser);
8143 break;
8144 default:
8145 gcc_unreachable ();
8148 if (stmt)
8149 SET_EXPR_LOCATION (stmt, loc);
8153 /* OpenMP 2.5:
8154 # pragma omp threadprivate (variable-list) */
8156 static void
8157 c_parser_omp_threadprivate (c_parser *parser)
8159 tree vars, t;
8161 c_parser_consume_pragma (parser);
8162 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8164 /* Mark every variable in VARS to be assigned thread local storage. */
8165 for (t = vars; t; t = TREE_CHAIN (t))
8167 tree v = TREE_PURPOSE (t);
8169 /* If V had already been marked threadprivate, it doesn't matter
8170 whether it had been used prior to this point. */
8171 if (TREE_CODE (v) != VAR_DECL)
8172 error ("%qD is not a variable", v);
8173 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8174 error ("%qE declared %<threadprivate%> after first use", v);
8175 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8176 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8177 else if (TREE_TYPE (v) == error_mark_node)
8179 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8180 error ("%<threadprivate%> %qE has incomplete type", v);
8181 else
8183 if (! DECL_THREAD_LOCAL_P (v))
8185 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8186 /* If rtl has been already set for this var, call
8187 make_decl_rtl once again, so that encode_section_info
8188 has a chance to look at the new decl flags. */
8189 if (DECL_RTL_SET_P (v))
8190 make_decl_rtl (v);
8192 C_DECL_THREADPRIVATE_P (v) = 1;
8196 c_parser_skip_to_pragma_eol (parser);
8200 /* Parse a single source file. */
8202 void
8203 c_parse_file (void)
8205 /* Use local storage to begin. If the first token is a pragma, parse it.
8206 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8207 which will cause garbage collection. */
8208 c_parser tparser;
8210 memset (&tparser, 0, sizeof tparser);
8211 the_parser = &tparser;
8213 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8214 c_parser_pragma_pch_preprocess (&tparser);
8216 the_parser = GGC_NEW (c_parser);
8217 *the_parser = tparser;
8219 c_parser_translation_unit (the_parser);
8220 the_parser = NULL;
8223 #include "gt-c-parser.h"