* xcoffout.h (xcoffout_source_line): Update prototype.
[official-gcc.git] / gcc / c-parser.c
blob244c238023d2ea530d83d70438c6373068e5d206
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "langhooks.h"
47 #include "input.h"
48 #include "cpplib.h"
49 #include "timevar.h"
50 #include "c-pragma.h"
51 #include "c-tree.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "toplev.h"
55 #include "ggc.h"
56 #include "c-common.h"
57 #include "vec.h"
58 #include "target.h"
59 #include "cgraph.h"
60 #include "plugin.h"
63 /* Initialization routine for this file. */
65 void
66 c_parse_init (void)
68 /* The only initialization required is of the reserved word
69 identifiers. */
70 unsigned int i;
71 tree id;
72 int mask = 0;
74 mask |= D_CXXONLY;
75 if (!flag_isoc99)
76 mask |= D_C99;
77 if (flag_no_asm)
79 mask |= D_ASM | D_EXT;
80 if (!flag_isoc99)
81 mask |= D_EXT89;
83 if (!c_dialect_objc ())
84 mask |= D_OBJC | D_CXX_OBJC;
86 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
87 for (i = 0; i < num_c_common_reswords; i++)
89 /* If a keyword is disabled, do not enter it into the table
90 and so create a canonical spelling that isn't a keyword. */
91 if (c_common_reswords[i].disable & mask)
93 if (warn_cxx_compat
94 && (c_common_reswords[i].disable & D_CXXWARN))
96 id = get_identifier (c_common_reswords[i].word);
97 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
98 C_IS_RESERVED_WORD (id) = 1;
100 continue;
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, c_common_reswords[i].rid);
105 C_IS_RESERVED_WORD (id) = 1;
106 ridpointers [(int) c_common_reswords[i].rid] = id;
110 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
111 and the C parser. Unlike the C++ lexer, the parser structure
112 stores the lexer information instead of using a separate structure.
113 Identifiers are separated into ordinary identifiers, type names,
114 keywords and some other Objective-C types of identifiers, and some
115 look-ahead is maintained.
117 ??? It might be a good idea to lex the whole file up front (as for
118 C++). It would then be possible to share more of the C and C++
119 lexer code, if desired. */
121 /* The following local token type is used. */
123 /* A keyword. */
124 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
126 /* More information about the type of a CPP_NAME token. */
127 typedef enum c_id_kind {
128 /* An ordinary identifier. */
129 C_ID_ID,
130 /* An identifier declared as a typedef name. */
131 C_ID_TYPENAME,
132 /* An identifier declared as an Objective-C class name. */
133 C_ID_CLASSNAME,
134 /* Not an identifier. */
135 C_ID_NONE
136 } c_id_kind;
138 /* A single C token after string literal concatenation and conversion
139 of preprocessing tokens to tokens. */
140 typedef struct GTY (()) c_token {
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 : 8;
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 GTY(()) c_parser {
162 /* The look-ahead tokens. */
163 c_token tokens[2];
164 /* How many look-ahead tokens are available (0, 1 or 2). */
165 short tokens_avail;
166 /* True if a syntax error is being recovered from; false otherwise.
167 c_parser_error sets this flag. It should clear this flag when
168 enough tokens have been consumed to recover from the error. */
169 BOOL_BITFIELD error : 1;
170 /* True if we're processing a pragma, and shouldn't automatically
171 consume CPP_PRAGMA_EOL. */
172 BOOL_BITFIELD in_pragma : 1;
173 /* True if we're parsing the outermost block of an if statement. */
174 BOOL_BITFIELD in_if_block : 1;
175 /* True if we want to lex an untranslated string. */
176 BOOL_BITFIELD lex_untranslated_string : 1;
177 /* Objective-C specific parser/lexer information. */
178 BOOL_BITFIELD objc_pq_context : 1;
179 /* The following flag is needed to contextualize Objective-C lexical
180 analysis. In some cases (e.g., 'int NSObject;'), it is
181 undesirable to bind an identifier to an Objective-C class, even
182 if a class with that name exists. */
183 BOOL_BITFIELD objc_need_raw_identifier : 1;
184 } c_parser;
187 /* The actual parser and external interface. ??? Does this need to be
188 garbage-collected? */
190 static GTY (()) c_parser *the_parser;
193 /* Read in and lex a single token, storing it in *TOKEN. */
195 static void
196 c_lex_one_token (c_parser *parser, c_token *token)
198 timevar_push (TV_LEX);
200 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
201 (parser->lex_untranslated_string
202 ? C_LEX_STRING_NO_TRANSLATE : 0));
203 token->id_kind = C_ID_NONE;
204 token->keyword = RID_MAX;
205 token->pragma_kind = PRAGMA_NONE;
207 switch (token->type)
209 case CPP_NAME:
211 tree decl;
213 bool objc_force_identifier = parser->objc_need_raw_identifier;
214 if (c_dialect_objc ())
215 parser->objc_need_raw_identifier = false;
217 if (C_IS_RESERVED_WORD (token->value))
219 enum rid rid_code = C_RID_CODE (token->value);
221 if (rid_code == RID_CXX_COMPAT_WARN)
223 warning_at (token->location,
224 OPT_Wc___compat,
225 "identifier %qE conflicts with C++ keyword",
226 token->value);
228 else if (c_dialect_objc ())
230 if (!objc_is_reserved_word (token->value)
231 && (!OBJC_IS_PQ_KEYWORD (rid_code)
232 || parser->objc_pq_context))
234 /* Return the canonical spelling for this keyword. */
235 token->value = ridpointers[(int) rid_code];
236 token->type = CPP_KEYWORD;
237 token->keyword = rid_code;
238 break;
241 else
243 token->type = CPP_KEYWORD;
244 token->keyword = rid_code;
245 break;
249 decl = lookup_name (token->value);
250 if (decl)
252 if (TREE_CODE (decl) == TYPE_DECL)
254 token->id_kind = C_ID_TYPENAME;
255 break;
258 else if (c_dialect_objc ())
260 tree objc_interface_decl = objc_is_class_name (token->value);
261 /* Objective-C class names are in the same namespace as
262 variables and typedefs, and hence are shadowed by local
263 declarations. */
264 if (objc_interface_decl
265 && (global_bindings_p ()
266 || (!objc_force_identifier && !decl)))
268 token->value = objc_interface_decl;
269 token->id_kind = C_ID_CLASSNAME;
270 break;
273 token->id_kind = C_ID_ID;
275 break;
276 case CPP_AT_NAME:
277 /* This only happens in Objective-C; it must be a keyword. */
278 token->type = CPP_KEYWORD;
279 token->keyword = C_RID_CODE (token->value);
280 break;
281 case CPP_COLON:
282 case CPP_COMMA:
283 case CPP_CLOSE_PAREN:
284 case CPP_SEMICOLON:
285 /* These tokens may affect the interpretation of any identifiers
286 following, if doing Objective-C. */
287 if (c_dialect_objc ())
288 parser->objc_need_raw_identifier = false;
289 break;
290 case CPP_PRAGMA:
291 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
292 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
293 token->value = NULL;
294 break;
295 default:
296 break;
298 timevar_pop (TV_LEX);
301 /* Return a pointer to the next token from PARSER, reading it in if
302 necessary. */
304 static inline c_token *
305 c_parser_peek_token (c_parser *parser)
307 if (parser->tokens_avail == 0)
309 c_lex_one_token (parser, &parser->tokens[0]);
310 parser->tokens_avail = 1;
312 return &parser->tokens[0];
315 /* Return true if the next token from PARSER has the indicated
316 TYPE. */
318 static inline bool
319 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
321 return c_parser_peek_token (parser)->type == type;
324 /* Return true if the next token from PARSER does not have the
325 indicated TYPE. */
327 static inline bool
328 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
330 return !c_parser_next_token_is (parser, type);
333 /* Return true if the next token from PARSER is the indicated
334 KEYWORD. */
336 static inline bool
337 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
339 return c_parser_peek_token (parser)->keyword == keyword;
342 /* Return true if TOKEN can start a type name,
343 false otherwise. */
344 static bool
345 c_token_starts_typename (c_token *token)
347 switch (token->type)
349 case CPP_NAME:
350 switch (token->id_kind)
352 case C_ID_ID:
353 return false;
354 case C_ID_TYPENAME:
355 return true;
356 case C_ID_CLASSNAME:
357 gcc_assert (c_dialect_objc ());
358 return true;
359 default:
360 gcc_unreachable ();
362 case CPP_KEYWORD:
363 switch (token->keyword)
365 case RID_UNSIGNED:
366 case RID_LONG:
367 case RID_SHORT:
368 case RID_SIGNED:
369 case RID_COMPLEX:
370 case RID_INT:
371 case RID_CHAR:
372 case RID_FLOAT:
373 case RID_DOUBLE:
374 case RID_VOID:
375 case RID_DFLOAT32:
376 case RID_DFLOAT64:
377 case RID_DFLOAT128:
378 case RID_BOOL:
379 case RID_ENUM:
380 case RID_STRUCT:
381 case RID_UNION:
382 case RID_TYPEOF:
383 case RID_CONST:
384 case RID_VOLATILE:
385 case RID_RESTRICT:
386 case RID_ATTRIBUTE:
387 case RID_FRACT:
388 case RID_ACCUM:
389 case RID_SAT:
390 return true;
391 default:
392 return false;
394 case CPP_LESS:
395 if (c_dialect_objc ())
396 return true;
397 return false;
398 default:
399 return false;
403 /* Return true if the next token from PARSER can start a type name,
404 false otherwise. */
405 static inline bool
406 c_parser_next_token_starts_typename (c_parser *parser)
408 c_token *token = c_parser_peek_token (parser);
409 return c_token_starts_typename (token);
412 /* Return true if TOKEN can start declaration specifiers, false
413 otherwise. */
414 static bool
415 c_token_starts_declspecs (c_token *token)
417 switch (token->type)
419 case CPP_NAME:
420 switch (token->id_kind)
422 case C_ID_ID:
423 return false;
424 case C_ID_TYPENAME:
425 return true;
426 case C_ID_CLASSNAME:
427 gcc_assert (c_dialect_objc ());
428 return true;
429 default:
430 gcc_unreachable ();
432 case CPP_KEYWORD:
433 switch (token->keyword)
435 case RID_STATIC:
436 case RID_EXTERN:
437 case RID_REGISTER:
438 case RID_TYPEDEF:
439 case RID_INLINE:
440 case RID_AUTO:
441 case RID_THREAD:
442 case RID_UNSIGNED:
443 case RID_LONG:
444 case RID_SHORT:
445 case RID_SIGNED:
446 case RID_COMPLEX:
447 case RID_INT:
448 case RID_CHAR:
449 case RID_FLOAT:
450 case RID_DOUBLE:
451 case RID_VOID:
452 case RID_DFLOAT32:
453 case RID_DFLOAT64:
454 case RID_DFLOAT128:
455 case RID_BOOL:
456 case RID_ENUM:
457 case RID_STRUCT:
458 case RID_UNION:
459 case RID_TYPEOF:
460 case RID_CONST:
461 case RID_VOLATILE:
462 case RID_RESTRICT:
463 case RID_ATTRIBUTE:
464 case RID_FRACT:
465 case RID_ACCUM:
466 case RID_SAT:
467 return true;
468 default:
469 return false;
471 case CPP_LESS:
472 if (c_dialect_objc ())
473 return true;
474 return false;
475 default:
476 return false;
480 /* Return true if the next token from PARSER can start declaration
481 specifiers, false otherwise. */
482 static inline bool
483 c_parser_next_token_starts_declspecs (c_parser *parser)
485 c_token *token = c_parser_peek_token (parser);
486 return c_token_starts_declspecs (token);
489 /* Return a pointer to the next-but-one token from PARSER, reading it
490 in if necessary. The next token is already read in. */
492 static c_token *
493 c_parser_peek_2nd_token (c_parser *parser)
495 if (parser->tokens_avail >= 2)
496 return &parser->tokens[1];
497 gcc_assert (parser->tokens_avail == 1);
498 gcc_assert (parser->tokens[0].type != CPP_EOF);
499 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
500 c_lex_one_token (parser, &parser->tokens[1]);
501 parser->tokens_avail = 2;
502 return &parser->tokens[1];
505 /* Consume the next token from PARSER. */
507 static void
508 c_parser_consume_token (c_parser *parser)
510 gcc_assert (parser->tokens_avail >= 1);
511 gcc_assert (parser->tokens[0].type != CPP_EOF);
512 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
513 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
514 if (parser->tokens_avail == 2)
515 parser->tokens[0] = parser->tokens[1];
516 parser->tokens_avail--;
519 /* Expect the current token to be a #pragma. Consume it and remember
520 that we've begun parsing a pragma. */
522 static void
523 c_parser_consume_pragma (c_parser *parser)
525 gcc_assert (!parser->in_pragma);
526 gcc_assert (parser->tokens_avail >= 1);
527 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
528 if (parser->tokens_avail == 2)
529 parser->tokens[0] = parser->tokens[1];
530 parser->tokens_avail--;
531 parser->in_pragma = true;
534 /* Update the globals input_location and in_system_header from
535 TOKEN. */
536 static inline void
537 c_parser_set_source_position_from_token (c_token *token)
539 if (token->type != CPP_EOF)
541 input_location = token->location;
545 /* Issue a diagnostic of the form
546 FILE:LINE: MESSAGE before TOKEN
547 where TOKEN is the next token in the input stream of PARSER.
548 MESSAGE (specified by the caller) is usually of the form "expected
549 OTHER-TOKEN".
551 Do not issue a diagnostic if still recovering from an error.
553 ??? This is taken from the C++ parser, but building up messages in
554 this way is not i18n-friendly and some other approach should be
555 used. */
557 static void
558 c_parser_error (c_parser *parser, const char *gmsgid)
560 c_token *token = c_parser_peek_token (parser);
561 if (parser->error)
562 return;
563 parser->error = true;
564 if (!gmsgid)
565 return;
566 /* This diagnostic makes more sense if it is tagged to the line of
567 the token we just peeked at. */
568 c_parser_set_source_position_from_token (token);
569 c_parse_error (gmsgid,
570 /* Because c_parse_error does not understand
571 CPP_KEYWORD, keywords are treated like
572 identifiers. */
573 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
574 /* ??? The C parser does not save the cpp flags of a
575 token, we need to pass 0 here and we will not get
576 the source spelling of some tokens but rather the
577 canonical spelling. */
578 token->value, /*flags=*/0);
581 /* If the next token is of the indicated TYPE, consume it. Otherwise,
582 issue the error MSGID. If MSGID is NULL then a message has already
583 been produced and no message will be produced this time. Returns
584 true if found, false otherwise. */
586 static bool
587 c_parser_require (c_parser *parser,
588 enum cpp_ttype type,
589 const char *msgid)
591 if (c_parser_next_token_is (parser, type))
593 c_parser_consume_token (parser);
594 return true;
596 else
598 c_parser_error (parser, msgid);
599 return false;
603 /* If the next token is the indicated keyword, consume it. Otherwise,
604 issue the error MSGID. Returns true if found, false otherwise. */
606 static bool
607 c_parser_require_keyword (c_parser *parser,
608 enum rid keyword,
609 const char *msgid)
611 if (c_parser_next_token_is_keyword (parser, keyword))
613 c_parser_consume_token (parser);
614 return true;
616 else
618 c_parser_error (parser, msgid);
619 return false;
623 /* Like c_parser_require, except that tokens will be skipped until the
624 desired token is found. An error message is still produced if the
625 next token is not as expected. If MSGID is NULL then a message has
626 already been produced and no message will be produced this
627 time. */
629 static void
630 c_parser_skip_until_found (c_parser *parser,
631 enum cpp_ttype type,
632 const char *msgid)
634 unsigned nesting_depth = 0;
636 if (c_parser_require (parser, type, msgid))
637 return;
639 /* Skip tokens until the desired token is found. */
640 while (true)
642 /* Peek at the next token. */
643 c_token *token = c_parser_peek_token (parser);
644 /* If we've reached the token we want, consume it and stop. */
645 if (token->type == type && !nesting_depth)
647 c_parser_consume_token (parser);
648 break;
651 /* If we've run out of tokens, stop. */
652 if (token->type == CPP_EOF)
653 return;
654 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
655 return;
656 if (token->type == CPP_OPEN_BRACE
657 || token->type == CPP_OPEN_PAREN
658 || token->type == CPP_OPEN_SQUARE)
659 ++nesting_depth;
660 else if (token->type == CPP_CLOSE_BRACE
661 || token->type == CPP_CLOSE_PAREN
662 || token->type == CPP_CLOSE_SQUARE)
664 if (nesting_depth-- == 0)
665 break;
667 /* Consume this token. */
668 c_parser_consume_token (parser);
670 parser->error = false;
673 /* Skip tokens until the end of a parameter is found, but do not
674 consume the comma, semicolon or closing delimiter. */
676 static void
677 c_parser_skip_to_end_of_parameter (c_parser *parser)
679 unsigned nesting_depth = 0;
681 while (true)
683 c_token *token = c_parser_peek_token (parser);
684 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
685 && !nesting_depth)
686 break;
687 /* If we've run out of tokens, stop. */
688 if (token->type == CPP_EOF)
689 return;
690 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
691 return;
692 if (token->type == CPP_OPEN_BRACE
693 || token->type == CPP_OPEN_PAREN
694 || token->type == CPP_OPEN_SQUARE)
695 ++nesting_depth;
696 else if (token->type == CPP_CLOSE_BRACE
697 || token->type == CPP_CLOSE_PAREN
698 || token->type == CPP_CLOSE_SQUARE)
700 if (nesting_depth-- == 0)
701 break;
703 /* Consume this token. */
704 c_parser_consume_token (parser);
706 parser->error = false;
709 /* Expect to be at the end of the pragma directive and consume an
710 end of line marker. */
712 static void
713 c_parser_skip_to_pragma_eol (c_parser *parser)
715 gcc_assert (parser->in_pragma);
716 parser->in_pragma = false;
718 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
719 while (true)
721 c_token *token = c_parser_peek_token (parser);
722 if (token->type == CPP_EOF)
723 break;
724 if (token->type == CPP_PRAGMA_EOL)
726 c_parser_consume_token (parser);
727 break;
729 c_parser_consume_token (parser);
732 parser->error = false;
735 /* Skip tokens until we have consumed an entire block, or until we
736 have consumed a non-nested ';'. */
738 static void
739 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
741 unsigned nesting_depth = 0;
742 bool save_error = parser->error;
744 while (true)
746 c_token *token;
748 /* Peek at the next token. */
749 token = c_parser_peek_token (parser);
751 switch (token->type)
753 case CPP_EOF:
754 return;
756 case CPP_PRAGMA_EOL:
757 if (parser->in_pragma)
758 return;
759 break;
761 case CPP_SEMICOLON:
762 /* If the next token is a ';', we have reached the
763 end of the statement. */
764 if (!nesting_depth)
766 /* Consume the ';'. */
767 c_parser_consume_token (parser);
768 goto finished;
770 break;
772 case CPP_CLOSE_BRACE:
773 /* If the next token is a non-nested '}', then we have
774 reached the end of the current block. */
775 if (nesting_depth == 0 || --nesting_depth == 0)
777 c_parser_consume_token (parser);
778 goto finished;
780 break;
782 case CPP_OPEN_BRACE:
783 /* If it the next token is a '{', then we are entering a new
784 block. Consume the entire block. */
785 ++nesting_depth;
786 break;
788 case CPP_PRAGMA:
789 /* If we see a pragma, consume the whole thing at once. We
790 have some safeguards against consuming pragmas willy-nilly.
791 Normally, we'd expect to be here with parser->error set,
792 which disables these safeguards. But it's possible to get
793 here for secondary error recovery, after parser->error has
794 been cleared. */
795 c_parser_consume_pragma (parser);
796 c_parser_skip_to_pragma_eol (parser);
797 parser->error = save_error;
798 continue;
800 default:
801 break;
804 c_parser_consume_token (parser);
807 finished:
808 parser->error = false;
811 /* CPP's options (initialized by c-opts.c). */
812 extern cpp_options *cpp_opts;
814 /* Save the warning flags which are controlled by __extension__. */
816 static inline int
817 disable_extension_diagnostics (void)
819 int ret = (pedantic
820 | (warn_pointer_arith << 1)
821 | (warn_traditional << 2)
822 | (flag_iso << 3)
823 | (warn_long_long << 4)
824 | (warn_cxx_compat << 5));
825 cpp_opts->pedantic = pedantic = 0;
826 warn_pointer_arith = 0;
827 cpp_opts->warn_traditional = warn_traditional = 0;
828 flag_iso = 0;
829 cpp_opts->warn_long_long = warn_long_long = 0;
830 warn_cxx_compat = 0;
831 return ret;
834 /* Restore the warning flags which are controlled by __extension__.
835 FLAGS is the return value from disable_extension_diagnostics. */
837 static inline void
838 restore_extension_diagnostics (int flags)
840 cpp_opts->pedantic = pedantic = flags & 1;
841 warn_pointer_arith = (flags >> 1) & 1;
842 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
843 flag_iso = (flags >> 3) & 1;
844 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
845 warn_cxx_compat = (flags >> 5) & 1;
848 /* Possibly kinds of declarator to parse. */
849 typedef enum c_dtr_syn {
850 /* A normal declarator with an identifier. */
851 C_DTR_NORMAL,
852 /* An abstract declarator (maybe empty). */
853 C_DTR_ABSTRACT,
854 /* A parameter declarator: may be either, but after a type name does
855 not redeclare a typedef name as an identifier if it can
856 alternatively be interpreted as a typedef name; see DR#009,
857 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
858 following DR#249. For example, given a typedef T, "int T" and
859 "int *T" are valid parameter declarations redeclaring T, while
860 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
861 abstract declarators rather than involving redundant parentheses;
862 the same applies with attributes inside the parentheses before
863 "T". */
864 C_DTR_PARM
865 } c_dtr_syn;
867 static void c_parser_external_declaration (c_parser *);
868 static void c_parser_asm_definition (c_parser *);
869 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
870 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
871 bool);
872 static struct c_typespec c_parser_enum_specifier (c_parser *);
873 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
874 static tree c_parser_struct_declaration (c_parser *);
875 static struct c_typespec c_parser_typeof_specifier (c_parser *);
876 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
877 bool *);
878 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
879 c_dtr_syn, bool *);
880 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
881 bool,
882 struct c_declarator *);
883 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
884 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
885 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
886 static tree c_parser_simple_asm_expr (c_parser *);
887 static tree c_parser_attributes (c_parser *);
888 static struct c_type_name *c_parser_type_name (c_parser *);
889 static struct c_expr c_parser_initializer (c_parser *);
890 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
891 static void c_parser_initelt (c_parser *);
892 static void c_parser_initval (c_parser *, struct c_expr *);
893 static tree c_parser_compound_statement (c_parser *);
894 static void c_parser_compound_statement_nostart (c_parser *);
895 static void c_parser_label (c_parser *);
896 static void c_parser_statement (c_parser *);
897 static void c_parser_statement_after_labels (c_parser *);
898 static void c_parser_if_statement (c_parser *);
899 static void c_parser_switch_statement (c_parser *);
900 static void c_parser_while_statement (c_parser *);
901 static void c_parser_do_statement (c_parser *);
902 static void c_parser_for_statement (c_parser *);
903 static tree c_parser_asm_statement (c_parser *);
904 static tree c_parser_asm_operands (c_parser *, bool);
905 static tree c_parser_asm_clobbers (c_parser *);
906 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
907 static struct c_expr c_parser_conditional_expression (c_parser *,
908 struct c_expr *);
909 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
910 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
911 static struct c_expr c_parser_unary_expression (c_parser *);
912 static struct c_expr c_parser_sizeof_expression (c_parser *);
913 static struct c_expr c_parser_alignof_expression (c_parser *);
914 static struct c_expr c_parser_postfix_expression (c_parser *);
915 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
916 struct c_type_name *,
917 location_t);
918 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
919 struct c_expr);
920 static struct c_expr c_parser_expression (c_parser *);
921 static struct c_expr c_parser_expression_conv (c_parser *);
922 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
923 VEC(tree,gc) **);
924 static void c_parser_omp_construct (c_parser *);
925 static void c_parser_omp_threadprivate (c_parser *);
926 static void c_parser_omp_barrier (c_parser *);
927 static void c_parser_omp_flush (c_parser *);
928 static void c_parser_omp_taskwait (c_parser *);
930 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
931 static bool c_parser_pragma (c_parser *, enum pragma_context);
933 /* These Objective-C parser functions are only ever called when
934 compiling Objective-C. */
935 static void c_parser_objc_class_definition (c_parser *);
936 static void c_parser_objc_class_instance_variables (c_parser *);
937 static void c_parser_objc_class_declaration (c_parser *);
938 static void c_parser_objc_alias_declaration (c_parser *);
939 static void c_parser_objc_protocol_definition (c_parser *);
940 static enum tree_code c_parser_objc_method_type (c_parser *);
941 static void c_parser_objc_method_definition (c_parser *);
942 static void c_parser_objc_methodprotolist (c_parser *);
943 static void c_parser_objc_methodproto (c_parser *);
944 static tree c_parser_objc_method_decl (c_parser *);
945 static tree c_parser_objc_type_name (c_parser *);
946 static tree c_parser_objc_protocol_refs (c_parser *);
947 static void c_parser_objc_try_catch_statement (c_parser *);
948 static void c_parser_objc_synchronized_statement (c_parser *);
949 static tree c_parser_objc_selector (c_parser *);
950 static tree c_parser_objc_selector_arg (c_parser *);
951 static tree c_parser_objc_receiver (c_parser *);
952 static tree c_parser_objc_message_args (c_parser *);
953 static tree c_parser_objc_keywordexpr (c_parser *);
955 /* Parse a translation unit (C90 6.7, C99 6.9).
957 translation-unit:
958 external-declarations
960 external-declarations:
961 external-declaration
962 external-declarations external-declaration
964 GNU extensions:
966 translation-unit:
967 empty
970 static void
971 c_parser_translation_unit (c_parser *parser)
973 if (c_parser_next_token_is (parser, CPP_EOF))
975 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
976 "ISO C forbids an empty translation unit");
978 else
980 void *obstack_position = obstack_alloc (&parser_obstack, 0);
981 mark_valid_location_for_stdc_pragma (false);
984 ggc_collect ();
985 c_parser_external_declaration (parser);
986 obstack_free (&parser_obstack, obstack_position);
988 while (c_parser_next_token_is_not (parser, CPP_EOF));
992 /* Parse an external declaration (C90 6.7, C99 6.9).
994 external-declaration:
995 function-definition
996 declaration
998 GNU extensions:
1000 external-declaration:
1001 asm-definition
1003 __extension__ external-declaration
1005 Objective-C:
1007 external-declaration:
1008 objc-class-definition
1009 objc-class-declaration
1010 objc-alias-declaration
1011 objc-protocol-definition
1012 objc-method-definition
1013 @end
1016 static void
1017 c_parser_external_declaration (c_parser *parser)
1019 int ext;
1020 switch (c_parser_peek_token (parser)->type)
1022 case CPP_KEYWORD:
1023 switch (c_parser_peek_token (parser)->keyword)
1025 case RID_EXTENSION:
1026 ext = disable_extension_diagnostics ();
1027 c_parser_consume_token (parser);
1028 c_parser_external_declaration (parser);
1029 restore_extension_diagnostics (ext);
1030 break;
1031 case RID_ASM:
1032 c_parser_asm_definition (parser);
1033 break;
1034 case RID_AT_INTERFACE:
1035 case RID_AT_IMPLEMENTATION:
1036 gcc_assert (c_dialect_objc ());
1037 c_parser_objc_class_definition (parser);
1038 break;
1039 case RID_CLASS:
1040 gcc_assert (c_dialect_objc ());
1041 c_parser_objc_class_declaration (parser);
1042 break;
1043 case RID_AT_ALIAS:
1044 gcc_assert (c_dialect_objc ());
1045 c_parser_objc_alias_declaration (parser);
1046 break;
1047 case RID_AT_PROTOCOL:
1048 gcc_assert (c_dialect_objc ());
1049 c_parser_objc_protocol_definition (parser);
1050 break;
1051 case RID_AT_END:
1052 gcc_assert (c_dialect_objc ());
1053 c_parser_consume_token (parser);
1054 objc_finish_implementation ();
1055 break;
1056 default:
1057 goto decl_or_fndef;
1059 break;
1060 case CPP_SEMICOLON:
1061 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1062 "ISO C does not allow extra %<;%> outside of a function");
1063 c_parser_consume_token (parser);
1064 break;
1065 case CPP_PRAGMA:
1066 mark_valid_location_for_stdc_pragma (true);
1067 c_parser_pragma (parser, pragma_external);
1068 mark_valid_location_for_stdc_pragma (false);
1069 break;
1070 case CPP_PLUS:
1071 case CPP_MINUS:
1072 if (c_dialect_objc ())
1074 c_parser_objc_method_definition (parser);
1075 break;
1077 /* Else fall through, and yield a syntax error trying to parse
1078 as a declaration or function definition. */
1079 default:
1080 decl_or_fndef:
1081 /* A declaration or a function definition. We can only tell
1082 which after parsing the declaration specifiers, if any, and
1083 the first declarator. */
1084 c_parser_declaration_or_fndef (parser, true, true, false, true);
1085 break;
1090 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1091 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1092 accepted; otherwise (old-style parameter declarations) only other
1093 declarations are accepted. If NESTED is true, we are inside a
1094 function or parsing old-style parameter declarations; any functions
1095 encountered are nested functions and declaration specifiers are
1096 required; otherwise we are at top level and functions are normal
1097 functions and declaration specifiers may be optional. If EMPTY_OK
1098 is true, empty declarations are OK (subject to all other
1099 constraints); otherwise (old-style parameter declarations) they are
1100 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1101 may start with attributes; otherwise they may not.
1103 declaration:
1104 declaration-specifiers init-declarator-list[opt] ;
1106 function-definition:
1107 declaration-specifiers[opt] declarator declaration-list[opt]
1108 compound-statement
1110 declaration-list:
1111 declaration
1112 declaration-list declaration
1114 init-declarator-list:
1115 init-declarator
1116 init-declarator-list , init-declarator
1118 init-declarator:
1119 declarator simple-asm-expr[opt] attributes[opt]
1120 declarator simple-asm-expr[opt] attributes[opt] = initializer
1122 GNU extensions:
1124 nested-function-definition:
1125 declaration-specifiers declarator declaration-list[opt]
1126 compound-statement
1128 The simple-asm-expr and attributes are GNU extensions.
1130 This function does not handle __extension__; that is handled in its
1131 callers. ??? Following the old parser, __extension__ may start
1132 external declarations, declarations in functions and declarations
1133 at the start of "for" loops, but not old-style parameter
1134 declarations.
1136 C99 requires declaration specifiers in a function definition; the
1137 absence is diagnosed through the diagnosis of implicit int. In GNU
1138 C we also allow but diagnose declarations without declaration
1139 specifiers, but only at top level (elsewhere they conflict with
1140 other syntax).
1142 OpenMP:
1144 declaration:
1145 threadprivate-directive */
1147 static void
1148 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1149 bool nested, bool start_attr_ok)
1151 struct c_declspecs *specs;
1152 tree prefix_attrs;
1153 tree all_prefix_attrs;
1154 bool diagnosed_no_specs = false;
1155 location_t here = c_parser_peek_token (parser)->location;
1157 specs = build_null_declspecs ();
1158 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1159 if (parser->error)
1161 c_parser_skip_to_end_of_block_or_statement (parser);
1162 return;
1164 if (nested && !specs->declspecs_seen_p)
1166 c_parser_error (parser, "expected declaration specifiers");
1167 c_parser_skip_to_end_of_block_or_statement (parser);
1168 return;
1170 finish_declspecs (specs);
1171 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1173 if (empty_ok)
1174 shadow_tag (specs);
1175 else
1177 shadow_tag_warned (specs, 1);
1178 pedwarn (here, 0, "empty declaration");
1180 c_parser_consume_token (parser);
1181 return;
1183 pending_xref_error ();
1184 prefix_attrs = specs->attrs;
1185 all_prefix_attrs = prefix_attrs;
1186 specs->attrs = NULL_TREE;
1187 while (true)
1189 struct c_declarator *declarator;
1190 bool dummy = false;
1191 tree fnbody;
1192 /* Declaring either one or more declarators (in which case we
1193 should diagnose if there were no declaration specifiers) or a
1194 function definition (in which case the diagnostic for
1195 implicit int suffices). */
1196 declarator = c_parser_declarator (parser, specs->type_seen_p,
1197 C_DTR_NORMAL, &dummy);
1198 if (declarator == NULL)
1200 c_parser_skip_to_end_of_block_or_statement (parser);
1201 return;
1203 if (c_parser_next_token_is (parser, CPP_EQ)
1204 || c_parser_next_token_is (parser, CPP_COMMA)
1205 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1206 || c_parser_next_token_is_keyword (parser, RID_ASM)
1207 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1209 tree asm_name = NULL_TREE;
1210 tree postfix_attrs = NULL_TREE;
1211 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1213 diagnosed_no_specs = true;
1214 pedwarn (here, 0, "data definition has no type or storage class");
1216 /* Having seen a data definition, there cannot now be a
1217 function definition. */
1218 fndef_ok = false;
1219 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1220 asm_name = c_parser_simple_asm_expr (parser);
1221 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1222 postfix_attrs = c_parser_attributes (parser);
1223 if (c_parser_next_token_is (parser, CPP_EQ))
1225 tree d;
1226 struct c_expr init;
1227 c_parser_consume_token (parser);
1228 /* The declaration of the variable is in effect while
1229 its initializer is parsed. */
1230 d = start_decl (declarator, specs, true,
1231 chainon (postfix_attrs, all_prefix_attrs));
1232 if (!d)
1233 d = error_mark_node;
1234 start_init (d, asm_name, global_bindings_p ());
1235 init = c_parser_initializer (parser);
1236 finish_init ();
1237 if (d != error_mark_node)
1239 maybe_warn_string_init (TREE_TYPE (d), init);
1240 finish_decl (d, init.value, init.original_type, asm_name);
1243 else
1245 tree d = start_decl (declarator, specs, false,
1246 chainon (postfix_attrs,
1247 all_prefix_attrs));
1248 if (d)
1249 finish_decl (d, NULL_TREE, NULL_TREE, asm_name);
1251 if (c_parser_next_token_is (parser, CPP_COMMA))
1253 c_parser_consume_token (parser);
1254 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1255 all_prefix_attrs = chainon (c_parser_attributes (parser),
1256 prefix_attrs);
1257 else
1258 all_prefix_attrs = prefix_attrs;
1259 continue;
1261 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1263 c_parser_consume_token (parser);
1264 return;
1266 else
1268 c_parser_error (parser, "expected %<,%> or %<;%>");
1269 c_parser_skip_to_end_of_block_or_statement (parser);
1270 return;
1273 else if (!fndef_ok)
1275 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1276 "%<asm%> or %<__attribute__%>");
1277 c_parser_skip_to_end_of_block_or_statement (parser);
1278 return;
1280 /* Function definition (nested or otherwise). */
1281 if (nested)
1283 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1284 c_push_function_context ();
1286 if (!start_function (specs, declarator, all_prefix_attrs))
1288 /* This can appear in many cases looking nothing like a
1289 function definition, so we don't give a more specific
1290 error suggesting there was one. */
1291 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1292 "or %<__attribute__%>");
1293 if (nested)
1294 c_pop_function_context ();
1295 break;
1297 /* Parse old-style parameter declarations. ??? Attributes are
1298 not allowed to start declaration specifiers here because of a
1299 syntax conflict between a function declaration with attribute
1300 suffix and a function definition with an attribute prefix on
1301 first old-style parameter declaration. Following the old
1302 parser, they are not accepted on subsequent old-style
1303 parameter declarations either. However, there is no
1304 ambiguity after the first declaration, nor indeed on the
1305 first as long as we don't allow postfix attributes after a
1306 declarator with a nonempty identifier list in a definition;
1307 and postfix attributes have never been accepted here in
1308 function definitions either. */
1309 while (c_parser_next_token_is_not (parser, CPP_EOF)
1310 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1311 c_parser_declaration_or_fndef (parser, false, false, true, false);
1312 store_parm_decls ();
1313 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1314 = c_parser_peek_token (parser)->location;
1315 fnbody = c_parser_compound_statement (parser);
1316 if (nested)
1318 tree decl = current_function_decl;
1319 add_stmt (fnbody);
1320 finish_function ();
1321 c_pop_function_context ();
1322 add_stmt (build_stmt (DECL_EXPR, decl));
1324 else
1326 add_stmt (fnbody);
1327 finish_function ();
1329 break;
1333 /* Parse an asm-definition (asm() outside a function body). This is a
1334 GNU extension.
1336 asm-definition:
1337 simple-asm-expr ;
1340 static void
1341 c_parser_asm_definition (c_parser *parser)
1343 tree asm_str = c_parser_simple_asm_expr (parser);
1344 if (asm_str)
1345 cgraph_add_asm_node (asm_str);
1346 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1349 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1350 6.7), adding them to SPECS (which may already include some).
1351 Storage class specifiers are accepted iff SCSPEC_OK; type
1352 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1353 the start iff START_ATTR_OK.
1355 declaration-specifiers:
1356 storage-class-specifier declaration-specifiers[opt]
1357 type-specifier declaration-specifiers[opt]
1358 type-qualifier declaration-specifiers[opt]
1359 function-specifier declaration-specifiers[opt]
1361 Function specifiers (inline) are from C99, and are currently
1362 handled as storage class specifiers, as is __thread.
1364 C90 6.5.1, C99 6.7.1:
1365 storage-class-specifier:
1366 typedef
1367 extern
1368 static
1369 auto
1370 register
1372 C99 6.7.4:
1373 function-specifier:
1374 inline
1376 C90 6.5.2, C99 6.7.2:
1377 type-specifier:
1378 void
1379 char
1380 short
1382 long
1383 float
1384 double
1385 signed
1386 unsigned
1387 _Bool
1388 _Complex
1389 [_Imaginary removed in C99 TC2]
1390 struct-or-union-specifier
1391 enum-specifier
1392 typedef-name
1394 (_Bool and _Complex are new in C99.)
1396 C90 6.5.3, C99 6.7.3:
1398 type-qualifier:
1399 const
1400 restrict
1401 volatile
1403 (restrict is new in C99.)
1405 GNU extensions:
1407 declaration-specifiers:
1408 attributes declaration-specifiers[opt]
1410 storage-class-specifier:
1411 __thread
1413 type-specifier:
1414 typeof-specifier
1415 _Decimal32
1416 _Decimal64
1417 _Decimal128
1418 _Fract
1419 _Accum
1420 _Sat
1422 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1423 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1425 Objective-C:
1427 type-specifier:
1428 class-name objc-protocol-refs[opt]
1429 typedef-name objc-protocol-refs
1430 objc-protocol-refs
1433 static void
1434 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1435 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1437 bool attrs_ok = start_attr_ok;
1438 bool seen_type = specs->type_seen_p;
1439 while (c_parser_next_token_is (parser, CPP_NAME)
1440 || c_parser_next_token_is (parser, CPP_KEYWORD)
1441 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1443 struct c_typespec t;
1444 tree attrs;
1445 if (c_parser_next_token_is (parser, CPP_NAME))
1447 tree value = c_parser_peek_token (parser)->value;
1448 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1449 /* This finishes the specifiers unless a type name is OK, it
1450 is declared as a type name and a type name hasn't yet
1451 been seen. */
1452 if (!typespec_ok || seen_type
1453 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1454 break;
1455 c_parser_consume_token (parser);
1456 seen_type = true;
1457 attrs_ok = true;
1458 if (kind == C_ID_TYPENAME
1459 && (!c_dialect_objc ()
1460 || c_parser_next_token_is_not (parser, CPP_LESS)))
1462 t.kind = ctsk_typedef;
1463 /* For a typedef name, record the meaning, not the name.
1464 In case of 'foo foo, bar;'. */
1465 t.spec = lookup_name (value);
1466 t.expr = NULL_TREE;
1467 t.expr_const_operands = true;
1469 else
1471 tree proto = NULL_TREE;
1472 gcc_assert (c_dialect_objc ());
1473 t.kind = ctsk_objc;
1474 if (c_parser_next_token_is (parser, CPP_LESS))
1475 proto = c_parser_objc_protocol_refs (parser);
1476 t.spec = objc_get_protocol_qualified_type (value, proto);
1477 t.expr = NULL_TREE;
1478 t.expr_const_operands = true;
1480 declspecs_add_type (specs, t);
1481 continue;
1483 if (c_parser_next_token_is (parser, CPP_LESS))
1485 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1486 nisse@lysator.liu.se. */
1487 tree proto;
1488 gcc_assert (c_dialect_objc ());
1489 if (!typespec_ok || seen_type)
1490 break;
1491 proto = c_parser_objc_protocol_refs (parser);
1492 t.kind = ctsk_objc;
1493 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1494 t.expr = NULL_TREE;
1495 t.expr_const_operands = true;
1496 declspecs_add_type (specs, t);
1497 continue;
1499 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1500 switch (c_parser_peek_token (parser)->keyword)
1502 case RID_STATIC:
1503 case RID_EXTERN:
1504 case RID_REGISTER:
1505 case RID_TYPEDEF:
1506 case RID_INLINE:
1507 case RID_AUTO:
1508 case RID_THREAD:
1509 if (!scspec_ok)
1510 goto out;
1511 attrs_ok = true;
1512 /* TODO: Distinguish between function specifiers (inline)
1513 and storage class specifiers, either here or in
1514 declspecs_add_scspec. */
1515 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1516 c_parser_consume_token (parser);
1517 break;
1518 case RID_UNSIGNED:
1519 case RID_LONG:
1520 case RID_SHORT:
1521 case RID_SIGNED:
1522 case RID_COMPLEX:
1523 case RID_INT:
1524 case RID_CHAR:
1525 case RID_FLOAT:
1526 case RID_DOUBLE:
1527 case RID_VOID:
1528 case RID_DFLOAT32:
1529 case RID_DFLOAT64:
1530 case RID_DFLOAT128:
1531 case RID_BOOL:
1532 case RID_FRACT:
1533 case RID_ACCUM:
1534 case RID_SAT:
1535 if (!typespec_ok)
1536 goto out;
1537 attrs_ok = true;
1538 seen_type = true;
1539 if (c_dialect_objc ())
1540 parser->objc_need_raw_identifier = true;
1541 t.kind = ctsk_resword;
1542 t.spec = c_parser_peek_token (parser)->value;
1543 t.expr = NULL_TREE;
1544 t.expr_const_operands = true;
1545 declspecs_add_type (specs, t);
1546 c_parser_consume_token (parser);
1547 break;
1548 case RID_ENUM:
1549 if (!typespec_ok)
1550 goto out;
1551 attrs_ok = true;
1552 seen_type = true;
1553 t = c_parser_enum_specifier (parser);
1554 declspecs_add_type (specs, t);
1555 break;
1556 case RID_STRUCT:
1557 case RID_UNION:
1558 if (!typespec_ok)
1559 goto out;
1560 attrs_ok = true;
1561 seen_type = true;
1562 t = c_parser_struct_or_union_specifier (parser);
1563 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1564 declspecs_add_type (specs, t);
1565 break;
1566 case RID_TYPEOF:
1567 /* ??? The old parser rejected typeof after other type
1568 specifiers, but is a syntax error the best way of
1569 handling this? */
1570 if (!typespec_ok || seen_type)
1571 goto out;
1572 attrs_ok = true;
1573 seen_type = true;
1574 t = c_parser_typeof_specifier (parser);
1575 declspecs_add_type (specs, t);
1576 break;
1577 case RID_CONST:
1578 case RID_VOLATILE:
1579 case RID_RESTRICT:
1580 attrs_ok = true;
1581 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1582 c_parser_consume_token (parser);
1583 break;
1584 case RID_ATTRIBUTE:
1585 if (!attrs_ok)
1586 goto out;
1587 attrs = c_parser_attributes (parser);
1588 declspecs_add_attrs (specs, attrs);
1589 break;
1590 default:
1591 goto out;
1594 out: ;
1597 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1599 enum-specifier:
1600 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1601 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1602 enum attributes[opt] identifier
1604 The form with trailing comma is new in C99. The forms with
1605 attributes are GNU extensions. In GNU C, we accept any expression
1606 without commas in the syntax (assignment expressions, not just
1607 conditional expressions); assignment expressions will be diagnosed
1608 as non-constant.
1610 enumerator-list:
1611 enumerator
1612 enumerator-list , enumerator
1614 enumerator:
1615 enumeration-constant
1616 enumeration-constant = constant-expression
1619 static struct c_typespec
1620 c_parser_enum_specifier (c_parser *parser)
1622 struct c_typespec ret;
1623 tree attrs;
1624 tree ident = NULL_TREE;
1625 location_t enum_loc;
1626 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1627 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1628 enum_loc = c_parser_peek_token (parser)->location;
1629 c_parser_consume_token (parser);
1630 attrs = c_parser_attributes (parser);
1631 /* Set the location in case we create a decl now. */
1632 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1633 if (c_parser_next_token_is (parser, CPP_NAME))
1635 ident = c_parser_peek_token (parser)->value;
1636 ident_loc = c_parser_peek_token (parser)->location;
1637 enum_loc = ident_loc;
1638 c_parser_consume_token (parser);
1640 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1642 /* Parse an enum definition. */
1643 struct c_enum_contents the_enum;
1644 tree type = start_enum (&the_enum, ident, enum_loc);
1645 tree postfix_attrs;
1646 /* We chain the enumerators in reverse order, then put them in
1647 forward order at the end. */
1648 tree values = NULL_TREE;
1649 c_parser_consume_token (parser);
1650 while (true)
1652 tree enum_id;
1653 tree enum_value;
1654 tree enum_decl;
1655 bool seen_comma;
1656 c_token *token;
1657 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1658 location_t value_loc;
1659 if (c_parser_next_token_is_not (parser, CPP_NAME))
1661 c_parser_error (parser, "expected identifier");
1662 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1663 values = error_mark_node;
1664 break;
1666 token = c_parser_peek_token (parser);
1667 enum_id = token->value;
1668 /* Set the location in case we create a decl now. */
1669 c_parser_set_source_position_from_token (token);
1670 value_loc = token->location;
1671 c_parser_consume_token (parser);
1672 if (c_parser_next_token_is (parser, CPP_EQ))
1674 c_parser_consume_token (parser);
1675 value_loc = c_parser_peek_token (parser)->location;
1676 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1678 else
1679 enum_value = NULL_TREE;
1680 enum_decl = build_enumerator (&the_enum, enum_id, enum_value,
1681 value_loc);
1682 TREE_CHAIN (enum_decl) = values;
1683 values = enum_decl;
1684 seen_comma = false;
1685 if (c_parser_next_token_is (parser, CPP_COMMA))
1687 comma_loc = c_parser_peek_token (parser)->location;
1688 seen_comma = true;
1689 c_parser_consume_token (parser);
1691 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1693 if (seen_comma && !flag_isoc99)
1694 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1695 c_parser_consume_token (parser);
1696 break;
1698 if (!seen_comma)
1700 c_parser_error (parser, "expected %<,%> or %<}%>");
1701 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1702 values = error_mark_node;
1703 break;
1706 postfix_attrs = c_parser_attributes (parser);
1707 ret.spec = finish_enum (type, nreverse (values),
1708 chainon (attrs, postfix_attrs));
1709 ret.kind = ctsk_tagdef;
1710 ret.expr = NULL_TREE;
1711 ret.expr_const_operands = true;
1712 return ret;
1714 else if (!ident)
1716 c_parser_error (parser, "expected %<{%>");
1717 ret.spec = error_mark_node;
1718 ret.kind = ctsk_tagref;
1719 ret.expr = NULL_TREE;
1720 ret.expr_const_operands = true;
1721 return ret;
1723 ret = parser_xref_tag (ENUMERAL_TYPE, ident, ident_loc);
1724 /* In ISO C, enumerated types can be referred to only if already
1725 defined. */
1726 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1728 gcc_assert (ident);
1729 pedwarn (ident_loc, OPT_pedantic,
1730 "ISO C forbids forward references to %<enum%> types");
1732 return ret;
1735 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1737 struct-or-union-specifier:
1738 struct-or-union attributes[opt] identifier[opt]
1739 { struct-contents } attributes[opt]
1740 struct-or-union attributes[opt] identifier
1742 struct-contents:
1743 struct-declaration-list
1745 struct-declaration-list:
1746 struct-declaration ;
1747 struct-declaration-list struct-declaration ;
1749 GNU extensions:
1751 struct-contents:
1752 empty
1753 struct-declaration
1754 struct-declaration-list struct-declaration
1756 struct-declaration-list:
1757 struct-declaration-list ;
1760 (Note that in the syntax here, unlike that in ISO C, the semicolons
1761 are included here rather than in struct-declaration, in order to
1762 describe the syntax with extra semicolons and missing semicolon at
1763 end.)
1765 Objective-C:
1767 struct-declaration-list:
1768 @defs ( class-name )
1770 (Note this does not include a trailing semicolon, but can be
1771 followed by further declarations, and gets a pedwarn-if-pedantic
1772 when followed by a semicolon.) */
1774 static struct c_typespec
1775 c_parser_struct_or_union_specifier (c_parser *parser)
1777 struct c_typespec ret;
1778 tree attrs;
1779 tree ident = NULL_TREE;
1780 location_t struct_loc;
1781 location_t ident_loc = UNKNOWN_LOCATION;
1782 enum tree_code code;
1783 switch (c_parser_peek_token (parser)->keyword)
1785 case RID_STRUCT:
1786 code = RECORD_TYPE;
1787 break;
1788 case RID_UNION:
1789 code = UNION_TYPE;
1790 break;
1791 default:
1792 gcc_unreachable ();
1794 struct_loc = c_parser_peek_token (parser)->location;
1795 c_parser_consume_token (parser);
1796 attrs = c_parser_attributes (parser);
1797 /* Set the location in case we create a decl now. */
1798 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1799 if (c_parser_next_token_is (parser, CPP_NAME))
1801 ident = c_parser_peek_token (parser)->value;
1802 ident_loc = c_parser_peek_token (parser)->location;
1803 struct_loc = ident_loc;
1804 c_parser_consume_token (parser);
1806 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1808 /* Parse a struct or union definition. Start the scope of the
1809 tag before parsing components. */
1810 bool in_struct;
1811 VEC(tree,heap) *struct_types;
1812 tree type = start_struct (code, ident, &in_struct, &struct_types,
1813 struct_loc);
1814 tree postfix_attrs;
1815 /* We chain the components in reverse order, then put them in
1816 forward order at the end. Each struct-declaration may
1817 declare multiple components (comma-separated), so we must use
1818 chainon to join them, although when parsing each
1819 struct-declaration we can use TREE_CHAIN directly.
1821 The theory behind all this is that there will be more
1822 semicolon separated fields than comma separated fields, and
1823 so we'll be minimizing the number of node traversals required
1824 by chainon. */
1825 tree contents = NULL_TREE;
1826 c_parser_consume_token (parser);
1827 /* Handle the Objective-C @defs construct,
1828 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1829 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1831 tree name;
1832 gcc_assert (c_dialect_objc ());
1833 c_parser_consume_token (parser);
1834 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1835 goto end_at_defs;
1836 if (c_parser_next_token_is (parser, CPP_NAME)
1837 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1839 name = c_parser_peek_token (parser)->value;
1840 c_parser_consume_token (parser);
1842 else
1844 c_parser_error (parser, "expected class name");
1845 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1846 goto end_at_defs;
1848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1849 "expected %<)%>");
1850 contents = nreverse (objc_get_class_ivars (name));
1852 end_at_defs:
1853 /* Parse the struct-declarations and semicolons. Problems with
1854 semicolons are diagnosed here; empty structures are diagnosed
1855 elsewhere. */
1856 while (true)
1858 tree decls;
1859 /* Parse any stray semicolon. */
1860 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1862 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1863 "extra semicolon in struct or union specified");
1864 c_parser_consume_token (parser);
1865 continue;
1867 /* Stop if at the end of the struct or union contents. */
1868 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1870 c_parser_consume_token (parser);
1871 break;
1873 /* Accept #pragmas at struct scope. */
1874 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1876 c_parser_pragma (parser, pragma_external);
1877 continue;
1879 /* Parse some comma-separated declarations, but not the
1880 trailing semicolon if any. */
1881 decls = c_parser_struct_declaration (parser);
1882 contents = chainon (decls, contents);
1883 /* If no semicolon follows, either we have a parse error or
1884 are at the end of the struct or union and should
1885 pedwarn. */
1886 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1887 c_parser_consume_token (parser);
1888 else
1890 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1891 pedwarn (c_parser_peek_token (parser)->location, 0,
1892 "no semicolon at end of struct or union");
1893 else
1895 c_parser_error (parser, "expected %<;%>");
1896 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1897 break;
1901 postfix_attrs = c_parser_attributes (parser);
1902 ret.spec = finish_struct (type, nreverse (contents),
1903 chainon (attrs, postfix_attrs),
1904 in_struct, struct_types);
1905 ret.kind = ctsk_tagdef;
1906 ret.expr = NULL_TREE;
1907 ret.expr_const_operands = true;
1908 return ret;
1910 else if (!ident)
1912 c_parser_error (parser, "expected %<{%>");
1913 ret.spec = error_mark_node;
1914 ret.kind = ctsk_tagref;
1915 ret.expr = NULL_TREE;
1916 ret.expr_const_operands = true;
1917 return ret;
1919 ret = parser_xref_tag (code, ident, ident_loc);
1920 return ret;
1923 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1924 the trailing semicolon.
1926 struct-declaration:
1927 specifier-qualifier-list struct-declarator-list
1929 specifier-qualifier-list:
1930 type-specifier specifier-qualifier-list[opt]
1931 type-qualifier specifier-qualifier-list[opt]
1932 attributes specifier-qualifier-list[opt]
1934 struct-declarator-list:
1935 struct-declarator
1936 struct-declarator-list , attributes[opt] struct-declarator
1938 struct-declarator:
1939 declarator attributes[opt]
1940 declarator[opt] : constant-expression attributes[opt]
1942 GNU extensions:
1944 struct-declaration:
1945 __extension__ struct-declaration
1946 specifier-qualifier-list
1948 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1949 of attributes where shown is a GNU extension. In GNU C, we accept
1950 any expression without commas in the syntax (assignment
1951 expressions, not just conditional expressions); assignment
1952 expressions will be diagnosed as non-constant. */
1954 static tree
1955 c_parser_struct_declaration (c_parser *parser)
1957 struct c_declspecs *specs;
1958 tree prefix_attrs;
1959 tree all_prefix_attrs;
1960 tree decls;
1961 location_t decl_loc;
1962 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1964 int ext;
1965 tree decl;
1966 ext = disable_extension_diagnostics ();
1967 c_parser_consume_token (parser);
1968 decl = c_parser_struct_declaration (parser);
1969 restore_extension_diagnostics (ext);
1970 return decl;
1972 specs = build_null_declspecs ();
1973 decl_loc = c_parser_peek_token (parser)->location;
1974 c_parser_declspecs (parser, specs, false, true, true);
1975 if (parser->error)
1976 return NULL_TREE;
1977 if (!specs->declspecs_seen_p)
1979 c_parser_error (parser, "expected specifier-qualifier-list");
1980 return NULL_TREE;
1982 finish_declspecs (specs);
1983 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1985 tree ret;
1986 if (!specs->type_seen_p)
1988 pedwarn (decl_loc, OPT_pedantic,
1989 "ISO C forbids member declarations with no members");
1990 shadow_tag_warned (specs, pedantic);
1991 ret = NULL_TREE;
1993 else
1995 /* Support for unnamed structs or unions as members of
1996 structs or unions (which is [a] useful and [b] supports
1997 MS P-SDK). */
1998 tree attrs = NULL;
2000 ret = grokfield (c_parser_peek_token (parser)->location,
2001 build_id_declarator (NULL_TREE), specs,
2002 NULL_TREE, &attrs);
2003 if (ret)
2004 decl_attributes (&ret, attrs, 0);
2006 return ret;
2008 pending_xref_error ();
2009 prefix_attrs = specs->attrs;
2010 all_prefix_attrs = prefix_attrs;
2011 specs->attrs = NULL_TREE;
2012 decls = NULL_TREE;
2013 while (true)
2015 /* Declaring one or more declarators or un-named bit-fields. */
2016 struct c_declarator *declarator;
2017 bool dummy = false;
2018 if (c_parser_next_token_is (parser, CPP_COLON))
2019 declarator = build_id_declarator (NULL_TREE);
2020 else
2021 declarator = c_parser_declarator (parser, specs->type_seen_p,
2022 C_DTR_NORMAL, &dummy);
2023 if (declarator == NULL)
2025 c_parser_skip_to_end_of_block_or_statement (parser);
2026 break;
2028 if (c_parser_next_token_is (parser, CPP_COLON)
2029 || c_parser_next_token_is (parser, CPP_COMMA)
2030 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2031 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2032 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2034 tree postfix_attrs = NULL_TREE;
2035 tree width = NULL_TREE;
2036 tree d;
2037 if (c_parser_next_token_is (parser, CPP_COLON))
2039 c_parser_consume_token (parser);
2040 width = c_parser_expr_no_commas (parser, NULL).value;
2042 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2043 postfix_attrs = c_parser_attributes (parser);
2044 d = grokfield (c_parser_peek_token (parser)->location,
2045 declarator, specs, width, &all_prefix_attrs);
2046 decl_attributes (&d, chainon (postfix_attrs,
2047 all_prefix_attrs), 0);
2048 TREE_CHAIN (d) = decls;
2049 decls = d;
2050 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2051 all_prefix_attrs = chainon (c_parser_attributes (parser),
2052 prefix_attrs);
2053 else
2054 all_prefix_attrs = prefix_attrs;
2055 if (c_parser_next_token_is (parser, CPP_COMMA))
2056 c_parser_consume_token (parser);
2057 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2058 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2060 /* Semicolon consumed in caller. */
2061 break;
2063 else
2065 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2066 break;
2069 else
2071 c_parser_error (parser,
2072 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2073 "%<__attribute__%>");
2074 break;
2077 return decls;
2080 /* Parse a typeof specifier (a GNU extension).
2082 typeof-specifier:
2083 typeof ( expression )
2084 typeof ( type-name )
2087 static struct c_typespec
2088 c_parser_typeof_specifier (c_parser *parser)
2090 struct c_typespec ret;
2091 ret.kind = ctsk_typeof;
2092 ret.spec = error_mark_node;
2093 ret.expr = NULL_TREE;
2094 ret.expr_const_operands = true;
2095 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2096 c_parser_consume_token (parser);
2097 skip_evaluation++;
2098 in_typeof++;
2099 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2101 skip_evaluation--;
2102 in_typeof--;
2103 return ret;
2105 if (c_parser_next_token_starts_typename (parser))
2107 struct c_type_name *type = c_parser_type_name (parser);
2108 skip_evaluation--;
2109 in_typeof--;
2110 if (type != NULL)
2112 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2113 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2116 else
2118 bool was_vm;
2119 location_t here = c_parser_peek_token (parser)->location;
2120 struct c_expr expr = c_parser_expression (parser);
2121 skip_evaluation--;
2122 in_typeof--;
2123 if (TREE_CODE (expr.value) == COMPONENT_REF
2124 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2125 error_at (here, "%<typeof%> applied to a bit-field");
2126 ret.spec = TREE_TYPE (expr.value);
2127 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2128 /* This is returned with the type so that when the type is
2129 evaluated, this can be evaluated. */
2130 if (was_vm)
2131 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2132 pop_maybe_used (was_vm);
2134 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2135 return ret;
2138 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2139 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2140 be redeclared; otherwise it may not. KIND indicates which kind of
2141 declarator is wanted. Returns a valid declarator except in the
2142 case of a syntax error in which case NULL is returned. *SEEN_ID is
2143 set to true if an identifier being declared is seen; this is used
2144 to diagnose bad forms of abstract array declarators and to
2145 determine whether an identifier list is syntactically permitted.
2147 declarator:
2148 pointer[opt] direct-declarator
2150 direct-declarator:
2151 identifier
2152 ( attributes[opt] declarator )
2153 direct-declarator array-declarator
2154 direct-declarator ( parameter-type-list )
2155 direct-declarator ( identifier-list[opt] )
2157 pointer:
2158 * type-qualifier-list[opt]
2159 * type-qualifier-list[opt] pointer
2161 type-qualifier-list:
2162 type-qualifier
2163 attributes
2164 type-qualifier-list type-qualifier
2165 type-qualifier-list attributes
2167 parameter-type-list:
2168 parameter-list
2169 parameter-list , ...
2171 parameter-list:
2172 parameter-declaration
2173 parameter-list , parameter-declaration
2175 parameter-declaration:
2176 declaration-specifiers declarator attributes[opt]
2177 declaration-specifiers abstract-declarator[opt] attributes[opt]
2179 identifier-list:
2180 identifier
2181 identifier-list , identifier
2183 abstract-declarator:
2184 pointer
2185 pointer[opt] direct-abstract-declarator
2187 direct-abstract-declarator:
2188 ( attributes[opt] abstract-declarator )
2189 direct-abstract-declarator[opt] array-declarator
2190 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2192 GNU extensions:
2194 direct-declarator:
2195 direct-declarator ( parameter-forward-declarations
2196 parameter-type-list[opt] )
2198 direct-abstract-declarator:
2199 direct-abstract-declarator[opt] ( parameter-forward-declarations
2200 parameter-type-list[opt] )
2202 parameter-forward-declarations:
2203 parameter-list ;
2204 parameter-forward-declarations parameter-list ;
2206 The uses of attributes shown above are GNU extensions.
2208 Some forms of array declarator are not included in C99 in the
2209 syntax for abstract declarators; these are disallowed elsewhere.
2210 This may be a defect (DR#289).
2212 This function also accepts an omitted abstract declarator as being
2213 an abstract declarator, although not part of the formal syntax. */
2215 static struct c_declarator *
2216 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2217 bool *seen_id)
2219 /* Parse any initial pointer part. */
2220 if (c_parser_next_token_is (parser, CPP_MULT))
2222 struct c_declspecs *quals_attrs = build_null_declspecs ();
2223 struct c_declarator *inner;
2224 c_parser_consume_token (parser);
2225 c_parser_declspecs (parser, quals_attrs, false, false, true);
2226 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2227 if (inner == NULL)
2228 return NULL;
2229 else
2230 return make_pointer_declarator (quals_attrs, inner);
2232 /* Now we have a direct declarator, direct abstract declarator or
2233 nothing (which counts as a direct abstract declarator here). */
2234 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2237 /* Parse a direct declarator or direct abstract declarator; arguments
2238 as c_parser_declarator. */
2240 static struct c_declarator *
2241 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2242 bool *seen_id)
2244 /* The direct declarator must start with an identifier (possibly
2245 omitted) or a parenthesized declarator (possibly abstract). In
2246 an ordinary declarator, initial parentheses must start a
2247 parenthesized declarator. In an abstract declarator or parameter
2248 declarator, they could start a parenthesized declarator or a
2249 parameter list. To tell which, the open parenthesis and any
2250 following attributes must be read. If a declaration specifier
2251 follows, then it is a parameter list; if the specifier is a
2252 typedef name, there might be an ambiguity about redeclaring it,
2253 which is resolved in the direction of treating it as a typedef
2254 name. If a close parenthesis follows, it is also an empty
2255 parameter list, as the syntax does not permit empty abstract
2256 declarators. Otherwise, it is a parenthesized declarator (in
2257 which case the analysis may be repeated inside it, recursively).
2259 ??? There is an ambiguity in a parameter declaration "int
2260 (__attribute__((foo)) x)", where x is not a typedef name: it
2261 could be an abstract declarator for a function, or declare x with
2262 parentheses. The proper resolution of this ambiguity needs
2263 documenting. At present we follow an accident of the old
2264 parser's implementation, whereby the first parameter must have
2265 some declaration specifiers other than just attributes. Thus as
2266 a parameter declaration it is treated as a parenthesized
2267 parameter named x, and as an abstract declarator it is
2268 rejected.
2270 ??? Also following the old parser, attributes inside an empty
2271 parameter list are ignored, making it a list not yielding a
2272 prototype, rather than giving an error or making it have one
2273 parameter with implicit type int.
2275 ??? Also following the old parser, typedef names may be
2276 redeclared in declarators, but not Objective-C class names. */
2278 if (kind != C_DTR_ABSTRACT
2279 && c_parser_next_token_is (parser, CPP_NAME)
2280 && ((type_seen_p
2281 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2282 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2284 struct c_declarator *inner
2285 = build_id_declarator (c_parser_peek_token (parser)->value);
2286 *seen_id = true;
2287 inner->id_loc = c_parser_peek_token (parser)->location;
2288 c_parser_consume_token (parser);
2289 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2292 if (kind != C_DTR_NORMAL
2293 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2295 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2296 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2299 /* Either we are at the end of an abstract declarator, or we have
2300 parentheses. */
2302 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2304 tree attrs;
2305 struct c_declarator *inner;
2306 c_parser_consume_token (parser);
2307 attrs = c_parser_attributes (parser);
2308 if (kind != C_DTR_NORMAL
2309 && (c_parser_next_token_starts_declspecs (parser)
2310 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2312 struct c_arg_info *args
2313 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2314 attrs);
2315 if (args == NULL)
2316 return NULL;
2317 else
2319 inner
2320 = build_function_declarator (args,
2321 build_id_declarator (NULL_TREE));
2322 return c_parser_direct_declarator_inner (parser, *seen_id,
2323 inner);
2326 /* A parenthesized declarator. */
2327 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2328 if (inner != NULL && attrs != NULL)
2329 inner = build_attrs_declarator (attrs, inner);
2330 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2332 c_parser_consume_token (parser);
2333 if (inner == NULL)
2334 return NULL;
2335 else
2336 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2338 else
2340 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2341 "expected %<)%>");
2342 return NULL;
2345 else
2347 if (kind == C_DTR_NORMAL)
2349 c_parser_error (parser, "expected identifier or %<(%>");
2350 return NULL;
2352 else
2353 return build_id_declarator (NULL_TREE);
2357 /* Parse part of a direct declarator or direct abstract declarator,
2358 given that some (in INNER) has already been parsed; ID_PRESENT is
2359 true if an identifier is present, false for an abstract
2360 declarator. */
2362 static struct c_declarator *
2363 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2364 struct c_declarator *inner)
2366 /* Parse a sequence of array declarators and parameter lists. */
2367 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2369 struct c_declarator *declarator;
2370 struct c_declspecs *quals_attrs = build_null_declspecs ();
2371 bool static_seen;
2372 bool star_seen;
2373 tree dimen;
2374 c_parser_consume_token (parser);
2375 c_parser_declspecs (parser, quals_attrs, false, false, true);
2376 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2377 if (static_seen)
2378 c_parser_consume_token (parser);
2379 if (static_seen && !quals_attrs->declspecs_seen_p)
2380 c_parser_declspecs (parser, quals_attrs, false, false, true);
2381 if (!quals_attrs->declspecs_seen_p)
2382 quals_attrs = NULL;
2383 /* If "static" is present, there must be an array dimension.
2384 Otherwise, there may be a dimension, "*", or no
2385 dimension. */
2386 if (static_seen)
2388 star_seen = false;
2389 dimen = c_parser_expr_no_commas (parser, NULL).value;
2391 else
2393 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2395 dimen = NULL_TREE;
2396 star_seen = false;
2398 else if (c_parser_next_token_is (parser, CPP_MULT))
2400 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2402 dimen = NULL_TREE;
2403 star_seen = true;
2404 c_parser_consume_token (parser);
2406 else
2408 star_seen = false;
2409 dimen = c_parser_expr_no_commas (parser, NULL).value;
2412 else
2414 star_seen = false;
2415 dimen = c_parser_expr_no_commas (parser, NULL).value;
2418 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2419 c_parser_consume_token (parser);
2420 else
2422 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2423 "expected %<]%>");
2424 return NULL;
2426 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2427 star_seen);
2428 if (declarator == NULL)
2429 return NULL;
2430 inner = set_array_declarator_inner (declarator, inner);
2431 return c_parser_direct_declarator_inner (parser, id_present, inner);
2433 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2435 tree attrs;
2436 struct c_arg_info *args;
2437 c_parser_consume_token (parser);
2438 attrs = c_parser_attributes (parser);
2439 args = c_parser_parms_declarator (parser, id_present, attrs);
2440 if (args == NULL)
2441 return NULL;
2442 else
2444 inner = build_function_declarator (args, inner);
2445 return c_parser_direct_declarator_inner (parser, id_present, inner);
2448 return inner;
2451 /* Parse a parameter list or identifier list, including the closing
2452 parenthesis but not the opening one. ATTRS are the attributes at
2453 the start of the list. ID_LIST_OK is true if an identifier list is
2454 acceptable; such a list must not have attributes at the start. */
2456 static struct c_arg_info *
2457 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2459 push_scope ();
2460 declare_parm_level ();
2461 /* If the list starts with an identifier, it is an identifier list.
2462 Otherwise, it is either a prototype list or an empty list. */
2463 if (id_list_ok
2464 && !attrs
2465 && c_parser_next_token_is (parser, CPP_NAME)
2466 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2468 tree list = NULL_TREE, *nextp = &list;
2469 while (c_parser_next_token_is (parser, CPP_NAME)
2470 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2472 *nextp = build_tree_list (NULL_TREE,
2473 c_parser_peek_token (parser)->value);
2474 nextp = & TREE_CHAIN (*nextp);
2475 c_parser_consume_token (parser);
2476 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2477 break;
2478 c_parser_consume_token (parser);
2479 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2481 c_parser_error (parser, "expected identifier");
2482 break;
2485 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2487 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2488 ret->parms = 0;
2489 ret->tags = 0;
2490 ret->types = list;
2491 ret->others = 0;
2492 ret->pending_sizes = 0;
2493 ret->had_vla_unspec = 0;
2494 c_parser_consume_token (parser);
2495 pop_scope ();
2496 return ret;
2498 else
2500 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2501 "expected %<)%>");
2502 pop_scope ();
2503 return NULL;
2506 else
2508 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2509 pop_scope ();
2510 return ret;
2514 /* Parse a parameter list (possibly empty), including the closing
2515 parenthesis but not the opening one. ATTRS are the attributes at
2516 the start of the list. */
2518 static struct c_arg_info *
2519 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2521 bool good_parm = false;
2522 /* ??? Following the old parser, forward parameter declarations may
2523 use abstract declarators, and if no real parameter declarations
2524 follow the forward declarations then this is not diagnosed. Also
2525 note as above that attributes are ignored as the only contents of
2526 the parentheses, or as the only contents after forward
2527 declarations. */
2528 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2530 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2531 ret->parms = 0;
2532 ret->tags = 0;
2533 ret->types = 0;
2534 ret->others = 0;
2535 ret->pending_sizes = 0;
2536 ret->had_vla_unspec = 0;
2537 c_parser_consume_token (parser);
2538 return ret;
2540 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2542 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2543 ret->parms = 0;
2544 ret->tags = 0;
2545 ret->others = 0;
2546 ret->pending_sizes = 0;
2547 ret->had_vla_unspec = 0;
2548 /* Suppress -Wold-style-definition for this case. */
2549 ret->types = error_mark_node;
2550 error_at (c_parser_peek_token (parser)->location,
2551 "ISO C requires a named argument before %<...%>");
2552 c_parser_consume_token (parser);
2553 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2555 c_parser_consume_token (parser);
2556 return ret;
2558 else
2560 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2561 "expected %<)%>");
2562 return NULL;
2565 /* Nonempty list of parameters, either terminated with semicolon
2566 (forward declarations; recurse) or with close parenthesis (normal
2567 function) or with ", ... )" (variadic function). */
2568 while (true)
2570 /* Parse a parameter. */
2571 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2572 attrs = NULL_TREE;
2573 if (parm != NULL)
2575 good_parm = true;
2576 push_parm_decl (parm);
2578 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2580 tree new_attrs;
2581 c_parser_consume_token (parser);
2582 mark_forward_parm_decls ();
2583 new_attrs = c_parser_attributes (parser);
2584 return c_parser_parms_list_declarator (parser, new_attrs);
2586 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2588 c_parser_consume_token (parser);
2589 if (good_parm)
2590 return get_parm_info (false);
2591 else
2593 struct c_arg_info *ret
2594 = XOBNEW (&parser_obstack, struct c_arg_info);
2595 ret->parms = 0;
2596 ret->tags = 0;
2597 ret->types = 0;
2598 ret->others = 0;
2599 ret->pending_sizes = 0;
2600 ret->had_vla_unspec = 0;
2601 return ret;
2604 if (!c_parser_require (parser, CPP_COMMA,
2605 "expected %<;%>, %<,%> or %<)%>"))
2607 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2608 get_pending_sizes ();
2609 return NULL;
2611 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2613 c_parser_consume_token (parser);
2614 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2616 c_parser_consume_token (parser);
2617 if (good_parm)
2618 return get_parm_info (true);
2619 else
2621 struct c_arg_info *ret
2622 = XOBNEW (&parser_obstack, struct c_arg_info);
2623 ret->parms = 0;
2624 ret->tags = 0;
2625 ret->types = 0;
2626 ret->others = 0;
2627 ret->pending_sizes = 0;
2628 ret->had_vla_unspec = 0;
2629 return ret;
2632 else
2634 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2635 "expected %<)%>");
2636 get_pending_sizes ();
2637 return NULL;
2643 /* Parse a parameter declaration. ATTRS are the attributes at the
2644 start of the declaration if it is the first parameter. */
2646 static struct c_parm *
2647 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2649 struct c_declspecs *specs;
2650 struct c_declarator *declarator;
2651 tree prefix_attrs;
2652 tree postfix_attrs = NULL_TREE;
2653 bool dummy = false;
2654 if (!c_parser_next_token_starts_declspecs (parser))
2656 /* ??? In some Objective-C cases '...' isn't applicable so there
2657 should be a different message. */
2658 c_parser_error (parser,
2659 "expected declaration specifiers or %<...%>");
2660 c_parser_skip_to_end_of_parameter (parser);
2661 return NULL;
2663 specs = build_null_declspecs ();
2664 if (attrs)
2666 declspecs_add_attrs (specs, attrs);
2667 attrs = NULL_TREE;
2669 c_parser_declspecs (parser, specs, true, true, true);
2670 finish_declspecs (specs);
2671 pending_xref_error ();
2672 prefix_attrs = specs->attrs;
2673 specs->attrs = NULL_TREE;
2674 declarator = c_parser_declarator (parser, specs->type_seen_p,
2675 C_DTR_PARM, &dummy);
2676 if (declarator == NULL)
2678 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2679 return NULL;
2681 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2682 postfix_attrs = c_parser_attributes (parser);
2683 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2684 declarator);
2687 /* Parse a string literal in an asm expression. It should not be
2688 translated, and wide string literals are an error although
2689 permitted by the syntax. This is a GNU extension.
2691 asm-string-literal:
2692 string-literal
2694 ??? At present, following the old parser, the caller needs to have
2695 set lex_untranslated_string to 1. It would be better to follow the
2696 C++ parser rather than using this kludge. */
2698 static tree
2699 c_parser_asm_string_literal (c_parser *parser)
2701 tree str;
2702 if (c_parser_next_token_is (parser, CPP_STRING))
2704 str = c_parser_peek_token (parser)->value;
2705 c_parser_consume_token (parser);
2707 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2709 error_at (c_parser_peek_token (parser)->location,
2710 "wide string literal in %<asm%>");
2711 str = build_string (1, "");
2712 c_parser_consume_token (parser);
2714 else
2716 c_parser_error (parser, "expected string literal");
2717 str = NULL_TREE;
2719 return str;
2722 /* Parse a simple asm expression. This is used in restricted
2723 contexts, where a full expression with inputs and outputs does not
2724 make sense. This is a GNU extension.
2726 simple-asm-expr:
2727 asm ( asm-string-literal )
2730 static tree
2731 c_parser_simple_asm_expr (c_parser *parser)
2733 tree str;
2734 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2735 /* ??? Follow the C++ parser rather than using the
2736 lex_untranslated_string kludge. */
2737 parser->lex_untranslated_string = true;
2738 c_parser_consume_token (parser);
2739 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2741 parser->lex_untranslated_string = false;
2742 return NULL_TREE;
2744 str = c_parser_asm_string_literal (parser);
2745 parser->lex_untranslated_string = false;
2746 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2748 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2749 return NULL_TREE;
2751 return str;
2754 /* Parse (possibly empty) attributes. This is a GNU extension.
2756 attributes:
2757 empty
2758 attributes attribute
2760 attribute:
2761 __attribute__ ( ( attribute-list ) )
2763 attribute-list:
2764 attrib
2765 attribute_list , attrib
2767 attrib:
2768 empty
2769 any-word
2770 any-word ( identifier )
2771 any-word ( identifier , nonempty-expr-list )
2772 any-word ( expr-list )
2774 where the "identifier" must not be declared as a type, and
2775 "any-word" may be any identifier (including one declared as a
2776 type), a reserved word storage class specifier, type specifier or
2777 type qualifier. ??? This still leaves out most reserved keywords
2778 (following the old parser), shouldn't we include them, and why not
2779 allow identifiers declared as types to start the arguments? */
2781 static tree
2782 c_parser_attributes (c_parser *parser)
2784 tree attrs = NULL_TREE;
2785 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2787 /* ??? Follow the C++ parser rather than using the
2788 lex_untranslated_string kludge. */
2789 parser->lex_untranslated_string = true;
2790 c_parser_consume_token (parser);
2791 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2793 parser->lex_untranslated_string = false;
2794 return attrs;
2796 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2798 parser->lex_untranslated_string = false;
2799 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2800 return attrs;
2802 /* Parse the attribute list. */
2803 while (c_parser_next_token_is (parser, CPP_COMMA)
2804 || c_parser_next_token_is (parser, CPP_NAME)
2805 || c_parser_next_token_is (parser, CPP_KEYWORD))
2807 tree attr, attr_name, attr_args;
2808 VEC(tree,gc) *expr_list;
2809 if (c_parser_next_token_is (parser, CPP_COMMA))
2811 c_parser_consume_token (parser);
2812 continue;
2814 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2816 /* ??? See comment above about what keywords are
2817 accepted here. */
2818 bool ok;
2819 switch (c_parser_peek_token (parser)->keyword)
2821 case RID_STATIC:
2822 case RID_UNSIGNED:
2823 case RID_LONG:
2824 case RID_CONST:
2825 case RID_EXTERN:
2826 case RID_REGISTER:
2827 case RID_TYPEDEF:
2828 case RID_SHORT:
2829 case RID_INLINE:
2830 case RID_VOLATILE:
2831 case RID_SIGNED:
2832 case RID_AUTO:
2833 case RID_RESTRICT:
2834 case RID_COMPLEX:
2835 case RID_THREAD:
2836 case RID_INT:
2837 case RID_CHAR:
2838 case RID_FLOAT:
2839 case RID_DOUBLE:
2840 case RID_VOID:
2841 case RID_DFLOAT32:
2842 case RID_DFLOAT64:
2843 case RID_DFLOAT128:
2844 case RID_BOOL:
2845 case RID_FRACT:
2846 case RID_ACCUM:
2847 case RID_SAT:
2848 ok = true;
2849 break;
2850 default:
2851 ok = false;
2852 break;
2854 if (!ok)
2855 break;
2856 /* Accept __attribute__((__const)) as __attribute__((const))
2857 etc. */
2858 attr_name
2859 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
2861 else
2862 attr_name = c_parser_peek_token (parser)->value;
2863 c_parser_consume_token (parser);
2864 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2866 attr = build_tree_list (attr_name, NULL_TREE);
2867 attrs = chainon (attrs, attr);
2868 continue;
2870 c_parser_consume_token (parser);
2871 /* Parse the attribute contents. If they start with an
2872 identifier which is followed by a comma or close
2873 parenthesis, then the arguments start with that
2874 identifier; otherwise they are an expression list. */
2875 if (c_parser_next_token_is (parser, CPP_NAME)
2876 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2877 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2878 || (c_parser_peek_2nd_token (parser)->type
2879 == CPP_CLOSE_PAREN)))
2881 tree arg1 = c_parser_peek_token (parser)->value;
2882 c_parser_consume_token (parser);
2883 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2884 attr_args = build_tree_list (NULL_TREE, arg1);
2885 else
2887 tree tree_list;
2888 c_parser_consume_token (parser);
2889 expr_list = c_parser_expr_list (parser, false, true, NULL);
2890 tree_list = build_tree_list_vec (expr_list);
2891 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
2892 release_tree_vector (expr_list);
2895 else
2897 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2898 attr_args = NULL_TREE;
2899 else
2901 expr_list = c_parser_expr_list (parser, false, true, NULL);
2902 attr_args = build_tree_list_vec (expr_list);
2903 release_tree_vector (expr_list);
2906 attr = build_tree_list (attr_name, attr_args);
2907 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2908 c_parser_consume_token (parser);
2909 else
2911 parser->lex_untranslated_string = false;
2912 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2913 "expected %<)%>");
2914 return attrs;
2916 attrs = chainon (attrs, attr);
2918 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2919 c_parser_consume_token (parser);
2920 else
2922 parser->lex_untranslated_string = false;
2923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2924 "expected %<)%>");
2925 return attrs;
2927 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2928 c_parser_consume_token (parser);
2929 else
2931 parser->lex_untranslated_string = false;
2932 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2933 "expected %<)%>");
2934 return attrs;
2936 parser->lex_untranslated_string = false;
2938 return attrs;
2941 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2943 type-name:
2944 specifier-qualifier-list abstract-declarator[opt]
2947 static struct c_type_name *
2948 c_parser_type_name (c_parser *parser)
2950 struct c_declspecs *specs = build_null_declspecs ();
2951 struct c_declarator *declarator;
2952 struct c_type_name *ret;
2953 bool dummy = false;
2954 c_parser_declspecs (parser, specs, false, true, true);
2955 if (!specs->declspecs_seen_p)
2957 c_parser_error (parser, "expected specifier-qualifier-list");
2958 return NULL;
2960 pending_xref_error ();
2961 finish_declspecs (specs);
2962 declarator = c_parser_declarator (parser, specs->type_seen_p,
2963 C_DTR_ABSTRACT, &dummy);
2964 if (declarator == NULL)
2965 return NULL;
2966 ret = XOBNEW (&parser_obstack, struct c_type_name);
2967 ret->specs = specs;
2968 ret->declarator = declarator;
2969 return ret;
2972 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2974 initializer:
2975 assignment-expression
2976 { initializer-list }
2977 { initializer-list , }
2979 initializer-list:
2980 designation[opt] initializer
2981 initializer-list , designation[opt] initializer
2983 designation:
2984 designator-list =
2986 designator-list:
2987 designator
2988 designator-list designator
2990 designator:
2991 array-designator
2992 . identifier
2994 array-designator:
2995 [ constant-expression ]
2997 GNU extensions:
2999 initializer:
3002 designation:
3003 array-designator
3004 identifier :
3006 array-designator:
3007 [ constant-expression ... constant-expression ]
3009 Any expression without commas is accepted in the syntax for the
3010 constant-expressions, with non-constant expressions rejected later.
3012 This function is only used for top-level initializers; for nested
3013 ones, see c_parser_initval. */
3015 static struct c_expr
3016 c_parser_initializer (c_parser *parser)
3018 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3019 return c_parser_braced_init (parser, NULL_TREE, false);
3020 else
3022 struct c_expr ret;
3023 ret = c_parser_expr_no_commas (parser, NULL);
3024 if (TREE_CODE (ret.value) != STRING_CST
3025 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3026 ret = default_function_array_conversion (ret);
3027 return ret;
3031 /* Parse a braced initializer list. TYPE is the type specified for a
3032 compound literal, and NULL_TREE for other initializers and for
3033 nested braced lists. NESTED_P is true for nested braced lists,
3034 false for the list of a compound literal or the list that is the
3035 top-level initializer in a declaration. */
3037 static struct c_expr
3038 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3040 location_t brace_loc = c_parser_peek_token (parser)->location;
3041 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3042 c_parser_consume_token (parser);
3043 if (nested_p)
3044 push_init_level (0);
3045 else
3046 really_start_incremental_init (type);
3047 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3049 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3051 else
3053 /* Parse a non-empty initializer list, possibly with a trailing
3054 comma. */
3055 while (true)
3057 c_parser_initelt (parser);
3058 if (parser->error)
3059 break;
3060 if (c_parser_next_token_is (parser, CPP_COMMA))
3061 c_parser_consume_token (parser);
3062 else
3063 break;
3064 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3065 break;
3068 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3070 struct c_expr ret;
3071 ret.value = error_mark_node;
3072 ret.original_code = ERROR_MARK;
3073 ret.original_type = NULL;
3074 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3075 pop_init_level (0);
3076 return ret;
3078 c_parser_consume_token (parser);
3079 return pop_init_level (0);
3082 /* Parse a nested initializer, including designators. */
3084 static void
3085 c_parser_initelt (c_parser *parser)
3087 /* Parse any designator or designator list. A single array
3088 designator may have the subsequent "=" omitted in GNU C, but a
3089 longer list or a structure member designator may not. */
3090 if (c_parser_next_token_is (parser, CPP_NAME)
3091 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3093 /* Old-style structure member designator. */
3094 set_init_label (c_parser_peek_token (parser)->value);
3095 /* Use the colon as the error location. */
3096 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3097 "obsolete use of designated initializer with %<:%>");
3098 c_parser_consume_token (parser);
3099 c_parser_consume_token (parser);
3101 else
3103 /* des_seen is 0 if there have been no designators, 1 if there
3104 has been a single array designator and 2 otherwise. */
3105 int des_seen = 0;
3106 /* Location of a designator. */
3107 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3108 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3109 || c_parser_next_token_is (parser, CPP_DOT))
3111 int des_prev = des_seen;
3112 if (!des_seen)
3113 des_loc = c_parser_peek_token (parser)->location;
3114 if (des_seen < 2)
3115 des_seen++;
3116 if (c_parser_next_token_is (parser, CPP_DOT))
3118 des_seen = 2;
3119 c_parser_consume_token (parser);
3120 if (c_parser_next_token_is (parser, CPP_NAME))
3122 set_init_label (c_parser_peek_token (parser)->value);
3123 c_parser_consume_token (parser);
3125 else
3127 struct c_expr init;
3128 init.value = error_mark_node;
3129 init.original_code = ERROR_MARK;
3130 init.original_type = NULL;
3131 c_parser_error (parser, "expected identifier");
3132 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3133 process_init_element (init, false);
3134 return;
3137 else
3139 tree first, second;
3140 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3141 /* ??? Following the old parser, [ objc-receiver
3142 objc-message-args ] is accepted as an initializer,
3143 being distinguished from a designator by what follows
3144 the first assignment expression inside the square
3145 brackets, but after a first array designator a
3146 subsequent square bracket is for Objective-C taken to
3147 start an expression, using the obsolete form of
3148 designated initializer without '=', rather than
3149 possibly being a second level of designation: in LALR
3150 terms, the '[' is shifted rather than reducing
3151 designator to designator-list. */
3152 if (des_prev == 1 && c_dialect_objc ())
3154 des_seen = des_prev;
3155 break;
3157 if (des_prev == 0 && c_dialect_objc ())
3159 /* This might be an array designator or an
3160 Objective-C message expression. If the former,
3161 continue parsing here; if the latter, parse the
3162 remainder of the initializer given the starting
3163 primary-expression. ??? It might make sense to
3164 distinguish when des_prev == 1 as well; see
3165 previous comment. */
3166 tree rec, args;
3167 struct c_expr mexpr;
3168 c_parser_consume_token (parser);
3169 if (c_parser_peek_token (parser)->type == CPP_NAME
3170 && ((c_parser_peek_token (parser)->id_kind
3171 == C_ID_TYPENAME)
3172 || (c_parser_peek_token (parser)->id_kind
3173 == C_ID_CLASSNAME)))
3175 /* Type name receiver. */
3176 tree id = c_parser_peek_token (parser)->value;
3177 c_parser_consume_token (parser);
3178 rec = objc_get_class_reference (id);
3179 goto parse_message_args;
3181 first = c_parser_expr_no_commas (parser, NULL).value;
3182 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3183 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3184 goto array_desig_after_first;
3185 /* Expression receiver. So far only one part
3186 without commas has been parsed; there might be
3187 more of the expression. */
3188 rec = first;
3189 while (c_parser_next_token_is (parser, CPP_COMMA))
3191 struct c_expr next;
3192 c_parser_consume_token (parser);
3193 next = c_parser_expr_no_commas (parser, NULL);
3194 next = default_function_array_conversion (next);
3195 rec = build_compound_expr (rec, next.value);
3197 parse_message_args:
3198 /* Now parse the objc-message-args. */
3199 args = c_parser_objc_message_args (parser);
3200 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3201 "expected %<]%>");
3202 mexpr.value
3203 = objc_build_message_expr (build_tree_list (rec, args));
3204 mexpr.original_code = ERROR_MARK;
3205 mexpr.original_type = NULL;
3206 /* Now parse and process the remainder of the
3207 initializer, starting with this message
3208 expression as a primary-expression. */
3209 c_parser_initval (parser, &mexpr);
3210 return;
3212 c_parser_consume_token (parser);
3213 first = c_parser_expr_no_commas (parser, NULL).value;
3214 array_desig_after_first:
3215 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3217 ellipsis_loc = c_parser_peek_token (parser)->location;
3218 c_parser_consume_token (parser);
3219 second = c_parser_expr_no_commas (parser, NULL).value;
3221 else
3222 second = NULL_TREE;
3223 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3225 c_parser_consume_token (parser);
3226 set_init_index (first, second);
3227 if (second)
3228 pedwarn (ellipsis_loc, OPT_pedantic,
3229 "ISO C forbids specifying range of elements to initialize");
3231 else
3232 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3233 "expected %<]%>");
3236 if (des_seen >= 1)
3238 if (c_parser_next_token_is (parser, CPP_EQ))
3240 if (!flag_isoc99)
3241 pedwarn (des_loc, OPT_pedantic,
3242 "ISO C90 forbids specifying subobject to initialize");
3243 c_parser_consume_token (parser);
3245 else
3247 if (des_seen == 1)
3248 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3249 "obsolete use of designated initializer without %<=%>");
3250 else
3252 struct c_expr init;
3253 init.value = error_mark_node;
3254 init.original_code = ERROR_MARK;
3255 init.original_type = NULL;
3256 c_parser_error (parser, "expected %<=%>");
3257 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3258 process_init_element (init, false);
3259 return;
3264 c_parser_initval (parser, NULL);
3267 /* Parse a nested initializer; as c_parser_initializer but parses
3268 initializers within braced lists, after any designators have been
3269 applied. If AFTER is not NULL then it is an Objective-C message
3270 expression which is the primary-expression starting the
3271 initializer. */
3273 static void
3274 c_parser_initval (c_parser *parser, struct c_expr *after)
3276 struct c_expr init;
3277 gcc_assert (!after || c_dialect_objc ());
3278 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3279 init = c_parser_braced_init (parser, NULL_TREE, true);
3280 else
3282 init = c_parser_expr_no_commas (parser, after);
3283 if (init.value != NULL_TREE
3284 && TREE_CODE (init.value) != STRING_CST
3285 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3286 init = default_function_array_conversion (init);
3288 process_init_element (init, false);
3291 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3292 C99 6.8.2).
3294 compound-statement:
3295 { block-item-list[opt] }
3296 { label-declarations block-item-list }
3298 block-item-list:
3299 block-item
3300 block-item-list block-item
3302 block-item:
3303 nested-declaration
3304 statement
3306 nested-declaration:
3307 declaration
3309 GNU extensions:
3311 compound-statement:
3312 { label-declarations block-item-list }
3314 nested-declaration:
3315 __extension__ nested-declaration
3316 nested-function-definition
3318 label-declarations:
3319 label-declaration
3320 label-declarations label-declaration
3322 label-declaration:
3323 __label__ identifier-list ;
3325 Allowing the mixing of declarations and code is new in C99. The
3326 GNU syntax also permits (not shown above) labels at the end of
3327 compound statements, which yield an error. We don't allow labels
3328 on declarations; this might seem like a natural extension, but
3329 there would be a conflict between attributes on the label and
3330 prefix attributes on the declaration. ??? The syntax follows the
3331 old parser in requiring something after label declarations.
3332 Although they are erroneous if the labels declared aren't defined,
3333 is it useful for the syntax to be this way?
3335 OpenMP:
3337 block-item:
3338 openmp-directive
3340 openmp-directive:
3341 barrier-directive
3342 flush-directive */
3344 static tree
3345 c_parser_compound_statement (c_parser *parser)
3347 tree stmt;
3348 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3350 /* Ensure a scope is entered and left anyway to avoid confusion
3351 if we have just prepared to enter a function body. */
3352 stmt = c_begin_compound_stmt (true);
3353 c_end_compound_stmt (stmt, true);
3354 return error_mark_node;
3356 stmt = c_begin_compound_stmt (true);
3357 c_parser_compound_statement_nostart (parser);
3358 return c_end_compound_stmt (stmt, true);
3361 /* Parse a compound statement except for the opening brace. This is
3362 used for parsing both compound statements and statement expressions
3363 (which follow different paths to handling the opening). */
3365 static void
3366 c_parser_compound_statement_nostart (c_parser *parser)
3368 bool last_stmt = false;
3369 bool last_label = false;
3370 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3371 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3372 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3374 c_parser_consume_token (parser);
3375 return;
3377 mark_valid_location_for_stdc_pragma (true);
3378 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3380 location_t err_loc = c_parser_peek_token (parser)->location;
3381 /* Read zero or more forward-declarations for labels that nested
3382 functions can jump to. */
3383 mark_valid_location_for_stdc_pragma (false);
3384 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3386 c_parser_consume_token (parser);
3387 /* Any identifiers, including those declared as type names,
3388 are OK here. */
3389 while (true)
3391 tree label;
3392 if (c_parser_next_token_is_not (parser, CPP_NAME))
3394 c_parser_error (parser, "expected identifier");
3395 break;
3397 label
3398 = declare_label (c_parser_peek_token (parser)->value);
3399 C_DECLARED_LABEL_FLAG (label) = 1;
3400 add_stmt (build_stmt (DECL_EXPR, label));
3401 c_parser_consume_token (parser);
3402 if (c_parser_next_token_is (parser, CPP_COMMA))
3403 c_parser_consume_token (parser);
3404 else
3405 break;
3407 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3409 pedwarn (err_loc, OPT_pedantic, "ISO C forbids label declarations");
3411 /* We must now have at least one statement, label or declaration. */
3412 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3414 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3415 c_parser_error (parser, "expected declaration or statement");
3416 c_parser_consume_token (parser);
3417 return;
3419 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3421 location_t loc = c_parser_peek_token (parser)->location;
3422 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3423 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3424 || (c_parser_next_token_is (parser, CPP_NAME)
3425 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3427 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3428 label_loc = c_parser_peek_2nd_token (parser)->location;
3429 else
3430 label_loc = c_parser_peek_token (parser)->location;
3431 last_label = true;
3432 last_stmt = false;
3433 mark_valid_location_for_stdc_pragma (false);
3434 c_parser_label (parser);
3436 else if (!last_label
3437 && c_parser_next_token_starts_declspecs (parser))
3439 last_label = false;
3440 mark_valid_location_for_stdc_pragma (false);
3441 c_parser_declaration_or_fndef (parser, true, true, true, true);
3442 if (last_stmt)
3443 pedwarn_c90 (loc,
3444 (pedantic && !flag_isoc99)
3445 ? OPT_pedantic
3446 : OPT_Wdeclaration_after_statement,
3447 "ISO C90 forbids mixed declarations and code");
3448 last_stmt = false;
3450 else if (!last_label
3451 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3453 /* __extension__ can start a declaration, but is also an
3454 unary operator that can start an expression. Consume all
3455 but the last of a possible series of __extension__ to
3456 determine which. */
3457 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3458 && (c_parser_peek_2nd_token (parser)->keyword
3459 == RID_EXTENSION))
3460 c_parser_consume_token (parser);
3461 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3463 int ext;
3464 ext = disable_extension_diagnostics ();
3465 c_parser_consume_token (parser);
3466 last_label = false;
3467 mark_valid_location_for_stdc_pragma (false);
3468 c_parser_declaration_or_fndef (parser, true, true, true, true);
3469 /* Following the old parser, __extension__ does not
3470 disable this diagnostic. */
3471 restore_extension_diagnostics (ext);
3472 if (last_stmt)
3473 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3474 ? OPT_pedantic
3475 : OPT_Wdeclaration_after_statement,
3476 "ISO C90 forbids mixed declarations and code");
3477 last_stmt = false;
3479 else
3480 goto statement;
3482 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3484 /* External pragmas, and some omp pragmas, are not associated
3485 with regular c code, and so are not to be considered statements
3486 syntactically. This ensures that the user doesn't put them
3487 places that would turn into syntax errors if the directive
3488 were ignored. */
3489 if (c_parser_pragma (parser, pragma_compound))
3490 last_label = false, last_stmt = true;
3492 else if (c_parser_next_token_is (parser, CPP_EOF))
3494 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3495 c_parser_error (parser, "expected declaration or statement");
3496 return;
3498 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3500 if (parser->in_if_block)
3502 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3503 error_at (loc, """expected %<}%> before %<else%>");
3504 return;
3506 else
3508 error_at (loc, "%<else%> without a previous %<if%>");
3509 c_parser_consume_token (parser);
3510 continue;
3513 else
3515 statement:
3516 last_label = false;
3517 last_stmt = true;
3518 mark_valid_location_for_stdc_pragma (false);
3519 c_parser_statement_after_labels (parser);
3522 parser->error = false;
3524 if (last_label)
3525 error_at (label_loc, "label at end of compound statement");
3526 c_parser_consume_token (parser);
3527 /* Restore the value we started with. */
3528 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3531 /* Parse a label (C90 6.6.1, C99 6.8.1).
3533 label:
3534 identifier : attributes[opt]
3535 case constant-expression :
3536 default :
3538 GNU extensions:
3540 label:
3541 case constant-expression ... constant-expression :
3543 The use of attributes on labels is a GNU extension. The syntax in
3544 GNU C accepts any expressions without commas, non-constant
3545 expressions being rejected later. */
3547 static void
3548 c_parser_label (c_parser *parser)
3550 location_t loc1 = c_parser_peek_token (parser)->location;
3551 tree label = NULL_TREE;
3552 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3554 tree exp1, exp2;
3555 c_parser_consume_token (parser);
3556 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3557 if (c_parser_next_token_is (parser, CPP_COLON))
3559 c_parser_consume_token (parser);
3560 label = do_case (exp1, NULL_TREE);
3562 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3564 c_parser_consume_token (parser);
3565 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3566 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3567 label = do_case (exp1, exp2);
3569 else
3570 c_parser_error (parser, "expected %<:%> or %<...%>");
3572 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3574 c_parser_consume_token (parser);
3575 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3576 label = do_case (NULL_TREE, NULL_TREE);
3578 else
3580 tree name = c_parser_peek_token (parser)->value;
3581 tree tlab;
3582 tree attrs;
3583 location_t loc2 = c_parser_peek_token (parser)->location;
3584 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3585 c_parser_consume_token (parser);
3586 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3587 c_parser_consume_token (parser);
3588 attrs = c_parser_attributes (parser);
3589 tlab = define_label (loc2, name);
3590 if (tlab)
3592 decl_attributes (&tlab, attrs, 0);
3593 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3596 if (label)
3598 SET_EXPR_LOCATION (label, loc1);
3599 if (c_parser_next_token_starts_declspecs (parser)
3600 && !(c_parser_next_token_is (parser, CPP_NAME)
3601 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3603 error_at (c_parser_peek_token (parser)->location,
3604 "a label can only be part of a statement and "
3605 "a declaration is not a statement");
3606 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3607 /*nested*/ true, /*empty_ok*/ false,
3608 /*start_attr_ok*/ true);
3613 /* Parse a statement (C90 6.6, C99 6.8).
3615 statement:
3616 labeled-statement
3617 compound-statement
3618 expression-statement
3619 selection-statement
3620 iteration-statement
3621 jump-statement
3623 labeled-statement:
3624 label statement
3626 expression-statement:
3627 expression[opt] ;
3629 selection-statement:
3630 if-statement
3631 switch-statement
3633 iteration-statement:
3634 while-statement
3635 do-statement
3636 for-statement
3638 jump-statement:
3639 goto identifier ;
3640 continue ;
3641 break ;
3642 return expression[opt] ;
3644 GNU extensions:
3646 statement:
3647 asm-statement
3649 jump-statement:
3650 goto * expression ;
3652 Objective-C:
3654 statement:
3655 objc-throw-statement
3656 objc-try-catch-statement
3657 objc-synchronized-statement
3659 objc-throw-statement:
3660 @throw expression ;
3661 @throw ;
3663 OpenMP:
3665 statement:
3666 openmp-construct
3668 openmp-construct:
3669 parallel-construct
3670 for-construct
3671 sections-construct
3672 single-construct
3673 parallel-for-construct
3674 parallel-sections-construct
3675 master-construct
3676 critical-construct
3677 atomic-construct
3678 ordered-construct
3680 parallel-construct:
3681 parallel-directive structured-block
3683 for-construct:
3684 for-directive iteration-statement
3686 sections-construct:
3687 sections-directive section-scope
3689 single-construct:
3690 single-directive structured-block
3692 parallel-for-construct:
3693 parallel-for-directive iteration-statement
3695 parallel-sections-construct:
3696 parallel-sections-directive section-scope
3698 master-construct:
3699 master-directive structured-block
3701 critical-construct:
3702 critical-directive structured-block
3704 atomic-construct:
3705 atomic-directive expression-statement
3707 ordered-construct:
3708 ordered-directive structured-block */
3710 static void
3711 c_parser_statement (c_parser *parser)
3713 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3714 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3715 || (c_parser_next_token_is (parser, CPP_NAME)
3716 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3717 c_parser_label (parser);
3718 c_parser_statement_after_labels (parser);
3721 /* Parse a statement, other than a labeled statement. */
3723 static void
3724 c_parser_statement_after_labels (c_parser *parser)
3726 location_t loc = c_parser_peek_token (parser)->location;
3727 tree stmt = NULL_TREE;
3728 bool in_if_block = parser->in_if_block;
3729 parser->in_if_block = false;
3730 switch (c_parser_peek_token (parser)->type)
3732 case CPP_OPEN_BRACE:
3733 add_stmt (c_parser_compound_statement (parser));
3734 break;
3735 case CPP_KEYWORD:
3736 switch (c_parser_peek_token (parser)->keyword)
3738 case RID_IF:
3739 c_parser_if_statement (parser);
3740 break;
3741 case RID_SWITCH:
3742 c_parser_switch_statement (parser);
3743 break;
3744 case RID_WHILE:
3745 c_parser_while_statement (parser);
3746 break;
3747 case RID_DO:
3748 c_parser_do_statement (parser);
3749 break;
3750 case RID_FOR:
3751 c_parser_for_statement (parser);
3752 break;
3753 case RID_GOTO:
3754 c_parser_consume_token (parser);
3755 if (c_parser_next_token_is (parser, CPP_NAME))
3757 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3758 c_parser_consume_token (parser);
3760 else if (c_parser_next_token_is (parser, CPP_MULT))
3762 c_parser_consume_token (parser);
3763 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3765 else
3766 c_parser_error (parser, "expected identifier or %<*%>");
3767 goto expect_semicolon;
3768 case RID_CONTINUE:
3769 c_parser_consume_token (parser);
3770 stmt = c_finish_bc_stmt (&c_cont_label, false);
3771 goto expect_semicolon;
3772 case RID_BREAK:
3773 c_parser_consume_token (parser);
3774 stmt = c_finish_bc_stmt (&c_break_label, true);
3775 goto expect_semicolon;
3776 case RID_RETURN:
3777 c_parser_consume_token (parser);
3778 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3780 stmt = c_finish_return (NULL_TREE, NULL_TREE);
3781 c_parser_consume_token (parser);
3783 else
3785 struct c_expr expr = c_parser_expression_conv (parser);
3786 stmt = c_finish_return (expr.value, expr.original_type);
3787 goto expect_semicolon;
3789 break;
3790 case RID_ASM:
3791 stmt = c_parser_asm_statement (parser);
3792 break;
3793 case RID_THROW:
3794 gcc_assert (c_dialect_objc ());
3795 c_parser_consume_token (parser);
3796 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3798 stmt = objc_build_throw_stmt (NULL_TREE);
3799 c_parser_consume_token (parser);
3801 else
3803 tree expr = c_parser_expression (parser).value;
3804 expr = c_fully_fold (expr, false, NULL);
3805 stmt = objc_build_throw_stmt (expr);
3806 goto expect_semicolon;
3808 break;
3809 case RID_TRY:
3810 gcc_assert (c_dialect_objc ());
3811 c_parser_objc_try_catch_statement (parser);
3812 break;
3813 case RID_AT_SYNCHRONIZED:
3814 gcc_assert (c_dialect_objc ());
3815 c_parser_objc_synchronized_statement (parser);
3816 break;
3817 default:
3818 goto expr_stmt;
3820 break;
3821 case CPP_SEMICOLON:
3822 c_parser_consume_token (parser);
3823 break;
3824 case CPP_CLOSE_PAREN:
3825 case CPP_CLOSE_SQUARE:
3826 /* Avoid infinite loop in error recovery:
3827 c_parser_skip_until_found stops at a closing nesting
3828 delimiter without consuming it, but here we need to consume
3829 it to proceed further. */
3830 c_parser_error (parser, "expected statement");
3831 c_parser_consume_token (parser);
3832 break;
3833 case CPP_PRAGMA:
3834 c_parser_pragma (parser, pragma_stmt);
3835 break;
3836 default:
3837 expr_stmt:
3838 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3839 expect_semicolon:
3840 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3841 break;
3843 /* Two cases cannot and do not have line numbers associated: If stmt
3844 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3845 cannot hold line numbers. But that's OK because the statement
3846 will either be changed to a MODIFY_EXPR during gimplification of
3847 the statement expr, or discarded. If stmt was compound, but
3848 without new variables, we will have skipped the creation of a
3849 BIND and will have a bare STATEMENT_LIST. But that's OK because
3850 (recursively) all of the component statements should already have
3851 line numbers assigned. ??? Can we discard no-op statements
3852 earlier? */
3853 protected_set_expr_location (stmt, loc);
3855 parser->in_if_block = in_if_block;
3858 /* Parse the condition from an if, do, while or for statements. */
3860 static tree
3861 c_parser_condition (c_parser *parser)
3863 location_t loc;
3864 tree cond;
3865 loc = c_parser_peek_token (parser)->location;
3866 cond = c_parser_expression_conv (parser).value;
3867 cond = c_objc_common_truthvalue_conversion (loc, cond);
3868 cond = c_fully_fold (cond, false, NULL);
3869 if (warn_sequence_point)
3870 verify_sequence_points (cond);
3871 return cond;
3874 /* Parse a parenthesized condition from an if, do or while statement.
3876 condition:
3877 ( expression )
3879 static tree
3880 c_parser_paren_condition (c_parser *parser)
3882 tree cond;
3883 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3884 return error_mark_node;
3885 cond = c_parser_condition (parser);
3886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3887 return cond;
3890 /* Parse a statement which is a block in C99. */
3892 static tree
3893 c_parser_c99_block_statement (c_parser *parser)
3895 tree block = c_begin_compound_stmt (flag_isoc99);
3896 c_parser_statement (parser);
3897 return c_end_compound_stmt (block, flag_isoc99);
3900 /* Parse the body of an if statement. This is just parsing a
3901 statement but (a) it is a block in C99, (b) we track whether the
3902 body is an if statement for the sake of -Wparentheses warnings, (c)
3903 we handle an empty body specially for the sake of -Wempty-body
3904 warnings, and (d) we call parser_compound_statement directly
3905 because c_parser_statement_after_labels resets
3906 parser->in_if_block. */
3908 static tree
3909 c_parser_if_body (c_parser *parser, bool *if_p)
3911 tree block = c_begin_compound_stmt (flag_isoc99);
3912 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3913 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3914 || (c_parser_next_token_is (parser, CPP_NAME)
3915 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3916 c_parser_label (parser);
3917 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3918 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3920 location_t loc = c_parser_peek_token (parser)->location;
3921 add_stmt (build_empty_stmt ());
3922 c_parser_consume_token (parser);
3923 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
3924 warning_at (loc, OPT_Wempty_body,
3925 "suggest braces around empty body in an %<if%> statement");
3927 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3928 add_stmt (c_parser_compound_statement (parser));
3929 else
3930 c_parser_statement_after_labels (parser);
3931 return c_end_compound_stmt (block, flag_isoc99);
3934 /* Parse the else body of an if statement. This is just parsing a
3935 statement but (a) it is a block in C99, (b) we handle an empty body
3936 specially for the sake of -Wempty-body warnings. */
3938 static tree
3939 c_parser_else_body (c_parser *parser)
3941 tree block = c_begin_compound_stmt (flag_isoc99);
3942 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3943 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3944 || (c_parser_next_token_is (parser, CPP_NAME)
3945 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3946 c_parser_label (parser);
3947 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3949 warning_at (c_parser_peek_token (parser)->location,
3950 OPT_Wempty_body,
3951 "suggest braces around empty body in an %<else%> statement");
3952 add_stmt (build_empty_stmt ());
3953 c_parser_consume_token (parser);
3955 else
3956 c_parser_statement_after_labels (parser);
3957 return c_end_compound_stmt (block, flag_isoc99);
3960 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3962 if-statement:
3963 if ( expression ) statement
3964 if ( expression ) statement else statement
3967 static void
3968 c_parser_if_statement (c_parser *parser)
3970 tree block;
3971 location_t loc;
3972 tree cond;
3973 bool first_if = false;
3974 tree first_body, second_body;
3975 bool in_if_block;
3977 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3978 c_parser_consume_token (parser);
3979 block = c_begin_compound_stmt (flag_isoc99);
3980 loc = c_parser_peek_token (parser)->location;
3981 cond = c_parser_paren_condition (parser);
3982 in_if_block = parser->in_if_block;
3983 parser->in_if_block = true;
3984 first_body = c_parser_if_body (parser, &first_if);
3985 parser->in_if_block = in_if_block;
3986 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3988 c_parser_consume_token (parser);
3989 second_body = c_parser_else_body (parser);
3991 else
3992 second_body = NULL_TREE;
3993 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3994 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3997 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3999 switch-statement:
4000 switch (expression) statement
4003 static void
4004 c_parser_switch_statement (c_parser *parser)
4006 tree block, expr, body, save_break;
4007 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4008 c_parser_consume_token (parser);
4009 block = c_begin_compound_stmt (flag_isoc99);
4010 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4012 expr = c_parser_expression (parser).value;
4013 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4015 else
4016 expr = error_mark_node;
4017 c_start_case (expr);
4018 save_break = c_break_label;
4019 c_break_label = NULL_TREE;
4020 body = c_parser_c99_block_statement (parser);
4021 c_finish_case (body);
4022 if (c_break_label)
4023 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
4024 c_break_label = save_break;
4025 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4028 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4030 while-statement:
4031 while (expression) statement
4034 static void
4035 c_parser_while_statement (c_parser *parser)
4037 tree block, cond, body, save_break, save_cont;
4038 location_t loc;
4039 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4040 c_parser_consume_token (parser);
4041 block = c_begin_compound_stmt (flag_isoc99);
4042 loc = c_parser_peek_token (parser)->location;
4043 cond = c_parser_paren_condition (parser);
4044 save_break = c_break_label;
4045 c_break_label = NULL_TREE;
4046 save_cont = c_cont_label;
4047 c_cont_label = NULL_TREE;
4048 body = c_parser_c99_block_statement (parser);
4049 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4050 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4051 c_break_label = save_break;
4052 c_cont_label = save_cont;
4055 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4057 do-statement:
4058 do statement while ( expression ) ;
4061 static void
4062 c_parser_do_statement (c_parser *parser)
4064 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4065 location_t loc;
4066 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4067 c_parser_consume_token (parser);
4068 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4069 warning_at (c_parser_peek_token (parser)->location,
4070 OPT_Wempty_body,
4071 "suggest braces around empty body in %<do%> statement");
4072 block = c_begin_compound_stmt (flag_isoc99);
4073 loc = c_parser_peek_token (parser)->location;
4074 save_break = c_break_label;
4075 c_break_label = NULL_TREE;
4076 save_cont = c_cont_label;
4077 c_cont_label = NULL_TREE;
4078 body = c_parser_c99_block_statement (parser);
4079 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4080 new_break = c_break_label;
4081 c_break_label = save_break;
4082 new_cont = c_cont_label;
4083 c_cont_label = save_cont;
4084 cond = c_parser_paren_condition (parser);
4085 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4086 c_parser_skip_to_end_of_block_or_statement (parser);
4087 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4088 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4091 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4093 for-statement:
4094 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4095 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4097 The form with a declaration is new in C99.
4099 ??? In accordance with the old parser, the declaration may be a
4100 nested function, which is then rejected in check_for_loop_decls,
4101 but does it make any sense for this to be included in the grammar?
4102 Note in particular that the nested function does not include a
4103 trailing ';', whereas the "declaration" production includes one.
4104 Also, can we reject bad declarations earlier and cheaper than
4105 check_for_loop_decls? */
4107 static void
4108 c_parser_for_statement (c_parser *parser)
4110 tree block, cond, incr, save_break, save_cont, body;
4111 location_t loc;
4112 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4113 loc = c_parser_peek_token (parser)->location;
4114 c_parser_consume_token (parser);
4115 block = c_begin_compound_stmt (flag_isoc99);
4116 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4118 /* Parse the initialization declaration or expression. */
4119 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4121 c_parser_consume_token (parser);
4122 c_finish_expr_stmt (NULL_TREE);
4124 else if (c_parser_next_token_starts_declspecs (parser))
4126 c_parser_declaration_or_fndef (parser, true, true, true, true);
4127 check_for_loop_decls ();
4129 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4131 /* __extension__ can start a declaration, but is also an
4132 unary operator that can start an expression. Consume all
4133 but the last of a possible series of __extension__ to
4134 determine which. */
4135 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4136 && (c_parser_peek_2nd_token (parser)->keyword
4137 == RID_EXTENSION))
4138 c_parser_consume_token (parser);
4139 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4141 int ext;
4142 ext = disable_extension_diagnostics ();
4143 c_parser_consume_token (parser);
4144 c_parser_declaration_or_fndef (parser, true, true, true, true);
4145 restore_extension_diagnostics (ext);
4146 check_for_loop_decls ();
4148 else
4149 goto init_expr;
4151 else
4153 init_expr:
4154 c_finish_expr_stmt (c_parser_expression (parser).value);
4155 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4157 /* Parse the loop condition. */
4158 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4160 c_parser_consume_token (parser);
4161 cond = NULL_TREE;
4163 else
4165 cond = c_parser_condition (parser);
4166 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4168 /* Parse the increment expression. */
4169 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4170 incr = c_process_expr_stmt (NULL_TREE);
4171 else
4172 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4173 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4175 else
4177 cond = error_mark_node;
4178 incr = error_mark_node;
4180 save_break = c_break_label;
4181 c_break_label = NULL_TREE;
4182 save_cont = c_cont_label;
4183 c_cont_label = NULL_TREE;
4184 body = c_parser_c99_block_statement (parser);
4185 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4186 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4187 c_break_label = save_break;
4188 c_cont_label = save_cont;
4191 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4192 statement with inputs, outputs, clobbers, and volatile tag
4193 allowed.
4195 asm-statement:
4196 asm type-qualifier[opt] ( asm-argument ) ;
4198 asm-argument:
4199 asm-string-literal
4200 asm-string-literal : asm-operands[opt]
4201 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4202 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4204 Qualifiers other than volatile are accepted in the syntax but
4205 warned for. */
4207 static tree
4208 c_parser_asm_statement (c_parser *parser)
4210 tree quals, str, outputs, inputs, clobbers, ret;
4211 bool simple;
4212 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4213 c_parser_consume_token (parser);
4214 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4216 quals = c_parser_peek_token (parser)->value;
4217 c_parser_consume_token (parser);
4219 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4220 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4222 warning_at (c_parser_peek_token (parser)->location,
4224 "%E qualifier ignored on asm",
4225 c_parser_peek_token (parser)->value);
4226 quals = NULL_TREE;
4227 c_parser_consume_token (parser);
4229 else
4230 quals = NULL_TREE;
4231 /* ??? Follow the C++ parser rather than using the
4232 lex_untranslated_string kludge. */
4233 parser->lex_untranslated_string = true;
4234 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4236 parser->lex_untranslated_string = false;
4237 return NULL_TREE;
4239 str = c_parser_asm_string_literal (parser);
4240 if (str == NULL_TREE)
4242 parser->lex_untranslated_string = false;
4243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4244 return NULL_TREE;
4246 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4248 simple = true;
4249 outputs = NULL_TREE;
4250 inputs = NULL_TREE;
4251 clobbers = NULL_TREE;
4252 goto done_asm;
4254 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4256 parser->lex_untranslated_string = false;
4257 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4258 return NULL_TREE;
4260 simple = false;
4261 /* Parse outputs. */
4262 if (c_parser_next_token_is (parser, CPP_COLON)
4263 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4264 outputs = NULL_TREE;
4265 else
4266 outputs = c_parser_asm_operands (parser, false);
4267 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4269 inputs = NULL_TREE;
4270 clobbers = NULL_TREE;
4271 goto done_asm;
4273 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4275 parser->lex_untranslated_string = false;
4276 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4277 return NULL_TREE;
4279 /* Parse inputs. */
4280 if (c_parser_next_token_is (parser, CPP_COLON)
4281 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4282 inputs = NULL_TREE;
4283 else
4284 inputs = c_parser_asm_operands (parser, true);
4285 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4287 clobbers = NULL_TREE;
4288 goto done_asm;
4290 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4292 parser->lex_untranslated_string = false;
4293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4294 return NULL_TREE;
4296 /* Parse clobbers. */
4297 clobbers = c_parser_asm_clobbers (parser);
4298 done_asm:
4299 parser->lex_untranslated_string = false;
4300 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4303 return NULL_TREE;
4305 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4306 c_parser_skip_to_end_of_block_or_statement (parser);
4307 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4308 clobbers, simple));
4309 return ret;
4312 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4313 not outputs), apply the default conversion of functions and arrays
4314 to pointers.
4316 asm-operands:
4317 asm-operand
4318 asm-operands , asm-operand
4320 asm-operand:
4321 asm-string-literal ( expression )
4322 [ identifier ] asm-string-literal ( expression )
4325 static tree
4326 c_parser_asm_operands (c_parser *parser, bool convert_p)
4328 tree list = NULL_TREE;
4329 while (true)
4331 tree name, str;
4332 struct c_expr expr;
4333 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4335 c_parser_consume_token (parser);
4336 if (c_parser_next_token_is (parser, CPP_NAME))
4338 tree id = c_parser_peek_token (parser)->value;
4339 c_parser_consume_token (parser);
4340 name = build_string (IDENTIFIER_LENGTH (id),
4341 IDENTIFIER_POINTER (id));
4343 else
4345 c_parser_error (parser, "expected identifier");
4346 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4347 return NULL_TREE;
4349 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4350 "expected %<]%>");
4352 else
4353 name = NULL_TREE;
4354 str = c_parser_asm_string_literal (parser);
4355 if (str == NULL_TREE)
4356 return NULL_TREE;
4357 parser->lex_untranslated_string = false;
4358 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4360 parser->lex_untranslated_string = true;
4361 return NULL_TREE;
4363 expr = c_parser_expression (parser);
4364 if (convert_p)
4365 expr = default_function_array_conversion (expr);
4366 expr.value = c_fully_fold (expr.value, false, NULL);
4367 parser->lex_untranslated_string = true;
4368 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4370 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4371 return NULL_TREE;
4373 list = chainon (list, build_tree_list (build_tree_list (name, str),
4374 expr.value));
4375 if (c_parser_next_token_is (parser, CPP_COMMA))
4376 c_parser_consume_token (parser);
4377 else
4378 break;
4380 return list;
4383 /* Parse asm clobbers, a GNU extension.
4385 asm-clobbers:
4386 asm-string-literal
4387 asm-clobbers , asm-string-literal
4390 static tree
4391 c_parser_asm_clobbers (c_parser *parser)
4393 tree list = NULL_TREE;
4394 while (true)
4396 tree str = c_parser_asm_string_literal (parser);
4397 if (str)
4398 list = tree_cons (NULL_TREE, str, list);
4399 else
4400 return NULL_TREE;
4401 if (c_parser_next_token_is (parser, CPP_COMMA))
4402 c_parser_consume_token (parser);
4403 else
4404 break;
4406 return list;
4409 /* Parse an expression other than a compound expression; that is, an
4410 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4411 NULL then it is an Objective-C message expression which is the
4412 primary-expression starting the expression as an initializer.
4414 assignment-expression:
4415 conditional-expression
4416 unary-expression assignment-operator assignment-expression
4418 assignment-operator: one of
4419 = *= /= %= += -= <<= >>= &= ^= |=
4421 In GNU C we accept any conditional expression on the LHS and
4422 diagnose the invalid lvalue rather than producing a syntax
4423 error. */
4425 static struct c_expr
4426 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4428 struct c_expr lhs, rhs, ret;
4429 enum tree_code code;
4430 location_t op_location;
4431 gcc_assert (!after || c_dialect_objc ());
4432 lhs = c_parser_conditional_expression (parser, after);
4433 op_location = c_parser_peek_token (parser)->location;
4434 switch (c_parser_peek_token (parser)->type)
4436 case CPP_EQ:
4437 code = NOP_EXPR;
4438 break;
4439 case CPP_MULT_EQ:
4440 code = MULT_EXPR;
4441 break;
4442 case CPP_DIV_EQ:
4443 code = TRUNC_DIV_EXPR;
4444 break;
4445 case CPP_MOD_EQ:
4446 code = TRUNC_MOD_EXPR;
4447 break;
4448 case CPP_PLUS_EQ:
4449 code = PLUS_EXPR;
4450 break;
4451 case CPP_MINUS_EQ:
4452 code = MINUS_EXPR;
4453 break;
4454 case CPP_LSHIFT_EQ:
4455 code = LSHIFT_EXPR;
4456 break;
4457 case CPP_RSHIFT_EQ:
4458 code = RSHIFT_EXPR;
4459 break;
4460 case CPP_AND_EQ:
4461 code = BIT_AND_EXPR;
4462 break;
4463 case CPP_XOR_EQ:
4464 code = BIT_XOR_EXPR;
4465 break;
4466 case CPP_OR_EQ:
4467 code = BIT_IOR_EXPR;
4468 break;
4469 default:
4470 return lhs;
4472 c_parser_consume_token (parser);
4473 rhs = c_parser_expr_no_commas (parser, NULL);
4474 rhs = default_function_array_conversion (rhs);
4475 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
4476 code, rhs.value, rhs.original_type);
4477 if (code == NOP_EXPR)
4478 ret.original_code = MODIFY_EXPR;
4479 else
4481 TREE_NO_WARNING (ret.value) = 1;
4482 ret.original_code = ERROR_MARK;
4484 ret.original_type = NULL;
4485 return ret;
4488 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4489 is not NULL then it is an Objective-C message expression which is
4490 the primary-expression starting the expression as an initializer.
4492 conditional-expression:
4493 logical-OR-expression
4494 logical-OR-expression ? expression : conditional-expression
4496 GNU extensions:
4498 conditional-expression:
4499 logical-OR-expression ? : conditional-expression
4502 static struct c_expr
4503 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4505 struct c_expr cond, exp1, exp2, ret;
4506 location_t cond_loc;
4508 gcc_assert (!after || c_dialect_objc ());
4510 cond_loc = c_parser_peek_token (parser)->location;
4511 cond = c_parser_binary_expression (parser, after);
4513 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4514 return cond;
4515 cond = default_function_array_conversion (cond);
4516 c_parser_consume_token (parser);
4517 if (c_parser_next_token_is (parser, CPP_COLON))
4519 tree eptype = NULL_TREE;
4520 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4521 "ISO C forbids omitting the middle term of a ?: expression");
4522 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4524 eptype = TREE_TYPE (cond.value);
4525 cond.value = TREE_OPERAND (cond.value, 0);
4527 /* Make sure first operand is calculated only once. */
4528 exp1.value = c_save_expr (default_conversion (cond.value));
4529 if (eptype)
4530 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
4531 exp1.original_type = NULL;
4532 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4533 skip_evaluation += cond.value == truthvalue_true_node;
4535 else
4537 cond.value
4538 = c_objc_common_truthvalue_conversion
4539 (cond_loc, default_conversion (cond.value));
4540 skip_evaluation += cond.value == truthvalue_false_node;
4541 exp1 = c_parser_expression_conv (parser);
4542 skip_evaluation += ((cond.value == truthvalue_true_node)
4543 - (cond.value == truthvalue_false_node));
4545 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4547 skip_evaluation -= cond.value == truthvalue_true_node;
4548 ret.value = error_mark_node;
4549 ret.original_code = ERROR_MARK;
4550 ret.original_type = NULL;
4551 return ret;
4553 exp2 = c_parser_conditional_expression (parser, NULL);
4554 exp2 = default_function_array_conversion (exp2);
4555 skip_evaluation -= cond.value == truthvalue_true_node;
4556 ret.value = build_conditional_expr (cond.value,
4557 cond.original_code == C_MAYBE_CONST_EXPR,
4558 exp1.value, exp2.value);
4559 ret.original_code = ERROR_MARK;
4560 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4561 ret.original_type = NULL;
4562 else
4564 tree t1, t2;
4566 /* If both sides are enum type, the default conversion will have
4567 made the type of the result be an integer type. We want to
4568 remember the enum types we started with. */
4569 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4570 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4571 ret.original_type = ((t1 != error_mark_node
4572 && t2 != error_mark_node
4573 && (TYPE_MAIN_VARIANT (t1)
4574 == TYPE_MAIN_VARIANT (t2)))
4575 ? t1
4576 : NULL);
4578 return ret;
4581 /* Parse a binary expression; that is, a logical-OR-expression (C90
4582 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4583 an Objective-C message expression which is the primary-expression
4584 starting the expression as an initializer.
4586 multiplicative-expression:
4587 cast-expression
4588 multiplicative-expression * cast-expression
4589 multiplicative-expression / cast-expression
4590 multiplicative-expression % cast-expression
4592 additive-expression:
4593 multiplicative-expression
4594 additive-expression + multiplicative-expression
4595 additive-expression - multiplicative-expression
4597 shift-expression:
4598 additive-expression
4599 shift-expression << additive-expression
4600 shift-expression >> additive-expression
4602 relational-expression:
4603 shift-expression
4604 relational-expression < shift-expression
4605 relational-expression > shift-expression
4606 relational-expression <= shift-expression
4607 relational-expression >= shift-expression
4609 equality-expression:
4610 relational-expression
4611 equality-expression == relational-expression
4612 equality-expression != relational-expression
4614 AND-expression:
4615 equality-expression
4616 AND-expression & equality-expression
4618 exclusive-OR-expression:
4619 AND-expression
4620 exclusive-OR-expression ^ AND-expression
4622 inclusive-OR-expression:
4623 exclusive-OR-expression
4624 inclusive-OR-expression | exclusive-OR-expression
4626 logical-AND-expression:
4627 inclusive-OR-expression
4628 logical-AND-expression && inclusive-OR-expression
4630 logical-OR-expression:
4631 logical-AND-expression
4632 logical-OR-expression || logical-AND-expression
4635 static struct c_expr
4636 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4638 /* A binary expression is parsed using operator-precedence parsing,
4639 with the operands being cast expressions. All the binary
4640 operators are left-associative. Thus a binary expression is of
4641 form:
4643 E0 op1 E1 op2 E2 ...
4645 which we represent on a stack. On the stack, the precedence
4646 levels are strictly increasing. When a new operator is
4647 encountered of higher precedence than that at the top of the
4648 stack, it is pushed; its LHS is the top expression, and its RHS
4649 is everything parsed until it is popped. When a new operator is
4650 encountered with precedence less than or equal to that at the top
4651 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4652 by the result of the operation until the operator at the top of
4653 the stack has lower precedence than the new operator or there is
4654 only one element on the stack; then the top expression is the LHS
4655 of the new operator. In the case of logical AND and OR
4656 expressions, we also need to adjust skip_evaluation as
4657 appropriate when the operators are pushed and popped. */
4659 /* The precedence levels, where 0 is a dummy lowest level used for
4660 the bottom of the stack. */
4661 enum prec {
4662 PREC_NONE,
4663 PREC_LOGOR,
4664 PREC_LOGAND,
4665 PREC_BITOR,
4666 PREC_BITXOR,
4667 PREC_BITAND,
4668 PREC_EQ,
4669 PREC_REL,
4670 PREC_SHIFT,
4671 PREC_ADD,
4672 PREC_MULT,
4673 NUM_PRECS
4675 struct {
4676 /* The expression at this stack level. */
4677 struct c_expr expr;
4678 /* The precedence of the operator on its left, PREC_NONE at the
4679 bottom of the stack. */
4680 enum prec prec;
4681 /* The operation on its left. */
4682 enum tree_code op;
4683 /* The source location of this operation. */
4684 location_t loc;
4685 } stack[NUM_PRECS];
4686 int sp;
4687 /* Location of the binary operator. */
4688 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4689 #define POP \
4690 do { \
4691 switch (stack[sp].op) \
4693 case TRUTH_ANDIF_EXPR: \
4694 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4695 break; \
4696 case TRUTH_ORIF_EXPR: \
4697 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4698 break; \
4699 default: \
4700 break; \
4702 stack[sp - 1].expr \
4703 = default_function_array_conversion (stack[sp - 1].expr); \
4704 stack[sp].expr \
4705 = default_function_array_conversion (stack[sp].expr); \
4706 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
4707 stack[sp].op, \
4708 stack[sp - 1].expr, \
4709 stack[sp].expr); \
4710 sp--; \
4711 } while (0)
4712 gcc_assert (!after || c_dialect_objc ());
4713 stack[0].loc = c_parser_peek_token (parser)->location;
4714 stack[0].expr = c_parser_cast_expression (parser, after);
4715 stack[0].prec = PREC_NONE;
4716 sp = 0;
4717 while (true)
4719 enum prec oprec;
4720 enum tree_code ocode;
4721 if (parser->error)
4722 goto out;
4723 switch (c_parser_peek_token (parser)->type)
4725 case CPP_MULT:
4726 oprec = PREC_MULT;
4727 ocode = MULT_EXPR;
4728 break;
4729 case CPP_DIV:
4730 oprec = PREC_MULT;
4731 ocode = TRUNC_DIV_EXPR;
4732 break;
4733 case CPP_MOD:
4734 oprec = PREC_MULT;
4735 ocode = TRUNC_MOD_EXPR;
4736 break;
4737 case CPP_PLUS:
4738 oprec = PREC_ADD;
4739 ocode = PLUS_EXPR;
4740 break;
4741 case CPP_MINUS:
4742 oprec = PREC_ADD;
4743 ocode = MINUS_EXPR;
4744 break;
4745 case CPP_LSHIFT:
4746 oprec = PREC_SHIFT;
4747 ocode = LSHIFT_EXPR;
4748 break;
4749 case CPP_RSHIFT:
4750 oprec = PREC_SHIFT;
4751 ocode = RSHIFT_EXPR;
4752 break;
4753 case CPP_LESS:
4754 oprec = PREC_REL;
4755 ocode = LT_EXPR;
4756 break;
4757 case CPP_GREATER:
4758 oprec = PREC_REL;
4759 ocode = GT_EXPR;
4760 break;
4761 case CPP_LESS_EQ:
4762 oprec = PREC_REL;
4763 ocode = LE_EXPR;
4764 break;
4765 case CPP_GREATER_EQ:
4766 oprec = PREC_REL;
4767 ocode = GE_EXPR;
4768 break;
4769 case CPP_EQ_EQ:
4770 oprec = PREC_EQ;
4771 ocode = EQ_EXPR;
4772 break;
4773 case CPP_NOT_EQ:
4774 oprec = PREC_EQ;
4775 ocode = NE_EXPR;
4776 break;
4777 case CPP_AND:
4778 oprec = PREC_BITAND;
4779 ocode = BIT_AND_EXPR;
4780 break;
4781 case CPP_XOR:
4782 oprec = PREC_BITXOR;
4783 ocode = BIT_XOR_EXPR;
4784 break;
4785 case CPP_OR:
4786 oprec = PREC_BITOR;
4787 ocode = BIT_IOR_EXPR;
4788 break;
4789 case CPP_AND_AND:
4790 oprec = PREC_LOGAND;
4791 ocode = TRUTH_ANDIF_EXPR;
4792 break;
4793 case CPP_OR_OR:
4794 oprec = PREC_LOGOR;
4795 ocode = TRUTH_ORIF_EXPR;
4796 break;
4797 default:
4798 /* Not a binary operator, so end of the binary
4799 expression. */
4800 goto out;
4802 binary_loc = c_parser_peek_token (parser)->location;
4803 c_parser_consume_token (parser);
4804 while (oprec <= stack[sp].prec)
4805 POP;
4806 switch (ocode)
4808 case TRUTH_ANDIF_EXPR:
4809 stack[sp].expr
4810 = default_function_array_conversion (stack[sp].expr);
4811 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4812 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4813 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4814 break;
4815 case TRUTH_ORIF_EXPR:
4816 stack[sp].expr
4817 = default_function_array_conversion (stack[sp].expr);
4818 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4819 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4820 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4821 break;
4822 default:
4823 break;
4825 sp++;
4826 stack[sp].loc = binary_loc;
4827 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4828 stack[sp].prec = oprec;
4829 stack[sp].op = ocode;
4831 out:
4832 while (sp > 0)
4833 POP;
4834 return stack[0].expr;
4835 #undef POP
4838 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4839 NULL then it is an Objective-C message expression which is the
4840 primary-expression starting the expression as an initializer.
4842 cast-expression:
4843 unary-expression
4844 ( type-name ) unary-expression
4847 static struct c_expr
4848 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4850 gcc_assert (!after || c_dialect_objc ());
4851 if (after)
4852 return c_parser_postfix_expression_after_primary (parser, *after);
4853 /* If the expression begins with a parenthesized type name, it may
4854 be either a cast or a compound literal; we need to see whether
4855 the next character is '{' to tell the difference. If not, it is
4856 an unary expression. */
4857 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4858 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4860 location_t loc;
4861 struct c_type_name *type_name;
4862 struct c_expr ret;
4863 struct c_expr expr;
4864 c_parser_consume_token (parser);
4865 loc = c_parser_peek_token (parser)->location;
4866 type_name = c_parser_type_name (parser);
4867 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4868 if (type_name == NULL)
4870 ret.value = error_mark_node;
4871 ret.original_code = ERROR_MARK;
4872 ret.original_type = NULL;
4873 return ret;
4876 /* Save casted types in the function's used types hash table. */
4877 used_types_insert (type_name->specs->type);
4879 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4880 return c_parser_postfix_expression_after_paren_type (parser, type_name,
4881 loc);
4882 expr = c_parser_cast_expression (parser, NULL);
4883 expr = default_function_array_conversion (expr);
4884 ret.value = c_cast_expr (type_name, expr.value, loc);
4885 ret.original_code = ERROR_MARK;
4886 ret.original_type = NULL;
4887 return ret;
4889 else
4890 return c_parser_unary_expression (parser);
4893 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4895 unary-expression:
4896 postfix-expression
4897 ++ unary-expression
4898 -- unary-expression
4899 unary-operator cast-expression
4900 sizeof unary-expression
4901 sizeof ( type-name )
4903 unary-operator: one of
4904 & * + - ~ !
4906 GNU extensions:
4908 unary-expression:
4909 __alignof__ unary-expression
4910 __alignof__ ( type-name )
4911 && identifier
4913 unary-operator: one of
4914 __extension__ __real__ __imag__
4916 In addition, the GNU syntax treats ++ and -- as unary operators, so
4917 they may be applied to cast expressions with errors for non-lvalues
4918 given later. */
4920 static struct c_expr
4921 c_parser_unary_expression (c_parser *parser)
4923 int ext;
4924 struct c_expr ret, op;
4925 location_t loc = c_parser_peek_token (parser)->location;
4926 ret.original_code = ERROR_MARK;
4927 ret.original_type = NULL;
4928 switch (c_parser_peek_token (parser)->type)
4930 case CPP_PLUS_PLUS:
4931 c_parser_consume_token (parser);
4932 op = c_parser_cast_expression (parser, NULL);
4933 op = default_function_array_conversion (op);
4934 return parser_build_unary_op (PREINCREMENT_EXPR, op, loc);
4935 case CPP_MINUS_MINUS:
4936 c_parser_consume_token (parser);
4937 op = c_parser_cast_expression (parser, NULL);
4938 op = default_function_array_conversion (op);
4939 return parser_build_unary_op (PREDECREMENT_EXPR, op, loc);
4940 case CPP_AND:
4941 c_parser_consume_token (parser);
4942 return parser_build_unary_op (ADDR_EXPR,
4943 c_parser_cast_expression (parser, NULL),
4944 loc);
4945 case CPP_MULT:
4946 c_parser_consume_token (parser);
4947 op = c_parser_cast_expression (parser, NULL);
4948 op = default_function_array_conversion (op);
4949 ret.value = build_indirect_ref (loc, op.value, "unary *");
4950 return ret;
4951 case CPP_PLUS:
4952 if (!c_dialect_objc () && !in_system_header)
4953 warning_at (c_parser_peek_token (parser)->location,
4954 OPT_Wtraditional,
4955 "traditional C rejects the unary plus operator");
4956 c_parser_consume_token (parser);
4957 op = c_parser_cast_expression (parser, NULL);
4958 op = default_function_array_conversion (op);
4959 return parser_build_unary_op (CONVERT_EXPR, op, loc);
4960 case CPP_MINUS:
4961 c_parser_consume_token (parser);
4962 op = c_parser_cast_expression (parser, NULL);
4963 op = default_function_array_conversion (op);
4964 return parser_build_unary_op (NEGATE_EXPR, op, loc);
4965 case CPP_COMPL:
4966 c_parser_consume_token (parser);
4967 op = c_parser_cast_expression (parser, NULL);
4968 op = default_function_array_conversion (op);
4969 return parser_build_unary_op (BIT_NOT_EXPR, op, loc);
4970 case CPP_NOT:
4971 c_parser_consume_token (parser);
4972 op = c_parser_cast_expression (parser, NULL);
4973 op = default_function_array_conversion (op);
4974 return parser_build_unary_op (TRUTH_NOT_EXPR, op, loc);
4975 case CPP_AND_AND:
4976 /* Refer to the address of a label as a pointer. */
4977 c_parser_consume_token (parser);
4978 if (c_parser_next_token_is (parser, CPP_NAME))
4980 ret.value = finish_label_address_expr
4981 (c_parser_peek_token (parser)->value, loc);
4982 c_parser_consume_token (parser);
4984 else
4986 c_parser_error (parser, "expected identifier");
4987 ret.value = error_mark_node;
4989 return ret;
4990 case CPP_KEYWORD:
4991 switch (c_parser_peek_token (parser)->keyword)
4993 case RID_SIZEOF:
4994 return c_parser_sizeof_expression (parser);
4995 case RID_ALIGNOF:
4996 return c_parser_alignof_expression (parser);
4997 case RID_EXTENSION:
4998 c_parser_consume_token (parser);
4999 ext = disable_extension_diagnostics ();
5000 ret = c_parser_cast_expression (parser, NULL);
5001 restore_extension_diagnostics (ext);
5002 return ret;
5003 case RID_REALPART:
5004 c_parser_consume_token (parser);
5005 op = c_parser_cast_expression (parser, NULL);
5006 op = default_function_array_conversion (op);
5007 return parser_build_unary_op (REALPART_EXPR, op, loc);
5008 case RID_IMAGPART:
5009 c_parser_consume_token (parser);
5010 op = c_parser_cast_expression (parser, NULL);
5011 op = default_function_array_conversion (op);
5012 return parser_build_unary_op (IMAGPART_EXPR, op, loc);
5013 default:
5014 return c_parser_postfix_expression (parser);
5016 default:
5017 return c_parser_postfix_expression (parser);
5021 /* Parse a sizeof expression. */
5023 static struct c_expr
5024 c_parser_sizeof_expression (c_parser *parser)
5026 struct c_expr expr;
5027 location_t expr_loc;
5028 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5029 c_parser_consume_token (parser);
5030 skip_evaluation++;
5031 in_sizeof++;
5032 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5033 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5035 /* Either sizeof ( type-name ) or sizeof unary-expression
5036 starting with a compound literal. */
5037 struct c_type_name *type_name;
5038 c_parser_consume_token (parser);
5039 expr_loc = c_parser_peek_token (parser)->location;
5040 type_name = c_parser_type_name (parser);
5041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5042 if (type_name == NULL)
5044 struct c_expr ret;
5045 skip_evaluation--;
5046 in_sizeof--;
5047 ret.value = error_mark_node;
5048 ret.original_code = ERROR_MARK;
5049 ret.original_type = NULL;
5050 return ret;
5052 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5054 expr = c_parser_postfix_expression_after_paren_type (parser,
5055 type_name,
5056 expr_loc);
5057 goto sizeof_expr;
5059 /* sizeof ( type-name ). */
5060 skip_evaluation--;
5061 in_sizeof--;
5062 return c_expr_sizeof_type (type_name);
5064 else
5066 expr_loc = c_parser_peek_token (parser)->location;
5067 expr = c_parser_unary_expression (parser);
5068 sizeof_expr:
5069 skip_evaluation--;
5070 in_sizeof--;
5071 if (TREE_CODE (expr.value) == COMPONENT_REF
5072 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5073 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5074 return c_expr_sizeof_expr (expr);
5078 /* Parse an alignof expression. */
5080 static struct c_expr
5081 c_parser_alignof_expression (c_parser *parser)
5083 struct c_expr expr;
5084 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5085 c_parser_consume_token (parser);
5086 skip_evaluation++;
5087 in_alignof++;
5088 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5089 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5091 /* Either __alignof__ ( type-name ) or __alignof__
5092 unary-expression starting with a compound literal. */
5093 location_t loc;
5094 struct c_type_name *type_name;
5095 struct c_expr ret;
5096 c_parser_consume_token (parser);
5097 loc = c_parser_peek_token (parser)->location;
5098 type_name = c_parser_type_name (parser);
5099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5100 if (type_name == NULL)
5102 struct c_expr ret;
5103 skip_evaluation--;
5104 in_alignof--;
5105 ret.value = error_mark_node;
5106 ret.original_code = ERROR_MARK;
5107 ret.original_type = NULL;
5108 return ret;
5110 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5112 expr = c_parser_postfix_expression_after_paren_type (parser,
5113 type_name,
5114 loc);
5115 goto alignof_expr;
5117 /* alignof ( type-name ). */
5118 skip_evaluation--;
5119 in_alignof--;
5120 ret.value = c_alignof (groktypename (type_name, NULL, NULL));
5121 ret.original_code = ERROR_MARK;
5122 ret.original_type = NULL;
5123 return ret;
5125 else
5127 struct c_expr ret;
5128 expr = c_parser_unary_expression (parser);
5129 alignof_expr:
5130 skip_evaluation--;
5131 in_alignof--;
5132 ret.value = c_alignof_expr (expr.value);
5133 ret.original_code = ERROR_MARK;
5134 ret.original_type = NULL;
5135 return ret;
5139 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5141 postfix-expression:
5142 primary-expression
5143 postfix-expression [ expression ]
5144 postfix-expression ( argument-expression-list[opt] )
5145 postfix-expression . identifier
5146 postfix-expression -> identifier
5147 postfix-expression ++
5148 postfix-expression --
5149 ( type-name ) { initializer-list }
5150 ( type-name ) { initializer-list , }
5152 argument-expression-list:
5153 argument-expression
5154 argument-expression-list , argument-expression
5156 primary-expression:
5157 identifier
5158 constant
5159 string-literal
5160 ( expression )
5162 GNU extensions:
5164 primary-expression:
5165 __func__
5166 (treated as a keyword in GNU C)
5167 __FUNCTION__
5168 __PRETTY_FUNCTION__
5169 ( compound-statement )
5170 __builtin_va_arg ( assignment-expression , type-name )
5171 __builtin_offsetof ( type-name , offsetof-member-designator )
5172 __builtin_choose_expr ( assignment-expression ,
5173 assignment-expression ,
5174 assignment-expression )
5175 __builtin_types_compatible_p ( type-name , type-name )
5177 offsetof-member-designator:
5178 identifier
5179 offsetof-member-designator . identifier
5180 offsetof-member-designator [ expression ]
5182 Objective-C:
5184 primary-expression:
5185 [ objc-receiver objc-message-args ]
5186 @selector ( objc-selector-arg )
5187 @protocol ( identifier )
5188 @encode ( type-name )
5189 objc-string-literal
5192 static struct c_expr
5193 c_parser_postfix_expression (c_parser *parser)
5195 struct c_expr expr, e1, e2, e3;
5196 struct c_type_name *t1, *t2;
5197 location_t loc;
5198 expr.original_code = ERROR_MARK;
5199 expr.original_type = NULL;
5200 switch (c_parser_peek_token (parser)->type)
5202 case CPP_NUMBER:
5203 expr.value = c_parser_peek_token (parser)->value;
5204 loc = c_parser_peek_token (parser)->location;
5205 c_parser_consume_token (parser);
5206 if (TREE_CODE (expr.value) == FIXED_CST
5207 && !targetm.fixed_point_supported_p ())
5209 error_at (loc, "fixed-point types not supported for this target");
5210 expr.value = error_mark_node;
5212 break;
5213 case CPP_CHAR:
5214 case CPP_CHAR16:
5215 case CPP_CHAR32:
5216 case CPP_WCHAR:
5217 expr.value = c_parser_peek_token (parser)->value;
5218 c_parser_consume_token (parser);
5219 break;
5220 case CPP_STRING:
5221 case CPP_STRING16:
5222 case CPP_STRING32:
5223 case CPP_WSTRING:
5224 expr.value = c_parser_peek_token (parser)->value;
5225 expr.original_code = STRING_CST;
5226 c_parser_consume_token (parser);
5227 break;
5228 case CPP_OBJC_STRING:
5229 gcc_assert (c_dialect_objc ());
5230 expr.value
5231 = objc_build_string_object (c_parser_peek_token (parser)->value);
5232 c_parser_consume_token (parser);
5233 break;
5234 case CPP_NAME:
5235 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5237 c_parser_error (parser, "expected expression");
5238 expr.value = error_mark_node;
5239 break;
5242 tree id = c_parser_peek_token (parser)->value;
5243 location_t loc = c_parser_peek_token (parser)->location;
5244 c_parser_consume_token (parser);
5245 expr.value = build_external_ref (id,
5246 (c_parser_peek_token (parser)->type
5247 == CPP_OPEN_PAREN), loc,
5248 &expr.original_type);
5250 break;
5251 case CPP_OPEN_PAREN:
5252 /* A parenthesized expression, statement expression or compound
5253 literal. */
5254 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5256 /* A statement expression. */
5257 tree stmt;
5258 location_t here = c_parser_peek_token (parser)->location;
5259 c_parser_consume_token (parser);
5260 c_parser_consume_token (parser);
5261 if (cur_stmt_list == NULL)
5263 error_at (here, "braced-group within expression allowed "
5264 "only inside a function");
5265 parser->error = true;
5266 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5267 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5268 expr.value = error_mark_node;
5269 break;
5271 stmt = c_begin_stmt_expr ();
5272 c_parser_compound_statement_nostart (parser);
5273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5274 "expected %<)%>");
5275 pedwarn (here, OPT_pedantic,
5276 "ISO C forbids braced-groups within expressions");
5277 expr.value = c_finish_stmt_expr (stmt);
5279 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5281 /* A compound literal. ??? Can we actually get here rather
5282 than going directly to
5283 c_parser_postfix_expression_after_paren_type from
5284 elsewhere? */
5285 location_t loc;
5286 struct c_type_name *type_name;
5287 c_parser_consume_token (parser);
5288 loc = c_parser_peek_token (parser)->location;
5289 type_name = c_parser_type_name (parser);
5290 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5291 "expected %<)%>");
5292 if (type_name == NULL)
5294 expr.value = error_mark_node;
5296 else
5297 expr = c_parser_postfix_expression_after_paren_type (parser,
5298 type_name,
5299 loc);
5301 else
5303 /* A parenthesized expression. */
5304 c_parser_consume_token (parser);
5305 expr = c_parser_expression (parser);
5306 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5307 TREE_NO_WARNING (expr.value) = 1;
5308 if (expr.original_code != C_MAYBE_CONST_EXPR)
5309 expr.original_code = ERROR_MARK;
5310 /* Don't change EXPR.ORIGINAL_TYPE. */
5311 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5312 "expected %<)%>");
5314 break;
5315 case CPP_KEYWORD:
5316 switch (c_parser_peek_token (parser)->keyword)
5318 case RID_FUNCTION_NAME:
5319 case RID_PRETTY_FUNCTION_NAME:
5320 case RID_C99_FUNCTION_NAME:
5321 expr.value = fname_decl (c_parser_peek_token (parser)->location,
5322 c_parser_peek_token (parser)->keyword,
5323 c_parser_peek_token (parser)->value);
5324 c_parser_consume_token (parser);
5325 break;
5326 case RID_VA_ARG:
5327 c_parser_consume_token (parser);
5328 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5330 expr.value = error_mark_node;
5331 break;
5333 e1 = c_parser_expr_no_commas (parser, NULL);
5334 e1.value = c_fully_fold (e1.value, false, NULL);
5335 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5338 expr.value = error_mark_node;
5339 break;
5341 loc = c_parser_peek_token (parser)->location;
5342 t1 = c_parser_type_name (parser);
5343 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5344 "expected %<)%>");
5345 if (t1 == NULL)
5347 expr.value = error_mark_node;
5349 else
5351 tree type_expr = NULL_TREE;
5352 expr.value = c_build_va_arg (e1.value,
5353 groktypename (t1, &type_expr, NULL),
5354 loc);
5355 if (type_expr)
5357 expr.value = build2 (C_MAYBE_CONST_EXPR,
5358 TREE_TYPE (expr.value), type_expr,
5359 expr.value);
5360 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5363 break;
5364 case RID_OFFSETOF:
5365 c_parser_consume_token (parser);
5366 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5368 expr.value = error_mark_node;
5369 break;
5371 t1 = c_parser_type_name (parser);
5372 if (t1 == NULL)
5374 expr.value = error_mark_node;
5375 break;
5377 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5379 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5380 expr.value = error_mark_node;
5381 break;
5384 tree type = groktypename (t1, NULL, NULL);
5385 tree offsetof_ref;
5386 if (type == error_mark_node)
5387 offsetof_ref = error_mark_node;
5388 else
5389 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5390 /* Parse the second argument to __builtin_offsetof. We
5391 must have one identifier, and beyond that we want to
5392 accept sub structure and sub array references. */
5393 if (c_parser_next_token_is (parser, CPP_NAME))
5395 offsetof_ref = build_component_ref
5396 (offsetof_ref, c_parser_peek_token (parser)->value);
5397 c_parser_consume_token (parser);
5398 while (c_parser_next_token_is (parser, CPP_DOT)
5399 || c_parser_next_token_is (parser,
5400 CPP_OPEN_SQUARE)
5401 || c_parser_next_token_is (parser,
5402 CPP_DEREF))
5404 if (c_parser_next_token_is (parser, CPP_DEREF))
5406 loc = c_parser_peek_token (parser)->location;
5407 offsetof_ref = build_array_ref (offsetof_ref,
5408 integer_zero_node,
5409 loc);
5410 goto do_dot;
5412 else if (c_parser_next_token_is (parser, CPP_DOT))
5414 do_dot:
5415 c_parser_consume_token (parser);
5416 if (c_parser_next_token_is_not (parser,
5417 CPP_NAME))
5419 c_parser_error (parser, "expected identifier");
5420 break;
5422 offsetof_ref = build_component_ref
5423 (offsetof_ref,
5424 c_parser_peek_token (parser)->value);
5425 c_parser_consume_token (parser);
5427 else
5429 tree idx;
5430 loc = c_parser_peek_token (parser)->location;
5431 c_parser_consume_token (parser);
5432 idx = c_parser_expression (parser).value;
5433 idx = c_fully_fold (idx, false, NULL);
5434 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5435 "expected %<]%>");
5436 offsetof_ref = build_array_ref (offsetof_ref, idx, loc);
5440 else
5441 c_parser_error (parser, "expected identifier");
5442 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5443 "expected %<)%>");
5444 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5446 break;
5447 case RID_CHOOSE_EXPR:
5448 c_parser_consume_token (parser);
5449 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5451 expr.value = error_mark_node;
5452 break;
5454 loc = c_parser_peek_token (parser)->location;
5455 e1 = c_parser_expr_no_commas (parser, NULL);
5456 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5458 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5459 expr.value = error_mark_node;
5460 break;
5462 e2 = c_parser_expr_no_commas (parser, NULL);
5463 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5466 expr.value = error_mark_node;
5467 break;
5469 e3 = c_parser_expr_no_commas (parser, NULL);
5470 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5471 "expected %<)%>");
5473 tree c;
5475 c = e1.value;
5476 if (TREE_CODE (c) != INTEGER_CST
5477 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
5478 error_at (loc,
5479 "first argument to %<__builtin_choose_expr%> not"
5480 " a constant");
5481 constant_expression_warning (c);
5482 expr = integer_zerop (c) ? e3 : e2;
5484 break;
5485 case RID_TYPES_COMPATIBLE_P:
5486 c_parser_consume_token (parser);
5487 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5489 expr.value = error_mark_node;
5490 break;
5492 t1 = c_parser_type_name (parser);
5493 if (t1 == NULL)
5495 expr.value = error_mark_node;
5496 break;
5498 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5500 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5501 expr.value = error_mark_node;
5502 break;
5504 t2 = c_parser_type_name (parser);
5505 if (t2 == NULL)
5507 expr.value = error_mark_node;
5508 break;
5510 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5511 "expected %<)%>");
5513 tree e1, e2;
5515 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5516 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
5518 expr.value = comptypes (e1, e2)
5519 ? build_int_cst (NULL_TREE, 1)
5520 : build_int_cst (NULL_TREE, 0);
5522 break;
5523 case RID_AT_SELECTOR:
5524 gcc_assert (c_dialect_objc ());
5525 c_parser_consume_token (parser);
5526 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5528 expr.value = error_mark_node;
5529 break;
5532 tree sel = c_parser_objc_selector_arg (parser);
5533 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5534 "expected %<)%>");
5535 expr.value = objc_build_selector_expr (sel);
5537 break;
5538 case RID_AT_PROTOCOL:
5539 gcc_assert (c_dialect_objc ());
5540 c_parser_consume_token (parser);
5541 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5543 expr.value = error_mark_node;
5544 break;
5546 if (c_parser_next_token_is_not (parser, CPP_NAME))
5548 c_parser_error (parser, "expected identifier");
5549 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5550 expr.value = error_mark_node;
5551 break;
5554 tree id = c_parser_peek_token (parser)->value;
5555 c_parser_consume_token (parser);
5556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5557 "expected %<)%>");
5558 expr.value = objc_build_protocol_expr (id);
5560 break;
5561 case RID_AT_ENCODE:
5562 /* Extension to support C-structures in the archiver. */
5563 gcc_assert (c_dialect_objc ());
5564 c_parser_consume_token (parser);
5565 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5567 expr.value = error_mark_node;
5568 break;
5570 t1 = c_parser_type_name (parser);
5571 if (t1 == NULL)
5573 expr.value = error_mark_node;
5574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5575 break;
5577 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5578 "expected %<)%>");
5580 tree type = groktypename (t1, NULL, NULL);
5581 expr.value = objc_build_encode_expr (type);
5583 break;
5584 default:
5585 c_parser_error (parser, "expected expression");
5586 expr.value = error_mark_node;
5587 break;
5589 break;
5590 case CPP_OPEN_SQUARE:
5591 if (c_dialect_objc ())
5593 tree receiver, args;
5594 c_parser_consume_token (parser);
5595 receiver = c_parser_objc_receiver (parser);
5596 args = c_parser_objc_message_args (parser);
5597 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5598 "expected %<]%>");
5599 expr.value = objc_build_message_expr (build_tree_list (receiver,
5600 args));
5601 break;
5603 /* Else fall through to report error. */
5604 default:
5605 c_parser_error (parser, "expected expression");
5606 expr.value = error_mark_node;
5607 break;
5609 return c_parser_postfix_expression_after_primary (parser, expr);
5612 /* Parse a postfix expression after a parenthesized type name: the
5613 brace-enclosed initializer of a compound literal, possibly followed
5614 by some postfix operators. This is separate because it is not
5615 possible to tell until after the type name whether a cast
5616 expression has a cast or a compound literal, or whether the operand
5617 of sizeof is a parenthesized type name or starts with a compound
5618 literal. TYPE_LOC is the location where TYPE_NAME starts--the
5619 location of the first token after the parentheses around the type
5620 name. */
5622 static struct c_expr
5623 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5624 struct c_type_name *type_name,
5625 location_t type_loc)
5627 tree type;
5628 struct c_expr init;
5629 bool non_const;
5630 struct c_expr expr;
5631 location_t start_loc;
5632 tree type_expr = NULL_TREE;
5633 bool type_expr_const = true;
5634 check_compound_literal_type (type_name, type_loc);
5635 start_init (NULL_TREE, NULL, 0);
5636 type = groktypename (type_name, &type_expr, &type_expr_const);
5637 start_loc = c_parser_peek_token (parser)->location;
5638 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5640 error_at (type_loc, "compound literal has variable size");
5641 type = error_mark_node;
5643 init = c_parser_braced_init (parser, type, false);
5644 finish_init ();
5645 maybe_warn_string_init (type, init);
5647 if (!flag_isoc99)
5648 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
5649 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5650 ? CONSTRUCTOR_NON_CONST (init.value)
5651 : init.original_code == C_MAYBE_CONST_EXPR);
5652 non_const |= !type_expr_const;
5653 expr.value = build_compound_literal (type, init.value, non_const);
5654 expr.original_code = ERROR_MARK;
5655 expr.original_type = NULL;
5656 if (type_expr)
5658 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5660 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
5661 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
5663 else
5665 gcc_assert (!non_const);
5666 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
5667 type_expr, expr.value);
5670 return c_parser_postfix_expression_after_primary (parser, expr);
5673 /* Parse a postfix expression after the initial primary or compound
5674 literal; that is, parse a series of postfix operators. */
5676 static struct c_expr
5677 c_parser_postfix_expression_after_primary (c_parser *parser,
5678 struct c_expr expr)
5680 struct c_expr orig_expr;
5681 tree ident, idx;
5682 VEC(tree,gc) *exprlist;
5683 VEC(tree,gc) *origtypes;
5684 location_t loc = c_parser_peek_token (parser)->location;
5685 while (true)
5687 switch (c_parser_peek_token (parser)->type)
5689 case CPP_OPEN_SQUARE:
5690 /* Array reference. */
5691 loc = c_parser_peek_token (parser)->location;
5692 c_parser_consume_token (parser);
5693 idx = c_parser_expression (parser).value;
5694 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5695 "expected %<]%>");
5696 expr.value = build_array_ref (expr.value, idx, loc);
5697 expr.original_code = ERROR_MARK;
5698 expr.original_type = NULL;
5699 break;
5700 case CPP_OPEN_PAREN:
5701 /* Function call. */
5702 c_parser_consume_token (parser);
5703 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5704 exprlist = NULL;
5705 else
5706 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
5707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5708 "expected %<)%>");
5709 orig_expr = expr;
5710 expr.value = build_function_call_vec (expr.value, exprlist,
5711 origtypes);
5712 expr.original_code = ERROR_MARK;
5713 if (TREE_CODE (expr.value) == INTEGER_CST
5714 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
5715 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
5716 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
5717 expr.original_code = C_MAYBE_CONST_EXPR;
5718 expr.original_type = NULL;
5719 if (exprlist != NULL)
5721 release_tree_vector (exprlist);
5722 release_tree_vector (origtypes);
5724 break;
5725 case CPP_DOT:
5726 /* Structure element reference. */
5727 c_parser_consume_token (parser);
5728 expr = default_function_array_conversion (expr);
5729 if (c_parser_next_token_is (parser, CPP_NAME))
5730 ident = c_parser_peek_token (parser)->value;
5731 else
5733 c_parser_error (parser, "expected identifier");
5734 expr.value = error_mark_node;
5735 expr.original_code = ERROR_MARK;
5736 expr.original_type = NULL;
5737 return expr;
5739 c_parser_consume_token (parser);
5740 expr.value = build_component_ref (expr.value, ident);
5741 expr.original_code = ERROR_MARK;
5742 if (TREE_CODE (expr.value) != COMPONENT_REF)
5743 expr.original_type = NULL;
5744 else
5746 /* Remember the original type of a bitfield. */
5747 tree field = TREE_OPERAND (expr.value, 1);
5748 if (TREE_CODE (field) != FIELD_DECL)
5749 expr.original_type = NULL;
5750 else
5751 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5753 break;
5754 case CPP_DEREF:
5755 /* Structure element reference. */
5756 c_parser_consume_token (parser);
5757 expr = default_function_array_conversion (expr);
5758 if (c_parser_next_token_is (parser, CPP_NAME))
5759 ident = c_parser_peek_token (parser)->value;
5760 else
5762 c_parser_error (parser, "expected identifier");
5763 expr.value = error_mark_node;
5764 expr.original_code = ERROR_MARK;
5765 expr.original_type = NULL;
5766 return expr;
5768 c_parser_consume_token (parser);
5769 expr.value = build_component_ref (build_indirect_ref (loc,
5770 expr.value,
5771 "->"),
5772 ident);
5773 expr.original_code = ERROR_MARK;
5774 if (TREE_CODE (expr.value) != COMPONENT_REF)
5775 expr.original_type = NULL;
5776 else
5778 /* Remember the original type of a bitfield. */
5779 tree field = TREE_OPERAND (expr.value, 1);
5780 if (TREE_CODE (field) != FIELD_DECL)
5781 expr.original_type = NULL;
5782 else
5783 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5785 break;
5786 case CPP_PLUS_PLUS:
5787 /* Postincrement. */
5788 c_parser_consume_token (parser);
5789 expr = default_function_array_conversion (expr);
5790 expr.value = build_unary_op (loc,
5791 POSTINCREMENT_EXPR, expr.value, 0);
5792 expr.original_code = ERROR_MARK;
5793 expr.original_type = NULL;
5794 break;
5795 case CPP_MINUS_MINUS:
5796 /* Postdecrement. */
5797 c_parser_consume_token (parser);
5798 expr = default_function_array_conversion (expr);
5799 expr.value = build_unary_op (loc,
5800 POSTDECREMENT_EXPR, expr.value, 0);
5801 expr.original_code = ERROR_MARK;
5802 expr.original_type = NULL;
5803 break;
5804 default:
5805 return expr;
5810 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5812 expression:
5813 assignment-expression
5814 expression , assignment-expression
5817 static struct c_expr
5818 c_parser_expression (c_parser *parser)
5820 struct c_expr expr;
5821 expr = c_parser_expr_no_commas (parser, NULL);
5822 while (c_parser_next_token_is (parser, CPP_COMMA))
5824 struct c_expr next;
5825 c_parser_consume_token (parser);
5826 next = c_parser_expr_no_commas (parser, NULL);
5827 next = default_function_array_conversion (next);
5828 expr.value = build_compound_expr (expr.value, next.value);
5829 expr.original_code = COMPOUND_EXPR;
5830 expr.original_type = next.original_type;
5832 return expr;
5835 /* Parse an expression and convert functions or arrays to
5836 pointers. */
5838 static struct c_expr
5839 c_parser_expression_conv (c_parser *parser)
5841 struct c_expr expr;
5842 expr = c_parser_expression (parser);
5843 expr = default_function_array_conversion (expr);
5844 return expr;
5847 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5848 functions and arrays to pointers. If FOLD_P, fold the expressions.
5850 nonempty-expr-list:
5851 assignment-expression
5852 nonempty-expr-list , assignment-expression
5855 static VEC(tree,gc) *
5856 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
5857 VEC(tree,gc) **p_orig_types)
5859 VEC(tree,gc) *ret;
5860 VEC(tree,gc) *orig_types;
5861 struct c_expr expr;
5863 ret = make_tree_vector ();
5864 if (p_orig_types == NULL)
5865 orig_types = NULL;
5866 else
5867 orig_types = make_tree_vector ();
5869 expr = c_parser_expr_no_commas (parser, NULL);
5870 if (convert_p)
5871 expr = default_function_array_conversion (expr);
5872 if (fold_p)
5873 expr.value = c_fully_fold (expr.value, false, NULL);
5874 VEC_quick_push (tree, ret, expr.value);
5875 if (orig_types != NULL)
5876 VEC_quick_push (tree, orig_types, expr.original_type);
5877 while (c_parser_next_token_is (parser, CPP_COMMA))
5879 c_parser_consume_token (parser);
5880 expr = c_parser_expr_no_commas (parser, NULL);
5881 if (convert_p)
5882 expr = default_function_array_conversion (expr);
5883 if (fold_p)
5884 expr.value = c_fully_fold (expr.value, false, NULL);
5885 VEC_safe_push (tree, gc, ret, expr.value);
5886 if (orig_types != NULL)
5887 VEC_safe_push (tree, gc, orig_types, expr.original_type);
5889 if (orig_types != NULL)
5890 *p_orig_types = orig_types;
5891 return ret;
5894 /* Parse Objective-C-specific constructs. */
5896 /* Parse an objc-class-definition.
5898 objc-class-definition:
5899 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5900 objc-class-instance-variables[opt] objc-methodprotolist @end
5901 @implementation identifier objc-superclass[opt]
5902 objc-class-instance-variables[opt]
5903 @interface identifier ( identifier ) objc-protocol-refs[opt]
5904 objc-methodprotolist @end
5905 @implementation identifier ( identifier )
5907 objc-superclass:
5908 : identifier
5910 "@interface identifier (" must start "@interface identifier (
5911 identifier ) ...": objc-methodprotolist in the first production may
5912 not start with a parenthesized identifier as a declarator of a data
5913 definition with no declaration specifiers if the objc-superclass,
5914 objc-protocol-refs and objc-class-instance-variables are omitted. */
5916 static void
5917 c_parser_objc_class_definition (c_parser *parser)
5919 bool iface_p;
5920 tree id1;
5921 tree superclass;
5922 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5923 iface_p = true;
5924 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5925 iface_p = false;
5926 else
5927 gcc_unreachable ();
5928 c_parser_consume_token (parser);
5929 if (c_parser_next_token_is_not (parser, CPP_NAME))
5931 c_parser_error (parser, "expected identifier");
5932 return;
5934 id1 = c_parser_peek_token (parser)->value;
5935 c_parser_consume_token (parser);
5936 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5938 tree id2;
5939 tree proto = NULL_TREE;
5940 c_parser_consume_token (parser);
5941 if (c_parser_next_token_is_not (parser, CPP_NAME))
5943 c_parser_error (parser, "expected identifier");
5944 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5945 return;
5947 id2 = c_parser_peek_token (parser)->value;
5948 c_parser_consume_token (parser);
5949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5950 if (!iface_p)
5952 objc_start_category_implementation (id1, id2);
5953 return;
5955 if (c_parser_next_token_is (parser, CPP_LESS))
5956 proto = c_parser_objc_protocol_refs (parser);
5957 objc_start_category_interface (id1, id2, proto);
5958 c_parser_objc_methodprotolist (parser);
5959 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5960 objc_finish_interface ();
5961 return;
5963 if (c_parser_next_token_is (parser, CPP_COLON))
5965 c_parser_consume_token (parser);
5966 if (c_parser_next_token_is_not (parser, CPP_NAME))
5968 c_parser_error (parser, "expected identifier");
5969 return;
5971 superclass = c_parser_peek_token (parser)->value;
5972 c_parser_consume_token (parser);
5974 else
5975 superclass = NULL_TREE;
5976 if (iface_p)
5978 tree proto = NULL_TREE;
5979 if (c_parser_next_token_is (parser, CPP_LESS))
5980 proto = c_parser_objc_protocol_refs (parser);
5981 objc_start_class_interface (id1, superclass, proto);
5983 else
5984 objc_start_class_implementation (id1, superclass);
5985 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5986 c_parser_objc_class_instance_variables (parser);
5987 if (iface_p)
5989 objc_continue_interface ();
5990 c_parser_objc_methodprotolist (parser);
5991 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5992 objc_finish_interface ();
5994 else
5996 objc_continue_implementation ();
5997 return;
6001 /* Parse objc-class-instance-variables.
6003 objc-class-instance-variables:
6004 { objc-instance-variable-decl-list[opt] }
6006 objc-instance-variable-decl-list:
6007 objc-visibility-spec
6008 objc-instance-variable-decl ;
6010 objc-instance-variable-decl-list objc-visibility-spec
6011 objc-instance-variable-decl-list objc-instance-variable-decl ;
6012 objc-instance-variable-decl-list ;
6014 objc-visibility-spec:
6015 @private
6016 @protected
6017 @public
6019 objc-instance-variable-decl:
6020 struct-declaration
6023 static void
6024 c_parser_objc_class_instance_variables (c_parser *parser)
6026 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6027 c_parser_consume_token (parser);
6028 while (c_parser_next_token_is_not (parser, CPP_EOF))
6030 tree decls;
6031 /* Parse any stray semicolon. */
6032 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6034 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6035 "extra semicolon in struct or union specified");
6036 c_parser_consume_token (parser);
6037 continue;
6039 /* Stop if at the end of the instance variables. */
6040 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6042 c_parser_consume_token (parser);
6043 break;
6045 /* Parse any objc-visibility-spec. */
6046 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
6048 c_parser_consume_token (parser);
6049 objc_set_visibility (2);
6050 continue;
6052 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
6054 c_parser_consume_token (parser);
6055 objc_set_visibility (0);
6056 continue;
6058 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
6060 c_parser_consume_token (parser);
6061 objc_set_visibility (1);
6062 continue;
6064 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6066 c_parser_pragma (parser, pragma_external);
6067 continue;
6070 /* Parse some comma-separated declarations. */
6071 decls = c_parser_struct_declaration (parser);
6073 /* Comma-separated instance variables are chained together in
6074 reverse order; add them one by one. */
6075 tree ivar = nreverse (decls);
6076 for (; ivar; ivar = TREE_CHAIN (ivar))
6077 objc_add_instance_variable (copy_node (ivar));
6079 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6083 /* Parse an objc-class-declaration.
6085 objc-class-declaration:
6086 @class identifier-list ;
6089 static void
6090 c_parser_objc_class_declaration (c_parser *parser)
6092 tree list = NULL_TREE;
6093 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
6094 c_parser_consume_token (parser);
6095 /* Any identifiers, including those declared as type names, are OK
6096 here. */
6097 while (true)
6099 tree id;
6100 if (c_parser_next_token_is_not (parser, CPP_NAME))
6102 c_parser_error (parser, "expected identifier");
6103 break;
6105 id = c_parser_peek_token (parser)->value;
6106 list = chainon (list, build_tree_list (NULL_TREE, id));
6107 c_parser_consume_token (parser);
6108 if (c_parser_next_token_is (parser, CPP_COMMA))
6109 c_parser_consume_token (parser);
6110 else
6111 break;
6113 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6114 objc_declare_class (list);
6117 /* Parse an objc-alias-declaration.
6119 objc-alias-declaration:
6120 @compatibility_alias identifier identifier ;
6123 static void
6124 c_parser_objc_alias_declaration (c_parser *parser)
6126 tree id1, id2;
6127 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6128 c_parser_consume_token (parser);
6129 if (c_parser_next_token_is_not (parser, CPP_NAME))
6131 c_parser_error (parser, "expected identifier");
6132 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6133 return;
6135 id1 = c_parser_peek_token (parser)->value;
6136 c_parser_consume_token (parser);
6137 if (c_parser_next_token_is_not (parser, CPP_NAME))
6139 c_parser_error (parser, "expected identifier");
6140 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6141 return;
6143 id2 = c_parser_peek_token (parser)->value;
6144 c_parser_consume_token (parser);
6145 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6146 objc_declare_alias (id1, id2);
6149 /* Parse an objc-protocol-definition.
6151 objc-protocol-definition:
6152 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6153 @protocol identifier-list ;
6155 "@protocol identifier ;" should be resolved as "@protocol
6156 identifier-list ;": objc-methodprotolist may not start with a
6157 semicolon in the first alternative if objc-protocol-refs are
6158 omitted. */
6160 static void
6161 c_parser_objc_protocol_definition (c_parser *parser)
6163 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6164 c_parser_consume_token (parser);
6165 if (c_parser_next_token_is_not (parser, CPP_NAME))
6167 c_parser_error (parser, "expected identifier");
6168 return;
6170 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6171 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6173 tree list = NULL_TREE;
6174 /* Any identifiers, including those declared as type names, are
6175 OK here. */
6176 while (true)
6178 tree id;
6179 if (c_parser_next_token_is_not (parser, CPP_NAME))
6181 c_parser_error (parser, "expected identifier");
6182 break;
6184 id = c_parser_peek_token (parser)->value;
6185 list = chainon (list, build_tree_list (NULL_TREE, id));
6186 c_parser_consume_token (parser);
6187 if (c_parser_next_token_is (parser, CPP_COMMA))
6188 c_parser_consume_token (parser);
6189 else
6190 break;
6192 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6193 objc_declare_protocols (list);
6195 else
6197 tree id = c_parser_peek_token (parser)->value;
6198 tree proto = NULL_TREE;
6199 c_parser_consume_token (parser);
6200 if (c_parser_next_token_is (parser, CPP_LESS))
6201 proto = c_parser_objc_protocol_refs (parser);
6202 parser->objc_pq_context = true;
6203 objc_start_protocol (id, proto);
6204 c_parser_objc_methodprotolist (parser);
6205 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6206 parser->objc_pq_context = false;
6207 objc_finish_interface ();
6211 /* Parse an objc-method-type.
6213 objc-method-type:
6218 static enum tree_code
6219 c_parser_objc_method_type (c_parser *parser)
6221 switch (c_parser_peek_token (parser)->type)
6223 case CPP_PLUS:
6224 c_parser_consume_token (parser);
6225 return PLUS_EXPR;
6226 case CPP_MINUS:
6227 c_parser_consume_token (parser);
6228 return MINUS_EXPR;
6229 default:
6230 gcc_unreachable ();
6234 /* Parse an objc-method-definition.
6236 objc-method-definition:
6237 objc-method-type objc-method-decl ;[opt] compound-statement
6240 static void
6241 c_parser_objc_method_definition (c_parser *parser)
6243 enum tree_code type = c_parser_objc_method_type (parser);
6244 tree decl;
6245 objc_set_method_type (type);
6246 parser->objc_pq_context = true;
6247 decl = c_parser_objc_method_decl (parser);
6248 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6250 c_parser_consume_token (parser);
6251 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6252 "extra semicolon in method definition specified");
6254 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6256 c_parser_error (parser, "expected %<{%>");
6257 return;
6259 parser->objc_pq_context = false;
6260 objc_start_method_definition (decl);
6261 add_stmt (c_parser_compound_statement (parser));
6262 objc_finish_method_definition (current_function_decl);
6265 /* Parse an objc-methodprotolist.
6267 objc-methodprotolist:
6268 empty
6269 objc-methodprotolist objc-methodproto
6270 objc-methodprotolist declaration
6271 objc-methodprotolist ;
6273 The declaration is a data definition, which may be missing
6274 declaration specifiers under the same rules and diagnostics as
6275 other data definitions outside functions, and the stray semicolon
6276 is diagnosed the same way as a stray semicolon outside a
6277 function. */
6279 static void
6280 c_parser_objc_methodprotolist (c_parser *parser)
6282 while (true)
6284 /* The list is terminated by @end. */
6285 switch (c_parser_peek_token (parser)->type)
6287 case CPP_SEMICOLON:
6288 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6289 "ISO C does not allow extra %<;%> outside of a function");
6290 c_parser_consume_token (parser);
6291 break;
6292 case CPP_PLUS:
6293 case CPP_MINUS:
6294 c_parser_objc_methodproto (parser);
6295 break;
6296 case CPP_PRAGMA:
6297 c_parser_pragma (parser, pragma_external);
6298 break;
6299 case CPP_EOF:
6300 return;
6301 default:
6302 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6303 return;
6304 c_parser_declaration_or_fndef (parser, false, true, false, true);
6305 break;
6310 /* Parse an objc-methodproto.
6312 objc-methodproto:
6313 objc-method-type objc-method-decl ;
6316 static void
6317 c_parser_objc_methodproto (c_parser *parser)
6319 enum tree_code type = c_parser_objc_method_type (parser);
6320 tree decl;
6321 objc_set_method_type (type);
6322 /* Remember protocol qualifiers in prototypes. */
6323 parser->objc_pq_context = true;
6324 decl = c_parser_objc_method_decl (parser);
6325 /* Forget protocol qualifiers here. */
6326 parser->objc_pq_context = false;
6327 objc_add_method_declaration (decl);
6328 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6331 /* Parse an objc-method-decl.
6333 objc-method-decl:
6334 ( objc-type-name ) objc-selector
6335 objc-selector
6336 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6337 objc-keyword-selector objc-optparmlist
6339 objc-keyword-selector:
6340 objc-keyword-decl
6341 objc-keyword-selector objc-keyword-decl
6343 objc-keyword-decl:
6344 objc-selector : ( objc-type-name ) identifier
6345 objc-selector : identifier
6346 : ( objc-type-name ) identifier
6347 : identifier
6349 objc-optparmlist:
6350 objc-optparms objc-optellipsis
6352 objc-optparms:
6353 empty
6354 objc-opt-parms , parameter-declaration
6356 objc-optellipsis:
6357 empty
6358 , ...
6361 static tree
6362 c_parser_objc_method_decl (c_parser *parser)
6364 tree type = NULL_TREE;
6365 tree sel;
6366 tree parms = NULL_TREE;
6367 bool ellipsis = false;
6369 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6371 c_parser_consume_token (parser);
6372 type = c_parser_objc_type_name (parser);
6373 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6375 sel = c_parser_objc_selector (parser);
6376 /* If there is no selector, or a colon follows, we have an
6377 objc-keyword-selector. If there is a selector, and a colon does
6378 not follow, that selector ends the objc-method-decl. */
6379 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6381 tree tsel = sel;
6382 tree list = NULL_TREE;
6383 while (true)
6385 tree atype = NULL_TREE, id, keyworddecl;
6386 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6387 break;
6388 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6390 c_parser_consume_token (parser);
6391 atype = c_parser_objc_type_name (parser);
6392 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6393 "expected %<)%>");
6395 if (c_parser_next_token_is_not (parser, CPP_NAME))
6397 c_parser_error (parser, "expected identifier");
6398 return error_mark_node;
6400 id = c_parser_peek_token (parser)->value;
6401 c_parser_consume_token (parser);
6402 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6403 list = chainon (list, keyworddecl);
6404 tsel = c_parser_objc_selector (parser);
6405 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6406 break;
6408 /* Parse the optional parameter list. Optional Objective-C
6409 method parameters follow the C syntax, and may include '...'
6410 to denote a variable number of arguments. */
6411 parms = make_node (TREE_LIST);
6412 while (c_parser_next_token_is (parser, CPP_COMMA))
6414 struct c_parm *parm;
6415 c_parser_consume_token (parser);
6416 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6418 ellipsis = true;
6419 c_parser_consume_token (parser);
6420 break;
6422 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6423 if (parm == NULL)
6424 break;
6425 parms = chainon (parms,
6426 build_tree_list (NULL_TREE, grokparm (parm)));
6428 sel = list;
6430 return objc_build_method_signature (type, sel, parms, ellipsis);
6433 /* Parse an objc-type-name.
6435 objc-type-name:
6436 objc-type-qualifiers[opt] type-name
6437 objc-type-qualifiers[opt]
6439 objc-type-qualifiers:
6440 objc-type-qualifier
6441 objc-type-qualifiers objc-type-qualifier
6443 objc-type-qualifier: one of
6444 in out inout bycopy byref oneway
6447 static tree
6448 c_parser_objc_type_name (c_parser *parser)
6450 tree quals = NULL_TREE;
6451 struct c_type_name *type_name = NULL;
6452 tree type = NULL_TREE;
6453 while (true)
6455 c_token *token = c_parser_peek_token (parser);
6456 if (token->type == CPP_KEYWORD
6457 && (token->keyword == RID_IN
6458 || token->keyword == RID_OUT
6459 || token->keyword == RID_INOUT
6460 || token->keyword == RID_BYCOPY
6461 || token->keyword == RID_BYREF
6462 || token->keyword == RID_ONEWAY))
6464 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6465 c_parser_consume_token (parser);
6467 else
6468 break;
6470 if (c_parser_next_token_starts_typename (parser))
6471 type_name = c_parser_type_name (parser);
6472 if (type_name)
6473 type = groktypename (type_name, NULL, NULL);
6474 return build_tree_list (quals, type);
6477 /* Parse objc-protocol-refs.
6479 objc-protocol-refs:
6480 < identifier-list >
6483 static tree
6484 c_parser_objc_protocol_refs (c_parser *parser)
6486 tree list = NULL_TREE;
6487 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6488 c_parser_consume_token (parser);
6489 /* Any identifiers, including those declared as type names, are OK
6490 here. */
6491 while (true)
6493 tree id;
6494 if (c_parser_next_token_is_not (parser, CPP_NAME))
6496 c_parser_error (parser, "expected identifier");
6497 break;
6499 id = c_parser_peek_token (parser)->value;
6500 list = chainon (list, build_tree_list (NULL_TREE, id));
6501 c_parser_consume_token (parser);
6502 if (c_parser_next_token_is (parser, CPP_COMMA))
6503 c_parser_consume_token (parser);
6504 else
6505 break;
6507 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6508 return list;
6511 /* Parse an objc-try-catch-statement.
6513 objc-try-catch-statement:
6514 @try compound-statement objc-catch-list[opt]
6515 @try compound-statement objc-catch-list[opt] @finally compound-statement
6517 objc-catch-list:
6518 @catch ( parameter-declaration ) compound-statement
6519 objc-catch-list @catch ( parameter-declaration ) compound-statement
6522 static void
6523 c_parser_objc_try_catch_statement (c_parser *parser)
6525 location_t loc;
6526 tree stmt;
6527 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6528 c_parser_consume_token (parser);
6529 loc = c_parser_peek_token (parser)->location;
6530 stmt = c_parser_compound_statement (parser);
6531 objc_begin_try_stmt (loc, stmt);
6532 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6534 struct c_parm *parm;
6535 c_parser_consume_token (parser);
6536 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6537 break;
6538 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6539 if (parm == NULL)
6541 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6542 break;
6544 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6545 objc_begin_catch_clause (grokparm (parm));
6546 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6547 c_parser_compound_statement_nostart (parser);
6548 objc_finish_catch_clause ();
6550 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6552 location_t finloc;
6553 tree finstmt;
6554 c_parser_consume_token (parser);
6555 finloc = c_parser_peek_token (parser)->location;
6556 finstmt = c_parser_compound_statement (parser);
6557 objc_build_finally_clause (finloc, finstmt);
6559 objc_finish_try_stmt ();
6562 /* Parse an objc-synchronized-statement.
6564 objc-synchronized-statement:
6565 @synchronized ( expression ) compound-statement
6568 static void
6569 c_parser_objc_synchronized_statement (c_parser *parser)
6571 location_t loc;
6572 tree expr, stmt;
6573 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6574 c_parser_consume_token (parser);
6575 loc = c_parser_peek_token (parser)->location;
6576 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6578 expr = c_parser_expression (parser).value;
6579 expr = c_fully_fold (expr, false, NULL);
6580 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6582 else
6583 expr = error_mark_node;
6584 stmt = c_parser_compound_statement (parser);
6585 objc_build_synchronized (loc, expr, stmt);
6588 /* Parse an objc-selector; return NULL_TREE without an error if the
6589 next token is not an objc-selector.
6591 objc-selector:
6592 identifier
6593 one of
6594 enum struct union if else while do for switch case default
6595 break continue return goto asm sizeof typeof __alignof
6596 unsigned long const short volatile signed restrict _Complex
6597 in out inout bycopy byref oneway int char float double void _Bool
6599 ??? Why this selection of keywords but not, for example, storage
6600 class specifiers? */
6602 static tree
6603 c_parser_objc_selector (c_parser *parser)
6605 c_token *token = c_parser_peek_token (parser);
6606 tree value = token->value;
6607 if (token->type == CPP_NAME)
6609 c_parser_consume_token (parser);
6610 return value;
6612 if (token->type != CPP_KEYWORD)
6613 return NULL_TREE;
6614 switch (token->keyword)
6616 case RID_ENUM:
6617 case RID_STRUCT:
6618 case RID_UNION:
6619 case RID_IF:
6620 case RID_ELSE:
6621 case RID_WHILE:
6622 case RID_DO:
6623 case RID_FOR:
6624 case RID_SWITCH:
6625 case RID_CASE:
6626 case RID_DEFAULT:
6627 case RID_BREAK:
6628 case RID_CONTINUE:
6629 case RID_RETURN:
6630 case RID_GOTO:
6631 case RID_ASM:
6632 case RID_SIZEOF:
6633 case RID_TYPEOF:
6634 case RID_ALIGNOF:
6635 case RID_UNSIGNED:
6636 case RID_LONG:
6637 case RID_CONST:
6638 case RID_SHORT:
6639 case RID_VOLATILE:
6640 case RID_SIGNED:
6641 case RID_RESTRICT:
6642 case RID_COMPLEX:
6643 case RID_IN:
6644 case RID_OUT:
6645 case RID_INOUT:
6646 case RID_BYCOPY:
6647 case RID_BYREF:
6648 case RID_ONEWAY:
6649 case RID_INT:
6650 case RID_CHAR:
6651 case RID_FLOAT:
6652 case RID_DOUBLE:
6653 case RID_VOID:
6654 case RID_BOOL:
6655 c_parser_consume_token (parser);
6656 return value;
6657 default:
6658 return NULL_TREE;
6662 /* Parse an objc-selector-arg.
6664 objc-selector-arg:
6665 objc-selector
6666 objc-keywordname-list
6668 objc-keywordname-list:
6669 objc-keywordname
6670 objc-keywordname-list objc-keywordname
6672 objc-keywordname:
6673 objc-selector :
6677 static tree
6678 c_parser_objc_selector_arg (c_parser *parser)
6680 tree sel = c_parser_objc_selector (parser);
6681 tree list = NULL_TREE;
6682 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6683 return sel;
6684 while (true)
6686 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6687 return list;
6688 list = chainon (list, build_tree_list (sel, NULL_TREE));
6689 sel = c_parser_objc_selector (parser);
6690 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6691 break;
6693 return list;
6696 /* Parse an objc-receiver.
6698 objc-receiver:
6699 expression
6700 class-name
6701 type-name
6704 static tree
6705 c_parser_objc_receiver (c_parser *parser)
6707 if (c_parser_peek_token (parser)->type == CPP_NAME
6708 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6709 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6711 tree id = c_parser_peek_token (parser)->value;
6712 c_parser_consume_token (parser);
6713 return objc_get_class_reference (id);
6715 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
6718 /* Parse objc-message-args.
6720 objc-message-args:
6721 objc-selector
6722 objc-keywordarg-list
6724 objc-keywordarg-list:
6725 objc-keywordarg
6726 objc-keywordarg-list objc-keywordarg
6728 objc-keywordarg:
6729 objc-selector : objc-keywordexpr
6730 : objc-keywordexpr
6733 static tree
6734 c_parser_objc_message_args (c_parser *parser)
6736 tree sel = c_parser_objc_selector (parser);
6737 tree list = NULL_TREE;
6738 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6739 return sel;
6740 while (true)
6742 tree keywordexpr;
6743 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6744 return list;
6745 keywordexpr = c_parser_objc_keywordexpr (parser);
6746 list = chainon (list, build_tree_list (sel, keywordexpr));
6747 sel = c_parser_objc_selector (parser);
6748 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6749 break;
6751 return list;
6754 /* Parse an objc-keywordexpr.
6756 objc-keywordexpr:
6757 nonempty-expr-list
6760 static tree
6761 c_parser_objc_keywordexpr (c_parser *parser)
6763 tree ret;
6764 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
6765 if (VEC_length (tree, expr_list) == 1)
6767 /* Just return the expression, remove a level of
6768 indirection. */
6769 ret = VEC_index (tree, expr_list, 0);
6771 else
6773 /* We have a comma expression, we will collapse later. */
6774 ret = build_tree_list_vec (expr_list);
6776 release_tree_vector (expr_list);
6777 return ret;
6781 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6782 should be considered, statements. ALLOW_STMT is true if we're within
6783 the context of a function and such pragmas are to be allowed. Returns
6784 true if we actually parsed such a pragma. */
6786 static bool
6787 c_parser_pragma (c_parser *parser, enum pragma_context context)
6789 unsigned int id;
6791 id = c_parser_peek_token (parser)->pragma_kind;
6792 gcc_assert (id != PRAGMA_NONE);
6794 switch (id)
6796 case PRAGMA_OMP_BARRIER:
6797 if (context != pragma_compound)
6799 if (context == pragma_stmt)
6800 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6801 "used in compound statements");
6802 goto bad_stmt;
6804 c_parser_omp_barrier (parser);
6805 return false;
6807 case PRAGMA_OMP_FLUSH:
6808 if (context != pragma_compound)
6810 if (context == pragma_stmt)
6811 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6812 "used in compound statements");
6813 goto bad_stmt;
6815 c_parser_omp_flush (parser);
6816 return false;
6818 case PRAGMA_OMP_TASKWAIT:
6819 if (context != pragma_compound)
6821 if (context == pragma_stmt)
6822 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
6823 "used in compound statements");
6824 goto bad_stmt;
6826 c_parser_omp_taskwait (parser);
6827 return false;
6829 case PRAGMA_OMP_THREADPRIVATE:
6830 c_parser_omp_threadprivate (parser);
6831 return false;
6833 case PRAGMA_OMP_SECTION:
6834 error_at (c_parser_peek_token (parser)->location,
6835 "%<#pragma omp section%> may only be used in "
6836 "%<#pragma omp sections%> construct");
6837 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6838 return false;
6840 case PRAGMA_GCC_PCH_PREPROCESS:
6841 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6842 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6843 return false;
6845 default:
6846 if (id < PRAGMA_FIRST_EXTERNAL)
6848 if (context == pragma_external)
6850 bad_stmt:
6851 c_parser_error (parser, "expected declaration specifiers");
6852 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6853 return false;
6855 c_parser_omp_construct (parser);
6856 return true;
6858 break;
6861 c_parser_consume_pragma (parser);
6862 c_invoke_pragma_handler (id);
6864 /* Skip to EOL, but suppress any error message. Those will have been
6865 generated by the handler routine through calling error, as opposed
6866 to calling c_parser_error. */
6867 parser->error = true;
6868 c_parser_skip_to_pragma_eol (parser);
6870 return false;
6873 /* The interface the pragma parsers have to the lexer. */
6875 enum cpp_ttype
6876 pragma_lex (tree *value)
6878 c_token *tok = c_parser_peek_token (the_parser);
6879 enum cpp_ttype ret = tok->type;
6881 *value = tok->value;
6882 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6883 ret = CPP_EOF;
6884 else
6886 if (ret == CPP_KEYWORD)
6887 ret = CPP_NAME;
6888 c_parser_consume_token (the_parser);
6891 return ret;
6894 static void
6895 c_parser_pragma_pch_preprocess (c_parser *parser)
6897 tree name = NULL;
6899 c_parser_consume_pragma (parser);
6900 if (c_parser_next_token_is (parser, CPP_STRING))
6902 name = c_parser_peek_token (parser)->value;
6903 c_parser_consume_token (parser);
6905 else
6906 c_parser_error (parser, "expected string literal");
6907 c_parser_skip_to_pragma_eol (parser);
6909 if (name)
6910 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6913 /* OpenMP 2.5 parsing routines. */
6915 /* Returns name of the next clause.
6916 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6917 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6918 returned and the token is consumed. */
6920 static pragma_omp_clause
6921 c_parser_omp_clause_name (c_parser *parser)
6923 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6925 if (c_parser_next_token_is_keyword (parser, RID_IF))
6926 result = PRAGMA_OMP_CLAUSE_IF;
6927 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6928 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6929 else if (c_parser_next_token_is (parser, CPP_NAME))
6931 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6933 switch (p[0])
6935 case 'c':
6936 if (!strcmp ("collapse", p))
6937 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
6938 else if (!strcmp ("copyin", p))
6939 result = PRAGMA_OMP_CLAUSE_COPYIN;
6940 else if (!strcmp ("copyprivate", p))
6941 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6942 break;
6943 case 'f':
6944 if (!strcmp ("firstprivate", p))
6945 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6946 break;
6947 case 'l':
6948 if (!strcmp ("lastprivate", p))
6949 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6950 break;
6951 case 'n':
6952 if (!strcmp ("nowait", p))
6953 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6954 else if (!strcmp ("num_threads", p))
6955 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6956 break;
6957 case 'o':
6958 if (!strcmp ("ordered", p))
6959 result = PRAGMA_OMP_CLAUSE_ORDERED;
6960 break;
6961 case 'p':
6962 if (!strcmp ("private", p))
6963 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6964 break;
6965 case 'r':
6966 if (!strcmp ("reduction", p))
6967 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6968 break;
6969 case 's':
6970 if (!strcmp ("schedule", p))
6971 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6972 else if (!strcmp ("shared", p))
6973 result = PRAGMA_OMP_CLAUSE_SHARED;
6974 break;
6975 case 'u':
6976 if (!strcmp ("untied", p))
6977 result = PRAGMA_OMP_CLAUSE_UNTIED;
6978 break;
6982 if (result != PRAGMA_OMP_CLAUSE_NONE)
6983 c_parser_consume_token (parser);
6985 return result;
6988 /* Validate that a clause of the given type does not already exist. */
6990 static void
6991 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
6992 const char *name)
6994 tree c;
6996 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6997 if (OMP_CLAUSE_CODE (c) == code)
6999 error ("too many %qs clauses", name);
7000 break;
7004 /* OpenMP 2.5:
7005 variable-list:
7006 identifier
7007 variable-list , identifier
7009 If KIND is nonzero, create the appropriate node and install the decl
7010 in OMP_CLAUSE_DECL and add the node to the head of the list.
7012 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7013 return the list created. */
7015 static tree
7016 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
7017 tree list)
7019 if (c_parser_next_token_is_not (parser, CPP_NAME)
7020 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7021 c_parser_error (parser, "expected identifier");
7023 while (c_parser_next_token_is (parser, CPP_NAME)
7024 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7026 tree t = lookup_name (c_parser_peek_token (parser)->value);
7028 if (t == NULL_TREE)
7029 undeclared_variable (c_parser_peek_token (parser)->value,
7030 c_parser_peek_token (parser)->location);
7031 else if (t == error_mark_node)
7033 else if (kind != 0)
7035 tree u = build_omp_clause (kind);
7036 OMP_CLAUSE_DECL (u) = t;
7037 OMP_CLAUSE_CHAIN (u) = list;
7038 list = u;
7040 else
7041 list = tree_cons (t, NULL_TREE, list);
7043 c_parser_consume_token (parser);
7045 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7046 break;
7048 c_parser_consume_token (parser);
7051 return list;
7054 /* Similarly, but expect leading and trailing parenthesis. This is a very
7055 common case for omp clauses. */
7057 static tree
7058 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7059 tree list)
7061 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7063 list = c_parser_omp_variable_list (parser, kind, list);
7064 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7066 return list;
7069 /* OpenMP 3.0:
7070 collapse ( constant-expression ) */
7072 static tree
7073 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7075 tree c, num = error_mark_node;
7076 HOST_WIDE_INT n;
7077 location_t loc;
7079 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7081 loc = c_parser_peek_token (parser)->location;
7082 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7084 num = c_parser_expr_no_commas (parser, NULL).value;
7085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7087 if (num == error_mark_node)
7088 return list;
7089 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7090 || !host_integerp (num, 0)
7091 || (n = tree_low_cst (num, 0)) <= 0
7092 || (int) n != n)
7094 error_at (loc,
7095 "collapse argument needs positive constant integer expression");
7096 return list;
7098 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
7099 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7100 OMP_CLAUSE_CHAIN (c) = list;
7101 return c;
7104 /* OpenMP 2.5:
7105 copyin ( variable-list ) */
7107 static tree
7108 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7110 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7113 /* OpenMP 2.5:
7114 copyprivate ( variable-list ) */
7116 static tree
7117 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7119 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7122 /* OpenMP 2.5:
7123 default ( shared | none ) */
7125 static tree
7126 c_parser_omp_clause_default (c_parser *parser, tree list)
7128 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7129 tree c;
7131 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7132 return list;
7133 if (c_parser_next_token_is (parser, CPP_NAME))
7135 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7137 switch (p[0])
7139 case 'n':
7140 if (strcmp ("none", p) != 0)
7141 goto invalid_kind;
7142 kind = OMP_CLAUSE_DEFAULT_NONE;
7143 break;
7145 case 's':
7146 if (strcmp ("shared", p) != 0)
7147 goto invalid_kind;
7148 kind = OMP_CLAUSE_DEFAULT_SHARED;
7149 break;
7151 default:
7152 goto invalid_kind;
7155 c_parser_consume_token (parser);
7157 else
7159 invalid_kind:
7160 c_parser_error (parser, "expected %<none%> or %<shared%>");
7162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7164 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7165 return list;
7167 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7168 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
7169 OMP_CLAUSE_CHAIN (c) = list;
7170 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7172 return c;
7175 /* OpenMP 2.5:
7176 firstprivate ( variable-list ) */
7178 static tree
7179 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7181 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7184 /* OpenMP 2.5:
7185 if ( expression ) */
7187 static tree
7188 c_parser_omp_clause_if (c_parser *parser, tree list)
7190 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7192 tree t = c_parser_paren_condition (parser);
7193 tree c;
7195 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7197 c = build_omp_clause (OMP_CLAUSE_IF);
7198 OMP_CLAUSE_IF_EXPR (c) = t;
7199 OMP_CLAUSE_CHAIN (c) = list;
7200 list = c;
7202 else
7203 c_parser_error (parser, "expected %<(%>");
7205 return list;
7208 /* OpenMP 2.5:
7209 lastprivate ( variable-list ) */
7211 static tree
7212 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7214 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7217 /* OpenMP 2.5:
7218 nowait */
7220 static tree
7221 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7223 tree c;
7225 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7227 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
7228 OMP_CLAUSE_CHAIN (c) = list;
7229 return c;
7232 /* OpenMP 2.5:
7233 num_threads ( expression ) */
7235 static tree
7236 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7238 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7240 location_t expr_loc = c_parser_peek_token (parser)->location;
7241 tree c, t = c_parser_expression (parser).value;
7242 t = c_fully_fold (t, false, NULL);
7244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7246 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7248 c_parser_error (parser, "expected integer expression");
7249 return list;
7252 /* Attempt to statically determine when the number isn't positive. */
7253 c = fold_build2 (LE_EXPR, boolean_type_node, t,
7254 build_int_cst (TREE_TYPE (t), 0));
7255 if (c == boolean_true_node)
7257 warning_at (expr_loc, 0,
7258 "%<num_threads%> value must be positive");
7259 t = integer_one_node;
7262 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7264 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
7265 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7266 OMP_CLAUSE_CHAIN (c) = list;
7267 list = c;
7270 return list;
7273 /* OpenMP 2.5:
7274 ordered */
7276 static tree
7277 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7279 tree c;
7281 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7283 c = build_omp_clause (OMP_CLAUSE_ORDERED);
7284 OMP_CLAUSE_CHAIN (c) = list;
7285 return c;
7288 /* OpenMP 2.5:
7289 private ( variable-list ) */
7291 static tree
7292 c_parser_omp_clause_private (c_parser *parser, tree list)
7294 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7297 /* OpenMP 2.5:
7298 reduction ( reduction-operator : variable-list )
7300 reduction-operator:
7301 One of: + * - & ^ | && || */
7303 static tree
7304 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7306 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7308 enum tree_code code;
7310 switch (c_parser_peek_token (parser)->type)
7312 case CPP_PLUS:
7313 code = PLUS_EXPR;
7314 break;
7315 case CPP_MULT:
7316 code = MULT_EXPR;
7317 break;
7318 case CPP_MINUS:
7319 code = MINUS_EXPR;
7320 break;
7321 case CPP_AND:
7322 code = BIT_AND_EXPR;
7323 break;
7324 case CPP_XOR:
7325 code = BIT_XOR_EXPR;
7326 break;
7327 case CPP_OR:
7328 code = BIT_IOR_EXPR;
7329 break;
7330 case CPP_AND_AND:
7331 code = TRUTH_ANDIF_EXPR;
7332 break;
7333 case CPP_OR_OR:
7334 code = TRUTH_ORIF_EXPR;
7335 break;
7336 default:
7337 c_parser_error (parser,
7338 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7339 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7340 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7341 return list;
7343 c_parser_consume_token (parser);
7344 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7346 tree nl, c;
7348 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
7349 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7350 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7352 list = nl;
7354 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7356 return list;
7359 /* OpenMP 2.5:
7360 schedule ( schedule-kind )
7361 schedule ( schedule-kind , expression )
7363 schedule-kind:
7364 static | dynamic | guided | runtime | auto
7367 static tree
7368 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7370 tree c, t;
7372 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7373 return list;
7375 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7377 if (c_parser_next_token_is (parser, CPP_NAME))
7379 tree kind = c_parser_peek_token (parser)->value;
7380 const char *p = IDENTIFIER_POINTER (kind);
7382 switch (p[0])
7384 case 'd':
7385 if (strcmp ("dynamic", p) != 0)
7386 goto invalid_kind;
7387 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7388 break;
7390 case 'g':
7391 if (strcmp ("guided", p) != 0)
7392 goto invalid_kind;
7393 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7394 break;
7396 case 'r':
7397 if (strcmp ("runtime", p) != 0)
7398 goto invalid_kind;
7399 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7400 break;
7402 default:
7403 goto invalid_kind;
7406 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7407 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7408 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7409 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7410 else
7411 goto invalid_kind;
7413 c_parser_consume_token (parser);
7414 if (c_parser_next_token_is (parser, CPP_COMMA))
7416 location_t here;
7417 c_parser_consume_token (parser);
7419 here = c_parser_peek_token (parser)->location;
7420 t = c_parser_expr_no_commas (parser, NULL).value;
7421 t = c_fully_fold (t, false, NULL);
7423 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7424 error_at (here, "schedule %<runtime%> does not take "
7425 "a %<chunk_size%> parameter");
7426 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7427 error_at (here,
7428 "schedule %<auto%> does not take "
7429 "a %<chunk_size%> parameter");
7430 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7431 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7432 else
7433 c_parser_error (parser, "expected integer expression");
7435 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7437 else
7438 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7439 "expected %<,%> or %<)%>");
7441 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7442 OMP_CLAUSE_CHAIN (c) = list;
7443 return c;
7445 invalid_kind:
7446 c_parser_error (parser, "invalid schedule kind");
7447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7448 return list;
7451 /* OpenMP 2.5:
7452 shared ( variable-list ) */
7454 static tree
7455 c_parser_omp_clause_shared (c_parser *parser, tree list)
7457 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7460 /* OpenMP 3.0:
7461 untied */
7463 static tree
7464 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7466 tree c;
7468 /* FIXME: Should we allow duplicates? */
7469 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7471 c = build_omp_clause (OMP_CLAUSE_UNTIED);
7472 OMP_CLAUSE_CHAIN (c) = list;
7473 return c;
7476 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7477 is a bitmask in MASK. Return the list of clauses found; the result
7478 of clause default goes in *pdefault. */
7480 static tree
7481 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7482 const char *where)
7484 tree clauses = NULL;
7485 bool first = true;
7487 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7489 location_t here;
7490 pragma_omp_clause c_kind;
7491 const char *c_name;
7492 tree prev = clauses;
7494 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7495 c_parser_consume_token (parser);
7497 first = false;
7498 here = c_parser_peek_token (parser)->location;
7499 c_kind = c_parser_omp_clause_name (parser);
7501 switch (c_kind)
7503 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7504 clauses = c_parser_omp_clause_collapse (parser, clauses);
7505 c_name = "collapse";
7506 break;
7507 case PRAGMA_OMP_CLAUSE_COPYIN:
7508 clauses = c_parser_omp_clause_copyin (parser, clauses);
7509 c_name = "copyin";
7510 break;
7511 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7512 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7513 c_name = "copyprivate";
7514 break;
7515 case PRAGMA_OMP_CLAUSE_DEFAULT:
7516 clauses = c_parser_omp_clause_default (parser, clauses);
7517 c_name = "default";
7518 break;
7519 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7520 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7521 c_name = "firstprivate";
7522 break;
7523 case PRAGMA_OMP_CLAUSE_IF:
7524 clauses = c_parser_omp_clause_if (parser, clauses);
7525 c_name = "if";
7526 break;
7527 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7528 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7529 c_name = "lastprivate";
7530 break;
7531 case PRAGMA_OMP_CLAUSE_NOWAIT:
7532 clauses = c_parser_omp_clause_nowait (parser, clauses);
7533 c_name = "nowait";
7534 break;
7535 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7536 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7537 c_name = "num_threads";
7538 break;
7539 case PRAGMA_OMP_CLAUSE_ORDERED:
7540 clauses = c_parser_omp_clause_ordered (parser, clauses);
7541 c_name = "ordered";
7542 break;
7543 case PRAGMA_OMP_CLAUSE_PRIVATE:
7544 clauses = c_parser_omp_clause_private (parser, clauses);
7545 c_name = "private";
7546 break;
7547 case PRAGMA_OMP_CLAUSE_REDUCTION:
7548 clauses = c_parser_omp_clause_reduction (parser, clauses);
7549 c_name = "reduction";
7550 break;
7551 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7552 clauses = c_parser_omp_clause_schedule (parser, clauses);
7553 c_name = "schedule";
7554 break;
7555 case PRAGMA_OMP_CLAUSE_SHARED:
7556 clauses = c_parser_omp_clause_shared (parser, clauses);
7557 c_name = "shared";
7558 break;
7559 case PRAGMA_OMP_CLAUSE_UNTIED:
7560 clauses = c_parser_omp_clause_untied (parser, clauses);
7561 c_name = "untied";
7562 break;
7563 default:
7564 c_parser_error (parser, "expected %<#pragma omp%> clause");
7565 goto saw_error;
7568 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7570 /* Remove the invalid clause(s) from the list to avoid
7571 confusing the rest of the compiler. */
7572 clauses = prev;
7573 error_at (here, "%qs is not valid for %qs", c_name, where);
7577 saw_error:
7578 c_parser_skip_to_pragma_eol (parser);
7580 return c_finish_omp_clauses (clauses);
7583 /* OpenMP 2.5:
7584 structured-block:
7585 statement
7587 In practice, we're also interested in adding the statement to an
7588 outer node. So it is convenient if we work around the fact that
7589 c_parser_statement calls add_stmt. */
7591 static tree
7592 c_parser_omp_structured_block (c_parser *parser)
7594 tree stmt = push_stmt_list ();
7595 c_parser_statement (parser);
7596 return pop_stmt_list (stmt);
7599 /* OpenMP 2.5:
7600 # pragma omp atomic new-line
7601 expression-stmt
7603 expression-stmt:
7604 x binop= expr | x++ | ++x | x-- | --x
7605 binop:
7606 +, *, -, /, &, ^, |, <<, >>
7608 where x is an lvalue expression with scalar type. */
7610 static void
7611 c_parser_omp_atomic (c_parser *parser)
7613 tree lhs, rhs;
7614 tree stmt;
7615 enum tree_code code;
7616 struct c_expr rhs_expr;
7618 c_parser_skip_to_pragma_eol (parser);
7620 lhs = c_parser_unary_expression (parser).value;
7621 lhs = c_fully_fold (lhs, false, NULL);
7622 switch (TREE_CODE (lhs))
7624 case ERROR_MARK:
7625 saw_error:
7626 c_parser_skip_to_end_of_block_or_statement (parser);
7627 return;
7629 case PREINCREMENT_EXPR:
7630 case POSTINCREMENT_EXPR:
7631 lhs = TREE_OPERAND (lhs, 0);
7632 code = PLUS_EXPR;
7633 rhs = integer_one_node;
7634 break;
7636 case PREDECREMENT_EXPR:
7637 case POSTDECREMENT_EXPR:
7638 lhs = TREE_OPERAND (lhs, 0);
7639 code = MINUS_EXPR;
7640 rhs = integer_one_node;
7641 break;
7643 default:
7644 switch (c_parser_peek_token (parser)->type)
7646 case CPP_MULT_EQ:
7647 code = MULT_EXPR;
7648 break;
7649 case CPP_DIV_EQ:
7650 code = TRUNC_DIV_EXPR;
7651 break;
7652 case CPP_PLUS_EQ:
7653 code = PLUS_EXPR;
7654 break;
7655 case CPP_MINUS_EQ:
7656 code = MINUS_EXPR;
7657 break;
7658 case CPP_LSHIFT_EQ:
7659 code = LSHIFT_EXPR;
7660 break;
7661 case CPP_RSHIFT_EQ:
7662 code = RSHIFT_EXPR;
7663 break;
7664 case CPP_AND_EQ:
7665 code = BIT_AND_EXPR;
7666 break;
7667 case CPP_OR_EQ:
7668 code = BIT_IOR_EXPR;
7669 break;
7670 case CPP_XOR_EQ:
7671 code = BIT_XOR_EXPR;
7672 break;
7673 default:
7674 c_parser_error (parser,
7675 "invalid operator for %<#pragma omp atomic%>");
7676 goto saw_error;
7679 c_parser_consume_token (parser);
7680 rhs_expr = c_parser_expression (parser);
7681 rhs_expr = default_function_array_conversion (rhs_expr);
7682 rhs = rhs_expr.value;
7683 rhs = c_fully_fold (rhs, false, NULL);
7684 break;
7686 stmt = c_finish_omp_atomic (code, lhs, rhs);
7687 if (stmt != error_mark_node)
7688 add_stmt (stmt);
7689 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7693 /* OpenMP 2.5:
7694 # pragma omp barrier new-line
7697 static void
7698 c_parser_omp_barrier (c_parser *parser)
7700 c_parser_consume_pragma (parser);
7701 c_parser_skip_to_pragma_eol (parser);
7703 c_finish_omp_barrier ();
7706 /* OpenMP 2.5:
7707 # pragma omp critical [(name)] new-line
7708 structured-block
7711 static tree
7712 c_parser_omp_critical (c_parser *parser)
7714 tree stmt, name = NULL;
7716 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7718 c_parser_consume_token (parser);
7719 if (c_parser_next_token_is (parser, CPP_NAME))
7721 name = c_parser_peek_token (parser)->value;
7722 c_parser_consume_token (parser);
7723 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7725 else
7726 c_parser_error (parser, "expected identifier");
7728 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7729 c_parser_error (parser, "expected %<(%> or end of line");
7730 c_parser_skip_to_pragma_eol (parser);
7732 stmt = c_parser_omp_structured_block (parser);
7733 return c_finish_omp_critical (stmt, name);
7736 /* OpenMP 2.5:
7737 # pragma omp flush flush-vars[opt] new-line
7739 flush-vars:
7740 ( variable-list ) */
7742 static void
7743 c_parser_omp_flush (c_parser *parser)
7745 c_parser_consume_pragma (parser);
7746 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7747 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
7748 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7749 c_parser_error (parser, "expected %<(%> or end of line");
7750 c_parser_skip_to_pragma_eol (parser);
7752 c_finish_omp_flush ();
7755 /* Parse the restricted form of the for statement allowed by OpenMP.
7756 The real trick here is to determine the loop control variable early
7757 so that we can push a new decl if necessary to make it private. */
7759 static tree
7760 c_parser_omp_for_loop (c_parser *parser, tree clauses, tree *par_clauses)
7762 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7763 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
7764 location_t loc;
7765 bool fail = false, open_brace_parsed = false;
7766 int i, collapse = 1, nbraces = 0;
7768 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
7769 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
7770 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
7772 gcc_assert (collapse >= 1);
7774 declv = make_tree_vec (collapse);
7775 initv = make_tree_vec (collapse);
7776 condv = make_tree_vec (collapse);
7777 incrv = make_tree_vec (collapse);
7779 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7781 c_parser_error (parser, "for statement expected");
7782 return NULL;
7784 loc = c_parser_peek_token (parser)->location;
7785 c_parser_consume_token (parser);
7787 for (i = 0; i < collapse; i++)
7789 int bracecount = 0;
7791 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7792 goto pop_scopes;
7794 /* Parse the initialization declaration or expression. */
7795 if (c_parser_next_token_starts_declspecs (parser))
7797 if (i > 0)
7798 for_block
7799 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
7800 c_parser_declaration_or_fndef (parser, true, true, true, true);
7801 decl = check_for_loop_decls ();
7802 if (decl == NULL)
7803 goto error_init;
7804 if (DECL_INITIAL (decl) == error_mark_node)
7805 decl = error_mark_node;
7806 init = decl;
7808 else if (c_parser_next_token_is (parser, CPP_NAME)
7809 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7811 struct c_expr decl_exp;
7812 struct c_expr init_exp;
7813 location_t init_loc;
7815 decl_exp = c_parser_postfix_expression (parser);
7816 decl = decl_exp.value;
7818 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7819 init_loc = c_parser_peek_token (parser)->location;
7821 init_exp = c_parser_expr_no_commas (parser, NULL);
7822 init_exp = default_function_array_conversion (init_exp);
7823 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
7824 NOP_EXPR, init_exp.value,
7825 init_exp.original_type);
7826 init = c_process_expr_stmt (init);
7828 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7830 else
7832 error_init:
7833 c_parser_error (parser,
7834 "expected iteration declaration or initialization");
7835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7836 "expected %<)%>");
7837 fail = true;
7838 goto parse_next;
7841 /* Parse the loop condition. */
7842 cond = NULL_TREE;
7843 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7845 location_t cond_loc = c_parser_peek_token (parser)->location;
7846 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
7848 cond = cond_expr.value;
7849 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
7850 cond = c_fully_fold (cond, false, NULL);
7851 switch (cond_expr.original_code)
7853 case GT_EXPR:
7854 case GE_EXPR:
7855 case LT_EXPR:
7856 case LE_EXPR:
7857 break;
7858 default:
7859 /* Can't be cond = error_mark_node, because we want to preserve
7860 the location until c_finish_omp_for. */
7861 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
7862 break;
7864 protected_set_expr_location (cond, cond_loc);
7866 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7868 /* Parse the increment expression. */
7869 incr = NULL_TREE;
7870 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7872 location_t incr_loc = c_parser_peek_token (parser)->location;
7874 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7875 protected_set_expr_location (incr, incr_loc);
7877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7879 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
7880 fail = true;
7881 else
7883 TREE_VEC_ELT (declv, i) = decl;
7884 TREE_VEC_ELT (initv, i) = init;
7885 TREE_VEC_ELT (condv, i) = cond;
7886 TREE_VEC_ELT (incrv, i) = incr;
7889 parse_next:
7890 if (i == collapse - 1)
7891 break;
7893 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
7894 in between the collapsed for loops to be still considered perfectly
7895 nested. Hopefully the final version clarifies this.
7896 For now handle (multiple) {'s and empty statements. */
7899 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7901 c_parser_consume_token (parser);
7902 break;
7904 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7906 c_parser_consume_token (parser);
7907 bracecount++;
7909 else if (bracecount
7910 && c_parser_next_token_is (parser, CPP_SEMICOLON))
7911 c_parser_consume_token (parser);
7912 else
7914 c_parser_error (parser, "not enough perfectly nested loops");
7915 if (bracecount)
7917 open_brace_parsed = true;
7918 bracecount--;
7920 fail = true;
7921 collapse = 0;
7922 break;
7925 while (1);
7927 nbraces += bracecount;
7930 save_break = c_break_label;
7931 c_break_label = size_one_node;
7932 save_cont = c_cont_label;
7933 c_cont_label = NULL_TREE;
7934 body = push_stmt_list ();
7936 if (open_brace_parsed)
7938 stmt = c_begin_compound_stmt (true);
7939 c_parser_compound_statement_nostart (parser);
7940 add_stmt (c_end_compound_stmt (stmt, true));
7942 else
7943 add_stmt (c_parser_c99_block_statement (parser));
7944 if (c_cont_label)
7945 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7947 body = pop_stmt_list (body);
7948 c_break_label = save_break;
7949 c_cont_label = save_cont;
7951 while (nbraces)
7953 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7955 c_parser_consume_token (parser);
7956 nbraces--;
7958 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7959 c_parser_consume_token (parser);
7960 else
7962 c_parser_error (parser, "collapsed loops not perfectly nested");
7963 while (nbraces)
7965 stmt = c_begin_compound_stmt (true);
7966 add_stmt (body);
7967 c_parser_compound_statement_nostart (parser);
7968 body = c_end_compound_stmt (stmt, true);
7969 nbraces--;
7971 goto pop_scopes;
7975 /* Only bother calling c_finish_omp_for if we haven't already generated
7976 an error from the initialization parsing. */
7977 if (!fail)
7979 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
7980 if (stmt)
7982 if (par_clauses != NULL)
7984 tree *c;
7985 for (c = par_clauses; *c ; )
7986 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
7987 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
7988 c = &OMP_CLAUSE_CHAIN (*c);
7989 else
7991 for (i = 0; i < collapse; i++)
7992 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
7993 break;
7994 if (i == collapse)
7995 c = &OMP_CLAUSE_CHAIN (*c);
7996 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
7998 error_at (loc,
7999 "iteration variable %qD should not be firstprivate",
8000 OMP_CLAUSE_DECL (*c));
8001 *c = OMP_CLAUSE_CHAIN (*c);
8003 else
8005 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8006 change it to shared (decl) in
8007 OMP_PARALLEL_CLAUSES. */
8008 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
8009 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8010 OMP_CLAUSE_CHAIN (l) = clauses;
8011 clauses = l;
8012 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8016 OMP_FOR_CLAUSES (stmt) = clauses;
8018 ret = stmt;
8020 pop_scopes:
8021 while (for_block)
8023 stmt = c_end_compound_stmt (TREE_VALUE (for_block), true);
8024 add_stmt (stmt);
8025 for_block = TREE_CHAIN (for_block);
8027 return ret;
8030 /* OpenMP 2.5:
8031 #pragma omp for for-clause[optseq] new-line
8032 for-loop
8035 #define OMP_FOR_CLAUSE_MASK \
8036 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8037 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8038 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8039 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8040 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8041 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
8042 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
8043 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8045 static tree
8046 c_parser_omp_for (c_parser *parser)
8048 tree block, clauses, ret;
8050 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8051 "#pragma omp for");
8053 block = c_begin_compound_stmt (true);
8054 ret = c_parser_omp_for_loop (parser, clauses, NULL);
8055 block = c_end_compound_stmt (block, true);
8056 add_stmt (block);
8058 return ret;
8061 /* OpenMP 2.5:
8062 # pragma omp master new-line
8063 structured-block
8066 static tree
8067 c_parser_omp_master (c_parser *parser)
8069 c_parser_skip_to_pragma_eol (parser);
8070 return c_finish_omp_master (c_parser_omp_structured_block (parser));
8073 /* OpenMP 2.5:
8074 # pragma omp ordered new-line
8075 structured-block
8078 static tree
8079 c_parser_omp_ordered (c_parser *parser)
8081 c_parser_skip_to_pragma_eol (parser);
8082 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
8085 /* OpenMP 2.5:
8087 section-scope:
8088 { section-sequence }
8090 section-sequence:
8091 section-directive[opt] structured-block
8092 section-sequence section-directive structured-block */
8094 static tree
8095 c_parser_omp_sections_scope (c_parser *parser)
8097 tree stmt, substmt;
8098 bool error_suppress = false;
8099 location_t loc;
8101 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8103 /* Avoid skipping until the end of the block. */
8104 parser->error = false;
8105 return NULL_TREE;
8108 stmt = push_stmt_list ();
8110 loc = c_parser_peek_token (parser)->location;
8111 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8113 substmt = push_stmt_list ();
8115 while (1)
8117 c_parser_statement (parser);
8119 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8120 break;
8121 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8122 break;
8123 if (c_parser_next_token_is (parser, CPP_EOF))
8124 break;
8127 substmt = pop_stmt_list (substmt);
8128 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8129 SET_EXPR_LOCATION (substmt, loc);
8130 add_stmt (substmt);
8133 while (1)
8135 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8136 break;
8137 if (c_parser_next_token_is (parser, CPP_EOF))
8138 break;
8140 loc = c_parser_peek_token (parser)->location;
8141 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8143 c_parser_consume_pragma (parser);
8144 c_parser_skip_to_pragma_eol (parser);
8145 error_suppress = false;
8147 else if (!error_suppress)
8149 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8150 error_suppress = true;
8153 substmt = c_parser_omp_structured_block (parser);
8154 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8155 SET_EXPR_LOCATION (substmt, loc);
8156 add_stmt (substmt);
8158 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8159 "expected %<#pragma omp section%> or %<}%>");
8161 substmt = pop_stmt_list (stmt);
8163 stmt = make_node (OMP_SECTIONS);
8164 TREE_TYPE (stmt) = void_type_node;
8165 OMP_SECTIONS_BODY (stmt) = substmt;
8167 return add_stmt (stmt);
8170 /* OpenMP 2.5:
8171 # pragma omp sections sections-clause[optseq] newline
8172 sections-scope
8175 #define OMP_SECTIONS_CLAUSE_MASK \
8176 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8177 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8178 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8179 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8180 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8182 static tree
8183 c_parser_omp_sections (c_parser *parser)
8185 tree block, clauses, ret;
8187 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8188 "#pragma omp sections");
8190 block = c_begin_compound_stmt (true);
8191 ret = c_parser_omp_sections_scope (parser);
8192 if (ret)
8193 OMP_SECTIONS_CLAUSES (ret) = clauses;
8194 block = c_end_compound_stmt (block, true);
8195 add_stmt (block);
8197 return ret;
8200 /* OpenMP 2.5:
8201 # pragma parallel parallel-clause new-line
8202 # pragma parallel for parallel-for-clause new-line
8203 # pragma parallel sections parallel-sections-clause new-line
8206 #define OMP_PARALLEL_CLAUSE_MASK \
8207 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8208 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8209 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8210 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8211 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8212 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8213 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8214 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8216 static tree
8217 c_parser_omp_parallel (c_parser *parser)
8219 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8220 const char *p_name = "#pragma omp parallel";
8221 tree stmt, clauses, par_clause, ws_clause, block;
8222 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8224 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8226 c_parser_consume_token (parser);
8227 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8228 p_name = "#pragma omp parallel for";
8229 mask |= OMP_FOR_CLAUSE_MASK;
8230 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8232 else if (c_parser_next_token_is (parser, CPP_NAME))
8234 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8235 if (strcmp (p, "sections") == 0)
8237 c_parser_consume_token (parser);
8238 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8239 p_name = "#pragma omp parallel sections";
8240 mask |= OMP_SECTIONS_CLAUSE_MASK;
8241 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8245 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8247 switch (p_kind)
8249 case PRAGMA_OMP_PARALLEL:
8250 block = c_begin_omp_parallel ();
8251 c_parser_statement (parser);
8252 stmt = c_finish_omp_parallel (clauses, block);
8253 break;
8255 case PRAGMA_OMP_PARALLEL_FOR:
8256 block = c_begin_omp_parallel ();
8257 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8258 c_parser_omp_for_loop (parser, ws_clause, &par_clause);
8259 stmt = c_finish_omp_parallel (par_clause, block);
8260 OMP_PARALLEL_COMBINED (stmt) = 1;
8261 break;
8263 case PRAGMA_OMP_PARALLEL_SECTIONS:
8264 block = c_begin_omp_parallel ();
8265 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
8266 stmt = c_parser_omp_sections_scope (parser);
8267 if (stmt)
8268 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8269 stmt = c_finish_omp_parallel (par_clause, block);
8270 OMP_PARALLEL_COMBINED (stmt) = 1;
8271 break;
8273 default:
8274 gcc_unreachable ();
8277 return stmt;
8280 /* OpenMP 2.5:
8281 # pragma omp single single-clause[optseq] new-line
8282 structured-block
8285 #define OMP_SINGLE_CLAUSE_MASK \
8286 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8287 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8288 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8289 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8291 static tree
8292 c_parser_omp_single (c_parser *parser)
8294 tree stmt = make_node (OMP_SINGLE);
8295 TREE_TYPE (stmt) = void_type_node;
8297 OMP_SINGLE_CLAUSES (stmt)
8298 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8299 "#pragma omp single");
8300 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8302 return add_stmt (stmt);
8305 /* OpenMP 3.0:
8306 # pragma omp task task-clause[optseq] new-line
8309 #define OMP_TASK_CLAUSE_MASK \
8310 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8311 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8312 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8313 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8314 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8315 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8317 static tree
8318 c_parser_omp_task (c_parser *parser)
8320 tree clauses, block;
8322 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8323 "#pragma omp task");
8325 block = c_begin_omp_task ();
8326 c_parser_statement (parser);
8327 return c_finish_omp_task (clauses, block);
8330 /* OpenMP 3.0:
8331 # pragma omp taskwait new-line
8334 static void
8335 c_parser_omp_taskwait (c_parser *parser)
8337 c_parser_consume_pragma (parser);
8338 c_parser_skip_to_pragma_eol (parser);
8340 c_finish_omp_taskwait ();
8343 /* Main entry point to parsing most OpenMP pragmas. */
8345 static void
8346 c_parser_omp_construct (c_parser *parser)
8348 enum pragma_kind p_kind;
8349 location_t loc;
8350 tree stmt;
8352 loc = c_parser_peek_token (parser)->location;
8353 p_kind = c_parser_peek_token (parser)->pragma_kind;
8354 c_parser_consume_pragma (parser);
8356 /* For all constructs below except #pragma omp atomic
8357 MUST_NOT_THROW catch handlers are needed when exceptions
8358 are enabled. */
8359 if (p_kind != PRAGMA_OMP_ATOMIC)
8360 c_maybe_initialize_eh ();
8362 switch (p_kind)
8364 case PRAGMA_OMP_ATOMIC:
8365 c_parser_omp_atomic (parser);
8366 return;
8367 case PRAGMA_OMP_CRITICAL:
8368 stmt = c_parser_omp_critical (parser);
8369 break;
8370 case PRAGMA_OMP_FOR:
8371 stmt = c_parser_omp_for (parser);
8372 break;
8373 case PRAGMA_OMP_MASTER:
8374 stmt = c_parser_omp_master (parser);
8375 break;
8376 case PRAGMA_OMP_ORDERED:
8377 stmt = c_parser_omp_ordered (parser);
8378 break;
8379 case PRAGMA_OMP_PARALLEL:
8380 stmt = c_parser_omp_parallel (parser);
8381 break;
8382 case PRAGMA_OMP_SECTIONS:
8383 stmt = c_parser_omp_sections (parser);
8384 break;
8385 case PRAGMA_OMP_SINGLE:
8386 stmt = c_parser_omp_single (parser);
8387 break;
8388 case PRAGMA_OMP_TASK:
8389 stmt = c_parser_omp_task (parser);
8390 break;
8391 default:
8392 gcc_unreachable ();
8395 if (stmt)
8396 SET_EXPR_LOCATION (stmt, loc);
8400 /* OpenMP 2.5:
8401 # pragma omp threadprivate (variable-list) */
8403 static void
8404 c_parser_omp_threadprivate (c_parser *parser)
8406 tree vars, t;
8408 c_parser_consume_pragma (parser);
8409 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8411 /* Mark every variable in VARS to be assigned thread local storage. */
8412 for (t = vars; t; t = TREE_CHAIN (t))
8414 tree v = TREE_PURPOSE (t);
8416 /* If V had already been marked threadprivate, it doesn't matter
8417 whether it had been used prior to this point. */
8418 if (TREE_CODE (v) != VAR_DECL)
8419 error ("%qD is not a variable", v);
8420 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8421 error ("%qE declared %<threadprivate%> after first use", v);
8422 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8423 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8424 else if (TREE_TYPE (v) == error_mark_node)
8426 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8427 error ("%<threadprivate%> %qE has incomplete type", v);
8428 else
8430 if (! DECL_THREAD_LOCAL_P (v))
8432 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8433 /* If rtl has been already set for this var, call
8434 make_decl_rtl once again, so that encode_section_info
8435 has a chance to look at the new decl flags. */
8436 if (DECL_RTL_SET_P (v))
8437 make_decl_rtl (v);
8439 C_DECL_THREADPRIVATE_P (v) = 1;
8443 c_parser_skip_to_pragma_eol (parser);
8447 /* Parse a single source file. */
8449 void
8450 c_parse_file (void)
8452 /* Use local storage to begin. If the first token is a pragma, parse it.
8453 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8454 which will cause garbage collection. */
8455 c_parser tparser;
8457 memset (&tparser, 0, sizeof tparser);
8458 the_parser = &tparser;
8460 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8461 c_parser_pragma_pch_preprocess (&tparser);
8463 the_parser = GGC_NEW (c_parser);
8464 *the_parser = tparser;
8466 c_parser_translation_unit (the_parser);
8467 the_parser = NULL;
8470 #include "gt-c-parser.h"