Daily bump.
[official-gcc.git] / gcc / c-parser.c
blob33e7362baa00b35f2be8190776d1fef7dc52944d
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 | (warn_overlength_strings << 6));
1050 cpp_opts->cpp_pedantic = pedantic = 0;
1051 warn_pointer_arith = 0;
1052 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1053 flag_iso = 0;
1054 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1055 warn_cxx_compat = 0;
1056 warn_overlength_strings = 0;
1057 return ret;
1060 /* Restore the warning flags which are controlled by __extension__.
1061 FLAGS is the return value from disable_extension_diagnostics. */
1063 static inline void
1064 restore_extension_diagnostics (int flags)
1066 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1067 warn_pointer_arith = (flags >> 1) & 1;
1068 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1069 flag_iso = (flags >> 3) & 1;
1070 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1071 warn_cxx_compat = (flags >> 5) & 1;
1072 warn_overlength_strings = (flags >> 6) & 1;
1075 /* Possibly kinds of declarator to parse. */
1076 typedef enum c_dtr_syn {
1077 /* A normal declarator with an identifier. */
1078 C_DTR_NORMAL,
1079 /* An abstract declarator (maybe empty). */
1080 C_DTR_ABSTRACT,
1081 /* A parameter declarator: may be either, but after a type name does
1082 not redeclare a typedef name as an identifier if it can
1083 alternatively be interpreted as a typedef name; see DR#009,
1084 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1085 following DR#249. For example, given a typedef T, "int T" and
1086 "int *T" are valid parameter declarations redeclaring T, while
1087 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1088 abstract declarators rather than involving redundant parentheses;
1089 the same applies with attributes inside the parentheses before
1090 "T". */
1091 C_DTR_PARM
1092 } c_dtr_syn;
1094 static void c_parser_external_declaration (c_parser *);
1095 static void c_parser_asm_definition (c_parser *);
1096 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1097 bool, bool, tree *);
1098 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1099 static void c_parser_static_assert_declaration (c_parser *);
1100 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1101 bool, enum c_lookahead_kind);
1102 static struct c_typespec c_parser_enum_specifier (c_parser *);
1103 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1104 static tree c_parser_struct_declaration (c_parser *);
1105 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1106 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1107 bool *);
1108 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1109 c_dtr_syn, bool *);
1110 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1111 bool,
1112 struct c_declarator *);
1113 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1114 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
1115 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1116 static tree c_parser_simple_asm_expr (c_parser *);
1117 static tree c_parser_attributes (c_parser *);
1118 static struct c_type_name *c_parser_type_name (c_parser *);
1119 static struct c_expr c_parser_initializer (c_parser *);
1120 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1121 static void c_parser_initelt (c_parser *, struct obstack *);
1122 static void c_parser_initval (c_parser *, struct c_expr *,
1123 struct obstack *);
1124 static tree c_parser_compound_statement (c_parser *);
1125 static void c_parser_compound_statement_nostart (c_parser *);
1126 static void c_parser_label (c_parser *);
1127 static void c_parser_statement (c_parser *);
1128 static void c_parser_statement_after_labels (c_parser *);
1129 static void c_parser_if_statement (c_parser *);
1130 static void c_parser_switch_statement (c_parser *);
1131 static void c_parser_while_statement (c_parser *);
1132 static void c_parser_do_statement (c_parser *);
1133 static void c_parser_for_statement (c_parser *);
1134 static tree c_parser_asm_statement (c_parser *);
1135 static tree c_parser_asm_operands (c_parser *, bool);
1136 static tree c_parser_asm_goto_operands (c_parser *);
1137 static tree c_parser_asm_clobbers (c_parser *);
1138 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1139 static struct c_expr c_parser_conditional_expression (c_parser *,
1140 struct c_expr *);
1141 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1142 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1143 static struct c_expr c_parser_unary_expression (c_parser *);
1144 static struct c_expr c_parser_sizeof_expression (c_parser *);
1145 static struct c_expr c_parser_alignof_expression (c_parser *);
1146 static struct c_expr c_parser_postfix_expression (c_parser *);
1147 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1148 struct c_type_name *,
1149 location_t);
1150 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1151 location_t loc,
1152 struct c_expr);
1153 static struct c_expr c_parser_expression (c_parser *);
1154 static struct c_expr c_parser_expression_conv (c_parser *);
1155 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1156 VEC(tree,gc) **);
1157 static void c_parser_omp_construct (c_parser *);
1158 static void c_parser_omp_threadprivate (c_parser *);
1159 static void c_parser_omp_barrier (c_parser *);
1160 static void c_parser_omp_flush (c_parser *);
1161 static void c_parser_omp_taskwait (c_parser *);
1163 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1164 static bool c_parser_pragma (c_parser *, enum pragma_context);
1166 /* These Objective-C parser functions are only ever called when
1167 compiling Objective-C. */
1168 static void c_parser_objc_class_definition (c_parser *, tree);
1169 static void c_parser_objc_class_instance_variables (c_parser *);
1170 static void c_parser_objc_class_declaration (c_parser *);
1171 static void c_parser_objc_alias_declaration (c_parser *);
1172 static void c_parser_objc_protocol_definition (c_parser *, tree);
1173 static bool c_parser_objc_method_type (c_parser *);
1174 static void c_parser_objc_method_definition (c_parser *);
1175 static void c_parser_objc_methodprotolist (c_parser *);
1176 static void c_parser_objc_methodproto (c_parser *);
1177 static tree c_parser_objc_method_decl (c_parser *, bool, tree *);
1178 static tree c_parser_objc_type_name (c_parser *);
1179 static tree c_parser_objc_protocol_refs (c_parser *);
1180 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1181 static void c_parser_objc_synchronized_statement (c_parser *);
1182 static tree c_parser_objc_selector (c_parser *);
1183 static tree c_parser_objc_selector_arg (c_parser *);
1184 static tree c_parser_objc_receiver (c_parser *);
1185 static tree c_parser_objc_message_args (c_parser *);
1186 static tree c_parser_objc_keywordexpr (c_parser *);
1187 static void c_parser_objc_at_property_declaration (c_parser *);
1188 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1189 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1190 static bool c_parser_objc_diagnose_bad_element_prefix
1191 (c_parser *, struct c_declspecs *);
1193 /* Parse a translation unit (C90 6.7, C99 6.9).
1195 translation-unit:
1196 external-declarations
1198 external-declarations:
1199 external-declaration
1200 external-declarations external-declaration
1202 GNU extensions:
1204 translation-unit:
1205 empty
1208 static void
1209 c_parser_translation_unit (c_parser *parser)
1211 if (c_parser_next_token_is (parser, CPP_EOF))
1213 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1214 "ISO C forbids an empty translation unit");
1216 else
1218 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1219 mark_valid_location_for_stdc_pragma (false);
1222 ggc_collect ();
1223 c_parser_external_declaration (parser);
1224 obstack_free (&parser_obstack, obstack_position);
1226 while (c_parser_next_token_is_not (parser, CPP_EOF));
1230 /* Parse an external declaration (C90 6.7, C99 6.9).
1232 external-declaration:
1233 function-definition
1234 declaration
1236 GNU extensions:
1238 external-declaration:
1239 asm-definition
1241 __extension__ external-declaration
1243 Objective-C:
1245 external-declaration:
1246 objc-class-definition
1247 objc-class-declaration
1248 objc-alias-declaration
1249 objc-protocol-definition
1250 objc-method-definition
1251 @end
1254 static void
1255 c_parser_external_declaration (c_parser *parser)
1257 int ext;
1258 switch (c_parser_peek_token (parser)->type)
1260 case CPP_KEYWORD:
1261 switch (c_parser_peek_token (parser)->keyword)
1263 case RID_EXTENSION:
1264 ext = disable_extension_diagnostics ();
1265 c_parser_consume_token (parser);
1266 c_parser_external_declaration (parser);
1267 restore_extension_diagnostics (ext);
1268 break;
1269 case RID_ASM:
1270 c_parser_asm_definition (parser);
1271 break;
1272 case RID_AT_INTERFACE:
1273 case RID_AT_IMPLEMENTATION:
1274 gcc_assert (c_dialect_objc ());
1275 c_parser_objc_class_definition (parser, NULL_TREE);
1276 break;
1277 case RID_AT_CLASS:
1278 gcc_assert (c_dialect_objc ());
1279 c_parser_objc_class_declaration (parser);
1280 break;
1281 case RID_AT_ALIAS:
1282 gcc_assert (c_dialect_objc ());
1283 c_parser_objc_alias_declaration (parser);
1284 break;
1285 case RID_AT_PROTOCOL:
1286 gcc_assert (c_dialect_objc ());
1287 c_parser_objc_protocol_definition (parser, NULL_TREE);
1288 break;
1289 case RID_AT_PROPERTY:
1290 gcc_assert (c_dialect_objc ());
1291 c_parser_objc_at_property_declaration (parser);
1292 break;
1293 case RID_AT_SYNTHESIZE:
1294 gcc_assert (c_dialect_objc ());
1295 c_parser_objc_at_synthesize_declaration (parser);
1296 break;
1297 case RID_AT_DYNAMIC:
1298 gcc_assert (c_dialect_objc ());
1299 c_parser_objc_at_dynamic_declaration (parser);
1300 break;
1301 case RID_AT_END:
1302 gcc_assert (c_dialect_objc ());
1303 c_parser_consume_token (parser);
1304 objc_finish_implementation ();
1305 break;
1306 default:
1307 goto decl_or_fndef;
1309 break;
1310 case CPP_SEMICOLON:
1311 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1312 "ISO C does not allow extra %<;%> outside of a function");
1313 c_parser_consume_token (parser);
1314 break;
1315 case CPP_PRAGMA:
1316 mark_valid_location_for_stdc_pragma (true);
1317 c_parser_pragma (parser, pragma_external);
1318 mark_valid_location_for_stdc_pragma (false);
1319 break;
1320 case CPP_PLUS:
1321 case CPP_MINUS:
1322 if (c_dialect_objc ())
1324 c_parser_objc_method_definition (parser);
1325 break;
1327 /* Else fall through, and yield a syntax error trying to parse
1328 as a declaration or function definition. */
1329 default:
1330 decl_or_fndef:
1331 /* A declaration or a function definition (or, in Objective-C,
1332 an @interface or @protocol with prefix attributes). We can
1333 only tell which after parsing the declaration specifiers, if
1334 any, and the first declarator. */
1335 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1336 break;
1340 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1341 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1342 accepted; otherwise (old-style parameter declarations) only other
1343 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1344 assertion is accepted; otherwise (old-style parameter declarations)
1345 it is not. If NESTED is true, we are inside a function or parsing
1346 old-style parameter declarations; any functions encountered are
1347 nested functions and declaration specifiers are required; otherwise
1348 we are at top level and functions are normal functions and
1349 declaration specifiers may be optional. If EMPTY_OK is true, empty
1350 declarations are OK (subject to all other constraints); otherwise
1351 (old-style parameter declarations) they are diagnosed. If
1352 START_ATTR_OK is true, the declaration specifiers may start with
1353 attributes; otherwise they may not.
1354 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1355 declaration when parsing an Objective-C foreach statement.
1357 declaration:
1358 declaration-specifiers init-declarator-list[opt] ;
1359 static_assert-declaration
1361 function-definition:
1362 declaration-specifiers[opt] declarator declaration-list[opt]
1363 compound-statement
1365 declaration-list:
1366 declaration
1367 declaration-list declaration
1369 init-declarator-list:
1370 init-declarator
1371 init-declarator-list , init-declarator
1373 init-declarator:
1374 declarator simple-asm-expr[opt] attributes[opt]
1375 declarator simple-asm-expr[opt] attributes[opt] = initializer
1377 GNU extensions:
1379 nested-function-definition:
1380 declaration-specifiers declarator declaration-list[opt]
1381 compound-statement
1383 Objective-C:
1384 attributes objc-class-definition
1385 attributes objc-category-definition
1386 attributes objc-protocol-definition
1388 The simple-asm-expr and attributes are GNU extensions.
1390 This function does not handle __extension__; that is handled in its
1391 callers. ??? Following the old parser, __extension__ may start
1392 external declarations, declarations in functions and declarations
1393 at the start of "for" loops, but not old-style parameter
1394 declarations.
1396 C99 requires declaration specifiers in a function definition; the
1397 absence is diagnosed through the diagnosis of implicit int. In GNU
1398 C we also allow but diagnose declarations without declaration
1399 specifiers, but only at top level (elsewhere they conflict with
1400 other syntax).
1402 In Objective-C, declarations of the looping variable in a foreach
1403 statement are exceptionally terminated by 'in' (for example, 'for
1404 (NSObject *object in array) { ... }').
1406 OpenMP:
1408 declaration:
1409 threadprivate-directive */
1411 static void
1412 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1413 bool static_assert_ok, bool empty_ok,
1414 bool nested, bool start_attr_ok,
1415 tree *objc_foreach_object_declaration)
1417 struct c_declspecs *specs;
1418 tree prefix_attrs;
1419 tree all_prefix_attrs;
1420 bool diagnosed_no_specs = false;
1421 location_t here = c_parser_peek_token (parser)->location;
1423 if (static_assert_ok
1424 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1426 c_parser_static_assert_declaration (parser);
1427 return;
1429 specs = build_null_declspecs ();
1431 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1432 if (c_parser_peek_token (parser)->type == CPP_NAME
1433 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1434 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1435 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1436 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1438 error_at (here, "unknown type name %qE",
1439 c_parser_peek_token (parser)->value);
1441 /* Parse declspecs normally to get a correct pointer type, but avoid
1442 a further "fails to be a type name" error. Refuse nested functions
1443 since it is not how the user likely wants us to recover. */
1444 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1445 c_parser_peek_token (parser)->keyword = RID_VOID;
1446 c_parser_peek_token (parser)->value = error_mark_node;
1447 fndef_ok = !nested;
1450 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1451 if (parser->error)
1453 c_parser_skip_to_end_of_block_or_statement (parser);
1454 return;
1456 if (nested && !specs->declspecs_seen_p)
1458 c_parser_error (parser, "expected declaration specifiers");
1459 c_parser_skip_to_end_of_block_or_statement (parser);
1460 return;
1462 finish_declspecs (specs);
1463 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1465 if (empty_ok)
1466 shadow_tag (specs);
1467 else
1469 shadow_tag_warned (specs, 1);
1470 pedwarn (here, 0, "empty declaration");
1472 c_parser_consume_token (parser);
1473 return;
1476 /* Provide better error recovery. Note that a type name here is usually
1477 better diagnosed as a redeclaration. */
1478 if (empty_ok
1479 && specs->typespec_kind == ctsk_tagdef
1480 && c_parser_next_token_starts_declspecs (parser)
1481 && !c_parser_next_token_is (parser, CPP_NAME))
1483 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1484 parser->error = false;
1485 shadow_tag_warned (specs, 1);
1486 return;
1488 else if (c_dialect_objc ())
1490 /* Prefix attributes are an error on method decls. */
1491 switch (c_parser_peek_token (parser)->type)
1493 case CPP_PLUS:
1494 case CPP_MINUS:
1495 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1496 return;
1497 if (specs->attrs)
1499 warning_at (c_parser_peek_token (parser)->location,
1500 OPT_Wattributes,
1501 "prefix attributes are ignored for methods");
1502 specs->attrs = NULL_TREE;
1504 if (fndef_ok)
1505 c_parser_objc_method_definition (parser);
1506 else
1507 c_parser_objc_methodproto (parser);
1508 return;
1509 break;
1510 default:
1511 break;
1513 /* This is where we parse 'attributes @interface ...',
1514 'attributes @implementation ...', 'attributes @protocol ...'
1515 (where attributes could be, for example, __attribute__
1516 ((deprecated)).
1518 switch (c_parser_peek_token (parser)->keyword)
1520 case RID_AT_INTERFACE:
1522 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1523 return;
1524 c_parser_objc_class_definition (parser, specs->attrs);
1525 return;
1527 break;
1528 case RID_AT_IMPLEMENTATION:
1530 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1531 return;
1532 if (specs->attrs)
1534 warning_at (c_parser_peek_token (parser)->location,
1535 OPT_Wattributes,
1536 "prefix attributes are ignored for implementations");
1537 specs->attrs = NULL_TREE;
1539 c_parser_objc_class_definition (parser, NULL_TREE);
1540 return;
1542 break;
1543 case RID_AT_PROTOCOL:
1545 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1546 return;
1547 c_parser_objc_protocol_definition (parser, specs->attrs);
1548 return;
1550 break;
1551 case RID_AT_ALIAS:
1552 case RID_AT_CLASS:
1553 case RID_AT_END:
1554 case RID_AT_PROPERTY:
1555 if (specs->attrs)
1557 c_parser_error (parser, "unexpected attribute");
1558 specs->attrs = NULL;
1560 break;
1561 default:
1562 break;
1566 pending_xref_error ();
1567 prefix_attrs = specs->attrs;
1568 all_prefix_attrs = prefix_attrs;
1569 specs->attrs = NULL_TREE;
1570 while (true)
1572 struct c_declarator *declarator;
1573 bool dummy = false;
1574 tree fnbody;
1575 /* Declaring either one or more declarators (in which case we
1576 should diagnose if there were no declaration specifiers) or a
1577 function definition (in which case the diagnostic for
1578 implicit int suffices). */
1579 declarator = c_parser_declarator (parser,
1580 specs->typespec_kind != ctsk_none,
1581 C_DTR_NORMAL, &dummy);
1582 if (declarator == NULL)
1584 c_parser_skip_to_end_of_block_or_statement (parser);
1585 return;
1587 if (c_parser_next_token_is (parser, CPP_EQ)
1588 || c_parser_next_token_is (parser, CPP_COMMA)
1589 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1590 || c_parser_next_token_is_keyword (parser, RID_ASM)
1591 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1592 || c_parser_next_token_is_keyword (parser, RID_IN))
1594 tree asm_name = NULL_TREE;
1595 tree postfix_attrs = NULL_TREE;
1596 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1598 diagnosed_no_specs = true;
1599 pedwarn (here, 0, "data definition has no type or storage class");
1601 /* Having seen a data definition, there cannot now be a
1602 function definition. */
1603 fndef_ok = false;
1604 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1605 asm_name = c_parser_simple_asm_expr (parser);
1606 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1607 postfix_attrs = c_parser_attributes (parser);
1608 if (c_parser_next_token_is (parser, CPP_EQ))
1610 tree d;
1611 struct c_expr init;
1612 location_t init_loc;
1613 c_parser_consume_token (parser);
1614 /* The declaration of the variable is in effect while
1615 its initializer is parsed. */
1616 d = start_decl (declarator, specs, true,
1617 chainon (postfix_attrs, all_prefix_attrs));
1618 if (!d)
1619 d = error_mark_node;
1620 start_init (d, asm_name, global_bindings_p ());
1621 init_loc = c_parser_peek_token (parser)->location;
1622 init = c_parser_initializer (parser);
1623 finish_init ();
1624 if (d != error_mark_node)
1626 maybe_warn_string_init (TREE_TYPE (d), init);
1627 finish_decl (d, init_loc, init.value,
1628 init.original_type, asm_name);
1631 else
1633 tree d = start_decl (declarator, specs, false,
1634 chainon (postfix_attrs,
1635 all_prefix_attrs));
1636 if (d)
1637 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1638 NULL_TREE, asm_name);
1640 if (c_parser_next_token_is_keyword (parser, RID_IN))
1642 if (d)
1643 *objc_foreach_object_declaration = d;
1644 else
1645 *objc_foreach_object_declaration = error_mark_node;
1648 if (c_parser_next_token_is (parser, CPP_COMMA))
1650 c_parser_consume_token (parser);
1651 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1652 all_prefix_attrs = chainon (c_parser_attributes (parser),
1653 prefix_attrs);
1654 else
1655 all_prefix_attrs = prefix_attrs;
1656 continue;
1658 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1660 c_parser_consume_token (parser);
1661 return;
1663 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1665 /* This can only happen in Objective-C: we found the
1666 'in' that terminates the declaration inside an
1667 Objective-C foreach statement. Do not consume the
1668 token, so that the caller can use it to determine
1669 that this indeed is a foreach context. */
1670 return;
1672 else
1674 c_parser_error (parser, "expected %<,%> or %<;%>");
1675 c_parser_skip_to_end_of_block_or_statement (parser);
1676 return;
1679 else if (!fndef_ok)
1681 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1682 "%<asm%> or %<__attribute__%>");
1683 c_parser_skip_to_end_of_block_or_statement (parser);
1684 return;
1686 /* Function definition (nested or otherwise). */
1687 if (nested)
1689 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1690 c_push_function_context ();
1692 if (!start_function (specs, declarator, all_prefix_attrs))
1694 /* This can appear in many cases looking nothing like a
1695 function definition, so we don't give a more specific
1696 error suggesting there was one. */
1697 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1698 "or %<__attribute__%>");
1699 if (nested)
1700 c_pop_function_context ();
1701 break;
1703 /* Parse old-style parameter declarations. ??? Attributes are
1704 not allowed to start declaration specifiers here because of a
1705 syntax conflict between a function declaration with attribute
1706 suffix and a function definition with an attribute prefix on
1707 first old-style parameter declaration. Following the old
1708 parser, they are not accepted on subsequent old-style
1709 parameter declarations either. However, there is no
1710 ambiguity after the first declaration, nor indeed on the
1711 first as long as we don't allow postfix attributes after a
1712 declarator with a nonempty identifier list in a definition;
1713 and postfix attributes have never been accepted here in
1714 function definitions either. */
1715 while (c_parser_next_token_is_not (parser, CPP_EOF)
1716 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1717 c_parser_declaration_or_fndef (parser, false, false, false,
1718 true, false, NULL);
1719 store_parm_decls ();
1720 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1721 = c_parser_peek_token (parser)->location;
1722 fnbody = c_parser_compound_statement (parser);
1723 if (nested)
1725 tree decl = current_function_decl;
1726 /* Mark nested functions as needing static-chain initially.
1727 lower_nested_functions will recompute it but the
1728 DECL_STATIC_CHAIN flag is also used before that happens,
1729 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1730 DECL_STATIC_CHAIN (decl) = 1;
1731 add_stmt (fnbody);
1732 finish_function ();
1733 c_pop_function_context ();
1734 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1736 else
1738 add_stmt (fnbody);
1739 finish_function ();
1741 break;
1745 /* Parse an asm-definition (asm() outside a function body). This is a
1746 GNU extension.
1748 asm-definition:
1749 simple-asm-expr ;
1752 static void
1753 c_parser_asm_definition (c_parser *parser)
1755 tree asm_str = c_parser_simple_asm_expr (parser);
1756 if (asm_str)
1757 cgraph_add_asm_node (asm_str);
1758 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1761 /* Parse a static assertion (C1X N1425 6.7.10).
1763 static_assert-declaration:
1764 static_assert-declaration-no-semi ;
1767 static void
1768 c_parser_static_assert_declaration (c_parser *parser)
1770 c_parser_static_assert_declaration_no_semi (parser);
1771 if (parser->error
1772 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1773 c_parser_skip_to_end_of_block_or_statement (parser);
1776 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1777 semicolon.
1779 static_assert-declaration-no-semi:
1780 _Static_assert ( constant-expression , string-literal )
1783 static void
1784 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1786 location_t assert_loc, value_loc;
1787 tree value;
1788 tree string;
1790 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1791 assert_loc = c_parser_peek_token (parser)->location;
1792 if (!flag_isoc1x)
1794 if (flag_isoc99)
1795 pedwarn (assert_loc, OPT_pedantic,
1796 "ISO C99 does not support %<_Static_assert%>");
1797 else
1798 pedwarn (assert_loc, OPT_pedantic,
1799 "ISO C90 does not support %<_Static_assert%>");
1801 c_parser_consume_token (parser);
1802 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1803 return;
1804 value_loc = c_parser_peek_token (parser)->location;
1805 value = c_parser_expr_no_commas (parser, NULL).value;
1806 parser->lex_untranslated_string = true;
1807 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1809 parser->lex_untranslated_string = false;
1810 return;
1812 switch (c_parser_peek_token (parser)->type)
1814 case CPP_STRING:
1815 case CPP_STRING16:
1816 case CPP_STRING32:
1817 case CPP_WSTRING:
1818 case CPP_UTF8STRING:
1819 string = c_parser_peek_token (parser)->value;
1820 c_parser_consume_token (parser);
1821 parser->lex_untranslated_string = false;
1822 break;
1823 default:
1824 c_parser_error (parser, "expected string literal");
1825 parser->lex_untranslated_string = false;
1826 return;
1828 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1830 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1832 error_at (value_loc, "expression in static assertion is not an integer");
1833 return;
1835 if (TREE_CODE (value) != INTEGER_CST)
1837 value = c_fully_fold (value, false, NULL);
1838 if (TREE_CODE (value) == INTEGER_CST)
1839 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1840 "is not an integer constant expression");
1842 if (TREE_CODE (value) != INTEGER_CST)
1844 error_at (value_loc, "expression in static assertion is not constant");
1845 return;
1847 constant_expression_warning (value);
1848 if (integer_zerop (value))
1849 error_at (assert_loc, "static assertion failed: %E", string);
1852 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1853 6.7), adding them to SPECS (which may already include some).
1854 Storage class specifiers are accepted iff SCSPEC_OK; type
1855 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1856 the start iff START_ATTR_OK.
1858 declaration-specifiers:
1859 storage-class-specifier declaration-specifiers[opt]
1860 type-specifier declaration-specifiers[opt]
1861 type-qualifier declaration-specifiers[opt]
1862 function-specifier declaration-specifiers[opt]
1864 Function specifiers (inline) are from C99, and are currently
1865 handled as storage class specifiers, as is __thread.
1867 C90 6.5.1, C99 6.7.1:
1868 storage-class-specifier:
1869 typedef
1870 extern
1871 static
1872 auto
1873 register
1875 C99 6.7.4:
1876 function-specifier:
1877 inline
1879 C90 6.5.2, C99 6.7.2:
1880 type-specifier:
1881 void
1882 char
1883 short
1885 long
1886 float
1887 double
1888 signed
1889 unsigned
1890 _Bool
1891 _Complex
1892 [_Imaginary removed in C99 TC2]
1893 struct-or-union-specifier
1894 enum-specifier
1895 typedef-name
1897 (_Bool and _Complex are new in C99.)
1899 C90 6.5.3, C99 6.7.3:
1901 type-qualifier:
1902 const
1903 restrict
1904 volatile
1905 address-space-qualifier
1907 (restrict is new in C99.)
1909 GNU extensions:
1911 declaration-specifiers:
1912 attributes declaration-specifiers[opt]
1914 type-qualifier:
1915 address-space
1917 address-space:
1918 identifier recognized by the target
1920 storage-class-specifier:
1921 __thread
1923 type-specifier:
1924 typeof-specifier
1925 __int128
1926 _Decimal32
1927 _Decimal64
1928 _Decimal128
1929 _Fract
1930 _Accum
1931 _Sat
1933 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1934 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1936 Objective-C:
1938 type-specifier:
1939 class-name objc-protocol-refs[opt]
1940 typedef-name objc-protocol-refs
1941 objc-protocol-refs
1944 static void
1945 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1946 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1947 enum c_lookahead_kind la)
1949 bool attrs_ok = start_attr_ok;
1950 bool seen_type = specs->typespec_kind != ctsk_none;
1952 if (!typespec_ok)
1953 gcc_assert (la == cla_prefer_id);
1955 while (c_parser_next_token_is (parser, CPP_NAME)
1956 || c_parser_next_token_is (parser, CPP_KEYWORD)
1957 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1959 struct c_typespec t;
1960 tree attrs;
1961 location_t loc = c_parser_peek_token (parser)->location;
1963 /* If we cannot accept a type, exit if the next token must start
1964 one. Also, if we already have seen a tagged definition,
1965 a typename would be an error anyway and likely the user
1966 has simply forgotten a semicolon, so we exit. */
1967 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
1968 && c_parser_next_tokens_start_typename (parser, la)
1969 && !c_parser_next_token_is_qualifier (parser))
1970 break;
1972 if (c_parser_next_token_is (parser, CPP_NAME))
1974 tree value = c_parser_peek_token (parser)->value;
1975 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1977 if (kind == C_ID_ADDRSPACE)
1979 addr_space_t as
1980 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
1981 declspecs_add_addrspace (specs, as);
1982 c_parser_consume_token (parser);
1983 attrs_ok = true;
1984 continue;
1987 gcc_assert (!c_parser_next_token_is_qualifier (parser));
1989 /* If we cannot accept a type, and the next token must start one,
1990 exit. Do the same if we already have seen a tagged definition,
1991 since it would be an error anyway and likely the user has simply
1992 forgotten a semicolon. */
1993 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
1994 break;
1996 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
1997 a C_ID_CLASSNAME. */
1998 c_parser_consume_token (parser);
1999 seen_type = true;
2000 attrs_ok = true;
2001 if (kind == C_ID_ID)
2003 error ("unknown type name %qE", value);
2004 t.kind = ctsk_typedef;
2005 t.spec = error_mark_node;
2007 else if (kind == C_ID_TYPENAME
2008 && (!c_dialect_objc ()
2009 || c_parser_next_token_is_not (parser, CPP_LESS)))
2011 t.kind = ctsk_typedef;
2012 /* For a typedef name, record the meaning, not the name.
2013 In case of 'foo foo, bar;'. */
2014 t.spec = lookup_name (value);
2016 else
2018 tree proto = NULL_TREE;
2019 gcc_assert (c_dialect_objc ());
2020 t.kind = ctsk_objc;
2021 if (c_parser_next_token_is (parser, CPP_LESS))
2022 proto = c_parser_objc_protocol_refs (parser);
2023 t.spec = objc_get_protocol_qualified_type (value, proto);
2025 t.expr = NULL_TREE;
2026 t.expr_const_operands = true;
2027 declspecs_add_type (loc, specs, t);
2028 continue;
2030 if (c_parser_next_token_is (parser, CPP_LESS))
2032 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2033 nisse@lysator.liu.se. */
2034 tree proto;
2035 gcc_assert (c_dialect_objc ());
2036 if (!typespec_ok || seen_type)
2037 break;
2038 proto = c_parser_objc_protocol_refs (parser);
2039 t.kind = ctsk_objc;
2040 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2041 t.expr = NULL_TREE;
2042 t.expr_const_operands = true;
2043 declspecs_add_type (loc, specs, t);
2044 continue;
2046 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2047 switch (c_parser_peek_token (parser)->keyword)
2049 case RID_STATIC:
2050 case RID_EXTERN:
2051 case RID_REGISTER:
2052 case RID_TYPEDEF:
2053 case RID_INLINE:
2054 case RID_AUTO:
2055 case RID_THREAD:
2056 if (!scspec_ok)
2057 goto out;
2058 attrs_ok = true;
2059 /* TODO: Distinguish between function specifiers (inline)
2060 and storage class specifiers, either here or in
2061 declspecs_add_scspec. */
2062 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2063 c_parser_consume_token (parser);
2064 break;
2065 case RID_UNSIGNED:
2066 case RID_LONG:
2067 case RID_INT128:
2068 case RID_SHORT:
2069 case RID_SIGNED:
2070 case RID_COMPLEX:
2071 case RID_INT:
2072 case RID_CHAR:
2073 case RID_FLOAT:
2074 case RID_DOUBLE:
2075 case RID_VOID:
2076 case RID_DFLOAT32:
2077 case RID_DFLOAT64:
2078 case RID_DFLOAT128:
2079 case RID_BOOL:
2080 case RID_FRACT:
2081 case RID_ACCUM:
2082 case RID_SAT:
2083 if (!typespec_ok)
2084 goto out;
2085 attrs_ok = true;
2086 seen_type = true;
2087 if (c_dialect_objc ())
2088 parser->objc_need_raw_identifier = true;
2089 t.kind = ctsk_resword;
2090 t.spec = c_parser_peek_token (parser)->value;
2091 t.expr = NULL_TREE;
2092 t.expr_const_operands = true;
2093 declspecs_add_type (loc, specs, t);
2094 c_parser_consume_token (parser);
2095 break;
2096 case RID_ENUM:
2097 if (!typespec_ok)
2098 goto out;
2099 attrs_ok = true;
2100 seen_type = true;
2101 t = c_parser_enum_specifier (parser);
2102 declspecs_add_type (loc, specs, t);
2103 break;
2104 case RID_STRUCT:
2105 case RID_UNION:
2106 if (!typespec_ok)
2107 goto out;
2108 attrs_ok = true;
2109 seen_type = true;
2110 t = c_parser_struct_or_union_specifier (parser);
2111 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2112 declspecs_add_type (loc, specs, t);
2113 break;
2114 case RID_TYPEOF:
2115 /* ??? The old parser rejected typeof after other type
2116 specifiers, but is a syntax error the best way of
2117 handling this? */
2118 if (!typespec_ok || seen_type)
2119 goto out;
2120 attrs_ok = true;
2121 seen_type = true;
2122 t = c_parser_typeof_specifier (parser);
2123 declspecs_add_type (loc, specs, t);
2124 break;
2125 case RID_CONST:
2126 case RID_VOLATILE:
2127 case RID_RESTRICT:
2128 attrs_ok = true;
2129 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2130 c_parser_consume_token (parser);
2131 break;
2132 case RID_ATTRIBUTE:
2133 if (!attrs_ok)
2134 goto out;
2135 attrs = c_parser_attributes (parser);
2136 declspecs_add_attrs (specs, attrs);
2137 break;
2138 default:
2139 goto out;
2142 out: ;
2145 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2147 enum-specifier:
2148 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2149 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2150 enum attributes[opt] identifier
2152 The form with trailing comma is new in C99. The forms with
2153 attributes are GNU extensions. In GNU C, we accept any expression
2154 without commas in the syntax (assignment expressions, not just
2155 conditional expressions); assignment expressions will be diagnosed
2156 as non-constant.
2158 enumerator-list:
2159 enumerator
2160 enumerator-list , enumerator
2162 enumerator:
2163 enumeration-constant
2164 enumeration-constant = constant-expression
2167 static struct c_typespec
2168 c_parser_enum_specifier (c_parser *parser)
2170 struct c_typespec ret;
2171 tree attrs;
2172 tree ident = NULL_TREE;
2173 location_t enum_loc;
2174 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2175 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2176 enum_loc = c_parser_peek_token (parser)->location;
2177 c_parser_consume_token (parser);
2178 attrs = c_parser_attributes (parser);
2179 enum_loc = c_parser_peek_token (parser)->location;
2180 /* Set the location in case we create a decl now. */
2181 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2182 if (c_parser_next_token_is (parser, CPP_NAME))
2184 ident = c_parser_peek_token (parser)->value;
2185 ident_loc = c_parser_peek_token (parser)->location;
2186 enum_loc = ident_loc;
2187 c_parser_consume_token (parser);
2189 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2191 /* Parse an enum definition. */
2192 struct c_enum_contents the_enum;
2193 tree type = start_enum (enum_loc, &the_enum, ident);
2194 tree postfix_attrs;
2195 /* We chain the enumerators in reverse order, then put them in
2196 forward order at the end. */
2197 tree values = NULL_TREE;
2198 c_parser_consume_token (parser);
2199 while (true)
2201 tree enum_id;
2202 tree enum_value;
2203 tree enum_decl;
2204 bool seen_comma;
2205 c_token *token;
2206 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2207 location_t decl_loc, value_loc;
2208 if (c_parser_next_token_is_not (parser, CPP_NAME))
2210 c_parser_error (parser, "expected identifier");
2211 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2212 values = error_mark_node;
2213 break;
2215 token = c_parser_peek_token (parser);
2216 enum_id = token->value;
2217 /* Set the location in case we create a decl now. */
2218 c_parser_set_source_position_from_token (token);
2219 decl_loc = value_loc = token->location;
2220 c_parser_consume_token (parser);
2221 if (c_parser_next_token_is (parser, CPP_EQ))
2223 c_parser_consume_token (parser);
2224 value_loc = c_parser_peek_token (parser)->location;
2225 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2227 else
2228 enum_value = NULL_TREE;
2229 enum_decl = build_enumerator (decl_loc, value_loc,
2230 &the_enum, enum_id, enum_value);
2231 TREE_CHAIN (enum_decl) = values;
2232 values = enum_decl;
2233 seen_comma = false;
2234 if (c_parser_next_token_is (parser, CPP_COMMA))
2236 comma_loc = c_parser_peek_token (parser)->location;
2237 seen_comma = true;
2238 c_parser_consume_token (parser);
2240 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2242 if (seen_comma && !flag_isoc99)
2243 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2244 c_parser_consume_token (parser);
2245 break;
2247 if (!seen_comma)
2249 c_parser_error (parser, "expected %<,%> or %<}%>");
2250 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2251 values = error_mark_node;
2252 break;
2255 postfix_attrs = c_parser_attributes (parser);
2256 ret.spec = finish_enum (type, nreverse (values),
2257 chainon (attrs, postfix_attrs));
2258 ret.kind = ctsk_tagdef;
2259 ret.expr = NULL_TREE;
2260 ret.expr_const_operands = true;
2261 return ret;
2263 else if (!ident)
2265 c_parser_error (parser, "expected %<{%>");
2266 ret.spec = error_mark_node;
2267 ret.kind = ctsk_tagref;
2268 ret.expr = NULL_TREE;
2269 ret.expr_const_operands = true;
2270 return ret;
2272 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2273 /* In ISO C, enumerated types can be referred to only if already
2274 defined. */
2275 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2277 gcc_assert (ident);
2278 pedwarn (enum_loc, OPT_pedantic,
2279 "ISO C forbids forward references to %<enum%> types");
2281 return ret;
2284 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2286 struct-or-union-specifier:
2287 struct-or-union attributes[opt] identifier[opt]
2288 { struct-contents } attributes[opt]
2289 struct-or-union attributes[opt] identifier
2291 struct-contents:
2292 struct-declaration-list
2294 struct-declaration-list:
2295 struct-declaration ;
2296 struct-declaration-list struct-declaration ;
2298 GNU extensions:
2300 struct-contents:
2301 empty
2302 struct-declaration
2303 struct-declaration-list struct-declaration
2305 struct-declaration-list:
2306 struct-declaration-list ;
2309 (Note that in the syntax here, unlike that in ISO C, the semicolons
2310 are included here rather than in struct-declaration, in order to
2311 describe the syntax with extra semicolons and missing semicolon at
2312 end.)
2314 Objective-C:
2316 struct-declaration-list:
2317 @defs ( class-name )
2319 (Note this does not include a trailing semicolon, but can be
2320 followed by further declarations, and gets a pedwarn-if-pedantic
2321 when followed by a semicolon.) */
2323 static struct c_typespec
2324 c_parser_struct_or_union_specifier (c_parser *parser)
2326 struct c_typespec ret;
2327 tree attrs;
2328 tree ident = NULL_TREE;
2329 location_t struct_loc;
2330 location_t ident_loc = UNKNOWN_LOCATION;
2331 enum tree_code code;
2332 switch (c_parser_peek_token (parser)->keyword)
2334 case RID_STRUCT:
2335 code = RECORD_TYPE;
2336 break;
2337 case RID_UNION:
2338 code = UNION_TYPE;
2339 break;
2340 default:
2341 gcc_unreachable ();
2343 struct_loc = c_parser_peek_token (parser)->location;
2344 c_parser_consume_token (parser);
2345 attrs = c_parser_attributes (parser);
2347 /* Set the location in case we create a decl now. */
2348 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2350 if (c_parser_next_token_is (parser, CPP_NAME))
2352 ident = c_parser_peek_token (parser)->value;
2353 ident_loc = c_parser_peek_token (parser)->location;
2354 struct_loc = ident_loc;
2355 c_parser_consume_token (parser);
2357 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2359 /* Parse a struct or union definition. Start the scope of the
2360 tag before parsing components. */
2361 struct c_struct_parse_info *struct_info;
2362 tree type = start_struct (struct_loc, code, ident, &struct_info);
2363 tree postfix_attrs;
2364 /* We chain the components in reverse order, then put them in
2365 forward order at the end. Each struct-declaration may
2366 declare multiple components (comma-separated), so we must use
2367 chainon to join them, although when parsing each
2368 struct-declaration we can use TREE_CHAIN directly.
2370 The theory behind all this is that there will be more
2371 semicolon separated fields than comma separated fields, and
2372 so we'll be minimizing the number of node traversals required
2373 by chainon. */
2374 tree contents = NULL_TREE;
2375 c_parser_consume_token (parser);
2376 /* Handle the Objective-C @defs construct,
2377 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2378 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2380 tree name;
2381 gcc_assert (c_dialect_objc ());
2382 c_parser_consume_token (parser);
2383 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2384 goto end_at_defs;
2385 if (c_parser_next_token_is (parser, CPP_NAME)
2386 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2388 name = c_parser_peek_token (parser)->value;
2389 c_parser_consume_token (parser);
2391 else
2393 c_parser_error (parser, "expected class name");
2394 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2395 goto end_at_defs;
2397 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2398 "expected %<)%>");
2399 contents = nreverse (objc_get_class_ivars (name));
2401 end_at_defs:
2402 /* Parse the struct-declarations and semicolons. Problems with
2403 semicolons are diagnosed here; empty structures are diagnosed
2404 elsewhere. */
2405 while (true)
2407 tree decls;
2408 /* Parse any stray semicolon. */
2409 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2411 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2412 "extra semicolon in struct or union specified");
2413 c_parser_consume_token (parser);
2414 continue;
2416 /* Stop if at the end of the struct or union contents. */
2417 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2419 c_parser_consume_token (parser);
2420 break;
2422 /* Accept #pragmas at struct scope. */
2423 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2425 c_parser_pragma (parser, pragma_external);
2426 continue;
2428 /* Parse some comma-separated declarations, but not the
2429 trailing semicolon if any. */
2430 decls = c_parser_struct_declaration (parser);
2431 contents = chainon (decls, contents);
2432 /* If no semicolon follows, either we have a parse error or
2433 are at the end of the struct or union and should
2434 pedwarn. */
2435 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2436 c_parser_consume_token (parser);
2437 else
2439 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2440 pedwarn (c_parser_peek_token (parser)->location, 0,
2441 "no semicolon at end of struct or union");
2442 else if (parser->error
2443 || !c_parser_next_token_starts_declspecs (parser))
2445 c_parser_error (parser, "expected %<;%>");
2446 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2447 break;
2450 /* If we come here, we have already emitted an error
2451 for an expected `;', identifier or `(', and we also
2452 recovered already. Go on with the next field. */
2455 postfix_attrs = c_parser_attributes (parser);
2456 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2457 chainon (attrs, postfix_attrs), struct_info);
2458 ret.kind = ctsk_tagdef;
2459 ret.expr = NULL_TREE;
2460 ret.expr_const_operands = true;
2461 return ret;
2463 else if (!ident)
2465 c_parser_error (parser, "expected %<{%>");
2466 ret.spec = error_mark_node;
2467 ret.kind = ctsk_tagref;
2468 ret.expr = NULL_TREE;
2469 ret.expr_const_operands = true;
2470 return ret;
2472 ret = parser_xref_tag (ident_loc, code, ident);
2473 return ret;
2476 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2477 the trailing semicolon.
2479 struct-declaration:
2480 specifier-qualifier-list struct-declarator-list
2481 static_assert-declaration-no-semi
2483 specifier-qualifier-list:
2484 type-specifier specifier-qualifier-list[opt]
2485 type-qualifier specifier-qualifier-list[opt]
2486 attributes specifier-qualifier-list[opt]
2488 struct-declarator-list:
2489 struct-declarator
2490 struct-declarator-list , attributes[opt] struct-declarator
2492 struct-declarator:
2493 declarator attributes[opt]
2494 declarator[opt] : constant-expression attributes[opt]
2496 GNU extensions:
2498 struct-declaration:
2499 __extension__ struct-declaration
2500 specifier-qualifier-list
2502 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2503 of attributes where shown is a GNU extension. In GNU C, we accept
2504 any expression without commas in the syntax (assignment
2505 expressions, not just conditional expressions); assignment
2506 expressions will be diagnosed as non-constant. */
2508 static tree
2509 c_parser_struct_declaration (c_parser *parser)
2511 struct c_declspecs *specs;
2512 tree prefix_attrs;
2513 tree all_prefix_attrs;
2514 tree decls;
2515 location_t decl_loc;
2516 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2518 int ext;
2519 tree decl;
2520 ext = disable_extension_diagnostics ();
2521 c_parser_consume_token (parser);
2522 decl = c_parser_struct_declaration (parser);
2523 restore_extension_diagnostics (ext);
2524 return decl;
2526 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2528 c_parser_static_assert_declaration_no_semi (parser);
2529 return NULL_TREE;
2531 specs = build_null_declspecs ();
2532 decl_loc = c_parser_peek_token (parser)->location;
2533 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2534 if (parser->error)
2535 return NULL_TREE;
2536 if (!specs->declspecs_seen_p)
2538 c_parser_error (parser, "expected specifier-qualifier-list");
2539 return NULL_TREE;
2541 finish_declspecs (specs);
2542 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2543 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2545 tree ret;
2546 if (specs->typespec_kind == ctsk_none)
2548 pedwarn (decl_loc, OPT_pedantic,
2549 "ISO C forbids member declarations with no members");
2550 shadow_tag_warned (specs, pedantic);
2551 ret = NULL_TREE;
2553 else
2555 /* Support for unnamed structs or unions as members of
2556 structs or unions (which is [a] useful and [b] supports
2557 MS P-SDK). */
2558 tree attrs = NULL;
2560 ret = grokfield (c_parser_peek_token (parser)->location,
2561 build_id_declarator (NULL_TREE), specs,
2562 NULL_TREE, &attrs);
2563 if (ret)
2564 decl_attributes (&ret, attrs, 0);
2566 return ret;
2569 /* Provide better error recovery. Note that a type name here is valid,
2570 and will be treated as a field name. */
2571 if (specs->typespec_kind == ctsk_tagdef
2572 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2573 && c_parser_next_token_starts_declspecs (parser)
2574 && !c_parser_next_token_is (parser, CPP_NAME))
2576 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2577 parser->error = false;
2578 return NULL_TREE;
2581 pending_xref_error ();
2582 prefix_attrs = specs->attrs;
2583 all_prefix_attrs = prefix_attrs;
2584 specs->attrs = NULL_TREE;
2585 decls = NULL_TREE;
2586 while (true)
2588 /* Declaring one or more declarators or un-named bit-fields. */
2589 struct c_declarator *declarator;
2590 bool dummy = false;
2591 if (c_parser_next_token_is (parser, CPP_COLON))
2592 declarator = build_id_declarator (NULL_TREE);
2593 else
2594 declarator = c_parser_declarator (parser,
2595 specs->typespec_kind != ctsk_none,
2596 C_DTR_NORMAL, &dummy);
2597 if (declarator == NULL)
2599 c_parser_skip_to_end_of_block_or_statement (parser);
2600 break;
2602 if (c_parser_next_token_is (parser, CPP_COLON)
2603 || c_parser_next_token_is (parser, CPP_COMMA)
2604 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2605 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2606 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2608 tree postfix_attrs = NULL_TREE;
2609 tree width = NULL_TREE;
2610 tree d;
2611 if (c_parser_next_token_is (parser, CPP_COLON))
2613 c_parser_consume_token (parser);
2614 width = c_parser_expr_no_commas (parser, NULL).value;
2616 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2617 postfix_attrs = c_parser_attributes (parser);
2618 d = grokfield (c_parser_peek_token (parser)->location,
2619 declarator, specs, width, &all_prefix_attrs);
2620 decl_attributes (&d, chainon (postfix_attrs,
2621 all_prefix_attrs), 0);
2622 DECL_CHAIN (d) = decls;
2623 decls = d;
2624 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2625 all_prefix_attrs = chainon (c_parser_attributes (parser),
2626 prefix_attrs);
2627 else
2628 all_prefix_attrs = prefix_attrs;
2629 if (c_parser_next_token_is (parser, CPP_COMMA))
2630 c_parser_consume_token (parser);
2631 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2632 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2634 /* Semicolon consumed in caller. */
2635 break;
2637 else
2639 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2640 break;
2643 else
2645 c_parser_error (parser,
2646 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2647 "%<__attribute__%>");
2648 break;
2651 return decls;
2654 /* Parse a typeof specifier (a GNU extension).
2656 typeof-specifier:
2657 typeof ( expression )
2658 typeof ( type-name )
2661 static struct c_typespec
2662 c_parser_typeof_specifier (c_parser *parser)
2664 struct c_typespec ret;
2665 ret.kind = ctsk_typeof;
2666 ret.spec = error_mark_node;
2667 ret.expr = NULL_TREE;
2668 ret.expr_const_operands = true;
2669 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2670 c_parser_consume_token (parser);
2671 c_inhibit_evaluation_warnings++;
2672 in_typeof++;
2673 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2675 c_inhibit_evaluation_warnings--;
2676 in_typeof--;
2677 return ret;
2679 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2681 struct c_type_name *type = c_parser_type_name (parser);
2682 c_inhibit_evaluation_warnings--;
2683 in_typeof--;
2684 if (type != NULL)
2686 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2687 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2690 else
2692 bool was_vm;
2693 location_t here = c_parser_peek_token (parser)->location;
2694 struct c_expr expr = c_parser_expression (parser);
2695 c_inhibit_evaluation_warnings--;
2696 in_typeof--;
2697 if (TREE_CODE (expr.value) == COMPONENT_REF
2698 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2699 error_at (here, "%<typeof%> applied to a bit-field");
2700 mark_exp_read (expr.value);
2701 ret.spec = TREE_TYPE (expr.value);
2702 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2703 /* This is returned with the type so that when the type is
2704 evaluated, this can be evaluated. */
2705 if (was_vm)
2706 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2707 pop_maybe_used (was_vm);
2709 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2710 return ret;
2713 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2714 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2715 be redeclared; otherwise it may not. KIND indicates which kind of
2716 declarator is wanted. Returns a valid declarator except in the
2717 case of a syntax error in which case NULL is returned. *SEEN_ID is
2718 set to true if an identifier being declared is seen; this is used
2719 to diagnose bad forms of abstract array declarators and to
2720 determine whether an identifier list is syntactically permitted.
2722 declarator:
2723 pointer[opt] direct-declarator
2725 direct-declarator:
2726 identifier
2727 ( attributes[opt] declarator )
2728 direct-declarator array-declarator
2729 direct-declarator ( parameter-type-list )
2730 direct-declarator ( identifier-list[opt] )
2732 pointer:
2733 * type-qualifier-list[opt]
2734 * type-qualifier-list[opt] pointer
2736 type-qualifier-list:
2737 type-qualifier
2738 attributes
2739 type-qualifier-list type-qualifier
2740 type-qualifier-list attributes
2742 parameter-type-list:
2743 parameter-list
2744 parameter-list , ...
2746 parameter-list:
2747 parameter-declaration
2748 parameter-list , parameter-declaration
2750 parameter-declaration:
2751 declaration-specifiers declarator attributes[opt]
2752 declaration-specifiers abstract-declarator[opt] attributes[opt]
2754 identifier-list:
2755 identifier
2756 identifier-list , identifier
2758 abstract-declarator:
2759 pointer
2760 pointer[opt] direct-abstract-declarator
2762 direct-abstract-declarator:
2763 ( attributes[opt] abstract-declarator )
2764 direct-abstract-declarator[opt] array-declarator
2765 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2767 GNU extensions:
2769 direct-declarator:
2770 direct-declarator ( parameter-forward-declarations
2771 parameter-type-list[opt] )
2773 direct-abstract-declarator:
2774 direct-abstract-declarator[opt] ( parameter-forward-declarations
2775 parameter-type-list[opt] )
2777 parameter-forward-declarations:
2778 parameter-list ;
2779 parameter-forward-declarations parameter-list ;
2781 The uses of attributes shown above are GNU extensions.
2783 Some forms of array declarator are not included in C99 in the
2784 syntax for abstract declarators; these are disallowed elsewhere.
2785 This may be a defect (DR#289).
2787 This function also accepts an omitted abstract declarator as being
2788 an abstract declarator, although not part of the formal syntax. */
2790 static struct c_declarator *
2791 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2792 bool *seen_id)
2794 /* Parse any initial pointer part. */
2795 if (c_parser_next_token_is (parser, CPP_MULT))
2797 struct c_declspecs *quals_attrs = build_null_declspecs ();
2798 struct c_declarator *inner;
2799 c_parser_consume_token (parser);
2800 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2801 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2802 if (inner == NULL)
2803 return NULL;
2804 else
2805 return make_pointer_declarator (quals_attrs, inner);
2807 /* Now we have a direct declarator, direct abstract declarator or
2808 nothing (which counts as a direct abstract declarator here). */
2809 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2812 /* Parse a direct declarator or direct abstract declarator; arguments
2813 as c_parser_declarator. */
2815 static struct c_declarator *
2816 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2817 bool *seen_id)
2819 /* The direct declarator must start with an identifier (possibly
2820 omitted) or a parenthesized declarator (possibly abstract). In
2821 an ordinary declarator, initial parentheses must start a
2822 parenthesized declarator. In an abstract declarator or parameter
2823 declarator, they could start a parenthesized declarator or a
2824 parameter list. To tell which, the open parenthesis and any
2825 following attributes must be read. If a declaration specifier
2826 follows, then it is a parameter list; if the specifier is a
2827 typedef name, there might be an ambiguity about redeclaring it,
2828 which is resolved in the direction of treating it as a typedef
2829 name. If a close parenthesis follows, it is also an empty
2830 parameter list, as the syntax does not permit empty abstract
2831 declarators. Otherwise, it is a parenthesized declarator (in
2832 which case the analysis may be repeated inside it, recursively).
2834 ??? There is an ambiguity in a parameter declaration "int
2835 (__attribute__((foo)) x)", where x is not a typedef name: it
2836 could be an abstract declarator for a function, or declare x with
2837 parentheses. The proper resolution of this ambiguity needs
2838 documenting. At present we follow an accident of the old
2839 parser's implementation, whereby the first parameter must have
2840 some declaration specifiers other than just attributes. Thus as
2841 a parameter declaration it is treated as a parenthesized
2842 parameter named x, and as an abstract declarator it is
2843 rejected.
2845 ??? Also following the old parser, attributes inside an empty
2846 parameter list are ignored, making it a list not yielding a
2847 prototype, rather than giving an error or making it have one
2848 parameter with implicit type int.
2850 ??? Also following the old parser, typedef names may be
2851 redeclared in declarators, but not Objective-C class names. */
2853 if (kind != C_DTR_ABSTRACT
2854 && c_parser_next_token_is (parser, CPP_NAME)
2855 && ((type_seen_p
2856 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2857 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2858 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2860 struct c_declarator *inner
2861 = build_id_declarator (c_parser_peek_token (parser)->value);
2862 *seen_id = true;
2863 inner->id_loc = c_parser_peek_token (parser)->location;
2864 c_parser_consume_token (parser);
2865 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2868 if (kind != C_DTR_NORMAL
2869 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2871 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2872 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2875 /* Either we are at the end of an abstract declarator, or we have
2876 parentheses. */
2878 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2880 tree attrs;
2881 struct c_declarator *inner;
2882 c_parser_consume_token (parser);
2883 attrs = c_parser_attributes (parser);
2884 if (kind != C_DTR_NORMAL
2885 && (c_parser_next_token_starts_declspecs (parser)
2886 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2888 struct c_arg_info *args
2889 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2890 attrs);
2891 if (args == NULL)
2892 return NULL;
2893 else
2895 inner
2896 = build_function_declarator (args,
2897 build_id_declarator (NULL_TREE));
2898 return c_parser_direct_declarator_inner (parser, *seen_id,
2899 inner);
2902 /* A parenthesized declarator. */
2903 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2904 if (inner != NULL && attrs != NULL)
2905 inner = build_attrs_declarator (attrs, inner);
2906 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2908 c_parser_consume_token (parser);
2909 if (inner == NULL)
2910 return NULL;
2911 else
2912 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2914 else
2916 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2917 "expected %<)%>");
2918 return NULL;
2921 else
2923 if (kind == C_DTR_NORMAL)
2925 c_parser_error (parser, "expected identifier or %<(%>");
2926 return NULL;
2928 else
2929 return build_id_declarator (NULL_TREE);
2933 /* Parse part of a direct declarator or direct abstract declarator,
2934 given that some (in INNER) has already been parsed; ID_PRESENT is
2935 true if an identifier is present, false for an abstract
2936 declarator. */
2938 static struct c_declarator *
2939 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2940 struct c_declarator *inner)
2942 /* Parse a sequence of array declarators and parameter lists. */
2943 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2945 location_t brace_loc = c_parser_peek_token (parser)->location;
2946 struct c_declarator *declarator;
2947 struct c_declspecs *quals_attrs = build_null_declspecs ();
2948 bool static_seen;
2949 bool star_seen;
2950 tree dimen;
2951 c_parser_consume_token (parser);
2952 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2953 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2954 if (static_seen)
2955 c_parser_consume_token (parser);
2956 if (static_seen && !quals_attrs->declspecs_seen_p)
2957 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2958 if (!quals_attrs->declspecs_seen_p)
2959 quals_attrs = NULL;
2960 /* If "static" is present, there must be an array dimension.
2961 Otherwise, there may be a dimension, "*", or no
2962 dimension. */
2963 if (static_seen)
2965 star_seen = false;
2966 dimen = c_parser_expr_no_commas (parser, NULL).value;
2968 else
2970 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2972 dimen = NULL_TREE;
2973 star_seen = false;
2975 else if (c_parser_next_token_is (parser, CPP_MULT))
2977 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2979 dimen = NULL_TREE;
2980 star_seen = true;
2981 c_parser_consume_token (parser);
2983 else
2985 star_seen = false;
2986 dimen = c_parser_expr_no_commas (parser, NULL).value;
2989 else
2991 star_seen = false;
2992 dimen = c_parser_expr_no_commas (parser, NULL).value;
2995 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2996 c_parser_consume_token (parser);
2997 else
2999 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3000 "expected %<]%>");
3001 return NULL;
3003 if (dimen)
3004 mark_exp_read (dimen);
3005 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3006 static_seen, star_seen);
3007 if (declarator == NULL)
3008 return NULL;
3009 inner = set_array_declarator_inner (declarator, inner);
3010 return c_parser_direct_declarator_inner (parser, id_present, inner);
3012 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3014 tree attrs;
3015 struct c_arg_info *args;
3016 c_parser_consume_token (parser);
3017 attrs = c_parser_attributes (parser);
3018 args = c_parser_parms_declarator (parser, id_present, attrs);
3019 if (args == NULL)
3020 return NULL;
3021 else
3023 inner = build_function_declarator (args, inner);
3024 return c_parser_direct_declarator_inner (parser, id_present, inner);
3027 return inner;
3030 /* Parse a parameter list or identifier list, including the closing
3031 parenthesis but not the opening one. ATTRS are the attributes at
3032 the start of the list. ID_LIST_OK is true if an identifier list is
3033 acceptable; such a list must not have attributes at the start. */
3035 static struct c_arg_info *
3036 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3038 push_scope ();
3039 declare_parm_level ();
3040 /* If the list starts with an identifier, it is an identifier list.
3041 Otherwise, it is either a prototype list or an empty list. */
3042 if (id_list_ok
3043 && !attrs
3044 && c_parser_next_token_is (parser, CPP_NAME)
3045 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3047 /* Look ahead to detect typos in type names. */
3048 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3049 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3050 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3051 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3053 tree list = NULL_TREE, *nextp = &list;
3054 while (c_parser_next_token_is (parser, CPP_NAME)
3055 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3057 *nextp = build_tree_list (NULL_TREE,
3058 c_parser_peek_token (parser)->value);
3059 nextp = & TREE_CHAIN (*nextp);
3060 c_parser_consume_token (parser);
3061 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3062 break;
3063 c_parser_consume_token (parser);
3064 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3066 c_parser_error (parser, "expected identifier");
3067 break;
3070 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3072 struct c_arg_info *ret = build_arg_info ();
3073 ret->types = list;
3074 c_parser_consume_token (parser);
3075 pop_scope ();
3076 return ret;
3078 else
3080 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3081 "expected %<)%>");
3082 pop_scope ();
3083 return NULL;
3086 else
3088 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
3089 pop_scope ();
3090 return ret;
3094 /* Parse a parameter list (possibly empty), including the closing
3095 parenthesis but not the opening one. ATTRS are the attributes at
3096 the start of the list. */
3098 static struct c_arg_info *
3099 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
3101 bool bad_parm = false;
3102 /* ??? Following the old parser, forward parameter declarations may
3103 use abstract declarators, and if no real parameter declarations
3104 follow the forward declarations then this is not diagnosed. Also
3105 note as above that attributes are ignored as the only contents of
3106 the parentheses, or as the only contents after forward
3107 declarations. */
3108 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3110 struct c_arg_info *ret = build_arg_info ();
3111 c_parser_consume_token (parser);
3112 return ret;
3114 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3116 struct c_arg_info *ret = build_arg_info ();
3117 /* Suppress -Wold-style-definition for this case. */
3118 ret->types = error_mark_node;
3119 error_at (c_parser_peek_token (parser)->location,
3120 "ISO C requires a named argument before %<...%>");
3121 c_parser_consume_token (parser);
3122 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3124 c_parser_consume_token (parser);
3125 return ret;
3127 else
3129 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3130 "expected %<)%>");
3131 return NULL;
3134 /* Nonempty list of parameters, either terminated with semicolon
3135 (forward declarations; recurse) or with close parenthesis (normal
3136 function) or with ", ... )" (variadic function). */
3137 while (true)
3139 /* Parse a parameter. */
3140 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3141 attrs = NULL_TREE;
3142 if (parm == NULL)
3143 bad_parm = true;
3144 else
3145 push_parm_decl (parm);
3146 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3148 tree new_attrs;
3149 c_parser_consume_token (parser);
3150 mark_forward_parm_decls ();
3151 new_attrs = c_parser_attributes (parser);
3152 return c_parser_parms_list_declarator (parser, new_attrs);
3154 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3156 c_parser_consume_token (parser);
3157 if (bad_parm)
3159 get_pending_sizes ();
3160 return NULL;
3162 else
3163 return get_parm_info (false);
3165 if (!c_parser_require (parser, CPP_COMMA,
3166 "expected %<;%>, %<,%> or %<)%>"))
3168 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3169 get_pending_sizes ();
3170 return NULL;
3172 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3174 c_parser_consume_token (parser);
3175 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3177 c_parser_consume_token (parser);
3178 if (bad_parm)
3180 get_pending_sizes ();
3181 return NULL;
3183 else
3184 return get_parm_info (true);
3186 else
3188 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3189 "expected %<)%>");
3190 get_pending_sizes ();
3191 return NULL;
3197 /* Parse a parameter declaration. ATTRS are the attributes at the
3198 start of the declaration if it is the first parameter. */
3200 static struct c_parm *
3201 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3203 struct c_declspecs *specs;
3204 struct c_declarator *declarator;
3205 tree prefix_attrs;
3206 tree postfix_attrs = NULL_TREE;
3207 bool dummy = false;
3208 if (!c_parser_next_token_starts_declspecs (parser))
3210 c_token *token = c_parser_peek_token (parser);
3211 if (parser->error)
3212 return NULL;
3213 c_parser_set_source_position_from_token (token);
3214 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3216 error ("unknown type name %qE", token->value);
3217 parser->error = true;
3219 /* ??? In some Objective-C cases '...' isn't applicable so there
3220 should be a different message. */
3221 else
3222 c_parser_error (parser,
3223 "expected declaration specifiers or %<...%>");
3224 c_parser_skip_to_end_of_parameter (parser);
3225 return NULL;
3227 specs = build_null_declspecs ();
3228 if (attrs)
3230 declspecs_add_attrs (specs, attrs);
3231 attrs = NULL_TREE;
3233 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3234 finish_declspecs (specs);
3235 pending_xref_error ();
3236 prefix_attrs = specs->attrs;
3237 specs->attrs = NULL_TREE;
3238 declarator = c_parser_declarator (parser,
3239 specs->typespec_kind != ctsk_none,
3240 C_DTR_PARM, &dummy);
3241 if (declarator == NULL)
3243 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3244 return NULL;
3246 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3247 postfix_attrs = c_parser_attributes (parser);
3248 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3249 declarator);
3252 /* Parse a string literal in an asm expression. It should not be
3253 translated, and wide string literals are an error although
3254 permitted by the syntax. This is a GNU extension.
3256 asm-string-literal:
3257 string-literal
3259 ??? At present, following the old parser, the caller needs to have
3260 set lex_untranslated_string to 1. It would be better to follow the
3261 C++ parser rather than using this kludge. */
3263 static tree
3264 c_parser_asm_string_literal (c_parser *parser)
3266 tree str;
3267 int save_flag = warn_overlength_strings;
3268 warn_overlength_strings = 0;
3269 if (c_parser_next_token_is (parser, CPP_STRING))
3271 str = c_parser_peek_token (parser)->value;
3272 c_parser_consume_token (parser);
3274 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3276 error_at (c_parser_peek_token (parser)->location,
3277 "wide string literal in %<asm%>");
3278 str = build_string (1, "");
3279 c_parser_consume_token (parser);
3281 else
3283 c_parser_error (parser, "expected string literal");
3284 str = NULL_TREE;
3286 warn_overlength_strings = save_flag;
3287 return str;
3290 /* Parse a simple asm expression. This is used in restricted
3291 contexts, where a full expression with inputs and outputs does not
3292 make sense. This is a GNU extension.
3294 simple-asm-expr:
3295 asm ( asm-string-literal )
3298 static tree
3299 c_parser_simple_asm_expr (c_parser *parser)
3301 tree str;
3302 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3303 /* ??? Follow the C++ parser rather than using the
3304 lex_untranslated_string kludge. */
3305 parser->lex_untranslated_string = true;
3306 c_parser_consume_token (parser);
3307 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3309 parser->lex_untranslated_string = false;
3310 return NULL_TREE;
3312 str = c_parser_asm_string_literal (parser);
3313 parser->lex_untranslated_string = false;
3314 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3317 return NULL_TREE;
3319 return str;
3322 /* Parse (possibly empty) attributes. This is a GNU extension.
3324 attributes:
3325 empty
3326 attributes attribute
3328 attribute:
3329 __attribute__ ( ( attribute-list ) )
3331 attribute-list:
3332 attrib
3333 attribute_list , attrib
3335 attrib:
3336 empty
3337 any-word
3338 any-word ( identifier )
3339 any-word ( identifier , nonempty-expr-list )
3340 any-word ( expr-list )
3342 where the "identifier" must not be declared as a type, and
3343 "any-word" may be any identifier (including one declared as a
3344 type), a reserved word storage class specifier, type specifier or
3345 type qualifier. ??? This still leaves out most reserved keywords
3346 (following the old parser), shouldn't we include them, and why not
3347 allow identifiers declared as types to start the arguments? */
3349 static tree
3350 c_parser_attributes (c_parser *parser)
3352 tree attrs = NULL_TREE;
3353 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3355 /* ??? Follow the C++ parser rather than using the
3356 lex_untranslated_string kludge. */
3357 parser->lex_untranslated_string = true;
3358 c_parser_consume_token (parser);
3359 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3361 parser->lex_untranslated_string = false;
3362 return attrs;
3364 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3366 parser->lex_untranslated_string = false;
3367 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3368 return attrs;
3370 /* Parse the attribute list. */
3371 while (c_parser_next_token_is (parser, CPP_COMMA)
3372 || c_parser_next_token_is (parser, CPP_NAME)
3373 || c_parser_next_token_is (parser, CPP_KEYWORD))
3375 tree attr, attr_name, attr_args;
3376 VEC(tree,gc) *expr_list;
3377 if (c_parser_next_token_is (parser, CPP_COMMA))
3379 c_parser_consume_token (parser);
3380 continue;
3382 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3384 /* ??? See comment above about what keywords are
3385 accepted here. */
3386 bool ok;
3387 switch (c_parser_peek_token (parser)->keyword)
3389 case RID_STATIC:
3390 case RID_UNSIGNED:
3391 case RID_LONG:
3392 case RID_INT128:
3393 case RID_CONST:
3394 case RID_EXTERN:
3395 case RID_REGISTER:
3396 case RID_TYPEDEF:
3397 case RID_SHORT:
3398 case RID_INLINE:
3399 case RID_VOLATILE:
3400 case RID_SIGNED:
3401 case RID_AUTO:
3402 case RID_RESTRICT:
3403 case RID_COMPLEX:
3404 case RID_THREAD:
3405 case RID_INT:
3406 case RID_CHAR:
3407 case RID_FLOAT:
3408 case RID_DOUBLE:
3409 case RID_VOID:
3410 case RID_DFLOAT32:
3411 case RID_DFLOAT64:
3412 case RID_DFLOAT128:
3413 case RID_BOOL:
3414 case RID_FRACT:
3415 case RID_ACCUM:
3416 case RID_SAT:
3417 ok = true;
3418 break;
3419 default:
3420 ok = false;
3421 break;
3423 if (!ok)
3424 break;
3425 /* Accept __attribute__((__const)) as __attribute__((const))
3426 etc. */
3427 attr_name
3428 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3430 else
3431 attr_name = c_parser_peek_token (parser)->value;
3432 c_parser_consume_token (parser);
3433 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3435 attr = build_tree_list (attr_name, NULL_TREE);
3436 attrs = chainon (attrs, attr);
3437 continue;
3439 c_parser_consume_token (parser);
3440 /* Parse the attribute contents. If they start with an
3441 identifier which is followed by a comma or close
3442 parenthesis, then the arguments start with that
3443 identifier; otherwise they are an expression list.
3444 In objective-c the identifier may be a classname. */
3445 if (c_parser_next_token_is (parser, CPP_NAME)
3446 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3447 || (c_dialect_objc ()
3448 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3449 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3450 || (c_parser_peek_2nd_token (parser)->type
3451 == CPP_CLOSE_PAREN)))
3453 tree arg1 = c_parser_peek_token (parser)->value;
3454 c_parser_consume_token (parser);
3455 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3456 attr_args = build_tree_list (NULL_TREE, arg1);
3457 else
3459 tree tree_list;
3460 c_parser_consume_token (parser);
3461 expr_list = c_parser_expr_list (parser, false, true, NULL);
3462 tree_list = build_tree_list_vec (expr_list);
3463 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3464 release_tree_vector (expr_list);
3467 else
3469 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3470 attr_args = NULL_TREE;
3471 else
3473 expr_list = c_parser_expr_list (parser, false, true, NULL);
3474 attr_args = build_tree_list_vec (expr_list);
3475 release_tree_vector (expr_list);
3478 attr = build_tree_list (attr_name, attr_args);
3479 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3480 c_parser_consume_token (parser);
3481 else
3483 parser->lex_untranslated_string = false;
3484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3485 "expected %<)%>");
3486 return attrs;
3488 attrs = chainon (attrs, attr);
3490 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3491 c_parser_consume_token (parser);
3492 else
3494 parser->lex_untranslated_string = false;
3495 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3496 "expected %<)%>");
3497 return attrs;
3499 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3500 c_parser_consume_token (parser);
3501 else
3503 parser->lex_untranslated_string = false;
3504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3505 "expected %<)%>");
3506 return attrs;
3508 parser->lex_untranslated_string = false;
3510 return attrs;
3513 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3515 type-name:
3516 specifier-qualifier-list abstract-declarator[opt]
3519 static struct c_type_name *
3520 c_parser_type_name (c_parser *parser)
3522 struct c_declspecs *specs = build_null_declspecs ();
3523 struct c_declarator *declarator;
3524 struct c_type_name *ret;
3525 bool dummy = false;
3526 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3527 if (!specs->declspecs_seen_p)
3529 c_parser_error (parser, "expected specifier-qualifier-list");
3530 return NULL;
3532 if (specs->type != error_mark_node)
3534 pending_xref_error ();
3535 finish_declspecs (specs);
3537 declarator = c_parser_declarator (parser,
3538 specs->typespec_kind != ctsk_none,
3539 C_DTR_ABSTRACT, &dummy);
3540 if (declarator == NULL)
3541 return NULL;
3542 ret = XOBNEW (&parser_obstack, struct c_type_name);
3543 ret->specs = specs;
3544 ret->declarator = declarator;
3545 return ret;
3548 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3550 initializer:
3551 assignment-expression
3552 { initializer-list }
3553 { initializer-list , }
3555 initializer-list:
3556 designation[opt] initializer
3557 initializer-list , designation[opt] initializer
3559 designation:
3560 designator-list =
3562 designator-list:
3563 designator
3564 designator-list designator
3566 designator:
3567 array-designator
3568 . identifier
3570 array-designator:
3571 [ constant-expression ]
3573 GNU extensions:
3575 initializer:
3578 designation:
3579 array-designator
3580 identifier :
3582 array-designator:
3583 [ constant-expression ... constant-expression ]
3585 Any expression without commas is accepted in the syntax for the
3586 constant-expressions, with non-constant expressions rejected later.
3588 This function is only used for top-level initializers; for nested
3589 ones, see c_parser_initval. */
3591 static struct c_expr
3592 c_parser_initializer (c_parser *parser)
3594 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3595 return c_parser_braced_init (parser, NULL_TREE, false);
3596 else
3598 struct c_expr ret;
3599 location_t loc = c_parser_peek_token (parser)->location;
3600 ret = c_parser_expr_no_commas (parser, NULL);
3601 if (TREE_CODE (ret.value) != STRING_CST
3602 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3603 ret = default_function_array_read_conversion (loc, ret);
3604 return ret;
3608 /* Parse a braced initializer list. TYPE is the type specified for a
3609 compound literal, and NULL_TREE for other initializers and for
3610 nested braced lists. NESTED_P is true for nested braced lists,
3611 false for the list of a compound literal or the list that is the
3612 top-level initializer in a declaration. */
3614 static struct c_expr
3615 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3617 struct c_expr ret;
3618 struct obstack braced_init_obstack;
3619 location_t brace_loc = c_parser_peek_token (parser)->location;
3620 gcc_obstack_init (&braced_init_obstack);
3621 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3622 c_parser_consume_token (parser);
3623 if (nested_p)
3624 push_init_level (0, &braced_init_obstack);
3625 else
3626 really_start_incremental_init (type);
3627 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3629 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3631 else
3633 /* Parse a non-empty initializer list, possibly with a trailing
3634 comma. */
3635 while (true)
3637 c_parser_initelt (parser, &braced_init_obstack);
3638 if (parser->error)
3639 break;
3640 if (c_parser_next_token_is (parser, CPP_COMMA))
3641 c_parser_consume_token (parser);
3642 else
3643 break;
3644 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3645 break;
3648 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3650 ret.value = error_mark_node;
3651 ret.original_code = ERROR_MARK;
3652 ret.original_type = NULL;
3653 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3654 pop_init_level (0, &braced_init_obstack);
3655 obstack_free (&braced_init_obstack, NULL);
3656 return ret;
3658 c_parser_consume_token (parser);
3659 ret = pop_init_level (0, &braced_init_obstack);
3660 obstack_free (&braced_init_obstack, NULL);
3661 return ret;
3664 /* Parse a nested initializer, including designators. */
3666 static void
3667 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3669 /* Parse any designator or designator list. A single array
3670 designator may have the subsequent "=" omitted in GNU C, but a
3671 longer list or a structure member designator may not. */
3672 if (c_parser_next_token_is (parser, CPP_NAME)
3673 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3675 /* Old-style structure member designator. */
3676 set_init_label (c_parser_peek_token (parser)->value,
3677 braced_init_obstack);
3678 /* Use the colon as the error location. */
3679 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3680 "obsolete use of designated initializer with %<:%>");
3681 c_parser_consume_token (parser);
3682 c_parser_consume_token (parser);
3684 else
3686 /* des_seen is 0 if there have been no designators, 1 if there
3687 has been a single array designator and 2 otherwise. */
3688 int des_seen = 0;
3689 /* Location of a designator. */
3690 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3691 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3692 || c_parser_next_token_is (parser, CPP_DOT))
3694 int des_prev = des_seen;
3695 if (!des_seen)
3696 des_loc = c_parser_peek_token (parser)->location;
3697 if (des_seen < 2)
3698 des_seen++;
3699 if (c_parser_next_token_is (parser, CPP_DOT))
3701 des_seen = 2;
3702 c_parser_consume_token (parser);
3703 if (c_parser_next_token_is (parser, CPP_NAME))
3705 set_init_label (c_parser_peek_token (parser)->value,
3706 braced_init_obstack);
3707 c_parser_consume_token (parser);
3709 else
3711 struct c_expr init;
3712 init.value = error_mark_node;
3713 init.original_code = ERROR_MARK;
3714 init.original_type = NULL;
3715 c_parser_error (parser, "expected identifier");
3716 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3717 process_init_element (init, false, braced_init_obstack);
3718 return;
3721 else
3723 tree first, second;
3724 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3725 /* ??? Following the old parser, [ objc-receiver
3726 objc-message-args ] is accepted as an initializer,
3727 being distinguished from a designator by what follows
3728 the first assignment expression inside the square
3729 brackets, but after a first array designator a
3730 subsequent square bracket is for Objective-C taken to
3731 start an expression, using the obsolete form of
3732 designated initializer without '=', rather than
3733 possibly being a second level of designation: in LALR
3734 terms, the '[' is shifted rather than reducing
3735 designator to designator-list. */
3736 if (des_prev == 1 && c_dialect_objc ())
3738 des_seen = des_prev;
3739 break;
3741 if (des_prev == 0 && c_dialect_objc ())
3743 /* This might be an array designator or an
3744 Objective-C message expression. If the former,
3745 continue parsing here; if the latter, parse the
3746 remainder of the initializer given the starting
3747 primary-expression. ??? It might make sense to
3748 distinguish when des_prev == 1 as well; see
3749 previous comment. */
3750 tree rec, args;
3751 struct c_expr mexpr;
3752 c_parser_consume_token (parser);
3753 if (c_parser_peek_token (parser)->type == CPP_NAME
3754 && ((c_parser_peek_token (parser)->id_kind
3755 == C_ID_TYPENAME)
3756 || (c_parser_peek_token (parser)->id_kind
3757 == C_ID_CLASSNAME)))
3759 /* Type name receiver. */
3760 tree id = c_parser_peek_token (parser)->value;
3761 c_parser_consume_token (parser);
3762 rec = objc_get_class_reference (id);
3763 goto parse_message_args;
3765 first = c_parser_expr_no_commas (parser, NULL).value;
3766 mark_exp_read (first);
3767 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3768 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3769 goto array_desig_after_first;
3770 /* Expression receiver. So far only one part
3771 without commas has been parsed; there might be
3772 more of the expression. */
3773 rec = first;
3774 while (c_parser_next_token_is (parser, CPP_COMMA))
3776 struct c_expr next;
3777 location_t comma_loc, exp_loc;
3778 comma_loc = c_parser_peek_token (parser)->location;
3779 c_parser_consume_token (parser);
3780 exp_loc = c_parser_peek_token (parser)->location;
3781 next = c_parser_expr_no_commas (parser, NULL);
3782 next = default_function_array_read_conversion (exp_loc,
3783 next);
3784 rec = build_compound_expr (comma_loc, rec, next.value);
3786 parse_message_args:
3787 /* Now parse the objc-message-args. */
3788 args = c_parser_objc_message_args (parser);
3789 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3790 "expected %<]%>");
3791 mexpr.value
3792 = objc_build_message_expr (build_tree_list (rec, args));
3793 mexpr.original_code = ERROR_MARK;
3794 mexpr.original_type = NULL;
3795 /* Now parse and process the remainder of the
3796 initializer, starting with this message
3797 expression as a primary-expression. */
3798 c_parser_initval (parser, &mexpr, braced_init_obstack);
3799 return;
3801 c_parser_consume_token (parser);
3802 first = c_parser_expr_no_commas (parser, NULL).value;
3803 mark_exp_read (first);
3804 array_desig_after_first:
3805 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3807 ellipsis_loc = c_parser_peek_token (parser)->location;
3808 c_parser_consume_token (parser);
3809 second = c_parser_expr_no_commas (parser, NULL).value;
3810 mark_exp_read (second);
3812 else
3813 second = NULL_TREE;
3814 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3816 c_parser_consume_token (parser);
3817 set_init_index (first, second, braced_init_obstack);
3818 if (second)
3819 pedwarn (ellipsis_loc, OPT_pedantic,
3820 "ISO C forbids specifying range of elements to initialize");
3822 else
3823 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3824 "expected %<]%>");
3827 if (des_seen >= 1)
3829 if (c_parser_next_token_is (parser, CPP_EQ))
3831 if (!flag_isoc99)
3832 pedwarn (des_loc, OPT_pedantic,
3833 "ISO C90 forbids specifying subobject to initialize");
3834 c_parser_consume_token (parser);
3836 else
3838 if (des_seen == 1)
3839 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3840 "obsolete use of designated initializer without %<=%>");
3841 else
3843 struct c_expr init;
3844 init.value = error_mark_node;
3845 init.original_code = ERROR_MARK;
3846 init.original_type = NULL;
3847 c_parser_error (parser, "expected %<=%>");
3848 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3849 process_init_element (init, false, braced_init_obstack);
3850 return;
3855 c_parser_initval (parser, NULL, braced_init_obstack);
3858 /* Parse a nested initializer; as c_parser_initializer but parses
3859 initializers within braced lists, after any designators have been
3860 applied. If AFTER is not NULL then it is an Objective-C message
3861 expression which is the primary-expression starting the
3862 initializer. */
3864 static void
3865 c_parser_initval (c_parser *parser, struct c_expr *after,
3866 struct obstack * braced_init_obstack)
3868 struct c_expr init;
3869 gcc_assert (!after || c_dialect_objc ());
3870 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3871 init = c_parser_braced_init (parser, NULL_TREE, true);
3872 else
3874 location_t loc = c_parser_peek_token (parser)->location;
3875 init = c_parser_expr_no_commas (parser, after);
3876 if (init.value != NULL_TREE
3877 && TREE_CODE (init.value) != STRING_CST
3878 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3879 init = default_function_array_read_conversion (loc, init);
3881 process_init_element (init, false, braced_init_obstack);
3884 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3885 C99 6.8.2).
3887 compound-statement:
3888 { block-item-list[opt] }
3889 { label-declarations block-item-list }
3891 block-item-list:
3892 block-item
3893 block-item-list block-item
3895 block-item:
3896 nested-declaration
3897 statement
3899 nested-declaration:
3900 declaration
3902 GNU extensions:
3904 compound-statement:
3905 { label-declarations block-item-list }
3907 nested-declaration:
3908 __extension__ nested-declaration
3909 nested-function-definition
3911 label-declarations:
3912 label-declaration
3913 label-declarations label-declaration
3915 label-declaration:
3916 __label__ identifier-list ;
3918 Allowing the mixing of declarations and code is new in C99. The
3919 GNU syntax also permits (not shown above) labels at the end of
3920 compound statements, which yield an error. We don't allow labels
3921 on declarations; this might seem like a natural extension, but
3922 there would be a conflict between attributes on the label and
3923 prefix attributes on the declaration. ??? The syntax follows the
3924 old parser in requiring something after label declarations.
3925 Although they are erroneous if the labels declared aren't defined,
3926 is it useful for the syntax to be this way?
3928 OpenMP:
3930 block-item:
3931 openmp-directive
3933 openmp-directive:
3934 barrier-directive
3935 flush-directive */
3937 static tree
3938 c_parser_compound_statement (c_parser *parser)
3940 tree stmt;
3941 location_t brace_loc;
3942 brace_loc = c_parser_peek_token (parser)->location;
3943 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3945 /* Ensure a scope is entered and left anyway to avoid confusion
3946 if we have just prepared to enter a function body. */
3947 stmt = c_begin_compound_stmt (true);
3948 c_end_compound_stmt (brace_loc, stmt, true);
3949 return error_mark_node;
3951 stmt = c_begin_compound_stmt (true);
3952 c_parser_compound_statement_nostart (parser);
3953 return c_end_compound_stmt (brace_loc, stmt, true);
3956 /* Parse a compound statement except for the opening brace. This is
3957 used for parsing both compound statements and statement expressions
3958 (which follow different paths to handling the opening). */
3960 static void
3961 c_parser_compound_statement_nostart (c_parser *parser)
3963 bool last_stmt = false;
3964 bool last_label = false;
3965 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3966 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3967 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3969 c_parser_consume_token (parser);
3970 return;
3972 mark_valid_location_for_stdc_pragma (true);
3973 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3975 /* Read zero or more forward-declarations for labels that nested
3976 functions can jump to. */
3977 mark_valid_location_for_stdc_pragma (false);
3978 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3980 label_loc = c_parser_peek_token (parser)->location;
3981 c_parser_consume_token (parser);
3982 /* Any identifiers, including those declared as type names,
3983 are OK here. */
3984 while (true)
3986 tree label;
3987 if (c_parser_next_token_is_not (parser, CPP_NAME))
3989 c_parser_error (parser, "expected identifier");
3990 break;
3992 label
3993 = declare_label (c_parser_peek_token (parser)->value);
3994 C_DECLARED_LABEL_FLAG (label) = 1;
3995 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
3996 c_parser_consume_token (parser);
3997 if (c_parser_next_token_is (parser, CPP_COMMA))
3998 c_parser_consume_token (parser);
3999 else
4000 break;
4002 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4004 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
4006 /* We must now have at least one statement, label or declaration. */
4007 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4009 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4010 c_parser_error (parser, "expected declaration or statement");
4011 c_parser_consume_token (parser);
4012 return;
4014 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4016 location_t loc = c_parser_peek_token (parser)->location;
4017 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4018 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4019 || (c_parser_next_token_is (parser, CPP_NAME)
4020 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4022 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4023 label_loc = c_parser_peek_2nd_token (parser)->location;
4024 else
4025 label_loc = c_parser_peek_token (parser)->location;
4026 last_label = true;
4027 last_stmt = false;
4028 mark_valid_location_for_stdc_pragma (false);
4029 c_parser_label (parser);
4031 else if (!last_label
4032 && c_parser_next_tokens_start_declaration (parser))
4034 last_label = false;
4035 mark_valid_location_for_stdc_pragma (false);
4036 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4037 if (last_stmt)
4038 pedwarn_c90 (loc,
4039 (pedantic && !flag_isoc99)
4040 ? OPT_pedantic
4041 : OPT_Wdeclaration_after_statement,
4042 "ISO C90 forbids mixed declarations and code");
4043 last_stmt = false;
4045 else if (!last_label
4046 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4048 /* __extension__ can start a declaration, but is also an
4049 unary operator that can start an expression. Consume all
4050 but the last of a possible series of __extension__ to
4051 determine which. */
4052 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4053 && (c_parser_peek_2nd_token (parser)->keyword
4054 == RID_EXTENSION))
4055 c_parser_consume_token (parser);
4056 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4058 int ext;
4059 ext = disable_extension_diagnostics ();
4060 c_parser_consume_token (parser);
4061 last_label = false;
4062 mark_valid_location_for_stdc_pragma (false);
4063 c_parser_declaration_or_fndef (parser, true, true, true, true,
4064 true, NULL);
4065 /* Following the old parser, __extension__ does not
4066 disable this diagnostic. */
4067 restore_extension_diagnostics (ext);
4068 if (last_stmt)
4069 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4070 ? OPT_pedantic
4071 : OPT_Wdeclaration_after_statement,
4072 "ISO C90 forbids mixed declarations and code");
4073 last_stmt = false;
4075 else
4076 goto statement;
4078 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4080 /* External pragmas, and some omp pragmas, are not associated
4081 with regular c code, and so are not to be considered statements
4082 syntactically. This ensures that the user doesn't put them
4083 places that would turn into syntax errors if the directive
4084 were ignored. */
4085 if (c_parser_pragma (parser, pragma_compound))
4086 last_label = false, last_stmt = true;
4088 else if (c_parser_next_token_is (parser, CPP_EOF))
4090 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4091 c_parser_error (parser, "expected declaration or statement");
4092 return;
4094 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4096 if (parser->in_if_block)
4098 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4099 error_at (loc, """expected %<}%> before %<else%>");
4100 return;
4102 else
4104 error_at (loc, "%<else%> without a previous %<if%>");
4105 c_parser_consume_token (parser);
4106 continue;
4109 else
4111 statement:
4112 last_label = false;
4113 last_stmt = true;
4114 mark_valid_location_for_stdc_pragma (false);
4115 c_parser_statement_after_labels (parser);
4118 parser->error = false;
4120 if (last_label)
4121 error_at (label_loc, "label at end of compound statement");
4122 c_parser_consume_token (parser);
4123 /* Restore the value we started with. */
4124 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4127 /* Parse a label (C90 6.6.1, C99 6.8.1).
4129 label:
4130 identifier : attributes[opt]
4131 case constant-expression :
4132 default :
4134 GNU extensions:
4136 label:
4137 case constant-expression ... constant-expression :
4139 The use of attributes on labels is a GNU extension. The syntax in
4140 GNU C accepts any expressions without commas, non-constant
4141 expressions being rejected later. */
4143 static void
4144 c_parser_label (c_parser *parser)
4146 location_t loc1 = c_parser_peek_token (parser)->location;
4147 tree label = NULL_TREE;
4148 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4150 tree exp1, exp2;
4151 c_parser_consume_token (parser);
4152 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4153 if (c_parser_next_token_is (parser, CPP_COLON))
4155 c_parser_consume_token (parser);
4156 label = do_case (loc1, exp1, NULL_TREE);
4158 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4160 c_parser_consume_token (parser);
4161 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4162 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4163 label = do_case (loc1, exp1, exp2);
4165 else
4166 c_parser_error (parser, "expected %<:%> or %<...%>");
4168 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4170 c_parser_consume_token (parser);
4171 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4172 label = do_case (loc1, NULL_TREE, NULL_TREE);
4174 else
4176 tree name = c_parser_peek_token (parser)->value;
4177 tree tlab;
4178 tree attrs;
4179 location_t loc2 = c_parser_peek_token (parser)->location;
4180 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4181 c_parser_consume_token (parser);
4182 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4183 c_parser_consume_token (parser);
4184 attrs = c_parser_attributes (parser);
4185 tlab = define_label (loc2, name);
4186 if (tlab)
4188 decl_attributes (&tlab, attrs, 0);
4189 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4192 if (label)
4194 if (c_parser_next_tokens_start_declaration (parser))
4196 error_at (c_parser_peek_token (parser)->location,
4197 "a label can only be part of a statement and "
4198 "a declaration is not a statement");
4199 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4200 /*static_assert_ok*/ true,
4201 /*nested*/ true, /*empty_ok*/ false,
4202 /*start_attr_ok*/ true, NULL);
4207 /* Parse a statement (C90 6.6, C99 6.8).
4209 statement:
4210 labeled-statement
4211 compound-statement
4212 expression-statement
4213 selection-statement
4214 iteration-statement
4215 jump-statement
4217 labeled-statement:
4218 label statement
4220 expression-statement:
4221 expression[opt] ;
4223 selection-statement:
4224 if-statement
4225 switch-statement
4227 iteration-statement:
4228 while-statement
4229 do-statement
4230 for-statement
4232 jump-statement:
4233 goto identifier ;
4234 continue ;
4235 break ;
4236 return expression[opt] ;
4238 GNU extensions:
4240 statement:
4241 asm-statement
4243 jump-statement:
4244 goto * expression ;
4246 Objective-C:
4248 statement:
4249 objc-throw-statement
4250 objc-try-catch-statement
4251 objc-synchronized-statement
4253 objc-throw-statement:
4254 @throw expression ;
4255 @throw ;
4257 OpenMP:
4259 statement:
4260 openmp-construct
4262 openmp-construct:
4263 parallel-construct
4264 for-construct
4265 sections-construct
4266 single-construct
4267 parallel-for-construct
4268 parallel-sections-construct
4269 master-construct
4270 critical-construct
4271 atomic-construct
4272 ordered-construct
4274 parallel-construct:
4275 parallel-directive structured-block
4277 for-construct:
4278 for-directive iteration-statement
4280 sections-construct:
4281 sections-directive section-scope
4283 single-construct:
4284 single-directive structured-block
4286 parallel-for-construct:
4287 parallel-for-directive iteration-statement
4289 parallel-sections-construct:
4290 parallel-sections-directive section-scope
4292 master-construct:
4293 master-directive structured-block
4295 critical-construct:
4296 critical-directive structured-block
4298 atomic-construct:
4299 atomic-directive expression-statement
4301 ordered-construct:
4302 ordered-directive structured-block */
4304 static void
4305 c_parser_statement (c_parser *parser)
4307 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4308 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4309 || (c_parser_next_token_is (parser, CPP_NAME)
4310 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4311 c_parser_label (parser);
4312 c_parser_statement_after_labels (parser);
4315 /* Parse a statement, other than a labeled statement. */
4317 static void
4318 c_parser_statement_after_labels (c_parser *parser)
4320 location_t loc = c_parser_peek_token (parser)->location;
4321 tree stmt = NULL_TREE;
4322 bool in_if_block = parser->in_if_block;
4323 parser->in_if_block = false;
4324 switch (c_parser_peek_token (parser)->type)
4326 case CPP_OPEN_BRACE:
4327 add_stmt (c_parser_compound_statement (parser));
4328 break;
4329 case CPP_KEYWORD:
4330 switch (c_parser_peek_token (parser)->keyword)
4332 case RID_IF:
4333 c_parser_if_statement (parser);
4334 break;
4335 case RID_SWITCH:
4336 c_parser_switch_statement (parser);
4337 break;
4338 case RID_WHILE:
4339 c_parser_while_statement (parser);
4340 break;
4341 case RID_DO:
4342 c_parser_do_statement (parser);
4343 break;
4344 case RID_FOR:
4345 c_parser_for_statement (parser);
4346 break;
4347 case RID_GOTO:
4348 c_parser_consume_token (parser);
4349 if (c_parser_next_token_is (parser, CPP_NAME))
4351 stmt = c_finish_goto_label (loc,
4352 c_parser_peek_token (parser)->value);
4353 c_parser_consume_token (parser);
4355 else if (c_parser_next_token_is (parser, CPP_MULT))
4357 tree val;
4359 c_parser_consume_token (parser);
4360 val = c_parser_expression (parser).value;
4361 mark_exp_read (val);
4362 stmt = c_finish_goto_ptr (loc, val);
4364 else
4365 c_parser_error (parser, "expected identifier or %<*%>");
4366 goto expect_semicolon;
4367 case RID_CONTINUE:
4368 c_parser_consume_token (parser);
4369 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4370 goto expect_semicolon;
4371 case RID_BREAK:
4372 c_parser_consume_token (parser);
4373 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4374 goto expect_semicolon;
4375 case RID_RETURN:
4376 c_parser_consume_token (parser);
4377 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4379 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4380 c_parser_consume_token (parser);
4382 else
4384 struct c_expr expr = c_parser_expression_conv (parser);
4385 mark_exp_read (expr.value);
4386 stmt = c_finish_return (loc, expr.value, expr.original_type);
4387 goto expect_semicolon;
4389 break;
4390 case RID_ASM:
4391 stmt = c_parser_asm_statement (parser);
4392 break;
4393 case RID_AT_THROW:
4394 gcc_assert (c_dialect_objc ());
4395 c_parser_consume_token (parser);
4396 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4398 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4399 c_parser_consume_token (parser);
4401 else
4403 tree expr = c_parser_expression (parser).value;
4404 expr = c_fully_fold (expr, false, NULL);
4405 stmt = objc_build_throw_stmt (loc, expr);
4406 goto expect_semicolon;
4408 break;
4409 case RID_AT_TRY:
4410 gcc_assert (c_dialect_objc ());
4411 c_parser_objc_try_catch_finally_statement (parser);
4412 break;
4413 case RID_AT_SYNCHRONIZED:
4414 gcc_assert (c_dialect_objc ());
4415 c_parser_objc_synchronized_statement (parser);
4416 break;
4417 default:
4418 goto expr_stmt;
4420 break;
4421 case CPP_SEMICOLON:
4422 c_parser_consume_token (parser);
4423 break;
4424 case CPP_CLOSE_PAREN:
4425 case CPP_CLOSE_SQUARE:
4426 /* Avoid infinite loop in error recovery:
4427 c_parser_skip_until_found stops at a closing nesting
4428 delimiter without consuming it, but here we need to consume
4429 it to proceed further. */
4430 c_parser_error (parser, "expected statement");
4431 c_parser_consume_token (parser);
4432 break;
4433 case CPP_PRAGMA:
4434 c_parser_pragma (parser, pragma_stmt);
4435 break;
4436 default:
4437 expr_stmt:
4438 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4439 expect_semicolon:
4440 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4441 break;
4443 /* Two cases cannot and do not have line numbers associated: If stmt
4444 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4445 cannot hold line numbers. But that's OK because the statement
4446 will either be changed to a MODIFY_EXPR during gimplification of
4447 the statement expr, or discarded. If stmt was compound, but
4448 without new variables, we will have skipped the creation of a
4449 BIND and will have a bare STATEMENT_LIST. But that's OK because
4450 (recursively) all of the component statements should already have
4451 line numbers assigned. ??? Can we discard no-op statements
4452 earlier? */
4453 if (CAN_HAVE_LOCATION_P (stmt)
4454 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4455 SET_EXPR_LOCATION (stmt, loc);
4457 parser->in_if_block = in_if_block;
4460 /* Parse the condition from an if, do, while or for statements. */
4462 static tree
4463 c_parser_condition (c_parser *parser)
4465 location_t loc = c_parser_peek_token (parser)->location;
4466 tree cond;
4467 cond = c_parser_expression_conv (parser).value;
4468 cond = c_objc_common_truthvalue_conversion (loc, cond);
4469 cond = c_fully_fold (cond, false, NULL);
4470 if (warn_sequence_point)
4471 verify_sequence_points (cond);
4472 return cond;
4475 /* Parse a parenthesized condition from an if, do or while statement.
4477 condition:
4478 ( expression )
4480 static tree
4481 c_parser_paren_condition (c_parser *parser)
4483 tree cond;
4484 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4485 return error_mark_node;
4486 cond = c_parser_condition (parser);
4487 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4488 return cond;
4491 /* Parse a statement which is a block in C99. */
4493 static tree
4494 c_parser_c99_block_statement (c_parser *parser)
4496 tree block = c_begin_compound_stmt (flag_isoc99);
4497 location_t loc = c_parser_peek_token (parser)->location;
4498 c_parser_statement (parser);
4499 return c_end_compound_stmt (loc, block, flag_isoc99);
4502 /* Parse the body of an if statement. This is just parsing a
4503 statement but (a) it is a block in C99, (b) we track whether the
4504 body is an if statement for the sake of -Wparentheses warnings, (c)
4505 we handle an empty body specially for the sake of -Wempty-body
4506 warnings, and (d) we call parser_compound_statement directly
4507 because c_parser_statement_after_labels resets
4508 parser->in_if_block. */
4510 static tree
4511 c_parser_if_body (c_parser *parser, bool *if_p)
4513 tree block = c_begin_compound_stmt (flag_isoc99);
4514 location_t body_loc = c_parser_peek_token (parser)->location;
4515 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4516 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4517 || (c_parser_next_token_is (parser, CPP_NAME)
4518 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4519 c_parser_label (parser);
4520 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4521 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4523 location_t loc = c_parser_peek_token (parser)->location;
4524 add_stmt (build_empty_stmt (loc));
4525 c_parser_consume_token (parser);
4526 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4527 warning_at (loc, OPT_Wempty_body,
4528 "suggest braces around empty body in an %<if%> statement");
4530 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4531 add_stmt (c_parser_compound_statement (parser));
4532 else
4533 c_parser_statement_after_labels (parser);
4534 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4537 /* Parse the else body of an if statement. This is just parsing a
4538 statement but (a) it is a block in C99, (b) we handle an empty body
4539 specially for the sake of -Wempty-body warnings. */
4541 static tree
4542 c_parser_else_body (c_parser *parser)
4544 location_t else_loc = c_parser_peek_token (parser)->location;
4545 tree block = c_begin_compound_stmt (flag_isoc99);
4546 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4547 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4548 || (c_parser_next_token_is (parser, CPP_NAME)
4549 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4550 c_parser_label (parser);
4551 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4553 location_t loc = c_parser_peek_token (parser)->location;
4554 warning_at (loc,
4555 OPT_Wempty_body,
4556 "suggest braces around empty body in an %<else%> statement");
4557 add_stmt (build_empty_stmt (loc));
4558 c_parser_consume_token (parser);
4560 else
4561 c_parser_statement_after_labels (parser);
4562 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4565 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4567 if-statement:
4568 if ( expression ) statement
4569 if ( expression ) statement else statement
4572 static void
4573 c_parser_if_statement (c_parser *parser)
4575 tree block;
4576 location_t loc;
4577 tree cond;
4578 bool first_if = false;
4579 tree first_body, second_body;
4580 bool in_if_block;
4582 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4583 c_parser_consume_token (parser);
4584 block = c_begin_compound_stmt (flag_isoc99);
4585 loc = c_parser_peek_token (parser)->location;
4586 cond = c_parser_paren_condition (parser);
4587 in_if_block = parser->in_if_block;
4588 parser->in_if_block = true;
4589 first_body = c_parser_if_body (parser, &first_if);
4590 parser->in_if_block = in_if_block;
4591 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4593 c_parser_consume_token (parser);
4594 second_body = c_parser_else_body (parser);
4596 else
4597 second_body = NULL_TREE;
4598 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4599 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4602 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4604 switch-statement:
4605 switch (expression) statement
4608 static void
4609 c_parser_switch_statement (c_parser *parser)
4611 tree block, expr, body, save_break;
4612 location_t switch_loc = c_parser_peek_token (parser)->location;
4613 location_t switch_cond_loc;
4614 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4615 c_parser_consume_token (parser);
4616 block = c_begin_compound_stmt (flag_isoc99);
4617 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4619 switch_cond_loc = c_parser_peek_token (parser)->location;
4620 expr = c_parser_expression (parser).value;
4621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4623 else
4625 switch_cond_loc = UNKNOWN_LOCATION;
4626 expr = error_mark_node;
4628 c_start_case (switch_loc, switch_cond_loc, expr);
4629 save_break = c_break_label;
4630 c_break_label = NULL_TREE;
4631 body = c_parser_c99_block_statement (parser);
4632 c_finish_case (body);
4633 if (c_break_label)
4635 location_t here = c_parser_peek_token (parser)->location;
4636 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4637 SET_EXPR_LOCATION (t, here);
4638 add_stmt (t);
4640 c_break_label = save_break;
4641 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4644 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4646 while-statement:
4647 while (expression) statement
4650 static void
4651 c_parser_while_statement (c_parser *parser)
4653 tree block, cond, body, save_break, save_cont;
4654 location_t loc;
4655 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4656 c_parser_consume_token (parser);
4657 block = c_begin_compound_stmt (flag_isoc99);
4658 loc = c_parser_peek_token (parser)->location;
4659 cond = c_parser_paren_condition (parser);
4660 save_break = c_break_label;
4661 c_break_label = NULL_TREE;
4662 save_cont = c_cont_label;
4663 c_cont_label = NULL_TREE;
4664 body = c_parser_c99_block_statement (parser);
4665 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4666 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4667 c_break_label = save_break;
4668 c_cont_label = save_cont;
4671 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4673 do-statement:
4674 do statement while ( expression ) ;
4677 static void
4678 c_parser_do_statement (c_parser *parser)
4680 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4681 location_t loc;
4682 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4683 c_parser_consume_token (parser);
4684 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4685 warning_at (c_parser_peek_token (parser)->location,
4686 OPT_Wempty_body,
4687 "suggest braces around empty body in %<do%> statement");
4688 block = c_begin_compound_stmt (flag_isoc99);
4689 loc = c_parser_peek_token (parser)->location;
4690 save_break = c_break_label;
4691 c_break_label = NULL_TREE;
4692 save_cont = c_cont_label;
4693 c_cont_label = NULL_TREE;
4694 body = c_parser_c99_block_statement (parser);
4695 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4696 new_break = c_break_label;
4697 c_break_label = save_break;
4698 new_cont = c_cont_label;
4699 c_cont_label = save_cont;
4700 cond = c_parser_paren_condition (parser);
4701 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4702 c_parser_skip_to_end_of_block_or_statement (parser);
4703 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4704 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4707 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4709 for-statement:
4710 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4711 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4713 The form with a declaration is new in C99.
4715 ??? In accordance with the old parser, the declaration may be a
4716 nested function, which is then rejected in check_for_loop_decls,
4717 but does it make any sense for this to be included in the grammar?
4718 Note in particular that the nested function does not include a
4719 trailing ';', whereas the "declaration" production includes one.
4720 Also, can we reject bad declarations earlier and cheaper than
4721 check_for_loop_decls?
4723 In Objective-C, there are two additional variants:
4725 foreach-statement:
4726 for ( expression in expresssion ) statement
4727 for ( declaration in expression ) statement
4729 This is inconsistent with C, because the second variant is allowed
4730 even if c99 is not enabled.
4732 The rest of the comment documents these Objective-C foreach-statement.
4734 Here is the canonical example of the first variant:
4735 for (object in array) { do something with object }
4736 we call the first expression ("object") the "object_expression" and
4737 the second expression ("array") the "collection_expression".
4738 object_expression must be an lvalue of type "id" (a generic Objective-C
4739 object) because the loop works by assigning to object_expression the
4740 various objects from the collection_expression. collection_expression
4741 must evaluate to something of type "id" which responds to the method
4742 countByEnumeratingWithState:objects:count:.
4744 The canonical example of the second variant is:
4745 for (id object in array) { do something with object }
4746 which is completely equivalent to
4748 id object;
4749 for (object in array) { do something with object }
4751 Note that initizializing 'object' in some way (eg, "for ((object =
4752 xxx) in array) { do something with object }") is possibly
4753 technically valid, but completely pointless as 'object' will be
4754 assigned to something else as soon as the loop starts. We should
4755 most likely reject it (TODO).
4757 The beginning of the Objective-C foreach-statement looks exactly
4758 like the beginning of the for-statement, and we can tell it is a
4759 foreach-statement only because the initial declaration or
4760 expression is terminated by 'in' instead of ';'.
4763 static void
4764 c_parser_for_statement (c_parser *parser)
4766 tree block, cond, incr, save_break, save_cont, body;
4767 /* The following are only used when parsing an ObjC foreach statement. */
4768 tree object_expression;
4769 /* Silence the bogus uninitialized warning. */
4770 tree collection_expression = NULL;
4771 location_t loc = c_parser_peek_token (parser)->location;
4772 location_t for_loc = c_parser_peek_token (parser)->location;
4773 bool is_foreach_statement = false;
4774 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4775 c_parser_consume_token (parser);
4776 /* Open a compound statement in Objective-C as well, just in case this is
4777 as foreach expression. */
4778 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4779 cond = error_mark_node;
4780 incr = error_mark_node;
4781 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4783 /* Parse the initialization declaration or expression. */
4784 object_expression = error_mark_node;
4785 parser->objc_could_be_foreach_context = c_dialect_objc ();
4786 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4788 parser->objc_could_be_foreach_context = false;
4789 c_parser_consume_token (parser);
4790 c_finish_expr_stmt (loc, NULL_TREE);
4792 else if (c_parser_next_tokens_start_declaration (parser))
4794 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4795 &object_expression);
4796 parser->objc_could_be_foreach_context = false;
4798 if (c_parser_next_token_is_keyword (parser, RID_IN))
4800 c_parser_consume_token (parser);
4801 is_foreach_statement = true;
4802 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4803 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4805 else
4806 check_for_loop_decls (for_loc, flag_isoc99);
4808 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4810 /* __extension__ can start a declaration, but is also an
4811 unary operator that can start an expression. Consume all
4812 but the last of a possible series of __extension__ to
4813 determine which. */
4814 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4815 && (c_parser_peek_2nd_token (parser)->keyword
4816 == RID_EXTENSION))
4817 c_parser_consume_token (parser);
4818 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4820 int ext;
4821 ext = disable_extension_diagnostics ();
4822 c_parser_consume_token (parser);
4823 c_parser_declaration_or_fndef (parser, true, true, true, true,
4824 true, &object_expression);
4825 parser->objc_could_be_foreach_context = false;
4827 restore_extension_diagnostics (ext);
4828 if (c_parser_next_token_is_keyword (parser, RID_IN))
4830 c_parser_consume_token (parser);
4831 is_foreach_statement = true;
4832 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4833 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4835 else
4836 check_for_loop_decls (for_loc, flag_isoc99);
4838 else
4839 goto init_expr;
4841 else
4843 init_expr:
4845 tree init_expression;
4846 init_expression = c_parser_expression (parser).value;
4847 parser->objc_could_be_foreach_context = false;
4848 if (c_parser_next_token_is_keyword (parser, RID_IN))
4850 c_parser_consume_token (parser);
4851 is_foreach_statement = true;
4852 if (! lvalue_p (init_expression))
4853 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4854 object_expression = c_fully_fold (init_expression, false, NULL);
4856 else
4858 c_finish_expr_stmt (loc, init_expression);
4859 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4863 /* Parse the loop condition. In the case of a foreach
4864 statement, there is no loop condition. */
4865 gcc_assert (!parser->objc_could_be_foreach_context);
4866 if (!is_foreach_statement)
4868 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4870 c_parser_consume_token (parser);
4871 cond = NULL_TREE;
4873 else
4875 cond = c_parser_condition (parser);
4876 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4879 /* Parse the increment expression (the third expression in a
4880 for-statement). In the case of a foreach-statement, this is
4881 the expression that follows the 'in'. */
4882 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4884 if (is_foreach_statement)
4886 c_parser_error (parser, "missing collection in fast enumeration");
4887 collection_expression = error_mark_node;
4889 else
4890 incr = c_process_expr_stmt (loc, NULL_TREE);
4892 else
4894 if (is_foreach_statement)
4895 collection_expression = c_fully_fold (c_parser_expression (parser).value,
4896 false, NULL);
4897 else
4898 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4902 save_break = c_break_label;
4903 c_break_label = NULL_TREE;
4904 save_cont = c_cont_label;
4905 c_cont_label = NULL_TREE;
4906 body = c_parser_c99_block_statement (parser);
4907 if (is_foreach_statement)
4908 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4909 else
4910 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4911 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4912 c_break_label = save_break;
4913 c_cont_label = save_cont;
4916 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4917 statement with inputs, outputs, clobbers, and volatile tag
4918 allowed.
4920 asm-statement:
4921 asm type-qualifier[opt] ( asm-argument ) ;
4922 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4924 asm-argument:
4925 asm-string-literal
4926 asm-string-literal : asm-operands[opt]
4927 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4928 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4930 asm-goto-argument:
4931 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4932 : asm-goto-operands
4934 Qualifiers other than volatile are accepted in the syntax but
4935 warned for. */
4937 static tree
4938 c_parser_asm_statement (c_parser *parser)
4940 tree quals, str, outputs, inputs, clobbers, labels, ret;
4941 bool simple, is_goto;
4942 location_t asm_loc = c_parser_peek_token (parser)->location;
4943 int section, nsections;
4945 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4946 c_parser_consume_token (parser);
4947 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4949 quals = c_parser_peek_token (parser)->value;
4950 c_parser_consume_token (parser);
4952 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4953 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4955 warning_at (c_parser_peek_token (parser)->location,
4957 "%E qualifier ignored on asm",
4958 c_parser_peek_token (parser)->value);
4959 quals = NULL_TREE;
4960 c_parser_consume_token (parser);
4962 else
4963 quals = NULL_TREE;
4965 is_goto = false;
4966 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
4968 c_parser_consume_token (parser);
4969 is_goto = true;
4972 /* ??? Follow the C++ parser rather than using the
4973 lex_untranslated_string kludge. */
4974 parser->lex_untranslated_string = true;
4975 ret = NULL;
4977 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4978 goto error;
4980 str = c_parser_asm_string_literal (parser);
4981 if (str == NULL_TREE)
4982 goto error_close_paren;
4984 simple = true;
4985 outputs = NULL_TREE;
4986 inputs = NULL_TREE;
4987 clobbers = NULL_TREE;
4988 labels = NULL_TREE;
4990 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
4991 goto done_asm;
4993 /* Parse each colon-delimited section of operands. */
4994 nsections = 3 + is_goto;
4995 for (section = 0; section < nsections; ++section)
4997 if (!c_parser_require (parser, CPP_COLON,
4998 is_goto
4999 ? "expected %<:%>"
5000 : "expected %<:%> or %<)%>"))
5001 goto error_close_paren;
5003 /* Once past any colon, we're no longer a simple asm. */
5004 simple = false;
5006 if ((!c_parser_next_token_is (parser, CPP_COLON)
5007 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5008 || section == 3)
5009 switch (section)
5011 case 0:
5012 /* For asm goto, we don't allow output operands, but reserve
5013 the slot for a future extension that does allow them. */
5014 if (!is_goto)
5015 outputs = c_parser_asm_operands (parser, false);
5016 break;
5017 case 1:
5018 inputs = c_parser_asm_operands (parser, true);
5019 break;
5020 case 2:
5021 clobbers = c_parser_asm_clobbers (parser);
5022 break;
5023 case 3:
5024 labels = c_parser_asm_goto_operands (parser);
5025 break;
5026 default:
5027 gcc_unreachable ();
5030 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5031 goto done_asm;
5034 done_asm:
5035 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5038 goto error;
5041 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5042 c_parser_skip_to_end_of_block_or_statement (parser);
5044 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5045 clobbers, labels, simple));
5047 error:
5048 parser->lex_untranslated_string = false;
5049 return ret;
5051 error_close_paren:
5052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5053 goto error;
5056 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5057 not outputs), apply the default conversion of functions and arrays
5058 to pointers.
5060 asm-operands:
5061 asm-operand
5062 asm-operands , asm-operand
5064 asm-operand:
5065 asm-string-literal ( expression )
5066 [ identifier ] asm-string-literal ( expression )
5069 static tree
5070 c_parser_asm_operands (c_parser *parser, bool convert_p)
5072 tree list = NULL_TREE;
5073 location_t loc;
5074 while (true)
5076 tree name, str;
5077 struct c_expr expr;
5078 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5080 c_parser_consume_token (parser);
5081 if (c_parser_next_token_is (parser, CPP_NAME))
5083 tree id = c_parser_peek_token (parser)->value;
5084 c_parser_consume_token (parser);
5085 name = build_string (IDENTIFIER_LENGTH (id),
5086 IDENTIFIER_POINTER (id));
5088 else
5090 c_parser_error (parser, "expected identifier");
5091 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5092 return NULL_TREE;
5094 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5095 "expected %<]%>");
5097 else
5098 name = NULL_TREE;
5099 str = c_parser_asm_string_literal (parser);
5100 if (str == NULL_TREE)
5101 return NULL_TREE;
5102 parser->lex_untranslated_string = false;
5103 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5105 parser->lex_untranslated_string = true;
5106 return NULL_TREE;
5108 loc = c_parser_peek_token (parser)->location;
5109 expr = c_parser_expression (parser);
5110 mark_exp_read (expr.value);
5111 if (convert_p)
5112 expr = default_function_array_conversion (loc, expr);
5113 expr.value = c_fully_fold (expr.value, false, NULL);
5114 parser->lex_untranslated_string = true;
5115 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5117 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5118 return NULL_TREE;
5120 list = chainon (list, build_tree_list (build_tree_list (name, str),
5121 expr.value));
5122 if (c_parser_next_token_is (parser, CPP_COMMA))
5123 c_parser_consume_token (parser);
5124 else
5125 break;
5127 return list;
5130 /* Parse asm clobbers, a GNU extension.
5132 asm-clobbers:
5133 asm-string-literal
5134 asm-clobbers , asm-string-literal
5137 static tree
5138 c_parser_asm_clobbers (c_parser *parser)
5140 tree list = NULL_TREE;
5141 while (true)
5143 tree str = c_parser_asm_string_literal (parser);
5144 if (str)
5145 list = tree_cons (NULL_TREE, str, list);
5146 else
5147 return NULL_TREE;
5148 if (c_parser_next_token_is (parser, CPP_COMMA))
5149 c_parser_consume_token (parser);
5150 else
5151 break;
5153 return list;
5156 /* Parse asm goto labels, a GNU extension.
5158 asm-goto-operands:
5159 identifier
5160 asm-goto-operands , identifier
5163 static tree
5164 c_parser_asm_goto_operands (c_parser *parser)
5166 tree list = NULL_TREE;
5167 while (true)
5169 tree name, label;
5171 if (c_parser_next_token_is (parser, CPP_NAME))
5173 c_token *tok = c_parser_peek_token (parser);
5174 name = tok->value;
5175 label = lookup_label_for_goto (tok->location, name);
5176 c_parser_consume_token (parser);
5177 TREE_USED (label) = 1;
5179 else
5181 c_parser_error (parser, "expected identifier");
5182 return NULL_TREE;
5185 name = build_string (IDENTIFIER_LENGTH (name),
5186 IDENTIFIER_POINTER (name));
5187 list = tree_cons (name, label, list);
5188 if (c_parser_next_token_is (parser, CPP_COMMA))
5189 c_parser_consume_token (parser);
5190 else
5191 return nreverse (list);
5195 /* Parse an expression other than a compound expression; that is, an
5196 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5197 NULL then it is an Objective-C message expression which is the
5198 primary-expression starting the expression as an initializer.
5200 assignment-expression:
5201 conditional-expression
5202 unary-expression assignment-operator assignment-expression
5204 assignment-operator: one of
5205 = *= /= %= += -= <<= >>= &= ^= |=
5207 In GNU C we accept any conditional expression on the LHS and
5208 diagnose the invalid lvalue rather than producing a syntax
5209 error. */
5211 static struct c_expr
5212 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5214 struct c_expr lhs, rhs, ret;
5215 enum tree_code code;
5216 location_t op_location, exp_location;
5217 gcc_assert (!after || c_dialect_objc ());
5218 lhs = c_parser_conditional_expression (parser, after);
5219 op_location = c_parser_peek_token (parser)->location;
5220 switch (c_parser_peek_token (parser)->type)
5222 case CPP_EQ:
5223 code = NOP_EXPR;
5224 break;
5225 case CPP_MULT_EQ:
5226 code = MULT_EXPR;
5227 break;
5228 case CPP_DIV_EQ:
5229 code = TRUNC_DIV_EXPR;
5230 break;
5231 case CPP_MOD_EQ:
5232 code = TRUNC_MOD_EXPR;
5233 break;
5234 case CPP_PLUS_EQ:
5235 code = PLUS_EXPR;
5236 break;
5237 case CPP_MINUS_EQ:
5238 code = MINUS_EXPR;
5239 break;
5240 case CPP_LSHIFT_EQ:
5241 code = LSHIFT_EXPR;
5242 break;
5243 case CPP_RSHIFT_EQ:
5244 code = RSHIFT_EXPR;
5245 break;
5246 case CPP_AND_EQ:
5247 code = BIT_AND_EXPR;
5248 break;
5249 case CPP_XOR_EQ:
5250 code = BIT_XOR_EXPR;
5251 break;
5252 case CPP_OR_EQ:
5253 code = BIT_IOR_EXPR;
5254 break;
5255 default:
5256 return lhs;
5258 c_parser_consume_token (parser);
5259 exp_location = c_parser_peek_token (parser)->location;
5260 rhs = c_parser_expr_no_commas (parser, NULL);
5261 rhs = default_function_array_read_conversion (exp_location, rhs);
5262 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5263 code, exp_location, rhs.value,
5264 rhs.original_type);
5265 if (code == NOP_EXPR)
5266 ret.original_code = MODIFY_EXPR;
5267 else
5269 TREE_NO_WARNING (ret.value) = 1;
5270 ret.original_code = ERROR_MARK;
5272 ret.original_type = NULL;
5273 return ret;
5276 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5277 is not NULL then it is an Objective-C message expression which is
5278 the primary-expression starting the expression as an initializer.
5280 conditional-expression:
5281 logical-OR-expression
5282 logical-OR-expression ? expression : conditional-expression
5284 GNU extensions:
5286 conditional-expression:
5287 logical-OR-expression ? : conditional-expression
5290 static struct c_expr
5291 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5293 struct c_expr cond, exp1, exp2, ret;
5294 location_t cond_loc, colon_loc, middle_loc;
5296 gcc_assert (!after || c_dialect_objc ());
5298 cond = c_parser_binary_expression (parser, after);
5300 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5301 return cond;
5302 cond_loc = c_parser_peek_token (parser)->location;
5303 cond = default_function_array_read_conversion (cond_loc, cond);
5304 c_parser_consume_token (parser);
5305 if (c_parser_next_token_is (parser, CPP_COLON))
5307 tree eptype = NULL_TREE;
5309 middle_loc = c_parser_peek_token (parser)->location;
5310 pedwarn (middle_loc, OPT_pedantic,
5311 "ISO C forbids omitting the middle term of a ?: expression");
5312 warn_for_omitted_condop (middle_loc, cond.value);
5313 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5315 eptype = TREE_TYPE (cond.value);
5316 cond.value = TREE_OPERAND (cond.value, 0);
5318 /* Make sure first operand is calculated only once. */
5319 exp1.value = c_save_expr (default_conversion (cond.value));
5320 if (eptype)
5321 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5322 exp1.original_type = NULL;
5323 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5324 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5326 else
5328 cond.value
5329 = c_objc_common_truthvalue_conversion
5330 (cond_loc, default_conversion (cond.value));
5331 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5332 exp1 = c_parser_expression_conv (parser);
5333 mark_exp_read (exp1.value);
5334 c_inhibit_evaluation_warnings +=
5335 ((cond.value == truthvalue_true_node)
5336 - (cond.value == truthvalue_false_node));
5339 colon_loc = c_parser_peek_token (parser)->location;
5340 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5342 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5343 ret.value = error_mark_node;
5344 ret.original_code = ERROR_MARK;
5345 ret.original_type = NULL;
5346 return ret;
5349 location_t exp2_loc = c_parser_peek_token (parser)->location;
5350 exp2 = c_parser_conditional_expression (parser, NULL);
5351 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5353 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5354 ret.value = build_conditional_expr (colon_loc, cond.value,
5355 cond.original_code == C_MAYBE_CONST_EXPR,
5356 exp1.value, exp1.original_type,
5357 exp2.value, exp2.original_type);
5358 ret.original_code = ERROR_MARK;
5359 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5360 ret.original_type = NULL;
5361 else
5363 tree t1, t2;
5365 /* If both sides are enum type, the default conversion will have
5366 made the type of the result be an integer type. We want to
5367 remember the enum types we started with. */
5368 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5369 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5370 ret.original_type = ((t1 != error_mark_node
5371 && t2 != error_mark_node
5372 && (TYPE_MAIN_VARIANT (t1)
5373 == TYPE_MAIN_VARIANT (t2)))
5374 ? t1
5375 : NULL);
5377 return ret;
5380 /* Parse a binary expression; that is, a logical-OR-expression (C90
5381 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5382 an Objective-C message expression which is the primary-expression
5383 starting the expression as an initializer.
5385 multiplicative-expression:
5386 cast-expression
5387 multiplicative-expression * cast-expression
5388 multiplicative-expression / cast-expression
5389 multiplicative-expression % cast-expression
5391 additive-expression:
5392 multiplicative-expression
5393 additive-expression + multiplicative-expression
5394 additive-expression - multiplicative-expression
5396 shift-expression:
5397 additive-expression
5398 shift-expression << additive-expression
5399 shift-expression >> additive-expression
5401 relational-expression:
5402 shift-expression
5403 relational-expression < shift-expression
5404 relational-expression > shift-expression
5405 relational-expression <= shift-expression
5406 relational-expression >= shift-expression
5408 equality-expression:
5409 relational-expression
5410 equality-expression == relational-expression
5411 equality-expression != relational-expression
5413 AND-expression:
5414 equality-expression
5415 AND-expression & equality-expression
5417 exclusive-OR-expression:
5418 AND-expression
5419 exclusive-OR-expression ^ AND-expression
5421 inclusive-OR-expression:
5422 exclusive-OR-expression
5423 inclusive-OR-expression | exclusive-OR-expression
5425 logical-AND-expression:
5426 inclusive-OR-expression
5427 logical-AND-expression && inclusive-OR-expression
5429 logical-OR-expression:
5430 logical-AND-expression
5431 logical-OR-expression || logical-AND-expression
5434 static struct c_expr
5435 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
5437 /* A binary expression is parsed using operator-precedence parsing,
5438 with the operands being cast expressions. All the binary
5439 operators are left-associative. Thus a binary expression is of
5440 form:
5442 E0 op1 E1 op2 E2 ...
5444 which we represent on a stack. On the stack, the precedence
5445 levels are strictly increasing. When a new operator is
5446 encountered of higher precedence than that at the top of the
5447 stack, it is pushed; its LHS is the top expression, and its RHS
5448 is everything parsed until it is popped. When a new operator is
5449 encountered with precedence less than or equal to that at the top
5450 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5451 by the result of the operation until the operator at the top of
5452 the stack has lower precedence than the new operator or there is
5453 only one element on the stack; then the top expression is the LHS
5454 of the new operator. In the case of logical AND and OR
5455 expressions, we also need to adjust c_inhibit_evaluation_warnings
5456 as appropriate when the operators are pushed and popped. */
5458 /* The precedence levels, where 0 is a dummy lowest level used for
5459 the bottom of the stack. */
5460 enum prec {
5461 PREC_NONE,
5462 PREC_LOGOR,
5463 PREC_LOGAND,
5464 PREC_BITOR,
5465 PREC_BITXOR,
5466 PREC_BITAND,
5467 PREC_EQ,
5468 PREC_REL,
5469 PREC_SHIFT,
5470 PREC_ADD,
5471 PREC_MULT,
5472 NUM_PRECS
5474 struct {
5475 /* The expression at this stack level. */
5476 struct c_expr expr;
5477 /* The precedence of the operator on its left, PREC_NONE at the
5478 bottom of the stack. */
5479 enum prec prec;
5480 /* The operation on its left. */
5481 enum tree_code op;
5482 /* The source location of this operation. */
5483 location_t loc;
5484 } stack[NUM_PRECS];
5485 int sp;
5486 /* Location of the binary operator. */
5487 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5488 #define POP \
5489 do { \
5490 switch (stack[sp].op) \
5492 case TRUTH_ANDIF_EXPR: \
5493 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5494 == truthvalue_false_node); \
5495 break; \
5496 case TRUTH_ORIF_EXPR: \
5497 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5498 == truthvalue_true_node); \
5499 break; \
5500 default: \
5501 break; \
5503 stack[sp - 1].expr \
5504 = default_function_array_read_conversion (stack[sp - 1].loc, \
5505 stack[sp - 1].expr); \
5506 stack[sp].expr \
5507 = default_function_array_read_conversion (stack[sp].loc, \
5508 stack[sp].expr); \
5509 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5510 stack[sp].op, \
5511 stack[sp - 1].expr, \
5512 stack[sp].expr); \
5513 sp--; \
5514 } while (0)
5515 gcc_assert (!after || c_dialect_objc ());
5516 stack[0].loc = c_parser_peek_token (parser)->location;
5517 stack[0].expr = c_parser_cast_expression (parser, after);
5518 stack[0].prec = PREC_NONE;
5519 sp = 0;
5520 while (true)
5522 enum prec oprec;
5523 enum tree_code ocode;
5524 if (parser->error)
5525 goto out;
5526 switch (c_parser_peek_token (parser)->type)
5528 case CPP_MULT:
5529 oprec = PREC_MULT;
5530 ocode = MULT_EXPR;
5531 break;
5532 case CPP_DIV:
5533 oprec = PREC_MULT;
5534 ocode = TRUNC_DIV_EXPR;
5535 break;
5536 case CPP_MOD:
5537 oprec = PREC_MULT;
5538 ocode = TRUNC_MOD_EXPR;
5539 break;
5540 case CPP_PLUS:
5541 oprec = PREC_ADD;
5542 ocode = PLUS_EXPR;
5543 break;
5544 case CPP_MINUS:
5545 oprec = PREC_ADD;
5546 ocode = MINUS_EXPR;
5547 break;
5548 case CPP_LSHIFT:
5549 oprec = PREC_SHIFT;
5550 ocode = LSHIFT_EXPR;
5551 break;
5552 case CPP_RSHIFT:
5553 oprec = PREC_SHIFT;
5554 ocode = RSHIFT_EXPR;
5555 break;
5556 case CPP_LESS:
5557 oprec = PREC_REL;
5558 ocode = LT_EXPR;
5559 break;
5560 case CPP_GREATER:
5561 oprec = PREC_REL;
5562 ocode = GT_EXPR;
5563 break;
5564 case CPP_LESS_EQ:
5565 oprec = PREC_REL;
5566 ocode = LE_EXPR;
5567 break;
5568 case CPP_GREATER_EQ:
5569 oprec = PREC_REL;
5570 ocode = GE_EXPR;
5571 break;
5572 case CPP_EQ_EQ:
5573 oprec = PREC_EQ;
5574 ocode = EQ_EXPR;
5575 break;
5576 case CPP_NOT_EQ:
5577 oprec = PREC_EQ;
5578 ocode = NE_EXPR;
5579 break;
5580 case CPP_AND:
5581 oprec = PREC_BITAND;
5582 ocode = BIT_AND_EXPR;
5583 break;
5584 case CPP_XOR:
5585 oprec = PREC_BITXOR;
5586 ocode = BIT_XOR_EXPR;
5587 break;
5588 case CPP_OR:
5589 oprec = PREC_BITOR;
5590 ocode = BIT_IOR_EXPR;
5591 break;
5592 case CPP_AND_AND:
5593 oprec = PREC_LOGAND;
5594 ocode = TRUTH_ANDIF_EXPR;
5595 break;
5596 case CPP_OR_OR:
5597 oprec = PREC_LOGOR;
5598 ocode = TRUTH_ORIF_EXPR;
5599 break;
5600 default:
5601 /* Not a binary operator, so end of the binary
5602 expression. */
5603 goto out;
5605 binary_loc = c_parser_peek_token (parser)->location;
5606 c_parser_consume_token (parser);
5607 while (oprec <= stack[sp].prec)
5608 POP;
5609 switch (ocode)
5611 case TRUTH_ANDIF_EXPR:
5612 stack[sp].expr
5613 = default_function_array_read_conversion (stack[sp].loc,
5614 stack[sp].expr);
5615 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5616 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5617 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5618 == truthvalue_false_node);
5619 break;
5620 case TRUTH_ORIF_EXPR:
5621 stack[sp].expr
5622 = default_function_array_read_conversion (stack[sp].loc,
5623 stack[sp].expr);
5624 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5625 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5626 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5627 == truthvalue_true_node);
5628 break;
5629 default:
5630 break;
5632 sp++;
5633 stack[sp].loc = binary_loc;
5634 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5635 stack[sp].prec = oprec;
5636 stack[sp].op = ocode;
5637 stack[sp].loc = binary_loc;
5639 out:
5640 while (sp > 0)
5641 POP;
5642 return stack[0].expr;
5643 #undef POP
5646 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5647 NULL then it is an Objective-C message expression which is the
5648 primary-expression starting the expression as an initializer.
5650 cast-expression:
5651 unary-expression
5652 ( type-name ) unary-expression
5655 static struct c_expr
5656 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5658 location_t cast_loc = c_parser_peek_token (parser)->location;
5659 gcc_assert (!after || c_dialect_objc ());
5660 if (after)
5661 return c_parser_postfix_expression_after_primary (parser,
5662 cast_loc, *after);
5663 /* If the expression begins with a parenthesized type name, it may
5664 be either a cast or a compound literal; we need to see whether
5665 the next character is '{' to tell the difference. If not, it is
5666 an unary expression. Full detection of unknown typenames here
5667 would require a 3-token lookahead. */
5668 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5669 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5671 struct c_type_name *type_name;
5672 struct c_expr ret;
5673 struct c_expr expr;
5674 c_parser_consume_token (parser);
5675 type_name = c_parser_type_name (parser);
5676 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5677 if (type_name == NULL)
5679 ret.value = error_mark_node;
5680 ret.original_code = ERROR_MARK;
5681 ret.original_type = NULL;
5682 return ret;
5685 /* Save casted types in the function's used types hash table. */
5686 used_types_insert (type_name->specs->type);
5688 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5689 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5690 cast_loc);
5692 location_t expr_loc = c_parser_peek_token (parser)->location;
5693 expr = c_parser_cast_expression (parser, NULL);
5694 expr = default_function_array_read_conversion (expr_loc, expr);
5696 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5697 ret.original_code = ERROR_MARK;
5698 ret.original_type = NULL;
5699 return ret;
5701 else
5702 return c_parser_unary_expression (parser);
5705 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5707 unary-expression:
5708 postfix-expression
5709 ++ unary-expression
5710 -- unary-expression
5711 unary-operator cast-expression
5712 sizeof unary-expression
5713 sizeof ( type-name )
5715 unary-operator: one of
5716 & * + - ~ !
5718 GNU extensions:
5720 unary-expression:
5721 __alignof__ unary-expression
5722 __alignof__ ( type-name )
5723 && identifier
5725 unary-operator: one of
5726 __extension__ __real__ __imag__
5728 In addition, the GNU syntax treats ++ and -- as unary operators, so
5729 they may be applied to cast expressions with errors for non-lvalues
5730 given later. */
5732 static struct c_expr
5733 c_parser_unary_expression (c_parser *parser)
5735 int ext;
5736 struct c_expr ret, op;
5737 location_t op_loc = c_parser_peek_token (parser)->location;
5738 location_t exp_loc;
5739 ret.original_code = ERROR_MARK;
5740 ret.original_type = NULL;
5741 switch (c_parser_peek_token (parser)->type)
5743 case CPP_PLUS_PLUS:
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, PREINCREMENT_EXPR, op);
5749 case CPP_MINUS_MINUS:
5750 c_parser_consume_token (parser);
5751 exp_loc = c_parser_peek_token (parser)->location;
5752 op = c_parser_cast_expression (parser, NULL);
5753 op = default_function_array_read_conversion (exp_loc, op);
5754 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5755 case CPP_AND:
5756 c_parser_consume_token (parser);
5757 op = c_parser_cast_expression (parser, NULL);
5758 mark_exp_read (op.value);
5759 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5760 case CPP_MULT:
5761 c_parser_consume_token (parser);
5762 exp_loc = c_parser_peek_token (parser)->location;
5763 op = c_parser_cast_expression (parser, NULL);
5764 op = default_function_array_read_conversion (exp_loc, op);
5765 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5766 return ret;
5767 case CPP_PLUS:
5768 if (!c_dialect_objc () && !in_system_header)
5769 warning_at (op_loc,
5770 OPT_Wtraditional,
5771 "traditional C rejects the unary plus operator");
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, CONVERT_EXPR, op);
5777 case CPP_MINUS:
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, NEGATE_EXPR, op);
5783 case CPP_COMPL:
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, BIT_NOT_EXPR, op);
5789 case CPP_NOT:
5790 c_parser_consume_token (parser);
5791 exp_loc = c_parser_peek_token (parser)->location;
5792 op = c_parser_cast_expression (parser, NULL);
5793 op = default_function_array_read_conversion (exp_loc, op);
5794 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5795 case CPP_AND_AND:
5796 /* Refer to the address of a label as a pointer. */
5797 c_parser_consume_token (parser);
5798 if (c_parser_next_token_is (parser, CPP_NAME))
5800 ret.value = finish_label_address_expr
5801 (c_parser_peek_token (parser)->value, op_loc);
5802 c_parser_consume_token (parser);
5804 else
5806 c_parser_error (parser, "expected identifier");
5807 ret.value = error_mark_node;
5809 return ret;
5810 case CPP_KEYWORD:
5811 switch (c_parser_peek_token (parser)->keyword)
5813 case RID_SIZEOF:
5814 return c_parser_sizeof_expression (parser);
5815 case RID_ALIGNOF:
5816 return c_parser_alignof_expression (parser);
5817 case RID_EXTENSION:
5818 c_parser_consume_token (parser);
5819 ext = disable_extension_diagnostics ();
5820 ret = c_parser_cast_expression (parser, NULL);
5821 restore_extension_diagnostics (ext);
5822 return ret;
5823 case RID_REALPART:
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, REALPART_EXPR, op);
5829 case RID_IMAGPART:
5830 c_parser_consume_token (parser);
5831 exp_loc = c_parser_peek_token (parser)->location;
5832 op = c_parser_cast_expression (parser, NULL);
5833 op = default_function_array_conversion (exp_loc, op);
5834 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5835 default:
5836 return c_parser_postfix_expression (parser);
5838 default:
5839 return c_parser_postfix_expression (parser);
5843 /* Parse a sizeof expression. */
5845 static struct c_expr
5846 c_parser_sizeof_expression (c_parser *parser)
5848 struct c_expr expr;
5849 location_t expr_loc;
5850 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5851 c_parser_consume_token (parser);
5852 c_inhibit_evaluation_warnings++;
5853 in_sizeof++;
5854 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5855 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5857 /* Either sizeof ( type-name ) or sizeof unary-expression
5858 starting with a compound literal. */
5859 struct c_type_name *type_name;
5860 c_parser_consume_token (parser);
5861 expr_loc = c_parser_peek_token (parser)->location;
5862 type_name = c_parser_type_name (parser);
5863 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5864 if (type_name == NULL)
5866 struct c_expr ret;
5867 c_inhibit_evaluation_warnings--;
5868 in_sizeof--;
5869 ret.value = error_mark_node;
5870 ret.original_code = ERROR_MARK;
5871 ret.original_type = NULL;
5872 return ret;
5874 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5876 expr = c_parser_postfix_expression_after_paren_type (parser,
5877 type_name,
5878 expr_loc);
5879 goto sizeof_expr;
5881 /* sizeof ( type-name ). */
5882 c_inhibit_evaluation_warnings--;
5883 in_sizeof--;
5884 return c_expr_sizeof_type (expr_loc, type_name);
5886 else
5888 expr_loc = c_parser_peek_token (parser)->location;
5889 expr = c_parser_unary_expression (parser);
5890 sizeof_expr:
5891 c_inhibit_evaluation_warnings--;
5892 in_sizeof--;
5893 mark_exp_read (expr.value);
5894 if (TREE_CODE (expr.value) == COMPONENT_REF
5895 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5896 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5897 return c_expr_sizeof_expr (expr_loc, expr);
5901 /* Parse an alignof expression. */
5903 static struct c_expr
5904 c_parser_alignof_expression (c_parser *parser)
5906 struct c_expr expr;
5907 location_t loc = c_parser_peek_token (parser)->location;
5908 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5909 c_parser_consume_token (parser);
5910 c_inhibit_evaluation_warnings++;
5911 in_alignof++;
5912 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5913 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5915 /* Either __alignof__ ( type-name ) or __alignof__
5916 unary-expression starting with a compound literal. */
5917 location_t loc;
5918 struct c_type_name *type_name;
5919 struct c_expr ret;
5920 c_parser_consume_token (parser);
5921 loc = c_parser_peek_token (parser)->location;
5922 type_name = c_parser_type_name (parser);
5923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5924 if (type_name == NULL)
5926 struct c_expr ret;
5927 c_inhibit_evaluation_warnings--;
5928 in_alignof--;
5929 ret.value = error_mark_node;
5930 ret.original_code = ERROR_MARK;
5931 ret.original_type = NULL;
5932 return ret;
5934 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5936 expr = c_parser_postfix_expression_after_paren_type (parser,
5937 type_name,
5938 loc);
5939 goto alignof_expr;
5941 /* alignof ( type-name ). */
5942 c_inhibit_evaluation_warnings--;
5943 in_alignof--;
5944 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5945 ret.original_code = ERROR_MARK;
5946 ret.original_type = NULL;
5947 return ret;
5949 else
5951 struct c_expr ret;
5952 expr = c_parser_unary_expression (parser);
5953 alignof_expr:
5954 mark_exp_read (expr.value);
5955 c_inhibit_evaluation_warnings--;
5956 in_alignof--;
5957 ret.value = c_alignof_expr (loc, expr.value);
5958 ret.original_code = ERROR_MARK;
5959 ret.original_type = NULL;
5960 return ret;
5964 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5966 postfix-expression:
5967 primary-expression
5968 postfix-expression [ expression ]
5969 postfix-expression ( argument-expression-list[opt] )
5970 postfix-expression . identifier
5971 postfix-expression -> identifier
5972 postfix-expression ++
5973 postfix-expression --
5974 ( type-name ) { initializer-list }
5975 ( type-name ) { initializer-list , }
5977 argument-expression-list:
5978 argument-expression
5979 argument-expression-list , argument-expression
5981 primary-expression:
5982 identifier
5983 constant
5984 string-literal
5985 ( expression )
5987 GNU extensions:
5989 primary-expression:
5990 __func__
5991 (treated as a keyword in GNU C)
5992 __FUNCTION__
5993 __PRETTY_FUNCTION__
5994 ( compound-statement )
5995 __builtin_va_arg ( assignment-expression , type-name )
5996 __builtin_offsetof ( type-name , offsetof-member-designator )
5997 __builtin_choose_expr ( assignment-expression ,
5998 assignment-expression ,
5999 assignment-expression )
6000 __builtin_types_compatible_p ( type-name , type-name )
6002 offsetof-member-designator:
6003 identifier
6004 offsetof-member-designator . identifier
6005 offsetof-member-designator [ expression ]
6007 Objective-C:
6009 primary-expression:
6010 [ objc-receiver objc-message-args ]
6011 @selector ( objc-selector-arg )
6012 @protocol ( identifier )
6013 @encode ( type-name )
6014 objc-string-literal
6015 Classname . identifier
6018 static struct c_expr
6019 c_parser_postfix_expression (c_parser *parser)
6021 struct c_expr expr, e1, e2, e3;
6022 struct c_type_name *t1, *t2;
6023 location_t loc = c_parser_peek_token (parser)->location;;
6024 expr.original_code = ERROR_MARK;
6025 expr.original_type = NULL;
6026 switch (c_parser_peek_token (parser)->type)
6028 case CPP_NUMBER:
6029 expr.value = c_parser_peek_token (parser)->value;
6030 loc = c_parser_peek_token (parser)->location;
6031 c_parser_consume_token (parser);
6032 if (TREE_CODE (expr.value) == FIXED_CST
6033 && !targetm.fixed_point_supported_p ())
6035 error_at (loc, "fixed-point types not supported for this target");
6036 expr.value = error_mark_node;
6038 break;
6039 case CPP_CHAR:
6040 case CPP_CHAR16:
6041 case CPP_CHAR32:
6042 case CPP_WCHAR:
6043 expr.value = c_parser_peek_token (parser)->value;
6044 c_parser_consume_token (parser);
6045 break;
6046 case CPP_STRING:
6047 case CPP_STRING16:
6048 case CPP_STRING32:
6049 case CPP_WSTRING:
6050 case CPP_UTF8STRING:
6051 expr.value = c_parser_peek_token (parser)->value;
6052 expr.original_code = STRING_CST;
6053 c_parser_consume_token (parser);
6054 break;
6055 case CPP_OBJC_STRING:
6056 gcc_assert (c_dialect_objc ());
6057 expr.value
6058 = objc_build_string_object (c_parser_peek_token (parser)->value);
6059 c_parser_consume_token (parser);
6060 break;
6061 case CPP_NAME:
6062 switch (c_parser_peek_token (parser)->id_kind)
6064 case C_ID_ID:
6066 tree id = c_parser_peek_token (parser)->value;
6067 c_parser_consume_token (parser);
6068 expr.value = build_external_ref (loc, id,
6069 (c_parser_peek_token (parser)->type
6070 == CPP_OPEN_PAREN),
6071 &expr.original_type);
6072 break;
6074 case C_ID_CLASSNAME:
6076 /* Here we parse the Objective-C 2.0 Class.name dot
6077 syntax. */
6078 tree class_name = c_parser_peek_token (parser)->value;
6079 tree component;
6080 c_parser_consume_token (parser);
6081 gcc_assert (c_dialect_objc ());
6082 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6084 expr.value = error_mark_node;
6085 break;
6087 if (c_parser_next_token_is_not (parser, CPP_NAME))
6089 c_parser_error (parser, "expected identifier");
6090 expr.value = error_mark_node;
6091 break;
6093 component = c_parser_peek_token (parser)->value;
6094 c_parser_consume_token (parser);
6095 expr.value = objc_build_class_component_ref (class_name,
6096 component);
6097 break;
6099 default:
6100 c_parser_error (parser, "expected expression");
6101 expr.value = error_mark_node;
6102 break;
6104 break;
6105 case CPP_OPEN_PAREN:
6106 /* A parenthesized expression, statement expression or compound
6107 literal. */
6108 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6110 /* A statement expression. */
6111 tree stmt;
6112 location_t brace_loc;
6113 c_parser_consume_token (parser);
6114 brace_loc = c_parser_peek_token (parser)->location;
6115 c_parser_consume_token (parser);
6116 if (cur_stmt_list == NULL)
6118 error_at (loc, "braced-group within expression allowed "
6119 "only inside a function");
6120 parser->error = true;
6121 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6123 expr.value = error_mark_node;
6124 break;
6126 stmt = c_begin_stmt_expr ();
6127 c_parser_compound_statement_nostart (parser);
6128 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6129 "expected %<)%>");
6130 pedwarn (loc, OPT_pedantic,
6131 "ISO C forbids braced-groups within expressions");
6132 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6133 mark_exp_read (expr.value);
6135 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6137 /* A compound literal. ??? Can we actually get here rather
6138 than going directly to
6139 c_parser_postfix_expression_after_paren_type from
6140 elsewhere? */
6141 location_t loc;
6142 struct c_type_name *type_name;
6143 c_parser_consume_token (parser);
6144 loc = c_parser_peek_token (parser)->location;
6145 type_name = c_parser_type_name (parser);
6146 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6147 "expected %<)%>");
6148 if (type_name == NULL)
6150 expr.value = error_mark_node;
6152 else
6153 expr = c_parser_postfix_expression_after_paren_type (parser,
6154 type_name,
6155 loc);
6157 else
6159 /* A parenthesized expression. */
6160 c_parser_consume_token (parser);
6161 expr = c_parser_expression (parser);
6162 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6163 TREE_NO_WARNING (expr.value) = 1;
6164 if (expr.original_code != C_MAYBE_CONST_EXPR)
6165 expr.original_code = ERROR_MARK;
6166 /* Don't change EXPR.ORIGINAL_TYPE. */
6167 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6168 "expected %<)%>");
6170 break;
6171 case CPP_KEYWORD:
6172 switch (c_parser_peek_token (parser)->keyword)
6174 case RID_FUNCTION_NAME:
6175 case RID_PRETTY_FUNCTION_NAME:
6176 case RID_C99_FUNCTION_NAME:
6177 expr.value = fname_decl (loc,
6178 c_parser_peek_token (parser)->keyword,
6179 c_parser_peek_token (parser)->value);
6180 c_parser_consume_token (parser);
6181 break;
6182 case RID_VA_ARG:
6183 c_parser_consume_token (parser);
6184 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6186 expr.value = error_mark_node;
6187 break;
6189 e1 = c_parser_expr_no_commas (parser, NULL);
6190 mark_exp_read (e1.value);
6191 e1.value = c_fully_fold (e1.value, false, NULL);
6192 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6194 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6195 expr.value = error_mark_node;
6196 break;
6198 loc = c_parser_peek_token (parser)->location;
6199 t1 = c_parser_type_name (parser);
6200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6201 "expected %<)%>");
6202 if (t1 == NULL)
6204 expr.value = error_mark_node;
6206 else
6208 tree type_expr = NULL_TREE;
6209 expr.value = c_build_va_arg (loc, e1.value,
6210 groktypename (t1, &type_expr, NULL));
6211 if (type_expr)
6213 expr.value = build2 (C_MAYBE_CONST_EXPR,
6214 TREE_TYPE (expr.value), type_expr,
6215 expr.value);
6216 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6219 break;
6220 case RID_OFFSETOF:
6221 c_parser_consume_token (parser);
6222 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6224 expr.value = error_mark_node;
6225 break;
6227 t1 = c_parser_type_name (parser);
6228 if (t1 == NULL)
6229 parser->error = true;
6230 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6231 gcc_assert (parser->error);
6232 if (parser->error)
6234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6235 expr.value = error_mark_node;
6236 break;
6240 tree type = groktypename (t1, NULL, NULL);
6241 tree offsetof_ref;
6242 if (type == error_mark_node)
6243 offsetof_ref = error_mark_node;
6244 else
6246 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6247 SET_EXPR_LOCATION (offsetof_ref, loc);
6249 /* Parse the second argument to __builtin_offsetof. We
6250 must have one identifier, and beyond that we want to
6251 accept sub structure and sub array references. */
6252 if (c_parser_next_token_is (parser, CPP_NAME))
6254 offsetof_ref = build_component_ref
6255 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6256 c_parser_consume_token (parser);
6257 while (c_parser_next_token_is (parser, CPP_DOT)
6258 || c_parser_next_token_is (parser,
6259 CPP_OPEN_SQUARE)
6260 || c_parser_next_token_is (parser,
6261 CPP_DEREF))
6263 if (c_parser_next_token_is (parser, CPP_DEREF))
6265 loc = c_parser_peek_token (parser)->location;
6266 offsetof_ref = build_array_ref (loc,
6267 offsetof_ref,
6268 integer_zero_node);
6269 goto do_dot;
6271 else if (c_parser_next_token_is (parser, CPP_DOT))
6273 do_dot:
6274 c_parser_consume_token (parser);
6275 if (c_parser_next_token_is_not (parser,
6276 CPP_NAME))
6278 c_parser_error (parser, "expected identifier");
6279 break;
6281 offsetof_ref = build_component_ref
6282 (loc, offsetof_ref,
6283 c_parser_peek_token (parser)->value);
6284 c_parser_consume_token (parser);
6286 else
6288 tree idx;
6289 loc = c_parser_peek_token (parser)->location;
6290 c_parser_consume_token (parser);
6291 idx = c_parser_expression (parser).value;
6292 idx = c_fully_fold (idx, false, NULL);
6293 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6294 "expected %<]%>");
6295 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6299 else
6300 c_parser_error (parser, "expected identifier");
6301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6302 "expected %<)%>");
6303 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6305 break;
6306 case RID_CHOOSE_EXPR:
6307 c_parser_consume_token (parser);
6308 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6310 expr.value = error_mark_node;
6311 break;
6313 loc = c_parser_peek_token (parser)->location;
6314 e1 = c_parser_expr_no_commas (parser, NULL);
6315 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6317 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6318 expr.value = error_mark_node;
6319 break;
6321 e2 = c_parser_expr_no_commas (parser, NULL);
6322 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6324 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6325 expr.value = error_mark_node;
6326 break;
6328 e3 = c_parser_expr_no_commas (parser, NULL);
6329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6330 "expected %<)%>");
6332 tree c;
6334 c = e1.value;
6335 mark_exp_read (e2.value);
6336 mark_exp_read (e3.value);
6337 if (TREE_CODE (c) != INTEGER_CST
6338 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6339 error_at (loc,
6340 "first argument to %<__builtin_choose_expr%> not"
6341 " a constant");
6342 constant_expression_warning (c);
6343 expr = integer_zerop (c) ? e3 : e2;
6345 break;
6346 case RID_TYPES_COMPATIBLE_P:
6347 c_parser_consume_token (parser);
6348 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6350 expr.value = error_mark_node;
6351 break;
6353 t1 = c_parser_type_name (parser);
6354 if (t1 == NULL)
6356 expr.value = error_mark_node;
6357 break;
6359 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6362 expr.value = error_mark_node;
6363 break;
6365 t2 = c_parser_type_name (parser);
6366 if (t2 == NULL)
6368 expr.value = error_mark_node;
6369 break;
6371 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6372 "expected %<)%>");
6374 tree e1, e2;
6376 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6377 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6379 expr.value
6380 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6382 break;
6383 case RID_AT_SELECTOR:
6384 gcc_assert (c_dialect_objc ());
6385 c_parser_consume_token (parser);
6386 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6388 expr.value = error_mark_node;
6389 break;
6392 tree sel = c_parser_objc_selector_arg (parser);
6393 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6394 "expected %<)%>");
6395 expr.value = objc_build_selector_expr (loc, sel);
6397 break;
6398 case RID_AT_PROTOCOL:
6399 gcc_assert (c_dialect_objc ());
6400 c_parser_consume_token (parser);
6401 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6403 expr.value = error_mark_node;
6404 break;
6406 if (c_parser_next_token_is_not (parser, CPP_NAME))
6408 c_parser_error (parser, "expected identifier");
6409 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6410 expr.value = error_mark_node;
6411 break;
6414 tree id = c_parser_peek_token (parser)->value;
6415 c_parser_consume_token (parser);
6416 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6417 "expected %<)%>");
6418 expr.value = objc_build_protocol_expr (id);
6420 break;
6421 case RID_AT_ENCODE:
6422 /* Extension to support C-structures in the archiver. */
6423 gcc_assert (c_dialect_objc ());
6424 c_parser_consume_token (parser);
6425 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6427 expr.value = error_mark_node;
6428 break;
6430 t1 = c_parser_type_name (parser);
6431 if (t1 == NULL)
6433 expr.value = error_mark_node;
6434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6435 break;
6437 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6438 "expected %<)%>");
6440 tree type = groktypename (t1, NULL, NULL);
6441 expr.value = objc_build_encode_expr (type);
6443 break;
6444 default:
6445 c_parser_error (parser, "expected expression");
6446 expr.value = error_mark_node;
6447 break;
6449 break;
6450 case CPP_OPEN_SQUARE:
6451 if (c_dialect_objc ())
6453 tree receiver, args;
6454 c_parser_consume_token (parser);
6455 receiver = c_parser_objc_receiver (parser);
6456 args = c_parser_objc_message_args (parser);
6457 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6458 "expected %<]%>");
6459 expr.value = objc_build_message_expr (build_tree_list (receiver,
6460 args));
6461 break;
6463 /* Else fall through to report error. */
6464 default:
6465 c_parser_error (parser, "expected expression");
6466 expr.value = error_mark_node;
6467 break;
6469 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6472 /* Parse a postfix expression after a parenthesized type name: the
6473 brace-enclosed initializer of a compound literal, possibly followed
6474 by some postfix operators. This is separate because it is not
6475 possible to tell until after the type name whether a cast
6476 expression has a cast or a compound literal, or whether the operand
6477 of sizeof is a parenthesized type name or starts with a compound
6478 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6479 location of the first token after the parentheses around the type
6480 name. */
6482 static struct c_expr
6483 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6484 struct c_type_name *type_name,
6485 location_t type_loc)
6487 tree type;
6488 struct c_expr init;
6489 bool non_const;
6490 struct c_expr expr;
6491 location_t start_loc;
6492 tree type_expr = NULL_TREE;
6493 bool type_expr_const = true;
6494 check_compound_literal_type (type_loc, type_name);
6495 start_init (NULL_TREE, NULL, 0);
6496 type = groktypename (type_name, &type_expr, &type_expr_const);
6497 start_loc = c_parser_peek_token (parser)->location;
6498 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6500 error_at (type_loc, "compound literal has variable size");
6501 type = error_mark_node;
6503 init = c_parser_braced_init (parser, type, false);
6504 finish_init ();
6505 maybe_warn_string_init (type, init);
6507 if (type != error_mark_node
6508 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6509 && current_function_decl)
6511 error ("compound literal qualified by address-space qualifier");
6512 type = error_mark_node;
6515 if (!flag_isoc99)
6516 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6517 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6518 ? CONSTRUCTOR_NON_CONST (init.value)
6519 : init.original_code == C_MAYBE_CONST_EXPR);
6520 non_const |= !type_expr_const;
6521 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6522 expr.original_code = ERROR_MARK;
6523 expr.original_type = NULL;
6524 if (type_expr)
6526 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6528 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6529 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6531 else
6533 gcc_assert (!non_const);
6534 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6535 type_expr, expr.value);
6538 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6541 /* Parse a postfix expression after the initial primary or compound
6542 literal; that is, parse a series of postfix operators.
6544 EXPR_LOC is the location of the primary expression. */
6546 static struct c_expr
6547 c_parser_postfix_expression_after_primary (c_parser *parser,
6548 location_t expr_loc,
6549 struct c_expr expr)
6551 struct c_expr orig_expr;
6552 tree ident, idx;
6553 VEC(tree,gc) *exprlist;
6554 VEC(tree,gc) *origtypes;
6555 while (true)
6557 location_t op_loc = c_parser_peek_token (parser)->location;
6558 switch (c_parser_peek_token (parser)->type)
6560 case CPP_OPEN_SQUARE:
6561 /* Array reference. */
6562 c_parser_consume_token (parser);
6563 idx = c_parser_expression (parser).value;
6564 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6565 "expected %<]%>");
6566 expr.value = build_array_ref (op_loc, expr.value, idx);
6567 expr.original_code = ERROR_MARK;
6568 expr.original_type = NULL;
6569 break;
6570 case CPP_OPEN_PAREN:
6571 /* Function call. */
6572 c_parser_consume_token (parser);
6573 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6574 exprlist = NULL;
6575 else
6576 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6577 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6578 "expected %<)%>");
6579 orig_expr = expr;
6580 mark_exp_read (expr.value);
6581 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6582 "(" after the FUNCNAME, which is what we have now. */
6583 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6584 origtypes);
6585 expr.original_code = ERROR_MARK;
6586 if (TREE_CODE (expr.value) == INTEGER_CST
6587 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6588 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6589 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6590 expr.original_code = C_MAYBE_CONST_EXPR;
6591 expr.original_type = NULL;
6592 if (exprlist != NULL)
6594 release_tree_vector (exprlist);
6595 release_tree_vector (origtypes);
6597 break;
6598 case CPP_DOT:
6599 /* Structure element reference. */
6600 c_parser_consume_token (parser);
6601 expr = default_function_array_conversion (expr_loc, expr);
6602 if (c_parser_next_token_is (parser, CPP_NAME))
6603 ident = c_parser_peek_token (parser)->value;
6604 else
6606 c_parser_error (parser, "expected identifier");
6607 expr.value = error_mark_node;
6608 expr.original_code = ERROR_MARK;
6609 expr.original_type = NULL;
6610 return expr;
6612 c_parser_consume_token (parser);
6613 expr.value = build_component_ref (op_loc, expr.value, ident);
6614 expr.original_code = ERROR_MARK;
6615 if (TREE_CODE (expr.value) != COMPONENT_REF)
6616 expr.original_type = NULL;
6617 else
6619 /* Remember the original type of a bitfield. */
6620 tree field = TREE_OPERAND (expr.value, 1);
6621 if (TREE_CODE (field) != FIELD_DECL)
6622 expr.original_type = NULL;
6623 else
6624 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6626 break;
6627 case CPP_DEREF:
6628 /* Structure element reference. */
6629 c_parser_consume_token (parser);
6630 expr = default_function_array_conversion (expr_loc, expr);
6631 if (c_parser_next_token_is (parser, CPP_NAME))
6632 ident = c_parser_peek_token (parser)->value;
6633 else
6635 c_parser_error (parser, "expected identifier");
6636 expr.value = error_mark_node;
6637 expr.original_code = ERROR_MARK;
6638 expr.original_type = NULL;
6639 return expr;
6641 c_parser_consume_token (parser);
6642 expr.value = build_component_ref (op_loc,
6643 build_indirect_ref (op_loc,
6644 expr.value,
6645 RO_ARROW),
6646 ident);
6647 expr.original_code = ERROR_MARK;
6648 if (TREE_CODE (expr.value) != COMPONENT_REF)
6649 expr.original_type = NULL;
6650 else
6652 /* Remember the original type of a bitfield. */
6653 tree field = TREE_OPERAND (expr.value, 1);
6654 if (TREE_CODE (field) != FIELD_DECL)
6655 expr.original_type = NULL;
6656 else
6657 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6659 break;
6660 case CPP_PLUS_PLUS:
6661 /* Postincrement. */
6662 c_parser_consume_token (parser);
6663 expr = default_function_array_read_conversion (expr_loc, expr);
6664 expr.value = build_unary_op (op_loc,
6665 POSTINCREMENT_EXPR, expr.value, 0);
6666 expr.original_code = ERROR_MARK;
6667 expr.original_type = NULL;
6668 break;
6669 case CPP_MINUS_MINUS:
6670 /* Postdecrement. */
6671 c_parser_consume_token (parser);
6672 expr = default_function_array_read_conversion (expr_loc, expr);
6673 expr.value = build_unary_op (op_loc,
6674 POSTDECREMENT_EXPR, expr.value, 0);
6675 expr.original_code = ERROR_MARK;
6676 expr.original_type = NULL;
6677 break;
6678 default:
6679 return expr;
6684 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6686 expression:
6687 assignment-expression
6688 expression , assignment-expression
6691 static struct c_expr
6692 c_parser_expression (c_parser *parser)
6694 struct c_expr expr;
6695 expr = c_parser_expr_no_commas (parser, NULL);
6696 while (c_parser_next_token_is (parser, CPP_COMMA))
6698 struct c_expr next;
6699 tree lhsval;
6700 location_t loc = c_parser_peek_token (parser)->location;
6701 location_t expr_loc;
6702 c_parser_consume_token (parser);
6703 expr_loc = c_parser_peek_token (parser)->location;
6704 lhsval = expr.value;
6705 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6706 lhsval = TREE_OPERAND (lhsval, 1);
6707 if (DECL_P (lhsval) || handled_component_p (lhsval))
6708 mark_exp_read (lhsval);
6709 next = c_parser_expr_no_commas (parser, NULL);
6710 next = default_function_array_conversion (expr_loc, next);
6711 expr.value = build_compound_expr (loc, expr.value, next.value);
6712 expr.original_code = COMPOUND_EXPR;
6713 expr.original_type = next.original_type;
6715 return expr;
6718 /* Parse an expression and convert functions or arrays to
6719 pointers. */
6721 static struct c_expr
6722 c_parser_expression_conv (c_parser *parser)
6724 struct c_expr expr;
6725 location_t loc = c_parser_peek_token (parser)->location;
6726 expr = c_parser_expression (parser);
6727 expr = default_function_array_conversion (loc, expr);
6728 return expr;
6731 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6732 functions and arrays to pointers. If FOLD_P, fold the expressions.
6734 nonempty-expr-list:
6735 assignment-expression
6736 nonempty-expr-list , assignment-expression
6739 static VEC(tree,gc) *
6740 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6741 VEC(tree,gc) **p_orig_types)
6743 VEC(tree,gc) *ret;
6744 VEC(tree,gc) *orig_types;
6745 struct c_expr expr;
6746 location_t loc = c_parser_peek_token (parser)->location;
6748 ret = make_tree_vector ();
6749 if (p_orig_types == NULL)
6750 orig_types = NULL;
6751 else
6752 orig_types = make_tree_vector ();
6754 expr = c_parser_expr_no_commas (parser, NULL);
6755 if (convert_p)
6756 expr = default_function_array_read_conversion (loc, expr);
6757 if (fold_p)
6758 expr.value = c_fully_fold (expr.value, false, NULL);
6759 VEC_quick_push (tree, ret, expr.value);
6760 if (orig_types != NULL)
6761 VEC_quick_push (tree, orig_types, expr.original_type);
6762 while (c_parser_next_token_is (parser, CPP_COMMA))
6764 c_parser_consume_token (parser);
6765 loc = c_parser_peek_token (parser)->location;
6766 expr = c_parser_expr_no_commas (parser, NULL);
6767 if (convert_p)
6768 expr = default_function_array_read_conversion (loc, expr);
6769 if (fold_p)
6770 expr.value = c_fully_fold (expr.value, false, NULL);
6771 VEC_safe_push (tree, gc, ret, expr.value);
6772 if (orig_types != NULL)
6773 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6775 if (orig_types != NULL)
6776 *p_orig_types = orig_types;
6777 return ret;
6780 /* Parse Objective-C-specific constructs. */
6782 /* Parse an objc-class-definition.
6784 objc-class-definition:
6785 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6786 objc-class-instance-variables[opt] objc-methodprotolist @end
6787 @implementation identifier objc-superclass[opt]
6788 objc-class-instance-variables[opt]
6789 @interface identifier ( identifier ) objc-protocol-refs[opt]
6790 objc-methodprotolist @end
6791 @interface identifier ( ) objc-protocol-refs[opt]
6792 objc-methodprotolist @end
6793 @implementation identifier ( identifier )
6795 objc-superclass:
6796 : identifier
6798 "@interface identifier (" must start "@interface identifier (
6799 identifier ) ...": objc-methodprotolist in the first production may
6800 not start with a parenthesized identifier as a declarator of a data
6801 definition with no declaration specifiers if the objc-superclass,
6802 objc-protocol-refs and objc-class-instance-variables are omitted. */
6804 static void
6805 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6807 bool iface_p;
6808 tree id1;
6809 tree superclass;
6810 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6811 iface_p = true;
6812 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6813 iface_p = false;
6814 else
6815 gcc_unreachable ();
6817 c_parser_consume_token (parser);
6818 if (c_parser_next_token_is_not (parser, CPP_NAME))
6820 c_parser_error (parser, "expected identifier");
6821 return;
6823 id1 = c_parser_peek_token (parser)->value;
6824 c_parser_consume_token (parser);
6825 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6827 /* We have a category or class extension. */
6828 tree id2;
6829 tree proto = NULL_TREE;
6830 c_parser_consume_token (parser);
6831 if (c_parser_next_token_is_not (parser, CPP_NAME))
6833 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6835 /* We have a class extension. */
6836 id2 = NULL_TREE;
6838 else
6840 c_parser_error (parser, "expected identifier or %<)%>");
6841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6842 return;
6845 else
6847 id2 = c_parser_peek_token (parser)->value;
6848 c_parser_consume_token (parser);
6850 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6851 if (!iface_p)
6853 objc_start_category_implementation (id1, id2);
6854 return;
6856 if (c_parser_next_token_is (parser, CPP_LESS))
6857 proto = c_parser_objc_protocol_refs (parser);
6858 objc_start_category_interface (id1, id2, proto, attributes);
6859 c_parser_objc_methodprotolist (parser);
6860 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6861 objc_finish_interface ();
6862 return;
6864 if (c_parser_next_token_is (parser, CPP_COLON))
6866 c_parser_consume_token (parser);
6867 if (c_parser_next_token_is_not (parser, CPP_NAME))
6869 c_parser_error (parser, "expected identifier");
6870 return;
6872 superclass = c_parser_peek_token (parser)->value;
6873 c_parser_consume_token (parser);
6875 else
6876 superclass = NULL_TREE;
6877 if (iface_p)
6879 tree proto = NULL_TREE;
6880 if (c_parser_next_token_is (parser, CPP_LESS))
6881 proto = c_parser_objc_protocol_refs (parser);
6882 objc_start_class_interface (id1, superclass, proto, attributes);
6884 else
6885 objc_start_class_implementation (id1, superclass);
6886 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6887 c_parser_objc_class_instance_variables (parser);
6888 if (iface_p)
6890 objc_continue_interface ();
6891 c_parser_objc_methodprotolist (parser);
6892 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6893 objc_finish_interface ();
6895 else
6897 objc_continue_implementation ();
6898 return;
6902 /* Parse objc-class-instance-variables.
6904 objc-class-instance-variables:
6905 { objc-instance-variable-decl-list[opt] }
6907 objc-instance-variable-decl-list:
6908 objc-visibility-spec
6909 objc-instance-variable-decl ;
6911 objc-instance-variable-decl-list objc-visibility-spec
6912 objc-instance-variable-decl-list objc-instance-variable-decl ;
6913 objc-instance-variable-decl-list ;
6915 objc-visibility-spec:
6916 @private
6917 @protected
6918 @public
6920 objc-instance-variable-decl:
6921 struct-declaration
6924 static void
6925 c_parser_objc_class_instance_variables (c_parser *parser)
6927 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6928 c_parser_consume_token (parser);
6929 while (c_parser_next_token_is_not (parser, CPP_EOF))
6931 tree decls;
6932 /* Parse any stray semicolon. */
6933 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6935 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6936 "extra semicolon in struct or union specified");
6937 c_parser_consume_token (parser);
6938 continue;
6940 /* Stop if at the end of the instance variables. */
6941 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6943 c_parser_consume_token (parser);
6944 break;
6946 /* Parse any objc-visibility-spec. */
6947 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6949 c_parser_consume_token (parser);
6950 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6951 continue;
6953 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6955 c_parser_consume_token (parser);
6956 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6957 continue;
6959 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6961 c_parser_consume_token (parser);
6962 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6963 continue;
6965 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6967 c_parser_consume_token (parser);
6968 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6969 continue;
6971 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6973 c_parser_pragma (parser, pragma_external);
6974 continue;
6977 /* Parse some comma-separated declarations. */
6978 decls = c_parser_struct_declaration (parser);
6980 /* Comma-separated instance variables are chained together in
6981 reverse order; add them one by one. */
6982 tree ivar = nreverse (decls);
6983 for (; ivar; ivar = DECL_CHAIN (ivar))
6984 objc_add_instance_variable (copy_node (ivar));
6986 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6990 /* Parse an objc-class-declaration.
6992 objc-class-declaration:
6993 @class identifier-list ;
6996 static void
6997 c_parser_objc_class_declaration (c_parser *parser)
6999 tree list = NULL_TREE;
7000 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7001 c_parser_consume_token (parser);
7002 /* Any identifiers, including those declared as type names, are OK
7003 here. */
7004 while (true)
7006 tree id;
7007 if (c_parser_next_token_is_not (parser, CPP_NAME))
7009 c_parser_error (parser, "expected identifier");
7010 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7011 parser->error = false;
7012 return;
7014 id = c_parser_peek_token (parser)->value;
7015 list = chainon (list, build_tree_list (NULL_TREE, id));
7016 c_parser_consume_token (parser);
7017 if (c_parser_next_token_is (parser, CPP_COMMA))
7018 c_parser_consume_token (parser);
7019 else
7020 break;
7022 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7023 objc_declare_class (list);
7026 /* Parse an objc-alias-declaration.
7028 objc-alias-declaration:
7029 @compatibility_alias identifier identifier ;
7032 static void
7033 c_parser_objc_alias_declaration (c_parser *parser)
7035 tree id1, id2;
7036 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7037 c_parser_consume_token (parser);
7038 if (c_parser_next_token_is_not (parser, CPP_NAME))
7040 c_parser_error (parser, "expected identifier");
7041 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7042 return;
7044 id1 = c_parser_peek_token (parser)->value;
7045 c_parser_consume_token (parser);
7046 if (c_parser_next_token_is_not (parser, CPP_NAME))
7048 c_parser_error (parser, "expected identifier");
7049 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7050 return;
7052 id2 = c_parser_peek_token (parser)->value;
7053 c_parser_consume_token (parser);
7054 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7055 objc_declare_alias (id1, id2);
7058 /* Parse an objc-protocol-definition.
7060 objc-protocol-definition:
7061 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7062 @protocol identifier-list ;
7064 "@protocol identifier ;" should be resolved as "@protocol
7065 identifier-list ;": objc-methodprotolist may not start with a
7066 semicolon in the first alternative if objc-protocol-refs are
7067 omitted. */
7069 static void
7070 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7072 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7074 c_parser_consume_token (parser);
7075 if (c_parser_next_token_is_not (parser, CPP_NAME))
7077 c_parser_error (parser, "expected identifier");
7078 return;
7080 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7081 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7083 tree list = NULL_TREE;
7084 /* Any identifiers, including those declared as type names, are
7085 OK here. */
7086 while (true)
7088 tree id;
7089 if (c_parser_next_token_is_not (parser, CPP_NAME))
7091 c_parser_error (parser, "expected identifier");
7092 break;
7094 id = c_parser_peek_token (parser)->value;
7095 list = chainon (list, build_tree_list (NULL_TREE, id));
7096 c_parser_consume_token (parser);
7097 if (c_parser_next_token_is (parser, CPP_COMMA))
7098 c_parser_consume_token (parser);
7099 else
7100 break;
7102 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7103 objc_declare_protocols (list, attributes);
7105 else
7107 tree id = c_parser_peek_token (parser)->value;
7108 tree proto = NULL_TREE;
7109 c_parser_consume_token (parser);
7110 if (c_parser_next_token_is (parser, CPP_LESS))
7111 proto = c_parser_objc_protocol_refs (parser);
7112 parser->objc_pq_context = true;
7113 objc_start_protocol (id, proto, attributes);
7114 c_parser_objc_methodprotolist (parser);
7115 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7116 parser->objc_pq_context = false;
7117 objc_finish_interface ();
7121 /* Parse an objc-method-type.
7123 objc-method-type:
7127 Return true if it is a class method (+) and false if it is
7128 an instance method (-).
7130 static inline bool
7131 c_parser_objc_method_type (c_parser *parser)
7133 switch (c_parser_peek_token (parser)->type)
7135 case CPP_PLUS:
7136 c_parser_consume_token (parser);
7137 return true;
7138 case CPP_MINUS:
7139 c_parser_consume_token (parser);
7140 return false;
7141 default:
7142 gcc_unreachable ();
7146 /* Parse an objc-method-definition.
7148 objc-method-definition:
7149 objc-method-type objc-method-decl ;[opt] compound-statement
7152 static void
7153 c_parser_objc_method_definition (c_parser *parser)
7155 bool is_class_method = c_parser_objc_method_type (parser);
7156 tree decl, attributes = NULL_TREE;
7157 parser->objc_pq_context = true;
7158 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7159 if (decl == error_mark_node)
7160 return; /* Bail here. */
7162 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7164 c_parser_consume_token (parser);
7165 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7166 "extra semicolon in method definition specified");
7169 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7171 c_parser_error (parser, "expected %<{%>");
7172 return;
7175 parser->objc_pq_context = false;
7176 if (objc_start_method_definition (is_class_method, decl, attributes))
7178 add_stmt (c_parser_compound_statement (parser));
7179 objc_finish_method_definition (current_function_decl);
7181 else
7183 /* This code is executed when we find a method definition
7184 outside of an @implementation context (or invalid for other
7185 reasons). Parse the method (to keep going) but do not emit
7186 any code.
7188 c_parser_compound_statement (parser);
7192 /* Parse an objc-methodprotolist.
7194 objc-methodprotolist:
7195 empty
7196 objc-methodprotolist objc-methodproto
7197 objc-methodprotolist declaration
7198 objc-methodprotolist ;
7199 @optional
7200 @required
7202 The declaration is a data definition, which may be missing
7203 declaration specifiers under the same rules and diagnostics as
7204 other data definitions outside functions, and the stray semicolon
7205 is diagnosed the same way as a stray semicolon outside a
7206 function. */
7208 static void
7209 c_parser_objc_methodprotolist (c_parser *parser)
7211 while (true)
7213 /* The list is terminated by @end. */
7214 switch (c_parser_peek_token (parser)->type)
7216 case CPP_SEMICOLON:
7217 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7218 "ISO C does not allow extra %<;%> outside of a function");
7219 c_parser_consume_token (parser);
7220 break;
7221 case CPP_PLUS:
7222 case CPP_MINUS:
7223 c_parser_objc_methodproto (parser);
7224 break;
7225 case CPP_PRAGMA:
7226 c_parser_pragma (parser, pragma_external);
7227 break;
7228 case CPP_EOF:
7229 return;
7230 default:
7231 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7232 return;
7233 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7234 c_parser_objc_at_property_declaration (parser);
7235 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7237 objc_set_method_opt (true);
7238 c_parser_consume_token (parser);
7240 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7242 objc_set_method_opt (false);
7243 c_parser_consume_token (parser);
7245 else
7246 c_parser_declaration_or_fndef (parser, false, false, true,
7247 false, true, NULL);
7248 break;
7253 /* Parse an objc-methodproto.
7255 objc-methodproto:
7256 objc-method-type objc-method-decl ;
7259 static void
7260 c_parser_objc_methodproto (c_parser *parser)
7262 bool is_class_method = c_parser_objc_method_type (parser);
7263 tree decl, attributes = NULL_TREE;
7265 /* Remember protocol qualifiers in prototypes. */
7266 parser->objc_pq_context = true;
7267 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes);
7268 /* Forget protocol qualifiers now. */
7269 parser->objc_pq_context = false;
7271 /* Do not allow the presence of attributes to hide an erroneous
7272 method implementation in the interface section. */
7273 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7275 c_parser_error (parser, "expected %<;%>");
7276 return;
7279 if (decl != error_mark_node)
7280 objc_add_method_declaration (is_class_method, decl, attributes);
7282 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7285 /* If we are at a position that method attributes may be present, check that
7286 there are not any parsed already (a syntax error) and then collect any
7287 specified at the current location. Finally, if new attributes were present,
7288 check that the next token is legal ( ';' for decls and '{' for defs). */
7290 static bool
7291 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7293 bool bad = false;
7294 if (*attributes)
7296 c_parser_error (parser,
7297 "method attributes must be specified at the end only");
7298 *attributes = NULL_TREE;
7299 bad = true;
7302 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7303 *attributes = c_parser_attributes (parser);
7305 /* If there were no attributes here, just report any earlier error. */
7306 if (*attributes == NULL_TREE || bad)
7307 return bad;
7309 /* If the attributes are followed by a ; or {, then just report any earlier
7310 error. */
7311 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7312 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7313 return bad;
7315 /* We've got attributes, but not at the end. */
7316 c_parser_error (parser,
7317 "expected %<;%> or %<{%> after method attribute definition");
7318 return true;
7321 /* Parse an objc-method-decl.
7323 objc-method-decl:
7324 ( objc-type-name ) objc-selector
7325 objc-selector
7326 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7327 objc-keyword-selector objc-optparmlist
7328 attributes
7330 objc-keyword-selector:
7331 objc-keyword-decl
7332 objc-keyword-selector objc-keyword-decl
7334 objc-keyword-decl:
7335 objc-selector : ( objc-type-name ) identifier
7336 objc-selector : identifier
7337 : ( objc-type-name ) identifier
7338 : identifier
7340 objc-optparmlist:
7341 objc-optparms objc-optellipsis
7343 objc-optparms:
7344 empty
7345 objc-opt-parms , parameter-declaration
7347 objc-optellipsis:
7348 empty
7349 , ...
7352 static tree
7353 c_parser_objc_method_decl (c_parser *parser, bool is_class_method, tree *attributes)
7355 tree type = NULL_TREE;
7356 tree sel;
7357 tree parms = NULL_TREE;
7358 bool ellipsis = false;
7359 bool attr_err = false;
7361 *attributes = NULL_TREE;
7362 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7364 c_parser_consume_token (parser);
7365 type = c_parser_objc_type_name (parser);
7366 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7368 sel = c_parser_objc_selector (parser);
7369 /* If there is no selector, or a colon follows, we have an
7370 objc-keyword-selector. If there is a selector, and a colon does
7371 not follow, that selector ends the objc-method-decl. */
7372 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7374 tree tsel = sel;
7375 tree list = NULL_TREE;
7376 while (true)
7378 tree atype = NULL_TREE, id, keyworddecl;
7379 tree param_attr = NULL_TREE;
7380 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7381 break;
7382 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7384 c_parser_consume_token (parser);
7385 atype = c_parser_objc_type_name (parser);
7386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7387 "expected %<)%>");
7389 /* New ObjC allows attributes on method parameters. */
7390 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7391 param_attr = c_parser_attributes (parser);
7392 if (c_parser_next_token_is_not (parser, CPP_NAME))
7394 c_parser_error (parser, "expected identifier");
7395 return error_mark_node;
7397 id = c_parser_peek_token (parser)->value;
7398 c_parser_consume_token (parser);
7399 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7400 list = chainon (list, keyworddecl);
7401 tsel = c_parser_objc_selector (parser);
7402 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7403 break;
7406 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7408 /* Parse the optional parameter list. Optional Objective-C
7409 method parameters follow the C syntax, and may include '...'
7410 to denote a variable number of arguments. */
7411 parms = make_node (TREE_LIST);
7412 while (c_parser_next_token_is (parser, CPP_COMMA))
7414 struct c_parm *parm;
7415 c_parser_consume_token (parser);
7416 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7418 ellipsis = true;
7419 c_parser_consume_token (parser);
7420 attr_err |= c_parser_objc_maybe_method_attributes
7421 (parser, attributes) ;
7422 break;
7424 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7425 if (parm == NULL)
7426 break;
7427 parms = chainon (parms,
7428 build_tree_list (NULL_TREE, grokparm (parm)));
7430 sel = list;
7432 else
7433 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7435 if (sel == NULL)
7437 c_parser_error (parser, "objective-c method declaration is expected");
7438 return error_mark_node;
7441 if (attr_err)
7442 return error_mark_node;
7444 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7447 /* Parse an objc-type-name.
7449 objc-type-name:
7450 objc-type-qualifiers[opt] type-name
7451 objc-type-qualifiers[opt]
7453 objc-type-qualifiers:
7454 objc-type-qualifier
7455 objc-type-qualifiers objc-type-qualifier
7457 objc-type-qualifier: one of
7458 in out inout bycopy byref oneway
7461 static tree
7462 c_parser_objc_type_name (c_parser *parser)
7464 tree quals = NULL_TREE;
7465 struct c_type_name *type_name = NULL;
7466 tree type = NULL_TREE;
7467 while (true)
7469 c_token *token = c_parser_peek_token (parser);
7470 if (token->type == CPP_KEYWORD
7471 && (token->keyword == RID_IN
7472 || token->keyword == RID_OUT
7473 || token->keyword == RID_INOUT
7474 || token->keyword == RID_BYCOPY
7475 || token->keyword == RID_BYREF
7476 || token->keyword == RID_ONEWAY))
7478 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7479 c_parser_consume_token (parser);
7481 else
7482 break;
7484 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7485 type_name = c_parser_type_name (parser);
7486 if (type_name)
7487 type = groktypename (type_name, NULL, NULL);
7489 /* If the type is unknown, and error has already been produced and
7490 we need to recover from the error. In that case, use NULL_TREE
7491 for the type, as if no type had been specified; this will use the
7492 default type ('id') which is good for error recovery. */
7493 if (type == error_mark_node)
7494 type = NULL_TREE;
7496 return build_tree_list (quals, type);
7499 /* Parse objc-protocol-refs.
7501 objc-protocol-refs:
7502 < identifier-list >
7505 static tree
7506 c_parser_objc_protocol_refs (c_parser *parser)
7508 tree list = NULL_TREE;
7509 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7510 c_parser_consume_token (parser);
7511 /* Any identifiers, including those declared as type names, are OK
7512 here. */
7513 while (true)
7515 tree id;
7516 if (c_parser_next_token_is_not (parser, CPP_NAME))
7518 c_parser_error (parser, "expected identifier");
7519 break;
7521 id = c_parser_peek_token (parser)->value;
7522 list = chainon (list, build_tree_list (NULL_TREE, id));
7523 c_parser_consume_token (parser);
7524 if (c_parser_next_token_is (parser, CPP_COMMA))
7525 c_parser_consume_token (parser);
7526 else
7527 break;
7529 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7530 return list;
7533 /* Parse an objc-try-catch-finally-statement.
7535 objc-try-catch-finally-statement:
7536 @try compound-statement objc-catch-list[opt]
7537 @try compound-statement objc-catch-list[opt] @finally compound-statement
7539 objc-catch-list:
7540 @catch ( objc-catch-parameter-declaration ) compound-statement
7541 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7543 objc-catch-parameter-declaration:
7544 parameter-declaration
7545 '...'
7547 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7549 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7550 for C++. Keep them in sync. */
7552 static void
7553 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7555 location_t location;
7556 tree stmt;
7558 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7559 c_parser_consume_token (parser);
7560 location = c_parser_peek_token (parser)->location;
7561 objc_maybe_warn_exceptions (location);
7562 stmt = c_parser_compound_statement (parser);
7563 objc_begin_try_stmt (location, stmt);
7565 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7567 struct c_parm *parm;
7568 tree parameter_declaration = error_mark_node;
7569 bool seen_open_paren = false;
7571 c_parser_consume_token (parser);
7572 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7573 seen_open_paren = true;
7574 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7576 /* We have "@catch (...)" (where the '...' are literally
7577 what is in the code). Skip the '...'.
7578 parameter_declaration is set to NULL_TREE, and
7579 objc_being_catch_clauses() knows that that means
7580 '...'. */
7581 c_parser_consume_token (parser);
7582 parameter_declaration = NULL_TREE;
7584 else
7586 /* We have "@catch (NSException *exception)" or something
7587 like that. Parse the parameter declaration. */
7588 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7589 if (parm == NULL)
7590 parameter_declaration = error_mark_node;
7591 else
7592 parameter_declaration = grokparm (parm);
7594 if (seen_open_paren)
7595 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7596 else
7598 /* If there was no open parenthesis, we are recovering from
7599 an error, and we are trying to figure out what mistake
7600 the user has made. */
7602 /* If there is an immediate closing parenthesis, the user
7603 probably forgot the opening one (ie, they typed "@catch
7604 NSException *e)". Parse the closing parenthesis and keep
7605 going. */
7606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7607 c_parser_consume_token (parser);
7609 /* If these is no immediate closing parenthesis, the user
7610 probably doesn't know that parenthesis are required at
7611 all (ie, they typed "@catch NSException *e"). So, just
7612 forget about the closing parenthesis and keep going. */
7614 objc_begin_catch_clause (parameter_declaration);
7615 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7616 c_parser_compound_statement_nostart (parser);
7617 objc_finish_catch_clause ();
7619 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7621 c_parser_consume_token (parser);
7622 location = c_parser_peek_token (parser)->location;
7623 stmt = c_parser_compound_statement (parser);
7624 objc_build_finally_clause (location, stmt);
7626 objc_finish_try_stmt ();
7629 /* Parse an objc-synchronized-statement.
7631 objc-synchronized-statement:
7632 @synchronized ( expression ) compound-statement
7635 static void
7636 c_parser_objc_synchronized_statement (c_parser *parser)
7638 location_t loc;
7639 tree expr, stmt;
7640 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7641 c_parser_consume_token (parser);
7642 loc = c_parser_peek_token (parser)->location;
7643 objc_maybe_warn_exceptions (loc);
7644 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7646 expr = c_parser_expression (parser).value;
7647 expr = c_fully_fold (expr, false, NULL);
7648 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7650 else
7651 expr = error_mark_node;
7652 stmt = c_parser_compound_statement (parser);
7653 objc_build_synchronized (loc, expr, stmt);
7656 /* Parse an objc-selector; return NULL_TREE without an error if the
7657 next token is not an objc-selector.
7659 objc-selector:
7660 identifier
7661 one of
7662 enum struct union if else while do for switch case default
7663 break continue return goto asm sizeof typeof __alignof
7664 unsigned long const short volatile signed restrict _Complex
7665 in out inout bycopy byref oneway int char float double void _Bool
7667 ??? Why this selection of keywords but not, for example, storage
7668 class specifiers? */
7670 static tree
7671 c_parser_objc_selector (c_parser *parser)
7673 c_token *token = c_parser_peek_token (parser);
7674 tree value = token->value;
7675 if (token->type == CPP_NAME)
7677 c_parser_consume_token (parser);
7678 return value;
7680 if (token->type != CPP_KEYWORD)
7681 return NULL_TREE;
7682 switch (token->keyword)
7684 case RID_ENUM:
7685 case RID_STRUCT:
7686 case RID_UNION:
7687 case RID_IF:
7688 case RID_ELSE:
7689 case RID_WHILE:
7690 case RID_DO:
7691 case RID_FOR:
7692 case RID_SWITCH:
7693 case RID_CASE:
7694 case RID_DEFAULT:
7695 case RID_BREAK:
7696 case RID_CONTINUE:
7697 case RID_RETURN:
7698 case RID_GOTO:
7699 case RID_ASM:
7700 case RID_SIZEOF:
7701 case RID_TYPEOF:
7702 case RID_ALIGNOF:
7703 case RID_UNSIGNED:
7704 case RID_LONG:
7705 case RID_INT128:
7706 case RID_CONST:
7707 case RID_SHORT:
7708 case RID_VOLATILE:
7709 case RID_SIGNED:
7710 case RID_RESTRICT:
7711 case RID_COMPLEX:
7712 case RID_IN:
7713 case RID_OUT:
7714 case RID_INOUT:
7715 case RID_BYCOPY:
7716 case RID_BYREF:
7717 case RID_ONEWAY:
7718 case RID_INT:
7719 case RID_CHAR:
7720 case RID_FLOAT:
7721 case RID_DOUBLE:
7722 case RID_VOID:
7723 case RID_BOOL:
7724 c_parser_consume_token (parser);
7725 return value;
7726 default:
7727 return NULL_TREE;
7731 /* Parse an objc-selector-arg.
7733 objc-selector-arg:
7734 objc-selector
7735 objc-keywordname-list
7737 objc-keywordname-list:
7738 objc-keywordname
7739 objc-keywordname-list objc-keywordname
7741 objc-keywordname:
7742 objc-selector :
7746 static tree
7747 c_parser_objc_selector_arg (c_parser *parser)
7749 tree sel = c_parser_objc_selector (parser);
7750 tree list = NULL_TREE;
7751 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7752 return sel;
7753 while (true)
7755 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7756 return list;
7757 list = chainon (list, build_tree_list (sel, NULL_TREE));
7758 sel = c_parser_objc_selector (parser);
7759 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7760 break;
7762 return list;
7765 /* Parse an objc-receiver.
7767 objc-receiver:
7768 expression
7769 class-name
7770 type-name
7773 static tree
7774 c_parser_objc_receiver (c_parser *parser)
7776 if (c_parser_peek_token (parser)->type == CPP_NAME
7777 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7778 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7780 tree id = c_parser_peek_token (parser)->value;
7781 c_parser_consume_token (parser);
7782 return objc_get_class_reference (id);
7784 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7787 /* Parse objc-message-args.
7789 objc-message-args:
7790 objc-selector
7791 objc-keywordarg-list
7793 objc-keywordarg-list:
7794 objc-keywordarg
7795 objc-keywordarg-list objc-keywordarg
7797 objc-keywordarg:
7798 objc-selector : objc-keywordexpr
7799 : objc-keywordexpr
7802 static tree
7803 c_parser_objc_message_args (c_parser *parser)
7805 tree sel = c_parser_objc_selector (parser);
7806 tree list = NULL_TREE;
7807 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7808 return sel;
7809 while (true)
7811 tree keywordexpr;
7812 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7813 return error_mark_node;
7814 keywordexpr = c_parser_objc_keywordexpr (parser);
7815 list = chainon (list, build_tree_list (sel, keywordexpr));
7816 sel = c_parser_objc_selector (parser);
7817 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7818 break;
7820 return list;
7823 /* Parse an objc-keywordexpr.
7825 objc-keywordexpr:
7826 nonempty-expr-list
7829 static tree
7830 c_parser_objc_keywordexpr (c_parser *parser)
7832 tree ret;
7833 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7834 if (VEC_length (tree, expr_list) == 1)
7836 /* Just return the expression, remove a level of
7837 indirection. */
7838 ret = VEC_index (tree, expr_list, 0);
7840 else
7842 /* We have a comma expression, we will collapse later. */
7843 ret = build_tree_list_vec (expr_list);
7845 release_tree_vector (expr_list);
7846 return ret;
7849 /* A check, needed in several places, that ObjC interface, implementation or
7850 method definitions are not prefixed by incorrect items. */
7851 static bool
7852 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7853 struct c_declspecs *specs)
7855 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7856 || specs->typespec_kind != ctsk_none)
7858 c_parser_error (parser,
7859 "no type or storage class may be specified here,");
7860 c_parser_skip_to_end_of_block_or_statement (parser);
7861 return true;
7863 return false;
7866 /* Parse an Objective-C @property declaration. The syntax is:
7868 objc-property-declaration:
7869 '@property' objc-property-attributes[opt] struct-declaration ;
7871 objc-property-attributes:
7872 '(' objc-property-attribute-list ')'
7874 objc-property-attribute-list:
7875 objc-property-attribute
7876 objc-property-attribute-list, objc-property-attribute
7878 objc-property-attribute
7879 'getter' = identifier
7880 'setter' = identifier
7881 'readonly'
7882 'readwrite'
7883 'assign'
7884 'retain'
7885 'copy'
7886 'nonatomic'
7888 For example:
7889 @property NSString *name;
7890 @property (readonly) id object;
7891 @property (retain, nonatomic, getter=getTheName) id name;
7892 @property int a, b, c;
7894 PS: This function is identical to cp_parser_objc_at_propery_declaration
7895 for C++. Keep them in sync. */
7896 static void
7897 c_parser_objc_at_property_declaration (c_parser *parser)
7899 /* The following variables hold the attributes of the properties as
7900 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7901 seen. When we see an attribute, we set them to 'true' (if they
7902 are boolean properties) or to the identifier (if they have an
7903 argument, ie, for getter and setter). Note that here we only
7904 parse the list of attributes, check the syntax and accumulate the
7905 attributes that we find. objc_add_property_declaration() will
7906 then process the information. */
7907 bool property_assign = false;
7908 bool property_copy = false;
7909 tree property_getter_ident = NULL_TREE;
7910 bool property_nonatomic = false;
7911 bool property_readonly = false;
7912 bool property_readwrite = false;
7913 bool property_retain = false;
7914 tree property_setter_ident = NULL_TREE;
7916 /* 'properties' is the list of properties that we read. Usually a
7917 single one, but maybe more (eg, in "@property int a, b, c;" there
7918 are three). */
7919 tree properties;
7920 location_t loc;
7922 loc = c_parser_peek_token (parser)->location;
7923 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7925 c_parser_consume_token (parser); /* Eat '@property'. */
7927 /* Parse the optional attribute list... */
7928 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7930 /* Eat the '(' */
7931 c_parser_consume_token (parser);
7933 /* Property attribute keywords are valid now. */
7934 parser->objc_property_attr_context = true;
7936 while (true)
7938 bool syntax_error = false;
7939 c_token *token = c_parser_peek_token (parser);
7940 enum rid keyword;
7942 if (token->type != CPP_KEYWORD)
7944 if (token->type == CPP_CLOSE_PAREN)
7945 c_parser_error (parser, "expected identifier");
7946 else
7948 c_parser_consume_token (parser);
7949 c_parser_error (parser, "unknown property attribute");
7951 break;
7953 keyword = token->keyword;
7954 c_parser_consume_token (parser);
7955 switch (keyword)
7957 case RID_ASSIGN: property_assign = true; break;
7958 case RID_COPY: property_copy = true; break;
7959 case RID_NONATOMIC: property_nonatomic = true; break;
7960 case RID_READONLY: property_readonly = true; break;
7961 case RID_READWRITE: property_readwrite = true; break;
7962 case RID_RETAIN: property_retain = true; break;
7964 case RID_GETTER:
7965 case RID_SETTER:
7966 if (c_parser_next_token_is_not (parser, CPP_EQ))
7968 if (keyword == RID_GETTER)
7969 c_parser_error (parser,
7970 "missing %<=%> (after %<getter%> attribute)");
7971 else
7972 c_parser_error (parser,
7973 "missing %<=%> (after %<setter%> attribute)");
7974 syntax_error = true;
7975 break;
7977 c_parser_consume_token (parser); /* eat the = */
7978 if (c_parser_next_token_is_not (parser, CPP_NAME))
7980 c_parser_error (parser, "expected identifier");
7981 syntax_error = true;
7982 break;
7984 if (keyword == RID_SETTER)
7986 if (property_setter_ident != NULL_TREE)
7987 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
7988 else
7989 property_setter_ident = c_parser_peek_token (parser)->value;
7990 c_parser_consume_token (parser);
7991 if (c_parser_next_token_is_not (parser, CPP_COLON))
7992 c_parser_error (parser, "setter name must terminate with %<:%>");
7993 else
7994 c_parser_consume_token (parser);
7996 else
7998 if (property_getter_ident != NULL_TREE)
7999 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8000 else
8001 property_getter_ident = c_parser_peek_token (parser)->value;
8002 c_parser_consume_token (parser);
8004 break;
8005 default:
8006 c_parser_error (parser, "unknown property attribute");
8007 syntax_error = true;
8008 break;
8011 if (syntax_error)
8012 break;
8014 if (c_parser_next_token_is (parser, CPP_COMMA))
8015 c_parser_consume_token (parser);
8016 else
8017 break;
8019 parser->objc_property_attr_context = false;
8020 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8022 /* ... and the property declaration(s). */
8023 properties = c_parser_struct_declaration (parser);
8025 if (properties == error_mark_node)
8027 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8028 parser->error = false;
8029 return;
8032 if (properties == NULL_TREE)
8033 c_parser_error (parser, "expected identifier");
8034 else
8036 /* Comma-separated properties are chained together in
8037 reverse order; add them one by one. */
8038 properties = nreverse (properties);
8040 for (; properties; properties = TREE_CHAIN (properties))
8041 objc_add_property_declaration (loc, copy_node (properties),
8042 property_readonly, property_readwrite,
8043 property_assign, property_retain,
8044 property_copy, property_nonatomic,
8045 property_getter_ident, property_setter_ident);
8048 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8049 parser->error = false;
8052 /* Parse an Objective-C @synthesize declaration. The syntax is:
8054 objc-synthesize-declaration:
8055 @synthesize objc-synthesize-identifier-list ;
8057 objc-synthesize-identifier-list:
8058 objc-synthesize-identifier
8059 objc-synthesize-identifier-list, objc-synthesize-identifier
8061 objc-synthesize-identifier
8062 identifier
8063 identifier = identifier
8065 For example:
8066 @synthesize MyProperty;
8067 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8069 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8070 for C++. Keep them in sync.
8072 static void
8073 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8075 tree list = NULL_TREE;
8076 location_t loc;
8077 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8078 loc = c_parser_peek_token (parser)->location;
8080 c_parser_consume_token (parser);
8081 while (true)
8083 tree property, ivar;
8084 if (c_parser_next_token_is_not (parser, CPP_NAME))
8086 c_parser_error (parser, "expected identifier");
8087 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8088 /* Once we find the semicolon, we can resume normal parsing.
8089 We have to reset parser->error manually because
8090 c_parser_skip_until_found() won't reset it for us if the
8091 next token is precisely a semicolon. */
8092 parser->error = false;
8093 return;
8095 property = c_parser_peek_token (parser)->value;
8096 c_parser_consume_token (parser);
8097 if (c_parser_next_token_is (parser, CPP_EQ))
8099 c_parser_consume_token (parser);
8100 if (c_parser_next_token_is_not (parser, CPP_NAME))
8102 c_parser_error (parser, "expected identifier");
8103 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8104 parser->error = false;
8105 return;
8107 ivar = c_parser_peek_token (parser)->value;
8108 c_parser_consume_token (parser);
8110 else
8111 ivar = NULL_TREE;
8112 list = chainon (list, build_tree_list (ivar, property));
8113 if (c_parser_next_token_is (parser, CPP_COMMA))
8114 c_parser_consume_token (parser);
8115 else
8116 break;
8118 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8119 objc_add_synthesize_declaration (loc, list);
8122 /* Parse an Objective-C @dynamic declaration. The syntax is:
8124 objc-dynamic-declaration:
8125 @dynamic identifier-list ;
8127 For example:
8128 @dynamic MyProperty;
8129 @dynamic MyProperty, AnotherProperty;
8131 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8132 for C++. Keep them in sync.
8134 static void
8135 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8137 tree list = NULL_TREE;
8138 location_t loc;
8139 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8140 loc = c_parser_peek_token (parser)->location;
8142 c_parser_consume_token (parser);
8143 while (true)
8145 tree property;
8146 if (c_parser_next_token_is_not (parser, CPP_NAME))
8148 c_parser_error (parser, "expected identifier");
8149 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8150 parser->error = false;
8151 return;
8153 property = c_parser_peek_token (parser)->value;
8154 list = chainon (list, build_tree_list (NULL_TREE, property));
8155 c_parser_consume_token (parser);
8156 if (c_parser_next_token_is (parser, CPP_COMMA))
8157 c_parser_consume_token (parser);
8158 else
8159 break;
8161 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8162 objc_add_dynamic_declaration (loc, list);
8166 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8167 should be considered, statements. ALLOW_STMT is true if we're within
8168 the context of a function and such pragmas are to be allowed. Returns
8169 true if we actually parsed such a pragma. */
8171 static bool
8172 c_parser_pragma (c_parser *parser, enum pragma_context context)
8174 unsigned int id;
8176 id = c_parser_peek_token (parser)->pragma_kind;
8177 gcc_assert (id != PRAGMA_NONE);
8179 switch (id)
8181 case PRAGMA_OMP_BARRIER:
8182 if (context != pragma_compound)
8184 if (context == pragma_stmt)
8185 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8186 "used in compound statements");
8187 goto bad_stmt;
8189 c_parser_omp_barrier (parser);
8190 return false;
8192 case PRAGMA_OMP_FLUSH:
8193 if (context != pragma_compound)
8195 if (context == pragma_stmt)
8196 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8197 "used in compound statements");
8198 goto bad_stmt;
8200 c_parser_omp_flush (parser);
8201 return false;
8203 case PRAGMA_OMP_TASKWAIT:
8204 if (context != pragma_compound)
8206 if (context == pragma_stmt)
8207 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8208 "used in compound statements");
8209 goto bad_stmt;
8211 c_parser_omp_taskwait (parser);
8212 return false;
8214 case PRAGMA_OMP_THREADPRIVATE:
8215 c_parser_omp_threadprivate (parser);
8216 return false;
8218 case PRAGMA_OMP_SECTION:
8219 error_at (c_parser_peek_token (parser)->location,
8220 "%<#pragma omp section%> may only be used in "
8221 "%<#pragma omp sections%> construct");
8222 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8223 return false;
8225 case PRAGMA_GCC_PCH_PREPROCESS:
8226 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8227 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8228 return false;
8230 default:
8231 if (id < PRAGMA_FIRST_EXTERNAL)
8233 if (context == pragma_external)
8235 bad_stmt:
8236 c_parser_error (parser, "expected declaration specifiers");
8237 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8238 return false;
8240 c_parser_omp_construct (parser);
8241 return true;
8243 break;
8246 c_parser_consume_pragma (parser);
8247 c_invoke_pragma_handler (id);
8249 /* Skip to EOL, but suppress any error message. Those will have been
8250 generated by the handler routine through calling error, as opposed
8251 to calling c_parser_error. */
8252 parser->error = true;
8253 c_parser_skip_to_pragma_eol (parser);
8255 return false;
8258 /* The interface the pragma parsers have to the lexer. */
8260 enum cpp_ttype
8261 pragma_lex (tree *value)
8263 c_token *tok = c_parser_peek_token (the_parser);
8264 enum cpp_ttype ret = tok->type;
8266 *value = tok->value;
8267 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8268 ret = CPP_EOF;
8269 else
8271 if (ret == CPP_KEYWORD)
8272 ret = CPP_NAME;
8273 c_parser_consume_token (the_parser);
8276 return ret;
8279 static void
8280 c_parser_pragma_pch_preprocess (c_parser *parser)
8282 tree name = NULL;
8284 c_parser_consume_pragma (parser);
8285 if (c_parser_next_token_is (parser, CPP_STRING))
8287 name = c_parser_peek_token (parser)->value;
8288 c_parser_consume_token (parser);
8290 else
8291 c_parser_error (parser, "expected string literal");
8292 c_parser_skip_to_pragma_eol (parser);
8294 if (name)
8295 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8298 /* OpenMP 2.5 parsing routines. */
8300 /* Returns name of the next clause.
8301 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8302 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8303 returned and the token is consumed. */
8305 static pragma_omp_clause
8306 c_parser_omp_clause_name (c_parser *parser)
8308 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8310 if (c_parser_next_token_is_keyword (parser, RID_IF))
8311 result = PRAGMA_OMP_CLAUSE_IF;
8312 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8313 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8314 else if (c_parser_next_token_is (parser, CPP_NAME))
8316 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8318 switch (p[0])
8320 case 'c':
8321 if (!strcmp ("collapse", p))
8322 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8323 else if (!strcmp ("copyin", p))
8324 result = PRAGMA_OMP_CLAUSE_COPYIN;
8325 else if (!strcmp ("copyprivate", p))
8326 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8327 break;
8328 case 'f':
8329 if (!strcmp ("firstprivate", p))
8330 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8331 break;
8332 case 'l':
8333 if (!strcmp ("lastprivate", p))
8334 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8335 break;
8336 case 'n':
8337 if (!strcmp ("nowait", p))
8338 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8339 else if (!strcmp ("num_threads", p))
8340 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8341 break;
8342 case 'o':
8343 if (!strcmp ("ordered", p))
8344 result = PRAGMA_OMP_CLAUSE_ORDERED;
8345 break;
8346 case 'p':
8347 if (!strcmp ("private", p))
8348 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8349 break;
8350 case 'r':
8351 if (!strcmp ("reduction", p))
8352 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8353 break;
8354 case 's':
8355 if (!strcmp ("schedule", p))
8356 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8357 else if (!strcmp ("shared", p))
8358 result = PRAGMA_OMP_CLAUSE_SHARED;
8359 break;
8360 case 'u':
8361 if (!strcmp ("untied", p))
8362 result = PRAGMA_OMP_CLAUSE_UNTIED;
8363 break;
8367 if (result != PRAGMA_OMP_CLAUSE_NONE)
8368 c_parser_consume_token (parser);
8370 return result;
8373 /* Validate that a clause of the given type does not already exist. */
8375 static void
8376 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8377 const char *name)
8379 tree c;
8381 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8382 if (OMP_CLAUSE_CODE (c) == code)
8384 location_t loc = OMP_CLAUSE_LOCATION (c);
8385 error_at (loc, "too many %qs clauses", name);
8386 break;
8390 /* OpenMP 2.5:
8391 variable-list:
8392 identifier
8393 variable-list , identifier
8395 If KIND is nonzero, create the appropriate node and install the
8396 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8397 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8399 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8400 return the list created. */
8402 static tree
8403 c_parser_omp_variable_list (c_parser *parser,
8404 location_t clause_loc,
8405 enum omp_clause_code kind,
8406 tree list)
8408 if (c_parser_next_token_is_not (parser, CPP_NAME)
8409 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8410 c_parser_error (parser, "expected identifier");
8412 while (c_parser_next_token_is (parser, CPP_NAME)
8413 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8415 tree t = lookup_name (c_parser_peek_token (parser)->value);
8417 if (t == NULL_TREE)
8418 undeclared_variable (c_parser_peek_token (parser)->location,
8419 c_parser_peek_token (parser)->value);
8420 else if (t == error_mark_node)
8422 else if (kind != 0)
8424 tree u = build_omp_clause (clause_loc, kind);
8425 OMP_CLAUSE_DECL (u) = t;
8426 OMP_CLAUSE_CHAIN (u) = list;
8427 list = u;
8429 else
8430 list = tree_cons (t, NULL_TREE, list);
8432 c_parser_consume_token (parser);
8434 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8435 break;
8437 c_parser_consume_token (parser);
8440 return list;
8443 /* Similarly, but expect leading and trailing parenthesis. This is a very
8444 common case for omp clauses. */
8446 static tree
8447 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8448 tree list)
8450 /* The clauses location. */
8451 location_t loc = c_parser_peek_token (parser)->location;
8453 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8455 list = c_parser_omp_variable_list (parser, loc, kind, list);
8456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8458 return list;
8461 /* OpenMP 3.0:
8462 collapse ( constant-expression ) */
8464 static tree
8465 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8467 tree c, num = error_mark_node;
8468 HOST_WIDE_INT n;
8469 location_t loc;
8471 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8473 loc = c_parser_peek_token (parser)->location;
8474 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8476 num = c_parser_expr_no_commas (parser, NULL).value;
8477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8479 if (num == error_mark_node)
8480 return list;
8481 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8482 || !host_integerp (num, 0)
8483 || (n = tree_low_cst (num, 0)) <= 0
8484 || (int) n != n)
8486 error_at (loc,
8487 "collapse argument needs positive constant integer expression");
8488 return list;
8490 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8491 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8492 OMP_CLAUSE_CHAIN (c) = list;
8493 return c;
8496 /* OpenMP 2.5:
8497 copyin ( variable-list ) */
8499 static tree
8500 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8502 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8505 /* OpenMP 2.5:
8506 copyprivate ( variable-list ) */
8508 static tree
8509 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8511 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8514 /* OpenMP 2.5:
8515 default ( shared | none ) */
8517 static tree
8518 c_parser_omp_clause_default (c_parser *parser, tree list)
8520 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8521 location_t loc = c_parser_peek_token (parser)->location;
8522 tree c;
8524 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8525 return list;
8526 if (c_parser_next_token_is (parser, CPP_NAME))
8528 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8530 switch (p[0])
8532 case 'n':
8533 if (strcmp ("none", p) != 0)
8534 goto invalid_kind;
8535 kind = OMP_CLAUSE_DEFAULT_NONE;
8536 break;
8538 case 's':
8539 if (strcmp ("shared", p) != 0)
8540 goto invalid_kind;
8541 kind = OMP_CLAUSE_DEFAULT_SHARED;
8542 break;
8544 default:
8545 goto invalid_kind;
8548 c_parser_consume_token (parser);
8550 else
8552 invalid_kind:
8553 c_parser_error (parser, "expected %<none%> or %<shared%>");
8555 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8557 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8558 return list;
8560 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8561 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8562 OMP_CLAUSE_CHAIN (c) = list;
8563 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8565 return c;
8568 /* OpenMP 2.5:
8569 firstprivate ( variable-list ) */
8571 static tree
8572 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8574 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8577 /* OpenMP 2.5:
8578 if ( expression ) */
8580 static tree
8581 c_parser_omp_clause_if (c_parser *parser, tree list)
8583 location_t loc = c_parser_peek_token (parser)->location;
8584 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8586 tree t = c_parser_paren_condition (parser);
8587 tree c;
8589 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8591 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8592 OMP_CLAUSE_IF_EXPR (c) = t;
8593 OMP_CLAUSE_CHAIN (c) = list;
8594 list = c;
8596 else
8597 c_parser_error (parser, "expected %<(%>");
8599 return list;
8602 /* OpenMP 2.5:
8603 lastprivate ( variable-list ) */
8605 static tree
8606 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8608 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8611 /* OpenMP 2.5:
8612 nowait */
8614 static tree
8615 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8617 tree c;
8618 location_t loc = c_parser_peek_token (parser)->location;
8620 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8622 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8623 OMP_CLAUSE_CHAIN (c) = list;
8624 return c;
8627 /* OpenMP 2.5:
8628 num_threads ( expression ) */
8630 static tree
8631 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8633 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8634 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8636 location_t expr_loc = c_parser_peek_token (parser)->location;
8637 tree c, t = c_parser_expression (parser).value;
8638 t = c_fully_fold (t, false, NULL);
8640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8642 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8644 c_parser_error (parser, "expected integer expression");
8645 return list;
8648 /* Attempt to statically determine when the number isn't positive. */
8649 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8650 build_int_cst (TREE_TYPE (t), 0));
8651 if (CAN_HAVE_LOCATION_P (c))
8652 SET_EXPR_LOCATION (c, expr_loc);
8653 if (c == boolean_true_node)
8655 warning_at (expr_loc, 0,
8656 "%<num_threads%> value must be positive");
8657 t = integer_one_node;
8660 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8662 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8663 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8664 OMP_CLAUSE_CHAIN (c) = list;
8665 list = c;
8668 return list;
8671 /* OpenMP 2.5:
8672 ordered */
8674 static tree
8675 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8677 tree c;
8679 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8681 c = build_omp_clause (c_parser_peek_token (parser)->location,
8682 OMP_CLAUSE_ORDERED);
8683 OMP_CLAUSE_CHAIN (c) = list;
8685 return c;
8688 /* OpenMP 2.5:
8689 private ( variable-list ) */
8691 static tree
8692 c_parser_omp_clause_private (c_parser *parser, tree list)
8694 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8697 /* OpenMP 2.5:
8698 reduction ( reduction-operator : variable-list )
8700 reduction-operator:
8701 One of: + * - & ^ | && || */
8703 static tree
8704 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8706 location_t clause_loc = c_parser_peek_token (parser)->location;
8707 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8709 enum tree_code code;
8711 switch (c_parser_peek_token (parser)->type)
8713 case CPP_PLUS:
8714 code = PLUS_EXPR;
8715 break;
8716 case CPP_MULT:
8717 code = MULT_EXPR;
8718 break;
8719 case CPP_MINUS:
8720 code = MINUS_EXPR;
8721 break;
8722 case CPP_AND:
8723 code = BIT_AND_EXPR;
8724 break;
8725 case CPP_XOR:
8726 code = BIT_XOR_EXPR;
8727 break;
8728 case CPP_OR:
8729 code = BIT_IOR_EXPR;
8730 break;
8731 case CPP_AND_AND:
8732 code = TRUTH_ANDIF_EXPR;
8733 break;
8734 case CPP_OR_OR:
8735 code = TRUTH_ORIF_EXPR;
8736 break;
8737 default:
8738 c_parser_error (parser,
8739 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8740 "%<^%>, %<|%>, %<&&%>, or %<||%>");
8741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8742 return list;
8744 c_parser_consume_token (parser);
8745 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8747 tree nl, c;
8749 nl = c_parser_omp_variable_list (parser, clause_loc,
8750 OMP_CLAUSE_REDUCTION, list);
8751 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8752 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8754 list = nl;
8756 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8758 return list;
8761 /* OpenMP 2.5:
8762 schedule ( schedule-kind )
8763 schedule ( schedule-kind , expression )
8765 schedule-kind:
8766 static | dynamic | guided | runtime | auto
8769 static tree
8770 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8772 tree c, t;
8773 location_t loc = c_parser_peek_token (parser)->location;
8775 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8776 return list;
8778 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8780 if (c_parser_next_token_is (parser, CPP_NAME))
8782 tree kind = c_parser_peek_token (parser)->value;
8783 const char *p = IDENTIFIER_POINTER (kind);
8785 switch (p[0])
8787 case 'd':
8788 if (strcmp ("dynamic", p) != 0)
8789 goto invalid_kind;
8790 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8791 break;
8793 case 'g':
8794 if (strcmp ("guided", p) != 0)
8795 goto invalid_kind;
8796 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8797 break;
8799 case 'r':
8800 if (strcmp ("runtime", p) != 0)
8801 goto invalid_kind;
8802 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8803 break;
8805 default:
8806 goto invalid_kind;
8809 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8810 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8811 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8812 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8813 else
8814 goto invalid_kind;
8816 c_parser_consume_token (parser);
8817 if (c_parser_next_token_is (parser, CPP_COMMA))
8819 location_t here;
8820 c_parser_consume_token (parser);
8822 here = c_parser_peek_token (parser)->location;
8823 t = c_parser_expr_no_commas (parser, NULL).value;
8824 t = c_fully_fold (t, false, NULL);
8826 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8827 error_at (here, "schedule %<runtime%> does not take "
8828 "a %<chunk_size%> parameter");
8829 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8830 error_at (here,
8831 "schedule %<auto%> does not take "
8832 "a %<chunk_size%> parameter");
8833 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8834 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8835 else
8836 c_parser_error (parser, "expected integer expression");
8838 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8840 else
8841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8842 "expected %<,%> or %<)%>");
8844 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8845 OMP_CLAUSE_CHAIN (c) = list;
8846 return c;
8848 invalid_kind:
8849 c_parser_error (parser, "invalid schedule kind");
8850 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8851 return list;
8854 /* OpenMP 2.5:
8855 shared ( variable-list ) */
8857 static tree
8858 c_parser_omp_clause_shared (c_parser *parser, tree list)
8860 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8863 /* OpenMP 3.0:
8864 untied */
8866 static tree
8867 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8869 tree c;
8871 /* FIXME: Should we allow duplicates? */
8872 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8874 c = build_omp_clause (c_parser_peek_token (parser)->location,
8875 OMP_CLAUSE_UNTIED);
8876 OMP_CLAUSE_CHAIN (c) = list;
8878 return c;
8881 /* Parse all OpenMP clauses. The set clauses allowed by the directive
8882 is a bitmask in MASK. Return the list of clauses found; the result
8883 of clause default goes in *pdefault. */
8885 static tree
8886 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
8887 const char *where)
8889 tree clauses = NULL;
8890 bool first = true;
8892 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
8894 location_t here;
8895 pragma_omp_clause c_kind;
8896 const char *c_name;
8897 tree prev = clauses;
8899 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
8900 c_parser_consume_token (parser);
8902 first = false;
8903 here = c_parser_peek_token (parser)->location;
8904 c_kind = c_parser_omp_clause_name (parser);
8906 switch (c_kind)
8908 case PRAGMA_OMP_CLAUSE_COLLAPSE:
8909 clauses = c_parser_omp_clause_collapse (parser, clauses);
8910 c_name = "collapse";
8911 break;
8912 case PRAGMA_OMP_CLAUSE_COPYIN:
8913 clauses = c_parser_omp_clause_copyin (parser, clauses);
8914 c_name = "copyin";
8915 break;
8916 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
8917 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
8918 c_name = "copyprivate";
8919 break;
8920 case PRAGMA_OMP_CLAUSE_DEFAULT:
8921 clauses = c_parser_omp_clause_default (parser, clauses);
8922 c_name = "default";
8923 break;
8924 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
8925 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
8926 c_name = "firstprivate";
8927 break;
8928 case PRAGMA_OMP_CLAUSE_IF:
8929 clauses = c_parser_omp_clause_if (parser, clauses);
8930 c_name = "if";
8931 break;
8932 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
8933 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
8934 c_name = "lastprivate";
8935 break;
8936 case PRAGMA_OMP_CLAUSE_NOWAIT:
8937 clauses = c_parser_omp_clause_nowait (parser, clauses);
8938 c_name = "nowait";
8939 break;
8940 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
8941 clauses = c_parser_omp_clause_num_threads (parser, clauses);
8942 c_name = "num_threads";
8943 break;
8944 case PRAGMA_OMP_CLAUSE_ORDERED:
8945 clauses = c_parser_omp_clause_ordered (parser, clauses);
8946 c_name = "ordered";
8947 break;
8948 case PRAGMA_OMP_CLAUSE_PRIVATE:
8949 clauses = c_parser_omp_clause_private (parser, clauses);
8950 c_name = "private";
8951 break;
8952 case PRAGMA_OMP_CLAUSE_REDUCTION:
8953 clauses = c_parser_omp_clause_reduction (parser, clauses);
8954 c_name = "reduction";
8955 break;
8956 case PRAGMA_OMP_CLAUSE_SCHEDULE:
8957 clauses = c_parser_omp_clause_schedule (parser, clauses);
8958 c_name = "schedule";
8959 break;
8960 case PRAGMA_OMP_CLAUSE_SHARED:
8961 clauses = c_parser_omp_clause_shared (parser, clauses);
8962 c_name = "shared";
8963 break;
8964 case PRAGMA_OMP_CLAUSE_UNTIED:
8965 clauses = c_parser_omp_clause_untied (parser, clauses);
8966 c_name = "untied";
8967 break;
8968 default:
8969 c_parser_error (parser, "expected %<#pragma omp%> clause");
8970 goto saw_error;
8973 if (((mask >> c_kind) & 1) == 0 && !parser->error)
8975 /* Remove the invalid clause(s) from the list to avoid
8976 confusing the rest of the compiler. */
8977 clauses = prev;
8978 error_at (here, "%qs is not valid for %qs", c_name, where);
8982 saw_error:
8983 c_parser_skip_to_pragma_eol (parser);
8985 return c_finish_omp_clauses (clauses);
8988 /* OpenMP 2.5:
8989 structured-block:
8990 statement
8992 In practice, we're also interested in adding the statement to an
8993 outer node. So it is convenient if we work around the fact that
8994 c_parser_statement calls add_stmt. */
8996 static tree
8997 c_parser_omp_structured_block (c_parser *parser)
8999 tree stmt = push_stmt_list ();
9000 c_parser_statement (parser);
9001 return pop_stmt_list (stmt);
9004 /* OpenMP 2.5:
9005 # pragma omp atomic new-line
9006 expression-stmt
9008 expression-stmt:
9009 x binop= expr | x++ | ++x | x-- | --x
9010 binop:
9011 +, *, -, /, &, ^, |, <<, >>
9013 where x is an lvalue expression with scalar type.
9015 LOC is the location of the #pragma token. */
9017 static void
9018 c_parser_omp_atomic (location_t loc, c_parser *parser)
9020 tree lhs, rhs;
9021 tree stmt;
9022 enum tree_code code;
9023 struct c_expr rhs_expr;
9025 c_parser_skip_to_pragma_eol (parser);
9027 lhs = c_parser_unary_expression (parser).value;
9028 lhs = c_fully_fold (lhs, false, NULL);
9029 switch (TREE_CODE (lhs))
9031 case ERROR_MARK:
9032 saw_error:
9033 c_parser_skip_to_end_of_block_or_statement (parser);
9034 return;
9036 case PREINCREMENT_EXPR:
9037 case POSTINCREMENT_EXPR:
9038 lhs = TREE_OPERAND (lhs, 0);
9039 code = PLUS_EXPR;
9040 rhs = integer_one_node;
9041 break;
9043 case PREDECREMENT_EXPR:
9044 case POSTDECREMENT_EXPR:
9045 lhs = TREE_OPERAND (lhs, 0);
9046 code = MINUS_EXPR;
9047 rhs = integer_one_node;
9048 break;
9050 case COMPOUND_EXPR:
9051 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9052 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9053 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9054 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9055 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9056 (TREE_OPERAND (lhs, 1), 0), 0)))
9057 == BOOLEAN_TYPE)
9058 /* Undo effects of boolean_increment for post {in,de}crement. */
9059 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9060 /* FALLTHRU */
9061 case MODIFY_EXPR:
9062 if (TREE_CODE (lhs) == MODIFY_EXPR
9063 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9065 /* Undo effects of boolean_increment. */
9066 if (integer_onep (TREE_OPERAND (lhs, 1)))
9068 /* This is pre or post increment. */
9069 rhs = TREE_OPERAND (lhs, 1);
9070 lhs = TREE_OPERAND (lhs, 0);
9071 code = NOP_EXPR;
9072 break;
9074 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9075 && TREE_OPERAND (lhs, 0)
9076 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9078 /* This is pre or post decrement. */
9079 rhs = TREE_OPERAND (lhs, 1);
9080 lhs = TREE_OPERAND (lhs, 0);
9081 code = NOP_EXPR;
9082 break;
9085 /* FALLTHRU */
9086 default:
9087 switch (c_parser_peek_token (parser)->type)
9089 case CPP_MULT_EQ:
9090 code = MULT_EXPR;
9091 break;
9092 case CPP_DIV_EQ:
9093 code = TRUNC_DIV_EXPR;
9094 break;
9095 case CPP_PLUS_EQ:
9096 code = PLUS_EXPR;
9097 break;
9098 case CPP_MINUS_EQ:
9099 code = MINUS_EXPR;
9100 break;
9101 case CPP_LSHIFT_EQ:
9102 code = LSHIFT_EXPR;
9103 break;
9104 case CPP_RSHIFT_EQ:
9105 code = RSHIFT_EXPR;
9106 break;
9107 case CPP_AND_EQ:
9108 code = BIT_AND_EXPR;
9109 break;
9110 case CPP_OR_EQ:
9111 code = BIT_IOR_EXPR;
9112 break;
9113 case CPP_XOR_EQ:
9114 code = BIT_XOR_EXPR;
9115 break;
9116 default:
9117 c_parser_error (parser,
9118 "invalid operator for %<#pragma omp atomic%>");
9119 goto saw_error;
9122 /* Arrange to pass the location of the assignment operator to
9123 c_finish_omp_atomic. */
9124 loc = c_parser_peek_token (parser)->location;
9125 c_parser_consume_token (parser);
9127 location_t rhs_loc = c_parser_peek_token (parser)->location;
9128 rhs_expr = c_parser_expression (parser);
9129 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9131 rhs = rhs_expr.value;
9132 rhs = c_fully_fold (rhs, false, NULL);
9133 break;
9135 stmt = c_finish_omp_atomic (loc, code, lhs, rhs);
9136 if (stmt != error_mark_node)
9137 add_stmt (stmt);
9138 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9142 /* OpenMP 2.5:
9143 # pragma omp barrier new-line
9146 static void
9147 c_parser_omp_barrier (c_parser *parser)
9149 location_t loc = c_parser_peek_token (parser)->location;
9150 c_parser_consume_pragma (parser);
9151 c_parser_skip_to_pragma_eol (parser);
9153 c_finish_omp_barrier (loc);
9156 /* OpenMP 2.5:
9157 # pragma omp critical [(name)] new-line
9158 structured-block
9160 LOC is the location of the #pragma itself. */
9162 static tree
9163 c_parser_omp_critical (location_t loc, c_parser *parser)
9165 tree stmt, name = NULL;
9167 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9169 c_parser_consume_token (parser);
9170 if (c_parser_next_token_is (parser, CPP_NAME))
9172 name = c_parser_peek_token (parser)->value;
9173 c_parser_consume_token (parser);
9174 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9176 else
9177 c_parser_error (parser, "expected identifier");
9179 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9180 c_parser_error (parser, "expected %<(%> or end of line");
9181 c_parser_skip_to_pragma_eol (parser);
9183 stmt = c_parser_omp_structured_block (parser);
9184 return c_finish_omp_critical (loc, stmt, name);
9187 /* OpenMP 2.5:
9188 # pragma omp flush flush-vars[opt] new-line
9190 flush-vars:
9191 ( variable-list ) */
9193 static void
9194 c_parser_omp_flush (c_parser *parser)
9196 location_t loc = c_parser_peek_token (parser)->location;
9197 c_parser_consume_pragma (parser);
9198 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9199 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9200 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9201 c_parser_error (parser, "expected %<(%> or end of line");
9202 c_parser_skip_to_pragma_eol (parser);
9204 c_finish_omp_flush (loc);
9207 /* Parse the restricted form of the for statement allowed by OpenMP.
9208 The real trick here is to determine the loop control variable early
9209 so that we can push a new decl if necessary to make it private.
9210 LOC is the location of the OMP in "#pragma omp". */
9212 static tree
9213 c_parser_omp_for_loop (location_t loc,
9214 c_parser *parser, tree clauses, tree *par_clauses)
9216 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9217 tree declv, condv, incrv, initv, ret = NULL;
9218 bool fail = false, open_brace_parsed = false;
9219 int i, collapse = 1, nbraces = 0;
9220 location_t for_loc;
9221 VEC(tree,gc) *for_block = make_tree_vector ();
9223 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9224 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9225 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9227 gcc_assert (collapse >= 1);
9229 declv = make_tree_vec (collapse);
9230 initv = make_tree_vec (collapse);
9231 condv = make_tree_vec (collapse);
9232 incrv = make_tree_vec (collapse);
9234 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9236 c_parser_error (parser, "for statement expected");
9237 return NULL;
9239 for_loc = c_parser_peek_token (parser)->location;
9240 c_parser_consume_token (parser);
9242 for (i = 0; i < collapse; i++)
9244 int bracecount = 0;
9246 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9247 goto pop_scopes;
9249 /* Parse the initialization declaration or expression. */
9250 if (c_parser_next_tokens_start_declaration (parser))
9252 if (i > 0)
9253 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9254 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9255 decl = check_for_loop_decls (for_loc, flag_isoc99);
9256 if (decl == NULL)
9257 goto error_init;
9258 if (DECL_INITIAL (decl) == error_mark_node)
9259 decl = error_mark_node;
9260 init = decl;
9262 else if (c_parser_next_token_is (parser, CPP_NAME)
9263 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9265 struct c_expr decl_exp;
9266 struct c_expr init_exp;
9267 location_t init_loc;
9269 decl_exp = c_parser_postfix_expression (parser);
9270 decl = decl_exp.value;
9272 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9274 init_loc = c_parser_peek_token (parser)->location;
9275 init_exp = c_parser_expr_no_commas (parser, NULL);
9276 init_exp = default_function_array_read_conversion (init_loc,
9277 init_exp);
9278 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9279 NOP_EXPR, init_loc, init_exp.value,
9280 init_exp.original_type);
9281 init = c_process_expr_stmt (init_loc, init);
9283 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9285 else
9287 error_init:
9288 c_parser_error (parser,
9289 "expected iteration declaration or initialization");
9290 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9291 "expected %<)%>");
9292 fail = true;
9293 goto parse_next;
9296 /* Parse the loop condition. */
9297 cond = NULL_TREE;
9298 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9300 location_t cond_loc = c_parser_peek_token (parser)->location;
9301 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL);
9303 cond = cond_expr.value;
9304 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9305 cond = c_fully_fold (cond, false, NULL);
9306 switch (cond_expr.original_code)
9308 case GT_EXPR:
9309 case GE_EXPR:
9310 case LT_EXPR:
9311 case LE_EXPR:
9312 break;
9313 default:
9314 /* Can't be cond = error_mark_node, because we want to preserve
9315 the location until c_finish_omp_for. */
9316 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9317 break;
9319 protected_set_expr_location (cond, cond_loc);
9321 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9323 /* Parse the increment expression. */
9324 incr = NULL_TREE;
9325 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9327 location_t incr_loc = c_parser_peek_token (parser)->location;
9329 incr = c_process_expr_stmt (incr_loc,
9330 c_parser_expression (parser).value);
9332 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9334 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9335 fail = true;
9336 else
9338 TREE_VEC_ELT (declv, i) = decl;
9339 TREE_VEC_ELT (initv, i) = init;
9340 TREE_VEC_ELT (condv, i) = cond;
9341 TREE_VEC_ELT (incrv, i) = incr;
9344 parse_next:
9345 if (i == collapse - 1)
9346 break;
9348 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9349 in between the collapsed for loops to be still considered perfectly
9350 nested. Hopefully the final version clarifies this.
9351 For now handle (multiple) {'s and empty statements. */
9354 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9356 c_parser_consume_token (parser);
9357 break;
9359 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9361 c_parser_consume_token (parser);
9362 bracecount++;
9364 else if (bracecount
9365 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9366 c_parser_consume_token (parser);
9367 else
9369 c_parser_error (parser, "not enough perfectly nested loops");
9370 if (bracecount)
9372 open_brace_parsed = true;
9373 bracecount--;
9375 fail = true;
9376 collapse = 0;
9377 break;
9380 while (1);
9382 nbraces += bracecount;
9385 save_break = c_break_label;
9386 c_break_label = size_one_node;
9387 save_cont = c_cont_label;
9388 c_cont_label = NULL_TREE;
9389 body = push_stmt_list ();
9391 if (open_brace_parsed)
9393 location_t here = c_parser_peek_token (parser)->location;
9394 stmt = c_begin_compound_stmt (true);
9395 c_parser_compound_statement_nostart (parser);
9396 add_stmt (c_end_compound_stmt (here, stmt, true));
9398 else
9399 add_stmt (c_parser_c99_block_statement (parser));
9400 if (c_cont_label)
9402 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9403 SET_EXPR_LOCATION (t, loc);
9404 add_stmt (t);
9407 body = pop_stmt_list (body);
9408 c_break_label = save_break;
9409 c_cont_label = save_cont;
9411 while (nbraces)
9413 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9415 c_parser_consume_token (parser);
9416 nbraces--;
9418 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9419 c_parser_consume_token (parser);
9420 else
9422 c_parser_error (parser, "collapsed loops not perfectly nested");
9423 while (nbraces)
9425 location_t here = c_parser_peek_token (parser)->location;
9426 stmt = c_begin_compound_stmt (true);
9427 add_stmt (body);
9428 c_parser_compound_statement_nostart (parser);
9429 body = c_end_compound_stmt (here, stmt, true);
9430 nbraces--;
9432 goto pop_scopes;
9436 /* Only bother calling c_finish_omp_for if we haven't already generated
9437 an error from the initialization parsing. */
9438 if (!fail)
9440 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9441 if (stmt)
9443 if (par_clauses != NULL)
9445 tree *c;
9446 for (c = par_clauses; *c ; )
9447 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9448 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9449 c = &OMP_CLAUSE_CHAIN (*c);
9450 else
9452 for (i = 0; i < collapse; i++)
9453 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9454 break;
9455 if (i == collapse)
9456 c = &OMP_CLAUSE_CHAIN (*c);
9457 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9459 error_at (loc,
9460 "iteration variable %qD should not be firstprivate",
9461 OMP_CLAUSE_DECL (*c));
9462 *c = OMP_CLAUSE_CHAIN (*c);
9464 else
9466 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9467 change it to shared (decl) in
9468 OMP_PARALLEL_CLAUSES. */
9469 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9470 OMP_CLAUSE_LASTPRIVATE);
9471 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9472 OMP_CLAUSE_CHAIN (l) = clauses;
9473 clauses = l;
9474 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9478 OMP_FOR_CLAUSES (stmt) = clauses;
9480 ret = stmt;
9482 pop_scopes:
9483 while (!VEC_empty (tree, for_block))
9485 /* FIXME diagnostics: LOC below should be the actual location of
9486 this particular for block. We need to build a list of
9487 locations to go along with FOR_BLOCK. */
9488 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9489 add_stmt (stmt);
9491 release_tree_vector (for_block);
9492 return ret;
9495 /* OpenMP 2.5:
9496 #pragma omp for for-clause[optseq] new-line
9497 for-loop
9499 LOC is the location of the #pragma token.
9502 #define OMP_FOR_CLAUSE_MASK \
9503 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9504 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9505 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9506 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9507 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9508 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9509 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9510 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9512 static tree
9513 c_parser_omp_for (location_t loc, c_parser *parser)
9515 tree block, clauses, ret;
9517 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9518 "#pragma omp for");
9520 block = c_begin_compound_stmt (true);
9521 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9522 block = c_end_compound_stmt (loc, block, true);
9523 add_stmt (block);
9525 return ret;
9528 /* OpenMP 2.5:
9529 # pragma omp master new-line
9530 structured-block
9532 LOC is the location of the #pragma token.
9535 static tree
9536 c_parser_omp_master (location_t loc, c_parser *parser)
9538 c_parser_skip_to_pragma_eol (parser);
9539 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9542 /* OpenMP 2.5:
9543 # pragma omp ordered new-line
9544 structured-block
9546 LOC is the location of the #pragma itself.
9549 static tree
9550 c_parser_omp_ordered (location_t loc, c_parser *parser)
9552 c_parser_skip_to_pragma_eol (parser);
9553 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9556 /* OpenMP 2.5:
9558 section-scope:
9559 { section-sequence }
9561 section-sequence:
9562 section-directive[opt] structured-block
9563 section-sequence section-directive structured-block
9565 SECTIONS_LOC is the location of the #pragma omp sections. */
9567 static tree
9568 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9570 tree stmt, substmt;
9571 bool error_suppress = false;
9572 location_t loc;
9574 loc = c_parser_peek_token (parser)->location;
9575 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9577 /* Avoid skipping until the end of the block. */
9578 parser->error = false;
9579 return NULL_TREE;
9582 stmt = push_stmt_list ();
9584 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9586 substmt = push_stmt_list ();
9588 while (1)
9590 c_parser_statement (parser);
9592 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9593 break;
9594 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9595 break;
9596 if (c_parser_next_token_is (parser, CPP_EOF))
9597 break;
9600 substmt = pop_stmt_list (substmt);
9601 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9602 SET_EXPR_LOCATION (substmt, loc);
9603 add_stmt (substmt);
9606 while (1)
9608 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9609 break;
9610 if (c_parser_next_token_is (parser, CPP_EOF))
9611 break;
9613 loc = c_parser_peek_token (parser)->location;
9614 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9616 c_parser_consume_pragma (parser);
9617 c_parser_skip_to_pragma_eol (parser);
9618 error_suppress = false;
9620 else if (!error_suppress)
9622 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9623 error_suppress = true;
9626 substmt = c_parser_omp_structured_block (parser);
9627 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9628 SET_EXPR_LOCATION (substmt, loc);
9629 add_stmt (substmt);
9631 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9632 "expected %<#pragma omp section%> or %<}%>");
9634 substmt = pop_stmt_list (stmt);
9636 stmt = make_node (OMP_SECTIONS);
9637 SET_EXPR_LOCATION (stmt, sections_loc);
9638 TREE_TYPE (stmt) = void_type_node;
9639 OMP_SECTIONS_BODY (stmt) = substmt;
9641 return add_stmt (stmt);
9644 /* OpenMP 2.5:
9645 # pragma omp sections sections-clause[optseq] newline
9646 sections-scope
9648 LOC is the location of the #pragma token.
9651 #define OMP_SECTIONS_CLAUSE_MASK \
9652 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9653 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9654 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9655 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9656 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9658 static tree
9659 c_parser_omp_sections (location_t loc, c_parser *parser)
9661 tree block, clauses, ret;
9663 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
9664 "#pragma omp sections");
9666 block = c_begin_compound_stmt (true);
9667 ret = c_parser_omp_sections_scope (loc, parser);
9668 if (ret)
9669 OMP_SECTIONS_CLAUSES (ret) = clauses;
9670 block = c_end_compound_stmt (loc, block, true);
9671 add_stmt (block);
9673 return ret;
9676 /* OpenMP 2.5:
9677 # pragma parallel parallel-clause new-line
9678 # pragma parallel for parallel-for-clause new-line
9679 # pragma parallel sections parallel-sections-clause new-line
9681 LOC is the location of the #pragma token.
9684 #define OMP_PARALLEL_CLAUSE_MASK \
9685 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9686 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9687 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9688 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9689 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
9690 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
9691 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9692 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
9694 static tree
9695 c_parser_omp_parallel (location_t loc, c_parser *parser)
9697 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
9698 const char *p_name = "#pragma omp parallel";
9699 tree stmt, clauses, par_clause, ws_clause, block;
9700 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
9702 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9704 c_parser_consume_token (parser);
9705 p_kind = PRAGMA_OMP_PARALLEL_FOR;
9706 p_name = "#pragma omp parallel for";
9707 mask |= OMP_FOR_CLAUSE_MASK;
9708 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9710 else if (c_parser_next_token_is (parser, CPP_NAME))
9712 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9713 if (strcmp (p, "sections") == 0)
9715 c_parser_consume_token (parser);
9716 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
9717 p_name = "#pragma omp parallel sections";
9718 mask |= OMP_SECTIONS_CLAUSE_MASK;
9719 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
9723 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
9725 switch (p_kind)
9727 case PRAGMA_OMP_PARALLEL:
9728 block = c_begin_omp_parallel ();
9729 c_parser_statement (parser);
9730 stmt = c_finish_omp_parallel (loc, clauses, block);
9731 break;
9733 case PRAGMA_OMP_PARALLEL_FOR:
9734 block = c_begin_omp_parallel ();
9735 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9736 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
9737 stmt = c_finish_omp_parallel (loc, par_clause, block);
9738 OMP_PARALLEL_COMBINED (stmt) = 1;
9739 break;
9741 case PRAGMA_OMP_PARALLEL_SECTIONS:
9742 block = c_begin_omp_parallel ();
9743 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
9744 stmt = c_parser_omp_sections_scope (loc, parser);
9745 if (stmt)
9746 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
9747 stmt = c_finish_omp_parallel (loc, par_clause, block);
9748 OMP_PARALLEL_COMBINED (stmt) = 1;
9749 break;
9751 default:
9752 gcc_unreachable ();
9755 return stmt;
9758 /* OpenMP 2.5:
9759 # pragma omp single single-clause[optseq] new-line
9760 structured-block
9762 LOC is the location of the #pragma.
9765 #define OMP_SINGLE_CLAUSE_MASK \
9766 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9767 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9768 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
9769 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9771 static tree
9772 c_parser_omp_single (location_t loc, c_parser *parser)
9774 tree stmt = make_node (OMP_SINGLE);
9775 SET_EXPR_LOCATION (stmt, loc);
9776 TREE_TYPE (stmt) = void_type_node;
9778 OMP_SINGLE_CLAUSES (stmt)
9779 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
9780 "#pragma omp single");
9781 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
9783 return add_stmt (stmt);
9786 /* OpenMP 3.0:
9787 # pragma omp task task-clause[optseq] new-line
9789 LOC is the location of the #pragma.
9792 #define OMP_TASK_CLAUSE_MASK \
9793 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
9794 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
9795 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
9796 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9797 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9798 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
9800 static tree
9801 c_parser_omp_task (location_t loc, c_parser *parser)
9803 tree clauses, block;
9805 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
9806 "#pragma omp task");
9808 block = c_begin_omp_task ();
9809 c_parser_statement (parser);
9810 return c_finish_omp_task (loc, clauses, block);
9813 /* OpenMP 3.0:
9814 # pragma omp taskwait new-line
9817 static void
9818 c_parser_omp_taskwait (c_parser *parser)
9820 location_t loc = c_parser_peek_token (parser)->location;
9821 c_parser_consume_pragma (parser);
9822 c_parser_skip_to_pragma_eol (parser);
9824 c_finish_omp_taskwait (loc);
9827 /* Main entry point to parsing most OpenMP pragmas. */
9829 static void
9830 c_parser_omp_construct (c_parser *parser)
9832 enum pragma_kind p_kind;
9833 location_t loc;
9834 tree stmt;
9836 loc = c_parser_peek_token (parser)->location;
9837 p_kind = c_parser_peek_token (parser)->pragma_kind;
9838 c_parser_consume_pragma (parser);
9840 switch (p_kind)
9842 case PRAGMA_OMP_ATOMIC:
9843 c_parser_omp_atomic (loc, parser);
9844 return;
9845 case PRAGMA_OMP_CRITICAL:
9846 stmt = c_parser_omp_critical (loc, parser);
9847 break;
9848 case PRAGMA_OMP_FOR:
9849 stmt = c_parser_omp_for (loc, parser);
9850 break;
9851 case PRAGMA_OMP_MASTER:
9852 stmt = c_parser_omp_master (loc, parser);
9853 break;
9854 case PRAGMA_OMP_ORDERED:
9855 stmt = c_parser_omp_ordered (loc, parser);
9856 break;
9857 case PRAGMA_OMP_PARALLEL:
9858 stmt = c_parser_omp_parallel (loc, parser);
9859 break;
9860 case PRAGMA_OMP_SECTIONS:
9861 stmt = c_parser_omp_sections (loc, parser);
9862 break;
9863 case PRAGMA_OMP_SINGLE:
9864 stmt = c_parser_omp_single (loc, parser);
9865 break;
9866 case PRAGMA_OMP_TASK:
9867 stmt = c_parser_omp_task (loc, parser);
9868 break;
9869 default:
9870 gcc_unreachable ();
9873 if (stmt)
9874 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
9878 /* OpenMP 2.5:
9879 # pragma omp threadprivate (variable-list) */
9881 static void
9882 c_parser_omp_threadprivate (c_parser *parser)
9884 tree vars, t;
9885 location_t loc;
9887 c_parser_consume_pragma (parser);
9888 loc = c_parser_peek_token (parser)->location;
9889 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9891 /* Mark every variable in VARS to be assigned thread local storage. */
9892 for (t = vars; t; t = TREE_CHAIN (t))
9894 tree v = TREE_PURPOSE (t);
9896 /* FIXME diagnostics: Ideally we should keep individual
9897 locations for all the variables in the var list to make the
9898 following errors more precise. Perhaps
9899 c_parser_omp_var_list_parens() should construct a list of
9900 locations to go along with the var list. */
9902 /* If V had already been marked threadprivate, it doesn't matter
9903 whether it had been used prior to this point. */
9904 if (TREE_CODE (v) != VAR_DECL)
9905 error_at (loc, "%qD is not a variable", v);
9906 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
9907 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
9908 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9909 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
9910 else if (TREE_TYPE (v) == error_mark_node)
9912 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
9913 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
9914 else
9916 if (! DECL_THREAD_LOCAL_P (v))
9918 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
9919 /* If rtl has been already set for this var, call
9920 make_decl_rtl once again, so that encode_section_info
9921 has a chance to look at the new decl flags. */
9922 if (DECL_RTL_SET_P (v))
9923 make_decl_rtl (v);
9925 C_DECL_THREADPRIVATE_P (v) = 1;
9929 c_parser_skip_to_pragma_eol (parser);
9933 /* Parse a single source file. */
9935 void
9936 c_parse_file (void)
9938 /* Use local storage to begin. If the first token is a pragma, parse it.
9939 If it is #pragma GCC pch_preprocess, then this will load a PCH file
9940 which will cause garbage collection. */
9941 c_parser tparser;
9943 memset (&tparser, 0, sizeof tparser);
9944 the_parser = &tparser;
9946 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
9947 c_parser_pragma_pch_preprocess (&tparser);
9949 the_parser = ggc_alloc_c_parser ();
9950 *the_parser = tparser;
9952 /* Initialize EH, if we've been told to do so. */
9953 if (flag_exceptions)
9954 using_eh_for_cleanups ();
9956 c_parser_translation_unit (the_parser);
9957 the_parser = NULL;
9960 #include "gt-c-parser.h"