* gcc.dg/vect/slp-perm-1.c (main): Make sure loops aren't vectorized.
[official-gcc.git] / gcc / c-parser.c
blob306d46ba65f28491f6bc415cb3edcddc72101d3a
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h" /* For rtl.h: needs enum reg_class. */
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-family/c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "toplev.h"
54 #include "ggc.h"
55 #include "c-family/c-common.h"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
59 #include "plugin.h"
62 /* Initialization routine for this file. */
64 void
65 c_parse_init (void)
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
71 int mask = 0;
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
77 mask |= D_CXXONLY;
78 if (!flag_isoc99)
79 mask |= D_C99;
80 if (flag_no_asm)
82 mask |= D_ASM | D_EXT;
83 if (!flag_isoc99)
84 mask |= D_EXT89;
86 if (!c_dialect_objc ())
87 mask |= D_OBJC | D_CXX_OBJC;
89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90 for (i = 0; i < num_c_common_reswords; i++)
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
94 if (c_common_reswords[i].disable & mask)
96 if (warn_cxx_compat
97 && (c_common_reswords[i].disable & D_CXXWARN))
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
103 continue;
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
108 C_IS_RESERVED_WORD (id) = 1;
109 ridpointers [(int) c_common_reswords[i].rid] = id;
113 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
124 /* The following local token type is used. */
126 /* A keyword. */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
129 /* More information about the type of a CPP_NAME token. */
130 typedef enum c_id_kind {
131 /* An ordinary identifier. */
132 C_ID_ID,
133 /* An identifier declared as a typedef name. */
134 C_ID_TYPENAME,
135 /* An identifier declared as an Objective-C class name. */
136 C_ID_CLASSNAME,
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
139 /* Not an identifier. */
140 C_ID_NONE
141 } c_id_kind;
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
158 location_t location;
159 /* The value associated with this token, if any. */
160 tree value;
161 } c_token;
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 typedef struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
168 c_token tokens[2];
169 /* How many look-ahead tokens are available (0, 1 or 2). */
170 short tokens_avail;
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
182 /* Objective-C specific parser/lexer information. */
183 BOOL_BITFIELD objc_pq_context : 1;
184 /* The following flag is needed to contextualize Objective-C lexical
185 analysis. In some cases (e.g., 'int NSObject;'), it is
186 undesirable to bind an identifier to an Objective-C class, even
187 if a class with that name exists. */
188 BOOL_BITFIELD objc_need_raw_identifier : 1;
189 } c_parser;
192 /* The actual parser and external interface. ??? Does this need to be
193 garbage-collected? */
195 static GTY (()) c_parser *the_parser;
198 /* Read in and lex a single token, storing it in *TOKEN. */
200 static void
201 c_lex_one_token (c_parser *parser, c_token *token)
203 timevar_push (TV_LEX);
205 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
206 (parser->lex_untranslated_string
207 ? C_LEX_STRING_NO_TRANSLATE : 0));
208 token->id_kind = C_ID_NONE;
209 token->keyword = RID_MAX;
210 token->pragma_kind = PRAGMA_NONE;
212 switch (token->type)
214 case CPP_NAME:
216 tree decl;
218 bool objc_force_identifier = parser->objc_need_raw_identifier;
219 if (c_dialect_objc ())
220 parser->objc_need_raw_identifier = false;
222 if (C_IS_RESERVED_WORD (token->value))
224 enum rid rid_code = C_RID_CODE (token->value);
226 if (rid_code == RID_CXX_COMPAT_WARN)
228 warning_at (token->location,
229 OPT_Wc___compat,
230 "identifier %qE conflicts with C++ keyword",
231 token->value);
233 else if (rid_code >= RID_FIRST_ADDR_SPACE
234 && rid_code <= RID_LAST_ADDR_SPACE)
236 token->id_kind = C_ID_ADDRSPACE;
237 token->keyword = rid_code;
238 break;
240 else if (c_dialect_objc ())
242 if (!objc_is_reserved_word (token->value)
243 && (!OBJC_IS_PQ_KEYWORD (rid_code)
244 || parser->objc_pq_context))
246 /* Return the canonical spelling for this keyword. */
247 token->value = ridpointers[(int) rid_code];
248 token->type = CPP_KEYWORD;
249 token->keyword = rid_code;
250 break;
253 else
255 token->type = CPP_KEYWORD;
256 token->keyword = rid_code;
257 break;
261 decl = lookup_name (token->value);
262 if (decl)
264 if (TREE_CODE (decl) == TYPE_DECL)
266 token->id_kind = C_ID_TYPENAME;
267 break;
270 else if (c_dialect_objc ())
272 tree objc_interface_decl = objc_is_class_name (token->value);
273 /* Objective-C class names are in the same namespace as
274 variables and typedefs, and hence are shadowed by local
275 declarations. */
276 if (objc_interface_decl
277 && (global_bindings_p ()
278 || (!objc_force_identifier && !decl)))
280 token->value = objc_interface_decl;
281 token->id_kind = C_ID_CLASSNAME;
282 break;
285 token->id_kind = C_ID_ID;
287 break;
288 case CPP_AT_NAME:
289 /* This only happens in Objective-C; it must be a keyword. */
290 token->type = CPP_KEYWORD;
291 token->keyword = C_RID_CODE (token->value);
292 break;
293 case CPP_COLON:
294 case CPP_COMMA:
295 case CPP_CLOSE_PAREN:
296 case CPP_SEMICOLON:
297 /* These tokens may affect the interpretation of any identifiers
298 following, if doing Objective-C. */
299 if (c_dialect_objc ())
300 parser->objc_need_raw_identifier = false;
301 break;
302 case CPP_PRAGMA:
303 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
304 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
305 token->value = NULL;
306 break;
307 default:
308 break;
310 timevar_pop (TV_LEX);
313 /* Return a pointer to the next token from PARSER, reading it in if
314 necessary. */
316 static inline c_token *
317 c_parser_peek_token (c_parser *parser)
319 if (parser->tokens_avail == 0)
321 c_lex_one_token (parser, &parser->tokens[0]);
322 parser->tokens_avail = 1;
324 return &parser->tokens[0];
327 /* Return true if the next token from PARSER has the indicated
328 TYPE. */
330 static inline bool
331 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
333 return c_parser_peek_token (parser)->type == type;
336 /* Return true if the next token from PARSER does not have the
337 indicated TYPE. */
339 static inline bool
340 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
342 return !c_parser_next_token_is (parser, type);
345 /* Return true if the next token from PARSER is the indicated
346 KEYWORD. */
348 static inline bool
349 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
351 return c_parser_peek_token (parser)->keyword == keyword;
354 /* Return true if TOKEN can start a type name,
355 false otherwise. */
356 static bool
357 c_token_starts_typename (c_token *token)
359 switch (token->type)
361 case CPP_NAME:
362 switch (token->id_kind)
364 case C_ID_ID:
365 return false;
366 case C_ID_ADDRSPACE:
367 return true;
368 case C_ID_TYPENAME:
369 return true;
370 case C_ID_CLASSNAME:
371 gcc_assert (c_dialect_objc ());
372 return true;
373 default:
374 gcc_unreachable ();
376 case CPP_KEYWORD:
377 switch (token->keyword)
379 case RID_UNSIGNED:
380 case RID_LONG:
381 case RID_INT128:
382 case RID_SHORT:
383 case RID_SIGNED:
384 case RID_COMPLEX:
385 case RID_INT:
386 case RID_CHAR:
387 case RID_FLOAT:
388 case RID_DOUBLE:
389 case RID_VOID:
390 case RID_DFLOAT32:
391 case RID_DFLOAT64:
392 case RID_DFLOAT128:
393 case RID_BOOL:
394 case RID_ENUM:
395 case RID_STRUCT:
396 case RID_UNION:
397 case RID_TYPEOF:
398 case RID_CONST:
399 case RID_VOLATILE:
400 case RID_RESTRICT:
401 case RID_ATTRIBUTE:
402 case RID_FRACT:
403 case RID_ACCUM:
404 case RID_SAT:
405 return true;
406 default:
407 return false;
409 case CPP_LESS:
410 if (c_dialect_objc ())
411 return true;
412 return false;
413 default:
414 return false;
418 /* Return true if the next token from PARSER can start a type name,
419 false otherwise. */
420 static inline bool
421 c_parser_next_token_starts_typename (c_parser *parser)
423 c_token *token = c_parser_peek_token (parser);
424 return c_token_starts_typename (token);
427 /* Return true if TOKEN can start declaration specifiers, false
428 otherwise. */
429 static bool
430 c_token_starts_declspecs (c_token *token)
432 switch (token->type)
434 case CPP_NAME:
435 switch (token->id_kind)
437 case C_ID_ID:
438 return false;
439 case C_ID_ADDRSPACE:
440 return true;
441 case C_ID_TYPENAME:
442 return true;
443 case C_ID_CLASSNAME:
444 gcc_assert (c_dialect_objc ());
445 return true;
446 default:
447 gcc_unreachable ();
449 case CPP_KEYWORD:
450 switch (token->keyword)
452 case RID_STATIC:
453 case RID_EXTERN:
454 case RID_REGISTER:
455 case RID_TYPEDEF:
456 case RID_INLINE:
457 case RID_AUTO:
458 case RID_THREAD:
459 case RID_UNSIGNED:
460 case RID_LONG:
461 case RID_INT128:
462 case RID_SHORT:
463 case RID_SIGNED:
464 case RID_COMPLEX:
465 case RID_INT:
466 case RID_CHAR:
467 case RID_FLOAT:
468 case RID_DOUBLE:
469 case RID_VOID:
470 case RID_DFLOAT32:
471 case RID_DFLOAT64:
472 case RID_DFLOAT128:
473 case RID_BOOL:
474 case RID_ENUM:
475 case RID_STRUCT:
476 case RID_UNION:
477 case RID_TYPEOF:
478 case RID_CONST:
479 case RID_VOLATILE:
480 case RID_RESTRICT:
481 case RID_ATTRIBUTE:
482 case RID_FRACT:
483 case RID_ACCUM:
484 case RID_SAT:
485 return true;
486 default:
487 return false;
489 case CPP_LESS:
490 if (c_dialect_objc ())
491 return true;
492 return false;
493 default:
494 return false;
499 /* Return true if TOKEN can start declaration specifiers or a static
500 assertion, false otherwise. */
501 static bool
502 c_token_starts_declaration (c_token *token)
504 if (c_token_starts_declspecs (token)
505 || token->keyword == RID_STATIC_ASSERT)
506 return true;
507 else
508 return false;
511 /* Return true if the next token from PARSER can start declaration
512 specifiers, false otherwise. */
513 static inline bool
514 c_parser_next_token_starts_declspecs (c_parser *parser)
516 c_token *token = c_parser_peek_token (parser);
517 return c_token_starts_declspecs (token);
520 /* Return true if the next token from PARSER can start declaration
521 specifiers or a static assertion, false otherwise. */
522 static inline bool
523 c_parser_next_token_starts_declaration (c_parser *parser)
525 c_token *token = c_parser_peek_token (parser);
526 return c_token_starts_declaration (token);
529 /* Return a pointer to the next-but-one token from PARSER, reading it
530 in if necessary. The next token is already read in. */
532 static c_token *
533 c_parser_peek_2nd_token (c_parser *parser)
535 if (parser->tokens_avail >= 2)
536 return &parser->tokens[1];
537 gcc_assert (parser->tokens_avail == 1);
538 gcc_assert (parser->tokens[0].type != CPP_EOF);
539 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
540 c_lex_one_token (parser, &parser->tokens[1]);
541 parser->tokens_avail = 2;
542 return &parser->tokens[1];
545 /* Consume the next token from PARSER. */
547 static void
548 c_parser_consume_token (c_parser *parser)
550 gcc_assert (parser->tokens_avail >= 1);
551 gcc_assert (parser->tokens[0].type != CPP_EOF);
552 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
553 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
554 if (parser->tokens_avail == 2)
555 parser->tokens[0] = parser->tokens[1];
556 parser->tokens_avail--;
559 /* Expect the current token to be a #pragma. Consume it and remember
560 that we've begun parsing a pragma. */
562 static void
563 c_parser_consume_pragma (c_parser *parser)
565 gcc_assert (!parser->in_pragma);
566 gcc_assert (parser->tokens_avail >= 1);
567 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
568 if (parser->tokens_avail == 2)
569 parser->tokens[0] = parser->tokens[1];
570 parser->tokens_avail--;
571 parser->in_pragma = true;
574 /* Update the globals input_location and in_system_header from
575 TOKEN. */
576 static inline void
577 c_parser_set_source_position_from_token (c_token *token)
579 if (token->type != CPP_EOF)
581 input_location = token->location;
585 /* Issue a diagnostic of the form
586 FILE:LINE: MESSAGE before TOKEN
587 where TOKEN is the next token in the input stream of PARSER.
588 MESSAGE (specified by the caller) is usually of the form "expected
589 OTHER-TOKEN".
591 Do not issue a diagnostic if still recovering from an error.
593 ??? This is taken from the C++ parser, but building up messages in
594 this way is not i18n-friendly and some other approach should be
595 used. */
597 static void
598 c_parser_error (c_parser *parser, const char *gmsgid)
600 c_token *token = c_parser_peek_token (parser);
601 if (parser->error)
602 return;
603 parser->error = true;
604 if (!gmsgid)
605 return;
606 /* This diagnostic makes more sense if it is tagged to the line of
607 the token we just peeked at. */
608 c_parser_set_source_position_from_token (token);
609 c_parse_error (gmsgid,
610 /* Because c_parse_error does not understand
611 CPP_KEYWORD, keywords are treated like
612 identifiers. */
613 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
614 /* ??? The C parser does not save the cpp flags of a
615 token, we need to pass 0 here and we will not get
616 the source spelling of some tokens but rather the
617 canonical spelling. */
618 token->value, /*flags=*/0);
621 /* If the next token is of the indicated TYPE, consume it. Otherwise,
622 issue the error MSGID. If MSGID is NULL then a message has already
623 been produced and no message will be produced this time. Returns
624 true if found, false otherwise. */
626 static bool
627 c_parser_require (c_parser *parser,
628 enum cpp_ttype type,
629 const char *msgid)
631 if (c_parser_next_token_is (parser, type))
633 c_parser_consume_token (parser);
634 return true;
636 else
638 c_parser_error (parser, msgid);
639 return false;
643 /* If the next token is the indicated keyword, consume it. Otherwise,
644 issue the error MSGID. Returns true if found, false otherwise. */
646 static bool
647 c_parser_require_keyword (c_parser *parser,
648 enum rid keyword,
649 const char *msgid)
651 if (c_parser_next_token_is_keyword (parser, keyword))
653 c_parser_consume_token (parser);
654 return true;
656 else
658 c_parser_error (parser, msgid);
659 return false;
663 /* Like c_parser_require, except that tokens will be skipped until the
664 desired token is found. An error message is still produced if the
665 next token is not as expected. If MSGID is NULL then a message has
666 already been produced and no message will be produced this
667 time. */
669 static void
670 c_parser_skip_until_found (c_parser *parser,
671 enum cpp_ttype type,
672 const char *msgid)
674 unsigned nesting_depth = 0;
676 if (c_parser_require (parser, type, msgid))
677 return;
679 /* Skip tokens until the desired token is found. */
680 while (true)
682 /* Peek at the next token. */
683 c_token *token = c_parser_peek_token (parser);
684 /* If we've reached the token we want, consume it and stop. */
685 if (token->type == type && !nesting_depth)
687 c_parser_consume_token (parser);
688 break;
691 /* If we've run out of tokens, stop. */
692 if (token->type == CPP_EOF)
693 return;
694 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
695 return;
696 if (token->type == CPP_OPEN_BRACE
697 || token->type == CPP_OPEN_PAREN
698 || token->type == CPP_OPEN_SQUARE)
699 ++nesting_depth;
700 else if (token->type == CPP_CLOSE_BRACE
701 || token->type == CPP_CLOSE_PAREN
702 || token->type == CPP_CLOSE_SQUARE)
704 if (nesting_depth-- == 0)
705 break;
707 /* Consume this token. */
708 c_parser_consume_token (parser);
710 parser->error = false;
713 /* Skip tokens until the end of a parameter is found, but do not
714 consume the comma, semicolon or closing delimiter. */
716 static void
717 c_parser_skip_to_end_of_parameter (c_parser *parser)
719 unsigned nesting_depth = 0;
721 while (true)
723 c_token *token = c_parser_peek_token (parser);
724 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
725 && !nesting_depth)
726 break;
727 /* If we've run out of tokens, stop. */
728 if (token->type == CPP_EOF)
729 return;
730 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
731 return;
732 if (token->type == CPP_OPEN_BRACE
733 || token->type == CPP_OPEN_PAREN
734 || token->type == CPP_OPEN_SQUARE)
735 ++nesting_depth;
736 else if (token->type == CPP_CLOSE_BRACE
737 || token->type == CPP_CLOSE_PAREN
738 || token->type == CPP_CLOSE_SQUARE)
740 if (nesting_depth-- == 0)
741 break;
743 /* Consume this token. */
744 c_parser_consume_token (parser);
746 parser->error = false;
749 /* Expect to be at the end of the pragma directive and consume an
750 end of line marker. */
752 static void
753 c_parser_skip_to_pragma_eol (c_parser *parser)
755 gcc_assert (parser->in_pragma);
756 parser->in_pragma = false;
758 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
759 while (true)
761 c_token *token = c_parser_peek_token (parser);
762 if (token->type == CPP_EOF)
763 break;
764 if (token->type == CPP_PRAGMA_EOL)
766 c_parser_consume_token (parser);
767 break;
769 c_parser_consume_token (parser);
772 parser->error = false;
775 /* Skip tokens until we have consumed an entire block, or until we
776 have consumed a non-nested ';'. */
778 static void
779 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
781 unsigned nesting_depth = 0;
782 bool save_error = parser->error;
784 while (true)
786 c_token *token;
788 /* Peek at the next token. */
789 token = c_parser_peek_token (parser);
791 switch (token->type)
793 case CPP_EOF:
794 return;
796 case CPP_PRAGMA_EOL:
797 if (parser->in_pragma)
798 return;
799 break;
801 case CPP_SEMICOLON:
802 /* If the next token is a ';', we have reached the
803 end of the statement. */
804 if (!nesting_depth)
806 /* Consume the ';'. */
807 c_parser_consume_token (parser);
808 goto finished;
810 break;
812 case CPP_CLOSE_BRACE:
813 /* If the next token is a non-nested '}', then we have
814 reached the end of the current block. */
815 if (nesting_depth == 0 || --nesting_depth == 0)
817 c_parser_consume_token (parser);
818 goto finished;
820 break;
822 case CPP_OPEN_BRACE:
823 /* If it the next token is a '{', then we are entering a new
824 block. Consume the entire block. */
825 ++nesting_depth;
826 break;
828 case CPP_PRAGMA:
829 /* If we see a pragma, consume the whole thing at once. We
830 have some safeguards against consuming pragmas willy-nilly.
831 Normally, we'd expect to be here with parser->error set,
832 which disables these safeguards. But it's possible to get
833 here for secondary error recovery, after parser->error has
834 been cleared. */
835 c_parser_consume_pragma (parser);
836 c_parser_skip_to_pragma_eol (parser);
837 parser->error = save_error;
838 continue;
840 default:
841 break;
844 c_parser_consume_token (parser);
847 finished:
848 parser->error = false;
851 /* CPP's options (initialized by c-opts.c). */
852 extern cpp_options *cpp_opts;
854 /* Save the warning flags which are controlled by __extension__. */
856 static inline int
857 disable_extension_diagnostics (void)
859 int ret = (pedantic
860 | (warn_pointer_arith << 1)
861 | (warn_traditional << 2)
862 | (flag_iso << 3)
863 | (warn_long_long << 4)
864 | (warn_cxx_compat << 5));
865 cpp_opts->pedantic = pedantic = 0;
866 warn_pointer_arith = 0;
867 cpp_opts->warn_traditional = warn_traditional = 0;
868 flag_iso = 0;
869 cpp_opts->warn_long_long = warn_long_long = 0;
870 warn_cxx_compat = 0;
871 return ret;
874 /* Restore the warning flags which are controlled by __extension__.
875 FLAGS is the return value from disable_extension_diagnostics. */
877 static inline void
878 restore_extension_diagnostics (int flags)
880 cpp_opts->pedantic = pedantic = flags & 1;
881 warn_pointer_arith = (flags >> 1) & 1;
882 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
883 flag_iso = (flags >> 3) & 1;
884 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
885 warn_cxx_compat = (flags >> 5) & 1;
888 /* Possibly kinds of declarator to parse. */
889 typedef enum c_dtr_syn {
890 /* A normal declarator with an identifier. */
891 C_DTR_NORMAL,
892 /* An abstract declarator (maybe empty). */
893 C_DTR_ABSTRACT,
894 /* A parameter declarator: may be either, but after a type name does
895 not redeclare a typedef name as an identifier if it can
896 alternatively be interpreted as a typedef name; see DR#009,
897 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
898 following DR#249. For example, given a typedef T, "int T" and
899 "int *T" are valid parameter declarations redeclaring T, while
900 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
901 abstract declarators rather than involving redundant parentheses;
902 the same applies with attributes inside the parentheses before
903 "T". */
904 C_DTR_PARM
905 } c_dtr_syn;
907 static void c_parser_external_declaration (c_parser *);
908 static void c_parser_asm_definition (c_parser *);
909 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
910 bool, bool);
911 static void c_parser_static_assert_declaration_no_semi (c_parser *);
912 static void c_parser_static_assert_declaration (c_parser *);
913 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
914 bool);
915 static struct c_typespec c_parser_enum_specifier (c_parser *);
916 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
917 static tree c_parser_struct_declaration (c_parser *);
918 static struct c_typespec c_parser_typeof_specifier (c_parser *);
919 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
920 bool *);
921 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
922 c_dtr_syn, bool *);
923 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
924 bool,
925 struct c_declarator *);
926 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
927 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
928 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
929 static tree c_parser_simple_asm_expr (c_parser *);
930 static tree c_parser_attributes (c_parser *);
931 static struct c_type_name *c_parser_type_name (c_parser *);
932 static struct c_expr c_parser_initializer (c_parser *);
933 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
934 static void c_parser_initelt (c_parser *, struct obstack *);
935 static void c_parser_initval (c_parser *, struct c_expr *,
936 struct obstack *);
937 static tree c_parser_compound_statement (c_parser *);
938 static void c_parser_compound_statement_nostart (c_parser *);
939 static void c_parser_label (c_parser *);
940 static void c_parser_statement (c_parser *);
941 static void c_parser_statement_after_labels (c_parser *);
942 static void c_parser_if_statement (c_parser *);
943 static void c_parser_switch_statement (c_parser *);
944 static void c_parser_while_statement (c_parser *);
945 static void c_parser_do_statement (c_parser *);
946 static void c_parser_for_statement (c_parser *);
947 static tree c_parser_asm_statement (c_parser *);
948 static tree c_parser_asm_operands (c_parser *, bool);
949 static tree c_parser_asm_goto_operands (c_parser *);
950 static tree c_parser_asm_clobbers (c_parser *);
951 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
952 static struct c_expr c_parser_conditional_expression (c_parser *,
953 struct c_expr *);
954 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
955 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
956 static struct c_expr c_parser_unary_expression (c_parser *);
957 static struct c_expr c_parser_sizeof_expression (c_parser *);
958 static struct c_expr c_parser_alignof_expression (c_parser *);
959 static struct c_expr c_parser_postfix_expression (c_parser *);
960 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
961 struct c_type_name *,
962 location_t);
963 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
964 location_t loc,
965 struct c_expr);
966 static struct c_expr c_parser_expression (c_parser *);
967 static struct c_expr c_parser_expression_conv (c_parser *);
968 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
969 VEC(tree,gc) **);
970 static void c_parser_omp_construct (c_parser *);
971 static void c_parser_omp_threadprivate (c_parser *);
972 static void c_parser_omp_barrier (c_parser *);
973 static void c_parser_omp_flush (c_parser *);
974 static void c_parser_omp_taskwait (c_parser *);
976 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
977 static bool c_parser_pragma (c_parser *, enum pragma_context);
979 /* These Objective-C parser functions are only ever called when
980 compiling Objective-C. */
981 static void c_parser_objc_class_definition (c_parser *);
982 static void c_parser_objc_class_instance_variables (c_parser *);
983 static void c_parser_objc_class_declaration (c_parser *);
984 static void c_parser_objc_alias_declaration (c_parser *);
985 static void c_parser_objc_protocol_definition (c_parser *);
986 static enum tree_code c_parser_objc_method_type (c_parser *);
987 static void c_parser_objc_method_definition (c_parser *);
988 static void c_parser_objc_methodprotolist (c_parser *);
989 static void c_parser_objc_methodproto (c_parser *);
990 static tree c_parser_objc_method_decl (c_parser *);
991 static tree c_parser_objc_type_name (c_parser *);
992 static tree c_parser_objc_protocol_refs (c_parser *);
993 static void c_parser_objc_try_catch_statement (c_parser *);
994 static void c_parser_objc_synchronized_statement (c_parser *);
995 static tree c_parser_objc_selector (c_parser *);
996 static tree c_parser_objc_selector_arg (c_parser *);
997 static tree c_parser_objc_receiver (c_parser *);
998 static tree c_parser_objc_message_args (c_parser *);
999 static tree c_parser_objc_keywordexpr (c_parser *);
1001 /* Parse a translation unit (C90 6.7, C99 6.9).
1003 translation-unit:
1004 external-declarations
1006 external-declarations:
1007 external-declaration
1008 external-declarations external-declaration
1010 GNU extensions:
1012 translation-unit:
1013 empty
1016 static void
1017 c_parser_translation_unit (c_parser *parser)
1019 if (c_parser_next_token_is (parser, CPP_EOF))
1021 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1022 "ISO C forbids an empty translation unit");
1024 else
1026 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1027 mark_valid_location_for_stdc_pragma (false);
1030 ggc_collect ();
1031 c_parser_external_declaration (parser);
1032 obstack_free (&parser_obstack, obstack_position);
1034 while (c_parser_next_token_is_not (parser, CPP_EOF));
1038 /* Parse an external declaration (C90 6.7, C99 6.9).
1040 external-declaration:
1041 function-definition
1042 declaration
1044 GNU extensions:
1046 external-declaration:
1047 asm-definition
1049 __extension__ external-declaration
1051 Objective-C:
1053 external-declaration:
1054 objc-class-definition
1055 objc-class-declaration
1056 objc-alias-declaration
1057 objc-protocol-definition
1058 objc-method-definition
1059 @end
1062 static void
1063 c_parser_external_declaration (c_parser *parser)
1065 int ext;
1066 switch (c_parser_peek_token (parser)->type)
1068 case CPP_KEYWORD:
1069 switch (c_parser_peek_token (parser)->keyword)
1071 case RID_EXTENSION:
1072 ext = disable_extension_diagnostics ();
1073 c_parser_consume_token (parser);
1074 c_parser_external_declaration (parser);
1075 restore_extension_diagnostics (ext);
1076 break;
1077 case RID_ASM:
1078 c_parser_asm_definition (parser);
1079 break;
1080 case RID_AT_INTERFACE:
1081 case RID_AT_IMPLEMENTATION:
1082 gcc_assert (c_dialect_objc ());
1083 c_parser_objc_class_definition (parser);
1084 break;
1085 case RID_CLASS:
1086 gcc_assert (c_dialect_objc ());
1087 c_parser_objc_class_declaration (parser);
1088 break;
1089 case RID_AT_ALIAS:
1090 gcc_assert (c_dialect_objc ());
1091 c_parser_objc_alias_declaration (parser);
1092 break;
1093 case RID_AT_PROTOCOL:
1094 gcc_assert (c_dialect_objc ());
1095 c_parser_objc_protocol_definition (parser);
1096 break;
1097 case RID_AT_END:
1098 gcc_assert (c_dialect_objc ());
1099 c_parser_consume_token (parser);
1100 objc_finish_implementation ();
1101 break;
1102 default:
1103 goto decl_or_fndef;
1105 break;
1106 case CPP_SEMICOLON:
1107 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1108 "ISO C does not allow extra %<;%> outside of a function");
1109 c_parser_consume_token (parser);
1110 break;
1111 case CPP_PRAGMA:
1112 mark_valid_location_for_stdc_pragma (true);
1113 c_parser_pragma (parser, pragma_external);
1114 mark_valid_location_for_stdc_pragma (false);
1115 break;
1116 case CPP_PLUS:
1117 case CPP_MINUS:
1118 if (c_dialect_objc ())
1120 c_parser_objc_method_definition (parser);
1121 break;
1123 /* Else fall through, and yield a syntax error trying to parse
1124 as a declaration or function definition. */
1125 default:
1126 decl_or_fndef:
1127 /* A declaration or a function definition. We can only tell
1128 which after parsing the declaration specifiers, if any, and
1129 the first declarator. */
1130 c_parser_declaration_or_fndef (parser, true, true, true, false, true);
1131 break;
1136 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1137 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1138 accepted; otherwise (old-style parameter declarations) only other
1139 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1140 assertion is accepted; otherwise (old-style parameter declarations)
1141 it is not. If NESTED is true, we are inside a function or parsing
1142 old-style parameter declarations; any functions encountered are
1143 nested functions and declaration specifiers are required; otherwise
1144 we are at top level and functions are normal functions and
1145 declaration specifiers may be optional. If EMPTY_OK is true, empty
1146 declarations are OK (subject to all other constraints); otherwise
1147 (old-style parameter declarations) they are diagnosed. If
1148 START_ATTR_OK is true, the declaration specifiers may start with
1149 attributes; otherwise they may not.
1151 declaration:
1152 declaration-specifiers init-declarator-list[opt] ;
1153 static_assert-declaration
1155 function-definition:
1156 declaration-specifiers[opt] declarator declaration-list[opt]
1157 compound-statement
1159 declaration-list:
1160 declaration
1161 declaration-list declaration
1163 init-declarator-list:
1164 init-declarator
1165 init-declarator-list , init-declarator
1167 init-declarator:
1168 declarator simple-asm-expr[opt] attributes[opt]
1169 declarator simple-asm-expr[opt] attributes[opt] = initializer
1171 GNU extensions:
1173 nested-function-definition:
1174 declaration-specifiers declarator declaration-list[opt]
1175 compound-statement
1177 The simple-asm-expr and attributes are GNU extensions.
1179 This function does not handle __extension__; that is handled in its
1180 callers. ??? Following the old parser, __extension__ may start
1181 external declarations, declarations in functions and declarations
1182 at the start of "for" loops, but not old-style parameter
1183 declarations.
1185 C99 requires declaration specifiers in a function definition; the
1186 absence is diagnosed through the diagnosis of implicit int. In GNU
1187 C we also allow but diagnose declarations without declaration
1188 specifiers, but only at top level (elsewhere they conflict with
1189 other syntax).
1191 OpenMP:
1193 declaration:
1194 threadprivate-directive */
1196 static void
1197 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1198 bool static_assert_ok, bool empty_ok,
1199 bool nested, bool start_attr_ok)
1201 struct c_declspecs *specs;
1202 tree prefix_attrs;
1203 tree all_prefix_attrs;
1204 bool diagnosed_no_specs = false;
1205 location_t here = c_parser_peek_token (parser)->location;
1207 if (static_assert_ok
1208 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1210 c_parser_static_assert_declaration (parser);
1211 return;
1213 specs = build_null_declspecs ();
1214 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1215 if (parser->error)
1217 c_parser_skip_to_end_of_block_or_statement (parser);
1218 return;
1220 if (nested && !specs->declspecs_seen_p)
1222 c_parser_error (parser, "expected declaration specifiers");
1223 c_parser_skip_to_end_of_block_or_statement (parser);
1224 return;
1226 finish_declspecs (specs);
1227 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1229 if (empty_ok)
1230 shadow_tag (specs);
1231 else
1233 shadow_tag_warned (specs, 1);
1234 pedwarn (here, 0, "empty declaration");
1236 c_parser_consume_token (parser);
1237 return;
1239 pending_xref_error ();
1240 prefix_attrs = specs->attrs;
1241 all_prefix_attrs = prefix_attrs;
1242 specs->attrs = NULL_TREE;
1243 while (true)
1245 struct c_declarator *declarator;
1246 bool dummy = false;
1247 tree fnbody;
1248 /* Declaring either one or more declarators (in which case we
1249 should diagnose if there were no declaration specifiers) or a
1250 function definition (in which case the diagnostic for
1251 implicit int suffices). */
1252 declarator = c_parser_declarator (parser, specs->type_seen_p,
1253 C_DTR_NORMAL, &dummy);
1254 if (declarator == NULL)
1256 c_parser_skip_to_end_of_block_or_statement (parser);
1257 return;
1259 if (c_parser_next_token_is (parser, CPP_EQ)
1260 || c_parser_next_token_is (parser, CPP_COMMA)
1261 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1262 || c_parser_next_token_is_keyword (parser, RID_ASM)
1263 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1265 tree asm_name = NULL_TREE;
1266 tree postfix_attrs = NULL_TREE;
1267 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1269 diagnosed_no_specs = true;
1270 pedwarn (here, 0, "data definition has no type or storage class");
1272 /* Having seen a data definition, there cannot now be a
1273 function definition. */
1274 fndef_ok = false;
1275 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1276 asm_name = c_parser_simple_asm_expr (parser);
1277 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1278 postfix_attrs = c_parser_attributes (parser);
1279 if (c_parser_next_token_is (parser, CPP_EQ))
1281 tree d;
1282 struct c_expr init;
1283 location_t init_loc;
1284 c_parser_consume_token (parser);
1285 /* The declaration of the variable is in effect while
1286 its initializer is parsed. */
1287 d = start_decl (declarator, specs, true,
1288 chainon (postfix_attrs, all_prefix_attrs));
1289 if (!d)
1290 d = error_mark_node;
1291 start_init (d, asm_name, global_bindings_p ());
1292 init_loc = c_parser_peek_token (parser)->location;
1293 init = c_parser_initializer (parser);
1294 finish_init ();
1295 if (d != error_mark_node)
1297 maybe_warn_string_init (TREE_TYPE (d), init);
1298 finish_decl (d, init_loc, init.value,
1299 init.original_type, asm_name);
1302 else
1304 tree d = start_decl (declarator, specs, false,
1305 chainon (postfix_attrs,
1306 all_prefix_attrs));
1307 if (d)
1308 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1309 NULL_TREE, asm_name);
1311 if (c_parser_next_token_is (parser, CPP_COMMA))
1313 c_parser_consume_token (parser);
1314 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1315 all_prefix_attrs = chainon (c_parser_attributes (parser),
1316 prefix_attrs);
1317 else
1318 all_prefix_attrs = prefix_attrs;
1319 continue;
1321 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1323 c_parser_consume_token (parser);
1324 return;
1326 else
1328 c_parser_error (parser, "expected %<,%> or %<;%>");
1329 c_parser_skip_to_end_of_block_or_statement (parser);
1330 return;
1333 else if (!fndef_ok)
1335 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1336 "%<asm%> or %<__attribute__%>");
1337 c_parser_skip_to_end_of_block_or_statement (parser);
1338 return;
1340 /* Function definition (nested or otherwise). */
1341 if (nested)
1343 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1344 c_push_function_context ();
1346 if (!start_function (specs, declarator, all_prefix_attrs))
1348 /* This can appear in many cases looking nothing like a
1349 function definition, so we don't give a more specific
1350 error suggesting there was one. */
1351 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1352 "or %<__attribute__%>");
1353 if (nested)
1354 c_pop_function_context ();
1355 break;
1357 /* Parse old-style parameter declarations. ??? Attributes are
1358 not allowed to start declaration specifiers here because of a
1359 syntax conflict between a function declaration with attribute
1360 suffix and a function definition with an attribute prefix on
1361 first old-style parameter declaration. Following the old
1362 parser, they are not accepted on subsequent old-style
1363 parameter declarations either. However, there is no
1364 ambiguity after the first declaration, nor indeed on the
1365 first as long as we don't allow postfix attributes after a
1366 declarator with a nonempty identifier list in a definition;
1367 and postfix attributes have never been accepted here in
1368 function definitions either. */
1369 while (c_parser_next_token_is_not (parser, CPP_EOF)
1370 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1371 c_parser_declaration_or_fndef (parser, false, false, false,
1372 true, false);
1373 store_parm_decls ();
1374 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1375 = c_parser_peek_token (parser)->location;
1376 fnbody = c_parser_compound_statement (parser);
1377 if (nested)
1379 tree decl = current_function_decl;
1380 /* Mark nested functions as needing static-chain initially.
1381 lower_nested_functions will recompute it but the
1382 DECL_STATIC_CHAIN flag is also used before that happens,
1383 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1384 DECL_STATIC_CHAIN (decl) = 1;
1385 add_stmt (fnbody);
1386 finish_function ();
1387 c_pop_function_context ();
1388 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1390 else
1392 add_stmt (fnbody);
1393 finish_function ();
1395 break;
1399 /* Parse an asm-definition (asm() outside a function body). This is a
1400 GNU extension.
1402 asm-definition:
1403 simple-asm-expr ;
1406 static void
1407 c_parser_asm_definition (c_parser *parser)
1409 tree asm_str = c_parser_simple_asm_expr (parser);
1410 if (asm_str)
1411 cgraph_add_asm_node (asm_str);
1412 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1415 /* Parse a static assertion (C1X N1425 6.7.10).
1417 static_assert-declaration:
1418 static_assert-declaration-no-semi ;
1421 static void
1422 c_parser_static_assert_declaration (c_parser *parser)
1424 c_parser_static_assert_declaration_no_semi (parser);
1425 if (parser->error
1426 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1427 c_parser_skip_to_end_of_block_or_statement (parser);
1430 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1431 semicolon.
1433 static_assert-declaration-no-semi:
1434 _Static_assert ( constant-expression , string-literal )
1437 static void
1438 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1440 location_t assert_loc, value_loc;
1441 tree value;
1442 tree string;
1444 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1445 assert_loc = c_parser_peek_token (parser)->location;
1446 if (!flag_isoc1x)
1448 if (flag_isoc99)
1449 pedwarn (assert_loc, OPT_pedantic,
1450 "ISO C99 does not support %<_Static_assert%>");
1451 else
1452 pedwarn (assert_loc, OPT_pedantic,
1453 "ISO C90 does not support %<_Static_assert%>");
1455 c_parser_consume_token (parser);
1456 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1457 return;
1458 value_loc = c_parser_peek_token (parser)->location;
1459 value = c_parser_expr_no_commas (parser, NULL).value;
1460 parser->lex_untranslated_string = true;
1461 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1463 parser->lex_untranslated_string = false;
1464 return;
1466 switch (c_parser_peek_token (parser)->type)
1468 case CPP_STRING:
1469 case CPP_STRING16:
1470 case CPP_STRING32:
1471 case CPP_WSTRING:
1472 case CPP_UTF8STRING:
1473 string = c_parser_peek_token (parser)->value;
1474 c_parser_consume_token (parser);
1475 parser->lex_untranslated_string = false;
1476 break;
1477 default:
1478 c_parser_error (parser, "expected string literal");
1479 parser->lex_untranslated_string = false;
1480 return;
1482 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1484 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1486 error_at (value_loc, "expression in static assertion is not an integer");
1487 return;
1489 if (TREE_CODE (value) != INTEGER_CST)
1491 value = c_fully_fold (value, false, NULL);
1492 if (TREE_CODE (value) == INTEGER_CST)
1493 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1494 "is not an integer constant expression");
1496 if (TREE_CODE (value) != INTEGER_CST)
1498 error_at (value_loc, "expression in static assertion is not constant");
1499 return;
1501 constant_expression_warning (value);
1502 if (integer_zerop (value))
1503 error_at (assert_loc, "static assertion failed: %E", string);
1506 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1507 6.7), adding them to SPECS (which may already include some).
1508 Storage class specifiers are accepted iff SCSPEC_OK; type
1509 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1510 the start iff START_ATTR_OK.
1512 declaration-specifiers:
1513 storage-class-specifier declaration-specifiers[opt]
1514 type-specifier declaration-specifiers[opt]
1515 type-qualifier declaration-specifiers[opt]
1516 function-specifier declaration-specifiers[opt]
1518 Function specifiers (inline) are from C99, and are currently
1519 handled as storage class specifiers, as is __thread.
1521 C90 6.5.1, C99 6.7.1:
1522 storage-class-specifier:
1523 typedef
1524 extern
1525 static
1526 auto
1527 register
1529 C99 6.7.4:
1530 function-specifier:
1531 inline
1533 C90 6.5.2, C99 6.7.2:
1534 type-specifier:
1535 void
1536 char
1537 short
1539 long
1540 float
1541 double
1542 signed
1543 unsigned
1544 _Bool
1545 _Complex
1546 [_Imaginary removed in C99 TC2]
1547 struct-or-union-specifier
1548 enum-specifier
1549 typedef-name
1551 (_Bool and _Complex are new in C99.)
1553 C90 6.5.3, C99 6.7.3:
1555 type-qualifier:
1556 const
1557 restrict
1558 volatile
1559 address-space-qualifier
1561 (restrict is new in C99.)
1563 GNU extensions:
1565 declaration-specifiers:
1566 attributes declaration-specifiers[opt]
1568 type-qualifier:
1569 address-space
1571 address-space:
1572 identifier recognized by the target
1574 storage-class-specifier:
1575 __thread
1577 type-specifier:
1578 typeof-specifier
1579 __int128
1580 _Decimal32
1581 _Decimal64
1582 _Decimal128
1583 _Fract
1584 _Accum
1585 _Sat
1587 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1588 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1590 Objective-C:
1592 type-specifier:
1593 class-name objc-protocol-refs[opt]
1594 typedef-name objc-protocol-refs
1595 objc-protocol-refs
1598 static void
1599 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1600 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1602 bool attrs_ok = start_attr_ok;
1603 bool seen_type = specs->type_seen_p;
1604 while (c_parser_next_token_is (parser, CPP_NAME)
1605 || c_parser_next_token_is (parser, CPP_KEYWORD)
1606 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1608 struct c_typespec t;
1609 tree attrs;
1610 location_t loc = c_parser_peek_token (parser)->location;
1611 if (c_parser_next_token_is (parser, CPP_NAME))
1613 tree value = c_parser_peek_token (parser)->value;
1614 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1616 if (kind == C_ID_ADDRSPACE)
1618 addr_space_t as
1619 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1620 declspecs_add_addrspace (specs, as);
1621 c_parser_consume_token (parser);
1622 attrs_ok = true;
1623 continue;
1626 /* This finishes the specifiers unless a type name is OK, it
1627 is declared as a type name and a type name hasn't yet
1628 been seen. */
1629 if (!typespec_ok || seen_type
1630 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1631 break;
1632 c_parser_consume_token (parser);
1633 seen_type = true;
1634 attrs_ok = true;
1635 if (kind == C_ID_TYPENAME
1636 && (!c_dialect_objc ()
1637 || c_parser_next_token_is_not (parser, CPP_LESS)))
1639 t.kind = ctsk_typedef;
1640 /* For a typedef name, record the meaning, not the name.
1641 In case of 'foo foo, bar;'. */
1642 t.spec = lookup_name (value);
1643 t.expr = NULL_TREE;
1644 t.expr_const_operands = true;
1646 else
1648 tree proto = NULL_TREE;
1649 gcc_assert (c_dialect_objc ());
1650 t.kind = ctsk_objc;
1651 if (c_parser_next_token_is (parser, CPP_LESS))
1652 proto = c_parser_objc_protocol_refs (parser);
1653 t.spec = objc_get_protocol_qualified_type (value, proto);
1654 t.expr = NULL_TREE;
1655 t.expr_const_operands = true;
1657 declspecs_add_type (loc, specs, t);
1658 continue;
1660 if (c_parser_next_token_is (parser, CPP_LESS))
1662 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1663 nisse@lysator.liu.se. */
1664 tree proto;
1665 gcc_assert (c_dialect_objc ());
1666 if (!typespec_ok || seen_type)
1667 break;
1668 proto = c_parser_objc_protocol_refs (parser);
1669 t.kind = ctsk_objc;
1670 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1671 t.expr = NULL_TREE;
1672 t.expr_const_operands = true;
1673 declspecs_add_type (loc, specs, t);
1674 continue;
1676 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1677 switch (c_parser_peek_token (parser)->keyword)
1679 case RID_STATIC:
1680 case RID_EXTERN:
1681 case RID_REGISTER:
1682 case RID_TYPEDEF:
1683 case RID_INLINE:
1684 case RID_AUTO:
1685 case RID_THREAD:
1686 if (!scspec_ok)
1687 goto out;
1688 attrs_ok = true;
1689 /* TODO: Distinguish between function specifiers (inline)
1690 and storage class specifiers, either here or in
1691 declspecs_add_scspec. */
1692 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1693 c_parser_consume_token (parser);
1694 break;
1695 case RID_UNSIGNED:
1696 case RID_LONG:
1697 case RID_INT128:
1698 case RID_SHORT:
1699 case RID_SIGNED:
1700 case RID_COMPLEX:
1701 case RID_INT:
1702 case RID_CHAR:
1703 case RID_FLOAT:
1704 case RID_DOUBLE:
1705 case RID_VOID:
1706 case RID_DFLOAT32:
1707 case RID_DFLOAT64:
1708 case RID_DFLOAT128:
1709 case RID_BOOL:
1710 case RID_FRACT:
1711 case RID_ACCUM:
1712 case RID_SAT:
1713 if (!typespec_ok)
1714 goto out;
1715 attrs_ok = true;
1716 seen_type = true;
1717 if (c_dialect_objc ())
1718 parser->objc_need_raw_identifier = true;
1719 t.kind = ctsk_resword;
1720 t.spec = c_parser_peek_token (parser)->value;
1721 t.expr = NULL_TREE;
1722 t.expr_const_operands = true;
1723 declspecs_add_type (loc, specs, t);
1724 c_parser_consume_token (parser);
1725 break;
1726 case RID_ENUM:
1727 if (!typespec_ok)
1728 goto out;
1729 attrs_ok = true;
1730 seen_type = true;
1731 t = c_parser_enum_specifier (parser);
1732 declspecs_add_type (loc, specs, t);
1733 break;
1734 case RID_STRUCT:
1735 case RID_UNION:
1736 if (!typespec_ok)
1737 goto out;
1738 attrs_ok = true;
1739 seen_type = true;
1740 t = c_parser_struct_or_union_specifier (parser);
1741 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1742 declspecs_add_type (loc, specs, t);
1743 break;
1744 case RID_TYPEOF:
1745 /* ??? The old parser rejected typeof after other type
1746 specifiers, but is a syntax error the best way of
1747 handling this? */
1748 if (!typespec_ok || seen_type)
1749 goto out;
1750 attrs_ok = true;
1751 seen_type = true;
1752 t = c_parser_typeof_specifier (parser);
1753 declspecs_add_type (loc, specs, t);
1754 break;
1755 case RID_CONST:
1756 case RID_VOLATILE:
1757 case RID_RESTRICT:
1758 attrs_ok = true;
1759 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1760 c_parser_consume_token (parser);
1761 break;
1762 case RID_ATTRIBUTE:
1763 if (!attrs_ok)
1764 goto out;
1765 attrs = c_parser_attributes (parser);
1766 declspecs_add_attrs (specs, attrs);
1767 break;
1768 default:
1769 goto out;
1772 out: ;
1775 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1777 enum-specifier:
1778 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1779 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1780 enum attributes[opt] identifier
1782 The form with trailing comma is new in C99. The forms with
1783 attributes are GNU extensions. In GNU C, we accept any expression
1784 without commas in the syntax (assignment expressions, not just
1785 conditional expressions); assignment expressions will be diagnosed
1786 as non-constant.
1788 enumerator-list:
1789 enumerator
1790 enumerator-list , enumerator
1792 enumerator:
1793 enumeration-constant
1794 enumeration-constant = constant-expression
1797 static struct c_typespec
1798 c_parser_enum_specifier (c_parser *parser)
1800 struct c_typespec ret;
1801 tree attrs;
1802 tree ident = NULL_TREE;
1803 location_t enum_loc;
1804 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1805 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1806 enum_loc = c_parser_peek_token (parser)->location;
1807 c_parser_consume_token (parser);
1808 attrs = c_parser_attributes (parser);
1809 enum_loc = c_parser_peek_token (parser)->location;
1810 /* Set the location in case we create a decl now. */
1811 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1812 if (c_parser_next_token_is (parser, CPP_NAME))
1814 ident = c_parser_peek_token (parser)->value;
1815 ident_loc = c_parser_peek_token (parser)->location;
1816 enum_loc = ident_loc;
1817 c_parser_consume_token (parser);
1819 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1821 /* Parse an enum definition. */
1822 struct c_enum_contents the_enum;
1823 tree type = start_enum (enum_loc, &the_enum, ident);
1824 tree postfix_attrs;
1825 /* We chain the enumerators in reverse order, then put them in
1826 forward order at the end. */
1827 tree values = NULL_TREE;
1828 c_parser_consume_token (parser);
1829 while (true)
1831 tree enum_id;
1832 tree enum_value;
1833 tree enum_decl;
1834 bool seen_comma;
1835 c_token *token;
1836 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1837 location_t value_loc;
1838 if (c_parser_next_token_is_not (parser, CPP_NAME))
1840 c_parser_error (parser, "expected identifier");
1841 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1842 values = error_mark_node;
1843 break;
1845 token = c_parser_peek_token (parser);
1846 enum_id = token->value;
1847 /* Set the location in case we create a decl now. */
1848 c_parser_set_source_position_from_token (token);
1849 value_loc = token->location;
1850 c_parser_consume_token (parser);
1851 if (c_parser_next_token_is (parser, CPP_EQ))
1853 c_parser_consume_token (parser);
1854 value_loc = c_parser_peek_token (parser)->location;
1855 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1857 else
1858 enum_value = NULL_TREE;
1859 enum_decl = build_enumerator (value_loc,
1860 &the_enum, enum_id, enum_value);
1861 TREE_CHAIN (enum_decl) = values;
1862 values = enum_decl;
1863 seen_comma = false;
1864 if (c_parser_next_token_is (parser, CPP_COMMA))
1866 comma_loc = c_parser_peek_token (parser)->location;
1867 seen_comma = true;
1868 c_parser_consume_token (parser);
1870 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1872 if (seen_comma && !flag_isoc99)
1873 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1874 c_parser_consume_token (parser);
1875 break;
1877 if (!seen_comma)
1879 c_parser_error (parser, "expected %<,%> or %<}%>");
1880 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1881 values = error_mark_node;
1882 break;
1885 postfix_attrs = c_parser_attributes (parser);
1886 ret.spec = finish_enum (type, nreverse (values),
1887 chainon (attrs, postfix_attrs));
1888 ret.kind = ctsk_tagdef;
1889 ret.expr = NULL_TREE;
1890 ret.expr_const_operands = true;
1891 return ret;
1893 else if (!ident)
1895 c_parser_error (parser, "expected %<{%>");
1896 ret.spec = error_mark_node;
1897 ret.kind = ctsk_tagref;
1898 ret.expr = NULL_TREE;
1899 ret.expr_const_operands = true;
1900 return ret;
1902 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
1903 /* In ISO C, enumerated types can be referred to only if already
1904 defined. */
1905 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1907 gcc_assert (ident);
1908 pedwarn (enum_loc, OPT_pedantic,
1909 "ISO C forbids forward references to %<enum%> types");
1911 return ret;
1914 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1916 struct-or-union-specifier:
1917 struct-or-union attributes[opt] identifier[opt]
1918 { struct-contents } attributes[opt]
1919 struct-or-union attributes[opt] identifier
1921 struct-contents:
1922 struct-declaration-list
1924 struct-declaration-list:
1925 struct-declaration ;
1926 struct-declaration-list struct-declaration ;
1928 GNU extensions:
1930 struct-contents:
1931 empty
1932 struct-declaration
1933 struct-declaration-list struct-declaration
1935 struct-declaration-list:
1936 struct-declaration-list ;
1939 (Note that in the syntax here, unlike that in ISO C, the semicolons
1940 are included here rather than in struct-declaration, in order to
1941 describe the syntax with extra semicolons and missing semicolon at
1942 end.)
1944 Objective-C:
1946 struct-declaration-list:
1947 @defs ( class-name )
1949 (Note this does not include a trailing semicolon, but can be
1950 followed by further declarations, and gets a pedwarn-if-pedantic
1951 when followed by a semicolon.) */
1953 static struct c_typespec
1954 c_parser_struct_or_union_specifier (c_parser *parser)
1956 struct c_typespec ret;
1957 tree attrs;
1958 tree ident = NULL_TREE;
1959 location_t struct_loc;
1960 location_t ident_loc = UNKNOWN_LOCATION;
1961 enum tree_code code;
1962 switch (c_parser_peek_token (parser)->keyword)
1964 case RID_STRUCT:
1965 code = RECORD_TYPE;
1966 break;
1967 case RID_UNION:
1968 code = UNION_TYPE;
1969 break;
1970 default:
1971 gcc_unreachable ();
1973 struct_loc = c_parser_peek_token (parser)->location;
1974 c_parser_consume_token (parser);
1975 attrs = c_parser_attributes (parser);
1977 /* Set the location in case we create a decl now. */
1978 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1980 if (c_parser_next_token_is (parser, CPP_NAME))
1982 ident = c_parser_peek_token (parser)->value;
1983 ident_loc = c_parser_peek_token (parser)->location;
1984 struct_loc = ident_loc;
1985 c_parser_consume_token (parser);
1987 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1989 /* Parse a struct or union definition. Start the scope of the
1990 tag before parsing components. */
1991 struct c_struct_parse_info *struct_info;
1992 tree type = start_struct (struct_loc, code, ident, &struct_info);
1993 tree postfix_attrs;
1994 /* We chain the components in reverse order, then put them in
1995 forward order at the end. Each struct-declaration may
1996 declare multiple components (comma-separated), so we must use
1997 chainon to join them, although when parsing each
1998 struct-declaration we can use TREE_CHAIN directly.
2000 The theory behind all this is that there will be more
2001 semicolon separated fields than comma separated fields, and
2002 so we'll be minimizing the number of node traversals required
2003 by chainon. */
2004 tree contents = NULL_TREE;
2005 c_parser_consume_token (parser);
2006 /* Handle the Objective-C @defs construct,
2007 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2008 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2010 tree name;
2011 gcc_assert (c_dialect_objc ());
2012 c_parser_consume_token (parser);
2013 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2014 goto end_at_defs;
2015 if (c_parser_next_token_is (parser, CPP_NAME)
2016 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2018 name = c_parser_peek_token (parser)->value;
2019 c_parser_consume_token (parser);
2021 else
2023 c_parser_error (parser, "expected class name");
2024 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2025 goto end_at_defs;
2027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2028 "expected %<)%>");
2029 contents = nreverse (objc_get_class_ivars (name));
2031 end_at_defs:
2032 /* Parse the struct-declarations and semicolons. Problems with
2033 semicolons are diagnosed here; empty structures are diagnosed
2034 elsewhere. */
2035 while (true)
2037 tree decls;
2038 /* Parse any stray semicolon. */
2039 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2041 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2042 "extra semicolon in struct or union specified");
2043 c_parser_consume_token (parser);
2044 continue;
2046 /* Stop if at the end of the struct or union contents. */
2047 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2049 c_parser_consume_token (parser);
2050 break;
2052 /* Accept #pragmas at struct scope. */
2053 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2055 c_parser_pragma (parser, pragma_external);
2056 continue;
2058 /* Parse some comma-separated declarations, but not the
2059 trailing semicolon if any. */
2060 decls = c_parser_struct_declaration (parser);
2061 contents = chainon (decls, contents);
2062 /* If no semicolon follows, either we have a parse error or
2063 are at the end of the struct or union and should
2064 pedwarn. */
2065 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2066 c_parser_consume_token (parser);
2067 else
2069 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2070 pedwarn (c_parser_peek_token (parser)->location, 0,
2071 "no semicolon at end of struct or union");
2072 else
2074 c_parser_error (parser, "expected %<;%>");
2075 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2076 break;
2080 postfix_attrs = c_parser_attributes (parser);
2081 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2082 chainon (attrs, postfix_attrs), struct_info);
2083 ret.kind = ctsk_tagdef;
2084 ret.expr = NULL_TREE;
2085 ret.expr_const_operands = true;
2086 return ret;
2088 else if (!ident)
2090 c_parser_error (parser, "expected %<{%>");
2091 ret.spec = error_mark_node;
2092 ret.kind = ctsk_tagref;
2093 ret.expr = NULL_TREE;
2094 ret.expr_const_operands = true;
2095 return ret;
2097 ret = parser_xref_tag (ident_loc, code, ident);
2098 return ret;
2101 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2102 the trailing semicolon.
2104 struct-declaration:
2105 specifier-qualifier-list struct-declarator-list
2106 static_assert-declaration-no-semi
2108 specifier-qualifier-list:
2109 type-specifier specifier-qualifier-list[opt]
2110 type-qualifier specifier-qualifier-list[opt]
2111 attributes specifier-qualifier-list[opt]
2113 struct-declarator-list:
2114 struct-declarator
2115 struct-declarator-list , attributes[opt] struct-declarator
2117 struct-declarator:
2118 declarator attributes[opt]
2119 declarator[opt] : constant-expression attributes[opt]
2121 GNU extensions:
2123 struct-declaration:
2124 __extension__ struct-declaration
2125 specifier-qualifier-list
2127 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2128 of attributes where shown is a GNU extension. In GNU C, we accept
2129 any expression without commas in the syntax (assignment
2130 expressions, not just conditional expressions); assignment
2131 expressions will be diagnosed as non-constant. */
2133 static tree
2134 c_parser_struct_declaration (c_parser *parser)
2136 struct c_declspecs *specs;
2137 tree prefix_attrs;
2138 tree all_prefix_attrs;
2139 tree decls;
2140 location_t decl_loc;
2141 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2143 int ext;
2144 tree decl;
2145 ext = disable_extension_diagnostics ();
2146 c_parser_consume_token (parser);
2147 decl = c_parser_struct_declaration (parser);
2148 restore_extension_diagnostics (ext);
2149 return decl;
2151 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2153 c_parser_static_assert_declaration_no_semi (parser);
2154 return NULL_TREE;
2156 specs = build_null_declspecs ();
2157 decl_loc = c_parser_peek_token (parser)->location;
2158 c_parser_declspecs (parser, specs, false, true, true);
2159 if (parser->error)
2160 return NULL_TREE;
2161 if (!specs->declspecs_seen_p)
2163 c_parser_error (parser, "expected specifier-qualifier-list");
2164 return NULL_TREE;
2166 finish_declspecs (specs);
2167 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2169 tree ret;
2170 if (!specs->type_seen_p)
2172 pedwarn (decl_loc, OPT_pedantic,
2173 "ISO C forbids member declarations with no members");
2174 shadow_tag_warned (specs, pedantic);
2175 ret = NULL_TREE;
2177 else
2179 /* Support for unnamed structs or unions as members of
2180 structs or unions (which is [a] useful and [b] supports
2181 MS P-SDK). */
2182 tree attrs = NULL;
2184 ret = grokfield (c_parser_peek_token (parser)->location,
2185 build_id_declarator (NULL_TREE), specs,
2186 NULL_TREE, &attrs);
2187 if (ret)
2188 decl_attributes (&ret, attrs, 0);
2190 return ret;
2192 pending_xref_error ();
2193 prefix_attrs = specs->attrs;
2194 all_prefix_attrs = prefix_attrs;
2195 specs->attrs = NULL_TREE;
2196 decls = NULL_TREE;
2197 while (true)
2199 /* Declaring one or more declarators or un-named bit-fields. */
2200 struct c_declarator *declarator;
2201 bool dummy = false;
2202 if (c_parser_next_token_is (parser, CPP_COLON))
2203 declarator = build_id_declarator (NULL_TREE);
2204 else
2205 declarator = c_parser_declarator (parser, specs->type_seen_p,
2206 C_DTR_NORMAL, &dummy);
2207 if (declarator == NULL)
2209 c_parser_skip_to_end_of_block_or_statement (parser);
2210 break;
2212 if (c_parser_next_token_is (parser, CPP_COLON)
2213 || c_parser_next_token_is (parser, CPP_COMMA)
2214 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2215 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2216 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2218 tree postfix_attrs = NULL_TREE;
2219 tree width = NULL_TREE;
2220 tree d;
2221 if (c_parser_next_token_is (parser, CPP_COLON))
2223 c_parser_consume_token (parser);
2224 width = c_parser_expr_no_commas (parser, NULL).value;
2226 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2227 postfix_attrs = c_parser_attributes (parser);
2228 d = grokfield (c_parser_peek_token (parser)->location,
2229 declarator, specs, width, &all_prefix_attrs);
2230 decl_attributes (&d, chainon (postfix_attrs,
2231 all_prefix_attrs), 0);
2232 DECL_CHAIN (d) = decls;
2233 decls = d;
2234 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2235 all_prefix_attrs = chainon (c_parser_attributes (parser),
2236 prefix_attrs);
2237 else
2238 all_prefix_attrs = prefix_attrs;
2239 if (c_parser_next_token_is (parser, CPP_COMMA))
2240 c_parser_consume_token (parser);
2241 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2242 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2244 /* Semicolon consumed in caller. */
2245 break;
2247 else
2249 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2250 break;
2253 else
2255 c_parser_error (parser,
2256 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2257 "%<__attribute__%>");
2258 break;
2261 return decls;
2264 /* Parse a typeof specifier (a GNU extension).
2266 typeof-specifier:
2267 typeof ( expression )
2268 typeof ( type-name )
2271 static struct c_typespec
2272 c_parser_typeof_specifier (c_parser *parser)
2274 struct c_typespec ret;
2275 ret.kind = ctsk_typeof;
2276 ret.spec = error_mark_node;
2277 ret.expr = NULL_TREE;
2278 ret.expr_const_operands = true;
2279 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2280 c_parser_consume_token (parser);
2281 c_inhibit_evaluation_warnings++;
2282 in_typeof++;
2283 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2285 c_inhibit_evaluation_warnings--;
2286 in_typeof--;
2287 return ret;
2289 if (c_parser_next_token_starts_typename (parser))
2291 struct c_type_name *type = c_parser_type_name (parser);
2292 c_inhibit_evaluation_warnings--;
2293 in_typeof--;
2294 if (type != NULL)
2296 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2297 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2300 else
2302 bool was_vm;
2303 location_t here = c_parser_peek_token (parser)->location;
2304 struct c_expr expr = c_parser_expression (parser);
2305 c_inhibit_evaluation_warnings--;
2306 in_typeof--;
2307 if (TREE_CODE (expr.value) == COMPONENT_REF
2308 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2309 error_at (here, "%<typeof%> applied to a bit-field");
2310 mark_exp_read (expr.value);
2311 ret.spec = TREE_TYPE (expr.value);
2312 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2313 /* This is returned with the type so that when the type is
2314 evaluated, this can be evaluated. */
2315 if (was_vm)
2316 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2317 pop_maybe_used (was_vm);
2319 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2320 return ret;
2323 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2324 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2325 be redeclared; otherwise it may not. KIND indicates which kind of
2326 declarator is wanted. Returns a valid declarator except in the
2327 case of a syntax error in which case NULL is returned. *SEEN_ID is
2328 set to true if an identifier being declared is seen; this is used
2329 to diagnose bad forms of abstract array declarators and to
2330 determine whether an identifier list is syntactically permitted.
2332 declarator:
2333 pointer[opt] direct-declarator
2335 direct-declarator:
2336 identifier
2337 ( attributes[opt] declarator )
2338 direct-declarator array-declarator
2339 direct-declarator ( parameter-type-list )
2340 direct-declarator ( identifier-list[opt] )
2342 pointer:
2343 * type-qualifier-list[opt]
2344 * type-qualifier-list[opt] pointer
2346 type-qualifier-list:
2347 type-qualifier
2348 attributes
2349 type-qualifier-list type-qualifier
2350 type-qualifier-list attributes
2352 parameter-type-list:
2353 parameter-list
2354 parameter-list , ...
2356 parameter-list:
2357 parameter-declaration
2358 parameter-list , parameter-declaration
2360 parameter-declaration:
2361 declaration-specifiers declarator attributes[opt]
2362 declaration-specifiers abstract-declarator[opt] attributes[opt]
2364 identifier-list:
2365 identifier
2366 identifier-list , identifier
2368 abstract-declarator:
2369 pointer
2370 pointer[opt] direct-abstract-declarator
2372 direct-abstract-declarator:
2373 ( attributes[opt] abstract-declarator )
2374 direct-abstract-declarator[opt] array-declarator
2375 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2377 GNU extensions:
2379 direct-declarator:
2380 direct-declarator ( parameter-forward-declarations
2381 parameter-type-list[opt] )
2383 direct-abstract-declarator:
2384 direct-abstract-declarator[opt] ( parameter-forward-declarations
2385 parameter-type-list[opt] )
2387 parameter-forward-declarations:
2388 parameter-list ;
2389 parameter-forward-declarations parameter-list ;
2391 The uses of attributes shown above are GNU extensions.
2393 Some forms of array declarator are not included in C99 in the
2394 syntax for abstract declarators; these are disallowed elsewhere.
2395 This may be a defect (DR#289).
2397 This function also accepts an omitted abstract declarator as being
2398 an abstract declarator, although not part of the formal syntax. */
2400 static struct c_declarator *
2401 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2402 bool *seen_id)
2404 /* Parse any initial pointer part. */
2405 if (c_parser_next_token_is (parser, CPP_MULT))
2407 struct c_declspecs *quals_attrs = build_null_declspecs ();
2408 struct c_declarator *inner;
2409 c_parser_consume_token (parser);
2410 c_parser_declspecs (parser, quals_attrs, false, false, true);
2411 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2412 if (inner == NULL)
2413 return NULL;
2414 else
2415 return make_pointer_declarator (quals_attrs, inner);
2417 /* Now we have a direct declarator, direct abstract declarator or
2418 nothing (which counts as a direct abstract declarator here). */
2419 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2422 /* Parse a direct declarator or direct abstract declarator; arguments
2423 as c_parser_declarator. */
2425 static struct c_declarator *
2426 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2427 bool *seen_id)
2429 /* The direct declarator must start with an identifier (possibly
2430 omitted) or a parenthesized declarator (possibly abstract). In
2431 an ordinary declarator, initial parentheses must start a
2432 parenthesized declarator. In an abstract declarator or parameter
2433 declarator, they could start a parenthesized declarator or a
2434 parameter list. To tell which, the open parenthesis and any
2435 following attributes must be read. If a declaration specifier
2436 follows, then it is a parameter list; if the specifier is a
2437 typedef name, there might be an ambiguity about redeclaring it,
2438 which is resolved in the direction of treating it as a typedef
2439 name. If a close parenthesis follows, it is also an empty
2440 parameter list, as the syntax does not permit empty abstract
2441 declarators. Otherwise, it is a parenthesized declarator (in
2442 which case the analysis may be repeated inside it, recursively).
2444 ??? There is an ambiguity in a parameter declaration "int
2445 (__attribute__((foo)) x)", where x is not a typedef name: it
2446 could be an abstract declarator for a function, or declare x with
2447 parentheses. The proper resolution of this ambiguity needs
2448 documenting. At present we follow an accident of the old
2449 parser's implementation, whereby the first parameter must have
2450 some declaration specifiers other than just attributes. Thus as
2451 a parameter declaration it is treated as a parenthesized
2452 parameter named x, and as an abstract declarator it is
2453 rejected.
2455 ??? Also following the old parser, attributes inside an empty
2456 parameter list are ignored, making it a list not yielding a
2457 prototype, rather than giving an error or making it have one
2458 parameter with implicit type int.
2460 ??? Also following the old parser, typedef names may be
2461 redeclared in declarators, but not Objective-C class names. */
2463 if (kind != C_DTR_ABSTRACT
2464 && c_parser_next_token_is (parser, CPP_NAME)
2465 && ((type_seen_p
2466 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2467 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2469 struct c_declarator *inner
2470 = build_id_declarator (c_parser_peek_token (parser)->value);
2471 *seen_id = true;
2472 inner->id_loc = c_parser_peek_token (parser)->location;
2473 c_parser_consume_token (parser);
2474 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2477 if (kind != C_DTR_NORMAL
2478 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2480 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2481 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2484 /* Either we are at the end of an abstract declarator, or we have
2485 parentheses. */
2487 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2489 tree attrs;
2490 struct c_declarator *inner;
2491 c_parser_consume_token (parser);
2492 attrs = c_parser_attributes (parser);
2493 if (kind != C_DTR_NORMAL
2494 && (c_parser_next_token_starts_declspecs (parser)
2495 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2497 struct c_arg_info *args
2498 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2499 attrs);
2500 if (args == NULL)
2501 return NULL;
2502 else
2504 inner
2505 = build_function_declarator (args,
2506 build_id_declarator (NULL_TREE));
2507 return c_parser_direct_declarator_inner (parser, *seen_id,
2508 inner);
2511 /* A parenthesized declarator. */
2512 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2513 if (inner != NULL && attrs != NULL)
2514 inner = build_attrs_declarator (attrs, inner);
2515 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2517 c_parser_consume_token (parser);
2518 if (inner == NULL)
2519 return NULL;
2520 else
2521 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2523 else
2525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2526 "expected %<)%>");
2527 return NULL;
2530 else
2532 if (kind == C_DTR_NORMAL)
2534 c_parser_error (parser, "expected identifier or %<(%>");
2535 return NULL;
2537 else
2538 return build_id_declarator (NULL_TREE);
2542 /* Parse part of a direct declarator or direct abstract declarator,
2543 given that some (in INNER) has already been parsed; ID_PRESENT is
2544 true if an identifier is present, false for an abstract
2545 declarator. */
2547 static struct c_declarator *
2548 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2549 struct c_declarator *inner)
2551 /* Parse a sequence of array declarators and parameter lists. */
2552 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2554 location_t brace_loc = c_parser_peek_token (parser)->location;
2555 struct c_declarator *declarator;
2556 struct c_declspecs *quals_attrs = build_null_declspecs ();
2557 bool static_seen;
2558 bool star_seen;
2559 tree dimen;
2560 c_parser_consume_token (parser);
2561 c_parser_declspecs (parser, quals_attrs, false, false, true);
2562 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2563 if (static_seen)
2564 c_parser_consume_token (parser);
2565 if (static_seen && !quals_attrs->declspecs_seen_p)
2566 c_parser_declspecs (parser, quals_attrs, false, false, true);
2567 if (!quals_attrs->declspecs_seen_p)
2568 quals_attrs = NULL;
2569 /* If "static" is present, there must be an array dimension.
2570 Otherwise, there may be a dimension, "*", or no
2571 dimension. */
2572 if (static_seen)
2574 star_seen = false;
2575 dimen = c_parser_expr_no_commas (parser, NULL).value;
2577 else
2579 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2581 dimen = NULL_TREE;
2582 star_seen = false;
2584 else if (c_parser_next_token_is (parser, CPP_MULT))
2586 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2588 dimen = NULL_TREE;
2589 star_seen = true;
2590 c_parser_consume_token (parser);
2592 else
2594 star_seen = false;
2595 dimen = c_parser_expr_no_commas (parser, NULL).value;
2598 else
2600 star_seen = false;
2601 dimen = c_parser_expr_no_commas (parser, NULL).value;
2604 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2605 c_parser_consume_token (parser);
2606 else
2608 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2609 "expected %<]%>");
2610 return NULL;
2612 if (dimen)
2613 mark_exp_read (dimen);
2614 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2615 static_seen, star_seen);
2616 if (declarator == NULL)
2617 return NULL;
2618 inner = set_array_declarator_inner (declarator, inner);
2619 return c_parser_direct_declarator_inner (parser, id_present, inner);
2621 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2623 tree attrs;
2624 struct c_arg_info *args;
2625 c_parser_consume_token (parser);
2626 attrs = c_parser_attributes (parser);
2627 args = c_parser_parms_declarator (parser, id_present, attrs);
2628 if (args == NULL)
2629 return NULL;
2630 else
2632 inner = build_function_declarator (args, inner);
2633 return c_parser_direct_declarator_inner (parser, id_present, inner);
2636 return inner;
2639 /* Parse a parameter list or identifier list, including the closing
2640 parenthesis but not the opening one. ATTRS are the attributes at
2641 the start of the list. ID_LIST_OK is true if an identifier list is
2642 acceptable; such a list must not have attributes at the start. */
2644 static struct c_arg_info *
2645 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2647 push_scope ();
2648 declare_parm_level ();
2649 /* If the list starts with an identifier, it is an identifier list.
2650 Otherwise, it is either a prototype list or an empty list. */
2651 if (id_list_ok
2652 && !attrs
2653 && c_parser_next_token_is (parser, CPP_NAME)
2654 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2656 tree list = NULL_TREE, *nextp = &list;
2657 while (c_parser_next_token_is (parser, CPP_NAME)
2658 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2660 *nextp = build_tree_list (NULL_TREE,
2661 c_parser_peek_token (parser)->value);
2662 nextp = & TREE_CHAIN (*nextp);
2663 c_parser_consume_token (parser);
2664 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2665 break;
2666 c_parser_consume_token (parser);
2667 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2669 c_parser_error (parser, "expected identifier");
2670 break;
2673 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2675 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2676 ret->parms = 0;
2677 ret->tags = 0;
2678 ret->types = list;
2679 ret->others = 0;
2680 ret->pending_sizes = 0;
2681 ret->had_vla_unspec = 0;
2682 c_parser_consume_token (parser);
2683 pop_scope ();
2684 return ret;
2686 else
2688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2689 "expected %<)%>");
2690 pop_scope ();
2691 return NULL;
2694 else
2696 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2697 pop_scope ();
2698 return ret;
2702 /* Parse a parameter list (possibly empty), including the closing
2703 parenthesis but not the opening one. ATTRS are the attributes at
2704 the start of the list. */
2706 static struct c_arg_info *
2707 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2709 bool bad_parm = false;
2710 /* ??? Following the old parser, forward parameter declarations may
2711 use abstract declarators, and if no real parameter declarations
2712 follow the forward declarations then this is not diagnosed. Also
2713 note as above that attributes are ignored as the only contents of
2714 the parentheses, or as the only contents after forward
2715 declarations. */
2716 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2718 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2719 ret->parms = 0;
2720 ret->tags = 0;
2721 ret->types = 0;
2722 ret->others = 0;
2723 ret->pending_sizes = 0;
2724 ret->had_vla_unspec = 0;
2725 c_parser_consume_token (parser);
2726 return ret;
2728 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2730 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2731 ret->parms = 0;
2732 ret->tags = 0;
2733 ret->others = 0;
2734 ret->pending_sizes = 0;
2735 ret->had_vla_unspec = 0;
2736 /* Suppress -Wold-style-definition for this case. */
2737 ret->types = error_mark_node;
2738 error_at (c_parser_peek_token (parser)->location,
2739 "ISO C requires a named argument before %<...%>");
2740 c_parser_consume_token (parser);
2741 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2743 c_parser_consume_token (parser);
2744 return ret;
2746 else
2748 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2749 "expected %<)%>");
2750 return NULL;
2753 /* Nonempty list of parameters, either terminated with semicolon
2754 (forward declarations; recurse) or with close parenthesis (normal
2755 function) or with ", ... )" (variadic function). */
2756 while (true)
2758 /* Parse a parameter. */
2759 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2760 attrs = NULL_TREE;
2761 if (parm == NULL)
2762 bad_parm = true;
2763 else
2764 push_parm_decl (parm);
2765 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2767 tree new_attrs;
2768 c_parser_consume_token (parser);
2769 mark_forward_parm_decls ();
2770 new_attrs = c_parser_attributes (parser);
2771 return c_parser_parms_list_declarator (parser, new_attrs);
2773 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2775 c_parser_consume_token (parser);
2776 if (bad_parm)
2778 get_pending_sizes ();
2779 return NULL;
2781 else
2782 return get_parm_info (false);
2784 if (!c_parser_require (parser, CPP_COMMA,
2785 "expected %<;%>, %<,%> or %<)%>"))
2787 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2788 get_pending_sizes ();
2789 return NULL;
2791 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2793 c_parser_consume_token (parser);
2794 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2796 c_parser_consume_token (parser);
2797 if (bad_parm)
2799 get_pending_sizes ();
2800 return NULL;
2802 else
2803 return get_parm_info (true);
2805 else
2807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2808 "expected %<)%>");
2809 get_pending_sizes ();
2810 return NULL;
2816 /* Parse a parameter declaration. ATTRS are the attributes at the
2817 start of the declaration if it is the first parameter. */
2819 static struct c_parm *
2820 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2822 struct c_declspecs *specs;
2823 struct c_declarator *declarator;
2824 tree prefix_attrs;
2825 tree postfix_attrs = NULL_TREE;
2826 bool dummy = false;
2827 if (!c_parser_next_token_starts_declspecs (parser))
2829 c_token *token = c_parser_peek_token (parser);
2830 if (parser->error)
2831 return NULL;
2832 c_parser_set_source_position_from_token (token);
2833 if (token->type == CPP_NAME
2834 && c_parser_peek_2nd_token (parser)->type != CPP_COMMA
2835 && c_parser_peek_2nd_token (parser)->type != CPP_CLOSE_PAREN)
2837 error ("unknown type name %qE", token->value);
2838 parser->error = true;
2840 /* ??? In some Objective-C cases '...' isn't applicable so there
2841 should be a different message. */
2842 else
2843 c_parser_error (parser,
2844 "expected declaration specifiers or %<...%>");
2845 c_parser_skip_to_end_of_parameter (parser);
2846 return NULL;
2848 specs = build_null_declspecs ();
2849 if (attrs)
2851 declspecs_add_attrs (specs, attrs);
2852 attrs = NULL_TREE;
2854 c_parser_declspecs (parser, specs, true, true, true);
2855 finish_declspecs (specs);
2856 pending_xref_error ();
2857 prefix_attrs = specs->attrs;
2858 specs->attrs = NULL_TREE;
2859 declarator = c_parser_declarator (parser, specs->type_seen_p,
2860 C_DTR_PARM, &dummy);
2861 if (declarator == NULL)
2863 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2864 return NULL;
2866 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2867 postfix_attrs = c_parser_attributes (parser);
2868 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2869 declarator);
2872 /* Parse a string literal in an asm expression. It should not be
2873 translated, and wide string literals are an error although
2874 permitted by the syntax. This is a GNU extension.
2876 asm-string-literal:
2877 string-literal
2879 ??? At present, following the old parser, the caller needs to have
2880 set lex_untranslated_string to 1. It would be better to follow the
2881 C++ parser rather than using this kludge. */
2883 static tree
2884 c_parser_asm_string_literal (c_parser *parser)
2886 tree str;
2887 if (c_parser_next_token_is (parser, CPP_STRING))
2889 str = c_parser_peek_token (parser)->value;
2890 c_parser_consume_token (parser);
2892 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2894 error_at (c_parser_peek_token (parser)->location,
2895 "wide string literal in %<asm%>");
2896 str = build_string (1, "");
2897 c_parser_consume_token (parser);
2899 else
2901 c_parser_error (parser, "expected string literal");
2902 str = NULL_TREE;
2904 return str;
2907 /* Parse a simple asm expression. This is used in restricted
2908 contexts, where a full expression with inputs and outputs does not
2909 make sense. This is a GNU extension.
2911 simple-asm-expr:
2912 asm ( asm-string-literal )
2915 static tree
2916 c_parser_simple_asm_expr (c_parser *parser)
2918 tree str;
2919 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2920 /* ??? Follow the C++ parser rather than using the
2921 lex_untranslated_string kludge. */
2922 parser->lex_untranslated_string = true;
2923 c_parser_consume_token (parser);
2924 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2926 parser->lex_untranslated_string = false;
2927 return NULL_TREE;
2929 str = c_parser_asm_string_literal (parser);
2930 parser->lex_untranslated_string = false;
2931 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2933 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2934 return NULL_TREE;
2936 return str;
2939 /* Parse (possibly empty) attributes. This is a GNU extension.
2941 attributes:
2942 empty
2943 attributes attribute
2945 attribute:
2946 __attribute__ ( ( attribute-list ) )
2948 attribute-list:
2949 attrib
2950 attribute_list , attrib
2952 attrib:
2953 empty
2954 any-word
2955 any-word ( identifier )
2956 any-word ( identifier , nonempty-expr-list )
2957 any-word ( expr-list )
2959 where the "identifier" must not be declared as a type, and
2960 "any-word" may be any identifier (including one declared as a
2961 type), a reserved word storage class specifier, type specifier or
2962 type qualifier. ??? This still leaves out most reserved keywords
2963 (following the old parser), shouldn't we include them, and why not
2964 allow identifiers declared as types to start the arguments? */
2966 static tree
2967 c_parser_attributes (c_parser *parser)
2969 tree attrs = NULL_TREE;
2970 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2972 /* ??? Follow the C++ parser rather than using the
2973 lex_untranslated_string kludge. */
2974 parser->lex_untranslated_string = true;
2975 c_parser_consume_token (parser);
2976 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2978 parser->lex_untranslated_string = false;
2979 return attrs;
2981 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2983 parser->lex_untranslated_string = false;
2984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2985 return attrs;
2987 /* Parse the attribute list. */
2988 while (c_parser_next_token_is (parser, CPP_COMMA)
2989 || c_parser_next_token_is (parser, CPP_NAME)
2990 || c_parser_next_token_is (parser, CPP_KEYWORD))
2992 tree attr, attr_name, attr_args;
2993 VEC(tree,gc) *expr_list;
2994 if (c_parser_next_token_is (parser, CPP_COMMA))
2996 c_parser_consume_token (parser);
2997 continue;
2999 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3001 /* ??? See comment above about what keywords are
3002 accepted here. */
3003 bool ok;
3004 switch (c_parser_peek_token (parser)->keyword)
3006 case RID_STATIC:
3007 case RID_UNSIGNED:
3008 case RID_LONG:
3009 case RID_INT128:
3010 case RID_CONST:
3011 case RID_EXTERN:
3012 case RID_REGISTER:
3013 case RID_TYPEDEF:
3014 case RID_SHORT:
3015 case RID_INLINE:
3016 case RID_VOLATILE:
3017 case RID_SIGNED:
3018 case RID_AUTO:
3019 case RID_RESTRICT:
3020 case RID_COMPLEX:
3021 case RID_THREAD:
3022 case RID_INT:
3023 case RID_CHAR:
3024 case RID_FLOAT:
3025 case RID_DOUBLE:
3026 case RID_VOID:
3027 case RID_DFLOAT32:
3028 case RID_DFLOAT64:
3029 case RID_DFLOAT128:
3030 case RID_BOOL:
3031 case RID_FRACT:
3032 case RID_ACCUM:
3033 case RID_SAT:
3034 ok = true;
3035 break;
3036 default:
3037 ok = false;
3038 break;
3040 if (!ok)
3041 break;
3042 /* Accept __attribute__((__const)) as __attribute__((const))
3043 etc. */
3044 attr_name
3045 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3047 else
3048 attr_name = c_parser_peek_token (parser)->value;
3049 c_parser_consume_token (parser);
3050 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3052 attr = build_tree_list (attr_name, NULL_TREE);
3053 attrs = chainon (attrs, attr);
3054 continue;
3056 c_parser_consume_token (parser);
3057 /* Parse the attribute contents. If they start with an
3058 identifier which is followed by a comma or close
3059 parenthesis, then the arguments start with that
3060 identifier; otherwise they are an expression list. */
3061 if (c_parser_next_token_is (parser, CPP_NAME)
3062 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3063 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3064 || (c_parser_peek_2nd_token (parser)->type
3065 == CPP_CLOSE_PAREN)))
3067 tree arg1 = c_parser_peek_token (parser)->value;
3068 c_parser_consume_token (parser);
3069 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3070 attr_args = build_tree_list (NULL_TREE, arg1);
3071 else
3073 tree tree_list;
3074 c_parser_consume_token (parser);
3075 expr_list = c_parser_expr_list (parser, false, true, NULL);
3076 tree_list = build_tree_list_vec (expr_list);
3077 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3078 release_tree_vector (expr_list);
3081 else
3083 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3084 attr_args = NULL_TREE;
3085 else
3087 expr_list = c_parser_expr_list (parser, false, true, NULL);
3088 attr_args = build_tree_list_vec (expr_list);
3089 release_tree_vector (expr_list);
3092 attr = build_tree_list (attr_name, attr_args);
3093 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3094 c_parser_consume_token (parser);
3095 else
3097 parser->lex_untranslated_string = false;
3098 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3099 "expected %<)%>");
3100 return attrs;
3102 attrs = chainon (attrs, attr);
3104 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3105 c_parser_consume_token (parser);
3106 else
3108 parser->lex_untranslated_string = false;
3109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3110 "expected %<)%>");
3111 return attrs;
3113 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3114 c_parser_consume_token (parser);
3115 else
3117 parser->lex_untranslated_string = false;
3118 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3119 "expected %<)%>");
3120 return attrs;
3122 parser->lex_untranslated_string = false;
3124 return attrs;
3127 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3129 type-name:
3130 specifier-qualifier-list abstract-declarator[opt]
3133 static struct c_type_name *
3134 c_parser_type_name (c_parser *parser)
3136 struct c_declspecs *specs = build_null_declspecs ();
3137 struct c_declarator *declarator;
3138 struct c_type_name *ret;
3139 bool dummy = false;
3140 c_parser_declspecs (parser, specs, false, true, true);
3141 if (!specs->declspecs_seen_p)
3143 c_parser_error (parser, "expected specifier-qualifier-list");
3144 return NULL;
3146 pending_xref_error ();
3147 finish_declspecs (specs);
3148 declarator = c_parser_declarator (parser, specs->type_seen_p,
3149 C_DTR_ABSTRACT, &dummy);
3150 if (declarator == NULL)
3151 return NULL;
3152 ret = XOBNEW (&parser_obstack, struct c_type_name);
3153 ret->specs = specs;
3154 ret->declarator = declarator;
3155 return ret;
3158 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3160 initializer:
3161 assignment-expression
3162 { initializer-list }
3163 { initializer-list , }
3165 initializer-list:
3166 designation[opt] initializer
3167 initializer-list , designation[opt] initializer
3169 designation:
3170 designator-list =
3172 designator-list:
3173 designator
3174 designator-list designator
3176 designator:
3177 array-designator
3178 . identifier
3180 array-designator:
3181 [ constant-expression ]
3183 GNU extensions:
3185 initializer:
3188 designation:
3189 array-designator
3190 identifier :
3192 array-designator:
3193 [ constant-expression ... constant-expression ]
3195 Any expression without commas is accepted in the syntax for the
3196 constant-expressions, with non-constant expressions rejected later.
3198 This function is only used for top-level initializers; for nested
3199 ones, see c_parser_initval. */
3201 static struct c_expr
3202 c_parser_initializer (c_parser *parser)
3204 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3205 return c_parser_braced_init (parser, NULL_TREE, false);
3206 else
3208 struct c_expr ret;
3209 location_t loc = c_parser_peek_token (parser)->location;
3210 ret = c_parser_expr_no_commas (parser, NULL);
3211 if (TREE_CODE (ret.value) != STRING_CST
3212 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3213 ret = default_function_array_read_conversion (loc, ret);
3214 return ret;
3218 /* Parse a braced initializer list. TYPE is the type specified for a
3219 compound literal, and NULL_TREE for other initializers and for
3220 nested braced lists. NESTED_P is true for nested braced lists,
3221 false for the list of a compound literal or the list that is the
3222 top-level initializer in a declaration. */
3224 static struct c_expr
3225 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3227 struct c_expr ret;
3228 struct obstack braced_init_obstack;
3229 location_t brace_loc = c_parser_peek_token (parser)->location;
3230 gcc_obstack_init (&braced_init_obstack);
3231 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3232 c_parser_consume_token (parser);
3233 if (nested_p)
3234 push_init_level (0, &braced_init_obstack);
3235 else
3236 really_start_incremental_init (type);
3237 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3239 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3241 else
3243 /* Parse a non-empty initializer list, possibly with a trailing
3244 comma. */
3245 while (true)
3247 c_parser_initelt (parser, &braced_init_obstack);
3248 if (parser->error)
3249 break;
3250 if (c_parser_next_token_is (parser, CPP_COMMA))
3251 c_parser_consume_token (parser);
3252 else
3253 break;
3254 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3255 break;
3258 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3260 ret.value = error_mark_node;
3261 ret.original_code = ERROR_MARK;
3262 ret.original_type = NULL;
3263 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3264 pop_init_level (0, &braced_init_obstack);
3265 obstack_free (&braced_init_obstack, NULL);
3266 return ret;
3268 c_parser_consume_token (parser);
3269 ret = pop_init_level (0, &braced_init_obstack);
3270 obstack_free (&braced_init_obstack, NULL);
3271 return ret;
3274 /* Parse a nested initializer, including designators. */
3276 static void
3277 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3279 /* Parse any designator or designator list. A single array
3280 designator may have the subsequent "=" omitted in GNU C, but a
3281 longer list or a structure member designator may not. */
3282 if (c_parser_next_token_is (parser, CPP_NAME)
3283 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3285 /* Old-style structure member designator. */
3286 set_init_label (c_parser_peek_token (parser)->value,
3287 braced_init_obstack);
3288 /* Use the colon as the error location. */
3289 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3290 "obsolete use of designated initializer with %<:%>");
3291 c_parser_consume_token (parser);
3292 c_parser_consume_token (parser);
3294 else
3296 /* des_seen is 0 if there have been no designators, 1 if there
3297 has been a single array designator and 2 otherwise. */
3298 int des_seen = 0;
3299 /* Location of a designator. */
3300 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3301 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3302 || c_parser_next_token_is (parser, CPP_DOT))
3304 int des_prev = des_seen;
3305 if (!des_seen)
3306 des_loc = c_parser_peek_token (parser)->location;
3307 if (des_seen < 2)
3308 des_seen++;
3309 if (c_parser_next_token_is (parser, CPP_DOT))
3311 des_seen = 2;
3312 c_parser_consume_token (parser);
3313 if (c_parser_next_token_is (parser, CPP_NAME))
3315 set_init_label (c_parser_peek_token (parser)->value,
3316 braced_init_obstack);
3317 c_parser_consume_token (parser);
3319 else
3321 struct c_expr init;
3322 init.value = error_mark_node;
3323 init.original_code = ERROR_MARK;
3324 init.original_type = NULL;
3325 c_parser_error (parser, "expected identifier");
3326 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3327 process_init_element (init, false, braced_init_obstack);
3328 return;
3331 else
3333 tree first, second;
3334 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3335 /* ??? Following the old parser, [ objc-receiver
3336 objc-message-args ] is accepted as an initializer,
3337 being distinguished from a designator by what follows
3338 the first assignment expression inside the square
3339 brackets, but after a first array designator a
3340 subsequent square bracket is for Objective-C taken to
3341 start an expression, using the obsolete form of
3342 designated initializer without '=', rather than
3343 possibly being a second level of designation: in LALR
3344 terms, the '[' is shifted rather than reducing
3345 designator to designator-list. */
3346 if (des_prev == 1 && c_dialect_objc ())
3348 des_seen = des_prev;
3349 break;
3351 if (des_prev == 0 && c_dialect_objc ())
3353 /* This might be an array designator or an
3354 Objective-C message expression. If the former,
3355 continue parsing here; if the latter, parse the
3356 remainder of the initializer given the starting
3357 primary-expression. ??? It might make sense to
3358 distinguish when des_prev == 1 as well; see
3359 previous comment. */
3360 tree rec, args;
3361 struct c_expr mexpr;
3362 c_parser_consume_token (parser);
3363 if (c_parser_peek_token (parser)->type == CPP_NAME
3364 && ((c_parser_peek_token (parser)->id_kind
3365 == C_ID_TYPENAME)
3366 || (c_parser_peek_token (parser)->id_kind
3367 == C_ID_CLASSNAME)))
3369 /* Type name receiver. */
3370 tree id = c_parser_peek_token (parser)->value;
3371 c_parser_consume_token (parser);
3372 rec = objc_get_class_reference (id);
3373 goto parse_message_args;
3375 first = c_parser_expr_no_commas (parser, NULL).value;
3376 mark_exp_read (first);
3377 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3378 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3379 goto array_desig_after_first;
3380 /* Expression receiver. So far only one part
3381 without commas has been parsed; there might be
3382 more of the expression. */
3383 rec = first;
3384 while (c_parser_next_token_is (parser, CPP_COMMA))
3386 struct c_expr next;
3387 location_t comma_loc, exp_loc;
3388 comma_loc = c_parser_peek_token (parser)->location;
3389 c_parser_consume_token (parser);
3390 exp_loc = c_parser_peek_token (parser)->location;
3391 next = c_parser_expr_no_commas (parser, NULL);
3392 next = default_function_array_read_conversion (exp_loc,
3393 next);
3394 rec = build_compound_expr (comma_loc, rec, next.value);
3396 parse_message_args:
3397 /* Now parse the objc-message-args. */
3398 args = c_parser_objc_message_args (parser);
3399 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3400 "expected %<]%>");
3401 mexpr.value
3402 = objc_build_message_expr (build_tree_list (rec, args));
3403 mexpr.original_code = ERROR_MARK;
3404 mexpr.original_type = NULL;
3405 /* Now parse and process the remainder of the
3406 initializer, starting with this message
3407 expression as a primary-expression. */
3408 c_parser_initval (parser, &mexpr, braced_init_obstack);
3409 return;
3411 c_parser_consume_token (parser);
3412 first = c_parser_expr_no_commas (parser, NULL).value;
3413 mark_exp_read (first);
3414 array_desig_after_first:
3415 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3417 ellipsis_loc = c_parser_peek_token (parser)->location;
3418 c_parser_consume_token (parser);
3419 second = c_parser_expr_no_commas (parser, NULL).value;
3420 mark_exp_read (second);
3422 else
3423 second = NULL_TREE;
3424 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3426 c_parser_consume_token (parser);
3427 set_init_index (first, second, braced_init_obstack);
3428 if (second)
3429 pedwarn (ellipsis_loc, OPT_pedantic,
3430 "ISO C forbids specifying range of elements to initialize");
3432 else
3433 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3434 "expected %<]%>");
3437 if (des_seen >= 1)
3439 if (c_parser_next_token_is (parser, CPP_EQ))
3441 if (!flag_isoc99)
3442 pedwarn (des_loc, OPT_pedantic,
3443 "ISO C90 forbids specifying subobject to initialize");
3444 c_parser_consume_token (parser);
3446 else
3448 if (des_seen == 1)
3449 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3450 "obsolete use of designated initializer without %<=%>");
3451 else
3453 struct c_expr init;
3454 init.value = error_mark_node;
3455 init.original_code = ERROR_MARK;
3456 init.original_type = NULL;
3457 c_parser_error (parser, "expected %<=%>");
3458 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3459 process_init_element (init, false, braced_init_obstack);
3460 return;
3465 c_parser_initval (parser, NULL, braced_init_obstack);
3468 /* Parse a nested initializer; as c_parser_initializer but parses
3469 initializers within braced lists, after any designators have been
3470 applied. If AFTER is not NULL then it is an Objective-C message
3471 expression which is the primary-expression starting the
3472 initializer. */
3474 static void
3475 c_parser_initval (c_parser *parser, struct c_expr *after,
3476 struct obstack * braced_init_obstack)
3478 struct c_expr init;
3479 gcc_assert (!after || c_dialect_objc ());
3480 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3481 init = c_parser_braced_init (parser, NULL_TREE, true);
3482 else
3484 location_t loc = c_parser_peek_token (parser)->location;
3485 init = c_parser_expr_no_commas (parser, after);
3486 if (init.value != NULL_TREE
3487 && TREE_CODE (init.value) != STRING_CST
3488 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3489 init = default_function_array_read_conversion (loc, init);
3491 process_init_element (init, false, braced_init_obstack);
3494 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3495 C99 6.8.2).
3497 compound-statement:
3498 { block-item-list[opt] }
3499 { label-declarations block-item-list }
3501 block-item-list:
3502 block-item
3503 block-item-list block-item
3505 block-item:
3506 nested-declaration
3507 statement
3509 nested-declaration:
3510 declaration
3512 GNU extensions:
3514 compound-statement:
3515 { label-declarations block-item-list }
3517 nested-declaration:
3518 __extension__ nested-declaration
3519 nested-function-definition
3521 label-declarations:
3522 label-declaration
3523 label-declarations label-declaration
3525 label-declaration:
3526 __label__ identifier-list ;
3528 Allowing the mixing of declarations and code is new in C99. The
3529 GNU syntax also permits (not shown above) labels at the end of
3530 compound statements, which yield an error. We don't allow labels
3531 on declarations; this might seem like a natural extension, but
3532 there would be a conflict between attributes on the label and
3533 prefix attributes on the declaration. ??? The syntax follows the
3534 old parser in requiring something after label declarations.
3535 Although they are erroneous if the labels declared aren't defined,
3536 is it useful for the syntax to be this way?
3538 OpenMP:
3540 block-item:
3541 openmp-directive
3543 openmp-directive:
3544 barrier-directive
3545 flush-directive */
3547 static tree
3548 c_parser_compound_statement (c_parser *parser)
3550 tree stmt;
3551 location_t brace_loc;
3552 brace_loc = c_parser_peek_token (parser)->location;
3553 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3555 /* Ensure a scope is entered and left anyway to avoid confusion
3556 if we have just prepared to enter a function body. */
3557 stmt = c_begin_compound_stmt (true);
3558 c_end_compound_stmt (brace_loc, stmt, true);
3559 return error_mark_node;
3561 stmt = c_begin_compound_stmt (true);
3562 c_parser_compound_statement_nostart (parser);
3563 return c_end_compound_stmt (brace_loc, stmt, true);
3566 /* Parse a compound statement except for the opening brace. This is
3567 used for parsing both compound statements and statement expressions
3568 (which follow different paths to handling the opening). */
3570 static void
3571 c_parser_compound_statement_nostart (c_parser *parser)
3573 bool last_stmt = false;
3574 bool last_label = false;
3575 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3576 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3577 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3579 c_parser_consume_token (parser);
3580 return;
3582 mark_valid_location_for_stdc_pragma (true);
3583 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3585 /* Read zero or more forward-declarations for labels that nested
3586 functions can jump to. */
3587 mark_valid_location_for_stdc_pragma (false);
3588 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3590 label_loc = c_parser_peek_token (parser)->location;
3591 c_parser_consume_token (parser);
3592 /* Any identifiers, including those declared as type names,
3593 are OK here. */
3594 while (true)
3596 tree label;
3597 if (c_parser_next_token_is_not (parser, CPP_NAME))
3599 c_parser_error (parser, "expected identifier");
3600 break;
3602 label
3603 = declare_label (c_parser_peek_token (parser)->value);
3604 C_DECLARED_LABEL_FLAG (label) = 1;
3605 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3606 c_parser_consume_token (parser);
3607 if (c_parser_next_token_is (parser, CPP_COMMA))
3608 c_parser_consume_token (parser);
3609 else
3610 break;
3612 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3614 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3616 /* We must now have at least one statement, label or declaration. */
3617 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3619 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3620 c_parser_error (parser, "expected declaration or statement");
3621 c_parser_consume_token (parser);
3622 return;
3624 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3626 location_t loc = c_parser_peek_token (parser)->location;
3627 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3628 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3629 || (c_parser_next_token_is (parser, CPP_NAME)
3630 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3632 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3633 label_loc = c_parser_peek_2nd_token (parser)->location;
3634 else
3635 label_loc = c_parser_peek_token (parser)->location;
3636 last_label = true;
3637 last_stmt = false;
3638 mark_valid_location_for_stdc_pragma (false);
3639 c_parser_label (parser);
3641 else if (!last_label
3642 && c_parser_next_token_starts_declaration (parser))
3644 last_label = false;
3645 mark_valid_location_for_stdc_pragma (false);
3646 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
3647 if (last_stmt)
3648 pedwarn_c90 (loc,
3649 (pedantic && !flag_isoc99)
3650 ? OPT_pedantic
3651 : OPT_Wdeclaration_after_statement,
3652 "ISO C90 forbids mixed declarations and code");
3653 last_stmt = false;
3655 else if (!last_label
3656 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3658 /* __extension__ can start a declaration, but is also an
3659 unary operator that can start an expression. Consume all
3660 but the last of a possible series of __extension__ to
3661 determine which. */
3662 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3663 && (c_parser_peek_2nd_token (parser)->keyword
3664 == RID_EXTENSION))
3665 c_parser_consume_token (parser);
3666 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
3668 int ext;
3669 ext = disable_extension_diagnostics ();
3670 c_parser_consume_token (parser);
3671 last_label = false;
3672 mark_valid_location_for_stdc_pragma (false);
3673 c_parser_declaration_or_fndef (parser, true, true, true, true,
3674 true);
3675 /* Following the old parser, __extension__ does not
3676 disable this diagnostic. */
3677 restore_extension_diagnostics (ext);
3678 if (last_stmt)
3679 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3680 ? OPT_pedantic
3681 : OPT_Wdeclaration_after_statement,
3682 "ISO C90 forbids mixed declarations and code");
3683 last_stmt = false;
3685 else
3686 goto statement;
3688 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3690 /* External pragmas, and some omp pragmas, are not associated
3691 with regular c code, and so are not to be considered statements
3692 syntactically. This ensures that the user doesn't put them
3693 places that would turn into syntax errors if the directive
3694 were ignored. */
3695 if (c_parser_pragma (parser, pragma_compound))
3696 last_label = false, last_stmt = true;
3698 else if (c_parser_next_token_is (parser, CPP_EOF))
3700 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3701 c_parser_error (parser, "expected declaration or statement");
3702 return;
3704 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3706 if (parser->in_if_block)
3708 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3709 error_at (loc, """expected %<}%> before %<else%>");
3710 return;
3712 else
3714 error_at (loc, "%<else%> without a previous %<if%>");
3715 c_parser_consume_token (parser);
3716 continue;
3719 else
3721 statement:
3722 last_label = false;
3723 last_stmt = true;
3724 mark_valid_location_for_stdc_pragma (false);
3725 c_parser_statement_after_labels (parser);
3728 parser->error = false;
3730 if (last_label)
3731 error_at (label_loc, "label at end of compound statement");
3732 c_parser_consume_token (parser);
3733 /* Restore the value we started with. */
3734 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3737 /* Parse a label (C90 6.6.1, C99 6.8.1).
3739 label:
3740 identifier : attributes[opt]
3741 case constant-expression :
3742 default :
3744 GNU extensions:
3746 label:
3747 case constant-expression ... constant-expression :
3749 The use of attributes on labels is a GNU extension. The syntax in
3750 GNU C accepts any expressions without commas, non-constant
3751 expressions being rejected later. */
3753 static void
3754 c_parser_label (c_parser *parser)
3756 location_t loc1 = c_parser_peek_token (parser)->location;
3757 tree label = NULL_TREE;
3758 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3760 tree exp1, exp2;
3761 c_parser_consume_token (parser);
3762 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3763 if (c_parser_next_token_is (parser, CPP_COLON))
3765 c_parser_consume_token (parser);
3766 label = do_case (loc1, exp1, NULL_TREE);
3768 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3770 c_parser_consume_token (parser);
3771 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3772 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3773 label = do_case (loc1, exp1, exp2);
3775 else
3776 c_parser_error (parser, "expected %<:%> or %<...%>");
3778 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3780 c_parser_consume_token (parser);
3781 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3782 label = do_case (loc1, NULL_TREE, NULL_TREE);
3784 else
3786 tree name = c_parser_peek_token (parser)->value;
3787 tree tlab;
3788 tree attrs;
3789 location_t loc2 = c_parser_peek_token (parser)->location;
3790 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3791 c_parser_consume_token (parser);
3792 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3793 c_parser_consume_token (parser);
3794 attrs = c_parser_attributes (parser);
3795 tlab = define_label (loc2, name);
3796 if (tlab)
3798 decl_attributes (&tlab, attrs, 0);
3799 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
3802 if (label)
3804 if (c_parser_next_token_starts_declaration (parser)
3805 && !(c_parser_next_token_is (parser, CPP_NAME)
3806 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3808 error_at (c_parser_peek_token (parser)->location,
3809 "a label can only be part of a statement and "
3810 "a declaration is not a statement");
3811 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3812 /*static_assert_ok*/ true,
3813 /*nested*/ true, /*empty_ok*/ false,
3814 /*start_attr_ok*/ true);
3819 /* Parse a statement (C90 6.6, C99 6.8).
3821 statement:
3822 labeled-statement
3823 compound-statement
3824 expression-statement
3825 selection-statement
3826 iteration-statement
3827 jump-statement
3829 labeled-statement:
3830 label statement
3832 expression-statement:
3833 expression[opt] ;
3835 selection-statement:
3836 if-statement
3837 switch-statement
3839 iteration-statement:
3840 while-statement
3841 do-statement
3842 for-statement
3844 jump-statement:
3845 goto identifier ;
3846 continue ;
3847 break ;
3848 return expression[opt] ;
3850 GNU extensions:
3852 statement:
3853 asm-statement
3855 jump-statement:
3856 goto * expression ;
3858 Objective-C:
3860 statement:
3861 objc-throw-statement
3862 objc-try-catch-statement
3863 objc-synchronized-statement
3865 objc-throw-statement:
3866 @throw expression ;
3867 @throw ;
3869 OpenMP:
3871 statement:
3872 openmp-construct
3874 openmp-construct:
3875 parallel-construct
3876 for-construct
3877 sections-construct
3878 single-construct
3879 parallel-for-construct
3880 parallel-sections-construct
3881 master-construct
3882 critical-construct
3883 atomic-construct
3884 ordered-construct
3886 parallel-construct:
3887 parallel-directive structured-block
3889 for-construct:
3890 for-directive iteration-statement
3892 sections-construct:
3893 sections-directive section-scope
3895 single-construct:
3896 single-directive structured-block
3898 parallel-for-construct:
3899 parallel-for-directive iteration-statement
3901 parallel-sections-construct:
3902 parallel-sections-directive section-scope
3904 master-construct:
3905 master-directive structured-block
3907 critical-construct:
3908 critical-directive structured-block
3910 atomic-construct:
3911 atomic-directive expression-statement
3913 ordered-construct:
3914 ordered-directive structured-block */
3916 static void
3917 c_parser_statement (c_parser *parser)
3919 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3920 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3921 || (c_parser_next_token_is (parser, CPP_NAME)
3922 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3923 c_parser_label (parser);
3924 c_parser_statement_after_labels (parser);
3927 /* Parse a statement, other than a labeled statement. */
3929 static void
3930 c_parser_statement_after_labels (c_parser *parser)
3932 location_t loc = c_parser_peek_token (parser)->location;
3933 tree stmt = NULL_TREE;
3934 bool in_if_block = parser->in_if_block;
3935 parser->in_if_block = false;
3936 switch (c_parser_peek_token (parser)->type)
3938 case CPP_OPEN_BRACE:
3939 add_stmt (c_parser_compound_statement (parser));
3940 break;
3941 case CPP_KEYWORD:
3942 switch (c_parser_peek_token (parser)->keyword)
3944 case RID_IF:
3945 c_parser_if_statement (parser);
3946 break;
3947 case RID_SWITCH:
3948 c_parser_switch_statement (parser);
3949 break;
3950 case RID_WHILE:
3951 c_parser_while_statement (parser);
3952 break;
3953 case RID_DO:
3954 c_parser_do_statement (parser);
3955 break;
3956 case RID_FOR:
3957 c_parser_for_statement (parser);
3958 break;
3959 case RID_GOTO:
3960 c_parser_consume_token (parser);
3961 if (c_parser_next_token_is (parser, CPP_NAME))
3963 stmt = c_finish_goto_label (loc,
3964 c_parser_peek_token (parser)->value);
3965 c_parser_consume_token (parser);
3967 else if (c_parser_next_token_is (parser, CPP_MULT))
3969 c_parser_consume_token (parser);
3970 stmt = c_finish_goto_ptr (loc,
3971 c_parser_expression (parser).value);
3973 else
3974 c_parser_error (parser, "expected identifier or %<*%>");
3975 goto expect_semicolon;
3976 case RID_CONTINUE:
3977 c_parser_consume_token (parser);
3978 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
3979 goto expect_semicolon;
3980 case RID_BREAK:
3981 c_parser_consume_token (parser);
3982 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
3983 goto expect_semicolon;
3984 case RID_RETURN:
3985 c_parser_consume_token (parser);
3986 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3988 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
3989 c_parser_consume_token (parser);
3991 else
3993 struct c_expr expr = c_parser_expression_conv (parser);
3994 mark_exp_read (expr.value);
3995 stmt = c_finish_return (loc, expr.value, expr.original_type);
3996 goto expect_semicolon;
3998 break;
3999 case RID_ASM:
4000 stmt = c_parser_asm_statement (parser);
4001 break;
4002 case RID_THROW:
4003 gcc_assert (c_dialect_objc ());
4004 c_parser_consume_token (parser);
4005 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4007 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4008 c_parser_consume_token (parser);
4010 else
4012 tree expr = c_parser_expression (parser).value;
4013 expr = c_fully_fold (expr, false, NULL);
4014 stmt = objc_build_throw_stmt (loc, expr);
4015 goto expect_semicolon;
4017 break;
4018 case RID_TRY:
4019 gcc_assert (c_dialect_objc ());
4020 c_parser_objc_try_catch_statement (parser);
4021 break;
4022 case RID_AT_SYNCHRONIZED:
4023 gcc_assert (c_dialect_objc ());
4024 c_parser_objc_synchronized_statement (parser);
4025 break;
4026 default:
4027 goto expr_stmt;
4029 break;
4030 case CPP_SEMICOLON:
4031 c_parser_consume_token (parser);
4032 break;
4033 case CPP_CLOSE_PAREN:
4034 case CPP_CLOSE_SQUARE:
4035 /* Avoid infinite loop in error recovery:
4036 c_parser_skip_until_found stops at a closing nesting
4037 delimiter without consuming it, but here we need to consume
4038 it to proceed further. */
4039 c_parser_error (parser, "expected statement");
4040 c_parser_consume_token (parser);
4041 break;
4042 case CPP_PRAGMA:
4043 c_parser_pragma (parser, pragma_stmt);
4044 break;
4045 default:
4046 expr_stmt:
4047 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4048 expect_semicolon:
4049 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4050 break;
4052 /* Two cases cannot and do not have line numbers associated: If stmt
4053 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4054 cannot hold line numbers. But that's OK because the statement
4055 will either be changed to a MODIFY_EXPR during gimplification of
4056 the statement expr, or discarded. If stmt was compound, but
4057 without new variables, we will have skipped the creation of a
4058 BIND and will have a bare STATEMENT_LIST. But that's OK because
4059 (recursively) all of the component statements should already have
4060 line numbers assigned. ??? Can we discard no-op statements
4061 earlier? */
4062 if (CAN_HAVE_LOCATION_P (stmt)
4063 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4064 SET_EXPR_LOCATION (stmt, loc);
4066 parser->in_if_block = in_if_block;
4069 /* Parse the condition from an if, do, while or for statements. */
4071 static tree
4072 c_parser_condition (c_parser *parser)
4074 location_t loc = c_parser_peek_token (parser)->location;
4075 tree cond;
4076 cond = c_parser_expression_conv (parser).value;
4077 cond = c_objc_common_truthvalue_conversion (loc, cond);
4078 cond = c_fully_fold (cond, false, NULL);
4079 if (warn_sequence_point)
4080 verify_sequence_points (cond);
4081 return cond;
4084 /* Parse a parenthesized condition from an if, do or while statement.
4086 condition:
4087 ( expression )
4089 static tree
4090 c_parser_paren_condition (c_parser *parser)
4092 tree cond;
4093 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4094 return error_mark_node;
4095 cond = c_parser_condition (parser);
4096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4097 return cond;
4100 /* Parse a statement which is a block in C99. */
4102 static tree
4103 c_parser_c99_block_statement (c_parser *parser)
4105 tree block = c_begin_compound_stmt (flag_isoc99);
4106 location_t loc = c_parser_peek_token (parser)->location;
4107 c_parser_statement (parser);
4108 return c_end_compound_stmt (loc, block, flag_isoc99);
4111 /* Parse the body of an if statement. This is just parsing a
4112 statement but (a) it is a block in C99, (b) we track whether the
4113 body is an if statement for the sake of -Wparentheses warnings, (c)
4114 we handle an empty body specially for the sake of -Wempty-body
4115 warnings, and (d) we call parser_compound_statement directly
4116 because c_parser_statement_after_labels resets
4117 parser->in_if_block. */
4119 static tree
4120 c_parser_if_body (c_parser *parser, bool *if_p)
4122 tree block = c_begin_compound_stmt (flag_isoc99);
4123 location_t body_loc = c_parser_peek_token (parser)->location;
4124 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4125 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4126 || (c_parser_next_token_is (parser, CPP_NAME)
4127 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4128 c_parser_label (parser);
4129 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4130 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4132 location_t loc = c_parser_peek_token (parser)->location;
4133 add_stmt (build_empty_stmt (loc));
4134 c_parser_consume_token (parser);
4135 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4136 warning_at (loc, OPT_Wempty_body,
4137 "suggest braces around empty body in an %<if%> statement");
4139 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4140 add_stmt (c_parser_compound_statement (parser));
4141 else
4142 c_parser_statement_after_labels (parser);
4143 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4146 /* Parse the else body of an if statement. This is just parsing a
4147 statement but (a) it is a block in C99, (b) we handle an empty body
4148 specially for the sake of -Wempty-body warnings. */
4150 static tree
4151 c_parser_else_body (c_parser *parser)
4153 location_t else_loc = c_parser_peek_token (parser)->location;
4154 tree block = c_begin_compound_stmt (flag_isoc99);
4155 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4156 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4157 || (c_parser_next_token_is (parser, CPP_NAME)
4158 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4159 c_parser_label (parser);
4160 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4162 location_t loc = c_parser_peek_token (parser)->location;
4163 warning_at (loc,
4164 OPT_Wempty_body,
4165 "suggest braces around empty body in an %<else%> statement");
4166 add_stmt (build_empty_stmt (loc));
4167 c_parser_consume_token (parser);
4169 else
4170 c_parser_statement_after_labels (parser);
4171 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4174 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4176 if-statement:
4177 if ( expression ) statement
4178 if ( expression ) statement else statement
4181 static void
4182 c_parser_if_statement (c_parser *parser)
4184 tree block;
4185 location_t loc;
4186 tree cond;
4187 bool first_if = false;
4188 tree first_body, second_body;
4189 bool in_if_block;
4191 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4192 c_parser_consume_token (parser);
4193 block = c_begin_compound_stmt (flag_isoc99);
4194 loc = c_parser_peek_token (parser)->location;
4195 cond = c_parser_paren_condition (parser);
4196 in_if_block = parser->in_if_block;
4197 parser->in_if_block = true;
4198 first_body = c_parser_if_body (parser, &first_if);
4199 parser->in_if_block = in_if_block;
4200 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4202 c_parser_consume_token (parser);
4203 second_body = c_parser_else_body (parser);
4205 else
4206 second_body = NULL_TREE;
4207 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4208 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4211 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4213 switch-statement:
4214 switch (expression) statement
4217 static void
4218 c_parser_switch_statement (c_parser *parser)
4220 tree block, expr, body, save_break;
4221 location_t switch_loc = c_parser_peek_token (parser)->location;
4222 location_t switch_cond_loc;
4223 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4224 c_parser_consume_token (parser);
4225 block = c_begin_compound_stmt (flag_isoc99);
4226 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4228 switch_cond_loc = c_parser_peek_token (parser)->location;
4229 expr = c_parser_expression (parser).value;
4230 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4232 else
4234 switch_cond_loc = UNKNOWN_LOCATION;
4235 expr = error_mark_node;
4237 c_start_case (switch_loc, switch_cond_loc, expr);
4238 save_break = c_break_label;
4239 c_break_label = NULL_TREE;
4240 body = c_parser_c99_block_statement (parser);
4241 c_finish_case (body);
4242 if (c_break_label)
4244 location_t here = c_parser_peek_token (parser)->location;
4245 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4246 SET_EXPR_LOCATION (t, here);
4247 add_stmt (t);
4249 c_break_label = save_break;
4250 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4253 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4255 while-statement:
4256 while (expression) statement
4259 static void
4260 c_parser_while_statement (c_parser *parser)
4262 tree block, cond, body, save_break, save_cont;
4263 location_t loc;
4264 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4265 c_parser_consume_token (parser);
4266 block = c_begin_compound_stmt (flag_isoc99);
4267 loc = c_parser_peek_token (parser)->location;
4268 cond = c_parser_paren_condition (parser);
4269 save_break = c_break_label;
4270 c_break_label = NULL_TREE;
4271 save_cont = c_cont_label;
4272 c_cont_label = NULL_TREE;
4273 body = c_parser_c99_block_statement (parser);
4274 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4275 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4276 c_break_label = save_break;
4277 c_cont_label = save_cont;
4280 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4282 do-statement:
4283 do statement while ( expression ) ;
4286 static void
4287 c_parser_do_statement (c_parser *parser)
4289 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4290 location_t loc;
4291 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4292 c_parser_consume_token (parser);
4293 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4294 warning_at (c_parser_peek_token (parser)->location,
4295 OPT_Wempty_body,
4296 "suggest braces around empty body in %<do%> statement");
4297 block = c_begin_compound_stmt (flag_isoc99);
4298 loc = c_parser_peek_token (parser)->location;
4299 save_break = c_break_label;
4300 c_break_label = NULL_TREE;
4301 save_cont = c_cont_label;
4302 c_cont_label = NULL_TREE;
4303 body = c_parser_c99_block_statement (parser);
4304 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4305 new_break = c_break_label;
4306 c_break_label = save_break;
4307 new_cont = c_cont_label;
4308 c_cont_label = save_cont;
4309 cond = c_parser_paren_condition (parser);
4310 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4311 c_parser_skip_to_end_of_block_or_statement (parser);
4312 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4313 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4316 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4318 for-statement:
4319 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4320 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4322 The form with a declaration is new in C99.
4324 ??? In accordance with the old parser, the declaration may be a
4325 nested function, which is then rejected in check_for_loop_decls,
4326 but does it make any sense for this to be included in the grammar?
4327 Note in particular that the nested function does not include a
4328 trailing ';', whereas the "declaration" production includes one.
4329 Also, can we reject bad declarations earlier and cheaper than
4330 check_for_loop_decls? */
4332 static void
4333 c_parser_for_statement (c_parser *parser)
4335 tree block, cond, incr, save_break, save_cont, body;
4336 location_t loc = c_parser_peek_token (parser)->location;
4337 location_t for_loc = c_parser_peek_token (parser)->location;
4338 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4339 c_parser_consume_token (parser);
4340 block = c_begin_compound_stmt (flag_isoc99);
4341 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4343 /* Parse the initialization declaration or expression. */
4344 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4346 c_parser_consume_token (parser);
4347 c_finish_expr_stmt (loc, NULL_TREE);
4349 else if (c_parser_next_token_starts_declaration (parser))
4351 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
4352 check_for_loop_decls (for_loc);
4354 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4356 /* __extension__ can start a declaration, but is also an
4357 unary operator that can start an expression. Consume all
4358 but the last of a possible series of __extension__ to
4359 determine which. */
4360 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4361 && (c_parser_peek_2nd_token (parser)->keyword
4362 == RID_EXTENSION))
4363 c_parser_consume_token (parser);
4364 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4366 int ext;
4367 ext = disable_extension_diagnostics ();
4368 c_parser_consume_token (parser);
4369 c_parser_declaration_or_fndef (parser, true, true, true, true,
4370 true);
4371 restore_extension_diagnostics (ext);
4372 check_for_loop_decls (for_loc);
4374 else
4375 goto init_expr;
4377 else
4379 init_expr:
4380 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
4381 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4383 /* Parse the loop condition. */
4384 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4386 c_parser_consume_token (parser);
4387 cond = NULL_TREE;
4389 else
4391 cond = c_parser_condition (parser);
4392 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4394 /* Parse the increment expression. */
4395 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4396 incr = c_process_expr_stmt (loc, NULL_TREE);
4397 else
4398 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4401 else
4403 cond = error_mark_node;
4404 incr = error_mark_node;
4406 save_break = c_break_label;
4407 c_break_label = NULL_TREE;
4408 save_cont = c_cont_label;
4409 c_cont_label = NULL_TREE;
4410 body = c_parser_c99_block_statement (parser);
4411 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4412 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4413 c_break_label = save_break;
4414 c_cont_label = save_cont;
4417 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4418 statement with inputs, outputs, clobbers, and volatile tag
4419 allowed.
4421 asm-statement:
4422 asm type-qualifier[opt] ( asm-argument ) ;
4423 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4425 asm-argument:
4426 asm-string-literal
4427 asm-string-literal : asm-operands[opt]
4428 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4429 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4431 asm-goto-argument:
4432 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4433 : asm-goto-operands
4435 Qualifiers other than volatile are accepted in the syntax but
4436 warned for. */
4438 static tree
4439 c_parser_asm_statement (c_parser *parser)
4441 tree quals, str, outputs, inputs, clobbers, labels, ret;
4442 bool simple, is_goto;
4443 location_t asm_loc = c_parser_peek_token (parser)->location;
4444 int section, nsections;
4446 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4447 c_parser_consume_token (parser);
4448 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4450 quals = c_parser_peek_token (parser)->value;
4451 c_parser_consume_token (parser);
4453 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4454 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4456 warning_at (c_parser_peek_token (parser)->location,
4458 "%E qualifier ignored on asm",
4459 c_parser_peek_token (parser)->value);
4460 quals = NULL_TREE;
4461 c_parser_consume_token (parser);
4463 else
4464 quals = NULL_TREE;
4466 is_goto = false;
4467 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4469 c_parser_consume_token (parser);
4470 is_goto = true;
4473 /* ??? Follow the C++ parser rather than using the
4474 lex_untranslated_string kludge. */
4475 parser->lex_untranslated_string = true;
4476 ret = NULL;
4478 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4479 goto error;
4481 str = c_parser_asm_string_literal (parser);
4482 if (str == NULL_TREE)
4483 goto error_close_paren;
4485 simple = true;
4486 outputs = NULL_TREE;
4487 inputs = NULL_TREE;
4488 clobbers = NULL_TREE;
4489 labels = NULL_TREE;
4491 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4492 goto done_asm;
4494 /* Parse each colon-delimited section of operands. */
4495 nsections = 3 + is_goto;
4496 for (section = 0; section < nsections; ++section)
4498 if (!c_parser_require (parser, CPP_COLON,
4499 is_goto
4500 ? "expected %<:%>"
4501 : "expected %<:%> or %<)%>"))
4502 goto error_close_paren;
4504 /* Once past any colon, we're no longer a simple asm. */
4505 simple = false;
4507 if ((!c_parser_next_token_is (parser, CPP_COLON)
4508 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4509 || section == 3)
4510 switch (section)
4512 case 0:
4513 /* For asm goto, we don't allow output operands, but reserve
4514 the slot for a future extension that does allow them. */
4515 if (!is_goto)
4516 outputs = c_parser_asm_operands (parser, false);
4517 break;
4518 case 1:
4519 inputs = c_parser_asm_operands (parser, true);
4520 break;
4521 case 2:
4522 clobbers = c_parser_asm_clobbers (parser);
4523 break;
4524 case 3:
4525 labels = c_parser_asm_goto_operands (parser);
4526 break;
4527 default:
4528 gcc_unreachable ();
4531 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4532 goto done_asm;
4535 done_asm:
4536 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4538 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4539 goto error;
4542 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4543 c_parser_skip_to_end_of_block_or_statement (parser);
4545 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4546 clobbers, labels, simple));
4548 error:
4549 parser->lex_untranslated_string = false;
4550 return ret;
4552 error_close_paren:
4553 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4554 goto error;
4557 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4558 not outputs), apply the default conversion of functions and arrays
4559 to pointers.
4561 asm-operands:
4562 asm-operand
4563 asm-operands , asm-operand
4565 asm-operand:
4566 asm-string-literal ( expression )
4567 [ identifier ] asm-string-literal ( expression )
4570 static tree
4571 c_parser_asm_operands (c_parser *parser, bool convert_p)
4573 tree list = NULL_TREE;
4574 location_t loc;
4575 while (true)
4577 tree name, str;
4578 struct c_expr expr;
4579 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4581 c_parser_consume_token (parser);
4582 if (c_parser_next_token_is (parser, CPP_NAME))
4584 tree id = c_parser_peek_token (parser)->value;
4585 c_parser_consume_token (parser);
4586 name = build_string (IDENTIFIER_LENGTH (id),
4587 IDENTIFIER_POINTER (id));
4589 else
4591 c_parser_error (parser, "expected identifier");
4592 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4593 return NULL_TREE;
4595 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4596 "expected %<]%>");
4598 else
4599 name = NULL_TREE;
4600 str = c_parser_asm_string_literal (parser);
4601 if (str == NULL_TREE)
4602 return NULL_TREE;
4603 parser->lex_untranslated_string = false;
4604 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4606 parser->lex_untranslated_string = true;
4607 return NULL_TREE;
4609 loc = c_parser_peek_token (parser)->location;
4610 expr = c_parser_expression (parser);
4611 mark_exp_read (expr.value);
4612 if (convert_p)
4613 expr = default_function_array_conversion (loc, expr);
4614 expr.value = c_fully_fold (expr.value, false, NULL);
4615 parser->lex_untranslated_string = true;
4616 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4618 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4619 return NULL_TREE;
4621 list = chainon (list, build_tree_list (build_tree_list (name, str),
4622 expr.value));
4623 if (c_parser_next_token_is (parser, CPP_COMMA))
4624 c_parser_consume_token (parser);
4625 else
4626 break;
4628 return list;
4631 /* Parse asm clobbers, a GNU extension.
4633 asm-clobbers:
4634 asm-string-literal
4635 asm-clobbers , asm-string-literal
4638 static tree
4639 c_parser_asm_clobbers (c_parser *parser)
4641 tree list = NULL_TREE;
4642 while (true)
4644 tree str = c_parser_asm_string_literal (parser);
4645 if (str)
4646 list = tree_cons (NULL_TREE, str, list);
4647 else
4648 return NULL_TREE;
4649 if (c_parser_next_token_is (parser, CPP_COMMA))
4650 c_parser_consume_token (parser);
4651 else
4652 break;
4654 return list;
4657 /* Parse asm goto labels, a GNU extension.
4659 asm-goto-operands:
4660 identifier
4661 asm-goto-operands , identifier
4664 static tree
4665 c_parser_asm_goto_operands (c_parser *parser)
4667 tree list = NULL_TREE;
4668 while (true)
4670 tree name, label;
4672 if (c_parser_next_token_is (parser, CPP_NAME))
4674 c_token *tok = c_parser_peek_token (parser);
4675 name = tok->value;
4676 label = lookup_label_for_goto (tok->location, name);
4677 c_parser_consume_token (parser);
4678 TREE_USED (label) = 1;
4680 else
4682 c_parser_error (parser, "expected identifier");
4683 return NULL_TREE;
4686 name = build_string (IDENTIFIER_LENGTH (name),
4687 IDENTIFIER_POINTER (name));
4688 list = tree_cons (name, label, list);
4689 if (c_parser_next_token_is (parser, CPP_COMMA))
4690 c_parser_consume_token (parser);
4691 else
4692 return nreverse (list);
4696 /* Parse an expression other than a compound expression; that is, an
4697 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4698 NULL then it is an Objective-C message expression which is the
4699 primary-expression starting the expression as an initializer.
4701 assignment-expression:
4702 conditional-expression
4703 unary-expression assignment-operator assignment-expression
4705 assignment-operator: one of
4706 = *= /= %= += -= <<= >>= &= ^= |=
4708 In GNU C we accept any conditional expression on the LHS and
4709 diagnose the invalid lvalue rather than producing a syntax
4710 error. */
4712 static struct c_expr
4713 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4715 struct c_expr lhs, rhs, ret;
4716 enum tree_code code;
4717 location_t op_location, exp_location;
4718 gcc_assert (!after || c_dialect_objc ());
4719 lhs = c_parser_conditional_expression (parser, after);
4720 op_location = c_parser_peek_token (parser)->location;
4721 switch (c_parser_peek_token (parser)->type)
4723 case CPP_EQ:
4724 code = NOP_EXPR;
4725 break;
4726 case CPP_MULT_EQ:
4727 code = MULT_EXPR;
4728 break;
4729 case CPP_DIV_EQ:
4730 code = TRUNC_DIV_EXPR;
4731 break;
4732 case CPP_MOD_EQ:
4733 code = TRUNC_MOD_EXPR;
4734 break;
4735 case CPP_PLUS_EQ:
4736 code = PLUS_EXPR;
4737 break;
4738 case CPP_MINUS_EQ:
4739 code = MINUS_EXPR;
4740 break;
4741 case CPP_LSHIFT_EQ:
4742 code = LSHIFT_EXPR;
4743 break;
4744 case CPP_RSHIFT_EQ:
4745 code = RSHIFT_EXPR;
4746 break;
4747 case CPP_AND_EQ:
4748 code = BIT_AND_EXPR;
4749 break;
4750 case CPP_XOR_EQ:
4751 code = BIT_XOR_EXPR;
4752 break;
4753 case CPP_OR_EQ:
4754 code = BIT_IOR_EXPR;
4755 break;
4756 default:
4757 return lhs;
4759 c_parser_consume_token (parser);
4760 exp_location = c_parser_peek_token (parser)->location;
4761 rhs = c_parser_expr_no_commas (parser, NULL);
4762 rhs = default_function_array_read_conversion (exp_location, rhs);
4763 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
4764 code, exp_location, rhs.value,
4765 rhs.original_type);
4766 if (code == NOP_EXPR)
4767 ret.original_code = MODIFY_EXPR;
4768 else
4770 TREE_NO_WARNING (ret.value) = 1;
4771 ret.original_code = ERROR_MARK;
4773 ret.original_type = NULL;
4774 return ret;
4777 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4778 is not NULL then it is an Objective-C message expression which is
4779 the primary-expression starting the expression as an initializer.
4781 conditional-expression:
4782 logical-OR-expression
4783 logical-OR-expression ? expression : conditional-expression
4785 GNU extensions:
4787 conditional-expression:
4788 logical-OR-expression ? : conditional-expression
4791 static struct c_expr
4792 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4794 struct c_expr cond, exp1, exp2, ret;
4795 location_t cond_loc, colon_loc, middle_loc;
4797 gcc_assert (!after || c_dialect_objc ());
4799 cond = c_parser_binary_expression (parser, after);
4801 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4802 return cond;
4803 cond_loc = c_parser_peek_token (parser)->location;
4804 cond = default_function_array_read_conversion (cond_loc, cond);
4805 c_parser_consume_token (parser);
4806 if (c_parser_next_token_is (parser, CPP_COLON))
4808 tree eptype = NULL_TREE;
4810 middle_loc = c_parser_peek_token (parser)->location;
4811 pedwarn (middle_loc, OPT_pedantic,
4812 "ISO C forbids omitting the middle term of a ?: expression");
4813 warn_for_omitted_condop (middle_loc, cond.value);
4814 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4816 eptype = TREE_TYPE (cond.value);
4817 cond.value = TREE_OPERAND (cond.value, 0);
4819 /* Make sure first operand is calculated only once. */
4820 exp1.value = c_save_expr (default_conversion (cond.value));
4821 if (eptype)
4822 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
4823 exp1.original_type = NULL;
4824 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4825 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
4827 else
4829 cond.value
4830 = c_objc_common_truthvalue_conversion
4831 (cond_loc, default_conversion (cond.value));
4832 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
4833 exp1 = c_parser_expression_conv (parser);
4834 mark_exp_read (exp1.value);
4835 c_inhibit_evaluation_warnings +=
4836 ((cond.value == truthvalue_true_node)
4837 - (cond.value == truthvalue_false_node));
4840 colon_loc = c_parser_peek_token (parser)->location;
4841 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4843 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4844 ret.value = error_mark_node;
4845 ret.original_code = ERROR_MARK;
4846 ret.original_type = NULL;
4847 return ret;
4850 location_t exp2_loc = c_parser_peek_token (parser)->location;
4851 exp2 = c_parser_conditional_expression (parser, NULL);
4852 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
4854 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4855 ret.value = build_conditional_expr (colon_loc, cond.value,
4856 cond.original_code == C_MAYBE_CONST_EXPR,
4857 exp1.value, exp1.original_type,
4858 exp2.value, exp2.original_type);
4859 ret.original_code = ERROR_MARK;
4860 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4861 ret.original_type = NULL;
4862 else
4864 tree t1, t2;
4866 /* If both sides are enum type, the default conversion will have
4867 made the type of the result be an integer type. We want to
4868 remember the enum types we started with. */
4869 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4870 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4871 ret.original_type = ((t1 != error_mark_node
4872 && t2 != error_mark_node
4873 && (TYPE_MAIN_VARIANT (t1)
4874 == TYPE_MAIN_VARIANT (t2)))
4875 ? t1
4876 : NULL);
4878 return ret;
4881 /* Parse a binary expression; that is, a logical-OR-expression (C90
4882 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4883 an Objective-C message expression which is the primary-expression
4884 starting the expression as an initializer.
4886 multiplicative-expression:
4887 cast-expression
4888 multiplicative-expression * cast-expression
4889 multiplicative-expression / cast-expression
4890 multiplicative-expression % cast-expression
4892 additive-expression:
4893 multiplicative-expression
4894 additive-expression + multiplicative-expression
4895 additive-expression - multiplicative-expression
4897 shift-expression:
4898 additive-expression
4899 shift-expression << additive-expression
4900 shift-expression >> additive-expression
4902 relational-expression:
4903 shift-expression
4904 relational-expression < shift-expression
4905 relational-expression > shift-expression
4906 relational-expression <= shift-expression
4907 relational-expression >= shift-expression
4909 equality-expression:
4910 relational-expression
4911 equality-expression == relational-expression
4912 equality-expression != relational-expression
4914 AND-expression:
4915 equality-expression
4916 AND-expression & equality-expression
4918 exclusive-OR-expression:
4919 AND-expression
4920 exclusive-OR-expression ^ AND-expression
4922 inclusive-OR-expression:
4923 exclusive-OR-expression
4924 inclusive-OR-expression | exclusive-OR-expression
4926 logical-AND-expression:
4927 inclusive-OR-expression
4928 logical-AND-expression && inclusive-OR-expression
4930 logical-OR-expression:
4931 logical-AND-expression
4932 logical-OR-expression || logical-AND-expression
4935 static struct c_expr
4936 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4938 /* A binary expression is parsed using operator-precedence parsing,
4939 with the operands being cast expressions. All the binary
4940 operators are left-associative. Thus a binary expression is of
4941 form:
4943 E0 op1 E1 op2 E2 ...
4945 which we represent on a stack. On the stack, the precedence
4946 levels are strictly increasing. When a new operator is
4947 encountered of higher precedence than that at the top of the
4948 stack, it is pushed; its LHS is the top expression, and its RHS
4949 is everything parsed until it is popped. When a new operator is
4950 encountered with precedence less than or equal to that at the top
4951 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4952 by the result of the operation until the operator at the top of
4953 the stack has lower precedence than the new operator or there is
4954 only one element on the stack; then the top expression is the LHS
4955 of the new operator. In the case of logical AND and OR
4956 expressions, we also need to adjust c_inhibit_evaluation_warnings
4957 as appropriate when the operators are pushed and popped. */
4959 /* The precedence levels, where 0 is a dummy lowest level used for
4960 the bottom of the stack. */
4961 enum prec {
4962 PREC_NONE,
4963 PREC_LOGOR,
4964 PREC_LOGAND,
4965 PREC_BITOR,
4966 PREC_BITXOR,
4967 PREC_BITAND,
4968 PREC_EQ,
4969 PREC_REL,
4970 PREC_SHIFT,
4971 PREC_ADD,
4972 PREC_MULT,
4973 NUM_PRECS
4975 struct {
4976 /* The expression at this stack level. */
4977 struct c_expr expr;
4978 /* The precedence of the operator on its left, PREC_NONE at the
4979 bottom of the stack. */
4980 enum prec prec;
4981 /* The operation on its left. */
4982 enum tree_code op;
4983 /* The source location of this operation. */
4984 location_t loc;
4985 } stack[NUM_PRECS];
4986 int sp;
4987 /* Location of the binary operator. */
4988 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4989 #define POP \
4990 do { \
4991 switch (stack[sp].op) \
4993 case TRUTH_ANDIF_EXPR: \
4994 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4995 == truthvalue_false_node); \
4996 break; \
4997 case TRUTH_ORIF_EXPR: \
4998 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4999 == truthvalue_true_node); \
5000 break; \
5001 default: \
5002 break; \
5004 stack[sp - 1].expr \
5005 = default_function_array_read_conversion (stack[sp - 1].loc, \
5006 stack[sp - 1].expr); \
5007 stack[sp].expr \
5008 = default_function_array_read_conversion (stack[sp].loc, \
5009 stack[sp].expr); \
5010 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5011 stack[sp].op, \
5012 stack[sp - 1].expr, \
5013 stack[sp].expr); \
5014 sp--; \
5015 } while (0)
5016 gcc_assert (!after || c_dialect_objc ());
5017 stack[0].loc = c_parser_peek_token (parser)->location;
5018 stack[0].expr = c_parser_cast_expression (parser, after);
5019 stack[0].prec = PREC_NONE;
5020 sp = 0;
5021 while (true)
5023 enum prec oprec;
5024 enum tree_code ocode;
5025 if (parser->error)
5026 goto out;
5027 switch (c_parser_peek_token (parser)->type)
5029 case CPP_MULT:
5030 oprec = PREC_MULT;
5031 ocode = MULT_EXPR;
5032 break;
5033 case CPP_DIV:
5034 oprec = PREC_MULT;
5035 ocode = TRUNC_DIV_EXPR;
5036 break;
5037 case CPP_MOD:
5038 oprec = PREC_MULT;
5039 ocode = TRUNC_MOD_EXPR;
5040 break;
5041 case CPP_PLUS:
5042 oprec = PREC_ADD;
5043 ocode = PLUS_EXPR;
5044 break;
5045 case CPP_MINUS:
5046 oprec = PREC_ADD;
5047 ocode = MINUS_EXPR;
5048 break;
5049 case CPP_LSHIFT:
5050 oprec = PREC_SHIFT;
5051 ocode = LSHIFT_EXPR;
5052 break;
5053 case CPP_RSHIFT:
5054 oprec = PREC_SHIFT;
5055 ocode = RSHIFT_EXPR;
5056 break;
5057 case CPP_LESS:
5058 oprec = PREC_REL;
5059 ocode = LT_EXPR;
5060 break;
5061 case CPP_GREATER:
5062 oprec = PREC_REL;
5063 ocode = GT_EXPR;
5064 break;
5065 case CPP_LESS_EQ:
5066 oprec = PREC_REL;
5067 ocode = LE_EXPR;
5068 break;
5069 case CPP_GREATER_EQ:
5070 oprec = PREC_REL;
5071 ocode = GE_EXPR;
5072 break;
5073 case CPP_EQ_EQ:
5074 oprec = PREC_EQ;
5075 ocode = EQ_EXPR;
5076 break;
5077 case CPP_NOT_EQ:
5078 oprec = PREC_EQ;
5079 ocode = NE_EXPR;
5080 break;
5081 case CPP_AND:
5082 oprec = PREC_BITAND;
5083 ocode = BIT_AND_EXPR;
5084 break;
5085 case CPP_XOR:
5086 oprec = PREC_BITXOR;
5087 ocode = BIT_XOR_EXPR;
5088 break;
5089 case CPP_OR:
5090 oprec = PREC_BITOR;
5091 ocode = BIT_IOR_EXPR;
5092 break;
5093 case CPP_AND_AND:
5094 oprec = PREC_LOGAND;
5095 ocode = TRUTH_ANDIF_EXPR;
5096 break;
5097 case CPP_OR_OR:
5098 oprec = PREC_LOGOR;
5099 ocode = TRUTH_ORIF_EXPR;
5100 break;
5101 default:
5102 /* Not a binary operator, so end of the binary
5103 expression. */
5104 goto out;
5106 binary_loc = c_parser_peek_token (parser)->location;
5107 c_parser_consume_token (parser);
5108 while (oprec <= stack[sp].prec)
5109 POP;
5110 switch (ocode)
5112 case TRUTH_ANDIF_EXPR:
5113 stack[sp].expr
5114 = default_function_array_read_conversion (stack[sp].loc,
5115 stack[sp].expr);
5116 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5117 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5118 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5119 == truthvalue_false_node);
5120 break;
5121 case TRUTH_ORIF_EXPR:
5122 stack[sp].expr
5123 = default_function_array_read_conversion (stack[sp].loc,
5124 stack[sp].expr);
5125 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5126 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5127 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5128 == truthvalue_true_node);
5129 break;
5130 default:
5131 break;
5133 sp++;
5134 stack[sp].loc = binary_loc;
5135 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5136 stack[sp].prec = oprec;
5137 stack[sp].op = ocode;
5138 stack[sp].loc = binary_loc;
5140 out:
5141 while (sp > 0)
5142 POP;
5143 return stack[0].expr;
5144 #undef POP
5147 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5148 NULL then it is an Objective-C message expression which is the
5149 primary-expression starting the expression as an initializer.
5151 cast-expression:
5152 unary-expression
5153 ( type-name ) unary-expression
5156 static struct c_expr
5157 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5159 location_t cast_loc = c_parser_peek_token (parser)->location;
5160 gcc_assert (!after || c_dialect_objc ());
5161 if (after)
5162 return c_parser_postfix_expression_after_primary (parser,
5163 cast_loc, *after);
5164 /* If the expression begins with a parenthesized type name, it may
5165 be either a cast or a compound literal; we need to see whether
5166 the next character is '{' to tell the difference. If not, it is
5167 an unary expression. */
5168 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5169 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5171 struct c_type_name *type_name;
5172 struct c_expr ret;
5173 struct c_expr expr;
5174 c_parser_consume_token (parser);
5175 type_name = c_parser_type_name (parser);
5176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5177 if (type_name == NULL)
5179 ret.value = error_mark_node;
5180 ret.original_code = ERROR_MARK;
5181 ret.original_type = NULL;
5182 return ret;
5185 /* Save casted types in the function's used types hash table. */
5186 used_types_insert (type_name->specs->type);
5188 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5189 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5190 cast_loc);
5192 location_t expr_loc = c_parser_peek_token (parser)->location;
5193 expr = c_parser_cast_expression (parser, NULL);
5194 expr = default_function_array_read_conversion (expr_loc, expr);
5196 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5197 ret.original_code = ERROR_MARK;
5198 ret.original_type = NULL;
5199 return ret;
5201 else
5202 return c_parser_unary_expression (parser);
5205 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5207 unary-expression:
5208 postfix-expression
5209 ++ unary-expression
5210 -- unary-expression
5211 unary-operator cast-expression
5212 sizeof unary-expression
5213 sizeof ( type-name )
5215 unary-operator: one of
5216 & * + - ~ !
5218 GNU extensions:
5220 unary-expression:
5221 __alignof__ unary-expression
5222 __alignof__ ( type-name )
5223 && identifier
5225 unary-operator: one of
5226 __extension__ __real__ __imag__
5228 In addition, the GNU syntax treats ++ and -- as unary operators, so
5229 they may be applied to cast expressions with errors for non-lvalues
5230 given later. */
5232 static struct c_expr
5233 c_parser_unary_expression (c_parser *parser)
5235 int ext;
5236 struct c_expr ret, op;
5237 location_t op_loc = c_parser_peek_token (parser)->location;
5238 location_t exp_loc;
5239 ret.original_code = ERROR_MARK;
5240 ret.original_type = NULL;
5241 switch (c_parser_peek_token (parser)->type)
5243 case CPP_PLUS_PLUS:
5244 c_parser_consume_token (parser);
5245 exp_loc = c_parser_peek_token (parser)->location;
5246 op = c_parser_cast_expression (parser, NULL);
5247 op = default_function_array_read_conversion (exp_loc, op);
5248 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5249 case CPP_MINUS_MINUS:
5250 c_parser_consume_token (parser);
5251 exp_loc = c_parser_peek_token (parser)->location;
5252 op = c_parser_cast_expression (parser, NULL);
5253 op = default_function_array_read_conversion (exp_loc, op);
5254 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5255 case CPP_AND:
5256 c_parser_consume_token (parser);
5257 op = c_parser_cast_expression (parser, NULL);
5258 mark_exp_read (op.value);
5259 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5260 case CPP_MULT:
5261 c_parser_consume_token (parser);
5262 exp_loc = c_parser_peek_token (parser)->location;
5263 op = c_parser_cast_expression (parser, NULL);
5264 op = default_function_array_read_conversion (exp_loc, op);
5265 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5266 return ret;
5267 case CPP_PLUS:
5268 if (!c_dialect_objc () && !in_system_header)
5269 warning_at (op_loc,
5270 OPT_Wtraditional,
5271 "traditional C rejects the unary plus operator");
5272 c_parser_consume_token (parser);
5273 exp_loc = c_parser_peek_token (parser)->location;
5274 op = c_parser_cast_expression (parser, NULL);
5275 op = default_function_array_read_conversion (exp_loc, op);
5276 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5277 case CPP_MINUS:
5278 c_parser_consume_token (parser);
5279 exp_loc = c_parser_peek_token (parser)->location;
5280 op = c_parser_cast_expression (parser, NULL);
5281 op = default_function_array_read_conversion (exp_loc, op);
5282 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5283 case CPP_COMPL:
5284 c_parser_consume_token (parser);
5285 exp_loc = c_parser_peek_token (parser)->location;
5286 op = c_parser_cast_expression (parser, NULL);
5287 op = default_function_array_read_conversion (exp_loc, op);
5288 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5289 case CPP_NOT:
5290 c_parser_consume_token (parser);
5291 exp_loc = c_parser_peek_token (parser)->location;
5292 op = c_parser_cast_expression (parser, NULL);
5293 op = default_function_array_read_conversion (exp_loc, op);
5294 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5295 case CPP_AND_AND:
5296 /* Refer to the address of a label as a pointer. */
5297 c_parser_consume_token (parser);
5298 if (c_parser_next_token_is (parser, CPP_NAME))
5300 ret.value = finish_label_address_expr
5301 (c_parser_peek_token (parser)->value, op_loc);
5302 c_parser_consume_token (parser);
5304 else
5306 c_parser_error (parser, "expected identifier");
5307 ret.value = error_mark_node;
5309 return ret;
5310 case CPP_KEYWORD:
5311 switch (c_parser_peek_token (parser)->keyword)
5313 case RID_SIZEOF:
5314 return c_parser_sizeof_expression (parser);
5315 case RID_ALIGNOF:
5316 return c_parser_alignof_expression (parser);
5317 case RID_EXTENSION:
5318 c_parser_consume_token (parser);
5319 ext = disable_extension_diagnostics ();
5320 ret = c_parser_cast_expression (parser, NULL);
5321 restore_extension_diagnostics (ext);
5322 return ret;
5323 case RID_REALPART:
5324 c_parser_consume_token (parser);
5325 exp_loc = c_parser_peek_token (parser)->location;
5326 op = c_parser_cast_expression (parser, NULL);
5327 op = default_function_array_conversion (exp_loc, op);
5328 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5329 case RID_IMAGPART:
5330 c_parser_consume_token (parser);
5331 exp_loc = c_parser_peek_token (parser)->location;
5332 op = c_parser_cast_expression (parser, NULL);
5333 op = default_function_array_conversion (exp_loc, op);
5334 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5335 default:
5336 return c_parser_postfix_expression (parser);
5338 default:
5339 return c_parser_postfix_expression (parser);
5343 /* Parse a sizeof expression. */
5345 static struct c_expr
5346 c_parser_sizeof_expression (c_parser *parser)
5348 struct c_expr expr;
5349 location_t expr_loc;
5350 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5351 c_parser_consume_token (parser);
5352 c_inhibit_evaluation_warnings++;
5353 in_sizeof++;
5354 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5355 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5357 /* Either sizeof ( type-name ) or sizeof unary-expression
5358 starting with a compound literal. */
5359 struct c_type_name *type_name;
5360 c_parser_consume_token (parser);
5361 expr_loc = c_parser_peek_token (parser)->location;
5362 type_name = c_parser_type_name (parser);
5363 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5364 if (type_name == NULL)
5366 struct c_expr ret;
5367 c_inhibit_evaluation_warnings--;
5368 in_sizeof--;
5369 ret.value = error_mark_node;
5370 ret.original_code = ERROR_MARK;
5371 ret.original_type = NULL;
5372 return ret;
5374 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5376 expr = c_parser_postfix_expression_after_paren_type (parser,
5377 type_name,
5378 expr_loc);
5379 goto sizeof_expr;
5381 /* sizeof ( type-name ). */
5382 c_inhibit_evaluation_warnings--;
5383 in_sizeof--;
5384 return c_expr_sizeof_type (expr_loc, type_name);
5386 else
5388 expr_loc = c_parser_peek_token (parser)->location;
5389 expr = c_parser_unary_expression (parser);
5390 sizeof_expr:
5391 c_inhibit_evaluation_warnings--;
5392 in_sizeof--;
5393 mark_exp_read (expr.value);
5394 if (TREE_CODE (expr.value) == COMPONENT_REF
5395 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5396 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5397 return c_expr_sizeof_expr (expr_loc, expr);
5401 /* Parse an alignof expression. */
5403 static struct c_expr
5404 c_parser_alignof_expression (c_parser *parser)
5406 struct c_expr expr;
5407 location_t loc = c_parser_peek_token (parser)->location;
5408 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5409 c_parser_consume_token (parser);
5410 c_inhibit_evaluation_warnings++;
5411 in_alignof++;
5412 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5413 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5415 /* Either __alignof__ ( type-name ) or __alignof__
5416 unary-expression starting with a compound literal. */
5417 location_t loc;
5418 struct c_type_name *type_name;
5419 struct c_expr ret;
5420 c_parser_consume_token (parser);
5421 loc = c_parser_peek_token (parser)->location;
5422 type_name = c_parser_type_name (parser);
5423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5424 if (type_name == NULL)
5426 struct c_expr ret;
5427 c_inhibit_evaluation_warnings--;
5428 in_alignof--;
5429 ret.value = error_mark_node;
5430 ret.original_code = ERROR_MARK;
5431 ret.original_type = NULL;
5432 return ret;
5434 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5436 expr = c_parser_postfix_expression_after_paren_type (parser,
5437 type_name,
5438 loc);
5439 goto alignof_expr;
5441 /* alignof ( type-name ). */
5442 c_inhibit_evaluation_warnings--;
5443 in_alignof--;
5444 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5445 ret.original_code = ERROR_MARK;
5446 ret.original_type = NULL;
5447 return ret;
5449 else
5451 struct c_expr ret;
5452 expr = c_parser_unary_expression (parser);
5453 alignof_expr:
5454 mark_exp_read (expr.value);
5455 c_inhibit_evaluation_warnings--;
5456 in_alignof--;
5457 ret.value = c_alignof_expr (loc, expr.value);
5458 ret.original_code = ERROR_MARK;
5459 ret.original_type = NULL;
5460 return ret;
5464 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5466 postfix-expression:
5467 primary-expression
5468 postfix-expression [ expression ]
5469 postfix-expression ( argument-expression-list[opt] )
5470 postfix-expression . identifier
5471 postfix-expression -> identifier
5472 postfix-expression ++
5473 postfix-expression --
5474 ( type-name ) { initializer-list }
5475 ( type-name ) { initializer-list , }
5477 argument-expression-list:
5478 argument-expression
5479 argument-expression-list , argument-expression
5481 primary-expression:
5482 identifier
5483 constant
5484 string-literal
5485 ( expression )
5487 GNU extensions:
5489 primary-expression:
5490 __func__
5491 (treated as a keyword in GNU C)
5492 __FUNCTION__
5493 __PRETTY_FUNCTION__
5494 ( compound-statement )
5495 __builtin_va_arg ( assignment-expression , type-name )
5496 __builtin_offsetof ( type-name , offsetof-member-designator )
5497 __builtin_choose_expr ( assignment-expression ,
5498 assignment-expression ,
5499 assignment-expression )
5500 __builtin_types_compatible_p ( type-name , type-name )
5502 offsetof-member-designator:
5503 identifier
5504 offsetof-member-designator . identifier
5505 offsetof-member-designator [ expression ]
5507 Objective-C:
5509 primary-expression:
5510 [ objc-receiver objc-message-args ]
5511 @selector ( objc-selector-arg )
5512 @protocol ( identifier )
5513 @encode ( type-name )
5514 objc-string-literal
5517 static struct c_expr
5518 c_parser_postfix_expression (c_parser *parser)
5520 struct c_expr expr, e1, e2, e3;
5521 struct c_type_name *t1, *t2;
5522 location_t loc = c_parser_peek_token (parser)->location;;
5523 expr.original_code = ERROR_MARK;
5524 expr.original_type = NULL;
5525 switch (c_parser_peek_token (parser)->type)
5527 case CPP_NUMBER:
5528 expr.value = c_parser_peek_token (parser)->value;
5529 loc = c_parser_peek_token (parser)->location;
5530 c_parser_consume_token (parser);
5531 if (TREE_CODE (expr.value) == FIXED_CST
5532 && !targetm.fixed_point_supported_p ())
5534 error_at (loc, "fixed-point types not supported for this target");
5535 expr.value = error_mark_node;
5537 break;
5538 case CPP_CHAR:
5539 case CPP_CHAR16:
5540 case CPP_CHAR32:
5541 case CPP_WCHAR:
5542 expr.value = c_parser_peek_token (parser)->value;
5543 c_parser_consume_token (parser);
5544 break;
5545 case CPP_STRING:
5546 case CPP_STRING16:
5547 case CPP_STRING32:
5548 case CPP_WSTRING:
5549 case CPP_UTF8STRING:
5550 expr.value = c_parser_peek_token (parser)->value;
5551 expr.original_code = STRING_CST;
5552 c_parser_consume_token (parser);
5553 break;
5554 case CPP_OBJC_STRING:
5555 gcc_assert (c_dialect_objc ());
5556 expr.value
5557 = objc_build_string_object (c_parser_peek_token (parser)->value);
5558 c_parser_consume_token (parser);
5559 break;
5560 case CPP_NAME:
5561 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5563 c_parser_error (parser, "expected expression");
5564 expr.value = error_mark_node;
5565 break;
5568 tree id = c_parser_peek_token (parser)->value;
5569 c_parser_consume_token (parser);
5570 expr.value = build_external_ref (loc, id,
5571 (c_parser_peek_token (parser)->type
5572 == CPP_OPEN_PAREN),
5573 &expr.original_type);
5575 break;
5576 case CPP_OPEN_PAREN:
5577 /* A parenthesized expression, statement expression or compound
5578 literal. */
5579 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5581 /* A statement expression. */
5582 tree stmt;
5583 location_t brace_loc;
5584 c_parser_consume_token (parser);
5585 brace_loc = c_parser_peek_token (parser)->location;
5586 c_parser_consume_token (parser);
5587 if (cur_stmt_list == NULL)
5589 error_at (loc, "braced-group within expression allowed "
5590 "only inside a function");
5591 parser->error = true;
5592 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5594 expr.value = error_mark_node;
5595 break;
5597 stmt = c_begin_stmt_expr ();
5598 c_parser_compound_statement_nostart (parser);
5599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5600 "expected %<)%>");
5601 pedwarn (loc, OPT_pedantic,
5602 "ISO C forbids braced-groups within expressions");
5603 expr.value = c_finish_stmt_expr (brace_loc, stmt);
5604 mark_exp_read (expr.value);
5606 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5608 /* A compound literal. ??? Can we actually get here rather
5609 than going directly to
5610 c_parser_postfix_expression_after_paren_type from
5611 elsewhere? */
5612 location_t loc;
5613 struct c_type_name *type_name;
5614 c_parser_consume_token (parser);
5615 loc = c_parser_peek_token (parser)->location;
5616 type_name = c_parser_type_name (parser);
5617 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5618 "expected %<)%>");
5619 if (type_name == NULL)
5621 expr.value = error_mark_node;
5623 else
5624 expr = c_parser_postfix_expression_after_paren_type (parser,
5625 type_name,
5626 loc);
5628 else
5630 /* A parenthesized expression. */
5631 c_parser_consume_token (parser);
5632 expr = c_parser_expression (parser);
5633 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5634 TREE_NO_WARNING (expr.value) = 1;
5635 if (expr.original_code != C_MAYBE_CONST_EXPR)
5636 expr.original_code = ERROR_MARK;
5637 /* Don't change EXPR.ORIGINAL_TYPE. */
5638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5639 "expected %<)%>");
5641 break;
5642 case CPP_KEYWORD:
5643 switch (c_parser_peek_token (parser)->keyword)
5645 case RID_FUNCTION_NAME:
5646 case RID_PRETTY_FUNCTION_NAME:
5647 case RID_C99_FUNCTION_NAME:
5648 expr.value = fname_decl (loc,
5649 c_parser_peek_token (parser)->keyword,
5650 c_parser_peek_token (parser)->value);
5651 c_parser_consume_token (parser);
5652 break;
5653 case RID_VA_ARG:
5654 c_parser_consume_token (parser);
5655 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5657 expr.value = error_mark_node;
5658 break;
5660 e1 = c_parser_expr_no_commas (parser, NULL);
5661 mark_exp_read (e1.value);
5662 e1.value = c_fully_fold (e1.value, false, NULL);
5663 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5666 expr.value = error_mark_node;
5667 break;
5669 loc = c_parser_peek_token (parser)->location;
5670 t1 = c_parser_type_name (parser);
5671 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5672 "expected %<)%>");
5673 if (t1 == NULL)
5675 expr.value = error_mark_node;
5677 else
5679 tree type_expr = NULL_TREE;
5680 expr.value = c_build_va_arg (loc, e1.value,
5681 groktypename (t1, &type_expr, NULL));
5682 if (type_expr)
5684 expr.value = build2 (C_MAYBE_CONST_EXPR,
5685 TREE_TYPE (expr.value), type_expr,
5686 expr.value);
5687 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5690 break;
5691 case RID_OFFSETOF:
5692 c_parser_consume_token (parser);
5693 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5695 expr.value = error_mark_node;
5696 break;
5698 t1 = c_parser_type_name (parser);
5699 if (t1 == NULL)
5701 expr.value = error_mark_node;
5702 break;
5704 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5707 expr.value = error_mark_node;
5708 break;
5711 tree type = groktypename (t1, NULL, NULL);
5712 tree offsetof_ref;
5713 if (type == error_mark_node)
5714 offsetof_ref = error_mark_node;
5715 else
5717 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5718 SET_EXPR_LOCATION (offsetof_ref, loc);
5720 /* Parse the second argument to __builtin_offsetof. We
5721 must have one identifier, and beyond that we want to
5722 accept sub structure and sub array references. */
5723 if (c_parser_next_token_is (parser, CPP_NAME))
5725 offsetof_ref = build_component_ref
5726 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
5727 c_parser_consume_token (parser);
5728 while (c_parser_next_token_is (parser, CPP_DOT)
5729 || c_parser_next_token_is (parser,
5730 CPP_OPEN_SQUARE)
5731 || c_parser_next_token_is (parser,
5732 CPP_DEREF))
5734 if (c_parser_next_token_is (parser, CPP_DEREF))
5736 loc = c_parser_peek_token (parser)->location;
5737 offsetof_ref = build_array_ref (loc,
5738 offsetof_ref,
5739 integer_zero_node);
5740 goto do_dot;
5742 else if (c_parser_next_token_is (parser, CPP_DOT))
5744 do_dot:
5745 c_parser_consume_token (parser);
5746 if (c_parser_next_token_is_not (parser,
5747 CPP_NAME))
5749 c_parser_error (parser, "expected identifier");
5750 break;
5752 offsetof_ref = build_component_ref
5753 (loc, offsetof_ref,
5754 c_parser_peek_token (parser)->value);
5755 c_parser_consume_token (parser);
5757 else
5759 tree idx;
5760 loc = c_parser_peek_token (parser)->location;
5761 c_parser_consume_token (parser);
5762 idx = c_parser_expression (parser).value;
5763 idx = c_fully_fold (idx, false, NULL);
5764 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5765 "expected %<]%>");
5766 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
5770 else
5771 c_parser_error (parser, "expected identifier");
5772 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5773 "expected %<)%>");
5774 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5776 break;
5777 case RID_CHOOSE_EXPR:
5778 c_parser_consume_token (parser);
5779 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5781 expr.value = error_mark_node;
5782 break;
5784 loc = c_parser_peek_token (parser)->location;
5785 e1 = c_parser_expr_no_commas (parser, NULL);
5786 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5788 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5789 expr.value = error_mark_node;
5790 break;
5792 e2 = c_parser_expr_no_commas (parser, NULL);
5793 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5796 expr.value = error_mark_node;
5797 break;
5799 e3 = c_parser_expr_no_commas (parser, NULL);
5800 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5801 "expected %<)%>");
5803 tree c;
5805 c = e1.value;
5806 mark_exp_read (e2.value);
5807 mark_exp_read (e3.value);
5808 if (TREE_CODE (c) != INTEGER_CST
5809 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
5810 error_at (loc,
5811 "first argument to %<__builtin_choose_expr%> not"
5812 " a constant");
5813 constant_expression_warning (c);
5814 expr = integer_zerop (c) ? e3 : e2;
5816 break;
5817 case RID_TYPES_COMPATIBLE_P:
5818 c_parser_consume_token (parser);
5819 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5821 expr.value = error_mark_node;
5822 break;
5824 t1 = c_parser_type_name (parser);
5825 if (t1 == NULL)
5827 expr.value = error_mark_node;
5828 break;
5830 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5833 expr.value = error_mark_node;
5834 break;
5836 t2 = c_parser_type_name (parser);
5837 if (t2 == NULL)
5839 expr.value = error_mark_node;
5840 break;
5842 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5843 "expected %<)%>");
5845 tree e1, e2;
5847 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5848 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
5850 expr.value
5851 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
5853 break;
5854 case RID_AT_SELECTOR:
5855 gcc_assert (c_dialect_objc ());
5856 c_parser_consume_token (parser);
5857 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5859 expr.value = error_mark_node;
5860 break;
5863 tree sel = c_parser_objc_selector_arg (parser);
5864 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5865 "expected %<)%>");
5866 expr.value = objc_build_selector_expr (loc, sel);
5868 break;
5869 case RID_AT_PROTOCOL:
5870 gcc_assert (c_dialect_objc ());
5871 c_parser_consume_token (parser);
5872 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5874 expr.value = error_mark_node;
5875 break;
5877 if (c_parser_next_token_is_not (parser, CPP_NAME))
5879 c_parser_error (parser, "expected identifier");
5880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5881 expr.value = error_mark_node;
5882 break;
5885 tree id = c_parser_peek_token (parser)->value;
5886 c_parser_consume_token (parser);
5887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5888 "expected %<)%>");
5889 expr.value = objc_build_protocol_expr (id);
5891 break;
5892 case RID_AT_ENCODE:
5893 /* Extension to support C-structures in the archiver. */
5894 gcc_assert (c_dialect_objc ());
5895 c_parser_consume_token (parser);
5896 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5898 expr.value = error_mark_node;
5899 break;
5901 t1 = c_parser_type_name (parser);
5902 if (t1 == NULL)
5904 expr.value = error_mark_node;
5905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5906 break;
5908 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5909 "expected %<)%>");
5911 tree type = groktypename (t1, NULL, NULL);
5912 expr.value = objc_build_encode_expr (type);
5914 break;
5915 default:
5916 c_parser_error (parser, "expected expression");
5917 expr.value = error_mark_node;
5918 break;
5920 break;
5921 case CPP_OPEN_SQUARE:
5922 if (c_dialect_objc ())
5924 tree receiver, args;
5925 c_parser_consume_token (parser);
5926 receiver = c_parser_objc_receiver (parser);
5927 args = c_parser_objc_message_args (parser);
5928 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5929 "expected %<]%>");
5930 expr.value = objc_build_message_expr (build_tree_list (receiver,
5931 args));
5932 break;
5934 /* Else fall through to report error. */
5935 default:
5936 c_parser_error (parser, "expected expression");
5937 expr.value = error_mark_node;
5938 break;
5940 return c_parser_postfix_expression_after_primary (parser, loc, expr);
5943 /* Parse a postfix expression after a parenthesized type name: the
5944 brace-enclosed initializer of a compound literal, possibly followed
5945 by some postfix operators. This is separate because it is not
5946 possible to tell until after the type name whether a cast
5947 expression has a cast or a compound literal, or whether the operand
5948 of sizeof is a parenthesized type name or starts with a compound
5949 literal. TYPE_LOC is the location where TYPE_NAME starts--the
5950 location of the first token after the parentheses around the type
5951 name. */
5953 static struct c_expr
5954 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5955 struct c_type_name *type_name,
5956 location_t type_loc)
5958 tree type;
5959 struct c_expr init;
5960 bool non_const;
5961 struct c_expr expr;
5962 location_t start_loc;
5963 tree type_expr = NULL_TREE;
5964 bool type_expr_const = true;
5965 check_compound_literal_type (type_loc, type_name);
5966 start_init (NULL_TREE, NULL, 0);
5967 type = groktypename (type_name, &type_expr, &type_expr_const);
5968 start_loc = c_parser_peek_token (parser)->location;
5969 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5971 error_at (type_loc, "compound literal has variable size");
5972 type = error_mark_node;
5974 init = c_parser_braced_init (parser, type, false);
5975 finish_init ();
5976 maybe_warn_string_init (type, init);
5978 if (type != error_mark_node
5979 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
5980 && current_function_decl)
5982 error ("compound literal qualified by address-space qualifier");
5983 type = error_mark_node;
5986 if (!flag_isoc99)
5987 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
5988 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5989 ? CONSTRUCTOR_NON_CONST (init.value)
5990 : init.original_code == C_MAYBE_CONST_EXPR);
5991 non_const |= !type_expr_const;
5992 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
5993 expr.original_code = ERROR_MARK;
5994 expr.original_type = NULL;
5995 if (type_expr)
5997 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5999 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6000 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6002 else
6004 gcc_assert (!non_const);
6005 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6006 type_expr, expr.value);
6009 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6012 /* Parse a postfix expression after the initial primary or compound
6013 literal; that is, parse a series of postfix operators.
6015 EXPR_LOC is the location of the primary expression. */
6017 static struct c_expr
6018 c_parser_postfix_expression_after_primary (c_parser *parser,
6019 location_t expr_loc,
6020 struct c_expr expr)
6022 struct c_expr orig_expr;
6023 tree ident, idx;
6024 VEC(tree,gc) *exprlist;
6025 VEC(tree,gc) *origtypes;
6026 while (true)
6028 location_t op_loc = c_parser_peek_token (parser)->location;
6029 switch (c_parser_peek_token (parser)->type)
6031 case CPP_OPEN_SQUARE:
6032 /* Array reference. */
6033 c_parser_consume_token (parser);
6034 idx = c_parser_expression (parser).value;
6035 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6036 "expected %<]%>");
6037 expr.value = build_array_ref (op_loc, expr.value, idx);
6038 expr.original_code = ERROR_MARK;
6039 expr.original_type = NULL;
6040 break;
6041 case CPP_OPEN_PAREN:
6042 /* Function call. */
6043 c_parser_consume_token (parser);
6044 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6045 exprlist = NULL;
6046 else
6047 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6048 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6049 "expected %<)%>");
6050 orig_expr = expr;
6051 mark_exp_read (expr.value);
6052 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6053 "(" after the FUNCNAME, which is what we have now. */
6054 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6055 origtypes);
6056 expr.original_code = ERROR_MARK;
6057 if (TREE_CODE (expr.value) == INTEGER_CST
6058 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6059 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6060 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6061 expr.original_code = C_MAYBE_CONST_EXPR;
6062 expr.original_type = NULL;
6063 if (exprlist != NULL)
6065 release_tree_vector (exprlist);
6066 release_tree_vector (origtypes);
6068 break;
6069 case CPP_DOT:
6070 /* Structure element reference. */
6071 c_parser_consume_token (parser);
6072 expr = default_function_array_conversion (expr_loc, expr);
6073 if (c_parser_next_token_is (parser, CPP_NAME))
6074 ident = c_parser_peek_token (parser)->value;
6075 else
6077 c_parser_error (parser, "expected identifier");
6078 expr.value = error_mark_node;
6079 expr.original_code = ERROR_MARK;
6080 expr.original_type = NULL;
6081 return expr;
6083 c_parser_consume_token (parser);
6084 expr.value = build_component_ref (op_loc, expr.value, ident);
6085 expr.original_code = ERROR_MARK;
6086 if (TREE_CODE (expr.value) != COMPONENT_REF)
6087 expr.original_type = NULL;
6088 else
6090 /* Remember the original type of a bitfield. */
6091 tree field = TREE_OPERAND (expr.value, 1);
6092 if (TREE_CODE (field) != FIELD_DECL)
6093 expr.original_type = NULL;
6094 else
6095 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6097 break;
6098 case CPP_DEREF:
6099 /* Structure element reference. */
6100 c_parser_consume_token (parser);
6101 expr = default_function_array_conversion (expr_loc, expr);
6102 if (c_parser_next_token_is (parser, CPP_NAME))
6103 ident = c_parser_peek_token (parser)->value;
6104 else
6106 c_parser_error (parser, "expected identifier");
6107 expr.value = error_mark_node;
6108 expr.original_code = ERROR_MARK;
6109 expr.original_type = NULL;
6110 return expr;
6112 c_parser_consume_token (parser);
6113 expr.value = build_component_ref (op_loc,
6114 build_indirect_ref (op_loc,
6115 expr.value,
6116 RO_ARROW),
6117 ident);
6118 expr.original_code = ERROR_MARK;
6119 if (TREE_CODE (expr.value) != COMPONENT_REF)
6120 expr.original_type = NULL;
6121 else
6123 /* Remember the original type of a bitfield. */
6124 tree field = TREE_OPERAND (expr.value, 1);
6125 if (TREE_CODE (field) != FIELD_DECL)
6126 expr.original_type = NULL;
6127 else
6128 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6130 break;
6131 case CPP_PLUS_PLUS:
6132 /* Postincrement. */
6133 c_parser_consume_token (parser);
6134 expr = default_function_array_read_conversion (expr_loc, expr);
6135 expr.value = build_unary_op (op_loc,
6136 POSTINCREMENT_EXPR, expr.value, 0);
6137 expr.original_code = ERROR_MARK;
6138 expr.original_type = NULL;
6139 break;
6140 case CPP_MINUS_MINUS:
6141 /* Postdecrement. */
6142 c_parser_consume_token (parser);
6143 expr = default_function_array_read_conversion (expr_loc, expr);
6144 expr.value = build_unary_op (op_loc,
6145 POSTDECREMENT_EXPR, expr.value, 0);
6146 expr.original_code = ERROR_MARK;
6147 expr.original_type = NULL;
6148 break;
6149 default:
6150 return expr;
6155 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6157 expression:
6158 assignment-expression
6159 expression , assignment-expression
6162 static struct c_expr
6163 c_parser_expression (c_parser *parser)
6165 struct c_expr expr;
6166 expr = c_parser_expr_no_commas (parser, NULL);
6167 while (c_parser_next_token_is (parser, CPP_COMMA))
6169 struct c_expr next;
6170 tree lhsval;
6171 location_t loc = c_parser_peek_token (parser)->location;
6172 location_t expr_loc;
6173 c_parser_consume_token (parser);
6174 expr_loc = c_parser_peek_token (parser)->location;
6175 lhsval = expr.value;
6176 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6177 lhsval = TREE_OPERAND (lhsval, 1);
6178 if (DECL_P (lhsval) || handled_component_p (lhsval))
6179 mark_exp_read (lhsval);
6180 next = c_parser_expr_no_commas (parser, NULL);
6181 next = default_function_array_conversion (expr_loc, next);
6182 expr.value = build_compound_expr (loc, expr.value, next.value);
6183 expr.original_code = COMPOUND_EXPR;
6184 expr.original_type = next.original_type;
6186 return expr;
6189 /* Parse an expression and convert functions or arrays to
6190 pointers. */
6192 static struct c_expr
6193 c_parser_expression_conv (c_parser *parser)
6195 struct c_expr expr;
6196 location_t loc = c_parser_peek_token (parser)->location;
6197 expr = c_parser_expression (parser);
6198 expr = default_function_array_conversion (loc, expr);
6199 return expr;
6202 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6203 functions and arrays to pointers. If FOLD_P, fold the expressions.
6205 nonempty-expr-list:
6206 assignment-expression
6207 nonempty-expr-list , assignment-expression
6210 static VEC(tree,gc) *
6211 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6212 VEC(tree,gc) **p_orig_types)
6214 VEC(tree,gc) *ret;
6215 VEC(tree,gc) *orig_types;
6216 struct c_expr expr;
6217 location_t loc = c_parser_peek_token (parser)->location;
6219 ret = make_tree_vector ();
6220 if (p_orig_types == NULL)
6221 orig_types = NULL;
6222 else
6223 orig_types = make_tree_vector ();
6225 expr = c_parser_expr_no_commas (parser, NULL);
6226 if (convert_p)
6227 expr = default_function_array_read_conversion (loc, expr);
6228 if (fold_p)
6229 expr.value = c_fully_fold (expr.value, false, NULL);
6230 VEC_quick_push (tree, ret, expr.value);
6231 if (orig_types != NULL)
6232 VEC_quick_push (tree, orig_types, expr.original_type);
6233 while (c_parser_next_token_is (parser, CPP_COMMA))
6235 c_parser_consume_token (parser);
6236 loc = c_parser_peek_token (parser)->location;
6237 expr = c_parser_expr_no_commas (parser, NULL);
6238 if (convert_p)
6239 expr = default_function_array_read_conversion (loc, expr);
6240 if (fold_p)
6241 expr.value = c_fully_fold (expr.value, false, NULL);
6242 VEC_safe_push (tree, gc, ret, expr.value);
6243 if (orig_types != NULL)
6244 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6246 if (orig_types != NULL)
6247 *p_orig_types = orig_types;
6248 return ret;
6251 /* Parse Objective-C-specific constructs. */
6253 /* Parse an objc-class-definition.
6255 objc-class-definition:
6256 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6257 objc-class-instance-variables[opt] objc-methodprotolist @end
6258 @implementation identifier objc-superclass[opt]
6259 objc-class-instance-variables[opt]
6260 @interface identifier ( identifier ) objc-protocol-refs[opt]
6261 objc-methodprotolist @end
6262 @implementation identifier ( identifier )
6264 objc-superclass:
6265 : identifier
6267 "@interface identifier (" must start "@interface identifier (
6268 identifier ) ...": objc-methodprotolist in the first production may
6269 not start with a parenthesized identifier as a declarator of a data
6270 definition with no declaration specifiers if the objc-superclass,
6271 objc-protocol-refs and objc-class-instance-variables are omitted. */
6273 static void
6274 c_parser_objc_class_definition (c_parser *parser)
6276 bool iface_p;
6277 tree id1;
6278 tree superclass;
6279 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6280 iface_p = true;
6281 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6282 iface_p = false;
6283 else
6284 gcc_unreachable ();
6285 c_parser_consume_token (parser);
6286 if (c_parser_next_token_is_not (parser, CPP_NAME))
6288 c_parser_error (parser, "expected identifier");
6289 return;
6291 id1 = c_parser_peek_token (parser)->value;
6292 c_parser_consume_token (parser);
6293 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6295 tree id2;
6296 tree proto = NULL_TREE;
6297 c_parser_consume_token (parser);
6298 if (c_parser_next_token_is_not (parser, CPP_NAME))
6300 c_parser_error (parser, "expected identifier");
6301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6302 return;
6304 id2 = c_parser_peek_token (parser)->value;
6305 c_parser_consume_token (parser);
6306 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6307 if (!iface_p)
6309 objc_start_category_implementation (id1, id2);
6310 return;
6312 if (c_parser_next_token_is (parser, CPP_LESS))
6313 proto = c_parser_objc_protocol_refs (parser);
6314 objc_start_category_interface (id1, id2, proto);
6315 c_parser_objc_methodprotolist (parser);
6316 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6317 objc_finish_interface ();
6318 return;
6320 if (c_parser_next_token_is (parser, CPP_COLON))
6322 c_parser_consume_token (parser);
6323 if (c_parser_next_token_is_not (parser, CPP_NAME))
6325 c_parser_error (parser, "expected identifier");
6326 return;
6328 superclass = c_parser_peek_token (parser)->value;
6329 c_parser_consume_token (parser);
6331 else
6332 superclass = NULL_TREE;
6333 if (iface_p)
6335 tree proto = NULL_TREE;
6336 if (c_parser_next_token_is (parser, CPP_LESS))
6337 proto = c_parser_objc_protocol_refs (parser);
6338 objc_start_class_interface (id1, superclass, proto);
6340 else
6341 objc_start_class_implementation (id1, superclass);
6342 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6343 c_parser_objc_class_instance_variables (parser);
6344 if (iface_p)
6346 objc_continue_interface ();
6347 c_parser_objc_methodprotolist (parser);
6348 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6349 objc_finish_interface ();
6351 else
6353 objc_continue_implementation ();
6354 return;
6358 /* Parse objc-class-instance-variables.
6360 objc-class-instance-variables:
6361 { objc-instance-variable-decl-list[opt] }
6363 objc-instance-variable-decl-list:
6364 objc-visibility-spec
6365 objc-instance-variable-decl ;
6367 objc-instance-variable-decl-list objc-visibility-spec
6368 objc-instance-variable-decl-list objc-instance-variable-decl ;
6369 objc-instance-variable-decl-list ;
6371 objc-visibility-spec:
6372 @private
6373 @protected
6374 @public
6376 objc-instance-variable-decl:
6377 struct-declaration
6380 static void
6381 c_parser_objc_class_instance_variables (c_parser *parser)
6383 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6384 c_parser_consume_token (parser);
6385 while (c_parser_next_token_is_not (parser, CPP_EOF))
6387 tree decls;
6388 /* Parse any stray semicolon. */
6389 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6391 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6392 "extra semicolon in struct or union specified");
6393 c_parser_consume_token (parser);
6394 continue;
6396 /* Stop if at the end of the instance variables. */
6397 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6399 c_parser_consume_token (parser);
6400 break;
6402 /* Parse any objc-visibility-spec. */
6403 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
6405 c_parser_consume_token (parser);
6406 objc_set_visibility (2);
6407 continue;
6409 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
6411 c_parser_consume_token (parser);
6412 objc_set_visibility (0);
6413 continue;
6415 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
6417 c_parser_consume_token (parser);
6418 objc_set_visibility (1);
6419 continue;
6421 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6423 c_parser_pragma (parser, pragma_external);
6424 continue;
6427 /* Parse some comma-separated declarations. */
6428 decls = c_parser_struct_declaration (parser);
6430 /* Comma-separated instance variables are chained together in
6431 reverse order; add them one by one. */
6432 tree ivar = nreverse (decls);
6433 for (; ivar; ivar = DECL_CHAIN (ivar))
6434 objc_add_instance_variable (copy_node (ivar));
6436 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6440 /* Parse an objc-class-declaration.
6442 objc-class-declaration:
6443 @class identifier-list ;
6446 static void
6447 c_parser_objc_class_declaration (c_parser *parser)
6449 tree list = NULL_TREE;
6450 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
6451 c_parser_consume_token (parser);
6452 /* Any identifiers, including those declared as type names, are OK
6453 here. */
6454 while (true)
6456 tree id;
6457 if (c_parser_next_token_is_not (parser, CPP_NAME))
6459 c_parser_error (parser, "expected identifier");
6460 break;
6462 id = c_parser_peek_token (parser)->value;
6463 list = chainon (list, build_tree_list (NULL_TREE, id));
6464 c_parser_consume_token (parser);
6465 if (c_parser_next_token_is (parser, CPP_COMMA))
6466 c_parser_consume_token (parser);
6467 else
6468 break;
6470 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6471 objc_declare_class (list);
6474 /* Parse an objc-alias-declaration.
6476 objc-alias-declaration:
6477 @compatibility_alias identifier identifier ;
6480 static void
6481 c_parser_objc_alias_declaration (c_parser *parser)
6483 tree id1, id2;
6484 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6485 c_parser_consume_token (parser);
6486 if (c_parser_next_token_is_not (parser, CPP_NAME))
6488 c_parser_error (parser, "expected identifier");
6489 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6490 return;
6492 id1 = c_parser_peek_token (parser)->value;
6493 c_parser_consume_token (parser);
6494 if (c_parser_next_token_is_not (parser, CPP_NAME))
6496 c_parser_error (parser, "expected identifier");
6497 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6498 return;
6500 id2 = c_parser_peek_token (parser)->value;
6501 c_parser_consume_token (parser);
6502 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6503 objc_declare_alias (id1, id2);
6506 /* Parse an objc-protocol-definition.
6508 objc-protocol-definition:
6509 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6510 @protocol identifier-list ;
6512 "@protocol identifier ;" should be resolved as "@protocol
6513 identifier-list ;": objc-methodprotolist may not start with a
6514 semicolon in the first alternative if objc-protocol-refs are
6515 omitted. */
6517 static void
6518 c_parser_objc_protocol_definition (c_parser *parser)
6520 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6521 c_parser_consume_token (parser);
6522 if (c_parser_next_token_is_not (parser, CPP_NAME))
6524 c_parser_error (parser, "expected identifier");
6525 return;
6527 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6528 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6530 tree list = NULL_TREE;
6531 /* Any identifiers, including those declared as type names, are
6532 OK here. */
6533 while (true)
6535 tree id;
6536 if (c_parser_next_token_is_not (parser, CPP_NAME))
6538 c_parser_error (parser, "expected identifier");
6539 break;
6541 id = c_parser_peek_token (parser)->value;
6542 list = chainon (list, build_tree_list (NULL_TREE, id));
6543 c_parser_consume_token (parser);
6544 if (c_parser_next_token_is (parser, CPP_COMMA))
6545 c_parser_consume_token (parser);
6546 else
6547 break;
6549 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6550 objc_declare_protocols (list);
6552 else
6554 tree id = c_parser_peek_token (parser)->value;
6555 tree proto = NULL_TREE;
6556 c_parser_consume_token (parser);
6557 if (c_parser_next_token_is (parser, CPP_LESS))
6558 proto = c_parser_objc_protocol_refs (parser);
6559 parser->objc_pq_context = true;
6560 objc_start_protocol (id, proto);
6561 c_parser_objc_methodprotolist (parser);
6562 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6563 parser->objc_pq_context = false;
6564 objc_finish_interface ();
6568 /* Parse an objc-method-type.
6570 objc-method-type:
6575 static enum tree_code
6576 c_parser_objc_method_type (c_parser *parser)
6578 switch (c_parser_peek_token (parser)->type)
6580 case CPP_PLUS:
6581 c_parser_consume_token (parser);
6582 return PLUS_EXPR;
6583 case CPP_MINUS:
6584 c_parser_consume_token (parser);
6585 return MINUS_EXPR;
6586 default:
6587 gcc_unreachable ();
6591 /* Parse an objc-method-definition.
6593 objc-method-definition:
6594 objc-method-type objc-method-decl ;[opt] compound-statement
6597 static void
6598 c_parser_objc_method_definition (c_parser *parser)
6600 enum tree_code type = c_parser_objc_method_type (parser);
6601 tree decl;
6602 objc_set_method_type (type);
6603 parser->objc_pq_context = true;
6604 decl = c_parser_objc_method_decl (parser);
6605 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6607 c_parser_consume_token (parser);
6608 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6609 "extra semicolon in method definition specified");
6611 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6613 c_parser_error (parser, "expected %<{%>");
6614 return;
6616 parser->objc_pq_context = false;
6617 objc_start_method_definition (decl);
6618 add_stmt (c_parser_compound_statement (parser));
6619 objc_finish_method_definition (current_function_decl);
6622 /* Parse an objc-methodprotolist.
6624 objc-methodprotolist:
6625 empty
6626 objc-methodprotolist objc-methodproto
6627 objc-methodprotolist declaration
6628 objc-methodprotolist ;
6630 The declaration is a data definition, which may be missing
6631 declaration specifiers under the same rules and diagnostics as
6632 other data definitions outside functions, and the stray semicolon
6633 is diagnosed the same way as a stray semicolon outside a
6634 function. */
6636 static void
6637 c_parser_objc_methodprotolist (c_parser *parser)
6639 while (true)
6641 /* The list is terminated by @end. */
6642 switch (c_parser_peek_token (parser)->type)
6644 case CPP_SEMICOLON:
6645 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6646 "ISO C does not allow extra %<;%> outside of a function");
6647 c_parser_consume_token (parser);
6648 break;
6649 case CPP_PLUS:
6650 case CPP_MINUS:
6651 c_parser_objc_methodproto (parser);
6652 break;
6653 case CPP_PRAGMA:
6654 c_parser_pragma (parser, pragma_external);
6655 break;
6656 case CPP_EOF:
6657 return;
6658 default:
6659 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6660 return;
6661 c_parser_declaration_or_fndef (parser, false, false, true,
6662 false, true);
6663 break;
6668 /* Parse an objc-methodproto.
6670 objc-methodproto:
6671 objc-method-type objc-method-decl ;
6674 static void
6675 c_parser_objc_methodproto (c_parser *parser)
6677 enum tree_code type = c_parser_objc_method_type (parser);
6678 tree decl;
6679 objc_set_method_type (type);
6680 /* Remember protocol qualifiers in prototypes. */
6681 parser->objc_pq_context = true;
6682 decl = c_parser_objc_method_decl (parser);
6683 /* Forget protocol qualifiers here. */
6684 parser->objc_pq_context = false;
6685 objc_add_method_declaration (decl);
6686 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6689 /* Parse an objc-method-decl.
6691 objc-method-decl:
6692 ( objc-type-name ) objc-selector
6693 objc-selector
6694 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6695 objc-keyword-selector objc-optparmlist
6697 objc-keyword-selector:
6698 objc-keyword-decl
6699 objc-keyword-selector objc-keyword-decl
6701 objc-keyword-decl:
6702 objc-selector : ( objc-type-name ) identifier
6703 objc-selector : identifier
6704 : ( objc-type-name ) identifier
6705 : identifier
6707 objc-optparmlist:
6708 objc-optparms objc-optellipsis
6710 objc-optparms:
6711 empty
6712 objc-opt-parms , parameter-declaration
6714 objc-optellipsis:
6715 empty
6716 , ...
6719 static tree
6720 c_parser_objc_method_decl (c_parser *parser)
6722 tree type = NULL_TREE;
6723 tree sel;
6724 tree parms = NULL_TREE;
6725 bool ellipsis = false;
6727 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6729 c_parser_consume_token (parser);
6730 type = c_parser_objc_type_name (parser);
6731 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6733 sel = c_parser_objc_selector (parser);
6734 /* If there is no selector, or a colon follows, we have an
6735 objc-keyword-selector. If there is a selector, and a colon does
6736 not follow, that selector ends the objc-method-decl. */
6737 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6739 tree tsel = sel;
6740 tree list = NULL_TREE;
6741 while (true)
6743 tree atype = NULL_TREE, id, keyworddecl;
6744 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6745 break;
6746 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6748 c_parser_consume_token (parser);
6749 atype = c_parser_objc_type_name (parser);
6750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6751 "expected %<)%>");
6753 if (c_parser_next_token_is_not (parser, CPP_NAME))
6755 c_parser_error (parser, "expected identifier");
6756 return error_mark_node;
6758 id = c_parser_peek_token (parser)->value;
6759 c_parser_consume_token (parser);
6760 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6761 list = chainon (list, keyworddecl);
6762 tsel = c_parser_objc_selector (parser);
6763 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6764 break;
6766 /* Parse the optional parameter list. Optional Objective-C
6767 method parameters follow the C syntax, and may include '...'
6768 to denote a variable number of arguments. */
6769 parms = make_node (TREE_LIST);
6770 while (c_parser_next_token_is (parser, CPP_COMMA))
6772 struct c_parm *parm;
6773 c_parser_consume_token (parser);
6774 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6776 ellipsis = true;
6777 c_parser_consume_token (parser);
6778 break;
6780 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6781 if (parm == NULL)
6782 break;
6783 parms = chainon (parms,
6784 build_tree_list (NULL_TREE, grokparm (parm)));
6786 sel = list;
6788 return objc_build_method_signature (type, sel, parms, ellipsis);
6791 /* Parse an objc-type-name.
6793 objc-type-name:
6794 objc-type-qualifiers[opt] type-name
6795 objc-type-qualifiers[opt]
6797 objc-type-qualifiers:
6798 objc-type-qualifier
6799 objc-type-qualifiers objc-type-qualifier
6801 objc-type-qualifier: one of
6802 in out inout bycopy byref oneway
6805 static tree
6806 c_parser_objc_type_name (c_parser *parser)
6808 tree quals = NULL_TREE;
6809 struct c_type_name *type_name = NULL;
6810 tree type = NULL_TREE;
6811 while (true)
6813 c_token *token = c_parser_peek_token (parser);
6814 if (token->type == CPP_KEYWORD
6815 && (token->keyword == RID_IN
6816 || token->keyword == RID_OUT
6817 || token->keyword == RID_INOUT
6818 || token->keyword == RID_BYCOPY
6819 || token->keyword == RID_BYREF
6820 || token->keyword == RID_ONEWAY))
6822 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6823 c_parser_consume_token (parser);
6825 else
6826 break;
6828 if (c_parser_next_token_starts_typename (parser))
6829 type_name = c_parser_type_name (parser);
6830 if (type_name)
6831 type = groktypename (type_name, NULL, NULL);
6832 return build_tree_list (quals, type);
6835 /* Parse objc-protocol-refs.
6837 objc-protocol-refs:
6838 < identifier-list >
6841 static tree
6842 c_parser_objc_protocol_refs (c_parser *parser)
6844 tree list = NULL_TREE;
6845 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6846 c_parser_consume_token (parser);
6847 /* Any identifiers, including those declared as type names, are OK
6848 here. */
6849 while (true)
6851 tree id;
6852 if (c_parser_next_token_is_not (parser, CPP_NAME))
6854 c_parser_error (parser, "expected identifier");
6855 break;
6857 id = c_parser_peek_token (parser)->value;
6858 list = chainon (list, build_tree_list (NULL_TREE, id));
6859 c_parser_consume_token (parser);
6860 if (c_parser_next_token_is (parser, CPP_COMMA))
6861 c_parser_consume_token (parser);
6862 else
6863 break;
6865 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6866 return list;
6869 /* Parse an objc-try-catch-statement.
6871 objc-try-catch-statement:
6872 @try compound-statement objc-catch-list[opt]
6873 @try compound-statement objc-catch-list[opt] @finally compound-statement
6875 objc-catch-list:
6876 @catch ( parameter-declaration ) compound-statement
6877 objc-catch-list @catch ( parameter-declaration ) compound-statement
6880 static void
6881 c_parser_objc_try_catch_statement (c_parser *parser)
6883 location_t loc;
6884 tree stmt;
6885 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6886 c_parser_consume_token (parser);
6887 loc = c_parser_peek_token (parser)->location;
6888 stmt = c_parser_compound_statement (parser);
6889 objc_begin_try_stmt (loc, stmt);
6890 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6892 struct c_parm *parm;
6893 c_parser_consume_token (parser);
6894 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6895 break;
6896 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6897 if (parm == NULL)
6899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6900 break;
6902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6903 objc_begin_catch_clause (grokparm (parm));
6904 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6905 c_parser_compound_statement_nostart (parser);
6906 objc_finish_catch_clause ();
6908 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6910 location_t finloc;
6911 tree finstmt;
6912 c_parser_consume_token (parser);
6913 finloc = c_parser_peek_token (parser)->location;
6914 finstmt = c_parser_compound_statement (parser);
6915 objc_build_finally_clause (finloc, finstmt);
6917 objc_finish_try_stmt ();
6920 /* Parse an objc-synchronized-statement.
6922 objc-synchronized-statement:
6923 @synchronized ( expression ) compound-statement
6926 static void
6927 c_parser_objc_synchronized_statement (c_parser *parser)
6929 location_t loc;
6930 tree expr, stmt;
6931 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6932 c_parser_consume_token (parser);
6933 loc = c_parser_peek_token (parser)->location;
6934 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6936 expr = c_parser_expression (parser).value;
6937 expr = c_fully_fold (expr, false, NULL);
6938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6940 else
6941 expr = error_mark_node;
6942 stmt = c_parser_compound_statement (parser);
6943 objc_build_synchronized (loc, expr, stmt);
6946 /* Parse an objc-selector; return NULL_TREE without an error if the
6947 next token is not an objc-selector.
6949 objc-selector:
6950 identifier
6951 one of
6952 enum struct union if else while do for switch case default
6953 break continue return goto asm sizeof typeof __alignof
6954 unsigned long const short volatile signed restrict _Complex
6955 in out inout bycopy byref oneway int char float double void _Bool
6957 ??? Why this selection of keywords but not, for example, storage
6958 class specifiers? */
6960 static tree
6961 c_parser_objc_selector (c_parser *parser)
6963 c_token *token = c_parser_peek_token (parser);
6964 tree value = token->value;
6965 if (token->type == CPP_NAME)
6967 c_parser_consume_token (parser);
6968 return value;
6970 if (token->type != CPP_KEYWORD)
6971 return NULL_TREE;
6972 switch (token->keyword)
6974 case RID_ENUM:
6975 case RID_STRUCT:
6976 case RID_UNION:
6977 case RID_IF:
6978 case RID_ELSE:
6979 case RID_WHILE:
6980 case RID_DO:
6981 case RID_FOR:
6982 case RID_SWITCH:
6983 case RID_CASE:
6984 case RID_DEFAULT:
6985 case RID_BREAK:
6986 case RID_CONTINUE:
6987 case RID_RETURN:
6988 case RID_GOTO:
6989 case RID_ASM:
6990 case RID_SIZEOF:
6991 case RID_TYPEOF:
6992 case RID_ALIGNOF:
6993 case RID_UNSIGNED:
6994 case RID_LONG:
6995 case RID_INT128:
6996 case RID_CONST:
6997 case RID_SHORT:
6998 case RID_VOLATILE:
6999 case RID_SIGNED:
7000 case RID_RESTRICT:
7001 case RID_COMPLEX:
7002 case RID_IN:
7003 case RID_OUT:
7004 case RID_INOUT:
7005 case RID_BYCOPY:
7006 case RID_BYREF:
7007 case RID_ONEWAY:
7008 case RID_INT:
7009 case RID_CHAR:
7010 case RID_FLOAT:
7011 case RID_DOUBLE:
7012 case RID_VOID:
7013 case RID_BOOL:
7014 c_parser_consume_token (parser);
7015 return value;
7016 default:
7017 return NULL_TREE;
7021 /* Parse an objc-selector-arg.
7023 objc-selector-arg:
7024 objc-selector
7025 objc-keywordname-list
7027 objc-keywordname-list:
7028 objc-keywordname
7029 objc-keywordname-list objc-keywordname
7031 objc-keywordname:
7032 objc-selector :
7036 static tree
7037 c_parser_objc_selector_arg (c_parser *parser)
7039 tree sel = c_parser_objc_selector (parser);
7040 tree list = NULL_TREE;
7041 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7042 return sel;
7043 while (true)
7045 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7046 return list;
7047 list = chainon (list, build_tree_list (sel, NULL_TREE));
7048 sel = c_parser_objc_selector (parser);
7049 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7050 break;
7052 return list;
7055 /* Parse an objc-receiver.
7057 objc-receiver:
7058 expression
7059 class-name
7060 type-name
7063 static tree
7064 c_parser_objc_receiver (c_parser *parser)
7066 if (c_parser_peek_token (parser)->type == CPP_NAME
7067 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7068 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7070 tree id = c_parser_peek_token (parser)->value;
7071 c_parser_consume_token (parser);
7072 return objc_get_class_reference (id);
7074 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7077 /* Parse objc-message-args.
7079 objc-message-args:
7080 objc-selector
7081 objc-keywordarg-list
7083 objc-keywordarg-list:
7084 objc-keywordarg
7085 objc-keywordarg-list objc-keywordarg
7087 objc-keywordarg:
7088 objc-selector : objc-keywordexpr
7089 : objc-keywordexpr
7092 static tree
7093 c_parser_objc_message_args (c_parser *parser)
7095 tree sel = c_parser_objc_selector (parser);
7096 tree list = NULL_TREE;
7097 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7098 return sel;
7099 while (true)
7101 tree keywordexpr;
7102 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7103 return error_mark_node;
7104 keywordexpr = c_parser_objc_keywordexpr (parser);
7105 list = chainon (list, build_tree_list (sel, keywordexpr));
7106 sel = c_parser_objc_selector (parser);
7107 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7108 break;
7110 return list;
7113 /* Parse an objc-keywordexpr.
7115 objc-keywordexpr:
7116 nonempty-expr-list
7119 static tree
7120 c_parser_objc_keywordexpr (c_parser *parser)
7122 tree ret;
7123 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7124 if (VEC_length (tree, expr_list) == 1)
7126 /* Just return the expression, remove a level of
7127 indirection. */
7128 ret = VEC_index (tree, expr_list, 0);
7130 else
7132 /* We have a comma expression, we will collapse later. */
7133 ret = build_tree_list_vec (expr_list);
7135 release_tree_vector (expr_list);
7136 return ret;
7140 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
7141 should be considered, statements. ALLOW_STMT is true if we're within
7142 the context of a function and such pragmas are to be allowed. Returns
7143 true if we actually parsed such a pragma. */
7145 static bool
7146 c_parser_pragma (c_parser *parser, enum pragma_context context)
7148 unsigned int id;
7150 id = c_parser_peek_token (parser)->pragma_kind;
7151 gcc_assert (id != PRAGMA_NONE);
7153 switch (id)
7155 case PRAGMA_OMP_BARRIER:
7156 if (context != pragma_compound)
7158 if (context == pragma_stmt)
7159 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7160 "used in compound statements");
7161 goto bad_stmt;
7163 c_parser_omp_barrier (parser);
7164 return false;
7166 case PRAGMA_OMP_FLUSH:
7167 if (context != pragma_compound)
7169 if (context == pragma_stmt)
7170 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7171 "used in compound statements");
7172 goto bad_stmt;
7174 c_parser_omp_flush (parser);
7175 return false;
7177 case PRAGMA_OMP_TASKWAIT:
7178 if (context != pragma_compound)
7180 if (context == pragma_stmt)
7181 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7182 "used in compound statements");
7183 goto bad_stmt;
7185 c_parser_omp_taskwait (parser);
7186 return false;
7188 case PRAGMA_OMP_THREADPRIVATE:
7189 c_parser_omp_threadprivate (parser);
7190 return false;
7192 case PRAGMA_OMP_SECTION:
7193 error_at (c_parser_peek_token (parser)->location,
7194 "%<#pragma omp section%> may only be used in "
7195 "%<#pragma omp sections%> construct");
7196 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7197 return false;
7199 case PRAGMA_GCC_PCH_PREPROCESS:
7200 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7201 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7202 return false;
7204 default:
7205 if (id < PRAGMA_FIRST_EXTERNAL)
7207 if (context == pragma_external)
7209 bad_stmt:
7210 c_parser_error (parser, "expected declaration specifiers");
7211 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7212 return false;
7214 c_parser_omp_construct (parser);
7215 return true;
7217 break;
7220 c_parser_consume_pragma (parser);
7221 c_invoke_pragma_handler (id);
7223 /* Skip to EOL, but suppress any error message. Those will have been
7224 generated by the handler routine through calling error, as opposed
7225 to calling c_parser_error. */
7226 parser->error = true;
7227 c_parser_skip_to_pragma_eol (parser);
7229 return false;
7232 /* The interface the pragma parsers have to the lexer. */
7234 enum cpp_ttype
7235 pragma_lex (tree *value)
7237 c_token *tok = c_parser_peek_token (the_parser);
7238 enum cpp_ttype ret = tok->type;
7240 *value = tok->value;
7241 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
7242 ret = CPP_EOF;
7243 else
7245 if (ret == CPP_KEYWORD)
7246 ret = CPP_NAME;
7247 c_parser_consume_token (the_parser);
7250 return ret;
7253 static void
7254 c_parser_pragma_pch_preprocess (c_parser *parser)
7256 tree name = NULL;
7258 c_parser_consume_pragma (parser);
7259 if (c_parser_next_token_is (parser, CPP_STRING))
7261 name = c_parser_peek_token (parser)->value;
7262 c_parser_consume_token (parser);
7264 else
7265 c_parser_error (parser, "expected string literal");
7266 c_parser_skip_to_pragma_eol (parser);
7268 if (name)
7269 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
7272 /* OpenMP 2.5 parsing routines. */
7274 /* Returns name of the next clause.
7275 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
7276 the token is not consumed. Otherwise appropriate pragma_omp_clause is
7277 returned and the token is consumed. */
7279 static pragma_omp_clause
7280 c_parser_omp_clause_name (c_parser *parser)
7282 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7284 if (c_parser_next_token_is_keyword (parser, RID_IF))
7285 result = PRAGMA_OMP_CLAUSE_IF;
7286 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7287 result = PRAGMA_OMP_CLAUSE_DEFAULT;
7288 else if (c_parser_next_token_is (parser, CPP_NAME))
7290 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7292 switch (p[0])
7294 case 'c':
7295 if (!strcmp ("collapse", p))
7296 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7297 else if (!strcmp ("copyin", p))
7298 result = PRAGMA_OMP_CLAUSE_COPYIN;
7299 else if (!strcmp ("copyprivate", p))
7300 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7301 break;
7302 case 'f':
7303 if (!strcmp ("firstprivate", p))
7304 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7305 break;
7306 case 'l':
7307 if (!strcmp ("lastprivate", p))
7308 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7309 break;
7310 case 'n':
7311 if (!strcmp ("nowait", p))
7312 result = PRAGMA_OMP_CLAUSE_NOWAIT;
7313 else if (!strcmp ("num_threads", p))
7314 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7315 break;
7316 case 'o':
7317 if (!strcmp ("ordered", p))
7318 result = PRAGMA_OMP_CLAUSE_ORDERED;
7319 break;
7320 case 'p':
7321 if (!strcmp ("private", p))
7322 result = PRAGMA_OMP_CLAUSE_PRIVATE;
7323 break;
7324 case 'r':
7325 if (!strcmp ("reduction", p))
7326 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7327 break;
7328 case 's':
7329 if (!strcmp ("schedule", p))
7330 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7331 else if (!strcmp ("shared", p))
7332 result = PRAGMA_OMP_CLAUSE_SHARED;
7333 break;
7334 case 'u':
7335 if (!strcmp ("untied", p))
7336 result = PRAGMA_OMP_CLAUSE_UNTIED;
7337 break;
7341 if (result != PRAGMA_OMP_CLAUSE_NONE)
7342 c_parser_consume_token (parser);
7344 return result;
7347 /* Validate that a clause of the given type does not already exist. */
7349 static void
7350 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7351 const char *name)
7353 tree c;
7355 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7356 if (OMP_CLAUSE_CODE (c) == code)
7358 location_t loc = OMP_CLAUSE_LOCATION (c);
7359 error_at (loc, "too many %qs clauses", name);
7360 break;
7364 /* OpenMP 2.5:
7365 variable-list:
7366 identifier
7367 variable-list , identifier
7369 If KIND is nonzero, create the appropriate node and install the
7370 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7371 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
7373 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7374 return the list created. */
7376 static tree
7377 c_parser_omp_variable_list (c_parser *parser,
7378 location_t clause_loc,
7379 enum omp_clause_code kind,
7380 tree list)
7382 if (c_parser_next_token_is_not (parser, CPP_NAME)
7383 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7384 c_parser_error (parser, "expected identifier");
7386 while (c_parser_next_token_is (parser, CPP_NAME)
7387 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7389 tree t = lookup_name (c_parser_peek_token (parser)->value);
7391 if (t == NULL_TREE)
7392 undeclared_variable (c_parser_peek_token (parser)->location,
7393 c_parser_peek_token (parser)->value);
7394 else if (t == error_mark_node)
7396 else if (kind != 0)
7398 tree u = build_omp_clause (clause_loc, kind);
7399 OMP_CLAUSE_DECL (u) = t;
7400 OMP_CLAUSE_CHAIN (u) = list;
7401 list = u;
7403 else
7404 list = tree_cons (t, NULL_TREE, list);
7406 c_parser_consume_token (parser);
7408 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7409 break;
7411 c_parser_consume_token (parser);
7414 return list;
7417 /* Similarly, but expect leading and trailing parenthesis. This is a very
7418 common case for omp clauses. */
7420 static tree
7421 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7422 tree list)
7424 /* The clauses location. */
7425 location_t loc = c_parser_peek_token (parser)->location;
7427 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7429 list = c_parser_omp_variable_list (parser, loc, kind, list);
7430 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7432 return list;
7435 /* OpenMP 3.0:
7436 collapse ( constant-expression ) */
7438 static tree
7439 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7441 tree c, num = error_mark_node;
7442 HOST_WIDE_INT n;
7443 location_t loc;
7445 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7447 loc = c_parser_peek_token (parser)->location;
7448 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7450 num = c_parser_expr_no_commas (parser, NULL).value;
7451 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7453 if (num == error_mark_node)
7454 return list;
7455 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7456 || !host_integerp (num, 0)
7457 || (n = tree_low_cst (num, 0)) <= 0
7458 || (int) n != n)
7460 error_at (loc,
7461 "collapse argument needs positive constant integer expression");
7462 return list;
7464 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
7465 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7466 OMP_CLAUSE_CHAIN (c) = list;
7467 return c;
7470 /* OpenMP 2.5:
7471 copyin ( variable-list ) */
7473 static tree
7474 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7476 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7479 /* OpenMP 2.5:
7480 copyprivate ( variable-list ) */
7482 static tree
7483 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7485 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7488 /* OpenMP 2.5:
7489 default ( shared | none ) */
7491 static tree
7492 c_parser_omp_clause_default (c_parser *parser, tree list)
7494 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7495 location_t loc = c_parser_peek_token (parser)->location;
7496 tree c;
7498 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7499 return list;
7500 if (c_parser_next_token_is (parser, CPP_NAME))
7502 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7504 switch (p[0])
7506 case 'n':
7507 if (strcmp ("none", p) != 0)
7508 goto invalid_kind;
7509 kind = OMP_CLAUSE_DEFAULT_NONE;
7510 break;
7512 case 's':
7513 if (strcmp ("shared", p) != 0)
7514 goto invalid_kind;
7515 kind = OMP_CLAUSE_DEFAULT_SHARED;
7516 break;
7518 default:
7519 goto invalid_kind;
7522 c_parser_consume_token (parser);
7524 else
7526 invalid_kind:
7527 c_parser_error (parser, "expected %<none%> or %<shared%>");
7529 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7531 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7532 return list;
7534 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7535 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
7536 OMP_CLAUSE_CHAIN (c) = list;
7537 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7539 return c;
7542 /* OpenMP 2.5:
7543 firstprivate ( variable-list ) */
7545 static tree
7546 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7548 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7551 /* OpenMP 2.5:
7552 if ( expression ) */
7554 static tree
7555 c_parser_omp_clause_if (c_parser *parser, tree list)
7557 location_t loc = c_parser_peek_token (parser)->location;
7558 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7560 tree t = c_parser_paren_condition (parser);
7561 tree c;
7563 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7565 c = build_omp_clause (loc, OMP_CLAUSE_IF);
7566 OMP_CLAUSE_IF_EXPR (c) = t;
7567 OMP_CLAUSE_CHAIN (c) = list;
7568 list = c;
7570 else
7571 c_parser_error (parser, "expected %<(%>");
7573 return list;
7576 /* OpenMP 2.5:
7577 lastprivate ( variable-list ) */
7579 static tree
7580 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7582 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7585 /* OpenMP 2.5:
7586 nowait */
7588 static tree
7589 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7591 tree c;
7592 location_t loc = c_parser_peek_token (parser)->location;
7594 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7596 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
7597 OMP_CLAUSE_CHAIN (c) = list;
7598 return c;
7601 /* OpenMP 2.5:
7602 num_threads ( expression ) */
7604 static tree
7605 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7607 location_t num_threads_loc = c_parser_peek_token (parser)->location;
7608 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7610 location_t expr_loc = c_parser_peek_token (parser)->location;
7611 tree c, t = c_parser_expression (parser).value;
7612 t = c_fully_fold (t, false, NULL);
7614 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7616 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7618 c_parser_error (parser, "expected integer expression");
7619 return list;
7622 /* Attempt to statically determine when the number isn't positive. */
7623 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
7624 build_int_cst (TREE_TYPE (t), 0));
7625 if (CAN_HAVE_LOCATION_P (c))
7626 SET_EXPR_LOCATION (c, expr_loc);
7627 if (c == boolean_true_node)
7629 warning_at (expr_loc, 0,
7630 "%<num_threads%> value must be positive");
7631 t = integer_one_node;
7634 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7636 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
7637 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7638 OMP_CLAUSE_CHAIN (c) = list;
7639 list = c;
7642 return list;
7645 /* OpenMP 2.5:
7646 ordered */
7648 static tree
7649 c_parser_omp_clause_ordered (c_parser *parser, tree list)
7651 tree c;
7653 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7655 c = build_omp_clause (c_parser_peek_token (parser)->location,
7656 OMP_CLAUSE_ORDERED);
7657 OMP_CLAUSE_CHAIN (c) = list;
7659 return c;
7662 /* OpenMP 2.5:
7663 private ( variable-list ) */
7665 static tree
7666 c_parser_omp_clause_private (c_parser *parser, tree list)
7668 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7671 /* OpenMP 2.5:
7672 reduction ( reduction-operator : variable-list )
7674 reduction-operator:
7675 One of: + * - & ^ | && || */
7677 static tree
7678 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7680 location_t clause_loc = c_parser_peek_token (parser)->location;
7681 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7683 enum tree_code code;
7685 switch (c_parser_peek_token (parser)->type)
7687 case CPP_PLUS:
7688 code = PLUS_EXPR;
7689 break;
7690 case CPP_MULT:
7691 code = MULT_EXPR;
7692 break;
7693 case CPP_MINUS:
7694 code = MINUS_EXPR;
7695 break;
7696 case CPP_AND:
7697 code = BIT_AND_EXPR;
7698 break;
7699 case CPP_XOR:
7700 code = BIT_XOR_EXPR;
7701 break;
7702 case CPP_OR:
7703 code = BIT_IOR_EXPR;
7704 break;
7705 case CPP_AND_AND:
7706 code = TRUTH_ANDIF_EXPR;
7707 break;
7708 case CPP_OR_OR:
7709 code = TRUTH_ORIF_EXPR;
7710 break;
7711 default:
7712 c_parser_error (parser,
7713 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7714 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7715 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7716 return list;
7718 c_parser_consume_token (parser);
7719 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7721 tree nl, c;
7723 nl = c_parser_omp_variable_list (parser, clause_loc,
7724 OMP_CLAUSE_REDUCTION, list);
7725 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7726 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7728 list = nl;
7730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7732 return list;
7735 /* OpenMP 2.5:
7736 schedule ( schedule-kind )
7737 schedule ( schedule-kind , expression )
7739 schedule-kind:
7740 static | dynamic | guided | runtime | auto
7743 static tree
7744 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7746 tree c, t;
7747 location_t loc = c_parser_peek_token (parser)->location;
7749 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7750 return list;
7752 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
7754 if (c_parser_next_token_is (parser, CPP_NAME))
7756 tree kind = c_parser_peek_token (parser)->value;
7757 const char *p = IDENTIFIER_POINTER (kind);
7759 switch (p[0])
7761 case 'd':
7762 if (strcmp ("dynamic", p) != 0)
7763 goto invalid_kind;
7764 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7765 break;
7767 case 'g':
7768 if (strcmp ("guided", p) != 0)
7769 goto invalid_kind;
7770 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7771 break;
7773 case 'r':
7774 if (strcmp ("runtime", p) != 0)
7775 goto invalid_kind;
7776 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7777 break;
7779 default:
7780 goto invalid_kind;
7783 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7784 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7785 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7786 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7787 else
7788 goto invalid_kind;
7790 c_parser_consume_token (parser);
7791 if (c_parser_next_token_is (parser, CPP_COMMA))
7793 location_t here;
7794 c_parser_consume_token (parser);
7796 here = c_parser_peek_token (parser)->location;
7797 t = c_parser_expr_no_commas (parser, NULL).value;
7798 t = c_fully_fold (t, false, NULL);
7800 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7801 error_at (here, "schedule %<runtime%> does not take "
7802 "a %<chunk_size%> parameter");
7803 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7804 error_at (here,
7805 "schedule %<auto%> does not take "
7806 "a %<chunk_size%> parameter");
7807 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7808 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7809 else
7810 c_parser_error (parser, "expected integer expression");
7812 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7814 else
7815 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7816 "expected %<,%> or %<)%>");
7818 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7819 OMP_CLAUSE_CHAIN (c) = list;
7820 return c;
7822 invalid_kind:
7823 c_parser_error (parser, "invalid schedule kind");
7824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7825 return list;
7828 /* OpenMP 2.5:
7829 shared ( variable-list ) */
7831 static tree
7832 c_parser_omp_clause_shared (c_parser *parser, tree list)
7834 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7837 /* OpenMP 3.0:
7838 untied */
7840 static tree
7841 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7843 tree c;
7845 /* FIXME: Should we allow duplicates? */
7846 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7848 c = build_omp_clause (c_parser_peek_token (parser)->location,
7849 OMP_CLAUSE_UNTIED);
7850 OMP_CLAUSE_CHAIN (c) = list;
7852 return c;
7855 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7856 is a bitmask in MASK. Return the list of clauses found; the result
7857 of clause default goes in *pdefault. */
7859 static tree
7860 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7861 const char *where)
7863 tree clauses = NULL;
7864 bool first = true;
7866 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7868 location_t here;
7869 pragma_omp_clause c_kind;
7870 const char *c_name;
7871 tree prev = clauses;
7873 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7874 c_parser_consume_token (parser);
7876 first = false;
7877 here = c_parser_peek_token (parser)->location;
7878 c_kind = c_parser_omp_clause_name (parser);
7880 switch (c_kind)
7882 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7883 clauses = c_parser_omp_clause_collapse (parser, clauses);
7884 c_name = "collapse";
7885 break;
7886 case PRAGMA_OMP_CLAUSE_COPYIN:
7887 clauses = c_parser_omp_clause_copyin (parser, clauses);
7888 c_name = "copyin";
7889 break;
7890 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7891 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7892 c_name = "copyprivate";
7893 break;
7894 case PRAGMA_OMP_CLAUSE_DEFAULT:
7895 clauses = c_parser_omp_clause_default (parser, clauses);
7896 c_name = "default";
7897 break;
7898 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7899 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7900 c_name = "firstprivate";
7901 break;
7902 case PRAGMA_OMP_CLAUSE_IF:
7903 clauses = c_parser_omp_clause_if (parser, clauses);
7904 c_name = "if";
7905 break;
7906 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7907 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7908 c_name = "lastprivate";
7909 break;
7910 case PRAGMA_OMP_CLAUSE_NOWAIT:
7911 clauses = c_parser_omp_clause_nowait (parser, clauses);
7912 c_name = "nowait";
7913 break;
7914 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7915 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7916 c_name = "num_threads";
7917 break;
7918 case PRAGMA_OMP_CLAUSE_ORDERED:
7919 clauses = c_parser_omp_clause_ordered (parser, clauses);
7920 c_name = "ordered";
7921 break;
7922 case PRAGMA_OMP_CLAUSE_PRIVATE:
7923 clauses = c_parser_omp_clause_private (parser, clauses);
7924 c_name = "private";
7925 break;
7926 case PRAGMA_OMP_CLAUSE_REDUCTION:
7927 clauses = c_parser_omp_clause_reduction (parser, clauses);
7928 c_name = "reduction";
7929 break;
7930 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7931 clauses = c_parser_omp_clause_schedule (parser, clauses);
7932 c_name = "schedule";
7933 break;
7934 case PRAGMA_OMP_CLAUSE_SHARED:
7935 clauses = c_parser_omp_clause_shared (parser, clauses);
7936 c_name = "shared";
7937 break;
7938 case PRAGMA_OMP_CLAUSE_UNTIED:
7939 clauses = c_parser_omp_clause_untied (parser, clauses);
7940 c_name = "untied";
7941 break;
7942 default:
7943 c_parser_error (parser, "expected %<#pragma omp%> clause");
7944 goto saw_error;
7947 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7949 /* Remove the invalid clause(s) from the list to avoid
7950 confusing the rest of the compiler. */
7951 clauses = prev;
7952 error_at (here, "%qs is not valid for %qs", c_name, where);
7956 saw_error:
7957 c_parser_skip_to_pragma_eol (parser);
7959 return c_finish_omp_clauses (clauses);
7962 /* OpenMP 2.5:
7963 structured-block:
7964 statement
7966 In practice, we're also interested in adding the statement to an
7967 outer node. So it is convenient if we work around the fact that
7968 c_parser_statement calls add_stmt. */
7970 static tree
7971 c_parser_omp_structured_block (c_parser *parser)
7973 tree stmt = push_stmt_list ();
7974 c_parser_statement (parser);
7975 return pop_stmt_list (stmt);
7978 /* OpenMP 2.5:
7979 # pragma omp atomic new-line
7980 expression-stmt
7982 expression-stmt:
7983 x binop= expr | x++ | ++x | x-- | --x
7984 binop:
7985 +, *, -, /, &, ^, |, <<, >>
7987 where x is an lvalue expression with scalar type.
7989 LOC is the location of the #pragma token. */
7991 static void
7992 c_parser_omp_atomic (location_t loc, c_parser *parser)
7994 tree lhs, rhs;
7995 tree stmt;
7996 enum tree_code code;
7997 struct c_expr rhs_expr;
7999 c_parser_skip_to_pragma_eol (parser);
8001 lhs = c_parser_unary_expression (parser).value;
8002 lhs = c_fully_fold (lhs, false, NULL);
8003 switch (TREE_CODE (lhs))
8005 case ERROR_MARK:
8006 saw_error:
8007 c_parser_skip_to_end_of_block_or_statement (parser);
8008 return;
8010 case PREINCREMENT_EXPR:
8011 case POSTINCREMENT_EXPR:
8012 lhs = TREE_OPERAND (lhs, 0);
8013 code = PLUS_EXPR;
8014 rhs = integer_one_node;
8015 break;
8017 case PREDECREMENT_EXPR:
8018 case POSTDECREMENT_EXPR:
8019 lhs = TREE_OPERAND (lhs, 0);
8020 code = MINUS_EXPR;
8021 rhs = integer_one_node;
8022 break;
8024 default:
8025 switch (c_parser_peek_token (parser)->type)
8027 case CPP_MULT_EQ:
8028 code = MULT_EXPR;
8029 break;
8030 case CPP_DIV_EQ:
8031 code = TRUNC_DIV_EXPR;
8032 break;
8033 case CPP_PLUS_EQ:
8034 code = PLUS_EXPR;
8035 break;
8036 case CPP_MINUS_EQ:
8037 code = MINUS_EXPR;
8038 break;
8039 case CPP_LSHIFT_EQ:
8040 code = LSHIFT_EXPR;
8041 break;
8042 case CPP_RSHIFT_EQ:
8043 code = RSHIFT_EXPR;
8044 break;
8045 case CPP_AND_EQ:
8046 code = BIT_AND_EXPR;
8047 break;
8048 case CPP_OR_EQ:
8049 code = BIT_IOR_EXPR;
8050 break;
8051 case CPP_XOR_EQ:
8052 code = BIT_XOR_EXPR;
8053 break;
8054 default:
8055 c_parser_error (parser,
8056 "invalid operator for %<#pragma omp atomic%>");
8057 goto saw_error;
8060 c_parser_consume_token (parser);
8062 location_t rhs_loc = c_parser_peek_token (parser)->location;
8063 rhs_expr = c_parser_expression (parser);
8064 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
8066 rhs = rhs_expr.value;
8067 rhs = c_fully_fold (rhs, false, NULL);
8068 break;
8070 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
8071 if (stmt != error_mark_node)
8072 add_stmt (stmt);
8073 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8077 /* OpenMP 2.5:
8078 # pragma omp barrier new-line
8081 static void
8082 c_parser_omp_barrier (c_parser *parser)
8084 location_t loc = c_parser_peek_token (parser)->location;
8085 c_parser_consume_pragma (parser);
8086 c_parser_skip_to_pragma_eol (parser);
8088 c_finish_omp_barrier (loc);
8091 /* OpenMP 2.5:
8092 # pragma omp critical [(name)] new-line
8093 structured-block
8095 LOC is the location of the #pragma itself. */
8097 static tree
8098 c_parser_omp_critical (location_t loc, c_parser *parser)
8100 tree stmt, name = NULL;
8102 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8104 c_parser_consume_token (parser);
8105 if (c_parser_next_token_is (parser, CPP_NAME))
8107 name = c_parser_peek_token (parser)->value;
8108 c_parser_consume_token (parser);
8109 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8111 else
8112 c_parser_error (parser, "expected identifier");
8114 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8115 c_parser_error (parser, "expected %<(%> or end of line");
8116 c_parser_skip_to_pragma_eol (parser);
8118 stmt = c_parser_omp_structured_block (parser);
8119 return c_finish_omp_critical (loc, stmt, name);
8122 /* OpenMP 2.5:
8123 # pragma omp flush flush-vars[opt] new-line
8125 flush-vars:
8126 ( variable-list ) */
8128 static void
8129 c_parser_omp_flush (c_parser *parser)
8131 location_t loc = c_parser_peek_token (parser)->location;
8132 c_parser_consume_pragma (parser);
8133 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8134 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8135 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8136 c_parser_error (parser, "expected %<(%> or end of line");
8137 c_parser_skip_to_pragma_eol (parser);
8139 c_finish_omp_flush (loc);
8142 /* Parse the restricted form of the for statement allowed by OpenMP.
8143 The real trick here is to determine the loop control variable early
8144 so that we can push a new decl if necessary to make it private.
8145 LOC is the location of the OMP in "#pragma omp". */
8147 static tree
8148 c_parser_omp_for_loop (location_t loc,
8149 c_parser *parser, tree clauses, tree *par_clauses)
8151 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
8152 tree declv, condv, incrv, initv, ret = NULL;
8153 bool fail = false, open_brace_parsed = false;
8154 int i, collapse = 1, nbraces = 0;
8155 location_t for_loc;
8156 VEC(tree,gc) *for_block = make_tree_vector ();
8158 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8159 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8160 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8162 gcc_assert (collapse >= 1);
8164 declv = make_tree_vec (collapse);
8165 initv = make_tree_vec (collapse);
8166 condv = make_tree_vec (collapse);
8167 incrv = make_tree_vec (collapse);
8169 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8171 c_parser_error (parser, "for statement expected");
8172 return NULL;
8174 for_loc = c_parser_peek_token (parser)->location;
8175 c_parser_consume_token (parser);
8177 for (i = 0; i < collapse; i++)
8179 int bracecount = 0;
8181 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8182 goto pop_scopes;
8184 /* Parse the initialization declaration or expression. */
8185 if (c_parser_next_token_starts_declaration (parser))
8187 if (i > 0)
8188 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
8189 c_parser_declaration_or_fndef (parser, true, true, true, true, true);
8190 decl = check_for_loop_decls (for_loc);
8191 if (decl == NULL)
8192 goto error_init;
8193 if (DECL_INITIAL (decl) == error_mark_node)
8194 decl = error_mark_node;
8195 init = decl;
8197 else if (c_parser_next_token_is (parser, CPP_NAME)
8198 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
8200 struct c_expr decl_exp;
8201 struct c_expr init_exp;
8202 location_t init_loc;
8204 decl_exp = c_parser_postfix_expression (parser);
8205 decl = decl_exp.value;
8207 c_parser_require (parser, CPP_EQ, "expected %<=%>");
8209 init_loc = c_parser_peek_token (parser)->location;
8210 init_exp = c_parser_expr_no_commas (parser, NULL);
8211 init_exp = default_function_array_read_conversion (init_loc,
8212 init_exp);
8213 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
8214 NOP_EXPR, init_loc, init_exp.value,
8215 init_exp.original_type);
8216 init = c_process_expr_stmt (init_loc, init);
8218 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8220 else
8222 error_init:
8223 c_parser_error (parser,
8224 "expected iteration declaration or initialization");
8225 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8226 "expected %<)%>");
8227 fail = true;
8228 goto parse_next;
8231 /* Parse the loop condition. */
8232 cond = NULL_TREE;
8233 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
8235 location_t cond_loc = c_parser_peek_token (parser)->location;
8236 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
8238 cond = cond_expr.value;
8239 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
8240 cond = c_fully_fold (cond, false, NULL);
8241 switch (cond_expr.original_code)
8243 case GT_EXPR:
8244 case GE_EXPR:
8245 case LT_EXPR:
8246 case LE_EXPR:
8247 break;
8248 default:
8249 /* Can't be cond = error_mark_node, because we want to preserve
8250 the location until c_finish_omp_for. */
8251 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
8252 break;
8254 protected_set_expr_location (cond, cond_loc);
8256 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8258 /* Parse the increment expression. */
8259 incr = NULL_TREE;
8260 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
8262 location_t incr_loc = c_parser_peek_token (parser)->location;
8264 incr = c_process_expr_stmt (incr_loc,
8265 c_parser_expression (parser).value);
8267 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8269 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
8270 fail = true;
8271 else
8273 TREE_VEC_ELT (declv, i) = decl;
8274 TREE_VEC_ELT (initv, i) = init;
8275 TREE_VEC_ELT (condv, i) = cond;
8276 TREE_VEC_ELT (incrv, i) = incr;
8279 parse_next:
8280 if (i == collapse - 1)
8281 break;
8283 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8284 in between the collapsed for loops to be still considered perfectly
8285 nested. Hopefully the final version clarifies this.
8286 For now handle (multiple) {'s and empty statements. */
8289 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8291 c_parser_consume_token (parser);
8292 break;
8294 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8296 c_parser_consume_token (parser);
8297 bracecount++;
8299 else if (bracecount
8300 && c_parser_next_token_is (parser, CPP_SEMICOLON))
8301 c_parser_consume_token (parser);
8302 else
8304 c_parser_error (parser, "not enough perfectly nested loops");
8305 if (bracecount)
8307 open_brace_parsed = true;
8308 bracecount--;
8310 fail = true;
8311 collapse = 0;
8312 break;
8315 while (1);
8317 nbraces += bracecount;
8320 save_break = c_break_label;
8321 c_break_label = size_one_node;
8322 save_cont = c_cont_label;
8323 c_cont_label = NULL_TREE;
8324 body = push_stmt_list ();
8326 if (open_brace_parsed)
8328 location_t here = c_parser_peek_token (parser)->location;
8329 stmt = c_begin_compound_stmt (true);
8330 c_parser_compound_statement_nostart (parser);
8331 add_stmt (c_end_compound_stmt (here, stmt, true));
8333 else
8334 add_stmt (c_parser_c99_block_statement (parser));
8335 if (c_cont_label)
8337 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8338 SET_EXPR_LOCATION (t, loc);
8339 add_stmt (t);
8342 body = pop_stmt_list (body);
8343 c_break_label = save_break;
8344 c_cont_label = save_cont;
8346 while (nbraces)
8348 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8350 c_parser_consume_token (parser);
8351 nbraces--;
8353 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8354 c_parser_consume_token (parser);
8355 else
8357 c_parser_error (parser, "collapsed loops not perfectly nested");
8358 while (nbraces)
8360 location_t here = c_parser_peek_token (parser)->location;
8361 stmt = c_begin_compound_stmt (true);
8362 add_stmt (body);
8363 c_parser_compound_statement_nostart (parser);
8364 body = c_end_compound_stmt (here, stmt, true);
8365 nbraces--;
8367 goto pop_scopes;
8371 /* Only bother calling c_finish_omp_for if we haven't already generated
8372 an error from the initialization parsing. */
8373 if (!fail)
8375 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8376 if (stmt)
8378 if (par_clauses != NULL)
8380 tree *c;
8381 for (c = par_clauses; *c ; )
8382 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8383 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8384 c = &OMP_CLAUSE_CHAIN (*c);
8385 else
8387 for (i = 0; i < collapse; i++)
8388 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8389 break;
8390 if (i == collapse)
8391 c = &OMP_CLAUSE_CHAIN (*c);
8392 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8394 error_at (loc,
8395 "iteration variable %qD should not be firstprivate",
8396 OMP_CLAUSE_DECL (*c));
8397 *c = OMP_CLAUSE_CHAIN (*c);
8399 else
8401 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8402 change it to shared (decl) in
8403 OMP_PARALLEL_CLAUSES. */
8404 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8405 OMP_CLAUSE_LASTPRIVATE);
8406 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8407 OMP_CLAUSE_CHAIN (l) = clauses;
8408 clauses = l;
8409 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8413 OMP_FOR_CLAUSES (stmt) = clauses;
8415 ret = stmt;
8417 pop_scopes:
8418 while (!VEC_empty (tree, for_block))
8420 /* FIXME diagnostics: LOC below should be the actual location of
8421 this particular for block. We need to build a list of
8422 locations to go along with FOR_BLOCK. */
8423 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
8424 add_stmt (stmt);
8426 release_tree_vector (for_block);
8427 return ret;
8430 /* OpenMP 2.5:
8431 #pragma omp for for-clause[optseq] new-line
8432 for-loop
8434 LOC is the location of the #pragma token.
8437 #define OMP_FOR_CLAUSE_MASK \
8438 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8439 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8440 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8441 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8442 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8443 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
8444 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
8445 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8447 static tree
8448 c_parser_omp_for (location_t loc, c_parser *parser)
8450 tree block, clauses, ret;
8452 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8453 "#pragma omp for");
8455 block = c_begin_compound_stmt (true);
8456 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8457 block = c_end_compound_stmt (loc, block, true);
8458 add_stmt (block);
8460 return ret;
8463 /* OpenMP 2.5:
8464 # pragma omp master new-line
8465 structured-block
8467 LOC is the location of the #pragma token.
8470 static tree
8471 c_parser_omp_master (location_t loc, c_parser *parser)
8473 c_parser_skip_to_pragma_eol (parser);
8474 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
8477 /* OpenMP 2.5:
8478 # pragma omp ordered new-line
8479 structured-block
8481 LOC is the location of the #pragma itself.
8484 static tree
8485 c_parser_omp_ordered (location_t loc, c_parser *parser)
8487 c_parser_skip_to_pragma_eol (parser);
8488 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
8491 /* OpenMP 2.5:
8493 section-scope:
8494 { section-sequence }
8496 section-sequence:
8497 section-directive[opt] structured-block
8498 section-sequence section-directive structured-block
8500 SECTIONS_LOC is the location of the #pragma omp sections. */
8502 static tree
8503 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
8505 tree stmt, substmt;
8506 bool error_suppress = false;
8507 location_t loc;
8509 loc = c_parser_peek_token (parser)->location;
8510 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8512 /* Avoid skipping until the end of the block. */
8513 parser->error = false;
8514 return NULL_TREE;
8517 stmt = push_stmt_list ();
8519 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8521 substmt = push_stmt_list ();
8523 while (1)
8525 c_parser_statement (parser);
8527 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8528 break;
8529 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8530 break;
8531 if (c_parser_next_token_is (parser, CPP_EOF))
8532 break;
8535 substmt = pop_stmt_list (substmt);
8536 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8537 SET_EXPR_LOCATION (substmt, loc);
8538 add_stmt (substmt);
8541 while (1)
8543 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8544 break;
8545 if (c_parser_next_token_is (parser, CPP_EOF))
8546 break;
8548 loc = c_parser_peek_token (parser)->location;
8549 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8551 c_parser_consume_pragma (parser);
8552 c_parser_skip_to_pragma_eol (parser);
8553 error_suppress = false;
8555 else if (!error_suppress)
8557 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8558 error_suppress = true;
8561 substmt = c_parser_omp_structured_block (parser);
8562 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8563 SET_EXPR_LOCATION (substmt, loc);
8564 add_stmt (substmt);
8566 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8567 "expected %<#pragma omp section%> or %<}%>");
8569 substmt = pop_stmt_list (stmt);
8571 stmt = make_node (OMP_SECTIONS);
8572 SET_EXPR_LOCATION (stmt, sections_loc);
8573 TREE_TYPE (stmt) = void_type_node;
8574 OMP_SECTIONS_BODY (stmt) = substmt;
8576 return add_stmt (stmt);
8579 /* OpenMP 2.5:
8580 # pragma omp sections sections-clause[optseq] newline
8581 sections-scope
8583 LOC is the location of the #pragma token.
8586 #define OMP_SECTIONS_CLAUSE_MASK \
8587 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8588 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8589 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8590 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8591 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8593 static tree
8594 c_parser_omp_sections (location_t loc, c_parser *parser)
8596 tree block, clauses, ret;
8598 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8599 "#pragma omp sections");
8601 block = c_begin_compound_stmt (true);
8602 ret = c_parser_omp_sections_scope (loc, parser);
8603 if (ret)
8604 OMP_SECTIONS_CLAUSES (ret) = clauses;
8605 block = c_end_compound_stmt (loc, block, true);
8606 add_stmt (block);
8608 return ret;
8611 /* OpenMP 2.5:
8612 # pragma parallel parallel-clause new-line
8613 # pragma parallel for parallel-for-clause new-line
8614 # pragma parallel sections parallel-sections-clause new-line
8616 LOC is the location of the #pragma token.
8619 #define OMP_PARALLEL_CLAUSE_MASK \
8620 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8621 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8622 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8623 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8624 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8625 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8626 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8627 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8629 static tree
8630 c_parser_omp_parallel (location_t loc, c_parser *parser)
8632 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8633 const char *p_name = "#pragma omp parallel";
8634 tree stmt, clauses, par_clause, ws_clause, block;
8635 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8637 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8639 c_parser_consume_token (parser);
8640 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8641 p_name = "#pragma omp parallel for";
8642 mask |= OMP_FOR_CLAUSE_MASK;
8643 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8645 else if (c_parser_next_token_is (parser, CPP_NAME))
8647 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8648 if (strcmp (p, "sections") == 0)
8650 c_parser_consume_token (parser);
8651 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8652 p_name = "#pragma omp parallel sections";
8653 mask |= OMP_SECTIONS_CLAUSE_MASK;
8654 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8658 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8660 switch (p_kind)
8662 case PRAGMA_OMP_PARALLEL:
8663 block = c_begin_omp_parallel ();
8664 c_parser_statement (parser);
8665 stmt = c_finish_omp_parallel (loc, clauses, block);
8666 break;
8668 case PRAGMA_OMP_PARALLEL_FOR:
8669 block = c_begin_omp_parallel ();
8670 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8671 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
8672 stmt = c_finish_omp_parallel (loc, par_clause, block);
8673 OMP_PARALLEL_COMBINED (stmt) = 1;
8674 break;
8676 case PRAGMA_OMP_PARALLEL_SECTIONS:
8677 block = c_begin_omp_parallel ();
8678 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8679 stmt = c_parser_omp_sections_scope (loc, parser);
8680 if (stmt)
8681 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8682 stmt = c_finish_omp_parallel (loc, par_clause, block);
8683 OMP_PARALLEL_COMBINED (stmt) = 1;
8684 break;
8686 default:
8687 gcc_unreachable ();
8690 return stmt;
8693 /* OpenMP 2.5:
8694 # pragma omp single single-clause[optseq] new-line
8695 structured-block
8697 LOC is the location of the #pragma.
8700 #define OMP_SINGLE_CLAUSE_MASK \
8701 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8702 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8703 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8704 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8706 static tree
8707 c_parser_omp_single (location_t loc, c_parser *parser)
8709 tree stmt = make_node (OMP_SINGLE);
8710 SET_EXPR_LOCATION (stmt, loc);
8711 TREE_TYPE (stmt) = void_type_node;
8713 OMP_SINGLE_CLAUSES (stmt)
8714 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8715 "#pragma omp single");
8716 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8718 return add_stmt (stmt);
8721 /* OpenMP 3.0:
8722 # pragma omp task task-clause[optseq] new-line
8724 LOC is the location of the #pragma.
8727 #define OMP_TASK_CLAUSE_MASK \
8728 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8729 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8730 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8731 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8732 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8733 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8735 static tree
8736 c_parser_omp_task (location_t loc, c_parser *parser)
8738 tree clauses, block;
8740 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8741 "#pragma omp task");
8743 block = c_begin_omp_task ();
8744 c_parser_statement (parser);
8745 return c_finish_omp_task (loc, clauses, block);
8748 /* OpenMP 3.0:
8749 # pragma omp taskwait new-line
8752 static void
8753 c_parser_omp_taskwait (c_parser *parser)
8755 location_t loc = c_parser_peek_token (parser)->location;
8756 c_parser_consume_pragma (parser);
8757 c_parser_skip_to_pragma_eol (parser);
8759 c_finish_omp_taskwait (loc);
8762 /* Main entry point to parsing most OpenMP pragmas. */
8764 static void
8765 c_parser_omp_construct (c_parser *parser)
8767 enum pragma_kind p_kind;
8768 location_t loc;
8769 tree stmt;
8771 loc = c_parser_peek_token (parser)->location;
8772 p_kind = c_parser_peek_token (parser)->pragma_kind;
8773 c_parser_consume_pragma (parser);
8775 switch (p_kind)
8777 case PRAGMA_OMP_ATOMIC:
8778 c_parser_omp_atomic (loc, parser);
8779 return;
8780 case PRAGMA_OMP_CRITICAL:
8781 stmt = c_parser_omp_critical (loc, parser);
8782 break;
8783 case PRAGMA_OMP_FOR:
8784 stmt = c_parser_omp_for (loc, parser);
8785 break;
8786 case PRAGMA_OMP_MASTER:
8787 stmt = c_parser_omp_master (loc, parser);
8788 break;
8789 case PRAGMA_OMP_ORDERED:
8790 stmt = c_parser_omp_ordered (loc, parser);
8791 break;
8792 case PRAGMA_OMP_PARALLEL:
8793 stmt = c_parser_omp_parallel (loc, parser);
8794 break;
8795 case PRAGMA_OMP_SECTIONS:
8796 stmt = c_parser_omp_sections (loc, parser);
8797 break;
8798 case PRAGMA_OMP_SINGLE:
8799 stmt = c_parser_omp_single (loc, parser);
8800 break;
8801 case PRAGMA_OMP_TASK:
8802 stmt = c_parser_omp_task (loc, parser);
8803 break;
8804 default:
8805 gcc_unreachable ();
8808 if (stmt)
8809 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
8813 /* OpenMP 2.5:
8814 # pragma omp threadprivate (variable-list) */
8816 static void
8817 c_parser_omp_threadprivate (c_parser *parser)
8819 tree vars, t;
8820 location_t loc;
8822 c_parser_consume_pragma (parser);
8823 loc = c_parser_peek_token (parser)->location;
8824 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8826 /* Mark every variable in VARS to be assigned thread local storage. */
8827 for (t = vars; t; t = TREE_CHAIN (t))
8829 tree v = TREE_PURPOSE (t);
8831 /* FIXME diagnostics: Ideally we should keep individual
8832 locations for all the variables in the var list to make the
8833 following errors more precise. Perhaps
8834 c_parser_omp_var_list_parens() should construct a list of
8835 locations to go along with the var list. */
8837 /* If V had already been marked threadprivate, it doesn't matter
8838 whether it had been used prior to this point. */
8839 if (TREE_CODE (v) != VAR_DECL)
8840 error_at (loc, "%qD is not a variable", v);
8841 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8842 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
8843 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8844 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
8845 else if (TREE_TYPE (v) == error_mark_node)
8847 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8848 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
8849 else
8851 if (! DECL_THREAD_LOCAL_P (v))
8853 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8854 /* If rtl has been already set for this var, call
8855 make_decl_rtl once again, so that encode_section_info
8856 has a chance to look at the new decl flags. */
8857 if (DECL_RTL_SET_P (v))
8858 make_decl_rtl (v);
8860 C_DECL_THREADPRIVATE_P (v) = 1;
8864 c_parser_skip_to_pragma_eol (parser);
8868 /* Parse a single source file. */
8870 void
8871 c_parse_file (void)
8873 /* Use local storage to begin. If the first token is a pragma, parse it.
8874 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8875 which will cause garbage collection. */
8876 c_parser tparser;
8878 memset (&tparser, 0, sizeof tparser);
8879 the_parser = &tparser;
8881 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8882 c_parser_pragma_pch_preprocess (&tparser);
8884 the_parser = ggc_alloc_c_parser ();
8885 *the_parser = tparser;
8887 /* Initialize EH, if we've been told to do so. */
8888 if (flag_exceptions)
8889 using_eh_for_cleanups ();
8891 c_parser_translation_unit (the_parser);
8892 the_parser = NULL;
8895 #include "gt-c-parser.h"