2011-07-21 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / gcc / c-parser.c
blob65966a9b50ff0108fe7689f98a53f1ab3f3dccc2
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, 2011
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. */
257 if (parser->objc_pq_context)
259 token->type = CPP_KEYWORD;
260 token->keyword = rid_code;
261 break;
263 else if (parser->objc_could_be_foreach_context
264 && rid_code == RID_IN)
266 /* We are in Objective-C, inside a (potential)
267 foreach context (which means after having
268 parsed 'for (', but before having parsed ';'),
269 and we found 'in'. We consider it the keyword
270 which terminates the declaration at the
271 beginning of a foreach-statement. Note that
272 this means you can't use 'in' for anything else
273 in that context; in particular, in Objective-C
274 you can't use 'in' as the name of the running
275 variable in a C for loop. We could potentially
276 try to add code here to disambiguate, but it
277 seems a reasonable limitation. */
278 token->type = CPP_KEYWORD;
279 token->keyword = rid_code;
280 break;
282 /* Else, "pq" keywords outside of the "pq" context are
283 not keywords, and we fall through to the code for
284 normal tokens. */
286 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
288 /* We found an Objective-C "property attribute"
289 keyword (getter, setter, readonly, etc). These are
290 only valid in the property context. */
291 if (parser->objc_property_attr_context)
293 token->type = CPP_KEYWORD;
294 token->keyword = rid_code;
295 break;
297 /* Else they are not special keywords.
300 else if (c_dialect_objc ()
301 && (OBJC_IS_AT_KEYWORD (rid_code)
302 || OBJC_IS_CXX_KEYWORD (rid_code)))
304 /* We found one of the Objective-C "@" keywords (defs,
305 selector, synchronized, etc) or one of the
306 Objective-C "cxx" keywords (class, private,
307 protected, public, try, catch, throw) without a
308 preceding '@' sign. Do nothing and fall through to
309 the code for normal tokens (in C++ we would still
310 consider the CXX ones keywords, but not in C). */
313 else
315 token->type = CPP_KEYWORD;
316 token->keyword = rid_code;
317 break;
321 decl = lookup_name (token->value);
322 if (decl)
324 if (TREE_CODE (decl) == TYPE_DECL)
326 token->id_kind = C_ID_TYPENAME;
327 break;
330 else if (c_dialect_objc ())
332 tree objc_interface_decl = objc_is_class_name (token->value);
333 /* Objective-C class names are in the same namespace as
334 variables and typedefs, and hence are shadowed by local
335 declarations. */
336 if (objc_interface_decl
337 && (!objc_force_identifier || global_bindings_p ()))
339 token->value = objc_interface_decl;
340 token->id_kind = C_ID_CLASSNAME;
341 break;
344 token->id_kind = C_ID_ID;
346 break;
347 case CPP_AT_NAME:
348 /* This only happens in Objective-C; it must be a keyword. */
349 token->type = CPP_KEYWORD;
350 switch (C_RID_CODE (token->value))
352 /* Replace 'class' with '@class', 'private' with '@private',
353 etc. This prevents confusion with the C++ keyword
354 'class', and makes the tokens consistent with other
355 Objective-C 'AT' keywords. For example '@class' is
356 reported as RID_AT_CLASS which is consistent with
357 '@synchronized', which is reported as
358 RID_AT_SYNCHRONIZED.
360 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
361 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
362 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
363 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
364 case RID_THROW: token->keyword = RID_AT_THROW; break;
365 case RID_TRY: token->keyword = RID_AT_TRY; break;
366 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
367 default: token->keyword = C_RID_CODE (token->value);
369 break;
370 case CPP_COLON:
371 case CPP_COMMA:
372 case CPP_CLOSE_PAREN:
373 case CPP_SEMICOLON:
374 /* These tokens may affect the interpretation of any identifiers
375 following, if doing Objective-C. */
376 if (c_dialect_objc ())
377 parser->objc_need_raw_identifier = false;
378 break;
379 case CPP_PRAGMA:
380 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
381 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
382 token->value = NULL;
383 break;
384 default:
385 break;
387 timevar_pop (TV_LEX);
390 /* Return a pointer to the next token from PARSER, reading it in if
391 necessary. */
393 static inline c_token *
394 c_parser_peek_token (c_parser *parser)
396 if (parser->tokens_avail == 0)
398 c_lex_one_token (parser, &parser->tokens[0]);
399 parser->tokens_avail = 1;
401 return &parser->tokens[0];
404 /* Return true if the next token from PARSER has the indicated
405 TYPE. */
407 static inline bool
408 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
410 return c_parser_peek_token (parser)->type == type;
413 /* Return true if the next token from PARSER does not have the
414 indicated TYPE. */
416 static inline bool
417 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
419 return !c_parser_next_token_is (parser, type);
422 /* Return true if the next token from PARSER is the indicated
423 KEYWORD. */
425 static inline bool
426 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
428 return c_parser_peek_token (parser)->keyword == keyword;
431 /* Return a pointer to the next-but-one token from PARSER, reading it
432 in if necessary. The next token is already read in. */
434 static c_token *
435 c_parser_peek_2nd_token (c_parser *parser)
437 if (parser->tokens_avail >= 2)
438 return &parser->tokens[1];
439 gcc_assert (parser->tokens_avail == 1);
440 gcc_assert (parser->tokens[0].type != CPP_EOF);
441 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
442 c_lex_one_token (parser, &parser->tokens[1]);
443 parser->tokens_avail = 2;
444 return &parser->tokens[1];
447 /* Return true if TOKEN can start a type name,
448 false otherwise. */
449 static bool
450 c_token_starts_typename (c_token *token)
452 switch (token->type)
454 case CPP_NAME:
455 switch (token->id_kind)
457 case C_ID_ID:
458 return false;
459 case C_ID_ADDRSPACE:
460 return true;
461 case C_ID_TYPENAME:
462 return true;
463 case C_ID_CLASSNAME:
464 gcc_assert (c_dialect_objc ());
465 return true;
466 default:
467 gcc_unreachable ();
469 case CPP_KEYWORD:
470 switch (token->keyword)
472 case RID_UNSIGNED:
473 case RID_LONG:
474 case RID_INT128:
475 case RID_SHORT:
476 case RID_SIGNED:
477 case RID_COMPLEX:
478 case RID_INT:
479 case RID_CHAR:
480 case RID_FLOAT:
481 case RID_DOUBLE:
482 case RID_VOID:
483 case RID_DFLOAT32:
484 case RID_DFLOAT64:
485 case RID_DFLOAT128:
486 case RID_BOOL:
487 case RID_ENUM:
488 case RID_STRUCT:
489 case RID_UNION:
490 case RID_TYPEOF:
491 case RID_CONST:
492 case RID_VOLATILE:
493 case RID_RESTRICT:
494 case RID_ATTRIBUTE:
495 case RID_FRACT:
496 case RID_ACCUM:
497 case RID_SAT:
498 return true;
499 default:
500 return false;
502 case CPP_LESS:
503 if (c_dialect_objc ())
504 return true;
505 return false;
506 default:
507 return false;
511 enum c_lookahead_kind {
512 /* Always treat unknown identifiers as typenames. */
513 cla_prefer_type,
515 /* Could be parsing a nonabstract declarator. Only treat an identifier
516 as a typename if followed by another identifier or a star. */
517 cla_nonabstract_decl,
519 /* Never treat identifiers as typenames. */
520 cla_prefer_id
523 /* Return true if the next token from PARSER can start a type name,
524 false otherwise. LA specifies how to do lookahead in order to
525 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
527 static inline bool
528 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
530 c_token *token = c_parser_peek_token (parser);
531 if (c_token_starts_typename (token))
532 return true;
534 /* Try a bit harder to detect an unknown typename. */
535 if (la != cla_prefer_id
536 && token->type == CPP_NAME
537 && token->id_kind == C_ID_ID
539 /* Do not try too hard when we could have "object in array". */
540 && !parser->objc_could_be_foreach_context
542 && (la == cla_prefer_type
543 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
544 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
546 /* Only unknown identifiers. */
547 && !lookup_name (token->value))
548 return true;
550 return false;
553 /* Return true if TOKEN is a type qualifier, false otherwise. */
554 static bool
555 c_token_is_qualifier (c_token *token)
557 switch (token->type)
559 case CPP_NAME:
560 switch (token->id_kind)
562 case C_ID_ADDRSPACE:
563 return true;
564 default:
565 return false;
567 case CPP_KEYWORD:
568 switch (token->keyword)
570 case RID_CONST:
571 case RID_VOLATILE:
572 case RID_RESTRICT:
573 case RID_ATTRIBUTE:
574 return true;
575 default:
576 return false;
578 case CPP_LESS:
579 return false;
580 default:
581 gcc_unreachable ();
585 /* Return true if the next token from PARSER is a type qualifier,
586 false otherwise. */
587 static inline bool
588 c_parser_next_token_is_qualifier (c_parser *parser)
590 c_token *token = c_parser_peek_token (parser);
591 return c_token_is_qualifier (token);
594 /* Return true if TOKEN can start declaration specifiers, false
595 otherwise. */
596 static bool
597 c_token_starts_declspecs (c_token *token)
599 switch (token->type)
601 case CPP_NAME:
602 switch (token->id_kind)
604 case C_ID_ID:
605 return false;
606 case C_ID_ADDRSPACE:
607 return true;
608 case C_ID_TYPENAME:
609 return true;
610 case C_ID_CLASSNAME:
611 gcc_assert (c_dialect_objc ());
612 return true;
613 default:
614 gcc_unreachable ();
616 case CPP_KEYWORD:
617 switch (token->keyword)
619 case RID_STATIC:
620 case RID_EXTERN:
621 case RID_REGISTER:
622 case RID_TYPEDEF:
623 case RID_INLINE:
624 case RID_AUTO:
625 case RID_THREAD:
626 case RID_UNSIGNED:
627 case RID_LONG:
628 case RID_INT128:
629 case RID_SHORT:
630 case RID_SIGNED:
631 case RID_COMPLEX:
632 case RID_INT:
633 case RID_CHAR:
634 case RID_FLOAT:
635 case RID_DOUBLE:
636 case RID_VOID:
637 case RID_DFLOAT32:
638 case RID_DFLOAT64:
639 case RID_DFLOAT128:
640 case RID_BOOL:
641 case RID_ENUM:
642 case RID_STRUCT:
643 case RID_UNION:
644 case RID_TYPEOF:
645 case RID_CONST:
646 case RID_VOLATILE:
647 case RID_RESTRICT:
648 case RID_ATTRIBUTE:
649 case RID_FRACT:
650 case RID_ACCUM:
651 case RID_SAT:
652 return true;
653 default:
654 return false;
656 case CPP_LESS:
657 if (c_dialect_objc ())
658 return true;
659 return false;
660 default:
661 return false;
666 /* Return true if TOKEN can start declaration specifiers or a static
667 assertion, false otherwise. */
668 static bool
669 c_token_starts_declaration (c_token *token)
671 if (c_token_starts_declspecs (token)
672 || token->keyword == RID_STATIC_ASSERT)
673 return true;
674 else
675 return false;
678 /* Return true if the next token from PARSER can start declaration
679 specifiers, false otherwise. */
680 static inline bool
681 c_parser_next_token_starts_declspecs (c_parser *parser)
683 c_token *token = c_parser_peek_token (parser);
685 /* In Objective-C, a classname normally starts a declspecs unless it
686 is immediately followed by a dot. In that case, it is the
687 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
688 setter/getter on the class. c_token_starts_declspecs() can't
689 differentiate between the two cases because it only checks the
690 current token, so we have a special check here. */
691 if (c_dialect_objc ()
692 && token->type == CPP_NAME
693 && token->id_kind == C_ID_CLASSNAME
694 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
695 return false;
697 return c_token_starts_declspecs (token);
700 /* Return true if the next tokens from PARSER can start declaration
701 specifiers or a static assertion, false otherwise. */
702 static inline bool
703 c_parser_next_tokens_start_declaration (c_parser *parser)
705 c_token *token = c_parser_peek_token (parser);
707 /* Same as above. */
708 if (c_dialect_objc ()
709 && token->type == CPP_NAME
710 && token->id_kind == C_ID_CLASSNAME
711 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
712 return false;
714 /* Labels do not start declarations. */
715 if (token->type == CPP_NAME
716 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
717 return false;
719 if (c_token_starts_declaration (token))
720 return true;
722 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
723 return true;
725 return false;
728 /* Consume the next token from PARSER. */
730 static void
731 c_parser_consume_token (c_parser *parser)
733 gcc_assert (parser->tokens_avail >= 1);
734 gcc_assert (parser->tokens[0].type != CPP_EOF);
735 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
736 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
737 if (parser->tokens_avail == 2)
738 parser->tokens[0] = parser->tokens[1];
739 parser->tokens_avail--;
742 /* Expect the current token to be a #pragma. Consume it and remember
743 that we've begun parsing a pragma. */
745 static void
746 c_parser_consume_pragma (c_parser *parser)
748 gcc_assert (!parser->in_pragma);
749 gcc_assert (parser->tokens_avail >= 1);
750 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
751 if (parser->tokens_avail == 2)
752 parser->tokens[0] = parser->tokens[1];
753 parser->tokens_avail--;
754 parser->in_pragma = true;
757 /* Update the globals input_location and in_system_header from
758 TOKEN. */
759 static inline void
760 c_parser_set_source_position_from_token (c_token *token)
762 if (token->type != CPP_EOF)
764 input_location = token->location;
768 /* Issue a diagnostic of the form
769 FILE:LINE: MESSAGE before TOKEN
770 where TOKEN is the next token in the input stream of PARSER.
771 MESSAGE (specified by the caller) is usually of the form "expected
772 OTHER-TOKEN".
774 Do not issue a diagnostic if still recovering from an error.
776 ??? This is taken from the C++ parser, but building up messages in
777 this way is not i18n-friendly and some other approach should be
778 used. */
780 static void
781 c_parser_error (c_parser *parser, const char *gmsgid)
783 c_token *token = c_parser_peek_token (parser);
784 if (parser->error)
785 return;
786 parser->error = true;
787 if (!gmsgid)
788 return;
789 /* This diagnostic makes more sense if it is tagged to the line of
790 the token we just peeked at. */
791 c_parser_set_source_position_from_token (token);
792 c_parse_error (gmsgid,
793 /* Because c_parse_error does not understand
794 CPP_KEYWORD, keywords are treated like
795 identifiers. */
796 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
797 /* ??? The C parser does not save the cpp flags of a
798 token, we need to pass 0 here and we will not get
799 the source spelling of some tokens but rather the
800 canonical spelling. */
801 token->value, /*flags=*/0);
804 /* If the next token is of the indicated TYPE, consume it. Otherwise,
805 issue the error MSGID. If MSGID is NULL then a message has already
806 been produced and no message will be produced this time. Returns
807 true if found, false otherwise. */
809 static bool
810 c_parser_require (c_parser *parser,
811 enum cpp_ttype type,
812 const char *msgid)
814 if (c_parser_next_token_is (parser, type))
816 c_parser_consume_token (parser);
817 return true;
819 else
821 c_parser_error (parser, msgid);
822 return false;
826 /* If the next token is the indicated keyword, consume it. Otherwise,
827 issue the error MSGID. Returns true if found, false otherwise. */
829 static bool
830 c_parser_require_keyword (c_parser *parser,
831 enum rid keyword,
832 const char *msgid)
834 if (c_parser_next_token_is_keyword (parser, keyword))
836 c_parser_consume_token (parser);
837 return true;
839 else
841 c_parser_error (parser, msgid);
842 return false;
846 /* Like c_parser_require, except that tokens will be skipped until the
847 desired token is found. An error message is still produced if the
848 next token is not as expected. If MSGID is NULL then a message has
849 already been produced and no message will be produced this
850 time. */
852 static void
853 c_parser_skip_until_found (c_parser *parser,
854 enum cpp_ttype type,
855 const char *msgid)
857 unsigned nesting_depth = 0;
859 if (c_parser_require (parser, type, msgid))
860 return;
862 /* Skip tokens until the desired token is found. */
863 while (true)
865 /* Peek at the next token. */
866 c_token *token = c_parser_peek_token (parser);
867 /* If we've reached the token we want, consume it and stop. */
868 if (token->type == type && !nesting_depth)
870 c_parser_consume_token (parser);
871 break;
874 /* If we've run out of tokens, stop. */
875 if (token->type == CPP_EOF)
876 return;
877 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
878 return;
879 if (token->type == CPP_OPEN_BRACE
880 || token->type == CPP_OPEN_PAREN
881 || token->type == CPP_OPEN_SQUARE)
882 ++nesting_depth;
883 else if (token->type == CPP_CLOSE_BRACE
884 || token->type == CPP_CLOSE_PAREN
885 || token->type == CPP_CLOSE_SQUARE)
887 if (nesting_depth-- == 0)
888 break;
890 /* Consume this token. */
891 c_parser_consume_token (parser);
893 parser->error = false;
896 /* Skip tokens until the end of a parameter is found, but do not
897 consume the comma, semicolon or closing delimiter. */
899 static void
900 c_parser_skip_to_end_of_parameter (c_parser *parser)
902 unsigned nesting_depth = 0;
904 while (true)
906 c_token *token = c_parser_peek_token (parser);
907 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
908 && !nesting_depth)
909 break;
910 /* If we've run out of tokens, stop. */
911 if (token->type == CPP_EOF)
912 return;
913 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
914 return;
915 if (token->type == CPP_OPEN_BRACE
916 || token->type == CPP_OPEN_PAREN
917 || token->type == CPP_OPEN_SQUARE)
918 ++nesting_depth;
919 else if (token->type == CPP_CLOSE_BRACE
920 || token->type == CPP_CLOSE_PAREN
921 || token->type == CPP_CLOSE_SQUARE)
923 if (nesting_depth-- == 0)
924 break;
926 /* Consume this token. */
927 c_parser_consume_token (parser);
929 parser->error = false;
932 /* Expect to be at the end of the pragma directive and consume an
933 end of line marker. */
935 static void
936 c_parser_skip_to_pragma_eol (c_parser *parser)
938 gcc_assert (parser->in_pragma);
939 parser->in_pragma = false;
941 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
942 while (true)
944 c_token *token = c_parser_peek_token (parser);
945 if (token->type == CPP_EOF)
946 break;
947 if (token->type == CPP_PRAGMA_EOL)
949 c_parser_consume_token (parser);
950 break;
952 c_parser_consume_token (parser);
955 parser->error = false;
958 /* Skip tokens until we have consumed an entire block, or until we
959 have consumed a non-nested ';'. */
961 static void
962 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
964 unsigned nesting_depth = 0;
965 bool save_error = parser->error;
967 while (true)
969 c_token *token;
971 /* Peek at the next token. */
972 token = c_parser_peek_token (parser);
974 switch (token->type)
976 case CPP_EOF:
977 return;
979 case CPP_PRAGMA_EOL:
980 if (parser->in_pragma)
981 return;
982 break;
984 case CPP_SEMICOLON:
985 /* If the next token is a ';', we have reached the
986 end of the statement. */
987 if (!nesting_depth)
989 /* Consume the ';'. */
990 c_parser_consume_token (parser);
991 goto finished;
993 break;
995 case CPP_CLOSE_BRACE:
996 /* If the next token is a non-nested '}', then we have
997 reached the end of the current block. */
998 if (nesting_depth == 0 || --nesting_depth == 0)
1000 c_parser_consume_token (parser);
1001 goto finished;
1003 break;
1005 case CPP_OPEN_BRACE:
1006 /* If it the next token is a '{', then we are entering a new
1007 block. Consume the entire block. */
1008 ++nesting_depth;
1009 break;
1011 case CPP_PRAGMA:
1012 /* If we see a pragma, consume the whole thing at once. We
1013 have some safeguards against consuming pragmas willy-nilly.
1014 Normally, we'd expect to be here with parser->error set,
1015 which disables these safeguards. But it's possible to get
1016 here for secondary error recovery, after parser->error has
1017 been cleared. */
1018 c_parser_consume_pragma (parser);
1019 c_parser_skip_to_pragma_eol (parser);
1020 parser->error = save_error;
1021 continue;
1023 default:
1024 break;
1027 c_parser_consume_token (parser);
1030 finished:
1031 parser->error = false;
1034 /* CPP's options (initialized by c-opts.c). */
1035 extern cpp_options *cpp_opts;
1037 /* Save the warning flags which are controlled by __extension__. */
1039 static inline int
1040 disable_extension_diagnostics (void)
1042 int ret = (pedantic
1043 | (warn_pointer_arith << 1)
1044 | (warn_traditional << 2)
1045 | (flag_iso << 3)
1046 | (warn_long_long << 4)
1047 | (warn_cxx_compat << 5)
1048 | (warn_overlength_strings << 6));
1049 cpp_opts->cpp_pedantic = pedantic = 0;
1050 warn_pointer_arith = 0;
1051 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1052 flag_iso = 0;
1053 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1054 warn_cxx_compat = 0;
1055 warn_overlength_strings = 0;
1056 return ret;
1059 /* Restore the warning flags which are controlled by __extension__.
1060 FLAGS is the return value from disable_extension_diagnostics. */
1062 static inline void
1063 restore_extension_diagnostics (int flags)
1065 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1066 warn_pointer_arith = (flags >> 1) & 1;
1067 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1068 flag_iso = (flags >> 3) & 1;
1069 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1070 warn_cxx_compat = (flags >> 5) & 1;
1071 warn_overlength_strings = (flags >> 6) & 1;
1074 /* Possibly kinds of declarator to parse. */
1075 typedef enum c_dtr_syn {
1076 /* A normal declarator with an identifier. */
1077 C_DTR_NORMAL,
1078 /* An abstract declarator (maybe empty). */
1079 C_DTR_ABSTRACT,
1080 /* A parameter declarator: may be either, but after a type name does
1081 not redeclare a typedef name as an identifier if it can
1082 alternatively be interpreted as a typedef name; see DR#009,
1083 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1084 following DR#249. For example, given a typedef T, "int T" and
1085 "int *T" are valid parameter declarations redeclaring T, while
1086 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1087 abstract declarators rather than involving redundant parentheses;
1088 the same applies with attributes inside the parentheses before
1089 "T". */
1090 C_DTR_PARM
1091 } c_dtr_syn;
1093 static void c_parser_external_declaration (c_parser *);
1094 static void c_parser_asm_definition (c_parser *);
1095 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1096 bool, bool, tree *);
1097 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1098 static void c_parser_static_assert_declaration (c_parser *);
1099 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1100 bool, enum c_lookahead_kind);
1101 static struct c_typespec c_parser_enum_specifier (c_parser *);
1102 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1103 static tree c_parser_struct_declaration (c_parser *);
1104 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1105 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1106 bool *);
1107 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1108 c_dtr_syn, bool *);
1109 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1110 bool,
1111 struct c_declarator *);
1112 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1113 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1114 tree);
1115 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1116 static tree c_parser_simple_asm_expr (c_parser *);
1117 static tree c_parser_attributes (c_parser *);
1118 static struct c_type_name *c_parser_type_name (c_parser *);
1119 static struct c_expr c_parser_initializer (c_parser *);
1120 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1121 static void c_parser_initelt (c_parser *, struct obstack *);
1122 static void c_parser_initval (c_parser *, struct c_expr *,
1123 struct obstack *);
1124 static tree c_parser_compound_statement (c_parser *);
1125 static void c_parser_compound_statement_nostart (c_parser *);
1126 static void c_parser_label (c_parser *);
1127 static void c_parser_statement (c_parser *);
1128 static void c_parser_statement_after_labels (c_parser *);
1129 static void c_parser_if_statement (c_parser *);
1130 static void c_parser_switch_statement (c_parser *);
1131 static void c_parser_while_statement (c_parser *);
1132 static void c_parser_do_statement (c_parser *);
1133 static void c_parser_for_statement (c_parser *);
1134 static tree c_parser_asm_statement (c_parser *);
1135 static tree c_parser_asm_operands (c_parser *, bool);
1136 static tree c_parser_asm_goto_operands (c_parser *);
1137 static tree c_parser_asm_clobbers (c_parser *);
1138 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1139 static struct c_expr c_parser_conditional_expression (c_parser *,
1140 struct c_expr *);
1141 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1142 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1143 static struct c_expr c_parser_unary_expression (c_parser *);
1144 static struct c_expr c_parser_sizeof_expression (c_parser *);
1145 static struct c_expr c_parser_alignof_expression (c_parser *);
1146 static struct c_expr c_parser_postfix_expression (c_parser *);
1147 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1148 struct c_type_name *,
1149 location_t);
1150 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1151 location_t loc,
1152 struct c_expr);
1153 static struct c_expr c_parser_expression (c_parser *);
1154 static struct c_expr c_parser_expression_conv (c_parser *);
1155 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1156 VEC(tree,gc) **);
1157 static void c_parser_omp_construct (c_parser *);
1158 static void c_parser_omp_threadprivate (c_parser *);
1159 static void c_parser_omp_barrier (c_parser *);
1160 static void c_parser_omp_flush (c_parser *);
1161 static void c_parser_omp_taskwait (c_parser *);
1163 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1164 static bool c_parser_pragma (c_parser *, enum pragma_context);
1166 /* These Objective-C parser functions are only ever called when
1167 compiling Objective-C. */
1168 static void c_parser_objc_class_definition (c_parser *, tree);
1169 static void c_parser_objc_class_instance_variables (c_parser *);
1170 static void c_parser_objc_class_declaration (c_parser *);
1171 static void c_parser_objc_alias_declaration (c_parser *);
1172 static void c_parser_objc_protocol_definition (c_parser *, tree);
1173 static bool c_parser_objc_method_type (c_parser *);
1174 static void c_parser_objc_method_definition (c_parser *);
1175 static void c_parser_objc_methodprotolist (c_parser *);
1176 static void c_parser_objc_methodproto (c_parser *);
1177 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1178 static tree c_parser_objc_type_name (c_parser *);
1179 static tree c_parser_objc_protocol_refs (c_parser *);
1180 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1181 static void c_parser_objc_synchronized_statement (c_parser *);
1182 static tree c_parser_objc_selector (c_parser *);
1183 static tree c_parser_objc_selector_arg (c_parser *);
1184 static tree c_parser_objc_receiver (c_parser *);
1185 static tree c_parser_objc_message_args (c_parser *);
1186 static tree c_parser_objc_keywordexpr (c_parser *);
1187 static void c_parser_objc_at_property_declaration (c_parser *);
1188 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1189 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1190 static bool c_parser_objc_diagnose_bad_element_prefix
1191 (c_parser *, struct c_declspecs *);
1193 /* Parse a translation unit (C90 6.7, C99 6.9).
1195 translation-unit:
1196 external-declarations
1198 external-declarations:
1199 external-declaration
1200 external-declarations external-declaration
1202 GNU extensions:
1204 translation-unit:
1205 empty
1208 static void
1209 c_parser_translation_unit (c_parser *parser)
1211 if (c_parser_next_token_is (parser, CPP_EOF))
1213 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1214 "ISO C forbids an empty translation unit");
1216 else
1218 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1219 mark_valid_location_for_stdc_pragma (false);
1222 ggc_collect ();
1223 c_parser_external_declaration (parser);
1224 obstack_free (&parser_obstack, obstack_position);
1226 while (c_parser_next_token_is_not (parser, CPP_EOF));
1230 /* Parse an external declaration (C90 6.7, C99 6.9).
1232 external-declaration:
1233 function-definition
1234 declaration
1236 GNU extensions:
1238 external-declaration:
1239 asm-definition
1241 __extension__ external-declaration
1243 Objective-C:
1245 external-declaration:
1246 objc-class-definition
1247 objc-class-declaration
1248 objc-alias-declaration
1249 objc-protocol-definition
1250 objc-method-definition
1251 @end
1254 static void
1255 c_parser_external_declaration (c_parser *parser)
1257 int ext;
1258 switch (c_parser_peek_token (parser)->type)
1260 case CPP_KEYWORD:
1261 switch (c_parser_peek_token (parser)->keyword)
1263 case RID_EXTENSION:
1264 ext = disable_extension_diagnostics ();
1265 c_parser_consume_token (parser);
1266 c_parser_external_declaration (parser);
1267 restore_extension_diagnostics (ext);
1268 break;
1269 case RID_ASM:
1270 c_parser_asm_definition (parser);
1271 break;
1272 case RID_AT_INTERFACE:
1273 case RID_AT_IMPLEMENTATION:
1274 gcc_assert (c_dialect_objc ());
1275 c_parser_objc_class_definition (parser, NULL_TREE);
1276 break;
1277 case RID_AT_CLASS:
1278 gcc_assert (c_dialect_objc ());
1279 c_parser_objc_class_declaration (parser);
1280 break;
1281 case RID_AT_ALIAS:
1282 gcc_assert (c_dialect_objc ());
1283 c_parser_objc_alias_declaration (parser);
1284 break;
1285 case RID_AT_PROTOCOL:
1286 gcc_assert (c_dialect_objc ());
1287 c_parser_objc_protocol_definition (parser, NULL_TREE);
1288 break;
1289 case RID_AT_PROPERTY:
1290 gcc_assert (c_dialect_objc ());
1291 c_parser_objc_at_property_declaration (parser);
1292 break;
1293 case RID_AT_SYNTHESIZE:
1294 gcc_assert (c_dialect_objc ());
1295 c_parser_objc_at_synthesize_declaration (parser);
1296 break;
1297 case RID_AT_DYNAMIC:
1298 gcc_assert (c_dialect_objc ());
1299 c_parser_objc_at_dynamic_declaration (parser);
1300 break;
1301 case RID_AT_END:
1302 gcc_assert (c_dialect_objc ());
1303 c_parser_consume_token (parser);
1304 objc_finish_implementation ();
1305 break;
1306 default:
1307 goto decl_or_fndef;
1309 break;
1310 case CPP_SEMICOLON:
1311 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1312 "ISO C does not allow extra %<;%> outside of a function");
1313 c_parser_consume_token (parser);
1314 break;
1315 case CPP_PRAGMA:
1316 mark_valid_location_for_stdc_pragma (true);
1317 c_parser_pragma (parser, pragma_external);
1318 mark_valid_location_for_stdc_pragma (false);
1319 break;
1320 case CPP_PLUS:
1321 case CPP_MINUS:
1322 if (c_dialect_objc ())
1324 c_parser_objc_method_definition (parser);
1325 break;
1327 /* Else fall through, and yield a syntax error trying to parse
1328 as a declaration or function definition. */
1329 default:
1330 decl_or_fndef:
1331 /* A declaration or a function definition (or, in Objective-C,
1332 an @interface or @protocol with prefix attributes). We can
1333 only tell which after parsing the declaration specifiers, if
1334 any, and the first declarator. */
1335 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1336 break;
1340 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1341 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1342 accepted; otherwise (old-style parameter declarations) only other
1343 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1344 assertion is accepted; otherwise (old-style parameter declarations)
1345 it is not. If NESTED is true, we are inside a function or parsing
1346 old-style parameter declarations; any functions encountered are
1347 nested functions and declaration specifiers are required; otherwise
1348 we are at top level and functions are normal functions and
1349 declaration specifiers may be optional. If EMPTY_OK is true, empty
1350 declarations are OK (subject to all other constraints); otherwise
1351 (old-style parameter declarations) they are diagnosed. If
1352 START_ATTR_OK is true, the declaration specifiers may start with
1353 attributes; otherwise they may not.
1354 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1355 declaration when parsing an Objective-C foreach statement.
1357 declaration:
1358 declaration-specifiers init-declarator-list[opt] ;
1359 static_assert-declaration
1361 function-definition:
1362 declaration-specifiers[opt] declarator declaration-list[opt]
1363 compound-statement
1365 declaration-list:
1366 declaration
1367 declaration-list declaration
1369 init-declarator-list:
1370 init-declarator
1371 init-declarator-list , init-declarator
1373 init-declarator:
1374 declarator simple-asm-expr[opt] attributes[opt]
1375 declarator simple-asm-expr[opt] attributes[opt] = initializer
1377 GNU extensions:
1379 nested-function-definition:
1380 declaration-specifiers declarator declaration-list[opt]
1381 compound-statement
1383 Objective-C:
1384 attributes objc-class-definition
1385 attributes objc-category-definition
1386 attributes objc-protocol-definition
1388 The simple-asm-expr and attributes are GNU extensions.
1390 This function does not handle __extension__; that is handled in its
1391 callers. ??? Following the old parser, __extension__ may start
1392 external declarations, declarations in functions and declarations
1393 at the start of "for" loops, but not old-style parameter
1394 declarations.
1396 C99 requires declaration specifiers in a function definition; the
1397 absence is diagnosed through the diagnosis of implicit int. In GNU
1398 C we also allow but diagnose declarations without declaration
1399 specifiers, but only at top level (elsewhere they conflict with
1400 other syntax).
1402 In Objective-C, declarations of the looping variable in a foreach
1403 statement are exceptionally terminated by 'in' (for example, 'for
1404 (NSObject *object in array) { ... }').
1406 OpenMP:
1408 declaration:
1409 threadprivate-directive */
1411 static void
1412 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1413 bool static_assert_ok, bool empty_ok,
1414 bool nested, bool start_attr_ok,
1415 tree *objc_foreach_object_declaration)
1417 struct c_declspecs *specs;
1418 tree prefix_attrs;
1419 tree all_prefix_attrs;
1420 bool diagnosed_no_specs = false;
1421 location_t here = c_parser_peek_token (parser)->location;
1423 if (static_assert_ok
1424 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1426 c_parser_static_assert_declaration (parser);
1427 return;
1429 specs = build_null_declspecs ();
1431 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1432 if (c_parser_peek_token (parser)->type == CPP_NAME
1433 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1434 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1435 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1436 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1438 error_at (here, "unknown type name %qE",
1439 c_parser_peek_token (parser)->value);
1441 /* Parse declspecs normally to get a correct pointer type, but avoid
1442 a further "fails to be a type name" error. Refuse nested functions
1443 since it is not how the user likely wants us to recover. */
1444 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1445 c_parser_peek_token (parser)->keyword = RID_VOID;
1446 c_parser_peek_token (parser)->value = error_mark_node;
1447 fndef_ok = !nested;
1450 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1451 if (parser->error)
1453 c_parser_skip_to_end_of_block_or_statement (parser);
1454 return;
1456 if (nested && !specs->declspecs_seen_p)
1458 c_parser_error (parser, "expected declaration specifiers");
1459 c_parser_skip_to_end_of_block_or_statement (parser);
1460 return;
1462 finish_declspecs (specs);
1463 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1465 if (empty_ok)
1466 shadow_tag (specs);
1467 else
1469 shadow_tag_warned (specs, 1);
1470 pedwarn (here, 0, "empty declaration");
1472 c_parser_consume_token (parser);
1473 return;
1476 /* Provide better error recovery. Note that a type name here is usually
1477 better diagnosed as a redeclaration. */
1478 if (empty_ok
1479 && specs->typespec_kind == ctsk_tagdef
1480 && c_parser_next_token_starts_declspecs (parser)
1481 && !c_parser_next_token_is (parser, CPP_NAME))
1483 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1484 parser->error = false;
1485 shadow_tag_warned (specs, 1);
1486 return;
1488 else if (c_dialect_objc ())
1490 /* Prefix attributes are an error on method decls. */
1491 switch (c_parser_peek_token (parser)->type)
1493 case CPP_PLUS:
1494 case CPP_MINUS:
1495 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1496 return;
1497 if (specs->attrs)
1499 warning_at (c_parser_peek_token (parser)->location,
1500 OPT_Wattributes,
1501 "prefix attributes are ignored for methods");
1502 specs->attrs = NULL_TREE;
1504 if (fndef_ok)
1505 c_parser_objc_method_definition (parser);
1506 else
1507 c_parser_objc_methodproto (parser);
1508 return;
1509 break;
1510 default:
1511 break;
1513 /* This is where we parse 'attributes @interface ...',
1514 'attributes @implementation ...', 'attributes @protocol ...'
1515 (where attributes could be, for example, __attribute__
1516 ((deprecated)).
1518 switch (c_parser_peek_token (parser)->keyword)
1520 case RID_AT_INTERFACE:
1522 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1523 return;
1524 c_parser_objc_class_definition (parser, specs->attrs);
1525 return;
1527 break;
1528 case RID_AT_IMPLEMENTATION:
1530 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1531 return;
1532 if (specs->attrs)
1534 warning_at (c_parser_peek_token (parser)->location,
1535 OPT_Wattributes,
1536 "prefix attributes are ignored for implementations");
1537 specs->attrs = NULL_TREE;
1539 c_parser_objc_class_definition (parser, NULL_TREE);
1540 return;
1542 break;
1543 case RID_AT_PROTOCOL:
1545 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1546 return;
1547 c_parser_objc_protocol_definition (parser, specs->attrs);
1548 return;
1550 break;
1551 case RID_AT_ALIAS:
1552 case RID_AT_CLASS:
1553 case RID_AT_END:
1554 case RID_AT_PROPERTY:
1555 if (specs->attrs)
1557 c_parser_error (parser, "unexpected attribute");
1558 specs->attrs = NULL;
1560 break;
1561 default:
1562 break;
1566 pending_xref_error ();
1567 prefix_attrs = specs->attrs;
1568 all_prefix_attrs = prefix_attrs;
1569 specs->attrs = NULL_TREE;
1570 while (true)
1572 struct c_declarator *declarator;
1573 bool dummy = false;
1574 timevar_id_t tv;
1575 tree fnbody;
1576 /* Declaring either one or more declarators (in which case we
1577 should diagnose if there were no declaration specifiers) or a
1578 function definition (in which case the diagnostic for
1579 implicit int suffices). */
1580 declarator = c_parser_declarator (parser,
1581 specs->typespec_kind != ctsk_none,
1582 C_DTR_NORMAL, &dummy);
1583 if (declarator == NULL)
1585 c_parser_skip_to_end_of_block_or_statement (parser);
1586 return;
1588 if (c_parser_next_token_is (parser, CPP_EQ)
1589 || c_parser_next_token_is (parser, CPP_COMMA)
1590 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1591 || c_parser_next_token_is_keyword (parser, RID_ASM)
1592 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1593 || c_parser_next_token_is_keyword (parser, RID_IN))
1595 tree asm_name = NULL_TREE;
1596 tree postfix_attrs = NULL_TREE;
1597 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1599 diagnosed_no_specs = true;
1600 pedwarn (here, 0, "data definition has no type or storage class");
1602 /* Having seen a data definition, there cannot now be a
1603 function definition. */
1604 fndef_ok = false;
1605 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1606 asm_name = c_parser_simple_asm_expr (parser);
1607 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1608 postfix_attrs = c_parser_attributes (parser);
1609 if (c_parser_next_token_is (parser, CPP_EQ))
1611 tree d;
1612 struct c_expr init;
1613 location_t init_loc;
1614 c_parser_consume_token (parser);
1615 /* The declaration of the variable is in effect while
1616 its initializer is parsed. */
1617 d = start_decl (declarator, specs, true,
1618 chainon (postfix_attrs, all_prefix_attrs));
1619 if (!d)
1620 d = error_mark_node;
1621 start_init (d, asm_name, global_bindings_p ());
1622 init_loc = c_parser_peek_token (parser)->location;
1623 init = c_parser_initializer (parser);
1624 finish_init ();
1625 if (d != error_mark_node)
1627 maybe_warn_string_init (TREE_TYPE (d), init);
1628 finish_decl (d, init_loc, init.value,
1629 init.original_type, asm_name);
1632 else
1634 tree d = start_decl (declarator, specs, false,
1635 chainon (postfix_attrs,
1636 all_prefix_attrs));
1637 if (d)
1638 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1639 NULL_TREE, asm_name);
1641 if (c_parser_next_token_is_keyword (parser, RID_IN))
1643 if (d)
1644 *objc_foreach_object_declaration = d;
1645 else
1646 *objc_foreach_object_declaration = error_mark_node;
1649 if (c_parser_next_token_is (parser, CPP_COMMA))
1651 c_parser_consume_token (parser);
1652 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1653 all_prefix_attrs = chainon (c_parser_attributes (parser),
1654 prefix_attrs);
1655 else
1656 all_prefix_attrs = prefix_attrs;
1657 continue;
1659 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1661 c_parser_consume_token (parser);
1662 return;
1664 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1666 /* This can only happen in Objective-C: we found the
1667 'in' that terminates the declaration inside an
1668 Objective-C foreach statement. Do not consume the
1669 token, so that the caller can use it to determine
1670 that this indeed is a foreach context. */
1671 return;
1673 else
1675 c_parser_error (parser, "expected %<,%> or %<;%>");
1676 c_parser_skip_to_end_of_block_or_statement (parser);
1677 return;
1680 else if (!fndef_ok)
1682 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1683 "%<asm%> or %<__attribute__%>");
1684 c_parser_skip_to_end_of_block_or_statement (parser);
1685 return;
1687 /* Function definition (nested or otherwise). */
1688 if (nested)
1690 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1691 c_push_function_context ();
1693 if (!start_function (specs, declarator, all_prefix_attrs))
1695 /* This can appear in many cases looking nothing like a
1696 function definition, so we don't give a more specific
1697 error suggesting there was one. */
1698 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1699 "or %<__attribute__%>");
1700 if (nested)
1701 c_pop_function_context ();
1702 break;
1705 if (DECL_DECLARED_INLINE_P (current_function_decl))
1706 tv = TV_PARSE_INLINE;
1707 else
1708 tv = TV_PARSE_FUNC;
1709 timevar_push (tv);
1711 /* Parse old-style parameter declarations. ??? Attributes are
1712 not allowed to start declaration specifiers here because of a
1713 syntax conflict between a function declaration with attribute
1714 suffix and a function definition with an attribute prefix on
1715 first old-style parameter declaration. Following the old
1716 parser, they are not accepted on subsequent old-style
1717 parameter declarations either. However, there is no
1718 ambiguity after the first declaration, nor indeed on the
1719 first as long as we don't allow postfix attributes after a
1720 declarator with a nonempty identifier list in a definition;
1721 and postfix attributes have never been accepted here in
1722 function definitions either. */
1723 while (c_parser_next_token_is_not (parser, CPP_EOF)
1724 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1725 c_parser_declaration_or_fndef (parser, false, false, false,
1726 true, false, NULL);
1727 store_parm_decls ();
1728 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1729 = c_parser_peek_token (parser)->location;
1730 fnbody = c_parser_compound_statement (parser);
1731 if (nested)
1733 tree decl = current_function_decl;
1734 /* Mark nested functions as needing static-chain initially.
1735 lower_nested_functions will recompute it but the
1736 DECL_STATIC_CHAIN flag is also used before that happens,
1737 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1738 DECL_STATIC_CHAIN (decl) = 1;
1739 add_stmt (fnbody);
1740 finish_function ();
1741 c_pop_function_context ();
1742 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1744 else
1746 add_stmt (fnbody);
1747 finish_function ();
1750 timevar_pop (tv);
1751 break;
1755 /* Parse an asm-definition (asm() outside a function body). This is a
1756 GNU extension.
1758 asm-definition:
1759 simple-asm-expr ;
1762 static void
1763 c_parser_asm_definition (c_parser *parser)
1765 tree asm_str = c_parser_simple_asm_expr (parser);
1766 if (asm_str)
1767 cgraph_add_asm_node (asm_str);
1768 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1771 /* Parse a static assertion (C1X N1425 6.7.10).
1773 static_assert-declaration:
1774 static_assert-declaration-no-semi ;
1777 static void
1778 c_parser_static_assert_declaration (c_parser *parser)
1780 c_parser_static_assert_declaration_no_semi (parser);
1781 if (parser->error
1782 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1783 c_parser_skip_to_end_of_block_or_statement (parser);
1786 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1787 semicolon.
1789 static_assert-declaration-no-semi:
1790 _Static_assert ( constant-expression , string-literal )
1793 static void
1794 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1796 location_t assert_loc, value_loc;
1797 tree value;
1798 tree string;
1800 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1801 assert_loc = c_parser_peek_token (parser)->location;
1802 if (!flag_isoc1x)
1804 if (flag_isoc99)
1805 pedwarn (assert_loc, OPT_pedantic,
1806 "ISO C99 does not support %<_Static_assert%>");
1807 else
1808 pedwarn (assert_loc, OPT_pedantic,
1809 "ISO C90 does not support %<_Static_assert%>");
1811 c_parser_consume_token (parser);
1812 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1813 return;
1814 value_loc = c_parser_peek_token (parser)->location;
1815 value = c_parser_expr_no_commas (parser, NULL).value;
1816 parser->lex_untranslated_string = true;
1817 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1819 parser->lex_untranslated_string = false;
1820 return;
1822 switch (c_parser_peek_token (parser)->type)
1824 case CPP_STRING:
1825 case CPP_STRING16:
1826 case CPP_STRING32:
1827 case CPP_WSTRING:
1828 case CPP_UTF8STRING:
1829 string = c_parser_peek_token (parser)->value;
1830 c_parser_consume_token (parser);
1831 parser->lex_untranslated_string = false;
1832 break;
1833 default:
1834 c_parser_error (parser, "expected string literal");
1835 parser->lex_untranslated_string = false;
1836 return;
1838 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1840 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1842 error_at (value_loc, "expression in static assertion is not an integer");
1843 return;
1845 if (TREE_CODE (value) != INTEGER_CST)
1847 value = c_fully_fold (value, false, NULL);
1848 if (TREE_CODE (value) == INTEGER_CST)
1849 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1850 "is not an integer constant expression");
1852 if (TREE_CODE (value) != INTEGER_CST)
1854 error_at (value_loc, "expression in static assertion is not constant");
1855 return;
1857 constant_expression_warning (value);
1858 if (integer_zerop (value))
1859 error_at (assert_loc, "static assertion failed: %E", string);
1862 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1863 6.7), adding them to SPECS (which may already include some).
1864 Storage class specifiers are accepted iff SCSPEC_OK; type
1865 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1866 the start iff START_ATTR_OK.
1868 declaration-specifiers:
1869 storage-class-specifier declaration-specifiers[opt]
1870 type-specifier declaration-specifiers[opt]
1871 type-qualifier declaration-specifiers[opt]
1872 function-specifier declaration-specifiers[opt]
1874 Function specifiers (inline) are from C99, and are currently
1875 handled as storage class specifiers, as is __thread.
1877 C90 6.5.1, C99 6.7.1:
1878 storage-class-specifier:
1879 typedef
1880 extern
1881 static
1882 auto
1883 register
1885 C99 6.7.4:
1886 function-specifier:
1887 inline
1889 C90 6.5.2, C99 6.7.2:
1890 type-specifier:
1891 void
1892 char
1893 short
1895 long
1896 float
1897 double
1898 signed
1899 unsigned
1900 _Bool
1901 _Complex
1902 [_Imaginary removed in C99 TC2]
1903 struct-or-union-specifier
1904 enum-specifier
1905 typedef-name
1907 (_Bool and _Complex are new in C99.)
1909 C90 6.5.3, C99 6.7.3:
1911 type-qualifier:
1912 const
1913 restrict
1914 volatile
1915 address-space-qualifier
1917 (restrict is new in C99.)
1919 GNU extensions:
1921 declaration-specifiers:
1922 attributes declaration-specifiers[opt]
1924 type-qualifier:
1925 address-space
1927 address-space:
1928 identifier recognized by the target
1930 storage-class-specifier:
1931 __thread
1933 type-specifier:
1934 typeof-specifier
1935 __int128
1936 _Decimal32
1937 _Decimal64
1938 _Decimal128
1939 _Fract
1940 _Accum
1941 _Sat
1943 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1944 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1946 Objective-C:
1948 type-specifier:
1949 class-name objc-protocol-refs[opt]
1950 typedef-name objc-protocol-refs
1951 objc-protocol-refs
1954 static void
1955 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1956 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1957 enum c_lookahead_kind la)
1959 bool attrs_ok = start_attr_ok;
1960 bool seen_type = specs->typespec_kind != ctsk_none;
1962 if (!typespec_ok)
1963 gcc_assert (la == cla_prefer_id);
1965 while (c_parser_next_token_is (parser, CPP_NAME)
1966 || c_parser_next_token_is (parser, CPP_KEYWORD)
1967 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1969 struct c_typespec t;
1970 tree attrs;
1971 location_t loc = c_parser_peek_token (parser)->location;
1973 /* If we cannot accept a type, exit if the next token must start
1974 one. Also, if we already have seen a tagged definition,
1975 a typename would be an error anyway and likely the user
1976 has simply forgotten a semicolon, so we exit. */
1977 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
1978 && c_parser_next_tokens_start_typename (parser, la)
1979 && !c_parser_next_token_is_qualifier (parser))
1980 break;
1982 if (c_parser_next_token_is (parser, CPP_NAME))
1984 tree value = c_parser_peek_token (parser)->value;
1985 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1987 if (kind == C_ID_ADDRSPACE)
1989 addr_space_t as
1990 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1991 declspecs_add_addrspace (specs, as);
1992 c_parser_consume_token (parser);
1993 attrs_ok = true;
1994 continue;
1997 gcc_assert (!c_parser_next_token_is_qualifier (parser));
1999 /* If we cannot accept a type, and the next token must start one,
2000 exit. Do the same if we already have seen a tagged definition,
2001 since it would be an error anyway and likely the user has simply
2002 forgotten a semicolon. */
2003 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2004 break;
2006 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2007 a C_ID_CLASSNAME. */
2008 c_parser_consume_token (parser);
2009 seen_type = true;
2010 attrs_ok = true;
2011 if (kind == C_ID_ID)
2013 error ("unknown type name %qE", value);
2014 t.kind = ctsk_typedef;
2015 t.spec = error_mark_node;
2017 else if (kind == C_ID_TYPENAME
2018 && (!c_dialect_objc ()
2019 || c_parser_next_token_is_not (parser, CPP_LESS)))
2021 t.kind = ctsk_typedef;
2022 /* For a typedef name, record the meaning, not the name.
2023 In case of 'foo foo, bar;'. */
2024 t.spec = lookup_name (value);
2026 else
2028 tree proto = NULL_TREE;
2029 gcc_assert (c_dialect_objc ());
2030 t.kind = ctsk_objc;
2031 if (c_parser_next_token_is (parser, CPP_LESS))
2032 proto = c_parser_objc_protocol_refs (parser);
2033 t.spec = objc_get_protocol_qualified_type (value, proto);
2035 t.expr = NULL_TREE;
2036 t.expr_const_operands = true;
2037 declspecs_add_type (loc, specs, t);
2038 continue;
2040 if (c_parser_next_token_is (parser, CPP_LESS))
2042 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2043 nisse@lysator.liu.se. */
2044 tree proto;
2045 gcc_assert (c_dialect_objc ());
2046 if (!typespec_ok || seen_type)
2047 break;
2048 proto = c_parser_objc_protocol_refs (parser);
2049 t.kind = ctsk_objc;
2050 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2051 t.expr = NULL_TREE;
2052 t.expr_const_operands = true;
2053 declspecs_add_type (loc, specs, t);
2054 continue;
2056 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2057 switch (c_parser_peek_token (parser)->keyword)
2059 case RID_STATIC:
2060 case RID_EXTERN:
2061 case RID_REGISTER:
2062 case RID_TYPEDEF:
2063 case RID_INLINE:
2064 case RID_AUTO:
2065 case RID_THREAD:
2066 if (!scspec_ok)
2067 goto out;
2068 attrs_ok = true;
2069 /* TODO: Distinguish between function specifiers (inline)
2070 and storage class specifiers, either here or in
2071 declspecs_add_scspec. */
2072 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2073 c_parser_consume_token (parser);
2074 break;
2075 case RID_UNSIGNED:
2076 case RID_LONG:
2077 case RID_INT128:
2078 case RID_SHORT:
2079 case RID_SIGNED:
2080 case RID_COMPLEX:
2081 case RID_INT:
2082 case RID_CHAR:
2083 case RID_FLOAT:
2084 case RID_DOUBLE:
2085 case RID_VOID:
2086 case RID_DFLOAT32:
2087 case RID_DFLOAT64:
2088 case RID_DFLOAT128:
2089 case RID_BOOL:
2090 case RID_FRACT:
2091 case RID_ACCUM:
2092 case RID_SAT:
2093 if (!typespec_ok)
2094 goto out;
2095 attrs_ok = true;
2096 seen_type = true;
2097 if (c_dialect_objc ())
2098 parser->objc_need_raw_identifier = true;
2099 t.kind = ctsk_resword;
2100 t.spec = c_parser_peek_token (parser)->value;
2101 t.expr = NULL_TREE;
2102 t.expr_const_operands = true;
2103 declspecs_add_type (loc, specs, t);
2104 c_parser_consume_token (parser);
2105 break;
2106 case RID_ENUM:
2107 if (!typespec_ok)
2108 goto out;
2109 attrs_ok = true;
2110 seen_type = true;
2111 t = c_parser_enum_specifier (parser);
2112 declspecs_add_type (loc, specs, t);
2113 break;
2114 case RID_STRUCT:
2115 case RID_UNION:
2116 if (!typespec_ok)
2117 goto out;
2118 attrs_ok = true;
2119 seen_type = true;
2120 t = c_parser_struct_or_union_specifier (parser);
2121 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2122 declspecs_add_type (loc, specs, t);
2123 break;
2124 case RID_TYPEOF:
2125 /* ??? The old parser rejected typeof after other type
2126 specifiers, but is a syntax error the best way of
2127 handling this? */
2128 if (!typespec_ok || seen_type)
2129 goto out;
2130 attrs_ok = true;
2131 seen_type = true;
2132 t = c_parser_typeof_specifier (parser);
2133 declspecs_add_type (loc, specs, t);
2134 break;
2135 case RID_CONST:
2136 case RID_VOLATILE:
2137 case RID_RESTRICT:
2138 attrs_ok = true;
2139 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2140 c_parser_consume_token (parser);
2141 break;
2142 case RID_ATTRIBUTE:
2143 if (!attrs_ok)
2144 goto out;
2145 attrs = c_parser_attributes (parser);
2146 declspecs_add_attrs (specs, attrs);
2147 break;
2148 default:
2149 goto out;
2152 out: ;
2155 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2157 enum-specifier:
2158 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2159 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2160 enum attributes[opt] identifier
2162 The form with trailing comma is new in C99. The forms with
2163 attributes are GNU extensions. In GNU C, we accept any expression
2164 without commas in the syntax (assignment expressions, not just
2165 conditional expressions); assignment expressions will be diagnosed
2166 as non-constant.
2168 enumerator-list:
2169 enumerator
2170 enumerator-list , enumerator
2172 enumerator:
2173 enumeration-constant
2174 enumeration-constant = constant-expression
2177 static struct c_typespec
2178 c_parser_enum_specifier (c_parser *parser)
2180 struct c_typespec ret;
2181 tree attrs;
2182 tree ident = NULL_TREE;
2183 location_t enum_loc;
2184 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2185 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2186 enum_loc = c_parser_peek_token (parser)->location;
2187 c_parser_consume_token (parser);
2188 attrs = c_parser_attributes (parser);
2189 enum_loc = c_parser_peek_token (parser)->location;
2190 /* Set the location in case we create a decl now. */
2191 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2192 if (c_parser_next_token_is (parser, CPP_NAME))
2194 ident = c_parser_peek_token (parser)->value;
2195 ident_loc = c_parser_peek_token (parser)->location;
2196 enum_loc = ident_loc;
2197 c_parser_consume_token (parser);
2199 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2201 /* Parse an enum definition. */
2202 struct c_enum_contents the_enum;
2203 tree type;
2204 tree postfix_attrs;
2205 /* We chain the enumerators in reverse order, then put them in
2206 forward order at the end. */
2207 tree values;
2208 timevar_push (TV_PARSE_ENUM);
2209 type = start_enum (enum_loc, &the_enum, ident);
2210 values = NULL_TREE;
2211 c_parser_consume_token (parser);
2212 while (true)
2214 tree enum_id;
2215 tree enum_value;
2216 tree enum_decl;
2217 bool seen_comma;
2218 c_token *token;
2219 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2220 location_t decl_loc, value_loc;
2221 if (c_parser_next_token_is_not (parser, CPP_NAME))
2223 c_parser_error (parser, "expected identifier");
2224 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2225 values = error_mark_node;
2226 break;
2228 token = c_parser_peek_token (parser);
2229 enum_id = token->value;
2230 /* Set the location in case we create a decl now. */
2231 c_parser_set_source_position_from_token (token);
2232 decl_loc = value_loc = token->location;
2233 c_parser_consume_token (parser);
2234 if (c_parser_next_token_is (parser, CPP_EQ))
2236 c_parser_consume_token (parser);
2237 value_loc = c_parser_peek_token (parser)->location;
2238 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2240 else
2241 enum_value = NULL_TREE;
2242 enum_decl = build_enumerator (decl_loc, value_loc,
2243 &the_enum, enum_id, enum_value);
2244 TREE_CHAIN (enum_decl) = values;
2245 values = enum_decl;
2246 seen_comma = false;
2247 if (c_parser_next_token_is (parser, CPP_COMMA))
2249 comma_loc = c_parser_peek_token (parser)->location;
2250 seen_comma = true;
2251 c_parser_consume_token (parser);
2253 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2255 if (seen_comma && !flag_isoc99)
2256 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2257 c_parser_consume_token (parser);
2258 break;
2260 if (!seen_comma)
2262 c_parser_error (parser, "expected %<,%> or %<}%>");
2263 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2264 values = error_mark_node;
2265 break;
2268 postfix_attrs = c_parser_attributes (parser);
2269 ret.spec = finish_enum (type, nreverse (values),
2270 chainon (attrs, postfix_attrs));
2271 ret.kind = ctsk_tagdef;
2272 ret.expr = NULL_TREE;
2273 ret.expr_const_operands = true;
2274 timevar_pop (TV_PARSE_ENUM);
2275 return ret;
2277 else if (!ident)
2279 c_parser_error (parser, "expected %<{%>");
2280 ret.spec = error_mark_node;
2281 ret.kind = ctsk_tagref;
2282 ret.expr = NULL_TREE;
2283 ret.expr_const_operands = true;
2284 return ret;
2286 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2287 /* In ISO C, enumerated types can be referred to only if already
2288 defined. */
2289 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2291 gcc_assert (ident);
2292 pedwarn (enum_loc, OPT_pedantic,
2293 "ISO C forbids forward references to %<enum%> types");
2295 return ret;
2298 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2300 struct-or-union-specifier:
2301 struct-or-union attributes[opt] identifier[opt]
2302 { struct-contents } attributes[opt]
2303 struct-or-union attributes[opt] identifier
2305 struct-contents:
2306 struct-declaration-list
2308 struct-declaration-list:
2309 struct-declaration ;
2310 struct-declaration-list struct-declaration ;
2312 GNU extensions:
2314 struct-contents:
2315 empty
2316 struct-declaration
2317 struct-declaration-list struct-declaration
2319 struct-declaration-list:
2320 struct-declaration-list ;
2323 (Note that in the syntax here, unlike that in ISO C, the semicolons
2324 are included here rather than in struct-declaration, in order to
2325 describe the syntax with extra semicolons and missing semicolon at
2326 end.)
2328 Objective-C:
2330 struct-declaration-list:
2331 @defs ( class-name )
2333 (Note this does not include a trailing semicolon, but can be
2334 followed by further declarations, and gets a pedwarn-if-pedantic
2335 when followed by a semicolon.) */
2337 static struct c_typespec
2338 c_parser_struct_or_union_specifier (c_parser *parser)
2340 struct c_typespec ret;
2341 tree attrs;
2342 tree ident = NULL_TREE;
2343 location_t struct_loc;
2344 location_t ident_loc = UNKNOWN_LOCATION;
2345 enum tree_code code;
2346 switch (c_parser_peek_token (parser)->keyword)
2348 case RID_STRUCT:
2349 code = RECORD_TYPE;
2350 break;
2351 case RID_UNION:
2352 code = UNION_TYPE;
2353 break;
2354 default:
2355 gcc_unreachable ();
2357 struct_loc = c_parser_peek_token (parser)->location;
2358 c_parser_consume_token (parser);
2359 attrs = c_parser_attributes (parser);
2361 /* Set the location in case we create a decl now. */
2362 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2364 if (c_parser_next_token_is (parser, CPP_NAME))
2366 ident = c_parser_peek_token (parser)->value;
2367 ident_loc = c_parser_peek_token (parser)->location;
2368 struct_loc = ident_loc;
2369 c_parser_consume_token (parser);
2371 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2373 /* Parse a struct or union definition. Start the scope of the
2374 tag before parsing components. */
2375 struct c_struct_parse_info *struct_info;
2376 tree type = start_struct (struct_loc, code, ident, &struct_info);
2377 tree postfix_attrs;
2378 /* We chain the components in reverse order, then put them in
2379 forward order at the end. Each struct-declaration may
2380 declare multiple components (comma-separated), so we must use
2381 chainon to join them, although when parsing each
2382 struct-declaration we can use TREE_CHAIN directly.
2384 The theory behind all this is that there will be more
2385 semicolon separated fields than comma separated fields, and
2386 so we'll be minimizing the number of node traversals required
2387 by chainon. */
2388 tree contents;
2389 timevar_push (TV_PARSE_STRUCT);
2390 contents = NULL_TREE;
2391 c_parser_consume_token (parser);
2392 /* Handle the Objective-C @defs construct,
2393 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2394 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2396 tree name;
2397 gcc_assert (c_dialect_objc ());
2398 c_parser_consume_token (parser);
2399 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2400 goto end_at_defs;
2401 if (c_parser_next_token_is (parser, CPP_NAME)
2402 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2404 name = c_parser_peek_token (parser)->value;
2405 c_parser_consume_token (parser);
2407 else
2409 c_parser_error (parser, "expected class name");
2410 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2411 goto end_at_defs;
2413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2414 "expected %<)%>");
2415 contents = nreverse (objc_get_class_ivars (name));
2417 end_at_defs:
2418 /* Parse the struct-declarations and semicolons. Problems with
2419 semicolons are diagnosed here; empty structures are diagnosed
2420 elsewhere. */
2421 while (true)
2423 tree decls;
2424 /* Parse any stray semicolon. */
2425 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2427 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2428 "extra semicolon in struct or union specified");
2429 c_parser_consume_token (parser);
2430 continue;
2432 /* Stop if at the end of the struct or union contents. */
2433 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2435 c_parser_consume_token (parser);
2436 break;
2438 /* Accept #pragmas at struct scope. */
2439 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2441 c_parser_pragma (parser, pragma_external);
2442 continue;
2444 /* Parse some comma-separated declarations, but not the
2445 trailing semicolon if any. */
2446 decls = c_parser_struct_declaration (parser);
2447 contents = chainon (decls, contents);
2448 /* If no semicolon follows, either we have a parse error or
2449 are at the end of the struct or union and should
2450 pedwarn. */
2451 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2452 c_parser_consume_token (parser);
2453 else
2455 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2456 pedwarn (c_parser_peek_token (parser)->location, 0,
2457 "no semicolon at end of struct or union");
2458 else if (parser->error
2459 || !c_parser_next_token_starts_declspecs (parser))
2461 c_parser_error (parser, "expected %<;%>");
2462 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2463 break;
2466 /* If we come here, we have already emitted an error
2467 for an expected `;', identifier or `(', and we also
2468 recovered already. Go on with the next field. */
2471 postfix_attrs = c_parser_attributes (parser);
2472 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2473 chainon (attrs, postfix_attrs), struct_info);
2474 ret.kind = ctsk_tagdef;
2475 ret.expr = NULL_TREE;
2476 ret.expr_const_operands = true;
2477 timevar_pop (TV_PARSE_STRUCT);
2478 return ret;
2480 else if (!ident)
2482 c_parser_error (parser, "expected %<{%>");
2483 ret.spec = error_mark_node;
2484 ret.kind = ctsk_tagref;
2485 ret.expr = NULL_TREE;
2486 ret.expr_const_operands = true;
2487 return ret;
2489 ret = parser_xref_tag (ident_loc, code, ident);
2490 return ret;
2493 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2494 the trailing semicolon.
2496 struct-declaration:
2497 specifier-qualifier-list struct-declarator-list
2498 static_assert-declaration-no-semi
2500 specifier-qualifier-list:
2501 type-specifier specifier-qualifier-list[opt]
2502 type-qualifier specifier-qualifier-list[opt]
2503 attributes specifier-qualifier-list[opt]
2505 struct-declarator-list:
2506 struct-declarator
2507 struct-declarator-list , attributes[opt] struct-declarator
2509 struct-declarator:
2510 declarator attributes[opt]
2511 declarator[opt] : constant-expression attributes[opt]
2513 GNU extensions:
2515 struct-declaration:
2516 __extension__ struct-declaration
2517 specifier-qualifier-list
2519 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2520 of attributes where shown is a GNU extension. In GNU C, we accept
2521 any expression without commas in the syntax (assignment
2522 expressions, not just conditional expressions); assignment
2523 expressions will be diagnosed as non-constant. */
2525 static tree
2526 c_parser_struct_declaration (c_parser *parser)
2528 struct c_declspecs *specs;
2529 tree prefix_attrs;
2530 tree all_prefix_attrs;
2531 tree decls;
2532 location_t decl_loc;
2533 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2535 int ext;
2536 tree decl;
2537 ext = disable_extension_diagnostics ();
2538 c_parser_consume_token (parser);
2539 decl = c_parser_struct_declaration (parser);
2540 restore_extension_diagnostics (ext);
2541 return decl;
2543 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2545 c_parser_static_assert_declaration_no_semi (parser);
2546 return NULL_TREE;
2548 specs = build_null_declspecs ();
2549 decl_loc = c_parser_peek_token (parser)->location;
2550 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2551 if (parser->error)
2552 return NULL_TREE;
2553 if (!specs->declspecs_seen_p)
2555 c_parser_error (parser, "expected specifier-qualifier-list");
2556 return NULL_TREE;
2558 finish_declspecs (specs);
2559 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2560 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2562 tree ret;
2563 if (specs->typespec_kind == ctsk_none)
2565 pedwarn (decl_loc, OPT_pedantic,
2566 "ISO C forbids member declarations with no members");
2567 shadow_tag_warned (specs, pedantic);
2568 ret = NULL_TREE;
2570 else
2572 /* Support for unnamed structs or unions as members of
2573 structs or unions (which is [a] useful and [b] supports
2574 MS P-SDK). */
2575 tree attrs = NULL;
2577 ret = grokfield (c_parser_peek_token (parser)->location,
2578 build_id_declarator (NULL_TREE), specs,
2579 NULL_TREE, &attrs);
2580 if (ret)
2581 decl_attributes (&ret, attrs, 0);
2583 return ret;
2586 /* Provide better error recovery. Note that a type name here is valid,
2587 and will be treated as a field name. */
2588 if (specs->typespec_kind == ctsk_tagdef
2589 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2590 && c_parser_next_token_starts_declspecs (parser)
2591 && !c_parser_next_token_is (parser, CPP_NAME))
2593 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2594 parser->error = false;
2595 return NULL_TREE;
2598 pending_xref_error ();
2599 prefix_attrs = specs->attrs;
2600 all_prefix_attrs = prefix_attrs;
2601 specs->attrs = NULL_TREE;
2602 decls = NULL_TREE;
2603 while (true)
2605 /* Declaring one or more declarators or un-named bit-fields. */
2606 struct c_declarator *declarator;
2607 bool dummy = false;
2608 if (c_parser_next_token_is (parser, CPP_COLON))
2609 declarator = build_id_declarator (NULL_TREE);
2610 else
2611 declarator = c_parser_declarator (parser,
2612 specs->typespec_kind != ctsk_none,
2613 C_DTR_NORMAL, &dummy);
2614 if (declarator == NULL)
2616 c_parser_skip_to_end_of_block_or_statement (parser);
2617 break;
2619 if (c_parser_next_token_is (parser, CPP_COLON)
2620 || c_parser_next_token_is (parser, CPP_COMMA)
2621 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2622 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2623 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2625 tree postfix_attrs = NULL_TREE;
2626 tree width = NULL_TREE;
2627 tree d;
2628 if (c_parser_next_token_is (parser, CPP_COLON))
2630 c_parser_consume_token (parser);
2631 width = c_parser_expr_no_commas (parser, NULL).value;
2633 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2634 postfix_attrs = c_parser_attributes (parser);
2635 d = grokfield (c_parser_peek_token (parser)->location,
2636 declarator, specs, width, &all_prefix_attrs);
2637 decl_attributes (&d, chainon (postfix_attrs,
2638 all_prefix_attrs), 0);
2639 DECL_CHAIN (d) = decls;
2640 decls = d;
2641 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2642 all_prefix_attrs = chainon (c_parser_attributes (parser),
2643 prefix_attrs);
2644 else
2645 all_prefix_attrs = prefix_attrs;
2646 if (c_parser_next_token_is (parser, CPP_COMMA))
2647 c_parser_consume_token (parser);
2648 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2649 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2651 /* Semicolon consumed in caller. */
2652 break;
2654 else
2656 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2657 break;
2660 else
2662 c_parser_error (parser,
2663 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2664 "%<__attribute__%>");
2665 break;
2668 return decls;
2671 /* Parse a typeof specifier (a GNU extension).
2673 typeof-specifier:
2674 typeof ( expression )
2675 typeof ( type-name )
2678 static struct c_typespec
2679 c_parser_typeof_specifier (c_parser *parser)
2681 struct c_typespec ret;
2682 ret.kind = ctsk_typeof;
2683 ret.spec = error_mark_node;
2684 ret.expr = NULL_TREE;
2685 ret.expr_const_operands = true;
2686 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2687 c_parser_consume_token (parser);
2688 c_inhibit_evaluation_warnings++;
2689 in_typeof++;
2690 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2692 c_inhibit_evaluation_warnings--;
2693 in_typeof--;
2694 return ret;
2696 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2698 struct c_type_name *type = c_parser_type_name (parser);
2699 c_inhibit_evaluation_warnings--;
2700 in_typeof--;
2701 if (type != NULL)
2703 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2704 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2707 else
2709 bool was_vm;
2710 location_t here = c_parser_peek_token (parser)->location;
2711 struct c_expr expr = c_parser_expression (parser);
2712 c_inhibit_evaluation_warnings--;
2713 in_typeof--;
2714 if (TREE_CODE (expr.value) == COMPONENT_REF
2715 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2716 error_at (here, "%<typeof%> applied to a bit-field");
2717 mark_exp_read (expr.value);
2718 ret.spec = TREE_TYPE (expr.value);
2719 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2720 /* This is returned with the type so that when the type is
2721 evaluated, this can be evaluated. */
2722 if (was_vm)
2723 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2724 pop_maybe_used (was_vm);
2726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2727 return ret;
2730 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2731 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2732 be redeclared; otherwise it may not. KIND indicates which kind of
2733 declarator is wanted. Returns a valid declarator except in the
2734 case of a syntax error in which case NULL is returned. *SEEN_ID is
2735 set to true if an identifier being declared is seen; this is used
2736 to diagnose bad forms of abstract array declarators and to
2737 determine whether an identifier list is syntactically permitted.
2739 declarator:
2740 pointer[opt] direct-declarator
2742 direct-declarator:
2743 identifier
2744 ( attributes[opt] declarator )
2745 direct-declarator array-declarator
2746 direct-declarator ( parameter-type-list )
2747 direct-declarator ( identifier-list[opt] )
2749 pointer:
2750 * type-qualifier-list[opt]
2751 * type-qualifier-list[opt] pointer
2753 type-qualifier-list:
2754 type-qualifier
2755 attributes
2756 type-qualifier-list type-qualifier
2757 type-qualifier-list attributes
2759 parameter-type-list:
2760 parameter-list
2761 parameter-list , ...
2763 parameter-list:
2764 parameter-declaration
2765 parameter-list , parameter-declaration
2767 parameter-declaration:
2768 declaration-specifiers declarator attributes[opt]
2769 declaration-specifiers abstract-declarator[opt] attributes[opt]
2771 identifier-list:
2772 identifier
2773 identifier-list , identifier
2775 abstract-declarator:
2776 pointer
2777 pointer[opt] direct-abstract-declarator
2779 direct-abstract-declarator:
2780 ( attributes[opt] abstract-declarator )
2781 direct-abstract-declarator[opt] array-declarator
2782 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2784 GNU extensions:
2786 direct-declarator:
2787 direct-declarator ( parameter-forward-declarations
2788 parameter-type-list[opt] )
2790 direct-abstract-declarator:
2791 direct-abstract-declarator[opt] ( parameter-forward-declarations
2792 parameter-type-list[opt] )
2794 parameter-forward-declarations:
2795 parameter-list ;
2796 parameter-forward-declarations parameter-list ;
2798 The uses of attributes shown above are GNU extensions.
2800 Some forms of array declarator are not included in C99 in the
2801 syntax for abstract declarators; these are disallowed elsewhere.
2802 This may be a defect (DR#289).
2804 This function also accepts an omitted abstract declarator as being
2805 an abstract declarator, although not part of the formal syntax. */
2807 static struct c_declarator *
2808 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2809 bool *seen_id)
2811 /* Parse any initial pointer part. */
2812 if (c_parser_next_token_is (parser, CPP_MULT))
2814 struct c_declspecs *quals_attrs = build_null_declspecs ();
2815 struct c_declarator *inner;
2816 c_parser_consume_token (parser);
2817 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2818 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2819 if (inner == NULL)
2820 return NULL;
2821 else
2822 return make_pointer_declarator (quals_attrs, inner);
2824 /* Now we have a direct declarator, direct abstract declarator or
2825 nothing (which counts as a direct abstract declarator here). */
2826 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2829 /* Parse a direct declarator or direct abstract declarator; arguments
2830 as c_parser_declarator. */
2832 static struct c_declarator *
2833 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2834 bool *seen_id)
2836 /* The direct declarator must start with an identifier (possibly
2837 omitted) or a parenthesized declarator (possibly abstract). In
2838 an ordinary declarator, initial parentheses must start a
2839 parenthesized declarator. In an abstract declarator or parameter
2840 declarator, they could start a parenthesized declarator or a
2841 parameter list. To tell which, the open parenthesis and any
2842 following attributes must be read. If a declaration specifier
2843 follows, then it is a parameter list; if the specifier is a
2844 typedef name, there might be an ambiguity about redeclaring it,
2845 which is resolved in the direction of treating it as a typedef
2846 name. If a close parenthesis follows, it is also an empty
2847 parameter list, as the syntax does not permit empty abstract
2848 declarators. Otherwise, it is a parenthesized declarator (in
2849 which case the analysis may be repeated inside it, recursively).
2851 ??? There is an ambiguity in a parameter declaration "int
2852 (__attribute__((foo)) x)", where x is not a typedef name: it
2853 could be an abstract declarator for a function, or declare x with
2854 parentheses. The proper resolution of this ambiguity needs
2855 documenting. At present we follow an accident of the old
2856 parser's implementation, whereby the first parameter must have
2857 some declaration specifiers other than just attributes. Thus as
2858 a parameter declaration it is treated as a parenthesized
2859 parameter named x, and as an abstract declarator it is
2860 rejected.
2862 ??? Also following the old parser, attributes inside an empty
2863 parameter list are ignored, making it a list not yielding a
2864 prototype, rather than giving an error or making it have one
2865 parameter with implicit type int.
2867 ??? Also following the old parser, typedef names may be
2868 redeclared in declarators, but not Objective-C class names. */
2870 if (kind != C_DTR_ABSTRACT
2871 && c_parser_next_token_is (parser, CPP_NAME)
2872 && ((type_seen_p
2873 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2874 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2875 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2877 struct c_declarator *inner
2878 = build_id_declarator (c_parser_peek_token (parser)->value);
2879 *seen_id = true;
2880 inner->id_loc = c_parser_peek_token (parser)->location;
2881 c_parser_consume_token (parser);
2882 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2885 if (kind != C_DTR_NORMAL
2886 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2888 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2889 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2892 /* Either we are at the end of an abstract declarator, or we have
2893 parentheses. */
2895 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2897 tree attrs;
2898 struct c_declarator *inner;
2899 c_parser_consume_token (parser);
2900 attrs = c_parser_attributes (parser);
2901 if (kind != C_DTR_NORMAL
2902 && (c_parser_next_token_starts_declspecs (parser)
2903 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2905 struct c_arg_info *args
2906 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2907 attrs);
2908 if (args == NULL)
2909 return NULL;
2910 else
2912 inner
2913 = build_function_declarator (args,
2914 build_id_declarator (NULL_TREE));
2915 return c_parser_direct_declarator_inner (parser, *seen_id,
2916 inner);
2919 /* A parenthesized declarator. */
2920 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2921 if (inner != NULL && attrs != NULL)
2922 inner = build_attrs_declarator (attrs, inner);
2923 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2925 c_parser_consume_token (parser);
2926 if (inner == NULL)
2927 return NULL;
2928 else
2929 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2931 else
2933 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2934 "expected %<)%>");
2935 return NULL;
2938 else
2940 if (kind == C_DTR_NORMAL)
2942 c_parser_error (parser, "expected identifier or %<(%>");
2943 return NULL;
2945 else
2946 return build_id_declarator (NULL_TREE);
2950 /* Parse part of a direct declarator or direct abstract declarator,
2951 given that some (in INNER) has already been parsed; ID_PRESENT is
2952 true if an identifier is present, false for an abstract
2953 declarator. */
2955 static struct c_declarator *
2956 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2957 struct c_declarator *inner)
2959 /* Parse a sequence of array declarators and parameter lists. */
2960 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2962 location_t brace_loc = c_parser_peek_token (parser)->location;
2963 struct c_declarator *declarator;
2964 struct c_declspecs *quals_attrs = build_null_declspecs ();
2965 bool static_seen;
2966 bool star_seen;
2967 tree dimen;
2968 c_parser_consume_token (parser);
2969 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2970 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2971 if (static_seen)
2972 c_parser_consume_token (parser);
2973 if (static_seen && !quals_attrs->declspecs_seen_p)
2974 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2975 if (!quals_attrs->declspecs_seen_p)
2976 quals_attrs = NULL;
2977 /* If "static" is present, there must be an array dimension.
2978 Otherwise, there may be a dimension, "*", or no
2979 dimension. */
2980 if (static_seen)
2982 star_seen = false;
2983 dimen = c_parser_expr_no_commas (parser, NULL).value;
2985 else
2987 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2989 dimen = NULL_TREE;
2990 star_seen = false;
2992 else if (c_parser_next_token_is (parser, CPP_MULT))
2994 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2996 dimen = NULL_TREE;
2997 star_seen = true;
2998 c_parser_consume_token (parser);
3000 else
3002 star_seen = false;
3003 dimen = c_parser_expr_no_commas (parser, NULL).value;
3006 else
3008 star_seen = false;
3009 dimen = c_parser_expr_no_commas (parser, NULL).value;
3012 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3013 c_parser_consume_token (parser);
3014 else
3016 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3017 "expected %<]%>");
3018 return NULL;
3020 if (dimen)
3021 mark_exp_read (dimen);
3022 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3023 static_seen, star_seen);
3024 if (declarator == NULL)
3025 return NULL;
3026 inner = set_array_declarator_inner (declarator, inner);
3027 return c_parser_direct_declarator_inner (parser, id_present, inner);
3029 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3031 tree attrs;
3032 struct c_arg_info *args;
3033 c_parser_consume_token (parser);
3034 attrs = c_parser_attributes (parser);
3035 args = c_parser_parms_declarator (parser, id_present, attrs);
3036 if (args == NULL)
3037 return NULL;
3038 else
3040 inner = build_function_declarator (args, inner);
3041 return c_parser_direct_declarator_inner (parser, id_present, inner);
3044 return inner;
3047 /* Parse a parameter list or identifier list, including the closing
3048 parenthesis but not the opening one. ATTRS are the attributes at
3049 the start of the list. ID_LIST_OK is true if an identifier list is
3050 acceptable; such a list must not have attributes at the start. */
3052 static struct c_arg_info *
3053 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3055 push_scope ();
3056 declare_parm_level ();
3057 /* If the list starts with an identifier, it is an identifier list.
3058 Otherwise, it is either a prototype list or an empty list. */
3059 if (id_list_ok
3060 && !attrs
3061 && c_parser_next_token_is (parser, CPP_NAME)
3062 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3064 /* Look ahead to detect typos in type names. */
3065 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3066 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3067 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3068 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3070 tree list = NULL_TREE, *nextp = &list;
3071 while (c_parser_next_token_is (parser, CPP_NAME)
3072 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3074 *nextp = build_tree_list (NULL_TREE,
3075 c_parser_peek_token (parser)->value);
3076 nextp = & TREE_CHAIN (*nextp);
3077 c_parser_consume_token (parser);
3078 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3079 break;
3080 c_parser_consume_token (parser);
3081 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3083 c_parser_error (parser, "expected identifier");
3084 break;
3087 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3089 struct c_arg_info *ret = build_arg_info ();
3090 ret->types = list;
3091 c_parser_consume_token (parser);
3092 pop_scope ();
3093 return ret;
3095 else
3097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3098 "expected %<)%>");
3099 pop_scope ();
3100 return NULL;
3103 else
3105 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3106 NULL);
3107 pop_scope ();
3108 return ret;
3112 /* Parse a parameter list (possibly empty), including the closing
3113 parenthesis but not the opening one. ATTRS are the attributes at
3114 the start of the list. EXPR is NULL or an expression that needs to
3115 be evaluated for the side effects of array size expressions in the
3116 parameters. */
3118 static struct c_arg_info *
3119 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3121 bool bad_parm = false;
3123 /* ??? Following the old parser, forward parameter declarations may
3124 use abstract declarators, and if no real parameter declarations
3125 follow the forward declarations then this is not diagnosed. Also
3126 note as above that attributes are ignored as the only contents of
3127 the parentheses, or as the only contents after forward
3128 declarations. */
3129 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3131 struct c_arg_info *ret = build_arg_info ();
3132 c_parser_consume_token (parser);
3133 return ret;
3135 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3137 struct c_arg_info *ret = build_arg_info ();
3138 /* Suppress -Wold-style-definition for this case. */
3139 ret->types = error_mark_node;
3140 error_at (c_parser_peek_token (parser)->location,
3141 "ISO C requires a named argument before %<...%>");
3142 c_parser_consume_token (parser);
3143 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3145 c_parser_consume_token (parser);
3146 return ret;
3148 else
3150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3151 "expected %<)%>");
3152 return NULL;
3155 /* Nonempty list of parameters, either terminated with semicolon
3156 (forward declarations; recurse) or with close parenthesis (normal
3157 function) or with ", ... )" (variadic function). */
3158 while (true)
3160 /* Parse a parameter. */
3161 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3162 attrs = NULL_TREE;
3163 if (parm == NULL)
3164 bad_parm = true;
3165 else
3166 push_parm_decl (parm, &expr);
3167 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3169 tree new_attrs;
3170 c_parser_consume_token (parser);
3171 mark_forward_parm_decls ();
3172 new_attrs = c_parser_attributes (parser);
3173 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3175 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3177 c_parser_consume_token (parser);
3178 if (bad_parm)
3179 return NULL;
3180 else
3181 return get_parm_info (false, expr);
3183 if (!c_parser_require (parser, CPP_COMMA,
3184 "expected %<;%>, %<,%> or %<)%>"))
3186 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3187 return NULL;
3189 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3191 c_parser_consume_token (parser);
3192 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3194 c_parser_consume_token (parser);
3195 if (bad_parm)
3196 return NULL;
3197 else
3198 return get_parm_info (true, expr);
3200 else
3202 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3203 "expected %<)%>");
3204 return NULL;
3210 /* Parse a parameter declaration. ATTRS are the attributes at the
3211 start of the declaration if it is the first parameter. */
3213 static struct c_parm *
3214 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3216 struct c_declspecs *specs;
3217 struct c_declarator *declarator;
3218 tree prefix_attrs;
3219 tree postfix_attrs = NULL_TREE;
3220 bool dummy = false;
3221 if (!c_parser_next_token_starts_declspecs (parser))
3223 c_token *token = c_parser_peek_token (parser);
3224 if (parser->error)
3225 return NULL;
3226 c_parser_set_source_position_from_token (token);
3227 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3229 error ("unknown type name %qE", token->value);
3230 parser->error = true;
3232 /* ??? In some Objective-C cases '...' isn't applicable so there
3233 should be a different message. */
3234 else
3235 c_parser_error (parser,
3236 "expected declaration specifiers or %<...%>");
3237 c_parser_skip_to_end_of_parameter (parser);
3238 return NULL;
3240 specs = build_null_declspecs ();
3241 if (attrs)
3243 declspecs_add_attrs (specs, attrs);
3244 attrs = NULL_TREE;
3246 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3247 finish_declspecs (specs);
3248 pending_xref_error ();
3249 prefix_attrs = specs->attrs;
3250 specs->attrs = NULL_TREE;
3251 declarator = c_parser_declarator (parser,
3252 specs->typespec_kind != ctsk_none,
3253 C_DTR_PARM, &dummy);
3254 if (declarator == NULL)
3256 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3257 return NULL;
3259 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3260 postfix_attrs = c_parser_attributes (parser);
3261 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3262 declarator);
3265 /* Parse a string literal in an asm expression. It should not be
3266 translated, and wide string literals are an error although
3267 permitted by the syntax. This is a GNU extension.
3269 asm-string-literal:
3270 string-literal
3272 ??? At present, following the old parser, the caller needs to have
3273 set lex_untranslated_string to 1. It would be better to follow the
3274 C++ parser rather than using this kludge. */
3276 static tree
3277 c_parser_asm_string_literal (c_parser *parser)
3279 tree str;
3280 int save_flag = warn_overlength_strings;
3281 warn_overlength_strings = 0;
3282 if (c_parser_next_token_is (parser, CPP_STRING))
3284 str = c_parser_peek_token (parser)->value;
3285 c_parser_consume_token (parser);
3287 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3289 error_at (c_parser_peek_token (parser)->location,
3290 "wide string literal in %<asm%>");
3291 str = build_string (1, "");
3292 c_parser_consume_token (parser);
3294 else
3296 c_parser_error (parser, "expected string literal");
3297 str = NULL_TREE;
3299 warn_overlength_strings = save_flag;
3300 return str;
3303 /* Parse a simple asm expression. This is used in restricted
3304 contexts, where a full expression with inputs and outputs does not
3305 make sense. This is a GNU extension.
3307 simple-asm-expr:
3308 asm ( asm-string-literal )
3311 static tree
3312 c_parser_simple_asm_expr (c_parser *parser)
3314 tree str;
3315 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3316 /* ??? Follow the C++ parser rather than using the
3317 lex_untranslated_string kludge. */
3318 parser->lex_untranslated_string = true;
3319 c_parser_consume_token (parser);
3320 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3322 parser->lex_untranslated_string = false;
3323 return NULL_TREE;
3325 str = c_parser_asm_string_literal (parser);
3326 parser->lex_untranslated_string = false;
3327 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3330 return NULL_TREE;
3332 return str;
3335 /* Parse (possibly empty) attributes. This is a GNU extension.
3337 attributes:
3338 empty
3339 attributes attribute
3341 attribute:
3342 __attribute__ ( ( attribute-list ) )
3344 attribute-list:
3345 attrib
3346 attribute_list , attrib
3348 attrib:
3349 empty
3350 any-word
3351 any-word ( identifier )
3352 any-word ( identifier , nonempty-expr-list )
3353 any-word ( expr-list )
3355 where the "identifier" must not be declared as a type, and
3356 "any-word" may be any identifier (including one declared as a
3357 type), a reserved word storage class specifier, type specifier or
3358 type qualifier. ??? This still leaves out most reserved keywords
3359 (following the old parser), shouldn't we include them, and why not
3360 allow identifiers declared as types to start the arguments? */
3362 static tree
3363 c_parser_attributes (c_parser *parser)
3365 tree attrs = NULL_TREE;
3366 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3368 /* ??? Follow the C++ parser rather than using the
3369 lex_untranslated_string kludge. */
3370 parser->lex_untranslated_string = true;
3371 c_parser_consume_token (parser);
3372 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3374 parser->lex_untranslated_string = false;
3375 return attrs;
3377 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3379 parser->lex_untranslated_string = false;
3380 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3381 return attrs;
3383 /* Parse the attribute list. */
3384 while (c_parser_next_token_is (parser, CPP_COMMA)
3385 || c_parser_next_token_is (parser, CPP_NAME)
3386 || c_parser_next_token_is (parser, CPP_KEYWORD))
3388 tree attr, attr_name, attr_args;
3389 VEC(tree,gc) *expr_list;
3390 if (c_parser_next_token_is (parser, CPP_COMMA))
3392 c_parser_consume_token (parser);
3393 continue;
3395 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3397 /* ??? See comment above about what keywords are
3398 accepted here. */
3399 bool ok;
3400 switch (c_parser_peek_token (parser)->keyword)
3402 case RID_STATIC:
3403 case RID_UNSIGNED:
3404 case RID_LONG:
3405 case RID_INT128:
3406 case RID_CONST:
3407 case RID_EXTERN:
3408 case RID_REGISTER:
3409 case RID_TYPEDEF:
3410 case RID_SHORT:
3411 case RID_INLINE:
3412 case RID_VOLATILE:
3413 case RID_SIGNED:
3414 case RID_AUTO:
3415 case RID_RESTRICT:
3416 case RID_COMPLEX:
3417 case RID_THREAD:
3418 case RID_INT:
3419 case RID_CHAR:
3420 case RID_FLOAT:
3421 case RID_DOUBLE:
3422 case RID_VOID:
3423 case RID_DFLOAT32:
3424 case RID_DFLOAT64:
3425 case RID_DFLOAT128:
3426 case RID_BOOL:
3427 case RID_FRACT:
3428 case RID_ACCUM:
3429 case RID_SAT:
3430 ok = true;
3431 break;
3432 default:
3433 ok = false;
3434 break;
3436 if (!ok)
3437 break;
3438 /* Accept __attribute__((__const)) as __attribute__((const))
3439 etc. */
3440 attr_name
3441 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3443 else
3444 attr_name = c_parser_peek_token (parser)->value;
3445 c_parser_consume_token (parser);
3446 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3448 attr = build_tree_list (attr_name, NULL_TREE);
3449 attrs = chainon (attrs, attr);
3450 continue;
3452 c_parser_consume_token (parser);
3453 /* Parse the attribute contents. If they start with an
3454 identifier which is followed by a comma or close
3455 parenthesis, then the arguments start with that
3456 identifier; otherwise they are an expression list.
3457 In objective-c the identifier may be a classname. */
3458 if (c_parser_next_token_is (parser, CPP_NAME)
3459 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3460 || (c_dialect_objc ()
3461 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3462 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3463 || (c_parser_peek_2nd_token (parser)->type
3464 == CPP_CLOSE_PAREN)))
3466 tree arg1 = c_parser_peek_token (parser)->value;
3467 c_parser_consume_token (parser);
3468 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3469 attr_args = build_tree_list (NULL_TREE, arg1);
3470 else
3472 tree tree_list;
3473 c_parser_consume_token (parser);
3474 expr_list = c_parser_expr_list (parser, false, true, NULL);
3475 tree_list = build_tree_list_vec (expr_list);
3476 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3477 release_tree_vector (expr_list);
3480 else
3482 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3483 attr_args = NULL_TREE;
3484 else
3486 expr_list = c_parser_expr_list (parser, false, true, NULL);
3487 attr_args = build_tree_list_vec (expr_list);
3488 release_tree_vector (expr_list);
3491 attr = build_tree_list (attr_name, attr_args);
3492 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3493 c_parser_consume_token (parser);
3494 else
3496 parser->lex_untranslated_string = false;
3497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3498 "expected %<)%>");
3499 return attrs;
3501 attrs = chainon (attrs, attr);
3503 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3504 c_parser_consume_token (parser);
3505 else
3507 parser->lex_untranslated_string = false;
3508 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3509 "expected %<)%>");
3510 return attrs;
3512 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3513 c_parser_consume_token (parser);
3514 else
3516 parser->lex_untranslated_string = false;
3517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3518 "expected %<)%>");
3519 return attrs;
3521 parser->lex_untranslated_string = false;
3523 return attrs;
3526 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3528 type-name:
3529 specifier-qualifier-list abstract-declarator[opt]
3532 static struct c_type_name *
3533 c_parser_type_name (c_parser *parser)
3535 struct c_declspecs *specs = build_null_declspecs ();
3536 struct c_declarator *declarator;
3537 struct c_type_name *ret;
3538 bool dummy = false;
3539 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3540 if (!specs->declspecs_seen_p)
3542 c_parser_error (parser, "expected specifier-qualifier-list");
3543 return NULL;
3545 if (specs->type != error_mark_node)
3547 pending_xref_error ();
3548 finish_declspecs (specs);
3550 declarator = c_parser_declarator (parser,
3551 specs->typespec_kind != ctsk_none,
3552 C_DTR_ABSTRACT, &dummy);
3553 if (declarator == NULL)
3554 return NULL;
3555 ret = XOBNEW (&parser_obstack, struct c_type_name);
3556 ret->specs = specs;
3557 ret->declarator = declarator;
3558 return ret;
3561 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3563 initializer:
3564 assignment-expression
3565 { initializer-list }
3566 { initializer-list , }
3568 initializer-list:
3569 designation[opt] initializer
3570 initializer-list , designation[opt] initializer
3572 designation:
3573 designator-list =
3575 designator-list:
3576 designator
3577 designator-list designator
3579 designator:
3580 array-designator
3581 . identifier
3583 array-designator:
3584 [ constant-expression ]
3586 GNU extensions:
3588 initializer:
3591 designation:
3592 array-designator
3593 identifier :
3595 array-designator:
3596 [ constant-expression ... constant-expression ]
3598 Any expression without commas is accepted in the syntax for the
3599 constant-expressions, with non-constant expressions rejected later.
3601 This function is only used for top-level initializers; for nested
3602 ones, see c_parser_initval. */
3604 static struct c_expr
3605 c_parser_initializer (c_parser *parser)
3607 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3608 return c_parser_braced_init (parser, NULL_TREE, false);
3609 else
3611 struct c_expr ret;
3612 location_t loc = c_parser_peek_token (parser)->location;
3613 ret = c_parser_expr_no_commas (parser, NULL);
3614 if (TREE_CODE (ret.value) != STRING_CST
3615 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3616 ret = default_function_array_read_conversion (loc, ret);
3617 return ret;
3621 /* Parse a braced initializer list. TYPE is the type specified for a
3622 compound literal, and NULL_TREE for other initializers and for
3623 nested braced lists. NESTED_P is true for nested braced lists,
3624 false for the list of a compound literal or the list that is the
3625 top-level initializer in a declaration. */
3627 static struct c_expr
3628 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3630 struct c_expr ret;
3631 struct obstack braced_init_obstack;
3632 location_t brace_loc = c_parser_peek_token (parser)->location;
3633 gcc_obstack_init (&braced_init_obstack);
3634 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3635 c_parser_consume_token (parser);
3636 if (nested_p)
3637 push_init_level (0, &braced_init_obstack);
3638 else
3639 really_start_incremental_init (type);
3640 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3642 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3644 else
3646 /* Parse a non-empty initializer list, possibly with a trailing
3647 comma. */
3648 while (true)
3650 c_parser_initelt (parser, &braced_init_obstack);
3651 if (parser->error)
3652 break;
3653 if (c_parser_next_token_is (parser, CPP_COMMA))
3654 c_parser_consume_token (parser);
3655 else
3656 break;
3657 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3658 break;
3661 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3663 ret.value = error_mark_node;
3664 ret.original_code = ERROR_MARK;
3665 ret.original_type = NULL;
3666 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3667 pop_init_level (0, &braced_init_obstack);
3668 obstack_free (&braced_init_obstack, NULL);
3669 return ret;
3671 c_parser_consume_token (parser);
3672 ret = pop_init_level (0, &braced_init_obstack);
3673 obstack_free (&braced_init_obstack, NULL);
3674 return ret;
3677 /* Parse a nested initializer, including designators. */
3679 static void
3680 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3682 /* Parse any designator or designator list. A single array
3683 designator may have the subsequent "=" omitted in GNU C, but a
3684 longer list or a structure member designator may not. */
3685 if (c_parser_next_token_is (parser, CPP_NAME)
3686 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3688 /* Old-style structure member designator. */
3689 set_init_label (c_parser_peek_token (parser)->value,
3690 braced_init_obstack);
3691 /* Use the colon as the error location. */
3692 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3693 "obsolete use of designated initializer with %<:%>");
3694 c_parser_consume_token (parser);
3695 c_parser_consume_token (parser);
3697 else
3699 /* des_seen is 0 if there have been no designators, 1 if there
3700 has been a single array designator and 2 otherwise. */
3701 int des_seen = 0;
3702 /* Location of a designator. */
3703 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3704 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3705 || c_parser_next_token_is (parser, CPP_DOT))
3707 int des_prev = des_seen;
3708 if (!des_seen)
3709 des_loc = c_parser_peek_token (parser)->location;
3710 if (des_seen < 2)
3711 des_seen++;
3712 if (c_parser_next_token_is (parser, CPP_DOT))
3714 des_seen = 2;
3715 c_parser_consume_token (parser);
3716 if (c_parser_next_token_is (parser, CPP_NAME))
3718 set_init_label (c_parser_peek_token (parser)->value,
3719 braced_init_obstack);
3720 c_parser_consume_token (parser);
3722 else
3724 struct c_expr init;
3725 init.value = error_mark_node;
3726 init.original_code = ERROR_MARK;
3727 init.original_type = NULL;
3728 c_parser_error (parser, "expected identifier");
3729 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3730 process_init_element (init, false, braced_init_obstack);
3731 return;
3734 else
3736 tree first, second;
3737 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3738 /* ??? Following the old parser, [ objc-receiver
3739 objc-message-args ] is accepted as an initializer,
3740 being distinguished from a designator by what follows
3741 the first assignment expression inside the square
3742 brackets, but after a first array designator a
3743 subsequent square bracket is for Objective-C taken to
3744 start an expression, using the obsolete form of
3745 designated initializer without '=', rather than
3746 possibly being a second level of designation: in LALR
3747 terms, the '[' is shifted rather than reducing
3748 designator to designator-list. */
3749 if (des_prev == 1 && c_dialect_objc ())
3751 des_seen = des_prev;
3752 break;
3754 if (des_prev == 0 && c_dialect_objc ())
3756 /* This might be an array designator or an
3757 Objective-C message expression. If the former,
3758 continue parsing here; if the latter, parse the
3759 remainder of the initializer given the starting
3760 primary-expression. ??? It might make sense to
3761 distinguish when des_prev == 1 as well; see
3762 previous comment. */
3763 tree rec, args;
3764 struct c_expr mexpr;
3765 c_parser_consume_token (parser);
3766 if (c_parser_peek_token (parser)->type == CPP_NAME
3767 && ((c_parser_peek_token (parser)->id_kind
3768 == C_ID_TYPENAME)
3769 || (c_parser_peek_token (parser)->id_kind
3770 == C_ID_CLASSNAME)))
3772 /* Type name receiver. */
3773 tree id = c_parser_peek_token (parser)->value;
3774 c_parser_consume_token (parser);
3775 rec = objc_get_class_reference (id);
3776 goto parse_message_args;
3778 first = c_parser_expr_no_commas (parser, NULL).value;
3779 mark_exp_read (first);
3780 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3781 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3782 goto array_desig_after_first;
3783 /* Expression receiver. So far only one part
3784 without commas has been parsed; there might be
3785 more of the expression. */
3786 rec = first;
3787 while (c_parser_next_token_is (parser, CPP_COMMA))
3789 struct c_expr next;
3790 location_t comma_loc, exp_loc;
3791 comma_loc = c_parser_peek_token (parser)->location;
3792 c_parser_consume_token (parser);
3793 exp_loc = c_parser_peek_token (parser)->location;
3794 next = c_parser_expr_no_commas (parser, NULL);
3795 next = default_function_array_read_conversion (exp_loc,
3796 next);
3797 rec = build_compound_expr (comma_loc, rec, next.value);
3799 parse_message_args:
3800 /* Now parse the objc-message-args. */
3801 args = c_parser_objc_message_args (parser);
3802 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3803 "expected %<]%>");
3804 mexpr.value
3805 = objc_build_message_expr (rec, args);
3806 mexpr.original_code = ERROR_MARK;
3807 mexpr.original_type = NULL;
3808 /* Now parse and process the remainder of the
3809 initializer, starting with this message
3810 expression as a primary-expression. */
3811 c_parser_initval (parser, &mexpr, braced_init_obstack);
3812 return;
3814 c_parser_consume_token (parser);
3815 first = c_parser_expr_no_commas (parser, NULL).value;
3816 mark_exp_read (first);
3817 array_desig_after_first:
3818 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3820 ellipsis_loc = c_parser_peek_token (parser)->location;
3821 c_parser_consume_token (parser);
3822 second = c_parser_expr_no_commas (parser, NULL).value;
3823 mark_exp_read (second);
3825 else
3826 second = NULL_TREE;
3827 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3829 c_parser_consume_token (parser);
3830 set_init_index (first, second, braced_init_obstack);
3831 if (second)
3832 pedwarn (ellipsis_loc, OPT_pedantic,
3833 "ISO C forbids specifying range of elements to initialize");
3835 else
3836 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3837 "expected %<]%>");
3840 if (des_seen >= 1)
3842 if (c_parser_next_token_is (parser, CPP_EQ))
3844 if (!flag_isoc99)
3845 pedwarn (des_loc, OPT_pedantic,
3846 "ISO C90 forbids specifying subobject to initialize");
3847 c_parser_consume_token (parser);
3849 else
3851 if (des_seen == 1)
3852 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3853 "obsolete use of designated initializer without %<=%>");
3854 else
3856 struct c_expr init;
3857 init.value = error_mark_node;
3858 init.original_code = ERROR_MARK;
3859 init.original_type = NULL;
3860 c_parser_error (parser, "expected %<=%>");
3861 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3862 process_init_element (init, false, braced_init_obstack);
3863 return;
3868 c_parser_initval (parser, NULL, braced_init_obstack);
3871 /* Parse a nested initializer; as c_parser_initializer but parses
3872 initializers within braced lists, after any designators have been
3873 applied. If AFTER is not NULL then it is an Objective-C message
3874 expression which is the primary-expression starting the
3875 initializer. */
3877 static void
3878 c_parser_initval (c_parser *parser, struct c_expr *after,
3879 struct obstack * braced_init_obstack)
3881 struct c_expr init;
3882 gcc_assert (!after || c_dialect_objc ());
3883 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3884 init = c_parser_braced_init (parser, NULL_TREE, true);
3885 else
3887 location_t loc = c_parser_peek_token (parser)->location;
3888 init = c_parser_expr_no_commas (parser, after);
3889 if (init.value != NULL_TREE
3890 && TREE_CODE (init.value) != STRING_CST
3891 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3892 init = default_function_array_read_conversion (loc, init);
3894 process_init_element (init, false, braced_init_obstack);
3897 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3898 C99 6.8.2).
3900 compound-statement:
3901 { block-item-list[opt] }
3902 { label-declarations block-item-list }
3904 block-item-list:
3905 block-item
3906 block-item-list block-item
3908 block-item:
3909 nested-declaration
3910 statement
3912 nested-declaration:
3913 declaration
3915 GNU extensions:
3917 compound-statement:
3918 { label-declarations block-item-list }
3920 nested-declaration:
3921 __extension__ nested-declaration
3922 nested-function-definition
3924 label-declarations:
3925 label-declaration
3926 label-declarations label-declaration
3928 label-declaration:
3929 __label__ identifier-list ;
3931 Allowing the mixing of declarations and code is new in C99. The
3932 GNU syntax also permits (not shown above) labels at the end of
3933 compound statements, which yield an error. We don't allow labels
3934 on declarations; this might seem like a natural extension, but
3935 there would be a conflict between attributes on the label and
3936 prefix attributes on the declaration. ??? The syntax follows the
3937 old parser in requiring something after label declarations.
3938 Although they are erroneous if the labels declared aren't defined,
3939 is it useful for the syntax to be this way?
3941 OpenMP:
3943 block-item:
3944 openmp-directive
3946 openmp-directive:
3947 barrier-directive
3948 flush-directive */
3950 static tree
3951 c_parser_compound_statement (c_parser *parser)
3953 tree stmt;
3954 location_t brace_loc;
3955 brace_loc = c_parser_peek_token (parser)->location;
3956 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3958 /* Ensure a scope is entered and left anyway to avoid confusion
3959 if we have just prepared to enter a function body. */
3960 stmt = c_begin_compound_stmt (true);
3961 c_end_compound_stmt (brace_loc, stmt, true);
3962 return error_mark_node;
3964 stmt = c_begin_compound_stmt (true);
3965 c_parser_compound_statement_nostart (parser);
3966 return c_end_compound_stmt (brace_loc, stmt, true);
3969 /* Parse a compound statement except for the opening brace. This is
3970 used for parsing both compound statements and statement expressions
3971 (which follow different paths to handling the opening). */
3973 static void
3974 c_parser_compound_statement_nostart (c_parser *parser)
3976 bool last_stmt = false;
3977 bool last_label = false;
3978 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3979 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3980 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3982 c_parser_consume_token (parser);
3983 return;
3985 mark_valid_location_for_stdc_pragma (true);
3986 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3988 /* Read zero or more forward-declarations for labels that nested
3989 functions can jump to. */
3990 mark_valid_location_for_stdc_pragma (false);
3991 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3993 label_loc = c_parser_peek_token (parser)->location;
3994 c_parser_consume_token (parser);
3995 /* Any identifiers, including those declared as type names,
3996 are OK here. */
3997 while (true)
3999 tree label;
4000 if (c_parser_next_token_is_not (parser, CPP_NAME))
4002 c_parser_error (parser, "expected identifier");
4003 break;
4005 label
4006 = declare_label (c_parser_peek_token (parser)->value);
4007 C_DECLARED_LABEL_FLAG (label) = 1;
4008 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4009 c_parser_consume_token (parser);
4010 if (c_parser_next_token_is (parser, CPP_COMMA))
4011 c_parser_consume_token (parser);
4012 else
4013 break;
4015 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4017 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
4019 /* We must now have at least one statement, label or declaration. */
4020 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4022 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4023 c_parser_error (parser, "expected declaration or statement");
4024 c_parser_consume_token (parser);
4025 return;
4027 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4029 location_t loc = c_parser_peek_token (parser)->location;
4030 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4031 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4032 || (c_parser_next_token_is (parser, CPP_NAME)
4033 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4035 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4036 label_loc = c_parser_peek_2nd_token (parser)->location;
4037 else
4038 label_loc = c_parser_peek_token (parser)->location;
4039 last_label = true;
4040 last_stmt = false;
4041 mark_valid_location_for_stdc_pragma (false);
4042 c_parser_label (parser);
4044 else if (!last_label
4045 && c_parser_next_tokens_start_declaration (parser))
4047 last_label = false;
4048 mark_valid_location_for_stdc_pragma (false);
4049 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4050 if (last_stmt)
4051 pedwarn_c90 (loc,
4052 (pedantic && !flag_isoc99)
4053 ? OPT_pedantic
4054 : OPT_Wdeclaration_after_statement,
4055 "ISO C90 forbids mixed declarations and code");
4056 last_stmt = false;
4058 else if (!last_label
4059 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4061 /* __extension__ can start a declaration, but is also an
4062 unary operator that can start an expression. Consume all
4063 but the last of a possible series of __extension__ to
4064 determine which. */
4065 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4066 && (c_parser_peek_2nd_token (parser)->keyword
4067 == RID_EXTENSION))
4068 c_parser_consume_token (parser);
4069 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4071 int ext;
4072 ext = disable_extension_diagnostics ();
4073 c_parser_consume_token (parser);
4074 last_label = false;
4075 mark_valid_location_for_stdc_pragma (false);
4076 c_parser_declaration_or_fndef (parser, true, true, true, true,
4077 true, NULL);
4078 /* Following the old parser, __extension__ does not
4079 disable this diagnostic. */
4080 restore_extension_diagnostics (ext);
4081 if (last_stmt)
4082 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4083 ? OPT_pedantic
4084 : OPT_Wdeclaration_after_statement,
4085 "ISO C90 forbids mixed declarations and code");
4086 last_stmt = false;
4088 else
4089 goto statement;
4091 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4093 /* External pragmas, and some omp pragmas, are not associated
4094 with regular c code, and so are not to be considered statements
4095 syntactically. This ensures that the user doesn't put them
4096 places that would turn into syntax errors if the directive
4097 were ignored. */
4098 if (c_parser_pragma (parser, pragma_compound))
4099 last_label = false, last_stmt = true;
4101 else if (c_parser_next_token_is (parser, CPP_EOF))
4103 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4104 c_parser_error (parser, "expected declaration or statement");
4105 return;
4107 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4109 if (parser->in_if_block)
4111 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4112 error_at (loc, """expected %<}%> before %<else%>");
4113 return;
4115 else
4117 error_at (loc, "%<else%> without a previous %<if%>");
4118 c_parser_consume_token (parser);
4119 continue;
4122 else
4124 statement:
4125 last_label = false;
4126 last_stmt = true;
4127 mark_valid_location_for_stdc_pragma (false);
4128 c_parser_statement_after_labels (parser);
4131 parser->error = false;
4133 if (last_label)
4134 error_at (label_loc, "label at end of compound statement");
4135 c_parser_consume_token (parser);
4136 /* Restore the value we started with. */
4137 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4140 /* Parse a label (C90 6.6.1, C99 6.8.1).
4142 label:
4143 identifier : attributes[opt]
4144 case constant-expression :
4145 default :
4147 GNU extensions:
4149 label:
4150 case constant-expression ... constant-expression :
4152 The use of attributes on labels is a GNU extension. The syntax in
4153 GNU C accepts any expressions without commas, non-constant
4154 expressions being rejected later. */
4156 static void
4157 c_parser_label (c_parser *parser)
4159 location_t loc1 = c_parser_peek_token (parser)->location;
4160 tree label = NULL_TREE;
4161 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4163 tree exp1, exp2;
4164 c_parser_consume_token (parser);
4165 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4166 if (c_parser_next_token_is (parser, CPP_COLON))
4168 c_parser_consume_token (parser);
4169 label = do_case (loc1, exp1, NULL_TREE);
4171 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4173 c_parser_consume_token (parser);
4174 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4175 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4176 label = do_case (loc1, exp1, exp2);
4178 else
4179 c_parser_error (parser, "expected %<:%> or %<...%>");
4181 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4183 c_parser_consume_token (parser);
4184 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4185 label = do_case (loc1, NULL_TREE, NULL_TREE);
4187 else
4189 tree name = c_parser_peek_token (parser)->value;
4190 tree tlab;
4191 tree attrs;
4192 location_t loc2 = c_parser_peek_token (parser)->location;
4193 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4194 c_parser_consume_token (parser);
4195 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4196 c_parser_consume_token (parser);
4197 attrs = c_parser_attributes (parser);
4198 tlab = define_label (loc2, name);
4199 if (tlab)
4201 decl_attributes (&tlab, attrs, 0);
4202 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4205 if (label)
4207 if (c_parser_next_tokens_start_declaration (parser))
4209 error_at (c_parser_peek_token (parser)->location,
4210 "a label can only be part of a statement and "
4211 "a declaration is not a statement");
4212 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4213 /*static_assert_ok*/ true,
4214 /*nested*/ true, /*empty_ok*/ false,
4215 /*start_attr_ok*/ true, NULL);
4220 /* Parse a statement (C90 6.6, C99 6.8).
4222 statement:
4223 labeled-statement
4224 compound-statement
4225 expression-statement
4226 selection-statement
4227 iteration-statement
4228 jump-statement
4230 labeled-statement:
4231 label statement
4233 expression-statement:
4234 expression[opt] ;
4236 selection-statement:
4237 if-statement
4238 switch-statement
4240 iteration-statement:
4241 while-statement
4242 do-statement
4243 for-statement
4245 jump-statement:
4246 goto identifier ;
4247 continue ;
4248 break ;
4249 return expression[opt] ;
4251 GNU extensions:
4253 statement:
4254 asm-statement
4256 jump-statement:
4257 goto * expression ;
4259 Objective-C:
4261 statement:
4262 objc-throw-statement
4263 objc-try-catch-statement
4264 objc-synchronized-statement
4266 objc-throw-statement:
4267 @throw expression ;
4268 @throw ;
4270 OpenMP:
4272 statement:
4273 openmp-construct
4275 openmp-construct:
4276 parallel-construct
4277 for-construct
4278 sections-construct
4279 single-construct
4280 parallel-for-construct
4281 parallel-sections-construct
4282 master-construct
4283 critical-construct
4284 atomic-construct
4285 ordered-construct
4287 parallel-construct:
4288 parallel-directive structured-block
4290 for-construct:
4291 for-directive iteration-statement
4293 sections-construct:
4294 sections-directive section-scope
4296 single-construct:
4297 single-directive structured-block
4299 parallel-for-construct:
4300 parallel-for-directive iteration-statement
4302 parallel-sections-construct:
4303 parallel-sections-directive section-scope
4305 master-construct:
4306 master-directive structured-block
4308 critical-construct:
4309 critical-directive structured-block
4311 atomic-construct:
4312 atomic-directive expression-statement
4314 ordered-construct:
4315 ordered-directive structured-block */
4317 static void
4318 c_parser_statement (c_parser *parser)
4320 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4321 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4322 || (c_parser_next_token_is (parser, CPP_NAME)
4323 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4324 c_parser_label (parser);
4325 c_parser_statement_after_labels (parser);
4328 /* Parse a statement, other than a labeled statement. */
4330 static void
4331 c_parser_statement_after_labels (c_parser *parser)
4333 location_t loc = c_parser_peek_token (parser)->location;
4334 tree stmt = NULL_TREE;
4335 bool in_if_block = parser->in_if_block;
4336 parser->in_if_block = false;
4337 switch (c_parser_peek_token (parser)->type)
4339 case CPP_OPEN_BRACE:
4340 add_stmt (c_parser_compound_statement (parser));
4341 break;
4342 case CPP_KEYWORD:
4343 switch (c_parser_peek_token (parser)->keyword)
4345 case RID_IF:
4346 c_parser_if_statement (parser);
4347 break;
4348 case RID_SWITCH:
4349 c_parser_switch_statement (parser);
4350 break;
4351 case RID_WHILE:
4352 c_parser_while_statement (parser);
4353 break;
4354 case RID_DO:
4355 c_parser_do_statement (parser);
4356 break;
4357 case RID_FOR:
4358 c_parser_for_statement (parser);
4359 break;
4360 case RID_GOTO:
4361 c_parser_consume_token (parser);
4362 if (c_parser_next_token_is (parser, CPP_NAME))
4364 stmt = c_finish_goto_label (loc,
4365 c_parser_peek_token (parser)->value);
4366 c_parser_consume_token (parser);
4368 else if (c_parser_next_token_is (parser, CPP_MULT))
4370 tree val;
4372 c_parser_consume_token (parser);
4373 val = c_parser_expression (parser).value;
4374 mark_exp_read (val);
4375 stmt = c_finish_goto_ptr (loc, val);
4377 else
4378 c_parser_error (parser, "expected identifier or %<*%>");
4379 goto expect_semicolon;
4380 case RID_CONTINUE:
4381 c_parser_consume_token (parser);
4382 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4383 goto expect_semicolon;
4384 case RID_BREAK:
4385 c_parser_consume_token (parser);
4386 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4387 goto expect_semicolon;
4388 case RID_RETURN:
4389 c_parser_consume_token (parser);
4390 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4392 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4393 c_parser_consume_token (parser);
4395 else
4397 struct c_expr expr = c_parser_expression_conv (parser);
4398 mark_exp_read (expr.value);
4399 stmt = c_finish_return (loc, expr.value, expr.original_type);
4400 goto expect_semicolon;
4402 break;
4403 case RID_ASM:
4404 stmt = c_parser_asm_statement (parser);
4405 break;
4406 case RID_AT_THROW:
4407 gcc_assert (c_dialect_objc ());
4408 c_parser_consume_token (parser);
4409 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4411 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4412 c_parser_consume_token (parser);
4414 else
4416 tree expr = c_parser_expression (parser).value;
4417 expr = c_fully_fold (expr, false, NULL);
4418 stmt = objc_build_throw_stmt (loc, expr);
4419 goto expect_semicolon;
4421 break;
4422 case RID_AT_TRY:
4423 gcc_assert (c_dialect_objc ());
4424 c_parser_objc_try_catch_finally_statement (parser);
4425 break;
4426 case RID_AT_SYNCHRONIZED:
4427 gcc_assert (c_dialect_objc ());
4428 c_parser_objc_synchronized_statement (parser);
4429 break;
4430 default:
4431 goto expr_stmt;
4433 break;
4434 case CPP_SEMICOLON:
4435 c_parser_consume_token (parser);
4436 break;
4437 case CPP_CLOSE_PAREN:
4438 case CPP_CLOSE_SQUARE:
4439 /* Avoid infinite loop in error recovery:
4440 c_parser_skip_until_found stops at a closing nesting
4441 delimiter without consuming it, but here we need to consume
4442 it to proceed further. */
4443 c_parser_error (parser, "expected statement");
4444 c_parser_consume_token (parser);
4445 break;
4446 case CPP_PRAGMA:
4447 c_parser_pragma (parser, pragma_stmt);
4448 break;
4449 default:
4450 expr_stmt:
4451 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4452 expect_semicolon:
4453 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4454 break;
4456 /* Two cases cannot and do not have line numbers associated: If stmt
4457 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4458 cannot hold line numbers. But that's OK because the statement
4459 will either be changed to a MODIFY_EXPR during gimplification of
4460 the statement expr, or discarded. If stmt was compound, but
4461 without new variables, we will have skipped the creation of a
4462 BIND and will have a bare STATEMENT_LIST. But that's OK because
4463 (recursively) all of the component statements should already have
4464 line numbers assigned. ??? Can we discard no-op statements
4465 earlier? */
4466 if (CAN_HAVE_LOCATION_P (stmt)
4467 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4468 SET_EXPR_LOCATION (stmt, loc);
4470 parser->in_if_block = in_if_block;
4473 /* Parse the condition from an if, do, while or for statements. */
4475 static tree
4476 c_parser_condition (c_parser *parser)
4478 location_t loc = c_parser_peek_token (parser)->location;
4479 tree cond;
4480 cond = c_parser_expression_conv (parser).value;
4481 cond = c_objc_common_truthvalue_conversion (loc, cond);
4482 cond = c_fully_fold (cond, false, NULL);
4483 if (warn_sequence_point)
4484 verify_sequence_points (cond);
4485 return cond;
4488 /* Parse a parenthesized condition from an if, do or while statement.
4490 condition:
4491 ( expression )
4493 static tree
4494 c_parser_paren_condition (c_parser *parser)
4496 tree cond;
4497 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4498 return error_mark_node;
4499 cond = c_parser_condition (parser);
4500 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4501 return cond;
4504 /* Parse a statement which is a block in C99. */
4506 static tree
4507 c_parser_c99_block_statement (c_parser *parser)
4509 tree block = c_begin_compound_stmt (flag_isoc99);
4510 location_t loc = c_parser_peek_token (parser)->location;
4511 c_parser_statement (parser);
4512 return c_end_compound_stmt (loc, block, flag_isoc99);
4515 /* Parse the body of an if statement. This is just parsing a
4516 statement but (a) it is a block in C99, (b) we track whether the
4517 body is an if statement for the sake of -Wparentheses warnings, (c)
4518 we handle an empty body specially for the sake of -Wempty-body
4519 warnings, and (d) we call parser_compound_statement directly
4520 because c_parser_statement_after_labels resets
4521 parser->in_if_block. */
4523 static tree
4524 c_parser_if_body (c_parser *parser, bool *if_p)
4526 tree block = c_begin_compound_stmt (flag_isoc99);
4527 location_t body_loc = c_parser_peek_token (parser)->location;
4528 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4529 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4530 || (c_parser_next_token_is (parser, CPP_NAME)
4531 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4532 c_parser_label (parser);
4533 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4534 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4536 location_t loc = c_parser_peek_token (parser)->location;
4537 add_stmt (build_empty_stmt (loc));
4538 c_parser_consume_token (parser);
4539 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4540 warning_at (loc, OPT_Wempty_body,
4541 "suggest braces around empty body in an %<if%> statement");
4543 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4544 add_stmt (c_parser_compound_statement (parser));
4545 else
4546 c_parser_statement_after_labels (parser);
4547 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4550 /* Parse the else body of an if statement. This is just parsing a
4551 statement but (a) it is a block in C99, (b) we handle an empty body
4552 specially for the sake of -Wempty-body warnings. */
4554 static tree
4555 c_parser_else_body (c_parser *parser)
4557 location_t else_loc = c_parser_peek_token (parser)->location;
4558 tree block = c_begin_compound_stmt (flag_isoc99);
4559 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4560 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4561 || (c_parser_next_token_is (parser, CPP_NAME)
4562 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4563 c_parser_label (parser);
4564 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4566 location_t loc = c_parser_peek_token (parser)->location;
4567 warning_at (loc,
4568 OPT_Wempty_body,
4569 "suggest braces around empty body in an %<else%> statement");
4570 add_stmt (build_empty_stmt (loc));
4571 c_parser_consume_token (parser);
4573 else
4574 c_parser_statement_after_labels (parser);
4575 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4578 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4580 if-statement:
4581 if ( expression ) statement
4582 if ( expression ) statement else statement
4585 static void
4586 c_parser_if_statement (c_parser *parser)
4588 tree block;
4589 location_t loc;
4590 tree cond;
4591 bool first_if = false;
4592 tree first_body, second_body;
4593 bool in_if_block;
4595 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4596 c_parser_consume_token (parser);
4597 block = c_begin_compound_stmt (flag_isoc99);
4598 loc = c_parser_peek_token (parser)->location;
4599 cond = c_parser_paren_condition (parser);
4600 in_if_block = parser->in_if_block;
4601 parser->in_if_block = true;
4602 first_body = c_parser_if_body (parser, &first_if);
4603 parser->in_if_block = in_if_block;
4604 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4606 c_parser_consume_token (parser);
4607 second_body = c_parser_else_body (parser);
4609 else
4610 second_body = NULL_TREE;
4611 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4612 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4615 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4617 switch-statement:
4618 switch (expression) statement
4621 static void
4622 c_parser_switch_statement (c_parser *parser)
4624 tree block, expr, body, save_break;
4625 location_t switch_loc = c_parser_peek_token (parser)->location;
4626 location_t switch_cond_loc;
4627 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4628 c_parser_consume_token (parser);
4629 block = c_begin_compound_stmt (flag_isoc99);
4630 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4632 switch_cond_loc = c_parser_peek_token (parser)->location;
4633 expr = c_parser_expression (parser).value;
4634 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4636 else
4638 switch_cond_loc = UNKNOWN_LOCATION;
4639 expr = error_mark_node;
4641 c_start_case (switch_loc, switch_cond_loc, expr);
4642 save_break = c_break_label;
4643 c_break_label = NULL_TREE;
4644 body = c_parser_c99_block_statement (parser);
4645 c_finish_case (body);
4646 if (c_break_label)
4648 location_t here = c_parser_peek_token (parser)->location;
4649 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4650 SET_EXPR_LOCATION (t, here);
4651 add_stmt (t);
4653 c_break_label = save_break;
4654 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4657 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4659 while-statement:
4660 while (expression) statement
4663 static void
4664 c_parser_while_statement (c_parser *parser)
4666 tree block, cond, body, save_break, save_cont;
4667 location_t loc;
4668 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4669 c_parser_consume_token (parser);
4670 block = c_begin_compound_stmt (flag_isoc99);
4671 loc = c_parser_peek_token (parser)->location;
4672 cond = c_parser_paren_condition (parser);
4673 save_break = c_break_label;
4674 c_break_label = NULL_TREE;
4675 save_cont = c_cont_label;
4676 c_cont_label = NULL_TREE;
4677 body = c_parser_c99_block_statement (parser);
4678 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4679 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4680 c_break_label = save_break;
4681 c_cont_label = save_cont;
4684 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4686 do-statement:
4687 do statement while ( expression ) ;
4690 static void
4691 c_parser_do_statement (c_parser *parser)
4693 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4694 location_t loc;
4695 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4696 c_parser_consume_token (parser);
4697 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4698 warning_at (c_parser_peek_token (parser)->location,
4699 OPT_Wempty_body,
4700 "suggest braces around empty body in %<do%> statement");
4701 block = c_begin_compound_stmt (flag_isoc99);
4702 loc = c_parser_peek_token (parser)->location;
4703 save_break = c_break_label;
4704 c_break_label = NULL_TREE;
4705 save_cont = c_cont_label;
4706 c_cont_label = NULL_TREE;
4707 body = c_parser_c99_block_statement (parser);
4708 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4709 new_break = c_break_label;
4710 c_break_label = save_break;
4711 new_cont = c_cont_label;
4712 c_cont_label = save_cont;
4713 cond = c_parser_paren_condition (parser);
4714 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4715 c_parser_skip_to_end_of_block_or_statement (parser);
4716 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4717 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4720 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4722 for-statement:
4723 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4724 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4726 The form with a declaration is new in C99.
4728 ??? In accordance with the old parser, the declaration may be a
4729 nested function, which is then rejected in check_for_loop_decls,
4730 but does it make any sense for this to be included in the grammar?
4731 Note in particular that the nested function does not include a
4732 trailing ';', whereas the "declaration" production includes one.
4733 Also, can we reject bad declarations earlier and cheaper than
4734 check_for_loop_decls?
4736 In Objective-C, there are two additional variants:
4738 foreach-statement:
4739 for ( expression in expresssion ) statement
4740 for ( declaration in expression ) statement
4742 This is inconsistent with C, because the second variant is allowed
4743 even if c99 is not enabled.
4745 The rest of the comment documents these Objective-C foreach-statement.
4747 Here is the canonical example of the first variant:
4748 for (object in array) { do something with object }
4749 we call the first expression ("object") the "object_expression" and
4750 the second expression ("array") the "collection_expression".
4751 object_expression must be an lvalue of type "id" (a generic Objective-C
4752 object) because the loop works by assigning to object_expression the
4753 various objects from the collection_expression. collection_expression
4754 must evaluate to something of type "id" which responds to the method
4755 countByEnumeratingWithState:objects:count:.
4757 The canonical example of the second variant is:
4758 for (id object in array) { do something with object }
4759 which is completely equivalent to
4761 id object;
4762 for (object in array) { do something with object }
4764 Note that initizializing 'object' in some way (eg, "for ((object =
4765 xxx) in array) { do something with object }") is possibly
4766 technically valid, but completely pointless as 'object' will be
4767 assigned to something else as soon as the loop starts. We should
4768 most likely reject it (TODO).
4770 The beginning of the Objective-C foreach-statement looks exactly
4771 like the beginning of the for-statement, and we can tell it is a
4772 foreach-statement only because the initial declaration or
4773 expression is terminated by 'in' instead of ';'.
4776 static void
4777 c_parser_for_statement (c_parser *parser)
4779 tree block, cond, incr, save_break, save_cont, body;
4780 /* The following are only used when parsing an ObjC foreach statement. */
4781 tree object_expression;
4782 /* Silence the bogus uninitialized warning. */
4783 tree collection_expression = NULL;
4784 location_t loc = c_parser_peek_token (parser)->location;
4785 location_t for_loc = c_parser_peek_token (parser)->location;
4786 bool is_foreach_statement = false;
4787 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4788 c_parser_consume_token (parser);
4789 /* Open a compound statement in Objective-C as well, just in case this is
4790 as foreach expression. */
4791 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4792 cond = error_mark_node;
4793 incr = error_mark_node;
4794 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4796 /* Parse the initialization declaration or expression. */
4797 object_expression = error_mark_node;
4798 parser->objc_could_be_foreach_context = c_dialect_objc ();
4799 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4801 parser->objc_could_be_foreach_context = false;
4802 c_parser_consume_token (parser);
4803 c_finish_expr_stmt (loc, NULL_TREE);
4805 else if (c_parser_next_tokens_start_declaration (parser))
4807 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4808 &object_expression);
4809 parser->objc_could_be_foreach_context = false;
4811 if (c_parser_next_token_is_keyword (parser, RID_IN))
4813 c_parser_consume_token (parser);
4814 is_foreach_statement = true;
4815 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4816 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4818 else
4819 check_for_loop_decls (for_loc, flag_isoc99);
4821 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4823 /* __extension__ can start a declaration, but is also an
4824 unary operator that can start an expression. Consume all
4825 but the last of a possible series of __extension__ to
4826 determine which. */
4827 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4828 && (c_parser_peek_2nd_token (parser)->keyword
4829 == RID_EXTENSION))
4830 c_parser_consume_token (parser);
4831 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4833 int ext;
4834 ext = disable_extension_diagnostics ();
4835 c_parser_consume_token (parser);
4836 c_parser_declaration_or_fndef (parser, true, true, true, true,
4837 true, &object_expression);
4838 parser->objc_could_be_foreach_context = false;
4840 restore_extension_diagnostics (ext);
4841 if (c_parser_next_token_is_keyword (parser, RID_IN))
4843 c_parser_consume_token (parser);
4844 is_foreach_statement = true;
4845 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4846 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4848 else
4849 check_for_loop_decls (for_loc, flag_isoc99);
4851 else
4852 goto init_expr;
4854 else
4856 init_expr:
4858 tree init_expression;
4859 init_expression = c_parser_expression (parser).value;
4860 parser->objc_could_be_foreach_context = false;
4861 if (c_parser_next_token_is_keyword (parser, RID_IN))
4863 c_parser_consume_token (parser);
4864 is_foreach_statement = true;
4865 if (! lvalue_p (init_expression))
4866 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4867 object_expression = c_fully_fold (init_expression, false, NULL);
4869 else
4871 c_finish_expr_stmt (loc, init_expression);
4872 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4876 /* Parse the loop condition. In the case of a foreach
4877 statement, there is no loop condition. */
4878 gcc_assert (!parser->objc_could_be_foreach_context);
4879 if (!is_foreach_statement)
4881 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4883 c_parser_consume_token (parser);
4884 cond = NULL_TREE;
4886 else
4888 cond = c_parser_condition (parser);
4889 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4892 /* Parse the increment expression (the third expression in a
4893 for-statement). In the case of a foreach-statement, this is
4894 the expression that follows the 'in'. */
4895 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4897 if (is_foreach_statement)
4899 c_parser_error (parser, "missing collection in fast enumeration");
4900 collection_expression = error_mark_node;
4902 else
4903 incr = c_process_expr_stmt (loc, NULL_TREE);
4905 else
4907 if (is_foreach_statement)
4908 collection_expression = c_fully_fold (c_parser_expression (parser).value,
4909 false, NULL);
4910 else
4911 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4913 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4915 save_break = c_break_label;
4916 c_break_label = NULL_TREE;
4917 save_cont = c_cont_label;
4918 c_cont_label = NULL_TREE;
4919 body = c_parser_c99_block_statement (parser);
4920 if (is_foreach_statement)
4921 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4922 else
4923 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4924 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4925 c_break_label = save_break;
4926 c_cont_label = save_cont;
4929 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4930 statement with inputs, outputs, clobbers, and volatile tag
4931 allowed.
4933 asm-statement:
4934 asm type-qualifier[opt] ( asm-argument ) ;
4935 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4937 asm-argument:
4938 asm-string-literal
4939 asm-string-literal : asm-operands[opt]
4940 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4941 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4943 asm-goto-argument:
4944 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4945 : asm-goto-operands
4947 Qualifiers other than volatile are accepted in the syntax but
4948 warned for. */
4950 static tree
4951 c_parser_asm_statement (c_parser *parser)
4953 tree quals, str, outputs, inputs, clobbers, labels, ret;
4954 bool simple, is_goto;
4955 location_t asm_loc = c_parser_peek_token (parser)->location;
4956 int section, nsections;
4958 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4959 c_parser_consume_token (parser);
4960 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4962 quals = c_parser_peek_token (parser)->value;
4963 c_parser_consume_token (parser);
4965 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4966 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4968 warning_at (c_parser_peek_token (parser)->location,
4970 "%E qualifier ignored on asm",
4971 c_parser_peek_token (parser)->value);
4972 quals = NULL_TREE;
4973 c_parser_consume_token (parser);
4975 else
4976 quals = NULL_TREE;
4978 is_goto = false;
4979 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4981 c_parser_consume_token (parser);
4982 is_goto = true;
4985 /* ??? Follow the C++ parser rather than using the
4986 lex_untranslated_string kludge. */
4987 parser->lex_untranslated_string = true;
4988 ret = NULL;
4990 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4991 goto error;
4993 str = c_parser_asm_string_literal (parser);
4994 if (str == NULL_TREE)
4995 goto error_close_paren;
4997 simple = true;
4998 outputs = NULL_TREE;
4999 inputs = NULL_TREE;
5000 clobbers = NULL_TREE;
5001 labels = NULL_TREE;
5003 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5004 goto done_asm;
5006 /* Parse each colon-delimited section of operands. */
5007 nsections = 3 + is_goto;
5008 for (section = 0; section < nsections; ++section)
5010 if (!c_parser_require (parser, CPP_COLON,
5011 is_goto
5012 ? "expected %<:%>"
5013 : "expected %<:%> or %<)%>"))
5014 goto error_close_paren;
5016 /* Once past any colon, we're no longer a simple asm. */
5017 simple = false;
5019 if ((!c_parser_next_token_is (parser, CPP_COLON)
5020 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5021 || section == 3)
5022 switch (section)
5024 case 0:
5025 /* For asm goto, we don't allow output operands, but reserve
5026 the slot for a future extension that does allow them. */
5027 if (!is_goto)
5028 outputs = c_parser_asm_operands (parser, false);
5029 break;
5030 case 1:
5031 inputs = c_parser_asm_operands (parser, true);
5032 break;
5033 case 2:
5034 clobbers = c_parser_asm_clobbers (parser);
5035 break;
5036 case 3:
5037 labels = c_parser_asm_goto_operands (parser);
5038 break;
5039 default:
5040 gcc_unreachable ();
5043 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5044 goto done_asm;
5047 done_asm:
5048 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5050 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5051 goto error;
5054 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5055 c_parser_skip_to_end_of_block_or_statement (parser);
5057 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5058 clobbers, labels, simple));
5060 error:
5061 parser->lex_untranslated_string = false;
5062 return ret;
5064 error_close_paren:
5065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5066 goto error;
5069 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5070 not outputs), apply the default conversion of functions and arrays
5071 to pointers.
5073 asm-operands:
5074 asm-operand
5075 asm-operands , asm-operand
5077 asm-operand:
5078 asm-string-literal ( expression )
5079 [ identifier ] asm-string-literal ( expression )
5082 static tree
5083 c_parser_asm_operands (c_parser *parser, bool convert_p)
5085 tree list = NULL_TREE;
5086 location_t loc;
5087 while (true)
5089 tree name, str;
5090 struct c_expr expr;
5091 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5093 c_parser_consume_token (parser);
5094 if (c_parser_next_token_is (parser, CPP_NAME))
5096 tree id = c_parser_peek_token (parser)->value;
5097 c_parser_consume_token (parser);
5098 name = build_string (IDENTIFIER_LENGTH (id),
5099 IDENTIFIER_POINTER (id));
5101 else
5103 c_parser_error (parser, "expected identifier");
5104 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5105 return NULL_TREE;
5107 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5108 "expected %<]%>");
5110 else
5111 name = NULL_TREE;
5112 str = c_parser_asm_string_literal (parser);
5113 if (str == NULL_TREE)
5114 return NULL_TREE;
5115 parser->lex_untranslated_string = false;
5116 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5118 parser->lex_untranslated_string = true;
5119 return NULL_TREE;
5121 loc = c_parser_peek_token (parser)->location;
5122 expr = c_parser_expression (parser);
5123 mark_exp_read (expr.value);
5124 if (convert_p)
5125 expr = default_function_array_conversion (loc, expr);
5126 expr.value = c_fully_fold (expr.value, false, NULL);
5127 parser->lex_untranslated_string = true;
5128 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5131 return NULL_TREE;
5133 list = chainon (list, build_tree_list (build_tree_list (name, str),
5134 expr.value));
5135 if (c_parser_next_token_is (parser, CPP_COMMA))
5136 c_parser_consume_token (parser);
5137 else
5138 break;
5140 return list;
5143 /* Parse asm clobbers, a GNU extension.
5145 asm-clobbers:
5146 asm-string-literal
5147 asm-clobbers , asm-string-literal
5150 static tree
5151 c_parser_asm_clobbers (c_parser *parser)
5153 tree list = NULL_TREE;
5154 while (true)
5156 tree str = c_parser_asm_string_literal (parser);
5157 if (str)
5158 list = tree_cons (NULL_TREE, str, list);
5159 else
5160 return NULL_TREE;
5161 if (c_parser_next_token_is (parser, CPP_COMMA))
5162 c_parser_consume_token (parser);
5163 else
5164 break;
5166 return list;
5169 /* Parse asm goto labels, a GNU extension.
5171 asm-goto-operands:
5172 identifier
5173 asm-goto-operands , identifier
5176 static tree
5177 c_parser_asm_goto_operands (c_parser *parser)
5179 tree list = NULL_TREE;
5180 while (true)
5182 tree name, label;
5184 if (c_parser_next_token_is (parser, CPP_NAME))
5186 c_token *tok = c_parser_peek_token (parser);
5187 name = tok->value;
5188 label = lookup_label_for_goto (tok->location, name);
5189 c_parser_consume_token (parser);
5190 TREE_USED (label) = 1;
5192 else
5194 c_parser_error (parser, "expected identifier");
5195 return NULL_TREE;
5198 name = build_string (IDENTIFIER_LENGTH (name),
5199 IDENTIFIER_POINTER (name));
5200 list = tree_cons (name, label, list);
5201 if (c_parser_next_token_is (parser, CPP_COMMA))
5202 c_parser_consume_token (parser);
5203 else
5204 return nreverse (list);
5208 /* Parse an expression other than a compound expression; that is, an
5209 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5210 NULL then it is an Objective-C message expression which is the
5211 primary-expression starting the expression as an initializer.
5213 assignment-expression:
5214 conditional-expression
5215 unary-expression assignment-operator assignment-expression
5217 assignment-operator: one of
5218 = *= /= %= += -= <<= >>= &= ^= |=
5220 In GNU C we accept any conditional expression on the LHS and
5221 diagnose the invalid lvalue rather than producing a syntax
5222 error. */
5224 static struct c_expr
5225 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5227 struct c_expr lhs, rhs, ret;
5228 enum tree_code code;
5229 location_t op_location, exp_location;
5230 gcc_assert (!after || c_dialect_objc ());
5231 lhs = c_parser_conditional_expression (parser, after);
5232 op_location = c_parser_peek_token (parser)->location;
5233 switch (c_parser_peek_token (parser)->type)
5235 case CPP_EQ:
5236 code = NOP_EXPR;
5237 break;
5238 case CPP_MULT_EQ:
5239 code = MULT_EXPR;
5240 break;
5241 case CPP_DIV_EQ:
5242 code = TRUNC_DIV_EXPR;
5243 break;
5244 case CPP_MOD_EQ:
5245 code = TRUNC_MOD_EXPR;
5246 break;
5247 case CPP_PLUS_EQ:
5248 code = PLUS_EXPR;
5249 break;
5250 case CPP_MINUS_EQ:
5251 code = MINUS_EXPR;
5252 break;
5253 case CPP_LSHIFT_EQ:
5254 code = LSHIFT_EXPR;
5255 break;
5256 case CPP_RSHIFT_EQ:
5257 code = RSHIFT_EXPR;
5258 break;
5259 case CPP_AND_EQ:
5260 code = BIT_AND_EXPR;
5261 break;
5262 case CPP_XOR_EQ:
5263 code = BIT_XOR_EXPR;
5264 break;
5265 case CPP_OR_EQ:
5266 code = BIT_IOR_EXPR;
5267 break;
5268 default:
5269 return lhs;
5271 c_parser_consume_token (parser);
5272 exp_location = c_parser_peek_token (parser)->location;
5273 rhs = c_parser_expr_no_commas (parser, NULL);
5274 rhs = default_function_array_read_conversion (exp_location, rhs);
5275 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5276 code, exp_location, rhs.value,
5277 rhs.original_type);
5278 if (code == NOP_EXPR)
5279 ret.original_code = MODIFY_EXPR;
5280 else
5282 TREE_NO_WARNING (ret.value) = 1;
5283 ret.original_code = ERROR_MARK;
5285 ret.original_type = NULL;
5286 return ret;
5289 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5290 is not NULL then it is an Objective-C message expression which is
5291 the primary-expression starting the expression as an initializer.
5293 conditional-expression:
5294 logical-OR-expression
5295 logical-OR-expression ? expression : conditional-expression
5297 GNU extensions:
5299 conditional-expression:
5300 logical-OR-expression ? : conditional-expression
5303 static struct c_expr
5304 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5306 struct c_expr cond, exp1, exp2, ret;
5307 location_t cond_loc, colon_loc, middle_loc;
5309 gcc_assert (!after || c_dialect_objc ());
5311 cond = c_parser_binary_expression (parser, after);
5313 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5314 return cond;
5315 cond_loc = c_parser_peek_token (parser)->location;
5316 cond = default_function_array_read_conversion (cond_loc, cond);
5317 c_parser_consume_token (parser);
5318 if (c_parser_next_token_is (parser, CPP_COLON))
5320 tree eptype = NULL_TREE;
5322 middle_loc = c_parser_peek_token (parser)->location;
5323 pedwarn (middle_loc, OPT_pedantic,
5324 "ISO C forbids omitting the middle term of a ?: expression");
5325 warn_for_omitted_condop (middle_loc, cond.value);
5326 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5328 eptype = TREE_TYPE (cond.value);
5329 cond.value = TREE_OPERAND (cond.value, 0);
5331 /* Make sure first operand is calculated only once. */
5332 exp1.value = c_save_expr (default_conversion (cond.value));
5333 if (eptype)
5334 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5335 exp1.original_type = NULL;
5336 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5337 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5339 else
5341 cond.value
5342 = c_objc_common_truthvalue_conversion
5343 (cond_loc, default_conversion (cond.value));
5344 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5345 exp1 = c_parser_expression_conv (parser);
5346 mark_exp_read (exp1.value);
5347 c_inhibit_evaluation_warnings +=
5348 ((cond.value == truthvalue_true_node)
5349 - (cond.value == truthvalue_false_node));
5352 colon_loc = c_parser_peek_token (parser)->location;
5353 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5355 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5356 ret.value = error_mark_node;
5357 ret.original_code = ERROR_MARK;
5358 ret.original_type = NULL;
5359 return ret;
5362 location_t exp2_loc = c_parser_peek_token (parser)->location;
5363 exp2 = c_parser_conditional_expression (parser, NULL);
5364 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5366 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5367 ret.value = build_conditional_expr (colon_loc, cond.value,
5368 cond.original_code == C_MAYBE_CONST_EXPR,
5369 exp1.value, exp1.original_type,
5370 exp2.value, exp2.original_type);
5371 ret.original_code = ERROR_MARK;
5372 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5373 ret.original_type = NULL;
5374 else
5376 tree t1, t2;
5378 /* If both sides are enum type, the default conversion will have
5379 made the type of the result be an integer type. We want to
5380 remember the enum types we started with. */
5381 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5382 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5383 ret.original_type = ((t1 != error_mark_node
5384 && t2 != error_mark_node
5385 && (TYPE_MAIN_VARIANT (t1)
5386 == TYPE_MAIN_VARIANT (t2)))
5387 ? t1
5388 : NULL);
5390 return ret;
5393 /* Parse a binary expression; that is, a logical-OR-expression (C90
5394 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5395 an Objective-C message expression which is the primary-expression
5396 starting the expression as an initializer.
5398 multiplicative-expression:
5399 cast-expression
5400 multiplicative-expression * cast-expression
5401 multiplicative-expression / cast-expression
5402 multiplicative-expression % cast-expression
5404 additive-expression:
5405 multiplicative-expression
5406 additive-expression + multiplicative-expression
5407 additive-expression - multiplicative-expression
5409 shift-expression:
5410 additive-expression
5411 shift-expression << additive-expression
5412 shift-expression >> additive-expression
5414 relational-expression:
5415 shift-expression
5416 relational-expression < shift-expression
5417 relational-expression > shift-expression
5418 relational-expression <= shift-expression
5419 relational-expression >= shift-expression
5421 equality-expression:
5422 relational-expression
5423 equality-expression == relational-expression
5424 equality-expression != relational-expression
5426 AND-expression:
5427 equality-expression
5428 AND-expression & equality-expression
5430 exclusive-OR-expression:
5431 AND-expression
5432 exclusive-OR-expression ^ AND-expression
5434 inclusive-OR-expression:
5435 exclusive-OR-expression
5436 inclusive-OR-expression | exclusive-OR-expression
5438 logical-AND-expression:
5439 inclusive-OR-expression
5440 logical-AND-expression && inclusive-OR-expression
5442 logical-OR-expression:
5443 logical-AND-expression
5444 logical-OR-expression || logical-AND-expression
5447 static struct c_expr
5448 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5450 /* A binary expression is parsed using operator-precedence parsing,
5451 with the operands being cast expressions. All the binary
5452 operators are left-associative. Thus a binary expression is of
5453 form:
5455 E0 op1 E1 op2 E2 ...
5457 which we represent on a stack. On the stack, the precedence
5458 levels are strictly increasing. When a new operator is
5459 encountered of higher precedence than that at the top of the
5460 stack, it is pushed; its LHS is the top expression, and its RHS
5461 is everything parsed until it is popped. When a new operator is
5462 encountered with precedence less than or equal to that at the top
5463 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5464 by the result of the operation until the operator at the top of
5465 the stack has lower precedence than the new operator or there is
5466 only one element on the stack; then the top expression is the LHS
5467 of the new operator. In the case of logical AND and OR
5468 expressions, we also need to adjust c_inhibit_evaluation_warnings
5469 as appropriate when the operators are pushed and popped. */
5471 /* The precedence levels, where 0 is a dummy lowest level used for
5472 the bottom of the stack. */
5473 enum prec {
5474 PREC_NONE,
5475 PREC_LOGOR,
5476 PREC_LOGAND,
5477 PREC_BITOR,
5478 PREC_BITXOR,
5479 PREC_BITAND,
5480 PREC_EQ,
5481 PREC_REL,
5482 PREC_SHIFT,
5483 PREC_ADD,
5484 PREC_MULT,
5485 NUM_PRECS
5487 struct {
5488 /* The expression at this stack level. */
5489 struct c_expr expr;
5490 /* The precedence of the operator on its left, PREC_NONE at the
5491 bottom of the stack. */
5492 enum prec prec;
5493 /* The operation on its left. */
5494 enum tree_code op;
5495 /* The source location of this operation. */
5496 location_t loc;
5497 } stack[NUM_PRECS];
5498 int sp;
5499 /* Location of the binary operator. */
5500 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5501 #define POP \
5502 do { \
5503 switch (stack[sp].op) \
5505 case TRUTH_ANDIF_EXPR: \
5506 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5507 == truthvalue_false_node); \
5508 break; \
5509 case TRUTH_ORIF_EXPR: \
5510 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5511 == truthvalue_true_node); \
5512 break; \
5513 default: \
5514 break; \
5516 stack[sp - 1].expr \
5517 = default_function_array_read_conversion (stack[sp - 1].loc, \
5518 stack[sp - 1].expr); \
5519 stack[sp].expr \
5520 = default_function_array_read_conversion (stack[sp].loc, \
5521 stack[sp].expr); \
5522 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5523 stack[sp].op, \
5524 stack[sp - 1].expr, \
5525 stack[sp].expr); \
5526 sp--; \
5527 } while (0)
5528 gcc_assert (!after || c_dialect_objc ());
5529 stack[0].loc = c_parser_peek_token (parser)->location;
5530 stack[0].expr = c_parser_cast_expression (parser, after);
5531 stack[0].prec = PREC_NONE;
5532 sp = 0;
5533 while (true)
5535 enum prec oprec;
5536 enum tree_code ocode;
5537 if (parser->error)
5538 goto out;
5539 switch (c_parser_peek_token (parser)->type)
5541 case CPP_MULT:
5542 oprec = PREC_MULT;
5543 ocode = MULT_EXPR;
5544 break;
5545 case CPP_DIV:
5546 oprec = PREC_MULT;
5547 ocode = TRUNC_DIV_EXPR;
5548 break;
5549 case CPP_MOD:
5550 oprec = PREC_MULT;
5551 ocode = TRUNC_MOD_EXPR;
5552 break;
5553 case CPP_PLUS:
5554 oprec = PREC_ADD;
5555 ocode = PLUS_EXPR;
5556 break;
5557 case CPP_MINUS:
5558 oprec = PREC_ADD;
5559 ocode = MINUS_EXPR;
5560 break;
5561 case CPP_LSHIFT:
5562 oprec = PREC_SHIFT;
5563 ocode = LSHIFT_EXPR;
5564 break;
5565 case CPP_RSHIFT:
5566 oprec = PREC_SHIFT;
5567 ocode = RSHIFT_EXPR;
5568 break;
5569 case CPP_LESS:
5570 oprec = PREC_REL;
5571 ocode = LT_EXPR;
5572 break;
5573 case CPP_GREATER:
5574 oprec = PREC_REL;
5575 ocode = GT_EXPR;
5576 break;
5577 case CPP_LESS_EQ:
5578 oprec = PREC_REL;
5579 ocode = LE_EXPR;
5580 break;
5581 case CPP_GREATER_EQ:
5582 oprec = PREC_REL;
5583 ocode = GE_EXPR;
5584 break;
5585 case CPP_EQ_EQ:
5586 oprec = PREC_EQ;
5587 ocode = EQ_EXPR;
5588 break;
5589 case CPP_NOT_EQ:
5590 oprec = PREC_EQ;
5591 ocode = NE_EXPR;
5592 break;
5593 case CPP_AND:
5594 oprec = PREC_BITAND;
5595 ocode = BIT_AND_EXPR;
5596 break;
5597 case CPP_XOR:
5598 oprec = PREC_BITXOR;
5599 ocode = BIT_XOR_EXPR;
5600 break;
5601 case CPP_OR:
5602 oprec = PREC_BITOR;
5603 ocode = BIT_IOR_EXPR;
5604 break;
5605 case CPP_AND_AND:
5606 oprec = PREC_LOGAND;
5607 ocode = TRUTH_ANDIF_EXPR;
5608 break;
5609 case CPP_OR_OR:
5610 oprec = PREC_LOGOR;
5611 ocode = TRUTH_ORIF_EXPR;
5612 break;
5613 default:
5614 /* Not a binary operator, so end of the binary
5615 expression. */
5616 goto out;
5618 binary_loc = c_parser_peek_token (parser)->location;
5619 c_parser_consume_token (parser);
5620 while (oprec <= stack[sp].prec)
5621 POP;
5622 switch (ocode)
5624 case TRUTH_ANDIF_EXPR:
5625 stack[sp].expr
5626 = default_function_array_read_conversion (stack[sp].loc,
5627 stack[sp].expr);
5628 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5629 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5630 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5631 == truthvalue_false_node);
5632 break;
5633 case TRUTH_ORIF_EXPR:
5634 stack[sp].expr
5635 = default_function_array_read_conversion (stack[sp].loc,
5636 stack[sp].expr);
5637 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5638 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5639 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5640 == truthvalue_true_node);
5641 break;
5642 default:
5643 break;
5645 sp++;
5646 stack[sp].loc = binary_loc;
5647 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5648 stack[sp].prec = oprec;
5649 stack[sp].op = ocode;
5650 stack[sp].loc = binary_loc;
5652 out:
5653 while (sp > 0)
5654 POP;
5655 return stack[0].expr;
5656 #undef POP
5659 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5660 NULL then it is an Objective-C message expression which is the
5661 primary-expression starting the expression as an initializer.
5663 cast-expression:
5664 unary-expression
5665 ( type-name ) unary-expression
5668 static struct c_expr
5669 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5671 location_t cast_loc = c_parser_peek_token (parser)->location;
5672 gcc_assert (!after || c_dialect_objc ());
5673 if (after)
5674 return c_parser_postfix_expression_after_primary (parser,
5675 cast_loc, *after);
5676 /* If the expression begins with a parenthesized type name, it may
5677 be either a cast or a compound literal; we need to see whether
5678 the next character is '{' to tell the difference. If not, it is
5679 an unary expression. Full detection of unknown typenames here
5680 would require a 3-token lookahead. */
5681 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5682 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5684 struct c_type_name *type_name;
5685 struct c_expr ret;
5686 struct c_expr expr;
5687 c_parser_consume_token (parser);
5688 type_name = c_parser_type_name (parser);
5689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5690 if (type_name == NULL)
5692 ret.value = error_mark_node;
5693 ret.original_code = ERROR_MARK;
5694 ret.original_type = NULL;
5695 return ret;
5698 /* Save casted types in the function's used types hash table. */
5699 used_types_insert (type_name->specs->type);
5701 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5702 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5703 cast_loc);
5705 location_t expr_loc = c_parser_peek_token (parser)->location;
5706 expr = c_parser_cast_expression (parser, NULL);
5707 expr = default_function_array_read_conversion (expr_loc, expr);
5709 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5710 ret.original_code = ERROR_MARK;
5711 ret.original_type = NULL;
5712 return ret;
5714 else
5715 return c_parser_unary_expression (parser);
5718 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5720 unary-expression:
5721 postfix-expression
5722 ++ unary-expression
5723 -- unary-expression
5724 unary-operator cast-expression
5725 sizeof unary-expression
5726 sizeof ( type-name )
5728 unary-operator: one of
5729 & * + - ~ !
5731 GNU extensions:
5733 unary-expression:
5734 __alignof__ unary-expression
5735 __alignof__ ( type-name )
5736 && identifier
5738 unary-operator: one of
5739 __extension__ __real__ __imag__
5741 In addition, the GNU syntax treats ++ and -- as unary operators, so
5742 they may be applied to cast expressions with errors for non-lvalues
5743 given later. */
5745 static struct c_expr
5746 c_parser_unary_expression (c_parser *parser)
5748 int ext;
5749 struct c_expr ret, op;
5750 location_t op_loc = c_parser_peek_token (parser)->location;
5751 location_t exp_loc;
5752 ret.original_code = ERROR_MARK;
5753 ret.original_type = NULL;
5754 switch (c_parser_peek_token (parser)->type)
5756 case CPP_PLUS_PLUS:
5757 c_parser_consume_token (parser);
5758 exp_loc = c_parser_peek_token (parser)->location;
5759 op = c_parser_cast_expression (parser, NULL);
5760 op = default_function_array_read_conversion (exp_loc, op);
5761 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5762 case CPP_MINUS_MINUS:
5763 c_parser_consume_token (parser);
5764 exp_loc = c_parser_peek_token (parser)->location;
5765 op = c_parser_cast_expression (parser, NULL);
5766 op = default_function_array_read_conversion (exp_loc, op);
5767 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5768 case CPP_AND:
5769 c_parser_consume_token (parser);
5770 op = c_parser_cast_expression (parser, NULL);
5771 mark_exp_read (op.value);
5772 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5773 case CPP_MULT:
5774 c_parser_consume_token (parser);
5775 exp_loc = c_parser_peek_token (parser)->location;
5776 op = c_parser_cast_expression (parser, NULL);
5777 op = default_function_array_read_conversion (exp_loc, op);
5778 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5779 return ret;
5780 case CPP_PLUS:
5781 if (!c_dialect_objc () && !in_system_header)
5782 warning_at (op_loc,
5783 OPT_Wtraditional,
5784 "traditional C rejects the unary plus operator");
5785 c_parser_consume_token (parser);
5786 exp_loc = c_parser_peek_token (parser)->location;
5787 op = c_parser_cast_expression (parser, NULL);
5788 op = default_function_array_read_conversion (exp_loc, op);
5789 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5790 case CPP_MINUS:
5791 c_parser_consume_token (parser);
5792 exp_loc = c_parser_peek_token (parser)->location;
5793 op = c_parser_cast_expression (parser, NULL);
5794 op = default_function_array_read_conversion (exp_loc, op);
5795 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5796 case CPP_COMPL:
5797 c_parser_consume_token (parser);
5798 exp_loc = c_parser_peek_token (parser)->location;
5799 op = c_parser_cast_expression (parser, NULL);
5800 op = default_function_array_read_conversion (exp_loc, op);
5801 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5802 case CPP_NOT:
5803 c_parser_consume_token (parser);
5804 exp_loc = c_parser_peek_token (parser)->location;
5805 op = c_parser_cast_expression (parser, NULL);
5806 op = default_function_array_read_conversion (exp_loc, op);
5807 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5808 case CPP_AND_AND:
5809 /* Refer to the address of a label as a pointer. */
5810 c_parser_consume_token (parser);
5811 if (c_parser_next_token_is (parser, CPP_NAME))
5813 ret.value = finish_label_address_expr
5814 (c_parser_peek_token (parser)->value, op_loc);
5815 c_parser_consume_token (parser);
5817 else
5819 c_parser_error (parser, "expected identifier");
5820 ret.value = error_mark_node;
5822 return ret;
5823 case CPP_KEYWORD:
5824 switch (c_parser_peek_token (parser)->keyword)
5826 case RID_SIZEOF:
5827 return c_parser_sizeof_expression (parser);
5828 case RID_ALIGNOF:
5829 return c_parser_alignof_expression (parser);
5830 case RID_EXTENSION:
5831 c_parser_consume_token (parser);
5832 ext = disable_extension_diagnostics ();
5833 ret = c_parser_cast_expression (parser, NULL);
5834 restore_extension_diagnostics (ext);
5835 return ret;
5836 case RID_REALPART:
5837 c_parser_consume_token (parser);
5838 exp_loc = c_parser_peek_token (parser)->location;
5839 op = c_parser_cast_expression (parser, NULL);
5840 op = default_function_array_conversion (exp_loc, op);
5841 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5842 case RID_IMAGPART:
5843 c_parser_consume_token (parser);
5844 exp_loc = c_parser_peek_token (parser)->location;
5845 op = c_parser_cast_expression (parser, NULL);
5846 op = default_function_array_conversion (exp_loc, op);
5847 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5848 default:
5849 return c_parser_postfix_expression (parser);
5851 default:
5852 return c_parser_postfix_expression (parser);
5856 /* Parse a sizeof expression. */
5858 static struct c_expr
5859 c_parser_sizeof_expression (c_parser *parser)
5861 struct c_expr expr;
5862 location_t expr_loc;
5863 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5864 c_parser_consume_token (parser);
5865 c_inhibit_evaluation_warnings++;
5866 in_sizeof++;
5867 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5868 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5870 /* Either sizeof ( type-name ) or sizeof unary-expression
5871 starting with a compound literal. */
5872 struct c_type_name *type_name;
5873 c_parser_consume_token (parser);
5874 expr_loc = c_parser_peek_token (parser)->location;
5875 type_name = c_parser_type_name (parser);
5876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5877 if (type_name == NULL)
5879 struct c_expr ret;
5880 c_inhibit_evaluation_warnings--;
5881 in_sizeof--;
5882 ret.value = error_mark_node;
5883 ret.original_code = ERROR_MARK;
5884 ret.original_type = NULL;
5885 return ret;
5887 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5889 expr = c_parser_postfix_expression_after_paren_type (parser,
5890 type_name,
5891 expr_loc);
5892 goto sizeof_expr;
5894 /* sizeof ( type-name ). */
5895 c_inhibit_evaluation_warnings--;
5896 in_sizeof--;
5897 return c_expr_sizeof_type (expr_loc, type_name);
5899 else
5901 expr_loc = c_parser_peek_token (parser)->location;
5902 expr = c_parser_unary_expression (parser);
5903 sizeof_expr:
5904 c_inhibit_evaluation_warnings--;
5905 in_sizeof--;
5906 mark_exp_read (expr.value);
5907 if (TREE_CODE (expr.value) == COMPONENT_REF
5908 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5909 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5910 return c_expr_sizeof_expr (expr_loc, expr);
5914 /* Parse an alignof expression. */
5916 static struct c_expr
5917 c_parser_alignof_expression (c_parser *parser)
5919 struct c_expr expr;
5920 location_t loc = c_parser_peek_token (parser)->location;
5921 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5922 c_parser_consume_token (parser);
5923 c_inhibit_evaluation_warnings++;
5924 in_alignof++;
5925 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5926 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5928 /* Either __alignof__ ( type-name ) or __alignof__
5929 unary-expression starting with a compound literal. */
5930 location_t loc;
5931 struct c_type_name *type_name;
5932 struct c_expr ret;
5933 c_parser_consume_token (parser);
5934 loc = c_parser_peek_token (parser)->location;
5935 type_name = c_parser_type_name (parser);
5936 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5937 if (type_name == NULL)
5939 struct c_expr ret;
5940 c_inhibit_evaluation_warnings--;
5941 in_alignof--;
5942 ret.value = error_mark_node;
5943 ret.original_code = ERROR_MARK;
5944 ret.original_type = NULL;
5945 return ret;
5947 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5949 expr = c_parser_postfix_expression_after_paren_type (parser,
5950 type_name,
5951 loc);
5952 goto alignof_expr;
5954 /* alignof ( type-name ). */
5955 c_inhibit_evaluation_warnings--;
5956 in_alignof--;
5957 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5958 ret.original_code = ERROR_MARK;
5959 ret.original_type = NULL;
5960 return ret;
5962 else
5964 struct c_expr ret;
5965 expr = c_parser_unary_expression (parser);
5966 alignof_expr:
5967 mark_exp_read (expr.value);
5968 c_inhibit_evaluation_warnings--;
5969 in_alignof--;
5970 ret.value = c_alignof_expr (loc, expr.value);
5971 ret.original_code = ERROR_MARK;
5972 ret.original_type = NULL;
5973 return ret;
5977 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5979 postfix-expression:
5980 primary-expression
5981 postfix-expression [ expression ]
5982 postfix-expression ( argument-expression-list[opt] )
5983 postfix-expression . identifier
5984 postfix-expression -> identifier
5985 postfix-expression ++
5986 postfix-expression --
5987 ( type-name ) { initializer-list }
5988 ( type-name ) { initializer-list , }
5990 argument-expression-list:
5991 argument-expression
5992 argument-expression-list , argument-expression
5994 primary-expression:
5995 identifier
5996 constant
5997 string-literal
5998 ( expression )
6000 GNU extensions:
6002 primary-expression:
6003 __func__
6004 (treated as a keyword in GNU C)
6005 __FUNCTION__
6006 __PRETTY_FUNCTION__
6007 ( compound-statement )
6008 __builtin_va_arg ( assignment-expression , type-name )
6009 __builtin_offsetof ( type-name , offsetof-member-designator )
6010 __builtin_choose_expr ( assignment-expression ,
6011 assignment-expression ,
6012 assignment-expression )
6013 __builtin_types_compatible_p ( type-name , type-name )
6015 offsetof-member-designator:
6016 identifier
6017 offsetof-member-designator . identifier
6018 offsetof-member-designator [ expression ]
6020 Objective-C:
6022 primary-expression:
6023 [ objc-receiver objc-message-args ]
6024 @selector ( objc-selector-arg )
6025 @protocol ( identifier )
6026 @encode ( type-name )
6027 objc-string-literal
6028 Classname . identifier
6031 static struct c_expr
6032 c_parser_postfix_expression (c_parser *parser)
6034 struct c_expr expr, e1, e2, e3;
6035 struct c_type_name *t1, *t2;
6036 location_t loc = c_parser_peek_token (parser)->location;;
6037 expr.original_code = ERROR_MARK;
6038 expr.original_type = NULL;
6039 switch (c_parser_peek_token (parser)->type)
6041 case CPP_NUMBER:
6042 expr.value = c_parser_peek_token (parser)->value;
6043 loc = c_parser_peek_token (parser)->location;
6044 c_parser_consume_token (parser);
6045 if (TREE_CODE (expr.value) == FIXED_CST
6046 && !targetm.fixed_point_supported_p ())
6048 error_at (loc, "fixed-point types not supported for this target");
6049 expr.value = error_mark_node;
6051 break;
6052 case CPP_CHAR:
6053 case CPP_CHAR16:
6054 case CPP_CHAR32:
6055 case CPP_WCHAR:
6056 expr.value = c_parser_peek_token (parser)->value;
6057 c_parser_consume_token (parser);
6058 break;
6059 case CPP_STRING:
6060 case CPP_STRING16:
6061 case CPP_STRING32:
6062 case CPP_WSTRING:
6063 case CPP_UTF8STRING:
6064 expr.value = c_parser_peek_token (parser)->value;
6065 expr.original_code = STRING_CST;
6066 c_parser_consume_token (parser);
6067 break;
6068 case CPP_OBJC_STRING:
6069 gcc_assert (c_dialect_objc ());
6070 expr.value
6071 = objc_build_string_object (c_parser_peek_token (parser)->value);
6072 c_parser_consume_token (parser);
6073 break;
6074 case CPP_NAME:
6075 switch (c_parser_peek_token (parser)->id_kind)
6077 case C_ID_ID:
6079 tree id = c_parser_peek_token (parser)->value;
6080 c_parser_consume_token (parser);
6081 expr.value = build_external_ref (loc, id,
6082 (c_parser_peek_token (parser)->type
6083 == CPP_OPEN_PAREN),
6084 &expr.original_type);
6085 break;
6087 case C_ID_CLASSNAME:
6089 /* Here we parse the Objective-C 2.0 Class.name dot
6090 syntax. */
6091 tree class_name = c_parser_peek_token (parser)->value;
6092 tree component;
6093 c_parser_consume_token (parser);
6094 gcc_assert (c_dialect_objc ());
6095 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6097 expr.value = error_mark_node;
6098 break;
6100 if (c_parser_next_token_is_not (parser, CPP_NAME))
6102 c_parser_error (parser, "expected identifier");
6103 expr.value = error_mark_node;
6104 break;
6106 component = c_parser_peek_token (parser)->value;
6107 c_parser_consume_token (parser);
6108 expr.value = objc_build_class_component_ref (class_name,
6109 component);
6110 break;
6112 default:
6113 c_parser_error (parser, "expected expression");
6114 expr.value = error_mark_node;
6115 break;
6117 break;
6118 case CPP_OPEN_PAREN:
6119 /* A parenthesized expression, statement expression or compound
6120 literal. */
6121 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6123 /* A statement expression. */
6124 tree stmt;
6125 location_t brace_loc;
6126 c_parser_consume_token (parser);
6127 brace_loc = c_parser_peek_token (parser)->location;
6128 c_parser_consume_token (parser);
6129 if (!building_stmt_list_p ())
6131 error_at (loc, "braced-group within expression allowed "
6132 "only inside a function");
6133 parser->error = true;
6134 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6135 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6136 expr.value = error_mark_node;
6137 break;
6139 stmt = c_begin_stmt_expr ();
6140 c_parser_compound_statement_nostart (parser);
6141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6142 "expected %<)%>");
6143 pedwarn (loc, OPT_pedantic,
6144 "ISO C forbids braced-groups within expressions");
6145 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6146 mark_exp_read (expr.value);
6148 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6150 /* A compound literal. ??? Can we actually get here rather
6151 than going directly to
6152 c_parser_postfix_expression_after_paren_type from
6153 elsewhere? */
6154 location_t loc;
6155 struct c_type_name *type_name;
6156 c_parser_consume_token (parser);
6157 loc = c_parser_peek_token (parser)->location;
6158 type_name = c_parser_type_name (parser);
6159 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6160 "expected %<)%>");
6161 if (type_name == NULL)
6163 expr.value = error_mark_node;
6165 else
6166 expr = c_parser_postfix_expression_after_paren_type (parser,
6167 type_name,
6168 loc);
6170 else
6172 /* A parenthesized expression. */
6173 c_parser_consume_token (parser);
6174 expr = c_parser_expression (parser);
6175 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6176 TREE_NO_WARNING (expr.value) = 1;
6177 if (expr.original_code != C_MAYBE_CONST_EXPR)
6178 expr.original_code = ERROR_MARK;
6179 /* Don't change EXPR.ORIGINAL_TYPE. */
6180 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6181 "expected %<)%>");
6183 break;
6184 case CPP_KEYWORD:
6185 switch (c_parser_peek_token (parser)->keyword)
6187 case RID_FUNCTION_NAME:
6188 case RID_PRETTY_FUNCTION_NAME:
6189 case RID_C99_FUNCTION_NAME:
6190 expr.value = fname_decl (loc,
6191 c_parser_peek_token (parser)->keyword,
6192 c_parser_peek_token (parser)->value);
6193 c_parser_consume_token (parser);
6194 break;
6195 case RID_VA_ARG:
6196 c_parser_consume_token (parser);
6197 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6199 expr.value = error_mark_node;
6200 break;
6202 e1 = c_parser_expr_no_commas (parser, NULL);
6203 mark_exp_read (e1.value);
6204 e1.value = c_fully_fold (e1.value, false, NULL);
6205 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6207 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6208 expr.value = error_mark_node;
6209 break;
6211 loc = c_parser_peek_token (parser)->location;
6212 t1 = c_parser_type_name (parser);
6213 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6214 "expected %<)%>");
6215 if (t1 == NULL)
6217 expr.value = error_mark_node;
6219 else
6221 tree type_expr = NULL_TREE;
6222 expr.value = c_build_va_arg (loc, e1.value,
6223 groktypename (t1, &type_expr, NULL));
6224 if (type_expr)
6226 expr.value = build2 (C_MAYBE_CONST_EXPR,
6227 TREE_TYPE (expr.value), type_expr,
6228 expr.value);
6229 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6232 break;
6233 case RID_OFFSETOF:
6234 c_parser_consume_token (parser);
6235 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6237 expr.value = error_mark_node;
6238 break;
6240 t1 = c_parser_type_name (parser);
6241 if (t1 == NULL)
6242 parser->error = true;
6243 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6244 gcc_assert (parser->error);
6245 if (parser->error)
6247 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6248 expr.value = error_mark_node;
6249 break;
6253 tree type = groktypename (t1, NULL, NULL);
6254 tree offsetof_ref;
6255 if (type == error_mark_node)
6256 offsetof_ref = error_mark_node;
6257 else
6259 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6260 SET_EXPR_LOCATION (offsetof_ref, loc);
6262 /* Parse the second argument to __builtin_offsetof. We
6263 must have one identifier, and beyond that we want to
6264 accept sub structure and sub array references. */
6265 if (c_parser_next_token_is (parser, CPP_NAME))
6267 offsetof_ref = build_component_ref
6268 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6269 c_parser_consume_token (parser);
6270 while (c_parser_next_token_is (parser, CPP_DOT)
6271 || c_parser_next_token_is (parser,
6272 CPP_OPEN_SQUARE)
6273 || c_parser_next_token_is (parser,
6274 CPP_DEREF))
6276 if (c_parser_next_token_is (parser, CPP_DEREF))
6278 loc = c_parser_peek_token (parser)->location;
6279 offsetof_ref = build_array_ref (loc,
6280 offsetof_ref,
6281 integer_zero_node);
6282 goto do_dot;
6284 else if (c_parser_next_token_is (parser, CPP_DOT))
6286 do_dot:
6287 c_parser_consume_token (parser);
6288 if (c_parser_next_token_is_not (parser,
6289 CPP_NAME))
6291 c_parser_error (parser, "expected identifier");
6292 break;
6294 offsetof_ref = build_component_ref
6295 (loc, offsetof_ref,
6296 c_parser_peek_token (parser)->value);
6297 c_parser_consume_token (parser);
6299 else
6301 tree idx;
6302 loc = c_parser_peek_token (parser)->location;
6303 c_parser_consume_token (parser);
6304 idx = c_parser_expression (parser).value;
6305 idx = c_fully_fold (idx, false, NULL);
6306 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6307 "expected %<]%>");
6308 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6312 else
6313 c_parser_error (parser, "expected identifier");
6314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6315 "expected %<)%>");
6316 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6318 break;
6319 case RID_CHOOSE_EXPR:
6320 c_parser_consume_token (parser);
6321 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6323 expr.value = error_mark_node;
6324 break;
6326 loc = c_parser_peek_token (parser)->location;
6327 e1 = c_parser_expr_no_commas (parser, NULL);
6328 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6330 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6331 expr.value = error_mark_node;
6332 break;
6334 e2 = c_parser_expr_no_commas (parser, NULL);
6335 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6338 expr.value = error_mark_node;
6339 break;
6341 e3 = c_parser_expr_no_commas (parser, NULL);
6342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6343 "expected %<)%>");
6345 tree c;
6347 c = e1.value;
6348 mark_exp_read (e2.value);
6349 mark_exp_read (e3.value);
6350 if (TREE_CODE (c) != INTEGER_CST
6351 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6352 error_at (loc,
6353 "first argument to %<__builtin_choose_expr%> not"
6354 " a constant");
6355 constant_expression_warning (c);
6356 expr = integer_zerop (c) ? e3 : e2;
6358 break;
6359 case RID_TYPES_COMPATIBLE_P:
6360 c_parser_consume_token (parser);
6361 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6363 expr.value = error_mark_node;
6364 break;
6366 t1 = c_parser_type_name (parser);
6367 if (t1 == NULL)
6369 expr.value = error_mark_node;
6370 break;
6372 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6375 expr.value = error_mark_node;
6376 break;
6378 t2 = c_parser_type_name (parser);
6379 if (t2 == NULL)
6381 expr.value = error_mark_node;
6382 break;
6384 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6385 "expected %<)%>");
6387 tree e1, e2;
6389 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6390 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6392 expr.value
6393 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6395 break;
6396 case RID_AT_SELECTOR:
6397 gcc_assert (c_dialect_objc ());
6398 c_parser_consume_token (parser);
6399 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6401 expr.value = error_mark_node;
6402 break;
6405 tree sel = c_parser_objc_selector_arg (parser);
6406 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6407 "expected %<)%>");
6408 expr.value = objc_build_selector_expr (loc, sel);
6410 break;
6411 case RID_AT_PROTOCOL:
6412 gcc_assert (c_dialect_objc ());
6413 c_parser_consume_token (parser);
6414 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6416 expr.value = error_mark_node;
6417 break;
6419 if (c_parser_next_token_is_not (parser, CPP_NAME))
6421 c_parser_error (parser, "expected identifier");
6422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6423 expr.value = error_mark_node;
6424 break;
6427 tree id = c_parser_peek_token (parser)->value;
6428 c_parser_consume_token (parser);
6429 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6430 "expected %<)%>");
6431 expr.value = objc_build_protocol_expr (id);
6433 break;
6434 case RID_AT_ENCODE:
6435 /* Extension to support C-structures in the archiver. */
6436 gcc_assert (c_dialect_objc ());
6437 c_parser_consume_token (parser);
6438 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6440 expr.value = error_mark_node;
6441 break;
6443 t1 = c_parser_type_name (parser);
6444 if (t1 == NULL)
6446 expr.value = error_mark_node;
6447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6448 break;
6450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6451 "expected %<)%>");
6453 tree type = groktypename (t1, NULL, NULL);
6454 expr.value = objc_build_encode_expr (type);
6456 break;
6457 default:
6458 c_parser_error (parser, "expected expression");
6459 expr.value = error_mark_node;
6460 break;
6462 break;
6463 case CPP_OPEN_SQUARE:
6464 if (c_dialect_objc ())
6466 tree receiver, args;
6467 c_parser_consume_token (parser);
6468 receiver = c_parser_objc_receiver (parser);
6469 args = c_parser_objc_message_args (parser);
6470 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6471 "expected %<]%>");
6472 expr.value = objc_build_message_expr (receiver, args);
6473 break;
6475 /* Else fall through to report error. */
6476 default:
6477 c_parser_error (parser, "expected expression");
6478 expr.value = error_mark_node;
6479 break;
6481 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6484 /* Parse a postfix expression after a parenthesized type name: the
6485 brace-enclosed initializer of a compound literal, possibly followed
6486 by some postfix operators. This is separate because it is not
6487 possible to tell until after the type name whether a cast
6488 expression has a cast or a compound literal, or whether the operand
6489 of sizeof is a parenthesized type name or starts with a compound
6490 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6491 location of the first token after the parentheses around the type
6492 name. */
6494 static struct c_expr
6495 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6496 struct c_type_name *type_name,
6497 location_t type_loc)
6499 tree type;
6500 struct c_expr init;
6501 bool non_const;
6502 struct c_expr expr;
6503 location_t start_loc;
6504 tree type_expr = NULL_TREE;
6505 bool type_expr_const = true;
6506 check_compound_literal_type (type_loc, type_name);
6507 start_init (NULL_TREE, NULL, 0);
6508 type = groktypename (type_name, &type_expr, &type_expr_const);
6509 start_loc = c_parser_peek_token (parser)->location;
6510 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6512 error_at (type_loc, "compound literal has variable size");
6513 type = error_mark_node;
6515 init = c_parser_braced_init (parser, type, false);
6516 finish_init ();
6517 maybe_warn_string_init (type, init);
6519 if (type != error_mark_node
6520 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6521 && current_function_decl)
6523 error ("compound literal qualified by address-space qualifier");
6524 type = error_mark_node;
6527 if (!flag_isoc99)
6528 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6529 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6530 ? CONSTRUCTOR_NON_CONST (init.value)
6531 : init.original_code == C_MAYBE_CONST_EXPR);
6532 non_const |= !type_expr_const;
6533 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6534 expr.original_code = ERROR_MARK;
6535 expr.original_type = NULL;
6536 if (type_expr)
6538 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6540 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6541 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6543 else
6545 gcc_assert (!non_const);
6546 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6547 type_expr, expr.value);
6550 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6553 /* Parse a postfix expression after the initial primary or compound
6554 literal; that is, parse a series of postfix operators.
6556 EXPR_LOC is the location of the primary expression. */
6558 static struct c_expr
6559 c_parser_postfix_expression_after_primary (c_parser *parser,
6560 location_t expr_loc,
6561 struct c_expr expr)
6563 struct c_expr orig_expr;
6564 tree ident, idx;
6565 VEC(tree,gc) *exprlist;
6566 VEC(tree,gc) *origtypes;
6567 while (true)
6569 location_t op_loc = c_parser_peek_token (parser)->location;
6570 switch (c_parser_peek_token (parser)->type)
6572 case CPP_OPEN_SQUARE:
6573 /* Array reference. */
6574 c_parser_consume_token (parser);
6575 idx = c_parser_expression (parser).value;
6576 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6577 "expected %<]%>");
6578 expr.value = build_array_ref (op_loc, expr.value, idx);
6579 expr.original_code = ERROR_MARK;
6580 expr.original_type = NULL;
6581 break;
6582 case CPP_OPEN_PAREN:
6583 /* Function call. */
6584 c_parser_consume_token (parser);
6585 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6586 exprlist = NULL;
6587 else
6588 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6590 "expected %<)%>");
6591 orig_expr = expr;
6592 mark_exp_read (expr.value);
6593 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6594 "(" after the FUNCNAME, which is what we have now. */
6595 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6596 origtypes);
6597 expr.original_code = ERROR_MARK;
6598 if (TREE_CODE (expr.value) == INTEGER_CST
6599 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6600 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6601 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6602 expr.original_code = C_MAYBE_CONST_EXPR;
6603 expr.original_type = NULL;
6604 if (exprlist != NULL)
6606 release_tree_vector (exprlist);
6607 release_tree_vector (origtypes);
6609 break;
6610 case CPP_DOT:
6611 /* Structure element reference. */
6612 c_parser_consume_token (parser);
6613 expr = default_function_array_conversion (expr_loc, expr);
6614 if (c_parser_next_token_is (parser, CPP_NAME))
6615 ident = c_parser_peek_token (parser)->value;
6616 else
6618 c_parser_error (parser, "expected identifier");
6619 expr.value = error_mark_node;
6620 expr.original_code = ERROR_MARK;
6621 expr.original_type = NULL;
6622 return expr;
6624 c_parser_consume_token (parser);
6625 expr.value = build_component_ref (op_loc, expr.value, ident);
6626 expr.original_code = ERROR_MARK;
6627 if (TREE_CODE (expr.value) != COMPONENT_REF)
6628 expr.original_type = NULL;
6629 else
6631 /* Remember the original type of a bitfield. */
6632 tree field = TREE_OPERAND (expr.value, 1);
6633 if (TREE_CODE (field) != FIELD_DECL)
6634 expr.original_type = NULL;
6635 else
6636 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6638 break;
6639 case CPP_DEREF:
6640 /* Structure element reference. */
6641 c_parser_consume_token (parser);
6642 expr = default_function_array_conversion (expr_loc, expr);
6643 if (c_parser_next_token_is (parser, CPP_NAME))
6644 ident = c_parser_peek_token (parser)->value;
6645 else
6647 c_parser_error (parser, "expected identifier");
6648 expr.value = error_mark_node;
6649 expr.original_code = ERROR_MARK;
6650 expr.original_type = NULL;
6651 return expr;
6653 c_parser_consume_token (parser);
6654 expr.value = build_component_ref (op_loc,
6655 build_indirect_ref (op_loc,
6656 expr.value,
6657 RO_ARROW),
6658 ident);
6659 expr.original_code = ERROR_MARK;
6660 if (TREE_CODE (expr.value) != COMPONENT_REF)
6661 expr.original_type = NULL;
6662 else
6664 /* Remember the original type of a bitfield. */
6665 tree field = TREE_OPERAND (expr.value, 1);
6666 if (TREE_CODE (field) != FIELD_DECL)
6667 expr.original_type = NULL;
6668 else
6669 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6671 break;
6672 case CPP_PLUS_PLUS:
6673 /* Postincrement. */
6674 c_parser_consume_token (parser);
6675 expr = default_function_array_read_conversion (expr_loc, expr);
6676 expr.value = build_unary_op (op_loc,
6677 POSTINCREMENT_EXPR, expr.value, 0);
6678 expr.original_code = ERROR_MARK;
6679 expr.original_type = NULL;
6680 break;
6681 case CPP_MINUS_MINUS:
6682 /* Postdecrement. */
6683 c_parser_consume_token (parser);
6684 expr = default_function_array_read_conversion (expr_loc, expr);
6685 expr.value = build_unary_op (op_loc,
6686 POSTDECREMENT_EXPR, expr.value, 0);
6687 expr.original_code = ERROR_MARK;
6688 expr.original_type = NULL;
6689 break;
6690 default:
6691 return expr;
6696 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6698 expression:
6699 assignment-expression
6700 expression , assignment-expression
6703 static struct c_expr
6704 c_parser_expression (c_parser *parser)
6706 struct c_expr expr;
6707 expr = c_parser_expr_no_commas (parser, NULL);
6708 while (c_parser_next_token_is (parser, CPP_COMMA))
6710 struct c_expr next;
6711 tree lhsval;
6712 location_t loc = c_parser_peek_token (parser)->location;
6713 location_t expr_loc;
6714 c_parser_consume_token (parser);
6715 expr_loc = c_parser_peek_token (parser)->location;
6716 lhsval = expr.value;
6717 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6718 lhsval = TREE_OPERAND (lhsval, 1);
6719 if (DECL_P (lhsval) || handled_component_p (lhsval))
6720 mark_exp_read (lhsval);
6721 next = c_parser_expr_no_commas (parser, NULL);
6722 next = default_function_array_conversion (expr_loc, next);
6723 expr.value = build_compound_expr (loc, expr.value, next.value);
6724 expr.original_code = COMPOUND_EXPR;
6725 expr.original_type = next.original_type;
6727 return expr;
6730 /* Parse an expression and convert functions or arrays to
6731 pointers. */
6733 static struct c_expr
6734 c_parser_expression_conv (c_parser *parser)
6736 struct c_expr expr;
6737 location_t loc = c_parser_peek_token (parser)->location;
6738 expr = c_parser_expression (parser);
6739 expr = default_function_array_conversion (loc, expr);
6740 return expr;
6743 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6744 functions and arrays to pointers. If FOLD_P, fold the expressions.
6746 nonempty-expr-list:
6747 assignment-expression
6748 nonempty-expr-list , assignment-expression
6751 static VEC(tree,gc) *
6752 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6753 VEC(tree,gc) **p_orig_types)
6755 VEC(tree,gc) *ret;
6756 VEC(tree,gc) *orig_types;
6757 struct c_expr expr;
6758 location_t loc = c_parser_peek_token (parser)->location;
6760 ret = make_tree_vector ();
6761 if (p_orig_types == NULL)
6762 orig_types = NULL;
6763 else
6764 orig_types = make_tree_vector ();
6766 expr = c_parser_expr_no_commas (parser, NULL);
6767 if (convert_p)
6768 expr = default_function_array_read_conversion (loc, expr);
6769 if (fold_p)
6770 expr.value = c_fully_fold (expr.value, false, NULL);
6771 VEC_quick_push (tree, ret, expr.value);
6772 if (orig_types != NULL)
6773 VEC_quick_push (tree, orig_types, expr.original_type);
6774 while (c_parser_next_token_is (parser, CPP_COMMA))
6776 c_parser_consume_token (parser);
6777 loc = c_parser_peek_token (parser)->location;
6778 expr = c_parser_expr_no_commas (parser, NULL);
6779 if (convert_p)
6780 expr = default_function_array_read_conversion (loc, expr);
6781 if (fold_p)
6782 expr.value = c_fully_fold (expr.value, false, NULL);
6783 VEC_safe_push (tree, gc, ret, expr.value);
6784 if (orig_types != NULL)
6785 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6787 if (orig_types != NULL)
6788 *p_orig_types = orig_types;
6789 return ret;
6792 /* Parse Objective-C-specific constructs. */
6794 /* Parse an objc-class-definition.
6796 objc-class-definition:
6797 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6798 objc-class-instance-variables[opt] objc-methodprotolist @end
6799 @implementation identifier objc-superclass[opt]
6800 objc-class-instance-variables[opt]
6801 @interface identifier ( identifier ) objc-protocol-refs[opt]
6802 objc-methodprotolist @end
6803 @interface identifier ( ) objc-protocol-refs[opt]
6804 objc-methodprotolist @end
6805 @implementation identifier ( identifier )
6807 objc-superclass:
6808 : identifier
6810 "@interface identifier (" must start "@interface identifier (
6811 identifier ) ...": objc-methodprotolist in the first production may
6812 not start with a parenthesized identifier as a declarator of a data
6813 definition with no declaration specifiers if the objc-superclass,
6814 objc-protocol-refs and objc-class-instance-variables are omitted. */
6816 static void
6817 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6819 bool iface_p;
6820 tree id1;
6821 tree superclass;
6822 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6823 iface_p = true;
6824 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6825 iface_p = false;
6826 else
6827 gcc_unreachable ();
6829 c_parser_consume_token (parser);
6830 if (c_parser_next_token_is_not (parser, CPP_NAME))
6832 c_parser_error (parser, "expected identifier");
6833 return;
6835 id1 = c_parser_peek_token (parser)->value;
6836 c_parser_consume_token (parser);
6837 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6839 /* We have a category or class extension. */
6840 tree id2;
6841 tree proto = NULL_TREE;
6842 c_parser_consume_token (parser);
6843 if (c_parser_next_token_is_not (parser, CPP_NAME))
6845 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6847 /* We have a class extension. */
6848 id2 = NULL_TREE;
6850 else
6852 c_parser_error (parser, "expected identifier or %<)%>");
6853 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6854 return;
6857 else
6859 id2 = c_parser_peek_token (parser)->value;
6860 c_parser_consume_token (parser);
6862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6863 if (!iface_p)
6865 objc_start_category_implementation (id1, id2);
6866 return;
6868 if (c_parser_next_token_is (parser, CPP_LESS))
6869 proto = c_parser_objc_protocol_refs (parser);
6870 objc_start_category_interface (id1, id2, proto, attributes);
6871 c_parser_objc_methodprotolist (parser);
6872 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6873 objc_finish_interface ();
6874 return;
6876 if (c_parser_next_token_is (parser, CPP_COLON))
6878 c_parser_consume_token (parser);
6879 if (c_parser_next_token_is_not (parser, CPP_NAME))
6881 c_parser_error (parser, "expected identifier");
6882 return;
6884 superclass = c_parser_peek_token (parser)->value;
6885 c_parser_consume_token (parser);
6887 else
6888 superclass = NULL_TREE;
6889 if (iface_p)
6891 tree proto = NULL_TREE;
6892 if (c_parser_next_token_is (parser, CPP_LESS))
6893 proto = c_parser_objc_protocol_refs (parser);
6894 objc_start_class_interface (id1, superclass, proto, attributes);
6896 else
6897 objc_start_class_implementation (id1, superclass);
6898 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6899 c_parser_objc_class_instance_variables (parser);
6900 if (iface_p)
6902 objc_continue_interface ();
6903 c_parser_objc_methodprotolist (parser);
6904 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6905 objc_finish_interface ();
6907 else
6909 objc_continue_implementation ();
6910 return;
6914 /* Parse objc-class-instance-variables.
6916 objc-class-instance-variables:
6917 { objc-instance-variable-decl-list[opt] }
6919 objc-instance-variable-decl-list:
6920 objc-visibility-spec
6921 objc-instance-variable-decl ;
6923 objc-instance-variable-decl-list objc-visibility-spec
6924 objc-instance-variable-decl-list objc-instance-variable-decl ;
6925 objc-instance-variable-decl-list ;
6927 objc-visibility-spec:
6928 @private
6929 @protected
6930 @public
6932 objc-instance-variable-decl:
6933 struct-declaration
6936 static void
6937 c_parser_objc_class_instance_variables (c_parser *parser)
6939 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6940 c_parser_consume_token (parser);
6941 while (c_parser_next_token_is_not (parser, CPP_EOF))
6943 tree decls;
6944 /* Parse any stray semicolon. */
6945 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6947 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6948 "extra semicolon");
6949 c_parser_consume_token (parser);
6950 continue;
6952 /* Stop if at the end of the instance variables. */
6953 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6955 c_parser_consume_token (parser);
6956 break;
6958 /* Parse any objc-visibility-spec. */
6959 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6961 c_parser_consume_token (parser);
6962 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6963 continue;
6965 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6967 c_parser_consume_token (parser);
6968 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6969 continue;
6971 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6973 c_parser_consume_token (parser);
6974 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6975 continue;
6977 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6979 c_parser_consume_token (parser);
6980 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6981 continue;
6983 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6985 c_parser_pragma (parser, pragma_external);
6986 continue;
6989 /* Parse some comma-separated declarations. */
6990 decls = c_parser_struct_declaration (parser);
6991 if (decls == NULL)
6993 /* There is a syntax error. We want to skip the offending
6994 tokens up to the next ';' (included) or '}'
6995 (excluded). */
6997 /* First, skip manually a ')' or ']'. This is because they
6998 reduce the nesting level, so c_parser_skip_until_found()
6999 wouldn't be able to skip past them. */
7000 c_token *token = c_parser_peek_token (parser);
7001 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7002 c_parser_consume_token (parser);
7004 /* Then, do the standard skipping. */
7005 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7007 /* We hopefully recovered. Start normal parsing again. */
7008 parser->error = false;
7009 continue;
7011 else
7013 /* Comma-separated instance variables are chained together
7014 in reverse order; add them one by one. */
7015 tree ivar = nreverse (decls);
7016 for (; ivar; ivar = DECL_CHAIN (ivar))
7017 objc_add_instance_variable (copy_node (ivar));
7019 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7023 /* Parse an objc-class-declaration.
7025 objc-class-declaration:
7026 @class identifier-list ;
7029 static void
7030 c_parser_objc_class_declaration (c_parser *parser)
7032 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7033 c_parser_consume_token (parser);
7034 /* Any identifiers, including those declared as type names, are OK
7035 here. */
7036 while (true)
7038 tree id;
7039 if (c_parser_next_token_is_not (parser, CPP_NAME))
7041 c_parser_error (parser, "expected identifier");
7042 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7043 parser->error = false;
7044 return;
7046 id = c_parser_peek_token (parser)->value;
7047 objc_declare_class (id);
7048 c_parser_consume_token (parser);
7049 if (c_parser_next_token_is (parser, CPP_COMMA))
7050 c_parser_consume_token (parser);
7051 else
7052 break;
7054 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7057 /* Parse an objc-alias-declaration.
7059 objc-alias-declaration:
7060 @compatibility_alias identifier identifier ;
7063 static void
7064 c_parser_objc_alias_declaration (c_parser *parser)
7066 tree id1, id2;
7067 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7068 c_parser_consume_token (parser);
7069 if (c_parser_next_token_is_not (parser, CPP_NAME))
7071 c_parser_error (parser, "expected identifier");
7072 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7073 return;
7075 id1 = c_parser_peek_token (parser)->value;
7076 c_parser_consume_token (parser);
7077 if (c_parser_next_token_is_not (parser, CPP_NAME))
7079 c_parser_error (parser, "expected identifier");
7080 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7081 return;
7083 id2 = c_parser_peek_token (parser)->value;
7084 c_parser_consume_token (parser);
7085 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7086 objc_declare_alias (id1, id2);
7089 /* Parse an objc-protocol-definition.
7091 objc-protocol-definition:
7092 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7093 @protocol identifier-list ;
7095 "@protocol identifier ;" should be resolved as "@protocol
7096 identifier-list ;": objc-methodprotolist may not start with a
7097 semicolon in the first alternative if objc-protocol-refs are
7098 omitted. */
7100 static void
7101 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7103 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7105 c_parser_consume_token (parser);
7106 if (c_parser_next_token_is_not (parser, CPP_NAME))
7108 c_parser_error (parser, "expected identifier");
7109 return;
7111 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7112 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7114 /* Any identifiers, including those declared as type names, are
7115 OK here. */
7116 while (true)
7118 tree id;
7119 if (c_parser_next_token_is_not (parser, CPP_NAME))
7121 c_parser_error (parser, "expected identifier");
7122 break;
7124 id = c_parser_peek_token (parser)->value;
7125 objc_declare_protocol (id, attributes);
7126 c_parser_consume_token (parser);
7127 if (c_parser_next_token_is (parser, CPP_COMMA))
7128 c_parser_consume_token (parser);
7129 else
7130 break;
7132 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7134 else
7136 tree id = c_parser_peek_token (parser)->value;
7137 tree proto = NULL_TREE;
7138 c_parser_consume_token (parser);
7139 if (c_parser_next_token_is (parser, CPP_LESS))
7140 proto = c_parser_objc_protocol_refs (parser);
7141 parser->objc_pq_context = true;
7142 objc_start_protocol (id, proto, attributes);
7143 c_parser_objc_methodprotolist (parser);
7144 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7145 parser->objc_pq_context = false;
7146 objc_finish_interface ();
7150 /* Parse an objc-method-type.
7152 objc-method-type:
7156 Return true if it is a class method (+) and false if it is
7157 an instance method (-).
7159 static inline bool
7160 c_parser_objc_method_type (c_parser *parser)
7162 switch (c_parser_peek_token (parser)->type)
7164 case CPP_PLUS:
7165 c_parser_consume_token (parser);
7166 return true;
7167 case CPP_MINUS:
7168 c_parser_consume_token (parser);
7169 return false;
7170 default:
7171 gcc_unreachable ();
7175 /* Parse an objc-method-definition.
7177 objc-method-definition:
7178 objc-method-type objc-method-decl ;[opt] compound-statement
7181 static void
7182 c_parser_objc_method_definition (c_parser *parser)
7184 bool is_class_method = c_parser_objc_method_type (parser);
7185 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7186 parser->objc_pq_context = true;
7187 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7188 &expr);
7189 if (decl == error_mark_node)
7190 return; /* Bail here. */
7192 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7194 c_parser_consume_token (parser);
7195 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7196 "extra semicolon in method definition specified");
7199 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7201 c_parser_error (parser, "expected %<{%>");
7202 return;
7205 parser->objc_pq_context = false;
7206 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7208 add_stmt (c_parser_compound_statement (parser));
7209 objc_finish_method_definition (current_function_decl);
7211 else
7213 /* This code is executed when we find a method definition
7214 outside of an @implementation context (or invalid for other
7215 reasons). Parse the method (to keep going) but do not emit
7216 any code.
7218 c_parser_compound_statement (parser);
7222 /* Parse an objc-methodprotolist.
7224 objc-methodprotolist:
7225 empty
7226 objc-methodprotolist objc-methodproto
7227 objc-methodprotolist declaration
7228 objc-methodprotolist ;
7229 @optional
7230 @required
7232 The declaration is a data definition, which may be missing
7233 declaration specifiers under the same rules and diagnostics as
7234 other data definitions outside functions, and the stray semicolon
7235 is diagnosed the same way as a stray semicolon outside a
7236 function. */
7238 static void
7239 c_parser_objc_methodprotolist (c_parser *parser)
7241 while (true)
7243 /* The list is terminated by @end. */
7244 switch (c_parser_peek_token (parser)->type)
7246 case CPP_SEMICOLON:
7247 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7248 "ISO C does not allow extra %<;%> outside of a function");
7249 c_parser_consume_token (parser);
7250 break;
7251 case CPP_PLUS:
7252 case CPP_MINUS:
7253 c_parser_objc_methodproto (parser);
7254 break;
7255 case CPP_PRAGMA:
7256 c_parser_pragma (parser, pragma_external);
7257 break;
7258 case CPP_EOF:
7259 return;
7260 default:
7261 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7262 return;
7263 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7264 c_parser_objc_at_property_declaration (parser);
7265 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7267 objc_set_method_opt (true);
7268 c_parser_consume_token (parser);
7270 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7272 objc_set_method_opt (false);
7273 c_parser_consume_token (parser);
7275 else
7276 c_parser_declaration_or_fndef (parser, false, false, true,
7277 false, true, NULL);
7278 break;
7283 /* Parse an objc-methodproto.
7285 objc-methodproto:
7286 objc-method-type objc-method-decl ;
7289 static void
7290 c_parser_objc_methodproto (c_parser *parser)
7292 bool is_class_method = c_parser_objc_method_type (parser);
7293 tree decl, attributes = NULL_TREE;
7295 /* Remember protocol qualifiers in prototypes. */
7296 parser->objc_pq_context = true;
7297 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7298 NULL);
7299 /* Forget protocol qualifiers now. */
7300 parser->objc_pq_context = false;
7302 /* Do not allow the presence of attributes to hide an erroneous
7303 method implementation in the interface section. */
7304 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7306 c_parser_error (parser, "expected %<;%>");
7307 return;
7310 if (decl != error_mark_node)
7311 objc_add_method_declaration (is_class_method, decl, attributes);
7313 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7316 /* If we are at a position that method attributes may be present, check that
7317 there are not any parsed already (a syntax error) and then collect any
7318 specified at the current location. Finally, if new attributes were present,
7319 check that the next token is legal ( ';' for decls and '{' for defs). */
7321 static bool
7322 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7324 bool bad = false;
7325 if (*attributes)
7327 c_parser_error (parser,
7328 "method attributes must be specified at the end only");
7329 *attributes = NULL_TREE;
7330 bad = true;
7333 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7334 *attributes = c_parser_attributes (parser);
7336 /* If there were no attributes here, just report any earlier error. */
7337 if (*attributes == NULL_TREE || bad)
7338 return bad;
7340 /* If the attributes are followed by a ; or {, then just report any earlier
7341 error. */
7342 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7343 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7344 return bad;
7346 /* We've got attributes, but not at the end. */
7347 c_parser_error (parser,
7348 "expected %<;%> or %<{%> after method attribute definition");
7349 return true;
7352 /* Parse an objc-method-decl.
7354 objc-method-decl:
7355 ( objc-type-name ) objc-selector
7356 objc-selector
7357 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7358 objc-keyword-selector objc-optparmlist
7359 attributes
7361 objc-keyword-selector:
7362 objc-keyword-decl
7363 objc-keyword-selector objc-keyword-decl
7365 objc-keyword-decl:
7366 objc-selector : ( objc-type-name ) identifier
7367 objc-selector : identifier
7368 : ( objc-type-name ) identifier
7369 : identifier
7371 objc-optparmlist:
7372 objc-optparms objc-optellipsis
7374 objc-optparms:
7375 empty
7376 objc-opt-parms , parameter-declaration
7378 objc-optellipsis:
7379 empty
7380 , ...
7383 static tree
7384 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7385 tree *attributes, tree *expr)
7387 tree type = NULL_TREE;
7388 tree sel;
7389 tree parms = NULL_TREE;
7390 bool ellipsis = false;
7391 bool attr_err = false;
7393 *attributes = NULL_TREE;
7394 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7396 c_parser_consume_token (parser);
7397 type = c_parser_objc_type_name (parser);
7398 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7400 sel = c_parser_objc_selector (parser);
7401 /* If there is no selector, or a colon follows, we have an
7402 objc-keyword-selector. If there is a selector, and a colon does
7403 not follow, that selector ends the objc-method-decl. */
7404 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7406 tree tsel = sel;
7407 tree list = NULL_TREE;
7408 while (true)
7410 tree atype = NULL_TREE, id, keyworddecl;
7411 tree param_attr = NULL_TREE;
7412 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7413 break;
7414 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7416 c_parser_consume_token (parser);
7417 atype = c_parser_objc_type_name (parser);
7418 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7419 "expected %<)%>");
7421 /* New ObjC allows attributes on method parameters. */
7422 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7423 param_attr = c_parser_attributes (parser);
7424 if (c_parser_next_token_is_not (parser, CPP_NAME))
7426 c_parser_error (parser, "expected identifier");
7427 return error_mark_node;
7429 id = c_parser_peek_token (parser)->value;
7430 c_parser_consume_token (parser);
7431 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7432 list = chainon (list, keyworddecl);
7433 tsel = c_parser_objc_selector (parser);
7434 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7435 break;
7438 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7440 /* Parse the optional parameter list. Optional Objective-C
7441 method parameters follow the C syntax, and may include '...'
7442 to denote a variable number of arguments. */
7443 parms = make_node (TREE_LIST);
7444 while (c_parser_next_token_is (parser, CPP_COMMA))
7446 struct c_parm *parm;
7447 c_parser_consume_token (parser);
7448 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7450 ellipsis = true;
7451 c_parser_consume_token (parser);
7452 attr_err |= c_parser_objc_maybe_method_attributes
7453 (parser, attributes) ;
7454 break;
7456 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7457 if (parm == NULL)
7458 break;
7459 parms = chainon (parms,
7460 build_tree_list (NULL_TREE, grokparm (parm, expr)));
7462 sel = list;
7464 else
7465 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7467 if (sel == NULL)
7469 c_parser_error (parser, "objective-c method declaration is expected");
7470 return error_mark_node;
7473 if (attr_err)
7474 return error_mark_node;
7476 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7479 /* Parse an objc-type-name.
7481 objc-type-name:
7482 objc-type-qualifiers[opt] type-name
7483 objc-type-qualifiers[opt]
7485 objc-type-qualifiers:
7486 objc-type-qualifier
7487 objc-type-qualifiers objc-type-qualifier
7489 objc-type-qualifier: one of
7490 in out inout bycopy byref oneway
7493 static tree
7494 c_parser_objc_type_name (c_parser *parser)
7496 tree quals = NULL_TREE;
7497 struct c_type_name *type_name = NULL;
7498 tree type = NULL_TREE;
7499 while (true)
7501 c_token *token = c_parser_peek_token (parser);
7502 if (token->type == CPP_KEYWORD
7503 && (token->keyword == RID_IN
7504 || token->keyword == RID_OUT
7505 || token->keyword == RID_INOUT
7506 || token->keyword == RID_BYCOPY
7507 || token->keyword == RID_BYREF
7508 || token->keyword == RID_ONEWAY))
7510 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7511 c_parser_consume_token (parser);
7513 else
7514 break;
7516 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7517 type_name = c_parser_type_name (parser);
7518 if (type_name)
7519 type = groktypename (type_name, NULL, NULL);
7521 /* If the type is unknown, and error has already been produced and
7522 we need to recover from the error. In that case, use NULL_TREE
7523 for the type, as if no type had been specified; this will use the
7524 default type ('id') which is good for error recovery. */
7525 if (type == error_mark_node)
7526 type = NULL_TREE;
7528 return build_tree_list (quals, type);
7531 /* Parse objc-protocol-refs.
7533 objc-protocol-refs:
7534 < identifier-list >
7537 static tree
7538 c_parser_objc_protocol_refs (c_parser *parser)
7540 tree list = NULL_TREE;
7541 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7542 c_parser_consume_token (parser);
7543 /* Any identifiers, including those declared as type names, are OK
7544 here. */
7545 while (true)
7547 tree id;
7548 if (c_parser_next_token_is_not (parser, CPP_NAME))
7550 c_parser_error (parser, "expected identifier");
7551 break;
7553 id = c_parser_peek_token (parser)->value;
7554 list = chainon (list, build_tree_list (NULL_TREE, id));
7555 c_parser_consume_token (parser);
7556 if (c_parser_next_token_is (parser, CPP_COMMA))
7557 c_parser_consume_token (parser);
7558 else
7559 break;
7561 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7562 return list;
7565 /* Parse an objc-try-catch-finally-statement.
7567 objc-try-catch-finally-statement:
7568 @try compound-statement objc-catch-list[opt]
7569 @try compound-statement objc-catch-list[opt] @finally compound-statement
7571 objc-catch-list:
7572 @catch ( objc-catch-parameter-declaration ) compound-statement
7573 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7575 objc-catch-parameter-declaration:
7576 parameter-declaration
7577 '...'
7579 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7581 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7582 for C++. Keep them in sync. */
7584 static void
7585 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7587 location_t location;
7588 tree stmt;
7590 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7591 c_parser_consume_token (parser);
7592 location = c_parser_peek_token (parser)->location;
7593 objc_maybe_warn_exceptions (location);
7594 stmt = c_parser_compound_statement (parser);
7595 objc_begin_try_stmt (location, stmt);
7597 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7599 struct c_parm *parm;
7600 tree parameter_declaration = error_mark_node;
7601 bool seen_open_paren = false;
7603 c_parser_consume_token (parser);
7604 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7605 seen_open_paren = true;
7606 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7608 /* We have "@catch (...)" (where the '...' are literally
7609 what is in the code). Skip the '...'.
7610 parameter_declaration is set to NULL_TREE, and
7611 objc_being_catch_clauses() knows that that means
7612 '...'. */
7613 c_parser_consume_token (parser);
7614 parameter_declaration = NULL_TREE;
7616 else
7618 /* We have "@catch (NSException *exception)" or something
7619 like that. Parse the parameter declaration. */
7620 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7621 if (parm == NULL)
7622 parameter_declaration = error_mark_node;
7623 else
7624 parameter_declaration = grokparm (parm, NULL);
7626 if (seen_open_paren)
7627 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7628 else
7630 /* If there was no open parenthesis, we are recovering from
7631 an error, and we are trying to figure out what mistake
7632 the user has made. */
7634 /* If there is an immediate closing parenthesis, the user
7635 probably forgot the opening one (ie, they typed "@catch
7636 NSException *e)". Parse the closing parenthesis and keep
7637 going. */
7638 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7639 c_parser_consume_token (parser);
7641 /* If these is no immediate closing parenthesis, the user
7642 probably doesn't know that parenthesis are required at
7643 all (ie, they typed "@catch NSException *e"). So, just
7644 forget about the closing parenthesis and keep going. */
7646 objc_begin_catch_clause (parameter_declaration);
7647 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7648 c_parser_compound_statement_nostart (parser);
7649 objc_finish_catch_clause ();
7651 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7653 c_parser_consume_token (parser);
7654 location = c_parser_peek_token (parser)->location;
7655 stmt = c_parser_compound_statement (parser);
7656 objc_build_finally_clause (location, stmt);
7658 objc_finish_try_stmt ();
7661 /* Parse an objc-synchronized-statement.
7663 objc-synchronized-statement:
7664 @synchronized ( expression ) compound-statement
7667 static void
7668 c_parser_objc_synchronized_statement (c_parser *parser)
7670 location_t loc;
7671 tree expr, stmt;
7672 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7673 c_parser_consume_token (parser);
7674 loc = c_parser_peek_token (parser)->location;
7675 objc_maybe_warn_exceptions (loc);
7676 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7678 expr = c_parser_expression (parser).value;
7679 expr = c_fully_fold (expr, false, NULL);
7680 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7682 else
7683 expr = error_mark_node;
7684 stmt = c_parser_compound_statement (parser);
7685 objc_build_synchronized (loc, expr, stmt);
7688 /* Parse an objc-selector; return NULL_TREE without an error if the
7689 next token is not an objc-selector.
7691 objc-selector:
7692 identifier
7693 one of
7694 enum struct union if else while do for switch case default
7695 break continue return goto asm sizeof typeof __alignof
7696 unsigned long const short volatile signed restrict _Complex
7697 in out inout bycopy byref oneway int char float double void _Bool
7699 ??? Why this selection of keywords but not, for example, storage
7700 class specifiers? */
7702 static tree
7703 c_parser_objc_selector (c_parser *parser)
7705 c_token *token = c_parser_peek_token (parser);
7706 tree value = token->value;
7707 if (token->type == CPP_NAME)
7709 c_parser_consume_token (parser);
7710 return value;
7712 if (token->type != CPP_KEYWORD)
7713 return NULL_TREE;
7714 switch (token->keyword)
7716 case RID_ENUM:
7717 case RID_STRUCT:
7718 case RID_UNION:
7719 case RID_IF:
7720 case RID_ELSE:
7721 case RID_WHILE:
7722 case RID_DO:
7723 case RID_FOR:
7724 case RID_SWITCH:
7725 case RID_CASE:
7726 case RID_DEFAULT:
7727 case RID_BREAK:
7728 case RID_CONTINUE:
7729 case RID_RETURN:
7730 case RID_GOTO:
7731 case RID_ASM:
7732 case RID_SIZEOF:
7733 case RID_TYPEOF:
7734 case RID_ALIGNOF:
7735 case RID_UNSIGNED:
7736 case RID_LONG:
7737 case RID_INT128:
7738 case RID_CONST:
7739 case RID_SHORT:
7740 case RID_VOLATILE:
7741 case RID_SIGNED:
7742 case RID_RESTRICT:
7743 case RID_COMPLEX:
7744 case RID_IN:
7745 case RID_OUT:
7746 case RID_INOUT:
7747 case RID_BYCOPY:
7748 case RID_BYREF:
7749 case RID_ONEWAY:
7750 case RID_INT:
7751 case RID_CHAR:
7752 case RID_FLOAT:
7753 case RID_DOUBLE:
7754 case RID_VOID:
7755 case RID_BOOL:
7756 c_parser_consume_token (parser);
7757 return value;
7758 default:
7759 return NULL_TREE;
7763 /* Parse an objc-selector-arg.
7765 objc-selector-arg:
7766 objc-selector
7767 objc-keywordname-list
7769 objc-keywordname-list:
7770 objc-keywordname
7771 objc-keywordname-list objc-keywordname
7773 objc-keywordname:
7774 objc-selector :
7778 static tree
7779 c_parser_objc_selector_arg (c_parser *parser)
7781 tree sel = c_parser_objc_selector (parser);
7782 tree list = NULL_TREE;
7783 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7784 return sel;
7785 while (true)
7787 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7788 return list;
7789 list = chainon (list, build_tree_list (sel, NULL_TREE));
7790 sel = c_parser_objc_selector (parser);
7791 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7792 break;
7794 return list;
7797 /* Parse an objc-receiver.
7799 objc-receiver:
7800 expression
7801 class-name
7802 type-name
7805 static tree
7806 c_parser_objc_receiver (c_parser *parser)
7808 if (c_parser_peek_token (parser)->type == CPP_NAME
7809 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7810 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7812 tree id = c_parser_peek_token (parser)->value;
7813 c_parser_consume_token (parser);
7814 return objc_get_class_reference (id);
7816 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7819 /* Parse objc-message-args.
7821 objc-message-args:
7822 objc-selector
7823 objc-keywordarg-list
7825 objc-keywordarg-list:
7826 objc-keywordarg
7827 objc-keywordarg-list objc-keywordarg
7829 objc-keywordarg:
7830 objc-selector : objc-keywordexpr
7831 : objc-keywordexpr
7834 static tree
7835 c_parser_objc_message_args (c_parser *parser)
7837 tree sel = c_parser_objc_selector (parser);
7838 tree list = NULL_TREE;
7839 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7840 return sel;
7841 while (true)
7843 tree keywordexpr;
7844 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7845 return error_mark_node;
7846 keywordexpr = c_parser_objc_keywordexpr (parser);
7847 list = chainon (list, build_tree_list (sel, keywordexpr));
7848 sel = c_parser_objc_selector (parser);
7849 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7850 break;
7852 return list;
7855 /* Parse an objc-keywordexpr.
7857 objc-keywordexpr:
7858 nonempty-expr-list
7861 static tree
7862 c_parser_objc_keywordexpr (c_parser *parser)
7864 tree ret;
7865 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7866 if (VEC_length (tree, expr_list) == 1)
7868 /* Just return the expression, remove a level of
7869 indirection. */
7870 ret = VEC_index (tree, expr_list, 0);
7872 else
7874 /* We have a comma expression, we will collapse later. */
7875 ret = build_tree_list_vec (expr_list);
7877 release_tree_vector (expr_list);
7878 return ret;
7881 /* A check, needed in several places, that ObjC interface, implementation or
7882 method definitions are not prefixed by incorrect items. */
7883 static bool
7884 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7885 struct c_declspecs *specs)
7887 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7888 || specs->typespec_kind != ctsk_none)
7890 c_parser_error (parser,
7891 "no type or storage class may be specified here,");
7892 c_parser_skip_to_end_of_block_or_statement (parser);
7893 return true;
7895 return false;
7898 /* Parse an Objective-C @property declaration. The syntax is:
7900 objc-property-declaration:
7901 '@property' objc-property-attributes[opt] struct-declaration ;
7903 objc-property-attributes:
7904 '(' objc-property-attribute-list ')'
7906 objc-property-attribute-list:
7907 objc-property-attribute
7908 objc-property-attribute-list, objc-property-attribute
7910 objc-property-attribute
7911 'getter' = identifier
7912 'setter' = identifier
7913 'readonly'
7914 'readwrite'
7915 'assign'
7916 'retain'
7917 'copy'
7918 'nonatomic'
7920 For example:
7921 @property NSString *name;
7922 @property (readonly) id object;
7923 @property (retain, nonatomic, getter=getTheName) id name;
7924 @property int a, b, c;
7926 PS: This function is identical to cp_parser_objc_at_propery_declaration
7927 for C++. Keep them in sync. */
7928 static void
7929 c_parser_objc_at_property_declaration (c_parser *parser)
7931 /* The following variables hold the attributes of the properties as
7932 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7933 seen. When we see an attribute, we set them to 'true' (if they
7934 are boolean properties) or to the identifier (if they have an
7935 argument, ie, for getter and setter). Note that here we only
7936 parse the list of attributes, check the syntax and accumulate the
7937 attributes that we find. objc_add_property_declaration() will
7938 then process the information. */
7939 bool property_assign = false;
7940 bool property_copy = false;
7941 tree property_getter_ident = NULL_TREE;
7942 bool property_nonatomic = false;
7943 bool property_readonly = false;
7944 bool property_readwrite = false;
7945 bool property_retain = false;
7946 tree property_setter_ident = NULL_TREE;
7948 /* 'properties' is the list of properties that we read. Usually a
7949 single one, but maybe more (eg, in "@property int a, b, c;" there
7950 are three). */
7951 tree properties;
7952 location_t loc;
7954 loc = c_parser_peek_token (parser)->location;
7955 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7957 c_parser_consume_token (parser); /* Eat '@property'. */
7959 /* Parse the optional attribute list... */
7960 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7962 /* Eat the '(' */
7963 c_parser_consume_token (parser);
7965 /* Property attribute keywords are valid now. */
7966 parser->objc_property_attr_context = true;
7968 while (true)
7970 bool syntax_error = false;
7971 c_token *token = c_parser_peek_token (parser);
7972 enum rid keyword;
7974 if (token->type != CPP_KEYWORD)
7976 if (token->type == CPP_CLOSE_PAREN)
7977 c_parser_error (parser, "expected identifier");
7978 else
7980 c_parser_consume_token (parser);
7981 c_parser_error (parser, "unknown property attribute");
7983 break;
7985 keyword = token->keyword;
7986 c_parser_consume_token (parser);
7987 switch (keyword)
7989 case RID_ASSIGN: property_assign = true; break;
7990 case RID_COPY: property_copy = true; break;
7991 case RID_NONATOMIC: property_nonatomic = true; break;
7992 case RID_READONLY: property_readonly = true; break;
7993 case RID_READWRITE: property_readwrite = true; break;
7994 case RID_RETAIN: property_retain = true; break;
7996 case RID_GETTER:
7997 case RID_SETTER:
7998 if (c_parser_next_token_is_not (parser, CPP_EQ))
8000 if (keyword == RID_GETTER)
8001 c_parser_error (parser,
8002 "missing %<=%> (after %<getter%> attribute)");
8003 else
8004 c_parser_error (parser,
8005 "missing %<=%> (after %<setter%> attribute)");
8006 syntax_error = true;
8007 break;
8009 c_parser_consume_token (parser); /* eat the = */
8010 if (c_parser_next_token_is_not (parser, CPP_NAME))
8012 c_parser_error (parser, "expected identifier");
8013 syntax_error = true;
8014 break;
8016 if (keyword == RID_SETTER)
8018 if (property_setter_ident != NULL_TREE)
8019 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8020 else
8021 property_setter_ident = c_parser_peek_token (parser)->value;
8022 c_parser_consume_token (parser);
8023 if (c_parser_next_token_is_not (parser, CPP_COLON))
8024 c_parser_error (parser, "setter name must terminate with %<:%>");
8025 else
8026 c_parser_consume_token (parser);
8028 else
8030 if (property_getter_ident != NULL_TREE)
8031 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8032 else
8033 property_getter_ident = c_parser_peek_token (parser)->value;
8034 c_parser_consume_token (parser);
8036 break;
8037 default:
8038 c_parser_error (parser, "unknown property attribute");
8039 syntax_error = true;
8040 break;
8043 if (syntax_error)
8044 break;
8046 if (c_parser_next_token_is (parser, CPP_COMMA))
8047 c_parser_consume_token (parser);
8048 else
8049 break;
8051 parser->objc_property_attr_context = false;
8052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8054 /* ... and the property declaration(s). */
8055 properties = c_parser_struct_declaration (parser);
8057 if (properties == error_mark_node)
8059 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8060 parser->error = false;
8061 return;
8064 if (properties == NULL_TREE)
8065 c_parser_error (parser, "expected identifier");
8066 else
8068 /* Comma-separated properties are chained together in
8069 reverse order; add them one by one. */
8070 properties = nreverse (properties);
8072 for (; properties; properties = TREE_CHAIN (properties))
8073 objc_add_property_declaration (loc, copy_node (properties),
8074 property_readonly, property_readwrite,
8075 property_assign, property_retain,
8076 property_copy, property_nonatomic,
8077 property_getter_ident, property_setter_ident);
8080 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8081 parser->error = false;
8084 /* Parse an Objective-C @synthesize declaration. The syntax is:
8086 objc-synthesize-declaration:
8087 @synthesize objc-synthesize-identifier-list ;
8089 objc-synthesize-identifier-list:
8090 objc-synthesize-identifier
8091 objc-synthesize-identifier-list, objc-synthesize-identifier
8093 objc-synthesize-identifier
8094 identifier
8095 identifier = identifier
8097 For example:
8098 @synthesize MyProperty;
8099 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8101 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8102 for C++. Keep them in sync.
8104 static void
8105 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8107 tree list = NULL_TREE;
8108 location_t loc;
8109 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8110 loc = c_parser_peek_token (parser)->location;
8112 c_parser_consume_token (parser);
8113 while (true)
8115 tree property, ivar;
8116 if (c_parser_next_token_is_not (parser, CPP_NAME))
8118 c_parser_error (parser, "expected identifier");
8119 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8120 /* Once we find the semicolon, we can resume normal parsing.
8121 We have to reset parser->error manually because
8122 c_parser_skip_until_found() won't reset it for us if the
8123 next token is precisely a semicolon. */
8124 parser->error = false;
8125 return;
8127 property = c_parser_peek_token (parser)->value;
8128 c_parser_consume_token (parser);
8129 if (c_parser_next_token_is (parser, CPP_EQ))
8131 c_parser_consume_token (parser);
8132 if (c_parser_next_token_is_not (parser, CPP_NAME))
8134 c_parser_error (parser, "expected identifier");
8135 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8136 parser->error = false;
8137 return;
8139 ivar = c_parser_peek_token (parser)->value;
8140 c_parser_consume_token (parser);
8142 else
8143 ivar = NULL_TREE;
8144 list = chainon (list, build_tree_list (ivar, property));
8145 if (c_parser_next_token_is (parser, CPP_COMMA))
8146 c_parser_consume_token (parser);
8147 else
8148 break;
8150 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8151 objc_add_synthesize_declaration (loc, list);
8154 /* Parse an Objective-C @dynamic declaration. The syntax is:
8156 objc-dynamic-declaration:
8157 @dynamic identifier-list ;
8159 For example:
8160 @dynamic MyProperty;
8161 @dynamic MyProperty, AnotherProperty;
8163 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8164 for C++. Keep them in sync.
8166 static void
8167 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8169 tree list = NULL_TREE;
8170 location_t loc;
8171 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8172 loc = c_parser_peek_token (parser)->location;
8174 c_parser_consume_token (parser);
8175 while (true)
8177 tree property;
8178 if (c_parser_next_token_is_not (parser, CPP_NAME))
8180 c_parser_error (parser, "expected identifier");
8181 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8182 parser->error = false;
8183 return;
8185 property = c_parser_peek_token (parser)->value;
8186 list = chainon (list, build_tree_list (NULL_TREE, property));
8187 c_parser_consume_token (parser);
8188 if (c_parser_next_token_is (parser, CPP_COMMA))
8189 c_parser_consume_token (parser);
8190 else
8191 break;
8193 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8194 objc_add_dynamic_declaration (loc, list);
8198 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8199 should be considered, statements. ALLOW_STMT is true if we're within
8200 the context of a function and such pragmas are to be allowed. Returns
8201 true if we actually parsed such a pragma. */
8203 static bool
8204 c_parser_pragma (c_parser *parser, enum pragma_context context)
8206 unsigned int id;
8208 id = c_parser_peek_token (parser)->pragma_kind;
8209 gcc_assert (id != PRAGMA_NONE);
8211 switch (id)
8213 case PRAGMA_OMP_BARRIER:
8214 if (context != pragma_compound)
8216 if (context == pragma_stmt)
8217 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8218 "used in compound statements");
8219 goto bad_stmt;
8221 c_parser_omp_barrier (parser);
8222 return false;
8224 case PRAGMA_OMP_FLUSH:
8225 if (context != pragma_compound)
8227 if (context == pragma_stmt)
8228 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8229 "used in compound statements");
8230 goto bad_stmt;
8232 c_parser_omp_flush (parser);
8233 return false;
8235 case PRAGMA_OMP_TASKWAIT:
8236 if (context != pragma_compound)
8238 if (context == pragma_stmt)
8239 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8240 "used in compound statements");
8241 goto bad_stmt;
8243 c_parser_omp_taskwait (parser);
8244 return false;
8246 case PRAGMA_OMP_THREADPRIVATE:
8247 c_parser_omp_threadprivate (parser);
8248 return false;
8250 case PRAGMA_OMP_SECTION:
8251 error_at (c_parser_peek_token (parser)->location,
8252 "%<#pragma omp section%> may only be used in "
8253 "%<#pragma omp sections%> construct");
8254 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8255 return false;
8257 case PRAGMA_GCC_PCH_PREPROCESS:
8258 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8259 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8260 return false;
8262 default:
8263 if (id < PRAGMA_FIRST_EXTERNAL)
8265 if (context == pragma_external)
8267 bad_stmt:
8268 c_parser_error (parser, "expected declaration specifiers");
8269 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8270 return false;
8272 c_parser_omp_construct (parser);
8273 return true;
8275 break;
8278 c_parser_consume_pragma (parser);
8279 c_invoke_pragma_handler (id);
8281 /* Skip to EOL, but suppress any error message. Those will have been
8282 generated by the handler routine through calling error, as opposed
8283 to calling c_parser_error. */
8284 parser->error = true;
8285 c_parser_skip_to_pragma_eol (parser);
8287 return false;
8290 /* The interface the pragma parsers have to the lexer. */
8292 enum cpp_ttype
8293 pragma_lex (tree *value)
8295 c_token *tok = c_parser_peek_token (the_parser);
8296 enum cpp_ttype ret = tok->type;
8298 *value = tok->value;
8299 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8300 ret = CPP_EOF;
8301 else
8303 if (ret == CPP_KEYWORD)
8304 ret = CPP_NAME;
8305 c_parser_consume_token (the_parser);
8308 return ret;
8311 static void
8312 c_parser_pragma_pch_preprocess (c_parser *parser)
8314 tree name = NULL;
8316 c_parser_consume_pragma (parser);
8317 if (c_parser_next_token_is (parser, CPP_STRING))
8319 name = c_parser_peek_token (parser)->value;
8320 c_parser_consume_token (parser);
8322 else
8323 c_parser_error (parser, "expected string literal");
8324 c_parser_skip_to_pragma_eol (parser);
8326 if (name)
8327 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8330 /* OpenMP 2.5 parsing routines. */
8332 /* Returns name of the next clause.
8333 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8334 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8335 returned and the token is consumed. */
8337 static pragma_omp_clause
8338 c_parser_omp_clause_name (c_parser *parser)
8340 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8342 if (c_parser_next_token_is_keyword (parser, RID_IF))
8343 result = PRAGMA_OMP_CLAUSE_IF;
8344 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8345 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8346 else if (c_parser_next_token_is (parser, CPP_NAME))
8348 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8350 switch (p[0])
8352 case 'c':
8353 if (!strcmp ("collapse", p))
8354 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8355 else if (!strcmp ("copyin", p))
8356 result = PRAGMA_OMP_CLAUSE_COPYIN;
8357 else if (!strcmp ("copyprivate", p))
8358 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8359 break;
8360 case 'f':
8361 if (!strcmp ("firstprivate", p))
8362 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8363 break;
8364 case 'l':
8365 if (!strcmp ("lastprivate", p))
8366 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8367 break;
8368 case 'n':
8369 if (!strcmp ("nowait", p))
8370 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8371 else if (!strcmp ("num_threads", p))
8372 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8373 break;
8374 case 'o':
8375 if (!strcmp ("ordered", p))
8376 result = PRAGMA_OMP_CLAUSE_ORDERED;
8377 break;
8378 case 'p':
8379 if (!strcmp ("private", p))
8380 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8381 break;
8382 case 'r':
8383 if (!strcmp ("reduction", p))
8384 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8385 break;
8386 case 's':
8387 if (!strcmp ("schedule", p))
8388 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8389 else if (!strcmp ("shared", p))
8390 result = PRAGMA_OMP_CLAUSE_SHARED;
8391 break;
8392 case 'u':
8393 if (!strcmp ("untied", p))
8394 result = PRAGMA_OMP_CLAUSE_UNTIED;
8395 break;
8399 if (result != PRAGMA_OMP_CLAUSE_NONE)
8400 c_parser_consume_token (parser);
8402 return result;
8405 /* Validate that a clause of the given type does not already exist. */
8407 static void
8408 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8409 const char *name)
8411 tree c;
8413 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8414 if (OMP_CLAUSE_CODE (c) == code)
8416 location_t loc = OMP_CLAUSE_LOCATION (c);
8417 error_at (loc, "too many %qs clauses", name);
8418 break;
8422 /* OpenMP 2.5:
8423 variable-list:
8424 identifier
8425 variable-list , identifier
8427 If KIND is nonzero, create the appropriate node and install the
8428 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8429 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8431 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8432 return the list created. */
8434 static tree
8435 c_parser_omp_variable_list (c_parser *parser,
8436 location_t clause_loc,
8437 enum omp_clause_code kind,
8438 tree list)
8440 if (c_parser_next_token_is_not (parser, CPP_NAME)
8441 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8442 c_parser_error (parser, "expected identifier");
8444 while (c_parser_next_token_is (parser, CPP_NAME)
8445 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8447 tree t = lookup_name (c_parser_peek_token (parser)->value);
8449 if (t == NULL_TREE)
8450 undeclared_variable (c_parser_peek_token (parser)->location,
8451 c_parser_peek_token (parser)->value);
8452 else if (t == error_mark_node)
8454 else if (kind != 0)
8456 tree u = build_omp_clause (clause_loc, kind);
8457 OMP_CLAUSE_DECL (u) = t;
8458 OMP_CLAUSE_CHAIN (u) = list;
8459 list = u;
8461 else
8462 list = tree_cons (t, NULL_TREE, list);
8464 c_parser_consume_token (parser);
8466 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8467 break;
8469 c_parser_consume_token (parser);
8472 return list;
8475 /* Similarly, but expect leading and trailing parenthesis. This is a very
8476 common case for omp clauses. */
8478 static tree
8479 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8480 tree list)
8482 /* The clauses location. */
8483 location_t loc = c_parser_peek_token (parser)->location;
8485 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8487 list = c_parser_omp_variable_list (parser, loc, kind, list);
8488 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8490 return list;
8493 /* OpenMP 3.0:
8494 collapse ( constant-expression ) */
8496 static tree
8497 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8499 tree c, num = error_mark_node;
8500 HOST_WIDE_INT n;
8501 location_t loc;
8503 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8505 loc = c_parser_peek_token (parser)->location;
8506 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8508 num = c_parser_expr_no_commas (parser, NULL).value;
8509 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8511 if (num == error_mark_node)
8512 return list;
8513 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8514 || !host_integerp (num, 0)
8515 || (n = tree_low_cst (num, 0)) <= 0
8516 || (int) n != n)
8518 error_at (loc,
8519 "collapse argument needs positive constant integer expression");
8520 return list;
8522 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8523 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8524 OMP_CLAUSE_CHAIN (c) = list;
8525 return c;
8528 /* OpenMP 2.5:
8529 copyin ( variable-list ) */
8531 static tree
8532 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8534 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8537 /* OpenMP 2.5:
8538 copyprivate ( variable-list ) */
8540 static tree
8541 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8543 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8546 /* OpenMP 2.5:
8547 default ( shared | none ) */
8549 static tree
8550 c_parser_omp_clause_default (c_parser *parser, tree list)
8552 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8553 location_t loc = c_parser_peek_token (parser)->location;
8554 tree c;
8556 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8557 return list;
8558 if (c_parser_next_token_is (parser, CPP_NAME))
8560 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8562 switch (p[0])
8564 case 'n':
8565 if (strcmp ("none", p) != 0)
8566 goto invalid_kind;
8567 kind = OMP_CLAUSE_DEFAULT_NONE;
8568 break;
8570 case 's':
8571 if (strcmp ("shared", p) != 0)
8572 goto invalid_kind;
8573 kind = OMP_CLAUSE_DEFAULT_SHARED;
8574 break;
8576 default:
8577 goto invalid_kind;
8580 c_parser_consume_token (parser);
8582 else
8584 invalid_kind:
8585 c_parser_error (parser, "expected %<none%> or %<shared%>");
8587 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8589 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8590 return list;
8592 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8593 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8594 OMP_CLAUSE_CHAIN (c) = list;
8595 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8597 return c;
8600 /* OpenMP 2.5:
8601 firstprivate ( variable-list ) */
8603 static tree
8604 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8606 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8609 /* OpenMP 2.5:
8610 if ( expression ) */
8612 static tree
8613 c_parser_omp_clause_if (c_parser *parser, tree list)
8615 location_t loc = c_parser_peek_token (parser)->location;
8616 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8618 tree t = c_parser_paren_condition (parser);
8619 tree c;
8621 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8623 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8624 OMP_CLAUSE_IF_EXPR (c) = t;
8625 OMP_CLAUSE_CHAIN (c) = list;
8626 list = c;
8628 else
8629 c_parser_error (parser, "expected %<(%>");
8631 return list;
8634 /* OpenMP 2.5:
8635 lastprivate ( variable-list ) */
8637 static tree
8638 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8640 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8643 /* OpenMP 2.5:
8644 nowait */
8646 static tree
8647 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8649 tree c;
8650 location_t loc = c_parser_peek_token (parser)->location;
8652 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8654 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8655 OMP_CLAUSE_CHAIN (c) = list;
8656 return c;
8659 /* OpenMP 2.5:
8660 num_threads ( expression ) */
8662 static tree
8663 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8665 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8666 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8668 location_t expr_loc = c_parser_peek_token (parser)->location;
8669 tree c, t = c_parser_expression (parser).value;
8670 t = c_fully_fold (t, false, NULL);
8672 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8674 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8676 c_parser_error (parser, "expected integer expression");
8677 return list;
8680 /* Attempt to statically determine when the number isn't positive. */
8681 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8682 build_int_cst (TREE_TYPE (t), 0));
8683 if (CAN_HAVE_LOCATION_P (c))
8684 SET_EXPR_LOCATION (c, expr_loc);
8685 if (c == boolean_true_node)
8687 warning_at (expr_loc, 0,
8688 "%<num_threads%> value must be positive");
8689 t = integer_one_node;
8692 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8694 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8695 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8696 OMP_CLAUSE_CHAIN (c) = list;
8697 list = c;
8700 return list;
8703 /* OpenMP 2.5:
8704 ordered */
8706 static tree
8707 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8709 tree c;
8711 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8713 c = build_omp_clause (c_parser_peek_token (parser)->location,
8714 OMP_CLAUSE_ORDERED);
8715 OMP_CLAUSE_CHAIN (c) = list;
8717 return c;
8720 /* OpenMP 2.5:
8721 private ( variable-list ) */
8723 static tree
8724 c_parser_omp_clause_private (c_parser *parser, tree list)
8726 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8729 /* OpenMP 2.5:
8730 reduction ( reduction-operator : variable-list )
8732 reduction-operator:
8733 One of: + * - & ^ | && || */
8735 static tree
8736 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8738 location_t clause_loc = c_parser_peek_token (parser)->location;
8739 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8741 enum tree_code code;
8743 switch (c_parser_peek_token (parser)->type)
8745 case CPP_PLUS:
8746 code = PLUS_EXPR;
8747 break;
8748 case CPP_MULT:
8749 code = MULT_EXPR;
8750 break;
8751 case CPP_MINUS:
8752 code = MINUS_EXPR;
8753 break;
8754 case CPP_AND:
8755 code = BIT_AND_EXPR;
8756 break;
8757 case CPP_XOR:
8758 code = BIT_XOR_EXPR;
8759 break;
8760 case CPP_OR:
8761 code = BIT_IOR_EXPR;
8762 break;
8763 case CPP_AND_AND:
8764 code = TRUTH_ANDIF_EXPR;
8765 break;
8766 case CPP_OR_OR:
8767 code = TRUTH_ORIF_EXPR;
8768 break;
8769 default:
8770 c_parser_error (parser,
8771 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8772 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8773 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8774 return list;
8776 c_parser_consume_token (parser);
8777 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8779 tree nl, c;
8781 nl = c_parser_omp_variable_list (parser, clause_loc,
8782 OMP_CLAUSE_REDUCTION, list);
8783 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8784 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8786 list = nl;
8788 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8790 return list;
8793 /* OpenMP 2.5:
8794 schedule ( schedule-kind )
8795 schedule ( schedule-kind , expression )
8797 schedule-kind:
8798 static | dynamic | guided | runtime | auto
8801 static tree
8802 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8804 tree c, t;
8805 location_t loc = c_parser_peek_token (parser)->location;
8807 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8808 return list;
8810 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8812 if (c_parser_next_token_is (parser, CPP_NAME))
8814 tree kind = c_parser_peek_token (parser)->value;
8815 const char *p = IDENTIFIER_POINTER (kind);
8817 switch (p[0])
8819 case 'd':
8820 if (strcmp ("dynamic", p) != 0)
8821 goto invalid_kind;
8822 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8823 break;
8825 case 'g':
8826 if (strcmp ("guided", p) != 0)
8827 goto invalid_kind;
8828 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8829 break;
8831 case 'r':
8832 if (strcmp ("runtime", p) != 0)
8833 goto invalid_kind;
8834 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8835 break;
8837 default:
8838 goto invalid_kind;
8841 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8842 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8843 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8844 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8845 else
8846 goto invalid_kind;
8848 c_parser_consume_token (parser);
8849 if (c_parser_next_token_is (parser, CPP_COMMA))
8851 location_t here;
8852 c_parser_consume_token (parser);
8854 here = c_parser_peek_token (parser)->location;
8855 t = c_parser_expr_no_commas (parser, NULL).value;
8856 t = c_fully_fold (t, false, NULL);
8858 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8859 error_at (here, "schedule %<runtime%> does not take "
8860 "a %<chunk_size%> parameter");
8861 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8862 error_at (here,
8863 "schedule %<auto%> does not take "
8864 "a %<chunk_size%> parameter");
8865 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8866 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8867 else
8868 c_parser_error (parser, "expected integer expression");
8870 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8872 else
8873 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8874 "expected %<,%> or %<)%>");
8876 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8877 OMP_CLAUSE_CHAIN (c) = list;
8878 return c;
8880 invalid_kind:
8881 c_parser_error (parser, "invalid schedule kind");
8882 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8883 return list;
8886 /* OpenMP 2.5:
8887 shared ( variable-list ) */
8889 static tree
8890 c_parser_omp_clause_shared (c_parser *parser, tree list)
8892 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8895 /* OpenMP 3.0:
8896 untied */
8898 static tree
8899 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8901 tree c;
8903 /* FIXME: Should we allow duplicates? */
8904 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8906 c = build_omp_clause (c_parser_peek_token (parser)->location,
8907 OMP_CLAUSE_UNTIED);
8908 OMP_CLAUSE_CHAIN (c) = list;
8910 return c;
8913 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8914 is a bitmask in MASK. Return the list of clauses found; the result
8915 of clause default goes in *pdefault. */
8917 static tree
8918 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8919 const char *where)
8921 tree clauses = NULL;
8922 bool first = true;
8924 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8926 location_t here;
8927 pragma_omp_clause c_kind;
8928 const char *c_name;
8929 tree prev = clauses;
8931 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8932 c_parser_consume_token (parser);
8934 first = false;
8935 here = c_parser_peek_token (parser)->location;
8936 c_kind = c_parser_omp_clause_name (parser);
8938 switch (c_kind)
8940 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8941 clauses = c_parser_omp_clause_collapse (parser, clauses);
8942 c_name = "collapse";
8943 break;
8944 case PRAGMA_OMP_CLAUSE_COPYIN:
8945 clauses = c_parser_omp_clause_copyin (parser, clauses);
8946 c_name = "copyin";
8947 break;
8948 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8949 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8950 c_name = "copyprivate";
8951 break;
8952 case PRAGMA_OMP_CLAUSE_DEFAULT:
8953 clauses = c_parser_omp_clause_default (parser, clauses);
8954 c_name = "default";
8955 break;
8956 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8957 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8958 c_name = "firstprivate";
8959 break;
8960 case PRAGMA_OMP_CLAUSE_IF:
8961 clauses = c_parser_omp_clause_if (parser, clauses);
8962 c_name = "if";
8963 break;
8964 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8965 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8966 c_name = "lastprivate";
8967 break;
8968 case PRAGMA_OMP_CLAUSE_NOWAIT:
8969 clauses = c_parser_omp_clause_nowait (parser, clauses);
8970 c_name = "nowait";
8971 break;
8972 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8973 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8974 c_name = "num_threads";
8975 break;
8976 case PRAGMA_OMP_CLAUSE_ORDERED:
8977 clauses = c_parser_omp_clause_ordered (parser, clauses);
8978 c_name = "ordered";
8979 break;
8980 case PRAGMA_OMP_CLAUSE_PRIVATE:
8981 clauses = c_parser_omp_clause_private (parser, clauses);
8982 c_name = "private";
8983 break;
8984 case PRAGMA_OMP_CLAUSE_REDUCTION:
8985 clauses = c_parser_omp_clause_reduction (parser, clauses);
8986 c_name = "reduction";
8987 break;
8988 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8989 clauses = c_parser_omp_clause_schedule (parser, clauses);
8990 c_name = "schedule";
8991 break;
8992 case PRAGMA_OMP_CLAUSE_SHARED:
8993 clauses = c_parser_omp_clause_shared (parser, clauses);
8994 c_name = "shared";
8995 break;
8996 case PRAGMA_OMP_CLAUSE_UNTIED:
8997 clauses = c_parser_omp_clause_untied (parser, clauses);
8998 c_name = "untied";
8999 break;
9000 default:
9001 c_parser_error (parser, "expected %<#pragma omp%> clause");
9002 goto saw_error;
9005 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9007 /* Remove the invalid clause(s) from the list to avoid
9008 confusing the rest of the compiler. */
9009 clauses = prev;
9010 error_at (here, "%qs is not valid for %qs", c_name, where);
9014 saw_error:
9015 c_parser_skip_to_pragma_eol (parser);
9017 return c_finish_omp_clauses (clauses);
9020 /* OpenMP 2.5:
9021 structured-block:
9022 statement
9024 In practice, we're also interested in adding the statement to an
9025 outer node. So it is convenient if we work around the fact that
9026 c_parser_statement calls add_stmt. */
9028 static tree
9029 c_parser_omp_structured_block (c_parser *parser)
9031 tree stmt = push_stmt_list ();
9032 c_parser_statement (parser);
9033 return pop_stmt_list (stmt);
9036 /* OpenMP 2.5:
9037 # pragma omp atomic new-line
9038 expression-stmt
9040 expression-stmt:
9041 x binop= expr | x++ | ++x | x-- | --x
9042 binop:
9043 +, *, -, /, &, ^, |, <<, >>
9045 where x is an lvalue expression with scalar type.
9047 LOC is the location of the #pragma token. */
9049 static void
9050 c_parser_omp_atomic (location_t loc, c_parser *parser)
9052 tree lhs, rhs;
9053 tree stmt;
9054 enum tree_code code;
9055 struct c_expr rhs_expr;
9057 c_parser_skip_to_pragma_eol (parser);
9059 lhs = c_parser_unary_expression (parser).value;
9060 lhs = c_fully_fold (lhs, false, NULL);
9061 switch (TREE_CODE (lhs))
9063 case ERROR_MARK:
9064 saw_error:
9065 c_parser_skip_to_end_of_block_or_statement (parser);
9066 return;
9068 case PREINCREMENT_EXPR:
9069 case POSTINCREMENT_EXPR:
9070 lhs = TREE_OPERAND (lhs, 0);
9071 code = PLUS_EXPR;
9072 rhs = integer_one_node;
9073 break;
9075 case PREDECREMENT_EXPR:
9076 case POSTDECREMENT_EXPR:
9077 lhs = TREE_OPERAND (lhs, 0);
9078 code = MINUS_EXPR;
9079 rhs = integer_one_node;
9080 break;
9082 case COMPOUND_EXPR:
9083 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9084 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9085 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9086 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9087 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9088 (TREE_OPERAND (lhs, 1), 0), 0)))
9089 == BOOLEAN_TYPE)
9090 /* Undo effects of boolean_increment for post {in,de}crement. */
9091 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9092 /* FALLTHRU */
9093 case MODIFY_EXPR:
9094 if (TREE_CODE (lhs) == MODIFY_EXPR
9095 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9097 /* Undo effects of boolean_increment. */
9098 if (integer_onep (TREE_OPERAND (lhs, 1)))
9100 /* This is pre or post increment. */
9101 rhs = TREE_OPERAND (lhs, 1);
9102 lhs = TREE_OPERAND (lhs, 0);
9103 code = NOP_EXPR;
9104 break;
9106 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9107 && TREE_OPERAND (lhs, 0)
9108 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9110 /* This is pre or post decrement. */
9111 rhs = TREE_OPERAND (lhs, 1);
9112 lhs = TREE_OPERAND (lhs, 0);
9113 code = NOP_EXPR;
9114 break;
9117 /* FALLTHRU */
9118 default:
9119 switch (c_parser_peek_token (parser)->type)
9121 case CPP_MULT_EQ:
9122 code = MULT_EXPR;
9123 break;
9124 case CPP_DIV_EQ:
9125 code = TRUNC_DIV_EXPR;
9126 break;
9127 case CPP_PLUS_EQ:
9128 code = PLUS_EXPR;
9129 break;
9130 case CPP_MINUS_EQ:
9131 code = MINUS_EXPR;
9132 break;
9133 case CPP_LSHIFT_EQ:
9134 code = LSHIFT_EXPR;
9135 break;
9136 case CPP_RSHIFT_EQ:
9137 code = RSHIFT_EXPR;
9138 break;
9139 case CPP_AND_EQ:
9140 code = BIT_AND_EXPR;
9141 break;
9142 case CPP_OR_EQ:
9143 code = BIT_IOR_EXPR;
9144 break;
9145 case CPP_XOR_EQ:
9146 code = BIT_XOR_EXPR;
9147 break;
9148 default:
9149 c_parser_error (parser,
9150 "invalid operator for %<#pragma omp atomic%>");
9151 goto saw_error;
9154 /* Arrange to pass the location of the assignment operator to
9155 c_finish_omp_atomic. */
9156 loc = c_parser_peek_token (parser)->location;
9157 c_parser_consume_token (parser);
9159 location_t rhs_loc = c_parser_peek_token (parser)->location;
9160 rhs_expr = c_parser_expression (parser);
9161 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9163 rhs = rhs_expr.value;
9164 rhs = c_fully_fold (rhs, false, NULL);
9165 break;
9167 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
9168 if (stmt != error_mark_node)
9169 add_stmt (stmt);
9170 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9174 /* OpenMP 2.5:
9175 # pragma omp barrier new-line
9178 static void
9179 c_parser_omp_barrier (c_parser *parser)
9181 location_t loc = c_parser_peek_token (parser)->location;
9182 c_parser_consume_pragma (parser);
9183 c_parser_skip_to_pragma_eol (parser);
9185 c_finish_omp_barrier (loc);
9188 /* OpenMP 2.5:
9189 # pragma omp critical [(name)] new-line
9190 structured-block
9192 LOC is the location of the #pragma itself. */
9194 static tree
9195 c_parser_omp_critical (location_t loc, c_parser *parser)
9197 tree stmt, name = NULL;
9199 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9201 c_parser_consume_token (parser);
9202 if (c_parser_next_token_is (parser, CPP_NAME))
9204 name = c_parser_peek_token (parser)->value;
9205 c_parser_consume_token (parser);
9206 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9208 else
9209 c_parser_error (parser, "expected identifier");
9211 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9212 c_parser_error (parser, "expected %<(%> or end of line");
9213 c_parser_skip_to_pragma_eol (parser);
9215 stmt = c_parser_omp_structured_block (parser);
9216 return c_finish_omp_critical (loc, stmt, name);
9219 /* OpenMP 2.5:
9220 # pragma omp flush flush-vars[opt] new-line
9222 flush-vars:
9223 ( variable-list ) */
9225 static void
9226 c_parser_omp_flush (c_parser *parser)
9228 location_t loc = c_parser_peek_token (parser)->location;
9229 c_parser_consume_pragma (parser);
9230 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9231 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9232 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9233 c_parser_error (parser, "expected %<(%> or end of line");
9234 c_parser_skip_to_pragma_eol (parser);
9236 c_finish_omp_flush (loc);
9239 /* Parse the restricted form of the for statement allowed by OpenMP.
9240 The real trick here is to determine the loop control variable early
9241 so that we can push a new decl if necessary to make it private.
9242 LOC is the location of the OMP in "#pragma omp". */
9244 static tree
9245 c_parser_omp_for_loop (location_t loc,
9246 c_parser *parser, tree clauses, tree *par_clauses)
9248 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9249 tree declv, condv, incrv, initv, ret = NULL;
9250 bool fail = false, open_brace_parsed = false;
9251 int i, collapse = 1, nbraces = 0;
9252 location_t for_loc;
9253 VEC(tree,gc) *for_block = make_tree_vector ();
9255 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9256 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9257 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9259 gcc_assert (collapse >= 1);
9261 declv = make_tree_vec (collapse);
9262 initv = make_tree_vec (collapse);
9263 condv = make_tree_vec (collapse);
9264 incrv = make_tree_vec (collapse);
9266 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9268 c_parser_error (parser, "for statement expected");
9269 return NULL;
9271 for_loc = c_parser_peek_token (parser)->location;
9272 c_parser_consume_token (parser);
9274 for (i = 0; i < collapse; i++)
9276 int bracecount = 0;
9278 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9279 goto pop_scopes;
9281 /* Parse the initialization declaration or expression. */
9282 if (c_parser_next_tokens_start_declaration (parser))
9284 if (i > 0)
9285 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9286 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9287 decl = check_for_loop_decls (for_loc, flag_isoc99);
9288 if (decl == NULL)
9289 goto error_init;
9290 if (DECL_INITIAL (decl) == error_mark_node)
9291 decl = error_mark_node;
9292 init = decl;
9294 else if (c_parser_next_token_is (parser, CPP_NAME)
9295 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9297 struct c_expr decl_exp;
9298 struct c_expr init_exp;
9299 location_t init_loc;
9301 decl_exp = c_parser_postfix_expression (parser);
9302 decl = decl_exp.value;
9304 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9306 init_loc = c_parser_peek_token (parser)->location;
9307 init_exp = c_parser_expr_no_commas (parser, NULL);
9308 init_exp = default_function_array_read_conversion (init_loc,
9309 init_exp);
9310 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9311 NOP_EXPR, init_loc, init_exp.value,
9312 init_exp.original_type);
9313 init = c_process_expr_stmt (init_loc, init);
9315 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9317 else
9319 error_init:
9320 c_parser_error (parser,
9321 "expected iteration declaration or initialization");
9322 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9323 "expected %<)%>");
9324 fail = true;
9325 goto parse_next;
9328 /* Parse the loop condition. */
9329 cond = NULL_TREE;
9330 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9332 location_t cond_loc = c_parser_peek_token (parser)->location;
9333 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9335 cond = cond_expr.value;
9336 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9337 cond = c_fully_fold (cond, false, NULL);
9338 switch (cond_expr.original_code)
9340 case GT_EXPR:
9341 case GE_EXPR:
9342 case LT_EXPR:
9343 case LE_EXPR:
9344 break;
9345 default:
9346 /* Can't be cond = error_mark_node, because we want to preserve
9347 the location until c_finish_omp_for. */
9348 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9349 break;
9351 protected_set_expr_location (cond, cond_loc);
9353 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9355 /* Parse the increment expression. */
9356 incr = NULL_TREE;
9357 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9359 location_t incr_loc = c_parser_peek_token (parser)->location;
9361 incr = c_process_expr_stmt (incr_loc,
9362 c_parser_expression (parser).value);
9364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9366 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9367 fail = true;
9368 else
9370 TREE_VEC_ELT (declv, i) = decl;
9371 TREE_VEC_ELT (initv, i) = init;
9372 TREE_VEC_ELT (condv, i) = cond;
9373 TREE_VEC_ELT (incrv, i) = incr;
9376 parse_next:
9377 if (i == collapse - 1)
9378 break;
9380 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9381 in between the collapsed for loops to be still considered perfectly
9382 nested. Hopefully the final version clarifies this.
9383 For now handle (multiple) {'s and empty statements. */
9386 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9388 c_parser_consume_token (parser);
9389 break;
9391 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9393 c_parser_consume_token (parser);
9394 bracecount++;
9396 else if (bracecount
9397 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9398 c_parser_consume_token (parser);
9399 else
9401 c_parser_error (parser, "not enough perfectly nested loops");
9402 if (bracecount)
9404 open_brace_parsed = true;
9405 bracecount--;
9407 fail = true;
9408 collapse = 0;
9409 break;
9412 while (1);
9414 nbraces += bracecount;
9417 save_break = c_break_label;
9418 c_break_label = size_one_node;
9419 save_cont = c_cont_label;
9420 c_cont_label = NULL_TREE;
9421 body = push_stmt_list ();
9423 if (open_brace_parsed)
9425 location_t here = c_parser_peek_token (parser)->location;
9426 stmt = c_begin_compound_stmt (true);
9427 c_parser_compound_statement_nostart (parser);
9428 add_stmt (c_end_compound_stmt (here, stmt, true));
9430 else
9431 add_stmt (c_parser_c99_block_statement (parser));
9432 if (c_cont_label)
9434 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9435 SET_EXPR_LOCATION (t, loc);
9436 add_stmt (t);
9439 body = pop_stmt_list (body);
9440 c_break_label = save_break;
9441 c_cont_label = save_cont;
9443 while (nbraces)
9445 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9447 c_parser_consume_token (parser);
9448 nbraces--;
9450 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9451 c_parser_consume_token (parser);
9452 else
9454 c_parser_error (parser, "collapsed loops not perfectly nested");
9455 while (nbraces)
9457 location_t here = c_parser_peek_token (parser)->location;
9458 stmt = c_begin_compound_stmt (true);
9459 add_stmt (body);
9460 c_parser_compound_statement_nostart (parser);
9461 body = c_end_compound_stmt (here, stmt, true);
9462 nbraces--;
9464 goto pop_scopes;
9468 /* Only bother calling c_finish_omp_for if we haven't already generated
9469 an error from the initialization parsing. */
9470 if (!fail)
9472 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9473 if (stmt)
9475 if (par_clauses != NULL)
9477 tree *c;
9478 for (c = par_clauses; *c ; )
9479 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9480 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9481 c = &OMP_CLAUSE_CHAIN (*c);
9482 else
9484 for (i = 0; i < collapse; i++)
9485 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9486 break;
9487 if (i == collapse)
9488 c = &OMP_CLAUSE_CHAIN (*c);
9489 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9491 error_at (loc,
9492 "iteration variable %qD should not be firstprivate",
9493 OMP_CLAUSE_DECL (*c));
9494 *c = OMP_CLAUSE_CHAIN (*c);
9496 else
9498 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9499 change it to shared (decl) in
9500 OMP_PARALLEL_CLAUSES. */
9501 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9502 OMP_CLAUSE_LASTPRIVATE);
9503 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9504 OMP_CLAUSE_CHAIN (l) = clauses;
9505 clauses = l;
9506 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9510 OMP_FOR_CLAUSES (stmt) = clauses;
9512 ret = stmt;
9514 pop_scopes:
9515 while (!VEC_empty (tree, for_block))
9517 /* FIXME diagnostics: LOC below should be the actual location of
9518 this particular for block. We need to build a list of
9519 locations to go along with FOR_BLOCK. */
9520 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9521 add_stmt (stmt);
9523 release_tree_vector (for_block);
9524 return ret;
9527 /* OpenMP 2.5:
9528 #pragma omp for for-clause[optseq] new-line
9529 for-loop
9531 LOC is the location of the #pragma token.
9534 #define OMP_FOR_CLAUSE_MASK \
9535 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9536 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9537 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9538 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9539 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9540 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9541 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9542 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9544 static tree
9545 c_parser_omp_for (location_t loc, c_parser *parser)
9547 tree block, clauses, ret;
9549 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9550 "#pragma omp for");
9552 block = c_begin_compound_stmt (true);
9553 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9554 block = c_end_compound_stmt (loc, block, true);
9555 add_stmt (block);
9557 return ret;
9560 /* OpenMP 2.5:
9561 # pragma omp master new-line
9562 structured-block
9564 LOC is the location of the #pragma token.
9567 static tree
9568 c_parser_omp_master (location_t loc, c_parser *parser)
9570 c_parser_skip_to_pragma_eol (parser);
9571 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9574 /* OpenMP 2.5:
9575 # pragma omp ordered new-line
9576 structured-block
9578 LOC is the location of the #pragma itself.
9581 static tree
9582 c_parser_omp_ordered (location_t loc, c_parser *parser)
9584 c_parser_skip_to_pragma_eol (parser);
9585 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9588 /* OpenMP 2.5:
9590 section-scope:
9591 { section-sequence }
9593 section-sequence:
9594 section-directive[opt] structured-block
9595 section-sequence section-directive structured-block
9597 SECTIONS_LOC is the location of the #pragma omp sections. */
9599 static tree
9600 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9602 tree stmt, substmt;
9603 bool error_suppress = false;
9604 location_t loc;
9606 loc = c_parser_peek_token (parser)->location;
9607 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9609 /* Avoid skipping until the end of the block. */
9610 parser->error = false;
9611 return NULL_TREE;
9614 stmt = push_stmt_list ();
9616 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9618 substmt = push_stmt_list ();
9620 while (1)
9622 c_parser_statement (parser);
9624 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9625 break;
9626 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9627 break;
9628 if (c_parser_next_token_is (parser, CPP_EOF))
9629 break;
9632 substmt = pop_stmt_list (substmt);
9633 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9634 SET_EXPR_LOCATION (substmt, loc);
9635 add_stmt (substmt);
9638 while (1)
9640 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9641 break;
9642 if (c_parser_next_token_is (parser, CPP_EOF))
9643 break;
9645 loc = c_parser_peek_token (parser)->location;
9646 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9648 c_parser_consume_pragma (parser);
9649 c_parser_skip_to_pragma_eol (parser);
9650 error_suppress = false;
9652 else if (!error_suppress)
9654 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9655 error_suppress = true;
9658 substmt = c_parser_omp_structured_block (parser);
9659 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9660 SET_EXPR_LOCATION (substmt, loc);
9661 add_stmt (substmt);
9663 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9664 "expected %<#pragma omp section%> or %<}%>");
9666 substmt = pop_stmt_list (stmt);
9668 stmt = make_node (OMP_SECTIONS);
9669 SET_EXPR_LOCATION (stmt, sections_loc);
9670 TREE_TYPE (stmt) = void_type_node;
9671 OMP_SECTIONS_BODY (stmt) = substmt;
9673 return add_stmt (stmt);
9676 /* OpenMP 2.5:
9677 # pragma omp sections sections-clause[optseq] newline
9678 sections-scope
9680 LOC is the location of the #pragma token.
9683 #define OMP_SECTIONS_CLAUSE_MASK \
9684 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9685 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9686 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9687 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9688 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9690 static tree
9691 c_parser_omp_sections (location_t loc, c_parser *parser)
9693 tree block, clauses, ret;
9695 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9696 "#pragma omp sections");
9698 block = c_begin_compound_stmt (true);
9699 ret = c_parser_omp_sections_scope (loc, parser);
9700 if (ret)
9701 OMP_SECTIONS_CLAUSES (ret) = clauses;
9702 block = c_end_compound_stmt (loc, block, true);
9703 add_stmt (block);
9705 return ret;
9708 /* OpenMP 2.5:
9709 # pragma parallel parallel-clause new-line
9710 # pragma parallel for parallel-for-clause new-line
9711 # pragma parallel sections parallel-sections-clause new-line
9713 LOC is the location of the #pragma token.
9716 #define OMP_PARALLEL_CLAUSE_MASK \
9717 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9718 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9719 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9720 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9721 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9722 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9723 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9724 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9726 static tree
9727 c_parser_omp_parallel (location_t loc, c_parser *parser)
9729 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9730 const char *p_name = "#pragma omp parallel";
9731 tree stmt, clauses, par_clause, ws_clause, block;
9732 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9734 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9736 c_parser_consume_token (parser);
9737 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9738 p_name = "#pragma omp parallel for";
9739 mask |= OMP_FOR_CLAUSE_MASK;
9740 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9742 else if (c_parser_next_token_is (parser, CPP_NAME))
9744 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9745 if (strcmp (p, "sections") == 0)
9747 c_parser_consume_token (parser);
9748 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9749 p_name = "#pragma omp parallel sections";
9750 mask |= OMP_SECTIONS_CLAUSE_MASK;
9751 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9755 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9757 switch (p_kind)
9759 case PRAGMA_OMP_PARALLEL:
9760 block = c_begin_omp_parallel ();
9761 c_parser_statement (parser);
9762 stmt = c_finish_omp_parallel (loc, clauses, block);
9763 break;
9765 case PRAGMA_OMP_PARALLEL_FOR:
9766 block = c_begin_omp_parallel ();
9767 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9768 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9769 stmt = c_finish_omp_parallel (loc, par_clause, block);
9770 OMP_PARALLEL_COMBINED (stmt) = 1;
9771 break;
9773 case PRAGMA_OMP_PARALLEL_SECTIONS:
9774 block = c_begin_omp_parallel ();
9775 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9776 stmt = c_parser_omp_sections_scope (loc, parser);
9777 if (stmt)
9778 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9779 stmt = c_finish_omp_parallel (loc, par_clause, block);
9780 OMP_PARALLEL_COMBINED (stmt) = 1;
9781 break;
9783 default:
9784 gcc_unreachable ();
9787 return stmt;
9790 /* OpenMP 2.5:
9791 # pragma omp single single-clause[optseq] new-line
9792 structured-block
9794 LOC is the location of the #pragma.
9797 #define OMP_SINGLE_CLAUSE_MASK \
9798 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9799 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9800 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9801 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9803 static tree
9804 c_parser_omp_single (location_t loc, c_parser *parser)
9806 tree stmt = make_node (OMP_SINGLE);
9807 SET_EXPR_LOCATION (stmt, loc);
9808 TREE_TYPE (stmt) = void_type_node;
9810 OMP_SINGLE_CLAUSES (stmt)
9811 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9812 "#pragma omp single");
9813 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9815 return add_stmt (stmt);
9818 /* OpenMP 3.0:
9819 # pragma omp task task-clause[optseq] new-line
9821 LOC is the location of the #pragma.
9824 #define OMP_TASK_CLAUSE_MASK \
9825 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9826 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9827 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9828 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9829 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9830 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9832 static tree
9833 c_parser_omp_task (location_t loc, c_parser *parser)
9835 tree clauses, block;
9837 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9838 "#pragma omp task");
9840 block = c_begin_omp_task ();
9841 c_parser_statement (parser);
9842 return c_finish_omp_task (loc, clauses, block);
9845 /* OpenMP 3.0:
9846 # pragma omp taskwait new-line
9849 static void
9850 c_parser_omp_taskwait (c_parser *parser)
9852 location_t loc = c_parser_peek_token (parser)->location;
9853 c_parser_consume_pragma (parser);
9854 c_parser_skip_to_pragma_eol (parser);
9856 c_finish_omp_taskwait (loc);
9859 /* Main entry point to parsing most OpenMP pragmas. */
9861 static void
9862 c_parser_omp_construct (c_parser *parser)
9864 enum pragma_kind p_kind;
9865 location_t loc;
9866 tree stmt;
9868 loc = c_parser_peek_token (parser)->location;
9869 p_kind = c_parser_peek_token (parser)->pragma_kind;
9870 c_parser_consume_pragma (parser);
9872 switch (p_kind)
9874 case PRAGMA_OMP_ATOMIC:
9875 c_parser_omp_atomic (loc, parser);
9876 return;
9877 case PRAGMA_OMP_CRITICAL:
9878 stmt = c_parser_omp_critical (loc, parser);
9879 break;
9880 case PRAGMA_OMP_FOR:
9881 stmt = c_parser_omp_for (loc, parser);
9882 break;
9883 case PRAGMA_OMP_MASTER:
9884 stmt = c_parser_omp_master (loc, parser);
9885 break;
9886 case PRAGMA_OMP_ORDERED:
9887 stmt = c_parser_omp_ordered (loc, parser);
9888 break;
9889 case PRAGMA_OMP_PARALLEL:
9890 stmt = c_parser_omp_parallel (loc, parser);
9891 break;
9892 case PRAGMA_OMP_SECTIONS:
9893 stmt = c_parser_omp_sections (loc, parser);
9894 break;
9895 case PRAGMA_OMP_SINGLE:
9896 stmt = c_parser_omp_single (loc, parser);
9897 break;
9898 case PRAGMA_OMP_TASK:
9899 stmt = c_parser_omp_task (loc, parser);
9900 break;
9901 default:
9902 gcc_unreachable ();
9905 if (stmt)
9906 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9910 /* OpenMP 2.5:
9911 # pragma omp threadprivate (variable-list) */
9913 static void
9914 c_parser_omp_threadprivate (c_parser *parser)
9916 tree vars, t;
9917 location_t loc;
9919 c_parser_consume_pragma (parser);
9920 loc = c_parser_peek_token (parser)->location;
9921 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9923 /* Mark every variable in VARS to be assigned thread local storage. */
9924 for (t = vars; t; t = TREE_CHAIN (t))
9926 tree v = TREE_PURPOSE (t);
9928 /* FIXME diagnostics: Ideally we should keep individual
9929 locations for all the variables in the var list to make the
9930 following errors more precise. Perhaps
9931 c_parser_omp_var_list_parens() should construct a list of
9932 locations to go along with the var list. */
9934 /* If V had already been marked threadprivate, it doesn't matter
9935 whether it had been used prior to this point. */
9936 if (TREE_CODE (v) != VAR_DECL)
9937 error_at (loc, "%qD is not a variable", v);
9938 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9939 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9940 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9941 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9942 else if (TREE_TYPE (v) == error_mark_node)
9944 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9945 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9946 else
9948 if (! DECL_THREAD_LOCAL_P (v))
9950 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9951 /* If rtl has been already set for this var, call
9952 make_decl_rtl once again, so that encode_section_info
9953 has a chance to look at the new decl flags. */
9954 if (DECL_RTL_SET_P (v))
9955 make_decl_rtl (v);
9957 C_DECL_THREADPRIVATE_P (v) = 1;
9961 c_parser_skip_to_pragma_eol (parser);
9965 /* Parse a single source file. */
9967 void
9968 c_parse_file (void)
9970 /* Use local storage to begin. If the first token is a pragma, parse it.
9971 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9972 which will cause garbage collection. */
9973 c_parser tparser;
9975 memset (&tparser, 0, sizeof tparser);
9976 the_parser = &tparser;
9978 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9979 c_parser_pragma_pch_preprocess (&tparser);
9981 the_parser = ggc_alloc_c_parser ();
9982 *the_parser = tparser;
9984 /* Initialize EH, if we've been told to do so. */
9985 if (flag_exceptions)
9986 using_eh_for_cleanups ();
9988 c_parser_translation_unit (the_parser);
9989 the_parser = NULL;
9992 #include "gt-c-parser.h"