* config/rs6000/aix61.h (PROCESSOR_DEFAULT): Change to
[official-gcc.git] / gcc / c-parser.c
blob69ce2e50befdd2f210f8c34772462741efba10e6
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 && (global_bindings_p ()
338 || (!objc_force_identifier && !decl)))
340 token->value = objc_interface_decl;
341 token->id_kind = C_ID_CLASSNAME;
342 break;
345 token->id_kind = C_ID_ID;
347 break;
348 case CPP_AT_NAME:
349 /* This only happens in Objective-C; it must be a keyword. */
350 token->type = CPP_KEYWORD;
351 switch (C_RID_CODE (token->value))
353 /* Replace 'class' with '@class', 'private' with '@private',
354 etc. This prevents confusion with the C++ keyword
355 'class', and makes the tokens consistent with other
356 Objective-C 'AT' keywords. For example '@class' is
357 reported as RID_AT_CLASS which is consistent with
358 '@synchronized', which is reported as
359 RID_AT_SYNCHRONIZED.
361 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
362 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
363 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
364 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
365 case RID_THROW: token->keyword = RID_AT_THROW; break;
366 case RID_TRY: token->keyword = RID_AT_TRY; break;
367 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
368 default: token->keyword = C_RID_CODE (token->value);
370 break;
371 case CPP_COLON:
372 case CPP_COMMA:
373 case CPP_CLOSE_PAREN:
374 case CPP_SEMICOLON:
375 /* These tokens may affect the interpretation of any identifiers
376 following, if doing Objective-C. */
377 if (c_dialect_objc ())
378 parser->objc_need_raw_identifier = false;
379 break;
380 case CPP_PRAGMA:
381 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
382 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
383 token->value = NULL;
384 break;
385 default:
386 break;
388 timevar_pop (TV_LEX);
391 /* Return a pointer to the next token from PARSER, reading it in if
392 necessary. */
394 static inline c_token *
395 c_parser_peek_token (c_parser *parser)
397 if (parser->tokens_avail == 0)
399 c_lex_one_token (parser, &parser->tokens[0]);
400 parser->tokens_avail = 1;
402 return &parser->tokens[0];
405 /* Return true if the next token from PARSER has the indicated
406 TYPE. */
408 static inline bool
409 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
411 return c_parser_peek_token (parser)->type == type;
414 /* Return true if the next token from PARSER does not have the
415 indicated TYPE. */
417 static inline bool
418 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
420 return !c_parser_next_token_is (parser, type);
423 /* Return true if the next token from PARSER is the indicated
424 KEYWORD. */
426 static inline bool
427 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
429 return c_parser_peek_token (parser)->keyword == keyword;
432 /* Return a pointer to the next-but-one token from PARSER, reading it
433 in if necessary. The next token is already read in. */
435 static c_token *
436 c_parser_peek_2nd_token (c_parser *parser)
438 if (parser->tokens_avail >= 2)
439 return &parser->tokens[1];
440 gcc_assert (parser->tokens_avail == 1);
441 gcc_assert (parser->tokens[0].type != CPP_EOF);
442 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
443 c_lex_one_token (parser, &parser->tokens[1]);
444 parser->tokens_avail = 2;
445 return &parser->tokens[1];
448 /* Return true if TOKEN can start a type name,
449 false otherwise. */
450 static bool
451 c_token_starts_typename (c_token *token)
453 switch (token->type)
455 case CPP_NAME:
456 switch (token->id_kind)
458 case C_ID_ID:
459 return false;
460 case C_ID_ADDRSPACE:
461 return true;
462 case C_ID_TYPENAME:
463 return true;
464 case C_ID_CLASSNAME:
465 gcc_assert (c_dialect_objc ());
466 return true;
467 default:
468 gcc_unreachable ();
470 case CPP_KEYWORD:
471 switch (token->keyword)
473 case RID_UNSIGNED:
474 case RID_LONG:
475 case RID_INT128:
476 case RID_SHORT:
477 case RID_SIGNED:
478 case RID_COMPLEX:
479 case RID_INT:
480 case RID_CHAR:
481 case RID_FLOAT:
482 case RID_DOUBLE:
483 case RID_VOID:
484 case RID_DFLOAT32:
485 case RID_DFLOAT64:
486 case RID_DFLOAT128:
487 case RID_BOOL:
488 case RID_ENUM:
489 case RID_STRUCT:
490 case RID_UNION:
491 case RID_TYPEOF:
492 case RID_CONST:
493 case RID_VOLATILE:
494 case RID_RESTRICT:
495 case RID_ATTRIBUTE:
496 case RID_FRACT:
497 case RID_ACCUM:
498 case RID_SAT:
499 return true;
500 default:
501 return false;
503 case CPP_LESS:
504 if (c_dialect_objc ())
505 return true;
506 return false;
507 default:
508 return false;
512 enum c_lookahead_kind {
513 /* Always treat unknown identifiers as typenames. */
514 cla_prefer_type,
516 /* Could be parsing a nonabstract declarator. Only treat an identifier
517 as a typename if followed by another identifier or a star. */
518 cla_nonabstract_decl,
520 /* Never treat identifiers as typenames. */
521 cla_prefer_id
524 /* Return true if the next token from PARSER can start a type name,
525 false otherwise. LA specifies how to do lookahead in order to
526 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
528 static inline bool
529 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
531 c_token *token = c_parser_peek_token (parser);
532 if (c_token_starts_typename (token))
533 return true;
535 /* Try a bit harder to detect an unknown typename. */
536 if (la != cla_prefer_id
537 && token->type == CPP_NAME
538 && token->id_kind == C_ID_ID
540 /* Do not try too hard when we could have "object in array". */
541 && !parser->objc_could_be_foreach_context
543 && (la == cla_prefer_type
544 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
545 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
547 /* Only unknown identifiers. */
548 && !lookup_name (token->value))
549 return true;
551 return false;
554 /* Return true if TOKEN is a type qualifier, false otherwise. */
555 static bool
556 c_token_is_qualifier (c_token *token)
558 switch (token->type)
560 case CPP_NAME:
561 switch (token->id_kind)
563 case C_ID_ADDRSPACE:
564 return true;
565 default:
566 return false;
568 case CPP_KEYWORD:
569 switch (token->keyword)
571 case RID_CONST:
572 case RID_VOLATILE:
573 case RID_RESTRICT:
574 case RID_ATTRIBUTE:
575 return true;
576 default:
577 return false;
579 case CPP_LESS:
580 return false;
581 default:
582 gcc_unreachable ();
586 /* Return true if the next token from PARSER is a type qualifier,
587 false otherwise. */
588 static inline bool
589 c_parser_next_token_is_qualifier (c_parser *parser)
591 c_token *token = c_parser_peek_token (parser);
592 return c_token_is_qualifier (token);
595 /* Return true if TOKEN can start declaration specifiers, false
596 otherwise. */
597 static bool
598 c_token_starts_declspecs (c_token *token)
600 switch (token->type)
602 case CPP_NAME:
603 switch (token->id_kind)
605 case C_ID_ID:
606 return false;
607 case C_ID_ADDRSPACE:
608 return true;
609 case C_ID_TYPENAME:
610 return true;
611 case C_ID_CLASSNAME:
612 gcc_assert (c_dialect_objc ());
613 return true;
614 default:
615 gcc_unreachable ();
617 case CPP_KEYWORD:
618 switch (token->keyword)
620 case RID_STATIC:
621 case RID_EXTERN:
622 case RID_REGISTER:
623 case RID_TYPEDEF:
624 case RID_INLINE:
625 case RID_AUTO:
626 case RID_THREAD:
627 case RID_UNSIGNED:
628 case RID_LONG:
629 case RID_INT128:
630 case RID_SHORT:
631 case RID_SIGNED:
632 case RID_COMPLEX:
633 case RID_INT:
634 case RID_CHAR:
635 case RID_FLOAT:
636 case RID_DOUBLE:
637 case RID_VOID:
638 case RID_DFLOAT32:
639 case RID_DFLOAT64:
640 case RID_DFLOAT128:
641 case RID_BOOL:
642 case RID_ENUM:
643 case RID_STRUCT:
644 case RID_UNION:
645 case RID_TYPEOF:
646 case RID_CONST:
647 case RID_VOLATILE:
648 case RID_RESTRICT:
649 case RID_ATTRIBUTE:
650 case RID_FRACT:
651 case RID_ACCUM:
652 case RID_SAT:
653 return true;
654 default:
655 return false;
657 case CPP_LESS:
658 if (c_dialect_objc ())
659 return true;
660 return false;
661 default:
662 return false;
667 /* Return true if TOKEN can start declaration specifiers or a static
668 assertion, false otherwise. */
669 static bool
670 c_token_starts_declaration (c_token *token)
672 if (c_token_starts_declspecs (token)
673 || token->keyword == RID_STATIC_ASSERT)
674 return true;
675 else
676 return false;
679 /* Return true if the next token from PARSER can start declaration
680 specifiers, false otherwise. */
681 static inline bool
682 c_parser_next_token_starts_declspecs (c_parser *parser)
684 c_token *token = c_parser_peek_token (parser);
686 /* In Objective-C, a classname normally starts a declspecs unless it
687 is immediately followed by a dot. In that case, it is the
688 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
689 setter/getter on the class. c_token_starts_declspecs() can't
690 differentiate between the two cases because it only checks the
691 current token, so we have a special check here. */
692 if (c_dialect_objc ()
693 && token->type == CPP_NAME
694 && token->id_kind == C_ID_CLASSNAME
695 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
696 return false;
698 return c_token_starts_declspecs (token);
701 /* Return true if the next tokens from PARSER can start declaration
702 specifiers or a static assertion, false otherwise. */
703 static inline bool
704 c_parser_next_tokens_start_declaration (c_parser *parser)
706 c_token *token = c_parser_peek_token (parser);
708 /* Same as above. */
709 if (c_dialect_objc ()
710 && token->type == CPP_NAME
711 && token->id_kind == C_ID_CLASSNAME
712 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
713 return false;
715 /* Labels do not start declarations. */
716 if (token->type == CPP_NAME
717 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
718 return false;
720 if (c_token_starts_declaration (token))
721 return true;
723 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
724 return true;
726 return false;
729 /* Consume the next token from PARSER. */
731 static void
732 c_parser_consume_token (c_parser *parser)
734 gcc_assert (parser->tokens_avail >= 1);
735 gcc_assert (parser->tokens[0].type != CPP_EOF);
736 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
737 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
738 if (parser->tokens_avail == 2)
739 parser->tokens[0] = parser->tokens[1];
740 parser->tokens_avail--;
743 /* Expect the current token to be a #pragma. Consume it and remember
744 that we've begun parsing a pragma. */
746 static void
747 c_parser_consume_pragma (c_parser *parser)
749 gcc_assert (!parser->in_pragma);
750 gcc_assert (parser->tokens_avail >= 1);
751 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
752 if (parser->tokens_avail == 2)
753 parser->tokens[0] = parser->tokens[1];
754 parser->tokens_avail--;
755 parser->in_pragma = true;
758 /* Update the globals input_location and in_system_header from
759 TOKEN. */
760 static inline void
761 c_parser_set_source_position_from_token (c_token *token)
763 if (token->type != CPP_EOF)
765 input_location = token->location;
769 /* Issue a diagnostic of the form
770 FILE:LINE: MESSAGE before TOKEN
771 where TOKEN is the next token in the input stream of PARSER.
772 MESSAGE (specified by the caller) is usually of the form "expected
773 OTHER-TOKEN".
775 Do not issue a diagnostic if still recovering from an error.
777 ??? This is taken from the C++ parser, but building up messages in
778 this way is not i18n-friendly and some other approach should be
779 used. */
781 static void
782 c_parser_error (c_parser *parser, const char *gmsgid)
784 c_token *token = c_parser_peek_token (parser);
785 if (parser->error)
786 return;
787 parser->error = true;
788 if (!gmsgid)
789 return;
790 /* This diagnostic makes more sense if it is tagged to the line of
791 the token we just peeked at. */
792 c_parser_set_source_position_from_token (token);
793 c_parse_error (gmsgid,
794 /* Because c_parse_error does not understand
795 CPP_KEYWORD, keywords are treated like
796 identifiers. */
797 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
798 /* ??? The C parser does not save the cpp flags of a
799 token, we need to pass 0 here and we will not get
800 the source spelling of some tokens but rather the
801 canonical spelling. */
802 token->value, /*flags=*/0);
805 /* If the next token is of the indicated TYPE, consume it. Otherwise,
806 issue the error MSGID. If MSGID is NULL then a message has already
807 been produced and no message will be produced this time. Returns
808 true if found, false otherwise. */
810 static bool
811 c_parser_require (c_parser *parser,
812 enum cpp_ttype type,
813 const char *msgid)
815 if (c_parser_next_token_is (parser, type))
817 c_parser_consume_token (parser);
818 return true;
820 else
822 c_parser_error (parser, msgid);
823 return false;
827 /* If the next token is the indicated keyword, consume it. Otherwise,
828 issue the error MSGID. Returns true if found, false otherwise. */
830 static bool
831 c_parser_require_keyword (c_parser *parser,
832 enum rid keyword,
833 const char *msgid)
835 if (c_parser_next_token_is_keyword (parser, keyword))
837 c_parser_consume_token (parser);
838 return true;
840 else
842 c_parser_error (parser, msgid);
843 return false;
847 /* Like c_parser_require, except that tokens will be skipped until the
848 desired token is found. An error message is still produced if the
849 next token is not as expected. If MSGID is NULL then a message has
850 already been produced and no message will be produced this
851 time. */
853 static void
854 c_parser_skip_until_found (c_parser *parser,
855 enum cpp_ttype type,
856 const char *msgid)
858 unsigned nesting_depth = 0;
860 if (c_parser_require (parser, type, msgid))
861 return;
863 /* Skip tokens until the desired token is found. */
864 while (true)
866 /* Peek at the next token. */
867 c_token *token = c_parser_peek_token (parser);
868 /* If we've reached the token we want, consume it and stop. */
869 if (token->type == type && !nesting_depth)
871 c_parser_consume_token (parser);
872 break;
875 /* If we've run out of tokens, stop. */
876 if (token->type == CPP_EOF)
877 return;
878 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
879 return;
880 if (token->type == CPP_OPEN_BRACE
881 || token->type == CPP_OPEN_PAREN
882 || token->type == CPP_OPEN_SQUARE)
883 ++nesting_depth;
884 else if (token->type == CPP_CLOSE_BRACE
885 || token->type == CPP_CLOSE_PAREN
886 || token->type == CPP_CLOSE_SQUARE)
888 if (nesting_depth-- == 0)
889 break;
891 /* Consume this token. */
892 c_parser_consume_token (parser);
894 parser->error = false;
897 /* Skip tokens until the end of a parameter is found, but do not
898 consume the comma, semicolon or closing delimiter. */
900 static void
901 c_parser_skip_to_end_of_parameter (c_parser *parser)
903 unsigned nesting_depth = 0;
905 while (true)
907 c_token *token = c_parser_peek_token (parser);
908 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
909 && !nesting_depth)
910 break;
911 /* If we've run out of tokens, stop. */
912 if (token->type == CPP_EOF)
913 return;
914 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
915 return;
916 if (token->type == CPP_OPEN_BRACE
917 || token->type == CPP_OPEN_PAREN
918 || token->type == CPP_OPEN_SQUARE)
919 ++nesting_depth;
920 else if (token->type == CPP_CLOSE_BRACE
921 || token->type == CPP_CLOSE_PAREN
922 || token->type == CPP_CLOSE_SQUARE)
924 if (nesting_depth-- == 0)
925 break;
927 /* Consume this token. */
928 c_parser_consume_token (parser);
930 parser->error = false;
933 /* Expect to be at the end of the pragma directive and consume an
934 end of line marker. */
936 static void
937 c_parser_skip_to_pragma_eol (c_parser *parser)
939 gcc_assert (parser->in_pragma);
940 parser->in_pragma = false;
942 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
943 while (true)
945 c_token *token = c_parser_peek_token (parser);
946 if (token->type == CPP_EOF)
947 break;
948 if (token->type == CPP_PRAGMA_EOL)
950 c_parser_consume_token (parser);
951 break;
953 c_parser_consume_token (parser);
956 parser->error = false;
959 /* Skip tokens until we have consumed an entire block, or until we
960 have consumed a non-nested ';'. */
962 static void
963 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
965 unsigned nesting_depth = 0;
966 bool save_error = parser->error;
968 while (true)
970 c_token *token;
972 /* Peek at the next token. */
973 token = c_parser_peek_token (parser);
975 switch (token->type)
977 case CPP_EOF:
978 return;
980 case CPP_PRAGMA_EOL:
981 if (parser->in_pragma)
982 return;
983 break;
985 case CPP_SEMICOLON:
986 /* If the next token is a ';', we have reached the
987 end of the statement. */
988 if (!nesting_depth)
990 /* Consume the ';'. */
991 c_parser_consume_token (parser);
992 goto finished;
994 break;
996 case CPP_CLOSE_BRACE:
997 /* If the next token is a non-nested '}', then we have
998 reached the end of the current block. */
999 if (nesting_depth == 0 || --nesting_depth == 0)
1001 c_parser_consume_token (parser);
1002 goto finished;
1004 break;
1006 case CPP_OPEN_BRACE:
1007 /* If it the next token is a '{', then we are entering a new
1008 block. Consume the entire block. */
1009 ++nesting_depth;
1010 break;
1012 case CPP_PRAGMA:
1013 /* If we see a pragma, consume the whole thing at once. We
1014 have some safeguards against consuming pragmas willy-nilly.
1015 Normally, we'd expect to be here with parser->error set,
1016 which disables these safeguards. But it's possible to get
1017 here for secondary error recovery, after parser->error has
1018 been cleared. */
1019 c_parser_consume_pragma (parser);
1020 c_parser_skip_to_pragma_eol (parser);
1021 parser->error = save_error;
1022 continue;
1024 default:
1025 break;
1028 c_parser_consume_token (parser);
1031 finished:
1032 parser->error = false;
1035 /* CPP's options (initialized by c-opts.c). */
1036 extern cpp_options *cpp_opts;
1038 /* Save the warning flags which are controlled by __extension__. */
1040 static inline int
1041 disable_extension_diagnostics (void)
1043 int ret = (pedantic
1044 | (warn_pointer_arith << 1)
1045 | (warn_traditional << 2)
1046 | (flag_iso << 3)
1047 | (warn_long_long << 4)
1048 | (warn_cxx_compat << 5));
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 return ret;
1058 /* Restore the warning flags which are controlled by __extension__.
1059 FLAGS is the return value from disable_extension_diagnostics. */
1061 static inline void
1062 restore_extension_diagnostics (int flags)
1064 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1065 warn_pointer_arith = (flags >> 1) & 1;
1066 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1067 flag_iso = (flags >> 3) & 1;
1068 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1069 warn_cxx_compat = (flags >> 5) & 1;
1072 /* Possibly kinds of declarator to parse. */
1073 typedef enum c_dtr_syn {
1074 /* A normal declarator with an identifier. */
1075 C_DTR_NORMAL,
1076 /* An abstract declarator (maybe empty). */
1077 C_DTR_ABSTRACT,
1078 /* A parameter declarator: may be either, but after a type name does
1079 not redeclare a typedef name as an identifier if it can
1080 alternatively be interpreted as a typedef name; see DR#009,
1081 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1082 following DR#249. For example, given a typedef T, "int T" and
1083 "int *T" are valid parameter declarations redeclaring T, while
1084 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1085 abstract declarators rather than involving redundant parentheses;
1086 the same applies with attributes inside the parentheses before
1087 "T". */
1088 C_DTR_PARM
1089 } c_dtr_syn;
1091 static void c_parser_external_declaration (c_parser *);
1092 static void c_parser_asm_definition (c_parser *);
1093 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1094 bool, bool, tree *);
1095 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1096 static void c_parser_static_assert_declaration (c_parser *);
1097 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1098 bool, enum c_lookahead_kind);
1099 static struct c_typespec c_parser_enum_specifier (c_parser *);
1100 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1101 static tree c_parser_struct_declaration (c_parser *);
1102 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1103 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1104 bool *);
1105 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1106 c_dtr_syn, bool *);
1107 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1108 bool,
1109 struct c_declarator *);
1110 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1111 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
1112 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1113 static tree c_parser_simple_asm_expr (c_parser *);
1114 static tree c_parser_attributes (c_parser *);
1115 static struct c_type_name *c_parser_type_name (c_parser *);
1116 static struct c_expr c_parser_initializer (c_parser *);
1117 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1118 static void c_parser_initelt (c_parser *, struct obstack *);
1119 static void c_parser_initval (c_parser *, struct c_expr *,
1120 struct obstack *);
1121 static tree c_parser_compound_statement (c_parser *);
1122 static void c_parser_compound_statement_nostart (c_parser *);
1123 static void c_parser_label (c_parser *);
1124 static void c_parser_statement (c_parser *);
1125 static void c_parser_statement_after_labels (c_parser *);
1126 static void c_parser_if_statement (c_parser *);
1127 static void c_parser_switch_statement (c_parser *);
1128 static void c_parser_while_statement (c_parser *);
1129 static void c_parser_do_statement (c_parser *);
1130 static void c_parser_for_statement (c_parser *);
1131 static tree c_parser_asm_statement (c_parser *);
1132 static tree c_parser_asm_operands (c_parser *, bool);
1133 static tree c_parser_asm_goto_operands (c_parser *);
1134 static tree c_parser_asm_clobbers (c_parser *);
1135 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1136 static struct c_expr c_parser_conditional_expression (c_parser *,
1137 struct c_expr *);
1138 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1139 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1140 static struct c_expr c_parser_unary_expression (c_parser *);
1141 static struct c_expr c_parser_sizeof_expression (c_parser *);
1142 static struct c_expr c_parser_alignof_expression (c_parser *);
1143 static struct c_expr c_parser_postfix_expression (c_parser *);
1144 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1145 struct c_type_name *,
1146 location_t);
1147 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1148 location_t loc,
1149 struct c_expr);
1150 static struct c_expr c_parser_expression (c_parser *);
1151 static struct c_expr c_parser_expression_conv (c_parser *);
1152 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1153 VEC(tree,gc) **);
1154 static void c_parser_omp_construct (c_parser *);
1155 static void c_parser_omp_threadprivate (c_parser *);
1156 static void c_parser_omp_barrier (c_parser *);
1157 static void c_parser_omp_flush (c_parser *);
1158 static void c_parser_omp_taskwait (c_parser *);
1160 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1161 static bool c_parser_pragma (c_parser *, enum pragma_context);
1163 /* These Objective-C parser functions are only ever called when
1164 compiling Objective-C. */
1165 static void c_parser_objc_class_definition (c_parser *, tree);
1166 static void c_parser_objc_class_instance_variables (c_parser *);
1167 static void c_parser_objc_class_declaration (c_parser *);
1168 static void c_parser_objc_alias_declaration (c_parser *);
1169 static void c_parser_objc_protocol_definition (c_parser *, tree);
1170 static bool c_parser_objc_method_type (c_parser *);
1171 static void c_parser_objc_method_definition (c_parser *);
1172 static void c_parser_objc_methodprotolist (c_parser *);
1173 static void c_parser_objc_methodproto (c_parser *);
1174 static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
1175 static tree c_parser_objc_type_name (c_parser *);
1176 static tree c_parser_objc_protocol_refs (c_parser *);
1177 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1178 static void c_parser_objc_synchronized_statement (c_parser *);
1179 static tree c_parser_objc_selector (c_parser *);
1180 static tree c_parser_objc_selector_arg (c_parser *);
1181 static tree c_parser_objc_receiver (c_parser *);
1182 static tree c_parser_objc_message_args (c_parser *);
1183 static tree c_parser_objc_keywordexpr (c_parser *);
1184 static void c_parser_objc_at_property_declaration (c_parser *);
1185 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1186 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1187 static bool c_parser_objc_diagnose_bad_element_prefix
1188 (c_parser *, struct c_declspecs *);
1190 /* Parse a translation unit (C90 6.7, C99 6.9).
1192 translation-unit:
1193 external-declarations
1195 external-declarations:
1196 external-declaration
1197 external-declarations external-declaration
1199 GNU extensions:
1201 translation-unit:
1202 empty
1205 static void
1206 c_parser_translation_unit (c_parser *parser)
1208 if (c_parser_next_token_is (parser, CPP_EOF))
1210 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1211 "ISO C forbids an empty translation unit");
1213 else
1215 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1216 mark_valid_location_for_stdc_pragma (false);
1219 ggc_collect ();
1220 c_parser_external_declaration (parser);
1221 obstack_free (&parser_obstack, obstack_position);
1223 while (c_parser_next_token_is_not (parser, CPP_EOF));
1227 /* Parse an external declaration (C90 6.7, C99 6.9).
1229 external-declaration:
1230 function-definition
1231 declaration
1233 GNU extensions:
1235 external-declaration:
1236 asm-definition
1238 __extension__ external-declaration
1240 Objective-C:
1242 external-declaration:
1243 objc-class-definition
1244 objc-class-declaration
1245 objc-alias-declaration
1246 objc-protocol-definition
1247 objc-method-definition
1248 @end
1251 static void
1252 c_parser_external_declaration (c_parser *parser)
1254 int ext;
1255 switch (c_parser_peek_token (parser)->type)
1257 case CPP_KEYWORD:
1258 switch (c_parser_peek_token (parser)->keyword)
1260 case RID_EXTENSION:
1261 ext = disable_extension_diagnostics ();
1262 c_parser_consume_token (parser);
1263 c_parser_external_declaration (parser);
1264 restore_extension_diagnostics (ext);
1265 break;
1266 case RID_ASM:
1267 c_parser_asm_definition (parser);
1268 break;
1269 case RID_AT_INTERFACE:
1270 case RID_AT_IMPLEMENTATION:
1271 gcc_assert (c_dialect_objc ());
1272 c_parser_objc_class_definition (parser, NULL_TREE);
1273 break;
1274 case RID_AT_CLASS:
1275 gcc_assert (c_dialect_objc ());
1276 c_parser_objc_class_declaration (parser);
1277 break;
1278 case RID_AT_ALIAS:
1279 gcc_assert (c_dialect_objc ());
1280 c_parser_objc_alias_declaration (parser);
1281 break;
1282 case RID_AT_PROTOCOL:
1283 gcc_assert (c_dialect_objc ());
1284 c_parser_objc_protocol_definition (parser, NULL_TREE);
1285 break;
1286 case RID_AT_PROPERTY:
1287 gcc_assert (c_dialect_objc ());
1288 c_parser_objc_at_property_declaration (parser);
1289 break;
1290 case RID_AT_SYNTHESIZE:
1291 gcc_assert (c_dialect_objc ());
1292 c_parser_objc_at_synthesize_declaration (parser);
1293 break;
1294 case RID_AT_DYNAMIC:
1295 gcc_assert (c_dialect_objc ());
1296 c_parser_objc_at_dynamic_declaration (parser);
1297 break;
1298 case RID_AT_END:
1299 gcc_assert (c_dialect_objc ());
1300 c_parser_consume_token (parser);
1301 objc_finish_implementation ();
1302 break;
1303 default:
1304 goto decl_or_fndef;
1306 break;
1307 case CPP_SEMICOLON:
1308 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1309 "ISO C does not allow extra %<;%> outside of a function");
1310 c_parser_consume_token (parser);
1311 break;
1312 case CPP_PRAGMA:
1313 mark_valid_location_for_stdc_pragma (true);
1314 c_parser_pragma (parser, pragma_external);
1315 mark_valid_location_for_stdc_pragma (false);
1316 break;
1317 case CPP_PLUS:
1318 case CPP_MINUS:
1319 if (c_dialect_objc ())
1321 c_parser_objc_method_definition (parser);
1322 break;
1324 /* Else fall through, and yield a syntax error trying to parse
1325 as a declaration or function definition. */
1326 default:
1327 decl_or_fndef:
1328 /* A declaration or a function definition (or, in Objective-C,
1329 an @interface or @protocol with prefix attributes). We can
1330 only tell which after parsing the declaration specifiers, if
1331 any, and the first declarator. */
1332 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1333 break;
1337 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1338 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1339 accepted; otherwise (old-style parameter declarations) only other
1340 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1341 assertion is accepted; otherwise (old-style parameter declarations)
1342 it is not. If NESTED is true, we are inside a function or parsing
1343 old-style parameter declarations; any functions encountered are
1344 nested functions and declaration specifiers are required; otherwise
1345 we are at top level and functions are normal functions and
1346 declaration specifiers may be optional. If EMPTY_OK is true, empty
1347 declarations are OK (subject to all other constraints); otherwise
1348 (old-style parameter declarations) they are diagnosed. If
1349 START_ATTR_OK is true, the declaration specifiers may start with
1350 attributes; otherwise they may not.
1351 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1352 declaration when parsing an Objective-C foreach statement.
1354 declaration:
1355 declaration-specifiers init-declarator-list[opt] ;
1356 static_assert-declaration
1358 function-definition:
1359 declaration-specifiers[opt] declarator declaration-list[opt]
1360 compound-statement
1362 declaration-list:
1363 declaration
1364 declaration-list declaration
1366 init-declarator-list:
1367 init-declarator
1368 init-declarator-list , init-declarator
1370 init-declarator:
1371 declarator simple-asm-expr[opt] attributes[opt]
1372 declarator simple-asm-expr[opt] attributes[opt] = initializer
1374 GNU extensions:
1376 nested-function-definition:
1377 declaration-specifiers declarator declaration-list[opt]
1378 compound-statement
1380 Objective-C:
1381 attributes objc-class-definition
1382 attributes objc-category-definition
1383 attributes objc-protocol-definition
1385 The simple-asm-expr and attributes are GNU extensions.
1387 This function does not handle __extension__; that is handled in its
1388 callers. ??? Following the old parser, __extension__ may start
1389 external declarations, declarations in functions and declarations
1390 at the start of "for" loops, but not old-style parameter
1391 declarations.
1393 C99 requires declaration specifiers in a function definition; the
1394 absence is diagnosed through the diagnosis of implicit int. In GNU
1395 C we also allow but diagnose declarations without declaration
1396 specifiers, but only at top level (elsewhere they conflict with
1397 other syntax).
1399 In Objective-C, declarations of the looping variable in a foreach
1400 statement are exceptionally terminated by 'in' (for example, 'for
1401 (NSObject *object in array) { ... }').
1403 OpenMP:
1405 declaration:
1406 threadprivate-directive */
1408 static void
1409 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1410 bool static_assert_ok, bool empty_ok,
1411 bool nested, bool start_attr_ok,
1412 tree *objc_foreach_object_declaration)
1414 struct c_declspecs *specs;
1415 tree prefix_attrs;
1416 tree all_prefix_attrs;
1417 bool diagnosed_no_specs = false;
1418 location_t here = c_parser_peek_token (parser)->location;
1420 if (static_assert_ok
1421 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1423 c_parser_static_assert_declaration (parser);
1424 return;
1426 specs = build_null_declspecs ();
1428 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1429 if (c_parser_peek_token (parser)->type == CPP_NAME
1430 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1431 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1432 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1433 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1435 error_at (here, "unknown type name %qE",
1436 c_parser_peek_token (parser)->value);
1438 /* Parse declspecs normally to get a correct pointer type, but avoid
1439 a further "fails to be a type name" error. Refuse nested functions
1440 since it is not how the user likely wants us to recover. */
1441 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1442 c_parser_peek_token (parser)->keyword = RID_VOID;
1443 c_parser_peek_token (parser)->value = error_mark_node;
1444 fndef_ok = !nested;
1447 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1448 if (parser->error)
1450 c_parser_skip_to_end_of_block_or_statement (parser);
1451 return;
1453 if (nested && !specs->declspecs_seen_p)
1455 c_parser_error (parser, "expected declaration specifiers");
1456 c_parser_skip_to_end_of_block_or_statement (parser);
1457 return;
1459 finish_declspecs (specs);
1460 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1462 if (empty_ok)
1463 shadow_tag (specs);
1464 else
1466 shadow_tag_warned (specs, 1);
1467 pedwarn (here, 0, "empty declaration");
1469 c_parser_consume_token (parser);
1470 return;
1473 /* Provide better error recovery. Note that a type name here is usually
1474 better diagnosed as a redeclaration. */
1475 if (empty_ok
1476 && specs->typespec_kind == ctsk_tagdef
1477 && c_parser_next_token_starts_declspecs (parser)
1478 && !c_parser_next_token_is (parser, CPP_NAME))
1480 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1481 parser->error = false;
1482 shadow_tag_warned (specs, 1);
1483 return;
1485 else if (c_dialect_objc ())
1487 /* Prefix attributes are an error on method decls. */
1488 switch (c_parser_peek_token (parser)->type)
1490 case CPP_PLUS:
1491 case CPP_MINUS:
1492 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1493 return;
1494 if (specs->attrs)
1496 warning_at (c_parser_peek_token (parser)->location,
1497 OPT_Wattributes,
1498 "prefix attributes are ignored for methods");
1499 specs->attrs = NULL_TREE;
1501 if (fndef_ok)
1502 c_parser_objc_method_definition (parser);
1503 else
1504 c_parser_objc_methodproto (parser);
1505 return;
1506 break;
1507 default:
1508 break;
1510 /* This is where we parse 'attributes @interface ...',
1511 'attributes @implementation ...', 'attributes @protocol ...'
1512 (where attributes could be, for example, __attribute__
1513 ((deprecated)).
1515 switch (c_parser_peek_token (parser)->keyword)
1517 case RID_AT_INTERFACE:
1519 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1520 return;
1521 c_parser_objc_class_definition (parser, specs->attrs);
1522 return;
1524 break;
1525 case RID_AT_IMPLEMENTATION:
1527 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1528 return;
1529 if (specs->attrs)
1531 warning_at (c_parser_peek_token (parser)->location,
1532 OPT_Wattributes,
1533 "prefix attributes are ignored for implementations");
1534 specs->attrs = NULL_TREE;
1536 c_parser_objc_class_definition (parser, NULL_TREE);
1537 return;
1539 break;
1540 case RID_AT_PROTOCOL:
1542 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1543 return;
1544 c_parser_objc_protocol_definition (parser, specs->attrs);
1545 return;
1547 break;
1548 case RID_AT_ALIAS:
1549 case RID_AT_CLASS:
1550 case RID_AT_END:
1551 case RID_AT_PROPERTY:
1552 if (specs->attrs)
1554 c_parser_error (parser, "unexpected attribute");
1555 specs->attrs = NULL;
1557 break;
1558 default:
1559 break;
1563 pending_xref_error ();
1564 prefix_attrs = specs->attrs;
1565 all_prefix_attrs = prefix_attrs;
1566 specs->attrs = NULL_TREE;
1567 while (true)
1569 struct c_declarator *declarator;
1570 bool dummy = false;
1571 tree fnbody;
1572 /* Declaring either one or more declarators (in which case we
1573 should diagnose if there were no declaration specifiers) or a
1574 function definition (in which case the diagnostic for
1575 implicit int suffices). */
1576 declarator = c_parser_declarator (parser,
1577 specs->typespec_kind != ctsk_none,
1578 C_DTR_NORMAL, &dummy);
1579 if (declarator == NULL)
1581 c_parser_skip_to_end_of_block_or_statement (parser);
1582 return;
1584 if (c_parser_next_token_is (parser, CPP_EQ)
1585 || c_parser_next_token_is (parser, CPP_COMMA)
1586 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1587 || c_parser_next_token_is_keyword (parser, RID_ASM)
1588 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1589 || c_parser_next_token_is_keyword (parser, RID_IN))
1591 tree asm_name = NULL_TREE;
1592 tree postfix_attrs = NULL_TREE;
1593 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1595 diagnosed_no_specs = true;
1596 pedwarn (here, 0, "data definition has no type or storage class");
1598 /* Having seen a data definition, there cannot now be a
1599 function definition. */
1600 fndef_ok = false;
1601 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1602 asm_name = c_parser_simple_asm_expr (parser);
1603 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1604 postfix_attrs = c_parser_attributes (parser);
1605 if (c_parser_next_token_is (parser, CPP_EQ))
1607 tree d;
1608 struct c_expr init;
1609 location_t init_loc;
1610 c_parser_consume_token (parser);
1611 /* The declaration of the variable is in effect while
1612 its initializer is parsed. */
1613 d = start_decl (declarator, specs, true,
1614 chainon (postfix_attrs, all_prefix_attrs));
1615 if (!d)
1616 d = error_mark_node;
1617 start_init (d, asm_name, global_bindings_p ());
1618 init_loc = c_parser_peek_token (parser)->location;
1619 init = c_parser_initializer (parser);
1620 finish_init ();
1621 if (d != error_mark_node)
1623 maybe_warn_string_init (TREE_TYPE (d), init);
1624 finish_decl (d, init_loc, init.value,
1625 init.original_type, asm_name);
1628 else
1630 tree d = start_decl (declarator, specs, false,
1631 chainon (postfix_attrs,
1632 all_prefix_attrs));
1633 if (d)
1634 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1635 NULL_TREE, asm_name);
1637 if (c_parser_next_token_is_keyword (parser, RID_IN))
1639 if (d)
1640 *objc_foreach_object_declaration = d;
1641 else
1642 *objc_foreach_object_declaration = error_mark_node;
1645 if (c_parser_next_token_is (parser, CPP_COMMA))
1647 c_parser_consume_token (parser);
1648 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1649 all_prefix_attrs = chainon (c_parser_attributes (parser),
1650 prefix_attrs);
1651 else
1652 all_prefix_attrs = prefix_attrs;
1653 continue;
1655 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1657 c_parser_consume_token (parser);
1658 return;
1660 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1662 /* This can only happen in Objective-C: we found the
1663 'in' that terminates the declaration inside an
1664 Objective-C foreach statement. Do not consume the
1665 token, so that the caller can use it to determine
1666 that this indeed is a foreach context. */
1667 return;
1669 else
1671 c_parser_error (parser, "expected %<,%> or %<;%>");
1672 c_parser_skip_to_end_of_block_or_statement (parser);
1673 return;
1676 else if (!fndef_ok)
1678 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1679 "%<asm%> or %<__attribute__%>");
1680 c_parser_skip_to_end_of_block_or_statement (parser);
1681 return;
1683 /* Function definition (nested or otherwise). */
1684 if (nested)
1686 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1687 c_push_function_context ();
1689 if (!start_function (specs, declarator, all_prefix_attrs))
1691 /* This can appear in many cases looking nothing like a
1692 function definition, so we don't give a more specific
1693 error suggesting there was one. */
1694 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1695 "or %<__attribute__%>");
1696 if (nested)
1697 c_pop_function_context ();
1698 break;
1700 /* Parse old-style parameter declarations. ??? Attributes are
1701 not allowed to start declaration specifiers here because of a
1702 syntax conflict between a function declaration with attribute
1703 suffix and a function definition with an attribute prefix on
1704 first old-style parameter declaration. Following the old
1705 parser, they are not accepted on subsequent old-style
1706 parameter declarations either. However, there is no
1707 ambiguity after the first declaration, nor indeed on the
1708 first as long as we don't allow postfix attributes after a
1709 declarator with a nonempty identifier list in a definition;
1710 and postfix attributes have never been accepted here in
1711 function definitions either. */
1712 while (c_parser_next_token_is_not (parser, CPP_EOF)
1713 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1714 c_parser_declaration_or_fndef (parser, false, false, false,
1715 true, false, NULL);
1716 store_parm_decls ();
1717 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1718 = c_parser_peek_token (parser)->location;
1719 fnbody = c_parser_compound_statement (parser);
1720 if (nested)
1722 tree decl = current_function_decl;
1723 /* Mark nested functions as needing static-chain initially.
1724 lower_nested_functions will recompute it but the
1725 DECL_STATIC_CHAIN flag is also used before that happens,
1726 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1727 DECL_STATIC_CHAIN (decl) = 1;
1728 add_stmt (fnbody);
1729 finish_function ();
1730 c_pop_function_context ();
1731 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1733 else
1735 add_stmt (fnbody);
1736 finish_function ();
1738 break;
1742 /* Parse an asm-definition (asm() outside a function body). This is a
1743 GNU extension.
1745 asm-definition:
1746 simple-asm-expr ;
1749 static void
1750 c_parser_asm_definition (c_parser *parser)
1752 tree asm_str = c_parser_simple_asm_expr (parser);
1753 if (asm_str)
1754 cgraph_add_asm_node (asm_str);
1755 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1758 /* Parse a static assertion (C1X N1425 6.7.10).
1760 static_assert-declaration:
1761 static_assert-declaration-no-semi ;
1764 static void
1765 c_parser_static_assert_declaration (c_parser *parser)
1767 c_parser_static_assert_declaration_no_semi (parser);
1768 if (parser->error
1769 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1770 c_parser_skip_to_end_of_block_or_statement (parser);
1773 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1774 semicolon.
1776 static_assert-declaration-no-semi:
1777 _Static_assert ( constant-expression , string-literal )
1780 static void
1781 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1783 location_t assert_loc, value_loc;
1784 tree value;
1785 tree string;
1787 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1788 assert_loc = c_parser_peek_token (parser)->location;
1789 if (!flag_isoc1x)
1791 if (flag_isoc99)
1792 pedwarn (assert_loc, OPT_pedantic,
1793 "ISO C99 does not support %<_Static_assert%>");
1794 else
1795 pedwarn (assert_loc, OPT_pedantic,
1796 "ISO C90 does not support %<_Static_assert%>");
1798 c_parser_consume_token (parser);
1799 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1800 return;
1801 value_loc = c_parser_peek_token (parser)->location;
1802 value = c_parser_expr_no_commas (parser, NULL).value;
1803 parser->lex_untranslated_string = true;
1804 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1806 parser->lex_untranslated_string = false;
1807 return;
1809 switch (c_parser_peek_token (parser)->type)
1811 case CPP_STRING:
1812 case CPP_STRING16:
1813 case CPP_STRING32:
1814 case CPP_WSTRING:
1815 case CPP_UTF8STRING:
1816 string = c_parser_peek_token (parser)->value;
1817 c_parser_consume_token (parser);
1818 parser->lex_untranslated_string = false;
1819 break;
1820 default:
1821 c_parser_error (parser, "expected string literal");
1822 parser->lex_untranslated_string = false;
1823 return;
1825 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1827 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1829 error_at (value_loc, "expression in static assertion is not an integer");
1830 return;
1832 if (TREE_CODE (value) != INTEGER_CST)
1834 value = c_fully_fold (value, false, NULL);
1835 if (TREE_CODE (value) == INTEGER_CST)
1836 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1837 "is not an integer constant expression");
1839 if (TREE_CODE (value) != INTEGER_CST)
1841 error_at (value_loc, "expression in static assertion is not constant");
1842 return;
1844 constant_expression_warning (value);
1845 if (integer_zerop (value))
1846 error_at (assert_loc, "static assertion failed: %E", string);
1849 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1850 6.7), adding them to SPECS (which may already include some).
1851 Storage class specifiers are accepted iff SCSPEC_OK; type
1852 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1853 the start iff START_ATTR_OK.
1855 declaration-specifiers:
1856 storage-class-specifier declaration-specifiers[opt]
1857 type-specifier declaration-specifiers[opt]
1858 type-qualifier declaration-specifiers[opt]
1859 function-specifier declaration-specifiers[opt]
1861 Function specifiers (inline) are from C99, and are currently
1862 handled as storage class specifiers, as is __thread.
1864 C90 6.5.1, C99 6.7.1:
1865 storage-class-specifier:
1866 typedef
1867 extern
1868 static
1869 auto
1870 register
1872 C99 6.7.4:
1873 function-specifier:
1874 inline
1876 C90 6.5.2, C99 6.7.2:
1877 type-specifier:
1878 void
1879 char
1880 short
1882 long
1883 float
1884 double
1885 signed
1886 unsigned
1887 _Bool
1888 _Complex
1889 [_Imaginary removed in C99 TC2]
1890 struct-or-union-specifier
1891 enum-specifier
1892 typedef-name
1894 (_Bool and _Complex are new in C99.)
1896 C90 6.5.3, C99 6.7.3:
1898 type-qualifier:
1899 const
1900 restrict
1901 volatile
1902 address-space-qualifier
1904 (restrict is new in C99.)
1906 GNU extensions:
1908 declaration-specifiers:
1909 attributes declaration-specifiers[opt]
1911 type-qualifier:
1912 address-space
1914 address-space:
1915 identifier recognized by the target
1917 storage-class-specifier:
1918 __thread
1920 type-specifier:
1921 typeof-specifier
1922 __int128
1923 _Decimal32
1924 _Decimal64
1925 _Decimal128
1926 _Fract
1927 _Accum
1928 _Sat
1930 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1931 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1933 Objective-C:
1935 type-specifier:
1936 class-name objc-protocol-refs[opt]
1937 typedef-name objc-protocol-refs
1938 objc-protocol-refs
1941 static void
1942 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1943 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1944 enum c_lookahead_kind la)
1946 bool attrs_ok = start_attr_ok;
1947 bool seen_type = specs->typespec_kind != ctsk_none;
1949 if (!typespec_ok)
1950 gcc_assert (la == cla_prefer_id);
1952 while (c_parser_next_token_is (parser, CPP_NAME)
1953 || c_parser_next_token_is (parser, CPP_KEYWORD)
1954 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1956 struct c_typespec t;
1957 tree attrs;
1958 location_t loc = c_parser_peek_token (parser)->location;
1960 /* If we cannot accept a type, exit if the next token must start
1961 one. Also, if we already have seen a tagged definition,
1962 a typename would be an error anyway and likely the user
1963 has simply forgotten a semicolon, so we exit. */
1964 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
1965 && c_parser_next_tokens_start_typename (parser, la)
1966 && !c_parser_next_token_is_qualifier (parser))
1967 break;
1969 if (c_parser_next_token_is (parser, CPP_NAME))
1971 tree value = c_parser_peek_token (parser)->value;
1972 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1974 if (kind == C_ID_ADDRSPACE)
1976 addr_space_t as
1977 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1978 declspecs_add_addrspace (specs, as);
1979 c_parser_consume_token (parser);
1980 attrs_ok = true;
1981 continue;
1984 gcc_assert (!c_parser_next_token_is_qualifier (parser));
1986 /* If we cannot accept a type, and the next token must start one,
1987 exit. Do the same if we already have seen a tagged definition,
1988 since it would be an error anyway and likely the user has simply
1989 forgotten a semicolon. */
1990 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
1991 break;
1993 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
1994 a C_ID_CLASSNAME. */
1995 c_parser_consume_token (parser);
1996 seen_type = true;
1997 attrs_ok = true;
1998 if (kind == C_ID_ID)
2000 error ("unknown type name %qE", value);
2001 t.kind = ctsk_typedef;
2002 t.spec = error_mark_node;
2004 else if (kind == C_ID_TYPENAME
2005 && (!c_dialect_objc ()
2006 || c_parser_next_token_is_not (parser, CPP_LESS)))
2008 t.kind = ctsk_typedef;
2009 /* For a typedef name, record the meaning, not the name.
2010 In case of 'foo foo, bar;'. */
2011 t.spec = lookup_name (value);
2013 else
2015 tree proto = NULL_TREE;
2016 gcc_assert (c_dialect_objc ());
2017 t.kind = ctsk_objc;
2018 if (c_parser_next_token_is (parser, CPP_LESS))
2019 proto = c_parser_objc_protocol_refs (parser);
2020 t.spec = objc_get_protocol_qualified_type (value, proto);
2022 t.expr = NULL_TREE;
2023 t.expr_const_operands = true;
2024 declspecs_add_type (loc, specs, t);
2025 continue;
2027 if (c_parser_next_token_is (parser, CPP_LESS))
2029 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2030 nisse@lysator.liu.se. */
2031 tree proto;
2032 gcc_assert (c_dialect_objc ());
2033 if (!typespec_ok || seen_type)
2034 break;
2035 proto = c_parser_objc_protocol_refs (parser);
2036 t.kind = ctsk_objc;
2037 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2038 t.expr = NULL_TREE;
2039 t.expr_const_operands = true;
2040 declspecs_add_type (loc, specs, t);
2041 continue;
2043 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2044 switch (c_parser_peek_token (parser)->keyword)
2046 case RID_STATIC:
2047 case RID_EXTERN:
2048 case RID_REGISTER:
2049 case RID_TYPEDEF:
2050 case RID_INLINE:
2051 case RID_AUTO:
2052 case RID_THREAD:
2053 if (!scspec_ok)
2054 goto out;
2055 attrs_ok = true;
2056 /* TODO: Distinguish between function specifiers (inline)
2057 and storage class specifiers, either here or in
2058 declspecs_add_scspec. */
2059 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2060 c_parser_consume_token (parser);
2061 break;
2062 case RID_UNSIGNED:
2063 case RID_LONG:
2064 case RID_INT128:
2065 case RID_SHORT:
2066 case RID_SIGNED:
2067 case RID_COMPLEX:
2068 case RID_INT:
2069 case RID_CHAR:
2070 case RID_FLOAT:
2071 case RID_DOUBLE:
2072 case RID_VOID:
2073 case RID_DFLOAT32:
2074 case RID_DFLOAT64:
2075 case RID_DFLOAT128:
2076 case RID_BOOL:
2077 case RID_FRACT:
2078 case RID_ACCUM:
2079 case RID_SAT:
2080 if (!typespec_ok)
2081 goto out;
2082 attrs_ok = true;
2083 seen_type = true;
2084 if (c_dialect_objc ())
2085 parser->objc_need_raw_identifier = true;
2086 t.kind = ctsk_resword;
2087 t.spec = c_parser_peek_token (parser)->value;
2088 t.expr = NULL_TREE;
2089 t.expr_const_operands = true;
2090 declspecs_add_type (loc, specs, t);
2091 c_parser_consume_token (parser);
2092 break;
2093 case RID_ENUM:
2094 if (!typespec_ok)
2095 goto out;
2096 attrs_ok = true;
2097 seen_type = true;
2098 t = c_parser_enum_specifier (parser);
2099 declspecs_add_type (loc, specs, t);
2100 break;
2101 case RID_STRUCT:
2102 case RID_UNION:
2103 if (!typespec_ok)
2104 goto out;
2105 attrs_ok = true;
2106 seen_type = true;
2107 t = c_parser_struct_or_union_specifier (parser);
2108 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2109 declspecs_add_type (loc, specs, t);
2110 break;
2111 case RID_TYPEOF:
2112 /* ??? The old parser rejected typeof after other type
2113 specifiers, but is a syntax error the best way of
2114 handling this? */
2115 if (!typespec_ok || seen_type)
2116 goto out;
2117 attrs_ok = true;
2118 seen_type = true;
2119 t = c_parser_typeof_specifier (parser);
2120 declspecs_add_type (loc, specs, t);
2121 break;
2122 case RID_CONST:
2123 case RID_VOLATILE:
2124 case RID_RESTRICT:
2125 attrs_ok = true;
2126 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2127 c_parser_consume_token (parser);
2128 break;
2129 case RID_ATTRIBUTE:
2130 if (!attrs_ok)
2131 goto out;
2132 attrs = c_parser_attributes (parser);
2133 declspecs_add_attrs (specs, attrs);
2134 break;
2135 default:
2136 goto out;
2139 out: ;
2142 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2144 enum-specifier:
2145 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2146 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2147 enum attributes[opt] identifier
2149 The form with trailing comma is new in C99. The forms with
2150 attributes are GNU extensions. In GNU C, we accept any expression
2151 without commas in the syntax (assignment expressions, not just
2152 conditional expressions); assignment expressions will be diagnosed
2153 as non-constant.
2155 enumerator-list:
2156 enumerator
2157 enumerator-list , enumerator
2159 enumerator:
2160 enumeration-constant
2161 enumeration-constant = constant-expression
2164 static struct c_typespec
2165 c_parser_enum_specifier (c_parser *parser)
2167 struct c_typespec ret;
2168 tree attrs;
2169 tree ident = NULL_TREE;
2170 location_t enum_loc;
2171 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2172 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2173 enum_loc = c_parser_peek_token (parser)->location;
2174 c_parser_consume_token (parser);
2175 attrs = c_parser_attributes (parser);
2176 enum_loc = c_parser_peek_token (parser)->location;
2177 /* Set the location in case we create a decl now. */
2178 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2179 if (c_parser_next_token_is (parser, CPP_NAME))
2181 ident = c_parser_peek_token (parser)->value;
2182 ident_loc = c_parser_peek_token (parser)->location;
2183 enum_loc = ident_loc;
2184 c_parser_consume_token (parser);
2186 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2188 /* Parse an enum definition. */
2189 struct c_enum_contents the_enum;
2190 tree type = start_enum (enum_loc, &the_enum, ident);
2191 tree postfix_attrs;
2192 /* We chain the enumerators in reverse order, then put them in
2193 forward order at the end. */
2194 tree values = NULL_TREE;
2195 c_parser_consume_token (parser);
2196 while (true)
2198 tree enum_id;
2199 tree enum_value;
2200 tree enum_decl;
2201 bool seen_comma;
2202 c_token *token;
2203 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2204 location_t decl_loc, value_loc;
2205 if (c_parser_next_token_is_not (parser, CPP_NAME))
2207 c_parser_error (parser, "expected identifier");
2208 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2209 values = error_mark_node;
2210 break;
2212 token = c_parser_peek_token (parser);
2213 enum_id = token->value;
2214 /* Set the location in case we create a decl now. */
2215 c_parser_set_source_position_from_token (token);
2216 decl_loc = value_loc = token->location;
2217 c_parser_consume_token (parser);
2218 if (c_parser_next_token_is (parser, CPP_EQ))
2220 c_parser_consume_token (parser);
2221 value_loc = c_parser_peek_token (parser)->location;
2222 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2224 else
2225 enum_value = NULL_TREE;
2226 enum_decl = build_enumerator (decl_loc, value_loc,
2227 &the_enum, enum_id, enum_value);
2228 TREE_CHAIN (enum_decl) = values;
2229 values = enum_decl;
2230 seen_comma = false;
2231 if (c_parser_next_token_is (parser, CPP_COMMA))
2233 comma_loc = c_parser_peek_token (parser)->location;
2234 seen_comma = true;
2235 c_parser_consume_token (parser);
2237 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2239 if (seen_comma && !flag_isoc99)
2240 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2241 c_parser_consume_token (parser);
2242 break;
2244 if (!seen_comma)
2246 c_parser_error (parser, "expected %<,%> or %<}%>");
2247 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2248 values = error_mark_node;
2249 break;
2252 postfix_attrs = c_parser_attributes (parser);
2253 ret.spec = finish_enum (type, nreverse (values),
2254 chainon (attrs, postfix_attrs));
2255 ret.kind = ctsk_tagdef;
2256 ret.expr = NULL_TREE;
2257 ret.expr_const_operands = true;
2258 return ret;
2260 else if (!ident)
2262 c_parser_error (parser, "expected %<{%>");
2263 ret.spec = error_mark_node;
2264 ret.kind = ctsk_tagref;
2265 ret.expr = NULL_TREE;
2266 ret.expr_const_operands = true;
2267 return ret;
2269 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2270 /* In ISO C, enumerated types can be referred to only if already
2271 defined. */
2272 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2274 gcc_assert (ident);
2275 pedwarn (enum_loc, OPT_pedantic,
2276 "ISO C forbids forward references to %<enum%> types");
2278 return ret;
2281 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2283 struct-or-union-specifier:
2284 struct-or-union attributes[opt] identifier[opt]
2285 { struct-contents } attributes[opt]
2286 struct-or-union attributes[opt] identifier
2288 struct-contents:
2289 struct-declaration-list
2291 struct-declaration-list:
2292 struct-declaration ;
2293 struct-declaration-list struct-declaration ;
2295 GNU extensions:
2297 struct-contents:
2298 empty
2299 struct-declaration
2300 struct-declaration-list struct-declaration
2302 struct-declaration-list:
2303 struct-declaration-list ;
2306 (Note that in the syntax here, unlike that in ISO C, the semicolons
2307 are included here rather than in struct-declaration, in order to
2308 describe the syntax with extra semicolons and missing semicolon at
2309 end.)
2311 Objective-C:
2313 struct-declaration-list:
2314 @defs ( class-name )
2316 (Note this does not include a trailing semicolon, but can be
2317 followed by further declarations, and gets a pedwarn-if-pedantic
2318 when followed by a semicolon.) */
2320 static struct c_typespec
2321 c_parser_struct_or_union_specifier (c_parser *parser)
2323 struct c_typespec ret;
2324 tree attrs;
2325 tree ident = NULL_TREE;
2326 location_t struct_loc;
2327 location_t ident_loc = UNKNOWN_LOCATION;
2328 enum tree_code code;
2329 switch (c_parser_peek_token (parser)->keyword)
2331 case RID_STRUCT:
2332 code = RECORD_TYPE;
2333 break;
2334 case RID_UNION:
2335 code = UNION_TYPE;
2336 break;
2337 default:
2338 gcc_unreachable ();
2340 struct_loc = c_parser_peek_token (parser)->location;
2341 c_parser_consume_token (parser);
2342 attrs = c_parser_attributes (parser);
2344 /* Set the location in case we create a decl now. */
2345 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2347 if (c_parser_next_token_is (parser, CPP_NAME))
2349 ident = c_parser_peek_token (parser)->value;
2350 ident_loc = c_parser_peek_token (parser)->location;
2351 struct_loc = ident_loc;
2352 c_parser_consume_token (parser);
2354 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2356 /* Parse a struct or union definition. Start the scope of the
2357 tag before parsing components. */
2358 struct c_struct_parse_info *struct_info;
2359 tree type = start_struct (struct_loc, code, ident, &struct_info);
2360 tree postfix_attrs;
2361 /* We chain the components in reverse order, then put them in
2362 forward order at the end. Each struct-declaration may
2363 declare multiple components (comma-separated), so we must use
2364 chainon to join them, although when parsing each
2365 struct-declaration we can use TREE_CHAIN directly.
2367 The theory behind all this is that there will be more
2368 semicolon separated fields than comma separated fields, and
2369 so we'll be minimizing the number of node traversals required
2370 by chainon. */
2371 tree contents = NULL_TREE;
2372 c_parser_consume_token (parser);
2373 /* Handle the Objective-C @defs construct,
2374 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2375 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2377 tree name;
2378 gcc_assert (c_dialect_objc ());
2379 c_parser_consume_token (parser);
2380 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2381 goto end_at_defs;
2382 if (c_parser_next_token_is (parser, CPP_NAME)
2383 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2385 name = c_parser_peek_token (parser)->value;
2386 c_parser_consume_token (parser);
2388 else
2390 c_parser_error (parser, "expected class name");
2391 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2392 goto end_at_defs;
2394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2395 "expected %<)%>");
2396 contents = nreverse (objc_get_class_ivars (name));
2398 end_at_defs:
2399 /* Parse the struct-declarations and semicolons. Problems with
2400 semicolons are diagnosed here; empty structures are diagnosed
2401 elsewhere. */
2402 while (true)
2404 tree decls;
2405 /* Parse any stray semicolon. */
2406 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2408 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2409 "extra semicolon in struct or union specified");
2410 c_parser_consume_token (parser);
2411 continue;
2413 /* Stop if at the end of the struct or union contents. */
2414 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2416 c_parser_consume_token (parser);
2417 break;
2419 /* Accept #pragmas at struct scope. */
2420 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2422 c_parser_pragma (parser, pragma_external);
2423 continue;
2425 /* Parse some comma-separated declarations, but not the
2426 trailing semicolon if any. */
2427 decls = c_parser_struct_declaration (parser);
2428 contents = chainon (decls, contents);
2429 /* If no semicolon follows, either we have a parse error or
2430 are at the end of the struct or union and should
2431 pedwarn. */
2432 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2433 c_parser_consume_token (parser);
2434 else
2436 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2437 pedwarn (c_parser_peek_token (parser)->location, 0,
2438 "no semicolon at end of struct or union");
2439 else if (parser->error
2440 || !c_parser_next_token_starts_declspecs (parser))
2442 c_parser_error (parser, "expected %<;%>");
2443 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2444 break;
2447 /* If we come here, we have already emitted an error
2448 for an expected `;', identifier or `(', and we also
2449 recovered already. Go on with the next field. */
2452 postfix_attrs = c_parser_attributes (parser);
2453 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2454 chainon (attrs, postfix_attrs), struct_info);
2455 ret.kind = ctsk_tagdef;
2456 ret.expr = NULL_TREE;
2457 ret.expr_const_operands = true;
2458 return ret;
2460 else if (!ident)
2462 c_parser_error (parser, "expected %<{%>");
2463 ret.spec = error_mark_node;
2464 ret.kind = ctsk_tagref;
2465 ret.expr = NULL_TREE;
2466 ret.expr_const_operands = true;
2467 return ret;
2469 ret = parser_xref_tag (ident_loc, code, ident);
2470 return ret;
2473 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2474 the trailing semicolon.
2476 struct-declaration:
2477 specifier-qualifier-list struct-declarator-list
2478 static_assert-declaration-no-semi
2480 specifier-qualifier-list:
2481 type-specifier specifier-qualifier-list[opt]
2482 type-qualifier specifier-qualifier-list[opt]
2483 attributes specifier-qualifier-list[opt]
2485 struct-declarator-list:
2486 struct-declarator
2487 struct-declarator-list , attributes[opt] struct-declarator
2489 struct-declarator:
2490 declarator attributes[opt]
2491 declarator[opt] : constant-expression attributes[opt]
2493 GNU extensions:
2495 struct-declaration:
2496 __extension__ struct-declaration
2497 specifier-qualifier-list
2499 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2500 of attributes where shown is a GNU extension. In GNU C, we accept
2501 any expression without commas in the syntax (assignment
2502 expressions, not just conditional expressions); assignment
2503 expressions will be diagnosed as non-constant. */
2505 static tree
2506 c_parser_struct_declaration (c_parser *parser)
2508 struct c_declspecs *specs;
2509 tree prefix_attrs;
2510 tree all_prefix_attrs;
2511 tree decls;
2512 location_t decl_loc;
2513 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2515 int ext;
2516 tree decl;
2517 ext = disable_extension_diagnostics ();
2518 c_parser_consume_token (parser);
2519 decl = c_parser_struct_declaration (parser);
2520 restore_extension_diagnostics (ext);
2521 return decl;
2523 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2525 c_parser_static_assert_declaration_no_semi (parser);
2526 return NULL_TREE;
2528 specs = build_null_declspecs ();
2529 decl_loc = c_parser_peek_token (parser)->location;
2530 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2531 if (parser->error)
2532 return NULL_TREE;
2533 if (!specs->declspecs_seen_p)
2535 c_parser_error (parser, "expected specifier-qualifier-list");
2536 return NULL_TREE;
2538 finish_declspecs (specs);
2539 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2540 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2542 tree ret;
2543 if (specs->typespec_kind == ctsk_none)
2545 pedwarn (decl_loc, OPT_pedantic,
2546 "ISO C forbids member declarations with no members");
2547 shadow_tag_warned (specs, pedantic);
2548 ret = NULL_TREE;
2550 else
2552 /* Support for unnamed structs or unions as members of
2553 structs or unions (which is [a] useful and [b] supports
2554 MS P-SDK). */
2555 tree attrs = NULL;
2557 ret = grokfield (c_parser_peek_token (parser)->location,
2558 build_id_declarator (NULL_TREE), specs,
2559 NULL_TREE, &attrs);
2560 if (ret)
2561 decl_attributes (&ret, attrs, 0);
2563 return ret;
2566 /* Provide better error recovery. Note that a type name here is valid,
2567 and will be treated as a field name. */
2568 if (specs->typespec_kind == ctsk_tagdef
2569 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2570 && c_parser_next_token_starts_declspecs (parser)
2571 && !c_parser_next_token_is (parser, CPP_NAME))
2573 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2574 parser->error = false;
2575 return NULL_TREE;
2578 pending_xref_error ();
2579 prefix_attrs = specs->attrs;
2580 all_prefix_attrs = prefix_attrs;
2581 specs->attrs = NULL_TREE;
2582 decls = NULL_TREE;
2583 while (true)
2585 /* Declaring one or more declarators or un-named bit-fields. */
2586 struct c_declarator *declarator;
2587 bool dummy = false;
2588 if (c_parser_next_token_is (parser, CPP_COLON))
2589 declarator = build_id_declarator (NULL_TREE);
2590 else
2591 declarator = c_parser_declarator (parser,
2592 specs->typespec_kind != ctsk_none,
2593 C_DTR_NORMAL, &dummy);
2594 if (declarator == NULL)
2596 c_parser_skip_to_end_of_block_or_statement (parser);
2597 break;
2599 if (c_parser_next_token_is (parser, CPP_COLON)
2600 || c_parser_next_token_is (parser, CPP_COMMA)
2601 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2602 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2603 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2605 tree postfix_attrs = NULL_TREE;
2606 tree width = NULL_TREE;
2607 tree d;
2608 if (c_parser_next_token_is (parser, CPP_COLON))
2610 c_parser_consume_token (parser);
2611 width = c_parser_expr_no_commas (parser, NULL).value;
2613 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2614 postfix_attrs = c_parser_attributes (parser);
2615 d = grokfield (c_parser_peek_token (parser)->location,
2616 declarator, specs, width, &all_prefix_attrs);
2617 decl_attributes (&d, chainon (postfix_attrs,
2618 all_prefix_attrs), 0);
2619 DECL_CHAIN (d) = decls;
2620 decls = d;
2621 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2622 all_prefix_attrs = chainon (c_parser_attributes (parser),
2623 prefix_attrs);
2624 else
2625 all_prefix_attrs = prefix_attrs;
2626 if (c_parser_next_token_is (parser, CPP_COMMA))
2627 c_parser_consume_token (parser);
2628 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2629 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2631 /* Semicolon consumed in caller. */
2632 break;
2634 else
2636 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2637 break;
2640 else
2642 c_parser_error (parser,
2643 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2644 "%<__attribute__%>");
2645 break;
2648 return decls;
2651 /* Parse a typeof specifier (a GNU extension).
2653 typeof-specifier:
2654 typeof ( expression )
2655 typeof ( type-name )
2658 static struct c_typespec
2659 c_parser_typeof_specifier (c_parser *parser)
2661 struct c_typespec ret;
2662 ret.kind = ctsk_typeof;
2663 ret.spec = error_mark_node;
2664 ret.expr = NULL_TREE;
2665 ret.expr_const_operands = true;
2666 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2667 c_parser_consume_token (parser);
2668 c_inhibit_evaluation_warnings++;
2669 in_typeof++;
2670 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2672 c_inhibit_evaluation_warnings--;
2673 in_typeof--;
2674 return ret;
2676 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2678 struct c_type_name *type = c_parser_type_name (parser);
2679 c_inhibit_evaluation_warnings--;
2680 in_typeof--;
2681 if (type != NULL)
2683 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2684 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2687 else
2689 bool was_vm;
2690 location_t here = c_parser_peek_token (parser)->location;
2691 struct c_expr expr = c_parser_expression (parser);
2692 c_inhibit_evaluation_warnings--;
2693 in_typeof--;
2694 if (TREE_CODE (expr.value) == COMPONENT_REF
2695 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2696 error_at (here, "%<typeof%> applied to a bit-field");
2697 mark_exp_read (expr.value);
2698 ret.spec = TREE_TYPE (expr.value);
2699 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2700 /* This is returned with the type so that when the type is
2701 evaluated, this can be evaluated. */
2702 if (was_vm)
2703 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2704 pop_maybe_used (was_vm);
2706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2707 return ret;
2710 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2711 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2712 be redeclared; otherwise it may not. KIND indicates which kind of
2713 declarator is wanted. Returns a valid declarator except in the
2714 case of a syntax error in which case NULL is returned. *SEEN_ID is
2715 set to true if an identifier being declared is seen; this is used
2716 to diagnose bad forms of abstract array declarators and to
2717 determine whether an identifier list is syntactically permitted.
2719 declarator:
2720 pointer[opt] direct-declarator
2722 direct-declarator:
2723 identifier
2724 ( attributes[opt] declarator )
2725 direct-declarator array-declarator
2726 direct-declarator ( parameter-type-list )
2727 direct-declarator ( identifier-list[opt] )
2729 pointer:
2730 * type-qualifier-list[opt]
2731 * type-qualifier-list[opt] pointer
2733 type-qualifier-list:
2734 type-qualifier
2735 attributes
2736 type-qualifier-list type-qualifier
2737 type-qualifier-list attributes
2739 parameter-type-list:
2740 parameter-list
2741 parameter-list , ...
2743 parameter-list:
2744 parameter-declaration
2745 parameter-list , parameter-declaration
2747 parameter-declaration:
2748 declaration-specifiers declarator attributes[opt]
2749 declaration-specifiers abstract-declarator[opt] attributes[opt]
2751 identifier-list:
2752 identifier
2753 identifier-list , identifier
2755 abstract-declarator:
2756 pointer
2757 pointer[opt] direct-abstract-declarator
2759 direct-abstract-declarator:
2760 ( attributes[opt] abstract-declarator )
2761 direct-abstract-declarator[opt] array-declarator
2762 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2764 GNU extensions:
2766 direct-declarator:
2767 direct-declarator ( parameter-forward-declarations
2768 parameter-type-list[opt] )
2770 direct-abstract-declarator:
2771 direct-abstract-declarator[opt] ( parameter-forward-declarations
2772 parameter-type-list[opt] )
2774 parameter-forward-declarations:
2775 parameter-list ;
2776 parameter-forward-declarations parameter-list ;
2778 The uses of attributes shown above are GNU extensions.
2780 Some forms of array declarator are not included in C99 in the
2781 syntax for abstract declarators; these are disallowed elsewhere.
2782 This may be a defect (DR#289).
2784 This function also accepts an omitted abstract declarator as being
2785 an abstract declarator, although not part of the formal syntax. */
2787 static struct c_declarator *
2788 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2789 bool *seen_id)
2791 /* Parse any initial pointer part. */
2792 if (c_parser_next_token_is (parser, CPP_MULT))
2794 struct c_declspecs *quals_attrs = build_null_declspecs ();
2795 struct c_declarator *inner;
2796 c_parser_consume_token (parser);
2797 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2798 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2799 if (inner == NULL)
2800 return NULL;
2801 else
2802 return make_pointer_declarator (quals_attrs, inner);
2804 /* Now we have a direct declarator, direct abstract declarator or
2805 nothing (which counts as a direct abstract declarator here). */
2806 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2809 /* Parse a direct declarator or direct abstract declarator; arguments
2810 as c_parser_declarator. */
2812 static struct c_declarator *
2813 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2814 bool *seen_id)
2816 /* The direct declarator must start with an identifier (possibly
2817 omitted) or a parenthesized declarator (possibly abstract). In
2818 an ordinary declarator, initial parentheses must start a
2819 parenthesized declarator. In an abstract declarator or parameter
2820 declarator, they could start a parenthesized declarator or a
2821 parameter list. To tell which, the open parenthesis and any
2822 following attributes must be read. If a declaration specifier
2823 follows, then it is a parameter list; if the specifier is a
2824 typedef name, there might be an ambiguity about redeclaring it,
2825 which is resolved in the direction of treating it as a typedef
2826 name. If a close parenthesis follows, it is also an empty
2827 parameter list, as the syntax does not permit empty abstract
2828 declarators. Otherwise, it is a parenthesized declarator (in
2829 which case the analysis may be repeated inside it, recursively).
2831 ??? There is an ambiguity in a parameter declaration "int
2832 (__attribute__((foo)) x)", where x is not a typedef name: it
2833 could be an abstract declarator for a function, or declare x with
2834 parentheses. The proper resolution of this ambiguity needs
2835 documenting. At present we follow an accident of the old
2836 parser's implementation, whereby the first parameter must have
2837 some declaration specifiers other than just attributes. Thus as
2838 a parameter declaration it is treated as a parenthesized
2839 parameter named x, and as an abstract declarator it is
2840 rejected.
2842 ??? Also following the old parser, attributes inside an empty
2843 parameter list are ignored, making it a list not yielding a
2844 prototype, rather than giving an error or making it have one
2845 parameter with implicit type int.
2847 ??? Also following the old parser, typedef names may be
2848 redeclared in declarators, but not Objective-C class names. */
2850 if (kind != C_DTR_ABSTRACT
2851 && c_parser_next_token_is (parser, CPP_NAME)
2852 && ((type_seen_p
2853 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2854 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2855 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2857 struct c_declarator *inner
2858 = build_id_declarator (c_parser_peek_token (parser)->value);
2859 *seen_id = true;
2860 inner->id_loc = c_parser_peek_token (parser)->location;
2861 c_parser_consume_token (parser);
2862 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2865 if (kind != C_DTR_NORMAL
2866 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2868 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2869 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2872 /* Either we are at the end of an abstract declarator, or we have
2873 parentheses. */
2875 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2877 tree attrs;
2878 struct c_declarator *inner;
2879 c_parser_consume_token (parser);
2880 attrs = c_parser_attributes (parser);
2881 if (kind != C_DTR_NORMAL
2882 && (c_parser_next_token_starts_declspecs (parser)
2883 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2885 struct c_arg_info *args
2886 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2887 attrs);
2888 if (args == NULL)
2889 return NULL;
2890 else
2892 inner
2893 = build_function_declarator (args,
2894 build_id_declarator (NULL_TREE));
2895 return c_parser_direct_declarator_inner (parser, *seen_id,
2896 inner);
2899 /* A parenthesized declarator. */
2900 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2901 if (inner != NULL && attrs != NULL)
2902 inner = build_attrs_declarator (attrs, inner);
2903 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2905 c_parser_consume_token (parser);
2906 if (inner == NULL)
2907 return NULL;
2908 else
2909 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2911 else
2913 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2914 "expected %<)%>");
2915 return NULL;
2918 else
2920 if (kind == C_DTR_NORMAL)
2922 c_parser_error (parser, "expected identifier or %<(%>");
2923 return NULL;
2925 else
2926 return build_id_declarator (NULL_TREE);
2930 /* Parse part of a direct declarator or direct abstract declarator,
2931 given that some (in INNER) has already been parsed; ID_PRESENT is
2932 true if an identifier is present, false for an abstract
2933 declarator. */
2935 static struct c_declarator *
2936 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2937 struct c_declarator *inner)
2939 /* Parse a sequence of array declarators and parameter lists. */
2940 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2942 location_t brace_loc = c_parser_peek_token (parser)->location;
2943 struct c_declarator *declarator;
2944 struct c_declspecs *quals_attrs = build_null_declspecs ();
2945 bool static_seen;
2946 bool star_seen;
2947 tree dimen;
2948 c_parser_consume_token (parser);
2949 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2950 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2951 if (static_seen)
2952 c_parser_consume_token (parser);
2953 if (static_seen && !quals_attrs->declspecs_seen_p)
2954 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2955 if (!quals_attrs->declspecs_seen_p)
2956 quals_attrs = NULL;
2957 /* If "static" is present, there must be an array dimension.
2958 Otherwise, there may be a dimension, "*", or no
2959 dimension. */
2960 if (static_seen)
2962 star_seen = false;
2963 dimen = c_parser_expr_no_commas (parser, NULL).value;
2965 else
2967 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2969 dimen = NULL_TREE;
2970 star_seen = false;
2972 else if (c_parser_next_token_is (parser, CPP_MULT))
2974 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2976 dimen = NULL_TREE;
2977 star_seen = true;
2978 c_parser_consume_token (parser);
2980 else
2982 star_seen = false;
2983 dimen = c_parser_expr_no_commas (parser, NULL).value;
2986 else
2988 star_seen = false;
2989 dimen = c_parser_expr_no_commas (parser, NULL).value;
2992 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2993 c_parser_consume_token (parser);
2994 else
2996 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2997 "expected %<]%>");
2998 return NULL;
3000 if (dimen)
3001 mark_exp_read (dimen);
3002 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3003 static_seen, star_seen);
3004 if (declarator == NULL)
3005 return NULL;
3006 inner = set_array_declarator_inner (declarator, inner);
3007 return c_parser_direct_declarator_inner (parser, id_present, inner);
3009 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3011 tree attrs;
3012 struct c_arg_info *args;
3013 c_parser_consume_token (parser);
3014 attrs = c_parser_attributes (parser);
3015 args = c_parser_parms_declarator (parser, id_present, attrs);
3016 if (args == NULL)
3017 return NULL;
3018 else
3020 inner = build_function_declarator (args, inner);
3021 return c_parser_direct_declarator_inner (parser, id_present, inner);
3024 return inner;
3027 /* Parse a parameter list or identifier list, including the closing
3028 parenthesis but not the opening one. ATTRS are the attributes at
3029 the start of the list. ID_LIST_OK is true if an identifier list is
3030 acceptable; such a list must not have attributes at the start. */
3032 static struct c_arg_info *
3033 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3035 push_scope ();
3036 declare_parm_level ();
3037 /* If the list starts with an identifier, it is an identifier list.
3038 Otherwise, it is either a prototype list or an empty list. */
3039 if (id_list_ok
3040 && !attrs
3041 && c_parser_next_token_is (parser, CPP_NAME)
3042 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3044 /* Look ahead to detect typos in type names. */
3045 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3046 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3047 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3048 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3050 tree list = NULL_TREE, *nextp = &list;
3051 while (c_parser_next_token_is (parser, CPP_NAME)
3052 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3054 *nextp = build_tree_list (NULL_TREE,
3055 c_parser_peek_token (parser)->value);
3056 nextp = & TREE_CHAIN (*nextp);
3057 c_parser_consume_token (parser);
3058 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3059 break;
3060 c_parser_consume_token (parser);
3061 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3063 c_parser_error (parser, "expected identifier");
3064 break;
3067 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3069 struct c_arg_info *ret = build_arg_info ();
3070 ret->types = list;
3071 c_parser_consume_token (parser);
3072 pop_scope ();
3073 return ret;
3075 else
3077 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3078 "expected %<)%>");
3079 pop_scope ();
3080 return NULL;
3083 else
3085 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
3086 pop_scope ();
3087 return ret;
3091 /* Parse a parameter list (possibly empty), including the closing
3092 parenthesis but not the opening one. ATTRS are the attributes at
3093 the start of the list. */
3095 static struct c_arg_info *
3096 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
3098 bool bad_parm = false;
3099 /* ??? Following the old parser, forward parameter declarations may
3100 use abstract declarators, and if no real parameter declarations
3101 follow the forward declarations then this is not diagnosed. Also
3102 note as above that attributes are ignored as the only contents of
3103 the parentheses, or as the only contents after forward
3104 declarations. */
3105 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3107 struct c_arg_info *ret = build_arg_info ();
3108 c_parser_consume_token (parser);
3109 return ret;
3111 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3113 struct c_arg_info *ret = build_arg_info ();
3114 /* Suppress -Wold-style-definition for this case. */
3115 ret->types = error_mark_node;
3116 error_at (c_parser_peek_token (parser)->location,
3117 "ISO C requires a named argument before %<...%>");
3118 c_parser_consume_token (parser);
3119 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3121 c_parser_consume_token (parser);
3122 return ret;
3124 else
3126 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3127 "expected %<)%>");
3128 return NULL;
3131 /* Nonempty list of parameters, either terminated with semicolon
3132 (forward declarations; recurse) or with close parenthesis (normal
3133 function) or with ", ... )" (variadic function). */
3134 while (true)
3136 /* Parse a parameter. */
3137 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3138 attrs = NULL_TREE;
3139 if (parm == NULL)
3140 bad_parm = true;
3141 else
3142 push_parm_decl (parm);
3143 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3145 tree new_attrs;
3146 c_parser_consume_token (parser);
3147 mark_forward_parm_decls ();
3148 new_attrs = c_parser_attributes (parser);
3149 return c_parser_parms_list_declarator (parser, new_attrs);
3151 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3153 c_parser_consume_token (parser);
3154 if (bad_parm)
3156 get_pending_sizes ();
3157 return NULL;
3159 else
3160 return get_parm_info (false);
3162 if (!c_parser_require (parser, CPP_COMMA,
3163 "expected %<;%>, %<,%> or %<)%>"))
3165 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3166 get_pending_sizes ();
3167 return NULL;
3169 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3171 c_parser_consume_token (parser);
3172 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3174 c_parser_consume_token (parser);
3175 if (bad_parm)
3177 get_pending_sizes ();
3178 return NULL;
3180 else
3181 return get_parm_info (true);
3183 else
3185 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3186 "expected %<)%>");
3187 get_pending_sizes ();
3188 return NULL;
3194 /* Parse a parameter declaration. ATTRS are the attributes at the
3195 start of the declaration if it is the first parameter. */
3197 static struct c_parm *
3198 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3200 struct c_declspecs *specs;
3201 struct c_declarator *declarator;
3202 tree prefix_attrs;
3203 tree postfix_attrs = NULL_TREE;
3204 bool dummy = false;
3205 if (!c_parser_next_token_starts_declspecs (parser))
3207 c_token *token = c_parser_peek_token (parser);
3208 if (parser->error)
3209 return NULL;
3210 c_parser_set_source_position_from_token (token);
3211 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3213 error ("unknown type name %qE", token->value);
3214 parser->error = true;
3216 /* ??? In some Objective-C cases '...' isn't applicable so there
3217 should be a different message. */
3218 else
3219 c_parser_error (parser,
3220 "expected declaration specifiers or %<...%>");
3221 c_parser_skip_to_end_of_parameter (parser);
3222 return NULL;
3224 specs = build_null_declspecs ();
3225 if (attrs)
3227 declspecs_add_attrs (specs, attrs);
3228 attrs = NULL_TREE;
3230 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3231 finish_declspecs (specs);
3232 pending_xref_error ();
3233 prefix_attrs = specs->attrs;
3234 specs->attrs = NULL_TREE;
3235 declarator = c_parser_declarator (parser,
3236 specs->typespec_kind != ctsk_none,
3237 C_DTR_PARM, &dummy);
3238 if (declarator == NULL)
3240 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3241 return NULL;
3243 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3244 postfix_attrs = c_parser_attributes (parser);
3245 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3246 declarator);
3249 /* Parse a string literal in an asm expression. It should not be
3250 translated, and wide string literals are an error although
3251 permitted by the syntax. This is a GNU extension.
3253 asm-string-literal:
3254 string-literal
3256 ??? At present, following the old parser, the caller needs to have
3257 set lex_untranslated_string to 1. It would be better to follow the
3258 C++ parser rather than using this kludge. */
3260 static tree
3261 c_parser_asm_string_literal (c_parser *parser)
3263 tree str;
3264 if (c_parser_next_token_is (parser, CPP_STRING))
3266 str = c_parser_peek_token (parser)->value;
3267 c_parser_consume_token (parser);
3269 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3271 error_at (c_parser_peek_token (parser)->location,
3272 "wide string literal in %<asm%>");
3273 str = build_string (1, "");
3274 c_parser_consume_token (parser);
3276 else
3278 c_parser_error (parser, "expected string literal");
3279 str = NULL_TREE;
3281 return str;
3284 /* Parse a simple asm expression. This is used in restricted
3285 contexts, where a full expression with inputs and outputs does not
3286 make sense. This is a GNU extension.
3288 simple-asm-expr:
3289 asm ( asm-string-literal )
3292 static tree
3293 c_parser_simple_asm_expr (c_parser *parser)
3295 tree str;
3296 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3297 /* ??? Follow the C++ parser rather than using the
3298 lex_untranslated_string kludge. */
3299 parser->lex_untranslated_string = true;
3300 c_parser_consume_token (parser);
3301 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3303 parser->lex_untranslated_string = false;
3304 return NULL_TREE;
3306 str = c_parser_asm_string_literal (parser);
3307 parser->lex_untranslated_string = false;
3308 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3311 return NULL_TREE;
3313 return str;
3316 /* Parse (possibly empty) attributes. This is a GNU extension.
3318 attributes:
3319 empty
3320 attributes attribute
3322 attribute:
3323 __attribute__ ( ( attribute-list ) )
3325 attribute-list:
3326 attrib
3327 attribute_list , attrib
3329 attrib:
3330 empty
3331 any-word
3332 any-word ( identifier )
3333 any-word ( identifier , nonempty-expr-list )
3334 any-word ( expr-list )
3336 where the "identifier" must not be declared as a type, and
3337 "any-word" may be any identifier (including one declared as a
3338 type), a reserved word storage class specifier, type specifier or
3339 type qualifier. ??? This still leaves out most reserved keywords
3340 (following the old parser), shouldn't we include them, and why not
3341 allow identifiers declared as types to start the arguments? */
3343 static tree
3344 c_parser_attributes (c_parser *parser)
3346 tree attrs = NULL_TREE;
3347 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3349 /* ??? Follow the C++ parser rather than using the
3350 lex_untranslated_string kludge. */
3351 parser->lex_untranslated_string = true;
3352 c_parser_consume_token (parser);
3353 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3355 parser->lex_untranslated_string = false;
3356 return attrs;
3358 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3360 parser->lex_untranslated_string = false;
3361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3362 return attrs;
3364 /* Parse the attribute list. */
3365 while (c_parser_next_token_is (parser, CPP_COMMA)
3366 || c_parser_next_token_is (parser, CPP_NAME)
3367 || c_parser_next_token_is (parser, CPP_KEYWORD))
3369 tree attr, attr_name, attr_args;
3370 VEC(tree,gc) *expr_list;
3371 if (c_parser_next_token_is (parser, CPP_COMMA))
3373 c_parser_consume_token (parser);
3374 continue;
3376 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3378 /* ??? See comment above about what keywords are
3379 accepted here. */
3380 bool ok;
3381 switch (c_parser_peek_token (parser)->keyword)
3383 case RID_STATIC:
3384 case RID_UNSIGNED:
3385 case RID_LONG:
3386 case RID_INT128:
3387 case RID_CONST:
3388 case RID_EXTERN:
3389 case RID_REGISTER:
3390 case RID_TYPEDEF:
3391 case RID_SHORT:
3392 case RID_INLINE:
3393 case RID_VOLATILE:
3394 case RID_SIGNED:
3395 case RID_AUTO:
3396 case RID_RESTRICT:
3397 case RID_COMPLEX:
3398 case RID_THREAD:
3399 case RID_INT:
3400 case RID_CHAR:
3401 case RID_FLOAT:
3402 case RID_DOUBLE:
3403 case RID_VOID:
3404 case RID_DFLOAT32:
3405 case RID_DFLOAT64:
3406 case RID_DFLOAT128:
3407 case RID_BOOL:
3408 case RID_FRACT:
3409 case RID_ACCUM:
3410 case RID_SAT:
3411 ok = true;
3412 break;
3413 default:
3414 ok = false;
3415 break;
3417 if (!ok)
3418 break;
3419 /* Accept __attribute__((__const)) as __attribute__((const))
3420 etc. */
3421 attr_name
3422 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3424 else
3425 attr_name = c_parser_peek_token (parser)->value;
3426 c_parser_consume_token (parser);
3427 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3429 attr = build_tree_list (attr_name, NULL_TREE);
3430 attrs = chainon (attrs, attr);
3431 continue;
3433 c_parser_consume_token (parser);
3434 /* Parse the attribute contents. If they start with an
3435 identifier which is followed by a comma or close
3436 parenthesis, then the arguments start with that
3437 identifier; otherwise they are an expression list.
3438 In objective-c the identifier may be a classname. */
3439 if (c_parser_next_token_is (parser, CPP_NAME)
3440 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3441 || (c_dialect_objc ()
3442 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3443 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3444 || (c_parser_peek_2nd_token (parser)->type
3445 == CPP_CLOSE_PAREN)))
3447 tree arg1 = c_parser_peek_token (parser)->value;
3448 c_parser_consume_token (parser);
3449 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3450 attr_args = build_tree_list (NULL_TREE, arg1);
3451 else
3453 tree tree_list;
3454 c_parser_consume_token (parser);
3455 expr_list = c_parser_expr_list (parser, false, true, NULL);
3456 tree_list = build_tree_list_vec (expr_list);
3457 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3458 release_tree_vector (expr_list);
3461 else
3463 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3464 attr_args = NULL_TREE;
3465 else
3467 expr_list = c_parser_expr_list (parser, false, true, NULL);
3468 attr_args = build_tree_list_vec (expr_list);
3469 release_tree_vector (expr_list);
3472 attr = build_tree_list (attr_name, attr_args);
3473 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3474 c_parser_consume_token (parser);
3475 else
3477 parser->lex_untranslated_string = false;
3478 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3479 "expected %<)%>");
3480 return attrs;
3482 attrs = chainon (attrs, attr);
3484 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3485 c_parser_consume_token (parser);
3486 else
3488 parser->lex_untranslated_string = false;
3489 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3490 "expected %<)%>");
3491 return attrs;
3493 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3494 c_parser_consume_token (parser);
3495 else
3497 parser->lex_untranslated_string = false;
3498 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3499 "expected %<)%>");
3500 return attrs;
3502 parser->lex_untranslated_string = false;
3504 return attrs;
3507 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3509 type-name:
3510 specifier-qualifier-list abstract-declarator[opt]
3513 static struct c_type_name *
3514 c_parser_type_name (c_parser *parser)
3516 struct c_declspecs *specs = build_null_declspecs ();
3517 struct c_declarator *declarator;
3518 struct c_type_name *ret;
3519 bool dummy = false;
3520 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3521 if (!specs->declspecs_seen_p)
3523 c_parser_error (parser, "expected specifier-qualifier-list");
3524 return NULL;
3526 if (specs->type != error_mark_node)
3528 pending_xref_error ();
3529 finish_declspecs (specs);
3531 declarator = c_parser_declarator (parser,
3532 specs->typespec_kind != ctsk_none,
3533 C_DTR_ABSTRACT, &dummy);
3534 if (declarator == NULL)
3535 return NULL;
3536 ret = XOBNEW (&parser_obstack, struct c_type_name);
3537 ret->specs = specs;
3538 ret->declarator = declarator;
3539 return ret;
3542 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3544 initializer:
3545 assignment-expression
3546 { initializer-list }
3547 { initializer-list , }
3549 initializer-list:
3550 designation[opt] initializer
3551 initializer-list , designation[opt] initializer
3553 designation:
3554 designator-list =
3556 designator-list:
3557 designator
3558 designator-list designator
3560 designator:
3561 array-designator
3562 . identifier
3564 array-designator:
3565 [ constant-expression ]
3567 GNU extensions:
3569 initializer:
3572 designation:
3573 array-designator
3574 identifier :
3576 array-designator:
3577 [ constant-expression ... constant-expression ]
3579 Any expression without commas is accepted in the syntax for the
3580 constant-expressions, with non-constant expressions rejected later.
3582 This function is only used for top-level initializers; for nested
3583 ones, see c_parser_initval. */
3585 static struct c_expr
3586 c_parser_initializer (c_parser *parser)
3588 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3589 return c_parser_braced_init (parser, NULL_TREE, false);
3590 else
3592 struct c_expr ret;
3593 location_t loc = c_parser_peek_token (parser)->location;
3594 ret = c_parser_expr_no_commas (parser, NULL);
3595 if (TREE_CODE (ret.value) != STRING_CST
3596 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3597 ret = default_function_array_read_conversion (loc, ret);
3598 return ret;
3602 /* Parse a braced initializer list. TYPE is the type specified for a
3603 compound literal, and NULL_TREE for other initializers and for
3604 nested braced lists. NESTED_P is true for nested braced lists,
3605 false for the list of a compound literal or the list that is the
3606 top-level initializer in a declaration. */
3608 static struct c_expr
3609 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3611 struct c_expr ret;
3612 struct obstack braced_init_obstack;
3613 location_t brace_loc = c_parser_peek_token (parser)->location;
3614 gcc_obstack_init (&braced_init_obstack);
3615 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3616 c_parser_consume_token (parser);
3617 if (nested_p)
3618 push_init_level (0, &braced_init_obstack);
3619 else
3620 really_start_incremental_init (type);
3621 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3623 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3625 else
3627 /* Parse a non-empty initializer list, possibly with a trailing
3628 comma. */
3629 while (true)
3631 c_parser_initelt (parser, &braced_init_obstack);
3632 if (parser->error)
3633 break;
3634 if (c_parser_next_token_is (parser, CPP_COMMA))
3635 c_parser_consume_token (parser);
3636 else
3637 break;
3638 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3639 break;
3642 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3644 ret.value = error_mark_node;
3645 ret.original_code = ERROR_MARK;
3646 ret.original_type = NULL;
3647 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3648 pop_init_level (0, &braced_init_obstack);
3649 obstack_free (&braced_init_obstack, NULL);
3650 return ret;
3652 c_parser_consume_token (parser);
3653 ret = pop_init_level (0, &braced_init_obstack);
3654 obstack_free (&braced_init_obstack, NULL);
3655 return ret;
3658 /* Parse a nested initializer, including designators. */
3660 static void
3661 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3663 /* Parse any designator or designator list. A single array
3664 designator may have the subsequent "=" omitted in GNU C, but a
3665 longer list or a structure member designator may not. */
3666 if (c_parser_next_token_is (parser, CPP_NAME)
3667 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3669 /* Old-style structure member designator. */
3670 set_init_label (c_parser_peek_token (parser)->value,
3671 braced_init_obstack);
3672 /* Use the colon as the error location. */
3673 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3674 "obsolete use of designated initializer with %<:%>");
3675 c_parser_consume_token (parser);
3676 c_parser_consume_token (parser);
3678 else
3680 /* des_seen is 0 if there have been no designators, 1 if there
3681 has been a single array designator and 2 otherwise. */
3682 int des_seen = 0;
3683 /* Location of a designator. */
3684 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3685 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3686 || c_parser_next_token_is (parser, CPP_DOT))
3688 int des_prev = des_seen;
3689 if (!des_seen)
3690 des_loc = c_parser_peek_token (parser)->location;
3691 if (des_seen < 2)
3692 des_seen++;
3693 if (c_parser_next_token_is (parser, CPP_DOT))
3695 des_seen = 2;
3696 c_parser_consume_token (parser);
3697 if (c_parser_next_token_is (parser, CPP_NAME))
3699 set_init_label (c_parser_peek_token (parser)->value,
3700 braced_init_obstack);
3701 c_parser_consume_token (parser);
3703 else
3705 struct c_expr init;
3706 init.value = error_mark_node;
3707 init.original_code = ERROR_MARK;
3708 init.original_type = NULL;
3709 c_parser_error (parser, "expected identifier");
3710 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3711 process_init_element (init, false, braced_init_obstack);
3712 return;
3715 else
3717 tree first, second;
3718 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3719 /* ??? Following the old parser, [ objc-receiver
3720 objc-message-args ] is accepted as an initializer,
3721 being distinguished from a designator by what follows
3722 the first assignment expression inside the square
3723 brackets, but after a first array designator a
3724 subsequent square bracket is for Objective-C taken to
3725 start an expression, using the obsolete form of
3726 designated initializer without '=', rather than
3727 possibly being a second level of designation: in LALR
3728 terms, the '[' is shifted rather than reducing
3729 designator to designator-list. */
3730 if (des_prev == 1 && c_dialect_objc ())
3732 des_seen = des_prev;
3733 break;
3735 if (des_prev == 0 && c_dialect_objc ())
3737 /* This might be an array designator or an
3738 Objective-C message expression. If the former,
3739 continue parsing here; if the latter, parse the
3740 remainder of the initializer given the starting
3741 primary-expression. ??? It might make sense to
3742 distinguish when des_prev == 1 as well; see
3743 previous comment. */
3744 tree rec, args;
3745 struct c_expr mexpr;
3746 c_parser_consume_token (parser);
3747 if (c_parser_peek_token (parser)->type == CPP_NAME
3748 && ((c_parser_peek_token (parser)->id_kind
3749 == C_ID_TYPENAME)
3750 || (c_parser_peek_token (parser)->id_kind
3751 == C_ID_CLASSNAME)))
3753 /* Type name receiver. */
3754 tree id = c_parser_peek_token (parser)->value;
3755 c_parser_consume_token (parser);
3756 rec = objc_get_class_reference (id);
3757 goto parse_message_args;
3759 first = c_parser_expr_no_commas (parser, NULL).value;
3760 mark_exp_read (first);
3761 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3762 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3763 goto array_desig_after_first;
3764 /* Expression receiver. So far only one part
3765 without commas has been parsed; there might be
3766 more of the expression. */
3767 rec = first;
3768 while (c_parser_next_token_is (parser, CPP_COMMA))
3770 struct c_expr next;
3771 location_t comma_loc, exp_loc;
3772 comma_loc = c_parser_peek_token (parser)->location;
3773 c_parser_consume_token (parser);
3774 exp_loc = c_parser_peek_token (parser)->location;
3775 next = c_parser_expr_no_commas (parser, NULL);
3776 next = default_function_array_read_conversion (exp_loc,
3777 next);
3778 rec = build_compound_expr (comma_loc, rec, next.value);
3780 parse_message_args:
3781 /* Now parse the objc-message-args. */
3782 args = c_parser_objc_message_args (parser);
3783 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3784 "expected %<]%>");
3785 mexpr.value
3786 = objc_build_message_expr (build_tree_list (rec, args));
3787 mexpr.original_code = ERROR_MARK;
3788 mexpr.original_type = NULL;
3789 /* Now parse and process the remainder of the
3790 initializer, starting with this message
3791 expression as a primary-expression. */
3792 c_parser_initval (parser, &mexpr, braced_init_obstack);
3793 return;
3795 c_parser_consume_token (parser);
3796 first = c_parser_expr_no_commas (parser, NULL).value;
3797 mark_exp_read (first);
3798 array_desig_after_first:
3799 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3801 ellipsis_loc = c_parser_peek_token (parser)->location;
3802 c_parser_consume_token (parser);
3803 second = c_parser_expr_no_commas (parser, NULL).value;
3804 mark_exp_read (second);
3806 else
3807 second = NULL_TREE;
3808 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3810 c_parser_consume_token (parser);
3811 set_init_index (first, second, braced_init_obstack);
3812 if (second)
3813 pedwarn (ellipsis_loc, OPT_pedantic,
3814 "ISO C forbids specifying range of elements to initialize");
3816 else
3817 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3818 "expected %<]%>");
3821 if (des_seen >= 1)
3823 if (c_parser_next_token_is (parser, CPP_EQ))
3825 if (!flag_isoc99)
3826 pedwarn (des_loc, OPT_pedantic,
3827 "ISO C90 forbids specifying subobject to initialize");
3828 c_parser_consume_token (parser);
3830 else
3832 if (des_seen == 1)
3833 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3834 "obsolete use of designated initializer without %<=%>");
3835 else
3837 struct c_expr init;
3838 init.value = error_mark_node;
3839 init.original_code = ERROR_MARK;
3840 init.original_type = NULL;
3841 c_parser_error (parser, "expected %<=%>");
3842 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3843 process_init_element (init, false, braced_init_obstack);
3844 return;
3849 c_parser_initval (parser, NULL, braced_init_obstack);
3852 /* Parse a nested initializer; as c_parser_initializer but parses
3853 initializers within braced lists, after any designators have been
3854 applied. If AFTER is not NULL then it is an Objective-C message
3855 expression which is the primary-expression starting the
3856 initializer. */
3858 static void
3859 c_parser_initval (c_parser *parser, struct c_expr *after,
3860 struct obstack * braced_init_obstack)
3862 struct c_expr init;
3863 gcc_assert (!after || c_dialect_objc ());
3864 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3865 init = c_parser_braced_init (parser, NULL_TREE, true);
3866 else
3868 location_t loc = c_parser_peek_token (parser)->location;
3869 init = c_parser_expr_no_commas (parser, after);
3870 if (init.value != NULL_TREE
3871 && TREE_CODE (init.value) != STRING_CST
3872 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3873 init = default_function_array_read_conversion (loc, init);
3875 process_init_element (init, false, braced_init_obstack);
3878 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3879 C99 6.8.2).
3881 compound-statement:
3882 { block-item-list[opt] }
3883 { label-declarations block-item-list }
3885 block-item-list:
3886 block-item
3887 block-item-list block-item
3889 block-item:
3890 nested-declaration
3891 statement
3893 nested-declaration:
3894 declaration
3896 GNU extensions:
3898 compound-statement:
3899 { label-declarations block-item-list }
3901 nested-declaration:
3902 __extension__ nested-declaration
3903 nested-function-definition
3905 label-declarations:
3906 label-declaration
3907 label-declarations label-declaration
3909 label-declaration:
3910 __label__ identifier-list ;
3912 Allowing the mixing of declarations and code is new in C99. The
3913 GNU syntax also permits (not shown above) labels at the end of
3914 compound statements, which yield an error. We don't allow labels
3915 on declarations; this might seem like a natural extension, but
3916 there would be a conflict between attributes on the label and
3917 prefix attributes on the declaration. ??? The syntax follows the
3918 old parser in requiring something after label declarations.
3919 Although they are erroneous if the labels declared aren't defined,
3920 is it useful for the syntax to be this way?
3922 OpenMP:
3924 block-item:
3925 openmp-directive
3927 openmp-directive:
3928 barrier-directive
3929 flush-directive */
3931 static tree
3932 c_parser_compound_statement (c_parser *parser)
3934 tree stmt;
3935 location_t brace_loc;
3936 brace_loc = c_parser_peek_token (parser)->location;
3937 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3939 /* Ensure a scope is entered and left anyway to avoid confusion
3940 if we have just prepared to enter a function body. */
3941 stmt = c_begin_compound_stmt (true);
3942 c_end_compound_stmt (brace_loc, stmt, true);
3943 return error_mark_node;
3945 stmt = c_begin_compound_stmt (true);
3946 c_parser_compound_statement_nostart (parser);
3947 return c_end_compound_stmt (brace_loc, stmt, true);
3950 /* Parse a compound statement except for the opening brace. This is
3951 used for parsing both compound statements and statement expressions
3952 (which follow different paths to handling the opening). */
3954 static void
3955 c_parser_compound_statement_nostart (c_parser *parser)
3957 bool last_stmt = false;
3958 bool last_label = false;
3959 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3960 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3961 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3963 c_parser_consume_token (parser);
3964 return;
3966 mark_valid_location_for_stdc_pragma (true);
3967 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3969 /* Read zero or more forward-declarations for labels that nested
3970 functions can jump to. */
3971 mark_valid_location_for_stdc_pragma (false);
3972 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3974 label_loc = c_parser_peek_token (parser)->location;
3975 c_parser_consume_token (parser);
3976 /* Any identifiers, including those declared as type names,
3977 are OK here. */
3978 while (true)
3980 tree label;
3981 if (c_parser_next_token_is_not (parser, CPP_NAME))
3983 c_parser_error (parser, "expected identifier");
3984 break;
3986 label
3987 = declare_label (c_parser_peek_token (parser)->value);
3988 C_DECLARED_LABEL_FLAG (label) = 1;
3989 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3990 c_parser_consume_token (parser);
3991 if (c_parser_next_token_is (parser, CPP_COMMA))
3992 c_parser_consume_token (parser);
3993 else
3994 break;
3996 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3998 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
4000 /* We must now have at least one statement, label or declaration. */
4001 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4003 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4004 c_parser_error (parser, "expected declaration or statement");
4005 c_parser_consume_token (parser);
4006 return;
4008 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4010 location_t loc = c_parser_peek_token (parser)->location;
4011 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4012 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4013 || (c_parser_next_token_is (parser, CPP_NAME)
4014 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4016 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4017 label_loc = c_parser_peek_2nd_token (parser)->location;
4018 else
4019 label_loc = c_parser_peek_token (parser)->location;
4020 last_label = true;
4021 last_stmt = false;
4022 mark_valid_location_for_stdc_pragma (false);
4023 c_parser_label (parser);
4025 else if (!last_label
4026 && c_parser_next_tokens_start_declaration (parser))
4028 last_label = false;
4029 mark_valid_location_for_stdc_pragma (false);
4030 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4031 if (last_stmt)
4032 pedwarn_c90 (loc,
4033 (pedantic && !flag_isoc99)
4034 ? OPT_pedantic
4035 : OPT_Wdeclaration_after_statement,
4036 "ISO C90 forbids mixed declarations and code");
4037 last_stmt = false;
4039 else if (!last_label
4040 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4042 /* __extension__ can start a declaration, but is also an
4043 unary operator that can start an expression. Consume all
4044 but the last of a possible series of __extension__ to
4045 determine which. */
4046 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4047 && (c_parser_peek_2nd_token (parser)->keyword
4048 == RID_EXTENSION))
4049 c_parser_consume_token (parser);
4050 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4052 int ext;
4053 ext = disable_extension_diagnostics ();
4054 c_parser_consume_token (parser);
4055 last_label = false;
4056 mark_valid_location_for_stdc_pragma (false);
4057 c_parser_declaration_or_fndef (parser, true, true, true, true,
4058 true, NULL);
4059 /* Following the old parser, __extension__ does not
4060 disable this diagnostic. */
4061 restore_extension_diagnostics (ext);
4062 if (last_stmt)
4063 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4064 ? OPT_pedantic
4065 : OPT_Wdeclaration_after_statement,
4066 "ISO C90 forbids mixed declarations and code");
4067 last_stmt = false;
4069 else
4070 goto statement;
4072 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4074 /* External pragmas, and some omp pragmas, are not associated
4075 with regular c code, and so are not to be considered statements
4076 syntactically. This ensures that the user doesn't put them
4077 places that would turn into syntax errors if the directive
4078 were ignored. */
4079 if (c_parser_pragma (parser, pragma_compound))
4080 last_label = false, last_stmt = true;
4082 else if (c_parser_next_token_is (parser, CPP_EOF))
4084 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4085 c_parser_error (parser, "expected declaration or statement");
4086 return;
4088 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4090 if (parser->in_if_block)
4092 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4093 error_at (loc, """expected %<}%> before %<else%>");
4094 return;
4096 else
4098 error_at (loc, "%<else%> without a previous %<if%>");
4099 c_parser_consume_token (parser);
4100 continue;
4103 else
4105 statement:
4106 last_label = false;
4107 last_stmt = true;
4108 mark_valid_location_for_stdc_pragma (false);
4109 c_parser_statement_after_labels (parser);
4112 parser->error = false;
4114 if (last_label)
4115 error_at (label_loc, "label at end of compound statement");
4116 c_parser_consume_token (parser);
4117 /* Restore the value we started with. */
4118 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4121 /* Parse a label (C90 6.6.1, C99 6.8.1).
4123 label:
4124 identifier : attributes[opt]
4125 case constant-expression :
4126 default :
4128 GNU extensions:
4130 label:
4131 case constant-expression ... constant-expression :
4133 The use of attributes on labels is a GNU extension. The syntax in
4134 GNU C accepts any expressions without commas, non-constant
4135 expressions being rejected later. */
4137 static void
4138 c_parser_label (c_parser *parser)
4140 location_t loc1 = c_parser_peek_token (parser)->location;
4141 tree label = NULL_TREE;
4142 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4144 tree exp1, exp2;
4145 c_parser_consume_token (parser);
4146 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4147 if (c_parser_next_token_is (parser, CPP_COLON))
4149 c_parser_consume_token (parser);
4150 label = do_case (loc1, exp1, NULL_TREE);
4152 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4154 c_parser_consume_token (parser);
4155 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4156 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4157 label = do_case (loc1, exp1, exp2);
4159 else
4160 c_parser_error (parser, "expected %<:%> or %<...%>");
4162 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4164 c_parser_consume_token (parser);
4165 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4166 label = do_case (loc1, NULL_TREE, NULL_TREE);
4168 else
4170 tree name = c_parser_peek_token (parser)->value;
4171 tree tlab;
4172 tree attrs;
4173 location_t loc2 = c_parser_peek_token (parser)->location;
4174 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4175 c_parser_consume_token (parser);
4176 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4177 c_parser_consume_token (parser);
4178 attrs = c_parser_attributes (parser);
4179 tlab = define_label (loc2, name);
4180 if (tlab)
4182 decl_attributes (&tlab, attrs, 0);
4183 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4186 if (label)
4188 if (c_parser_next_tokens_start_declaration (parser))
4190 error_at (c_parser_peek_token (parser)->location,
4191 "a label can only be part of a statement and "
4192 "a declaration is not a statement");
4193 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4194 /*static_assert_ok*/ true,
4195 /*nested*/ true, /*empty_ok*/ false,
4196 /*start_attr_ok*/ true, NULL);
4201 /* Parse a statement (C90 6.6, C99 6.8).
4203 statement:
4204 labeled-statement
4205 compound-statement
4206 expression-statement
4207 selection-statement
4208 iteration-statement
4209 jump-statement
4211 labeled-statement:
4212 label statement
4214 expression-statement:
4215 expression[opt] ;
4217 selection-statement:
4218 if-statement
4219 switch-statement
4221 iteration-statement:
4222 while-statement
4223 do-statement
4224 for-statement
4226 jump-statement:
4227 goto identifier ;
4228 continue ;
4229 break ;
4230 return expression[opt] ;
4232 GNU extensions:
4234 statement:
4235 asm-statement
4237 jump-statement:
4238 goto * expression ;
4240 Objective-C:
4242 statement:
4243 objc-throw-statement
4244 objc-try-catch-statement
4245 objc-synchronized-statement
4247 objc-throw-statement:
4248 @throw expression ;
4249 @throw ;
4251 OpenMP:
4253 statement:
4254 openmp-construct
4256 openmp-construct:
4257 parallel-construct
4258 for-construct
4259 sections-construct
4260 single-construct
4261 parallel-for-construct
4262 parallel-sections-construct
4263 master-construct
4264 critical-construct
4265 atomic-construct
4266 ordered-construct
4268 parallel-construct:
4269 parallel-directive structured-block
4271 for-construct:
4272 for-directive iteration-statement
4274 sections-construct:
4275 sections-directive section-scope
4277 single-construct:
4278 single-directive structured-block
4280 parallel-for-construct:
4281 parallel-for-directive iteration-statement
4283 parallel-sections-construct:
4284 parallel-sections-directive section-scope
4286 master-construct:
4287 master-directive structured-block
4289 critical-construct:
4290 critical-directive structured-block
4292 atomic-construct:
4293 atomic-directive expression-statement
4295 ordered-construct:
4296 ordered-directive structured-block */
4298 static void
4299 c_parser_statement (c_parser *parser)
4301 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4302 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4303 || (c_parser_next_token_is (parser, CPP_NAME)
4304 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4305 c_parser_label (parser);
4306 c_parser_statement_after_labels (parser);
4309 /* Parse a statement, other than a labeled statement. */
4311 static void
4312 c_parser_statement_after_labels (c_parser *parser)
4314 location_t loc = c_parser_peek_token (parser)->location;
4315 tree stmt = NULL_TREE;
4316 bool in_if_block = parser->in_if_block;
4317 parser->in_if_block = false;
4318 switch (c_parser_peek_token (parser)->type)
4320 case CPP_OPEN_BRACE:
4321 add_stmt (c_parser_compound_statement (parser));
4322 break;
4323 case CPP_KEYWORD:
4324 switch (c_parser_peek_token (parser)->keyword)
4326 case RID_IF:
4327 c_parser_if_statement (parser);
4328 break;
4329 case RID_SWITCH:
4330 c_parser_switch_statement (parser);
4331 break;
4332 case RID_WHILE:
4333 c_parser_while_statement (parser);
4334 break;
4335 case RID_DO:
4336 c_parser_do_statement (parser);
4337 break;
4338 case RID_FOR:
4339 c_parser_for_statement (parser);
4340 break;
4341 case RID_GOTO:
4342 c_parser_consume_token (parser);
4343 if (c_parser_next_token_is (parser, CPP_NAME))
4345 stmt = c_finish_goto_label (loc,
4346 c_parser_peek_token (parser)->value);
4347 c_parser_consume_token (parser);
4349 else if (c_parser_next_token_is (parser, CPP_MULT))
4351 tree val;
4353 c_parser_consume_token (parser);
4354 val = c_parser_expression (parser).value;
4355 mark_exp_read (val);
4356 stmt = c_finish_goto_ptr (loc, val);
4358 else
4359 c_parser_error (parser, "expected identifier or %<*%>");
4360 goto expect_semicolon;
4361 case RID_CONTINUE:
4362 c_parser_consume_token (parser);
4363 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4364 goto expect_semicolon;
4365 case RID_BREAK:
4366 c_parser_consume_token (parser);
4367 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4368 goto expect_semicolon;
4369 case RID_RETURN:
4370 c_parser_consume_token (parser);
4371 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4373 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4374 c_parser_consume_token (parser);
4376 else
4378 struct c_expr expr = c_parser_expression_conv (parser);
4379 mark_exp_read (expr.value);
4380 stmt = c_finish_return (loc, expr.value, expr.original_type);
4381 goto expect_semicolon;
4383 break;
4384 case RID_ASM:
4385 stmt = c_parser_asm_statement (parser);
4386 break;
4387 case RID_AT_THROW:
4388 gcc_assert (c_dialect_objc ());
4389 c_parser_consume_token (parser);
4390 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4392 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4393 c_parser_consume_token (parser);
4395 else
4397 tree expr = c_parser_expression (parser).value;
4398 expr = c_fully_fold (expr, false, NULL);
4399 stmt = objc_build_throw_stmt (loc, expr);
4400 goto expect_semicolon;
4402 break;
4403 case RID_AT_TRY:
4404 gcc_assert (c_dialect_objc ());
4405 c_parser_objc_try_catch_finally_statement (parser);
4406 break;
4407 case RID_AT_SYNCHRONIZED:
4408 gcc_assert (c_dialect_objc ());
4409 c_parser_objc_synchronized_statement (parser);
4410 break;
4411 default:
4412 goto expr_stmt;
4414 break;
4415 case CPP_SEMICOLON:
4416 c_parser_consume_token (parser);
4417 break;
4418 case CPP_CLOSE_PAREN:
4419 case CPP_CLOSE_SQUARE:
4420 /* Avoid infinite loop in error recovery:
4421 c_parser_skip_until_found stops at a closing nesting
4422 delimiter without consuming it, but here we need to consume
4423 it to proceed further. */
4424 c_parser_error (parser, "expected statement");
4425 c_parser_consume_token (parser);
4426 break;
4427 case CPP_PRAGMA:
4428 c_parser_pragma (parser, pragma_stmt);
4429 break;
4430 default:
4431 expr_stmt:
4432 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4433 expect_semicolon:
4434 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4435 break;
4437 /* Two cases cannot and do not have line numbers associated: If stmt
4438 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4439 cannot hold line numbers. But that's OK because the statement
4440 will either be changed to a MODIFY_EXPR during gimplification of
4441 the statement expr, or discarded. If stmt was compound, but
4442 without new variables, we will have skipped the creation of a
4443 BIND and will have a bare STATEMENT_LIST. But that's OK because
4444 (recursively) all of the component statements should already have
4445 line numbers assigned. ??? Can we discard no-op statements
4446 earlier? */
4447 if (CAN_HAVE_LOCATION_P (stmt)
4448 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4449 SET_EXPR_LOCATION (stmt, loc);
4451 parser->in_if_block = in_if_block;
4454 /* Parse the condition from an if, do, while or for statements. */
4456 static tree
4457 c_parser_condition (c_parser *parser)
4459 location_t loc = c_parser_peek_token (parser)->location;
4460 tree cond;
4461 cond = c_parser_expression_conv (parser).value;
4462 cond = c_objc_common_truthvalue_conversion (loc, cond);
4463 cond = c_fully_fold (cond, false, NULL);
4464 if (warn_sequence_point)
4465 verify_sequence_points (cond);
4466 return cond;
4469 /* Parse a parenthesized condition from an if, do or while statement.
4471 condition:
4472 ( expression )
4474 static tree
4475 c_parser_paren_condition (c_parser *parser)
4477 tree cond;
4478 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4479 return error_mark_node;
4480 cond = c_parser_condition (parser);
4481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4482 return cond;
4485 /* Parse a statement which is a block in C99. */
4487 static tree
4488 c_parser_c99_block_statement (c_parser *parser)
4490 tree block = c_begin_compound_stmt (flag_isoc99);
4491 location_t loc = c_parser_peek_token (parser)->location;
4492 c_parser_statement (parser);
4493 return c_end_compound_stmt (loc, block, flag_isoc99);
4496 /* Parse the body of an if statement. This is just parsing a
4497 statement but (a) it is a block in C99, (b) we track whether the
4498 body is an if statement for the sake of -Wparentheses warnings, (c)
4499 we handle an empty body specially for the sake of -Wempty-body
4500 warnings, and (d) we call parser_compound_statement directly
4501 because c_parser_statement_after_labels resets
4502 parser->in_if_block. */
4504 static tree
4505 c_parser_if_body (c_parser *parser, bool *if_p)
4507 tree block = c_begin_compound_stmt (flag_isoc99);
4508 location_t body_loc = c_parser_peek_token (parser)->location;
4509 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4510 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4511 || (c_parser_next_token_is (parser, CPP_NAME)
4512 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4513 c_parser_label (parser);
4514 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4515 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4517 location_t loc = c_parser_peek_token (parser)->location;
4518 add_stmt (build_empty_stmt (loc));
4519 c_parser_consume_token (parser);
4520 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4521 warning_at (loc, OPT_Wempty_body,
4522 "suggest braces around empty body in an %<if%> statement");
4524 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4525 add_stmt (c_parser_compound_statement (parser));
4526 else
4527 c_parser_statement_after_labels (parser);
4528 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4531 /* Parse the else body of an if statement. This is just parsing a
4532 statement but (a) it is a block in C99, (b) we handle an empty body
4533 specially for the sake of -Wempty-body warnings. */
4535 static tree
4536 c_parser_else_body (c_parser *parser)
4538 location_t else_loc = c_parser_peek_token (parser)->location;
4539 tree block = c_begin_compound_stmt (flag_isoc99);
4540 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4541 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4542 || (c_parser_next_token_is (parser, CPP_NAME)
4543 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4544 c_parser_label (parser);
4545 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4547 location_t loc = c_parser_peek_token (parser)->location;
4548 warning_at (loc,
4549 OPT_Wempty_body,
4550 "suggest braces around empty body in an %<else%> statement");
4551 add_stmt (build_empty_stmt (loc));
4552 c_parser_consume_token (parser);
4554 else
4555 c_parser_statement_after_labels (parser);
4556 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4559 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4561 if-statement:
4562 if ( expression ) statement
4563 if ( expression ) statement else statement
4566 static void
4567 c_parser_if_statement (c_parser *parser)
4569 tree block;
4570 location_t loc;
4571 tree cond;
4572 bool first_if = false;
4573 tree first_body, second_body;
4574 bool in_if_block;
4576 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4577 c_parser_consume_token (parser);
4578 block = c_begin_compound_stmt (flag_isoc99);
4579 loc = c_parser_peek_token (parser)->location;
4580 cond = c_parser_paren_condition (parser);
4581 in_if_block = parser->in_if_block;
4582 parser->in_if_block = true;
4583 first_body = c_parser_if_body (parser, &first_if);
4584 parser->in_if_block = in_if_block;
4585 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4587 c_parser_consume_token (parser);
4588 second_body = c_parser_else_body (parser);
4590 else
4591 second_body = NULL_TREE;
4592 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4593 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4596 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4598 switch-statement:
4599 switch (expression) statement
4602 static void
4603 c_parser_switch_statement (c_parser *parser)
4605 tree block, expr, body, save_break;
4606 location_t switch_loc = c_parser_peek_token (parser)->location;
4607 location_t switch_cond_loc;
4608 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4609 c_parser_consume_token (parser);
4610 block = c_begin_compound_stmt (flag_isoc99);
4611 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4613 switch_cond_loc = c_parser_peek_token (parser)->location;
4614 expr = c_parser_expression (parser).value;
4615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4617 else
4619 switch_cond_loc = UNKNOWN_LOCATION;
4620 expr = error_mark_node;
4622 c_start_case (switch_loc, switch_cond_loc, expr);
4623 save_break = c_break_label;
4624 c_break_label = NULL_TREE;
4625 body = c_parser_c99_block_statement (parser);
4626 c_finish_case (body);
4627 if (c_break_label)
4629 location_t here = c_parser_peek_token (parser)->location;
4630 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4631 SET_EXPR_LOCATION (t, here);
4632 add_stmt (t);
4634 c_break_label = save_break;
4635 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4638 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4640 while-statement:
4641 while (expression) statement
4644 static void
4645 c_parser_while_statement (c_parser *parser)
4647 tree block, cond, body, save_break, save_cont;
4648 location_t loc;
4649 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4650 c_parser_consume_token (parser);
4651 block = c_begin_compound_stmt (flag_isoc99);
4652 loc = c_parser_peek_token (parser)->location;
4653 cond = c_parser_paren_condition (parser);
4654 save_break = c_break_label;
4655 c_break_label = NULL_TREE;
4656 save_cont = c_cont_label;
4657 c_cont_label = NULL_TREE;
4658 body = c_parser_c99_block_statement (parser);
4659 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4660 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4661 c_break_label = save_break;
4662 c_cont_label = save_cont;
4665 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4667 do-statement:
4668 do statement while ( expression ) ;
4671 static void
4672 c_parser_do_statement (c_parser *parser)
4674 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4675 location_t loc;
4676 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4677 c_parser_consume_token (parser);
4678 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4679 warning_at (c_parser_peek_token (parser)->location,
4680 OPT_Wempty_body,
4681 "suggest braces around empty body in %<do%> statement");
4682 block = c_begin_compound_stmt (flag_isoc99);
4683 loc = c_parser_peek_token (parser)->location;
4684 save_break = c_break_label;
4685 c_break_label = NULL_TREE;
4686 save_cont = c_cont_label;
4687 c_cont_label = NULL_TREE;
4688 body = c_parser_c99_block_statement (parser);
4689 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4690 new_break = c_break_label;
4691 c_break_label = save_break;
4692 new_cont = c_cont_label;
4693 c_cont_label = save_cont;
4694 cond = c_parser_paren_condition (parser);
4695 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4696 c_parser_skip_to_end_of_block_or_statement (parser);
4697 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4698 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4701 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4703 for-statement:
4704 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4705 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4707 The form with a declaration is new in C99.
4709 ??? In accordance with the old parser, the declaration may be a
4710 nested function, which is then rejected in check_for_loop_decls,
4711 but does it make any sense for this to be included in the grammar?
4712 Note in particular that the nested function does not include a
4713 trailing ';', whereas the "declaration" production includes one.
4714 Also, can we reject bad declarations earlier and cheaper than
4715 check_for_loop_decls?
4717 In Objective-C, there are two additional variants:
4719 foreach-statement:
4720 for ( expression in expresssion ) statement
4721 for ( declaration in expression ) statement
4723 This is inconsistent with C, because the second variant is allowed
4724 even if c99 is not enabled.
4726 The rest of the comment documents these Objective-C foreach-statement.
4728 Here is the canonical example of the first variant:
4729 for (object in array) { do something with object }
4730 we call the first expression ("object") the "object_expression" and
4731 the second expression ("array") the "collection_expression".
4732 object_expression must be an lvalue of type "id" (a generic Objective-C
4733 object) because the loop works by assigning to object_expression the
4734 various objects from the collection_expression. collection_expression
4735 must evaluate to something of type "id" which responds to the method
4736 countByEnumeratingWithState:objects:count:.
4738 The canonical example of the second variant is:
4739 for (id object in array) { do something with object }
4740 which is completely equivalent to
4742 id object;
4743 for (object in array) { do something with object }
4745 Note that initizializing 'object' in some way (eg, "for ((object =
4746 xxx) in array) { do something with object }") is possibly
4747 technically valid, but completely pointless as 'object' will be
4748 assigned to something else as soon as the loop starts. We should
4749 most likely reject it (TODO).
4751 The beginning of the Objective-C foreach-statement looks exactly
4752 like the beginning of the for-statement, and we can tell it is a
4753 foreach-statement only because the initial declaration or
4754 expression is terminated by 'in' instead of ';'.
4757 static void
4758 c_parser_for_statement (c_parser *parser)
4760 tree block, cond, incr, save_break, save_cont, body;
4761 /* The following are only used when parsing an ObjC foreach statement. */
4762 tree object_expression;
4763 /* Silence the bogus uninitialized warning. */
4764 tree collection_expression = NULL;
4765 location_t loc = c_parser_peek_token (parser)->location;
4766 location_t for_loc = c_parser_peek_token (parser)->location;
4767 bool is_foreach_statement = false;
4768 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4769 c_parser_consume_token (parser);
4770 /* Open a compound statement in Objective-C as well, just in case this is
4771 as foreach expression. */
4772 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4773 cond = error_mark_node;
4774 incr = error_mark_node;
4775 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4777 /* Parse the initialization declaration or expression. */
4778 object_expression = error_mark_node;
4779 parser->objc_could_be_foreach_context = c_dialect_objc ();
4780 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4782 parser->objc_could_be_foreach_context = false;
4783 c_parser_consume_token (parser);
4784 c_finish_expr_stmt (loc, NULL_TREE);
4786 else if (c_parser_next_tokens_start_declaration (parser))
4788 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4789 &object_expression);
4790 parser->objc_could_be_foreach_context = false;
4792 if (c_parser_next_token_is_keyword (parser, RID_IN))
4794 c_parser_consume_token (parser);
4795 is_foreach_statement = true;
4796 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4797 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4799 else
4800 check_for_loop_decls (for_loc, flag_isoc99);
4802 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4804 /* __extension__ can start a declaration, but is also an
4805 unary operator that can start an expression. Consume all
4806 but the last of a possible series of __extension__ to
4807 determine which. */
4808 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4809 && (c_parser_peek_2nd_token (parser)->keyword
4810 == RID_EXTENSION))
4811 c_parser_consume_token (parser);
4812 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4814 int ext;
4815 ext = disable_extension_diagnostics ();
4816 c_parser_consume_token (parser);
4817 c_parser_declaration_or_fndef (parser, true, true, true, true,
4818 true, &object_expression);
4819 parser->objc_could_be_foreach_context = false;
4821 restore_extension_diagnostics (ext);
4822 if (c_parser_next_token_is_keyword (parser, RID_IN))
4824 c_parser_consume_token (parser);
4825 is_foreach_statement = true;
4826 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4827 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4829 else
4830 check_for_loop_decls (for_loc, flag_isoc99);
4832 else
4833 goto init_expr;
4835 else
4837 init_expr:
4839 tree init_expression;
4840 init_expression = c_parser_expression (parser).value;
4841 parser->objc_could_be_foreach_context = false;
4842 if (c_parser_next_token_is_keyword (parser, RID_IN))
4844 c_parser_consume_token (parser);
4845 is_foreach_statement = true;
4846 if (! lvalue_p (init_expression))
4847 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4848 object_expression = c_fully_fold (init_expression, false, NULL);
4850 else
4852 c_finish_expr_stmt (loc, init_expression);
4853 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4857 /* Parse the loop condition. In the case of a foreach
4858 statement, there is no loop condition. */
4859 gcc_assert (!parser->objc_could_be_foreach_context);
4860 if (!is_foreach_statement)
4862 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4864 c_parser_consume_token (parser);
4865 cond = NULL_TREE;
4867 else
4869 cond = c_parser_condition (parser);
4870 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4873 /* Parse the increment expression (the third expression in a
4874 for-statement). In the case of a foreach-statement, this is
4875 the expression that follows the 'in'. */
4876 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4878 if (is_foreach_statement)
4880 c_parser_error (parser, "missing collection in fast enumeration");
4881 collection_expression = error_mark_node;
4883 else
4884 incr = c_process_expr_stmt (loc, NULL_TREE);
4886 else
4888 if (is_foreach_statement)
4889 collection_expression = c_fully_fold (c_parser_expression (parser).value,
4890 false, NULL);
4891 else
4892 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4894 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4896 save_break = c_break_label;
4897 c_break_label = NULL_TREE;
4898 save_cont = c_cont_label;
4899 c_cont_label = NULL_TREE;
4900 body = c_parser_c99_block_statement (parser);
4901 if (is_foreach_statement)
4902 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4903 else
4904 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4905 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4906 c_break_label = save_break;
4907 c_cont_label = save_cont;
4910 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4911 statement with inputs, outputs, clobbers, and volatile tag
4912 allowed.
4914 asm-statement:
4915 asm type-qualifier[opt] ( asm-argument ) ;
4916 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4918 asm-argument:
4919 asm-string-literal
4920 asm-string-literal : asm-operands[opt]
4921 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4922 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4924 asm-goto-argument:
4925 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4926 : asm-goto-operands
4928 Qualifiers other than volatile are accepted in the syntax but
4929 warned for. */
4931 static tree
4932 c_parser_asm_statement (c_parser *parser)
4934 tree quals, str, outputs, inputs, clobbers, labels, ret;
4935 bool simple, is_goto;
4936 location_t asm_loc = c_parser_peek_token (parser)->location;
4937 int section, nsections;
4939 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4940 c_parser_consume_token (parser);
4941 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4943 quals = c_parser_peek_token (parser)->value;
4944 c_parser_consume_token (parser);
4946 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4947 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4949 warning_at (c_parser_peek_token (parser)->location,
4951 "%E qualifier ignored on asm",
4952 c_parser_peek_token (parser)->value);
4953 quals = NULL_TREE;
4954 c_parser_consume_token (parser);
4956 else
4957 quals = NULL_TREE;
4959 is_goto = false;
4960 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4962 c_parser_consume_token (parser);
4963 is_goto = true;
4966 /* ??? Follow the C++ parser rather than using the
4967 lex_untranslated_string kludge. */
4968 parser->lex_untranslated_string = true;
4969 ret = NULL;
4971 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4972 goto error;
4974 str = c_parser_asm_string_literal (parser);
4975 if (str == NULL_TREE)
4976 goto error_close_paren;
4978 simple = true;
4979 outputs = NULL_TREE;
4980 inputs = NULL_TREE;
4981 clobbers = NULL_TREE;
4982 labels = NULL_TREE;
4984 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4985 goto done_asm;
4987 /* Parse each colon-delimited section of operands. */
4988 nsections = 3 + is_goto;
4989 for (section = 0; section < nsections; ++section)
4991 if (!c_parser_require (parser, CPP_COLON,
4992 is_goto
4993 ? "expected %<:%>"
4994 : "expected %<:%> or %<)%>"))
4995 goto error_close_paren;
4997 /* Once past any colon, we're no longer a simple asm. */
4998 simple = false;
5000 if ((!c_parser_next_token_is (parser, CPP_COLON)
5001 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5002 || section == 3)
5003 switch (section)
5005 case 0:
5006 /* For asm goto, we don't allow output operands, but reserve
5007 the slot for a future extension that does allow them. */
5008 if (!is_goto)
5009 outputs = c_parser_asm_operands (parser, false);
5010 break;
5011 case 1:
5012 inputs = c_parser_asm_operands (parser, true);
5013 break;
5014 case 2:
5015 clobbers = c_parser_asm_clobbers (parser);
5016 break;
5017 case 3:
5018 labels = c_parser_asm_goto_operands (parser);
5019 break;
5020 default:
5021 gcc_unreachable ();
5024 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5025 goto done_asm;
5028 done_asm:
5029 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5031 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5032 goto error;
5035 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5036 c_parser_skip_to_end_of_block_or_statement (parser);
5038 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5039 clobbers, labels, simple));
5041 error:
5042 parser->lex_untranslated_string = false;
5043 return ret;
5045 error_close_paren:
5046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5047 goto error;
5050 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5051 not outputs), apply the default conversion of functions and arrays
5052 to pointers.
5054 asm-operands:
5055 asm-operand
5056 asm-operands , asm-operand
5058 asm-operand:
5059 asm-string-literal ( expression )
5060 [ identifier ] asm-string-literal ( expression )
5063 static tree
5064 c_parser_asm_operands (c_parser *parser, bool convert_p)
5066 tree list = NULL_TREE;
5067 location_t loc;
5068 while (true)
5070 tree name, str;
5071 struct c_expr expr;
5072 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5074 c_parser_consume_token (parser);
5075 if (c_parser_next_token_is (parser, CPP_NAME))
5077 tree id = c_parser_peek_token (parser)->value;
5078 c_parser_consume_token (parser);
5079 name = build_string (IDENTIFIER_LENGTH (id),
5080 IDENTIFIER_POINTER (id));
5082 else
5084 c_parser_error (parser, "expected identifier");
5085 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5086 return NULL_TREE;
5088 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5089 "expected %<]%>");
5091 else
5092 name = NULL_TREE;
5093 str = c_parser_asm_string_literal (parser);
5094 if (str == NULL_TREE)
5095 return NULL_TREE;
5096 parser->lex_untranslated_string = false;
5097 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5099 parser->lex_untranslated_string = true;
5100 return NULL_TREE;
5102 loc = c_parser_peek_token (parser)->location;
5103 expr = c_parser_expression (parser);
5104 mark_exp_read (expr.value);
5105 if (convert_p)
5106 expr = default_function_array_conversion (loc, expr);
5107 expr.value = c_fully_fold (expr.value, false, NULL);
5108 parser->lex_untranslated_string = true;
5109 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5111 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5112 return NULL_TREE;
5114 list = chainon (list, build_tree_list (build_tree_list (name, str),
5115 expr.value));
5116 if (c_parser_next_token_is (parser, CPP_COMMA))
5117 c_parser_consume_token (parser);
5118 else
5119 break;
5121 return list;
5124 /* Parse asm clobbers, a GNU extension.
5126 asm-clobbers:
5127 asm-string-literal
5128 asm-clobbers , asm-string-literal
5131 static tree
5132 c_parser_asm_clobbers (c_parser *parser)
5134 tree list = NULL_TREE;
5135 while (true)
5137 tree str = c_parser_asm_string_literal (parser);
5138 if (str)
5139 list = tree_cons (NULL_TREE, str, list);
5140 else
5141 return NULL_TREE;
5142 if (c_parser_next_token_is (parser, CPP_COMMA))
5143 c_parser_consume_token (parser);
5144 else
5145 break;
5147 return list;
5150 /* Parse asm goto labels, a GNU extension.
5152 asm-goto-operands:
5153 identifier
5154 asm-goto-operands , identifier
5157 static tree
5158 c_parser_asm_goto_operands (c_parser *parser)
5160 tree list = NULL_TREE;
5161 while (true)
5163 tree name, label;
5165 if (c_parser_next_token_is (parser, CPP_NAME))
5167 c_token *tok = c_parser_peek_token (parser);
5168 name = tok->value;
5169 label = lookup_label_for_goto (tok->location, name);
5170 c_parser_consume_token (parser);
5171 TREE_USED (label) = 1;
5173 else
5175 c_parser_error (parser, "expected identifier");
5176 return NULL_TREE;
5179 name = build_string (IDENTIFIER_LENGTH (name),
5180 IDENTIFIER_POINTER (name));
5181 list = tree_cons (name, label, list);
5182 if (c_parser_next_token_is (parser, CPP_COMMA))
5183 c_parser_consume_token (parser);
5184 else
5185 return nreverse (list);
5189 /* Parse an expression other than a compound expression; that is, an
5190 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5191 NULL then it is an Objective-C message expression which is the
5192 primary-expression starting the expression as an initializer.
5194 assignment-expression:
5195 conditional-expression
5196 unary-expression assignment-operator assignment-expression
5198 assignment-operator: one of
5199 = *= /= %= += -= <<= >>= &= ^= |=
5201 In GNU C we accept any conditional expression on the LHS and
5202 diagnose the invalid lvalue rather than producing a syntax
5203 error. */
5205 static struct c_expr
5206 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5208 struct c_expr lhs, rhs, ret;
5209 enum tree_code code;
5210 location_t op_location, exp_location;
5211 gcc_assert (!after || c_dialect_objc ());
5212 lhs = c_parser_conditional_expression (parser, after);
5213 op_location = c_parser_peek_token (parser)->location;
5214 switch (c_parser_peek_token (parser)->type)
5216 case CPP_EQ:
5217 code = NOP_EXPR;
5218 break;
5219 case CPP_MULT_EQ:
5220 code = MULT_EXPR;
5221 break;
5222 case CPP_DIV_EQ:
5223 code = TRUNC_DIV_EXPR;
5224 break;
5225 case CPP_MOD_EQ:
5226 code = TRUNC_MOD_EXPR;
5227 break;
5228 case CPP_PLUS_EQ:
5229 code = PLUS_EXPR;
5230 break;
5231 case CPP_MINUS_EQ:
5232 code = MINUS_EXPR;
5233 break;
5234 case CPP_LSHIFT_EQ:
5235 code = LSHIFT_EXPR;
5236 break;
5237 case CPP_RSHIFT_EQ:
5238 code = RSHIFT_EXPR;
5239 break;
5240 case CPP_AND_EQ:
5241 code = BIT_AND_EXPR;
5242 break;
5243 case CPP_XOR_EQ:
5244 code = BIT_XOR_EXPR;
5245 break;
5246 case CPP_OR_EQ:
5247 code = BIT_IOR_EXPR;
5248 break;
5249 default:
5250 return lhs;
5252 c_parser_consume_token (parser);
5253 exp_location = c_parser_peek_token (parser)->location;
5254 rhs = c_parser_expr_no_commas (parser, NULL);
5255 rhs = default_function_array_read_conversion (exp_location, rhs);
5256 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5257 code, exp_location, rhs.value,
5258 rhs.original_type);
5259 if (code == NOP_EXPR)
5260 ret.original_code = MODIFY_EXPR;
5261 else
5263 TREE_NO_WARNING (ret.value) = 1;
5264 ret.original_code = ERROR_MARK;
5266 ret.original_type = NULL;
5267 return ret;
5270 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5271 is not NULL then it is an Objective-C message expression which is
5272 the primary-expression starting the expression as an initializer.
5274 conditional-expression:
5275 logical-OR-expression
5276 logical-OR-expression ? expression : conditional-expression
5278 GNU extensions:
5280 conditional-expression:
5281 logical-OR-expression ? : conditional-expression
5284 static struct c_expr
5285 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5287 struct c_expr cond, exp1, exp2, ret;
5288 location_t cond_loc, colon_loc, middle_loc;
5290 gcc_assert (!after || c_dialect_objc ());
5292 cond = c_parser_binary_expression (parser, after);
5294 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5295 return cond;
5296 cond_loc = c_parser_peek_token (parser)->location;
5297 cond = default_function_array_read_conversion (cond_loc, cond);
5298 c_parser_consume_token (parser);
5299 if (c_parser_next_token_is (parser, CPP_COLON))
5301 tree eptype = NULL_TREE;
5303 middle_loc = c_parser_peek_token (parser)->location;
5304 pedwarn (middle_loc, OPT_pedantic,
5305 "ISO C forbids omitting the middle term of a ?: expression");
5306 warn_for_omitted_condop (middle_loc, cond.value);
5307 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5309 eptype = TREE_TYPE (cond.value);
5310 cond.value = TREE_OPERAND (cond.value, 0);
5312 /* Make sure first operand is calculated only once. */
5313 exp1.value = c_save_expr (default_conversion (cond.value));
5314 if (eptype)
5315 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5316 exp1.original_type = NULL;
5317 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5318 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5320 else
5322 cond.value
5323 = c_objc_common_truthvalue_conversion
5324 (cond_loc, default_conversion (cond.value));
5325 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5326 exp1 = c_parser_expression_conv (parser);
5327 mark_exp_read (exp1.value);
5328 c_inhibit_evaluation_warnings +=
5329 ((cond.value == truthvalue_true_node)
5330 - (cond.value == truthvalue_false_node));
5333 colon_loc = c_parser_peek_token (parser)->location;
5334 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5336 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5337 ret.value = error_mark_node;
5338 ret.original_code = ERROR_MARK;
5339 ret.original_type = NULL;
5340 return ret;
5343 location_t exp2_loc = c_parser_peek_token (parser)->location;
5344 exp2 = c_parser_conditional_expression (parser, NULL);
5345 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5347 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5348 ret.value = build_conditional_expr (colon_loc, cond.value,
5349 cond.original_code == C_MAYBE_CONST_EXPR,
5350 exp1.value, exp1.original_type,
5351 exp2.value, exp2.original_type);
5352 ret.original_code = ERROR_MARK;
5353 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5354 ret.original_type = NULL;
5355 else
5357 tree t1, t2;
5359 /* If both sides are enum type, the default conversion will have
5360 made the type of the result be an integer type. We want to
5361 remember the enum types we started with. */
5362 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5363 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5364 ret.original_type = ((t1 != error_mark_node
5365 && t2 != error_mark_node
5366 && (TYPE_MAIN_VARIANT (t1)
5367 == TYPE_MAIN_VARIANT (t2)))
5368 ? t1
5369 : NULL);
5371 return ret;
5374 /* Parse a binary expression; that is, a logical-OR-expression (C90
5375 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5376 an Objective-C message expression which is the primary-expression
5377 starting the expression as an initializer.
5379 multiplicative-expression:
5380 cast-expression
5381 multiplicative-expression * cast-expression
5382 multiplicative-expression / cast-expression
5383 multiplicative-expression % cast-expression
5385 additive-expression:
5386 multiplicative-expression
5387 additive-expression + multiplicative-expression
5388 additive-expression - multiplicative-expression
5390 shift-expression:
5391 additive-expression
5392 shift-expression << additive-expression
5393 shift-expression >> additive-expression
5395 relational-expression:
5396 shift-expression
5397 relational-expression < shift-expression
5398 relational-expression > shift-expression
5399 relational-expression <= shift-expression
5400 relational-expression >= shift-expression
5402 equality-expression:
5403 relational-expression
5404 equality-expression == relational-expression
5405 equality-expression != relational-expression
5407 AND-expression:
5408 equality-expression
5409 AND-expression & equality-expression
5411 exclusive-OR-expression:
5412 AND-expression
5413 exclusive-OR-expression ^ AND-expression
5415 inclusive-OR-expression:
5416 exclusive-OR-expression
5417 inclusive-OR-expression | exclusive-OR-expression
5419 logical-AND-expression:
5420 inclusive-OR-expression
5421 logical-AND-expression && inclusive-OR-expression
5423 logical-OR-expression:
5424 logical-AND-expression
5425 logical-OR-expression || logical-AND-expression
5428 static struct c_expr
5429 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5431 /* A binary expression is parsed using operator-precedence parsing,
5432 with the operands being cast expressions. All the binary
5433 operators are left-associative. Thus a binary expression is of
5434 form:
5436 E0 op1 E1 op2 E2 ...
5438 which we represent on a stack. On the stack, the precedence
5439 levels are strictly increasing. When a new operator is
5440 encountered of higher precedence than that at the top of the
5441 stack, it is pushed; its LHS is the top expression, and its RHS
5442 is everything parsed until it is popped. When a new operator is
5443 encountered with precedence less than or equal to that at the top
5444 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5445 by the result of the operation until the operator at the top of
5446 the stack has lower precedence than the new operator or there is
5447 only one element on the stack; then the top expression is the LHS
5448 of the new operator. In the case of logical AND and OR
5449 expressions, we also need to adjust c_inhibit_evaluation_warnings
5450 as appropriate when the operators are pushed and popped. */
5452 /* The precedence levels, where 0 is a dummy lowest level used for
5453 the bottom of the stack. */
5454 enum prec {
5455 PREC_NONE,
5456 PREC_LOGOR,
5457 PREC_LOGAND,
5458 PREC_BITOR,
5459 PREC_BITXOR,
5460 PREC_BITAND,
5461 PREC_EQ,
5462 PREC_REL,
5463 PREC_SHIFT,
5464 PREC_ADD,
5465 PREC_MULT,
5466 NUM_PRECS
5468 struct {
5469 /* The expression at this stack level. */
5470 struct c_expr expr;
5471 /* The precedence of the operator on its left, PREC_NONE at the
5472 bottom of the stack. */
5473 enum prec prec;
5474 /* The operation on its left. */
5475 enum tree_code op;
5476 /* The source location of this operation. */
5477 location_t loc;
5478 } stack[NUM_PRECS];
5479 int sp;
5480 /* Location of the binary operator. */
5481 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5482 #define POP \
5483 do { \
5484 switch (stack[sp].op) \
5486 case TRUTH_ANDIF_EXPR: \
5487 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5488 == truthvalue_false_node); \
5489 break; \
5490 case TRUTH_ORIF_EXPR: \
5491 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5492 == truthvalue_true_node); \
5493 break; \
5494 default: \
5495 break; \
5497 stack[sp - 1].expr \
5498 = default_function_array_read_conversion (stack[sp - 1].loc, \
5499 stack[sp - 1].expr); \
5500 stack[sp].expr \
5501 = default_function_array_read_conversion (stack[sp].loc, \
5502 stack[sp].expr); \
5503 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5504 stack[sp].op, \
5505 stack[sp - 1].expr, \
5506 stack[sp].expr); \
5507 sp--; \
5508 } while (0)
5509 gcc_assert (!after || c_dialect_objc ());
5510 stack[0].loc = c_parser_peek_token (parser)->location;
5511 stack[0].expr = c_parser_cast_expression (parser, after);
5512 stack[0].prec = PREC_NONE;
5513 sp = 0;
5514 while (true)
5516 enum prec oprec;
5517 enum tree_code ocode;
5518 if (parser->error)
5519 goto out;
5520 switch (c_parser_peek_token (parser)->type)
5522 case CPP_MULT:
5523 oprec = PREC_MULT;
5524 ocode = MULT_EXPR;
5525 break;
5526 case CPP_DIV:
5527 oprec = PREC_MULT;
5528 ocode = TRUNC_DIV_EXPR;
5529 break;
5530 case CPP_MOD:
5531 oprec = PREC_MULT;
5532 ocode = TRUNC_MOD_EXPR;
5533 break;
5534 case CPP_PLUS:
5535 oprec = PREC_ADD;
5536 ocode = PLUS_EXPR;
5537 break;
5538 case CPP_MINUS:
5539 oprec = PREC_ADD;
5540 ocode = MINUS_EXPR;
5541 break;
5542 case CPP_LSHIFT:
5543 oprec = PREC_SHIFT;
5544 ocode = LSHIFT_EXPR;
5545 break;
5546 case CPP_RSHIFT:
5547 oprec = PREC_SHIFT;
5548 ocode = RSHIFT_EXPR;
5549 break;
5550 case CPP_LESS:
5551 oprec = PREC_REL;
5552 ocode = LT_EXPR;
5553 break;
5554 case CPP_GREATER:
5555 oprec = PREC_REL;
5556 ocode = GT_EXPR;
5557 break;
5558 case CPP_LESS_EQ:
5559 oprec = PREC_REL;
5560 ocode = LE_EXPR;
5561 break;
5562 case CPP_GREATER_EQ:
5563 oprec = PREC_REL;
5564 ocode = GE_EXPR;
5565 break;
5566 case CPP_EQ_EQ:
5567 oprec = PREC_EQ;
5568 ocode = EQ_EXPR;
5569 break;
5570 case CPP_NOT_EQ:
5571 oprec = PREC_EQ;
5572 ocode = NE_EXPR;
5573 break;
5574 case CPP_AND:
5575 oprec = PREC_BITAND;
5576 ocode = BIT_AND_EXPR;
5577 break;
5578 case CPP_XOR:
5579 oprec = PREC_BITXOR;
5580 ocode = BIT_XOR_EXPR;
5581 break;
5582 case CPP_OR:
5583 oprec = PREC_BITOR;
5584 ocode = BIT_IOR_EXPR;
5585 break;
5586 case CPP_AND_AND:
5587 oprec = PREC_LOGAND;
5588 ocode = TRUTH_ANDIF_EXPR;
5589 break;
5590 case CPP_OR_OR:
5591 oprec = PREC_LOGOR;
5592 ocode = TRUTH_ORIF_EXPR;
5593 break;
5594 default:
5595 /* Not a binary operator, so end of the binary
5596 expression. */
5597 goto out;
5599 binary_loc = c_parser_peek_token (parser)->location;
5600 c_parser_consume_token (parser);
5601 while (oprec <= stack[sp].prec)
5602 POP;
5603 switch (ocode)
5605 case TRUTH_ANDIF_EXPR:
5606 stack[sp].expr
5607 = default_function_array_read_conversion (stack[sp].loc,
5608 stack[sp].expr);
5609 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5610 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5611 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5612 == truthvalue_false_node);
5613 break;
5614 case TRUTH_ORIF_EXPR:
5615 stack[sp].expr
5616 = default_function_array_read_conversion (stack[sp].loc,
5617 stack[sp].expr);
5618 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5619 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5620 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5621 == truthvalue_true_node);
5622 break;
5623 default:
5624 break;
5626 sp++;
5627 stack[sp].loc = binary_loc;
5628 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5629 stack[sp].prec = oprec;
5630 stack[sp].op = ocode;
5631 stack[sp].loc = binary_loc;
5633 out:
5634 while (sp > 0)
5635 POP;
5636 return stack[0].expr;
5637 #undef POP
5640 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5641 NULL then it is an Objective-C message expression which is the
5642 primary-expression starting the expression as an initializer.
5644 cast-expression:
5645 unary-expression
5646 ( type-name ) unary-expression
5649 static struct c_expr
5650 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5652 location_t cast_loc = c_parser_peek_token (parser)->location;
5653 gcc_assert (!after || c_dialect_objc ());
5654 if (after)
5655 return c_parser_postfix_expression_after_primary (parser,
5656 cast_loc, *after);
5657 /* If the expression begins with a parenthesized type name, it may
5658 be either a cast or a compound literal; we need to see whether
5659 the next character is '{' to tell the difference. If not, it is
5660 an unary expression. Full detection of unknown typenames here
5661 would require a 3-token lookahead. */
5662 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5663 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5665 struct c_type_name *type_name;
5666 struct c_expr ret;
5667 struct c_expr expr;
5668 c_parser_consume_token (parser);
5669 type_name = c_parser_type_name (parser);
5670 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5671 if (type_name == NULL)
5673 ret.value = error_mark_node;
5674 ret.original_code = ERROR_MARK;
5675 ret.original_type = NULL;
5676 return ret;
5679 /* Save casted types in the function's used types hash table. */
5680 used_types_insert (type_name->specs->type);
5682 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5683 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5684 cast_loc);
5686 location_t expr_loc = c_parser_peek_token (parser)->location;
5687 expr = c_parser_cast_expression (parser, NULL);
5688 expr = default_function_array_read_conversion (expr_loc, expr);
5690 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5691 ret.original_code = ERROR_MARK;
5692 ret.original_type = NULL;
5693 return ret;
5695 else
5696 return c_parser_unary_expression (parser);
5699 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5701 unary-expression:
5702 postfix-expression
5703 ++ unary-expression
5704 -- unary-expression
5705 unary-operator cast-expression
5706 sizeof unary-expression
5707 sizeof ( type-name )
5709 unary-operator: one of
5710 & * + - ~ !
5712 GNU extensions:
5714 unary-expression:
5715 __alignof__ unary-expression
5716 __alignof__ ( type-name )
5717 && identifier
5719 unary-operator: one of
5720 __extension__ __real__ __imag__
5722 In addition, the GNU syntax treats ++ and -- as unary operators, so
5723 they may be applied to cast expressions with errors for non-lvalues
5724 given later. */
5726 static struct c_expr
5727 c_parser_unary_expression (c_parser *parser)
5729 int ext;
5730 struct c_expr ret, op;
5731 location_t op_loc = c_parser_peek_token (parser)->location;
5732 location_t exp_loc;
5733 ret.original_code = ERROR_MARK;
5734 ret.original_type = NULL;
5735 switch (c_parser_peek_token (parser)->type)
5737 case CPP_PLUS_PLUS:
5738 c_parser_consume_token (parser);
5739 exp_loc = c_parser_peek_token (parser)->location;
5740 op = c_parser_cast_expression (parser, NULL);
5741 op = default_function_array_read_conversion (exp_loc, op);
5742 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5743 case CPP_MINUS_MINUS:
5744 c_parser_consume_token (parser);
5745 exp_loc = c_parser_peek_token (parser)->location;
5746 op = c_parser_cast_expression (parser, NULL);
5747 op = default_function_array_read_conversion (exp_loc, op);
5748 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5749 case CPP_AND:
5750 c_parser_consume_token (parser);
5751 op = c_parser_cast_expression (parser, NULL);
5752 mark_exp_read (op.value);
5753 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5754 case CPP_MULT:
5755 c_parser_consume_token (parser);
5756 exp_loc = c_parser_peek_token (parser)->location;
5757 op = c_parser_cast_expression (parser, NULL);
5758 op = default_function_array_read_conversion (exp_loc, op);
5759 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5760 return ret;
5761 case CPP_PLUS:
5762 if (!c_dialect_objc () && !in_system_header)
5763 warning_at (op_loc,
5764 OPT_Wtraditional,
5765 "traditional C rejects the unary plus operator");
5766 c_parser_consume_token (parser);
5767 exp_loc = c_parser_peek_token (parser)->location;
5768 op = c_parser_cast_expression (parser, NULL);
5769 op = default_function_array_read_conversion (exp_loc, op);
5770 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5771 case CPP_MINUS:
5772 c_parser_consume_token (parser);
5773 exp_loc = c_parser_peek_token (parser)->location;
5774 op = c_parser_cast_expression (parser, NULL);
5775 op = default_function_array_read_conversion (exp_loc, op);
5776 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5777 case CPP_COMPL:
5778 c_parser_consume_token (parser);
5779 exp_loc = c_parser_peek_token (parser)->location;
5780 op = c_parser_cast_expression (parser, NULL);
5781 op = default_function_array_read_conversion (exp_loc, op);
5782 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5783 case CPP_NOT:
5784 c_parser_consume_token (parser);
5785 exp_loc = c_parser_peek_token (parser)->location;
5786 op = c_parser_cast_expression (parser, NULL);
5787 op = default_function_array_read_conversion (exp_loc, op);
5788 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5789 case CPP_AND_AND:
5790 /* Refer to the address of a label as a pointer. */
5791 c_parser_consume_token (parser);
5792 if (c_parser_next_token_is (parser, CPP_NAME))
5794 ret.value = finish_label_address_expr
5795 (c_parser_peek_token (parser)->value, op_loc);
5796 c_parser_consume_token (parser);
5798 else
5800 c_parser_error (parser, "expected identifier");
5801 ret.value = error_mark_node;
5803 return ret;
5804 case CPP_KEYWORD:
5805 switch (c_parser_peek_token (parser)->keyword)
5807 case RID_SIZEOF:
5808 return c_parser_sizeof_expression (parser);
5809 case RID_ALIGNOF:
5810 return c_parser_alignof_expression (parser);
5811 case RID_EXTENSION:
5812 c_parser_consume_token (parser);
5813 ext = disable_extension_diagnostics ();
5814 ret = c_parser_cast_expression (parser, NULL);
5815 restore_extension_diagnostics (ext);
5816 return ret;
5817 case RID_REALPART:
5818 c_parser_consume_token (parser);
5819 exp_loc = c_parser_peek_token (parser)->location;
5820 op = c_parser_cast_expression (parser, NULL);
5821 op = default_function_array_conversion (exp_loc, op);
5822 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5823 case RID_IMAGPART:
5824 c_parser_consume_token (parser);
5825 exp_loc = c_parser_peek_token (parser)->location;
5826 op = c_parser_cast_expression (parser, NULL);
5827 op = default_function_array_conversion (exp_loc, op);
5828 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5829 default:
5830 return c_parser_postfix_expression (parser);
5832 default:
5833 return c_parser_postfix_expression (parser);
5837 /* Parse a sizeof expression. */
5839 static struct c_expr
5840 c_parser_sizeof_expression (c_parser *parser)
5842 struct c_expr expr;
5843 location_t expr_loc;
5844 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5845 c_parser_consume_token (parser);
5846 c_inhibit_evaluation_warnings++;
5847 in_sizeof++;
5848 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5849 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5851 /* Either sizeof ( type-name ) or sizeof unary-expression
5852 starting with a compound literal. */
5853 struct c_type_name *type_name;
5854 c_parser_consume_token (parser);
5855 expr_loc = c_parser_peek_token (parser)->location;
5856 type_name = c_parser_type_name (parser);
5857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5858 if (type_name == NULL)
5860 struct c_expr ret;
5861 c_inhibit_evaluation_warnings--;
5862 in_sizeof--;
5863 ret.value = error_mark_node;
5864 ret.original_code = ERROR_MARK;
5865 ret.original_type = NULL;
5866 return ret;
5868 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5870 expr = c_parser_postfix_expression_after_paren_type (parser,
5871 type_name,
5872 expr_loc);
5873 goto sizeof_expr;
5875 /* sizeof ( type-name ). */
5876 c_inhibit_evaluation_warnings--;
5877 in_sizeof--;
5878 return c_expr_sizeof_type (expr_loc, type_name);
5880 else
5882 expr_loc = c_parser_peek_token (parser)->location;
5883 expr = c_parser_unary_expression (parser);
5884 sizeof_expr:
5885 c_inhibit_evaluation_warnings--;
5886 in_sizeof--;
5887 mark_exp_read (expr.value);
5888 if (TREE_CODE (expr.value) == COMPONENT_REF
5889 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5890 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5891 return c_expr_sizeof_expr (expr_loc, expr);
5895 /* Parse an alignof expression. */
5897 static struct c_expr
5898 c_parser_alignof_expression (c_parser *parser)
5900 struct c_expr expr;
5901 location_t loc = c_parser_peek_token (parser)->location;
5902 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5903 c_parser_consume_token (parser);
5904 c_inhibit_evaluation_warnings++;
5905 in_alignof++;
5906 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5907 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5909 /* Either __alignof__ ( type-name ) or __alignof__
5910 unary-expression starting with a compound literal. */
5911 location_t loc;
5912 struct c_type_name *type_name;
5913 struct c_expr ret;
5914 c_parser_consume_token (parser);
5915 loc = c_parser_peek_token (parser)->location;
5916 type_name = c_parser_type_name (parser);
5917 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5918 if (type_name == NULL)
5920 struct c_expr ret;
5921 c_inhibit_evaluation_warnings--;
5922 in_alignof--;
5923 ret.value = error_mark_node;
5924 ret.original_code = ERROR_MARK;
5925 ret.original_type = NULL;
5926 return ret;
5928 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5930 expr = c_parser_postfix_expression_after_paren_type (parser,
5931 type_name,
5932 loc);
5933 goto alignof_expr;
5935 /* alignof ( type-name ). */
5936 c_inhibit_evaluation_warnings--;
5937 in_alignof--;
5938 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5939 ret.original_code = ERROR_MARK;
5940 ret.original_type = NULL;
5941 return ret;
5943 else
5945 struct c_expr ret;
5946 expr = c_parser_unary_expression (parser);
5947 alignof_expr:
5948 mark_exp_read (expr.value);
5949 c_inhibit_evaluation_warnings--;
5950 in_alignof--;
5951 ret.value = c_alignof_expr (loc, expr.value);
5952 ret.original_code = ERROR_MARK;
5953 ret.original_type = NULL;
5954 return ret;
5958 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5960 postfix-expression:
5961 primary-expression
5962 postfix-expression [ expression ]
5963 postfix-expression ( argument-expression-list[opt] )
5964 postfix-expression . identifier
5965 postfix-expression -> identifier
5966 postfix-expression ++
5967 postfix-expression --
5968 ( type-name ) { initializer-list }
5969 ( type-name ) { initializer-list , }
5971 argument-expression-list:
5972 argument-expression
5973 argument-expression-list , argument-expression
5975 primary-expression:
5976 identifier
5977 constant
5978 string-literal
5979 ( expression )
5981 GNU extensions:
5983 primary-expression:
5984 __func__
5985 (treated as a keyword in GNU C)
5986 __FUNCTION__
5987 __PRETTY_FUNCTION__
5988 ( compound-statement )
5989 __builtin_va_arg ( assignment-expression , type-name )
5990 __builtin_offsetof ( type-name , offsetof-member-designator )
5991 __builtin_choose_expr ( assignment-expression ,
5992 assignment-expression ,
5993 assignment-expression )
5994 __builtin_types_compatible_p ( type-name , type-name )
5996 offsetof-member-designator:
5997 identifier
5998 offsetof-member-designator . identifier
5999 offsetof-member-designator [ expression ]
6001 Objective-C:
6003 primary-expression:
6004 [ objc-receiver objc-message-args ]
6005 @selector ( objc-selector-arg )
6006 @protocol ( identifier )
6007 @encode ( type-name )
6008 objc-string-literal
6009 Classname . identifier
6012 static struct c_expr
6013 c_parser_postfix_expression (c_parser *parser)
6015 struct c_expr expr, e1, e2, e3;
6016 struct c_type_name *t1, *t2;
6017 location_t loc = c_parser_peek_token (parser)->location;;
6018 expr.original_code = ERROR_MARK;
6019 expr.original_type = NULL;
6020 switch (c_parser_peek_token (parser)->type)
6022 case CPP_NUMBER:
6023 expr.value = c_parser_peek_token (parser)->value;
6024 loc = c_parser_peek_token (parser)->location;
6025 c_parser_consume_token (parser);
6026 if (TREE_CODE (expr.value) == FIXED_CST
6027 && !targetm.fixed_point_supported_p ())
6029 error_at (loc, "fixed-point types not supported for this target");
6030 expr.value = error_mark_node;
6032 break;
6033 case CPP_CHAR:
6034 case CPP_CHAR16:
6035 case CPP_CHAR32:
6036 case CPP_WCHAR:
6037 expr.value = c_parser_peek_token (parser)->value;
6038 c_parser_consume_token (parser);
6039 break;
6040 case CPP_STRING:
6041 case CPP_STRING16:
6042 case CPP_STRING32:
6043 case CPP_WSTRING:
6044 case CPP_UTF8STRING:
6045 expr.value = c_parser_peek_token (parser)->value;
6046 expr.original_code = STRING_CST;
6047 c_parser_consume_token (parser);
6048 break;
6049 case CPP_OBJC_STRING:
6050 gcc_assert (c_dialect_objc ());
6051 expr.value
6052 = objc_build_string_object (c_parser_peek_token (parser)->value);
6053 c_parser_consume_token (parser);
6054 break;
6055 case CPP_NAME:
6056 switch (c_parser_peek_token (parser)->id_kind)
6058 case C_ID_ID:
6060 tree id = c_parser_peek_token (parser)->value;
6061 c_parser_consume_token (parser);
6062 expr.value = build_external_ref (loc, id,
6063 (c_parser_peek_token (parser)->type
6064 == CPP_OPEN_PAREN),
6065 &expr.original_type);
6066 break;
6068 case C_ID_CLASSNAME:
6070 /* Here we parse the Objective-C 2.0 Class.name dot
6071 syntax. */
6072 tree class_name = c_parser_peek_token (parser)->value;
6073 tree component;
6074 c_parser_consume_token (parser);
6075 gcc_assert (c_dialect_objc ());
6076 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6078 expr.value = error_mark_node;
6079 break;
6081 if (c_parser_next_token_is_not (parser, CPP_NAME))
6083 c_parser_error (parser, "expected identifier");
6084 expr.value = error_mark_node;
6085 break;
6087 component = c_parser_peek_token (parser)->value;
6088 c_parser_consume_token (parser);
6089 expr.value = objc_build_class_component_ref (class_name,
6090 component);
6091 break;
6093 default:
6094 c_parser_error (parser, "expected expression");
6095 expr.value = error_mark_node;
6096 break;
6098 break;
6099 case CPP_OPEN_PAREN:
6100 /* A parenthesized expression, statement expression or compound
6101 literal. */
6102 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6104 /* A statement expression. */
6105 tree stmt;
6106 location_t brace_loc;
6107 c_parser_consume_token (parser);
6108 brace_loc = c_parser_peek_token (parser)->location;
6109 c_parser_consume_token (parser);
6110 if (cur_stmt_list == NULL)
6112 error_at (loc, "braced-group within expression allowed "
6113 "only inside a function");
6114 parser->error = true;
6115 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6117 expr.value = error_mark_node;
6118 break;
6120 stmt = c_begin_stmt_expr ();
6121 c_parser_compound_statement_nostart (parser);
6122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6123 "expected %<)%>");
6124 pedwarn (loc, OPT_pedantic,
6125 "ISO C forbids braced-groups within expressions");
6126 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6127 mark_exp_read (expr.value);
6129 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6131 /* A compound literal. ??? Can we actually get here rather
6132 than going directly to
6133 c_parser_postfix_expression_after_paren_type from
6134 elsewhere? */
6135 location_t loc;
6136 struct c_type_name *type_name;
6137 c_parser_consume_token (parser);
6138 loc = c_parser_peek_token (parser)->location;
6139 type_name = c_parser_type_name (parser);
6140 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6141 "expected %<)%>");
6142 if (type_name == NULL)
6144 expr.value = error_mark_node;
6146 else
6147 expr = c_parser_postfix_expression_after_paren_type (parser,
6148 type_name,
6149 loc);
6151 else
6153 /* A parenthesized expression. */
6154 c_parser_consume_token (parser);
6155 expr = c_parser_expression (parser);
6156 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6157 TREE_NO_WARNING (expr.value) = 1;
6158 if (expr.original_code != C_MAYBE_CONST_EXPR)
6159 expr.original_code = ERROR_MARK;
6160 /* Don't change EXPR.ORIGINAL_TYPE. */
6161 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6162 "expected %<)%>");
6164 break;
6165 case CPP_KEYWORD:
6166 switch (c_parser_peek_token (parser)->keyword)
6168 case RID_FUNCTION_NAME:
6169 case RID_PRETTY_FUNCTION_NAME:
6170 case RID_C99_FUNCTION_NAME:
6171 expr.value = fname_decl (loc,
6172 c_parser_peek_token (parser)->keyword,
6173 c_parser_peek_token (parser)->value);
6174 c_parser_consume_token (parser);
6175 break;
6176 case RID_VA_ARG:
6177 c_parser_consume_token (parser);
6178 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6180 expr.value = error_mark_node;
6181 break;
6183 e1 = c_parser_expr_no_commas (parser, NULL);
6184 mark_exp_read (e1.value);
6185 e1.value = c_fully_fold (e1.value, false, NULL);
6186 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6188 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6189 expr.value = error_mark_node;
6190 break;
6192 loc = c_parser_peek_token (parser)->location;
6193 t1 = c_parser_type_name (parser);
6194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6195 "expected %<)%>");
6196 if (t1 == NULL)
6198 expr.value = error_mark_node;
6200 else
6202 tree type_expr = NULL_TREE;
6203 expr.value = c_build_va_arg (loc, e1.value,
6204 groktypename (t1, &type_expr, NULL));
6205 if (type_expr)
6207 expr.value = build2 (C_MAYBE_CONST_EXPR,
6208 TREE_TYPE (expr.value), type_expr,
6209 expr.value);
6210 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6213 break;
6214 case RID_OFFSETOF:
6215 c_parser_consume_token (parser);
6216 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6218 expr.value = error_mark_node;
6219 break;
6221 t1 = c_parser_type_name (parser);
6222 if (t1 == NULL)
6223 parser->error = true;
6224 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6225 gcc_assert (parser->error);
6226 if (parser->error)
6228 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6229 expr.value = error_mark_node;
6230 break;
6234 tree type = groktypename (t1, NULL, NULL);
6235 tree offsetof_ref;
6236 if (type == error_mark_node)
6237 offsetof_ref = error_mark_node;
6238 else
6240 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6241 SET_EXPR_LOCATION (offsetof_ref, loc);
6243 /* Parse the second argument to __builtin_offsetof. We
6244 must have one identifier, and beyond that we want to
6245 accept sub structure and sub array references. */
6246 if (c_parser_next_token_is (parser, CPP_NAME))
6248 offsetof_ref = build_component_ref
6249 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6250 c_parser_consume_token (parser);
6251 while (c_parser_next_token_is (parser, CPP_DOT)
6252 || c_parser_next_token_is (parser,
6253 CPP_OPEN_SQUARE)
6254 || c_parser_next_token_is (parser,
6255 CPP_DEREF))
6257 if (c_parser_next_token_is (parser, CPP_DEREF))
6259 loc = c_parser_peek_token (parser)->location;
6260 offsetof_ref = build_array_ref (loc,
6261 offsetof_ref,
6262 integer_zero_node);
6263 goto do_dot;
6265 else if (c_parser_next_token_is (parser, CPP_DOT))
6267 do_dot:
6268 c_parser_consume_token (parser);
6269 if (c_parser_next_token_is_not (parser,
6270 CPP_NAME))
6272 c_parser_error (parser, "expected identifier");
6273 break;
6275 offsetof_ref = build_component_ref
6276 (loc, offsetof_ref,
6277 c_parser_peek_token (parser)->value);
6278 c_parser_consume_token (parser);
6280 else
6282 tree idx;
6283 loc = c_parser_peek_token (parser)->location;
6284 c_parser_consume_token (parser);
6285 idx = c_parser_expression (parser).value;
6286 idx = c_fully_fold (idx, false, NULL);
6287 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6288 "expected %<]%>");
6289 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6293 else
6294 c_parser_error (parser, "expected identifier");
6295 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6296 "expected %<)%>");
6297 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6299 break;
6300 case RID_CHOOSE_EXPR:
6301 c_parser_consume_token (parser);
6302 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6304 expr.value = error_mark_node;
6305 break;
6307 loc = c_parser_peek_token (parser)->location;
6308 e1 = c_parser_expr_no_commas (parser, NULL);
6309 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6311 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6312 expr.value = error_mark_node;
6313 break;
6315 e2 = c_parser_expr_no_commas (parser, NULL);
6316 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6318 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6319 expr.value = error_mark_node;
6320 break;
6322 e3 = c_parser_expr_no_commas (parser, NULL);
6323 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6324 "expected %<)%>");
6326 tree c;
6328 c = e1.value;
6329 mark_exp_read (e2.value);
6330 mark_exp_read (e3.value);
6331 if (TREE_CODE (c) != INTEGER_CST
6332 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6333 error_at (loc,
6334 "first argument to %<__builtin_choose_expr%> not"
6335 " a constant");
6336 constant_expression_warning (c);
6337 expr = integer_zerop (c) ? e3 : e2;
6339 break;
6340 case RID_TYPES_COMPATIBLE_P:
6341 c_parser_consume_token (parser);
6342 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6344 expr.value = error_mark_node;
6345 break;
6347 t1 = c_parser_type_name (parser);
6348 if (t1 == NULL)
6350 expr.value = error_mark_node;
6351 break;
6353 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6356 expr.value = error_mark_node;
6357 break;
6359 t2 = c_parser_type_name (parser);
6360 if (t2 == NULL)
6362 expr.value = error_mark_node;
6363 break;
6365 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6366 "expected %<)%>");
6368 tree e1, e2;
6370 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6371 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6373 expr.value
6374 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6376 break;
6377 case RID_AT_SELECTOR:
6378 gcc_assert (c_dialect_objc ());
6379 c_parser_consume_token (parser);
6380 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6382 expr.value = error_mark_node;
6383 break;
6386 tree sel = c_parser_objc_selector_arg (parser);
6387 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6388 "expected %<)%>");
6389 expr.value = objc_build_selector_expr (loc, sel);
6391 break;
6392 case RID_AT_PROTOCOL:
6393 gcc_assert (c_dialect_objc ());
6394 c_parser_consume_token (parser);
6395 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6397 expr.value = error_mark_node;
6398 break;
6400 if (c_parser_next_token_is_not (parser, CPP_NAME))
6402 c_parser_error (parser, "expected identifier");
6403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6404 expr.value = error_mark_node;
6405 break;
6408 tree id = c_parser_peek_token (parser)->value;
6409 c_parser_consume_token (parser);
6410 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6411 "expected %<)%>");
6412 expr.value = objc_build_protocol_expr (id);
6414 break;
6415 case RID_AT_ENCODE:
6416 /* Extension to support C-structures in the archiver. */
6417 gcc_assert (c_dialect_objc ());
6418 c_parser_consume_token (parser);
6419 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6421 expr.value = error_mark_node;
6422 break;
6424 t1 = c_parser_type_name (parser);
6425 if (t1 == NULL)
6427 expr.value = error_mark_node;
6428 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6429 break;
6431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6432 "expected %<)%>");
6434 tree type = groktypename (t1, NULL, NULL);
6435 expr.value = objc_build_encode_expr (type);
6437 break;
6438 default:
6439 c_parser_error (parser, "expected expression");
6440 expr.value = error_mark_node;
6441 break;
6443 break;
6444 case CPP_OPEN_SQUARE:
6445 if (c_dialect_objc ())
6447 tree receiver, args;
6448 c_parser_consume_token (parser);
6449 receiver = c_parser_objc_receiver (parser);
6450 args = c_parser_objc_message_args (parser);
6451 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6452 "expected %<]%>");
6453 expr.value = objc_build_message_expr (build_tree_list (receiver,
6454 args));
6455 break;
6457 /* Else fall through to report error. */
6458 default:
6459 c_parser_error (parser, "expected expression");
6460 expr.value = error_mark_node;
6461 break;
6463 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6466 /* Parse a postfix expression after a parenthesized type name: the
6467 brace-enclosed initializer of a compound literal, possibly followed
6468 by some postfix operators. This is separate because it is not
6469 possible to tell until after the type name whether a cast
6470 expression has a cast or a compound literal, or whether the operand
6471 of sizeof is a parenthesized type name or starts with a compound
6472 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6473 location of the first token after the parentheses around the type
6474 name. */
6476 static struct c_expr
6477 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6478 struct c_type_name *type_name,
6479 location_t type_loc)
6481 tree type;
6482 struct c_expr init;
6483 bool non_const;
6484 struct c_expr expr;
6485 location_t start_loc;
6486 tree type_expr = NULL_TREE;
6487 bool type_expr_const = true;
6488 check_compound_literal_type (type_loc, type_name);
6489 start_init (NULL_TREE, NULL, 0);
6490 type = groktypename (type_name, &type_expr, &type_expr_const);
6491 start_loc = c_parser_peek_token (parser)->location;
6492 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6494 error_at (type_loc, "compound literal has variable size");
6495 type = error_mark_node;
6497 init = c_parser_braced_init (parser, type, false);
6498 finish_init ();
6499 maybe_warn_string_init (type, init);
6501 if (type != error_mark_node
6502 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6503 && current_function_decl)
6505 error ("compound literal qualified by address-space qualifier");
6506 type = error_mark_node;
6509 if (!flag_isoc99)
6510 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6511 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6512 ? CONSTRUCTOR_NON_CONST (init.value)
6513 : init.original_code == C_MAYBE_CONST_EXPR);
6514 non_const |= !type_expr_const;
6515 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6516 expr.original_code = ERROR_MARK;
6517 expr.original_type = NULL;
6518 if (type_expr)
6520 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6522 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6523 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6525 else
6527 gcc_assert (!non_const);
6528 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6529 type_expr, expr.value);
6532 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6535 /* Parse a postfix expression after the initial primary or compound
6536 literal; that is, parse a series of postfix operators.
6538 EXPR_LOC is the location of the primary expression. */
6540 static struct c_expr
6541 c_parser_postfix_expression_after_primary (c_parser *parser,
6542 location_t expr_loc,
6543 struct c_expr expr)
6545 struct c_expr orig_expr;
6546 tree ident, idx;
6547 VEC(tree,gc) *exprlist;
6548 VEC(tree,gc) *origtypes;
6549 while (true)
6551 location_t op_loc = c_parser_peek_token (parser)->location;
6552 switch (c_parser_peek_token (parser)->type)
6554 case CPP_OPEN_SQUARE:
6555 /* Array reference. */
6556 c_parser_consume_token (parser);
6557 idx = c_parser_expression (parser).value;
6558 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6559 "expected %<]%>");
6560 expr.value = build_array_ref (op_loc, expr.value, idx);
6561 expr.original_code = ERROR_MARK;
6562 expr.original_type = NULL;
6563 break;
6564 case CPP_OPEN_PAREN:
6565 /* Function call. */
6566 c_parser_consume_token (parser);
6567 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6568 exprlist = NULL;
6569 else
6570 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6572 "expected %<)%>");
6573 orig_expr = expr;
6574 mark_exp_read (expr.value);
6575 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6576 "(" after the FUNCNAME, which is what we have now. */
6577 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6578 origtypes);
6579 expr.original_code = ERROR_MARK;
6580 if (TREE_CODE (expr.value) == INTEGER_CST
6581 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6582 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6583 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6584 expr.original_code = C_MAYBE_CONST_EXPR;
6585 expr.original_type = NULL;
6586 if (exprlist != NULL)
6588 release_tree_vector (exprlist);
6589 release_tree_vector (origtypes);
6591 break;
6592 case CPP_DOT:
6593 /* Structure element reference. */
6594 c_parser_consume_token (parser);
6595 expr = default_function_array_conversion (expr_loc, expr);
6596 if (c_parser_next_token_is (parser, CPP_NAME))
6597 ident = c_parser_peek_token (parser)->value;
6598 else
6600 c_parser_error (parser, "expected identifier");
6601 expr.value = error_mark_node;
6602 expr.original_code = ERROR_MARK;
6603 expr.original_type = NULL;
6604 return expr;
6606 c_parser_consume_token (parser);
6607 expr.value = build_component_ref (op_loc, expr.value, ident);
6608 expr.original_code = ERROR_MARK;
6609 if (TREE_CODE (expr.value) != COMPONENT_REF)
6610 expr.original_type = NULL;
6611 else
6613 /* Remember the original type of a bitfield. */
6614 tree field = TREE_OPERAND (expr.value, 1);
6615 if (TREE_CODE (field) != FIELD_DECL)
6616 expr.original_type = NULL;
6617 else
6618 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6620 break;
6621 case CPP_DEREF:
6622 /* Structure element reference. */
6623 c_parser_consume_token (parser);
6624 expr = default_function_array_conversion (expr_loc, expr);
6625 if (c_parser_next_token_is (parser, CPP_NAME))
6626 ident = c_parser_peek_token (parser)->value;
6627 else
6629 c_parser_error (parser, "expected identifier");
6630 expr.value = error_mark_node;
6631 expr.original_code = ERROR_MARK;
6632 expr.original_type = NULL;
6633 return expr;
6635 c_parser_consume_token (parser);
6636 expr.value = build_component_ref (op_loc,
6637 build_indirect_ref (op_loc,
6638 expr.value,
6639 RO_ARROW),
6640 ident);
6641 expr.original_code = ERROR_MARK;
6642 if (TREE_CODE (expr.value) != COMPONENT_REF)
6643 expr.original_type = NULL;
6644 else
6646 /* Remember the original type of a bitfield. */
6647 tree field = TREE_OPERAND (expr.value, 1);
6648 if (TREE_CODE (field) != FIELD_DECL)
6649 expr.original_type = NULL;
6650 else
6651 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6653 break;
6654 case CPP_PLUS_PLUS:
6655 /* Postincrement. */
6656 c_parser_consume_token (parser);
6657 expr = default_function_array_read_conversion (expr_loc, expr);
6658 expr.value = build_unary_op (op_loc,
6659 POSTINCREMENT_EXPR, expr.value, 0);
6660 expr.original_code = ERROR_MARK;
6661 expr.original_type = NULL;
6662 break;
6663 case CPP_MINUS_MINUS:
6664 /* Postdecrement. */
6665 c_parser_consume_token (parser);
6666 expr = default_function_array_read_conversion (expr_loc, expr);
6667 expr.value = build_unary_op (op_loc,
6668 POSTDECREMENT_EXPR, expr.value, 0);
6669 expr.original_code = ERROR_MARK;
6670 expr.original_type = NULL;
6671 break;
6672 default:
6673 return expr;
6678 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6680 expression:
6681 assignment-expression
6682 expression , assignment-expression
6685 static struct c_expr
6686 c_parser_expression (c_parser *parser)
6688 struct c_expr expr;
6689 expr = c_parser_expr_no_commas (parser, NULL);
6690 while (c_parser_next_token_is (parser, CPP_COMMA))
6692 struct c_expr next;
6693 tree lhsval;
6694 location_t loc = c_parser_peek_token (parser)->location;
6695 location_t expr_loc;
6696 c_parser_consume_token (parser);
6697 expr_loc = c_parser_peek_token (parser)->location;
6698 lhsval = expr.value;
6699 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6700 lhsval = TREE_OPERAND (lhsval, 1);
6701 if (DECL_P (lhsval) || handled_component_p (lhsval))
6702 mark_exp_read (lhsval);
6703 next = c_parser_expr_no_commas (parser, NULL);
6704 next = default_function_array_conversion (expr_loc, next);
6705 expr.value = build_compound_expr (loc, expr.value, next.value);
6706 expr.original_code = COMPOUND_EXPR;
6707 expr.original_type = next.original_type;
6709 return expr;
6712 /* Parse an expression and convert functions or arrays to
6713 pointers. */
6715 static struct c_expr
6716 c_parser_expression_conv (c_parser *parser)
6718 struct c_expr expr;
6719 location_t loc = c_parser_peek_token (parser)->location;
6720 expr = c_parser_expression (parser);
6721 expr = default_function_array_conversion (loc, expr);
6722 return expr;
6725 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6726 functions and arrays to pointers. If FOLD_P, fold the expressions.
6728 nonempty-expr-list:
6729 assignment-expression
6730 nonempty-expr-list , assignment-expression
6733 static VEC(tree,gc) *
6734 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6735 VEC(tree,gc) **p_orig_types)
6737 VEC(tree,gc) *ret;
6738 VEC(tree,gc) *orig_types;
6739 struct c_expr expr;
6740 location_t loc = c_parser_peek_token (parser)->location;
6742 ret = make_tree_vector ();
6743 if (p_orig_types == NULL)
6744 orig_types = NULL;
6745 else
6746 orig_types = make_tree_vector ();
6748 expr = c_parser_expr_no_commas (parser, NULL);
6749 if (convert_p)
6750 expr = default_function_array_read_conversion (loc, expr);
6751 if (fold_p)
6752 expr.value = c_fully_fold (expr.value, false, NULL);
6753 VEC_quick_push (tree, ret, expr.value);
6754 if (orig_types != NULL)
6755 VEC_quick_push (tree, orig_types, expr.original_type);
6756 while (c_parser_next_token_is (parser, CPP_COMMA))
6758 c_parser_consume_token (parser);
6759 loc = c_parser_peek_token (parser)->location;
6760 expr = c_parser_expr_no_commas (parser, NULL);
6761 if (convert_p)
6762 expr = default_function_array_read_conversion (loc, expr);
6763 if (fold_p)
6764 expr.value = c_fully_fold (expr.value, false, NULL);
6765 VEC_safe_push (tree, gc, ret, expr.value);
6766 if (orig_types != NULL)
6767 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6769 if (orig_types != NULL)
6770 *p_orig_types = orig_types;
6771 return ret;
6774 /* Parse Objective-C-specific constructs. */
6776 /* Parse an objc-class-definition.
6778 objc-class-definition:
6779 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6780 objc-class-instance-variables[opt] objc-methodprotolist @end
6781 @implementation identifier objc-superclass[opt]
6782 objc-class-instance-variables[opt]
6783 @interface identifier ( identifier ) objc-protocol-refs[opt]
6784 objc-methodprotolist @end
6785 @interface identifier ( ) objc-protocol-refs[opt]
6786 objc-methodprotolist @end
6787 @implementation identifier ( identifier )
6789 objc-superclass:
6790 : identifier
6792 "@interface identifier (" must start "@interface identifier (
6793 identifier ) ...": objc-methodprotolist in the first production may
6794 not start with a parenthesized identifier as a declarator of a data
6795 definition with no declaration specifiers if the objc-superclass,
6796 objc-protocol-refs and objc-class-instance-variables are omitted. */
6798 static void
6799 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6801 bool iface_p;
6802 tree id1;
6803 tree superclass;
6804 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6805 iface_p = true;
6806 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6807 iface_p = false;
6808 else
6809 gcc_unreachable ();
6811 c_parser_consume_token (parser);
6812 if (c_parser_next_token_is_not (parser, CPP_NAME))
6814 c_parser_error (parser, "expected identifier");
6815 return;
6817 id1 = c_parser_peek_token (parser)->value;
6818 c_parser_consume_token (parser);
6819 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6821 /* We have a category or class extension. */
6822 tree id2;
6823 tree proto = NULL_TREE;
6824 c_parser_consume_token (parser);
6825 if (c_parser_next_token_is_not (parser, CPP_NAME))
6827 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6829 /* We have a class extension. */
6830 id2 = NULL_TREE;
6832 else
6834 c_parser_error (parser, "expected identifier or %<)%>");
6835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6836 return;
6839 else
6841 id2 = c_parser_peek_token (parser)->value;
6842 c_parser_consume_token (parser);
6844 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6845 if (!iface_p)
6847 objc_start_category_implementation (id1, id2);
6848 return;
6850 if (c_parser_next_token_is (parser, CPP_LESS))
6851 proto = c_parser_objc_protocol_refs (parser);
6852 objc_start_category_interface (id1, id2, proto, attributes);
6853 c_parser_objc_methodprotolist (parser);
6854 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6855 objc_finish_interface ();
6856 return;
6858 if (c_parser_next_token_is (parser, CPP_COLON))
6860 c_parser_consume_token (parser);
6861 if (c_parser_next_token_is_not (parser, CPP_NAME))
6863 c_parser_error (parser, "expected identifier");
6864 return;
6866 superclass = c_parser_peek_token (parser)->value;
6867 c_parser_consume_token (parser);
6869 else
6870 superclass = NULL_TREE;
6871 if (iface_p)
6873 tree proto = NULL_TREE;
6874 if (c_parser_next_token_is (parser, CPP_LESS))
6875 proto = c_parser_objc_protocol_refs (parser);
6876 objc_start_class_interface (id1, superclass, proto, attributes);
6878 else
6879 objc_start_class_implementation (id1, superclass);
6880 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6881 c_parser_objc_class_instance_variables (parser);
6882 if (iface_p)
6884 objc_continue_interface ();
6885 c_parser_objc_methodprotolist (parser);
6886 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6887 objc_finish_interface ();
6889 else
6891 objc_continue_implementation ();
6892 return;
6896 /* Parse objc-class-instance-variables.
6898 objc-class-instance-variables:
6899 { objc-instance-variable-decl-list[opt] }
6901 objc-instance-variable-decl-list:
6902 objc-visibility-spec
6903 objc-instance-variable-decl ;
6905 objc-instance-variable-decl-list objc-visibility-spec
6906 objc-instance-variable-decl-list objc-instance-variable-decl ;
6907 objc-instance-variable-decl-list ;
6909 objc-visibility-spec:
6910 @private
6911 @protected
6912 @public
6914 objc-instance-variable-decl:
6915 struct-declaration
6918 static void
6919 c_parser_objc_class_instance_variables (c_parser *parser)
6921 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6922 c_parser_consume_token (parser);
6923 while (c_parser_next_token_is_not (parser, CPP_EOF))
6925 tree decls;
6926 /* Parse any stray semicolon. */
6927 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6929 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6930 "extra semicolon in struct or union specified");
6931 c_parser_consume_token (parser);
6932 continue;
6934 /* Stop if at the end of the instance variables. */
6935 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6937 c_parser_consume_token (parser);
6938 break;
6940 /* Parse any objc-visibility-spec. */
6941 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6943 c_parser_consume_token (parser);
6944 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6945 continue;
6947 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6949 c_parser_consume_token (parser);
6950 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6951 continue;
6953 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6955 c_parser_consume_token (parser);
6956 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6957 continue;
6959 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6961 c_parser_consume_token (parser);
6962 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6963 continue;
6965 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6967 c_parser_pragma (parser, pragma_external);
6968 continue;
6971 /* Parse some comma-separated declarations. */
6972 decls = c_parser_struct_declaration (parser);
6974 /* Comma-separated instance variables are chained together in
6975 reverse order; add them one by one. */
6976 tree ivar = nreverse (decls);
6977 for (; ivar; ivar = DECL_CHAIN (ivar))
6978 objc_add_instance_variable (copy_node (ivar));
6980 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6984 /* Parse an objc-class-declaration.
6986 objc-class-declaration:
6987 @class identifier-list ;
6990 static void
6991 c_parser_objc_class_declaration (c_parser *parser)
6993 tree list = NULL_TREE;
6994 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
6995 c_parser_consume_token (parser);
6996 /* Any identifiers, including those declared as type names, are OK
6997 here. */
6998 while (true)
7000 tree id;
7001 if (c_parser_next_token_is_not (parser, CPP_NAME))
7003 c_parser_error (parser, "expected identifier");
7004 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7005 parser->error = false;
7006 return;
7008 id = c_parser_peek_token (parser)->value;
7009 list = chainon (list, build_tree_list (NULL_TREE, id));
7010 c_parser_consume_token (parser);
7011 if (c_parser_next_token_is (parser, CPP_COMMA))
7012 c_parser_consume_token (parser);
7013 else
7014 break;
7016 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7017 objc_declare_class (list);
7020 /* Parse an objc-alias-declaration.
7022 objc-alias-declaration:
7023 @compatibility_alias identifier identifier ;
7026 static void
7027 c_parser_objc_alias_declaration (c_parser *parser)
7029 tree id1, id2;
7030 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7031 c_parser_consume_token (parser);
7032 if (c_parser_next_token_is_not (parser, CPP_NAME))
7034 c_parser_error (parser, "expected identifier");
7035 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7036 return;
7038 id1 = c_parser_peek_token (parser)->value;
7039 c_parser_consume_token (parser);
7040 if (c_parser_next_token_is_not (parser, CPP_NAME))
7042 c_parser_error (parser, "expected identifier");
7043 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7044 return;
7046 id2 = c_parser_peek_token (parser)->value;
7047 c_parser_consume_token (parser);
7048 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7049 objc_declare_alias (id1, id2);
7052 /* Parse an objc-protocol-definition.
7054 objc-protocol-definition:
7055 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7056 @protocol identifier-list ;
7058 "@protocol identifier ;" should be resolved as "@protocol
7059 identifier-list ;": objc-methodprotolist may not start with a
7060 semicolon in the first alternative if objc-protocol-refs are
7061 omitted. */
7063 static void
7064 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7066 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
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 return;
7074 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7075 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7077 tree list = NULL_TREE;
7078 /* Any identifiers, including those declared as type names, are
7079 OK here. */
7080 while (true)
7082 tree id;
7083 if (c_parser_next_token_is_not (parser, CPP_NAME))
7085 c_parser_error (parser, "expected identifier");
7086 break;
7088 id = c_parser_peek_token (parser)->value;
7089 list = chainon (list, build_tree_list (NULL_TREE, id));
7090 c_parser_consume_token (parser);
7091 if (c_parser_next_token_is (parser, CPP_COMMA))
7092 c_parser_consume_token (parser);
7093 else
7094 break;
7096 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7097 objc_declare_protocols (list, attributes);
7099 else
7101 tree id = c_parser_peek_token (parser)->value;
7102 tree proto = NULL_TREE;
7103 c_parser_consume_token (parser);
7104 if (c_parser_next_token_is (parser, CPP_LESS))
7105 proto = c_parser_objc_protocol_refs (parser);
7106 parser->objc_pq_context = true;
7107 objc_start_protocol (id, proto, attributes);
7108 c_parser_objc_methodprotolist (parser);
7109 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7110 parser->objc_pq_context = false;
7111 objc_finish_interface ();
7115 /* Parse an objc-method-type.
7117 objc-method-type:
7121 Return true if it is a class method (+) and false if it is
7122 an instance method (-).
7124 static inline bool
7125 c_parser_objc_method_type (c_parser *parser)
7127 switch (c_parser_peek_token (parser)->type)
7129 case CPP_PLUS:
7130 c_parser_consume_token (parser);
7131 return true;
7132 case CPP_MINUS:
7133 c_parser_consume_token (parser);
7134 return false;
7135 default:
7136 gcc_unreachable ();
7140 /* Parse an objc-method-definition.
7142 objc-method-definition:
7143 objc-method-type objc-method-decl ;[opt] compound-statement
7146 static void
7147 c_parser_objc_method_definition (c_parser *parser)
7149 bool is_class_method = c_parser_objc_method_type (parser);
7150 tree decl, attributes = NULL_TREE;
7151 parser->objc_pq_context = true;
7152 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7153 if (decl == error_mark_node)
7154 return; /* Bail here. */
7156 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7158 c_parser_consume_token (parser);
7159 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7160 "extra semicolon in method definition specified");
7163 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7165 c_parser_error (parser, "expected %<{%>");
7166 return;
7169 parser->objc_pq_context = false;
7170 if (objc_start_method_definition (is_class_method, decl, attributes))
7172 add_stmt (c_parser_compound_statement (parser));
7173 objc_finish_method_definition (current_function_decl);
7175 else
7177 /* This code is executed when we find a method definition
7178 outside of an @implementation context (or invalid for other
7179 reasons). Parse the method (to keep going) but do not emit
7180 any code.
7182 c_parser_compound_statement (parser);
7186 /* Parse an objc-methodprotolist.
7188 objc-methodprotolist:
7189 empty
7190 objc-methodprotolist objc-methodproto
7191 objc-methodprotolist declaration
7192 objc-methodprotolist ;
7193 @optional
7194 @required
7196 The declaration is a data definition, which may be missing
7197 declaration specifiers under the same rules and diagnostics as
7198 other data definitions outside functions, and the stray semicolon
7199 is diagnosed the same way as a stray semicolon outside a
7200 function. */
7202 static void
7203 c_parser_objc_methodprotolist (c_parser *parser)
7205 while (true)
7207 /* The list is terminated by @end. */
7208 switch (c_parser_peek_token (parser)->type)
7210 case CPP_SEMICOLON:
7211 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7212 "ISO C does not allow extra %<;%> outside of a function");
7213 c_parser_consume_token (parser);
7214 break;
7215 case CPP_PLUS:
7216 case CPP_MINUS:
7217 c_parser_objc_methodproto (parser);
7218 break;
7219 case CPP_PRAGMA:
7220 c_parser_pragma (parser, pragma_external);
7221 break;
7222 case CPP_EOF:
7223 return;
7224 default:
7225 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7226 return;
7227 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7228 c_parser_objc_at_property_declaration (parser);
7229 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7231 objc_set_method_opt (true);
7232 c_parser_consume_token (parser);
7234 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7236 objc_set_method_opt (false);
7237 c_parser_consume_token (parser);
7239 else
7240 c_parser_declaration_or_fndef (parser, false, false, true,
7241 false, true, NULL);
7242 break;
7247 /* Parse an objc-methodproto.
7249 objc-methodproto:
7250 objc-method-type objc-method-decl ;
7253 static void
7254 c_parser_objc_methodproto (c_parser *parser)
7256 bool is_class_method = c_parser_objc_method_type (parser);
7257 tree decl, attributes = NULL_TREE;
7259 /* Remember protocol qualifiers in prototypes. */
7260 parser->objc_pq_context = true;
7261 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7262 /* Forget protocol qualifiers now. */
7263 parser->objc_pq_context = false;
7265 /* Do not allow the presence of attributes to hide an erroneous
7266 method implementation in the interface section. */
7267 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7269 c_parser_error (parser, "expected %<;%>");
7270 return;
7273 if (decl != error_mark_node)
7274 objc_add_method_declaration (is_class_method, decl, attributes);
7276 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7279 /* If we are at a position that method attributes may be present, check that
7280 there are not any parsed already (a syntax error) and then collect any
7281 specified at the current location. Finally, if new attributes were present,
7282 check that the next token is legal ( ';' for decls and '{' for defs). */
7284 static bool
7285 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7287 bool bad = false;
7288 if (*attributes)
7290 c_parser_error (parser,
7291 "method attributes must be specified at the end only");
7292 *attributes = NULL_TREE;
7293 bad = true;
7296 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7297 *attributes = c_parser_attributes (parser);
7299 /* If there were no attributes here, just report any earlier error. */
7300 if (*attributes == NULL_TREE || bad)
7301 return bad;
7303 /* If the attributes are followed by a ; or {, then just report any earlier
7304 error. */
7305 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7306 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7307 return bad;
7309 /* We've got attributes, but not at the end. */
7310 c_parser_error (parser,
7311 "expected %<;%> or %<{%> after method attribute definition");
7312 return true;
7315 /* Parse an objc-method-decl.
7317 objc-method-decl:
7318 ( objc-type-name ) objc-selector
7319 objc-selector
7320 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7321 objc-keyword-selector objc-optparmlist
7322 attributes
7324 objc-keyword-selector:
7325 objc-keyword-decl
7326 objc-keyword-selector objc-keyword-decl
7328 objc-keyword-decl:
7329 objc-selector : ( objc-type-name ) identifier
7330 objc-selector : identifier
7331 : ( objc-type-name ) identifier
7332 : identifier
7334 objc-optparmlist:
7335 objc-optparms objc-optellipsis
7337 objc-optparms:
7338 empty
7339 objc-opt-parms , parameter-declaration
7341 objc-optellipsis:
7342 empty
7343 , ...
7346 static tree
7347 c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
7349 tree type = NULL_TREE;
7350 tree sel;
7351 tree parms = NULL_TREE;
7352 bool ellipsis = false;
7353 bool attr_err = false;
7355 *attributes = NULL_TREE;
7356 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7358 c_parser_consume_token (parser);
7359 type = c_parser_objc_type_name (parser);
7360 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7362 sel = c_parser_objc_selector (parser);
7363 /* If there is no selector, or a colon follows, we have an
7364 objc-keyword-selector. If there is a selector, and a colon does
7365 not follow, that selector ends the objc-method-decl. */
7366 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7368 tree tsel = sel;
7369 tree list = NULL_TREE;
7370 while (true)
7372 tree atype = NULL_TREE, id, keyworddecl;
7373 tree param_attr = NULL_TREE;
7374 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7375 break;
7376 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7378 c_parser_consume_token (parser);
7379 atype = c_parser_objc_type_name (parser);
7380 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7381 "expected %<)%>");
7383 /* New ObjC allows attributes on method parameters. */
7384 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7385 param_attr = c_parser_attributes (parser);
7386 if (c_parser_next_token_is_not (parser, CPP_NAME))
7388 c_parser_error (parser, "expected identifier");
7389 return error_mark_node;
7391 id = c_parser_peek_token (parser)->value;
7392 c_parser_consume_token (parser);
7393 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7394 list = chainon (list, keyworddecl);
7395 tsel = c_parser_objc_selector (parser);
7396 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7397 break;
7400 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7402 /* Parse the optional parameter list. Optional Objective-C
7403 method parameters follow the C syntax, and may include '...'
7404 to denote a variable number of arguments. */
7405 parms = make_node (TREE_LIST);
7406 while (c_parser_next_token_is (parser, CPP_COMMA))
7408 struct c_parm *parm;
7409 c_parser_consume_token (parser);
7410 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7412 ellipsis = true;
7413 c_parser_consume_token (parser);
7414 attr_err |= c_parser_objc_maybe_method_attributes
7415 (parser, attributes) ;
7416 break;
7418 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7419 if (parm == NULL)
7420 break;
7421 parms = chainon (parms,
7422 build_tree_list (NULL_TREE, grokparm (parm)));
7424 sel = list;
7426 else
7427 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7429 if (sel == NULL)
7431 c_parser_error (parser, "objective-c method declaration is expected");
7432 return error_mark_node;
7435 if (attr_err)
7436 return error_mark_node;
7438 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7441 /* Parse an objc-type-name.
7443 objc-type-name:
7444 objc-type-qualifiers[opt] type-name
7445 objc-type-qualifiers[opt]
7447 objc-type-qualifiers:
7448 objc-type-qualifier
7449 objc-type-qualifiers objc-type-qualifier
7451 objc-type-qualifier: one of
7452 in out inout bycopy byref oneway
7455 static tree
7456 c_parser_objc_type_name (c_parser *parser)
7458 tree quals = NULL_TREE;
7459 struct c_type_name *type_name = NULL;
7460 tree type = NULL_TREE;
7461 while (true)
7463 c_token *token = c_parser_peek_token (parser);
7464 if (token->type == CPP_KEYWORD
7465 && (token->keyword == RID_IN
7466 || token->keyword == RID_OUT
7467 || token->keyword == RID_INOUT
7468 || token->keyword == RID_BYCOPY
7469 || token->keyword == RID_BYREF
7470 || token->keyword == RID_ONEWAY))
7472 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7473 c_parser_consume_token (parser);
7475 else
7476 break;
7478 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7479 type_name = c_parser_type_name (parser);
7480 if (type_name)
7481 type = groktypename (type_name, NULL, NULL);
7483 /* If the type is unknown, and error has already been produced and
7484 we need to recover from the error. In that case, use NULL_TREE
7485 for the type, as if no type had been specified; this will use the
7486 default type ('id') which is good for error recovery. */
7487 if (type == error_mark_node)
7488 type = NULL_TREE;
7490 return build_tree_list (quals, type);
7493 /* Parse objc-protocol-refs.
7495 objc-protocol-refs:
7496 < identifier-list >
7499 static tree
7500 c_parser_objc_protocol_refs (c_parser *parser)
7502 tree list = NULL_TREE;
7503 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7504 c_parser_consume_token (parser);
7505 /* Any identifiers, including those declared as type names, are OK
7506 here. */
7507 while (true)
7509 tree id;
7510 if (c_parser_next_token_is_not (parser, CPP_NAME))
7512 c_parser_error (parser, "expected identifier");
7513 break;
7515 id = c_parser_peek_token (parser)->value;
7516 list = chainon (list, build_tree_list (NULL_TREE, id));
7517 c_parser_consume_token (parser);
7518 if (c_parser_next_token_is (parser, CPP_COMMA))
7519 c_parser_consume_token (parser);
7520 else
7521 break;
7523 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7524 return list;
7527 /* Parse an objc-try-catch-finally-statement.
7529 objc-try-catch-finally-statement:
7530 @try compound-statement objc-catch-list[opt]
7531 @try compound-statement objc-catch-list[opt] @finally compound-statement
7533 objc-catch-list:
7534 @catch ( objc-catch-parameter-declaration ) compound-statement
7535 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7537 objc-catch-parameter-declaration:
7538 parameter-declaration
7539 '...'
7541 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7543 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7544 for C++. Keep them in sync. */
7546 static void
7547 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7549 location_t location;
7550 tree stmt;
7552 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7553 c_parser_consume_token (parser);
7554 location = c_parser_peek_token (parser)->location;
7555 objc_maybe_warn_exceptions (location);
7556 stmt = c_parser_compound_statement (parser);
7557 objc_begin_try_stmt (location, stmt);
7559 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7561 struct c_parm *parm;
7562 tree parameter_declaration = error_mark_node;
7563 bool seen_open_paren = false;
7565 c_parser_consume_token (parser);
7566 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7567 seen_open_paren = true;
7568 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7570 /* We have "@catch (...)" (where the '...' are literally
7571 what is in the code). Skip the '...'.
7572 parameter_declaration is set to NULL_TREE, and
7573 objc_being_catch_clauses() knows that that means
7574 '...'. */
7575 c_parser_consume_token (parser);
7576 parameter_declaration = NULL_TREE;
7578 else
7580 /* We have "@catch (NSException *exception)" or something
7581 like that. Parse the parameter declaration. */
7582 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7583 if (parm == NULL)
7584 parameter_declaration = error_mark_node;
7585 else
7586 parameter_declaration = grokparm (parm);
7588 if (seen_open_paren)
7589 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7590 else
7592 /* If there was no open parenthesis, we are recovering from
7593 an error, and we are trying to figure out what mistake
7594 the user has made. */
7596 /* If there is an immediate closing parenthesis, the user
7597 probably forgot the opening one (ie, they typed "@catch
7598 NSException *e)". Parse the closing parenthesis and keep
7599 going. */
7600 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7601 c_parser_consume_token (parser);
7603 /* If these is no immediate closing parenthesis, the user
7604 probably doesn't know that parenthesis are required at
7605 all (ie, they typed "@catch NSException *e"). So, just
7606 forget about the closing parenthesis and keep going. */
7608 objc_begin_catch_clause (parameter_declaration);
7609 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7610 c_parser_compound_statement_nostart (parser);
7611 objc_finish_catch_clause ();
7613 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7615 c_parser_consume_token (parser);
7616 location = c_parser_peek_token (parser)->location;
7617 stmt = c_parser_compound_statement (parser);
7618 objc_build_finally_clause (location, stmt);
7620 objc_finish_try_stmt ();
7623 /* Parse an objc-synchronized-statement.
7625 objc-synchronized-statement:
7626 @synchronized ( expression ) compound-statement
7629 static void
7630 c_parser_objc_synchronized_statement (c_parser *parser)
7632 location_t loc;
7633 tree expr, stmt;
7634 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7635 c_parser_consume_token (parser);
7636 loc = c_parser_peek_token (parser)->location;
7637 objc_maybe_warn_exceptions (loc);
7638 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7640 expr = c_parser_expression (parser).value;
7641 expr = c_fully_fold (expr, false, NULL);
7642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7644 else
7645 expr = error_mark_node;
7646 stmt = c_parser_compound_statement (parser);
7647 objc_build_synchronized (loc, expr, stmt);
7650 /* Parse an objc-selector; return NULL_TREE without an error if the
7651 next token is not an objc-selector.
7653 objc-selector:
7654 identifier
7655 one of
7656 enum struct union if else while do for switch case default
7657 break continue return goto asm sizeof typeof __alignof
7658 unsigned long const short volatile signed restrict _Complex
7659 in out inout bycopy byref oneway int char float double void _Bool
7661 ??? Why this selection of keywords but not, for example, storage
7662 class specifiers? */
7664 static tree
7665 c_parser_objc_selector (c_parser *parser)
7667 c_token *token = c_parser_peek_token (parser);
7668 tree value = token->value;
7669 if (token->type == CPP_NAME)
7671 c_parser_consume_token (parser);
7672 return value;
7674 if (token->type != CPP_KEYWORD)
7675 return NULL_TREE;
7676 switch (token->keyword)
7678 case RID_ENUM:
7679 case RID_STRUCT:
7680 case RID_UNION:
7681 case RID_IF:
7682 case RID_ELSE:
7683 case RID_WHILE:
7684 case RID_DO:
7685 case RID_FOR:
7686 case RID_SWITCH:
7687 case RID_CASE:
7688 case RID_DEFAULT:
7689 case RID_BREAK:
7690 case RID_CONTINUE:
7691 case RID_RETURN:
7692 case RID_GOTO:
7693 case RID_ASM:
7694 case RID_SIZEOF:
7695 case RID_TYPEOF:
7696 case RID_ALIGNOF:
7697 case RID_UNSIGNED:
7698 case RID_LONG:
7699 case RID_INT128:
7700 case RID_CONST:
7701 case RID_SHORT:
7702 case RID_VOLATILE:
7703 case RID_SIGNED:
7704 case RID_RESTRICT:
7705 case RID_COMPLEX:
7706 case RID_IN:
7707 case RID_OUT:
7708 case RID_INOUT:
7709 case RID_BYCOPY:
7710 case RID_BYREF:
7711 case RID_ONEWAY:
7712 case RID_INT:
7713 case RID_CHAR:
7714 case RID_FLOAT:
7715 case RID_DOUBLE:
7716 case RID_VOID:
7717 case RID_BOOL:
7718 c_parser_consume_token (parser);
7719 return value;
7720 default:
7721 return NULL_TREE;
7725 /* Parse an objc-selector-arg.
7727 objc-selector-arg:
7728 objc-selector
7729 objc-keywordname-list
7731 objc-keywordname-list:
7732 objc-keywordname
7733 objc-keywordname-list objc-keywordname
7735 objc-keywordname:
7736 objc-selector :
7740 static tree
7741 c_parser_objc_selector_arg (c_parser *parser)
7743 tree sel = c_parser_objc_selector (parser);
7744 tree list = NULL_TREE;
7745 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7746 return sel;
7747 while (true)
7749 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7750 return list;
7751 list = chainon (list, build_tree_list (sel, NULL_TREE));
7752 sel = c_parser_objc_selector (parser);
7753 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7754 break;
7756 return list;
7759 /* Parse an objc-receiver.
7761 objc-receiver:
7762 expression
7763 class-name
7764 type-name
7767 static tree
7768 c_parser_objc_receiver (c_parser *parser)
7770 if (c_parser_peek_token (parser)->type == CPP_NAME
7771 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7772 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7774 tree id = c_parser_peek_token (parser)->value;
7775 c_parser_consume_token (parser);
7776 return objc_get_class_reference (id);
7778 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7781 /* Parse objc-message-args.
7783 objc-message-args:
7784 objc-selector
7785 objc-keywordarg-list
7787 objc-keywordarg-list:
7788 objc-keywordarg
7789 objc-keywordarg-list objc-keywordarg
7791 objc-keywordarg:
7792 objc-selector : objc-keywordexpr
7793 : objc-keywordexpr
7796 static tree
7797 c_parser_objc_message_args (c_parser *parser)
7799 tree sel = c_parser_objc_selector (parser);
7800 tree list = NULL_TREE;
7801 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7802 return sel;
7803 while (true)
7805 tree keywordexpr;
7806 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7807 return error_mark_node;
7808 keywordexpr = c_parser_objc_keywordexpr (parser);
7809 list = chainon (list, build_tree_list (sel, keywordexpr));
7810 sel = c_parser_objc_selector (parser);
7811 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7812 break;
7814 return list;
7817 /* Parse an objc-keywordexpr.
7819 objc-keywordexpr:
7820 nonempty-expr-list
7823 static tree
7824 c_parser_objc_keywordexpr (c_parser *parser)
7826 tree ret;
7827 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7828 if (VEC_length (tree, expr_list) == 1)
7830 /* Just return the expression, remove a level of
7831 indirection. */
7832 ret = VEC_index (tree, expr_list, 0);
7834 else
7836 /* We have a comma expression, we will collapse later. */
7837 ret = build_tree_list_vec (expr_list);
7839 release_tree_vector (expr_list);
7840 return ret;
7843 /* A check, needed in several places, that ObjC interface, implementation or
7844 method definitions are not prefixed by incorrect items. */
7845 static bool
7846 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7847 struct c_declspecs *specs)
7849 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7850 || specs->typespec_kind != ctsk_none)
7852 c_parser_error (parser,
7853 "no type or storage class may be specified here,");
7854 c_parser_skip_to_end_of_block_or_statement (parser);
7855 return true;
7857 return false;
7860 /* Parse an Objective-C @property declaration. The syntax is:
7862 objc-property-declaration:
7863 '@property' objc-property-attributes[opt] struct-declaration ;
7865 objc-property-attributes:
7866 '(' objc-property-attribute-list ')'
7868 objc-property-attribute-list:
7869 objc-property-attribute
7870 objc-property-attribute-list, objc-property-attribute
7872 objc-property-attribute
7873 'getter' = identifier
7874 'setter' = identifier
7875 'readonly'
7876 'readwrite'
7877 'assign'
7878 'retain'
7879 'copy'
7880 'nonatomic'
7882 For example:
7883 @property NSString *name;
7884 @property (readonly) id object;
7885 @property (retain, nonatomic, getter=getTheName) id name;
7886 @property int a, b, c;
7888 PS: This function is identical to cp_parser_objc_at_propery_declaration
7889 for C++. Keep them in sync. */
7890 static void
7891 c_parser_objc_at_property_declaration (c_parser *parser)
7893 /* The following variables hold the attributes of the properties as
7894 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7895 seen. When we see an attribute, we set them to 'true' (if they
7896 are boolean properties) or to the identifier (if they have an
7897 argument, ie, for getter and setter). Note that here we only
7898 parse the list of attributes, check the syntax and accumulate the
7899 attributes that we find. objc_add_property_declaration() will
7900 then process the information. */
7901 bool property_assign = false;
7902 bool property_copy = false;
7903 tree property_getter_ident = NULL_TREE;
7904 bool property_nonatomic = false;
7905 bool property_readonly = false;
7906 bool property_readwrite = false;
7907 bool property_retain = false;
7908 tree property_setter_ident = NULL_TREE;
7910 /* 'properties' is the list of properties that we read. Usually a
7911 single one, but maybe more (eg, in "@property int a, b, c;" there
7912 are three). */
7913 tree properties;
7914 location_t loc;
7916 loc = c_parser_peek_token (parser)->location;
7917 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7919 c_parser_consume_token (parser); /* Eat '@property'. */
7921 /* Parse the optional attribute list... */
7922 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7924 /* Eat the '(' */
7925 c_parser_consume_token (parser);
7927 /* Property attribute keywords are valid now. */
7928 parser->objc_property_attr_context = true;
7930 while (true)
7932 bool syntax_error = false;
7933 c_token *token = c_parser_peek_token (parser);
7934 enum rid keyword;
7936 if (token->type != CPP_KEYWORD)
7938 if (token->type == CPP_CLOSE_PAREN)
7939 c_parser_error (parser, "expected identifier");
7940 else
7942 c_parser_consume_token (parser);
7943 c_parser_error (parser, "unknown property attribute");
7945 break;
7947 keyword = token->keyword;
7948 c_parser_consume_token (parser);
7949 switch (keyword)
7951 case RID_ASSIGN: property_assign = true; break;
7952 case RID_COPY: property_copy = true; break;
7953 case RID_NONATOMIC: property_nonatomic = true; break;
7954 case RID_READONLY: property_readonly = true; break;
7955 case RID_READWRITE: property_readwrite = true; break;
7956 case RID_RETAIN: property_retain = true; break;
7958 case RID_GETTER:
7959 case RID_SETTER:
7960 if (c_parser_next_token_is_not (parser, CPP_EQ))
7962 if (keyword == RID_GETTER)
7963 c_parser_error (parser,
7964 "missing %<=%> (after %<getter%> attribute)");
7965 else
7966 c_parser_error (parser,
7967 "missing %<=%> (after %<setter%> attribute)");
7968 syntax_error = true;
7969 break;
7971 c_parser_consume_token (parser); /* eat the = */
7972 if (c_parser_next_token_is_not (parser, CPP_NAME))
7974 c_parser_error (parser, "expected identifier");
7975 syntax_error = true;
7976 break;
7978 if (keyword == RID_SETTER)
7980 if (property_setter_ident != NULL_TREE)
7981 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7982 else
7983 property_setter_ident = c_parser_peek_token (parser)->value;
7984 c_parser_consume_token (parser);
7985 if (c_parser_next_token_is_not (parser, CPP_COLON))
7986 c_parser_error (parser, "setter name must terminate with %<:%>");
7987 else
7988 c_parser_consume_token (parser);
7990 else
7992 if (property_getter_ident != NULL_TREE)
7993 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
7994 else
7995 property_getter_ident = c_parser_peek_token (parser)->value;
7996 c_parser_consume_token (parser);
7998 break;
7999 default:
8000 c_parser_error (parser, "unknown property attribute");
8001 syntax_error = true;
8002 break;
8005 if (syntax_error)
8006 break;
8008 if (c_parser_next_token_is (parser, CPP_COMMA))
8009 c_parser_consume_token (parser);
8010 else
8011 break;
8013 parser->objc_property_attr_context = false;
8014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8016 /* ... and the property declaration(s). */
8017 properties = c_parser_struct_declaration (parser);
8019 if (properties == error_mark_node)
8021 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8022 parser->error = false;
8023 return;
8026 if (properties == NULL_TREE)
8027 c_parser_error (parser, "expected identifier");
8028 else
8030 /* Comma-separated properties are chained together in
8031 reverse order; add them one by one. */
8032 properties = nreverse (properties);
8034 for (; properties; properties = TREE_CHAIN (properties))
8035 objc_add_property_declaration (loc, copy_node (properties),
8036 property_readonly, property_readwrite,
8037 property_assign, property_retain,
8038 property_copy, property_nonatomic,
8039 property_getter_ident, property_setter_ident);
8042 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8043 parser->error = false;
8046 /* Parse an Objective-C @synthesize declaration. The syntax is:
8048 objc-synthesize-declaration:
8049 @synthesize objc-synthesize-identifier-list ;
8051 objc-synthesize-identifier-list:
8052 objc-synthesize-identifier
8053 objc-synthesize-identifier-list, objc-synthesize-identifier
8055 objc-synthesize-identifier
8056 identifier
8057 identifier = identifier
8059 For example:
8060 @synthesize MyProperty;
8061 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8063 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8064 for C++. Keep them in sync.
8066 static void
8067 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8069 tree list = NULL_TREE;
8070 location_t loc;
8071 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8072 loc = c_parser_peek_token (parser)->location;
8074 c_parser_consume_token (parser);
8075 while (true)
8077 tree property, ivar;
8078 if (c_parser_next_token_is_not (parser, CPP_NAME))
8080 c_parser_error (parser, "expected identifier");
8081 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8082 /* Once we find the semicolon, we can resume normal parsing.
8083 We have to reset parser->error manually because
8084 c_parser_skip_until_found() won't reset it for us if the
8085 next token is precisely a semicolon. */
8086 parser->error = false;
8087 return;
8089 property = c_parser_peek_token (parser)->value;
8090 c_parser_consume_token (parser);
8091 if (c_parser_next_token_is (parser, CPP_EQ))
8093 c_parser_consume_token (parser);
8094 if (c_parser_next_token_is_not (parser, CPP_NAME))
8096 c_parser_error (parser, "expected identifier");
8097 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8098 parser->error = false;
8099 return;
8101 ivar = c_parser_peek_token (parser)->value;
8102 c_parser_consume_token (parser);
8104 else
8105 ivar = NULL_TREE;
8106 list = chainon (list, build_tree_list (ivar, property));
8107 if (c_parser_next_token_is (parser, CPP_COMMA))
8108 c_parser_consume_token (parser);
8109 else
8110 break;
8112 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8113 objc_add_synthesize_declaration (loc, list);
8116 /* Parse an Objective-C @dynamic declaration. The syntax is:
8118 objc-dynamic-declaration:
8119 @dynamic identifier-list ;
8121 For example:
8122 @dynamic MyProperty;
8123 @dynamic MyProperty, AnotherProperty;
8125 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8126 for C++. Keep them in sync.
8128 static void
8129 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8131 tree list = NULL_TREE;
8132 location_t loc;
8133 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8134 loc = c_parser_peek_token (parser)->location;
8136 c_parser_consume_token (parser);
8137 while (true)
8139 tree property;
8140 if (c_parser_next_token_is_not (parser, CPP_NAME))
8142 c_parser_error (parser, "expected identifier");
8143 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8144 parser->error = false;
8145 return;
8147 property = c_parser_peek_token (parser)->value;
8148 list = chainon (list, build_tree_list (NULL_TREE, property));
8149 c_parser_consume_token (parser);
8150 if (c_parser_next_token_is (parser, CPP_COMMA))
8151 c_parser_consume_token (parser);
8152 else
8153 break;
8155 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8156 objc_add_dynamic_declaration (loc, list);
8160 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8161 should be considered, statements. ALLOW_STMT is true if we're within
8162 the context of a function and such pragmas are to be allowed. Returns
8163 true if we actually parsed such a pragma. */
8165 static bool
8166 c_parser_pragma (c_parser *parser, enum pragma_context context)
8168 unsigned int id;
8170 id = c_parser_peek_token (parser)->pragma_kind;
8171 gcc_assert (id != PRAGMA_NONE);
8173 switch (id)
8175 case PRAGMA_OMP_BARRIER:
8176 if (context != pragma_compound)
8178 if (context == pragma_stmt)
8179 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8180 "used in compound statements");
8181 goto bad_stmt;
8183 c_parser_omp_barrier (parser);
8184 return false;
8186 case PRAGMA_OMP_FLUSH:
8187 if (context != pragma_compound)
8189 if (context == pragma_stmt)
8190 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8191 "used in compound statements");
8192 goto bad_stmt;
8194 c_parser_omp_flush (parser);
8195 return false;
8197 case PRAGMA_OMP_TASKWAIT:
8198 if (context != pragma_compound)
8200 if (context == pragma_stmt)
8201 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8202 "used in compound statements");
8203 goto bad_stmt;
8205 c_parser_omp_taskwait (parser);
8206 return false;
8208 case PRAGMA_OMP_THREADPRIVATE:
8209 c_parser_omp_threadprivate (parser);
8210 return false;
8212 case PRAGMA_OMP_SECTION:
8213 error_at (c_parser_peek_token (parser)->location,
8214 "%<#pragma omp section%> may only be used in "
8215 "%<#pragma omp sections%> construct");
8216 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8217 return false;
8219 case PRAGMA_GCC_PCH_PREPROCESS:
8220 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8221 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8222 return false;
8224 default:
8225 if (id < PRAGMA_FIRST_EXTERNAL)
8227 if (context == pragma_external)
8229 bad_stmt:
8230 c_parser_error (parser, "expected declaration specifiers");
8231 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8232 return false;
8234 c_parser_omp_construct (parser);
8235 return true;
8237 break;
8240 c_parser_consume_pragma (parser);
8241 c_invoke_pragma_handler (id);
8243 /* Skip to EOL, but suppress any error message. Those will have been
8244 generated by the handler routine through calling error, as opposed
8245 to calling c_parser_error. */
8246 parser->error = true;
8247 c_parser_skip_to_pragma_eol (parser);
8249 return false;
8252 /* The interface the pragma parsers have to the lexer. */
8254 enum cpp_ttype
8255 pragma_lex (tree *value)
8257 c_token *tok = c_parser_peek_token (the_parser);
8258 enum cpp_ttype ret = tok->type;
8260 *value = tok->value;
8261 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8262 ret = CPP_EOF;
8263 else
8265 if (ret == CPP_KEYWORD)
8266 ret = CPP_NAME;
8267 c_parser_consume_token (the_parser);
8270 return ret;
8273 static void
8274 c_parser_pragma_pch_preprocess (c_parser *parser)
8276 tree name = NULL;
8278 c_parser_consume_pragma (parser);
8279 if (c_parser_next_token_is (parser, CPP_STRING))
8281 name = c_parser_peek_token (parser)->value;
8282 c_parser_consume_token (parser);
8284 else
8285 c_parser_error (parser, "expected string literal");
8286 c_parser_skip_to_pragma_eol (parser);
8288 if (name)
8289 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8292 /* OpenMP 2.5 parsing routines. */
8294 /* Returns name of the next clause.
8295 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8296 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8297 returned and the token is consumed. */
8299 static pragma_omp_clause
8300 c_parser_omp_clause_name (c_parser *parser)
8302 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8304 if (c_parser_next_token_is_keyword (parser, RID_IF))
8305 result = PRAGMA_OMP_CLAUSE_IF;
8306 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8307 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8308 else if (c_parser_next_token_is (parser, CPP_NAME))
8310 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8312 switch (p[0])
8314 case 'c':
8315 if (!strcmp ("collapse", p))
8316 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8317 else if (!strcmp ("copyin", p))
8318 result = PRAGMA_OMP_CLAUSE_COPYIN;
8319 else if (!strcmp ("copyprivate", p))
8320 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8321 break;
8322 case 'f':
8323 if (!strcmp ("firstprivate", p))
8324 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8325 break;
8326 case 'l':
8327 if (!strcmp ("lastprivate", p))
8328 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8329 break;
8330 case 'n':
8331 if (!strcmp ("nowait", p))
8332 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8333 else if (!strcmp ("num_threads", p))
8334 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8335 break;
8336 case 'o':
8337 if (!strcmp ("ordered", p))
8338 result = PRAGMA_OMP_CLAUSE_ORDERED;
8339 break;
8340 case 'p':
8341 if (!strcmp ("private", p))
8342 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8343 break;
8344 case 'r':
8345 if (!strcmp ("reduction", p))
8346 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8347 break;
8348 case 's':
8349 if (!strcmp ("schedule", p))
8350 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8351 else if (!strcmp ("shared", p))
8352 result = PRAGMA_OMP_CLAUSE_SHARED;
8353 break;
8354 case 'u':
8355 if (!strcmp ("untied", p))
8356 result = PRAGMA_OMP_CLAUSE_UNTIED;
8357 break;
8361 if (result != PRAGMA_OMP_CLAUSE_NONE)
8362 c_parser_consume_token (parser);
8364 return result;
8367 /* Validate that a clause of the given type does not already exist. */
8369 static void
8370 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8371 const char *name)
8373 tree c;
8375 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8376 if (OMP_CLAUSE_CODE (c) == code)
8378 location_t loc = OMP_CLAUSE_LOCATION (c);
8379 error_at (loc, "too many %qs clauses", name);
8380 break;
8384 /* OpenMP 2.5:
8385 variable-list:
8386 identifier
8387 variable-list , identifier
8389 If KIND is nonzero, create the appropriate node and install the
8390 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8391 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8393 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8394 return the list created. */
8396 static tree
8397 c_parser_omp_variable_list (c_parser *parser,
8398 location_t clause_loc,
8399 enum omp_clause_code kind,
8400 tree list)
8402 if (c_parser_next_token_is_not (parser, CPP_NAME)
8403 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8404 c_parser_error (parser, "expected identifier");
8406 while (c_parser_next_token_is (parser, CPP_NAME)
8407 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8409 tree t = lookup_name (c_parser_peek_token (parser)->value);
8411 if (t == NULL_TREE)
8412 undeclared_variable (c_parser_peek_token (parser)->location,
8413 c_parser_peek_token (parser)->value);
8414 else if (t == error_mark_node)
8416 else if (kind != 0)
8418 tree u = build_omp_clause (clause_loc, kind);
8419 OMP_CLAUSE_DECL (u) = t;
8420 OMP_CLAUSE_CHAIN (u) = list;
8421 list = u;
8423 else
8424 list = tree_cons (t, NULL_TREE, list);
8426 c_parser_consume_token (parser);
8428 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8429 break;
8431 c_parser_consume_token (parser);
8434 return list;
8437 /* Similarly, but expect leading and trailing parenthesis. This is a very
8438 common case for omp clauses. */
8440 static tree
8441 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8442 tree list)
8444 /* The clauses location. */
8445 location_t loc = c_parser_peek_token (parser)->location;
8447 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8449 list = c_parser_omp_variable_list (parser, loc, kind, list);
8450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8452 return list;
8455 /* OpenMP 3.0:
8456 collapse ( constant-expression ) */
8458 static tree
8459 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8461 tree c, num = error_mark_node;
8462 HOST_WIDE_INT n;
8463 location_t loc;
8465 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8467 loc = c_parser_peek_token (parser)->location;
8468 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8470 num = c_parser_expr_no_commas (parser, NULL).value;
8471 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8473 if (num == error_mark_node)
8474 return list;
8475 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8476 || !host_integerp (num, 0)
8477 || (n = tree_low_cst (num, 0)) <= 0
8478 || (int) n != n)
8480 error_at (loc,
8481 "collapse argument needs positive constant integer expression");
8482 return list;
8484 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8485 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8486 OMP_CLAUSE_CHAIN (c) = list;
8487 return c;
8490 /* OpenMP 2.5:
8491 copyin ( variable-list ) */
8493 static tree
8494 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8496 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8499 /* OpenMP 2.5:
8500 copyprivate ( variable-list ) */
8502 static tree
8503 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8505 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8508 /* OpenMP 2.5:
8509 default ( shared | none ) */
8511 static tree
8512 c_parser_omp_clause_default (c_parser *parser, tree list)
8514 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8515 location_t loc = c_parser_peek_token (parser)->location;
8516 tree c;
8518 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8519 return list;
8520 if (c_parser_next_token_is (parser, CPP_NAME))
8522 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8524 switch (p[0])
8526 case 'n':
8527 if (strcmp ("none", p) != 0)
8528 goto invalid_kind;
8529 kind = OMP_CLAUSE_DEFAULT_NONE;
8530 break;
8532 case 's':
8533 if (strcmp ("shared", p) != 0)
8534 goto invalid_kind;
8535 kind = OMP_CLAUSE_DEFAULT_SHARED;
8536 break;
8538 default:
8539 goto invalid_kind;
8542 c_parser_consume_token (parser);
8544 else
8546 invalid_kind:
8547 c_parser_error (parser, "expected %<none%> or %<shared%>");
8549 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8551 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8552 return list;
8554 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8555 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8556 OMP_CLAUSE_CHAIN (c) = list;
8557 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8559 return c;
8562 /* OpenMP 2.5:
8563 firstprivate ( variable-list ) */
8565 static tree
8566 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8568 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8571 /* OpenMP 2.5:
8572 if ( expression ) */
8574 static tree
8575 c_parser_omp_clause_if (c_parser *parser, tree list)
8577 location_t loc = c_parser_peek_token (parser)->location;
8578 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8580 tree t = c_parser_paren_condition (parser);
8581 tree c;
8583 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8585 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8586 OMP_CLAUSE_IF_EXPR (c) = t;
8587 OMP_CLAUSE_CHAIN (c) = list;
8588 list = c;
8590 else
8591 c_parser_error (parser, "expected %<(%>");
8593 return list;
8596 /* OpenMP 2.5:
8597 lastprivate ( variable-list ) */
8599 static tree
8600 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8602 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8605 /* OpenMP 2.5:
8606 nowait */
8608 static tree
8609 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8611 tree c;
8612 location_t loc = c_parser_peek_token (parser)->location;
8614 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8616 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8617 OMP_CLAUSE_CHAIN (c) = list;
8618 return c;
8621 /* OpenMP 2.5:
8622 num_threads ( expression ) */
8624 static tree
8625 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8627 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8628 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8630 location_t expr_loc = c_parser_peek_token (parser)->location;
8631 tree c, t = c_parser_expression (parser).value;
8632 t = c_fully_fold (t, false, NULL);
8634 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8636 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8638 c_parser_error (parser, "expected integer expression");
8639 return list;
8642 /* Attempt to statically determine when the number isn't positive. */
8643 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8644 build_int_cst (TREE_TYPE (t), 0));
8645 if (CAN_HAVE_LOCATION_P (c))
8646 SET_EXPR_LOCATION (c, expr_loc);
8647 if (c == boolean_true_node)
8649 warning_at (expr_loc, 0,
8650 "%<num_threads%> value must be positive");
8651 t = integer_one_node;
8654 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8656 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8657 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8658 OMP_CLAUSE_CHAIN (c) = list;
8659 list = c;
8662 return list;
8665 /* OpenMP 2.5:
8666 ordered */
8668 static tree
8669 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8671 tree c;
8673 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8675 c = build_omp_clause (c_parser_peek_token (parser)->location,
8676 OMP_CLAUSE_ORDERED);
8677 OMP_CLAUSE_CHAIN (c) = list;
8679 return c;
8682 /* OpenMP 2.5:
8683 private ( variable-list ) */
8685 static tree
8686 c_parser_omp_clause_private (c_parser *parser, tree list)
8688 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8691 /* OpenMP 2.5:
8692 reduction ( reduction-operator : variable-list )
8694 reduction-operator:
8695 One of: + * - & ^ | && || */
8697 static tree
8698 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8700 location_t clause_loc = c_parser_peek_token (parser)->location;
8701 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8703 enum tree_code code;
8705 switch (c_parser_peek_token (parser)->type)
8707 case CPP_PLUS:
8708 code = PLUS_EXPR;
8709 break;
8710 case CPP_MULT:
8711 code = MULT_EXPR;
8712 break;
8713 case CPP_MINUS:
8714 code = MINUS_EXPR;
8715 break;
8716 case CPP_AND:
8717 code = BIT_AND_EXPR;
8718 break;
8719 case CPP_XOR:
8720 code = BIT_XOR_EXPR;
8721 break;
8722 case CPP_OR:
8723 code = BIT_IOR_EXPR;
8724 break;
8725 case CPP_AND_AND:
8726 code = TRUTH_ANDIF_EXPR;
8727 break;
8728 case CPP_OR_OR:
8729 code = TRUTH_ORIF_EXPR;
8730 break;
8731 default:
8732 c_parser_error (parser,
8733 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8734 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8736 return list;
8738 c_parser_consume_token (parser);
8739 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8741 tree nl, c;
8743 nl = c_parser_omp_variable_list (parser, clause_loc,
8744 OMP_CLAUSE_REDUCTION, list);
8745 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8746 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8748 list = nl;
8750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8752 return list;
8755 /* OpenMP 2.5:
8756 schedule ( schedule-kind )
8757 schedule ( schedule-kind , expression )
8759 schedule-kind:
8760 static | dynamic | guided | runtime | auto
8763 static tree
8764 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8766 tree c, t;
8767 location_t loc = c_parser_peek_token (parser)->location;
8769 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8770 return list;
8772 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8774 if (c_parser_next_token_is (parser, CPP_NAME))
8776 tree kind = c_parser_peek_token (parser)->value;
8777 const char *p = IDENTIFIER_POINTER (kind);
8779 switch (p[0])
8781 case 'd':
8782 if (strcmp ("dynamic", p) != 0)
8783 goto invalid_kind;
8784 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8785 break;
8787 case 'g':
8788 if (strcmp ("guided", p) != 0)
8789 goto invalid_kind;
8790 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8791 break;
8793 case 'r':
8794 if (strcmp ("runtime", p) != 0)
8795 goto invalid_kind;
8796 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8797 break;
8799 default:
8800 goto invalid_kind;
8803 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8804 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8805 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8806 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8807 else
8808 goto invalid_kind;
8810 c_parser_consume_token (parser);
8811 if (c_parser_next_token_is (parser, CPP_COMMA))
8813 location_t here;
8814 c_parser_consume_token (parser);
8816 here = c_parser_peek_token (parser)->location;
8817 t = c_parser_expr_no_commas (parser, NULL).value;
8818 t = c_fully_fold (t, false, NULL);
8820 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8821 error_at (here, "schedule %<runtime%> does not take "
8822 "a %<chunk_size%> parameter");
8823 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8824 error_at (here,
8825 "schedule %<auto%> does not take "
8826 "a %<chunk_size%> parameter");
8827 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8828 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8829 else
8830 c_parser_error (parser, "expected integer expression");
8832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8834 else
8835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8836 "expected %<,%> or %<)%>");
8838 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8839 OMP_CLAUSE_CHAIN (c) = list;
8840 return c;
8842 invalid_kind:
8843 c_parser_error (parser, "invalid schedule kind");
8844 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8845 return list;
8848 /* OpenMP 2.5:
8849 shared ( variable-list ) */
8851 static tree
8852 c_parser_omp_clause_shared (c_parser *parser, tree list)
8854 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8857 /* OpenMP 3.0:
8858 untied */
8860 static tree
8861 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8863 tree c;
8865 /* FIXME: Should we allow duplicates? */
8866 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8868 c = build_omp_clause (c_parser_peek_token (parser)->location,
8869 OMP_CLAUSE_UNTIED);
8870 OMP_CLAUSE_CHAIN (c) = list;
8872 return c;
8875 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8876 is a bitmask in MASK. Return the list of clauses found; the result
8877 of clause default goes in *pdefault. */
8879 static tree
8880 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8881 const char *where)
8883 tree clauses = NULL;
8884 bool first = true;
8886 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8888 location_t here;
8889 pragma_omp_clause c_kind;
8890 const char *c_name;
8891 tree prev = clauses;
8893 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8894 c_parser_consume_token (parser);
8896 first = false;
8897 here = c_parser_peek_token (parser)->location;
8898 c_kind = c_parser_omp_clause_name (parser);
8900 switch (c_kind)
8902 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8903 clauses = c_parser_omp_clause_collapse (parser, clauses);
8904 c_name = "collapse";
8905 break;
8906 case PRAGMA_OMP_CLAUSE_COPYIN:
8907 clauses = c_parser_omp_clause_copyin (parser, clauses);
8908 c_name = "copyin";
8909 break;
8910 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8911 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8912 c_name = "copyprivate";
8913 break;
8914 case PRAGMA_OMP_CLAUSE_DEFAULT:
8915 clauses = c_parser_omp_clause_default (parser, clauses);
8916 c_name = "default";
8917 break;
8918 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8919 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8920 c_name = "firstprivate";
8921 break;
8922 case PRAGMA_OMP_CLAUSE_IF:
8923 clauses = c_parser_omp_clause_if (parser, clauses);
8924 c_name = "if";
8925 break;
8926 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8927 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8928 c_name = "lastprivate";
8929 break;
8930 case PRAGMA_OMP_CLAUSE_NOWAIT:
8931 clauses = c_parser_omp_clause_nowait (parser, clauses);
8932 c_name = "nowait";
8933 break;
8934 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8935 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8936 c_name = "num_threads";
8937 break;
8938 case PRAGMA_OMP_CLAUSE_ORDERED:
8939 clauses = c_parser_omp_clause_ordered (parser, clauses);
8940 c_name = "ordered";
8941 break;
8942 case PRAGMA_OMP_CLAUSE_PRIVATE:
8943 clauses = c_parser_omp_clause_private (parser, clauses);
8944 c_name = "private";
8945 break;
8946 case PRAGMA_OMP_CLAUSE_REDUCTION:
8947 clauses = c_parser_omp_clause_reduction (parser, clauses);
8948 c_name = "reduction";
8949 break;
8950 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8951 clauses = c_parser_omp_clause_schedule (parser, clauses);
8952 c_name = "schedule";
8953 break;
8954 case PRAGMA_OMP_CLAUSE_SHARED:
8955 clauses = c_parser_omp_clause_shared (parser, clauses);
8956 c_name = "shared";
8957 break;
8958 case PRAGMA_OMP_CLAUSE_UNTIED:
8959 clauses = c_parser_omp_clause_untied (parser, clauses);
8960 c_name = "untied";
8961 break;
8962 default:
8963 c_parser_error (parser, "expected %<#pragma omp%> clause");
8964 goto saw_error;
8967 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8969 /* Remove the invalid clause(s) from the list to avoid
8970 confusing the rest of the compiler. */
8971 clauses = prev;
8972 error_at (here, "%qs is not valid for %qs", c_name, where);
8976 saw_error:
8977 c_parser_skip_to_pragma_eol (parser);
8979 return c_finish_omp_clauses (clauses);
8982 /* OpenMP 2.5:
8983 structured-block:
8984 statement
8986 In practice, we're also interested in adding the statement to an
8987 outer node. So it is convenient if we work around the fact that
8988 c_parser_statement calls add_stmt. */
8990 static tree
8991 c_parser_omp_structured_block (c_parser *parser)
8993 tree stmt = push_stmt_list ();
8994 c_parser_statement (parser);
8995 return pop_stmt_list (stmt);
8998 /* OpenMP 2.5:
8999 # pragma omp atomic new-line
9000 expression-stmt
9002 expression-stmt:
9003 x binop= expr | x++ | ++x | x-- | --x
9004 binop:
9005 +, *, -, /, &, ^, |, <<, >>
9007 where x is an lvalue expression with scalar type.
9009 LOC is the location of the #pragma token. */
9011 static void
9012 c_parser_omp_atomic (location_t loc, c_parser *parser)
9014 tree lhs, rhs;
9015 tree stmt;
9016 enum tree_code code;
9017 struct c_expr rhs_expr;
9019 c_parser_skip_to_pragma_eol (parser);
9021 lhs = c_parser_unary_expression (parser).value;
9022 lhs = c_fully_fold (lhs, false, NULL);
9023 switch (TREE_CODE (lhs))
9025 case ERROR_MARK:
9026 saw_error:
9027 c_parser_skip_to_end_of_block_or_statement (parser);
9028 return;
9030 case PREINCREMENT_EXPR:
9031 case POSTINCREMENT_EXPR:
9032 lhs = TREE_OPERAND (lhs, 0);
9033 code = PLUS_EXPR;
9034 rhs = integer_one_node;
9035 break;
9037 case PREDECREMENT_EXPR:
9038 case POSTDECREMENT_EXPR:
9039 lhs = TREE_OPERAND (lhs, 0);
9040 code = MINUS_EXPR;
9041 rhs = integer_one_node;
9042 break;
9044 case COMPOUND_EXPR:
9045 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9046 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9047 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9048 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9049 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9050 (TREE_OPERAND (lhs, 1), 0), 0)))
9051 == BOOLEAN_TYPE)
9052 /* Undo effects of boolean_increment for post {in,de}crement. */
9053 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9054 /* FALLTHRU */
9055 case MODIFY_EXPR:
9056 if (TREE_CODE (lhs) == MODIFY_EXPR
9057 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9059 /* Undo effects of boolean_increment. */
9060 if (integer_onep (TREE_OPERAND (lhs, 1)))
9062 /* This is pre or post increment. */
9063 rhs = TREE_OPERAND (lhs, 1);
9064 lhs = TREE_OPERAND (lhs, 0);
9065 code = NOP_EXPR;
9066 break;
9068 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9069 && TREE_OPERAND (lhs, 0)
9070 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9072 /* This is pre or post decrement. */
9073 rhs = TREE_OPERAND (lhs, 1);
9074 lhs = TREE_OPERAND (lhs, 0);
9075 code = NOP_EXPR;
9076 break;
9079 /* FALLTHRU */
9080 default:
9081 switch (c_parser_peek_token (parser)->type)
9083 case CPP_MULT_EQ:
9084 code = MULT_EXPR;
9085 break;
9086 case CPP_DIV_EQ:
9087 code = TRUNC_DIV_EXPR;
9088 break;
9089 case CPP_PLUS_EQ:
9090 code = PLUS_EXPR;
9091 break;
9092 case CPP_MINUS_EQ:
9093 code = MINUS_EXPR;
9094 break;
9095 case CPP_LSHIFT_EQ:
9096 code = LSHIFT_EXPR;
9097 break;
9098 case CPP_RSHIFT_EQ:
9099 code = RSHIFT_EXPR;
9100 break;
9101 case CPP_AND_EQ:
9102 code = BIT_AND_EXPR;
9103 break;
9104 case CPP_OR_EQ:
9105 code = BIT_IOR_EXPR;
9106 break;
9107 case CPP_XOR_EQ:
9108 code = BIT_XOR_EXPR;
9109 break;
9110 default:
9111 c_parser_error (parser,
9112 "invalid operator for %<#pragma omp atomic%>");
9113 goto saw_error;
9116 /* Arrange to pass the location of the assignment operator to
9117 c_finish_omp_atomic. */
9118 loc = c_parser_peek_token (parser)->location;
9119 c_parser_consume_token (parser);
9121 location_t rhs_loc = c_parser_peek_token (parser)->location;
9122 rhs_expr = c_parser_expression (parser);
9123 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9125 rhs = rhs_expr.value;
9126 rhs = c_fully_fold (rhs, false, NULL);
9127 break;
9129 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
9130 if (stmt != error_mark_node)
9131 add_stmt (stmt);
9132 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9136 /* OpenMP 2.5:
9137 # pragma omp barrier new-line
9140 static void
9141 c_parser_omp_barrier (c_parser *parser)
9143 location_t loc = c_parser_peek_token (parser)->location;
9144 c_parser_consume_pragma (parser);
9145 c_parser_skip_to_pragma_eol (parser);
9147 c_finish_omp_barrier (loc);
9150 /* OpenMP 2.5:
9151 # pragma omp critical [(name)] new-line
9152 structured-block
9154 LOC is the location of the #pragma itself. */
9156 static tree
9157 c_parser_omp_critical (location_t loc, c_parser *parser)
9159 tree stmt, name = NULL;
9161 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9163 c_parser_consume_token (parser);
9164 if (c_parser_next_token_is (parser, CPP_NAME))
9166 name = c_parser_peek_token (parser)->value;
9167 c_parser_consume_token (parser);
9168 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9170 else
9171 c_parser_error (parser, "expected identifier");
9173 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9174 c_parser_error (parser, "expected %<(%> or end of line");
9175 c_parser_skip_to_pragma_eol (parser);
9177 stmt = c_parser_omp_structured_block (parser);
9178 return c_finish_omp_critical (loc, stmt, name);
9181 /* OpenMP 2.5:
9182 # pragma omp flush flush-vars[opt] new-line
9184 flush-vars:
9185 ( variable-list ) */
9187 static void
9188 c_parser_omp_flush (c_parser *parser)
9190 location_t loc = c_parser_peek_token (parser)->location;
9191 c_parser_consume_pragma (parser);
9192 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9193 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9194 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9195 c_parser_error (parser, "expected %<(%> or end of line");
9196 c_parser_skip_to_pragma_eol (parser);
9198 c_finish_omp_flush (loc);
9201 /* Parse the restricted form of the for statement allowed by OpenMP.
9202 The real trick here is to determine the loop control variable early
9203 so that we can push a new decl if necessary to make it private.
9204 LOC is the location of the OMP in "#pragma omp". */
9206 static tree
9207 c_parser_omp_for_loop (location_t loc,
9208 c_parser *parser, tree clauses, tree *par_clauses)
9210 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9211 tree declv, condv, incrv, initv, ret = NULL;
9212 bool fail = false, open_brace_parsed = false;
9213 int i, collapse = 1, nbraces = 0;
9214 location_t for_loc;
9215 VEC(tree,gc) *for_block = make_tree_vector ();
9217 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9218 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9219 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9221 gcc_assert (collapse >= 1);
9223 declv = make_tree_vec (collapse);
9224 initv = make_tree_vec (collapse);
9225 condv = make_tree_vec (collapse);
9226 incrv = make_tree_vec (collapse);
9228 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9230 c_parser_error (parser, "for statement expected");
9231 return NULL;
9233 for_loc = c_parser_peek_token (parser)->location;
9234 c_parser_consume_token (parser);
9236 for (i = 0; i < collapse; i++)
9238 int bracecount = 0;
9240 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9241 goto pop_scopes;
9243 /* Parse the initialization declaration or expression. */
9244 if (c_parser_next_tokens_start_declaration (parser))
9246 if (i > 0)
9247 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9248 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9249 decl = check_for_loop_decls (for_loc, flag_isoc99);
9250 if (decl == NULL)
9251 goto error_init;
9252 if (DECL_INITIAL (decl) == error_mark_node)
9253 decl = error_mark_node;
9254 init = decl;
9256 else if (c_parser_next_token_is (parser, CPP_NAME)
9257 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9259 struct c_expr decl_exp;
9260 struct c_expr init_exp;
9261 location_t init_loc;
9263 decl_exp = c_parser_postfix_expression (parser);
9264 decl = decl_exp.value;
9266 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9268 init_loc = c_parser_peek_token (parser)->location;
9269 init_exp = c_parser_expr_no_commas (parser, NULL);
9270 init_exp = default_function_array_read_conversion (init_loc,
9271 init_exp);
9272 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9273 NOP_EXPR, init_loc, init_exp.value,
9274 init_exp.original_type);
9275 init = c_process_expr_stmt (init_loc, init);
9277 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9279 else
9281 error_init:
9282 c_parser_error (parser,
9283 "expected iteration declaration or initialization");
9284 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9285 "expected %<)%>");
9286 fail = true;
9287 goto parse_next;
9290 /* Parse the loop condition. */
9291 cond = NULL_TREE;
9292 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9294 location_t cond_loc = c_parser_peek_token (parser)->location;
9295 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9297 cond = cond_expr.value;
9298 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9299 cond = c_fully_fold (cond, false, NULL);
9300 switch (cond_expr.original_code)
9302 case GT_EXPR:
9303 case GE_EXPR:
9304 case LT_EXPR:
9305 case LE_EXPR:
9306 break;
9307 default:
9308 /* Can't be cond = error_mark_node, because we want to preserve
9309 the location until c_finish_omp_for. */
9310 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9311 break;
9313 protected_set_expr_location (cond, cond_loc);
9315 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9317 /* Parse the increment expression. */
9318 incr = NULL_TREE;
9319 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9321 location_t incr_loc = c_parser_peek_token (parser)->location;
9323 incr = c_process_expr_stmt (incr_loc,
9324 c_parser_expression (parser).value);
9326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9328 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9329 fail = true;
9330 else
9332 TREE_VEC_ELT (declv, i) = decl;
9333 TREE_VEC_ELT (initv, i) = init;
9334 TREE_VEC_ELT (condv, i) = cond;
9335 TREE_VEC_ELT (incrv, i) = incr;
9338 parse_next:
9339 if (i == collapse - 1)
9340 break;
9342 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9343 in between the collapsed for loops to be still considered perfectly
9344 nested. Hopefully the final version clarifies this.
9345 For now handle (multiple) {'s and empty statements. */
9348 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9350 c_parser_consume_token (parser);
9351 break;
9353 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9355 c_parser_consume_token (parser);
9356 bracecount++;
9358 else if (bracecount
9359 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9360 c_parser_consume_token (parser);
9361 else
9363 c_parser_error (parser, "not enough perfectly nested loops");
9364 if (bracecount)
9366 open_brace_parsed = true;
9367 bracecount--;
9369 fail = true;
9370 collapse = 0;
9371 break;
9374 while (1);
9376 nbraces += bracecount;
9379 save_break = c_break_label;
9380 c_break_label = size_one_node;
9381 save_cont = c_cont_label;
9382 c_cont_label = NULL_TREE;
9383 body = push_stmt_list ();
9385 if (open_brace_parsed)
9387 location_t here = c_parser_peek_token (parser)->location;
9388 stmt = c_begin_compound_stmt (true);
9389 c_parser_compound_statement_nostart (parser);
9390 add_stmt (c_end_compound_stmt (here, stmt, true));
9392 else
9393 add_stmt (c_parser_c99_block_statement (parser));
9394 if (c_cont_label)
9396 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9397 SET_EXPR_LOCATION (t, loc);
9398 add_stmt (t);
9401 body = pop_stmt_list (body);
9402 c_break_label = save_break;
9403 c_cont_label = save_cont;
9405 while (nbraces)
9407 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9409 c_parser_consume_token (parser);
9410 nbraces--;
9412 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9413 c_parser_consume_token (parser);
9414 else
9416 c_parser_error (parser, "collapsed loops not perfectly nested");
9417 while (nbraces)
9419 location_t here = c_parser_peek_token (parser)->location;
9420 stmt = c_begin_compound_stmt (true);
9421 add_stmt (body);
9422 c_parser_compound_statement_nostart (parser);
9423 body = c_end_compound_stmt (here, stmt, true);
9424 nbraces--;
9426 goto pop_scopes;
9430 /* Only bother calling c_finish_omp_for if we haven't already generated
9431 an error from the initialization parsing. */
9432 if (!fail)
9434 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9435 if (stmt)
9437 if (par_clauses != NULL)
9439 tree *c;
9440 for (c = par_clauses; *c ; )
9441 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9442 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9443 c = &OMP_CLAUSE_CHAIN (*c);
9444 else
9446 for (i = 0; i < collapse; i++)
9447 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9448 break;
9449 if (i == collapse)
9450 c = &OMP_CLAUSE_CHAIN (*c);
9451 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9453 error_at (loc,
9454 "iteration variable %qD should not be firstprivate",
9455 OMP_CLAUSE_DECL (*c));
9456 *c = OMP_CLAUSE_CHAIN (*c);
9458 else
9460 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9461 change it to shared (decl) in
9462 OMP_PARALLEL_CLAUSES. */
9463 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9464 OMP_CLAUSE_LASTPRIVATE);
9465 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9466 OMP_CLAUSE_CHAIN (l) = clauses;
9467 clauses = l;
9468 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9472 OMP_FOR_CLAUSES (stmt) = clauses;
9474 ret = stmt;
9476 pop_scopes:
9477 while (!VEC_empty (tree, for_block))
9479 /* FIXME diagnostics: LOC below should be the actual location of
9480 this particular for block. We need to build a list of
9481 locations to go along with FOR_BLOCK. */
9482 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9483 add_stmt (stmt);
9485 release_tree_vector (for_block);
9486 return ret;
9489 /* OpenMP 2.5:
9490 #pragma omp for for-clause[optseq] new-line
9491 for-loop
9493 LOC is the location of the #pragma token.
9496 #define OMP_FOR_CLAUSE_MASK \
9497 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9498 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9499 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9500 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9501 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9502 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9503 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9504 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9506 static tree
9507 c_parser_omp_for (location_t loc, c_parser *parser)
9509 tree block, clauses, ret;
9511 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9512 "#pragma omp for");
9514 block = c_begin_compound_stmt (true);
9515 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9516 block = c_end_compound_stmt (loc, block, true);
9517 add_stmt (block);
9519 return ret;
9522 /* OpenMP 2.5:
9523 # pragma omp master new-line
9524 structured-block
9526 LOC is the location of the #pragma token.
9529 static tree
9530 c_parser_omp_master (location_t loc, c_parser *parser)
9532 c_parser_skip_to_pragma_eol (parser);
9533 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9536 /* OpenMP 2.5:
9537 # pragma omp ordered new-line
9538 structured-block
9540 LOC is the location of the #pragma itself.
9543 static tree
9544 c_parser_omp_ordered (location_t loc, c_parser *parser)
9546 c_parser_skip_to_pragma_eol (parser);
9547 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9550 /* OpenMP 2.5:
9552 section-scope:
9553 { section-sequence }
9555 section-sequence:
9556 section-directive[opt] structured-block
9557 section-sequence section-directive structured-block
9559 SECTIONS_LOC is the location of the #pragma omp sections. */
9561 static tree
9562 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9564 tree stmt, substmt;
9565 bool error_suppress = false;
9566 location_t loc;
9568 loc = c_parser_peek_token (parser)->location;
9569 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9571 /* Avoid skipping until the end of the block. */
9572 parser->error = false;
9573 return NULL_TREE;
9576 stmt = push_stmt_list ();
9578 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9580 substmt = push_stmt_list ();
9582 while (1)
9584 c_parser_statement (parser);
9586 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9587 break;
9588 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9589 break;
9590 if (c_parser_next_token_is (parser, CPP_EOF))
9591 break;
9594 substmt = pop_stmt_list (substmt);
9595 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9596 SET_EXPR_LOCATION (substmt, loc);
9597 add_stmt (substmt);
9600 while (1)
9602 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9603 break;
9604 if (c_parser_next_token_is (parser, CPP_EOF))
9605 break;
9607 loc = c_parser_peek_token (parser)->location;
9608 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9610 c_parser_consume_pragma (parser);
9611 c_parser_skip_to_pragma_eol (parser);
9612 error_suppress = false;
9614 else if (!error_suppress)
9616 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9617 error_suppress = true;
9620 substmt = c_parser_omp_structured_block (parser);
9621 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9622 SET_EXPR_LOCATION (substmt, loc);
9623 add_stmt (substmt);
9625 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9626 "expected %<#pragma omp section%> or %<}%>");
9628 substmt = pop_stmt_list (stmt);
9630 stmt = make_node (OMP_SECTIONS);
9631 SET_EXPR_LOCATION (stmt, sections_loc);
9632 TREE_TYPE (stmt) = void_type_node;
9633 OMP_SECTIONS_BODY (stmt) = substmt;
9635 return add_stmt (stmt);
9638 /* OpenMP 2.5:
9639 # pragma omp sections sections-clause[optseq] newline
9640 sections-scope
9642 LOC is the location of the #pragma token.
9645 #define OMP_SECTIONS_CLAUSE_MASK \
9646 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9647 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9648 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9649 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9650 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9652 static tree
9653 c_parser_omp_sections (location_t loc, c_parser *parser)
9655 tree block, clauses, ret;
9657 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9658 "#pragma omp sections");
9660 block = c_begin_compound_stmt (true);
9661 ret = c_parser_omp_sections_scope (loc, parser);
9662 if (ret)
9663 OMP_SECTIONS_CLAUSES (ret) = clauses;
9664 block = c_end_compound_stmt (loc, block, true);
9665 add_stmt (block);
9667 return ret;
9670 /* OpenMP 2.5:
9671 # pragma parallel parallel-clause new-line
9672 # pragma parallel for parallel-for-clause new-line
9673 # pragma parallel sections parallel-sections-clause new-line
9675 LOC is the location of the #pragma token.
9678 #define OMP_PARALLEL_CLAUSE_MASK \
9679 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9680 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9681 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9682 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9683 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9684 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9685 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9686 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9688 static tree
9689 c_parser_omp_parallel (location_t loc, c_parser *parser)
9691 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9692 const char *p_name = "#pragma omp parallel";
9693 tree stmt, clauses, par_clause, ws_clause, block;
9694 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9696 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9698 c_parser_consume_token (parser);
9699 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9700 p_name = "#pragma omp parallel for";
9701 mask |= OMP_FOR_CLAUSE_MASK;
9702 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9704 else if (c_parser_next_token_is (parser, CPP_NAME))
9706 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9707 if (strcmp (p, "sections") == 0)
9709 c_parser_consume_token (parser);
9710 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9711 p_name = "#pragma omp parallel sections";
9712 mask |= OMP_SECTIONS_CLAUSE_MASK;
9713 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9717 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9719 switch (p_kind)
9721 case PRAGMA_OMP_PARALLEL:
9722 block = c_begin_omp_parallel ();
9723 c_parser_statement (parser);
9724 stmt = c_finish_omp_parallel (loc, clauses, block);
9725 break;
9727 case PRAGMA_OMP_PARALLEL_FOR:
9728 block = c_begin_omp_parallel ();
9729 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9730 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9731 stmt = c_finish_omp_parallel (loc, par_clause, block);
9732 OMP_PARALLEL_COMBINED (stmt) = 1;
9733 break;
9735 case PRAGMA_OMP_PARALLEL_SECTIONS:
9736 block = c_begin_omp_parallel ();
9737 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9738 stmt = c_parser_omp_sections_scope (loc, parser);
9739 if (stmt)
9740 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9741 stmt = c_finish_omp_parallel (loc, par_clause, block);
9742 OMP_PARALLEL_COMBINED (stmt) = 1;
9743 break;
9745 default:
9746 gcc_unreachable ();
9749 return stmt;
9752 /* OpenMP 2.5:
9753 # pragma omp single single-clause[optseq] new-line
9754 structured-block
9756 LOC is the location of the #pragma.
9759 #define OMP_SINGLE_CLAUSE_MASK \
9760 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9761 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9762 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9763 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9765 static tree
9766 c_parser_omp_single (location_t loc, c_parser *parser)
9768 tree stmt = make_node (OMP_SINGLE);
9769 SET_EXPR_LOCATION (stmt, loc);
9770 TREE_TYPE (stmt) = void_type_node;
9772 OMP_SINGLE_CLAUSES (stmt)
9773 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9774 "#pragma omp single");
9775 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9777 return add_stmt (stmt);
9780 /* OpenMP 3.0:
9781 # pragma omp task task-clause[optseq] new-line
9783 LOC is the location of the #pragma.
9786 #define OMP_TASK_CLAUSE_MASK \
9787 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9788 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9789 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9790 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9791 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9792 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9794 static tree
9795 c_parser_omp_task (location_t loc, c_parser *parser)
9797 tree clauses, block;
9799 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9800 "#pragma omp task");
9802 block = c_begin_omp_task ();
9803 c_parser_statement (parser);
9804 return c_finish_omp_task (loc, clauses, block);
9807 /* OpenMP 3.0:
9808 # pragma omp taskwait new-line
9811 static void
9812 c_parser_omp_taskwait (c_parser *parser)
9814 location_t loc = c_parser_peek_token (parser)->location;
9815 c_parser_consume_pragma (parser);
9816 c_parser_skip_to_pragma_eol (parser);
9818 c_finish_omp_taskwait (loc);
9821 /* Main entry point to parsing most OpenMP pragmas. */
9823 static void
9824 c_parser_omp_construct (c_parser *parser)
9826 enum pragma_kind p_kind;
9827 location_t loc;
9828 tree stmt;
9830 loc = c_parser_peek_token (parser)->location;
9831 p_kind = c_parser_peek_token (parser)->pragma_kind;
9832 c_parser_consume_pragma (parser);
9834 switch (p_kind)
9836 case PRAGMA_OMP_ATOMIC:
9837 c_parser_omp_atomic (loc, parser);
9838 return;
9839 case PRAGMA_OMP_CRITICAL:
9840 stmt = c_parser_omp_critical (loc, parser);
9841 break;
9842 case PRAGMA_OMP_FOR:
9843 stmt = c_parser_omp_for (loc, parser);
9844 break;
9845 case PRAGMA_OMP_MASTER:
9846 stmt = c_parser_omp_master (loc, parser);
9847 break;
9848 case PRAGMA_OMP_ORDERED:
9849 stmt = c_parser_omp_ordered (loc, parser);
9850 break;
9851 case PRAGMA_OMP_PARALLEL:
9852 stmt = c_parser_omp_parallel (loc, parser);
9853 break;
9854 case PRAGMA_OMP_SECTIONS:
9855 stmt = c_parser_omp_sections (loc, parser);
9856 break;
9857 case PRAGMA_OMP_SINGLE:
9858 stmt = c_parser_omp_single (loc, parser);
9859 break;
9860 case PRAGMA_OMP_TASK:
9861 stmt = c_parser_omp_task (loc, parser);
9862 break;
9863 default:
9864 gcc_unreachable ();
9867 if (stmt)
9868 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9872 /* OpenMP 2.5:
9873 # pragma omp threadprivate (variable-list) */
9875 static void
9876 c_parser_omp_threadprivate (c_parser *parser)
9878 tree vars, t;
9879 location_t loc;
9881 c_parser_consume_pragma (parser);
9882 loc = c_parser_peek_token (parser)->location;
9883 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9885 /* Mark every variable in VARS to be assigned thread local storage. */
9886 for (t = vars; t; t = TREE_CHAIN (t))
9888 tree v = TREE_PURPOSE (t);
9890 /* FIXME diagnostics: Ideally we should keep individual
9891 locations for all the variables in the var list to make the
9892 following errors more precise. Perhaps
9893 c_parser_omp_var_list_parens() should construct a list of
9894 locations to go along with the var list. */
9896 /* If V had already been marked threadprivate, it doesn't matter
9897 whether it had been used prior to this point. */
9898 if (TREE_CODE (v) != VAR_DECL)
9899 error_at (loc, "%qD is not a variable", v);
9900 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9901 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9902 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9903 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9904 else if (TREE_TYPE (v) == error_mark_node)
9906 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9907 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9908 else
9910 if (! DECL_THREAD_LOCAL_P (v))
9912 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9913 /* If rtl has been already set for this var, call
9914 make_decl_rtl once again, so that encode_section_info
9915 has a chance to look at the new decl flags. */
9916 if (DECL_RTL_SET_P (v))
9917 make_decl_rtl (v);
9919 C_DECL_THREADPRIVATE_P (v) = 1;
9923 c_parser_skip_to_pragma_eol (parser);
9927 /* Parse a single source file. */
9929 void
9930 c_parse_file (void)
9932 /* Use local storage to begin. If the first token is a pragma, parse it.
9933 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9934 which will cause garbage collection. */
9935 c_parser tparser;
9937 memset (&tparser, 0, sizeof tparser);
9938 the_parser = &tparser;
9940 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9941 c_parser_pragma_pch_preprocess (&tparser);
9943 the_parser = ggc_alloc_c_parser ();
9944 *the_parser = tparser;
9946 /* Initialize EH, if we've been told to do so. */
9947 if (flag_exceptions)
9948 using_eh_for_cleanups ();
9950 c_parser_translation_unit (the_parser);
9951 the_parser = NULL;
9954 #include "gt-c-parser.h"