gcc:
[official-gcc.git] / gcc / c-parser.c
blob261ea3da56e94d72b0ebd8f9fb6da9fc1d5ab2b5
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h" /* For rtl.h: needs enum reg_class. */
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-family/c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "ggc.h"
54 #include "c-family/c-common.h"
55 #include "c-family/c-objc.h"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
59 #include "plugin.h"
62 /* Initialization routine for this file. */
64 void
65 c_parse_init (void)
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
71 int mask = 0;
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
77 mask |= D_CXXONLY;
78 if (!flag_isoc99)
79 mask |= D_C99;
80 if (flag_no_asm)
82 mask |= D_ASM | D_EXT;
83 if (!flag_isoc99)
84 mask |= D_EXT89;
86 if (!c_dialect_objc ())
87 mask |= D_OBJC | D_CXX_OBJC;
89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90 for (i = 0; i < num_c_common_reswords; i++)
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
94 if (c_common_reswords[i].disable & mask)
96 if (warn_cxx_compat
97 && (c_common_reswords[i].disable & D_CXXWARN))
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
103 continue;
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
108 C_IS_RESERVED_WORD (id) = 1;
109 ridpointers [(int) c_common_reswords[i].rid] = id;
113 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
124 /* The following local token type is used. */
126 /* A keyword. */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
129 /* More information about the type of a CPP_NAME token. */
130 typedef enum c_id_kind {
131 /* An ordinary identifier. */
132 C_ID_ID,
133 /* An identifier declared as a typedef name. */
134 C_ID_TYPENAME,
135 /* An identifier declared as an Objective-C class name. */
136 C_ID_CLASSNAME,
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
139 /* Not an identifier. */
140 C_ID_NONE
141 } c_id_kind;
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
158 location_t location;
159 /* The value associated with this token, if any. */
160 tree value;
161 } c_token;
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 typedef struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
168 c_token tokens[2];
169 /* How many look-ahead tokens are available (0, 1 or 2). */
170 short tokens_avail;
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
183 /* Objective-C specific parser/lexer information. */
185 /* True if we are in a context where the Objective-C "PQ" keywords
186 are considered keywords. */
187 BOOL_BITFIELD objc_pq_context : 1;
188 /* True if we are parsing a (potential) Objective-C foreach
189 statement. This is set to true after we parsed 'for (' and while
190 we wait for 'in' or ';' to decide if it's a standard C for loop or an
191 Objective-C foreach loop. */
192 BOOL_BITFIELD objc_could_be_foreach_context : 1;
193 /* The following flag is needed to contextualize Objective-C lexical
194 analysis. In some cases (e.g., 'int NSObject;'), it is
195 undesirable to bind an identifier to an Objective-C class, even
196 if a class with that name exists. */
197 BOOL_BITFIELD objc_need_raw_identifier : 1;
198 /* True if we are in a context where the Objective-C "Property attribute"
199 keywords are valid. */
200 BOOL_BITFIELD objc_property_attr_context : 1;
201 } c_parser;
204 /* The actual parser and external interface. ??? Does this need to be
205 garbage-collected? */
207 static GTY (()) c_parser *the_parser;
209 /* Read in and lex a single token, storing it in *TOKEN. */
211 static void
212 c_lex_one_token (c_parser *parser, c_token *token)
214 timevar_push (TV_LEX);
216 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
217 (parser->lex_untranslated_string
218 ? C_LEX_STRING_NO_TRANSLATE : 0));
219 token->id_kind = C_ID_NONE;
220 token->keyword = RID_MAX;
221 token->pragma_kind = PRAGMA_NONE;
223 switch (token->type)
225 case CPP_NAME:
227 tree decl;
229 bool objc_force_identifier = parser->objc_need_raw_identifier;
230 if (c_dialect_objc ())
231 parser->objc_need_raw_identifier = false;
233 if (C_IS_RESERVED_WORD (token->value))
235 enum rid rid_code = C_RID_CODE (token->value);
237 if (rid_code == RID_CXX_COMPAT_WARN)
239 warning_at (token->location,
240 OPT_Wc___compat,
241 "identifier %qE conflicts with C++ keyword",
242 token->value);
244 else if (rid_code >= RID_FIRST_ADDR_SPACE
245 && rid_code <= RID_LAST_ADDR_SPACE)
247 token->id_kind = C_ID_ADDRSPACE;
248 token->keyword = rid_code;
249 break;
251 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
253 /* We found an Objective-C "pq" keyword (in, out,
254 inout, bycopy, byref, oneway). They need special
255 care because the interpretation depends on the
256 context.
258 if (parser->objc_pq_context)
260 token->type = CPP_KEYWORD;
261 token->keyword = rid_code;
262 break;
264 else if (parser->objc_could_be_foreach_context
265 && rid_code == RID_IN)
267 /* We are in Objective-C, inside a (potential)
268 foreach context (which means after having
269 parsed 'for (', but before having parsed ';'),
270 and we found 'in'. We consider it the keyword
271 which terminates the declaration at the
272 beginning of a foreach-statement. Note that
273 this means you can't use 'in' for anything else
274 in that context; in particular, in Objective-C
275 you can't use 'in' as the name of the running
276 variable in a C for loop. We could potentially
277 try to add code here to disambiguate, but it
278 seems a reasonable limitation.
280 token->type = CPP_KEYWORD;
281 token->keyword = rid_code;
282 break;
284 /* Else, "pq" keywords outside of the "pq" context are
285 not keywords, and we fall through to the code for
286 normal tokens.
289 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
291 /* We found an Objective-C "property attribute" keyword
292 (readonly, copies, getter, setter, ivar). These are
293 only valid in the property context. */
294 if (parser->objc_property_attr_context)
296 token->type = CPP_KEYWORD;
297 token->keyword = rid_code;
298 break;
300 /* Else they are not special keywords.
303 else if (c_dialect_objc ()
304 && (OBJC_IS_AT_KEYWORD (rid_code)
305 || OBJC_IS_CXX_KEYWORD (rid_code)))
307 /* We found one of the Objective-C "@" keywords (defs,
308 selector, synchronized, etc) or one of the
309 Objective-C "cxx" keywords (class, private,
310 protected, public, try, catch, throw) without a
311 preceding '@' sign. Do nothing and fall through to
312 the code for normal tokens (in C++ we would still
313 consider the CXX ones keywords, but not in C).
317 else
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
325 decl = lookup_name (token->value);
326 if (decl)
328 if (TREE_CODE (decl) == TYPE_DECL)
330 token->id_kind = C_ID_TYPENAME;
331 break;
334 else if (c_dialect_objc ())
336 tree objc_interface_decl = objc_is_class_name (token->value);
337 /* Objective-C class names are in the same namespace as
338 variables and typedefs, and hence are shadowed by local
339 declarations. */
340 if (objc_interface_decl
341 && (global_bindings_p ()
342 || (!objc_force_identifier && !decl)))
344 token->value = objc_interface_decl;
345 token->id_kind = C_ID_CLASSNAME;
346 break;
349 token->id_kind = C_ID_ID;
351 break;
352 case CPP_AT_NAME:
353 /* This only happens in Objective-C; it must be a keyword. */
354 token->type = CPP_KEYWORD;
355 switch (C_RID_CODE (token->value))
357 /* Replace 'class' with '@class', 'private' with '@private',
358 etc. This prevents confusion with the C++ keyword
359 'class', and makes the tokens consistent with other
360 Objective-C 'AT' keywords. For example '@class' is
361 reported as RID_AT_CLASS which is consistent with
362 '@synchronized', which is reported as
363 RID_AT_SYNCHRONIZED.
365 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
366 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
367 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
368 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
369 case RID_THROW: token->keyword = RID_AT_THROW; break;
370 case RID_TRY: token->keyword = RID_AT_TRY; break;
371 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
372 default: token->keyword = C_RID_CODE (token->value);
374 break;
375 case CPP_COLON:
376 case CPP_COMMA:
377 case CPP_CLOSE_PAREN:
378 case CPP_SEMICOLON:
379 /* These tokens may affect the interpretation of any identifiers
380 following, if doing Objective-C. */
381 if (c_dialect_objc ())
382 parser->objc_need_raw_identifier = false;
383 break;
384 case CPP_PRAGMA:
385 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
386 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
387 token->value = NULL;
388 break;
389 default:
390 break;
392 timevar_pop (TV_LEX);
395 /* Return a pointer to the next token from PARSER, reading it in if
396 necessary. */
398 static inline c_token *
399 c_parser_peek_token (c_parser *parser)
401 if (parser->tokens_avail == 0)
403 c_lex_one_token (parser, &parser->tokens[0]);
404 parser->tokens_avail = 1;
406 return &parser->tokens[0];
409 /* Return true if the next token from PARSER has the indicated
410 TYPE. */
412 static inline bool
413 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
415 return c_parser_peek_token (parser)->type == type;
418 /* Return true if the next token from PARSER does not have the
419 indicated TYPE. */
421 static inline bool
422 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
424 return !c_parser_next_token_is (parser, type);
427 /* Return true if the next token from PARSER is the indicated
428 KEYWORD. */
430 static inline bool
431 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
433 return c_parser_peek_token (parser)->keyword == keyword;
436 /* Return a pointer to the next-but-one token from PARSER, reading it
437 in if necessary. The next token is already read in. */
439 static c_token *
440 c_parser_peek_2nd_token (c_parser *parser)
442 if (parser->tokens_avail >= 2)
443 return &parser->tokens[1];
444 gcc_assert (parser->tokens_avail == 1);
445 gcc_assert (parser->tokens[0].type != CPP_EOF);
446 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
447 c_lex_one_token (parser, &parser->tokens[1]);
448 parser->tokens_avail = 2;
449 return &parser->tokens[1];
452 /* Return true if TOKEN can start a type name,
453 false otherwise. */
454 static bool
455 c_token_starts_typename (c_token *token)
457 switch (token->type)
459 case CPP_NAME:
460 switch (token->id_kind)
462 case C_ID_ID:
463 return false;
464 case C_ID_ADDRSPACE:
465 return true;
466 case C_ID_TYPENAME:
467 return true;
468 case C_ID_CLASSNAME:
469 gcc_assert (c_dialect_objc ());
470 return true;
471 default:
472 gcc_unreachable ();
474 case CPP_KEYWORD:
475 switch (token->keyword)
477 case RID_UNSIGNED:
478 case RID_LONG:
479 case RID_INT128:
480 case RID_SHORT:
481 case RID_SIGNED:
482 case RID_COMPLEX:
483 case RID_INT:
484 case RID_CHAR:
485 case RID_FLOAT:
486 case RID_DOUBLE:
487 case RID_VOID:
488 case RID_DFLOAT32:
489 case RID_DFLOAT64:
490 case RID_DFLOAT128:
491 case RID_BOOL:
492 case RID_ENUM:
493 case RID_STRUCT:
494 case RID_UNION:
495 case RID_TYPEOF:
496 case RID_CONST:
497 case RID_VOLATILE:
498 case RID_RESTRICT:
499 case RID_ATTRIBUTE:
500 case RID_FRACT:
501 case RID_ACCUM:
502 case RID_SAT:
503 return true;
504 default:
505 return false;
507 case CPP_LESS:
508 if (c_dialect_objc ())
509 return true;
510 return false;
511 default:
512 return false;
516 enum c_lookahead_kind {
517 /* Always treat unknown identifiers as typenames. */
518 cla_prefer_type,
520 /* Could be parsing a nonabstract declarator. Only treat an identifier
521 as a typename if followed by another identifier or a star. */
522 cla_nonabstract_decl,
524 /* Never treat identifiers as typenames. */
525 cla_prefer_id
528 /* Return true if the next token from PARSER can start a type name,
529 false otherwise. LA specifies how to do lookahead in order to
530 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
532 static inline bool
533 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
535 c_token *token = c_parser_peek_token (parser);
536 if (c_token_starts_typename (token))
537 return true;
539 /* Try a bit harder to detect an unknown typename. */
540 if (la != cla_prefer_id
541 && token->type == CPP_NAME
542 && token->id_kind == C_ID_ID
544 /* Do not try too hard when we could have "object in array". */
545 && !parser->objc_could_be_foreach_context
547 && (la == cla_prefer_type
548 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
549 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
551 /* Only unknown identifiers. */
552 && !lookup_name (token->value))
553 return true;
555 return false;
558 /* Return true if TOKEN is a type qualifier, false otherwise. */
559 static bool
560 c_token_is_qualifier (c_token *token)
562 switch (token->type)
564 case CPP_NAME:
565 switch (token->id_kind)
567 case C_ID_ADDRSPACE:
568 return true;
569 default:
570 return false;
572 case CPP_KEYWORD:
573 switch (token->keyword)
575 case RID_CONST:
576 case RID_VOLATILE:
577 case RID_RESTRICT:
578 case RID_ATTRIBUTE:
579 return true;
580 default:
581 return false;
583 case CPP_LESS:
584 return false;
585 default:
586 gcc_unreachable ();
590 /* Return true if the next token from PARSER is a type qualifier,
591 false otherwise. */
592 static inline bool
593 c_parser_next_token_is_qualifier (c_parser *parser)
595 c_token *token = c_parser_peek_token (parser);
596 return c_token_is_qualifier (token);
599 /* Return true if TOKEN can start declaration specifiers, false
600 otherwise. */
601 static bool
602 c_token_starts_declspecs (c_token *token)
604 switch (token->type)
606 case CPP_NAME:
607 switch (token->id_kind)
609 case C_ID_ID:
610 return false;
611 case C_ID_ADDRSPACE:
612 return true;
613 case C_ID_TYPENAME:
614 return true;
615 case C_ID_CLASSNAME:
616 gcc_assert (c_dialect_objc ());
617 return true;
618 default:
619 gcc_unreachable ();
621 case CPP_KEYWORD:
622 switch (token->keyword)
624 case RID_STATIC:
625 case RID_EXTERN:
626 case RID_REGISTER:
627 case RID_TYPEDEF:
628 case RID_INLINE:
629 case RID_AUTO:
630 case RID_THREAD:
631 case RID_UNSIGNED:
632 case RID_LONG:
633 case RID_INT128:
634 case RID_SHORT:
635 case RID_SIGNED:
636 case RID_COMPLEX:
637 case RID_INT:
638 case RID_CHAR:
639 case RID_FLOAT:
640 case RID_DOUBLE:
641 case RID_VOID:
642 case RID_DFLOAT32:
643 case RID_DFLOAT64:
644 case RID_DFLOAT128:
645 case RID_BOOL:
646 case RID_ENUM:
647 case RID_STRUCT:
648 case RID_UNION:
649 case RID_TYPEOF:
650 case RID_CONST:
651 case RID_VOLATILE:
652 case RID_RESTRICT:
653 case RID_ATTRIBUTE:
654 case RID_FRACT:
655 case RID_ACCUM:
656 case RID_SAT:
657 return true;
658 default:
659 return false;
661 case CPP_LESS:
662 if (c_dialect_objc ())
663 return true;
664 return false;
665 default:
666 return false;
671 /* Return true if TOKEN can start declaration specifiers or a static
672 assertion, false otherwise. */
673 static bool
674 c_token_starts_declaration (c_token *token)
676 if (c_token_starts_declspecs (token)
677 || token->keyword == RID_STATIC_ASSERT)
678 return true;
679 else
680 return false;
683 /* Return true if the next token from PARSER can start declaration
684 specifiers, false otherwise. */
685 static inline bool
686 c_parser_next_token_starts_declspecs (c_parser *parser)
688 c_token *token = c_parser_peek_token (parser);
690 /* In Objective-C, a classname normally starts a declspecs unless it
691 is immediately followed by a dot. In that case, it is the
692 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
693 setter/getter on the class. c_token_starts_declspecs() can't
694 differentiate between the two cases because it only checks the
695 current token, so we have a special check here. */
696 if (c_dialect_objc ()
697 && token->type == CPP_NAME
698 && token->id_kind == C_ID_CLASSNAME
699 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
700 return false;
702 return c_token_starts_declspecs (token);
705 /* Return true if the next tokens from PARSER can start declaration
706 specifiers or a static assertion, false otherwise. */
707 static inline bool
708 c_parser_next_tokens_start_declaration (c_parser *parser)
710 c_token *token = c_parser_peek_token (parser);
712 /* Same as above. */
713 if (c_dialect_objc ()
714 && token->type == CPP_NAME
715 && token->id_kind == C_ID_CLASSNAME
716 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
717 return false;
719 /* Labels do not start declarations. */
720 if (token->type == CPP_NAME
721 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
722 return false;
724 if (c_token_starts_declaration (token))
725 return true;
727 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
728 return true;
730 return false;
733 /* Consume the next token from PARSER. */
735 static void
736 c_parser_consume_token (c_parser *parser)
738 gcc_assert (parser->tokens_avail >= 1);
739 gcc_assert (parser->tokens[0].type != CPP_EOF);
740 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
741 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
742 if (parser->tokens_avail == 2)
743 parser->tokens[0] = parser->tokens[1];
744 parser->tokens_avail--;
747 /* Expect the current token to be a #pragma. Consume it and remember
748 that we've begun parsing a pragma. */
750 static void
751 c_parser_consume_pragma (c_parser *parser)
753 gcc_assert (!parser->in_pragma);
754 gcc_assert (parser->tokens_avail >= 1);
755 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
756 if (parser->tokens_avail == 2)
757 parser->tokens[0] = parser->tokens[1];
758 parser->tokens_avail--;
759 parser->in_pragma = true;
762 /* Update the globals input_location and in_system_header from
763 TOKEN. */
764 static inline void
765 c_parser_set_source_position_from_token (c_token *token)
767 if (token->type != CPP_EOF)
769 input_location = token->location;
773 /* Issue a diagnostic of the form
774 FILE:LINE: MESSAGE before TOKEN
775 where TOKEN is the next token in the input stream of PARSER.
776 MESSAGE (specified by the caller) is usually of the form "expected
777 OTHER-TOKEN".
779 Do not issue a diagnostic if still recovering from an error.
781 ??? This is taken from the C++ parser, but building up messages in
782 this way is not i18n-friendly and some other approach should be
783 used. */
785 static void
786 c_parser_error (c_parser *parser, const char *gmsgid)
788 c_token *token = c_parser_peek_token (parser);
789 if (parser->error)
790 return;
791 parser->error = true;
792 if (!gmsgid)
793 return;
794 /* This diagnostic makes more sense if it is tagged to the line of
795 the token we just peeked at. */
796 c_parser_set_source_position_from_token (token);
797 c_parse_error (gmsgid,
798 /* Because c_parse_error does not understand
799 CPP_KEYWORD, keywords are treated like
800 identifiers. */
801 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
802 /* ??? The C parser does not save the cpp flags of a
803 token, we need to pass 0 here and we will not get
804 the source spelling of some tokens but rather the
805 canonical spelling. */
806 token->value, /*flags=*/0);
809 /* If the next token is of the indicated TYPE, consume it. Otherwise,
810 issue the error MSGID. If MSGID is NULL then a message has already
811 been produced and no message will be produced this time. Returns
812 true if found, false otherwise. */
814 static bool
815 c_parser_require (c_parser *parser,
816 enum cpp_ttype type,
817 const char *msgid)
819 if (c_parser_next_token_is (parser, type))
821 c_parser_consume_token (parser);
822 return true;
824 else
826 c_parser_error (parser, msgid);
827 return false;
831 /* If the next token is the indicated keyword, consume it. Otherwise,
832 issue the error MSGID. Returns true if found, false otherwise. */
834 static bool
835 c_parser_require_keyword (c_parser *parser,
836 enum rid keyword,
837 const char *msgid)
839 if (c_parser_next_token_is_keyword (parser, keyword))
841 c_parser_consume_token (parser);
842 return true;
844 else
846 c_parser_error (parser, msgid);
847 return false;
851 /* Like c_parser_require, except that tokens will be skipped until the
852 desired token is found. An error message is still produced if the
853 next token is not as expected. If MSGID is NULL then a message has
854 already been produced and no message will be produced this
855 time. */
857 static void
858 c_parser_skip_until_found (c_parser *parser,
859 enum cpp_ttype type,
860 const char *msgid)
862 unsigned nesting_depth = 0;
864 if (c_parser_require (parser, type, msgid))
865 return;
867 /* Skip tokens until the desired token is found. */
868 while (true)
870 /* Peek at the next token. */
871 c_token *token = c_parser_peek_token (parser);
872 /* If we've reached the token we want, consume it and stop. */
873 if (token->type == type && !nesting_depth)
875 c_parser_consume_token (parser);
876 break;
879 /* If we've run out of tokens, stop. */
880 if (token->type == CPP_EOF)
881 return;
882 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
883 return;
884 if (token->type == CPP_OPEN_BRACE
885 || token->type == CPP_OPEN_PAREN
886 || token->type == CPP_OPEN_SQUARE)
887 ++nesting_depth;
888 else if (token->type == CPP_CLOSE_BRACE
889 || token->type == CPP_CLOSE_PAREN
890 || token->type == CPP_CLOSE_SQUARE)
892 if (nesting_depth-- == 0)
893 break;
895 /* Consume this token. */
896 c_parser_consume_token (parser);
898 parser->error = false;
901 /* Skip tokens until the end of a parameter is found, but do not
902 consume the comma, semicolon or closing delimiter. */
904 static void
905 c_parser_skip_to_end_of_parameter (c_parser *parser)
907 unsigned nesting_depth = 0;
909 while (true)
911 c_token *token = c_parser_peek_token (parser);
912 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
913 && !nesting_depth)
914 break;
915 /* If we've run out of tokens, stop. */
916 if (token->type == CPP_EOF)
917 return;
918 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
919 return;
920 if (token->type == CPP_OPEN_BRACE
921 || token->type == CPP_OPEN_PAREN
922 || token->type == CPP_OPEN_SQUARE)
923 ++nesting_depth;
924 else if (token->type == CPP_CLOSE_BRACE
925 || token->type == CPP_CLOSE_PAREN
926 || token->type == CPP_CLOSE_SQUARE)
928 if (nesting_depth-- == 0)
929 break;
931 /* Consume this token. */
932 c_parser_consume_token (parser);
934 parser->error = false;
937 /* Expect to be at the end of the pragma directive and consume an
938 end of line marker. */
940 static void
941 c_parser_skip_to_pragma_eol (c_parser *parser)
943 gcc_assert (parser->in_pragma);
944 parser->in_pragma = false;
946 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
947 while (true)
949 c_token *token = c_parser_peek_token (parser);
950 if (token->type == CPP_EOF)
951 break;
952 if (token->type == CPP_PRAGMA_EOL)
954 c_parser_consume_token (parser);
955 break;
957 c_parser_consume_token (parser);
960 parser->error = false;
963 /* Skip tokens until we have consumed an entire block, or until we
964 have consumed a non-nested ';'. */
966 static void
967 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
969 unsigned nesting_depth = 0;
970 bool save_error = parser->error;
972 while (true)
974 c_token *token;
976 /* Peek at the next token. */
977 token = c_parser_peek_token (parser);
979 switch (token->type)
981 case CPP_EOF:
982 return;
984 case CPP_PRAGMA_EOL:
985 if (parser->in_pragma)
986 return;
987 break;
989 case CPP_SEMICOLON:
990 /* If the next token is a ';', we have reached the
991 end of the statement. */
992 if (!nesting_depth)
994 /* Consume the ';'. */
995 c_parser_consume_token (parser);
996 goto finished;
998 break;
1000 case CPP_CLOSE_BRACE:
1001 /* If the next token is a non-nested '}', then we have
1002 reached the end of the current block. */
1003 if (nesting_depth == 0 || --nesting_depth == 0)
1005 c_parser_consume_token (parser);
1006 goto finished;
1008 break;
1010 case CPP_OPEN_BRACE:
1011 /* If it the next token is a '{', then we are entering a new
1012 block. Consume the entire block. */
1013 ++nesting_depth;
1014 break;
1016 case CPP_PRAGMA:
1017 /* If we see a pragma, consume the whole thing at once. We
1018 have some safeguards against consuming pragmas willy-nilly.
1019 Normally, we'd expect to be here with parser->error set,
1020 which disables these safeguards. But it's possible to get
1021 here for secondary error recovery, after parser->error has
1022 been cleared. */
1023 c_parser_consume_pragma (parser);
1024 c_parser_skip_to_pragma_eol (parser);
1025 parser->error = save_error;
1026 continue;
1028 default:
1029 break;
1032 c_parser_consume_token (parser);
1035 finished:
1036 parser->error = false;
1039 /* CPP's options (initialized by c-opts.c). */
1040 extern cpp_options *cpp_opts;
1042 /* Save the warning flags which are controlled by __extension__. */
1044 static inline int
1045 disable_extension_diagnostics (void)
1047 int ret = (pedantic
1048 | (warn_pointer_arith << 1)
1049 | (warn_traditional << 2)
1050 | (flag_iso << 3)
1051 | (warn_long_long << 4)
1052 | (warn_cxx_compat << 5));
1053 cpp_opts->cpp_pedantic = pedantic = 0;
1054 warn_pointer_arith = 0;
1055 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1056 flag_iso = 0;
1057 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1058 warn_cxx_compat = 0;
1059 return ret;
1062 /* Restore the warning flags which are controlled by __extension__.
1063 FLAGS is the return value from disable_extension_diagnostics. */
1065 static inline void
1066 restore_extension_diagnostics (int flags)
1068 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1069 warn_pointer_arith = (flags >> 1) & 1;
1070 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1071 flag_iso = (flags >> 3) & 1;
1072 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1073 warn_cxx_compat = (flags >> 5) & 1;
1076 /* Possibly kinds of declarator to parse. */
1077 typedef enum c_dtr_syn {
1078 /* A normal declarator with an identifier. */
1079 C_DTR_NORMAL,
1080 /* An abstract declarator (maybe empty). */
1081 C_DTR_ABSTRACT,
1082 /* A parameter declarator: may be either, but after a type name does
1083 not redeclare a typedef name as an identifier if it can
1084 alternatively be interpreted as a typedef name; see DR#009,
1085 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1086 following DR#249. For example, given a typedef T, "int T" and
1087 "int *T" are valid parameter declarations redeclaring T, while
1088 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1089 abstract declarators rather than involving redundant parentheses;
1090 the same applies with attributes inside the parentheses before
1091 "T". */
1092 C_DTR_PARM
1093 } c_dtr_syn;
1095 static void c_parser_external_declaration (c_parser *);
1096 static void c_parser_asm_definition (c_parser *);
1097 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1098 bool, bool, tree *);
1099 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1100 static void c_parser_static_assert_declaration (c_parser *);
1101 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1102 bool, enum c_lookahead_kind);
1103 static struct c_typespec c_parser_enum_specifier (c_parser *);
1104 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1105 static tree c_parser_struct_declaration (c_parser *);
1106 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1107 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1108 bool *);
1109 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1110 c_dtr_syn, bool *);
1111 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1112 bool,
1113 struct c_declarator *);
1114 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1115 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
1116 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1117 static tree c_parser_simple_asm_expr (c_parser *);
1118 static tree c_parser_attributes (c_parser *);
1119 static struct c_type_name *c_parser_type_name (c_parser *);
1120 static struct c_expr c_parser_initializer (c_parser *);
1121 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1122 static void c_parser_initelt (c_parser *, struct obstack *);
1123 static void c_parser_initval (c_parser *, struct c_expr *,
1124 struct obstack *);
1125 static tree c_parser_compound_statement (c_parser *);
1126 static void c_parser_compound_statement_nostart (c_parser *);
1127 static void c_parser_label (c_parser *);
1128 static void c_parser_statement (c_parser *);
1129 static void c_parser_statement_after_labels (c_parser *);
1130 static void c_parser_if_statement (c_parser *);
1131 static void c_parser_switch_statement (c_parser *);
1132 static void c_parser_while_statement (c_parser *);
1133 static void c_parser_do_statement (c_parser *);
1134 static void c_parser_for_statement (c_parser *);
1135 static tree c_parser_asm_statement (c_parser *);
1136 static tree c_parser_asm_operands (c_parser *, bool);
1137 static tree c_parser_asm_goto_operands (c_parser *);
1138 static tree c_parser_asm_clobbers (c_parser *);
1139 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1140 static struct c_expr c_parser_conditional_expression (c_parser *,
1141 struct c_expr *);
1142 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1143 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1144 static struct c_expr c_parser_unary_expression (c_parser *);
1145 static struct c_expr c_parser_sizeof_expression (c_parser *);
1146 static struct c_expr c_parser_alignof_expression (c_parser *);
1147 static struct c_expr c_parser_postfix_expression (c_parser *);
1148 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1149 struct c_type_name *,
1150 location_t);
1151 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1152 location_t loc,
1153 struct c_expr);
1154 static struct c_expr c_parser_expression (c_parser *);
1155 static struct c_expr c_parser_expression_conv (c_parser *);
1156 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1157 VEC(tree,gc) **);
1158 static void c_parser_omp_construct (c_parser *);
1159 static void c_parser_omp_threadprivate (c_parser *);
1160 static void c_parser_omp_barrier (c_parser *);
1161 static void c_parser_omp_flush (c_parser *);
1162 static void c_parser_omp_taskwait (c_parser *);
1164 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1165 static bool c_parser_pragma (c_parser *, enum pragma_context);
1167 /* These Objective-C parser functions are only ever called when
1168 compiling Objective-C. */
1169 static void c_parser_objc_class_definition (c_parser *, tree);
1170 static void c_parser_objc_class_instance_variables (c_parser *);
1171 static void c_parser_objc_class_declaration (c_parser *);
1172 static void c_parser_objc_alias_declaration (c_parser *);
1173 static void c_parser_objc_protocol_definition (c_parser *, tree);
1174 static bool c_parser_objc_method_type (c_parser *);
1175 static void c_parser_objc_method_definition (c_parser *);
1176 static void c_parser_objc_methodprotolist (c_parser *);
1177 static void c_parser_objc_methodproto (c_parser *);
1178 static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
1179 static tree c_parser_objc_type_name (c_parser *);
1180 static tree c_parser_objc_protocol_refs (c_parser *);
1181 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1182 static void c_parser_objc_synchronized_statement (c_parser *);
1183 static tree c_parser_objc_selector (c_parser *);
1184 static tree c_parser_objc_selector_arg (c_parser *);
1185 static tree c_parser_objc_receiver (c_parser *);
1186 static tree c_parser_objc_message_args (c_parser *);
1187 static tree c_parser_objc_keywordexpr (c_parser *);
1188 static void c_parser_objc_at_property_declaration (c_parser *);
1189 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1190 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1191 static bool c_parser_objc_diagnose_bad_element_prefix
1192 (c_parser *, struct c_declspecs *);
1194 /* Parse a translation unit (C90 6.7, C99 6.9).
1196 translation-unit:
1197 external-declarations
1199 external-declarations:
1200 external-declaration
1201 external-declarations external-declaration
1203 GNU extensions:
1205 translation-unit:
1206 empty
1209 static void
1210 c_parser_translation_unit (c_parser *parser)
1212 if (c_parser_next_token_is (parser, CPP_EOF))
1214 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1215 "ISO C forbids an empty translation unit");
1217 else
1219 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1220 mark_valid_location_for_stdc_pragma (false);
1223 ggc_collect ();
1224 c_parser_external_declaration (parser);
1225 obstack_free (&parser_obstack, obstack_position);
1227 while (c_parser_next_token_is_not (parser, CPP_EOF));
1231 /* Parse an external declaration (C90 6.7, C99 6.9).
1233 external-declaration:
1234 function-definition
1235 declaration
1237 GNU extensions:
1239 external-declaration:
1240 asm-definition
1242 __extension__ external-declaration
1244 Objective-C:
1246 external-declaration:
1247 objc-class-definition
1248 objc-class-declaration
1249 objc-alias-declaration
1250 objc-protocol-definition
1251 objc-method-definition
1252 @end
1255 static void
1256 c_parser_external_declaration (c_parser *parser)
1258 int ext;
1259 switch (c_parser_peek_token (parser)->type)
1261 case CPP_KEYWORD:
1262 switch (c_parser_peek_token (parser)->keyword)
1264 case RID_EXTENSION:
1265 ext = disable_extension_diagnostics ();
1266 c_parser_consume_token (parser);
1267 c_parser_external_declaration (parser);
1268 restore_extension_diagnostics (ext);
1269 break;
1270 case RID_ASM:
1271 c_parser_asm_definition (parser);
1272 break;
1273 case RID_AT_INTERFACE:
1274 case RID_AT_IMPLEMENTATION:
1275 gcc_assert (c_dialect_objc ());
1276 c_parser_objc_class_definition (parser, NULL_TREE);
1277 break;
1278 case RID_AT_CLASS:
1279 gcc_assert (c_dialect_objc ());
1280 c_parser_objc_class_declaration (parser);
1281 break;
1282 case RID_AT_ALIAS:
1283 gcc_assert (c_dialect_objc ());
1284 c_parser_objc_alias_declaration (parser);
1285 break;
1286 case RID_AT_PROTOCOL:
1287 gcc_assert (c_dialect_objc ());
1288 c_parser_objc_protocol_definition (parser, NULL_TREE);
1289 break;
1290 case RID_AT_PROPERTY:
1291 gcc_assert (c_dialect_objc ());
1292 c_parser_objc_at_property_declaration (parser);
1293 break;
1294 case RID_AT_SYNTHESIZE:
1295 gcc_assert (c_dialect_objc ());
1296 c_parser_objc_at_synthesize_declaration (parser);
1297 break;
1298 case RID_AT_DYNAMIC:
1299 gcc_assert (c_dialect_objc ());
1300 c_parser_objc_at_dynamic_declaration (parser);
1301 break;
1302 case RID_AT_END:
1303 gcc_assert (c_dialect_objc ());
1304 c_parser_consume_token (parser);
1305 objc_finish_implementation ();
1306 break;
1307 default:
1308 goto decl_or_fndef;
1310 break;
1311 case CPP_SEMICOLON:
1312 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1313 "ISO C does not allow extra %<;%> outside of a function");
1314 c_parser_consume_token (parser);
1315 break;
1316 case CPP_PRAGMA:
1317 mark_valid_location_for_stdc_pragma (true);
1318 c_parser_pragma (parser, pragma_external);
1319 mark_valid_location_for_stdc_pragma (false);
1320 break;
1321 case CPP_PLUS:
1322 case CPP_MINUS:
1323 if (c_dialect_objc ())
1325 c_parser_objc_method_definition (parser);
1326 break;
1328 /* Else fall through, and yield a syntax error trying to parse
1329 as a declaration or function definition. */
1330 default:
1331 decl_or_fndef:
1332 /* A declaration or a function definition (or, in Objective-C,
1333 an @interface or @protocol with prefix attributes). We can
1334 only tell which after parsing the declaration specifiers, if
1335 any, and the first declarator. */
1336 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1337 break;
1341 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1342 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1343 accepted; otherwise (old-style parameter declarations) only other
1344 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1345 assertion is accepted; otherwise (old-style parameter declarations)
1346 it is not. If NESTED is true, we are inside a function or parsing
1347 old-style parameter declarations; any functions encountered are
1348 nested functions and declaration specifiers are required; otherwise
1349 we are at top level and functions are normal functions and
1350 declaration specifiers may be optional. If EMPTY_OK is true, empty
1351 declarations are OK (subject to all other constraints); otherwise
1352 (old-style parameter declarations) they are diagnosed. If
1353 START_ATTR_OK is true, the declaration specifiers may start with
1354 attributes; otherwise they may not.
1355 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1356 declaration when parsing an Objective-C foreach statement.
1358 declaration:
1359 declaration-specifiers init-declarator-list[opt] ;
1360 static_assert-declaration
1362 function-definition:
1363 declaration-specifiers[opt] declarator declaration-list[opt]
1364 compound-statement
1366 declaration-list:
1367 declaration
1368 declaration-list declaration
1370 init-declarator-list:
1371 init-declarator
1372 init-declarator-list , init-declarator
1374 init-declarator:
1375 declarator simple-asm-expr[opt] attributes[opt]
1376 declarator simple-asm-expr[opt] attributes[opt] = initializer
1378 GNU extensions:
1380 nested-function-definition:
1381 declaration-specifiers declarator declaration-list[opt]
1382 compound-statement
1384 Objective-C:
1385 attributes objc-class-definition
1386 attributes objc-category-definition
1387 attributes objc-protocol-definition
1389 The simple-asm-expr and attributes are GNU extensions.
1391 This function does not handle __extension__; that is handled in its
1392 callers. ??? Following the old parser, __extension__ may start
1393 external declarations, declarations in functions and declarations
1394 at the start of "for" loops, but not old-style parameter
1395 declarations.
1397 C99 requires declaration specifiers in a function definition; the
1398 absence is diagnosed through the diagnosis of implicit int. In GNU
1399 C we also allow but diagnose declarations without declaration
1400 specifiers, but only at top level (elsewhere they conflict with
1401 other syntax).
1403 In Objective-C, declarations of the looping variable in a foreach
1404 statement are exceptionally terminated by 'in' (for example, 'for
1405 (NSObject *object in array) { ... }').
1407 OpenMP:
1409 declaration:
1410 threadprivate-directive */
1412 static void
1413 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1414 bool static_assert_ok, bool empty_ok,
1415 bool nested, bool start_attr_ok,
1416 tree *objc_foreach_object_declaration)
1418 struct c_declspecs *specs;
1419 tree prefix_attrs;
1420 tree all_prefix_attrs;
1421 bool diagnosed_no_specs = false;
1422 location_t here = c_parser_peek_token (parser)->location;
1424 if (static_assert_ok
1425 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1427 c_parser_static_assert_declaration (parser);
1428 return;
1430 specs = build_null_declspecs ();
1432 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1433 if (c_parser_peek_token (parser)->type == CPP_NAME
1434 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1435 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1436 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1437 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1439 error_at (here, "unknown type name %qE",
1440 c_parser_peek_token (parser)->value);
1442 /* Parse declspecs normally to get a correct pointer type, but avoid
1443 a further "fails to be a type name" error. Refuse nested functions
1444 since it is not how the user likely wants us to recover. */
1445 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1446 c_parser_peek_token (parser)->keyword = RID_VOID;
1447 c_parser_peek_token (parser)->value = error_mark_node;
1448 fndef_ok = !nested;
1451 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1452 if (parser->error)
1454 c_parser_skip_to_end_of_block_or_statement (parser);
1455 return;
1457 if (nested && !specs->declspecs_seen_p)
1459 c_parser_error (parser, "expected declaration specifiers");
1460 c_parser_skip_to_end_of_block_or_statement (parser);
1461 return;
1463 finish_declspecs (specs);
1464 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1466 if (empty_ok)
1467 shadow_tag (specs);
1468 else
1470 shadow_tag_warned (specs, 1);
1471 pedwarn (here, 0, "empty declaration");
1473 c_parser_consume_token (parser);
1474 return;
1477 /* Provide better error recovery. Note that a type name here is usually
1478 better diagnosed as a redeclaration. */
1479 if (empty_ok
1480 && specs->typespec_kind == ctsk_tagdef
1481 && c_parser_next_token_starts_declspecs (parser)
1482 && !c_parser_next_token_is (parser, CPP_NAME))
1484 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1485 parser->error = false;
1486 shadow_tag_warned (specs, 1);
1487 return;
1489 else if (c_dialect_objc ())
1491 /* Prefix attributes are an error on method decls. */
1492 switch (c_parser_peek_token (parser)->type)
1494 case CPP_PLUS:
1495 case CPP_MINUS:
1496 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1497 return;
1498 if (specs->attrs)
1500 warning_at (c_parser_peek_token (parser)->location,
1501 OPT_Wattributes,
1502 "prefix attributes are ignored for methods");
1503 specs->attrs = NULL_TREE;
1505 if (fndef_ok)
1506 c_parser_objc_method_definition (parser);
1507 else
1508 c_parser_objc_methodproto (parser);
1509 return;
1510 break;
1511 default:
1512 break;
1514 /* This is where we parse 'attributes @interface ...',
1515 'attributes @implementation ...', 'attributes @protocol ...'
1516 (where attributes could be, for example, __attribute__
1517 ((deprecated)).
1519 switch (c_parser_peek_token (parser)->keyword)
1521 case RID_AT_INTERFACE:
1523 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1524 return;
1525 c_parser_objc_class_definition (parser, specs->attrs);
1526 return;
1528 break;
1529 case RID_AT_IMPLEMENTATION:
1531 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1532 return;
1533 if (specs->attrs)
1535 warning_at (c_parser_peek_token (parser)->location,
1536 OPT_Wattributes,
1537 "prefix attributes are ignored for implementations");
1538 specs->attrs = NULL_TREE;
1540 c_parser_objc_class_definition (parser, NULL_TREE);
1541 return;
1543 break;
1544 case RID_AT_PROTOCOL:
1546 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1547 return;
1548 c_parser_objc_protocol_definition (parser, specs->attrs);
1549 return;
1551 break;
1552 case RID_AT_ALIAS:
1553 case RID_AT_CLASS:
1554 case RID_AT_END:
1555 case RID_AT_PROPERTY:
1556 if (specs->attrs)
1558 c_parser_error (parser,
1559 "attributes may not be specified before" );
1560 specs->attrs = NULL;
1562 break;
1563 default:
1564 break;
1568 pending_xref_error ();
1569 prefix_attrs = specs->attrs;
1570 all_prefix_attrs = prefix_attrs;
1571 specs->attrs = NULL_TREE;
1572 while (true)
1574 struct c_declarator *declarator;
1575 bool dummy = false;
1576 tree fnbody;
1577 /* Declaring either one or more declarators (in which case we
1578 should diagnose if there were no declaration specifiers) or a
1579 function definition (in which case the diagnostic for
1580 implicit int suffices). */
1581 declarator = c_parser_declarator (parser,
1582 specs->typespec_kind != ctsk_none,
1583 C_DTR_NORMAL, &dummy);
1584 if (declarator == NULL)
1586 c_parser_skip_to_end_of_block_or_statement (parser);
1587 return;
1589 if (c_parser_next_token_is (parser, CPP_EQ)
1590 || c_parser_next_token_is (parser, CPP_COMMA)
1591 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1592 || c_parser_next_token_is_keyword (parser, RID_ASM)
1593 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1594 || c_parser_next_token_is_keyword (parser, RID_IN))
1596 tree asm_name = NULL_TREE;
1597 tree postfix_attrs = NULL_TREE;
1598 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1600 diagnosed_no_specs = true;
1601 pedwarn (here, 0, "data definition has no type or storage class");
1603 /* Having seen a data definition, there cannot now be a
1604 function definition. */
1605 fndef_ok = false;
1606 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1607 asm_name = c_parser_simple_asm_expr (parser);
1608 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1609 postfix_attrs = c_parser_attributes (parser);
1610 if (c_parser_next_token_is (parser, CPP_EQ))
1612 tree d;
1613 struct c_expr init;
1614 location_t init_loc;
1615 c_parser_consume_token (parser);
1616 /* The declaration of the variable is in effect while
1617 its initializer is parsed. */
1618 d = start_decl (declarator, specs, true,
1619 chainon (postfix_attrs, all_prefix_attrs));
1620 if (!d)
1621 d = error_mark_node;
1622 start_init (d, asm_name, global_bindings_p ());
1623 init_loc = c_parser_peek_token (parser)->location;
1624 init = c_parser_initializer (parser);
1625 finish_init ();
1626 if (d != error_mark_node)
1628 maybe_warn_string_init (TREE_TYPE (d), init);
1629 finish_decl (d, init_loc, init.value,
1630 init.original_type, asm_name);
1633 else
1635 tree d = start_decl (declarator, specs, false,
1636 chainon (postfix_attrs,
1637 all_prefix_attrs));
1638 if (d)
1639 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1640 NULL_TREE, asm_name);
1642 if (c_parser_next_token_is_keyword (parser, RID_IN))
1644 if (d)
1645 *objc_foreach_object_declaration = d;
1646 else
1647 *objc_foreach_object_declaration = error_mark_node;
1650 if (c_parser_next_token_is (parser, CPP_COMMA))
1652 c_parser_consume_token (parser);
1653 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1654 all_prefix_attrs = chainon (c_parser_attributes (parser),
1655 prefix_attrs);
1656 else
1657 all_prefix_attrs = prefix_attrs;
1658 continue;
1660 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1662 c_parser_consume_token (parser);
1663 return;
1665 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1667 /* This can only happen in Objective-C: we found the
1668 'in' that terminates the declaration inside an
1669 Objective-C foreach statement. Do not consume the
1670 token, so that the caller can use it to determine
1671 that this indeed is a foreach context. */
1672 return;
1674 else
1676 c_parser_error (parser, "expected %<,%> or %<;%>");
1677 c_parser_skip_to_end_of_block_or_statement (parser);
1678 return;
1681 else if (!fndef_ok)
1683 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1684 "%<asm%> or %<__attribute__%>");
1685 c_parser_skip_to_end_of_block_or_statement (parser);
1686 return;
1688 /* Function definition (nested or otherwise). */
1689 if (nested)
1691 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1692 c_push_function_context ();
1694 if (!start_function (specs, declarator, all_prefix_attrs))
1696 /* This can appear in many cases looking nothing like a
1697 function definition, so we don't give a more specific
1698 error suggesting there was one. */
1699 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1700 "or %<__attribute__%>");
1701 if (nested)
1702 c_pop_function_context ();
1703 break;
1705 /* Parse old-style parameter declarations. ??? Attributes are
1706 not allowed to start declaration specifiers here because of a
1707 syntax conflict between a function declaration with attribute
1708 suffix and a function definition with an attribute prefix on
1709 first old-style parameter declaration. Following the old
1710 parser, they are not accepted on subsequent old-style
1711 parameter declarations either. However, there is no
1712 ambiguity after the first declaration, nor indeed on the
1713 first as long as we don't allow postfix attributes after a
1714 declarator with a nonempty identifier list in a definition;
1715 and postfix attributes have never been accepted here in
1716 function definitions either. */
1717 while (c_parser_next_token_is_not (parser, CPP_EOF)
1718 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1719 c_parser_declaration_or_fndef (parser, false, false, false,
1720 true, false, NULL);
1721 store_parm_decls ();
1722 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1723 = c_parser_peek_token (parser)->location;
1724 fnbody = c_parser_compound_statement (parser);
1725 if (nested)
1727 tree decl = current_function_decl;
1728 /* Mark nested functions as needing static-chain initially.
1729 lower_nested_functions will recompute it but the
1730 DECL_STATIC_CHAIN flag is also used before that happens,
1731 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1732 DECL_STATIC_CHAIN (decl) = 1;
1733 add_stmt (fnbody);
1734 finish_function ();
1735 c_pop_function_context ();
1736 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1738 else
1740 add_stmt (fnbody);
1741 finish_function ();
1743 break;
1747 /* Parse an asm-definition (asm() outside a function body). This is a
1748 GNU extension.
1750 asm-definition:
1751 simple-asm-expr ;
1754 static void
1755 c_parser_asm_definition (c_parser *parser)
1757 tree asm_str = c_parser_simple_asm_expr (parser);
1758 if (asm_str)
1759 cgraph_add_asm_node (asm_str);
1760 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1763 /* Parse a static assertion (C1X N1425 6.7.10).
1765 static_assert-declaration:
1766 static_assert-declaration-no-semi ;
1769 static void
1770 c_parser_static_assert_declaration (c_parser *parser)
1772 c_parser_static_assert_declaration_no_semi (parser);
1773 if (parser->error
1774 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1775 c_parser_skip_to_end_of_block_or_statement (parser);
1778 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1779 semicolon.
1781 static_assert-declaration-no-semi:
1782 _Static_assert ( constant-expression , string-literal )
1785 static void
1786 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1788 location_t assert_loc, value_loc;
1789 tree value;
1790 tree string;
1792 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1793 assert_loc = c_parser_peek_token (parser)->location;
1794 if (!flag_isoc1x)
1796 if (flag_isoc99)
1797 pedwarn (assert_loc, OPT_pedantic,
1798 "ISO C99 does not support %<_Static_assert%>");
1799 else
1800 pedwarn (assert_loc, OPT_pedantic,
1801 "ISO C90 does not support %<_Static_assert%>");
1803 c_parser_consume_token (parser);
1804 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1805 return;
1806 value_loc = c_parser_peek_token (parser)->location;
1807 value = c_parser_expr_no_commas (parser, NULL).value;
1808 parser->lex_untranslated_string = true;
1809 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1811 parser->lex_untranslated_string = false;
1812 return;
1814 switch (c_parser_peek_token (parser)->type)
1816 case CPP_STRING:
1817 case CPP_STRING16:
1818 case CPP_STRING32:
1819 case CPP_WSTRING:
1820 case CPP_UTF8STRING:
1821 string = c_parser_peek_token (parser)->value;
1822 c_parser_consume_token (parser);
1823 parser->lex_untranslated_string = false;
1824 break;
1825 default:
1826 c_parser_error (parser, "expected string literal");
1827 parser->lex_untranslated_string = false;
1828 return;
1830 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1832 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1834 error_at (value_loc, "expression in static assertion is not an integer");
1835 return;
1837 if (TREE_CODE (value) != INTEGER_CST)
1839 value = c_fully_fold (value, false, NULL);
1840 if (TREE_CODE (value) == INTEGER_CST)
1841 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1842 "is not an integer constant expression");
1844 if (TREE_CODE (value) != INTEGER_CST)
1846 error_at (value_loc, "expression in static assertion is not constant");
1847 return;
1849 constant_expression_warning (value);
1850 if (integer_zerop (value))
1851 error_at (assert_loc, "static assertion failed: %E", string);
1854 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1855 6.7), adding them to SPECS (which may already include some).
1856 Storage class specifiers are accepted iff SCSPEC_OK; type
1857 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1858 the start iff START_ATTR_OK.
1860 declaration-specifiers:
1861 storage-class-specifier declaration-specifiers[opt]
1862 type-specifier declaration-specifiers[opt]
1863 type-qualifier declaration-specifiers[opt]
1864 function-specifier declaration-specifiers[opt]
1866 Function specifiers (inline) are from C99, and are currently
1867 handled as storage class specifiers, as is __thread.
1869 C90 6.5.1, C99 6.7.1:
1870 storage-class-specifier:
1871 typedef
1872 extern
1873 static
1874 auto
1875 register
1877 C99 6.7.4:
1878 function-specifier:
1879 inline
1881 C90 6.5.2, C99 6.7.2:
1882 type-specifier:
1883 void
1884 char
1885 short
1887 long
1888 float
1889 double
1890 signed
1891 unsigned
1892 _Bool
1893 _Complex
1894 [_Imaginary removed in C99 TC2]
1895 struct-or-union-specifier
1896 enum-specifier
1897 typedef-name
1899 (_Bool and _Complex are new in C99.)
1901 C90 6.5.3, C99 6.7.3:
1903 type-qualifier:
1904 const
1905 restrict
1906 volatile
1907 address-space-qualifier
1909 (restrict is new in C99.)
1911 GNU extensions:
1913 declaration-specifiers:
1914 attributes declaration-specifiers[opt]
1916 type-qualifier:
1917 address-space
1919 address-space:
1920 identifier recognized by the target
1922 storage-class-specifier:
1923 __thread
1925 type-specifier:
1926 typeof-specifier
1927 __int128
1928 _Decimal32
1929 _Decimal64
1930 _Decimal128
1931 _Fract
1932 _Accum
1933 _Sat
1935 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1936 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1938 Objective-C:
1940 type-specifier:
1941 class-name objc-protocol-refs[opt]
1942 typedef-name objc-protocol-refs
1943 objc-protocol-refs
1946 static void
1947 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1948 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1949 enum c_lookahead_kind la)
1951 bool attrs_ok = start_attr_ok;
1952 bool seen_type = specs->typespec_kind != ctsk_none;
1954 if (!typespec_ok)
1955 gcc_assert (la == cla_prefer_id);
1957 while (c_parser_next_token_is (parser, CPP_NAME)
1958 || c_parser_next_token_is (parser, CPP_KEYWORD)
1959 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1961 struct c_typespec t;
1962 tree attrs;
1963 location_t loc = c_parser_peek_token (parser)->location;
1965 /* If we cannot accept a type, exit if the next token must start
1966 one. Also, if we already have seen a tagged definition,
1967 a typename would be an error anyway and likely the user
1968 has simply forgotten a semicolon, so we exit. */
1969 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
1970 && c_parser_next_tokens_start_typename (parser, la)
1971 && !c_parser_next_token_is_qualifier (parser))
1972 break;
1974 if (c_parser_next_token_is (parser, CPP_NAME))
1976 tree value = c_parser_peek_token (parser)->value;
1977 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1979 if (kind == C_ID_ADDRSPACE)
1981 addr_space_t as
1982 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1983 declspecs_add_addrspace (specs, as);
1984 c_parser_consume_token (parser);
1985 attrs_ok = true;
1986 continue;
1989 gcc_assert (!c_parser_next_token_is_qualifier (parser));
1991 /* If we cannot accept a type, and the next token must start one,
1992 exit. Do the same if we already have seen a tagged definition,
1993 since it would be an error anyway and likely the user has simply
1994 forgotten a semicolon. */
1995 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
1996 break;
1998 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
1999 a C_ID_CLASSNAME. */
2000 c_parser_consume_token (parser);
2001 seen_type = true;
2002 attrs_ok = true;
2003 if (kind == C_ID_ID)
2005 error ("unknown type name %qE", value);
2006 t.kind = ctsk_typedef;
2007 t.spec = error_mark_node;
2009 else if (kind == C_ID_TYPENAME
2010 && (!c_dialect_objc ()
2011 || c_parser_next_token_is_not (parser, CPP_LESS)))
2013 t.kind = ctsk_typedef;
2014 /* For a typedef name, record the meaning, not the name.
2015 In case of 'foo foo, bar;'. */
2016 t.spec = lookup_name (value);
2018 else
2020 tree proto = NULL_TREE;
2021 gcc_assert (c_dialect_objc ());
2022 t.kind = ctsk_objc;
2023 if (c_parser_next_token_is (parser, CPP_LESS))
2024 proto = c_parser_objc_protocol_refs (parser);
2025 t.spec = objc_get_protocol_qualified_type (value, proto);
2027 t.expr = NULL_TREE;
2028 t.expr_const_operands = true;
2029 declspecs_add_type (loc, specs, t);
2030 continue;
2032 if (c_parser_next_token_is (parser, CPP_LESS))
2034 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2035 nisse@lysator.liu.se. */
2036 tree proto;
2037 gcc_assert (c_dialect_objc ());
2038 if (!typespec_ok || seen_type)
2039 break;
2040 proto = c_parser_objc_protocol_refs (parser);
2041 t.kind = ctsk_objc;
2042 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2043 t.expr = NULL_TREE;
2044 t.expr_const_operands = true;
2045 declspecs_add_type (loc, specs, t);
2046 continue;
2048 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2049 switch (c_parser_peek_token (parser)->keyword)
2051 case RID_STATIC:
2052 case RID_EXTERN:
2053 case RID_REGISTER:
2054 case RID_TYPEDEF:
2055 case RID_INLINE:
2056 case RID_AUTO:
2057 case RID_THREAD:
2058 if (!scspec_ok)
2059 goto out;
2060 attrs_ok = true;
2061 /* TODO: Distinguish between function specifiers (inline)
2062 and storage class specifiers, either here or in
2063 declspecs_add_scspec. */
2064 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2065 c_parser_consume_token (parser);
2066 break;
2067 case RID_UNSIGNED:
2068 case RID_LONG:
2069 case RID_INT128:
2070 case RID_SHORT:
2071 case RID_SIGNED:
2072 case RID_COMPLEX:
2073 case RID_INT:
2074 case RID_CHAR:
2075 case RID_FLOAT:
2076 case RID_DOUBLE:
2077 case RID_VOID:
2078 case RID_DFLOAT32:
2079 case RID_DFLOAT64:
2080 case RID_DFLOAT128:
2081 case RID_BOOL:
2082 case RID_FRACT:
2083 case RID_ACCUM:
2084 case RID_SAT:
2085 if (!typespec_ok)
2086 goto out;
2087 attrs_ok = true;
2088 seen_type = true;
2089 if (c_dialect_objc ())
2090 parser->objc_need_raw_identifier = true;
2091 t.kind = ctsk_resword;
2092 t.spec = c_parser_peek_token (parser)->value;
2093 t.expr = NULL_TREE;
2094 t.expr_const_operands = true;
2095 declspecs_add_type (loc, specs, t);
2096 c_parser_consume_token (parser);
2097 break;
2098 case RID_ENUM:
2099 if (!typespec_ok)
2100 goto out;
2101 attrs_ok = true;
2102 seen_type = true;
2103 t = c_parser_enum_specifier (parser);
2104 declspecs_add_type (loc, specs, t);
2105 break;
2106 case RID_STRUCT:
2107 case RID_UNION:
2108 if (!typespec_ok)
2109 goto out;
2110 attrs_ok = true;
2111 seen_type = true;
2112 t = c_parser_struct_or_union_specifier (parser);
2113 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2114 declspecs_add_type (loc, specs, t);
2115 break;
2116 case RID_TYPEOF:
2117 /* ??? The old parser rejected typeof after other type
2118 specifiers, but is a syntax error the best way of
2119 handling this? */
2120 if (!typespec_ok || seen_type)
2121 goto out;
2122 attrs_ok = true;
2123 seen_type = true;
2124 t = c_parser_typeof_specifier (parser);
2125 declspecs_add_type (loc, specs, t);
2126 break;
2127 case RID_CONST:
2128 case RID_VOLATILE:
2129 case RID_RESTRICT:
2130 attrs_ok = true;
2131 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2132 c_parser_consume_token (parser);
2133 break;
2134 case RID_ATTRIBUTE:
2135 if (!attrs_ok)
2136 goto out;
2137 attrs = c_parser_attributes (parser);
2138 declspecs_add_attrs (specs, attrs);
2139 break;
2140 default:
2141 goto out;
2144 out: ;
2147 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2149 enum-specifier:
2150 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2151 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2152 enum attributes[opt] identifier
2154 The form with trailing comma is new in C99. The forms with
2155 attributes are GNU extensions. In GNU C, we accept any expression
2156 without commas in the syntax (assignment expressions, not just
2157 conditional expressions); assignment expressions will be diagnosed
2158 as non-constant.
2160 enumerator-list:
2161 enumerator
2162 enumerator-list , enumerator
2164 enumerator:
2165 enumeration-constant
2166 enumeration-constant = constant-expression
2169 static struct c_typespec
2170 c_parser_enum_specifier (c_parser *parser)
2172 struct c_typespec ret;
2173 tree attrs;
2174 tree ident = NULL_TREE;
2175 location_t enum_loc;
2176 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2177 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2178 enum_loc = c_parser_peek_token (parser)->location;
2179 c_parser_consume_token (parser);
2180 attrs = c_parser_attributes (parser);
2181 enum_loc = c_parser_peek_token (parser)->location;
2182 /* Set the location in case we create a decl now. */
2183 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2184 if (c_parser_next_token_is (parser, CPP_NAME))
2186 ident = c_parser_peek_token (parser)->value;
2187 ident_loc = c_parser_peek_token (parser)->location;
2188 enum_loc = ident_loc;
2189 c_parser_consume_token (parser);
2191 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2193 /* Parse an enum definition. */
2194 struct c_enum_contents the_enum;
2195 tree type = start_enum (enum_loc, &the_enum, ident);
2196 tree postfix_attrs;
2197 /* We chain the enumerators in reverse order, then put them in
2198 forward order at the end. */
2199 tree values = NULL_TREE;
2200 c_parser_consume_token (parser);
2201 while (true)
2203 tree enum_id;
2204 tree enum_value;
2205 tree enum_decl;
2206 bool seen_comma;
2207 c_token *token;
2208 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2209 location_t decl_loc, value_loc;
2210 if (c_parser_next_token_is_not (parser, CPP_NAME))
2212 c_parser_error (parser, "expected identifier");
2213 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2214 values = error_mark_node;
2215 break;
2217 token = c_parser_peek_token (parser);
2218 enum_id = token->value;
2219 /* Set the location in case we create a decl now. */
2220 c_parser_set_source_position_from_token (token);
2221 decl_loc = value_loc = token->location;
2222 c_parser_consume_token (parser);
2223 if (c_parser_next_token_is (parser, CPP_EQ))
2225 c_parser_consume_token (parser);
2226 value_loc = c_parser_peek_token (parser)->location;
2227 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2229 else
2230 enum_value = NULL_TREE;
2231 enum_decl = build_enumerator (decl_loc, value_loc,
2232 &the_enum, enum_id, enum_value);
2233 TREE_CHAIN (enum_decl) = values;
2234 values = enum_decl;
2235 seen_comma = false;
2236 if (c_parser_next_token_is (parser, CPP_COMMA))
2238 comma_loc = c_parser_peek_token (parser)->location;
2239 seen_comma = true;
2240 c_parser_consume_token (parser);
2242 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2244 if (seen_comma && !flag_isoc99)
2245 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2246 c_parser_consume_token (parser);
2247 break;
2249 if (!seen_comma)
2251 c_parser_error (parser, "expected %<,%> or %<}%>");
2252 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2253 values = error_mark_node;
2254 break;
2257 postfix_attrs = c_parser_attributes (parser);
2258 ret.spec = finish_enum (type, nreverse (values),
2259 chainon (attrs, postfix_attrs));
2260 ret.kind = ctsk_tagdef;
2261 ret.expr = NULL_TREE;
2262 ret.expr_const_operands = true;
2263 return ret;
2265 else if (!ident)
2267 c_parser_error (parser, "expected %<{%>");
2268 ret.spec = error_mark_node;
2269 ret.kind = ctsk_tagref;
2270 ret.expr = NULL_TREE;
2271 ret.expr_const_operands = true;
2272 return ret;
2274 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2275 /* In ISO C, enumerated types can be referred to only if already
2276 defined. */
2277 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2279 gcc_assert (ident);
2280 pedwarn (enum_loc, OPT_pedantic,
2281 "ISO C forbids forward references to %<enum%> types");
2283 return ret;
2286 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2288 struct-or-union-specifier:
2289 struct-or-union attributes[opt] identifier[opt]
2290 { struct-contents } attributes[opt]
2291 struct-or-union attributes[opt] identifier
2293 struct-contents:
2294 struct-declaration-list
2296 struct-declaration-list:
2297 struct-declaration ;
2298 struct-declaration-list struct-declaration ;
2300 GNU extensions:
2302 struct-contents:
2303 empty
2304 struct-declaration
2305 struct-declaration-list struct-declaration
2307 struct-declaration-list:
2308 struct-declaration-list ;
2311 (Note that in the syntax here, unlike that in ISO C, the semicolons
2312 are included here rather than in struct-declaration, in order to
2313 describe the syntax with extra semicolons and missing semicolon at
2314 end.)
2316 Objective-C:
2318 struct-declaration-list:
2319 @defs ( class-name )
2321 (Note this does not include a trailing semicolon, but can be
2322 followed by further declarations, and gets a pedwarn-if-pedantic
2323 when followed by a semicolon.) */
2325 static struct c_typespec
2326 c_parser_struct_or_union_specifier (c_parser *parser)
2328 struct c_typespec ret;
2329 tree attrs;
2330 tree ident = NULL_TREE;
2331 location_t struct_loc;
2332 location_t ident_loc = UNKNOWN_LOCATION;
2333 enum tree_code code;
2334 switch (c_parser_peek_token (parser)->keyword)
2336 case RID_STRUCT:
2337 code = RECORD_TYPE;
2338 break;
2339 case RID_UNION:
2340 code = UNION_TYPE;
2341 break;
2342 default:
2343 gcc_unreachable ();
2345 struct_loc = c_parser_peek_token (parser)->location;
2346 c_parser_consume_token (parser);
2347 attrs = c_parser_attributes (parser);
2349 /* Set the location in case we create a decl now. */
2350 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2352 if (c_parser_next_token_is (parser, CPP_NAME))
2354 ident = c_parser_peek_token (parser)->value;
2355 ident_loc = c_parser_peek_token (parser)->location;
2356 struct_loc = ident_loc;
2357 c_parser_consume_token (parser);
2359 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2361 /* Parse a struct or union definition. Start the scope of the
2362 tag before parsing components. */
2363 struct c_struct_parse_info *struct_info;
2364 tree type = start_struct (struct_loc, code, ident, &struct_info);
2365 tree postfix_attrs;
2366 /* We chain the components in reverse order, then put them in
2367 forward order at the end. Each struct-declaration may
2368 declare multiple components (comma-separated), so we must use
2369 chainon to join them, although when parsing each
2370 struct-declaration we can use TREE_CHAIN directly.
2372 The theory behind all this is that there will be more
2373 semicolon separated fields than comma separated fields, and
2374 so we'll be minimizing the number of node traversals required
2375 by chainon. */
2376 tree contents = NULL_TREE;
2377 c_parser_consume_token (parser);
2378 /* Handle the Objective-C @defs construct,
2379 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2380 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2382 tree name;
2383 gcc_assert (c_dialect_objc ());
2384 c_parser_consume_token (parser);
2385 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2386 goto end_at_defs;
2387 if (c_parser_next_token_is (parser, CPP_NAME)
2388 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2390 name = c_parser_peek_token (parser)->value;
2391 c_parser_consume_token (parser);
2393 else
2395 c_parser_error (parser, "expected class name");
2396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2397 goto end_at_defs;
2399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2400 "expected %<)%>");
2401 contents = nreverse (objc_get_class_ivars (name));
2403 end_at_defs:
2404 /* Parse the struct-declarations and semicolons. Problems with
2405 semicolons are diagnosed here; empty structures are diagnosed
2406 elsewhere. */
2407 while (true)
2409 tree decls;
2410 /* Parse any stray semicolon. */
2411 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2413 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2414 "extra semicolon in struct or union specified");
2415 c_parser_consume_token (parser);
2416 continue;
2418 /* Stop if at the end of the struct or union contents. */
2419 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2421 c_parser_consume_token (parser);
2422 break;
2424 /* Accept #pragmas at struct scope. */
2425 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2427 c_parser_pragma (parser, pragma_external);
2428 continue;
2430 /* Parse some comma-separated declarations, but not the
2431 trailing semicolon if any. */
2432 decls = c_parser_struct_declaration (parser);
2433 contents = chainon (decls, contents);
2434 /* If no semicolon follows, either we have a parse error or
2435 are at the end of the struct or union and should
2436 pedwarn. */
2437 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2438 c_parser_consume_token (parser);
2439 else
2441 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2442 pedwarn (c_parser_peek_token (parser)->location, 0,
2443 "no semicolon at end of struct or union");
2444 else if (parser->error
2445 || !c_parser_next_token_starts_declspecs (parser))
2447 c_parser_error (parser, "expected %<;%>");
2448 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2449 break;
2452 /* If we come here, we have already emitted an error
2453 for an expected `;', identifier or `(', and we also
2454 recovered already. Go on with the next field. */
2457 postfix_attrs = c_parser_attributes (parser);
2458 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2459 chainon (attrs, postfix_attrs), struct_info);
2460 ret.kind = ctsk_tagdef;
2461 ret.expr = NULL_TREE;
2462 ret.expr_const_operands = true;
2463 return ret;
2465 else if (!ident)
2467 c_parser_error (parser, "expected %<{%>");
2468 ret.spec = error_mark_node;
2469 ret.kind = ctsk_tagref;
2470 ret.expr = NULL_TREE;
2471 ret.expr_const_operands = true;
2472 return ret;
2474 ret = parser_xref_tag (ident_loc, code, ident);
2475 return ret;
2478 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2479 the trailing semicolon.
2481 struct-declaration:
2482 specifier-qualifier-list struct-declarator-list
2483 static_assert-declaration-no-semi
2485 specifier-qualifier-list:
2486 type-specifier specifier-qualifier-list[opt]
2487 type-qualifier specifier-qualifier-list[opt]
2488 attributes specifier-qualifier-list[opt]
2490 struct-declarator-list:
2491 struct-declarator
2492 struct-declarator-list , attributes[opt] struct-declarator
2494 struct-declarator:
2495 declarator attributes[opt]
2496 declarator[opt] : constant-expression attributes[opt]
2498 GNU extensions:
2500 struct-declaration:
2501 __extension__ struct-declaration
2502 specifier-qualifier-list
2504 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2505 of attributes where shown is a GNU extension. In GNU C, we accept
2506 any expression without commas in the syntax (assignment
2507 expressions, not just conditional expressions); assignment
2508 expressions will be diagnosed as non-constant. */
2510 static tree
2511 c_parser_struct_declaration (c_parser *parser)
2513 struct c_declspecs *specs;
2514 tree prefix_attrs;
2515 tree all_prefix_attrs;
2516 tree decls;
2517 location_t decl_loc;
2518 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2520 int ext;
2521 tree decl;
2522 ext = disable_extension_diagnostics ();
2523 c_parser_consume_token (parser);
2524 decl = c_parser_struct_declaration (parser);
2525 restore_extension_diagnostics (ext);
2526 return decl;
2528 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2530 c_parser_static_assert_declaration_no_semi (parser);
2531 return NULL_TREE;
2533 specs = build_null_declspecs ();
2534 decl_loc = c_parser_peek_token (parser)->location;
2535 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2536 if (parser->error)
2537 return NULL_TREE;
2538 if (!specs->declspecs_seen_p)
2540 c_parser_error (parser, "expected specifier-qualifier-list");
2541 return NULL_TREE;
2543 finish_declspecs (specs);
2544 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2545 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2547 tree ret;
2548 if (specs->typespec_kind == ctsk_none)
2550 pedwarn (decl_loc, OPT_pedantic,
2551 "ISO C forbids member declarations with no members");
2552 shadow_tag_warned (specs, pedantic);
2553 ret = NULL_TREE;
2555 else
2557 /* Support for unnamed structs or unions as members of
2558 structs or unions (which is [a] useful and [b] supports
2559 MS P-SDK). */
2560 tree attrs = NULL;
2562 ret = grokfield (c_parser_peek_token (parser)->location,
2563 build_id_declarator (NULL_TREE), specs,
2564 NULL_TREE, &attrs);
2565 if (ret)
2566 decl_attributes (&ret, attrs, 0);
2568 return ret;
2571 /* Provide better error recovery. Note that a type name here is valid,
2572 and will be treated as a field name. */
2573 if (specs->typespec_kind == ctsk_tagdef
2574 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2575 && c_parser_next_token_starts_declspecs (parser)
2576 && !c_parser_next_token_is (parser, CPP_NAME))
2578 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2579 parser->error = false;
2580 return NULL_TREE;
2583 pending_xref_error ();
2584 prefix_attrs = specs->attrs;
2585 all_prefix_attrs = prefix_attrs;
2586 specs->attrs = NULL_TREE;
2587 decls = NULL_TREE;
2588 while (true)
2590 /* Declaring one or more declarators or un-named bit-fields. */
2591 struct c_declarator *declarator;
2592 bool dummy = false;
2593 if (c_parser_next_token_is (parser, CPP_COLON))
2594 declarator = build_id_declarator (NULL_TREE);
2595 else
2596 declarator = c_parser_declarator (parser,
2597 specs->typespec_kind != ctsk_none,
2598 C_DTR_NORMAL, &dummy);
2599 if (declarator == NULL)
2601 c_parser_skip_to_end_of_block_or_statement (parser);
2602 break;
2604 if (c_parser_next_token_is (parser, CPP_COLON)
2605 || c_parser_next_token_is (parser, CPP_COMMA)
2606 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2607 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2608 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2610 tree postfix_attrs = NULL_TREE;
2611 tree width = NULL_TREE;
2612 tree d;
2613 if (c_parser_next_token_is (parser, CPP_COLON))
2615 c_parser_consume_token (parser);
2616 width = c_parser_expr_no_commas (parser, NULL).value;
2618 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2619 postfix_attrs = c_parser_attributes (parser);
2620 d = grokfield (c_parser_peek_token (parser)->location,
2621 declarator, specs, width, &all_prefix_attrs);
2622 decl_attributes (&d, chainon (postfix_attrs,
2623 all_prefix_attrs), 0);
2624 DECL_CHAIN (d) = decls;
2625 decls = d;
2626 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2627 all_prefix_attrs = chainon (c_parser_attributes (parser),
2628 prefix_attrs);
2629 else
2630 all_prefix_attrs = prefix_attrs;
2631 if (c_parser_next_token_is (parser, CPP_COMMA))
2632 c_parser_consume_token (parser);
2633 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2634 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2636 /* Semicolon consumed in caller. */
2637 break;
2639 else
2641 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2642 break;
2645 else
2647 c_parser_error (parser,
2648 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2649 "%<__attribute__%>");
2650 break;
2653 return decls;
2656 /* Parse a typeof specifier (a GNU extension).
2658 typeof-specifier:
2659 typeof ( expression )
2660 typeof ( type-name )
2663 static struct c_typespec
2664 c_parser_typeof_specifier (c_parser *parser)
2666 struct c_typespec ret;
2667 ret.kind = ctsk_typeof;
2668 ret.spec = error_mark_node;
2669 ret.expr = NULL_TREE;
2670 ret.expr_const_operands = true;
2671 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2672 c_parser_consume_token (parser);
2673 c_inhibit_evaluation_warnings++;
2674 in_typeof++;
2675 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2677 c_inhibit_evaluation_warnings--;
2678 in_typeof--;
2679 return ret;
2681 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2683 struct c_type_name *type = c_parser_type_name (parser);
2684 c_inhibit_evaluation_warnings--;
2685 in_typeof--;
2686 if (type != NULL)
2688 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2689 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2692 else
2694 bool was_vm;
2695 location_t here = c_parser_peek_token (parser)->location;
2696 struct c_expr expr = c_parser_expression (parser);
2697 c_inhibit_evaluation_warnings--;
2698 in_typeof--;
2699 if (TREE_CODE (expr.value) == COMPONENT_REF
2700 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2701 error_at (here, "%<typeof%> applied to a bit-field");
2702 mark_exp_read (expr.value);
2703 ret.spec = TREE_TYPE (expr.value);
2704 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2705 /* This is returned with the type so that when the type is
2706 evaluated, this can be evaluated. */
2707 if (was_vm)
2708 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2709 pop_maybe_used (was_vm);
2711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2712 return ret;
2715 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2716 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2717 be redeclared; otherwise it may not. KIND indicates which kind of
2718 declarator is wanted. Returns a valid declarator except in the
2719 case of a syntax error in which case NULL is returned. *SEEN_ID is
2720 set to true if an identifier being declared is seen; this is used
2721 to diagnose bad forms of abstract array declarators and to
2722 determine whether an identifier list is syntactically permitted.
2724 declarator:
2725 pointer[opt] direct-declarator
2727 direct-declarator:
2728 identifier
2729 ( attributes[opt] declarator )
2730 direct-declarator array-declarator
2731 direct-declarator ( parameter-type-list )
2732 direct-declarator ( identifier-list[opt] )
2734 pointer:
2735 * type-qualifier-list[opt]
2736 * type-qualifier-list[opt] pointer
2738 type-qualifier-list:
2739 type-qualifier
2740 attributes
2741 type-qualifier-list type-qualifier
2742 type-qualifier-list attributes
2744 parameter-type-list:
2745 parameter-list
2746 parameter-list , ...
2748 parameter-list:
2749 parameter-declaration
2750 parameter-list , parameter-declaration
2752 parameter-declaration:
2753 declaration-specifiers declarator attributes[opt]
2754 declaration-specifiers abstract-declarator[opt] attributes[opt]
2756 identifier-list:
2757 identifier
2758 identifier-list , identifier
2760 abstract-declarator:
2761 pointer
2762 pointer[opt] direct-abstract-declarator
2764 direct-abstract-declarator:
2765 ( attributes[opt] abstract-declarator )
2766 direct-abstract-declarator[opt] array-declarator
2767 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2769 GNU extensions:
2771 direct-declarator:
2772 direct-declarator ( parameter-forward-declarations
2773 parameter-type-list[opt] )
2775 direct-abstract-declarator:
2776 direct-abstract-declarator[opt] ( parameter-forward-declarations
2777 parameter-type-list[opt] )
2779 parameter-forward-declarations:
2780 parameter-list ;
2781 parameter-forward-declarations parameter-list ;
2783 The uses of attributes shown above are GNU extensions.
2785 Some forms of array declarator are not included in C99 in the
2786 syntax for abstract declarators; these are disallowed elsewhere.
2787 This may be a defect (DR#289).
2789 This function also accepts an omitted abstract declarator as being
2790 an abstract declarator, although not part of the formal syntax. */
2792 static struct c_declarator *
2793 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2794 bool *seen_id)
2796 /* Parse any initial pointer part. */
2797 if (c_parser_next_token_is (parser, CPP_MULT))
2799 struct c_declspecs *quals_attrs = build_null_declspecs ();
2800 struct c_declarator *inner;
2801 c_parser_consume_token (parser);
2802 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2803 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2804 if (inner == NULL)
2805 return NULL;
2806 else
2807 return make_pointer_declarator (quals_attrs, inner);
2809 /* Now we have a direct declarator, direct abstract declarator or
2810 nothing (which counts as a direct abstract declarator here). */
2811 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2814 /* Parse a direct declarator or direct abstract declarator; arguments
2815 as c_parser_declarator. */
2817 static struct c_declarator *
2818 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2819 bool *seen_id)
2821 /* The direct declarator must start with an identifier (possibly
2822 omitted) or a parenthesized declarator (possibly abstract). In
2823 an ordinary declarator, initial parentheses must start a
2824 parenthesized declarator. In an abstract declarator or parameter
2825 declarator, they could start a parenthesized declarator or a
2826 parameter list. To tell which, the open parenthesis and any
2827 following attributes must be read. If a declaration specifier
2828 follows, then it is a parameter list; if the specifier is a
2829 typedef name, there might be an ambiguity about redeclaring it,
2830 which is resolved in the direction of treating it as a typedef
2831 name. If a close parenthesis follows, it is also an empty
2832 parameter list, as the syntax does not permit empty abstract
2833 declarators. Otherwise, it is a parenthesized declarator (in
2834 which case the analysis may be repeated inside it, recursively).
2836 ??? There is an ambiguity in a parameter declaration "int
2837 (__attribute__((foo)) x)", where x is not a typedef name: it
2838 could be an abstract declarator for a function, or declare x with
2839 parentheses. The proper resolution of this ambiguity needs
2840 documenting. At present we follow an accident of the old
2841 parser's implementation, whereby the first parameter must have
2842 some declaration specifiers other than just attributes. Thus as
2843 a parameter declaration it is treated as a parenthesized
2844 parameter named x, and as an abstract declarator it is
2845 rejected.
2847 ??? Also following the old parser, attributes inside an empty
2848 parameter list are ignored, making it a list not yielding a
2849 prototype, rather than giving an error or making it have one
2850 parameter with implicit type int.
2852 ??? Also following the old parser, typedef names may be
2853 redeclared in declarators, but not Objective-C class names. */
2855 if (kind != C_DTR_ABSTRACT
2856 && c_parser_next_token_is (parser, CPP_NAME)
2857 && ((type_seen_p
2858 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2859 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2860 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2862 struct c_declarator *inner
2863 = build_id_declarator (c_parser_peek_token (parser)->value);
2864 *seen_id = true;
2865 inner->id_loc = c_parser_peek_token (parser)->location;
2866 c_parser_consume_token (parser);
2867 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2870 if (kind != C_DTR_NORMAL
2871 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2873 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2874 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2877 /* Either we are at the end of an abstract declarator, or we have
2878 parentheses. */
2880 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2882 tree attrs;
2883 struct c_declarator *inner;
2884 c_parser_consume_token (parser);
2885 attrs = c_parser_attributes (parser);
2886 if (kind != C_DTR_NORMAL
2887 && (c_parser_next_token_starts_declspecs (parser)
2888 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2890 struct c_arg_info *args
2891 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2892 attrs);
2893 if (args == NULL)
2894 return NULL;
2895 else
2897 inner
2898 = build_function_declarator (args,
2899 build_id_declarator (NULL_TREE));
2900 return c_parser_direct_declarator_inner (parser, *seen_id,
2901 inner);
2904 /* A parenthesized declarator. */
2905 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2906 if (inner != NULL && attrs != NULL)
2907 inner = build_attrs_declarator (attrs, inner);
2908 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2910 c_parser_consume_token (parser);
2911 if (inner == NULL)
2912 return NULL;
2913 else
2914 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2916 else
2918 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2919 "expected %<)%>");
2920 return NULL;
2923 else
2925 if (kind == C_DTR_NORMAL)
2927 c_parser_error (parser, "expected identifier or %<(%>");
2928 return NULL;
2930 else
2931 return build_id_declarator (NULL_TREE);
2935 /* Parse part of a direct declarator or direct abstract declarator,
2936 given that some (in INNER) has already been parsed; ID_PRESENT is
2937 true if an identifier is present, false for an abstract
2938 declarator. */
2940 static struct c_declarator *
2941 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2942 struct c_declarator *inner)
2944 /* Parse a sequence of array declarators and parameter lists. */
2945 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2947 location_t brace_loc = c_parser_peek_token (parser)->location;
2948 struct c_declarator *declarator;
2949 struct c_declspecs *quals_attrs = build_null_declspecs ();
2950 bool static_seen;
2951 bool star_seen;
2952 tree dimen;
2953 c_parser_consume_token (parser);
2954 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2955 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2956 if (static_seen)
2957 c_parser_consume_token (parser);
2958 if (static_seen && !quals_attrs->declspecs_seen_p)
2959 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2960 if (!quals_attrs->declspecs_seen_p)
2961 quals_attrs = NULL;
2962 /* If "static" is present, there must be an array dimension.
2963 Otherwise, there may be a dimension, "*", or no
2964 dimension. */
2965 if (static_seen)
2967 star_seen = false;
2968 dimen = c_parser_expr_no_commas (parser, NULL).value;
2970 else
2972 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2974 dimen = NULL_TREE;
2975 star_seen = false;
2977 else if (c_parser_next_token_is (parser, CPP_MULT))
2979 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2981 dimen = NULL_TREE;
2982 star_seen = true;
2983 c_parser_consume_token (parser);
2985 else
2987 star_seen = false;
2988 dimen = c_parser_expr_no_commas (parser, NULL).value;
2991 else
2993 star_seen = false;
2994 dimen = c_parser_expr_no_commas (parser, NULL).value;
2997 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2998 c_parser_consume_token (parser);
2999 else
3001 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3002 "expected %<]%>");
3003 return NULL;
3005 if (dimen)
3006 mark_exp_read (dimen);
3007 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3008 static_seen, star_seen);
3009 if (declarator == NULL)
3010 return NULL;
3011 inner = set_array_declarator_inner (declarator, inner);
3012 return c_parser_direct_declarator_inner (parser, id_present, inner);
3014 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3016 tree attrs;
3017 struct c_arg_info *args;
3018 c_parser_consume_token (parser);
3019 attrs = c_parser_attributes (parser);
3020 args = c_parser_parms_declarator (parser, id_present, attrs);
3021 if (args == NULL)
3022 return NULL;
3023 else
3025 inner = build_function_declarator (args, inner);
3026 return c_parser_direct_declarator_inner (parser, id_present, inner);
3029 return inner;
3032 /* Parse a parameter list or identifier list, including the closing
3033 parenthesis but not the opening one. ATTRS are the attributes at
3034 the start of the list. ID_LIST_OK is true if an identifier list is
3035 acceptable; such a list must not have attributes at the start. */
3037 static struct c_arg_info *
3038 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3040 push_scope ();
3041 declare_parm_level ();
3042 /* If the list starts with an identifier, it is an identifier list.
3043 Otherwise, it is either a prototype list or an empty list. */
3044 if (id_list_ok
3045 && !attrs
3046 && c_parser_next_token_is (parser, CPP_NAME)
3047 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3049 /* Look ahead to detect typos in type names. */
3050 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3051 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3052 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3053 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3055 tree list = NULL_TREE, *nextp = &list;
3056 while (c_parser_next_token_is (parser, CPP_NAME)
3057 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3059 *nextp = build_tree_list (NULL_TREE,
3060 c_parser_peek_token (parser)->value);
3061 nextp = & TREE_CHAIN (*nextp);
3062 c_parser_consume_token (parser);
3063 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3064 break;
3065 c_parser_consume_token (parser);
3066 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3068 c_parser_error (parser, "expected identifier");
3069 break;
3072 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3074 struct c_arg_info *ret = build_arg_info ();
3075 ret->types = list;
3076 c_parser_consume_token (parser);
3077 pop_scope ();
3078 return ret;
3080 else
3082 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3083 "expected %<)%>");
3084 pop_scope ();
3085 return NULL;
3088 else
3090 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
3091 pop_scope ();
3092 return ret;
3096 /* Parse a parameter list (possibly empty), including the closing
3097 parenthesis but not the opening one. ATTRS are the attributes at
3098 the start of the list. */
3100 static struct c_arg_info *
3101 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
3103 bool bad_parm = false;
3104 /* ??? Following the old parser, forward parameter declarations may
3105 use abstract declarators, and if no real parameter declarations
3106 follow the forward declarations then this is not diagnosed. Also
3107 note as above that attributes are ignored as the only contents of
3108 the parentheses, or as the only contents after forward
3109 declarations. */
3110 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3112 struct c_arg_info *ret = build_arg_info ();
3113 c_parser_consume_token (parser);
3114 return ret;
3116 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3118 struct c_arg_info *ret = build_arg_info ();
3119 /* Suppress -Wold-style-definition for this case. */
3120 ret->types = error_mark_node;
3121 error_at (c_parser_peek_token (parser)->location,
3122 "ISO C requires a named argument before %<...%>");
3123 c_parser_consume_token (parser);
3124 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3126 c_parser_consume_token (parser);
3127 return ret;
3129 else
3131 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3132 "expected %<)%>");
3133 return NULL;
3136 /* Nonempty list of parameters, either terminated with semicolon
3137 (forward declarations; recurse) or with close parenthesis (normal
3138 function) or with ", ... )" (variadic function). */
3139 while (true)
3141 /* Parse a parameter. */
3142 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3143 attrs = NULL_TREE;
3144 if (parm == NULL)
3145 bad_parm = true;
3146 else
3147 push_parm_decl (parm);
3148 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3150 tree new_attrs;
3151 c_parser_consume_token (parser);
3152 mark_forward_parm_decls ();
3153 new_attrs = c_parser_attributes (parser);
3154 return c_parser_parms_list_declarator (parser, new_attrs);
3156 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3158 c_parser_consume_token (parser);
3159 if (bad_parm)
3161 get_pending_sizes ();
3162 return NULL;
3164 else
3165 return get_parm_info (false);
3167 if (!c_parser_require (parser, CPP_COMMA,
3168 "expected %<;%>, %<,%> or %<)%>"))
3170 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3171 get_pending_sizes ();
3172 return NULL;
3174 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3176 c_parser_consume_token (parser);
3177 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3179 c_parser_consume_token (parser);
3180 if (bad_parm)
3182 get_pending_sizes ();
3183 return NULL;
3185 else
3186 return get_parm_info (true);
3188 else
3190 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3191 "expected %<)%>");
3192 get_pending_sizes ();
3193 return NULL;
3199 /* Parse a parameter declaration. ATTRS are the attributes at the
3200 start of the declaration if it is the first parameter. */
3202 static struct c_parm *
3203 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3205 struct c_declspecs *specs;
3206 struct c_declarator *declarator;
3207 tree prefix_attrs;
3208 tree postfix_attrs = NULL_TREE;
3209 bool dummy = false;
3210 if (!c_parser_next_token_starts_declspecs (parser))
3212 c_token *token = c_parser_peek_token (parser);
3213 if (parser->error)
3214 return NULL;
3215 c_parser_set_source_position_from_token (token);
3216 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3218 error ("unknown type name %qE", token->value);
3219 parser->error = true;
3221 /* ??? In some Objective-C cases '...' isn't applicable so there
3222 should be a different message. */
3223 else
3224 c_parser_error (parser,
3225 "expected declaration specifiers or %<...%>");
3226 c_parser_skip_to_end_of_parameter (parser);
3227 return NULL;
3229 specs = build_null_declspecs ();
3230 if (attrs)
3232 declspecs_add_attrs (specs, attrs);
3233 attrs = NULL_TREE;
3235 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3236 finish_declspecs (specs);
3237 pending_xref_error ();
3238 prefix_attrs = specs->attrs;
3239 specs->attrs = NULL_TREE;
3240 declarator = c_parser_declarator (parser,
3241 specs->typespec_kind != ctsk_none,
3242 C_DTR_PARM, &dummy);
3243 if (declarator == NULL)
3245 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3246 return NULL;
3248 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3249 postfix_attrs = c_parser_attributes (parser);
3250 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3251 declarator);
3254 /* Parse a string literal in an asm expression. It should not be
3255 translated, and wide string literals are an error although
3256 permitted by the syntax. This is a GNU extension.
3258 asm-string-literal:
3259 string-literal
3261 ??? At present, following the old parser, the caller needs to have
3262 set lex_untranslated_string to 1. It would be better to follow the
3263 C++ parser rather than using this kludge. */
3265 static tree
3266 c_parser_asm_string_literal (c_parser *parser)
3268 tree str;
3269 if (c_parser_next_token_is (parser, CPP_STRING))
3271 str = c_parser_peek_token (parser)->value;
3272 c_parser_consume_token (parser);
3274 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3276 error_at (c_parser_peek_token (parser)->location,
3277 "wide string literal in %<asm%>");
3278 str = build_string (1, "");
3279 c_parser_consume_token (parser);
3281 else
3283 c_parser_error (parser, "expected string literal");
3284 str = NULL_TREE;
3286 return str;
3289 /* Parse a simple asm expression. This is used in restricted
3290 contexts, where a full expression with inputs and outputs does not
3291 make sense. This is a GNU extension.
3293 simple-asm-expr:
3294 asm ( asm-string-literal )
3297 static tree
3298 c_parser_simple_asm_expr (c_parser *parser)
3300 tree str;
3301 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3302 /* ??? Follow the C++ parser rather than using the
3303 lex_untranslated_string kludge. */
3304 parser->lex_untranslated_string = true;
3305 c_parser_consume_token (parser);
3306 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3308 parser->lex_untranslated_string = false;
3309 return NULL_TREE;
3311 str = c_parser_asm_string_literal (parser);
3312 parser->lex_untranslated_string = false;
3313 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3316 return NULL_TREE;
3318 return str;
3321 /* Parse (possibly empty) attributes. This is a GNU extension.
3323 attributes:
3324 empty
3325 attributes attribute
3327 attribute:
3328 __attribute__ ( ( attribute-list ) )
3330 attribute-list:
3331 attrib
3332 attribute_list , attrib
3334 attrib:
3335 empty
3336 any-word
3337 any-word ( identifier )
3338 any-word ( identifier , nonempty-expr-list )
3339 any-word ( expr-list )
3341 where the "identifier" must not be declared as a type, and
3342 "any-word" may be any identifier (including one declared as a
3343 type), a reserved word storage class specifier, type specifier or
3344 type qualifier. ??? This still leaves out most reserved keywords
3345 (following the old parser), shouldn't we include them, and why not
3346 allow identifiers declared as types to start the arguments? */
3348 static tree
3349 c_parser_attributes (c_parser *parser)
3351 tree attrs = NULL_TREE;
3352 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3354 /* ??? Follow the C++ parser rather than using the
3355 lex_untranslated_string kludge. */
3356 parser->lex_untranslated_string = true;
3357 c_parser_consume_token (parser);
3358 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3360 parser->lex_untranslated_string = false;
3361 return attrs;
3363 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3365 parser->lex_untranslated_string = false;
3366 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3367 return attrs;
3369 /* Parse the attribute list. */
3370 while (c_parser_next_token_is (parser, CPP_COMMA)
3371 || c_parser_next_token_is (parser, CPP_NAME)
3372 || c_parser_next_token_is (parser, CPP_KEYWORD))
3374 tree attr, attr_name, attr_args;
3375 VEC(tree,gc) *expr_list;
3376 if (c_parser_next_token_is (parser, CPP_COMMA))
3378 c_parser_consume_token (parser);
3379 continue;
3381 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3383 /* ??? See comment above about what keywords are
3384 accepted here. */
3385 bool ok;
3386 switch (c_parser_peek_token (parser)->keyword)
3388 case RID_STATIC:
3389 case RID_UNSIGNED:
3390 case RID_LONG:
3391 case RID_INT128:
3392 case RID_CONST:
3393 case RID_EXTERN:
3394 case RID_REGISTER:
3395 case RID_TYPEDEF:
3396 case RID_SHORT:
3397 case RID_INLINE:
3398 case RID_VOLATILE:
3399 case RID_SIGNED:
3400 case RID_AUTO:
3401 case RID_RESTRICT:
3402 case RID_COMPLEX:
3403 case RID_THREAD:
3404 case RID_INT:
3405 case RID_CHAR:
3406 case RID_FLOAT:
3407 case RID_DOUBLE:
3408 case RID_VOID:
3409 case RID_DFLOAT32:
3410 case RID_DFLOAT64:
3411 case RID_DFLOAT128:
3412 case RID_BOOL:
3413 case RID_FRACT:
3414 case RID_ACCUM:
3415 case RID_SAT:
3416 ok = true;
3417 break;
3418 default:
3419 ok = false;
3420 break;
3422 if (!ok)
3423 break;
3424 /* Accept __attribute__((__const)) as __attribute__((const))
3425 etc. */
3426 attr_name
3427 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3429 else
3430 attr_name = c_parser_peek_token (parser)->value;
3431 c_parser_consume_token (parser);
3432 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3434 attr = build_tree_list (attr_name, NULL_TREE);
3435 attrs = chainon (attrs, attr);
3436 continue;
3438 c_parser_consume_token (parser);
3439 /* Parse the attribute contents. If they start with an
3440 identifier which is followed by a comma or close
3441 parenthesis, then the arguments start with that
3442 identifier; otherwise they are an expression list.
3443 In objective-c the identifier may be a classname. */
3444 if (c_parser_next_token_is (parser, CPP_NAME)
3445 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3446 || (c_dialect_objc ()
3447 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3448 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3449 || (c_parser_peek_2nd_token (parser)->type
3450 == CPP_CLOSE_PAREN)))
3452 tree arg1 = c_parser_peek_token (parser)->value;
3453 c_parser_consume_token (parser);
3454 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3455 attr_args = build_tree_list (NULL_TREE, arg1);
3456 else
3458 tree tree_list;
3459 c_parser_consume_token (parser);
3460 expr_list = c_parser_expr_list (parser, false, true, NULL);
3461 tree_list = build_tree_list_vec (expr_list);
3462 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3463 release_tree_vector (expr_list);
3466 else
3468 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3469 attr_args = NULL_TREE;
3470 else
3472 expr_list = c_parser_expr_list (parser, false, true, NULL);
3473 attr_args = build_tree_list_vec (expr_list);
3474 release_tree_vector (expr_list);
3477 attr = build_tree_list (attr_name, attr_args);
3478 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3479 c_parser_consume_token (parser);
3480 else
3482 parser->lex_untranslated_string = false;
3483 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3484 "expected %<)%>");
3485 return attrs;
3487 attrs = chainon (attrs, attr);
3489 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3490 c_parser_consume_token (parser);
3491 else
3493 parser->lex_untranslated_string = false;
3494 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3495 "expected %<)%>");
3496 return attrs;
3498 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3499 c_parser_consume_token (parser);
3500 else
3502 parser->lex_untranslated_string = false;
3503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3504 "expected %<)%>");
3505 return attrs;
3507 parser->lex_untranslated_string = false;
3509 return attrs;
3512 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3514 type-name:
3515 specifier-qualifier-list abstract-declarator[opt]
3518 static struct c_type_name *
3519 c_parser_type_name (c_parser *parser)
3521 struct c_declspecs *specs = build_null_declspecs ();
3522 struct c_declarator *declarator;
3523 struct c_type_name *ret;
3524 bool dummy = false;
3525 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3526 if (!specs->declspecs_seen_p)
3528 c_parser_error (parser, "expected specifier-qualifier-list");
3529 return NULL;
3531 if (specs->type != error_mark_node)
3533 pending_xref_error ();
3534 finish_declspecs (specs);
3536 declarator = c_parser_declarator (parser,
3537 specs->typespec_kind != ctsk_none,
3538 C_DTR_ABSTRACT, &dummy);
3539 if (declarator == NULL)
3540 return NULL;
3541 ret = XOBNEW (&parser_obstack, struct c_type_name);
3542 ret->specs = specs;
3543 ret->declarator = declarator;
3544 return ret;
3547 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3549 initializer:
3550 assignment-expression
3551 { initializer-list }
3552 { initializer-list , }
3554 initializer-list:
3555 designation[opt] initializer
3556 initializer-list , designation[opt] initializer
3558 designation:
3559 designator-list =
3561 designator-list:
3562 designator
3563 designator-list designator
3565 designator:
3566 array-designator
3567 . identifier
3569 array-designator:
3570 [ constant-expression ]
3572 GNU extensions:
3574 initializer:
3577 designation:
3578 array-designator
3579 identifier :
3581 array-designator:
3582 [ constant-expression ... constant-expression ]
3584 Any expression without commas is accepted in the syntax for the
3585 constant-expressions, with non-constant expressions rejected later.
3587 This function is only used for top-level initializers; for nested
3588 ones, see c_parser_initval. */
3590 static struct c_expr
3591 c_parser_initializer (c_parser *parser)
3593 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3594 return c_parser_braced_init (parser, NULL_TREE, false);
3595 else
3597 struct c_expr ret;
3598 location_t loc = c_parser_peek_token (parser)->location;
3599 ret = c_parser_expr_no_commas (parser, NULL);
3600 if (TREE_CODE (ret.value) != STRING_CST
3601 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3602 ret = default_function_array_read_conversion (loc, ret);
3603 return ret;
3607 /* Parse a braced initializer list. TYPE is the type specified for a
3608 compound literal, and NULL_TREE for other initializers and for
3609 nested braced lists. NESTED_P is true for nested braced lists,
3610 false for the list of a compound literal or the list that is the
3611 top-level initializer in a declaration. */
3613 static struct c_expr
3614 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3616 struct c_expr ret;
3617 struct obstack braced_init_obstack;
3618 location_t brace_loc = c_parser_peek_token (parser)->location;
3619 gcc_obstack_init (&braced_init_obstack);
3620 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3621 c_parser_consume_token (parser);
3622 if (nested_p)
3623 push_init_level (0, &braced_init_obstack);
3624 else
3625 really_start_incremental_init (type);
3626 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3628 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3630 else
3632 /* Parse a non-empty initializer list, possibly with a trailing
3633 comma. */
3634 while (true)
3636 c_parser_initelt (parser, &braced_init_obstack);
3637 if (parser->error)
3638 break;
3639 if (c_parser_next_token_is (parser, CPP_COMMA))
3640 c_parser_consume_token (parser);
3641 else
3642 break;
3643 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3644 break;
3647 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3649 ret.value = error_mark_node;
3650 ret.original_code = ERROR_MARK;
3651 ret.original_type = NULL;
3652 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3653 pop_init_level (0, &braced_init_obstack);
3654 obstack_free (&braced_init_obstack, NULL);
3655 return ret;
3657 c_parser_consume_token (parser);
3658 ret = pop_init_level (0, &braced_init_obstack);
3659 obstack_free (&braced_init_obstack, NULL);
3660 return ret;
3663 /* Parse a nested initializer, including designators. */
3665 static void
3666 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3668 /* Parse any designator or designator list. A single array
3669 designator may have the subsequent "=" omitted in GNU C, but a
3670 longer list or a structure member designator may not. */
3671 if (c_parser_next_token_is (parser, CPP_NAME)
3672 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3674 /* Old-style structure member designator. */
3675 set_init_label (c_parser_peek_token (parser)->value,
3676 braced_init_obstack);
3677 /* Use the colon as the error location. */
3678 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3679 "obsolete use of designated initializer with %<:%>");
3680 c_parser_consume_token (parser);
3681 c_parser_consume_token (parser);
3683 else
3685 /* des_seen is 0 if there have been no designators, 1 if there
3686 has been a single array designator and 2 otherwise. */
3687 int des_seen = 0;
3688 /* Location of a designator. */
3689 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3690 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3691 || c_parser_next_token_is (parser, CPP_DOT))
3693 int des_prev = des_seen;
3694 if (!des_seen)
3695 des_loc = c_parser_peek_token (parser)->location;
3696 if (des_seen < 2)
3697 des_seen++;
3698 if (c_parser_next_token_is (parser, CPP_DOT))
3700 des_seen = 2;
3701 c_parser_consume_token (parser);
3702 if (c_parser_next_token_is (parser, CPP_NAME))
3704 set_init_label (c_parser_peek_token (parser)->value,
3705 braced_init_obstack);
3706 c_parser_consume_token (parser);
3708 else
3710 struct c_expr init;
3711 init.value = error_mark_node;
3712 init.original_code = ERROR_MARK;
3713 init.original_type = NULL;
3714 c_parser_error (parser, "expected identifier");
3715 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3716 process_init_element (init, false, braced_init_obstack);
3717 return;
3720 else
3722 tree first, second;
3723 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3724 /* ??? Following the old parser, [ objc-receiver
3725 objc-message-args ] is accepted as an initializer,
3726 being distinguished from a designator by what follows
3727 the first assignment expression inside the square
3728 brackets, but after a first array designator a
3729 subsequent square bracket is for Objective-C taken to
3730 start an expression, using the obsolete form of
3731 designated initializer without '=', rather than
3732 possibly being a second level of designation: in LALR
3733 terms, the '[' is shifted rather than reducing
3734 designator to designator-list. */
3735 if (des_prev == 1 && c_dialect_objc ())
3737 des_seen = des_prev;
3738 break;
3740 if (des_prev == 0 && c_dialect_objc ())
3742 /* This might be an array designator or an
3743 Objective-C message expression. If the former,
3744 continue parsing here; if the latter, parse the
3745 remainder of the initializer given the starting
3746 primary-expression. ??? It might make sense to
3747 distinguish when des_prev == 1 as well; see
3748 previous comment. */
3749 tree rec, args;
3750 struct c_expr mexpr;
3751 c_parser_consume_token (parser);
3752 if (c_parser_peek_token (parser)->type == CPP_NAME
3753 && ((c_parser_peek_token (parser)->id_kind
3754 == C_ID_TYPENAME)
3755 || (c_parser_peek_token (parser)->id_kind
3756 == C_ID_CLASSNAME)))
3758 /* Type name receiver. */
3759 tree id = c_parser_peek_token (parser)->value;
3760 c_parser_consume_token (parser);
3761 rec = objc_get_class_reference (id);
3762 goto parse_message_args;
3764 first = c_parser_expr_no_commas (parser, NULL).value;
3765 mark_exp_read (first);
3766 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3767 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3768 goto array_desig_after_first;
3769 /* Expression receiver. So far only one part
3770 without commas has been parsed; there might be
3771 more of the expression. */
3772 rec = first;
3773 while (c_parser_next_token_is (parser, CPP_COMMA))
3775 struct c_expr next;
3776 location_t comma_loc, exp_loc;
3777 comma_loc = c_parser_peek_token (parser)->location;
3778 c_parser_consume_token (parser);
3779 exp_loc = c_parser_peek_token (parser)->location;
3780 next = c_parser_expr_no_commas (parser, NULL);
3781 next = default_function_array_read_conversion (exp_loc,
3782 next);
3783 rec = build_compound_expr (comma_loc, rec, next.value);
3785 parse_message_args:
3786 /* Now parse the objc-message-args. */
3787 args = c_parser_objc_message_args (parser);
3788 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3789 "expected %<]%>");
3790 mexpr.value
3791 = objc_build_message_expr (build_tree_list (rec, args));
3792 mexpr.original_code = ERROR_MARK;
3793 mexpr.original_type = NULL;
3794 /* Now parse and process the remainder of the
3795 initializer, starting with this message
3796 expression as a primary-expression. */
3797 c_parser_initval (parser, &mexpr, braced_init_obstack);
3798 return;
3800 c_parser_consume_token (parser);
3801 first = c_parser_expr_no_commas (parser, NULL).value;
3802 mark_exp_read (first);
3803 array_desig_after_first:
3804 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3806 ellipsis_loc = c_parser_peek_token (parser)->location;
3807 c_parser_consume_token (parser);
3808 second = c_parser_expr_no_commas (parser, NULL).value;
3809 mark_exp_read (second);
3811 else
3812 second = NULL_TREE;
3813 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3815 c_parser_consume_token (parser);
3816 set_init_index (first, second, braced_init_obstack);
3817 if (second)
3818 pedwarn (ellipsis_loc, OPT_pedantic,
3819 "ISO C forbids specifying range of elements to initialize");
3821 else
3822 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3823 "expected %<]%>");
3826 if (des_seen >= 1)
3828 if (c_parser_next_token_is (parser, CPP_EQ))
3830 if (!flag_isoc99)
3831 pedwarn (des_loc, OPT_pedantic,
3832 "ISO C90 forbids specifying subobject to initialize");
3833 c_parser_consume_token (parser);
3835 else
3837 if (des_seen == 1)
3838 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3839 "obsolete use of designated initializer without %<=%>");
3840 else
3842 struct c_expr init;
3843 init.value = error_mark_node;
3844 init.original_code = ERROR_MARK;
3845 init.original_type = NULL;
3846 c_parser_error (parser, "expected %<=%>");
3847 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3848 process_init_element (init, false, braced_init_obstack);
3849 return;
3854 c_parser_initval (parser, NULL, braced_init_obstack);
3857 /* Parse a nested initializer; as c_parser_initializer but parses
3858 initializers within braced lists, after any designators have been
3859 applied. If AFTER is not NULL then it is an Objective-C message
3860 expression which is the primary-expression starting the
3861 initializer. */
3863 static void
3864 c_parser_initval (c_parser *parser, struct c_expr *after,
3865 struct obstack * braced_init_obstack)
3867 struct c_expr init;
3868 gcc_assert (!after || c_dialect_objc ());
3869 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3870 init = c_parser_braced_init (parser, NULL_TREE, true);
3871 else
3873 location_t loc = c_parser_peek_token (parser)->location;
3874 init = c_parser_expr_no_commas (parser, after);
3875 if (init.value != NULL_TREE
3876 && TREE_CODE (init.value) != STRING_CST
3877 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3878 init = default_function_array_read_conversion (loc, init);
3880 process_init_element (init, false, braced_init_obstack);
3883 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3884 C99 6.8.2).
3886 compound-statement:
3887 { block-item-list[opt] }
3888 { label-declarations block-item-list }
3890 block-item-list:
3891 block-item
3892 block-item-list block-item
3894 block-item:
3895 nested-declaration
3896 statement
3898 nested-declaration:
3899 declaration
3901 GNU extensions:
3903 compound-statement:
3904 { label-declarations block-item-list }
3906 nested-declaration:
3907 __extension__ nested-declaration
3908 nested-function-definition
3910 label-declarations:
3911 label-declaration
3912 label-declarations label-declaration
3914 label-declaration:
3915 __label__ identifier-list ;
3917 Allowing the mixing of declarations and code is new in C99. The
3918 GNU syntax also permits (not shown above) labels at the end of
3919 compound statements, which yield an error. We don't allow labels
3920 on declarations; this might seem like a natural extension, but
3921 there would be a conflict between attributes on the label and
3922 prefix attributes on the declaration. ??? The syntax follows the
3923 old parser in requiring something after label declarations.
3924 Although they are erroneous if the labels declared aren't defined,
3925 is it useful for the syntax to be this way?
3927 OpenMP:
3929 block-item:
3930 openmp-directive
3932 openmp-directive:
3933 barrier-directive
3934 flush-directive */
3936 static tree
3937 c_parser_compound_statement (c_parser *parser)
3939 tree stmt;
3940 location_t brace_loc;
3941 brace_loc = c_parser_peek_token (parser)->location;
3942 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3944 /* Ensure a scope is entered and left anyway to avoid confusion
3945 if we have just prepared to enter a function body. */
3946 stmt = c_begin_compound_stmt (true);
3947 c_end_compound_stmt (brace_loc, stmt, true);
3948 return error_mark_node;
3950 stmt = c_begin_compound_stmt (true);
3951 c_parser_compound_statement_nostart (parser);
3952 return c_end_compound_stmt (brace_loc, stmt, true);
3955 /* Parse a compound statement except for the opening brace. This is
3956 used for parsing both compound statements and statement expressions
3957 (which follow different paths to handling the opening). */
3959 static void
3960 c_parser_compound_statement_nostart (c_parser *parser)
3962 bool last_stmt = false;
3963 bool last_label = false;
3964 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3965 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3966 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3968 c_parser_consume_token (parser);
3969 return;
3971 mark_valid_location_for_stdc_pragma (true);
3972 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3974 /* Read zero or more forward-declarations for labels that nested
3975 functions can jump to. */
3976 mark_valid_location_for_stdc_pragma (false);
3977 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3979 label_loc = c_parser_peek_token (parser)->location;
3980 c_parser_consume_token (parser);
3981 /* Any identifiers, including those declared as type names,
3982 are OK here. */
3983 while (true)
3985 tree label;
3986 if (c_parser_next_token_is_not (parser, CPP_NAME))
3988 c_parser_error (parser, "expected identifier");
3989 break;
3991 label
3992 = declare_label (c_parser_peek_token (parser)->value);
3993 C_DECLARED_LABEL_FLAG (label) = 1;
3994 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3995 c_parser_consume_token (parser);
3996 if (c_parser_next_token_is (parser, CPP_COMMA))
3997 c_parser_consume_token (parser);
3998 else
3999 break;
4001 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4003 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
4005 /* We must now have at least one statement, label or declaration. */
4006 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4008 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4009 c_parser_error (parser, "expected declaration or statement");
4010 c_parser_consume_token (parser);
4011 return;
4013 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4015 location_t loc = c_parser_peek_token (parser)->location;
4016 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4017 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4018 || (c_parser_next_token_is (parser, CPP_NAME)
4019 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4021 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4022 label_loc = c_parser_peek_2nd_token (parser)->location;
4023 else
4024 label_loc = c_parser_peek_token (parser)->location;
4025 last_label = true;
4026 last_stmt = false;
4027 mark_valid_location_for_stdc_pragma (false);
4028 c_parser_label (parser);
4030 else if (!last_label
4031 && c_parser_next_tokens_start_declaration (parser))
4033 last_label = false;
4034 mark_valid_location_for_stdc_pragma (false);
4035 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4036 if (last_stmt)
4037 pedwarn_c90 (loc,
4038 (pedantic && !flag_isoc99)
4039 ? OPT_pedantic
4040 : OPT_Wdeclaration_after_statement,
4041 "ISO C90 forbids mixed declarations and code");
4042 last_stmt = false;
4044 else if (!last_label
4045 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4047 /* __extension__ can start a declaration, but is also an
4048 unary operator that can start an expression. Consume all
4049 but the last of a possible series of __extension__ to
4050 determine which. */
4051 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4052 && (c_parser_peek_2nd_token (parser)->keyword
4053 == RID_EXTENSION))
4054 c_parser_consume_token (parser);
4055 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4057 int ext;
4058 ext = disable_extension_diagnostics ();
4059 c_parser_consume_token (parser);
4060 last_label = false;
4061 mark_valid_location_for_stdc_pragma (false);
4062 c_parser_declaration_or_fndef (parser, true, true, true, true,
4063 true, NULL);
4064 /* Following the old parser, __extension__ does not
4065 disable this diagnostic. */
4066 restore_extension_diagnostics (ext);
4067 if (last_stmt)
4068 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4069 ? OPT_pedantic
4070 : OPT_Wdeclaration_after_statement,
4071 "ISO C90 forbids mixed declarations and code");
4072 last_stmt = false;
4074 else
4075 goto statement;
4077 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4079 /* External pragmas, and some omp pragmas, are not associated
4080 with regular c code, and so are not to be considered statements
4081 syntactically. This ensures that the user doesn't put them
4082 places that would turn into syntax errors if the directive
4083 were ignored. */
4084 if (c_parser_pragma (parser, pragma_compound))
4085 last_label = false, last_stmt = true;
4087 else if (c_parser_next_token_is (parser, CPP_EOF))
4089 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4090 c_parser_error (parser, "expected declaration or statement");
4091 return;
4093 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4095 if (parser->in_if_block)
4097 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4098 error_at (loc, """expected %<}%> before %<else%>");
4099 return;
4101 else
4103 error_at (loc, "%<else%> without a previous %<if%>");
4104 c_parser_consume_token (parser);
4105 continue;
4108 else
4110 statement:
4111 last_label = false;
4112 last_stmt = true;
4113 mark_valid_location_for_stdc_pragma (false);
4114 c_parser_statement_after_labels (parser);
4117 parser->error = false;
4119 if (last_label)
4120 error_at (label_loc, "label at end of compound statement");
4121 c_parser_consume_token (parser);
4122 /* Restore the value we started with. */
4123 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4126 /* Parse a label (C90 6.6.1, C99 6.8.1).
4128 label:
4129 identifier : attributes[opt]
4130 case constant-expression :
4131 default :
4133 GNU extensions:
4135 label:
4136 case constant-expression ... constant-expression :
4138 The use of attributes on labels is a GNU extension. The syntax in
4139 GNU C accepts any expressions without commas, non-constant
4140 expressions being rejected later. */
4142 static void
4143 c_parser_label (c_parser *parser)
4145 location_t loc1 = c_parser_peek_token (parser)->location;
4146 tree label = NULL_TREE;
4147 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4149 tree exp1, exp2;
4150 c_parser_consume_token (parser);
4151 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4152 if (c_parser_next_token_is (parser, CPP_COLON))
4154 c_parser_consume_token (parser);
4155 label = do_case (loc1, exp1, NULL_TREE);
4157 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4159 c_parser_consume_token (parser);
4160 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4161 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4162 label = do_case (loc1, exp1, exp2);
4164 else
4165 c_parser_error (parser, "expected %<:%> or %<...%>");
4167 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4169 c_parser_consume_token (parser);
4170 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4171 label = do_case (loc1, NULL_TREE, NULL_TREE);
4173 else
4175 tree name = c_parser_peek_token (parser)->value;
4176 tree tlab;
4177 tree attrs;
4178 location_t loc2 = c_parser_peek_token (parser)->location;
4179 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4180 c_parser_consume_token (parser);
4181 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4182 c_parser_consume_token (parser);
4183 attrs = c_parser_attributes (parser);
4184 tlab = define_label (loc2, name);
4185 if (tlab)
4187 decl_attributes (&tlab, attrs, 0);
4188 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4191 if (label)
4193 if (c_parser_next_tokens_start_declaration (parser))
4195 error_at (c_parser_peek_token (parser)->location,
4196 "a label can only be part of a statement and "
4197 "a declaration is not a statement");
4198 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4199 /*static_assert_ok*/ true,
4200 /*nested*/ true, /*empty_ok*/ false,
4201 /*start_attr_ok*/ true, NULL);
4206 /* Parse a statement (C90 6.6, C99 6.8).
4208 statement:
4209 labeled-statement
4210 compound-statement
4211 expression-statement
4212 selection-statement
4213 iteration-statement
4214 jump-statement
4216 labeled-statement:
4217 label statement
4219 expression-statement:
4220 expression[opt] ;
4222 selection-statement:
4223 if-statement
4224 switch-statement
4226 iteration-statement:
4227 while-statement
4228 do-statement
4229 for-statement
4231 jump-statement:
4232 goto identifier ;
4233 continue ;
4234 break ;
4235 return expression[opt] ;
4237 GNU extensions:
4239 statement:
4240 asm-statement
4242 jump-statement:
4243 goto * expression ;
4245 Objective-C:
4247 statement:
4248 objc-throw-statement
4249 objc-try-catch-statement
4250 objc-synchronized-statement
4252 objc-throw-statement:
4253 @throw expression ;
4254 @throw ;
4256 OpenMP:
4258 statement:
4259 openmp-construct
4261 openmp-construct:
4262 parallel-construct
4263 for-construct
4264 sections-construct
4265 single-construct
4266 parallel-for-construct
4267 parallel-sections-construct
4268 master-construct
4269 critical-construct
4270 atomic-construct
4271 ordered-construct
4273 parallel-construct:
4274 parallel-directive structured-block
4276 for-construct:
4277 for-directive iteration-statement
4279 sections-construct:
4280 sections-directive section-scope
4282 single-construct:
4283 single-directive structured-block
4285 parallel-for-construct:
4286 parallel-for-directive iteration-statement
4288 parallel-sections-construct:
4289 parallel-sections-directive section-scope
4291 master-construct:
4292 master-directive structured-block
4294 critical-construct:
4295 critical-directive structured-block
4297 atomic-construct:
4298 atomic-directive expression-statement
4300 ordered-construct:
4301 ordered-directive structured-block */
4303 static void
4304 c_parser_statement (c_parser *parser)
4306 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4307 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4308 || (c_parser_next_token_is (parser, CPP_NAME)
4309 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4310 c_parser_label (parser);
4311 c_parser_statement_after_labels (parser);
4314 /* Parse a statement, other than a labeled statement. */
4316 static void
4317 c_parser_statement_after_labels (c_parser *parser)
4319 location_t loc = c_parser_peek_token (parser)->location;
4320 tree stmt = NULL_TREE;
4321 bool in_if_block = parser->in_if_block;
4322 parser->in_if_block = false;
4323 switch (c_parser_peek_token (parser)->type)
4325 case CPP_OPEN_BRACE:
4326 add_stmt (c_parser_compound_statement (parser));
4327 break;
4328 case CPP_KEYWORD:
4329 switch (c_parser_peek_token (parser)->keyword)
4331 case RID_IF:
4332 c_parser_if_statement (parser);
4333 break;
4334 case RID_SWITCH:
4335 c_parser_switch_statement (parser);
4336 break;
4337 case RID_WHILE:
4338 c_parser_while_statement (parser);
4339 break;
4340 case RID_DO:
4341 c_parser_do_statement (parser);
4342 break;
4343 case RID_FOR:
4344 c_parser_for_statement (parser);
4345 break;
4346 case RID_GOTO:
4347 c_parser_consume_token (parser);
4348 if (c_parser_next_token_is (parser, CPP_NAME))
4350 stmt = c_finish_goto_label (loc,
4351 c_parser_peek_token (parser)->value);
4352 c_parser_consume_token (parser);
4354 else if (c_parser_next_token_is (parser, CPP_MULT))
4356 tree val;
4358 c_parser_consume_token (parser);
4359 val = c_parser_expression (parser).value;
4360 mark_exp_read (val);
4361 stmt = c_finish_goto_ptr (loc, val);
4363 else
4364 c_parser_error (parser, "expected identifier or %<*%>");
4365 goto expect_semicolon;
4366 case RID_CONTINUE:
4367 c_parser_consume_token (parser);
4368 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4369 goto expect_semicolon;
4370 case RID_BREAK:
4371 c_parser_consume_token (parser);
4372 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4373 goto expect_semicolon;
4374 case RID_RETURN:
4375 c_parser_consume_token (parser);
4376 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4378 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4379 c_parser_consume_token (parser);
4381 else
4383 struct c_expr expr = c_parser_expression_conv (parser);
4384 mark_exp_read (expr.value);
4385 stmt = c_finish_return (loc, expr.value, expr.original_type);
4386 goto expect_semicolon;
4388 break;
4389 case RID_ASM:
4390 stmt = c_parser_asm_statement (parser);
4391 break;
4392 case RID_AT_THROW:
4393 gcc_assert (c_dialect_objc ());
4394 c_parser_consume_token (parser);
4395 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4397 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4398 c_parser_consume_token (parser);
4400 else
4402 tree expr = c_parser_expression (parser).value;
4403 expr = c_fully_fold (expr, false, NULL);
4404 stmt = objc_build_throw_stmt (loc, expr);
4405 goto expect_semicolon;
4407 break;
4408 case RID_AT_TRY:
4409 gcc_assert (c_dialect_objc ());
4410 c_parser_objc_try_catch_finally_statement (parser);
4411 break;
4412 case RID_AT_SYNCHRONIZED:
4413 gcc_assert (c_dialect_objc ());
4414 c_parser_objc_synchronized_statement (parser);
4415 break;
4416 default:
4417 goto expr_stmt;
4419 break;
4420 case CPP_SEMICOLON:
4421 c_parser_consume_token (parser);
4422 break;
4423 case CPP_CLOSE_PAREN:
4424 case CPP_CLOSE_SQUARE:
4425 /* Avoid infinite loop in error recovery:
4426 c_parser_skip_until_found stops at a closing nesting
4427 delimiter without consuming it, but here we need to consume
4428 it to proceed further. */
4429 c_parser_error (parser, "expected statement");
4430 c_parser_consume_token (parser);
4431 break;
4432 case CPP_PRAGMA:
4433 c_parser_pragma (parser, pragma_stmt);
4434 break;
4435 default:
4436 expr_stmt:
4437 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4438 expect_semicolon:
4439 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4440 break;
4442 /* Two cases cannot and do not have line numbers associated: If stmt
4443 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4444 cannot hold line numbers. But that's OK because the statement
4445 will either be changed to a MODIFY_EXPR during gimplification of
4446 the statement expr, or discarded. If stmt was compound, but
4447 without new variables, we will have skipped the creation of a
4448 BIND and will have a bare STATEMENT_LIST. But that's OK because
4449 (recursively) all of the component statements should already have
4450 line numbers assigned. ??? Can we discard no-op statements
4451 earlier? */
4452 if (CAN_HAVE_LOCATION_P (stmt)
4453 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4454 SET_EXPR_LOCATION (stmt, loc);
4456 parser->in_if_block = in_if_block;
4459 /* Parse the condition from an if, do, while or for statements. */
4461 static tree
4462 c_parser_condition (c_parser *parser)
4464 location_t loc = c_parser_peek_token (parser)->location;
4465 tree cond;
4466 cond = c_parser_expression_conv (parser).value;
4467 cond = c_objc_common_truthvalue_conversion (loc, cond);
4468 cond = c_fully_fold (cond, false, NULL);
4469 if (warn_sequence_point)
4470 verify_sequence_points (cond);
4471 return cond;
4474 /* Parse a parenthesized condition from an if, do or while statement.
4476 condition:
4477 ( expression )
4479 static tree
4480 c_parser_paren_condition (c_parser *parser)
4482 tree cond;
4483 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4484 return error_mark_node;
4485 cond = c_parser_condition (parser);
4486 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4487 return cond;
4490 /* Parse a statement which is a block in C99. */
4492 static tree
4493 c_parser_c99_block_statement (c_parser *parser)
4495 tree block = c_begin_compound_stmt (flag_isoc99);
4496 location_t loc = c_parser_peek_token (parser)->location;
4497 c_parser_statement (parser);
4498 return c_end_compound_stmt (loc, block, flag_isoc99);
4501 /* Parse the body of an if statement. This is just parsing a
4502 statement but (a) it is a block in C99, (b) we track whether the
4503 body is an if statement for the sake of -Wparentheses warnings, (c)
4504 we handle an empty body specially for the sake of -Wempty-body
4505 warnings, and (d) we call parser_compound_statement directly
4506 because c_parser_statement_after_labels resets
4507 parser->in_if_block. */
4509 static tree
4510 c_parser_if_body (c_parser *parser, bool *if_p)
4512 tree block = c_begin_compound_stmt (flag_isoc99);
4513 location_t body_loc = c_parser_peek_token (parser)->location;
4514 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4515 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4516 || (c_parser_next_token_is (parser, CPP_NAME)
4517 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4518 c_parser_label (parser);
4519 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4520 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4522 location_t loc = c_parser_peek_token (parser)->location;
4523 add_stmt (build_empty_stmt (loc));
4524 c_parser_consume_token (parser);
4525 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4526 warning_at (loc, OPT_Wempty_body,
4527 "suggest braces around empty body in an %<if%> statement");
4529 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4530 add_stmt (c_parser_compound_statement (parser));
4531 else
4532 c_parser_statement_after_labels (parser);
4533 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4536 /* Parse the else body of an if statement. This is just parsing a
4537 statement but (a) it is a block in C99, (b) we handle an empty body
4538 specially for the sake of -Wempty-body warnings. */
4540 static tree
4541 c_parser_else_body (c_parser *parser)
4543 location_t else_loc = c_parser_peek_token (parser)->location;
4544 tree block = c_begin_compound_stmt (flag_isoc99);
4545 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4546 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4547 || (c_parser_next_token_is (parser, CPP_NAME)
4548 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4549 c_parser_label (parser);
4550 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4552 location_t loc = c_parser_peek_token (parser)->location;
4553 warning_at (loc,
4554 OPT_Wempty_body,
4555 "suggest braces around empty body in an %<else%> statement");
4556 add_stmt (build_empty_stmt (loc));
4557 c_parser_consume_token (parser);
4559 else
4560 c_parser_statement_after_labels (parser);
4561 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4564 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4566 if-statement:
4567 if ( expression ) statement
4568 if ( expression ) statement else statement
4571 static void
4572 c_parser_if_statement (c_parser *parser)
4574 tree block;
4575 location_t loc;
4576 tree cond;
4577 bool first_if = false;
4578 tree first_body, second_body;
4579 bool in_if_block;
4581 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4582 c_parser_consume_token (parser);
4583 block = c_begin_compound_stmt (flag_isoc99);
4584 loc = c_parser_peek_token (parser)->location;
4585 cond = c_parser_paren_condition (parser);
4586 in_if_block = parser->in_if_block;
4587 parser->in_if_block = true;
4588 first_body = c_parser_if_body (parser, &first_if);
4589 parser->in_if_block = in_if_block;
4590 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4592 c_parser_consume_token (parser);
4593 second_body = c_parser_else_body (parser);
4595 else
4596 second_body = NULL_TREE;
4597 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4598 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4601 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4603 switch-statement:
4604 switch (expression) statement
4607 static void
4608 c_parser_switch_statement (c_parser *parser)
4610 tree block, expr, body, save_break;
4611 location_t switch_loc = c_parser_peek_token (parser)->location;
4612 location_t switch_cond_loc;
4613 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4614 c_parser_consume_token (parser);
4615 block = c_begin_compound_stmt (flag_isoc99);
4616 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4618 switch_cond_loc = c_parser_peek_token (parser)->location;
4619 expr = c_parser_expression (parser).value;
4620 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4622 else
4624 switch_cond_loc = UNKNOWN_LOCATION;
4625 expr = error_mark_node;
4627 c_start_case (switch_loc, switch_cond_loc, expr);
4628 save_break = c_break_label;
4629 c_break_label = NULL_TREE;
4630 body = c_parser_c99_block_statement (parser);
4631 c_finish_case (body);
4632 if (c_break_label)
4634 location_t here = c_parser_peek_token (parser)->location;
4635 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4636 SET_EXPR_LOCATION (t, here);
4637 add_stmt (t);
4639 c_break_label = save_break;
4640 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4643 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4645 while-statement:
4646 while (expression) statement
4649 static void
4650 c_parser_while_statement (c_parser *parser)
4652 tree block, cond, body, save_break, save_cont;
4653 location_t loc;
4654 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4655 c_parser_consume_token (parser);
4656 block = c_begin_compound_stmt (flag_isoc99);
4657 loc = c_parser_peek_token (parser)->location;
4658 cond = c_parser_paren_condition (parser);
4659 save_break = c_break_label;
4660 c_break_label = NULL_TREE;
4661 save_cont = c_cont_label;
4662 c_cont_label = NULL_TREE;
4663 body = c_parser_c99_block_statement (parser);
4664 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4665 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4666 c_break_label = save_break;
4667 c_cont_label = save_cont;
4670 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4672 do-statement:
4673 do statement while ( expression ) ;
4676 static void
4677 c_parser_do_statement (c_parser *parser)
4679 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4680 location_t loc;
4681 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4682 c_parser_consume_token (parser);
4683 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4684 warning_at (c_parser_peek_token (parser)->location,
4685 OPT_Wempty_body,
4686 "suggest braces around empty body in %<do%> statement");
4687 block = c_begin_compound_stmt (flag_isoc99);
4688 loc = c_parser_peek_token (parser)->location;
4689 save_break = c_break_label;
4690 c_break_label = NULL_TREE;
4691 save_cont = c_cont_label;
4692 c_cont_label = NULL_TREE;
4693 body = c_parser_c99_block_statement (parser);
4694 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4695 new_break = c_break_label;
4696 c_break_label = save_break;
4697 new_cont = c_cont_label;
4698 c_cont_label = save_cont;
4699 cond = c_parser_paren_condition (parser);
4700 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4701 c_parser_skip_to_end_of_block_or_statement (parser);
4702 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4703 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4706 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4708 for-statement:
4709 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4710 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4712 The form with a declaration is new in C99.
4714 ??? In accordance with the old parser, the declaration may be a
4715 nested function, which is then rejected in check_for_loop_decls,
4716 but does it make any sense for this to be included in the grammar?
4717 Note in particular that the nested function does not include a
4718 trailing ';', whereas the "declaration" production includes one.
4719 Also, can we reject bad declarations earlier and cheaper than
4720 check_for_loop_decls?
4722 In Objective-C, there are two additional variants:
4724 foreach-statement:
4725 for ( expression in expresssion ) statement
4726 for ( declaration in expression ) statement
4728 This is inconsistent with C, because the second variant is allowed
4729 even if c99 is not enabled.
4731 The rest of the comment documents these Objective-C foreach-statement.
4733 Here is the canonical example of the first variant:
4734 for (object in array) { do something with object }
4735 we call the first expression ("object") the "object_expression" and
4736 the second expression ("array") the "collection_expression".
4737 object_expression must be an lvalue of type "id" (a generic Objective-C
4738 object) because the loop works by assigning to object_expression the
4739 various objects from the collection_expression. collection_expression
4740 must evaluate to something of type "id" which responds to the method
4741 countByEnumeratingWithState:objects:count:.
4743 The canonical example of the second variant is:
4744 for (id object in array) { do something with object }
4745 which is completely equivalent to
4747 id object;
4748 for (object in array) { do something with object }
4750 Note that initizializing 'object' in some way (eg, "for ((object =
4751 xxx) in array) { do something with object }") is possibly
4752 technically valid, but completely pointless as 'object' will be
4753 assigned to something else as soon as the loop starts. We should
4754 most likely reject it (TODO).
4756 The beginning of the Objective-C foreach-statement looks exactly
4757 like the beginning of the for-statement, and we can tell it is a
4758 foreach-statement only because the initial declaration or
4759 expression is terminated by 'in' instead of ';'.
4762 static void
4763 c_parser_for_statement (c_parser *parser)
4765 tree block, cond, incr, save_break, save_cont, body;
4766 /* The following are only used when parsing an ObjC foreach statement. */
4767 tree object_expression, collection_expression;
4768 location_t loc = c_parser_peek_token (parser)->location;
4769 location_t for_loc = c_parser_peek_token (parser)->location;
4770 bool is_foreach_statement = false;
4771 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4772 c_parser_consume_token (parser);
4773 /* Open a compound statement in Objective-C as well, just in case this is
4774 as foreach expression. */
4775 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4776 cond = error_mark_node;
4777 incr = error_mark_node;
4778 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4780 /* Parse the initialization declaration or expression. */
4781 object_expression = error_mark_node;
4782 parser->objc_could_be_foreach_context = c_dialect_objc ();
4783 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4785 parser->objc_could_be_foreach_context = false;
4786 c_parser_consume_token (parser);
4787 c_finish_expr_stmt (loc, NULL_TREE);
4789 else if (c_parser_next_tokens_start_declaration (parser))
4791 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4792 &object_expression);
4793 parser->objc_could_be_foreach_context = false;
4795 if (c_parser_next_token_is_keyword (parser, RID_IN))
4797 c_parser_consume_token (parser);
4798 is_foreach_statement = true;
4799 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4800 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4802 else
4803 check_for_loop_decls (for_loc, flag_isoc99);
4805 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4807 /* __extension__ can start a declaration, but is also an
4808 unary operator that can start an expression. Consume all
4809 but the last of a possible series of __extension__ to
4810 determine which. */
4811 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4812 && (c_parser_peek_2nd_token (parser)->keyword
4813 == RID_EXTENSION))
4814 c_parser_consume_token (parser);
4815 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4817 int ext;
4818 ext = disable_extension_diagnostics ();
4819 c_parser_consume_token (parser);
4820 c_parser_declaration_or_fndef (parser, true, true, true, true,
4821 true, &object_expression);
4822 parser->objc_could_be_foreach_context = false;
4824 restore_extension_diagnostics (ext);
4825 if (c_parser_next_token_is_keyword (parser, RID_IN))
4827 c_parser_consume_token (parser);
4828 is_foreach_statement = true;
4829 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4830 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4832 else
4833 check_for_loop_decls (for_loc, flag_isoc99);
4835 else
4836 goto init_expr;
4838 else
4840 init_expr:
4842 tree init_expression;
4843 init_expression = c_parser_expression (parser).value;
4844 parser->objc_could_be_foreach_context = false;
4845 if (c_parser_next_token_is_keyword (parser, RID_IN))
4847 c_parser_consume_token (parser);
4848 is_foreach_statement = true;
4849 if (! lvalue_p (init_expression))
4850 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4851 object_expression = c_fully_fold (init_expression, false, NULL);
4853 else
4855 c_finish_expr_stmt (loc, init_expression);
4856 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4860 /* Parse the loop condition. In the case of a foreach
4861 statement, there is no loop condition. */
4862 gcc_assert (!parser->objc_could_be_foreach_context);
4863 if (!is_foreach_statement)
4865 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4867 c_parser_consume_token (parser);
4868 cond = NULL_TREE;
4870 else
4872 cond = c_parser_condition (parser);
4873 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4876 /* Parse the increment expression (the third expression in a
4877 for-statement). In the case of a foreach-statement, this is
4878 the expression that follows the 'in'. */
4879 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4881 if (is_foreach_statement)
4883 c_parser_error (parser, "missing collection in fast enumeration");
4884 collection_expression = error_mark_node;
4886 else
4887 incr = c_process_expr_stmt (loc, NULL_TREE);
4889 else
4891 if (is_foreach_statement)
4892 collection_expression = c_fully_fold (c_parser_expression (parser).value,
4893 false, NULL);
4894 else
4895 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4899 save_break = c_break_label;
4900 c_break_label = NULL_TREE;
4901 save_cont = c_cont_label;
4902 c_cont_label = NULL_TREE;
4903 body = c_parser_c99_block_statement (parser);
4904 if (is_foreach_statement)
4905 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4906 else
4907 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4908 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4909 c_break_label = save_break;
4910 c_cont_label = save_cont;
4913 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4914 statement with inputs, outputs, clobbers, and volatile tag
4915 allowed.
4917 asm-statement:
4918 asm type-qualifier[opt] ( asm-argument ) ;
4919 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4921 asm-argument:
4922 asm-string-literal
4923 asm-string-literal : asm-operands[opt]
4924 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4925 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4927 asm-goto-argument:
4928 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4929 : asm-goto-operands
4931 Qualifiers other than volatile are accepted in the syntax but
4932 warned for. */
4934 static tree
4935 c_parser_asm_statement (c_parser *parser)
4937 tree quals, str, outputs, inputs, clobbers, labels, ret;
4938 bool simple, is_goto;
4939 location_t asm_loc = c_parser_peek_token (parser)->location;
4940 int section, nsections;
4942 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4943 c_parser_consume_token (parser);
4944 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4946 quals = c_parser_peek_token (parser)->value;
4947 c_parser_consume_token (parser);
4949 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4950 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4952 warning_at (c_parser_peek_token (parser)->location,
4954 "%E qualifier ignored on asm",
4955 c_parser_peek_token (parser)->value);
4956 quals = NULL_TREE;
4957 c_parser_consume_token (parser);
4959 else
4960 quals = NULL_TREE;
4962 is_goto = false;
4963 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4965 c_parser_consume_token (parser);
4966 is_goto = true;
4969 /* ??? Follow the C++ parser rather than using the
4970 lex_untranslated_string kludge. */
4971 parser->lex_untranslated_string = true;
4972 ret = NULL;
4974 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4975 goto error;
4977 str = c_parser_asm_string_literal (parser);
4978 if (str == NULL_TREE)
4979 goto error_close_paren;
4981 simple = true;
4982 outputs = NULL_TREE;
4983 inputs = NULL_TREE;
4984 clobbers = NULL_TREE;
4985 labels = NULL_TREE;
4987 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4988 goto done_asm;
4990 /* Parse each colon-delimited section of operands. */
4991 nsections = 3 + is_goto;
4992 for (section = 0; section < nsections; ++section)
4994 if (!c_parser_require (parser, CPP_COLON,
4995 is_goto
4996 ? "expected %<:%>"
4997 : "expected %<:%> or %<)%>"))
4998 goto error_close_paren;
5000 /* Once past any colon, we're no longer a simple asm. */
5001 simple = false;
5003 if ((!c_parser_next_token_is (parser, CPP_COLON)
5004 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5005 || section == 3)
5006 switch (section)
5008 case 0:
5009 /* For asm goto, we don't allow output operands, but reserve
5010 the slot for a future extension that does allow them. */
5011 if (!is_goto)
5012 outputs = c_parser_asm_operands (parser, false);
5013 break;
5014 case 1:
5015 inputs = c_parser_asm_operands (parser, true);
5016 break;
5017 case 2:
5018 clobbers = c_parser_asm_clobbers (parser);
5019 break;
5020 case 3:
5021 labels = c_parser_asm_goto_operands (parser);
5022 break;
5023 default:
5024 gcc_unreachable ();
5027 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5028 goto done_asm;
5031 done_asm:
5032 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5035 goto error;
5038 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5039 c_parser_skip_to_end_of_block_or_statement (parser);
5041 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5042 clobbers, labels, simple));
5044 error:
5045 parser->lex_untranslated_string = false;
5046 return ret;
5048 error_close_paren:
5049 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5050 goto error;
5053 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5054 not outputs), apply the default conversion of functions and arrays
5055 to pointers.
5057 asm-operands:
5058 asm-operand
5059 asm-operands , asm-operand
5061 asm-operand:
5062 asm-string-literal ( expression )
5063 [ identifier ] asm-string-literal ( expression )
5066 static tree
5067 c_parser_asm_operands (c_parser *parser, bool convert_p)
5069 tree list = NULL_TREE;
5070 location_t loc;
5071 while (true)
5073 tree name, str;
5074 struct c_expr expr;
5075 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5077 c_parser_consume_token (parser);
5078 if (c_parser_next_token_is (parser, CPP_NAME))
5080 tree id = c_parser_peek_token (parser)->value;
5081 c_parser_consume_token (parser);
5082 name = build_string (IDENTIFIER_LENGTH (id),
5083 IDENTIFIER_POINTER (id));
5085 else
5087 c_parser_error (parser, "expected identifier");
5088 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5089 return NULL_TREE;
5091 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5092 "expected %<]%>");
5094 else
5095 name = NULL_TREE;
5096 str = c_parser_asm_string_literal (parser);
5097 if (str == NULL_TREE)
5098 return NULL_TREE;
5099 parser->lex_untranslated_string = false;
5100 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5102 parser->lex_untranslated_string = true;
5103 return NULL_TREE;
5105 loc = c_parser_peek_token (parser)->location;
5106 expr = c_parser_expression (parser);
5107 mark_exp_read (expr.value);
5108 if (convert_p)
5109 expr = default_function_array_conversion (loc, expr);
5110 expr.value = c_fully_fold (expr.value, false, NULL);
5111 parser->lex_untranslated_string = true;
5112 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5114 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5115 return NULL_TREE;
5117 list = chainon (list, build_tree_list (build_tree_list (name, str),
5118 expr.value));
5119 if (c_parser_next_token_is (parser, CPP_COMMA))
5120 c_parser_consume_token (parser);
5121 else
5122 break;
5124 return list;
5127 /* Parse asm clobbers, a GNU extension.
5129 asm-clobbers:
5130 asm-string-literal
5131 asm-clobbers , asm-string-literal
5134 static tree
5135 c_parser_asm_clobbers (c_parser *parser)
5137 tree list = NULL_TREE;
5138 while (true)
5140 tree str = c_parser_asm_string_literal (parser);
5141 if (str)
5142 list = tree_cons (NULL_TREE, str, list);
5143 else
5144 return NULL_TREE;
5145 if (c_parser_next_token_is (parser, CPP_COMMA))
5146 c_parser_consume_token (parser);
5147 else
5148 break;
5150 return list;
5153 /* Parse asm goto labels, a GNU extension.
5155 asm-goto-operands:
5156 identifier
5157 asm-goto-operands , identifier
5160 static tree
5161 c_parser_asm_goto_operands (c_parser *parser)
5163 tree list = NULL_TREE;
5164 while (true)
5166 tree name, label;
5168 if (c_parser_next_token_is (parser, CPP_NAME))
5170 c_token *tok = c_parser_peek_token (parser);
5171 name = tok->value;
5172 label = lookup_label_for_goto (tok->location, name);
5173 c_parser_consume_token (parser);
5174 TREE_USED (label) = 1;
5176 else
5178 c_parser_error (parser, "expected identifier");
5179 return NULL_TREE;
5182 name = build_string (IDENTIFIER_LENGTH (name),
5183 IDENTIFIER_POINTER (name));
5184 list = tree_cons (name, label, list);
5185 if (c_parser_next_token_is (parser, CPP_COMMA))
5186 c_parser_consume_token (parser);
5187 else
5188 return nreverse (list);
5192 /* Parse an expression other than a compound expression; that is, an
5193 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5194 NULL then it is an Objective-C message expression which is the
5195 primary-expression starting the expression as an initializer.
5197 assignment-expression:
5198 conditional-expression
5199 unary-expression assignment-operator assignment-expression
5201 assignment-operator: one of
5202 = *= /= %= += -= <<= >>= &= ^= |=
5204 In GNU C we accept any conditional expression on the LHS and
5205 diagnose the invalid lvalue rather than producing a syntax
5206 error. */
5208 static struct c_expr
5209 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5211 struct c_expr lhs, rhs, ret;
5212 enum tree_code code;
5213 location_t op_location, exp_location;
5214 gcc_assert (!after || c_dialect_objc ());
5215 lhs = c_parser_conditional_expression (parser, after);
5216 op_location = c_parser_peek_token (parser)->location;
5217 switch (c_parser_peek_token (parser)->type)
5219 case CPP_EQ:
5220 code = NOP_EXPR;
5221 break;
5222 case CPP_MULT_EQ:
5223 code = MULT_EXPR;
5224 break;
5225 case CPP_DIV_EQ:
5226 code = TRUNC_DIV_EXPR;
5227 break;
5228 case CPP_MOD_EQ:
5229 code = TRUNC_MOD_EXPR;
5230 break;
5231 case CPP_PLUS_EQ:
5232 code = PLUS_EXPR;
5233 break;
5234 case CPP_MINUS_EQ:
5235 code = MINUS_EXPR;
5236 break;
5237 case CPP_LSHIFT_EQ:
5238 code = LSHIFT_EXPR;
5239 break;
5240 case CPP_RSHIFT_EQ:
5241 code = RSHIFT_EXPR;
5242 break;
5243 case CPP_AND_EQ:
5244 code = BIT_AND_EXPR;
5245 break;
5246 case CPP_XOR_EQ:
5247 code = BIT_XOR_EXPR;
5248 break;
5249 case CPP_OR_EQ:
5250 code = BIT_IOR_EXPR;
5251 break;
5252 default:
5253 return lhs;
5255 c_parser_consume_token (parser);
5256 exp_location = c_parser_peek_token (parser)->location;
5257 rhs = c_parser_expr_no_commas (parser, NULL);
5258 rhs = default_function_array_read_conversion (exp_location, rhs);
5259 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5260 code, exp_location, rhs.value,
5261 rhs.original_type);
5262 if (code == NOP_EXPR)
5263 ret.original_code = MODIFY_EXPR;
5264 else
5266 TREE_NO_WARNING (ret.value) = 1;
5267 ret.original_code = ERROR_MARK;
5269 ret.original_type = NULL;
5270 return ret;
5273 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5274 is not NULL then it is an Objective-C message expression which is
5275 the primary-expression starting the expression as an initializer.
5277 conditional-expression:
5278 logical-OR-expression
5279 logical-OR-expression ? expression : conditional-expression
5281 GNU extensions:
5283 conditional-expression:
5284 logical-OR-expression ? : conditional-expression
5287 static struct c_expr
5288 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5290 struct c_expr cond, exp1, exp2, ret;
5291 location_t cond_loc, colon_loc, middle_loc;
5293 gcc_assert (!after || c_dialect_objc ());
5295 cond = c_parser_binary_expression (parser, after);
5297 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5298 return cond;
5299 cond_loc = c_parser_peek_token (parser)->location;
5300 cond = default_function_array_read_conversion (cond_loc, cond);
5301 c_parser_consume_token (parser);
5302 if (c_parser_next_token_is (parser, CPP_COLON))
5304 tree eptype = NULL_TREE;
5306 middle_loc = c_parser_peek_token (parser)->location;
5307 pedwarn (middle_loc, OPT_pedantic,
5308 "ISO C forbids omitting the middle term of a ?: expression");
5309 warn_for_omitted_condop (middle_loc, cond.value);
5310 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5312 eptype = TREE_TYPE (cond.value);
5313 cond.value = TREE_OPERAND (cond.value, 0);
5315 /* Make sure first operand is calculated only once. */
5316 exp1.value = c_save_expr (default_conversion (cond.value));
5317 if (eptype)
5318 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5319 exp1.original_type = NULL;
5320 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5321 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5323 else
5325 cond.value
5326 = c_objc_common_truthvalue_conversion
5327 (cond_loc, default_conversion (cond.value));
5328 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5329 exp1 = c_parser_expression_conv (parser);
5330 mark_exp_read (exp1.value);
5331 c_inhibit_evaluation_warnings +=
5332 ((cond.value == truthvalue_true_node)
5333 - (cond.value == truthvalue_false_node));
5336 colon_loc = c_parser_peek_token (parser)->location;
5337 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5339 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5340 ret.value = error_mark_node;
5341 ret.original_code = ERROR_MARK;
5342 ret.original_type = NULL;
5343 return ret;
5346 location_t exp2_loc = c_parser_peek_token (parser)->location;
5347 exp2 = c_parser_conditional_expression (parser, NULL);
5348 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5350 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5351 ret.value = build_conditional_expr (colon_loc, cond.value,
5352 cond.original_code == C_MAYBE_CONST_EXPR,
5353 exp1.value, exp1.original_type,
5354 exp2.value, exp2.original_type);
5355 ret.original_code = ERROR_MARK;
5356 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5357 ret.original_type = NULL;
5358 else
5360 tree t1, t2;
5362 /* If both sides are enum type, the default conversion will have
5363 made the type of the result be an integer type. We want to
5364 remember the enum types we started with. */
5365 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5366 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5367 ret.original_type = ((t1 != error_mark_node
5368 && t2 != error_mark_node
5369 && (TYPE_MAIN_VARIANT (t1)
5370 == TYPE_MAIN_VARIANT (t2)))
5371 ? t1
5372 : NULL);
5374 return ret;
5377 /* Parse a binary expression; that is, a logical-OR-expression (C90
5378 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5379 an Objective-C message expression which is the primary-expression
5380 starting the expression as an initializer.
5382 multiplicative-expression:
5383 cast-expression
5384 multiplicative-expression * cast-expression
5385 multiplicative-expression / cast-expression
5386 multiplicative-expression % cast-expression
5388 additive-expression:
5389 multiplicative-expression
5390 additive-expression + multiplicative-expression
5391 additive-expression - multiplicative-expression
5393 shift-expression:
5394 additive-expression
5395 shift-expression << additive-expression
5396 shift-expression >> additive-expression
5398 relational-expression:
5399 shift-expression
5400 relational-expression < shift-expression
5401 relational-expression > shift-expression
5402 relational-expression <= shift-expression
5403 relational-expression >= shift-expression
5405 equality-expression:
5406 relational-expression
5407 equality-expression == relational-expression
5408 equality-expression != relational-expression
5410 AND-expression:
5411 equality-expression
5412 AND-expression & equality-expression
5414 exclusive-OR-expression:
5415 AND-expression
5416 exclusive-OR-expression ^ AND-expression
5418 inclusive-OR-expression:
5419 exclusive-OR-expression
5420 inclusive-OR-expression | exclusive-OR-expression
5422 logical-AND-expression:
5423 inclusive-OR-expression
5424 logical-AND-expression && inclusive-OR-expression
5426 logical-OR-expression:
5427 logical-AND-expression
5428 logical-OR-expression || logical-AND-expression
5431 static struct c_expr
5432 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5434 /* A binary expression is parsed using operator-precedence parsing,
5435 with the operands being cast expressions. All the binary
5436 operators are left-associative. Thus a binary expression is of
5437 form:
5439 E0 op1 E1 op2 E2 ...
5441 which we represent on a stack. On the stack, the precedence
5442 levels are strictly increasing. When a new operator is
5443 encountered of higher precedence than that at the top of the
5444 stack, it is pushed; its LHS is the top expression, and its RHS
5445 is everything parsed until it is popped. When a new operator is
5446 encountered with precedence less than or equal to that at the top
5447 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5448 by the result of the operation until the operator at the top of
5449 the stack has lower precedence than the new operator or there is
5450 only one element on the stack; then the top expression is the LHS
5451 of the new operator. In the case of logical AND and OR
5452 expressions, we also need to adjust c_inhibit_evaluation_warnings
5453 as appropriate when the operators are pushed and popped. */
5455 /* The precedence levels, where 0 is a dummy lowest level used for
5456 the bottom of the stack. */
5457 enum prec {
5458 PREC_NONE,
5459 PREC_LOGOR,
5460 PREC_LOGAND,
5461 PREC_BITOR,
5462 PREC_BITXOR,
5463 PREC_BITAND,
5464 PREC_EQ,
5465 PREC_REL,
5466 PREC_SHIFT,
5467 PREC_ADD,
5468 PREC_MULT,
5469 NUM_PRECS
5471 struct {
5472 /* The expression at this stack level. */
5473 struct c_expr expr;
5474 /* The precedence of the operator on its left, PREC_NONE at the
5475 bottom of the stack. */
5476 enum prec prec;
5477 /* The operation on its left. */
5478 enum tree_code op;
5479 /* The source location of this operation. */
5480 location_t loc;
5481 } stack[NUM_PRECS];
5482 int sp;
5483 /* Location of the binary operator. */
5484 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5485 #define POP \
5486 do { \
5487 switch (stack[sp].op) \
5489 case TRUTH_ANDIF_EXPR: \
5490 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5491 == truthvalue_false_node); \
5492 break; \
5493 case TRUTH_ORIF_EXPR: \
5494 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5495 == truthvalue_true_node); \
5496 break; \
5497 default: \
5498 break; \
5500 stack[sp - 1].expr \
5501 = default_function_array_read_conversion (stack[sp - 1].loc, \
5502 stack[sp - 1].expr); \
5503 stack[sp].expr \
5504 = default_function_array_read_conversion (stack[sp].loc, \
5505 stack[sp].expr); \
5506 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5507 stack[sp].op, \
5508 stack[sp - 1].expr, \
5509 stack[sp].expr); \
5510 sp--; \
5511 } while (0)
5512 gcc_assert (!after || c_dialect_objc ());
5513 stack[0].loc = c_parser_peek_token (parser)->location;
5514 stack[0].expr = c_parser_cast_expression (parser, after);
5515 stack[0].prec = PREC_NONE;
5516 sp = 0;
5517 while (true)
5519 enum prec oprec;
5520 enum tree_code ocode;
5521 if (parser->error)
5522 goto out;
5523 switch (c_parser_peek_token (parser)->type)
5525 case CPP_MULT:
5526 oprec = PREC_MULT;
5527 ocode = MULT_EXPR;
5528 break;
5529 case CPP_DIV:
5530 oprec = PREC_MULT;
5531 ocode = TRUNC_DIV_EXPR;
5532 break;
5533 case CPP_MOD:
5534 oprec = PREC_MULT;
5535 ocode = TRUNC_MOD_EXPR;
5536 break;
5537 case CPP_PLUS:
5538 oprec = PREC_ADD;
5539 ocode = PLUS_EXPR;
5540 break;
5541 case CPP_MINUS:
5542 oprec = PREC_ADD;
5543 ocode = MINUS_EXPR;
5544 break;
5545 case CPP_LSHIFT:
5546 oprec = PREC_SHIFT;
5547 ocode = LSHIFT_EXPR;
5548 break;
5549 case CPP_RSHIFT:
5550 oprec = PREC_SHIFT;
5551 ocode = RSHIFT_EXPR;
5552 break;
5553 case CPP_LESS:
5554 oprec = PREC_REL;
5555 ocode = LT_EXPR;
5556 break;
5557 case CPP_GREATER:
5558 oprec = PREC_REL;
5559 ocode = GT_EXPR;
5560 break;
5561 case CPP_LESS_EQ:
5562 oprec = PREC_REL;
5563 ocode = LE_EXPR;
5564 break;
5565 case CPP_GREATER_EQ:
5566 oprec = PREC_REL;
5567 ocode = GE_EXPR;
5568 break;
5569 case CPP_EQ_EQ:
5570 oprec = PREC_EQ;
5571 ocode = EQ_EXPR;
5572 break;
5573 case CPP_NOT_EQ:
5574 oprec = PREC_EQ;
5575 ocode = NE_EXPR;
5576 break;
5577 case CPP_AND:
5578 oprec = PREC_BITAND;
5579 ocode = BIT_AND_EXPR;
5580 break;
5581 case CPP_XOR:
5582 oprec = PREC_BITXOR;
5583 ocode = BIT_XOR_EXPR;
5584 break;
5585 case CPP_OR:
5586 oprec = PREC_BITOR;
5587 ocode = BIT_IOR_EXPR;
5588 break;
5589 case CPP_AND_AND:
5590 oprec = PREC_LOGAND;
5591 ocode = TRUTH_ANDIF_EXPR;
5592 break;
5593 case CPP_OR_OR:
5594 oprec = PREC_LOGOR;
5595 ocode = TRUTH_ORIF_EXPR;
5596 break;
5597 default:
5598 /* Not a binary operator, so end of the binary
5599 expression. */
5600 goto out;
5602 binary_loc = c_parser_peek_token (parser)->location;
5603 c_parser_consume_token (parser);
5604 while (oprec <= stack[sp].prec)
5605 POP;
5606 switch (ocode)
5608 case TRUTH_ANDIF_EXPR:
5609 stack[sp].expr
5610 = default_function_array_read_conversion (stack[sp].loc,
5611 stack[sp].expr);
5612 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5613 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5614 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5615 == truthvalue_false_node);
5616 break;
5617 case TRUTH_ORIF_EXPR:
5618 stack[sp].expr
5619 = default_function_array_read_conversion (stack[sp].loc,
5620 stack[sp].expr);
5621 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5622 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5623 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5624 == truthvalue_true_node);
5625 break;
5626 default:
5627 break;
5629 sp++;
5630 stack[sp].loc = binary_loc;
5631 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5632 stack[sp].prec = oprec;
5633 stack[sp].op = ocode;
5634 stack[sp].loc = binary_loc;
5636 out:
5637 while (sp > 0)
5638 POP;
5639 return stack[0].expr;
5640 #undef POP
5643 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5644 NULL then it is an Objective-C message expression which is the
5645 primary-expression starting the expression as an initializer.
5647 cast-expression:
5648 unary-expression
5649 ( type-name ) unary-expression
5652 static struct c_expr
5653 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5655 location_t cast_loc = c_parser_peek_token (parser)->location;
5656 gcc_assert (!after || c_dialect_objc ());
5657 if (after)
5658 return c_parser_postfix_expression_after_primary (parser,
5659 cast_loc, *after);
5660 /* If the expression begins with a parenthesized type name, it may
5661 be either a cast or a compound literal; we need to see whether
5662 the next character is '{' to tell the difference. If not, it is
5663 an unary expression. Full detection of unknown typenames here
5664 would require a 3-token lookahead. */
5665 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5666 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5668 struct c_type_name *type_name;
5669 struct c_expr ret;
5670 struct c_expr expr;
5671 c_parser_consume_token (parser);
5672 type_name = c_parser_type_name (parser);
5673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5674 if (type_name == NULL)
5676 ret.value = error_mark_node;
5677 ret.original_code = ERROR_MARK;
5678 ret.original_type = NULL;
5679 return ret;
5682 /* Save casted types in the function's used types hash table. */
5683 used_types_insert (type_name->specs->type);
5685 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5686 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5687 cast_loc);
5689 location_t expr_loc = c_parser_peek_token (parser)->location;
5690 expr = c_parser_cast_expression (parser, NULL);
5691 expr = default_function_array_read_conversion (expr_loc, expr);
5693 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5694 ret.original_code = ERROR_MARK;
5695 ret.original_type = NULL;
5696 return ret;
5698 else
5699 return c_parser_unary_expression (parser);
5702 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5704 unary-expression:
5705 postfix-expression
5706 ++ unary-expression
5707 -- unary-expression
5708 unary-operator cast-expression
5709 sizeof unary-expression
5710 sizeof ( type-name )
5712 unary-operator: one of
5713 & * + - ~ !
5715 GNU extensions:
5717 unary-expression:
5718 __alignof__ unary-expression
5719 __alignof__ ( type-name )
5720 && identifier
5722 unary-operator: one of
5723 __extension__ __real__ __imag__
5725 In addition, the GNU syntax treats ++ and -- as unary operators, so
5726 they may be applied to cast expressions with errors for non-lvalues
5727 given later. */
5729 static struct c_expr
5730 c_parser_unary_expression (c_parser *parser)
5732 int ext;
5733 struct c_expr ret, op;
5734 location_t op_loc = c_parser_peek_token (parser)->location;
5735 location_t exp_loc;
5736 ret.original_code = ERROR_MARK;
5737 ret.original_type = NULL;
5738 switch (c_parser_peek_token (parser)->type)
5740 case CPP_PLUS_PLUS:
5741 c_parser_consume_token (parser);
5742 exp_loc = c_parser_peek_token (parser)->location;
5743 op = c_parser_cast_expression (parser, NULL);
5744 op = default_function_array_read_conversion (exp_loc, op);
5745 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5746 case CPP_MINUS_MINUS:
5747 c_parser_consume_token (parser);
5748 exp_loc = c_parser_peek_token (parser)->location;
5749 op = c_parser_cast_expression (parser, NULL);
5750 op = default_function_array_read_conversion (exp_loc, op);
5751 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5752 case CPP_AND:
5753 c_parser_consume_token (parser);
5754 op = c_parser_cast_expression (parser, NULL);
5755 mark_exp_read (op.value);
5756 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5757 case CPP_MULT:
5758 c_parser_consume_token (parser);
5759 exp_loc = c_parser_peek_token (parser)->location;
5760 op = c_parser_cast_expression (parser, NULL);
5761 op = default_function_array_read_conversion (exp_loc, op);
5762 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5763 return ret;
5764 case CPP_PLUS:
5765 if (!c_dialect_objc () && !in_system_header)
5766 warning_at (op_loc,
5767 OPT_Wtraditional,
5768 "traditional C rejects the unary plus operator");
5769 c_parser_consume_token (parser);
5770 exp_loc = c_parser_peek_token (parser)->location;
5771 op = c_parser_cast_expression (parser, NULL);
5772 op = default_function_array_read_conversion (exp_loc, op);
5773 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5774 case CPP_MINUS:
5775 c_parser_consume_token (parser);
5776 exp_loc = c_parser_peek_token (parser)->location;
5777 op = c_parser_cast_expression (parser, NULL);
5778 op = default_function_array_read_conversion (exp_loc, op);
5779 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5780 case CPP_COMPL:
5781 c_parser_consume_token (parser);
5782 exp_loc = c_parser_peek_token (parser)->location;
5783 op = c_parser_cast_expression (parser, NULL);
5784 op = default_function_array_read_conversion (exp_loc, op);
5785 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5786 case CPP_NOT:
5787 c_parser_consume_token (parser);
5788 exp_loc = c_parser_peek_token (parser)->location;
5789 op = c_parser_cast_expression (parser, NULL);
5790 op = default_function_array_read_conversion (exp_loc, op);
5791 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5792 case CPP_AND_AND:
5793 /* Refer to the address of a label as a pointer. */
5794 c_parser_consume_token (parser);
5795 if (c_parser_next_token_is (parser, CPP_NAME))
5797 ret.value = finish_label_address_expr
5798 (c_parser_peek_token (parser)->value, op_loc);
5799 c_parser_consume_token (parser);
5801 else
5803 c_parser_error (parser, "expected identifier");
5804 ret.value = error_mark_node;
5806 return ret;
5807 case CPP_KEYWORD:
5808 switch (c_parser_peek_token (parser)->keyword)
5810 case RID_SIZEOF:
5811 return c_parser_sizeof_expression (parser);
5812 case RID_ALIGNOF:
5813 return c_parser_alignof_expression (parser);
5814 case RID_EXTENSION:
5815 c_parser_consume_token (parser);
5816 ext = disable_extension_diagnostics ();
5817 ret = c_parser_cast_expression (parser, NULL);
5818 restore_extension_diagnostics (ext);
5819 return ret;
5820 case RID_REALPART:
5821 c_parser_consume_token (parser);
5822 exp_loc = c_parser_peek_token (parser)->location;
5823 op = c_parser_cast_expression (parser, NULL);
5824 op = default_function_array_conversion (exp_loc, op);
5825 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5826 case RID_IMAGPART:
5827 c_parser_consume_token (parser);
5828 exp_loc = c_parser_peek_token (parser)->location;
5829 op = c_parser_cast_expression (parser, NULL);
5830 op = default_function_array_conversion (exp_loc, op);
5831 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5832 default:
5833 return c_parser_postfix_expression (parser);
5835 default:
5836 return c_parser_postfix_expression (parser);
5840 /* Parse a sizeof expression. */
5842 static struct c_expr
5843 c_parser_sizeof_expression (c_parser *parser)
5845 struct c_expr expr;
5846 location_t expr_loc;
5847 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5848 c_parser_consume_token (parser);
5849 c_inhibit_evaluation_warnings++;
5850 in_sizeof++;
5851 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5852 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5854 /* Either sizeof ( type-name ) or sizeof unary-expression
5855 starting with a compound literal. */
5856 struct c_type_name *type_name;
5857 c_parser_consume_token (parser);
5858 expr_loc = c_parser_peek_token (parser)->location;
5859 type_name = c_parser_type_name (parser);
5860 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5861 if (type_name == NULL)
5863 struct c_expr ret;
5864 c_inhibit_evaluation_warnings--;
5865 in_sizeof--;
5866 ret.value = error_mark_node;
5867 ret.original_code = ERROR_MARK;
5868 ret.original_type = NULL;
5869 return ret;
5871 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5873 expr = c_parser_postfix_expression_after_paren_type (parser,
5874 type_name,
5875 expr_loc);
5876 goto sizeof_expr;
5878 /* sizeof ( type-name ). */
5879 c_inhibit_evaluation_warnings--;
5880 in_sizeof--;
5881 return c_expr_sizeof_type (expr_loc, type_name);
5883 else
5885 expr_loc = c_parser_peek_token (parser)->location;
5886 expr = c_parser_unary_expression (parser);
5887 sizeof_expr:
5888 c_inhibit_evaluation_warnings--;
5889 in_sizeof--;
5890 mark_exp_read (expr.value);
5891 if (TREE_CODE (expr.value) == COMPONENT_REF
5892 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5893 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5894 return c_expr_sizeof_expr (expr_loc, expr);
5898 /* Parse an alignof expression. */
5900 static struct c_expr
5901 c_parser_alignof_expression (c_parser *parser)
5903 struct c_expr expr;
5904 location_t loc = c_parser_peek_token (parser)->location;
5905 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5906 c_parser_consume_token (parser);
5907 c_inhibit_evaluation_warnings++;
5908 in_alignof++;
5909 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5910 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5912 /* Either __alignof__ ( type-name ) or __alignof__
5913 unary-expression starting with a compound literal. */
5914 location_t loc;
5915 struct c_type_name *type_name;
5916 struct c_expr ret;
5917 c_parser_consume_token (parser);
5918 loc = c_parser_peek_token (parser)->location;
5919 type_name = c_parser_type_name (parser);
5920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5921 if (type_name == NULL)
5923 struct c_expr ret;
5924 c_inhibit_evaluation_warnings--;
5925 in_alignof--;
5926 ret.value = error_mark_node;
5927 ret.original_code = ERROR_MARK;
5928 ret.original_type = NULL;
5929 return ret;
5931 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5933 expr = c_parser_postfix_expression_after_paren_type (parser,
5934 type_name,
5935 loc);
5936 goto alignof_expr;
5938 /* alignof ( type-name ). */
5939 c_inhibit_evaluation_warnings--;
5940 in_alignof--;
5941 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5942 ret.original_code = ERROR_MARK;
5943 ret.original_type = NULL;
5944 return ret;
5946 else
5948 struct c_expr ret;
5949 expr = c_parser_unary_expression (parser);
5950 alignof_expr:
5951 mark_exp_read (expr.value);
5952 c_inhibit_evaluation_warnings--;
5953 in_alignof--;
5954 ret.value = c_alignof_expr (loc, expr.value);
5955 ret.original_code = ERROR_MARK;
5956 ret.original_type = NULL;
5957 return ret;
5961 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5963 postfix-expression:
5964 primary-expression
5965 postfix-expression [ expression ]
5966 postfix-expression ( argument-expression-list[opt] )
5967 postfix-expression . identifier
5968 postfix-expression -> identifier
5969 postfix-expression ++
5970 postfix-expression --
5971 ( type-name ) { initializer-list }
5972 ( type-name ) { initializer-list , }
5974 argument-expression-list:
5975 argument-expression
5976 argument-expression-list , argument-expression
5978 primary-expression:
5979 identifier
5980 constant
5981 string-literal
5982 ( expression )
5984 GNU extensions:
5986 primary-expression:
5987 __func__
5988 (treated as a keyword in GNU C)
5989 __FUNCTION__
5990 __PRETTY_FUNCTION__
5991 ( compound-statement )
5992 __builtin_va_arg ( assignment-expression , type-name )
5993 __builtin_offsetof ( type-name , offsetof-member-designator )
5994 __builtin_choose_expr ( assignment-expression ,
5995 assignment-expression ,
5996 assignment-expression )
5997 __builtin_types_compatible_p ( type-name , type-name )
5999 offsetof-member-designator:
6000 identifier
6001 offsetof-member-designator . identifier
6002 offsetof-member-designator [ expression ]
6004 Objective-C:
6006 primary-expression:
6007 [ objc-receiver objc-message-args ]
6008 @selector ( objc-selector-arg )
6009 @protocol ( identifier )
6010 @encode ( type-name )
6011 objc-string-literal
6012 Classname . identifier
6015 static struct c_expr
6016 c_parser_postfix_expression (c_parser *parser)
6018 struct c_expr expr, e1, e2, e3;
6019 struct c_type_name *t1, *t2;
6020 location_t loc = c_parser_peek_token (parser)->location;;
6021 expr.original_code = ERROR_MARK;
6022 expr.original_type = NULL;
6023 switch (c_parser_peek_token (parser)->type)
6025 case CPP_NUMBER:
6026 expr.value = c_parser_peek_token (parser)->value;
6027 loc = c_parser_peek_token (parser)->location;
6028 c_parser_consume_token (parser);
6029 if (TREE_CODE (expr.value) == FIXED_CST
6030 && !targetm.fixed_point_supported_p ())
6032 error_at (loc, "fixed-point types not supported for this target");
6033 expr.value = error_mark_node;
6035 break;
6036 case CPP_CHAR:
6037 case CPP_CHAR16:
6038 case CPP_CHAR32:
6039 case CPP_WCHAR:
6040 expr.value = c_parser_peek_token (parser)->value;
6041 c_parser_consume_token (parser);
6042 break;
6043 case CPP_STRING:
6044 case CPP_STRING16:
6045 case CPP_STRING32:
6046 case CPP_WSTRING:
6047 case CPP_UTF8STRING:
6048 expr.value = c_parser_peek_token (parser)->value;
6049 expr.original_code = STRING_CST;
6050 c_parser_consume_token (parser);
6051 break;
6052 case CPP_OBJC_STRING:
6053 gcc_assert (c_dialect_objc ());
6054 expr.value
6055 = objc_build_string_object (c_parser_peek_token (parser)->value);
6056 c_parser_consume_token (parser);
6057 break;
6058 case CPP_NAME:
6059 switch (c_parser_peek_token (parser)->id_kind)
6061 case C_ID_ID:
6063 tree id = c_parser_peek_token (parser)->value;
6064 c_parser_consume_token (parser);
6065 expr.value = build_external_ref (loc, id,
6066 (c_parser_peek_token (parser)->type
6067 == CPP_OPEN_PAREN),
6068 &expr.original_type);
6069 break;
6071 case C_ID_CLASSNAME:
6073 /* Here we parse the Objective-C 2.0 Class.name dot
6074 syntax. */
6075 tree class_name = c_parser_peek_token (parser)->value;
6076 tree component;
6077 c_parser_consume_token (parser);
6078 gcc_assert (c_dialect_objc ());
6079 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6081 expr.value = error_mark_node;
6082 break;
6084 if (c_parser_next_token_is_not (parser, CPP_NAME))
6086 c_parser_error (parser, "expected identifier");
6087 expr.value = error_mark_node;
6088 break;
6090 component = c_parser_peek_token (parser)->value;
6091 c_parser_consume_token (parser);
6092 expr.value = objc_build_class_component_ref (class_name,
6093 component);
6094 break;
6096 default:
6097 c_parser_error (parser, "expected expression");
6098 expr.value = error_mark_node;
6099 break;
6101 break;
6102 case CPP_OPEN_PAREN:
6103 /* A parenthesized expression, statement expression or compound
6104 literal. */
6105 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6107 /* A statement expression. */
6108 tree stmt;
6109 location_t brace_loc;
6110 c_parser_consume_token (parser);
6111 brace_loc = c_parser_peek_token (parser)->location;
6112 c_parser_consume_token (parser);
6113 if (cur_stmt_list == NULL)
6115 error_at (loc, "braced-group within expression allowed "
6116 "only inside a function");
6117 parser->error = true;
6118 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6119 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6120 expr.value = error_mark_node;
6121 break;
6123 stmt = c_begin_stmt_expr ();
6124 c_parser_compound_statement_nostart (parser);
6125 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6126 "expected %<)%>");
6127 pedwarn (loc, OPT_pedantic,
6128 "ISO C forbids braced-groups within expressions");
6129 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6130 mark_exp_read (expr.value);
6132 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6134 /* A compound literal. ??? Can we actually get here rather
6135 than going directly to
6136 c_parser_postfix_expression_after_paren_type from
6137 elsewhere? */
6138 location_t loc;
6139 struct c_type_name *type_name;
6140 c_parser_consume_token (parser);
6141 loc = c_parser_peek_token (parser)->location;
6142 type_name = c_parser_type_name (parser);
6143 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6144 "expected %<)%>");
6145 if (type_name == NULL)
6147 expr.value = error_mark_node;
6149 else
6150 expr = c_parser_postfix_expression_after_paren_type (parser,
6151 type_name,
6152 loc);
6154 else
6156 /* A parenthesized expression. */
6157 c_parser_consume_token (parser);
6158 expr = c_parser_expression (parser);
6159 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6160 TREE_NO_WARNING (expr.value) = 1;
6161 if (expr.original_code != C_MAYBE_CONST_EXPR)
6162 expr.original_code = ERROR_MARK;
6163 /* Don't change EXPR.ORIGINAL_TYPE. */
6164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6165 "expected %<)%>");
6167 break;
6168 case CPP_KEYWORD:
6169 switch (c_parser_peek_token (parser)->keyword)
6171 case RID_FUNCTION_NAME:
6172 case RID_PRETTY_FUNCTION_NAME:
6173 case RID_C99_FUNCTION_NAME:
6174 expr.value = fname_decl (loc,
6175 c_parser_peek_token (parser)->keyword,
6176 c_parser_peek_token (parser)->value);
6177 c_parser_consume_token (parser);
6178 break;
6179 case RID_VA_ARG:
6180 c_parser_consume_token (parser);
6181 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6183 expr.value = error_mark_node;
6184 break;
6186 e1 = c_parser_expr_no_commas (parser, NULL);
6187 mark_exp_read (e1.value);
6188 e1.value = c_fully_fold (e1.value, false, NULL);
6189 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6191 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6192 expr.value = error_mark_node;
6193 break;
6195 loc = c_parser_peek_token (parser)->location;
6196 t1 = c_parser_type_name (parser);
6197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6198 "expected %<)%>");
6199 if (t1 == NULL)
6201 expr.value = error_mark_node;
6203 else
6205 tree type_expr = NULL_TREE;
6206 expr.value = c_build_va_arg (loc, e1.value,
6207 groktypename (t1, &type_expr, NULL));
6208 if (type_expr)
6210 expr.value = build2 (C_MAYBE_CONST_EXPR,
6211 TREE_TYPE (expr.value), type_expr,
6212 expr.value);
6213 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6216 break;
6217 case RID_OFFSETOF:
6218 c_parser_consume_token (parser);
6219 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6221 expr.value = error_mark_node;
6222 break;
6224 t1 = c_parser_type_name (parser);
6225 if (t1 == NULL)
6226 parser->error = true;
6227 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6228 gcc_assert (parser->error);
6229 if (parser->error)
6231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6232 expr.value = error_mark_node;
6233 break;
6237 tree type = groktypename (t1, NULL, NULL);
6238 tree offsetof_ref;
6239 if (type == error_mark_node)
6240 offsetof_ref = error_mark_node;
6241 else
6243 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6244 SET_EXPR_LOCATION (offsetof_ref, loc);
6246 /* Parse the second argument to __builtin_offsetof. We
6247 must have one identifier, and beyond that we want to
6248 accept sub structure and sub array references. */
6249 if (c_parser_next_token_is (parser, CPP_NAME))
6251 offsetof_ref = build_component_ref
6252 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6253 c_parser_consume_token (parser);
6254 while (c_parser_next_token_is (parser, CPP_DOT)
6255 || c_parser_next_token_is (parser,
6256 CPP_OPEN_SQUARE)
6257 || c_parser_next_token_is (parser,
6258 CPP_DEREF))
6260 if (c_parser_next_token_is (parser, CPP_DEREF))
6262 loc = c_parser_peek_token (parser)->location;
6263 offsetof_ref = build_array_ref (loc,
6264 offsetof_ref,
6265 integer_zero_node);
6266 goto do_dot;
6268 else if (c_parser_next_token_is (parser, CPP_DOT))
6270 do_dot:
6271 c_parser_consume_token (parser);
6272 if (c_parser_next_token_is_not (parser,
6273 CPP_NAME))
6275 c_parser_error (parser, "expected identifier");
6276 break;
6278 offsetof_ref = build_component_ref
6279 (loc, offsetof_ref,
6280 c_parser_peek_token (parser)->value);
6281 c_parser_consume_token (parser);
6283 else
6285 tree idx;
6286 loc = c_parser_peek_token (parser)->location;
6287 c_parser_consume_token (parser);
6288 idx = c_parser_expression (parser).value;
6289 idx = c_fully_fold (idx, false, NULL);
6290 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6291 "expected %<]%>");
6292 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6296 else
6297 c_parser_error (parser, "expected identifier");
6298 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6299 "expected %<)%>");
6300 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6302 break;
6303 case RID_CHOOSE_EXPR:
6304 c_parser_consume_token (parser);
6305 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6307 expr.value = error_mark_node;
6308 break;
6310 loc = c_parser_peek_token (parser)->location;
6311 e1 = c_parser_expr_no_commas (parser, NULL);
6312 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6315 expr.value = error_mark_node;
6316 break;
6318 e2 = c_parser_expr_no_commas (parser, NULL);
6319 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6322 expr.value = error_mark_node;
6323 break;
6325 e3 = c_parser_expr_no_commas (parser, NULL);
6326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6327 "expected %<)%>");
6329 tree c;
6331 c = e1.value;
6332 mark_exp_read (e2.value);
6333 mark_exp_read (e3.value);
6334 if (TREE_CODE (c) != INTEGER_CST
6335 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6336 error_at (loc,
6337 "first argument to %<__builtin_choose_expr%> not"
6338 " a constant");
6339 constant_expression_warning (c);
6340 expr = integer_zerop (c) ? e3 : e2;
6342 break;
6343 case RID_TYPES_COMPATIBLE_P:
6344 c_parser_consume_token (parser);
6345 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6347 expr.value = error_mark_node;
6348 break;
6350 t1 = c_parser_type_name (parser);
6351 if (t1 == NULL)
6353 expr.value = error_mark_node;
6354 break;
6356 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6358 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6359 expr.value = error_mark_node;
6360 break;
6362 t2 = c_parser_type_name (parser);
6363 if (t2 == NULL)
6365 expr.value = error_mark_node;
6366 break;
6368 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6369 "expected %<)%>");
6371 tree e1, e2;
6373 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6374 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6376 expr.value
6377 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6379 break;
6380 case RID_AT_SELECTOR:
6381 gcc_assert (c_dialect_objc ());
6382 c_parser_consume_token (parser);
6383 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6385 expr.value = error_mark_node;
6386 break;
6389 tree sel = c_parser_objc_selector_arg (parser);
6390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6391 "expected %<)%>");
6392 expr.value = objc_build_selector_expr (loc, sel);
6394 break;
6395 case RID_AT_PROTOCOL:
6396 gcc_assert (c_dialect_objc ());
6397 c_parser_consume_token (parser);
6398 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6400 expr.value = error_mark_node;
6401 break;
6403 if (c_parser_next_token_is_not (parser, CPP_NAME))
6405 c_parser_error (parser, "expected identifier");
6406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6407 expr.value = error_mark_node;
6408 break;
6411 tree id = c_parser_peek_token (parser)->value;
6412 c_parser_consume_token (parser);
6413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6414 "expected %<)%>");
6415 expr.value = objc_build_protocol_expr (id);
6417 break;
6418 case RID_AT_ENCODE:
6419 /* Extension to support C-structures in the archiver. */
6420 gcc_assert (c_dialect_objc ());
6421 c_parser_consume_token (parser);
6422 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6424 expr.value = error_mark_node;
6425 break;
6427 t1 = c_parser_type_name (parser);
6428 if (t1 == NULL)
6430 expr.value = error_mark_node;
6431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6432 break;
6434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6435 "expected %<)%>");
6437 tree type = groktypename (t1, NULL, NULL);
6438 expr.value = objc_build_encode_expr (type);
6440 break;
6441 default:
6442 c_parser_error (parser, "expected expression");
6443 expr.value = error_mark_node;
6444 break;
6446 break;
6447 case CPP_OPEN_SQUARE:
6448 if (c_dialect_objc ())
6450 tree receiver, args;
6451 c_parser_consume_token (parser);
6452 receiver = c_parser_objc_receiver (parser);
6453 args = c_parser_objc_message_args (parser);
6454 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6455 "expected %<]%>");
6456 expr.value = objc_build_message_expr (build_tree_list (receiver,
6457 args));
6458 break;
6460 /* Else fall through to report error. */
6461 default:
6462 c_parser_error (parser, "expected expression");
6463 expr.value = error_mark_node;
6464 break;
6466 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6469 /* Parse a postfix expression after a parenthesized type name: the
6470 brace-enclosed initializer of a compound literal, possibly followed
6471 by some postfix operators. This is separate because it is not
6472 possible to tell until after the type name whether a cast
6473 expression has a cast or a compound literal, or whether the operand
6474 of sizeof is a parenthesized type name or starts with a compound
6475 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6476 location of the first token after the parentheses around the type
6477 name. */
6479 static struct c_expr
6480 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6481 struct c_type_name *type_name,
6482 location_t type_loc)
6484 tree type;
6485 struct c_expr init;
6486 bool non_const;
6487 struct c_expr expr;
6488 location_t start_loc;
6489 tree type_expr = NULL_TREE;
6490 bool type_expr_const = true;
6491 check_compound_literal_type (type_loc, type_name);
6492 start_init (NULL_TREE, NULL, 0);
6493 type = groktypename (type_name, &type_expr, &type_expr_const);
6494 start_loc = c_parser_peek_token (parser)->location;
6495 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6497 error_at (type_loc, "compound literal has variable size");
6498 type = error_mark_node;
6500 init = c_parser_braced_init (parser, type, false);
6501 finish_init ();
6502 maybe_warn_string_init (type, init);
6504 if (type != error_mark_node
6505 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6506 && current_function_decl)
6508 error ("compound literal qualified by address-space qualifier");
6509 type = error_mark_node;
6512 if (!flag_isoc99)
6513 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6514 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6515 ? CONSTRUCTOR_NON_CONST (init.value)
6516 : init.original_code == C_MAYBE_CONST_EXPR);
6517 non_const |= !type_expr_const;
6518 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6519 expr.original_code = ERROR_MARK;
6520 expr.original_type = NULL;
6521 if (type_expr)
6523 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6525 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6526 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6528 else
6530 gcc_assert (!non_const);
6531 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6532 type_expr, expr.value);
6535 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6538 /* Parse a postfix expression after the initial primary or compound
6539 literal; that is, parse a series of postfix operators.
6541 EXPR_LOC is the location of the primary expression. */
6543 static struct c_expr
6544 c_parser_postfix_expression_after_primary (c_parser *parser,
6545 location_t expr_loc,
6546 struct c_expr expr)
6548 struct c_expr orig_expr;
6549 tree ident, idx;
6550 VEC(tree,gc) *exprlist;
6551 VEC(tree,gc) *origtypes;
6552 while (true)
6554 location_t op_loc = c_parser_peek_token (parser)->location;
6555 switch (c_parser_peek_token (parser)->type)
6557 case CPP_OPEN_SQUARE:
6558 /* Array reference. */
6559 c_parser_consume_token (parser);
6560 idx = c_parser_expression (parser).value;
6561 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6562 "expected %<]%>");
6563 expr.value = build_array_ref (op_loc, expr.value, idx);
6564 expr.original_code = ERROR_MARK;
6565 expr.original_type = NULL;
6566 break;
6567 case CPP_OPEN_PAREN:
6568 /* Function call. */
6569 c_parser_consume_token (parser);
6570 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6571 exprlist = NULL;
6572 else
6573 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6575 "expected %<)%>");
6576 orig_expr = expr;
6577 mark_exp_read (expr.value);
6578 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6579 "(" after the FUNCNAME, which is what we have now. */
6580 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6581 origtypes);
6582 expr.original_code = ERROR_MARK;
6583 if (TREE_CODE (expr.value) == INTEGER_CST
6584 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6585 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6586 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6587 expr.original_code = C_MAYBE_CONST_EXPR;
6588 expr.original_type = NULL;
6589 if (exprlist != NULL)
6591 release_tree_vector (exprlist);
6592 release_tree_vector (origtypes);
6594 break;
6595 case CPP_DOT:
6596 /* Structure element reference. */
6597 c_parser_consume_token (parser);
6598 expr = default_function_array_conversion (expr_loc, expr);
6599 if (c_parser_next_token_is (parser, CPP_NAME))
6600 ident = c_parser_peek_token (parser)->value;
6601 else
6603 c_parser_error (parser, "expected identifier");
6604 expr.value = error_mark_node;
6605 expr.original_code = ERROR_MARK;
6606 expr.original_type = NULL;
6607 return expr;
6609 c_parser_consume_token (parser);
6610 expr.value = build_component_ref (op_loc, expr.value, ident);
6611 expr.original_code = ERROR_MARK;
6612 if (TREE_CODE (expr.value) != COMPONENT_REF)
6613 expr.original_type = NULL;
6614 else
6616 /* Remember the original type of a bitfield. */
6617 tree field = TREE_OPERAND (expr.value, 1);
6618 if (TREE_CODE (field) != FIELD_DECL)
6619 expr.original_type = NULL;
6620 else
6621 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6623 break;
6624 case CPP_DEREF:
6625 /* Structure element reference. */
6626 c_parser_consume_token (parser);
6627 expr = default_function_array_conversion (expr_loc, expr);
6628 if (c_parser_next_token_is (parser, CPP_NAME))
6629 ident = c_parser_peek_token (parser)->value;
6630 else
6632 c_parser_error (parser, "expected identifier");
6633 expr.value = error_mark_node;
6634 expr.original_code = ERROR_MARK;
6635 expr.original_type = NULL;
6636 return expr;
6638 c_parser_consume_token (parser);
6639 expr.value = build_component_ref (op_loc,
6640 build_indirect_ref (op_loc,
6641 expr.value,
6642 RO_ARROW),
6643 ident);
6644 expr.original_code = ERROR_MARK;
6645 if (TREE_CODE (expr.value) != COMPONENT_REF)
6646 expr.original_type = NULL;
6647 else
6649 /* Remember the original type of a bitfield. */
6650 tree field = TREE_OPERAND (expr.value, 1);
6651 if (TREE_CODE (field) != FIELD_DECL)
6652 expr.original_type = NULL;
6653 else
6654 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6656 break;
6657 case CPP_PLUS_PLUS:
6658 /* Postincrement. */
6659 c_parser_consume_token (parser);
6660 expr = default_function_array_read_conversion (expr_loc, expr);
6661 expr.value = build_unary_op (op_loc,
6662 POSTINCREMENT_EXPR, expr.value, 0);
6663 expr.original_code = ERROR_MARK;
6664 expr.original_type = NULL;
6665 break;
6666 case CPP_MINUS_MINUS:
6667 /* Postdecrement. */
6668 c_parser_consume_token (parser);
6669 expr = default_function_array_read_conversion (expr_loc, expr);
6670 expr.value = build_unary_op (op_loc,
6671 POSTDECREMENT_EXPR, expr.value, 0);
6672 expr.original_code = ERROR_MARK;
6673 expr.original_type = NULL;
6674 break;
6675 default:
6676 return expr;
6681 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6683 expression:
6684 assignment-expression
6685 expression , assignment-expression
6688 static struct c_expr
6689 c_parser_expression (c_parser *parser)
6691 struct c_expr expr;
6692 expr = c_parser_expr_no_commas (parser, NULL);
6693 while (c_parser_next_token_is (parser, CPP_COMMA))
6695 struct c_expr next;
6696 tree lhsval;
6697 location_t loc = c_parser_peek_token (parser)->location;
6698 location_t expr_loc;
6699 c_parser_consume_token (parser);
6700 expr_loc = c_parser_peek_token (parser)->location;
6701 lhsval = expr.value;
6702 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6703 lhsval = TREE_OPERAND (lhsval, 1);
6704 if (DECL_P (lhsval) || handled_component_p (lhsval))
6705 mark_exp_read (lhsval);
6706 next = c_parser_expr_no_commas (parser, NULL);
6707 next = default_function_array_conversion (expr_loc, next);
6708 expr.value = build_compound_expr (loc, expr.value, next.value);
6709 expr.original_code = COMPOUND_EXPR;
6710 expr.original_type = next.original_type;
6712 return expr;
6715 /* Parse an expression and convert functions or arrays to
6716 pointers. */
6718 static struct c_expr
6719 c_parser_expression_conv (c_parser *parser)
6721 struct c_expr expr;
6722 location_t loc = c_parser_peek_token (parser)->location;
6723 expr = c_parser_expression (parser);
6724 expr = default_function_array_conversion (loc, expr);
6725 return expr;
6728 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6729 functions and arrays to pointers. If FOLD_P, fold the expressions.
6731 nonempty-expr-list:
6732 assignment-expression
6733 nonempty-expr-list , assignment-expression
6736 static VEC(tree,gc) *
6737 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6738 VEC(tree,gc) **p_orig_types)
6740 VEC(tree,gc) *ret;
6741 VEC(tree,gc) *orig_types;
6742 struct c_expr expr;
6743 location_t loc = c_parser_peek_token (parser)->location;
6745 ret = make_tree_vector ();
6746 if (p_orig_types == NULL)
6747 orig_types = NULL;
6748 else
6749 orig_types = make_tree_vector ();
6751 expr = c_parser_expr_no_commas (parser, NULL);
6752 if (convert_p)
6753 expr = default_function_array_read_conversion (loc, expr);
6754 if (fold_p)
6755 expr.value = c_fully_fold (expr.value, false, NULL);
6756 VEC_quick_push (tree, ret, expr.value);
6757 if (orig_types != NULL)
6758 VEC_quick_push (tree, orig_types, expr.original_type);
6759 while (c_parser_next_token_is (parser, CPP_COMMA))
6761 c_parser_consume_token (parser);
6762 loc = c_parser_peek_token (parser)->location;
6763 expr = c_parser_expr_no_commas (parser, NULL);
6764 if (convert_p)
6765 expr = default_function_array_read_conversion (loc, expr);
6766 if (fold_p)
6767 expr.value = c_fully_fold (expr.value, false, NULL);
6768 VEC_safe_push (tree, gc, ret, expr.value);
6769 if (orig_types != NULL)
6770 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6772 if (orig_types != NULL)
6773 *p_orig_types = orig_types;
6774 return ret;
6777 /* Parse Objective-C-specific constructs. */
6779 /* Parse an objc-class-definition.
6781 objc-class-definition:
6782 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6783 objc-class-instance-variables[opt] objc-methodprotolist @end
6784 @implementation identifier objc-superclass[opt]
6785 objc-class-instance-variables[opt]
6786 @interface identifier ( identifier ) objc-protocol-refs[opt]
6787 objc-methodprotolist @end
6788 @interface identifier ( ) objc-protocol-refs[opt]
6789 objc-methodprotolist @end
6790 @implementation identifier ( identifier )
6792 objc-superclass:
6793 : identifier
6795 "@interface identifier (" must start "@interface identifier (
6796 identifier ) ...": objc-methodprotolist in the first production may
6797 not start with a parenthesized identifier as a declarator of a data
6798 definition with no declaration specifiers if the objc-superclass,
6799 objc-protocol-refs and objc-class-instance-variables are omitted. */
6801 static void
6802 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6804 bool iface_p;
6805 tree id1;
6806 tree superclass;
6807 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6808 iface_p = true;
6809 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6810 iface_p = false;
6811 else
6812 gcc_unreachable ();
6814 c_parser_consume_token (parser);
6815 if (c_parser_next_token_is_not (parser, CPP_NAME))
6817 c_parser_error (parser, "expected identifier");
6818 return;
6820 id1 = c_parser_peek_token (parser)->value;
6821 c_parser_consume_token (parser);
6822 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6824 /* We have a category or class extension. */
6825 tree id2;
6826 tree proto = NULL_TREE;
6827 c_parser_consume_token (parser);
6828 if (c_parser_next_token_is_not (parser, CPP_NAME))
6830 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6832 /* We have a class extension. */
6833 id2 = NULL_TREE;
6835 else
6837 c_parser_error (parser, "expected identifier or %<)%>");
6838 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6839 return;
6842 else
6844 id2 = c_parser_peek_token (parser)->value;
6845 c_parser_consume_token (parser);
6847 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6848 if (!iface_p)
6850 objc_start_category_implementation (id1, id2);
6851 return;
6853 if (c_parser_next_token_is (parser, CPP_LESS))
6854 proto = c_parser_objc_protocol_refs (parser);
6855 objc_start_category_interface (id1, id2, proto, attributes);
6856 c_parser_objc_methodprotolist (parser);
6857 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6858 objc_finish_interface ();
6859 return;
6861 if (c_parser_next_token_is (parser, CPP_COLON))
6863 c_parser_consume_token (parser);
6864 if (c_parser_next_token_is_not (parser, CPP_NAME))
6866 c_parser_error (parser, "expected identifier");
6867 return;
6869 superclass = c_parser_peek_token (parser)->value;
6870 c_parser_consume_token (parser);
6872 else
6873 superclass = NULL_TREE;
6874 if (iface_p)
6876 tree proto = NULL_TREE;
6877 if (c_parser_next_token_is (parser, CPP_LESS))
6878 proto = c_parser_objc_protocol_refs (parser);
6879 objc_start_class_interface (id1, superclass, proto, attributes);
6881 else
6882 objc_start_class_implementation (id1, superclass);
6883 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6884 c_parser_objc_class_instance_variables (parser);
6885 if (iface_p)
6887 objc_continue_interface ();
6888 c_parser_objc_methodprotolist (parser);
6889 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6890 objc_finish_interface ();
6892 else
6894 objc_continue_implementation ();
6895 return;
6899 /* Parse objc-class-instance-variables.
6901 objc-class-instance-variables:
6902 { objc-instance-variable-decl-list[opt] }
6904 objc-instance-variable-decl-list:
6905 objc-visibility-spec
6906 objc-instance-variable-decl ;
6908 objc-instance-variable-decl-list objc-visibility-spec
6909 objc-instance-variable-decl-list objc-instance-variable-decl ;
6910 objc-instance-variable-decl-list ;
6912 objc-visibility-spec:
6913 @private
6914 @protected
6915 @public
6917 objc-instance-variable-decl:
6918 struct-declaration
6921 static void
6922 c_parser_objc_class_instance_variables (c_parser *parser)
6924 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6925 c_parser_consume_token (parser);
6926 while (c_parser_next_token_is_not (parser, CPP_EOF))
6928 tree decls;
6929 /* Parse any stray semicolon. */
6930 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6932 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6933 "extra semicolon in struct or union specified");
6934 c_parser_consume_token (parser);
6935 continue;
6937 /* Stop if at the end of the instance variables. */
6938 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6940 c_parser_consume_token (parser);
6941 break;
6943 /* Parse any objc-visibility-spec. */
6944 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6946 c_parser_consume_token (parser);
6947 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6948 continue;
6950 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6952 c_parser_consume_token (parser);
6953 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6954 continue;
6956 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6958 c_parser_consume_token (parser);
6959 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6960 continue;
6962 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6964 c_parser_consume_token (parser);
6965 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6966 continue;
6968 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6970 c_parser_pragma (parser, pragma_external);
6971 continue;
6974 /* Parse some comma-separated declarations. */
6975 decls = c_parser_struct_declaration (parser);
6977 /* Comma-separated instance variables are chained together in
6978 reverse order; add them one by one. */
6979 tree ivar = nreverse (decls);
6980 for (; ivar; ivar = DECL_CHAIN (ivar))
6981 objc_add_instance_variable (copy_node (ivar));
6983 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6987 /* Parse an objc-class-declaration.
6989 objc-class-declaration:
6990 @class identifier-list ;
6993 static void
6994 c_parser_objc_class_declaration (c_parser *parser)
6996 tree list = NULL_TREE;
6997 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6998 c_parser_consume_token (parser);
6999 /* Any identifiers, including those declared as type names, are OK
7000 here. */
7001 while (true)
7003 tree id;
7004 if (c_parser_next_token_is_not (parser, CPP_NAME))
7006 c_parser_error (parser, "expected identifier");
7007 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7008 parser->error = false;
7009 return;
7011 id = c_parser_peek_token (parser)->value;
7012 list = chainon (list, build_tree_list (NULL_TREE, id));
7013 c_parser_consume_token (parser);
7014 if (c_parser_next_token_is (parser, CPP_COMMA))
7015 c_parser_consume_token (parser);
7016 else
7017 break;
7019 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7020 objc_declare_class (list);
7023 /* Parse an objc-alias-declaration.
7025 objc-alias-declaration:
7026 @compatibility_alias identifier identifier ;
7029 static void
7030 c_parser_objc_alias_declaration (c_parser *parser)
7032 tree id1, id2;
7033 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7034 c_parser_consume_token (parser);
7035 if (c_parser_next_token_is_not (parser, CPP_NAME))
7037 c_parser_error (parser, "expected identifier");
7038 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7039 return;
7041 id1 = c_parser_peek_token (parser)->value;
7042 c_parser_consume_token (parser);
7043 if (c_parser_next_token_is_not (parser, CPP_NAME))
7045 c_parser_error (parser, "expected identifier");
7046 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7047 return;
7049 id2 = c_parser_peek_token (parser)->value;
7050 c_parser_consume_token (parser);
7051 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7052 objc_declare_alias (id1, id2);
7055 /* Parse an objc-protocol-definition.
7057 objc-protocol-definition:
7058 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7059 @protocol identifier-list ;
7061 "@protocol identifier ;" should be resolved as "@protocol
7062 identifier-list ;": objc-methodprotolist may not start with a
7063 semicolon in the first alternative if objc-protocol-refs are
7064 omitted. */
7066 static void
7067 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7069 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7071 c_parser_consume_token (parser);
7072 if (c_parser_next_token_is_not (parser, CPP_NAME))
7074 c_parser_error (parser, "expected identifier");
7075 return;
7077 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7078 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7080 tree list = NULL_TREE;
7081 /* Any identifiers, including those declared as type names, are
7082 OK here. */
7083 while (true)
7085 tree id;
7086 if (c_parser_next_token_is_not (parser, CPP_NAME))
7088 c_parser_error (parser, "expected identifier");
7089 break;
7091 id = c_parser_peek_token (parser)->value;
7092 list = chainon (list, build_tree_list (NULL_TREE, id));
7093 c_parser_consume_token (parser);
7094 if (c_parser_next_token_is (parser, CPP_COMMA))
7095 c_parser_consume_token (parser);
7096 else
7097 break;
7099 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7100 objc_declare_protocols (list, attributes);
7102 else
7104 tree id = c_parser_peek_token (parser)->value;
7105 tree proto = NULL_TREE;
7106 c_parser_consume_token (parser);
7107 if (c_parser_next_token_is (parser, CPP_LESS))
7108 proto = c_parser_objc_protocol_refs (parser);
7109 parser->objc_pq_context = true;
7110 objc_start_protocol (id, proto, attributes);
7111 c_parser_objc_methodprotolist (parser);
7112 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7113 parser->objc_pq_context = false;
7114 objc_finish_interface ();
7118 /* Parse an objc-method-type.
7120 objc-method-type:
7124 Return true if it is a class method (+) and false if it is
7125 an instance method (-).
7127 static inline bool
7128 c_parser_objc_method_type (c_parser *parser)
7130 switch (c_parser_peek_token (parser)->type)
7132 case CPP_PLUS:
7133 c_parser_consume_token (parser);
7134 return true;
7135 case CPP_MINUS:
7136 c_parser_consume_token (parser);
7137 return false;
7138 default:
7139 gcc_unreachable ();
7143 /* Parse an objc-method-definition.
7145 objc-method-definition:
7146 objc-method-type objc-method-decl ;[opt] compound-statement
7149 static void
7150 c_parser_objc_method_definition (c_parser *parser)
7152 bool is_class_method = c_parser_objc_method_type (parser);
7153 tree decl, attributes = NULL_TREE;
7154 parser->objc_pq_context = true;
7155 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7156 if (decl == error_mark_node)
7157 return; /* Bail here. */
7159 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7161 c_parser_consume_token (parser);
7162 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7163 "extra semicolon in method definition specified");
7166 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7168 c_parser_error (parser, "expected %<{%>");
7169 return;
7172 parser->objc_pq_context = false;
7173 if (objc_start_method_definition (is_class_method, decl, attributes))
7175 add_stmt (c_parser_compound_statement (parser));
7176 objc_finish_method_definition (current_function_decl);
7178 else
7180 /* This code is executed when we find a method definition
7181 outside of an @implementation context (or invalid for other
7182 reasons). Parse the method (to keep going) but do not emit
7183 any code.
7185 c_parser_compound_statement (parser);
7189 /* Parse an objc-methodprotolist.
7191 objc-methodprotolist:
7192 empty
7193 objc-methodprotolist objc-methodproto
7194 objc-methodprotolist declaration
7195 objc-methodprotolist ;
7196 @optional
7197 @required
7199 The declaration is a data definition, which may be missing
7200 declaration specifiers under the same rules and diagnostics as
7201 other data definitions outside functions, and the stray semicolon
7202 is diagnosed the same way as a stray semicolon outside a
7203 function. */
7205 static void
7206 c_parser_objc_methodprotolist (c_parser *parser)
7208 while (true)
7210 /* The list is terminated by @end. */
7211 switch (c_parser_peek_token (parser)->type)
7213 case CPP_SEMICOLON:
7214 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7215 "ISO C does not allow extra %<;%> outside of a function");
7216 c_parser_consume_token (parser);
7217 break;
7218 case CPP_PLUS:
7219 case CPP_MINUS:
7220 c_parser_objc_methodproto (parser);
7221 break;
7222 case CPP_PRAGMA:
7223 c_parser_pragma (parser, pragma_external);
7224 break;
7225 case CPP_EOF:
7226 return;
7227 default:
7228 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7229 return;
7230 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7231 c_parser_objc_at_property_declaration (parser);
7232 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7234 objc_set_method_opt (true);
7235 c_parser_consume_token (parser);
7237 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7239 objc_set_method_opt (false);
7240 c_parser_consume_token (parser);
7242 else
7243 c_parser_declaration_or_fndef (parser, false, false, true,
7244 false, true, NULL);
7245 break;
7250 /* Parse an objc-methodproto.
7252 objc-methodproto:
7253 objc-method-type objc-method-decl ;
7256 static void
7257 c_parser_objc_methodproto (c_parser *parser)
7259 bool is_class_method = c_parser_objc_method_type (parser);
7260 tree decl, attributes = NULL_TREE;
7262 /* Remember protocol qualifiers in prototypes. */
7263 parser->objc_pq_context = true;
7264 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7265 /* Forget protocol qualifiers now. */
7266 parser->objc_pq_context = false;
7268 /* Do not allow the presence of attributes to hide an erroneous
7269 method implementation in the interface section. */
7270 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7272 c_parser_error (parser, "expected %<;%>");
7273 return;
7276 if (decl != error_mark_node)
7277 objc_add_method_declaration (is_class_method, decl, attributes);
7279 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7282 /* If we are at a position that method attributes may be present, check that
7283 there are not any parsed already (a syntax error) and then collect any
7284 specified at the current location. Finally, if new attributes were present,
7285 check that the next token is legal ( ';' for decls and '{' for defs). */
7287 static bool
7288 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7290 bool bad = false;
7291 if (*attributes)
7293 c_parser_error (parser,
7294 "method attributes must be specified at the end only");
7295 *attributes = NULL_TREE;
7296 bad = true;
7299 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7300 *attributes = c_parser_attributes (parser);
7302 /* If there were no attributes here, just report any earlier error. */
7303 if (*attributes == NULL_TREE || bad)
7304 return bad;
7306 /* If the attributes are followed by a ; or {, then just report any earlier
7307 error. */
7308 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7309 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7310 return bad;
7312 /* We've got attributes, but not at the end. */
7313 c_parser_error (parser,
7314 "expected %<;%> or %<{%> after method attribute definition");
7315 return true;
7318 /* Parse an objc-method-decl.
7320 objc-method-decl:
7321 ( objc-type-name ) objc-selector
7322 objc-selector
7323 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7324 objc-keyword-selector objc-optparmlist
7325 attributes
7327 objc-keyword-selector:
7328 objc-keyword-decl
7329 objc-keyword-selector objc-keyword-decl
7331 objc-keyword-decl:
7332 objc-selector : ( objc-type-name ) identifier
7333 objc-selector : identifier
7334 : ( objc-type-name ) identifier
7335 : identifier
7337 objc-optparmlist:
7338 objc-optparms objc-optellipsis
7340 objc-optparms:
7341 empty
7342 objc-opt-parms , parameter-declaration
7344 objc-optellipsis:
7345 empty
7346 , ...
7349 static tree
7350 c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
7352 tree type = NULL_TREE;
7353 tree sel;
7354 tree parms = NULL_TREE;
7355 bool ellipsis = false;
7356 bool attr_err = false;
7358 *attributes = NULL_TREE;
7359 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7361 c_parser_consume_token (parser);
7362 type = c_parser_objc_type_name (parser);
7363 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7365 sel = c_parser_objc_selector (parser);
7366 /* If there is no selector, or a colon follows, we have an
7367 objc-keyword-selector. If there is a selector, and a colon does
7368 not follow, that selector ends the objc-method-decl. */
7369 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7371 tree tsel = sel;
7372 tree list = NULL_TREE;
7373 while (true)
7375 tree atype = NULL_TREE, id, keyworddecl;
7376 tree param_attr = NULL_TREE;
7377 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7378 break;
7379 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7381 c_parser_consume_token (parser);
7382 atype = c_parser_objc_type_name (parser);
7383 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7384 "expected %<)%>");
7386 /* New ObjC allows attributes on method parameters. */
7387 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7388 param_attr = c_parser_attributes (parser);
7389 if (c_parser_next_token_is_not (parser, CPP_NAME))
7391 c_parser_error (parser, "expected identifier");
7392 return error_mark_node;
7394 id = c_parser_peek_token (parser)->value;
7395 c_parser_consume_token (parser);
7396 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7397 list = chainon (list, keyworddecl);
7398 tsel = c_parser_objc_selector (parser);
7399 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7400 break;
7403 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7405 /* Parse the optional parameter list. Optional Objective-C
7406 method parameters follow the C syntax, and may include '...'
7407 to denote a variable number of arguments. */
7408 parms = make_node (TREE_LIST);
7409 while (c_parser_next_token_is (parser, CPP_COMMA))
7411 struct c_parm *parm;
7412 c_parser_consume_token (parser);
7413 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7415 ellipsis = true;
7416 c_parser_consume_token (parser);
7417 attr_err |= c_parser_objc_maybe_method_attributes
7418 (parser, attributes) ;
7419 break;
7421 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7422 if (parm == NULL)
7423 break;
7424 parms = chainon (parms,
7425 build_tree_list (NULL_TREE, grokparm (parm)));
7427 sel = list;
7429 else
7430 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7432 if (sel == NULL)
7434 c_parser_error (parser, "objective-c method declaration is expected");
7435 return error_mark_node;
7438 if (attr_err)
7439 return error_mark_node;
7441 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7444 /* Parse an objc-type-name.
7446 objc-type-name:
7447 objc-type-qualifiers[opt] type-name
7448 objc-type-qualifiers[opt]
7450 objc-type-qualifiers:
7451 objc-type-qualifier
7452 objc-type-qualifiers objc-type-qualifier
7454 objc-type-qualifier: one of
7455 in out inout bycopy byref oneway
7458 static tree
7459 c_parser_objc_type_name (c_parser *parser)
7461 tree quals = NULL_TREE;
7462 struct c_type_name *type_name = NULL;
7463 tree type = NULL_TREE;
7464 while (true)
7466 c_token *token = c_parser_peek_token (parser);
7467 if (token->type == CPP_KEYWORD
7468 && (token->keyword == RID_IN
7469 || token->keyword == RID_OUT
7470 || token->keyword == RID_INOUT
7471 || token->keyword == RID_BYCOPY
7472 || token->keyword == RID_BYREF
7473 || token->keyword == RID_ONEWAY))
7475 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7476 c_parser_consume_token (parser);
7478 else
7479 break;
7481 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7482 type_name = c_parser_type_name (parser);
7483 if (type_name)
7484 type = groktypename (type_name, NULL, NULL);
7485 return build_tree_list (quals, type);
7488 /* Parse objc-protocol-refs.
7490 objc-protocol-refs:
7491 < identifier-list >
7494 static tree
7495 c_parser_objc_protocol_refs (c_parser *parser)
7497 tree list = NULL_TREE;
7498 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7499 c_parser_consume_token (parser);
7500 /* Any identifiers, including those declared as type names, are OK
7501 here. */
7502 while (true)
7504 tree id;
7505 if (c_parser_next_token_is_not (parser, CPP_NAME))
7507 c_parser_error (parser, "expected identifier");
7508 break;
7510 id = c_parser_peek_token (parser)->value;
7511 list = chainon (list, build_tree_list (NULL_TREE, id));
7512 c_parser_consume_token (parser);
7513 if (c_parser_next_token_is (parser, CPP_COMMA))
7514 c_parser_consume_token (parser);
7515 else
7516 break;
7518 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7519 return list;
7522 /* Parse an objc-try-catch-finally-statement.
7524 objc-try-catch-finally-statement:
7525 @try compound-statement objc-catch-list[opt]
7526 @try compound-statement objc-catch-list[opt] @finally compound-statement
7528 objc-catch-list:
7529 @catch ( objc-catch-parameter-declaration ) compound-statement
7530 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7532 objc-catch-parameter-declaration:
7533 parameter-declaration
7534 '...'
7536 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7538 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7539 for C++. Keep them in sync. */
7541 static void
7542 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7544 location_t location;
7545 tree stmt;
7547 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7548 c_parser_consume_token (parser);
7549 location = c_parser_peek_token (parser)->location;
7550 stmt = c_parser_compound_statement (parser);
7551 objc_begin_try_stmt (location, stmt);
7553 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7555 struct c_parm *parm;
7556 tree parameter_declaration = error_mark_node;
7557 bool seen_open_paren = false;
7559 c_parser_consume_token (parser);
7560 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7561 seen_open_paren = true;
7562 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7564 /* We have "@catch (...)" (where the '...' are literally
7565 what is in the code). Skip the '...'.
7566 parameter_declaration is set to NULL_TREE, and
7567 objc_being_catch_clauses() knows that that means
7568 '...'. */
7569 c_parser_consume_token (parser);
7570 parameter_declaration = NULL_TREE;
7572 else
7574 /* We have "@catch (NSException *exception)" or something
7575 like that. Parse the parameter declaration. */
7576 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7577 if (parm == NULL)
7578 parameter_declaration = error_mark_node;
7579 else
7580 parameter_declaration = grokparm (parm);
7582 if (seen_open_paren)
7583 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7584 else
7586 /* If there was no open parenthesis, we are recovering from
7587 an error, and we are trying to figure out what mistake
7588 the user has made. */
7590 /* If there is an immediate closing parenthesis, the user
7591 probably forgot the opening one (ie, they typed "@catch
7592 NSException *e)". Parse the closing parenthesis and keep
7593 going. */
7594 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7595 c_parser_consume_token (parser);
7597 /* If these is no immediate closing parenthesis, the user
7598 probably doesn't know that parenthesis are required at
7599 all (ie, they typed "@catch NSException *e"). So, just
7600 forget about the closing parenthesis and keep going. */
7602 objc_begin_catch_clause (parameter_declaration);
7603 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7604 c_parser_compound_statement_nostart (parser);
7605 objc_finish_catch_clause ();
7607 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7609 c_parser_consume_token (parser);
7610 location = c_parser_peek_token (parser)->location;
7611 stmt = c_parser_compound_statement (parser);
7612 objc_build_finally_clause (location, stmt);
7614 objc_finish_try_stmt ();
7617 /* Parse an objc-synchronized-statement.
7619 objc-synchronized-statement:
7620 @synchronized ( expression ) compound-statement
7623 static void
7624 c_parser_objc_synchronized_statement (c_parser *parser)
7626 location_t loc;
7627 tree expr, stmt;
7628 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7629 c_parser_consume_token (parser);
7630 loc = c_parser_peek_token (parser)->location;
7631 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7633 expr = c_parser_expression (parser).value;
7634 expr = c_fully_fold (expr, false, NULL);
7635 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7637 else
7638 expr = error_mark_node;
7639 stmt = c_parser_compound_statement (parser);
7640 objc_build_synchronized (loc, expr, stmt);
7643 /* Parse an objc-selector; return NULL_TREE without an error if the
7644 next token is not an objc-selector.
7646 objc-selector:
7647 identifier
7648 one of
7649 enum struct union if else while do for switch case default
7650 break continue return goto asm sizeof typeof __alignof
7651 unsigned long const short volatile signed restrict _Complex
7652 in out inout bycopy byref oneway int char float double void _Bool
7654 ??? Why this selection of keywords but not, for example, storage
7655 class specifiers? */
7657 static tree
7658 c_parser_objc_selector (c_parser *parser)
7660 c_token *token = c_parser_peek_token (parser);
7661 tree value = token->value;
7662 if (token->type == CPP_NAME)
7664 c_parser_consume_token (parser);
7665 return value;
7667 if (token->type != CPP_KEYWORD)
7668 return NULL_TREE;
7669 switch (token->keyword)
7671 case RID_ENUM:
7672 case RID_STRUCT:
7673 case RID_UNION:
7674 case RID_IF:
7675 case RID_ELSE:
7676 case RID_WHILE:
7677 case RID_DO:
7678 case RID_FOR:
7679 case RID_SWITCH:
7680 case RID_CASE:
7681 case RID_DEFAULT:
7682 case RID_BREAK:
7683 case RID_CONTINUE:
7684 case RID_RETURN:
7685 case RID_GOTO:
7686 case RID_ASM:
7687 case RID_SIZEOF:
7688 case RID_TYPEOF:
7689 case RID_ALIGNOF:
7690 case RID_UNSIGNED:
7691 case RID_LONG:
7692 case RID_INT128:
7693 case RID_CONST:
7694 case RID_SHORT:
7695 case RID_VOLATILE:
7696 case RID_SIGNED:
7697 case RID_RESTRICT:
7698 case RID_COMPLEX:
7699 case RID_IN:
7700 case RID_OUT:
7701 case RID_INOUT:
7702 case RID_BYCOPY:
7703 case RID_BYREF:
7704 case RID_ONEWAY:
7705 case RID_INT:
7706 case RID_CHAR:
7707 case RID_FLOAT:
7708 case RID_DOUBLE:
7709 case RID_VOID:
7710 case RID_BOOL:
7711 c_parser_consume_token (parser);
7712 return value;
7713 default:
7714 return NULL_TREE;
7718 /* Parse an objc-selector-arg.
7720 objc-selector-arg:
7721 objc-selector
7722 objc-keywordname-list
7724 objc-keywordname-list:
7725 objc-keywordname
7726 objc-keywordname-list objc-keywordname
7728 objc-keywordname:
7729 objc-selector :
7733 static tree
7734 c_parser_objc_selector_arg (c_parser *parser)
7736 tree sel = c_parser_objc_selector (parser);
7737 tree list = NULL_TREE;
7738 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7739 return sel;
7740 while (true)
7742 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7743 return list;
7744 list = chainon (list, build_tree_list (sel, NULL_TREE));
7745 sel = c_parser_objc_selector (parser);
7746 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7747 break;
7749 return list;
7752 /* Parse an objc-receiver.
7754 objc-receiver:
7755 expression
7756 class-name
7757 type-name
7760 static tree
7761 c_parser_objc_receiver (c_parser *parser)
7763 if (c_parser_peek_token (parser)->type == CPP_NAME
7764 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7765 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7767 tree id = c_parser_peek_token (parser)->value;
7768 c_parser_consume_token (parser);
7769 return objc_get_class_reference (id);
7771 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7774 /* Parse objc-message-args.
7776 objc-message-args:
7777 objc-selector
7778 objc-keywordarg-list
7780 objc-keywordarg-list:
7781 objc-keywordarg
7782 objc-keywordarg-list objc-keywordarg
7784 objc-keywordarg:
7785 objc-selector : objc-keywordexpr
7786 : objc-keywordexpr
7789 static tree
7790 c_parser_objc_message_args (c_parser *parser)
7792 tree sel = c_parser_objc_selector (parser);
7793 tree list = NULL_TREE;
7794 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7795 return sel;
7796 while (true)
7798 tree keywordexpr;
7799 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7800 return error_mark_node;
7801 keywordexpr = c_parser_objc_keywordexpr (parser);
7802 list = chainon (list, build_tree_list (sel, keywordexpr));
7803 sel = c_parser_objc_selector (parser);
7804 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7805 break;
7807 return list;
7810 /* Parse an objc-keywordexpr.
7812 objc-keywordexpr:
7813 nonempty-expr-list
7816 static tree
7817 c_parser_objc_keywordexpr (c_parser *parser)
7819 tree ret;
7820 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7821 if (VEC_length (tree, expr_list) == 1)
7823 /* Just return the expression, remove a level of
7824 indirection. */
7825 ret = VEC_index (tree, expr_list, 0);
7827 else
7829 /* We have a comma expression, we will collapse later. */
7830 ret = build_tree_list_vec (expr_list);
7832 release_tree_vector (expr_list);
7833 return ret;
7836 /* A check, needed in several places, that ObjC interface, implementation or
7837 method definitions are not prefixed by incorrect items. */
7838 static bool
7839 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7840 struct c_declspecs *specs)
7842 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7843 || specs->typespec_kind != ctsk_none)
7845 c_parser_error (parser,
7846 "no type or storage class may be specified here,");
7847 c_parser_skip_to_end_of_block_or_statement (parser);
7848 return true;
7850 return false;
7853 /* Parse an Objective-C @property declaration. The syntax is:
7855 objc-property-declaration:
7856 '@property' objc-property-attributes[opt] struct-declaration ;
7858 objc-property-attributes:
7859 '(' objc-property-attribute-list ')'
7861 objc-property-attribute-list:
7862 objc-property-attribute
7863 objc-property-attribute-list, objc-property-attribute
7865 objc-property-attribute
7866 'getter' = identifier
7867 'setter' = identifier
7868 'readonly'
7869 'readwrite'
7870 'assign'
7871 'retain'
7872 'copy'
7873 'nonatomic'
7875 For example:
7876 @property NSString *name;
7877 @property (readonly) id object;
7878 @property (retain, nonatomic, getter=getTheName) id name;
7879 @property int a, b, c;
7881 PS: This function is identical to cp_parser_objc_at_propery_declaration
7882 for C++. Keep them in sync. */
7883 static void
7884 c_parser_objc_at_property_declaration (c_parser *parser)
7886 /* The following variables hold the attributes of the properties as
7887 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7888 seen. When we see an attribute, we set them to 'true' (if they
7889 are boolean properties) or to the identifier (if they have an
7890 argument, ie, for getter and setter). Note that here we only
7891 parse the list of attributes, check the syntax and accumulate the
7892 attributes that we find. objc_add_property_declaration() will
7893 then process the information. */
7894 bool property_assign = false;
7895 bool property_copy = false;
7896 tree property_getter_ident = NULL_TREE;
7897 bool property_nonatomic = false;
7898 bool property_readonly = false;
7899 bool property_readwrite = false;
7900 bool property_retain = false;
7901 tree property_setter_ident = NULL_TREE;
7903 /* 'properties' is the list of properties that we read. Usually a
7904 single one, but maybe more (eg, in "@property int a, b, c;" there
7905 are three). */
7906 tree properties;
7907 location_t loc;
7909 loc = c_parser_peek_token (parser)->location;
7910 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7912 c_parser_consume_token (parser); /* Eat '@property'. */
7914 /* Parse the optional attribute list... */
7915 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7917 /* Eat the '(' */
7918 c_parser_consume_token (parser);
7920 /* Property attribute keywords are valid now. */
7921 parser->objc_property_attr_context = true;
7923 while (true)
7925 bool syntax_error = false;
7926 c_token *token = c_parser_peek_token (parser);
7927 enum rid keyword;
7929 if (token->type != CPP_KEYWORD)
7931 if (token->type == CPP_CLOSE_PAREN)
7932 c_parser_error (parser, "expected identifier");
7933 else
7935 c_parser_consume_token (parser);
7936 c_parser_error (parser, "unknown property attribute");
7938 break;
7940 keyword = token->keyword;
7941 c_parser_consume_token (parser);
7942 switch (keyword)
7944 case RID_ASSIGN: property_assign = true; break;
7945 case RID_COPY: property_copy = true; break;
7946 case RID_NONATOMIC: property_nonatomic = true; break;
7947 case RID_READONLY: property_readonly = true; break;
7948 case RID_READWRITE: property_readwrite = true; break;
7949 case RID_RETAIN: property_retain = true; break;
7951 case RID_GETTER:
7952 case RID_SETTER:
7953 if (c_parser_next_token_is_not (parser, CPP_EQ))
7955 c_parser_error (parser,
7956 "getter/setter attribute must be followed by %<=%>");
7957 syntax_error = true;
7958 break;
7960 c_parser_consume_token (parser); /* eat the = */
7961 if (c_parser_next_token_is_not (parser, CPP_NAME))
7963 c_parser_error (parser, "expected identifier");
7964 syntax_error = true;
7965 break;
7967 if (keyword == RID_SETTER)
7969 if (property_setter_ident != NULL_TREE)
7970 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7971 else
7972 property_setter_ident = c_parser_peek_token (parser)->value;
7973 c_parser_consume_token (parser);
7974 if (c_parser_next_token_is_not (parser, CPP_COLON))
7975 c_parser_error (parser, "setter name must terminate with %<:%>");
7976 else
7977 c_parser_consume_token (parser);
7979 else
7981 if (property_getter_ident != NULL_TREE)
7982 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
7983 else
7984 property_getter_ident = c_parser_peek_token (parser)->value;
7985 c_parser_consume_token (parser);
7987 break;
7988 default:
7989 c_parser_error (parser, "unknown property attribute");
7990 syntax_error = true;
7991 break;
7994 if (syntax_error)
7995 break;
7997 if (c_parser_next_token_is (parser, CPP_COMMA))
7998 c_parser_consume_token (parser);
7999 else
8000 break;
8002 parser->objc_property_attr_context = false;
8003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8005 /* ... and the property declaration(s). */
8006 properties = c_parser_struct_declaration (parser);
8008 if (properties == error_mark_node)
8010 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8011 parser->error = false;
8012 return;
8015 if (properties == NULL_TREE)
8016 c_parser_error (parser, "expected identifier");
8017 else
8019 /* Comma-separated properties are chained together in
8020 reverse order; add them one by one. */
8021 properties = nreverse (properties);
8023 for (; properties; properties = TREE_CHAIN (properties))
8024 objc_add_property_declaration (loc, copy_node (properties),
8025 property_readonly, property_readwrite,
8026 property_assign, property_retain,
8027 property_copy, property_nonatomic,
8028 property_getter_ident, property_setter_ident);
8031 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8032 parser->error = false;
8035 /* Parse an Objective-C @synthesize declaration. The syntax is:
8037 objc-synthesize-declaration:
8038 @synthesize objc-synthesize-identifier-list ;
8040 objc-synthesize-identifier-list:
8041 objc-synthesize-identifier
8042 objc-synthesize-identifier-list, objc-synthesize-identifier
8044 objc-synthesize-identifier
8045 identifier
8046 identifier = identifier
8048 For example:
8049 @synthesize MyProperty;
8050 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8052 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8053 for C++. Keep them in sync.
8055 static void
8056 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8058 tree list = NULL_TREE;
8059 location_t loc;
8060 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8061 loc = c_parser_peek_token (parser)->location;
8063 c_parser_consume_token (parser);
8064 while (true)
8066 tree property, ivar;
8067 if (c_parser_next_token_is_not (parser, CPP_NAME))
8069 c_parser_error (parser, "expected identifier");
8070 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8071 /* Once we find the semicolon, we can resume normal parsing.
8072 We have to reset parser->error manually because
8073 c_parser_skip_until_found() won't reset it for us if the
8074 next token is precisely a semicolon. */
8075 parser->error = false;
8076 return;
8078 property = c_parser_peek_token (parser)->value;
8079 c_parser_consume_token (parser);
8080 if (c_parser_next_token_is (parser, CPP_EQ))
8082 c_parser_consume_token (parser);
8083 if (c_parser_next_token_is_not (parser, CPP_NAME))
8085 c_parser_error (parser, "expected identifier");
8086 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8087 parser->error = false;
8088 return;
8090 ivar = c_parser_peek_token (parser)->value;
8091 c_parser_consume_token (parser);
8093 else
8094 ivar = NULL_TREE;
8095 list = chainon (list, build_tree_list (ivar, property));
8096 if (c_parser_next_token_is (parser, CPP_COMMA))
8097 c_parser_consume_token (parser);
8098 else
8099 break;
8101 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8102 objc_add_synthesize_declaration (loc, list);
8105 /* Parse an Objective-C @dynamic declaration. The syntax is:
8107 objc-dynamic-declaration:
8108 @dynamic identifier-list ;
8110 For example:
8111 @dynamic MyProperty;
8112 @dynamic MyProperty, AnotherProperty;
8114 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8115 for C++. Keep them in sync.
8117 static void
8118 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8120 tree list = NULL_TREE;
8121 location_t loc;
8122 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8123 loc = c_parser_peek_token (parser)->location;
8125 c_parser_consume_token (parser);
8126 while (true)
8128 tree property;
8129 if (c_parser_next_token_is_not (parser, CPP_NAME))
8131 c_parser_error (parser, "expected identifier");
8132 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8133 parser->error = false;
8134 return;
8136 property = c_parser_peek_token (parser)->value;
8137 list = chainon (list, build_tree_list (NULL_TREE, property));
8138 c_parser_consume_token (parser);
8139 if (c_parser_next_token_is (parser, CPP_COMMA))
8140 c_parser_consume_token (parser);
8141 else
8142 break;
8144 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8145 objc_add_dynamic_declaration (loc, list);
8149 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8150 should be considered, statements. ALLOW_STMT is true if we're within
8151 the context of a function and such pragmas are to be allowed. Returns
8152 true if we actually parsed such a pragma. */
8154 static bool
8155 c_parser_pragma (c_parser *parser, enum pragma_context context)
8157 unsigned int id;
8159 id = c_parser_peek_token (parser)->pragma_kind;
8160 gcc_assert (id != PRAGMA_NONE);
8162 switch (id)
8164 case PRAGMA_OMP_BARRIER:
8165 if (context != pragma_compound)
8167 if (context == pragma_stmt)
8168 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8169 "used in compound statements");
8170 goto bad_stmt;
8172 c_parser_omp_barrier (parser);
8173 return false;
8175 case PRAGMA_OMP_FLUSH:
8176 if (context != pragma_compound)
8178 if (context == pragma_stmt)
8179 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8180 "used in compound statements");
8181 goto bad_stmt;
8183 c_parser_omp_flush (parser);
8184 return false;
8186 case PRAGMA_OMP_TASKWAIT:
8187 if (context != pragma_compound)
8189 if (context == pragma_stmt)
8190 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8191 "used in compound statements");
8192 goto bad_stmt;
8194 c_parser_omp_taskwait (parser);
8195 return false;
8197 case PRAGMA_OMP_THREADPRIVATE:
8198 c_parser_omp_threadprivate (parser);
8199 return false;
8201 case PRAGMA_OMP_SECTION:
8202 error_at (c_parser_peek_token (parser)->location,
8203 "%<#pragma omp section%> may only be used in "
8204 "%<#pragma omp sections%> construct");
8205 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8206 return false;
8208 case PRAGMA_GCC_PCH_PREPROCESS:
8209 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8210 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8211 return false;
8213 default:
8214 if (id < PRAGMA_FIRST_EXTERNAL)
8216 if (context == pragma_external)
8218 bad_stmt:
8219 c_parser_error (parser, "expected declaration specifiers");
8220 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8221 return false;
8223 c_parser_omp_construct (parser);
8224 return true;
8226 break;
8229 c_parser_consume_pragma (parser);
8230 c_invoke_pragma_handler (id);
8232 /* Skip to EOL, but suppress any error message. Those will have been
8233 generated by the handler routine through calling error, as opposed
8234 to calling c_parser_error. */
8235 parser->error = true;
8236 c_parser_skip_to_pragma_eol (parser);
8238 return false;
8241 /* The interface the pragma parsers have to the lexer. */
8243 enum cpp_ttype
8244 pragma_lex (tree *value)
8246 c_token *tok = c_parser_peek_token (the_parser);
8247 enum cpp_ttype ret = tok->type;
8249 *value = tok->value;
8250 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8251 ret = CPP_EOF;
8252 else
8254 if (ret == CPP_KEYWORD)
8255 ret = CPP_NAME;
8256 c_parser_consume_token (the_parser);
8259 return ret;
8262 static void
8263 c_parser_pragma_pch_preprocess (c_parser *parser)
8265 tree name = NULL;
8267 c_parser_consume_pragma (parser);
8268 if (c_parser_next_token_is (parser, CPP_STRING))
8270 name = c_parser_peek_token (parser)->value;
8271 c_parser_consume_token (parser);
8273 else
8274 c_parser_error (parser, "expected string literal");
8275 c_parser_skip_to_pragma_eol (parser);
8277 if (name)
8278 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8281 /* OpenMP 2.5 parsing routines. */
8283 /* Returns name of the next clause.
8284 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8285 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8286 returned and the token is consumed. */
8288 static pragma_omp_clause
8289 c_parser_omp_clause_name (c_parser *parser)
8291 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8293 if (c_parser_next_token_is_keyword (parser, RID_IF))
8294 result = PRAGMA_OMP_CLAUSE_IF;
8295 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8296 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8297 else if (c_parser_next_token_is (parser, CPP_NAME))
8299 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8301 switch (p[0])
8303 case 'c':
8304 if (!strcmp ("collapse", p))
8305 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8306 else if (!strcmp ("copyin", p))
8307 result = PRAGMA_OMP_CLAUSE_COPYIN;
8308 else if (!strcmp ("copyprivate", p))
8309 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8310 break;
8311 case 'f':
8312 if (!strcmp ("firstprivate", p))
8313 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8314 break;
8315 case 'l':
8316 if (!strcmp ("lastprivate", p))
8317 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8318 break;
8319 case 'n':
8320 if (!strcmp ("nowait", p))
8321 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8322 else if (!strcmp ("num_threads", p))
8323 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8324 break;
8325 case 'o':
8326 if (!strcmp ("ordered", p))
8327 result = PRAGMA_OMP_CLAUSE_ORDERED;
8328 break;
8329 case 'p':
8330 if (!strcmp ("private", p))
8331 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8332 break;
8333 case 'r':
8334 if (!strcmp ("reduction", p))
8335 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8336 break;
8337 case 's':
8338 if (!strcmp ("schedule", p))
8339 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8340 else if (!strcmp ("shared", p))
8341 result = PRAGMA_OMP_CLAUSE_SHARED;
8342 break;
8343 case 'u':
8344 if (!strcmp ("untied", p))
8345 result = PRAGMA_OMP_CLAUSE_UNTIED;
8346 break;
8350 if (result != PRAGMA_OMP_CLAUSE_NONE)
8351 c_parser_consume_token (parser);
8353 return result;
8356 /* Validate that a clause of the given type does not already exist. */
8358 static void
8359 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8360 const char *name)
8362 tree c;
8364 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8365 if (OMP_CLAUSE_CODE (c) == code)
8367 location_t loc = OMP_CLAUSE_LOCATION (c);
8368 error_at (loc, "too many %qs clauses", name);
8369 break;
8373 /* OpenMP 2.5:
8374 variable-list:
8375 identifier
8376 variable-list , identifier
8378 If KIND is nonzero, create the appropriate node and install the
8379 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8380 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8382 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8383 return the list created. */
8385 static tree
8386 c_parser_omp_variable_list (c_parser *parser,
8387 location_t clause_loc,
8388 enum omp_clause_code kind,
8389 tree list)
8391 if (c_parser_next_token_is_not (parser, CPP_NAME)
8392 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8393 c_parser_error (parser, "expected identifier");
8395 while (c_parser_next_token_is (parser, CPP_NAME)
8396 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8398 tree t = lookup_name (c_parser_peek_token (parser)->value);
8400 if (t == NULL_TREE)
8401 undeclared_variable (c_parser_peek_token (parser)->location,
8402 c_parser_peek_token (parser)->value);
8403 else if (t == error_mark_node)
8405 else if (kind != 0)
8407 tree u = build_omp_clause (clause_loc, kind);
8408 OMP_CLAUSE_DECL (u) = t;
8409 OMP_CLAUSE_CHAIN (u) = list;
8410 list = u;
8412 else
8413 list = tree_cons (t, NULL_TREE, list);
8415 c_parser_consume_token (parser);
8417 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8418 break;
8420 c_parser_consume_token (parser);
8423 return list;
8426 /* Similarly, but expect leading and trailing parenthesis. This is a very
8427 common case for omp clauses. */
8429 static tree
8430 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8431 tree list)
8433 /* The clauses location. */
8434 location_t loc = c_parser_peek_token (parser)->location;
8436 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8438 list = c_parser_omp_variable_list (parser, loc, kind, list);
8439 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8441 return list;
8444 /* OpenMP 3.0:
8445 collapse ( constant-expression ) */
8447 static tree
8448 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8450 tree c, num = error_mark_node;
8451 HOST_WIDE_INT n;
8452 location_t loc;
8454 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8456 loc = c_parser_peek_token (parser)->location;
8457 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8459 num = c_parser_expr_no_commas (parser, NULL).value;
8460 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8462 if (num == error_mark_node)
8463 return list;
8464 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8465 || !host_integerp (num, 0)
8466 || (n = tree_low_cst (num, 0)) <= 0
8467 || (int) n != n)
8469 error_at (loc,
8470 "collapse argument needs positive constant integer expression");
8471 return list;
8473 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8474 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8475 OMP_CLAUSE_CHAIN (c) = list;
8476 return c;
8479 /* OpenMP 2.5:
8480 copyin ( variable-list ) */
8482 static tree
8483 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8485 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8488 /* OpenMP 2.5:
8489 copyprivate ( variable-list ) */
8491 static tree
8492 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8494 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8497 /* OpenMP 2.5:
8498 default ( shared | none ) */
8500 static tree
8501 c_parser_omp_clause_default (c_parser *parser, tree list)
8503 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8504 location_t loc = c_parser_peek_token (parser)->location;
8505 tree c;
8507 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8508 return list;
8509 if (c_parser_next_token_is (parser, CPP_NAME))
8511 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8513 switch (p[0])
8515 case 'n':
8516 if (strcmp ("none", p) != 0)
8517 goto invalid_kind;
8518 kind = OMP_CLAUSE_DEFAULT_NONE;
8519 break;
8521 case 's':
8522 if (strcmp ("shared", p) != 0)
8523 goto invalid_kind;
8524 kind = OMP_CLAUSE_DEFAULT_SHARED;
8525 break;
8527 default:
8528 goto invalid_kind;
8531 c_parser_consume_token (parser);
8533 else
8535 invalid_kind:
8536 c_parser_error (parser, "expected %<none%> or %<shared%>");
8538 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8540 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8541 return list;
8543 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8544 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8545 OMP_CLAUSE_CHAIN (c) = list;
8546 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8548 return c;
8551 /* OpenMP 2.5:
8552 firstprivate ( variable-list ) */
8554 static tree
8555 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8557 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8560 /* OpenMP 2.5:
8561 if ( expression ) */
8563 static tree
8564 c_parser_omp_clause_if (c_parser *parser, tree list)
8566 location_t loc = c_parser_peek_token (parser)->location;
8567 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8569 tree t = c_parser_paren_condition (parser);
8570 tree c;
8572 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8574 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8575 OMP_CLAUSE_IF_EXPR (c) = t;
8576 OMP_CLAUSE_CHAIN (c) = list;
8577 list = c;
8579 else
8580 c_parser_error (parser, "expected %<(%>");
8582 return list;
8585 /* OpenMP 2.5:
8586 lastprivate ( variable-list ) */
8588 static tree
8589 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8591 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8594 /* OpenMP 2.5:
8595 nowait */
8597 static tree
8598 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8600 tree c;
8601 location_t loc = c_parser_peek_token (parser)->location;
8603 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8605 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8606 OMP_CLAUSE_CHAIN (c) = list;
8607 return c;
8610 /* OpenMP 2.5:
8611 num_threads ( expression ) */
8613 static tree
8614 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8616 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8617 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8619 location_t expr_loc = c_parser_peek_token (parser)->location;
8620 tree c, t = c_parser_expression (parser).value;
8621 t = c_fully_fold (t, false, NULL);
8623 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8625 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8627 c_parser_error (parser, "expected integer expression");
8628 return list;
8631 /* Attempt to statically determine when the number isn't positive. */
8632 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8633 build_int_cst (TREE_TYPE (t), 0));
8634 if (CAN_HAVE_LOCATION_P (c))
8635 SET_EXPR_LOCATION (c, expr_loc);
8636 if (c == boolean_true_node)
8638 warning_at (expr_loc, 0,
8639 "%<num_threads%> value must be positive");
8640 t = integer_one_node;
8643 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8645 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8646 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8647 OMP_CLAUSE_CHAIN (c) = list;
8648 list = c;
8651 return list;
8654 /* OpenMP 2.5:
8655 ordered */
8657 static tree
8658 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8660 tree c;
8662 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8664 c = build_omp_clause (c_parser_peek_token (parser)->location,
8665 OMP_CLAUSE_ORDERED);
8666 OMP_CLAUSE_CHAIN (c) = list;
8668 return c;
8671 /* OpenMP 2.5:
8672 private ( variable-list ) */
8674 static tree
8675 c_parser_omp_clause_private (c_parser *parser, tree list)
8677 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8680 /* OpenMP 2.5:
8681 reduction ( reduction-operator : variable-list )
8683 reduction-operator:
8684 One of: + * - & ^ | && || */
8686 static tree
8687 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8689 location_t clause_loc = c_parser_peek_token (parser)->location;
8690 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8692 enum tree_code code;
8694 switch (c_parser_peek_token (parser)->type)
8696 case CPP_PLUS:
8697 code = PLUS_EXPR;
8698 break;
8699 case CPP_MULT:
8700 code = MULT_EXPR;
8701 break;
8702 case CPP_MINUS:
8703 code = MINUS_EXPR;
8704 break;
8705 case CPP_AND:
8706 code = BIT_AND_EXPR;
8707 break;
8708 case CPP_XOR:
8709 code = BIT_XOR_EXPR;
8710 break;
8711 case CPP_OR:
8712 code = BIT_IOR_EXPR;
8713 break;
8714 case CPP_AND_AND:
8715 code = TRUTH_ANDIF_EXPR;
8716 break;
8717 case CPP_OR_OR:
8718 code = TRUTH_ORIF_EXPR;
8719 break;
8720 default:
8721 c_parser_error (parser,
8722 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8723 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8724 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8725 return list;
8727 c_parser_consume_token (parser);
8728 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8730 tree nl, c;
8732 nl = c_parser_omp_variable_list (parser, clause_loc,
8733 OMP_CLAUSE_REDUCTION, list);
8734 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8735 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8737 list = nl;
8739 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8741 return list;
8744 /* OpenMP 2.5:
8745 schedule ( schedule-kind )
8746 schedule ( schedule-kind , expression )
8748 schedule-kind:
8749 static | dynamic | guided | runtime | auto
8752 static tree
8753 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8755 tree c, t;
8756 location_t loc = c_parser_peek_token (parser)->location;
8758 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8759 return list;
8761 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8763 if (c_parser_next_token_is (parser, CPP_NAME))
8765 tree kind = c_parser_peek_token (parser)->value;
8766 const char *p = IDENTIFIER_POINTER (kind);
8768 switch (p[0])
8770 case 'd':
8771 if (strcmp ("dynamic", p) != 0)
8772 goto invalid_kind;
8773 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8774 break;
8776 case 'g':
8777 if (strcmp ("guided", p) != 0)
8778 goto invalid_kind;
8779 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8780 break;
8782 case 'r':
8783 if (strcmp ("runtime", p) != 0)
8784 goto invalid_kind;
8785 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8786 break;
8788 default:
8789 goto invalid_kind;
8792 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8793 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8794 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8795 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8796 else
8797 goto invalid_kind;
8799 c_parser_consume_token (parser);
8800 if (c_parser_next_token_is (parser, CPP_COMMA))
8802 location_t here;
8803 c_parser_consume_token (parser);
8805 here = c_parser_peek_token (parser)->location;
8806 t = c_parser_expr_no_commas (parser, NULL).value;
8807 t = c_fully_fold (t, false, NULL);
8809 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8810 error_at (here, "schedule %<runtime%> does not take "
8811 "a %<chunk_size%> parameter");
8812 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8813 error_at (here,
8814 "schedule %<auto%> does not take "
8815 "a %<chunk_size%> parameter");
8816 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8817 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8818 else
8819 c_parser_error (parser, "expected integer expression");
8821 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8823 else
8824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8825 "expected %<,%> or %<)%>");
8827 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8828 OMP_CLAUSE_CHAIN (c) = list;
8829 return c;
8831 invalid_kind:
8832 c_parser_error (parser, "invalid schedule kind");
8833 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8834 return list;
8837 /* OpenMP 2.5:
8838 shared ( variable-list ) */
8840 static tree
8841 c_parser_omp_clause_shared (c_parser *parser, tree list)
8843 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8846 /* OpenMP 3.0:
8847 untied */
8849 static tree
8850 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8852 tree c;
8854 /* FIXME: Should we allow duplicates? */
8855 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8857 c = build_omp_clause (c_parser_peek_token (parser)->location,
8858 OMP_CLAUSE_UNTIED);
8859 OMP_CLAUSE_CHAIN (c) = list;
8861 return c;
8864 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8865 is a bitmask in MASK. Return the list of clauses found; the result
8866 of clause default goes in *pdefault. */
8868 static tree
8869 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8870 const char *where)
8872 tree clauses = NULL;
8873 bool first = true;
8875 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8877 location_t here;
8878 pragma_omp_clause c_kind;
8879 const char *c_name;
8880 tree prev = clauses;
8882 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8883 c_parser_consume_token (parser);
8885 first = false;
8886 here = c_parser_peek_token (parser)->location;
8887 c_kind = c_parser_omp_clause_name (parser);
8889 switch (c_kind)
8891 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8892 clauses = c_parser_omp_clause_collapse (parser, clauses);
8893 c_name = "collapse";
8894 break;
8895 case PRAGMA_OMP_CLAUSE_COPYIN:
8896 clauses = c_parser_omp_clause_copyin (parser, clauses);
8897 c_name = "copyin";
8898 break;
8899 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8900 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8901 c_name = "copyprivate";
8902 break;
8903 case PRAGMA_OMP_CLAUSE_DEFAULT:
8904 clauses = c_parser_omp_clause_default (parser, clauses);
8905 c_name = "default";
8906 break;
8907 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8908 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8909 c_name = "firstprivate";
8910 break;
8911 case PRAGMA_OMP_CLAUSE_IF:
8912 clauses = c_parser_omp_clause_if (parser, clauses);
8913 c_name = "if";
8914 break;
8915 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8916 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8917 c_name = "lastprivate";
8918 break;
8919 case PRAGMA_OMP_CLAUSE_NOWAIT:
8920 clauses = c_parser_omp_clause_nowait (parser, clauses);
8921 c_name = "nowait";
8922 break;
8923 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8924 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8925 c_name = "num_threads";
8926 break;
8927 case PRAGMA_OMP_CLAUSE_ORDERED:
8928 clauses = c_parser_omp_clause_ordered (parser, clauses);
8929 c_name = "ordered";
8930 break;
8931 case PRAGMA_OMP_CLAUSE_PRIVATE:
8932 clauses = c_parser_omp_clause_private (parser, clauses);
8933 c_name = "private";
8934 break;
8935 case PRAGMA_OMP_CLAUSE_REDUCTION:
8936 clauses = c_parser_omp_clause_reduction (parser, clauses);
8937 c_name = "reduction";
8938 break;
8939 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8940 clauses = c_parser_omp_clause_schedule (parser, clauses);
8941 c_name = "schedule";
8942 break;
8943 case PRAGMA_OMP_CLAUSE_SHARED:
8944 clauses = c_parser_omp_clause_shared (parser, clauses);
8945 c_name = "shared";
8946 break;
8947 case PRAGMA_OMP_CLAUSE_UNTIED:
8948 clauses = c_parser_omp_clause_untied (parser, clauses);
8949 c_name = "untied";
8950 break;
8951 default:
8952 c_parser_error (parser, "expected %<#pragma omp%> clause");
8953 goto saw_error;
8956 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8958 /* Remove the invalid clause(s) from the list to avoid
8959 confusing the rest of the compiler. */
8960 clauses = prev;
8961 error_at (here, "%qs is not valid for %qs", c_name, where);
8965 saw_error:
8966 c_parser_skip_to_pragma_eol (parser);
8968 return c_finish_omp_clauses (clauses);
8971 /* OpenMP 2.5:
8972 structured-block:
8973 statement
8975 In practice, we're also interested in adding the statement to an
8976 outer node. So it is convenient if we work around the fact that
8977 c_parser_statement calls add_stmt. */
8979 static tree
8980 c_parser_omp_structured_block (c_parser *parser)
8982 tree stmt = push_stmt_list ();
8983 c_parser_statement (parser);
8984 return pop_stmt_list (stmt);
8987 /* OpenMP 2.5:
8988 # pragma omp atomic new-line
8989 expression-stmt
8991 expression-stmt:
8992 x binop= expr | x++ | ++x | x-- | --x
8993 binop:
8994 +, *, -, /, &, ^, |, <<, >>
8996 where x is an lvalue expression with scalar type.
8998 LOC is the location of the #pragma token. */
9000 static void
9001 c_parser_omp_atomic (location_t loc, c_parser *parser)
9003 tree lhs, rhs;
9004 tree stmt;
9005 enum tree_code code;
9006 struct c_expr rhs_expr;
9008 c_parser_skip_to_pragma_eol (parser);
9010 lhs = c_parser_unary_expression (parser).value;
9011 lhs = c_fully_fold (lhs, false, NULL);
9012 switch (TREE_CODE (lhs))
9014 case ERROR_MARK:
9015 saw_error:
9016 c_parser_skip_to_end_of_block_or_statement (parser);
9017 return;
9019 case PREINCREMENT_EXPR:
9020 case POSTINCREMENT_EXPR:
9021 lhs = TREE_OPERAND (lhs, 0);
9022 code = PLUS_EXPR;
9023 rhs = integer_one_node;
9024 break;
9026 case PREDECREMENT_EXPR:
9027 case POSTDECREMENT_EXPR:
9028 lhs = TREE_OPERAND (lhs, 0);
9029 code = MINUS_EXPR;
9030 rhs = integer_one_node;
9031 break;
9033 case COMPOUND_EXPR:
9034 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9035 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9036 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9037 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9038 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9039 (TREE_OPERAND (lhs, 1), 0), 0)))
9040 == BOOLEAN_TYPE)
9041 /* Undo effects of boolean_increment for post {in,de}crement. */
9042 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9043 /* FALLTHRU */
9044 case MODIFY_EXPR:
9045 if (TREE_CODE (lhs) == MODIFY_EXPR
9046 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9048 /* Undo effects of boolean_increment. */
9049 if (integer_onep (TREE_OPERAND (lhs, 1)))
9051 /* This is pre or post increment. */
9052 rhs = TREE_OPERAND (lhs, 1);
9053 lhs = TREE_OPERAND (lhs, 0);
9054 code = NOP_EXPR;
9055 break;
9057 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9058 && TREE_OPERAND (lhs, 0)
9059 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9061 /* This is pre or post decrement. */
9062 rhs = TREE_OPERAND (lhs, 1);
9063 lhs = TREE_OPERAND (lhs, 0);
9064 code = NOP_EXPR;
9065 break;
9068 /* FALLTHRU */
9069 default:
9070 switch (c_parser_peek_token (parser)->type)
9072 case CPP_MULT_EQ:
9073 code = MULT_EXPR;
9074 break;
9075 case CPP_DIV_EQ:
9076 code = TRUNC_DIV_EXPR;
9077 break;
9078 case CPP_PLUS_EQ:
9079 code = PLUS_EXPR;
9080 break;
9081 case CPP_MINUS_EQ:
9082 code = MINUS_EXPR;
9083 break;
9084 case CPP_LSHIFT_EQ:
9085 code = LSHIFT_EXPR;
9086 break;
9087 case CPP_RSHIFT_EQ:
9088 code = RSHIFT_EXPR;
9089 break;
9090 case CPP_AND_EQ:
9091 code = BIT_AND_EXPR;
9092 break;
9093 case CPP_OR_EQ:
9094 code = BIT_IOR_EXPR;
9095 break;
9096 case CPP_XOR_EQ:
9097 code = BIT_XOR_EXPR;
9098 break;
9099 default:
9100 c_parser_error (parser,
9101 "invalid operator for %<#pragma omp atomic%>");
9102 goto saw_error;
9105 c_parser_consume_token (parser);
9107 location_t rhs_loc = c_parser_peek_token (parser)->location;
9108 rhs_expr = c_parser_expression (parser);
9109 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9111 rhs = rhs_expr.value;
9112 rhs = c_fully_fold (rhs, false, NULL);
9113 break;
9115 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
9116 if (stmt != error_mark_node)
9117 add_stmt (stmt);
9118 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9122 /* OpenMP 2.5:
9123 # pragma omp barrier new-line
9126 static void
9127 c_parser_omp_barrier (c_parser *parser)
9129 location_t loc = c_parser_peek_token (parser)->location;
9130 c_parser_consume_pragma (parser);
9131 c_parser_skip_to_pragma_eol (parser);
9133 c_finish_omp_barrier (loc);
9136 /* OpenMP 2.5:
9137 # pragma omp critical [(name)] new-line
9138 structured-block
9140 LOC is the location of the #pragma itself. */
9142 static tree
9143 c_parser_omp_critical (location_t loc, c_parser *parser)
9145 tree stmt, name = NULL;
9147 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9149 c_parser_consume_token (parser);
9150 if (c_parser_next_token_is (parser, CPP_NAME))
9152 name = c_parser_peek_token (parser)->value;
9153 c_parser_consume_token (parser);
9154 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9156 else
9157 c_parser_error (parser, "expected identifier");
9159 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9160 c_parser_error (parser, "expected %<(%> or end of line");
9161 c_parser_skip_to_pragma_eol (parser);
9163 stmt = c_parser_omp_structured_block (parser);
9164 return c_finish_omp_critical (loc, stmt, name);
9167 /* OpenMP 2.5:
9168 # pragma omp flush flush-vars[opt] new-line
9170 flush-vars:
9171 ( variable-list ) */
9173 static void
9174 c_parser_omp_flush (c_parser *parser)
9176 location_t loc = c_parser_peek_token (parser)->location;
9177 c_parser_consume_pragma (parser);
9178 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9179 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9180 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9181 c_parser_error (parser, "expected %<(%> or end of line");
9182 c_parser_skip_to_pragma_eol (parser);
9184 c_finish_omp_flush (loc);
9187 /* Parse the restricted form of the for statement allowed by OpenMP.
9188 The real trick here is to determine the loop control variable early
9189 so that we can push a new decl if necessary to make it private.
9190 LOC is the location of the OMP in "#pragma omp". */
9192 static tree
9193 c_parser_omp_for_loop (location_t loc,
9194 c_parser *parser, tree clauses, tree *par_clauses)
9196 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9197 tree declv, condv, incrv, initv, ret = NULL;
9198 bool fail = false, open_brace_parsed = false;
9199 int i, collapse = 1, nbraces = 0;
9200 location_t for_loc;
9201 VEC(tree,gc) *for_block = make_tree_vector ();
9203 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9204 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9205 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9207 gcc_assert (collapse >= 1);
9209 declv = make_tree_vec (collapse);
9210 initv = make_tree_vec (collapse);
9211 condv = make_tree_vec (collapse);
9212 incrv = make_tree_vec (collapse);
9214 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9216 c_parser_error (parser, "for statement expected");
9217 return NULL;
9219 for_loc = c_parser_peek_token (parser)->location;
9220 c_parser_consume_token (parser);
9222 for (i = 0; i < collapse; i++)
9224 int bracecount = 0;
9226 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9227 goto pop_scopes;
9229 /* Parse the initialization declaration or expression. */
9230 if (c_parser_next_tokens_start_declaration (parser))
9232 if (i > 0)
9233 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9234 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9235 decl = check_for_loop_decls (for_loc, flag_isoc99);
9236 if (decl == NULL)
9237 goto error_init;
9238 if (DECL_INITIAL (decl) == error_mark_node)
9239 decl = error_mark_node;
9240 init = decl;
9242 else if (c_parser_next_token_is (parser, CPP_NAME)
9243 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9245 struct c_expr decl_exp;
9246 struct c_expr init_exp;
9247 location_t init_loc;
9249 decl_exp = c_parser_postfix_expression (parser);
9250 decl = decl_exp.value;
9252 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9254 init_loc = c_parser_peek_token (parser)->location;
9255 init_exp = c_parser_expr_no_commas (parser, NULL);
9256 init_exp = default_function_array_read_conversion (init_loc,
9257 init_exp);
9258 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9259 NOP_EXPR, init_loc, init_exp.value,
9260 init_exp.original_type);
9261 init = c_process_expr_stmt (init_loc, init);
9263 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9265 else
9267 error_init:
9268 c_parser_error (parser,
9269 "expected iteration declaration or initialization");
9270 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9271 "expected %<)%>");
9272 fail = true;
9273 goto parse_next;
9276 /* Parse the loop condition. */
9277 cond = NULL_TREE;
9278 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9280 location_t cond_loc = c_parser_peek_token (parser)->location;
9281 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9283 cond = cond_expr.value;
9284 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9285 cond = c_fully_fold (cond, false, NULL);
9286 switch (cond_expr.original_code)
9288 case GT_EXPR:
9289 case GE_EXPR:
9290 case LT_EXPR:
9291 case LE_EXPR:
9292 break;
9293 default:
9294 /* Can't be cond = error_mark_node, because we want to preserve
9295 the location until c_finish_omp_for. */
9296 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9297 break;
9299 protected_set_expr_location (cond, cond_loc);
9301 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9303 /* Parse the increment expression. */
9304 incr = NULL_TREE;
9305 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9307 location_t incr_loc = c_parser_peek_token (parser)->location;
9309 incr = c_process_expr_stmt (incr_loc,
9310 c_parser_expression (parser).value);
9312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9314 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9315 fail = true;
9316 else
9318 TREE_VEC_ELT (declv, i) = decl;
9319 TREE_VEC_ELT (initv, i) = init;
9320 TREE_VEC_ELT (condv, i) = cond;
9321 TREE_VEC_ELT (incrv, i) = incr;
9324 parse_next:
9325 if (i == collapse - 1)
9326 break;
9328 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9329 in between the collapsed for loops to be still considered perfectly
9330 nested. Hopefully the final version clarifies this.
9331 For now handle (multiple) {'s and empty statements. */
9334 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9336 c_parser_consume_token (parser);
9337 break;
9339 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9341 c_parser_consume_token (parser);
9342 bracecount++;
9344 else if (bracecount
9345 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9346 c_parser_consume_token (parser);
9347 else
9349 c_parser_error (parser, "not enough perfectly nested loops");
9350 if (bracecount)
9352 open_brace_parsed = true;
9353 bracecount--;
9355 fail = true;
9356 collapse = 0;
9357 break;
9360 while (1);
9362 nbraces += bracecount;
9365 save_break = c_break_label;
9366 c_break_label = size_one_node;
9367 save_cont = c_cont_label;
9368 c_cont_label = NULL_TREE;
9369 body = push_stmt_list ();
9371 if (open_brace_parsed)
9373 location_t here = c_parser_peek_token (parser)->location;
9374 stmt = c_begin_compound_stmt (true);
9375 c_parser_compound_statement_nostart (parser);
9376 add_stmt (c_end_compound_stmt (here, stmt, true));
9378 else
9379 add_stmt (c_parser_c99_block_statement (parser));
9380 if (c_cont_label)
9382 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9383 SET_EXPR_LOCATION (t, loc);
9384 add_stmt (t);
9387 body = pop_stmt_list (body);
9388 c_break_label = save_break;
9389 c_cont_label = save_cont;
9391 while (nbraces)
9393 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9395 c_parser_consume_token (parser);
9396 nbraces--;
9398 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9399 c_parser_consume_token (parser);
9400 else
9402 c_parser_error (parser, "collapsed loops not perfectly nested");
9403 while (nbraces)
9405 location_t here = c_parser_peek_token (parser)->location;
9406 stmt = c_begin_compound_stmt (true);
9407 add_stmt (body);
9408 c_parser_compound_statement_nostart (parser);
9409 body = c_end_compound_stmt (here, stmt, true);
9410 nbraces--;
9412 goto pop_scopes;
9416 /* Only bother calling c_finish_omp_for if we haven't already generated
9417 an error from the initialization parsing. */
9418 if (!fail)
9420 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9421 if (stmt)
9423 if (par_clauses != NULL)
9425 tree *c;
9426 for (c = par_clauses; *c ; )
9427 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9428 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9429 c = &OMP_CLAUSE_CHAIN (*c);
9430 else
9432 for (i = 0; i < collapse; i++)
9433 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9434 break;
9435 if (i == collapse)
9436 c = &OMP_CLAUSE_CHAIN (*c);
9437 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9439 error_at (loc,
9440 "iteration variable %qD should not be firstprivate",
9441 OMP_CLAUSE_DECL (*c));
9442 *c = OMP_CLAUSE_CHAIN (*c);
9444 else
9446 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9447 change it to shared (decl) in
9448 OMP_PARALLEL_CLAUSES. */
9449 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9450 OMP_CLAUSE_LASTPRIVATE);
9451 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9452 OMP_CLAUSE_CHAIN (l) = clauses;
9453 clauses = l;
9454 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9458 OMP_FOR_CLAUSES (stmt) = clauses;
9460 ret = stmt;
9462 pop_scopes:
9463 while (!VEC_empty (tree, for_block))
9465 /* FIXME diagnostics: LOC below should be the actual location of
9466 this particular for block. We need to build a list of
9467 locations to go along with FOR_BLOCK. */
9468 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9469 add_stmt (stmt);
9471 release_tree_vector (for_block);
9472 return ret;
9475 /* OpenMP 2.5:
9476 #pragma omp for for-clause[optseq] new-line
9477 for-loop
9479 LOC is the location of the #pragma token.
9482 #define OMP_FOR_CLAUSE_MASK \
9483 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9484 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9485 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9486 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9487 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9488 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9489 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9490 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9492 static tree
9493 c_parser_omp_for (location_t loc, c_parser *parser)
9495 tree block, clauses, ret;
9497 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9498 "#pragma omp for");
9500 block = c_begin_compound_stmt (true);
9501 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9502 block = c_end_compound_stmt (loc, block, true);
9503 add_stmt (block);
9505 return ret;
9508 /* OpenMP 2.5:
9509 # pragma omp master new-line
9510 structured-block
9512 LOC is the location of the #pragma token.
9515 static tree
9516 c_parser_omp_master (location_t loc, c_parser *parser)
9518 c_parser_skip_to_pragma_eol (parser);
9519 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9522 /* OpenMP 2.5:
9523 # pragma omp ordered new-line
9524 structured-block
9526 LOC is the location of the #pragma itself.
9529 static tree
9530 c_parser_omp_ordered (location_t loc, c_parser *parser)
9532 c_parser_skip_to_pragma_eol (parser);
9533 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9536 /* OpenMP 2.5:
9538 section-scope:
9539 { section-sequence }
9541 section-sequence:
9542 section-directive[opt] structured-block
9543 section-sequence section-directive structured-block
9545 SECTIONS_LOC is the location of the #pragma omp sections. */
9547 static tree
9548 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9550 tree stmt, substmt;
9551 bool error_suppress = false;
9552 location_t loc;
9554 loc = c_parser_peek_token (parser)->location;
9555 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9557 /* Avoid skipping until the end of the block. */
9558 parser->error = false;
9559 return NULL_TREE;
9562 stmt = push_stmt_list ();
9564 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9566 substmt = push_stmt_list ();
9568 while (1)
9570 c_parser_statement (parser);
9572 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9573 break;
9574 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9575 break;
9576 if (c_parser_next_token_is (parser, CPP_EOF))
9577 break;
9580 substmt = pop_stmt_list (substmt);
9581 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9582 SET_EXPR_LOCATION (substmt, loc);
9583 add_stmt (substmt);
9586 while (1)
9588 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9589 break;
9590 if (c_parser_next_token_is (parser, CPP_EOF))
9591 break;
9593 loc = c_parser_peek_token (parser)->location;
9594 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9596 c_parser_consume_pragma (parser);
9597 c_parser_skip_to_pragma_eol (parser);
9598 error_suppress = false;
9600 else if (!error_suppress)
9602 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9603 error_suppress = true;
9606 substmt = c_parser_omp_structured_block (parser);
9607 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9608 SET_EXPR_LOCATION (substmt, loc);
9609 add_stmt (substmt);
9611 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9612 "expected %<#pragma omp section%> or %<}%>");
9614 substmt = pop_stmt_list (stmt);
9616 stmt = make_node (OMP_SECTIONS);
9617 SET_EXPR_LOCATION (stmt, sections_loc);
9618 TREE_TYPE (stmt) = void_type_node;
9619 OMP_SECTIONS_BODY (stmt) = substmt;
9621 return add_stmt (stmt);
9624 /* OpenMP 2.5:
9625 # pragma omp sections sections-clause[optseq] newline
9626 sections-scope
9628 LOC is the location of the #pragma token.
9631 #define OMP_SECTIONS_CLAUSE_MASK \
9632 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9633 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9634 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9635 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9636 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9638 static tree
9639 c_parser_omp_sections (location_t loc, c_parser *parser)
9641 tree block, clauses, ret;
9643 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9644 "#pragma omp sections");
9646 block = c_begin_compound_stmt (true);
9647 ret = c_parser_omp_sections_scope (loc, parser);
9648 if (ret)
9649 OMP_SECTIONS_CLAUSES (ret) = clauses;
9650 block = c_end_compound_stmt (loc, block, true);
9651 add_stmt (block);
9653 return ret;
9656 /* OpenMP 2.5:
9657 # pragma parallel parallel-clause new-line
9658 # pragma parallel for parallel-for-clause new-line
9659 # pragma parallel sections parallel-sections-clause new-line
9661 LOC is the location of the #pragma token.
9664 #define OMP_PARALLEL_CLAUSE_MASK \
9665 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9666 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9667 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9668 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9669 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9670 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9671 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9672 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9674 static tree
9675 c_parser_omp_parallel (location_t loc, c_parser *parser)
9677 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9678 const char *p_name = "#pragma omp parallel";
9679 tree stmt, clauses, par_clause, ws_clause, block;
9680 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9682 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9684 c_parser_consume_token (parser);
9685 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9686 p_name = "#pragma omp parallel for";
9687 mask |= OMP_FOR_CLAUSE_MASK;
9688 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9690 else if (c_parser_next_token_is (parser, CPP_NAME))
9692 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9693 if (strcmp (p, "sections") == 0)
9695 c_parser_consume_token (parser);
9696 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9697 p_name = "#pragma omp parallel sections";
9698 mask |= OMP_SECTIONS_CLAUSE_MASK;
9699 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9703 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9705 switch (p_kind)
9707 case PRAGMA_OMP_PARALLEL:
9708 block = c_begin_omp_parallel ();
9709 c_parser_statement (parser);
9710 stmt = c_finish_omp_parallel (loc, clauses, block);
9711 break;
9713 case PRAGMA_OMP_PARALLEL_FOR:
9714 block = c_begin_omp_parallel ();
9715 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9716 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9717 stmt = c_finish_omp_parallel (loc, par_clause, block);
9718 OMP_PARALLEL_COMBINED (stmt) = 1;
9719 break;
9721 case PRAGMA_OMP_PARALLEL_SECTIONS:
9722 block = c_begin_omp_parallel ();
9723 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9724 stmt = c_parser_omp_sections_scope (loc, parser);
9725 if (stmt)
9726 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9727 stmt = c_finish_omp_parallel (loc, par_clause, block);
9728 OMP_PARALLEL_COMBINED (stmt) = 1;
9729 break;
9731 default:
9732 gcc_unreachable ();
9735 return stmt;
9738 /* OpenMP 2.5:
9739 # pragma omp single single-clause[optseq] new-line
9740 structured-block
9742 LOC is the location of the #pragma.
9745 #define OMP_SINGLE_CLAUSE_MASK \
9746 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9747 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9748 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9749 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9751 static tree
9752 c_parser_omp_single (location_t loc, c_parser *parser)
9754 tree stmt = make_node (OMP_SINGLE);
9755 SET_EXPR_LOCATION (stmt, loc);
9756 TREE_TYPE (stmt) = void_type_node;
9758 OMP_SINGLE_CLAUSES (stmt)
9759 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9760 "#pragma omp single");
9761 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9763 return add_stmt (stmt);
9766 /* OpenMP 3.0:
9767 # pragma omp task task-clause[optseq] new-line
9769 LOC is the location of the #pragma.
9772 #define OMP_TASK_CLAUSE_MASK \
9773 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9774 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9775 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9776 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9777 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9778 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9780 static tree
9781 c_parser_omp_task (location_t loc, c_parser *parser)
9783 tree clauses, block;
9785 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9786 "#pragma omp task");
9788 block = c_begin_omp_task ();
9789 c_parser_statement (parser);
9790 return c_finish_omp_task (loc, clauses, block);
9793 /* OpenMP 3.0:
9794 # pragma omp taskwait new-line
9797 static void
9798 c_parser_omp_taskwait (c_parser *parser)
9800 location_t loc = c_parser_peek_token (parser)->location;
9801 c_parser_consume_pragma (parser);
9802 c_parser_skip_to_pragma_eol (parser);
9804 c_finish_omp_taskwait (loc);
9807 /* Main entry point to parsing most OpenMP pragmas. */
9809 static void
9810 c_parser_omp_construct (c_parser *parser)
9812 enum pragma_kind p_kind;
9813 location_t loc;
9814 tree stmt;
9816 loc = c_parser_peek_token (parser)->location;
9817 p_kind = c_parser_peek_token (parser)->pragma_kind;
9818 c_parser_consume_pragma (parser);
9820 switch (p_kind)
9822 case PRAGMA_OMP_ATOMIC:
9823 c_parser_omp_atomic (loc, parser);
9824 return;
9825 case PRAGMA_OMP_CRITICAL:
9826 stmt = c_parser_omp_critical (loc, parser);
9827 break;
9828 case PRAGMA_OMP_FOR:
9829 stmt = c_parser_omp_for (loc, parser);
9830 break;
9831 case PRAGMA_OMP_MASTER:
9832 stmt = c_parser_omp_master (loc, parser);
9833 break;
9834 case PRAGMA_OMP_ORDERED:
9835 stmt = c_parser_omp_ordered (loc, parser);
9836 break;
9837 case PRAGMA_OMP_PARALLEL:
9838 stmt = c_parser_omp_parallel (loc, parser);
9839 break;
9840 case PRAGMA_OMP_SECTIONS:
9841 stmt = c_parser_omp_sections (loc, parser);
9842 break;
9843 case PRAGMA_OMP_SINGLE:
9844 stmt = c_parser_omp_single (loc, parser);
9845 break;
9846 case PRAGMA_OMP_TASK:
9847 stmt = c_parser_omp_task (loc, parser);
9848 break;
9849 default:
9850 gcc_unreachable ();
9853 if (stmt)
9854 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9858 /* OpenMP 2.5:
9859 # pragma omp threadprivate (variable-list) */
9861 static void
9862 c_parser_omp_threadprivate (c_parser *parser)
9864 tree vars, t;
9865 location_t loc;
9867 c_parser_consume_pragma (parser);
9868 loc = c_parser_peek_token (parser)->location;
9869 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9871 /* Mark every variable in VARS to be assigned thread local storage. */
9872 for (t = vars; t; t = TREE_CHAIN (t))
9874 tree v = TREE_PURPOSE (t);
9876 /* FIXME diagnostics: Ideally we should keep individual
9877 locations for all the variables in the var list to make the
9878 following errors more precise. Perhaps
9879 c_parser_omp_var_list_parens() should construct a list of
9880 locations to go along with the var list. */
9882 /* If V had already been marked threadprivate, it doesn't matter
9883 whether it had been used prior to this point. */
9884 if (TREE_CODE (v) != VAR_DECL)
9885 error_at (loc, "%qD is not a variable", v);
9886 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9887 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9888 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9889 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9890 else if (TREE_TYPE (v) == error_mark_node)
9892 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9893 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9894 else
9896 if (! DECL_THREAD_LOCAL_P (v))
9898 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9899 /* If rtl has been already set for this var, call
9900 make_decl_rtl once again, so that encode_section_info
9901 has a chance to look at the new decl flags. */
9902 if (DECL_RTL_SET_P (v))
9903 make_decl_rtl (v);
9905 C_DECL_THREADPRIVATE_P (v) = 1;
9909 c_parser_skip_to_pragma_eol (parser);
9913 /* Parse a single source file. */
9915 void
9916 c_parse_file (void)
9918 /* Use local storage to begin. If the first token is a pragma, parse it.
9919 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9920 which will cause garbage collection. */
9921 c_parser tparser;
9923 memset (&tparser, 0, sizeof tparser);
9924 the_parser = &tparser;
9926 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9927 c_parser_pragma_pch_preprocess (&tparser);
9929 the_parser = ggc_alloc_c_parser ();
9930 *the_parser = tparser;
9932 /* Initialize EH, if we've been told to do so. */
9933 if (flag_exceptions)
9934 using_eh_for_cleanups ();
9936 c_parser_translation_unit (the_parser);
9937 the_parser = NULL;
9940 #include "gt-c-parser.h"