* team.c (gomp_team_end): Free team immediately if it has
[official-gcc.git] / gcc / c-parser.c
blobaba007c9bc2c1b47e33f2a330a69b3004b230d17
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "langhooks.h"
47 #include "input.h"
48 #include "cpplib.h"
49 #include "timevar.h"
50 #include "c-pragma.h"
51 #include "c-tree.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "toplev.h"
55 #include "ggc.h"
56 #include "c-common.h"
57 #include "vec.h"
58 #include "target.h"
59 #include "cgraph.h"
62 /* Initialization routine for this file. */
64 void
65 c_parse_init (void)
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
71 int mask = 0;
73 mask |= D_CXXONLY;
74 if (!flag_isoc99)
75 mask |= D_C99;
76 if (flag_no_asm)
78 mask |= D_ASM | D_EXT;
79 if (!flag_isoc99)
80 mask |= D_EXT89;
82 if (!c_dialect_objc ())
83 mask |= D_OBJC | D_CXX_OBJC;
85 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
86 for (i = 0; i < num_c_common_reswords; i++)
88 /* If a keyword is disabled, do not enter it into the table
89 and so create a canonical spelling that isn't a keyword. */
90 if (c_common_reswords[i].disable & mask)
92 if (warn_cxx_compat
93 && (c_common_reswords[i].disable & D_CXXWARN))
95 id = get_identifier (c_common_reswords[i].word);
96 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
97 C_IS_RESERVED_WORD (id) = 1;
99 continue;
102 id = get_identifier (c_common_reswords[i].word);
103 C_SET_RID_CODE (id, c_common_reswords[i].rid);
104 C_IS_RESERVED_WORD (id) = 1;
105 ridpointers [(int) c_common_reswords[i].rid] = id;
109 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
110 and the C parser. Unlike the C++ lexer, the parser structure
111 stores the lexer information instead of using a separate structure.
112 Identifiers are separated into ordinary identifiers, type names,
113 keywords and some other Objective-C types of identifiers, and some
114 look-ahead is maintained.
116 ??? It might be a good idea to lex the whole file up front (as for
117 C++). It would then be possible to share more of the C and C++
118 lexer code, if desired. */
120 /* The following local token type is used. */
122 /* A keyword. */
123 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
125 /* More information about the type of a CPP_NAME token. */
126 typedef enum c_id_kind {
127 /* An ordinary identifier. */
128 C_ID_ID,
129 /* An identifier declared as a typedef name. */
130 C_ID_TYPENAME,
131 /* An identifier declared as an Objective-C class name. */
132 C_ID_CLASSNAME,
133 /* Not an identifier. */
134 C_ID_NONE
135 } c_id_kind;
137 /* A single C token after string literal concatenation and conversion
138 of preprocessing tokens to tokens. */
139 typedef struct c_token GTY (())
141 /* The kind of token. */
142 ENUM_BITFIELD (cpp_ttype) type : 8;
143 /* If this token is a CPP_NAME, this value indicates whether also
144 declared as some kind of type. Otherwise, it is C_ID_NONE. */
145 ENUM_BITFIELD (c_id_kind) id_kind : 8;
146 /* If this token is a keyword, this value indicates which keyword.
147 Otherwise, this value is RID_MAX. */
148 ENUM_BITFIELD (rid) keyword : 8;
149 /* If this token is a CPP_PRAGMA, this indicates the pragma that
150 was seen. Otherwise it is PRAGMA_NONE. */
151 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
152 /* True if this token is from a system header. */
153 BOOL_BITFIELD in_system_header : 1;
154 /* The value associated with this token, if any. */
155 tree value;
156 /* The location at which this token was found. */
157 location_t location;
158 } c_token;
160 /* A parser structure recording information about the state and
161 context of parsing. Includes lexer information with up to two
162 tokens of look-ahead; more are not needed for C. */
163 typedef struct c_parser GTY(())
165 /* The look-ahead tokens. */
166 c_token tokens[2];
167 /* How many look-ahead tokens are available (0, 1 or 2). */
168 short tokens_avail;
169 /* True if a syntax error is being recovered from; false otherwise.
170 c_parser_error sets this flag. It should clear this flag when
171 enough tokens have been consumed to recover from the error. */
172 BOOL_BITFIELD error : 1;
173 /* True if we're processing a pragma, and shouldn't automatically
174 consume CPP_PRAGMA_EOL. */
175 BOOL_BITFIELD in_pragma : 1;
176 /* True if we're parsing the outermost block of an if statement. */
177 BOOL_BITFIELD in_if_block : 1;
178 /* True if we want to lex an untranslated string. */
179 BOOL_BITFIELD lex_untranslated_string : 1;
180 /* Objective-C specific parser/lexer information. */
181 BOOL_BITFIELD objc_pq_context : 1;
182 /* The following flag is needed to contextualize Objective-C lexical
183 analysis. In some cases (e.g., 'int NSObject;'), it is
184 undesirable to bind an identifier to an Objective-C class, even
185 if a class with that name exists. */
186 BOOL_BITFIELD objc_need_raw_identifier : 1;
187 } c_parser;
190 /* The actual parser and external interface. ??? Does this need to be
191 garbage-collected? */
193 static GTY (()) c_parser *the_parser;
196 /* Read in and lex a single token, storing it in *TOKEN. */
198 static void
199 c_lex_one_token (c_parser *parser, c_token *token)
201 timevar_push (TV_LEX);
203 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
204 (parser->lex_untranslated_string
205 ? C_LEX_STRING_NO_TRANSLATE : 0));
206 token->id_kind = C_ID_NONE;
207 token->keyword = RID_MAX;
208 token->pragma_kind = PRAGMA_NONE;
209 token->in_system_header = in_system_header;
211 switch (token->type)
213 case CPP_NAME:
215 tree decl;
217 bool objc_force_identifier = parser->objc_need_raw_identifier;
218 if (c_dialect_objc ())
219 parser->objc_need_raw_identifier = false;
221 if (C_IS_RESERVED_WORD (token->value))
223 enum rid rid_code = C_RID_CODE (token->value);
225 if (rid_code == RID_CXX_COMPAT_WARN)
227 warning (OPT_Wc___compat,
228 "%Hidentifier %qs conflicts with C++ keyword",
229 &token->location,
230 IDENTIFIER_POINTER (token->value));
232 else if (c_dialect_objc ())
234 if (!objc_is_reserved_word (token->value)
235 && (!OBJC_IS_PQ_KEYWORD (rid_code)
236 || parser->objc_pq_context))
238 /* Return the canonical spelling for this keyword. */
239 token->value = ridpointers[(int) rid_code];
240 token->type = CPP_KEYWORD;
241 token->keyword = rid_code;
242 break;
245 else
247 /* Return the canonical spelling for this keyword. */
248 token->value = ridpointers[(int) rid_code];
249 token->type = CPP_KEYWORD;
250 token->keyword = rid_code;
251 break;
255 decl = lookup_name (token->value);
256 if (decl)
258 if (TREE_CODE (decl) == TYPE_DECL)
260 token->id_kind = C_ID_TYPENAME;
261 break;
264 else if (c_dialect_objc ())
266 tree objc_interface_decl = objc_is_class_name (token->value);
267 /* Objective-C class names are in the same namespace as
268 variables and typedefs, and hence are shadowed by local
269 declarations. */
270 if (objc_interface_decl
271 && (global_bindings_p ()
272 || (!objc_force_identifier && !decl)))
274 token->value = objc_interface_decl;
275 token->id_kind = C_ID_CLASSNAME;
276 break;
279 token->id_kind = C_ID_ID;
281 break;
282 case CPP_AT_NAME:
283 /* This only happens in Objective-C; it must be a keyword. */
284 token->type = CPP_KEYWORD;
285 token->keyword = C_RID_CODE (token->value);
286 break;
287 case CPP_COLON:
288 case CPP_COMMA:
289 case CPP_CLOSE_PAREN:
290 case CPP_SEMICOLON:
291 /* These tokens may affect the interpretation of any identifiers
292 following, if doing Objective-C. */
293 if (c_dialect_objc ())
294 parser->objc_need_raw_identifier = false;
295 break;
296 case CPP_PRAGMA:
297 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
298 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
299 token->value = NULL;
300 break;
301 default:
302 break;
304 timevar_pop (TV_LEX);
307 /* Return a pointer to the next token from PARSER, reading it in if
308 necessary. */
310 static inline c_token *
311 c_parser_peek_token (c_parser *parser)
313 if (parser->tokens_avail == 0)
315 c_lex_one_token (parser, &parser->tokens[0]);
316 parser->tokens_avail = 1;
318 return &parser->tokens[0];
321 /* Return true if the next token from PARSER has the indicated
322 TYPE. */
324 static inline bool
325 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
327 return c_parser_peek_token (parser)->type == type;
330 /* Return true if the next token from PARSER does not have the
331 indicated TYPE. */
333 static inline bool
334 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
336 return !c_parser_next_token_is (parser, type);
339 /* Return true if the next token from PARSER is the indicated
340 KEYWORD. */
342 static inline bool
343 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
345 return c_parser_peek_token (parser)->keyword == keyword;
348 /* Return true if TOKEN can start a type name,
349 false otherwise. */
350 static bool
351 c_token_starts_typename (c_token *token)
353 switch (token->type)
355 case CPP_NAME:
356 switch (token->id_kind)
358 case C_ID_ID:
359 return false;
360 case C_ID_TYPENAME:
361 return true;
362 case C_ID_CLASSNAME:
363 gcc_assert (c_dialect_objc ());
364 return true;
365 default:
366 gcc_unreachable ();
368 case CPP_KEYWORD:
369 switch (token->keyword)
371 case RID_UNSIGNED:
372 case RID_LONG:
373 case RID_SHORT:
374 case RID_SIGNED:
375 case RID_COMPLEX:
376 case RID_INT:
377 case RID_CHAR:
378 case RID_FLOAT:
379 case RID_DOUBLE:
380 case RID_VOID:
381 case RID_DFLOAT32:
382 case RID_DFLOAT64:
383 case RID_DFLOAT128:
384 case RID_BOOL:
385 case RID_ENUM:
386 case RID_STRUCT:
387 case RID_UNION:
388 case RID_TYPEOF:
389 case RID_CONST:
390 case RID_VOLATILE:
391 case RID_RESTRICT:
392 case RID_ATTRIBUTE:
393 case RID_FRACT:
394 case RID_ACCUM:
395 case RID_SAT:
396 return true;
397 default:
398 return false;
400 case CPP_LESS:
401 if (c_dialect_objc ())
402 return true;
403 return false;
404 default:
405 return false;
409 /* Return true if the next token from PARSER can start a type name,
410 false otherwise. */
411 static inline bool
412 c_parser_next_token_starts_typename (c_parser *parser)
414 c_token *token = c_parser_peek_token (parser);
415 return c_token_starts_typename (token);
418 /* Return true if TOKEN can start declaration specifiers, false
419 otherwise. */
420 static bool
421 c_token_starts_declspecs (c_token *token)
423 switch (token->type)
425 case CPP_NAME:
426 switch (token->id_kind)
428 case C_ID_ID:
429 return false;
430 case C_ID_TYPENAME:
431 return true;
432 case C_ID_CLASSNAME:
433 gcc_assert (c_dialect_objc ());
434 return true;
435 default:
436 gcc_unreachable ();
438 case CPP_KEYWORD:
439 switch (token->keyword)
441 case RID_STATIC:
442 case RID_EXTERN:
443 case RID_REGISTER:
444 case RID_TYPEDEF:
445 case RID_INLINE:
446 case RID_AUTO:
447 case RID_THREAD:
448 case RID_UNSIGNED:
449 case RID_LONG:
450 case RID_SHORT:
451 case RID_SIGNED:
452 case RID_COMPLEX:
453 case RID_INT:
454 case RID_CHAR:
455 case RID_FLOAT:
456 case RID_DOUBLE:
457 case RID_VOID:
458 case RID_DFLOAT32:
459 case RID_DFLOAT64:
460 case RID_DFLOAT128:
461 case RID_BOOL:
462 case RID_ENUM:
463 case RID_STRUCT:
464 case RID_UNION:
465 case RID_TYPEOF:
466 case RID_CONST:
467 case RID_VOLATILE:
468 case RID_RESTRICT:
469 case RID_ATTRIBUTE:
470 case RID_FRACT:
471 case RID_ACCUM:
472 case RID_SAT:
473 return true;
474 default:
475 return false;
477 case CPP_LESS:
478 if (c_dialect_objc ())
479 return true;
480 return false;
481 default:
482 return false;
486 /* Return true if the next token from PARSER can start declaration
487 specifiers, false otherwise. */
488 static inline bool
489 c_parser_next_token_starts_declspecs (c_parser *parser)
491 c_token *token = c_parser_peek_token (parser);
492 return c_token_starts_declspecs (token);
495 /* Return a pointer to the next-but-one token from PARSER, reading it
496 in if necessary. The next token is already read in. */
498 static c_token *
499 c_parser_peek_2nd_token (c_parser *parser)
501 if (parser->tokens_avail >= 2)
502 return &parser->tokens[1];
503 gcc_assert (parser->tokens_avail == 1);
504 gcc_assert (parser->tokens[0].type != CPP_EOF);
505 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
506 c_lex_one_token (parser, &parser->tokens[1]);
507 parser->tokens_avail = 2;
508 return &parser->tokens[1];
511 /* Consume the next token from PARSER. */
513 static void
514 c_parser_consume_token (c_parser *parser)
516 gcc_assert (parser->tokens_avail >= 1);
517 gcc_assert (parser->tokens[0].type != CPP_EOF);
518 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
519 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
520 if (parser->tokens_avail == 2)
521 parser->tokens[0] = parser->tokens[1];
522 parser->tokens_avail--;
525 /* Expect the current token to be a #pragma. Consume it and remember
526 that we've begun parsing a pragma. */
528 static void
529 c_parser_consume_pragma (c_parser *parser)
531 gcc_assert (!parser->in_pragma);
532 gcc_assert (parser->tokens_avail >= 1);
533 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
534 if (parser->tokens_avail == 2)
535 parser->tokens[0] = parser->tokens[1];
536 parser->tokens_avail--;
537 parser->in_pragma = true;
540 /* Update the globals input_location and in_system_header from
541 TOKEN. */
542 static inline void
543 c_parser_set_source_position_from_token (c_token *token)
545 if (token->type != CPP_EOF)
547 input_location = token->location;
548 in_system_header = token->in_system_header;
552 /* Issue a diagnostic of the form
553 FILE:LINE: MESSAGE before TOKEN
554 where TOKEN is the next token in the input stream of PARSER.
555 MESSAGE (specified by the caller) is usually of the form "expected
556 OTHER-TOKEN".
558 Do not issue a diagnostic if still recovering from an error.
560 ??? This is taken from the C++ parser, but building up messages in
561 this way is not i18n-friendly and some other approach should be
562 used. */
564 static void
565 c_parser_error (c_parser *parser, const char *gmsgid)
567 c_token *token = c_parser_peek_token (parser);
568 if (parser->error)
569 return;
570 parser->error = true;
571 if (!gmsgid)
572 return;
573 /* This diagnostic makes more sense if it is tagged to the line of
574 the token we just peeked at. */
575 c_parser_set_source_position_from_token (token);
576 c_parse_error (gmsgid,
577 /* Because c_parse_error does not understand
578 CPP_KEYWORD, keywords are treated like
579 identifiers. */
580 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
581 token->value);
584 /* If the next token is of the indicated TYPE, consume it. Otherwise,
585 issue the error MSGID. If MSGID is NULL then a message has already
586 been produced and no message will be produced this time. Returns
587 true if found, false otherwise. */
589 static bool
590 c_parser_require (c_parser *parser,
591 enum cpp_ttype type,
592 const char *msgid)
594 if (c_parser_next_token_is (parser, type))
596 c_parser_consume_token (parser);
597 return true;
599 else
601 c_parser_error (parser, msgid);
602 return false;
606 /* If the next token is the indicated keyword, consume it. Otherwise,
607 issue the error MSGID. Returns true if found, false otherwise. */
609 static bool
610 c_parser_require_keyword (c_parser *parser,
611 enum rid keyword,
612 const char *msgid)
614 if (c_parser_next_token_is_keyword (parser, keyword))
616 c_parser_consume_token (parser);
617 return true;
619 else
621 c_parser_error (parser, msgid);
622 return false;
626 /* Like c_parser_require, except that tokens will be skipped until the
627 desired token is found. An error message is still produced if the
628 next token is not as expected. If MSGID is NULL then a message has
629 already been produced and no message will be produced this
630 time. */
632 static void
633 c_parser_skip_until_found (c_parser *parser,
634 enum cpp_ttype type,
635 const char *msgid)
637 unsigned nesting_depth = 0;
639 if (c_parser_require (parser, type, msgid))
640 return;
642 /* Skip tokens until the desired token is found. */
643 while (true)
645 /* Peek at the next token. */
646 c_token *token = c_parser_peek_token (parser);
647 /* If we've reached the token we want, consume it and stop. */
648 if (token->type == type && !nesting_depth)
650 c_parser_consume_token (parser);
651 break;
654 /* If we've run out of tokens, stop. */
655 if (token->type == CPP_EOF)
656 return;
657 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
658 return;
659 if (token->type == CPP_OPEN_BRACE
660 || token->type == CPP_OPEN_PAREN
661 || token->type == CPP_OPEN_SQUARE)
662 ++nesting_depth;
663 else if (token->type == CPP_CLOSE_BRACE
664 || token->type == CPP_CLOSE_PAREN
665 || token->type == CPP_CLOSE_SQUARE)
667 if (nesting_depth-- == 0)
668 break;
670 /* Consume this token. */
671 c_parser_consume_token (parser);
673 parser->error = false;
676 /* Skip tokens until the end of a parameter is found, but do not
677 consume the comma, semicolon or closing delimiter. */
679 static void
680 c_parser_skip_to_end_of_parameter (c_parser *parser)
682 unsigned nesting_depth = 0;
684 while (true)
686 c_token *token = c_parser_peek_token (parser);
687 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
688 && !nesting_depth)
689 break;
690 /* If we've run out of tokens, stop. */
691 if (token->type == CPP_EOF)
692 return;
693 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
694 return;
695 if (token->type == CPP_OPEN_BRACE
696 || token->type == CPP_OPEN_PAREN
697 || token->type == CPP_OPEN_SQUARE)
698 ++nesting_depth;
699 else if (token->type == CPP_CLOSE_BRACE
700 || token->type == CPP_CLOSE_PAREN
701 || token->type == CPP_CLOSE_SQUARE)
703 if (nesting_depth-- == 0)
704 break;
706 /* Consume this token. */
707 c_parser_consume_token (parser);
709 parser->error = false;
712 /* Expect to be at the end of the pragma directive and consume an
713 end of line marker. */
715 static void
716 c_parser_skip_to_pragma_eol (c_parser *parser)
718 gcc_assert (parser->in_pragma);
719 parser->in_pragma = false;
721 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
722 while (true)
724 c_token *token = c_parser_peek_token (parser);
725 if (token->type == CPP_EOF)
726 break;
727 if (token->type == CPP_PRAGMA_EOL)
729 c_parser_consume_token (parser);
730 break;
732 c_parser_consume_token (parser);
735 parser->error = false;
738 /* Skip tokens until we have consumed an entire block, or until we
739 have consumed a non-nested ';'. */
741 static void
742 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
744 unsigned nesting_depth = 0;
745 bool save_error = parser->error;
747 while (true)
749 c_token *token;
751 /* Peek at the next token. */
752 token = c_parser_peek_token (parser);
754 switch (token->type)
756 case CPP_EOF:
757 return;
759 case CPP_PRAGMA_EOL:
760 if (parser->in_pragma)
761 return;
762 break;
764 case CPP_SEMICOLON:
765 /* If the next token is a ';', we have reached the
766 end of the statement. */
767 if (!nesting_depth)
769 /* Consume the ';'. */
770 c_parser_consume_token (parser);
771 goto finished;
773 break;
775 case CPP_CLOSE_BRACE:
776 /* If the next token is a non-nested '}', then we have
777 reached the end of the current block. */
778 if (nesting_depth == 0 || --nesting_depth == 0)
780 c_parser_consume_token (parser);
781 goto finished;
783 break;
785 case CPP_OPEN_BRACE:
786 /* If it the next token is a '{', then we are entering a new
787 block. Consume the entire block. */
788 ++nesting_depth;
789 break;
791 case CPP_PRAGMA:
792 /* If we see a pragma, consume the whole thing at once. We
793 have some safeguards against consuming pragmas willy-nilly.
794 Normally, we'd expect to be here with parser->error set,
795 which disables these safeguards. But it's possible to get
796 here for secondary error recovery, after parser->error has
797 been cleared. */
798 c_parser_consume_pragma (parser);
799 c_parser_skip_to_pragma_eol (parser);
800 parser->error = save_error;
801 continue;
803 default:
804 break;
807 c_parser_consume_token (parser);
810 finished:
811 parser->error = false;
814 /* Save the warning flags which are controlled by __extension__. */
816 static inline int
817 disable_extension_diagnostics (void)
819 int ret = (pedantic
820 | (warn_pointer_arith << 1)
821 | (warn_traditional << 2)
822 | (flag_iso << 3));
823 pedantic = 0;
824 warn_pointer_arith = 0;
825 warn_traditional = 0;
826 flag_iso = 0;
827 return ret;
830 /* Restore the warning flags which are controlled by __extension__.
831 FLAGS is the return value from disable_extension_diagnostics. */
833 static inline void
834 restore_extension_diagnostics (int flags)
836 pedantic = flags & 1;
837 warn_pointer_arith = (flags >> 1) & 1;
838 warn_traditional = (flags >> 2) & 1;
839 flag_iso = (flags >> 3) & 1;
842 /* Possibly kinds of declarator to parse. */
843 typedef enum c_dtr_syn {
844 /* A normal declarator with an identifier. */
845 C_DTR_NORMAL,
846 /* An abstract declarator (maybe empty). */
847 C_DTR_ABSTRACT,
848 /* A parameter declarator: may be either, but after a type name does
849 not redeclare a typedef name as an identifier if it can
850 alternatively be interpreted as a typedef name; see DR#009,
851 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
852 following DR#249. For example, given a typedef T, "int T" and
853 "int *T" are valid parameter declarations redeclaring T, while
854 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
855 abstract declarators rather than involving redundant parentheses;
856 the same applies with attributes inside the parentheses before
857 "T". */
858 C_DTR_PARM
859 } c_dtr_syn;
861 static void c_parser_external_declaration (c_parser *);
862 static void c_parser_asm_definition (c_parser *);
863 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
864 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
865 bool);
866 static struct c_typespec c_parser_enum_specifier (c_parser *);
867 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
868 static tree c_parser_struct_declaration (c_parser *);
869 static struct c_typespec c_parser_typeof_specifier (c_parser *);
870 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
871 bool *);
872 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
873 c_dtr_syn, bool *);
874 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
875 bool,
876 struct c_declarator *);
877 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
878 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
879 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
880 static tree c_parser_simple_asm_expr (c_parser *);
881 static tree c_parser_attributes (c_parser *);
882 static struct c_type_name *c_parser_type_name (c_parser *);
883 static struct c_expr c_parser_initializer (c_parser *);
884 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
885 static void c_parser_initelt (c_parser *);
886 static void c_parser_initval (c_parser *, struct c_expr *);
887 static tree c_parser_compound_statement (c_parser *);
888 static void c_parser_compound_statement_nostart (c_parser *);
889 static void c_parser_label (c_parser *);
890 static void c_parser_statement (c_parser *);
891 static void c_parser_statement_after_labels (c_parser *);
892 static void c_parser_if_statement (c_parser *);
893 static void c_parser_switch_statement (c_parser *);
894 static void c_parser_while_statement (c_parser *);
895 static void c_parser_do_statement (c_parser *);
896 static void c_parser_for_statement (c_parser *);
897 static tree c_parser_asm_statement (c_parser *);
898 static tree c_parser_asm_operands (c_parser *, bool);
899 static tree c_parser_asm_clobbers (c_parser *);
900 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
901 static struct c_expr c_parser_conditional_expression (c_parser *,
902 struct c_expr *);
903 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
904 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
905 static struct c_expr c_parser_unary_expression (c_parser *);
906 static struct c_expr c_parser_sizeof_expression (c_parser *);
907 static struct c_expr c_parser_alignof_expression (c_parser *);
908 static struct c_expr c_parser_postfix_expression (c_parser *);
909 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
910 struct c_type_name *);
911 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
912 struct c_expr);
913 static struct c_expr c_parser_expression (c_parser *);
914 static struct c_expr c_parser_expression_conv (c_parser *);
915 static tree c_parser_expr_list (c_parser *, bool);
916 static void c_parser_omp_construct (c_parser *);
917 static void c_parser_omp_threadprivate (c_parser *);
918 static void c_parser_omp_barrier (c_parser *);
919 static void c_parser_omp_flush (c_parser *);
920 static void c_parser_omp_taskwait (c_parser *);
922 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
923 static bool c_parser_pragma (c_parser *, enum pragma_context);
925 /* These Objective-C parser functions are only ever called when
926 compiling Objective-C. */
927 static void c_parser_objc_class_definition (c_parser *);
928 static void c_parser_objc_class_instance_variables (c_parser *);
929 static void c_parser_objc_class_declaration (c_parser *);
930 static void c_parser_objc_alias_declaration (c_parser *);
931 static void c_parser_objc_protocol_definition (c_parser *);
932 static enum tree_code c_parser_objc_method_type (c_parser *);
933 static void c_parser_objc_method_definition (c_parser *);
934 static void c_parser_objc_methodprotolist (c_parser *);
935 static void c_parser_objc_methodproto (c_parser *);
936 static tree c_parser_objc_method_decl (c_parser *);
937 static tree c_parser_objc_type_name (c_parser *);
938 static tree c_parser_objc_protocol_refs (c_parser *);
939 static void c_parser_objc_try_catch_statement (c_parser *);
940 static void c_parser_objc_synchronized_statement (c_parser *);
941 static tree c_parser_objc_selector (c_parser *);
942 static tree c_parser_objc_selector_arg (c_parser *);
943 static tree c_parser_objc_receiver (c_parser *);
944 static tree c_parser_objc_message_args (c_parser *);
945 static tree c_parser_objc_keywordexpr (c_parser *);
947 /* Parse a translation unit (C90 6.7, C99 6.9).
949 translation-unit:
950 external-declarations
952 external-declarations:
953 external-declaration
954 external-declarations external-declaration
956 GNU extensions:
958 translation-unit:
959 empty
962 static void
963 c_parser_translation_unit (c_parser *parser)
965 if (c_parser_next_token_is (parser, CPP_EOF))
967 if (pedantic)
968 pedwarn ("%HISO C forbids an empty translation unit",
969 &c_parser_peek_token (parser)->location);
971 else
973 void *obstack_position = obstack_alloc (&parser_obstack, 0);
976 ggc_collect ();
977 c_parser_external_declaration (parser);
978 obstack_free (&parser_obstack, obstack_position);
980 while (c_parser_next_token_is_not (parser, CPP_EOF));
984 /* Parse an external declaration (C90 6.7, C99 6.9).
986 external-declaration:
987 function-definition
988 declaration
990 GNU extensions:
992 external-declaration:
993 asm-definition
995 __extension__ external-declaration
997 Objective-C:
999 external-declaration:
1000 objc-class-definition
1001 objc-class-declaration
1002 objc-alias-declaration
1003 objc-protocol-definition
1004 objc-method-definition
1005 @end
1008 static void
1009 c_parser_external_declaration (c_parser *parser)
1011 int ext;
1012 switch (c_parser_peek_token (parser)->type)
1014 case CPP_KEYWORD:
1015 switch (c_parser_peek_token (parser)->keyword)
1017 case RID_EXTENSION:
1018 ext = disable_extension_diagnostics ();
1019 c_parser_consume_token (parser);
1020 c_parser_external_declaration (parser);
1021 restore_extension_diagnostics (ext);
1022 break;
1023 case RID_ASM:
1024 c_parser_asm_definition (parser);
1025 break;
1026 case RID_AT_INTERFACE:
1027 case RID_AT_IMPLEMENTATION:
1028 gcc_assert (c_dialect_objc ());
1029 c_parser_objc_class_definition (parser);
1030 break;
1031 case RID_CLASS:
1032 gcc_assert (c_dialect_objc ());
1033 c_parser_objc_class_declaration (parser);
1034 break;
1035 case RID_AT_ALIAS:
1036 gcc_assert (c_dialect_objc ());
1037 c_parser_objc_alias_declaration (parser);
1038 break;
1039 case RID_AT_PROTOCOL:
1040 gcc_assert (c_dialect_objc ());
1041 c_parser_objc_protocol_definition (parser);
1042 break;
1043 case RID_AT_END:
1044 gcc_assert (c_dialect_objc ());
1045 c_parser_consume_token (parser);
1046 objc_finish_implementation ();
1047 break;
1048 default:
1049 goto decl_or_fndef;
1051 break;
1052 case CPP_SEMICOLON:
1053 if (pedantic)
1054 pedwarn ("%HISO C does not allow extra %<;%> outside of a function",
1055 &c_parser_peek_token (parser)->location);
1056 c_parser_consume_token (parser);
1057 break;
1058 case CPP_PRAGMA:
1059 c_parser_pragma (parser, pragma_external);
1060 break;
1061 case CPP_PLUS:
1062 case CPP_MINUS:
1063 if (c_dialect_objc ())
1065 c_parser_objc_method_definition (parser);
1066 break;
1068 /* Else fall through, and yield a syntax error trying to parse
1069 as a declaration or function definition. */
1070 default:
1071 decl_or_fndef:
1072 /* A declaration or a function definition. We can only tell
1073 which after parsing the declaration specifiers, if any, and
1074 the first declarator. */
1075 c_parser_declaration_or_fndef (parser, true, true, false, true);
1076 break;
1081 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1082 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1083 accepted; otherwise (old-style parameter declarations) only other
1084 declarations are accepted. If NESTED is true, we are inside a
1085 function or parsing old-style parameter declarations; any functions
1086 encountered are nested functions and declaration specifiers are
1087 required; otherwise we are at top level and functions are normal
1088 functions and declaration specifiers may be optional. If EMPTY_OK
1089 is true, empty declarations are OK (subject to all other
1090 constraints); otherwise (old-style parameter declarations) they are
1091 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1092 may start with attributes; otherwise they may not.
1094 declaration:
1095 declaration-specifiers init-declarator-list[opt] ;
1097 function-definition:
1098 declaration-specifiers[opt] declarator declaration-list[opt]
1099 compound-statement
1101 declaration-list:
1102 declaration
1103 declaration-list declaration
1105 init-declarator-list:
1106 init-declarator
1107 init-declarator-list , init-declarator
1109 init-declarator:
1110 declarator simple-asm-expr[opt] attributes[opt]
1111 declarator simple-asm-expr[opt] attributes[opt] = initializer
1113 GNU extensions:
1115 nested-function-definition:
1116 declaration-specifiers declarator declaration-list[opt]
1117 compound-statement
1119 The simple-asm-expr and attributes are GNU extensions.
1121 This function does not handle __extension__; that is handled in its
1122 callers. ??? Following the old parser, __extension__ may start
1123 external declarations, declarations in functions and declarations
1124 at the start of "for" loops, but not old-style parameter
1125 declarations.
1127 C99 requires declaration specifiers in a function definition; the
1128 absence is diagnosed through the diagnosis of implicit int. In GNU
1129 C we also allow but diagnose declarations without declaration
1130 specifiers, but only at top level (elsewhere they conflict with
1131 other syntax).
1133 OpenMP:
1135 declaration:
1136 threadprivate-directive */
1138 static void
1139 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1140 bool nested, bool start_attr_ok)
1142 struct c_declspecs *specs;
1143 tree prefix_attrs;
1144 tree all_prefix_attrs;
1145 bool diagnosed_no_specs = false;
1146 location_t here = c_parser_peek_token (parser)->location;
1148 specs = build_null_declspecs ();
1149 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1150 if (parser->error)
1152 c_parser_skip_to_end_of_block_or_statement (parser);
1153 return;
1155 if (nested && !specs->declspecs_seen_p)
1157 c_parser_error (parser, "expected declaration specifiers");
1158 c_parser_skip_to_end_of_block_or_statement (parser);
1159 return;
1161 finish_declspecs (specs);
1162 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1164 if (empty_ok)
1165 shadow_tag (specs);
1166 else
1168 shadow_tag_warned (specs, 1);
1169 pedwarn ("%Hempty declaration", &here);
1171 c_parser_consume_token (parser);
1172 return;
1174 pending_xref_error ();
1175 prefix_attrs = specs->attrs;
1176 all_prefix_attrs = prefix_attrs;
1177 specs->attrs = NULL_TREE;
1178 while (true)
1180 struct c_declarator *declarator;
1181 bool dummy = false;
1182 tree fnbody;
1183 /* Declaring either one or more declarators (in which case we
1184 should diagnose if there were no declaration specifiers) or a
1185 function definition (in which case the diagnostic for
1186 implicit int suffices). */
1187 declarator = c_parser_declarator (parser, specs->type_seen_p,
1188 C_DTR_NORMAL, &dummy);
1189 if (declarator == NULL)
1191 c_parser_skip_to_end_of_block_or_statement (parser);
1192 return;
1194 if (c_parser_next_token_is (parser, CPP_EQ)
1195 || c_parser_next_token_is (parser, CPP_COMMA)
1196 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1197 || c_parser_next_token_is_keyword (parser, RID_ASM)
1198 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1200 tree asm_name = NULL_TREE;
1201 tree postfix_attrs = NULL_TREE;
1202 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1204 diagnosed_no_specs = true;
1205 pedwarn ("%Hdata definition has no type or storage class",
1206 &here);
1208 /* Having seen a data definition, there cannot now be a
1209 function definition. */
1210 fndef_ok = false;
1211 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1212 asm_name = c_parser_simple_asm_expr (parser);
1213 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1214 postfix_attrs = c_parser_attributes (parser);
1215 if (c_parser_next_token_is (parser, CPP_EQ))
1217 tree d;
1218 struct c_expr init;
1219 c_parser_consume_token (parser);
1220 /* The declaration of the variable is in effect while
1221 its initializer is parsed. */
1222 d = start_decl (declarator, specs, true,
1223 chainon (postfix_attrs, all_prefix_attrs));
1224 if (!d)
1225 d = error_mark_node;
1226 start_init (d, asm_name, global_bindings_p ());
1227 init = c_parser_initializer (parser);
1228 finish_init ();
1229 if (d != error_mark_node)
1231 maybe_warn_string_init (TREE_TYPE (d), init);
1232 finish_decl (d, init.value, asm_name);
1235 else
1237 tree d = start_decl (declarator, specs, false,
1238 chainon (postfix_attrs,
1239 all_prefix_attrs));
1240 if (d)
1241 finish_decl (d, NULL_TREE, asm_name);
1243 if (c_parser_next_token_is (parser, CPP_COMMA))
1245 c_parser_consume_token (parser);
1246 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1247 all_prefix_attrs = chainon (c_parser_attributes (parser),
1248 prefix_attrs);
1249 else
1250 all_prefix_attrs = prefix_attrs;
1251 continue;
1253 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1255 c_parser_consume_token (parser);
1256 return;
1258 else
1260 c_parser_error (parser, "expected %<,%> or %<;%>");
1261 c_parser_skip_to_end_of_block_or_statement (parser);
1262 return;
1265 else if (!fndef_ok)
1267 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1268 "%<asm%> or %<__attribute__%>");
1269 c_parser_skip_to_end_of_block_or_statement (parser);
1270 return;
1272 /* Function definition (nested or otherwise). */
1273 if (nested)
1275 if (pedantic)
1276 pedwarn ("%HISO C forbids nested functions", &here);
1277 c_push_function_context ();
1279 if (!start_function (specs, declarator, all_prefix_attrs))
1281 /* This can appear in many cases looking nothing like a
1282 function definition, so we don't give a more specific
1283 error suggesting there was one. */
1284 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1285 "or %<__attribute__%>");
1286 if (nested)
1287 c_pop_function_context ();
1288 break;
1290 /* Parse old-style parameter declarations. ??? Attributes are
1291 not allowed to start declaration specifiers here because of a
1292 syntax conflict between a function declaration with attribute
1293 suffix and a function definition with an attribute prefix on
1294 first old-style parameter declaration. Following the old
1295 parser, they are not accepted on subsequent old-style
1296 parameter declarations either. However, there is no
1297 ambiguity after the first declaration, nor indeed on the
1298 first as long as we don't allow postfix attributes after a
1299 declarator with a nonempty identifier list in a definition;
1300 and postfix attributes have never been accepted here in
1301 function definitions either. */
1302 while (c_parser_next_token_is_not (parser, CPP_EOF)
1303 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1304 c_parser_declaration_or_fndef (parser, false, false, true, false);
1305 DECL_SOURCE_LOCATION (current_function_decl)
1306 = c_parser_peek_token (parser)->location;
1307 store_parm_decls ();
1308 fnbody = c_parser_compound_statement (parser);
1309 if (nested)
1311 tree decl = current_function_decl;
1312 add_stmt (fnbody);
1313 finish_function ();
1314 c_pop_function_context ();
1315 add_stmt (build_stmt (DECL_EXPR, decl));
1317 else
1319 add_stmt (fnbody);
1320 finish_function ();
1322 break;
1326 /* Parse an asm-definition (asm() outside a function body). This is a
1327 GNU extension.
1329 asm-definition:
1330 simple-asm-expr ;
1333 static void
1334 c_parser_asm_definition (c_parser *parser)
1336 tree asm_str = c_parser_simple_asm_expr (parser);
1337 if (asm_str)
1338 cgraph_add_asm_node (asm_str);
1339 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1342 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1343 6.7), adding them to SPECS (which may already include some).
1344 Storage class specifiers are accepted iff SCSPEC_OK; type
1345 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1346 the start iff START_ATTR_OK.
1348 declaration-specifiers:
1349 storage-class-specifier declaration-specifiers[opt]
1350 type-specifier declaration-specifiers[opt]
1351 type-qualifier declaration-specifiers[opt]
1352 function-specifier declaration-specifiers[opt]
1354 Function specifiers (inline) are from C99, and are currently
1355 handled as storage class specifiers, as is __thread.
1357 C90 6.5.1, C99 6.7.1:
1358 storage-class-specifier:
1359 typedef
1360 extern
1361 static
1362 auto
1363 register
1365 C99 6.7.4:
1366 function-specifier:
1367 inline
1369 C90 6.5.2, C99 6.7.2:
1370 type-specifier:
1371 void
1372 char
1373 short
1375 long
1376 float
1377 double
1378 signed
1379 unsigned
1380 _Bool
1381 _Complex
1382 [_Imaginary removed in C99 TC2]
1383 struct-or-union-specifier
1384 enum-specifier
1385 typedef-name
1387 (_Bool and _Complex are new in C99.)
1389 C90 6.5.3, C99 6.7.3:
1391 type-qualifier:
1392 const
1393 restrict
1394 volatile
1396 (restrict is new in C99.)
1398 GNU extensions:
1400 declaration-specifiers:
1401 attributes declaration-specifiers[opt]
1403 storage-class-specifier:
1404 __thread
1406 type-specifier:
1407 typeof-specifier
1408 _Decimal32
1409 _Decimal64
1410 _Decimal128
1411 _Fract
1412 _Accum
1413 _Sat
1415 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1416 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1418 Objective-C:
1420 type-specifier:
1421 class-name objc-protocol-refs[opt]
1422 typedef-name objc-protocol-refs
1423 objc-protocol-refs
1426 static void
1427 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1428 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1430 bool attrs_ok = start_attr_ok;
1431 bool seen_type = specs->type_seen_p;
1432 while (c_parser_next_token_is (parser, CPP_NAME)
1433 || c_parser_next_token_is (parser, CPP_KEYWORD)
1434 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1436 struct c_typespec t;
1437 tree attrs;
1438 if (c_parser_next_token_is (parser, CPP_NAME))
1440 tree value = c_parser_peek_token (parser)->value;
1441 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1442 /* This finishes the specifiers unless a type name is OK, it
1443 is declared as a type name and a type name hasn't yet
1444 been seen. */
1445 if (!typespec_ok || seen_type
1446 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1447 break;
1448 c_parser_consume_token (parser);
1449 seen_type = true;
1450 attrs_ok = true;
1451 if (kind == C_ID_TYPENAME
1452 && (!c_dialect_objc ()
1453 || c_parser_next_token_is_not (parser, CPP_LESS)))
1455 t.kind = ctsk_typedef;
1456 /* For a typedef name, record the meaning, not the name.
1457 In case of 'foo foo, bar;'. */
1458 t.spec = lookup_name (value);
1460 else
1462 tree proto = NULL_TREE;
1463 gcc_assert (c_dialect_objc ());
1464 t.kind = ctsk_objc;
1465 if (c_parser_next_token_is (parser, CPP_LESS))
1466 proto = c_parser_objc_protocol_refs (parser);
1467 t.spec = objc_get_protocol_qualified_type (value, proto);
1469 declspecs_add_type (specs, t);
1470 continue;
1472 if (c_parser_next_token_is (parser, CPP_LESS))
1474 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1475 nisse@lysator.liu.se. */
1476 tree proto;
1477 gcc_assert (c_dialect_objc ());
1478 if (!typespec_ok || seen_type)
1479 break;
1480 proto = c_parser_objc_protocol_refs (parser);
1481 t.kind = ctsk_objc;
1482 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1483 declspecs_add_type (specs, t);
1484 continue;
1486 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1487 switch (c_parser_peek_token (parser)->keyword)
1489 case RID_STATIC:
1490 case RID_EXTERN:
1491 case RID_REGISTER:
1492 case RID_TYPEDEF:
1493 case RID_INLINE:
1494 case RID_AUTO:
1495 case RID_THREAD:
1496 if (!scspec_ok)
1497 goto out;
1498 attrs_ok = true;
1499 /* TODO: Distinguish between function specifiers (inline)
1500 and storage class specifiers, either here or in
1501 declspecs_add_scspec. */
1502 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1503 c_parser_consume_token (parser);
1504 break;
1505 case RID_UNSIGNED:
1506 case RID_LONG:
1507 case RID_SHORT:
1508 case RID_SIGNED:
1509 case RID_COMPLEX:
1510 case RID_INT:
1511 case RID_CHAR:
1512 case RID_FLOAT:
1513 case RID_DOUBLE:
1514 case RID_VOID:
1515 case RID_DFLOAT32:
1516 case RID_DFLOAT64:
1517 case RID_DFLOAT128:
1518 case RID_BOOL:
1519 case RID_FRACT:
1520 case RID_ACCUM:
1521 case RID_SAT:
1522 if (!typespec_ok)
1523 goto out;
1524 attrs_ok = true;
1525 seen_type = true;
1526 if (c_dialect_objc ())
1527 parser->objc_need_raw_identifier = true;
1528 t.kind = ctsk_resword;
1529 t.spec = c_parser_peek_token (parser)->value;
1530 declspecs_add_type (specs, t);
1531 c_parser_consume_token (parser);
1532 break;
1533 case RID_ENUM:
1534 if (!typespec_ok)
1535 goto out;
1536 attrs_ok = true;
1537 seen_type = true;
1538 t = c_parser_enum_specifier (parser);
1539 declspecs_add_type (specs, t);
1540 break;
1541 case RID_STRUCT:
1542 case RID_UNION:
1543 if (!typespec_ok)
1544 goto out;
1545 attrs_ok = true;
1546 seen_type = true;
1547 t = c_parser_struct_or_union_specifier (parser);
1548 declspecs_add_type (specs, t);
1549 break;
1550 case RID_TYPEOF:
1551 /* ??? The old parser rejected typeof after other type
1552 specifiers, but is a syntax error the best way of
1553 handling this? */
1554 if (!typespec_ok || seen_type)
1555 goto out;
1556 attrs_ok = true;
1557 seen_type = true;
1558 t = c_parser_typeof_specifier (parser);
1559 declspecs_add_type (specs, t);
1560 break;
1561 case RID_CONST:
1562 case RID_VOLATILE:
1563 case RID_RESTRICT:
1564 attrs_ok = true;
1565 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1566 c_parser_consume_token (parser);
1567 break;
1568 case RID_ATTRIBUTE:
1569 if (!attrs_ok)
1570 goto out;
1571 attrs = c_parser_attributes (parser);
1572 declspecs_add_attrs (specs, attrs);
1573 break;
1574 default:
1575 goto out;
1578 out: ;
1581 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1583 enum-specifier:
1584 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1585 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1586 enum attributes[opt] identifier
1588 The form with trailing comma is new in C99. The forms with
1589 attributes are GNU extensions. In GNU C, we accept any expression
1590 without commas in the syntax (assignment expressions, not just
1591 conditional expressions); assignment expressions will be diagnosed
1592 as non-constant.
1594 enumerator-list:
1595 enumerator
1596 enumerator-list , enumerator
1598 enumerator:
1599 enumeration-constant
1600 enumeration-constant = constant-expression
1603 static struct c_typespec
1604 c_parser_enum_specifier (c_parser *parser)
1606 struct c_typespec ret;
1607 tree attrs;
1608 tree ident = NULL_TREE;
1609 location_t ident_loc;
1610 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1611 c_parser_consume_token (parser);
1612 attrs = c_parser_attributes (parser);
1613 /* Set the location in case we create a decl now. */
1614 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1615 if (c_parser_next_token_is (parser, CPP_NAME))
1617 ident = c_parser_peek_token (parser)->value;
1618 ident_loc = c_parser_peek_token (parser)->location;
1619 c_parser_consume_token (parser);
1621 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1623 /* Parse an enum definition. */
1624 struct c_enum_contents the_enum;
1625 tree type = start_enum (&the_enum, ident);
1626 tree postfix_attrs;
1627 /* We chain the enumerators in reverse order, then put them in
1628 forward order at the end. */
1629 tree values = NULL_TREE;
1630 c_parser_consume_token (parser);
1631 while (true)
1633 tree enum_id;
1634 tree enum_value;
1635 tree enum_decl;
1636 bool seen_comma;
1637 c_token *token;
1638 location_t comma_loc;
1639 if (c_parser_next_token_is_not (parser, CPP_NAME))
1641 c_parser_error (parser, "expected identifier");
1642 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1643 values = error_mark_node;
1644 break;
1646 token = c_parser_peek_token (parser);
1647 enum_id = token->value;
1648 /* Set the location in case we create a decl now. */
1649 c_parser_set_source_position_from_token (token);
1650 c_parser_consume_token (parser);
1651 if (c_parser_next_token_is (parser, CPP_EQ))
1653 c_parser_consume_token (parser);
1654 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1656 else
1657 enum_value = NULL_TREE;
1658 enum_decl = build_enumerator (&the_enum, enum_id, enum_value);
1659 TREE_CHAIN (enum_decl) = values;
1660 values = enum_decl;
1661 seen_comma = false;
1662 if (c_parser_next_token_is (parser, CPP_COMMA))
1664 comma_loc = c_parser_peek_token (parser)->location;
1665 seen_comma = true;
1666 c_parser_consume_token (parser);
1668 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1670 if (seen_comma && pedantic && !flag_isoc99)
1671 pedwarn ("%Hcomma at end of enumerator list", &comma_loc);
1672 c_parser_consume_token (parser);
1673 break;
1675 if (!seen_comma)
1677 c_parser_error (parser, "expected %<,%> or %<}%>");
1678 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1679 values = error_mark_node;
1680 break;
1683 postfix_attrs = c_parser_attributes (parser);
1684 ret.spec = finish_enum (type, nreverse (values),
1685 chainon (attrs, postfix_attrs));
1686 ret.kind = ctsk_tagdef;
1687 return ret;
1689 else if (!ident)
1691 c_parser_error (parser, "expected %<{%>");
1692 ret.spec = error_mark_node;
1693 ret.kind = ctsk_tagref;
1694 return ret;
1696 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1697 /* In ISO C, enumerated types can be referred to only if already
1698 defined. */
1699 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1701 gcc_assert (ident);
1702 pedwarn ("%HISO C forbids forward references to %<enum%> types",
1703 &ident_loc);
1705 return ret;
1708 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1710 struct-or-union-specifier:
1711 struct-or-union attributes[opt] identifier[opt]
1712 { struct-contents } attributes[opt]
1713 struct-or-union attributes[opt] identifier
1715 struct-contents:
1716 struct-declaration-list
1718 struct-declaration-list:
1719 struct-declaration ;
1720 struct-declaration-list struct-declaration ;
1722 GNU extensions:
1724 struct-contents:
1725 empty
1726 struct-declaration
1727 struct-declaration-list struct-declaration
1729 struct-declaration-list:
1730 struct-declaration-list ;
1733 (Note that in the syntax here, unlike that in ISO C, the semicolons
1734 are included here rather than in struct-declaration, in order to
1735 describe the syntax with extra semicolons and missing semicolon at
1736 end.)
1738 Objective-C:
1740 struct-declaration-list:
1741 @defs ( class-name )
1743 (Note this does not include a trailing semicolon, but can be
1744 followed by further declarations, and gets a pedwarn-if-pedantic
1745 when followed by a semicolon.) */
1747 static struct c_typespec
1748 c_parser_struct_or_union_specifier (c_parser *parser)
1750 struct c_typespec ret;
1751 tree attrs;
1752 tree ident = NULL_TREE;
1753 enum tree_code code;
1754 switch (c_parser_peek_token (parser)->keyword)
1756 case RID_STRUCT:
1757 code = RECORD_TYPE;
1758 break;
1759 case RID_UNION:
1760 code = UNION_TYPE;
1761 break;
1762 default:
1763 gcc_unreachable ();
1765 c_parser_consume_token (parser);
1766 attrs = c_parser_attributes (parser);
1767 /* Set the location in case we create a decl now. */
1768 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1769 if (c_parser_next_token_is (parser, CPP_NAME))
1771 ident = c_parser_peek_token (parser)->value;
1772 c_parser_consume_token (parser);
1774 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1776 /* Parse a struct or union definition. Start the scope of the
1777 tag before parsing components. */
1778 tree type = start_struct (code, ident);
1779 tree postfix_attrs;
1780 /* We chain the components in reverse order, then put them in
1781 forward order at the end. Each struct-declaration may
1782 declare multiple components (comma-separated), so we must use
1783 chainon to join them, although when parsing each
1784 struct-declaration we can use TREE_CHAIN directly.
1786 The theory behind all this is that there will be more
1787 semicolon separated fields than comma separated fields, and
1788 so we'll be minimizing the number of node traversals required
1789 by chainon. */
1790 tree contents = NULL_TREE;
1791 c_parser_consume_token (parser);
1792 /* Handle the Objective-C @defs construct,
1793 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1794 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1796 tree name;
1797 gcc_assert (c_dialect_objc ());
1798 c_parser_consume_token (parser);
1799 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1800 goto end_at_defs;
1801 if (c_parser_next_token_is (parser, CPP_NAME)
1802 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1804 name = c_parser_peek_token (parser)->value;
1805 c_parser_consume_token (parser);
1807 else
1809 c_parser_error (parser, "expected class name");
1810 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1811 goto end_at_defs;
1813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1814 "expected %<)%>");
1815 contents = nreverse (objc_get_class_ivars (name));
1817 end_at_defs:
1818 /* Parse the struct-declarations and semicolons. Problems with
1819 semicolons are diagnosed here; empty structures are diagnosed
1820 elsewhere. */
1821 while (true)
1823 tree decls;
1824 /* Parse any stray semicolon. */
1825 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1827 if (pedantic)
1828 pedwarn ("%Hextra semicolon in struct or union specified",
1829 &c_parser_peek_token (parser)->location);
1830 c_parser_consume_token (parser);
1831 continue;
1833 /* Stop if at the end of the struct or union contents. */
1834 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1836 c_parser_consume_token (parser);
1837 break;
1839 /* Accept #pragmas at struct scope. */
1840 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1842 c_parser_pragma (parser, pragma_external);
1843 continue;
1845 /* Parse some comma-separated declarations, but not the
1846 trailing semicolon if any. */
1847 decls = c_parser_struct_declaration (parser);
1848 contents = chainon (decls, contents);
1849 /* If no semicolon follows, either we have a parse error or
1850 are at the end of the struct or union and should
1851 pedwarn. */
1852 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1853 c_parser_consume_token (parser);
1854 else
1856 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1857 pedwarn ("%Hno semicolon at end of struct or union",
1858 &c_parser_peek_token (parser)->location);
1859 else
1861 c_parser_error (parser, "expected %<;%>");
1862 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1863 break;
1867 postfix_attrs = c_parser_attributes (parser);
1868 ret.spec = finish_struct (type, nreverse (contents),
1869 chainon (attrs, postfix_attrs));
1870 ret.kind = ctsk_tagdef;
1871 return ret;
1873 else if (!ident)
1875 c_parser_error (parser, "expected %<{%>");
1876 ret.spec = error_mark_node;
1877 ret.kind = ctsk_tagref;
1878 return ret;
1880 ret = parser_xref_tag (code, ident);
1881 return ret;
1884 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1885 the trailing semicolon.
1887 struct-declaration:
1888 specifier-qualifier-list struct-declarator-list
1890 specifier-qualifier-list:
1891 type-specifier specifier-qualifier-list[opt]
1892 type-qualifier specifier-qualifier-list[opt]
1893 attributes specifier-qualifier-list[opt]
1895 struct-declarator-list:
1896 struct-declarator
1897 struct-declarator-list , attributes[opt] struct-declarator
1899 struct-declarator:
1900 declarator attributes[opt]
1901 declarator[opt] : constant-expression attributes[opt]
1903 GNU extensions:
1905 struct-declaration:
1906 __extension__ struct-declaration
1907 specifier-qualifier-list
1909 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1910 of attributes where shown is a GNU extension. In GNU C, we accept
1911 any expression without commas in the syntax (assignment
1912 expressions, not just conditional expressions); assignment
1913 expressions will be diagnosed as non-constant. */
1915 static tree
1916 c_parser_struct_declaration (c_parser *parser)
1918 struct c_declspecs *specs;
1919 tree prefix_attrs;
1920 tree all_prefix_attrs;
1921 tree decls;
1922 location_t decl_loc;
1923 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1925 int ext;
1926 tree decl;
1927 ext = disable_extension_diagnostics ();
1928 c_parser_consume_token (parser);
1929 decl = c_parser_struct_declaration (parser);
1930 restore_extension_diagnostics (ext);
1931 return decl;
1933 specs = build_null_declspecs ();
1934 decl_loc = c_parser_peek_token (parser)->location;
1935 c_parser_declspecs (parser, specs, false, true, true);
1936 if (parser->error)
1937 return NULL_TREE;
1938 if (!specs->declspecs_seen_p)
1940 c_parser_error (parser, "expected specifier-qualifier-list");
1941 return NULL_TREE;
1943 finish_declspecs (specs);
1944 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1946 tree ret;
1947 if (!specs->type_seen_p)
1949 if (pedantic)
1950 pedwarn ("%HISO C forbids member declarations with no members",
1951 &decl_loc);
1952 shadow_tag_warned (specs, pedantic);
1953 ret = NULL_TREE;
1955 else
1957 /* Support for unnamed structs or unions as members of
1958 structs or unions (which is [a] useful and [b] supports
1959 MS P-SDK). */
1960 tree attrs = NULL;
1961 ret = grokfield (build_id_declarator (NULL_TREE), specs,
1962 NULL_TREE, &attrs);
1963 if (ret)
1964 decl_attributes (&ret, attrs, 0);
1966 return ret;
1968 pending_xref_error ();
1969 prefix_attrs = specs->attrs;
1970 all_prefix_attrs = prefix_attrs;
1971 specs->attrs = NULL_TREE;
1972 decls = NULL_TREE;
1973 while (true)
1975 /* Declaring one or more declarators or un-named bit-fields. */
1976 struct c_declarator *declarator;
1977 bool dummy = false;
1978 if (c_parser_next_token_is (parser, CPP_COLON))
1979 declarator = build_id_declarator (NULL_TREE);
1980 else
1981 declarator = c_parser_declarator (parser, specs->type_seen_p,
1982 C_DTR_NORMAL, &dummy);
1983 if (declarator == NULL)
1985 c_parser_skip_to_end_of_block_or_statement (parser);
1986 break;
1988 if (c_parser_next_token_is (parser, CPP_COLON)
1989 || c_parser_next_token_is (parser, CPP_COMMA)
1990 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1991 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1992 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1994 tree postfix_attrs = NULL_TREE;
1995 tree width = NULL_TREE;
1996 tree d;
1997 if (c_parser_next_token_is (parser, CPP_COLON))
1999 c_parser_consume_token (parser);
2000 width = c_parser_expr_no_commas (parser, NULL).value;
2002 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2003 postfix_attrs = c_parser_attributes (parser);
2004 d = grokfield (declarator, specs, width, &all_prefix_attrs);
2005 decl_attributes (&d, chainon (postfix_attrs,
2006 all_prefix_attrs), 0);
2007 TREE_CHAIN (d) = decls;
2008 decls = d;
2009 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2010 all_prefix_attrs = chainon (c_parser_attributes (parser),
2011 prefix_attrs);
2012 else
2013 all_prefix_attrs = prefix_attrs;
2014 if (c_parser_next_token_is (parser, CPP_COMMA))
2015 c_parser_consume_token (parser);
2016 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2017 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2019 /* Semicolon consumed in caller. */
2020 break;
2022 else
2024 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2025 break;
2028 else
2030 c_parser_error (parser,
2031 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2032 "%<__attribute__%>");
2033 break;
2036 return decls;
2039 /* Parse a typeof specifier (a GNU extension).
2041 typeof-specifier:
2042 typeof ( expression )
2043 typeof ( type-name )
2046 static struct c_typespec
2047 c_parser_typeof_specifier (c_parser *parser)
2049 struct c_typespec ret;
2050 ret.kind = ctsk_typeof;
2051 ret.spec = error_mark_node;
2052 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2053 c_parser_consume_token (parser);
2054 skip_evaluation++;
2055 in_typeof++;
2056 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2058 skip_evaluation--;
2059 in_typeof--;
2060 return ret;
2062 if (c_parser_next_token_starts_typename (parser))
2064 struct c_type_name *type = c_parser_type_name (parser);
2065 skip_evaluation--;
2066 in_typeof--;
2067 if (type != NULL)
2069 ret.spec = groktypename (type);
2070 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2073 else
2075 bool was_vm;
2076 location_t here = c_parser_peek_token (parser)->location;
2077 struct c_expr expr = c_parser_expression (parser);
2078 skip_evaluation--;
2079 in_typeof--;
2080 if (TREE_CODE (expr.value) == COMPONENT_REF
2081 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2082 error ("%H%<typeof%> applied to a bit-field", &here);
2083 ret.spec = TREE_TYPE (expr.value);
2084 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2085 /* This should be returned with the type so that when the type
2086 is evaluated, this can be evaluated. For now, we avoid
2087 evaluation when the context might. */
2088 if (!skip_evaluation && was_vm)
2090 tree e = expr.value;
2092 /* If the expression is not of a type to which we cannot assign a line
2093 number, wrap the thing in a no-op NOP_EXPR. */
2094 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2095 e = build1 (NOP_EXPR, void_type_node, e);
2097 if (CAN_HAVE_LOCATION_P (e))
2098 SET_EXPR_LOCATION (e, input_location);
2100 add_stmt (e);
2102 pop_maybe_used (was_vm);
2104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2105 return ret;
2108 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2109 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2110 be redeclared; otherwise it may not. KIND indicates which kind of
2111 declarator is wanted. Returns a valid declarator except in the
2112 case of a syntax error in which case NULL is returned. *SEEN_ID is
2113 set to true if an identifier being declared is seen; this is used
2114 to diagnose bad forms of abstract array declarators and to
2115 determine whether an identifier list is syntactically permitted.
2117 declarator:
2118 pointer[opt] direct-declarator
2120 direct-declarator:
2121 identifier
2122 ( attributes[opt] declarator )
2123 direct-declarator array-declarator
2124 direct-declarator ( parameter-type-list )
2125 direct-declarator ( identifier-list[opt] )
2127 pointer:
2128 * type-qualifier-list[opt]
2129 * type-qualifier-list[opt] pointer
2131 type-qualifier-list:
2132 type-qualifier
2133 attributes
2134 type-qualifier-list type-qualifier
2135 type-qualifier-list attributes
2137 parameter-type-list:
2138 parameter-list
2139 parameter-list , ...
2141 parameter-list:
2142 parameter-declaration
2143 parameter-list , parameter-declaration
2145 parameter-declaration:
2146 declaration-specifiers declarator attributes[opt]
2147 declaration-specifiers abstract-declarator[opt] attributes[opt]
2149 identifier-list:
2150 identifier
2151 identifier-list , identifier
2153 abstract-declarator:
2154 pointer
2155 pointer[opt] direct-abstract-declarator
2157 direct-abstract-declarator:
2158 ( attributes[opt] abstract-declarator )
2159 direct-abstract-declarator[opt] array-declarator
2160 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2162 GNU extensions:
2164 direct-declarator:
2165 direct-declarator ( parameter-forward-declarations
2166 parameter-type-list[opt] )
2168 direct-abstract-declarator:
2169 direct-abstract-declarator[opt] ( parameter-forward-declarations
2170 parameter-type-list[opt] )
2172 parameter-forward-declarations:
2173 parameter-list ;
2174 parameter-forward-declarations parameter-list ;
2176 The uses of attributes shown above are GNU extensions.
2178 Some forms of array declarator are not included in C99 in the
2179 syntax for abstract declarators; these are disallowed elsewhere.
2180 This may be a defect (DR#289).
2182 This function also accepts an omitted abstract declarator as being
2183 an abstract declarator, although not part of the formal syntax. */
2185 static struct c_declarator *
2186 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2187 bool *seen_id)
2189 /* Parse any initial pointer part. */
2190 if (c_parser_next_token_is (parser, CPP_MULT))
2192 struct c_declspecs *quals_attrs = build_null_declspecs ();
2193 struct c_declarator *inner;
2194 c_parser_consume_token (parser);
2195 c_parser_declspecs (parser, quals_attrs, false, false, true);
2196 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2197 if (inner == NULL)
2198 return NULL;
2199 else
2200 return make_pointer_declarator (quals_attrs, inner);
2202 /* Now we have a direct declarator, direct abstract declarator or
2203 nothing (which counts as a direct abstract declarator here). */
2204 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2207 /* Parse a direct declarator or direct abstract declarator; arguments
2208 as c_parser_declarator. */
2210 static struct c_declarator *
2211 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2212 bool *seen_id)
2214 /* The direct declarator must start with an identifier (possibly
2215 omitted) or a parenthesized declarator (possibly abstract). In
2216 an ordinary declarator, initial parentheses must start a
2217 parenthesized declarator. In an abstract declarator or parameter
2218 declarator, they could start a parenthesized declarator or a
2219 parameter list. To tell which, the open parenthesis and any
2220 following attributes must be read. If a declaration specifier
2221 follows, then it is a parameter list; if the specifier is a
2222 typedef name, there might be an ambiguity about redeclaring it,
2223 which is resolved in the direction of treating it as a typedef
2224 name. If a close parenthesis follows, it is also an empty
2225 parameter list, as the syntax does not permit empty abstract
2226 declarators. Otherwise, it is a parenthesized declarator (in
2227 which case the analysis may be repeated inside it, recursively).
2229 ??? There is an ambiguity in a parameter declaration "int
2230 (__attribute__((foo)) x)", where x is not a typedef name: it
2231 could be an abstract declarator for a function, or declare x with
2232 parentheses. The proper resolution of this ambiguity needs
2233 documenting. At present we follow an accident of the old
2234 parser's implementation, whereby the first parameter must have
2235 some declaration specifiers other than just attributes. Thus as
2236 a parameter declaration it is treated as a parenthesized
2237 parameter named x, and as an abstract declarator it is
2238 rejected.
2240 ??? Also following the old parser, attributes inside an empty
2241 parameter list are ignored, making it a list not yielding a
2242 prototype, rather than giving an error or making it have one
2243 parameter with implicit type int.
2245 ??? Also following the old parser, typedef names may be
2246 redeclared in declarators, but not Objective-C class names. */
2248 if (kind != C_DTR_ABSTRACT
2249 && c_parser_next_token_is (parser, CPP_NAME)
2250 && ((type_seen_p
2251 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2252 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2254 struct c_declarator *inner
2255 = build_id_declarator (c_parser_peek_token (parser)->value);
2256 *seen_id = true;
2257 inner->id_loc = c_parser_peek_token (parser)->location;
2258 c_parser_consume_token (parser);
2259 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2262 if (kind != C_DTR_NORMAL
2263 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2265 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2266 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2269 /* Either we are at the end of an abstract declarator, or we have
2270 parentheses. */
2272 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2274 tree attrs;
2275 struct c_declarator *inner;
2276 c_parser_consume_token (parser);
2277 attrs = c_parser_attributes (parser);
2278 if (kind != C_DTR_NORMAL
2279 && (c_parser_next_token_starts_declspecs (parser)
2280 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2282 struct c_arg_info *args
2283 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2284 attrs);
2285 if (args == NULL)
2286 return NULL;
2287 else
2289 inner
2290 = build_function_declarator (args,
2291 build_id_declarator (NULL_TREE));
2292 return c_parser_direct_declarator_inner (parser, *seen_id,
2293 inner);
2296 /* A parenthesized declarator. */
2297 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2298 if (inner != NULL && attrs != NULL)
2299 inner = build_attrs_declarator (attrs, inner);
2300 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2302 c_parser_consume_token (parser);
2303 if (inner == NULL)
2304 return NULL;
2305 else
2306 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2308 else
2310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2311 "expected %<)%>");
2312 return NULL;
2315 else
2317 if (kind == C_DTR_NORMAL)
2319 c_parser_error (parser, "expected identifier or %<(%>");
2320 return NULL;
2322 else
2323 return build_id_declarator (NULL_TREE);
2327 /* Parse part of a direct declarator or direct abstract declarator,
2328 given that some (in INNER) has already been parsed; ID_PRESENT is
2329 true if an identifier is present, false for an abstract
2330 declarator. */
2332 static struct c_declarator *
2333 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2334 struct c_declarator *inner)
2336 /* Parse a sequence of array declarators and parameter lists. */
2337 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2339 struct c_declarator *declarator;
2340 struct c_declspecs *quals_attrs = build_null_declspecs ();
2341 bool static_seen;
2342 bool star_seen;
2343 tree dimen;
2344 c_parser_consume_token (parser);
2345 c_parser_declspecs (parser, quals_attrs, false, false, true);
2346 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2347 if (static_seen)
2348 c_parser_consume_token (parser);
2349 if (static_seen && !quals_attrs->declspecs_seen_p)
2350 c_parser_declspecs (parser, quals_attrs, false, false, true);
2351 if (!quals_attrs->declspecs_seen_p)
2352 quals_attrs = NULL;
2353 /* If "static" is present, there must be an array dimension.
2354 Otherwise, there may be a dimension, "*", or no
2355 dimension. */
2356 if (static_seen)
2358 star_seen = false;
2359 dimen = c_parser_expr_no_commas (parser, NULL).value;
2361 else
2363 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2365 dimen = NULL_TREE;
2366 star_seen = false;
2368 else if (c_parser_next_token_is (parser, CPP_MULT))
2370 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2372 dimen = NULL_TREE;
2373 star_seen = true;
2374 c_parser_consume_token (parser);
2376 else
2378 star_seen = false;
2379 dimen = c_parser_expr_no_commas (parser, NULL).value;
2382 else
2384 star_seen = false;
2385 dimen = c_parser_expr_no_commas (parser, NULL).value;
2388 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2389 c_parser_consume_token (parser);
2390 else
2392 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2393 "expected %<]%>");
2394 return NULL;
2396 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2397 star_seen);
2398 if (declarator == NULL)
2399 return NULL;
2400 inner = set_array_declarator_inner (declarator, inner);
2401 return c_parser_direct_declarator_inner (parser, id_present, inner);
2403 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2405 tree attrs;
2406 struct c_arg_info *args;
2407 c_parser_consume_token (parser);
2408 attrs = c_parser_attributes (parser);
2409 args = c_parser_parms_declarator (parser, id_present, attrs);
2410 if (args == NULL)
2411 return NULL;
2412 else
2414 inner = build_function_declarator (args, inner);
2415 return c_parser_direct_declarator_inner (parser, id_present, inner);
2418 return inner;
2421 /* Parse a parameter list or identifier list, including the closing
2422 parenthesis but not the opening one. ATTRS are the attributes at
2423 the start of the list. ID_LIST_OK is true if an identifier list is
2424 acceptable; such a list must not have attributes at the start. */
2426 static struct c_arg_info *
2427 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2429 push_scope ();
2430 declare_parm_level ();
2431 /* If the list starts with an identifier, it is an identifier list.
2432 Otherwise, it is either a prototype list or an empty list. */
2433 if (id_list_ok
2434 && !attrs
2435 && c_parser_next_token_is (parser, CPP_NAME)
2436 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2438 tree list = NULL_TREE, *nextp = &list;
2439 while (c_parser_next_token_is (parser, CPP_NAME)
2440 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2442 *nextp = build_tree_list (NULL_TREE,
2443 c_parser_peek_token (parser)->value);
2444 nextp = & TREE_CHAIN (*nextp);
2445 c_parser_consume_token (parser);
2446 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2447 break;
2448 c_parser_consume_token (parser);
2449 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2451 c_parser_error (parser, "expected identifier");
2452 break;
2455 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2457 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2458 ret->parms = 0;
2459 ret->tags = 0;
2460 ret->types = list;
2461 ret->others = 0;
2462 ret->pending_sizes = 0;
2463 ret->had_vla_unspec = 0;
2464 c_parser_consume_token (parser);
2465 pop_scope ();
2466 return ret;
2468 else
2470 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2471 "expected %<)%>");
2472 pop_scope ();
2473 return NULL;
2476 else
2478 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2479 pop_scope ();
2480 return ret;
2484 /* Parse a parameter list (possibly empty), including the closing
2485 parenthesis but not the opening one. ATTRS are the attributes at
2486 the start of the list. */
2488 static struct c_arg_info *
2489 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2491 bool good_parm = false;
2492 /* ??? Following the old parser, forward parameter declarations may
2493 use abstract declarators, and if no real parameter declarations
2494 follow the forward declarations then this is not diagnosed. Also
2495 note as above that attributes are ignored as the only contents of
2496 the parentheses, or as the only contents after forward
2497 declarations. */
2498 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2500 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2501 ret->parms = 0;
2502 ret->tags = 0;
2503 ret->types = 0;
2504 ret->others = 0;
2505 ret->pending_sizes = 0;
2506 ret->had_vla_unspec = 0;
2507 c_parser_consume_token (parser);
2508 return ret;
2510 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2512 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2513 ret->parms = 0;
2514 ret->tags = 0;
2515 ret->others = 0;
2516 ret->pending_sizes = 0;
2517 ret->had_vla_unspec = 0;
2518 /* Suppress -Wold-style-definition for this case. */
2519 ret->types = error_mark_node;
2520 error ("%HISO C requires a named argument before %<...%>",
2521 &c_parser_peek_token (parser)->location);
2522 c_parser_consume_token (parser);
2523 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2525 c_parser_consume_token (parser);
2526 return ret;
2528 else
2530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2531 "expected %<)%>");
2532 return NULL;
2535 /* Nonempty list of parameters, either terminated with semicolon
2536 (forward declarations; recurse) or with close parenthesis (normal
2537 function) or with ", ... )" (variadic function). */
2538 while (true)
2540 /* Parse a parameter. */
2541 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2542 attrs = NULL_TREE;
2543 if (parm != NULL)
2545 good_parm = true;
2546 push_parm_decl (parm);
2548 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2550 tree new_attrs;
2551 c_parser_consume_token (parser);
2552 mark_forward_parm_decls ();
2553 new_attrs = c_parser_attributes (parser);
2554 return c_parser_parms_list_declarator (parser, new_attrs);
2556 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2558 c_parser_consume_token (parser);
2559 if (good_parm)
2560 return get_parm_info (false);
2561 else
2563 struct c_arg_info *ret
2564 = XOBNEW (&parser_obstack, struct c_arg_info);
2565 ret->parms = 0;
2566 ret->tags = 0;
2567 ret->types = 0;
2568 ret->others = 0;
2569 ret->pending_sizes = 0;
2570 ret->had_vla_unspec = 0;
2571 return ret;
2574 if (!c_parser_require (parser, CPP_COMMA,
2575 "expected %<;%>, %<,%> or %<)%>"))
2577 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2578 return NULL;
2580 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2582 c_parser_consume_token (parser);
2583 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2585 c_parser_consume_token (parser);
2586 if (good_parm)
2587 return get_parm_info (true);
2588 else
2590 struct c_arg_info *ret
2591 = XOBNEW (&parser_obstack, struct c_arg_info);
2592 ret->parms = 0;
2593 ret->tags = 0;
2594 ret->types = 0;
2595 ret->others = 0;
2596 ret->pending_sizes = 0;
2597 ret->had_vla_unspec = 0;
2598 return ret;
2601 else
2603 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2604 "expected %<)%>");
2605 return NULL;
2611 /* Parse a parameter declaration. ATTRS are the attributes at the
2612 start of the declaration if it is the first parameter. */
2614 static struct c_parm *
2615 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2617 struct c_declspecs *specs;
2618 struct c_declarator *declarator;
2619 tree prefix_attrs;
2620 tree postfix_attrs = NULL_TREE;
2621 bool dummy = false;
2622 if (!c_parser_next_token_starts_declspecs (parser))
2624 /* ??? In some Objective-C cases '...' isn't applicable so there
2625 should be a different message. */
2626 c_parser_error (parser,
2627 "expected declaration specifiers or %<...%>");
2628 c_parser_skip_to_end_of_parameter (parser);
2629 return NULL;
2631 specs = build_null_declspecs ();
2632 if (attrs)
2634 declspecs_add_attrs (specs, attrs);
2635 attrs = NULL_TREE;
2637 c_parser_declspecs (parser, specs, true, true, true);
2638 finish_declspecs (specs);
2639 pending_xref_error ();
2640 prefix_attrs = specs->attrs;
2641 specs->attrs = NULL_TREE;
2642 declarator = c_parser_declarator (parser, specs->type_seen_p,
2643 C_DTR_PARM, &dummy);
2644 if (declarator == NULL)
2646 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2647 return NULL;
2649 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2650 postfix_attrs = c_parser_attributes (parser);
2651 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2652 declarator);
2655 /* Parse a string literal in an asm expression. It should not be
2656 translated, and wide string literals are an error although
2657 permitted by the syntax. This is a GNU extension.
2659 asm-string-literal:
2660 string-literal
2662 ??? At present, following the old parser, the caller needs to have
2663 set lex_untranslated_string to 1. It would be better to follow the
2664 C++ parser rather than using this kludge. */
2666 static tree
2667 c_parser_asm_string_literal (c_parser *parser)
2669 tree str;
2670 if (c_parser_next_token_is (parser, CPP_STRING))
2672 str = c_parser_peek_token (parser)->value;
2673 c_parser_consume_token (parser);
2675 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2677 error ("%Hwide string literal in %<asm%>",
2678 &c_parser_peek_token (parser)->location);
2679 str = build_string (1, "");
2680 c_parser_consume_token (parser);
2682 else
2684 c_parser_error (parser, "expected string literal");
2685 str = NULL_TREE;
2687 return str;
2690 /* Parse a simple asm expression. This is used in restricted
2691 contexts, where a full expression with inputs and outputs does not
2692 make sense. This is a GNU extension.
2694 simple-asm-expr:
2695 asm ( asm-string-literal )
2698 static tree
2699 c_parser_simple_asm_expr (c_parser *parser)
2701 tree str;
2702 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2703 /* ??? Follow the C++ parser rather than using the
2704 lex_untranslated_string kludge. */
2705 parser->lex_untranslated_string = true;
2706 c_parser_consume_token (parser);
2707 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2709 parser->lex_untranslated_string = false;
2710 return NULL_TREE;
2712 str = c_parser_asm_string_literal (parser);
2713 parser->lex_untranslated_string = false;
2714 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2717 return NULL_TREE;
2719 return str;
2722 /* Parse (possibly empty) attributes. This is a GNU extension.
2724 attributes:
2725 empty
2726 attributes attribute
2728 attribute:
2729 __attribute__ ( ( attribute-list ) )
2731 attribute-list:
2732 attrib
2733 attribute_list , attrib
2735 attrib:
2736 empty
2737 any-word
2738 any-word ( identifier )
2739 any-word ( identifier , nonempty-expr-list )
2740 any-word ( expr-list )
2742 where the "identifier" must not be declared as a type, and
2743 "any-word" may be any identifier (including one declared as a
2744 type), a reserved word storage class specifier, type specifier or
2745 type qualifier. ??? This still leaves out most reserved keywords
2746 (following the old parser), shouldn't we include them, and why not
2747 allow identifiers declared as types to start the arguments? */
2749 static tree
2750 c_parser_attributes (c_parser *parser)
2752 tree attrs = NULL_TREE;
2753 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2755 /* ??? Follow the C++ parser rather than using the
2756 lex_untranslated_string kludge. */
2757 parser->lex_untranslated_string = true;
2758 c_parser_consume_token (parser);
2759 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2761 parser->lex_untranslated_string = false;
2762 return attrs;
2764 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2766 parser->lex_untranslated_string = false;
2767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2768 return attrs;
2770 /* Parse the attribute list. */
2771 while (c_parser_next_token_is (parser, CPP_COMMA)
2772 || c_parser_next_token_is (parser, CPP_NAME)
2773 || c_parser_next_token_is (parser, CPP_KEYWORD))
2775 tree attr, attr_name, attr_args;
2776 if (c_parser_next_token_is (parser, CPP_COMMA))
2778 c_parser_consume_token (parser);
2779 continue;
2781 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2783 /* ??? See comment above about what keywords are
2784 accepted here. */
2785 bool ok;
2786 switch (c_parser_peek_token (parser)->keyword)
2788 case RID_STATIC:
2789 case RID_UNSIGNED:
2790 case RID_LONG:
2791 case RID_CONST:
2792 case RID_EXTERN:
2793 case RID_REGISTER:
2794 case RID_TYPEDEF:
2795 case RID_SHORT:
2796 case RID_INLINE:
2797 case RID_VOLATILE:
2798 case RID_SIGNED:
2799 case RID_AUTO:
2800 case RID_RESTRICT:
2801 case RID_COMPLEX:
2802 case RID_THREAD:
2803 case RID_INT:
2804 case RID_CHAR:
2805 case RID_FLOAT:
2806 case RID_DOUBLE:
2807 case RID_VOID:
2808 case RID_DFLOAT32:
2809 case RID_DFLOAT64:
2810 case RID_DFLOAT128:
2811 case RID_BOOL:
2812 case RID_FRACT:
2813 case RID_ACCUM:
2814 case RID_SAT:
2815 ok = true;
2816 break;
2817 default:
2818 ok = false;
2819 break;
2821 if (!ok)
2822 break;
2824 attr_name = c_parser_peek_token (parser)->value;
2825 c_parser_consume_token (parser);
2826 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2828 attr = build_tree_list (attr_name, NULL_TREE);
2829 attrs = chainon (attrs, attr);
2830 continue;
2832 c_parser_consume_token (parser);
2833 /* Parse the attribute contents. If they start with an
2834 identifier which is followed by a comma or close
2835 parenthesis, then the arguments start with that
2836 identifier; otherwise they are an expression list. */
2837 if (c_parser_next_token_is (parser, CPP_NAME)
2838 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2839 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2840 || (c_parser_peek_2nd_token (parser)->type
2841 == CPP_CLOSE_PAREN)))
2843 tree arg1 = c_parser_peek_token (parser)->value;
2844 c_parser_consume_token (parser);
2845 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2846 attr_args = build_tree_list (NULL_TREE, arg1);
2847 else
2849 c_parser_consume_token (parser);
2850 attr_args = tree_cons (NULL_TREE, arg1,
2851 c_parser_expr_list (parser, false));
2854 else
2856 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2857 attr_args = NULL_TREE;
2858 else
2859 attr_args = c_parser_expr_list (parser, false);
2861 attr = build_tree_list (attr_name, attr_args);
2862 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2863 c_parser_consume_token (parser);
2864 else
2866 parser->lex_untranslated_string = false;
2867 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2868 "expected %<)%>");
2869 return attrs;
2871 attrs = chainon (attrs, attr);
2873 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2874 c_parser_consume_token (parser);
2875 else
2877 parser->lex_untranslated_string = false;
2878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2879 "expected %<)%>");
2880 return attrs;
2882 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2883 c_parser_consume_token (parser);
2884 else
2886 parser->lex_untranslated_string = false;
2887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2888 "expected %<)%>");
2889 return attrs;
2891 parser->lex_untranslated_string = false;
2893 return attrs;
2896 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2898 type-name:
2899 specifier-qualifier-list abstract-declarator[opt]
2902 static struct c_type_name *
2903 c_parser_type_name (c_parser *parser)
2905 struct c_declspecs *specs = build_null_declspecs ();
2906 struct c_declarator *declarator;
2907 struct c_type_name *ret;
2908 bool dummy = false;
2909 c_parser_declspecs (parser, specs, false, true, true);
2910 if (!specs->declspecs_seen_p)
2912 c_parser_error (parser, "expected specifier-qualifier-list");
2913 return NULL;
2915 pending_xref_error ();
2916 finish_declspecs (specs);
2917 declarator = c_parser_declarator (parser, specs->type_seen_p,
2918 C_DTR_ABSTRACT, &dummy);
2919 if (declarator == NULL)
2920 return NULL;
2921 ret = XOBNEW (&parser_obstack, struct c_type_name);
2922 ret->specs = specs;
2923 ret->declarator = declarator;
2924 return ret;
2927 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2929 initializer:
2930 assignment-expression
2931 { initializer-list }
2932 { initializer-list , }
2934 initializer-list:
2935 designation[opt] initializer
2936 initializer-list , designation[opt] initializer
2938 designation:
2939 designator-list =
2941 designator-list:
2942 designator
2943 designator-list designator
2945 designator:
2946 array-designator
2947 . identifier
2949 array-designator:
2950 [ constant-expression ]
2952 GNU extensions:
2954 initializer:
2957 designation:
2958 array-designator
2959 identifier :
2961 array-designator:
2962 [ constant-expression ... constant-expression ]
2964 Any expression without commas is accepted in the syntax for the
2965 constant-expressions, with non-constant expressions rejected later.
2967 This function is only used for top-level initializers; for nested
2968 ones, see c_parser_initval. */
2970 static struct c_expr
2971 c_parser_initializer (c_parser *parser)
2973 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2974 return c_parser_braced_init (parser, NULL_TREE, false);
2975 else
2977 struct c_expr ret;
2978 ret = c_parser_expr_no_commas (parser, NULL);
2979 if (TREE_CODE (ret.value) != STRING_CST
2980 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
2981 ret = default_function_array_conversion (ret);
2982 return ret;
2986 /* Parse a braced initializer list. TYPE is the type specified for a
2987 compound literal, and NULL_TREE for other initializers and for
2988 nested braced lists. NESTED_P is true for nested braced lists,
2989 false for the list of a compound literal or the list that is the
2990 top-level initializer in a declaration. */
2992 static struct c_expr
2993 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2995 location_t brace_loc = c_parser_peek_token (parser)->location;
2996 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2997 c_parser_consume_token (parser);
2998 if (nested_p)
2999 push_init_level (0);
3000 else
3001 really_start_incremental_init (type);
3002 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3004 if (pedantic)
3005 pedwarn ("%HISO C forbids empty initializer braces", &brace_loc);
3007 else
3009 /* Parse a non-empty initializer list, possibly with a trailing
3010 comma. */
3011 while (true)
3013 c_parser_initelt (parser);
3014 if (parser->error)
3015 break;
3016 if (c_parser_next_token_is (parser, CPP_COMMA))
3017 c_parser_consume_token (parser);
3018 else
3019 break;
3020 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3021 break;
3024 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3026 struct c_expr ret;
3027 ret.value = error_mark_node;
3028 ret.original_code = ERROR_MARK;
3029 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3030 return ret;
3032 c_parser_consume_token (parser);
3033 return pop_init_level (0);
3036 /* Parse a nested initializer, including designators. */
3038 static void
3039 c_parser_initelt (c_parser *parser)
3041 /* Parse any designator or designator list. A single array
3042 designator may have the subsequent "=" omitted in GNU C, but a
3043 longer list or a structure member designator may not. */
3044 if (c_parser_next_token_is (parser, CPP_NAME)
3045 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3047 /* Old-style structure member designator. */
3048 set_init_label (c_parser_peek_token (parser)->value);
3049 if (pedantic)
3051 /* Use the colon as the error location. */
3052 pedwarn ("%Hobsolete use of designated initializer with %<:%>",
3053 &c_parser_peek_2nd_token (parser)->location);
3055 c_parser_consume_token (parser);
3056 c_parser_consume_token (parser);
3058 else
3060 /* des_seen is 0 if there have been no designators, 1 if there
3061 has been a single array designator and 2 otherwise. */
3062 int des_seen = 0;
3063 /* Location of a designator. */
3064 location_t des_loc;
3065 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3066 || c_parser_next_token_is (parser, CPP_DOT))
3068 int des_prev = des_seen;
3069 if (!des_seen)
3070 des_loc = c_parser_peek_token (parser)->location;
3071 if (des_seen < 2)
3072 des_seen++;
3073 if (c_parser_next_token_is (parser, CPP_DOT))
3075 des_seen = 2;
3076 c_parser_consume_token (parser);
3077 if (c_parser_next_token_is (parser, CPP_NAME))
3079 set_init_label (c_parser_peek_token (parser)->value);
3080 c_parser_consume_token (parser);
3082 else
3084 struct c_expr init;
3085 init.value = error_mark_node;
3086 init.original_code = ERROR_MARK;
3087 c_parser_error (parser, "expected identifier");
3088 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3089 process_init_element (init);
3090 return;
3093 else
3095 tree first, second;
3096 location_t ellipsis_loc;
3097 /* ??? Following the old parser, [ objc-receiver
3098 objc-message-args ] is accepted as an initializer,
3099 being distinguished from a designator by what follows
3100 the first assignment expression inside the square
3101 brackets, but after a first array designator a
3102 subsequent square bracket is for Objective-C taken to
3103 start an expression, using the obsolete form of
3104 designated initializer without '=', rather than
3105 possibly being a second level of designation: in LALR
3106 terms, the '[' is shifted rather than reducing
3107 designator to designator-list. */
3108 if (des_prev == 1 && c_dialect_objc ())
3110 des_seen = des_prev;
3111 break;
3113 if (des_prev == 0 && c_dialect_objc ())
3115 /* This might be an array designator or an
3116 Objective-C message expression. If the former,
3117 continue parsing here; if the latter, parse the
3118 remainder of the initializer given the starting
3119 primary-expression. ??? It might make sense to
3120 distinguish when des_prev == 1 as well; see
3121 previous comment. */
3122 tree rec, args;
3123 struct c_expr mexpr;
3124 c_parser_consume_token (parser);
3125 if (c_parser_peek_token (parser)->type == CPP_NAME
3126 && ((c_parser_peek_token (parser)->id_kind
3127 == C_ID_TYPENAME)
3128 || (c_parser_peek_token (parser)->id_kind
3129 == C_ID_CLASSNAME)))
3131 /* Type name receiver. */
3132 tree id = c_parser_peek_token (parser)->value;
3133 c_parser_consume_token (parser);
3134 rec = objc_get_class_reference (id);
3135 goto parse_message_args;
3137 first = c_parser_expr_no_commas (parser, NULL).value;
3138 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3139 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3140 goto array_desig_after_first;
3141 /* Expression receiver. So far only one part
3142 without commas has been parsed; there might be
3143 more of the expression. */
3144 rec = first;
3145 while (c_parser_next_token_is (parser, CPP_COMMA))
3147 struct c_expr next;
3148 c_parser_consume_token (parser);
3149 next = c_parser_expr_no_commas (parser, NULL);
3150 next = default_function_array_conversion (next);
3151 rec = build_compound_expr (rec, next.value);
3153 parse_message_args:
3154 /* Now parse the objc-message-args. */
3155 args = c_parser_objc_message_args (parser);
3156 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3157 "expected %<]%>");
3158 mexpr.value
3159 = objc_build_message_expr (build_tree_list (rec, args));
3160 mexpr.original_code = ERROR_MARK;
3161 /* Now parse and process the remainder of the
3162 initializer, starting with this message
3163 expression as a primary-expression. */
3164 c_parser_initval (parser, &mexpr);
3165 return;
3167 c_parser_consume_token (parser);
3168 first = c_parser_expr_no_commas (parser, NULL).value;
3169 array_desig_after_first:
3170 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3172 ellipsis_loc = c_parser_peek_token (parser)->location;
3173 c_parser_consume_token (parser);
3174 second = c_parser_expr_no_commas (parser, NULL).value;
3176 else
3177 second = NULL_TREE;
3178 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3180 c_parser_consume_token (parser);
3181 set_init_index (first, second);
3182 if (pedantic && second)
3183 pedwarn ("%HISO C forbids specifying range of "
3184 "elements to initialize", &ellipsis_loc);
3186 else
3187 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3188 "expected %<]%>");
3191 if (des_seen >= 1)
3193 if (c_parser_next_token_is (parser, CPP_EQ))
3195 if (pedantic && !flag_isoc99)
3196 pedwarn ("%HISO C90 forbids specifying subobject "
3197 "to initialize", &des_loc);
3198 c_parser_consume_token (parser);
3200 else
3202 if (des_seen == 1)
3204 if (pedantic)
3205 pedwarn ("%Hobsolete use of designated initializer "
3206 "without %<=%>",
3207 &c_parser_peek_token (parser)->location);
3209 else
3211 struct c_expr init;
3212 init.value = error_mark_node;
3213 init.original_code = ERROR_MARK;
3214 c_parser_error (parser, "expected %<=%>");
3215 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3216 process_init_element (init);
3217 return;
3222 c_parser_initval (parser, NULL);
3225 /* Parse a nested initializer; as c_parser_initializer but parses
3226 initializers within braced lists, after any designators have been
3227 applied. If AFTER is not NULL then it is an Objective-C message
3228 expression which is the primary-expression starting the
3229 initializer. */
3231 static void
3232 c_parser_initval (c_parser *parser, struct c_expr *after)
3234 struct c_expr init;
3235 gcc_assert (!after || c_dialect_objc ());
3236 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3237 init = c_parser_braced_init (parser, NULL_TREE, true);
3238 else
3240 init = c_parser_expr_no_commas (parser, after);
3241 if (init.value != NULL_TREE
3242 && TREE_CODE (init.value) != STRING_CST
3243 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3244 init = default_function_array_conversion (init);
3246 process_init_element (init);
3249 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3250 C99 6.8.2).
3252 compound-statement:
3253 { block-item-list[opt] }
3254 { label-declarations block-item-list }
3256 block-item-list:
3257 block-item
3258 block-item-list block-item
3260 block-item:
3261 nested-declaration
3262 statement
3264 nested-declaration:
3265 declaration
3267 GNU extensions:
3269 compound-statement:
3270 { label-declarations block-item-list }
3272 nested-declaration:
3273 __extension__ nested-declaration
3274 nested-function-definition
3276 label-declarations:
3277 label-declaration
3278 label-declarations label-declaration
3280 label-declaration:
3281 __label__ identifier-list ;
3283 Allowing the mixing of declarations and code is new in C99. The
3284 GNU syntax also permits (not shown above) labels at the end of
3285 compound statements, which yield an error. We don't allow labels
3286 on declarations; this might seem like a natural extension, but
3287 there would be a conflict between attributes on the label and
3288 prefix attributes on the declaration. ??? The syntax follows the
3289 old parser in requiring something after label declarations.
3290 Although they are erroneous if the labels declared aren't defined,
3291 is it useful for the syntax to be this way?
3293 OpenMP:
3295 block-item:
3296 openmp-directive
3298 openmp-directive:
3299 barrier-directive
3300 flush-directive */
3302 static tree
3303 c_parser_compound_statement (c_parser *parser)
3305 tree stmt;
3306 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3307 return error_mark_node;
3308 stmt = c_begin_compound_stmt (true);
3309 c_parser_compound_statement_nostart (parser);
3310 return c_end_compound_stmt (stmt, true);
3313 /* Parse a compound statement except for the opening brace. This is
3314 used for parsing both compound statements and statement expressions
3315 (which follow different paths to handling the opening). */
3317 static void
3318 c_parser_compound_statement_nostart (c_parser *parser)
3320 bool last_stmt = false;
3321 bool last_label = false;
3322 location_t label_loc;
3323 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3325 c_parser_consume_token (parser);
3326 return;
3328 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3330 location_t err_loc = c_parser_peek_token (parser)->location;
3331 /* Read zero or more forward-declarations for labels that nested
3332 functions can jump to. */
3333 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3335 c_parser_consume_token (parser);
3336 /* Any identifiers, including those declared as type names,
3337 are OK here. */
3338 while (true)
3340 tree label;
3341 if (c_parser_next_token_is_not (parser, CPP_NAME))
3343 c_parser_error (parser, "expected identifier");
3344 break;
3346 label
3347 = declare_label (c_parser_peek_token (parser)->value);
3348 C_DECLARED_LABEL_FLAG (label) = 1;
3349 add_stmt (build_stmt (DECL_EXPR, label));
3350 c_parser_consume_token (parser);
3351 if (c_parser_next_token_is (parser, CPP_COMMA))
3352 c_parser_consume_token (parser);
3353 else
3354 break;
3356 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3358 if (pedantic)
3359 pedwarn ("%HISO C forbids label declarations", &err_loc);
3361 /* We must now have at least one statement, label or declaration. */
3362 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3364 c_parser_error (parser, "expected declaration or statement");
3365 c_parser_consume_token (parser);
3366 return;
3368 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3370 location_t loc = c_parser_peek_token (parser)->location;
3371 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3372 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3373 || (c_parser_next_token_is (parser, CPP_NAME)
3374 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3376 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3377 label_loc = c_parser_peek_2nd_token (parser)->location;
3378 else
3379 label_loc = c_parser_peek_token (parser)->location;
3380 last_label = true;
3381 last_stmt = false;
3382 c_parser_label (parser);
3384 else if (!last_label
3385 && c_parser_next_token_starts_declspecs (parser))
3387 last_label = false;
3388 c_parser_declaration_or_fndef (parser, true, true, true, true);
3389 if (last_stmt
3390 && ((pedantic && !flag_isoc99)
3391 || warn_declaration_after_statement))
3392 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3393 &loc);
3394 last_stmt = false;
3396 else if (!last_label
3397 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3399 /* __extension__ can start a declaration, but is also an
3400 unary operator that can start an expression. Consume all
3401 but the last of a possible series of __extension__ to
3402 determine which. */
3403 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3404 && (c_parser_peek_2nd_token (parser)->keyword
3405 == RID_EXTENSION))
3406 c_parser_consume_token (parser);
3407 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3409 int ext;
3410 ext = disable_extension_diagnostics ();
3411 c_parser_consume_token (parser);
3412 last_label = false;
3413 c_parser_declaration_or_fndef (parser, true, true, true, true);
3414 /* Following the old parser, __extension__ does not
3415 disable this diagnostic. */
3416 restore_extension_diagnostics (ext);
3417 if (last_stmt
3418 && ((pedantic && !flag_isoc99)
3419 || warn_declaration_after_statement))
3420 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3421 &loc);
3422 last_stmt = false;
3424 else
3425 goto statement;
3427 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3429 /* External pragmas, and some omp pragmas, are not associated
3430 with regular c code, and so are not to be considered statements
3431 syntactically. This ensures that the user doesn't put them
3432 places that would turn into syntax errors if the directive
3433 were ignored. */
3434 if (c_parser_pragma (parser, pragma_compound))
3435 last_label = false, last_stmt = true;
3437 else if (c_parser_next_token_is (parser, CPP_EOF))
3439 c_parser_error (parser, "expected declaration or statement");
3440 return;
3442 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3444 if (parser->in_if_block)
3446 error ("%H""expected %<}%> before %<else%>", &loc);
3447 return;
3449 else
3451 error ("%H%<else%> without a previous %<if%>", &loc);
3452 c_parser_consume_token (parser);
3453 continue;
3456 else
3458 statement:
3459 last_label = false;
3460 last_stmt = true;
3461 c_parser_statement_after_labels (parser);
3464 parser->error = false;
3466 if (last_label)
3467 error ("%Hlabel at end of compound statement", &label_loc);
3468 c_parser_consume_token (parser);
3471 /* Parse a label (C90 6.6.1, C99 6.8.1).
3473 label:
3474 identifier : attributes[opt]
3475 case constant-expression :
3476 default :
3478 GNU extensions:
3480 label:
3481 case constant-expression ... constant-expression :
3483 The use of attributes on labels is a GNU extension. The syntax in
3484 GNU C accepts any expressions without commas, non-constant
3485 expressions being rejected later. */
3487 static void
3488 c_parser_label (c_parser *parser)
3490 location_t loc1 = c_parser_peek_token (parser)->location;
3491 tree label = NULL_TREE;
3492 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3494 tree exp1, exp2;
3495 c_parser_consume_token (parser);
3496 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3497 if (c_parser_next_token_is (parser, CPP_COLON))
3499 c_parser_consume_token (parser);
3500 label = do_case (exp1, NULL_TREE);
3502 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3504 c_parser_consume_token (parser);
3505 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3506 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3507 label = do_case (exp1, exp2);
3509 else
3510 c_parser_error (parser, "expected %<:%> or %<...%>");
3512 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3514 c_parser_consume_token (parser);
3515 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3516 label = do_case (NULL_TREE, NULL_TREE);
3518 else
3520 tree name = c_parser_peek_token (parser)->value;
3521 tree tlab;
3522 tree attrs;
3523 location_t loc2 = c_parser_peek_token (parser)->location;
3524 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3525 c_parser_consume_token (parser);
3526 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3527 c_parser_consume_token (parser);
3528 attrs = c_parser_attributes (parser);
3529 tlab = define_label (loc2, name);
3530 if (tlab)
3532 decl_attributes (&tlab, attrs, 0);
3533 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3536 if (label)
3538 SET_EXPR_LOCATION (label, loc1);
3539 if (c_parser_next_token_starts_declspecs (parser)
3540 && !(c_parser_next_token_is (parser, CPP_NAME)
3541 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3543 error ("%Ha label can only be part of a statement and "
3544 "a declaration is not a statement",
3545 &c_parser_peek_token (parser)->location);
3546 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3547 /*nested*/ true, /*empty_ok*/ false,
3548 /*start_attr_ok*/ true);
3553 /* Parse a statement (C90 6.6, C99 6.8).
3555 statement:
3556 labeled-statement
3557 compound-statement
3558 expression-statement
3559 selection-statement
3560 iteration-statement
3561 jump-statement
3563 labeled-statement:
3564 label statement
3566 expression-statement:
3567 expression[opt] ;
3569 selection-statement:
3570 if-statement
3571 switch-statement
3573 iteration-statement:
3574 while-statement
3575 do-statement
3576 for-statement
3578 jump-statement:
3579 goto identifier ;
3580 continue ;
3581 break ;
3582 return expression[opt] ;
3584 GNU extensions:
3586 statement:
3587 asm-statement
3589 jump-statement:
3590 goto * expression ;
3592 Objective-C:
3594 statement:
3595 objc-throw-statement
3596 objc-try-catch-statement
3597 objc-synchronized-statement
3599 objc-throw-statement:
3600 @throw expression ;
3601 @throw ;
3603 OpenMP:
3605 statement:
3606 openmp-construct
3608 openmp-construct:
3609 parallel-construct
3610 for-construct
3611 sections-construct
3612 single-construct
3613 parallel-for-construct
3614 parallel-sections-construct
3615 master-construct
3616 critical-construct
3617 atomic-construct
3618 ordered-construct
3620 parallel-construct:
3621 parallel-directive structured-block
3623 for-construct:
3624 for-directive iteration-statement
3626 sections-construct:
3627 sections-directive section-scope
3629 single-construct:
3630 single-directive structured-block
3632 parallel-for-construct:
3633 parallel-for-directive iteration-statement
3635 parallel-sections-construct:
3636 parallel-sections-directive section-scope
3638 master-construct:
3639 master-directive structured-block
3641 critical-construct:
3642 critical-directive structured-block
3644 atomic-construct:
3645 atomic-directive expression-statement
3647 ordered-construct:
3648 ordered-directive structured-block */
3650 static void
3651 c_parser_statement (c_parser *parser)
3653 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3654 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3655 || (c_parser_next_token_is (parser, CPP_NAME)
3656 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3657 c_parser_label (parser);
3658 c_parser_statement_after_labels (parser);
3661 /* Parse a statement, other than a labeled statement. */
3663 static void
3664 c_parser_statement_after_labels (c_parser *parser)
3666 location_t loc = c_parser_peek_token (parser)->location;
3667 tree stmt = NULL_TREE;
3668 bool in_if_block = parser->in_if_block;
3669 parser->in_if_block = false;
3670 switch (c_parser_peek_token (parser)->type)
3672 case CPP_OPEN_BRACE:
3673 add_stmt (c_parser_compound_statement (parser));
3674 break;
3675 case CPP_KEYWORD:
3676 switch (c_parser_peek_token (parser)->keyword)
3678 case RID_IF:
3679 c_parser_if_statement (parser);
3680 break;
3681 case RID_SWITCH:
3682 c_parser_switch_statement (parser);
3683 break;
3684 case RID_WHILE:
3685 c_parser_while_statement (parser);
3686 break;
3687 case RID_DO:
3688 c_parser_do_statement (parser);
3689 break;
3690 case RID_FOR:
3691 c_parser_for_statement (parser);
3692 break;
3693 case RID_GOTO:
3694 c_parser_consume_token (parser);
3695 if (c_parser_next_token_is (parser, CPP_NAME))
3697 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3698 c_parser_consume_token (parser);
3700 else if (c_parser_next_token_is (parser, CPP_MULT))
3702 c_parser_consume_token (parser);
3703 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3705 else
3706 c_parser_error (parser, "expected identifier or %<*%>");
3707 goto expect_semicolon;
3708 case RID_CONTINUE:
3709 c_parser_consume_token (parser);
3710 stmt = c_finish_bc_stmt (&c_cont_label, false);
3711 goto expect_semicolon;
3712 case RID_BREAK:
3713 c_parser_consume_token (parser);
3714 stmt = c_finish_bc_stmt (&c_break_label, true);
3715 goto expect_semicolon;
3716 case RID_RETURN:
3717 c_parser_consume_token (parser);
3718 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3720 stmt = c_finish_return (NULL_TREE);
3721 c_parser_consume_token (parser);
3723 else
3725 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3726 goto expect_semicolon;
3728 break;
3729 case RID_ASM:
3730 stmt = c_parser_asm_statement (parser);
3731 break;
3732 case RID_THROW:
3733 gcc_assert (c_dialect_objc ());
3734 c_parser_consume_token (parser);
3735 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3737 stmt = objc_build_throw_stmt (NULL_TREE);
3738 c_parser_consume_token (parser);
3740 else
3742 stmt
3743 = objc_build_throw_stmt (c_parser_expression (parser).value);
3744 goto expect_semicolon;
3746 break;
3747 case RID_TRY:
3748 gcc_assert (c_dialect_objc ());
3749 c_parser_objc_try_catch_statement (parser);
3750 break;
3751 case RID_AT_SYNCHRONIZED:
3752 gcc_assert (c_dialect_objc ());
3753 c_parser_objc_synchronized_statement (parser);
3754 break;
3755 default:
3756 goto expr_stmt;
3758 break;
3759 case CPP_SEMICOLON:
3760 c_parser_consume_token (parser);
3761 break;
3762 case CPP_CLOSE_PAREN:
3763 case CPP_CLOSE_SQUARE:
3764 /* Avoid infinite loop in error recovery:
3765 c_parser_skip_until_found stops at a closing nesting
3766 delimiter without consuming it, but here we need to consume
3767 it to proceed further. */
3768 c_parser_error (parser, "expected statement");
3769 c_parser_consume_token (parser);
3770 break;
3771 case CPP_PRAGMA:
3772 c_parser_pragma (parser, pragma_stmt);
3773 break;
3774 default:
3775 expr_stmt:
3776 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3777 expect_semicolon:
3778 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3779 break;
3781 /* Two cases cannot and do not have line numbers associated: If stmt
3782 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3783 cannot hold line numbers. But that's OK because the statement
3784 will either be changed to a MODIFY_EXPR during gimplification of
3785 the statement expr, or discarded. If stmt was compound, but
3786 without new variables, we will have skipped the creation of a
3787 BIND and will have a bare STATEMENT_LIST. But that's OK because
3788 (recursively) all of the component statements should already have
3789 line numbers assigned. ??? Can we discard no-op statements
3790 earlier? */
3791 if (stmt && CAN_HAVE_LOCATION_P (stmt))
3792 SET_EXPR_LOCATION (stmt, loc);
3794 parser->in_if_block = in_if_block;
3797 /* Parse a parenthesized condition from an if, do or while statement.
3799 condition:
3800 ( expression )
3802 static tree
3803 c_parser_paren_condition (c_parser *parser)
3805 location_t loc;
3806 tree cond;
3807 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3808 return error_mark_node;
3809 loc = c_parser_peek_token (parser)->location;
3810 cond = c_objc_common_truthvalue_conversion
3811 (c_parser_expression_conv (parser).value);
3812 if (CAN_HAVE_LOCATION_P (cond))
3813 SET_EXPR_LOCATION (cond, loc);
3814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3815 return cond;
3818 /* Parse a statement which is a block in C99. */
3820 static tree
3821 c_parser_c99_block_statement (c_parser *parser)
3823 tree block = c_begin_compound_stmt (flag_isoc99);
3824 c_parser_statement (parser);
3825 return c_end_compound_stmt (block, flag_isoc99);
3828 /* Parse the body of an if statement. This is just parsing a
3829 statement but (a) it is a block in C99, (b) we track whether the
3830 body is an if statement for the sake of -Wparentheses warnings, (c)
3831 we handle an empty body specially for the sake of -Wempty-body
3832 warnings, and (d) we call parser_compound_statement directly
3833 because c_parser_statement_after_labels resets
3834 parser->in_if_block. */
3836 static tree
3837 c_parser_if_body (c_parser *parser, bool *if_p)
3839 tree block = c_begin_compound_stmt (flag_isoc99);
3840 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3841 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3842 || (c_parser_next_token_is (parser, CPP_NAME)
3843 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3844 c_parser_label (parser);
3845 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3846 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3848 add_stmt (build_empty_stmt ());
3849 c_parser_consume_token (parser);
3851 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3852 add_stmt (c_parser_compound_statement (parser));
3853 else
3854 c_parser_statement_after_labels (parser);
3855 return c_end_compound_stmt (block, flag_isoc99);
3858 /* Parse the else body of an if statement. This is just parsing a
3859 statement but (a) it is a block in C99, (b) we handle an empty body
3860 specially for the sake of -Wempty-body warnings. */
3862 static tree
3863 c_parser_else_body (c_parser *parser)
3865 tree block = c_begin_compound_stmt (flag_isoc99);
3866 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3867 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3868 || (c_parser_next_token_is (parser, CPP_NAME)
3869 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3870 c_parser_label (parser);
3871 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3873 add_stmt (build_empty_stmt ());
3874 c_parser_consume_token (parser);
3876 else
3877 c_parser_statement_after_labels (parser);
3878 return c_end_compound_stmt (block, flag_isoc99);
3881 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3883 if-statement:
3884 if ( expression ) statement
3885 if ( expression ) statement else statement
3888 static void
3889 c_parser_if_statement (c_parser *parser)
3891 tree block;
3892 location_t loc;
3893 tree cond;
3894 bool first_if = false;
3895 tree first_body, second_body;
3896 bool in_if_block;
3898 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3899 c_parser_consume_token (parser);
3900 block = c_begin_compound_stmt (flag_isoc99);
3901 loc = c_parser_peek_token (parser)->location;
3902 cond = c_parser_paren_condition (parser);
3903 in_if_block = parser->in_if_block;
3904 parser->in_if_block = true;
3905 first_body = c_parser_if_body (parser, &first_if);
3906 parser->in_if_block = in_if_block;
3907 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3909 c_parser_consume_token (parser);
3910 second_body = c_parser_else_body (parser);
3912 else
3913 second_body = NULL_TREE;
3914 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3915 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3918 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3920 switch-statement:
3921 switch (expression) statement
3924 static void
3925 c_parser_switch_statement (c_parser *parser)
3927 tree block, expr, body, save_break;
3928 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3929 c_parser_consume_token (parser);
3930 block = c_begin_compound_stmt (flag_isoc99);
3931 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3933 expr = c_parser_expression (parser).value;
3934 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3936 else
3937 expr = error_mark_node;
3938 c_start_case (expr);
3939 save_break = c_break_label;
3940 c_break_label = NULL_TREE;
3941 body = c_parser_c99_block_statement (parser);
3942 c_finish_case (body);
3943 if (c_break_label)
3944 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3945 c_break_label = save_break;
3946 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3949 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3951 while-statement:
3952 while (expression) statement
3955 static void
3956 c_parser_while_statement (c_parser *parser)
3958 tree block, cond, body, save_break, save_cont;
3959 location_t loc;
3960 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3961 c_parser_consume_token (parser);
3962 block = c_begin_compound_stmt (flag_isoc99);
3963 loc = c_parser_peek_token (parser)->location;
3964 cond = c_parser_paren_condition (parser);
3965 save_break = c_break_label;
3966 c_break_label = NULL_TREE;
3967 save_cont = c_cont_label;
3968 c_cont_label = NULL_TREE;
3969 body = c_parser_c99_block_statement (parser);
3970 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3971 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3972 c_break_label = save_break;
3973 c_cont_label = save_cont;
3976 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3978 do-statement:
3979 do statement while ( expression ) ;
3982 static void
3983 c_parser_do_statement (c_parser *parser)
3985 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3986 location_t loc;
3987 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3988 c_parser_consume_token (parser);
3989 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3990 warning (OPT_Wempty_body,
3991 "%Hsuggest braces around empty body in %<do%> statement",
3992 &c_parser_peek_token (parser)->location);
3993 block = c_begin_compound_stmt (flag_isoc99);
3994 loc = c_parser_peek_token (parser)->location;
3995 save_break = c_break_label;
3996 c_break_label = NULL_TREE;
3997 save_cont = c_cont_label;
3998 c_cont_label = NULL_TREE;
3999 body = c_parser_c99_block_statement (parser);
4000 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4001 new_break = c_break_label;
4002 c_break_label = save_break;
4003 new_cont = c_cont_label;
4004 c_cont_label = save_cont;
4005 cond = c_parser_paren_condition (parser);
4006 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4007 c_parser_skip_to_end_of_block_or_statement (parser);
4008 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4009 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4012 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4014 for-statement:
4015 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4016 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4018 The form with a declaration is new in C99.
4020 ??? In accordance with the old parser, the declaration may be a
4021 nested function, which is then rejected in check_for_loop_decls,
4022 but does it make any sense for this to be included in the grammar?
4023 Note in particular that the nested function does not include a
4024 trailing ';', whereas the "declaration" production includes one.
4025 Also, can we reject bad declarations earlier and cheaper than
4026 check_for_loop_decls? */
4028 static void
4029 c_parser_for_statement (c_parser *parser)
4031 tree block, cond, incr, save_break, save_cont, body;
4032 location_t loc;
4033 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4034 loc = c_parser_peek_token (parser)->location;
4035 c_parser_consume_token (parser);
4036 block = c_begin_compound_stmt (flag_isoc99);
4037 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4039 /* Parse the initialization declaration or expression. */
4040 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4042 c_parser_consume_token (parser);
4043 c_finish_expr_stmt (NULL_TREE);
4045 else if (c_parser_next_token_starts_declspecs (parser))
4047 c_parser_declaration_or_fndef (parser, true, true, true, true);
4048 check_for_loop_decls ();
4050 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4052 /* __extension__ can start a declaration, but is also an
4053 unary operator that can start an expression. Consume all
4054 but the last of a possible series of __extension__ to
4055 determine which. */
4056 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4057 && (c_parser_peek_2nd_token (parser)->keyword
4058 == RID_EXTENSION))
4059 c_parser_consume_token (parser);
4060 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4062 int ext;
4063 ext = disable_extension_diagnostics ();
4064 c_parser_consume_token (parser);
4065 c_parser_declaration_or_fndef (parser, true, true, true, true);
4066 restore_extension_diagnostics (ext);
4067 check_for_loop_decls ();
4069 else
4070 goto init_expr;
4072 else
4074 init_expr:
4075 c_finish_expr_stmt (c_parser_expression (parser).value);
4076 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4078 /* Parse the loop condition. */
4079 loc = c_parser_peek_token (parser)->location;
4080 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4082 c_parser_consume_token (parser);
4083 cond = NULL_TREE;
4085 else
4087 tree ocond = c_parser_expression_conv (parser).value;
4088 cond = c_objc_common_truthvalue_conversion (ocond);
4089 if (CAN_HAVE_LOCATION_P (cond))
4090 SET_EXPR_LOCATION (cond, loc);
4091 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4093 /* Parse the increment expression. */
4094 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4095 incr = c_process_expr_stmt (NULL_TREE);
4096 else
4097 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4098 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4100 else
4102 cond = error_mark_node;
4103 incr = error_mark_node;
4105 save_break = c_break_label;
4106 c_break_label = NULL_TREE;
4107 save_cont = c_cont_label;
4108 c_cont_label = NULL_TREE;
4109 body = c_parser_c99_block_statement (parser);
4110 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4111 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4112 c_break_label = save_break;
4113 c_cont_label = save_cont;
4116 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4117 statement with inputs, outputs, clobbers, and volatile tag
4118 allowed.
4120 asm-statement:
4121 asm type-qualifier[opt] ( asm-argument ) ;
4123 asm-argument:
4124 asm-string-literal
4125 asm-string-literal : asm-operands[opt]
4126 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4127 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4129 Qualifiers other than volatile are accepted in the syntax but
4130 warned for. */
4132 static tree
4133 c_parser_asm_statement (c_parser *parser)
4135 tree quals, str, outputs, inputs, clobbers, ret;
4136 bool simple;
4137 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4138 c_parser_consume_token (parser);
4139 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4141 quals = c_parser_peek_token (parser)->value;
4142 c_parser_consume_token (parser);
4144 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4145 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4147 warning (0, "%H%E qualifier ignored on asm",
4148 &c_parser_peek_token (parser)->location,
4149 c_parser_peek_token (parser)->value);
4150 quals = NULL_TREE;
4151 c_parser_consume_token (parser);
4153 else
4154 quals = NULL_TREE;
4155 /* ??? Follow the C++ parser rather than using the
4156 lex_untranslated_string kludge. */
4157 parser->lex_untranslated_string = true;
4158 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4160 parser->lex_untranslated_string = false;
4161 return NULL_TREE;
4163 str = c_parser_asm_string_literal (parser);
4164 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4166 simple = true;
4167 outputs = NULL_TREE;
4168 inputs = NULL_TREE;
4169 clobbers = NULL_TREE;
4170 goto done_asm;
4172 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4174 parser->lex_untranslated_string = false;
4175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4176 return NULL_TREE;
4178 simple = false;
4179 /* Parse outputs. */
4180 if (c_parser_next_token_is (parser, CPP_COLON)
4181 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4182 outputs = NULL_TREE;
4183 else
4184 outputs = c_parser_asm_operands (parser, false);
4185 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4187 inputs = NULL_TREE;
4188 clobbers = NULL_TREE;
4189 goto done_asm;
4191 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4193 parser->lex_untranslated_string = false;
4194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4195 return NULL_TREE;
4197 /* Parse inputs. */
4198 if (c_parser_next_token_is (parser, CPP_COLON)
4199 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4200 inputs = NULL_TREE;
4201 else
4202 inputs = c_parser_asm_operands (parser, true);
4203 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4205 clobbers = NULL_TREE;
4206 goto done_asm;
4208 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4210 parser->lex_untranslated_string = false;
4211 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4212 return NULL_TREE;
4214 /* Parse clobbers. */
4215 clobbers = c_parser_asm_clobbers (parser);
4216 done_asm:
4217 parser->lex_untranslated_string = false;
4218 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4220 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4221 return NULL_TREE;
4223 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4224 c_parser_skip_to_end_of_block_or_statement (parser);
4225 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4226 clobbers, simple));
4227 return ret;
4230 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4231 not outputs), apply the default conversion of functions and arrays
4232 to pointers.
4234 asm-operands:
4235 asm-operand
4236 asm-operands , asm-operand
4238 asm-operand:
4239 asm-string-literal ( expression )
4240 [ identifier ] asm-string-literal ( expression )
4243 static tree
4244 c_parser_asm_operands (c_parser *parser, bool convert_p)
4246 tree list = NULL_TREE;
4247 while (true)
4249 tree name, str;
4250 struct c_expr expr;
4251 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4253 c_parser_consume_token (parser);
4254 if (c_parser_next_token_is (parser, CPP_NAME))
4256 tree id = c_parser_peek_token (parser)->value;
4257 c_parser_consume_token (parser);
4258 name = build_string (IDENTIFIER_LENGTH (id),
4259 IDENTIFIER_POINTER (id));
4261 else
4263 c_parser_error (parser, "expected identifier");
4264 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4265 return NULL_TREE;
4267 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4268 "expected %<]%>");
4270 else
4271 name = NULL_TREE;
4272 str = c_parser_asm_string_literal (parser);
4273 if (str == NULL_TREE)
4274 return NULL_TREE;
4275 parser->lex_untranslated_string = false;
4276 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4278 parser->lex_untranslated_string = true;
4279 return NULL_TREE;
4281 expr = c_parser_expression (parser);
4282 if (convert_p)
4283 expr = default_function_array_conversion (expr);
4284 parser->lex_untranslated_string = true;
4285 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4287 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4288 return NULL_TREE;
4290 list = chainon (list, build_tree_list (build_tree_list (name, str),
4291 expr.value));
4292 if (c_parser_next_token_is (parser, CPP_COMMA))
4293 c_parser_consume_token (parser);
4294 else
4295 break;
4297 return list;
4300 /* Parse asm clobbers, a GNU extension.
4302 asm-clobbers:
4303 asm-string-literal
4304 asm-clobbers , asm-string-literal
4307 static tree
4308 c_parser_asm_clobbers (c_parser *parser)
4310 tree list = NULL_TREE;
4311 while (true)
4313 tree str = c_parser_asm_string_literal (parser);
4314 if (str)
4315 list = tree_cons (NULL_TREE, str, list);
4316 else
4317 return NULL_TREE;
4318 if (c_parser_next_token_is (parser, CPP_COMMA))
4319 c_parser_consume_token (parser);
4320 else
4321 break;
4323 return list;
4326 /* Parse an expression other than a compound expression; that is, an
4327 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4328 NULL then it is an Objective-C message expression which is the
4329 primary-expression starting the expression as an initializer.
4331 assignment-expression:
4332 conditional-expression
4333 unary-expression assignment-operator assignment-expression
4335 assignment-operator: one of
4336 = *= /= %= += -= <<= >>= &= ^= |=
4338 In GNU C we accept any conditional expression on the LHS and
4339 diagnose the invalid lvalue rather than producing a syntax
4340 error. */
4342 static struct c_expr
4343 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4345 struct c_expr lhs, rhs, ret;
4346 enum tree_code code;
4347 gcc_assert (!after || c_dialect_objc ());
4348 lhs = c_parser_conditional_expression (parser, after);
4349 switch (c_parser_peek_token (parser)->type)
4351 case CPP_EQ:
4352 code = NOP_EXPR;
4353 break;
4354 case CPP_MULT_EQ:
4355 code = MULT_EXPR;
4356 break;
4357 case CPP_DIV_EQ:
4358 code = TRUNC_DIV_EXPR;
4359 break;
4360 case CPP_MOD_EQ:
4361 code = TRUNC_MOD_EXPR;
4362 break;
4363 case CPP_PLUS_EQ:
4364 code = PLUS_EXPR;
4365 break;
4366 case CPP_MINUS_EQ:
4367 code = MINUS_EXPR;
4368 break;
4369 case CPP_LSHIFT_EQ:
4370 code = LSHIFT_EXPR;
4371 break;
4372 case CPP_RSHIFT_EQ:
4373 code = RSHIFT_EXPR;
4374 break;
4375 case CPP_AND_EQ:
4376 code = BIT_AND_EXPR;
4377 break;
4378 case CPP_XOR_EQ:
4379 code = BIT_XOR_EXPR;
4380 break;
4381 case CPP_OR_EQ:
4382 code = BIT_IOR_EXPR;
4383 break;
4384 default:
4385 return lhs;
4387 c_parser_consume_token (parser);
4388 rhs = c_parser_expr_no_commas (parser, NULL);
4389 rhs = default_function_array_conversion (rhs);
4390 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4391 if (code == NOP_EXPR)
4392 ret.original_code = MODIFY_EXPR;
4393 else
4395 TREE_NO_WARNING (ret.value) = 1;
4396 ret.original_code = ERROR_MARK;
4398 return ret;
4401 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4402 is not NULL then it is an Objective-C message expression which is
4403 the primary-expression starting the expression as an initializer.
4405 conditional-expression:
4406 logical-OR-expression
4407 logical-OR-expression ? expression : conditional-expression
4409 GNU extensions:
4411 conditional-expression:
4412 logical-OR-expression ? : conditional-expression
4415 static struct c_expr
4416 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4418 struct c_expr cond, exp1, exp2, ret;
4419 gcc_assert (!after || c_dialect_objc ());
4420 cond = c_parser_binary_expression (parser, after);
4421 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4422 return cond;
4423 cond = default_function_array_conversion (cond);
4424 c_parser_consume_token (parser);
4425 if (c_parser_next_token_is (parser, CPP_COLON))
4427 if (pedantic)
4428 pedwarn ("%HISO C forbids omitting the middle term of a ?: expression",
4429 &c_parser_peek_token (parser)->location);
4430 /* Make sure first operand is calculated only once. */
4431 exp1.value = save_expr (default_conversion (cond.value));
4432 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4433 skip_evaluation += cond.value == truthvalue_true_node;
4435 else
4437 cond.value
4438 = c_objc_common_truthvalue_conversion
4439 (default_conversion (cond.value));
4440 skip_evaluation += cond.value == truthvalue_false_node;
4441 exp1 = c_parser_expression_conv (parser);
4442 skip_evaluation += ((cond.value == truthvalue_true_node)
4443 - (cond.value == truthvalue_false_node));
4445 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4447 skip_evaluation -= cond.value == truthvalue_true_node;
4448 ret.value = error_mark_node;
4449 ret.original_code = ERROR_MARK;
4450 return ret;
4452 exp2 = c_parser_conditional_expression (parser, NULL);
4453 exp2 = default_function_array_conversion (exp2);
4454 skip_evaluation -= cond.value == truthvalue_true_node;
4455 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4456 ret.original_code = ERROR_MARK;
4457 return ret;
4460 /* Parse a binary expression; that is, a logical-OR-expression (C90
4461 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4462 an Objective-C message expression which is the primary-expression
4463 starting the expression as an initializer.
4465 multiplicative-expression:
4466 cast-expression
4467 multiplicative-expression * cast-expression
4468 multiplicative-expression / cast-expression
4469 multiplicative-expression % cast-expression
4471 additive-expression:
4472 multiplicative-expression
4473 additive-expression + multiplicative-expression
4474 additive-expression - multiplicative-expression
4476 shift-expression:
4477 additive-expression
4478 shift-expression << additive-expression
4479 shift-expression >> additive-expression
4481 relational-expression:
4482 shift-expression
4483 relational-expression < shift-expression
4484 relational-expression > shift-expression
4485 relational-expression <= shift-expression
4486 relational-expression >= shift-expression
4488 equality-expression:
4489 relational-expression
4490 equality-expression == relational-expression
4491 equality-expression != relational-expression
4493 AND-expression:
4494 equality-expression
4495 AND-expression & equality-expression
4497 exclusive-OR-expression:
4498 AND-expression
4499 exclusive-OR-expression ^ AND-expression
4501 inclusive-OR-expression:
4502 exclusive-OR-expression
4503 inclusive-OR-expression | exclusive-OR-expression
4505 logical-AND-expression:
4506 inclusive-OR-expression
4507 logical-AND-expression && inclusive-OR-expression
4509 logical-OR-expression:
4510 logical-AND-expression
4511 logical-OR-expression || logical-AND-expression
4514 static struct c_expr
4515 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4517 /* A binary expression is parsed using operator-precedence parsing,
4518 with the operands being cast expressions. All the binary
4519 operators are left-associative. Thus a binary expression is of
4520 form:
4522 E0 op1 E1 op2 E2 ...
4524 which we represent on a stack. On the stack, the precedence
4525 levels are strictly increasing. When a new operator is
4526 encountered of higher precedence than that at the top of the
4527 stack, it is pushed; its LHS is the top expression, and its RHS
4528 is everything parsed until it is popped. When a new operator is
4529 encountered with precedence less than or equal to that at the top
4530 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4531 by the result of the operation until the operator at the top of
4532 the stack has lower precedence than the new operator or there is
4533 only one element on the stack; then the top expression is the LHS
4534 of the new operator. In the case of logical AND and OR
4535 expressions, we also need to adjust skip_evaluation as
4536 appropriate when the operators are pushed and popped. */
4538 /* The precedence levels, where 0 is a dummy lowest level used for
4539 the bottom of the stack. */
4540 enum prec {
4541 PREC_NONE,
4542 PREC_LOGOR,
4543 PREC_LOGAND,
4544 PREC_BITOR,
4545 PREC_BITXOR,
4546 PREC_BITAND,
4547 PREC_EQ,
4548 PREC_REL,
4549 PREC_SHIFT,
4550 PREC_ADD,
4551 PREC_MULT,
4552 NUM_PRECS
4554 struct {
4555 /* The expression at this stack level. */
4556 struct c_expr expr;
4557 /* The precedence of the operator on its left, PREC_NONE at the
4558 bottom of the stack. */
4559 enum prec prec;
4560 /* The operation on its left. */
4561 enum tree_code op;
4562 } stack[NUM_PRECS];
4563 int sp;
4564 #define POP \
4565 do { \
4566 switch (stack[sp].op) \
4568 case TRUTH_ANDIF_EXPR: \
4569 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4570 break; \
4571 case TRUTH_ORIF_EXPR: \
4572 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4573 break; \
4574 default: \
4575 break; \
4577 stack[sp - 1].expr \
4578 = default_function_array_conversion (stack[sp - 1].expr); \
4579 stack[sp].expr \
4580 = default_function_array_conversion (stack[sp].expr); \
4581 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4582 stack[sp - 1].expr, \
4583 stack[sp].expr); \
4584 sp--; \
4585 } while (0)
4586 gcc_assert (!after || c_dialect_objc ());
4587 stack[0].expr = c_parser_cast_expression (parser, after);
4588 stack[0].prec = PREC_NONE;
4589 sp = 0;
4590 while (true)
4592 enum prec oprec;
4593 enum tree_code ocode;
4594 if (parser->error)
4595 goto out;
4596 switch (c_parser_peek_token (parser)->type)
4598 case CPP_MULT:
4599 oprec = PREC_MULT;
4600 ocode = MULT_EXPR;
4601 break;
4602 case CPP_DIV:
4603 oprec = PREC_MULT;
4604 ocode = TRUNC_DIV_EXPR;
4605 break;
4606 case CPP_MOD:
4607 oprec = PREC_MULT;
4608 ocode = TRUNC_MOD_EXPR;
4609 break;
4610 case CPP_PLUS:
4611 oprec = PREC_ADD;
4612 ocode = PLUS_EXPR;
4613 break;
4614 case CPP_MINUS:
4615 oprec = PREC_ADD;
4616 ocode = MINUS_EXPR;
4617 break;
4618 case CPP_LSHIFT:
4619 oprec = PREC_SHIFT;
4620 ocode = LSHIFT_EXPR;
4621 break;
4622 case CPP_RSHIFT:
4623 oprec = PREC_SHIFT;
4624 ocode = RSHIFT_EXPR;
4625 break;
4626 case CPP_LESS:
4627 oprec = PREC_REL;
4628 ocode = LT_EXPR;
4629 break;
4630 case CPP_GREATER:
4631 oprec = PREC_REL;
4632 ocode = GT_EXPR;
4633 break;
4634 case CPP_LESS_EQ:
4635 oprec = PREC_REL;
4636 ocode = LE_EXPR;
4637 break;
4638 case CPP_GREATER_EQ:
4639 oprec = PREC_REL;
4640 ocode = GE_EXPR;
4641 break;
4642 case CPP_EQ_EQ:
4643 oprec = PREC_EQ;
4644 ocode = EQ_EXPR;
4645 break;
4646 case CPP_NOT_EQ:
4647 oprec = PREC_EQ;
4648 ocode = NE_EXPR;
4649 break;
4650 case CPP_AND:
4651 oprec = PREC_BITAND;
4652 ocode = BIT_AND_EXPR;
4653 break;
4654 case CPP_XOR:
4655 oprec = PREC_BITXOR;
4656 ocode = BIT_XOR_EXPR;
4657 break;
4658 case CPP_OR:
4659 oprec = PREC_BITOR;
4660 ocode = BIT_IOR_EXPR;
4661 break;
4662 case CPP_AND_AND:
4663 oprec = PREC_LOGAND;
4664 ocode = TRUTH_ANDIF_EXPR;
4665 break;
4666 case CPP_OR_OR:
4667 oprec = PREC_LOGOR;
4668 ocode = TRUTH_ORIF_EXPR;
4669 break;
4670 default:
4671 /* Not a binary operator, so end of the binary
4672 expression. */
4673 goto out;
4675 c_parser_consume_token (parser);
4676 while (oprec <= stack[sp].prec)
4677 POP;
4678 switch (ocode)
4680 case TRUTH_ANDIF_EXPR:
4681 stack[sp].expr
4682 = default_function_array_conversion (stack[sp].expr);
4683 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4684 (default_conversion (stack[sp].expr.value));
4685 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4686 break;
4687 case TRUTH_ORIF_EXPR:
4688 stack[sp].expr
4689 = default_function_array_conversion (stack[sp].expr);
4690 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4691 (default_conversion (stack[sp].expr.value));
4692 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4693 break;
4694 default:
4695 break;
4697 sp++;
4698 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4699 stack[sp].prec = oprec;
4700 stack[sp].op = ocode;
4702 out:
4703 while (sp > 0)
4704 POP;
4705 return stack[0].expr;
4706 #undef POP
4709 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4710 NULL then it is an Objective-C message expression which is the
4711 primary-expression starting the expression as an initializer.
4713 cast-expression:
4714 unary-expression
4715 ( type-name ) unary-expression
4718 static struct c_expr
4719 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4721 gcc_assert (!after || c_dialect_objc ());
4722 if (after)
4723 return c_parser_postfix_expression_after_primary (parser, *after);
4724 /* If the expression begins with a parenthesized type name, it may
4725 be either a cast or a compound literal; we need to see whether
4726 the next character is '{' to tell the difference. If not, it is
4727 an unary expression. */
4728 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4729 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4731 struct c_type_name *type_name;
4732 struct c_expr ret;
4733 struct c_expr expr;
4734 c_parser_consume_token (parser);
4735 type_name = c_parser_type_name (parser);
4736 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4737 if (type_name == NULL)
4739 ret.value = error_mark_node;
4740 ret.original_code = ERROR_MARK;
4741 return ret;
4744 /* Save casted types in the function's used types hash table. */
4745 used_types_insert (type_name->specs->type);
4747 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4748 return c_parser_postfix_expression_after_paren_type (parser,
4749 type_name);
4750 expr = c_parser_cast_expression (parser, NULL);
4751 expr = default_function_array_conversion (expr);
4752 ret.value = c_cast_expr (type_name, expr.value);
4753 ret.original_code = ERROR_MARK;
4754 return ret;
4756 else
4757 return c_parser_unary_expression (parser);
4760 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4762 unary-expression:
4763 postfix-expression
4764 ++ unary-expression
4765 -- unary-expression
4766 unary-operator cast-expression
4767 sizeof unary-expression
4768 sizeof ( type-name )
4770 unary-operator: one of
4771 & * + - ~ !
4773 GNU extensions:
4775 unary-expression:
4776 __alignof__ unary-expression
4777 __alignof__ ( type-name )
4778 && identifier
4780 unary-operator: one of
4781 __extension__ __real__ __imag__
4783 In addition, the GNU syntax treats ++ and -- as unary operators, so
4784 they may be applied to cast expressions with errors for non-lvalues
4785 given later. */
4787 static struct c_expr
4788 c_parser_unary_expression (c_parser *parser)
4790 int ext;
4791 struct c_expr ret, op;
4792 switch (c_parser_peek_token (parser)->type)
4794 case CPP_PLUS_PLUS:
4795 c_parser_consume_token (parser);
4796 op = c_parser_cast_expression (parser, NULL);
4797 op = default_function_array_conversion (op);
4798 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4799 case CPP_MINUS_MINUS:
4800 c_parser_consume_token (parser);
4801 op = c_parser_cast_expression (parser, NULL);
4802 op = default_function_array_conversion (op);
4803 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4804 case CPP_AND:
4805 c_parser_consume_token (parser);
4806 return parser_build_unary_op (ADDR_EXPR,
4807 c_parser_cast_expression (parser, NULL));
4808 case CPP_MULT:
4809 c_parser_consume_token (parser);
4810 op = c_parser_cast_expression (parser, NULL);
4811 op = default_function_array_conversion (op);
4812 ret.value = build_indirect_ref (op.value, "unary *");
4813 ret.original_code = ERROR_MARK;
4814 return ret;
4815 case CPP_PLUS:
4816 if (!c_dialect_objc () && !in_system_header)
4817 warning (OPT_Wtraditional,
4818 "%Htraditional C rejects the unary plus operator",
4819 &c_parser_peek_token (parser)->location);
4820 c_parser_consume_token (parser);
4821 op = c_parser_cast_expression (parser, NULL);
4822 op = default_function_array_conversion (op);
4823 return parser_build_unary_op (CONVERT_EXPR, op);
4824 case CPP_MINUS:
4825 c_parser_consume_token (parser);
4826 op = c_parser_cast_expression (parser, NULL);
4827 op = default_function_array_conversion (op);
4828 return parser_build_unary_op (NEGATE_EXPR, op);
4829 case CPP_COMPL:
4830 c_parser_consume_token (parser);
4831 op = c_parser_cast_expression (parser, NULL);
4832 op = default_function_array_conversion (op);
4833 return parser_build_unary_op (BIT_NOT_EXPR, op);
4834 case CPP_NOT:
4835 c_parser_consume_token (parser);
4836 op = c_parser_cast_expression (parser, NULL);
4837 op = default_function_array_conversion (op);
4838 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4839 case CPP_AND_AND:
4840 /* Refer to the address of a label as a pointer. */
4841 c_parser_consume_token (parser);
4842 if (c_parser_next_token_is (parser, CPP_NAME))
4844 ret.value = finish_label_address_expr
4845 (c_parser_peek_token (parser)->value);
4846 c_parser_consume_token (parser);
4848 else
4850 c_parser_error (parser, "expected identifier");
4851 ret.value = error_mark_node;
4853 ret.original_code = ERROR_MARK;
4854 return ret;
4855 case CPP_KEYWORD:
4856 switch (c_parser_peek_token (parser)->keyword)
4858 case RID_SIZEOF:
4859 return c_parser_sizeof_expression (parser);
4860 case RID_ALIGNOF:
4861 return c_parser_alignof_expression (parser);
4862 case RID_EXTENSION:
4863 c_parser_consume_token (parser);
4864 ext = disable_extension_diagnostics ();
4865 ret = c_parser_cast_expression (parser, NULL);
4866 restore_extension_diagnostics (ext);
4867 return ret;
4868 case RID_REALPART:
4869 c_parser_consume_token (parser);
4870 op = c_parser_cast_expression (parser, NULL);
4871 op = default_function_array_conversion (op);
4872 return parser_build_unary_op (REALPART_EXPR, op);
4873 case RID_IMAGPART:
4874 c_parser_consume_token (parser);
4875 op = c_parser_cast_expression (parser, NULL);
4876 op = default_function_array_conversion (op);
4877 return parser_build_unary_op (IMAGPART_EXPR, op);
4878 default:
4879 return c_parser_postfix_expression (parser);
4881 default:
4882 return c_parser_postfix_expression (parser);
4886 /* Parse a sizeof expression. */
4888 static struct c_expr
4889 c_parser_sizeof_expression (c_parser *parser)
4891 struct c_expr expr;
4892 location_t expr_loc;
4893 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4894 c_parser_consume_token (parser);
4895 skip_evaluation++;
4896 in_sizeof++;
4897 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4898 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4900 /* Either sizeof ( type-name ) or sizeof unary-expression
4901 starting with a compound literal. */
4902 struct c_type_name *type_name;
4903 c_parser_consume_token (parser);
4904 expr_loc = c_parser_peek_token (parser)->location;
4905 type_name = c_parser_type_name (parser);
4906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4907 if (type_name == NULL)
4909 struct c_expr ret;
4910 skip_evaluation--;
4911 in_sizeof--;
4912 ret.value = error_mark_node;
4913 ret.original_code = ERROR_MARK;
4914 return ret;
4916 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4918 expr = c_parser_postfix_expression_after_paren_type (parser,
4919 type_name);
4920 goto sizeof_expr;
4922 /* sizeof ( type-name ). */
4923 skip_evaluation--;
4924 in_sizeof--;
4925 if (type_name->declarator->kind == cdk_array
4926 && type_name->declarator->u.array.vla_unspec_p)
4928 /* C99 6.7.5.2p4 */
4929 error ("%H%<[*]%> not allowed in other than a declaration",
4930 &expr_loc);
4932 return c_expr_sizeof_type (type_name);
4934 else
4936 expr_loc = c_parser_peek_token (parser)->location;
4937 expr = c_parser_unary_expression (parser);
4938 sizeof_expr:
4939 skip_evaluation--;
4940 in_sizeof--;
4941 if (TREE_CODE (expr.value) == COMPONENT_REF
4942 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4943 error ("%H%<sizeof%> applied to a bit-field", &expr_loc);
4944 return c_expr_sizeof_expr (expr);
4948 /* Parse an alignof expression. */
4950 static struct c_expr
4951 c_parser_alignof_expression (c_parser *parser)
4953 struct c_expr expr;
4954 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4955 c_parser_consume_token (parser);
4956 skip_evaluation++;
4957 in_alignof++;
4958 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4959 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4961 /* Either __alignof__ ( type-name ) or __alignof__
4962 unary-expression starting with a compound literal. */
4963 struct c_type_name *type_name;
4964 struct c_expr ret;
4965 c_parser_consume_token (parser);
4966 type_name = c_parser_type_name (parser);
4967 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4968 if (type_name == NULL)
4970 struct c_expr ret;
4971 skip_evaluation--;
4972 in_alignof--;
4973 ret.value = error_mark_node;
4974 ret.original_code = ERROR_MARK;
4975 return ret;
4977 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4979 expr = c_parser_postfix_expression_after_paren_type (parser,
4980 type_name);
4981 goto alignof_expr;
4983 /* alignof ( type-name ). */
4984 skip_evaluation--;
4985 in_alignof--;
4986 ret.value = c_alignof (groktypename (type_name));
4987 ret.original_code = ERROR_MARK;
4988 return ret;
4990 else
4992 struct c_expr ret;
4993 expr = c_parser_unary_expression (parser);
4994 alignof_expr:
4995 skip_evaluation--;
4996 in_alignof--;
4997 ret.value = c_alignof_expr (expr.value);
4998 ret.original_code = ERROR_MARK;
4999 return ret;
5003 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5005 postfix-expression:
5006 primary-expression
5007 postfix-expression [ expression ]
5008 postfix-expression ( argument-expression-list[opt] )
5009 postfix-expression . identifier
5010 postfix-expression -> identifier
5011 postfix-expression ++
5012 postfix-expression --
5013 ( type-name ) { initializer-list }
5014 ( type-name ) { initializer-list , }
5016 argument-expression-list:
5017 argument-expression
5018 argument-expression-list , argument-expression
5020 primary-expression:
5021 identifier
5022 constant
5023 string-literal
5024 ( expression )
5026 GNU extensions:
5028 primary-expression:
5029 __func__
5030 (treated as a keyword in GNU C)
5031 __FUNCTION__
5032 __PRETTY_FUNCTION__
5033 ( compound-statement )
5034 __builtin_va_arg ( assignment-expression , type-name )
5035 __builtin_offsetof ( type-name , offsetof-member-designator )
5036 __builtin_choose_expr ( assignment-expression ,
5037 assignment-expression ,
5038 assignment-expression )
5039 __builtin_types_compatible_p ( type-name , type-name )
5041 offsetof-member-designator:
5042 identifier
5043 offsetof-member-designator . identifier
5044 offsetof-member-designator [ expression ]
5046 Objective-C:
5048 primary-expression:
5049 [ objc-receiver objc-message-args ]
5050 @selector ( objc-selector-arg )
5051 @protocol ( identifier )
5052 @encode ( type-name )
5053 objc-string-literal
5056 static struct c_expr
5057 c_parser_postfix_expression (c_parser *parser)
5059 struct c_expr expr, e1, e2, e3;
5060 struct c_type_name *t1, *t2;
5061 location_t loc;
5062 switch (c_parser_peek_token (parser)->type)
5064 case CPP_NUMBER:
5065 case CPP_CHAR:
5066 case CPP_CHAR16:
5067 case CPP_CHAR32:
5068 case CPP_WCHAR:
5069 expr.value = c_parser_peek_token (parser)->value;
5070 expr.original_code = ERROR_MARK;
5071 c_parser_consume_token (parser);
5072 break;
5073 case CPP_STRING:
5074 case CPP_STRING16:
5075 case CPP_STRING32:
5076 case CPP_WSTRING:
5077 expr.value = c_parser_peek_token (parser)->value;
5078 expr.original_code = STRING_CST;
5079 c_parser_consume_token (parser);
5080 break;
5081 case CPP_OBJC_STRING:
5082 gcc_assert (c_dialect_objc ());
5083 expr.value
5084 = objc_build_string_object (c_parser_peek_token (parser)->value);
5085 expr.original_code = ERROR_MARK;
5086 c_parser_consume_token (parser);
5087 break;
5088 case CPP_NAME:
5089 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5091 c_parser_error (parser, "expected expression");
5092 expr.value = error_mark_node;
5093 expr.original_code = ERROR_MARK;
5094 break;
5097 tree id = c_parser_peek_token (parser)->value;
5098 location_t loc = c_parser_peek_token (parser)->location;
5099 c_parser_consume_token (parser);
5100 expr.value = build_external_ref (id,
5101 (c_parser_peek_token (parser)->type
5102 == CPP_OPEN_PAREN), loc);
5103 expr.original_code = ERROR_MARK;
5105 break;
5106 case CPP_OPEN_PAREN:
5107 /* A parenthesized expression, statement expression or compound
5108 literal. */
5109 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5111 /* A statement expression. */
5112 tree stmt;
5113 location_t here = c_parser_peek_token (parser)->location;
5114 c_parser_consume_token (parser);
5115 c_parser_consume_token (parser);
5116 if (cur_stmt_list == NULL)
5118 error ("%Hbraced-group within expression allowed "
5119 "only inside a function", &here);
5120 parser->error = true;
5121 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5123 expr.value = error_mark_node;
5124 expr.original_code = ERROR_MARK;
5125 break;
5127 stmt = c_begin_stmt_expr ();
5128 c_parser_compound_statement_nostart (parser);
5129 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5130 "expected %<)%>");
5131 if (pedantic)
5132 pedwarn ("%HISO C forbids braced-groups within expressions",
5133 &here);
5134 expr.value = c_finish_stmt_expr (stmt);
5135 expr.original_code = ERROR_MARK;
5137 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5139 /* A compound literal. ??? Can we actually get here rather
5140 than going directly to
5141 c_parser_postfix_expression_after_paren_type from
5142 elsewhere? */
5143 struct c_type_name *type_name;
5144 c_parser_consume_token (parser);
5145 type_name = c_parser_type_name (parser);
5146 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5147 "expected %<)%>");
5148 if (type_name == NULL)
5150 expr.value = error_mark_node;
5151 expr.original_code = ERROR_MARK;
5153 else
5154 expr = c_parser_postfix_expression_after_paren_type (parser,
5155 type_name);
5157 else
5159 /* A parenthesized expression. */
5160 c_parser_consume_token (parser);
5161 expr = c_parser_expression (parser);
5162 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5163 TREE_NO_WARNING (expr.value) = 1;
5164 expr.original_code = ERROR_MARK;
5165 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5166 "expected %<)%>");
5168 break;
5169 case CPP_KEYWORD:
5170 switch (c_parser_peek_token (parser)->keyword)
5172 case RID_FUNCTION_NAME:
5173 case RID_PRETTY_FUNCTION_NAME:
5174 case RID_C99_FUNCTION_NAME:
5175 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
5176 c_parser_peek_token (parser)->value);
5177 expr.original_code = ERROR_MARK;
5178 c_parser_consume_token (parser);
5179 break;
5180 case RID_VA_ARG:
5181 c_parser_consume_token (parser);
5182 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5184 expr.value = error_mark_node;
5185 expr.original_code = ERROR_MARK;
5186 break;
5188 e1 = c_parser_expr_no_commas (parser, NULL);
5189 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5191 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5192 expr.value = error_mark_node;
5193 expr.original_code = ERROR_MARK;
5194 break;
5196 t1 = c_parser_type_name (parser);
5197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5198 "expected %<)%>");
5199 if (t1 == NULL)
5201 expr.value = error_mark_node;
5202 expr.original_code = ERROR_MARK;
5204 else
5206 expr.value = build_va_arg (e1.value, groktypename (t1));
5207 expr.original_code = ERROR_MARK;
5209 break;
5210 case RID_OFFSETOF:
5211 c_parser_consume_token (parser);
5212 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5214 expr.value = error_mark_node;
5215 expr.original_code = ERROR_MARK;
5216 break;
5218 t1 = c_parser_type_name (parser);
5219 if (t1 == NULL)
5221 expr.value = error_mark_node;
5222 expr.original_code = ERROR_MARK;
5223 break;
5225 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5227 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5228 expr.value = error_mark_node;
5229 expr.original_code = ERROR_MARK;
5230 break;
5233 tree type = groktypename (t1);
5234 tree offsetof_ref;
5235 if (type == error_mark_node)
5236 offsetof_ref = error_mark_node;
5237 else
5238 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5239 /* Parse the second argument to __builtin_offsetof. We
5240 must have one identifier, and beyond that we want to
5241 accept sub structure and sub array references. */
5242 if (c_parser_next_token_is (parser, CPP_NAME))
5244 offsetof_ref = build_component_ref
5245 (offsetof_ref, c_parser_peek_token (parser)->value);
5246 c_parser_consume_token (parser);
5247 while (c_parser_next_token_is (parser, CPP_DOT)
5248 || c_parser_next_token_is (parser,
5249 CPP_OPEN_SQUARE))
5251 if (c_parser_next_token_is (parser, CPP_DOT))
5253 c_parser_consume_token (parser);
5254 if (c_parser_next_token_is_not (parser,
5255 CPP_NAME))
5257 c_parser_error (parser, "expected identifier");
5258 break;
5260 offsetof_ref = build_component_ref
5261 (offsetof_ref,
5262 c_parser_peek_token (parser)->value);
5263 c_parser_consume_token (parser);
5265 else
5267 tree idx;
5268 c_parser_consume_token (parser);
5269 idx = c_parser_expression (parser).value;
5270 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5271 "expected %<]%>");
5272 offsetof_ref = build_array_ref (offsetof_ref, idx);
5276 else
5277 c_parser_error (parser, "expected identifier");
5278 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5279 "expected %<)%>");
5280 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5281 expr.original_code = ERROR_MARK;
5283 break;
5284 case RID_CHOOSE_EXPR:
5285 c_parser_consume_token (parser);
5286 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5288 expr.value = error_mark_node;
5289 expr.original_code = ERROR_MARK;
5290 break;
5292 loc = c_parser_peek_token (parser)->location;
5293 e1 = c_parser_expr_no_commas (parser, NULL);
5294 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5297 expr.value = error_mark_node;
5298 expr.original_code = ERROR_MARK;
5299 break;
5301 e2 = c_parser_expr_no_commas (parser, NULL);
5302 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5305 expr.value = error_mark_node;
5306 expr.original_code = ERROR_MARK;
5307 break;
5309 e3 = c_parser_expr_no_commas (parser, NULL);
5310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5311 "expected %<)%>");
5313 tree c;
5315 c = fold (e1.value);
5316 if (TREE_CODE (c) != INTEGER_CST)
5317 error ("%Hfirst argument to %<__builtin_choose_expr%> not"
5318 " a constant", &loc);
5319 expr = integer_zerop (c) ? e3 : e2;
5321 break;
5322 case RID_TYPES_COMPATIBLE_P:
5323 c_parser_consume_token (parser);
5324 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5326 expr.value = error_mark_node;
5327 expr.original_code = ERROR_MARK;
5328 break;
5330 t1 = c_parser_type_name (parser);
5331 if (t1 == NULL)
5333 expr.value = error_mark_node;
5334 expr.original_code = ERROR_MARK;
5335 break;
5337 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5339 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5340 expr.value = error_mark_node;
5341 expr.original_code = ERROR_MARK;
5342 break;
5344 t2 = c_parser_type_name (parser);
5345 if (t2 == NULL)
5347 expr.value = error_mark_node;
5348 expr.original_code = ERROR_MARK;
5349 break;
5351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5352 "expected %<)%>");
5354 tree e1, e2;
5356 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5357 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5359 expr.value = comptypes (e1, e2)
5360 ? build_int_cst (NULL_TREE, 1)
5361 : build_int_cst (NULL_TREE, 0);
5362 expr.original_code = ERROR_MARK;
5364 break;
5365 case RID_AT_SELECTOR:
5366 gcc_assert (c_dialect_objc ());
5367 c_parser_consume_token (parser);
5368 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5370 expr.value = error_mark_node;
5371 expr.original_code = ERROR_MARK;
5372 break;
5375 tree sel = c_parser_objc_selector_arg (parser);
5376 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5377 "expected %<)%>");
5378 expr.value = objc_build_selector_expr (sel);
5379 expr.original_code = ERROR_MARK;
5381 break;
5382 case RID_AT_PROTOCOL:
5383 gcc_assert (c_dialect_objc ());
5384 c_parser_consume_token (parser);
5385 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5387 expr.value = error_mark_node;
5388 expr.original_code = ERROR_MARK;
5389 break;
5391 if (c_parser_next_token_is_not (parser, CPP_NAME))
5393 c_parser_error (parser, "expected identifier");
5394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5395 expr.value = error_mark_node;
5396 expr.original_code = ERROR_MARK;
5397 break;
5400 tree id = c_parser_peek_token (parser)->value;
5401 c_parser_consume_token (parser);
5402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5403 "expected %<)%>");
5404 expr.value = objc_build_protocol_expr (id);
5405 expr.original_code = ERROR_MARK;
5407 break;
5408 case RID_AT_ENCODE:
5409 /* Extension to support C-structures in the archiver. */
5410 gcc_assert (c_dialect_objc ());
5411 c_parser_consume_token (parser);
5412 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5414 expr.value = error_mark_node;
5415 expr.original_code = ERROR_MARK;
5416 break;
5418 t1 = c_parser_type_name (parser);
5419 if (t1 == NULL)
5421 expr.value = error_mark_node;
5422 expr.original_code = ERROR_MARK;
5423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5424 break;
5426 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5427 "expected %<)%>");
5429 tree type = groktypename (t1);
5430 expr.value = objc_build_encode_expr (type);
5431 expr.original_code = ERROR_MARK;
5433 break;
5434 default:
5435 c_parser_error (parser, "expected expression");
5436 expr.value = error_mark_node;
5437 expr.original_code = ERROR_MARK;
5438 break;
5440 break;
5441 case CPP_OPEN_SQUARE:
5442 if (c_dialect_objc ())
5444 tree receiver, args;
5445 c_parser_consume_token (parser);
5446 receiver = c_parser_objc_receiver (parser);
5447 args = c_parser_objc_message_args (parser);
5448 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5449 "expected %<]%>");
5450 expr.value = objc_build_message_expr (build_tree_list (receiver,
5451 args));
5452 expr.original_code = ERROR_MARK;
5453 break;
5455 /* Else fall through to report error. */
5456 default:
5457 c_parser_error (parser, "expected expression");
5458 expr.value = error_mark_node;
5459 expr.original_code = ERROR_MARK;
5460 break;
5462 return c_parser_postfix_expression_after_primary (parser, expr);
5465 /* Parse a postfix expression after a parenthesized type name: the
5466 brace-enclosed initializer of a compound literal, possibly followed
5467 by some postfix operators. This is separate because it is not
5468 possible to tell until after the type name whether a cast
5469 expression has a cast or a compound literal, or whether the operand
5470 of sizeof is a parenthesized type name or starts with a compound
5471 literal. */
5473 static struct c_expr
5474 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5475 struct c_type_name *type_name)
5477 tree type;
5478 struct c_expr init;
5479 struct c_expr expr;
5480 location_t start_loc;
5481 start_init (NULL_TREE, NULL, 0);
5482 type = groktypename (type_name);
5483 start_loc = c_parser_peek_token (parser)->location;
5484 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5486 error ("%Hcompound literal has variable size", &start_loc);
5487 type = error_mark_node;
5489 init = c_parser_braced_init (parser, type, false);
5490 finish_init ();
5491 maybe_warn_string_init (type, init);
5493 if (pedantic && !flag_isoc99)
5494 pedwarn ("%HISO C90 forbids compound literals", &start_loc);
5495 expr.value = build_compound_literal (type, init.value);
5496 expr.original_code = ERROR_MARK;
5497 return c_parser_postfix_expression_after_primary (parser, expr);
5500 /* Parse a postfix expression after the initial primary or compound
5501 literal; that is, parse a series of postfix operators. */
5503 static struct c_expr
5504 c_parser_postfix_expression_after_primary (c_parser *parser,
5505 struct c_expr expr)
5507 tree ident, idx, exprlist;
5508 while (true)
5510 switch (c_parser_peek_token (parser)->type)
5512 case CPP_OPEN_SQUARE:
5513 /* Array reference. */
5514 c_parser_consume_token (parser);
5515 idx = c_parser_expression (parser).value;
5516 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5517 "expected %<]%>");
5518 expr.value = build_array_ref (expr.value, idx);
5519 expr.original_code = ERROR_MARK;
5520 break;
5521 case CPP_OPEN_PAREN:
5522 /* Function call. */
5523 c_parser_consume_token (parser);
5524 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5525 exprlist = NULL_TREE;
5526 else
5527 exprlist = c_parser_expr_list (parser, true);
5528 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5529 "expected %<)%>");
5530 expr.value = build_function_call (expr.value, exprlist);
5531 expr.original_code = ERROR_MARK;
5532 if (warn_disallowed_functions)
5533 warn_if_disallowed_function_p (expr.value);
5534 break;
5535 case CPP_DOT:
5536 /* Structure element reference. */
5537 c_parser_consume_token (parser);
5538 expr = default_function_array_conversion (expr);
5539 if (c_parser_next_token_is (parser, CPP_NAME))
5540 ident = c_parser_peek_token (parser)->value;
5541 else
5543 c_parser_error (parser, "expected identifier");
5544 expr.value = error_mark_node;
5545 expr.original_code = ERROR_MARK;
5546 return expr;
5548 c_parser_consume_token (parser);
5549 expr.value = build_component_ref (expr.value, ident);
5550 expr.original_code = ERROR_MARK;
5551 break;
5552 case CPP_DEREF:
5553 /* Structure element reference. */
5554 c_parser_consume_token (parser);
5555 expr = default_function_array_conversion (expr);
5556 if (c_parser_next_token_is (parser, CPP_NAME))
5557 ident = c_parser_peek_token (parser)->value;
5558 else
5560 c_parser_error (parser, "expected identifier");
5561 expr.value = error_mark_node;
5562 expr.original_code = ERROR_MARK;
5563 return expr;
5565 c_parser_consume_token (parser);
5566 expr.value = build_component_ref (build_indirect_ref (expr.value,
5567 "->"), ident);
5568 expr.original_code = ERROR_MARK;
5569 break;
5570 case CPP_PLUS_PLUS:
5571 /* Postincrement. */
5572 c_parser_consume_token (parser);
5573 expr = default_function_array_conversion (expr);
5574 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5575 expr.original_code = ERROR_MARK;
5576 break;
5577 case CPP_MINUS_MINUS:
5578 /* Postdecrement. */
5579 c_parser_consume_token (parser);
5580 expr = default_function_array_conversion (expr);
5581 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5582 expr.original_code = ERROR_MARK;
5583 break;
5584 default:
5585 return expr;
5590 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5592 expression:
5593 assignment-expression
5594 expression , assignment-expression
5597 static struct c_expr
5598 c_parser_expression (c_parser *parser)
5600 struct c_expr expr;
5601 expr = c_parser_expr_no_commas (parser, NULL);
5602 while (c_parser_next_token_is (parser, CPP_COMMA))
5604 struct c_expr next;
5605 c_parser_consume_token (parser);
5606 next = c_parser_expr_no_commas (parser, NULL);
5607 next = default_function_array_conversion (next);
5608 expr.value = build_compound_expr (expr.value, next.value);
5609 expr.original_code = COMPOUND_EXPR;
5611 return expr;
5614 /* Parse an expression and convert functions or arrays to
5615 pointers. */
5617 static struct c_expr
5618 c_parser_expression_conv (c_parser *parser)
5620 struct c_expr expr;
5621 expr = c_parser_expression (parser);
5622 expr = default_function_array_conversion (expr);
5623 return expr;
5626 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5627 functions and arrays to pointers.
5629 nonempty-expr-list:
5630 assignment-expression
5631 nonempty-expr-list , assignment-expression
5634 static tree
5635 c_parser_expr_list (c_parser *parser, bool convert_p)
5637 struct c_expr expr;
5638 tree ret, cur;
5639 expr = c_parser_expr_no_commas (parser, NULL);
5640 if (convert_p)
5641 expr = default_function_array_conversion (expr);
5642 ret = cur = build_tree_list (NULL_TREE, expr.value);
5643 while (c_parser_next_token_is (parser, CPP_COMMA))
5645 c_parser_consume_token (parser);
5646 expr = c_parser_expr_no_commas (parser, NULL);
5647 if (convert_p)
5648 expr = default_function_array_conversion (expr);
5649 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5651 return ret;
5655 /* Parse Objective-C-specific constructs. */
5657 /* Parse an objc-class-definition.
5659 objc-class-definition:
5660 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5661 objc-class-instance-variables[opt] objc-methodprotolist @end
5662 @implementation identifier objc-superclass[opt]
5663 objc-class-instance-variables[opt]
5664 @interface identifier ( identifier ) objc-protocol-refs[opt]
5665 objc-methodprotolist @end
5666 @implementation identifier ( identifier )
5668 objc-superclass:
5669 : identifier
5671 "@interface identifier (" must start "@interface identifier (
5672 identifier ) ...": objc-methodprotolist in the first production may
5673 not start with a parenthesized identifier as a declarator of a data
5674 definition with no declaration specifiers if the objc-superclass,
5675 objc-protocol-refs and objc-class-instance-variables are omitted. */
5677 static void
5678 c_parser_objc_class_definition (c_parser *parser)
5680 bool iface_p;
5681 tree id1;
5682 tree superclass;
5683 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5684 iface_p = true;
5685 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5686 iface_p = false;
5687 else
5688 gcc_unreachable ();
5689 c_parser_consume_token (parser);
5690 if (c_parser_next_token_is_not (parser, CPP_NAME))
5692 c_parser_error (parser, "expected identifier");
5693 return;
5695 id1 = c_parser_peek_token (parser)->value;
5696 c_parser_consume_token (parser);
5697 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5699 tree id2;
5700 tree proto = NULL_TREE;
5701 c_parser_consume_token (parser);
5702 if (c_parser_next_token_is_not (parser, CPP_NAME))
5704 c_parser_error (parser, "expected identifier");
5705 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5706 return;
5708 id2 = c_parser_peek_token (parser)->value;
5709 c_parser_consume_token (parser);
5710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5711 if (!iface_p)
5713 objc_start_category_implementation (id1, id2);
5714 return;
5716 if (c_parser_next_token_is (parser, CPP_LESS))
5717 proto = c_parser_objc_protocol_refs (parser);
5718 objc_start_category_interface (id1, id2, proto);
5719 c_parser_objc_methodprotolist (parser);
5720 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5721 objc_finish_interface ();
5722 return;
5724 if (c_parser_next_token_is (parser, CPP_COLON))
5726 c_parser_consume_token (parser);
5727 if (c_parser_next_token_is_not (parser, CPP_NAME))
5729 c_parser_error (parser, "expected identifier");
5730 return;
5732 superclass = c_parser_peek_token (parser)->value;
5733 c_parser_consume_token (parser);
5735 else
5736 superclass = NULL_TREE;
5737 if (iface_p)
5739 tree proto = NULL_TREE;
5740 if (c_parser_next_token_is (parser, CPP_LESS))
5741 proto = c_parser_objc_protocol_refs (parser);
5742 objc_start_class_interface (id1, superclass, proto);
5744 else
5745 objc_start_class_implementation (id1, superclass);
5746 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5747 c_parser_objc_class_instance_variables (parser);
5748 if (iface_p)
5750 objc_continue_interface ();
5751 c_parser_objc_methodprotolist (parser);
5752 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5753 objc_finish_interface ();
5755 else
5757 objc_continue_implementation ();
5758 return;
5762 /* Parse objc-class-instance-variables.
5764 objc-class-instance-variables:
5765 { objc-instance-variable-decl-list[opt] }
5767 objc-instance-variable-decl-list:
5768 objc-visibility-spec
5769 objc-instance-variable-decl ;
5771 objc-instance-variable-decl-list objc-visibility-spec
5772 objc-instance-variable-decl-list objc-instance-variable-decl ;
5773 objc-instance-variable-decl-list ;
5775 objc-visibility-spec:
5776 @private
5777 @protected
5778 @public
5780 objc-instance-variable-decl:
5781 struct-declaration
5784 static void
5785 c_parser_objc_class_instance_variables (c_parser *parser)
5787 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5788 c_parser_consume_token (parser);
5789 while (c_parser_next_token_is_not (parser, CPP_EOF))
5791 tree decls;
5792 /* Parse any stray semicolon. */
5793 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5795 if (pedantic)
5796 pedwarn ("%Hextra semicolon in struct or union specified",
5797 &c_parser_peek_token (parser)->location);
5798 c_parser_consume_token (parser);
5799 continue;
5801 /* Stop if at the end of the instance variables. */
5802 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5804 c_parser_consume_token (parser);
5805 break;
5807 /* Parse any objc-visibility-spec. */
5808 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
5810 c_parser_consume_token (parser);
5811 objc_set_visibility (2);
5812 continue;
5814 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
5816 c_parser_consume_token (parser);
5817 objc_set_visibility (0);
5818 continue;
5820 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
5822 c_parser_consume_token (parser);
5823 objc_set_visibility (1);
5824 continue;
5826 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5828 c_parser_pragma (parser, pragma_external);
5829 continue;
5832 /* Parse some comma-separated declarations. */
5833 decls = c_parser_struct_declaration (parser);
5835 /* Comma-separated instance variables are chained together in
5836 reverse order; add them one by one. */
5837 tree ivar = nreverse (decls);
5838 for (; ivar; ivar = TREE_CHAIN (ivar))
5839 objc_add_instance_variable (copy_node (ivar));
5841 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5845 /* Parse an objc-class-declaration.
5847 objc-class-declaration:
5848 @class identifier-list ;
5851 static void
5852 c_parser_objc_class_declaration (c_parser *parser)
5854 tree list = NULL_TREE;
5855 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
5856 c_parser_consume_token (parser);
5857 /* Any identifiers, including those declared as type names, are OK
5858 here. */
5859 while (true)
5861 tree id;
5862 if (c_parser_next_token_is_not (parser, CPP_NAME))
5864 c_parser_error (parser, "expected identifier");
5865 break;
5867 id = c_parser_peek_token (parser)->value;
5868 list = chainon (list, build_tree_list (NULL_TREE, id));
5869 c_parser_consume_token (parser);
5870 if (c_parser_next_token_is (parser, CPP_COMMA))
5871 c_parser_consume_token (parser);
5872 else
5873 break;
5875 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5876 objc_declare_class (list);
5879 /* Parse an objc-alias-declaration.
5881 objc-alias-declaration:
5882 @compatibility_alias identifier identifier ;
5885 static void
5886 c_parser_objc_alias_declaration (c_parser *parser)
5888 tree id1, id2;
5889 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5890 c_parser_consume_token (parser);
5891 if (c_parser_next_token_is_not (parser, CPP_NAME))
5893 c_parser_error (parser, "expected identifier");
5894 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5895 return;
5897 id1 = c_parser_peek_token (parser)->value;
5898 c_parser_consume_token (parser);
5899 if (c_parser_next_token_is_not (parser, CPP_NAME))
5901 c_parser_error (parser, "expected identifier");
5902 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5903 return;
5905 id2 = c_parser_peek_token (parser)->value;
5906 c_parser_consume_token (parser);
5907 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5908 objc_declare_alias (id1, id2);
5911 /* Parse an objc-protocol-definition.
5913 objc-protocol-definition:
5914 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5915 @protocol identifier-list ;
5917 "@protocol identifier ;" should be resolved as "@protocol
5918 identifier-list ;": objc-methodprotolist may not start with a
5919 semicolon in the first alternative if objc-protocol-refs are
5920 omitted. */
5922 static void
5923 c_parser_objc_protocol_definition (c_parser *parser)
5925 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5926 c_parser_consume_token (parser);
5927 if (c_parser_next_token_is_not (parser, CPP_NAME))
5929 c_parser_error (parser, "expected identifier");
5930 return;
5932 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5933 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5935 tree list = NULL_TREE;
5936 /* Any identifiers, including those declared as type names, are
5937 OK here. */
5938 while (true)
5940 tree id;
5941 if (c_parser_next_token_is_not (parser, CPP_NAME))
5943 c_parser_error (parser, "expected identifier");
5944 break;
5946 id = c_parser_peek_token (parser)->value;
5947 list = chainon (list, build_tree_list (NULL_TREE, id));
5948 c_parser_consume_token (parser);
5949 if (c_parser_next_token_is (parser, CPP_COMMA))
5950 c_parser_consume_token (parser);
5951 else
5952 break;
5954 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5955 objc_declare_protocols (list);
5957 else
5959 tree id = c_parser_peek_token (parser)->value;
5960 tree proto = NULL_TREE;
5961 c_parser_consume_token (parser);
5962 if (c_parser_next_token_is (parser, CPP_LESS))
5963 proto = c_parser_objc_protocol_refs (parser);
5964 parser->objc_pq_context = true;
5965 objc_start_protocol (id, proto);
5966 c_parser_objc_methodprotolist (parser);
5967 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5968 parser->objc_pq_context = false;
5969 objc_finish_interface ();
5973 /* Parse an objc-method-type.
5975 objc-method-type:
5980 static enum tree_code
5981 c_parser_objc_method_type (c_parser *parser)
5983 switch (c_parser_peek_token (parser)->type)
5985 case CPP_PLUS:
5986 c_parser_consume_token (parser);
5987 return PLUS_EXPR;
5988 case CPP_MINUS:
5989 c_parser_consume_token (parser);
5990 return MINUS_EXPR;
5991 default:
5992 gcc_unreachable ();
5996 /* Parse an objc-method-definition.
5998 objc-method-definition:
5999 objc-method-type objc-method-decl ;[opt] compound-statement
6002 static void
6003 c_parser_objc_method_definition (c_parser *parser)
6005 enum tree_code type = c_parser_objc_method_type (parser);
6006 tree decl;
6007 objc_set_method_type (type);
6008 parser->objc_pq_context = true;
6009 decl = c_parser_objc_method_decl (parser);
6010 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6012 c_parser_consume_token (parser);
6013 if (pedantic)
6014 pedwarn ("%Hextra semicolon in method definition specified",
6015 &c_parser_peek_token (parser)->location);
6017 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6019 c_parser_error (parser, "expected %<{%>");
6020 return;
6022 parser->objc_pq_context = false;
6023 objc_start_method_definition (decl);
6024 add_stmt (c_parser_compound_statement (parser));
6025 objc_finish_method_definition (current_function_decl);
6028 /* Parse an objc-methodprotolist.
6030 objc-methodprotolist:
6031 empty
6032 objc-methodprotolist objc-methodproto
6033 objc-methodprotolist declaration
6034 objc-methodprotolist ;
6036 The declaration is a data definition, which may be missing
6037 declaration specifiers under the same rules and diagnostics as
6038 other data definitions outside functions, and the stray semicolon
6039 is diagnosed the same way as a stray semicolon outside a
6040 function. */
6042 static void
6043 c_parser_objc_methodprotolist (c_parser *parser)
6045 while (true)
6047 /* The list is terminated by @end. */
6048 switch (c_parser_peek_token (parser)->type)
6050 case CPP_SEMICOLON:
6051 if (pedantic)
6052 pedwarn ("%HISO C does not allow extra %<;%> "
6053 "outside of a function",
6054 &c_parser_peek_token (parser)->location);
6055 c_parser_consume_token (parser);
6056 break;
6057 case CPP_PLUS:
6058 case CPP_MINUS:
6059 c_parser_objc_methodproto (parser);
6060 break;
6061 case CPP_PRAGMA:
6062 c_parser_pragma (parser, pragma_external);
6063 break;
6064 case CPP_EOF:
6065 return;
6066 default:
6067 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6068 return;
6069 c_parser_declaration_or_fndef (parser, false, true, false, true);
6070 break;
6075 /* Parse an objc-methodproto.
6077 objc-methodproto:
6078 objc-method-type objc-method-decl ;
6081 static void
6082 c_parser_objc_methodproto (c_parser *parser)
6084 enum tree_code type = c_parser_objc_method_type (parser);
6085 tree decl;
6086 objc_set_method_type (type);
6087 /* Remember protocol qualifiers in prototypes. */
6088 parser->objc_pq_context = true;
6089 decl = c_parser_objc_method_decl (parser);
6090 /* Forget protocol qualifiers here. */
6091 parser->objc_pq_context = false;
6092 objc_add_method_declaration (decl);
6093 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6096 /* Parse an objc-method-decl.
6098 objc-method-decl:
6099 ( objc-type-name ) objc-selector
6100 objc-selector
6101 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6102 objc-keyword-selector objc-optparmlist
6104 objc-keyword-selector:
6105 objc-keyword-decl
6106 objc-keyword-selector objc-keyword-decl
6108 objc-keyword-decl:
6109 objc-selector : ( objc-type-name ) identifier
6110 objc-selector : identifier
6111 : ( objc-type-name ) identifier
6112 : identifier
6114 objc-optparmlist:
6115 objc-optparms objc-optellipsis
6117 objc-optparms:
6118 empty
6119 objc-opt-parms , parameter-declaration
6121 objc-optellipsis:
6122 empty
6123 , ...
6126 static tree
6127 c_parser_objc_method_decl (c_parser *parser)
6129 tree type = NULL_TREE;
6130 tree sel;
6131 tree parms = NULL_TREE;
6132 bool ellipsis = false;
6134 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6136 c_parser_consume_token (parser);
6137 type = c_parser_objc_type_name (parser);
6138 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6140 sel = c_parser_objc_selector (parser);
6141 /* If there is no selector, or a colon follows, we have an
6142 objc-keyword-selector. If there is a selector, and a colon does
6143 not follow, that selector ends the objc-method-decl. */
6144 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6146 tree tsel = sel;
6147 tree list = NULL_TREE;
6148 while (true)
6150 tree atype = NULL_TREE, id, keyworddecl;
6151 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6152 break;
6153 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6155 c_parser_consume_token (parser);
6156 atype = c_parser_objc_type_name (parser);
6157 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6158 "expected %<)%>");
6160 if (c_parser_next_token_is_not (parser, CPP_NAME))
6162 c_parser_error (parser, "expected identifier");
6163 return error_mark_node;
6165 id = c_parser_peek_token (parser)->value;
6166 c_parser_consume_token (parser);
6167 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6168 list = chainon (list, keyworddecl);
6169 tsel = c_parser_objc_selector (parser);
6170 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6171 break;
6173 /* Parse the optional parameter list. Optional Objective-C
6174 method parameters follow the C syntax, and may include '...'
6175 to denote a variable number of arguments. */
6176 parms = make_node (TREE_LIST);
6177 while (c_parser_next_token_is (parser, CPP_COMMA))
6179 struct c_parm *parm;
6180 c_parser_consume_token (parser);
6181 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6183 ellipsis = true;
6184 c_parser_consume_token (parser);
6185 break;
6187 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6188 if (parm == NULL)
6189 break;
6190 parms = chainon (parms,
6191 build_tree_list (NULL_TREE, grokparm (parm)));
6193 sel = list;
6195 return objc_build_method_signature (type, sel, parms, ellipsis);
6198 /* Parse an objc-type-name.
6200 objc-type-name:
6201 objc-type-qualifiers[opt] type-name
6202 objc-type-qualifiers[opt]
6204 objc-type-qualifiers:
6205 objc-type-qualifier
6206 objc-type-qualifiers objc-type-qualifier
6208 objc-type-qualifier: one of
6209 in out inout bycopy byref oneway
6212 static tree
6213 c_parser_objc_type_name (c_parser *parser)
6215 tree quals = NULL_TREE;
6216 struct c_type_name *type_name = NULL;
6217 tree type = NULL_TREE;
6218 while (true)
6220 c_token *token = c_parser_peek_token (parser);
6221 if (token->type == CPP_KEYWORD
6222 && (token->keyword == RID_IN
6223 || token->keyword == RID_OUT
6224 || token->keyword == RID_INOUT
6225 || token->keyword == RID_BYCOPY
6226 || token->keyword == RID_BYREF
6227 || token->keyword == RID_ONEWAY))
6229 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6230 c_parser_consume_token (parser);
6232 else
6233 break;
6235 if (c_parser_next_token_starts_typename (parser))
6236 type_name = c_parser_type_name (parser);
6237 if (type_name)
6238 type = groktypename (type_name);
6239 return build_tree_list (quals, type);
6242 /* Parse objc-protocol-refs.
6244 objc-protocol-refs:
6245 < identifier-list >
6248 static tree
6249 c_parser_objc_protocol_refs (c_parser *parser)
6251 tree list = NULL_TREE;
6252 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6253 c_parser_consume_token (parser);
6254 /* Any identifiers, including those declared as type names, are OK
6255 here. */
6256 while (true)
6258 tree id;
6259 if (c_parser_next_token_is_not (parser, CPP_NAME))
6261 c_parser_error (parser, "expected identifier");
6262 break;
6264 id = c_parser_peek_token (parser)->value;
6265 list = chainon (list, build_tree_list (NULL_TREE, id));
6266 c_parser_consume_token (parser);
6267 if (c_parser_next_token_is (parser, CPP_COMMA))
6268 c_parser_consume_token (parser);
6269 else
6270 break;
6272 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6273 return list;
6276 /* Parse an objc-try-catch-statement.
6278 objc-try-catch-statement:
6279 @try compound-statement objc-catch-list[opt]
6280 @try compound-statement objc-catch-list[opt] @finally compound-statement
6282 objc-catch-list:
6283 @catch ( parameter-declaration ) compound-statement
6284 objc-catch-list @catch ( parameter-declaration ) compound-statement
6287 static void
6288 c_parser_objc_try_catch_statement (c_parser *parser)
6290 location_t loc;
6291 tree stmt;
6292 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6293 c_parser_consume_token (parser);
6294 loc = c_parser_peek_token (parser)->location;
6295 stmt = c_parser_compound_statement (parser);
6296 objc_begin_try_stmt (loc, stmt);
6297 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6299 struct c_parm *parm;
6300 c_parser_consume_token (parser);
6301 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6302 break;
6303 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6304 if (parm == NULL)
6306 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6307 break;
6309 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6310 objc_begin_catch_clause (grokparm (parm));
6311 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6312 c_parser_compound_statement_nostart (parser);
6313 objc_finish_catch_clause ();
6315 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6317 location_t finloc;
6318 tree finstmt;
6319 c_parser_consume_token (parser);
6320 finloc = c_parser_peek_token (parser)->location;
6321 finstmt = c_parser_compound_statement (parser);
6322 objc_build_finally_clause (finloc, finstmt);
6324 objc_finish_try_stmt ();
6327 /* Parse an objc-synchronized-statement.
6329 objc-synchronized-statement:
6330 @synchronized ( expression ) compound-statement
6333 static void
6334 c_parser_objc_synchronized_statement (c_parser *parser)
6336 location_t loc;
6337 tree expr, stmt;
6338 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6339 c_parser_consume_token (parser);
6340 loc = c_parser_peek_token (parser)->location;
6341 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6343 expr = c_parser_expression (parser).value;
6344 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6346 else
6347 expr = error_mark_node;
6348 stmt = c_parser_compound_statement (parser);
6349 objc_build_synchronized (loc, expr, stmt);
6352 /* Parse an objc-selector; return NULL_TREE without an error if the
6353 next token is not an objc-selector.
6355 objc-selector:
6356 identifier
6357 one of
6358 enum struct union if else while do for switch case default
6359 break continue return goto asm sizeof typeof __alignof
6360 unsigned long const short volatile signed restrict _Complex
6361 in out inout bycopy byref oneway int char float double void _Bool
6363 ??? Why this selection of keywords but not, for example, storage
6364 class specifiers? */
6366 static tree
6367 c_parser_objc_selector (c_parser *parser)
6369 c_token *token = c_parser_peek_token (parser);
6370 tree value = token->value;
6371 if (token->type == CPP_NAME)
6373 c_parser_consume_token (parser);
6374 return value;
6376 if (token->type != CPP_KEYWORD)
6377 return NULL_TREE;
6378 switch (token->keyword)
6380 case RID_ENUM:
6381 case RID_STRUCT:
6382 case RID_UNION:
6383 case RID_IF:
6384 case RID_ELSE:
6385 case RID_WHILE:
6386 case RID_DO:
6387 case RID_FOR:
6388 case RID_SWITCH:
6389 case RID_CASE:
6390 case RID_DEFAULT:
6391 case RID_BREAK:
6392 case RID_CONTINUE:
6393 case RID_RETURN:
6394 case RID_GOTO:
6395 case RID_ASM:
6396 case RID_SIZEOF:
6397 case RID_TYPEOF:
6398 case RID_ALIGNOF:
6399 case RID_UNSIGNED:
6400 case RID_LONG:
6401 case RID_CONST:
6402 case RID_SHORT:
6403 case RID_VOLATILE:
6404 case RID_SIGNED:
6405 case RID_RESTRICT:
6406 case RID_COMPLEX:
6407 case RID_IN:
6408 case RID_OUT:
6409 case RID_INOUT:
6410 case RID_BYCOPY:
6411 case RID_BYREF:
6412 case RID_ONEWAY:
6413 case RID_INT:
6414 case RID_CHAR:
6415 case RID_FLOAT:
6416 case RID_DOUBLE:
6417 case RID_VOID:
6418 case RID_BOOL:
6419 c_parser_consume_token (parser);
6420 return value;
6421 default:
6422 return NULL_TREE;
6426 /* Parse an objc-selector-arg.
6428 objc-selector-arg:
6429 objc-selector
6430 objc-keywordname-list
6432 objc-keywordname-list:
6433 objc-keywordname
6434 objc-keywordname-list objc-keywordname
6436 objc-keywordname:
6437 objc-selector :
6441 static tree
6442 c_parser_objc_selector_arg (c_parser *parser)
6444 tree sel = c_parser_objc_selector (parser);
6445 tree list = NULL_TREE;
6446 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6447 return sel;
6448 while (true)
6450 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6451 return list;
6452 list = chainon (list, build_tree_list (sel, NULL_TREE));
6453 sel = c_parser_objc_selector (parser);
6454 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6455 break;
6457 return list;
6460 /* Parse an objc-receiver.
6462 objc-receiver:
6463 expression
6464 class-name
6465 type-name
6468 static tree
6469 c_parser_objc_receiver (c_parser *parser)
6471 if (c_parser_peek_token (parser)->type == CPP_NAME
6472 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6473 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6475 tree id = c_parser_peek_token (parser)->value;
6476 c_parser_consume_token (parser);
6477 return objc_get_class_reference (id);
6479 return c_parser_expression (parser).value;
6482 /* Parse objc-message-args.
6484 objc-message-args:
6485 objc-selector
6486 objc-keywordarg-list
6488 objc-keywordarg-list:
6489 objc-keywordarg
6490 objc-keywordarg-list objc-keywordarg
6492 objc-keywordarg:
6493 objc-selector : objc-keywordexpr
6494 : objc-keywordexpr
6497 static tree
6498 c_parser_objc_message_args (c_parser *parser)
6500 tree sel = c_parser_objc_selector (parser);
6501 tree list = NULL_TREE;
6502 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6503 return sel;
6504 while (true)
6506 tree keywordexpr;
6507 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6508 return list;
6509 keywordexpr = c_parser_objc_keywordexpr (parser);
6510 list = chainon (list, build_tree_list (sel, keywordexpr));
6511 sel = c_parser_objc_selector (parser);
6512 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6513 break;
6515 return list;
6518 /* Parse an objc-keywordexpr.
6520 objc-keywordexpr:
6521 nonempty-expr-list
6524 static tree
6525 c_parser_objc_keywordexpr (c_parser *parser)
6527 tree list = c_parser_expr_list (parser, true);
6528 if (TREE_CHAIN (list) == NULL_TREE)
6530 /* Just return the expression, remove a level of
6531 indirection. */
6532 return TREE_VALUE (list);
6534 else
6536 /* We have a comma expression, we will collapse later. */
6537 return list;
6542 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6543 should be considered, statements. ALLOW_STMT is true if we're within
6544 the context of a function and such pragmas are to be allowed. Returns
6545 true if we actually parsed such a pragma. */
6547 static bool
6548 c_parser_pragma (c_parser *parser, enum pragma_context context)
6550 unsigned int id;
6552 id = c_parser_peek_token (parser)->pragma_kind;
6553 gcc_assert (id != PRAGMA_NONE);
6555 switch (id)
6557 case PRAGMA_OMP_BARRIER:
6558 if (context != pragma_compound)
6560 if (context == pragma_stmt)
6561 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6562 "used in compound statements");
6563 goto bad_stmt;
6565 c_parser_omp_barrier (parser);
6566 return false;
6568 case PRAGMA_OMP_FLUSH:
6569 if (context != pragma_compound)
6571 if (context == pragma_stmt)
6572 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6573 "used in compound statements");
6574 goto bad_stmt;
6576 c_parser_omp_flush (parser);
6577 return false;
6579 case PRAGMA_OMP_TASKWAIT:
6580 if (context != pragma_compound)
6582 if (context == pragma_stmt)
6583 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
6584 "used in compound statements");
6585 goto bad_stmt;
6587 c_parser_omp_taskwait (parser);
6588 return false;
6590 case PRAGMA_OMP_THREADPRIVATE:
6591 c_parser_omp_threadprivate (parser);
6592 return false;
6594 case PRAGMA_OMP_SECTION:
6595 error ("%H%<#pragma omp section%> may only be used in "
6596 "%<#pragma omp sections%> construct",
6597 &c_parser_peek_token (parser)->location);
6598 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6599 return false;
6601 case PRAGMA_GCC_PCH_PREPROCESS:
6602 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6603 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6604 return false;
6606 default:
6607 if (id < PRAGMA_FIRST_EXTERNAL)
6609 if (context == pragma_external)
6611 bad_stmt:
6612 c_parser_error (parser, "expected declaration specifiers");
6613 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6614 return false;
6616 c_parser_omp_construct (parser);
6617 return true;
6619 break;
6622 c_parser_consume_pragma (parser);
6623 c_invoke_pragma_handler (id);
6625 /* Skip to EOL, but suppress any error message. Those will have been
6626 generated by the handler routine through calling error, as opposed
6627 to calling c_parser_error. */
6628 parser->error = true;
6629 c_parser_skip_to_pragma_eol (parser);
6631 return false;
6634 /* The interface the pragma parsers have to the lexer. */
6636 enum cpp_ttype
6637 pragma_lex (tree *value)
6639 c_token *tok = c_parser_peek_token (the_parser);
6640 enum cpp_ttype ret = tok->type;
6642 *value = tok->value;
6643 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6644 ret = CPP_EOF;
6645 else
6647 if (ret == CPP_KEYWORD)
6648 ret = CPP_NAME;
6649 c_parser_consume_token (the_parser);
6652 return ret;
6655 static void
6656 c_parser_pragma_pch_preprocess (c_parser *parser)
6658 tree name = NULL;
6660 c_parser_consume_pragma (parser);
6661 if (c_parser_next_token_is (parser, CPP_STRING))
6663 name = c_parser_peek_token (parser)->value;
6664 c_parser_consume_token (parser);
6666 else
6667 c_parser_error (parser, "expected string literal");
6668 c_parser_skip_to_pragma_eol (parser);
6670 if (name)
6671 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6674 /* OpenMP 2.5 parsing routines. */
6676 /* Returns name of the next clause.
6677 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6678 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6679 returned and the token is consumed. */
6681 static pragma_omp_clause
6682 c_parser_omp_clause_name (c_parser *parser)
6684 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6686 if (c_parser_next_token_is_keyword (parser, RID_IF))
6687 result = PRAGMA_OMP_CLAUSE_IF;
6688 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6689 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6690 else if (c_parser_next_token_is (parser, CPP_NAME))
6692 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6694 switch (p[0])
6696 case 'c':
6697 if (!strcmp ("collapse", p))
6698 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
6699 else if (!strcmp ("copyin", p))
6700 result = PRAGMA_OMP_CLAUSE_COPYIN;
6701 else if (!strcmp ("copyprivate", p))
6702 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6703 break;
6704 case 'f':
6705 if (!strcmp ("firstprivate", p))
6706 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6707 break;
6708 case 'l':
6709 if (!strcmp ("lastprivate", p))
6710 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6711 break;
6712 case 'n':
6713 if (!strcmp ("nowait", p))
6714 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6715 else if (!strcmp ("num_threads", p))
6716 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6717 break;
6718 case 'o':
6719 if (!strcmp ("ordered", p))
6720 result = PRAGMA_OMP_CLAUSE_ORDERED;
6721 break;
6722 case 'p':
6723 if (!strcmp ("private", p))
6724 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6725 break;
6726 case 'r':
6727 if (!strcmp ("reduction", p))
6728 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6729 break;
6730 case 's':
6731 if (!strcmp ("schedule", p))
6732 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6733 else if (!strcmp ("shared", p))
6734 result = PRAGMA_OMP_CLAUSE_SHARED;
6735 break;
6736 case 'u':
6737 if (!strcmp ("untied", p))
6738 result = PRAGMA_OMP_CLAUSE_UNTIED;
6739 break;
6743 if (result != PRAGMA_OMP_CLAUSE_NONE)
6744 c_parser_consume_token (parser);
6746 return result;
6749 /* Validate that a clause of the given type does not already exist. */
6751 static void
6752 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
6753 const char *name)
6755 tree c;
6757 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6758 if (OMP_CLAUSE_CODE (c) == code)
6760 error ("too many %qs clauses", name);
6761 break;
6765 /* OpenMP 2.5:
6766 variable-list:
6767 identifier
6768 variable-list , identifier
6770 If KIND is nonzero, create the appropriate node and install the decl
6771 in OMP_CLAUSE_DECL and add the node to the head of the list.
6773 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6774 return the list created. */
6776 static tree
6777 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6778 tree list)
6780 if (c_parser_next_token_is_not (parser, CPP_NAME)
6781 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6782 c_parser_error (parser, "expected identifier");
6784 while (c_parser_next_token_is (parser, CPP_NAME)
6785 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6787 tree t = lookup_name (c_parser_peek_token (parser)->value);
6789 if (t == NULL_TREE)
6790 undeclared_variable (c_parser_peek_token (parser)->value,
6791 c_parser_peek_token (parser)->location);
6792 else if (t == error_mark_node)
6794 else if (kind != 0)
6796 tree u = build_omp_clause (kind);
6797 OMP_CLAUSE_DECL (u) = t;
6798 OMP_CLAUSE_CHAIN (u) = list;
6799 list = u;
6801 else
6802 list = tree_cons (t, NULL_TREE, list);
6804 c_parser_consume_token (parser);
6806 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6807 break;
6809 c_parser_consume_token (parser);
6812 return list;
6815 /* Similarly, but expect leading and trailing parenthesis. This is a very
6816 common case for omp clauses. */
6818 static tree
6819 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
6820 tree list)
6822 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6824 list = c_parser_omp_variable_list (parser, kind, list);
6825 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6827 return list;
6830 /* OpenMP 3.0:
6831 collapse ( constant-expression ) */
6833 static tree
6834 c_parser_omp_clause_collapse (c_parser *parser, tree list)
6836 tree c, num = error_mark_node;
6837 HOST_WIDE_INT n;
6838 location_t loc;
6840 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
6842 loc = c_parser_peek_token (parser)->location;
6843 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6845 num = c_parser_expr_no_commas (parser, NULL).value;
6846 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6848 if (num == error_mark_node)
6849 return list;
6850 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
6851 || !host_integerp (num, 0)
6852 || (n = tree_low_cst (num, 0)) <= 0
6853 || (int) n != n)
6855 error ("%Hcollapse argument needs positive constant integer expression",
6856 &loc);
6857 return list;
6859 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
6860 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
6861 OMP_CLAUSE_CHAIN (c) = list;
6862 return c;
6865 /* OpenMP 2.5:
6866 copyin ( variable-list ) */
6868 static tree
6869 c_parser_omp_clause_copyin (c_parser *parser, tree list)
6871 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
6874 /* OpenMP 2.5:
6875 copyprivate ( variable-list ) */
6877 static tree
6878 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
6880 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
6883 /* OpenMP 2.5:
6884 default ( shared | none ) */
6886 static tree
6887 c_parser_omp_clause_default (c_parser *parser, tree list)
6889 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
6890 tree c;
6892 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6893 return list;
6894 if (c_parser_next_token_is (parser, CPP_NAME))
6896 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6898 switch (p[0])
6900 case 'n':
6901 if (strcmp ("none", p) != 0)
6902 goto invalid_kind;
6903 kind = OMP_CLAUSE_DEFAULT_NONE;
6904 break;
6906 case 's':
6907 if (strcmp ("shared", p) != 0)
6908 goto invalid_kind;
6909 kind = OMP_CLAUSE_DEFAULT_SHARED;
6910 break;
6912 default:
6913 goto invalid_kind;
6916 c_parser_consume_token (parser);
6918 else
6920 invalid_kind:
6921 c_parser_error (parser, "expected %<none%> or %<shared%>");
6923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6925 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6926 return list;
6928 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
6929 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
6930 OMP_CLAUSE_CHAIN (c) = list;
6931 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
6933 return c;
6936 /* OpenMP 2.5:
6937 firstprivate ( variable-list ) */
6939 static tree
6940 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
6942 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
6945 /* OpenMP 2.5:
6946 if ( expression ) */
6948 static tree
6949 c_parser_omp_clause_if (c_parser *parser, tree list)
6951 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6953 tree t = c_parser_paren_condition (parser);
6954 tree c;
6956 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
6958 c = build_omp_clause (OMP_CLAUSE_IF);
6959 OMP_CLAUSE_IF_EXPR (c) = t;
6960 OMP_CLAUSE_CHAIN (c) = list;
6961 list = c;
6963 else
6964 c_parser_error (parser, "expected %<(%>");
6966 return list;
6969 /* OpenMP 2.5:
6970 lastprivate ( variable-list ) */
6972 static tree
6973 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
6975 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
6978 /* OpenMP 2.5:
6979 nowait */
6981 static tree
6982 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6984 tree c;
6986 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
6988 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
6989 OMP_CLAUSE_CHAIN (c) = list;
6990 return c;
6993 /* OpenMP 2.5:
6994 num_threads ( expression ) */
6996 static tree
6997 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
6999 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7001 location_t expr_loc = c_parser_peek_token (parser)->location;
7002 tree c, t = c_parser_expression (parser).value;
7004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7006 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7008 c_parser_error (parser, "expected integer expression");
7009 return list;
7012 /* Attempt to statically determine when the number isn't positive. */
7013 c = fold_build2 (LE_EXPR, boolean_type_node, t,
7014 build_int_cst (TREE_TYPE (t), 0));
7015 if (c == boolean_true_node)
7017 warning (0, "%H%<num_threads%> value must be positive", &expr_loc);
7018 t = integer_one_node;
7021 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7023 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
7024 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7025 OMP_CLAUSE_CHAIN (c) = list;
7026 list = c;
7029 return list;
7032 /* OpenMP 2.5:
7033 ordered */
7035 static tree
7036 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7038 tree c;
7040 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7042 c = build_omp_clause (OMP_CLAUSE_ORDERED);
7043 OMP_CLAUSE_CHAIN (c) = list;
7044 return c;
7047 /* OpenMP 2.5:
7048 private ( variable-list ) */
7050 static tree
7051 c_parser_omp_clause_private (c_parser *parser, tree list)
7053 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7056 /* OpenMP 2.5:
7057 reduction ( reduction-operator : variable-list )
7059 reduction-operator:
7060 One of: + * - & ^ | && || */
7062 static tree
7063 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7065 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7067 enum tree_code code;
7069 switch (c_parser_peek_token (parser)->type)
7071 case CPP_PLUS:
7072 code = PLUS_EXPR;
7073 break;
7074 case CPP_MULT:
7075 code = MULT_EXPR;
7076 break;
7077 case CPP_MINUS:
7078 code = MINUS_EXPR;
7079 break;
7080 case CPP_AND:
7081 code = BIT_AND_EXPR;
7082 break;
7083 case CPP_XOR:
7084 code = BIT_XOR_EXPR;
7085 break;
7086 case CPP_OR:
7087 code = BIT_IOR_EXPR;
7088 break;
7089 case CPP_AND_AND:
7090 code = TRUTH_ANDIF_EXPR;
7091 break;
7092 case CPP_OR_OR:
7093 code = TRUTH_ORIF_EXPR;
7094 break;
7095 default:
7096 c_parser_error (parser,
7097 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7098 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7100 return list;
7102 c_parser_consume_token (parser);
7103 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7105 tree nl, c;
7107 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
7108 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7109 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7111 list = nl;
7113 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7115 return list;
7118 /* OpenMP 2.5:
7119 schedule ( schedule-kind )
7120 schedule ( schedule-kind , expression )
7122 schedule-kind:
7123 static | dynamic | guided | runtime | auto
7126 static tree
7127 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7129 tree c, t;
7131 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7132 return list;
7134 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7136 if (c_parser_next_token_is (parser, CPP_NAME))
7138 tree kind = c_parser_peek_token (parser)->value;
7139 const char *p = IDENTIFIER_POINTER (kind);
7141 switch (p[0])
7143 case 'd':
7144 if (strcmp ("dynamic", p) != 0)
7145 goto invalid_kind;
7146 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7147 break;
7149 case 'g':
7150 if (strcmp ("guided", p) != 0)
7151 goto invalid_kind;
7152 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7153 break;
7155 case 'r':
7156 if (strcmp ("runtime", p) != 0)
7157 goto invalid_kind;
7158 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7159 break;
7161 default:
7162 goto invalid_kind;
7165 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7166 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7167 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7168 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7169 else
7170 goto invalid_kind;
7172 c_parser_consume_token (parser);
7173 if (c_parser_next_token_is (parser, CPP_COMMA))
7175 location_t here;
7176 c_parser_consume_token (parser);
7178 here = c_parser_peek_token (parser)->location;
7179 t = c_parser_expr_no_commas (parser, NULL).value;
7181 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7182 error ("%Hschedule %<runtime%> does not take "
7183 "a %<chunk_size%> parameter", &here);
7184 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7185 error ("%Hschedule %<auto%> does not take "
7186 "a %<chunk_size%> parameter", &here);
7187 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7188 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7189 else
7190 c_parser_error (parser, "expected integer expression");
7192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7194 else
7195 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7196 "expected %<,%> or %<)%>");
7198 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7199 OMP_CLAUSE_CHAIN (c) = list;
7200 return c;
7202 invalid_kind:
7203 c_parser_error (parser, "invalid schedule kind");
7204 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7205 return list;
7208 /* OpenMP 2.5:
7209 shared ( variable-list ) */
7211 static tree
7212 c_parser_omp_clause_shared (c_parser *parser, tree list)
7214 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7217 /* OpenMP 3.0:
7218 untied */
7220 static tree
7221 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7223 tree c;
7225 /* FIXME: Should we allow duplicates? */
7226 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7228 c = build_omp_clause (OMP_CLAUSE_UNTIED);
7229 OMP_CLAUSE_CHAIN (c) = list;
7230 return c;
7233 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7234 is a bitmask in MASK. Return the list of clauses found; the result
7235 of clause default goes in *pdefault. */
7237 static tree
7238 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7239 const char *where)
7241 tree clauses = NULL;
7242 bool first = true;
7244 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7246 location_t here;
7247 pragma_omp_clause c_kind;
7248 const char *c_name;
7249 tree prev = clauses;
7251 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7252 c_parser_consume_token (parser);
7254 first = false;
7255 here = c_parser_peek_token (parser)->location;
7256 c_kind = c_parser_omp_clause_name (parser);
7258 switch (c_kind)
7260 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7261 clauses = c_parser_omp_clause_collapse (parser, clauses);
7262 c_name = "collapse";
7263 break;
7264 case PRAGMA_OMP_CLAUSE_COPYIN:
7265 clauses = c_parser_omp_clause_copyin (parser, clauses);
7266 c_name = "copyin";
7267 break;
7268 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7269 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7270 c_name = "copyprivate";
7271 break;
7272 case PRAGMA_OMP_CLAUSE_DEFAULT:
7273 clauses = c_parser_omp_clause_default (parser, clauses);
7274 c_name = "default";
7275 break;
7276 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7277 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7278 c_name = "firstprivate";
7279 break;
7280 case PRAGMA_OMP_CLAUSE_IF:
7281 clauses = c_parser_omp_clause_if (parser, clauses);
7282 c_name = "if";
7283 break;
7284 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7285 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7286 c_name = "lastprivate";
7287 break;
7288 case PRAGMA_OMP_CLAUSE_NOWAIT:
7289 clauses = c_parser_omp_clause_nowait (parser, clauses);
7290 c_name = "nowait";
7291 break;
7292 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7293 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7294 c_name = "num_threads";
7295 break;
7296 case PRAGMA_OMP_CLAUSE_ORDERED:
7297 clauses = c_parser_omp_clause_ordered (parser, clauses);
7298 c_name = "ordered";
7299 break;
7300 case PRAGMA_OMP_CLAUSE_PRIVATE:
7301 clauses = c_parser_omp_clause_private (parser, clauses);
7302 c_name = "private";
7303 break;
7304 case PRAGMA_OMP_CLAUSE_REDUCTION:
7305 clauses = c_parser_omp_clause_reduction (parser, clauses);
7306 c_name = "reduction";
7307 break;
7308 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7309 clauses = c_parser_omp_clause_schedule (parser, clauses);
7310 c_name = "schedule";
7311 break;
7312 case PRAGMA_OMP_CLAUSE_SHARED:
7313 clauses = c_parser_omp_clause_shared (parser, clauses);
7314 c_name = "shared";
7315 break;
7316 case PRAGMA_OMP_CLAUSE_UNTIED:
7317 clauses = c_parser_omp_clause_untied (parser, clauses);
7318 c_name = "untied";
7319 break;
7320 default:
7321 c_parser_error (parser, "expected %<#pragma omp%> clause");
7322 goto saw_error;
7325 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7327 /* Remove the invalid clause(s) from the list to avoid
7328 confusing the rest of the compiler. */
7329 clauses = prev;
7330 error ("%H%qs is not valid for %qs", &here, c_name, where);
7334 saw_error:
7335 c_parser_skip_to_pragma_eol (parser);
7337 return c_finish_omp_clauses (clauses);
7340 /* OpenMP 2.5:
7341 structured-block:
7342 statement
7344 In practice, we're also interested in adding the statement to an
7345 outer node. So it is convenient if we work around the fact that
7346 c_parser_statement calls add_stmt. */
7348 static tree
7349 c_parser_omp_structured_block (c_parser *parser)
7351 tree stmt = push_stmt_list ();
7352 c_parser_statement (parser);
7353 return pop_stmt_list (stmt);
7356 /* OpenMP 2.5:
7357 # pragma omp atomic new-line
7358 expression-stmt
7360 expression-stmt:
7361 x binop= expr | x++ | ++x | x-- | --x
7362 binop:
7363 +, *, -, /, &, ^, |, <<, >>
7365 where x is an lvalue expression with scalar type. */
7367 static void
7368 c_parser_omp_atomic (c_parser *parser)
7370 tree lhs, rhs;
7371 tree stmt;
7372 enum tree_code code;
7373 struct c_expr rhs_expr;
7375 c_parser_skip_to_pragma_eol (parser);
7377 lhs = c_parser_unary_expression (parser).value;
7378 switch (TREE_CODE (lhs))
7380 case ERROR_MARK:
7381 saw_error:
7382 c_parser_skip_to_end_of_block_or_statement (parser);
7383 return;
7385 case PREINCREMENT_EXPR:
7386 case POSTINCREMENT_EXPR:
7387 lhs = TREE_OPERAND (lhs, 0);
7388 code = PLUS_EXPR;
7389 rhs = integer_one_node;
7390 break;
7392 case PREDECREMENT_EXPR:
7393 case POSTDECREMENT_EXPR:
7394 lhs = TREE_OPERAND (lhs, 0);
7395 code = MINUS_EXPR;
7396 rhs = integer_one_node;
7397 break;
7399 default:
7400 switch (c_parser_peek_token (parser)->type)
7402 case CPP_MULT_EQ:
7403 code = MULT_EXPR;
7404 break;
7405 case CPP_DIV_EQ:
7406 code = TRUNC_DIV_EXPR;
7407 break;
7408 case CPP_PLUS_EQ:
7409 code = PLUS_EXPR;
7410 break;
7411 case CPP_MINUS_EQ:
7412 code = MINUS_EXPR;
7413 break;
7414 case CPP_LSHIFT_EQ:
7415 code = LSHIFT_EXPR;
7416 break;
7417 case CPP_RSHIFT_EQ:
7418 code = RSHIFT_EXPR;
7419 break;
7420 case CPP_AND_EQ:
7421 code = BIT_AND_EXPR;
7422 break;
7423 case CPP_OR_EQ:
7424 code = BIT_IOR_EXPR;
7425 break;
7426 case CPP_XOR_EQ:
7427 code = BIT_XOR_EXPR;
7428 break;
7429 default:
7430 c_parser_error (parser,
7431 "invalid operator for %<#pragma omp atomic%>");
7432 goto saw_error;
7435 c_parser_consume_token (parser);
7436 rhs_expr = c_parser_expression (parser);
7437 rhs_expr = default_function_array_conversion (rhs_expr);
7438 rhs = rhs_expr.value;
7439 break;
7441 stmt = c_finish_omp_atomic (code, lhs, rhs);
7442 if (stmt != error_mark_node)
7443 add_stmt (stmt);
7444 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7448 /* OpenMP 2.5:
7449 # pragma omp barrier new-line
7452 static void
7453 c_parser_omp_barrier (c_parser *parser)
7455 c_parser_consume_pragma (parser);
7456 c_parser_skip_to_pragma_eol (parser);
7458 c_finish_omp_barrier ();
7461 /* OpenMP 2.5:
7462 # pragma omp critical [(name)] new-line
7463 structured-block
7466 static tree
7467 c_parser_omp_critical (c_parser *parser)
7469 tree stmt, name = NULL;
7471 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7473 c_parser_consume_token (parser);
7474 if (c_parser_next_token_is (parser, CPP_NAME))
7476 name = c_parser_peek_token (parser)->value;
7477 c_parser_consume_token (parser);
7478 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7480 else
7481 c_parser_error (parser, "expected identifier");
7483 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7484 c_parser_error (parser, "expected %<(%> or end of line");
7485 c_parser_skip_to_pragma_eol (parser);
7487 stmt = c_parser_omp_structured_block (parser);
7488 return c_finish_omp_critical (stmt, name);
7491 /* OpenMP 2.5:
7492 # pragma omp flush flush-vars[opt] new-line
7494 flush-vars:
7495 ( variable-list ) */
7497 static void
7498 c_parser_omp_flush (c_parser *parser)
7500 c_parser_consume_pragma (parser);
7501 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7502 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
7503 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7504 c_parser_error (parser, "expected %<(%> or end of line");
7505 c_parser_skip_to_pragma_eol (parser);
7507 c_finish_omp_flush ();
7510 /* Parse the restricted form of the for statement allowed by OpenMP.
7511 The real trick here is to determine the loop control variable early
7512 so that we can push a new decl if necessary to make it private. */
7514 static tree
7515 c_parser_omp_for_loop (c_parser *parser, tree clauses, tree *par_clauses)
7517 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7518 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
7519 location_t loc;
7520 bool fail = false, open_brace_parsed = false;
7521 int i, collapse = 1, nbraces = 0;
7523 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
7524 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
7525 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
7527 gcc_assert (collapse >= 1);
7529 declv = make_tree_vec (collapse);
7530 initv = make_tree_vec (collapse);
7531 condv = make_tree_vec (collapse);
7532 incrv = make_tree_vec (collapse);
7534 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7536 c_parser_error (parser, "for statement expected");
7537 return NULL;
7539 loc = c_parser_peek_token (parser)->location;
7540 c_parser_consume_token (parser);
7542 for (i = 0; i < collapse; i++)
7544 int bracecount = 0;
7546 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7547 goto pop_scopes;
7549 /* Parse the initialization declaration or expression. */
7550 if (c_parser_next_token_starts_declspecs (parser))
7552 if (i > 0)
7553 for_block
7554 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
7555 c_parser_declaration_or_fndef (parser, true, true, true, true);
7556 decl = check_for_loop_decls ();
7557 if (decl == NULL)
7558 goto error_init;
7559 if (DECL_INITIAL (decl) == error_mark_node)
7560 decl = error_mark_node;
7561 init = decl;
7563 else if (c_parser_next_token_is (parser, CPP_NAME)
7564 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7566 struct c_expr init_exp;
7568 decl = c_parser_postfix_expression (parser).value;
7570 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7572 init_exp = c_parser_expr_no_commas (parser, NULL);
7573 init_exp = default_function_array_conversion (init_exp);
7574 init = build_modify_expr (decl, NOP_EXPR, init_exp.value);
7575 init = c_process_expr_stmt (init);
7577 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7579 else
7581 error_init:
7582 c_parser_error (parser,
7583 "expected iteration declaration or initialization");
7584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7585 "expected %<)%>");
7586 fail = true;
7587 goto parse_next;
7590 /* Parse the loop condition. */
7591 cond = NULL_TREE;
7592 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7594 cond = c_parser_expression_conv (parser).value;
7595 cond = c_objc_common_truthvalue_conversion (cond);
7596 if (CAN_HAVE_LOCATION_P (cond))
7597 SET_EXPR_LOCATION (cond, input_location);
7599 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7601 /* Parse the increment expression. */
7602 incr = NULL_TREE;
7603 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7604 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7607 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
7608 fail = true;
7609 else
7611 TREE_VEC_ELT (declv, i) = decl;
7612 TREE_VEC_ELT (initv, i) = init;
7613 TREE_VEC_ELT (condv, i) = cond;
7614 TREE_VEC_ELT (incrv, i) = incr;
7617 parse_next:
7618 if (i == collapse - 1)
7619 break;
7621 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
7622 in between the collapsed for loops to be still considered perfectly
7623 nested. Hopefully the final version clarifies this.
7624 For now handle (multiple) {'s and empty statements. */
7627 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7629 c_parser_consume_token (parser);
7630 break;
7632 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7634 c_parser_consume_token (parser);
7635 bracecount++;
7637 else if (bracecount
7638 && c_parser_next_token_is (parser, CPP_SEMICOLON))
7639 c_parser_consume_token (parser);
7640 else
7642 c_parser_error (parser, "not enough perfectly nested loops");
7643 if (bracecount)
7645 open_brace_parsed = true;
7646 bracecount--;
7648 fail = true;
7649 collapse = 0;
7650 break;
7653 while (1);
7655 nbraces += bracecount;
7658 save_break = c_break_label;
7659 c_break_label = size_one_node;
7660 save_cont = c_cont_label;
7661 c_cont_label = NULL_TREE;
7662 body = push_stmt_list ();
7664 if (open_brace_parsed)
7666 stmt = c_begin_compound_stmt (true);
7667 c_parser_compound_statement_nostart (parser);
7668 add_stmt (c_end_compound_stmt (stmt, true));
7670 else
7671 add_stmt (c_parser_c99_block_statement (parser));
7672 if (c_cont_label)
7673 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7675 body = pop_stmt_list (body);
7676 c_break_label = save_break;
7677 c_cont_label = save_cont;
7679 while (nbraces)
7681 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7683 c_parser_consume_token (parser);
7684 nbraces--;
7686 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7687 c_parser_consume_token (parser);
7688 else
7690 c_parser_error (parser, "collapsed loops not perfectly nested");
7691 while (nbraces)
7693 stmt = c_begin_compound_stmt (true);
7694 add_stmt (body);
7695 c_parser_compound_statement_nostart (parser);
7696 body = c_end_compound_stmt (stmt, true);
7697 nbraces--;
7699 goto pop_scopes;
7703 /* Only bother calling c_finish_omp_for if we haven't already generated
7704 an error from the initialization parsing. */
7705 if (!fail)
7707 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
7708 if (stmt)
7710 if (par_clauses != NULL)
7712 tree *c;
7713 for (c = par_clauses; *c ; )
7714 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
7715 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
7716 c = &OMP_CLAUSE_CHAIN (*c);
7717 else
7719 for (i = 0; i < collapse; i++)
7720 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
7721 break;
7722 if (i == collapse)
7723 c = &OMP_CLAUSE_CHAIN (*c);
7724 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
7726 error ("%Hiteration variable %qD should not be firstprivate",
7727 &loc, OMP_CLAUSE_DECL (*c));
7728 *c = OMP_CLAUSE_CHAIN (*c);
7730 else
7732 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
7733 change it to shared (decl) in
7734 OMP_PARALLEL_CLAUSES. */
7735 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
7736 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
7737 OMP_CLAUSE_CHAIN (l) = clauses;
7738 clauses = l;
7739 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
7743 OMP_FOR_CLAUSES (stmt) = clauses;
7745 ret = stmt;
7747 pop_scopes:
7748 while (for_block)
7750 stmt = c_end_compound_stmt (TREE_VALUE (for_block), true);
7751 add_stmt (stmt);
7752 for_block = TREE_CHAIN (for_block);
7754 return ret;
7757 /* OpenMP 2.5:
7758 #pragma omp for for-clause[optseq] new-line
7759 for-loop
7762 #define OMP_FOR_CLAUSE_MASK \
7763 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7764 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7765 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7766 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7767 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7768 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7769 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
7770 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7772 static tree
7773 c_parser_omp_for (c_parser *parser)
7775 tree block, clauses, ret;
7777 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7778 "#pragma omp for");
7780 block = c_begin_compound_stmt (true);
7781 ret = c_parser_omp_for_loop (parser, clauses, NULL);
7782 block = c_end_compound_stmt (block, true);
7783 add_stmt (block);
7785 return ret;
7788 /* OpenMP 2.5:
7789 # pragma omp master new-line
7790 structured-block
7793 static tree
7794 c_parser_omp_master (c_parser *parser)
7796 c_parser_skip_to_pragma_eol (parser);
7797 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7800 /* OpenMP 2.5:
7801 # pragma omp ordered new-line
7802 structured-block
7805 static tree
7806 c_parser_omp_ordered (c_parser *parser)
7808 c_parser_skip_to_pragma_eol (parser);
7809 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
7812 /* OpenMP 2.5:
7814 section-scope:
7815 { section-sequence }
7817 section-sequence:
7818 section-directive[opt] structured-block
7819 section-sequence section-directive structured-block */
7821 static tree
7822 c_parser_omp_sections_scope (c_parser *parser)
7824 tree stmt, substmt;
7825 bool error_suppress = false;
7826 location_t loc;
7828 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7830 /* Avoid skipping until the end of the block. */
7831 parser->error = false;
7832 return NULL_TREE;
7835 stmt = push_stmt_list ();
7837 loc = c_parser_peek_token (parser)->location;
7838 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
7840 substmt = push_stmt_list ();
7842 while (1)
7844 c_parser_statement (parser);
7846 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7847 break;
7848 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7849 break;
7850 if (c_parser_next_token_is (parser, CPP_EOF))
7851 break;
7854 substmt = pop_stmt_list (substmt);
7855 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7856 SET_EXPR_LOCATION (substmt, loc);
7857 add_stmt (substmt);
7860 while (1)
7862 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7863 break;
7864 if (c_parser_next_token_is (parser, CPP_EOF))
7865 break;
7867 loc = c_parser_peek_token (parser)->location;
7868 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7870 c_parser_consume_pragma (parser);
7871 c_parser_skip_to_pragma_eol (parser);
7872 error_suppress = false;
7874 else if (!error_suppress)
7876 error ("%Hexpected %<#pragma omp section%> or %<}%>",
7877 &loc);
7878 error_suppress = true;
7881 substmt = c_parser_omp_structured_block (parser);
7882 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7883 SET_EXPR_LOCATION (substmt, loc);
7884 add_stmt (substmt);
7886 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
7887 "expected %<#pragma omp section%> or %<}%>");
7889 substmt = pop_stmt_list (stmt);
7891 stmt = make_node (OMP_SECTIONS);
7892 TREE_TYPE (stmt) = void_type_node;
7893 OMP_SECTIONS_BODY (stmt) = substmt;
7895 return add_stmt (stmt);
7898 /* OpenMP 2.5:
7899 # pragma omp sections sections-clause[optseq] newline
7900 sections-scope
7903 #define OMP_SECTIONS_CLAUSE_MASK \
7904 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7905 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7906 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7907 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7908 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7910 static tree
7911 c_parser_omp_sections (c_parser *parser)
7913 tree block, clauses, ret;
7915 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
7916 "#pragma omp sections");
7918 block = c_begin_compound_stmt (true);
7919 ret = c_parser_omp_sections_scope (parser);
7920 if (ret)
7921 OMP_SECTIONS_CLAUSES (ret) = clauses;
7922 block = c_end_compound_stmt (block, true);
7923 add_stmt (block);
7925 return ret;
7928 /* OpenMP 2.5:
7929 # pragma parallel parallel-clause new-line
7930 # pragma parallel for parallel-for-clause new-line
7931 # pragma parallel sections parallel-sections-clause new-line
7934 #define OMP_PARALLEL_CLAUSE_MASK \
7935 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7936 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7937 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7938 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7939 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7940 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7941 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7942 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7944 static tree
7945 c_parser_omp_parallel (c_parser *parser)
7947 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
7948 const char *p_name = "#pragma omp parallel";
7949 tree stmt, clauses, par_clause, ws_clause, block;
7950 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
7952 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7954 c_parser_consume_token (parser);
7955 p_kind = PRAGMA_OMP_PARALLEL_FOR;
7956 p_name = "#pragma omp parallel for";
7957 mask |= OMP_FOR_CLAUSE_MASK;
7958 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7960 else if (c_parser_next_token_is (parser, CPP_NAME))
7962 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7963 if (strcmp (p, "sections") == 0)
7965 c_parser_consume_token (parser);
7966 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
7967 p_name = "#pragma omp parallel sections";
7968 mask |= OMP_SECTIONS_CLAUSE_MASK;
7969 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7973 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
7975 switch (p_kind)
7977 case PRAGMA_OMP_PARALLEL:
7978 block = c_begin_omp_parallel ();
7979 c_parser_statement (parser);
7980 stmt = c_finish_omp_parallel (clauses, block);
7981 break;
7983 case PRAGMA_OMP_PARALLEL_FOR:
7984 block = c_begin_omp_parallel ();
7985 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7986 c_parser_omp_for_loop (parser, ws_clause, &par_clause);
7987 stmt = c_finish_omp_parallel (par_clause, block);
7988 OMP_PARALLEL_COMBINED (stmt) = 1;
7989 break;
7991 case PRAGMA_OMP_PARALLEL_SECTIONS:
7992 block = c_begin_omp_parallel ();
7993 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7994 stmt = c_parser_omp_sections_scope (parser);
7995 if (stmt)
7996 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
7997 stmt = c_finish_omp_parallel (par_clause, block);
7998 OMP_PARALLEL_COMBINED (stmt) = 1;
7999 break;
8001 default:
8002 gcc_unreachable ();
8005 return stmt;
8008 /* OpenMP 2.5:
8009 # pragma omp single single-clause[optseq] new-line
8010 structured-block
8013 #define OMP_SINGLE_CLAUSE_MASK \
8014 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8015 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8016 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8017 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8019 static tree
8020 c_parser_omp_single (c_parser *parser)
8022 tree stmt = make_node (OMP_SINGLE);
8023 TREE_TYPE (stmt) = void_type_node;
8025 OMP_SINGLE_CLAUSES (stmt)
8026 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8027 "#pragma omp single");
8028 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8030 return add_stmt (stmt);
8033 /* OpenMP 3.0:
8034 # pragma omp task task-clause[optseq] new-line
8037 #define OMP_TASK_CLAUSE_MASK \
8038 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8039 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8040 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8041 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8042 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8043 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8045 static tree
8046 c_parser_omp_task (c_parser *parser)
8048 tree clauses, block;
8050 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8051 "#pragma omp task");
8053 block = c_begin_omp_task ();
8054 c_parser_statement (parser);
8055 return c_finish_omp_task (clauses, block);
8058 /* OpenMP 3.0:
8059 # pragma omp taskwait new-line
8062 static void
8063 c_parser_omp_taskwait (c_parser *parser)
8065 c_parser_consume_pragma (parser);
8066 c_parser_skip_to_pragma_eol (parser);
8068 c_finish_omp_taskwait ();
8071 /* Main entry point to parsing most OpenMP pragmas. */
8073 static void
8074 c_parser_omp_construct (c_parser *parser)
8076 enum pragma_kind p_kind;
8077 location_t loc;
8078 tree stmt;
8080 loc = c_parser_peek_token (parser)->location;
8081 p_kind = c_parser_peek_token (parser)->pragma_kind;
8082 c_parser_consume_pragma (parser);
8084 /* For all constructs below except #pragma omp atomic
8085 MUST_NOT_THROW catch handlers are needed when exceptions
8086 are enabled. */
8087 if (p_kind != PRAGMA_OMP_ATOMIC)
8088 c_maybe_initialize_eh ();
8090 switch (p_kind)
8092 case PRAGMA_OMP_ATOMIC:
8093 c_parser_omp_atomic (parser);
8094 return;
8095 case PRAGMA_OMP_CRITICAL:
8096 stmt = c_parser_omp_critical (parser);
8097 break;
8098 case PRAGMA_OMP_FOR:
8099 stmt = c_parser_omp_for (parser);
8100 break;
8101 case PRAGMA_OMP_MASTER:
8102 stmt = c_parser_omp_master (parser);
8103 break;
8104 case PRAGMA_OMP_ORDERED:
8105 stmt = c_parser_omp_ordered (parser);
8106 break;
8107 case PRAGMA_OMP_PARALLEL:
8108 stmt = c_parser_omp_parallel (parser);
8109 break;
8110 case PRAGMA_OMP_SECTIONS:
8111 stmt = c_parser_omp_sections (parser);
8112 break;
8113 case PRAGMA_OMP_SINGLE:
8114 stmt = c_parser_omp_single (parser);
8115 break;
8116 case PRAGMA_OMP_TASK:
8117 stmt = c_parser_omp_task (parser);
8118 break;
8119 default:
8120 gcc_unreachable ();
8123 if (stmt)
8124 SET_EXPR_LOCATION (stmt, loc);
8128 /* OpenMP 2.5:
8129 # pragma omp threadprivate (variable-list) */
8131 static void
8132 c_parser_omp_threadprivate (c_parser *parser)
8134 tree vars, t;
8136 c_parser_consume_pragma (parser);
8137 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8139 /* Mark every variable in VARS to be assigned thread local storage. */
8140 for (t = vars; t; t = TREE_CHAIN (t))
8142 tree v = TREE_PURPOSE (t);
8144 /* If V had already been marked threadprivate, it doesn't matter
8145 whether it had been used prior to this point. */
8146 if (TREE_CODE (v) != VAR_DECL)
8147 error ("%qD is not a variable", v);
8148 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8149 error ("%qE declared %<threadprivate%> after first use", v);
8150 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8151 error ("automatic variable %qE cannot be %<threadprivate%>", v);
8152 else if (TREE_TYPE (v) == error_mark_node)
8154 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8155 error ("%<threadprivate%> %qE has incomplete type", v);
8156 else
8158 if (! DECL_THREAD_LOCAL_P (v))
8160 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8161 /* If rtl has been already set for this var, call
8162 make_decl_rtl once again, so that encode_section_info
8163 has a chance to look at the new decl flags. */
8164 if (DECL_RTL_SET_P (v))
8165 make_decl_rtl (v);
8167 C_DECL_THREADPRIVATE_P (v) = 1;
8171 c_parser_skip_to_pragma_eol (parser);
8175 /* Parse a single source file. */
8177 void
8178 c_parse_file (void)
8180 /* Use local storage to begin. If the first token is a pragma, parse it.
8181 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8182 which will cause garbage collection. */
8183 c_parser tparser;
8185 memset (&tparser, 0, sizeof tparser);
8186 the_parser = &tparser;
8188 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8189 c_parser_pragma_pch_preprocess (&tparser);
8191 the_parser = GGC_NEW (c_parser);
8192 *the_parser = tparser;
8194 c_parser_translation_unit (the_parser);
8195 the_parser = NULL;
8198 #include "gt-c-parser.h"