Merge trunk version 200575 into gupc branch.
[official-gcc.git] / gcc / c / c-parser.c
blob2e4c92c32aaa3644541df861508a4fec2f20f689
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "tree.h"
43 #include "langhooks.h"
44 #include "input.h"
45 #include "cpplib.h"
46 #include "timevar.h"
47 #include "c-family/c-pragma.h"
48 #include "c-tree.h"
49 #include "flags.h"
50 #include "ggc.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
53 #include "c-family/c-upc.h"
54 #include "vec.h"
55 #include "target.h"
56 #include "cgraph.h"
57 #include "plugin.h"
59 #include "upc/upc-tree.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 if (!c_dialect_upc ())
90 mask |= D_UPC;
92 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
93 for (i = 0; i < num_c_common_reswords; i++)
95 /* If a keyword is disabled, do not enter it into the table
96 and so create a canonical spelling that isn't a keyword. */
97 if (c_common_reswords[i].disable & mask)
99 if (warn_cxx_compat
100 && (c_common_reswords[i].disable & D_CXXWARN))
102 id = get_identifier (c_common_reswords[i].word);
103 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
104 C_IS_RESERVED_WORD (id) = 1;
106 continue;
109 id = get_identifier (c_common_reswords[i].word);
110 C_SET_RID_CODE (id, c_common_reswords[i].rid);
111 C_IS_RESERVED_WORD (id) = 1;
112 ridpointers [(int) c_common_reswords[i].rid] = id;
116 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
117 and the C parser. Unlike the C++ lexer, the parser structure
118 stores the lexer information instead of using a separate structure.
119 Identifiers are separated into ordinary identifiers, type names,
120 keywords and some other Objective-C types of identifiers, and some
121 look-ahead is maintained.
123 ??? It might be a good idea to lex the whole file up front (as for
124 C++). It would then be possible to share more of the C and C++
125 lexer code, if desired. */
127 /* The following local token type is used. */
129 /* A keyword. */
130 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
132 /* More information about the type of a CPP_NAME token. */
133 typedef enum c_id_kind {
134 /* An ordinary identifier. */
135 C_ID_ID,
136 /* An identifier declared as a typedef name. */
137 C_ID_TYPENAME,
138 /* An identifier declared as an Objective-C class name. */
139 C_ID_CLASSNAME,
140 /* An address space identifier. */
141 C_ID_ADDRSPACE,
142 /* Not an identifier. */
143 C_ID_NONE
144 } c_id_kind;
146 /* A single C token after string literal concatenation and conversion
147 of preprocessing tokens to tokens. */
148 typedef struct GTY (()) c_token {
149 /* The kind of token. */
150 ENUM_BITFIELD (cpp_ttype) type : 8;
151 /* If this token is a CPP_NAME, this value indicates whether also
152 declared as some kind of type. Otherwise, it is C_ID_NONE. */
153 ENUM_BITFIELD (c_id_kind) id_kind : 8;
154 /* If this token is a keyword, this value indicates which keyword.
155 Otherwise, this value is RID_MAX. */
156 ENUM_BITFIELD (rid) keyword : 8;
157 /* If this token is a CPP_PRAGMA, this indicates the pragma that
158 was seen. Otherwise it is PRAGMA_NONE. */
159 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
160 /* The location at which this token was found. */
161 location_t location;
162 /* The value associated with this token, if any. */
163 tree value;
164 } c_token;
166 /* A parser structure recording information about the state and
167 context of parsing. Includes lexer information with up to two
168 tokens of look-ahead; more are not needed for C. */
169 typedef struct GTY(()) c_parser {
170 /* The look-ahead tokens. */
171 c_token tokens[2];
172 /* How many look-ahead tokens are available (0, 1 or 2). */
173 short tokens_avail;
174 /* True if a syntax error is being recovered from; false otherwise.
175 c_parser_error sets this flag. It should clear this flag when
176 enough tokens have been consumed to recover from the error. */
177 BOOL_BITFIELD error : 1;
178 /* True if we're processing a pragma, and shouldn't automatically
179 consume CPP_PRAGMA_EOL. */
180 BOOL_BITFIELD in_pragma : 1;
181 /* True if we're parsing the outermost block of an if statement. */
182 BOOL_BITFIELD in_if_block : 1;
183 /* True if we want to lex an untranslated string. */
184 BOOL_BITFIELD lex_untranslated_string : 1;
186 /* Objective-C specific parser/lexer information. */
188 /* True if we are in a context where the Objective-C "PQ" keywords
189 are considered keywords. */
190 BOOL_BITFIELD objc_pq_context : 1;
191 /* True if we are parsing a (potential) Objective-C foreach
192 statement. This is set to true after we parsed 'for (' and while
193 we wait for 'in' or ';' to decide if it's a standard C for loop or an
194 Objective-C foreach loop. */
195 BOOL_BITFIELD objc_could_be_foreach_context : 1;
196 /* The following flag is needed to contextualize Objective-C lexical
197 analysis. In some cases (e.g., 'int NSObject;'), it is
198 undesirable to bind an identifier to an Objective-C class, even
199 if a class with that name exists. */
200 BOOL_BITFIELD objc_need_raw_identifier : 1;
201 /* Nonzero if we're processing a __transaction statement. The value
202 is 1 | TM_STMT_ATTR_*. */
203 unsigned int in_transaction : 4;
204 /* True if we are in a context where the Objective-C "Property attribute"
205 keywords are valid. */
206 BOOL_BITFIELD objc_property_attr_context : 1;
207 } c_parser;
210 /* The actual parser and external interface. ??? Does this need to be
211 garbage-collected? */
213 static GTY (()) c_parser *the_parser;
215 /* Read in and lex a single token, storing it in *TOKEN. */
217 static void
218 c_lex_one_token (c_parser *parser, c_token *token)
220 timevar_push (TV_LEX);
222 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
223 (parser->lex_untranslated_string
224 ? C_LEX_STRING_NO_TRANSLATE : 0));
225 token->id_kind = C_ID_NONE;
226 token->keyword = RID_MAX;
227 token->pragma_kind = PRAGMA_NONE;
229 switch (token->type)
231 case CPP_NAME:
233 tree decl;
235 bool objc_force_identifier = parser->objc_need_raw_identifier;
236 if (c_dialect_objc ())
237 parser->objc_need_raw_identifier = false;
239 if (C_IS_RESERVED_WORD (token->value))
241 enum rid rid_code = C_RID_CODE (token->value);
243 if (rid_code == RID_CXX_COMPAT_WARN)
245 warning_at (token->location,
246 OPT_Wc___compat,
247 "identifier %qE conflicts with C++ keyword",
248 token->value);
250 else if (rid_code >= RID_FIRST_ADDR_SPACE
251 && rid_code <= RID_LAST_ADDR_SPACE)
253 token->id_kind = C_ID_ADDRSPACE;
254 token->keyword = rid_code;
255 break;
257 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
259 /* We found an Objective-C "pq" keyword (in, out,
260 inout, bycopy, byref, oneway). They need special
261 care because the interpretation depends on the
262 context. */
263 if (parser->objc_pq_context)
265 token->type = CPP_KEYWORD;
266 token->keyword = rid_code;
267 break;
269 else if (parser->objc_could_be_foreach_context
270 && rid_code == RID_IN)
272 /* We are in Objective-C, inside a (potential)
273 foreach context (which means after having
274 parsed 'for (', but before having parsed ';'),
275 and we found 'in'. We consider it the keyword
276 which terminates the declaration at the
277 beginning of a foreach-statement. Note that
278 this means you can't use 'in' for anything else
279 in that context; in particular, in Objective-C
280 you can't use 'in' as the name of the running
281 variable in a C for loop. We could potentially
282 try to add code here to disambiguate, but it
283 seems a reasonable limitation. */
284 token->type = CPP_KEYWORD;
285 token->keyword = rid_code;
286 break;
288 /* Else, "pq" keywords outside of the "pq" context are
289 not keywords, and we fall through to the code for
290 normal tokens. */
292 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
294 /* We found an Objective-C "property attribute"
295 keyword (getter, setter, readonly, etc). These are
296 only valid in the property context. */
297 if (parser->objc_property_attr_context)
299 token->type = CPP_KEYWORD;
300 token->keyword = rid_code;
301 break;
303 /* Else they are not special keywords.
306 else if (c_dialect_objc ()
307 && (OBJC_IS_AT_KEYWORD (rid_code)
308 || OBJC_IS_CXX_KEYWORD (rid_code)))
310 /* We found one of the Objective-C "@" keywords (defs,
311 selector, synchronized, etc) or one of the
312 Objective-C "cxx" keywords (class, private,
313 protected, public, try, catch, throw) without a
314 preceding '@' sign. Do nothing and fall through to
315 the code for normal tokens (in C++ we would still
316 consider the CXX ones keywords, but not in C). */
319 else
321 token->type = CPP_KEYWORD;
322 token->keyword = rid_code;
323 break;
327 decl = lookup_name (token->value);
328 if (decl)
330 if (TREE_CODE (decl) == TYPE_DECL)
332 token->id_kind = C_ID_TYPENAME;
333 break;
336 else if (c_dialect_objc ())
338 tree objc_interface_decl = objc_is_class_name (token->value);
339 /* Objective-C class names are in the same namespace as
340 variables and typedefs, and hence are shadowed by local
341 declarations. */
342 if (objc_interface_decl
343 && (!objc_force_identifier || global_bindings_p ()))
345 token->value = objc_interface_decl;
346 token->id_kind = C_ID_CLASSNAME;
347 break;
350 token->id_kind = C_ID_ID;
352 break;
353 case CPP_AT_NAME:
354 /* This only happens in Objective-C; it must be a keyword. */
355 token->type = CPP_KEYWORD;
356 switch (C_RID_CODE (token->value))
358 /* Replace 'class' with '@class', 'private' with '@private',
359 etc. This prevents confusion with the C++ keyword
360 'class', and makes the tokens consistent with other
361 Objective-C 'AT' keywords. For example '@class' is
362 reported as RID_AT_CLASS which is consistent with
363 '@synchronized', which is reported as
364 RID_AT_SYNCHRONIZED.
366 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
367 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
368 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
369 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
370 case RID_THROW: token->keyword = RID_AT_THROW; break;
371 case RID_TRY: token->keyword = RID_AT_TRY; break;
372 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
373 default: token->keyword = C_RID_CODE (token->value);
375 break;
376 case CPP_COLON:
377 case CPP_COMMA:
378 case CPP_CLOSE_PAREN:
379 case CPP_SEMICOLON:
380 /* These tokens may affect the interpretation of any identifiers
381 following, if doing Objective-C. */
382 if (c_dialect_objc ())
383 parser->objc_need_raw_identifier = false;
384 break;
385 case CPP_PRAGMA:
386 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
387 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
388 token->value = NULL;
389 break;
390 default:
391 break;
393 timevar_pop (TV_LEX);
396 /* Return a pointer to the next token from PARSER, reading it in if
397 necessary. */
399 static inline c_token *
400 c_parser_peek_token (c_parser *parser)
402 if (parser->tokens_avail == 0)
404 c_lex_one_token (parser, &parser->tokens[0]);
405 parser->tokens_avail = 1;
407 return &parser->tokens[0];
410 /* Return true if the next token from PARSER has the indicated
411 TYPE. */
413 static inline bool
414 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
416 return c_parser_peek_token (parser)->type == type;
419 /* Return true if the next token from PARSER does not have the
420 indicated TYPE. */
422 static inline bool
423 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
425 return !c_parser_next_token_is (parser, type);
428 /* Return true if the next token from PARSER is the indicated
429 KEYWORD. */
431 static inline bool
432 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
434 return c_parser_peek_token (parser)->keyword == keyword;
437 /* Return a pointer to the next-but-one token from PARSER, reading it
438 in if necessary. The next token is already read in. */
440 static c_token *
441 c_parser_peek_2nd_token (c_parser *parser)
443 if (parser->tokens_avail >= 2)
444 return &parser->tokens[1];
445 gcc_assert (parser->tokens_avail == 1);
446 gcc_assert (parser->tokens[0].type != CPP_EOF);
447 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
448 c_lex_one_token (parser, &parser->tokens[1]);
449 parser->tokens_avail = 2;
450 return &parser->tokens[1];
453 /* Return true if TOKEN can start a type name,
454 false otherwise. */
455 static bool
456 c_token_starts_typename (c_token *token)
458 switch (token->type)
460 case CPP_NAME:
461 switch (token->id_kind)
463 case C_ID_ID:
464 return false;
465 case C_ID_ADDRSPACE:
466 return true;
467 case C_ID_TYPENAME:
468 return true;
469 case C_ID_CLASSNAME:
470 gcc_assert (c_dialect_objc ());
471 return true;
472 default:
473 gcc_unreachable ();
475 case CPP_KEYWORD:
476 switch (token->keyword)
478 case RID_UNSIGNED:
479 case RID_LONG:
480 case RID_INT128:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 case RID_BOOL:
493 case RID_ENUM:
494 case RID_STRUCT:
495 case RID_UNION:
496 case RID_TYPEOF:
497 case RID_CONST:
498 case RID_VOLATILE:
499 case RID_RESTRICT:
500 case RID_ATTRIBUTE:
501 case RID_FRACT:
502 case RID_ACCUM:
503 case RID_SAT:
504 return true;
505 /* UPC qualifiers */
506 case RID_SHARED:
507 case RID_STRICT:
508 case RID_RELAXED:
509 return true;
510 default:
511 return false;
513 case CPP_LESS:
514 if (c_dialect_objc ())
515 return true;
516 return false;
517 default:
518 return false;
522 enum c_lookahead_kind {
523 /* Always treat unknown identifiers as typenames. */
524 cla_prefer_type,
526 /* Could be parsing a nonabstract declarator. Only treat an identifier
527 as a typename if followed by another identifier or a star. */
528 cla_nonabstract_decl,
530 /* Never treat identifiers as typenames. */
531 cla_prefer_id
534 /* Return true if the next token from PARSER can start a type name,
535 false otherwise. LA specifies how to do lookahead in order to
536 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
538 static inline bool
539 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
541 c_token *token = c_parser_peek_token (parser);
542 if (c_token_starts_typename (token))
543 return true;
545 /* Try a bit harder to detect an unknown typename. */
546 if (la != cla_prefer_id
547 && token->type == CPP_NAME
548 && token->id_kind == C_ID_ID
550 /* Do not try too hard when we could have "object in array". */
551 && !parser->objc_could_be_foreach_context
553 && (la == cla_prefer_type
554 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
555 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
557 /* Only unknown identifiers. */
558 && !lookup_name (token->value))
559 return true;
561 return false;
564 /* Return true if TOKEN is a type qualifier, false otherwise. */
565 static bool
566 c_token_is_qualifier (c_token *token)
568 switch (token->type)
570 case CPP_NAME:
571 switch (token->id_kind)
573 case C_ID_ADDRSPACE:
574 return true;
575 default:
576 return false;
578 case CPP_KEYWORD:
579 switch (token->keyword)
581 case RID_CONST:
582 case RID_VOLATILE:
583 case RID_RESTRICT:
584 case RID_ATTRIBUTE:
585 return true;
586 case RID_SHARED:
587 case RID_STRICT:
588 case RID_RELAXED:
589 /* UPC qualifiers */
590 return true;
591 default:
592 return false;
594 case CPP_LESS:
595 return false;
596 default:
597 gcc_unreachable ();
601 /* Return true if the next token from PARSER is a type qualifier,
602 false otherwise. */
603 static inline bool
604 c_parser_next_token_is_qualifier (c_parser *parser)
606 c_token *token = c_parser_peek_token (parser);
607 return c_token_is_qualifier (token);
610 /* Return true if TOKEN can start declaration specifiers, false
611 otherwise. */
612 static bool
613 c_token_starts_declspecs (c_token *token)
615 switch (token->type)
617 case CPP_NAME:
618 switch (token->id_kind)
620 case C_ID_ID:
621 return false;
622 case C_ID_ADDRSPACE:
623 return true;
624 case C_ID_TYPENAME:
625 return true;
626 case C_ID_CLASSNAME:
627 gcc_assert (c_dialect_objc ());
628 return true;
629 default:
630 gcc_unreachable ();
632 case CPP_KEYWORD:
633 switch (token->keyword)
635 case RID_STATIC:
636 case RID_EXTERN:
637 case RID_REGISTER:
638 case RID_TYPEDEF:
639 case RID_INLINE:
640 case RID_NORETURN:
641 case RID_AUTO:
642 case RID_THREAD:
643 case RID_UNSIGNED:
644 case RID_LONG:
645 case RID_INT128:
646 case RID_SHORT:
647 case RID_SIGNED:
648 case RID_COMPLEX:
649 case RID_INT:
650 case RID_CHAR:
651 case RID_FLOAT:
652 case RID_DOUBLE:
653 case RID_VOID:
654 case RID_DFLOAT32:
655 case RID_DFLOAT64:
656 case RID_DFLOAT128:
657 case RID_BOOL:
658 case RID_ENUM:
659 case RID_STRUCT:
660 case RID_UNION:
661 case RID_TYPEOF:
662 case RID_CONST:
663 case RID_VOLATILE:
664 case RID_RESTRICT:
665 case RID_ATTRIBUTE:
666 case RID_FRACT:
667 case RID_ACCUM:
668 case RID_SAT:
669 case RID_ALIGNAS:
670 return true;
671 /* UPC qualifiers */
672 case RID_SHARED:
673 case RID_STRICT:
674 case RID_RELAXED:
675 return true;
676 default:
677 return false;
679 case CPP_LESS:
680 if (c_dialect_objc ())
681 return true;
682 return false;
683 default:
684 return false;
689 /* Return true if TOKEN can start declaration specifiers or a static
690 assertion, false otherwise. */
691 static bool
692 c_token_starts_declaration (c_token *token)
694 if (c_token_starts_declspecs (token)
695 || token->keyword == RID_STATIC_ASSERT)
696 return true;
697 else
698 return false;
701 /* Return true if the next token from PARSER can start declaration
702 specifiers, false otherwise. */
703 static inline bool
704 c_parser_next_token_starts_declspecs (c_parser *parser)
706 c_token *token = c_parser_peek_token (parser);
708 /* In Objective-C, a classname normally starts a declspecs unless it
709 is immediately followed by a dot. In that case, it is the
710 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
711 setter/getter on the class. c_token_starts_declspecs() can't
712 differentiate between the two cases because it only checks the
713 current token, so we have a special check here. */
714 if (c_dialect_objc ()
715 && token->type == CPP_NAME
716 && token->id_kind == C_ID_CLASSNAME
717 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
718 return false;
720 return c_token_starts_declspecs (token);
723 /* Return true if the next tokens from PARSER can start declaration
724 specifiers or a static assertion, false otherwise. */
725 static inline bool
726 c_parser_next_tokens_start_declaration (c_parser *parser)
728 c_token *token = c_parser_peek_token (parser);
730 /* Same as above. */
731 if (c_dialect_objc ()
732 && token->type == CPP_NAME
733 && token->id_kind == C_ID_CLASSNAME
734 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
735 return false;
737 /* Labels do not start declarations. */
738 if (token->type == CPP_NAME
739 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
740 return false;
742 if (c_token_starts_declaration (token))
743 return true;
745 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
746 return true;
748 return false;
751 /* Consume the next token from PARSER. */
753 static void
754 c_parser_consume_token (c_parser *parser)
756 gcc_assert (parser->tokens_avail >= 1);
757 gcc_assert (parser->tokens[0].type != CPP_EOF);
758 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
759 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
760 if (parser->tokens_avail == 2)
761 parser->tokens[0] = parser->tokens[1];
762 parser->tokens_avail--;
765 /* Expect the current token to be a #pragma. Consume it and remember
766 that we've begun parsing a pragma. */
768 static void
769 c_parser_consume_pragma (c_parser *parser)
771 gcc_assert (!parser->in_pragma);
772 gcc_assert (parser->tokens_avail >= 1);
773 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
774 if (parser->tokens_avail == 2)
775 parser->tokens[0] = parser->tokens[1];
776 parser->tokens_avail--;
777 parser->in_pragma = true;
780 /* Update the globals input_location and in_system_header from
781 TOKEN. */
782 static inline void
783 c_parser_set_source_position_from_token (c_token *token)
785 if (token->type != CPP_EOF)
787 input_location = token->location;
791 /* Issue a diagnostic of the form
792 FILE:LINE: MESSAGE before TOKEN
793 where TOKEN is the next token in the input stream of PARSER.
794 MESSAGE (specified by the caller) is usually of the form "expected
795 OTHER-TOKEN".
797 Do not issue a diagnostic if still recovering from an error.
799 ??? This is taken from the C++ parser, but building up messages in
800 this way is not i18n-friendly and some other approach should be
801 used. */
803 static void
804 c_parser_error (c_parser *parser, const char *gmsgid)
806 c_token *token = c_parser_peek_token (parser);
807 if (parser->error)
808 return;
809 parser->error = true;
810 if (!gmsgid)
811 return;
812 /* This diagnostic makes more sense if it is tagged to the line of
813 the token we just peeked at. */
814 c_parser_set_source_position_from_token (token);
815 c_parse_error (gmsgid,
816 /* Because c_parse_error does not understand
817 CPP_KEYWORD, keywords are treated like
818 identifiers. */
819 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
820 /* ??? The C parser does not save the cpp flags of a
821 token, we need to pass 0 here and we will not get
822 the source spelling of some tokens but rather the
823 canonical spelling. */
824 token->value, /*flags=*/0);
827 /* If the next token is of the indicated TYPE, consume it. Otherwise,
828 issue the error MSGID. If MSGID is NULL then a message has already
829 been produced and no message will be produced this time. Returns
830 true if found, false otherwise. */
832 static bool
833 c_parser_require (c_parser *parser,
834 enum cpp_ttype type,
835 const char *msgid)
837 if (c_parser_next_token_is (parser, type))
839 c_parser_consume_token (parser);
840 return true;
842 else
844 c_parser_error (parser, msgid);
845 return false;
849 /* If the next token is the indicated keyword, consume it. Otherwise,
850 issue the error MSGID. Returns true if found, false otherwise. */
852 static bool
853 c_parser_require_keyword (c_parser *parser,
854 enum rid keyword,
855 const char *msgid)
857 if (c_parser_next_token_is_keyword (parser, keyword))
859 c_parser_consume_token (parser);
860 return true;
862 else
864 c_parser_error (parser, msgid);
865 return false;
869 /* Like c_parser_require, except that tokens will be skipped until the
870 desired token is found. An error message is still produced if the
871 next token is not as expected. If MSGID is NULL then a message has
872 already been produced and no message will be produced this
873 time. */
875 static void
876 c_parser_skip_until_found (c_parser *parser,
877 enum cpp_ttype type,
878 const char *msgid)
880 unsigned nesting_depth = 0;
882 if (c_parser_require (parser, type, msgid))
883 return;
885 /* Skip tokens until the desired token is found. */
886 while (true)
888 /* Peek at the next token. */
889 c_token *token = c_parser_peek_token (parser);
890 /* If we've reached the token we want, consume it and stop. */
891 if (token->type == type && !nesting_depth)
893 c_parser_consume_token (parser);
894 break;
897 /* If we've run out of tokens, stop. */
898 if (token->type == CPP_EOF)
899 return;
900 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
901 return;
902 if (token->type == CPP_OPEN_BRACE
903 || token->type == CPP_OPEN_PAREN
904 || token->type == CPP_OPEN_SQUARE)
905 ++nesting_depth;
906 else if (token->type == CPP_CLOSE_BRACE
907 || token->type == CPP_CLOSE_PAREN
908 || token->type == CPP_CLOSE_SQUARE)
910 if (nesting_depth-- == 0)
911 break;
913 /* Consume this token. */
914 c_parser_consume_token (parser);
916 parser->error = false;
919 /* Skip tokens until the end of a parameter is found, but do not
920 consume the comma, semicolon or closing delimiter. */
922 static void
923 c_parser_skip_to_end_of_parameter (c_parser *parser)
925 unsigned nesting_depth = 0;
927 while (true)
929 c_token *token = c_parser_peek_token (parser);
930 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
931 && !nesting_depth)
932 break;
933 /* If we've run out of tokens, stop. */
934 if (token->type == CPP_EOF)
935 return;
936 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
937 return;
938 if (token->type == CPP_OPEN_BRACE
939 || token->type == CPP_OPEN_PAREN
940 || token->type == CPP_OPEN_SQUARE)
941 ++nesting_depth;
942 else if (token->type == CPP_CLOSE_BRACE
943 || token->type == CPP_CLOSE_PAREN
944 || token->type == CPP_CLOSE_SQUARE)
946 if (nesting_depth-- == 0)
947 break;
949 /* Consume this token. */
950 c_parser_consume_token (parser);
952 parser->error = false;
955 /* Expect to be at the end of the pragma directive and consume an
956 end of line marker. */
958 static void
959 c_parser_skip_to_pragma_eol (c_parser *parser)
961 gcc_assert (parser->in_pragma);
962 parser->in_pragma = false;
964 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
965 while (true)
967 c_token *token = c_parser_peek_token (parser);
968 if (token->type == CPP_EOF)
969 break;
970 if (token->type == CPP_PRAGMA_EOL)
972 c_parser_consume_token (parser);
973 break;
975 c_parser_consume_token (parser);
978 parser->error = false;
981 /* Skip tokens until we have consumed an entire block, or until we
982 have consumed a non-nested ';'. */
984 static void
985 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
987 unsigned nesting_depth = 0;
988 bool save_error = parser->error;
990 while (true)
992 c_token *token;
994 /* Peek at the next token. */
995 token = c_parser_peek_token (parser);
997 switch (token->type)
999 case CPP_EOF:
1000 return;
1002 case CPP_PRAGMA_EOL:
1003 if (parser->in_pragma)
1004 return;
1005 break;
1007 case CPP_SEMICOLON:
1008 /* If the next token is a ';', we have reached the
1009 end of the statement. */
1010 if (!nesting_depth)
1012 /* Consume the ';'. */
1013 c_parser_consume_token (parser);
1014 goto finished;
1016 break;
1018 case CPP_CLOSE_BRACE:
1019 /* If the next token is a non-nested '}', then we have
1020 reached the end of the current block. */
1021 if (nesting_depth == 0 || --nesting_depth == 0)
1023 c_parser_consume_token (parser);
1024 goto finished;
1026 break;
1028 case CPP_OPEN_BRACE:
1029 /* If it the next token is a '{', then we are entering a new
1030 block. Consume the entire block. */
1031 ++nesting_depth;
1032 break;
1034 case CPP_PRAGMA:
1035 /* If we see a pragma, consume the whole thing at once. We
1036 have some safeguards against consuming pragmas willy-nilly.
1037 Normally, we'd expect to be here with parser->error set,
1038 which disables these safeguards. But it's possible to get
1039 here for secondary error recovery, after parser->error has
1040 been cleared. */
1041 c_parser_consume_pragma (parser);
1042 c_parser_skip_to_pragma_eol (parser);
1043 parser->error = save_error;
1044 continue;
1046 default:
1047 break;
1050 c_parser_consume_token (parser);
1053 finished:
1054 parser->error = false;
1057 /* CPP's options (initialized by c-opts.c). */
1058 extern cpp_options *cpp_opts;
1060 /* Save the warning flags which are controlled by __extension__. */
1062 static inline int
1063 disable_extension_diagnostics (void)
1065 int ret = (pedantic
1066 | (warn_pointer_arith << 1)
1067 | (warn_traditional << 2)
1068 | (flag_iso << 3)
1069 | (warn_long_long << 4)
1070 | (warn_cxx_compat << 5)
1071 | (warn_overlength_strings << 6));
1072 cpp_opts->cpp_pedantic = pedantic = 0;
1073 warn_pointer_arith = 0;
1074 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1075 flag_iso = 0;
1076 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1077 warn_cxx_compat = 0;
1078 warn_overlength_strings = 0;
1079 return ret;
1082 /* Restore the warning flags which are controlled by __extension__.
1083 FLAGS is the return value from disable_extension_diagnostics. */
1085 static inline void
1086 restore_extension_diagnostics (int flags)
1088 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1089 warn_pointer_arith = (flags >> 1) & 1;
1090 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1091 flag_iso = (flags >> 3) & 1;
1092 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1093 warn_cxx_compat = (flags >> 5) & 1;
1094 warn_overlength_strings = (flags >> 6) & 1;
1097 /* Possibly kinds of declarator to parse. */
1098 typedef enum c_dtr_syn {
1099 /* A normal declarator with an identifier. */
1100 C_DTR_NORMAL,
1101 /* An abstract declarator (maybe empty). */
1102 C_DTR_ABSTRACT,
1103 /* A parameter declarator: may be either, but after a type name does
1104 not redeclare a typedef name as an identifier if it can
1105 alternatively be interpreted as a typedef name; see DR#009,
1106 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1107 following DR#249. For example, given a typedef T, "int T" and
1108 "int *T" are valid parameter declarations redeclaring T, while
1109 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1110 abstract declarators rather than involving redundant parentheses;
1111 the same applies with attributes inside the parentheses before
1112 "T". */
1113 C_DTR_PARM
1114 } c_dtr_syn;
1116 /* The binary operation precedence levels, where 0 is a dummy lowest level
1117 used for the bottom of the stack. */
1118 enum c_parser_prec {
1119 PREC_NONE,
1120 PREC_LOGOR,
1121 PREC_LOGAND,
1122 PREC_BITOR,
1123 PREC_BITXOR,
1124 PREC_BITAND,
1125 PREC_EQ,
1126 PREC_REL,
1127 PREC_SHIFT,
1128 PREC_ADD,
1129 PREC_MULT,
1130 NUM_PRECS
1133 static void c_parser_external_declaration (c_parser *);
1134 static void c_parser_asm_definition (c_parser *);
1135 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1136 bool, bool, tree *);
1137 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1138 static void c_parser_static_assert_declaration (c_parser *);
1139 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1140 bool, enum c_lookahead_kind);
1141 static struct c_typespec c_parser_enum_specifier (c_parser *);
1142 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1143 static tree c_parser_struct_declaration (c_parser *);
1144 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1145 static tree c_parser_alignas_specifier (c_parser *);
1146 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1147 bool *);
1148 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1149 c_dtr_syn, bool *);
1150 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1151 bool,
1152 struct c_declarator *);
1153 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1154 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1155 tree);
1156 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1157 static tree c_parser_simple_asm_expr (c_parser *);
1158 static tree c_parser_attributes (c_parser *);
1159 static struct c_type_name *c_parser_type_name (c_parser *);
1160 static struct c_expr c_parser_initializer (c_parser *);
1161 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1162 static void c_parser_initelt (c_parser *, struct obstack *);
1163 static void c_parser_initval (c_parser *, struct c_expr *,
1164 struct obstack *);
1165 static tree c_parser_compound_statement (c_parser *);
1166 static void c_parser_compound_statement_nostart (c_parser *);
1167 static void c_parser_label (c_parser *);
1168 static void c_parser_statement (c_parser *);
1169 static void c_parser_statement_after_labels (c_parser *);
1170 static void c_parser_if_statement (c_parser *);
1171 static void c_parser_switch_statement (c_parser *);
1172 static void c_parser_while_statement (c_parser *);
1173 static void c_parser_do_statement (c_parser *);
1174 static void c_parser_for_statement (c_parser *);
1175 static tree c_parser_asm_statement (c_parser *);
1176 static tree c_parser_asm_operands (c_parser *);
1177 static tree c_parser_asm_goto_operands (c_parser *);
1178 static tree c_parser_asm_clobbers (c_parser *);
1179 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1180 static struct c_expr c_parser_conditional_expression (c_parser *,
1181 struct c_expr *);
1182 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1183 enum c_parser_prec);
1184 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1185 static struct c_expr c_parser_unary_expression (c_parser *);
1186 static struct c_expr c_parser_sizeof_expression (c_parser *);
1187 static struct c_expr c_parser_alignof_expression (c_parser *);
1188 static struct c_expr c_parser_postfix_expression (c_parser *);
1189 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1190 struct c_type_name *,
1191 location_t);
1192 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1193 location_t loc,
1194 struct c_expr);
1195 static tree c_parser_transaction (c_parser *, enum rid);
1196 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1197 static tree c_parser_transaction_cancel (c_parser *);
1198 static struct c_expr c_parser_expression (c_parser *);
1199 static struct c_expr c_parser_expression_conv (c_parser *);
1200 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1201 vec<tree, va_gc> **, location_t *,
1202 tree *);
1203 static void c_parser_omp_construct (c_parser *);
1204 static void c_parser_omp_threadprivate (c_parser *);
1205 static void c_parser_omp_barrier (c_parser *);
1206 static void c_parser_omp_flush (c_parser *);
1207 static void c_parser_omp_taskwait (c_parser *);
1208 static void c_parser_omp_taskyield (c_parser *);
1210 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1211 static bool c_parser_pragma (c_parser *, enum pragma_context);
1213 /* These Objective-C parser functions are only ever called when
1214 compiling Objective-C. */
1215 static void c_parser_objc_class_definition (c_parser *, tree);
1216 static void c_parser_objc_class_instance_variables (c_parser *);
1217 static void c_parser_objc_class_declaration (c_parser *);
1218 static void c_parser_objc_alias_declaration (c_parser *);
1219 static void c_parser_objc_protocol_definition (c_parser *, tree);
1220 static bool c_parser_objc_method_type (c_parser *);
1221 static void c_parser_objc_method_definition (c_parser *);
1222 static void c_parser_objc_methodprotolist (c_parser *);
1223 static void c_parser_objc_methodproto (c_parser *);
1224 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1225 static tree c_parser_objc_type_name (c_parser *);
1226 static tree c_parser_objc_protocol_refs (c_parser *);
1227 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1228 static void c_parser_objc_synchronized_statement (c_parser *);
1229 static tree c_parser_objc_selector (c_parser *);
1230 static tree c_parser_objc_selector_arg (c_parser *);
1231 static tree c_parser_objc_receiver (c_parser *);
1232 static tree c_parser_objc_message_args (c_parser *);
1233 static tree c_parser_objc_keywordexpr (c_parser *);
1234 static void c_parser_objc_at_property_declaration (c_parser *);
1235 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1236 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1237 static bool c_parser_objc_diagnose_bad_element_prefix
1238 (c_parser *, struct c_declspecs *);
1240 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1242 /* These UPC parser functions are only ever called when
1243 compiling UPC. */
1244 static void c_parser_upc_forall_statement (c_parser *);
1245 static void c_parser_upc_sync_statement (c_parser *, int);
1246 static void c_parser_upc_shared_qual (source_location,
1247 c_parser *,
1248 struct c_declspecs *);
1250 /* Parse a translation unit (C90 6.7, C99 6.9).
1252 translation-unit:
1253 external-declarations
1255 external-declarations:
1256 external-declaration
1257 external-declarations external-declaration
1259 GNU extensions:
1261 translation-unit:
1262 empty
1265 static void
1266 c_parser_translation_unit (c_parser *parser)
1268 if (c_parser_next_token_is (parser, CPP_EOF))
1270 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1271 "ISO C forbids an empty translation unit");
1273 else
1275 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1276 mark_valid_location_for_stdc_pragma (false);
1279 ggc_collect ();
1280 c_parser_external_declaration (parser);
1281 obstack_free (&parser_obstack, obstack_position);
1283 while (c_parser_next_token_is_not (parser, CPP_EOF));
1287 /* Parse an external declaration (C90 6.7, C99 6.9).
1289 external-declaration:
1290 function-definition
1291 declaration
1293 GNU extensions:
1295 external-declaration:
1296 asm-definition
1298 __extension__ external-declaration
1300 Objective-C:
1302 external-declaration:
1303 objc-class-definition
1304 objc-class-declaration
1305 objc-alias-declaration
1306 objc-protocol-definition
1307 objc-method-definition
1308 @end
1311 static void
1312 c_parser_external_declaration (c_parser *parser)
1314 int ext;
1315 switch (c_parser_peek_token (parser)->type)
1317 case CPP_KEYWORD:
1318 switch (c_parser_peek_token (parser)->keyword)
1320 case RID_EXTENSION:
1321 ext = disable_extension_diagnostics ();
1322 c_parser_consume_token (parser);
1323 c_parser_external_declaration (parser);
1324 restore_extension_diagnostics (ext);
1325 break;
1326 case RID_ASM:
1327 c_parser_asm_definition (parser);
1328 break;
1329 case RID_AT_INTERFACE:
1330 case RID_AT_IMPLEMENTATION:
1331 gcc_assert (c_dialect_objc ());
1332 c_parser_objc_class_definition (parser, NULL_TREE);
1333 break;
1334 case RID_AT_CLASS:
1335 gcc_assert (c_dialect_objc ());
1336 c_parser_objc_class_declaration (parser);
1337 break;
1338 case RID_AT_ALIAS:
1339 gcc_assert (c_dialect_objc ());
1340 c_parser_objc_alias_declaration (parser);
1341 break;
1342 case RID_AT_PROTOCOL:
1343 gcc_assert (c_dialect_objc ());
1344 c_parser_objc_protocol_definition (parser, NULL_TREE);
1345 break;
1346 case RID_AT_PROPERTY:
1347 gcc_assert (c_dialect_objc ());
1348 c_parser_objc_at_property_declaration (parser);
1349 break;
1350 case RID_AT_SYNTHESIZE:
1351 gcc_assert (c_dialect_objc ());
1352 c_parser_objc_at_synthesize_declaration (parser);
1353 break;
1354 case RID_AT_DYNAMIC:
1355 gcc_assert (c_dialect_objc ());
1356 c_parser_objc_at_dynamic_declaration (parser);
1357 break;
1358 case RID_AT_END:
1359 gcc_assert (c_dialect_objc ());
1360 c_parser_consume_token (parser);
1361 objc_finish_implementation ();
1362 break;
1363 default:
1364 goto decl_or_fndef;
1366 break;
1367 case CPP_SEMICOLON:
1368 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1369 "ISO C does not allow extra %<;%> outside of a function");
1370 c_parser_consume_token (parser);
1371 break;
1372 case CPP_PRAGMA:
1373 mark_valid_location_for_stdc_pragma (true);
1374 c_parser_pragma (parser, pragma_external);
1375 mark_valid_location_for_stdc_pragma (false);
1376 break;
1377 case CPP_PLUS:
1378 case CPP_MINUS:
1379 if (c_dialect_objc ())
1381 c_parser_objc_method_definition (parser);
1382 break;
1384 /* Else fall through, and yield a syntax error trying to parse
1385 as a declaration or function definition. */
1386 default:
1387 decl_or_fndef:
1388 /* A declaration or a function definition (or, in Objective-C,
1389 an @interface or @protocol with prefix attributes). We can
1390 only tell which after parsing the declaration specifiers, if
1391 any, and the first declarator. */
1392 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1393 break;
1397 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1398 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1399 accepted; otherwise (old-style parameter declarations) only other
1400 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1401 assertion is accepted; otherwise (old-style parameter declarations)
1402 it is not. If NESTED is true, we are inside a function or parsing
1403 old-style parameter declarations; any functions encountered are
1404 nested functions and declaration specifiers are required; otherwise
1405 we are at top level and functions are normal functions and
1406 declaration specifiers may be optional. If EMPTY_OK is true, empty
1407 declarations are OK (subject to all other constraints); otherwise
1408 (old-style parameter declarations) they are diagnosed. If
1409 START_ATTR_OK is true, the declaration specifiers may start with
1410 attributes; otherwise they may not.
1411 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1412 declaration when parsing an Objective-C foreach statement.
1414 declaration:
1415 declaration-specifiers init-declarator-list[opt] ;
1416 static_assert-declaration
1418 function-definition:
1419 declaration-specifiers[opt] declarator declaration-list[opt]
1420 compound-statement
1422 declaration-list:
1423 declaration
1424 declaration-list declaration
1426 init-declarator-list:
1427 init-declarator
1428 init-declarator-list , init-declarator
1430 init-declarator:
1431 declarator simple-asm-expr[opt] attributes[opt]
1432 declarator simple-asm-expr[opt] attributes[opt] = initializer
1434 GNU extensions:
1436 nested-function-definition:
1437 declaration-specifiers declarator declaration-list[opt]
1438 compound-statement
1440 Objective-C:
1441 attributes objc-class-definition
1442 attributes objc-category-definition
1443 attributes objc-protocol-definition
1445 The simple-asm-expr and attributes are GNU extensions.
1447 This function does not handle __extension__; that is handled in its
1448 callers. ??? Following the old parser, __extension__ may start
1449 external declarations, declarations in functions and declarations
1450 at the start of "for" loops, but not old-style parameter
1451 declarations.
1453 C99 requires declaration specifiers in a function definition; the
1454 absence is diagnosed through the diagnosis of implicit int. In GNU
1455 C we also allow but diagnose declarations without declaration
1456 specifiers, but only at top level (elsewhere they conflict with
1457 other syntax).
1459 In Objective-C, declarations of the looping variable in a foreach
1460 statement are exceptionally terminated by 'in' (for example, 'for
1461 (NSObject *object in array) { ... }').
1463 OpenMP:
1465 declaration:
1466 threadprivate-directive */
1468 static void
1469 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1470 bool static_assert_ok, bool empty_ok,
1471 bool nested, bool start_attr_ok,
1472 tree *objc_foreach_object_declaration)
1474 struct c_declspecs *specs;
1475 tree prefix_attrs;
1476 tree all_prefix_attrs;
1477 bool diagnosed_no_specs = false;
1478 location_t here = c_parser_peek_token (parser)->location;
1480 if (static_assert_ok
1481 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1483 c_parser_static_assert_declaration (parser);
1484 return;
1486 specs = build_null_declspecs ();
1488 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1489 if (c_parser_peek_token (parser)->type == CPP_NAME
1490 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1491 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1492 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1493 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1495 error_at (here, "unknown type name %qE",
1496 c_parser_peek_token (parser)->value);
1498 /* Parse declspecs normally to get a correct pointer type, but avoid
1499 a further "fails to be a type name" error. Refuse nested functions
1500 since it is not how the user likely wants us to recover. */
1501 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1502 c_parser_peek_token (parser)->keyword = RID_VOID;
1503 c_parser_peek_token (parser)->value = error_mark_node;
1504 fndef_ok = !nested;
1507 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1508 if (parser->error)
1510 c_parser_skip_to_end_of_block_or_statement (parser);
1511 return;
1513 if (nested && !specs->declspecs_seen_p)
1515 c_parser_error (parser, "expected declaration specifiers");
1516 c_parser_skip_to_end_of_block_or_statement (parser);
1517 return;
1519 finish_declspecs (specs);
1520 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1522 if (empty_ok)
1523 shadow_tag (specs);
1524 else
1526 shadow_tag_warned (specs, 1);
1527 pedwarn (here, 0, "empty declaration");
1529 c_parser_consume_token (parser);
1530 return;
1533 /* Provide better error recovery. Note that a type name here is usually
1534 better diagnosed as a redeclaration. */
1535 if (empty_ok
1536 && specs->typespec_kind == ctsk_tagdef
1537 && c_parser_next_token_starts_declspecs (parser)
1538 && !c_parser_next_token_is (parser, CPP_NAME))
1540 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1541 parser->error = false;
1542 shadow_tag_warned (specs, 1);
1543 return;
1545 else if (c_dialect_objc ())
1547 /* Prefix attributes are an error on method decls. */
1548 switch (c_parser_peek_token (parser)->type)
1550 case CPP_PLUS:
1551 case CPP_MINUS:
1552 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1553 return;
1554 if (specs->attrs)
1556 warning_at (c_parser_peek_token (parser)->location,
1557 OPT_Wattributes,
1558 "prefix attributes are ignored for methods");
1559 specs->attrs = NULL_TREE;
1561 if (fndef_ok)
1562 c_parser_objc_method_definition (parser);
1563 else
1564 c_parser_objc_methodproto (parser);
1565 return;
1566 break;
1567 default:
1568 break;
1570 /* This is where we parse 'attributes @interface ...',
1571 'attributes @implementation ...', 'attributes @protocol ...'
1572 (where attributes could be, for example, __attribute__
1573 ((deprecated)).
1575 switch (c_parser_peek_token (parser)->keyword)
1577 case RID_AT_INTERFACE:
1579 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1580 return;
1581 c_parser_objc_class_definition (parser, specs->attrs);
1582 return;
1584 break;
1585 case RID_AT_IMPLEMENTATION:
1587 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1588 return;
1589 if (specs->attrs)
1591 warning_at (c_parser_peek_token (parser)->location,
1592 OPT_Wattributes,
1593 "prefix attributes are ignored for implementations");
1594 specs->attrs = NULL_TREE;
1596 c_parser_objc_class_definition (parser, NULL_TREE);
1597 return;
1599 break;
1600 case RID_AT_PROTOCOL:
1602 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1603 return;
1604 c_parser_objc_protocol_definition (parser, specs->attrs);
1605 return;
1607 break;
1608 case RID_AT_ALIAS:
1609 case RID_AT_CLASS:
1610 case RID_AT_END:
1611 case RID_AT_PROPERTY:
1612 if (specs->attrs)
1614 c_parser_error (parser, "unexpected attribute");
1615 specs->attrs = NULL;
1617 break;
1618 default:
1619 break;
1623 pending_xref_error ();
1624 prefix_attrs = specs->attrs;
1625 all_prefix_attrs = prefix_attrs;
1626 specs->attrs = NULL_TREE;
1627 while (true)
1629 struct c_declarator *declarator;
1630 bool dummy = false;
1631 timevar_id_t tv;
1632 tree fnbody;
1633 /* Declaring either one or more declarators (in which case we
1634 should diagnose if there were no declaration specifiers) or a
1635 function definition (in which case the diagnostic for
1636 implicit int suffices). */
1637 declarator = c_parser_declarator (parser,
1638 specs->typespec_kind != ctsk_none,
1639 C_DTR_NORMAL, &dummy);
1640 if (declarator == NULL)
1642 c_parser_skip_to_end_of_block_or_statement (parser);
1643 return;
1645 if (c_parser_next_token_is (parser, CPP_EQ)
1646 || c_parser_next_token_is (parser, CPP_COMMA)
1647 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1648 || c_parser_next_token_is_keyword (parser, RID_ASM)
1649 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1650 || c_parser_next_token_is_keyword (parser, RID_IN))
1652 tree asm_name = NULL_TREE;
1653 tree postfix_attrs = NULL_TREE;
1654 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1656 diagnosed_no_specs = true;
1657 pedwarn (here, 0, "data definition has no type or storage class");
1659 /* Having seen a data definition, there cannot now be a
1660 function definition. */
1661 fndef_ok = false;
1662 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1663 asm_name = c_parser_simple_asm_expr (parser);
1664 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1665 postfix_attrs = c_parser_attributes (parser);
1666 if (c_parser_next_token_is (parser, CPP_EQ))
1668 tree d;
1669 struct c_expr init;
1670 location_t init_loc;
1671 c_parser_consume_token (parser);
1672 /* The declaration of the variable is in effect while
1673 its initializer is parsed. */
1674 d = start_decl (declarator, specs, true,
1675 chainon (postfix_attrs, all_prefix_attrs));
1676 if (!d)
1677 d = error_mark_node;
1678 start_init (d, asm_name, global_bindings_p ());
1679 init_loc = c_parser_peek_token (parser)->location;
1680 init = c_parser_initializer (parser);
1681 finish_init ();
1682 if (d != error_mark_node)
1684 maybe_warn_string_init (TREE_TYPE (d), init);
1685 finish_decl (d, init_loc, init.value,
1686 init.original_type, asm_name);
1689 else
1691 tree d = start_decl (declarator, specs, false,
1692 chainon (postfix_attrs,
1693 all_prefix_attrs));
1694 if (d)
1695 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1696 NULL_TREE, asm_name);
1698 if (c_parser_next_token_is_keyword (parser, RID_IN))
1700 if (d)
1701 *objc_foreach_object_declaration = d;
1702 else
1703 *objc_foreach_object_declaration = error_mark_node;
1706 if (c_parser_next_token_is (parser, CPP_COMMA))
1708 c_parser_consume_token (parser);
1709 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1710 all_prefix_attrs = chainon (c_parser_attributes (parser),
1711 prefix_attrs);
1712 else
1713 all_prefix_attrs = prefix_attrs;
1714 continue;
1716 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1718 c_parser_consume_token (parser);
1719 return;
1721 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1723 /* This can only happen in Objective-C: we found the
1724 'in' that terminates the declaration inside an
1725 Objective-C foreach statement. Do not consume the
1726 token, so that the caller can use it to determine
1727 that this indeed is a foreach context. */
1728 return;
1730 else
1732 c_parser_error (parser, "expected %<,%> or %<;%>");
1733 c_parser_skip_to_end_of_block_or_statement (parser);
1734 return;
1737 else if (!fndef_ok)
1739 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1740 "%<asm%> or %<__attribute__%>");
1741 c_parser_skip_to_end_of_block_or_statement (parser);
1742 return;
1744 /* Function definition (nested or otherwise). */
1745 if (nested)
1747 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1748 c_push_function_context ();
1750 if (!start_function (specs, declarator, all_prefix_attrs))
1752 /* This can appear in many cases looking nothing like a
1753 function definition, so we don't give a more specific
1754 error suggesting there was one. */
1755 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1756 "or %<__attribute__%>");
1757 if (nested)
1758 c_pop_function_context ();
1759 break;
1762 if (DECL_DECLARED_INLINE_P (current_function_decl))
1763 tv = TV_PARSE_INLINE;
1764 else
1765 tv = TV_PARSE_FUNC;
1766 timevar_push (tv);
1768 /* Parse old-style parameter declarations. ??? Attributes are
1769 not allowed to start declaration specifiers here because of a
1770 syntax conflict between a function declaration with attribute
1771 suffix and a function definition with an attribute prefix on
1772 first old-style parameter declaration. Following the old
1773 parser, they are not accepted on subsequent old-style
1774 parameter declarations either. However, there is no
1775 ambiguity after the first declaration, nor indeed on the
1776 first as long as we don't allow postfix attributes after a
1777 declarator with a nonempty identifier list in a definition;
1778 and postfix attributes have never been accepted here in
1779 function definitions either. */
1780 while (c_parser_next_token_is_not (parser, CPP_EOF)
1781 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1782 c_parser_declaration_or_fndef (parser, false, false, false,
1783 true, false, NULL);
1784 store_parm_decls ();
1785 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1786 = c_parser_peek_token (parser)->location;
1787 fnbody = c_parser_compound_statement (parser);
1788 if (flag_enable_cilkplus && contains_array_notation_expr (fnbody))
1789 fnbody = expand_array_notation_exprs (fnbody);
1790 if (nested)
1792 tree decl = current_function_decl;
1793 /* Mark nested functions as needing static-chain initially.
1794 lower_nested_functions will recompute it but the
1795 DECL_STATIC_CHAIN flag is also used before that happens,
1796 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1797 DECL_STATIC_CHAIN (decl) = 1;
1798 add_stmt (fnbody);
1799 finish_function ();
1800 c_pop_function_context ();
1801 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1803 else
1805 add_stmt (fnbody);
1806 finish_function ();
1809 timevar_pop (tv);
1810 break;
1814 /* Parse an asm-definition (asm() outside a function body). This is a
1815 GNU extension.
1817 asm-definition:
1818 simple-asm-expr ;
1821 static void
1822 c_parser_asm_definition (c_parser *parser)
1824 tree asm_str = c_parser_simple_asm_expr (parser);
1825 if (asm_str)
1826 add_asm_node (asm_str);
1827 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1830 /* Parse a static assertion (C11 6.7.10).
1832 static_assert-declaration:
1833 static_assert-declaration-no-semi ;
1836 static void
1837 c_parser_static_assert_declaration (c_parser *parser)
1839 c_parser_static_assert_declaration_no_semi (parser);
1840 if (parser->error
1841 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1842 c_parser_skip_to_end_of_block_or_statement (parser);
1845 /* Parse a static assertion (C11 6.7.10), without the trailing
1846 semicolon.
1848 static_assert-declaration-no-semi:
1849 _Static_assert ( constant-expression , string-literal )
1852 static void
1853 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1855 location_t assert_loc, value_loc;
1856 tree value;
1857 tree string;
1859 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1860 assert_loc = c_parser_peek_token (parser)->location;
1861 if (!flag_isoc11)
1863 if (flag_isoc99)
1864 pedwarn (assert_loc, OPT_Wpedantic,
1865 "ISO C99 does not support %<_Static_assert%>");
1866 else
1867 pedwarn (assert_loc, OPT_Wpedantic,
1868 "ISO C90 does not support %<_Static_assert%>");
1870 c_parser_consume_token (parser);
1871 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1872 return;
1873 value_loc = c_parser_peek_token (parser)->location;
1874 value = c_parser_expr_no_commas (parser, NULL).value;
1875 parser->lex_untranslated_string = true;
1876 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1878 parser->lex_untranslated_string = false;
1879 return;
1881 switch (c_parser_peek_token (parser)->type)
1883 case CPP_STRING:
1884 case CPP_STRING16:
1885 case CPP_STRING32:
1886 case CPP_WSTRING:
1887 case CPP_UTF8STRING:
1888 string = c_parser_peek_token (parser)->value;
1889 c_parser_consume_token (parser);
1890 parser->lex_untranslated_string = false;
1891 break;
1892 default:
1893 c_parser_error (parser, "expected string literal");
1894 parser->lex_untranslated_string = false;
1895 return;
1897 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1899 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1901 error_at (value_loc, "expression in static assertion is not an integer");
1902 return;
1904 if (TREE_CODE (value) != INTEGER_CST)
1906 value = c_fully_fold (value, false, NULL);
1907 if (TREE_CODE (value) == INTEGER_CST)
1908 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
1909 "is not an integer constant expression");
1911 if (TREE_CODE (value) != INTEGER_CST)
1913 error_at (value_loc, "expression in static assertion is not constant");
1914 return;
1916 constant_expression_warning (value);
1917 if (integer_zerop (value))
1918 error_at (assert_loc, "static assertion failed: %E", string);
1921 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1922 6.7), adding them to SPECS (which may already include some).
1923 Storage class specifiers are accepted iff SCSPEC_OK; type
1924 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1925 the start iff START_ATTR_OK.
1927 declaration-specifiers:
1928 storage-class-specifier declaration-specifiers[opt]
1929 type-specifier declaration-specifiers[opt]
1930 type-qualifier declaration-specifiers[opt]
1931 function-specifier declaration-specifiers[opt]
1932 alignment-specifier declaration-specifiers[opt]
1934 Function specifiers (inline) are from C99, and are currently
1935 handled as storage class specifiers, as is __thread. Alignment
1936 specifiers are from C11.
1938 C90 6.5.1, C99 6.7.1:
1939 storage-class-specifier:
1940 typedef
1941 extern
1942 static
1943 auto
1944 register
1946 C99 6.7.4:
1947 function-specifier:
1948 inline
1949 _Noreturn
1951 (_Noreturn is new in C11.)
1953 C90 6.5.2, C99 6.7.2:
1954 type-specifier:
1955 void
1956 char
1957 short
1959 long
1960 float
1961 double
1962 signed
1963 unsigned
1964 _Bool
1965 _Complex
1966 [_Imaginary removed in C99 TC2]
1967 struct-or-union-specifier
1968 enum-specifier
1969 typedef-name
1971 (_Bool and _Complex are new in C99.)
1973 C90 6.5.3, C99 6.7.3:
1975 type-qualifier:
1976 const
1977 restrict
1978 volatile
1979 address-space-qualifier
1981 (restrict is new in C99.)
1983 GNU extensions:
1985 declaration-specifiers:
1986 attributes declaration-specifiers[opt]
1988 type-qualifier:
1989 address-space
1991 address-space:
1992 identifier recognized by the target
1994 storage-class-specifier:
1995 __thread
1997 type-specifier:
1998 typeof-specifier
1999 __int128
2000 _Decimal32
2001 _Decimal64
2002 _Decimal128
2003 _Fract
2004 _Accum
2005 _Sat
2007 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2008 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2010 Objective-C:
2012 type-specifier:
2013 class-name objc-protocol-refs[opt]
2014 typedef-name objc-protocol-refs
2015 objc-protocol-refs
2018 static void
2019 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2020 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2021 enum c_lookahead_kind la)
2023 bool attrs_ok = start_attr_ok;
2024 bool seen_type = specs->typespec_kind != ctsk_none;
2026 if (!typespec_ok)
2027 gcc_assert (la == cla_prefer_id);
2029 while (c_parser_next_token_is (parser, CPP_NAME)
2030 || c_parser_next_token_is (parser, CPP_KEYWORD)
2031 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2033 struct c_typespec t;
2034 tree attrs;
2035 tree align;
2036 location_t loc = c_parser_peek_token (parser)->location;
2038 /* If we cannot accept a type, exit if the next token must start
2039 one. Also, if we already have seen a tagged definition,
2040 a typename would be an error anyway and likely the user
2041 has simply forgotten a semicolon, so we exit. */
2042 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2043 && c_parser_next_tokens_start_typename (parser, la)
2044 && !c_parser_next_token_is_qualifier (parser))
2045 break;
2047 if (c_parser_next_token_is (parser, CPP_NAME))
2049 c_token *name_token = c_parser_peek_token (parser);
2050 tree value = name_token->value;
2051 c_id_kind kind = name_token->id_kind;
2053 if (kind == C_ID_ADDRSPACE)
2055 addr_space_t as
2056 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2057 declspecs_add_addrspace (name_token->location, specs, as);
2058 c_parser_consume_token (parser);
2059 attrs_ok = true;
2060 continue;
2063 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2065 /* If we cannot accept a type, and the next token must start one,
2066 exit. Do the same if we already have seen a tagged definition,
2067 since it would be an error anyway and likely the user has simply
2068 forgotten a semicolon. */
2069 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2070 break;
2072 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2073 a C_ID_CLASSNAME. */
2074 c_parser_consume_token (parser);
2075 seen_type = true;
2076 attrs_ok = true;
2077 if (kind == C_ID_ID)
2079 error ("unknown type name %qE", value);
2080 t.kind = ctsk_typedef;
2081 t.spec = error_mark_node;
2083 else if (kind == C_ID_TYPENAME
2084 && (!c_dialect_objc ()
2085 || c_parser_next_token_is_not (parser, CPP_LESS)))
2087 t.kind = ctsk_typedef;
2088 /* For a typedef name, record the meaning, not the name.
2089 In case of 'foo foo, bar;'. */
2090 t.spec = lookup_name (value);
2092 else
2094 tree proto = NULL_TREE;
2095 gcc_assert (c_dialect_objc ());
2096 t.kind = ctsk_objc;
2097 if (c_parser_next_token_is (parser, CPP_LESS))
2098 proto = c_parser_objc_protocol_refs (parser);
2099 t.spec = objc_get_protocol_qualified_type (value, proto);
2101 t.expr = NULL_TREE;
2102 t.expr_const_operands = true;
2103 declspecs_add_type (name_token->location, specs, t);
2104 continue;
2106 if (c_parser_next_token_is (parser, CPP_LESS))
2108 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2109 nisse@lysator.liu.se. */
2110 tree proto;
2111 gcc_assert (c_dialect_objc ());
2112 if (!typespec_ok || seen_type)
2113 break;
2114 proto = c_parser_objc_protocol_refs (parser);
2115 t.kind = ctsk_objc;
2116 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2117 t.expr = NULL_TREE;
2118 t.expr_const_operands = true;
2119 declspecs_add_type (loc, specs, t);
2120 continue;
2122 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2123 switch (c_parser_peek_token (parser)->keyword)
2125 case RID_STATIC:
2126 case RID_EXTERN:
2127 case RID_REGISTER:
2128 case RID_TYPEDEF:
2129 case RID_INLINE:
2130 case RID_NORETURN:
2131 case RID_AUTO:
2132 case RID_THREAD:
2133 if (!scspec_ok)
2134 goto out;
2135 attrs_ok = true;
2136 /* TODO: Distinguish between function specifiers (inline, noreturn)
2137 and storage class specifiers, either here or in
2138 declspecs_add_scspec. */
2139 declspecs_add_scspec (loc, specs,
2140 c_parser_peek_token (parser)->value);
2141 c_parser_consume_token (parser);
2142 break;
2143 case RID_UNSIGNED:
2144 case RID_LONG:
2145 case RID_INT128:
2146 case RID_SHORT:
2147 case RID_SIGNED:
2148 case RID_COMPLEX:
2149 case RID_INT:
2150 case RID_CHAR:
2151 case RID_FLOAT:
2152 case RID_DOUBLE:
2153 case RID_VOID:
2154 case RID_DFLOAT32:
2155 case RID_DFLOAT64:
2156 case RID_DFLOAT128:
2157 case RID_BOOL:
2158 case RID_FRACT:
2159 case RID_ACCUM:
2160 case RID_SAT:
2161 if (!typespec_ok)
2162 goto out;
2163 attrs_ok = true;
2164 seen_type = true;
2165 if (c_dialect_objc ())
2166 parser->objc_need_raw_identifier = true;
2167 t.kind = ctsk_resword;
2168 t.spec = c_parser_peek_token (parser)->value;
2169 t.expr = NULL_TREE;
2170 t.expr_const_operands = true;
2171 declspecs_add_type (loc, specs, t);
2172 c_parser_consume_token (parser);
2173 break;
2174 case RID_ENUM:
2175 if (!typespec_ok)
2176 goto out;
2177 attrs_ok = true;
2178 seen_type = true;
2179 t = c_parser_enum_specifier (parser);
2180 declspecs_add_type (loc, specs, t);
2181 break;
2182 case RID_STRUCT:
2183 case RID_UNION:
2184 if (!typespec_ok)
2185 goto out;
2186 attrs_ok = true;
2187 seen_type = true;
2188 t = c_parser_struct_or_union_specifier (parser);
2189 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2190 declspecs_add_type (loc, specs, t);
2191 break;
2192 case RID_TYPEOF:
2193 /* ??? The old parser rejected typeof after other type
2194 specifiers, but is a syntax error the best way of
2195 handling this? */
2196 if (!typespec_ok || seen_type)
2197 goto out;
2198 attrs_ok = true;
2199 seen_type = true;
2200 t = c_parser_typeof_specifier (parser);
2201 declspecs_add_type (loc, specs, t);
2202 break;
2203 case RID_CONST:
2204 case RID_VOLATILE:
2205 case RID_RESTRICT:
2206 attrs_ok = true;
2207 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2208 c_parser_consume_token (parser);
2209 break;
2210 /* UPC qualifiers */
2211 case RID_SHARED:
2212 attrs_ok = true;
2213 c_parser_upc_shared_qual (loc, parser, specs);
2214 break;
2215 case RID_STRICT:
2216 case RID_RELAXED:
2217 attrs_ok = true;
2218 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2219 c_parser_consume_token (parser);
2220 break;
2221 case RID_ATTRIBUTE:
2222 if (!attrs_ok)
2223 goto out;
2224 attrs = c_parser_attributes (parser);
2225 declspecs_add_attrs (loc, specs, attrs);
2226 break;
2227 case RID_ALIGNAS:
2228 align = c_parser_alignas_specifier (parser);
2229 declspecs_add_alignas (loc, specs, align);
2230 break;
2231 default:
2232 goto out;
2235 out: ;
2238 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2240 enum-specifier:
2241 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2242 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2243 enum attributes[opt] identifier
2245 The form with trailing comma is new in C99. The forms with
2246 attributes are GNU extensions. In GNU C, we accept any expression
2247 without commas in the syntax (assignment expressions, not just
2248 conditional expressions); assignment expressions will be diagnosed
2249 as non-constant.
2251 enumerator-list:
2252 enumerator
2253 enumerator-list , enumerator
2255 enumerator:
2256 enumeration-constant
2257 enumeration-constant = constant-expression
2260 static struct c_typespec
2261 c_parser_enum_specifier (c_parser *parser)
2263 struct c_typespec ret;
2264 tree attrs;
2265 tree ident = NULL_TREE;
2266 location_t enum_loc;
2267 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2268 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2269 enum_loc = c_parser_peek_token (parser)->location;
2270 c_parser_consume_token (parser);
2271 attrs = c_parser_attributes (parser);
2272 enum_loc = c_parser_peek_token (parser)->location;
2273 /* Set the location in case we create a decl now. */
2274 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2275 if (c_parser_next_token_is (parser, CPP_NAME))
2277 ident = c_parser_peek_token (parser)->value;
2278 ident_loc = c_parser_peek_token (parser)->location;
2279 enum_loc = ident_loc;
2280 c_parser_consume_token (parser);
2282 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2284 /* Parse an enum definition. */
2285 struct c_enum_contents the_enum;
2286 tree type;
2287 tree postfix_attrs;
2288 /* We chain the enumerators in reverse order, then put them in
2289 forward order at the end. */
2290 tree values;
2291 timevar_push (TV_PARSE_ENUM);
2292 type = start_enum (enum_loc, &the_enum, ident);
2293 values = NULL_TREE;
2294 c_parser_consume_token (parser);
2295 while (true)
2297 tree enum_id;
2298 tree enum_value;
2299 tree enum_decl;
2300 bool seen_comma;
2301 c_token *token;
2302 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2303 location_t decl_loc, value_loc;
2304 if (c_parser_next_token_is_not (parser, CPP_NAME))
2306 c_parser_error (parser, "expected identifier");
2307 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2308 values = error_mark_node;
2309 break;
2311 token = c_parser_peek_token (parser);
2312 enum_id = token->value;
2313 /* Set the location in case we create a decl now. */
2314 c_parser_set_source_position_from_token (token);
2315 decl_loc = value_loc = token->location;
2316 c_parser_consume_token (parser);
2317 if (c_parser_next_token_is (parser, CPP_EQ))
2319 c_parser_consume_token (parser);
2320 value_loc = c_parser_peek_token (parser)->location;
2321 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2323 else
2324 enum_value = NULL_TREE;
2325 enum_decl = build_enumerator (decl_loc, value_loc,
2326 &the_enum, enum_id, enum_value);
2327 TREE_CHAIN (enum_decl) = values;
2328 values = enum_decl;
2329 seen_comma = false;
2330 if (c_parser_next_token_is (parser, CPP_COMMA))
2332 comma_loc = c_parser_peek_token (parser)->location;
2333 seen_comma = true;
2334 c_parser_consume_token (parser);
2336 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2338 if (seen_comma && !flag_isoc99)
2339 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2340 c_parser_consume_token (parser);
2341 break;
2343 if (!seen_comma)
2345 c_parser_error (parser, "expected %<,%> or %<}%>");
2346 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2347 values = error_mark_node;
2348 break;
2351 postfix_attrs = c_parser_attributes (parser);
2352 ret.spec = finish_enum (type, nreverse (values),
2353 chainon (attrs, postfix_attrs));
2354 ret.kind = ctsk_tagdef;
2355 ret.expr = NULL_TREE;
2356 ret.expr_const_operands = true;
2357 timevar_pop (TV_PARSE_ENUM);
2358 return ret;
2360 else if (!ident)
2362 c_parser_error (parser, "expected %<{%>");
2363 ret.spec = error_mark_node;
2364 ret.kind = ctsk_tagref;
2365 ret.expr = NULL_TREE;
2366 ret.expr_const_operands = true;
2367 return ret;
2369 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2370 /* In ISO C, enumerated types can be referred to only if already
2371 defined. */
2372 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2374 gcc_assert (ident);
2375 pedwarn (enum_loc, OPT_Wpedantic,
2376 "ISO C forbids forward references to %<enum%> types");
2378 return ret;
2381 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2383 struct-or-union-specifier:
2384 struct-or-union attributes[opt] identifier[opt]
2385 { struct-contents } attributes[opt]
2386 struct-or-union attributes[opt] identifier
2388 struct-contents:
2389 struct-declaration-list
2391 struct-declaration-list:
2392 struct-declaration ;
2393 struct-declaration-list struct-declaration ;
2395 GNU extensions:
2397 struct-contents:
2398 empty
2399 struct-declaration
2400 struct-declaration-list struct-declaration
2402 struct-declaration-list:
2403 struct-declaration-list ;
2406 (Note that in the syntax here, unlike that in ISO C, the semicolons
2407 are included here rather than in struct-declaration, in order to
2408 describe the syntax with extra semicolons and missing semicolon at
2409 end.)
2411 Objective-C:
2413 struct-declaration-list:
2414 @defs ( class-name )
2416 (Note this does not include a trailing semicolon, but can be
2417 followed by further declarations, and gets a pedwarn-if-pedantic
2418 when followed by a semicolon.) */
2420 static struct c_typespec
2421 c_parser_struct_or_union_specifier (c_parser *parser)
2423 struct c_typespec ret;
2424 tree attrs;
2425 tree ident = NULL_TREE;
2426 location_t struct_loc;
2427 location_t ident_loc = UNKNOWN_LOCATION;
2428 enum tree_code code;
2429 switch (c_parser_peek_token (parser)->keyword)
2431 case RID_STRUCT:
2432 code = RECORD_TYPE;
2433 break;
2434 case RID_UNION:
2435 code = UNION_TYPE;
2436 break;
2437 default:
2438 gcc_unreachable ();
2440 struct_loc = c_parser_peek_token (parser)->location;
2441 c_parser_consume_token (parser);
2442 attrs = c_parser_attributes (parser);
2444 /* Set the location in case we create a decl now. */
2445 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2447 if (c_parser_next_token_is (parser, CPP_NAME))
2449 ident = c_parser_peek_token (parser)->value;
2450 ident_loc = c_parser_peek_token (parser)->location;
2451 struct_loc = ident_loc;
2452 c_parser_consume_token (parser);
2454 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2456 /* Parse a struct or union definition. Start the scope of the
2457 tag before parsing components. */
2458 struct c_struct_parse_info *struct_info;
2459 tree type = start_struct (struct_loc, code, ident, &struct_info);
2460 tree postfix_attrs;
2461 /* We chain the components in reverse order, then put them in
2462 forward order at the end. Each struct-declaration may
2463 declare multiple components (comma-separated), so we must use
2464 chainon to join them, although when parsing each
2465 struct-declaration we can use TREE_CHAIN directly.
2467 The theory behind all this is that there will be more
2468 semicolon separated fields than comma separated fields, and
2469 so we'll be minimizing the number of node traversals required
2470 by chainon. */
2471 tree contents;
2472 timevar_push (TV_PARSE_STRUCT);
2473 contents = NULL_TREE;
2474 c_parser_consume_token (parser);
2475 /* Handle the Objective-C @defs construct,
2476 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2477 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2479 tree name;
2480 gcc_assert (c_dialect_objc ());
2481 c_parser_consume_token (parser);
2482 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2483 goto end_at_defs;
2484 if (c_parser_next_token_is (parser, CPP_NAME)
2485 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2487 name = c_parser_peek_token (parser)->value;
2488 c_parser_consume_token (parser);
2490 else
2492 c_parser_error (parser, "expected class name");
2493 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2494 goto end_at_defs;
2496 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2497 "expected %<)%>");
2498 contents = nreverse (objc_get_class_ivars (name));
2500 end_at_defs:
2501 /* Parse the struct-declarations and semicolons. Problems with
2502 semicolons are diagnosed here; empty structures are diagnosed
2503 elsewhere. */
2504 while (true)
2506 tree decls;
2507 /* Parse any stray semicolon. */
2508 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2510 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2511 "extra semicolon in struct or union specified");
2512 c_parser_consume_token (parser);
2513 continue;
2515 /* Stop if at the end of the struct or union contents. */
2516 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2518 c_parser_consume_token (parser);
2519 break;
2521 /* Accept #pragmas at struct scope. */
2522 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2524 c_parser_pragma (parser, pragma_external);
2525 continue;
2527 /* Parse some comma-separated declarations, but not the
2528 trailing semicolon if any. */
2529 decls = c_parser_struct_declaration (parser);
2530 contents = chainon (decls, contents);
2531 /* If no semicolon follows, either we have a parse error or
2532 are at the end of the struct or union and should
2533 pedwarn. */
2534 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2535 c_parser_consume_token (parser);
2536 else
2538 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2539 pedwarn (c_parser_peek_token (parser)->location, 0,
2540 "no semicolon at end of struct or union");
2541 else if (parser->error
2542 || !c_parser_next_token_starts_declspecs (parser))
2544 c_parser_error (parser, "expected %<;%>");
2545 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2546 break;
2549 /* If we come here, we have already emitted an error
2550 for an expected `;', identifier or `(', and we also
2551 recovered already. Go on with the next field. */
2554 postfix_attrs = c_parser_attributes (parser);
2555 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2556 chainon (attrs, postfix_attrs), struct_info);
2557 ret.kind = ctsk_tagdef;
2558 ret.expr = NULL_TREE;
2559 ret.expr_const_operands = true;
2560 timevar_pop (TV_PARSE_STRUCT);
2561 return ret;
2563 else if (!ident)
2565 c_parser_error (parser, "expected %<{%>");
2566 ret.spec = error_mark_node;
2567 ret.kind = ctsk_tagref;
2568 ret.expr = NULL_TREE;
2569 ret.expr_const_operands = true;
2570 return ret;
2572 ret = parser_xref_tag (ident_loc, code, ident);
2573 return ret;
2576 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2577 the trailing semicolon.
2579 struct-declaration:
2580 specifier-qualifier-list struct-declarator-list
2581 static_assert-declaration-no-semi
2583 specifier-qualifier-list:
2584 type-specifier specifier-qualifier-list[opt]
2585 type-qualifier specifier-qualifier-list[opt]
2586 attributes specifier-qualifier-list[opt]
2588 struct-declarator-list:
2589 struct-declarator
2590 struct-declarator-list , attributes[opt] struct-declarator
2592 struct-declarator:
2593 declarator attributes[opt]
2594 declarator[opt] : constant-expression attributes[opt]
2596 GNU extensions:
2598 struct-declaration:
2599 __extension__ struct-declaration
2600 specifier-qualifier-list
2602 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2603 of attributes where shown is a GNU extension. In GNU C, we accept
2604 any expression without commas in the syntax (assignment
2605 expressions, not just conditional expressions); assignment
2606 expressions will be diagnosed as non-constant. */
2608 static tree
2609 c_parser_struct_declaration (c_parser *parser)
2611 struct c_declspecs *specs;
2612 tree prefix_attrs;
2613 tree all_prefix_attrs;
2614 tree decls;
2615 location_t decl_loc;
2616 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2618 int ext;
2619 tree decl;
2620 ext = disable_extension_diagnostics ();
2621 c_parser_consume_token (parser);
2622 decl = c_parser_struct_declaration (parser);
2623 restore_extension_diagnostics (ext);
2624 return decl;
2626 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2628 c_parser_static_assert_declaration_no_semi (parser);
2629 return NULL_TREE;
2631 specs = build_null_declspecs ();
2632 decl_loc = c_parser_peek_token (parser)->location;
2633 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2634 if (parser->error)
2635 return NULL_TREE;
2636 if (!specs->declspecs_seen_p)
2638 c_parser_error (parser, "expected specifier-qualifier-list");
2639 return NULL_TREE;
2641 finish_declspecs (specs);
2642 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2643 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2645 tree ret;
2646 if (specs->typespec_kind == ctsk_none)
2648 pedwarn (decl_loc, OPT_Wpedantic,
2649 "ISO C forbids member declarations with no members");
2650 shadow_tag_warned (specs, pedantic);
2651 ret = NULL_TREE;
2653 else
2655 /* Support for unnamed structs or unions as members of
2656 structs or unions (which is [a] useful and [b] supports
2657 MS P-SDK). */
2658 tree attrs = NULL;
2660 ret = grokfield (c_parser_peek_token (parser)->location,
2661 build_id_declarator (NULL_TREE), specs,
2662 NULL_TREE, &attrs);
2663 if (ret)
2664 decl_attributes (&ret, attrs, 0);
2666 return ret;
2669 /* Provide better error recovery. Note that a type name here is valid,
2670 and will be treated as a field name. */
2671 if (specs->typespec_kind == ctsk_tagdef
2672 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2673 && c_parser_next_token_starts_declspecs (parser)
2674 && !c_parser_next_token_is (parser, CPP_NAME))
2676 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2677 parser->error = false;
2678 return NULL_TREE;
2681 pending_xref_error ();
2682 prefix_attrs = specs->attrs;
2683 all_prefix_attrs = prefix_attrs;
2684 specs->attrs = NULL_TREE;
2685 decls = NULL_TREE;
2686 while (true)
2688 /* Declaring one or more declarators or un-named bit-fields. */
2689 struct c_declarator *declarator;
2690 bool dummy = false;
2691 if (c_parser_next_token_is (parser, CPP_COLON))
2692 declarator = build_id_declarator (NULL_TREE);
2693 else
2694 declarator = c_parser_declarator (parser,
2695 specs->typespec_kind != ctsk_none,
2696 C_DTR_NORMAL, &dummy);
2697 if (declarator == NULL)
2699 c_parser_skip_to_end_of_block_or_statement (parser);
2700 break;
2702 if (c_parser_next_token_is (parser, CPP_COLON)
2703 || c_parser_next_token_is (parser, CPP_COMMA)
2704 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2705 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2706 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2708 tree postfix_attrs = NULL_TREE;
2709 tree width = NULL_TREE;
2710 tree d;
2711 if (c_parser_next_token_is (parser, CPP_COLON))
2713 c_parser_consume_token (parser);
2714 width = c_parser_expr_no_commas (parser, NULL).value;
2716 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2717 postfix_attrs = c_parser_attributes (parser);
2718 d = grokfield (c_parser_peek_token (parser)->location,
2719 declarator, specs, width, &all_prefix_attrs);
2720 decl_attributes (&d, chainon (postfix_attrs,
2721 all_prefix_attrs), 0);
2722 DECL_CHAIN (d) = decls;
2723 decls = d;
2724 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2725 all_prefix_attrs = chainon (c_parser_attributes (parser),
2726 prefix_attrs);
2727 else
2728 all_prefix_attrs = prefix_attrs;
2729 if (c_parser_next_token_is (parser, CPP_COMMA))
2730 c_parser_consume_token (parser);
2731 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2732 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2734 /* Semicolon consumed in caller. */
2735 break;
2737 else
2739 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2740 break;
2743 else
2745 c_parser_error (parser,
2746 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2747 "%<__attribute__%>");
2748 break;
2751 return decls;
2754 /* Parse a typeof specifier (a GNU extension).
2756 typeof-specifier:
2757 typeof ( expression )
2758 typeof ( type-name )
2761 static struct c_typespec
2762 c_parser_typeof_specifier (c_parser *parser)
2764 struct c_typespec ret;
2765 ret.kind = ctsk_typeof;
2766 ret.spec = error_mark_node;
2767 ret.expr = NULL_TREE;
2768 ret.expr_const_operands = true;
2769 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2770 c_parser_consume_token (parser);
2771 c_inhibit_evaluation_warnings++;
2772 in_typeof++;
2773 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2775 c_inhibit_evaluation_warnings--;
2776 in_typeof--;
2777 return ret;
2779 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2781 struct c_type_name *type = c_parser_type_name (parser);
2782 c_inhibit_evaluation_warnings--;
2783 in_typeof--;
2784 if (type != NULL)
2786 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2787 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2790 else
2792 bool was_vm;
2793 location_t here = c_parser_peek_token (parser)->location;
2794 struct c_expr expr = c_parser_expression (parser);
2795 c_inhibit_evaluation_warnings--;
2796 in_typeof--;
2797 if (TREE_CODE (expr.value) == COMPONENT_REF
2798 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2799 error_at (here, "%<typeof%> applied to a bit-field");
2800 mark_exp_read (expr.value);
2801 ret.spec = TREE_TYPE (expr.value);
2802 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2803 /* This is returned with the type so that when the type is
2804 evaluated, this can be evaluated. */
2805 if (was_vm)
2806 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2807 pop_maybe_used (was_vm);
2809 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2810 return ret;
2813 /* Parse an alignment-specifier.
2815 C11 6.7.5:
2817 alignment-specifier:
2818 _Alignas ( type-name )
2819 _Alignas ( constant-expression )
2822 static tree
2823 c_parser_alignas_specifier (c_parser * parser)
2825 tree ret = error_mark_node;
2826 location_t loc = c_parser_peek_token (parser)->location;
2827 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2828 c_parser_consume_token (parser);
2829 if (!flag_isoc11)
2831 if (flag_isoc99)
2832 pedwarn (loc, OPT_Wpedantic,
2833 "ISO C99 does not support %<_Alignas%>");
2834 else
2835 pedwarn (loc, OPT_Wpedantic,
2836 "ISO C90 does not support %<_Alignas%>");
2838 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2839 return ret;
2840 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2842 struct c_type_name *type = c_parser_type_name (parser);
2843 if (type != NULL)
2844 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2846 else
2847 ret = c_parser_expr_no_commas (parser, NULL).value;
2848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2849 return ret;
2852 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2853 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2854 be redeclared; otherwise it may not. KIND indicates which kind of
2855 declarator is wanted. Returns a valid declarator except in the
2856 case of a syntax error in which case NULL is returned. *SEEN_ID is
2857 set to true if an identifier being declared is seen; this is used
2858 to diagnose bad forms of abstract array declarators and to
2859 determine whether an identifier list is syntactically permitted.
2861 declarator:
2862 pointer[opt] direct-declarator
2864 direct-declarator:
2865 identifier
2866 ( attributes[opt] declarator )
2867 direct-declarator array-declarator
2868 direct-declarator ( parameter-type-list )
2869 direct-declarator ( identifier-list[opt] )
2871 pointer:
2872 * type-qualifier-list[opt]
2873 * type-qualifier-list[opt] pointer
2875 type-qualifier-list:
2876 type-qualifier
2877 attributes
2878 type-qualifier-list type-qualifier
2879 type-qualifier-list attributes
2881 parameter-type-list:
2882 parameter-list
2883 parameter-list , ...
2885 parameter-list:
2886 parameter-declaration
2887 parameter-list , parameter-declaration
2889 parameter-declaration:
2890 declaration-specifiers declarator attributes[opt]
2891 declaration-specifiers abstract-declarator[opt] attributes[opt]
2893 identifier-list:
2894 identifier
2895 identifier-list , identifier
2897 abstract-declarator:
2898 pointer
2899 pointer[opt] direct-abstract-declarator
2901 direct-abstract-declarator:
2902 ( attributes[opt] abstract-declarator )
2903 direct-abstract-declarator[opt] array-declarator
2904 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2906 GNU extensions:
2908 direct-declarator:
2909 direct-declarator ( parameter-forward-declarations
2910 parameter-type-list[opt] )
2912 direct-abstract-declarator:
2913 direct-abstract-declarator[opt] ( parameter-forward-declarations
2914 parameter-type-list[opt] )
2916 parameter-forward-declarations:
2917 parameter-list ;
2918 parameter-forward-declarations parameter-list ;
2920 The uses of attributes shown above are GNU extensions.
2922 Some forms of array declarator are not included in C99 in the
2923 syntax for abstract declarators; these are disallowed elsewhere.
2924 This may be a defect (DR#289).
2926 This function also accepts an omitted abstract declarator as being
2927 an abstract declarator, although not part of the formal syntax. */
2929 static struct c_declarator *
2930 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2931 bool *seen_id)
2933 /* Parse any initial pointer part. */
2934 if (c_parser_next_token_is (parser, CPP_MULT))
2936 struct c_declspecs *quals_attrs = build_null_declspecs ();
2937 struct c_declarator *inner;
2938 c_parser_consume_token (parser);
2939 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2940 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2941 if (inner == NULL)
2942 return NULL;
2943 else
2944 return make_pointer_declarator (quals_attrs, inner);
2946 /* Now we have a direct declarator, direct abstract declarator or
2947 nothing (which counts as a direct abstract declarator here). */
2948 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2951 /* Parse a direct declarator or direct abstract declarator; arguments
2952 as c_parser_declarator. */
2954 static struct c_declarator *
2955 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2956 bool *seen_id)
2958 /* The direct declarator must start with an identifier (possibly
2959 omitted) or a parenthesized declarator (possibly abstract). In
2960 an ordinary declarator, initial parentheses must start a
2961 parenthesized declarator. In an abstract declarator or parameter
2962 declarator, they could start a parenthesized declarator or a
2963 parameter list. To tell which, the open parenthesis and any
2964 following attributes must be read. If a declaration specifier
2965 follows, then it is a parameter list; if the specifier is a
2966 typedef name, there might be an ambiguity about redeclaring it,
2967 which is resolved in the direction of treating it as a typedef
2968 name. If a close parenthesis follows, it is also an empty
2969 parameter list, as the syntax does not permit empty abstract
2970 declarators. Otherwise, it is a parenthesized declarator (in
2971 which case the analysis may be repeated inside it, recursively).
2973 ??? There is an ambiguity in a parameter declaration "int
2974 (__attribute__((foo)) x)", where x is not a typedef name: it
2975 could be an abstract declarator for a function, or declare x with
2976 parentheses. The proper resolution of this ambiguity needs
2977 documenting. At present we follow an accident of the old
2978 parser's implementation, whereby the first parameter must have
2979 some declaration specifiers other than just attributes. Thus as
2980 a parameter declaration it is treated as a parenthesized
2981 parameter named x, and as an abstract declarator it is
2982 rejected.
2984 ??? Also following the old parser, attributes inside an empty
2985 parameter list are ignored, making it a list not yielding a
2986 prototype, rather than giving an error or making it have one
2987 parameter with implicit type int.
2989 ??? Also following the old parser, typedef names may be
2990 redeclared in declarators, but not Objective-C class names. */
2992 if (kind != C_DTR_ABSTRACT
2993 && c_parser_next_token_is (parser, CPP_NAME)
2994 && ((type_seen_p
2995 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2996 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2997 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2999 struct c_declarator *inner
3000 = build_id_declarator (c_parser_peek_token (parser)->value);
3001 *seen_id = true;
3002 inner->id_loc = c_parser_peek_token (parser)->location;
3003 c_parser_consume_token (parser);
3004 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3007 if (kind != C_DTR_NORMAL
3008 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3010 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3011 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3014 /* Either we are at the end of an abstract declarator, or we have
3015 parentheses. */
3017 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3019 tree attrs;
3020 struct c_declarator *inner;
3021 c_parser_consume_token (parser);
3022 attrs = c_parser_attributes (parser);
3023 if (kind != C_DTR_NORMAL
3024 && (c_parser_next_token_starts_declspecs (parser)
3025 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3027 struct c_arg_info *args
3028 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3029 attrs);
3030 if (args == NULL)
3031 return NULL;
3032 else
3034 inner
3035 = build_function_declarator (args,
3036 build_id_declarator (NULL_TREE));
3037 return c_parser_direct_declarator_inner (parser, *seen_id,
3038 inner);
3041 /* A parenthesized declarator. */
3042 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3043 if (inner != NULL && attrs != NULL)
3044 inner = build_attrs_declarator (attrs, inner);
3045 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3047 c_parser_consume_token (parser);
3048 if (inner == NULL)
3049 return NULL;
3050 else
3051 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3053 else
3055 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3056 "expected %<)%>");
3057 return NULL;
3060 else
3062 if (kind == C_DTR_NORMAL)
3064 c_parser_error (parser, "expected identifier or %<(%>");
3065 return NULL;
3067 else
3068 return build_id_declarator (NULL_TREE);
3072 /* Parse part of a direct declarator or direct abstract declarator,
3073 given that some (in INNER) has already been parsed; ID_PRESENT is
3074 true if an identifier is present, false for an abstract
3075 declarator. */
3077 static struct c_declarator *
3078 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3079 struct c_declarator *inner)
3081 /* Parse a sequence of array declarators and parameter lists. */
3082 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3084 location_t brace_loc = c_parser_peek_token (parser)->location;
3085 struct c_declarator *declarator;
3086 struct c_declspecs *quals_attrs = build_null_declspecs ();
3087 bool static_seen;
3088 bool star_seen;
3089 tree dimen;
3090 c_parser_consume_token (parser);
3091 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3092 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3093 if (static_seen)
3094 c_parser_consume_token (parser);
3095 if (static_seen && !quals_attrs->declspecs_seen_p)
3096 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3097 if (!quals_attrs->declspecs_seen_p)
3098 quals_attrs = NULL;
3099 /* If "static" is present, there must be an array dimension.
3100 Otherwise, there may be a dimension, "*", or no
3101 dimension. */
3102 if (static_seen)
3104 star_seen = false;
3105 dimen = c_parser_expr_no_commas (parser, NULL).value;
3107 else
3109 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3111 dimen = NULL_TREE;
3112 star_seen = false;
3114 else if (flag_enable_cilkplus
3115 && c_parser_next_token_is (parser, CPP_COLON))
3117 dimen = error_mark_node;
3118 star_seen = false;
3119 error_at (c_parser_peek_token (parser)->location,
3120 "array notations cannot be used in declaration");
3121 c_parser_consume_token (parser);
3123 else if (c_parser_next_token_is (parser, CPP_MULT))
3125 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3127 dimen = NULL_TREE;
3128 star_seen = true;
3129 c_parser_consume_token (parser);
3131 else
3133 star_seen = false;
3134 dimen = c_parser_expr_no_commas (parser, NULL).value;
3137 else
3139 star_seen = false;
3140 dimen = c_parser_expr_no_commas (parser, NULL).value;
3143 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3144 c_parser_consume_token (parser);
3145 else if (flag_enable_cilkplus
3146 && c_parser_next_token_is (parser, CPP_COLON))
3148 error_at (c_parser_peek_token (parser)->location,
3149 "array notations cannot be used in declaration");
3150 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3151 return NULL;
3153 else
3155 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3156 "expected %<]%>");
3157 return NULL;
3159 if (dimen)
3160 mark_exp_read (dimen);
3161 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3162 static_seen, star_seen);
3163 if (declarator == NULL)
3164 return NULL;
3165 inner = set_array_declarator_inner (declarator, inner);
3166 return c_parser_direct_declarator_inner (parser, id_present, inner);
3168 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3170 tree attrs;
3171 struct c_arg_info *args;
3172 c_parser_consume_token (parser);
3173 attrs = c_parser_attributes (parser);
3174 args = c_parser_parms_declarator (parser, id_present, attrs);
3175 if (args == NULL)
3176 return NULL;
3177 else
3179 inner = build_function_declarator (args, inner);
3180 return c_parser_direct_declarator_inner (parser, id_present, inner);
3183 return inner;
3186 /* Parse a parameter list or identifier list, including the closing
3187 parenthesis but not the opening one. ATTRS are the attributes at
3188 the start of the list. ID_LIST_OK is true if an identifier list is
3189 acceptable; such a list must not have attributes at the start. */
3191 static struct c_arg_info *
3192 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3194 push_scope ();
3195 declare_parm_level ();
3196 /* If the list starts with an identifier, it is an identifier list.
3197 Otherwise, it is either a prototype list or an empty list. */
3198 if (id_list_ok
3199 && !attrs
3200 && c_parser_next_token_is (parser, CPP_NAME)
3201 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3203 /* Look ahead to detect typos in type names. */
3204 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3205 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3206 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3207 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3209 tree list = NULL_TREE, *nextp = &list;
3210 while (c_parser_next_token_is (parser, CPP_NAME)
3211 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3213 *nextp = build_tree_list (NULL_TREE,
3214 c_parser_peek_token (parser)->value);
3215 nextp = & TREE_CHAIN (*nextp);
3216 c_parser_consume_token (parser);
3217 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3218 break;
3219 c_parser_consume_token (parser);
3220 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3222 c_parser_error (parser, "expected identifier");
3223 break;
3226 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3228 struct c_arg_info *ret = build_arg_info ();
3229 ret->types = list;
3230 c_parser_consume_token (parser);
3231 pop_scope ();
3232 return ret;
3234 else
3236 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3237 "expected %<)%>");
3238 pop_scope ();
3239 return NULL;
3242 else
3244 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3245 NULL);
3246 pop_scope ();
3247 return ret;
3251 /* Parse a parameter list (possibly empty), including the closing
3252 parenthesis but not the opening one. ATTRS are the attributes at
3253 the start of the list. EXPR is NULL or an expression that needs to
3254 be evaluated for the side effects of array size expressions in the
3255 parameters. */
3257 static struct c_arg_info *
3258 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3260 bool bad_parm = false;
3262 /* ??? Following the old parser, forward parameter declarations may
3263 use abstract declarators, and if no real parameter declarations
3264 follow the forward declarations then this is not diagnosed. Also
3265 note as above that attributes are ignored as the only contents of
3266 the parentheses, or as the only contents after forward
3267 declarations. */
3268 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3270 struct c_arg_info *ret = build_arg_info ();
3271 c_parser_consume_token (parser);
3272 return ret;
3274 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3276 struct c_arg_info *ret = build_arg_info ();
3278 if (flag_allow_parameterless_variadic_functions)
3280 /* F (...) is allowed. */
3281 ret->types = NULL_TREE;
3283 else
3285 /* Suppress -Wold-style-definition for this case. */
3286 ret->types = error_mark_node;
3287 error_at (c_parser_peek_token (parser)->location,
3288 "ISO C requires a named argument before %<...%>");
3290 c_parser_consume_token (parser);
3291 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3293 c_parser_consume_token (parser);
3294 return ret;
3296 else
3298 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3299 "expected %<)%>");
3300 return NULL;
3303 /* Nonempty list of parameters, either terminated with semicolon
3304 (forward declarations; recurse) or with close parenthesis (normal
3305 function) or with ", ... )" (variadic function). */
3306 while (true)
3308 /* Parse a parameter. */
3309 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3310 attrs = NULL_TREE;
3311 if (parm == NULL)
3312 bad_parm = true;
3313 else
3314 push_parm_decl (parm, &expr);
3315 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3317 tree new_attrs;
3318 c_parser_consume_token (parser);
3319 mark_forward_parm_decls ();
3320 new_attrs = c_parser_attributes (parser);
3321 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3323 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3325 c_parser_consume_token (parser);
3326 if (bad_parm)
3327 return NULL;
3328 else
3329 return get_parm_info (false, expr);
3331 if (!c_parser_require (parser, CPP_COMMA,
3332 "expected %<;%>, %<,%> or %<)%>"))
3334 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3335 return NULL;
3337 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3339 c_parser_consume_token (parser);
3340 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3342 c_parser_consume_token (parser);
3343 if (bad_parm)
3344 return NULL;
3345 else
3346 return get_parm_info (true, expr);
3348 else
3350 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3351 "expected %<)%>");
3352 return NULL;
3358 /* Parse a parameter declaration. ATTRS are the attributes at the
3359 start of the declaration if it is the first parameter. */
3361 static struct c_parm *
3362 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3364 struct c_declspecs *specs;
3365 struct c_declarator *declarator;
3366 tree prefix_attrs;
3367 tree postfix_attrs = NULL_TREE;
3368 bool dummy = false;
3370 /* Accept #pragmas between parameter declarations. */
3371 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3372 c_parser_pragma (parser, pragma_external);
3374 if (!c_parser_next_token_starts_declspecs (parser))
3376 c_token *token = c_parser_peek_token (parser);
3377 if (parser->error)
3378 return NULL;
3379 c_parser_set_source_position_from_token (token);
3380 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3382 error ("unknown type name %qE", token->value);
3383 parser->error = true;
3385 /* ??? In some Objective-C cases '...' isn't applicable so there
3386 should be a different message. */
3387 else
3388 c_parser_error (parser,
3389 "expected declaration specifiers or %<...%>");
3390 c_parser_skip_to_end_of_parameter (parser);
3391 return NULL;
3393 specs = build_null_declspecs ();
3394 if (attrs)
3396 declspecs_add_attrs (input_location, specs, attrs);
3397 attrs = NULL_TREE;
3399 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3400 finish_declspecs (specs);
3401 pending_xref_error ();
3402 prefix_attrs = specs->attrs;
3403 specs->attrs = NULL_TREE;
3404 declarator = c_parser_declarator (parser,
3405 specs->typespec_kind != ctsk_none,
3406 C_DTR_PARM, &dummy);
3407 if (declarator == NULL)
3409 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3410 return NULL;
3412 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3413 postfix_attrs = c_parser_attributes (parser);
3414 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3415 declarator);
3418 /* Parse a string literal in an asm expression. It should not be
3419 translated, and wide string literals are an error although
3420 permitted by the syntax. This is a GNU extension.
3422 asm-string-literal:
3423 string-literal
3425 ??? At present, following the old parser, the caller needs to have
3426 set lex_untranslated_string to 1. It would be better to follow the
3427 C++ parser rather than using this kludge. */
3429 static tree
3430 c_parser_asm_string_literal (c_parser *parser)
3432 tree str;
3433 int save_flag = warn_overlength_strings;
3434 warn_overlength_strings = 0;
3435 if (c_parser_next_token_is (parser, CPP_STRING))
3437 str = c_parser_peek_token (parser)->value;
3438 c_parser_consume_token (parser);
3440 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3442 error_at (c_parser_peek_token (parser)->location,
3443 "wide string literal in %<asm%>");
3444 str = build_string (1, "");
3445 c_parser_consume_token (parser);
3447 else
3449 c_parser_error (parser, "expected string literal");
3450 str = NULL_TREE;
3452 warn_overlength_strings = save_flag;
3453 return str;
3456 /* Parse a simple asm expression. This is used in restricted
3457 contexts, where a full expression with inputs and outputs does not
3458 make sense. This is a GNU extension.
3460 simple-asm-expr:
3461 asm ( asm-string-literal )
3464 static tree
3465 c_parser_simple_asm_expr (c_parser *parser)
3467 tree str;
3468 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3469 /* ??? Follow the C++ parser rather than using the
3470 lex_untranslated_string kludge. */
3471 parser->lex_untranslated_string = true;
3472 c_parser_consume_token (parser);
3473 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3475 parser->lex_untranslated_string = false;
3476 return NULL_TREE;
3478 str = c_parser_asm_string_literal (parser);
3479 parser->lex_untranslated_string = false;
3480 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3482 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3483 return NULL_TREE;
3485 return str;
3488 static tree
3489 c_parser_attribute_any_word (c_parser *parser)
3491 tree attr_name = NULL_TREE;
3493 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3495 /* ??? See comment above about what keywords are accepted here. */
3496 bool ok;
3497 switch (c_parser_peek_token (parser)->keyword)
3499 case RID_STATIC:
3500 case RID_UNSIGNED:
3501 case RID_LONG:
3502 case RID_INT128:
3503 case RID_CONST:
3504 case RID_EXTERN:
3505 case RID_REGISTER:
3506 case RID_TYPEDEF:
3507 case RID_SHORT:
3508 case RID_INLINE:
3509 case RID_NORETURN:
3510 case RID_VOLATILE:
3511 case RID_SIGNED:
3512 case RID_AUTO:
3513 case RID_RESTRICT:
3514 case RID_COMPLEX:
3515 case RID_THREAD:
3516 case RID_INT:
3517 case RID_CHAR:
3518 case RID_FLOAT:
3519 case RID_DOUBLE:
3520 case RID_VOID:
3521 case RID_DFLOAT32:
3522 case RID_DFLOAT64:
3523 case RID_DFLOAT128:
3524 case RID_BOOL:
3525 case RID_FRACT:
3526 case RID_ACCUM:
3527 case RID_SAT:
3528 case RID_TRANSACTION_ATOMIC:
3529 case RID_TRANSACTION_CANCEL:
3530 ok = true;
3531 break;
3532 default:
3533 ok = false;
3534 break;
3536 if (!ok)
3537 return NULL_TREE;
3539 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3540 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3542 else if (c_parser_next_token_is (parser, CPP_NAME))
3543 attr_name = c_parser_peek_token (parser)->value;
3545 return attr_name;
3548 /* Parse (possibly empty) attributes. This is a GNU extension.
3550 attributes:
3551 empty
3552 attributes attribute
3554 attribute:
3555 __attribute__ ( ( attribute-list ) )
3557 attribute-list:
3558 attrib
3559 attribute_list , attrib
3561 attrib:
3562 empty
3563 any-word
3564 any-word ( identifier )
3565 any-word ( identifier , nonempty-expr-list )
3566 any-word ( expr-list )
3568 where the "identifier" must not be declared as a type, and
3569 "any-word" may be any identifier (including one declared as a
3570 type), a reserved word storage class specifier, type specifier or
3571 type qualifier. ??? This still leaves out most reserved keywords
3572 (following the old parser), shouldn't we include them, and why not
3573 allow identifiers declared as types to start the arguments? */
3575 static tree
3576 c_parser_attributes (c_parser *parser)
3578 tree attrs = NULL_TREE;
3579 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3581 /* ??? Follow the C++ parser rather than using the
3582 lex_untranslated_string kludge. */
3583 parser->lex_untranslated_string = true;
3584 c_parser_consume_token (parser);
3585 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3587 parser->lex_untranslated_string = false;
3588 return attrs;
3590 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3592 parser->lex_untranslated_string = false;
3593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3594 return attrs;
3596 /* Parse the attribute list. */
3597 while (c_parser_next_token_is (parser, CPP_COMMA)
3598 || c_parser_next_token_is (parser, CPP_NAME)
3599 || c_parser_next_token_is (parser, CPP_KEYWORD))
3601 tree attr, attr_name, attr_args;
3602 vec<tree, va_gc> *expr_list;
3603 if (c_parser_next_token_is (parser, CPP_COMMA))
3605 c_parser_consume_token (parser);
3606 continue;
3609 attr_name = c_parser_attribute_any_word (parser);
3610 if (attr_name == NULL)
3611 break;
3612 c_parser_consume_token (parser);
3613 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3615 attr = build_tree_list (attr_name, NULL_TREE);
3616 attrs = chainon (attrs, attr);
3617 continue;
3619 c_parser_consume_token (parser);
3620 /* Parse the attribute contents. If they start with an
3621 identifier which is followed by a comma or close
3622 parenthesis, then the arguments start with that
3623 identifier; otherwise they are an expression list.
3624 In objective-c the identifier may be a classname. */
3625 if (c_parser_next_token_is (parser, CPP_NAME)
3626 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3627 || (c_dialect_objc ()
3628 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3629 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3630 || (c_parser_peek_2nd_token (parser)->type
3631 == CPP_CLOSE_PAREN)))
3633 tree arg1 = c_parser_peek_token (parser)->value;
3634 c_parser_consume_token (parser);
3635 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3636 attr_args = build_tree_list (NULL_TREE, arg1);
3637 else
3639 tree tree_list;
3640 c_parser_consume_token (parser);
3641 expr_list = c_parser_expr_list (parser, false, true,
3642 NULL, NULL, NULL);
3643 tree_list = build_tree_list_vec (expr_list);
3644 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3645 release_tree_vector (expr_list);
3648 else
3650 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3651 attr_args = NULL_TREE;
3652 else
3654 expr_list = c_parser_expr_list (parser, false, true,
3655 NULL, NULL, NULL);
3656 attr_args = build_tree_list_vec (expr_list);
3657 release_tree_vector (expr_list);
3660 attr = build_tree_list (attr_name, attr_args);
3661 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3662 c_parser_consume_token (parser);
3663 else
3665 parser->lex_untranslated_string = false;
3666 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3667 "expected %<)%>");
3668 return attrs;
3670 attrs = chainon (attrs, attr);
3672 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3673 c_parser_consume_token (parser);
3674 else
3676 parser->lex_untranslated_string = false;
3677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3678 "expected %<)%>");
3679 return attrs;
3681 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3682 c_parser_consume_token (parser);
3683 else
3685 parser->lex_untranslated_string = false;
3686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3687 "expected %<)%>");
3688 return attrs;
3690 parser->lex_untranslated_string = false;
3692 return attrs;
3695 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3697 type-name:
3698 specifier-qualifier-list abstract-declarator[opt]
3701 static struct c_type_name *
3702 c_parser_type_name (c_parser *parser)
3704 struct c_declspecs *specs = build_null_declspecs ();
3705 struct c_declarator *declarator;
3706 struct c_type_name *ret;
3707 bool dummy = false;
3708 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3709 if (!specs->declspecs_seen_p)
3711 c_parser_error (parser, "expected specifier-qualifier-list");
3712 return NULL;
3714 if (specs->type != error_mark_node)
3716 pending_xref_error ();
3717 finish_declspecs (specs);
3719 declarator = c_parser_declarator (parser,
3720 specs->typespec_kind != ctsk_none,
3721 C_DTR_ABSTRACT, &dummy);
3722 if (declarator == NULL)
3723 return NULL;
3724 ret = XOBNEW (&parser_obstack, struct c_type_name);
3725 ret->specs = specs;
3726 ret->declarator = declarator;
3727 return ret;
3730 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3732 initializer:
3733 assignment-expression
3734 { initializer-list }
3735 { initializer-list , }
3737 initializer-list:
3738 designation[opt] initializer
3739 initializer-list , designation[opt] initializer
3741 designation:
3742 designator-list =
3744 designator-list:
3745 designator
3746 designator-list designator
3748 designator:
3749 array-designator
3750 . identifier
3752 array-designator:
3753 [ constant-expression ]
3755 GNU extensions:
3757 initializer:
3760 designation:
3761 array-designator
3762 identifier :
3764 array-designator:
3765 [ constant-expression ... constant-expression ]
3767 Any expression without commas is accepted in the syntax for the
3768 constant-expressions, with non-constant expressions rejected later.
3770 This function is only used for top-level initializers; for nested
3771 ones, see c_parser_initval. */
3773 static struct c_expr
3774 c_parser_initializer (c_parser *parser)
3776 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3777 return c_parser_braced_init (parser, NULL_TREE, false);
3778 else
3780 struct c_expr ret;
3781 location_t loc = c_parser_peek_token (parser)->location;
3782 ret = c_parser_expr_no_commas (parser, NULL);
3783 if (TREE_CODE (ret.value) != STRING_CST
3784 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3785 ret = default_function_array_read_conversion (loc, ret);
3786 return ret;
3790 /* Parse a braced initializer list. TYPE is the type specified for a
3791 compound literal, and NULL_TREE for other initializers and for
3792 nested braced lists. NESTED_P is true for nested braced lists,
3793 false for the list of a compound literal or the list that is the
3794 top-level initializer in a declaration. */
3796 static struct c_expr
3797 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3799 struct c_expr ret;
3800 struct obstack braced_init_obstack;
3801 location_t brace_loc = c_parser_peek_token (parser)->location;
3802 gcc_obstack_init (&braced_init_obstack);
3803 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3804 c_parser_consume_token (parser);
3805 if (nested_p)
3806 push_init_level (0, &braced_init_obstack);
3807 else
3808 really_start_incremental_init (type);
3809 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3811 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
3813 else
3815 /* Parse a non-empty initializer list, possibly with a trailing
3816 comma. */
3817 while (true)
3819 c_parser_initelt (parser, &braced_init_obstack);
3820 if (parser->error)
3821 break;
3822 if (c_parser_next_token_is (parser, CPP_COMMA))
3823 c_parser_consume_token (parser);
3824 else
3825 break;
3826 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3827 break;
3830 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3832 ret.value = error_mark_node;
3833 ret.original_code = ERROR_MARK;
3834 ret.original_type = NULL;
3835 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3836 pop_init_level (0, &braced_init_obstack);
3837 obstack_free (&braced_init_obstack, NULL);
3838 return ret;
3840 c_parser_consume_token (parser);
3841 ret = pop_init_level (0, &braced_init_obstack);
3842 obstack_free (&braced_init_obstack, NULL);
3843 return ret;
3846 /* Parse a nested initializer, including designators. */
3848 static void
3849 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3851 /* Parse any designator or designator list. A single array
3852 designator may have the subsequent "=" omitted in GNU C, but a
3853 longer list or a structure member designator may not. */
3854 if (c_parser_next_token_is (parser, CPP_NAME)
3855 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3857 /* Old-style structure member designator. */
3858 set_init_label (c_parser_peek_token (parser)->value,
3859 braced_init_obstack);
3860 /* Use the colon as the error location. */
3861 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
3862 "obsolete use of designated initializer with %<:%>");
3863 c_parser_consume_token (parser);
3864 c_parser_consume_token (parser);
3866 else
3868 /* des_seen is 0 if there have been no designators, 1 if there
3869 has been a single array designator and 2 otherwise. */
3870 int des_seen = 0;
3871 /* Location of a designator. */
3872 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3873 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3874 || c_parser_next_token_is (parser, CPP_DOT))
3876 int des_prev = des_seen;
3877 if (!des_seen)
3878 des_loc = c_parser_peek_token (parser)->location;
3879 if (des_seen < 2)
3880 des_seen++;
3881 if (c_parser_next_token_is (parser, CPP_DOT))
3883 des_seen = 2;
3884 c_parser_consume_token (parser);
3885 if (c_parser_next_token_is (parser, CPP_NAME))
3887 set_init_label (c_parser_peek_token (parser)->value,
3888 braced_init_obstack);
3889 c_parser_consume_token (parser);
3891 else
3893 struct c_expr init;
3894 init.value = error_mark_node;
3895 init.original_code = ERROR_MARK;
3896 init.original_type = NULL;
3897 c_parser_error (parser, "expected identifier");
3898 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3899 process_init_element (init, false, braced_init_obstack);
3900 return;
3903 else
3905 tree first, second;
3906 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3907 /* ??? Following the old parser, [ objc-receiver
3908 objc-message-args ] is accepted as an initializer,
3909 being distinguished from a designator by what follows
3910 the first assignment expression inside the square
3911 brackets, but after a first array designator a
3912 subsequent square bracket is for Objective-C taken to
3913 start an expression, using the obsolete form of
3914 designated initializer without '=', rather than
3915 possibly being a second level of designation: in LALR
3916 terms, the '[' is shifted rather than reducing
3917 designator to designator-list. */
3918 if (des_prev == 1 && c_dialect_objc ())
3920 des_seen = des_prev;
3921 break;
3923 if (des_prev == 0 && c_dialect_objc ())
3925 /* This might be an array designator or an
3926 Objective-C message expression. If the former,
3927 continue parsing here; if the latter, parse the
3928 remainder of the initializer given the starting
3929 primary-expression. ??? It might make sense to
3930 distinguish when des_prev == 1 as well; see
3931 previous comment. */
3932 tree rec, args;
3933 struct c_expr mexpr;
3934 c_parser_consume_token (parser);
3935 if (c_parser_peek_token (parser)->type == CPP_NAME
3936 && ((c_parser_peek_token (parser)->id_kind
3937 == C_ID_TYPENAME)
3938 || (c_parser_peek_token (parser)->id_kind
3939 == C_ID_CLASSNAME)))
3941 /* Type name receiver. */
3942 tree id = c_parser_peek_token (parser)->value;
3943 c_parser_consume_token (parser);
3944 rec = objc_get_class_reference (id);
3945 goto parse_message_args;
3947 first = c_parser_expr_no_commas (parser, NULL).value;
3948 mark_exp_read (first);
3949 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3950 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3951 goto array_desig_after_first;
3952 /* Expression receiver. So far only one part
3953 without commas has been parsed; there might be
3954 more of the expression. */
3955 rec = first;
3956 while (c_parser_next_token_is (parser, CPP_COMMA))
3958 struct c_expr next;
3959 location_t comma_loc, exp_loc;
3960 comma_loc = c_parser_peek_token (parser)->location;
3961 c_parser_consume_token (parser);
3962 exp_loc = c_parser_peek_token (parser)->location;
3963 next = c_parser_expr_no_commas (parser, NULL);
3964 next = default_function_array_read_conversion (exp_loc,
3965 next);
3966 rec = build_compound_expr (comma_loc, rec, next.value);
3968 parse_message_args:
3969 /* Now parse the objc-message-args. */
3970 args = c_parser_objc_message_args (parser);
3971 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3972 "expected %<]%>");
3973 mexpr.value
3974 = objc_build_message_expr (rec, args);
3975 mexpr.original_code = ERROR_MARK;
3976 mexpr.original_type = NULL;
3977 /* Now parse and process the remainder of the
3978 initializer, starting with this message
3979 expression as a primary-expression. */
3980 c_parser_initval (parser, &mexpr, braced_init_obstack);
3981 return;
3983 c_parser_consume_token (parser);
3984 first = c_parser_expr_no_commas (parser, NULL).value;
3985 mark_exp_read (first);
3986 array_desig_after_first:
3987 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3989 ellipsis_loc = c_parser_peek_token (parser)->location;
3990 c_parser_consume_token (parser);
3991 second = c_parser_expr_no_commas (parser, NULL).value;
3992 mark_exp_read (second);
3994 else
3995 second = NULL_TREE;
3996 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3998 c_parser_consume_token (parser);
3999 set_init_index (first, second, braced_init_obstack);
4000 if (second)
4001 pedwarn (ellipsis_loc, OPT_Wpedantic,
4002 "ISO C forbids specifying range of elements to initialize");
4004 else
4005 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4006 "expected %<]%>");
4009 if (des_seen >= 1)
4011 if (c_parser_next_token_is (parser, CPP_EQ))
4013 if (!flag_isoc99)
4014 pedwarn (des_loc, OPT_Wpedantic,
4015 "ISO C90 forbids specifying subobject to initialize");
4016 c_parser_consume_token (parser);
4018 else
4020 if (des_seen == 1)
4021 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4022 "obsolete use of designated initializer without %<=%>");
4023 else
4025 struct c_expr init;
4026 init.value = error_mark_node;
4027 init.original_code = ERROR_MARK;
4028 init.original_type = NULL;
4029 c_parser_error (parser, "expected %<=%>");
4030 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4031 process_init_element (init, false, braced_init_obstack);
4032 return;
4037 c_parser_initval (parser, NULL, braced_init_obstack);
4040 /* Parse a nested initializer; as c_parser_initializer but parses
4041 initializers within braced lists, after any designators have been
4042 applied. If AFTER is not NULL then it is an Objective-C message
4043 expression which is the primary-expression starting the
4044 initializer. */
4046 static void
4047 c_parser_initval (c_parser *parser, struct c_expr *after,
4048 struct obstack * braced_init_obstack)
4050 struct c_expr init;
4051 gcc_assert (!after || c_dialect_objc ());
4052 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4053 init = c_parser_braced_init (parser, NULL_TREE, true);
4054 else
4056 location_t loc = c_parser_peek_token (parser)->location;
4057 init = c_parser_expr_no_commas (parser, after);
4058 if (init.value != NULL_TREE
4059 && TREE_CODE (init.value) != STRING_CST
4060 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4061 init = default_function_array_read_conversion (loc, init);
4063 process_init_element (init, false, braced_init_obstack);
4066 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4067 C99 6.8.2).
4069 compound-statement:
4070 { block-item-list[opt] }
4071 { label-declarations block-item-list }
4073 block-item-list:
4074 block-item
4075 block-item-list block-item
4077 block-item:
4078 nested-declaration
4079 statement
4081 nested-declaration:
4082 declaration
4084 GNU extensions:
4086 compound-statement:
4087 { label-declarations block-item-list }
4089 nested-declaration:
4090 __extension__ nested-declaration
4091 nested-function-definition
4093 label-declarations:
4094 label-declaration
4095 label-declarations label-declaration
4097 label-declaration:
4098 __label__ identifier-list ;
4100 Allowing the mixing of declarations and code is new in C99. The
4101 GNU syntax also permits (not shown above) labels at the end of
4102 compound statements, which yield an error. We don't allow labels
4103 on declarations; this might seem like a natural extension, but
4104 there would be a conflict between attributes on the label and
4105 prefix attributes on the declaration. ??? The syntax follows the
4106 old parser in requiring something after label declarations.
4107 Although they are erroneous if the labels declared aren't defined,
4108 is it useful for the syntax to be this way?
4110 OpenMP:
4112 block-item:
4113 openmp-directive
4115 openmp-directive:
4116 barrier-directive
4117 flush-directive */
4119 static tree
4120 c_parser_compound_statement (c_parser *parser)
4122 tree stmt;
4123 location_t brace_loc;
4124 brace_loc = c_parser_peek_token (parser)->location;
4125 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4127 /* Ensure a scope is entered and left anyway to avoid confusion
4128 if we have just prepared to enter a function body. */
4129 stmt = c_begin_compound_stmt (true);
4130 c_end_compound_stmt (brace_loc, stmt, true);
4131 return error_mark_node;
4133 stmt = c_begin_compound_stmt (true);
4134 c_parser_compound_statement_nostart (parser);
4136 /* If the compound stmt contains array notations, then we expand them. */
4137 if (flag_enable_cilkplus && contains_array_notation_expr (stmt))
4138 stmt = expand_array_notation_exprs (stmt);
4139 return c_end_compound_stmt (brace_loc, stmt, true);
4142 /* Parse a compound statement except for the opening brace. This is
4143 used for parsing both compound statements and statement expressions
4144 (which follow different paths to handling the opening). */
4146 static void
4147 c_parser_compound_statement_nostart (c_parser *parser)
4149 bool last_stmt = false;
4150 bool last_label = false;
4151 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4152 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4153 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4155 c_parser_consume_token (parser);
4156 return;
4158 mark_valid_location_for_stdc_pragma (true);
4159 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4161 /* Read zero or more forward-declarations for labels that nested
4162 functions can jump to. */
4163 mark_valid_location_for_stdc_pragma (false);
4164 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4166 label_loc = c_parser_peek_token (parser)->location;
4167 c_parser_consume_token (parser);
4168 /* Any identifiers, including those declared as type names,
4169 are OK here. */
4170 while (true)
4172 tree label;
4173 if (c_parser_next_token_is_not (parser, CPP_NAME))
4175 c_parser_error (parser, "expected identifier");
4176 break;
4178 label
4179 = declare_label (c_parser_peek_token (parser)->value);
4180 C_DECLARED_LABEL_FLAG (label) = 1;
4181 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4182 c_parser_consume_token (parser);
4183 if (c_parser_next_token_is (parser, CPP_COMMA))
4184 c_parser_consume_token (parser);
4185 else
4186 break;
4188 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4190 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4192 /* We must now have at least one statement, label or declaration. */
4193 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4195 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4196 c_parser_error (parser, "expected declaration or statement");
4197 c_parser_consume_token (parser);
4198 return;
4200 /* Process all #pragma's just after the opening brace. This
4201 handles #pragma upc, which can only appear just after
4202 the opening brace, when it appears within a function body. */
4203 push_upc_consistency_mode ();
4204 permit_pragma_upc ();
4205 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4207 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4208 if (c_parser_pragma (parser, pragma_compound))
4209 last_label = false, last_stmt = true;
4210 parser->error = false;
4212 deny_pragma_upc ();
4213 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4215 location_t loc = c_parser_peek_token (parser)->location;
4216 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4217 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4218 || (c_parser_next_token_is (parser, CPP_NAME)
4219 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4221 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4222 label_loc = c_parser_peek_2nd_token (parser)->location;
4223 else
4224 label_loc = c_parser_peek_token (parser)->location;
4225 last_label = true;
4226 last_stmt = false;
4227 mark_valid_location_for_stdc_pragma (false);
4228 c_parser_label (parser);
4230 else if (!last_label
4231 && c_parser_next_tokens_start_declaration (parser))
4233 last_label = false;
4234 mark_valid_location_for_stdc_pragma (false);
4235 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4236 if (last_stmt)
4237 pedwarn_c90 (loc,
4238 (pedantic && !flag_isoc99)
4239 ? OPT_Wpedantic
4240 : OPT_Wdeclaration_after_statement,
4241 "ISO C90 forbids mixed declarations and code");
4242 last_stmt = false;
4244 else if (!last_label
4245 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4247 /* __extension__ can start a declaration, but is also an
4248 unary operator that can start an expression. Consume all
4249 but the last of a possible series of __extension__ to
4250 determine which. */
4251 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4252 && (c_parser_peek_2nd_token (parser)->keyword
4253 == RID_EXTENSION))
4254 c_parser_consume_token (parser);
4255 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4257 int ext;
4258 ext = disable_extension_diagnostics ();
4259 c_parser_consume_token (parser);
4260 last_label = false;
4261 mark_valid_location_for_stdc_pragma (false);
4262 c_parser_declaration_or_fndef (parser, true, true, true, true,
4263 true, NULL);
4264 /* Following the old parser, __extension__ does not
4265 disable this diagnostic. */
4266 restore_extension_diagnostics (ext);
4267 if (last_stmt)
4268 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4269 ? OPT_Wpedantic
4270 : OPT_Wdeclaration_after_statement,
4271 "ISO C90 forbids mixed declarations and code");
4272 last_stmt = false;
4274 else
4275 goto statement;
4277 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4279 /* External pragmas, and some omp pragmas, are not associated
4280 with regular c code, and so are not to be considered statements
4281 syntactically. This ensures that the user doesn't put them
4282 places that would turn into syntax errors if the directive
4283 were ignored. */
4284 if (c_parser_pragma (parser, pragma_compound))
4285 last_label = false, last_stmt = true;
4287 else if (c_parser_next_token_is (parser, CPP_EOF))
4289 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4290 c_parser_error (parser, "expected declaration or statement");
4291 return;
4293 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4295 if (parser->in_if_block)
4297 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4298 error_at (loc, """expected %<}%> before %<else%>");
4299 return;
4301 else
4303 error_at (loc, "%<else%> without a previous %<if%>");
4304 c_parser_consume_token (parser);
4305 continue;
4308 else
4310 statement:
4311 last_label = false;
4312 last_stmt = true;
4313 mark_valid_location_for_stdc_pragma (false);
4314 c_parser_statement_after_labels (parser);
4317 parser->error = false;
4319 if (last_label)
4320 error_at (label_loc, "label at end of compound statement");
4321 c_parser_consume_token (parser);
4322 pop_upc_consistency_mode ();
4323 /* Restore the value we started with. */
4324 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4327 /* Parse a label (C90 6.6.1, C99 6.8.1).
4329 label:
4330 identifier : attributes[opt]
4331 case constant-expression :
4332 default :
4334 GNU extensions:
4336 label:
4337 case constant-expression ... constant-expression :
4339 The use of attributes on labels is a GNU extension. The syntax in
4340 GNU C accepts any expressions without commas, non-constant
4341 expressions being rejected later. */
4343 static void
4344 c_parser_label (c_parser *parser)
4346 location_t loc1 = c_parser_peek_token (parser)->location;
4347 tree label = NULL_TREE;
4348 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4350 tree exp1, exp2;
4351 c_parser_consume_token (parser);
4352 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4353 if (c_parser_next_token_is (parser, CPP_COLON))
4355 c_parser_consume_token (parser);
4356 label = do_case (loc1, exp1, NULL_TREE);
4358 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4360 c_parser_consume_token (parser);
4361 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4362 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4363 label = do_case (loc1, exp1, exp2);
4365 else
4366 c_parser_error (parser, "expected %<:%> or %<...%>");
4368 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4370 c_parser_consume_token (parser);
4371 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4372 label = do_case (loc1, NULL_TREE, NULL_TREE);
4374 else
4376 tree name = c_parser_peek_token (parser)->value;
4377 tree tlab;
4378 tree attrs;
4379 location_t loc2 = c_parser_peek_token (parser)->location;
4380 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4381 c_parser_consume_token (parser);
4382 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4383 c_parser_consume_token (parser);
4384 attrs = c_parser_attributes (parser);
4385 tlab = define_label (loc2, name);
4386 if (tlab)
4388 decl_attributes (&tlab, attrs, 0);
4389 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4392 if (label)
4394 if (c_parser_next_tokens_start_declaration (parser))
4396 error_at (c_parser_peek_token (parser)->location,
4397 "a label can only be part of a statement and "
4398 "a declaration is not a statement");
4399 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4400 /*static_assert_ok*/ true,
4401 /*empty_ok*/ true, /*nested*/ true,
4402 /*start_attr_ok*/ true, NULL);
4407 /* Parse a statement (C90 6.6, C99 6.8).
4409 statement:
4410 labeled-statement
4411 compound-statement
4412 expression-statement
4413 selection-statement
4414 iteration-statement
4415 jump-statement
4417 labeled-statement:
4418 label statement
4420 expression-statement:
4421 expression[opt] ;
4423 selection-statement:
4424 if-statement
4425 switch-statement
4427 iteration-statement:
4428 while-statement
4429 do-statement
4430 for-statement
4432 jump-statement:
4433 goto identifier ;
4434 continue ;
4435 break ;
4436 return expression[opt] ;
4438 GNU extensions:
4440 statement:
4441 asm-statement
4443 jump-statement:
4444 goto * expression ;
4446 Objective-C:
4448 statement:
4449 objc-throw-statement
4450 objc-try-catch-statement
4451 objc-synchronized-statement
4453 objc-throw-statement:
4454 @throw expression ;
4455 @throw ;
4457 OpenMP:
4459 statement:
4460 openmp-construct
4462 openmp-construct:
4463 parallel-construct
4464 for-construct
4465 sections-construct
4466 single-construct
4467 parallel-for-construct
4468 parallel-sections-construct
4469 master-construct
4470 critical-construct
4471 atomic-construct
4472 ordered-construct
4474 parallel-construct:
4475 parallel-directive structured-block
4477 for-construct:
4478 for-directive iteration-statement
4480 sections-construct:
4481 sections-directive section-scope
4483 single-construct:
4484 single-directive structured-block
4486 parallel-for-construct:
4487 parallel-for-directive iteration-statement
4489 parallel-sections-construct:
4490 parallel-sections-directive section-scope
4492 master-construct:
4493 master-directive structured-block
4495 critical-construct:
4496 critical-directive structured-block
4498 atomic-construct:
4499 atomic-directive expression-statement
4501 ordered-construct:
4502 ordered-directive structured-block
4504 Transactional Memory:
4506 statement:
4507 transaction-statement
4508 transaction-cancel-statement
4511 static void
4512 c_parser_statement (c_parser *parser)
4514 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4515 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4516 || (c_parser_next_token_is (parser, CPP_NAME)
4517 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4518 c_parser_label (parser);
4519 c_parser_statement_after_labels (parser);
4522 /* Parse a statement, other than a labeled statement. */
4524 static void
4525 c_parser_statement_after_labels (c_parser *parser)
4527 location_t loc = c_parser_peek_token (parser)->location;
4528 tree stmt = NULL_TREE;
4529 bool in_if_block = parser->in_if_block;
4530 parser->in_if_block = false;
4531 switch (c_parser_peek_token (parser)->type)
4533 case CPP_OPEN_BRACE:
4534 add_stmt (c_parser_compound_statement (parser));
4535 break;
4536 case CPP_KEYWORD:
4537 switch (c_parser_peek_token (parser)->keyword)
4539 case RID_IF:
4540 c_parser_if_statement (parser);
4541 break;
4542 case RID_SWITCH:
4543 c_parser_switch_statement (parser);
4544 break;
4545 case RID_WHILE:
4546 c_parser_while_statement (parser);
4547 break;
4548 case RID_DO:
4549 c_parser_do_statement (parser);
4550 break;
4551 case RID_FOR:
4552 c_parser_for_statement (parser);
4553 break;
4554 case RID_GOTO:
4555 c_parser_consume_token (parser);
4556 if (c_parser_next_token_is (parser, CPP_NAME))
4558 stmt = c_finish_goto_label (loc,
4559 c_parser_peek_token (parser)->value);
4560 c_parser_consume_token (parser);
4562 else if (c_parser_next_token_is (parser, CPP_MULT))
4564 tree val;
4566 c_parser_consume_token (parser);
4567 val = c_parser_expression (parser).value;
4568 mark_exp_read (val);
4569 stmt = c_finish_goto_ptr (loc, val);
4571 else
4572 c_parser_error (parser, "expected identifier or %<*%>");
4573 goto expect_semicolon;
4574 case RID_CONTINUE:
4575 c_parser_consume_token (parser);
4576 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4577 goto expect_semicolon;
4578 case RID_BREAK:
4579 c_parser_consume_token (parser);
4580 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4581 goto expect_semicolon;
4582 case RID_RETURN:
4583 c_parser_consume_token (parser);
4584 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4586 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4587 c_parser_consume_token (parser);
4589 else
4591 struct c_expr expr = c_parser_expression_conv (parser);
4592 mark_exp_read (expr.value);
4593 stmt = c_finish_return (loc, expr.value, expr.original_type);
4594 goto expect_semicolon;
4596 break;
4597 case RID_ASM:
4598 stmt = c_parser_asm_statement (parser);
4599 break;
4600 case RID_TRANSACTION_ATOMIC:
4601 case RID_TRANSACTION_RELAXED:
4602 stmt = c_parser_transaction (parser,
4603 c_parser_peek_token (parser)->keyword);
4604 break;
4605 case RID_TRANSACTION_CANCEL:
4606 stmt = c_parser_transaction_cancel (parser);
4607 goto expect_semicolon;
4608 case RID_AT_THROW:
4609 gcc_assert (c_dialect_objc ());
4610 c_parser_consume_token (parser);
4611 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4613 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4614 c_parser_consume_token (parser);
4616 else
4618 tree expr = c_parser_expression (parser).value;
4619 expr = c_fully_fold (expr, false, NULL);
4620 stmt = objc_build_throw_stmt (loc, expr);
4621 goto expect_semicolon;
4623 break;
4624 case RID_AT_TRY:
4625 gcc_assert (c_dialect_objc ());
4626 c_parser_objc_try_catch_finally_statement (parser);
4627 break;
4628 case RID_AT_SYNCHRONIZED:
4629 gcc_assert (c_dialect_objc ());
4630 c_parser_objc_synchronized_statement (parser);
4631 break;
4632 case RID_UPC_FORALL:
4633 gcc_assert (c_dialect_upc ());
4634 c_parser_upc_forall_statement (parser);
4635 break;
4636 case RID_UPC_NOTIFY:
4637 gcc_assert (c_dialect_upc ());
4638 c_parser_upc_sync_statement (parser, UPC_SYNC_NOTIFY_OP);
4639 goto expect_semicolon;
4640 case RID_UPC_WAIT:
4641 gcc_assert (c_dialect_upc ());
4642 c_parser_upc_sync_statement (parser, UPC_SYNC_WAIT_OP);
4643 goto expect_semicolon;
4644 case RID_UPC_BARRIER:
4645 gcc_assert (c_dialect_upc ());
4646 c_parser_upc_sync_statement (parser, UPC_SYNC_BARRIER_OP);
4647 goto expect_semicolon;
4648 default:
4649 goto expr_stmt;
4651 break;
4652 case CPP_SEMICOLON:
4653 c_parser_consume_token (parser);
4654 break;
4655 case CPP_CLOSE_PAREN:
4656 case CPP_CLOSE_SQUARE:
4657 /* Avoid infinite loop in error recovery:
4658 c_parser_skip_until_found stops at a closing nesting
4659 delimiter without consuming it, but here we need to consume
4660 it to proceed further. */
4661 c_parser_error (parser, "expected statement");
4662 c_parser_consume_token (parser);
4663 break;
4664 case CPP_PRAGMA:
4665 c_parser_pragma (parser, pragma_stmt);
4666 break;
4667 default:
4668 expr_stmt:
4669 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4670 expect_semicolon:
4671 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4672 break;
4674 /* Two cases cannot and do not have line numbers associated: If stmt
4675 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4676 cannot hold line numbers. But that's OK because the statement
4677 will either be changed to a MODIFY_EXPR during gimplification of
4678 the statement expr, or discarded. If stmt was compound, but
4679 without new variables, we will have skipped the creation of a
4680 BIND and will have a bare STATEMENT_LIST. But that's OK because
4681 (recursively) all of the component statements should already have
4682 line numbers assigned. ??? Can we discard no-op statements
4683 earlier? */
4684 if (CAN_HAVE_LOCATION_P (stmt)
4685 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4686 SET_EXPR_LOCATION (stmt, loc);
4688 parser->in_if_block = in_if_block;
4691 /* Parse the condition from an if, do, while or for statements. */
4693 static tree
4694 c_parser_condition (c_parser *parser)
4696 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4697 tree cond;
4698 cond = c_parser_expression_conv (parser).value;
4699 cond = c_objc_common_truthvalue_conversion (loc, cond);
4700 cond = c_fully_fold (cond, false, NULL);
4701 if (warn_sequence_point)
4702 verify_sequence_points (cond);
4703 return cond;
4706 /* Parse a parenthesized condition from an if, do or while statement.
4708 condition:
4709 ( expression )
4711 static tree
4712 c_parser_paren_condition (c_parser *parser)
4714 tree cond;
4715 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4716 return error_mark_node;
4717 cond = c_parser_condition (parser);
4718 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4719 return cond;
4722 /* Parse a statement which is a block in C99. */
4724 static tree
4725 c_parser_c99_block_statement (c_parser *parser)
4727 tree block = c_begin_compound_stmt (flag_isoc99);
4728 location_t loc = c_parser_peek_token (parser)->location;
4729 c_parser_statement (parser);
4730 return c_end_compound_stmt (loc, block, flag_isoc99);
4733 /* Parse the body of an if statement. This is just parsing a
4734 statement but (a) it is a block in C99, (b) we track whether the
4735 body is an if statement for the sake of -Wparentheses warnings, (c)
4736 we handle an empty body specially for the sake of -Wempty-body
4737 warnings, and (d) we call parser_compound_statement directly
4738 because c_parser_statement_after_labels resets
4739 parser->in_if_block. */
4741 static tree
4742 c_parser_if_body (c_parser *parser, bool *if_p)
4744 tree block = c_begin_compound_stmt (flag_isoc99);
4745 location_t body_loc = c_parser_peek_token (parser)->location;
4746 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4747 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4748 || (c_parser_next_token_is (parser, CPP_NAME)
4749 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4750 c_parser_label (parser);
4751 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4752 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4754 location_t loc = c_parser_peek_token (parser)->location;
4755 add_stmt (build_empty_stmt (loc));
4756 c_parser_consume_token (parser);
4757 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4758 warning_at (loc, OPT_Wempty_body,
4759 "suggest braces around empty body in an %<if%> statement");
4761 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4762 add_stmt (c_parser_compound_statement (parser));
4763 else
4764 c_parser_statement_after_labels (parser);
4765 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4768 /* Parse the else body of an if statement. This is just parsing a
4769 statement but (a) it is a block in C99, (b) we handle an empty body
4770 specially for the sake of -Wempty-body warnings. */
4772 static tree
4773 c_parser_else_body (c_parser *parser)
4775 location_t else_loc = c_parser_peek_token (parser)->location;
4776 tree block = c_begin_compound_stmt (flag_isoc99);
4777 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4778 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4779 || (c_parser_next_token_is (parser, CPP_NAME)
4780 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4781 c_parser_label (parser);
4782 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4784 location_t loc = c_parser_peek_token (parser)->location;
4785 warning_at (loc,
4786 OPT_Wempty_body,
4787 "suggest braces around empty body in an %<else%> statement");
4788 add_stmt (build_empty_stmt (loc));
4789 c_parser_consume_token (parser);
4791 else
4792 c_parser_statement_after_labels (parser);
4793 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4796 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4798 if-statement:
4799 if ( expression ) statement
4800 if ( expression ) statement else statement
4803 static void
4804 c_parser_if_statement (c_parser *parser)
4806 tree block;
4807 location_t loc;
4808 tree cond;
4809 bool first_if = false;
4810 tree first_body, second_body;
4811 bool in_if_block;
4812 tree if_stmt;
4814 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4815 c_parser_consume_token (parser);
4816 block = c_begin_compound_stmt (flag_isoc99);
4817 loc = c_parser_peek_token (parser)->location;
4818 cond = c_parser_paren_condition (parser);
4819 in_if_block = parser->in_if_block;
4820 parser->in_if_block = true;
4821 first_body = c_parser_if_body (parser, &first_if);
4822 parser->in_if_block = in_if_block;
4823 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4825 c_parser_consume_token (parser);
4826 second_body = c_parser_else_body (parser);
4828 else
4829 second_body = NULL_TREE;
4830 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4831 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
4833 /* If the if statement contains array notations, then we expand them. */
4834 if (flag_enable_cilkplus && contains_array_notation_expr (if_stmt))
4835 if_stmt = fix_conditional_array_notations (if_stmt);
4836 add_stmt (if_stmt);
4839 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4841 switch-statement:
4842 switch (expression) statement
4845 static void
4846 c_parser_switch_statement (c_parser *parser)
4848 tree block, expr, body, save_break;
4849 location_t switch_loc = c_parser_peek_token (parser)->location;
4850 location_t switch_cond_loc;
4851 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4852 c_parser_consume_token (parser);
4853 block = c_begin_compound_stmt (flag_isoc99);
4854 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4856 switch_cond_loc = c_parser_peek_token (parser)->location;
4857 expr = c_parser_expression (parser).value;
4858 if (flag_enable_cilkplus && contains_array_notation_expr (expr))
4860 error_at (switch_cond_loc,
4861 "array notations cannot be used as a condition for switch "
4862 "statement");
4863 expr = error_mark_node;
4865 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4867 else
4869 switch_cond_loc = UNKNOWN_LOCATION;
4870 expr = error_mark_node;
4872 c_start_case (switch_loc, switch_cond_loc, expr);
4873 save_break = c_break_label;
4874 c_break_label = NULL_TREE;
4875 body = c_parser_c99_block_statement (parser);
4876 c_finish_case (body);
4877 if (c_break_label)
4879 location_t here = c_parser_peek_token (parser)->location;
4880 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4881 SET_EXPR_LOCATION (t, here);
4882 add_stmt (t);
4884 c_break_label = save_break;
4885 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4888 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4890 while-statement:
4891 while (expression) statement
4894 static void
4895 c_parser_while_statement (c_parser *parser)
4897 tree block, cond, body, save_break, save_cont;
4898 location_t loc;
4899 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4900 c_parser_consume_token (parser);
4901 block = c_begin_compound_stmt (flag_isoc99);
4902 loc = c_parser_peek_token (parser)->location;
4903 cond = c_parser_paren_condition (parser);
4904 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
4906 error_at (loc, "array notations cannot be used as a condition for while "
4907 "statement");
4908 cond = error_mark_node;
4910 save_break = c_break_label;
4911 c_break_label = NULL_TREE;
4912 save_cont = c_cont_label;
4913 c_cont_label = NULL_TREE;
4914 body = c_parser_c99_block_statement (parser);
4915 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4916 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4917 c_break_label = save_break;
4918 c_cont_label = save_cont;
4921 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4923 do-statement:
4924 do statement while ( expression ) ;
4927 static void
4928 c_parser_do_statement (c_parser *parser)
4930 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4931 location_t loc;
4932 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4933 c_parser_consume_token (parser);
4934 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4935 warning_at (c_parser_peek_token (parser)->location,
4936 OPT_Wempty_body,
4937 "suggest braces around empty body in %<do%> statement");
4938 block = c_begin_compound_stmt (flag_isoc99);
4939 loc = c_parser_peek_token (parser)->location;
4940 save_break = c_break_label;
4941 c_break_label = NULL_TREE;
4942 save_cont = c_cont_label;
4943 c_cont_label = NULL_TREE;
4944 body = c_parser_c99_block_statement (parser);
4945 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4946 new_break = c_break_label;
4947 c_break_label = save_break;
4948 new_cont = c_cont_label;
4949 c_cont_label = save_cont;
4950 cond = c_parser_paren_condition (parser);
4951 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
4953 error_at (loc, "array notations cannot be used as a condition for a "
4954 "do-while statement");
4955 cond = error_mark_node;
4958 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4959 c_parser_skip_to_end_of_block_or_statement (parser);
4960 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4961 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4964 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4966 for-statement:
4967 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4968 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4970 The form with a declaration is new in C99.
4972 ??? In accordance with the old parser, the declaration may be a
4973 nested function, which is then rejected in check_for_loop_decls,
4974 but does it make any sense for this to be included in the grammar?
4975 Note in particular that the nested function does not include a
4976 trailing ';', whereas the "declaration" production includes one.
4977 Also, can we reject bad declarations earlier and cheaper than
4978 check_for_loop_decls?
4980 In Objective-C, there are two additional variants:
4982 foreach-statement:
4983 for ( expression in expresssion ) statement
4984 for ( declaration in expression ) statement
4986 This is inconsistent with C, because the second variant is allowed
4987 even if c99 is not enabled.
4989 The rest of the comment documents these Objective-C foreach-statement.
4991 Here is the canonical example of the first variant:
4992 for (object in array) { do something with object }
4993 we call the first expression ("object") the "object_expression" and
4994 the second expression ("array") the "collection_expression".
4995 object_expression must be an lvalue of type "id" (a generic Objective-C
4996 object) because the loop works by assigning to object_expression the
4997 various objects from the collection_expression. collection_expression
4998 must evaluate to something of type "id" which responds to the method
4999 countByEnumeratingWithState:objects:count:.
5001 The canonical example of the second variant is:
5002 for (id object in array) { do something with object }
5003 which is completely equivalent to
5005 id object;
5006 for (object in array) { do something with object }
5008 Note that initizializing 'object' in some way (eg, "for ((object =
5009 xxx) in array) { do something with object }") is possibly
5010 technically valid, but completely pointless as 'object' will be
5011 assigned to something else as soon as the loop starts. We should
5012 most likely reject it (TODO).
5014 The beginning of the Objective-C foreach-statement looks exactly
5015 like the beginning of the for-statement, and we can tell it is a
5016 foreach-statement only because the initial declaration or
5017 expression is terminated by 'in' instead of ';'.
5020 static void
5021 c_parser_for_statement (c_parser *parser)
5023 tree block, cond, incr, save_break, save_cont, body;
5024 /* The following are only used when parsing an ObjC foreach statement. */
5025 tree object_expression;
5026 /* Silence the bogus uninitialized warning. */
5027 tree collection_expression = NULL;
5028 location_t loc = c_parser_peek_token (parser)->location;
5029 location_t for_loc = c_parser_peek_token (parser)->location;
5030 bool is_foreach_statement = false;
5031 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5032 c_parser_consume_token (parser);
5033 /* Open a compound statement in Objective-C as well, just in case this is
5034 as foreach expression. */
5035 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5036 cond = error_mark_node;
5037 incr = error_mark_node;
5038 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5040 /* Parse the initialization declaration or expression. */
5041 object_expression = error_mark_node;
5042 parser->objc_could_be_foreach_context = c_dialect_objc ();
5043 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5045 parser->objc_could_be_foreach_context = false;
5046 c_parser_consume_token (parser);
5047 c_finish_expr_stmt (loc, NULL_TREE);
5049 else if (c_parser_next_tokens_start_declaration (parser))
5051 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5052 &object_expression);
5053 parser->objc_could_be_foreach_context = false;
5055 if (c_parser_next_token_is_keyword (parser, RID_IN))
5057 c_parser_consume_token (parser);
5058 is_foreach_statement = true;
5059 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5060 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5062 else
5063 check_for_loop_decls (for_loc, flag_isoc99);
5065 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5067 /* __extension__ can start a declaration, but is also an
5068 unary operator that can start an expression. Consume all
5069 but the last of a possible series of __extension__ to
5070 determine which. */
5071 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5072 && (c_parser_peek_2nd_token (parser)->keyword
5073 == RID_EXTENSION))
5074 c_parser_consume_token (parser);
5075 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5077 int ext;
5078 ext = disable_extension_diagnostics ();
5079 c_parser_consume_token (parser);
5080 c_parser_declaration_or_fndef (parser, true, true, true, true,
5081 true, &object_expression);
5082 parser->objc_could_be_foreach_context = false;
5084 restore_extension_diagnostics (ext);
5085 if (c_parser_next_token_is_keyword (parser, RID_IN))
5087 c_parser_consume_token (parser);
5088 is_foreach_statement = true;
5089 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5090 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5092 else
5093 check_for_loop_decls (for_loc, flag_isoc99);
5095 else
5096 goto init_expr;
5098 else
5100 init_expr:
5102 tree init_expression;
5103 init_expression = c_parser_expression (parser).value;
5104 parser->objc_could_be_foreach_context = false;
5105 if (c_parser_next_token_is_keyword (parser, RID_IN))
5107 c_parser_consume_token (parser);
5108 is_foreach_statement = true;
5109 if (! lvalue_p (init_expression))
5110 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5111 object_expression = c_fully_fold (init_expression, false, NULL);
5113 else
5115 c_finish_expr_stmt (loc, init_expression);
5116 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5120 /* Parse the loop condition. In the case of a foreach
5121 statement, there is no loop condition. */
5122 gcc_assert (!parser->objc_could_be_foreach_context);
5123 if (!is_foreach_statement)
5125 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5127 c_parser_consume_token (parser);
5128 cond = NULL_TREE;
5130 else
5132 cond = c_parser_condition (parser);
5133 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
5135 error_at (loc, "array notations cannot be used in a "
5136 "condition for a for-loop");
5137 cond = error_mark_node;
5139 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5140 "expected %<;%>");
5143 /* Parse the increment expression (the third expression in a
5144 for-statement). In the case of a foreach-statement, this is
5145 the expression that follows the 'in'. */
5146 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5148 if (is_foreach_statement)
5150 c_parser_error (parser, "missing collection in fast enumeration");
5151 collection_expression = error_mark_node;
5153 else
5154 incr = c_process_expr_stmt (loc, NULL_TREE);
5156 else
5158 if (is_foreach_statement)
5159 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5160 false, NULL);
5161 else
5162 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5166 save_break = c_break_label;
5167 c_break_label = NULL_TREE;
5168 save_cont = c_cont_label;
5169 c_cont_label = NULL_TREE;
5170 body = c_parser_c99_block_statement (parser);
5171 if (is_foreach_statement)
5172 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5173 else
5174 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5175 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5176 c_break_label = save_break;
5177 c_cont_label = save_cont;
5180 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5181 statement with inputs, outputs, clobbers, and volatile tag
5182 allowed.
5184 asm-statement:
5185 asm type-qualifier[opt] ( asm-argument ) ;
5186 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5188 asm-argument:
5189 asm-string-literal
5190 asm-string-literal : asm-operands[opt]
5191 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5192 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5194 asm-goto-argument:
5195 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5196 : asm-goto-operands
5198 Qualifiers other than volatile are accepted in the syntax but
5199 warned for. */
5201 static tree
5202 c_parser_asm_statement (c_parser *parser)
5204 tree quals, str, outputs, inputs, clobbers, labels, ret;
5205 bool simple, is_goto;
5206 location_t asm_loc = c_parser_peek_token (parser)->location;
5207 int section, nsections;
5209 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5210 c_parser_consume_token (parser);
5211 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5213 quals = c_parser_peek_token (parser)->value;
5214 c_parser_consume_token (parser);
5216 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5217 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5219 warning_at (c_parser_peek_token (parser)->location,
5221 "%E qualifier ignored on asm",
5222 c_parser_peek_token (parser)->value);
5223 quals = NULL_TREE;
5224 c_parser_consume_token (parser);
5226 else
5227 quals = NULL_TREE;
5229 is_goto = false;
5230 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5232 c_parser_consume_token (parser);
5233 is_goto = true;
5236 /* ??? Follow the C++ parser rather than using the
5237 lex_untranslated_string kludge. */
5238 parser->lex_untranslated_string = true;
5239 ret = NULL;
5241 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5242 goto error;
5244 str = c_parser_asm_string_literal (parser);
5245 if (str == NULL_TREE)
5246 goto error_close_paren;
5248 simple = true;
5249 outputs = NULL_TREE;
5250 inputs = NULL_TREE;
5251 clobbers = NULL_TREE;
5252 labels = NULL_TREE;
5254 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5255 goto done_asm;
5257 /* Parse each colon-delimited section of operands. */
5258 nsections = 3 + is_goto;
5259 for (section = 0; section < nsections; ++section)
5261 if (!c_parser_require (parser, CPP_COLON,
5262 is_goto
5263 ? "expected %<:%>"
5264 : "expected %<:%> or %<)%>"))
5265 goto error_close_paren;
5267 /* Once past any colon, we're no longer a simple asm. */
5268 simple = false;
5270 if ((!c_parser_next_token_is (parser, CPP_COLON)
5271 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5272 || section == 3)
5273 switch (section)
5275 case 0:
5276 /* For asm goto, we don't allow output operands, but reserve
5277 the slot for a future extension that does allow them. */
5278 if (!is_goto)
5279 outputs = c_parser_asm_operands (parser);
5280 break;
5281 case 1:
5282 inputs = c_parser_asm_operands (parser);
5283 break;
5284 case 2:
5285 clobbers = c_parser_asm_clobbers (parser);
5286 break;
5287 case 3:
5288 labels = c_parser_asm_goto_operands (parser);
5289 break;
5290 default:
5291 gcc_unreachable ();
5294 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5295 goto done_asm;
5298 done_asm:
5299 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5302 goto error;
5305 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5306 c_parser_skip_to_end_of_block_or_statement (parser);
5308 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5309 clobbers, labels, simple));
5311 error:
5312 parser->lex_untranslated_string = false;
5313 return ret;
5315 error_close_paren:
5316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5317 goto error;
5320 /* Parse asm operands, a GNU extension.
5322 asm-operands:
5323 asm-operand
5324 asm-operands , asm-operand
5326 asm-operand:
5327 asm-string-literal ( expression )
5328 [ identifier ] asm-string-literal ( expression )
5331 static tree
5332 c_parser_asm_operands (c_parser *parser)
5334 tree list = NULL_TREE;
5335 while (true)
5337 tree name, str;
5338 struct c_expr expr;
5339 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5341 c_parser_consume_token (parser);
5342 if (c_parser_next_token_is (parser, CPP_NAME))
5344 tree id = c_parser_peek_token (parser)->value;
5345 c_parser_consume_token (parser);
5346 name = build_string (IDENTIFIER_LENGTH (id),
5347 IDENTIFIER_POINTER (id));
5349 else
5351 c_parser_error (parser, "expected identifier");
5352 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5353 return NULL_TREE;
5355 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5356 "expected %<]%>");
5358 else
5359 name = NULL_TREE;
5360 str = c_parser_asm_string_literal (parser);
5361 if (str == NULL_TREE)
5362 return NULL_TREE;
5363 parser->lex_untranslated_string = false;
5364 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5366 parser->lex_untranslated_string = true;
5367 return NULL_TREE;
5369 expr = c_parser_expression (parser);
5370 mark_exp_read (expr.value);
5371 parser->lex_untranslated_string = true;
5372 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5375 return NULL_TREE;
5377 list = chainon (list, build_tree_list (build_tree_list (name, str),
5378 expr.value));
5379 if (c_parser_next_token_is (parser, CPP_COMMA))
5380 c_parser_consume_token (parser);
5381 else
5382 break;
5384 return list;
5387 /* Parse asm clobbers, a GNU extension.
5389 asm-clobbers:
5390 asm-string-literal
5391 asm-clobbers , asm-string-literal
5394 static tree
5395 c_parser_asm_clobbers (c_parser *parser)
5397 tree list = NULL_TREE;
5398 while (true)
5400 tree str = c_parser_asm_string_literal (parser);
5401 if (str)
5402 list = tree_cons (NULL_TREE, str, list);
5403 else
5404 return NULL_TREE;
5405 if (c_parser_next_token_is (parser, CPP_COMMA))
5406 c_parser_consume_token (parser);
5407 else
5408 break;
5410 return list;
5413 /* Parse asm goto labels, a GNU extension.
5415 asm-goto-operands:
5416 identifier
5417 asm-goto-operands , identifier
5420 static tree
5421 c_parser_asm_goto_operands (c_parser *parser)
5423 tree list = NULL_TREE;
5424 while (true)
5426 tree name, label;
5428 if (c_parser_next_token_is (parser, CPP_NAME))
5430 c_token *tok = c_parser_peek_token (parser);
5431 name = tok->value;
5432 label = lookup_label_for_goto (tok->location, name);
5433 c_parser_consume_token (parser);
5434 TREE_USED (label) = 1;
5436 else
5438 c_parser_error (parser, "expected identifier");
5439 return NULL_TREE;
5442 name = build_string (IDENTIFIER_LENGTH (name),
5443 IDENTIFIER_POINTER (name));
5444 list = tree_cons (name, label, list);
5445 if (c_parser_next_token_is (parser, CPP_COMMA))
5446 c_parser_consume_token (parser);
5447 else
5448 return nreverse (list);
5452 /* Parse an expression other than a compound expression; that is, an
5453 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5454 NULL then it is an Objective-C message expression which is the
5455 primary-expression starting the expression as an initializer.
5457 assignment-expression:
5458 conditional-expression
5459 unary-expression assignment-operator assignment-expression
5461 assignment-operator: one of
5462 = *= /= %= += -= <<= >>= &= ^= |=
5464 In GNU C we accept any conditional expression on the LHS and
5465 diagnose the invalid lvalue rather than producing a syntax
5466 error. */
5468 static struct c_expr
5469 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5471 struct c_expr lhs, rhs, ret;
5472 enum tree_code code;
5473 location_t op_location, exp_location;
5474 gcc_assert (!after || c_dialect_objc ());
5475 lhs = c_parser_conditional_expression (parser, after);
5476 op_location = c_parser_peek_token (parser)->location;
5477 switch (c_parser_peek_token (parser)->type)
5479 case CPP_EQ:
5480 code = NOP_EXPR;
5481 break;
5482 case CPP_MULT_EQ:
5483 code = MULT_EXPR;
5484 break;
5485 case CPP_DIV_EQ:
5486 code = TRUNC_DIV_EXPR;
5487 break;
5488 case CPP_MOD_EQ:
5489 code = TRUNC_MOD_EXPR;
5490 break;
5491 case CPP_PLUS_EQ:
5492 code = PLUS_EXPR;
5493 break;
5494 case CPP_MINUS_EQ:
5495 code = MINUS_EXPR;
5496 break;
5497 case CPP_LSHIFT_EQ:
5498 code = LSHIFT_EXPR;
5499 break;
5500 case CPP_RSHIFT_EQ:
5501 code = RSHIFT_EXPR;
5502 break;
5503 case CPP_AND_EQ:
5504 code = BIT_AND_EXPR;
5505 break;
5506 case CPP_XOR_EQ:
5507 code = BIT_XOR_EXPR;
5508 break;
5509 case CPP_OR_EQ:
5510 code = BIT_IOR_EXPR;
5511 break;
5512 default:
5513 return lhs;
5515 c_parser_consume_token (parser);
5516 exp_location = c_parser_peek_token (parser)->location;
5517 rhs = c_parser_expr_no_commas (parser, NULL);
5518 rhs = default_function_array_read_conversion (exp_location, rhs);
5520 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5521 code, exp_location, rhs.value,
5522 rhs.original_type);
5523 if (code == NOP_EXPR)
5524 ret.original_code = MODIFY_EXPR;
5525 else
5527 TREE_NO_WARNING (ret.value) = 1;
5528 ret.original_code = ERROR_MARK;
5530 ret.original_type = NULL;
5531 return ret;
5534 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5535 is not NULL then it is an Objective-C message expression which is
5536 the primary-expression starting the expression as an initializer.
5538 conditional-expression:
5539 logical-OR-expression
5540 logical-OR-expression ? expression : conditional-expression
5542 GNU extensions:
5544 conditional-expression:
5545 logical-OR-expression ? : conditional-expression
5548 static struct c_expr
5549 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5551 struct c_expr cond, exp1, exp2, ret;
5552 location_t cond_loc, colon_loc, middle_loc;
5554 gcc_assert (!after || c_dialect_objc ());
5556 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5558 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5559 return cond;
5560 cond_loc = c_parser_peek_token (parser)->location;
5561 cond = default_function_array_read_conversion (cond_loc, cond);
5562 c_parser_consume_token (parser);
5563 if (c_parser_next_token_is (parser, CPP_COLON))
5565 tree eptype = NULL_TREE;
5567 middle_loc = c_parser_peek_token (parser)->location;
5568 pedwarn (middle_loc, OPT_Wpedantic,
5569 "ISO C forbids omitting the middle term of a ?: expression");
5570 warn_for_omitted_condop (middle_loc, cond.value);
5571 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5573 eptype = TREE_TYPE (cond.value);
5574 cond.value = TREE_OPERAND (cond.value, 0);
5576 /* Make sure first operand is calculated only once. */
5577 exp1.value = c_save_expr (default_conversion (cond.value));
5578 if (eptype)
5579 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5580 exp1.original_type = NULL;
5581 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5582 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5584 else
5586 cond.value
5587 = c_objc_common_truthvalue_conversion
5588 (cond_loc, default_conversion (cond.value));
5589 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5590 exp1 = c_parser_expression_conv (parser);
5591 mark_exp_read (exp1.value);
5592 c_inhibit_evaluation_warnings +=
5593 ((cond.value == truthvalue_true_node)
5594 - (cond.value == truthvalue_false_node));
5597 colon_loc = c_parser_peek_token (parser)->location;
5598 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5600 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5601 ret.value = error_mark_node;
5602 ret.original_code = ERROR_MARK;
5603 ret.original_type = NULL;
5604 return ret;
5607 location_t exp2_loc = c_parser_peek_token (parser)->location;
5608 exp2 = c_parser_conditional_expression (parser, NULL);
5609 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5611 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5612 ret.value = build_conditional_expr (colon_loc, cond.value,
5613 cond.original_code == C_MAYBE_CONST_EXPR,
5614 exp1.value, exp1.original_type,
5615 exp2.value, exp2.original_type);
5616 ret.original_code = ERROR_MARK;
5617 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5618 ret.original_type = NULL;
5619 else
5621 tree t1, t2;
5623 /* If both sides are enum type, the default conversion will have
5624 made the type of the result be an integer type. We want to
5625 remember the enum types we started with. */
5626 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5627 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5628 ret.original_type = ((t1 != error_mark_node
5629 && t2 != error_mark_node
5630 && (TYPE_MAIN_VARIANT (t1)
5631 == TYPE_MAIN_VARIANT (t2)))
5632 ? t1
5633 : NULL);
5635 return ret;
5638 /* Parse a binary expression; that is, a logical-OR-expression (C90
5639 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5640 an Objective-C message expression which is the primary-expression
5641 starting the expression as an initializer. PREC is the starting
5642 precedence, usually PREC_NONE.
5644 multiplicative-expression:
5645 cast-expression
5646 multiplicative-expression * cast-expression
5647 multiplicative-expression / cast-expression
5648 multiplicative-expression % cast-expression
5650 additive-expression:
5651 multiplicative-expression
5652 additive-expression + multiplicative-expression
5653 additive-expression - multiplicative-expression
5655 shift-expression:
5656 additive-expression
5657 shift-expression << additive-expression
5658 shift-expression >> additive-expression
5660 relational-expression:
5661 shift-expression
5662 relational-expression < shift-expression
5663 relational-expression > shift-expression
5664 relational-expression <= shift-expression
5665 relational-expression >= shift-expression
5667 equality-expression:
5668 relational-expression
5669 equality-expression == relational-expression
5670 equality-expression != relational-expression
5672 AND-expression:
5673 equality-expression
5674 AND-expression & equality-expression
5676 exclusive-OR-expression:
5677 AND-expression
5678 exclusive-OR-expression ^ AND-expression
5680 inclusive-OR-expression:
5681 exclusive-OR-expression
5682 inclusive-OR-expression | exclusive-OR-expression
5684 logical-AND-expression:
5685 inclusive-OR-expression
5686 logical-AND-expression && inclusive-OR-expression
5688 logical-OR-expression:
5689 logical-AND-expression
5690 logical-OR-expression || logical-AND-expression
5693 static struct c_expr
5694 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5695 enum c_parser_prec prec)
5697 /* A binary expression is parsed using operator-precedence parsing,
5698 with the operands being cast expressions. All the binary
5699 operators are left-associative. Thus a binary expression is of
5700 form:
5702 E0 op1 E1 op2 E2 ...
5704 which we represent on a stack. On the stack, the precedence
5705 levels are strictly increasing. When a new operator is
5706 encountered of higher precedence than that at the top of the
5707 stack, it is pushed; its LHS is the top expression, and its RHS
5708 is everything parsed until it is popped. When a new operator is
5709 encountered with precedence less than or equal to that at the top
5710 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5711 by the result of the operation until the operator at the top of
5712 the stack has lower precedence than the new operator or there is
5713 only one element on the stack; then the top expression is the LHS
5714 of the new operator. In the case of logical AND and OR
5715 expressions, we also need to adjust c_inhibit_evaluation_warnings
5716 as appropriate when the operators are pushed and popped. */
5718 struct {
5719 /* The expression at this stack level. */
5720 struct c_expr expr;
5721 /* The precedence of the operator on its left, PREC_NONE at the
5722 bottom of the stack. */
5723 enum c_parser_prec prec;
5724 /* The operation on its left. */
5725 enum tree_code op;
5726 /* The source location of this operation. */
5727 location_t loc;
5728 } stack[NUM_PRECS];
5729 int sp;
5730 /* Location of the binary operator. */
5731 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5732 #define POP \
5733 do { \
5734 switch (stack[sp].op) \
5736 case TRUTH_ANDIF_EXPR: \
5737 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5738 == truthvalue_false_node); \
5739 break; \
5740 case TRUTH_ORIF_EXPR: \
5741 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5742 == truthvalue_true_node); \
5743 break; \
5744 default: \
5745 break; \
5747 stack[sp - 1].expr \
5748 = default_function_array_read_conversion (stack[sp - 1].loc, \
5749 stack[sp - 1].expr); \
5750 stack[sp].expr \
5751 = default_function_array_read_conversion (stack[sp].loc, \
5752 stack[sp].expr); \
5753 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5754 stack[sp].op, \
5755 stack[sp - 1].expr, \
5756 stack[sp].expr); \
5757 sp--; \
5758 } while (0)
5759 gcc_assert (!after || c_dialect_objc ());
5760 stack[0].loc = c_parser_peek_token (parser)->location;
5761 stack[0].expr = c_parser_cast_expression (parser, after);
5762 stack[0].prec = prec;
5763 sp = 0;
5764 while (true)
5766 enum c_parser_prec oprec;
5767 enum tree_code ocode;
5768 if (parser->error)
5769 goto out;
5770 switch (c_parser_peek_token (parser)->type)
5772 case CPP_MULT:
5773 oprec = PREC_MULT;
5774 ocode = MULT_EXPR;
5775 break;
5776 case CPP_DIV:
5777 oprec = PREC_MULT;
5778 ocode = TRUNC_DIV_EXPR;
5779 break;
5780 case CPP_MOD:
5781 oprec = PREC_MULT;
5782 ocode = TRUNC_MOD_EXPR;
5783 break;
5784 case CPP_PLUS:
5785 oprec = PREC_ADD;
5786 ocode = PLUS_EXPR;
5787 break;
5788 case CPP_MINUS:
5789 oprec = PREC_ADD;
5790 ocode = MINUS_EXPR;
5791 break;
5792 case CPP_LSHIFT:
5793 oprec = PREC_SHIFT;
5794 ocode = LSHIFT_EXPR;
5795 break;
5796 case CPP_RSHIFT:
5797 oprec = PREC_SHIFT;
5798 ocode = RSHIFT_EXPR;
5799 break;
5800 case CPP_LESS:
5801 oprec = PREC_REL;
5802 ocode = LT_EXPR;
5803 break;
5804 case CPP_GREATER:
5805 oprec = PREC_REL;
5806 ocode = GT_EXPR;
5807 break;
5808 case CPP_LESS_EQ:
5809 oprec = PREC_REL;
5810 ocode = LE_EXPR;
5811 break;
5812 case CPP_GREATER_EQ:
5813 oprec = PREC_REL;
5814 ocode = GE_EXPR;
5815 break;
5816 case CPP_EQ_EQ:
5817 oprec = PREC_EQ;
5818 ocode = EQ_EXPR;
5819 break;
5820 case CPP_NOT_EQ:
5821 oprec = PREC_EQ;
5822 ocode = NE_EXPR;
5823 break;
5824 case CPP_AND:
5825 oprec = PREC_BITAND;
5826 ocode = BIT_AND_EXPR;
5827 break;
5828 case CPP_XOR:
5829 oprec = PREC_BITXOR;
5830 ocode = BIT_XOR_EXPR;
5831 break;
5832 case CPP_OR:
5833 oprec = PREC_BITOR;
5834 ocode = BIT_IOR_EXPR;
5835 break;
5836 case CPP_AND_AND:
5837 oprec = PREC_LOGAND;
5838 ocode = TRUTH_ANDIF_EXPR;
5839 break;
5840 case CPP_OR_OR:
5841 oprec = PREC_LOGOR;
5842 ocode = TRUTH_ORIF_EXPR;
5843 break;
5844 default:
5845 /* Not a binary operator, so end of the binary
5846 expression. */
5847 goto out;
5849 binary_loc = c_parser_peek_token (parser)->location;
5850 while (oprec <= stack[sp].prec)
5852 if (sp == 0)
5853 goto out;
5854 POP;
5856 c_parser_consume_token (parser);
5857 switch (ocode)
5859 case TRUTH_ANDIF_EXPR:
5860 stack[sp].expr
5861 = default_function_array_read_conversion (stack[sp].loc,
5862 stack[sp].expr);
5863 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5864 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5865 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5866 == truthvalue_false_node);
5867 break;
5868 case TRUTH_ORIF_EXPR:
5869 stack[sp].expr
5870 = default_function_array_read_conversion (stack[sp].loc,
5871 stack[sp].expr);
5872 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5873 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5874 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5875 == truthvalue_true_node);
5876 break;
5877 default:
5878 break;
5880 sp++;
5881 stack[sp].loc = binary_loc;
5882 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5883 stack[sp].prec = oprec;
5884 stack[sp].op = ocode;
5885 stack[sp].loc = binary_loc;
5887 out:
5888 while (sp > 0)
5889 POP;
5890 return stack[0].expr;
5891 #undef POP
5894 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5895 NULL then it is an Objective-C message expression which is the
5896 primary-expression starting the expression as an initializer.
5898 cast-expression:
5899 unary-expression
5900 ( type-name ) unary-expression
5903 static struct c_expr
5904 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5906 location_t cast_loc = c_parser_peek_token (parser)->location;
5907 gcc_assert (!after || c_dialect_objc ());
5908 if (after)
5909 return c_parser_postfix_expression_after_primary (parser,
5910 cast_loc, *after);
5911 /* If the expression begins with a parenthesized type name, it may
5912 be either a cast or a compound literal; we need to see whether
5913 the next character is '{' to tell the difference. If not, it is
5914 an unary expression. Full detection of unknown typenames here
5915 would require a 3-token lookahead. */
5916 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5917 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5919 struct c_type_name *type_name;
5920 struct c_expr ret;
5921 struct c_expr expr;
5922 c_parser_consume_token (parser);
5923 type_name = c_parser_type_name (parser);
5924 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5925 if (type_name == NULL)
5927 ret.value = error_mark_node;
5928 ret.original_code = ERROR_MARK;
5929 ret.original_type = NULL;
5930 return ret;
5933 /* Save casted types in the function's used types hash table. */
5934 used_types_insert (type_name->specs->type);
5936 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5937 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5938 cast_loc);
5940 location_t expr_loc = c_parser_peek_token (parser)->location;
5941 expr = c_parser_cast_expression (parser, NULL);
5942 expr = default_function_array_read_conversion (expr_loc, expr);
5944 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5945 ret.original_code = ERROR_MARK;
5946 ret.original_type = NULL;
5947 return ret;
5949 else
5950 return c_parser_unary_expression (parser);
5953 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5955 unary-expression:
5956 postfix-expression
5957 ++ unary-expression
5958 -- unary-expression
5959 unary-operator cast-expression
5960 sizeof unary-expression
5961 sizeof ( type-name )
5963 unary-operator: one of
5964 & * + - ~ !
5966 GNU extensions:
5968 unary-expression:
5969 __alignof__ unary-expression
5970 __alignof__ ( type-name )
5971 && identifier
5973 (C11 permits _Alignof with type names only.)
5975 unary-operator: one of
5976 __extension__ __real__ __imag__
5978 Transactional Memory:
5980 unary-expression:
5981 transaction-expression
5983 In addition, the GNU syntax treats ++ and -- as unary operators, so
5984 they may be applied to cast expressions with errors for non-lvalues
5985 given later. */
5987 static struct c_expr
5988 c_parser_unary_expression (c_parser *parser)
5990 int ext;
5991 struct c_expr ret, op;
5992 location_t op_loc = c_parser_peek_token (parser)->location;
5993 location_t exp_loc;
5994 ret.original_code = ERROR_MARK;
5995 ret.original_type = NULL;
5996 switch (c_parser_peek_token (parser)->type)
5998 case CPP_PLUS_PLUS:
5999 c_parser_consume_token (parser);
6000 exp_loc = c_parser_peek_token (parser)->location;
6001 op = c_parser_cast_expression (parser, NULL);
6003 /* If there is array notations in op, we expand them. */
6004 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6005 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6006 else
6008 op = default_function_array_read_conversion (exp_loc, op);
6009 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6011 case CPP_MINUS_MINUS:
6012 c_parser_consume_token (parser);
6013 exp_loc = c_parser_peek_token (parser)->location;
6014 op = c_parser_cast_expression (parser, NULL);
6016 /* If there is array notations in op, we expand them. */
6017 if (flag_enable_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6018 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6019 else
6021 op = default_function_array_read_conversion (exp_loc, op);
6022 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6024 case CPP_AND:
6025 c_parser_consume_token (parser);
6026 op = c_parser_cast_expression (parser, NULL);
6027 mark_exp_read (op.value);
6028 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6029 case CPP_MULT:
6030 c_parser_consume_token (parser);
6031 exp_loc = c_parser_peek_token (parser)->location;
6032 op = c_parser_cast_expression (parser, NULL);
6033 op = default_function_array_read_conversion (exp_loc, op);
6034 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6035 return ret;
6036 case CPP_PLUS:
6037 if (!c_dialect_objc () && !in_system_header)
6038 warning_at (op_loc,
6039 OPT_Wtraditional,
6040 "traditional C rejects the unary plus operator");
6041 c_parser_consume_token (parser);
6042 exp_loc = c_parser_peek_token (parser)->location;
6043 op = c_parser_cast_expression (parser, NULL);
6044 op = default_function_array_read_conversion (exp_loc, op);
6045 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6046 case CPP_MINUS:
6047 c_parser_consume_token (parser);
6048 exp_loc = c_parser_peek_token (parser)->location;
6049 op = c_parser_cast_expression (parser, NULL);
6050 op = default_function_array_read_conversion (exp_loc, op);
6051 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6052 case CPP_COMPL:
6053 c_parser_consume_token (parser);
6054 exp_loc = c_parser_peek_token (parser)->location;
6055 op = c_parser_cast_expression (parser, NULL);
6056 op = default_function_array_read_conversion (exp_loc, op);
6057 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6058 case CPP_NOT:
6059 c_parser_consume_token (parser);
6060 exp_loc = c_parser_peek_token (parser)->location;
6061 op = c_parser_cast_expression (parser, NULL);
6062 op = default_function_array_read_conversion (exp_loc, op);
6063 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6064 case CPP_AND_AND:
6065 /* Refer to the address of a label as a pointer. */
6066 c_parser_consume_token (parser);
6067 if (c_parser_next_token_is (parser, CPP_NAME))
6069 ret.value = finish_label_address_expr
6070 (c_parser_peek_token (parser)->value, op_loc);
6071 c_parser_consume_token (parser);
6073 else
6075 c_parser_error (parser, "expected identifier");
6076 ret.value = error_mark_node;
6078 return ret;
6079 case CPP_KEYWORD:
6080 switch (c_parser_peek_token (parser)->keyword)
6082 case RID_SIZEOF:
6083 return c_parser_sizeof_expression (parser);
6084 case RID_UPC_BLOCKSIZEOF:
6085 case RID_UPC_ELEMSIZEOF:
6086 case RID_UPC_LOCALSIZEOF:
6087 gcc_assert (c_dialect_upc ());
6088 return c_parser_sizeof_expression (parser);
6089 case RID_ALIGNOF:
6090 return c_parser_alignof_expression (parser);
6091 case RID_EXTENSION:
6092 c_parser_consume_token (parser);
6093 ext = disable_extension_diagnostics ();
6094 ret = c_parser_cast_expression (parser, NULL);
6095 restore_extension_diagnostics (ext);
6096 return ret;
6097 case RID_REALPART:
6098 c_parser_consume_token (parser);
6099 exp_loc = c_parser_peek_token (parser)->location;
6100 op = c_parser_cast_expression (parser, NULL);
6101 op = default_function_array_conversion (exp_loc, op);
6102 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6103 case RID_IMAGPART:
6104 c_parser_consume_token (parser);
6105 exp_loc = c_parser_peek_token (parser)->location;
6106 op = c_parser_cast_expression (parser, NULL);
6107 op = default_function_array_conversion (exp_loc, op);
6108 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6109 case RID_TRANSACTION_ATOMIC:
6110 case RID_TRANSACTION_RELAXED:
6111 return c_parser_transaction_expression (parser,
6112 c_parser_peek_token (parser)->keyword);
6113 default:
6114 return c_parser_postfix_expression (parser);
6116 default:
6117 return c_parser_postfix_expression (parser);
6121 /* Return the result of upc_blocksizeof applied to EXPR. */
6123 static
6124 struct c_expr
6125 upc_blocksizeof_expr (location_t loc, struct c_expr expr)
6127 struct c_expr ret;
6128 ret.original_code = ERROR_MARK;
6129 ret.original_type = NULL_TREE;
6130 if (expr.value == error_mark_node)
6132 ret.value = error_mark_node;
6133 pop_maybe_used (false);
6135 else
6137 ret.value = upc_blocksizeof (loc, TREE_TYPE (expr.value));
6138 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6140 return ret;
6143 /* Return the result of upc_blocksizeof applied to T, a structure
6144 for the type name passed to sizeof (rather than the type itself). */
6146 static
6147 struct c_expr
6148 upc_blocksizeof_type (location_t loc, struct c_type_name *t)
6150 tree type;
6151 struct c_expr ret;
6152 ret.original_code = ERROR_MARK;
6153 ret.original_type = NULL_TREE;
6154 type = groktypename (t, NULL, NULL);
6155 if (type == error_mark_node)
6157 ret.value = error_mark_node;
6158 pop_maybe_used (false);
6160 else
6162 ret.value = upc_blocksizeof (loc, type);
6163 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6165 return ret;
6168 /* Return the result of upc_elemsizeof applied to EXPR. */
6170 static
6171 struct c_expr
6172 upc_elemsizeof_expr (location_t loc, struct c_expr expr)
6174 struct c_expr ret;
6175 ret.original_code = ERROR_MARK;
6176 ret.original_type = NULL_TREE;
6177 if (expr.value == error_mark_node)
6179 ret.value = error_mark_node;
6180 pop_maybe_used (false);
6182 else
6184 ret.value = upc_elemsizeof (loc, TREE_TYPE (expr.value));
6185 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6187 return ret;
6190 /* Return the result of upc_elemsizeof applied to T, a structure
6191 for the type name passed to sizeof (rather than the type itself). */
6193 static
6194 struct c_expr
6195 upc_elemsizeof_type (location_t loc, struct c_type_name *t)
6197 tree type;
6198 struct c_expr ret;
6199 ret.original_code = ERROR_MARK;
6200 ret.original_type = NULL_TREE;
6201 type = groktypename (t, NULL, NULL);
6202 if (type == error_mark_node)
6204 ret.value = error_mark_node;
6205 pop_maybe_used (false);
6207 else
6209 ret.value = upc_elemsizeof (loc, type);
6210 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6212 return ret;
6215 /* Return the result of upc_localsizeof applied to EXPR. */
6217 static
6218 struct c_expr
6219 upc_localsizeof_expr (location_t loc, struct c_expr expr)
6221 struct c_expr ret;
6222 ret.original_code = ERROR_MARK;
6223 ret.original_type = NULL_TREE;
6224 if (expr.value == error_mark_node)
6226 ret.value = error_mark_node;
6227 pop_maybe_used (false);
6229 else
6231 ret.value = upc_localsizeof (loc, TREE_TYPE (expr.value));
6232 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6234 return ret;
6237 /* Return the result of upc_localsizeof applied to T, a structure
6238 for the type name passed to sizeof (rather than the type itself). */
6240 static
6241 struct c_expr
6242 upc_localsizeof_type (location_t loc, struct c_type_name *t)
6244 tree type;
6245 struct c_expr ret;
6246 ret.original_code = ERROR_MARK;
6247 ret.original_type = NULL_TREE;
6248 type = groktypename (t, NULL, NULL);
6249 if (type == error_mark_node)
6251 ret.value = error_mark_node;
6252 pop_maybe_used (false);
6254 else
6256 ret.value = upc_localsizeof (loc, type);
6257 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6259 return ret;
6262 /* Parse a sizeof expression. */
6264 static struct c_expr
6265 c_parser_sizeof_expression (c_parser *parser)
6267 struct c_expr expr;
6268 location_t expr_loc;
6269 enum rid keyword = c_parser_peek_token (parser)->keyword;
6270 c_parser_consume_token (parser);
6271 c_inhibit_evaluation_warnings++;
6272 in_sizeof++;
6273 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6274 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6276 /* Either sizeof ( type-name ) or sizeof unary-expression
6277 starting with a compound literal. */
6278 struct c_type_name *type_name;
6279 c_parser_consume_token (parser);
6280 expr_loc = c_parser_peek_token (parser)->location;
6281 type_name = c_parser_type_name (parser);
6282 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6283 if (type_name == NULL)
6285 struct c_expr ret;
6286 c_inhibit_evaluation_warnings--;
6287 in_sizeof--;
6288 ret.value = error_mark_node;
6289 ret.original_code = ERROR_MARK;
6290 ret.original_type = NULL;
6291 return ret;
6293 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6295 expr = c_parser_postfix_expression_after_paren_type (parser,
6296 type_name,
6297 expr_loc);
6298 goto sizeof_expr;
6300 /* sizeof ( type-name ). */
6301 c_inhibit_evaluation_warnings--;
6302 in_sizeof--;
6303 /* Handle upc_*_sizeof (type) operations. */
6304 switch (keyword)
6306 case RID_UPC_BLOCKSIZEOF:
6307 return upc_blocksizeof_type (expr_loc, type_name);
6308 case RID_UPC_ELEMSIZEOF:
6309 return upc_elemsizeof_type (expr_loc, type_name);
6310 case RID_UPC_LOCALSIZEOF:
6311 return upc_localsizeof_type (expr_loc, type_name);
6312 default: break;
6314 return c_expr_sizeof_type (expr_loc, type_name);
6316 else
6318 expr_loc = c_parser_peek_token (parser)->location;
6319 expr = c_parser_unary_expression (parser);
6320 sizeof_expr:
6321 c_inhibit_evaluation_warnings--;
6322 in_sizeof--;
6323 mark_exp_read (expr.value);
6324 if (TREE_CODE (expr.value) == COMPONENT_REF
6325 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6326 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6327 /* Handle upc_*_sizeof (expr) operations. */
6328 switch (keyword)
6330 case RID_UPC_BLOCKSIZEOF:
6331 return upc_blocksizeof_expr (expr_loc, expr);
6332 case RID_UPC_ELEMSIZEOF:
6333 return upc_elemsizeof_expr (expr_loc, expr);
6334 case RID_UPC_LOCALSIZEOF:
6335 return upc_localsizeof_expr (expr_loc, expr);
6336 case RID_SIZEOF:
6337 return c_expr_sizeof_expr (expr_loc, expr);
6338 default: break;
6340 return c_expr_sizeof_expr (expr_loc, expr);
6344 /* Parse an alignof expression. */
6346 static struct c_expr
6347 c_parser_alignof_expression (c_parser *parser)
6349 struct c_expr expr;
6350 location_t loc = c_parser_peek_token (parser)->location;
6351 tree alignof_spelling = c_parser_peek_token (parser)->value;
6352 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6353 /* A diagnostic is not required for the use of this identifier in
6354 the implementation namespace; only diagnose it for the C11
6355 spelling because of existing code using the other spellings. */
6356 if (!flag_isoc11
6357 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6359 if (flag_isoc99)
6360 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6361 alignof_spelling);
6362 else
6363 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6364 alignof_spelling);
6366 c_parser_consume_token (parser);
6367 c_inhibit_evaluation_warnings++;
6368 in_alignof++;
6369 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6370 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6372 /* Either __alignof__ ( type-name ) or __alignof__
6373 unary-expression starting with a compound literal. */
6374 location_t loc;
6375 struct c_type_name *type_name;
6376 struct c_expr ret;
6377 c_parser_consume_token (parser);
6378 loc = c_parser_peek_token (parser)->location;
6379 type_name = c_parser_type_name (parser);
6380 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6381 if (type_name == NULL)
6383 struct c_expr ret;
6384 c_inhibit_evaluation_warnings--;
6385 in_alignof--;
6386 ret.value = error_mark_node;
6387 ret.original_code = ERROR_MARK;
6388 ret.original_type = NULL;
6389 return ret;
6391 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6393 expr = c_parser_postfix_expression_after_paren_type (parser,
6394 type_name,
6395 loc);
6396 goto alignof_expr;
6398 /* alignof ( type-name ). */
6399 c_inhibit_evaluation_warnings--;
6400 in_alignof--;
6401 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6402 ret.original_code = ERROR_MARK;
6403 ret.original_type = NULL;
6404 return ret;
6406 else
6408 struct c_expr ret;
6409 expr = c_parser_unary_expression (parser);
6410 alignof_expr:
6411 mark_exp_read (expr.value);
6412 c_inhibit_evaluation_warnings--;
6413 in_alignof--;
6414 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6415 alignof_spelling);
6416 ret.value = c_alignof_expr (loc, expr.value);
6417 ret.original_code = ERROR_MARK;
6418 ret.original_type = NULL;
6419 return ret;
6423 /* Helper function to read arguments of builtins which are interfaces
6424 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6425 others. The name of the builtin is passed using BNAME parameter.
6426 Function returns true if there were no errors while parsing and
6427 stores the arguments in CEXPR_LIST. */
6428 static bool
6429 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6430 vec<c_expr_t, va_gc> **ret_cexpr_list,
6431 bool choose_expr_p)
6433 location_t loc = c_parser_peek_token (parser)->location;
6434 vec<c_expr_t, va_gc> *cexpr_list;
6435 c_expr_t expr;
6436 bool saved_force_folding_builtin_constant_p;
6438 *ret_cexpr_list = NULL;
6439 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6441 error_at (loc, "cannot take address of %qs", bname);
6442 return false;
6445 c_parser_consume_token (parser);
6447 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6449 c_parser_consume_token (parser);
6450 return true;
6453 saved_force_folding_builtin_constant_p
6454 = force_folding_builtin_constant_p;
6455 force_folding_builtin_constant_p |= choose_expr_p;
6456 expr = c_parser_expr_no_commas (parser, NULL);
6457 force_folding_builtin_constant_p
6458 = saved_force_folding_builtin_constant_p;
6459 vec_alloc (cexpr_list, 1);
6460 C_EXPR_APPEND (cexpr_list, expr);
6461 while (c_parser_next_token_is (parser, CPP_COMMA))
6463 c_parser_consume_token (parser);
6464 expr = c_parser_expr_no_commas (parser, NULL);
6465 C_EXPR_APPEND (cexpr_list, expr);
6468 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6469 return false;
6471 *ret_cexpr_list = cexpr_list;
6472 return true;
6476 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6478 postfix-expression:
6479 primary-expression
6480 postfix-expression [ expression ]
6481 postfix-expression ( argument-expression-list[opt] )
6482 postfix-expression . identifier
6483 postfix-expression -> identifier
6484 postfix-expression ++
6485 postfix-expression --
6486 ( type-name ) { initializer-list }
6487 ( type-name ) { initializer-list , }
6489 argument-expression-list:
6490 argument-expression
6491 argument-expression-list , argument-expression
6493 primary-expression:
6494 identifier
6495 constant
6496 string-literal
6497 ( expression )
6499 GNU extensions:
6501 primary-expression:
6502 __func__
6503 (treated as a keyword in GNU C)
6504 __FUNCTION__
6505 __PRETTY_FUNCTION__
6506 ( compound-statement )
6507 __builtin_va_arg ( assignment-expression , type-name )
6508 __builtin_offsetof ( type-name , offsetof-member-designator )
6509 __builtin_choose_expr ( assignment-expression ,
6510 assignment-expression ,
6511 assignment-expression )
6512 __builtin_types_compatible_p ( type-name , type-name )
6513 __builtin_complex ( assignment-expression , assignment-expression )
6514 __builtin_shuffle ( assignment-expression , assignment-expression )
6515 __builtin_shuffle ( assignment-expression ,
6516 assignment-expression ,
6517 assignment-expression, )
6519 offsetof-member-designator:
6520 identifier
6521 offsetof-member-designator . identifier
6522 offsetof-member-designator [ expression ]
6524 Objective-C:
6526 primary-expression:
6527 [ objc-receiver objc-message-args ]
6528 @selector ( objc-selector-arg )
6529 @protocol ( identifier )
6530 @encode ( type-name )
6531 objc-string-literal
6532 Classname . identifier
6535 static struct c_expr
6536 c_parser_postfix_expression (c_parser *parser)
6538 struct c_expr expr, e1;
6539 struct c_type_name *t1, *t2;
6540 location_t loc = c_parser_peek_token (parser)->location;;
6541 expr.original_code = ERROR_MARK;
6542 expr.original_type = NULL;
6543 switch (c_parser_peek_token (parser)->type)
6545 case CPP_NUMBER:
6546 expr.value = c_parser_peek_token (parser)->value;
6547 loc = c_parser_peek_token (parser)->location;
6548 c_parser_consume_token (parser);
6549 if (TREE_CODE (expr.value) == FIXED_CST
6550 && !targetm.fixed_point_supported_p ())
6552 error_at (loc, "fixed-point types not supported for this target");
6553 expr.value = error_mark_node;
6555 break;
6556 case CPP_CHAR:
6557 case CPP_CHAR16:
6558 case CPP_CHAR32:
6559 case CPP_WCHAR:
6560 expr.value = c_parser_peek_token (parser)->value;
6561 c_parser_consume_token (parser);
6562 break;
6563 case CPP_STRING:
6564 case CPP_STRING16:
6565 case CPP_STRING32:
6566 case CPP_WSTRING:
6567 case CPP_UTF8STRING:
6568 expr.value = c_parser_peek_token (parser)->value;
6569 expr.original_code = STRING_CST;
6570 c_parser_consume_token (parser);
6571 break;
6572 case CPP_OBJC_STRING:
6573 gcc_assert (c_dialect_objc ());
6574 expr.value
6575 = objc_build_string_object (c_parser_peek_token (parser)->value);
6576 c_parser_consume_token (parser);
6577 break;
6578 case CPP_NAME:
6579 switch (c_parser_peek_token (parser)->id_kind)
6581 case C_ID_ID:
6583 tree id = c_parser_peek_token (parser)->value;
6584 c_parser_consume_token (parser);
6585 expr.value = build_external_ref (loc, id,
6586 (c_parser_peek_token (parser)->type
6587 == CPP_OPEN_PAREN),
6588 &expr.original_type);
6589 break;
6591 case C_ID_CLASSNAME:
6593 /* Here we parse the Objective-C 2.0 Class.name dot
6594 syntax. */
6595 tree class_name = c_parser_peek_token (parser)->value;
6596 tree component;
6597 c_parser_consume_token (parser);
6598 gcc_assert (c_dialect_objc ());
6599 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6601 expr.value = error_mark_node;
6602 break;
6604 if (c_parser_next_token_is_not (parser, CPP_NAME))
6606 c_parser_error (parser, "expected identifier");
6607 expr.value = error_mark_node;
6608 break;
6610 component = c_parser_peek_token (parser)->value;
6611 c_parser_consume_token (parser);
6612 expr.value = objc_build_class_component_ref (class_name,
6613 component);
6614 break;
6616 default:
6617 c_parser_error (parser, "expected expression");
6618 expr.value = error_mark_node;
6619 break;
6621 break;
6622 case CPP_OPEN_PAREN:
6623 /* A parenthesized expression, statement expression or compound
6624 literal. */
6625 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6627 /* A statement expression. */
6628 tree stmt;
6629 location_t brace_loc;
6630 c_parser_consume_token (parser);
6631 brace_loc = c_parser_peek_token (parser)->location;
6632 c_parser_consume_token (parser);
6633 if (!building_stmt_list_p ())
6635 error_at (loc, "braced-group within expression allowed "
6636 "only inside a function");
6637 parser->error = true;
6638 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6639 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6640 expr.value = error_mark_node;
6641 break;
6643 stmt = c_begin_stmt_expr ();
6644 c_parser_compound_statement_nostart (parser);
6645 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6646 "expected %<)%>");
6647 pedwarn (loc, OPT_Wpedantic,
6648 "ISO C forbids braced-groups within expressions");
6649 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6650 mark_exp_read (expr.value);
6652 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6654 /* A compound literal. ??? Can we actually get here rather
6655 than going directly to
6656 c_parser_postfix_expression_after_paren_type from
6657 elsewhere? */
6658 location_t loc;
6659 struct c_type_name *type_name;
6660 c_parser_consume_token (parser);
6661 loc = c_parser_peek_token (parser)->location;
6662 type_name = c_parser_type_name (parser);
6663 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6664 "expected %<)%>");
6665 if (type_name == NULL)
6667 expr.value = error_mark_node;
6669 else
6670 expr = c_parser_postfix_expression_after_paren_type (parser,
6671 type_name,
6672 loc);
6674 else
6676 /* A parenthesized expression. */
6677 c_parser_consume_token (parser);
6678 expr = c_parser_expression (parser);
6679 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6680 TREE_NO_WARNING (expr.value) = 1;
6681 if (expr.original_code != C_MAYBE_CONST_EXPR)
6682 expr.original_code = ERROR_MARK;
6683 /* Don't change EXPR.ORIGINAL_TYPE. */
6684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6685 "expected %<)%>");
6687 break;
6688 case CPP_KEYWORD:
6689 switch (c_parser_peek_token (parser)->keyword)
6691 case RID_FUNCTION_NAME:
6692 case RID_PRETTY_FUNCTION_NAME:
6693 case RID_C99_FUNCTION_NAME:
6694 expr.value = fname_decl (loc,
6695 c_parser_peek_token (parser)->keyword,
6696 c_parser_peek_token (parser)->value);
6697 c_parser_consume_token (parser);
6698 break;
6699 case RID_VA_ARG:
6700 c_parser_consume_token (parser);
6701 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6703 expr.value = error_mark_node;
6704 break;
6706 e1 = c_parser_expr_no_commas (parser, NULL);
6707 mark_exp_read (e1.value);
6708 e1.value = c_fully_fold (e1.value, false, NULL);
6709 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6712 expr.value = error_mark_node;
6713 break;
6715 loc = c_parser_peek_token (parser)->location;
6716 t1 = c_parser_type_name (parser);
6717 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6718 "expected %<)%>");
6719 if (t1 == NULL)
6721 expr.value = error_mark_node;
6723 else
6725 tree type_expr = NULL_TREE;
6726 expr.value = c_build_va_arg (loc, e1.value,
6727 groktypename (t1, &type_expr, NULL));
6728 if (type_expr)
6730 expr.value = build2 (C_MAYBE_CONST_EXPR,
6731 TREE_TYPE (expr.value), type_expr,
6732 expr.value);
6733 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6736 break;
6737 case RID_OFFSETOF:
6738 c_parser_consume_token (parser);
6739 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6741 expr.value = error_mark_node;
6742 break;
6744 t1 = c_parser_type_name (parser);
6745 if (t1 == NULL)
6746 parser->error = true;
6747 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6748 gcc_assert (parser->error);
6749 if (parser->error)
6751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6752 expr.value = error_mark_node;
6753 break;
6757 tree type = groktypename (t1, NULL, NULL);
6758 tree offsetof_ref;
6759 if (type == error_mark_node)
6760 offsetof_ref = error_mark_node;
6761 else
6763 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6764 SET_EXPR_LOCATION (offsetof_ref, loc);
6766 /* Parse the second argument to __builtin_offsetof. We
6767 must have one identifier, and beyond that we want to
6768 accept sub structure and sub array references. */
6769 if (c_parser_next_token_is (parser, CPP_NAME))
6771 offsetof_ref = build_component_ref
6772 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6773 c_parser_consume_token (parser);
6774 while (c_parser_next_token_is (parser, CPP_DOT)
6775 || c_parser_next_token_is (parser,
6776 CPP_OPEN_SQUARE)
6777 || c_parser_next_token_is (parser,
6778 CPP_DEREF))
6780 if (c_parser_next_token_is (parser, CPP_DEREF))
6782 loc = c_parser_peek_token (parser)->location;
6783 offsetof_ref = build_array_ref (loc,
6784 offsetof_ref,
6785 integer_zero_node);
6786 goto do_dot;
6788 else if (c_parser_next_token_is (parser, CPP_DOT))
6790 do_dot:
6791 c_parser_consume_token (parser);
6792 if (c_parser_next_token_is_not (parser,
6793 CPP_NAME))
6795 c_parser_error (parser, "expected identifier");
6796 break;
6798 offsetof_ref = build_component_ref
6799 (loc, offsetof_ref,
6800 c_parser_peek_token (parser)->value);
6801 c_parser_consume_token (parser);
6803 else
6805 tree idx;
6806 loc = c_parser_peek_token (parser)->location;
6807 c_parser_consume_token (parser);
6808 idx = c_parser_expression (parser).value;
6809 idx = c_fully_fold (idx, false, NULL);
6810 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6811 "expected %<]%>");
6812 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6816 else
6817 c_parser_error (parser, "expected identifier");
6818 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6819 "expected %<)%>");
6820 expr.value = fold_offsetof (offsetof_ref);
6822 break;
6823 case RID_CHOOSE_EXPR:
6825 vec<c_expr_t, va_gc> *cexpr_list;
6826 c_expr_t *e1_p, *e2_p, *e3_p;
6827 tree c;
6829 c_parser_consume_token (parser);
6830 if (!c_parser_get_builtin_args (parser,
6831 "__builtin_choose_expr",
6832 &cexpr_list, true))
6834 expr.value = error_mark_node;
6835 break;
6838 if (vec_safe_length (cexpr_list) != 3)
6840 error_at (loc, "wrong number of arguments to "
6841 "%<__builtin_choose_expr%>");
6842 expr.value = error_mark_node;
6843 break;
6846 e1_p = &(*cexpr_list)[0];
6847 e2_p = &(*cexpr_list)[1];
6848 e3_p = &(*cexpr_list)[2];
6850 c = e1_p->value;
6851 mark_exp_read (e2_p->value);
6852 mark_exp_read (e3_p->value);
6853 if (TREE_CODE (c) != INTEGER_CST
6854 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6855 error_at (loc,
6856 "first argument to %<__builtin_choose_expr%> not"
6857 " a constant");
6858 constant_expression_warning (c);
6859 expr = integer_zerop (c) ? *e3_p : *e2_p;
6860 break;
6862 case RID_TYPES_COMPATIBLE_P:
6863 c_parser_consume_token (parser);
6864 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6866 expr.value = error_mark_node;
6867 break;
6869 t1 = c_parser_type_name (parser);
6870 if (t1 == NULL)
6872 expr.value = error_mark_node;
6873 break;
6875 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6878 expr.value = error_mark_node;
6879 break;
6881 t2 = c_parser_type_name (parser);
6882 if (t2 == NULL)
6884 expr.value = error_mark_node;
6885 break;
6887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6888 "expected %<)%>");
6890 tree e1, e2;
6891 e1 = groktypename (t1, NULL, NULL);
6892 e2 = groktypename (t2, NULL, NULL);
6893 if (e1 == error_mark_node || e2 == error_mark_node)
6895 expr.value = error_mark_node;
6896 break;
6899 e1 = TYPE_MAIN_VARIANT (e1);
6900 e2 = TYPE_MAIN_VARIANT (e2);
6902 expr.value
6903 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6905 break;
6906 case RID_BUILTIN_COMPLEX:
6908 vec<c_expr_t, va_gc> *cexpr_list;
6909 c_expr_t *e1_p, *e2_p;
6911 c_parser_consume_token (parser);
6912 if (!c_parser_get_builtin_args (parser,
6913 "__builtin_complex",
6914 &cexpr_list, false))
6916 expr.value = error_mark_node;
6917 break;
6920 if (vec_safe_length (cexpr_list) != 2)
6922 error_at (loc, "wrong number of arguments to "
6923 "%<__builtin_complex%>");
6924 expr.value = error_mark_node;
6925 break;
6928 e1_p = &(*cexpr_list)[0];
6929 e2_p = &(*cexpr_list)[1];
6931 mark_exp_read (e1_p->value);
6932 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6933 e1_p->value = convert (TREE_TYPE (e1_p->value),
6934 TREE_OPERAND (e1_p->value, 0));
6935 mark_exp_read (e2_p->value);
6936 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6937 e2_p->value = convert (TREE_TYPE (e2_p->value),
6938 TREE_OPERAND (e2_p->value, 0));
6939 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6940 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6941 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6942 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6944 error_at (loc, "%<__builtin_complex%> operand "
6945 "not of real binary floating-point type");
6946 expr.value = error_mark_node;
6947 break;
6949 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6950 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6952 error_at (loc,
6953 "%<__builtin_complex%> operands of different types");
6954 expr.value = error_mark_node;
6955 break;
6957 if (!flag_isoc99)
6958 pedwarn (loc, OPT_Wpedantic,
6959 "ISO C90 does not support complex types");
6960 expr.value = build2 (COMPLEX_EXPR,
6961 build_complex_type
6962 (TYPE_MAIN_VARIANT
6963 (TREE_TYPE (e1_p->value))),
6964 e1_p->value, e2_p->value);
6965 break;
6967 case RID_BUILTIN_SHUFFLE:
6969 vec<c_expr_t, va_gc> *cexpr_list;
6970 unsigned int i;
6971 c_expr_t *p;
6973 c_parser_consume_token (parser);
6974 if (!c_parser_get_builtin_args (parser,
6975 "__builtin_shuffle",
6976 &cexpr_list, false))
6978 expr.value = error_mark_node;
6979 break;
6982 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
6983 mark_exp_read (p->value);
6985 if (vec_safe_length (cexpr_list) == 2)
6986 expr.value =
6987 c_build_vec_perm_expr
6988 (loc, (*cexpr_list)[0].value,
6989 NULL_TREE, (*cexpr_list)[1].value);
6991 else if (vec_safe_length (cexpr_list) == 3)
6992 expr.value =
6993 c_build_vec_perm_expr
6994 (loc, (*cexpr_list)[0].value,
6995 (*cexpr_list)[1].value,
6996 (*cexpr_list)[2].value);
6997 else
6999 error_at (loc, "wrong number of arguments to "
7000 "%<__builtin_shuffle%>");
7001 expr.value = error_mark_node;
7003 break;
7005 case RID_AT_SELECTOR:
7006 gcc_assert (c_dialect_objc ());
7007 c_parser_consume_token (parser);
7008 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7010 expr.value = error_mark_node;
7011 break;
7014 tree sel = c_parser_objc_selector_arg (parser);
7015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7016 "expected %<)%>");
7017 expr.value = objc_build_selector_expr (loc, sel);
7019 break;
7020 case RID_AT_PROTOCOL:
7021 gcc_assert (c_dialect_objc ());
7022 c_parser_consume_token (parser);
7023 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7025 expr.value = error_mark_node;
7026 break;
7028 if (c_parser_next_token_is_not (parser, CPP_NAME))
7030 c_parser_error (parser, "expected identifier");
7031 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7032 expr.value = error_mark_node;
7033 break;
7036 tree id = c_parser_peek_token (parser)->value;
7037 c_parser_consume_token (parser);
7038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7039 "expected %<)%>");
7040 expr.value = objc_build_protocol_expr (id);
7042 break;
7043 case RID_AT_ENCODE:
7044 /* Extension to support C-structures in the archiver. */
7045 gcc_assert (c_dialect_objc ());
7046 c_parser_consume_token (parser);
7047 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7049 expr.value = error_mark_node;
7050 break;
7052 t1 = c_parser_type_name (parser);
7053 if (t1 == NULL)
7055 expr.value = error_mark_node;
7056 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7057 break;
7059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7060 "expected %<)%>");
7062 tree type = groktypename (t1, NULL, NULL);
7063 expr.value = objc_build_encode_expr (type);
7065 break;
7066 default:
7067 c_parser_error (parser, "expected expression");
7068 expr.value = error_mark_node;
7069 break;
7071 break;
7072 case CPP_OPEN_SQUARE:
7073 if (c_dialect_objc ())
7075 tree receiver, args;
7076 c_parser_consume_token (parser);
7077 receiver = c_parser_objc_receiver (parser);
7078 args = c_parser_objc_message_args (parser);
7079 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7080 "expected %<]%>");
7081 expr.value = objc_build_message_expr (receiver, args);
7082 break;
7084 /* Else fall through to report error. */
7085 default:
7086 c_parser_error (parser, "expected expression");
7087 expr.value = error_mark_node;
7088 break;
7090 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7093 /* Parse a postfix expression after a parenthesized type name: the
7094 brace-enclosed initializer of a compound literal, possibly followed
7095 by some postfix operators. This is separate because it is not
7096 possible to tell until after the type name whether a cast
7097 expression has a cast or a compound literal, or whether the operand
7098 of sizeof is a parenthesized type name or starts with a compound
7099 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7100 location of the first token after the parentheses around the type
7101 name. */
7103 static struct c_expr
7104 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7105 struct c_type_name *type_name,
7106 location_t type_loc)
7108 tree type;
7109 struct c_expr init;
7110 bool non_const;
7111 struct c_expr expr;
7112 location_t start_loc;
7113 tree type_expr = NULL_TREE;
7114 bool type_expr_const = true;
7115 check_compound_literal_type (type_loc, type_name);
7116 start_init (NULL_TREE, NULL, 0);
7117 type = groktypename (type_name, &type_expr, &type_expr_const);
7118 start_loc = c_parser_peek_token (parser)->location;
7119 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7121 error_at (type_loc, "compound literal has variable size");
7122 type = error_mark_node;
7124 init = c_parser_braced_init (parser, type, false);
7125 finish_init ();
7126 maybe_warn_string_init (type, init);
7128 if (type != error_mark_node
7129 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7130 && current_function_decl)
7132 error ("compound literal qualified by address-space qualifier");
7133 type = error_mark_node;
7136 if (!flag_isoc99)
7137 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7138 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7139 ? CONSTRUCTOR_NON_CONST (init.value)
7140 : init.original_code == C_MAYBE_CONST_EXPR);
7141 non_const |= !type_expr_const;
7142 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7143 expr.original_code = ERROR_MARK;
7144 expr.original_type = NULL;
7145 if (type_expr)
7147 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7149 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7150 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7152 else
7154 gcc_assert (!non_const);
7155 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7156 type_expr, expr.value);
7159 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7162 /* Callback function for sizeof_pointer_memaccess_warning to compare
7163 types. */
7165 static bool
7166 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7168 return comptypes (type1, type2) == 1;
7171 /* Parse a postfix expression after the initial primary or compound
7172 literal; that is, parse a series of postfix operators.
7174 EXPR_LOC is the location of the primary expression. */
7176 static struct c_expr
7177 c_parser_postfix_expression_after_primary (c_parser *parser,
7178 location_t expr_loc,
7179 struct c_expr expr)
7181 struct c_expr orig_expr;
7182 tree ident, idx;
7183 location_t sizeof_arg_loc[3];
7184 tree sizeof_arg[3];
7185 unsigned int i;
7186 vec<tree, va_gc> *exprlist;
7187 vec<tree, va_gc> *origtypes = NULL;
7188 while (true)
7190 location_t op_loc = c_parser_peek_token (parser)->location;
7191 switch (c_parser_peek_token (parser)->type)
7193 case CPP_OPEN_SQUARE:
7194 /* Array reference. */
7195 c_parser_consume_token (parser);
7196 if (flag_enable_cilkplus
7197 && c_parser_peek_token (parser)->type == CPP_COLON)
7198 /* If we are here, then we have something like this:
7199 Array [ : ]
7201 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7202 expr.value);
7203 else
7205 idx = c_parser_expression (parser).value;
7206 /* Here we have 3 options:
7207 1. Array [EXPR] -- Normal Array call.
7208 2. Array [EXPR : EXPR] -- Array notation without stride.
7209 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7211 For 1, we just handle it just like a normal array expression.
7212 For 2 and 3 we handle it like we handle array notations. The
7213 idx value we have above becomes the initial/start index.
7215 if (flag_enable_cilkplus
7216 && c_parser_peek_token (parser)->type == CPP_COLON)
7217 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7218 expr.value);
7219 else
7221 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7222 "expected %<]%>");
7223 expr.value = build_array_ref (op_loc, expr.value, idx);
7226 expr.original_code = ERROR_MARK;
7227 expr.original_type = NULL;
7228 break;
7229 case CPP_OPEN_PAREN:
7230 /* Function call. */
7231 c_parser_consume_token (parser);
7232 for (i = 0; i < 3; i++)
7234 sizeof_arg[i] = NULL_TREE;
7235 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7237 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7238 exprlist = NULL;
7239 else
7240 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7241 sizeof_arg_loc, sizeof_arg);
7242 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7243 "expected %<)%>");
7244 orig_expr = expr;
7245 mark_exp_read (expr.value);
7246 if (warn_sizeof_pointer_memaccess)
7247 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7248 expr.value, exprlist,
7249 sizeof_arg,
7250 sizeof_ptr_memacc_comptypes);
7251 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7252 "(" after the FUNCNAME, which is what we have now. */
7253 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7254 origtypes);
7255 expr.original_code = ERROR_MARK;
7256 if (TREE_CODE (expr.value) == INTEGER_CST
7257 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7258 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7259 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7260 expr.original_code = C_MAYBE_CONST_EXPR;
7261 expr.original_type = NULL;
7262 if (exprlist)
7264 release_tree_vector (exprlist);
7265 release_tree_vector (origtypes);
7267 break;
7268 case CPP_DOT:
7269 /* Structure element reference. */
7270 c_parser_consume_token (parser);
7271 expr = default_function_array_conversion (expr_loc, expr);
7272 if (c_parser_next_token_is (parser, CPP_NAME))
7273 ident = c_parser_peek_token (parser)->value;
7274 else
7276 c_parser_error (parser, "expected identifier");
7277 expr.value = error_mark_node;
7278 expr.original_code = ERROR_MARK;
7279 expr.original_type = NULL;
7280 return expr;
7282 c_parser_consume_token (parser);
7283 expr.value = build_component_ref (op_loc, expr.value, ident);
7284 expr.original_code = ERROR_MARK;
7285 if (TREE_CODE (expr.value) != COMPONENT_REF)
7286 expr.original_type = NULL;
7287 else
7289 /* Remember the original type of a bitfield. */
7290 tree field = TREE_OPERAND (expr.value, 1);
7291 if (TREE_CODE (field) != FIELD_DECL)
7292 expr.original_type = NULL;
7293 else
7294 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7296 break;
7297 case CPP_DEREF:
7298 /* Structure element reference. */
7299 c_parser_consume_token (parser);
7300 expr = default_function_array_conversion (expr_loc, expr);
7301 if (c_parser_next_token_is (parser, CPP_NAME))
7302 ident = c_parser_peek_token (parser)->value;
7303 else
7305 c_parser_error (parser, "expected identifier");
7306 expr.value = error_mark_node;
7307 expr.original_code = ERROR_MARK;
7308 expr.original_type = NULL;
7309 return expr;
7311 c_parser_consume_token (parser);
7312 expr.value = build_component_ref (op_loc,
7313 build_indirect_ref (op_loc,
7314 expr.value,
7315 RO_ARROW),
7316 ident);
7317 expr.original_code = ERROR_MARK;
7318 if (TREE_CODE (expr.value) != COMPONENT_REF)
7319 expr.original_type = NULL;
7320 else
7322 /* Remember the original type of a bitfield. */
7323 tree field = TREE_OPERAND (expr.value, 1);
7324 if (TREE_CODE (field) != FIELD_DECL)
7325 expr.original_type = NULL;
7326 else
7327 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7329 break;
7330 case CPP_PLUS_PLUS:
7331 /* Postincrement. */
7332 c_parser_consume_token (parser);
7333 /* If the expressions have array notations, we expand them. */
7334 if (flag_enable_cilkplus
7335 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7336 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7337 else
7339 expr = default_function_array_read_conversion (expr_loc, expr);
7340 expr.value = build_unary_op (op_loc,
7341 POSTINCREMENT_EXPR, expr.value, 0);
7343 expr.original_code = ERROR_MARK;
7344 expr.original_type = NULL;
7345 break;
7346 case CPP_MINUS_MINUS:
7347 /* Postdecrement. */
7348 c_parser_consume_token (parser);
7349 /* If the expressions have array notations, we expand them. */
7350 if (flag_enable_cilkplus
7351 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7352 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7353 else
7355 expr = default_function_array_read_conversion (expr_loc, expr);
7356 expr.value = build_unary_op (op_loc,
7357 POSTDECREMENT_EXPR, expr.value, 0);
7359 expr.original_code = ERROR_MARK;
7360 expr.original_type = NULL;
7361 break;
7362 default:
7363 return expr;
7368 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7370 expression:
7371 assignment-expression
7372 expression , assignment-expression
7375 static struct c_expr
7376 c_parser_expression (c_parser *parser)
7378 struct c_expr expr;
7379 expr = c_parser_expr_no_commas (parser, NULL);
7380 while (c_parser_next_token_is (parser, CPP_COMMA))
7382 struct c_expr next;
7383 tree lhsval;
7384 location_t loc = c_parser_peek_token (parser)->location;
7385 location_t expr_loc;
7386 c_parser_consume_token (parser);
7387 expr_loc = c_parser_peek_token (parser)->location;
7388 lhsval = expr.value;
7389 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7390 lhsval = TREE_OPERAND (lhsval, 1);
7391 if (DECL_P (lhsval) || handled_component_p (lhsval))
7392 mark_exp_read (lhsval);
7393 next = c_parser_expr_no_commas (parser, NULL);
7394 next = default_function_array_conversion (expr_loc, next);
7395 expr.value = build_compound_expr (loc, expr.value, next.value);
7396 expr.original_code = COMPOUND_EXPR;
7397 expr.original_type = next.original_type;
7399 return expr;
7402 /* Parse an expression and convert functions or arrays to
7403 pointers. */
7405 static struct c_expr
7406 c_parser_expression_conv (c_parser *parser)
7408 struct c_expr expr;
7409 location_t loc = c_parser_peek_token (parser)->location;
7410 expr = c_parser_expression (parser);
7411 expr = default_function_array_conversion (loc, expr);
7412 return expr;
7415 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7416 functions and arrays to pointers. If FOLD_P, fold the expressions.
7418 nonempty-expr-list:
7419 assignment-expression
7420 nonempty-expr-list , assignment-expression
7423 static vec<tree, va_gc> *
7424 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7425 vec<tree, va_gc> **p_orig_types,
7426 location_t *sizeof_arg_loc, tree *sizeof_arg)
7428 vec<tree, va_gc> *ret;
7429 vec<tree, va_gc> *orig_types;
7430 struct c_expr expr;
7431 location_t loc = c_parser_peek_token (parser)->location;
7432 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7433 unsigned int idx = 0;
7435 ret = make_tree_vector ();
7436 if (p_orig_types == NULL)
7437 orig_types = NULL;
7438 else
7439 orig_types = make_tree_vector ();
7441 if (sizeof_arg != NULL
7442 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7443 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7444 expr = c_parser_expr_no_commas (parser, NULL);
7445 if (convert_p)
7446 expr = default_function_array_read_conversion (loc, expr);
7447 if (fold_p)
7448 expr.value = c_fully_fold (expr.value, false, NULL);
7449 ret->quick_push (expr.value);
7450 if (orig_types)
7451 orig_types->quick_push (expr.original_type);
7452 if (sizeof_arg != NULL
7453 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7454 && expr.original_code == SIZEOF_EXPR)
7456 sizeof_arg[0] = c_last_sizeof_arg;
7457 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7459 while (c_parser_next_token_is (parser, CPP_COMMA))
7461 c_parser_consume_token (parser);
7462 loc = c_parser_peek_token (parser)->location;
7463 if (sizeof_arg != NULL
7464 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7465 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7466 else
7467 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7468 expr = c_parser_expr_no_commas (parser, NULL);
7469 if (convert_p)
7470 expr = default_function_array_read_conversion (loc, expr);
7471 if (fold_p)
7472 expr.value = c_fully_fold (expr.value, false, NULL);
7473 vec_safe_push (ret, expr.value);
7474 if (orig_types)
7475 vec_safe_push (orig_types, expr.original_type);
7476 if (++idx < 3
7477 && sizeof_arg != NULL
7478 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7479 && expr.original_code == SIZEOF_EXPR)
7481 sizeof_arg[idx] = c_last_sizeof_arg;
7482 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7485 if (orig_types)
7486 *p_orig_types = orig_types;
7487 return ret;
7490 /* Parse Objective-C-specific constructs. */
7492 /* Parse an objc-class-definition.
7494 objc-class-definition:
7495 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7496 objc-class-instance-variables[opt] objc-methodprotolist @end
7497 @implementation identifier objc-superclass[opt]
7498 objc-class-instance-variables[opt]
7499 @interface identifier ( identifier ) objc-protocol-refs[opt]
7500 objc-methodprotolist @end
7501 @interface identifier ( ) objc-protocol-refs[opt]
7502 objc-methodprotolist @end
7503 @implementation identifier ( identifier )
7505 objc-superclass:
7506 : identifier
7508 "@interface identifier (" must start "@interface identifier (
7509 identifier ) ...": objc-methodprotolist in the first production may
7510 not start with a parenthesized identifier as a declarator of a data
7511 definition with no declaration specifiers if the objc-superclass,
7512 objc-protocol-refs and objc-class-instance-variables are omitted. */
7514 static void
7515 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7517 bool iface_p;
7518 tree id1;
7519 tree superclass;
7520 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7521 iface_p = true;
7522 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7523 iface_p = false;
7524 else
7525 gcc_unreachable ();
7527 c_parser_consume_token (parser);
7528 if (c_parser_next_token_is_not (parser, CPP_NAME))
7530 c_parser_error (parser, "expected identifier");
7531 return;
7533 id1 = c_parser_peek_token (parser)->value;
7534 c_parser_consume_token (parser);
7535 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7537 /* We have a category or class extension. */
7538 tree id2;
7539 tree proto = NULL_TREE;
7540 c_parser_consume_token (parser);
7541 if (c_parser_next_token_is_not (parser, CPP_NAME))
7543 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7545 /* We have a class extension. */
7546 id2 = NULL_TREE;
7548 else
7550 c_parser_error (parser, "expected identifier or %<)%>");
7551 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7552 return;
7555 else
7557 id2 = c_parser_peek_token (parser)->value;
7558 c_parser_consume_token (parser);
7560 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7561 if (!iface_p)
7563 objc_start_category_implementation (id1, id2);
7564 return;
7566 if (c_parser_next_token_is (parser, CPP_LESS))
7567 proto = c_parser_objc_protocol_refs (parser);
7568 objc_start_category_interface (id1, id2, proto, attributes);
7569 c_parser_objc_methodprotolist (parser);
7570 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7571 objc_finish_interface ();
7572 return;
7574 if (c_parser_next_token_is (parser, CPP_COLON))
7576 c_parser_consume_token (parser);
7577 if (c_parser_next_token_is_not (parser, CPP_NAME))
7579 c_parser_error (parser, "expected identifier");
7580 return;
7582 superclass = c_parser_peek_token (parser)->value;
7583 c_parser_consume_token (parser);
7585 else
7586 superclass = NULL_TREE;
7587 if (iface_p)
7589 tree proto = NULL_TREE;
7590 if (c_parser_next_token_is (parser, CPP_LESS))
7591 proto = c_parser_objc_protocol_refs (parser);
7592 objc_start_class_interface (id1, superclass, proto, attributes);
7594 else
7595 objc_start_class_implementation (id1, superclass);
7596 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7597 c_parser_objc_class_instance_variables (parser);
7598 if (iface_p)
7600 objc_continue_interface ();
7601 c_parser_objc_methodprotolist (parser);
7602 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7603 objc_finish_interface ();
7605 else
7607 objc_continue_implementation ();
7608 return;
7612 /* Parse objc-class-instance-variables.
7614 objc-class-instance-variables:
7615 { objc-instance-variable-decl-list[opt] }
7617 objc-instance-variable-decl-list:
7618 objc-visibility-spec
7619 objc-instance-variable-decl ;
7621 objc-instance-variable-decl-list objc-visibility-spec
7622 objc-instance-variable-decl-list objc-instance-variable-decl ;
7623 objc-instance-variable-decl-list ;
7625 objc-visibility-spec:
7626 @private
7627 @protected
7628 @public
7630 objc-instance-variable-decl:
7631 struct-declaration
7634 static void
7635 c_parser_objc_class_instance_variables (c_parser *parser)
7637 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7638 c_parser_consume_token (parser);
7639 while (c_parser_next_token_is_not (parser, CPP_EOF))
7641 tree decls;
7642 /* Parse any stray semicolon. */
7643 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7645 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7646 "extra semicolon");
7647 c_parser_consume_token (parser);
7648 continue;
7650 /* Stop if at the end of the instance variables. */
7651 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7653 c_parser_consume_token (parser);
7654 break;
7656 /* Parse any objc-visibility-spec. */
7657 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7659 c_parser_consume_token (parser);
7660 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7661 continue;
7663 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7665 c_parser_consume_token (parser);
7666 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7667 continue;
7669 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7671 c_parser_consume_token (parser);
7672 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7673 continue;
7675 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7677 c_parser_consume_token (parser);
7678 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7679 continue;
7681 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7683 c_parser_pragma (parser, pragma_external);
7684 continue;
7687 /* Parse some comma-separated declarations. */
7688 decls = c_parser_struct_declaration (parser);
7689 if (decls == NULL)
7691 /* There is a syntax error. We want to skip the offending
7692 tokens up to the next ';' (included) or '}'
7693 (excluded). */
7695 /* First, skip manually a ')' or ']'. This is because they
7696 reduce the nesting level, so c_parser_skip_until_found()
7697 wouldn't be able to skip past them. */
7698 c_token *token = c_parser_peek_token (parser);
7699 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7700 c_parser_consume_token (parser);
7702 /* Then, do the standard skipping. */
7703 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7705 /* We hopefully recovered. Start normal parsing again. */
7706 parser->error = false;
7707 continue;
7709 else
7711 /* Comma-separated instance variables are chained together
7712 in reverse order; add them one by one. */
7713 tree ivar = nreverse (decls);
7714 for (; ivar; ivar = DECL_CHAIN (ivar))
7715 objc_add_instance_variable (copy_node (ivar));
7717 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7721 /* Parse an objc-class-declaration.
7723 objc-class-declaration:
7724 @class identifier-list ;
7727 static void
7728 c_parser_objc_class_declaration (c_parser *parser)
7730 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7731 c_parser_consume_token (parser);
7732 /* Any identifiers, including those declared as type names, are OK
7733 here. */
7734 while (true)
7736 tree id;
7737 if (c_parser_next_token_is_not (parser, CPP_NAME))
7739 c_parser_error (parser, "expected identifier");
7740 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7741 parser->error = false;
7742 return;
7744 id = c_parser_peek_token (parser)->value;
7745 objc_declare_class (id);
7746 c_parser_consume_token (parser);
7747 if (c_parser_next_token_is (parser, CPP_COMMA))
7748 c_parser_consume_token (parser);
7749 else
7750 break;
7752 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7755 /* Parse an objc-alias-declaration.
7757 objc-alias-declaration:
7758 @compatibility_alias identifier identifier ;
7761 static void
7762 c_parser_objc_alias_declaration (c_parser *parser)
7764 tree id1, id2;
7765 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7766 c_parser_consume_token (parser);
7767 if (c_parser_next_token_is_not (parser, CPP_NAME))
7769 c_parser_error (parser, "expected identifier");
7770 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7771 return;
7773 id1 = c_parser_peek_token (parser)->value;
7774 c_parser_consume_token (parser);
7775 if (c_parser_next_token_is_not (parser, CPP_NAME))
7777 c_parser_error (parser, "expected identifier");
7778 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7779 return;
7781 id2 = c_parser_peek_token (parser)->value;
7782 c_parser_consume_token (parser);
7783 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7784 objc_declare_alias (id1, id2);
7787 /* Parse an objc-protocol-definition.
7789 objc-protocol-definition:
7790 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7791 @protocol identifier-list ;
7793 "@protocol identifier ;" should be resolved as "@protocol
7794 identifier-list ;": objc-methodprotolist may not start with a
7795 semicolon in the first alternative if objc-protocol-refs are
7796 omitted. */
7798 static void
7799 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7801 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7803 c_parser_consume_token (parser);
7804 if (c_parser_next_token_is_not (parser, CPP_NAME))
7806 c_parser_error (parser, "expected identifier");
7807 return;
7809 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7810 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7812 /* Any identifiers, including those declared as type names, are
7813 OK here. */
7814 while (true)
7816 tree id;
7817 if (c_parser_next_token_is_not (parser, CPP_NAME))
7819 c_parser_error (parser, "expected identifier");
7820 break;
7822 id = c_parser_peek_token (parser)->value;
7823 objc_declare_protocol (id, attributes);
7824 c_parser_consume_token (parser);
7825 if (c_parser_next_token_is (parser, CPP_COMMA))
7826 c_parser_consume_token (parser);
7827 else
7828 break;
7830 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7832 else
7834 tree id = c_parser_peek_token (parser)->value;
7835 tree proto = NULL_TREE;
7836 c_parser_consume_token (parser);
7837 if (c_parser_next_token_is (parser, CPP_LESS))
7838 proto = c_parser_objc_protocol_refs (parser);
7839 parser->objc_pq_context = true;
7840 objc_start_protocol (id, proto, attributes);
7841 c_parser_objc_methodprotolist (parser);
7842 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7843 parser->objc_pq_context = false;
7844 objc_finish_interface ();
7848 /* Parse an objc-method-type.
7850 objc-method-type:
7854 Return true if it is a class method (+) and false if it is
7855 an instance method (-).
7857 static inline bool
7858 c_parser_objc_method_type (c_parser *parser)
7860 switch (c_parser_peek_token (parser)->type)
7862 case CPP_PLUS:
7863 c_parser_consume_token (parser);
7864 return true;
7865 case CPP_MINUS:
7866 c_parser_consume_token (parser);
7867 return false;
7868 default:
7869 gcc_unreachable ();
7873 /* Parse an objc-method-definition.
7875 objc-method-definition:
7876 objc-method-type objc-method-decl ;[opt] compound-statement
7879 static void
7880 c_parser_objc_method_definition (c_parser *parser)
7882 bool is_class_method = c_parser_objc_method_type (parser);
7883 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7884 parser->objc_pq_context = true;
7885 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7886 &expr);
7887 if (decl == error_mark_node)
7888 return; /* Bail here. */
7890 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7892 c_parser_consume_token (parser);
7893 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7894 "extra semicolon in method definition specified");
7897 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7899 c_parser_error (parser, "expected %<{%>");
7900 return;
7903 parser->objc_pq_context = false;
7904 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7906 add_stmt (c_parser_compound_statement (parser));
7907 objc_finish_method_definition (current_function_decl);
7909 else
7911 /* This code is executed when we find a method definition
7912 outside of an @implementation context (or invalid for other
7913 reasons). Parse the method (to keep going) but do not emit
7914 any code.
7916 c_parser_compound_statement (parser);
7920 /* Parse an objc-methodprotolist.
7922 objc-methodprotolist:
7923 empty
7924 objc-methodprotolist objc-methodproto
7925 objc-methodprotolist declaration
7926 objc-methodprotolist ;
7927 @optional
7928 @required
7930 The declaration is a data definition, which may be missing
7931 declaration specifiers under the same rules and diagnostics as
7932 other data definitions outside functions, and the stray semicolon
7933 is diagnosed the same way as a stray semicolon outside a
7934 function. */
7936 static void
7937 c_parser_objc_methodprotolist (c_parser *parser)
7939 while (true)
7941 /* The list is terminated by @end. */
7942 switch (c_parser_peek_token (parser)->type)
7944 case CPP_SEMICOLON:
7945 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7946 "ISO C does not allow extra %<;%> outside of a function");
7947 c_parser_consume_token (parser);
7948 break;
7949 case CPP_PLUS:
7950 case CPP_MINUS:
7951 c_parser_objc_methodproto (parser);
7952 break;
7953 case CPP_PRAGMA:
7954 c_parser_pragma (parser, pragma_external);
7955 break;
7956 case CPP_EOF:
7957 return;
7958 default:
7959 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7960 return;
7961 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7962 c_parser_objc_at_property_declaration (parser);
7963 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7965 objc_set_method_opt (true);
7966 c_parser_consume_token (parser);
7968 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7970 objc_set_method_opt (false);
7971 c_parser_consume_token (parser);
7973 else
7974 c_parser_declaration_or_fndef (parser, false, false, true,
7975 false, true, NULL);
7976 break;
7981 /* Parse an objc-methodproto.
7983 objc-methodproto:
7984 objc-method-type objc-method-decl ;
7987 static void
7988 c_parser_objc_methodproto (c_parser *parser)
7990 bool is_class_method = c_parser_objc_method_type (parser);
7991 tree decl, attributes = NULL_TREE;
7993 /* Remember protocol qualifiers in prototypes. */
7994 parser->objc_pq_context = true;
7995 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7996 NULL);
7997 /* Forget protocol qualifiers now. */
7998 parser->objc_pq_context = false;
8000 /* Do not allow the presence of attributes to hide an erroneous
8001 method implementation in the interface section. */
8002 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8004 c_parser_error (parser, "expected %<;%>");
8005 return;
8008 if (decl != error_mark_node)
8009 objc_add_method_declaration (is_class_method, decl, attributes);
8011 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8014 /* If we are at a position that method attributes may be present, check that
8015 there are not any parsed already (a syntax error) and then collect any
8016 specified at the current location. Finally, if new attributes were present,
8017 check that the next token is legal ( ';' for decls and '{' for defs). */
8019 static bool
8020 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8022 bool bad = false;
8023 if (*attributes)
8025 c_parser_error (parser,
8026 "method attributes must be specified at the end only");
8027 *attributes = NULL_TREE;
8028 bad = true;
8031 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8032 *attributes = c_parser_attributes (parser);
8034 /* If there were no attributes here, just report any earlier error. */
8035 if (*attributes == NULL_TREE || bad)
8036 return bad;
8038 /* If the attributes are followed by a ; or {, then just report any earlier
8039 error. */
8040 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8041 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8042 return bad;
8044 /* We've got attributes, but not at the end. */
8045 c_parser_error (parser,
8046 "expected %<;%> or %<{%> after method attribute definition");
8047 return true;
8050 /* Parse an objc-method-decl.
8052 objc-method-decl:
8053 ( objc-type-name ) objc-selector
8054 objc-selector
8055 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8056 objc-keyword-selector objc-optparmlist
8057 attributes
8059 objc-keyword-selector:
8060 objc-keyword-decl
8061 objc-keyword-selector objc-keyword-decl
8063 objc-keyword-decl:
8064 objc-selector : ( objc-type-name ) identifier
8065 objc-selector : identifier
8066 : ( objc-type-name ) identifier
8067 : identifier
8069 objc-optparmlist:
8070 objc-optparms objc-optellipsis
8072 objc-optparms:
8073 empty
8074 objc-opt-parms , parameter-declaration
8076 objc-optellipsis:
8077 empty
8078 , ...
8081 static tree
8082 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8083 tree *attributes, tree *expr)
8085 tree type = NULL_TREE;
8086 tree sel;
8087 tree parms = NULL_TREE;
8088 bool ellipsis = false;
8089 bool attr_err = false;
8091 *attributes = NULL_TREE;
8092 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8094 c_parser_consume_token (parser);
8095 type = c_parser_objc_type_name (parser);
8096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8098 sel = c_parser_objc_selector (parser);
8099 /* If there is no selector, or a colon follows, we have an
8100 objc-keyword-selector. If there is a selector, and a colon does
8101 not follow, that selector ends the objc-method-decl. */
8102 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8104 tree tsel = sel;
8105 tree list = NULL_TREE;
8106 while (true)
8108 tree atype = NULL_TREE, id, keyworddecl;
8109 tree param_attr = NULL_TREE;
8110 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8111 break;
8112 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8114 c_parser_consume_token (parser);
8115 atype = c_parser_objc_type_name (parser);
8116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8117 "expected %<)%>");
8119 /* New ObjC allows attributes on method parameters. */
8120 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8121 param_attr = c_parser_attributes (parser);
8122 if (c_parser_next_token_is_not (parser, CPP_NAME))
8124 c_parser_error (parser, "expected identifier");
8125 return error_mark_node;
8127 id = c_parser_peek_token (parser)->value;
8128 c_parser_consume_token (parser);
8129 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8130 list = chainon (list, keyworddecl);
8131 tsel = c_parser_objc_selector (parser);
8132 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8133 break;
8136 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8138 /* Parse the optional parameter list. Optional Objective-C
8139 method parameters follow the C syntax, and may include '...'
8140 to denote a variable number of arguments. */
8141 parms = make_node (TREE_LIST);
8142 while (c_parser_next_token_is (parser, CPP_COMMA))
8144 struct c_parm *parm;
8145 c_parser_consume_token (parser);
8146 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8148 ellipsis = true;
8149 c_parser_consume_token (parser);
8150 attr_err |= c_parser_objc_maybe_method_attributes
8151 (parser, attributes) ;
8152 break;
8154 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8155 if (parm == NULL)
8156 break;
8157 parms = chainon (parms,
8158 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8160 sel = list;
8162 else
8163 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8165 if (sel == NULL)
8167 c_parser_error (parser, "objective-c method declaration is expected");
8168 return error_mark_node;
8171 if (attr_err)
8172 return error_mark_node;
8174 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8177 /* Parse an objc-type-name.
8179 objc-type-name:
8180 objc-type-qualifiers[opt] type-name
8181 objc-type-qualifiers[opt]
8183 objc-type-qualifiers:
8184 objc-type-qualifier
8185 objc-type-qualifiers objc-type-qualifier
8187 objc-type-qualifier: one of
8188 in out inout bycopy byref oneway
8191 static tree
8192 c_parser_objc_type_name (c_parser *parser)
8194 tree quals = NULL_TREE;
8195 struct c_type_name *type_name = NULL;
8196 tree type = NULL_TREE;
8197 while (true)
8199 c_token *token = c_parser_peek_token (parser);
8200 if (token->type == CPP_KEYWORD
8201 && (token->keyword == RID_IN
8202 || token->keyword == RID_OUT
8203 || token->keyword == RID_INOUT
8204 || token->keyword == RID_BYCOPY
8205 || token->keyword == RID_BYREF
8206 || token->keyword == RID_ONEWAY))
8208 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8209 c_parser_consume_token (parser);
8211 else
8212 break;
8214 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8215 type_name = c_parser_type_name (parser);
8216 if (type_name)
8217 type = groktypename (type_name, NULL, NULL);
8219 /* If the type is unknown, and error has already been produced and
8220 we need to recover from the error. In that case, use NULL_TREE
8221 for the type, as if no type had been specified; this will use the
8222 default type ('id') which is good for error recovery. */
8223 if (type == error_mark_node)
8224 type = NULL_TREE;
8226 return build_tree_list (quals, type);
8229 /* Parse objc-protocol-refs.
8231 objc-protocol-refs:
8232 < identifier-list >
8235 static tree
8236 c_parser_objc_protocol_refs (c_parser *parser)
8238 tree list = NULL_TREE;
8239 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8240 c_parser_consume_token (parser);
8241 /* Any identifiers, including those declared as type names, are OK
8242 here. */
8243 while (true)
8245 tree id;
8246 if (c_parser_next_token_is_not (parser, CPP_NAME))
8248 c_parser_error (parser, "expected identifier");
8249 break;
8251 id = c_parser_peek_token (parser)->value;
8252 list = chainon (list, build_tree_list (NULL_TREE, id));
8253 c_parser_consume_token (parser);
8254 if (c_parser_next_token_is (parser, CPP_COMMA))
8255 c_parser_consume_token (parser);
8256 else
8257 break;
8259 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8260 return list;
8263 /* Parse an objc-try-catch-finally-statement.
8265 objc-try-catch-finally-statement:
8266 @try compound-statement objc-catch-list[opt]
8267 @try compound-statement objc-catch-list[opt] @finally compound-statement
8269 objc-catch-list:
8270 @catch ( objc-catch-parameter-declaration ) compound-statement
8271 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8273 objc-catch-parameter-declaration:
8274 parameter-declaration
8275 '...'
8277 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8279 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8280 for C++. Keep them in sync. */
8282 static void
8283 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8285 location_t location;
8286 tree stmt;
8288 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8289 c_parser_consume_token (parser);
8290 location = c_parser_peek_token (parser)->location;
8291 objc_maybe_warn_exceptions (location);
8292 stmt = c_parser_compound_statement (parser);
8293 objc_begin_try_stmt (location, stmt);
8295 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8297 struct c_parm *parm;
8298 tree parameter_declaration = error_mark_node;
8299 bool seen_open_paren = false;
8301 c_parser_consume_token (parser);
8302 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8303 seen_open_paren = true;
8304 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8306 /* We have "@catch (...)" (where the '...' are literally
8307 what is in the code). Skip the '...'.
8308 parameter_declaration is set to NULL_TREE, and
8309 objc_being_catch_clauses() knows that that means
8310 '...'. */
8311 c_parser_consume_token (parser);
8312 parameter_declaration = NULL_TREE;
8314 else
8316 /* We have "@catch (NSException *exception)" or something
8317 like that. Parse the parameter declaration. */
8318 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8319 if (parm == NULL)
8320 parameter_declaration = error_mark_node;
8321 else
8322 parameter_declaration = grokparm (parm, NULL);
8324 if (seen_open_paren)
8325 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8326 else
8328 /* If there was no open parenthesis, we are recovering from
8329 an error, and we are trying to figure out what mistake
8330 the user has made. */
8332 /* If there is an immediate closing parenthesis, the user
8333 probably forgot the opening one (ie, they typed "@catch
8334 NSException *e)". Parse the closing parenthesis and keep
8335 going. */
8336 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8337 c_parser_consume_token (parser);
8339 /* If these is no immediate closing parenthesis, the user
8340 probably doesn't know that parenthesis are required at
8341 all (ie, they typed "@catch NSException *e"). So, just
8342 forget about the closing parenthesis and keep going. */
8344 objc_begin_catch_clause (parameter_declaration);
8345 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8346 c_parser_compound_statement_nostart (parser);
8347 objc_finish_catch_clause ();
8349 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8351 c_parser_consume_token (parser);
8352 location = c_parser_peek_token (parser)->location;
8353 stmt = c_parser_compound_statement (parser);
8354 objc_build_finally_clause (location, stmt);
8356 objc_finish_try_stmt ();
8359 /* Parse an objc-synchronized-statement.
8361 objc-synchronized-statement:
8362 @synchronized ( expression ) compound-statement
8365 static void
8366 c_parser_objc_synchronized_statement (c_parser *parser)
8368 location_t loc;
8369 tree expr, stmt;
8370 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8371 c_parser_consume_token (parser);
8372 loc = c_parser_peek_token (parser)->location;
8373 objc_maybe_warn_exceptions (loc);
8374 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8376 expr = c_parser_expression (parser).value;
8377 expr = c_fully_fold (expr, false, NULL);
8378 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8380 else
8381 expr = error_mark_node;
8382 stmt = c_parser_compound_statement (parser);
8383 objc_build_synchronized (loc, expr, stmt);
8386 /* Parse an objc-selector; return NULL_TREE without an error if the
8387 next token is not an objc-selector.
8389 objc-selector:
8390 identifier
8391 one of
8392 enum struct union if else while do for switch case default
8393 break continue return goto asm sizeof typeof __alignof
8394 unsigned long const short volatile signed restrict _Complex
8395 in out inout bycopy byref oneway int char float double void _Bool
8397 ??? Why this selection of keywords but not, for example, storage
8398 class specifiers? */
8400 static tree
8401 c_parser_objc_selector (c_parser *parser)
8403 c_token *token = c_parser_peek_token (parser);
8404 tree value = token->value;
8405 if (token->type == CPP_NAME)
8407 c_parser_consume_token (parser);
8408 return value;
8410 if (token->type != CPP_KEYWORD)
8411 return NULL_TREE;
8412 switch (token->keyword)
8414 case RID_ENUM:
8415 case RID_STRUCT:
8416 case RID_UNION:
8417 case RID_IF:
8418 case RID_ELSE:
8419 case RID_WHILE:
8420 case RID_DO:
8421 case RID_FOR:
8422 case RID_SWITCH:
8423 case RID_CASE:
8424 case RID_DEFAULT:
8425 case RID_BREAK:
8426 case RID_CONTINUE:
8427 case RID_RETURN:
8428 case RID_GOTO:
8429 case RID_ASM:
8430 case RID_SIZEOF:
8431 case RID_TYPEOF:
8432 case RID_ALIGNOF:
8433 case RID_UNSIGNED:
8434 case RID_LONG:
8435 case RID_INT128:
8436 case RID_CONST:
8437 case RID_SHORT:
8438 case RID_VOLATILE:
8439 case RID_SIGNED:
8440 case RID_RESTRICT:
8441 case RID_COMPLEX:
8442 case RID_IN:
8443 case RID_OUT:
8444 case RID_INOUT:
8445 case RID_BYCOPY:
8446 case RID_BYREF:
8447 case RID_ONEWAY:
8448 case RID_INT:
8449 case RID_CHAR:
8450 case RID_FLOAT:
8451 case RID_DOUBLE:
8452 case RID_VOID:
8453 case RID_BOOL:
8454 c_parser_consume_token (parser);
8455 return value;
8456 default:
8457 return NULL_TREE;
8461 /* Parse an objc-selector-arg.
8463 objc-selector-arg:
8464 objc-selector
8465 objc-keywordname-list
8467 objc-keywordname-list:
8468 objc-keywordname
8469 objc-keywordname-list objc-keywordname
8471 objc-keywordname:
8472 objc-selector :
8476 static tree
8477 c_parser_objc_selector_arg (c_parser *parser)
8479 tree sel = c_parser_objc_selector (parser);
8480 tree list = NULL_TREE;
8481 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8482 return sel;
8483 while (true)
8485 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8486 return list;
8487 list = chainon (list, build_tree_list (sel, NULL_TREE));
8488 sel = c_parser_objc_selector (parser);
8489 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8490 break;
8492 return list;
8495 /* Parse an objc-receiver.
8497 objc-receiver:
8498 expression
8499 class-name
8500 type-name
8503 static tree
8504 c_parser_objc_receiver (c_parser *parser)
8506 if (c_parser_peek_token (parser)->type == CPP_NAME
8507 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8508 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8510 tree id = c_parser_peek_token (parser)->value;
8511 c_parser_consume_token (parser);
8512 return objc_get_class_reference (id);
8514 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8517 /* Parse objc-message-args.
8519 objc-message-args:
8520 objc-selector
8521 objc-keywordarg-list
8523 objc-keywordarg-list:
8524 objc-keywordarg
8525 objc-keywordarg-list objc-keywordarg
8527 objc-keywordarg:
8528 objc-selector : objc-keywordexpr
8529 : objc-keywordexpr
8532 static tree
8533 c_parser_objc_message_args (c_parser *parser)
8535 tree sel = c_parser_objc_selector (parser);
8536 tree list = NULL_TREE;
8537 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8538 return sel;
8539 while (true)
8541 tree keywordexpr;
8542 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8543 return error_mark_node;
8544 keywordexpr = c_parser_objc_keywordexpr (parser);
8545 list = chainon (list, build_tree_list (sel, keywordexpr));
8546 sel = c_parser_objc_selector (parser);
8547 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8548 break;
8550 return list;
8553 /* Parse an objc-keywordexpr.
8555 objc-keywordexpr:
8556 nonempty-expr-list
8559 static tree
8560 c_parser_objc_keywordexpr (c_parser *parser)
8562 tree ret;
8563 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
8564 NULL, NULL, NULL);
8565 if (vec_safe_length (expr_list) == 1)
8567 /* Just return the expression, remove a level of
8568 indirection. */
8569 ret = (*expr_list)[0];
8571 else
8573 /* We have a comma expression, we will collapse later. */
8574 ret = build_tree_list_vec (expr_list);
8576 release_tree_vector (expr_list);
8577 return ret;
8580 /* A check, needed in several places, that ObjC interface, implementation or
8581 method definitions are not prefixed by incorrect items. */
8582 static bool
8583 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8584 struct c_declspecs *specs)
8586 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8587 || specs->typespec_kind != ctsk_none)
8589 c_parser_error (parser,
8590 "no type or storage class may be specified here,");
8591 c_parser_skip_to_end_of_block_or_statement (parser);
8592 return true;
8594 return false;
8597 /* Parse an Objective-C @property declaration. The syntax is:
8599 objc-property-declaration:
8600 '@property' objc-property-attributes[opt] struct-declaration ;
8602 objc-property-attributes:
8603 '(' objc-property-attribute-list ')'
8605 objc-property-attribute-list:
8606 objc-property-attribute
8607 objc-property-attribute-list, objc-property-attribute
8609 objc-property-attribute
8610 'getter' = identifier
8611 'setter' = identifier
8612 'readonly'
8613 'readwrite'
8614 'assign'
8615 'retain'
8616 'copy'
8617 'nonatomic'
8619 For example:
8620 @property NSString *name;
8621 @property (readonly) id object;
8622 @property (retain, nonatomic, getter=getTheName) id name;
8623 @property int a, b, c;
8625 PS: This function is identical to cp_parser_objc_at_propery_declaration
8626 for C++. Keep them in sync. */
8627 static void
8628 c_parser_objc_at_property_declaration (c_parser *parser)
8630 /* The following variables hold the attributes of the properties as
8631 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8632 seen. When we see an attribute, we set them to 'true' (if they
8633 are boolean properties) or to the identifier (if they have an
8634 argument, ie, for getter and setter). Note that here we only
8635 parse the list of attributes, check the syntax and accumulate the
8636 attributes that we find. objc_add_property_declaration() will
8637 then process the information. */
8638 bool property_assign = false;
8639 bool property_copy = false;
8640 tree property_getter_ident = NULL_TREE;
8641 bool property_nonatomic = false;
8642 bool property_readonly = false;
8643 bool property_readwrite = false;
8644 bool property_retain = false;
8645 tree property_setter_ident = NULL_TREE;
8647 /* 'properties' is the list of properties that we read. Usually a
8648 single one, but maybe more (eg, in "@property int a, b, c;" there
8649 are three). */
8650 tree properties;
8651 location_t loc;
8653 loc = c_parser_peek_token (parser)->location;
8654 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8656 c_parser_consume_token (parser); /* Eat '@property'. */
8658 /* Parse the optional attribute list... */
8659 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8661 /* Eat the '(' */
8662 c_parser_consume_token (parser);
8664 /* Property attribute keywords are valid now. */
8665 parser->objc_property_attr_context = true;
8667 while (true)
8669 bool syntax_error = false;
8670 c_token *token = c_parser_peek_token (parser);
8671 enum rid keyword;
8673 if (token->type != CPP_KEYWORD)
8675 if (token->type == CPP_CLOSE_PAREN)
8676 c_parser_error (parser, "expected identifier");
8677 else
8679 c_parser_consume_token (parser);
8680 c_parser_error (parser, "unknown property attribute");
8682 break;
8684 keyword = token->keyword;
8685 c_parser_consume_token (parser);
8686 switch (keyword)
8688 case RID_ASSIGN: property_assign = true; break;
8689 case RID_COPY: property_copy = true; break;
8690 case RID_NONATOMIC: property_nonatomic = true; break;
8691 case RID_READONLY: property_readonly = true; break;
8692 case RID_READWRITE: property_readwrite = true; break;
8693 case RID_RETAIN: property_retain = true; break;
8695 case RID_GETTER:
8696 case RID_SETTER:
8697 if (c_parser_next_token_is_not (parser, CPP_EQ))
8699 if (keyword == RID_GETTER)
8700 c_parser_error (parser,
8701 "missing %<=%> (after %<getter%> attribute)");
8702 else
8703 c_parser_error (parser,
8704 "missing %<=%> (after %<setter%> attribute)");
8705 syntax_error = true;
8706 break;
8708 c_parser_consume_token (parser); /* eat the = */
8709 if (c_parser_next_token_is_not (parser, CPP_NAME))
8711 c_parser_error (parser, "expected identifier");
8712 syntax_error = true;
8713 break;
8715 if (keyword == RID_SETTER)
8717 if (property_setter_ident != NULL_TREE)
8718 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8719 else
8720 property_setter_ident = c_parser_peek_token (parser)->value;
8721 c_parser_consume_token (parser);
8722 if (c_parser_next_token_is_not (parser, CPP_COLON))
8723 c_parser_error (parser, "setter name must terminate with %<:%>");
8724 else
8725 c_parser_consume_token (parser);
8727 else
8729 if (property_getter_ident != NULL_TREE)
8730 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8731 else
8732 property_getter_ident = c_parser_peek_token (parser)->value;
8733 c_parser_consume_token (parser);
8735 break;
8736 default:
8737 c_parser_error (parser, "unknown property attribute");
8738 syntax_error = true;
8739 break;
8742 if (syntax_error)
8743 break;
8745 if (c_parser_next_token_is (parser, CPP_COMMA))
8746 c_parser_consume_token (parser);
8747 else
8748 break;
8750 parser->objc_property_attr_context = false;
8751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8753 /* ... and the property declaration(s). */
8754 properties = c_parser_struct_declaration (parser);
8756 if (properties == error_mark_node)
8758 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8759 parser->error = false;
8760 return;
8763 if (properties == NULL_TREE)
8764 c_parser_error (parser, "expected identifier");
8765 else
8767 /* Comma-separated properties are chained together in
8768 reverse order; add them one by one. */
8769 properties = nreverse (properties);
8771 for (; properties; properties = TREE_CHAIN (properties))
8772 objc_add_property_declaration (loc, copy_node (properties),
8773 property_readonly, property_readwrite,
8774 property_assign, property_retain,
8775 property_copy, property_nonatomic,
8776 property_getter_ident, property_setter_ident);
8779 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8780 parser->error = false;
8783 /* Parse an Objective-C @synthesize declaration. The syntax is:
8785 objc-synthesize-declaration:
8786 @synthesize objc-synthesize-identifier-list ;
8788 objc-synthesize-identifier-list:
8789 objc-synthesize-identifier
8790 objc-synthesize-identifier-list, objc-synthesize-identifier
8792 objc-synthesize-identifier
8793 identifier
8794 identifier = identifier
8796 For example:
8797 @synthesize MyProperty;
8798 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8800 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8801 for C++. Keep them in sync.
8803 static void
8804 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8806 tree list = NULL_TREE;
8807 location_t loc;
8808 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8809 loc = c_parser_peek_token (parser)->location;
8811 c_parser_consume_token (parser);
8812 while (true)
8814 tree property, ivar;
8815 if (c_parser_next_token_is_not (parser, CPP_NAME))
8817 c_parser_error (parser, "expected identifier");
8818 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8819 /* Once we find the semicolon, we can resume normal parsing.
8820 We have to reset parser->error manually because
8821 c_parser_skip_until_found() won't reset it for us if the
8822 next token is precisely a semicolon. */
8823 parser->error = false;
8824 return;
8826 property = c_parser_peek_token (parser)->value;
8827 c_parser_consume_token (parser);
8828 if (c_parser_next_token_is (parser, CPP_EQ))
8830 c_parser_consume_token (parser);
8831 if (c_parser_next_token_is_not (parser, CPP_NAME))
8833 c_parser_error (parser, "expected identifier");
8834 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8835 parser->error = false;
8836 return;
8838 ivar = c_parser_peek_token (parser)->value;
8839 c_parser_consume_token (parser);
8841 else
8842 ivar = NULL_TREE;
8843 list = chainon (list, build_tree_list (ivar, property));
8844 if (c_parser_next_token_is (parser, CPP_COMMA))
8845 c_parser_consume_token (parser);
8846 else
8847 break;
8849 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8850 objc_add_synthesize_declaration (loc, list);
8853 /* Parse an Objective-C @dynamic declaration. The syntax is:
8855 objc-dynamic-declaration:
8856 @dynamic identifier-list ;
8858 For example:
8859 @dynamic MyProperty;
8860 @dynamic MyProperty, AnotherProperty;
8862 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8863 for C++. Keep them in sync.
8865 static void
8866 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8868 tree list = NULL_TREE;
8869 location_t loc;
8870 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8871 loc = c_parser_peek_token (parser)->location;
8873 c_parser_consume_token (parser);
8874 while (true)
8876 tree property;
8877 if (c_parser_next_token_is_not (parser, CPP_NAME))
8879 c_parser_error (parser, "expected identifier");
8880 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8881 parser->error = false;
8882 return;
8884 property = c_parser_peek_token (parser)->value;
8885 list = chainon (list, build_tree_list (NULL_TREE, property));
8886 c_parser_consume_token (parser);
8887 if (c_parser_next_token_is (parser, CPP_COMMA))
8888 c_parser_consume_token (parser);
8889 else
8890 break;
8892 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8893 objc_add_dynamic_declaration (loc, list);
8896 /* Parse UPC shared qualifier
8898 shared-type-qualifier: shared layout-qualifier-opt
8899 layout-qualifier: [ constant-expression-opt ] | [ * ]
8902 static void
8903 c_parser_upc_shared_qual (source_location loc,
8904 c_parser *parser,
8905 struct c_declspecs *specs)
8907 tree array_qual, arg1;
8909 /* consume "shared" part */
8910 c_parser_consume_token (parser);
8912 /* check for shared array layout specifier */
8913 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
8915 declspecs_add_qual (loc, specs, ridpointers[RID_SHARED]);
8916 return;
8918 c_parser_consume_token (parser);
8919 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8921 /* [] layout specifier */
8922 arg1 = size_zero_node;
8924 else if (c_parser_next_token_is (parser, CPP_MULT))
8926 /* [*] layout specifier */
8927 arg1 = build1 (INDIRECT_REF, NULL_TREE, NULL_TREE);
8928 c_parser_consume_token (parser);
8930 else
8932 /* [ expression ] layout specifier */
8933 arg1 = c_parser_expression (parser).value;
8935 array_qual = build4 (ARRAY_REF, NULL_TREE, NULL_TREE,
8936 arg1, NULL_TREE, NULL_TREE);
8937 declspecs_add_qual (loc, specs, array_qual);
8939 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8941 c_parser_error (parser, "expected ]");
8943 c_parser_consume_token (parser);
8946 /* Parse a UPC upc_forall statement
8948 upc_forall-statement:
8949 upc_forall ( expression[opt] ; expression[opt] ;
8950 expression[opt] ; affinity[opt] ) statement
8951 affinity: experssion | continue */
8953 static void
8954 c_parser_upc_forall_statement (c_parser *parser)
8956 tree block, cond, incr, save_break, save_cont, body;
8957 tree affinity;
8958 location_t loc = c_parser_peek_token (parser)->location;
8959 location_t affinity_loc = UNKNOWN_LOCATION;
8960 const int profile_upc_forall = flag_upc_instrument && get_upc_pupc_mode();
8961 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_FORALL));
8962 c_parser_consume_token (parser);
8963 block = c_begin_compound_stmt (flag_isoc99);
8964 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8966 /* Parse the initialization declaration or expression. */
8967 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8969 c_parser_consume_token (parser);
8970 c_finish_expr_stmt (loc, NULL_TREE);
8972 else if (c_parser_next_token_starts_declspecs (parser))
8974 c_parser_declaration_or_fndef (parser, true, true, true,
8975 true, true, NULL);
8976 check_for_loop_decls (loc, true);
8978 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
8980 /* __extension__ can start a declaration, but is also an
8981 unary operator that can start an expression. Consume all
8982 but the last of a possible series of __extension__ to
8983 determine which. */
8984 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
8985 && (c_parser_peek_2nd_token (parser)->keyword
8986 == RID_EXTENSION))
8987 c_parser_consume_token (parser);
8988 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
8990 int ext;
8991 ext = disable_extension_diagnostics ();
8992 c_parser_consume_token (parser);
8993 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
8994 restore_extension_diagnostics (ext);
8995 check_for_loop_decls (loc, true);
8997 else
8998 goto init_expr;
9000 else
9002 init_expr:
9003 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
9004 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9006 /* Parse the loop condition. */
9007 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9009 c_parser_consume_token (parser);
9010 cond = NULL_TREE;
9012 else
9014 cond = c_parser_condition (parser);
9015 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9017 /* Parse the increment expression. */
9018 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9019 incr = c_process_expr_stmt (loc, NULL_TREE);
9020 else
9021 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
9022 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9023 /* Parse the UPC affinity expression. */
9024 affinity_loc = c_parser_peek_token (parser)->location;
9025 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9027 affinity = NULL_TREE;
9029 else if (c_parser_peek_token (parser)->type == CPP_KEYWORD
9030 && c_parser_peek_token (parser)->keyword == RID_CONTINUE)
9032 affinity = NULL_TREE;
9033 c_parser_consume_token (parser);
9035 else
9037 affinity = c_parser_expression_conv (parser).value;
9038 affinity = c_fully_fold (affinity, false, NULL);
9040 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9041 if (affinity)
9042 affinity = upc_affinity_test (affinity_loc, affinity);
9044 else
9046 cond = error_mark_node;
9047 incr = error_mark_node;
9048 affinity = error_mark_node;
9050 save_break = c_break_label;
9051 c_break_label = NULL_TREE;
9052 save_cont = c_cont_label;
9053 c_cont_label = NULL_TREE;
9054 body = c_parser_c99_block_statement (parser);
9055 if (profile_upc_forall)
9057 const tree gasp_start = upc_instrument_forall (loc, 1 /* start */);
9058 add_stmt (gasp_start);
9060 loc = c_parser_peek_token (parser)->location;
9061 if (affinity != NULL_TREE && affinity != error_mark_node)
9063 tree upc_forall_depth = upc_rts_forall_depth_var ();
9064 tree inc_depth, depth_gt_one;
9065 inc_depth = build_unary_op (loc, PREINCREMENT_EXPR, upc_forall_depth, 0);
9066 c_finish_expr_stmt (loc, inc_depth);
9067 depth_gt_one = build_binary_op (affinity_loc,
9068 GT_EXPR, upc_forall_depth, integer_one_node, 0);
9069 depth_gt_one = c_objc_common_truthvalue_conversion (affinity_loc, depth_gt_one);
9070 depth_gt_one = c_fully_fold (depth_gt_one, false, NULL);
9071 affinity = build_binary_op (affinity_loc, TRUTH_OR_EXPR,
9072 depth_gt_one, affinity, 0);
9073 body = build3 (COND_EXPR, void_type_node, affinity,
9074 body, NULL_TREE);
9075 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
9076 c_finish_expr_stmt (loc,
9077 build_unary_op (loc, PREDECREMENT_EXPR, upc_forall_depth, 0));
9079 else
9080 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
9081 if (profile_upc_forall)
9083 const tree gasp_end = upc_instrument_forall (loc, 0 /* start */);
9084 add_stmt (gasp_end);
9086 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
9087 c_break_label = save_break;
9088 c_cont_label = save_cont;
9091 /* Parse an upc-sync-statement.
9093 upc_barrier, upc_wait, upc_notify
9096 static void
9097 c_parser_upc_sync_statement (c_parser *parser, int sync_kind)
9099 location_t loc;
9100 tree expr = NULL_TREE;
9101 tree stmt;
9102 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_BARRIER) ||
9103 c_parser_next_token_is_keyword (parser, RID_UPC_NOTIFY) ||
9104 c_parser_next_token_is_keyword (parser, RID_UPC_WAIT));
9105 loc = c_parser_peek_token (parser)->location;
9106 c_parser_consume_token (parser);
9107 if (c_parser_peek_token (parser)->type != CPP_SEMICOLON)
9109 loc = c_parser_peek_token (parser)->location;
9110 expr = c_parser_expression (parser).value;
9111 if (expr == error_mark_node)
9112 expr = NULL;
9114 stmt = size_int (sync_kind);
9115 (void) upc_build_sync_stmt (loc, stmt, expr);
9119 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9120 should be considered, statements. ALLOW_STMT is true if we're within
9121 the context of a function and such pragmas are to be allowed. Returns
9122 true if we actually parsed such a pragma. */
9124 static bool
9125 c_parser_pragma (c_parser *parser, enum pragma_context context)
9127 unsigned int id;
9129 id = c_parser_peek_token (parser)->pragma_kind;
9130 gcc_assert (id != PRAGMA_NONE);
9132 switch (id)
9134 case PRAGMA_OMP_BARRIER:
9135 if (context != pragma_compound)
9137 if (context == pragma_stmt)
9138 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9139 "used in compound statements");
9140 goto bad_stmt;
9142 c_parser_omp_barrier (parser);
9143 return false;
9145 case PRAGMA_OMP_FLUSH:
9146 if (context != pragma_compound)
9148 if (context == pragma_stmt)
9149 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9150 "used in compound statements");
9151 goto bad_stmt;
9153 c_parser_omp_flush (parser);
9154 return false;
9156 case PRAGMA_OMP_TASKWAIT:
9157 if (context != pragma_compound)
9159 if (context == pragma_stmt)
9160 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9161 "used in compound statements");
9162 goto bad_stmt;
9164 c_parser_omp_taskwait (parser);
9165 return false;
9167 case PRAGMA_OMP_TASKYIELD:
9168 if (context != pragma_compound)
9170 if (context == pragma_stmt)
9171 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9172 "used in compound statements");
9173 goto bad_stmt;
9175 c_parser_omp_taskyield (parser);
9176 return false;
9178 case PRAGMA_OMP_THREADPRIVATE:
9179 c_parser_omp_threadprivate (parser);
9180 return false;
9182 case PRAGMA_OMP_SECTION:
9183 error_at (c_parser_peek_token (parser)->location,
9184 "%<#pragma omp section%> may only be used in "
9185 "%<#pragma omp sections%> construct");
9186 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9187 return false;
9189 case PRAGMA_GCC_PCH_PREPROCESS:
9190 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9191 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9192 return false;
9194 default:
9195 if (id < PRAGMA_FIRST_EXTERNAL)
9197 if (context == pragma_external)
9199 bad_stmt:
9200 c_parser_error (parser, "expected declaration specifiers");
9201 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9202 return false;
9204 c_parser_omp_construct (parser);
9205 return true;
9207 break;
9210 c_parser_consume_pragma (parser);
9211 c_invoke_pragma_handler (id);
9213 /* Skip to EOL, but suppress any error message. Those will have been
9214 generated by the handler routine through calling error, as opposed
9215 to calling c_parser_error. */
9216 parser->error = true;
9217 c_parser_skip_to_pragma_eol (parser);
9219 return false;
9222 /* The interface the pragma parsers have to the lexer. */
9224 enum cpp_ttype
9225 pragma_lex (tree *value)
9227 c_token *tok = c_parser_peek_token (the_parser);
9228 enum cpp_ttype ret = tok->type;
9230 *value = tok->value;
9231 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9232 ret = CPP_EOF;
9233 else
9235 if (ret == CPP_KEYWORD)
9236 ret = CPP_NAME;
9237 c_parser_consume_token (the_parser);
9240 return ret;
9243 static void
9244 c_parser_pragma_pch_preprocess (c_parser *parser)
9246 tree name = NULL;
9248 c_parser_consume_pragma (parser);
9249 if (c_parser_next_token_is (parser, CPP_STRING))
9251 name = c_parser_peek_token (parser)->value;
9252 c_parser_consume_token (parser);
9254 else
9255 c_parser_error (parser, "expected string literal");
9256 c_parser_skip_to_pragma_eol (parser);
9258 if (name)
9259 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9262 /* OpenMP 2.5 parsing routines. */
9264 /* Returns name of the next clause.
9265 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9266 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9267 returned and the token is consumed. */
9269 static pragma_omp_clause
9270 c_parser_omp_clause_name (c_parser *parser)
9272 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9274 if (c_parser_next_token_is_keyword (parser, RID_IF))
9275 result = PRAGMA_OMP_CLAUSE_IF;
9276 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9277 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9278 else if (c_parser_next_token_is (parser, CPP_NAME))
9280 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9282 switch (p[0])
9284 case 'c':
9285 if (!strcmp ("collapse", p))
9286 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9287 else if (!strcmp ("copyin", p))
9288 result = PRAGMA_OMP_CLAUSE_COPYIN;
9289 else if (!strcmp ("copyprivate", p))
9290 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9291 break;
9292 case 'f':
9293 if (!strcmp ("final", p))
9294 result = PRAGMA_OMP_CLAUSE_FINAL;
9295 else if (!strcmp ("firstprivate", p))
9296 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9297 break;
9298 case 'l':
9299 if (!strcmp ("lastprivate", p))
9300 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9301 break;
9302 case 'm':
9303 if (!strcmp ("mergeable", p))
9304 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9305 break;
9306 case 'n':
9307 if (!strcmp ("nowait", p))
9308 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9309 else if (!strcmp ("num_threads", p))
9310 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9311 break;
9312 case 'o':
9313 if (!strcmp ("ordered", p))
9314 result = PRAGMA_OMP_CLAUSE_ORDERED;
9315 break;
9316 case 'p':
9317 if (!strcmp ("private", p))
9318 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9319 break;
9320 case 'r':
9321 if (!strcmp ("reduction", p))
9322 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9323 break;
9324 case 's':
9325 if (!strcmp ("schedule", p))
9326 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9327 else if (!strcmp ("shared", p))
9328 result = PRAGMA_OMP_CLAUSE_SHARED;
9329 break;
9330 case 'u':
9331 if (!strcmp ("untied", p))
9332 result = PRAGMA_OMP_CLAUSE_UNTIED;
9333 break;
9337 if (result != PRAGMA_OMP_CLAUSE_NONE)
9338 c_parser_consume_token (parser);
9340 return result;
9343 /* Validate that a clause of the given type does not already exist. */
9345 static void
9346 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9347 const char *name)
9349 tree c;
9351 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9352 if (OMP_CLAUSE_CODE (c) == code)
9354 location_t loc = OMP_CLAUSE_LOCATION (c);
9355 error_at (loc, "too many %qs clauses", name);
9356 break;
9360 /* OpenMP 2.5:
9361 variable-list:
9362 identifier
9363 variable-list , identifier
9365 If KIND is nonzero, create the appropriate node and install the
9366 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9367 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9369 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9370 return the list created. */
9372 static tree
9373 c_parser_omp_variable_list (c_parser *parser,
9374 location_t clause_loc,
9375 enum omp_clause_code kind,
9376 tree list)
9378 if (c_parser_next_token_is_not (parser, CPP_NAME)
9379 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9380 c_parser_error (parser, "expected identifier");
9382 while (c_parser_next_token_is (parser, CPP_NAME)
9383 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9385 tree t = lookup_name (c_parser_peek_token (parser)->value);
9387 if (t == NULL_TREE)
9388 undeclared_variable (c_parser_peek_token (parser)->location,
9389 c_parser_peek_token (parser)->value);
9390 else if (t == error_mark_node)
9392 else if (kind != 0)
9394 tree u = build_omp_clause (clause_loc, kind);
9395 OMP_CLAUSE_DECL (u) = t;
9396 OMP_CLAUSE_CHAIN (u) = list;
9397 list = u;
9399 else
9400 list = tree_cons (t, NULL_TREE, list);
9402 c_parser_consume_token (parser);
9404 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9405 break;
9407 c_parser_consume_token (parser);
9410 return list;
9413 /* Similarly, but expect leading and trailing parenthesis. This is a very
9414 common case for omp clauses. */
9416 static tree
9417 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9418 tree list)
9420 /* The clauses location. */
9421 location_t loc = c_parser_peek_token (parser)->location;
9423 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9425 list = c_parser_omp_variable_list (parser, loc, kind, list);
9426 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9428 return list;
9431 /* OpenMP 3.0:
9432 collapse ( constant-expression ) */
9434 static tree
9435 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9437 tree c, num = error_mark_node;
9438 HOST_WIDE_INT n;
9439 location_t loc;
9441 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9443 loc = c_parser_peek_token (parser)->location;
9444 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9446 num = c_parser_expr_no_commas (parser, NULL).value;
9447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9449 if (num == error_mark_node)
9450 return list;
9451 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9452 || !host_integerp (num, 0)
9453 || (n = tree_low_cst (num, 0)) <= 0
9454 || (int) n != n)
9456 error_at (loc,
9457 "collapse argument needs positive constant integer expression");
9458 return list;
9460 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9461 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9462 OMP_CLAUSE_CHAIN (c) = list;
9463 return c;
9466 /* OpenMP 2.5:
9467 copyin ( variable-list ) */
9469 static tree
9470 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9472 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9475 /* OpenMP 2.5:
9476 copyprivate ( variable-list ) */
9478 static tree
9479 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9481 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9484 /* OpenMP 2.5:
9485 default ( shared | none ) */
9487 static tree
9488 c_parser_omp_clause_default (c_parser *parser, tree list)
9490 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9491 location_t loc = c_parser_peek_token (parser)->location;
9492 tree c;
9494 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9495 return list;
9496 if (c_parser_next_token_is (parser, CPP_NAME))
9498 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9500 switch (p[0])
9502 case 'n':
9503 if (strcmp ("none", p) != 0)
9504 goto invalid_kind;
9505 kind = OMP_CLAUSE_DEFAULT_NONE;
9506 break;
9508 case 's':
9509 if (strcmp ("shared", p) != 0)
9510 goto invalid_kind;
9511 kind = OMP_CLAUSE_DEFAULT_SHARED;
9512 break;
9514 default:
9515 goto invalid_kind;
9518 c_parser_consume_token (parser);
9520 else
9522 invalid_kind:
9523 c_parser_error (parser, "expected %<none%> or %<shared%>");
9525 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9527 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9528 return list;
9530 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9531 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9532 OMP_CLAUSE_CHAIN (c) = list;
9533 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9535 return c;
9538 /* OpenMP 2.5:
9539 firstprivate ( variable-list ) */
9541 static tree
9542 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9544 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9547 /* OpenMP 3.1:
9548 final ( expression ) */
9550 static tree
9551 c_parser_omp_clause_final (c_parser *parser, tree list)
9553 location_t loc = c_parser_peek_token (parser)->location;
9554 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9556 tree t = c_parser_paren_condition (parser);
9557 tree c;
9559 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9561 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9562 OMP_CLAUSE_FINAL_EXPR (c) = t;
9563 OMP_CLAUSE_CHAIN (c) = list;
9564 list = c;
9566 else
9567 c_parser_error (parser, "expected %<(%>");
9569 return list;
9572 /* OpenMP 2.5:
9573 if ( expression ) */
9575 static tree
9576 c_parser_omp_clause_if (c_parser *parser, tree list)
9578 location_t loc = c_parser_peek_token (parser)->location;
9579 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9581 tree t = c_parser_paren_condition (parser);
9582 tree c;
9584 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9586 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9587 OMP_CLAUSE_IF_EXPR (c) = t;
9588 OMP_CLAUSE_CHAIN (c) = list;
9589 list = c;
9591 else
9592 c_parser_error (parser, "expected %<(%>");
9594 return list;
9597 /* OpenMP 2.5:
9598 lastprivate ( variable-list ) */
9600 static tree
9601 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9603 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9606 /* OpenMP 3.1:
9607 mergeable */
9609 static tree
9610 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9612 tree c;
9614 /* FIXME: Should we allow duplicates? */
9615 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9617 c = build_omp_clause (c_parser_peek_token (parser)->location,
9618 OMP_CLAUSE_MERGEABLE);
9619 OMP_CLAUSE_CHAIN (c) = list;
9621 return c;
9624 /* OpenMP 2.5:
9625 nowait */
9627 static tree
9628 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9630 tree c;
9631 location_t loc = c_parser_peek_token (parser)->location;
9633 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9635 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9636 OMP_CLAUSE_CHAIN (c) = list;
9637 return c;
9640 /* OpenMP 2.5:
9641 num_threads ( expression ) */
9643 static tree
9644 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9646 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9647 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9649 location_t expr_loc = c_parser_peek_token (parser)->location;
9650 tree c, t = c_parser_expression (parser).value;
9651 mark_exp_read (t);
9652 t = c_fully_fold (t, false, NULL);
9654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9656 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9658 c_parser_error (parser, "expected integer expression");
9659 return list;
9662 /* Attempt to statically determine when the number isn't positive. */
9663 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9664 build_int_cst (TREE_TYPE (t), 0));
9665 if (CAN_HAVE_LOCATION_P (c))
9666 SET_EXPR_LOCATION (c, expr_loc);
9667 if (c == boolean_true_node)
9669 warning_at (expr_loc, 0,
9670 "%<num_threads%> value must be positive");
9671 t = integer_one_node;
9674 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9676 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9677 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9678 OMP_CLAUSE_CHAIN (c) = list;
9679 list = c;
9682 return list;
9685 /* OpenMP 2.5:
9686 ordered */
9688 static tree
9689 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9691 tree c;
9693 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9695 c = build_omp_clause (c_parser_peek_token (parser)->location,
9696 OMP_CLAUSE_ORDERED);
9697 OMP_CLAUSE_CHAIN (c) = list;
9699 return c;
9702 /* OpenMP 2.5:
9703 private ( variable-list ) */
9705 static tree
9706 c_parser_omp_clause_private (c_parser *parser, tree list)
9708 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9711 /* OpenMP 2.5:
9712 reduction ( reduction-operator : variable-list )
9714 reduction-operator:
9715 One of: + * - & ^ | && ||
9717 OpenMP 3.1:
9719 reduction-operator:
9720 One of: + * - & ^ | && || max min */
9722 static tree
9723 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9725 location_t clause_loc = c_parser_peek_token (parser)->location;
9726 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9728 enum tree_code code;
9730 switch (c_parser_peek_token (parser)->type)
9732 case CPP_PLUS:
9733 code = PLUS_EXPR;
9734 break;
9735 case CPP_MULT:
9736 code = MULT_EXPR;
9737 break;
9738 case CPP_MINUS:
9739 code = MINUS_EXPR;
9740 break;
9741 case CPP_AND:
9742 code = BIT_AND_EXPR;
9743 break;
9744 case CPP_XOR:
9745 code = BIT_XOR_EXPR;
9746 break;
9747 case CPP_OR:
9748 code = BIT_IOR_EXPR;
9749 break;
9750 case CPP_AND_AND:
9751 code = TRUTH_ANDIF_EXPR;
9752 break;
9753 case CPP_OR_OR:
9754 code = TRUTH_ORIF_EXPR;
9755 break;
9756 case CPP_NAME:
9758 const char *p
9759 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9760 if (strcmp (p, "min") == 0)
9762 code = MIN_EXPR;
9763 break;
9765 if (strcmp (p, "max") == 0)
9767 code = MAX_EXPR;
9768 break;
9771 /* FALLTHRU */
9772 default:
9773 c_parser_error (parser,
9774 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9775 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9777 return list;
9779 c_parser_consume_token (parser);
9780 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9782 tree nl, c;
9784 nl = c_parser_omp_variable_list (parser, clause_loc,
9785 OMP_CLAUSE_REDUCTION, list);
9786 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9787 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9789 list = nl;
9791 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9793 return list;
9796 /* OpenMP 2.5:
9797 schedule ( schedule-kind )
9798 schedule ( schedule-kind , expression )
9800 schedule-kind:
9801 static | dynamic | guided | runtime | auto
9804 static tree
9805 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9807 tree c, t;
9808 location_t loc = c_parser_peek_token (parser)->location;
9810 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9811 return list;
9813 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9815 if (c_parser_next_token_is (parser, CPP_NAME))
9817 tree kind = c_parser_peek_token (parser)->value;
9818 const char *p = IDENTIFIER_POINTER (kind);
9820 switch (p[0])
9822 case 'd':
9823 if (strcmp ("dynamic", p) != 0)
9824 goto invalid_kind;
9825 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9826 break;
9828 case 'g':
9829 if (strcmp ("guided", p) != 0)
9830 goto invalid_kind;
9831 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9832 break;
9834 case 'r':
9835 if (strcmp ("runtime", p) != 0)
9836 goto invalid_kind;
9837 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9838 break;
9840 default:
9841 goto invalid_kind;
9844 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9845 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9846 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9847 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9848 else
9849 goto invalid_kind;
9851 c_parser_consume_token (parser);
9852 if (c_parser_next_token_is (parser, CPP_COMMA))
9854 location_t here;
9855 c_parser_consume_token (parser);
9857 here = c_parser_peek_token (parser)->location;
9858 t = c_parser_expr_no_commas (parser, NULL).value;
9859 mark_exp_read (t);
9860 t = c_fully_fold (t, false, NULL);
9862 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9863 error_at (here, "schedule %<runtime%> does not take "
9864 "a %<chunk_size%> parameter");
9865 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9866 error_at (here,
9867 "schedule %<auto%> does not take "
9868 "a %<chunk_size%> parameter");
9869 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9870 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9871 else
9872 c_parser_error (parser, "expected integer expression");
9874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9876 else
9877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9878 "expected %<,%> or %<)%>");
9880 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9881 OMP_CLAUSE_CHAIN (c) = list;
9882 return c;
9884 invalid_kind:
9885 c_parser_error (parser, "invalid schedule kind");
9886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9887 return list;
9890 /* OpenMP 2.5:
9891 shared ( variable-list ) */
9893 static tree
9894 c_parser_omp_clause_shared (c_parser *parser, tree list)
9896 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9899 /* OpenMP 3.0:
9900 untied */
9902 static tree
9903 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9905 tree c;
9907 /* FIXME: Should we allow duplicates? */
9908 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9910 c = build_omp_clause (c_parser_peek_token (parser)->location,
9911 OMP_CLAUSE_UNTIED);
9912 OMP_CLAUSE_CHAIN (c) = list;
9914 return c;
9917 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9918 is a bitmask in MASK. Return the list of clauses found; the result
9919 of clause default goes in *pdefault. */
9921 static tree
9922 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9923 const char *where)
9925 tree clauses = NULL;
9926 bool first = true;
9928 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9930 location_t here;
9931 pragma_omp_clause c_kind;
9932 const char *c_name;
9933 tree prev = clauses;
9935 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9936 c_parser_consume_token (parser);
9938 first = false;
9939 here = c_parser_peek_token (parser)->location;
9940 c_kind = c_parser_omp_clause_name (parser);
9942 switch (c_kind)
9944 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9945 clauses = c_parser_omp_clause_collapse (parser, clauses);
9946 c_name = "collapse";
9947 break;
9948 case PRAGMA_OMP_CLAUSE_COPYIN:
9949 clauses = c_parser_omp_clause_copyin (parser, clauses);
9950 c_name = "copyin";
9951 break;
9952 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9953 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9954 c_name = "copyprivate";
9955 break;
9956 case PRAGMA_OMP_CLAUSE_DEFAULT:
9957 clauses = c_parser_omp_clause_default (parser, clauses);
9958 c_name = "default";
9959 break;
9960 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9961 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9962 c_name = "firstprivate";
9963 break;
9964 case PRAGMA_OMP_CLAUSE_FINAL:
9965 clauses = c_parser_omp_clause_final (parser, clauses);
9966 c_name = "final";
9967 break;
9968 case PRAGMA_OMP_CLAUSE_IF:
9969 clauses = c_parser_omp_clause_if (parser, clauses);
9970 c_name = "if";
9971 break;
9972 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9973 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9974 c_name = "lastprivate";
9975 break;
9976 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9977 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9978 c_name = "mergeable";
9979 break;
9980 case PRAGMA_OMP_CLAUSE_NOWAIT:
9981 clauses = c_parser_omp_clause_nowait (parser, clauses);
9982 c_name = "nowait";
9983 break;
9984 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9985 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9986 c_name = "num_threads";
9987 break;
9988 case PRAGMA_OMP_CLAUSE_ORDERED:
9989 clauses = c_parser_omp_clause_ordered (parser, clauses);
9990 c_name = "ordered";
9991 break;
9992 case PRAGMA_OMP_CLAUSE_PRIVATE:
9993 clauses = c_parser_omp_clause_private (parser, clauses);
9994 c_name = "private";
9995 break;
9996 case PRAGMA_OMP_CLAUSE_REDUCTION:
9997 clauses = c_parser_omp_clause_reduction (parser, clauses);
9998 c_name = "reduction";
9999 break;
10000 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10001 clauses = c_parser_omp_clause_schedule (parser, clauses);
10002 c_name = "schedule";
10003 break;
10004 case PRAGMA_OMP_CLAUSE_SHARED:
10005 clauses = c_parser_omp_clause_shared (parser, clauses);
10006 c_name = "shared";
10007 break;
10008 case PRAGMA_OMP_CLAUSE_UNTIED:
10009 clauses = c_parser_omp_clause_untied (parser, clauses);
10010 c_name = "untied";
10011 break;
10012 default:
10013 c_parser_error (parser, "expected %<#pragma omp%> clause");
10014 goto saw_error;
10017 if (((mask >> c_kind) & 1) == 0 && !parser->error)
10019 /* Remove the invalid clause(s) from the list to avoid
10020 confusing the rest of the compiler. */
10021 clauses = prev;
10022 error_at (here, "%qs is not valid for %qs", c_name, where);
10026 saw_error:
10027 c_parser_skip_to_pragma_eol (parser);
10029 return c_finish_omp_clauses (clauses);
10032 /* OpenMP 2.5:
10033 structured-block:
10034 statement
10036 In practice, we're also interested in adding the statement to an
10037 outer node. So it is convenient if we work around the fact that
10038 c_parser_statement calls add_stmt. */
10040 static tree
10041 c_parser_omp_structured_block (c_parser *parser)
10043 tree stmt = push_stmt_list ();
10044 c_parser_statement (parser);
10045 return pop_stmt_list (stmt);
10048 /* OpenMP 2.5:
10049 # pragma omp atomic new-line
10050 expression-stmt
10052 expression-stmt:
10053 x binop= expr | x++ | ++x | x-- | --x
10054 binop:
10055 +, *, -, /, &, ^, |, <<, >>
10057 where x is an lvalue expression with scalar type.
10059 OpenMP 3.1:
10060 # pragma omp atomic new-line
10061 update-stmt
10063 # pragma omp atomic read new-line
10064 read-stmt
10066 # pragma omp atomic write new-line
10067 write-stmt
10069 # pragma omp atomic update new-line
10070 update-stmt
10072 # pragma omp atomic capture new-line
10073 capture-stmt
10075 # pragma omp atomic capture new-line
10076 capture-block
10078 read-stmt:
10079 v = x
10080 write-stmt:
10081 x = expr
10082 update-stmt:
10083 expression-stmt | x = x binop expr
10084 capture-stmt:
10085 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
10086 capture-block:
10087 { v = x; update-stmt; } | { update-stmt; v = x; }
10089 where x and v are lvalue expressions with scalar type.
10091 LOC is the location of the #pragma token. */
10093 static void
10094 c_parser_omp_atomic (location_t loc, c_parser *parser)
10096 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
10097 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
10098 tree stmt, orig_lhs;
10099 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
10100 struct c_expr rhs_expr;
10101 bool structured_block = false;
10103 if (c_parser_next_token_is (parser, CPP_NAME))
10105 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10107 if (!strcmp (p, "read"))
10108 code = OMP_ATOMIC_READ;
10109 else if (!strcmp (p, "write"))
10110 code = NOP_EXPR;
10111 else if (!strcmp (p, "update"))
10112 code = OMP_ATOMIC;
10113 else if (!strcmp (p, "capture"))
10114 code = OMP_ATOMIC_CAPTURE_NEW;
10115 else
10116 p = NULL;
10117 if (p)
10118 c_parser_consume_token (parser);
10120 c_parser_skip_to_pragma_eol (parser);
10122 switch (code)
10124 case OMP_ATOMIC_READ:
10125 case NOP_EXPR: /* atomic write */
10126 v = c_parser_unary_expression (parser).value;
10127 v = c_fully_fold (v, false, NULL);
10128 if (v == error_mark_node)
10129 goto saw_error;
10130 loc = c_parser_peek_token (parser)->location;
10131 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10132 goto saw_error;
10133 if (code == NOP_EXPR)
10134 lhs = c_parser_expression (parser).value;
10135 else
10136 lhs = c_parser_unary_expression (parser).value;
10137 lhs = c_fully_fold (lhs, false, NULL);
10138 if (lhs == error_mark_node)
10139 goto saw_error;
10140 if (code == NOP_EXPR)
10142 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
10143 opcode. */
10144 code = OMP_ATOMIC;
10145 rhs = lhs;
10146 lhs = v;
10147 v = NULL_TREE;
10149 goto done;
10150 case OMP_ATOMIC_CAPTURE_NEW:
10151 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10153 c_parser_consume_token (parser);
10154 structured_block = true;
10156 else
10158 v = c_parser_unary_expression (parser).value;
10159 v = c_fully_fold (v, false, NULL);
10160 if (v == error_mark_node)
10161 goto saw_error;
10162 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10163 goto saw_error;
10165 break;
10166 default:
10167 break;
10170 /* For structured_block case we don't know yet whether
10171 old or new x should be captured. */
10172 restart:
10173 lhs = c_parser_unary_expression (parser).value;
10174 lhs = c_fully_fold (lhs, false, NULL);
10175 orig_lhs = lhs;
10176 switch (TREE_CODE (lhs))
10178 case ERROR_MARK:
10179 saw_error:
10180 c_parser_skip_to_end_of_block_or_statement (parser);
10181 if (structured_block)
10183 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10184 c_parser_consume_token (parser);
10185 else if (code == OMP_ATOMIC_CAPTURE_NEW)
10187 c_parser_skip_to_end_of_block_or_statement (parser);
10188 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10189 c_parser_consume_token (parser);
10192 return;
10194 case POSTINCREMENT_EXPR:
10195 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10196 code = OMP_ATOMIC_CAPTURE_OLD;
10197 /* FALLTHROUGH */
10198 case PREINCREMENT_EXPR:
10199 lhs = TREE_OPERAND (lhs, 0);
10200 opcode = PLUS_EXPR;
10201 rhs = integer_one_node;
10202 break;
10204 case POSTDECREMENT_EXPR:
10205 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10206 code = OMP_ATOMIC_CAPTURE_OLD;
10207 /* FALLTHROUGH */
10208 case PREDECREMENT_EXPR:
10209 lhs = TREE_OPERAND (lhs, 0);
10210 opcode = MINUS_EXPR;
10211 rhs = integer_one_node;
10212 break;
10214 case COMPOUND_EXPR:
10215 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
10216 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
10217 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
10218 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
10219 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
10220 (TREE_OPERAND (lhs, 1), 0), 0)))
10221 == BOOLEAN_TYPE)
10222 /* Undo effects of boolean_increment for post {in,de}crement. */
10223 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
10224 /* FALLTHRU */
10225 case MODIFY_EXPR:
10226 if (TREE_CODE (lhs) == MODIFY_EXPR
10227 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
10229 /* Undo effects of boolean_increment. */
10230 if (integer_onep (TREE_OPERAND (lhs, 1)))
10232 /* This is pre or post increment. */
10233 rhs = TREE_OPERAND (lhs, 1);
10234 lhs = TREE_OPERAND (lhs, 0);
10235 opcode = NOP_EXPR;
10236 if (code == OMP_ATOMIC_CAPTURE_NEW
10237 && !structured_block
10238 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10239 code = OMP_ATOMIC_CAPTURE_OLD;
10240 break;
10242 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
10243 && TREE_OPERAND (lhs, 0)
10244 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
10246 /* This is pre or post decrement. */
10247 rhs = TREE_OPERAND (lhs, 1);
10248 lhs = TREE_OPERAND (lhs, 0);
10249 opcode = NOP_EXPR;
10250 if (code == OMP_ATOMIC_CAPTURE_NEW
10251 && !structured_block
10252 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10253 code = OMP_ATOMIC_CAPTURE_OLD;
10254 break;
10257 /* FALLTHRU */
10258 default:
10259 switch (c_parser_peek_token (parser)->type)
10261 case CPP_MULT_EQ:
10262 opcode = MULT_EXPR;
10263 break;
10264 case CPP_DIV_EQ:
10265 opcode = TRUNC_DIV_EXPR;
10266 break;
10267 case CPP_PLUS_EQ:
10268 opcode = PLUS_EXPR;
10269 break;
10270 case CPP_MINUS_EQ:
10271 opcode = MINUS_EXPR;
10272 break;
10273 case CPP_LSHIFT_EQ:
10274 opcode = LSHIFT_EXPR;
10275 break;
10276 case CPP_RSHIFT_EQ:
10277 opcode = RSHIFT_EXPR;
10278 break;
10279 case CPP_AND_EQ:
10280 opcode = BIT_AND_EXPR;
10281 break;
10282 case CPP_OR_EQ:
10283 opcode = BIT_IOR_EXPR;
10284 break;
10285 case CPP_XOR_EQ:
10286 opcode = BIT_XOR_EXPR;
10287 break;
10288 case CPP_EQ:
10289 if (structured_block || code == OMP_ATOMIC)
10291 location_t aloc = c_parser_peek_token (parser)->location;
10292 location_t rhs_loc;
10293 enum c_parser_prec oprec = PREC_NONE;
10295 c_parser_consume_token (parser);
10296 rhs1 = c_parser_unary_expression (parser).value;
10297 rhs1 = c_fully_fold (rhs1, false, NULL);
10298 if (rhs1 == error_mark_node)
10299 goto saw_error;
10300 switch (c_parser_peek_token (parser)->type)
10302 case CPP_SEMICOLON:
10303 if (code == OMP_ATOMIC_CAPTURE_NEW)
10305 code = OMP_ATOMIC_CAPTURE_OLD;
10306 v = lhs;
10307 lhs = NULL_TREE;
10308 lhs1 = rhs1;
10309 rhs1 = NULL_TREE;
10310 c_parser_consume_token (parser);
10311 goto restart;
10313 c_parser_error (parser,
10314 "invalid form of %<#pragma omp atomic%>");
10315 goto saw_error;
10316 case CPP_MULT:
10317 opcode = MULT_EXPR;
10318 oprec = PREC_MULT;
10319 break;
10320 case CPP_DIV:
10321 opcode = TRUNC_DIV_EXPR;
10322 oprec = PREC_MULT;
10323 break;
10324 case CPP_PLUS:
10325 opcode = PLUS_EXPR;
10326 oprec = PREC_ADD;
10327 break;
10328 case CPP_MINUS:
10329 opcode = MINUS_EXPR;
10330 oprec = PREC_ADD;
10331 break;
10332 case CPP_LSHIFT:
10333 opcode = LSHIFT_EXPR;
10334 oprec = PREC_SHIFT;
10335 break;
10336 case CPP_RSHIFT:
10337 opcode = RSHIFT_EXPR;
10338 oprec = PREC_SHIFT;
10339 break;
10340 case CPP_AND:
10341 opcode = BIT_AND_EXPR;
10342 oprec = PREC_BITAND;
10343 break;
10344 case CPP_OR:
10345 opcode = BIT_IOR_EXPR;
10346 oprec = PREC_BITOR;
10347 break;
10348 case CPP_XOR:
10349 opcode = BIT_XOR_EXPR;
10350 oprec = PREC_BITXOR;
10351 break;
10352 default:
10353 c_parser_error (parser,
10354 "invalid operator for %<#pragma omp atomic%>");
10355 goto saw_error;
10357 loc = aloc;
10358 c_parser_consume_token (parser);
10359 rhs_loc = c_parser_peek_token (parser)->location;
10360 if (commutative_tree_code (opcode))
10361 oprec = (enum c_parser_prec) (oprec - 1);
10362 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
10363 rhs_expr = default_function_array_read_conversion (rhs_loc,
10364 rhs_expr);
10365 rhs = rhs_expr.value;
10366 rhs = c_fully_fold (rhs, false, NULL);
10367 goto stmt_done;
10369 /* FALLTHROUGH */
10370 default:
10371 c_parser_error (parser,
10372 "invalid operator for %<#pragma omp atomic%>");
10373 goto saw_error;
10376 /* Arrange to pass the location of the assignment operator to
10377 c_finish_omp_atomic. */
10378 loc = c_parser_peek_token (parser)->location;
10379 c_parser_consume_token (parser);
10381 location_t rhs_loc = c_parser_peek_token (parser)->location;
10382 rhs_expr = c_parser_expression (parser);
10383 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
10385 rhs = rhs_expr.value;
10386 rhs = c_fully_fold (rhs, false, NULL);
10387 break;
10389 stmt_done:
10390 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
10392 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
10393 goto saw_error;
10394 v = c_parser_unary_expression (parser).value;
10395 v = c_fully_fold (v, false, NULL);
10396 if (v == error_mark_node)
10397 goto saw_error;
10398 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10399 goto saw_error;
10400 lhs1 = c_parser_unary_expression (parser).value;
10401 lhs1 = c_fully_fold (lhs1, false, NULL);
10402 if (lhs1 == error_mark_node)
10403 goto saw_error;
10405 if (structured_block)
10407 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10408 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
10410 done:
10411 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
10412 if (stmt != error_mark_node)
10413 add_stmt (stmt);
10415 if (!structured_block)
10416 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10420 /* OpenMP 2.5:
10421 # pragma omp barrier new-line
10424 static void
10425 c_parser_omp_barrier (c_parser *parser)
10427 location_t loc = c_parser_peek_token (parser)->location;
10428 c_parser_consume_pragma (parser);
10429 c_parser_skip_to_pragma_eol (parser);
10431 c_finish_omp_barrier (loc);
10434 /* OpenMP 2.5:
10435 # pragma omp critical [(name)] new-line
10436 structured-block
10438 LOC is the location of the #pragma itself. */
10440 static tree
10441 c_parser_omp_critical (location_t loc, c_parser *parser)
10443 tree stmt, name = NULL;
10445 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10447 c_parser_consume_token (parser);
10448 if (c_parser_next_token_is (parser, CPP_NAME))
10450 name = c_parser_peek_token (parser)->value;
10451 c_parser_consume_token (parser);
10452 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10454 else
10455 c_parser_error (parser, "expected identifier");
10457 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10458 c_parser_error (parser, "expected %<(%> or end of line");
10459 c_parser_skip_to_pragma_eol (parser);
10461 stmt = c_parser_omp_structured_block (parser);
10462 return c_finish_omp_critical (loc, stmt, name);
10465 /* OpenMP 2.5:
10466 # pragma omp flush flush-vars[opt] new-line
10468 flush-vars:
10469 ( variable-list ) */
10471 static void
10472 c_parser_omp_flush (c_parser *parser)
10474 location_t loc = c_parser_peek_token (parser)->location;
10475 c_parser_consume_pragma (parser);
10476 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10477 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10478 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10479 c_parser_error (parser, "expected %<(%> or end of line");
10480 c_parser_skip_to_pragma_eol (parser);
10482 c_finish_omp_flush (loc);
10485 /* Parse the restricted form of the for statement allowed by OpenMP.
10486 The real trick here is to determine the loop control variable early
10487 so that we can push a new decl if necessary to make it private.
10488 LOC is the location of the OMP in "#pragma omp". */
10490 static tree
10491 c_parser_omp_for_loop (location_t loc,
10492 c_parser *parser, tree clauses, tree *par_clauses)
10494 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
10495 tree declv, condv, incrv, initv, ret = NULL;
10496 bool fail = false, open_brace_parsed = false;
10497 int i, collapse = 1, nbraces = 0;
10498 location_t for_loc;
10499 vec<tree, va_gc> *for_block = make_tree_vector ();
10501 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
10502 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
10503 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
10505 gcc_assert (collapse >= 1);
10507 declv = make_tree_vec (collapse);
10508 initv = make_tree_vec (collapse);
10509 condv = make_tree_vec (collapse);
10510 incrv = make_tree_vec (collapse);
10512 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
10514 c_parser_error (parser, "for statement expected");
10515 return NULL;
10517 for_loc = c_parser_peek_token (parser)->location;
10518 c_parser_consume_token (parser);
10520 for (i = 0; i < collapse; i++)
10522 int bracecount = 0;
10524 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10525 goto pop_scopes;
10527 /* Parse the initialization declaration or expression. */
10528 if (c_parser_next_tokens_start_declaration (parser))
10530 if (i > 0)
10531 vec_safe_push (for_block, c_begin_compound_stmt (true));
10532 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
10533 decl = check_for_loop_decls (for_loc, flag_isoc99);
10534 if (decl == NULL)
10535 goto error_init;
10536 if (DECL_INITIAL (decl) == error_mark_node)
10537 decl = error_mark_node;
10538 init = decl;
10540 else if (c_parser_next_token_is (parser, CPP_NAME)
10541 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
10543 struct c_expr decl_exp;
10544 struct c_expr init_exp;
10545 location_t init_loc;
10547 decl_exp = c_parser_postfix_expression (parser);
10548 decl = decl_exp.value;
10550 c_parser_require (parser, CPP_EQ, "expected %<=%>");
10552 init_loc = c_parser_peek_token (parser)->location;
10553 init_exp = c_parser_expr_no_commas (parser, NULL);
10554 init_exp = default_function_array_read_conversion (init_loc,
10555 init_exp);
10556 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
10557 NOP_EXPR, init_loc, init_exp.value,
10558 init_exp.original_type);
10559 init = c_process_expr_stmt (init_loc, init);
10561 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10563 else
10565 error_init:
10566 c_parser_error (parser,
10567 "expected iteration declaration or initialization");
10568 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10569 "expected %<)%>");
10570 fail = true;
10571 goto parse_next;
10574 /* Parse the loop condition. */
10575 cond = NULL_TREE;
10576 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
10578 location_t cond_loc = c_parser_peek_token (parser)->location;
10579 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
10580 PREC_NONE);
10582 cond = cond_expr.value;
10583 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
10584 cond = c_fully_fold (cond, false, NULL);
10585 switch (cond_expr.original_code)
10587 case GT_EXPR:
10588 case GE_EXPR:
10589 case LT_EXPR:
10590 case LE_EXPR:
10591 break;
10592 default:
10593 /* Can't be cond = error_mark_node, because we want to preserve
10594 the location until c_finish_omp_for. */
10595 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
10596 break;
10598 protected_set_expr_location (cond, cond_loc);
10600 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10602 /* Parse the increment expression. */
10603 incr = NULL_TREE;
10604 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
10606 location_t incr_loc = c_parser_peek_token (parser)->location;
10608 incr = c_process_expr_stmt (incr_loc,
10609 c_parser_expression (parser).value);
10611 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10613 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
10614 fail = true;
10615 else
10617 TREE_VEC_ELT (declv, i) = decl;
10618 TREE_VEC_ELT (initv, i) = init;
10619 TREE_VEC_ELT (condv, i) = cond;
10620 TREE_VEC_ELT (incrv, i) = incr;
10623 parse_next:
10624 if (i == collapse - 1)
10625 break;
10627 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10628 in between the collapsed for loops to be still considered perfectly
10629 nested. Hopefully the final version clarifies this.
10630 For now handle (multiple) {'s and empty statements. */
10633 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10635 c_parser_consume_token (parser);
10636 break;
10638 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10640 c_parser_consume_token (parser);
10641 bracecount++;
10643 else if (bracecount
10644 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10645 c_parser_consume_token (parser);
10646 else
10648 c_parser_error (parser, "not enough perfectly nested loops");
10649 if (bracecount)
10651 open_brace_parsed = true;
10652 bracecount--;
10654 fail = true;
10655 collapse = 0;
10656 break;
10659 while (1);
10661 nbraces += bracecount;
10664 save_break = c_break_label;
10665 c_break_label = size_one_node;
10666 save_cont = c_cont_label;
10667 c_cont_label = NULL_TREE;
10668 body = push_stmt_list ();
10670 if (open_brace_parsed)
10672 location_t here = c_parser_peek_token (parser)->location;
10673 stmt = c_begin_compound_stmt (true);
10674 c_parser_compound_statement_nostart (parser);
10675 add_stmt (c_end_compound_stmt (here, stmt, true));
10677 else
10678 add_stmt (c_parser_c99_block_statement (parser));
10679 if (c_cont_label)
10681 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10682 SET_EXPR_LOCATION (t, loc);
10683 add_stmt (t);
10686 body = pop_stmt_list (body);
10687 c_break_label = save_break;
10688 c_cont_label = save_cont;
10690 while (nbraces)
10692 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10694 c_parser_consume_token (parser);
10695 nbraces--;
10697 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10698 c_parser_consume_token (parser);
10699 else
10701 c_parser_error (parser, "collapsed loops not perfectly nested");
10702 while (nbraces)
10704 location_t here = c_parser_peek_token (parser)->location;
10705 stmt = c_begin_compound_stmt (true);
10706 add_stmt (body);
10707 c_parser_compound_statement_nostart (parser);
10708 body = c_end_compound_stmt (here, stmt, true);
10709 nbraces--;
10711 goto pop_scopes;
10715 /* Only bother calling c_finish_omp_for if we haven't already generated
10716 an error from the initialization parsing. */
10717 if (!fail)
10719 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10720 if (stmt)
10722 if (par_clauses != NULL)
10724 tree *c;
10725 for (c = par_clauses; *c ; )
10726 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10727 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10728 c = &OMP_CLAUSE_CHAIN (*c);
10729 else
10731 for (i = 0; i < collapse; i++)
10732 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10733 break;
10734 if (i == collapse)
10735 c = &OMP_CLAUSE_CHAIN (*c);
10736 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10738 error_at (loc,
10739 "iteration variable %qD should not be firstprivate",
10740 OMP_CLAUSE_DECL (*c));
10741 *c = OMP_CLAUSE_CHAIN (*c);
10743 else
10745 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10746 change it to shared (decl) in
10747 OMP_PARALLEL_CLAUSES. */
10748 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10749 OMP_CLAUSE_LASTPRIVATE);
10750 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10751 OMP_CLAUSE_CHAIN (l) = clauses;
10752 clauses = l;
10753 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10757 OMP_FOR_CLAUSES (stmt) = clauses;
10759 ret = stmt;
10761 pop_scopes:
10762 while (!for_block->is_empty ())
10764 /* FIXME diagnostics: LOC below should be the actual location of
10765 this particular for block. We need to build a list of
10766 locations to go along with FOR_BLOCK. */
10767 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
10768 add_stmt (stmt);
10770 release_tree_vector (for_block);
10771 return ret;
10774 /* OpenMP 2.5:
10775 #pragma omp for for-clause[optseq] new-line
10776 for-loop
10778 LOC is the location of the #pragma token.
10781 #define OMP_FOR_CLAUSE_MASK \
10782 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10783 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10784 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10785 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10786 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10787 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10788 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10789 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10791 static tree
10792 c_parser_omp_for (location_t loc, c_parser *parser)
10794 tree block, clauses, ret;
10796 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10797 "#pragma omp for");
10799 block = c_begin_compound_stmt (true);
10800 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10801 block = c_end_compound_stmt (loc, block, true);
10802 add_stmt (block);
10804 return ret;
10807 /* OpenMP 2.5:
10808 # pragma omp master new-line
10809 structured-block
10811 LOC is the location of the #pragma token.
10814 static tree
10815 c_parser_omp_master (location_t loc, c_parser *parser)
10817 c_parser_skip_to_pragma_eol (parser);
10818 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10821 /* OpenMP 2.5:
10822 # pragma omp ordered new-line
10823 structured-block
10825 LOC is the location of the #pragma itself.
10828 static tree
10829 c_parser_omp_ordered (location_t loc, c_parser *parser)
10831 c_parser_skip_to_pragma_eol (parser);
10832 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10835 /* OpenMP 2.5:
10837 section-scope:
10838 { section-sequence }
10840 section-sequence:
10841 section-directive[opt] structured-block
10842 section-sequence section-directive structured-block
10844 SECTIONS_LOC is the location of the #pragma omp sections. */
10846 static tree
10847 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10849 tree stmt, substmt;
10850 bool error_suppress = false;
10851 location_t loc;
10853 loc = c_parser_peek_token (parser)->location;
10854 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10856 /* Avoid skipping until the end of the block. */
10857 parser->error = false;
10858 return NULL_TREE;
10861 stmt = push_stmt_list ();
10863 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10865 substmt = push_stmt_list ();
10867 while (1)
10869 c_parser_statement (parser);
10871 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10872 break;
10873 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10874 break;
10875 if (c_parser_next_token_is (parser, CPP_EOF))
10876 break;
10879 substmt = pop_stmt_list (substmt);
10880 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10881 SET_EXPR_LOCATION (substmt, loc);
10882 add_stmt (substmt);
10885 while (1)
10887 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10888 break;
10889 if (c_parser_next_token_is (parser, CPP_EOF))
10890 break;
10892 loc = c_parser_peek_token (parser)->location;
10893 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10895 c_parser_consume_pragma (parser);
10896 c_parser_skip_to_pragma_eol (parser);
10897 error_suppress = false;
10899 else if (!error_suppress)
10901 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10902 error_suppress = true;
10905 substmt = c_parser_omp_structured_block (parser);
10906 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10907 SET_EXPR_LOCATION (substmt, loc);
10908 add_stmt (substmt);
10910 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10911 "expected %<#pragma omp section%> or %<}%>");
10913 substmt = pop_stmt_list (stmt);
10915 stmt = make_node (OMP_SECTIONS);
10916 SET_EXPR_LOCATION (stmt, sections_loc);
10917 TREE_TYPE (stmt) = void_type_node;
10918 OMP_SECTIONS_BODY (stmt) = substmt;
10920 return add_stmt (stmt);
10923 /* OpenMP 2.5:
10924 # pragma omp sections sections-clause[optseq] newline
10925 sections-scope
10927 LOC is the location of the #pragma token.
10930 #define OMP_SECTIONS_CLAUSE_MASK \
10931 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10932 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10933 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10934 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10935 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10937 static tree
10938 c_parser_omp_sections (location_t loc, c_parser *parser)
10940 tree block, clauses, ret;
10942 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10943 "#pragma omp sections");
10945 block = c_begin_compound_stmt (true);
10946 ret = c_parser_omp_sections_scope (loc, parser);
10947 if (ret)
10948 OMP_SECTIONS_CLAUSES (ret) = clauses;
10949 block = c_end_compound_stmt (loc, block, true);
10950 add_stmt (block);
10952 return ret;
10955 /* OpenMP 2.5:
10956 # pragma parallel parallel-clause new-line
10957 # pragma parallel for parallel-for-clause new-line
10958 # pragma parallel sections parallel-sections-clause new-line
10960 LOC is the location of the #pragma token.
10963 #define OMP_PARALLEL_CLAUSE_MASK \
10964 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10965 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10966 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10967 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10968 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10969 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10970 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10971 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10973 static tree
10974 c_parser_omp_parallel (location_t loc, c_parser *parser)
10976 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10977 const char *p_name = "#pragma omp parallel";
10978 tree stmt, clauses, par_clause, ws_clause, block;
10979 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10981 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10983 c_parser_consume_token (parser);
10984 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10985 p_name = "#pragma omp parallel for";
10986 mask |= OMP_FOR_CLAUSE_MASK;
10987 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10989 else if (c_parser_next_token_is (parser, CPP_NAME))
10991 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10992 if (strcmp (p, "sections") == 0)
10994 c_parser_consume_token (parser);
10995 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10996 p_name = "#pragma omp parallel sections";
10997 mask |= OMP_SECTIONS_CLAUSE_MASK;
10998 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
11002 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
11004 switch (p_kind)
11006 case PRAGMA_OMP_PARALLEL:
11007 block = c_begin_omp_parallel ();
11008 c_parser_statement (parser);
11009 stmt = c_finish_omp_parallel (loc, clauses, block);
11010 break;
11012 case PRAGMA_OMP_PARALLEL_FOR:
11013 block = c_begin_omp_parallel ();
11014 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
11015 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
11016 stmt = c_finish_omp_parallel (loc, par_clause, block);
11017 OMP_PARALLEL_COMBINED (stmt) = 1;
11018 break;
11020 case PRAGMA_OMP_PARALLEL_SECTIONS:
11021 block = c_begin_omp_parallel ();
11022 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
11023 stmt = c_parser_omp_sections_scope (loc, parser);
11024 if (stmt)
11025 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
11026 stmt = c_finish_omp_parallel (loc, par_clause, block);
11027 OMP_PARALLEL_COMBINED (stmt) = 1;
11028 break;
11030 default:
11031 gcc_unreachable ();
11034 return stmt;
11037 /* OpenMP 2.5:
11038 # pragma omp single single-clause[optseq] new-line
11039 structured-block
11041 LOC is the location of the #pragma.
11044 #define OMP_SINGLE_CLAUSE_MASK \
11045 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
11046 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11047 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
11048 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
11050 static tree
11051 c_parser_omp_single (location_t loc, c_parser *parser)
11053 tree stmt = make_node (OMP_SINGLE);
11054 SET_EXPR_LOCATION (stmt, loc);
11055 TREE_TYPE (stmt) = void_type_node;
11057 OMP_SINGLE_CLAUSES (stmt)
11058 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
11059 "#pragma omp single");
11060 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
11062 return add_stmt (stmt);
11065 /* OpenMP 3.0:
11066 # pragma omp task task-clause[optseq] new-line
11068 LOC is the location of the #pragma.
11071 #define OMP_TASK_CLAUSE_MASK \
11072 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
11073 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
11074 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
11075 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
11076 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11077 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
11078 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
11079 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
11081 static tree
11082 c_parser_omp_task (location_t loc, c_parser *parser)
11084 tree clauses, block;
11086 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
11087 "#pragma omp task");
11089 block = c_begin_omp_task ();
11090 c_parser_statement (parser);
11091 return c_finish_omp_task (loc, clauses, block);
11094 /* OpenMP 3.0:
11095 # pragma omp taskwait new-line
11098 static void
11099 c_parser_omp_taskwait (c_parser *parser)
11101 location_t loc = c_parser_peek_token (parser)->location;
11102 c_parser_consume_pragma (parser);
11103 c_parser_skip_to_pragma_eol (parser);
11105 c_finish_omp_taskwait (loc);
11108 /* OpenMP 3.1:
11109 # pragma omp taskyield new-line
11112 static void
11113 c_parser_omp_taskyield (c_parser *parser)
11115 location_t loc = c_parser_peek_token (parser)->location;
11116 c_parser_consume_pragma (parser);
11117 c_parser_skip_to_pragma_eol (parser);
11119 c_finish_omp_taskyield (loc);
11122 /* Main entry point to parsing most OpenMP pragmas. */
11124 static void
11125 c_parser_omp_construct (c_parser *parser)
11127 enum pragma_kind p_kind;
11128 location_t loc;
11129 tree stmt;
11131 loc = c_parser_peek_token (parser)->location;
11132 p_kind = c_parser_peek_token (parser)->pragma_kind;
11133 c_parser_consume_pragma (parser);
11135 switch (p_kind)
11137 case PRAGMA_OMP_ATOMIC:
11138 c_parser_omp_atomic (loc, parser);
11139 return;
11140 case PRAGMA_OMP_CRITICAL:
11141 stmt = c_parser_omp_critical (loc, parser);
11142 break;
11143 case PRAGMA_OMP_FOR:
11144 stmt = c_parser_omp_for (loc, parser);
11145 break;
11146 case PRAGMA_OMP_MASTER:
11147 stmt = c_parser_omp_master (loc, parser);
11148 break;
11149 case PRAGMA_OMP_ORDERED:
11150 stmt = c_parser_omp_ordered (loc, parser);
11151 break;
11152 case PRAGMA_OMP_PARALLEL:
11153 stmt = c_parser_omp_parallel (loc, parser);
11154 break;
11155 case PRAGMA_OMP_SECTIONS:
11156 stmt = c_parser_omp_sections (loc, parser);
11157 break;
11158 case PRAGMA_OMP_SINGLE:
11159 stmt = c_parser_omp_single (loc, parser);
11160 break;
11161 case PRAGMA_OMP_TASK:
11162 stmt = c_parser_omp_task (loc, parser);
11163 break;
11164 default:
11165 gcc_unreachable ();
11168 if (stmt)
11169 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
11173 /* OpenMP 2.5:
11174 # pragma omp threadprivate (variable-list) */
11176 static void
11177 c_parser_omp_threadprivate (c_parser *parser)
11179 tree vars, t;
11180 location_t loc;
11182 c_parser_consume_pragma (parser);
11183 loc = c_parser_peek_token (parser)->location;
11184 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11186 /* Mark every variable in VARS to be assigned thread local storage. */
11187 for (t = vars; t; t = TREE_CHAIN (t))
11189 tree v = TREE_PURPOSE (t);
11191 /* FIXME diagnostics: Ideally we should keep individual
11192 locations for all the variables in the var list to make the
11193 following errors more precise. Perhaps
11194 c_parser_omp_var_list_parens() should construct a list of
11195 locations to go along with the var list. */
11197 /* If V had already been marked threadprivate, it doesn't matter
11198 whether it had been used prior to this point. */
11199 if (TREE_CODE (v) != VAR_DECL)
11200 error_at (loc, "%qD is not a variable", v);
11201 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
11202 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
11203 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
11204 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
11205 else if (TREE_TYPE (v) == error_mark_node)
11207 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
11208 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
11209 else
11211 if (! DECL_THREAD_LOCAL_P (v))
11213 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
11214 /* If rtl has been already set for this var, call
11215 make_decl_rtl once again, so that encode_section_info
11216 has a chance to look at the new decl flags. */
11217 if (DECL_RTL_SET_P (v))
11218 make_decl_rtl (v);
11220 C_DECL_THREADPRIVATE_P (v) = 1;
11224 c_parser_skip_to_pragma_eol (parser);
11227 /* Parse a transaction attribute (GCC Extension).
11229 transaction-attribute:
11230 attributes
11231 [ [ any-word ] ]
11233 The transactional memory language description is written for C++,
11234 and uses the C++0x attribute syntax. For compatibility, allow the
11235 bracket style for transactions in C as well. */
11237 static tree
11238 c_parser_transaction_attributes (c_parser *parser)
11240 tree attr_name, attr = NULL;
11242 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11243 return c_parser_attributes (parser);
11245 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11246 return NULL_TREE;
11247 c_parser_consume_token (parser);
11248 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
11249 goto error1;
11251 attr_name = c_parser_attribute_any_word (parser);
11252 if (attr_name)
11254 c_parser_consume_token (parser);
11255 attr = build_tree_list (attr_name, NULL_TREE);
11257 else
11258 c_parser_error (parser, "expected identifier");
11260 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11261 error1:
11262 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11263 return attr;
11266 /* Parse a __transaction_atomic or __transaction_relaxed statement
11267 (GCC Extension).
11269 transaction-statement:
11270 __transaction_atomic transaction-attribute[opt] compound-statement
11271 __transaction_relaxed compound-statement
11273 Note that the only valid attribute is: "outer".
11276 static tree
11277 c_parser_transaction (c_parser *parser, enum rid keyword)
11279 unsigned int old_in = parser->in_transaction;
11280 unsigned int this_in = 1, new_in;
11281 location_t loc = c_parser_peek_token (parser)->location;
11282 tree stmt, attrs;
11284 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11285 || keyword == RID_TRANSACTION_RELAXED)
11286 && c_parser_next_token_is_keyword (parser, keyword));
11287 c_parser_consume_token (parser);
11289 if (keyword == RID_TRANSACTION_RELAXED)
11290 this_in |= TM_STMT_ATTR_RELAXED;
11291 else
11293 attrs = c_parser_transaction_attributes (parser);
11294 if (attrs)
11295 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
11298 /* Keep track if we're in the lexical scope of an outer transaction. */
11299 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
11301 parser->in_transaction = new_in;
11302 stmt = c_parser_compound_statement (parser);
11303 parser->in_transaction = old_in;
11305 if (flag_tm)
11306 stmt = c_finish_transaction (loc, stmt, this_in);
11307 else
11308 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11309 "%<__transaction_atomic%> without transactional memory support enabled"
11310 : "%<__transaction_relaxed %> "
11311 "without transactional memory support enabled"));
11313 return stmt;
11316 /* Parse a __transaction_atomic or __transaction_relaxed expression
11317 (GCC Extension).
11319 transaction-expression:
11320 __transaction_atomic ( expression )
11321 __transaction_relaxed ( expression )
11324 static struct c_expr
11325 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
11327 struct c_expr ret;
11328 unsigned int old_in = parser->in_transaction;
11329 unsigned int this_in = 1;
11330 location_t loc = c_parser_peek_token (parser)->location;
11331 tree attrs;
11333 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11334 || keyword == RID_TRANSACTION_RELAXED)
11335 && c_parser_next_token_is_keyword (parser, keyword));
11336 c_parser_consume_token (parser);
11338 if (keyword == RID_TRANSACTION_RELAXED)
11339 this_in |= TM_STMT_ATTR_RELAXED;
11340 else
11342 attrs = c_parser_transaction_attributes (parser);
11343 if (attrs)
11344 this_in |= parse_tm_stmt_attr (attrs, 0);
11347 parser->in_transaction = this_in;
11348 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11350 tree expr = c_parser_expression (parser).value;
11351 ret.original_type = TREE_TYPE (expr);
11352 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
11353 if (this_in & TM_STMT_ATTR_RELAXED)
11354 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
11355 SET_EXPR_LOCATION (ret.value, loc);
11356 ret.original_code = TRANSACTION_EXPR;
11357 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11359 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
11360 goto error;
11363 else
11365 error:
11366 ret.value = error_mark_node;
11367 ret.original_code = ERROR_MARK;
11368 ret.original_type = NULL;
11370 parser->in_transaction = old_in;
11372 if (!flag_tm)
11373 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11374 "%<__transaction_atomic%> without transactional memory support enabled"
11375 : "%<__transaction_relaxed %> "
11376 "without transactional memory support enabled"));
11378 return ret;
11381 /* Parse a __transaction_cancel statement (GCC Extension).
11383 transaction-cancel-statement:
11384 __transaction_cancel transaction-attribute[opt] ;
11386 Note that the only valid attribute is "outer".
11389 static tree
11390 c_parser_transaction_cancel(c_parser *parser)
11392 location_t loc = c_parser_peek_token (parser)->location;
11393 tree attrs;
11394 bool is_outer = false;
11396 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
11397 c_parser_consume_token (parser);
11399 attrs = c_parser_transaction_attributes (parser);
11400 if (attrs)
11401 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
11403 if (!flag_tm)
11405 error_at (loc, "%<__transaction_cancel%> without "
11406 "transactional memory support enabled");
11407 goto ret_error;
11409 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
11411 error_at (loc, "%<__transaction_cancel%> within a "
11412 "%<__transaction_relaxed%>");
11413 goto ret_error;
11415 else if (is_outer)
11417 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
11418 && !is_tm_may_cancel_outer (current_function_decl))
11420 error_at (loc, "outer %<__transaction_cancel%> not "
11421 "within outer %<__transaction_atomic%>");
11422 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
11423 goto ret_error;
11426 else if (parser->in_transaction == 0)
11428 error_at (loc, "%<__transaction_cancel%> not within "
11429 "%<__transaction_atomic%>");
11430 goto ret_error;
11433 return add_stmt (build_tm_abort_call (loc, is_outer));
11435 ret_error:
11436 return build1 (NOP_EXPR, void_type_node, error_mark_node);
11439 /* Parse a single source file. */
11441 void
11442 c_parse_file (void)
11444 /* Use local storage to begin. If the first token is a pragma, parse it.
11445 If it is #pragma GCC pch_preprocess, then this will load a PCH file
11446 which will cause garbage collection. */
11447 c_parser tparser;
11449 memset (&tparser, 0, sizeof tparser);
11450 the_parser = &tparser;
11452 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
11453 c_parser_pragma_pch_preprocess (&tparser);
11455 the_parser = ggc_alloc_c_parser ();
11456 *the_parser = tparser;
11458 /* Initialize EH, if we've been told to do so. */
11459 if (flag_exceptions)
11460 using_eh_for_cleanups ();
11462 c_parser_translation_unit (the_parser);
11463 the_parser = NULL;
11466 /* This function parses Cilk Plus array notation. The starting index is
11467 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
11468 return value of this function is a tree_node called VALUE_TREE of type
11469 ARRAY_NOTATION_REF. */
11471 static tree
11472 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
11473 tree array_value)
11475 c_token *token = NULL;
11476 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
11477 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
11478 tree array_type_domain = NULL_TREE;
11480 if (array_value == error_mark_node)
11482 /* No need to continue. If either of these 2 were true, then an error
11483 must be emitted already. Thus, no need to emit them twice. */
11484 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11485 return error_mark_node;
11488 array_type = TREE_TYPE (array_value);
11489 gcc_assert (array_type);
11490 type = TREE_TYPE (array_type);
11491 token = c_parser_peek_token (parser);
11493 if (token->type == CPP_EOF)
11495 c_parser_error (parser, "expected %<:%> or numeral");
11496 return value_tree;
11498 else if (token->type == CPP_COLON)
11500 if (!initial_index)
11502 /* If we are here, then we have a case like this A[:]. */
11503 c_parser_consume_token (parser);
11504 if (TREE_CODE (array_type) == POINTER_TYPE)
11506 error_at (loc, "start-index and length fields necessary for "
11507 "using array notations in pointers");
11508 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11509 return error_mark_node;
11511 if (TREE_CODE (array_type) == FUNCTION_TYPE)
11513 error_at (loc, "array notations cannot be used with function "
11514 "type");
11515 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11516 return error_mark_node;
11518 array_type_domain = TYPE_DOMAIN (array_type);
11520 if (!array_type_domain)
11522 error_at (loc, "start-index and length fields necessary for "
11523 "using array notations in dimensionless arrays");
11524 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11525 return error_mark_node;
11528 start_index = TYPE_MINVAL (array_type_domain);
11529 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
11530 start_index);
11531 if (!TYPE_MAXVAL (array_type_domain)
11532 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
11534 error_at (loc, "start-index and length fields necessary for "
11535 "using array notations in variable-length arrays");
11536 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11537 return error_mark_node;
11539 end_index = TYPE_MAXVAL (array_type_domain);
11540 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
11541 end_index, integer_one_node);
11542 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
11543 stride = build_int_cst (integer_type_node, 1);
11544 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
11546 else if (initial_index != error_mark_node)
11548 /* If we are here, then there should be 2 possibilities:
11549 1. Array [EXPR : EXPR]
11550 2. Array [EXPR : EXPR : EXPR]
11552 start_index = initial_index;
11554 if (TREE_CODE (array_type) == FUNCTION_TYPE)
11556 error_at (loc, "array notations cannot be used with function "
11557 "type");
11558 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
11559 return error_mark_node;
11561 c_parser_consume_token (parser); /* consume the ':' */
11562 end_index = c_parser_expression (parser).value;
11563 if (!end_index || end_index == error_mark_node)
11565 c_parser_skip_to_end_of_block_or_statement (parser);
11566 return error_mark_node;
11568 if (c_parser_peek_token (parser)->type == CPP_COLON)
11570 c_parser_consume_token (parser);
11571 stride = c_parser_expression (parser).value;
11572 if (!stride || stride == error_mark_node)
11574 c_parser_skip_to_end_of_block_or_statement (parser);
11575 return error_mark_node;
11579 else
11580 c_parser_error (parser, "expected array notation expression");
11582 else
11583 c_parser_error (parser, "expected array notation expression");
11585 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11587 value_tree = build_array_notation_ref (loc, array_value, start_index,
11588 end_index, stride, type);
11589 if (value_tree != error_mark_node)
11590 SET_EXPR_LOCATION (value_tree, loc);
11591 return value_tree;
11594 #include "gt-c-c-parser.h"