Daily bump.
[official-gcc.git] / gcc / c-parser.c
blob27f0b819880cf0062522dcdbc209d88b15184130
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "rtl.h"
46 #include "langhooks.h"
47 #include "input.h"
48 #include "cpplib.h"
49 #include "timevar.h"
50 #include "c-pragma.h"
51 #include "c-tree.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "toplev.h"
55 #include "ggc.h"
56 #include "c-common.h"
57 #include "vec.h"
58 #include "target.h"
59 #include "cgraph.h"
60 #include "plugin.h"
61 #include "except.h"
64 /* Initialization routine for this file. */
66 void
67 c_parse_init (void)
69 /* The only initialization required is of the reserved word
70 identifiers. */
71 unsigned int i;
72 tree id;
73 int mask = 0;
75 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
76 the c_token structure. */
77 gcc_assert (RID_MAX <= 255);
79 mask |= D_CXXONLY;
80 if (!flag_isoc99)
81 mask |= D_C99;
82 if (flag_no_asm)
84 mask |= D_ASM | D_EXT;
85 if (!flag_isoc99)
86 mask |= D_EXT89;
88 if (!c_dialect_objc ())
89 mask |= D_OBJC | D_CXX_OBJC;
91 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
92 for (i = 0; i < num_c_common_reswords; i++)
94 /* If a keyword is disabled, do not enter it into the table
95 and so create a canonical spelling that isn't a keyword. */
96 if (c_common_reswords[i].disable & mask)
98 if (warn_cxx_compat
99 && (c_common_reswords[i].disable & D_CXXWARN))
101 id = get_identifier (c_common_reswords[i].word);
102 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
103 C_IS_RESERVED_WORD (id) = 1;
105 continue;
108 id = get_identifier (c_common_reswords[i].word);
109 C_SET_RID_CODE (id, c_common_reswords[i].rid);
110 C_IS_RESERVED_WORD (id) = 1;
111 ridpointers [(int) c_common_reswords[i].rid] = id;
115 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
116 and the C parser. Unlike the C++ lexer, the parser structure
117 stores the lexer information instead of using a separate structure.
118 Identifiers are separated into ordinary identifiers, type names,
119 keywords and some other Objective-C types of identifiers, and some
120 look-ahead is maintained.
122 ??? It might be a good idea to lex the whole file up front (as for
123 C++). It would then be possible to share more of the C and C++
124 lexer code, if desired. */
126 /* The following local token type is used. */
128 /* A keyword. */
129 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
131 /* More information about the type of a CPP_NAME token. */
132 typedef enum c_id_kind {
133 /* An ordinary identifier. */
134 C_ID_ID,
135 /* An identifier declared as a typedef name. */
136 C_ID_TYPENAME,
137 /* An identifier declared as an Objective-C class name. */
138 C_ID_CLASSNAME,
139 /* An address space identifier. */
140 C_ID_ADDRSPACE,
141 /* Not an identifier. */
142 C_ID_NONE
143 } c_id_kind;
145 /* A single C token after string literal concatenation and conversion
146 of preprocessing tokens to tokens. */
147 typedef struct GTY (()) c_token {
148 /* The kind of token. */
149 ENUM_BITFIELD (cpp_ttype) type : 8;
150 /* If this token is a CPP_NAME, this value indicates whether also
151 declared as some kind of type. Otherwise, it is C_ID_NONE. */
152 ENUM_BITFIELD (c_id_kind) id_kind : 8;
153 /* If this token is a keyword, this value indicates which keyword.
154 Otherwise, this value is RID_MAX. */
155 ENUM_BITFIELD (rid) keyword : 8;
156 /* If this token is a CPP_PRAGMA, this indicates the pragma that
157 was seen. Otherwise it is PRAGMA_NONE. */
158 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
159 /* The value associated with this token, if any. */
160 tree value;
161 /* The location at which this token was found. */
162 location_t location;
163 } c_token;
165 /* A parser structure recording information about the state and
166 context of parsing. Includes lexer information with up to two
167 tokens of look-ahead; more are not needed for C. */
168 typedef struct GTY(()) c_parser {
169 /* The look-ahead tokens. */
170 c_token tokens[2];
171 /* How many look-ahead tokens are available (0, 1 or 2). */
172 short tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
184 /* Objective-C specific parser/lexer information. */
185 BOOL_BITFIELD objc_pq_context : 1;
186 /* The following flag is needed to contextualize Objective-C lexical
187 analysis. In some cases (e.g., 'int NSObject;'), it is
188 undesirable to bind an identifier to an Objective-C class, even
189 if a class with that name exists. */
190 BOOL_BITFIELD objc_need_raw_identifier : 1;
191 } c_parser;
194 /* The actual parser and external interface. ??? Does this need to be
195 garbage-collected? */
197 static GTY (()) c_parser *the_parser;
200 /* Read in and lex a single token, storing it in *TOKEN. */
202 static void
203 c_lex_one_token (c_parser *parser, c_token *token)
205 timevar_push (TV_LEX);
207 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
208 (parser->lex_untranslated_string
209 ? C_LEX_STRING_NO_TRANSLATE : 0));
210 token->id_kind = C_ID_NONE;
211 token->keyword = RID_MAX;
212 token->pragma_kind = PRAGMA_NONE;
214 switch (token->type)
216 case CPP_NAME:
218 tree decl;
220 bool objc_force_identifier = parser->objc_need_raw_identifier;
221 if (c_dialect_objc ())
222 parser->objc_need_raw_identifier = false;
224 if (C_IS_RESERVED_WORD (token->value))
226 enum rid rid_code = C_RID_CODE (token->value);
228 if (rid_code == RID_CXX_COMPAT_WARN)
230 warning_at (token->location,
231 OPT_Wc___compat,
232 "identifier %qE conflicts with C++ keyword",
233 token->value);
235 else if (rid_code >= RID_FIRST_ADDR_SPACE
236 && rid_code <= RID_LAST_ADDR_SPACE)
238 token->id_kind = C_ID_ADDRSPACE;
239 token->keyword = rid_code;
240 break;
242 else if (c_dialect_objc ())
244 if (!objc_is_reserved_word (token->value)
245 && (!OBJC_IS_PQ_KEYWORD (rid_code)
246 || parser->objc_pq_context))
248 /* Return the canonical spelling for this keyword. */
249 token->value = ridpointers[(int) rid_code];
250 token->type = CPP_KEYWORD;
251 token->keyword = rid_code;
252 break;
255 else
257 token->type = CPP_KEYWORD;
258 token->keyword = rid_code;
259 break;
263 decl = lookup_name (token->value);
264 if (decl)
266 if (TREE_CODE (decl) == TYPE_DECL)
268 token->id_kind = C_ID_TYPENAME;
269 break;
272 else if (c_dialect_objc ())
274 tree objc_interface_decl = objc_is_class_name (token->value);
275 /* Objective-C class names are in the same namespace as
276 variables and typedefs, and hence are shadowed by local
277 declarations. */
278 if (objc_interface_decl
279 && (global_bindings_p ()
280 || (!objc_force_identifier && !decl)))
282 token->value = objc_interface_decl;
283 token->id_kind = C_ID_CLASSNAME;
284 break;
287 token->id_kind = C_ID_ID;
289 break;
290 case CPP_AT_NAME:
291 /* This only happens in Objective-C; it must be a keyword. */
292 token->type = CPP_KEYWORD;
293 token->keyword = C_RID_CODE (token->value);
294 break;
295 case CPP_COLON:
296 case CPP_COMMA:
297 case CPP_CLOSE_PAREN:
298 case CPP_SEMICOLON:
299 /* These tokens may affect the interpretation of any identifiers
300 following, if doing Objective-C. */
301 if (c_dialect_objc ())
302 parser->objc_need_raw_identifier = false;
303 break;
304 case CPP_PRAGMA:
305 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
306 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
307 token->value = NULL;
308 break;
309 default:
310 break;
312 timevar_pop (TV_LEX);
315 /* Return a pointer to the next token from PARSER, reading it in if
316 necessary. */
318 static inline c_token *
319 c_parser_peek_token (c_parser *parser)
321 if (parser->tokens_avail == 0)
323 c_lex_one_token (parser, &parser->tokens[0]);
324 parser->tokens_avail = 1;
326 return &parser->tokens[0];
329 /* Return true if the next token from PARSER has the indicated
330 TYPE. */
332 static inline bool
333 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
335 return c_parser_peek_token (parser)->type == type;
338 /* Return true if the next token from PARSER does not have the
339 indicated TYPE. */
341 static inline bool
342 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
344 return !c_parser_next_token_is (parser, type);
347 /* Return true if the next token from PARSER is the indicated
348 KEYWORD. */
350 static inline bool
351 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
353 return c_parser_peek_token (parser)->keyword == keyword;
356 /* Return true if TOKEN can start a type name,
357 false otherwise. */
358 static bool
359 c_token_starts_typename (c_token *token)
361 switch (token->type)
363 case CPP_NAME:
364 switch (token->id_kind)
366 case C_ID_ID:
367 return false;
368 case C_ID_ADDRSPACE:
369 return true;
370 case C_ID_TYPENAME:
371 return true;
372 case C_ID_CLASSNAME:
373 gcc_assert (c_dialect_objc ());
374 return true;
375 default:
376 gcc_unreachable ();
378 case CPP_KEYWORD:
379 switch (token->keyword)
381 case RID_UNSIGNED:
382 case RID_LONG:
383 case RID_SHORT:
384 case RID_SIGNED:
385 case RID_COMPLEX:
386 case RID_INT:
387 case RID_CHAR:
388 case RID_FLOAT:
389 case RID_DOUBLE:
390 case RID_VOID:
391 case RID_DFLOAT32:
392 case RID_DFLOAT64:
393 case RID_DFLOAT128:
394 case RID_BOOL:
395 case RID_ENUM:
396 case RID_STRUCT:
397 case RID_UNION:
398 case RID_TYPEOF:
399 case RID_CONST:
400 case RID_VOLATILE:
401 case RID_RESTRICT:
402 case RID_ATTRIBUTE:
403 case RID_FRACT:
404 case RID_ACCUM:
405 case RID_SAT:
406 return true;
407 default:
408 return false;
410 case CPP_LESS:
411 if (c_dialect_objc ())
412 return true;
413 return false;
414 default:
415 return false;
419 /* Return true if the next token from PARSER can start a type name,
420 false otherwise. */
421 static inline bool
422 c_parser_next_token_starts_typename (c_parser *parser)
424 c_token *token = c_parser_peek_token (parser);
425 return c_token_starts_typename (token);
428 /* Return true if TOKEN can start declaration specifiers, false
429 otherwise. */
430 static bool
431 c_token_starts_declspecs (c_token *token)
433 switch (token->type)
435 case CPP_NAME:
436 switch (token->id_kind)
438 case C_ID_ID:
439 return false;
440 case C_ID_ADDRSPACE:
441 return true;
442 case C_ID_TYPENAME:
443 return true;
444 case C_ID_CLASSNAME:
445 gcc_assert (c_dialect_objc ());
446 return true;
447 default:
448 gcc_unreachable ();
450 case CPP_KEYWORD:
451 switch (token->keyword)
453 case RID_STATIC:
454 case RID_EXTERN:
455 case RID_REGISTER:
456 case RID_TYPEDEF:
457 case RID_INLINE:
458 case RID_AUTO:
459 case RID_THREAD:
460 case RID_UNSIGNED:
461 case RID_LONG:
462 case RID_SHORT:
463 case RID_SIGNED:
464 case RID_COMPLEX:
465 case RID_INT:
466 case RID_CHAR:
467 case RID_FLOAT:
468 case RID_DOUBLE:
469 case RID_VOID:
470 case RID_DFLOAT32:
471 case RID_DFLOAT64:
472 case RID_DFLOAT128:
473 case RID_BOOL:
474 case RID_ENUM:
475 case RID_STRUCT:
476 case RID_UNION:
477 case RID_TYPEOF:
478 case RID_CONST:
479 case RID_VOLATILE:
480 case RID_RESTRICT:
481 case RID_ATTRIBUTE:
482 case RID_FRACT:
483 case RID_ACCUM:
484 case RID_SAT:
485 return true;
486 default:
487 return false;
489 case CPP_LESS:
490 if (c_dialect_objc ())
491 return true;
492 return false;
493 default:
494 return false;
498 /* Return true if the next token from PARSER can start declaration
499 specifiers, false otherwise. */
500 static inline bool
501 c_parser_next_token_starts_declspecs (c_parser *parser)
503 c_token *token = c_parser_peek_token (parser);
504 return c_token_starts_declspecs (token);
507 /* Return a pointer to the next-but-one token from PARSER, reading it
508 in if necessary. The next token is already read in. */
510 static c_token *
511 c_parser_peek_2nd_token (c_parser *parser)
513 if (parser->tokens_avail >= 2)
514 return &parser->tokens[1];
515 gcc_assert (parser->tokens_avail == 1);
516 gcc_assert (parser->tokens[0].type != CPP_EOF);
517 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
518 c_lex_one_token (parser, &parser->tokens[1]);
519 parser->tokens_avail = 2;
520 return &parser->tokens[1];
523 /* Consume the next token from PARSER. */
525 static void
526 c_parser_consume_token (c_parser *parser)
528 gcc_assert (parser->tokens_avail >= 1);
529 gcc_assert (parser->tokens[0].type != CPP_EOF);
530 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
531 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
532 if (parser->tokens_avail == 2)
533 parser->tokens[0] = parser->tokens[1];
534 parser->tokens_avail--;
537 /* Expect the current token to be a #pragma. Consume it and remember
538 that we've begun parsing a pragma. */
540 static void
541 c_parser_consume_pragma (c_parser *parser)
543 gcc_assert (!parser->in_pragma);
544 gcc_assert (parser->tokens_avail >= 1);
545 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
546 if (parser->tokens_avail == 2)
547 parser->tokens[0] = parser->tokens[1];
548 parser->tokens_avail--;
549 parser->in_pragma = true;
552 /* Update the globals input_location and in_system_header from
553 TOKEN. */
554 static inline void
555 c_parser_set_source_position_from_token (c_token *token)
557 if (token->type != CPP_EOF)
559 input_location = token->location;
563 /* Issue a diagnostic of the form
564 FILE:LINE: MESSAGE before TOKEN
565 where TOKEN is the next token in the input stream of PARSER.
566 MESSAGE (specified by the caller) is usually of the form "expected
567 OTHER-TOKEN".
569 Do not issue a diagnostic if still recovering from an error.
571 ??? This is taken from the C++ parser, but building up messages in
572 this way is not i18n-friendly and some other approach should be
573 used. */
575 static void
576 c_parser_error (c_parser *parser, const char *gmsgid)
578 c_token *token = c_parser_peek_token (parser);
579 if (parser->error)
580 return;
581 parser->error = true;
582 if (!gmsgid)
583 return;
584 /* This diagnostic makes more sense if it is tagged to the line of
585 the token we just peeked at. */
586 c_parser_set_source_position_from_token (token);
587 c_parse_error (gmsgid,
588 /* Because c_parse_error does not understand
589 CPP_KEYWORD, keywords are treated like
590 identifiers. */
591 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
592 /* ??? The C parser does not save the cpp flags of a
593 token, we need to pass 0 here and we will not get
594 the source spelling of some tokens but rather the
595 canonical spelling. */
596 token->value, /*flags=*/0);
599 /* If the next token is of the indicated TYPE, consume it. Otherwise,
600 issue the error MSGID. If MSGID is NULL then a message has already
601 been produced and no message will be produced this time. Returns
602 true if found, false otherwise. */
604 static bool
605 c_parser_require (c_parser *parser,
606 enum cpp_ttype type,
607 const char *msgid)
609 if (c_parser_next_token_is (parser, type))
611 c_parser_consume_token (parser);
612 return true;
614 else
616 c_parser_error (parser, msgid);
617 return false;
621 /* If the next token is the indicated keyword, consume it. Otherwise,
622 issue the error MSGID. Returns true if found, false otherwise. */
624 static bool
625 c_parser_require_keyword (c_parser *parser,
626 enum rid keyword,
627 const char *msgid)
629 if (c_parser_next_token_is_keyword (parser, keyword))
631 c_parser_consume_token (parser);
632 return true;
634 else
636 c_parser_error (parser, msgid);
637 return false;
641 /* Like c_parser_require, except that tokens will be skipped until the
642 desired token is found. An error message is still produced if the
643 next token is not as expected. If MSGID is NULL then a message has
644 already been produced and no message will be produced this
645 time. */
647 static void
648 c_parser_skip_until_found (c_parser *parser,
649 enum cpp_ttype type,
650 const char *msgid)
652 unsigned nesting_depth = 0;
654 if (c_parser_require (parser, type, msgid))
655 return;
657 /* Skip tokens until the desired token is found. */
658 while (true)
660 /* Peek at the next token. */
661 c_token *token = c_parser_peek_token (parser);
662 /* If we've reached the token we want, consume it and stop. */
663 if (token->type == type && !nesting_depth)
665 c_parser_consume_token (parser);
666 break;
669 /* If we've run out of tokens, stop. */
670 if (token->type == CPP_EOF)
671 return;
672 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
673 return;
674 if (token->type == CPP_OPEN_BRACE
675 || token->type == CPP_OPEN_PAREN
676 || token->type == CPP_OPEN_SQUARE)
677 ++nesting_depth;
678 else if (token->type == CPP_CLOSE_BRACE
679 || token->type == CPP_CLOSE_PAREN
680 || token->type == CPP_CLOSE_SQUARE)
682 if (nesting_depth-- == 0)
683 break;
685 /* Consume this token. */
686 c_parser_consume_token (parser);
688 parser->error = false;
691 /* Skip tokens until the end of a parameter is found, but do not
692 consume the comma, semicolon or closing delimiter. */
694 static void
695 c_parser_skip_to_end_of_parameter (c_parser *parser)
697 unsigned nesting_depth = 0;
699 while (true)
701 c_token *token = c_parser_peek_token (parser);
702 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
703 && !nesting_depth)
704 break;
705 /* If we've run out of tokens, stop. */
706 if (token->type == CPP_EOF)
707 return;
708 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
709 return;
710 if (token->type == CPP_OPEN_BRACE
711 || token->type == CPP_OPEN_PAREN
712 || token->type == CPP_OPEN_SQUARE)
713 ++nesting_depth;
714 else if (token->type == CPP_CLOSE_BRACE
715 || token->type == CPP_CLOSE_PAREN
716 || token->type == CPP_CLOSE_SQUARE)
718 if (nesting_depth-- == 0)
719 break;
721 /* Consume this token. */
722 c_parser_consume_token (parser);
724 parser->error = false;
727 /* Expect to be at the end of the pragma directive and consume an
728 end of line marker. */
730 static void
731 c_parser_skip_to_pragma_eol (c_parser *parser)
733 gcc_assert (parser->in_pragma);
734 parser->in_pragma = false;
736 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
737 while (true)
739 c_token *token = c_parser_peek_token (parser);
740 if (token->type == CPP_EOF)
741 break;
742 if (token->type == CPP_PRAGMA_EOL)
744 c_parser_consume_token (parser);
745 break;
747 c_parser_consume_token (parser);
750 parser->error = false;
753 /* Skip tokens until we have consumed an entire block, or until we
754 have consumed a non-nested ';'. */
756 static void
757 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
759 unsigned nesting_depth = 0;
760 bool save_error = parser->error;
762 while (true)
764 c_token *token;
766 /* Peek at the next token. */
767 token = c_parser_peek_token (parser);
769 switch (token->type)
771 case CPP_EOF:
772 return;
774 case CPP_PRAGMA_EOL:
775 if (parser->in_pragma)
776 return;
777 break;
779 case CPP_SEMICOLON:
780 /* If the next token is a ';', we have reached the
781 end of the statement. */
782 if (!nesting_depth)
784 /* Consume the ';'. */
785 c_parser_consume_token (parser);
786 goto finished;
788 break;
790 case CPP_CLOSE_BRACE:
791 /* If the next token is a non-nested '}', then we have
792 reached the end of the current block. */
793 if (nesting_depth == 0 || --nesting_depth == 0)
795 c_parser_consume_token (parser);
796 goto finished;
798 break;
800 case CPP_OPEN_BRACE:
801 /* If it the next token is a '{', then we are entering a new
802 block. Consume the entire block. */
803 ++nesting_depth;
804 break;
806 case CPP_PRAGMA:
807 /* If we see a pragma, consume the whole thing at once. We
808 have some safeguards against consuming pragmas willy-nilly.
809 Normally, we'd expect to be here with parser->error set,
810 which disables these safeguards. But it's possible to get
811 here for secondary error recovery, after parser->error has
812 been cleared. */
813 c_parser_consume_pragma (parser);
814 c_parser_skip_to_pragma_eol (parser);
815 parser->error = save_error;
816 continue;
818 default:
819 break;
822 c_parser_consume_token (parser);
825 finished:
826 parser->error = false;
829 /* CPP's options (initialized by c-opts.c). */
830 extern cpp_options *cpp_opts;
832 /* Save the warning flags which are controlled by __extension__. */
834 static inline int
835 disable_extension_diagnostics (void)
837 int ret = (pedantic
838 | (warn_pointer_arith << 1)
839 | (warn_traditional << 2)
840 | (flag_iso << 3)
841 | (warn_long_long << 4)
842 | (warn_cxx_compat << 5));
843 cpp_opts->pedantic = pedantic = 0;
844 warn_pointer_arith = 0;
845 cpp_opts->warn_traditional = warn_traditional = 0;
846 flag_iso = 0;
847 cpp_opts->warn_long_long = warn_long_long = 0;
848 warn_cxx_compat = 0;
849 return ret;
852 /* Restore the warning flags which are controlled by __extension__.
853 FLAGS is the return value from disable_extension_diagnostics. */
855 static inline void
856 restore_extension_diagnostics (int flags)
858 cpp_opts->pedantic = pedantic = flags & 1;
859 warn_pointer_arith = (flags >> 1) & 1;
860 cpp_opts->warn_traditional = warn_traditional = (flags >> 2) & 1;
861 flag_iso = (flags >> 3) & 1;
862 cpp_opts->warn_long_long = warn_long_long = (flags >> 4) & 1;
863 warn_cxx_compat = (flags >> 5) & 1;
866 /* Possibly kinds of declarator to parse. */
867 typedef enum c_dtr_syn {
868 /* A normal declarator with an identifier. */
869 C_DTR_NORMAL,
870 /* An abstract declarator (maybe empty). */
871 C_DTR_ABSTRACT,
872 /* A parameter declarator: may be either, but after a type name does
873 not redeclare a typedef name as an identifier if it can
874 alternatively be interpreted as a typedef name; see DR#009,
875 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
876 following DR#249. For example, given a typedef T, "int T" and
877 "int *T" are valid parameter declarations redeclaring T, while
878 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
879 abstract declarators rather than involving redundant parentheses;
880 the same applies with attributes inside the parentheses before
881 "T". */
882 C_DTR_PARM
883 } c_dtr_syn;
885 static void c_parser_external_declaration (c_parser *);
886 static void c_parser_asm_definition (c_parser *);
887 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
888 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
889 bool);
890 static struct c_typespec c_parser_enum_specifier (c_parser *);
891 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
892 static tree c_parser_struct_declaration (c_parser *);
893 static struct c_typespec c_parser_typeof_specifier (c_parser *);
894 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
895 bool *);
896 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
897 c_dtr_syn, bool *);
898 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
899 bool,
900 struct c_declarator *);
901 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
902 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
903 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
904 static tree c_parser_simple_asm_expr (c_parser *);
905 static tree c_parser_attributes (c_parser *);
906 static struct c_type_name *c_parser_type_name (c_parser *);
907 static struct c_expr c_parser_initializer (c_parser *);
908 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
909 static void c_parser_initelt (c_parser *);
910 static void c_parser_initval (c_parser *, struct c_expr *);
911 static tree c_parser_compound_statement (c_parser *);
912 static void c_parser_compound_statement_nostart (c_parser *);
913 static void c_parser_label (c_parser *);
914 static void c_parser_statement (c_parser *);
915 static void c_parser_statement_after_labels (c_parser *);
916 static void c_parser_if_statement (c_parser *);
917 static void c_parser_switch_statement (c_parser *);
918 static void c_parser_while_statement (c_parser *);
919 static void c_parser_do_statement (c_parser *);
920 static void c_parser_for_statement (c_parser *);
921 static tree c_parser_asm_statement (c_parser *);
922 static tree c_parser_asm_operands (c_parser *, bool);
923 static tree c_parser_asm_goto_operands (c_parser *);
924 static tree c_parser_asm_clobbers (c_parser *);
925 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
926 static struct c_expr c_parser_conditional_expression (c_parser *,
927 struct c_expr *);
928 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
929 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
930 static struct c_expr c_parser_unary_expression (c_parser *);
931 static struct c_expr c_parser_sizeof_expression (c_parser *);
932 static struct c_expr c_parser_alignof_expression (c_parser *);
933 static struct c_expr c_parser_postfix_expression (c_parser *);
934 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
935 struct c_type_name *,
936 location_t);
937 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
938 location_t loc,
939 struct c_expr);
940 static struct c_expr c_parser_expression (c_parser *);
941 static struct c_expr c_parser_expression_conv (c_parser *);
942 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
943 VEC(tree,gc) **);
944 static void c_parser_omp_construct (c_parser *);
945 static void c_parser_omp_threadprivate (c_parser *);
946 static void c_parser_omp_barrier (c_parser *);
947 static void c_parser_omp_flush (c_parser *);
948 static void c_parser_omp_taskwait (c_parser *);
950 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
951 static bool c_parser_pragma (c_parser *, enum pragma_context);
953 /* These Objective-C parser functions are only ever called when
954 compiling Objective-C. */
955 static void c_parser_objc_class_definition (c_parser *);
956 static void c_parser_objc_class_instance_variables (c_parser *);
957 static void c_parser_objc_class_declaration (c_parser *);
958 static void c_parser_objc_alias_declaration (c_parser *);
959 static void c_parser_objc_protocol_definition (c_parser *);
960 static enum tree_code c_parser_objc_method_type (c_parser *);
961 static void c_parser_objc_method_definition (c_parser *);
962 static void c_parser_objc_methodprotolist (c_parser *);
963 static void c_parser_objc_methodproto (c_parser *);
964 static tree c_parser_objc_method_decl (c_parser *);
965 static tree c_parser_objc_type_name (c_parser *);
966 static tree c_parser_objc_protocol_refs (c_parser *);
967 static void c_parser_objc_try_catch_statement (c_parser *);
968 static void c_parser_objc_synchronized_statement (c_parser *);
969 static tree c_parser_objc_selector (c_parser *);
970 static tree c_parser_objc_selector_arg (c_parser *);
971 static tree c_parser_objc_receiver (c_parser *);
972 static tree c_parser_objc_message_args (c_parser *);
973 static tree c_parser_objc_keywordexpr (c_parser *);
975 /* Parse a translation unit (C90 6.7, C99 6.9).
977 translation-unit:
978 external-declarations
980 external-declarations:
981 external-declaration
982 external-declarations external-declaration
984 GNU extensions:
986 translation-unit:
987 empty
990 static void
991 c_parser_translation_unit (c_parser *parser)
993 if (c_parser_next_token_is (parser, CPP_EOF))
995 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
996 "ISO C forbids an empty translation unit");
998 else
1000 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1001 mark_valid_location_for_stdc_pragma (false);
1004 ggc_collect ();
1005 c_parser_external_declaration (parser);
1006 obstack_free (&parser_obstack, obstack_position);
1008 while (c_parser_next_token_is_not (parser, CPP_EOF));
1012 /* Parse an external declaration (C90 6.7, C99 6.9).
1014 external-declaration:
1015 function-definition
1016 declaration
1018 GNU extensions:
1020 external-declaration:
1021 asm-definition
1023 __extension__ external-declaration
1025 Objective-C:
1027 external-declaration:
1028 objc-class-definition
1029 objc-class-declaration
1030 objc-alias-declaration
1031 objc-protocol-definition
1032 objc-method-definition
1033 @end
1036 static void
1037 c_parser_external_declaration (c_parser *parser)
1039 int ext;
1040 switch (c_parser_peek_token (parser)->type)
1042 case CPP_KEYWORD:
1043 switch (c_parser_peek_token (parser)->keyword)
1045 case RID_EXTENSION:
1046 ext = disable_extension_diagnostics ();
1047 c_parser_consume_token (parser);
1048 c_parser_external_declaration (parser);
1049 restore_extension_diagnostics (ext);
1050 break;
1051 case RID_ASM:
1052 c_parser_asm_definition (parser);
1053 break;
1054 case RID_AT_INTERFACE:
1055 case RID_AT_IMPLEMENTATION:
1056 gcc_assert (c_dialect_objc ());
1057 c_parser_objc_class_definition (parser);
1058 break;
1059 case RID_CLASS:
1060 gcc_assert (c_dialect_objc ());
1061 c_parser_objc_class_declaration (parser);
1062 break;
1063 case RID_AT_ALIAS:
1064 gcc_assert (c_dialect_objc ());
1065 c_parser_objc_alias_declaration (parser);
1066 break;
1067 case RID_AT_PROTOCOL:
1068 gcc_assert (c_dialect_objc ());
1069 c_parser_objc_protocol_definition (parser);
1070 break;
1071 case RID_AT_END:
1072 gcc_assert (c_dialect_objc ());
1073 c_parser_consume_token (parser);
1074 objc_finish_implementation ();
1075 break;
1076 default:
1077 goto decl_or_fndef;
1079 break;
1080 case CPP_SEMICOLON:
1081 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1082 "ISO C does not allow extra %<;%> outside of a function");
1083 c_parser_consume_token (parser);
1084 break;
1085 case CPP_PRAGMA:
1086 mark_valid_location_for_stdc_pragma (true);
1087 c_parser_pragma (parser, pragma_external);
1088 mark_valid_location_for_stdc_pragma (false);
1089 break;
1090 case CPP_PLUS:
1091 case CPP_MINUS:
1092 if (c_dialect_objc ())
1094 c_parser_objc_method_definition (parser);
1095 break;
1097 /* Else fall through, and yield a syntax error trying to parse
1098 as a declaration or function definition. */
1099 default:
1100 decl_or_fndef:
1101 /* A declaration or a function definition. We can only tell
1102 which after parsing the declaration specifiers, if any, and
1103 the first declarator. */
1104 c_parser_declaration_or_fndef (parser, true, true, false, true);
1105 break;
1110 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1111 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1112 accepted; otherwise (old-style parameter declarations) only other
1113 declarations are accepted. If NESTED is true, we are inside a
1114 function or parsing old-style parameter declarations; any functions
1115 encountered are nested functions and declaration specifiers are
1116 required; otherwise we are at top level and functions are normal
1117 functions and declaration specifiers may be optional. If EMPTY_OK
1118 is true, empty declarations are OK (subject to all other
1119 constraints); otherwise (old-style parameter declarations) they are
1120 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1121 may start with attributes; otherwise they may not.
1123 declaration:
1124 declaration-specifiers init-declarator-list[opt] ;
1126 function-definition:
1127 declaration-specifiers[opt] declarator declaration-list[opt]
1128 compound-statement
1130 declaration-list:
1131 declaration
1132 declaration-list declaration
1134 init-declarator-list:
1135 init-declarator
1136 init-declarator-list , init-declarator
1138 init-declarator:
1139 declarator simple-asm-expr[opt] attributes[opt]
1140 declarator simple-asm-expr[opt] attributes[opt] = initializer
1142 GNU extensions:
1144 nested-function-definition:
1145 declaration-specifiers declarator declaration-list[opt]
1146 compound-statement
1148 The simple-asm-expr and attributes are GNU extensions.
1150 This function does not handle __extension__; that is handled in its
1151 callers. ??? Following the old parser, __extension__ may start
1152 external declarations, declarations in functions and declarations
1153 at the start of "for" loops, but not old-style parameter
1154 declarations.
1156 C99 requires declaration specifiers in a function definition; the
1157 absence is diagnosed through the diagnosis of implicit int. In GNU
1158 C we also allow but diagnose declarations without declaration
1159 specifiers, but only at top level (elsewhere they conflict with
1160 other syntax).
1162 OpenMP:
1164 declaration:
1165 threadprivate-directive */
1167 static void
1168 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1169 bool nested, bool start_attr_ok)
1171 struct c_declspecs *specs;
1172 tree prefix_attrs;
1173 tree all_prefix_attrs;
1174 bool diagnosed_no_specs = false;
1175 location_t here = c_parser_peek_token (parser)->location;
1177 specs = build_null_declspecs ();
1178 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1179 if (parser->error)
1181 c_parser_skip_to_end_of_block_or_statement (parser);
1182 return;
1184 if (nested && !specs->declspecs_seen_p)
1186 c_parser_error (parser, "expected declaration specifiers");
1187 c_parser_skip_to_end_of_block_or_statement (parser);
1188 return;
1190 finish_declspecs (specs);
1191 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1193 if (empty_ok)
1194 shadow_tag (specs);
1195 else
1197 shadow_tag_warned (specs, 1);
1198 pedwarn (here, 0, "empty declaration");
1200 c_parser_consume_token (parser);
1201 return;
1203 pending_xref_error ();
1204 prefix_attrs = specs->attrs;
1205 all_prefix_attrs = prefix_attrs;
1206 specs->attrs = NULL_TREE;
1207 while (true)
1209 struct c_declarator *declarator;
1210 bool dummy = false;
1211 tree fnbody;
1212 /* Declaring either one or more declarators (in which case we
1213 should diagnose if there were no declaration specifiers) or a
1214 function definition (in which case the diagnostic for
1215 implicit int suffices). */
1216 declarator = c_parser_declarator (parser, specs->type_seen_p,
1217 C_DTR_NORMAL, &dummy);
1218 if (declarator == NULL)
1220 c_parser_skip_to_end_of_block_or_statement (parser);
1221 return;
1223 if (c_parser_next_token_is (parser, CPP_EQ)
1224 || c_parser_next_token_is (parser, CPP_COMMA)
1225 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1226 || c_parser_next_token_is_keyword (parser, RID_ASM)
1227 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1229 tree asm_name = NULL_TREE;
1230 tree postfix_attrs = NULL_TREE;
1231 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1233 diagnosed_no_specs = true;
1234 pedwarn (here, 0, "data definition has no type or storage class");
1236 /* Having seen a data definition, there cannot now be a
1237 function definition. */
1238 fndef_ok = false;
1239 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1240 asm_name = c_parser_simple_asm_expr (parser);
1241 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1242 postfix_attrs = c_parser_attributes (parser);
1243 if (c_parser_next_token_is (parser, CPP_EQ))
1245 tree d;
1246 struct c_expr init;
1247 location_t init_loc;
1248 c_parser_consume_token (parser);
1249 /* The declaration of the variable is in effect while
1250 its initializer is parsed. */
1251 d = start_decl (declarator, specs, true,
1252 chainon (postfix_attrs, all_prefix_attrs));
1253 if (!d)
1254 d = error_mark_node;
1255 start_init (d, asm_name, global_bindings_p ());
1256 init_loc = c_parser_peek_token (parser)->location;
1257 init = c_parser_initializer (parser);
1258 finish_init ();
1259 if (d != error_mark_node)
1261 maybe_warn_string_init (TREE_TYPE (d), init);
1262 finish_decl (d, init_loc, init.value,
1263 init.original_type, asm_name);
1266 else
1268 tree d = start_decl (declarator, specs, false,
1269 chainon (postfix_attrs,
1270 all_prefix_attrs));
1271 if (d)
1272 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1273 NULL_TREE, asm_name);
1275 if (c_parser_next_token_is (parser, CPP_COMMA))
1277 c_parser_consume_token (parser);
1278 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1279 all_prefix_attrs = chainon (c_parser_attributes (parser),
1280 prefix_attrs);
1281 else
1282 all_prefix_attrs = prefix_attrs;
1283 continue;
1285 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1287 c_parser_consume_token (parser);
1288 return;
1290 else
1292 c_parser_error (parser, "expected %<,%> or %<;%>");
1293 c_parser_skip_to_end_of_block_or_statement (parser);
1294 return;
1297 else if (!fndef_ok)
1299 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1300 "%<asm%> or %<__attribute__%>");
1301 c_parser_skip_to_end_of_block_or_statement (parser);
1302 return;
1304 /* Function definition (nested or otherwise). */
1305 if (nested)
1307 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1308 c_push_function_context ();
1310 if (!start_function (specs, declarator, all_prefix_attrs))
1312 /* This can appear in many cases looking nothing like a
1313 function definition, so we don't give a more specific
1314 error suggesting there was one. */
1315 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1316 "or %<__attribute__%>");
1317 if (nested)
1318 c_pop_function_context ();
1319 break;
1321 /* Parse old-style parameter declarations. ??? Attributes are
1322 not allowed to start declaration specifiers here because of a
1323 syntax conflict between a function declaration with attribute
1324 suffix and a function definition with an attribute prefix on
1325 first old-style parameter declaration. Following the old
1326 parser, they are not accepted on subsequent old-style
1327 parameter declarations either. However, there is no
1328 ambiguity after the first declaration, nor indeed on the
1329 first as long as we don't allow postfix attributes after a
1330 declarator with a nonempty identifier list in a definition;
1331 and postfix attributes have never been accepted here in
1332 function definitions either. */
1333 while (c_parser_next_token_is_not (parser, CPP_EOF)
1334 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1335 c_parser_declaration_or_fndef (parser, false, false, true, false);
1336 store_parm_decls ();
1337 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1338 = c_parser_peek_token (parser)->location;
1339 fnbody = c_parser_compound_statement (parser);
1340 if (nested)
1342 tree decl = current_function_decl;
1343 /* Mark nested functions as needing static-chain initially.
1344 lower_nested_functions will recompute it but the
1345 DECL_STATIC_CHAIN flag is also used before that happens,
1346 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1347 DECL_STATIC_CHAIN (decl) = 1;
1348 add_stmt (fnbody);
1349 finish_function ();
1350 c_pop_function_context ();
1351 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1353 else
1355 add_stmt (fnbody);
1356 finish_function ();
1358 break;
1362 /* Parse an asm-definition (asm() outside a function body). This is a
1363 GNU extension.
1365 asm-definition:
1366 simple-asm-expr ;
1369 static void
1370 c_parser_asm_definition (c_parser *parser)
1372 tree asm_str = c_parser_simple_asm_expr (parser);
1373 if (asm_str)
1374 cgraph_add_asm_node (asm_str);
1375 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1378 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1379 6.7), adding them to SPECS (which may already include some).
1380 Storage class specifiers are accepted iff SCSPEC_OK; type
1381 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1382 the start iff START_ATTR_OK.
1384 declaration-specifiers:
1385 storage-class-specifier declaration-specifiers[opt]
1386 type-specifier declaration-specifiers[opt]
1387 type-qualifier declaration-specifiers[opt]
1388 function-specifier declaration-specifiers[opt]
1390 Function specifiers (inline) are from C99, and are currently
1391 handled as storage class specifiers, as is __thread.
1393 C90 6.5.1, C99 6.7.1:
1394 storage-class-specifier:
1395 typedef
1396 extern
1397 static
1398 auto
1399 register
1401 C99 6.7.4:
1402 function-specifier:
1403 inline
1405 C90 6.5.2, C99 6.7.2:
1406 type-specifier:
1407 void
1408 char
1409 short
1411 long
1412 float
1413 double
1414 signed
1415 unsigned
1416 _Bool
1417 _Complex
1418 [_Imaginary removed in C99 TC2]
1419 struct-or-union-specifier
1420 enum-specifier
1421 typedef-name
1423 (_Bool and _Complex are new in C99.)
1425 C90 6.5.3, C99 6.7.3:
1427 type-qualifier:
1428 const
1429 restrict
1430 volatile
1431 address-space-qualifier
1433 (restrict is new in C99.)
1435 GNU extensions:
1437 declaration-specifiers:
1438 attributes declaration-specifiers[opt]
1440 type-qualifier:
1441 address-space
1443 address-space:
1444 identifier recognized by the target
1446 storage-class-specifier:
1447 __thread
1449 type-specifier:
1450 typeof-specifier
1451 _Decimal32
1452 _Decimal64
1453 _Decimal128
1454 _Fract
1455 _Accum
1456 _Sat
1458 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1459 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1461 Objective-C:
1463 type-specifier:
1464 class-name objc-protocol-refs[opt]
1465 typedef-name objc-protocol-refs
1466 objc-protocol-refs
1469 static void
1470 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1471 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1473 bool attrs_ok = start_attr_ok;
1474 bool seen_type = specs->type_seen_p;
1475 while (c_parser_next_token_is (parser, CPP_NAME)
1476 || c_parser_next_token_is (parser, CPP_KEYWORD)
1477 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1479 struct c_typespec t;
1480 tree attrs;
1481 location_t loc = c_parser_peek_token (parser)->location;
1482 if (c_parser_next_token_is (parser, CPP_NAME))
1484 tree value = c_parser_peek_token (parser)->value;
1485 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1487 if (kind == C_ID_ADDRSPACE)
1489 addr_space_t as
1490 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1491 declspecs_add_addrspace (specs, as);
1492 c_parser_consume_token (parser);
1493 attrs_ok = true;
1494 continue;
1497 /* This finishes the specifiers unless a type name is OK, it
1498 is declared as a type name and a type name hasn't yet
1499 been seen. */
1500 if (!typespec_ok || seen_type
1501 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1502 break;
1503 c_parser_consume_token (parser);
1504 seen_type = true;
1505 attrs_ok = true;
1506 if (kind == C_ID_TYPENAME
1507 && (!c_dialect_objc ()
1508 || c_parser_next_token_is_not (parser, CPP_LESS)))
1510 t.kind = ctsk_typedef;
1511 /* For a typedef name, record the meaning, not the name.
1512 In case of 'foo foo, bar;'. */
1513 t.spec = lookup_name (value);
1514 t.expr = NULL_TREE;
1515 t.expr_const_operands = true;
1517 else
1519 tree proto = NULL_TREE;
1520 gcc_assert (c_dialect_objc ());
1521 t.kind = ctsk_objc;
1522 if (c_parser_next_token_is (parser, CPP_LESS))
1523 proto = c_parser_objc_protocol_refs (parser);
1524 t.spec = objc_get_protocol_qualified_type (value, proto);
1525 t.expr = NULL_TREE;
1526 t.expr_const_operands = true;
1528 declspecs_add_type (loc, specs, t);
1529 continue;
1531 if (c_parser_next_token_is (parser, CPP_LESS))
1533 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1534 nisse@lysator.liu.se. */
1535 tree proto;
1536 gcc_assert (c_dialect_objc ());
1537 if (!typespec_ok || seen_type)
1538 break;
1539 proto = c_parser_objc_protocol_refs (parser);
1540 t.kind = ctsk_objc;
1541 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1542 t.expr = NULL_TREE;
1543 t.expr_const_operands = true;
1544 declspecs_add_type (loc, specs, t);
1545 continue;
1547 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1548 switch (c_parser_peek_token (parser)->keyword)
1550 case RID_STATIC:
1551 case RID_EXTERN:
1552 case RID_REGISTER:
1553 case RID_TYPEDEF:
1554 case RID_INLINE:
1555 case RID_AUTO:
1556 case RID_THREAD:
1557 if (!scspec_ok)
1558 goto out;
1559 attrs_ok = true;
1560 /* TODO: Distinguish between function specifiers (inline)
1561 and storage class specifiers, either here or in
1562 declspecs_add_scspec. */
1563 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1564 c_parser_consume_token (parser);
1565 break;
1566 case RID_UNSIGNED:
1567 case RID_LONG:
1568 case RID_SHORT:
1569 case RID_SIGNED:
1570 case RID_COMPLEX:
1571 case RID_INT:
1572 case RID_CHAR:
1573 case RID_FLOAT:
1574 case RID_DOUBLE:
1575 case RID_VOID:
1576 case RID_DFLOAT32:
1577 case RID_DFLOAT64:
1578 case RID_DFLOAT128:
1579 case RID_BOOL:
1580 case RID_FRACT:
1581 case RID_ACCUM:
1582 case RID_SAT:
1583 if (!typespec_ok)
1584 goto out;
1585 attrs_ok = true;
1586 seen_type = true;
1587 if (c_dialect_objc ())
1588 parser->objc_need_raw_identifier = true;
1589 t.kind = ctsk_resword;
1590 t.spec = c_parser_peek_token (parser)->value;
1591 t.expr = NULL_TREE;
1592 t.expr_const_operands = true;
1593 declspecs_add_type (loc, specs, t);
1594 c_parser_consume_token (parser);
1595 break;
1596 case RID_ENUM:
1597 if (!typespec_ok)
1598 goto out;
1599 attrs_ok = true;
1600 seen_type = true;
1601 t = c_parser_enum_specifier (parser);
1602 declspecs_add_type (loc, specs, t);
1603 break;
1604 case RID_STRUCT:
1605 case RID_UNION:
1606 if (!typespec_ok)
1607 goto out;
1608 attrs_ok = true;
1609 seen_type = true;
1610 t = c_parser_struct_or_union_specifier (parser);
1611 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
1612 declspecs_add_type (loc, specs, t);
1613 break;
1614 case RID_TYPEOF:
1615 /* ??? The old parser rejected typeof after other type
1616 specifiers, but is a syntax error the best way of
1617 handling this? */
1618 if (!typespec_ok || seen_type)
1619 goto out;
1620 attrs_ok = true;
1621 seen_type = true;
1622 t = c_parser_typeof_specifier (parser);
1623 declspecs_add_type (loc, specs, t);
1624 break;
1625 case RID_CONST:
1626 case RID_VOLATILE:
1627 case RID_RESTRICT:
1628 attrs_ok = true;
1629 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1630 c_parser_consume_token (parser);
1631 break;
1632 case RID_ATTRIBUTE:
1633 if (!attrs_ok)
1634 goto out;
1635 attrs = c_parser_attributes (parser);
1636 declspecs_add_attrs (specs, attrs);
1637 break;
1638 default:
1639 goto out;
1642 out: ;
1645 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1647 enum-specifier:
1648 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1649 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1650 enum attributes[opt] identifier
1652 The form with trailing comma is new in C99. The forms with
1653 attributes are GNU extensions. In GNU C, we accept any expression
1654 without commas in the syntax (assignment expressions, not just
1655 conditional expressions); assignment expressions will be diagnosed
1656 as non-constant.
1658 enumerator-list:
1659 enumerator
1660 enumerator-list , enumerator
1662 enumerator:
1663 enumeration-constant
1664 enumeration-constant = constant-expression
1667 static struct c_typespec
1668 c_parser_enum_specifier (c_parser *parser)
1670 struct c_typespec ret;
1671 tree attrs;
1672 tree ident = NULL_TREE;
1673 location_t enum_loc;
1674 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1675 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1676 enum_loc = c_parser_peek_token (parser)->location;
1677 c_parser_consume_token (parser);
1678 attrs = c_parser_attributes (parser);
1679 enum_loc = c_parser_peek_token (parser)->location;
1680 /* Set the location in case we create a decl now. */
1681 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1682 if (c_parser_next_token_is (parser, CPP_NAME))
1684 ident = c_parser_peek_token (parser)->value;
1685 ident_loc = c_parser_peek_token (parser)->location;
1686 enum_loc = ident_loc;
1687 c_parser_consume_token (parser);
1689 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1691 /* Parse an enum definition. */
1692 struct c_enum_contents the_enum;
1693 tree type = start_enum (enum_loc, &the_enum, ident);
1694 tree postfix_attrs;
1695 /* We chain the enumerators in reverse order, then put them in
1696 forward order at the end. */
1697 tree values = NULL_TREE;
1698 c_parser_consume_token (parser);
1699 while (true)
1701 tree enum_id;
1702 tree enum_value;
1703 tree enum_decl;
1704 bool seen_comma;
1705 c_token *token;
1706 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
1707 location_t value_loc;
1708 if (c_parser_next_token_is_not (parser, CPP_NAME))
1710 c_parser_error (parser, "expected identifier");
1711 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1712 values = error_mark_node;
1713 break;
1715 token = c_parser_peek_token (parser);
1716 enum_id = token->value;
1717 /* Set the location in case we create a decl now. */
1718 c_parser_set_source_position_from_token (token);
1719 value_loc = token->location;
1720 c_parser_consume_token (parser);
1721 if (c_parser_next_token_is (parser, CPP_EQ))
1723 c_parser_consume_token (parser);
1724 value_loc = c_parser_peek_token (parser)->location;
1725 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1727 else
1728 enum_value = NULL_TREE;
1729 enum_decl = build_enumerator (value_loc,
1730 &the_enum, enum_id, enum_value);
1731 TREE_CHAIN (enum_decl) = values;
1732 values = enum_decl;
1733 seen_comma = false;
1734 if (c_parser_next_token_is (parser, CPP_COMMA))
1736 comma_loc = c_parser_peek_token (parser)->location;
1737 seen_comma = true;
1738 c_parser_consume_token (parser);
1740 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1742 if (seen_comma && !flag_isoc99)
1743 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
1744 c_parser_consume_token (parser);
1745 break;
1747 if (!seen_comma)
1749 c_parser_error (parser, "expected %<,%> or %<}%>");
1750 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1751 values = error_mark_node;
1752 break;
1755 postfix_attrs = c_parser_attributes (parser);
1756 ret.spec = finish_enum (type, nreverse (values),
1757 chainon (attrs, postfix_attrs));
1758 ret.kind = ctsk_tagdef;
1759 ret.expr = NULL_TREE;
1760 ret.expr_const_operands = true;
1761 return ret;
1763 else if (!ident)
1765 c_parser_error (parser, "expected %<{%>");
1766 ret.spec = error_mark_node;
1767 ret.kind = ctsk_tagref;
1768 ret.expr = NULL_TREE;
1769 ret.expr_const_operands = true;
1770 return ret;
1772 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
1773 /* In ISO C, enumerated types can be referred to only if already
1774 defined. */
1775 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1777 gcc_assert (ident);
1778 pedwarn (enum_loc, OPT_pedantic,
1779 "ISO C forbids forward references to %<enum%> types");
1781 return ret;
1784 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1786 struct-or-union-specifier:
1787 struct-or-union attributes[opt] identifier[opt]
1788 { struct-contents } attributes[opt]
1789 struct-or-union attributes[opt] identifier
1791 struct-contents:
1792 struct-declaration-list
1794 struct-declaration-list:
1795 struct-declaration ;
1796 struct-declaration-list struct-declaration ;
1798 GNU extensions:
1800 struct-contents:
1801 empty
1802 struct-declaration
1803 struct-declaration-list struct-declaration
1805 struct-declaration-list:
1806 struct-declaration-list ;
1809 (Note that in the syntax here, unlike that in ISO C, the semicolons
1810 are included here rather than in struct-declaration, in order to
1811 describe the syntax with extra semicolons and missing semicolon at
1812 end.)
1814 Objective-C:
1816 struct-declaration-list:
1817 @defs ( class-name )
1819 (Note this does not include a trailing semicolon, but can be
1820 followed by further declarations, and gets a pedwarn-if-pedantic
1821 when followed by a semicolon.) */
1823 static struct c_typespec
1824 c_parser_struct_or_union_specifier (c_parser *parser)
1826 struct c_typespec ret;
1827 tree attrs;
1828 tree ident = NULL_TREE;
1829 location_t struct_loc;
1830 location_t ident_loc = UNKNOWN_LOCATION;
1831 enum tree_code code;
1832 switch (c_parser_peek_token (parser)->keyword)
1834 case RID_STRUCT:
1835 code = RECORD_TYPE;
1836 break;
1837 case RID_UNION:
1838 code = UNION_TYPE;
1839 break;
1840 default:
1841 gcc_unreachable ();
1843 struct_loc = c_parser_peek_token (parser)->location;
1844 c_parser_consume_token (parser);
1845 attrs = c_parser_attributes (parser);
1847 /* Set the location in case we create a decl now. */
1848 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
1850 if (c_parser_next_token_is (parser, CPP_NAME))
1852 ident = c_parser_peek_token (parser)->value;
1853 ident_loc = c_parser_peek_token (parser)->location;
1854 struct_loc = ident_loc;
1855 c_parser_consume_token (parser);
1857 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1859 /* Parse a struct or union definition. Start the scope of the
1860 tag before parsing components. */
1861 struct c_struct_parse_info *struct_info;
1862 tree type = start_struct (struct_loc, code, ident, &struct_info);
1863 tree postfix_attrs;
1864 /* We chain the components in reverse order, then put them in
1865 forward order at the end. Each struct-declaration may
1866 declare multiple components (comma-separated), so we must use
1867 chainon to join them, although when parsing each
1868 struct-declaration we can use TREE_CHAIN directly.
1870 The theory behind all this is that there will be more
1871 semicolon separated fields than comma separated fields, and
1872 so we'll be minimizing the number of node traversals required
1873 by chainon. */
1874 tree contents = NULL_TREE;
1875 c_parser_consume_token (parser);
1876 /* Handle the Objective-C @defs construct,
1877 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1878 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1880 tree name;
1881 gcc_assert (c_dialect_objc ());
1882 c_parser_consume_token (parser);
1883 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1884 goto end_at_defs;
1885 if (c_parser_next_token_is (parser, CPP_NAME)
1886 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1888 name = c_parser_peek_token (parser)->value;
1889 c_parser_consume_token (parser);
1891 else
1893 c_parser_error (parser, "expected class name");
1894 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1895 goto end_at_defs;
1897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1898 "expected %<)%>");
1899 contents = nreverse (objc_get_class_ivars (name));
1901 end_at_defs:
1902 /* Parse the struct-declarations and semicolons. Problems with
1903 semicolons are diagnosed here; empty structures are diagnosed
1904 elsewhere. */
1905 while (true)
1907 tree decls;
1908 /* Parse any stray semicolon. */
1909 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1911 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1912 "extra semicolon in struct or union specified");
1913 c_parser_consume_token (parser);
1914 continue;
1916 /* Stop if at the end of the struct or union contents. */
1917 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1919 c_parser_consume_token (parser);
1920 break;
1922 /* Accept #pragmas at struct scope. */
1923 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1925 c_parser_pragma (parser, pragma_external);
1926 continue;
1928 /* Parse some comma-separated declarations, but not the
1929 trailing semicolon if any. */
1930 decls = c_parser_struct_declaration (parser);
1931 contents = chainon (decls, contents);
1932 /* If no semicolon follows, either we have a parse error or
1933 are at the end of the struct or union and should
1934 pedwarn. */
1935 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1936 c_parser_consume_token (parser);
1937 else
1939 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1940 pedwarn (c_parser_peek_token (parser)->location, 0,
1941 "no semicolon at end of struct or union");
1942 else
1944 c_parser_error (parser, "expected %<;%>");
1945 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1946 break;
1950 postfix_attrs = c_parser_attributes (parser);
1951 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
1952 chainon (attrs, postfix_attrs), struct_info);
1953 ret.kind = ctsk_tagdef;
1954 ret.expr = NULL_TREE;
1955 ret.expr_const_operands = true;
1956 return ret;
1958 else if (!ident)
1960 c_parser_error (parser, "expected %<{%>");
1961 ret.spec = error_mark_node;
1962 ret.kind = ctsk_tagref;
1963 ret.expr = NULL_TREE;
1964 ret.expr_const_operands = true;
1965 return ret;
1967 ret = parser_xref_tag (ident_loc, code, ident);
1968 return ret;
1971 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1972 the trailing semicolon.
1974 struct-declaration:
1975 specifier-qualifier-list struct-declarator-list
1977 specifier-qualifier-list:
1978 type-specifier specifier-qualifier-list[opt]
1979 type-qualifier specifier-qualifier-list[opt]
1980 attributes specifier-qualifier-list[opt]
1982 struct-declarator-list:
1983 struct-declarator
1984 struct-declarator-list , attributes[opt] struct-declarator
1986 struct-declarator:
1987 declarator attributes[opt]
1988 declarator[opt] : constant-expression attributes[opt]
1990 GNU extensions:
1992 struct-declaration:
1993 __extension__ struct-declaration
1994 specifier-qualifier-list
1996 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1997 of attributes where shown is a GNU extension. In GNU C, we accept
1998 any expression without commas in the syntax (assignment
1999 expressions, not just conditional expressions); assignment
2000 expressions will be diagnosed as non-constant. */
2002 static tree
2003 c_parser_struct_declaration (c_parser *parser)
2005 struct c_declspecs *specs;
2006 tree prefix_attrs;
2007 tree all_prefix_attrs;
2008 tree decls;
2009 location_t decl_loc;
2010 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2012 int ext;
2013 tree decl;
2014 ext = disable_extension_diagnostics ();
2015 c_parser_consume_token (parser);
2016 decl = c_parser_struct_declaration (parser);
2017 restore_extension_diagnostics (ext);
2018 return decl;
2020 specs = build_null_declspecs ();
2021 decl_loc = c_parser_peek_token (parser)->location;
2022 c_parser_declspecs (parser, specs, false, true, true);
2023 if (parser->error)
2024 return NULL_TREE;
2025 if (!specs->declspecs_seen_p)
2027 c_parser_error (parser, "expected specifier-qualifier-list");
2028 return NULL_TREE;
2030 finish_declspecs (specs);
2031 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2033 tree ret;
2034 if (!specs->type_seen_p)
2036 pedwarn (decl_loc, OPT_pedantic,
2037 "ISO C forbids member declarations with no members");
2038 shadow_tag_warned (specs, pedantic);
2039 ret = NULL_TREE;
2041 else
2043 /* Support for unnamed structs or unions as members of
2044 structs or unions (which is [a] useful and [b] supports
2045 MS P-SDK). */
2046 tree attrs = NULL;
2048 ret = grokfield (c_parser_peek_token (parser)->location,
2049 build_id_declarator (NULL_TREE), specs,
2050 NULL_TREE, &attrs);
2051 if (ret)
2052 decl_attributes (&ret, attrs, 0);
2054 return ret;
2056 pending_xref_error ();
2057 prefix_attrs = specs->attrs;
2058 all_prefix_attrs = prefix_attrs;
2059 specs->attrs = NULL_TREE;
2060 decls = NULL_TREE;
2061 while (true)
2063 /* Declaring one or more declarators or un-named bit-fields. */
2064 struct c_declarator *declarator;
2065 bool dummy = false;
2066 if (c_parser_next_token_is (parser, CPP_COLON))
2067 declarator = build_id_declarator (NULL_TREE);
2068 else
2069 declarator = c_parser_declarator (parser, specs->type_seen_p,
2070 C_DTR_NORMAL, &dummy);
2071 if (declarator == NULL)
2073 c_parser_skip_to_end_of_block_or_statement (parser);
2074 break;
2076 if (c_parser_next_token_is (parser, CPP_COLON)
2077 || c_parser_next_token_is (parser, CPP_COMMA)
2078 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2079 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2080 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2082 tree postfix_attrs = NULL_TREE;
2083 tree width = NULL_TREE;
2084 tree d;
2085 if (c_parser_next_token_is (parser, CPP_COLON))
2087 c_parser_consume_token (parser);
2088 width = c_parser_expr_no_commas (parser, NULL).value;
2090 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2091 postfix_attrs = c_parser_attributes (parser);
2092 d = grokfield (c_parser_peek_token (parser)->location,
2093 declarator, specs, width, &all_prefix_attrs);
2094 decl_attributes (&d, chainon (postfix_attrs,
2095 all_prefix_attrs), 0);
2096 TREE_CHAIN (d) = decls;
2097 decls = d;
2098 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2099 all_prefix_attrs = chainon (c_parser_attributes (parser),
2100 prefix_attrs);
2101 else
2102 all_prefix_attrs = prefix_attrs;
2103 if (c_parser_next_token_is (parser, CPP_COMMA))
2104 c_parser_consume_token (parser);
2105 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2106 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2108 /* Semicolon consumed in caller. */
2109 break;
2111 else
2113 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2114 break;
2117 else
2119 c_parser_error (parser,
2120 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2121 "%<__attribute__%>");
2122 break;
2125 return decls;
2128 /* Parse a typeof specifier (a GNU extension).
2130 typeof-specifier:
2131 typeof ( expression )
2132 typeof ( type-name )
2135 static struct c_typespec
2136 c_parser_typeof_specifier (c_parser *parser)
2138 struct c_typespec ret;
2139 ret.kind = ctsk_typeof;
2140 ret.spec = error_mark_node;
2141 ret.expr = NULL_TREE;
2142 ret.expr_const_operands = true;
2143 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2144 c_parser_consume_token (parser);
2145 c_inhibit_evaluation_warnings++;
2146 in_typeof++;
2147 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2149 c_inhibit_evaluation_warnings--;
2150 in_typeof--;
2151 return ret;
2153 if (c_parser_next_token_starts_typename (parser))
2155 struct c_type_name *type = c_parser_type_name (parser);
2156 c_inhibit_evaluation_warnings--;
2157 in_typeof--;
2158 if (type != NULL)
2160 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2161 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2164 else
2166 bool was_vm;
2167 location_t here = c_parser_peek_token (parser)->location;
2168 struct c_expr expr = c_parser_expression (parser);
2169 c_inhibit_evaluation_warnings--;
2170 in_typeof--;
2171 if (TREE_CODE (expr.value) == COMPONENT_REF
2172 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2173 error_at (here, "%<typeof%> applied to a bit-field");
2174 mark_exp_read (expr.value);
2175 ret.spec = TREE_TYPE (expr.value);
2176 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2177 /* This is returned with the type so that when the type is
2178 evaluated, this can be evaluated. */
2179 if (was_vm)
2180 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2181 pop_maybe_used (was_vm);
2183 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2184 return ret;
2187 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2188 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2189 be redeclared; otherwise it may not. KIND indicates which kind of
2190 declarator is wanted. Returns a valid declarator except in the
2191 case of a syntax error in which case NULL is returned. *SEEN_ID is
2192 set to true if an identifier being declared is seen; this is used
2193 to diagnose bad forms of abstract array declarators and to
2194 determine whether an identifier list is syntactically permitted.
2196 declarator:
2197 pointer[opt] direct-declarator
2199 direct-declarator:
2200 identifier
2201 ( attributes[opt] declarator )
2202 direct-declarator array-declarator
2203 direct-declarator ( parameter-type-list )
2204 direct-declarator ( identifier-list[opt] )
2206 pointer:
2207 * type-qualifier-list[opt]
2208 * type-qualifier-list[opt] pointer
2210 type-qualifier-list:
2211 type-qualifier
2212 attributes
2213 type-qualifier-list type-qualifier
2214 type-qualifier-list attributes
2216 parameter-type-list:
2217 parameter-list
2218 parameter-list , ...
2220 parameter-list:
2221 parameter-declaration
2222 parameter-list , parameter-declaration
2224 parameter-declaration:
2225 declaration-specifiers declarator attributes[opt]
2226 declaration-specifiers abstract-declarator[opt] attributes[opt]
2228 identifier-list:
2229 identifier
2230 identifier-list , identifier
2232 abstract-declarator:
2233 pointer
2234 pointer[opt] direct-abstract-declarator
2236 direct-abstract-declarator:
2237 ( attributes[opt] abstract-declarator )
2238 direct-abstract-declarator[opt] array-declarator
2239 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2241 GNU extensions:
2243 direct-declarator:
2244 direct-declarator ( parameter-forward-declarations
2245 parameter-type-list[opt] )
2247 direct-abstract-declarator:
2248 direct-abstract-declarator[opt] ( parameter-forward-declarations
2249 parameter-type-list[opt] )
2251 parameter-forward-declarations:
2252 parameter-list ;
2253 parameter-forward-declarations parameter-list ;
2255 The uses of attributes shown above are GNU extensions.
2257 Some forms of array declarator are not included in C99 in the
2258 syntax for abstract declarators; these are disallowed elsewhere.
2259 This may be a defect (DR#289).
2261 This function also accepts an omitted abstract declarator as being
2262 an abstract declarator, although not part of the formal syntax. */
2264 static struct c_declarator *
2265 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2266 bool *seen_id)
2268 /* Parse any initial pointer part. */
2269 if (c_parser_next_token_is (parser, CPP_MULT))
2271 struct c_declspecs *quals_attrs = build_null_declspecs ();
2272 struct c_declarator *inner;
2273 c_parser_consume_token (parser);
2274 c_parser_declspecs (parser, quals_attrs, false, false, true);
2275 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2276 if (inner == NULL)
2277 return NULL;
2278 else
2279 return make_pointer_declarator (quals_attrs, inner);
2281 /* Now we have a direct declarator, direct abstract declarator or
2282 nothing (which counts as a direct abstract declarator here). */
2283 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2286 /* Parse a direct declarator or direct abstract declarator; arguments
2287 as c_parser_declarator. */
2289 static struct c_declarator *
2290 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2291 bool *seen_id)
2293 /* The direct declarator must start with an identifier (possibly
2294 omitted) or a parenthesized declarator (possibly abstract). In
2295 an ordinary declarator, initial parentheses must start a
2296 parenthesized declarator. In an abstract declarator or parameter
2297 declarator, they could start a parenthesized declarator or a
2298 parameter list. To tell which, the open parenthesis and any
2299 following attributes must be read. If a declaration specifier
2300 follows, then it is a parameter list; if the specifier is a
2301 typedef name, there might be an ambiguity about redeclaring it,
2302 which is resolved in the direction of treating it as a typedef
2303 name. If a close parenthesis follows, it is also an empty
2304 parameter list, as the syntax does not permit empty abstract
2305 declarators. Otherwise, it is a parenthesized declarator (in
2306 which case the analysis may be repeated inside it, recursively).
2308 ??? There is an ambiguity in a parameter declaration "int
2309 (__attribute__((foo)) x)", where x is not a typedef name: it
2310 could be an abstract declarator for a function, or declare x with
2311 parentheses. The proper resolution of this ambiguity needs
2312 documenting. At present we follow an accident of the old
2313 parser's implementation, whereby the first parameter must have
2314 some declaration specifiers other than just attributes. Thus as
2315 a parameter declaration it is treated as a parenthesized
2316 parameter named x, and as an abstract declarator it is
2317 rejected.
2319 ??? Also following the old parser, attributes inside an empty
2320 parameter list are ignored, making it a list not yielding a
2321 prototype, rather than giving an error or making it have one
2322 parameter with implicit type int.
2324 ??? Also following the old parser, typedef names may be
2325 redeclared in declarators, but not Objective-C class names. */
2327 if (kind != C_DTR_ABSTRACT
2328 && c_parser_next_token_is (parser, CPP_NAME)
2329 && ((type_seen_p
2330 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2331 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2333 struct c_declarator *inner
2334 = build_id_declarator (c_parser_peek_token (parser)->value);
2335 *seen_id = true;
2336 inner->id_loc = c_parser_peek_token (parser)->location;
2337 c_parser_consume_token (parser);
2338 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2341 if (kind != C_DTR_NORMAL
2342 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2344 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2345 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2348 /* Either we are at the end of an abstract declarator, or we have
2349 parentheses. */
2351 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2353 tree attrs;
2354 struct c_declarator *inner;
2355 c_parser_consume_token (parser);
2356 attrs = c_parser_attributes (parser);
2357 if (kind != C_DTR_NORMAL
2358 && (c_parser_next_token_starts_declspecs (parser)
2359 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2361 struct c_arg_info *args
2362 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2363 attrs);
2364 if (args == NULL)
2365 return NULL;
2366 else
2368 inner
2369 = build_function_declarator (args,
2370 build_id_declarator (NULL_TREE));
2371 return c_parser_direct_declarator_inner (parser, *seen_id,
2372 inner);
2375 /* A parenthesized declarator. */
2376 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2377 if (inner != NULL && attrs != NULL)
2378 inner = build_attrs_declarator (attrs, inner);
2379 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2381 c_parser_consume_token (parser);
2382 if (inner == NULL)
2383 return NULL;
2384 else
2385 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2387 else
2389 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2390 "expected %<)%>");
2391 return NULL;
2394 else
2396 if (kind == C_DTR_NORMAL)
2398 c_parser_error (parser, "expected identifier or %<(%>");
2399 return NULL;
2401 else
2402 return build_id_declarator (NULL_TREE);
2406 /* Parse part of a direct declarator or direct abstract declarator,
2407 given that some (in INNER) has already been parsed; ID_PRESENT is
2408 true if an identifier is present, false for an abstract
2409 declarator. */
2411 static struct c_declarator *
2412 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2413 struct c_declarator *inner)
2415 /* Parse a sequence of array declarators and parameter lists. */
2416 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2418 location_t brace_loc = c_parser_peek_token (parser)->location;
2419 struct c_declarator *declarator;
2420 struct c_declspecs *quals_attrs = build_null_declspecs ();
2421 bool static_seen;
2422 bool star_seen;
2423 tree dimen;
2424 c_parser_consume_token (parser);
2425 c_parser_declspecs (parser, quals_attrs, false, false, true);
2426 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2427 if (static_seen)
2428 c_parser_consume_token (parser);
2429 if (static_seen && !quals_attrs->declspecs_seen_p)
2430 c_parser_declspecs (parser, quals_attrs, false, false, true);
2431 if (!quals_attrs->declspecs_seen_p)
2432 quals_attrs = NULL;
2433 /* If "static" is present, there must be an array dimension.
2434 Otherwise, there may be a dimension, "*", or no
2435 dimension. */
2436 if (static_seen)
2438 star_seen = false;
2439 dimen = c_parser_expr_no_commas (parser, NULL).value;
2441 else
2443 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2445 dimen = NULL_TREE;
2446 star_seen = false;
2448 else if (c_parser_next_token_is (parser, CPP_MULT))
2450 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2452 dimen = NULL_TREE;
2453 star_seen = true;
2454 c_parser_consume_token (parser);
2456 else
2458 star_seen = false;
2459 dimen = c_parser_expr_no_commas (parser, NULL).value;
2462 else
2464 star_seen = false;
2465 dimen = c_parser_expr_no_commas (parser, NULL).value;
2468 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2469 c_parser_consume_token (parser);
2470 else
2472 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2473 "expected %<]%>");
2474 return NULL;
2476 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
2477 static_seen, star_seen);
2478 if (declarator == NULL)
2479 return NULL;
2480 inner = set_array_declarator_inner (declarator, inner);
2481 return c_parser_direct_declarator_inner (parser, id_present, inner);
2483 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2485 tree attrs;
2486 struct c_arg_info *args;
2487 c_parser_consume_token (parser);
2488 attrs = c_parser_attributes (parser);
2489 args = c_parser_parms_declarator (parser, id_present, attrs);
2490 if (args == NULL)
2491 return NULL;
2492 else
2494 inner = build_function_declarator (args, inner);
2495 return c_parser_direct_declarator_inner (parser, id_present, inner);
2498 return inner;
2501 /* Parse a parameter list or identifier list, including the closing
2502 parenthesis but not the opening one. ATTRS are the attributes at
2503 the start of the list. ID_LIST_OK is true if an identifier list is
2504 acceptable; such a list must not have attributes at the start. */
2506 static struct c_arg_info *
2507 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2509 push_scope ();
2510 declare_parm_level ();
2511 /* If the list starts with an identifier, it is an identifier list.
2512 Otherwise, it is either a prototype list or an empty list. */
2513 if (id_list_ok
2514 && !attrs
2515 && c_parser_next_token_is (parser, CPP_NAME)
2516 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2518 tree list = NULL_TREE, *nextp = &list;
2519 while (c_parser_next_token_is (parser, CPP_NAME)
2520 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2522 *nextp = build_tree_list (NULL_TREE,
2523 c_parser_peek_token (parser)->value);
2524 nextp = & TREE_CHAIN (*nextp);
2525 c_parser_consume_token (parser);
2526 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2527 break;
2528 c_parser_consume_token (parser);
2529 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2531 c_parser_error (parser, "expected identifier");
2532 break;
2535 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2537 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2538 ret->parms = 0;
2539 ret->tags = 0;
2540 ret->types = list;
2541 ret->others = 0;
2542 ret->pending_sizes = 0;
2543 ret->had_vla_unspec = 0;
2544 c_parser_consume_token (parser);
2545 pop_scope ();
2546 return ret;
2548 else
2550 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2551 "expected %<)%>");
2552 pop_scope ();
2553 return NULL;
2556 else
2558 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2559 pop_scope ();
2560 return ret;
2564 /* Parse a parameter list (possibly empty), including the closing
2565 parenthesis but not the opening one. ATTRS are the attributes at
2566 the start of the list. */
2568 static struct c_arg_info *
2569 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2571 bool good_parm = false;
2572 /* ??? Following the old parser, forward parameter declarations may
2573 use abstract declarators, and if no real parameter declarations
2574 follow the forward declarations then this is not diagnosed. Also
2575 note as above that attributes are ignored as the only contents of
2576 the parentheses, or as the only contents after forward
2577 declarations. */
2578 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2580 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2581 ret->parms = 0;
2582 ret->tags = 0;
2583 ret->types = 0;
2584 ret->others = 0;
2585 ret->pending_sizes = 0;
2586 ret->had_vla_unspec = 0;
2587 c_parser_consume_token (parser);
2588 return ret;
2590 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2592 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2593 ret->parms = 0;
2594 ret->tags = 0;
2595 ret->others = 0;
2596 ret->pending_sizes = 0;
2597 ret->had_vla_unspec = 0;
2598 /* Suppress -Wold-style-definition for this case. */
2599 ret->types = error_mark_node;
2600 error_at (c_parser_peek_token (parser)->location,
2601 "ISO C requires a named argument before %<...%>");
2602 c_parser_consume_token (parser);
2603 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2605 c_parser_consume_token (parser);
2606 return ret;
2608 else
2610 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2611 "expected %<)%>");
2612 return NULL;
2615 /* Nonempty list of parameters, either terminated with semicolon
2616 (forward declarations; recurse) or with close parenthesis (normal
2617 function) or with ", ... )" (variadic function). */
2618 while (true)
2620 /* Parse a parameter. */
2621 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2622 attrs = NULL_TREE;
2623 if (parm != NULL)
2625 good_parm = true;
2626 push_parm_decl (parm);
2628 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2630 tree new_attrs;
2631 c_parser_consume_token (parser);
2632 mark_forward_parm_decls ();
2633 new_attrs = c_parser_attributes (parser);
2634 return c_parser_parms_list_declarator (parser, new_attrs);
2636 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2638 c_parser_consume_token (parser);
2639 if (good_parm)
2640 return get_parm_info (false);
2641 else
2643 struct c_arg_info *ret
2644 = XOBNEW (&parser_obstack, struct c_arg_info);
2645 ret->parms = 0;
2646 ret->tags = 0;
2647 ret->types = 0;
2648 ret->others = 0;
2649 ret->pending_sizes = 0;
2650 ret->had_vla_unspec = 0;
2651 return ret;
2654 if (!c_parser_require (parser, CPP_COMMA,
2655 "expected %<;%>, %<,%> or %<)%>"))
2657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2658 get_pending_sizes ();
2659 return NULL;
2661 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2663 c_parser_consume_token (parser);
2664 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2666 c_parser_consume_token (parser);
2667 if (good_parm)
2668 return get_parm_info (true);
2669 else
2671 struct c_arg_info *ret
2672 = XOBNEW (&parser_obstack, struct c_arg_info);
2673 ret->parms = 0;
2674 ret->tags = 0;
2675 ret->types = 0;
2676 ret->others = 0;
2677 ret->pending_sizes = 0;
2678 ret->had_vla_unspec = 0;
2679 return ret;
2682 else
2684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2685 "expected %<)%>");
2686 get_pending_sizes ();
2687 return NULL;
2693 /* Parse a parameter declaration. ATTRS are the attributes at the
2694 start of the declaration if it is the first parameter. */
2696 static struct c_parm *
2697 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2699 struct c_declspecs *specs;
2700 struct c_declarator *declarator;
2701 tree prefix_attrs;
2702 tree postfix_attrs = NULL_TREE;
2703 bool dummy = false;
2704 if (!c_parser_next_token_starts_declspecs (parser))
2706 /* ??? In some Objective-C cases '...' isn't applicable so there
2707 should be a different message. */
2708 c_parser_error (parser,
2709 "expected declaration specifiers or %<...%>");
2710 c_parser_skip_to_end_of_parameter (parser);
2711 return NULL;
2713 specs = build_null_declspecs ();
2714 if (attrs)
2716 declspecs_add_attrs (specs, attrs);
2717 attrs = NULL_TREE;
2719 c_parser_declspecs (parser, specs, true, true, true);
2720 finish_declspecs (specs);
2721 pending_xref_error ();
2722 prefix_attrs = specs->attrs;
2723 specs->attrs = NULL_TREE;
2724 declarator = c_parser_declarator (parser, specs->type_seen_p,
2725 C_DTR_PARM, &dummy);
2726 if (declarator == NULL)
2728 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2729 return NULL;
2731 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2732 postfix_attrs = c_parser_attributes (parser);
2733 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2734 declarator);
2737 /* Parse a string literal in an asm expression. It should not be
2738 translated, and wide string literals are an error although
2739 permitted by the syntax. This is a GNU extension.
2741 asm-string-literal:
2742 string-literal
2744 ??? At present, following the old parser, the caller needs to have
2745 set lex_untranslated_string to 1. It would be better to follow the
2746 C++ parser rather than using this kludge. */
2748 static tree
2749 c_parser_asm_string_literal (c_parser *parser)
2751 tree str;
2752 if (c_parser_next_token_is (parser, CPP_STRING))
2754 str = c_parser_peek_token (parser)->value;
2755 c_parser_consume_token (parser);
2757 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2759 error_at (c_parser_peek_token (parser)->location,
2760 "wide string literal in %<asm%>");
2761 str = build_string (1, "");
2762 c_parser_consume_token (parser);
2764 else
2766 c_parser_error (parser, "expected string literal");
2767 str = NULL_TREE;
2769 return str;
2772 /* Parse a simple asm expression. This is used in restricted
2773 contexts, where a full expression with inputs and outputs does not
2774 make sense. This is a GNU extension.
2776 simple-asm-expr:
2777 asm ( asm-string-literal )
2780 static tree
2781 c_parser_simple_asm_expr (c_parser *parser)
2783 tree str;
2784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2785 /* ??? Follow the C++ parser rather than using the
2786 lex_untranslated_string kludge. */
2787 parser->lex_untranslated_string = true;
2788 c_parser_consume_token (parser);
2789 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2791 parser->lex_untranslated_string = false;
2792 return NULL_TREE;
2794 str = c_parser_asm_string_literal (parser);
2795 parser->lex_untranslated_string = false;
2796 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2799 return NULL_TREE;
2801 return str;
2804 /* Parse (possibly empty) attributes. This is a GNU extension.
2806 attributes:
2807 empty
2808 attributes attribute
2810 attribute:
2811 __attribute__ ( ( attribute-list ) )
2813 attribute-list:
2814 attrib
2815 attribute_list , attrib
2817 attrib:
2818 empty
2819 any-word
2820 any-word ( identifier )
2821 any-word ( identifier , nonempty-expr-list )
2822 any-word ( expr-list )
2824 where the "identifier" must not be declared as a type, and
2825 "any-word" may be any identifier (including one declared as a
2826 type), a reserved word storage class specifier, type specifier or
2827 type qualifier. ??? This still leaves out most reserved keywords
2828 (following the old parser), shouldn't we include them, and why not
2829 allow identifiers declared as types to start the arguments? */
2831 static tree
2832 c_parser_attributes (c_parser *parser)
2834 tree attrs = NULL_TREE;
2835 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2837 /* ??? Follow the C++ parser rather than using the
2838 lex_untranslated_string kludge. */
2839 parser->lex_untranslated_string = true;
2840 c_parser_consume_token (parser);
2841 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2843 parser->lex_untranslated_string = false;
2844 return attrs;
2846 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2848 parser->lex_untranslated_string = false;
2849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2850 return attrs;
2852 /* Parse the attribute list. */
2853 while (c_parser_next_token_is (parser, CPP_COMMA)
2854 || c_parser_next_token_is (parser, CPP_NAME)
2855 || c_parser_next_token_is (parser, CPP_KEYWORD))
2857 tree attr, attr_name, attr_args;
2858 VEC(tree,gc) *expr_list;
2859 if (c_parser_next_token_is (parser, CPP_COMMA))
2861 c_parser_consume_token (parser);
2862 continue;
2864 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2866 /* ??? See comment above about what keywords are
2867 accepted here. */
2868 bool ok;
2869 switch (c_parser_peek_token (parser)->keyword)
2871 case RID_STATIC:
2872 case RID_UNSIGNED:
2873 case RID_LONG:
2874 case RID_CONST:
2875 case RID_EXTERN:
2876 case RID_REGISTER:
2877 case RID_TYPEDEF:
2878 case RID_SHORT:
2879 case RID_INLINE:
2880 case RID_VOLATILE:
2881 case RID_SIGNED:
2882 case RID_AUTO:
2883 case RID_RESTRICT:
2884 case RID_COMPLEX:
2885 case RID_THREAD:
2886 case RID_INT:
2887 case RID_CHAR:
2888 case RID_FLOAT:
2889 case RID_DOUBLE:
2890 case RID_VOID:
2891 case RID_DFLOAT32:
2892 case RID_DFLOAT64:
2893 case RID_DFLOAT128:
2894 case RID_BOOL:
2895 case RID_FRACT:
2896 case RID_ACCUM:
2897 case RID_SAT:
2898 ok = true;
2899 break;
2900 default:
2901 ok = false;
2902 break;
2904 if (!ok)
2905 break;
2906 /* Accept __attribute__((__const)) as __attribute__((const))
2907 etc. */
2908 attr_name
2909 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
2911 else
2912 attr_name = c_parser_peek_token (parser)->value;
2913 c_parser_consume_token (parser);
2914 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2916 attr = build_tree_list (attr_name, NULL_TREE);
2917 attrs = chainon (attrs, attr);
2918 continue;
2920 c_parser_consume_token (parser);
2921 /* Parse the attribute contents. If they start with an
2922 identifier which is followed by a comma or close
2923 parenthesis, then the arguments start with that
2924 identifier; otherwise they are an expression list. */
2925 if (c_parser_next_token_is (parser, CPP_NAME)
2926 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2927 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2928 || (c_parser_peek_2nd_token (parser)->type
2929 == CPP_CLOSE_PAREN)))
2931 tree arg1 = c_parser_peek_token (parser)->value;
2932 c_parser_consume_token (parser);
2933 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2934 attr_args = build_tree_list (NULL_TREE, arg1);
2935 else
2937 tree tree_list;
2938 c_parser_consume_token (parser);
2939 expr_list = c_parser_expr_list (parser, false, true, NULL);
2940 tree_list = build_tree_list_vec (expr_list);
2941 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
2942 release_tree_vector (expr_list);
2945 else
2947 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2948 attr_args = NULL_TREE;
2949 else
2951 expr_list = c_parser_expr_list (parser, false, true, NULL);
2952 attr_args = build_tree_list_vec (expr_list);
2953 release_tree_vector (expr_list);
2956 attr = build_tree_list (attr_name, attr_args);
2957 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2958 c_parser_consume_token (parser);
2959 else
2961 parser->lex_untranslated_string = false;
2962 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2963 "expected %<)%>");
2964 return attrs;
2966 attrs = chainon (attrs, attr);
2968 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2969 c_parser_consume_token (parser);
2970 else
2972 parser->lex_untranslated_string = false;
2973 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2974 "expected %<)%>");
2975 return attrs;
2977 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2978 c_parser_consume_token (parser);
2979 else
2981 parser->lex_untranslated_string = false;
2982 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2983 "expected %<)%>");
2984 return attrs;
2986 parser->lex_untranslated_string = false;
2988 return attrs;
2991 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2993 type-name:
2994 specifier-qualifier-list abstract-declarator[opt]
2997 static struct c_type_name *
2998 c_parser_type_name (c_parser *parser)
3000 struct c_declspecs *specs = build_null_declspecs ();
3001 struct c_declarator *declarator;
3002 struct c_type_name *ret;
3003 bool dummy = false;
3004 c_parser_declspecs (parser, specs, false, true, true);
3005 if (!specs->declspecs_seen_p)
3007 c_parser_error (parser, "expected specifier-qualifier-list");
3008 return NULL;
3010 pending_xref_error ();
3011 finish_declspecs (specs);
3012 declarator = c_parser_declarator (parser, specs->type_seen_p,
3013 C_DTR_ABSTRACT, &dummy);
3014 if (declarator == NULL)
3015 return NULL;
3016 ret = XOBNEW (&parser_obstack, struct c_type_name);
3017 ret->specs = specs;
3018 ret->declarator = declarator;
3019 return ret;
3022 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3024 initializer:
3025 assignment-expression
3026 { initializer-list }
3027 { initializer-list , }
3029 initializer-list:
3030 designation[opt] initializer
3031 initializer-list , designation[opt] initializer
3033 designation:
3034 designator-list =
3036 designator-list:
3037 designator
3038 designator-list designator
3040 designator:
3041 array-designator
3042 . identifier
3044 array-designator:
3045 [ constant-expression ]
3047 GNU extensions:
3049 initializer:
3052 designation:
3053 array-designator
3054 identifier :
3056 array-designator:
3057 [ constant-expression ... constant-expression ]
3059 Any expression without commas is accepted in the syntax for the
3060 constant-expressions, with non-constant expressions rejected later.
3062 This function is only used for top-level initializers; for nested
3063 ones, see c_parser_initval. */
3065 static struct c_expr
3066 c_parser_initializer (c_parser *parser)
3068 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3069 return c_parser_braced_init (parser, NULL_TREE, false);
3070 else
3072 struct c_expr ret;
3073 location_t loc = c_parser_peek_token (parser)->location;
3074 ret = c_parser_expr_no_commas (parser, NULL);
3075 if (TREE_CODE (ret.value) != STRING_CST
3076 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3077 ret = default_function_array_read_conversion (loc, ret);
3078 return ret;
3082 /* Parse a braced initializer list. TYPE is the type specified for a
3083 compound literal, and NULL_TREE for other initializers and for
3084 nested braced lists. NESTED_P is true for nested braced lists,
3085 false for the list of a compound literal or the list that is the
3086 top-level initializer in a declaration. */
3088 static struct c_expr
3089 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3091 location_t brace_loc = c_parser_peek_token (parser)->location;
3092 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3093 c_parser_consume_token (parser);
3094 if (nested_p)
3095 push_init_level (0);
3096 else
3097 really_start_incremental_init (type);
3098 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3100 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3102 else
3104 /* Parse a non-empty initializer list, possibly with a trailing
3105 comma. */
3106 while (true)
3108 c_parser_initelt (parser);
3109 if (parser->error)
3110 break;
3111 if (c_parser_next_token_is (parser, CPP_COMMA))
3112 c_parser_consume_token (parser);
3113 else
3114 break;
3115 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3116 break;
3119 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3121 struct c_expr ret;
3122 ret.value = error_mark_node;
3123 ret.original_code = ERROR_MARK;
3124 ret.original_type = NULL;
3125 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3126 pop_init_level (0);
3127 return ret;
3129 c_parser_consume_token (parser);
3130 return pop_init_level (0);
3133 /* Parse a nested initializer, including designators. */
3135 static void
3136 c_parser_initelt (c_parser *parser)
3138 /* Parse any designator or designator list. A single array
3139 designator may have the subsequent "=" omitted in GNU C, but a
3140 longer list or a structure member designator may not. */
3141 if (c_parser_next_token_is (parser, CPP_NAME)
3142 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3144 /* Old-style structure member designator. */
3145 set_init_label (c_parser_peek_token (parser)->value);
3146 /* Use the colon as the error location. */
3147 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3148 "obsolete use of designated initializer with %<:%>");
3149 c_parser_consume_token (parser);
3150 c_parser_consume_token (parser);
3152 else
3154 /* des_seen is 0 if there have been no designators, 1 if there
3155 has been a single array designator and 2 otherwise. */
3156 int des_seen = 0;
3157 /* Location of a designator. */
3158 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3159 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3160 || c_parser_next_token_is (parser, CPP_DOT))
3162 int des_prev = des_seen;
3163 if (!des_seen)
3164 des_loc = c_parser_peek_token (parser)->location;
3165 if (des_seen < 2)
3166 des_seen++;
3167 if (c_parser_next_token_is (parser, CPP_DOT))
3169 des_seen = 2;
3170 c_parser_consume_token (parser);
3171 if (c_parser_next_token_is (parser, CPP_NAME))
3173 set_init_label (c_parser_peek_token (parser)->value);
3174 c_parser_consume_token (parser);
3176 else
3178 struct c_expr init;
3179 init.value = error_mark_node;
3180 init.original_code = ERROR_MARK;
3181 init.original_type = NULL;
3182 c_parser_error (parser, "expected identifier");
3183 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3184 process_init_element (init, false);
3185 return;
3188 else
3190 tree first, second;
3191 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3192 /* ??? Following the old parser, [ objc-receiver
3193 objc-message-args ] is accepted as an initializer,
3194 being distinguished from a designator by what follows
3195 the first assignment expression inside the square
3196 brackets, but after a first array designator a
3197 subsequent square bracket is for Objective-C taken to
3198 start an expression, using the obsolete form of
3199 designated initializer without '=', rather than
3200 possibly being a second level of designation: in LALR
3201 terms, the '[' is shifted rather than reducing
3202 designator to designator-list. */
3203 if (des_prev == 1 && c_dialect_objc ())
3205 des_seen = des_prev;
3206 break;
3208 if (des_prev == 0 && c_dialect_objc ())
3210 /* This might be an array designator or an
3211 Objective-C message expression. If the former,
3212 continue parsing here; if the latter, parse the
3213 remainder of the initializer given the starting
3214 primary-expression. ??? It might make sense to
3215 distinguish when des_prev == 1 as well; see
3216 previous comment. */
3217 tree rec, args;
3218 struct c_expr mexpr;
3219 c_parser_consume_token (parser);
3220 if (c_parser_peek_token (parser)->type == CPP_NAME
3221 && ((c_parser_peek_token (parser)->id_kind
3222 == C_ID_TYPENAME)
3223 || (c_parser_peek_token (parser)->id_kind
3224 == C_ID_CLASSNAME)))
3226 /* Type name receiver. */
3227 tree id = c_parser_peek_token (parser)->value;
3228 c_parser_consume_token (parser);
3229 rec = objc_get_class_reference (id);
3230 goto parse_message_args;
3232 first = c_parser_expr_no_commas (parser, NULL).value;
3233 mark_exp_read (first);
3234 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3235 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3236 goto array_desig_after_first;
3237 /* Expression receiver. So far only one part
3238 without commas has been parsed; there might be
3239 more of the expression. */
3240 rec = first;
3241 while (c_parser_next_token_is (parser, CPP_COMMA))
3243 struct c_expr next;
3244 location_t comma_loc, exp_loc;
3245 comma_loc = c_parser_peek_token (parser)->location;
3246 c_parser_consume_token (parser);
3247 exp_loc = c_parser_peek_token (parser)->location;
3248 next = c_parser_expr_no_commas (parser, NULL);
3249 next = default_function_array_read_conversion (exp_loc,
3250 next);
3251 rec = build_compound_expr (comma_loc, rec, next.value);
3253 parse_message_args:
3254 /* Now parse the objc-message-args. */
3255 args = c_parser_objc_message_args (parser);
3256 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3257 "expected %<]%>");
3258 mexpr.value
3259 = objc_build_message_expr (build_tree_list (rec, args));
3260 mexpr.original_code = ERROR_MARK;
3261 mexpr.original_type = NULL;
3262 /* Now parse and process the remainder of the
3263 initializer, starting with this message
3264 expression as a primary-expression. */
3265 c_parser_initval (parser, &mexpr);
3266 return;
3268 c_parser_consume_token (parser);
3269 first = c_parser_expr_no_commas (parser, NULL).value;
3270 mark_exp_read (first);
3271 array_desig_after_first:
3272 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3274 ellipsis_loc = c_parser_peek_token (parser)->location;
3275 c_parser_consume_token (parser);
3276 second = c_parser_expr_no_commas (parser, NULL).value;
3277 mark_exp_read (second);
3279 else
3280 second = NULL_TREE;
3281 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3283 c_parser_consume_token (parser);
3284 set_init_index (first, second);
3285 if (second)
3286 pedwarn (ellipsis_loc, OPT_pedantic,
3287 "ISO C forbids specifying range of elements to initialize");
3289 else
3290 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3291 "expected %<]%>");
3294 if (des_seen >= 1)
3296 if (c_parser_next_token_is (parser, CPP_EQ))
3298 if (!flag_isoc99)
3299 pedwarn (des_loc, OPT_pedantic,
3300 "ISO C90 forbids specifying subobject to initialize");
3301 c_parser_consume_token (parser);
3303 else
3305 if (des_seen == 1)
3306 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3307 "obsolete use of designated initializer without %<=%>");
3308 else
3310 struct c_expr init;
3311 init.value = error_mark_node;
3312 init.original_code = ERROR_MARK;
3313 init.original_type = NULL;
3314 c_parser_error (parser, "expected %<=%>");
3315 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3316 process_init_element (init, false);
3317 return;
3322 c_parser_initval (parser, NULL);
3325 /* Parse a nested initializer; as c_parser_initializer but parses
3326 initializers within braced lists, after any designators have been
3327 applied. If AFTER is not NULL then it is an Objective-C message
3328 expression which is the primary-expression starting the
3329 initializer. */
3331 static void
3332 c_parser_initval (c_parser *parser, struct c_expr *after)
3334 struct c_expr init;
3335 gcc_assert (!after || c_dialect_objc ());
3336 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3337 init = c_parser_braced_init (parser, NULL_TREE, true);
3338 else
3340 location_t loc = c_parser_peek_token (parser)->location;
3341 init = c_parser_expr_no_commas (parser, after);
3342 if (init.value != NULL_TREE
3343 && TREE_CODE (init.value) != STRING_CST
3344 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3345 init = default_function_array_read_conversion (loc, init);
3347 process_init_element (init, false);
3350 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3351 C99 6.8.2).
3353 compound-statement:
3354 { block-item-list[opt] }
3355 { label-declarations block-item-list }
3357 block-item-list:
3358 block-item
3359 block-item-list block-item
3361 block-item:
3362 nested-declaration
3363 statement
3365 nested-declaration:
3366 declaration
3368 GNU extensions:
3370 compound-statement:
3371 { label-declarations block-item-list }
3373 nested-declaration:
3374 __extension__ nested-declaration
3375 nested-function-definition
3377 label-declarations:
3378 label-declaration
3379 label-declarations label-declaration
3381 label-declaration:
3382 __label__ identifier-list ;
3384 Allowing the mixing of declarations and code is new in C99. The
3385 GNU syntax also permits (not shown above) labels at the end of
3386 compound statements, which yield an error. We don't allow labels
3387 on declarations; this might seem like a natural extension, but
3388 there would be a conflict between attributes on the label and
3389 prefix attributes on the declaration. ??? The syntax follows the
3390 old parser in requiring something after label declarations.
3391 Although they are erroneous if the labels declared aren't defined,
3392 is it useful for the syntax to be this way?
3394 OpenMP:
3396 block-item:
3397 openmp-directive
3399 openmp-directive:
3400 barrier-directive
3401 flush-directive */
3403 static tree
3404 c_parser_compound_statement (c_parser *parser)
3406 tree stmt;
3407 location_t brace_loc;
3408 brace_loc = c_parser_peek_token (parser)->location;
3409 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3411 /* Ensure a scope is entered and left anyway to avoid confusion
3412 if we have just prepared to enter a function body. */
3413 stmt = c_begin_compound_stmt (true);
3414 c_end_compound_stmt (brace_loc, stmt, true);
3415 return error_mark_node;
3417 stmt = c_begin_compound_stmt (true);
3418 c_parser_compound_statement_nostart (parser);
3419 return c_end_compound_stmt (brace_loc, stmt, true);
3422 /* Parse a compound statement except for the opening brace. This is
3423 used for parsing both compound statements and statement expressions
3424 (which follow different paths to handling the opening). */
3426 static void
3427 c_parser_compound_statement_nostart (c_parser *parser)
3429 bool last_stmt = false;
3430 bool last_label = false;
3431 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3432 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3433 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3435 c_parser_consume_token (parser);
3436 return;
3438 mark_valid_location_for_stdc_pragma (true);
3439 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3441 /* Read zero or more forward-declarations for labels that nested
3442 functions can jump to. */
3443 mark_valid_location_for_stdc_pragma (false);
3444 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3446 label_loc = c_parser_peek_token (parser)->location;
3447 c_parser_consume_token (parser);
3448 /* Any identifiers, including those declared as type names,
3449 are OK here. */
3450 while (true)
3452 tree label;
3453 if (c_parser_next_token_is_not (parser, CPP_NAME))
3455 c_parser_error (parser, "expected identifier");
3456 break;
3458 label
3459 = declare_label (c_parser_peek_token (parser)->value);
3460 C_DECLARED_LABEL_FLAG (label) = 1;
3461 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3462 c_parser_consume_token (parser);
3463 if (c_parser_next_token_is (parser, CPP_COMMA))
3464 c_parser_consume_token (parser);
3465 else
3466 break;
3468 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3470 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
3472 /* We must now have at least one statement, label or declaration. */
3473 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3475 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3476 c_parser_error (parser, "expected declaration or statement");
3477 c_parser_consume_token (parser);
3478 return;
3480 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3482 location_t loc = c_parser_peek_token (parser)->location;
3483 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3484 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3485 || (c_parser_next_token_is (parser, CPP_NAME)
3486 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3488 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3489 label_loc = c_parser_peek_2nd_token (parser)->location;
3490 else
3491 label_loc = c_parser_peek_token (parser)->location;
3492 last_label = true;
3493 last_stmt = false;
3494 mark_valid_location_for_stdc_pragma (false);
3495 c_parser_label (parser);
3497 else if (!last_label
3498 && c_parser_next_token_starts_declspecs (parser))
3500 last_label = false;
3501 mark_valid_location_for_stdc_pragma (false);
3502 c_parser_declaration_or_fndef (parser, true, true, true, true);
3503 if (last_stmt)
3504 pedwarn_c90 (loc,
3505 (pedantic && !flag_isoc99)
3506 ? OPT_pedantic
3507 : OPT_Wdeclaration_after_statement,
3508 "ISO C90 forbids mixed declarations and code");
3509 last_stmt = false;
3511 else if (!last_label
3512 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3514 /* __extension__ can start a declaration, but is also an
3515 unary operator that can start an expression. Consume all
3516 but the last of a possible series of __extension__ to
3517 determine which. */
3518 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3519 && (c_parser_peek_2nd_token (parser)->keyword
3520 == RID_EXTENSION))
3521 c_parser_consume_token (parser);
3522 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3524 int ext;
3525 ext = disable_extension_diagnostics ();
3526 c_parser_consume_token (parser);
3527 last_label = false;
3528 mark_valid_location_for_stdc_pragma (false);
3529 c_parser_declaration_or_fndef (parser, true, true, true, true);
3530 /* Following the old parser, __extension__ does not
3531 disable this diagnostic. */
3532 restore_extension_diagnostics (ext);
3533 if (last_stmt)
3534 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
3535 ? OPT_pedantic
3536 : OPT_Wdeclaration_after_statement,
3537 "ISO C90 forbids mixed declarations and code");
3538 last_stmt = false;
3540 else
3541 goto statement;
3543 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3545 /* External pragmas, and some omp pragmas, are not associated
3546 with regular c code, and so are not to be considered statements
3547 syntactically. This ensures that the user doesn't put them
3548 places that would turn into syntax errors if the directive
3549 were ignored. */
3550 if (c_parser_pragma (parser, pragma_compound))
3551 last_label = false, last_stmt = true;
3553 else if (c_parser_next_token_is (parser, CPP_EOF))
3555 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3556 c_parser_error (parser, "expected declaration or statement");
3557 return;
3559 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3561 if (parser->in_if_block)
3563 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3564 error_at (loc, """expected %<}%> before %<else%>");
3565 return;
3567 else
3569 error_at (loc, "%<else%> without a previous %<if%>");
3570 c_parser_consume_token (parser);
3571 continue;
3574 else
3576 statement:
3577 last_label = false;
3578 last_stmt = true;
3579 mark_valid_location_for_stdc_pragma (false);
3580 c_parser_statement_after_labels (parser);
3583 parser->error = false;
3585 if (last_label)
3586 error_at (label_loc, "label at end of compound statement");
3587 c_parser_consume_token (parser);
3588 /* Restore the value we started with. */
3589 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
3592 /* Parse a label (C90 6.6.1, C99 6.8.1).
3594 label:
3595 identifier : attributes[opt]
3596 case constant-expression :
3597 default :
3599 GNU extensions:
3601 label:
3602 case constant-expression ... constant-expression :
3604 The use of attributes on labels is a GNU extension. The syntax in
3605 GNU C accepts any expressions without commas, non-constant
3606 expressions being rejected later. */
3608 static void
3609 c_parser_label (c_parser *parser)
3611 location_t loc1 = c_parser_peek_token (parser)->location;
3612 tree label = NULL_TREE;
3613 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3615 tree exp1, exp2;
3616 c_parser_consume_token (parser);
3617 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3618 if (c_parser_next_token_is (parser, CPP_COLON))
3620 c_parser_consume_token (parser);
3621 label = do_case (loc1, exp1, NULL_TREE);
3623 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3625 c_parser_consume_token (parser);
3626 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3627 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3628 label = do_case (loc1, exp1, exp2);
3630 else
3631 c_parser_error (parser, "expected %<:%> or %<...%>");
3633 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3635 c_parser_consume_token (parser);
3636 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3637 label = do_case (loc1, NULL_TREE, NULL_TREE);
3639 else
3641 tree name = c_parser_peek_token (parser)->value;
3642 tree tlab;
3643 tree attrs;
3644 location_t loc2 = c_parser_peek_token (parser)->location;
3645 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3646 c_parser_consume_token (parser);
3647 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3648 c_parser_consume_token (parser);
3649 attrs = c_parser_attributes (parser);
3650 tlab = define_label (loc2, name);
3651 if (tlab)
3653 decl_attributes (&tlab, attrs, 0);
3654 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
3657 if (label)
3659 if (c_parser_next_token_starts_declspecs (parser)
3660 && !(c_parser_next_token_is (parser, CPP_NAME)
3661 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3663 error_at (c_parser_peek_token (parser)->location,
3664 "a label can only be part of a statement and "
3665 "a declaration is not a statement");
3666 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
3667 /*nested*/ true, /*empty_ok*/ false,
3668 /*start_attr_ok*/ true);
3673 /* Parse a statement (C90 6.6, C99 6.8).
3675 statement:
3676 labeled-statement
3677 compound-statement
3678 expression-statement
3679 selection-statement
3680 iteration-statement
3681 jump-statement
3683 labeled-statement:
3684 label statement
3686 expression-statement:
3687 expression[opt] ;
3689 selection-statement:
3690 if-statement
3691 switch-statement
3693 iteration-statement:
3694 while-statement
3695 do-statement
3696 for-statement
3698 jump-statement:
3699 goto identifier ;
3700 continue ;
3701 break ;
3702 return expression[opt] ;
3704 GNU extensions:
3706 statement:
3707 asm-statement
3709 jump-statement:
3710 goto * expression ;
3712 Objective-C:
3714 statement:
3715 objc-throw-statement
3716 objc-try-catch-statement
3717 objc-synchronized-statement
3719 objc-throw-statement:
3720 @throw expression ;
3721 @throw ;
3723 OpenMP:
3725 statement:
3726 openmp-construct
3728 openmp-construct:
3729 parallel-construct
3730 for-construct
3731 sections-construct
3732 single-construct
3733 parallel-for-construct
3734 parallel-sections-construct
3735 master-construct
3736 critical-construct
3737 atomic-construct
3738 ordered-construct
3740 parallel-construct:
3741 parallel-directive structured-block
3743 for-construct:
3744 for-directive iteration-statement
3746 sections-construct:
3747 sections-directive section-scope
3749 single-construct:
3750 single-directive structured-block
3752 parallel-for-construct:
3753 parallel-for-directive iteration-statement
3755 parallel-sections-construct:
3756 parallel-sections-directive section-scope
3758 master-construct:
3759 master-directive structured-block
3761 critical-construct:
3762 critical-directive structured-block
3764 atomic-construct:
3765 atomic-directive expression-statement
3767 ordered-construct:
3768 ordered-directive structured-block */
3770 static void
3771 c_parser_statement (c_parser *parser)
3773 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3774 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3775 || (c_parser_next_token_is (parser, CPP_NAME)
3776 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3777 c_parser_label (parser);
3778 c_parser_statement_after_labels (parser);
3781 /* Parse a statement, other than a labeled statement. */
3783 static void
3784 c_parser_statement_after_labels (c_parser *parser)
3786 location_t loc = c_parser_peek_token (parser)->location;
3787 tree stmt = NULL_TREE;
3788 bool in_if_block = parser->in_if_block;
3789 parser->in_if_block = false;
3790 switch (c_parser_peek_token (parser)->type)
3792 case CPP_OPEN_BRACE:
3793 add_stmt (c_parser_compound_statement (parser));
3794 break;
3795 case CPP_KEYWORD:
3796 switch (c_parser_peek_token (parser)->keyword)
3798 case RID_IF:
3799 c_parser_if_statement (parser);
3800 break;
3801 case RID_SWITCH:
3802 c_parser_switch_statement (parser);
3803 break;
3804 case RID_WHILE:
3805 c_parser_while_statement (parser);
3806 break;
3807 case RID_DO:
3808 c_parser_do_statement (parser);
3809 break;
3810 case RID_FOR:
3811 c_parser_for_statement (parser);
3812 break;
3813 case RID_GOTO:
3814 c_parser_consume_token (parser);
3815 if (c_parser_next_token_is (parser, CPP_NAME))
3817 stmt = c_finish_goto_label (loc,
3818 c_parser_peek_token (parser)->value);
3819 c_parser_consume_token (parser);
3821 else if (c_parser_next_token_is (parser, CPP_MULT))
3823 c_parser_consume_token (parser);
3824 stmt = c_finish_goto_ptr (loc,
3825 c_parser_expression (parser).value);
3827 else
3828 c_parser_error (parser, "expected identifier or %<*%>");
3829 goto expect_semicolon;
3830 case RID_CONTINUE:
3831 c_parser_consume_token (parser);
3832 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
3833 goto expect_semicolon;
3834 case RID_BREAK:
3835 c_parser_consume_token (parser);
3836 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
3837 goto expect_semicolon;
3838 case RID_RETURN:
3839 c_parser_consume_token (parser);
3840 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3842 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
3843 c_parser_consume_token (parser);
3845 else
3847 struct c_expr expr = c_parser_expression_conv (parser);
3848 mark_exp_read (expr.value);
3849 stmt = c_finish_return (loc, expr.value, expr.original_type);
3850 goto expect_semicolon;
3852 break;
3853 case RID_ASM:
3854 stmt = c_parser_asm_statement (parser);
3855 break;
3856 case RID_THROW:
3857 gcc_assert (c_dialect_objc ());
3858 c_parser_consume_token (parser);
3859 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3861 stmt = objc_build_throw_stmt (loc, NULL_TREE);
3862 c_parser_consume_token (parser);
3864 else
3866 tree expr = c_parser_expression (parser).value;
3867 expr = c_fully_fold (expr, false, NULL);
3868 stmt = objc_build_throw_stmt (loc, expr);
3869 goto expect_semicolon;
3871 break;
3872 case RID_TRY:
3873 gcc_assert (c_dialect_objc ());
3874 c_parser_objc_try_catch_statement (parser);
3875 break;
3876 case RID_AT_SYNCHRONIZED:
3877 gcc_assert (c_dialect_objc ());
3878 c_parser_objc_synchronized_statement (parser);
3879 break;
3880 default:
3881 goto expr_stmt;
3883 break;
3884 case CPP_SEMICOLON:
3885 c_parser_consume_token (parser);
3886 break;
3887 case CPP_CLOSE_PAREN:
3888 case CPP_CLOSE_SQUARE:
3889 /* Avoid infinite loop in error recovery:
3890 c_parser_skip_until_found stops at a closing nesting
3891 delimiter without consuming it, but here we need to consume
3892 it to proceed further. */
3893 c_parser_error (parser, "expected statement");
3894 c_parser_consume_token (parser);
3895 break;
3896 case CPP_PRAGMA:
3897 c_parser_pragma (parser, pragma_stmt);
3898 break;
3899 default:
3900 expr_stmt:
3901 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
3902 expect_semicolon:
3903 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3904 break;
3906 /* Two cases cannot and do not have line numbers associated: If stmt
3907 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3908 cannot hold line numbers. But that's OK because the statement
3909 will either be changed to a MODIFY_EXPR during gimplification of
3910 the statement expr, or discarded. If stmt was compound, but
3911 without new variables, we will have skipped the creation of a
3912 BIND and will have a bare STATEMENT_LIST. But that's OK because
3913 (recursively) all of the component statements should already have
3914 line numbers assigned. ??? Can we discard no-op statements
3915 earlier? */
3916 if (CAN_HAVE_LOCATION_P (stmt)
3917 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
3918 SET_EXPR_LOCATION (stmt, loc);
3920 parser->in_if_block = in_if_block;
3923 /* Parse the condition from an if, do, while or for statements. */
3925 static tree
3926 c_parser_condition (c_parser *parser)
3928 location_t loc = c_parser_peek_token (parser)->location;
3929 tree cond;
3930 cond = c_parser_expression_conv (parser).value;
3931 cond = c_objc_common_truthvalue_conversion (loc, cond);
3932 cond = c_fully_fold (cond, false, NULL);
3933 if (warn_sequence_point)
3934 verify_sequence_points (cond);
3935 return cond;
3938 /* Parse a parenthesized condition from an if, do or while statement.
3940 condition:
3941 ( expression )
3943 static tree
3944 c_parser_paren_condition (c_parser *parser)
3946 tree cond;
3947 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3948 return error_mark_node;
3949 cond = c_parser_condition (parser);
3950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3951 return cond;
3954 /* Parse a statement which is a block in C99. */
3956 static tree
3957 c_parser_c99_block_statement (c_parser *parser)
3959 tree block = c_begin_compound_stmt (flag_isoc99);
3960 location_t loc = c_parser_peek_token (parser)->location;
3961 c_parser_statement (parser);
3962 return c_end_compound_stmt (loc, block, flag_isoc99);
3965 /* Parse the body of an if statement. This is just parsing a
3966 statement but (a) it is a block in C99, (b) we track whether the
3967 body is an if statement for the sake of -Wparentheses warnings, (c)
3968 we handle an empty body specially for the sake of -Wempty-body
3969 warnings, and (d) we call parser_compound_statement directly
3970 because c_parser_statement_after_labels resets
3971 parser->in_if_block. */
3973 static tree
3974 c_parser_if_body (c_parser *parser, bool *if_p)
3976 tree block = c_begin_compound_stmt (flag_isoc99);
3977 location_t body_loc = c_parser_peek_token (parser)->location;
3978 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3979 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3980 || (c_parser_next_token_is (parser, CPP_NAME)
3981 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3982 c_parser_label (parser);
3983 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3984 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3986 location_t loc = c_parser_peek_token (parser)->location;
3987 add_stmt (build_empty_stmt (loc));
3988 c_parser_consume_token (parser);
3989 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
3990 warning_at (loc, OPT_Wempty_body,
3991 "suggest braces around empty body in an %<if%> statement");
3993 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3994 add_stmt (c_parser_compound_statement (parser));
3995 else
3996 c_parser_statement_after_labels (parser);
3997 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4000 /* Parse the else body of an if statement. This is just parsing a
4001 statement but (a) it is a block in C99, (b) we handle an empty body
4002 specially for the sake of -Wempty-body warnings. */
4004 static tree
4005 c_parser_else_body (c_parser *parser)
4007 location_t else_loc = c_parser_peek_token (parser)->location;
4008 tree block = c_begin_compound_stmt (flag_isoc99);
4009 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4010 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4011 || (c_parser_next_token_is (parser, CPP_NAME)
4012 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4013 c_parser_label (parser);
4014 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4016 location_t loc = c_parser_peek_token (parser)->location;
4017 warning_at (loc,
4018 OPT_Wempty_body,
4019 "suggest braces around empty body in an %<else%> statement");
4020 add_stmt (build_empty_stmt (loc));
4021 c_parser_consume_token (parser);
4023 else
4024 c_parser_statement_after_labels (parser);
4025 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4028 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4030 if-statement:
4031 if ( expression ) statement
4032 if ( expression ) statement else statement
4035 static void
4036 c_parser_if_statement (c_parser *parser)
4038 tree block;
4039 location_t loc;
4040 tree cond;
4041 bool first_if = false;
4042 tree first_body, second_body;
4043 bool in_if_block;
4045 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4046 c_parser_consume_token (parser);
4047 block = c_begin_compound_stmt (flag_isoc99);
4048 loc = c_parser_peek_token (parser)->location;
4049 cond = c_parser_paren_condition (parser);
4050 in_if_block = parser->in_if_block;
4051 parser->in_if_block = true;
4052 first_body = c_parser_if_body (parser, &first_if);
4053 parser->in_if_block = in_if_block;
4054 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4056 c_parser_consume_token (parser);
4057 second_body = c_parser_else_body (parser);
4059 else
4060 second_body = NULL_TREE;
4061 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4062 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4065 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4067 switch-statement:
4068 switch (expression) statement
4071 static void
4072 c_parser_switch_statement (c_parser *parser)
4074 tree block, expr, body, save_break;
4075 location_t switch_loc = c_parser_peek_token (parser)->location;
4076 location_t switch_cond_loc;
4077 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4078 c_parser_consume_token (parser);
4079 block = c_begin_compound_stmt (flag_isoc99);
4080 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4082 switch_cond_loc = c_parser_peek_token (parser)->location;
4083 expr = c_parser_expression (parser).value;
4084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4086 else
4088 switch_cond_loc = UNKNOWN_LOCATION;
4089 expr = error_mark_node;
4091 c_start_case (switch_loc, switch_cond_loc, expr);
4092 save_break = c_break_label;
4093 c_break_label = NULL_TREE;
4094 body = c_parser_c99_block_statement (parser);
4095 c_finish_case (body);
4096 if (c_break_label)
4098 location_t here = c_parser_peek_token (parser)->location;
4099 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4100 SET_EXPR_LOCATION (t, here);
4101 add_stmt (t);
4103 c_break_label = save_break;
4104 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4107 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4109 while-statement:
4110 while (expression) statement
4113 static void
4114 c_parser_while_statement (c_parser *parser)
4116 tree block, cond, body, save_break, save_cont;
4117 location_t loc;
4118 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4119 c_parser_consume_token (parser);
4120 block = c_begin_compound_stmt (flag_isoc99);
4121 loc = c_parser_peek_token (parser)->location;
4122 cond = c_parser_paren_condition (parser);
4123 save_break = c_break_label;
4124 c_break_label = NULL_TREE;
4125 save_cont = c_cont_label;
4126 c_cont_label = NULL_TREE;
4127 body = c_parser_c99_block_statement (parser);
4128 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4129 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4130 c_break_label = save_break;
4131 c_cont_label = save_cont;
4134 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4136 do-statement:
4137 do statement while ( expression ) ;
4140 static void
4141 c_parser_do_statement (c_parser *parser)
4143 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4144 location_t loc;
4145 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4146 c_parser_consume_token (parser);
4147 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4148 warning_at (c_parser_peek_token (parser)->location,
4149 OPT_Wempty_body,
4150 "suggest braces around empty body in %<do%> statement");
4151 block = c_begin_compound_stmt (flag_isoc99);
4152 loc = c_parser_peek_token (parser)->location;
4153 save_break = c_break_label;
4154 c_break_label = NULL_TREE;
4155 save_cont = c_cont_label;
4156 c_cont_label = NULL_TREE;
4157 body = c_parser_c99_block_statement (parser);
4158 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4159 new_break = c_break_label;
4160 c_break_label = save_break;
4161 new_cont = c_cont_label;
4162 c_cont_label = save_cont;
4163 cond = c_parser_paren_condition (parser);
4164 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4165 c_parser_skip_to_end_of_block_or_statement (parser);
4166 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4167 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4170 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4172 for-statement:
4173 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4174 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4176 The form with a declaration is new in C99.
4178 ??? In accordance with the old parser, the declaration may be a
4179 nested function, which is then rejected in check_for_loop_decls,
4180 but does it make any sense for this to be included in the grammar?
4181 Note in particular that the nested function does not include a
4182 trailing ';', whereas the "declaration" production includes one.
4183 Also, can we reject bad declarations earlier and cheaper than
4184 check_for_loop_decls? */
4186 static void
4187 c_parser_for_statement (c_parser *parser)
4189 tree block, cond, incr, save_break, save_cont, body;
4190 location_t loc = c_parser_peek_token (parser)->location;
4191 location_t for_loc = c_parser_peek_token (parser)->location;
4192 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4193 c_parser_consume_token (parser);
4194 block = c_begin_compound_stmt (flag_isoc99);
4195 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4197 /* Parse the initialization declaration or expression. */
4198 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4200 c_parser_consume_token (parser);
4201 c_finish_expr_stmt (loc, NULL_TREE);
4203 else if (c_parser_next_token_starts_declspecs (parser))
4205 c_parser_declaration_or_fndef (parser, true, true, true, true);
4206 check_for_loop_decls (for_loc);
4208 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4210 /* __extension__ can start a declaration, but is also an
4211 unary operator that can start an expression. Consume all
4212 but the last of a possible series of __extension__ to
4213 determine which. */
4214 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4215 && (c_parser_peek_2nd_token (parser)->keyword
4216 == RID_EXTENSION))
4217 c_parser_consume_token (parser);
4218 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4220 int ext;
4221 ext = disable_extension_diagnostics ();
4222 c_parser_consume_token (parser);
4223 c_parser_declaration_or_fndef (parser, true, true, true, true);
4224 restore_extension_diagnostics (ext);
4225 check_for_loop_decls (for_loc);
4227 else
4228 goto init_expr;
4230 else
4232 init_expr:
4233 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
4234 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4236 /* Parse the loop condition. */
4237 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4239 c_parser_consume_token (parser);
4240 cond = NULL_TREE;
4242 else
4244 cond = c_parser_condition (parser);
4245 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4247 /* Parse the increment expression. */
4248 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4249 incr = c_process_expr_stmt (loc, NULL_TREE);
4250 else
4251 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4254 else
4256 cond = error_mark_node;
4257 incr = error_mark_node;
4259 save_break = c_break_label;
4260 c_break_label = NULL_TREE;
4261 save_cont = c_cont_label;
4262 c_cont_label = NULL_TREE;
4263 body = c_parser_c99_block_statement (parser);
4264 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4265 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4266 c_break_label = save_break;
4267 c_cont_label = save_cont;
4270 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4271 statement with inputs, outputs, clobbers, and volatile tag
4272 allowed.
4274 asm-statement:
4275 asm type-qualifier[opt] ( asm-argument ) ;
4276 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4278 asm-argument:
4279 asm-string-literal
4280 asm-string-literal : asm-operands[opt]
4281 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4282 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4284 asm-goto-argument:
4285 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4286 : asm-goto-operands
4288 Qualifiers other than volatile are accepted in the syntax but
4289 warned for. */
4291 static tree
4292 c_parser_asm_statement (c_parser *parser)
4294 tree quals, str, outputs, inputs, clobbers, labels, ret;
4295 bool simple, is_goto;
4296 location_t asm_loc = c_parser_peek_token (parser)->location;
4297 int section, nsections;
4299 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4300 c_parser_consume_token (parser);
4301 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4303 quals = c_parser_peek_token (parser)->value;
4304 c_parser_consume_token (parser);
4306 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4307 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4309 warning_at (c_parser_peek_token (parser)->location,
4311 "%E qualifier ignored on asm",
4312 c_parser_peek_token (parser)->value);
4313 quals = NULL_TREE;
4314 c_parser_consume_token (parser);
4316 else
4317 quals = NULL_TREE;
4319 is_goto = false;
4320 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4322 c_parser_consume_token (parser);
4323 is_goto = true;
4326 /* ??? Follow the C++ parser rather than using the
4327 lex_untranslated_string kludge. */
4328 parser->lex_untranslated_string = true;
4329 ret = NULL;
4331 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4332 goto error;
4334 str = c_parser_asm_string_literal (parser);
4335 if (str == NULL_TREE)
4336 goto error_close_paren;
4338 simple = true;
4339 outputs = NULL_TREE;
4340 inputs = NULL_TREE;
4341 clobbers = NULL_TREE;
4342 labels = NULL_TREE;
4344 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4345 goto done_asm;
4347 /* Parse each colon-delimited section of operands. */
4348 nsections = 3 + is_goto;
4349 for (section = 0; section < nsections; ++section)
4351 if (!c_parser_require (parser, CPP_COLON,
4352 is_goto
4353 ? "expected %<:%>"
4354 : "expected %<:%> or %<)%>"))
4355 goto error_close_paren;
4357 /* Once past any colon, we're no longer a simple asm. */
4358 simple = false;
4360 if ((!c_parser_next_token_is (parser, CPP_COLON)
4361 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4362 || section == 3)
4363 switch (section)
4365 case 0:
4366 /* For asm goto, we don't allow output operands, but reserve
4367 the slot for a future extension that does allow them. */
4368 if (!is_goto)
4369 outputs = c_parser_asm_operands (parser, false);
4370 break;
4371 case 1:
4372 inputs = c_parser_asm_operands (parser, true);
4373 break;
4374 case 2:
4375 clobbers = c_parser_asm_clobbers (parser);
4376 break;
4377 case 3:
4378 labels = c_parser_asm_goto_operands (parser);
4379 break;
4380 default:
4381 gcc_unreachable ();
4384 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4385 goto done_asm;
4388 done_asm:
4389 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4391 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4392 goto error;
4395 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4396 c_parser_skip_to_end_of_block_or_statement (parser);
4398 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
4399 clobbers, labels, simple));
4401 error:
4402 parser->lex_untranslated_string = false;
4403 return ret;
4405 error_close_paren:
4406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4407 goto error;
4410 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4411 not outputs), apply the default conversion of functions and arrays
4412 to pointers.
4414 asm-operands:
4415 asm-operand
4416 asm-operands , asm-operand
4418 asm-operand:
4419 asm-string-literal ( expression )
4420 [ identifier ] asm-string-literal ( expression )
4423 static tree
4424 c_parser_asm_operands (c_parser *parser, bool convert_p)
4426 tree list = NULL_TREE;
4427 location_t loc;
4428 while (true)
4430 tree name, str;
4431 struct c_expr expr;
4432 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4434 c_parser_consume_token (parser);
4435 if (c_parser_next_token_is (parser, CPP_NAME))
4437 tree id = c_parser_peek_token (parser)->value;
4438 c_parser_consume_token (parser);
4439 name = build_string (IDENTIFIER_LENGTH (id),
4440 IDENTIFIER_POINTER (id));
4442 else
4444 c_parser_error (parser, "expected identifier");
4445 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4446 return NULL_TREE;
4448 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4449 "expected %<]%>");
4451 else
4452 name = NULL_TREE;
4453 str = c_parser_asm_string_literal (parser);
4454 if (str == NULL_TREE)
4455 return NULL_TREE;
4456 parser->lex_untranslated_string = false;
4457 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4459 parser->lex_untranslated_string = true;
4460 return NULL_TREE;
4462 loc = c_parser_peek_token (parser)->location;
4463 expr = c_parser_expression (parser);
4464 mark_exp_read (expr.value);
4465 if (convert_p)
4466 expr = default_function_array_conversion (loc, expr);
4467 expr.value = c_fully_fold (expr.value, false, NULL);
4468 parser->lex_untranslated_string = true;
4469 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4471 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4472 return NULL_TREE;
4474 list = chainon (list, build_tree_list (build_tree_list (name, str),
4475 expr.value));
4476 if (c_parser_next_token_is (parser, CPP_COMMA))
4477 c_parser_consume_token (parser);
4478 else
4479 break;
4481 return list;
4484 /* Parse asm clobbers, a GNU extension.
4486 asm-clobbers:
4487 asm-string-literal
4488 asm-clobbers , asm-string-literal
4491 static tree
4492 c_parser_asm_clobbers (c_parser *parser)
4494 tree list = NULL_TREE;
4495 while (true)
4497 tree str = c_parser_asm_string_literal (parser);
4498 if (str)
4499 list = tree_cons (NULL_TREE, str, list);
4500 else
4501 return NULL_TREE;
4502 if (c_parser_next_token_is (parser, CPP_COMMA))
4503 c_parser_consume_token (parser);
4504 else
4505 break;
4507 return list;
4510 /* Parse asm goto labels, a GNU extension.
4512 asm-goto-operands:
4513 identifier
4514 asm-goto-operands , identifier
4517 static tree
4518 c_parser_asm_goto_operands (c_parser *parser)
4520 tree list = NULL_TREE;
4521 while (true)
4523 tree name, label;
4525 if (c_parser_next_token_is (parser, CPP_NAME))
4527 c_token *tok = c_parser_peek_token (parser);
4528 name = tok->value;
4529 label = lookup_label_for_goto (tok->location, name);
4530 c_parser_consume_token (parser);
4531 TREE_USED (label) = 1;
4533 else
4535 c_parser_error (parser, "expected identifier");
4536 return NULL_TREE;
4539 name = build_string (IDENTIFIER_LENGTH (name),
4540 IDENTIFIER_POINTER (name));
4541 list = tree_cons (name, label, list);
4542 if (c_parser_next_token_is (parser, CPP_COMMA))
4543 c_parser_consume_token (parser);
4544 else
4545 return nreverse (list);
4549 /* Parse an expression other than a compound expression; that is, an
4550 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4551 NULL then it is an Objective-C message expression which is the
4552 primary-expression starting the expression as an initializer.
4554 assignment-expression:
4555 conditional-expression
4556 unary-expression assignment-operator assignment-expression
4558 assignment-operator: one of
4559 = *= /= %= += -= <<= >>= &= ^= |=
4561 In GNU C we accept any conditional expression on the LHS and
4562 diagnose the invalid lvalue rather than producing a syntax
4563 error. */
4565 static struct c_expr
4566 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4568 struct c_expr lhs, rhs, ret;
4569 enum tree_code code;
4570 location_t op_location, exp_location;
4571 gcc_assert (!after || c_dialect_objc ());
4572 lhs = c_parser_conditional_expression (parser, after);
4573 op_location = c_parser_peek_token (parser)->location;
4574 switch (c_parser_peek_token (parser)->type)
4576 case CPP_EQ:
4577 code = NOP_EXPR;
4578 break;
4579 case CPP_MULT_EQ:
4580 code = MULT_EXPR;
4581 break;
4582 case CPP_DIV_EQ:
4583 code = TRUNC_DIV_EXPR;
4584 break;
4585 case CPP_MOD_EQ:
4586 code = TRUNC_MOD_EXPR;
4587 break;
4588 case CPP_PLUS_EQ:
4589 code = PLUS_EXPR;
4590 break;
4591 case CPP_MINUS_EQ:
4592 code = MINUS_EXPR;
4593 break;
4594 case CPP_LSHIFT_EQ:
4595 code = LSHIFT_EXPR;
4596 break;
4597 case CPP_RSHIFT_EQ:
4598 code = RSHIFT_EXPR;
4599 break;
4600 case CPP_AND_EQ:
4601 code = BIT_AND_EXPR;
4602 break;
4603 case CPP_XOR_EQ:
4604 code = BIT_XOR_EXPR;
4605 break;
4606 case CPP_OR_EQ:
4607 code = BIT_IOR_EXPR;
4608 break;
4609 default:
4610 return lhs;
4612 c_parser_consume_token (parser);
4613 exp_location = c_parser_peek_token (parser)->location;
4614 rhs = c_parser_expr_no_commas (parser, NULL);
4615 rhs = default_function_array_read_conversion (exp_location, rhs);
4616 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
4617 code, exp_location, rhs.value,
4618 rhs.original_type);
4619 if (code == NOP_EXPR)
4620 ret.original_code = MODIFY_EXPR;
4621 else
4623 TREE_NO_WARNING (ret.value) = 1;
4624 ret.original_code = ERROR_MARK;
4626 ret.original_type = NULL;
4627 return ret;
4630 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4631 is not NULL then it is an Objective-C message expression which is
4632 the primary-expression starting the expression as an initializer.
4634 conditional-expression:
4635 logical-OR-expression
4636 logical-OR-expression ? expression : conditional-expression
4638 GNU extensions:
4640 conditional-expression:
4641 logical-OR-expression ? : conditional-expression
4644 static struct c_expr
4645 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4647 struct c_expr cond, exp1, exp2, ret;
4648 location_t cond_loc, colon_loc;
4650 gcc_assert (!after || c_dialect_objc ());
4652 cond = c_parser_binary_expression (parser, after);
4654 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4655 return cond;
4656 cond_loc = c_parser_peek_token (parser)->location;
4657 cond = default_function_array_read_conversion (cond_loc, cond);
4658 c_parser_consume_token (parser);
4659 if (c_parser_next_token_is (parser, CPP_COLON))
4661 tree eptype = NULL_TREE;
4662 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
4663 "ISO C forbids omitting the middle term of a ?: expression");
4664 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
4666 eptype = TREE_TYPE (cond.value);
4667 cond.value = TREE_OPERAND (cond.value, 0);
4669 /* Make sure first operand is calculated only once. */
4670 exp1.value = c_save_expr (default_conversion (cond.value));
4671 if (eptype)
4672 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
4673 exp1.original_type = NULL;
4674 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
4675 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
4677 else
4679 cond.value
4680 = c_objc_common_truthvalue_conversion
4681 (cond_loc, default_conversion (cond.value));
4682 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
4683 exp1 = c_parser_expression_conv (parser);
4684 mark_exp_read (exp1.value);
4685 c_inhibit_evaluation_warnings +=
4686 ((cond.value == truthvalue_true_node)
4687 - (cond.value == truthvalue_false_node));
4690 colon_loc = c_parser_peek_token (parser)->location;
4691 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4693 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4694 ret.value = error_mark_node;
4695 ret.original_code = ERROR_MARK;
4696 ret.original_type = NULL;
4697 return ret;
4700 location_t exp2_loc = c_parser_peek_token (parser)->location;
4701 exp2 = c_parser_conditional_expression (parser, NULL);
4702 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
4704 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
4705 ret.value = build_conditional_expr (colon_loc, cond.value,
4706 cond.original_code == C_MAYBE_CONST_EXPR,
4707 exp1.value, exp1.original_type,
4708 exp2.value, exp2.original_type);
4709 ret.original_code = ERROR_MARK;
4710 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
4711 ret.original_type = NULL;
4712 else
4714 tree t1, t2;
4716 /* If both sides are enum type, the default conversion will have
4717 made the type of the result be an integer type. We want to
4718 remember the enum types we started with. */
4719 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
4720 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
4721 ret.original_type = ((t1 != error_mark_node
4722 && t2 != error_mark_node
4723 && (TYPE_MAIN_VARIANT (t1)
4724 == TYPE_MAIN_VARIANT (t2)))
4725 ? t1
4726 : NULL);
4728 return ret;
4731 /* Parse a binary expression; that is, a logical-OR-expression (C90
4732 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4733 an Objective-C message expression which is the primary-expression
4734 starting the expression as an initializer.
4736 multiplicative-expression:
4737 cast-expression
4738 multiplicative-expression * cast-expression
4739 multiplicative-expression / cast-expression
4740 multiplicative-expression % cast-expression
4742 additive-expression:
4743 multiplicative-expression
4744 additive-expression + multiplicative-expression
4745 additive-expression - multiplicative-expression
4747 shift-expression:
4748 additive-expression
4749 shift-expression << additive-expression
4750 shift-expression >> additive-expression
4752 relational-expression:
4753 shift-expression
4754 relational-expression < shift-expression
4755 relational-expression > shift-expression
4756 relational-expression <= shift-expression
4757 relational-expression >= shift-expression
4759 equality-expression:
4760 relational-expression
4761 equality-expression == relational-expression
4762 equality-expression != relational-expression
4764 AND-expression:
4765 equality-expression
4766 AND-expression & equality-expression
4768 exclusive-OR-expression:
4769 AND-expression
4770 exclusive-OR-expression ^ AND-expression
4772 inclusive-OR-expression:
4773 exclusive-OR-expression
4774 inclusive-OR-expression | exclusive-OR-expression
4776 logical-AND-expression:
4777 inclusive-OR-expression
4778 logical-AND-expression && inclusive-OR-expression
4780 logical-OR-expression:
4781 logical-AND-expression
4782 logical-OR-expression || logical-AND-expression
4785 static struct c_expr
4786 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4788 /* A binary expression is parsed using operator-precedence parsing,
4789 with the operands being cast expressions. All the binary
4790 operators are left-associative. Thus a binary expression is of
4791 form:
4793 E0 op1 E1 op2 E2 ...
4795 which we represent on a stack. On the stack, the precedence
4796 levels are strictly increasing. When a new operator is
4797 encountered of higher precedence than that at the top of the
4798 stack, it is pushed; its LHS is the top expression, and its RHS
4799 is everything parsed until it is popped. When a new operator is
4800 encountered with precedence less than or equal to that at the top
4801 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4802 by the result of the operation until the operator at the top of
4803 the stack has lower precedence than the new operator or there is
4804 only one element on the stack; then the top expression is the LHS
4805 of the new operator. In the case of logical AND and OR
4806 expressions, we also need to adjust c_inhibit_evaluation_warnings
4807 as appropriate when the operators are pushed and popped. */
4809 /* The precedence levels, where 0 is a dummy lowest level used for
4810 the bottom of the stack. */
4811 enum prec {
4812 PREC_NONE,
4813 PREC_LOGOR,
4814 PREC_LOGAND,
4815 PREC_BITOR,
4816 PREC_BITXOR,
4817 PREC_BITAND,
4818 PREC_EQ,
4819 PREC_REL,
4820 PREC_SHIFT,
4821 PREC_ADD,
4822 PREC_MULT,
4823 NUM_PRECS
4825 struct {
4826 /* The expression at this stack level. */
4827 struct c_expr expr;
4828 /* The precedence of the operator on its left, PREC_NONE at the
4829 bottom of the stack. */
4830 enum prec prec;
4831 /* The operation on its left. */
4832 enum tree_code op;
4833 /* The source location of this operation. */
4834 location_t loc;
4835 } stack[NUM_PRECS];
4836 int sp;
4837 /* Location of the binary operator. */
4838 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4839 #define POP \
4840 do { \
4841 switch (stack[sp].op) \
4843 case TRUTH_ANDIF_EXPR: \
4844 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4845 == truthvalue_false_node); \
4846 break; \
4847 case TRUTH_ORIF_EXPR: \
4848 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
4849 == truthvalue_true_node); \
4850 break; \
4851 default: \
4852 break; \
4854 stack[sp - 1].expr \
4855 = default_function_array_read_conversion (stack[sp - 1].loc, \
4856 stack[sp - 1].expr); \
4857 stack[sp].expr \
4858 = default_function_array_read_conversion (stack[sp].loc, \
4859 stack[sp].expr); \
4860 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
4861 stack[sp].op, \
4862 stack[sp - 1].expr, \
4863 stack[sp].expr); \
4864 sp--; \
4865 } while (0)
4866 gcc_assert (!after || c_dialect_objc ());
4867 stack[0].loc = c_parser_peek_token (parser)->location;
4868 stack[0].expr = c_parser_cast_expression (parser, after);
4869 stack[0].prec = PREC_NONE;
4870 sp = 0;
4871 while (true)
4873 enum prec oprec;
4874 enum tree_code ocode;
4875 if (parser->error)
4876 goto out;
4877 switch (c_parser_peek_token (parser)->type)
4879 case CPP_MULT:
4880 oprec = PREC_MULT;
4881 ocode = MULT_EXPR;
4882 break;
4883 case CPP_DIV:
4884 oprec = PREC_MULT;
4885 ocode = TRUNC_DIV_EXPR;
4886 break;
4887 case CPP_MOD:
4888 oprec = PREC_MULT;
4889 ocode = TRUNC_MOD_EXPR;
4890 break;
4891 case CPP_PLUS:
4892 oprec = PREC_ADD;
4893 ocode = PLUS_EXPR;
4894 break;
4895 case CPP_MINUS:
4896 oprec = PREC_ADD;
4897 ocode = MINUS_EXPR;
4898 break;
4899 case CPP_LSHIFT:
4900 oprec = PREC_SHIFT;
4901 ocode = LSHIFT_EXPR;
4902 break;
4903 case CPP_RSHIFT:
4904 oprec = PREC_SHIFT;
4905 ocode = RSHIFT_EXPR;
4906 break;
4907 case CPP_LESS:
4908 oprec = PREC_REL;
4909 ocode = LT_EXPR;
4910 break;
4911 case CPP_GREATER:
4912 oprec = PREC_REL;
4913 ocode = GT_EXPR;
4914 break;
4915 case CPP_LESS_EQ:
4916 oprec = PREC_REL;
4917 ocode = LE_EXPR;
4918 break;
4919 case CPP_GREATER_EQ:
4920 oprec = PREC_REL;
4921 ocode = GE_EXPR;
4922 break;
4923 case CPP_EQ_EQ:
4924 oprec = PREC_EQ;
4925 ocode = EQ_EXPR;
4926 break;
4927 case CPP_NOT_EQ:
4928 oprec = PREC_EQ;
4929 ocode = NE_EXPR;
4930 break;
4931 case CPP_AND:
4932 oprec = PREC_BITAND;
4933 ocode = BIT_AND_EXPR;
4934 break;
4935 case CPP_XOR:
4936 oprec = PREC_BITXOR;
4937 ocode = BIT_XOR_EXPR;
4938 break;
4939 case CPP_OR:
4940 oprec = PREC_BITOR;
4941 ocode = BIT_IOR_EXPR;
4942 break;
4943 case CPP_AND_AND:
4944 oprec = PREC_LOGAND;
4945 ocode = TRUTH_ANDIF_EXPR;
4946 break;
4947 case CPP_OR_OR:
4948 oprec = PREC_LOGOR;
4949 ocode = TRUTH_ORIF_EXPR;
4950 break;
4951 default:
4952 /* Not a binary operator, so end of the binary
4953 expression. */
4954 goto out;
4956 binary_loc = c_parser_peek_token (parser)->location;
4957 c_parser_consume_token (parser);
4958 while (oprec <= stack[sp].prec)
4959 POP;
4960 switch (ocode)
4962 case TRUTH_ANDIF_EXPR:
4963 stack[sp].expr
4964 = default_function_array_read_conversion (stack[sp].loc,
4965 stack[sp].expr);
4966 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4967 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4968 c_inhibit_evaluation_warnings += (stack[sp].expr.value
4969 == truthvalue_false_node);
4970 break;
4971 case TRUTH_ORIF_EXPR:
4972 stack[sp].expr
4973 = default_function_array_read_conversion (stack[sp].loc,
4974 stack[sp].expr);
4975 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4976 (stack[sp].loc, default_conversion (stack[sp].expr.value));
4977 c_inhibit_evaluation_warnings += (stack[sp].expr.value
4978 == truthvalue_true_node);
4979 break;
4980 default:
4981 break;
4983 sp++;
4984 stack[sp].loc = binary_loc;
4985 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4986 stack[sp].prec = oprec;
4987 stack[sp].op = ocode;
4988 stack[sp].loc = binary_loc;
4990 out:
4991 while (sp > 0)
4992 POP;
4993 return stack[0].expr;
4994 #undef POP
4997 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4998 NULL then it is an Objective-C message expression which is the
4999 primary-expression starting the expression as an initializer.
5001 cast-expression:
5002 unary-expression
5003 ( type-name ) unary-expression
5006 static struct c_expr
5007 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5009 location_t cast_loc = c_parser_peek_token (parser)->location;
5010 gcc_assert (!after || c_dialect_objc ());
5011 if (after)
5012 return c_parser_postfix_expression_after_primary (parser,
5013 cast_loc, *after);
5014 /* If the expression begins with a parenthesized type name, it may
5015 be either a cast or a compound literal; we need to see whether
5016 the next character is '{' to tell the difference. If not, it is
5017 an unary expression. */
5018 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5019 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5021 struct c_type_name *type_name;
5022 struct c_expr ret;
5023 struct c_expr expr;
5024 c_parser_consume_token (parser);
5025 type_name = c_parser_type_name (parser);
5026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5027 if (type_name == NULL)
5029 ret.value = error_mark_node;
5030 ret.original_code = ERROR_MARK;
5031 ret.original_type = NULL;
5032 return ret;
5035 /* Save casted types in the function's used types hash table. */
5036 used_types_insert (type_name->specs->type);
5038 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5039 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5040 cast_loc);
5042 location_t expr_loc = c_parser_peek_token (parser)->location;
5043 expr = c_parser_cast_expression (parser, NULL);
5044 expr = default_function_array_read_conversion (expr_loc, expr);
5046 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5047 ret.original_code = ERROR_MARK;
5048 ret.original_type = NULL;
5049 return ret;
5051 else
5052 return c_parser_unary_expression (parser);
5055 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5057 unary-expression:
5058 postfix-expression
5059 ++ unary-expression
5060 -- unary-expression
5061 unary-operator cast-expression
5062 sizeof unary-expression
5063 sizeof ( type-name )
5065 unary-operator: one of
5066 & * + - ~ !
5068 GNU extensions:
5070 unary-expression:
5071 __alignof__ unary-expression
5072 __alignof__ ( type-name )
5073 && identifier
5075 unary-operator: one of
5076 __extension__ __real__ __imag__
5078 In addition, the GNU syntax treats ++ and -- as unary operators, so
5079 they may be applied to cast expressions with errors for non-lvalues
5080 given later. */
5082 static struct c_expr
5083 c_parser_unary_expression (c_parser *parser)
5085 int ext;
5086 struct c_expr ret, op;
5087 location_t op_loc = c_parser_peek_token (parser)->location;
5088 location_t exp_loc;
5089 ret.original_code = ERROR_MARK;
5090 ret.original_type = NULL;
5091 switch (c_parser_peek_token (parser)->type)
5093 case CPP_PLUS_PLUS:
5094 c_parser_consume_token (parser);
5095 exp_loc = c_parser_peek_token (parser)->location;
5096 op = c_parser_cast_expression (parser, NULL);
5097 op = default_function_array_read_conversion (exp_loc, op);
5098 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5099 case CPP_MINUS_MINUS:
5100 c_parser_consume_token (parser);
5101 exp_loc = c_parser_peek_token (parser)->location;
5102 op = c_parser_cast_expression (parser, NULL);
5103 op = default_function_array_read_conversion (exp_loc, op);
5104 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5105 case CPP_AND:
5106 c_parser_consume_token (parser);
5107 op = c_parser_cast_expression (parser, NULL);
5108 mark_exp_read (op.value);
5109 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5110 case CPP_MULT:
5111 c_parser_consume_token (parser);
5112 exp_loc = c_parser_peek_token (parser)->location;
5113 op = c_parser_cast_expression (parser, NULL);
5114 op = default_function_array_read_conversion (exp_loc, op);
5115 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5116 return ret;
5117 case CPP_PLUS:
5118 if (!c_dialect_objc () && !in_system_header)
5119 warning_at (op_loc,
5120 OPT_Wtraditional,
5121 "traditional C rejects the unary plus operator");
5122 c_parser_consume_token (parser);
5123 exp_loc = c_parser_peek_token (parser)->location;
5124 op = c_parser_cast_expression (parser, NULL);
5125 op = default_function_array_read_conversion (exp_loc, op);
5126 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5127 case CPP_MINUS:
5128 c_parser_consume_token (parser);
5129 exp_loc = c_parser_peek_token (parser)->location;
5130 op = c_parser_cast_expression (parser, NULL);
5131 op = default_function_array_read_conversion (exp_loc, op);
5132 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5133 case CPP_COMPL:
5134 c_parser_consume_token (parser);
5135 exp_loc = c_parser_peek_token (parser)->location;
5136 op = c_parser_cast_expression (parser, NULL);
5137 op = default_function_array_read_conversion (exp_loc, op);
5138 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5139 case CPP_NOT:
5140 c_parser_consume_token (parser);
5141 exp_loc = c_parser_peek_token (parser)->location;
5142 op = c_parser_cast_expression (parser, NULL);
5143 op = default_function_array_read_conversion (exp_loc, op);
5144 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5145 case CPP_AND_AND:
5146 /* Refer to the address of a label as a pointer. */
5147 c_parser_consume_token (parser);
5148 if (c_parser_next_token_is (parser, CPP_NAME))
5150 ret.value = finish_label_address_expr
5151 (c_parser_peek_token (parser)->value, op_loc);
5152 c_parser_consume_token (parser);
5154 else
5156 c_parser_error (parser, "expected identifier");
5157 ret.value = error_mark_node;
5159 return ret;
5160 case CPP_KEYWORD:
5161 switch (c_parser_peek_token (parser)->keyword)
5163 case RID_SIZEOF:
5164 return c_parser_sizeof_expression (parser);
5165 case RID_ALIGNOF:
5166 return c_parser_alignof_expression (parser);
5167 case RID_EXTENSION:
5168 c_parser_consume_token (parser);
5169 ext = disable_extension_diagnostics ();
5170 ret = c_parser_cast_expression (parser, NULL);
5171 restore_extension_diagnostics (ext);
5172 return ret;
5173 case RID_REALPART:
5174 c_parser_consume_token (parser);
5175 exp_loc = c_parser_peek_token (parser)->location;
5176 op = c_parser_cast_expression (parser, NULL);
5177 op = default_function_array_conversion (exp_loc, op);
5178 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5179 case RID_IMAGPART:
5180 c_parser_consume_token (parser);
5181 exp_loc = c_parser_peek_token (parser)->location;
5182 op = c_parser_cast_expression (parser, NULL);
5183 op = default_function_array_conversion (exp_loc, op);
5184 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5185 default:
5186 return c_parser_postfix_expression (parser);
5188 default:
5189 return c_parser_postfix_expression (parser);
5193 /* Parse a sizeof expression. */
5195 static struct c_expr
5196 c_parser_sizeof_expression (c_parser *parser)
5198 struct c_expr expr;
5199 location_t expr_loc;
5200 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5201 c_parser_consume_token (parser);
5202 c_inhibit_evaluation_warnings++;
5203 in_sizeof++;
5204 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5205 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5207 /* Either sizeof ( type-name ) or sizeof unary-expression
5208 starting with a compound literal. */
5209 struct c_type_name *type_name;
5210 c_parser_consume_token (parser);
5211 expr_loc = c_parser_peek_token (parser)->location;
5212 type_name = c_parser_type_name (parser);
5213 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5214 if (type_name == NULL)
5216 struct c_expr ret;
5217 c_inhibit_evaluation_warnings--;
5218 in_sizeof--;
5219 ret.value = error_mark_node;
5220 ret.original_code = ERROR_MARK;
5221 ret.original_type = NULL;
5222 return ret;
5224 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5226 expr = c_parser_postfix_expression_after_paren_type (parser,
5227 type_name,
5228 expr_loc);
5229 goto sizeof_expr;
5231 /* sizeof ( type-name ). */
5232 c_inhibit_evaluation_warnings--;
5233 in_sizeof--;
5234 return c_expr_sizeof_type (expr_loc, type_name);
5236 else
5238 expr_loc = c_parser_peek_token (parser)->location;
5239 expr = c_parser_unary_expression (parser);
5240 sizeof_expr:
5241 c_inhibit_evaluation_warnings--;
5242 in_sizeof--;
5243 mark_exp_read (expr.value);
5244 if (TREE_CODE (expr.value) == COMPONENT_REF
5245 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5246 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5247 return c_expr_sizeof_expr (expr_loc, expr);
5251 /* Parse an alignof expression. */
5253 static struct c_expr
5254 c_parser_alignof_expression (c_parser *parser)
5256 struct c_expr expr;
5257 location_t loc = c_parser_peek_token (parser)->location;
5258 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5259 c_parser_consume_token (parser);
5260 c_inhibit_evaluation_warnings++;
5261 in_alignof++;
5262 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5263 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5265 /* Either __alignof__ ( type-name ) or __alignof__
5266 unary-expression starting with a compound literal. */
5267 location_t loc;
5268 struct c_type_name *type_name;
5269 struct c_expr ret;
5270 c_parser_consume_token (parser);
5271 loc = c_parser_peek_token (parser)->location;
5272 type_name = c_parser_type_name (parser);
5273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5274 if (type_name == NULL)
5276 struct c_expr ret;
5277 c_inhibit_evaluation_warnings--;
5278 in_alignof--;
5279 ret.value = error_mark_node;
5280 ret.original_code = ERROR_MARK;
5281 ret.original_type = NULL;
5282 return ret;
5284 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5286 expr = c_parser_postfix_expression_after_paren_type (parser,
5287 type_name,
5288 loc);
5289 goto alignof_expr;
5291 /* alignof ( type-name ). */
5292 c_inhibit_evaluation_warnings--;
5293 in_alignof--;
5294 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5295 ret.original_code = ERROR_MARK;
5296 ret.original_type = NULL;
5297 return ret;
5299 else
5301 struct c_expr ret;
5302 expr = c_parser_unary_expression (parser);
5303 alignof_expr:
5304 mark_exp_read (expr.value);
5305 c_inhibit_evaluation_warnings--;
5306 in_alignof--;
5307 ret.value = c_alignof_expr (loc, expr.value);
5308 ret.original_code = ERROR_MARK;
5309 ret.original_type = NULL;
5310 return ret;
5314 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5316 postfix-expression:
5317 primary-expression
5318 postfix-expression [ expression ]
5319 postfix-expression ( argument-expression-list[opt] )
5320 postfix-expression . identifier
5321 postfix-expression -> identifier
5322 postfix-expression ++
5323 postfix-expression --
5324 ( type-name ) { initializer-list }
5325 ( type-name ) { initializer-list , }
5327 argument-expression-list:
5328 argument-expression
5329 argument-expression-list , argument-expression
5331 primary-expression:
5332 identifier
5333 constant
5334 string-literal
5335 ( expression )
5337 GNU extensions:
5339 primary-expression:
5340 __func__
5341 (treated as a keyword in GNU C)
5342 __FUNCTION__
5343 __PRETTY_FUNCTION__
5344 ( compound-statement )
5345 __builtin_va_arg ( assignment-expression , type-name )
5346 __builtin_offsetof ( type-name , offsetof-member-designator )
5347 __builtin_choose_expr ( assignment-expression ,
5348 assignment-expression ,
5349 assignment-expression )
5350 __builtin_types_compatible_p ( type-name , type-name )
5352 offsetof-member-designator:
5353 identifier
5354 offsetof-member-designator . identifier
5355 offsetof-member-designator [ expression ]
5357 Objective-C:
5359 primary-expression:
5360 [ objc-receiver objc-message-args ]
5361 @selector ( objc-selector-arg )
5362 @protocol ( identifier )
5363 @encode ( type-name )
5364 objc-string-literal
5367 static struct c_expr
5368 c_parser_postfix_expression (c_parser *parser)
5370 struct c_expr expr, e1, e2, e3;
5371 struct c_type_name *t1, *t2;
5372 location_t loc = c_parser_peek_token (parser)->location;;
5373 expr.original_code = ERROR_MARK;
5374 expr.original_type = NULL;
5375 switch (c_parser_peek_token (parser)->type)
5377 case CPP_NUMBER:
5378 expr.value = c_parser_peek_token (parser)->value;
5379 loc = c_parser_peek_token (parser)->location;
5380 c_parser_consume_token (parser);
5381 if (TREE_CODE (expr.value) == FIXED_CST
5382 && !targetm.fixed_point_supported_p ())
5384 error_at (loc, "fixed-point types not supported for this target");
5385 expr.value = error_mark_node;
5387 break;
5388 case CPP_CHAR:
5389 case CPP_CHAR16:
5390 case CPP_CHAR32:
5391 case CPP_WCHAR:
5392 expr.value = c_parser_peek_token (parser)->value;
5393 c_parser_consume_token (parser);
5394 break;
5395 case CPP_STRING:
5396 case CPP_STRING16:
5397 case CPP_STRING32:
5398 case CPP_WSTRING:
5399 case CPP_UTF8STRING:
5400 expr.value = c_parser_peek_token (parser)->value;
5401 expr.original_code = STRING_CST;
5402 c_parser_consume_token (parser);
5403 break;
5404 case CPP_OBJC_STRING:
5405 gcc_assert (c_dialect_objc ());
5406 expr.value
5407 = objc_build_string_object (c_parser_peek_token (parser)->value);
5408 c_parser_consume_token (parser);
5409 break;
5410 case CPP_NAME:
5411 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5413 c_parser_error (parser, "expected expression");
5414 expr.value = error_mark_node;
5415 break;
5418 tree id = c_parser_peek_token (parser)->value;
5419 c_parser_consume_token (parser);
5420 expr.value = build_external_ref (loc, id,
5421 (c_parser_peek_token (parser)->type
5422 == CPP_OPEN_PAREN),
5423 &expr.original_type);
5425 break;
5426 case CPP_OPEN_PAREN:
5427 /* A parenthesized expression, statement expression or compound
5428 literal. */
5429 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5431 /* A statement expression. */
5432 tree stmt;
5433 location_t brace_loc;
5434 c_parser_consume_token (parser);
5435 brace_loc = c_parser_peek_token (parser)->location;
5436 c_parser_consume_token (parser);
5437 if (cur_stmt_list == NULL)
5439 error_at (loc, "braced-group within expression allowed "
5440 "only inside a function");
5441 parser->error = true;
5442 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5443 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5444 expr.value = error_mark_node;
5445 break;
5447 stmt = c_begin_stmt_expr ();
5448 c_parser_compound_statement_nostart (parser);
5449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5450 "expected %<)%>");
5451 pedwarn (loc, OPT_pedantic,
5452 "ISO C forbids braced-groups within expressions");
5453 expr.value = c_finish_stmt_expr (brace_loc, stmt);
5455 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5457 /* A compound literal. ??? Can we actually get here rather
5458 than going directly to
5459 c_parser_postfix_expression_after_paren_type from
5460 elsewhere? */
5461 location_t loc;
5462 struct c_type_name *type_name;
5463 c_parser_consume_token (parser);
5464 loc = c_parser_peek_token (parser)->location;
5465 type_name = c_parser_type_name (parser);
5466 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5467 "expected %<)%>");
5468 if (type_name == NULL)
5470 expr.value = error_mark_node;
5472 else
5473 expr = c_parser_postfix_expression_after_paren_type (parser,
5474 type_name,
5475 loc);
5477 else
5479 /* A parenthesized expression. */
5480 c_parser_consume_token (parser);
5481 expr = c_parser_expression (parser);
5482 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5483 TREE_NO_WARNING (expr.value) = 1;
5484 if (expr.original_code != C_MAYBE_CONST_EXPR)
5485 expr.original_code = ERROR_MARK;
5486 /* Don't change EXPR.ORIGINAL_TYPE. */
5487 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5488 "expected %<)%>");
5490 break;
5491 case CPP_KEYWORD:
5492 switch (c_parser_peek_token (parser)->keyword)
5494 case RID_FUNCTION_NAME:
5495 case RID_PRETTY_FUNCTION_NAME:
5496 case RID_C99_FUNCTION_NAME:
5497 expr.value = fname_decl (loc,
5498 c_parser_peek_token (parser)->keyword,
5499 c_parser_peek_token (parser)->value);
5500 c_parser_consume_token (parser);
5501 break;
5502 case RID_VA_ARG:
5503 c_parser_consume_token (parser);
5504 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5506 expr.value = error_mark_node;
5507 break;
5509 e1 = c_parser_expr_no_commas (parser, NULL);
5510 mark_exp_read (e1.value);
5511 e1.value = c_fully_fold (e1.value, false, NULL);
5512 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5514 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5515 expr.value = error_mark_node;
5516 break;
5518 loc = c_parser_peek_token (parser)->location;
5519 t1 = c_parser_type_name (parser);
5520 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5521 "expected %<)%>");
5522 if (t1 == NULL)
5524 expr.value = error_mark_node;
5526 else
5528 tree type_expr = NULL_TREE;
5529 expr.value = c_build_va_arg (loc, e1.value,
5530 groktypename (t1, &type_expr, NULL));
5531 if (type_expr)
5533 expr.value = build2 (C_MAYBE_CONST_EXPR,
5534 TREE_TYPE (expr.value), type_expr,
5535 expr.value);
5536 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
5539 break;
5540 case RID_OFFSETOF:
5541 c_parser_consume_token (parser);
5542 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5544 expr.value = error_mark_node;
5545 break;
5547 t1 = c_parser_type_name (parser);
5548 if (t1 == NULL)
5550 expr.value = error_mark_node;
5551 break;
5553 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5555 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5556 expr.value = error_mark_node;
5557 break;
5560 tree type = groktypename (t1, NULL, NULL);
5561 tree offsetof_ref;
5562 if (type == error_mark_node)
5563 offsetof_ref = error_mark_node;
5564 else
5566 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5567 SET_EXPR_LOCATION (offsetof_ref, loc);
5569 /* Parse the second argument to __builtin_offsetof. We
5570 must have one identifier, and beyond that we want to
5571 accept sub structure and sub array references. */
5572 if (c_parser_next_token_is (parser, CPP_NAME))
5574 offsetof_ref = build_component_ref
5575 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
5576 c_parser_consume_token (parser);
5577 while (c_parser_next_token_is (parser, CPP_DOT)
5578 || c_parser_next_token_is (parser,
5579 CPP_OPEN_SQUARE)
5580 || c_parser_next_token_is (parser,
5581 CPP_DEREF))
5583 if (c_parser_next_token_is (parser, CPP_DEREF))
5585 loc = c_parser_peek_token (parser)->location;
5586 offsetof_ref = build_array_ref (loc,
5587 offsetof_ref,
5588 integer_zero_node);
5589 goto do_dot;
5591 else if (c_parser_next_token_is (parser, CPP_DOT))
5593 do_dot:
5594 c_parser_consume_token (parser);
5595 if (c_parser_next_token_is_not (parser,
5596 CPP_NAME))
5598 c_parser_error (parser, "expected identifier");
5599 break;
5601 offsetof_ref = build_component_ref
5602 (loc, offsetof_ref,
5603 c_parser_peek_token (parser)->value);
5604 c_parser_consume_token (parser);
5606 else
5608 tree idx;
5609 loc = c_parser_peek_token (parser)->location;
5610 c_parser_consume_token (parser);
5611 idx = c_parser_expression (parser).value;
5612 idx = c_fully_fold (idx, false, NULL);
5613 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5614 "expected %<]%>");
5615 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
5619 else
5620 c_parser_error (parser, "expected identifier");
5621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5622 "expected %<)%>");
5623 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5625 break;
5626 case RID_CHOOSE_EXPR:
5627 c_parser_consume_token (parser);
5628 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5630 expr.value = error_mark_node;
5631 break;
5633 loc = c_parser_peek_token (parser)->location;
5634 e1 = c_parser_expr_no_commas (parser, NULL);
5635 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5637 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5638 expr.value = error_mark_node;
5639 break;
5641 e2 = c_parser_expr_no_commas (parser, NULL);
5642 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5645 expr.value = error_mark_node;
5646 break;
5648 e3 = c_parser_expr_no_commas (parser, NULL);
5649 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5650 "expected %<)%>");
5652 tree c;
5654 c = e1.value;
5655 mark_exp_read (e2.value);
5656 mark_exp_read (e3.value);
5657 if (TREE_CODE (c) != INTEGER_CST
5658 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
5659 error_at (loc,
5660 "first argument to %<__builtin_choose_expr%> not"
5661 " a constant");
5662 constant_expression_warning (c);
5663 expr = integer_zerop (c) ? e3 : e2;
5665 break;
5666 case RID_TYPES_COMPATIBLE_P:
5667 c_parser_consume_token (parser);
5668 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5670 expr.value = error_mark_node;
5671 break;
5673 t1 = c_parser_type_name (parser);
5674 if (t1 == NULL)
5676 expr.value = error_mark_node;
5677 break;
5679 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5682 expr.value = error_mark_node;
5683 break;
5685 t2 = c_parser_type_name (parser);
5686 if (t2 == NULL)
5688 expr.value = error_mark_node;
5689 break;
5691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5692 "expected %<)%>");
5694 tree e1, e2;
5696 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
5697 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
5699 expr.value = comptypes (e1, e2)
5700 ? build_int_cst (NULL_TREE, 1)
5701 : build_int_cst (NULL_TREE, 0);
5703 break;
5704 case RID_AT_SELECTOR:
5705 gcc_assert (c_dialect_objc ());
5706 c_parser_consume_token (parser);
5707 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5709 expr.value = error_mark_node;
5710 break;
5713 tree sel = c_parser_objc_selector_arg (parser);
5714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5715 "expected %<)%>");
5716 expr.value = objc_build_selector_expr (loc, sel);
5718 break;
5719 case RID_AT_PROTOCOL:
5720 gcc_assert (c_dialect_objc ());
5721 c_parser_consume_token (parser);
5722 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5724 expr.value = error_mark_node;
5725 break;
5727 if (c_parser_next_token_is_not (parser, CPP_NAME))
5729 c_parser_error (parser, "expected identifier");
5730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5731 expr.value = error_mark_node;
5732 break;
5735 tree id = c_parser_peek_token (parser)->value;
5736 c_parser_consume_token (parser);
5737 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5738 "expected %<)%>");
5739 expr.value = objc_build_protocol_expr (id);
5741 break;
5742 case RID_AT_ENCODE:
5743 /* Extension to support C-structures in the archiver. */
5744 gcc_assert (c_dialect_objc ());
5745 c_parser_consume_token (parser);
5746 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5748 expr.value = error_mark_node;
5749 break;
5751 t1 = c_parser_type_name (parser);
5752 if (t1 == NULL)
5754 expr.value = error_mark_node;
5755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5756 break;
5758 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5759 "expected %<)%>");
5761 tree type = groktypename (t1, NULL, NULL);
5762 expr.value = objc_build_encode_expr (type);
5764 break;
5765 default:
5766 c_parser_error (parser, "expected expression");
5767 expr.value = error_mark_node;
5768 break;
5770 break;
5771 case CPP_OPEN_SQUARE:
5772 if (c_dialect_objc ())
5774 tree receiver, args;
5775 c_parser_consume_token (parser);
5776 receiver = c_parser_objc_receiver (parser);
5777 args = c_parser_objc_message_args (parser);
5778 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5779 "expected %<]%>");
5780 expr.value = objc_build_message_expr (build_tree_list (receiver,
5781 args));
5782 break;
5784 /* Else fall through to report error. */
5785 default:
5786 c_parser_error (parser, "expected expression");
5787 expr.value = error_mark_node;
5788 break;
5790 return c_parser_postfix_expression_after_primary (parser, loc, expr);
5793 /* Parse a postfix expression after a parenthesized type name: the
5794 brace-enclosed initializer of a compound literal, possibly followed
5795 by some postfix operators. This is separate because it is not
5796 possible to tell until after the type name whether a cast
5797 expression has a cast or a compound literal, or whether the operand
5798 of sizeof is a parenthesized type name or starts with a compound
5799 literal. TYPE_LOC is the location where TYPE_NAME starts--the
5800 location of the first token after the parentheses around the type
5801 name. */
5803 static struct c_expr
5804 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5805 struct c_type_name *type_name,
5806 location_t type_loc)
5808 tree type;
5809 struct c_expr init;
5810 bool non_const;
5811 struct c_expr expr;
5812 location_t start_loc;
5813 tree type_expr = NULL_TREE;
5814 bool type_expr_const = true;
5815 check_compound_literal_type (type_loc, type_name);
5816 start_init (NULL_TREE, NULL, 0);
5817 type = groktypename (type_name, &type_expr, &type_expr_const);
5818 start_loc = c_parser_peek_token (parser)->location;
5819 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5821 error_at (type_loc, "compound literal has variable size");
5822 type = error_mark_node;
5824 init = c_parser_braced_init (parser, type, false);
5825 finish_init ();
5826 maybe_warn_string_init (type, init);
5828 if (type != error_mark_node
5829 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
5830 && current_function_decl)
5832 error ("compound literal qualified by address-space qualifier");
5833 type = error_mark_node;
5836 if (!flag_isoc99)
5837 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
5838 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
5839 ? CONSTRUCTOR_NON_CONST (init.value)
5840 : init.original_code == C_MAYBE_CONST_EXPR);
5841 non_const |= !type_expr_const;
5842 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
5843 expr.original_code = ERROR_MARK;
5844 expr.original_type = NULL;
5845 if (type_expr)
5847 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
5849 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
5850 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
5852 else
5854 gcc_assert (!non_const);
5855 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
5856 type_expr, expr.value);
5859 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
5862 /* Parse a postfix expression after the initial primary or compound
5863 literal; that is, parse a series of postfix operators.
5865 EXPR_LOC is the location of the primary expression. */
5867 static struct c_expr
5868 c_parser_postfix_expression_after_primary (c_parser *parser,
5869 location_t expr_loc,
5870 struct c_expr expr)
5872 struct c_expr orig_expr;
5873 tree ident, idx;
5874 VEC(tree,gc) *exprlist;
5875 VEC(tree,gc) *origtypes;
5876 while (true)
5878 location_t op_loc = c_parser_peek_token (parser)->location;
5879 switch (c_parser_peek_token (parser)->type)
5881 case CPP_OPEN_SQUARE:
5882 /* Array reference. */
5883 c_parser_consume_token (parser);
5884 idx = c_parser_expression (parser).value;
5885 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5886 "expected %<]%>");
5887 expr.value = build_array_ref (op_loc, expr.value, idx);
5888 expr.original_code = ERROR_MARK;
5889 expr.original_type = NULL;
5890 break;
5891 case CPP_OPEN_PAREN:
5892 /* Function call. */
5893 c_parser_consume_token (parser);
5894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5895 exprlist = NULL;
5896 else
5897 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
5898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5899 "expected %<)%>");
5900 orig_expr = expr;
5901 mark_exp_read (expr.value);
5902 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
5903 "(" after the FUNCNAME, which is what we have now. */
5904 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
5905 origtypes);
5906 expr.original_code = ERROR_MARK;
5907 if (TREE_CODE (expr.value) == INTEGER_CST
5908 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
5909 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
5910 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
5911 expr.original_code = C_MAYBE_CONST_EXPR;
5912 expr.original_type = NULL;
5913 if (exprlist != NULL)
5915 release_tree_vector (exprlist);
5916 release_tree_vector (origtypes);
5918 break;
5919 case CPP_DOT:
5920 /* Structure element reference. */
5921 c_parser_consume_token (parser);
5922 expr = default_function_array_conversion (expr_loc, expr);
5923 if (c_parser_next_token_is (parser, CPP_NAME))
5924 ident = c_parser_peek_token (parser)->value;
5925 else
5927 c_parser_error (parser, "expected identifier");
5928 expr.value = error_mark_node;
5929 expr.original_code = ERROR_MARK;
5930 expr.original_type = NULL;
5931 return expr;
5933 c_parser_consume_token (parser);
5934 expr.value = build_component_ref (op_loc, expr.value, ident);
5935 expr.original_code = ERROR_MARK;
5936 if (TREE_CODE (expr.value) != COMPONENT_REF)
5937 expr.original_type = NULL;
5938 else
5940 /* Remember the original type of a bitfield. */
5941 tree field = TREE_OPERAND (expr.value, 1);
5942 if (TREE_CODE (field) != FIELD_DECL)
5943 expr.original_type = NULL;
5944 else
5945 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5947 break;
5948 case CPP_DEREF:
5949 /* Structure element reference. */
5950 c_parser_consume_token (parser);
5951 expr = default_function_array_conversion (expr_loc, expr);
5952 if (c_parser_next_token_is (parser, CPP_NAME))
5953 ident = c_parser_peek_token (parser)->value;
5954 else
5956 c_parser_error (parser, "expected identifier");
5957 expr.value = error_mark_node;
5958 expr.original_code = ERROR_MARK;
5959 expr.original_type = NULL;
5960 return expr;
5962 c_parser_consume_token (parser);
5963 expr.value = build_component_ref (op_loc,
5964 build_indirect_ref (op_loc,
5965 expr.value,
5966 RO_ARROW),
5967 ident);
5968 expr.original_code = ERROR_MARK;
5969 if (TREE_CODE (expr.value) != COMPONENT_REF)
5970 expr.original_type = NULL;
5971 else
5973 /* Remember the original type of a bitfield. */
5974 tree field = TREE_OPERAND (expr.value, 1);
5975 if (TREE_CODE (field) != FIELD_DECL)
5976 expr.original_type = NULL;
5977 else
5978 expr.original_type = DECL_BIT_FIELD_TYPE (field);
5980 break;
5981 case CPP_PLUS_PLUS:
5982 /* Postincrement. */
5983 c_parser_consume_token (parser);
5984 expr = default_function_array_read_conversion (expr_loc, expr);
5985 expr.value = build_unary_op (op_loc,
5986 POSTINCREMENT_EXPR, expr.value, 0);
5987 expr.original_code = ERROR_MARK;
5988 expr.original_type = NULL;
5989 break;
5990 case CPP_MINUS_MINUS:
5991 /* Postdecrement. */
5992 c_parser_consume_token (parser);
5993 expr = default_function_array_read_conversion (expr_loc, expr);
5994 expr.value = build_unary_op (op_loc,
5995 POSTDECREMENT_EXPR, expr.value, 0);
5996 expr.original_code = ERROR_MARK;
5997 expr.original_type = NULL;
5998 break;
5999 default:
6000 return expr;
6005 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6007 expression:
6008 assignment-expression
6009 expression , assignment-expression
6012 static struct c_expr
6013 c_parser_expression (c_parser *parser)
6015 struct c_expr expr;
6016 expr = c_parser_expr_no_commas (parser, NULL);
6017 while (c_parser_next_token_is (parser, CPP_COMMA))
6019 struct c_expr next;
6020 location_t loc = c_parser_peek_token (parser)->location;
6021 location_t expr_loc;
6022 c_parser_consume_token (parser);
6023 expr_loc = c_parser_peek_token (parser)->location;
6024 next = c_parser_expr_no_commas (parser, NULL);
6025 next = default_function_array_conversion (expr_loc, next);
6026 expr.value = build_compound_expr (loc, expr.value, next.value);
6027 expr.original_code = COMPOUND_EXPR;
6028 expr.original_type = next.original_type;
6030 return expr;
6033 /* Parse an expression and convert functions or arrays to
6034 pointers. */
6036 static struct c_expr
6037 c_parser_expression_conv (c_parser *parser)
6039 struct c_expr expr;
6040 location_t loc = c_parser_peek_token (parser)->location;
6041 expr = c_parser_expression (parser);
6042 expr = default_function_array_conversion (loc, expr);
6043 return expr;
6046 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6047 functions and arrays to pointers. If FOLD_P, fold the expressions.
6049 nonempty-expr-list:
6050 assignment-expression
6051 nonempty-expr-list , assignment-expression
6054 static VEC(tree,gc) *
6055 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6056 VEC(tree,gc) **p_orig_types)
6058 VEC(tree,gc) *ret;
6059 VEC(tree,gc) *orig_types;
6060 struct c_expr expr;
6061 location_t loc = c_parser_peek_token (parser)->location;
6063 ret = make_tree_vector ();
6064 if (p_orig_types == NULL)
6065 orig_types = NULL;
6066 else
6067 orig_types = make_tree_vector ();
6069 expr = c_parser_expr_no_commas (parser, NULL);
6070 if (convert_p)
6071 expr = default_function_array_read_conversion (loc, expr);
6072 if (fold_p)
6073 expr.value = c_fully_fold (expr.value, false, NULL);
6074 VEC_quick_push (tree, ret, expr.value);
6075 if (orig_types != NULL)
6076 VEC_quick_push (tree, orig_types, expr.original_type);
6077 while (c_parser_next_token_is (parser, CPP_COMMA))
6079 c_parser_consume_token (parser);
6080 loc = c_parser_peek_token (parser)->location;
6081 expr = c_parser_expr_no_commas (parser, NULL);
6082 if (convert_p)
6083 expr = default_function_array_read_conversion (loc, expr);
6084 if (fold_p)
6085 expr.value = c_fully_fold (expr.value, false, NULL);
6086 VEC_safe_push (tree, gc, ret, expr.value);
6087 if (orig_types != NULL)
6088 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6090 if (orig_types != NULL)
6091 *p_orig_types = orig_types;
6092 return ret;
6095 /* Parse Objective-C-specific constructs. */
6097 /* Parse an objc-class-definition.
6099 objc-class-definition:
6100 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6101 objc-class-instance-variables[opt] objc-methodprotolist @end
6102 @implementation identifier objc-superclass[opt]
6103 objc-class-instance-variables[opt]
6104 @interface identifier ( identifier ) objc-protocol-refs[opt]
6105 objc-methodprotolist @end
6106 @implementation identifier ( identifier )
6108 objc-superclass:
6109 : identifier
6111 "@interface identifier (" must start "@interface identifier (
6112 identifier ) ...": objc-methodprotolist in the first production may
6113 not start with a parenthesized identifier as a declarator of a data
6114 definition with no declaration specifiers if the objc-superclass,
6115 objc-protocol-refs and objc-class-instance-variables are omitted. */
6117 static void
6118 c_parser_objc_class_definition (c_parser *parser)
6120 bool iface_p;
6121 tree id1;
6122 tree superclass;
6123 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6124 iface_p = true;
6125 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6126 iface_p = false;
6127 else
6128 gcc_unreachable ();
6129 c_parser_consume_token (parser);
6130 if (c_parser_next_token_is_not (parser, CPP_NAME))
6132 c_parser_error (parser, "expected identifier");
6133 return;
6135 id1 = c_parser_peek_token (parser)->value;
6136 c_parser_consume_token (parser);
6137 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6139 tree id2;
6140 tree proto = NULL_TREE;
6141 c_parser_consume_token (parser);
6142 if (c_parser_next_token_is_not (parser, CPP_NAME))
6144 c_parser_error (parser, "expected identifier");
6145 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6146 return;
6148 id2 = c_parser_peek_token (parser)->value;
6149 c_parser_consume_token (parser);
6150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6151 if (!iface_p)
6153 objc_start_category_implementation (id1, id2);
6154 return;
6156 if (c_parser_next_token_is (parser, CPP_LESS))
6157 proto = c_parser_objc_protocol_refs (parser);
6158 objc_start_category_interface (id1, id2, proto);
6159 c_parser_objc_methodprotolist (parser);
6160 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6161 objc_finish_interface ();
6162 return;
6164 if (c_parser_next_token_is (parser, CPP_COLON))
6166 c_parser_consume_token (parser);
6167 if (c_parser_next_token_is_not (parser, CPP_NAME))
6169 c_parser_error (parser, "expected identifier");
6170 return;
6172 superclass = c_parser_peek_token (parser)->value;
6173 c_parser_consume_token (parser);
6175 else
6176 superclass = NULL_TREE;
6177 if (iface_p)
6179 tree proto = NULL_TREE;
6180 if (c_parser_next_token_is (parser, CPP_LESS))
6181 proto = c_parser_objc_protocol_refs (parser);
6182 objc_start_class_interface (id1, superclass, proto);
6184 else
6185 objc_start_class_implementation (id1, superclass);
6186 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6187 c_parser_objc_class_instance_variables (parser);
6188 if (iface_p)
6190 objc_continue_interface ();
6191 c_parser_objc_methodprotolist (parser);
6192 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6193 objc_finish_interface ();
6195 else
6197 objc_continue_implementation ();
6198 return;
6202 /* Parse objc-class-instance-variables.
6204 objc-class-instance-variables:
6205 { objc-instance-variable-decl-list[opt] }
6207 objc-instance-variable-decl-list:
6208 objc-visibility-spec
6209 objc-instance-variable-decl ;
6211 objc-instance-variable-decl-list objc-visibility-spec
6212 objc-instance-variable-decl-list objc-instance-variable-decl ;
6213 objc-instance-variable-decl-list ;
6215 objc-visibility-spec:
6216 @private
6217 @protected
6218 @public
6220 objc-instance-variable-decl:
6221 struct-declaration
6224 static void
6225 c_parser_objc_class_instance_variables (c_parser *parser)
6227 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6228 c_parser_consume_token (parser);
6229 while (c_parser_next_token_is_not (parser, CPP_EOF))
6231 tree decls;
6232 /* Parse any stray semicolon. */
6233 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6235 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6236 "extra semicolon in struct or union specified");
6237 c_parser_consume_token (parser);
6238 continue;
6240 /* Stop if at the end of the instance variables. */
6241 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6243 c_parser_consume_token (parser);
6244 break;
6246 /* Parse any objc-visibility-spec. */
6247 if (c_parser_next_token_is_keyword (parser, RID_PRIVATE))
6249 c_parser_consume_token (parser);
6250 objc_set_visibility (2);
6251 continue;
6253 else if (c_parser_next_token_is_keyword (parser, RID_PROTECTED))
6255 c_parser_consume_token (parser);
6256 objc_set_visibility (0);
6257 continue;
6259 else if (c_parser_next_token_is_keyword (parser, RID_PUBLIC))
6261 c_parser_consume_token (parser);
6262 objc_set_visibility (1);
6263 continue;
6265 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6267 c_parser_pragma (parser, pragma_external);
6268 continue;
6271 /* Parse some comma-separated declarations. */
6272 decls = c_parser_struct_declaration (parser);
6274 /* Comma-separated instance variables are chained together in
6275 reverse order; add them one by one. */
6276 tree ivar = nreverse (decls);
6277 for (; ivar; ivar = TREE_CHAIN (ivar))
6278 objc_add_instance_variable (copy_node (ivar));
6280 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6284 /* Parse an objc-class-declaration.
6286 objc-class-declaration:
6287 @class identifier-list ;
6290 static void
6291 c_parser_objc_class_declaration (c_parser *parser)
6293 tree list = NULL_TREE;
6294 gcc_assert (c_parser_next_token_is_keyword (parser, RID_CLASS));
6295 c_parser_consume_token (parser);
6296 /* Any identifiers, including those declared as type names, are OK
6297 here. */
6298 while (true)
6300 tree id;
6301 if (c_parser_next_token_is_not (parser, CPP_NAME))
6303 c_parser_error (parser, "expected identifier");
6304 break;
6306 id = c_parser_peek_token (parser)->value;
6307 list = chainon (list, build_tree_list (NULL_TREE, id));
6308 c_parser_consume_token (parser);
6309 if (c_parser_next_token_is (parser, CPP_COMMA))
6310 c_parser_consume_token (parser);
6311 else
6312 break;
6314 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6315 objc_declare_class (list);
6318 /* Parse an objc-alias-declaration.
6320 objc-alias-declaration:
6321 @compatibility_alias identifier identifier ;
6324 static void
6325 c_parser_objc_alias_declaration (c_parser *parser)
6327 tree id1, id2;
6328 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
6329 c_parser_consume_token (parser);
6330 if (c_parser_next_token_is_not (parser, CPP_NAME))
6332 c_parser_error (parser, "expected identifier");
6333 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6334 return;
6336 id1 = c_parser_peek_token (parser)->value;
6337 c_parser_consume_token (parser);
6338 if (c_parser_next_token_is_not (parser, CPP_NAME))
6340 c_parser_error (parser, "expected identifier");
6341 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
6342 return;
6344 id2 = c_parser_peek_token (parser)->value;
6345 c_parser_consume_token (parser);
6346 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6347 objc_declare_alias (id1, id2);
6350 /* Parse an objc-protocol-definition.
6352 objc-protocol-definition:
6353 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
6354 @protocol identifier-list ;
6356 "@protocol identifier ;" should be resolved as "@protocol
6357 identifier-list ;": objc-methodprotolist may not start with a
6358 semicolon in the first alternative if objc-protocol-refs are
6359 omitted. */
6361 static void
6362 c_parser_objc_protocol_definition (c_parser *parser)
6364 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
6365 c_parser_consume_token (parser);
6366 if (c_parser_next_token_is_not (parser, CPP_NAME))
6368 c_parser_error (parser, "expected identifier");
6369 return;
6371 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
6372 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
6374 tree list = NULL_TREE;
6375 /* Any identifiers, including those declared as type names, are
6376 OK here. */
6377 while (true)
6379 tree id;
6380 if (c_parser_next_token_is_not (parser, CPP_NAME))
6382 c_parser_error (parser, "expected identifier");
6383 break;
6385 id = c_parser_peek_token (parser)->value;
6386 list = chainon (list, build_tree_list (NULL_TREE, id));
6387 c_parser_consume_token (parser);
6388 if (c_parser_next_token_is (parser, CPP_COMMA))
6389 c_parser_consume_token (parser);
6390 else
6391 break;
6393 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6394 objc_declare_protocols (list);
6396 else
6398 tree id = c_parser_peek_token (parser)->value;
6399 tree proto = NULL_TREE;
6400 c_parser_consume_token (parser);
6401 if (c_parser_next_token_is (parser, CPP_LESS))
6402 proto = c_parser_objc_protocol_refs (parser);
6403 parser->objc_pq_context = true;
6404 objc_start_protocol (id, proto);
6405 c_parser_objc_methodprotolist (parser);
6406 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6407 parser->objc_pq_context = false;
6408 objc_finish_interface ();
6412 /* Parse an objc-method-type.
6414 objc-method-type:
6419 static enum tree_code
6420 c_parser_objc_method_type (c_parser *parser)
6422 switch (c_parser_peek_token (parser)->type)
6424 case CPP_PLUS:
6425 c_parser_consume_token (parser);
6426 return PLUS_EXPR;
6427 case CPP_MINUS:
6428 c_parser_consume_token (parser);
6429 return MINUS_EXPR;
6430 default:
6431 gcc_unreachable ();
6435 /* Parse an objc-method-definition.
6437 objc-method-definition:
6438 objc-method-type objc-method-decl ;[opt] compound-statement
6441 static void
6442 c_parser_objc_method_definition (c_parser *parser)
6444 enum tree_code type = c_parser_objc_method_type (parser);
6445 tree decl;
6446 objc_set_method_type (type);
6447 parser->objc_pq_context = true;
6448 decl = c_parser_objc_method_decl (parser);
6449 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6451 c_parser_consume_token (parser);
6452 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6453 "extra semicolon in method definition specified");
6455 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6457 c_parser_error (parser, "expected %<{%>");
6458 return;
6460 parser->objc_pq_context = false;
6461 objc_start_method_definition (decl);
6462 add_stmt (c_parser_compound_statement (parser));
6463 objc_finish_method_definition (current_function_decl);
6466 /* Parse an objc-methodprotolist.
6468 objc-methodprotolist:
6469 empty
6470 objc-methodprotolist objc-methodproto
6471 objc-methodprotolist declaration
6472 objc-methodprotolist ;
6474 The declaration is a data definition, which may be missing
6475 declaration specifiers under the same rules and diagnostics as
6476 other data definitions outside functions, and the stray semicolon
6477 is diagnosed the same way as a stray semicolon outside a
6478 function. */
6480 static void
6481 c_parser_objc_methodprotolist (c_parser *parser)
6483 while (true)
6485 /* The list is terminated by @end. */
6486 switch (c_parser_peek_token (parser)->type)
6488 case CPP_SEMICOLON:
6489 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6490 "ISO C does not allow extra %<;%> outside of a function");
6491 c_parser_consume_token (parser);
6492 break;
6493 case CPP_PLUS:
6494 case CPP_MINUS:
6495 c_parser_objc_methodproto (parser);
6496 break;
6497 case CPP_PRAGMA:
6498 c_parser_pragma (parser, pragma_external);
6499 break;
6500 case CPP_EOF:
6501 return;
6502 default:
6503 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6504 return;
6505 c_parser_declaration_or_fndef (parser, false, true, false, true);
6506 break;
6511 /* Parse an objc-methodproto.
6513 objc-methodproto:
6514 objc-method-type objc-method-decl ;
6517 static void
6518 c_parser_objc_methodproto (c_parser *parser)
6520 enum tree_code type = c_parser_objc_method_type (parser);
6521 tree decl;
6522 objc_set_method_type (type);
6523 /* Remember protocol qualifiers in prototypes. */
6524 parser->objc_pq_context = true;
6525 decl = c_parser_objc_method_decl (parser);
6526 /* Forget protocol qualifiers here. */
6527 parser->objc_pq_context = false;
6528 objc_add_method_declaration (decl);
6529 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6532 /* Parse an objc-method-decl.
6534 objc-method-decl:
6535 ( objc-type-name ) objc-selector
6536 objc-selector
6537 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6538 objc-keyword-selector objc-optparmlist
6540 objc-keyword-selector:
6541 objc-keyword-decl
6542 objc-keyword-selector objc-keyword-decl
6544 objc-keyword-decl:
6545 objc-selector : ( objc-type-name ) identifier
6546 objc-selector : identifier
6547 : ( objc-type-name ) identifier
6548 : identifier
6550 objc-optparmlist:
6551 objc-optparms objc-optellipsis
6553 objc-optparms:
6554 empty
6555 objc-opt-parms , parameter-declaration
6557 objc-optellipsis:
6558 empty
6559 , ...
6562 static tree
6563 c_parser_objc_method_decl (c_parser *parser)
6565 tree type = NULL_TREE;
6566 tree sel;
6567 tree parms = NULL_TREE;
6568 bool ellipsis = false;
6570 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6572 c_parser_consume_token (parser);
6573 type = c_parser_objc_type_name (parser);
6574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6576 sel = c_parser_objc_selector (parser);
6577 /* If there is no selector, or a colon follows, we have an
6578 objc-keyword-selector. If there is a selector, and a colon does
6579 not follow, that selector ends the objc-method-decl. */
6580 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6582 tree tsel = sel;
6583 tree list = NULL_TREE;
6584 while (true)
6586 tree atype = NULL_TREE, id, keyworddecl;
6587 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6588 break;
6589 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6591 c_parser_consume_token (parser);
6592 atype = c_parser_objc_type_name (parser);
6593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6594 "expected %<)%>");
6596 if (c_parser_next_token_is_not (parser, CPP_NAME))
6598 c_parser_error (parser, "expected identifier");
6599 return error_mark_node;
6601 id = c_parser_peek_token (parser)->value;
6602 c_parser_consume_token (parser);
6603 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6604 list = chainon (list, keyworddecl);
6605 tsel = c_parser_objc_selector (parser);
6606 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6607 break;
6609 /* Parse the optional parameter list. Optional Objective-C
6610 method parameters follow the C syntax, and may include '...'
6611 to denote a variable number of arguments. */
6612 parms = make_node (TREE_LIST);
6613 while (c_parser_next_token_is (parser, CPP_COMMA))
6615 struct c_parm *parm;
6616 c_parser_consume_token (parser);
6617 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6619 ellipsis = true;
6620 c_parser_consume_token (parser);
6621 break;
6623 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6624 if (parm == NULL)
6625 break;
6626 parms = chainon (parms,
6627 build_tree_list (NULL_TREE, grokparm (parm)));
6629 sel = list;
6631 return objc_build_method_signature (type, sel, parms, ellipsis);
6634 /* Parse an objc-type-name.
6636 objc-type-name:
6637 objc-type-qualifiers[opt] type-name
6638 objc-type-qualifiers[opt]
6640 objc-type-qualifiers:
6641 objc-type-qualifier
6642 objc-type-qualifiers objc-type-qualifier
6644 objc-type-qualifier: one of
6645 in out inout bycopy byref oneway
6648 static tree
6649 c_parser_objc_type_name (c_parser *parser)
6651 tree quals = NULL_TREE;
6652 struct c_type_name *type_name = NULL;
6653 tree type = NULL_TREE;
6654 while (true)
6656 c_token *token = c_parser_peek_token (parser);
6657 if (token->type == CPP_KEYWORD
6658 && (token->keyword == RID_IN
6659 || token->keyword == RID_OUT
6660 || token->keyword == RID_INOUT
6661 || token->keyword == RID_BYCOPY
6662 || token->keyword == RID_BYREF
6663 || token->keyword == RID_ONEWAY))
6665 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6666 c_parser_consume_token (parser);
6668 else
6669 break;
6671 if (c_parser_next_token_starts_typename (parser))
6672 type_name = c_parser_type_name (parser);
6673 if (type_name)
6674 type = groktypename (type_name, NULL, NULL);
6675 return build_tree_list (quals, type);
6678 /* Parse objc-protocol-refs.
6680 objc-protocol-refs:
6681 < identifier-list >
6684 static tree
6685 c_parser_objc_protocol_refs (c_parser *parser)
6687 tree list = NULL_TREE;
6688 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6689 c_parser_consume_token (parser);
6690 /* Any identifiers, including those declared as type names, are OK
6691 here. */
6692 while (true)
6694 tree id;
6695 if (c_parser_next_token_is_not (parser, CPP_NAME))
6697 c_parser_error (parser, "expected identifier");
6698 break;
6700 id = c_parser_peek_token (parser)->value;
6701 list = chainon (list, build_tree_list (NULL_TREE, id));
6702 c_parser_consume_token (parser);
6703 if (c_parser_next_token_is (parser, CPP_COMMA))
6704 c_parser_consume_token (parser);
6705 else
6706 break;
6708 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6709 return list;
6712 /* Parse an objc-try-catch-statement.
6714 objc-try-catch-statement:
6715 @try compound-statement objc-catch-list[opt]
6716 @try compound-statement objc-catch-list[opt] @finally compound-statement
6718 objc-catch-list:
6719 @catch ( parameter-declaration ) compound-statement
6720 objc-catch-list @catch ( parameter-declaration ) compound-statement
6723 static void
6724 c_parser_objc_try_catch_statement (c_parser *parser)
6726 location_t loc;
6727 tree stmt;
6728 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRY));
6729 c_parser_consume_token (parser);
6730 loc = c_parser_peek_token (parser)->location;
6731 stmt = c_parser_compound_statement (parser);
6732 objc_begin_try_stmt (loc, stmt);
6733 while (c_parser_next_token_is_keyword (parser, RID_CATCH))
6735 struct c_parm *parm;
6736 c_parser_consume_token (parser);
6737 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6738 break;
6739 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6740 if (parm == NULL)
6742 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6743 break;
6745 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6746 objc_begin_catch_clause (grokparm (parm));
6747 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6748 c_parser_compound_statement_nostart (parser);
6749 objc_finish_catch_clause ();
6751 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6753 location_t finloc;
6754 tree finstmt;
6755 c_parser_consume_token (parser);
6756 finloc = c_parser_peek_token (parser)->location;
6757 finstmt = c_parser_compound_statement (parser);
6758 objc_build_finally_clause (finloc, finstmt);
6760 objc_finish_try_stmt ();
6763 /* Parse an objc-synchronized-statement.
6765 objc-synchronized-statement:
6766 @synchronized ( expression ) compound-statement
6769 static void
6770 c_parser_objc_synchronized_statement (c_parser *parser)
6772 location_t loc;
6773 tree expr, stmt;
6774 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6775 c_parser_consume_token (parser);
6776 loc = c_parser_peek_token (parser)->location;
6777 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6779 expr = c_parser_expression (parser).value;
6780 expr = c_fully_fold (expr, false, NULL);
6781 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6783 else
6784 expr = error_mark_node;
6785 stmt = c_parser_compound_statement (parser);
6786 objc_build_synchronized (loc, expr, stmt);
6789 /* Parse an objc-selector; return NULL_TREE without an error if the
6790 next token is not an objc-selector.
6792 objc-selector:
6793 identifier
6794 one of
6795 enum struct union if else while do for switch case default
6796 break continue return goto asm sizeof typeof __alignof
6797 unsigned long const short volatile signed restrict _Complex
6798 in out inout bycopy byref oneway int char float double void _Bool
6800 ??? Why this selection of keywords but not, for example, storage
6801 class specifiers? */
6803 static tree
6804 c_parser_objc_selector (c_parser *parser)
6806 c_token *token = c_parser_peek_token (parser);
6807 tree value = token->value;
6808 if (token->type == CPP_NAME)
6810 c_parser_consume_token (parser);
6811 return value;
6813 if (token->type != CPP_KEYWORD)
6814 return NULL_TREE;
6815 switch (token->keyword)
6817 case RID_ENUM:
6818 case RID_STRUCT:
6819 case RID_UNION:
6820 case RID_IF:
6821 case RID_ELSE:
6822 case RID_WHILE:
6823 case RID_DO:
6824 case RID_FOR:
6825 case RID_SWITCH:
6826 case RID_CASE:
6827 case RID_DEFAULT:
6828 case RID_BREAK:
6829 case RID_CONTINUE:
6830 case RID_RETURN:
6831 case RID_GOTO:
6832 case RID_ASM:
6833 case RID_SIZEOF:
6834 case RID_TYPEOF:
6835 case RID_ALIGNOF:
6836 case RID_UNSIGNED:
6837 case RID_LONG:
6838 case RID_CONST:
6839 case RID_SHORT:
6840 case RID_VOLATILE:
6841 case RID_SIGNED:
6842 case RID_RESTRICT:
6843 case RID_COMPLEX:
6844 case RID_IN:
6845 case RID_OUT:
6846 case RID_INOUT:
6847 case RID_BYCOPY:
6848 case RID_BYREF:
6849 case RID_ONEWAY:
6850 case RID_INT:
6851 case RID_CHAR:
6852 case RID_FLOAT:
6853 case RID_DOUBLE:
6854 case RID_VOID:
6855 case RID_BOOL:
6856 c_parser_consume_token (parser);
6857 return value;
6858 default:
6859 return NULL_TREE;
6863 /* Parse an objc-selector-arg.
6865 objc-selector-arg:
6866 objc-selector
6867 objc-keywordname-list
6869 objc-keywordname-list:
6870 objc-keywordname
6871 objc-keywordname-list objc-keywordname
6873 objc-keywordname:
6874 objc-selector :
6878 static tree
6879 c_parser_objc_selector_arg (c_parser *parser)
6881 tree sel = c_parser_objc_selector (parser);
6882 tree list = NULL_TREE;
6883 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6884 return sel;
6885 while (true)
6887 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6888 return list;
6889 list = chainon (list, build_tree_list (sel, NULL_TREE));
6890 sel = c_parser_objc_selector (parser);
6891 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6892 break;
6894 return list;
6897 /* Parse an objc-receiver.
6899 objc-receiver:
6900 expression
6901 class-name
6902 type-name
6905 static tree
6906 c_parser_objc_receiver (c_parser *parser)
6908 if (c_parser_peek_token (parser)->type == CPP_NAME
6909 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6910 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6912 tree id = c_parser_peek_token (parser)->value;
6913 c_parser_consume_token (parser);
6914 return objc_get_class_reference (id);
6916 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
6919 /* Parse objc-message-args.
6921 objc-message-args:
6922 objc-selector
6923 objc-keywordarg-list
6925 objc-keywordarg-list:
6926 objc-keywordarg
6927 objc-keywordarg-list objc-keywordarg
6929 objc-keywordarg:
6930 objc-selector : objc-keywordexpr
6931 : objc-keywordexpr
6934 static tree
6935 c_parser_objc_message_args (c_parser *parser)
6937 tree sel = c_parser_objc_selector (parser);
6938 tree list = NULL_TREE;
6939 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6940 return sel;
6941 while (true)
6943 tree keywordexpr;
6944 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6945 return error_mark_node;
6946 keywordexpr = c_parser_objc_keywordexpr (parser);
6947 list = chainon (list, build_tree_list (sel, keywordexpr));
6948 sel = c_parser_objc_selector (parser);
6949 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6950 break;
6952 return list;
6955 /* Parse an objc-keywordexpr.
6957 objc-keywordexpr:
6958 nonempty-expr-list
6961 static tree
6962 c_parser_objc_keywordexpr (c_parser *parser)
6964 tree ret;
6965 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
6966 if (VEC_length (tree, expr_list) == 1)
6968 /* Just return the expression, remove a level of
6969 indirection. */
6970 ret = VEC_index (tree, expr_list, 0);
6972 else
6974 /* We have a comma expression, we will collapse later. */
6975 ret = build_tree_list_vec (expr_list);
6977 release_tree_vector (expr_list);
6978 return ret;
6982 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6983 should be considered, statements. ALLOW_STMT is true if we're within
6984 the context of a function and such pragmas are to be allowed. Returns
6985 true if we actually parsed such a pragma. */
6987 static bool
6988 c_parser_pragma (c_parser *parser, enum pragma_context context)
6990 unsigned int id;
6992 id = c_parser_peek_token (parser)->pragma_kind;
6993 gcc_assert (id != PRAGMA_NONE);
6995 switch (id)
6997 case PRAGMA_OMP_BARRIER:
6998 if (context != pragma_compound)
7000 if (context == pragma_stmt)
7001 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
7002 "used in compound statements");
7003 goto bad_stmt;
7005 c_parser_omp_barrier (parser);
7006 return false;
7008 case PRAGMA_OMP_FLUSH:
7009 if (context != pragma_compound)
7011 if (context == pragma_stmt)
7012 c_parser_error (parser, "%<#pragma omp flush%> may only be "
7013 "used in compound statements");
7014 goto bad_stmt;
7016 c_parser_omp_flush (parser);
7017 return false;
7019 case PRAGMA_OMP_TASKWAIT:
7020 if (context != pragma_compound)
7022 if (context == pragma_stmt)
7023 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
7024 "used in compound statements");
7025 goto bad_stmt;
7027 c_parser_omp_taskwait (parser);
7028 return false;
7030 case PRAGMA_OMP_THREADPRIVATE:
7031 c_parser_omp_threadprivate (parser);
7032 return false;
7034 case PRAGMA_OMP_SECTION:
7035 error_at (c_parser_peek_token (parser)->location,
7036 "%<#pragma omp section%> may only be used in "
7037 "%<#pragma omp sections%> construct");
7038 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7039 return false;
7041 case PRAGMA_GCC_PCH_PREPROCESS:
7042 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
7043 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7044 return false;
7046 default:
7047 if (id < PRAGMA_FIRST_EXTERNAL)
7049 if (context == pragma_external)
7051 bad_stmt:
7052 c_parser_error (parser, "expected declaration specifiers");
7053 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
7054 return false;
7056 c_parser_omp_construct (parser);
7057 return true;
7059 break;
7062 c_parser_consume_pragma (parser);
7063 c_invoke_pragma_handler (id);
7065 /* Skip to EOL, but suppress any error message. Those will have been
7066 generated by the handler routine through calling error, as opposed
7067 to calling c_parser_error. */
7068 parser->error = true;
7069 c_parser_skip_to_pragma_eol (parser);
7071 return false;
7074 /* The interface the pragma parsers have to the lexer. */
7076 enum cpp_ttype
7077 pragma_lex (tree *value)
7079 c_token *tok = c_parser_peek_token (the_parser);
7080 enum cpp_ttype ret = tok->type;
7082 *value = tok->value;
7083 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
7084 ret = CPP_EOF;
7085 else
7087 if (ret == CPP_KEYWORD)
7088 ret = CPP_NAME;
7089 c_parser_consume_token (the_parser);
7092 return ret;
7095 static void
7096 c_parser_pragma_pch_preprocess (c_parser *parser)
7098 tree name = NULL;
7100 c_parser_consume_pragma (parser);
7101 if (c_parser_next_token_is (parser, CPP_STRING))
7103 name = c_parser_peek_token (parser)->value;
7104 c_parser_consume_token (parser);
7106 else
7107 c_parser_error (parser, "expected string literal");
7108 c_parser_skip_to_pragma_eol (parser);
7110 if (name)
7111 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
7114 /* OpenMP 2.5 parsing routines. */
7116 /* Returns name of the next clause.
7117 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
7118 the token is not consumed. Otherwise appropriate pragma_omp_clause is
7119 returned and the token is consumed. */
7121 static pragma_omp_clause
7122 c_parser_omp_clause_name (c_parser *parser)
7124 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
7126 if (c_parser_next_token_is_keyword (parser, RID_IF))
7127 result = PRAGMA_OMP_CLAUSE_IF;
7128 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
7129 result = PRAGMA_OMP_CLAUSE_DEFAULT;
7130 else if (c_parser_next_token_is (parser, CPP_NAME))
7132 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7134 switch (p[0])
7136 case 'c':
7137 if (!strcmp ("collapse", p))
7138 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
7139 else if (!strcmp ("copyin", p))
7140 result = PRAGMA_OMP_CLAUSE_COPYIN;
7141 else if (!strcmp ("copyprivate", p))
7142 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
7143 break;
7144 case 'f':
7145 if (!strcmp ("firstprivate", p))
7146 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
7147 break;
7148 case 'l':
7149 if (!strcmp ("lastprivate", p))
7150 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
7151 break;
7152 case 'n':
7153 if (!strcmp ("nowait", p))
7154 result = PRAGMA_OMP_CLAUSE_NOWAIT;
7155 else if (!strcmp ("num_threads", p))
7156 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
7157 break;
7158 case 'o':
7159 if (!strcmp ("ordered", p))
7160 result = PRAGMA_OMP_CLAUSE_ORDERED;
7161 break;
7162 case 'p':
7163 if (!strcmp ("private", p))
7164 result = PRAGMA_OMP_CLAUSE_PRIVATE;
7165 break;
7166 case 'r':
7167 if (!strcmp ("reduction", p))
7168 result = PRAGMA_OMP_CLAUSE_REDUCTION;
7169 break;
7170 case 's':
7171 if (!strcmp ("schedule", p))
7172 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
7173 else if (!strcmp ("shared", p))
7174 result = PRAGMA_OMP_CLAUSE_SHARED;
7175 break;
7176 case 'u':
7177 if (!strcmp ("untied", p))
7178 result = PRAGMA_OMP_CLAUSE_UNTIED;
7179 break;
7183 if (result != PRAGMA_OMP_CLAUSE_NONE)
7184 c_parser_consume_token (parser);
7186 return result;
7189 /* Validate that a clause of the given type does not already exist. */
7191 static void
7192 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
7193 const char *name)
7195 tree c;
7197 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
7198 if (OMP_CLAUSE_CODE (c) == code)
7200 location_t loc = OMP_CLAUSE_LOCATION (c);
7201 error_at (loc, "too many %qs clauses", name);
7202 break;
7206 /* OpenMP 2.5:
7207 variable-list:
7208 identifier
7209 variable-list , identifier
7211 If KIND is nonzero, create the appropriate node and install the
7212 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
7213 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
7215 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
7216 return the list created. */
7218 static tree
7219 c_parser_omp_variable_list (c_parser *parser,
7220 location_t clause_loc,
7221 enum omp_clause_code kind,
7222 tree list)
7224 if (c_parser_next_token_is_not (parser, CPP_NAME)
7225 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
7226 c_parser_error (parser, "expected identifier");
7228 while (c_parser_next_token_is (parser, CPP_NAME)
7229 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
7231 tree t = lookup_name (c_parser_peek_token (parser)->value);
7233 if (t == NULL_TREE)
7234 undeclared_variable (c_parser_peek_token (parser)->location,
7235 c_parser_peek_token (parser)->value);
7236 else if (t == error_mark_node)
7238 else if (kind != 0)
7240 tree u = build_omp_clause (clause_loc, kind);
7241 OMP_CLAUSE_DECL (u) = t;
7242 OMP_CLAUSE_CHAIN (u) = list;
7243 list = u;
7245 else
7246 list = tree_cons (t, NULL_TREE, list);
7248 c_parser_consume_token (parser);
7250 if (c_parser_next_token_is_not (parser, CPP_COMMA))
7251 break;
7253 c_parser_consume_token (parser);
7256 return list;
7259 /* Similarly, but expect leading and trailing parenthesis. This is a very
7260 common case for omp clauses. */
7262 static tree
7263 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
7264 tree list)
7266 /* The clauses location. */
7267 location_t loc = c_parser_peek_token (parser)->location;
7269 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7271 list = c_parser_omp_variable_list (parser, loc, kind, list);
7272 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7274 return list;
7277 /* OpenMP 3.0:
7278 collapse ( constant-expression ) */
7280 static tree
7281 c_parser_omp_clause_collapse (c_parser *parser, tree list)
7283 tree c, num = error_mark_node;
7284 HOST_WIDE_INT n;
7285 location_t loc;
7287 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
7289 loc = c_parser_peek_token (parser)->location;
7290 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7292 num = c_parser_expr_no_commas (parser, NULL).value;
7293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7295 if (num == error_mark_node)
7296 return list;
7297 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
7298 || !host_integerp (num, 0)
7299 || (n = tree_low_cst (num, 0)) <= 0
7300 || (int) n != n)
7302 error_at (loc,
7303 "collapse argument needs positive constant integer expression");
7304 return list;
7306 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
7307 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
7308 OMP_CLAUSE_CHAIN (c) = list;
7309 return c;
7312 /* OpenMP 2.5:
7313 copyin ( variable-list ) */
7315 static tree
7316 c_parser_omp_clause_copyin (c_parser *parser, tree list)
7318 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
7321 /* OpenMP 2.5:
7322 copyprivate ( variable-list ) */
7324 static tree
7325 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
7327 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
7330 /* OpenMP 2.5:
7331 default ( shared | none ) */
7333 static tree
7334 c_parser_omp_clause_default (c_parser *parser, tree list)
7336 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
7337 location_t loc = c_parser_peek_token (parser)->location;
7338 tree c;
7340 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7341 return list;
7342 if (c_parser_next_token_is (parser, CPP_NAME))
7344 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7346 switch (p[0])
7348 case 'n':
7349 if (strcmp ("none", p) != 0)
7350 goto invalid_kind;
7351 kind = OMP_CLAUSE_DEFAULT_NONE;
7352 break;
7354 case 's':
7355 if (strcmp ("shared", p) != 0)
7356 goto invalid_kind;
7357 kind = OMP_CLAUSE_DEFAULT_SHARED;
7358 break;
7360 default:
7361 goto invalid_kind;
7364 c_parser_consume_token (parser);
7366 else
7368 invalid_kind:
7369 c_parser_error (parser, "expected %<none%> or %<shared%>");
7371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7373 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
7374 return list;
7376 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
7377 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
7378 OMP_CLAUSE_CHAIN (c) = list;
7379 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
7381 return c;
7384 /* OpenMP 2.5:
7385 firstprivate ( variable-list ) */
7387 static tree
7388 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
7390 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
7393 /* OpenMP 2.5:
7394 if ( expression ) */
7396 static tree
7397 c_parser_omp_clause_if (c_parser *parser, tree list)
7399 location_t loc = c_parser_peek_token (parser)->location;
7400 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7402 tree t = c_parser_paren_condition (parser);
7403 tree c;
7405 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
7407 c = build_omp_clause (loc, OMP_CLAUSE_IF);
7408 OMP_CLAUSE_IF_EXPR (c) = t;
7409 OMP_CLAUSE_CHAIN (c) = list;
7410 list = c;
7412 else
7413 c_parser_error (parser, "expected %<(%>");
7415 return list;
7418 /* OpenMP 2.5:
7419 lastprivate ( variable-list ) */
7421 static tree
7422 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
7424 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
7427 /* OpenMP 2.5:
7428 nowait */
7430 static tree
7431 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7433 tree c;
7434 location_t loc = c_parser_peek_token (parser)->location;
7436 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
7438 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
7439 OMP_CLAUSE_CHAIN (c) = list;
7440 return c;
7443 /* OpenMP 2.5:
7444 num_threads ( expression ) */
7446 static tree
7447 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
7449 location_t num_threads_loc = c_parser_peek_token (parser)->location;
7450 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7452 location_t expr_loc = c_parser_peek_token (parser)->location;
7453 tree c, t = c_parser_expression (parser).value;
7454 t = c_fully_fold (t, false, NULL);
7456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7458 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
7460 c_parser_error (parser, "expected integer expression");
7461 return list;
7464 /* Attempt to statically determine when the number isn't positive. */
7465 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
7466 build_int_cst (TREE_TYPE (t), 0));
7467 if (CAN_HAVE_LOCATION_P (c))
7468 SET_EXPR_LOCATION (c, expr_loc);
7469 if (c == boolean_true_node)
7471 warning_at (expr_loc, 0,
7472 "%<num_threads%> value must be positive");
7473 t = integer_one_node;
7476 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
7478 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
7479 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
7480 OMP_CLAUSE_CHAIN (c) = list;
7481 list = c;
7484 return list;
7487 /* OpenMP 2.5:
7488 ordered */
7490 static tree
7491 c_parser_omp_clause_ordered (c_parser *parser, tree list)
7493 tree c;
7495 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
7497 c = build_omp_clause (c_parser_peek_token (parser)->location,
7498 OMP_CLAUSE_ORDERED);
7499 OMP_CLAUSE_CHAIN (c) = list;
7501 return c;
7504 /* OpenMP 2.5:
7505 private ( variable-list ) */
7507 static tree
7508 c_parser_omp_clause_private (c_parser *parser, tree list)
7510 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
7513 /* OpenMP 2.5:
7514 reduction ( reduction-operator : variable-list )
7516 reduction-operator:
7517 One of: + * - & ^ | && || */
7519 static tree
7520 c_parser_omp_clause_reduction (c_parser *parser, tree list)
7522 location_t clause_loc = c_parser_peek_token (parser)->location;
7523 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7525 enum tree_code code;
7527 switch (c_parser_peek_token (parser)->type)
7529 case CPP_PLUS:
7530 code = PLUS_EXPR;
7531 break;
7532 case CPP_MULT:
7533 code = MULT_EXPR;
7534 break;
7535 case CPP_MINUS:
7536 code = MINUS_EXPR;
7537 break;
7538 case CPP_AND:
7539 code = BIT_AND_EXPR;
7540 break;
7541 case CPP_XOR:
7542 code = BIT_XOR_EXPR;
7543 break;
7544 case CPP_OR:
7545 code = BIT_IOR_EXPR;
7546 break;
7547 case CPP_AND_AND:
7548 code = TRUTH_ANDIF_EXPR;
7549 break;
7550 case CPP_OR_OR:
7551 code = TRUTH_ORIF_EXPR;
7552 break;
7553 default:
7554 c_parser_error (parser,
7555 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7556 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7557 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7558 return list;
7560 c_parser_consume_token (parser);
7561 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7563 tree nl, c;
7565 nl = c_parser_omp_variable_list (parser, clause_loc,
7566 OMP_CLAUSE_REDUCTION, list);
7567 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7568 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7570 list = nl;
7572 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7574 return list;
7577 /* OpenMP 2.5:
7578 schedule ( schedule-kind )
7579 schedule ( schedule-kind , expression )
7581 schedule-kind:
7582 static | dynamic | guided | runtime | auto
7585 static tree
7586 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7588 tree c, t;
7589 location_t loc = c_parser_peek_token (parser)->location;
7591 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7592 return list;
7594 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
7596 if (c_parser_next_token_is (parser, CPP_NAME))
7598 tree kind = c_parser_peek_token (parser)->value;
7599 const char *p = IDENTIFIER_POINTER (kind);
7601 switch (p[0])
7603 case 'd':
7604 if (strcmp ("dynamic", p) != 0)
7605 goto invalid_kind;
7606 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7607 break;
7609 case 'g':
7610 if (strcmp ("guided", p) != 0)
7611 goto invalid_kind;
7612 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7613 break;
7615 case 'r':
7616 if (strcmp ("runtime", p) != 0)
7617 goto invalid_kind;
7618 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7619 break;
7621 default:
7622 goto invalid_kind;
7625 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7626 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7627 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
7628 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
7629 else
7630 goto invalid_kind;
7632 c_parser_consume_token (parser);
7633 if (c_parser_next_token_is (parser, CPP_COMMA))
7635 location_t here;
7636 c_parser_consume_token (parser);
7638 here = c_parser_peek_token (parser)->location;
7639 t = c_parser_expr_no_commas (parser, NULL).value;
7640 t = c_fully_fold (t, false, NULL);
7642 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7643 error_at (here, "schedule %<runtime%> does not take "
7644 "a %<chunk_size%> parameter");
7645 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
7646 error_at (here,
7647 "schedule %<auto%> does not take "
7648 "a %<chunk_size%> parameter");
7649 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7650 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7651 else
7652 c_parser_error (parser, "expected integer expression");
7654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7656 else
7657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7658 "expected %<,%> or %<)%>");
7660 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7661 OMP_CLAUSE_CHAIN (c) = list;
7662 return c;
7664 invalid_kind:
7665 c_parser_error (parser, "invalid schedule kind");
7666 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7667 return list;
7670 /* OpenMP 2.5:
7671 shared ( variable-list ) */
7673 static tree
7674 c_parser_omp_clause_shared (c_parser *parser, tree list)
7676 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7679 /* OpenMP 3.0:
7680 untied */
7682 static tree
7683 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
7685 tree c;
7687 /* FIXME: Should we allow duplicates? */
7688 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
7690 c = build_omp_clause (c_parser_peek_token (parser)->location,
7691 OMP_CLAUSE_UNTIED);
7692 OMP_CLAUSE_CHAIN (c) = list;
7694 return c;
7697 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7698 is a bitmask in MASK. Return the list of clauses found; the result
7699 of clause default goes in *pdefault. */
7701 static tree
7702 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7703 const char *where)
7705 tree clauses = NULL;
7706 bool first = true;
7708 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7710 location_t here;
7711 pragma_omp_clause c_kind;
7712 const char *c_name;
7713 tree prev = clauses;
7715 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
7716 c_parser_consume_token (parser);
7718 first = false;
7719 here = c_parser_peek_token (parser)->location;
7720 c_kind = c_parser_omp_clause_name (parser);
7722 switch (c_kind)
7724 case PRAGMA_OMP_CLAUSE_COLLAPSE:
7725 clauses = c_parser_omp_clause_collapse (parser, clauses);
7726 c_name = "collapse";
7727 break;
7728 case PRAGMA_OMP_CLAUSE_COPYIN:
7729 clauses = c_parser_omp_clause_copyin (parser, clauses);
7730 c_name = "copyin";
7731 break;
7732 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7733 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7734 c_name = "copyprivate";
7735 break;
7736 case PRAGMA_OMP_CLAUSE_DEFAULT:
7737 clauses = c_parser_omp_clause_default (parser, clauses);
7738 c_name = "default";
7739 break;
7740 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7741 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7742 c_name = "firstprivate";
7743 break;
7744 case PRAGMA_OMP_CLAUSE_IF:
7745 clauses = c_parser_omp_clause_if (parser, clauses);
7746 c_name = "if";
7747 break;
7748 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7749 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7750 c_name = "lastprivate";
7751 break;
7752 case PRAGMA_OMP_CLAUSE_NOWAIT:
7753 clauses = c_parser_omp_clause_nowait (parser, clauses);
7754 c_name = "nowait";
7755 break;
7756 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7757 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7758 c_name = "num_threads";
7759 break;
7760 case PRAGMA_OMP_CLAUSE_ORDERED:
7761 clauses = c_parser_omp_clause_ordered (parser, clauses);
7762 c_name = "ordered";
7763 break;
7764 case PRAGMA_OMP_CLAUSE_PRIVATE:
7765 clauses = c_parser_omp_clause_private (parser, clauses);
7766 c_name = "private";
7767 break;
7768 case PRAGMA_OMP_CLAUSE_REDUCTION:
7769 clauses = c_parser_omp_clause_reduction (parser, clauses);
7770 c_name = "reduction";
7771 break;
7772 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7773 clauses = c_parser_omp_clause_schedule (parser, clauses);
7774 c_name = "schedule";
7775 break;
7776 case PRAGMA_OMP_CLAUSE_SHARED:
7777 clauses = c_parser_omp_clause_shared (parser, clauses);
7778 c_name = "shared";
7779 break;
7780 case PRAGMA_OMP_CLAUSE_UNTIED:
7781 clauses = c_parser_omp_clause_untied (parser, clauses);
7782 c_name = "untied";
7783 break;
7784 default:
7785 c_parser_error (parser, "expected %<#pragma omp%> clause");
7786 goto saw_error;
7789 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7791 /* Remove the invalid clause(s) from the list to avoid
7792 confusing the rest of the compiler. */
7793 clauses = prev;
7794 error_at (here, "%qs is not valid for %qs", c_name, where);
7798 saw_error:
7799 c_parser_skip_to_pragma_eol (parser);
7801 return c_finish_omp_clauses (clauses);
7804 /* OpenMP 2.5:
7805 structured-block:
7806 statement
7808 In practice, we're also interested in adding the statement to an
7809 outer node. So it is convenient if we work around the fact that
7810 c_parser_statement calls add_stmt. */
7812 static tree
7813 c_parser_omp_structured_block (c_parser *parser)
7815 tree stmt = push_stmt_list ();
7816 c_parser_statement (parser);
7817 return pop_stmt_list (stmt);
7820 /* OpenMP 2.5:
7821 # pragma omp atomic new-line
7822 expression-stmt
7824 expression-stmt:
7825 x binop= expr | x++ | ++x | x-- | --x
7826 binop:
7827 +, *, -, /, &, ^, |, <<, >>
7829 where x is an lvalue expression with scalar type.
7831 LOC is the location of the #pragma token. */
7833 static void
7834 c_parser_omp_atomic (location_t loc, c_parser *parser)
7836 tree lhs, rhs;
7837 tree stmt;
7838 enum tree_code code;
7839 struct c_expr rhs_expr;
7841 c_parser_skip_to_pragma_eol (parser);
7843 lhs = c_parser_unary_expression (parser).value;
7844 lhs = c_fully_fold (lhs, false, NULL);
7845 switch (TREE_CODE (lhs))
7847 case ERROR_MARK:
7848 saw_error:
7849 c_parser_skip_to_end_of_block_or_statement (parser);
7850 return;
7852 case PREINCREMENT_EXPR:
7853 case POSTINCREMENT_EXPR:
7854 lhs = TREE_OPERAND (lhs, 0);
7855 code = PLUS_EXPR;
7856 rhs = integer_one_node;
7857 break;
7859 case PREDECREMENT_EXPR:
7860 case POSTDECREMENT_EXPR:
7861 lhs = TREE_OPERAND (lhs, 0);
7862 code = MINUS_EXPR;
7863 rhs = integer_one_node;
7864 break;
7866 default:
7867 switch (c_parser_peek_token (parser)->type)
7869 case CPP_MULT_EQ:
7870 code = MULT_EXPR;
7871 break;
7872 case CPP_DIV_EQ:
7873 code = TRUNC_DIV_EXPR;
7874 break;
7875 case CPP_PLUS_EQ:
7876 code = PLUS_EXPR;
7877 break;
7878 case CPP_MINUS_EQ:
7879 code = MINUS_EXPR;
7880 break;
7881 case CPP_LSHIFT_EQ:
7882 code = LSHIFT_EXPR;
7883 break;
7884 case CPP_RSHIFT_EQ:
7885 code = RSHIFT_EXPR;
7886 break;
7887 case CPP_AND_EQ:
7888 code = BIT_AND_EXPR;
7889 break;
7890 case CPP_OR_EQ:
7891 code = BIT_IOR_EXPR;
7892 break;
7893 case CPP_XOR_EQ:
7894 code = BIT_XOR_EXPR;
7895 break;
7896 default:
7897 c_parser_error (parser,
7898 "invalid operator for %<#pragma omp atomic%>");
7899 goto saw_error;
7902 c_parser_consume_token (parser);
7904 location_t rhs_loc = c_parser_peek_token (parser)->location;
7905 rhs_expr = c_parser_expression (parser);
7906 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
7908 rhs = rhs_expr.value;
7909 rhs = c_fully_fold (rhs, false, NULL);
7910 break;
7912 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
7913 if (stmt != error_mark_node)
7914 add_stmt (stmt);
7915 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7919 /* OpenMP 2.5:
7920 # pragma omp barrier new-line
7923 static void
7924 c_parser_omp_barrier (c_parser *parser)
7926 location_t loc = c_parser_peek_token (parser)->location;
7927 c_parser_consume_pragma (parser);
7928 c_parser_skip_to_pragma_eol (parser);
7930 c_finish_omp_barrier (loc);
7933 /* OpenMP 2.5:
7934 # pragma omp critical [(name)] new-line
7935 structured-block
7937 LOC is the location of the #pragma itself. */
7939 static tree
7940 c_parser_omp_critical (location_t loc, c_parser *parser)
7942 tree stmt, name = NULL;
7944 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7946 c_parser_consume_token (parser);
7947 if (c_parser_next_token_is (parser, CPP_NAME))
7949 name = c_parser_peek_token (parser)->value;
7950 c_parser_consume_token (parser);
7951 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7953 else
7954 c_parser_error (parser, "expected identifier");
7956 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7957 c_parser_error (parser, "expected %<(%> or end of line");
7958 c_parser_skip_to_pragma_eol (parser);
7960 stmt = c_parser_omp_structured_block (parser);
7961 return c_finish_omp_critical (loc, stmt, name);
7964 /* OpenMP 2.5:
7965 # pragma omp flush flush-vars[opt] new-line
7967 flush-vars:
7968 ( variable-list ) */
7970 static void
7971 c_parser_omp_flush (c_parser *parser)
7973 location_t loc = c_parser_peek_token (parser)->location;
7974 c_parser_consume_pragma (parser);
7975 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7976 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
7977 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7978 c_parser_error (parser, "expected %<(%> or end of line");
7979 c_parser_skip_to_pragma_eol (parser);
7981 c_finish_omp_flush (loc);
7984 /* Parse the restricted form of the for statement allowed by OpenMP.
7985 The real trick here is to determine the loop control variable early
7986 so that we can push a new decl if necessary to make it private.
7987 LOC is the location of the OMP in "#pragma omp". */
7989 static tree
7990 c_parser_omp_for_loop (location_t loc,
7991 c_parser *parser, tree clauses, tree *par_clauses)
7993 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
7994 tree declv, condv, incrv, initv, for_block = NULL, ret = NULL;
7995 bool fail = false, open_brace_parsed = false;
7996 int i, collapse = 1, nbraces = 0;
7997 location_t for_loc;
7999 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
8000 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
8001 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
8003 gcc_assert (collapse >= 1);
8005 declv = make_tree_vec (collapse);
8006 initv = make_tree_vec (collapse);
8007 condv = make_tree_vec (collapse);
8008 incrv = make_tree_vec (collapse);
8010 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
8012 c_parser_error (parser, "for statement expected");
8013 return NULL;
8015 for_loc = c_parser_peek_token (parser)->location;
8016 c_parser_consume_token (parser);
8018 for (i = 0; i < collapse; i++)
8020 int bracecount = 0;
8022 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8023 goto pop_scopes;
8025 /* Parse the initialization declaration or expression. */
8026 if (c_parser_next_token_starts_declspecs (parser))
8028 if (i > 0)
8029 for_block
8030 = tree_cons (NULL, c_begin_compound_stmt (true), for_block);
8031 c_parser_declaration_or_fndef (parser, true, true, true, true);
8032 decl = check_for_loop_decls (for_loc);
8033 if (decl == NULL)
8034 goto error_init;
8035 if (DECL_INITIAL (decl) == error_mark_node)
8036 decl = error_mark_node;
8037 init = decl;
8039 else if (c_parser_next_token_is (parser, CPP_NAME)
8040 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
8042 struct c_expr decl_exp;
8043 struct c_expr init_exp;
8044 location_t init_loc;
8046 decl_exp = c_parser_postfix_expression (parser);
8047 decl = decl_exp.value;
8049 c_parser_require (parser, CPP_EQ, "expected %<=%>");
8051 init_loc = c_parser_peek_token (parser)->location;
8052 init_exp = c_parser_expr_no_commas (parser, NULL);
8053 init_exp = default_function_array_read_conversion (init_loc,
8054 init_exp);
8055 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
8056 NOP_EXPR, init_loc, init_exp.value,
8057 init_exp.original_type);
8058 init = c_process_expr_stmt (init_loc, init);
8060 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8062 else
8064 error_init:
8065 c_parser_error (parser,
8066 "expected iteration declaration or initialization");
8067 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8068 "expected %<)%>");
8069 fail = true;
8070 goto parse_next;
8073 /* Parse the loop condition. */
8074 cond = NULL_TREE;
8075 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
8077 location_t cond_loc = c_parser_peek_token (parser)->location;
8078 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
8080 cond = cond_expr.value;
8081 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
8082 cond = c_fully_fold (cond, false, NULL);
8083 switch (cond_expr.original_code)
8085 case GT_EXPR:
8086 case GE_EXPR:
8087 case LT_EXPR:
8088 case LE_EXPR:
8089 break;
8090 default:
8091 /* Can't be cond = error_mark_node, because we want to preserve
8092 the location until c_finish_omp_for. */
8093 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
8094 break;
8096 protected_set_expr_location (cond, cond_loc);
8098 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8100 /* Parse the increment expression. */
8101 incr = NULL_TREE;
8102 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
8104 location_t incr_loc = c_parser_peek_token (parser)->location;
8106 incr = c_process_expr_stmt (incr_loc,
8107 c_parser_expression (parser).value);
8109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8111 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
8112 fail = true;
8113 else
8115 TREE_VEC_ELT (declv, i) = decl;
8116 TREE_VEC_ELT (initv, i) = init;
8117 TREE_VEC_ELT (condv, i) = cond;
8118 TREE_VEC_ELT (incrv, i) = incr;
8121 parse_next:
8122 if (i == collapse - 1)
8123 break;
8125 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
8126 in between the collapsed for loops to be still considered perfectly
8127 nested. Hopefully the final version clarifies this.
8128 For now handle (multiple) {'s and empty statements. */
8131 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8133 c_parser_consume_token (parser);
8134 break;
8136 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8138 c_parser_consume_token (parser);
8139 bracecount++;
8141 else if (bracecount
8142 && c_parser_next_token_is (parser, CPP_SEMICOLON))
8143 c_parser_consume_token (parser);
8144 else
8146 c_parser_error (parser, "not enough perfectly nested loops");
8147 if (bracecount)
8149 open_brace_parsed = true;
8150 bracecount--;
8152 fail = true;
8153 collapse = 0;
8154 break;
8157 while (1);
8159 nbraces += bracecount;
8162 save_break = c_break_label;
8163 c_break_label = size_one_node;
8164 save_cont = c_cont_label;
8165 c_cont_label = NULL_TREE;
8166 body = push_stmt_list ();
8168 if (open_brace_parsed)
8170 location_t here = c_parser_peek_token (parser)->location;
8171 stmt = c_begin_compound_stmt (true);
8172 c_parser_compound_statement_nostart (parser);
8173 add_stmt (c_end_compound_stmt (here, stmt, true));
8175 else
8176 add_stmt (c_parser_c99_block_statement (parser));
8177 if (c_cont_label)
8179 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
8180 SET_EXPR_LOCATION (t, loc);
8181 add_stmt (t);
8184 body = pop_stmt_list (body);
8185 c_break_label = save_break;
8186 c_cont_label = save_cont;
8188 while (nbraces)
8190 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8192 c_parser_consume_token (parser);
8193 nbraces--;
8195 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8196 c_parser_consume_token (parser);
8197 else
8199 c_parser_error (parser, "collapsed loops not perfectly nested");
8200 while (nbraces)
8202 location_t here = c_parser_peek_token (parser)->location;
8203 stmt = c_begin_compound_stmt (true);
8204 add_stmt (body);
8205 c_parser_compound_statement_nostart (parser);
8206 body = c_end_compound_stmt (here, stmt, true);
8207 nbraces--;
8209 goto pop_scopes;
8213 /* Only bother calling c_finish_omp_for if we haven't already generated
8214 an error from the initialization parsing. */
8215 if (!fail)
8217 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
8218 if (stmt)
8220 if (par_clauses != NULL)
8222 tree *c;
8223 for (c = par_clauses; *c ; )
8224 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
8225 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
8226 c = &OMP_CLAUSE_CHAIN (*c);
8227 else
8229 for (i = 0; i < collapse; i++)
8230 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
8231 break;
8232 if (i == collapse)
8233 c = &OMP_CLAUSE_CHAIN (*c);
8234 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
8236 error_at (loc,
8237 "iteration variable %qD should not be firstprivate",
8238 OMP_CLAUSE_DECL (*c));
8239 *c = OMP_CLAUSE_CHAIN (*c);
8241 else
8243 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
8244 change it to shared (decl) in
8245 OMP_PARALLEL_CLAUSES. */
8246 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
8247 OMP_CLAUSE_LASTPRIVATE);
8248 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
8249 OMP_CLAUSE_CHAIN (l) = clauses;
8250 clauses = l;
8251 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
8255 OMP_FOR_CLAUSES (stmt) = clauses;
8257 ret = stmt;
8259 pop_scopes:
8260 while (for_block)
8262 /* FIXME diagnostics: LOC below should be the actual location of
8263 this particular for block. We need to build a list of
8264 locations to go along with FOR_BLOCK. */
8265 stmt = c_end_compound_stmt (loc, TREE_VALUE (for_block), true);
8266 add_stmt (stmt);
8267 for_block = TREE_CHAIN (for_block);
8269 return ret;
8272 /* OpenMP 2.5:
8273 #pragma omp for for-clause[optseq] new-line
8274 for-loop
8276 LOC is the location of the #pragma token.
8279 #define OMP_FOR_CLAUSE_MASK \
8280 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8281 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8282 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8283 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8284 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
8285 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
8286 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
8287 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8289 static tree
8290 c_parser_omp_for (location_t loc, c_parser *parser)
8292 tree block, clauses, ret;
8294 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
8295 "#pragma omp for");
8297 block = c_begin_compound_stmt (true);
8298 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
8299 block = c_end_compound_stmt (loc, block, true);
8300 add_stmt (block);
8302 return ret;
8305 /* OpenMP 2.5:
8306 # pragma omp master new-line
8307 structured-block
8309 LOC is the location of the #pragma token.
8312 static tree
8313 c_parser_omp_master (location_t loc, c_parser *parser)
8315 c_parser_skip_to_pragma_eol (parser);
8316 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
8319 /* OpenMP 2.5:
8320 # pragma omp ordered new-line
8321 structured-block
8323 LOC is the location of the #pragma itself.
8326 static tree
8327 c_parser_omp_ordered (location_t loc, c_parser *parser)
8329 c_parser_skip_to_pragma_eol (parser);
8330 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
8333 /* OpenMP 2.5:
8335 section-scope:
8336 { section-sequence }
8338 section-sequence:
8339 section-directive[opt] structured-block
8340 section-sequence section-directive structured-block
8342 SECTIONS_LOC is the location of the #pragma omp sections. */
8344 static tree
8345 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
8347 tree stmt, substmt;
8348 bool error_suppress = false;
8349 location_t loc;
8351 loc = c_parser_peek_token (parser)->location;
8352 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8354 /* Avoid skipping until the end of the block. */
8355 parser->error = false;
8356 return NULL_TREE;
8359 stmt = push_stmt_list ();
8361 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
8363 substmt = push_stmt_list ();
8365 while (1)
8367 c_parser_statement (parser);
8369 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8370 break;
8371 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8372 break;
8373 if (c_parser_next_token_is (parser, CPP_EOF))
8374 break;
8377 substmt = pop_stmt_list (substmt);
8378 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8379 SET_EXPR_LOCATION (substmt, loc);
8380 add_stmt (substmt);
8383 while (1)
8385 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8386 break;
8387 if (c_parser_next_token_is (parser, CPP_EOF))
8388 break;
8390 loc = c_parser_peek_token (parser)->location;
8391 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
8393 c_parser_consume_pragma (parser);
8394 c_parser_skip_to_pragma_eol (parser);
8395 error_suppress = false;
8397 else if (!error_suppress)
8399 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
8400 error_suppress = true;
8403 substmt = c_parser_omp_structured_block (parser);
8404 substmt = build1 (OMP_SECTION, void_type_node, substmt);
8405 SET_EXPR_LOCATION (substmt, loc);
8406 add_stmt (substmt);
8408 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
8409 "expected %<#pragma omp section%> or %<}%>");
8411 substmt = pop_stmt_list (stmt);
8413 stmt = make_node (OMP_SECTIONS);
8414 SET_EXPR_LOCATION (stmt, sections_loc);
8415 TREE_TYPE (stmt) = void_type_node;
8416 OMP_SECTIONS_BODY (stmt) = substmt;
8418 return add_stmt (stmt);
8421 /* OpenMP 2.5:
8422 # pragma omp sections sections-clause[optseq] newline
8423 sections-scope
8425 LOC is the location of the #pragma token.
8428 #define OMP_SECTIONS_CLAUSE_MASK \
8429 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8430 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8431 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
8432 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8433 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8435 static tree
8436 c_parser_omp_sections (location_t loc, c_parser *parser)
8438 tree block, clauses, ret;
8440 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
8441 "#pragma omp sections");
8443 block = c_begin_compound_stmt (true);
8444 ret = c_parser_omp_sections_scope (loc, parser);
8445 if (ret)
8446 OMP_SECTIONS_CLAUSES (ret) = clauses;
8447 block = c_end_compound_stmt (loc, block, true);
8448 add_stmt (block);
8450 return ret;
8453 /* OpenMP 2.5:
8454 # pragma parallel parallel-clause new-line
8455 # pragma parallel for parallel-for-clause new-line
8456 # pragma parallel sections parallel-sections-clause new-line
8458 LOC is the location of the #pragma token.
8461 #define OMP_PARALLEL_CLAUSE_MASK \
8462 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8463 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8464 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8465 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8466 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
8467 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
8468 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
8469 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
8471 static tree
8472 c_parser_omp_parallel (location_t loc, c_parser *parser)
8474 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
8475 const char *p_name = "#pragma omp parallel";
8476 tree stmt, clauses, par_clause, ws_clause, block;
8477 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
8479 if (c_parser_next_token_is_keyword (parser, RID_FOR))
8481 c_parser_consume_token (parser);
8482 p_kind = PRAGMA_OMP_PARALLEL_FOR;
8483 p_name = "#pragma omp parallel for";
8484 mask |= OMP_FOR_CLAUSE_MASK;
8485 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8487 else if (c_parser_next_token_is (parser, CPP_NAME))
8489 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8490 if (strcmp (p, "sections") == 0)
8492 c_parser_consume_token (parser);
8493 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
8494 p_name = "#pragma omp parallel sections";
8495 mask |= OMP_SECTIONS_CLAUSE_MASK;
8496 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
8500 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
8502 switch (p_kind)
8504 case PRAGMA_OMP_PARALLEL:
8505 block = c_begin_omp_parallel ();
8506 c_parser_statement (parser);
8507 stmt = c_finish_omp_parallel (loc, clauses, block);
8508 break;
8510 case PRAGMA_OMP_PARALLEL_FOR:
8511 block = c_begin_omp_parallel ();
8512 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8513 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
8514 stmt = c_finish_omp_parallel (loc, par_clause, block);
8515 OMP_PARALLEL_COMBINED (stmt) = 1;
8516 break;
8518 case PRAGMA_OMP_PARALLEL_SECTIONS:
8519 block = c_begin_omp_parallel ();
8520 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8521 stmt = c_parser_omp_sections_scope (loc, parser);
8522 if (stmt)
8523 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
8524 stmt = c_finish_omp_parallel (loc, par_clause, block);
8525 OMP_PARALLEL_COMBINED (stmt) = 1;
8526 break;
8528 default:
8529 gcc_unreachable ();
8532 return stmt;
8535 /* OpenMP 2.5:
8536 # pragma omp single single-clause[optseq] new-line
8537 structured-block
8539 LOC is the location of the #pragma.
8542 #define OMP_SINGLE_CLAUSE_MASK \
8543 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8544 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8545 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
8546 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
8548 static tree
8549 c_parser_omp_single (location_t loc, c_parser *parser)
8551 tree stmt = make_node (OMP_SINGLE);
8552 SET_EXPR_LOCATION (stmt, loc);
8553 TREE_TYPE (stmt) = void_type_node;
8555 OMP_SINGLE_CLAUSES (stmt)
8556 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
8557 "#pragma omp single");
8558 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
8560 return add_stmt (stmt);
8563 /* OpenMP 3.0:
8564 # pragma omp task task-clause[optseq] new-line
8566 LOC is the location of the #pragma.
8569 #define OMP_TASK_CLAUSE_MASK \
8570 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
8571 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
8572 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
8573 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
8574 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
8575 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
8577 static tree
8578 c_parser_omp_task (location_t loc, c_parser *parser)
8580 tree clauses, block;
8582 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
8583 "#pragma omp task");
8585 block = c_begin_omp_task ();
8586 c_parser_statement (parser);
8587 return c_finish_omp_task (loc, clauses, block);
8590 /* OpenMP 3.0:
8591 # pragma omp taskwait new-line
8594 static void
8595 c_parser_omp_taskwait (c_parser *parser)
8597 location_t loc = c_parser_peek_token (parser)->location;
8598 c_parser_consume_pragma (parser);
8599 c_parser_skip_to_pragma_eol (parser);
8601 c_finish_omp_taskwait (loc);
8604 /* Main entry point to parsing most OpenMP pragmas. */
8606 static void
8607 c_parser_omp_construct (c_parser *parser)
8609 enum pragma_kind p_kind;
8610 location_t loc;
8611 tree stmt;
8613 loc = c_parser_peek_token (parser)->location;
8614 p_kind = c_parser_peek_token (parser)->pragma_kind;
8615 c_parser_consume_pragma (parser);
8617 switch (p_kind)
8619 case PRAGMA_OMP_ATOMIC:
8620 c_parser_omp_atomic (loc, parser);
8621 return;
8622 case PRAGMA_OMP_CRITICAL:
8623 stmt = c_parser_omp_critical (loc, parser);
8624 break;
8625 case PRAGMA_OMP_FOR:
8626 stmt = c_parser_omp_for (loc, parser);
8627 break;
8628 case PRAGMA_OMP_MASTER:
8629 stmt = c_parser_omp_master (loc, parser);
8630 break;
8631 case PRAGMA_OMP_ORDERED:
8632 stmt = c_parser_omp_ordered (loc, parser);
8633 break;
8634 case PRAGMA_OMP_PARALLEL:
8635 stmt = c_parser_omp_parallel (loc, parser);
8636 break;
8637 case PRAGMA_OMP_SECTIONS:
8638 stmt = c_parser_omp_sections (loc, parser);
8639 break;
8640 case PRAGMA_OMP_SINGLE:
8641 stmt = c_parser_omp_single (loc, parser);
8642 break;
8643 case PRAGMA_OMP_TASK:
8644 stmt = c_parser_omp_task (loc, parser);
8645 break;
8646 default:
8647 gcc_unreachable ();
8650 if (stmt)
8651 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
8655 /* OpenMP 2.5:
8656 # pragma omp threadprivate (variable-list) */
8658 static void
8659 c_parser_omp_threadprivate (c_parser *parser)
8661 tree vars, t;
8662 location_t loc;
8664 c_parser_consume_pragma (parser);
8665 loc = c_parser_peek_token (parser)->location;
8666 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
8668 /* Mark every variable in VARS to be assigned thread local storage. */
8669 for (t = vars; t; t = TREE_CHAIN (t))
8671 tree v = TREE_PURPOSE (t);
8673 /* FIXME diagnostics: Ideally we should keep individual
8674 locations for all the variables in the var list to make the
8675 following errors more precise. Perhaps
8676 c_parser_omp_var_list_parens() should construct a list of
8677 locations to go along with the var list. */
8679 /* If V had already been marked threadprivate, it doesn't matter
8680 whether it had been used prior to this point. */
8681 if (TREE_CODE (v) != VAR_DECL)
8682 error_at (loc, "%qD is not a variable", v);
8683 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
8684 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
8685 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
8686 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
8687 else if (TREE_TYPE (v) == error_mark_node)
8689 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
8690 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
8691 else
8693 if (! DECL_THREAD_LOCAL_P (v))
8695 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
8696 /* If rtl has been already set for this var, call
8697 make_decl_rtl once again, so that encode_section_info
8698 has a chance to look at the new decl flags. */
8699 if (DECL_RTL_SET_P (v))
8700 make_decl_rtl (v);
8702 C_DECL_THREADPRIVATE_P (v) = 1;
8706 c_parser_skip_to_pragma_eol (parser);
8710 /* Parse a single source file. */
8712 void
8713 c_parse_file (void)
8715 /* Use local storage to begin. If the first token is a pragma, parse it.
8716 If it is #pragma GCC pch_preprocess, then this will load a PCH file
8717 which will cause garbage collection. */
8718 c_parser tparser;
8720 memset (&tparser, 0, sizeof tparser);
8721 the_parser = &tparser;
8723 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
8724 c_parser_pragma_pch_preprocess (&tparser);
8726 the_parser = GGC_NEW (c_parser);
8727 *the_parser = tparser;
8729 /* Initialize EH, if we've been told to do so. */
8730 if (flag_exceptions)
8731 using_eh_for_cleanups ();
8733 c_parser_translation_unit (the_parser);
8734 the_parser = NULL;
8737 #include "gt-c-parser.h"