Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / c / c-parser.c
blobec1ef2c703981d6305c303ab4f72d8086b66aef1
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 /* These UPC parser functions are only ever called when
1241 compiling UPC. */
1242 static void c_parser_upc_forall_statement (c_parser *);
1243 static void c_parser_upc_sync_statement (c_parser *, int);
1244 static void c_parser_upc_shared_qual (source_location,
1245 c_parser *,
1246 struct c_declspecs *);
1248 /* Parse a translation unit (C90 6.7, C99 6.9).
1250 translation-unit:
1251 external-declarations
1253 external-declarations:
1254 external-declaration
1255 external-declarations external-declaration
1257 GNU extensions:
1259 translation-unit:
1260 empty
1263 static void
1264 c_parser_translation_unit (c_parser *parser)
1266 if (c_parser_next_token_is (parser, CPP_EOF))
1268 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1269 "ISO C forbids an empty translation unit");
1271 else
1273 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1274 mark_valid_location_for_stdc_pragma (false);
1277 ggc_collect ();
1278 c_parser_external_declaration (parser);
1279 obstack_free (&parser_obstack, obstack_position);
1281 while (c_parser_next_token_is_not (parser, CPP_EOF));
1285 /* Parse an external declaration (C90 6.7, C99 6.9).
1287 external-declaration:
1288 function-definition
1289 declaration
1291 GNU extensions:
1293 external-declaration:
1294 asm-definition
1296 __extension__ external-declaration
1298 Objective-C:
1300 external-declaration:
1301 objc-class-definition
1302 objc-class-declaration
1303 objc-alias-declaration
1304 objc-protocol-definition
1305 objc-method-definition
1306 @end
1309 static void
1310 c_parser_external_declaration (c_parser *parser)
1312 int ext;
1313 switch (c_parser_peek_token (parser)->type)
1315 case CPP_KEYWORD:
1316 switch (c_parser_peek_token (parser)->keyword)
1318 case RID_EXTENSION:
1319 ext = disable_extension_diagnostics ();
1320 c_parser_consume_token (parser);
1321 c_parser_external_declaration (parser);
1322 restore_extension_diagnostics (ext);
1323 break;
1324 case RID_ASM:
1325 c_parser_asm_definition (parser);
1326 break;
1327 case RID_AT_INTERFACE:
1328 case RID_AT_IMPLEMENTATION:
1329 gcc_assert (c_dialect_objc ());
1330 c_parser_objc_class_definition (parser, NULL_TREE);
1331 break;
1332 case RID_AT_CLASS:
1333 gcc_assert (c_dialect_objc ());
1334 c_parser_objc_class_declaration (parser);
1335 break;
1336 case RID_AT_ALIAS:
1337 gcc_assert (c_dialect_objc ());
1338 c_parser_objc_alias_declaration (parser);
1339 break;
1340 case RID_AT_PROTOCOL:
1341 gcc_assert (c_dialect_objc ());
1342 c_parser_objc_protocol_definition (parser, NULL_TREE);
1343 break;
1344 case RID_AT_PROPERTY:
1345 gcc_assert (c_dialect_objc ());
1346 c_parser_objc_at_property_declaration (parser);
1347 break;
1348 case RID_AT_SYNTHESIZE:
1349 gcc_assert (c_dialect_objc ());
1350 c_parser_objc_at_synthesize_declaration (parser);
1351 break;
1352 case RID_AT_DYNAMIC:
1353 gcc_assert (c_dialect_objc ());
1354 c_parser_objc_at_dynamic_declaration (parser);
1355 break;
1356 case RID_AT_END:
1357 gcc_assert (c_dialect_objc ());
1358 c_parser_consume_token (parser);
1359 objc_finish_implementation ();
1360 break;
1361 default:
1362 goto decl_or_fndef;
1364 break;
1365 case CPP_SEMICOLON:
1366 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1367 "ISO C does not allow extra %<;%> outside of a function");
1368 c_parser_consume_token (parser);
1369 break;
1370 case CPP_PRAGMA:
1371 mark_valid_location_for_stdc_pragma (true);
1372 c_parser_pragma (parser, pragma_external);
1373 mark_valid_location_for_stdc_pragma (false);
1374 break;
1375 case CPP_PLUS:
1376 case CPP_MINUS:
1377 if (c_dialect_objc ())
1379 c_parser_objc_method_definition (parser);
1380 break;
1382 /* Else fall through, and yield a syntax error trying to parse
1383 as a declaration or function definition. */
1384 default:
1385 decl_or_fndef:
1386 /* A declaration or a function definition (or, in Objective-C,
1387 an @interface or @protocol with prefix attributes). We can
1388 only tell which after parsing the declaration specifiers, if
1389 any, and the first declarator. */
1390 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1391 break;
1395 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1396 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1397 accepted; otherwise (old-style parameter declarations) only other
1398 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1399 assertion is accepted; otherwise (old-style parameter declarations)
1400 it is not. If NESTED is true, we are inside a function or parsing
1401 old-style parameter declarations; any functions encountered are
1402 nested functions and declaration specifiers are required; otherwise
1403 we are at top level and functions are normal functions and
1404 declaration specifiers may be optional. If EMPTY_OK is true, empty
1405 declarations are OK (subject to all other constraints); otherwise
1406 (old-style parameter declarations) they are diagnosed. If
1407 START_ATTR_OK is true, the declaration specifiers may start with
1408 attributes; otherwise they may not.
1409 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1410 declaration when parsing an Objective-C foreach statement.
1412 declaration:
1413 declaration-specifiers init-declarator-list[opt] ;
1414 static_assert-declaration
1416 function-definition:
1417 declaration-specifiers[opt] declarator declaration-list[opt]
1418 compound-statement
1420 declaration-list:
1421 declaration
1422 declaration-list declaration
1424 init-declarator-list:
1425 init-declarator
1426 init-declarator-list , init-declarator
1428 init-declarator:
1429 declarator simple-asm-expr[opt] attributes[opt]
1430 declarator simple-asm-expr[opt] attributes[opt] = initializer
1432 GNU extensions:
1434 nested-function-definition:
1435 declaration-specifiers declarator declaration-list[opt]
1436 compound-statement
1438 Objective-C:
1439 attributes objc-class-definition
1440 attributes objc-category-definition
1441 attributes objc-protocol-definition
1443 The simple-asm-expr and attributes are GNU extensions.
1445 This function does not handle __extension__; that is handled in its
1446 callers. ??? Following the old parser, __extension__ may start
1447 external declarations, declarations in functions and declarations
1448 at the start of "for" loops, but not old-style parameter
1449 declarations.
1451 C99 requires declaration specifiers in a function definition; the
1452 absence is diagnosed through the diagnosis of implicit int. In GNU
1453 C we also allow but diagnose declarations without declaration
1454 specifiers, but only at top level (elsewhere they conflict with
1455 other syntax).
1457 In Objective-C, declarations of the looping variable in a foreach
1458 statement are exceptionally terminated by 'in' (for example, 'for
1459 (NSObject *object in array) { ... }').
1461 OpenMP:
1463 declaration:
1464 threadprivate-directive */
1466 static void
1467 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1468 bool static_assert_ok, bool empty_ok,
1469 bool nested, bool start_attr_ok,
1470 tree *objc_foreach_object_declaration)
1472 struct c_declspecs *specs;
1473 tree prefix_attrs;
1474 tree all_prefix_attrs;
1475 bool diagnosed_no_specs = false;
1476 location_t here = c_parser_peek_token (parser)->location;
1478 if (static_assert_ok
1479 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1481 c_parser_static_assert_declaration (parser);
1482 return;
1484 specs = build_null_declspecs ();
1486 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1487 if (c_parser_peek_token (parser)->type == CPP_NAME
1488 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1489 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1490 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1491 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1493 error_at (here, "unknown type name %qE",
1494 c_parser_peek_token (parser)->value);
1496 /* Parse declspecs normally to get a correct pointer type, but avoid
1497 a further "fails to be a type name" error. Refuse nested functions
1498 since it is not how the user likely wants us to recover. */
1499 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1500 c_parser_peek_token (parser)->keyword = RID_VOID;
1501 c_parser_peek_token (parser)->value = error_mark_node;
1502 fndef_ok = !nested;
1505 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1506 if (parser->error)
1508 c_parser_skip_to_end_of_block_or_statement (parser);
1509 return;
1511 if (nested && !specs->declspecs_seen_p)
1513 c_parser_error (parser, "expected declaration specifiers");
1514 c_parser_skip_to_end_of_block_or_statement (parser);
1515 return;
1517 finish_declspecs (specs);
1518 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1520 if (empty_ok)
1521 shadow_tag (specs);
1522 else
1524 shadow_tag_warned (specs, 1);
1525 pedwarn (here, 0, "empty declaration");
1527 c_parser_consume_token (parser);
1528 return;
1531 /* Provide better error recovery. Note that a type name here is usually
1532 better diagnosed as a redeclaration. */
1533 if (empty_ok
1534 && specs->typespec_kind == ctsk_tagdef
1535 && c_parser_next_token_starts_declspecs (parser)
1536 && !c_parser_next_token_is (parser, CPP_NAME))
1538 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1539 parser->error = false;
1540 shadow_tag_warned (specs, 1);
1541 return;
1543 else if (c_dialect_objc ())
1545 /* Prefix attributes are an error on method decls. */
1546 switch (c_parser_peek_token (parser)->type)
1548 case CPP_PLUS:
1549 case CPP_MINUS:
1550 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1551 return;
1552 if (specs->attrs)
1554 warning_at (c_parser_peek_token (parser)->location,
1555 OPT_Wattributes,
1556 "prefix attributes are ignored for methods");
1557 specs->attrs = NULL_TREE;
1559 if (fndef_ok)
1560 c_parser_objc_method_definition (parser);
1561 else
1562 c_parser_objc_methodproto (parser);
1563 return;
1564 break;
1565 default:
1566 break;
1568 /* This is where we parse 'attributes @interface ...',
1569 'attributes @implementation ...', 'attributes @protocol ...'
1570 (where attributes could be, for example, __attribute__
1571 ((deprecated)).
1573 switch (c_parser_peek_token (parser)->keyword)
1575 case RID_AT_INTERFACE:
1577 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1578 return;
1579 c_parser_objc_class_definition (parser, specs->attrs);
1580 return;
1582 break;
1583 case RID_AT_IMPLEMENTATION:
1585 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1586 return;
1587 if (specs->attrs)
1589 warning_at (c_parser_peek_token (parser)->location,
1590 OPT_Wattributes,
1591 "prefix attributes are ignored for implementations");
1592 specs->attrs = NULL_TREE;
1594 c_parser_objc_class_definition (parser, NULL_TREE);
1595 return;
1597 break;
1598 case RID_AT_PROTOCOL:
1600 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1601 return;
1602 c_parser_objc_protocol_definition (parser, specs->attrs);
1603 return;
1605 break;
1606 case RID_AT_ALIAS:
1607 case RID_AT_CLASS:
1608 case RID_AT_END:
1609 case RID_AT_PROPERTY:
1610 if (specs->attrs)
1612 c_parser_error (parser, "unexpected attribute");
1613 specs->attrs = NULL;
1615 break;
1616 default:
1617 break;
1621 pending_xref_error ();
1622 prefix_attrs = specs->attrs;
1623 all_prefix_attrs = prefix_attrs;
1624 specs->attrs = NULL_TREE;
1625 while (true)
1627 struct c_declarator *declarator;
1628 bool dummy = false;
1629 timevar_id_t tv;
1630 tree fnbody;
1631 /* Declaring either one or more declarators (in which case we
1632 should diagnose if there were no declaration specifiers) or a
1633 function definition (in which case the diagnostic for
1634 implicit int suffices). */
1635 declarator = c_parser_declarator (parser,
1636 specs->typespec_kind != ctsk_none,
1637 C_DTR_NORMAL, &dummy);
1638 if (declarator == NULL)
1640 c_parser_skip_to_end_of_block_or_statement (parser);
1641 return;
1643 if (c_parser_next_token_is (parser, CPP_EQ)
1644 || c_parser_next_token_is (parser, CPP_COMMA)
1645 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1646 || c_parser_next_token_is_keyword (parser, RID_ASM)
1647 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1648 || c_parser_next_token_is_keyword (parser, RID_IN))
1650 tree asm_name = NULL_TREE;
1651 tree postfix_attrs = NULL_TREE;
1652 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1654 diagnosed_no_specs = true;
1655 pedwarn (here, 0, "data definition has no type or storage class");
1657 /* Having seen a data definition, there cannot now be a
1658 function definition. */
1659 fndef_ok = false;
1660 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1661 asm_name = c_parser_simple_asm_expr (parser);
1662 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1663 postfix_attrs = c_parser_attributes (parser);
1664 if (c_parser_next_token_is (parser, CPP_EQ))
1666 tree d;
1667 struct c_expr init;
1668 location_t init_loc;
1669 c_parser_consume_token (parser);
1670 /* The declaration of the variable is in effect while
1671 its initializer is parsed. */
1672 d = start_decl (declarator, specs, true,
1673 chainon (postfix_attrs, all_prefix_attrs));
1674 if (!d)
1675 d = error_mark_node;
1676 start_init (d, asm_name, global_bindings_p ());
1677 init_loc = c_parser_peek_token (parser)->location;
1678 init = c_parser_initializer (parser);
1679 finish_init ();
1680 if (d != error_mark_node)
1682 maybe_warn_string_init (TREE_TYPE (d), init);
1683 finish_decl (d, init_loc, init.value,
1684 init.original_type, asm_name);
1687 else
1689 tree d = start_decl (declarator, specs, false,
1690 chainon (postfix_attrs,
1691 all_prefix_attrs));
1692 if (d)
1693 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1694 NULL_TREE, asm_name);
1696 if (c_parser_next_token_is_keyword (parser, RID_IN))
1698 if (d)
1699 *objc_foreach_object_declaration = d;
1700 else
1701 *objc_foreach_object_declaration = error_mark_node;
1704 if (c_parser_next_token_is (parser, CPP_COMMA))
1706 c_parser_consume_token (parser);
1707 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1708 all_prefix_attrs = chainon (c_parser_attributes (parser),
1709 prefix_attrs);
1710 else
1711 all_prefix_attrs = prefix_attrs;
1712 continue;
1714 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1716 c_parser_consume_token (parser);
1717 return;
1719 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1721 /* This can only happen in Objective-C: we found the
1722 'in' that terminates the declaration inside an
1723 Objective-C foreach statement. Do not consume the
1724 token, so that the caller can use it to determine
1725 that this indeed is a foreach context. */
1726 return;
1728 else
1730 c_parser_error (parser, "expected %<,%> or %<;%>");
1731 c_parser_skip_to_end_of_block_or_statement (parser);
1732 return;
1735 else if (!fndef_ok)
1737 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1738 "%<asm%> or %<__attribute__%>");
1739 c_parser_skip_to_end_of_block_or_statement (parser);
1740 return;
1742 /* Function definition (nested or otherwise). */
1743 if (nested)
1745 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1746 c_push_function_context ();
1748 if (!start_function (specs, declarator, all_prefix_attrs))
1750 /* This can appear in many cases looking nothing like a
1751 function definition, so we don't give a more specific
1752 error suggesting there was one. */
1753 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1754 "or %<__attribute__%>");
1755 if (nested)
1756 c_pop_function_context ();
1757 break;
1760 if (DECL_DECLARED_INLINE_P (current_function_decl))
1761 tv = TV_PARSE_INLINE;
1762 else
1763 tv = TV_PARSE_FUNC;
1764 timevar_push (tv);
1766 /* Parse old-style parameter declarations. ??? Attributes are
1767 not allowed to start declaration specifiers here because of a
1768 syntax conflict between a function declaration with attribute
1769 suffix and a function definition with an attribute prefix on
1770 first old-style parameter declaration. Following the old
1771 parser, they are not accepted on subsequent old-style
1772 parameter declarations either. However, there is no
1773 ambiguity after the first declaration, nor indeed on the
1774 first as long as we don't allow postfix attributes after a
1775 declarator with a nonempty identifier list in a definition;
1776 and postfix attributes have never been accepted here in
1777 function definitions either. */
1778 while (c_parser_next_token_is_not (parser, CPP_EOF)
1779 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1780 c_parser_declaration_or_fndef (parser, false, false, false,
1781 true, false, NULL);
1782 store_parm_decls ();
1783 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1784 = c_parser_peek_token (parser)->location;
1785 fnbody = c_parser_compound_statement (parser);
1786 if (nested)
1788 tree decl = current_function_decl;
1789 /* Mark nested functions as needing static-chain initially.
1790 lower_nested_functions will recompute it but the
1791 DECL_STATIC_CHAIN flag is also used before that happens,
1792 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1793 DECL_STATIC_CHAIN (decl) = 1;
1794 add_stmt (fnbody);
1795 finish_function ();
1796 c_pop_function_context ();
1797 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1799 else
1801 add_stmt (fnbody);
1802 finish_function ();
1805 timevar_pop (tv);
1806 break;
1810 /* Parse an asm-definition (asm() outside a function body). This is a
1811 GNU extension.
1813 asm-definition:
1814 simple-asm-expr ;
1817 static void
1818 c_parser_asm_definition (c_parser *parser)
1820 tree asm_str = c_parser_simple_asm_expr (parser);
1821 if (asm_str)
1822 add_asm_node (asm_str);
1823 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1826 /* Parse a static assertion (C11 6.7.10).
1828 static_assert-declaration:
1829 static_assert-declaration-no-semi ;
1832 static void
1833 c_parser_static_assert_declaration (c_parser *parser)
1835 c_parser_static_assert_declaration_no_semi (parser);
1836 if (parser->error
1837 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1838 c_parser_skip_to_end_of_block_or_statement (parser);
1841 /* Parse a static assertion (C11 6.7.10), without the trailing
1842 semicolon.
1844 static_assert-declaration-no-semi:
1845 _Static_assert ( constant-expression , string-literal )
1848 static void
1849 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1851 location_t assert_loc, value_loc;
1852 tree value;
1853 tree string;
1855 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1856 assert_loc = c_parser_peek_token (parser)->location;
1857 if (!flag_isoc11)
1859 if (flag_isoc99)
1860 pedwarn (assert_loc, OPT_Wpedantic,
1861 "ISO C99 does not support %<_Static_assert%>");
1862 else
1863 pedwarn (assert_loc, OPT_Wpedantic,
1864 "ISO C90 does not support %<_Static_assert%>");
1866 c_parser_consume_token (parser);
1867 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1868 return;
1869 value_loc = c_parser_peek_token (parser)->location;
1870 value = c_parser_expr_no_commas (parser, NULL).value;
1871 parser->lex_untranslated_string = true;
1872 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1874 parser->lex_untranslated_string = false;
1875 return;
1877 switch (c_parser_peek_token (parser)->type)
1879 case CPP_STRING:
1880 case CPP_STRING16:
1881 case CPP_STRING32:
1882 case CPP_WSTRING:
1883 case CPP_UTF8STRING:
1884 string = c_parser_peek_token (parser)->value;
1885 c_parser_consume_token (parser);
1886 parser->lex_untranslated_string = false;
1887 break;
1888 default:
1889 c_parser_error (parser, "expected string literal");
1890 parser->lex_untranslated_string = false;
1891 return;
1893 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1895 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1897 error_at (value_loc, "expression in static assertion is not an integer");
1898 return;
1900 if (TREE_CODE (value) != INTEGER_CST)
1902 value = c_fully_fold (value, false, NULL);
1903 if (TREE_CODE (value) == INTEGER_CST)
1904 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
1905 "is not an integer constant expression");
1907 if (TREE_CODE (value) != INTEGER_CST)
1909 error_at (value_loc, "expression in static assertion is not constant");
1910 return;
1912 constant_expression_warning (value);
1913 if (integer_zerop (value))
1914 error_at (assert_loc, "static assertion failed: %E", string);
1917 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1918 6.7), adding them to SPECS (which may already include some).
1919 Storage class specifiers are accepted iff SCSPEC_OK; type
1920 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1921 the start iff START_ATTR_OK.
1923 declaration-specifiers:
1924 storage-class-specifier declaration-specifiers[opt]
1925 type-specifier declaration-specifiers[opt]
1926 type-qualifier declaration-specifiers[opt]
1927 function-specifier declaration-specifiers[opt]
1928 alignment-specifier declaration-specifiers[opt]
1930 Function specifiers (inline) are from C99, and are currently
1931 handled as storage class specifiers, as is __thread. Alignment
1932 specifiers are from C11.
1934 C90 6.5.1, C99 6.7.1:
1935 storage-class-specifier:
1936 typedef
1937 extern
1938 static
1939 auto
1940 register
1942 C99 6.7.4:
1943 function-specifier:
1944 inline
1945 _Noreturn
1947 (_Noreturn is new in C11.)
1949 C90 6.5.2, C99 6.7.2:
1950 type-specifier:
1951 void
1952 char
1953 short
1955 long
1956 float
1957 double
1958 signed
1959 unsigned
1960 _Bool
1961 _Complex
1962 [_Imaginary removed in C99 TC2]
1963 struct-or-union-specifier
1964 enum-specifier
1965 typedef-name
1967 (_Bool and _Complex are new in C99.)
1969 C90 6.5.3, C99 6.7.3:
1971 type-qualifier:
1972 const
1973 restrict
1974 volatile
1975 address-space-qualifier
1977 (restrict is new in C99.)
1979 GNU extensions:
1981 declaration-specifiers:
1982 attributes declaration-specifiers[opt]
1984 type-qualifier:
1985 address-space
1987 address-space:
1988 identifier recognized by the target
1990 storage-class-specifier:
1991 __thread
1993 type-specifier:
1994 typeof-specifier
1995 __int128
1996 _Decimal32
1997 _Decimal64
1998 _Decimal128
1999 _Fract
2000 _Accum
2001 _Sat
2003 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2004 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2006 Objective-C:
2008 type-specifier:
2009 class-name objc-protocol-refs[opt]
2010 typedef-name objc-protocol-refs
2011 objc-protocol-refs
2014 static void
2015 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2016 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2017 enum c_lookahead_kind la)
2019 bool attrs_ok = start_attr_ok;
2020 bool seen_type = specs->typespec_kind != ctsk_none;
2022 if (!typespec_ok)
2023 gcc_assert (la == cla_prefer_id);
2025 while (c_parser_next_token_is (parser, CPP_NAME)
2026 || c_parser_next_token_is (parser, CPP_KEYWORD)
2027 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2029 struct c_typespec t;
2030 tree attrs;
2031 tree align;
2032 location_t loc = c_parser_peek_token (parser)->location;
2034 /* If we cannot accept a type, exit if the next token must start
2035 one. Also, if we already have seen a tagged definition,
2036 a typename would be an error anyway and likely the user
2037 has simply forgotten a semicolon, so we exit. */
2038 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2039 && c_parser_next_tokens_start_typename (parser, la)
2040 && !c_parser_next_token_is_qualifier (parser))
2041 break;
2043 if (c_parser_next_token_is (parser, CPP_NAME))
2045 c_token *name_token = c_parser_peek_token (parser);
2046 tree value = name_token->value;
2047 c_id_kind kind = name_token->id_kind;
2049 if (kind == C_ID_ADDRSPACE)
2051 addr_space_t as
2052 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2053 declspecs_add_addrspace (name_token->location, specs, as);
2054 c_parser_consume_token (parser);
2055 attrs_ok = true;
2056 continue;
2059 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2061 /* If we cannot accept a type, and the next token must start one,
2062 exit. Do the same if we already have seen a tagged definition,
2063 since it would be an error anyway and likely the user has simply
2064 forgotten a semicolon. */
2065 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2066 break;
2068 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2069 a C_ID_CLASSNAME. */
2070 c_parser_consume_token (parser);
2071 seen_type = true;
2072 attrs_ok = true;
2073 if (kind == C_ID_ID)
2075 error ("unknown type name %qE", value);
2076 t.kind = ctsk_typedef;
2077 t.spec = error_mark_node;
2079 else if (kind == C_ID_TYPENAME
2080 && (!c_dialect_objc ()
2081 || c_parser_next_token_is_not (parser, CPP_LESS)))
2083 t.kind = ctsk_typedef;
2084 /* For a typedef name, record the meaning, not the name.
2085 In case of 'foo foo, bar;'. */
2086 t.spec = lookup_name (value);
2088 else
2090 tree proto = NULL_TREE;
2091 gcc_assert (c_dialect_objc ());
2092 t.kind = ctsk_objc;
2093 if (c_parser_next_token_is (parser, CPP_LESS))
2094 proto = c_parser_objc_protocol_refs (parser);
2095 t.spec = objc_get_protocol_qualified_type (value, proto);
2097 t.expr = NULL_TREE;
2098 t.expr_const_operands = true;
2099 declspecs_add_type (name_token->location, specs, t);
2100 continue;
2102 if (c_parser_next_token_is (parser, CPP_LESS))
2104 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2105 nisse@lysator.liu.se. */
2106 tree proto;
2107 gcc_assert (c_dialect_objc ());
2108 if (!typespec_ok || seen_type)
2109 break;
2110 proto = c_parser_objc_protocol_refs (parser);
2111 t.kind = ctsk_objc;
2112 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2113 t.expr = NULL_TREE;
2114 t.expr_const_operands = true;
2115 declspecs_add_type (loc, specs, t);
2116 continue;
2118 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2119 switch (c_parser_peek_token (parser)->keyword)
2121 case RID_STATIC:
2122 case RID_EXTERN:
2123 case RID_REGISTER:
2124 case RID_TYPEDEF:
2125 case RID_INLINE:
2126 case RID_NORETURN:
2127 case RID_AUTO:
2128 case RID_THREAD:
2129 if (!scspec_ok)
2130 goto out;
2131 attrs_ok = true;
2132 /* TODO: Distinguish between function specifiers (inline, noreturn)
2133 and storage class specifiers, either here or in
2134 declspecs_add_scspec. */
2135 declspecs_add_scspec (loc, specs,
2136 c_parser_peek_token (parser)->value);
2137 c_parser_consume_token (parser);
2138 break;
2139 case RID_UNSIGNED:
2140 case RID_LONG:
2141 case RID_INT128:
2142 case RID_SHORT:
2143 case RID_SIGNED:
2144 case RID_COMPLEX:
2145 case RID_INT:
2146 case RID_CHAR:
2147 case RID_FLOAT:
2148 case RID_DOUBLE:
2149 case RID_VOID:
2150 case RID_DFLOAT32:
2151 case RID_DFLOAT64:
2152 case RID_DFLOAT128:
2153 case RID_BOOL:
2154 case RID_FRACT:
2155 case RID_ACCUM:
2156 case RID_SAT:
2157 if (!typespec_ok)
2158 goto out;
2159 attrs_ok = true;
2160 seen_type = true;
2161 if (c_dialect_objc ())
2162 parser->objc_need_raw_identifier = true;
2163 t.kind = ctsk_resword;
2164 t.spec = c_parser_peek_token (parser)->value;
2165 t.expr = NULL_TREE;
2166 t.expr_const_operands = true;
2167 declspecs_add_type (loc, specs, t);
2168 c_parser_consume_token (parser);
2169 break;
2170 case RID_ENUM:
2171 if (!typespec_ok)
2172 goto out;
2173 attrs_ok = true;
2174 seen_type = true;
2175 t = c_parser_enum_specifier (parser);
2176 declspecs_add_type (loc, specs, t);
2177 break;
2178 case RID_STRUCT:
2179 case RID_UNION:
2180 if (!typespec_ok)
2181 goto out;
2182 attrs_ok = true;
2183 seen_type = true;
2184 t = c_parser_struct_or_union_specifier (parser);
2185 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2186 declspecs_add_type (loc, specs, t);
2187 break;
2188 case RID_TYPEOF:
2189 /* ??? The old parser rejected typeof after other type
2190 specifiers, but is a syntax error the best way of
2191 handling this? */
2192 if (!typespec_ok || seen_type)
2193 goto out;
2194 attrs_ok = true;
2195 seen_type = true;
2196 t = c_parser_typeof_specifier (parser);
2197 declspecs_add_type (loc, specs, t);
2198 break;
2199 case RID_CONST:
2200 case RID_VOLATILE:
2201 case RID_RESTRICT:
2202 attrs_ok = true;
2203 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2204 c_parser_consume_token (parser);
2205 break;
2206 /* UPC qualifiers */
2207 case RID_SHARED:
2208 attrs_ok = true;
2209 c_parser_upc_shared_qual (loc, parser, specs);
2210 break;
2211 case RID_STRICT:
2212 case RID_RELAXED:
2213 attrs_ok = true;
2214 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2215 c_parser_consume_token (parser);
2216 break;
2217 case RID_ATTRIBUTE:
2218 if (!attrs_ok)
2219 goto out;
2220 attrs = c_parser_attributes (parser);
2221 declspecs_add_attrs (loc, specs, attrs);
2222 break;
2223 case RID_ALIGNAS:
2224 align = c_parser_alignas_specifier (parser);
2225 declspecs_add_alignas (loc, specs, align);
2226 break;
2227 default:
2228 goto out;
2231 out: ;
2234 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2236 enum-specifier:
2237 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2238 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2239 enum attributes[opt] identifier
2241 The form with trailing comma is new in C99. The forms with
2242 attributes are GNU extensions. In GNU C, we accept any expression
2243 without commas in the syntax (assignment expressions, not just
2244 conditional expressions); assignment expressions will be diagnosed
2245 as non-constant.
2247 enumerator-list:
2248 enumerator
2249 enumerator-list , enumerator
2251 enumerator:
2252 enumeration-constant
2253 enumeration-constant = constant-expression
2256 static struct c_typespec
2257 c_parser_enum_specifier (c_parser *parser)
2259 struct c_typespec ret;
2260 tree attrs;
2261 tree ident = NULL_TREE;
2262 location_t enum_loc;
2263 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2264 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2265 enum_loc = c_parser_peek_token (parser)->location;
2266 c_parser_consume_token (parser);
2267 attrs = c_parser_attributes (parser);
2268 enum_loc = c_parser_peek_token (parser)->location;
2269 /* Set the location in case we create a decl now. */
2270 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2271 if (c_parser_next_token_is (parser, CPP_NAME))
2273 ident = c_parser_peek_token (parser)->value;
2274 ident_loc = c_parser_peek_token (parser)->location;
2275 enum_loc = ident_loc;
2276 c_parser_consume_token (parser);
2278 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2280 /* Parse an enum definition. */
2281 struct c_enum_contents the_enum;
2282 tree type;
2283 tree postfix_attrs;
2284 /* We chain the enumerators in reverse order, then put them in
2285 forward order at the end. */
2286 tree values;
2287 timevar_push (TV_PARSE_ENUM);
2288 type = start_enum (enum_loc, &the_enum, ident);
2289 values = NULL_TREE;
2290 c_parser_consume_token (parser);
2291 while (true)
2293 tree enum_id;
2294 tree enum_value;
2295 tree enum_decl;
2296 bool seen_comma;
2297 c_token *token;
2298 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2299 location_t decl_loc, value_loc;
2300 if (c_parser_next_token_is_not (parser, CPP_NAME))
2302 c_parser_error (parser, "expected identifier");
2303 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2304 values = error_mark_node;
2305 break;
2307 token = c_parser_peek_token (parser);
2308 enum_id = token->value;
2309 /* Set the location in case we create a decl now. */
2310 c_parser_set_source_position_from_token (token);
2311 decl_loc = value_loc = token->location;
2312 c_parser_consume_token (parser);
2313 if (c_parser_next_token_is (parser, CPP_EQ))
2315 c_parser_consume_token (parser);
2316 value_loc = c_parser_peek_token (parser)->location;
2317 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2319 else
2320 enum_value = NULL_TREE;
2321 enum_decl = build_enumerator (decl_loc, value_loc,
2322 &the_enum, enum_id, enum_value);
2323 TREE_CHAIN (enum_decl) = values;
2324 values = enum_decl;
2325 seen_comma = false;
2326 if (c_parser_next_token_is (parser, CPP_COMMA))
2328 comma_loc = c_parser_peek_token (parser)->location;
2329 seen_comma = true;
2330 c_parser_consume_token (parser);
2332 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2334 if (seen_comma && !flag_isoc99)
2335 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2336 c_parser_consume_token (parser);
2337 break;
2339 if (!seen_comma)
2341 c_parser_error (parser, "expected %<,%> or %<}%>");
2342 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2343 values = error_mark_node;
2344 break;
2347 postfix_attrs = c_parser_attributes (parser);
2348 ret.spec = finish_enum (type, nreverse (values),
2349 chainon (attrs, postfix_attrs));
2350 ret.kind = ctsk_tagdef;
2351 ret.expr = NULL_TREE;
2352 ret.expr_const_operands = true;
2353 timevar_pop (TV_PARSE_ENUM);
2354 return ret;
2356 else if (!ident)
2358 c_parser_error (parser, "expected %<{%>");
2359 ret.spec = error_mark_node;
2360 ret.kind = ctsk_tagref;
2361 ret.expr = NULL_TREE;
2362 ret.expr_const_operands = true;
2363 return ret;
2365 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2366 /* In ISO C, enumerated types can be referred to only if already
2367 defined. */
2368 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2370 gcc_assert (ident);
2371 pedwarn (enum_loc, OPT_Wpedantic,
2372 "ISO C forbids forward references to %<enum%> types");
2374 return ret;
2377 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2379 struct-or-union-specifier:
2380 struct-or-union attributes[opt] identifier[opt]
2381 { struct-contents } attributes[opt]
2382 struct-or-union attributes[opt] identifier
2384 struct-contents:
2385 struct-declaration-list
2387 struct-declaration-list:
2388 struct-declaration ;
2389 struct-declaration-list struct-declaration ;
2391 GNU extensions:
2393 struct-contents:
2394 empty
2395 struct-declaration
2396 struct-declaration-list struct-declaration
2398 struct-declaration-list:
2399 struct-declaration-list ;
2402 (Note that in the syntax here, unlike that in ISO C, the semicolons
2403 are included here rather than in struct-declaration, in order to
2404 describe the syntax with extra semicolons and missing semicolon at
2405 end.)
2407 Objective-C:
2409 struct-declaration-list:
2410 @defs ( class-name )
2412 (Note this does not include a trailing semicolon, but can be
2413 followed by further declarations, and gets a pedwarn-if-pedantic
2414 when followed by a semicolon.) */
2416 static struct c_typespec
2417 c_parser_struct_or_union_specifier (c_parser *parser)
2419 struct c_typespec ret;
2420 tree attrs;
2421 tree ident = NULL_TREE;
2422 location_t struct_loc;
2423 location_t ident_loc = UNKNOWN_LOCATION;
2424 enum tree_code code;
2425 switch (c_parser_peek_token (parser)->keyword)
2427 case RID_STRUCT:
2428 code = RECORD_TYPE;
2429 break;
2430 case RID_UNION:
2431 code = UNION_TYPE;
2432 break;
2433 default:
2434 gcc_unreachable ();
2436 struct_loc = c_parser_peek_token (parser)->location;
2437 c_parser_consume_token (parser);
2438 attrs = c_parser_attributes (parser);
2440 /* Set the location in case we create a decl now. */
2441 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2443 if (c_parser_next_token_is (parser, CPP_NAME))
2445 ident = c_parser_peek_token (parser)->value;
2446 ident_loc = c_parser_peek_token (parser)->location;
2447 struct_loc = ident_loc;
2448 c_parser_consume_token (parser);
2450 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2452 /* Parse a struct or union definition. Start the scope of the
2453 tag before parsing components. */
2454 struct c_struct_parse_info *struct_info;
2455 tree type = start_struct (struct_loc, code, ident, &struct_info);
2456 tree postfix_attrs;
2457 /* We chain the components in reverse order, then put them in
2458 forward order at the end. Each struct-declaration may
2459 declare multiple components (comma-separated), so we must use
2460 chainon to join them, although when parsing each
2461 struct-declaration we can use TREE_CHAIN directly.
2463 The theory behind all this is that there will be more
2464 semicolon separated fields than comma separated fields, and
2465 so we'll be minimizing the number of node traversals required
2466 by chainon. */
2467 tree contents;
2468 timevar_push (TV_PARSE_STRUCT);
2469 contents = NULL_TREE;
2470 c_parser_consume_token (parser);
2471 /* Handle the Objective-C @defs construct,
2472 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2473 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2475 tree name;
2476 gcc_assert (c_dialect_objc ());
2477 c_parser_consume_token (parser);
2478 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2479 goto end_at_defs;
2480 if (c_parser_next_token_is (parser, CPP_NAME)
2481 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2483 name = c_parser_peek_token (parser)->value;
2484 c_parser_consume_token (parser);
2486 else
2488 c_parser_error (parser, "expected class name");
2489 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2490 goto end_at_defs;
2492 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2493 "expected %<)%>");
2494 contents = nreverse (objc_get_class_ivars (name));
2496 end_at_defs:
2497 /* Parse the struct-declarations and semicolons. Problems with
2498 semicolons are diagnosed here; empty structures are diagnosed
2499 elsewhere. */
2500 while (true)
2502 tree decls;
2503 /* Parse any stray semicolon. */
2504 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2506 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2507 "extra semicolon in struct or union specified");
2508 c_parser_consume_token (parser);
2509 continue;
2511 /* Stop if at the end of the struct or union contents. */
2512 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2514 c_parser_consume_token (parser);
2515 break;
2517 /* Accept #pragmas at struct scope. */
2518 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2520 c_parser_pragma (parser, pragma_external);
2521 continue;
2523 /* Parse some comma-separated declarations, but not the
2524 trailing semicolon if any. */
2525 decls = c_parser_struct_declaration (parser);
2526 contents = chainon (decls, contents);
2527 /* If no semicolon follows, either we have a parse error or
2528 are at the end of the struct or union and should
2529 pedwarn. */
2530 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2531 c_parser_consume_token (parser);
2532 else
2534 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2535 pedwarn (c_parser_peek_token (parser)->location, 0,
2536 "no semicolon at end of struct or union");
2537 else if (parser->error
2538 || !c_parser_next_token_starts_declspecs (parser))
2540 c_parser_error (parser, "expected %<;%>");
2541 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2542 break;
2545 /* If we come here, we have already emitted an error
2546 for an expected `;', identifier or `(', and we also
2547 recovered already. Go on with the next field. */
2550 postfix_attrs = c_parser_attributes (parser);
2551 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2552 chainon (attrs, postfix_attrs), struct_info);
2553 ret.kind = ctsk_tagdef;
2554 ret.expr = NULL_TREE;
2555 ret.expr_const_operands = true;
2556 timevar_pop (TV_PARSE_STRUCT);
2557 return ret;
2559 else if (!ident)
2561 c_parser_error (parser, "expected %<{%>");
2562 ret.spec = error_mark_node;
2563 ret.kind = ctsk_tagref;
2564 ret.expr = NULL_TREE;
2565 ret.expr_const_operands = true;
2566 return ret;
2568 ret = parser_xref_tag (ident_loc, code, ident);
2569 return ret;
2572 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2573 the trailing semicolon.
2575 struct-declaration:
2576 specifier-qualifier-list struct-declarator-list
2577 static_assert-declaration-no-semi
2579 specifier-qualifier-list:
2580 type-specifier specifier-qualifier-list[opt]
2581 type-qualifier specifier-qualifier-list[opt]
2582 attributes specifier-qualifier-list[opt]
2584 struct-declarator-list:
2585 struct-declarator
2586 struct-declarator-list , attributes[opt] struct-declarator
2588 struct-declarator:
2589 declarator attributes[opt]
2590 declarator[opt] : constant-expression attributes[opt]
2592 GNU extensions:
2594 struct-declaration:
2595 __extension__ struct-declaration
2596 specifier-qualifier-list
2598 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2599 of attributes where shown is a GNU extension. In GNU C, we accept
2600 any expression without commas in the syntax (assignment
2601 expressions, not just conditional expressions); assignment
2602 expressions will be diagnosed as non-constant. */
2604 static tree
2605 c_parser_struct_declaration (c_parser *parser)
2607 struct c_declspecs *specs;
2608 tree prefix_attrs;
2609 tree all_prefix_attrs;
2610 tree decls;
2611 location_t decl_loc;
2612 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2614 int ext;
2615 tree decl;
2616 ext = disable_extension_diagnostics ();
2617 c_parser_consume_token (parser);
2618 decl = c_parser_struct_declaration (parser);
2619 restore_extension_diagnostics (ext);
2620 return decl;
2622 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2624 c_parser_static_assert_declaration_no_semi (parser);
2625 return NULL_TREE;
2627 specs = build_null_declspecs ();
2628 decl_loc = c_parser_peek_token (parser)->location;
2629 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2630 if (parser->error)
2631 return NULL_TREE;
2632 if (!specs->declspecs_seen_p)
2634 c_parser_error (parser, "expected specifier-qualifier-list");
2635 return NULL_TREE;
2637 finish_declspecs (specs);
2638 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2639 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2641 tree ret;
2642 if (specs->typespec_kind == ctsk_none)
2644 pedwarn (decl_loc, OPT_Wpedantic,
2645 "ISO C forbids member declarations with no members");
2646 shadow_tag_warned (specs, pedantic);
2647 ret = NULL_TREE;
2649 else
2651 /* Support for unnamed structs or unions as members of
2652 structs or unions (which is [a] useful and [b] supports
2653 MS P-SDK). */
2654 tree attrs = NULL;
2656 ret = grokfield (c_parser_peek_token (parser)->location,
2657 build_id_declarator (NULL_TREE), specs,
2658 NULL_TREE, &attrs);
2659 if (ret)
2660 decl_attributes (&ret, attrs, 0);
2662 return ret;
2665 /* Provide better error recovery. Note that a type name here is valid,
2666 and will be treated as a field name. */
2667 if (specs->typespec_kind == ctsk_tagdef
2668 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2669 && c_parser_next_token_starts_declspecs (parser)
2670 && !c_parser_next_token_is (parser, CPP_NAME))
2672 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2673 parser->error = false;
2674 return NULL_TREE;
2677 pending_xref_error ();
2678 prefix_attrs = specs->attrs;
2679 all_prefix_attrs = prefix_attrs;
2680 specs->attrs = NULL_TREE;
2681 decls = NULL_TREE;
2682 while (true)
2684 /* Declaring one or more declarators or un-named bit-fields. */
2685 struct c_declarator *declarator;
2686 bool dummy = false;
2687 if (c_parser_next_token_is (parser, CPP_COLON))
2688 declarator = build_id_declarator (NULL_TREE);
2689 else
2690 declarator = c_parser_declarator (parser,
2691 specs->typespec_kind != ctsk_none,
2692 C_DTR_NORMAL, &dummy);
2693 if (declarator == NULL)
2695 c_parser_skip_to_end_of_block_or_statement (parser);
2696 break;
2698 if (c_parser_next_token_is (parser, CPP_COLON)
2699 || c_parser_next_token_is (parser, CPP_COMMA)
2700 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2701 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2702 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2704 tree postfix_attrs = NULL_TREE;
2705 tree width = NULL_TREE;
2706 tree d;
2707 if (c_parser_next_token_is (parser, CPP_COLON))
2709 c_parser_consume_token (parser);
2710 width = c_parser_expr_no_commas (parser, NULL).value;
2712 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2713 postfix_attrs = c_parser_attributes (parser);
2714 d = grokfield (c_parser_peek_token (parser)->location,
2715 declarator, specs, width, &all_prefix_attrs);
2716 decl_attributes (&d, chainon (postfix_attrs,
2717 all_prefix_attrs), 0);
2718 DECL_CHAIN (d) = decls;
2719 decls = d;
2720 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2721 all_prefix_attrs = chainon (c_parser_attributes (parser),
2722 prefix_attrs);
2723 else
2724 all_prefix_attrs = prefix_attrs;
2725 if (c_parser_next_token_is (parser, CPP_COMMA))
2726 c_parser_consume_token (parser);
2727 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2728 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2730 /* Semicolon consumed in caller. */
2731 break;
2733 else
2735 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2736 break;
2739 else
2741 c_parser_error (parser,
2742 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2743 "%<__attribute__%>");
2744 break;
2747 return decls;
2750 /* Parse a typeof specifier (a GNU extension).
2752 typeof-specifier:
2753 typeof ( expression )
2754 typeof ( type-name )
2757 static struct c_typespec
2758 c_parser_typeof_specifier (c_parser *parser)
2760 struct c_typespec ret;
2761 ret.kind = ctsk_typeof;
2762 ret.spec = error_mark_node;
2763 ret.expr = NULL_TREE;
2764 ret.expr_const_operands = true;
2765 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2766 c_parser_consume_token (parser);
2767 c_inhibit_evaluation_warnings++;
2768 in_typeof++;
2769 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2771 c_inhibit_evaluation_warnings--;
2772 in_typeof--;
2773 return ret;
2775 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2777 struct c_type_name *type = c_parser_type_name (parser);
2778 c_inhibit_evaluation_warnings--;
2779 in_typeof--;
2780 if (type != NULL)
2782 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2783 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2786 else
2788 bool was_vm;
2789 location_t here = c_parser_peek_token (parser)->location;
2790 struct c_expr expr = c_parser_expression (parser);
2791 c_inhibit_evaluation_warnings--;
2792 in_typeof--;
2793 if (TREE_CODE (expr.value) == COMPONENT_REF
2794 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2795 error_at (here, "%<typeof%> applied to a bit-field");
2796 mark_exp_read (expr.value);
2797 ret.spec = TREE_TYPE (expr.value);
2798 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2799 /* This is returned with the type so that when the type is
2800 evaluated, this can be evaluated. */
2801 if (was_vm)
2802 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2803 pop_maybe_used (was_vm);
2805 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2806 return ret;
2809 /* Parse an alignment-specifier.
2811 C11 6.7.5:
2813 alignment-specifier:
2814 _Alignas ( type-name )
2815 _Alignas ( constant-expression )
2818 static tree
2819 c_parser_alignas_specifier (c_parser * parser)
2821 tree ret = error_mark_node;
2822 location_t loc = c_parser_peek_token (parser)->location;
2823 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2824 c_parser_consume_token (parser);
2825 if (!flag_isoc11)
2827 if (flag_isoc99)
2828 pedwarn (loc, OPT_Wpedantic,
2829 "ISO C99 does not support %<_Alignas%>");
2830 else
2831 pedwarn (loc, OPT_Wpedantic,
2832 "ISO C90 does not support %<_Alignas%>");
2834 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2835 return ret;
2836 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2838 struct c_type_name *type = c_parser_type_name (parser);
2839 if (type != NULL)
2840 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2842 else
2843 ret = c_parser_expr_no_commas (parser, NULL).value;
2844 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2845 return ret;
2848 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2849 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2850 be redeclared; otherwise it may not. KIND indicates which kind of
2851 declarator is wanted. Returns a valid declarator except in the
2852 case of a syntax error in which case NULL is returned. *SEEN_ID is
2853 set to true if an identifier being declared is seen; this is used
2854 to diagnose bad forms of abstract array declarators and to
2855 determine whether an identifier list is syntactically permitted.
2857 declarator:
2858 pointer[opt] direct-declarator
2860 direct-declarator:
2861 identifier
2862 ( attributes[opt] declarator )
2863 direct-declarator array-declarator
2864 direct-declarator ( parameter-type-list )
2865 direct-declarator ( identifier-list[opt] )
2867 pointer:
2868 * type-qualifier-list[opt]
2869 * type-qualifier-list[opt] pointer
2871 type-qualifier-list:
2872 type-qualifier
2873 attributes
2874 type-qualifier-list type-qualifier
2875 type-qualifier-list attributes
2877 parameter-type-list:
2878 parameter-list
2879 parameter-list , ...
2881 parameter-list:
2882 parameter-declaration
2883 parameter-list , parameter-declaration
2885 parameter-declaration:
2886 declaration-specifiers declarator attributes[opt]
2887 declaration-specifiers abstract-declarator[opt] attributes[opt]
2889 identifier-list:
2890 identifier
2891 identifier-list , identifier
2893 abstract-declarator:
2894 pointer
2895 pointer[opt] direct-abstract-declarator
2897 direct-abstract-declarator:
2898 ( attributes[opt] abstract-declarator )
2899 direct-abstract-declarator[opt] array-declarator
2900 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2902 GNU extensions:
2904 direct-declarator:
2905 direct-declarator ( parameter-forward-declarations
2906 parameter-type-list[opt] )
2908 direct-abstract-declarator:
2909 direct-abstract-declarator[opt] ( parameter-forward-declarations
2910 parameter-type-list[opt] )
2912 parameter-forward-declarations:
2913 parameter-list ;
2914 parameter-forward-declarations parameter-list ;
2916 The uses of attributes shown above are GNU extensions.
2918 Some forms of array declarator are not included in C99 in the
2919 syntax for abstract declarators; these are disallowed elsewhere.
2920 This may be a defect (DR#289).
2922 This function also accepts an omitted abstract declarator as being
2923 an abstract declarator, although not part of the formal syntax. */
2925 static struct c_declarator *
2926 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2927 bool *seen_id)
2929 /* Parse any initial pointer part. */
2930 if (c_parser_next_token_is (parser, CPP_MULT))
2932 struct c_declspecs *quals_attrs = build_null_declspecs ();
2933 struct c_declarator *inner;
2934 c_parser_consume_token (parser);
2935 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2936 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2937 if (inner == NULL)
2938 return NULL;
2939 else
2940 return make_pointer_declarator (quals_attrs, inner);
2942 /* Now we have a direct declarator, direct abstract declarator or
2943 nothing (which counts as a direct abstract declarator here). */
2944 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2947 /* Parse a direct declarator or direct abstract declarator; arguments
2948 as c_parser_declarator. */
2950 static struct c_declarator *
2951 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2952 bool *seen_id)
2954 /* The direct declarator must start with an identifier (possibly
2955 omitted) or a parenthesized declarator (possibly abstract). In
2956 an ordinary declarator, initial parentheses must start a
2957 parenthesized declarator. In an abstract declarator or parameter
2958 declarator, they could start a parenthesized declarator or a
2959 parameter list. To tell which, the open parenthesis and any
2960 following attributes must be read. If a declaration specifier
2961 follows, then it is a parameter list; if the specifier is a
2962 typedef name, there might be an ambiguity about redeclaring it,
2963 which is resolved in the direction of treating it as a typedef
2964 name. If a close parenthesis follows, it is also an empty
2965 parameter list, as the syntax does not permit empty abstract
2966 declarators. Otherwise, it is a parenthesized declarator (in
2967 which case the analysis may be repeated inside it, recursively).
2969 ??? There is an ambiguity in a parameter declaration "int
2970 (__attribute__((foo)) x)", where x is not a typedef name: it
2971 could be an abstract declarator for a function, or declare x with
2972 parentheses. The proper resolution of this ambiguity needs
2973 documenting. At present we follow an accident of the old
2974 parser's implementation, whereby the first parameter must have
2975 some declaration specifiers other than just attributes. Thus as
2976 a parameter declaration it is treated as a parenthesized
2977 parameter named x, and as an abstract declarator it is
2978 rejected.
2980 ??? Also following the old parser, attributes inside an empty
2981 parameter list are ignored, making it a list not yielding a
2982 prototype, rather than giving an error or making it have one
2983 parameter with implicit type int.
2985 ??? Also following the old parser, typedef names may be
2986 redeclared in declarators, but not Objective-C class names. */
2988 if (kind != C_DTR_ABSTRACT
2989 && c_parser_next_token_is (parser, CPP_NAME)
2990 && ((type_seen_p
2991 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2992 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2993 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2995 struct c_declarator *inner
2996 = build_id_declarator (c_parser_peek_token (parser)->value);
2997 *seen_id = true;
2998 inner->id_loc = c_parser_peek_token (parser)->location;
2999 c_parser_consume_token (parser);
3000 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3003 if (kind != C_DTR_NORMAL
3004 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3006 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3007 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3010 /* Either we are at the end of an abstract declarator, or we have
3011 parentheses. */
3013 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3015 tree attrs;
3016 struct c_declarator *inner;
3017 c_parser_consume_token (parser);
3018 attrs = c_parser_attributes (parser);
3019 if (kind != C_DTR_NORMAL
3020 && (c_parser_next_token_starts_declspecs (parser)
3021 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3023 struct c_arg_info *args
3024 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3025 attrs);
3026 if (args == NULL)
3027 return NULL;
3028 else
3030 inner
3031 = build_function_declarator (args,
3032 build_id_declarator (NULL_TREE));
3033 return c_parser_direct_declarator_inner (parser, *seen_id,
3034 inner);
3037 /* A parenthesized declarator. */
3038 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3039 if (inner != NULL && attrs != NULL)
3040 inner = build_attrs_declarator (attrs, inner);
3041 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3043 c_parser_consume_token (parser);
3044 if (inner == NULL)
3045 return NULL;
3046 else
3047 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3049 else
3051 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3052 "expected %<)%>");
3053 return NULL;
3056 else
3058 if (kind == C_DTR_NORMAL)
3060 c_parser_error (parser, "expected identifier or %<(%>");
3061 return NULL;
3063 else
3064 return build_id_declarator (NULL_TREE);
3068 /* Parse part of a direct declarator or direct abstract declarator,
3069 given that some (in INNER) has already been parsed; ID_PRESENT is
3070 true if an identifier is present, false for an abstract
3071 declarator. */
3073 static struct c_declarator *
3074 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3075 struct c_declarator *inner)
3077 /* Parse a sequence of array declarators and parameter lists. */
3078 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3080 location_t brace_loc = c_parser_peek_token (parser)->location;
3081 struct c_declarator *declarator;
3082 struct c_declspecs *quals_attrs = build_null_declspecs ();
3083 bool static_seen;
3084 bool star_seen;
3085 tree dimen;
3086 c_parser_consume_token (parser);
3087 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3088 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3089 if (static_seen)
3090 c_parser_consume_token (parser);
3091 if (static_seen && !quals_attrs->declspecs_seen_p)
3092 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3093 if (!quals_attrs->declspecs_seen_p)
3094 quals_attrs = NULL;
3095 /* If "static" is present, there must be an array dimension.
3096 Otherwise, there may be a dimension, "*", or no
3097 dimension. */
3098 if (static_seen)
3100 star_seen = false;
3101 dimen = c_parser_expr_no_commas (parser, NULL).value;
3103 else
3105 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3107 dimen = NULL_TREE;
3108 star_seen = false;
3110 else if (c_parser_next_token_is (parser, CPP_MULT))
3112 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3114 dimen = NULL_TREE;
3115 star_seen = true;
3116 c_parser_consume_token (parser);
3118 else
3120 star_seen = false;
3121 dimen = c_parser_expr_no_commas (parser, NULL).value;
3124 else
3126 star_seen = false;
3127 dimen = c_parser_expr_no_commas (parser, NULL).value;
3130 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3131 c_parser_consume_token (parser);
3132 else
3134 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3135 "expected %<]%>");
3136 return NULL;
3138 if (dimen)
3139 mark_exp_read (dimen);
3140 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3141 static_seen, star_seen);
3142 if (declarator == NULL)
3143 return NULL;
3144 inner = set_array_declarator_inner (declarator, inner);
3145 return c_parser_direct_declarator_inner (parser, id_present, inner);
3147 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3149 tree attrs;
3150 struct c_arg_info *args;
3151 c_parser_consume_token (parser);
3152 attrs = c_parser_attributes (parser);
3153 args = c_parser_parms_declarator (parser, id_present, attrs);
3154 if (args == NULL)
3155 return NULL;
3156 else
3158 inner = build_function_declarator (args, inner);
3159 return c_parser_direct_declarator_inner (parser, id_present, inner);
3162 return inner;
3165 /* Parse a parameter list or identifier list, including the closing
3166 parenthesis but not the opening one. ATTRS are the attributes at
3167 the start of the list. ID_LIST_OK is true if an identifier list is
3168 acceptable; such a list must not have attributes at the start. */
3170 static struct c_arg_info *
3171 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3173 push_scope ();
3174 declare_parm_level ();
3175 /* If the list starts with an identifier, it is an identifier list.
3176 Otherwise, it is either a prototype list or an empty list. */
3177 if (id_list_ok
3178 && !attrs
3179 && c_parser_next_token_is (parser, CPP_NAME)
3180 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3182 /* Look ahead to detect typos in type names. */
3183 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3184 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3185 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3186 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3188 tree list = NULL_TREE, *nextp = &list;
3189 while (c_parser_next_token_is (parser, CPP_NAME)
3190 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3192 *nextp = build_tree_list (NULL_TREE,
3193 c_parser_peek_token (parser)->value);
3194 nextp = & TREE_CHAIN (*nextp);
3195 c_parser_consume_token (parser);
3196 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3197 break;
3198 c_parser_consume_token (parser);
3199 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3201 c_parser_error (parser, "expected identifier");
3202 break;
3205 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3207 struct c_arg_info *ret = build_arg_info ();
3208 ret->types = list;
3209 c_parser_consume_token (parser);
3210 pop_scope ();
3211 return ret;
3213 else
3215 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3216 "expected %<)%>");
3217 pop_scope ();
3218 return NULL;
3221 else
3223 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3224 NULL);
3225 pop_scope ();
3226 return ret;
3230 /* Parse a parameter list (possibly empty), including the closing
3231 parenthesis but not the opening one. ATTRS are the attributes at
3232 the start of the list. EXPR is NULL or an expression that needs to
3233 be evaluated for the side effects of array size expressions in the
3234 parameters. */
3236 static struct c_arg_info *
3237 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3239 bool bad_parm = false;
3241 /* ??? Following the old parser, forward parameter declarations may
3242 use abstract declarators, and if no real parameter declarations
3243 follow the forward declarations then this is not diagnosed. Also
3244 note as above that attributes are ignored as the only contents of
3245 the parentheses, or as the only contents after forward
3246 declarations. */
3247 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3249 struct c_arg_info *ret = build_arg_info ();
3250 c_parser_consume_token (parser);
3251 return ret;
3253 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3255 struct c_arg_info *ret = build_arg_info ();
3257 if (flag_allow_parameterless_variadic_functions)
3259 /* F (...) is allowed. */
3260 ret->types = NULL_TREE;
3262 else
3264 /* Suppress -Wold-style-definition for this case. */
3265 ret->types = error_mark_node;
3266 error_at (c_parser_peek_token (parser)->location,
3267 "ISO C requires a named argument before %<...%>");
3269 c_parser_consume_token (parser);
3270 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3272 c_parser_consume_token (parser);
3273 return ret;
3275 else
3277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3278 "expected %<)%>");
3279 return NULL;
3282 /* Nonempty list of parameters, either terminated with semicolon
3283 (forward declarations; recurse) or with close parenthesis (normal
3284 function) or with ", ... )" (variadic function). */
3285 while (true)
3287 /* Parse a parameter. */
3288 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3289 attrs = NULL_TREE;
3290 if (parm == NULL)
3291 bad_parm = true;
3292 else
3293 push_parm_decl (parm, &expr);
3294 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3296 tree new_attrs;
3297 c_parser_consume_token (parser);
3298 mark_forward_parm_decls ();
3299 new_attrs = c_parser_attributes (parser);
3300 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3302 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3304 c_parser_consume_token (parser);
3305 if (bad_parm)
3306 return NULL;
3307 else
3308 return get_parm_info (false, expr);
3310 if (!c_parser_require (parser, CPP_COMMA,
3311 "expected %<;%>, %<,%> or %<)%>"))
3313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3314 return NULL;
3316 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3318 c_parser_consume_token (parser);
3319 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3321 c_parser_consume_token (parser);
3322 if (bad_parm)
3323 return NULL;
3324 else
3325 return get_parm_info (true, expr);
3327 else
3329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3330 "expected %<)%>");
3331 return NULL;
3337 /* Parse a parameter declaration. ATTRS are the attributes at the
3338 start of the declaration if it is the first parameter. */
3340 static struct c_parm *
3341 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3343 struct c_declspecs *specs;
3344 struct c_declarator *declarator;
3345 tree prefix_attrs;
3346 tree postfix_attrs = NULL_TREE;
3347 bool dummy = false;
3349 /* Accept #pragmas between parameter declarations. */
3350 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3351 c_parser_pragma (parser, pragma_external);
3353 if (!c_parser_next_token_starts_declspecs (parser))
3355 c_token *token = c_parser_peek_token (parser);
3356 if (parser->error)
3357 return NULL;
3358 c_parser_set_source_position_from_token (token);
3359 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3361 error ("unknown type name %qE", token->value);
3362 parser->error = true;
3364 /* ??? In some Objective-C cases '...' isn't applicable so there
3365 should be a different message. */
3366 else
3367 c_parser_error (parser,
3368 "expected declaration specifiers or %<...%>");
3369 c_parser_skip_to_end_of_parameter (parser);
3370 return NULL;
3372 specs = build_null_declspecs ();
3373 if (attrs)
3375 declspecs_add_attrs (input_location, specs, attrs);
3376 attrs = NULL_TREE;
3378 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3379 finish_declspecs (specs);
3380 pending_xref_error ();
3381 prefix_attrs = specs->attrs;
3382 specs->attrs = NULL_TREE;
3383 declarator = c_parser_declarator (parser,
3384 specs->typespec_kind != ctsk_none,
3385 C_DTR_PARM, &dummy);
3386 if (declarator == NULL)
3388 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3389 return NULL;
3391 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3392 postfix_attrs = c_parser_attributes (parser);
3393 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3394 declarator);
3397 /* Parse a string literal in an asm expression. It should not be
3398 translated, and wide string literals are an error although
3399 permitted by the syntax. This is a GNU extension.
3401 asm-string-literal:
3402 string-literal
3404 ??? At present, following the old parser, the caller needs to have
3405 set lex_untranslated_string to 1. It would be better to follow the
3406 C++ parser rather than using this kludge. */
3408 static tree
3409 c_parser_asm_string_literal (c_parser *parser)
3411 tree str;
3412 int save_flag = warn_overlength_strings;
3413 warn_overlength_strings = 0;
3414 if (c_parser_next_token_is (parser, CPP_STRING))
3416 str = c_parser_peek_token (parser)->value;
3417 c_parser_consume_token (parser);
3419 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3421 error_at (c_parser_peek_token (parser)->location,
3422 "wide string literal in %<asm%>");
3423 str = build_string (1, "");
3424 c_parser_consume_token (parser);
3426 else
3428 c_parser_error (parser, "expected string literal");
3429 str = NULL_TREE;
3431 warn_overlength_strings = save_flag;
3432 return str;
3435 /* Parse a simple asm expression. This is used in restricted
3436 contexts, where a full expression with inputs and outputs does not
3437 make sense. This is a GNU extension.
3439 simple-asm-expr:
3440 asm ( asm-string-literal )
3443 static tree
3444 c_parser_simple_asm_expr (c_parser *parser)
3446 tree str;
3447 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3448 /* ??? Follow the C++ parser rather than using the
3449 lex_untranslated_string kludge. */
3450 parser->lex_untranslated_string = true;
3451 c_parser_consume_token (parser);
3452 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3454 parser->lex_untranslated_string = false;
3455 return NULL_TREE;
3457 str = c_parser_asm_string_literal (parser);
3458 parser->lex_untranslated_string = false;
3459 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3461 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3462 return NULL_TREE;
3464 return str;
3467 static tree
3468 c_parser_attribute_any_word (c_parser *parser)
3470 tree attr_name = NULL_TREE;
3472 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3474 /* ??? See comment above about what keywords are accepted here. */
3475 bool ok;
3476 switch (c_parser_peek_token (parser)->keyword)
3478 case RID_STATIC:
3479 case RID_UNSIGNED:
3480 case RID_LONG:
3481 case RID_INT128:
3482 case RID_CONST:
3483 case RID_EXTERN:
3484 case RID_REGISTER:
3485 case RID_TYPEDEF:
3486 case RID_SHORT:
3487 case RID_INLINE:
3488 case RID_NORETURN:
3489 case RID_VOLATILE:
3490 case RID_SIGNED:
3491 case RID_AUTO:
3492 case RID_RESTRICT:
3493 case RID_COMPLEX:
3494 case RID_THREAD:
3495 case RID_INT:
3496 case RID_CHAR:
3497 case RID_FLOAT:
3498 case RID_DOUBLE:
3499 case RID_VOID:
3500 case RID_DFLOAT32:
3501 case RID_DFLOAT64:
3502 case RID_DFLOAT128:
3503 case RID_BOOL:
3504 case RID_FRACT:
3505 case RID_ACCUM:
3506 case RID_SAT:
3507 case RID_TRANSACTION_ATOMIC:
3508 case RID_TRANSACTION_CANCEL:
3509 ok = true;
3510 break;
3511 default:
3512 ok = false;
3513 break;
3515 if (!ok)
3516 return NULL_TREE;
3518 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3519 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3521 else if (c_parser_next_token_is (parser, CPP_NAME))
3522 attr_name = c_parser_peek_token (parser)->value;
3524 return attr_name;
3527 /* Parse (possibly empty) attributes. This is a GNU extension.
3529 attributes:
3530 empty
3531 attributes attribute
3533 attribute:
3534 __attribute__ ( ( attribute-list ) )
3536 attribute-list:
3537 attrib
3538 attribute_list , attrib
3540 attrib:
3541 empty
3542 any-word
3543 any-word ( identifier )
3544 any-word ( identifier , nonempty-expr-list )
3545 any-word ( expr-list )
3547 where the "identifier" must not be declared as a type, and
3548 "any-word" may be any identifier (including one declared as a
3549 type), a reserved word storage class specifier, type specifier or
3550 type qualifier. ??? This still leaves out most reserved keywords
3551 (following the old parser), shouldn't we include them, and why not
3552 allow identifiers declared as types to start the arguments? */
3554 static tree
3555 c_parser_attributes (c_parser *parser)
3557 tree attrs = NULL_TREE;
3558 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3560 /* ??? Follow the C++ parser rather than using the
3561 lex_untranslated_string kludge. */
3562 parser->lex_untranslated_string = true;
3563 c_parser_consume_token (parser);
3564 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3566 parser->lex_untranslated_string = false;
3567 return attrs;
3569 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3571 parser->lex_untranslated_string = false;
3572 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3573 return attrs;
3575 /* Parse the attribute list. */
3576 while (c_parser_next_token_is (parser, CPP_COMMA)
3577 || c_parser_next_token_is (parser, CPP_NAME)
3578 || c_parser_next_token_is (parser, CPP_KEYWORD))
3580 tree attr, attr_name, attr_args;
3581 vec<tree, va_gc> *expr_list;
3582 if (c_parser_next_token_is (parser, CPP_COMMA))
3584 c_parser_consume_token (parser);
3585 continue;
3588 attr_name = c_parser_attribute_any_word (parser);
3589 if (attr_name == NULL)
3590 break;
3591 c_parser_consume_token (parser);
3592 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3594 attr = build_tree_list (attr_name, NULL_TREE);
3595 attrs = chainon (attrs, attr);
3596 continue;
3598 c_parser_consume_token (parser);
3599 /* Parse the attribute contents. If they start with an
3600 identifier which is followed by a comma or close
3601 parenthesis, then the arguments start with that
3602 identifier; otherwise they are an expression list.
3603 In objective-c the identifier may be a classname. */
3604 if (c_parser_next_token_is (parser, CPP_NAME)
3605 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3606 || (c_dialect_objc ()
3607 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3608 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3609 || (c_parser_peek_2nd_token (parser)->type
3610 == CPP_CLOSE_PAREN)))
3612 tree arg1 = c_parser_peek_token (parser)->value;
3613 c_parser_consume_token (parser);
3614 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3615 attr_args = build_tree_list (NULL_TREE, arg1);
3616 else
3618 tree tree_list;
3619 c_parser_consume_token (parser);
3620 expr_list = c_parser_expr_list (parser, false, true,
3621 NULL, NULL, NULL);
3622 tree_list = build_tree_list_vec (expr_list);
3623 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3624 release_tree_vector (expr_list);
3627 else
3629 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3630 attr_args = NULL_TREE;
3631 else
3633 expr_list = c_parser_expr_list (parser, false, true,
3634 NULL, NULL, NULL);
3635 attr_args = build_tree_list_vec (expr_list);
3636 release_tree_vector (expr_list);
3639 attr = build_tree_list (attr_name, attr_args);
3640 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3641 c_parser_consume_token (parser);
3642 else
3644 parser->lex_untranslated_string = false;
3645 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3646 "expected %<)%>");
3647 return attrs;
3649 attrs = chainon (attrs, attr);
3651 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3652 c_parser_consume_token (parser);
3653 else
3655 parser->lex_untranslated_string = false;
3656 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3657 "expected %<)%>");
3658 return attrs;
3660 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3661 c_parser_consume_token (parser);
3662 else
3664 parser->lex_untranslated_string = false;
3665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3666 "expected %<)%>");
3667 return attrs;
3669 parser->lex_untranslated_string = false;
3671 return attrs;
3674 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3676 type-name:
3677 specifier-qualifier-list abstract-declarator[opt]
3680 static struct c_type_name *
3681 c_parser_type_name (c_parser *parser)
3683 struct c_declspecs *specs = build_null_declspecs ();
3684 struct c_declarator *declarator;
3685 struct c_type_name *ret;
3686 bool dummy = false;
3687 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3688 if (!specs->declspecs_seen_p)
3690 c_parser_error (parser, "expected specifier-qualifier-list");
3691 return NULL;
3693 if (specs->type != error_mark_node)
3695 pending_xref_error ();
3696 finish_declspecs (specs);
3698 declarator = c_parser_declarator (parser,
3699 specs->typespec_kind != ctsk_none,
3700 C_DTR_ABSTRACT, &dummy);
3701 if (declarator == NULL)
3702 return NULL;
3703 ret = XOBNEW (&parser_obstack, struct c_type_name);
3704 ret->specs = specs;
3705 ret->declarator = declarator;
3706 return ret;
3709 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3711 initializer:
3712 assignment-expression
3713 { initializer-list }
3714 { initializer-list , }
3716 initializer-list:
3717 designation[opt] initializer
3718 initializer-list , designation[opt] initializer
3720 designation:
3721 designator-list =
3723 designator-list:
3724 designator
3725 designator-list designator
3727 designator:
3728 array-designator
3729 . identifier
3731 array-designator:
3732 [ constant-expression ]
3734 GNU extensions:
3736 initializer:
3739 designation:
3740 array-designator
3741 identifier :
3743 array-designator:
3744 [ constant-expression ... constant-expression ]
3746 Any expression without commas is accepted in the syntax for the
3747 constant-expressions, with non-constant expressions rejected later.
3749 This function is only used for top-level initializers; for nested
3750 ones, see c_parser_initval. */
3752 static struct c_expr
3753 c_parser_initializer (c_parser *parser)
3755 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3756 return c_parser_braced_init (parser, NULL_TREE, false);
3757 else
3759 struct c_expr ret;
3760 location_t loc = c_parser_peek_token (parser)->location;
3761 ret = c_parser_expr_no_commas (parser, NULL);
3762 if (TREE_CODE (ret.value) != STRING_CST
3763 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3764 ret = default_function_array_read_conversion (loc, ret);
3765 return ret;
3769 /* Parse a braced initializer list. TYPE is the type specified for a
3770 compound literal, and NULL_TREE for other initializers and for
3771 nested braced lists. NESTED_P is true for nested braced lists,
3772 false for the list of a compound literal or the list that is the
3773 top-level initializer in a declaration. */
3775 static struct c_expr
3776 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3778 struct c_expr ret;
3779 struct obstack braced_init_obstack;
3780 location_t brace_loc = c_parser_peek_token (parser)->location;
3781 gcc_obstack_init (&braced_init_obstack);
3782 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3783 c_parser_consume_token (parser);
3784 if (nested_p)
3785 push_init_level (0, &braced_init_obstack);
3786 else
3787 really_start_incremental_init (type);
3788 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3790 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
3792 else
3794 /* Parse a non-empty initializer list, possibly with a trailing
3795 comma. */
3796 while (true)
3798 c_parser_initelt (parser, &braced_init_obstack);
3799 if (parser->error)
3800 break;
3801 if (c_parser_next_token_is (parser, CPP_COMMA))
3802 c_parser_consume_token (parser);
3803 else
3804 break;
3805 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3806 break;
3809 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3811 ret.value = error_mark_node;
3812 ret.original_code = ERROR_MARK;
3813 ret.original_type = NULL;
3814 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3815 pop_init_level (0, &braced_init_obstack);
3816 obstack_free (&braced_init_obstack, NULL);
3817 return ret;
3819 c_parser_consume_token (parser);
3820 ret = pop_init_level (0, &braced_init_obstack);
3821 obstack_free (&braced_init_obstack, NULL);
3822 return ret;
3825 /* Parse a nested initializer, including designators. */
3827 static void
3828 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3830 /* Parse any designator or designator list. A single array
3831 designator may have the subsequent "=" omitted in GNU C, but a
3832 longer list or a structure member designator may not. */
3833 if (c_parser_next_token_is (parser, CPP_NAME)
3834 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3836 /* Old-style structure member designator. */
3837 set_init_label (c_parser_peek_token (parser)->value,
3838 braced_init_obstack);
3839 /* Use the colon as the error location. */
3840 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
3841 "obsolete use of designated initializer with %<:%>");
3842 c_parser_consume_token (parser);
3843 c_parser_consume_token (parser);
3845 else
3847 /* des_seen is 0 if there have been no designators, 1 if there
3848 has been a single array designator and 2 otherwise. */
3849 int des_seen = 0;
3850 /* Location of a designator. */
3851 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3852 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3853 || c_parser_next_token_is (parser, CPP_DOT))
3855 int des_prev = des_seen;
3856 if (!des_seen)
3857 des_loc = c_parser_peek_token (parser)->location;
3858 if (des_seen < 2)
3859 des_seen++;
3860 if (c_parser_next_token_is (parser, CPP_DOT))
3862 des_seen = 2;
3863 c_parser_consume_token (parser);
3864 if (c_parser_next_token_is (parser, CPP_NAME))
3866 set_init_label (c_parser_peek_token (parser)->value,
3867 braced_init_obstack);
3868 c_parser_consume_token (parser);
3870 else
3872 struct c_expr init;
3873 init.value = error_mark_node;
3874 init.original_code = ERROR_MARK;
3875 init.original_type = NULL;
3876 c_parser_error (parser, "expected identifier");
3877 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3878 process_init_element (init, false, braced_init_obstack);
3879 return;
3882 else
3884 tree first, second;
3885 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3886 /* ??? Following the old parser, [ objc-receiver
3887 objc-message-args ] is accepted as an initializer,
3888 being distinguished from a designator by what follows
3889 the first assignment expression inside the square
3890 brackets, but after a first array designator a
3891 subsequent square bracket is for Objective-C taken to
3892 start an expression, using the obsolete form of
3893 designated initializer without '=', rather than
3894 possibly being a second level of designation: in LALR
3895 terms, the '[' is shifted rather than reducing
3896 designator to designator-list. */
3897 if (des_prev == 1 && c_dialect_objc ())
3899 des_seen = des_prev;
3900 break;
3902 if (des_prev == 0 && c_dialect_objc ())
3904 /* This might be an array designator or an
3905 Objective-C message expression. If the former,
3906 continue parsing here; if the latter, parse the
3907 remainder of the initializer given the starting
3908 primary-expression. ??? It might make sense to
3909 distinguish when des_prev == 1 as well; see
3910 previous comment. */
3911 tree rec, args;
3912 struct c_expr mexpr;
3913 c_parser_consume_token (parser);
3914 if (c_parser_peek_token (parser)->type == CPP_NAME
3915 && ((c_parser_peek_token (parser)->id_kind
3916 == C_ID_TYPENAME)
3917 || (c_parser_peek_token (parser)->id_kind
3918 == C_ID_CLASSNAME)))
3920 /* Type name receiver. */
3921 tree id = c_parser_peek_token (parser)->value;
3922 c_parser_consume_token (parser);
3923 rec = objc_get_class_reference (id);
3924 goto parse_message_args;
3926 first = c_parser_expr_no_commas (parser, NULL).value;
3927 mark_exp_read (first);
3928 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3929 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3930 goto array_desig_after_first;
3931 /* Expression receiver. So far only one part
3932 without commas has been parsed; there might be
3933 more of the expression. */
3934 rec = first;
3935 while (c_parser_next_token_is (parser, CPP_COMMA))
3937 struct c_expr next;
3938 location_t comma_loc, exp_loc;
3939 comma_loc = c_parser_peek_token (parser)->location;
3940 c_parser_consume_token (parser);
3941 exp_loc = c_parser_peek_token (parser)->location;
3942 next = c_parser_expr_no_commas (parser, NULL);
3943 next = default_function_array_read_conversion (exp_loc,
3944 next);
3945 rec = build_compound_expr (comma_loc, rec, next.value);
3947 parse_message_args:
3948 /* Now parse the objc-message-args. */
3949 args = c_parser_objc_message_args (parser);
3950 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3951 "expected %<]%>");
3952 mexpr.value
3953 = objc_build_message_expr (rec, args);
3954 mexpr.original_code = ERROR_MARK;
3955 mexpr.original_type = NULL;
3956 /* Now parse and process the remainder of the
3957 initializer, starting with this message
3958 expression as a primary-expression. */
3959 c_parser_initval (parser, &mexpr, braced_init_obstack);
3960 return;
3962 c_parser_consume_token (parser);
3963 first = c_parser_expr_no_commas (parser, NULL).value;
3964 mark_exp_read (first);
3965 array_desig_after_first:
3966 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3968 ellipsis_loc = c_parser_peek_token (parser)->location;
3969 c_parser_consume_token (parser);
3970 second = c_parser_expr_no_commas (parser, NULL).value;
3971 mark_exp_read (second);
3973 else
3974 second = NULL_TREE;
3975 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3977 c_parser_consume_token (parser);
3978 set_init_index (first, second, braced_init_obstack);
3979 if (second)
3980 pedwarn (ellipsis_loc, OPT_Wpedantic,
3981 "ISO C forbids specifying range of elements to initialize");
3983 else
3984 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3985 "expected %<]%>");
3988 if (des_seen >= 1)
3990 if (c_parser_next_token_is (parser, CPP_EQ))
3992 if (!flag_isoc99)
3993 pedwarn (des_loc, OPT_Wpedantic,
3994 "ISO C90 forbids specifying subobject to initialize");
3995 c_parser_consume_token (parser);
3997 else
3999 if (des_seen == 1)
4000 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4001 "obsolete use of designated initializer without %<=%>");
4002 else
4004 struct c_expr init;
4005 init.value = error_mark_node;
4006 init.original_code = ERROR_MARK;
4007 init.original_type = NULL;
4008 c_parser_error (parser, "expected %<=%>");
4009 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4010 process_init_element (init, false, braced_init_obstack);
4011 return;
4016 c_parser_initval (parser, NULL, braced_init_obstack);
4019 /* Parse a nested initializer; as c_parser_initializer but parses
4020 initializers within braced lists, after any designators have been
4021 applied. If AFTER is not NULL then it is an Objective-C message
4022 expression which is the primary-expression starting the
4023 initializer. */
4025 static void
4026 c_parser_initval (c_parser *parser, struct c_expr *after,
4027 struct obstack * braced_init_obstack)
4029 struct c_expr init;
4030 gcc_assert (!after || c_dialect_objc ());
4031 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4032 init = c_parser_braced_init (parser, NULL_TREE, true);
4033 else
4035 location_t loc = c_parser_peek_token (parser)->location;
4036 init = c_parser_expr_no_commas (parser, after);
4037 if (init.value != NULL_TREE
4038 && TREE_CODE (init.value) != STRING_CST
4039 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4040 init = default_function_array_read_conversion (loc, init);
4042 process_init_element (init, false, braced_init_obstack);
4045 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4046 C99 6.8.2).
4048 compound-statement:
4049 { block-item-list[opt] }
4050 { label-declarations block-item-list }
4052 block-item-list:
4053 block-item
4054 block-item-list block-item
4056 block-item:
4057 nested-declaration
4058 statement
4060 nested-declaration:
4061 declaration
4063 GNU extensions:
4065 compound-statement:
4066 { label-declarations block-item-list }
4068 nested-declaration:
4069 __extension__ nested-declaration
4070 nested-function-definition
4072 label-declarations:
4073 label-declaration
4074 label-declarations label-declaration
4076 label-declaration:
4077 __label__ identifier-list ;
4079 Allowing the mixing of declarations and code is new in C99. The
4080 GNU syntax also permits (not shown above) labels at the end of
4081 compound statements, which yield an error. We don't allow labels
4082 on declarations; this might seem like a natural extension, but
4083 there would be a conflict between attributes on the label and
4084 prefix attributes on the declaration. ??? The syntax follows the
4085 old parser in requiring something after label declarations.
4086 Although they are erroneous if the labels declared aren't defined,
4087 is it useful for the syntax to be this way?
4089 OpenMP:
4091 block-item:
4092 openmp-directive
4094 openmp-directive:
4095 barrier-directive
4096 flush-directive */
4098 static tree
4099 c_parser_compound_statement (c_parser *parser)
4101 tree stmt;
4102 location_t brace_loc;
4103 brace_loc = c_parser_peek_token (parser)->location;
4104 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4106 /* Ensure a scope is entered and left anyway to avoid confusion
4107 if we have just prepared to enter a function body. */
4108 stmt = c_begin_compound_stmt (true);
4109 c_end_compound_stmt (brace_loc, stmt, true);
4110 return error_mark_node;
4112 stmt = c_begin_compound_stmt (true);
4113 c_parser_compound_statement_nostart (parser);
4114 return c_end_compound_stmt (brace_loc, stmt, true);
4117 /* Parse a compound statement except for the opening brace. This is
4118 used for parsing both compound statements and statement expressions
4119 (which follow different paths to handling the opening). */
4121 static void
4122 c_parser_compound_statement_nostart (c_parser *parser)
4124 bool last_stmt = false;
4125 bool last_label = false;
4126 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4127 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4128 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4130 c_parser_consume_token (parser);
4131 return;
4133 mark_valid_location_for_stdc_pragma (true);
4134 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4136 /* Read zero or more forward-declarations for labels that nested
4137 functions can jump to. */
4138 mark_valid_location_for_stdc_pragma (false);
4139 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4141 label_loc = c_parser_peek_token (parser)->location;
4142 c_parser_consume_token (parser);
4143 /* Any identifiers, including those declared as type names,
4144 are OK here. */
4145 while (true)
4147 tree label;
4148 if (c_parser_next_token_is_not (parser, CPP_NAME))
4150 c_parser_error (parser, "expected identifier");
4151 break;
4153 label
4154 = declare_label (c_parser_peek_token (parser)->value);
4155 C_DECLARED_LABEL_FLAG (label) = 1;
4156 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4157 c_parser_consume_token (parser);
4158 if (c_parser_next_token_is (parser, CPP_COMMA))
4159 c_parser_consume_token (parser);
4160 else
4161 break;
4163 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4165 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4167 /* We must now have at least one statement, label or declaration. */
4168 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4170 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4171 c_parser_error (parser, "expected declaration or statement");
4172 c_parser_consume_token (parser);
4173 return;
4175 /* Process all #pragma's just after the opening brace. This
4176 handles #pragma upc, which can only appear just after
4177 the opening brace, when it appears within a function body. */
4178 push_upc_consistency_mode ();
4179 permit_pragma_upc ();
4180 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4182 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4183 if (c_parser_pragma (parser, pragma_compound))
4184 last_label = false, last_stmt = true;
4185 parser->error = false;
4187 deny_pragma_upc ();
4188 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4190 location_t loc = c_parser_peek_token (parser)->location;
4191 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4192 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4193 || (c_parser_next_token_is (parser, CPP_NAME)
4194 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4196 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4197 label_loc = c_parser_peek_2nd_token (parser)->location;
4198 else
4199 label_loc = c_parser_peek_token (parser)->location;
4200 last_label = true;
4201 last_stmt = false;
4202 mark_valid_location_for_stdc_pragma (false);
4203 c_parser_label (parser);
4205 else if (!last_label
4206 && c_parser_next_tokens_start_declaration (parser))
4208 last_label = false;
4209 mark_valid_location_for_stdc_pragma (false);
4210 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4211 if (last_stmt)
4212 pedwarn_c90 (loc,
4213 (pedantic && !flag_isoc99)
4214 ? OPT_Wpedantic
4215 : OPT_Wdeclaration_after_statement,
4216 "ISO C90 forbids mixed declarations and code");
4217 last_stmt = false;
4219 else if (!last_label
4220 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4222 /* __extension__ can start a declaration, but is also an
4223 unary operator that can start an expression. Consume all
4224 but the last of a possible series of __extension__ to
4225 determine which. */
4226 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4227 && (c_parser_peek_2nd_token (parser)->keyword
4228 == RID_EXTENSION))
4229 c_parser_consume_token (parser);
4230 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4232 int ext;
4233 ext = disable_extension_diagnostics ();
4234 c_parser_consume_token (parser);
4235 last_label = false;
4236 mark_valid_location_for_stdc_pragma (false);
4237 c_parser_declaration_or_fndef (parser, true, true, true, true,
4238 true, NULL);
4239 /* Following the old parser, __extension__ does not
4240 disable this diagnostic. */
4241 restore_extension_diagnostics (ext);
4242 if (last_stmt)
4243 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4244 ? OPT_Wpedantic
4245 : OPT_Wdeclaration_after_statement,
4246 "ISO C90 forbids mixed declarations and code");
4247 last_stmt = false;
4249 else
4250 goto statement;
4252 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4254 /* External pragmas, and some omp pragmas, are not associated
4255 with regular c code, and so are not to be considered statements
4256 syntactically. This ensures that the user doesn't put them
4257 places that would turn into syntax errors if the directive
4258 were ignored. */
4259 if (c_parser_pragma (parser, pragma_compound))
4260 last_label = false, last_stmt = true;
4262 else if (c_parser_next_token_is (parser, CPP_EOF))
4264 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4265 c_parser_error (parser, "expected declaration or statement");
4266 return;
4268 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4270 if (parser->in_if_block)
4272 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4273 error_at (loc, """expected %<}%> before %<else%>");
4274 return;
4276 else
4278 error_at (loc, "%<else%> without a previous %<if%>");
4279 c_parser_consume_token (parser);
4280 continue;
4283 else
4285 statement:
4286 last_label = false;
4287 last_stmt = true;
4288 mark_valid_location_for_stdc_pragma (false);
4289 c_parser_statement_after_labels (parser);
4292 parser->error = false;
4294 if (last_label)
4295 error_at (label_loc, "label at end of compound statement");
4296 c_parser_consume_token (parser);
4297 pop_upc_consistency_mode ();
4298 /* Restore the value we started with. */
4299 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4302 /* Parse a label (C90 6.6.1, C99 6.8.1).
4304 label:
4305 identifier : attributes[opt]
4306 case constant-expression :
4307 default :
4309 GNU extensions:
4311 label:
4312 case constant-expression ... constant-expression :
4314 The use of attributes on labels is a GNU extension. The syntax in
4315 GNU C accepts any expressions without commas, non-constant
4316 expressions being rejected later. */
4318 static void
4319 c_parser_label (c_parser *parser)
4321 location_t loc1 = c_parser_peek_token (parser)->location;
4322 tree label = NULL_TREE;
4323 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4325 tree exp1, exp2;
4326 c_parser_consume_token (parser);
4327 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4328 if (c_parser_next_token_is (parser, CPP_COLON))
4330 c_parser_consume_token (parser);
4331 label = do_case (loc1, exp1, NULL_TREE);
4333 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4335 c_parser_consume_token (parser);
4336 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4337 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4338 label = do_case (loc1, exp1, exp2);
4340 else
4341 c_parser_error (parser, "expected %<:%> or %<...%>");
4343 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4345 c_parser_consume_token (parser);
4346 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4347 label = do_case (loc1, NULL_TREE, NULL_TREE);
4349 else
4351 tree name = c_parser_peek_token (parser)->value;
4352 tree tlab;
4353 tree attrs;
4354 location_t loc2 = c_parser_peek_token (parser)->location;
4355 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4356 c_parser_consume_token (parser);
4357 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4358 c_parser_consume_token (parser);
4359 attrs = c_parser_attributes (parser);
4360 tlab = define_label (loc2, name);
4361 if (tlab)
4363 decl_attributes (&tlab, attrs, 0);
4364 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4367 if (label)
4369 if (c_parser_next_tokens_start_declaration (parser))
4371 error_at (c_parser_peek_token (parser)->location,
4372 "a label can only be part of a statement and "
4373 "a declaration is not a statement");
4374 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4375 /*static_assert_ok*/ true,
4376 /*empty_ok*/ true, /*nested*/ true,
4377 /*start_attr_ok*/ true, NULL);
4382 /* Parse a statement (C90 6.6, C99 6.8).
4384 statement:
4385 labeled-statement
4386 compound-statement
4387 expression-statement
4388 selection-statement
4389 iteration-statement
4390 jump-statement
4392 labeled-statement:
4393 label statement
4395 expression-statement:
4396 expression[opt] ;
4398 selection-statement:
4399 if-statement
4400 switch-statement
4402 iteration-statement:
4403 while-statement
4404 do-statement
4405 for-statement
4407 jump-statement:
4408 goto identifier ;
4409 continue ;
4410 break ;
4411 return expression[opt] ;
4413 GNU extensions:
4415 statement:
4416 asm-statement
4418 jump-statement:
4419 goto * expression ;
4421 Objective-C:
4423 statement:
4424 objc-throw-statement
4425 objc-try-catch-statement
4426 objc-synchronized-statement
4428 objc-throw-statement:
4429 @throw expression ;
4430 @throw ;
4432 OpenMP:
4434 statement:
4435 openmp-construct
4437 openmp-construct:
4438 parallel-construct
4439 for-construct
4440 sections-construct
4441 single-construct
4442 parallel-for-construct
4443 parallel-sections-construct
4444 master-construct
4445 critical-construct
4446 atomic-construct
4447 ordered-construct
4449 parallel-construct:
4450 parallel-directive structured-block
4452 for-construct:
4453 for-directive iteration-statement
4455 sections-construct:
4456 sections-directive section-scope
4458 single-construct:
4459 single-directive structured-block
4461 parallel-for-construct:
4462 parallel-for-directive iteration-statement
4464 parallel-sections-construct:
4465 parallel-sections-directive section-scope
4467 master-construct:
4468 master-directive structured-block
4470 critical-construct:
4471 critical-directive structured-block
4473 atomic-construct:
4474 atomic-directive expression-statement
4476 ordered-construct:
4477 ordered-directive structured-block
4479 Transactional Memory:
4481 statement:
4482 transaction-statement
4483 transaction-cancel-statement
4486 static void
4487 c_parser_statement (c_parser *parser)
4489 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4490 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4491 || (c_parser_next_token_is (parser, CPP_NAME)
4492 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4493 c_parser_label (parser);
4494 c_parser_statement_after_labels (parser);
4497 /* Parse a statement, other than a labeled statement. */
4499 static void
4500 c_parser_statement_after_labels (c_parser *parser)
4502 location_t loc = c_parser_peek_token (parser)->location;
4503 tree stmt = NULL_TREE;
4504 bool in_if_block = parser->in_if_block;
4505 parser->in_if_block = false;
4506 switch (c_parser_peek_token (parser)->type)
4508 case CPP_OPEN_BRACE:
4509 add_stmt (c_parser_compound_statement (parser));
4510 break;
4511 case CPP_KEYWORD:
4512 switch (c_parser_peek_token (parser)->keyword)
4514 case RID_IF:
4515 c_parser_if_statement (parser);
4516 break;
4517 case RID_SWITCH:
4518 c_parser_switch_statement (parser);
4519 break;
4520 case RID_WHILE:
4521 c_parser_while_statement (parser);
4522 break;
4523 case RID_DO:
4524 c_parser_do_statement (parser);
4525 break;
4526 case RID_FOR:
4527 c_parser_for_statement (parser);
4528 break;
4529 case RID_GOTO:
4530 c_parser_consume_token (parser);
4531 if (c_parser_next_token_is (parser, CPP_NAME))
4533 stmt = c_finish_goto_label (loc,
4534 c_parser_peek_token (parser)->value);
4535 c_parser_consume_token (parser);
4537 else if (c_parser_next_token_is (parser, CPP_MULT))
4539 tree val;
4541 c_parser_consume_token (parser);
4542 val = c_parser_expression (parser).value;
4543 mark_exp_read (val);
4544 stmt = c_finish_goto_ptr (loc, val);
4546 else
4547 c_parser_error (parser, "expected identifier or %<*%>");
4548 goto expect_semicolon;
4549 case RID_CONTINUE:
4550 c_parser_consume_token (parser);
4551 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4552 goto expect_semicolon;
4553 case RID_BREAK:
4554 c_parser_consume_token (parser);
4555 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4556 goto expect_semicolon;
4557 case RID_RETURN:
4558 c_parser_consume_token (parser);
4559 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4561 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4562 c_parser_consume_token (parser);
4564 else
4566 struct c_expr expr = c_parser_expression_conv (parser);
4567 mark_exp_read (expr.value);
4568 stmt = c_finish_return (loc, expr.value, expr.original_type);
4569 goto expect_semicolon;
4571 break;
4572 case RID_ASM:
4573 stmt = c_parser_asm_statement (parser);
4574 break;
4575 case RID_TRANSACTION_ATOMIC:
4576 case RID_TRANSACTION_RELAXED:
4577 stmt = c_parser_transaction (parser,
4578 c_parser_peek_token (parser)->keyword);
4579 break;
4580 case RID_TRANSACTION_CANCEL:
4581 stmt = c_parser_transaction_cancel (parser);
4582 goto expect_semicolon;
4583 case RID_AT_THROW:
4584 gcc_assert (c_dialect_objc ());
4585 c_parser_consume_token (parser);
4586 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4588 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4589 c_parser_consume_token (parser);
4591 else
4593 tree expr = c_parser_expression (parser).value;
4594 expr = c_fully_fold (expr, false, NULL);
4595 stmt = objc_build_throw_stmt (loc, expr);
4596 goto expect_semicolon;
4598 break;
4599 case RID_AT_TRY:
4600 gcc_assert (c_dialect_objc ());
4601 c_parser_objc_try_catch_finally_statement (parser);
4602 break;
4603 case RID_AT_SYNCHRONIZED:
4604 gcc_assert (c_dialect_objc ());
4605 c_parser_objc_synchronized_statement (parser);
4606 break;
4607 case RID_UPC_FORALL:
4608 gcc_assert (c_dialect_upc ());
4609 c_parser_upc_forall_statement (parser);
4610 break;
4611 case RID_UPC_NOTIFY:
4612 gcc_assert (c_dialect_upc ());
4613 c_parser_upc_sync_statement (parser, UPC_SYNC_NOTIFY_OP);
4614 goto expect_semicolon;
4615 case RID_UPC_WAIT:
4616 gcc_assert (c_dialect_upc ());
4617 c_parser_upc_sync_statement (parser, UPC_SYNC_WAIT_OP);
4618 goto expect_semicolon;
4619 case RID_UPC_BARRIER:
4620 gcc_assert (c_dialect_upc ());
4621 c_parser_upc_sync_statement (parser, UPC_SYNC_BARRIER_OP);
4622 goto expect_semicolon;
4623 default:
4624 goto expr_stmt;
4626 break;
4627 case CPP_SEMICOLON:
4628 c_parser_consume_token (parser);
4629 break;
4630 case CPP_CLOSE_PAREN:
4631 case CPP_CLOSE_SQUARE:
4632 /* Avoid infinite loop in error recovery:
4633 c_parser_skip_until_found stops at a closing nesting
4634 delimiter without consuming it, but here we need to consume
4635 it to proceed further. */
4636 c_parser_error (parser, "expected statement");
4637 c_parser_consume_token (parser);
4638 break;
4639 case CPP_PRAGMA:
4640 c_parser_pragma (parser, pragma_stmt);
4641 break;
4642 default:
4643 expr_stmt:
4644 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4645 expect_semicolon:
4646 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4647 break;
4649 /* Two cases cannot and do not have line numbers associated: If stmt
4650 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4651 cannot hold line numbers. But that's OK because the statement
4652 will either be changed to a MODIFY_EXPR during gimplification of
4653 the statement expr, or discarded. If stmt was compound, but
4654 without new variables, we will have skipped the creation of a
4655 BIND and will have a bare STATEMENT_LIST. But that's OK because
4656 (recursively) all of the component statements should already have
4657 line numbers assigned. ??? Can we discard no-op statements
4658 earlier? */
4659 if (CAN_HAVE_LOCATION_P (stmt)
4660 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4661 SET_EXPR_LOCATION (stmt, loc);
4663 parser->in_if_block = in_if_block;
4666 /* Parse the condition from an if, do, while or for statements. */
4668 static tree
4669 c_parser_condition (c_parser *parser)
4671 location_t loc ATTRIBUTE_UNUSED = c_parser_peek_token (parser)->location;
4672 tree cond;
4673 cond = c_parser_expression_conv (parser).value;
4674 cond = c_objc_common_truthvalue_conversion (loc, cond);
4675 cond = c_fully_fold (cond, false, NULL);
4676 if (warn_sequence_point)
4677 verify_sequence_points (cond);
4678 return cond;
4681 /* Parse a parenthesized condition from an if, do or while statement.
4683 condition:
4684 ( expression )
4686 static tree
4687 c_parser_paren_condition (c_parser *parser)
4689 tree cond;
4690 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4691 return error_mark_node;
4692 cond = c_parser_condition (parser);
4693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4694 return cond;
4697 /* Parse a statement which is a block in C99. */
4699 static tree
4700 c_parser_c99_block_statement (c_parser *parser)
4702 tree block = c_begin_compound_stmt (flag_isoc99);
4703 location_t loc = c_parser_peek_token (parser)->location;
4704 c_parser_statement (parser);
4705 return c_end_compound_stmt (loc, block, flag_isoc99);
4708 /* Parse the body of an if statement. This is just parsing a
4709 statement but (a) it is a block in C99, (b) we track whether the
4710 body is an if statement for the sake of -Wparentheses warnings, (c)
4711 we handle an empty body specially for the sake of -Wempty-body
4712 warnings, and (d) we call parser_compound_statement directly
4713 because c_parser_statement_after_labels resets
4714 parser->in_if_block. */
4716 static tree
4717 c_parser_if_body (c_parser *parser, bool *if_p)
4719 tree block = c_begin_compound_stmt (flag_isoc99);
4720 location_t body_loc = c_parser_peek_token (parser)->location;
4721 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4722 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4723 || (c_parser_next_token_is (parser, CPP_NAME)
4724 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4725 c_parser_label (parser);
4726 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4727 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4729 location_t loc = c_parser_peek_token (parser)->location;
4730 add_stmt (build_empty_stmt (loc));
4731 c_parser_consume_token (parser);
4732 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4733 warning_at (loc, OPT_Wempty_body,
4734 "suggest braces around empty body in an %<if%> statement");
4736 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4737 add_stmt (c_parser_compound_statement (parser));
4738 else
4739 c_parser_statement_after_labels (parser);
4740 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4743 /* Parse the else body of an if statement. This is just parsing a
4744 statement but (a) it is a block in C99, (b) we handle an empty body
4745 specially for the sake of -Wempty-body warnings. */
4747 static tree
4748 c_parser_else_body (c_parser *parser)
4750 location_t else_loc = c_parser_peek_token (parser)->location;
4751 tree block = c_begin_compound_stmt (flag_isoc99);
4752 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4753 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4754 || (c_parser_next_token_is (parser, CPP_NAME)
4755 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4756 c_parser_label (parser);
4757 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4759 location_t loc = c_parser_peek_token (parser)->location;
4760 warning_at (loc,
4761 OPT_Wempty_body,
4762 "suggest braces around empty body in an %<else%> statement");
4763 add_stmt (build_empty_stmt (loc));
4764 c_parser_consume_token (parser);
4766 else
4767 c_parser_statement_after_labels (parser);
4768 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4771 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4773 if-statement:
4774 if ( expression ) statement
4775 if ( expression ) statement else statement
4778 static void
4779 c_parser_if_statement (c_parser *parser)
4781 tree block;
4782 location_t loc;
4783 tree cond;
4784 bool first_if = false;
4785 tree first_body, second_body;
4786 bool in_if_block;
4788 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4789 c_parser_consume_token (parser);
4790 block = c_begin_compound_stmt (flag_isoc99);
4791 loc = c_parser_peek_token (parser)->location;
4792 cond = c_parser_paren_condition (parser);
4793 in_if_block = parser->in_if_block;
4794 parser->in_if_block = true;
4795 first_body = c_parser_if_body (parser, &first_if);
4796 parser->in_if_block = in_if_block;
4797 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4799 c_parser_consume_token (parser);
4800 second_body = c_parser_else_body (parser);
4802 else
4803 second_body = NULL_TREE;
4804 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4805 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4808 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4810 switch-statement:
4811 switch (expression) statement
4814 static void
4815 c_parser_switch_statement (c_parser *parser)
4817 tree block, expr, body, save_break;
4818 location_t switch_loc = c_parser_peek_token (parser)->location;
4819 location_t switch_cond_loc;
4820 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4821 c_parser_consume_token (parser);
4822 block = c_begin_compound_stmt (flag_isoc99);
4823 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4825 switch_cond_loc = c_parser_peek_token (parser)->location;
4826 expr = c_parser_expression (parser).value;
4827 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4829 else
4831 switch_cond_loc = UNKNOWN_LOCATION;
4832 expr = error_mark_node;
4834 c_start_case (switch_loc, switch_cond_loc, expr);
4835 save_break = c_break_label;
4836 c_break_label = NULL_TREE;
4837 body = c_parser_c99_block_statement (parser);
4838 c_finish_case (body);
4839 if (c_break_label)
4841 location_t here = c_parser_peek_token (parser)->location;
4842 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4843 SET_EXPR_LOCATION (t, here);
4844 add_stmt (t);
4846 c_break_label = save_break;
4847 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4850 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4852 while-statement:
4853 while (expression) statement
4856 static void
4857 c_parser_while_statement (c_parser *parser)
4859 tree block, cond, body, save_break, save_cont;
4860 location_t loc;
4861 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4862 c_parser_consume_token (parser);
4863 block = c_begin_compound_stmt (flag_isoc99);
4864 loc = c_parser_peek_token (parser)->location;
4865 cond = c_parser_paren_condition (parser);
4866 save_break = c_break_label;
4867 c_break_label = NULL_TREE;
4868 save_cont = c_cont_label;
4869 c_cont_label = NULL_TREE;
4870 body = c_parser_c99_block_statement (parser);
4871 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4872 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4873 c_break_label = save_break;
4874 c_cont_label = save_cont;
4877 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4879 do-statement:
4880 do statement while ( expression ) ;
4883 static void
4884 c_parser_do_statement (c_parser *parser)
4886 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4887 location_t loc;
4888 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4889 c_parser_consume_token (parser);
4890 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4891 warning_at (c_parser_peek_token (parser)->location,
4892 OPT_Wempty_body,
4893 "suggest braces around empty body in %<do%> statement");
4894 block = c_begin_compound_stmt (flag_isoc99);
4895 loc = c_parser_peek_token (parser)->location;
4896 save_break = c_break_label;
4897 c_break_label = NULL_TREE;
4898 save_cont = c_cont_label;
4899 c_cont_label = NULL_TREE;
4900 body = c_parser_c99_block_statement (parser);
4901 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4902 new_break = c_break_label;
4903 c_break_label = save_break;
4904 new_cont = c_cont_label;
4905 c_cont_label = save_cont;
4906 cond = c_parser_paren_condition (parser);
4907 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4908 c_parser_skip_to_end_of_block_or_statement (parser);
4909 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4910 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4913 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4915 for-statement:
4916 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4917 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4919 The form with a declaration is new in C99.
4921 ??? In accordance with the old parser, the declaration may be a
4922 nested function, which is then rejected in check_for_loop_decls,
4923 but does it make any sense for this to be included in the grammar?
4924 Note in particular that the nested function does not include a
4925 trailing ';', whereas the "declaration" production includes one.
4926 Also, can we reject bad declarations earlier and cheaper than
4927 check_for_loop_decls?
4929 In Objective-C, there are two additional variants:
4931 foreach-statement:
4932 for ( expression in expresssion ) statement
4933 for ( declaration in expression ) statement
4935 This is inconsistent with C, because the second variant is allowed
4936 even if c99 is not enabled.
4938 The rest of the comment documents these Objective-C foreach-statement.
4940 Here is the canonical example of the first variant:
4941 for (object in array) { do something with object }
4942 we call the first expression ("object") the "object_expression" and
4943 the second expression ("array") the "collection_expression".
4944 object_expression must be an lvalue of type "id" (a generic Objective-C
4945 object) because the loop works by assigning to object_expression the
4946 various objects from the collection_expression. collection_expression
4947 must evaluate to something of type "id" which responds to the method
4948 countByEnumeratingWithState:objects:count:.
4950 The canonical example of the second variant is:
4951 for (id object in array) { do something with object }
4952 which is completely equivalent to
4954 id object;
4955 for (object in array) { do something with object }
4957 Note that initizializing 'object' in some way (eg, "for ((object =
4958 xxx) in array) { do something with object }") is possibly
4959 technically valid, but completely pointless as 'object' will be
4960 assigned to something else as soon as the loop starts. We should
4961 most likely reject it (TODO).
4963 The beginning of the Objective-C foreach-statement looks exactly
4964 like the beginning of the for-statement, and we can tell it is a
4965 foreach-statement only because the initial declaration or
4966 expression is terminated by 'in' instead of ';'.
4969 static void
4970 c_parser_for_statement (c_parser *parser)
4972 tree block, cond, incr, save_break, save_cont, body;
4973 /* The following are only used when parsing an ObjC foreach statement. */
4974 tree object_expression;
4975 /* Silence the bogus uninitialized warning. */
4976 tree collection_expression = NULL;
4977 location_t loc = c_parser_peek_token (parser)->location;
4978 location_t for_loc = c_parser_peek_token (parser)->location;
4979 bool is_foreach_statement = false;
4980 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4981 c_parser_consume_token (parser);
4982 /* Open a compound statement in Objective-C as well, just in case this is
4983 as foreach expression. */
4984 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4985 cond = error_mark_node;
4986 incr = error_mark_node;
4987 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4989 /* Parse the initialization declaration or expression. */
4990 object_expression = error_mark_node;
4991 parser->objc_could_be_foreach_context = c_dialect_objc ();
4992 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4994 parser->objc_could_be_foreach_context = false;
4995 c_parser_consume_token (parser);
4996 c_finish_expr_stmt (loc, NULL_TREE);
4998 else if (c_parser_next_tokens_start_declaration (parser))
5000 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5001 &object_expression);
5002 parser->objc_could_be_foreach_context = false;
5004 if (c_parser_next_token_is_keyword (parser, RID_IN))
5006 c_parser_consume_token (parser);
5007 is_foreach_statement = true;
5008 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5009 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5011 else
5012 check_for_loop_decls (for_loc, flag_isoc99);
5014 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5016 /* __extension__ can start a declaration, but is also an
5017 unary operator that can start an expression. Consume all
5018 but the last of a possible series of __extension__ to
5019 determine which. */
5020 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5021 && (c_parser_peek_2nd_token (parser)->keyword
5022 == RID_EXTENSION))
5023 c_parser_consume_token (parser);
5024 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5026 int ext;
5027 ext = disable_extension_diagnostics ();
5028 c_parser_consume_token (parser);
5029 c_parser_declaration_or_fndef (parser, true, true, true, true,
5030 true, &object_expression);
5031 parser->objc_could_be_foreach_context = false;
5033 restore_extension_diagnostics (ext);
5034 if (c_parser_next_token_is_keyword (parser, RID_IN))
5036 c_parser_consume_token (parser);
5037 is_foreach_statement = true;
5038 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5039 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5041 else
5042 check_for_loop_decls (for_loc, flag_isoc99);
5044 else
5045 goto init_expr;
5047 else
5049 init_expr:
5051 tree init_expression;
5052 init_expression = c_parser_expression (parser).value;
5053 parser->objc_could_be_foreach_context = false;
5054 if (c_parser_next_token_is_keyword (parser, RID_IN))
5056 c_parser_consume_token (parser);
5057 is_foreach_statement = true;
5058 if (! lvalue_p (init_expression))
5059 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5060 object_expression = c_fully_fold (init_expression, false, NULL);
5062 else
5064 c_finish_expr_stmt (loc, init_expression);
5065 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5069 /* Parse the loop condition. In the case of a foreach
5070 statement, there is no loop condition. */
5071 gcc_assert (!parser->objc_could_be_foreach_context);
5072 if (!is_foreach_statement)
5074 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5076 c_parser_consume_token (parser);
5077 cond = NULL_TREE;
5079 else
5081 cond = c_parser_condition (parser);
5082 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5085 /* Parse the increment expression (the third expression in a
5086 for-statement). In the case of a foreach-statement, this is
5087 the expression that follows the 'in'. */
5088 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5090 if (is_foreach_statement)
5092 c_parser_error (parser, "missing collection in fast enumeration");
5093 collection_expression = error_mark_node;
5095 else
5096 incr = c_process_expr_stmt (loc, NULL_TREE);
5098 else
5100 if (is_foreach_statement)
5101 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5102 false, NULL);
5103 else
5104 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5108 save_break = c_break_label;
5109 c_break_label = NULL_TREE;
5110 save_cont = c_cont_label;
5111 c_cont_label = NULL_TREE;
5112 body = c_parser_c99_block_statement (parser);
5113 if (is_foreach_statement)
5114 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5115 else
5116 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5117 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5118 c_break_label = save_break;
5119 c_cont_label = save_cont;
5122 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5123 statement with inputs, outputs, clobbers, and volatile tag
5124 allowed.
5126 asm-statement:
5127 asm type-qualifier[opt] ( asm-argument ) ;
5128 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5130 asm-argument:
5131 asm-string-literal
5132 asm-string-literal : asm-operands[opt]
5133 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5134 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5136 asm-goto-argument:
5137 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5138 : asm-goto-operands
5140 Qualifiers other than volatile are accepted in the syntax but
5141 warned for. */
5143 static tree
5144 c_parser_asm_statement (c_parser *parser)
5146 tree quals, str, outputs, inputs, clobbers, labels, ret;
5147 bool simple, is_goto;
5148 location_t asm_loc = c_parser_peek_token (parser)->location;
5149 int section, nsections;
5151 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5152 c_parser_consume_token (parser);
5153 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5155 quals = c_parser_peek_token (parser)->value;
5156 c_parser_consume_token (parser);
5158 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5159 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5161 warning_at (c_parser_peek_token (parser)->location,
5163 "%E qualifier ignored on asm",
5164 c_parser_peek_token (parser)->value);
5165 quals = NULL_TREE;
5166 c_parser_consume_token (parser);
5168 else
5169 quals = NULL_TREE;
5171 is_goto = false;
5172 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5174 c_parser_consume_token (parser);
5175 is_goto = true;
5178 /* ??? Follow the C++ parser rather than using the
5179 lex_untranslated_string kludge. */
5180 parser->lex_untranslated_string = true;
5181 ret = NULL;
5183 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5184 goto error;
5186 str = c_parser_asm_string_literal (parser);
5187 if (str == NULL_TREE)
5188 goto error_close_paren;
5190 simple = true;
5191 outputs = NULL_TREE;
5192 inputs = NULL_TREE;
5193 clobbers = NULL_TREE;
5194 labels = NULL_TREE;
5196 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5197 goto done_asm;
5199 /* Parse each colon-delimited section of operands. */
5200 nsections = 3 + is_goto;
5201 for (section = 0; section < nsections; ++section)
5203 if (!c_parser_require (parser, CPP_COLON,
5204 is_goto
5205 ? "expected %<:%>"
5206 : "expected %<:%> or %<)%>"))
5207 goto error_close_paren;
5209 /* Once past any colon, we're no longer a simple asm. */
5210 simple = false;
5212 if ((!c_parser_next_token_is (parser, CPP_COLON)
5213 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5214 || section == 3)
5215 switch (section)
5217 case 0:
5218 /* For asm goto, we don't allow output operands, but reserve
5219 the slot for a future extension that does allow them. */
5220 if (!is_goto)
5221 outputs = c_parser_asm_operands (parser);
5222 break;
5223 case 1:
5224 inputs = c_parser_asm_operands (parser);
5225 break;
5226 case 2:
5227 clobbers = c_parser_asm_clobbers (parser);
5228 break;
5229 case 3:
5230 labels = c_parser_asm_goto_operands (parser);
5231 break;
5232 default:
5233 gcc_unreachable ();
5236 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5237 goto done_asm;
5240 done_asm:
5241 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5244 goto error;
5247 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5248 c_parser_skip_to_end_of_block_or_statement (parser);
5250 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5251 clobbers, labels, simple));
5253 error:
5254 parser->lex_untranslated_string = false;
5255 return ret;
5257 error_close_paren:
5258 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5259 goto error;
5262 /* Parse asm operands, a GNU extension.
5264 asm-operands:
5265 asm-operand
5266 asm-operands , asm-operand
5268 asm-operand:
5269 asm-string-literal ( expression )
5270 [ identifier ] asm-string-literal ( expression )
5273 static tree
5274 c_parser_asm_operands (c_parser *parser)
5276 tree list = NULL_TREE;
5277 while (true)
5279 tree name, str;
5280 struct c_expr expr;
5281 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5283 c_parser_consume_token (parser);
5284 if (c_parser_next_token_is (parser, CPP_NAME))
5286 tree id = c_parser_peek_token (parser)->value;
5287 c_parser_consume_token (parser);
5288 name = build_string (IDENTIFIER_LENGTH (id),
5289 IDENTIFIER_POINTER (id));
5291 else
5293 c_parser_error (parser, "expected identifier");
5294 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5295 return NULL_TREE;
5297 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5298 "expected %<]%>");
5300 else
5301 name = NULL_TREE;
5302 str = c_parser_asm_string_literal (parser);
5303 if (str == NULL_TREE)
5304 return NULL_TREE;
5305 parser->lex_untranslated_string = false;
5306 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5308 parser->lex_untranslated_string = true;
5309 return NULL_TREE;
5311 expr = c_parser_expression (parser);
5312 mark_exp_read (expr.value);
5313 parser->lex_untranslated_string = true;
5314 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5317 return NULL_TREE;
5319 list = chainon (list, build_tree_list (build_tree_list (name, str),
5320 expr.value));
5321 if (c_parser_next_token_is (parser, CPP_COMMA))
5322 c_parser_consume_token (parser);
5323 else
5324 break;
5326 return list;
5329 /* Parse asm clobbers, a GNU extension.
5331 asm-clobbers:
5332 asm-string-literal
5333 asm-clobbers , asm-string-literal
5336 static tree
5337 c_parser_asm_clobbers (c_parser *parser)
5339 tree list = NULL_TREE;
5340 while (true)
5342 tree str = c_parser_asm_string_literal (parser);
5343 if (str)
5344 list = tree_cons (NULL_TREE, str, list);
5345 else
5346 return NULL_TREE;
5347 if (c_parser_next_token_is (parser, CPP_COMMA))
5348 c_parser_consume_token (parser);
5349 else
5350 break;
5352 return list;
5355 /* Parse asm goto labels, a GNU extension.
5357 asm-goto-operands:
5358 identifier
5359 asm-goto-operands , identifier
5362 static tree
5363 c_parser_asm_goto_operands (c_parser *parser)
5365 tree list = NULL_TREE;
5366 while (true)
5368 tree name, label;
5370 if (c_parser_next_token_is (parser, CPP_NAME))
5372 c_token *tok = c_parser_peek_token (parser);
5373 name = tok->value;
5374 label = lookup_label_for_goto (tok->location, name);
5375 c_parser_consume_token (parser);
5376 TREE_USED (label) = 1;
5378 else
5380 c_parser_error (parser, "expected identifier");
5381 return NULL_TREE;
5384 name = build_string (IDENTIFIER_LENGTH (name),
5385 IDENTIFIER_POINTER (name));
5386 list = tree_cons (name, label, list);
5387 if (c_parser_next_token_is (parser, CPP_COMMA))
5388 c_parser_consume_token (parser);
5389 else
5390 return nreverse (list);
5394 /* Parse an expression other than a compound expression; that is, an
5395 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5396 NULL then it is an Objective-C message expression which is the
5397 primary-expression starting the expression as an initializer.
5399 assignment-expression:
5400 conditional-expression
5401 unary-expression assignment-operator assignment-expression
5403 assignment-operator: one of
5404 = *= /= %= += -= <<= >>= &= ^= |=
5406 In GNU C we accept any conditional expression on the LHS and
5407 diagnose the invalid lvalue rather than producing a syntax
5408 error. */
5410 static struct c_expr
5411 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5413 struct c_expr lhs, rhs, ret;
5414 enum tree_code code;
5415 location_t op_location, exp_location;
5416 gcc_assert (!after || c_dialect_objc ());
5417 lhs = c_parser_conditional_expression (parser, after);
5418 op_location = c_parser_peek_token (parser)->location;
5419 switch (c_parser_peek_token (parser)->type)
5421 case CPP_EQ:
5422 code = NOP_EXPR;
5423 break;
5424 case CPP_MULT_EQ:
5425 code = MULT_EXPR;
5426 break;
5427 case CPP_DIV_EQ:
5428 code = TRUNC_DIV_EXPR;
5429 break;
5430 case CPP_MOD_EQ:
5431 code = TRUNC_MOD_EXPR;
5432 break;
5433 case CPP_PLUS_EQ:
5434 code = PLUS_EXPR;
5435 break;
5436 case CPP_MINUS_EQ:
5437 code = MINUS_EXPR;
5438 break;
5439 case CPP_LSHIFT_EQ:
5440 code = LSHIFT_EXPR;
5441 break;
5442 case CPP_RSHIFT_EQ:
5443 code = RSHIFT_EXPR;
5444 break;
5445 case CPP_AND_EQ:
5446 code = BIT_AND_EXPR;
5447 break;
5448 case CPP_XOR_EQ:
5449 code = BIT_XOR_EXPR;
5450 break;
5451 case CPP_OR_EQ:
5452 code = BIT_IOR_EXPR;
5453 break;
5454 default:
5455 return lhs;
5457 c_parser_consume_token (parser);
5458 exp_location = c_parser_peek_token (parser)->location;
5459 rhs = c_parser_expr_no_commas (parser, NULL);
5460 rhs = default_function_array_read_conversion (exp_location, rhs);
5461 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5462 code, exp_location, rhs.value,
5463 rhs.original_type);
5464 if (code == NOP_EXPR)
5465 ret.original_code = MODIFY_EXPR;
5466 else
5468 TREE_NO_WARNING (ret.value) = 1;
5469 ret.original_code = ERROR_MARK;
5471 ret.original_type = NULL;
5472 return ret;
5475 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5476 is not NULL then it is an Objective-C message expression which is
5477 the primary-expression starting the expression as an initializer.
5479 conditional-expression:
5480 logical-OR-expression
5481 logical-OR-expression ? expression : conditional-expression
5483 GNU extensions:
5485 conditional-expression:
5486 logical-OR-expression ? : conditional-expression
5489 static struct c_expr
5490 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5492 struct c_expr cond, exp1, exp2, ret;
5493 location_t cond_loc, colon_loc, middle_loc;
5495 gcc_assert (!after || c_dialect_objc ());
5497 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5499 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5500 return cond;
5501 cond_loc = c_parser_peek_token (parser)->location;
5502 cond = default_function_array_read_conversion (cond_loc, cond);
5503 c_parser_consume_token (parser);
5504 if (c_parser_next_token_is (parser, CPP_COLON))
5506 tree eptype = NULL_TREE;
5508 middle_loc = c_parser_peek_token (parser)->location;
5509 pedwarn (middle_loc, OPT_Wpedantic,
5510 "ISO C forbids omitting the middle term of a ?: expression");
5511 warn_for_omitted_condop (middle_loc, cond.value);
5512 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5514 eptype = TREE_TYPE (cond.value);
5515 cond.value = TREE_OPERAND (cond.value, 0);
5517 /* Make sure first operand is calculated only once. */
5518 exp1.value = c_save_expr (default_conversion (cond.value));
5519 if (eptype)
5520 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5521 exp1.original_type = NULL;
5522 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5523 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5525 else
5527 cond.value
5528 = c_objc_common_truthvalue_conversion
5529 (cond_loc, default_conversion (cond.value));
5530 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5531 exp1 = c_parser_expression_conv (parser);
5532 mark_exp_read (exp1.value);
5533 c_inhibit_evaluation_warnings +=
5534 ((cond.value == truthvalue_true_node)
5535 - (cond.value == truthvalue_false_node));
5538 colon_loc = c_parser_peek_token (parser)->location;
5539 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5541 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5542 ret.value = error_mark_node;
5543 ret.original_code = ERROR_MARK;
5544 ret.original_type = NULL;
5545 return ret;
5548 location_t exp2_loc = c_parser_peek_token (parser)->location;
5549 exp2 = c_parser_conditional_expression (parser, NULL);
5550 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5552 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5553 ret.value = build_conditional_expr (colon_loc, cond.value,
5554 cond.original_code == C_MAYBE_CONST_EXPR,
5555 exp1.value, exp1.original_type,
5556 exp2.value, exp2.original_type);
5557 ret.original_code = ERROR_MARK;
5558 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5559 ret.original_type = NULL;
5560 else
5562 tree t1, t2;
5564 /* If both sides are enum type, the default conversion will have
5565 made the type of the result be an integer type. We want to
5566 remember the enum types we started with. */
5567 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5568 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5569 ret.original_type = ((t1 != error_mark_node
5570 && t2 != error_mark_node
5571 && (TYPE_MAIN_VARIANT (t1)
5572 == TYPE_MAIN_VARIANT (t2)))
5573 ? t1
5574 : NULL);
5576 return ret;
5579 /* Parse a binary expression; that is, a logical-OR-expression (C90
5580 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5581 an Objective-C message expression which is the primary-expression
5582 starting the expression as an initializer. PREC is the starting
5583 precedence, usually PREC_NONE.
5585 multiplicative-expression:
5586 cast-expression
5587 multiplicative-expression * cast-expression
5588 multiplicative-expression / cast-expression
5589 multiplicative-expression % cast-expression
5591 additive-expression:
5592 multiplicative-expression
5593 additive-expression + multiplicative-expression
5594 additive-expression - multiplicative-expression
5596 shift-expression:
5597 additive-expression
5598 shift-expression << additive-expression
5599 shift-expression >> additive-expression
5601 relational-expression:
5602 shift-expression
5603 relational-expression < shift-expression
5604 relational-expression > shift-expression
5605 relational-expression <= shift-expression
5606 relational-expression >= shift-expression
5608 equality-expression:
5609 relational-expression
5610 equality-expression == relational-expression
5611 equality-expression != relational-expression
5613 AND-expression:
5614 equality-expression
5615 AND-expression & equality-expression
5617 exclusive-OR-expression:
5618 AND-expression
5619 exclusive-OR-expression ^ AND-expression
5621 inclusive-OR-expression:
5622 exclusive-OR-expression
5623 inclusive-OR-expression | exclusive-OR-expression
5625 logical-AND-expression:
5626 inclusive-OR-expression
5627 logical-AND-expression && inclusive-OR-expression
5629 logical-OR-expression:
5630 logical-AND-expression
5631 logical-OR-expression || logical-AND-expression
5634 static struct c_expr
5635 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5636 enum c_parser_prec prec)
5638 /* A binary expression is parsed using operator-precedence parsing,
5639 with the operands being cast expressions. All the binary
5640 operators are left-associative. Thus a binary expression is of
5641 form:
5643 E0 op1 E1 op2 E2 ...
5645 which we represent on a stack. On the stack, the precedence
5646 levels are strictly increasing. When a new operator is
5647 encountered of higher precedence than that at the top of the
5648 stack, it is pushed; its LHS is the top expression, and its RHS
5649 is everything parsed until it is popped. When a new operator is
5650 encountered with precedence less than or equal to that at the top
5651 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5652 by the result of the operation until the operator at the top of
5653 the stack has lower precedence than the new operator or there is
5654 only one element on the stack; then the top expression is the LHS
5655 of the new operator. In the case of logical AND and OR
5656 expressions, we also need to adjust c_inhibit_evaluation_warnings
5657 as appropriate when the operators are pushed and popped. */
5659 struct {
5660 /* The expression at this stack level. */
5661 struct c_expr expr;
5662 /* The precedence of the operator on its left, PREC_NONE at the
5663 bottom of the stack. */
5664 enum c_parser_prec prec;
5665 /* The operation on its left. */
5666 enum tree_code op;
5667 /* The source location of this operation. */
5668 location_t loc;
5669 } stack[NUM_PRECS];
5670 int sp;
5671 /* Location of the binary operator. */
5672 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5673 #define POP \
5674 do { \
5675 switch (stack[sp].op) \
5677 case TRUTH_ANDIF_EXPR: \
5678 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5679 == truthvalue_false_node); \
5680 break; \
5681 case TRUTH_ORIF_EXPR: \
5682 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5683 == truthvalue_true_node); \
5684 break; \
5685 default: \
5686 break; \
5688 stack[sp - 1].expr \
5689 = default_function_array_read_conversion (stack[sp - 1].loc, \
5690 stack[sp - 1].expr); \
5691 stack[sp].expr \
5692 = default_function_array_read_conversion (stack[sp].loc, \
5693 stack[sp].expr); \
5694 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5695 stack[sp].op, \
5696 stack[sp - 1].expr, \
5697 stack[sp].expr); \
5698 sp--; \
5699 } while (0)
5700 gcc_assert (!after || c_dialect_objc ());
5701 stack[0].loc = c_parser_peek_token (parser)->location;
5702 stack[0].expr = c_parser_cast_expression (parser, after);
5703 stack[0].prec = prec;
5704 sp = 0;
5705 while (true)
5707 enum c_parser_prec oprec;
5708 enum tree_code ocode;
5709 if (parser->error)
5710 goto out;
5711 switch (c_parser_peek_token (parser)->type)
5713 case CPP_MULT:
5714 oprec = PREC_MULT;
5715 ocode = MULT_EXPR;
5716 break;
5717 case CPP_DIV:
5718 oprec = PREC_MULT;
5719 ocode = TRUNC_DIV_EXPR;
5720 break;
5721 case CPP_MOD:
5722 oprec = PREC_MULT;
5723 ocode = TRUNC_MOD_EXPR;
5724 break;
5725 case CPP_PLUS:
5726 oprec = PREC_ADD;
5727 ocode = PLUS_EXPR;
5728 break;
5729 case CPP_MINUS:
5730 oprec = PREC_ADD;
5731 ocode = MINUS_EXPR;
5732 break;
5733 case CPP_LSHIFT:
5734 oprec = PREC_SHIFT;
5735 ocode = LSHIFT_EXPR;
5736 break;
5737 case CPP_RSHIFT:
5738 oprec = PREC_SHIFT;
5739 ocode = RSHIFT_EXPR;
5740 break;
5741 case CPP_LESS:
5742 oprec = PREC_REL;
5743 ocode = LT_EXPR;
5744 break;
5745 case CPP_GREATER:
5746 oprec = PREC_REL;
5747 ocode = GT_EXPR;
5748 break;
5749 case CPP_LESS_EQ:
5750 oprec = PREC_REL;
5751 ocode = LE_EXPR;
5752 break;
5753 case CPP_GREATER_EQ:
5754 oprec = PREC_REL;
5755 ocode = GE_EXPR;
5756 break;
5757 case CPP_EQ_EQ:
5758 oprec = PREC_EQ;
5759 ocode = EQ_EXPR;
5760 break;
5761 case CPP_NOT_EQ:
5762 oprec = PREC_EQ;
5763 ocode = NE_EXPR;
5764 break;
5765 case CPP_AND:
5766 oprec = PREC_BITAND;
5767 ocode = BIT_AND_EXPR;
5768 break;
5769 case CPP_XOR:
5770 oprec = PREC_BITXOR;
5771 ocode = BIT_XOR_EXPR;
5772 break;
5773 case CPP_OR:
5774 oprec = PREC_BITOR;
5775 ocode = BIT_IOR_EXPR;
5776 break;
5777 case CPP_AND_AND:
5778 oprec = PREC_LOGAND;
5779 ocode = TRUTH_ANDIF_EXPR;
5780 break;
5781 case CPP_OR_OR:
5782 oprec = PREC_LOGOR;
5783 ocode = TRUTH_ORIF_EXPR;
5784 break;
5785 default:
5786 /* Not a binary operator, so end of the binary
5787 expression. */
5788 goto out;
5790 binary_loc = c_parser_peek_token (parser)->location;
5791 while (oprec <= stack[sp].prec)
5793 if (sp == 0)
5794 goto out;
5795 POP;
5797 c_parser_consume_token (parser);
5798 switch (ocode)
5800 case TRUTH_ANDIF_EXPR:
5801 stack[sp].expr
5802 = default_function_array_read_conversion (stack[sp].loc,
5803 stack[sp].expr);
5804 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5805 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5806 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5807 == truthvalue_false_node);
5808 break;
5809 case TRUTH_ORIF_EXPR:
5810 stack[sp].expr
5811 = default_function_array_read_conversion (stack[sp].loc,
5812 stack[sp].expr);
5813 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5814 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5815 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5816 == truthvalue_true_node);
5817 break;
5818 default:
5819 break;
5821 sp++;
5822 stack[sp].loc = binary_loc;
5823 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5824 stack[sp].prec = oprec;
5825 stack[sp].op = ocode;
5826 stack[sp].loc = binary_loc;
5828 out:
5829 while (sp > 0)
5830 POP;
5831 return stack[0].expr;
5832 #undef POP
5835 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5836 NULL then it is an Objective-C message expression which is the
5837 primary-expression starting the expression as an initializer.
5839 cast-expression:
5840 unary-expression
5841 ( type-name ) unary-expression
5844 static struct c_expr
5845 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5847 location_t cast_loc = c_parser_peek_token (parser)->location;
5848 gcc_assert (!after || c_dialect_objc ());
5849 if (after)
5850 return c_parser_postfix_expression_after_primary (parser,
5851 cast_loc, *after);
5852 /* If the expression begins with a parenthesized type name, it may
5853 be either a cast or a compound literal; we need to see whether
5854 the next character is '{' to tell the difference. If not, it is
5855 an unary expression. Full detection of unknown typenames here
5856 would require a 3-token lookahead. */
5857 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5858 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5860 struct c_type_name *type_name;
5861 struct c_expr ret;
5862 struct c_expr expr;
5863 c_parser_consume_token (parser);
5864 type_name = c_parser_type_name (parser);
5865 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5866 if (type_name == NULL)
5868 ret.value = error_mark_node;
5869 ret.original_code = ERROR_MARK;
5870 ret.original_type = NULL;
5871 return ret;
5874 /* Save casted types in the function's used types hash table. */
5875 used_types_insert (type_name->specs->type);
5877 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5878 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5879 cast_loc);
5881 location_t expr_loc = c_parser_peek_token (parser)->location;
5882 expr = c_parser_cast_expression (parser, NULL);
5883 expr = default_function_array_read_conversion (expr_loc, expr);
5885 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5886 ret.original_code = ERROR_MARK;
5887 ret.original_type = NULL;
5888 return ret;
5890 else
5891 return c_parser_unary_expression (parser);
5894 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5896 unary-expression:
5897 postfix-expression
5898 ++ unary-expression
5899 -- unary-expression
5900 unary-operator cast-expression
5901 sizeof unary-expression
5902 sizeof ( type-name )
5904 unary-operator: one of
5905 & * + - ~ !
5907 GNU extensions:
5909 unary-expression:
5910 __alignof__ unary-expression
5911 __alignof__ ( type-name )
5912 && identifier
5914 (C11 permits _Alignof with type names only.)
5916 unary-operator: one of
5917 __extension__ __real__ __imag__
5919 Transactional Memory:
5921 unary-expression:
5922 transaction-expression
5924 In addition, the GNU syntax treats ++ and -- as unary operators, so
5925 they may be applied to cast expressions with errors for non-lvalues
5926 given later. */
5928 static struct c_expr
5929 c_parser_unary_expression (c_parser *parser)
5931 int ext;
5932 struct c_expr ret, op;
5933 location_t op_loc = c_parser_peek_token (parser)->location;
5934 location_t exp_loc;
5935 ret.original_code = ERROR_MARK;
5936 ret.original_type = NULL;
5937 switch (c_parser_peek_token (parser)->type)
5939 case CPP_PLUS_PLUS:
5940 c_parser_consume_token (parser);
5941 exp_loc = c_parser_peek_token (parser)->location;
5942 op = c_parser_cast_expression (parser, NULL);
5943 op = default_function_array_read_conversion (exp_loc, op);
5944 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5945 case CPP_MINUS_MINUS:
5946 c_parser_consume_token (parser);
5947 exp_loc = c_parser_peek_token (parser)->location;
5948 op = c_parser_cast_expression (parser, NULL);
5949 op = default_function_array_read_conversion (exp_loc, op);
5950 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5951 case CPP_AND:
5952 c_parser_consume_token (parser);
5953 op = c_parser_cast_expression (parser, NULL);
5954 mark_exp_read (op.value);
5955 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5956 case CPP_MULT:
5957 c_parser_consume_token (parser);
5958 exp_loc = c_parser_peek_token (parser)->location;
5959 op = c_parser_cast_expression (parser, NULL);
5960 op = default_function_array_read_conversion (exp_loc, op);
5961 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5962 return ret;
5963 case CPP_PLUS:
5964 if (!c_dialect_objc () && !in_system_header)
5965 warning_at (op_loc,
5966 OPT_Wtraditional,
5967 "traditional C rejects the unary plus operator");
5968 c_parser_consume_token (parser);
5969 exp_loc = c_parser_peek_token (parser)->location;
5970 op = c_parser_cast_expression (parser, NULL);
5971 op = default_function_array_read_conversion (exp_loc, op);
5972 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5973 case CPP_MINUS:
5974 c_parser_consume_token (parser);
5975 exp_loc = c_parser_peek_token (parser)->location;
5976 op = c_parser_cast_expression (parser, NULL);
5977 op = default_function_array_read_conversion (exp_loc, op);
5978 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5979 case CPP_COMPL:
5980 c_parser_consume_token (parser);
5981 exp_loc = c_parser_peek_token (parser)->location;
5982 op = c_parser_cast_expression (parser, NULL);
5983 op = default_function_array_read_conversion (exp_loc, op);
5984 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5985 case CPP_NOT:
5986 c_parser_consume_token (parser);
5987 exp_loc = c_parser_peek_token (parser)->location;
5988 op = c_parser_cast_expression (parser, NULL);
5989 op = default_function_array_read_conversion (exp_loc, op);
5990 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5991 case CPP_AND_AND:
5992 /* Refer to the address of a label as a pointer. */
5993 c_parser_consume_token (parser);
5994 if (c_parser_next_token_is (parser, CPP_NAME))
5996 ret.value = finish_label_address_expr
5997 (c_parser_peek_token (parser)->value, op_loc);
5998 c_parser_consume_token (parser);
6000 else
6002 c_parser_error (parser, "expected identifier");
6003 ret.value = error_mark_node;
6005 return ret;
6006 case CPP_KEYWORD:
6007 switch (c_parser_peek_token (parser)->keyword)
6009 case RID_SIZEOF:
6010 return c_parser_sizeof_expression (parser);
6011 case RID_UPC_BLOCKSIZEOF:
6012 case RID_UPC_ELEMSIZEOF:
6013 case RID_UPC_LOCALSIZEOF:
6014 gcc_assert (c_dialect_upc ());
6015 return c_parser_sizeof_expression (parser);
6016 case RID_ALIGNOF:
6017 return c_parser_alignof_expression (parser);
6018 case RID_EXTENSION:
6019 c_parser_consume_token (parser);
6020 ext = disable_extension_diagnostics ();
6021 ret = c_parser_cast_expression (parser, NULL);
6022 restore_extension_diagnostics (ext);
6023 return ret;
6024 case RID_REALPART:
6025 c_parser_consume_token (parser);
6026 exp_loc = c_parser_peek_token (parser)->location;
6027 op = c_parser_cast_expression (parser, NULL);
6028 op = default_function_array_conversion (exp_loc, op);
6029 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6030 case RID_IMAGPART:
6031 c_parser_consume_token (parser);
6032 exp_loc = c_parser_peek_token (parser)->location;
6033 op = c_parser_cast_expression (parser, NULL);
6034 op = default_function_array_conversion (exp_loc, op);
6035 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6036 case RID_TRANSACTION_ATOMIC:
6037 case RID_TRANSACTION_RELAXED:
6038 return c_parser_transaction_expression (parser,
6039 c_parser_peek_token (parser)->keyword);
6040 default:
6041 return c_parser_postfix_expression (parser);
6043 default:
6044 return c_parser_postfix_expression (parser);
6048 /* Return the result of upc_blocksizeof applied to EXPR. */
6050 static
6051 struct c_expr
6052 upc_blocksizeof_expr (location_t loc, struct c_expr expr)
6054 struct c_expr ret;
6055 ret.original_code = ERROR_MARK;
6056 ret.original_type = NULL_TREE;
6057 if (expr.value == error_mark_node)
6059 ret.value = error_mark_node;
6060 pop_maybe_used (false);
6062 else
6064 ret.value = upc_blocksizeof (loc, TREE_TYPE (expr.value));
6065 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6067 return ret;
6070 /* Return the result of upc_blocksizeof applied to T, a structure
6071 for the type name passed to sizeof (rather than the type itself). */
6073 static
6074 struct c_expr
6075 upc_blocksizeof_type (location_t loc, struct c_type_name *t)
6077 tree type;
6078 struct c_expr ret;
6079 ret.original_code = ERROR_MARK;
6080 ret.original_type = NULL_TREE;
6081 type = groktypename (t, NULL, NULL);
6082 if (type == error_mark_node)
6084 ret.value = error_mark_node;
6085 pop_maybe_used (false);
6087 else
6089 ret.value = upc_blocksizeof (loc, type);
6090 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6092 return ret;
6095 /* Return the result of upc_elemsizeof applied to EXPR. */
6097 static
6098 struct c_expr
6099 upc_elemsizeof_expr (location_t loc, struct c_expr expr)
6101 struct c_expr ret;
6102 ret.original_code = ERROR_MARK;
6103 ret.original_type = NULL_TREE;
6104 if (expr.value == error_mark_node)
6106 ret.value = error_mark_node;
6107 pop_maybe_used (false);
6109 else
6111 ret.value = upc_elemsizeof (loc, TREE_TYPE (expr.value));
6112 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6114 return ret;
6117 /* Return the result of upc_elemsizeof applied to T, a structure
6118 for the type name passed to sizeof (rather than the type itself). */
6120 static
6121 struct c_expr
6122 upc_elemsizeof_type (location_t loc, struct c_type_name *t)
6124 tree type;
6125 struct c_expr ret;
6126 ret.original_code = ERROR_MARK;
6127 ret.original_type = NULL_TREE;
6128 type = groktypename (t, NULL, NULL);
6129 if (type == error_mark_node)
6131 ret.value = error_mark_node;
6132 pop_maybe_used (false);
6134 else
6136 ret.value = upc_elemsizeof (loc, type);
6137 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6139 return ret;
6142 /* Return the result of upc_localsizeof applied to EXPR. */
6144 static
6145 struct c_expr
6146 upc_localsizeof_expr (location_t loc, struct c_expr expr)
6148 struct c_expr ret;
6149 ret.original_code = ERROR_MARK;
6150 ret.original_type = NULL_TREE;
6151 if (expr.value == error_mark_node)
6153 ret.value = error_mark_node;
6154 pop_maybe_used (false);
6156 else
6158 ret.value = upc_localsizeof (loc, TREE_TYPE (expr.value));
6159 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
6161 return ret;
6164 /* Return the result of upc_localsizeof applied to T, a structure
6165 for the type name passed to sizeof (rather than the type itself). */
6167 static
6168 struct c_expr
6169 upc_localsizeof_type (location_t loc, struct c_type_name *t)
6171 tree type;
6172 struct c_expr ret;
6173 ret.original_code = ERROR_MARK;
6174 ret.original_type = NULL_TREE;
6175 type = groktypename (t, NULL, NULL);
6176 if (type == error_mark_node)
6178 ret.value = error_mark_node;
6179 pop_maybe_used (false);
6181 else
6183 ret.value = upc_localsizeof (loc, type);
6184 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
6186 return ret;
6189 /* Parse a sizeof expression. */
6191 static struct c_expr
6192 c_parser_sizeof_expression (c_parser *parser)
6194 struct c_expr expr;
6195 location_t expr_loc;
6196 enum rid keyword = c_parser_peek_token (parser)->keyword;
6197 c_parser_consume_token (parser);
6198 c_inhibit_evaluation_warnings++;
6199 in_sizeof++;
6200 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6201 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6203 /* Either sizeof ( type-name ) or sizeof unary-expression
6204 starting with a compound literal. */
6205 struct c_type_name *type_name;
6206 c_parser_consume_token (parser);
6207 expr_loc = c_parser_peek_token (parser)->location;
6208 type_name = c_parser_type_name (parser);
6209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6210 if (type_name == NULL)
6212 struct c_expr ret;
6213 c_inhibit_evaluation_warnings--;
6214 in_sizeof--;
6215 ret.value = error_mark_node;
6216 ret.original_code = ERROR_MARK;
6217 ret.original_type = NULL;
6218 return ret;
6220 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6222 expr = c_parser_postfix_expression_after_paren_type (parser,
6223 type_name,
6224 expr_loc);
6225 goto sizeof_expr;
6227 /* sizeof ( type-name ). */
6228 c_inhibit_evaluation_warnings--;
6229 in_sizeof--;
6230 /* Handle upc_*_sizeof (type) operations. */
6231 switch (keyword)
6233 case RID_UPC_BLOCKSIZEOF:
6234 return upc_blocksizeof_type (expr_loc, type_name);
6235 case RID_UPC_ELEMSIZEOF:
6236 return upc_elemsizeof_type (expr_loc, type_name);
6237 case RID_UPC_LOCALSIZEOF:
6238 return upc_localsizeof_type (expr_loc, type_name);
6239 default: break;
6241 return c_expr_sizeof_type (expr_loc, type_name);
6243 else
6245 expr_loc = c_parser_peek_token (parser)->location;
6246 expr = c_parser_unary_expression (parser);
6247 sizeof_expr:
6248 c_inhibit_evaluation_warnings--;
6249 in_sizeof--;
6250 mark_exp_read (expr.value);
6251 if (TREE_CODE (expr.value) == COMPONENT_REF
6252 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6253 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6254 /* Handle upc_*_sizeof (expr) operations. */
6255 switch (keyword)
6257 case RID_UPC_BLOCKSIZEOF:
6258 return upc_blocksizeof_expr (expr_loc, expr);
6259 case RID_UPC_ELEMSIZEOF:
6260 return upc_elemsizeof_expr (expr_loc, expr);
6261 case RID_UPC_LOCALSIZEOF:
6262 return upc_localsizeof_expr (expr_loc, expr);
6263 case RID_SIZEOF:
6264 return c_expr_sizeof_expr (expr_loc, expr);
6265 default: break;
6267 return c_expr_sizeof_expr (expr_loc, expr);
6271 /* Parse an alignof expression. */
6273 static struct c_expr
6274 c_parser_alignof_expression (c_parser *parser)
6276 struct c_expr expr;
6277 location_t loc = c_parser_peek_token (parser)->location;
6278 tree alignof_spelling = c_parser_peek_token (parser)->value;
6279 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6280 /* A diagnostic is not required for the use of this identifier in
6281 the implementation namespace; only diagnose it for the C11
6282 spelling because of existing code using the other spellings. */
6283 if (!flag_isoc11
6284 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6286 if (flag_isoc99)
6287 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6288 alignof_spelling);
6289 else
6290 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6291 alignof_spelling);
6293 c_parser_consume_token (parser);
6294 c_inhibit_evaluation_warnings++;
6295 in_alignof++;
6296 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6297 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6299 /* Either __alignof__ ( type-name ) or __alignof__
6300 unary-expression starting with a compound literal. */
6301 location_t loc;
6302 struct c_type_name *type_name;
6303 struct c_expr ret;
6304 c_parser_consume_token (parser);
6305 loc = c_parser_peek_token (parser)->location;
6306 type_name = c_parser_type_name (parser);
6307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6308 if (type_name == NULL)
6310 struct c_expr ret;
6311 c_inhibit_evaluation_warnings--;
6312 in_alignof--;
6313 ret.value = error_mark_node;
6314 ret.original_code = ERROR_MARK;
6315 ret.original_type = NULL;
6316 return ret;
6318 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6320 expr = c_parser_postfix_expression_after_paren_type (parser,
6321 type_name,
6322 loc);
6323 goto alignof_expr;
6325 /* alignof ( type-name ). */
6326 c_inhibit_evaluation_warnings--;
6327 in_alignof--;
6328 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6329 ret.original_code = ERROR_MARK;
6330 ret.original_type = NULL;
6331 return ret;
6333 else
6335 struct c_expr ret;
6336 expr = c_parser_unary_expression (parser);
6337 alignof_expr:
6338 mark_exp_read (expr.value);
6339 c_inhibit_evaluation_warnings--;
6340 in_alignof--;
6341 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6342 alignof_spelling);
6343 ret.value = c_alignof_expr (loc, expr.value);
6344 ret.original_code = ERROR_MARK;
6345 ret.original_type = NULL;
6346 return ret;
6350 /* Helper function to read arguments of builtins which are interfaces
6351 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6352 others. The name of the builtin is passed using BNAME parameter.
6353 Function returns true if there were no errors while parsing and
6354 stores the arguments in CEXPR_LIST. */
6355 static bool
6356 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6357 vec<c_expr_t, va_gc> **ret_cexpr_list)
6359 location_t loc = c_parser_peek_token (parser)->location;
6360 vec<c_expr_t, va_gc> *cexpr_list;
6361 c_expr_t expr;
6363 *ret_cexpr_list = NULL;
6364 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6366 error_at (loc, "cannot take address of %qs", bname);
6367 return false;
6370 c_parser_consume_token (parser);
6372 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6374 c_parser_consume_token (parser);
6375 return true;
6378 expr = c_parser_expr_no_commas (parser, NULL);
6379 vec_alloc (cexpr_list, 1);
6380 C_EXPR_APPEND (cexpr_list, expr);
6381 while (c_parser_next_token_is (parser, CPP_COMMA))
6383 c_parser_consume_token (parser);
6384 expr = c_parser_expr_no_commas (parser, NULL);
6385 C_EXPR_APPEND (cexpr_list, expr);
6388 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6389 return false;
6391 *ret_cexpr_list = cexpr_list;
6392 return true;
6396 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6398 postfix-expression:
6399 primary-expression
6400 postfix-expression [ expression ]
6401 postfix-expression ( argument-expression-list[opt] )
6402 postfix-expression . identifier
6403 postfix-expression -> identifier
6404 postfix-expression ++
6405 postfix-expression --
6406 ( type-name ) { initializer-list }
6407 ( type-name ) { initializer-list , }
6409 argument-expression-list:
6410 argument-expression
6411 argument-expression-list , argument-expression
6413 primary-expression:
6414 identifier
6415 constant
6416 string-literal
6417 ( expression )
6419 GNU extensions:
6421 primary-expression:
6422 __func__
6423 (treated as a keyword in GNU C)
6424 __FUNCTION__
6425 __PRETTY_FUNCTION__
6426 ( compound-statement )
6427 __builtin_va_arg ( assignment-expression , type-name )
6428 __builtin_offsetof ( type-name , offsetof-member-designator )
6429 __builtin_choose_expr ( assignment-expression ,
6430 assignment-expression ,
6431 assignment-expression )
6432 __builtin_types_compatible_p ( type-name , type-name )
6433 __builtin_complex ( assignment-expression , assignment-expression )
6434 __builtin_shuffle ( assignment-expression , assignment-expression )
6435 __builtin_shuffle ( assignment-expression ,
6436 assignment-expression ,
6437 assignment-expression, )
6439 offsetof-member-designator:
6440 identifier
6441 offsetof-member-designator . identifier
6442 offsetof-member-designator [ expression ]
6444 Objective-C:
6446 primary-expression:
6447 [ objc-receiver objc-message-args ]
6448 @selector ( objc-selector-arg )
6449 @protocol ( identifier )
6450 @encode ( type-name )
6451 objc-string-literal
6452 Classname . identifier
6455 static struct c_expr
6456 c_parser_postfix_expression (c_parser *parser)
6458 struct c_expr expr, e1;
6459 struct c_type_name *t1, *t2;
6460 location_t loc = c_parser_peek_token (parser)->location;;
6461 expr.original_code = ERROR_MARK;
6462 expr.original_type = NULL;
6463 switch (c_parser_peek_token (parser)->type)
6465 case CPP_NUMBER:
6466 expr.value = c_parser_peek_token (parser)->value;
6467 loc = c_parser_peek_token (parser)->location;
6468 c_parser_consume_token (parser);
6469 if (TREE_CODE (expr.value) == FIXED_CST
6470 && !targetm.fixed_point_supported_p ())
6472 error_at (loc, "fixed-point types not supported for this target");
6473 expr.value = error_mark_node;
6475 break;
6476 case CPP_CHAR:
6477 case CPP_CHAR16:
6478 case CPP_CHAR32:
6479 case CPP_WCHAR:
6480 expr.value = c_parser_peek_token (parser)->value;
6481 c_parser_consume_token (parser);
6482 break;
6483 case CPP_STRING:
6484 case CPP_STRING16:
6485 case CPP_STRING32:
6486 case CPP_WSTRING:
6487 case CPP_UTF8STRING:
6488 expr.value = c_parser_peek_token (parser)->value;
6489 expr.original_code = STRING_CST;
6490 c_parser_consume_token (parser);
6491 break;
6492 case CPP_OBJC_STRING:
6493 gcc_assert (c_dialect_objc ());
6494 expr.value
6495 = objc_build_string_object (c_parser_peek_token (parser)->value);
6496 c_parser_consume_token (parser);
6497 break;
6498 case CPP_NAME:
6499 switch (c_parser_peek_token (parser)->id_kind)
6501 case C_ID_ID:
6503 tree id = c_parser_peek_token (parser)->value;
6504 c_parser_consume_token (parser);
6505 expr.value = build_external_ref (loc, id,
6506 (c_parser_peek_token (parser)->type
6507 == CPP_OPEN_PAREN),
6508 &expr.original_type);
6509 break;
6511 case C_ID_CLASSNAME:
6513 /* Here we parse the Objective-C 2.0 Class.name dot
6514 syntax. */
6515 tree class_name = c_parser_peek_token (parser)->value;
6516 tree component;
6517 c_parser_consume_token (parser);
6518 gcc_assert (c_dialect_objc ());
6519 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6521 expr.value = error_mark_node;
6522 break;
6524 if (c_parser_next_token_is_not (parser, CPP_NAME))
6526 c_parser_error (parser, "expected identifier");
6527 expr.value = error_mark_node;
6528 break;
6530 component = c_parser_peek_token (parser)->value;
6531 c_parser_consume_token (parser);
6532 expr.value = objc_build_class_component_ref (class_name,
6533 component);
6534 break;
6536 default:
6537 c_parser_error (parser, "expected expression");
6538 expr.value = error_mark_node;
6539 break;
6541 break;
6542 case CPP_OPEN_PAREN:
6543 /* A parenthesized expression, statement expression or compound
6544 literal. */
6545 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6547 /* A statement expression. */
6548 tree stmt;
6549 location_t brace_loc;
6550 c_parser_consume_token (parser);
6551 brace_loc = c_parser_peek_token (parser)->location;
6552 c_parser_consume_token (parser);
6553 if (!building_stmt_list_p ())
6555 error_at (loc, "braced-group within expression allowed "
6556 "only inside a function");
6557 parser->error = true;
6558 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6559 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6560 expr.value = error_mark_node;
6561 break;
6563 stmt = c_begin_stmt_expr ();
6564 c_parser_compound_statement_nostart (parser);
6565 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6566 "expected %<)%>");
6567 pedwarn (loc, OPT_Wpedantic,
6568 "ISO C forbids braced-groups within expressions");
6569 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6570 mark_exp_read (expr.value);
6572 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6574 /* A compound literal. ??? Can we actually get here rather
6575 than going directly to
6576 c_parser_postfix_expression_after_paren_type from
6577 elsewhere? */
6578 location_t loc;
6579 struct c_type_name *type_name;
6580 c_parser_consume_token (parser);
6581 loc = c_parser_peek_token (parser)->location;
6582 type_name = c_parser_type_name (parser);
6583 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6584 "expected %<)%>");
6585 if (type_name == NULL)
6587 expr.value = error_mark_node;
6589 else
6590 expr = c_parser_postfix_expression_after_paren_type (parser,
6591 type_name,
6592 loc);
6594 else
6596 /* A parenthesized expression. */
6597 c_parser_consume_token (parser);
6598 expr = c_parser_expression (parser);
6599 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6600 TREE_NO_WARNING (expr.value) = 1;
6601 if (expr.original_code != C_MAYBE_CONST_EXPR)
6602 expr.original_code = ERROR_MARK;
6603 /* Don't change EXPR.ORIGINAL_TYPE. */
6604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6605 "expected %<)%>");
6607 break;
6608 case CPP_KEYWORD:
6609 switch (c_parser_peek_token (parser)->keyword)
6611 case RID_FUNCTION_NAME:
6612 case RID_PRETTY_FUNCTION_NAME:
6613 case RID_C99_FUNCTION_NAME:
6614 expr.value = fname_decl (loc,
6615 c_parser_peek_token (parser)->keyword,
6616 c_parser_peek_token (parser)->value);
6617 c_parser_consume_token (parser);
6618 break;
6619 case RID_VA_ARG:
6620 c_parser_consume_token (parser);
6621 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6623 expr.value = error_mark_node;
6624 break;
6626 e1 = c_parser_expr_no_commas (parser, NULL);
6627 mark_exp_read (e1.value);
6628 e1.value = c_fully_fold (e1.value, false, NULL);
6629 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6632 expr.value = error_mark_node;
6633 break;
6635 loc = c_parser_peek_token (parser)->location;
6636 t1 = c_parser_type_name (parser);
6637 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6638 "expected %<)%>");
6639 if (t1 == NULL)
6641 expr.value = error_mark_node;
6643 else
6645 tree type_expr = NULL_TREE;
6646 expr.value = c_build_va_arg (loc, e1.value,
6647 groktypename (t1, &type_expr, NULL));
6648 if (type_expr)
6650 expr.value = build2 (C_MAYBE_CONST_EXPR,
6651 TREE_TYPE (expr.value), type_expr,
6652 expr.value);
6653 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6656 break;
6657 case RID_OFFSETOF:
6658 c_parser_consume_token (parser);
6659 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6661 expr.value = error_mark_node;
6662 break;
6664 t1 = c_parser_type_name (parser);
6665 if (t1 == NULL)
6666 parser->error = true;
6667 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6668 gcc_assert (parser->error);
6669 if (parser->error)
6671 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6672 expr.value = error_mark_node;
6673 break;
6677 tree type = groktypename (t1, NULL, NULL);
6678 tree offsetof_ref;
6679 if (type == error_mark_node)
6680 offsetof_ref = error_mark_node;
6681 else
6683 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6684 SET_EXPR_LOCATION (offsetof_ref, loc);
6686 /* Parse the second argument to __builtin_offsetof. We
6687 must have one identifier, and beyond that we want to
6688 accept sub structure and sub array references. */
6689 if (c_parser_next_token_is (parser, CPP_NAME))
6691 offsetof_ref = build_component_ref
6692 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6693 c_parser_consume_token (parser);
6694 while (c_parser_next_token_is (parser, CPP_DOT)
6695 || c_parser_next_token_is (parser,
6696 CPP_OPEN_SQUARE)
6697 || c_parser_next_token_is (parser,
6698 CPP_DEREF))
6700 if (c_parser_next_token_is (parser, CPP_DEREF))
6702 loc = c_parser_peek_token (parser)->location;
6703 offsetof_ref = build_array_ref (loc,
6704 offsetof_ref,
6705 integer_zero_node);
6706 goto do_dot;
6708 else if (c_parser_next_token_is (parser, CPP_DOT))
6710 do_dot:
6711 c_parser_consume_token (parser);
6712 if (c_parser_next_token_is_not (parser,
6713 CPP_NAME))
6715 c_parser_error (parser, "expected identifier");
6716 break;
6718 offsetof_ref = build_component_ref
6719 (loc, offsetof_ref,
6720 c_parser_peek_token (parser)->value);
6721 c_parser_consume_token (parser);
6723 else
6725 tree idx;
6726 loc = c_parser_peek_token (parser)->location;
6727 c_parser_consume_token (parser);
6728 idx = c_parser_expression (parser).value;
6729 idx = c_fully_fold (idx, false, NULL);
6730 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6731 "expected %<]%>");
6732 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6736 else
6737 c_parser_error (parser, "expected identifier");
6738 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6739 "expected %<)%>");
6740 expr.value = fold_offsetof (offsetof_ref);
6742 break;
6743 case RID_CHOOSE_EXPR:
6745 vec<c_expr_t, va_gc> *cexpr_list;
6746 c_expr_t *e1_p, *e2_p, *e3_p;
6747 tree c;
6749 c_parser_consume_token (parser);
6750 if (!c_parser_get_builtin_args (parser,
6751 "__builtin_choose_expr",
6752 &cexpr_list))
6754 expr.value = error_mark_node;
6755 break;
6758 if (vec_safe_length (cexpr_list) != 3)
6760 error_at (loc, "wrong number of arguments to "
6761 "%<__builtin_choose_expr%>");
6762 expr.value = error_mark_node;
6763 break;
6766 e1_p = &(*cexpr_list)[0];
6767 e2_p = &(*cexpr_list)[1];
6768 e3_p = &(*cexpr_list)[2];
6770 c = e1_p->value;
6771 mark_exp_read (e2_p->value);
6772 mark_exp_read (e3_p->value);
6773 if (TREE_CODE (c) != INTEGER_CST
6774 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6775 error_at (loc,
6776 "first argument to %<__builtin_choose_expr%> not"
6777 " a constant");
6778 constant_expression_warning (c);
6779 expr = integer_zerop (c) ? *e3_p : *e2_p;
6780 break;
6782 case RID_TYPES_COMPATIBLE_P:
6783 c_parser_consume_token (parser);
6784 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6786 expr.value = error_mark_node;
6787 break;
6789 t1 = c_parser_type_name (parser);
6790 if (t1 == NULL)
6792 expr.value = error_mark_node;
6793 break;
6795 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6797 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6798 expr.value = error_mark_node;
6799 break;
6801 t2 = c_parser_type_name (parser);
6802 if (t2 == NULL)
6804 expr.value = error_mark_node;
6805 break;
6807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6808 "expected %<)%>");
6810 tree e1, e2;
6811 e1 = groktypename (t1, NULL, NULL);
6812 e2 = groktypename (t2, NULL, NULL);
6813 if (e1 == error_mark_node || e2 == error_mark_node)
6815 expr.value = error_mark_node;
6816 break;
6819 e1 = TYPE_MAIN_VARIANT (e1);
6820 e2 = TYPE_MAIN_VARIANT (e2);
6822 expr.value
6823 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6825 break;
6826 case RID_BUILTIN_COMPLEX:
6828 vec<c_expr_t, va_gc> *cexpr_list;
6829 c_expr_t *e1_p, *e2_p;
6831 c_parser_consume_token (parser);
6832 if (!c_parser_get_builtin_args (parser,
6833 "__builtin_complex",
6834 &cexpr_list))
6836 expr.value = error_mark_node;
6837 break;
6840 if (vec_safe_length (cexpr_list) != 2)
6842 error_at (loc, "wrong number of arguments to "
6843 "%<__builtin_complex%>");
6844 expr.value = error_mark_node;
6845 break;
6848 e1_p = &(*cexpr_list)[0];
6849 e2_p = &(*cexpr_list)[1];
6851 mark_exp_read (e1_p->value);
6852 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6853 e1_p->value = convert (TREE_TYPE (e1_p->value),
6854 TREE_OPERAND (e1_p->value, 0));
6855 mark_exp_read (e2_p->value);
6856 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6857 e2_p->value = convert (TREE_TYPE (e2_p->value),
6858 TREE_OPERAND (e2_p->value, 0));
6859 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6860 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6861 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6862 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6864 error_at (loc, "%<__builtin_complex%> operand "
6865 "not of real binary floating-point type");
6866 expr.value = error_mark_node;
6867 break;
6869 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6870 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6872 error_at (loc,
6873 "%<__builtin_complex%> operands of different types");
6874 expr.value = error_mark_node;
6875 break;
6877 if (!flag_isoc99)
6878 pedwarn (loc, OPT_Wpedantic,
6879 "ISO C90 does not support complex types");
6880 expr.value = build2 (COMPLEX_EXPR,
6881 build_complex_type
6882 (TYPE_MAIN_VARIANT
6883 (TREE_TYPE (e1_p->value))),
6884 e1_p->value, e2_p->value);
6885 break;
6887 case RID_BUILTIN_SHUFFLE:
6889 vec<c_expr_t, va_gc> *cexpr_list;
6890 unsigned int i;
6891 c_expr_t *p;
6893 c_parser_consume_token (parser);
6894 if (!c_parser_get_builtin_args (parser,
6895 "__builtin_shuffle",
6896 &cexpr_list))
6898 expr.value = error_mark_node;
6899 break;
6902 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
6903 mark_exp_read (p->value);
6905 if (vec_safe_length (cexpr_list) == 2)
6906 expr.value =
6907 c_build_vec_perm_expr
6908 (loc, (*cexpr_list)[0].value,
6909 NULL_TREE, (*cexpr_list)[1].value);
6911 else if (vec_safe_length (cexpr_list) == 3)
6912 expr.value =
6913 c_build_vec_perm_expr
6914 (loc, (*cexpr_list)[0].value,
6915 (*cexpr_list)[1].value,
6916 (*cexpr_list)[2].value);
6917 else
6919 error_at (loc, "wrong number of arguments to "
6920 "%<__builtin_shuffle%>");
6921 expr.value = error_mark_node;
6923 break;
6925 case RID_AT_SELECTOR:
6926 gcc_assert (c_dialect_objc ());
6927 c_parser_consume_token (parser);
6928 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6930 expr.value = error_mark_node;
6931 break;
6934 tree sel = c_parser_objc_selector_arg (parser);
6935 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6936 "expected %<)%>");
6937 expr.value = objc_build_selector_expr (loc, sel);
6939 break;
6940 case RID_AT_PROTOCOL:
6941 gcc_assert (c_dialect_objc ());
6942 c_parser_consume_token (parser);
6943 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6945 expr.value = error_mark_node;
6946 break;
6948 if (c_parser_next_token_is_not (parser, CPP_NAME))
6950 c_parser_error (parser, "expected identifier");
6951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6952 expr.value = error_mark_node;
6953 break;
6956 tree id = c_parser_peek_token (parser)->value;
6957 c_parser_consume_token (parser);
6958 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6959 "expected %<)%>");
6960 expr.value = objc_build_protocol_expr (id);
6962 break;
6963 case RID_AT_ENCODE:
6964 /* Extension to support C-structures in the archiver. */
6965 gcc_assert (c_dialect_objc ());
6966 c_parser_consume_token (parser);
6967 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6969 expr.value = error_mark_node;
6970 break;
6972 t1 = c_parser_type_name (parser);
6973 if (t1 == NULL)
6975 expr.value = error_mark_node;
6976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6977 break;
6979 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6980 "expected %<)%>");
6982 tree type = groktypename (t1, NULL, NULL);
6983 expr.value = objc_build_encode_expr (type);
6985 break;
6986 default:
6987 c_parser_error (parser, "expected expression");
6988 expr.value = error_mark_node;
6989 break;
6991 break;
6992 case CPP_OPEN_SQUARE:
6993 if (c_dialect_objc ())
6995 tree receiver, args;
6996 c_parser_consume_token (parser);
6997 receiver = c_parser_objc_receiver (parser);
6998 args = c_parser_objc_message_args (parser);
6999 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7000 "expected %<]%>");
7001 expr.value = objc_build_message_expr (receiver, args);
7002 break;
7004 /* Else fall through to report error. */
7005 default:
7006 c_parser_error (parser, "expected expression");
7007 expr.value = error_mark_node;
7008 break;
7010 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7013 /* Parse a postfix expression after a parenthesized type name: the
7014 brace-enclosed initializer of a compound literal, possibly followed
7015 by some postfix operators. This is separate because it is not
7016 possible to tell until after the type name whether a cast
7017 expression has a cast or a compound literal, or whether the operand
7018 of sizeof is a parenthesized type name or starts with a compound
7019 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7020 location of the first token after the parentheses around the type
7021 name. */
7023 static struct c_expr
7024 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7025 struct c_type_name *type_name,
7026 location_t type_loc)
7028 tree type;
7029 struct c_expr init;
7030 bool non_const;
7031 struct c_expr expr;
7032 location_t start_loc;
7033 tree type_expr = NULL_TREE;
7034 bool type_expr_const = true;
7035 check_compound_literal_type (type_loc, type_name);
7036 start_init (NULL_TREE, NULL, 0);
7037 type = groktypename (type_name, &type_expr, &type_expr_const);
7038 start_loc = c_parser_peek_token (parser)->location;
7039 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7041 error_at (type_loc, "compound literal has variable size");
7042 type = error_mark_node;
7044 init = c_parser_braced_init (parser, type, false);
7045 finish_init ();
7046 maybe_warn_string_init (type, init);
7048 if (type != error_mark_node
7049 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7050 && current_function_decl)
7052 error ("compound literal qualified by address-space qualifier");
7053 type = error_mark_node;
7056 if (!flag_isoc99)
7057 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7058 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7059 ? CONSTRUCTOR_NON_CONST (init.value)
7060 : init.original_code == C_MAYBE_CONST_EXPR);
7061 non_const |= !type_expr_const;
7062 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7063 expr.original_code = ERROR_MARK;
7064 expr.original_type = NULL;
7065 if (type_expr)
7067 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7069 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7070 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7072 else
7074 gcc_assert (!non_const);
7075 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7076 type_expr, expr.value);
7079 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7082 /* Callback function for sizeof_pointer_memaccess_warning to compare
7083 types. */
7085 static bool
7086 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7088 return comptypes (type1, type2) == 1;
7091 /* Parse a postfix expression after the initial primary or compound
7092 literal; that is, parse a series of postfix operators.
7094 EXPR_LOC is the location of the primary expression. */
7096 static struct c_expr
7097 c_parser_postfix_expression_after_primary (c_parser *parser,
7098 location_t expr_loc,
7099 struct c_expr expr)
7101 struct c_expr orig_expr;
7102 tree ident, idx;
7103 location_t sizeof_arg_loc[3];
7104 tree sizeof_arg[3];
7105 unsigned int i;
7106 vec<tree, va_gc> *exprlist;
7107 vec<tree, va_gc> *origtypes;
7108 while (true)
7110 location_t op_loc = c_parser_peek_token (parser)->location;
7111 switch (c_parser_peek_token (parser)->type)
7113 case CPP_OPEN_SQUARE:
7114 /* Array reference. */
7115 c_parser_consume_token (parser);
7116 idx = c_parser_expression (parser).value;
7117 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7118 "expected %<]%>");
7119 expr.value = build_array_ref (op_loc, expr.value, idx);
7120 expr.original_code = ERROR_MARK;
7121 expr.original_type = NULL;
7122 break;
7123 case CPP_OPEN_PAREN:
7124 /* Function call. */
7125 c_parser_consume_token (parser);
7126 for (i = 0; i < 3; i++)
7128 sizeof_arg[i] = NULL_TREE;
7129 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7131 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7132 exprlist = NULL;
7133 else
7134 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7135 sizeof_arg_loc, sizeof_arg);
7136 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7137 "expected %<)%>");
7138 orig_expr = expr;
7139 mark_exp_read (expr.value);
7140 if (warn_sizeof_pointer_memaccess)
7141 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7142 expr.value, exprlist,
7143 sizeof_arg,
7144 sizeof_ptr_memacc_comptypes);
7145 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
7146 "(" after the FUNCNAME, which is what we have now. */
7147 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
7148 origtypes);
7149 expr.original_code = ERROR_MARK;
7150 if (TREE_CODE (expr.value) == INTEGER_CST
7151 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7152 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7153 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7154 expr.original_code = C_MAYBE_CONST_EXPR;
7155 expr.original_type = NULL;
7156 if (exprlist)
7158 release_tree_vector (exprlist);
7159 release_tree_vector (origtypes);
7161 break;
7162 case CPP_DOT:
7163 /* Structure element reference. */
7164 c_parser_consume_token (parser);
7165 expr = default_function_array_conversion (expr_loc, expr);
7166 if (c_parser_next_token_is (parser, CPP_NAME))
7167 ident = c_parser_peek_token (parser)->value;
7168 else
7170 c_parser_error (parser, "expected identifier");
7171 expr.value = error_mark_node;
7172 expr.original_code = ERROR_MARK;
7173 expr.original_type = NULL;
7174 return expr;
7176 c_parser_consume_token (parser);
7177 expr.value = build_component_ref (op_loc, expr.value, ident);
7178 expr.original_code = ERROR_MARK;
7179 if (TREE_CODE (expr.value) != COMPONENT_REF)
7180 expr.original_type = NULL;
7181 else
7183 /* Remember the original type of a bitfield. */
7184 tree field = TREE_OPERAND (expr.value, 1);
7185 if (TREE_CODE (field) != FIELD_DECL)
7186 expr.original_type = NULL;
7187 else
7188 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7190 break;
7191 case CPP_DEREF:
7192 /* Structure element reference. */
7193 c_parser_consume_token (parser);
7194 expr = default_function_array_conversion (expr_loc, expr);
7195 if (c_parser_next_token_is (parser, CPP_NAME))
7196 ident = c_parser_peek_token (parser)->value;
7197 else
7199 c_parser_error (parser, "expected identifier");
7200 expr.value = error_mark_node;
7201 expr.original_code = ERROR_MARK;
7202 expr.original_type = NULL;
7203 return expr;
7205 c_parser_consume_token (parser);
7206 expr.value = build_component_ref (op_loc,
7207 build_indirect_ref (op_loc,
7208 expr.value,
7209 RO_ARROW),
7210 ident);
7211 expr.original_code = ERROR_MARK;
7212 if (TREE_CODE (expr.value) != COMPONENT_REF)
7213 expr.original_type = NULL;
7214 else
7216 /* Remember the original type of a bitfield. */
7217 tree field = TREE_OPERAND (expr.value, 1);
7218 if (TREE_CODE (field) != FIELD_DECL)
7219 expr.original_type = NULL;
7220 else
7221 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7223 break;
7224 case CPP_PLUS_PLUS:
7225 /* Postincrement. */
7226 c_parser_consume_token (parser);
7227 expr = default_function_array_read_conversion (expr_loc, expr);
7228 expr.value = build_unary_op (op_loc,
7229 POSTINCREMENT_EXPR, expr.value, 0);
7230 expr.original_code = ERROR_MARK;
7231 expr.original_type = NULL;
7232 break;
7233 case CPP_MINUS_MINUS:
7234 /* Postdecrement. */
7235 c_parser_consume_token (parser);
7236 expr = default_function_array_read_conversion (expr_loc, expr);
7237 expr.value = build_unary_op (op_loc,
7238 POSTDECREMENT_EXPR, expr.value, 0);
7239 expr.original_code = ERROR_MARK;
7240 expr.original_type = NULL;
7241 break;
7242 default:
7243 return expr;
7248 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7250 expression:
7251 assignment-expression
7252 expression , assignment-expression
7255 static struct c_expr
7256 c_parser_expression (c_parser *parser)
7258 struct c_expr expr;
7259 expr = c_parser_expr_no_commas (parser, NULL);
7260 while (c_parser_next_token_is (parser, CPP_COMMA))
7262 struct c_expr next;
7263 tree lhsval;
7264 location_t loc = c_parser_peek_token (parser)->location;
7265 location_t expr_loc;
7266 c_parser_consume_token (parser);
7267 expr_loc = c_parser_peek_token (parser)->location;
7268 lhsval = expr.value;
7269 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7270 lhsval = TREE_OPERAND (lhsval, 1);
7271 if (DECL_P (lhsval) || handled_component_p (lhsval))
7272 mark_exp_read (lhsval);
7273 next = c_parser_expr_no_commas (parser, NULL);
7274 next = default_function_array_conversion (expr_loc, next);
7275 expr.value = build_compound_expr (loc, expr.value, next.value);
7276 expr.original_code = COMPOUND_EXPR;
7277 expr.original_type = next.original_type;
7279 return expr;
7282 /* Parse an expression and convert functions or arrays to
7283 pointers. */
7285 static struct c_expr
7286 c_parser_expression_conv (c_parser *parser)
7288 struct c_expr expr;
7289 location_t loc = c_parser_peek_token (parser)->location;
7290 expr = c_parser_expression (parser);
7291 expr = default_function_array_conversion (loc, expr);
7292 return expr;
7295 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7296 functions and arrays to pointers. If FOLD_P, fold the expressions.
7298 nonempty-expr-list:
7299 assignment-expression
7300 nonempty-expr-list , assignment-expression
7303 static vec<tree, va_gc> *
7304 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7305 vec<tree, va_gc> **p_orig_types,
7306 location_t *sizeof_arg_loc, tree *sizeof_arg)
7308 vec<tree, va_gc> *ret;
7309 vec<tree, va_gc> *orig_types;
7310 struct c_expr expr;
7311 location_t loc = c_parser_peek_token (parser)->location;
7312 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7313 unsigned int idx = 0;
7315 ret = make_tree_vector ();
7316 if (p_orig_types == NULL)
7317 orig_types = NULL;
7318 else
7319 orig_types = make_tree_vector ();
7321 if (sizeof_arg != NULL
7322 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7323 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7324 expr = c_parser_expr_no_commas (parser, NULL);
7325 if (convert_p)
7326 expr = default_function_array_read_conversion (loc, expr);
7327 if (fold_p)
7328 expr.value = c_fully_fold (expr.value, false, NULL);
7329 ret->quick_push (expr.value);
7330 if (orig_types)
7331 orig_types->quick_push (expr.original_type);
7332 if (sizeof_arg != NULL
7333 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7334 && expr.original_code == SIZEOF_EXPR)
7336 sizeof_arg[0] = c_last_sizeof_arg;
7337 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7339 while (c_parser_next_token_is (parser, CPP_COMMA))
7341 c_parser_consume_token (parser);
7342 loc = c_parser_peek_token (parser)->location;
7343 if (sizeof_arg != NULL
7344 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7345 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7346 else
7347 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7348 expr = c_parser_expr_no_commas (parser, NULL);
7349 if (convert_p)
7350 expr = default_function_array_read_conversion (loc, expr);
7351 if (fold_p)
7352 expr.value = c_fully_fold (expr.value, false, NULL);
7353 vec_safe_push (ret, expr.value);
7354 if (orig_types)
7355 vec_safe_push (orig_types, expr.original_type);
7356 if (++idx < 3
7357 && sizeof_arg != NULL
7358 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7359 && expr.original_code == SIZEOF_EXPR)
7361 sizeof_arg[idx] = c_last_sizeof_arg;
7362 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7365 if (orig_types)
7366 *p_orig_types = orig_types;
7367 return ret;
7370 /* Parse Objective-C-specific constructs. */
7372 /* Parse an objc-class-definition.
7374 objc-class-definition:
7375 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7376 objc-class-instance-variables[opt] objc-methodprotolist @end
7377 @implementation identifier objc-superclass[opt]
7378 objc-class-instance-variables[opt]
7379 @interface identifier ( identifier ) objc-protocol-refs[opt]
7380 objc-methodprotolist @end
7381 @interface identifier ( ) objc-protocol-refs[opt]
7382 objc-methodprotolist @end
7383 @implementation identifier ( identifier )
7385 objc-superclass:
7386 : identifier
7388 "@interface identifier (" must start "@interface identifier (
7389 identifier ) ...": objc-methodprotolist in the first production may
7390 not start with a parenthesized identifier as a declarator of a data
7391 definition with no declaration specifiers if the objc-superclass,
7392 objc-protocol-refs and objc-class-instance-variables are omitted. */
7394 static void
7395 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7397 bool iface_p;
7398 tree id1;
7399 tree superclass;
7400 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7401 iface_p = true;
7402 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7403 iface_p = false;
7404 else
7405 gcc_unreachable ();
7407 c_parser_consume_token (parser);
7408 if (c_parser_next_token_is_not (parser, CPP_NAME))
7410 c_parser_error (parser, "expected identifier");
7411 return;
7413 id1 = c_parser_peek_token (parser)->value;
7414 c_parser_consume_token (parser);
7415 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7417 /* We have a category or class extension. */
7418 tree id2;
7419 tree proto = NULL_TREE;
7420 c_parser_consume_token (parser);
7421 if (c_parser_next_token_is_not (parser, CPP_NAME))
7423 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7425 /* We have a class extension. */
7426 id2 = NULL_TREE;
7428 else
7430 c_parser_error (parser, "expected identifier or %<)%>");
7431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7432 return;
7435 else
7437 id2 = c_parser_peek_token (parser)->value;
7438 c_parser_consume_token (parser);
7440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7441 if (!iface_p)
7443 objc_start_category_implementation (id1, id2);
7444 return;
7446 if (c_parser_next_token_is (parser, CPP_LESS))
7447 proto = c_parser_objc_protocol_refs (parser);
7448 objc_start_category_interface (id1, id2, proto, attributes);
7449 c_parser_objc_methodprotolist (parser);
7450 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7451 objc_finish_interface ();
7452 return;
7454 if (c_parser_next_token_is (parser, CPP_COLON))
7456 c_parser_consume_token (parser);
7457 if (c_parser_next_token_is_not (parser, CPP_NAME))
7459 c_parser_error (parser, "expected identifier");
7460 return;
7462 superclass = c_parser_peek_token (parser)->value;
7463 c_parser_consume_token (parser);
7465 else
7466 superclass = NULL_TREE;
7467 if (iface_p)
7469 tree proto = NULL_TREE;
7470 if (c_parser_next_token_is (parser, CPP_LESS))
7471 proto = c_parser_objc_protocol_refs (parser);
7472 objc_start_class_interface (id1, superclass, proto, attributes);
7474 else
7475 objc_start_class_implementation (id1, superclass);
7476 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7477 c_parser_objc_class_instance_variables (parser);
7478 if (iface_p)
7480 objc_continue_interface ();
7481 c_parser_objc_methodprotolist (parser);
7482 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7483 objc_finish_interface ();
7485 else
7487 objc_continue_implementation ();
7488 return;
7492 /* Parse objc-class-instance-variables.
7494 objc-class-instance-variables:
7495 { objc-instance-variable-decl-list[opt] }
7497 objc-instance-variable-decl-list:
7498 objc-visibility-spec
7499 objc-instance-variable-decl ;
7501 objc-instance-variable-decl-list objc-visibility-spec
7502 objc-instance-variable-decl-list objc-instance-variable-decl ;
7503 objc-instance-variable-decl-list ;
7505 objc-visibility-spec:
7506 @private
7507 @protected
7508 @public
7510 objc-instance-variable-decl:
7511 struct-declaration
7514 static void
7515 c_parser_objc_class_instance_variables (c_parser *parser)
7517 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7518 c_parser_consume_token (parser);
7519 while (c_parser_next_token_is_not (parser, CPP_EOF))
7521 tree decls;
7522 /* Parse any stray semicolon. */
7523 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7525 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7526 "extra semicolon");
7527 c_parser_consume_token (parser);
7528 continue;
7530 /* Stop if at the end of the instance variables. */
7531 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7533 c_parser_consume_token (parser);
7534 break;
7536 /* Parse any objc-visibility-spec. */
7537 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7539 c_parser_consume_token (parser);
7540 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7541 continue;
7543 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7545 c_parser_consume_token (parser);
7546 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7547 continue;
7549 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7551 c_parser_consume_token (parser);
7552 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7553 continue;
7555 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7557 c_parser_consume_token (parser);
7558 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7559 continue;
7561 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7563 c_parser_pragma (parser, pragma_external);
7564 continue;
7567 /* Parse some comma-separated declarations. */
7568 decls = c_parser_struct_declaration (parser);
7569 if (decls == NULL)
7571 /* There is a syntax error. We want to skip the offending
7572 tokens up to the next ';' (included) or '}'
7573 (excluded). */
7575 /* First, skip manually a ')' or ']'. This is because they
7576 reduce the nesting level, so c_parser_skip_until_found()
7577 wouldn't be able to skip past them. */
7578 c_token *token = c_parser_peek_token (parser);
7579 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7580 c_parser_consume_token (parser);
7582 /* Then, do the standard skipping. */
7583 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7585 /* We hopefully recovered. Start normal parsing again. */
7586 parser->error = false;
7587 continue;
7589 else
7591 /* Comma-separated instance variables are chained together
7592 in reverse order; add them one by one. */
7593 tree ivar = nreverse (decls);
7594 for (; ivar; ivar = DECL_CHAIN (ivar))
7595 objc_add_instance_variable (copy_node (ivar));
7597 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7601 /* Parse an objc-class-declaration.
7603 objc-class-declaration:
7604 @class identifier-list ;
7607 static void
7608 c_parser_objc_class_declaration (c_parser *parser)
7610 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7611 c_parser_consume_token (parser);
7612 /* Any identifiers, including those declared as type names, are OK
7613 here. */
7614 while (true)
7616 tree id;
7617 if (c_parser_next_token_is_not (parser, CPP_NAME))
7619 c_parser_error (parser, "expected identifier");
7620 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7621 parser->error = false;
7622 return;
7624 id = c_parser_peek_token (parser)->value;
7625 objc_declare_class (id);
7626 c_parser_consume_token (parser);
7627 if (c_parser_next_token_is (parser, CPP_COMMA))
7628 c_parser_consume_token (parser);
7629 else
7630 break;
7632 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7635 /* Parse an objc-alias-declaration.
7637 objc-alias-declaration:
7638 @compatibility_alias identifier identifier ;
7641 static void
7642 c_parser_objc_alias_declaration (c_parser *parser)
7644 tree id1, id2;
7645 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7646 c_parser_consume_token (parser);
7647 if (c_parser_next_token_is_not (parser, CPP_NAME))
7649 c_parser_error (parser, "expected identifier");
7650 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7651 return;
7653 id1 = c_parser_peek_token (parser)->value;
7654 c_parser_consume_token (parser);
7655 if (c_parser_next_token_is_not (parser, CPP_NAME))
7657 c_parser_error (parser, "expected identifier");
7658 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7659 return;
7661 id2 = c_parser_peek_token (parser)->value;
7662 c_parser_consume_token (parser);
7663 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7664 objc_declare_alias (id1, id2);
7667 /* Parse an objc-protocol-definition.
7669 objc-protocol-definition:
7670 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7671 @protocol identifier-list ;
7673 "@protocol identifier ;" should be resolved as "@protocol
7674 identifier-list ;": objc-methodprotolist may not start with a
7675 semicolon in the first alternative if objc-protocol-refs are
7676 omitted. */
7678 static void
7679 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7681 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7683 c_parser_consume_token (parser);
7684 if (c_parser_next_token_is_not (parser, CPP_NAME))
7686 c_parser_error (parser, "expected identifier");
7687 return;
7689 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7690 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7692 /* Any identifiers, including those declared as type names, are
7693 OK here. */
7694 while (true)
7696 tree id;
7697 if (c_parser_next_token_is_not (parser, CPP_NAME))
7699 c_parser_error (parser, "expected identifier");
7700 break;
7702 id = c_parser_peek_token (parser)->value;
7703 objc_declare_protocol (id, attributes);
7704 c_parser_consume_token (parser);
7705 if (c_parser_next_token_is (parser, CPP_COMMA))
7706 c_parser_consume_token (parser);
7707 else
7708 break;
7710 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7712 else
7714 tree id = c_parser_peek_token (parser)->value;
7715 tree proto = NULL_TREE;
7716 c_parser_consume_token (parser);
7717 if (c_parser_next_token_is (parser, CPP_LESS))
7718 proto = c_parser_objc_protocol_refs (parser);
7719 parser->objc_pq_context = true;
7720 objc_start_protocol (id, proto, attributes);
7721 c_parser_objc_methodprotolist (parser);
7722 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7723 parser->objc_pq_context = false;
7724 objc_finish_interface ();
7728 /* Parse an objc-method-type.
7730 objc-method-type:
7734 Return true if it is a class method (+) and false if it is
7735 an instance method (-).
7737 static inline bool
7738 c_parser_objc_method_type (c_parser *parser)
7740 switch (c_parser_peek_token (parser)->type)
7742 case CPP_PLUS:
7743 c_parser_consume_token (parser);
7744 return true;
7745 case CPP_MINUS:
7746 c_parser_consume_token (parser);
7747 return false;
7748 default:
7749 gcc_unreachable ();
7753 /* Parse an objc-method-definition.
7755 objc-method-definition:
7756 objc-method-type objc-method-decl ;[opt] compound-statement
7759 static void
7760 c_parser_objc_method_definition (c_parser *parser)
7762 bool is_class_method = c_parser_objc_method_type (parser);
7763 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7764 parser->objc_pq_context = true;
7765 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7766 &expr);
7767 if (decl == error_mark_node)
7768 return; /* Bail here. */
7770 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7772 c_parser_consume_token (parser);
7773 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7774 "extra semicolon in method definition specified");
7777 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7779 c_parser_error (parser, "expected %<{%>");
7780 return;
7783 parser->objc_pq_context = false;
7784 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7786 add_stmt (c_parser_compound_statement (parser));
7787 objc_finish_method_definition (current_function_decl);
7789 else
7791 /* This code is executed when we find a method definition
7792 outside of an @implementation context (or invalid for other
7793 reasons). Parse the method (to keep going) but do not emit
7794 any code.
7796 c_parser_compound_statement (parser);
7800 /* Parse an objc-methodprotolist.
7802 objc-methodprotolist:
7803 empty
7804 objc-methodprotolist objc-methodproto
7805 objc-methodprotolist declaration
7806 objc-methodprotolist ;
7807 @optional
7808 @required
7810 The declaration is a data definition, which may be missing
7811 declaration specifiers under the same rules and diagnostics as
7812 other data definitions outside functions, and the stray semicolon
7813 is diagnosed the same way as a stray semicolon outside a
7814 function. */
7816 static void
7817 c_parser_objc_methodprotolist (c_parser *parser)
7819 while (true)
7821 /* The list is terminated by @end. */
7822 switch (c_parser_peek_token (parser)->type)
7824 case CPP_SEMICOLON:
7825 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7826 "ISO C does not allow extra %<;%> outside of a function");
7827 c_parser_consume_token (parser);
7828 break;
7829 case CPP_PLUS:
7830 case CPP_MINUS:
7831 c_parser_objc_methodproto (parser);
7832 break;
7833 case CPP_PRAGMA:
7834 c_parser_pragma (parser, pragma_external);
7835 break;
7836 case CPP_EOF:
7837 return;
7838 default:
7839 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7840 return;
7841 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7842 c_parser_objc_at_property_declaration (parser);
7843 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7845 objc_set_method_opt (true);
7846 c_parser_consume_token (parser);
7848 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7850 objc_set_method_opt (false);
7851 c_parser_consume_token (parser);
7853 else
7854 c_parser_declaration_or_fndef (parser, false, false, true,
7855 false, true, NULL);
7856 break;
7861 /* Parse an objc-methodproto.
7863 objc-methodproto:
7864 objc-method-type objc-method-decl ;
7867 static void
7868 c_parser_objc_methodproto (c_parser *parser)
7870 bool is_class_method = c_parser_objc_method_type (parser);
7871 tree decl, attributes = NULL_TREE;
7873 /* Remember protocol qualifiers in prototypes. */
7874 parser->objc_pq_context = true;
7875 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7876 NULL);
7877 /* Forget protocol qualifiers now. */
7878 parser->objc_pq_context = false;
7880 /* Do not allow the presence of attributes to hide an erroneous
7881 method implementation in the interface section. */
7882 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7884 c_parser_error (parser, "expected %<;%>");
7885 return;
7888 if (decl != error_mark_node)
7889 objc_add_method_declaration (is_class_method, decl, attributes);
7891 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7894 /* If we are at a position that method attributes may be present, check that
7895 there are not any parsed already (a syntax error) and then collect any
7896 specified at the current location. Finally, if new attributes were present,
7897 check that the next token is legal ( ';' for decls and '{' for defs). */
7899 static bool
7900 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7902 bool bad = false;
7903 if (*attributes)
7905 c_parser_error (parser,
7906 "method attributes must be specified at the end only");
7907 *attributes = NULL_TREE;
7908 bad = true;
7911 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7912 *attributes = c_parser_attributes (parser);
7914 /* If there were no attributes here, just report any earlier error. */
7915 if (*attributes == NULL_TREE || bad)
7916 return bad;
7918 /* If the attributes are followed by a ; or {, then just report any earlier
7919 error. */
7920 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7921 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7922 return bad;
7924 /* We've got attributes, but not at the end. */
7925 c_parser_error (parser,
7926 "expected %<;%> or %<{%> after method attribute definition");
7927 return true;
7930 /* Parse an objc-method-decl.
7932 objc-method-decl:
7933 ( objc-type-name ) objc-selector
7934 objc-selector
7935 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7936 objc-keyword-selector objc-optparmlist
7937 attributes
7939 objc-keyword-selector:
7940 objc-keyword-decl
7941 objc-keyword-selector objc-keyword-decl
7943 objc-keyword-decl:
7944 objc-selector : ( objc-type-name ) identifier
7945 objc-selector : identifier
7946 : ( objc-type-name ) identifier
7947 : identifier
7949 objc-optparmlist:
7950 objc-optparms objc-optellipsis
7952 objc-optparms:
7953 empty
7954 objc-opt-parms , parameter-declaration
7956 objc-optellipsis:
7957 empty
7958 , ...
7961 static tree
7962 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7963 tree *attributes, tree *expr)
7965 tree type = NULL_TREE;
7966 tree sel;
7967 tree parms = NULL_TREE;
7968 bool ellipsis = false;
7969 bool attr_err = false;
7971 *attributes = NULL_TREE;
7972 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7974 c_parser_consume_token (parser);
7975 type = c_parser_objc_type_name (parser);
7976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7978 sel = c_parser_objc_selector (parser);
7979 /* If there is no selector, or a colon follows, we have an
7980 objc-keyword-selector. If there is a selector, and a colon does
7981 not follow, that selector ends the objc-method-decl. */
7982 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7984 tree tsel = sel;
7985 tree list = NULL_TREE;
7986 while (true)
7988 tree atype = NULL_TREE, id, keyworddecl;
7989 tree param_attr = NULL_TREE;
7990 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7991 break;
7992 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7994 c_parser_consume_token (parser);
7995 atype = c_parser_objc_type_name (parser);
7996 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7997 "expected %<)%>");
7999 /* New ObjC allows attributes on method parameters. */
8000 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8001 param_attr = c_parser_attributes (parser);
8002 if (c_parser_next_token_is_not (parser, CPP_NAME))
8004 c_parser_error (parser, "expected identifier");
8005 return error_mark_node;
8007 id = c_parser_peek_token (parser)->value;
8008 c_parser_consume_token (parser);
8009 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8010 list = chainon (list, keyworddecl);
8011 tsel = c_parser_objc_selector (parser);
8012 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8013 break;
8016 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8018 /* Parse the optional parameter list. Optional Objective-C
8019 method parameters follow the C syntax, and may include '...'
8020 to denote a variable number of arguments. */
8021 parms = make_node (TREE_LIST);
8022 while (c_parser_next_token_is (parser, CPP_COMMA))
8024 struct c_parm *parm;
8025 c_parser_consume_token (parser);
8026 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8028 ellipsis = true;
8029 c_parser_consume_token (parser);
8030 attr_err |= c_parser_objc_maybe_method_attributes
8031 (parser, attributes) ;
8032 break;
8034 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8035 if (parm == NULL)
8036 break;
8037 parms = chainon (parms,
8038 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8040 sel = list;
8042 else
8043 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8045 if (sel == NULL)
8047 c_parser_error (parser, "objective-c method declaration is expected");
8048 return error_mark_node;
8051 if (attr_err)
8052 return error_mark_node;
8054 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8057 /* Parse an objc-type-name.
8059 objc-type-name:
8060 objc-type-qualifiers[opt] type-name
8061 objc-type-qualifiers[opt]
8063 objc-type-qualifiers:
8064 objc-type-qualifier
8065 objc-type-qualifiers objc-type-qualifier
8067 objc-type-qualifier: one of
8068 in out inout bycopy byref oneway
8071 static tree
8072 c_parser_objc_type_name (c_parser *parser)
8074 tree quals = NULL_TREE;
8075 struct c_type_name *type_name = NULL;
8076 tree type = NULL_TREE;
8077 while (true)
8079 c_token *token = c_parser_peek_token (parser);
8080 if (token->type == CPP_KEYWORD
8081 && (token->keyword == RID_IN
8082 || token->keyword == RID_OUT
8083 || token->keyword == RID_INOUT
8084 || token->keyword == RID_BYCOPY
8085 || token->keyword == RID_BYREF
8086 || token->keyword == RID_ONEWAY))
8088 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8089 c_parser_consume_token (parser);
8091 else
8092 break;
8094 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8095 type_name = c_parser_type_name (parser);
8096 if (type_name)
8097 type = groktypename (type_name, NULL, NULL);
8099 /* If the type is unknown, and error has already been produced and
8100 we need to recover from the error. In that case, use NULL_TREE
8101 for the type, as if no type had been specified; this will use the
8102 default type ('id') which is good for error recovery. */
8103 if (type == error_mark_node)
8104 type = NULL_TREE;
8106 return build_tree_list (quals, type);
8109 /* Parse objc-protocol-refs.
8111 objc-protocol-refs:
8112 < identifier-list >
8115 static tree
8116 c_parser_objc_protocol_refs (c_parser *parser)
8118 tree list = NULL_TREE;
8119 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8120 c_parser_consume_token (parser);
8121 /* Any identifiers, including those declared as type names, are OK
8122 here. */
8123 while (true)
8125 tree id;
8126 if (c_parser_next_token_is_not (parser, CPP_NAME))
8128 c_parser_error (parser, "expected identifier");
8129 break;
8131 id = c_parser_peek_token (parser)->value;
8132 list = chainon (list, build_tree_list (NULL_TREE, id));
8133 c_parser_consume_token (parser);
8134 if (c_parser_next_token_is (parser, CPP_COMMA))
8135 c_parser_consume_token (parser);
8136 else
8137 break;
8139 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8140 return list;
8143 /* Parse an objc-try-catch-finally-statement.
8145 objc-try-catch-finally-statement:
8146 @try compound-statement objc-catch-list[opt]
8147 @try compound-statement objc-catch-list[opt] @finally compound-statement
8149 objc-catch-list:
8150 @catch ( objc-catch-parameter-declaration ) compound-statement
8151 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8153 objc-catch-parameter-declaration:
8154 parameter-declaration
8155 '...'
8157 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8159 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8160 for C++. Keep them in sync. */
8162 static void
8163 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8165 location_t location;
8166 tree stmt;
8168 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8169 c_parser_consume_token (parser);
8170 location = c_parser_peek_token (parser)->location;
8171 objc_maybe_warn_exceptions (location);
8172 stmt = c_parser_compound_statement (parser);
8173 objc_begin_try_stmt (location, stmt);
8175 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8177 struct c_parm *parm;
8178 tree parameter_declaration = error_mark_node;
8179 bool seen_open_paren = false;
8181 c_parser_consume_token (parser);
8182 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8183 seen_open_paren = true;
8184 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8186 /* We have "@catch (...)" (where the '...' are literally
8187 what is in the code). Skip the '...'.
8188 parameter_declaration is set to NULL_TREE, and
8189 objc_being_catch_clauses() knows that that means
8190 '...'. */
8191 c_parser_consume_token (parser);
8192 parameter_declaration = NULL_TREE;
8194 else
8196 /* We have "@catch (NSException *exception)" or something
8197 like that. Parse the parameter declaration. */
8198 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8199 if (parm == NULL)
8200 parameter_declaration = error_mark_node;
8201 else
8202 parameter_declaration = grokparm (parm, NULL);
8204 if (seen_open_paren)
8205 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8206 else
8208 /* If there was no open parenthesis, we are recovering from
8209 an error, and we are trying to figure out what mistake
8210 the user has made. */
8212 /* If there is an immediate closing parenthesis, the user
8213 probably forgot the opening one (ie, they typed "@catch
8214 NSException *e)". Parse the closing parenthesis and keep
8215 going. */
8216 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8217 c_parser_consume_token (parser);
8219 /* If these is no immediate closing parenthesis, the user
8220 probably doesn't know that parenthesis are required at
8221 all (ie, they typed "@catch NSException *e"). So, just
8222 forget about the closing parenthesis and keep going. */
8224 objc_begin_catch_clause (parameter_declaration);
8225 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8226 c_parser_compound_statement_nostart (parser);
8227 objc_finish_catch_clause ();
8229 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8231 c_parser_consume_token (parser);
8232 location = c_parser_peek_token (parser)->location;
8233 stmt = c_parser_compound_statement (parser);
8234 objc_build_finally_clause (location, stmt);
8236 objc_finish_try_stmt ();
8239 /* Parse an objc-synchronized-statement.
8241 objc-synchronized-statement:
8242 @synchronized ( expression ) compound-statement
8245 static void
8246 c_parser_objc_synchronized_statement (c_parser *parser)
8248 location_t loc;
8249 tree expr, stmt;
8250 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8251 c_parser_consume_token (parser);
8252 loc = c_parser_peek_token (parser)->location;
8253 objc_maybe_warn_exceptions (loc);
8254 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8256 expr = c_parser_expression (parser).value;
8257 expr = c_fully_fold (expr, false, NULL);
8258 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8260 else
8261 expr = error_mark_node;
8262 stmt = c_parser_compound_statement (parser);
8263 objc_build_synchronized (loc, expr, stmt);
8266 /* Parse an objc-selector; return NULL_TREE without an error if the
8267 next token is not an objc-selector.
8269 objc-selector:
8270 identifier
8271 one of
8272 enum struct union if else while do for switch case default
8273 break continue return goto asm sizeof typeof __alignof
8274 unsigned long const short volatile signed restrict _Complex
8275 in out inout bycopy byref oneway int char float double void _Bool
8277 ??? Why this selection of keywords but not, for example, storage
8278 class specifiers? */
8280 static tree
8281 c_parser_objc_selector (c_parser *parser)
8283 c_token *token = c_parser_peek_token (parser);
8284 tree value = token->value;
8285 if (token->type == CPP_NAME)
8287 c_parser_consume_token (parser);
8288 return value;
8290 if (token->type != CPP_KEYWORD)
8291 return NULL_TREE;
8292 switch (token->keyword)
8294 case RID_ENUM:
8295 case RID_STRUCT:
8296 case RID_UNION:
8297 case RID_IF:
8298 case RID_ELSE:
8299 case RID_WHILE:
8300 case RID_DO:
8301 case RID_FOR:
8302 case RID_SWITCH:
8303 case RID_CASE:
8304 case RID_DEFAULT:
8305 case RID_BREAK:
8306 case RID_CONTINUE:
8307 case RID_RETURN:
8308 case RID_GOTO:
8309 case RID_ASM:
8310 case RID_SIZEOF:
8311 case RID_TYPEOF:
8312 case RID_ALIGNOF:
8313 case RID_UNSIGNED:
8314 case RID_LONG:
8315 case RID_INT128:
8316 case RID_CONST:
8317 case RID_SHORT:
8318 case RID_VOLATILE:
8319 case RID_SIGNED:
8320 case RID_RESTRICT:
8321 case RID_COMPLEX:
8322 case RID_IN:
8323 case RID_OUT:
8324 case RID_INOUT:
8325 case RID_BYCOPY:
8326 case RID_BYREF:
8327 case RID_ONEWAY:
8328 case RID_INT:
8329 case RID_CHAR:
8330 case RID_FLOAT:
8331 case RID_DOUBLE:
8332 case RID_VOID:
8333 case RID_BOOL:
8334 c_parser_consume_token (parser);
8335 return value;
8336 default:
8337 return NULL_TREE;
8341 /* Parse an objc-selector-arg.
8343 objc-selector-arg:
8344 objc-selector
8345 objc-keywordname-list
8347 objc-keywordname-list:
8348 objc-keywordname
8349 objc-keywordname-list objc-keywordname
8351 objc-keywordname:
8352 objc-selector :
8356 static tree
8357 c_parser_objc_selector_arg (c_parser *parser)
8359 tree sel = c_parser_objc_selector (parser);
8360 tree list = NULL_TREE;
8361 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8362 return sel;
8363 while (true)
8365 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8366 return list;
8367 list = chainon (list, build_tree_list (sel, NULL_TREE));
8368 sel = c_parser_objc_selector (parser);
8369 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8370 break;
8372 return list;
8375 /* Parse an objc-receiver.
8377 objc-receiver:
8378 expression
8379 class-name
8380 type-name
8383 static tree
8384 c_parser_objc_receiver (c_parser *parser)
8386 if (c_parser_peek_token (parser)->type == CPP_NAME
8387 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8388 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8390 tree id = c_parser_peek_token (parser)->value;
8391 c_parser_consume_token (parser);
8392 return objc_get_class_reference (id);
8394 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8397 /* Parse objc-message-args.
8399 objc-message-args:
8400 objc-selector
8401 objc-keywordarg-list
8403 objc-keywordarg-list:
8404 objc-keywordarg
8405 objc-keywordarg-list objc-keywordarg
8407 objc-keywordarg:
8408 objc-selector : objc-keywordexpr
8409 : objc-keywordexpr
8412 static tree
8413 c_parser_objc_message_args (c_parser *parser)
8415 tree sel = c_parser_objc_selector (parser);
8416 tree list = NULL_TREE;
8417 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8418 return sel;
8419 while (true)
8421 tree keywordexpr;
8422 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8423 return error_mark_node;
8424 keywordexpr = c_parser_objc_keywordexpr (parser);
8425 list = chainon (list, build_tree_list (sel, keywordexpr));
8426 sel = c_parser_objc_selector (parser);
8427 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8428 break;
8430 return list;
8433 /* Parse an objc-keywordexpr.
8435 objc-keywordexpr:
8436 nonempty-expr-list
8439 static tree
8440 c_parser_objc_keywordexpr (c_parser *parser)
8442 tree ret;
8443 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
8444 NULL, NULL, NULL);
8445 if (vec_safe_length (expr_list) == 1)
8447 /* Just return the expression, remove a level of
8448 indirection. */
8449 ret = (*expr_list)[0];
8451 else
8453 /* We have a comma expression, we will collapse later. */
8454 ret = build_tree_list_vec (expr_list);
8456 release_tree_vector (expr_list);
8457 return ret;
8460 /* A check, needed in several places, that ObjC interface, implementation or
8461 method definitions are not prefixed by incorrect items. */
8462 static bool
8463 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8464 struct c_declspecs *specs)
8466 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8467 || specs->typespec_kind != ctsk_none)
8469 c_parser_error (parser,
8470 "no type or storage class may be specified here,");
8471 c_parser_skip_to_end_of_block_or_statement (parser);
8472 return true;
8474 return false;
8477 /* Parse an Objective-C @property declaration. The syntax is:
8479 objc-property-declaration:
8480 '@property' objc-property-attributes[opt] struct-declaration ;
8482 objc-property-attributes:
8483 '(' objc-property-attribute-list ')'
8485 objc-property-attribute-list:
8486 objc-property-attribute
8487 objc-property-attribute-list, objc-property-attribute
8489 objc-property-attribute
8490 'getter' = identifier
8491 'setter' = identifier
8492 'readonly'
8493 'readwrite'
8494 'assign'
8495 'retain'
8496 'copy'
8497 'nonatomic'
8499 For example:
8500 @property NSString *name;
8501 @property (readonly) id object;
8502 @property (retain, nonatomic, getter=getTheName) id name;
8503 @property int a, b, c;
8505 PS: This function is identical to cp_parser_objc_at_propery_declaration
8506 for C++. Keep them in sync. */
8507 static void
8508 c_parser_objc_at_property_declaration (c_parser *parser)
8510 /* The following variables hold the attributes of the properties as
8511 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8512 seen. When we see an attribute, we set them to 'true' (if they
8513 are boolean properties) or to the identifier (if they have an
8514 argument, ie, for getter and setter). Note that here we only
8515 parse the list of attributes, check the syntax and accumulate the
8516 attributes that we find. objc_add_property_declaration() will
8517 then process the information. */
8518 bool property_assign = false;
8519 bool property_copy = false;
8520 tree property_getter_ident = NULL_TREE;
8521 bool property_nonatomic = false;
8522 bool property_readonly = false;
8523 bool property_readwrite = false;
8524 bool property_retain = false;
8525 tree property_setter_ident = NULL_TREE;
8527 /* 'properties' is the list of properties that we read. Usually a
8528 single one, but maybe more (eg, in "@property int a, b, c;" there
8529 are three). */
8530 tree properties;
8531 location_t loc;
8533 loc = c_parser_peek_token (parser)->location;
8534 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8536 c_parser_consume_token (parser); /* Eat '@property'. */
8538 /* Parse the optional attribute list... */
8539 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8541 /* Eat the '(' */
8542 c_parser_consume_token (parser);
8544 /* Property attribute keywords are valid now. */
8545 parser->objc_property_attr_context = true;
8547 while (true)
8549 bool syntax_error = false;
8550 c_token *token = c_parser_peek_token (parser);
8551 enum rid keyword;
8553 if (token->type != CPP_KEYWORD)
8555 if (token->type == CPP_CLOSE_PAREN)
8556 c_parser_error (parser, "expected identifier");
8557 else
8559 c_parser_consume_token (parser);
8560 c_parser_error (parser, "unknown property attribute");
8562 break;
8564 keyword = token->keyword;
8565 c_parser_consume_token (parser);
8566 switch (keyword)
8568 case RID_ASSIGN: property_assign = true; break;
8569 case RID_COPY: property_copy = true; break;
8570 case RID_NONATOMIC: property_nonatomic = true; break;
8571 case RID_READONLY: property_readonly = true; break;
8572 case RID_READWRITE: property_readwrite = true; break;
8573 case RID_RETAIN: property_retain = true; break;
8575 case RID_GETTER:
8576 case RID_SETTER:
8577 if (c_parser_next_token_is_not (parser, CPP_EQ))
8579 if (keyword == RID_GETTER)
8580 c_parser_error (parser,
8581 "missing %<=%> (after %<getter%> attribute)");
8582 else
8583 c_parser_error (parser,
8584 "missing %<=%> (after %<setter%> attribute)");
8585 syntax_error = true;
8586 break;
8588 c_parser_consume_token (parser); /* eat the = */
8589 if (c_parser_next_token_is_not (parser, CPP_NAME))
8591 c_parser_error (parser, "expected identifier");
8592 syntax_error = true;
8593 break;
8595 if (keyword == RID_SETTER)
8597 if (property_setter_ident != NULL_TREE)
8598 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8599 else
8600 property_setter_ident = c_parser_peek_token (parser)->value;
8601 c_parser_consume_token (parser);
8602 if (c_parser_next_token_is_not (parser, CPP_COLON))
8603 c_parser_error (parser, "setter name must terminate with %<:%>");
8604 else
8605 c_parser_consume_token (parser);
8607 else
8609 if (property_getter_ident != NULL_TREE)
8610 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8611 else
8612 property_getter_ident = c_parser_peek_token (parser)->value;
8613 c_parser_consume_token (parser);
8615 break;
8616 default:
8617 c_parser_error (parser, "unknown property attribute");
8618 syntax_error = true;
8619 break;
8622 if (syntax_error)
8623 break;
8625 if (c_parser_next_token_is (parser, CPP_COMMA))
8626 c_parser_consume_token (parser);
8627 else
8628 break;
8630 parser->objc_property_attr_context = false;
8631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8633 /* ... and the property declaration(s). */
8634 properties = c_parser_struct_declaration (parser);
8636 if (properties == error_mark_node)
8638 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8639 parser->error = false;
8640 return;
8643 if (properties == NULL_TREE)
8644 c_parser_error (parser, "expected identifier");
8645 else
8647 /* Comma-separated properties are chained together in
8648 reverse order; add them one by one. */
8649 properties = nreverse (properties);
8651 for (; properties; properties = TREE_CHAIN (properties))
8652 objc_add_property_declaration (loc, copy_node (properties),
8653 property_readonly, property_readwrite,
8654 property_assign, property_retain,
8655 property_copy, property_nonatomic,
8656 property_getter_ident, property_setter_ident);
8659 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8660 parser->error = false;
8663 /* Parse an Objective-C @synthesize declaration. The syntax is:
8665 objc-synthesize-declaration:
8666 @synthesize objc-synthesize-identifier-list ;
8668 objc-synthesize-identifier-list:
8669 objc-synthesize-identifier
8670 objc-synthesize-identifier-list, objc-synthesize-identifier
8672 objc-synthesize-identifier
8673 identifier
8674 identifier = identifier
8676 For example:
8677 @synthesize MyProperty;
8678 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8680 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8681 for C++. Keep them in sync.
8683 static void
8684 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8686 tree list = NULL_TREE;
8687 location_t loc;
8688 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8689 loc = c_parser_peek_token (parser)->location;
8691 c_parser_consume_token (parser);
8692 while (true)
8694 tree property, ivar;
8695 if (c_parser_next_token_is_not (parser, CPP_NAME))
8697 c_parser_error (parser, "expected identifier");
8698 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8699 /* Once we find the semicolon, we can resume normal parsing.
8700 We have to reset parser->error manually because
8701 c_parser_skip_until_found() won't reset it for us if the
8702 next token is precisely a semicolon. */
8703 parser->error = false;
8704 return;
8706 property = c_parser_peek_token (parser)->value;
8707 c_parser_consume_token (parser);
8708 if (c_parser_next_token_is (parser, CPP_EQ))
8710 c_parser_consume_token (parser);
8711 if (c_parser_next_token_is_not (parser, CPP_NAME))
8713 c_parser_error (parser, "expected identifier");
8714 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8715 parser->error = false;
8716 return;
8718 ivar = c_parser_peek_token (parser)->value;
8719 c_parser_consume_token (parser);
8721 else
8722 ivar = NULL_TREE;
8723 list = chainon (list, build_tree_list (ivar, property));
8724 if (c_parser_next_token_is (parser, CPP_COMMA))
8725 c_parser_consume_token (parser);
8726 else
8727 break;
8729 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8730 objc_add_synthesize_declaration (loc, list);
8733 /* Parse an Objective-C @dynamic declaration. The syntax is:
8735 objc-dynamic-declaration:
8736 @dynamic identifier-list ;
8738 For example:
8739 @dynamic MyProperty;
8740 @dynamic MyProperty, AnotherProperty;
8742 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8743 for C++. Keep them in sync.
8745 static void
8746 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8748 tree list = NULL_TREE;
8749 location_t loc;
8750 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8751 loc = c_parser_peek_token (parser)->location;
8753 c_parser_consume_token (parser);
8754 while (true)
8756 tree property;
8757 if (c_parser_next_token_is_not (parser, CPP_NAME))
8759 c_parser_error (parser, "expected identifier");
8760 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8761 parser->error = false;
8762 return;
8764 property = c_parser_peek_token (parser)->value;
8765 list = chainon (list, build_tree_list (NULL_TREE, property));
8766 c_parser_consume_token (parser);
8767 if (c_parser_next_token_is (parser, CPP_COMMA))
8768 c_parser_consume_token (parser);
8769 else
8770 break;
8772 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8773 objc_add_dynamic_declaration (loc, list);
8776 /* Parse UPC shared qualifier
8778 shared-type-qualifier: shared layout-qualifier-opt
8779 layout-qualifier: [ constant-expression-opt ] | [ * ]
8782 static void
8783 c_parser_upc_shared_qual (source_location loc,
8784 c_parser *parser,
8785 struct c_declspecs *specs)
8787 tree array_qual, arg1;
8789 /* consume "shared" part */
8790 c_parser_consume_token (parser);
8792 /* check for shared array layout specifier */
8793 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
8795 declspecs_add_qual (loc, specs, ridpointers[RID_SHARED]);
8796 return;
8798 c_parser_consume_token (parser);
8799 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8801 /* [] layout specifier */
8802 arg1 = size_zero_node;
8804 else if (c_parser_next_token_is (parser, CPP_MULT))
8806 /* [*] layout specifier */
8807 arg1 = build1 (INDIRECT_REF, NULL_TREE, NULL_TREE);
8808 c_parser_consume_token (parser);
8810 else
8812 /* [ expression ] layout specifier */
8813 arg1 = c_parser_expression (parser).value;
8815 array_qual = build4 (ARRAY_REF, NULL_TREE, NULL_TREE,
8816 arg1, NULL_TREE, NULL_TREE);
8817 declspecs_add_qual (loc, specs, array_qual);
8819 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
8821 c_parser_error (parser, "expected ]");
8823 c_parser_consume_token (parser);
8826 /* Parse a UPC upc_forall statement
8828 upc_forall-statement:
8829 upc_forall ( expression[opt] ; expression[opt] ;
8830 expression[opt] ; affinity[opt] ) statement
8831 affinity: experssion | continue */
8833 static void
8834 c_parser_upc_forall_statement (c_parser *parser)
8836 tree block, cond, incr, save_break, save_cont, body;
8837 tree affinity;
8838 location_t loc = c_parser_peek_token (parser)->location;
8839 location_t affinity_loc = UNKNOWN_LOCATION;
8840 const int profile_upc_forall = flag_upc_instrument && get_upc_pupc_mode();
8841 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_FORALL));
8842 c_parser_consume_token (parser);
8843 block = c_begin_compound_stmt (flag_isoc99);
8844 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8846 /* Parse the initialization declaration or expression. */
8847 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8849 c_parser_consume_token (parser);
8850 c_finish_expr_stmt (loc, NULL_TREE);
8852 else if (c_parser_next_token_starts_declspecs (parser))
8854 c_parser_declaration_or_fndef (parser, true, true, true,
8855 true, true, NULL);
8856 check_for_loop_decls (loc, true);
8858 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
8860 /* __extension__ can start a declaration, but is also an
8861 unary operator that can start an expression. Consume all
8862 but the last of a possible series of __extension__ to
8863 determine which. */
8864 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
8865 && (c_parser_peek_2nd_token (parser)->keyword
8866 == RID_EXTENSION))
8867 c_parser_consume_token (parser);
8868 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
8870 int ext;
8871 ext = disable_extension_diagnostics ();
8872 c_parser_consume_token (parser);
8873 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
8874 restore_extension_diagnostics (ext);
8875 check_for_loop_decls (loc, true);
8877 else
8878 goto init_expr;
8880 else
8882 init_expr:
8883 c_finish_expr_stmt (loc, c_parser_expression (parser).value);
8884 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8886 /* Parse the loop condition. */
8887 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8889 c_parser_consume_token (parser);
8890 cond = NULL_TREE;
8892 else
8894 cond = c_parser_condition (parser);
8895 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8897 /* Parse the increment expression. */
8898 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8899 incr = c_process_expr_stmt (loc, NULL_TREE);
8900 else
8901 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
8902 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8903 /* Parse the UPC affinity expression. */
8904 affinity_loc = c_parser_peek_token (parser)->location;
8905 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8907 affinity = NULL_TREE;
8909 else if (c_parser_peek_token (parser)->type == CPP_KEYWORD
8910 && c_parser_peek_token (parser)->keyword == RID_CONTINUE)
8912 affinity = NULL_TREE;
8913 c_parser_consume_token (parser);
8915 else
8917 affinity = c_parser_expression_conv (parser).value;
8918 affinity = c_fully_fold (affinity, false, NULL);
8920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8921 if (affinity)
8922 affinity = upc_affinity_test (affinity_loc, affinity);
8924 else
8926 cond = error_mark_node;
8927 incr = error_mark_node;
8928 affinity = error_mark_node;
8930 save_break = c_break_label;
8931 c_break_label = NULL_TREE;
8932 save_cont = c_cont_label;
8933 c_cont_label = NULL_TREE;
8934 body = c_parser_c99_block_statement (parser);
8935 if (profile_upc_forall)
8937 const tree gasp_start = upc_instrument_forall (loc, 1 /* start */);
8938 add_stmt (gasp_start);
8940 loc = c_parser_peek_token (parser)->location;
8941 if (affinity != NULL_TREE && affinity != error_mark_node)
8943 tree upc_forall_depth = upc_rts_forall_depth_var ();
8944 tree inc_depth, depth_gt_one;
8945 inc_depth = build_unary_op (loc, PREINCREMENT_EXPR, upc_forall_depth, 0);
8946 c_finish_expr_stmt (loc, inc_depth);
8947 depth_gt_one = build_binary_op (affinity_loc,
8948 GT_EXPR, upc_forall_depth, integer_one_node, 0);
8949 depth_gt_one = c_objc_common_truthvalue_conversion (affinity_loc, depth_gt_one);
8950 depth_gt_one = c_fully_fold (depth_gt_one, false, NULL);
8951 affinity = build_binary_op (affinity_loc, TRUTH_OR_EXPR,
8952 depth_gt_one, affinity, 0);
8953 body = build3 (COND_EXPR, void_type_node, affinity,
8954 body, NULL_TREE);
8955 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
8956 c_finish_expr_stmt (loc,
8957 build_unary_op (loc, PREDECREMENT_EXPR, upc_forall_depth, 0));
8959 else
8960 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
8961 if (profile_upc_forall)
8963 const tree gasp_end = upc_instrument_forall (loc, 0 /* start */);
8964 add_stmt (gasp_end);
8966 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
8967 c_break_label = save_break;
8968 c_cont_label = save_cont;
8971 /* Parse an upc-sync-statement.
8973 upc_barrier, upc_wait, upc_notify
8976 static void
8977 c_parser_upc_sync_statement (c_parser *parser, int sync_kind)
8979 location_t loc;
8980 tree expr = NULL_TREE;
8981 tree stmt;
8982 gcc_assert (c_parser_next_token_is_keyword (parser, RID_UPC_BARRIER) ||
8983 c_parser_next_token_is_keyword (parser, RID_UPC_NOTIFY) ||
8984 c_parser_next_token_is_keyword (parser, RID_UPC_WAIT));
8985 loc = c_parser_peek_token (parser)->location;
8986 c_parser_consume_token (parser);
8987 if (c_parser_peek_token (parser)->type != CPP_SEMICOLON)
8989 expr = c_parser_expression (parser).value;
8990 if (expr == error_mark_node)
8991 expr = NULL;
8992 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
8994 c_parser_error (parser, "expected integer expression");
8995 expr = NULL;
8998 stmt = size_int (sync_kind);
8999 (void) upc_build_sync_stmt (loc, stmt, expr);
9003 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9004 should be considered, statements. ALLOW_STMT is true if we're within
9005 the context of a function and such pragmas are to be allowed. Returns
9006 true if we actually parsed such a pragma. */
9008 static bool
9009 c_parser_pragma (c_parser *parser, enum pragma_context context)
9011 unsigned int id;
9013 id = c_parser_peek_token (parser)->pragma_kind;
9014 gcc_assert (id != PRAGMA_NONE);
9016 switch (id)
9018 case PRAGMA_OMP_BARRIER:
9019 if (context != pragma_compound)
9021 if (context == pragma_stmt)
9022 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9023 "used in compound statements");
9024 goto bad_stmt;
9026 c_parser_omp_barrier (parser);
9027 return false;
9029 case PRAGMA_OMP_FLUSH:
9030 if (context != pragma_compound)
9032 if (context == pragma_stmt)
9033 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9034 "used in compound statements");
9035 goto bad_stmt;
9037 c_parser_omp_flush (parser);
9038 return false;
9040 case PRAGMA_OMP_TASKWAIT:
9041 if (context != pragma_compound)
9043 if (context == pragma_stmt)
9044 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9045 "used in compound statements");
9046 goto bad_stmt;
9048 c_parser_omp_taskwait (parser);
9049 return false;
9051 case PRAGMA_OMP_TASKYIELD:
9052 if (context != pragma_compound)
9054 if (context == pragma_stmt)
9055 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9056 "used in compound statements");
9057 goto bad_stmt;
9059 c_parser_omp_taskyield (parser);
9060 return false;
9062 case PRAGMA_OMP_THREADPRIVATE:
9063 c_parser_omp_threadprivate (parser);
9064 return false;
9066 case PRAGMA_OMP_SECTION:
9067 error_at (c_parser_peek_token (parser)->location,
9068 "%<#pragma omp section%> may only be used in "
9069 "%<#pragma omp sections%> construct");
9070 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9071 return false;
9073 case PRAGMA_GCC_PCH_PREPROCESS:
9074 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9075 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9076 return false;
9078 default:
9079 if (id < PRAGMA_FIRST_EXTERNAL)
9081 if (context == pragma_external)
9083 bad_stmt:
9084 c_parser_error (parser, "expected declaration specifiers");
9085 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9086 return false;
9088 c_parser_omp_construct (parser);
9089 return true;
9091 break;
9094 c_parser_consume_pragma (parser);
9095 c_invoke_pragma_handler (id);
9097 /* Skip to EOL, but suppress any error message. Those will have been
9098 generated by the handler routine through calling error, as opposed
9099 to calling c_parser_error. */
9100 parser->error = true;
9101 c_parser_skip_to_pragma_eol (parser);
9103 return false;
9106 /* The interface the pragma parsers have to the lexer. */
9108 enum cpp_ttype
9109 pragma_lex (tree *value)
9111 c_token *tok = c_parser_peek_token (the_parser);
9112 enum cpp_ttype ret = tok->type;
9114 *value = tok->value;
9115 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9116 ret = CPP_EOF;
9117 else
9119 if (ret == CPP_KEYWORD)
9120 ret = CPP_NAME;
9121 c_parser_consume_token (the_parser);
9124 return ret;
9127 static void
9128 c_parser_pragma_pch_preprocess (c_parser *parser)
9130 tree name = NULL;
9132 c_parser_consume_pragma (parser);
9133 if (c_parser_next_token_is (parser, CPP_STRING))
9135 name = c_parser_peek_token (parser)->value;
9136 c_parser_consume_token (parser);
9138 else
9139 c_parser_error (parser, "expected string literal");
9140 c_parser_skip_to_pragma_eol (parser);
9142 if (name)
9143 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9146 /* OpenMP 2.5 parsing routines. */
9148 /* Returns name of the next clause.
9149 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9150 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9151 returned and the token is consumed. */
9153 static pragma_omp_clause
9154 c_parser_omp_clause_name (c_parser *parser)
9156 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9158 if (c_parser_next_token_is_keyword (parser, RID_IF))
9159 result = PRAGMA_OMP_CLAUSE_IF;
9160 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9161 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9162 else if (c_parser_next_token_is (parser, CPP_NAME))
9164 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9166 switch (p[0])
9168 case 'c':
9169 if (!strcmp ("collapse", p))
9170 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9171 else if (!strcmp ("copyin", p))
9172 result = PRAGMA_OMP_CLAUSE_COPYIN;
9173 else if (!strcmp ("copyprivate", p))
9174 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9175 break;
9176 case 'f':
9177 if (!strcmp ("final", p))
9178 result = PRAGMA_OMP_CLAUSE_FINAL;
9179 else if (!strcmp ("firstprivate", p))
9180 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9181 break;
9182 case 'l':
9183 if (!strcmp ("lastprivate", p))
9184 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9185 break;
9186 case 'm':
9187 if (!strcmp ("mergeable", p))
9188 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9189 break;
9190 case 'n':
9191 if (!strcmp ("nowait", p))
9192 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9193 else if (!strcmp ("num_threads", p))
9194 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9195 break;
9196 case 'o':
9197 if (!strcmp ("ordered", p))
9198 result = PRAGMA_OMP_CLAUSE_ORDERED;
9199 break;
9200 case 'p':
9201 if (!strcmp ("private", p))
9202 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9203 break;
9204 case 'r':
9205 if (!strcmp ("reduction", p))
9206 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9207 break;
9208 case 's':
9209 if (!strcmp ("schedule", p))
9210 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9211 else if (!strcmp ("shared", p))
9212 result = PRAGMA_OMP_CLAUSE_SHARED;
9213 break;
9214 case 'u':
9215 if (!strcmp ("untied", p))
9216 result = PRAGMA_OMP_CLAUSE_UNTIED;
9217 break;
9221 if (result != PRAGMA_OMP_CLAUSE_NONE)
9222 c_parser_consume_token (parser);
9224 return result;
9227 /* Validate that a clause of the given type does not already exist. */
9229 static void
9230 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9231 const char *name)
9233 tree c;
9235 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9236 if (OMP_CLAUSE_CODE (c) == code)
9238 location_t loc = OMP_CLAUSE_LOCATION (c);
9239 error_at (loc, "too many %qs clauses", name);
9240 break;
9244 /* OpenMP 2.5:
9245 variable-list:
9246 identifier
9247 variable-list , identifier
9249 If KIND is nonzero, create the appropriate node and install the
9250 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9251 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9253 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9254 return the list created. */
9256 static tree
9257 c_parser_omp_variable_list (c_parser *parser,
9258 location_t clause_loc,
9259 enum omp_clause_code kind,
9260 tree list)
9262 if (c_parser_next_token_is_not (parser, CPP_NAME)
9263 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9264 c_parser_error (parser, "expected identifier");
9266 while (c_parser_next_token_is (parser, CPP_NAME)
9267 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9269 tree t = lookup_name (c_parser_peek_token (parser)->value);
9271 if (t == NULL_TREE)
9272 undeclared_variable (c_parser_peek_token (parser)->location,
9273 c_parser_peek_token (parser)->value);
9274 else if (t == error_mark_node)
9276 else if (kind != 0)
9278 tree u = build_omp_clause (clause_loc, kind);
9279 OMP_CLAUSE_DECL (u) = t;
9280 OMP_CLAUSE_CHAIN (u) = list;
9281 list = u;
9283 else
9284 list = tree_cons (t, NULL_TREE, list);
9286 c_parser_consume_token (parser);
9288 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9289 break;
9291 c_parser_consume_token (parser);
9294 return list;
9297 /* Similarly, but expect leading and trailing parenthesis. This is a very
9298 common case for omp clauses. */
9300 static tree
9301 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9302 tree list)
9304 /* The clauses location. */
9305 location_t loc = c_parser_peek_token (parser)->location;
9307 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9309 list = c_parser_omp_variable_list (parser, loc, kind, list);
9310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9312 return list;
9315 /* OpenMP 3.0:
9316 collapse ( constant-expression ) */
9318 static tree
9319 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9321 tree c, num = error_mark_node;
9322 HOST_WIDE_INT n;
9323 location_t loc;
9325 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9327 loc = c_parser_peek_token (parser)->location;
9328 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9330 num = c_parser_expr_no_commas (parser, NULL).value;
9331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9333 if (num == error_mark_node)
9334 return list;
9335 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9336 || !host_integerp (num, 0)
9337 || (n = tree_low_cst (num, 0)) <= 0
9338 || (int) n != n)
9340 error_at (loc,
9341 "collapse argument needs positive constant integer expression");
9342 return list;
9344 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9345 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9346 OMP_CLAUSE_CHAIN (c) = list;
9347 return c;
9350 /* OpenMP 2.5:
9351 copyin ( variable-list ) */
9353 static tree
9354 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9356 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9359 /* OpenMP 2.5:
9360 copyprivate ( variable-list ) */
9362 static tree
9363 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9365 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9368 /* OpenMP 2.5:
9369 default ( shared | none ) */
9371 static tree
9372 c_parser_omp_clause_default (c_parser *parser, tree list)
9374 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9375 location_t loc = c_parser_peek_token (parser)->location;
9376 tree c;
9378 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9379 return list;
9380 if (c_parser_next_token_is (parser, CPP_NAME))
9382 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9384 switch (p[0])
9386 case 'n':
9387 if (strcmp ("none", p) != 0)
9388 goto invalid_kind;
9389 kind = OMP_CLAUSE_DEFAULT_NONE;
9390 break;
9392 case 's':
9393 if (strcmp ("shared", p) != 0)
9394 goto invalid_kind;
9395 kind = OMP_CLAUSE_DEFAULT_SHARED;
9396 break;
9398 default:
9399 goto invalid_kind;
9402 c_parser_consume_token (parser);
9404 else
9406 invalid_kind:
9407 c_parser_error (parser, "expected %<none%> or %<shared%>");
9409 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9411 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9412 return list;
9414 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9415 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9416 OMP_CLAUSE_CHAIN (c) = list;
9417 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9419 return c;
9422 /* OpenMP 2.5:
9423 firstprivate ( variable-list ) */
9425 static tree
9426 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9428 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9431 /* OpenMP 3.1:
9432 final ( expression ) */
9434 static tree
9435 c_parser_omp_clause_final (c_parser *parser, tree list)
9437 location_t loc = c_parser_peek_token (parser)->location;
9438 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9440 tree t = c_parser_paren_condition (parser);
9441 tree c;
9443 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9445 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9446 OMP_CLAUSE_FINAL_EXPR (c) = t;
9447 OMP_CLAUSE_CHAIN (c) = list;
9448 list = c;
9450 else
9451 c_parser_error (parser, "expected %<(%>");
9453 return list;
9456 /* OpenMP 2.5:
9457 if ( expression ) */
9459 static tree
9460 c_parser_omp_clause_if (c_parser *parser, tree list)
9462 location_t loc = c_parser_peek_token (parser)->location;
9463 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9465 tree t = c_parser_paren_condition (parser);
9466 tree c;
9468 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9470 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9471 OMP_CLAUSE_IF_EXPR (c) = t;
9472 OMP_CLAUSE_CHAIN (c) = list;
9473 list = c;
9475 else
9476 c_parser_error (parser, "expected %<(%>");
9478 return list;
9481 /* OpenMP 2.5:
9482 lastprivate ( variable-list ) */
9484 static tree
9485 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
9487 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
9490 /* OpenMP 3.1:
9491 mergeable */
9493 static tree
9494 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9496 tree c;
9498 /* FIXME: Should we allow duplicates? */
9499 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
9501 c = build_omp_clause (c_parser_peek_token (parser)->location,
9502 OMP_CLAUSE_MERGEABLE);
9503 OMP_CLAUSE_CHAIN (c) = list;
9505 return c;
9508 /* OpenMP 2.5:
9509 nowait */
9511 static tree
9512 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9514 tree c;
9515 location_t loc = c_parser_peek_token (parser)->location;
9517 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9519 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9520 OMP_CLAUSE_CHAIN (c) = list;
9521 return c;
9524 /* OpenMP 2.5:
9525 num_threads ( expression ) */
9527 static tree
9528 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9530 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9531 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9533 location_t expr_loc = c_parser_peek_token (parser)->location;
9534 tree c, t = c_parser_expression (parser).value;
9535 mark_exp_read (t);
9536 t = c_fully_fold (t, false, NULL);
9538 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9540 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9542 c_parser_error (parser, "expected integer expression");
9543 return list;
9546 /* Attempt to statically determine when the number isn't positive. */
9547 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9548 build_int_cst (TREE_TYPE (t), 0));
9549 if (CAN_HAVE_LOCATION_P (c))
9550 SET_EXPR_LOCATION (c, expr_loc);
9551 if (c == boolean_true_node)
9553 warning_at (expr_loc, 0,
9554 "%<num_threads%> value must be positive");
9555 t = integer_one_node;
9558 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9560 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9561 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9562 OMP_CLAUSE_CHAIN (c) = list;
9563 list = c;
9566 return list;
9569 /* OpenMP 2.5:
9570 ordered */
9572 static tree
9573 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9575 tree c;
9577 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9579 c = build_omp_clause (c_parser_peek_token (parser)->location,
9580 OMP_CLAUSE_ORDERED);
9581 OMP_CLAUSE_CHAIN (c) = list;
9583 return c;
9586 /* OpenMP 2.5:
9587 private ( variable-list ) */
9589 static tree
9590 c_parser_omp_clause_private (c_parser *parser, tree list)
9592 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9595 /* OpenMP 2.5:
9596 reduction ( reduction-operator : variable-list )
9598 reduction-operator:
9599 One of: + * - & ^ | && ||
9601 OpenMP 3.1:
9603 reduction-operator:
9604 One of: + * - & ^ | && || max min */
9606 static tree
9607 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9609 location_t clause_loc = c_parser_peek_token (parser)->location;
9610 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9612 enum tree_code code;
9614 switch (c_parser_peek_token (parser)->type)
9616 case CPP_PLUS:
9617 code = PLUS_EXPR;
9618 break;
9619 case CPP_MULT:
9620 code = MULT_EXPR;
9621 break;
9622 case CPP_MINUS:
9623 code = MINUS_EXPR;
9624 break;
9625 case CPP_AND:
9626 code = BIT_AND_EXPR;
9627 break;
9628 case CPP_XOR:
9629 code = BIT_XOR_EXPR;
9630 break;
9631 case CPP_OR:
9632 code = BIT_IOR_EXPR;
9633 break;
9634 case CPP_AND_AND:
9635 code = TRUTH_ANDIF_EXPR;
9636 break;
9637 case CPP_OR_OR:
9638 code = TRUTH_ORIF_EXPR;
9639 break;
9640 case CPP_NAME:
9642 const char *p
9643 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9644 if (strcmp (p, "min") == 0)
9646 code = MIN_EXPR;
9647 break;
9649 if (strcmp (p, "max") == 0)
9651 code = MAX_EXPR;
9652 break;
9655 /* FALLTHRU */
9656 default:
9657 c_parser_error (parser,
9658 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9659 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9661 return list;
9663 c_parser_consume_token (parser);
9664 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9666 tree nl, c;
9668 nl = c_parser_omp_variable_list (parser, clause_loc,
9669 OMP_CLAUSE_REDUCTION, list);
9670 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9671 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9673 list = nl;
9675 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9677 return list;
9680 /* OpenMP 2.5:
9681 schedule ( schedule-kind )
9682 schedule ( schedule-kind , expression )
9684 schedule-kind:
9685 static | dynamic | guided | runtime | auto
9688 static tree
9689 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9691 tree c, t;
9692 location_t loc = c_parser_peek_token (parser)->location;
9694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9695 return list;
9697 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9699 if (c_parser_next_token_is (parser, CPP_NAME))
9701 tree kind = c_parser_peek_token (parser)->value;
9702 const char *p = IDENTIFIER_POINTER (kind);
9704 switch (p[0])
9706 case 'd':
9707 if (strcmp ("dynamic", p) != 0)
9708 goto invalid_kind;
9709 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9710 break;
9712 case 'g':
9713 if (strcmp ("guided", p) != 0)
9714 goto invalid_kind;
9715 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9716 break;
9718 case 'r':
9719 if (strcmp ("runtime", p) != 0)
9720 goto invalid_kind;
9721 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9722 break;
9724 default:
9725 goto invalid_kind;
9728 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9729 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9730 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9731 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9732 else
9733 goto invalid_kind;
9735 c_parser_consume_token (parser);
9736 if (c_parser_next_token_is (parser, CPP_COMMA))
9738 location_t here;
9739 c_parser_consume_token (parser);
9741 here = c_parser_peek_token (parser)->location;
9742 t = c_parser_expr_no_commas (parser, NULL).value;
9743 mark_exp_read (t);
9744 t = c_fully_fold (t, false, NULL);
9746 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9747 error_at (here, "schedule %<runtime%> does not take "
9748 "a %<chunk_size%> parameter");
9749 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9750 error_at (here,
9751 "schedule %<auto%> does not take "
9752 "a %<chunk_size%> parameter");
9753 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9754 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9755 else
9756 c_parser_error (parser, "expected integer expression");
9758 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9760 else
9761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9762 "expected %<,%> or %<)%>");
9764 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9765 OMP_CLAUSE_CHAIN (c) = list;
9766 return c;
9768 invalid_kind:
9769 c_parser_error (parser, "invalid schedule kind");
9770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9771 return list;
9774 /* OpenMP 2.5:
9775 shared ( variable-list ) */
9777 static tree
9778 c_parser_omp_clause_shared (c_parser *parser, tree list)
9780 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9783 /* OpenMP 3.0:
9784 untied */
9786 static tree
9787 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9789 tree c;
9791 /* FIXME: Should we allow duplicates? */
9792 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9794 c = build_omp_clause (c_parser_peek_token (parser)->location,
9795 OMP_CLAUSE_UNTIED);
9796 OMP_CLAUSE_CHAIN (c) = list;
9798 return c;
9801 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9802 is a bitmask in MASK. Return the list of clauses found; the result
9803 of clause default goes in *pdefault. */
9805 static tree
9806 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9807 const char *where)
9809 tree clauses = NULL;
9810 bool first = true;
9812 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9814 location_t here;
9815 pragma_omp_clause c_kind;
9816 const char *c_name;
9817 tree prev = clauses;
9819 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9820 c_parser_consume_token (parser);
9822 first = false;
9823 here = c_parser_peek_token (parser)->location;
9824 c_kind = c_parser_omp_clause_name (parser);
9826 switch (c_kind)
9828 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9829 clauses = c_parser_omp_clause_collapse (parser, clauses);
9830 c_name = "collapse";
9831 break;
9832 case PRAGMA_OMP_CLAUSE_COPYIN:
9833 clauses = c_parser_omp_clause_copyin (parser, clauses);
9834 c_name = "copyin";
9835 break;
9836 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9837 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9838 c_name = "copyprivate";
9839 break;
9840 case PRAGMA_OMP_CLAUSE_DEFAULT:
9841 clauses = c_parser_omp_clause_default (parser, clauses);
9842 c_name = "default";
9843 break;
9844 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9845 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9846 c_name = "firstprivate";
9847 break;
9848 case PRAGMA_OMP_CLAUSE_FINAL:
9849 clauses = c_parser_omp_clause_final (parser, clauses);
9850 c_name = "final";
9851 break;
9852 case PRAGMA_OMP_CLAUSE_IF:
9853 clauses = c_parser_omp_clause_if (parser, clauses);
9854 c_name = "if";
9855 break;
9856 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9857 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9858 c_name = "lastprivate";
9859 break;
9860 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9861 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9862 c_name = "mergeable";
9863 break;
9864 case PRAGMA_OMP_CLAUSE_NOWAIT:
9865 clauses = c_parser_omp_clause_nowait (parser, clauses);
9866 c_name = "nowait";
9867 break;
9868 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9869 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9870 c_name = "num_threads";
9871 break;
9872 case PRAGMA_OMP_CLAUSE_ORDERED:
9873 clauses = c_parser_omp_clause_ordered (parser, clauses);
9874 c_name = "ordered";
9875 break;
9876 case PRAGMA_OMP_CLAUSE_PRIVATE:
9877 clauses = c_parser_omp_clause_private (parser, clauses);
9878 c_name = "private";
9879 break;
9880 case PRAGMA_OMP_CLAUSE_REDUCTION:
9881 clauses = c_parser_omp_clause_reduction (parser, clauses);
9882 c_name = "reduction";
9883 break;
9884 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9885 clauses = c_parser_omp_clause_schedule (parser, clauses);
9886 c_name = "schedule";
9887 break;
9888 case PRAGMA_OMP_CLAUSE_SHARED:
9889 clauses = c_parser_omp_clause_shared (parser, clauses);
9890 c_name = "shared";
9891 break;
9892 case PRAGMA_OMP_CLAUSE_UNTIED:
9893 clauses = c_parser_omp_clause_untied (parser, clauses);
9894 c_name = "untied";
9895 break;
9896 default:
9897 c_parser_error (parser, "expected %<#pragma omp%> clause");
9898 goto saw_error;
9901 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9903 /* Remove the invalid clause(s) from the list to avoid
9904 confusing the rest of the compiler. */
9905 clauses = prev;
9906 error_at (here, "%qs is not valid for %qs", c_name, where);
9910 saw_error:
9911 c_parser_skip_to_pragma_eol (parser);
9913 return c_finish_omp_clauses (clauses);
9916 /* OpenMP 2.5:
9917 structured-block:
9918 statement
9920 In practice, we're also interested in adding the statement to an
9921 outer node. So it is convenient if we work around the fact that
9922 c_parser_statement calls add_stmt. */
9924 static tree
9925 c_parser_omp_structured_block (c_parser *parser)
9927 tree stmt = push_stmt_list ();
9928 c_parser_statement (parser);
9929 return pop_stmt_list (stmt);
9932 /* OpenMP 2.5:
9933 # pragma omp atomic new-line
9934 expression-stmt
9936 expression-stmt:
9937 x binop= expr | x++ | ++x | x-- | --x
9938 binop:
9939 +, *, -, /, &, ^, |, <<, >>
9941 where x is an lvalue expression with scalar type.
9943 OpenMP 3.1:
9944 # pragma omp atomic new-line
9945 update-stmt
9947 # pragma omp atomic read new-line
9948 read-stmt
9950 # pragma omp atomic write new-line
9951 write-stmt
9953 # pragma omp atomic update new-line
9954 update-stmt
9956 # pragma omp atomic capture new-line
9957 capture-stmt
9959 # pragma omp atomic capture new-line
9960 capture-block
9962 read-stmt:
9963 v = x
9964 write-stmt:
9965 x = expr
9966 update-stmt:
9967 expression-stmt | x = x binop expr
9968 capture-stmt:
9969 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9970 capture-block:
9971 { v = x; update-stmt; } | { update-stmt; v = x; }
9973 where x and v are lvalue expressions with scalar type.
9975 LOC is the location of the #pragma token. */
9977 static void
9978 c_parser_omp_atomic (location_t loc, c_parser *parser)
9980 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9981 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9982 tree stmt, orig_lhs;
9983 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9984 struct c_expr rhs_expr;
9985 bool structured_block = false;
9987 if (c_parser_next_token_is (parser, CPP_NAME))
9989 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9991 if (!strcmp (p, "read"))
9992 code = OMP_ATOMIC_READ;
9993 else if (!strcmp (p, "write"))
9994 code = NOP_EXPR;
9995 else if (!strcmp (p, "update"))
9996 code = OMP_ATOMIC;
9997 else if (!strcmp (p, "capture"))
9998 code = OMP_ATOMIC_CAPTURE_NEW;
9999 else
10000 p = NULL;
10001 if (p)
10002 c_parser_consume_token (parser);
10004 c_parser_skip_to_pragma_eol (parser);
10006 switch (code)
10008 case OMP_ATOMIC_READ:
10009 case NOP_EXPR: /* atomic write */
10010 v = c_parser_unary_expression (parser).value;
10011 v = c_fully_fold (v, false, NULL);
10012 if (v == error_mark_node)
10013 goto saw_error;
10014 loc = c_parser_peek_token (parser)->location;
10015 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10016 goto saw_error;
10017 if (code == NOP_EXPR)
10018 lhs = c_parser_expression (parser).value;
10019 else
10020 lhs = c_parser_unary_expression (parser).value;
10021 lhs = c_fully_fold (lhs, false, NULL);
10022 if (lhs == error_mark_node)
10023 goto saw_error;
10024 if (code == NOP_EXPR)
10026 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
10027 opcode. */
10028 code = OMP_ATOMIC;
10029 rhs = lhs;
10030 lhs = v;
10031 v = NULL_TREE;
10033 goto done;
10034 case OMP_ATOMIC_CAPTURE_NEW:
10035 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10037 c_parser_consume_token (parser);
10038 structured_block = true;
10040 else
10042 v = c_parser_unary_expression (parser).value;
10043 v = c_fully_fold (v, false, NULL);
10044 if (v == error_mark_node)
10045 goto saw_error;
10046 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10047 goto saw_error;
10049 break;
10050 default:
10051 break;
10054 /* For structured_block case we don't know yet whether
10055 old or new x should be captured. */
10056 restart:
10057 lhs = c_parser_unary_expression (parser).value;
10058 lhs = c_fully_fold (lhs, false, NULL);
10059 orig_lhs = lhs;
10060 switch (TREE_CODE (lhs))
10062 case ERROR_MARK:
10063 saw_error:
10064 c_parser_skip_to_end_of_block_or_statement (parser);
10065 if (structured_block)
10067 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10068 c_parser_consume_token (parser);
10069 else if (code == OMP_ATOMIC_CAPTURE_NEW)
10071 c_parser_skip_to_end_of_block_or_statement (parser);
10072 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10073 c_parser_consume_token (parser);
10076 return;
10078 case POSTINCREMENT_EXPR:
10079 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10080 code = OMP_ATOMIC_CAPTURE_OLD;
10081 /* FALLTHROUGH */
10082 case PREINCREMENT_EXPR:
10083 lhs = TREE_OPERAND (lhs, 0);
10084 opcode = PLUS_EXPR;
10085 rhs = integer_one_node;
10086 break;
10088 case POSTDECREMENT_EXPR:
10089 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
10090 code = OMP_ATOMIC_CAPTURE_OLD;
10091 /* FALLTHROUGH */
10092 case PREDECREMENT_EXPR:
10093 lhs = TREE_OPERAND (lhs, 0);
10094 opcode = MINUS_EXPR;
10095 rhs = integer_one_node;
10096 break;
10098 case COMPOUND_EXPR:
10099 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
10100 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
10101 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
10102 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
10103 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
10104 (TREE_OPERAND (lhs, 1), 0), 0)))
10105 == BOOLEAN_TYPE)
10106 /* Undo effects of boolean_increment for post {in,de}crement. */
10107 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
10108 /* FALLTHRU */
10109 case MODIFY_EXPR:
10110 if (TREE_CODE (lhs) == MODIFY_EXPR
10111 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
10113 /* Undo effects of boolean_increment. */
10114 if (integer_onep (TREE_OPERAND (lhs, 1)))
10116 /* This is pre or post increment. */
10117 rhs = TREE_OPERAND (lhs, 1);
10118 lhs = TREE_OPERAND (lhs, 0);
10119 opcode = NOP_EXPR;
10120 if (code == OMP_ATOMIC_CAPTURE_NEW
10121 && !structured_block
10122 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10123 code = OMP_ATOMIC_CAPTURE_OLD;
10124 break;
10126 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
10127 && TREE_OPERAND (lhs, 0)
10128 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
10130 /* This is pre or post decrement. */
10131 rhs = TREE_OPERAND (lhs, 1);
10132 lhs = TREE_OPERAND (lhs, 0);
10133 opcode = NOP_EXPR;
10134 if (code == OMP_ATOMIC_CAPTURE_NEW
10135 && !structured_block
10136 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
10137 code = OMP_ATOMIC_CAPTURE_OLD;
10138 break;
10141 /* FALLTHRU */
10142 default:
10143 switch (c_parser_peek_token (parser)->type)
10145 case CPP_MULT_EQ:
10146 opcode = MULT_EXPR;
10147 break;
10148 case CPP_DIV_EQ:
10149 opcode = TRUNC_DIV_EXPR;
10150 break;
10151 case CPP_PLUS_EQ:
10152 opcode = PLUS_EXPR;
10153 break;
10154 case CPP_MINUS_EQ:
10155 opcode = MINUS_EXPR;
10156 break;
10157 case CPP_LSHIFT_EQ:
10158 opcode = LSHIFT_EXPR;
10159 break;
10160 case CPP_RSHIFT_EQ:
10161 opcode = RSHIFT_EXPR;
10162 break;
10163 case CPP_AND_EQ:
10164 opcode = BIT_AND_EXPR;
10165 break;
10166 case CPP_OR_EQ:
10167 opcode = BIT_IOR_EXPR;
10168 break;
10169 case CPP_XOR_EQ:
10170 opcode = BIT_XOR_EXPR;
10171 break;
10172 case CPP_EQ:
10173 if (structured_block || code == OMP_ATOMIC)
10175 location_t aloc = c_parser_peek_token (parser)->location;
10176 location_t rhs_loc;
10177 enum c_parser_prec oprec = PREC_NONE;
10179 c_parser_consume_token (parser);
10180 rhs1 = c_parser_unary_expression (parser).value;
10181 rhs1 = c_fully_fold (rhs1, false, NULL);
10182 if (rhs1 == error_mark_node)
10183 goto saw_error;
10184 switch (c_parser_peek_token (parser)->type)
10186 case CPP_SEMICOLON:
10187 if (code == OMP_ATOMIC_CAPTURE_NEW)
10189 code = OMP_ATOMIC_CAPTURE_OLD;
10190 v = lhs;
10191 lhs = NULL_TREE;
10192 lhs1 = rhs1;
10193 rhs1 = NULL_TREE;
10194 c_parser_consume_token (parser);
10195 goto restart;
10197 c_parser_error (parser,
10198 "invalid form of %<#pragma omp atomic%>");
10199 goto saw_error;
10200 case CPP_MULT:
10201 opcode = MULT_EXPR;
10202 oprec = PREC_MULT;
10203 break;
10204 case CPP_DIV:
10205 opcode = TRUNC_DIV_EXPR;
10206 oprec = PREC_MULT;
10207 break;
10208 case CPP_PLUS:
10209 opcode = PLUS_EXPR;
10210 oprec = PREC_ADD;
10211 break;
10212 case CPP_MINUS:
10213 opcode = MINUS_EXPR;
10214 oprec = PREC_ADD;
10215 break;
10216 case CPP_LSHIFT:
10217 opcode = LSHIFT_EXPR;
10218 oprec = PREC_SHIFT;
10219 break;
10220 case CPP_RSHIFT:
10221 opcode = RSHIFT_EXPR;
10222 oprec = PREC_SHIFT;
10223 break;
10224 case CPP_AND:
10225 opcode = BIT_AND_EXPR;
10226 oprec = PREC_BITAND;
10227 break;
10228 case CPP_OR:
10229 opcode = BIT_IOR_EXPR;
10230 oprec = PREC_BITOR;
10231 break;
10232 case CPP_XOR:
10233 opcode = BIT_XOR_EXPR;
10234 oprec = PREC_BITXOR;
10235 break;
10236 default:
10237 c_parser_error (parser,
10238 "invalid operator for %<#pragma omp atomic%>");
10239 goto saw_error;
10241 loc = aloc;
10242 c_parser_consume_token (parser);
10243 rhs_loc = c_parser_peek_token (parser)->location;
10244 if (commutative_tree_code (opcode))
10245 oprec = (enum c_parser_prec) (oprec - 1);
10246 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
10247 rhs_expr = default_function_array_read_conversion (rhs_loc,
10248 rhs_expr);
10249 rhs = rhs_expr.value;
10250 rhs = c_fully_fold (rhs, false, NULL);
10251 goto stmt_done;
10253 /* FALLTHROUGH */
10254 default:
10255 c_parser_error (parser,
10256 "invalid operator for %<#pragma omp atomic%>");
10257 goto saw_error;
10260 /* Arrange to pass the location of the assignment operator to
10261 c_finish_omp_atomic. */
10262 loc = c_parser_peek_token (parser)->location;
10263 c_parser_consume_token (parser);
10265 location_t rhs_loc = c_parser_peek_token (parser)->location;
10266 rhs_expr = c_parser_expression (parser);
10267 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
10269 rhs = rhs_expr.value;
10270 rhs = c_fully_fold (rhs, false, NULL);
10271 break;
10273 stmt_done:
10274 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
10276 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
10277 goto saw_error;
10278 v = c_parser_unary_expression (parser).value;
10279 v = c_fully_fold (v, false, NULL);
10280 if (v == error_mark_node)
10281 goto saw_error;
10282 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
10283 goto saw_error;
10284 lhs1 = c_parser_unary_expression (parser).value;
10285 lhs1 = c_fully_fold (lhs1, false, NULL);
10286 if (lhs1 == error_mark_node)
10287 goto saw_error;
10289 if (structured_block)
10291 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10292 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
10294 done:
10295 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
10296 if (stmt != error_mark_node)
10297 add_stmt (stmt);
10299 if (!structured_block)
10300 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10304 /* OpenMP 2.5:
10305 # pragma omp barrier new-line
10308 static void
10309 c_parser_omp_barrier (c_parser *parser)
10311 location_t loc = c_parser_peek_token (parser)->location;
10312 c_parser_consume_pragma (parser);
10313 c_parser_skip_to_pragma_eol (parser);
10315 c_finish_omp_barrier (loc);
10318 /* OpenMP 2.5:
10319 # pragma omp critical [(name)] new-line
10320 structured-block
10322 LOC is the location of the #pragma itself. */
10324 static tree
10325 c_parser_omp_critical (location_t loc, c_parser *parser)
10327 tree stmt, name = NULL;
10329 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10331 c_parser_consume_token (parser);
10332 if (c_parser_next_token_is (parser, CPP_NAME))
10334 name = c_parser_peek_token (parser)->value;
10335 c_parser_consume_token (parser);
10336 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10338 else
10339 c_parser_error (parser, "expected identifier");
10341 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10342 c_parser_error (parser, "expected %<(%> or end of line");
10343 c_parser_skip_to_pragma_eol (parser);
10345 stmt = c_parser_omp_structured_block (parser);
10346 return c_finish_omp_critical (loc, stmt, name);
10349 /* OpenMP 2.5:
10350 # pragma omp flush flush-vars[opt] new-line
10352 flush-vars:
10353 ( variable-list ) */
10355 static void
10356 c_parser_omp_flush (c_parser *parser)
10358 location_t loc = c_parser_peek_token (parser)->location;
10359 c_parser_consume_pragma (parser);
10360 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10361 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10362 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10363 c_parser_error (parser, "expected %<(%> or end of line");
10364 c_parser_skip_to_pragma_eol (parser);
10366 c_finish_omp_flush (loc);
10369 /* Parse the restricted form of the for statement allowed by OpenMP.
10370 The real trick here is to determine the loop control variable early
10371 so that we can push a new decl if necessary to make it private.
10372 LOC is the location of the OMP in "#pragma omp". */
10374 static tree
10375 c_parser_omp_for_loop (location_t loc,
10376 c_parser *parser, tree clauses, tree *par_clauses)
10378 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
10379 tree declv, condv, incrv, initv, ret = NULL;
10380 bool fail = false, open_brace_parsed = false;
10381 int i, collapse = 1, nbraces = 0;
10382 location_t for_loc;
10383 vec<tree, va_gc> *for_block = make_tree_vector ();
10385 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
10386 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
10387 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
10389 gcc_assert (collapse >= 1);
10391 declv = make_tree_vec (collapse);
10392 initv = make_tree_vec (collapse);
10393 condv = make_tree_vec (collapse);
10394 incrv = make_tree_vec (collapse);
10396 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
10398 c_parser_error (parser, "for statement expected");
10399 return NULL;
10401 for_loc = c_parser_peek_token (parser)->location;
10402 c_parser_consume_token (parser);
10404 for (i = 0; i < collapse; i++)
10406 int bracecount = 0;
10408 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10409 goto pop_scopes;
10411 /* Parse the initialization declaration or expression. */
10412 if (c_parser_next_tokens_start_declaration (parser))
10414 if (i > 0)
10415 vec_safe_push (for_block, c_begin_compound_stmt (true));
10416 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
10417 decl = check_for_loop_decls (for_loc, flag_isoc99);
10418 if (decl == NULL)
10419 goto error_init;
10420 if (DECL_INITIAL (decl) == error_mark_node)
10421 decl = error_mark_node;
10422 init = decl;
10424 else if (c_parser_next_token_is (parser, CPP_NAME)
10425 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
10427 struct c_expr decl_exp;
10428 struct c_expr init_exp;
10429 location_t init_loc;
10431 decl_exp = c_parser_postfix_expression (parser);
10432 decl = decl_exp.value;
10434 c_parser_require (parser, CPP_EQ, "expected %<=%>");
10436 init_loc = c_parser_peek_token (parser)->location;
10437 init_exp = c_parser_expr_no_commas (parser, NULL);
10438 init_exp = default_function_array_read_conversion (init_loc,
10439 init_exp);
10440 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
10441 NOP_EXPR, init_loc, init_exp.value,
10442 init_exp.original_type);
10443 init = c_process_expr_stmt (init_loc, init);
10445 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10447 else
10449 error_init:
10450 c_parser_error (parser,
10451 "expected iteration declaration or initialization");
10452 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10453 "expected %<)%>");
10454 fail = true;
10455 goto parse_next;
10458 /* Parse the loop condition. */
10459 cond = NULL_TREE;
10460 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
10462 location_t cond_loc = c_parser_peek_token (parser)->location;
10463 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
10464 PREC_NONE);
10466 cond = cond_expr.value;
10467 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
10468 cond = c_fully_fold (cond, false, NULL);
10469 switch (cond_expr.original_code)
10471 case GT_EXPR:
10472 case GE_EXPR:
10473 case LT_EXPR:
10474 case LE_EXPR:
10475 break;
10476 default:
10477 /* Can't be cond = error_mark_node, because we want to preserve
10478 the location until c_finish_omp_for. */
10479 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
10480 break;
10482 protected_set_expr_location (cond, cond_loc);
10484 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10486 /* Parse the increment expression. */
10487 incr = NULL_TREE;
10488 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
10490 location_t incr_loc = c_parser_peek_token (parser)->location;
10492 incr = c_process_expr_stmt (incr_loc,
10493 c_parser_expression (parser).value);
10495 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10497 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
10498 fail = true;
10499 else
10501 TREE_VEC_ELT (declv, i) = decl;
10502 TREE_VEC_ELT (initv, i) = init;
10503 TREE_VEC_ELT (condv, i) = cond;
10504 TREE_VEC_ELT (incrv, i) = incr;
10507 parse_next:
10508 if (i == collapse - 1)
10509 break;
10511 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10512 in between the collapsed for loops to be still considered perfectly
10513 nested. Hopefully the final version clarifies this.
10514 For now handle (multiple) {'s and empty statements. */
10517 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10519 c_parser_consume_token (parser);
10520 break;
10522 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10524 c_parser_consume_token (parser);
10525 bracecount++;
10527 else if (bracecount
10528 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10529 c_parser_consume_token (parser);
10530 else
10532 c_parser_error (parser, "not enough perfectly nested loops");
10533 if (bracecount)
10535 open_brace_parsed = true;
10536 bracecount--;
10538 fail = true;
10539 collapse = 0;
10540 break;
10543 while (1);
10545 nbraces += bracecount;
10548 save_break = c_break_label;
10549 c_break_label = size_one_node;
10550 save_cont = c_cont_label;
10551 c_cont_label = NULL_TREE;
10552 body = push_stmt_list ();
10554 if (open_brace_parsed)
10556 location_t here = c_parser_peek_token (parser)->location;
10557 stmt = c_begin_compound_stmt (true);
10558 c_parser_compound_statement_nostart (parser);
10559 add_stmt (c_end_compound_stmt (here, stmt, true));
10561 else
10562 add_stmt (c_parser_c99_block_statement (parser));
10563 if (c_cont_label)
10565 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10566 SET_EXPR_LOCATION (t, loc);
10567 add_stmt (t);
10570 body = pop_stmt_list (body);
10571 c_break_label = save_break;
10572 c_cont_label = save_cont;
10574 while (nbraces)
10576 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10578 c_parser_consume_token (parser);
10579 nbraces--;
10581 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10582 c_parser_consume_token (parser);
10583 else
10585 c_parser_error (parser, "collapsed loops not perfectly nested");
10586 while (nbraces)
10588 location_t here = c_parser_peek_token (parser)->location;
10589 stmt = c_begin_compound_stmt (true);
10590 add_stmt (body);
10591 c_parser_compound_statement_nostart (parser);
10592 body = c_end_compound_stmt (here, stmt, true);
10593 nbraces--;
10595 goto pop_scopes;
10599 /* Only bother calling c_finish_omp_for if we haven't already generated
10600 an error from the initialization parsing. */
10601 if (!fail)
10603 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10604 if (stmt)
10606 if (par_clauses != NULL)
10608 tree *c;
10609 for (c = par_clauses; *c ; )
10610 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10611 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10612 c = &OMP_CLAUSE_CHAIN (*c);
10613 else
10615 for (i = 0; i < collapse; i++)
10616 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10617 break;
10618 if (i == collapse)
10619 c = &OMP_CLAUSE_CHAIN (*c);
10620 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10622 error_at (loc,
10623 "iteration variable %qD should not be firstprivate",
10624 OMP_CLAUSE_DECL (*c));
10625 *c = OMP_CLAUSE_CHAIN (*c);
10627 else
10629 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10630 change it to shared (decl) in
10631 OMP_PARALLEL_CLAUSES. */
10632 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10633 OMP_CLAUSE_LASTPRIVATE);
10634 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10635 OMP_CLAUSE_CHAIN (l) = clauses;
10636 clauses = l;
10637 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10641 OMP_FOR_CLAUSES (stmt) = clauses;
10643 ret = stmt;
10645 pop_scopes:
10646 while (!for_block->is_empty ())
10648 /* FIXME diagnostics: LOC below should be the actual location of
10649 this particular for block. We need to build a list of
10650 locations to go along with FOR_BLOCK. */
10651 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
10652 add_stmt (stmt);
10654 release_tree_vector (for_block);
10655 return ret;
10658 /* OpenMP 2.5:
10659 #pragma omp for for-clause[optseq] new-line
10660 for-loop
10662 LOC is the location of the #pragma token.
10665 #define OMP_FOR_CLAUSE_MASK \
10666 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10667 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10668 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10669 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10670 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10671 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10672 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10673 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10675 static tree
10676 c_parser_omp_for (location_t loc, c_parser *parser)
10678 tree block, clauses, ret;
10680 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10681 "#pragma omp for");
10683 block = c_begin_compound_stmt (true);
10684 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10685 block = c_end_compound_stmt (loc, block, true);
10686 add_stmt (block);
10688 return ret;
10691 /* OpenMP 2.5:
10692 # pragma omp master new-line
10693 structured-block
10695 LOC is the location of the #pragma token.
10698 static tree
10699 c_parser_omp_master (location_t loc, c_parser *parser)
10701 c_parser_skip_to_pragma_eol (parser);
10702 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10705 /* OpenMP 2.5:
10706 # pragma omp ordered new-line
10707 structured-block
10709 LOC is the location of the #pragma itself.
10712 static tree
10713 c_parser_omp_ordered (location_t loc, c_parser *parser)
10715 c_parser_skip_to_pragma_eol (parser);
10716 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10719 /* OpenMP 2.5:
10721 section-scope:
10722 { section-sequence }
10724 section-sequence:
10725 section-directive[opt] structured-block
10726 section-sequence section-directive structured-block
10728 SECTIONS_LOC is the location of the #pragma omp sections. */
10730 static tree
10731 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10733 tree stmt, substmt;
10734 bool error_suppress = false;
10735 location_t loc;
10737 loc = c_parser_peek_token (parser)->location;
10738 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10740 /* Avoid skipping until the end of the block. */
10741 parser->error = false;
10742 return NULL_TREE;
10745 stmt = push_stmt_list ();
10747 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10749 substmt = push_stmt_list ();
10751 while (1)
10753 c_parser_statement (parser);
10755 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10756 break;
10757 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10758 break;
10759 if (c_parser_next_token_is (parser, CPP_EOF))
10760 break;
10763 substmt = pop_stmt_list (substmt);
10764 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10765 SET_EXPR_LOCATION (substmt, loc);
10766 add_stmt (substmt);
10769 while (1)
10771 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10772 break;
10773 if (c_parser_next_token_is (parser, CPP_EOF))
10774 break;
10776 loc = c_parser_peek_token (parser)->location;
10777 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10779 c_parser_consume_pragma (parser);
10780 c_parser_skip_to_pragma_eol (parser);
10781 error_suppress = false;
10783 else if (!error_suppress)
10785 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10786 error_suppress = true;
10789 substmt = c_parser_omp_structured_block (parser);
10790 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10791 SET_EXPR_LOCATION (substmt, loc);
10792 add_stmt (substmt);
10794 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10795 "expected %<#pragma omp section%> or %<}%>");
10797 substmt = pop_stmt_list (stmt);
10799 stmt = make_node (OMP_SECTIONS);
10800 SET_EXPR_LOCATION (stmt, sections_loc);
10801 TREE_TYPE (stmt) = void_type_node;
10802 OMP_SECTIONS_BODY (stmt) = substmt;
10804 return add_stmt (stmt);
10807 /* OpenMP 2.5:
10808 # pragma omp sections sections-clause[optseq] newline
10809 sections-scope
10811 LOC is the location of the #pragma token.
10814 #define OMP_SECTIONS_CLAUSE_MASK \
10815 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10816 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10817 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10818 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10819 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10821 static tree
10822 c_parser_omp_sections (location_t loc, c_parser *parser)
10824 tree block, clauses, ret;
10826 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10827 "#pragma omp sections");
10829 block = c_begin_compound_stmt (true);
10830 ret = c_parser_omp_sections_scope (loc, parser);
10831 if (ret)
10832 OMP_SECTIONS_CLAUSES (ret) = clauses;
10833 block = c_end_compound_stmt (loc, block, true);
10834 add_stmt (block);
10836 return ret;
10839 /* OpenMP 2.5:
10840 # pragma parallel parallel-clause new-line
10841 # pragma parallel for parallel-for-clause new-line
10842 # pragma parallel sections parallel-sections-clause new-line
10844 LOC is the location of the #pragma token.
10847 #define OMP_PARALLEL_CLAUSE_MASK \
10848 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10849 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10850 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10851 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10852 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10853 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10854 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10855 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10857 static tree
10858 c_parser_omp_parallel (location_t loc, c_parser *parser)
10860 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10861 const char *p_name = "#pragma omp parallel";
10862 tree stmt, clauses, par_clause, ws_clause, block;
10863 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10865 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10867 c_parser_consume_token (parser);
10868 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10869 p_name = "#pragma omp parallel for";
10870 mask |= OMP_FOR_CLAUSE_MASK;
10871 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10873 else if (c_parser_next_token_is (parser, CPP_NAME))
10875 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10876 if (strcmp (p, "sections") == 0)
10878 c_parser_consume_token (parser);
10879 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10880 p_name = "#pragma omp parallel sections";
10881 mask |= OMP_SECTIONS_CLAUSE_MASK;
10882 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10886 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10888 switch (p_kind)
10890 case PRAGMA_OMP_PARALLEL:
10891 block = c_begin_omp_parallel ();
10892 c_parser_statement (parser);
10893 stmt = c_finish_omp_parallel (loc, clauses, block);
10894 break;
10896 case PRAGMA_OMP_PARALLEL_FOR:
10897 block = c_begin_omp_parallel ();
10898 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10899 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10900 stmt = c_finish_omp_parallel (loc, par_clause, block);
10901 OMP_PARALLEL_COMBINED (stmt) = 1;
10902 break;
10904 case PRAGMA_OMP_PARALLEL_SECTIONS:
10905 block = c_begin_omp_parallel ();
10906 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10907 stmt = c_parser_omp_sections_scope (loc, parser);
10908 if (stmt)
10909 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10910 stmt = c_finish_omp_parallel (loc, par_clause, block);
10911 OMP_PARALLEL_COMBINED (stmt) = 1;
10912 break;
10914 default:
10915 gcc_unreachable ();
10918 return stmt;
10921 /* OpenMP 2.5:
10922 # pragma omp single single-clause[optseq] new-line
10923 structured-block
10925 LOC is the location of the #pragma.
10928 #define OMP_SINGLE_CLAUSE_MASK \
10929 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10930 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10931 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10932 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10934 static tree
10935 c_parser_omp_single (location_t loc, c_parser *parser)
10937 tree stmt = make_node (OMP_SINGLE);
10938 SET_EXPR_LOCATION (stmt, loc);
10939 TREE_TYPE (stmt) = void_type_node;
10941 OMP_SINGLE_CLAUSES (stmt)
10942 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10943 "#pragma omp single");
10944 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10946 return add_stmt (stmt);
10949 /* OpenMP 3.0:
10950 # pragma omp task task-clause[optseq] new-line
10952 LOC is the location of the #pragma.
10955 #define OMP_TASK_CLAUSE_MASK \
10956 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10957 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10958 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10959 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10960 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10961 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10962 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10963 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10965 static tree
10966 c_parser_omp_task (location_t loc, c_parser *parser)
10968 tree clauses, block;
10970 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10971 "#pragma omp task");
10973 block = c_begin_omp_task ();
10974 c_parser_statement (parser);
10975 return c_finish_omp_task (loc, clauses, block);
10978 /* OpenMP 3.0:
10979 # pragma omp taskwait new-line
10982 static void
10983 c_parser_omp_taskwait (c_parser *parser)
10985 location_t loc = c_parser_peek_token (parser)->location;
10986 c_parser_consume_pragma (parser);
10987 c_parser_skip_to_pragma_eol (parser);
10989 c_finish_omp_taskwait (loc);
10992 /* OpenMP 3.1:
10993 # pragma omp taskyield new-line
10996 static void
10997 c_parser_omp_taskyield (c_parser *parser)
10999 location_t loc = c_parser_peek_token (parser)->location;
11000 c_parser_consume_pragma (parser);
11001 c_parser_skip_to_pragma_eol (parser);
11003 c_finish_omp_taskyield (loc);
11006 /* Main entry point to parsing most OpenMP pragmas. */
11008 static void
11009 c_parser_omp_construct (c_parser *parser)
11011 enum pragma_kind p_kind;
11012 location_t loc;
11013 tree stmt;
11015 loc = c_parser_peek_token (parser)->location;
11016 p_kind = c_parser_peek_token (parser)->pragma_kind;
11017 c_parser_consume_pragma (parser);
11019 switch (p_kind)
11021 case PRAGMA_OMP_ATOMIC:
11022 c_parser_omp_atomic (loc, parser);
11023 return;
11024 case PRAGMA_OMP_CRITICAL:
11025 stmt = c_parser_omp_critical (loc, parser);
11026 break;
11027 case PRAGMA_OMP_FOR:
11028 stmt = c_parser_omp_for (loc, parser);
11029 break;
11030 case PRAGMA_OMP_MASTER:
11031 stmt = c_parser_omp_master (loc, parser);
11032 break;
11033 case PRAGMA_OMP_ORDERED:
11034 stmt = c_parser_omp_ordered (loc, parser);
11035 break;
11036 case PRAGMA_OMP_PARALLEL:
11037 stmt = c_parser_omp_parallel (loc, parser);
11038 break;
11039 case PRAGMA_OMP_SECTIONS:
11040 stmt = c_parser_omp_sections (loc, parser);
11041 break;
11042 case PRAGMA_OMP_SINGLE:
11043 stmt = c_parser_omp_single (loc, parser);
11044 break;
11045 case PRAGMA_OMP_TASK:
11046 stmt = c_parser_omp_task (loc, parser);
11047 break;
11048 default:
11049 gcc_unreachable ();
11052 if (stmt)
11053 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
11057 /* OpenMP 2.5:
11058 # pragma omp threadprivate (variable-list) */
11060 static void
11061 c_parser_omp_threadprivate (c_parser *parser)
11063 tree vars, t;
11064 location_t loc;
11066 c_parser_consume_pragma (parser);
11067 loc = c_parser_peek_token (parser)->location;
11068 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11070 /* Mark every variable in VARS to be assigned thread local storage. */
11071 for (t = vars; t; t = TREE_CHAIN (t))
11073 tree v = TREE_PURPOSE (t);
11075 /* FIXME diagnostics: Ideally we should keep individual
11076 locations for all the variables in the var list to make the
11077 following errors more precise. Perhaps
11078 c_parser_omp_var_list_parens() should construct a list of
11079 locations to go along with the var list. */
11081 /* If V had already been marked threadprivate, it doesn't matter
11082 whether it had been used prior to this point. */
11083 if (TREE_CODE (v) != VAR_DECL)
11084 error_at (loc, "%qD is not a variable", v);
11085 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
11086 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
11087 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
11088 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
11089 else if (TREE_TYPE (v) == error_mark_node)
11091 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
11092 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
11093 else
11095 if (! DECL_THREAD_LOCAL_P (v))
11097 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
11098 /* If rtl has been already set for this var, call
11099 make_decl_rtl once again, so that encode_section_info
11100 has a chance to look at the new decl flags. */
11101 if (DECL_RTL_SET_P (v))
11102 make_decl_rtl (v);
11104 C_DECL_THREADPRIVATE_P (v) = 1;
11108 c_parser_skip_to_pragma_eol (parser);
11111 /* Parse a transaction attribute (GCC Extension).
11113 transaction-attribute:
11114 attributes
11115 [ [ any-word ] ]
11117 The transactional memory language description is written for C++,
11118 and uses the C++0x attribute syntax. For compatibility, allow the
11119 bracket style for transactions in C as well. */
11121 static tree
11122 c_parser_transaction_attributes (c_parser *parser)
11124 tree attr_name, attr = NULL;
11126 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
11127 return c_parser_attributes (parser);
11129 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11130 return NULL_TREE;
11131 c_parser_consume_token (parser);
11132 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
11133 goto error1;
11135 attr_name = c_parser_attribute_any_word (parser);
11136 if (attr_name)
11138 c_parser_consume_token (parser);
11139 attr = build_tree_list (attr_name, NULL_TREE);
11141 else
11142 c_parser_error (parser, "expected identifier");
11144 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11145 error1:
11146 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
11147 return attr;
11150 /* Parse a __transaction_atomic or __transaction_relaxed statement
11151 (GCC Extension).
11153 transaction-statement:
11154 __transaction_atomic transaction-attribute[opt] compound-statement
11155 __transaction_relaxed compound-statement
11157 Note that the only valid attribute is: "outer".
11160 static tree
11161 c_parser_transaction (c_parser *parser, enum rid keyword)
11163 unsigned int old_in = parser->in_transaction;
11164 unsigned int this_in = 1, new_in;
11165 location_t loc = c_parser_peek_token (parser)->location;
11166 tree stmt, attrs;
11168 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11169 || keyword == RID_TRANSACTION_RELAXED)
11170 && c_parser_next_token_is_keyword (parser, keyword));
11171 c_parser_consume_token (parser);
11173 if (keyword == RID_TRANSACTION_RELAXED)
11174 this_in |= TM_STMT_ATTR_RELAXED;
11175 else
11177 attrs = c_parser_transaction_attributes (parser);
11178 if (attrs)
11179 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
11182 /* Keep track if we're in the lexical scope of an outer transaction. */
11183 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
11185 parser->in_transaction = new_in;
11186 stmt = c_parser_compound_statement (parser);
11187 parser->in_transaction = old_in;
11189 if (flag_tm)
11190 stmt = c_finish_transaction (loc, stmt, this_in);
11191 else
11192 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11193 "%<__transaction_atomic%> without transactional memory support enabled"
11194 : "%<__transaction_relaxed %> "
11195 "without transactional memory support enabled"));
11197 return stmt;
11200 /* Parse a __transaction_atomic or __transaction_relaxed expression
11201 (GCC Extension).
11203 transaction-expression:
11204 __transaction_atomic ( expression )
11205 __transaction_relaxed ( expression )
11208 static struct c_expr
11209 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
11211 struct c_expr ret;
11212 unsigned int old_in = parser->in_transaction;
11213 unsigned int this_in = 1;
11214 location_t loc = c_parser_peek_token (parser)->location;
11215 tree attrs;
11217 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
11218 || keyword == RID_TRANSACTION_RELAXED)
11219 && c_parser_next_token_is_keyword (parser, keyword));
11220 c_parser_consume_token (parser);
11222 if (keyword == RID_TRANSACTION_RELAXED)
11223 this_in |= TM_STMT_ATTR_RELAXED;
11224 else
11226 attrs = c_parser_transaction_attributes (parser);
11227 if (attrs)
11228 this_in |= parse_tm_stmt_attr (attrs, 0);
11231 parser->in_transaction = this_in;
11232 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11234 tree expr = c_parser_expression (parser).value;
11235 ret.original_type = TREE_TYPE (expr);
11236 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
11237 if (this_in & TM_STMT_ATTR_RELAXED)
11238 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
11239 SET_EXPR_LOCATION (ret.value, loc);
11240 ret.original_code = TRANSACTION_EXPR;
11241 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
11244 goto error;
11247 else
11249 error:
11250 ret.value = error_mark_node;
11251 ret.original_code = ERROR_MARK;
11252 ret.original_type = NULL;
11254 parser->in_transaction = old_in;
11256 if (!flag_tm)
11257 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
11258 "%<__transaction_atomic%> without transactional memory support enabled"
11259 : "%<__transaction_relaxed %> "
11260 "without transactional memory support enabled"));
11262 return ret;
11265 /* Parse a __transaction_cancel statement (GCC Extension).
11267 transaction-cancel-statement:
11268 __transaction_cancel transaction-attribute[opt] ;
11270 Note that the only valid attribute is "outer".
11273 static tree
11274 c_parser_transaction_cancel(c_parser *parser)
11276 location_t loc = c_parser_peek_token (parser)->location;
11277 tree attrs;
11278 bool is_outer = false;
11280 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
11281 c_parser_consume_token (parser);
11283 attrs = c_parser_transaction_attributes (parser);
11284 if (attrs)
11285 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
11287 if (!flag_tm)
11289 error_at (loc, "%<__transaction_cancel%> without "
11290 "transactional memory support enabled");
11291 goto ret_error;
11293 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
11295 error_at (loc, "%<__transaction_cancel%> within a "
11296 "%<__transaction_relaxed%>");
11297 goto ret_error;
11299 else if (is_outer)
11301 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
11302 && !is_tm_may_cancel_outer (current_function_decl))
11304 error_at (loc, "outer %<__transaction_cancel%> not "
11305 "within outer %<__transaction_atomic%>");
11306 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
11307 goto ret_error;
11310 else if (parser->in_transaction == 0)
11312 error_at (loc, "%<__transaction_cancel%> not within "
11313 "%<__transaction_atomic%>");
11314 goto ret_error;
11317 return add_stmt (build_tm_abort_call (loc, is_outer));
11319 ret_error:
11320 return build1 (NOP_EXPR, void_type_node, error_mark_node);
11323 /* Parse a single source file. */
11325 void
11326 c_parse_file (void)
11328 /* Use local storage to begin. If the first token is a pragma, parse it.
11329 If it is #pragma GCC pch_preprocess, then this will load a PCH file
11330 which will cause garbage collection. */
11331 c_parser tparser;
11333 memset (&tparser, 0, sizeof tparser);
11334 the_parser = &tparser;
11336 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
11337 c_parser_pragma_pch_preprocess (&tparser);
11339 the_parser = ggc_alloc_c_parser ();
11340 *the_parser = tparser;
11342 /* Initialize EH, if we've been told to do so. */
11343 if (flag_exceptions)
11344 using_eh_for_cleanups ();
11346 c_parser_translation_unit (the_parser);
11347 the_parser = NULL;
11350 #include "gt-c-c-parser.h"