Enable dumping of alias graphs.
[official-gcc/Ramakrishna.git] / gcc / c-parser.c
blob43b0c8ce38cf13f7c3ee9c0735f04b854dfb3662
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 location_t loc,
920 struct c_expr);
921 static struct c_expr c_parser_expression (c_parser *);
922 static struct c_expr c_parser_expression_conv (c_parser *);
923 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
924 VEC(tree,gc) **);
925 static void c_parser_omp_construct (c_parser *);
926 static void c_parser_omp_threadprivate (c_parser *);
927 static void c_parser_omp_barrier (c_parser *);
928 static void c_parser_omp_flush (c_parser *);
929 static void c_parser_omp_taskwait (c_parser *);
931 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
932 static bool c_parser_pragma (c_parser *, enum pragma_context);
934 /* These Objective-C parser functions are only ever called when
935 compiling Objective-C. */
936 static void c_parser_objc_class_definition (c_parser *);
937 static void c_parser_objc_class_instance_variables (c_parser *);
938 static void c_parser_objc_class_declaration (c_parser *);
939 static void c_parser_objc_alias_declaration (c_parser *);
940 static void c_parser_objc_protocol_definition (c_parser *);
941 static enum tree_code c_parser_objc_method_type (c_parser *);
942 static void c_parser_objc_method_definition (c_parser *);
943 static void c_parser_objc_methodprotolist (c_parser *);
944 static void c_parser_objc_methodproto (c_parser *);
945 static tree c_parser_objc_method_decl (c_parser *);
946 static tree c_parser_objc_type_name (c_parser *);
947 static tree c_parser_objc_protocol_refs (c_parser *);
948 static void c_parser_objc_try_catch_statement (c_parser *);
949 static void c_parser_objc_synchronized_statement (c_parser *);
950 static tree c_parser_objc_selector (c_parser *);
951 static tree c_parser_objc_selector_arg (c_parser *);
952 static tree c_parser_objc_receiver (c_parser *);
953 static tree c_parser_objc_message_args (c_parser *);
954 static tree c_parser_objc_keywordexpr (c_parser *);
956 /* Parse a translation unit (C90 6.7, C99 6.9).
958 translation-unit:
959 external-declarations
961 external-declarations:
962 external-declaration
963 external-declarations external-declaration
965 GNU extensions:
967 translation-unit:
968 empty
971 static void
972 c_parser_translation_unit (c_parser *parser)
974 if (c_parser_next_token_is (parser, CPP_EOF))
976 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
977 "ISO C forbids an empty translation unit");
979 else
981 void *obstack_position = obstack_alloc (&parser_obstack, 0);
982 mark_valid_location_for_stdc_pragma (false);
985 ggc_collect ();
986 c_parser_external_declaration (parser);
987 obstack_free (&parser_obstack, obstack_position);
989 while (c_parser_next_token_is_not (parser, CPP_EOF));
993 /* Parse an external declaration (C90 6.7, C99 6.9).
995 external-declaration:
996 function-definition
997 declaration
999 GNU extensions:
1001 external-declaration:
1002 asm-definition
1004 __extension__ external-declaration
1006 Objective-C:
1008 external-declaration:
1009 objc-class-definition
1010 objc-class-declaration
1011 objc-alias-declaration
1012 objc-protocol-definition
1013 objc-method-definition
1014 @end
1017 static void
1018 c_parser_external_declaration (c_parser *parser)
1020 int ext;
1021 switch (c_parser_peek_token (parser)->type)
1023 case CPP_KEYWORD:
1024 switch (c_parser_peek_token (parser)->keyword)
1026 case RID_EXTENSION:
1027 ext = disable_extension_diagnostics ();
1028 c_parser_consume_token (parser);
1029 c_parser_external_declaration (parser);
1030 restore_extension_diagnostics (ext);
1031 break;
1032 case RID_ASM:
1033 c_parser_asm_definition (parser);
1034 break;
1035 case RID_AT_INTERFACE:
1036 case RID_AT_IMPLEMENTATION:
1037 gcc_assert (c_dialect_objc ());
1038 c_parser_objc_class_definition (parser);
1039 break;
1040 case RID_CLASS:
1041 gcc_assert (c_dialect_objc ());
1042 c_parser_objc_class_declaration (parser);
1043 break;
1044 case RID_AT_ALIAS:
1045 gcc_assert (c_dialect_objc ());
1046 c_parser_objc_alias_declaration (parser);
1047 break;
1048 case RID_AT_PROTOCOL:
1049 gcc_assert (c_dialect_objc ());
1050 c_parser_objc_protocol_definition (parser);
1051 break;
1052 case RID_AT_END:
1053 gcc_assert (c_dialect_objc ());
1054 c_parser_consume_token (parser);
1055 objc_finish_implementation ();
1056 break;
1057 default:
1058 goto decl_or_fndef;
1060 break;
1061 case CPP_SEMICOLON:
1062 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1063 "ISO C does not allow extra %<;%> outside of a function");
1064 c_parser_consume_token (parser);
1065 break;
1066 case CPP_PRAGMA:
1067 mark_valid_location_for_stdc_pragma (true);
1068 c_parser_pragma (parser, pragma_external);
1069 mark_valid_location_for_stdc_pragma (false);
1070 break;
1071 case CPP_PLUS:
1072 case CPP_MINUS:
1073 if (c_dialect_objc ())
1075 c_parser_objc_method_definition (parser);
1076 break;
1078 /* Else fall through, and yield a syntax error trying to parse
1079 as a declaration or function definition. */
1080 default:
1081 decl_or_fndef:
1082 /* A declaration or a function definition. We can only tell
1083 which after parsing the declaration specifiers, if any, and
1084 the first declarator. */
1085 c_parser_declaration_or_fndef (parser, true, true, false, true);
1086 break;
1091 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1092 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1093 accepted; otherwise (old-style parameter declarations) only other
1094 declarations are accepted. If NESTED is true, we are inside a
1095 function or parsing old-style parameter declarations; any functions
1096 encountered are nested functions and declaration specifiers are
1097 required; otherwise we are at top level and functions are normal
1098 functions and declaration specifiers may be optional. If EMPTY_OK
1099 is true, empty declarations are OK (subject to all other
1100 constraints); otherwise (old-style parameter declarations) they are
1101 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1102 may start with attributes; otherwise they may not.
1104 declaration:
1105 declaration-specifiers init-declarator-list[opt] ;
1107 function-definition:
1108 declaration-specifiers[opt] declarator declaration-list[opt]
1109 compound-statement
1111 declaration-list:
1112 declaration
1113 declaration-list declaration
1115 init-declarator-list:
1116 init-declarator
1117 init-declarator-list , init-declarator
1119 init-declarator:
1120 declarator simple-asm-expr[opt] attributes[opt]
1121 declarator simple-asm-expr[opt] attributes[opt] = initializer
1123 GNU extensions:
1125 nested-function-definition:
1126 declaration-specifiers declarator declaration-list[opt]
1127 compound-statement
1129 The simple-asm-expr and attributes are GNU extensions.
1131 This function does not handle __extension__; that is handled in its
1132 callers. ??? Following the old parser, __extension__ may start
1133 external declarations, declarations in functions and declarations
1134 at the start of "for" loops, but not old-style parameter
1135 declarations.
1137 C99 requires declaration specifiers in a function definition; the
1138 absence is diagnosed through the diagnosis of implicit int. In GNU
1139 C we also allow but diagnose declarations without declaration
1140 specifiers, but only at top level (elsewhere they conflict with
1141 other syntax).
1143 OpenMP:
1145 declaration:
1146 threadprivate-directive */
1148 static void
1149 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1150 bool nested, bool start_attr_ok)
1152 struct c_declspecs *specs;
1153 tree prefix_attrs;
1154 tree all_prefix_attrs;
1155 bool diagnosed_no_specs = false;
1156 location_t here = c_parser_peek_token (parser)->location;
1158 specs = build_null_declspecs ();
1159 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1160 if (parser->error)
1162 c_parser_skip_to_end_of_block_or_statement (parser);
1163 return;
1165 if (nested && !specs->declspecs_seen_p)
1167 c_parser_error (parser, "expected declaration specifiers");
1168 c_parser_skip_to_end_of_block_or_statement (parser);
1169 return;
1171 finish_declspecs (specs);
1172 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1174 if (empty_ok)
1175 shadow_tag (specs);
1176 else
1178 shadow_tag_warned (specs, 1);
1179 pedwarn (here, 0, "empty declaration");
1181 c_parser_consume_token (parser);
1182 return;
1184 pending_xref_error ();
1185 prefix_attrs = specs->attrs;
1186 all_prefix_attrs = prefix_attrs;
1187 specs->attrs = NULL_TREE;
1188 while (true)
1190 struct c_declarator *declarator;
1191 bool dummy = false;
1192 tree fnbody;
1193 /* Declaring either one or more declarators (in which case we
1194 should diagnose if there were no declaration specifiers) or a
1195 function definition (in which case the diagnostic for
1196 implicit int suffices). */
1197 declarator = c_parser_declarator (parser, specs->type_seen_p,
1198 C_DTR_NORMAL, &dummy);
1199 if (declarator == NULL)
1201 c_parser_skip_to_end_of_block_or_statement (parser);
1202 return;
1204 if (c_parser_next_token_is (parser, CPP_EQ)
1205 || c_parser_next_token_is (parser, CPP_COMMA)
1206 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1207 || c_parser_next_token_is_keyword (parser, RID_ASM)
1208 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1210 tree asm_name = NULL_TREE;
1211 tree postfix_attrs = NULL_TREE;
1212 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1214 diagnosed_no_specs = true;
1215 pedwarn (here, 0, "data definition has no type or storage class");
1217 /* Having seen a data definition, there cannot now be a
1218 function definition. */
1219 fndef_ok = false;
1220 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1221 asm_name = c_parser_simple_asm_expr (parser);
1222 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1223 postfix_attrs = c_parser_attributes (parser);
1224 if (c_parser_next_token_is (parser, CPP_EQ))
1226 tree d;
1227 struct c_expr init;
1228 location_t init_loc;
1229 c_parser_consume_token (parser);
1230 /* The declaration of the variable is in effect while
1231 its initializer is parsed. */
1232 d = start_decl (declarator, specs, true,
1233 chainon (postfix_attrs, all_prefix_attrs));
1234 if (!d)
1235 d = error_mark_node;
1236 start_init (d, asm_name, global_bindings_p ());
1237 init_loc = c_parser_peek_token (parser)->location;
1238 init = c_parser_initializer (parser);
1239 finish_init ();
1240 if (d != error_mark_node)
1242 maybe_warn_string_init (TREE_TYPE (d), init);
1243 finish_decl (d, init_loc, init.value,
1244 init.original_type, asm_name);
1247 else
1249 tree d = start_decl (declarator, specs, false,
1250 chainon (postfix_attrs,
1251 all_prefix_attrs));
1252 if (d)
1253 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1254 NULL_TREE, asm_name);
1256 if (c_parser_next_token_is (parser, CPP_COMMA))
1258 c_parser_consume_token (parser);
1259 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1260 all_prefix_attrs = chainon (c_parser_attributes (parser),
1261 prefix_attrs);
1262 else
1263 all_prefix_attrs = prefix_attrs;
1264 continue;
1266 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1268 c_parser_consume_token (parser);
1269 return;
1271 else
1273 c_parser_error (parser, "expected %<,%> or %<;%>");
1274 c_parser_skip_to_end_of_block_or_statement (parser);
1275 return;
1278 else if (!fndef_ok)
1280 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1281 "%<asm%> or %<__attribute__%>");
1282 c_parser_skip_to_end_of_block_or_statement (parser);
1283 return;
1285 /* Function definition (nested or otherwise). */
1286 if (nested)
1288 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1289 c_push_function_context ();
1291 if (!start_function (specs, declarator, all_prefix_attrs))
1293 /* This can appear in many cases looking nothing like a
1294 function definition, so we don't give a more specific
1295 error suggesting there was one. */
1296 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1297 "or %<__attribute__%>");
1298 if (nested)
1299 c_pop_function_context ();
1300 break;
1302 /* Parse old-style parameter declarations. ??? Attributes are
1303 not allowed to start declaration specifiers here because of a
1304 syntax conflict between a function declaration with attribute
1305 suffix and a function definition with an attribute prefix on
1306 first old-style parameter declaration. Following the old
1307 parser, they are not accepted on subsequent old-style
1308 parameter declarations either. However, there is no
1309 ambiguity after the first declaration, nor indeed on the
1310 first as long as we don't allow postfix attributes after a
1311 declarator with a nonempty identifier list in a definition;
1312 and postfix attributes have never been accepted here in
1313 function definitions either. */
1314 while (c_parser_next_token_is_not (parser, CPP_EOF)
1315 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1316 c_parser_declaration_or_fndef (parser, false, false, true, false);
1317 store_parm_decls ();
1318 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1319 = c_parser_peek_token (parser)->location;
1320 fnbody = c_parser_compound_statement (parser);
1321 if (nested)
1323 tree decl = current_function_decl;
1324 add_stmt (fnbody);
1325 finish_function ();
1326 c_pop_function_context ();
1327 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1329 else
1331 add_stmt (fnbody);
1332 finish_function ();
1334 break;
1338 /* Parse an asm-definition (asm() outside a function body). This is a
1339 GNU extension.
1341 asm-definition:
1342 simple-asm-expr ;
1345 static void
1346 c_parser_asm_definition (c_parser *parser)
1348 tree asm_str = c_parser_simple_asm_expr (parser);
1349 if (asm_str)
1350 cgraph_add_asm_node (asm_str);
1351 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1354 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1355 6.7), adding them to SPECS (which may already include some).
1356 Storage class specifiers are accepted iff SCSPEC_OK; type
1357 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1358 the start iff START_ATTR_OK.
1360 declaration-specifiers:
1361 storage-class-specifier declaration-specifiers[opt]
1362 type-specifier declaration-specifiers[opt]
1363 type-qualifier declaration-specifiers[opt]
1364 function-specifier declaration-specifiers[opt]
1366 Function specifiers (inline) are from C99, and are currently
1367 handled as storage class specifiers, as is __thread.
1369 C90 6.5.1, C99 6.7.1:
1370 storage-class-specifier:
1371 typedef
1372 extern
1373 static
1374 auto
1375 register
1377 C99 6.7.4:
1378 function-specifier:
1379 inline
1381 C90 6.5.2, C99 6.7.2:
1382 type-specifier:
1383 void
1384 char
1385 short
1387 long
1388 float
1389 double
1390 signed
1391 unsigned
1392 _Bool
1393 _Complex
1394 [_Imaginary removed in C99 TC2]
1395 struct-or-union-specifier
1396 enum-specifier
1397 typedef-name
1399 (_Bool and _Complex are new in C99.)
1401 C90 6.5.3, C99 6.7.3:
1403 type-qualifier:
1404 const
1405 restrict
1406 volatile
1408 (restrict is new in C99.)
1410 GNU extensions:
1412 declaration-specifiers:
1413 attributes declaration-specifiers[opt]
1415 storage-class-specifier:
1416 __thread
1418 type-specifier:
1419 typeof-specifier
1420 _Decimal32
1421 _Decimal64
1422 _Decimal128
1423 _Fract
1424 _Accum
1425 _Sat
1427 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1428 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1430 Objective-C:
1432 type-specifier:
1433 class-name objc-protocol-refs[opt]
1434 typedef-name objc-protocol-refs
1435 objc-protocol-refs
1438 static void
1439 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1440 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1442 bool attrs_ok = start_attr_ok;
1443 bool seen_type = specs->type_seen_p;
1444 while (c_parser_next_token_is (parser, CPP_NAME)
1445 || c_parser_next_token_is (parser, CPP_KEYWORD)
1446 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1448 struct c_typespec t;
1449 tree attrs;
1450 location_t loc = c_parser_peek_token (parser)->location;
1451 if (c_parser_next_token_is (parser, CPP_NAME))
1453 tree value = c_parser_peek_token (parser)->value;
1454 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1455 /* This finishes the specifiers unless a type name is OK, it
1456 is declared as a type name and a type name hasn't yet
1457 been seen. */
1458 if (!typespec_ok || seen_type
1459 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1460 break;
1461 c_parser_consume_token (parser);
1462 seen_type = true;
1463 attrs_ok = true;
1464 if (kind == C_ID_TYPENAME
1465 && (!c_dialect_objc ()
1466 || c_parser_next_token_is_not (parser, CPP_LESS)))
1468 t.kind = ctsk_typedef;
1469 /* For a typedef name, record the meaning, not the name.
1470 In case of 'foo foo, bar;'. */
1471 t.spec = lookup_name (value);
1472 t.expr = NULL_TREE;
1473 t.expr_const_operands = true;
1475 else
1477 tree proto = NULL_TREE;
1478 gcc_assert (c_dialect_objc ());
1479 t.kind = ctsk_objc;
1480 if (c_parser_next_token_is (parser, CPP_LESS))
1481 proto = c_parser_objc_protocol_refs (parser);
1482 t.spec = objc_get_protocol_qualified_type (value, proto);
1483 t.expr = NULL_TREE;
1484 t.expr_const_operands = true;
1486 declspecs_add_type (loc, specs, t);
1487 continue;
1489 if (c_parser_next_token_is (parser, CPP_LESS))
1491 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1492 nisse@lysator.liu.se. */
1493 tree proto;
1494 gcc_assert (c_dialect_objc ());
1495 if (!typespec_ok || seen_type)
1496 break;
1497 proto = c_parser_objc_protocol_refs (parser);
1498 t.kind = ctsk_objc;
1499 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1500 t.expr = NULL_TREE;
1501 t.expr_const_operands = true;
1502 declspecs_add_type (loc, specs, t);
1503 continue;
1505 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1506 switch (c_parser_peek_token (parser)->keyword)
1508 case RID_STATIC:
1509 case RID_EXTERN:
1510 case RID_REGISTER:
1511 case RID_TYPEDEF:
1512 case RID_INLINE:
1513 case RID_AUTO:
1514 case RID_THREAD:
1515 if (!scspec_ok)
1516 goto out;
1517 attrs_ok = true;
1518 /* TODO: Distinguish between function specifiers (inline)
1519 and storage class specifiers, either here or in
1520 declspecs_add_scspec. */
1521 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1522 c_parser_consume_token (parser);
1523 break;
1524 case RID_UNSIGNED:
1525 case RID_LONG:
1526 case RID_SHORT:
1527 case RID_SIGNED:
1528 case RID_COMPLEX:
1529 case RID_INT:
1530 case RID_CHAR:
1531 case RID_FLOAT:
1532 case RID_DOUBLE:
1533 case RID_VOID:
1534 case RID_DFLOAT32:
1535 case RID_DFLOAT64:
1536 case RID_DFLOAT128:
1537 case RID_BOOL:
1538 case RID_FRACT:
1539 case RID_ACCUM:
1540 case RID_SAT:
1541 if (!typespec_ok)
1542 goto out;
1543 attrs_ok = true;
1544 seen_type = true;
1545 if (c_dialect_objc ())
1546 parser->objc_need_raw_identifier = true;
1547 t.kind = ctsk_resword;
1548 t.spec = c_parser_peek_token (parser)->value;
1549 t.expr = NULL_TREE;
1550 t.expr_const_operands = true;
1551 declspecs_add_type (loc, specs, t);
1552 c_parser_consume_token (parser);
1553 break;
1554 case RID_ENUM:
1555 if (!typespec_ok)
1556 goto out;
1557 attrs_ok = true;
1558 seen_type = true;
1559 t = c_parser_enum_specifier (parser);
1560 declspecs_add_type (loc, specs, t);
1561 break;
1562 case RID_STRUCT:
1563 case RID_UNION:
1564 if (!typespec_ok)
1565 goto out;
1566 attrs_ok = true;
1567 seen_type = true;
1568 t = c_parser_struct_or_union_specifier (parser);
1569 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1570 declspecs_add_type (loc, specs, t);
1571 break;
1572 case RID_TYPEOF:
1573 /* ??? The old parser rejected typeof after other type
1574 specifiers, but is a syntax error the best way of
1575 handling this? */
1576 if (!typespec_ok || seen_type)
1577 goto out;
1578 attrs_ok = true;
1579 seen_type = true;
1580 t = c_parser_typeof_specifier (parser);
1581 declspecs_add_type (loc, specs, t);
1582 break;
1583 case RID_CONST:
1584 case RID_VOLATILE:
1585 case RID_RESTRICT:
1586 attrs_ok = true;
1587 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1588 c_parser_consume_token (parser);
1589 break;
1590 case RID_ATTRIBUTE:
1591 if (!attrs_ok)
1592 goto out;
1593 attrs = c_parser_attributes (parser);
1594 declspecs_add_attrs (specs, attrs);
1595 break;
1596 default:
1597 goto out;
1600 out: ;
1603 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1605 enum-specifier:
1606 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1607 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1608 enum attributes[opt] identifier
1610 The form with trailing comma is new in C99. The forms with
1611 attributes are GNU extensions. In GNU C, we accept any expression
1612 without commas in the syntax (assignment expressions, not just
1613 conditional expressions); assignment expressions will be diagnosed
1614 as non-constant.
1616 enumerator-list:
1617 enumerator
1618 enumerator-list , enumerator
1620 enumerator:
1621 enumeration-constant
1622 enumeration-constant = constant-expression
1625 static struct c_typespec
1626 c_parser_enum_specifier (c_parser *parser)
1628 struct c_typespec ret;
1629 tree attrs;
1630 tree ident = NULL_TREE;
1631 location_t enum_loc;
1632 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1633 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1634 enum_loc = c_parser_peek_token (parser)->location;
1635 c_parser_consume_token (parser);
1636 attrs = c_parser_attributes (parser);
1637 enum_loc = c_parser_peek_token (parser)->location;
1638 /* Set the location in case we create a decl now. */
1639 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1640 if (c_parser_next_token_is (parser, CPP_NAME))
1642 ident = c_parser_peek_token (parser)->value;
1643 ident_loc = c_parser_peek_token (parser)->location;
1644 enum_loc = ident_loc;
1645 c_parser_consume_token (parser);
1647 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1649 /* Parse an enum definition. */
1650 struct c_enum_contents the_enum;
1651 tree type = start_enum (enum_loc, &the_enum, ident);
1652 tree postfix_attrs;
1653 /* We chain the enumerators in reverse order, then put them in
1654 forward order at the end. */
1655 tree values = NULL_TREE;
1656 c_parser_consume_token (parser);
1657 while (true)
1659 tree enum_id;
1660 tree enum_value;
1661 tree enum_decl;
1662 bool seen_comma;
1663 c_token *token;
1664 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1665 location_t value_loc;
1666 if (c_parser_next_token_is_not (parser, CPP_NAME))
1668 c_parser_error (parser, "expected identifier");
1669 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1670 values = error_mark_node;
1671 break;
1673 token = c_parser_peek_token (parser);
1674 enum_id = token->value;
1675 /* Set the location in case we create a decl now. */
1676 c_parser_set_source_position_from_token (token);
1677 value_loc = token->location;
1678 c_parser_consume_token (parser);
1679 if (c_parser_next_token_is (parser, CPP_EQ))
1681 c_parser_consume_token (parser);
1682 value_loc = c_parser_peek_token (parser)->location;
1683 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1685 else
1686 enum_value = NULL_TREE;
1687 enum_decl = build_enumerator (value_loc,
1688 &the_enum, enum_id, enum_value);
1689 TREE_CHAIN (enum_decl) = values;
1690 values = enum_decl;
1691 seen_comma = false;
1692 if (c_parser_next_token_is (parser, CPP_COMMA))
1694 comma_loc = c_parser_peek_token (parser)->location;
1695 seen_comma = true;
1696 c_parser_consume_token (parser);
1698 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1700 if (seen_comma && !flag_isoc99)
1701 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1702 c_parser_consume_token (parser);
1703 break;
1705 if (!seen_comma)
1707 c_parser_error (parser, "expected %<,%> or %<}%>");
1708 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1709 values = error_mark_node;
1710 break;
1713 postfix_attrs = c_parser_attributes (parser);
1714 ret.spec = finish_enum (type, nreverse (values),
1715 chainon (attrs, postfix_attrs));
1716 ret.kind = ctsk_tagdef;
1717 ret.expr = NULL_TREE;
1718 ret.expr_const_operands = true;
1719 return ret;
1721 else if (!ident)
1723 c_parser_error (parser, "expected %<{%>");
1724 ret.spec = error_mark_node;
1725 ret.kind = ctsk_tagref;
1726 ret.expr = NULL_TREE;
1727 ret.expr_const_operands = true;
1728 return ret;
1730 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
1731 /* In ISO C, enumerated types can be referred to only if already
1732 defined. */
1733 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1735 gcc_assert (ident);
1736 pedwarn (enum_loc, OPT_pedantic,
1737 "ISO C forbids forward references to %<enum%> types");
1739 return ret;
1742 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1744 struct-or-union-specifier:
1745 struct-or-union attributes[opt] identifier[opt]
1746 { struct-contents } attributes[opt]
1747 struct-or-union attributes[opt] identifier
1749 struct-contents:
1750 struct-declaration-list
1752 struct-declaration-list:
1753 struct-declaration ;
1754 struct-declaration-list struct-declaration ;
1756 GNU extensions:
1758 struct-contents:
1759 empty
1760 struct-declaration
1761 struct-declaration-list struct-declaration
1763 struct-declaration-list:
1764 struct-declaration-list ;
1767 (Note that in the syntax here, unlike that in ISO C, the semicolons
1768 are included here rather than in struct-declaration, in order to
1769 describe the syntax with extra semicolons and missing semicolon at
1770 end.)
1772 Objective-C:
1774 struct-declaration-list:
1775 @defs ( class-name )
1777 (Note this does not include a trailing semicolon, but can be
1778 followed by further declarations, and gets a pedwarn-if-pedantic
1779 when followed by a semicolon.) */
1781 static struct c_typespec
1782 c_parser_struct_or_union_specifier (c_parser *parser)
1784 struct c_typespec ret;
1785 tree attrs;
1786 tree ident = NULL_TREE;
1787 location_t struct_loc;
1788 location_t ident_loc = UNKNOWN_LOCATION;
1789 enum tree_code code;
1790 switch (c_parser_peek_token (parser)->keyword)
1792 case RID_STRUCT:
1793 code = RECORD_TYPE;
1794 break;
1795 case RID_UNION:
1796 code = UNION_TYPE;
1797 break;
1798 default:
1799 gcc_unreachable ();
1801 struct_loc = c_parser_peek_token (parser)->location;
1802 c_parser_consume_token (parser);
1803 attrs = c_parser_attributes (parser);
1805 /* Set the location in case we create a decl now. */
1806 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1808 if (c_parser_next_token_is (parser, CPP_NAME))
1810 ident = c_parser_peek_token (parser)->value;
1811 ident_loc = c_parser_peek_token (parser)->location;
1812 struct_loc = ident_loc;
1813 c_parser_consume_token (parser);
1815 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1817 /* Parse a struct or union definition. Start the scope of the
1818 tag before parsing components. */
1819 struct c_struct_parse_info *struct_info;
1820 tree type = start_struct (struct_loc, code, ident, &struct_info);
1821 tree postfix_attrs;
1822 /* We chain the components in reverse order, then put them in
1823 forward order at the end. Each struct-declaration may
1824 declare multiple components (comma-separated), so we must use
1825 chainon to join them, although when parsing each
1826 struct-declaration we can use TREE_CHAIN directly.
1828 The theory behind all this is that there will be more
1829 semicolon separated fields than comma separated fields, and
1830 so we'll be minimizing the number of node traversals required
1831 by chainon. */
1832 tree contents = NULL_TREE;
1833 c_parser_consume_token (parser);
1834 /* Handle the Objective-C @defs construct,
1835 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1836 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1838 tree name;
1839 gcc_assert (c_dialect_objc ());
1840 c_parser_consume_token (parser);
1841 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1842 goto end_at_defs;
1843 if (c_parser_next_token_is (parser, CPP_NAME)
1844 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1846 name = c_parser_peek_token (parser)->value;
1847 c_parser_consume_token (parser);
1849 else
1851 c_parser_error (parser, "expected class name");
1852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1853 goto end_at_defs;
1855 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1856 "expected %<)%>");
1857 contents = nreverse (objc_get_class_ivars (name));
1859 end_at_defs:
1860 /* Parse the struct-declarations and semicolons. Problems with
1861 semicolons are diagnosed here; empty structures are diagnosed
1862 elsewhere. */
1863 while (true)
1865 tree decls;
1866 /* Parse any stray semicolon. */
1867 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1869 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1870 "extra semicolon in struct or union specified");
1871 c_parser_consume_token (parser);
1872 continue;
1874 /* Stop if at the end of the struct or union contents. */
1875 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1877 c_parser_consume_token (parser);
1878 break;
1880 /* Accept #pragmas at struct scope. */
1881 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1883 c_parser_pragma (parser, pragma_external);
1884 continue;
1886 /* Parse some comma-separated declarations, but not the
1887 trailing semicolon if any. */
1888 decls = c_parser_struct_declaration (parser);
1889 contents = chainon (decls, contents);
1890 /* If no semicolon follows, either we have a parse error or
1891 are at the end of the struct or union and should
1892 pedwarn. */
1893 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1894 c_parser_consume_token (parser);
1895 else
1897 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1898 pedwarn (c_parser_peek_token (parser)->location, 0,
1899 "no semicolon at end of struct or union");
1900 else
1902 c_parser_error (parser, "expected %<;%>");
1903 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1904 break;
1908 postfix_attrs = c_parser_attributes (parser);
1909 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
1910 chainon (attrs, postfix_attrs), struct_info);
1911 ret.kind = ctsk_tagdef;
1912 ret.expr = NULL_TREE;
1913 ret.expr_const_operands = true;
1914 return ret;
1916 else if (!ident)
1918 c_parser_error (parser, "expected %<{%>");
1919 ret.spec = error_mark_node;
1920 ret.kind = ctsk_tagref;
1921 ret.expr = NULL_TREE;
1922 ret.expr_const_operands = true;
1923 return ret;
1925 ret = parser_xref_tag (ident_loc, code, ident);
1926 return ret;
1929 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1930 the trailing semicolon.
1932 struct-declaration:
1933 specifier-qualifier-list struct-declarator-list
1935 specifier-qualifier-list:
1936 type-specifier specifier-qualifier-list[opt]
1937 type-qualifier specifier-qualifier-list[opt]
1938 attributes specifier-qualifier-list[opt]
1940 struct-declarator-list:
1941 struct-declarator
1942 struct-declarator-list , attributes[opt] struct-declarator
1944 struct-declarator:
1945 declarator attributes[opt]
1946 declarator[opt] : constant-expression attributes[opt]
1948 GNU extensions:
1950 struct-declaration:
1951 __extension__ struct-declaration
1952 specifier-qualifier-list
1954 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1955 of attributes where shown is a GNU extension. In GNU C, we accept
1956 any expression without commas in the syntax (assignment
1957 expressions, not just conditional expressions); assignment
1958 expressions will be diagnosed as non-constant. */
1960 static tree
1961 c_parser_struct_declaration (c_parser *parser)
1963 struct c_declspecs *specs;
1964 tree prefix_attrs;
1965 tree all_prefix_attrs;
1966 tree decls;
1967 location_t decl_loc;
1968 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1970 int ext;
1971 tree decl;
1972 ext = disable_extension_diagnostics ();
1973 c_parser_consume_token (parser);
1974 decl = c_parser_struct_declaration (parser);
1975 restore_extension_diagnostics (ext);
1976 return decl;
1978 specs = build_null_declspecs ();
1979 decl_loc = c_parser_peek_token (parser)->location;
1980 c_parser_declspecs (parser, specs, false, true, true);
1981 if (parser->error)
1982 return NULL_TREE;
1983 if (!specs->declspecs_seen_p)
1985 c_parser_error (parser, "expected specifier-qualifier-list");
1986 return NULL_TREE;
1988 finish_declspecs (specs);
1989 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1991 tree ret;
1992 if (!specs->type_seen_p)
1994 pedwarn (decl_loc, OPT_pedantic,
1995 "ISO C forbids member declarations with no members");
1996 shadow_tag_warned (specs, pedantic);
1997 ret = NULL_TREE;
1999 else
2001 /* Support for unnamed structs or unions as members of
2002 structs or unions (which is [a] useful and [b] supports
2003 MS P-SDK). */
2004 tree attrs = NULL;
2006 ret = grokfield (c_parser_peek_token (parser)->location,
2007 build_id_declarator (NULL_TREE), specs,
2008 NULL_TREE, &attrs);
2009 if (ret)
2010 decl_attributes (&ret, attrs, 0);
2012 return ret;
2014 pending_xref_error ();
2015 prefix_attrs = specs->attrs;
2016 all_prefix_attrs = prefix_attrs;
2017 specs->attrs = NULL_TREE;
2018 decls = NULL_TREE;
2019 while (true)
2021 /* Declaring one or more declarators or un-named bit-fields. */
2022 struct c_declarator *declarator;
2023 bool dummy = false;
2024 if (c_parser_next_token_is (parser, CPP_COLON))
2025 declarator = build_id_declarator (NULL_TREE);
2026 else
2027 declarator = c_parser_declarator (parser, specs->type_seen_p,
2028 C_DTR_NORMAL, &dummy);
2029 if (declarator == NULL)
2031 c_parser_skip_to_end_of_block_or_statement (parser);
2032 break;
2034 if (c_parser_next_token_is (parser, CPP_COLON)
2035 || c_parser_next_token_is (parser, CPP_COMMA)
2036 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2037 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2038 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2040 tree postfix_attrs = NULL_TREE;
2041 tree width = NULL_TREE;
2042 tree d;
2043 if (c_parser_next_token_is (parser, CPP_COLON))
2045 c_parser_consume_token (parser);
2046 width = c_parser_expr_no_commas (parser, NULL).value;
2048 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2049 postfix_attrs = c_parser_attributes (parser);
2050 d = grokfield (c_parser_peek_token (parser)->location,
2051 declarator, specs, width, &all_prefix_attrs);
2052 decl_attributes (&d, chainon (postfix_attrs,
2053 all_prefix_attrs), 0);
2054 TREE_CHAIN (d) = decls;
2055 decls = d;
2056 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2057 all_prefix_attrs = chainon (c_parser_attributes (parser),
2058 prefix_attrs);
2059 else
2060 all_prefix_attrs = prefix_attrs;
2061 if (c_parser_next_token_is (parser, CPP_COMMA))
2062 c_parser_consume_token (parser);
2063 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2064 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2066 /* Semicolon consumed in caller. */
2067 break;
2069 else
2071 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2072 break;
2075 else
2077 c_parser_error (parser,
2078 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2079 "%<__attribute__%>");
2080 break;
2083 return decls;
2086 /* Parse a typeof specifier (a GNU extension).
2088 typeof-specifier:
2089 typeof ( expression )
2090 typeof ( type-name )
2093 static struct c_typespec
2094 c_parser_typeof_specifier (c_parser *parser)
2096 struct c_typespec ret;
2097 ret.kind = ctsk_typeof;
2098 ret.spec = error_mark_node;
2099 ret.expr = NULL_TREE;
2100 ret.expr_const_operands = true;
2101 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2102 c_parser_consume_token (parser);
2103 c_inhibit_evaluation_warnings++;
2104 in_typeof++;
2105 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2107 c_inhibit_evaluation_warnings--;
2108 in_typeof--;
2109 return ret;
2111 if (c_parser_next_token_starts_typename (parser))
2113 struct c_type_name *type = c_parser_type_name (parser);
2114 c_inhibit_evaluation_warnings--;
2115 in_typeof--;
2116 if (type != NULL)
2118 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2119 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2122 else
2124 bool was_vm;
2125 location_t here = c_parser_peek_token (parser)->location;
2126 struct c_expr expr = c_parser_expression (parser);
2127 c_inhibit_evaluation_warnings--;
2128 in_typeof--;
2129 if (TREE_CODE (expr.value) == COMPONENT_REF
2130 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2131 error_at (here, "%<typeof%> applied to a bit-field");
2132 ret.spec = TREE_TYPE (expr.value);
2133 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2134 /* This is returned with the type so that when the type is
2135 evaluated, this can be evaluated. */
2136 if (was_vm)
2137 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2138 pop_maybe_used (was_vm);
2140 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2141 return ret;
2144 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2145 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2146 be redeclared; otherwise it may not. KIND indicates which kind of
2147 declarator is wanted. Returns a valid declarator except in the
2148 case of a syntax error in which case NULL is returned. *SEEN_ID is
2149 set to true if an identifier being declared is seen; this is used
2150 to diagnose bad forms of abstract array declarators and to
2151 determine whether an identifier list is syntactically permitted.
2153 declarator:
2154 pointer[opt] direct-declarator
2156 direct-declarator:
2157 identifier
2158 ( attributes[opt] declarator )
2159 direct-declarator array-declarator
2160 direct-declarator ( parameter-type-list )
2161 direct-declarator ( identifier-list[opt] )
2163 pointer:
2164 * type-qualifier-list[opt]
2165 * type-qualifier-list[opt] pointer
2167 type-qualifier-list:
2168 type-qualifier
2169 attributes
2170 type-qualifier-list type-qualifier
2171 type-qualifier-list attributes
2173 parameter-type-list:
2174 parameter-list
2175 parameter-list , ...
2177 parameter-list:
2178 parameter-declaration
2179 parameter-list , parameter-declaration
2181 parameter-declaration:
2182 declaration-specifiers declarator attributes[opt]
2183 declaration-specifiers abstract-declarator[opt] attributes[opt]
2185 identifier-list:
2186 identifier
2187 identifier-list , identifier
2189 abstract-declarator:
2190 pointer
2191 pointer[opt] direct-abstract-declarator
2193 direct-abstract-declarator:
2194 ( attributes[opt] abstract-declarator )
2195 direct-abstract-declarator[opt] array-declarator
2196 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2198 GNU extensions:
2200 direct-declarator:
2201 direct-declarator ( parameter-forward-declarations
2202 parameter-type-list[opt] )
2204 direct-abstract-declarator:
2205 direct-abstract-declarator[opt] ( parameter-forward-declarations
2206 parameter-type-list[opt] )
2208 parameter-forward-declarations:
2209 parameter-list ;
2210 parameter-forward-declarations parameter-list ;
2212 The uses of attributes shown above are GNU extensions.
2214 Some forms of array declarator are not included in C99 in the
2215 syntax for abstract declarators; these are disallowed elsewhere.
2216 This may be a defect (DR#289).
2218 This function also accepts an omitted abstract declarator as being
2219 an abstract declarator, although not part of the formal syntax. */
2221 static struct c_declarator *
2222 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2223 bool *seen_id)
2225 /* Parse any initial pointer part. */
2226 if (c_parser_next_token_is (parser, CPP_MULT))
2228 struct c_declspecs *quals_attrs = build_null_declspecs ();
2229 struct c_declarator *inner;
2230 c_parser_consume_token (parser);
2231 c_parser_declspecs (parser, quals_attrs, false, false, true);
2232 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2233 if (inner == NULL)
2234 return NULL;
2235 else
2236 return make_pointer_declarator (quals_attrs, inner);
2238 /* Now we have a direct declarator, direct abstract declarator or
2239 nothing (which counts as a direct abstract declarator here). */
2240 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2243 /* Parse a direct declarator or direct abstract declarator; arguments
2244 as c_parser_declarator. */
2246 static struct c_declarator *
2247 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2248 bool *seen_id)
2250 /* The direct declarator must start with an identifier (possibly
2251 omitted) or a parenthesized declarator (possibly abstract). In
2252 an ordinary declarator, initial parentheses must start a
2253 parenthesized declarator. In an abstract declarator or parameter
2254 declarator, they could start a parenthesized declarator or a
2255 parameter list. To tell which, the open parenthesis and any
2256 following attributes must be read. If a declaration specifier
2257 follows, then it is a parameter list; if the specifier is a
2258 typedef name, there might be an ambiguity about redeclaring it,
2259 which is resolved in the direction of treating it as a typedef
2260 name. If a close parenthesis follows, it is also an empty
2261 parameter list, as the syntax does not permit empty abstract
2262 declarators. Otherwise, it is a parenthesized declarator (in
2263 which case the analysis may be repeated inside it, recursively).
2265 ??? There is an ambiguity in a parameter declaration "int
2266 (__attribute__((foo)) x)", where x is not a typedef name: it
2267 could be an abstract declarator for a function, or declare x with
2268 parentheses. The proper resolution of this ambiguity needs
2269 documenting. At present we follow an accident of the old
2270 parser's implementation, whereby the first parameter must have
2271 some declaration specifiers other than just attributes. Thus as
2272 a parameter declaration it is treated as a parenthesized
2273 parameter named x, and as an abstract declarator it is
2274 rejected.
2276 ??? Also following the old parser, attributes inside an empty
2277 parameter list are ignored, making it a list not yielding a
2278 prototype, rather than giving an error or making it have one
2279 parameter with implicit type int.
2281 ??? Also following the old parser, typedef names may be
2282 redeclared in declarators, but not Objective-C class names. */
2284 if (kind != C_DTR_ABSTRACT
2285 && c_parser_next_token_is (parser, CPP_NAME)
2286 && ((type_seen_p
2287 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2288 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2290 struct c_declarator *inner
2291 = build_id_declarator (c_parser_peek_token (parser)->value);
2292 *seen_id = true;
2293 inner->id_loc = c_parser_peek_token (parser)->location;
2294 c_parser_consume_token (parser);
2295 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2298 if (kind != C_DTR_NORMAL
2299 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2301 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2302 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2305 /* Either we are at the end of an abstract declarator, or we have
2306 parentheses. */
2308 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2310 tree attrs;
2311 struct c_declarator *inner;
2312 c_parser_consume_token (parser);
2313 attrs = c_parser_attributes (parser);
2314 if (kind != C_DTR_NORMAL
2315 && (c_parser_next_token_starts_declspecs (parser)
2316 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2318 struct c_arg_info *args
2319 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2320 attrs);
2321 if (args == NULL)
2322 return NULL;
2323 else
2325 inner
2326 = build_function_declarator (args,
2327 build_id_declarator (NULL_TREE));
2328 return c_parser_direct_declarator_inner (parser, *seen_id,
2329 inner);
2332 /* A parenthesized declarator. */
2333 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2334 if (inner != NULL && attrs != NULL)
2335 inner = build_attrs_declarator (attrs, inner);
2336 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2338 c_parser_consume_token (parser);
2339 if (inner == NULL)
2340 return NULL;
2341 else
2342 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2344 else
2346 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2347 "expected %<)%>");
2348 return NULL;
2351 else
2353 if (kind == C_DTR_NORMAL)
2355 c_parser_error (parser, "expected identifier or %<(%>");
2356 return NULL;
2358 else
2359 return build_id_declarator (NULL_TREE);
2363 /* Parse part of a direct declarator or direct abstract declarator,
2364 given that some (in INNER) has already been parsed; ID_PRESENT is
2365 true if an identifier is present, false for an abstract
2366 declarator. */
2368 static struct c_declarator *
2369 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2370 struct c_declarator *inner)
2372 /* Parse a sequence of array declarators and parameter lists. */
2373 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2375 location_t brace_loc = c_parser_peek_token (parser)->location;
2376 struct c_declarator *declarator;
2377 struct c_declspecs *quals_attrs = build_null_declspecs ();
2378 bool static_seen;
2379 bool star_seen;
2380 tree dimen;
2381 c_parser_consume_token (parser);
2382 c_parser_declspecs (parser, quals_attrs, false, false, true);
2383 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2384 if (static_seen)
2385 c_parser_consume_token (parser);
2386 if (static_seen && !quals_attrs->declspecs_seen_p)
2387 c_parser_declspecs (parser, quals_attrs, false, false, true);
2388 if (!quals_attrs->declspecs_seen_p)
2389 quals_attrs = NULL;
2390 /* If "static" is present, there must be an array dimension.
2391 Otherwise, there may be a dimension, "*", or no
2392 dimension. */
2393 if (static_seen)
2395 star_seen = false;
2396 dimen = c_parser_expr_no_commas (parser, NULL).value;
2398 else
2400 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2402 dimen = NULL_TREE;
2403 star_seen = false;
2405 else if (c_parser_next_token_is (parser, CPP_MULT))
2407 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2409 dimen = NULL_TREE;
2410 star_seen = true;
2411 c_parser_consume_token (parser);
2413 else
2415 star_seen = false;
2416 dimen = c_parser_expr_no_commas (parser, NULL).value;
2419 else
2421 star_seen = false;
2422 dimen = c_parser_expr_no_commas (parser, NULL).value;
2425 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2426 c_parser_consume_token (parser);
2427 else
2429 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2430 "expected %<]%>");
2431 return NULL;
2433 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2434 static_seen, star_seen);
2435 if (declarator == NULL)
2436 return NULL;
2437 inner = set_array_declarator_inner (declarator, inner);
2438 return c_parser_direct_declarator_inner (parser, id_present, inner);
2440 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2442 tree attrs;
2443 struct c_arg_info *args;
2444 c_parser_consume_token (parser);
2445 attrs = c_parser_attributes (parser);
2446 args = c_parser_parms_declarator (parser, id_present, attrs);
2447 if (args == NULL)
2448 return NULL;
2449 else
2451 inner = build_function_declarator (args, inner);
2452 return c_parser_direct_declarator_inner (parser, id_present, inner);
2455 return inner;
2458 /* Parse a parameter list or identifier list, including the closing
2459 parenthesis but not the opening one. ATTRS are the attributes at
2460 the start of the list. ID_LIST_OK is true if an identifier list is
2461 acceptable; such a list must not have attributes at the start. */
2463 static struct c_arg_info *
2464 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2466 push_scope ();
2467 declare_parm_level ();
2468 /* If the list starts with an identifier, it is an identifier list.
2469 Otherwise, it is either a prototype list or an empty list. */
2470 if (id_list_ok
2471 && !attrs
2472 && c_parser_next_token_is (parser, CPP_NAME)
2473 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2475 tree list = NULL_TREE, *nextp = &list;
2476 while (c_parser_next_token_is (parser, CPP_NAME)
2477 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2479 *nextp = build_tree_list (NULL_TREE,
2480 c_parser_peek_token (parser)->value);
2481 nextp = & TREE_CHAIN (*nextp);
2482 c_parser_consume_token (parser);
2483 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2484 break;
2485 c_parser_consume_token (parser);
2486 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2488 c_parser_error (parser, "expected identifier");
2489 break;
2492 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2494 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2495 ret->parms = 0;
2496 ret->tags = 0;
2497 ret->types = list;
2498 ret->others = 0;
2499 ret->pending_sizes = 0;
2500 ret->had_vla_unspec = 0;
2501 c_parser_consume_token (parser);
2502 pop_scope ();
2503 return ret;
2505 else
2507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2508 "expected %<)%>");
2509 pop_scope ();
2510 return NULL;
2513 else
2515 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2516 pop_scope ();
2517 return ret;
2521 /* Parse a parameter list (possibly empty), including the closing
2522 parenthesis but not the opening one. ATTRS are the attributes at
2523 the start of the list. */
2525 static struct c_arg_info *
2526 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2528 bool good_parm = false;
2529 /* ??? Following the old parser, forward parameter declarations may
2530 use abstract declarators, and if no real parameter declarations
2531 follow the forward declarations then this is not diagnosed. Also
2532 note as above that attributes are ignored as the only contents of
2533 the parentheses, or as the only contents after forward
2534 declarations. */
2535 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2537 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2538 ret->parms = 0;
2539 ret->tags = 0;
2540 ret->types = 0;
2541 ret->others = 0;
2542 ret->pending_sizes = 0;
2543 ret->had_vla_unspec = 0;
2544 c_parser_consume_token (parser);
2545 return ret;
2547 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2549 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2550 ret->parms = 0;
2551 ret->tags = 0;
2552 ret->others = 0;
2553 ret->pending_sizes = 0;
2554 ret->had_vla_unspec = 0;
2555 /* Suppress -Wold-style-definition for this case. */
2556 ret->types = error_mark_node;
2557 error_at (c_parser_peek_token (parser)->location,
2558 "ISO C requires a named argument before %<...%>");
2559 c_parser_consume_token (parser);
2560 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2562 c_parser_consume_token (parser);
2563 return ret;
2565 else
2567 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2568 "expected %<)%>");
2569 return NULL;
2572 /* Nonempty list of parameters, either terminated with semicolon
2573 (forward declarations; recurse) or with close parenthesis (normal
2574 function) or with ", ... )" (variadic function). */
2575 while (true)
2577 /* Parse a parameter. */
2578 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2579 attrs = NULL_TREE;
2580 if (parm != NULL)
2582 good_parm = true;
2583 push_parm_decl (parm);
2585 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2587 tree new_attrs;
2588 c_parser_consume_token (parser);
2589 mark_forward_parm_decls ();
2590 new_attrs = c_parser_attributes (parser);
2591 return c_parser_parms_list_declarator (parser, new_attrs);
2593 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2595 c_parser_consume_token (parser);
2596 if (good_parm)
2597 return get_parm_info (false);
2598 else
2600 struct c_arg_info *ret
2601 = XOBNEW (&parser_obstack, struct c_arg_info);
2602 ret->parms = 0;
2603 ret->tags = 0;
2604 ret->types = 0;
2605 ret->others = 0;
2606 ret->pending_sizes = 0;
2607 ret->had_vla_unspec = 0;
2608 return ret;
2611 if (!c_parser_require (parser, CPP_COMMA,
2612 "expected %<;%>, %<,%> or %<)%>"))
2614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2615 get_pending_sizes ();
2616 return NULL;
2618 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2620 c_parser_consume_token (parser);
2621 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2623 c_parser_consume_token (parser);
2624 if (good_parm)
2625 return get_parm_info (true);
2626 else
2628 struct c_arg_info *ret
2629 = XOBNEW (&parser_obstack, struct c_arg_info);
2630 ret->parms = 0;
2631 ret->tags = 0;
2632 ret->types = 0;
2633 ret->others = 0;
2634 ret->pending_sizes = 0;
2635 ret->had_vla_unspec = 0;
2636 return ret;
2639 else
2641 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2642 "expected %<)%>");
2643 get_pending_sizes ();
2644 return NULL;
2650 /* Parse a parameter declaration. ATTRS are the attributes at the
2651 start of the declaration if it is the first parameter. */
2653 static struct c_parm *
2654 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2656 struct c_declspecs *specs;
2657 struct c_declarator *declarator;
2658 tree prefix_attrs;
2659 tree postfix_attrs = NULL_TREE;
2660 bool dummy = false;
2661 if (!c_parser_next_token_starts_declspecs (parser))
2663 /* ??? In some Objective-C cases '...' isn't applicable so there
2664 should be a different message. */
2665 c_parser_error (parser,
2666 "expected declaration specifiers or %<...%>");
2667 c_parser_skip_to_end_of_parameter (parser);
2668 return NULL;
2670 specs = build_null_declspecs ();
2671 if (attrs)
2673 declspecs_add_attrs (specs, attrs);
2674 attrs = NULL_TREE;
2676 c_parser_declspecs (parser, specs, true, true, true);
2677 finish_declspecs (specs);
2678 pending_xref_error ();
2679 prefix_attrs = specs->attrs;
2680 specs->attrs = NULL_TREE;
2681 declarator = c_parser_declarator (parser, specs->type_seen_p,
2682 C_DTR_PARM, &dummy);
2683 if (declarator == NULL)
2685 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2686 return NULL;
2688 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2689 postfix_attrs = c_parser_attributes (parser);
2690 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2691 declarator);
2694 /* Parse a string literal in an asm expression. It should not be
2695 translated, and wide string literals are an error although
2696 permitted by the syntax. This is a GNU extension.
2698 asm-string-literal:
2699 string-literal
2701 ??? At present, following the old parser, the caller needs to have
2702 set lex_untranslated_string to 1. It would be better to follow the
2703 C++ parser rather than using this kludge. */
2705 static tree
2706 c_parser_asm_string_literal (c_parser *parser)
2708 tree str;
2709 if (c_parser_next_token_is (parser, CPP_STRING))
2711 str = c_parser_peek_token (parser)->value;
2712 c_parser_consume_token (parser);
2714 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2716 error_at (c_parser_peek_token (parser)->location,
2717 "wide string literal in %<asm%>");
2718 str = build_string (1, "");
2719 c_parser_consume_token (parser);
2721 else
2723 c_parser_error (parser, "expected string literal");
2724 str = NULL_TREE;
2726 return str;
2729 /* Parse a simple asm expression. This is used in restricted
2730 contexts, where a full expression with inputs and outputs does not
2731 make sense. This is a GNU extension.
2733 simple-asm-expr:
2734 asm ( asm-string-literal )
2737 static tree
2738 c_parser_simple_asm_expr (c_parser *parser)
2740 tree str;
2741 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2742 /* ??? Follow the C++ parser rather than using the
2743 lex_untranslated_string kludge. */
2744 parser->lex_untranslated_string = true;
2745 c_parser_consume_token (parser);
2746 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2748 parser->lex_untranslated_string = false;
2749 return NULL_TREE;
2751 str = c_parser_asm_string_literal (parser);
2752 parser->lex_untranslated_string = false;
2753 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2756 return NULL_TREE;
2758 return str;
2761 /* Parse (possibly empty) attributes. This is a GNU extension.
2763 attributes:
2764 empty
2765 attributes attribute
2767 attribute:
2768 __attribute__ ( ( attribute-list ) )
2770 attribute-list:
2771 attrib
2772 attribute_list , attrib
2774 attrib:
2775 empty
2776 any-word
2777 any-word ( identifier )
2778 any-word ( identifier , nonempty-expr-list )
2779 any-word ( expr-list )
2781 where the "identifier" must not be declared as a type, and
2782 "any-word" may be any identifier (including one declared as a
2783 type), a reserved word storage class specifier, type specifier or
2784 type qualifier. ??? This still leaves out most reserved keywords
2785 (following the old parser), shouldn't we include them, and why not
2786 allow identifiers declared as types to start the arguments? */
2788 static tree
2789 c_parser_attributes (c_parser *parser)
2791 tree attrs = NULL_TREE;
2792 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2794 /* ??? Follow the C++ parser rather than using the
2795 lex_untranslated_string kludge. */
2796 parser->lex_untranslated_string = true;
2797 c_parser_consume_token (parser);
2798 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2800 parser->lex_untranslated_string = false;
2801 return attrs;
2803 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2805 parser->lex_untranslated_string = false;
2806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2807 return attrs;
2809 /* Parse the attribute list. */
2810 while (c_parser_next_token_is (parser, CPP_COMMA)
2811 || c_parser_next_token_is (parser, CPP_NAME)
2812 || c_parser_next_token_is (parser, CPP_KEYWORD))
2814 tree attr, attr_name, attr_args;
2815 VEC(tree,gc) *expr_list;
2816 if (c_parser_next_token_is (parser, CPP_COMMA))
2818 c_parser_consume_token (parser);
2819 continue;
2821 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2823 /* ??? See comment above about what keywords are
2824 accepted here. */
2825 bool ok;
2826 switch (c_parser_peek_token (parser)->keyword)
2828 case RID_STATIC:
2829 case RID_UNSIGNED:
2830 case RID_LONG:
2831 case RID_CONST:
2832 case RID_EXTERN:
2833 case RID_REGISTER:
2834 case RID_TYPEDEF:
2835 case RID_SHORT:
2836 case RID_INLINE:
2837 case RID_VOLATILE:
2838 case RID_SIGNED:
2839 case RID_AUTO:
2840 case RID_RESTRICT:
2841 case RID_COMPLEX:
2842 case RID_THREAD:
2843 case RID_INT:
2844 case RID_CHAR:
2845 case RID_FLOAT:
2846 case RID_DOUBLE:
2847 case RID_VOID:
2848 case RID_DFLOAT32:
2849 case RID_DFLOAT64:
2850 case RID_DFLOAT128:
2851 case RID_BOOL:
2852 case RID_FRACT:
2853 case RID_ACCUM:
2854 case RID_SAT:
2855 ok = true;
2856 break;
2857 default:
2858 ok = false;
2859 break;
2861 if (!ok)
2862 break;
2863 /* Accept __attribute__((__const)) as __attribute__((const))
2864 etc. */
2865 attr_name
2866 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
2868 else
2869 attr_name = c_parser_peek_token (parser)->value;
2870 c_parser_consume_token (parser);
2871 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2873 attr = build_tree_list (attr_name, NULL_TREE);
2874 attrs = chainon (attrs, attr);
2875 continue;
2877 c_parser_consume_token (parser);
2878 /* Parse the attribute contents. If they start with an
2879 identifier which is followed by a comma or close
2880 parenthesis, then the arguments start with that
2881 identifier; otherwise they are an expression list. */
2882 if (c_parser_next_token_is (parser, CPP_NAME)
2883 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2884 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2885 || (c_parser_peek_2nd_token (parser)->type
2886 == CPP_CLOSE_PAREN)))
2888 tree arg1 = c_parser_peek_token (parser)->value;
2889 c_parser_consume_token (parser);
2890 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2891 attr_args = build_tree_list (NULL_TREE, arg1);
2892 else
2894 tree tree_list;
2895 c_parser_consume_token (parser);
2896 expr_list = c_parser_expr_list (parser, false, true, NULL);
2897 tree_list = build_tree_list_vec (expr_list);
2898 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
2899 release_tree_vector (expr_list);
2902 else
2904 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2905 attr_args = NULL_TREE;
2906 else
2908 expr_list = c_parser_expr_list (parser, false, true, NULL);
2909 attr_args = build_tree_list_vec (expr_list);
2910 release_tree_vector (expr_list);
2913 attr = build_tree_list (attr_name, attr_args);
2914 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2915 c_parser_consume_token (parser);
2916 else
2918 parser->lex_untranslated_string = false;
2919 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2920 "expected %<)%>");
2921 return attrs;
2923 attrs = chainon (attrs, attr);
2925 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2926 c_parser_consume_token (parser);
2927 else
2929 parser->lex_untranslated_string = false;
2930 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2931 "expected %<)%>");
2932 return attrs;
2934 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2935 c_parser_consume_token (parser);
2936 else
2938 parser->lex_untranslated_string = false;
2939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2940 "expected %<)%>");
2941 return attrs;
2943 parser->lex_untranslated_string = false;
2945 return attrs;
2948 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2950 type-name:
2951 specifier-qualifier-list abstract-declarator[opt]
2954 static struct c_type_name *
2955 c_parser_type_name (c_parser *parser)
2957 struct c_declspecs *specs = build_null_declspecs ();
2958 struct c_declarator *declarator;
2959 struct c_type_name *ret;
2960 bool dummy = false;
2961 c_parser_declspecs (parser, specs, false, true, true);
2962 if (!specs->declspecs_seen_p)
2964 c_parser_error (parser, "expected specifier-qualifier-list");
2965 return NULL;
2967 pending_xref_error ();
2968 finish_declspecs (specs);
2969 declarator = c_parser_declarator (parser, specs->type_seen_p,
2970 C_DTR_ABSTRACT, &dummy);
2971 if (declarator == NULL)
2972 return NULL;
2973 ret = XOBNEW (&parser_obstack, struct c_type_name);
2974 ret->specs = specs;
2975 ret->declarator = declarator;
2976 return ret;
2979 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2981 initializer:
2982 assignment-expression
2983 { initializer-list }
2984 { initializer-list , }
2986 initializer-list:
2987 designation[opt] initializer
2988 initializer-list , designation[opt] initializer
2990 designation:
2991 designator-list =
2993 designator-list:
2994 designator
2995 designator-list designator
2997 designator:
2998 array-designator
2999 . identifier
3001 array-designator:
3002 [ constant-expression ]
3004 GNU extensions:
3006 initializer:
3009 designation:
3010 array-designator
3011 identifier :
3013 array-designator:
3014 [ constant-expression ... constant-expression ]
3016 Any expression without commas is accepted in the syntax for the
3017 constant-expressions, with non-constant expressions rejected later.
3019 This function is only used for top-level initializers; for nested
3020 ones, see c_parser_initval. */
3022 static struct c_expr
3023 c_parser_initializer (c_parser *parser)
3025 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3026 return c_parser_braced_init (parser, NULL_TREE, false);
3027 else
3029 struct c_expr ret;
3030 location_t loc = c_parser_peek_token (parser)->location;
3031 ret = c_parser_expr_no_commas (parser, NULL);
3032 if (TREE_CODE (ret.value) != STRING_CST
3033 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3034 ret = default_function_array_conversion (loc, ret);
3035 return ret;
3039 /* Parse a braced initializer list. TYPE is the type specified for a
3040 compound literal, and NULL_TREE for other initializers and for
3041 nested braced lists. NESTED_P is true for nested braced lists,
3042 false for the list of a compound literal or the list that is the
3043 top-level initializer in a declaration. */
3045 static struct c_expr
3046 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3048 location_t brace_loc = c_parser_peek_token (parser)->location;
3049 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3050 c_parser_consume_token (parser);
3051 if (nested_p)
3052 push_init_level (0);
3053 else
3054 really_start_incremental_init (type);
3055 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3057 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3059 else
3061 /* Parse a non-empty initializer list, possibly with a trailing
3062 comma. */
3063 while (true)
3065 c_parser_initelt (parser);
3066 if (parser->error)
3067 break;
3068 if (c_parser_next_token_is (parser, CPP_COMMA))
3069 c_parser_consume_token (parser);
3070 else
3071 break;
3072 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3073 break;
3076 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3078 struct c_expr ret;
3079 ret.value = error_mark_node;
3080 ret.original_code = ERROR_MARK;
3081 ret.original_type = NULL;
3082 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3083 pop_init_level (0);
3084 return ret;
3086 c_parser_consume_token (parser);
3087 return pop_init_level (0);
3090 /* Parse a nested initializer, including designators. */
3092 static void
3093 c_parser_initelt (c_parser *parser)
3095 /* Parse any designator or designator list. A single array
3096 designator may have the subsequent "=" omitted in GNU C, but a
3097 longer list or a structure member designator may not. */
3098 if (c_parser_next_token_is (parser, CPP_NAME)
3099 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3101 /* Old-style structure member designator. */
3102 set_init_label (c_parser_peek_token (parser)->value);
3103 /* Use the colon as the error location. */
3104 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3105 "obsolete use of designated initializer with %<:%>");
3106 c_parser_consume_token (parser);
3107 c_parser_consume_token (parser);
3109 else
3111 /* des_seen is 0 if there have been no designators, 1 if there
3112 has been a single array designator and 2 otherwise. */
3113 int des_seen = 0;
3114 /* Location of a designator. */
3115 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3116 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3117 || c_parser_next_token_is (parser, CPP_DOT))
3119 int des_prev = des_seen;
3120 if (!des_seen)
3121 des_loc = c_parser_peek_token (parser)->location;
3122 if (des_seen < 2)
3123 des_seen++;
3124 if (c_parser_next_token_is (parser, CPP_DOT))
3126 des_seen = 2;
3127 c_parser_consume_token (parser);
3128 if (c_parser_next_token_is (parser, CPP_NAME))
3130 set_init_label (c_parser_peek_token (parser)->value);
3131 c_parser_consume_token (parser);
3133 else
3135 struct c_expr init;
3136 init.value = error_mark_node;
3137 init.original_code = ERROR_MARK;
3138 init.original_type = NULL;
3139 c_parser_error (parser, "expected identifier");
3140 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3141 process_init_element (init, false);
3142 return;
3145 else
3147 tree first, second;
3148 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3149 /* ??? Following the old parser, [ objc-receiver
3150 objc-message-args ] is accepted as an initializer,
3151 being distinguished from a designator by what follows
3152 the first assignment expression inside the square
3153 brackets, but after a first array designator a
3154 subsequent square bracket is for Objective-C taken to
3155 start an expression, using the obsolete form of
3156 designated initializer without '=', rather than
3157 possibly being a second level of designation: in LALR
3158 terms, the '[' is shifted rather than reducing
3159 designator to designator-list. */
3160 if (des_prev == 1 && c_dialect_objc ())
3162 des_seen = des_prev;
3163 break;
3165 if (des_prev == 0 && c_dialect_objc ())
3167 /* This might be an array designator or an
3168 Objective-C message expression. If the former,
3169 continue parsing here; if the latter, parse the
3170 remainder of the initializer given the starting
3171 primary-expression. ??? It might make sense to
3172 distinguish when des_prev == 1 as well; see
3173 previous comment. */
3174 tree rec, args;
3175 struct c_expr mexpr;
3176 c_parser_consume_token (parser);
3177 if (c_parser_peek_token (parser)->type == CPP_NAME
3178 && ((c_parser_peek_token (parser)->id_kind
3179 == C_ID_TYPENAME)
3180 || (c_parser_peek_token (parser)->id_kind
3181 == C_ID_CLASSNAME)))
3183 /* Type name receiver. */
3184 tree id = c_parser_peek_token (parser)->value;
3185 c_parser_consume_token (parser);
3186 rec = objc_get_class_reference (id);
3187 goto parse_message_args;
3189 first = c_parser_expr_no_commas (parser, NULL).value;
3190 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3191 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3192 goto array_desig_after_first;
3193 /* Expression receiver. So far only one part
3194 without commas has been parsed; there might be
3195 more of the expression. */
3196 rec = first;
3197 while (c_parser_next_token_is (parser, CPP_COMMA))
3199 struct c_expr next;
3200 location_t comma_loc, exp_loc;
3201 comma_loc = c_parser_peek_token (parser)->location;
3202 c_parser_consume_token (parser);
3203 exp_loc = c_parser_peek_token (parser)->location;
3204 next = c_parser_expr_no_commas (parser, NULL);
3205 next = default_function_array_conversion (exp_loc, next);
3206 rec = build_compound_expr (comma_loc, rec, next.value);
3208 parse_message_args:
3209 /* Now parse the objc-message-args. */
3210 args = c_parser_objc_message_args (parser);
3211 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3212 "expected %<]%>");
3213 mexpr.value
3214 = objc_build_message_expr (build_tree_list (rec, args));
3215 mexpr.original_code = ERROR_MARK;
3216 mexpr.original_type = NULL;
3217 /* Now parse and process the remainder of the
3218 initializer, starting with this message
3219 expression as a primary-expression. */
3220 c_parser_initval (parser, &mexpr);
3221 return;
3223 c_parser_consume_token (parser);
3224 first = c_parser_expr_no_commas (parser, NULL).value;
3225 array_desig_after_first:
3226 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3228 ellipsis_loc = c_parser_peek_token (parser)->location;
3229 c_parser_consume_token (parser);
3230 second = c_parser_expr_no_commas (parser, NULL).value;
3232 else
3233 second = NULL_TREE;
3234 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3236 c_parser_consume_token (parser);
3237 set_init_index (first, second);
3238 if (second)
3239 pedwarn (ellipsis_loc, OPT_pedantic,
3240 "ISO C forbids specifying range of elements to initialize");
3242 else
3243 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3244 "expected %<]%>");
3247 if (des_seen >= 1)
3249 if (c_parser_next_token_is (parser, CPP_EQ))
3251 if (!flag_isoc99)
3252 pedwarn (des_loc, OPT_pedantic,
3253 "ISO C90 forbids specifying subobject to initialize");
3254 c_parser_consume_token (parser);
3256 else
3258 if (des_seen == 1)
3259 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3260 "obsolete use of designated initializer without %<=%>");
3261 else
3263 struct c_expr init;
3264 init.value = error_mark_node;
3265 init.original_code = ERROR_MARK;
3266 init.original_type = NULL;
3267 c_parser_error (parser, "expected %<=%>");
3268 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3269 process_init_element (init, false);
3270 return;
3275 c_parser_initval (parser, NULL);
3278 /* Parse a nested initializer; as c_parser_initializer but parses
3279 initializers within braced lists, after any designators have been
3280 applied. If AFTER is not NULL then it is an Objective-C message
3281 expression which is the primary-expression starting the
3282 initializer. */
3284 static void
3285 c_parser_initval (c_parser *parser, struct c_expr *after)
3287 struct c_expr init;
3288 gcc_assert (!after || c_dialect_objc ());
3289 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3290 init = c_parser_braced_init (parser, NULL_TREE, true);
3291 else
3293 location_t loc = c_parser_peek_token (parser)->location;
3294 init = c_parser_expr_no_commas (parser, after);
3295 if (init.value != NULL_TREE
3296 && TREE_CODE (init.value) != STRING_CST
3297 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3298 init = default_function_array_conversion (loc, init);
3300 process_init_element (init, false);
3303 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3304 C99 6.8.2).
3306 compound-statement:
3307 { block-item-list[opt] }
3308 { label-declarations block-item-list }
3310 block-item-list:
3311 block-item
3312 block-item-list block-item
3314 block-item:
3315 nested-declaration
3316 statement
3318 nested-declaration:
3319 declaration
3321 GNU extensions:
3323 compound-statement:
3324 { label-declarations block-item-list }
3326 nested-declaration:
3327 __extension__ nested-declaration
3328 nested-function-definition
3330 label-declarations:
3331 label-declaration
3332 label-declarations label-declaration
3334 label-declaration:
3335 __label__ identifier-list ;
3337 Allowing the mixing of declarations and code is new in C99. The
3338 GNU syntax also permits (not shown above) labels at the end of
3339 compound statements, which yield an error. We don't allow labels
3340 on declarations; this might seem like a natural extension, but
3341 there would be a conflict between attributes on the label and
3342 prefix attributes on the declaration. ??? The syntax follows the
3343 old parser in requiring something after label declarations.
3344 Although they are erroneous if the labels declared aren't defined,
3345 is it useful for the syntax to be this way?
3347 OpenMP:
3349 block-item:
3350 openmp-directive
3352 openmp-directive:
3353 barrier-directive
3354 flush-directive */
3356 static tree
3357 c_parser_compound_statement (c_parser *parser)
3359 tree stmt;
3360 location_t brace_loc;
3361 brace_loc = c_parser_peek_token (parser)->location;
3362 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3364 /* Ensure a scope is entered and left anyway to avoid confusion
3365 if we have just prepared to enter a function body. */
3366 stmt = c_begin_compound_stmt (true);
3367 c_end_compound_stmt (brace_loc, stmt, true);
3368 return error_mark_node;
3370 stmt = c_begin_compound_stmt (true);
3371 c_parser_compound_statement_nostart (parser);
3372 return c_end_compound_stmt (brace_loc, stmt, true);
3375 /* Parse a compound statement except for the opening brace. This is
3376 used for parsing both compound statements and statement expressions
3377 (which follow different paths to handling the opening). */
3379 static void
3380 c_parser_compound_statement_nostart (c_parser *parser)
3382 bool last_stmt = false;
3383 bool last_label = false;
3384 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3385 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3386 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3388 c_parser_consume_token (parser);
3389 return;
3391 mark_valid_location_for_stdc_pragma (true);
3392 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3394 /* Read zero or more forward-declarations for labels that nested
3395 functions can jump to. */
3396 mark_valid_location_for_stdc_pragma (false);
3397 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3399 label_loc = c_parser_peek_token (parser)->location;
3400 c_parser_consume_token (parser);
3401 /* Any identifiers, including those declared as type names,
3402 are OK here. */
3403 while (true)
3405 tree label;
3406 if (c_parser_next_token_is_not (parser, CPP_NAME))
3408 c_parser_error (parser, "expected identifier");
3409 break;
3411 label
3412 = declare_label (c_parser_peek_token (parser)->value);
3413 C_DECLARED_LABEL_FLAG (label) = 1;
3414 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3415 c_parser_consume_token (parser);
3416 if (c_parser_next_token_is (parser, CPP_COMMA))
3417 c_parser_consume_token (parser);
3418 else
3419 break;
3421 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3423 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3425 /* We must now have at least one statement, label or declaration. */
3426 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3428 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3429 c_parser_error (parser, "expected declaration or statement");
3430 c_parser_consume_token (parser);
3431 return;
3433 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3435 location_t loc = c_parser_peek_token (parser)->location;
3436 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3437 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3438 || (c_parser_next_token_is (parser, CPP_NAME)
3439 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3441 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3442 label_loc = c_parser_peek_2nd_token (parser)->location;
3443 else
3444 label_loc = c_parser_peek_token (parser)->location;
3445 last_label = true;
3446 last_stmt = false;
3447 mark_valid_location_for_stdc_pragma (false);
3448 c_parser_label (parser);
3450 else if (!last_label
3451 && c_parser_next_token_starts_declspecs (parser))
3453 last_label = false;
3454 mark_valid_location_for_stdc_pragma (false);
3455 c_parser_declaration_or_fndef (parser, true, true, true, true);
3456 if (last_stmt)
3457 pedwarn_c90 (loc,
3458 (pedantic && !flag_isoc99)
3459 ? OPT_pedantic
3460 : OPT_Wdeclaration_after_statement,
3461 "ISO C90 forbids mixed declarations and code");
3462 last_stmt = false;
3464 else if (!last_label
3465 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3467 /* __extension__ can start a declaration, but is also an
3468 unary operator that can start an expression. Consume all
3469 but the last of a possible series of __extension__ to
3470 determine which. */
3471 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3472 && (c_parser_peek_2nd_token (parser)->keyword
3473 == RID_EXTENSION))
3474 c_parser_consume_token (parser);
3475 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3477 int ext;
3478 ext = disable_extension_diagnostics ();
3479 c_parser_consume_token (parser);
3480 last_label = false;
3481 mark_valid_location_for_stdc_pragma (false);
3482 c_parser_declaration_or_fndef (parser, true, true, true, true);
3483 /* Following the old parser, __extension__ does not
3484 disable this diagnostic. */
3485 restore_extension_diagnostics (ext);
3486 if (last_stmt)
3487 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3488 ? OPT_pedantic
3489 : OPT_Wdeclaration_after_statement,
3490 "ISO C90 forbids mixed declarations and code");
3491 last_stmt = false;
3493 else
3494 goto statement;
3496 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3498 /* External pragmas, and some omp pragmas, are not associated
3499 with regular c code, and so are not to be considered statements
3500 syntactically. This ensures that the user doesn't put them
3501 places that would turn into syntax errors if the directive
3502 were ignored. */
3503 if (c_parser_pragma (parser, pragma_compound))
3504 last_label = false, last_stmt = true;
3506 else if (c_parser_next_token_is (parser, CPP_EOF))
3508 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3509 c_parser_error (parser, "expected declaration or statement");
3510 return;
3512 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3514 if (parser->in_if_block)
3516 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3517 error_at (loc, """expected %<}%> before %<else%>");
3518 return;
3520 else
3522 error_at (loc, "%<else%> without a previous %<if%>");
3523 c_parser_consume_token (parser);
3524 continue;
3527 else
3529 statement:
3530 last_label = false;
3531 last_stmt = true;
3532 mark_valid_location_for_stdc_pragma (false);
3533 c_parser_statement_after_labels (parser);
3536 parser->error = false;
3538 if (last_label)
3539 error_at (label_loc, "label at end of compound statement");
3540 c_parser_consume_token (parser);
3541 /* Restore the value we started with. */
3542 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3545 /* Parse a label (C90 6.6.1, C99 6.8.1).
3547 label:
3548 identifier : attributes[opt]
3549 case constant-expression :
3550 default :
3552 GNU extensions:
3554 label:
3555 case constant-expression ... constant-expression :
3557 The use of attributes on labels is a GNU extension. The syntax in
3558 GNU C accepts any expressions without commas, non-constant
3559 expressions being rejected later. */
3561 static void
3562 c_parser_label (c_parser *parser)
3564 location_t loc1 = c_parser_peek_token (parser)->location;
3565 tree label = NULL_TREE;
3566 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3568 tree exp1, exp2;
3569 c_parser_consume_token (parser);
3570 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3571 if (c_parser_next_token_is (parser, CPP_COLON))
3573 c_parser_consume_token (parser);
3574 label = do_case (loc1, exp1, NULL_TREE);
3576 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3578 c_parser_consume_token (parser);
3579 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3580 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3581 label = do_case (loc1, exp1, exp2);
3583 else
3584 c_parser_error (parser, "expected %<:%> or %<...%>");
3586 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3588 c_parser_consume_token (parser);
3589 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3590 label = do_case (loc1, NULL_TREE, NULL_TREE);
3592 else
3594 tree name = c_parser_peek_token (parser)->value;
3595 tree tlab;
3596 tree attrs;
3597 location_t loc2 = c_parser_peek_token (parser)->location;
3598 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3599 c_parser_consume_token (parser);
3600 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3601 c_parser_consume_token (parser);
3602 attrs = c_parser_attributes (parser);
3603 tlab = define_label (loc2, name);
3604 if (tlab)
3606 decl_attributes (&tlab, attrs, 0);
3607 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
3610 if (label)
3612 if (c_parser_next_token_starts_declspecs (parser)
3613 && !(c_parser_next_token_is (parser, CPP_NAME)
3614 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3616 error_at (c_parser_peek_token (parser)->location,
3617 "a label can only be part of a statement and "
3618 "a declaration is not a statement");
3619 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3620 /*nested*/ true, /*empty_ok*/ false,
3621 /*start_attr_ok*/ true);
3626 /* Parse a statement (C90 6.6, C99 6.8).
3628 statement:
3629 labeled-statement
3630 compound-statement
3631 expression-statement
3632 selection-statement
3633 iteration-statement
3634 jump-statement
3636 labeled-statement:
3637 label statement
3639 expression-statement:
3640 expression[opt] ;
3642 selection-statement:
3643 if-statement
3644 switch-statement
3646 iteration-statement:
3647 while-statement
3648 do-statement
3649 for-statement
3651 jump-statement:
3652 goto identifier ;
3653 continue ;
3654 break ;
3655 return expression[opt] ;
3657 GNU extensions:
3659 statement:
3660 asm-statement
3662 jump-statement:
3663 goto * expression ;
3665 Objective-C:
3667 statement:
3668 objc-throw-statement
3669 objc-try-catch-statement
3670 objc-synchronized-statement
3672 objc-throw-statement:
3673 @throw expression ;
3674 @throw ;
3676 OpenMP:
3678 statement:
3679 openmp-construct
3681 openmp-construct:
3682 parallel-construct
3683 for-construct
3684 sections-construct
3685 single-construct
3686 parallel-for-construct
3687 parallel-sections-construct
3688 master-construct
3689 critical-construct
3690 atomic-construct
3691 ordered-construct
3693 parallel-construct:
3694 parallel-directive structured-block
3696 for-construct:
3697 for-directive iteration-statement
3699 sections-construct:
3700 sections-directive section-scope
3702 single-construct:
3703 single-directive structured-block
3705 parallel-for-construct:
3706 parallel-for-directive iteration-statement
3708 parallel-sections-construct:
3709 parallel-sections-directive section-scope
3711 master-construct:
3712 master-directive structured-block
3714 critical-construct:
3715 critical-directive structured-block
3717 atomic-construct:
3718 atomic-directive expression-statement
3720 ordered-construct:
3721 ordered-directive structured-block */
3723 static void
3724 c_parser_statement (c_parser *parser)
3726 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3727 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3728 || (c_parser_next_token_is (parser, CPP_NAME)
3729 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3730 c_parser_label (parser);
3731 c_parser_statement_after_labels (parser);
3734 /* Parse a statement, other than a labeled statement. */
3736 static void
3737 c_parser_statement_after_labels (c_parser *parser)
3739 location_t loc = c_parser_peek_token (parser)->location;
3740 tree stmt = NULL_TREE;
3741 bool in_if_block = parser->in_if_block;
3742 parser->in_if_block = false;
3743 switch (c_parser_peek_token (parser)->type)
3745 case CPP_OPEN_BRACE:
3746 add_stmt (c_parser_compound_statement (parser));
3747 break;
3748 case CPP_KEYWORD:
3749 switch (c_parser_peek_token (parser)->keyword)
3751 case RID_IF:
3752 c_parser_if_statement (parser);
3753 break;
3754 case RID_SWITCH:
3755 c_parser_switch_statement (parser);
3756 break;
3757 case RID_WHILE:
3758 c_parser_while_statement (parser);
3759 break;
3760 case RID_DO:
3761 c_parser_do_statement (parser);
3762 break;
3763 case RID_FOR:
3764 c_parser_for_statement (parser);
3765 break;
3766 case RID_GOTO:
3767 c_parser_consume_token (parser);
3768 if (c_parser_next_token_is (parser, CPP_NAME))
3770 stmt = c_finish_goto_label (loc,
3771 c_parser_peek_token (parser)->value);
3772 c_parser_consume_token (parser);
3774 else if (c_parser_next_token_is (parser, CPP_MULT))
3776 c_parser_consume_token (parser);
3777 stmt = c_finish_goto_ptr (loc,
3778 c_parser_expression (parser).value);
3780 else
3781 c_parser_error (parser, "expected identifier or %<*%>");
3782 goto expect_semicolon;
3783 case RID_CONTINUE:
3784 c_parser_consume_token (parser);
3785 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
3786 goto expect_semicolon;
3787 case RID_BREAK:
3788 c_parser_consume_token (parser);
3789 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
3790 goto expect_semicolon;
3791 case RID_RETURN:
3792 c_parser_consume_token (parser);
3793 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3795 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
3796 c_parser_consume_token (parser);
3798 else
3800 struct c_expr expr = c_parser_expression_conv (parser);
3801 stmt = c_finish_return (loc, expr.value, expr.original_type);
3802 goto expect_semicolon;
3804 break;
3805 case RID_ASM:
3806 stmt = c_parser_asm_statement (parser);
3807 break;
3808 case RID_THROW:
3809 gcc_assert (c_dialect_objc ());
3810 c_parser_consume_token (parser);
3811 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3813 stmt = objc_build_throw_stmt (loc, NULL_TREE);
3814 c_parser_consume_token (parser);
3816 else
3818 tree expr = c_parser_expression (parser).value;
3819 expr = c_fully_fold (expr, false, NULL);
3820 stmt = objc_build_throw_stmt (loc, expr);
3821 goto expect_semicolon;
3823 break;
3824 case RID_TRY:
3825 gcc_assert (c_dialect_objc ());
3826 c_parser_objc_try_catch_statement (parser);
3827 break;
3828 case RID_AT_SYNCHRONIZED:
3829 gcc_assert (c_dialect_objc ());
3830 c_parser_objc_synchronized_statement (parser);
3831 break;
3832 default:
3833 goto expr_stmt;
3835 break;
3836 case CPP_SEMICOLON:
3837 c_parser_consume_token (parser);
3838 break;
3839 case CPP_CLOSE_PAREN:
3840 case CPP_CLOSE_SQUARE:
3841 /* Avoid infinite loop in error recovery:
3842 c_parser_skip_until_found stops at a closing nesting
3843 delimiter without consuming it, but here we need to consume
3844 it to proceed further. */
3845 c_parser_error (parser, "expected statement");
3846 c_parser_consume_token (parser);
3847 break;
3848 case CPP_PRAGMA:
3849 c_parser_pragma (parser, pragma_stmt);
3850 break;
3851 default:
3852 expr_stmt:
3853 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
3854 expect_semicolon:
3855 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3856 break;
3858 /* Two cases cannot and do not have line numbers associated: If stmt
3859 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3860 cannot hold line numbers. But that's OK because the statement
3861 will either be changed to a MODIFY_EXPR during gimplification of
3862 the statement expr, or discarded. If stmt was compound, but
3863 without new variables, we will have skipped the creation of a
3864 BIND and will have a bare STATEMENT_LIST. But that's OK because
3865 (recursively) all of the component statements should already have
3866 line numbers assigned. ??? Can we discard no-op statements
3867 earlier? */
3868 if (CAN_HAVE_LOCATION_P (stmt)
3869 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
3870 SET_EXPR_LOCATION (stmt, loc);
3872 parser->in_if_block = in_if_block;
3875 /* Parse the condition from an if, do, while or for statements. */
3877 static tree
3878 c_parser_condition (c_parser *parser)
3880 location_t loc = c_parser_peek_token (parser)->location;
3881 tree cond;
3882 cond = c_parser_expression_conv (parser).value;
3883 cond = c_objc_common_truthvalue_conversion (loc, cond);
3884 cond = c_fully_fold (cond, false, NULL);
3885 if (warn_sequence_point)
3886 verify_sequence_points (cond);
3887 return cond;
3890 /* Parse a parenthesized condition from an if, do or while statement.
3892 condition:
3893 ( expression )
3895 static tree
3896 c_parser_paren_condition (c_parser *parser)
3898 tree cond;
3899 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3900 return error_mark_node;
3901 cond = c_parser_condition (parser);
3902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3903 return cond;
3906 /* Parse a statement which is a block in C99. */
3908 static tree
3909 c_parser_c99_block_statement (c_parser *parser)
3911 tree block = c_begin_compound_stmt (flag_isoc99);
3912 location_t loc = c_parser_peek_token (parser)->location;
3913 c_parser_statement (parser);
3914 return c_end_compound_stmt (loc, block, flag_isoc99);
3917 /* Parse the body of an if statement. This is just parsing a
3918 statement but (a) it is a block in C99, (b) we track whether the
3919 body is an if statement for the sake of -Wparentheses warnings, (c)
3920 we handle an empty body specially for the sake of -Wempty-body
3921 warnings, and (d) we call parser_compound_statement directly
3922 because c_parser_statement_after_labels resets
3923 parser->in_if_block. */
3925 static tree
3926 c_parser_if_body (c_parser *parser, bool *if_p)
3928 tree block = c_begin_compound_stmt (flag_isoc99);
3929 location_t body_loc = c_parser_peek_token (parser)->location;
3930 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3931 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3932 || (c_parser_next_token_is (parser, CPP_NAME)
3933 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3934 c_parser_label (parser);
3935 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3936 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3938 location_t loc = c_parser_peek_token (parser)->location;
3939 add_stmt (build_empty_stmt (loc));
3940 c_parser_consume_token (parser);
3941 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
3942 warning_at (loc, OPT_Wempty_body,
3943 "suggest braces around empty body in an %<if%> statement");
3945 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3946 add_stmt (c_parser_compound_statement (parser));
3947 else
3948 c_parser_statement_after_labels (parser);
3949 return c_end_compound_stmt (body_loc, block, flag_isoc99);
3952 /* Parse the else body of an if statement. This is just parsing a
3953 statement but (a) it is a block in C99, (b) we handle an empty body
3954 specially for the sake of -Wempty-body warnings. */
3956 static tree
3957 c_parser_else_body (c_parser *parser)
3959 location_t else_loc = c_parser_peek_token (parser)->location;
3960 tree block = c_begin_compound_stmt (flag_isoc99);
3961 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3962 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3963 || (c_parser_next_token_is (parser, CPP_NAME)
3964 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3965 c_parser_label (parser);
3966 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3968 location_t loc = c_parser_peek_token (parser)->location;
3969 warning_at (loc,
3970 OPT_Wempty_body,
3971 "suggest braces around empty body in an %<else%> statement");
3972 add_stmt (build_empty_stmt (loc));
3973 c_parser_consume_token (parser);
3975 else
3976 c_parser_statement_after_labels (parser);
3977 return c_end_compound_stmt (else_loc, block, flag_isoc99);
3980 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3982 if-statement:
3983 if ( expression ) statement
3984 if ( expression ) statement else statement
3987 static void
3988 c_parser_if_statement (c_parser *parser)
3990 tree block;
3991 location_t loc;
3992 tree cond;
3993 bool first_if = false;
3994 tree first_body, second_body;
3995 bool in_if_block;
3997 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3998 c_parser_consume_token (parser);
3999 block = c_begin_compound_stmt (flag_isoc99);
4000 loc = c_parser_peek_token (parser)->location;
4001 cond = c_parser_paren_condition (parser);
4002 in_if_block = parser->in_if_block;
4003 parser->in_if_block = true;
4004 first_body = c_parser_if_body (parser, &first_if);
4005 parser->in_if_block = in_if_block;
4006 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4008 c_parser_consume_token (parser);
4009 second_body = c_parser_else_body (parser);
4011 else
4012 second_body = NULL_TREE;
4013 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4014 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4017 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4019 switch-statement:
4020 switch (expression) statement
4023 static void
4024 c_parser_switch_statement (c_parser *parser)
4026 tree block, expr, body, save_break;
4027 location_t switch_loc = c_parser_peek_token (parser)->location;
4028 location_t switch_cond_loc;
4029 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4030 c_parser_consume_token (parser);
4031 block = c_begin_compound_stmt (flag_isoc99);
4032 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4034 switch_cond_loc = c_parser_peek_token (parser)->location;
4035 expr = c_parser_expression (parser).value;
4036 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4038 else
4040 switch_cond_loc = UNKNOWN_LOCATION;
4041 expr = error_mark_node;
4043 c_start_case (switch_loc, switch_cond_loc, expr);
4044 save_break = c_break_label;
4045 c_break_label = NULL_TREE;
4046 body = c_parser_c99_block_statement (parser);
4047 c_finish_case (body);
4048 if (c_break_label)
4050 location_t here = c_parser_peek_token (parser)->location;
4051 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4052 SET_EXPR_LOCATION (t, here);
4053 add_stmt (t);
4055 c_break_label = save_break;
4056 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4059 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4061 while-statement:
4062 while (expression) statement
4065 static void
4066 c_parser_while_statement (c_parser *parser)
4068 tree block, cond, body, save_break, save_cont;
4069 location_t loc;
4070 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4071 c_parser_consume_token (parser);
4072 block = c_begin_compound_stmt (flag_isoc99);
4073 loc = c_parser_peek_token (parser)->location;
4074 cond = c_parser_paren_condition (parser);
4075 save_break = c_break_label;
4076 c_break_label = NULL_TREE;
4077 save_cont = c_cont_label;
4078 c_cont_label = NULL_TREE;
4079 body = c_parser_c99_block_statement (parser);
4080 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4081 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4082 c_break_label = save_break;
4083 c_cont_label = save_cont;
4086 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4088 do-statement:
4089 do statement while ( expression ) ;
4092 static void
4093 c_parser_do_statement (c_parser *parser)
4095 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4096 location_t loc;
4097 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4098 c_parser_consume_token (parser);
4099 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4100 warning_at (c_parser_peek_token (parser)->location,
4101 OPT_Wempty_body,
4102 "suggest braces around empty body in %<do%> statement");
4103 block = c_begin_compound_stmt (flag_isoc99);
4104 loc = c_parser_peek_token (parser)->location;
4105 save_break = c_break_label;
4106 c_break_label = NULL_TREE;
4107 save_cont = c_cont_label;
4108 c_cont_label = NULL_TREE;
4109 body = c_parser_c99_block_statement (parser);
4110 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4111 new_break = c_break_label;
4112 c_break_label = save_break;
4113 new_cont = c_cont_label;
4114 c_cont_label = save_cont;
4115 cond = c_parser_paren_condition (parser);
4116 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4117 c_parser_skip_to_end_of_block_or_statement (parser);
4118 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4119 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4122 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4124 for-statement:
4125 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4126 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4128 The form with a declaration is new in C99.
4130 ??? In accordance with the old parser, the declaration may be a
4131 nested function, which is then rejected in check_for_loop_decls,
4132 but does it make any sense for this to be included in the grammar?
4133 Note in particular that the nested function does not include a
4134 trailing ';', whereas the "declaration" production includes one.
4135 Also, can we reject bad declarations earlier and cheaper than
4136 check_for_loop_decls? */
4138 static void
4139 c_parser_for_statement (c_parser *parser)
4141 tree block, cond, incr, save_break, save_cont, body;
4142 location_t loc = c_parser_peek_token (parser)->location;
4143 location_t for_loc = c_parser_peek_token (parser)->location;
4144 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4145 c_parser_consume_token (parser);
4146 block = c_begin_compound_stmt (flag_isoc99);
4147 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4149 /* Parse the initialization declaration or expression. */
4150 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4152 c_parser_consume_token (parser);
4153 c_finish_expr_stmt (loc, NULL_TREE);
4155 else if (c_parser_next_token_starts_declspecs (parser))
4157 c_parser_declaration_or_fndef (parser, true, true, true, true);
4158 check_for_loop_decls (for_loc);
4160 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4162 /* __extension__ can start a declaration, but is also an
4163 unary operator that can start an expression. Consume all
4164 but the last of a possible series of __extension__ to
4165 determine which. */
4166 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4167 && (c_parser_peek_2nd_token (parser)->keyword
4168 == RID_EXTENSION))
4169 c_parser_consume_token (parser);
4170 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4172 int ext;
4173 ext = disable_extension_diagnostics ();
4174 c_parser_consume_token (parser);
4175 c_parser_declaration_or_fndef (parser, true, true, true, true);
4176 restore_extension_diagnostics (ext);
4177 check_for_loop_decls (for_loc);
4179 else
4180 goto init_expr;
4182 else
4184 init_expr:
4185 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
4186 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4188 /* Parse the loop condition. */
4189 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4191 c_parser_consume_token (parser);
4192 cond = NULL_TREE;
4194 else
4196 cond = c_parser_condition (parser);
4197 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4199 /* Parse the increment expression. */
4200 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4201 incr = c_process_expr_stmt (loc, NULL_TREE);
4202 else
4203 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4204 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4206 else
4208 cond = error_mark_node;
4209 incr = error_mark_node;
4211 save_break = c_break_label;
4212 c_break_label = NULL_TREE;
4213 save_cont = c_cont_label;
4214 c_cont_label = NULL_TREE;
4215 body = c_parser_c99_block_statement (parser);
4216 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4217 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4218 c_break_label = save_break;
4219 c_cont_label = save_cont;
4222 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4223 statement with inputs, outputs, clobbers, and volatile tag
4224 allowed.
4226 asm-statement:
4227 asm type-qualifier[opt] ( asm-argument ) ;
4229 asm-argument:
4230 asm-string-literal
4231 asm-string-literal : asm-operands[opt]
4232 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4233 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4235 Qualifiers other than volatile are accepted in the syntax but
4236 warned for. */
4238 static tree
4239 c_parser_asm_statement (c_parser *parser)
4241 tree quals, str, outputs, inputs, clobbers, ret;
4242 bool simple;
4243 location_t asm_loc = c_parser_peek_token (parser)->location;
4244 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4245 c_parser_consume_token (parser);
4246 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4248 quals = c_parser_peek_token (parser)->value;
4249 c_parser_consume_token (parser);
4251 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4252 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4254 warning_at (c_parser_peek_token (parser)->location,
4256 "%E qualifier ignored on asm",
4257 c_parser_peek_token (parser)->value);
4258 quals = NULL_TREE;
4259 c_parser_consume_token (parser);
4261 else
4262 quals = NULL_TREE;
4263 /* ??? Follow the C++ parser rather than using the
4264 lex_untranslated_string kludge. */
4265 parser->lex_untranslated_string = true;
4266 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4268 parser->lex_untranslated_string = false;
4269 return NULL_TREE;
4271 str = c_parser_asm_string_literal (parser);
4272 if (str == NULL_TREE)
4274 parser->lex_untranslated_string = false;
4275 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4276 return NULL_TREE;
4278 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4280 simple = true;
4281 outputs = NULL_TREE;
4282 inputs = NULL_TREE;
4283 clobbers = NULL_TREE;
4284 goto done_asm;
4286 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4288 parser->lex_untranslated_string = false;
4289 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4290 return NULL_TREE;
4292 simple = false;
4293 /* Parse outputs. */
4294 if (c_parser_next_token_is (parser, CPP_COLON)
4295 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4296 outputs = NULL_TREE;
4297 else
4298 outputs = c_parser_asm_operands (parser, false);
4299 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4301 inputs = NULL_TREE;
4302 clobbers = NULL_TREE;
4303 goto done_asm;
4305 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4307 parser->lex_untranslated_string = false;
4308 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4309 return NULL_TREE;
4311 /* Parse inputs. */
4312 if (c_parser_next_token_is (parser, CPP_COLON)
4313 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4314 inputs = NULL_TREE;
4315 else
4316 inputs = c_parser_asm_operands (parser, true);
4317 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4319 clobbers = NULL_TREE;
4320 goto done_asm;
4322 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4324 parser->lex_untranslated_string = false;
4325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4326 return NULL_TREE;
4328 /* Parse clobbers. */
4329 clobbers = c_parser_asm_clobbers (parser);
4330 done_asm:
4331 parser->lex_untranslated_string = false;
4332 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4334 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4335 return NULL_TREE;
4337 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4338 c_parser_skip_to_end_of_block_or_statement (parser);
4339 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4340 clobbers, simple));
4341 return ret;
4344 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4345 not outputs), apply the default conversion of functions and arrays
4346 to pointers.
4348 asm-operands:
4349 asm-operand
4350 asm-operands , asm-operand
4352 asm-operand:
4353 asm-string-literal ( expression )
4354 [ identifier ] asm-string-literal ( expression )
4357 static tree
4358 c_parser_asm_operands (c_parser *parser, bool convert_p)
4360 tree list = NULL_TREE;
4361 location_t loc;
4362 while (true)
4364 tree name, str;
4365 struct c_expr expr;
4366 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4368 c_parser_consume_token (parser);
4369 if (c_parser_next_token_is (parser, CPP_NAME))
4371 tree id = c_parser_peek_token (parser)->value;
4372 c_parser_consume_token (parser);
4373 name = build_string (IDENTIFIER_LENGTH (id),
4374 IDENTIFIER_POINTER (id));
4376 else
4378 c_parser_error (parser, "expected identifier");
4379 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4380 return NULL_TREE;
4382 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4383 "expected %<]%>");
4385 else
4386 name = NULL_TREE;
4387 str = c_parser_asm_string_literal (parser);
4388 if (str == NULL_TREE)
4389 return NULL_TREE;
4390 parser->lex_untranslated_string = false;
4391 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4393 parser->lex_untranslated_string = true;
4394 return NULL_TREE;
4396 loc = c_parser_peek_token (parser)->location;
4397 expr = c_parser_expression (parser);
4398 if (convert_p)
4399 expr = default_function_array_conversion (loc, expr);
4400 expr.value = c_fully_fold (expr.value, false, NULL);
4401 parser->lex_untranslated_string = true;
4402 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4404 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4405 return NULL_TREE;
4407 list = chainon (list, build_tree_list (build_tree_list (name, str),
4408 expr.value));
4409 if (c_parser_next_token_is (parser, CPP_COMMA))
4410 c_parser_consume_token (parser);
4411 else
4412 break;
4414 return list;
4417 /* Parse asm clobbers, a GNU extension.
4419 asm-clobbers:
4420 asm-string-literal
4421 asm-clobbers , asm-string-literal
4424 static tree
4425 c_parser_asm_clobbers (c_parser *parser)
4427 tree list = NULL_TREE;
4428 while (true)
4430 tree str = c_parser_asm_string_literal (parser);
4431 if (str)
4432 list = tree_cons (NULL_TREE, str, list);
4433 else
4434 return NULL_TREE;
4435 if (c_parser_next_token_is (parser, CPP_COMMA))
4436 c_parser_consume_token (parser);
4437 else
4438 break;
4440 return list;
4443 /* Parse an expression other than a compound expression; that is, an
4444 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4445 NULL then it is an Objective-C message expression which is the
4446 primary-expression starting the expression as an initializer.
4448 assignment-expression:
4449 conditional-expression
4450 unary-expression assignment-operator assignment-expression
4452 assignment-operator: one of
4453 = *= /= %= += -= <<= >>= &= ^= |=
4455 In GNU C we accept any conditional expression on the LHS and
4456 diagnose the invalid lvalue rather than producing a syntax
4457 error. */
4459 static struct c_expr
4460 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4462 struct c_expr lhs, rhs, ret;
4463 enum tree_code code;
4464 location_t op_location, exp_location;
4465 gcc_assert (!after || c_dialect_objc ());
4466 lhs = c_parser_conditional_expression (parser, after);
4467 op_location = c_parser_peek_token (parser)->location;
4468 switch (c_parser_peek_token (parser)->type)
4470 case CPP_EQ:
4471 code = NOP_EXPR;
4472 break;
4473 case CPP_MULT_EQ:
4474 code = MULT_EXPR;
4475 break;
4476 case CPP_DIV_EQ:
4477 code = TRUNC_DIV_EXPR;
4478 break;
4479 case CPP_MOD_EQ:
4480 code = TRUNC_MOD_EXPR;
4481 break;
4482 case CPP_PLUS_EQ:
4483 code = PLUS_EXPR;
4484 break;
4485 case CPP_MINUS_EQ:
4486 code = MINUS_EXPR;
4487 break;
4488 case CPP_LSHIFT_EQ:
4489 code = LSHIFT_EXPR;
4490 break;
4491 case CPP_RSHIFT_EQ:
4492 code = RSHIFT_EXPR;
4493 break;
4494 case CPP_AND_EQ:
4495 code = BIT_AND_EXPR;
4496 break;
4497 case CPP_XOR_EQ:
4498 code = BIT_XOR_EXPR;
4499 break;
4500 case CPP_OR_EQ:
4501 code = BIT_IOR_EXPR;
4502 break;
4503 default:
4504 return lhs;
4506 c_parser_consume_token (parser);
4507 exp_location = c_parser_peek_token (parser)->location;
4508 rhs = c_parser_expr_no_commas (parser, NULL);
4509 rhs = default_function_array_conversion (exp_location, rhs);
4510 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
4511 code, exp_location, rhs.value,
4512 rhs.original_type);
4513 if (code == NOP_EXPR)
4514 ret.original_code = MODIFY_EXPR;
4515 else
4517 TREE_NO_WARNING (ret.value) = 1;
4518 ret.original_code = ERROR_MARK;
4520 ret.original_type = NULL;
4521 return ret;
4524 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4525 is not NULL then it is an Objective-C message expression which is
4526 the primary-expression starting the expression as an initializer.
4528 conditional-expression:
4529 logical-OR-expression
4530 logical-OR-expression ? expression : conditional-expression
4532 GNU extensions:
4534 conditional-expression:
4535 logical-OR-expression ? : conditional-expression
4538 static struct c_expr
4539 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4541 struct c_expr cond, exp1, exp2, ret;
4542 location_t cond_loc, colon_loc;
4544 gcc_assert (!after || c_dialect_objc ());
4546 cond = c_parser_binary_expression (parser, after);
4548 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4549 return cond;
4550 cond_loc = c_parser_peek_token (parser)->location;
4551 cond = default_function_array_conversion (cond_loc, cond);
4552 c_parser_consume_token (parser);
4553 if (c_parser_next_token_is (parser, CPP_COLON))
4555 tree eptype = NULL_TREE;
4556 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4557 "ISO C forbids omitting the middle term of a ?: expression");
4558 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4560 eptype = TREE_TYPE (cond.value);
4561 cond.value = TREE_OPERAND (cond.value, 0);
4563 /* Make sure first operand is calculated only once. */
4564 exp1.value = c_save_expr (default_conversion (cond.value));
4565 if (eptype)
4566 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
4567 exp1.original_type = NULL;
4568 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4569 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
4571 else
4573 cond.value
4574 = c_objc_common_truthvalue_conversion
4575 (cond_loc, default_conversion (cond.value));
4576 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
4577 exp1 = c_parser_expression_conv (parser);
4578 c_inhibit_evaluation_warnings +=
4579 ((cond.value == truthvalue_true_node)
4580 - (cond.value == truthvalue_false_node));
4583 colon_loc = c_parser_peek_token (parser)->location;
4584 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4586 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4587 ret.value = error_mark_node;
4588 ret.original_code = ERROR_MARK;
4589 ret.original_type = NULL;
4590 return ret;
4593 location_t exp2_loc = c_parser_peek_token (parser)->location;
4594 exp2 = c_parser_conditional_expression (parser, NULL);
4595 exp2 = default_function_array_conversion (exp2_loc, exp2);
4597 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4598 ret.value = build_conditional_expr (colon_loc, cond.value,
4599 cond.original_code == C_MAYBE_CONST_EXPR,
4600 exp1.value, exp1.original_type,
4601 exp2.value, exp2.original_type);
4602 ret.original_code = ERROR_MARK;
4603 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4604 ret.original_type = NULL;
4605 else
4607 tree t1, t2;
4609 /* If both sides are enum type, the default conversion will have
4610 made the type of the result be an integer type. We want to
4611 remember the enum types we started with. */
4612 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4613 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4614 ret.original_type = ((t1 != error_mark_node
4615 && t2 != error_mark_node
4616 && (TYPE_MAIN_VARIANT (t1)
4617 == TYPE_MAIN_VARIANT (t2)))
4618 ? t1
4619 : NULL);
4621 return ret;
4624 /* Parse a binary expression; that is, a logical-OR-expression (C90
4625 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4626 an Objective-C message expression which is the primary-expression
4627 starting the expression as an initializer.
4629 multiplicative-expression:
4630 cast-expression
4631 multiplicative-expression * cast-expression
4632 multiplicative-expression / cast-expression
4633 multiplicative-expression % cast-expression
4635 additive-expression:
4636 multiplicative-expression
4637 additive-expression + multiplicative-expression
4638 additive-expression - multiplicative-expression
4640 shift-expression:
4641 additive-expression
4642 shift-expression << additive-expression
4643 shift-expression >> additive-expression
4645 relational-expression:
4646 shift-expression
4647 relational-expression < shift-expression
4648 relational-expression > shift-expression
4649 relational-expression <= shift-expression
4650 relational-expression >= shift-expression
4652 equality-expression:
4653 relational-expression
4654 equality-expression == relational-expression
4655 equality-expression != relational-expression
4657 AND-expression:
4658 equality-expression
4659 AND-expression & equality-expression
4661 exclusive-OR-expression:
4662 AND-expression
4663 exclusive-OR-expression ^ AND-expression
4665 inclusive-OR-expression:
4666 exclusive-OR-expression
4667 inclusive-OR-expression | exclusive-OR-expression
4669 logical-AND-expression:
4670 inclusive-OR-expression
4671 logical-AND-expression && inclusive-OR-expression
4673 logical-OR-expression:
4674 logical-AND-expression
4675 logical-OR-expression || logical-AND-expression
4678 static struct c_expr
4679 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4681 /* A binary expression is parsed using operator-precedence parsing,
4682 with the operands being cast expressions. All the binary
4683 operators are left-associative. Thus a binary expression is of
4684 form:
4686 E0 op1 E1 op2 E2 ...
4688 which we represent on a stack. On the stack, the precedence
4689 levels are strictly increasing. When a new operator is
4690 encountered of higher precedence than that at the top of the
4691 stack, it is pushed; its LHS is the top expression, and its RHS
4692 is everything parsed until it is popped. When a new operator is
4693 encountered with precedence less than or equal to that at the top
4694 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4695 by the result of the operation until the operator at the top of
4696 the stack has lower precedence than the new operator or there is
4697 only one element on the stack; then the top expression is the LHS
4698 of the new operator. In the case of logical AND and OR
4699 expressions, we also need to adjust c_inhibit_evaluation_warnings
4700 as appropriate when the operators are pushed and popped. */
4702 /* The precedence levels, where 0 is a dummy lowest level used for
4703 the bottom of the stack. */
4704 enum prec {
4705 PREC_NONE,
4706 PREC_LOGOR,
4707 PREC_LOGAND,
4708 PREC_BITOR,
4709 PREC_BITXOR,
4710 PREC_BITAND,
4711 PREC_EQ,
4712 PREC_REL,
4713 PREC_SHIFT,
4714 PREC_ADD,
4715 PREC_MULT,
4716 NUM_PRECS
4718 struct {
4719 /* The expression at this stack level. */
4720 struct c_expr expr;
4721 /* The precedence of the operator on its left, PREC_NONE at the
4722 bottom of the stack. */
4723 enum prec prec;
4724 /* The operation on its left. */
4725 enum tree_code op;
4726 /* The source location of this operation. */
4727 location_t loc;
4728 } stack[NUM_PRECS];
4729 int sp;
4730 /* Location of the binary operator. */
4731 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4732 #define POP \
4733 do { \
4734 switch (stack[sp].op) \
4736 case TRUTH_ANDIF_EXPR: \
4737 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4738 == truthvalue_false_node); \
4739 break; \
4740 case TRUTH_ORIF_EXPR: \
4741 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4742 == truthvalue_true_node); \
4743 break; \
4744 default: \
4745 break; \
4747 stack[sp - 1].expr \
4748 = default_function_array_conversion (stack[sp - 1].loc, \
4749 stack[sp - 1].expr); \
4750 stack[sp].expr \
4751 = default_function_array_conversion (stack[sp].loc, stack[sp].expr); \
4752 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
4753 stack[sp].op, \
4754 stack[sp - 1].expr, \
4755 stack[sp].expr); \
4756 sp--; \
4757 } while (0)
4758 gcc_assert (!after || c_dialect_objc ());
4759 stack[0].loc = c_parser_peek_token (parser)->location;
4760 stack[0].expr = c_parser_cast_expression (parser, after);
4761 stack[0].prec = PREC_NONE;
4762 sp = 0;
4763 while (true)
4765 enum prec oprec;
4766 enum tree_code ocode;
4767 if (parser->error)
4768 goto out;
4769 switch (c_parser_peek_token (parser)->type)
4771 case CPP_MULT:
4772 oprec = PREC_MULT;
4773 ocode = MULT_EXPR;
4774 break;
4775 case CPP_DIV:
4776 oprec = PREC_MULT;
4777 ocode = TRUNC_DIV_EXPR;
4778 break;
4779 case CPP_MOD:
4780 oprec = PREC_MULT;
4781 ocode = TRUNC_MOD_EXPR;
4782 break;
4783 case CPP_PLUS:
4784 oprec = PREC_ADD;
4785 ocode = PLUS_EXPR;
4786 break;
4787 case CPP_MINUS:
4788 oprec = PREC_ADD;
4789 ocode = MINUS_EXPR;
4790 break;
4791 case CPP_LSHIFT:
4792 oprec = PREC_SHIFT;
4793 ocode = LSHIFT_EXPR;
4794 break;
4795 case CPP_RSHIFT:
4796 oprec = PREC_SHIFT;
4797 ocode = RSHIFT_EXPR;
4798 break;
4799 case CPP_LESS:
4800 oprec = PREC_REL;
4801 ocode = LT_EXPR;
4802 break;
4803 case CPP_GREATER:
4804 oprec = PREC_REL;
4805 ocode = GT_EXPR;
4806 break;
4807 case CPP_LESS_EQ:
4808 oprec = PREC_REL;
4809 ocode = LE_EXPR;
4810 break;
4811 case CPP_GREATER_EQ:
4812 oprec = PREC_REL;
4813 ocode = GE_EXPR;
4814 break;
4815 case CPP_EQ_EQ:
4816 oprec = PREC_EQ;
4817 ocode = EQ_EXPR;
4818 break;
4819 case CPP_NOT_EQ:
4820 oprec = PREC_EQ;
4821 ocode = NE_EXPR;
4822 break;
4823 case CPP_AND:
4824 oprec = PREC_BITAND;
4825 ocode = BIT_AND_EXPR;
4826 break;
4827 case CPP_XOR:
4828 oprec = PREC_BITXOR;
4829 ocode = BIT_XOR_EXPR;
4830 break;
4831 case CPP_OR:
4832 oprec = PREC_BITOR;
4833 ocode = BIT_IOR_EXPR;
4834 break;
4835 case CPP_AND_AND:
4836 oprec = PREC_LOGAND;
4837 ocode = TRUTH_ANDIF_EXPR;
4838 break;
4839 case CPP_OR_OR:
4840 oprec = PREC_LOGOR;
4841 ocode = TRUTH_ORIF_EXPR;
4842 break;
4843 default:
4844 /* Not a binary operator, so end of the binary
4845 expression. */
4846 goto out;
4848 binary_loc = c_parser_peek_token (parser)->location;
4849 c_parser_consume_token (parser);
4850 while (oprec <= stack[sp].prec)
4851 POP;
4852 switch (ocode)
4854 case TRUTH_ANDIF_EXPR:
4855 stack[sp].expr
4856 = default_function_array_conversion (stack[sp].loc,
4857 stack[sp].expr);
4858 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4859 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4860 c_inhibit_evaluation_warnings += (stack[sp].expr.value
4861 == truthvalue_false_node);
4862 break;
4863 case TRUTH_ORIF_EXPR:
4864 stack[sp].expr
4865 = default_function_array_conversion (stack[sp].loc,
4866 stack[sp].expr);
4867 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4868 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4869 c_inhibit_evaluation_warnings += (stack[sp].expr.value
4870 == truthvalue_true_node);
4871 break;
4872 default:
4873 break;
4875 sp++;
4876 stack[sp].loc = binary_loc;
4877 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4878 stack[sp].prec = oprec;
4879 stack[sp].op = ocode;
4880 stack[sp].loc = binary_loc;
4882 out:
4883 while (sp > 0)
4884 POP;
4885 return stack[0].expr;
4886 #undef POP
4889 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4890 NULL then it is an Objective-C message expression which is the
4891 primary-expression starting the expression as an initializer.
4893 cast-expression:
4894 unary-expression
4895 ( type-name ) unary-expression
4898 static struct c_expr
4899 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4901 location_t cast_loc = c_parser_peek_token (parser)->location;
4902 gcc_assert (!after || c_dialect_objc ());
4903 if (after)
4904 return c_parser_postfix_expression_after_primary (parser,
4905 cast_loc, *after);
4906 /* If the expression begins with a parenthesized type name, it may
4907 be either a cast or a compound literal; we need to see whether
4908 the next character is '{' to tell the difference. If not, it is
4909 an unary expression. */
4910 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4911 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4913 struct c_type_name *type_name;
4914 struct c_expr ret;
4915 struct c_expr expr;
4916 c_parser_consume_token (parser);
4917 type_name = c_parser_type_name (parser);
4918 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4919 if (type_name == NULL)
4921 ret.value = error_mark_node;
4922 ret.original_code = ERROR_MARK;
4923 ret.original_type = NULL;
4924 return ret;
4927 /* Save casted types in the function's used types hash table. */
4928 used_types_insert (type_name->specs->type);
4930 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4931 return c_parser_postfix_expression_after_paren_type (parser, type_name,
4932 cast_loc);
4934 location_t expr_loc = c_parser_peek_token (parser)->location;
4935 expr = c_parser_cast_expression (parser, NULL);
4936 expr = default_function_array_conversion (expr_loc, expr);
4938 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
4939 ret.original_code = ERROR_MARK;
4940 ret.original_type = NULL;
4941 return ret;
4943 else
4944 return c_parser_unary_expression (parser);
4947 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4949 unary-expression:
4950 postfix-expression
4951 ++ unary-expression
4952 -- unary-expression
4953 unary-operator cast-expression
4954 sizeof unary-expression
4955 sizeof ( type-name )
4957 unary-operator: one of
4958 & * + - ~ !
4960 GNU extensions:
4962 unary-expression:
4963 __alignof__ unary-expression
4964 __alignof__ ( type-name )
4965 && identifier
4967 unary-operator: one of
4968 __extension__ __real__ __imag__
4970 In addition, the GNU syntax treats ++ and -- as unary operators, so
4971 they may be applied to cast expressions with errors for non-lvalues
4972 given later. */
4974 static struct c_expr
4975 c_parser_unary_expression (c_parser *parser)
4977 int ext;
4978 struct c_expr ret, op;
4979 location_t op_loc = c_parser_peek_token (parser)->location;
4980 location_t exp_loc;
4981 ret.original_code = ERROR_MARK;
4982 ret.original_type = NULL;
4983 switch (c_parser_peek_token (parser)->type)
4985 case CPP_PLUS_PLUS:
4986 c_parser_consume_token (parser);
4987 exp_loc = c_parser_peek_token (parser)->location;
4988 op = c_parser_cast_expression (parser, NULL);
4989 op = default_function_array_conversion (exp_loc, op);
4990 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
4991 case CPP_MINUS_MINUS:
4992 c_parser_consume_token (parser);
4993 exp_loc = c_parser_peek_token (parser)->location;
4994 op = c_parser_cast_expression (parser, NULL);
4995 op = default_function_array_conversion (exp_loc, op);
4996 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
4997 case CPP_AND:
4998 c_parser_consume_token (parser);
4999 return parser_build_unary_op (op_loc, ADDR_EXPR,
5000 c_parser_cast_expression (parser, NULL));
5001 case CPP_MULT:
5002 c_parser_consume_token (parser);
5003 exp_loc = c_parser_peek_token (parser)->location;
5004 op = c_parser_cast_expression (parser, NULL);
5005 op = default_function_array_conversion (exp_loc, op);
5006 ret.value = build_indirect_ref (op_loc, op.value, "unary *");
5007 return ret;
5008 case CPP_PLUS:
5009 if (!c_dialect_objc () && !in_system_header)
5010 warning_at (op_loc,
5011 OPT_Wtraditional,
5012 "traditional C rejects the unary plus operator");
5013 c_parser_consume_token (parser);
5014 exp_loc = c_parser_peek_token (parser)->location;
5015 op = c_parser_cast_expression (parser, NULL);
5016 op = default_function_array_conversion (exp_loc, op);
5017 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5018 case CPP_MINUS:
5019 c_parser_consume_token (parser);
5020 exp_loc = c_parser_peek_token (parser)->location;
5021 op = c_parser_cast_expression (parser, NULL);
5022 op = default_function_array_conversion (exp_loc, op);
5023 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5024 case CPP_COMPL:
5025 c_parser_consume_token (parser);
5026 exp_loc = c_parser_peek_token (parser)->location;
5027 op = c_parser_cast_expression (parser, NULL);
5028 op = default_function_array_conversion (exp_loc, op);
5029 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5030 case CPP_NOT:
5031 c_parser_consume_token (parser);
5032 exp_loc = c_parser_peek_token (parser)->location;
5033 op = c_parser_cast_expression (parser, NULL);
5034 op = default_function_array_conversion (exp_loc, op);
5035 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5036 case CPP_AND_AND:
5037 /* Refer to the address of a label as a pointer. */
5038 c_parser_consume_token (parser);
5039 if (c_parser_next_token_is (parser, CPP_NAME))
5041 ret.value = finish_label_address_expr
5042 (c_parser_peek_token (parser)->value, op_loc);
5043 c_parser_consume_token (parser);
5045 else
5047 c_parser_error (parser, "expected identifier");
5048 ret.value = error_mark_node;
5050 return ret;
5051 case CPP_KEYWORD:
5052 switch (c_parser_peek_token (parser)->keyword)
5054 case RID_SIZEOF:
5055 return c_parser_sizeof_expression (parser);
5056 case RID_ALIGNOF:
5057 return c_parser_alignof_expression (parser);
5058 case RID_EXTENSION:
5059 c_parser_consume_token (parser);
5060 ext = disable_extension_diagnostics ();
5061 ret = c_parser_cast_expression (parser, NULL);
5062 restore_extension_diagnostics (ext);
5063 return ret;
5064 case RID_REALPART:
5065 c_parser_consume_token (parser);
5066 exp_loc = c_parser_peek_token (parser)->location;
5067 op = c_parser_cast_expression (parser, NULL);
5068 op = default_function_array_conversion (exp_loc, op);
5069 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5070 case RID_IMAGPART:
5071 c_parser_consume_token (parser);
5072 exp_loc = c_parser_peek_token (parser)->location;
5073 op = c_parser_cast_expression (parser, NULL);
5074 op = default_function_array_conversion (exp_loc, op);
5075 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5076 default:
5077 return c_parser_postfix_expression (parser);
5079 default:
5080 return c_parser_postfix_expression (parser);
5084 /* Parse a sizeof expression. */
5086 static struct c_expr
5087 c_parser_sizeof_expression (c_parser *parser)
5089 struct c_expr expr;
5090 location_t expr_loc;
5091 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5092 c_parser_consume_token (parser);
5093 c_inhibit_evaluation_warnings++;
5094 in_sizeof++;
5095 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5096 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5098 /* Either sizeof ( type-name ) or sizeof unary-expression
5099 starting with a compound literal. */
5100 struct c_type_name *type_name;
5101 c_parser_consume_token (parser);
5102 expr_loc = c_parser_peek_token (parser)->location;
5103 type_name = c_parser_type_name (parser);
5104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5105 if (type_name == NULL)
5107 struct c_expr ret;
5108 c_inhibit_evaluation_warnings--;
5109 in_sizeof--;
5110 ret.value = error_mark_node;
5111 ret.original_code = ERROR_MARK;
5112 ret.original_type = NULL;
5113 return ret;
5115 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5117 expr = c_parser_postfix_expression_after_paren_type (parser,
5118 type_name,
5119 expr_loc);
5120 goto sizeof_expr;
5122 /* sizeof ( type-name ). */
5123 c_inhibit_evaluation_warnings--;
5124 in_sizeof--;
5125 return c_expr_sizeof_type (expr_loc, type_name);
5127 else
5129 expr_loc = c_parser_peek_token (parser)->location;
5130 expr = c_parser_unary_expression (parser);
5131 sizeof_expr:
5132 c_inhibit_evaluation_warnings--;
5133 in_sizeof--;
5134 if (TREE_CODE (expr.value) == COMPONENT_REF
5135 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5136 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5137 return c_expr_sizeof_expr (expr_loc, expr);
5141 /* Parse an alignof expression. */
5143 static struct c_expr
5144 c_parser_alignof_expression (c_parser *parser)
5146 struct c_expr expr;
5147 location_t loc = c_parser_peek_token (parser)->location;
5148 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5149 c_parser_consume_token (parser);
5150 c_inhibit_evaluation_warnings++;
5151 in_alignof++;
5152 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5153 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5155 /* Either __alignof__ ( type-name ) or __alignof__
5156 unary-expression starting with a compound literal. */
5157 location_t loc;
5158 struct c_type_name *type_name;
5159 struct c_expr ret;
5160 c_parser_consume_token (parser);
5161 loc = c_parser_peek_token (parser)->location;
5162 type_name = c_parser_type_name (parser);
5163 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5164 if (type_name == NULL)
5166 struct c_expr ret;
5167 c_inhibit_evaluation_warnings--;
5168 in_alignof--;
5169 ret.value = error_mark_node;
5170 ret.original_code = ERROR_MARK;
5171 ret.original_type = NULL;
5172 return ret;
5174 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5176 expr = c_parser_postfix_expression_after_paren_type (parser,
5177 type_name,
5178 loc);
5179 goto alignof_expr;
5181 /* alignof ( type-name ). */
5182 c_inhibit_evaluation_warnings--;
5183 in_alignof--;
5184 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5185 ret.original_code = ERROR_MARK;
5186 ret.original_type = NULL;
5187 return ret;
5189 else
5191 struct c_expr ret;
5192 expr = c_parser_unary_expression (parser);
5193 alignof_expr:
5194 c_inhibit_evaluation_warnings--;
5195 in_alignof--;
5196 ret.value = c_alignof_expr (loc, expr.value);
5197 ret.original_code = ERROR_MARK;
5198 ret.original_type = NULL;
5199 return ret;
5203 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5205 postfix-expression:
5206 primary-expression
5207 postfix-expression [ expression ]
5208 postfix-expression ( argument-expression-list[opt] )
5209 postfix-expression . identifier
5210 postfix-expression -> identifier
5211 postfix-expression ++
5212 postfix-expression --
5213 ( type-name ) { initializer-list }
5214 ( type-name ) { initializer-list , }
5216 argument-expression-list:
5217 argument-expression
5218 argument-expression-list , argument-expression
5220 primary-expression:
5221 identifier
5222 constant
5223 string-literal
5224 ( expression )
5226 GNU extensions:
5228 primary-expression:
5229 __func__
5230 (treated as a keyword in GNU C)
5231 __FUNCTION__
5232 __PRETTY_FUNCTION__
5233 ( compound-statement )
5234 __builtin_va_arg ( assignment-expression , type-name )
5235 __builtin_offsetof ( type-name , offsetof-member-designator )
5236 __builtin_choose_expr ( assignment-expression ,
5237 assignment-expression ,
5238 assignment-expression )
5239 __builtin_types_compatible_p ( type-name , type-name )
5241 offsetof-member-designator:
5242 identifier
5243 offsetof-member-designator . identifier
5244 offsetof-member-designator [ expression ]
5246 Objective-C:
5248 primary-expression:
5249 [ objc-receiver objc-message-args ]
5250 @selector ( objc-selector-arg )
5251 @protocol ( identifier )
5252 @encode ( type-name )
5253 objc-string-literal
5256 static struct c_expr
5257 c_parser_postfix_expression (c_parser *parser)
5259 struct c_expr expr, e1, e2, e3;
5260 struct c_type_name *t1, *t2;
5261 location_t loc = c_parser_peek_token (parser)->location;;
5262 expr.original_code = ERROR_MARK;
5263 expr.original_type = NULL;
5264 switch (c_parser_peek_token (parser)->type)
5266 case CPP_NUMBER:
5267 expr.value = c_parser_peek_token (parser)->value;
5268 loc = c_parser_peek_token (parser)->location;
5269 c_parser_consume_token (parser);
5270 if (TREE_CODE (expr.value) == FIXED_CST
5271 && !targetm.fixed_point_supported_p ())
5273 error_at (loc, "fixed-point types not supported for this target");
5274 expr.value = error_mark_node;
5276 break;
5277 case CPP_CHAR:
5278 case CPP_CHAR16:
5279 case CPP_CHAR32:
5280 case CPP_WCHAR:
5281 expr.value = c_parser_peek_token (parser)->value;
5282 c_parser_consume_token (parser);
5283 break;
5284 case CPP_STRING:
5285 case CPP_STRING16:
5286 case CPP_STRING32:
5287 case CPP_WSTRING:
5288 expr.value = c_parser_peek_token (parser)->value;
5289 expr.original_code = STRING_CST;
5290 c_parser_consume_token (parser);
5291 break;
5292 case CPP_OBJC_STRING:
5293 gcc_assert (c_dialect_objc ());
5294 expr.value
5295 = objc_build_string_object (c_parser_peek_token (parser)->value);
5296 c_parser_consume_token (parser);
5297 break;
5298 case CPP_NAME:
5299 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5301 c_parser_error (parser, "expected expression");
5302 expr.value = error_mark_node;
5303 break;
5306 tree id = c_parser_peek_token (parser)->value;
5307 c_parser_consume_token (parser);
5308 expr.value = build_external_ref (loc, id,
5309 (c_parser_peek_token (parser)->type
5310 == CPP_OPEN_PAREN),
5311 &expr.original_type);
5313 break;
5314 case CPP_OPEN_PAREN:
5315 /* A parenthesized expression, statement expression or compound
5316 literal. */
5317 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5319 /* A statement expression. */
5320 tree stmt;
5321 location_t brace_loc;
5322 c_parser_consume_token (parser);
5323 brace_loc = c_parser_peek_token (parser)->location;
5324 c_parser_consume_token (parser);
5325 if (cur_stmt_list == NULL)
5327 error_at (loc, "braced-group within expression allowed "
5328 "only inside a function");
5329 parser->error = true;
5330 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5332 expr.value = error_mark_node;
5333 break;
5335 stmt = c_begin_stmt_expr ();
5336 c_parser_compound_statement_nostart (parser);
5337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5338 "expected %<)%>");
5339 pedwarn (loc, OPT_pedantic,
5340 "ISO C forbids braced-groups within expressions");
5341 expr.value = c_finish_stmt_expr (brace_loc, stmt);
5343 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5345 /* A compound literal. ??? Can we actually get here rather
5346 than going directly to
5347 c_parser_postfix_expression_after_paren_type from
5348 elsewhere? */
5349 location_t loc;
5350 struct c_type_name *type_name;
5351 c_parser_consume_token (parser);
5352 loc = c_parser_peek_token (parser)->location;
5353 type_name = c_parser_type_name (parser);
5354 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5355 "expected %<)%>");
5356 if (type_name == NULL)
5358 expr.value = error_mark_node;
5360 else
5361 expr = c_parser_postfix_expression_after_paren_type (parser,
5362 type_name,
5363 loc);
5365 else
5367 /* A parenthesized expression. */
5368 c_parser_consume_token (parser);
5369 expr = c_parser_expression (parser);
5370 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5371 TREE_NO_WARNING (expr.value) = 1;
5372 if (expr.original_code != C_MAYBE_CONST_EXPR)
5373 expr.original_code = ERROR_MARK;
5374 /* Don't change EXPR.ORIGINAL_TYPE. */
5375 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5376 "expected %<)%>");
5378 break;
5379 case CPP_KEYWORD:
5380 switch (c_parser_peek_token (parser)->keyword)
5382 case RID_FUNCTION_NAME:
5383 case RID_PRETTY_FUNCTION_NAME:
5384 case RID_C99_FUNCTION_NAME:
5385 expr.value = fname_decl (loc,
5386 c_parser_peek_token (parser)->keyword,
5387 c_parser_peek_token (parser)->value);
5388 c_parser_consume_token (parser);
5389 break;
5390 case RID_VA_ARG:
5391 c_parser_consume_token (parser);
5392 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5394 expr.value = error_mark_node;
5395 break;
5397 e1 = c_parser_expr_no_commas (parser, NULL);
5398 e1.value = c_fully_fold (e1.value, false, NULL);
5399 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5401 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5402 expr.value = error_mark_node;
5403 break;
5405 loc = c_parser_peek_token (parser)->location;
5406 t1 = c_parser_type_name (parser);
5407 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5408 "expected %<)%>");
5409 if (t1 == NULL)
5411 expr.value = error_mark_node;
5413 else
5415 tree type_expr = NULL_TREE;
5416 expr.value = c_build_va_arg (loc, e1.value,
5417 groktypename (t1, &type_expr, NULL));
5418 if (type_expr)
5420 expr.value = build2 (C_MAYBE_CONST_EXPR,
5421 TREE_TYPE (expr.value), type_expr,
5422 expr.value);
5423 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5426 break;
5427 case RID_OFFSETOF:
5428 c_parser_consume_token (parser);
5429 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5431 expr.value = error_mark_node;
5432 break;
5434 t1 = c_parser_type_name (parser);
5435 if (t1 == NULL)
5437 expr.value = error_mark_node;
5438 break;
5440 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5442 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5443 expr.value = error_mark_node;
5444 break;
5447 tree type = groktypename (t1, NULL, NULL);
5448 tree offsetof_ref;
5449 if (type == error_mark_node)
5450 offsetof_ref = error_mark_node;
5451 else
5453 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5454 SET_EXPR_LOCATION (offsetof_ref, loc);
5456 /* Parse the second argument to __builtin_offsetof. We
5457 must have one identifier, and beyond that we want to
5458 accept sub structure and sub array references. */
5459 if (c_parser_next_token_is (parser, CPP_NAME))
5461 offsetof_ref = build_component_ref
5462 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
5463 c_parser_consume_token (parser);
5464 while (c_parser_next_token_is (parser, CPP_DOT)
5465 || c_parser_next_token_is (parser,
5466 CPP_OPEN_SQUARE)
5467 || c_parser_next_token_is (parser,
5468 CPP_DEREF))
5470 if (c_parser_next_token_is (parser, CPP_DEREF))
5472 loc = c_parser_peek_token (parser)->location;
5473 offsetof_ref = build_array_ref (loc,
5474 offsetof_ref,
5475 integer_zero_node);
5476 goto do_dot;
5478 else if (c_parser_next_token_is (parser, CPP_DOT))
5480 do_dot:
5481 c_parser_consume_token (parser);
5482 if (c_parser_next_token_is_not (parser,
5483 CPP_NAME))
5485 c_parser_error (parser, "expected identifier");
5486 break;
5488 offsetof_ref = build_component_ref
5489 (loc, offsetof_ref,
5490 c_parser_peek_token (parser)->value);
5491 c_parser_consume_token (parser);
5493 else
5495 tree idx;
5496 loc = c_parser_peek_token (parser)->location;
5497 c_parser_consume_token (parser);
5498 idx = c_parser_expression (parser).value;
5499 idx = c_fully_fold (idx, false, NULL);
5500 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5501 "expected %<]%>");
5502 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
5506 else
5507 c_parser_error (parser, "expected identifier");
5508 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5509 "expected %<)%>");
5510 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5512 break;
5513 case RID_CHOOSE_EXPR:
5514 c_parser_consume_token (parser);
5515 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5517 expr.value = error_mark_node;
5518 break;
5520 loc = c_parser_peek_token (parser)->location;
5521 e1 = c_parser_expr_no_commas (parser, NULL);
5522 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5524 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5525 expr.value = error_mark_node;
5526 break;
5528 e2 = c_parser_expr_no_commas (parser, NULL);
5529 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5531 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5532 expr.value = error_mark_node;
5533 break;
5535 e3 = c_parser_expr_no_commas (parser, NULL);
5536 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5537 "expected %<)%>");
5539 tree c;
5541 c = e1.value;
5542 if (TREE_CODE (c) != INTEGER_CST
5543 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
5544 error_at (loc,
5545 "first argument to %<__builtin_choose_expr%> not"
5546 " a constant");
5547 constant_expression_warning (c);
5548 expr = integer_zerop (c) ? e3 : e2;
5550 break;
5551 case RID_TYPES_COMPATIBLE_P:
5552 c_parser_consume_token (parser);
5553 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5555 expr.value = error_mark_node;
5556 break;
5558 t1 = c_parser_type_name (parser);
5559 if (t1 == NULL)
5561 expr.value = error_mark_node;
5562 break;
5564 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5566 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5567 expr.value = error_mark_node;
5568 break;
5570 t2 = c_parser_type_name (parser);
5571 if (t2 == NULL)
5573 expr.value = error_mark_node;
5574 break;
5576 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5577 "expected %<)%>");
5579 tree e1, e2;
5581 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5582 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
5584 expr.value = comptypes (e1, e2)
5585 ? build_int_cst (NULL_TREE, 1)
5586 : build_int_cst (NULL_TREE, 0);
5588 break;
5589 case RID_AT_SELECTOR:
5590 gcc_assert (c_dialect_objc ());
5591 c_parser_consume_token (parser);
5592 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5594 expr.value = error_mark_node;
5595 break;
5598 tree sel = c_parser_objc_selector_arg (parser);
5599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5600 "expected %<)%>");
5601 expr.value = objc_build_selector_expr (loc, sel);
5603 break;
5604 case RID_AT_PROTOCOL:
5605 gcc_assert (c_dialect_objc ());
5606 c_parser_consume_token (parser);
5607 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5609 expr.value = error_mark_node;
5610 break;
5612 if (c_parser_next_token_is_not (parser, CPP_NAME))
5614 c_parser_error (parser, "expected identifier");
5615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5616 expr.value = error_mark_node;
5617 break;
5620 tree id = c_parser_peek_token (parser)->value;
5621 c_parser_consume_token (parser);
5622 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5623 "expected %<)%>");
5624 expr.value = objc_build_protocol_expr (id);
5626 break;
5627 case RID_AT_ENCODE:
5628 /* Extension to support C-structures in the archiver. */
5629 gcc_assert (c_dialect_objc ());
5630 c_parser_consume_token (parser);
5631 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5633 expr.value = error_mark_node;
5634 break;
5636 t1 = c_parser_type_name (parser);
5637 if (t1 == NULL)
5639 expr.value = error_mark_node;
5640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5641 break;
5643 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5644 "expected %<)%>");
5646 tree type = groktypename (t1, NULL, NULL);
5647 expr.value = objc_build_encode_expr (type);
5649 break;
5650 default:
5651 c_parser_error (parser, "expected expression");
5652 expr.value = error_mark_node;
5653 break;
5655 break;
5656 case CPP_OPEN_SQUARE:
5657 if (c_dialect_objc ())
5659 tree receiver, args;
5660 c_parser_consume_token (parser);
5661 receiver = c_parser_objc_receiver (parser);
5662 args = c_parser_objc_message_args (parser);
5663 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5664 "expected %<]%>");
5665 expr.value = objc_build_message_expr (build_tree_list (receiver,
5666 args));
5667 break;
5669 /* Else fall through to report error. */
5670 default:
5671 c_parser_error (parser, "expected expression");
5672 expr.value = error_mark_node;
5673 break;
5675 return c_parser_postfix_expression_after_primary (parser, loc, expr);
5678 /* Parse a postfix expression after a parenthesized type name: the
5679 brace-enclosed initializer of a compound literal, possibly followed
5680 by some postfix operators. This is separate because it is not
5681 possible to tell until after the type name whether a cast
5682 expression has a cast or a compound literal, or whether the operand
5683 of sizeof is a parenthesized type name or starts with a compound
5684 literal. TYPE_LOC is the location where TYPE_NAME starts--the
5685 location of the first token after the parentheses around the type
5686 name. */
5688 static struct c_expr
5689 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5690 struct c_type_name *type_name,
5691 location_t type_loc)
5693 tree type;
5694 struct c_expr init;
5695 bool non_const;
5696 struct c_expr expr;
5697 location_t start_loc;
5698 tree type_expr = NULL_TREE;
5699 bool type_expr_const = true;
5700 check_compound_literal_type (type_loc, type_name);
5701 start_init (NULL_TREE, NULL, 0);
5702 type = groktypename (type_name, &type_expr, &type_expr_const);
5703 start_loc = c_parser_peek_token (parser)->location;
5704 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5706 error_at (type_loc, "compound literal has variable size");
5707 type = error_mark_node;
5709 init = c_parser_braced_init (parser, type, false);
5710 finish_init ();
5711 maybe_warn_string_init (type, init);
5713 if (!flag_isoc99)
5714 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
5715 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5716 ? CONSTRUCTOR_NON_CONST (init.value)
5717 : init.original_code == C_MAYBE_CONST_EXPR);
5718 non_const |= !type_expr_const;
5719 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
5720 expr.original_code = ERROR_MARK;
5721 expr.original_type = NULL;
5722 if (type_expr)
5724 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5726 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
5727 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
5729 else
5731 gcc_assert (!non_const);
5732 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
5733 type_expr, expr.value);
5736 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
5739 /* Parse a postfix expression after the initial primary or compound
5740 literal; that is, parse a series of postfix operators.
5742 EXPR_LOC is the location of the primary expression. */
5744 static struct c_expr
5745 c_parser_postfix_expression_after_primary (c_parser *parser,
5746 location_t expr_loc,
5747 struct c_expr expr)
5749 struct c_expr orig_expr;
5750 tree ident, idx;
5751 VEC(tree,gc) *exprlist;
5752 VEC(tree,gc) *origtypes;
5753 while (true)
5755 location_t op_loc = c_parser_peek_token (parser)->location;
5756 switch (c_parser_peek_token (parser)->type)
5758 case CPP_OPEN_SQUARE:
5759 /* Array reference. */
5760 c_parser_consume_token (parser);
5761 idx = c_parser_expression (parser).value;
5762 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5763 "expected %<]%>");
5764 expr.value = build_array_ref (op_loc, expr.value, idx);
5765 expr.original_code = ERROR_MARK;
5766 expr.original_type = NULL;
5767 break;
5768 case CPP_OPEN_PAREN:
5769 /* Function call. */
5770 c_parser_consume_token (parser);
5771 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5772 exprlist = NULL;
5773 else
5774 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
5775 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5776 "expected %<)%>");
5777 orig_expr = expr;
5778 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
5779 "(" after the FUNCNAME, which is what we have now. */
5780 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
5781 origtypes);
5782 expr.original_code = ERROR_MARK;
5783 if (TREE_CODE (expr.value) == INTEGER_CST
5784 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
5785 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
5786 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
5787 expr.original_code = C_MAYBE_CONST_EXPR;
5788 expr.original_type = NULL;
5789 if (exprlist != NULL)
5791 release_tree_vector (exprlist);
5792 release_tree_vector (origtypes);
5794 break;
5795 case CPP_DOT:
5796 /* Structure element reference. */
5797 c_parser_consume_token (parser);
5798 expr = default_function_array_conversion (expr_loc, expr);
5799 if (c_parser_next_token_is (parser, CPP_NAME))
5800 ident = c_parser_peek_token (parser)->value;
5801 else
5803 c_parser_error (parser, "expected identifier");
5804 expr.value = error_mark_node;
5805 expr.original_code = ERROR_MARK;
5806 expr.original_type = NULL;
5807 return expr;
5809 c_parser_consume_token (parser);
5810 expr.value = build_component_ref (op_loc, expr.value, ident);
5811 expr.original_code = ERROR_MARK;
5812 if (TREE_CODE (expr.value) != COMPONENT_REF)
5813 expr.original_type = NULL;
5814 else
5816 /* Remember the original type of a bitfield. */
5817 tree field = TREE_OPERAND (expr.value, 1);
5818 if (TREE_CODE (field) != FIELD_DECL)
5819 expr.original_type = NULL;
5820 else
5821 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5823 break;
5824 case CPP_DEREF:
5825 /* Structure element reference. */
5826 c_parser_consume_token (parser);
5827 expr = default_function_array_conversion (expr_loc, expr);
5828 if (c_parser_next_token_is (parser, CPP_NAME))
5829 ident = c_parser_peek_token (parser)->value;
5830 else
5832 c_parser_error (parser, "expected identifier");
5833 expr.value = error_mark_node;
5834 expr.original_code = ERROR_MARK;
5835 expr.original_type = NULL;
5836 return expr;
5838 c_parser_consume_token (parser);
5839 expr.value = build_component_ref (op_loc,
5840 build_indirect_ref (op_loc,
5841 expr.value,
5842 "->"),
5843 ident);
5844 expr.original_code = ERROR_MARK;
5845 if (TREE_CODE (expr.value) != COMPONENT_REF)
5846 expr.original_type = NULL;
5847 else
5849 /* Remember the original type of a bitfield. */
5850 tree field = TREE_OPERAND (expr.value, 1);
5851 if (TREE_CODE (field) != FIELD_DECL)
5852 expr.original_type = NULL;
5853 else
5854 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5856 break;
5857 case CPP_PLUS_PLUS:
5858 /* Postincrement. */
5859 c_parser_consume_token (parser);
5860 expr = default_function_array_conversion (expr_loc, expr);
5861 expr.value = build_unary_op (op_loc,
5862 POSTINCREMENT_EXPR, expr.value, 0);
5863 expr.original_code = ERROR_MARK;
5864 expr.original_type = NULL;
5865 break;
5866 case CPP_MINUS_MINUS:
5867 /* Postdecrement. */
5868 c_parser_consume_token (parser);
5869 expr = default_function_array_conversion (expr_loc, expr);
5870 expr.value = build_unary_op (op_loc,
5871 POSTDECREMENT_EXPR, expr.value, 0);
5872 expr.original_code = ERROR_MARK;
5873 expr.original_type = NULL;
5874 break;
5875 default:
5876 return expr;
5881 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5883 expression:
5884 assignment-expression
5885 expression , assignment-expression
5888 static struct c_expr
5889 c_parser_expression (c_parser *parser)
5891 struct c_expr expr;
5892 expr = c_parser_expr_no_commas (parser, NULL);
5893 while (c_parser_next_token_is (parser, CPP_COMMA))
5895 struct c_expr next;
5896 location_t loc = c_parser_peek_token (parser)->location;
5897 location_t expr_loc;
5898 c_parser_consume_token (parser);
5899 expr_loc = c_parser_peek_token (parser)->location;
5900 next = c_parser_expr_no_commas (parser, NULL);
5901 next = default_function_array_conversion (expr_loc, next);
5902 expr.value = build_compound_expr (loc, expr.value, next.value);
5903 expr.original_code = COMPOUND_EXPR;
5904 expr.original_type = next.original_type;
5906 return expr;
5909 /* Parse an expression and convert functions or arrays to
5910 pointers. */
5912 static struct c_expr
5913 c_parser_expression_conv (c_parser *parser)
5915 struct c_expr expr;
5916 location_t loc = c_parser_peek_token (parser)->location;
5917 expr = c_parser_expression (parser);
5918 expr = default_function_array_conversion (loc, expr);
5919 return expr;
5922 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5923 functions and arrays to pointers. If FOLD_P, fold the expressions.
5925 nonempty-expr-list:
5926 assignment-expression
5927 nonempty-expr-list , assignment-expression
5930 static VEC(tree,gc) *
5931 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
5932 VEC(tree,gc) **p_orig_types)
5934 VEC(tree,gc) *ret;
5935 VEC(tree,gc) *orig_types;
5936 struct c_expr expr;
5937 location_t loc = c_parser_peek_token (parser)->location;
5939 ret = make_tree_vector ();
5940 if (p_orig_types == NULL)
5941 orig_types = NULL;
5942 else
5943 orig_types = make_tree_vector ();
5945 expr = c_parser_expr_no_commas (parser, NULL);
5946 if (convert_p)
5947 expr = default_function_array_conversion (loc, expr);
5948 if (fold_p)
5949 expr.value = c_fully_fold (expr.value, false, NULL);
5950 VEC_quick_push (tree, ret, expr.value);
5951 if (orig_types != NULL)
5952 VEC_quick_push (tree, orig_types, expr.original_type);
5953 while (c_parser_next_token_is (parser, CPP_COMMA))
5955 c_parser_consume_token (parser);
5956 loc = c_parser_peek_token (parser)->location;
5957 expr = c_parser_expr_no_commas (parser, NULL);
5958 if (convert_p)
5959 expr = default_function_array_conversion (loc, expr);
5960 if (fold_p)
5961 expr.value = c_fully_fold (expr.value, false, NULL);
5962 VEC_safe_push (tree, gc, ret, expr.value);
5963 if (orig_types != NULL)
5964 VEC_safe_push (tree, gc, orig_types, expr.original_type);
5966 if (orig_types != NULL)
5967 *p_orig_types = orig_types;
5968 return ret;
5971 /* Parse Objective-C-specific constructs. */
5973 /* Parse an objc-class-definition.
5975 objc-class-definition:
5976 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5977 objc-class-instance-variables[opt] objc-methodprotolist @end
5978 @implementation identifier objc-superclass[opt]
5979 objc-class-instance-variables[opt]
5980 @interface identifier ( identifier ) objc-protocol-refs[opt]
5981 objc-methodprotolist @end
5982 @implementation identifier ( identifier )
5984 objc-superclass:
5985 : identifier
5987 "@interface identifier (" must start "@interface identifier (
5988 identifier ) ...": objc-methodprotolist in the first production may
5989 not start with a parenthesized identifier as a declarator of a data
5990 definition with no declaration specifiers if the objc-superclass,
5991 objc-protocol-refs and objc-class-instance-variables are omitted. */
5993 static void
5994 c_parser_objc_class_definition (c_parser *parser)
5996 bool iface_p;
5997 tree id1;
5998 tree superclass;
5999 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6000 iface_p = true;
6001 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6002 iface_p = false;
6003 else
6004 gcc_unreachable ();
6005 c_parser_consume_token (parser);
6006 if (c_parser_next_token_is_not (parser, CPP_NAME))
6008 c_parser_error (parser, "expected identifier");
6009 return;
6011 id1 = c_parser_peek_token (parser)->value;
6012 c_parser_consume_token (parser);
6013 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6015 tree id2;
6016 tree proto = NULL_TREE;
6017 c_parser_consume_token (parser);
6018 if (c_parser_next_token_is_not (parser, CPP_NAME))
6020 c_parser_error (parser, "expected identifier");
6021 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6022 return;
6024 id2 = c_parser_peek_token (parser)->value;
6025 c_parser_consume_token (parser);
6026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6027 if (!iface_p)
6029 objc_start_category_implementation (id1, id2);
6030 return;
6032 if (c_parser_next_token_is (parser, CPP_LESS))
6033 proto = c_parser_objc_protocol_refs (parser);
6034 objc_start_category_interface (id1, id2, proto);
6035 c_parser_objc_methodprotolist (parser);
6036 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6037 objc_finish_interface ();
6038 return;
6040 if (c_parser_next_token_is (parser, CPP_COLON))
6042 c_parser_consume_token (parser);
6043 if (c_parser_next_token_is_not (parser, CPP_NAME))
6045 c_parser_error (parser, "expected identifier");
6046 return;
6048 superclass = c_parser_peek_token (parser)->value;
6049 c_parser_consume_token (parser);
6051 else
6052 superclass = NULL_TREE;
6053 if (iface_p)
6055 tree proto = NULL_TREE;
6056 if (c_parser_next_token_is (parser, CPP_LESS))
6057 proto = c_parser_objc_protocol_refs (parser);
6058 objc_start_class_interface (id1, superclass, proto);
6060 else
6061 objc_start_class_implementation (id1, superclass);
6062 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6063 c_parser_objc_class_instance_variables (parser);
6064 if (iface_p)
6066 objc_continue_interface ();
6067 c_parser_objc_methodprotolist (parser);
6068 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6069 objc_finish_interface ();
6071 else
6073 objc_continue_implementation ();
6074 return;
6078 /* Parse objc-class-instance-variables.
6080 objc-class-instance-variables:
6081 { objc-instance-variable-decl-list[opt] }
6083 objc-instance-variable-decl-list:
6084 objc-visibility-spec
6085 objc-instance-variable-decl ;
6087 objc-instance-variable-decl-list objc-visibility-spec
6088 objc-instance-variable-decl-list objc-instance-variable-decl ;
6089 objc-instance-variable-decl-list ;
6091 objc-visibility-spec:
6092 @private
6093 @protected
6094 @public
6096 objc-instance-variable-decl:
6097 struct-declaration
6100 static void
6101 c_parser_objc_class_instance_variables (c_parser *parser)
6103 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6104 c_parser_consume_token (parser);
6105 while (c_parser_next_token_is_not (parser, CPP_EOF))
6107 tree decls;
6108 /* Parse any stray semicolon. */
6109 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6111 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6112 "extra semicolon in struct or union specified");
6113 c_parser_consume_token (parser);
6114 continue;
6116 /* Stop if at the end of the instance variables. */
6117 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6119 c_parser_consume_token (parser);
6120 break;
6122 /* Parse any objc-visibility-spec. */
6123 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
6125 c_parser_consume_token (parser);
6126 objc_set_visibility (2);
6127 continue;
6129 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
6131 c_parser_consume_token (parser);
6132 objc_set_visibility (0);
6133 continue;
6135 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
6137 c_parser_consume_token (parser);
6138 objc_set_visibility (1);
6139 continue;
6141 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6143 c_parser_pragma (parser, pragma_external);
6144 continue;
6147 /* Parse some comma-separated declarations. */
6148 decls = c_parser_struct_declaration (parser);
6150 /* Comma-separated instance variables are chained together in
6151 reverse order; add them one by one. */
6152 tree ivar = nreverse (decls);
6153 for (; ivar; ivar = TREE_CHAIN (ivar))
6154 objc_add_instance_variable (copy_node (ivar));
6156 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6160 /* Parse an objc-class-declaration.
6162 objc-class-declaration:
6163 @class identifier-list ;
6166 static void
6167 c_parser_objc_class_declaration (c_parser *parser)
6169 tree list = NULL_TREE;
6170 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
6171 c_parser_consume_token (parser);
6172 /* Any identifiers, including those declared as type names, are OK
6173 here. */
6174 while (true)
6176 tree id;
6177 if (c_parser_next_token_is_not (parser, CPP_NAME))
6179 c_parser_error (parser, "expected identifier");
6180 break;
6182 id = c_parser_peek_token (parser)->value;
6183 list = chainon (list, build_tree_list (NULL_TREE, id));
6184 c_parser_consume_token (parser);
6185 if (c_parser_next_token_is (parser, CPP_COMMA))
6186 c_parser_consume_token (parser);
6187 else
6188 break;
6190 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6191 objc_declare_class (list);
6194 /* Parse an objc-alias-declaration.
6196 objc-alias-declaration:
6197 @compatibility_alias identifier identifier ;
6200 static void
6201 c_parser_objc_alias_declaration (c_parser *parser)
6203 tree id1, id2;
6204 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6205 c_parser_consume_token (parser);
6206 if (c_parser_next_token_is_not (parser, CPP_NAME))
6208 c_parser_error (parser, "expected identifier");
6209 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6210 return;
6212 id1 = c_parser_peek_token (parser)->value;
6213 c_parser_consume_token (parser);
6214 if (c_parser_next_token_is_not (parser, CPP_NAME))
6216 c_parser_error (parser, "expected identifier");
6217 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6218 return;
6220 id2 = c_parser_peek_token (parser)->value;
6221 c_parser_consume_token (parser);
6222 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6223 objc_declare_alias (id1, id2);
6226 /* Parse an objc-protocol-definition.
6228 objc-protocol-definition:
6229 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6230 @protocol identifier-list ;
6232 "@protocol identifier ;" should be resolved as "@protocol
6233 identifier-list ;": objc-methodprotolist may not start with a
6234 semicolon in the first alternative if objc-protocol-refs are
6235 omitted. */
6237 static void
6238 c_parser_objc_protocol_definition (c_parser *parser)
6240 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6241 c_parser_consume_token (parser);
6242 if (c_parser_next_token_is_not (parser, CPP_NAME))
6244 c_parser_error (parser, "expected identifier");
6245 return;
6247 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6248 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6250 tree list = NULL_TREE;
6251 /* Any identifiers, including those declared as type names, are
6252 OK here. */
6253 while (true)
6255 tree id;
6256 if (c_parser_next_token_is_not (parser, CPP_NAME))
6258 c_parser_error (parser, "expected identifier");
6259 break;
6261 id = c_parser_peek_token (parser)->value;
6262 list = chainon (list, build_tree_list (NULL_TREE, id));
6263 c_parser_consume_token (parser);
6264 if (c_parser_next_token_is (parser, CPP_COMMA))
6265 c_parser_consume_token (parser);
6266 else
6267 break;
6269 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6270 objc_declare_protocols (list);
6272 else
6274 tree id = c_parser_peek_token (parser)->value;
6275 tree proto = NULL_TREE;
6276 c_parser_consume_token (parser);
6277 if (c_parser_next_token_is (parser, CPP_LESS))
6278 proto = c_parser_objc_protocol_refs (parser);
6279 parser->objc_pq_context = true;
6280 objc_start_protocol (id, proto);
6281 c_parser_objc_methodprotolist (parser);
6282 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6283 parser->objc_pq_context = false;
6284 objc_finish_interface ();
6288 /* Parse an objc-method-type.
6290 objc-method-type:
6295 static enum tree_code
6296 c_parser_objc_method_type (c_parser *parser)
6298 switch (c_parser_peek_token (parser)->type)
6300 case CPP_PLUS:
6301 c_parser_consume_token (parser);
6302 return PLUS_EXPR;
6303 case CPP_MINUS:
6304 c_parser_consume_token (parser);
6305 return MINUS_EXPR;
6306 default:
6307 gcc_unreachable ();
6311 /* Parse an objc-method-definition.
6313 objc-method-definition:
6314 objc-method-type objc-method-decl ;[opt] compound-statement
6317 static void
6318 c_parser_objc_method_definition (c_parser *parser)
6320 enum tree_code type = c_parser_objc_method_type (parser);
6321 tree decl;
6322 objc_set_method_type (type);
6323 parser->objc_pq_context = true;
6324 decl = c_parser_objc_method_decl (parser);
6325 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6327 c_parser_consume_token (parser);
6328 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6329 "extra semicolon in method definition specified");
6331 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6333 c_parser_error (parser, "expected %<{%>");
6334 return;
6336 parser->objc_pq_context = false;
6337 objc_start_method_definition (decl);
6338 add_stmt (c_parser_compound_statement (parser));
6339 objc_finish_method_definition (current_function_decl);
6342 /* Parse an objc-methodprotolist.
6344 objc-methodprotolist:
6345 empty
6346 objc-methodprotolist objc-methodproto
6347 objc-methodprotolist declaration
6348 objc-methodprotolist ;
6350 The declaration is a data definition, which may be missing
6351 declaration specifiers under the same rules and diagnostics as
6352 other data definitions outside functions, and the stray semicolon
6353 is diagnosed the same way as a stray semicolon outside a
6354 function. */
6356 static void
6357 c_parser_objc_methodprotolist (c_parser *parser)
6359 while (true)
6361 /* The list is terminated by @end. */
6362 switch (c_parser_peek_token (parser)->type)
6364 case CPP_SEMICOLON:
6365 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6366 "ISO C does not allow extra %<;%> outside of a function");
6367 c_parser_consume_token (parser);
6368 break;
6369 case CPP_PLUS:
6370 case CPP_MINUS:
6371 c_parser_objc_methodproto (parser);
6372 break;
6373 case CPP_PRAGMA:
6374 c_parser_pragma (parser, pragma_external);
6375 break;
6376 case CPP_EOF:
6377 return;
6378 default:
6379 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6380 return;
6381 c_parser_declaration_or_fndef (parser, false, true, false, true);
6382 break;
6387 /* Parse an objc-methodproto.
6389 objc-methodproto:
6390 objc-method-type objc-method-decl ;
6393 static void
6394 c_parser_objc_methodproto (c_parser *parser)
6396 enum tree_code type = c_parser_objc_method_type (parser);
6397 tree decl;
6398 objc_set_method_type (type);
6399 /* Remember protocol qualifiers in prototypes. */
6400 parser->objc_pq_context = true;
6401 decl = c_parser_objc_method_decl (parser);
6402 /* Forget protocol qualifiers here. */
6403 parser->objc_pq_context = false;
6404 objc_add_method_declaration (decl);
6405 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6408 /* Parse an objc-method-decl.
6410 objc-method-decl:
6411 ( objc-type-name ) objc-selector
6412 objc-selector
6413 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6414 objc-keyword-selector objc-optparmlist
6416 objc-keyword-selector:
6417 objc-keyword-decl
6418 objc-keyword-selector objc-keyword-decl
6420 objc-keyword-decl:
6421 objc-selector : ( objc-type-name ) identifier
6422 objc-selector : identifier
6423 : ( objc-type-name ) identifier
6424 : identifier
6426 objc-optparmlist:
6427 objc-optparms objc-optellipsis
6429 objc-optparms:
6430 empty
6431 objc-opt-parms , parameter-declaration
6433 objc-optellipsis:
6434 empty
6435 , ...
6438 static tree
6439 c_parser_objc_method_decl (c_parser *parser)
6441 tree type = NULL_TREE;
6442 tree sel;
6443 tree parms = NULL_TREE;
6444 bool ellipsis = false;
6446 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6448 c_parser_consume_token (parser);
6449 type = c_parser_objc_type_name (parser);
6450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6452 sel = c_parser_objc_selector (parser);
6453 /* If there is no selector, or a colon follows, we have an
6454 objc-keyword-selector. If there is a selector, and a colon does
6455 not follow, that selector ends the objc-method-decl. */
6456 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6458 tree tsel = sel;
6459 tree list = NULL_TREE;
6460 while (true)
6462 tree atype = NULL_TREE, id, keyworddecl;
6463 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6464 break;
6465 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6467 c_parser_consume_token (parser);
6468 atype = c_parser_objc_type_name (parser);
6469 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6470 "expected %<)%>");
6472 if (c_parser_next_token_is_not (parser, CPP_NAME))
6474 c_parser_error (parser, "expected identifier");
6475 return error_mark_node;
6477 id = c_parser_peek_token (parser)->value;
6478 c_parser_consume_token (parser);
6479 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6480 list = chainon (list, keyworddecl);
6481 tsel = c_parser_objc_selector (parser);
6482 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6483 break;
6485 /* Parse the optional parameter list. Optional Objective-C
6486 method parameters follow the C syntax, and may include '...'
6487 to denote a variable number of arguments. */
6488 parms = make_node (TREE_LIST);
6489 while (c_parser_next_token_is (parser, CPP_COMMA))
6491 struct c_parm *parm;
6492 c_parser_consume_token (parser);
6493 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6495 ellipsis = true;
6496 c_parser_consume_token (parser);
6497 break;
6499 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6500 if (parm == NULL)
6501 break;
6502 parms = chainon (parms,
6503 build_tree_list (NULL_TREE, grokparm (parm)));
6505 sel = list;
6507 return objc_build_method_signature (type, sel, parms, ellipsis);
6510 /* Parse an objc-type-name.
6512 objc-type-name:
6513 objc-type-qualifiers[opt] type-name
6514 objc-type-qualifiers[opt]
6516 objc-type-qualifiers:
6517 objc-type-qualifier
6518 objc-type-qualifiers objc-type-qualifier
6520 objc-type-qualifier: one of
6521 in out inout bycopy byref oneway
6524 static tree
6525 c_parser_objc_type_name (c_parser *parser)
6527 tree quals = NULL_TREE;
6528 struct c_type_name *type_name = NULL;
6529 tree type = NULL_TREE;
6530 while (true)
6532 c_token *token = c_parser_peek_token (parser);
6533 if (token->type == CPP_KEYWORD
6534 && (token->keyword == RID_IN
6535 || token->keyword == RID_OUT
6536 || token->keyword == RID_INOUT
6537 || token->keyword == RID_BYCOPY
6538 || token->keyword == RID_BYREF
6539 || token->keyword == RID_ONEWAY))
6541 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6542 c_parser_consume_token (parser);
6544 else
6545 break;
6547 if (c_parser_next_token_starts_typename (parser))
6548 type_name = c_parser_type_name (parser);
6549 if (type_name)
6550 type = groktypename (type_name, NULL, NULL);
6551 return build_tree_list (quals, type);
6554 /* Parse objc-protocol-refs.
6556 objc-protocol-refs:
6557 < identifier-list >
6560 static tree
6561 c_parser_objc_protocol_refs (c_parser *parser)
6563 tree list = NULL_TREE;
6564 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6565 c_parser_consume_token (parser);
6566 /* Any identifiers, including those declared as type names, are OK
6567 here. */
6568 while (true)
6570 tree id;
6571 if (c_parser_next_token_is_not (parser, CPP_NAME))
6573 c_parser_error (parser, "expected identifier");
6574 break;
6576 id = c_parser_peek_token (parser)->value;
6577 list = chainon (list, build_tree_list (NULL_TREE, id));
6578 c_parser_consume_token (parser);
6579 if (c_parser_next_token_is (parser, CPP_COMMA))
6580 c_parser_consume_token (parser);
6581 else
6582 break;
6584 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6585 return list;
6588 /* Parse an objc-try-catch-statement.
6590 objc-try-catch-statement:
6591 @try compound-statement objc-catch-list[opt]
6592 @try compound-statement objc-catch-list[opt] @finally compound-statement
6594 objc-catch-list:
6595 @catch ( parameter-declaration ) compound-statement
6596 objc-catch-list @catch ( parameter-declaration ) compound-statement
6599 static void
6600 c_parser_objc_try_catch_statement (c_parser *parser)
6602 location_t loc;
6603 tree stmt;
6604 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6605 c_parser_consume_token (parser);
6606 loc = c_parser_peek_token (parser)->location;
6607 stmt = c_parser_compound_statement (parser);
6608 objc_begin_try_stmt (loc, stmt);
6609 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6611 struct c_parm *parm;
6612 c_parser_consume_token (parser);
6613 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6614 break;
6615 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6616 if (parm == NULL)
6618 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6619 break;
6621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6622 objc_begin_catch_clause (grokparm (parm));
6623 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6624 c_parser_compound_statement_nostart (parser);
6625 objc_finish_catch_clause ();
6627 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6629 location_t finloc;
6630 tree finstmt;
6631 c_parser_consume_token (parser);
6632 finloc = c_parser_peek_token (parser)->location;
6633 finstmt = c_parser_compound_statement (parser);
6634 objc_build_finally_clause (finloc, finstmt);
6636 objc_finish_try_stmt ();
6639 /* Parse an objc-synchronized-statement.
6641 objc-synchronized-statement:
6642 @synchronized ( expression ) compound-statement
6645 static void
6646 c_parser_objc_synchronized_statement (c_parser *parser)
6648 location_t loc;
6649 tree expr, stmt;
6650 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6651 c_parser_consume_token (parser);
6652 loc = c_parser_peek_token (parser)->location;
6653 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6655 expr = c_parser_expression (parser).value;
6656 expr = c_fully_fold (expr, false, NULL);
6657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6659 else
6660 expr = error_mark_node;
6661 stmt = c_parser_compound_statement (parser);
6662 objc_build_synchronized (loc, expr, stmt);
6665 /* Parse an objc-selector; return NULL_TREE without an error if the
6666 next token is not an objc-selector.
6668 objc-selector:
6669 identifier
6670 one of
6671 enum struct union if else while do for switch case default
6672 break continue return goto asm sizeof typeof __alignof
6673 unsigned long const short volatile signed restrict _Complex
6674 in out inout bycopy byref oneway int char float double void _Bool
6676 ??? Why this selection of keywords but not, for example, storage
6677 class specifiers? */
6679 static tree
6680 c_parser_objc_selector (c_parser *parser)
6682 c_token *token = c_parser_peek_token (parser);
6683 tree value = token->value;
6684 if (token->type == CPP_NAME)
6686 c_parser_consume_token (parser);
6687 return value;
6689 if (token->type != CPP_KEYWORD)
6690 return NULL_TREE;
6691 switch (token->keyword)
6693 case RID_ENUM:
6694 case RID_STRUCT:
6695 case RID_UNION:
6696 case RID_IF:
6697 case RID_ELSE:
6698 case RID_WHILE:
6699 case RID_DO:
6700 case RID_FOR:
6701 case RID_SWITCH:
6702 case RID_CASE:
6703 case RID_DEFAULT:
6704 case RID_BREAK:
6705 case RID_CONTINUE:
6706 case RID_RETURN:
6707 case RID_GOTO:
6708 case RID_ASM:
6709 case RID_SIZEOF:
6710 case RID_TYPEOF:
6711 case RID_ALIGNOF:
6712 case RID_UNSIGNED:
6713 case RID_LONG:
6714 case RID_CONST:
6715 case RID_SHORT:
6716 case RID_VOLATILE:
6717 case RID_SIGNED:
6718 case RID_RESTRICT:
6719 case RID_COMPLEX:
6720 case RID_IN:
6721 case RID_OUT:
6722 case RID_INOUT:
6723 case RID_BYCOPY:
6724 case RID_BYREF:
6725 case RID_ONEWAY:
6726 case RID_INT:
6727 case RID_CHAR:
6728 case RID_FLOAT:
6729 case RID_DOUBLE:
6730 case RID_VOID:
6731 case RID_BOOL:
6732 c_parser_consume_token (parser);
6733 return value;
6734 default:
6735 return NULL_TREE;
6739 /* Parse an objc-selector-arg.
6741 objc-selector-arg:
6742 objc-selector
6743 objc-keywordname-list
6745 objc-keywordname-list:
6746 objc-keywordname
6747 objc-keywordname-list objc-keywordname
6749 objc-keywordname:
6750 objc-selector :
6754 static tree
6755 c_parser_objc_selector_arg (c_parser *parser)
6757 tree sel = c_parser_objc_selector (parser);
6758 tree list = NULL_TREE;
6759 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6760 return sel;
6761 while (true)
6763 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6764 return list;
6765 list = chainon (list, build_tree_list (sel, NULL_TREE));
6766 sel = c_parser_objc_selector (parser);
6767 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6768 break;
6770 return list;
6773 /* Parse an objc-receiver.
6775 objc-receiver:
6776 expression
6777 class-name
6778 type-name
6781 static tree
6782 c_parser_objc_receiver (c_parser *parser)
6784 if (c_parser_peek_token (parser)->type == CPP_NAME
6785 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6786 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6788 tree id = c_parser_peek_token (parser)->value;
6789 c_parser_consume_token (parser);
6790 return objc_get_class_reference (id);
6792 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
6795 /* Parse objc-message-args.
6797 objc-message-args:
6798 objc-selector
6799 objc-keywordarg-list
6801 objc-keywordarg-list:
6802 objc-keywordarg
6803 objc-keywordarg-list objc-keywordarg
6805 objc-keywordarg:
6806 objc-selector : objc-keywordexpr
6807 : objc-keywordexpr
6810 static tree
6811 c_parser_objc_message_args (c_parser *parser)
6813 tree sel = c_parser_objc_selector (parser);
6814 tree list = NULL_TREE;
6815 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6816 return sel;
6817 while (true)
6819 tree keywordexpr;
6820 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6821 return error_mark_node;
6822 keywordexpr = c_parser_objc_keywordexpr (parser);
6823 list = chainon (list, build_tree_list (sel, keywordexpr));
6824 sel = c_parser_objc_selector (parser);
6825 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6826 break;
6828 return list;
6831 /* Parse an objc-keywordexpr.
6833 objc-keywordexpr:
6834 nonempty-expr-list
6837 static tree
6838 c_parser_objc_keywordexpr (c_parser *parser)
6840 tree ret;
6841 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
6842 if (VEC_length (tree, expr_list) == 1)
6844 /* Just return the expression, remove a level of
6845 indirection. */
6846 ret = VEC_index (tree, expr_list, 0);
6848 else
6850 /* We have a comma expression, we will collapse later. */
6851 ret = build_tree_list_vec (expr_list);
6853 release_tree_vector (expr_list);
6854 return ret;
6858 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6859 should be considered, statements. ALLOW_STMT is true if we're within
6860 the context of a function and such pragmas are to be allowed. Returns
6861 true if we actually parsed such a pragma. */
6863 static bool
6864 c_parser_pragma (c_parser *parser, enum pragma_context context)
6866 unsigned int id;
6868 id = c_parser_peek_token (parser)->pragma_kind;
6869 gcc_assert (id != PRAGMA_NONE);
6871 switch (id)
6873 case PRAGMA_OMP_BARRIER:
6874 if (context != pragma_compound)
6876 if (context == pragma_stmt)
6877 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6878 "used in compound statements");
6879 goto bad_stmt;
6881 c_parser_omp_barrier (parser);
6882 return false;
6884 case PRAGMA_OMP_FLUSH:
6885 if (context != pragma_compound)
6887 if (context == pragma_stmt)
6888 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6889 "used in compound statements");
6890 goto bad_stmt;
6892 c_parser_omp_flush (parser);
6893 return false;
6895 case PRAGMA_OMP_TASKWAIT:
6896 if (context != pragma_compound)
6898 if (context == pragma_stmt)
6899 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
6900 "used in compound statements");
6901 goto bad_stmt;
6903 c_parser_omp_taskwait (parser);
6904 return false;
6906 case PRAGMA_OMP_THREADPRIVATE:
6907 c_parser_omp_threadprivate (parser);
6908 return false;
6910 case PRAGMA_OMP_SECTION:
6911 error_at (c_parser_peek_token (parser)->location,
6912 "%<#pragma omp section%> may only be used in "
6913 "%<#pragma omp sections%> construct");
6914 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6915 return false;
6917 case PRAGMA_GCC_PCH_PREPROCESS:
6918 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6919 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6920 return false;
6922 default:
6923 if (id < PRAGMA_FIRST_EXTERNAL)
6925 if (context == pragma_external)
6927 bad_stmt:
6928 c_parser_error (parser, "expected declaration specifiers");
6929 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6930 return false;
6932 c_parser_omp_construct (parser);
6933 return true;
6935 break;
6938 c_parser_consume_pragma (parser);
6939 c_invoke_pragma_handler (id);
6941 /* Skip to EOL, but suppress any error message. Those will have been
6942 generated by the handler routine through calling error, as opposed
6943 to calling c_parser_error. */
6944 parser->error = true;
6945 c_parser_skip_to_pragma_eol (parser);
6947 return false;
6950 /* The interface the pragma parsers have to the lexer. */
6952 enum cpp_ttype
6953 pragma_lex (tree *value)
6955 c_token *tok = c_parser_peek_token (the_parser);
6956 enum cpp_ttype ret = tok->type;
6958 *value = tok->value;
6959 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6960 ret = CPP_EOF;
6961 else
6963 if (ret == CPP_KEYWORD)
6964 ret = CPP_NAME;
6965 c_parser_consume_token (the_parser);
6968 return ret;
6971 static void
6972 c_parser_pragma_pch_preprocess (c_parser *parser)
6974 tree name = NULL;
6976 c_parser_consume_pragma (parser);
6977 if (c_parser_next_token_is (parser, CPP_STRING))
6979 name = c_parser_peek_token (parser)->value;
6980 c_parser_consume_token (parser);
6982 else
6983 c_parser_error (parser, "expected string literal");
6984 c_parser_skip_to_pragma_eol (parser);
6986 if (name)
6987 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6990 /* OpenMP 2.5 parsing routines. */
6992 /* Returns name of the next clause.
6993 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6994 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6995 returned and the token is consumed. */
6997 static pragma_omp_clause
6998 c_parser_omp_clause_name (c_parser *parser)
7000 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7002 if (c_parser_next_token_is_keyword (parser, RID_IF))
7003 result = PRAGMA_OMP_CLAUSE_IF;
7004 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7005 result = PRAGMA_OMP_CLAUSE_DEFAULT;
7006 else if (c_parser_next_token_is (parser, CPP_NAME))
7008 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7010 switch (p[0])
7012 case 'c':
7013 if (!strcmp ("collapse", p))
7014 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7015 else if (!strcmp ("copyin", p))
7016 result = PRAGMA_OMP_CLAUSE_COPYIN;
7017 else if (!strcmp ("copyprivate", p))
7018 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7019 break;
7020 case 'f':
7021 if (!strcmp ("firstprivate", p))
7022 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7023 break;
7024 case 'l':
7025 if (!strcmp ("lastprivate", p))
7026 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7027 break;
7028 case 'n':
7029 if (!strcmp ("nowait", p))
7030 result = PRAGMA_OMP_CLAUSE_NOWAIT;
7031 else if (!strcmp ("num_threads", p))
7032 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7033 break;
7034 case 'o':
7035 if (!strcmp ("ordered", p))
7036 result = PRAGMA_OMP_CLAUSE_ORDERED;
7037 break;
7038 case 'p':
7039 if (!strcmp ("private", p))
7040 result = PRAGMA_OMP_CLAUSE_PRIVATE;
7041 break;
7042 case 'r':
7043 if (!strcmp ("reduction", p))
7044 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7045 break;
7046 case 's':
7047 if (!strcmp ("schedule", p))
7048 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7049 else if (!strcmp ("shared", p))
7050 result = PRAGMA_OMP_CLAUSE_SHARED;
7051 break;
7052 case 'u':
7053 if (!strcmp ("untied", p))
7054 result = PRAGMA_OMP_CLAUSE_UNTIED;
7055 break;
7059 if (result != PRAGMA_OMP_CLAUSE_NONE)
7060 c_parser_consume_token (parser);
7062 return result;
7065 /* Validate that a clause of the given type does not already exist. */
7067 static void
7068 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7069 const char *name)
7071 tree c;
7073 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7074 if (OMP_CLAUSE_CODE (c) == code)
7076 location_t loc = OMP_CLAUSE_LOCATION (c);
7077 error_at (loc, "too many %qs clauses", name);
7078 break;
7082 /* OpenMP 2.5:
7083 variable-list:
7084 identifier
7085 variable-list , identifier
7087 If KIND is nonzero, create the appropriate node and install the
7088 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7089 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
7091 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7092 return the list created. */
7094 static tree
7095 c_parser_omp_variable_list (c_parser *parser,
7096 location_t clause_loc,
7097 enum omp_clause_code kind,
7098 tree list)
7100 if (c_parser_next_token_is_not (parser, CPP_NAME)
7101 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7102 c_parser_error (parser, "expected identifier");
7104 while (c_parser_next_token_is (parser, CPP_NAME)
7105 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7107 tree t = lookup_name (c_parser_peek_token (parser)->value);
7109 if (t == NULL_TREE)
7110 undeclared_variable (c_parser_peek_token (parser)->location,
7111 c_parser_peek_token (parser)->value);
7112 else if (t == error_mark_node)
7114 else if (kind != 0)
7116 tree u = build_omp_clause (clause_loc, kind);
7117 OMP_CLAUSE_DECL (u) = t;
7118 OMP_CLAUSE_CHAIN (u) = list;
7119 list = u;
7121 else
7122 list = tree_cons (t, NULL_TREE, list);
7124 c_parser_consume_token (parser);
7126 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7127 break;
7129 c_parser_consume_token (parser);
7132 return list;
7135 /* Similarly, but expect leading and trailing parenthesis. This is a very
7136 common case for omp clauses. */
7138 static tree
7139 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7140 tree list)
7142 /* The clauses location. */
7143 location_t loc = c_parser_peek_token (parser)->location;
7145 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7147 list = c_parser_omp_variable_list (parser, loc, kind, list);
7148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7150 return list;
7153 /* OpenMP 3.0:
7154 collapse ( constant-expression ) */
7156 static tree
7157 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7159 tree c, num = error_mark_node;
7160 HOST_WIDE_INT n;
7161 location_t loc;
7163 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7165 loc = c_parser_peek_token (parser)->location;
7166 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7168 num = c_parser_expr_no_commas (parser, NULL).value;
7169 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7171 if (num == error_mark_node)
7172 return list;
7173 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7174 || !host_integerp (num, 0)
7175 || (n = tree_low_cst (num, 0)) <= 0
7176 || (int) n != n)
7178 error_at (loc,
7179 "collapse argument needs positive constant integer expression");
7180 return list;
7182 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
7183 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7184 OMP_CLAUSE_CHAIN (c) = list;
7185 return c;
7188 /* OpenMP 2.5:
7189 copyin ( variable-list ) */
7191 static tree
7192 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7194 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7197 /* OpenMP 2.5:
7198 copyprivate ( variable-list ) */
7200 static tree
7201 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7203 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7206 /* OpenMP 2.5:
7207 default ( shared | none ) */
7209 static tree
7210 c_parser_omp_clause_default (c_parser *parser, tree list)
7212 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7213 location_t loc = c_parser_peek_token (parser)->location;
7214 tree c;
7216 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7217 return list;
7218 if (c_parser_next_token_is (parser, CPP_NAME))
7220 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7222 switch (p[0])
7224 case 'n':
7225 if (strcmp ("none", p) != 0)
7226 goto invalid_kind;
7227 kind = OMP_CLAUSE_DEFAULT_NONE;
7228 break;
7230 case 's':
7231 if (strcmp ("shared", p) != 0)
7232 goto invalid_kind;
7233 kind = OMP_CLAUSE_DEFAULT_SHARED;
7234 break;
7236 default:
7237 goto invalid_kind;
7240 c_parser_consume_token (parser);
7242 else
7244 invalid_kind:
7245 c_parser_error (parser, "expected %<none%> or %<shared%>");
7247 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7249 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7250 return list;
7252 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7253 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
7254 OMP_CLAUSE_CHAIN (c) = list;
7255 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7257 return c;
7260 /* OpenMP 2.5:
7261 firstprivate ( variable-list ) */
7263 static tree
7264 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7266 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7269 /* OpenMP 2.5:
7270 if ( expression ) */
7272 static tree
7273 c_parser_omp_clause_if (c_parser *parser, tree list)
7275 location_t loc = c_parser_peek_token (parser)->location;
7276 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7278 tree t = c_parser_paren_condition (parser);
7279 tree c;
7281 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7283 c = build_omp_clause (loc, OMP_CLAUSE_IF);
7284 OMP_CLAUSE_IF_EXPR (c) = t;
7285 OMP_CLAUSE_CHAIN (c) = list;
7286 list = c;
7288 else
7289 c_parser_error (parser, "expected %<(%>");
7291 return list;
7294 /* OpenMP 2.5:
7295 lastprivate ( variable-list ) */
7297 static tree
7298 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7300 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7303 /* OpenMP 2.5:
7304 nowait */
7306 static tree
7307 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7309 tree c;
7310 location_t loc = c_parser_peek_token (parser)->location;
7312 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7314 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
7315 OMP_CLAUSE_CHAIN (c) = list;
7316 return c;
7319 /* OpenMP 2.5:
7320 num_threads ( expression ) */
7322 static tree
7323 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7325 location_t num_threads_loc = c_parser_peek_token (parser)->location;
7326 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7328 location_t expr_loc = c_parser_peek_token (parser)->location;
7329 tree c, t = c_parser_expression (parser).value;
7330 t = c_fully_fold (t, false, NULL);
7332 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7334 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7336 c_parser_error (parser, "expected integer expression");
7337 return list;
7340 /* Attempt to statically determine when the number isn't positive. */
7341 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
7342 build_int_cst (TREE_TYPE (t), 0));
7343 if (CAN_HAVE_LOCATION_P (c))
7344 SET_EXPR_LOCATION (c, expr_loc);
7345 if (c == boolean_true_node)
7347 warning_at (expr_loc, 0,
7348 "%<num_threads%> value must be positive");
7349 t = integer_one_node;
7352 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7354 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
7355 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7356 OMP_CLAUSE_CHAIN (c) = list;
7357 list = c;
7360 return list;
7363 /* OpenMP 2.5:
7364 ordered */
7366 static tree
7367 c_parser_omp_clause_ordered (c_parser *parser, tree list)
7369 tree c;
7371 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7373 c = build_omp_clause (c_parser_peek_token (parser)->location,
7374 OMP_CLAUSE_ORDERED);
7375 OMP_CLAUSE_CHAIN (c) = list;
7377 return c;
7380 /* OpenMP 2.5:
7381 private ( variable-list ) */
7383 static tree
7384 c_parser_omp_clause_private (c_parser *parser, tree list)
7386 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7389 /* OpenMP 2.5:
7390 reduction ( reduction-operator : variable-list )
7392 reduction-operator:
7393 One of: + * - & ^ | && || */
7395 static tree
7396 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7398 location_t clause_loc = c_parser_peek_token (parser)->location;
7399 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7401 enum tree_code code;
7403 switch (c_parser_peek_token (parser)->type)
7405 case CPP_PLUS:
7406 code = PLUS_EXPR;
7407 break;
7408 case CPP_MULT:
7409 code = MULT_EXPR;
7410 break;
7411 case CPP_MINUS:
7412 code = MINUS_EXPR;
7413 break;
7414 case CPP_AND:
7415 code = BIT_AND_EXPR;
7416 break;
7417 case CPP_XOR:
7418 code = BIT_XOR_EXPR;
7419 break;
7420 case CPP_OR:
7421 code = BIT_IOR_EXPR;
7422 break;
7423 case CPP_AND_AND:
7424 code = TRUTH_ANDIF_EXPR;
7425 break;
7426 case CPP_OR_OR:
7427 code = TRUTH_ORIF_EXPR;
7428 break;
7429 default:
7430 c_parser_error (parser,
7431 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7432 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7433 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7434 return list;
7436 c_parser_consume_token (parser);
7437 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7439 tree nl, c;
7441 nl = c_parser_omp_variable_list (parser, clause_loc,
7442 OMP_CLAUSE_REDUCTION, list);
7443 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7444 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7446 list = nl;
7448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7450 return list;
7453 /* OpenMP 2.5:
7454 schedule ( schedule-kind )
7455 schedule ( schedule-kind , expression )
7457 schedule-kind:
7458 static | dynamic | guided | runtime | auto
7461 static tree
7462 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7464 tree c, t;
7465 location_t loc = c_parser_peek_token (parser)->location;
7467 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7468 return list;
7470 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
7472 if (c_parser_next_token_is (parser, CPP_NAME))
7474 tree kind = c_parser_peek_token (parser)->value;
7475 const char *p = IDENTIFIER_POINTER (kind);
7477 switch (p[0])
7479 case 'd':
7480 if (strcmp ("dynamic", p) != 0)
7481 goto invalid_kind;
7482 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7483 break;
7485 case 'g':
7486 if (strcmp ("guided", p) != 0)
7487 goto invalid_kind;
7488 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7489 break;
7491 case 'r':
7492 if (strcmp ("runtime", p) != 0)
7493 goto invalid_kind;
7494 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7495 break;
7497 default:
7498 goto invalid_kind;
7501 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7502 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7503 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7504 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7505 else
7506 goto invalid_kind;
7508 c_parser_consume_token (parser);
7509 if (c_parser_next_token_is (parser, CPP_COMMA))
7511 location_t here;
7512 c_parser_consume_token (parser);
7514 here = c_parser_peek_token (parser)->location;
7515 t = c_parser_expr_no_commas (parser, NULL).value;
7516 t = c_fully_fold (t, false, NULL);
7518 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7519 error_at (here, "schedule %<runtime%> does not take "
7520 "a %<chunk_size%> parameter");
7521 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7522 error_at (here,
7523 "schedule %<auto%> does not take "
7524 "a %<chunk_size%> parameter");
7525 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7526 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7527 else
7528 c_parser_error (parser, "expected integer expression");
7530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7532 else
7533 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7534 "expected %<,%> or %<)%>");
7536 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7537 OMP_CLAUSE_CHAIN (c) = list;
7538 return c;
7540 invalid_kind:
7541 c_parser_error (parser, "invalid schedule kind");
7542 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7543 return list;
7546 /* OpenMP 2.5:
7547 shared ( variable-list ) */
7549 static tree
7550 c_parser_omp_clause_shared (c_parser *parser, tree list)
7552 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7555 /* OpenMP 3.0:
7556 untied */
7558 static tree
7559 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7561 tree c;
7563 /* FIXME: Should we allow duplicates? */
7564 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7566 c = build_omp_clause (c_parser_peek_token (parser)->location,
7567 OMP_CLAUSE_UNTIED);
7568 OMP_CLAUSE_CHAIN (c) = list;
7570 return c;
7573 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7574 is a bitmask in MASK. Return the list of clauses found; the result
7575 of clause default goes in *pdefault. */
7577 static tree
7578 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7579 const char *where)
7581 tree clauses = NULL;
7582 bool first = true;
7584 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7586 location_t here;
7587 pragma_omp_clause c_kind;
7588 const char *c_name;
7589 tree prev = clauses;
7591 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7592 c_parser_consume_token (parser);
7594 first = false;
7595 here = c_parser_peek_token (parser)->location;
7596 c_kind = c_parser_omp_clause_name (parser);
7598 switch (c_kind)
7600 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7601 clauses = c_parser_omp_clause_collapse (parser, clauses);
7602 c_name = "collapse";
7603 break;
7604 case PRAGMA_OMP_CLAUSE_COPYIN:
7605 clauses = c_parser_omp_clause_copyin (parser, clauses);
7606 c_name = "copyin";
7607 break;
7608 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7609 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7610 c_name = "copyprivate";
7611 break;
7612 case PRAGMA_OMP_CLAUSE_DEFAULT:
7613 clauses = c_parser_omp_clause_default (parser, clauses);
7614 c_name = "default";
7615 break;
7616 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7617 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7618 c_name = "firstprivate";
7619 break;
7620 case PRAGMA_OMP_CLAUSE_IF:
7621 clauses = c_parser_omp_clause_if (parser, clauses);
7622 c_name = "if";
7623 break;
7624 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7625 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7626 c_name = "lastprivate";
7627 break;
7628 case PRAGMA_OMP_CLAUSE_NOWAIT:
7629 clauses = c_parser_omp_clause_nowait (parser, clauses);
7630 c_name = "nowait";
7631 break;
7632 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7633 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7634 c_name = "num_threads";
7635 break;
7636 case PRAGMA_OMP_CLAUSE_ORDERED:
7637 clauses = c_parser_omp_clause_ordered (parser, clauses);
7638 c_name = "ordered";
7639 break;
7640 case PRAGMA_OMP_CLAUSE_PRIVATE:
7641 clauses = c_parser_omp_clause_private (parser, clauses);
7642 c_name = "private";
7643 break;
7644 case PRAGMA_OMP_CLAUSE_REDUCTION:
7645 clauses = c_parser_omp_clause_reduction (parser, clauses);
7646 c_name = "reduction";
7647 break;
7648 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7649 clauses = c_parser_omp_clause_schedule (parser, clauses);
7650 c_name = "schedule";
7651 break;
7652 case PRAGMA_OMP_CLAUSE_SHARED:
7653 clauses = c_parser_omp_clause_shared (parser, clauses);
7654 c_name = "shared";
7655 break;
7656 case PRAGMA_OMP_CLAUSE_UNTIED:
7657 clauses = c_parser_omp_clause_untied (parser, clauses);
7658 c_name = "untied";
7659 break;
7660 default:
7661 c_parser_error (parser, "expected %<#pragma omp%> clause");
7662 goto saw_error;
7665 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7667 /* Remove the invalid clause(s) from the list to avoid
7668 confusing the rest of the compiler. */
7669 clauses = prev;
7670 error_at (here, "%qs is not valid for %qs", c_name, where);
7674 saw_error:
7675 c_parser_skip_to_pragma_eol (parser);
7677 return c_finish_omp_clauses (clauses);
7680 /* OpenMP 2.5:
7681 structured-block:
7682 statement
7684 In practice, we're also interested in adding the statement to an
7685 outer node. So it is convenient if we work around the fact that
7686 c_parser_statement calls add_stmt. */
7688 static tree
7689 c_parser_omp_structured_block (c_parser *parser)
7691 tree stmt = push_stmt_list ();
7692 c_parser_statement (parser);
7693 return pop_stmt_list (stmt);
7696 /* OpenMP 2.5:
7697 # pragma omp atomic new-line
7698 expression-stmt
7700 expression-stmt:
7701 x binop= expr | x++ | ++x | x-- | --x
7702 binop:
7703 +, *, -, /, &, ^, |, <<, >>
7705 where x is an lvalue expression with scalar type.
7707 LOC is the location of the #pragma token. */
7709 static void
7710 c_parser_omp_atomic (location_t loc, c_parser *parser)
7712 tree lhs, rhs;
7713 tree stmt;
7714 enum tree_code code;
7715 struct c_expr rhs_expr;
7717 c_parser_skip_to_pragma_eol (parser);
7719 lhs = c_parser_unary_expression (parser).value;
7720 lhs = c_fully_fold (lhs, false, NULL);
7721 switch (TREE_CODE (lhs))
7723 case ERROR_MARK:
7724 saw_error:
7725 c_parser_skip_to_end_of_block_or_statement (parser);
7726 return;
7728 case PREINCREMENT_EXPR:
7729 case POSTINCREMENT_EXPR:
7730 lhs = TREE_OPERAND (lhs, 0);
7731 code = PLUS_EXPR;
7732 rhs = integer_one_node;
7733 break;
7735 case PREDECREMENT_EXPR:
7736 case POSTDECREMENT_EXPR:
7737 lhs = TREE_OPERAND (lhs, 0);
7738 code = MINUS_EXPR;
7739 rhs = integer_one_node;
7740 break;
7742 default:
7743 switch (c_parser_peek_token (parser)->type)
7745 case CPP_MULT_EQ:
7746 code = MULT_EXPR;
7747 break;
7748 case CPP_DIV_EQ:
7749 code = TRUNC_DIV_EXPR;
7750 break;
7751 case CPP_PLUS_EQ:
7752 code = PLUS_EXPR;
7753 break;
7754 case CPP_MINUS_EQ:
7755 code = MINUS_EXPR;
7756 break;
7757 case CPP_LSHIFT_EQ:
7758 code = LSHIFT_EXPR;
7759 break;
7760 case CPP_RSHIFT_EQ:
7761 code = RSHIFT_EXPR;
7762 break;
7763 case CPP_AND_EQ:
7764 code = BIT_AND_EXPR;
7765 break;
7766 case CPP_OR_EQ:
7767 code = BIT_IOR_EXPR;
7768 break;
7769 case CPP_XOR_EQ:
7770 code = BIT_XOR_EXPR;
7771 break;
7772 default:
7773 c_parser_error (parser,
7774 "invalid operator for %<#pragma omp atomic%>");
7775 goto saw_error;
7778 c_parser_consume_token (parser);
7780 location_t rhs_loc = c_parser_peek_token (parser)->location;
7781 rhs_expr = c_parser_expression (parser);
7782 rhs_expr = default_function_array_conversion (rhs_loc, rhs_expr);
7784 rhs = rhs_expr.value;
7785 rhs = c_fully_fold (rhs, false, NULL);
7786 break;
7788 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
7789 if (stmt != error_mark_node)
7790 add_stmt (stmt);
7791 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7795 /* OpenMP 2.5:
7796 # pragma omp barrier new-line
7799 static void
7800 c_parser_omp_barrier (c_parser *parser)
7802 location_t loc = c_parser_peek_token (parser)->location;
7803 c_parser_consume_pragma (parser);
7804 c_parser_skip_to_pragma_eol (parser);
7806 c_finish_omp_barrier (loc);
7809 /* OpenMP 2.5:
7810 # pragma omp critical [(name)] new-line
7811 structured-block
7813 LOC is the location of the #pragma itself. */
7815 static tree
7816 c_parser_omp_critical (location_t loc, c_parser *parser)
7818 tree stmt, name = NULL;
7820 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7822 c_parser_consume_token (parser);
7823 if (c_parser_next_token_is (parser, CPP_NAME))
7825 name = c_parser_peek_token (parser)->value;
7826 c_parser_consume_token (parser);
7827 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7829 else
7830 c_parser_error (parser, "expected identifier");
7832 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7833 c_parser_error (parser, "expected %<(%> or end of line");
7834 c_parser_skip_to_pragma_eol (parser);
7836 stmt = c_parser_omp_structured_block (parser);
7837 return c_finish_omp_critical (loc, stmt, name);
7840 /* OpenMP 2.5:
7841 # pragma omp flush flush-vars[opt] new-line
7843 flush-vars:
7844 ( variable-list ) */
7846 static void
7847 c_parser_omp_flush (c_parser *parser)
7849 location_t loc = c_parser_peek_token (parser)->location;
7850 c_parser_consume_pragma (parser);
7851 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7852 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
7853 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7854 c_parser_error (parser, "expected %<(%> or end of line");
7855 c_parser_skip_to_pragma_eol (parser);
7857 c_finish_omp_flush (loc);
7860 /* Parse the restricted form of the for statement allowed by OpenMP.
7861 The real trick here is to determine the loop control variable early
7862 so that we can push a new decl if necessary to make it private.
7863 LOC is the location of the OMP in "#pragma omp". */
7865 static tree
7866 c_parser_omp_for_loop (location_t loc,
7867 c_parser *parser, tree clauses, tree *par_clauses)
7869 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7870 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
7871 bool fail = false, open_brace_parsed = false;
7872 int i, collapse = 1, nbraces = 0;
7873 location_t for_loc;
7875 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
7876 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
7877 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
7879 gcc_assert (collapse >= 1);
7881 declv = make_tree_vec (collapse);
7882 initv = make_tree_vec (collapse);
7883 condv = make_tree_vec (collapse);
7884 incrv = make_tree_vec (collapse);
7886 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7888 c_parser_error (parser, "for statement expected");
7889 return NULL;
7891 for_loc = c_parser_peek_token (parser)->location;
7892 c_parser_consume_token (parser);
7894 for (i = 0; i < collapse; i++)
7896 int bracecount = 0;
7898 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7899 goto pop_scopes;
7901 /* Parse the initialization declaration or expression. */
7902 if (c_parser_next_token_starts_declspecs (parser))
7904 if (i > 0)
7905 for_block
7906 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
7907 c_parser_declaration_or_fndef (parser, true, true, true, true);
7908 decl = check_for_loop_decls (for_loc);
7909 if (decl == NULL)
7910 goto error_init;
7911 if (DECL_INITIAL (decl) == error_mark_node)
7912 decl = error_mark_node;
7913 init = decl;
7915 else if (c_parser_next_token_is (parser, CPP_NAME)
7916 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7918 struct c_expr decl_exp;
7919 struct c_expr init_exp;
7920 location_t init_loc;
7922 decl_exp = c_parser_postfix_expression (parser);
7923 decl = decl_exp.value;
7925 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7927 init_loc = c_parser_peek_token (parser)->location;
7928 init_exp = c_parser_expr_no_commas (parser, NULL);
7929 init_exp = default_function_array_conversion (init_loc, init_exp);
7930 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
7931 NOP_EXPR, init_loc, init_exp.value,
7932 init_exp.original_type);
7933 init = c_process_expr_stmt (init_loc, init);
7935 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7937 else
7939 error_init:
7940 c_parser_error (parser,
7941 "expected iteration declaration or initialization");
7942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7943 "expected %<)%>");
7944 fail = true;
7945 goto parse_next;
7948 /* Parse the loop condition. */
7949 cond = NULL_TREE;
7950 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7952 location_t cond_loc = c_parser_peek_token (parser)->location;
7953 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
7955 cond = cond_expr.value;
7956 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
7957 cond = c_fully_fold (cond, false, NULL);
7958 switch (cond_expr.original_code)
7960 case GT_EXPR:
7961 case GE_EXPR:
7962 case LT_EXPR:
7963 case LE_EXPR:
7964 break;
7965 default:
7966 /* Can't be cond = error_mark_node, because we want to preserve
7967 the location until c_finish_omp_for. */
7968 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
7969 break;
7971 protected_set_expr_location (cond, cond_loc);
7973 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7975 /* Parse the increment expression. */
7976 incr = NULL_TREE;
7977 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7979 location_t incr_loc = c_parser_peek_token (parser)->location;
7981 incr = c_process_expr_stmt (incr_loc,
7982 c_parser_expression (parser).value);
7984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7986 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
7987 fail = true;
7988 else
7990 TREE_VEC_ELT (declv, i) = decl;
7991 TREE_VEC_ELT (initv, i) = init;
7992 TREE_VEC_ELT (condv, i) = cond;
7993 TREE_VEC_ELT (incrv, i) = incr;
7996 parse_next:
7997 if (i == collapse - 1)
7998 break;
8000 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8001 in between the collapsed for loops to be still considered perfectly
8002 nested. Hopefully the final version clarifies this.
8003 For now handle (multiple) {'s and empty statements. */
8006 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8008 c_parser_consume_token (parser);
8009 break;
8011 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8013 c_parser_consume_token (parser);
8014 bracecount++;
8016 else if (bracecount
8017 && c_parser_next_token_is (parser, CPP_SEMICOLON))
8018 c_parser_consume_token (parser);
8019 else
8021 c_parser_error (parser, "not enough perfectly nested loops");
8022 if (bracecount)
8024 open_brace_parsed = true;
8025 bracecount--;
8027 fail = true;
8028 collapse = 0;
8029 break;
8032 while (1);
8034 nbraces += bracecount;
8037 save_break = c_break_label;
8038 c_break_label = size_one_node;
8039 save_cont = c_cont_label;
8040 c_cont_label = NULL_TREE;
8041 body = push_stmt_list ();
8043 if (open_brace_parsed)
8045 location_t here = c_parser_peek_token (parser)->location;
8046 stmt = c_begin_compound_stmt (true);
8047 c_parser_compound_statement_nostart (parser);
8048 add_stmt (c_end_compound_stmt (here, stmt, true));
8050 else
8051 add_stmt (c_parser_c99_block_statement (parser));
8052 if (c_cont_label)
8054 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8055 SET_EXPR_LOCATION (t, loc);
8056 add_stmt (t);
8059 body = pop_stmt_list (body);
8060 c_break_label = save_break;
8061 c_cont_label = save_cont;
8063 while (nbraces)
8065 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8067 c_parser_consume_token (parser);
8068 nbraces--;
8070 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8071 c_parser_consume_token (parser);
8072 else
8074 c_parser_error (parser, "collapsed loops not perfectly nested");
8075 while (nbraces)
8077 location_t here = c_parser_peek_token (parser)->location;
8078 stmt = c_begin_compound_stmt (true);
8079 add_stmt (body);
8080 c_parser_compound_statement_nostart (parser);
8081 body = c_end_compound_stmt (here, stmt, true);
8082 nbraces--;
8084 goto pop_scopes;
8088 /* Only bother calling c_finish_omp_for if we haven't already generated
8089 an error from the initialization parsing. */
8090 if (!fail)
8092 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8093 if (stmt)
8095 if (par_clauses != NULL)
8097 tree *c;
8098 for (c = par_clauses; *c ; )
8099 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8100 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8101 c = &OMP_CLAUSE_CHAIN (*c);
8102 else
8104 for (i = 0; i < collapse; i++)
8105 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8106 break;
8107 if (i == collapse)
8108 c = &OMP_CLAUSE_CHAIN (*c);
8109 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8111 error_at (loc,
8112 "iteration variable %qD should not be firstprivate",
8113 OMP_CLAUSE_DECL (*c));
8114 *c = OMP_CLAUSE_CHAIN (*c);
8116 else
8118 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8119 change it to shared (decl) in
8120 OMP_PARALLEL_CLAUSES. */
8121 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8122 OMP_CLAUSE_LASTPRIVATE);
8123 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8124 OMP_CLAUSE_CHAIN (l) = clauses;
8125 clauses = l;
8126 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8130 OMP_FOR_CLAUSES (stmt) = clauses;
8132 ret = stmt;
8134 pop_scopes:
8135 while (for_block)
8137 /* FIXME diagnostics: LOC below should be the actual location of
8138 this particular for block. We need to build a list of
8139 locations to go along with FOR_BLOCK. */
8140 stmt = c_end_compound_stmt (loc, TREE_VALUE (for_block), true);
8141 add_stmt (stmt);
8142 for_block = TREE_CHAIN (for_block);
8144 return ret;
8147 /* OpenMP 2.5:
8148 #pragma omp for for-clause[optseq] new-line
8149 for-loop
8151 LOC is the location of the #pragma token.
8154 #define OMP_FOR_CLAUSE_MASK \
8155 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8156 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8157 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8158 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8159 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8160 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
8161 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
8162 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8164 static tree
8165 c_parser_omp_for (location_t loc, c_parser *parser)
8167 tree block, clauses, ret;
8169 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8170 "#pragma omp for");
8172 block = c_begin_compound_stmt (true);
8173 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8174 block = c_end_compound_stmt (loc, block, true);
8175 add_stmt (block);
8177 return ret;
8180 /* OpenMP 2.5:
8181 # pragma omp master new-line
8182 structured-block
8184 LOC is the location of the #pragma token.
8187 static tree
8188 c_parser_omp_master (location_t loc, c_parser *parser)
8190 c_parser_skip_to_pragma_eol (parser);
8191 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
8194 /* OpenMP 2.5:
8195 # pragma omp ordered new-line
8196 structured-block
8198 LOC is the location of the #pragma itself.
8201 static tree
8202 c_parser_omp_ordered (location_t loc, c_parser *parser)
8204 c_parser_skip_to_pragma_eol (parser);
8205 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
8208 /* OpenMP 2.5:
8210 section-scope:
8211 { section-sequence }
8213 section-sequence:
8214 section-directive[opt] structured-block
8215 section-sequence section-directive structured-block
8217 SECTIONS_LOC is the location of the #pragma omp sections. */
8219 static tree
8220 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
8222 tree stmt, substmt;
8223 bool error_suppress = false;
8224 location_t loc;
8226 loc = c_parser_peek_token (parser)->location;
8227 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8229 /* Avoid skipping until the end of the block. */
8230 parser->error = false;
8231 return NULL_TREE;
8234 stmt = push_stmt_list ();
8236 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8238 substmt = push_stmt_list ();
8240 while (1)
8242 c_parser_statement (parser);
8244 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8245 break;
8246 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8247 break;
8248 if (c_parser_next_token_is (parser, CPP_EOF))
8249 break;
8252 substmt = pop_stmt_list (substmt);
8253 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8254 SET_EXPR_LOCATION (substmt, loc);
8255 add_stmt (substmt);
8258 while (1)
8260 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8261 break;
8262 if (c_parser_next_token_is (parser, CPP_EOF))
8263 break;
8265 loc = c_parser_peek_token (parser)->location;
8266 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8268 c_parser_consume_pragma (parser);
8269 c_parser_skip_to_pragma_eol (parser);
8270 error_suppress = false;
8272 else if (!error_suppress)
8274 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8275 error_suppress = true;
8278 substmt = c_parser_omp_structured_block (parser);
8279 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8280 SET_EXPR_LOCATION (substmt, loc);
8281 add_stmt (substmt);
8283 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8284 "expected %<#pragma omp section%> or %<}%>");
8286 substmt = pop_stmt_list (stmt);
8288 stmt = make_node (OMP_SECTIONS);
8289 SET_EXPR_LOCATION (stmt, sections_loc);
8290 TREE_TYPE (stmt) = void_type_node;
8291 OMP_SECTIONS_BODY (stmt) = substmt;
8293 return add_stmt (stmt);
8296 /* OpenMP 2.5:
8297 # pragma omp sections sections-clause[optseq] newline
8298 sections-scope
8300 LOC is the location of the #pragma token.
8303 #define OMP_SECTIONS_CLAUSE_MASK \
8304 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8305 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8306 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8307 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8308 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8310 static tree
8311 c_parser_omp_sections (location_t loc, c_parser *parser)
8313 tree block, clauses, ret;
8315 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8316 "#pragma omp sections");
8318 block = c_begin_compound_stmt (true);
8319 ret = c_parser_omp_sections_scope (loc, parser);
8320 if (ret)
8321 OMP_SECTIONS_CLAUSES (ret) = clauses;
8322 block = c_end_compound_stmt (loc, block, true);
8323 add_stmt (block);
8325 return ret;
8328 /* OpenMP 2.5:
8329 # pragma parallel parallel-clause new-line
8330 # pragma parallel for parallel-for-clause new-line
8331 # pragma parallel sections parallel-sections-clause new-line
8333 LOC is the location of the #pragma token.
8336 #define OMP_PARALLEL_CLAUSE_MASK \
8337 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8338 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8339 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8340 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8341 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8342 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8343 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8344 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8346 static tree
8347 c_parser_omp_parallel (location_t loc, c_parser *parser)
8349 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8350 const char *p_name = "#pragma omp parallel";
8351 tree stmt, clauses, par_clause, ws_clause, block;
8352 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8354 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8356 c_parser_consume_token (parser);
8357 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8358 p_name = "#pragma omp parallel for";
8359 mask |= OMP_FOR_CLAUSE_MASK;
8360 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8362 else if (c_parser_next_token_is (parser, CPP_NAME))
8364 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8365 if (strcmp (p, "sections") == 0)
8367 c_parser_consume_token (parser);
8368 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8369 p_name = "#pragma omp parallel sections";
8370 mask |= OMP_SECTIONS_CLAUSE_MASK;
8371 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8375 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8377 switch (p_kind)
8379 case PRAGMA_OMP_PARALLEL:
8380 block = c_begin_omp_parallel ();
8381 c_parser_statement (parser);
8382 stmt = c_finish_omp_parallel (loc, clauses, block);
8383 break;
8385 case PRAGMA_OMP_PARALLEL_FOR:
8386 block = c_begin_omp_parallel ();
8387 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8388 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
8389 stmt = c_finish_omp_parallel (loc, par_clause, block);
8390 OMP_PARALLEL_COMBINED (stmt) = 1;
8391 break;
8393 case PRAGMA_OMP_PARALLEL_SECTIONS:
8394 block = c_begin_omp_parallel ();
8395 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8396 stmt = c_parser_omp_sections_scope (loc, parser);
8397 if (stmt)
8398 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8399 stmt = c_finish_omp_parallel (loc, par_clause, block);
8400 OMP_PARALLEL_COMBINED (stmt) = 1;
8401 break;
8403 default:
8404 gcc_unreachable ();
8407 return stmt;
8410 /* OpenMP 2.5:
8411 # pragma omp single single-clause[optseq] new-line
8412 structured-block
8414 LOC is the location of the #pragma.
8417 #define OMP_SINGLE_CLAUSE_MASK \
8418 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8419 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8420 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8421 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8423 static tree
8424 c_parser_omp_single (location_t loc, c_parser *parser)
8426 tree stmt = make_node (OMP_SINGLE);
8427 SET_EXPR_LOCATION (stmt, loc);
8428 TREE_TYPE (stmt) = void_type_node;
8430 OMP_SINGLE_CLAUSES (stmt)
8431 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8432 "#pragma omp single");
8433 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8435 return add_stmt (stmt);
8438 /* OpenMP 3.0:
8439 # pragma omp task task-clause[optseq] new-line
8441 LOC is the location of the #pragma.
8444 #define OMP_TASK_CLAUSE_MASK \
8445 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8446 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8447 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8448 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8449 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8450 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8452 static tree
8453 c_parser_omp_task (location_t loc, c_parser *parser)
8455 tree clauses, block;
8457 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8458 "#pragma omp task");
8460 block = c_begin_omp_task ();
8461 c_parser_statement (parser);
8462 return c_finish_omp_task (loc, clauses, block);
8465 /* OpenMP 3.0:
8466 # pragma omp taskwait new-line
8469 static void
8470 c_parser_omp_taskwait (c_parser *parser)
8472 location_t loc = c_parser_peek_token (parser)->location;
8473 c_parser_consume_pragma (parser);
8474 c_parser_skip_to_pragma_eol (parser);
8476 c_finish_omp_taskwait (loc);
8479 /* Main entry point to parsing most OpenMP pragmas. */
8481 static void
8482 c_parser_omp_construct (c_parser *parser)
8484 enum pragma_kind p_kind;
8485 location_t loc;
8486 tree stmt;
8488 loc = c_parser_peek_token (parser)->location;
8489 p_kind = c_parser_peek_token (parser)->pragma_kind;
8490 c_parser_consume_pragma (parser);
8492 /* For all constructs below except #pragma omp atomic
8493 MUST_NOT_THROW catch handlers are needed when exceptions
8494 are enabled. */
8495 if (p_kind != PRAGMA_OMP_ATOMIC)
8496 c_maybe_initialize_eh ();
8498 switch (p_kind)
8500 case PRAGMA_OMP_ATOMIC:
8501 c_parser_omp_atomic (loc, parser);
8502 return;
8503 case PRAGMA_OMP_CRITICAL:
8504 stmt = c_parser_omp_critical (loc, parser);
8505 break;
8506 case PRAGMA_OMP_FOR:
8507 stmt = c_parser_omp_for (loc, parser);
8508 break;
8509 case PRAGMA_OMP_MASTER:
8510 stmt = c_parser_omp_master (loc, parser);
8511 break;
8512 case PRAGMA_OMP_ORDERED:
8513 stmt = c_parser_omp_ordered (loc, parser);
8514 break;
8515 case PRAGMA_OMP_PARALLEL:
8516 stmt = c_parser_omp_parallel (loc, parser);
8517 break;
8518 case PRAGMA_OMP_SECTIONS:
8519 stmt = c_parser_omp_sections (loc, parser);
8520 break;
8521 case PRAGMA_OMP_SINGLE:
8522 stmt = c_parser_omp_single (loc, parser);
8523 break;
8524 case PRAGMA_OMP_TASK:
8525 stmt = c_parser_omp_task (loc, parser);
8526 break;
8527 default:
8528 gcc_unreachable ();
8531 if (stmt)
8532 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
8536 /* OpenMP 2.5:
8537 # pragma omp threadprivate (variable-list) */
8539 static void
8540 c_parser_omp_threadprivate (c_parser *parser)
8542 tree vars, t;
8543 location_t loc;
8545 c_parser_consume_pragma (parser);
8546 loc = c_parser_peek_token (parser)->location;
8547 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8549 /* Mark every variable in VARS to be assigned thread local storage. */
8550 for (t = vars; t; t = TREE_CHAIN (t))
8552 tree v = TREE_PURPOSE (t);
8554 /* FIXME diagnostics: Ideally we should keep individual
8555 locations for all the variables in the var list to make the
8556 following errors more precise. Perhaps
8557 c_parser_omp_var_list_parens() should construct a list of
8558 locations to go along with the var list. */
8560 /* If V had already been marked threadprivate, it doesn't matter
8561 whether it had been used prior to this point. */
8562 if (TREE_CODE (v) != VAR_DECL)
8563 error_at (loc, "%qD is not a variable", v);
8564 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8565 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
8566 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8567 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
8568 else if (TREE_TYPE (v) == error_mark_node)
8570 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8571 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
8572 else
8574 if (! DECL_THREAD_LOCAL_P (v))
8576 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8577 /* If rtl has been already set for this var, call
8578 make_decl_rtl once again, so that encode_section_info
8579 has a chance to look at the new decl flags. */
8580 if (DECL_RTL_SET_P (v))
8581 make_decl_rtl (v);
8583 C_DECL_THREADPRIVATE_P (v) = 1;
8587 c_parser_skip_to_pragma_eol (parser);
8591 /* Parse a single source file. */
8593 void
8594 c_parse_file (void)
8596 /* Use local storage to begin. If the first token is a pragma, parse it.
8597 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8598 which will cause garbage collection. */
8599 c_parser tparser;
8601 memset (&tparser, 0, sizeof tparser);
8602 the_parser = &tparser;
8604 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8605 c_parser_pragma_pch_preprocess (&tparser);
8607 the_parser = GGC_NEW (c_parser);
8608 *the_parser = tparser;
8610 c_parser_translation_unit (the_parser);
8611 the_parser = NULL;
8614 #include "gt-c-parser.h"