2014-10-31 Olivier Hainque <hainque@adacore.com>
[official-gcc.git] / gcc / c / c-parser.c
blobd316216cbe1dd29ab430d554db98e43542c771de
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2014 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 "stringpool.h"
44 #include "attribs.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "trans-mem.h"
48 #include "langhooks.h"
49 #include "input.h"
50 #include "cpplib.h"
51 #include "timevar.h"
52 #include "c-family/c-pragma.h"
53 #include "c-tree.h"
54 #include "c-lang.h"
55 #include "flags.h"
56 #include "ggc.h"
57 #include "c-family/c-common.h"
58 #include "c-family/c-objc.h"
59 #include "vec.h"
60 #include "target.h"
61 #include "hash-map.h"
62 #include "is-a.h"
63 #include "plugin-api.h"
64 #include "hashtab.h"
65 #include "hash-set.h"
66 #include "machmode.h"
67 #include "hard-reg-set.h"
68 #include "function.h"
69 #include "ipa-ref.h"
70 #include "cgraph.h"
71 #include "plugin.h"
72 #include "omp-low.h"
73 #include "builtins.h"
76 /* Initialization routine for this file. */
78 void
79 c_parse_init (void)
81 /* The only initialization required is of the reserved word
82 identifiers. */
83 unsigned int i;
84 tree id;
85 int mask = 0;
87 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
88 the c_token structure. */
89 gcc_assert (RID_MAX <= 255);
91 mask |= D_CXXONLY;
92 if (!flag_isoc99)
93 mask |= D_C99;
94 if (flag_no_asm)
96 mask |= D_ASM | D_EXT;
97 if (!flag_isoc99)
98 mask |= D_EXT89;
100 if (!c_dialect_objc ())
101 mask |= D_OBJC | D_CXX_OBJC;
103 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
104 for (i = 0; i < num_c_common_reswords; i++)
106 /* If a keyword is disabled, do not enter it into the table
107 and so create a canonical spelling that isn't a keyword. */
108 if (c_common_reswords[i].disable & mask)
110 if (warn_cxx_compat
111 && (c_common_reswords[i].disable & D_CXXWARN))
113 id = get_identifier (c_common_reswords[i].word);
114 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
115 C_IS_RESERVED_WORD (id) = 1;
117 continue;
120 id = get_identifier (c_common_reswords[i].word);
121 C_SET_RID_CODE (id, c_common_reswords[i].rid);
122 C_IS_RESERVED_WORD (id) = 1;
123 ridpointers [(int) c_common_reswords[i].rid] = id;
126 for (i = 0; i < NUM_INT_N_ENTS; i++)
128 /* We always create the symbols but they aren't always supported. */
129 char name[50];
130 sprintf (name, "__int%d", int_n_data[i].bitsize);
131 id = get_identifier (xstrdup (name));
132 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
133 C_IS_RESERVED_WORD (id) = 1;
137 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
138 and the C parser. Unlike the C++ lexer, the parser structure
139 stores the lexer information instead of using a separate structure.
140 Identifiers are separated into ordinary identifiers, type names,
141 keywords and some other Objective-C types of identifiers, and some
142 look-ahead is maintained.
144 ??? It might be a good idea to lex the whole file up front (as for
145 C++). It would then be possible to share more of the C and C++
146 lexer code, if desired. */
148 /* More information about the type of a CPP_NAME token. */
149 typedef enum c_id_kind {
150 /* An ordinary identifier. */
151 C_ID_ID,
152 /* An identifier declared as a typedef name. */
153 C_ID_TYPENAME,
154 /* An identifier declared as an Objective-C class name. */
155 C_ID_CLASSNAME,
156 /* An address space identifier. */
157 C_ID_ADDRSPACE,
158 /* Not an identifier. */
159 C_ID_NONE
160 } c_id_kind;
162 /* A single C token after string literal concatenation and conversion
163 of preprocessing tokens to tokens. */
164 typedef struct GTY (()) c_token {
165 /* The kind of token. */
166 ENUM_BITFIELD (cpp_ttype) type : 8;
167 /* If this token is a CPP_NAME, this value indicates whether also
168 declared as some kind of type. Otherwise, it is C_ID_NONE. */
169 ENUM_BITFIELD (c_id_kind) id_kind : 8;
170 /* If this token is a keyword, this value indicates which keyword.
171 Otherwise, this value is RID_MAX. */
172 ENUM_BITFIELD (rid) keyword : 8;
173 /* If this token is a CPP_PRAGMA, this indicates the pragma that
174 was seen. Otherwise it is PRAGMA_NONE. */
175 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
176 /* The location at which this token was found. */
177 location_t location;
178 /* The value associated with this token, if any. */
179 tree value;
180 } c_token;
182 /* A parser structure recording information about the state and
183 context of parsing. Includes lexer information with up to two
184 tokens of look-ahead; more are not needed for C. */
185 typedef struct GTY(()) c_parser {
186 /* The look-ahead tokens. */
187 c_token * GTY((skip)) tokens;
188 /* Buffer for look-ahead tokens. */
189 c_token tokens_buf[2];
190 /* How many look-ahead tokens are available (0, 1 or 2, or
191 more if parsing from pre-lexed tokens). */
192 unsigned int tokens_avail;
193 /* True if a syntax error is being recovered from; false otherwise.
194 c_parser_error sets this flag. It should clear this flag when
195 enough tokens have been consumed to recover from the error. */
196 BOOL_BITFIELD error : 1;
197 /* True if we're processing a pragma, and shouldn't automatically
198 consume CPP_PRAGMA_EOL. */
199 BOOL_BITFIELD in_pragma : 1;
200 /* True if we're parsing the outermost block of an if statement. */
201 BOOL_BITFIELD in_if_block : 1;
202 /* True if we want to lex an untranslated string. */
203 BOOL_BITFIELD lex_untranslated_string : 1;
205 /* Objective-C specific parser/lexer information. */
207 /* True if we are in a context where the Objective-C "PQ" keywords
208 are considered keywords. */
209 BOOL_BITFIELD objc_pq_context : 1;
210 /* True if we are parsing a (potential) Objective-C foreach
211 statement. This is set to true after we parsed 'for (' and while
212 we wait for 'in' or ';' to decide if it's a standard C for loop or an
213 Objective-C foreach loop. */
214 BOOL_BITFIELD objc_could_be_foreach_context : 1;
215 /* The following flag is needed to contextualize Objective-C lexical
216 analysis. In some cases (e.g., 'int NSObject;'), it is
217 undesirable to bind an identifier to an Objective-C class, even
218 if a class with that name exists. */
219 BOOL_BITFIELD objc_need_raw_identifier : 1;
220 /* Nonzero if we're processing a __transaction statement. The value
221 is 1 | TM_STMT_ATTR_*. */
222 unsigned int in_transaction : 4;
223 /* True if we are in a context where the Objective-C "Property attribute"
224 keywords are valid. */
225 BOOL_BITFIELD objc_property_attr_context : 1;
227 /* Cilk Plus specific parser/lexer information. */
229 /* Buffer to hold all the tokens from parsing the vector attribute for the
230 SIMD-enabled functions (formerly known as elemental functions). */
231 vec <c_token, va_gc> *cilk_simd_fn_tokens;
232 } c_parser;
235 /* The actual parser and external interface. ??? Does this need to be
236 garbage-collected? */
238 static GTY (()) c_parser *the_parser;
240 /* Read in and lex a single token, storing it in *TOKEN. */
242 static void
243 c_lex_one_token (c_parser *parser, c_token *token)
245 timevar_push (TV_LEX);
247 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
248 (parser->lex_untranslated_string
249 ? C_LEX_STRING_NO_TRANSLATE : 0));
250 token->id_kind = C_ID_NONE;
251 token->keyword = RID_MAX;
252 token->pragma_kind = PRAGMA_NONE;
254 switch (token->type)
256 case CPP_NAME:
258 tree decl;
260 bool objc_force_identifier = parser->objc_need_raw_identifier;
261 if (c_dialect_objc ())
262 parser->objc_need_raw_identifier = false;
264 if (C_IS_RESERVED_WORD (token->value))
266 enum rid rid_code = C_RID_CODE (token->value);
268 if (rid_code == RID_CXX_COMPAT_WARN)
270 warning_at (token->location,
271 OPT_Wc___compat,
272 "identifier %qE conflicts with C++ keyword",
273 token->value);
275 else if (rid_code >= RID_FIRST_ADDR_SPACE
276 && rid_code <= RID_LAST_ADDR_SPACE)
278 token->id_kind = C_ID_ADDRSPACE;
279 token->keyword = rid_code;
280 break;
282 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
284 /* We found an Objective-C "pq" keyword (in, out,
285 inout, bycopy, byref, oneway). They need special
286 care because the interpretation depends on the
287 context. */
288 if (parser->objc_pq_context)
290 token->type = CPP_KEYWORD;
291 token->keyword = rid_code;
292 break;
294 else if (parser->objc_could_be_foreach_context
295 && rid_code == RID_IN)
297 /* We are in Objective-C, inside a (potential)
298 foreach context (which means after having
299 parsed 'for (', but before having parsed ';'),
300 and we found 'in'. We consider it the keyword
301 which terminates the declaration at the
302 beginning of a foreach-statement. Note that
303 this means you can't use 'in' for anything else
304 in that context; in particular, in Objective-C
305 you can't use 'in' as the name of the running
306 variable in a C for loop. We could potentially
307 try to add code here to disambiguate, but it
308 seems a reasonable limitation. */
309 token->type = CPP_KEYWORD;
310 token->keyword = rid_code;
311 break;
313 /* Else, "pq" keywords outside of the "pq" context are
314 not keywords, and we fall through to the code for
315 normal tokens. */
317 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
319 /* We found an Objective-C "property attribute"
320 keyword (getter, setter, readonly, etc). These are
321 only valid in the property context. */
322 if (parser->objc_property_attr_context)
324 token->type = CPP_KEYWORD;
325 token->keyword = rid_code;
326 break;
328 /* Else they are not special keywords.
331 else if (c_dialect_objc ()
332 && (OBJC_IS_AT_KEYWORD (rid_code)
333 || OBJC_IS_CXX_KEYWORD (rid_code)))
335 /* We found one of the Objective-C "@" keywords (defs,
336 selector, synchronized, etc) or one of the
337 Objective-C "cxx" keywords (class, private,
338 protected, public, try, catch, throw) without a
339 preceding '@' sign. Do nothing and fall through to
340 the code for normal tokens (in C++ we would still
341 consider the CXX ones keywords, but not in C). */
344 else
346 token->type = CPP_KEYWORD;
347 token->keyword = rid_code;
348 break;
352 decl = lookup_name (token->value);
353 if (decl)
355 if (TREE_CODE (decl) == TYPE_DECL)
357 token->id_kind = C_ID_TYPENAME;
358 break;
361 else if (c_dialect_objc ())
363 tree objc_interface_decl = objc_is_class_name (token->value);
364 /* Objective-C class names are in the same namespace as
365 variables and typedefs, and hence are shadowed by local
366 declarations. */
367 if (objc_interface_decl
368 && (!objc_force_identifier || global_bindings_p ()))
370 token->value = objc_interface_decl;
371 token->id_kind = C_ID_CLASSNAME;
372 break;
375 token->id_kind = C_ID_ID;
377 break;
378 case CPP_AT_NAME:
379 /* This only happens in Objective-C; it must be a keyword. */
380 token->type = CPP_KEYWORD;
381 switch (C_RID_CODE (token->value))
383 /* Replace 'class' with '@class', 'private' with '@private',
384 etc. This prevents confusion with the C++ keyword
385 'class', and makes the tokens consistent with other
386 Objective-C 'AT' keywords. For example '@class' is
387 reported as RID_AT_CLASS which is consistent with
388 '@synchronized', which is reported as
389 RID_AT_SYNCHRONIZED.
391 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
392 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
393 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
394 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
395 case RID_THROW: token->keyword = RID_AT_THROW; break;
396 case RID_TRY: token->keyword = RID_AT_TRY; break;
397 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
398 default: token->keyword = C_RID_CODE (token->value);
400 break;
401 case CPP_COLON:
402 case CPP_COMMA:
403 case CPP_CLOSE_PAREN:
404 case CPP_SEMICOLON:
405 /* These tokens may affect the interpretation of any identifiers
406 following, if doing Objective-C. */
407 if (c_dialect_objc ())
408 parser->objc_need_raw_identifier = false;
409 break;
410 case CPP_PRAGMA:
411 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
412 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
413 token->value = NULL;
414 break;
415 default:
416 break;
418 timevar_pop (TV_LEX);
421 /* Return a pointer to the next token from PARSER, reading it in if
422 necessary. */
424 static inline c_token *
425 c_parser_peek_token (c_parser *parser)
427 if (parser->tokens_avail == 0)
429 c_lex_one_token (parser, &parser->tokens[0]);
430 parser->tokens_avail = 1;
432 return &parser->tokens[0];
435 /* Return true if the next token from PARSER has the indicated
436 TYPE. */
438 static inline bool
439 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
441 return c_parser_peek_token (parser)->type == type;
444 /* Return true if the next token from PARSER does not have the
445 indicated TYPE. */
447 static inline bool
448 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
450 return !c_parser_next_token_is (parser, type);
453 /* Return true if the next token from PARSER is the indicated
454 KEYWORD. */
456 static inline bool
457 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
459 return c_parser_peek_token (parser)->keyword == keyword;
462 /* Return a pointer to the next-but-one token from PARSER, reading it
463 in if necessary. The next token is already read in. */
465 static c_token *
466 c_parser_peek_2nd_token (c_parser *parser)
468 if (parser->tokens_avail >= 2)
469 return &parser->tokens[1];
470 gcc_assert (parser->tokens_avail == 1);
471 gcc_assert (parser->tokens[0].type != CPP_EOF);
472 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
473 c_lex_one_token (parser, &parser->tokens[1]);
474 parser->tokens_avail = 2;
475 return &parser->tokens[1];
478 /* Return true if TOKEN can start a type name,
479 false otherwise. */
480 static bool
481 c_token_starts_typename (c_token *token)
483 switch (token->type)
485 case CPP_NAME:
486 switch (token->id_kind)
488 case C_ID_ID:
489 return false;
490 case C_ID_ADDRSPACE:
491 return true;
492 case C_ID_TYPENAME:
493 return true;
494 case C_ID_CLASSNAME:
495 gcc_assert (c_dialect_objc ());
496 return true;
497 default:
498 gcc_unreachable ();
500 case CPP_KEYWORD:
501 switch (token->keyword)
503 case RID_UNSIGNED:
504 case RID_LONG:
505 case RID_SHORT:
506 case RID_SIGNED:
507 case RID_COMPLEX:
508 case RID_INT:
509 case RID_CHAR:
510 case RID_FLOAT:
511 case RID_DOUBLE:
512 case RID_VOID:
513 case RID_DFLOAT32:
514 case RID_DFLOAT64:
515 case RID_DFLOAT128:
516 case RID_BOOL:
517 case RID_ENUM:
518 case RID_STRUCT:
519 case RID_UNION:
520 case RID_TYPEOF:
521 case RID_CONST:
522 case RID_ATOMIC:
523 case RID_VOLATILE:
524 case RID_RESTRICT:
525 case RID_ATTRIBUTE:
526 case RID_FRACT:
527 case RID_ACCUM:
528 case RID_SAT:
529 case RID_AUTO_TYPE:
530 return true;
531 default:
532 if (token->keyword >= RID_FIRST_INT_N
533 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
534 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
535 return true;
536 return false;
538 case CPP_LESS:
539 if (c_dialect_objc ())
540 return true;
541 return false;
542 default:
543 return false;
547 enum c_lookahead_kind {
548 /* Always treat unknown identifiers as typenames. */
549 cla_prefer_type,
551 /* Could be parsing a nonabstract declarator. Only treat an identifier
552 as a typename if followed by another identifier or a star. */
553 cla_nonabstract_decl,
555 /* Never treat identifiers as typenames. */
556 cla_prefer_id
559 /* Return true if the next token from PARSER can start a type name,
560 false otherwise. LA specifies how to do lookahead in order to
561 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
563 static inline bool
564 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
566 c_token *token = c_parser_peek_token (parser);
567 if (c_token_starts_typename (token))
568 return true;
570 /* Try a bit harder to detect an unknown typename. */
571 if (la != cla_prefer_id
572 && token->type == CPP_NAME
573 && token->id_kind == C_ID_ID
575 /* Do not try too hard when we could have "object in array". */
576 && !parser->objc_could_be_foreach_context
578 && (la == cla_prefer_type
579 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
580 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
582 /* Only unknown identifiers. */
583 && !lookup_name (token->value))
584 return true;
586 return false;
589 /* Return true if TOKEN is a type qualifier, false otherwise. */
590 static bool
591 c_token_is_qualifier (c_token *token)
593 switch (token->type)
595 case CPP_NAME:
596 switch (token->id_kind)
598 case C_ID_ADDRSPACE:
599 return true;
600 default:
601 return false;
603 case CPP_KEYWORD:
604 switch (token->keyword)
606 case RID_CONST:
607 case RID_VOLATILE:
608 case RID_RESTRICT:
609 case RID_ATTRIBUTE:
610 case RID_ATOMIC:
611 return true;
612 default:
613 return false;
615 case CPP_LESS:
616 return false;
617 default:
618 gcc_unreachable ();
622 /* Return true if the next token from PARSER is a type qualifier,
623 false otherwise. */
624 static inline bool
625 c_parser_next_token_is_qualifier (c_parser *parser)
627 c_token *token = c_parser_peek_token (parser);
628 return c_token_is_qualifier (token);
631 /* Return true if TOKEN can start declaration specifiers, false
632 otherwise. */
633 static bool
634 c_token_starts_declspecs (c_token *token)
636 switch (token->type)
638 case CPP_NAME:
639 switch (token->id_kind)
641 case C_ID_ID:
642 return false;
643 case C_ID_ADDRSPACE:
644 return true;
645 case C_ID_TYPENAME:
646 return true;
647 case C_ID_CLASSNAME:
648 gcc_assert (c_dialect_objc ());
649 return true;
650 default:
651 gcc_unreachable ();
653 case CPP_KEYWORD:
654 switch (token->keyword)
656 case RID_STATIC:
657 case RID_EXTERN:
658 case RID_REGISTER:
659 case RID_TYPEDEF:
660 case RID_INLINE:
661 case RID_NORETURN:
662 case RID_AUTO:
663 case RID_THREAD:
664 case RID_UNSIGNED:
665 case RID_LONG:
666 case RID_SHORT:
667 case RID_SIGNED:
668 case RID_COMPLEX:
669 case RID_INT:
670 case RID_CHAR:
671 case RID_FLOAT:
672 case RID_DOUBLE:
673 case RID_VOID:
674 case RID_DFLOAT32:
675 case RID_DFLOAT64:
676 case RID_DFLOAT128:
677 case RID_BOOL:
678 case RID_ENUM:
679 case RID_STRUCT:
680 case RID_UNION:
681 case RID_TYPEOF:
682 case RID_CONST:
683 case RID_VOLATILE:
684 case RID_RESTRICT:
685 case RID_ATTRIBUTE:
686 case RID_FRACT:
687 case RID_ACCUM:
688 case RID_SAT:
689 case RID_ALIGNAS:
690 case RID_ATOMIC:
691 case RID_AUTO_TYPE:
692 return true;
693 default:
694 if (token->keyword >= RID_FIRST_INT_N
695 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
696 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
697 return true;
698 return false;
700 case CPP_LESS:
701 if (c_dialect_objc ())
702 return true;
703 return false;
704 default:
705 return false;
710 /* Return true if TOKEN can start declaration specifiers or a static
711 assertion, false otherwise. */
712 static bool
713 c_token_starts_declaration (c_token *token)
715 if (c_token_starts_declspecs (token)
716 || token->keyword == RID_STATIC_ASSERT)
717 return true;
718 else
719 return false;
722 /* Return true if the next token from PARSER can start declaration
723 specifiers, false otherwise. */
724 static inline bool
725 c_parser_next_token_starts_declspecs (c_parser *parser)
727 c_token *token = c_parser_peek_token (parser);
729 /* In Objective-C, a classname normally starts a declspecs unless it
730 is immediately followed by a dot. In that case, it is the
731 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
732 setter/getter on the class. c_token_starts_declspecs() can't
733 differentiate between the two cases because it only checks the
734 current token, so we have a special check here. */
735 if (c_dialect_objc ()
736 && token->type == CPP_NAME
737 && token->id_kind == C_ID_CLASSNAME
738 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
739 return false;
741 return c_token_starts_declspecs (token);
744 /* Return true if the next tokens from PARSER can start declaration
745 specifiers or a static assertion, false otherwise. */
746 static inline bool
747 c_parser_next_tokens_start_declaration (c_parser *parser)
749 c_token *token = c_parser_peek_token (parser);
751 /* Same as above. */
752 if (c_dialect_objc ()
753 && token->type == CPP_NAME
754 && token->id_kind == C_ID_CLASSNAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
756 return false;
758 /* Labels do not start declarations. */
759 if (token->type == CPP_NAME
760 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
761 return false;
763 if (c_token_starts_declaration (token))
764 return true;
766 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
767 return true;
769 return false;
772 /* Consume the next token from PARSER. */
774 static void
775 c_parser_consume_token (c_parser *parser)
777 gcc_assert (parser->tokens_avail >= 1);
778 gcc_assert (parser->tokens[0].type != CPP_EOF);
779 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
780 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
781 if (parser->tokens != &parser->tokens_buf[0])
782 parser->tokens++;
783 else if (parser->tokens_avail == 2)
784 parser->tokens[0] = parser->tokens[1];
785 parser->tokens_avail--;
788 /* Expect the current token to be a #pragma. Consume it and remember
789 that we've begun parsing a pragma. */
791 static void
792 c_parser_consume_pragma (c_parser *parser)
794 gcc_assert (!parser->in_pragma);
795 gcc_assert (parser->tokens_avail >= 1);
796 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
797 if (parser->tokens != &parser->tokens_buf[0])
798 parser->tokens++;
799 else if (parser->tokens_avail == 2)
800 parser->tokens[0] = parser->tokens[1];
801 parser->tokens_avail--;
802 parser->in_pragma = true;
805 /* Update the global input_location from TOKEN. */
806 static inline void
807 c_parser_set_source_position_from_token (c_token *token)
809 if (token->type != CPP_EOF)
811 input_location = token->location;
815 /* Issue a diagnostic of the form
816 FILE:LINE: MESSAGE before TOKEN
817 where TOKEN is the next token in the input stream of PARSER.
818 MESSAGE (specified by the caller) is usually of the form "expected
819 OTHER-TOKEN".
821 Do not issue a diagnostic if still recovering from an error.
823 ??? This is taken from the C++ parser, but building up messages in
824 this way is not i18n-friendly and some other approach should be
825 used. */
827 static void
828 c_parser_error (c_parser *parser, const char *gmsgid)
830 c_token *token = c_parser_peek_token (parser);
831 if (parser->error)
832 return;
833 parser->error = true;
834 if (!gmsgid)
835 return;
836 /* This diagnostic makes more sense if it is tagged to the line of
837 the token we just peeked at. */
838 c_parser_set_source_position_from_token (token);
839 c_parse_error (gmsgid,
840 /* Because c_parse_error does not understand
841 CPP_KEYWORD, keywords are treated like
842 identifiers. */
843 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
844 /* ??? The C parser does not save the cpp flags of a
845 token, we need to pass 0 here and we will not get
846 the source spelling of some tokens but rather the
847 canonical spelling. */
848 token->value, /*flags=*/0);
851 /* If the next token is of the indicated TYPE, consume it. Otherwise,
852 issue the error MSGID. If MSGID is NULL then a message has already
853 been produced and no message will be produced this time. Returns
854 true if found, false otherwise. */
856 static bool
857 c_parser_require (c_parser *parser,
858 enum cpp_ttype type,
859 const char *msgid)
861 if (c_parser_next_token_is (parser, type))
863 c_parser_consume_token (parser);
864 return true;
866 else
868 c_parser_error (parser, msgid);
869 return false;
873 /* If the next token is the indicated keyword, consume it. Otherwise,
874 issue the error MSGID. Returns true if found, false otherwise. */
876 static bool
877 c_parser_require_keyword (c_parser *parser,
878 enum rid keyword,
879 const char *msgid)
881 if (c_parser_next_token_is_keyword (parser, keyword))
883 c_parser_consume_token (parser);
884 return true;
886 else
888 c_parser_error (parser, msgid);
889 return false;
893 /* Like c_parser_require, except that tokens will be skipped until the
894 desired token is found. An error message is still produced if the
895 next token is not as expected. If MSGID is NULL then a message has
896 already been produced and no message will be produced this
897 time. */
899 static void
900 c_parser_skip_until_found (c_parser *parser,
901 enum cpp_ttype type,
902 const char *msgid)
904 unsigned nesting_depth = 0;
906 if (c_parser_require (parser, type, msgid))
907 return;
909 /* Skip tokens until the desired token is found. */
910 while (true)
912 /* Peek at the next token. */
913 c_token *token = c_parser_peek_token (parser);
914 /* If we've reached the token we want, consume it and stop. */
915 if (token->type == type && !nesting_depth)
917 c_parser_consume_token (parser);
918 break;
921 /* If we've run out of tokens, stop. */
922 if (token->type == CPP_EOF)
923 return;
924 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
925 return;
926 if (token->type == CPP_OPEN_BRACE
927 || token->type == CPP_OPEN_PAREN
928 || token->type == CPP_OPEN_SQUARE)
929 ++nesting_depth;
930 else if (token->type == CPP_CLOSE_BRACE
931 || token->type == CPP_CLOSE_PAREN
932 || token->type == CPP_CLOSE_SQUARE)
934 if (nesting_depth-- == 0)
935 break;
937 /* Consume this token. */
938 c_parser_consume_token (parser);
940 parser->error = false;
943 /* Skip tokens until the end of a parameter is found, but do not
944 consume the comma, semicolon or closing delimiter. */
946 static void
947 c_parser_skip_to_end_of_parameter (c_parser *parser)
949 unsigned nesting_depth = 0;
951 while (true)
953 c_token *token = c_parser_peek_token (parser);
954 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
955 && !nesting_depth)
956 break;
957 /* If we've run out of tokens, stop. */
958 if (token->type == CPP_EOF)
959 return;
960 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
961 return;
962 if (token->type == CPP_OPEN_BRACE
963 || token->type == CPP_OPEN_PAREN
964 || token->type == CPP_OPEN_SQUARE)
965 ++nesting_depth;
966 else if (token->type == CPP_CLOSE_BRACE
967 || token->type == CPP_CLOSE_PAREN
968 || token->type == CPP_CLOSE_SQUARE)
970 if (nesting_depth-- == 0)
971 break;
973 /* Consume this token. */
974 c_parser_consume_token (parser);
976 parser->error = false;
979 /* Expect to be at the end of the pragma directive and consume an
980 end of line marker. */
982 static void
983 c_parser_skip_to_pragma_eol (c_parser *parser)
985 gcc_assert (parser->in_pragma);
986 parser->in_pragma = false;
988 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
989 while (true)
991 c_token *token = c_parser_peek_token (parser);
992 if (token->type == CPP_EOF)
993 break;
994 if (token->type == CPP_PRAGMA_EOL)
996 c_parser_consume_token (parser);
997 break;
999 c_parser_consume_token (parser);
1002 parser->error = false;
1005 /* Skip tokens until we have consumed an entire block, or until we
1006 have consumed a non-nested ';'. */
1008 static void
1009 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1011 unsigned nesting_depth = 0;
1012 bool save_error = parser->error;
1014 while (true)
1016 c_token *token;
1018 /* Peek at the next token. */
1019 token = c_parser_peek_token (parser);
1021 switch (token->type)
1023 case CPP_EOF:
1024 return;
1026 case CPP_PRAGMA_EOL:
1027 if (parser->in_pragma)
1028 return;
1029 break;
1031 case CPP_SEMICOLON:
1032 /* If the next token is a ';', we have reached the
1033 end of the statement. */
1034 if (!nesting_depth)
1036 /* Consume the ';'. */
1037 c_parser_consume_token (parser);
1038 goto finished;
1040 break;
1042 case CPP_CLOSE_BRACE:
1043 /* If the next token is a non-nested '}', then we have
1044 reached the end of the current block. */
1045 if (nesting_depth == 0 || --nesting_depth == 0)
1047 c_parser_consume_token (parser);
1048 goto finished;
1050 break;
1052 case CPP_OPEN_BRACE:
1053 /* If it the next token is a '{', then we are entering a new
1054 block. Consume the entire block. */
1055 ++nesting_depth;
1056 break;
1058 case CPP_PRAGMA:
1059 /* If we see a pragma, consume the whole thing at once. We
1060 have some safeguards against consuming pragmas willy-nilly.
1061 Normally, we'd expect to be here with parser->error set,
1062 which disables these safeguards. But it's possible to get
1063 here for secondary error recovery, after parser->error has
1064 been cleared. */
1065 c_parser_consume_pragma (parser);
1066 c_parser_skip_to_pragma_eol (parser);
1067 parser->error = save_error;
1068 continue;
1070 default:
1071 break;
1074 c_parser_consume_token (parser);
1077 finished:
1078 parser->error = false;
1081 /* CPP's options (initialized by c-opts.c). */
1082 extern cpp_options *cpp_opts;
1084 /* Save the warning flags which are controlled by __extension__. */
1086 static inline int
1087 disable_extension_diagnostics (void)
1089 int ret = (pedantic
1090 | (warn_pointer_arith << 1)
1091 | (warn_traditional << 2)
1092 | (flag_iso << 3)
1093 | (warn_long_long << 4)
1094 | (warn_cxx_compat << 5)
1095 | (warn_overlength_strings << 6)
1096 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1097 play tricks to properly restore it. */
1098 | ((warn_c90_c99_compat == 1) << 7)
1099 | ((warn_c90_c99_compat == -1) << 8)
1100 /* Similarly for warn_c99_c11_compat. */
1101 | ((warn_c99_c11_compat == 1) << 9)
1102 | ((warn_c99_c11_compat == -1) << 10)
1104 cpp_opts->cpp_pedantic = pedantic = 0;
1105 warn_pointer_arith = 0;
1106 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1107 flag_iso = 0;
1108 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1109 warn_cxx_compat = 0;
1110 warn_overlength_strings = 0;
1111 warn_c90_c99_compat = 0;
1112 warn_c99_c11_compat = 0;
1113 return ret;
1116 /* Restore the warning flags which are controlled by __extension__.
1117 FLAGS is the return value from disable_extension_diagnostics. */
1119 static inline void
1120 restore_extension_diagnostics (int flags)
1122 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1123 warn_pointer_arith = (flags >> 1) & 1;
1124 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1125 flag_iso = (flags >> 3) & 1;
1126 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1127 warn_cxx_compat = (flags >> 5) & 1;
1128 warn_overlength_strings = (flags >> 6) & 1;
1129 /* See above for why is this needed. */
1130 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1131 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1134 /* Possibly kinds of declarator to parse. */
1135 typedef enum c_dtr_syn {
1136 /* A normal declarator with an identifier. */
1137 C_DTR_NORMAL,
1138 /* An abstract declarator (maybe empty). */
1139 C_DTR_ABSTRACT,
1140 /* A parameter declarator: may be either, but after a type name does
1141 not redeclare a typedef name as an identifier if it can
1142 alternatively be interpreted as a typedef name; see DR#009,
1143 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1144 following DR#249. For example, given a typedef T, "int T" and
1145 "int *T" are valid parameter declarations redeclaring T, while
1146 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1147 abstract declarators rather than involving redundant parentheses;
1148 the same applies with attributes inside the parentheses before
1149 "T". */
1150 C_DTR_PARM
1151 } c_dtr_syn;
1153 /* The binary operation precedence levels, where 0 is a dummy lowest level
1154 used for the bottom of the stack. */
1155 enum c_parser_prec {
1156 PREC_NONE,
1157 PREC_LOGOR,
1158 PREC_LOGAND,
1159 PREC_BITOR,
1160 PREC_BITXOR,
1161 PREC_BITAND,
1162 PREC_EQ,
1163 PREC_REL,
1164 PREC_SHIFT,
1165 PREC_ADD,
1166 PREC_MULT,
1167 NUM_PRECS
1170 static void c_parser_external_declaration (c_parser *);
1171 static void c_parser_asm_definition (c_parser *);
1172 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1173 bool, bool, tree *, vec<c_token>);
1174 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1175 static void c_parser_static_assert_declaration (c_parser *);
1176 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1177 bool, bool, bool, enum c_lookahead_kind);
1178 static struct c_typespec c_parser_enum_specifier (c_parser *);
1179 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1180 static tree c_parser_struct_declaration (c_parser *);
1181 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1182 static tree c_parser_alignas_specifier (c_parser *);
1183 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1184 bool *);
1185 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1186 c_dtr_syn, bool *);
1187 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1188 bool,
1189 struct c_declarator *);
1190 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1191 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1192 tree);
1193 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1194 static tree c_parser_simple_asm_expr (c_parser *);
1195 static tree c_parser_attributes (c_parser *);
1196 static struct c_type_name *c_parser_type_name (c_parser *);
1197 static struct c_expr c_parser_initializer (c_parser *);
1198 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1199 static void c_parser_initelt (c_parser *, struct obstack *);
1200 static void c_parser_initval (c_parser *, struct c_expr *,
1201 struct obstack *);
1202 static tree c_parser_compound_statement (c_parser *);
1203 static void c_parser_compound_statement_nostart (c_parser *);
1204 static void c_parser_label (c_parser *);
1205 static void c_parser_statement (c_parser *);
1206 static void c_parser_statement_after_labels (c_parser *);
1207 static void c_parser_if_statement (c_parser *);
1208 static void c_parser_switch_statement (c_parser *);
1209 static void c_parser_while_statement (c_parser *, bool);
1210 static void c_parser_do_statement (c_parser *, bool);
1211 static void c_parser_for_statement (c_parser *, bool);
1212 static tree c_parser_asm_statement (c_parser *);
1213 static tree c_parser_asm_operands (c_parser *);
1214 static tree c_parser_asm_goto_operands (c_parser *);
1215 static tree c_parser_asm_clobbers (c_parser *);
1216 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1217 tree = NULL_TREE);
1218 static struct c_expr c_parser_conditional_expression (c_parser *,
1219 struct c_expr *, tree);
1220 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1221 tree);
1222 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1223 static struct c_expr c_parser_unary_expression (c_parser *);
1224 static struct c_expr c_parser_sizeof_expression (c_parser *);
1225 static struct c_expr c_parser_alignof_expression (c_parser *);
1226 static struct c_expr c_parser_postfix_expression (c_parser *);
1227 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1228 struct c_type_name *,
1229 location_t);
1230 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1231 location_t loc,
1232 struct c_expr);
1233 static tree c_parser_transaction (c_parser *, enum rid);
1234 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1235 static tree c_parser_transaction_cancel (c_parser *);
1236 static struct c_expr c_parser_expression (c_parser *);
1237 static struct c_expr c_parser_expression_conv (c_parser *);
1238 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1239 vec<tree, va_gc> **, location_t *,
1240 tree *, vec<location_t> *,
1241 unsigned int * = NULL);
1242 static void c_parser_omp_construct (c_parser *);
1243 static void c_parser_omp_threadprivate (c_parser *);
1244 static void c_parser_omp_barrier (c_parser *);
1245 static void c_parser_omp_flush (c_parser *);
1246 static void c_parser_omp_taskwait (c_parser *);
1247 static void c_parser_omp_taskyield (c_parser *);
1248 static void c_parser_omp_cancel (c_parser *);
1249 static void c_parser_omp_cancellation_point (c_parser *);
1251 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1252 pragma_stmt, pragma_compound };
1253 static bool c_parser_pragma (c_parser *, enum pragma_context);
1254 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1255 static void c_parser_omp_end_declare_target (c_parser *);
1256 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1258 /* These Objective-C parser functions are only ever called when
1259 compiling Objective-C. */
1260 static void c_parser_objc_class_definition (c_parser *, tree);
1261 static void c_parser_objc_class_instance_variables (c_parser *);
1262 static void c_parser_objc_class_declaration (c_parser *);
1263 static void c_parser_objc_alias_declaration (c_parser *);
1264 static void c_parser_objc_protocol_definition (c_parser *, tree);
1265 static bool c_parser_objc_method_type (c_parser *);
1266 static void c_parser_objc_method_definition (c_parser *);
1267 static void c_parser_objc_methodprotolist (c_parser *);
1268 static void c_parser_objc_methodproto (c_parser *);
1269 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1270 static tree c_parser_objc_type_name (c_parser *);
1271 static tree c_parser_objc_protocol_refs (c_parser *);
1272 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1273 static void c_parser_objc_synchronized_statement (c_parser *);
1274 static tree c_parser_objc_selector (c_parser *);
1275 static tree c_parser_objc_selector_arg (c_parser *);
1276 static tree c_parser_objc_receiver (c_parser *);
1277 static tree c_parser_objc_message_args (c_parser *);
1278 static tree c_parser_objc_keywordexpr (c_parser *);
1279 static void c_parser_objc_at_property_declaration (c_parser *);
1280 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1281 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1282 static bool c_parser_objc_diagnose_bad_element_prefix
1283 (c_parser *, struct c_declspecs *);
1285 /* Cilk Plus supporting routines. */
1286 static void c_parser_cilk_simd (c_parser *);
1287 static void c_parser_cilk_for (c_parser *, tree);
1288 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1289 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1290 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1291 static void c_parser_cilk_grainsize (c_parser *);
1293 /* Parse a translation unit (C90 6.7, C99 6.9).
1295 translation-unit:
1296 external-declarations
1298 external-declarations:
1299 external-declaration
1300 external-declarations external-declaration
1302 GNU extensions:
1304 translation-unit:
1305 empty
1308 static void
1309 c_parser_translation_unit (c_parser *parser)
1311 if (c_parser_next_token_is (parser, CPP_EOF))
1313 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1314 "ISO C forbids an empty translation unit");
1316 else
1318 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1319 mark_valid_location_for_stdc_pragma (false);
1322 ggc_collect ();
1323 c_parser_external_declaration (parser);
1324 obstack_free (&parser_obstack, obstack_position);
1326 while (c_parser_next_token_is_not (parser, CPP_EOF));
1330 /* Parse an external declaration (C90 6.7, C99 6.9).
1332 external-declaration:
1333 function-definition
1334 declaration
1336 GNU extensions:
1338 external-declaration:
1339 asm-definition
1341 __extension__ external-declaration
1343 Objective-C:
1345 external-declaration:
1346 objc-class-definition
1347 objc-class-declaration
1348 objc-alias-declaration
1349 objc-protocol-definition
1350 objc-method-definition
1351 @end
1354 static void
1355 c_parser_external_declaration (c_parser *parser)
1357 int ext;
1358 switch (c_parser_peek_token (parser)->type)
1360 case CPP_KEYWORD:
1361 switch (c_parser_peek_token (parser)->keyword)
1363 case RID_EXTENSION:
1364 ext = disable_extension_diagnostics ();
1365 c_parser_consume_token (parser);
1366 c_parser_external_declaration (parser);
1367 restore_extension_diagnostics (ext);
1368 break;
1369 case RID_ASM:
1370 c_parser_asm_definition (parser);
1371 break;
1372 case RID_AT_INTERFACE:
1373 case RID_AT_IMPLEMENTATION:
1374 gcc_assert (c_dialect_objc ());
1375 c_parser_objc_class_definition (parser, NULL_TREE);
1376 break;
1377 case RID_AT_CLASS:
1378 gcc_assert (c_dialect_objc ());
1379 c_parser_objc_class_declaration (parser);
1380 break;
1381 case RID_AT_ALIAS:
1382 gcc_assert (c_dialect_objc ());
1383 c_parser_objc_alias_declaration (parser);
1384 break;
1385 case RID_AT_PROTOCOL:
1386 gcc_assert (c_dialect_objc ());
1387 c_parser_objc_protocol_definition (parser, NULL_TREE);
1388 break;
1389 case RID_AT_PROPERTY:
1390 gcc_assert (c_dialect_objc ());
1391 c_parser_objc_at_property_declaration (parser);
1392 break;
1393 case RID_AT_SYNTHESIZE:
1394 gcc_assert (c_dialect_objc ());
1395 c_parser_objc_at_synthesize_declaration (parser);
1396 break;
1397 case RID_AT_DYNAMIC:
1398 gcc_assert (c_dialect_objc ());
1399 c_parser_objc_at_dynamic_declaration (parser);
1400 break;
1401 case RID_AT_END:
1402 gcc_assert (c_dialect_objc ());
1403 c_parser_consume_token (parser);
1404 objc_finish_implementation ();
1405 break;
1406 default:
1407 goto decl_or_fndef;
1409 break;
1410 case CPP_SEMICOLON:
1411 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1412 "ISO C does not allow extra %<;%> outside of a function");
1413 c_parser_consume_token (parser);
1414 break;
1415 case CPP_PRAGMA:
1416 mark_valid_location_for_stdc_pragma (true);
1417 c_parser_pragma (parser, pragma_external);
1418 mark_valid_location_for_stdc_pragma (false);
1419 break;
1420 case CPP_PLUS:
1421 case CPP_MINUS:
1422 if (c_dialect_objc ())
1424 c_parser_objc_method_definition (parser);
1425 break;
1427 /* Else fall through, and yield a syntax error trying to parse
1428 as a declaration or function definition. */
1429 default:
1430 decl_or_fndef:
1431 /* A declaration or a function definition (or, in Objective-C,
1432 an @interface or @protocol with prefix attributes). We can
1433 only tell which after parsing the declaration specifiers, if
1434 any, and the first declarator. */
1435 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1436 NULL, vNULL);
1437 break;
1441 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1443 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1444 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1445 accepted; otherwise (old-style parameter declarations) only other
1446 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1447 assertion is accepted; otherwise (old-style parameter declarations)
1448 it is not. If NESTED is true, we are inside a function or parsing
1449 old-style parameter declarations; any functions encountered are
1450 nested functions and declaration specifiers are required; otherwise
1451 we are at top level and functions are normal functions and
1452 declaration specifiers may be optional. If EMPTY_OK is true, empty
1453 declarations are OK (subject to all other constraints); otherwise
1454 (old-style parameter declarations) they are diagnosed. If
1455 START_ATTR_OK is true, the declaration specifiers may start with
1456 attributes; otherwise they may not.
1457 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1458 declaration when parsing an Objective-C foreach statement.
1460 declaration:
1461 declaration-specifiers init-declarator-list[opt] ;
1462 static_assert-declaration
1464 function-definition:
1465 declaration-specifiers[opt] declarator declaration-list[opt]
1466 compound-statement
1468 declaration-list:
1469 declaration
1470 declaration-list declaration
1472 init-declarator-list:
1473 init-declarator
1474 init-declarator-list , init-declarator
1476 init-declarator:
1477 declarator simple-asm-expr[opt] attributes[opt]
1478 declarator simple-asm-expr[opt] attributes[opt] = initializer
1480 GNU extensions:
1482 nested-function-definition:
1483 declaration-specifiers declarator declaration-list[opt]
1484 compound-statement
1486 Objective-C:
1487 attributes objc-class-definition
1488 attributes objc-category-definition
1489 attributes objc-protocol-definition
1491 The simple-asm-expr and attributes are GNU extensions.
1493 This function does not handle __extension__; that is handled in its
1494 callers. ??? Following the old parser, __extension__ may start
1495 external declarations, declarations in functions and declarations
1496 at the start of "for" loops, but not old-style parameter
1497 declarations.
1499 C99 requires declaration specifiers in a function definition; the
1500 absence is diagnosed through the diagnosis of implicit int. In GNU
1501 C we also allow but diagnose declarations without declaration
1502 specifiers, but only at top level (elsewhere they conflict with
1503 other syntax).
1505 In Objective-C, declarations of the looping variable in a foreach
1506 statement are exceptionally terminated by 'in' (for example, 'for
1507 (NSObject *object in array) { ... }').
1509 OpenMP:
1511 declaration:
1512 threadprivate-directive */
1514 static void
1515 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1516 bool static_assert_ok, bool empty_ok,
1517 bool nested, bool start_attr_ok,
1518 tree *objc_foreach_object_declaration,
1519 vec<c_token> omp_declare_simd_clauses)
1521 struct c_declspecs *specs;
1522 tree prefix_attrs;
1523 tree all_prefix_attrs;
1524 bool diagnosed_no_specs = false;
1525 location_t here = c_parser_peek_token (parser)->location;
1527 if (static_assert_ok
1528 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1530 c_parser_static_assert_declaration (parser);
1531 return;
1533 specs = build_null_declspecs ();
1535 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1536 if (c_parser_peek_token (parser)->type == CPP_NAME
1537 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1538 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1539 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1540 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1542 error_at (here, "unknown type name %qE",
1543 c_parser_peek_token (parser)->value);
1545 /* Parse declspecs normally to get a correct pointer type, but avoid
1546 a further "fails to be a type name" error. Refuse nested functions
1547 since it is not how the user likely wants us to recover. */
1548 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1549 c_parser_peek_token (parser)->keyword = RID_VOID;
1550 c_parser_peek_token (parser)->value = error_mark_node;
1551 fndef_ok = !nested;
1554 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1555 true, true, cla_nonabstract_decl);
1556 if (parser->error)
1558 c_parser_skip_to_end_of_block_or_statement (parser);
1559 return;
1561 if (nested && !specs->declspecs_seen_p)
1563 c_parser_error (parser, "expected declaration specifiers");
1564 c_parser_skip_to_end_of_block_or_statement (parser);
1565 return;
1567 finish_declspecs (specs);
1568 bool auto_type_p = specs->typespec_word == cts_auto_type;
1569 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1571 if (auto_type_p)
1572 error_at (here, "%<__auto_type%> in empty declaration");
1573 else if (empty_ok)
1574 shadow_tag (specs);
1575 else
1577 shadow_tag_warned (specs, 1);
1578 pedwarn (here, 0, "empty declaration");
1580 c_parser_consume_token (parser);
1581 return;
1584 /* Provide better error recovery. Note that a type name here is usually
1585 better diagnosed as a redeclaration. */
1586 if (empty_ok
1587 && specs->typespec_kind == ctsk_tagdef
1588 && c_parser_next_token_starts_declspecs (parser)
1589 && !c_parser_next_token_is (parser, CPP_NAME))
1591 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1592 parser->error = false;
1593 shadow_tag_warned (specs, 1);
1594 return;
1596 else if (c_dialect_objc () && !auto_type_p)
1598 /* Prefix attributes are an error on method decls. */
1599 switch (c_parser_peek_token (parser)->type)
1601 case CPP_PLUS:
1602 case CPP_MINUS:
1603 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1604 return;
1605 if (specs->attrs)
1607 warning_at (c_parser_peek_token (parser)->location,
1608 OPT_Wattributes,
1609 "prefix attributes are ignored for methods");
1610 specs->attrs = NULL_TREE;
1612 if (fndef_ok)
1613 c_parser_objc_method_definition (parser);
1614 else
1615 c_parser_objc_methodproto (parser);
1616 return;
1617 break;
1618 default:
1619 break;
1621 /* This is where we parse 'attributes @interface ...',
1622 'attributes @implementation ...', 'attributes @protocol ...'
1623 (where attributes could be, for example, __attribute__
1624 ((deprecated)).
1626 switch (c_parser_peek_token (parser)->keyword)
1628 case RID_AT_INTERFACE:
1630 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1631 return;
1632 c_parser_objc_class_definition (parser, specs->attrs);
1633 return;
1635 break;
1636 case RID_AT_IMPLEMENTATION:
1638 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1639 return;
1640 if (specs->attrs)
1642 warning_at (c_parser_peek_token (parser)->location,
1643 OPT_Wattributes,
1644 "prefix attributes are ignored for implementations");
1645 specs->attrs = NULL_TREE;
1647 c_parser_objc_class_definition (parser, NULL_TREE);
1648 return;
1650 break;
1651 case RID_AT_PROTOCOL:
1653 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1654 return;
1655 c_parser_objc_protocol_definition (parser, specs->attrs);
1656 return;
1658 break;
1659 case RID_AT_ALIAS:
1660 case RID_AT_CLASS:
1661 case RID_AT_END:
1662 case RID_AT_PROPERTY:
1663 if (specs->attrs)
1665 c_parser_error (parser, "unexpected attribute");
1666 specs->attrs = NULL;
1668 break;
1669 default:
1670 break;
1674 pending_xref_error ();
1675 prefix_attrs = specs->attrs;
1676 all_prefix_attrs = prefix_attrs;
1677 specs->attrs = NULL_TREE;
1678 while (true)
1680 struct c_declarator *declarator;
1681 bool dummy = false;
1682 timevar_id_t tv;
1683 tree fnbody;
1684 /* Declaring either one or more declarators (in which case we
1685 should diagnose if there were no declaration specifiers) or a
1686 function definition (in which case the diagnostic for
1687 implicit int suffices). */
1688 declarator = c_parser_declarator (parser,
1689 specs->typespec_kind != ctsk_none,
1690 C_DTR_NORMAL, &dummy);
1691 if (declarator == NULL)
1693 if (omp_declare_simd_clauses.exists ()
1694 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1695 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1696 omp_declare_simd_clauses);
1697 c_parser_skip_to_end_of_block_or_statement (parser);
1698 return;
1700 if (auto_type_p && declarator->kind != cdk_id)
1702 error_at (here,
1703 "%<__auto_type%> requires a plain identifier"
1704 " as declarator");
1705 c_parser_skip_to_end_of_block_or_statement (parser);
1706 return;
1708 if (c_parser_next_token_is (parser, CPP_EQ)
1709 || c_parser_next_token_is (parser, CPP_COMMA)
1710 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1711 || c_parser_next_token_is_keyword (parser, RID_ASM)
1712 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1713 || c_parser_next_token_is_keyword (parser, RID_IN))
1715 tree asm_name = NULL_TREE;
1716 tree postfix_attrs = NULL_TREE;
1717 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1719 diagnosed_no_specs = true;
1720 pedwarn (here, 0, "data definition has no type or storage class");
1722 /* Having seen a data definition, there cannot now be a
1723 function definition. */
1724 fndef_ok = false;
1725 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1726 asm_name = c_parser_simple_asm_expr (parser);
1727 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1729 postfix_attrs = c_parser_attributes (parser);
1730 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1732 /* This means there is an attribute specifier after
1733 the declarator in a function definition. Provide
1734 some more information for the user. */
1735 error_at (here, "attributes should be specified before the "
1736 "declarator in a function definition");
1737 c_parser_skip_to_end_of_block_or_statement (parser);
1738 return;
1741 if (c_parser_next_token_is (parser, CPP_EQ))
1743 tree d;
1744 struct c_expr init;
1745 location_t init_loc;
1746 c_parser_consume_token (parser);
1747 if (auto_type_p)
1749 start_init (NULL_TREE, asm_name, global_bindings_p ());
1750 init_loc = c_parser_peek_token (parser)->location;
1751 init = c_parser_expr_no_commas (parser, NULL);
1752 if (TREE_CODE (init.value) == COMPONENT_REF
1753 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1754 error_at (here,
1755 "%<__auto_type%> used with a bit-field"
1756 " initializer");
1757 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1758 tree init_type = TREE_TYPE (init.value);
1759 /* As with typeof, remove all qualifiers from atomic types. */
1760 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1761 init_type
1762 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1763 bool vm_type = variably_modified_type_p (init_type,
1764 NULL_TREE);
1765 if (vm_type)
1766 init.value = c_save_expr (init.value);
1767 finish_init ();
1768 specs->typespec_kind = ctsk_typeof;
1769 specs->locations[cdw_typedef] = init_loc;
1770 specs->typedef_p = true;
1771 specs->type = init_type;
1772 if (vm_type)
1774 bool maybe_const = true;
1775 tree type_expr = c_fully_fold (init.value, false,
1776 &maybe_const);
1777 specs->expr_const_operands &= maybe_const;
1778 if (specs->expr)
1779 specs->expr = build2 (COMPOUND_EXPR,
1780 TREE_TYPE (type_expr),
1781 specs->expr, type_expr);
1782 else
1783 specs->expr = type_expr;
1785 d = start_decl (declarator, specs, true,
1786 chainon (postfix_attrs, all_prefix_attrs));
1787 if (!d)
1788 d = error_mark_node;
1789 if (omp_declare_simd_clauses.exists ()
1790 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1791 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1792 omp_declare_simd_clauses);
1794 else
1796 /* The declaration of the variable is in effect while
1797 its initializer is parsed. */
1798 d = start_decl (declarator, specs, true,
1799 chainon (postfix_attrs, all_prefix_attrs));
1800 if (!d)
1801 d = error_mark_node;
1802 if (omp_declare_simd_clauses.exists ()
1803 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1804 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1805 omp_declare_simd_clauses);
1806 start_init (d, asm_name, global_bindings_p ());
1807 init_loc = c_parser_peek_token (parser)->location;
1808 init = c_parser_initializer (parser);
1809 finish_init ();
1811 if (d != error_mark_node)
1813 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1814 finish_decl (d, init_loc, init.value,
1815 init.original_type, asm_name);
1818 else
1820 if (auto_type_p)
1822 error_at (here,
1823 "%<__auto_type%> requires an initialized "
1824 "data declaration");
1825 c_parser_skip_to_end_of_block_or_statement (parser);
1826 return;
1828 tree d = start_decl (declarator, specs, false,
1829 chainon (postfix_attrs,
1830 all_prefix_attrs));
1831 if (omp_declare_simd_clauses.exists ()
1832 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1834 tree parms = NULL_TREE;
1835 if (d && TREE_CODE (d) == FUNCTION_DECL)
1837 struct c_declarator *ce = declarator;
1838 while (ce != NULL)
1839 if (ce->kind == cdk_function)
1841 parms = ce->u.arg_info->parms;
1842 break;
1844 else
1845 ce = ce->declarator;
1847 if (parms)
1848 temp_store_parm_decls (d, parms);
1849 c_finish_omp_declare_simd (parser, d, parms,
1850 omp_declare_simd_clauses);
1851 if (parms)
1852 temp_pop_parm_decls ();
1854 if (d)
1855 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1856 NULL_TREE, asm_name);
1858 if (c_parser_next_token_is_keyword (parser, RID_IN))
1860 if (d)
1861 *objc_foreach_object_declaration = d;
1862 else
1863 *objc_foreach_object_declaration = error_mark_node;
1866 if (c_parser_next_token_is (parser, CPP_COMMA))
1868 if (auto_type_p)
1870 error_at (here,
1871 "%<__auto_type%> may only be used with"
1872 " a single declarator");
1873 c_parser_skip_to_end_of_block_or_statement (parser);
1874 return;
1876 c_parser_consume_token (parser);
1877 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1878 all_prefix_attrs = chainon (c_parser_attributes (parser),
1879 prefix_attrs);
1880 else
1881 all_prefix_attrs = prefix_attrs;
1882 continue;
1884 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1886 c_parser_consume_token (parser);
1887 return;
1889 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1891 /* This can only happen in Objective-C: we found the
1892 'in' that terminates the declaration inside an
1893 Objective-C foreach statement. Do not consume the
1894 token, so that the caller can use it to determine
1895 that this indeed is a foreach context. */
1896 return;
1898 else
1900 c_parser_error (parser, "expected %<,%> or %<;%>");
1901 c_parser_skip_to_end_of_block_or_statement (parser);
1902 return;
1905 else if (auto_type_p)
1907 error_at (here,
1908 "%<__auto_type%> requires an initialized data declaration");
1909 c_parser_skip_to_end_of_block_or_statement (parser);
1910 return;
1912 else if (!fndef_ok)
1914 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1915 "%<asm%> or %<__attribute__%>");
1916 c_parser_skip_to_end_of_block_or_statement (parser);
1917 return;
1919 /* Function definition (nested or otherwise). */
1920 if (nested)
1922 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1923 c_push_function_context ();
1925 if (!start_function (specs, declarator, all_prefix_attrs))
1927 /* This can appear in many cases looking nothing like a
1928 function definition, so we don't give a more specific
1929 error suggesting there was one. */
1930 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1931 "or %<__attribute__%>");
1932 if (nested)
1933 c_pop_function_context ();
1934 break;
1937 if (DECL_DECLARED_INLINE_P (current_function_decl))
1938 tv = TV_PARSE_INLINE;
1939 else
1940 tv = TV_PARSE_FUNC;
1941 timevar_push (tv);
1943 /* Parse old-style parameter declarations. ??? Attributes are
1944 not allowed to start declaration specifiers here because of a
1945 syntax conflict between a function declaration with attribute
1946 suffix and a function definition with an attribute prefix on
1947 first old-style parameter declaration. Following the old
1948 parser, they are not accepted on subsequent old-style
1949 parameter declarations either. However, there is no
1950 ambiguity after the first declaration, nor indeed on the
1951 first as long as we don't allow postfix attributes after a
1952 declarator with a nonempty identifier list in a definition;
1953 and postfix attributes have never been accepted here in
1954 function definitions either. */
1955 while (c_parser_next_token_is_not (parser, CPP_EOF)
1956 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1957 c_parser_declaration_or_fndef (parser, false, false, false,
1958 true, false, NULL, vNULL);
1959 store_parm_decls ();
1960 if (omp_declare_simd_clauses.exists ()
1961 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1962 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1963 omp_declare_simd_clauses);
1964 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1965 = c_parser_peek_token (parser)->location;
1966 fnbody = c_parser_compound_statement (parser);
1967 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1968 fnbody = expand_array_notation_exprs (fnbody);
1969 if (nested)
1971 tree decl = current_function_decl;
1972 /* Mark nested functions as needing static-chain initially.
1973 lower_nested_functions will recompute it but the
1974 DECL_STATIC_CHAIN flag is also used before that happens,
1975 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1976 DECL_STATIC_CHAIN (decl) = 1;
1977 add_stmt (fnbody);
1978 finish_function ();
1979 c_pop_function_context ();
1980 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1982 else
1984 add_stmt (fnbody);
1985 finish_function ();
1988 timevar_pop (tv);
1989 break;
1993 /* Parse an asm-definition (asm() outside a function body). This is a
1994 GNU extension.
1996 asm-definition:
1997 simple-asm-expr ;
2000 static void
2001 c_parser_asm_definition (c_parser *parser)
2003 tree asm_str = c_parser_simple_asm_expr (parser);
2004 if (asm_str)
2005 symtab->finalize_toplevel_asm (asm_str);
2006 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2009 /* Parse a static assertion (C11 6.7.10).
2011 static_assert-declaration:
2012 static_assert-declaration-no-semi ;
2015 static void
2016 c_parser_static_assert_declaration (c_parser *parser)
2018 c_parser_static_assert_declaration_no_semi (parser);
2019 if (parser->error
2020 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2021 c_parser_skip_to_end_of_block_or_statement (parser);
2024 /* Parse a static assertion (C11 6.7.10), without the trailing
2025 semicolon.
2027 static_assert-declaration-no-semi:
2028 _Static_assert ( constant-expression , string-literal )
2031 static void
2032 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2034 location_t assert_loc, value_loc;
2035 tree value;
2036 tree string;
2038 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2039 assert_loc = c_parser_peek_token (parser)->location;
2040 if (flag_isoc99)
2041 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2042 "ISO C99 does not support %<_Static_assert%>");
2043 else
2044 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2045 "ISO C90 does not support %<_Static_assert%>");
2046 c_parser_consume_token (parser);
2047 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2048 return;
2049 value_loc = c_parser_peek_token (parser)->location;
2050 value = c_parser_expr_no_commas (parser, NULL).value;
2051 parser->lex_untranslated_string = true;
2052 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2054 parser->lex_untranslated_string = false;
2055 return;
2057 switch (c_parser_peek_token (parser)->type)
2059 case CPP_STRING:
2060 case CPP_STRING16:
2061 case CPP_STRING32:
2062 case CPP_WSTRING:
2063 case CPP_UTF8STRING:
2064 string = c_parser_peek_token (parser)->value;
2065 c_parser_consume_token (parser);
2066 parser->lex_untranslated_string = false;
2067 break;
2068 default:
2069 c_parser_error (parser, "expected string literal");
2070 parser->lex_untranslated_string = false;
2071 return;
2073 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2075 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2077 error_at (value_loc, "expression in static assertion is not an integer");
2078 return;
2080 if (TREE_CODE (value) != INTEGER_CST)
2082 value = c_fully_fold (value, false, NULL);
2083 /* Strip no-op conversions. */
2084 STRIP_TYPE_NOPS (value);
2085 if (TREE_CODE (value) == INTEGER_CST)
2086 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2087 "is not an integer constant expression");
2089 if (TREE_CODE (value) != INTEGER_CST)
2091 error_at (value_loc, "expression in static assertion is not constant");
2092 return;
2094 constant_expression_warning (value);
2095 if (integer_zerop (value))
2096 error_at (assert_loc, "static assertion failed: %E", string);
2099 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2100 6.7), adding them to SPECS (which may already include some).
2101 Storage class specifiers are accepted iff SCSPEC_OK; type
2102 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2103 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2104 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2106 declaration-specifiers:
2107 storage-class-specifier declaration-specifiers[opt]
2108 type-specifier declaration-specifiers[opt]
2109 type-qualifier declaration-specifiers[opt]
2110 function-specifier declaration-specifiers[opt]
2111 alignment-specifier declaration-specifiers[opt]
2113 Function specifiers (inline) are from C99, and are currently
2114 handled as storage class specifiers, as is __thread. Alignment
2115 specifiers are from C11.
2117 C90 6.5.1, C99 6.7.1:
2118 storage-class-specifier:
2119 typedef
2120 extern
2121 static
2122 auto
2123 register
2124 _Thread_local
2126 (_Thread_local is new in C11.)
2128 C99 6.7.4:
2129 function-specifier:
2130 inline
2131 _Noreturn
2133 (_Noreturn is new in C11.)
2135 C90 6.5.2, C99 6.7.2:
2136 type-specifier:
2137 void
2138 char
2139 short
2141 long
2142 float
2143 double
2144 signed
2145 unsigned
2146 _Bool
2147 _Complex
2148 [_Imaginary removed in C99 TC2]
2149 struct-or-union-specifier
2150 enum-specifier
2151 typedef-name
2152 atomic-type-specifier
2154 (_Bool and _Complex are new in C99.)
2155 (atomic-type-specifier is new in C11.)
2157 C90 6.5.3, C99 6.7.3:
2159 type-qualifier:
2160 const
2161 restrict
2162 volatile
2163 address-space-qualifier
2164 _Atomic
2166 (restrict is new in C99.)
2167 (_Atomic is new in C11.)
2169 GNU extensions:
2171 declaration-specifiers:
2172 attributes declaration-specifiers[opt]
2174 type-qualifier:
2175 address-space
2177 address-space:
2178 identifier recognized by the target
2180 storage-class-specifier:
2181 __thread
2183 type-specifier:
2184 typeof-specifier
2185 __auto_type
2186 __intN
2187 _Decimal32
2188 _Decimal64
2189 _Decimal128
2190 _Fract
2191 _Accum
2192 _Sat
2194 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2195 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2197 atomic-type-specifier
2198 _Atomic ( type-name )
2200 Objective-C:
2202 type-specifier:
2203 class-name objc-protocol-refs[opt]
2204 typedef-name objc-protocol-refs
2205 objc-protocol-refs
2208 static void
2209 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2210 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2211 bool alignspec_ok, bool auto_type_ok,
2212 enum c_lookahead_kind la)
2214 bool attrs_ok = start_attr_ok;
2215 bool seen_type = specs->typespec_kind != ctsk_none;
2217 if (!typespec_ok)
2218 gcc_assert (la == cla_prefer_id);
2220 while (c_parser_next_token_is (parser, CPP_NAME)
2221 || c_parser_next_token_is (parser, CPP_KEYWORD)
2222 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2224 struct c_typespec t;
2225 tree attrs;
2226 tree align;
2227 location_t loc = c_parser_peek_token (parser)->location;
2229 /* If we cannot accept a type, exit if the next token must start
2230 one. Also, if we already have seen a tagged definition,
2231 a typename would be an error anyway and likely the user
2232 has simply forgotten a semicolon, so we exit. */
2233 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2234 && c_parser_next_tokens_start_typename (parser, la)
2235 && !c_parser_next_token_is_qualifier (parser))
2236 break;
2238 if (c_parser_next_token_is (parser, CPP_NAME))
2240 c_token *name_token = c_parser_peek_token (parser);
2241 tree value = name_token->value;
2242 c_id_kind kind = name_token->id_kind;
2244 if (kind == C_ID_ADDRSPACE)
2246 addr_space_t as
2247 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2248 declspecs_add_addrspace (name_token->location, specs, as);
2249 c_parser_consume_token (parser);
2250 attrs_ok = true;
2251 continue;
2254 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2256 /* If we cannot accept a type, and the next token must start one,
2257 exit. Do the same if we already have seen a tagged definition,
2258 since it would be an error anyway and likely the user has simply
2259 forgotten a semicolon. */
2260 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2261 break;
2263 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2264 a C_ID_CLASSNAME. */
2265 c_parser_consume_token (parser);
2266 seen_type = true;
2267 attrs_ok = true;
2268 if (kind == C_ID_ID)
2270 error_at (loc, "unknown type name %qE", value);
2271 t.kind = ctsk_typedef;
2272 t.spec = error_mark_node;
2274 else if (kind == C_ID_TYPENAME
2275 && (!c_dialect_objc ()
2276 || c_parser_next_token_is_not (parser, CPP_LESS)))
2278 t.kind = ctsk_typedef;
2279 /* For a typedef name, record the meaning, not the name.
2280 In case of 'foo foo, bar;'. */
2281 t.spec = lookup_name (value);
2283 else
2285 tree proto = NULL_TREE;
2286 gcc_assert (c_dialect_objc ());
2287 t.kind = ctsk_objc;
2288 if (c_parser_next_token_is (parser, CPP_LESS))
2289 proto = c_parser_objc_protocol_refs (parser);
2290 t.spec = objc_get_protocol_qualified_type (value, proto);
2292 t.expr = NULL_TREE;
2293 t.expr_const_operands = true;
2294 declspecs_add_type (name_token->location, specs, t);
2295 continue;
2297 if (c_parser_next_token_is (parser, CPP_LESS))
2299 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2300 nisse@lysator.liu.se. */
2301 tree proto;
2302 gcc_assert (c_dialect_objc ());
2303 if (!typespec_ok || seen_type)
2304 break;
2305 proto = c_parser_objc_protocol_refs (parser);
2306 t.kind = ctsk_objc;
2307 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2308 t.expr = NULL_TREE;
2309 t.expr_const_operands = true;
2310 declspecs_add_type (loc, specs, t);
2311 continue;
2313 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2314 switch (c_parser_peek_token (parser)->keyword)
2316 case RID_STATIC:
2317 case RID_EXTERN:
2318 case RID_REGISTER:
2319 case RID_TYPEDEF:
2320 case RID_INLINE:
2321 case RID_NORETURN:
2322 case RID_AUTO:
2323 case RID_THREAD:
2324 if (!scspec_ok)
2325 goto out;
2326 attrs_ok = true;
2327 /* TODO: Distinguish between function specifiers (inline, noreturn)
2328 and storage class specifiers, either here or in
2329 declspecs_add_scspec. */
2330 declspecs_add_scspec (loc, specs,
2331 c_parser_peek_token (parser)->value);
2332 c_parser_consume_token (parser);
2333 break;
2334 case RID_AUTO_TYPE:
2335 if (!auto_type_ok)
2336 goto out;
2337 /* Fall through. */
2338 case RID_UNSIGNED:
2339 case RID_LONG:
2340 case RID_SHORT:
2341 case RID_SIGNED:
2342 case RID_COMPLEX:
2343 case RID_INT:
2344 case RID_CHAR:
2345 case RID_FLOAT:
2346 case RID_DOUBLE:
2347 case RID_VOID:
2348 case RID_DFLOAT32:
2349 case RID_DFLOAT64:
2350 case RID_DFLOAT128:
2351 case RID_BOOL:
2352 case RID_FRACT:
2353 case RID_ACCUM:
2354 case RID_SAT:
2355 case RID_INT_N_0:
2356 case RID_INT_N_1:
2357 case RID_INT_N_2:
2358 case RID_INT_N_3:
2359 if (!typespec_ok)
2360 goto out;
2361 attrs_ok = true;
2362 seen_type = true;
2363 if (c_dialect_objc ())
2364 parser->objc_need_raw_identifier = true;
2365 t.kind = ctsk_resword;
2366 t.spec = c_parser_peek_token (parser)->value;
2367 t.expr = NULL_TREE;
2368 t.expr_const_operands = true;
2369 declspecs_add_type (loc, specs, t);
2370 c_parser_consume_token (parser);
2371 break;
2372 case RID_ENUM:
2373 if (!typespec_ok)
2374 goto out;
2375 attrs_ok = true;
2376 seen_type = true;
2377 t = c_parser_enum_specifier (parser);
2378 declspecs_add_type (loc, specs, t);
2379 break;
2380 case RID_STRUCT:
2381 case RID_UNION:
2382 if (!typespec_ok)
2383 goto out;
2384 attrs_ok = true;
2385 seen_type = true;
2386 t = c_parser_struct_or_union_specifier (parser);
2387 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2388 declspecs_add_type (loc, specs, t);
2389 break;
2390 case RID_TYPEOF:
2391 /* ??? The old parser rejected typeof after other type
2392 specifiers, but is a syntax error the best way of
2393 handling this? */
2394 if (!typespec_ok || seen_type)
2395 goto out;
2396 attrs_ok = true;
2397 seen_type = true;
2398 t = c_parser_typeof_specifier (parser);
2399 declspecs_add_type (loc, specs, t);
2400 break;
2401 case RID_ATOMIC:
2402 /* C parser handling of Objective-C constructs needs
2403 checking for correct lvalue-to-rvalue conversions, and
2404 the code in build_modify_expr handling various
2405 Objective-C cases, and that in build_unary_op handling
2406 Objective-C cases for increment / decrement, also needs
2407 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2408 and objc_types_are_equivalent may also need updates. */
2409 if (c_dialect_objc ())
2410 sorry ("%<_Atomic%> in Objective-C");
2411 /* C parser handling of OpenMP constructs needs checking for
2412 correct lvalue-to-rvalue conversions. */
2413 if (flag_openmp)
2414 sorry ("%<_Atomic%> with OpenMP");
2415 if (flag_isoc99)
2416 pedwarn_c99 (loc, OPT_Wpedantic,
2417 "ISO C99 does not support the %<_Atomic%> qualifier");
2418 else
2419 pedwarn_c99 (loc, OPT_Wpedantic,
2420 "ISO C90 does not support the %<_Atomic%> qualifier");
2421 attrs_ok = true;
2422 tree value;
2423 value = c_parser_peek_token (parser)->value;
2424 c_parser_consume_token (parser);
2425 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2427 /* _Atomic ( type-name ). */
2428 seen_type = true;
2429 c_parser_consume_token (parser);
2430 struct c_type_name *type = c_parser_type_name (parser);
2431 t.kind = ctsk_typeof;
2432 t.spec = error_mark_node;
2433 t.expr = NULL_TREE;
2434 t.expr_const_operands = true;
2435 if (type != NULL)
2436 t.spec = groktypename (type, &t.expr,
2437 &t.expr_const_operands);
2438 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2439 "expected %<)%>");
2440 if (t.spec != error_mark_node)
2442 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2443 error_at (loc, "%<_Atomic%>-qualified array type");
2444 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2445 error_at (loc, "%<_Atomic%>-qualified function type");
2446 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2447 error_at (loc, "%<_Atomic%> applied to a qualified type");
2448 else
2449 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2451 declspecs_add_type (loc, specs, t);
2453 else
2454 declspecs_add_qual (loc, specs, value);
2455 break;
2456 case RID_CONST:
2457 case RID_VOLATILE:
2458 case RID_RESTRICT:
2459 attrs_ok = true;
2460 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2461 c_parser_consume_token (parser);
2462 break;
2463 case RID_ATTRIBUTE:
2464 if (!attrs_ok)
2465 goto out;
2466 attrs = c_parser_attributes (parser);
2467 declspecs_add_attrs (loc, specs, attrs);
2468 break;
2469 case RID_ALIGNAS:
2470 if (!alignspec_ok)
2471 goto out;
2472 align = c_parser_alignas_specifier (parser);
2473 declspecs_add_alignas (loc, specs, align);
2474 break;
2475 default:
2476 goto out;
2479 out: ;
2482 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2484 enum-specifier:
2485 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2486 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2487 enum attributes[opt] identifier
2489 The form with trailing comma is new in C99. The forms with
2490 attributes are GNU extensions. In GNU C, we accept any expression
2491 without commas in the syntax (assignment expressions, not just
2492 conditional expressions); assignment expressions will be diagnosed
2493 as non-constant.
2495 enumerator-list:
2496 enumerator
2497 enumerator-list , enumerator
2499 enumerator:
2500 enumeration-constant
2501 enumeration-constant = constant-expression
2504 static struct c_typespec
2505 c_parser_enum_specifier (c_parser *parser)
2507 struct c_typespec ret;
2508 tree attrs;
2509 tree ident = NULL_TREE;
2510 location_t enum_loc;
2511 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2512 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2513 enum_loc = c_parser_peek_token (parser)->location;
2514 c_parser_consume_token (parser);
2515 attrs = c_parser_attributes (parser);
2516 enum_loc = c_parser_peek_token (parser)->location;
2517 /* Set the location in case we create a decl now. */
2518 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2519 if (c_parser_next_token_is (parser, CPP_NAME))
2521 ident = c_parser_peek_token (parser)->value;
2522 ident_loc = c_parser_peek_token (parser)->location;
2523 enum_loc = ident_loc;
2524 c_parser_consume_token (parser);
2526 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2528 /* Parse an enum definition. */
2529 struct c_enum_contents the_enum;
2530 tree type;
2531 tree postfix_attrs;
2532 /* We chain the enumerators in reverse order, then put them in
2533 forward order at the end. */
2534 tree values;
2535 timevar_push (TV_PARSE_ENUM);
2536 type = start_enum (enum_loc, &the_enum, ident);
2537 values = NULL_TREE;
2538 c_parser_consume_token (parser);
2539 while (true)
2541 tree enum_id;
2542 tree enum_value;
2543 tree enum_decl;
2544 bool seen_comma;
2545 c_token *token;
2546 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2547 location_t decl_loc, value_loc;
2548 if (c_parser_next_token_is_not (parser, CPP_NAME))
2550 c_parser_error (parser, "expected identifier");
2551 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2552 values = error_mark_node;
2553 break;
2555 token = c_parser_peek_token (parser);
2556 enum_id = token->value;
2557 /* Set the location in case we create a decl now. */
2558 c_parser_set_source_position_from_token (token);
2559 decl_loc = value_loc = token->location;
2560 c_parser_consume_token (parser);
2561 if (c_parser_next_token_is (parser, CPP_EQ))
2563 c_parser_consume_token (parser);
2564 value_loc = c_parser_peek_token (parser)->location;
2565 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2567 else
2568 enum_value = NULL_TREE;
2569 enum_decl = build_enumerator (decl_loc, value_loc,
2570 &the_enum, enum_id, enum_value);
2571 TREE_CHAIN (enum_decl) = values;
2572 values = enum_decl;
2573 seen_comma = false;
2574 if (c_parser_next_token_is (parser, CPP_COMMA))
2576 comma_loc = c_parser_peek_token (parser)->location;
2577 seen_comma = true;
2578 c_parser_consume_token (parser);
2580 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2582 if (seen_comma)
2583 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2584 "comma at end of enumerator list");
2585 c_parser_consume_token (parser);
2586 break;
2588 if (!seen_comma)
2590 c_parser_error (parser, "expected %<,%> or %<}%>");
2591 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2592 values = error_mark_node;
2593 break;
2596 postfix_attrs = c_parser_attributes (parser);
2597 ret.spec = finish_enum (type, nreverse (values),
2598 chainon (attrs, postfix_attrs));
2599 ret.kind = ctsk_tagdef;
2600 ret.expr = NULL_TREE;
2601 ret.expr_const_operands = true;
2602 timevar_pop (TV_PARSE_ENUM);
2603 return ret;
2605 else if (!ident)
2607 c_parser_error (parser, "expected %<{%>");
2608 ret.spec = error_mark_node;
2609 ret.kind = ctsk_tagref;
2610 ret.expr = NULL_TREE;
2611 ret.expr_const_operands = true;
2612 return ret;
2614 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2615 /* In ISO C, enumerated types can be referred to only if already
2616 defined. */
2617 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2619 gcc_assert (ident);
2620 pedwarn (enum_loc, OPT_Wpedantic,
2621 "ISO C forbids forward references to %<enum%> types");
2623 return ret;
2626 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2628 struct-or-union-specifier:
2629 struct-or-union attributes[opt] identifier[opt]
2630 { struct-contents } attributes[opt]
2631 struct-or-union attributes[opt] identifier
2633 struct-contents:
2634 struct-declaration-list
2636 struct-declaration-list:
2637 struct-declaration ;
2638 struct-declaration-list struct-declaration ;
2640 GNU extensions:
2642 struct-contents:
2643 empty
2644 struct-declaration
2645 struct-declaration-list struct-declaration
2647 struct-declaration-list:
2648 struct-declaration-list ;
2651 (Note that in the syntax here, unlike that in ISO C, the semicolons
2652 are included here rather than in struct-declaration, in order to
2653 describe the syntax with extra semicolons and missing semicolon at
2654 end.)
2656 Objective-C:
2658 struct-declaration-list:
2659 @defs ( class-name )
2661 (Note this does not include a trailing semicolon, but can be
2662 followed by further declarations, and gets a pedwarn-if-pedantic
2663 when followed by a semicolon.) */
2665 static struct c_typespec
2666 c_parser_struct_or_union_specifier (c_parser *parser)
2668 struct c_typespec ret;
2669 tree attrs;
2670 tree ident = NULL_TREE;
2671 location_t struct_loc;
2672 location_t ident_loc = UNKNOWN_LOCATION;
2673 enum tree_code code;
2674 switch (c_parser_peek_token (parser)->keyword)
2676 case RID_STRUCT:
2677 code = RECORD_TYPE;
2678 break;
2679 case RID_UNION:
2680 code = UNION_TYPE;
2681 break;
2682 default:
2683 gcc_unreachable ();
2685 struct_loc = c_parser_peek_token (parser)->location;
2686 c_parser_consume_token (parser);
2687 attrs = c_parser_attributes (parser);
2689 /* Set the location in case we create a decl now. */
2690 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2692 if (c_parser_next_token_is (parser, CPP_NAME))
2694 ident = c_parser_peek_token (parser)->value;
2695 ident_loc = c_parser_peek_token (parser)->location;
2696 struct_loc = ident_loc;
2697 c_parser_consume_token (parser);
2699 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2701 /* Parse a struct or union definition. Start the scope of the
2702 tag before parsing components. */
2703 struct c_struct_parse_info *struct_info;
2704 tree type = start_struct (struct_loc, code, ident, &struct_info);
2705 tree postfix_attrs;
2706 /* We chain the components in reverse order, then put them in
2707 forward order at the end. Each struct-declaration may
2708 declare multiple components (comma-separated), so we must use
2709 chainon to join them, although when parsing each
2710 struct-declaration we can use TREE_CHAIN directly.
2712 The theory behind all this is that there will be more
2713 semicolon separated fields than comma separated fields, and
2714 so we'll be minimizing the number of node traversals required
2715 by chainon. */
2716 tree contents;
2717 timevar_push (TV_PARSE_STRUCT);
2718 contents = NULL_TREE;
2719 c_parser_consume_token (parser);
2720 /* Handle the Objective-C @defs construct,
2721 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2722 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2724 tree name;
2725 gcc_assert (c_dialect_objc ());
2726 c_parser_consume_token (parser);
2727 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2728 goto end_at_defs;
2729 if (c_parser_next_token_is (parser, CPP_NAME)
2730 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2732 name = c_parser_peek_token (parser)->value;
2733 c_parser_consume_token (parser);
2735 else
2737 c_parser_error (parser, "expected class name");
2738 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2739 goto end_at_defs;
2741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2742 "expected %<)%>");
2743 contents = nreverse (objc_get_class_ivars (name));
2745 end_at_defs:
2746 /* Parse the struct-declarations and semicolons. Problems with
2747 semicolons are diagnosed here; empty structures are diagnosed
2748 elsewhere. */
2749 while (true)
2751 tree decls;
2752 /* Parse any stray semicolon. */
2753 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2755 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2756 "extra semicolon in struct or union specified");
2757 c_parser_consume_token (parser);
2758 continue;
2760 /* Stop if at the end of the struct or union contents. */
2761 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2763 c_parser_consume_token (parser);
2764 break;
2766 /* Accept #pragmas at struct scope. */
2767 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2769 c_parser_pragma (parser, pragma_struct);
2770 continue;
2772 /* Parse some comma-separated declarations, but not the
2773 trailing semicolon if any. */
2774 decls = c_parser_struct_declaration (parser);
2775 contents = chainon (decls, contents);
2776 /* If no semicolon follows, either we have a parse error or
2777 are at the end of the struct or union and should
2778 pedwarn. */
2779 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2780 c_parser_consume_token (parser);
2781 else
2783 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2784 pedwarn (c_parser_peek_token (parser)->location, 0,
2785 "no semicolon at end of struct or union");
2786 else if (parser->error
2787 || !c_parser_next_token_starts_declspecs (parser))
2789 c_parser_error (parser, "expected %<;%>");
2790 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2791 break;
2794 /* If we come here, we have already emitted an error
2795 for an expected `;', identifier or `(', and we also
2796 recovered already. Go on with the next field. */
2799 postfix_attrs = c_parser_attributes (parser);
2800 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2801 chainon (attrs, postfix_attrs), struct_info);
2802 ret.kind = ctsk_tagdef;
2803 ret.expr = NULL_TREE;
2804 ret.expr_const_operands = true;
2805 timevar_pop (TV_PARSE_STRUCT);
2806 return ret;
2808 else if (!ident)
2810 c_parser_error (parser, "expected %<{%>");
2811 ret.spec = error_mark_node;
2812 ret.kind = ctsk_tagref;
2813 ret.expr = NULL_TREE;
2814 ret.expr_const_operands = true;
2815 return ret;
2817 ret = parser_xref_tag (ident_loc, code, ident);
2818 return ret;
2821 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2822 the trailing semicolon.
2824 struct-declaration:
2825 specifier-qualifier-list struct-declarator-list
2826 static_assert-declaration-no-semi
2828 specifier-qualifier-list:
2829 type-specifier specifier-qualifier-list[opt]
2830 type-qualifier specifier-qualifier-list[opt]
2831 attributes specifier-qualifier-list[opt]
2833 struct-declarator-list:
2834 struct-declarator
2835 struct-declarator-list , attributes[opt] struct-declarator
2837 struct-declarator:
2838 declarator attributes[opt]
2839 declarator[opt] : constant-expression attributes[opt]
2841 GNU extensions:
2843 struct-declaration:
2844 __extension__ struct-declaration
2845 specifier-qualifier-list
2847 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2848 of attributes where shown is a GNU extension. In GNU C, we accept
2849 any expression without commas in the syntax (assignment
2850 expressions, not just conditional expressions); assignment
2851 expressions will be diagnosed as non-constant. */
2853 static tree
2854 c_parser_struct_declaration (c_parser *parser)
2856 struct c_declspecs *specs;
2857 tree prefix_attrs;
2858 tree all_prefix_attrs;
2859 tree decls;
2860 location_t decl_loc;
2861 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2863 int ext;
2864 tree decl;
2865 ext = disable_extension_diagnostics ();
2866 c_parser_consume_token (parser);
2867 decl = c_parser_struct_declaration (parser);
2868 restore_extension_diagnostics (ext);
2869 return decl;
2871 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2873 c_parser_static_assert_declaration_no_semi (parser);
2874 return NULL_TREE;
2876 specs = build_null_declspecs ();
2877 decl_loc = c_parser_peek_token (parser)->location;
2878 /* Strictly by the standard, we shouldn't allow _Alignas here,
2879 but it appears to have been intended to allow it there, so
2880 we're keeping it as it is until WG14 reaches a conclusion
2881 of N1731.
2882 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2883 c_parser_declspecs (parser, specs, false, true, true,
2884 true, false, cla_nonabstract_decl);
2885 if (parser->error)
2886 return NULL_TREE;
2887 if (!specs->declspecs_seen_p)
2889 c_parser_error (parser, "expected specifier-qualifier-list");
2890 return NULL_TREE;
2892 finish_declspecs (specs);
2893 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2894 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2896 tree ret;
2897 if (specs->typespec_kind == ctsk_none)
2899 pedwarn (decl_loc, OPT_Wpedantic,
2900 "ISO C forbids member declarations with no members");
2901 shadow_tag_warned (specs, pedantic);
2902 ret = NULL_TREE;
2904 else
2906 /* Support for unnamed structs or unions as members of
2907 structs or unions (which is [a] useful and [b] supports
2908 MS P-SDK). */
2909 tree attrs = NULL;
2911 ret = grokfield (c_parser_peek_token (parser)->location,
2912 build_id_declarator (NULL_TREE), specs,
2913 NULL_TREE, &attrs);
2914 if (ret)
2915 decl_attributes (&ret, attrs, 0);
2917 return ret;
2920 /* Provide better error recovery. Note that a type name here is valid,
2921 and will be treated as a field name. */
2922 if (specs->typespec_kind == ctsk_tagdef
2923 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2924 && c_parser_next_token_starts_declspecs (parser)
2925 && !c_parser_next_token_is (parser, CPP_NAME))
2927 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2928 parser->error = false;
2929 return NULL_TREE;
2932 pending_xref_error ();
2933 prefix_attrs = specs->attrs;
2934 all_prefix_attrs = prefix_attrs;
2935 specs->attrs = NULL_TREE;
2936 decls = NULL_TREE;
2937 while (true)
2939 /* Declaring one or more declarators or un-named bit-fields. */
2940 struct c_declarator *declarator;
2941 bool dummy = false;
2942 if (c_parser_next_token_is (parser, CPP_COLON))
2943 declarator = build_id_declarator (NULL_TREE);
2944 else
2945 declarator = c_parser_declarator (parser,
2946 specs->typespec_kind != ctsk_none,
2947 C_DTR_NORMAL, &dummy);
2948 if (declarator == NULL)
2950 c_parser_skip_to_end_of_block_or_statement (parser);
2951 break;
2953 if (c_parser_next_token_is (parser, CPP_COLON)
2954 || c_parser_next_token_is (parser, CPP_COMMA)
2955 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2956 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2957 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2959 tree postfix_attrs = NULL_TREE;
2960 tree width = NULL_TREE;
2961 tree d;
2962 if (c_parser_next_token_is (parser, CPP_COLON))
2964 c_parser_consume_token (parser);
2965 width = c_parser_expr_no_commas (parser, NULL).value;
2967 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2968 postfix_attrs = c_parser_attributes (parser);
2969 d = grokfield (c_parser_peek_token (parser)->location,
2970 declarator, specs, width, &all_prefix_attrs);
2971 decl_attributes (&d, chainon (postfix_attrs,
2972 all_prefix_attrs), 0);
2973 DECL_CHAIN (d) = decls;
2974 decls = d;
2975 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2976 all_prefix_attrs = chainon (c_parser_attributes (parser),
2977 prefix_attrs);
2978 else
2979 all_prefix_attrs = prefix_attrs;
2980 if (c_parser_next_token_is (parser, CPP_COMMA))
2981 c_parser_consume_token (parser);
2982 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2983 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2985 /* Semicolon consumed in caller. */
2986 break;
2988 else
2990 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2991 break;
2994 else
2996 c_parser_error (parser,
2997 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2998 "%<__attribute__%>");
2999 break;
3002 return decls;
3005 /* Parse a typeof specifier (a GNU extension).
3007 typeof-specifier:
3008 typeof ( expression )
3009 typeof ( type-name )
3012 static struct c_typespec
3013 c_parser_typeof_specifier (c_parser *parser)
3015 struct c_typespec ret;
3016 ret.kind = ctsk_typeof;
3017 ret.spec = error_mark_node;
3018 ret.expr = NULL_TREE;
3019 ret.expr_const_operands = true;
3020 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3021 c_parser_consume_token (parser);
3022 c_inhibit_evaluation_warnings++;
3023 in_typeof++;
3024 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3026 c_inhibit_evaluation_warnings--;
3027 in_typeof--;
3028 return ret;
3030 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3032 struct c_type_name *type = c_parser_type_name (parser);
3033 c_inhibit_evaluation_warnings--;
3034 in_typeof--;
3035 if (type != NULL)
3037 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3038 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3041 else
3043 bool was_vm;
3044 location_t here = c_parser_peek_token (parser)->location;
3045 struct c_expr expr = c_parser_expression (parser);
3046 c_inhibit_evaluation_warnings--;
3047 in_typeof--;
3048 if (TREE_CODE (expr.value) == COMPONENT_REF
3049 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3050 error_at (here, "%<typeof%> applied to a bit-field");
3051 mark_exp_read (expr.value);
3052 ret.spec = TREE_TYPE (expr.value);
3053 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3054 /* This is returned with the type so that when the type is
3055 evaluated, this can be evaluated. */
3056 if (was_vm)
3057 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3058 pop_maybe_used (was_vm);
3059 /* For use in macros such as those in <stdatomic.h>, remove all
3060 qualifiers from atomic types. (const can be an issue for more macros
3061 using typeof than just the <stdatomic.h> ones.) */
3062 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3063 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3066 return ret;
3069 /* Parse an alignment-specifier.
3071 C11 6.7.5:
3073 alignment-specifier:
3074 _Alignas ( type-name )
3075 _Alignas ( constant-expression )
3078 static tree
3079 c_parser_alignas_specifier (c_parser * parser)
3081 tree ret = error_mark_node;
3082 location_t loc = c_parser_peek_token (parser)->location;
3083 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3084 c_parser_consume_token (parser);
3085 if (flag_isoc99)
3086 pedwarn_c99 (loc, OPT_Wpedantic,
3087 "ISO C99 does not support %<_Alignas%>");
3088 else
3089 pedwarn_c99 (loc, OPT_Wpedantic,
3090 "ISO C90 does not support %<_Alignas%>");
3091 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3092 return ret;
3093 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3095 struct c_type_name *type = c_parser_type_name (parser);
3096 if (type != NULL)
3097 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3098 false, true, 1);
3100 else
3101 ret = c_parser_expr_no_commas (parser, NULL).value;
3102 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3103 return ret;
3106 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3107 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3108 be redeclared; otherwise it may not. KIND indicates which kind of
3109 declarator is wanted. Returns a valid declarator except in the
3110 case of a syntax error in which case NULL is returned. *SEEN_ID is
3111 set to true if an identifier being declared is seen; this is used
3112 to diagnose bad forms of abstract array declarators and to
3113 determine whether an identifier list is syntactically permitted.
3115 declarator:
3116 pointer[opt] direct-declarator
3118 direct-declarator:
3119 identifier
3120 ( attributes[opt] declarator )
3121 direct-declarator array-declarator
3122 direct-declarator ( parameter-type-list )
3123 direct-declarator ( identifier-list[opt] )
3125 pointer:
3126 * type-qualifier-list[opt]
3127 * type-qualifier-list[opt] pointer
3129 type-qualifier-list:
3130 type-qualifier
3131 attributes
3132 type-qualifier-list type-qualifier
3133 type-qualifier-list attributes
3135 array-declarator:
3136 [ type-qualifier-list[opt] assignment-expression[opt] ]
3137 [ static type-qualifier-list[opt] assignment-expression ]
3138 [ type-qualifier-list static assignment-expression ]
3139 [ type-qualifier-list[opt] * ]
3141 parameter-type-list:
3142 parameter-list
3143 parameter-list , ...
3145 parameter-list:
3146 parameter-declaration
3147 parameter-list , parameter-declaration
3149 parameter-declaration:
3150 declaration-specifiers declarator attributes[opt]
3151 declaration-specifiers abstract-declarator[opt] attributes[opt]
3153 identifier-list:
3154 identifier
3155 identifier-list , identifier
3157 abstract-declarator:
3158 pointer
3159 pointer[opt] direct-abstract-declarator
3161 direct-abstract-declarator:
3162 ( attributes[opt] abstract-declarator )
3163 direct-abstract-declarator[opt] array-declarator
3164 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3166 GNU extensions:
3168 direct-declarator:
3169 direct-declarator ( parameter-forward-declarations
3170 parameter-type-list[opt] )
3172 direct-abstract-declarator:
3173 direct-abstract-declarator[opt] ( parameter-forward-declarations
3174 parameter-type-list[opt] )
3176 parameter-forward-declarations:
3177 parameter-list ;
3178 parameter-forward-declarations parameter-list ;
3180 The uses of attributes shown above are GNU extensions.
3182 Some forms of array declarator are not included in C99 in the
3183 syntax for abstract declarators; these are disallowed elsewhere.
3184 This may be a defect (DR#289).
3186 This function also accepts an omitted abstract declarator as being
3187 an abstract declarator, although not part of the formal syntax. */
3189 static struct c_declarator *
3190 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3191 bool *seen_id)
3193 /* Parse any initial pointer part. */
3194 if (c_parser_next_token_is (parser, CPP_MULT))
3196 struct c_declspecs *quals_attrs = build_null_declspecs ();
3197 struct c_declarator *inner;
3198 c_parser_consume_token (parser);
3199 c_parser_declspecs (parser, quals_attrs, false, false, true,
3200 false, false, cla_prefer_id);
3201 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3202 if (inner == NULL)
3203 return NULL;
3204 else
3205 return make_pointer_declarator (quals_attrs, inner);
3207 /* Now we have a direct declarator, direct abstract declarator or
3208 nothing (which counts as a direct abstract declarator here). */
3209 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3212 /* Parse a direct declarator or direct abstract declarator; arguments
3213 as c_parser_declarator. */
3215 static struct c_declarator *
3216 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3217 bool *seen_id)
3219 /* The direct declarator must start with an identifier (possibly
3220 omitted) or a parenthesized declarator (possibly abstract). In
3221 an ordinary declarator, initial parentheses must start a
3222 parenthesized declarator. In an abstract declarator or parameter
3223 declarator, they could start a parenthesized declarator or a
3224 parameter list. To tell which, the open parenthesis and any
3225 following attributes must be read. If a declaration specifier
3226 follows, then it is a parameter list; if the specifier is a
3227 typedef name, there might be an ambiguity about redeclaring it,
3228 which is resolved in the direction of treating it as a typedef
3229 name. If a close parenthesis follows, it is also an empty
3230 parameter list, as the syntax does not permit empty abstract
3231 declarators. Otherwise, it is a parenthesized declarator (in
3232 which case the analysis may be repeated inside it, recursively).
3234 ??? There is an ambiguity in a parameter declaration "int
3235 (__attribute__((foo)) x)", where x is not a typedef name: it
3236 could be an abstract declarator for a function, or declare x with
3237 parentheses. The proper resolution of this ambiguity needs
3238 documenting. At present we follow an accident of the old
3239 parser's implementation, whereby the first parameter must have
3240 some declaration specifiers other than just attributes. Thus as
3241 a parameter declaration it is treated as a parenthesized
3242 parameter named x, and as an abstract declarator it is
3243 rejected.
3245 ??? Also following the old parser, attributes inside an empty
3246 parameter list are ignored, making it a list not yielding a
3247 prototype, rather than giving an error or making it have one
3248 parameter with implicit type int.
3250 ??? Also following the old parser, typedef names may be
3251 redeclared in declarators, but not Objective-C class names. */
3253 if (kind != C_DTR_ABSTRACT
3254 && c_parser_next_token_is (parser, CPP_NAME)
3255 && ((type_seen_p
3256 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3257 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3258 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3260 struct c_declarator *inner
3261 = build_id_declarator (c_parser_peek_token (parser)->value);
3262 *seen_id = true;
3263 inner->id_loc = c_parser_peek_token (parser)->location;
3264 c_parser_consume_token (parser);
3265 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3268 if (kind != C_DTR_NORMAL
3269 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3271 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3272 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3275 /* Either we are at the end of an abstract declarator, or we have
3276 parentheses. */
3278 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3280 tree attrs;
3281 struct c_declarator *inner;
3282 c_parser_consume_token (parser);
3283 attrs = c_parser_attributes (parser);
3284 if (kind != C_DTR_NORMAL
3285 && (c_parser_next_token_starts_declspecs (parser)
3286 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3288 struct c_arg_info *args
3289 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3290 attrs);
3291 if (args == NULL)
3292 return NULL;
3293 else
3295 inner
3296 = build_function_declarator (args,
3297 build_id_declarator (NULL_TREE));
3298 return c_parser_direct_declarator_inner (parser, *seen_id,
3299 inner);
3302 /* A parenthesized declarator. */
3303 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3304 if (inner != NULL && attrs != NULL)
3305 inner = build_attrs_declarator (attrs, inner);
3306 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3308 c_parser_consume_token (parser);
3309 if (inner == NULL)
3310 return NULL;
3311 else
3312 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3314 else
3316 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3317 "expected %<)%>");
3318 return NULL;
3321 else
3323 if (kind == C_DTR_NORMAL)
3325 c_parser_error (parser, "expected identifier or %<(%>");
3326 return NULL;
3328 else
3329 return build_id_declarator (NULL_TREE);
3333 /* Parse part of a direct declarator or direct abstract declarator,
3334 given that some (in INNER) has already been parsed; ID_PRESENT is
3335 true if an identifier is present, false for an abstract
3336 declarator. */
3338 static struct c_declarator *
3339 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3340 struct c_declarator *inner)
3342 /* Parse a sequence of array declarators and parameter lists. */
3343 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3345 location_t brace_loc = c_parser_peek_token (parser)->location;
3346 struct c_declarator *declarator;
3347 struct c_declspecs *quals_attrs = build_null_declspecs ();
3348 bool static_seen;
3349 bool star_seen;
3350 struct c_expr dimen;
3351 dimen.value = NULL_TREE;
3352 dimen.original_code = ERROR_MARK;
3353 dimen.original_type = NULL_TREE;
3354 c_parser_consume_token (parser);
3355 c_parser_declspecs (parser, quals_attrs, false, false, true,
3356 false, false, cla_prefer_id);
3357 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3358 if (static_seen)
3359 c_parser_consume_token (parser);
3360 if (static_seen && !quals_attrs->declspecs_seen_p)
3361 c_parser_declspecs (parser, quals_attrs, false, false, true,
3362 false, false, cla_prefer_id);
3363 if (!quals_attrs->declspecs_seen_p)
3364 quals_attrs = NULL;
3365 /* If "static" is present, there must be an array dimension.
3366 Otherwise, there may be a dimension, "*", or no
3367 dimension. */
3368 if (static_seen)
3370 star_seen = false;
3371 dimen = c_parser_expr_no_commas (parser, NULL);
3373 else
3375 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3377 dimen.value = NULL_TREE;
3378 star_seen = false;
3380 else if (flag_cilkplus
3381 && c_parser_next_token_is (parser, CPP_COLON))
3383 dimen.value = error_mark_node;
3384 star_seen = false;
3385 error_at (c_parser_peek_token (parser)->location,
3386 "array notations cannot be used in declaration");
3387 c_parser_consume_token (parser);
3389 else if (c_parser_next_token_is (parser, CPP_MULT))
3391 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3393 dimen.value = NULL_TREE;
3394 star_seen = true;
3395 c_parser_consume_token (parser);
3397 else
3399 star_seen = false;
3400 dimen = c_parser_expr_no_commas (parser, NULL);
3403 else
3405 star_seen = false;
3406 dimen = c_parser_expr_no_commas (parser, NULL);
3409 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3410 c_parser_consume_token (parser);
3411 else if (flag_cilkplus
3412 && c_parser_next_token_is (parser, CPP_COLON))
3414 error_at (c_parser_peek_token (parser)->location,
3415 "array notations cannot be used in declaration");
3416 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3417 return NULL;
3419 else
3421 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3422 "expected %<]%>");
3423 return NULL;
3425 if (dimen.value)
3426 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3427 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3428 static_seen, star_seen);
3429 if (declarator == NULL)
3430 return NULL;
3431 inner = set_array_declarator_inner (declarator, inner);
3432 return c_parser_direct_declarator_inner (parser, id_present, inner);
3434 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3436 tree attrs;
3437 struct c_arg_info *args;
3438 c_parser_consume_token (parser);
3439 attrs = c_parser_attributes (parser);
3440 args = c_parser_parms_declarator (parser, id_present, attrs);
3441 if (args == NULL)
3442 return NULL;
3443 else
3445 inner = build_function_declarator (args, inner);
3446 return c_parser_direct_declarator_inner (parser, id_present, inner);
3449 return inner;
3452 /* Parse a parameter list or identifier list, including the closing
3453 parenthesis but not the opening one. ATTRS are the attributes at
3454 the start of the list. ID_LIST_OK is true if an identifier list is
3455 acceptable; such a list must not have attributes at the start. */
3457 static struct c_arg_info *
3458 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3460 push_scope ();
3461 declare_parm_level ();
3462 /* If the list starts with an identifier, it is an identifier list.
3463 Otherwise, it is either a prototype list or an empty list. */
3464 if (id_list_ok
3465 && !attrs
3466 && c_parser_next_token_is (parser, CPP_NAME)
3467 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3469 /* Look ahead to detect typos in type names. */
3470 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3471 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3472 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3473 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3475 tree list = NULL_TREE, *nextp = &list;
3476 while (c_parser_next_token_is (parser, CPP_NAME)
3477 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3479 *nextp = build_tree_list (NULL_TREE,
3480 c_parser_peek_token (parser)->value);
3481 nextp = & TREE_CHAIN (*nextp);
3482 c_parser_consume_token (parser);
3483 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3484 break;
3485 c_parser_consume_token (parser);
3486 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3488 c_parser_error (parser, "expected identifier");
3489 break;
3492 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3494 struct c_arg_info *ret = build_arg_info ();
3495 ret->types = list;
3496 c_parser_consume_token (parser);
3497 pop_scope ();
3498 return ret;
3500 else
3502 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3503 "expected %<)%>");
3504 pop_scope ();
3505 return NULL;
3508 else
3510 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3511 NULL);
3512 pop_scope ();
3513 return ret;
3517 /* Parse a parameter list (possibly empty), including the closing
3518 parenthesis but not the opening one. ATTRS are the attributes at
3519 the start of the list. EXPR is NULL or an expression that needs to
3520 be evaluated for the side effects of array size expressions in the
3521 parameters. */
3523 static struct c_arg_info *
3524 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3526 bool bad_parm = false;
3528 /* ??? Following the old parser, forward parameter declarations may
3529 use abstract declarators, and if no real parameter declarations
3530 follow the forward declarations then this is not diagnosed. Also
3531 note as above that attributes are ignored as the only contents of
3532 the parentheses, or as the only contents after forward
3533 declarations. */
3534 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3536 struct c_arg_info *ret = build_arg_info ();
3537 c_parser_consume_token (parser);
3538 return ret;
3540 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3542 struct c_arg_info *ret = build_arg_info ();
3544 if (flag_allow_parameterless_variadic_functions)
3546 /* F (...) is allowed. */
3547 ret->types = NULL_TREE;
3549 else
3551 /* Suppress -Wold-style-definition for this case. */
3552 ret->types = error_mark_node;
3553 error_at (c_parser_peek_token (parser)->location,
3554 "ISO C requires a named argument before %<...%>");
3556 c_parser_consume_token (parser);
3557 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3559 c_parser_consume_token (parser);
3560 return ret;
3562 else
3564 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3565 "expected %<)%>");
3566 return NULL;
3569 /* Nonempty list of parameters, either terminated with semicolon
3570 (forward declarations; recurse) or with close parenthesis (normal
3571 function) or with ", ... )" (variadic function). */
3572 while (true)
3574 /* Parse a parameter. */
3575 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3576 attrs = NULL_TREE;
3577 if (parm == NULL)
3578 bad_parm = true;
3579 else
3580 push_parm_decl (parm, &expr);
3581 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3583 tree new_attrs;
3584 c_parser_consume_token (parser);
3585 mark_forward_parm_decls ();
3586 new_attrs = c_parser_attributes (parser);
3587 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3589 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3591 c_parser_consume_token (parser);
3592 if (bad_parm)
3593 return NULL;
3594 else
3595 return get_parm_info (false, expr);
3597 if (!c_parser_require (parser, CPP_COMMA,
3598 "expected %<;%>, %<,%> or %<)%>"))
3600 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3601 return NULL;
3603 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3605 c_parser_consume_token (parser);
3606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3608 c_parser_consume_token (parser);
3609 if (bad_parm)
3610 return NULL;
3611 else
3612 return get_parm_info (true, expr);
3614 else
3616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3617 "expected %<)%>");
3618 return NULL;
3624 /* Parse a parameter declaration. ATTRS are the attributes at the
3625 start of the declaration if it is the first parameter. */
3627 static struct c_parm *
3628 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3630 struct c_declspecs *specs;
3631 struct c_declarator *declarator;
3632 tree prefix_attrs;
3633 tree postfix_attrs = NULL_TREE;
3634 bool dummy = false;
3636 /* Accept #pragmas between parameter declarations. */
3637 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3638 c_parser_pragma (parser, pragma_param);
3640 if (!c_parser_next_token_starts_declspecs (parser))
3642 c_token *token = c_parser_peek_token (parser);
3643 if (parser->error)
3644 return NULL;
3645 c_parser_set_source_position_from_token (token);
3646 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3648 error_at (token->location, "unknown type name %qE", token->value);
3649 parser->error = true;
3651 /* ??? In some Objective-C cases '...' isn't applicable so there
3652 should be a different message. */
3653 else
3654 c_parser_error (parser,
3655 "expected declaration specifiers or %<...%>");
3656 c_parser_skip_to_end_of_parameter (parser);
3657 return NULL;
3659 specs = build_null_declspecs ();
3660 if (attrs)
3662 declspecs_add_attrs (input_location, specs, attrs);
3663 attrs = NULL_TREE;
3665 c_parser_declspecs (parser, specs, true, true, true, true, false,
3666 cla_nonabstract_decl);
3667 finish_declspecs (specs);
3668 pending_xref_error ();
3669 prefix_attrs = specs->attrs;
3670 specs->attrs = NULL_TREE;
3671 declarator = c_parser_declarator (parser,
3672 specs->typespec_kind != ctsk_none,
3673 C_DTR_PARM, &dummy);
3674 if (declarator == NULL)
3676 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3677 return NULL;
3679 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3680 postfix_attrs = c_parser_attributes (parser);
3681 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3682 declarator);
3685 /* Parse a string literal in an asm expression. It should not be
3686 translated, and wide string literals are an error although
3687 permitted by the syntax. This is a GNU extension.
3689 asm-string-literal:
3690 string-literal
3692 ??? At present, following the old parser, the caller needs to have
3693 set lex_untranslated_string to 1. It would be better to follow the
3694 C++ parser rather than using this kludge. */
3696 static tree
3697 c_parser_asm_string_literal (c_parser *parser)
3699 tree str;
3700 int save_flag = warn_overlength_strings;
3701 warn_overlength_strings = 0;
3702 if (c_parser_next_token_is (parser, CPP_STRING))
3704 str = c_parser_peek_token (parser)->value;
3705 c_parser_consume_token (parser);
3707 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3709 error_at (c_parser_peek_token (parser)->location,
3710 "wide string literal in %<asm%>");
3711 str = build_string (1, "");
3712 c_parser_consume_token (parser);
3714 else
3716 c_parser_error (parser, "expected string literal");
3717 str = NULL_TREE;
3719 warn_overlength_strings = save_flag;
3720 return str;
3723 /* Parse a simple asm expression. This is used in restricted
3724 contexts, where a full expression with inputs and outputs does not
3725 make sense. This is a GNU extension.
3727 simple-asm-expr:
3728 asm ( asm-string-literal )
3731 static tree
3732 c_parser_simple_asm_expr (c_parser *parser)
3734 tree str;
3735 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3736 /* ??? Follow the C++ parser rather than using the
3737 lex_untranslated_string kludge. */
3738 parser->lex_untranslated_string = true;
3739 c_parser_consume_token (parser);
3740 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3742 parser->lex_untranslated_string = false;
3743 return NULL_TREE;
3745 str = c_parser_asm_string_literal (parser);
3746 parser->lex_untranslated_string = false;
3747 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3749 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3750 return NULL_TREE;
3752 return str;
3755 static tree
3756 c_parser_attribute_any_word (c_parser *parser)
3758 tree attr_name = NULL_TREE;
3760 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3762 /* ??? See comment above about what keywords are accepted here. */
3763 bool ok;
3764 switch (c_parser_peek_token (parser)->keyword)
3766 case RID_STATIC:
3767 case RID_UNSIGNED:
3768 case RID_LONG:
3769 case RID_CONST:
3770 case RID_EXTERN:
3771 case RID_REGISTER:
3772 case RID_TYPEDEF:
3773 case RID_SHORT:
3774 case RID_INLINE:
3775 case RID_NORETURN:
3776 case RID_VOLATILE:
3777 case RID_SIGNED:
3778 case RID_AUTO:
3779 case RID_RESTRICT:
3780 case RID_COMPLEX:
3781 case RID_THREAD:
3782 case RID_INT:
3783 case RID_CHAR:
3784 case RID_FLOAT:
3785 case RID_DOUBLE:
3786 case RID_VOID:
3787 case RID_DFLOAT32:
3788 case RID_DFLOAT64:
3789 case RID_DFLOAT128:
3790 case RID_BOOL:
3791 case RID_FRACT:
3792 case RID_ACCUM:
3793 case RID_SAT:
3794 case RID_TRANSACTION_ATOMIC:
3795 case RID_TRANSACTION_CANCEL:
3796 case RID_ATOMIC:
3797 case RID_AUTO_TYPE:
3798 case RID_INT_N_0:
3799 case RID_INT_N_1:
3800 case RID_INT_N_2:
3801 case RID_INT_N_3:
3802 ok = true;
3803 break;
3804 default:
3805 ok = false;
3806 break;
3808 if (!ok)
3809 return NULL_TREE;
3811 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3812 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3814 else if (c_parser_next_token_is (parser, CPP_NAME))
3815 attr_name = c_parser_peek_token (parser)->value;
3817 return attr_name;
3820 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3821 "__vector" or "__vector__." */
3823 static inline bool
3824 is_cilkplus_vector_p (tree name)
3826 if (flag_cilkplus && is_attribute_p ("vector", name))
3827 return true;
3828 return false;
3831 #define CILK_SIMD_FN_CLAUSE_MASK \
3832 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3833 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3834 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3835 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3836 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3838 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3839 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3840 pushed into the token list.
3841 Syntax:
3842 vector
3843 vector (<vector attributes>). */
3845 static void
3846 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3848 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3850 int paren_scope = 0;
3851 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3852 /* Consume the "vector" token. */
3853 c_parser_consume_token (parser);
3855 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3857 c_parser_consume_token (parser);
3858 paren_scope++;
3860 while (paren_scope > 0)
3862 c_token *token = c_parser_peek_token (parser);
3863 if (token->type == CPP_OPEN_PAREN)
3864 paren_scope++;
3865 else if (token->type == CPP_CLOSE_PAREN)
3866 paren_scope--;
3867 /* Do not push the last ')' since we are not pushing the '('. */
3868 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3869 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3870 c_parser_consume_token (parser);
3873 /* Since we are converting an attribute to a pragma, we need to end the
3874 attribute with PRAGMA_EOL. */
3875 c_token eol_token;
3876 memset (&eol_token, 0, sizeof (eol_token));
3877 eol_token.type = CPP_PRAGMA_EOL;
3878 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3881 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3883 static void
3884 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3886 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3888 /* c_parser_attributes is called in several places, so if these EOF
3889 tokens are already inserted, then don't do them again. */
3890 if (last_token.type == CPP_EOF)
3891 return;
3893 /* Two CPP_EOF token are added as a safety net since the normal C
3894 front-end has two token look-ahead. */
3895 c_token eof_token;
3896 eof_token.type = CPP_EOF;
3897 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3898 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3901 /* Parse (possibly empty) attributes. This is a GNU extension.
3903 attributes:
3904 empty
3905 attributes attribute
3907 attribute:
3908 __attribute__ ( ( attribute-list ) )
3910 attribute-list:
3911 attrib
3912 attribute_list , attrib
3914 attrib:
3915 empty
3916 any-word
3917 any-word ( identifier )
3918 any-word ( identifier , nonempty-expr-list )
3919 any-word ( expr-list )
3921 where the "identifier" must not be declared as a type, and
3922 "any-word" may be any identifier (including one declared as a
3923 type), a reserved word storage class specifier, type specifier or
3924 type qualifier. ??? This still leaves out most reserved keywords
3925 (following the old parser), shouldn't we include them, and why not
3926 allow identifiers declared as types to start the arguments? */
3928 static tree
3929 c_parser_attributes (c_parser *parser)
3931 tree attrs = NULL_TREE;
3932 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3934 /* ??? Follow the C++ parser rather than using the
3935 lex_untranslated_string kludge. */
3936 parser->lex_untranslated_string = true;
3937 c_parser_consume_token (parser);
3938 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3940 parser->lex_untranslated_string = false;
3941 return attrs;
3943 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3945 parser->lex_untranslated_string = false;
3946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3947 return attrs;
3949 /* Parse the attribute list. */
3950 while (c_parser_next_token_is (parser, CPP_COMMA)
3951 || c_parser_next_token_is (parser, CPP_NAME)
3952 || c_parser_next_token_is (parser, CPP_KEYWORD))
3954 tree attr, attr_name, attr_args;
3955 vec<tree, va_gc> *expr_list;
3956 if (c_parser_next_token_is (parser, CPP_COMMA))
3958 c_parser_consume_token (parser);
3959 continue;
3962 attr_name = c_parser_attribute_any_word (parser);
3963 if (attr_name == NULL)
3964 break;
3965 if (is_cilkplus_vector_p (attr_name))
3967 c_token *v_token = c_parser_peek_token (parser);
3968 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3969 continue;
3971 c_parser_consume_token (parser);
3972 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3974 attr = build_tree_list (attr_name, NULL_TREE);
3975 attrs = chainon (attrs, attr);
3976 continue;
3978 c_parser_consume_token (parser);
3979 /* Parse the attribute contents. If they start with an
3980 identifier which is followed by a comma or close
3981 parenthesis, then the arguments start with that
3982 identifier; otherwise they are an expression list.
3983 In objective-c the identifier may be a classname. */
3984 if (c_parser_next_token_is (parser, CPP_NAME)
3985 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3986 || (c_dialect_objc ()
3987 && c_parser_peek_token (parser)->id_kind
3988 == C_ID_CLASSNAME))
3989 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3990 || (c_parser_peek_2nd_token (parser)->type
3991 == CPP_CLOSE_PAREN))
3992 && (attribute_takes_identifier_p (attr_name)
3993 || (c_dialect_objc ()
3994 && c_parser_peek_token (parser)->id_kind
3995 == C_ID_CLASSNAME)))
3997 tree arg1 = c_parser_peek_token (parser)->value;
3998 c_parser_consume_token (parser);
3999 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4000 attr_args = build_tree_list (NULL_TREE, arg1);
4001 else
4003 tree tree_list;
4004 c_parser_consume_token (parser);
4005 expr_list = c_parser_expr_list (parser, false, true,
4006 NULL, NULL, NULL, NULL);
4007 tree_list = build_tree_list_vec (expr_list);
4008 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4009 release_tree_vector (expr_list);
4012 else
4014 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4015 attr_args = NULL_TREE;
4016 else
4018 expr_list = c_parser_expr_list (parser, false, true,
4019 NULL, NULL, NULL, NULL);
4020 attr_args = build_tree_list_vec (expr_list);
4021 release_tree_vector (expr_list);
4024 attr = build_tree_list (attr_name, attr_args);
4025 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4026 c_parser_consume_token (parser);
4027 else
4029 parser->lex_untranslated_string = false;
4030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4031 "expected %<)%>");
4032 return attrs;
4034 attrs = chainon (attrs, attr);
4036 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4037 c_parser_consume_token (parser);
4038 else
4040 parser->lex_untranslated_string = false;
4041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4042 "expected %<)%>");
4043 return attrs;
4045 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4046 c_parser_consume_token (parser);
4047 else
4049 parser->lex_untranslated_string = false;
4050 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4051 "expected %<)%>");
4052 return attrs;
4054 parser->lex_untranslated_string = false;
4057 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4058 c_finish_cilk_simd_fn_tokens (parser);
4059 return attrs;
4062 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4064 type-name:
4065 specifier-qualifier-list abstract-declarator[opt]
4068 static struct c_type_name *
4069 c_parser_type_name (c_parser *parser)
4071 struct c_declspecs *specs = build_null_declspecs ();
4072 struct c_declarator *declarator;
4073 struct c_type_name *ret;
4074 bool dummy = false;
4075 c_parser_declspecs (parser, specs, false, true, true, false, false,
4076 cla_prefer_type);
4077 if (!specs->declspecs_seen_p)
4079 c_parser_error (parser, "expected specifier-qualifier-list");
4080 return NULL;
4082 if (specs->type != error_mark_node)
4084 pending_xref_error ();
4085 finish_declspecs (specs);
4087 declarator = c_parser_declarator (parser,
4088 specs->typespec_kind != ctsk_none,
4089 C_DTR_ABSTRACT, &dummy);
4090 if (declarator == NULL)
4091 return NULL;
4092 ret = XOBNEW (&parser_obstack, struct c_type_name);
4093 ret->specs = specs;
4094 ret->declarator = declarator;
4095 return ret;
4098 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4100 initializer:
4101 assignment-expression
4102 { initializer-list }
4103 { initializer-list , }
4105 initializer-list:
4106 designation[opt] initializer
4107 initializer-list , designation[opt] initializer
4109 designation:
4110 designator-list =
4112 designator-list:
4113 designator
4114 designator-list designator
4116 designator:
4117 array-designator
4118 . identifier
4120 array-designator:
4121 [ constant-expression ]
4123 GNU extensions:
4125 initializer:
4128 designation:
4129 array-designator
4130 identifier :
4132 array-designator:
4133 [ constant-expression ... constant-expression ]
4135 Any expression without commas is accepted in the syntax for the
4136 constant-expressions, with non-constant expressions rejected later.
4138 This function is only used for top-level initializers; for nested
4139 ones, see c_parser_initval. */
4141 static struct c_expr
4142 c_parser_initializer (c_parser *parser)
4144 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4145 return c_parser_braced_init (parser, NULL_TREE, false);
4146 else
4148 struct c_expr ret;
4149 location_t loc = c_parser_peek_token (parser)->location;
4150 ret = c_parser_expr_no_commas (parser, NULL);
4151 if (TREE_CODE (ret.value) != STRING_CST
4152 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4153 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4154 return ret;
4158 /* Parse a braced initializer list. TYPE is the type specified for a
4159 compound literal, and NULL_TREE for other initializers and for
4160 nested braced lists. NESTED_P is true for nested braced lists,
4161 false for the list of a compound literal or the list that is the
4162 top-level initializer in a declaration. */
4164 static struct c_expr
4165 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4167 struct c_expr ret;
4168 struct obstack braced_init_obstack;
4169 location_t brace_loc = c_parser_peek_token (parser)->location;
4170 gcc_obstack_init (&braced_init_obstack);
4171 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4172 c_parser_consume_token (parser);
4173 if (nested_p)
4174 push_init_level (brace_loc, 0, &braced_init_obstack);
4175 else
4176 really_start_incremental_init (type);
4177 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4179 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4181 else
4183 /* Parse a non-empty initializer list, possibly with a trailing
4184 comma. */
4185 while (true)
4187 c_parser_initelt (parser, &braced_init_obstack);
4188 if (parser->error)
4189 break;
4190 if (c_parser_next_token_is (parser, CPP_COMMA))
4191 c_parser_consume_token (parser);
4192 else
4193 break;
4194 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4195 break;
4198 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4200 ret.value = error_mark_node;
4201 ret.original_code = ERROR_MARK;
4202 ret.original_type = NULL;
4203 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4204 pop_init_level (brace_loc, 0, &braced_init_obstack);
4205 obstack_free (&braced_init_obstack, NULL);
4206 return ret;
4208 c_parser_consume_token (parser);
4209 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4210 obstack_free (&braced_init_obstack, NULL);
4211 return ret;
4214 /* Parse a nested initializer, including designators. */
4216 static void
4217 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4219 /* Parse any designator or designator list. A single array
4220 designator may have the subsequent "=" omitted in GNU C, but a
4221 longer list or a structure member designator may not. */
4222 if (c_parser_next_token_is (parser, CPP_NAME)
4223 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4225 /* Old-style structure member designator. */
4226 set_init_label (c_parser_peek_token (parser)->location,
4227 c_parser_peek_token (parser)->value,
4228 braced_init_obstack);
4229 /* Use the colon as the error location. */
4230 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4231 "obsolete use of designated initializer with %<:%>");
4232 c_parser_consume_token (parser);
4233 c_parser_consume_token (parser);
4235 else
4237 /* des_seen is 0 if there have been no designators, 1 if there
4238 has been a single array designator and 2 otherwise. */
4239 int des_seen = 0;
4240 /* Location of a designator. */
4241 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4242 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4243 || c_parser_next_token_is (parser, CPP_DOT))
4245 int des_prev = des_seen;
4246 if (!des_seen)
4247 des_loc = c_parser_peek_token (parser)->location;
4248 if (des_seen < 2)
4249 des_seen++;
4250 if (c_parser_next_token_is (parser, CPP_DOT))
4252 des_seen = 2;
4253 c_parser_consume_token (parser);
4254 if (c_parser_next_token_is (parser, CPP_NAME))
4256 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4257 braced_init_obstack);
4258 c_parser_consume_token (parser);
4260 else
4262 struct c_expr init;
4263 init.value = error_mark_node;
4264 init.original_code = ERROR_MARK;
4265 init.original_type = NULL;
4266 c_parser_error (parser, "expected identifier");
4267 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4268 process_init_element (input_location, init, false,
4269 braced_init_obstack);
4270 return;
4273 else
4275 tree first, second;
4276 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4277 location_t array_index_loc = UNKNOWN_LOCATION;
4278 /* ??? Following the old parser, [ objc-receiver
4279 objc-message-args ] is accepted as an initializer,
4280 being distinguished from a designator by what follows
4281 the first assignment expression inside the square
4282 brackets, but after a first array designator a
4283 subsequent square bracket is for Objective-C taken to
4284 start an expression, using the obsolete form of
4285 designated initializer without '=', rather than
4286 possibly being a second level of designation: in LALR
4287 terms, the '[' is shifted rather than reducing
4288 designator to designator-list. */
4289 if (des_prev == 1 && c_dialect_objc ())
4291 des_seen = des_prev;
4292 break;
4294 if (des_prev == 0 && c_dialect_objc ())
4296 /* This might be an array designator or an
4297 Objective-C message expression. If the former,
4298 continue parsing here; if the latter, parse the
4299 remainder of the initializer given the starting
4300 primary-expression. ??? It might make sense to
4301 distinguish when des_prev == 1 as well; see
4302 previous comment. */
4303 tree rec, args;
4304 struct c_expr mexpr;
4305 c_parser_consume_token (parser);
4306 if (c_parser_peek_token (parser)->type == CPP_NAME
4307 && ((c_parser_peek_token (parser)->id_kind
4308 == C_ID_TYPENAME)
4309 || (c_parser_peek_token (parser)->id_kind
4310 == C_ID_CLASSNAME)))
4312 /* Type name receiver. */
4313 tree id = c_parser_peek_token (parser)->value;
4314 c_parser_consume_token (parser);
4315 rec = objc_get_class_reference (id);
4316 goto parse_message_args;
4318 first = c_parser_expr_no_commas (parser, NULL).value;
4319 mark_exp_read (first);
4320 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4321 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4322 goto array_desig_after_first;
4323 /* Expression receiver. So far only one part
4324 without commas has been parsed; there might be
4325 more of the expression. */
4326 rec = first;
4327 while (c_parser_next_token_is (parser, CPP_COMMA))
4329 struct c_expr next;
4330 location_t comma_loc, exp_loc;
4331 comma_loc = c_parser_peek_token (parser)->location;
4332 c_parser_consume_token (parser);
4333 exp_loc = c_parser_peek_token (parser)->location;
4334 next = c_parser_expr_no_commas (parser, NULL);
4335 next = convert_lvalue_to_rvalue (exp_loc, next,
4336 true, true);
4337 rec = build_compound_expr (comma_loc, rec, next.value);
4339 parse_message_args:
4340 /* Now parse the objc-message-args. */
4341 args = c_parser_objc_message_args (parser);
4342 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4343 "expected %<]%>");
4344 mexpr.value
4345 = objc_build_message_expr (rec, args);
4346 mexpr.original_code = ERROR_MARK;
4347 mexpr.original_type = NULL;
4348 /* Now parse and process the remainder of the
4349 initializer, starting with this message
4350 expression as a primary-expression. */
4351 c_parser_initval (parser, &mexpr, braced_init_obstack);
4352 return;
4354 c_parser_consume_token (parser);
4355 array_index_loc = c_parser_peek_token (parser)->location;
4356 first = c_parser_expr_no_commas (parser, NULL).value;
4357 mark_exp_read (first);
4358 array_desig_after_first:
4359 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4361 ellipsis_loc = c_parser_peek_token (parser)->location;
4362 c_parser_consume_token (parser);
4363 second = c_parser_expr_no_commas (parser, NULL).value;
4364 mark_exp_read (second);
4366 else
4367 second = NULL_TREE;
4368 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4370 c_parser_consume_token (parser);
4371 set_init_index (array_index_loc, first, second,
4372 braced_init_obstack);
4373 if (second)
4374 pedwarn (ellipsis_loc, OPT_Wpedantic,
4375 "ISO C forbids specifying range of elements to initialize");
4377 else
4378 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4379 "expected %<]%>");
4382 if (des_seen >= 1)
4384 if (c_parser_next_token_is (parser, CPP_EQ))
4386 pedwarn_c90 (des_loc, OPT_Wpedantic,
4387 "ISO C90 forbids specifying subobject "
4388 "to initialize");
4389 c_parser_consume_token (parser);
4391 else
4393 if (des_seen == 1)
4394 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4395 "obsolete use of designated initializer without %<=%>");
4396 else
4398 struct c_expr init;
4399 init.value = error_mark_node;
4400 init.original_code = ERROR_MARK;
4401 init.original_type = NULL;
4402 c_parser_error (parser, "expected %<=%>");
4403 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4404 process_init_element (input_location, init, false,
4405 braced_init_obstack);
4406 return;
4411 c_parser_initval (parser, NULL, braced_init_obstack);
4414 /* Parse a nested initializer; as c_parser_initializer but parses
4415 initializers within braced lists, after any designators have been
4416 applied. If AFTER is not NULL then it is an Objective-C message
4417 expression which is the primary-expression starting the
4418 initializer. */
4420 static void
4421 c_parser_initval (c_parser *parser, struct c_expr *after,
4422 struct obstack * braced_init_obstack)
4424 struct c_expr init;
4425 gcc_assert (!after || c_dialect_objc ());
4426 location_t loc = c_parser_peek_token (parser)->location;
4428 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4429 init = c_parser_braced_init (parser, NULL_TREE, true);
4430 else
4432 init = c_parser_expr_no_commas (parser, after);
4433 if (init.value != NULL_TREE
4434 && TREE_CODE (init.value) != STRING_CST
4435 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4436 init = convert_lvalue_to_rvalue (loc, init, true, true);
4438 process_init_element (loc, init, false, braced_init_obstack);
4441 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4442 C99 6.8.2).
4444 compound-statement:
4445 { block-item-list[opt] }
4446 { label-declarations block-item-list }
4448 block-item-list:
4449 block-item
4450 block-item-list block-item
4452 block-item:
4453 nested-declaration
4454 statement
4456 nested-declaration:
4457 declaration
4459 GNU extensions:
4461 compound-statement:
4462 { label-declarations block-item-list }
4464 nested-declaration:
4465 __extension__ nested-declaration
4466 nested-function-definition
4468 label-declarations:
4469 label-declaration
4470 label-declarations label-declaration
4472 label-declaration:
4473 __label__ identifier-list ;
4475 Allowing the mixing of declarations and code is new in C99. The
4476 GNU syntax also permits (not shown above) labels at the end of
4477 compound statements, which yield an error. We don't allow labels
4478 on declarations; this might seem like a natural extension, but
4479 there would be a conflict between attributes on the label and
4480 prefix attributes on the declaration. ??? The syntax follows the
4481 old parser in requiring something after label declarations.
4482 Although they are erroneous if the labels declared aren't defined,
4483 is it useful for the syntax to be this way?
4485 OpenMP:
4487 block-item:
4488 openmp-directive
4490 openmp-directive:
4491 barrier-directive
4492 flush-directive
4493 taskwait-directive
4494 taskyield-directive
4495 cancel-directive
4496 cancellation-point-directive */
4498 static tree
4499 c_parser_compound_statement (c_parser *parser)
4501 tree stmt;
4502 location_t brace_loc;
4503 brace_loc = c_parser_peek_token (parser)->location;
4504 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4506 /* Ensure a scope is entered and left anyway to avoid confusion
4507 if we have just prepared to enter a function body. */
4508 stmt = c_begin_compound_stmt (true);
4509 c_end_compound_stmt (brace_loc, stmt, true);
4510 return error_mark_node;
4512 stmt = c_begin_compound_stmt (true);
4513 c_parser_compound_statement_nostart (parser);
4515 /* If the compound stmt contains array notations, then we expand them. */
4516 if (flag_cilkplus && contains_array_notation_expr (stmt))
4517 stmt = expand_array_notation_exprs (stmt);
4518 return c_end_compound_stmt (brace_loc, stmt, true);
4521 /* Parse a compound statement except for the opening brace. This is
4522 used for parsing both compound statements and statement expressions
4523 (which follow different paths to handling the opening). */
4525 static void
4526 c_parser_compound_statement_nostart (c_parser *parser)
4528 bool last_stmt = false;
4529 bool last_label = false;
4530 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4531 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4532 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4534 c_parser_consume_token (parser);
4535 return;
4537 mark_valid_location_for_stdc_pragma (true);
4538 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4540 /* Read zero or more forward-declarations for labels that nested
4541 functions can jump to. */
4542 mark_valid_location_for_stdc_pragma (false);
4543 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4545 label_loc = c_parser_peek_token (parser)->location;
4546 c_parser_consume_token (parser);
4547 /* Any identifiers, including those declared as type names,
4548 are OK here. */
4549 while (true)
4551 tree label;
4552 if (c_parser_next_token_is_not (parser, CPP_NAME))
4554 c_parser_error (parser, "expected identifier");
4555 break;
4557 label
4558 = declare_label (c_parser_peek_token (parser)->value);
4559 C_DECLARED_LABEL_FLAG (label) = 1;
4560 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4561 c_parser_consume_token (parser);
4562 if (c_parser_next_token_is (parser, CPP_COMMA))
4563 c_parser_consume_token (parser);
4564 else
4565 break;
4567 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4569 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4571 /* We must now have at least one statement, label or declaration. */
4572 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4574 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4575 c_parser_error (parser, "expected declaration or statement");
4576 c_parser_consume_token (parser);
4577 return;
4579 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4581 location_t loc = c_parser_peek_token (parser)->location;
4582 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4583 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4584 || (c_parser_next_token_is (parser, CPP_NAME)
4585 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4587 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4588 label_loc = c_parser_peek_2nd_token (parser)->location;
4589 else
4590 label_loc = c_parser_peek_token (parser)->location;
4591 last_label = true;
4592 last_stmt = false;
4593 mark_valid_location_for_stdc_pragma (false);
4594 c_parser_label (parser);
4596 else if (!last_label
4597 && c_parser_next_tokens_start_declaration (parser))
4599 last_label = false;
4600 mark_valid_location_for_stdc_pragma (false);
4601 c_parser_declaration_or_fndef (parser, true, true, true, true,
4602 true, NULL, vNULL);
4603 if (last_stmt)
4604 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4605 "ISO C90 forbids mixed declarations and code");
4606 last_stmt = false;
4608 else if (!last_label
4609 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4611 /* __extension__ can start a declaration, but is also an
4612 unary operator that can start an expression. Consume all
4613 but the last of a possible series of __extension__ to
4614 determine which. */
4615 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4616 && (c_parser_peek_2nd_token (parser)->keyword
4617 == RID_EXTENSION))
4618 c_parser_consume_token (parser);
4619 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4621 int ext;
4622 ext = disable_extension_diagnostics ();
4623 c_parser_consume_token (parser);
4624 last_label = false;
4625 mark_valid_location_for_stdc_pragma (false);
4626 c_parser_declaration_or_fndef (parser, true, true, true, true,
4627 true, NULL, vNULL);
4628 /* Following the old parser, __extension__ does not
4629 disable this diagnostic. */
4630 restore_extension_diagnostics (ext);
4631 if (last_stmt)
4632 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4633 "ISO C90 forbids mixed declarations and code");
4634 last_stmt = false;
4636 else
4637 goto statement;
4639 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4641 /* External pragmas, and some omp pragmas, are not associated
4642 with regular c code, and so are not to be considered statements
4643 syntactically. This ensures that the user doesn't put them
4644 places that would turn into syntax errors if the directive
4645 were ignored. */
4646 if (c_parser_pragma (parser, pragma_compound))
4647 last_label = false, last_stmt = true;
4649 else if (c_parser_next_token_is (parser, CPP_EOF))
4651 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4652 c_parser_error (parser, "expected declaration or statement");
4653 return;
4655 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4657 if (parser->in_if_block)
4659 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4660 error_at (loc, """expected %<}%> before %<else%>");
4661 return;
4663 else
4665 error_at (loc, "%<else%> without a previous %<if%>");
4666 c_parser_consume_token (parser);
4667 continue;
4670 else
4672 statement:
4673 last_label = false;
4674 last_stmt = true;
4675 mark_valid_location_for_stdc_pragma (false);
4676 c_parser_statement_after_labels (parser);
4679 parser->error = false;
4681 if (last_label)
4682 error_at (label_loc, "label at end of compound statement");
4683 c_parser_consume_token (parser);
4684 /* Restore the value we started with. */
4685 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4688 /* Parse all consecutive labels. */
4690 static void
4691 c_parser_all_labels (c_parser *parser)
4693 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4694 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4695 || (c_parser_next_token_is (parser, CPP_NAME)
4696 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4697 c_parser_label (parser);
4700 /* Parse a label (C90 6.6.1, C99 6.8.1).
4702 label:
4703 identifier : attributes[opt]
4704 case constant-expression :
4705 default :
4707 GNU extensions:
4709 label:
4710 case constant-expression ... constant-expression :
4712 The use of attributes on labels is a GNU extension. The syntax in
4713 GNU C accepts any expressions without commas, non-constant
4714 expressions being rejected later. */
4716 static void
4717 c_parser_label (c_parser *parser)
4719 location_t loc1 = c_parser_peek_token (parser)->location;
4720 tree label = NULL_TREE;
4721 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4723 tree exp1, exp2;
4724 c_parser_consume_token (parser);
4725 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4726 if (c_parser_next_token_is (parser, CPP_COLON))
4728 c_parser_consume_token (parser);
4729 label = do_case (loc1, exp1, NULL_TREE);
4731 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4733 c_parser_consume_token (parser);
4734 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4735 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4736 label = do_case (loc1, exp1, exp2);
4738 else
4739 c_parser_error (parser, "expected %<:%> or %<...%>");
4741 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4743 c_parser_consume_token (parser);
4744 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4745 label = do_case (loc1, NULL_TREE, NULL_TREE);
4747 else
4749 tree name = c_parser_peek_token (parser)->value;
4750 tree tlab;
4751 tree attrs;
4752 location_t loc2 = c_parser_peek_token (parser)->location;
4753 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4754 c_parser_consume_token (parser);
4755 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4756 c_parser_consume_token (parser);
4757 attrs = c_parser_attributes (parser);
4758 tlab = define_label (loc2, name);
4759 if (tlab)
4761 decl_attributes (&tlab, attrs, 0);
4762 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4765 if (label)
4767 if (c_parser_next_tokens_start_declaration (parser))
4769 error_at (c_parser_peek_token (parser)->location,
4770 "a label can only be part of a statement and "
4771 "a declaration is not a statement");
4772 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4773 /*static_assert_ok*/ true,
4774 /*empty_ok*/ true, /*nested*/ true,
4775 /*start_attr_ok*/ true, NULL,
4776 vNULL);
4781 /* Parse a statement (C90 6.6, C99 6.8).
4783 statement:
4784 labeled-statement
4785 compound-statement
4786 expression-statement
4787 selection-statement
4788 iteration-statement
4789 jump-statement
4791 labeled-statement:
4792 label statement
4794 expression-statement:
4795 expression[opt] ;
4797 selection-statement:
4798 if-statement
4799 switch-statement
4801 iteration-statement:
4802 while-statement
4803 do-statement
4804 for-statement
4806 jump-statement:
4807 goto identifier ;
4808 continue ;
4809 break ;
4810 return expression[opt] ;
4812 GNU extensions:
4814 statement:
4815 asm-statement
4817 jump-statement:
4818 goto * expression ;
4820 Objective-C:
4822 statement:
4823 objc-throw-statement
4824 objc-try-catch-statement
4825 objc-synchronized-statement
4827 objc-throw-statement:
4828 @throw expression ;
4829 @throw ;
4831 OpenMP:
4833 statement:
4834 openmp-construct
4836 openmp-construct:
4837 parallel-construct
4838 for-construct
4839 simd-construct
4840 for-simd-construct
4841 sections-construct
4842 single-construct
4843 parallel-for-construct
4844 parallel-for-simd-construct
4845 parallel-sections-construct
4846 master-construct
4847 critical-construct
4848 atomic-construct
4849 ordered-construct
4851 parallel-construct:
4852 parallel-directive structured-block
4854 for-construct:
4855 for-directive iteration-statement
4857 simd-construct:
4858 simd-directive iteration-statements
4860 for-simd-construct:
4861 for-simd-directive iteration-statements
4863 sections-construct:
4864 sections-directive section-scope
4866 single-construct:
4867 single-directive structured-block
4869 parallel-for-construct:
4870 parallel-for-directive iteration-statement
4872 parallel-for-simd-construct:
4873 parallel-for-simd-directive iteration-statement
4875 parallel-sections-construct:
4876 parallel-sections-directive section-scope
4878 master-construct:
4879 master-directive structured-block
4881 critical-construct:
4882 critical-directive structured-block
4884 atomic-construct:
4885 atomic-directive expression-statement
4887 ordered-construct:
4888 ordered-directive structured-block
4890 Transactional Memory:
4892 statement:
4893 transaction-statement
4894 transaction-cancel-statement
4897 static void
4898 c_parser_statement (c_parser *parser)
4900 c_parser_all_labels (parser);
4901 c_parser_statement_after_labels (parser);
4904 /* Parse a statement, other than a labeled statement. */
4906 static void
4907 c_parser_statement_after_labels (c_parser *parser)
4909 location_t loc = c_parser_peek_token (parser)->location;
4910 tree stmt = NULL_TREE;
4911 bool in_if_block = parser->in_if_block;
4912 parser->in_if_block = false;
4913 switch (c_parser_peek_token (parser)->type)
4915 case CPP_OPEN_BRACE:
4916 add_stmt (c_parser_compound_statement (parser));
4917 break;
4918 case CPP_KEYWORD:
4919 switch (c_parser_peek_token (parser)->keyword)
4921 case RID_IF:
4922 c_parser_if_statement (parser);
4923 break;
4924 case RID_SWITCH:
4925 c_parser_switch_statement (parser);
4926 break;
4927 case RID_WHILE:
4928 c_parser_while_statement (parser, false);
4929 break;
4930 case RID_DO:
4931 c_parser_do_statement (parser, false);
4932 break;
4933 case RID_FOR:
4934 c_parser_for_statement (parser, false);
4935 break;
4936 case RID_CILK_FOR:
4937 if (!flag_cilkplus)
4939 error_at (c_parser_peek_token (parser)->location,
4940 "-fcilkplus must be enabled to use %<_Cilk_for%>");
4941 c_parser_skip_to_end_of_block_or_statement (parser);
4943 else
4944 c_parser_cilk_for (parser, integer_zero_node);
4945 break;
4946 case RID_CILK_SYNC:
4947 c_parser_consume_token (parser);
4948 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4949 if (!flag_cilkplus)
4950 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4951 else
4952 add_stmt (build_cilk_sync ());
4953 break;
4954 case RID_GOTO:
4955 c_parser_consume_token (parser);
4956 if (c_parser_next_token_is (parser, CPP_NAME))
4958 stmt = c_finish_goto_label (loc,
4959 c_parser_peek_token (parser)->value);
4960 c_parser_consume_token (parser);
4962 else if (c_parser_next_token_is (parser, CPP_MULT))
4964 struct c_expr val;
4966 c_parser_consume_token (parser);
4967 val = c_parser_expression (parser);
4968 val = convert_lvalue_to_rvalue (loc, val, false, true);
4969 stmt = c_finish_goto_ptr (loc, val.value);
4971 else
4972 c_parser_error (parser, "expected identifier or %<*%>");
4973 goto expect_semicolon;
4974 case RID_CONTINUE:
4975 c_parser_consume_token (parser);
4976 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4977 goto expect_semicolon;
4978 case RID_BREAK:
4979 c_parser_consume_token (parser);
4980 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4981 goto expect_semicolon;
4982 case RID_RETURN:
4983 c_parser_consume_token (parser);
4984 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4986 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4987 c_parser_consume_token (parser);
4989 else
4991 location_t xloc = c_parser_peek_token (parser)->location;
4992 struct c_expr expr = c_parser_expression_conv (parser);
4993 mark_exp_read (expr.value);
4994 stmt = c_finish_return (xloc, expr.value, expr.original_type);
4995 goto expect_semicolon;
4997 break;
4998 case RID_ASM:
4999 stmt = c_parser_asm_statement (parser);
5000 break;
5001 case RID_TRANSACTION_ATOMIC:
5002 case RID_TRANSACTION_RELAXED:
5003 stmt = c_parser_transaction (parser,
5004 c_parser_peek_token (parser)->keyword);
5005 break;
5006 case RID_TRANSACTION_CANCEL:
5007 stmt = c_parser_transaction_cancel (parser);
5008 goto expect_semicolon;
5009 case RID_AT_THROW:
5010 gcc_assert (c_dialect_objc ());
5011 c_parser_consume_token (parser);
5012 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5014 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5015 c_parser_consume_token (parser);
5017 else
5019 struct c_expr expr = c_parser_expression (parser);
5020 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5021 expr.value = c_fully_fold (expr.value, false, NULL);
5022 stmt = objc_build_throw_stmt (loc, expr.value);
5023 goto expect_semicolon;
5025 break;
5026 case RID_AT_TRY:
5027 gcc_assert (c_dialect_objc ());
5028 c_parser_objc_try_catch_finally_statement (parser);
5029 break;
5030 case RID_AT_SYNCHRONIZED:
5031 gcc_assert (c_dialect_objc ());
5032 c_parser_objc_synchronized_statement (parser);
5033 break;
5034 default:
5035 goto expr_stmt;
5037 break;
5038 case CPP_SEMICOLON:
5039 c_parser_consume_token (parser);
5040 break;
5041 case CPP_CLOSE_PAREN:
5042 case CPP_CLOSE_SQUARE:
5043 /* Avoid infinite loop in error recovery:
5044 c_parser_skip_until_found stops at a closing nesting
5045 delimiter without consuming it, but here we need to consume
5046 it to proceed further. */
5047 c_parser_error (parser, "expected statement");
5048 c_parser_consume_token (parser);
5049 break;
5050 case CPP_PRAGMA:
5051 c_parser_pragma (parser, pragma_stmt);
5052 break;
5053 default:
5054 expr_stmt:
5055 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5056 expect_semicolon:
5057 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5058 break;
5060 /* Two cases cannot and do not have line numbers associated: If stmt
5061 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5062 cannot hold line numbers. But that's OK because the statement
5063 will either be changed to a MODIFY_EXPR during gimplification of
5064 the statement expr, or discarded. If stmt was compound, but
5065 without new variables, we will have skipped the creation of a
5066 BIND and will have a bare STATEMENT_LIST. But that's OK because
5067 (recursively) all of the component statements should already have
5068 line numbers assigned. ??? Can we discard no-op statements
5069 earlier? */
5070 if (CAN_HAVE_LOCATION_P (stmt)
5071 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5072 SET_EXPR_LOCATION (stmt, loc);
5074 parser->in_if_block = in_if_block;
5077 /* Parse the condition from an if, do, while or for statements. */
5079 static tree
5080 c_parser_condition (c_parser *parser)
5082 location_t loc = c_parser_peek_token (parser)->location;
5083 tree cond;
5084 cond = c_parser_expression_conv (parser).value;
5085 cond = c_objc_common_truthvalue_conversion (loc, cond);
5086 cond = c_fully_fold (cond, false, NULL);
5087 if (warn_sequence_point)
5088 verify_sequence_points (cond);
5089 return cond;
5092 /* Parse a parenthesized condition from an if, do or while statement.
5094 condition:
5095 ( expression )
5097 static tree
5098 c_parser_paren_condition (c_parser *parser)
5100 tree cond;
5101 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5102 return error_mark_node;
5103 cond = c_parser_condition (parser);
5104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5105 return cond;
5108 /* Parse a statement which is a block in C99. */
5110 static tree
5111 c_parser_c99_block_statement (c_parser *parser)
5113 tree block = c_begin_compound_stmt (flag_isoc99);
5114 location_t loc = c_parser_peek_token (parser)->location;
5115 c_parser_statement (parser);
5116 return c_end_compound_stmt (loc, block, flag_isoc99);
5119 /* Parse the body of an if statement. This is just parsing a
5120 statement but (a) it is a block in C99, (b) we track whether the
5121 body is an if statement for the sake of -Wparentheses warnings, (c)
5122 we handle an empty body specially for the sake of -Wempty-body
5123 warnings, and (d) we call parser_compound_statement directly
5124 because c_parser_statement_after_labels resets
5125 parser->in_if_block. */
5127 static tree
5128 c_parser_if_body (c_parser *parser, bool *if_p)
5130 tree block = c_begin_compound_stmt (flag_isoc99);
5131 location_t body_loc = c_parser_peek_token (parser)->location;
5132 c_parser_all_labels (parser);
5133 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5134 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5136 location_t loc = c_parser_peek_token (parser)->location;
5137 add_stmt (build_empty_stmt (loc));
5138 c_parser_consume_token (parser);
5139 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5140 warning_at (loc, OPT_Wempty_body,
5141 "suggest braces around empty body in an %<if%> statement");
5143 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5144 add_stmt (c_parser_compound_statement (parser));
5145 else
5146 c_parser_statement_after_labels (parser);
5147 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5150 /* Parse the else body of an if statement. This is just parsing a
5151 statement but (a) it is a block in C99, (b) we handle an empty body
5152 specially for the sake of -Wempty-body warnings. */
5154 static tree
5155 c_parser_else_body (c_parser *parser)
5157 location_t else_loc = c_parser_peek_token (parser)->location;
5158 tree block = c_begin_compound_stmt (flag_isoc99);
5159 c_parser_all_labels (parser);
5160 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5162 location_t loc = c_parser_peek_token (parser)->location;
5163 warning_at (loc,
5164 OPT_Wempty_body,
5165 "suggest braces around empty body in an %<else%> statement");
5166 add_stmt (build_empty_stmt (loc));
5167 c_parser_consume_token (parser);
5169 else
5170 c_parser_statement_after_labels (parser);
5171 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5174 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5176 if-statement:
5177 if ( expression ) statement
5178 if ( expression ) statement else statement
5181 static void
5182 c_parser_if_statement (c_parser *parser)
5184 tree block;
5185 location_t loc;
5186 tree cond;
5187 bool first_if = false;
5188 tree first_body, second_body;
5189 bool in_if_block;
5190 tree if_stmt;
5192 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5193 c_parser_consume_token (parser);
5194 block = c_begin_compound_stmt (flag_isoc99);
5195 loc = c_parser_peek_token (parser)->location;
5196 cond = c_parser_paren_condition (parser);
5197 in_if_block = parser->in_if_block;
5198 parser->in_if_block = true;
5199 first_body = c_parser_if_body (parser, &first_if);
5200 parser->in_if_block = in_if_block;
5201 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5203 c_parser_consume_token (parser);
5204 second_body = c_parser_else_body (parser);
5206 else
5207 second_body = NULL_TREE;
5208 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5209 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5211 /* If the if statement contains array notations, then we expand them. */
5212 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5213 if_stmt = fix_conditional_array_notations (if_stmt);
5214 add_stmt (if_stmt);
5217 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5219 switch-statement:
5220 switch (expression) statement
5223 static void
5224 c_parser_switch_statement (c_parser *parser)
5226 struct c_expr ce;
5227 tree block, expr, body, save_break;
5228 location_t switch_loc = c_parser_peek_token (parser)->location;
5229 location_t switch_cond_loc;
5230 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5231 c_parser_consume_token (parser);
5232 block = c_begin_compound_stmt (flag_isoc99);
5233 bool explicit_cast_p = false;
5234 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5236 switch_cond_loc = c_parser_peek_token (parser)->location;
5237 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5238 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5239 explicit_cast_p = true;
5240 ce = c_parser_expression (parser);
5241 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5242 expr = ce.value;
5243 if (flag_cilkplus && contains_array_notation_expr (expr))
5245 error_at (switch_cond_loc,
5246 "array notations cannot be used as a condition for switch "
5247 "statement");
5248 expr = error_mark_node;
5250 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5252 else
5254 switch_cond_loc = UNKNOWN_LOCATION;
5255 expr = error_mark_node;
5257 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5258 save_break = c_break_label;
5259 c_break_label = NULL_TREE;
5260 body = c_parser_c99_block_statement (parser);
5261 c_finish_case (body, ce.original_type);
5262 if (c_break_label)
5264 location_t here = c_parser_peek_token (parser)->location;
5265 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5266 SET_EXPR_LOCATION (t, here);
5267 add_stmt (t);
5269 c_break_label = save_break;
5270 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5273 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5275 while-statement:
5276 while (expression) statement
5279 static void
5280 c_parser_while_statement (c_parser *parser, bool ivdep)
5282 tree block, cond, body, save_break, save_cont;
5283 location_t loc;
5284 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5285 c_parser_consume_token (parser);
5286 block = c_begin_compound_stmt (flag_isoc99);
5287 loc = c_parser_peek_token (parser)->location;
5288 cond = c_parser_paren_condition (parser);
5289 if (flag_cilkplus && contains_array_notation_expr (cond))
5291 error_at (loc, "array notations cannot be used as a condition for while "
5292 "statement");
5293 cond = error_mark_node;
5296 if (ivdep && cond != error_mark_node)
5297 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5298 build_int_cst (integer_type_node,
5299 annot_expr_ivdep_kind));
5300 save_break = c_break_label;
5301 c_break_label = NULL_TREE;
5302 save_cont = c_cont_label;
5303 c_cont_label = NULL_TREE;
5304 body = c_parser_c99_block_statement (parser);
5305 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5306 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5307 c_break_label = save_break;
5308 c_cont_label = save_cont;
5311 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5313 do-statement:
5314 do statement while ( expression ) ;
5317 static void
5318 c_parser_do_statement (c_parser *parser, bool ivdep)
5320 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5321 location_t loc;
5322 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5323 c_parser_consume_token (parser);
5324 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5325 warning_at (c_parser_peek_token (parser)->location,
5326 OPT_Wempty_body,
5327 "suggest braces around empty body in %<do%> statement");
5328 block = c_begin_compound_stmt (flag_isoc99);
5329 loc = c_parser_peek_token (parser)->location;
5330 save_break = c_break_label;
5331 c_break_label = NULL_TREE;
5332 save_cont = c_cont_label;
5333 c_cont_label = NULL_TREE;
5334 body = c_parser_c99_block_statement (parser);
5335 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5336 new_break = c_break_label;
5337 c_break_label = save_break;
5338 new_cont = c_cont_label;
5339 c_cont_label = save_cont;
5340 cond = c_parser_paren_condition (parser);
5341 if (flag_cilkplus && contains_array_notation_expr (cond))
5343 error_at (loc, "array notations cannot be used as a condition for a "
5344 "do-while statement");
5345 cond = error_mark_node;
5347 if (ivdep && cond != error_mark_node)
5348 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5349 build_int_cst (integer_type_node,
5350 annot_expr_ivdep_kind));
5351 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5352 c_parser_skip_to_end_of_block_or_statement (parser);
5353 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5354 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5357 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5359 for-statement:
5360 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5361 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5363 The form with a declaration is new in C99.
5365 ??? In accordance with the old parser, the declaration may be a
5366 nested function, which is then rejected in check_for_loop_decls,
5367 but does it make any sense for this to be included in the grammar?
5368 Note in particular that the nested function does not include a
5369 trailing ';', whereas the "declaration" production includes one.
5370 Also, can we reject bad declarations earlier and cheaper than
5371 check_for_loop_decls?
5373 In Objective-C, there are two additional variants:
5375 foreach-statement:
5376 for ( expression in expresssion ) statement
5377 for ( declaration in expression ) statement
5379 This is inconsistent with C, because the second variant is allowed
5380 even if c99 is not enabled.
5382 The rest of the comment documents these Objective-C foreach-statement.
5384 Here is the canonical example of the first variant:
5385 for (object in array) { do something with object }
5386 we call the first expression ("object") the "object_expression" and
5387 the second expression ("array") the "collection_expression".
5388 object_expression must be an lvalue of type "id" (a generic Objective-C
5389 object) because the loop works by assigning to object_expression the
5390 various objects from the collection_expression. collection_expression
5391 must evaluate to something of type "id" which responds to the method
5392 countByEnumeratingWithState:objects:count:.
5394 The canonical example of the second variant is:
5395 for (id object in array) { do something with object }
5396 which is completely equivalent to
5398 id object;
5399 for (object in array) { do something with object }
5401 Note that initizializing 'object' in some way (eg, "for ((object =
5402 xxx) in array) { do something with object }") is possibly
5403 technically valid, but completely pointless as 'object' will be
5404 assigned to something else as soon as the loop starts. We should
5405 most likely reject it (TODO).
5407 The beginning of the Objective-C foreach-statement looks exactly
5408 like the beginning of the for-statement, and we can tell it is a
5409 foreach-statement only because the initial declaration or
5410 expression is terminated by 'in' instead of ';'.
5413 static void
5414 c_parser_for_statement (c_parser *parser, bool ivdep)
5416 tree block, cond, incr, save_break, save_cont, body;
5417 /* The following are only used when parsing an ObjC foreach statement. */
5418 tree object_expression;
5419 /* Silence the bogus uninitialized warning. */
5420 tree collection_expression = NULL;
5421 location_t loc = c_parser_peek_token (parser)->location;
5422 location_t for_loc = c_parser_peek_token (parser)->location;
5423 bool is_foreach_statement = false;
5424 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5425 c_parser_consume_token (parser);
5426 /* Open a compound statement in Objective-C as well, just in case this is
5427 as foreach expression. */
5428 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5429 cond = error_mark_node;
5430 incr = error_mark_node;
5431 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5433 /* Parse the initialization declaration or expression. */
5434 object_expression = error_mark_node;
5435 parser->objc_could_be_foreach_context = c_dialect_objc ();
5436 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5438 parser->objc_could_be_foreach_context = false;
5439 c_parser_consume_token (parser);
5440 c_finish_expr_stmt (loc, NULL_TREE);
5442 else if (c_parser_next_tokens_start_declaration (parser))
5444 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5445 &object_expression, vNULL);
5446 parser->objc_could_be_foreach_context = false;
5448 if (c_parser_next_token_is_keyword (parser, RID_IN))
5450 c_parser_consume_token (parser);
5451 is_foreach_statement = true;
5452 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5453 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5455 else
5456 check_for_loop_decls (for_loc, flag_isoc99);
5458 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5460 /* __extension__ can start a declaration, but is also an
5461 unary operator that can start an expression. Consume all
5462 but the last of a possible series of __extension__ to
5463 determine which. */
5464 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5465 && (c_parser_peek_2nd_token (parser)->keyword
5466 == RID_EXTENSION))
5467 c_parser_consume_token (parser);
5468 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5470 int ext;
5471 ext = disable_extension_diagnostics ();
5472 c_parser_consume_token (parser);
5473 c_parser_declaration_or_fndef (parser, true, true, true, true,
5474 true, &object_expression, vNULL);
5475 parser->objc_could_be_foreach_context = false;
5477 restore_extension_diagnostics (ext);
5478 if (c_parser_next_token_is_keyword (parser, RID_IN))
5480 c_parser_consume_token (parser);
5481 is_foreach_statement = true;
5482 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5483 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5485 else
5486 check_for_loop_decls (for_loc, flag_isoc99);
5488 else
5489 goto init_expr;
5491 else
5493 init_expr:
5495 struct c_expr ce;
5496 tree init_expression;
5497 ce = c_parser_expression (parser);
5498 init_expression = ce.value;
5499 parser->objc_could_be_foreach_context = false;
5500 if (c_parser_next_token_is_keyword (parser, RID_IN))
5502 c_parser_consume_token (parser);
5503 is_foreach_statement = true;
5504 if (! lvalue_p (init_expression))
5505 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5506 object_expression = c_fully_fold (init_expression, false, NULL);
5508 else
5510 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5511 init_expression = ce.value;
5512 c_finish_expr_stmt (loc, init_expression);
5513 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5517 /* Parse the loop condition. In the case of a foreach
5518 statement, there is no loop condition. */
5519 gcc_assert (!parser->objc_could_be_foreach_context);
5520 if (!is_foreach_statement)
5522 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5524 if (ivdep)
5526 c_parser_error (parser, "missing loop condition in loop with "
5527 "%<GCC ivdep%> pragma");
5528 cond = error_mark_node;
5530 else
5532 c_parser_consume_token (parser);
5533 cond = NULL_TREE;
5536 else
5538 cond = c_parser_condition (parser);
5539 if (flag_cilkplus && contains_array_notation_expr (cond))
5541 error_at (loc, "array notations cannot be used in a "
5542 "condition for a for-loop");
5543 cond = error_mark_node;
5545 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5546 "expected %<;%>");
5548 if (ivdep && cond != error_mark_node)
5549 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5550 build_int_cst (integer_type_node,
5551 annot_expr_ivdep_kind));
5553 /* Parse the increment expression (the third expression in a
5554 for-statement). In the case of a foreach-statement, this is
5555 the expression that follows the 'in'. */
5556 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5558 if (is_foreach_statement)
5560 c_parser_error (parser, "missing collection in fast enumeration");
5561 collection_expression = error_mark_node;
5563 else
5564 incr = c_process_expr_stmt (loc, NULL_TREE);
5566 else
5568 if (is_foreach_statement)
5569 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5570 false, NULL);
5571 else
5573 struct c_expr ce = c_parser_expression (parser);
5574 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5575 incr = c_process_expr_stmt (loc, ce.value);
5578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5580 save_break = c_break_label;
5581 c_break_label = NULL_TREE;
5582 save_cont = c_cont_label;
5583 c_cont_label = NULL_TREE;
5584 body = c_parser_c99_block_statement (parser);
5585 if (is_foreach_statement)
5586 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5587 else
5588 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5589 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5590 c_break_label = save_break;
5591 c_cont_label = save_cont;
5594 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5595 statement with inputs, outputs, clobbers, and volatile tag
5596 allowed.
5598 asm-statement:
5599 asm type-qualifier[opt] ( asm-argument ) ;
5600 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5602 asm-argument:
5603 asm-string-literal
5604 asm-string-literal : asm-operands[opt]
5605 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5606 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5608 asm-goto-argument:
5609 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5610 : asm-goto-operands
5612 Qualifiers other than volatile are accepted in the syntax but
5613 warned for. */
5615 static tree
5616 c_parser_asm_statement (c_parser *parser)
5618 tree quals, str, outputs, inputs, clobbers, labels, ret;
5619 bool simple, is_goto;
5620 location_t asm_loc = c_parser_peek_token (parser)->location;
5621 int section, nsections;
5623 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5624 c_parser_consume_token (parser);
5625 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5627 quals = c_parser_peek_token (parser)->value;
5628 c_parser_consume_token (parser);
5630 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5631 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5633 warning_at (c_parser_peek_token (parser)->location,
5635 "%E qualifier ignored on asm",
5636 c_parser_peek_token (parser)->value);
5637 quals = NULL_TREE;
5638 c_parser_consume_token (parser);
5640 else
5641 quals = NULL_TREE;
5643 is_goto = false;
5644 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5646 c_parser_consume_token (parser);
5647 is_goto = true;
5650 /* ??? Follow the C++ parser rather than using the
5651 lex_untranslated_string kludge. */
5652 parser->lex_untranslated_string = true;
5653 ret = NULL;
5655 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5656 goto error;
5658 str = c_parser_asm_string_literal (parser);
5659 if (str == NULL_TREE)
5660 goto error_close_paren;
5662 simple = true;
5663 outputs = NULL_TREE;
5664 inputs = NULL_TREE;
5665 clobbers = NULL_TREE;
5666 labels = NULL_TREE;
5668 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5669 goto done_asm;
5671 /* Parse each colon-delimited section of operands. */
5672 nsections = 3 + is_goto;
5673 for (section = 0; section < nsections; ++section)
5675 if (!c_parser_require (parser, CPP_COLON,
5676 is_goto
5677 ? "expected %<:%>"
5678 : "expected %<:%> or %<)%>"))
5679 goto error_close_paren;
5681 /* Once past any colon, we're no longer a simple asm. */
5682 simple = false;
5684 if ((!c_parser_next_token_is (parser, CPP_COLON)
5685 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5686 || section == 3)
5687 switch (section)
5689 case 0:
5690 /* For asm goto, we don't allow output operands, but reserve
5691 the slot for a future extension that does allow them. */
5692 if (!is_goto)
5693 outputs = c_parser_asm_operands (parser);
5694 break;
5695 case 1:
5696 inputs = c_parser_asm_operands (parser);
5697 break;
5698 case 2:
5699 clobbers = c_parser_asm_clobbers (parser);
5700 break;
5701 case 3:
5702 labels = c_parser_asm_goto_operands (parser);
5703 break;
5704 default:
5705 gcc_unreachable ();
5708 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5709 goto done_asm;
5712 done_asm:
5713 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5715 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5716 goto error;
5719 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5720 c_parser_skip_to_end_of_block_or_statement (parser);
5722 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5723 clobbers, labels, simple));
5725 error:
5726 parser->lex_untranslated_string = false;
5727 return ret;
5729 error_close_paren:
5730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5731 goto error;
5734 /* Parse asm operands, a GNU extension.
5736 asm-operands:
5737 asm-operand
5738 asm-operands , asm-operand
5740 asm-operand:
5741 asm-string-literal ( expression )
5742 [ identifier ] asm-string-literal ( expression )
5745 static tree
5746 c_parser_asm_operands (c_parser *parser)
5748 tree list = NULL_TREE;
5749 while (true)
5751 tree name, str;
5752 struct c_expr expr;
5753 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5755 c_parser_consume_token (parser);
5756 if (c_parser_next_token_is (parser, CPP_NAME))
5758 tree id = c_parser_peek_token (parser)->value;
5759 c_parser_consume_token (parser);
5760 name = build_string (IDENTIFIER_LENGTH (id),
5761 IDENTIFIER_POINTER (id));
5763 else
5765 c_parser_error (parser, "expected identifier");
5766 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5767 return NULL_TREE;
5769 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5770 "expected %<]%>");
5772 else
5773 name = NULL_TREE;
5774 str = c_parser_asm_string_literal (parser);
5775 if (str == NULL_TREE)
5776 return NULL_TREE;
5777 parser->lex_untranslated_string = false;
5778 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5780 parser->lex_untranslated_string = true;
5781 return NULL_TREE;
5783 expr = c_parser_expression (parser);
5784 mark_exp_read (expr.value);
5785 parser->lex_untranslated_string = true;
5786 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5788 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5789 return NULL_TREE;
5791 list = chainon (list, build_tree_list (build_tree_list (name, str),
5792 expr.value));
5793 if (c_parser_next_token_is (parser, CPP_COMMA))
5794 c_parser_consume_token (parser);
5795 else
5796 break;
5798 return list;
5801 /* Parse asm clobbers, a GNU extension.
5803 asm-clobbers:
5804 asm-string-literal
5805 asm-clobbers , asm-string-literal
5808 static tree
5809 c_parser_asm_clobbers (c_parser *parser)
5811 tree list = NULL_TREE;
5812 while (true)
5814 tree str = c_parser_asm_string_literal (parser);
5815 if (str)
5816 list = tree_cons (NULL_TREE, str, list);
5817 else
5818 return NULL_TREE;
5819 if (c_parser_next_token_is (parser, CPP_COMMA))
5820 c_parser_consume_token (parser);
5821 else
5822 break;
5824 return list;
5827 /* Parse asm goto labels, a GNU extension.
5829 asm-goto-operands:
5830 identifier
5831 asm-goto-operands , identifier
5834 static tree
5835 c_parser_asm_goto_operands (c_parser *parser)
5837 tree list = NULL_TREE;
5838 while (true)
5840 tree name, label;
5842 if (c_parser_next_token_is (parser, CPP_NAME))
5844 c_token *tok = c_parser_peek_token (parser);
5845 name = tok->value;
5846 label = lookup_label_for_goto (tok->location, name);
5847 c_parser_consume_token (parser);
5848 TREE_USED (label) = 1;
5850 else
5852 c_parser_error (parser, "expected identifier");
5853 return NULL_TREE;
5856 name = build_string (IDENTIFIER_LENGTH (name),
5857 IDENTIFIER_POINTER (name));
5858 list = tree_cons (name, label, list);
5859 if (c_parser_next_token_is (parser, CPP_COMMA))
5860 c_parser_consume_token (parser);
5861 else
5862 return nreverse (list);
5866 /* Parse an expression other than a compound expression; that is, an
5867 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5868 NULL then it is an Objective-C message expression which is the
5869 primary-expression starting the expression as an initializer.
5871 assignment-expression:
5872 conditional-expression
5873 unary-expression assignment-operator assignment-expression
5875 assignment-operator: one of
5876 = *= /= %= += -= <<= >>= &= ^= |=
5878 In GNU C we accept any conditional expression on the LHS and
5879 diagnose the invalid lvalue rather than producing a syntax
5880 error. */
5882 static struct c_expr
5883 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5884 tree omp_atomic_lhs)
5886 struct c_expr lhs, rhs, ret;
5887 enum tree_code code;
5888 location_t op_location, exp_location;
5889 gcc_assert (!after || c_dialect_objc ());
5890 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5891 op_location = c_parser_peek_token (parser)->location;
5892 switch (c_parser_peek_token (parser)->type)
5894 case CPP_EQ:
5895 code = NOP_EXPR;
5896 break;
5897 case CPP_MULT_EQ:
5898 code = MULT_EXPR;
5899 break;
5900 case CPP_DIV_EQ:
5901 code = TRUNC_DIV_EXPR;
5902 break;
5903 case CPP_MOD_EQ:
5904 code = TRUNC_MOD_EXPR;
5905 break;
5906 case CPP_PLUS_EQ:
5907 code = PLUS_EXPR;
5908 break;
5909 case CPP_MINUS_EQ:
5910 code = MINUS_EXPR;
5911 break;
5912 case CPP_LSHIFT_EQ:
5913 code = LSHIFT_EXPR;
5914 break;
5915 case CPP_RSHIFT_EQ:
5916 code = RSHIFT_EXPR;
5917 break;
5918 case CPP_AND_EQ:
5919 code = BIT_AND_EXPR;
5920 break;
5921 case CPP_XOR_EQ:
5922 code = BIT_XOR_EXPR;
5923 break;
5924 case CPP_OR_EQ:
5925 code = BIT_IOR_EXPR;
5926 break;
5927 default:
5928 return lhs;
5930 c_parser_consume_token (parser);
5931 exp_location = c_parser_peek_token (parser)->location;
5932 rhs = c_parser_expr_no_commas (parser, NULL);
5933 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5935 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5936 code, exp_location, rhs.value,
5937 rhs.original_type);
5938 if (code == NOP_EXPR)
5939 ret.original_code = MODIFY_EXPR;
5940 else
5942 TREE_NO_WARNING (ret.value) = 1;
5943 ret.original_code = ERROR_MARK;
5945 ret.original_type = NULL;
5946 return ret;
5949 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5950 is not NULL then it is an Objective-C message expression which is
5951 the primary-expression starting the expression as an initializer.
5953 conditional-expression:
5954 logical-OR-expression
5955 logical-OR-expression ? expression : conditional-expression
5957 GNU extensions:
5959 conditional-expression:
5960 logical-OR-expression ? : conditional-expression
5963 static struct c_expr
5964 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5965 tree omp_atomic_lhs)
5967 struct c_expr cond, exp1, exp2, ret;
5968 location_t cond_loc, colon_loc, middle_loc;
5970 gcc_assert (!after || c_dialect_objc ());
5972 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5974 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5975 return cond;
5976 cond_loc = c_parser_peek_token (parser)->location;
5977 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5978 c_parser_consume_token (parser);
5979 if (c_parser_next_token_is (parser, CPP_COLON))
5981 tree eptype = NULL_TREE;
5983 middle_loc = c_parser_peek_token (parser)->location;
5984 pedwarn (middle_loc, OPT_Wpedantic,
5985 "ISO C forbids omitting the middle term of a ?: expression");
5986 warn_for_omitted_condop (middle_loc, cond.value);
5987 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5989 eptype = TREE_TYPE (cond.value);
5990 cond.value = TREE_OPERAND (cond.value, 0);
5992 /* Make sure first operand is calculated only once. */
5993 exp1.value = c_save_expr (default_conversion (cond.value));
5994 if (eptype)
5995 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5996 exp1.original_type = NULL;
5997 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5998 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6000 else
6002 cond.value
6003 = c_objc_common_truthvalue_conversion
6004 (cond_loc, default_conversion (cond.value));
6005 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6006 exp1 = c_parser_expression_conv (parser);
6007 mark_exp_read (exp1.value);
6008 c_inhibit_evaluation_warnings +=
6009 ((cond.value == truthvalue_true_node)
6010 - (cond.value == truthvalue_false_node));
6013 colon_loc = c_parser_peek_token (parser)->location;
6014 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6016 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6017 ret.value = error_mark_node;
6018 ret.original_code = ERROR_MARK;
6019 ret.original_type = NULL;
6020 return ret;
6023 location_t exp2_loc = c_parser_peek_token (parser)->location;
6024 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6025 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6027 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6028 ret.value = build_conditional_expr (colon_loc, cond.value,
6029 cond.original_code == C_MAYBE_CONST_EXPR,
6030 exp1.value, exp1.original_type,
6031 exp2.value, exp2.original_type);
6032 ret.original_code = ERROR_MARK;
6033 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6034 ret.original_type = NULL;
6035 else
6037 tree t1, t2;
6039 /* If both sides are enum type, the default conversion will have
6040 made the type of the result be an integer type. We want to
6041 remember the enum types we started with. */
6042 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6043 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6044 ret.original_type = ((t1 != error_mark_node
6045 && t2 != error_mark_node
6046 && (TYPE_MAIN_VARIANT (t1)
6047 == TYPE_MAIN_VARIANT (t2)))
6048 ? t1
6049 : NULL);
6051 return ret;
6054 /* Parse a binary expression; that is, a logical-OR-expression (C90
6055 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6056 an Objective-C message expression which is the primary-expression
6057 starting the expression as an initializer.
6059 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6060 when it should be the unfolded lhs. In a valid OpenMP source,
6061 one of the operands of the toplevel binary expression must be equal
6062 to it. In that case, just return a build2 created binary operation
6063 rather than result of parser_build_binary_op.
6065 multiplicative-expression:
6066 cast-expression
6067 multiplicative-expression * cast-expression
6068 multiplicative-expression / cast-expression
6069 multiplicative-expression % cast-expression
6071 additive-expression:
6072 multiplicative-expression
6073 additive-expression + multiplicative-expression
6074 additive-expression - multiplicative-expression
6076 shift-expression:
6077 additive-expression
6078 shift-expression << additive-expression
6079 shift-expression >> additive-expression
6081 relational-expression:
6082 shift-expression
6083 relational-expression < shift-expression
6084 relational-expression > shift-expression
6085 relational-expression <= shift-expression
6086 relational-expression >= shift-expression
6088 equality-expression:
6089 relational-expression
6090 equality-expression == relational-expression
6091 equality-expression != relational-expression
6093 AND-expression:
6094 equality-expression
6095 AND-expression & equality-expression
6097 exclusive-OR-expression:
6098 AND-expression
6099 exclusive-OR-expression ^ AND-expression
6101 inclusive-OR-expression:
6102 exclusive-OR-expression
6103 inclusive-OR-expression | exclusive-OR-expression
6105 logical-AND-expression:
6106 inclusive-OR-expression
6107 logical-AND-expression && inclusive-OR-expression
6109 logical-OR-expression:
6110 logical-AND-expression
6111 logical-OR-expression || logical-AND-expression
6114 static struct c_expr
6115 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6116 tree omp_atomic_lhs)
6118 /* A binary expression is parsed using operator-precedence parsing,
6119 with the operands being cast expressions. All the binary
6120 operators are left-associative. Thus a binary expression is of
6121 form:
6123 E0 op1 E1 op2 E2 ...
6125 which we represent on a stack. On the stack, the precedence
6126 levels are strictly increasing. When a new operator is
6127 encountered of higher precedence than that at the top of the
6128 stack, it is pushed; its LHS is the top expression, and its RHS
6129 is everything parsed until it is popped. When a new operator is
6130 encountered with precedence less than or equal to that at the top
6131 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6132 by the result of the operation until the operator at the top of
6133 the stack has lower precedence than the new operator or there is
6134 only one element on the stack; then the top expression is the LHS
6135 of the new operator. In the case of logical AND and OR
6136 expressions, we also need to adjust c_inhibit_evaluation_warnings
6137 as appropriate when the operators are pushed and popped. */
6139 struct {
6140 /* The expression at this stack level. */
6141 struct c_expr expr;
6142 /* The precedence of the operator on its left, PREC_NONE at the
6143 bottom of the stack. */
6144 enum c_parser_prec prec;
6145 /* The operation on its left. */
6146 enum tree_code op;
6147 /* The source location of this operation. */
6148 location_t loc;
6149 } stack[NUM_PRECS];
6150 int sp;
6151 /* Location of the binary operator. */
6152 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6153 #define POP \
6154 do { \
6155 switch (stack[sp].op) \
6157 case TRUTH_ANDIF_EXPR: \
6158 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6159 == truthvalue_false_node); \
6160 break; \
6161 case TRUTH_ORIF_EXPR: \
6162 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6163 == truthvalue_true_node); \
6164 break; \
6165 default: \
6166 break; \
6168 stack[sp - 1].expr \
6169 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6170 stack[sp - 1].expr, true, true); \
6171 stack[sp].expr \
6172 = convert_lvalue_to_rvalue (stack[sp].loc, \
6173 stack[sp].expr, true, true); \
6174 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6175 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6176 && ((1 << stack[sp].prec) \
6177 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6178 | PREC_ADD | PREC_MULT))) \
6179 && stack[sp].op != TRUNC_MOD_EXPR \
6180 && stack[0].expr.value != error_mark_node \
6181 && stack[1].expr.value != error_mark_node \
6182 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6183 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6184 stack[0].expr.value \
6185 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6186 stack[0].expr.value, stack[1].expr.value); \
6187 else \
6188 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6189 stack[sp].op, \
6190 stack[sp - 1].expr, \
6191 stack[sp].expr); \
6192 sp--; \
6193 } while (0)
6194 gcc_assert (!after || c_dialect_objc ());
6195 stack[0].loc = c_parser_peek_token (parser)->location;
6196 stack[0].expr = c_parser_cast_expression (parser, after);
6197 stack[0].prec = PREC_NONE;
6198 sp = 0;
6199 while (true)
6201 enum c_parser_prec oprec;
6202 enum tree_code ocode;
6203 if (parser->error)
6204 goto out;
6205 switch (c_parser_peek_token (parser)->type)
6207 case CPP_MULT:
6208 oprec = PREC_MULT;
6209 ocode = MULT_EXPR;
6210 break;
6211 case CPP_DIV:
6212 oprec = PREC_MULT;
6213 ocode = TRUNC_DIV_EXPR;
6214 break;
6215 case CPP_MOD:
6216 oprec = PREC_MULT;
6217 ocode = TRUNC_MOD_EXPR;
6218 break;
6219 case CPP_PLUS:
6220 oprec = PREC_ADD;
6221 ocode = PLUS_EXPR;
6222 break;
6223 case CPP_MINUS:
6224 oprec = PREC_ADD;
6225 ocode = MINUS_EXPR;
6226 break;
6227 case CPP_LSHIFT:
6228 oprec = PREC_SHIFT;
6229 ocode = LSHIFT_EXPR;
6230 break;
6231 case CPP_RSHIFT:
6232 oprec = PREC_SHIFT;
6233 ocode = RSHIFT_EXPR;
6234 break;
6235 case CPP_LESS:
6236 oprec = PREC_REL;
6237 ocode = LT_EXPR;
6238 break;
6239 case CPP_GREATER:
6240 oprec = PREC_REL;
6241 ocode = GT_EXPR;
6242 break;
6243 case CPP_LESS_EQ:
6244 oprec = PREC_REL;
6245 ocode = LE_EXPR;
6246 break;
6247 case CPP_GREATER_EQ:
6248 oprec = PREC_REL;
6249 ocode = GE_EXPR;
6250 break;
6251 case CPP_EQ_EQ:
6252 oprec = PREC_EQ;
6253 ocode = EQ_EXPR;
6254 break;
6255 case CPP_NOT_EQ:
6256 oprec = PREC_EQ;
6257 ocode = NE_EXPR;
6258 break;
6259 case CPP_AND:
6260 oprec = PREC_BITAND;
6261 ocode = BIT_AND_EXPR;
6262 break;
6263 case CPP_XOR:
6264 oprec = PREC_BITXOR;
6265 ocode = BIT_XOR_EXPR;
6266 break;
6267 case CPP_OR:
6268 oprec = PREC_BITOR;
6269 ocode = BIT_IOR_EXPR;
6270 break;
6271 case CPP_AND_AND:
6272 oprec = PREC_LOGAND;
6273 ocode = TRUTH_ANDIF_EXPR;
6274 break;
6275 case CPP_OR_OR:
6276 oprec = PREC_LOGOR;
6277 ocode = TRUTH_ORIF_EXPR;
6278 break;
6279 default:
6280 /* Not a binary operator, so end of the binary
6281 expression. */
6282 goto out;
6284 binary_loc = c_parser_peek_token (parser)->location;
6285 while (oprec <= stack[sp].prec)
6286 POP;
6287 c_parser_consume_token (parser);
6288 switch (ocode)
6290 case TRUTH_ANDIF_EXPR:
6291 stack[sp].expr
6292 = convert_lvalue_to_rvalue (stack[sp].loc,
6293 stack[sp].expr, true, true);
6294 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6295 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6296 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6297 == truthvalue_false_node);
6298 break;
6299 case TRUTH_ORIF_EXPR:
6300 stack[sp].expr
6301 = convert_lvalue_to_rvalue (stack[sp].loc,
6302 stack[sp].expr, true, true);
6303 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6304 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6305 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6306 == truthvalue_true_node);
6307 break;
6308 default:
6309 break;
6311 sp++;
6312 stack[sp].loc = binary_loc;
6313 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6314 stack[sp].prec = oprec;
6315 stack[sp].op = ocode;
6316 stack[sp].loc = binary_loc;
6318 out:
6319 while (sp > 0)
6320 POP;
6321 return stack[0].expr;
6322 #undef POP
6325 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6326 NULL then it is an Objective-C message expression which is the
6327 primary-expression starting the expression as an initializer.
6329 cast-expression:
6330 unary-expression
6331 ( type-name ) unary-expression
6334 static struct c_expr
6335 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6337 location_t cast_loc = c_parser_peek_token (parser)->location;
6338 gcc_assert (!after || c_dialect_objc ());
6339 if (after)
6340 return c_parser_postfix_expression_after_primary (parser,
6341 cast_loc, *after);
6342 /* If the expression begins with a parenthesized type name, it may
6343 be either a cast or a compound literal; we need to see whether
6344 the next character is '{' to tell the difference. If not, it is
6345 an unary expression. Full detection of unknown typenames here
6346 would require a 3-token lookahead. */
6347 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6348 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6350 struct c_type_name *type_name;
6351 struct c_expr ret;
6352 struct c_expr expr;
6353 c_parser_consume_token (parser);
6354 type_name = c_parser_type_name (parser);
6355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6356 if (type_name == NULL)
6358 ret.value = error_mark_node;
6359 ret.original_code = ERROR_MARK;
6360 ret.original_type = NULL;
6361 return ret;
6364 /* Save casted types in the function's used types hash table. */
6365 used_types_insert (type_name->specs->type);
6367 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6368 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6369 cast_loc);
6371 location_t expr_loc = c_parser_peek_token (parser)->location;
6372 expr = c_parser_cast_expression (parser, NULL);
6373 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6375 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6376 ret.original_code = ERROR_MARK;
6377 ret.original_type = NULL;
6378 return ret;
6380 else
6381 return c_parser_unary_expression (parser);
6384 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6386 unary-expression:
6387 postfix-expression
6388 ++ unary-expression
6389 -- unary-expression
6390 unary-operator cast-expression
6391 sizeof unary-expression
6392 sizeof ( type-name )
6394 unary-operator: one of
6395 & * + - ~ !
6397 GNU extensions:
6399 unary-expression:
6400 __alignof__ unary-expression
6401 __alignof__ ( type-name )
6402 && identifier
6404 (C11 permits _Alignof with type names only.)
6406 unary-operator: one of
6407 __extension__ __real__ __imag__
6409 Transactional Memory:
6411 unary-expression:
6412 transaction-expression
6414 In addition, the GNU syntax treats ++ and -- as unary operators, so
6415 they may be applied to cast expressions with errors for non-lvalues
6416 given later. */
6418 static struct c_expr
6419 c_parser_unary_expression (c_parser *parser)
6421 int ext;
6422 struct c_expr ret, op;
6423 location_t op_loc = c_parser_peek_token (parser)->location;
6424 location_t exp_loc;
6425 ret.original_code = ERROR_MARK;
6426 ret.original_type = NULL;
6427 switch (c_parser_peek_token (parser)->type)
6429 case CPP_PLUS_PLUS:
6430 c_parser_consume_token (parser);
6431 exp_loc = c_parser_peek_token (parser)->location;
6432 op = c_parser_cast_expression (parser, NULL);
6434 /* If there is array notations in op, we expand them. */
6435 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6436 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6437 else
6439 op = default_function_array_read_conversion (exp_loc, op);
6440 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6442 case CPP_MINUS_MINUS:
6443 c_parser_consume_token (parser);
6444 exp_loc = c_parser_peek_token (parser)->location;
6445 op = c_parser_cast_expression (parser, NULL);
6447 /* If there is array notations in op, we expand them. */
6448 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6449 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6450 else
6452 op = default_function_array_read_conversion (exp_loc, op);
6453 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6455 case CPP_AND:
6456 c_parser_consume_token (parser);
6457 op = c_parser_cast_expression (parser, NULL);
6458 mark_exp_read (op.value);
6459 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6460 case CPP_MULT:
6461 c_parser_consume_token (parser);
6462 exp_loc = c_parser_peek_token (parser)->location;
6463 op = c_parser_cast_expression (parser, NULL);
6464 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6465 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6466 return ret;
6467 case CPP_PLUS:
6468 if (!c_dialect_objc () && !in_system_header_at (input_location))
6469 warning_at (op_loc,
6470 OPT_Wtraditional,
6471 "traditional C rejects the unary plus operator");
6472 c_parser_consume_token (parser);
6473 exp_loc = c_parser_peek_token (parser)->location;
6474 op = c_parser_cast_expression (parser, NULL);
6475 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6476 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6477 case CPP_MINUS:
6478 c_parser_consume_token (parser);
6479 exp_loc = c_parser_peek_token (parser)->location;
6480 op = c_parser_cast_expression (parser, NULL);
6481 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6482 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6483 case CPP_COMPL:
6484 c_parser_consume_token (parser);
6485 exp_loc = c_parser_peek_token (parser)->location;
6486 op = c_parser_cast_expression (parser, NULL);
6487 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6488 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6489 case CPP_NOT:
6490 c_parser_consume_token (parser);
6491 exp_loc = c_parser_peek_token (parser)->location;
6492 op = c_parser_cast_expression (parser, NULL);
6493 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6494 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6495 case CPP_AND_AND:
6496 /* Refer to the address of a label as a pointer. */
6497 c_parser_consume_token (parser);
6498 if (c_parser_next_token_is (parser, CPP_NAME))
6500 ret.value = finish_label_address_expr
6501 (c_parser_peek_token (parser)->value, op_loc);
6502 c_parser_consume_token (parser);
6504 else
6506 c_parser_error (parser, "expected identifier");
6507 ret.value = error_mark_node;
6509 return ret;
6510 case CPP_KEYWORD:
6511 switch (c_parser_peek_token (parser)->keyword)
6513 case RID_SIZEOF:
6514 return c_parser_sizeof_expression (parser);
6515 case RID_ALIGNOF:
6516 return c_parser_alignof_expression (parser);
6517 case RID_EXTENSION:
6518 c_parser_consume_token (parser);
6519 ext = disable_extension_diagnostics ();
6520 ret = c_parser_cast_expression (parser, NULL);
6521 restore_extension_diagnostics (ext);
6522 return ret;
6523 case RID_REALPART:
6524 c_parser_consume_token (parser);
6525 exp_loc = c_parser_peek_token (parser)->location;
6526 op = c_parser_cast_expression (parser, NULL);
6527 op = default_function_array_conversion (exp_loc, op);
6528 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6529 case RID_IMAGPART:
6530 c_parser_consume_token (parser);
6531 exp_loc = c_parser_peek_token (parser)->location;
6532 op = c_parser_cast_expression (parser, NULL);
6533 op = default_function_array_conversion (exp_loc, op);
6534 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6535 case RID_TRANSACTION_ATOMIC:
6536 case RID_TRANSACTION_RELAXED:
6537 return c_parser_transaction_expression (parser,
6538 c_parser_peek_token (parser)->keyword);
6539 default:
6540 return c_parser_postfix_expression (parser);
6542 default:
6543 return c_parser_postfix_expression (parser);
6547 /* Parse a sizeof expression. */
6549 static struct c_expr
6550 c_parser_sizeof_expression (c_parser *parser)
6552 struct c_expr expr;
6553 location_t expr_loc;
6554 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6555 c_parser_consume_token (parser);
6556 c_inhibit_evaluation_warnings++;
6557 in_sizeof++;
6558 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6559 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6561 /* Either sizeof ( type-name ) or sizeof unary-expression
6562 starting with a compound literal. */
6563 struct c_type_name *type_name;
6564 c_parser_consume_token (parser);
6565 expr_loc = c_parser_peek_token (parser)->location;
6566 type_name = c_parser_type_name (parser);
6567 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6568 if (type_name == NULL)
6570 struct c_expr ret;
6571 c_inhibit_evaluation_warnings--;
6572 in_sizeof--;
6573 ret.value = error_mark_node;
6574 ret.original_code = ERROR_MARK;
6575 ret.original_type = NULL;
6576 return ret;
6578 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6580 expr = c_parser_postfix_expression_after_paren_type (parser,
6581 type_name,
6582 expr_loc);
6583 goto sizeof_expr;
6585 /* sizeof ( type-name ). */
6586 c_inhibit_evaluation_warnings--;
6587 in_sizeof--;
6588 return c_expr_sizeof_type (expr_loc, type_name);
6590 else
6592 expr_loc = c_parser_peek_token (parser)->location;
6593 expr = c_parser_unary_expression (parser);
6594 sizeof_expr:
6595 c_inhibit_evaluation_warnings--;
6596 in_sizeof--;
6597 mark_exp_read (expr.value);
6598 if (TREE_CODE (expr.value) == COMPONENT_REF
6599 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6600 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6601 return c_expr_sizeof_expr (expr_loc, expr);
6605 /* Parse an alignof expression. */
6607 static struct c_expr
6608 c_parser_alignof_expression (c_parser *parser)
6610 struct c_expr expr;
6611 location_t loc = c_parser_peek_token (parser)->location;
6612 tree alignof_spelling = c_parser_peek_token (parser)->value;
6613 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6614 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6615 "_Alignof") == 0;
6616 /* A diagnostic is not required for the use of this identifier in
6617 the implementation namespace; only diagnose it for the C11
6618 spelling because of existing code using the other spellings. */
6619 if (is_c11_alignof)
6621 if (flag_isoc99)
6622 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6623 alignof_spelling);
6624 else
6625 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6626 alignof_spelling);
6628 c_parser_consume_token (parser);
6629 c_inhibit_evaluation_warnings++;
6630 in_alignof++;
6631 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6632 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6634 /* Either __alignof__ ( type-name ) or __alignof__
6635 unary-expression starting with a compound literal. */
6636 location_t loc;
6637 struct c_type_name *type_name;
6638 struct c_expr ret;
6639 c_parser_consume_token (parser);
6640 loc = c_parser_peek_token (parser)->location;
6641 type_name = c_parser_type_name (parser);
6642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6643 if (type_name == NULL)
6645 struct c_expr ret;
6646 c_inhibit_evaluation_warnings--;
6647 in_alignof--;
6648 ret.value = error_mark_node;
6649 ret.original_code = ERROR_MARK;
6650 ret.original_type = NULL;
6651 return ret;
6653 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6655 expr = c_parser_postfix_expression_after_paren_type (parser,
6656 type_name,
6657 loc);
6658 goto alignof_expr;
6660 /* alignof ( type-name ). */
6661 c_inhibit_evaluation_warnings--;
6662 in_alignof--;
6663 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6664 NULL, NULL),
6665 false, is_c11_alignof, 1);
6666 ret.original_code = ERROR_MARK;
6667 ret.original_type = NULL;
6668 return ret;
6670 else
6672 struct c_expr ret;
6673 expr = c_parser_unary_expression (parser);
6674 alignof_expr:
6675 mark_exp_read (expr.value);
6676 c_inhibit_evaluation_warnings--;
6677 in_alignof--;
6678 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6679 alignof_spelling);
6680 ret.value = c_alignof_expr (loc, expr.value);
6681 ret.original_code = ERROR_MARK;
6682 ret.original_type = NULL;
6683 return ret;
6687 /* Helper function to read arguments of builtins which are interfaces
6688 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6689 others. The name of the builtin is passed using BNAME parameter.
6690 Function returns true if there were no errors while parsing and
6691 stores the arguments in CEXPR_LIST. */
6692 static bool
6693 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6694 vec<c_expr_t, va_gc> **ret_cexpr_list,
6695 bool choose_expr_p)
6697 location_t loc = c_parser_peek_token (parser)->location;
6698 vec<c_expr_t, va_gc> *cexpr_list;
6699 c_expr_t expr;
6700 bool saved_force_folding_builtin_constant_p;
6702 *ret_cexpr_list = NULL;
6703 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6705 error_at (loc, "cannot take address of %qs", bname);
6706 return false;
6709 c_parser_consume_token (parser);
6711 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6713 c_parser_consume_token (parser);
6714 return true;
6717 saved_force_folding_builtin_constant_p
6718 = force_folding_builtin_constant_p;
6719 force_folding_builtin_constant_p |= choose_expr_p;
6720 expr = c_parser_expr_no_commas (parser, NULL);
6721 force_folding_builtin_constant_p
6722 = saved_force_folding_builtin_constant_p;
6723 vec_alloc (cexpr_list, 1);
6724 vec_safe_push (cexpr_list, expr);
6725 while (c_parser_next_token_is (parser, CPP_COMMA))
6727 c_parser_consume_token (parser);
6728 expr = c_parser_expr_no_commas (parser, NULL);
6729 vec_safe_push (cexpr_list, expr);
6732 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6733 return false;
6735 *ret_cexpr_list = cexpr_list;
6736 return true;
6739 /* This represents a single generic-association. */
6741 struct c_generic_association
6743 /* The location of the starting token of the type. */
6744 location_t type_location;
6745 /* The association's type, or NULL_TREE for 'default'. */
6746 tree type;
6747 /* The association's expression. */
6748 struct c_expr expression;
6751 /* Parse a generic-selection. (C11 6.5.1.1).
6753 generic-selection:
6754 _Generic ( assignment-expression , generic-assoc-list )
6756 generic-assoc-list:
6757 generic-association
6758 generic-assoc-list , generic-association
6760 generic-association:
6761 type-name : assignment-expression
6762 default : assignment-expression
6765 static struct c_expr
6766 c_parser_generic_selection (c_parser *parser)
6768 vec<c_generic_association> associations = vNULL;
6769 struct c_expr selector, error_expr;
6770 tree selector_type;
6771 struct c_generic_association matched_assoc;
6772 bool match_found = false;
6773 location_t generic_loc, selector_loc;
6775 error_expr.original_code = ERROR_MARK;
6776 error_expr.original_type = NULL;
6777 error_expr.value = error_mark_node;
6778 matched_assoc.type_location = UNKNOWN_LOCATION;
6779 matched_assoc.type = NULL_TREE;
6780 matched_assoc.expression = error_expr;
6782 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6783 generic_loc = c_parser_peek_token (parser)->location;
6784 c_parser_consume_token (parser);
6785 if (flag_isoc99)
6786 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6787 "ISO C99 does not support %<_Generic%>");
6788 else
6789 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6790 "ISO C90 does not support %<_Generic%>");
6792 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6793 return error_expr;
6795 c_inhibit_evaluation_warnings++;
6796 selector_loc = c_parser_peek_token (parser)->location;
6797 selector = c_parser_expr_no_commas (parser, NULL);
6798 selector = default_function_array_conversion (selector_loc, selector);
6799 c_inhibit_evaluation_warnings--;
6801 if (selector.value == error_mark_node)
6803 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6804 return selector;
6806 selector_type = TREE_TYPE (selector.value);
6807 /* In ISO C terms, rvalues (including the controlling expression of
6808 _Generic) do not have qualified types. */
6809 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6810 selector_type = TYPE_MAIN_VARIANT (selector_type);
6811 /* In ISO C terms, _Noreturn is not part of the type of expressions
6812 such as &abort, but in GCC it is represented internally as a type
6813 qualifier. */
6814 if (FUNCTION_POINTER_TYPE_P (selector_type)
6815 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6816 selector_type
6817 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6819 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6821 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6822 return error_expr;
6825 while (1)
6827 struct c_generic_association assoc, *iter;
6828 unsigned int ix;
6829 c_token *token = c_parser_peek_token (parser);
6831 assoc.type_location = token->location;
6832 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6834 c_parser_consume_token (parser);
6835 assoc.type = NULL_TREE;
6837 else
6839 struct c_type_name *type_name;
6841 type_name = c_parser_type_name (parser);
6842 if (type_name == NULL)
6844 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6845 goto error_exit;
6847 assoc.type = groktypename (type_name, NULL, NULL);
6848 if (assoc.type == error_mark_node)
6850 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6851 goto error_exit;
6854 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6855 error_at (assoc.type_location,
6856 "%<_Generic%> association has function type");
6857 else if (!COMPLETE_TYPE_P (assoc.type))
6858 error_at (assoc.type_location,
6859 "%<_Generic%> association has incomplete type");
6861 if (variably_modified_type_p (assoc.type, NULL_TREE))
6862 error_at (assoc.type_location,
6863 "%<_Generic%> association has "
6864 "variable length type");
6867 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6869 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6870 goto error_exit;
6873 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6874 if (assoc.expression.value == error_mark_node)
6876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6877 goto error_exit;
6880 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6882 if (assoc.type == NULL_TREE)
6884 if (iter->type == NULL_TREE)
6886 error_at (assoc.type_location,
6887 "duplicate %<default%> case in %<_Generic%>");
6888 inform (iter->type_location, "original %<default%> is here");
6891 else if (iter->type != NULL_TREE)
6893 if (comptypes (assoc.type, iter->type))
6895 error_at (assoc.type_location,
6896 "%<_Generic%> specifies two compatible types");
6897 inform (iter->type_location, "compatible type is here");
6902 if (assoc.type == NULL_TREE)
6904 if (!match_found)
6906 matched_assoc = assoc;
6907 match_found = true;
6910 else if (comptypes (assoc.type, selector_type))
6912 if (!match_found || matched_assoc.type == NULL_TREE)
6914 matched_assoc = assoc;
6915 match_found = true;
6917 else
6919 error_at (assoc.type_location,
6920 "%<_Generic> selector matches multiple associations");
6921 inform (matched_assoc.type_location,
6922 "other match is here");
6926 associations.safe_push (assoc);
6928 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6929 break;
6930 c_parser_consume_token (parser);
6933 associations.release ();
6935 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6938 return error_expr;
6941 if (!match_found)
6943 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6944 "compatible with any association",
6945 selector_type);
6946 return error_expr;
6949 return matched_assoc.expression;
6951 error_exit:
6952 associations.release ();
6953 return error_expr;
6956 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6958 postfix-expression:
6959 primary-expression
6960 postfix-expression [ expression ]
6961 postfix-expression ( argument-expression-list[opt] )
6962 postfix-expression . identifier
6963 postfix-expression -> identifier
6964 postfix-expression ++
6965 postfix-expression --
6966 ( type-name ) { initializer-list }
6967 ( type-name ) { initializer-list , }
6969 argument-expression-list:
6970 argument-expression
6971 argument-expression-list , argument-expression
6973 primary-expression:
6974 identifier
6975 constant
6976 string-literal
6977 ( expression )
6978 generic-selection
6980 GNU extensions:
6982 primary-expression:
6983 __func__
6984 (treated as a keyword in GNU C)
6985 __FUNCTION__
6986 __PRETTY_FUNCTION__
6987 ( compound-statement )
6988 __builtin_va_arg ( assignment-expression , type-name )
6989 __builtin_offsetof ( type-name , offsetof-member-designator )
6990 __builtin_choose_expr ( assignment-expression ,
6991 assignment-expression ,
6992 assignment-expression )
6993 __builtin_types_compatible_p ( type-name , type-name )
6994 __builtin_complex ( assignment-expression , assignment-expression )
6995 __builtin_shuffle ( assignment-expression , assignment-expression )
6996 __builtin_shuffle ( assignment-expression ,
6997 assignment-expression ,
6998 assignment-expression, )
7000 offsetof-member-designator:
7001 identifier
7002 offsetof-member-designator . identifier
7003 offsetof-member-designator [ expression ]
7005 Objective-C:
7007 primary-expression:
7008 [ objc-receiver objc-message-args ]
7009 @selector ( objc-selector-arg )
7010 @protocol ( identifier )
7011 @encode ( type-name )
7012 objc-string-literal
7013 Classname . identifier
7016 static struct c_expr
7017 c_parser_postfix_expression (c_parser *parser)
7019 struct c_expr expr, e1;
7020 struct c_type_name *t1, *t2;
7021 location_t loc = c_parser_peek_token (parser)->location;;
7022 expr.original_code = ERROR_MARK;
7023 expr.original_type = NULL;
7024 switch (c_parser_peek_token (parser)->type)
7026 case CPP_NUMBER:
7027 expr.value = c_parser_peek_token (parser)->value;
7028 loc = c_parser_peek_token (parser)->location;
7029 c_parser_consume_token (parser);
7030 if (TREE_CODE (expr.value) == FIXED_CST
7031 && !targetm.fixed_point_supported_p ())
7033 error_at (loc, "fixed-point types not supported for this target");
7034 expr.value = error_mark_node;
7036 break;
7037 case CPP_CHAR:
7038 case CPP_CHAR16:
7039 case CPP_CHAR32:
7040 case CPP_WCHAR:
7041 expr.value = c_parser_peek_token (parser)->value;
7042 c_parser_consume_token (parser);
7043 break;
7044 case CPP_STRING:
7045 case CPP_STRING16:
7046 case CPP_STRING32:
7047 case CPP_WSTRING:
7048 case CPP_UTF8STRING:
7049 expr.value = c_parser_peek_token (parser)->value;
7050 expr.original_code = STRING_CST;
7051 c_parser_consume_token (parser);
7052 break;
7053 case CPP_OBJC_STRING:
7054 gcc_assert (c_dialect_objc ());
7055 expr.value
7056 = objc_build_string_object (c_parser_peek_token (parser)->value);
7057 c_parser_consume_token (parser);
7058 break;
7059 case CPP_NAME:
7060 switch (c_parser_peek_token (parser)->id_kind)
7062 case C_ID_ID:
7064 tree id = c_parser_peek_token (parser)->value;
7065 c_parser_consume_token (parser);
7066 expr.value = build_external_ref (loc, id,
7067 (c_parser_peek_token (parser)->type
7068 == CPP_OPEN_PAREN),
7069 &expr.original_type);
7070 break;
7072 case C_ID_CLASSNAME:
7074 /* Here we parse the Objective-C 2.0 Class.name dot
7075 syntax. */
7076 tree class_name = c_parser_peek_token (parser)->value;
7077 tree component;
7078 c_parser_consume_token (parser);
7079 gcc_assert (c_dialect_objc ());
7080 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7082 expr.value = error_mark_node;
7083 break;
7085 if (c_parser_next_token_is_not (parser, CPP_NAME))
7087 c_parser_error (parser, "expected identifier");
7088 expr.value = error_mark_node;
7089 break;
7091 component = c_parser_peek_token (parser)->value;
7092 c_parser_consume_token (parser);
7093 expr.value = objc_build_class_component_ref (class_name,
7094 component);
7095 break;
7097 default:
7098 c_parser_error (parser, "expected expression");
7099 expr.value = error_mark_node;
7100 break;
7102 break;
7103 case CPP_OPEN_PAREN:
7104 /* A parenthesized expression, statement expression or compound
7105 literal. */
7106 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7108 /* A statement expression. */
7109 tree stmt;
7110 location_t brace_loc;
7111 c_parser_consume_token (parser);
7112 brace_loc = c_parser_peek_token (parser)->location;
7113 c_parser_consume_token (parser);
7114 if (!building_stmt_list_p ())
7116 error_at (loc, "braced-group within expression allowed "
7117 "only inside a function");
7118 parser->error = true;
7119 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7120 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7121 expr.value = error_mark_node;
7122 break;
7124 stmt = c_begin_stmt_expr ();
7125 c_parser_compound_statement_nostart (parser);
7126 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7127 "expected %<)%>");
7128 pedwarn (loc, OPT_Wpedantic,
7129 "ISO C forbids braced-groups within expressions");
7130 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7131 mark_exp_read (expr.value);
7133 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7135 /* A compound literal. ??? Can we actually get here rather
7136 than going directly to
7137 c_parser_postfix_expression_after_paren_type from
7138 elsewhere? */
7139 location_t loc;
7140 struct c_type_name *type_name;
7141 c_parser_consume_token (parser);
7142 loc = c_parser_peek_token (parser)->location;
7143 type_name = c_parser_type_name (parser);
7144 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7145 "expected %<)%>");
7146 if (type_name == NULL)
7148 expr.value = error_mark_node;
7150 else
7151 expr = c_parser_postfix_expression_after_paren_type (parser,
7152 type_name,
7153 loc);
7155 else
7157 /* A parenthesized expression. */
7158 c_parser_consume_token (parser);
7159 expr = c_parser_expression (parser);
7160 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7161 TREE_NO_WARNING (expr.value) = 1;
7162 if (expr.original_code != C_MAYBE_CONST_EXPR)
7163 expr.original_code = ERROR_MARK;
7164 /* Don't change EXPR.ORIGINAL_TYPE. */
7165 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7166 "expected %<)%>");
7168 break;
7169 case CPP_KEYWORD:
7170 switch (c_parser_peek_token (parser)->keyword)
7172 case RID_FUNCTION_NAME:
7173 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7174 "%<__FUNCTION__%> predefined identifier");
7175 expr.value = fname_decl (loc,
7176 c_parser_peek_token (parser)->keyword,
7177 c_parser_peek_token (parser)->value);
7178 c_parser_consume_token (parser);
7179 break;
7180 case RID_PRETTY_FUNCTION_NAME:
7181 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7182 "%<__PRETTY_FUNCTION__%> predefined identifier");
7183 expr.value = fname_decl (loc,
7184 c_parser_peek_token (parser)->keyword,
7185 c_parser_peek_token (parser)->value);
7186 c_parser_consume_token (parser);
7187 break;
7188 case RID_C99_FUNCTION_NAME:
7189 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7190 "%<__func__%> predefined identifier");
7191 expr.value = fname_decl (loc,
7192 c_parser_peek_token (parser)->keyword,
7193 c_parser_peek_token (parser)->value);
7194 c_parser_consume_token (parser);
7195 break;
7196 case RID_VA_ARG:
7197 c_parser_consume_token (parser);
7198 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7200 expr.value = error_mark_node;
7201 break;
7203 e1 = c_parser_expr_no_commas (parser, NULL);
7204 mark_exp_read (e1.value);
7205 e1.value = c_fully_fold (e1.value, false, NULL);
7206 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7209 expr.value = error_mark_node;
7210 break;
7212 loc = c_parser_peek_token (parser)->location;
7213 t1 = c_parser_type_name (parser);
7214 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7215 "expected %<)%>");
7216 if (t1 == NULL)
7218 expr.value = error_mark_node;
7220 else
7222 tree type_expr = NULL_TREE;
7223 expr.value = c_build_va_arg (loc, e1.value,
7224 groktypename (t1, &type_expr, NULL));
7225 if (type_expr)
7227 expr.value = build2 (C_MAYBE_CONST_EXPR,
7228 TREE_TYPE (expr.value), type_expr,
7229 expr.value);
7230 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7233 break;
7234 case RID_OFFSETOF:
7235 c_parser_consume_token (parser);
7236 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7238 expr.value = error_mark_node;
7239 break;
7241 t1 = c_parser_type_name (parser);
7242 if (t1 == NULL)
7243 parser->error = true;
7244 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7245 gcc_assert (parser->error);
7246 if (parser->error)
7248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7249 expr.value = error_mark_node;
7250 break;
7254 tree type = groktypename (t1, NULL, NULL);
7255 tree offsetof_ref;
7256 if (type == error_mark_node)
7257 offsetof_ref = error_mark_node;
7258 else
7260 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7261 SET_EXPR_LOCATION (offsetof_ref, loc);
7263 /* Parse the second argument to __builtin_offsetof. We
7264 must have one identifier, and beyond that we want to
7265 accept sub structure and sub array references. */
7266 if (c_parser_next_token_is (parser, CPP_NAME))
7268 offsetof_ref = build_component_ref
7269 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7270 c_parser_consume_token (parser);
7271 while (c_parser_next_token_is (parser, CPP_DOT)
7272 || c_parser_next_token_is (parser,
7273 CPP_OPEN_SQUARE)
7274 || c_parser_next_token_is (parser,
7275 CPP_DEREF))
7277 if (c_parser_next_token_is (parser, CPP_DEREF))
7279 loc = c_parser_peek_token (parser)->location;
7280 offsetof_ref = build_array_ref (loc,
7281 offsetof_ref,
7282 integer_zero_node);
7283 goto do_dot;
7285 else if (c_parser_next_token_is (parser, CPP_DOT))
7287 do_dot:
7288 c_parser_consume_token (parser);
7289 if (c_parser_next_token_is_not (parser,
7290 CPP_NAME))
7292 c_parser_error (parser, "expected identifier");
7293 break;
7295 offsetof_ref = build_component_ref
7296 (loc, offsetof_ref,
7297 c_parser_peek_token (parser)->value);
7298 c_parser_consume_token (parser);
7300 else
7302 struct c_expr ce;
7303 tree idx;
7304 loc = c_parser_peek_token (parser)->location;
7305 c_parser_consume_token (parser);
7306 ce = c_parser_expression (parser);
7307 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7308 idx = ce.value;
7309 idx = c_fully_fold (idx, false, NULL);
7310 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7311 "expected %<]%>");
7312 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7316 else
7317 c_parser_error (parser, "expected identifier");
7318 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7319 "expected %<)%>");
7320 expr.value = fold_offsetof (offsetof_ref);
7322 break;
7323 case RID_CHOOSE_EXPR:
7325 vec<c_expr_t, va_gc> *cexpr_list;
7326 c_expr_t *e1_p, *e2_p, *e3_p;
7327 tree c;
7329 c_parser_consume_token (parser);
7330 if (!c_parser_get_builtin_args (parser,
7331 "__builtin_choose_expr",
7332 &cexpr_list, true))
7334 expr.value = error_mark_node;
7335 break;
7338 if (vec_safe_length (cexpr_list) != 3)
7340 error_at (loc, "wrong number of arguments to "
7341 "%<__builtin_choose_expr%>");
7342 expr.value = error_mark_node;
7343 break;
7346 e1_p = &(*cexpr_list)[0];
7347 e2_p = &(*cexpr_list)[1];
7348 e3_p = &(*cexpr_list)[2];
7350 c = e1_p->value;
7351 mark_exp_read (e2_p->value);
7352 mark_exp_read (e3_p->value);
7353 if (TREE_CODE (c) != INTEGER_CST
7354 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7355 error_at (loc,
7356 "first argument to %<__builtin_choose_expr%> not"
7357 " a constant");
7358 constant_expression_warning (c);
7359 expr = integer_zerop (c) ? *e3_p : *e2_p;
7360 break;
7362 case RID_TYPES_COMPATIBLE_P:
7363 c_parser_consume_token (parser);
7364 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7366 expr.value = error_mark_node;
7367 break;
7369 t1 = c_parser_type_name (parser);
7370 if (t1 == NULL)
7372 expr.value = error_mark_node;
7373 break;
7375 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7377 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7378 expr.value = error_mark_node;
7379 break;
7381 t2 = c_parser_type_name (parser);
7382 if (t2 == NULL)
7384 expr.value = error_mark_node;
7385 break;
7387 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7388 "expected %<)%>");
7390 tree e1, e2;
7391 e1 = groktypename (t1, NULL, NULL);
7392 e2 = groktypename (t2, NULL, NULL);
7393 if (e1 == error_mark_node || e2 == error_mark_node)
7395 expr.value = error_mark_node;
7396 break;
7399 e1 = TYPE_MAIN_VARIANT (e1);
7400 e2 = TYPE_MAIN_VARIANT (e2);
7402 expr.value
7403 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7405 break;
7406 case RID_BUILTIN_COMPLEX:
7408 vec<c_expr_t, va_gc> *cexpr_list;
7409 c_expr_t *e1_p, *e2_p;
7411 c_parser_consume_token (parser);
7412 if (!c_parser_get_builtin_args (parser,
7413 "__builtin_complex",
7414 &cexpr_list, false))
7416 expr.value = error_mark_node;
7417 break;
7420 if (vec_safe_length (cexpr_list) != 2)
7422 error_at (loc, "wrong number of arguments to "
7423 "%<__builtin_complex%>");
7424 expr.value = error_mark_node;
7425 break;
7428 e1_p = &(*cexpr_list)[0];
7429 e2_p = &(*cexpr_list)[1];
7431 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7432 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7433 e1_p->value = convert (TREE_TYPE (e1_p->value),
7434 TREE_OPERAND (e1_p->value, 0));
7435 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7436 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7437 e2_p->value = convert (TREE_TYPE (e2_p->value),
7438 TREE_OPERAND (e2_p->value, 0));
7439 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7440 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7441 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7442 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7444 error_at (loc, "%<__builtin_complex%> operand "
7445 "not of real binary floating-point type");
7446 expr.value = error_mark_node;
7447 break;
7449 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7450 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7452 error_at (loc,
7453 "%<__builtin_complex%> operands of different types");
7454 expr.value = error_mark_node;
7455 break;
7457 pedwarn_c90 (loc, OPT_Wpedantic,
7458 "ISO C90 does not support complex types");
7459 expr.value = build2 (COMPLEX_EXPR,
7460 build_complex_type
7461 (TYPE_MAIN_VARIANT
7462 (TREE_TYPE (e1_p->value))),
7463 e1_p->value, e2_p->value);
7464 break;
7466 case RID_BUILTIN_SHUFFLE:
7468 vec<c_expr_t, va_gc> *cexpr_list;
7469 unsigned int i;
7470 c_expr_t *p;
7472 c_parser_consume_token (parser);
7473 if (!c_parser_get_builtin_args (parser,
7474 "__builtin_shuffle",
7475 &cexpr_list, false))
7477 expr.value = error_mark_node;
7478 break;
7481 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7482 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7484 if (vec_safe_length (cexpr_list) == 2)
7485 expr.value =
7486 c_build_vec_perm_expr
7487 (loc, (*cexpr_list)[0].value,
7488 NULL_TREE, (*cexpr_list)[1].value);
7490 else if (vec_safe_length (cexpr_list) == 3)
7491 expr.value =
7492 c_build_vec_perm_expr
7493 (loc, (*cexpr_list)[0].value,
7494 (*cexpr_list)[1].value,
7495 (*cexpr_list)[2].value);
7496 else
7498 error_at (loc, "wrong number of arguments to "
7499 "%<__builtin_shuffle%>");
7500 expr.value = error_mark_node;
7502 break;
7504 case RID_AT_SELECTOR:
7505 gcc_assert (c_dialect_objc ());
7506 c_parser_consume_token (parser);
7507 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7509 expr.value = error_mark_node;
7510 break;
7513 tree sel = c_parser_objc_selector_arg (parser);
7514 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7515 "expected %<)%>");
7516 expr.value = objc_build_selector_expr (loc, sel);
7518 break;
7519 case RID_AT_PROTOCOL:
7520 gcc_assert (c_dialect_objc ());
7521 c_parser_consume_token (parser);
7522 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7524 expr.value = error_mark_node;
7525 break;
7527 if (c_parser_next_token_is_not (parser, CPP_NAME))
7529 c_parser_error (parser, "expected identifier");
7530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7531 expr.value = error_mark_node;
7532 break;
7535 tree id = c_parser_peek_token (parser)->value;
7536 c_parser_consume_token (parser);
7537 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7538 "expected %<)%>");
7539 expr.value = objc_build_protocol_expr (id);
7541 break;
7542 case RID_AT_ENCODE:
7543 /* Extension to support C-structures in the archiver. */
7544 gcc_assert (c_dialect_objc ());
7545 c_parser_consume_token (parser);
7546 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7548 expr.value = error_mark_node;
7549 break;
7551 t1 = c_parser_type_name (parser);
7552 if (t1 == NULL)
7554 expr.value = error_mark_node;
7555 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7556 break;
7558 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7559 "expected %<)%>");
7561 tree type = groktypename (t1, NULL, NULL);
7562 expr.value = objc_build_encode_expr (type);
7564 break;
7565 case RID_GENERIC:
7566 expr = c_parser_generic_selection (parser);
7567 break;
7568 case RID_CILK_SPAWN:
7569 c_parser_consume_token (parser);
7570 if (!flag_cilkplus)
7572 error_at (loc, "-fcilkplus must be enabled to use "
7573 "%<_Cilk_spawn%>");
7574 expr = c_parser_postfix_expression (parser);
7575 expr.value = error_mark_node;
7577 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7579 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7580 "are not permitted");
7581 /* Now flush out all the _Cilk_spawns. */
7582 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7583 c_parser_consume_token (parser);
7584 expr = c_parser_postfix_expression (parser);
7586 else
7588 expr = c_parser_postfix_expression (parser);
7589 expr.value = build_cilk_spawn (loc, expr.value);
7591 break;
7592 default:
7593 c_parser_error (parser, "expected expression");
7594 expr.value = error_mark_node;
7595 break;
7597 break;
7598 case CPP_OPEN_SQUARE:
7599 if (c_dialect_objc ())
7601 tree receiver, args;
7602 c_parser_consume_token (parser);
7603 receiver = c_parser_objc_receiver (parser);
7604 args = c_parser_objc_message_args (parser);
7605 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7606 "expected %<]%>");
7607 expr.value = objc_build_message_expr (receiver, args);
7608 break;
7610 /* Else fall through to report error. */
7611 default:
7612 c_parser_error (parser, "expected expression");
7613 expr.value = error_mark_node;
7614 break;
7616 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7619 /* Parse a postfix expression after a parenthesized type name: the
7620 brace-enclosed initializer of a compound literal, possibly followed
7621 by some postfix operators. This is separate because it is not
7622 possible to tell until after the type name whether a cast
7623 expression has a cast or a compound literal, or whether the operand
7624 of sizeof is a parenthesized type name or starts with a compound
7625 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7626 location of the first token after the parentheses around the type
7627 name. */
7629 static struct c_expr
7630 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7631 struct c_type_name *type_name,
7632 location_t type_loc)
7634 tree type;
7635 struct c_expr init;
7636 bool non_const;
7637 struct c_expr expr;
7638 location_t start_loc;
7639 tree type_expr = NULL_TREE;
7640 bool type_expr_const = true;
7641 check_compound_literal_type (type_loc, type_name);
7642 start_init (NULL_TREE, NULL, 0);
7643 type = groktypename (type_name, &type_expr, &type_expr_const);
7644 start_loc = c_parser_peek_token (parser)->location;
7645 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7647 error_at (type_loc, "compound literal has variable size");
7648 type = error_mark_node;
7650 init = c_parser_braced_init (parser, type, false);
7651 finish_init ();
7652 maybe_warn_string_init (type_loc, type, init);
7654 if (type != error_mark_node
7655 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7656 && current_function_decl)
7658 error ("compound literal qualified by address-space qualifier");
7659 type = error_mark_node;
7662 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7663 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7664 ? CONSTRUCTOR_NON_CONST (init.value)
7665 : init.original_code == C_MAYBE_CONST_EXPR);
7666 non_const |= !type_expr_const;
7667 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7668 expr.original_code = ERROR_MARK;
7669 expr.original_type = NULL;
7670 if (type_expr)
7672 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7674 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7675 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7677 else
7679 gcc_assert (!non_const);
7680 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7681 type_expr, expr.value);
7684 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7687 /* Callback function for sizeof_pointer_memaccess_warning to compare
7688 types. */
7690 static bool
7691 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7693 return comptypes (type1, type2) == 1;
7696 /* Parse a postfix expression after the initial primary or compound
7697 literal; that is, parse a series of postfix operators.
7699 EXPR_LOC is the location of the primary expression. */
7701 static struct c_expr
7702 c_parser_postfix_expression_after_primary (c_parser *parser,
7703 location_t expr_loc,
7704 struct c_expr expr)
7706 struct c_expr orig_expr;
7707 tree ident, idx;
7708 location_t sizeof_arg_loc[3];
7709 tree sizeof_arg[3];
7710 unsigned int literal_zero_mask;
7711 unsigned int i;
7712 vec<tree, va_gc> *exprlist;
7713 vec<tree, va_gc> *origtypes = NULL;
7714 vec<location_t> arg_loc = vNULL;
7716 while (true)
7718 location_t op_loc = c_parser_peek_token (parser)->location;
7719 switch (c_parser_peek_token (parser)->type)
7721 case CPP_OPEN_SQUARE:
7722 /* Array reference. */
7723 c_parser_consume_token (parser);
7724 if (flag_cilkplus
7725 && c_parser_peek_token (parser)->type == CPP_COLON)
7726 /* If we are here, then we have something like this:
7727 Array [ : ]
7729 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7730 expr.value);
7731 else
7733 idx = c_parser_expression (parser).value;
7734 /* Here we have 3 options:
7735 1. Array [EXPR] -- Normal Array call.
7736 2. Array [EXPR : EXPR] -- Array notation without stride.
7737 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7739 For 1, we just handle it just like a normal array expression.
7740 For 2 and 3 we handle it like we handle array notations. The
7741 idx value we have above becomes the initial/start index.
7743 if (flag_cilkplus
7744 && c_parser_peek_token (parser)->type == CPP_COLON)
7745 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7746 expr.value);
7747 else
7749 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7750 "expected %<]%>");
7751 expr.value = build_array_ref (op_loc, expr.value, idx);
7754 expr.original_code = ERROR_MARK;
7755 expr.original_type = NULL;
7756 break;
7757 case CPP_OPEN_PAREN:
7758 /* Function call. */
7759 c_parser_consume_token (parser);
7760 for (i = 0; i < 3; i++)
7762 sizeof_arg[i] = NULL_TREE;
7763 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7765 literal_zero_mask = 0;
7766 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7767 exprlist = NULL;
7768 else
7769 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7770 sizeof_arg_loc, sizeof_arg,
7771 &arg_loc, &literal_zero_mask);
7772 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7773 "expected %<)%>");
7774 orig_expr = expr;
7775 mark_exp_read (expr.value);
7776 if (warn_sizeof_pointer_memaccess)
7777 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7778 expr.value, exprlist,
7779 sizeof_arg,
7780 sizeof_ptr_memacc_comptypes);
7781 if (warn_memset_transposed_args
7782 && TREE_CODE (expr.value) == FUNCTION_DECL
7783 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7784 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7785 && vec_safe_length (exprlist) == 3
7786 && integer_zerop ((*exprlist)[2])
7787 && (literal_zero_mask & (1 << 2)) != 0
7788 && (!integer_zerop ((*exprlist)[1])
7789 || (literal_zero_mask & (1 << 1)) == 0))
7790 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7791 "%<memset%> used with constant zero length parameter; "
7792 "this could be due to transposed parameters");
7794 expr.value
7795 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7796 exprlist, origtypes);
7797 expr.original_code = ERROR_MARK;
7798 if (TREE_CODE (expr.value) == INTEGER_CST
7799 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7800 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7801 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7802 expr.original_code = C_MAYBE_CONST_EXPR;
7803 expr.original_type = NULL;
7804 if (exprlist)
7806 release_tree_vector (exprlist);
7807 release_tree_vector (origtypes);
7809 arg_loc.release ();
7810 break;
7811 case CPP_DOT:
7812 /* Structure element reference. */
7813 c_parser_consume_token (parser);
7814 expr = default_function_array_conversion (expr_loc, expr);
7815 if (c_parser_next_token_is (parser, CPP_NAME))
7816 ident = c_parser_peek_token (parser)->value;
7817 else
7819 c_parser_error (parser, "expected identifier");
7820 expr.value = error_mark_node;
7821 expr.original_code = ERROR_MARK;
7822 expr.original_type = NULL;
7823 return expr;
7825 c_parser_consume_token (parser);
7826 expr.value = build_component_ref (op_loc, expr.value, ident);
7827 expr.original_code = ERROR_MARK;
7828 if (TREE_CODE (expr.value) != COMPONENT_REF)
7829 expr.original_type = NULL;
7830 else
7832 /* Remember the original type of a bitfield. */
7833 tree field = TREE_OPERAND (expr.value, 1);
7834 if (TREE_CODE (field) != FIELD_DECL)
7835 expr.original_type = NULL;
7836 else
7837 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7839 break;
7840 case CPP_DEREF:
7841 /* Structure element reference. */
7842 c_parser_consume_token (parser);
7843 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7844 if (c_parser_next_token_is (parser, CPP_NAME))
7845 ident = c_parser_peek_token (parser)->value;
7846 else
7848 c_parser_error (parser, "expected identifier");
7849 expr.value = error_mark_node;
7850 expr.original_code = ERROR_MARK;
7851 expr.original_type = NULL;
7852 return expr;
7854 c_parser_consume_token (parser);
7855 expr.value = build_component_ref (op_loc,
7856 build_indirect_ref (op_loc,
7857 expr.value,
7858 RO_ARROW),
7859 ident);
7860 expr.original_code = ERROR_MARK;
7861 if (TREE_CODE (expr.value) != COMPONENT_REF)
7862 expr.original_type = NULL;
7863 else
7865 /* Remember the original type of a bitfield. */
7866 tree field = TREE_OPERAND (expr.value, 1);
7867 if (TREE_CODE (field) != FIELD_DECL)
7868 expr.original_type = NULL;
7869 else
7870 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7872 break;
7873 case CPP_PLUS_PLUS:
7874 /* Postincrement. */
7875 c_parser_consume_token (parser);
7876 /* If the expressions have array notations, we expand them. */
7877 if (flag_cilkplus
7878 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7879 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7880 else
7882 expr = default_function_array_read_conversion (expr_loc, expr);
7883 expr.value = build_unary_op (op_loc,
7884 POSTINCREMENT_EXPR, expr.value, 0);
7886 expr.original_code = ERROR_MARK;
7887 expr.original_type = NULL;
7888 break;
7889 case CPP_MINUS_MINUS:
7890 /* Postdecrement. */
7891 c_parser_consume_token (parser);
7892 /* If the expressions have array notations, we expand them. */
7893 if (flag_cilkplus
7894 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7895 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7896 else
7898 expr = default_function_array_read_conversion (expr_loc, expr);
7899 expr.value = build_unary_op (op_loc,
7900 POSTDECREMENT_EXPR, expr.value, 0);
7902 expr.original_code = ERROR_MARK;
7903 expr.original_type = NULL;
7904 break;
7905 default:
7906 return expr;
7911 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7913 expression:
7914 assignment-expression
7915 expression , assignment-expression
7918 static struct c_expr
7919 c_parser_expression (c_parser *parser)
7921 location_t tloc = c_parser_peek_token (parser)->location;
7922 struct c_expr expr;
7923 expr = c_parser_expr_no_commas (parser, NULL);
7924 if (c_parser_next_token_is (parser, CPP_COMMA))
7925 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7926 while (c_parser_next_token_is (parser, CPP_COMMA))
7928 struct c_expr next;
7929 tree lhsval;
7930 location_t loc = c_parser_peek_token (parser)->location;
7931 location_t expr_loc;
7932 c_parser_consume_token (parser);
7933 expr_loc = c_parser_peek_token (parser)->location;
7934 lhsval = expr.value;
7935 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7936 lhsval = TREE_OPERAND (lhsval, 1);
7937 if (DECL_P (lhsval) || handled_component_p (lhsval))
7938 mark_exp_read (lhsval);
7939 next = c_parser_expr_no_commas (parser, NULL);
7940 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7941 expr.value = build_compound_expr (loc, expr.value, next.value);
7942 expr.original_code = COMPOUND_EXPR;
7943 expr.original_type = next.original_type;
7945 return expr;
7948 /* Parse an expression and convert functions or arrays to pointers and
7949 lvalues to rvalues. */
7951 static struct c_expr
7952 c_parser_expression_conv (c_parser *parser)
7954 struct c_expr expr;
7955 location_t loc = c_parser_peek_token (parser)->location;
7956 expr = c_parser_expression (parser);
7957 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7958 return expr;
7961 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
7962 argument is a literal zero alone and if so, set it in literal_zero_mask. */
7964 static inline void
7965 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
7966 unsigned int idx)
7968 if (idx >= HOST_BITS_PER_INT)
7969 return;
7971 c_token *tok = c_parser_peek_token (parser);
7972 switch (tok->type)
7974 case CPP_NUMBER:
7975 case CPP_CHAR:
7976 case CPP_WCHAR:
7977 case CPP_CHAR16:
7978 case CPP_CHAR32:
7979 /* If a parameter is literal zero alone, remember it
7980 for -Wmemset-transposed-args warning. */
7981 if (integer_zerop (tok->value)
7982 && !TREE_OVERFLOW (tok->value)
7983 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7984 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
7985 *literal_zero_mask |= 1U << idx;
7986 default:
7987 break;
7991 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7992 functions and arrays to pointers and lvalues to rvalues. If
7993 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7994 locations of function arguments into this vector.
7996 nonempty-expr-list:
7997 assignment-expression
7998 nonempty-expr-list , assignment-expression
8001 static vec<tree, va_gc> *
8002 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8003 vec<tree, va_gc> **p_orig_types,
8004 location_t *sizeof_arg_loc, tree *sizeof_arg,
8005 vec<location_t> *locations,
8006 unsigned int *literal_zero_mask)
8008 vec<tree, va_gc> *ret;
8009 vec<tree, va_gc> *orig_types;
8010 struct c_expr expr;
8011 location_t loc = c_parser_peek_token (parser)->location;
8012 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8013 unsigned int idx = 0;
8015 ret = make_tree_vector ();
8016 if (p_orig_types == NULL)
8017 orig_types = NULL;
8018 else
8019 orig_types = make_tree_vector ();
8021 if (sizeof_arg != NULL
8022 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8023 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8024 if (literal_zero_mask)
8025 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8026 expr = c_parser_expr_no_commas (parser, NULL);
8027 if (convert_p)
8028 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8029 if (fold_p)
8030 expr.value = c_fully_fold (expr.value, false, NULL);
8031 ret->quick_push (expr.value);
8032 if (orig_types)
8033 orig_types->quick_push (expr.original_type);
8034 if (locations)
8035 locations->safe_push (loc);
8036 if (sizeof_arg != NULL
8037 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8038 && expr.original_code == SIZEOF_EXPR)
8040 sizeof_arg[0] = c_last_sizeof_arg;
8041 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8043 while (c_parser_next_token_is (parser, CPP_COMMA))
8045 c_parser_consume_token (parser);
8046 loc = c_parser_peek_token (parser)->location;
8047 if (sizeof_arg != NULL
8048 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8049 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8050 else
8051 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8052 if (literal_zero_mask)
8053 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8054 expr = c_parser_expr_no_commas (parser, NULL);
8055 if (convert_p)
8056 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8057 if (fold_p)
8058 expr.value = c_fully_fold (expr.value, false, NULL);
8059 vec_safe_push (ret, expr.value);
8060 if (orig_types)
8061 vec_safe_push (orig_types, expr.original_type);
8062 if (locations)
8063 locations->safe_push (loc);
8064 if (++idx < 3
8065 && sizeof_arg != NULL
8066 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8067 && expr.original_code == SIZEOF_EXPR)
8069 sizeof_arg[idx] = c_last_sizeof_arg;
8070 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8073 if (orig_types)
8074 *p_orig_types = orig_types;
8075 return ret;
8078 /* Parse Objective-C-specific constructs. */
8080 /* Parse an objc-class-definition.
8082 objc-class-definition:
8083 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8084 objc-class-instance-variables[opt] objc-methodprotolist @end
8085 @implementation identifier objc-superclass[opt]
8086 objc-class-instance-variables[opt]
8087 @interface identifier ( identifier ) objc-protocol-refs[opt]
8088 objc-methodprotolist @end
8089 @interface identifier ( ) objc-protocol-refs[opt]
8090 objc-methodprotolist @end
8091 @implementation identifier ( identifier )
8093 objc-superclass:
8094 : identifier
8096 "@interface identifier (" must start "@interface identifier (
8097 identifier ) ...": objc-methodprotolist in the first production may
8098 not start with a parenthesized identifier as a declarator of a data
8099 definition with no declaration specifiers if the objc-superclass,
8100 objc-protocol-refs and objc-class-instance-variables are omitted. */
8102 static void
8103 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8105 bool iface_p;
8106 tree id1;
8107 tree superclass;
8108 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8109 iface_p = true;
8110 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8111 iface_p = false;
8112 else
8113 gcc_unreachable ();
8115 c_parser_consume_token (parser);
8116 if (c_parser_next_token_is_not (parser, CPP_NAME))
8118 c_parser_error (parser, "expected identifier");
8119 return;
8121 id1 = c_parser_peek_token (parser)->value;
8122 c_parser_consume_token (parser);
8123 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8125 /* We have a category or class extension. */
8126 tree id2;
8127 tree proto = NULL_TREE;
8128 c_parser_consume_token (parser);
8129 if (c_parser_next_token_is_not (parser, CPP_NAME))
8131 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8133 /* We have a class extension. */
8134 id2 = NULL_TREE;
8136 else
8138 c_parser_error (parser, "expected identifier or %<)%>");
8139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8140 return;
8143 else
8145 id2 = c_parser_peek_token (parser)->value;
8146 c_parser_consume_token (parser);
8148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8149 if (!iface_p)
8151 objc_start_category_implementation (id1, id2);
8152 return;
8154 if (c_parser_next_token_is (parser, CPP_LESS))
8155 proto = c_parser_objc_protocol_refs (parser);
8156 objc_start_category_interface (id1, id2, proto, attributes);
8157 c_parser_objc_methodprotolist (parser);
8158 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8159 objc_finish_interface ();
8160 return;
8162 if (c_parser_next_token_is (parser, CPP_COLON))
8164 c_parser_consume_token (parser);
8165 if (c_parser_next_token_is_not (parser, CPP_NAME))
8167 c_parser_error (parser, "expected identifier");
8168 return;
8170 superclass = c_parser_peek_token (parser)->value;
8171 c_parser_consume_token (parser);
8173 else
8174 superclass = NULL_TREE;
8175 if (iface_p)
8177 tree proto = NULL_TREE;
8178 if (c_parser_next_token_is (parser, CPP_LESS))
8179 proto = c_parser_objc_protocol_refs (parser);
8180 objc_start_class_interface (id1, superclass, proto, attributes);
8182 else
8183 objc_start_class_implementation (id1, superclass);
8184 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8185 c_parser_objc_class_instance_variables (parser);
8186 if (iface_p)
8188 objc_continue_interface ();
8189 c_parser_objc_methodprotolist (parser);
8190 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8191 objc_finish_interface ();
8193 else
8195 objc_continue_implementation ();
8196 return;
8200 /* Parse objc-class-instance-variables.
8202 objc-class-instance-variables:
8203 { objc-instance-variable-decl-list[opt] }
8205 objc-instance-variable-decl-list:
8206 objc-visibility-spec
8207 objc-instance-variable-decl ;
8209 objc-instance-variable-decl-list objc-visibility-spec
8210 objc-instance-variable-decl-list objc-instance-variable-decl ;
8211 objc-instance-variable-decl-list ;
8213 objc-visibility-spec:
8214 @private
8215 @protected
8216 @public
8218 objc-instance-variable-decl:
8219 struct-declaration
8222 static void
8223 c_parser_objc_class_instance_variables (c_parser *parser)
8225 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8226 c_parser_consume_token (parser);
8227 while (c_parser_next_token_is_not (parser, CPP_EOF))
8229 tree decls;
8230 /* Parse any stray semicolon. */
8231 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8233 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8234 "extra semicolon");
8235 c_parser_consume_token (parser);
8236 continue;
8238 /* Stop if at the end of the instance variables. */
8239 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8241 c_parser_consume_token (parser);
8242 break;
8244 /* Parse any objc-visibility-spec. */
8245 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8247 c_parser_consume_token (parser);
8248 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8249 continue;
8251 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8253 c_parser_consume_token (parser);
8254 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8255 continue;
8257 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8259 c_parser_consume_token (parser);
8260 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8261 continue;
8263 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8265 c_parser_consume_token (parser);
8266 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8267 continue;
8269 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8271 c_parser_pragma (parser, pragma_external);
8272 continue;
8275 /* Parse some comma-separated declarations. */
8276 decls = c_parser_struct_declaration (parser);
8277 if (decls == NULL)
8279 /* There is a syntax error. We want to skip the offending
8280 tokens up to the next ';' (included) or '}'
8281 (excluded). */
8283 /* First, skip manually a ')' or ']'. This is because they
8284 reduce the nesting level, so c_parser_skip_until_found()
8285 wouldn't be able to skip past them. */
8286 c_token *token = c_parser_peek_token (parser);
8287 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8288 c_parser_consume_token (parser);
8290 /* Then, do the standard skipping. */
8291 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8293 /* We hopefully recovered. Start normal parsing again. */
8294 parser->error = false;
8295 continue;
8297 else
8299 /* Comma-separated instance variables are chained together
8300 in reverse order; add them one by one. */
8301 tree ivar = nreverse (decls);
8302 for (; ivar; ivar = DECL_CHAIN (ivar))
8303 objc_add_instance_variable (copy_node (ivar));
8305 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8309 /* Parse an objc-class-declaration.
8311 objc-class-declaration:
8312 @class identifier-list ;
8315 static void
8316 c_parser_objc_class_declaration (c_parser *parser)
8318 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8319 c_parser_consume_token (parser);
8320 /* Any identifiers, including those declared as type names, are OK
8321 here. */
8322 while (true)
8324 tree id;
8325 if (c_parser_next_token_is_not (parser, CPP_NAME))
8327 c_parser_error (parser, "expected identifier");
8328 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8329 parser->error = false;
8330 return;
8332 id = c_parser_peek_token (parser)->value;
8333 objc_declare_class (id);
8334 c_parser_consume_token (parser);
8335 if (c_parser_next_token_is (parser, CPP_COMMA))
8336 c_parser_consume_token (parser);
8337 else
8338 break;
8340 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8343 /* Parse an objc-alias-declaration.
8345 objc-alias-declaration:
8346 @compatibility_alias identifier identifier ;
8349 static void
8350 c_parser_objc_alias_declaration (c_parser *parser)
8352 tree id1, id2;
8353 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8354 c_parser_consume_token (parser);
8355 if (c_parser_next_token_is_not (parser, CPP_NAME))
8357 c_parser_error (parser, "expected identifier");
8358 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8359 return;
8361 id1 = c_parser_peek_token (parser)->value;
8362 c_parser_consume_token (parser);
8363 if (c_parser_next_token_is_not (parser, CPP_NAME))
8365 c_parser_error (parser, "expected identifier");
8366 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8367 return;
8369 id2 = c_parser_peek_token (parser)->value;
8370 c_parser_consume_token (parser);
8371 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8372 objc_declare_alias (id1, id2);
8375 /* Parse an objc-protocol-definition.
8377 objc-protocol-definition:
8378 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8379 @protocol identifier-list ;
8381 "@protocol identifier ;" should be resolved as "@protocol
8382 identifier-list ;": objc-methodprotolist may not start with a
8383 semicolon in the first alternative if objc-protocol-refs are
8384 omitted. */
8386 static void
8387 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8389 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8391 c_parser_consume_token (parser);
8392 if (c_parser_next_token_is_not (parser, CPP_NAME))
8394 c_parser_error (parser, "expected identifier");
8395 return;
8397 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8398 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8400 /* Any identifiers, including those declared as type names, are
8401 OK here. */
8402 while (true)
8404 tree id;
8405 if (c_parser_next_token_is_not (parser, CPP_NAME))
8407 c_parser_error (parser, "expected identifier");
8408 break;
8410 id = c_parser_peek_token (parser)->value;
8411 objc_declare_protocol (id, attributes);
8412 c_parser_consume_token (parser);
8413 if (c_parser_next_token_is (parser, CPP_COMMA))
8414 c_parser_consume_token (parser);
8415 else
8416 break;
8418 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8420 else
8422 tree id = c_parser_peek_token (parser)->value;
8423 tree proto = NULL_TREE;
8424 c_parser_consume_token (parser);
8425 if (c_parser_next_token_is (parser, CPP_LESS))
8426 proto = c_parser_objc_protocol_refs (parser);
8427 parser->objc_pq_context = true;
8428 objc_start_protocol (id, proto, attributes);
8429 c_parser_objc_methodprotolist (parser);
8430 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8431 parser->objc_pq_context = false;
8432 objc_finish_interface ();
8436 /* Parse an objc-method-type.
8438 objc-method-type:
8442 Return true if it is a class method (+) and false if it is
8443 an instance method (-).
8445 static inline bool
8446 c_parser_objc_method_type (c_parser *parser)
8448 switch (c_parser_peek_token (parser)->type)
8450 case CPP_PLUS:
8451 c_parser_consume_token (parser);
8452 return true;
8453 case CPP_MINUS:
8454 c_parser_consume_token (parser);
8455 return false;
8456 default:
8457 gcc_unreachable ();
8461 /* Parse an objc-method-definition.
8463 objc-method-definition:
8464 objc-method-type objc-method-decl ;[opt] compound-statement
8467 static void
8468 c_parser_objc_method_definition (c_parser *parser)
8470 bool is_class_method = c_parser_objc_method_type (parser);
8471 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8472 parser->objc_pq_context = true;
8473 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8474 &expr);
8475 if (decl == error_mark_node)
8476 return; /* Bail here. */
8478 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8480 c_parser_consume_token (parser);
8481 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8482 "extra semicolon in method definition specified");
8485 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8487 c_parser_error (parser, "expected %<{%>");
8488 return;
8491 parser->objc_pq_context = false;
8492 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8494 add_stmt (c_parser_compound_statement (parser));
8495 objc_finish_method_definition (current_function_decl);
8497 else
8499 /* This code is executed when we find a method definition
8500 outside of an @implementation context (or invalid for other
8501 reasons). Parse the method (to keep going) but do not emit
8502 any code.
8504 c_parser_compound_statement (parser);
8508 /* Parse an objc-methodprotolist.
8510 objc-methodprotolist:
8511 empty
8512 objc-methodprotolist objc-methodproto
8513 objc-methodprotolist declaration
8514 objc-methodprotolist ;
8515 @optional
8516 @required
8518 The declaration is a data definition, which may be missing
8519 declaration specifiers under the same rules and diagnostics as
8520 other data definitions outside functions, and the stray semicolon
8521 is diagnosed the same way as a stray semicolon outside a
8522 function. */
8524 static void
8525 c_parser_objc_methodprotolist (c_parser *parser)
8527 while (true)
8529 /* The list is terminated by @end. */
8530 switch (c_parser_peek_token (parser)->type)
8532 case CPP_SEMICOLON:
8533 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8534 "ISO C does not allow extra %<;%> outside of a function");
8535 c_parser_consume_token (parser);
8536 break;
8537 case CPP_PLUS:
8538 case CPP_MINUS:
8539 c_parser_objc_methodproto (parser);
8540 break;
8541 case CPP_PRAGMA:
8542 c_parser_pragma (parser, pragma_external);
8543 break;
8544 case CPP_EOF:
8545 return;
8546 default:
8547 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8548 return;
8549 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8550 c_parser_objc_at_property_declaration (parser);
8551 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8553 objc_set_method_opt (true);
8554 c_parser_consume_token (parser);
8556 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8558 objc_set_method_opt (false);
8559 c_parser_consume_token (parser);
8561 else
8562 c_parser_declaration_or_fndef (parser, false, false, true,
8563 false, true, NULL, vNULL);
8564 break;
8569 /* Parse an objc-methodproto.
8571 objc-methodproto:
8572 objc-method-type objc-method-decl ;
8575 static void
8576 c_parser_objc_methodproto (c_parser *parser)
8578 bool is_class_method = c_parser_objc_method_type (parser);
8579 tree decl, attributes = NULL_TREE;
8581 /* Remember protocol qualifiers in prototypes. */
8582 parser->objc_pq_context = true;
8583 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8584 NULL);
8585 /* Forget protocol qualifiers now. */
8586 parser->objc_pq_context = false;
8588 /* Do not allow the presence of attributes to hide an erroneous
8589 method implementation in the interface section. */
8590 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8592 c_parser_error (parser, "expected %<;%>");
8593 return;
8596 if (decl != error_mark_node)
8597 objc_add_method_declaration (is_class_method, decl, attributes);
8599 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8602 /* If we are at a position that method attributes may be present, check that
8603 there are not any parsed already (a syntax error) and then collect any
8604 specified at the current location. Finally, if new attributes were present,
8605 check that the next token is legal ( ';' for decls and '{' for defs). */
8607 static bool
8608 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8610 bool bad = false;
8611 if (*attributes)
8613 c_parser_error (parser,
8614 "method attributes must be specified at the end only");
8615 *attributes = NULL_TREE;
8616 bad = true;
8619 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8620 *attributes = c_parser_attributes (parser);
8622 /* If there were no attributes here, just report any earlier error. */
8623 if (*attributes == NULL_TREE || bad)
8624 return bad;
8626 /* If the attributes are followed by a ; or {, then just report any earlier
8627 error. */
8628 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8629 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8630 return bad;
8632 /* We've got attributes, but not at the end. */
8633 c_parser_error (parser,
8634 "expected %<;%> or %<{%> after method attribute definition");
8635 return true;
8638 /* Parse an objc-method-decl.
8640 objc-method-decl:
8641 ( objc-type-name ) objc-selector
8642 objc-selector
8643 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8644 objc-keyword-selector objc-optparmlist
8645 attributes
8647 objc-keyword-selector:
8648 objc-keyword-decl
8649 objc-keyword-selector objc-keyword-decl
8651 objc-keyword-decl:
8652 objc-selector : ( objc-type-name ) identifier
8653 objc-selector : identifier
8654 : ( objc-type-name ) identifier
8655 : identifier
8657 objc-optparmlist:
8658 objc-optparms objc-optellipsis
8660 objc-optparms:
8661 empty
8662 objc-opt-parms , parameter-declaration
8664 objc-optellipsis:
8665 empty
8666 , ...
8669 static tree
8670 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8671 tree *attributes, tree *expr)
8673 tree type = NULL_TREE;
8674 tree sel;
8675 tree parms = NULL_TREE;
8676 bool ellipsis = false;
8677 bool attr_err = false;
8679 *attributes = NULL_TREE;
8680 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8682 c_parser_consume_token (parser);
8683 type = c_parser_objc_type_name (parser);
8684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8686 sel = c_parser_objc_selector (parser);
8687 /* If there is no selector, or a colon follows, we have an
8688 objc-keyword-selector. If there is a selector, and a colon does
8689 not follow, that selector ends the objc-method-decl. */
8690 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8692 tree tsel = sel;
8693 tree list = NULL_TREE;
8694 while (true)
8696 tree atype = NULL_TREE, id, keyworddecl;
8697 tree param_attr = NULL_TREE;
8698 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8699 break;
8700 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8702 c_parser_consume_token (parser);
8703 atype = c_parser_objc_type_name (parser);
8704 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8705 "expected %<)%>");
8707 /* New ObjC allows attributes on method parameters. */
8708 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8709 param_attr = c_parser_attributes (parser);
8710 if (c_parser_next_token_is_not (parser, CPP_NAME))
8712 c_parser_error (parser, "expected identifier");
8713 return error_mark_node;
8715 id = c_parser_peek_token (parser)->value;
8716 c_parser_consume_token (parser);
8717 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8718 list = chainon (list, keyworddecl);
8719 tsel = c_parser_objc_selector (parser);
8720 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8721 break;
8724 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8726 /* Parse the optional parameter list. Optional Objective-C
8727 method parameters follow the C syntax, and may include '...'
8728 to denote a variable number of arguments. */
8729 parms = make_node (TREE_LIST);
8730 while (c_parser_next_token_is (parser, CPP_COMMA))
8732 struct c_parm *parm;
8733 c_parser_consume_token (parser);
8734 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8736 ellipsis = true;
8737 c_parser_consume_token (parser);
8738 attr_err |= c_parser_objc_maybe_method_attributes
8739 (parser, attributes) ;
8740 break;
8742 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8743 if (parm == NULL)
8744 break;
8745 parms = chainon (parms,
8746 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8748 sel = list;
8750 else
8751 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8753 if (sel == NULL)
8755 c_parser_error (parser, "objective-c method declaration is expected");
8756 return error_mark_node;
8759 if (attr_err)
8760 return error_mark_node;
8762 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8765 /* Parse an objc-type-name.
8767 objc-type-name:
8768 objc-type-qualifiers[opt] type-name
8769 objc-type-qualifiers[opt]
8771 objc-type-qualifiers:
8772 objc-type-qualifier
8773 objc-type-qualifiers objc-type-qualifier
8775 objc-type-qualifier: one of
8776 in out inout bycopy byref oneway
8779 static tree
8780 c_parser_objc_type_name (c_parser *parser)
8782 tree quals = NULL_TREE;
8783 struct c_type_name *type_name = NULL;
8784 tree type = NULL_TREE;
8785 while (true)
8787 c_token *token = c_parser_peek_token (parser);
8788 if (token->type == CPP_KEYWORD
8789 && (token->keyword == RID_IN
8790 || token->keyword == RID_OUT
8791 || token->keyword == RID_INOUT
8792 || token->keyword == RID_BYCOPY
8793 || token->keyword == RID_BYREF
8794 || token->keyword == RID_ONEWAY))
8796 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8797 c_parser_consume_token (parser);
8799 else
8800 break;
8802 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8803 type_name = c_parser_type_name (parser);
8804 if (type_name)
8805 type = groktypename (type_name, NULL, NULL);
8807 /* If the type is unknown, and error has already been produced and
8808 we need to recover from the error. In that case, use NULL_TREE
8809 for the type, as if no type had been specified; this will use the
8810 default type ('id') which is good for error recovery. */
8811 if (type == error_mark_node)
8812 type = NULL_TREE;
8814 return build_tree_list (quals, type);
8817 /* Parse objc-protocol-refs.
8819 objc-protocol-refs:
8820 < identifier-list >
8823 static tree
8824 c_parser_objc_protocol_refs (c_parser *parser)
8826 tree list = NULL_TREE;
8827 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8828 c_parser_consume_token (parser);
8829 /* Any identifiers, including those declared as type names, are OK
8830 here. */
8831 while (true)
8833 tree id;
8834 if (c_parser_next_token_is_not (parser, CPP_NAME))
8836 c_parser_error (parser, "expected identifier");
8837 break;
8839 id = c_parser_peek_token (parser)->value;
8840 list = chainon (list, build_tree_list (NULL_TREE, id));
8841 c_parser_consume_token (parser);
8842 if (c_parser_next_token_is (parser, CPP_COMMA))
8843 c_parser_consume_token (parser);
8844 else
8845 break;
8847 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8848 return list;
8851 /* Parse an objc-try-catch-finally-statement.
8853 objc-try-catch-finally-statement:
8854 @try compound-statement objc-catch-list[opt]
8855 @try compound-statement objc-catch-list[opt] @finally compound-statement
8857 objc-catch-list:
8858 @catch ( objc-catch-parameter-declaration ) compound-statement
8859 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8861 objc-catch-parameter-declaration:
8862 parameter-declaration
8863 '...'
8865 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8867 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8868 for C++. Keep them in sync. */
8870 static void
8871 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8873 location_t location;
8874 tree stmt;
8876 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8877 c_parser_consume_token (parser);
8878 location = c_parser_peek_token (parser)->location;
8879 objc_maybe_warn_exceptions (location);
8880 stmt = c_parser_compound_statement (parser);
8881 objc_begin_try_stmt (location, stmt);
8883 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8885 struct c_parm *parm;
8886 tree parameter_declaration = error_mark_node;
8887 bool seen_open_paren = false;
8889 c_parser_consume_token (parser);
8890 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8891 seen_open_paren = true;
8892 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8894 /* We have "@catch (...)" (where the '...' are literally
8895 what is in the code). Skip the '...'.
8896 parameter_declaration is set to NULL_TREE, and
8897 objc_being_catch_clauses() knows that that means
8898 '...'. */
8899 c_parser_consume_token (parser);
8900 parameter_declaration = NULL_TREE;
8902 else
8904 /* We have "@catch (NSException *exception)" or something
8905 like that. Parse the parameter declaration. */
8906 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8907 if (parm == NULL)
8908 parameter_declaration = error_mark_node;
8909 else
8910 parameter_declaration = grokparm (parm, NULL);
8912 if (seen_open_paren)
8913 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8914 else
8916 /* If there was no open parenthesis, we are recovering from
8917 an error, and we are trying to figure out what mistake
8918 the user has made. */
8920 /* If there is an immediate closing parenthesis, the user
8921 probably forgot the opening one (ie, they typed "@catch
8922 NSException *e)". Parse the closing parenthesis and keep
8923 going. */
8924 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8925 c_parser_consume_token (parser);
8927 /* If these is no immediate closing parenthesis, the user
8928 probably doesn't know that parenthesis are required at
8929 all (ie, they typed "@catch NSException *e"). So, just
8930 forget about the closing parenthesis and keep going. */
8932 objc_begin_catch_clause (parameter_declaration);
8933 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8934 c_parser_compound_statement_nostart (parser);
8935 objc_finish_catch_clause ();
8937 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8939 c_parser_consume_token (parser);
8940 location = c_parser_peek_token (parser)->location;
8941 stmt = c_parser_compound_statement (parser);
8942 objc_build_finally_clause (location, stmt);
8944 objc_finish_try_stmt ();
8947 /* Parse an objc-synchronized-statement.
8949 objc-synchronized-statement:
8950 @synchronized ( expression ) compound-statement
8953 static void
8954 c_parser_objc_synchronized_statement (c_parser *parser)
8956 location_t loc;
8957 tree expr, stmt;
8958 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8959 c_parser_consume_token (parser);
8960 loc = c_parser_peek_token (parser)->location;
8961 objc_maybe_warn_exceptions (loc);
8962 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8964 struct c_expr ce = c_parser_expression (parser);
8965 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8966 expr = ce.value;
8967 expr = c_fully_fold (expr, false, NULL);
8968 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8970 else
8971 expr = error_mark_node;
8972 stmt = c_parser_compound_statement (parser);
8973 objc_build_synchronized (loc, expr, stmt);
8976 /* Parse an objc-selector; return NULL_TREE without an error if the
8977 next token is not an objc-selector.
8979 objc-selector:
8980 identifier
8981 one of
8982 enum struct union if else while do for switch case default
8983 break continue return goto asm sizeof typeof __alignof
8984 unsigned long const short volatile signed restrict _Complex
8985 in out inout bycopy byref oneway int char float double void _Bool
8986 _Atomic
8988 ??? Why this selection of keywords but not, for example, storage
8989 class specifiers? */
8991 static tree
8992 c_parser_objc_selector (c_parser *parser)
8994 c_token *token = c_parser_peek_token (parser);
8995 tree value = token->value;
8996 if (token->type == CPP_NAME)
8998 c_parser_consume_token (parser);
8999 return value;
9001 if (token->type != CPP_KEYWORD)
9002 return NULL_TREE;
9003 switch (token->keyword)
9005 case RID_ENUM:
9006 case RID_STRUCT:
9007 case RID_UNION:
9008 case RID_IF:
9009 case RID_ELSE:
9010 case RID_WHILE:
9011 case RID_DO:
9012 case RID_FOR:
9013 case RID_SWITCH:
9014 case RID_CASE:
9015 case RID_DEFAULT:
9016 case RID_BREAK:
9017 case RID_CONTINUE:
9018 case RID_RETURN:
9019 case RID_GOTO:
9020 case RID_ASM:
9021 case RID_SIZEOF:
9022 case RID_TYPEOF:
9023 case RID_ALIGNOF:
9024 case RID_UNSIGNED:
9025 case RID_LONG:
9026 case RID_CONST:
9027 case RID_SHORT:
9028 case RID_VOLATILE:
9029 case RID_SIGNED:
9030 case RID_RESTRICT:
9031 case RID_COMPLEX:
9032 case RID_IN:
9033 case RID_OUT:
9034 case RID_INOUT:
9035 case RID_BYCOPY:
9036 case RID_BYREF:
9037 case RID_ONEWAY:
9038 case RID_INT:
9039 case RID_CHAR:
9040 case RID_FLOAT:
9041 case RID_DOUBLE:
9042 case RID_VOID:
9043 case RID_BOOL:
9044 case RID_ATOMIC:
9045 case RID_AUTO_TYPE:
9046 case RID_INT_N_0:
9047 case RID_INT_N_1:
9048 case RID_INT_N_2:
9049 case RID_INT_N_3:
9050 c_parser_consume_token (parser);
9051 return value;
9052 default:
9053 return NULL_TREE;
9057 /* Parse an objc-selector-arg.
9059 objc-selector-arg:
9060 objc-selector
9061 objc-keywordname-list
9063 objc-keywordname-list:
9064 objc-keywordname
9065 objc-keywordname-list objc-keywordname
9067 objc-keywordname:
9068 objc-selector :
9072 static tree
9073 c_parser_objc_selector_arg (c_parser *parser)
9075 tree sel = c_parser_objc_selector (parser);
9076 tree list = NULL_TREE;
9077 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9078 return sel;
9079 while (true)
9081 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9082 return list;
9083 list = chainon (list, build_tree_list (sel, NULL_TREE));
9084 sel = c_parser_objc_selector (parser);
9085 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9086 break;
9088 return list;
9091 /* Parse an objc-receiver.
9093 objc-receiver:
9094 expression
9095 class-name
9096 type-name
9099 static tree
9100 c_parser_objc_receiver (c_parser *parser)
9102 location_t loc = c_parser_peek_token (parser)->location;
9104 if (c_parser_peek_token (parser)->type == CPP_NAME
9105 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9106 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9108 tree id = c_parser_peek_token (parser)->value;
9109 c_parser_consume_token (parser);
9110 return objc_get_class_reference (id);
9112 struct c_expr ce = c_parser_expression (parser);
9113 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9114 return c_fully_fold (ce.value, false, NULL);
9117 /* Parse objc-message-args.
9119 objc-message-args:
9120 objc-selector
9121 objc-keywordarg-list
9123 objc-keywordarg-list:
9124 objc-keywordarg
9125 objc-keywordarg-list objc-keywordarg
9127 objc-keywordarg:
9128 objc-selector : objc-keywordexpr
9129 : objc-keywordexpr
9132 static tree
9133 c_parser_objc_message_args (c_parser *parser)
9135 tree sel = c_parser_objc_selector (parser);
9136 tree list = NULL_TREE;
9137 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9138 return sel;
9139 while (true)
9141 tree keywordexpr;
9142 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9143 return error_mark_node;
9144 keywordexpr = c_parser_objc_keywordexpr (parser);
9145 list = chainon (list, build_tree_list (sel, keywordexpr));
9146 sel = c_parser_objc_selector (parser);
9147 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9148 break;
9150 return list;
9153 /* Parse an objc-keywordexpr.
9155 objc-keywordexpr:
9156 nonempty-expr-list
9159 static tree
9160 c_parser_objc_keywordexpr (c_parser *parser)
9162 tree ret;
9163 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9164 NULL, NULL, NULL, NULL);
9165 if (vec_safe_length (expr_list) == 1)
9167 /* Just return the expression, remove a level of
9168 indirection. */
9169 ret = (*expr_list)[0];
9171 else
9173 /* We have a comma expression, we will collapse later. */
9174 ret = build_tree_list_vec (expr_list);
9176 release_tree_vector (expr_list);
9177 return ret;
9180 /* A check, needed in several places, that ObjC interface, implementation or
9181 method definitions are not prefixed by incorrect items. */
9182 static bool
9183 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9184 struct c_declspecs *specs)
9186 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9187 || specs->typespec_kind != ctsk_none)
9189 c_parser_error (parser,
9190 "no type or storage class may be specified here,");
9191 c_parser_skip_to_end_of_block_or_statement (parser);
9192 return true;
9194 return false;
9197 /* Parse an Objective-C @property declaration. The syntax is:
9199 objc-property-declaration:
9200 '@property' objc-property-attributes[opt] struct-declaration ;
9202 objc-property-attributes:
9203 '(' objc-property-attribute-list ')'
9205 objc-property-attribute-list:
9206 objc-property-attribute
9207 objc-property-attribute-list, objc-property-attribute
9209 objc-property-attribute
9210 'getter' = identifier
9211 'setter' = identifier
9212 'readonly'
9213 'readwrite'
9214 'assign'
9215 'retain'
9216 'copy'
9217 'nonatomic'
9219 For example:
9220 @property NSString *name;
9221 @property (readonly) id object;
9222 @property (retain, nonatomic, getter=getTheName) id name;
9223 @property int a, b, c;
9225 PS: This function is identical to cp_parser_objc_at_propery_declaration
9226 for C++. Keep them in sync. */
9227 static void
9228 c_parser_objc_at_property_declaration (c_parser *parser)
9230 /* The following variables hold the attributes of the properties as
9231 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9232 seen. When we see an attribute, we set them to 'true' (if they
9233 are boolean properties) or to the identifier (if they have an
9234 argument, ie, for getter and setter). Note that here we only
9235 parse the list of attributes, check the syntax and accumulate the
9236 attributes that we find. objc_add_property_declaration() will
9237 then process the information. */
9238 bool property_assign = false;
9239 bool property_copy = false;
9240 tree property_getter_ident = NULL_TREE;
9241 bool property_nonatomic = false;
9242 bool property_readonly = false;
9243 bool property_readwrite = false;
9244 bool property_retain = false;
9245 tree property_setter_ident = NULL_TREE;
9247 /* 'properties' is the list of properties that we read. Usually a
9248 single one, but maybe more (eg, in "@property int a, b, c;" there
9249 are three). */
9250 tree properties;
9251 location_t loc;
9253 loc = c_parser_peek_token (parser)->location;
9254 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9256 c_parser_consume_token (parser); /* Eat '@property'. */
9258 /* Parse the optional attribute list... */
9259 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9261 /* Eat the '(' */
9262 c_parser_consume_token (parser);
9264 /* Property attribute keywords are valid now. */
9265 parser->objc_property_attr_context = true;
9267 while (true)
9269 bool syntax_error = false;
9270 c_token *token = c_parser_peek_token (parser);
9271 enum rid keyword;
9273 if (token->type != CPP_KEYWORD)
9275 if (token->type == CPP_CLOSE_PAREN)
9276 c_parser_error (parser, "expected identifier");
9277 else
9279 c_parser_consume_token (parser);
9280 c_parser_error (parser, "unknown property attribute");
9282 break;
9284 keyword = token->keyword;
9285 c_parser_consume_token (parser);
9286 switch (keyword)
9288 case RID_ASSIGN: property_assign = true; break;
9289 case RID_COPY: property_copy = true; break;
9290 case RID_NONATOMIC: property_nonatomic = true; break;
9291 case RID_READONLY: property_readonly = true; break;
9292 case RID_READWRITE: property_readwrite = true; break;
9293 case RID_RETAIN: property_retain = true; break;
9295 case RID_GETTER:
9296 case RID_SETTER:
9297 if (c_parser_next_token_is_not (parser, CPP_EQ))
9299 if (keyword == RID_GETTER)
9300 c_parser_error (parser,
9301 "missing %<=%> (after %<getter%> attribute)");
9302 else
9303 c_parser_error (parser,
9304 "missing %<=%> (after %<setter%> attribute)");
9305 syntax_error = true;
9306 break;
9308 c_parser_consume_token (parser); /* eat the = */
9309 if (c_parser_next_token_is_not (parser, CPP_NAME))
9311 c_parser_error (parser, "expected identifier");
9312 syntax_error = true;
9313 break;
9315 if (keyword == RID_SETTER)
9317 if (property_setter_ident != NULL_TREE)
9318 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9319 else
9320 property_setter_ident = c_parser_peek_token (parser)->value;
9321 c_parser_consume_token (parser);
9322 if (c_parser_next_token_is_not (parser, CPP_COLON))
9323 c_parser_error (parser, "setter name must terminate with %<:%>");
9324 else
9325 c_parser_consume_token (parser);
9327 else
9329 if (property_getter_ident != NULL_TREE)
9330 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9331 else
9332 property_getter_ident = c_parser_peek_token (parser)->value;
9333 c_parser_consume_token (parser);
9335 break;
9336 default:
9337 c_parser_error (parser, "unknown property attribute");
9338 syntax_error = true;
9339 break;
9342 if (syntax_error)
9343 break;
9345 if (c_parser_next_token_is (parser, CPP_COMMA))
9346 c_parser_consume_token (parser);
9347 else
9348 break;
9350 parser->objc_property_attr_context = false;
9351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9353 /* ... and the property declaration(s). */
9354 properties = c_parser_struct_declaration (parser);
9356 if (properties == error_mark_node)
9358 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9359 parser->error = false;
9360 return;
9363 if (properties == NULL_TREE)
9364 c_parser_error (parser, "expected identifier");
9365 else
9367 /* Comma-separated properties are chained together in
9368 reverse order; add them one by one. */
9369 properties = nreverse (properties);
9371 for (; properties; properties = TREE_CHAIN (properties))
9372 objc_add_property_declaration (loc, copy_node (properties),
9373 property_readonly, property_readwrite,
9374 property_assign, property_retain,
9375 property_copy, property_nonatomic,
9376 property_getter_ident, property_setter_ident);
9379 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9380 parser->error = false;
9383 /* Parse an Objective-C @synthesize declaration. The syntax is:
9385 objc-synthesize-declaration:
9386 @synthesize objc-synthesize-identifier-list ;
9388 objc-synthesize-identifier-list:
9389 objc-synthesize-identifier
9390 objc-synthesize-identifier-list, objc-synthesize-identifier
9392 objc-synthesize-identifier
9393 identifier
9394 identifier = identifier
9396 For example:
9397 @synthesize MyProperty;
9398 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9400 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9401 for C++. Keep them in sync.
9403 static void
9404 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9406 tree list = NULL_TREE;
9407 location_t loc;
9408 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9409 loc = c_parser_peek_token (parser)->location;
9411 c_parser_consume_token (parser);
9412 while (true)
9414 tree property, ivar;
9415 if (c_parser_next_token_is_not (parser, CPP_NAME))
9417 c_parser_error (parser, "expected identifier");
9418 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9419 /* Once we find the semicolon, we can resume normal parsing.
9420 We have to reset parser->error manually because
9421 c_parser_skip_until_found() won't reset it for us if the
9422 next token is precisely a semicolon. */
9423 parser->error = false;
9424 return;
9426 property = c_parser_peek_token (parser)->value;
9427 c_parser_consume_token (parser);
9428 if (c_parser_next_token_is (parser, CPP_EQ))
9430 c_parser_consume_token (parser);
9431 if (c_parser_next_token_is_not (parser, CPP_NAME))
9433 c_parser_error (parser, "expected identifier");
9434 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9435 parser->error = false;
9436 return;
9438 ivar = c_parser_peek_token (parser)->value;
9439 c_parser_consume_token (parser);
9441 else
9442 ivar = NULL_TREE;
9443 list = chainon (list, build_tree_list (ivar, property));
9444 if (c_parser_next_token_is (parser, CPP_COMMA))
9445 c_parser_consume_token (parser);
9446 else
9447 break;
9449 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9450 objc_add_synthesize_declaration (loc, list);
9453 /* Parse an Objective-C @dynamic declaration. The syntax is:
9455 objc-dynamic-declaration:
9456 @dynamic identifier-list ;
9458 For example:
9459 @dynamic MyProperty;
9460 @dynamic MyProperty, AnotherProperty;
9462 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9463 for C++. Keep them in sync.
9465 static void
9466 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9468 tree list = NULL_TREE;
9469 location_t loc;
9470 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9471 loc = c_parser_peek_token (parser)->location;
9473 c_parser_consume_token (parser);
9474 while (true)
9476 tree property;
9477 if (c_parser_next_token_is_not (parser, CPP_NAME))
9479 c_parser_error (parser, "expected identifier");
9480 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9481 parser->error = false;
9482 return;
9484 property = c_parser_peek_token (parser)->value;
9485 list = chainon (list, build_tree_list (NULL_TREE, property));
9486 c_parser_consume_token (parser);
9487 if (c_parser_next_token_is (parser, CPP_COMMA))
9488 c_parser_consume_token (parser);
9489 else
9490 break;
9492 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9493 objc_add_dynamic_declaration (loc, list);
9497 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9498 should be considered, statements. ALLOW_STMT is true if we're within
9499 the context of a function and such pragmas are to be allowed. Returns
9500 true if we actually parsed such a pragma. */
9502 static bool
9503 c_parser_pragma (c_parser *parser, enum pragma_context context)
9505 unsigned int id;
9507 id = c_parser_peek_token (parser)->pragma_kind;
9508 gcc_assert (id != PRAGMA_NONE);
9510 switch (id)
9512 case PRAGMA_OMP_BARRIER:
9513 if (context != pragma_compound)
9515 if (context == pragma_stmt)
9516 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9517 "used in compound statements");
9518 goto bad_stmt;
9520 c_parser_omp_barrier (parser);
9521 return false;
9523 case PRAGMA_OMP_FLUSH:
9524 if (context != pragma_compound)
9526 if (context == pragma_stmt)
9527 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9528 "used in compound statements");
9529 goto bad_stmt;
9531 c_parser_omp_flush (parser);
9532 return false;
9534 case PRAGMA_OMP_TASKWAIT:
9535 if (context != pragma_compound)
9537 if (context == pragma_stmt)
9538 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9539 "used in compound statements");
9540 goto bad_stmt;
9542 c_parser_omp_taskwait (parser);
9543 return false;
9545 case PRAGMA_OMP_TASKYIELD:
9546 if (context != pragma_compound)
9548 if (context == pragma_stmt)
9549 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9550 "used in compound statements");
9551 goto bad_stmt;
9553 c_parser_omp_taskyield (parser);
9554 return false;
9556 case PRAGMA_OMP_CANCEL:
9557 if (context != pragma_compound)
9559 if (context == pragma_stmt)
9560 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9561 "used in compound statements");
9562 goto bad_stmt;
9564 c_parser_omp_cancel (parser);
9565 return false;
9567 case PRAGMA_OMP_CANCELLATION_POINT:
9568 if (context != pragma_compound)
9570 if (context == pragma_stmt)
9571 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9572 "only be used in compound statements");
9573 goto bad_stmt;
9575 c_parser_omp_cancellation_point (parser);
9576 return false;
9578 case PRAGMA_OMP_THREADPRIVATE:
9579 c_parser_omp_threadprivate (parser);
9580 return false;
9582 case PRAGMA_OMP_TARGET:
9583 return c_parser_omp_target (parser, context);
9585 case PRAGMA_OMP_END_DECLARE_TARGET:
9586 c_parser_omp_end_declare_target (parser);
9587 return false;
9589 case PRAGMA_OMP_SECTION:
9590 error_at (c_parser_peek_token (parser)->location,
9591 "%<#pragma omp section%> may only be used in "
9592 "%<#pragma omp sections%> construct");
9593 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9594 return false;
9596 case PRAGMA_OMP_DECLARE_REDUCTION:
9597 c_parser_omp_declare (parser, context);
9598 return false;
9599 case PRAGMA_IVDEP:
9600 c_parser_consume_pragma (parser);
9601 c_parser_skip_to_pragma_eol (parser);
9602 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9603 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9604 && !c_parser_next_token_is_keyword (parser, RID_DO))
9606 c_parser_error (parser, "for, while or do statement expected");
9607 return false;
9609 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9610 c_parser_for_statement (parser, true);
9611 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9612 c_parser_while_statement (parser, true);
9613 else
9614 c_parser_do_statement (parser, true);
9615 return false;
9617 case PRAGMA_GCC_PCH_PREPROCESS:
9618 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9619 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9620 return false;
9622 case PRAGMA_CILK_SIMD:
9623 if (!c_parser_cilk_verify_simd (parser, context))
9624 return false;
9625 c_parser_consume_pragma (parser);
9626 c_parser_cilk_simd (parser);
9627 return false;
9628 case PRAGMA_CILK_GRAINSIZE:
9629 if (!flag_cilkplus)
9631 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9632 " enabled");
9633 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9634 return false;
9636 if (context == pragma_external)
9638 error_at (c_parser_peek_token (parser)->location,
9639 "%<#pragma grainsize%> must be inside a function");
9640 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9641 return false;
9643 c_parser_cilk_grainsize (parser);
9644 return false;
9646 default:
9647 if (id < PRAGMA_FIRST_EXTERNAL)
9649 if (context != pragma_stmt && context != pragma_compound)
9651 bad_stmt:
9652 c_parser_error (parser, "expected declaration specifiers");
9653 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9654 return false;
9656 c_parser_omp_construct (parser);
9657 return true;
9659 break;
9662 c_parser_consume_pragma (parser);
9663 c_invoke_pragma_handler (id);
9665 /* Skip to EOL, but suppress any error message. Those will have been
9666 generated by the handler routine through calling error, as opposed
9667 to calling c_parser_error. */
9668 parser->error = true;
9669 c_parser_skip_to_pragma_eol (parser);
9671 return false;
9674 /* The interface the pragma parsers have to the lexer. */
9676 enum cpp_ttype
9677 pragma_lex (tree *value)
9679 c_token *tok = c_parser_peek_token (the_parser);
9680 enum cpp_ttype ret = tok->type;
9682 *value = tok->value;
9683 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9684 ret = CPP_EOF;
9685 else
9687 if (ret == CPP_KEYWORD)
9688 ret = CPP_NAME;
9689 c_parser_consume_token (the_parser);
9692 return ret;
9695 static void
9696 c_parser_pragma_pch_preprocess (c_parser *parser)
9698 tree name = NULL;
9700 c_parser_consume_pragma (parser);
9701 if (c_parser_next_token_is (parser, CPP_STRING))
9703 name = c_parser_peek_token (parser)->value;
9704 c_parser_consume_token (parser);
9706 else
9707 c_parser_error (parser, "expected string literal");
9708 c_parser_skip_to_pragma_eol (parser);
9710 if (name)
9711 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9714 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9716 /* Returns name of the next clause.
9717 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9718 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9719 returned and the token is consumed. */
9721 static pragma_omp_clause
9722 c_parser_omp_clause_name (c_parser *parser)
9724 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9726 if (c_parser_next_token_is_keyword (parser, RID_IF))
9727 result = PRAGMA_OMP_CLAUSE_IF;
9728 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9729 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9730 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9731 result = PRAGMA_OMP_CLAUSE_FOR;
9732 else if (c_parser_next_token_is (parser, CPP_NAME))
9734 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9736 switch (p[0])
9738 case 'a':
9739 if (!strcmp ("aligned", p))
9740 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9741 break;
9742 case 'c':
9743 if (!strcmp ("collapse", p))
9744 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9745 else if (!strcmp ("copyin", p))
9746 result = PRAGMA_OMP_CLAUSE_COPYIN;
9747 else if (!strcmp ("copyprivate", p))
9748 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9749 break;
9750 case 'd':
9751 if (!strcmp ("depend", p))
9752 result = PRAGMA_OMP_CLAUSE_DEPEND;
9753 else if (!strcmp ("device", p))
9754 result = PRAGMA_OMP_CLAUSE_DEVICE;
9755 else if (!strcmp ("dist_schedule", p))
9756 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9757 break;
9758 case 'f':
9759 if (!strcmp ("final", p))
9760 result = PRAGMA_OMP_CLAUSE_FINAL;
9761 else if (!strcmp ("firstprivate", p))
9762 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9763 else if (!strcmp ("from", p))
9764 result = PRAGMA_OMP_CLAUSE_FROM;
9765 break;
9766 case 'i':
9767 if (!strcmp ("inbranch", p))
9768 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9769 break;
9770 case 'l':
9771 if (!strcmp ("lastprivate", p))
9772 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9773 else if (!strcmp ("linear", p))
9774 result = PRAGMA_OMP_CLAUSE_LINEAR;
9775 break;
9776 case 'm':
9777 if (!strcmp ("map", p))
9778 result = PRAGMA_OMP_CLAUSE_MAP;
9779 else if (!strcmp ("mergeable", p))
9780 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9781 else if (flag_cilkplus && !strcmp ("mask", p))
9782 result = PRAGMA_CILK_CLAUSE_MASK;
9783 break;
9784 case 'n':
9785 if (!strcmp ("notinbranch", p))
9786 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9787 else if (!strcmp ("nowait", p))
9788 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9789 else if (!strcmp ("num_teams", p))
9790 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9791 else if (!strcmp ("num_threads", p))
9792 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9793 else if (flag_cilkplus && !strcmp ("nomask", p))
9794 result = PRAGMA_CILK_CLAUSE_NOMASK;
9795 break;
9796 case 'o':
9797 if (!strcmp ("ordered", p))
9798 result = PRAGMA_OMP_CLAUSE_ORDERED;
9799 break;
9800 case 'p':
9801 if (!strcmp ("parallel", p))
9802 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9803 else if (!strcmp ("private", p))
9804 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9805 else if (!strcmp ("proc_bind", p))
9806 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9807 break;
9808 case 'r':
9809 if (!strcmp ("reduction", p))
9810 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9811 break;
9812 case 's':
9813 if (!strcmp ("safelen", p))
9814 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9815 else if (!strcmp ("schedule", p))
9816 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9817 else if (!strcmp ("sections", p))
9818 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9819 else if (!strcmp ("shared", p))
9820 result = PRAGMA_OMP_CLAUSE_SHARED;
9821 else if (!strcmp ("simdlen", p))
9822 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9823 break;
9824 case 't':
9825 if (!strcmp ("taskgroup", p))
9826 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9827 else if (!strcmp ("thread_limit", p))
9828 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9829 else if (!strcmp ("to", p))
9830 result = PRAGMA_OMP_CLAUSE_TO;
9831 break;
9832 case 'u':
9833 if (!strcmp ("uniform", p))
9834 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9835 else if (!strcmp ("untied", p))
9836 result = PRAGMA_OMP_CLAUSE_UNTIED;
9837 break;
9838 case 'v':
9839 if (flag_cilkplus && !strcmp ("vectorlength", p))
9840 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9841 break;
9845 if (result != PRAGMA_OMP_CLAUSE_NONE)
9846 c_parser_consume_token (parser);
9848 return result;
9851 /* Validate that a clause of the given type does not already exist. */
9853 static void
9854 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9855 const char *name)
9857 tree c;
9859 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9860 if (OMP_CLAUSE_CODE (c) == code)
9862 location_t loc = OMP_CLAUSE_LOCATION (c);
9863 error_at (loc, "too many %qs clauses", name);
9864 break;
9868 /* OpenMP 2.5:
9869 variable-list:
9870 identifier
9871 variable-list , identifier
9873 If KIND is nonzero, create the appropriate node and install the
9874 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9875 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9877 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9878 return the list created. */
9880 static tree
9881 c_parser_omp_variable_list (c_parser *parser,
9882 location_t clause_loc,
9883 enum omp_clause_code kind, tree list)
9885 if (c_parser_next_token_is_not (parser, CPP_NAME)
9886 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9887 c_parser_error (parser, "expected identifier");
9889 while (c_parser_next_token_is (parser, CPP_NAME)
9890 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9892 tree t = lookup_name (c_parser_peek_token (parser)->value);
9894 if (t == NULL_TREE)
9896 undeclared_variable (c_parser_peek_token (parser)->location,
9897 c_parser_peek_token (parser)->value);
9898 t = error_mark_node;
9901 c_parser_consume_token (parser);
9903 if (t == error_mark_node)
9905 else if (kind != 0)
9907 switch (kind)
9909 case OMP_CLAUSE_MAP:
9910 case OMP_CLAUSE_FROM:
9911 case OMP_CLAUSE_TO:
9912 case OMP_CLAUSE_DEPEND:
9913 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9915 tree low_bound = NULL_TREE, length = NULL_TREE;
9917 c_parser_consume_token (parser);
9918 if (!c_parser_next_token_is (parser, CPP_COLON))
9920 low_bound = c_parser_expression (parser).value;
9921 mark_exp_read (low_bound);
9923 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9924 length = integer_one_node;
9925 else
9927 /* Look for `:'. */
9928 if (!c_parser_require (parser, CPP_COLON,
9929 "expected %<:%>"))
9931 t = error_mark_node;
9932 break;
9934 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9936 length = c_parser_expression (parser).value;
9937 mark_exp_read (length);
9940 /* Look for the closing `]'. */
9941 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9942 "expected %<]%>"))
9944 t = error_mark_node;
9945 break;
9947 t = tree_cons (low_bound, length, t);
9949 break;
9950 default:
9951 break;
9954 if (t != error_mark_node)
9956 tree u = build_omp_clause (clause_loc, kind);
9957 OMP_CLAUSE_DECL (u) = t;
9958 OMP_CLAUSE_CHAIN (u) = list;
9959 list = u;
9962 else
9963 list = tree_cons (t, NULL_TREE, list);
9965 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9966 break;
9968 c_parser_consume_token (parser);
9971 return list;
9974 /* Similarly, but expect leading and trailing parenthesis. This is a very
9975 common case for omp clauses. */
9977 static tree
9978 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9979 tree list)
9981 /* The clauses location. */
9982 location_t loc = c_parser_peek_token (parser)->location;
9984 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9986 list = c_parser_omp_variable_list (parser, loc, kind, list);
9987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9989 return list;
9992 /* OpenMP 3.0:
9993 collapse ( constant-expression ) */
9995 static tree
9996 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9998 tree c, num = error_mark_node;
9999 HOST_WIDE_INT n;
10000 location_t loc;
10002 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10004 loc = c_parser_peek_token (parser)->location;
10005 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10007 num = c_parser_expr_no_commas (parser, NULL).value;
10008 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10010 if (num == error_mark_node)
10011 return list;
10012 mark_exp_read (num);
10013 num = c_fully_fold (num, false, NULL);
10014 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10015 || !tree_fits_shwi_p (num)
10016 || (n = tree_to_shwi (num)) <= 0
10017 || (int) n != n)
10019 error_at (loc,
10020 "collapse argument needs positive constant integer expression");
10021 return list;
10023 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10024 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10025 OMP_CLAUSE_CHAIN (c) = list;
10026 return c;
10029 /* OpenMP 2.5:
10030 copyin ( variable-list ) */
10032 static tree
10033 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10035 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10038 /* OpenMP 2.5:
10039 copyprivate ( variable-list ) */
10041 static tree
10042 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10044 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10047 /* OpenMP 2.5:
10048 default ( shared | none ) */
10050 static tree
10051 c_parser_omp_clause_default (c_parser *parser, tree list)
10053 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10054 location_t loc = c_parser_peek_token (parser)->location;
10055 tree c;
10057 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10058 return list;
10059 if (c_parser_next_token_is (parser, CPP_NAME))
10061 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10063 switch (p[0])
10065 case 'n':
10066 if (strcmp ("none", p) != 0)
10067 goto invalid_kind;
10068 kind = OMP_CLAUSE_DEFAULT_NONE;
10069 break;
10071 case 's':
10072 if (strcmp ("shared", p) != 0)
10073 goto invalid_kind;
10074 kind = OMP_CLAUSE_DEFAULT_SHARED;
10075 break;
10077 default:
10078 goto invalid_kind;
10081 c_parser_consume_token (parser);
10083 else
10085 invalid_kind:
10086 c_parser_error (parser, "expected %<none%> or %<shared%>");
10088 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10090 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10091 return list;
10093 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10094 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10095 OMP_CLAUSE_CHAIN (c) = list;
10096 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10098 return c;
10101 /* OpenMP 2.5:
10102 firstprivate ( variable-list ) */
10104 static tree
10105 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10107 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10110 /* OpenMP 3.1:
10111 final ( expression ) */
10113 static tree
10114 c_parser_omp_clause_final (c_parser *parser, tree list)
10116 location_t loc = c_parser_peek_token (parser)->location;
10117 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10119 tree t = c_parser_paren_condition (parser);
10120 tree c;
10122 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10124 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10125 OMP_CLAUSE_FINAL_EXPR (c) = t;
10126 OMP_CLAUSE_CHAIN (c) = list;
10127 list = c;
10129 else
10130 c_parser_error (parser, "expected %<(%>");
10132 return list;
10135 /* OpenMP 2.5:
10136 if ( expression ) */
10138 static tree
10139 c_parser_omp_clause_if (c_parser *parser, tree list)
10141 location_t loc = c_parser_peek_token (parser)->location;
10142 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10144 tree t = c_parser_paren_condition (parser);
10145 tree c;
10147 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10149 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10150 OMP_CLAUSE_IF_EXPR (c) = t;
10151 OMP_CLAUSE_CHAIN (c) = list;
10152 list = c;
10154 else
10155 c_parser_error (parser, "expected %<(%>");
10157 return list;
10160 /* OpenMP 2.5:
10161 lastprivate ( variable-list ) */
10163 static tree
10164 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10166 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10169 /* OpenMP 3.1:
10170 mergeable */
10172 static tree
10173 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10175 tree c;
10177 /* FIXME: Should we allow duplicates? */
10178 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10180 c = build_omp_clause (c_parser_peek_token (parser)->location,
10181 OMP_CLAUSE_MERGEABLE);
10182 OMP_CLAUSE_CHAIN (c) = list;
10184 return c;
10187 /* OpenMP 2.5:
10188 nowait */
10190 static tree
10191 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10193 tree c;
10194 location_t loc = c_parser_peek_token (parser)->location;
10196 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10198 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10199 OMP_CLAUSE_CHAIN (c) = list;
10200 return c;
10203 /* OpenMP 2.5:
10204 num_threads ( expression ) */
10206 static tree
10207 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10209 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10210 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10212 location_t expr_loc = c_parser_peek_token (parser)->location;
10213 tree c, t = c_parser_expression (parser).value;
10214 mark_exp_read (t);
10215 t = c_fully_fold (t, false, NULL);
10217 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10219 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10221 c_parser_error (parser, "expected integer expression");
10222 return list;
10225 /* Attempt to statically determine when the number isn't positive. */
10226 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10227 build_int_cst (TREE_TYPE (t), 0));
10228 if (CAN_HAVE_LOCATION_P (c))
10229 SET_EXPR_LOCATION (c, expr_loc);
10230 if (c == boolean_true_node)
10232 warning_at (expr_loc, 0,
10233 "%<num_threads%> value must be positive");
10234 t = integer_one_node;
10237 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10239 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10240 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10241 OMP_CLAUSE_CHAIN (c) = list;
10242 list = c;
10245 return list;
10248 /* OpenMP 2.5:
10249 ordered */
10251 static tree
10252 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10254 tree c;
10256 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10258 c = build_omp_clause (c_parser_peek_token (parser)->location,
10259 OMP_CLAUSE_ORDERED);
10260 OMP_CLAUSE_CHAIN (c) = list;
10262 return c;
10265 /* OpenMP 2.5:
10266 private ( variable-list ) */
10268 static tree
10269 c_parser_omp_clause_private (c_parser *parser, tree list)
10271 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10274 /* OpenMP 2.5:
10275 reduction ( reduction-operator : variable-list )
10277 reduction-operator:
10278 One of: + * - & ^ | && ||
10280 OpenMP 3.1:
10282 reduction-operator:
10283 One of: + * - & ^ | && || max min
10285 OpenMP 4.0:
10287 reduction-operator:
10288 One of: + * - & ^ | && ||
10289 identifier */
10291 static tree
10292 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10294 location_t clause_loc = c_parser_peek_token (parser)->location;
10295 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10297 enum tree_code code = ERROR_MARK;
10298 tree reduc_id = NULL_TREE;
10300 switch (c_parser_peek_token (parser)->type)
10302 case CPP_PLUS:
10303 code = PLUS_EXPR;
10304 break;
10305 case CPP_MULT:
10306 code = MULT_EXPR;
10307 break;
10308 case CPP_MINUS:
10309 code = MINUS_EXPR;
10310 break;
10311 case CPP_AND:
10312 code = BIT_AND_EXPR;
10313 break;
10314 case CPP_XOR:
10315 code = BIT_XOR_EXPR;
10316 break;
10317 case CPP_OR:
10318 code = BIT_IOR_EXPR;
10319 break;
10320 case CPP_AND_AND:
10321 code = TRUTH_ANDIF_EXPR;
10322 break;
10323 case CPP_OR_OR:
10324 code = TRUTH_ORIF_EXPR;
10325 break;
10326 case CPP_NAME:
10328 const char *p
10329 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10330 if (strcmp (p, "min") == 0)
10332 code = MIN_EXPR;
10333 break;
10335 if (strcmp (p, "max") == 0)
10337 code = MAX_EXPR;
10338 break;
10340 reduc_id = c_parser_peek_token (parser)->value;
10341 break;
10343 default:
10344 c_parser_error (parser,
10345 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10346 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10348 return list;
10350 c_parser_consume_token (parser);
10351 reduc_id = c_omp_reduction_id (code, reduc_id);
10352 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10354 tree nl, c;
10356 nl = c_parser_omp_variable_list (parser, clause_loc,
10357 OMP_CLAUSE_REDUCTION, list);
10358 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10360 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10361 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10362 if (code == ERROR_MARK
10363 || !(INTEGRAL_TYPE_P (type)
10364 || TREE_CODE (type) == REAL_TYPE
10365 || TREE_CODE (type) == COMPLEX_TYPE))
10366 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10367 = c_omp_reduction_lookup (reduc_id,
10368 TYPE_MAIN_VARIANT (type));
10371 list = nl;
10373 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10375 return list;
10378 /* OpenMP 2.5:
10379 schedule ( schedule-kind )
10380 schedule ( schedule-kind , expression )
10382 schedule-kind:
10383 static | dynamic | guided | runtime | auto
10386 static tree
10387 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10389 tree c, t;
10390 location_t loc = c_parser_peek_token (parser)->location;
10392 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10393 return list;
10395 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10397 if (c_parser_next_token_is (parser, CPP_NAME))
10399 tree kind = c_parser_peek_token (parser)->value;
10400 const char *p = IDENTIFIER_POINTER (kind);
10402 switch (p[0])
10404 case 'd':
10405 if (strcmp ("dynamic", p) != 0)
10406 goto invalid_kind;
10407 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10408 break;
10410 case 'g':
10411 if (strcmp ("guided", p) != 0)
10412 goto invalid_kind;
10413 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10414 break;
10416 case 'r':
10417 if (strcmp ("runtime", p) != 0)
10418 goto invalid_kind;
10419 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10420 break;
10422 default:
10423 goto invalid_kind;
10426 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10427 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10428 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10429 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10430 else
10431 goto invalid_kind;
10433 c_parser_consume_token (parser);
10434 if (c_parser_next_token_is (parser, CPP_COMMA))
10436 location_t here;
10437 c_parser_consume_token (parser);
10439 here = c_parser_peek_token (parser)->location;
10440 t = c_parser_expr_no_commas (parser, NULL).value;
10441 mark_exp_read (t);
10442 t = c_fully_fold (t, false, NULL);
10444 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10445 error_at (here, "schedule %<runtime%> does not take "
10446 "a %<chunk_size%> parameter");
10447 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10448 error_at (here,
10449 "schedule %<auto%> does not take "
10450 "a %<chunk_size%> parameter");
10451 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10452 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10453 else
10454 c_parser_error (parser, "expected integer expression");
10456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10458 else
10459 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10460 "expected %<,%> or %<)%>");
10462 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10463 OMP_CLAUSE_CHAIN (c) = list;
10464 return c;
10466 invalid_kind:
10467 c_parser_error (parser, "invalid schedule kind");
10468 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10469 return list;
10472 /* OpenMP 2.5:
10473 shared ( variable-list ) */
10475 static tree
10476 c_parser_omp_clause_shared (c_parser *parser, tree list)
10478 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10481 /* OpenMP 3.0:
10482 untied */
10484 static tree
10485 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10487 tree c;
10489 /* FIXME: Should we allow duplicates? */
10490 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10492 c = build_omp_clause (c_parser_peek_token (parser)->location,
10493 OMP_CLAUSE_UNTIED);
10494 OMP_CLAUSE_CHAIN (c) = list;
10496 return c;
10499 /* OpenMP 4.0:
10500 inbranch
10501 notinbranch */
10503 static tree
10504 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10505 enum omp_clause_code code, tree list)
10507 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10509 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10510 OMP_CLAUSE_CHAIN (c) = list;
10512 return c;
10515 /* OpenMP 4.0:
10516 parallel
10518 sections
10519 taskgroup */
10521 static tree
10522 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10523 enum omp_clause_code code, tree list)
10525 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10526 OMP_CLAUSE_CHAIN (c) = list;
10528 return c;
10531 /* OpenMP 4.0:
10532 num_teams ( expression ) */
10534 static tree
10535 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10537 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10538 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10540 location_t expr_loc = c_parser_peek_token (parser)->location;
10541 tree c, t = c_parser_expression (parser).value;
10542 mark_exp_read (t);
10543 t = c_fully_fold (t, false, NULL);
10545 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10547 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10549 c_parser_error (parser, "expected integer expression");
10550 return list;
10553 /* Attempt to statically determine when the number isn't positive. */
10554 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10555 build_int_cst (TREE_TYPE (t), 0));
10556 if (CAN_HAVE_LOCATION_P (c))
10557 SET_EXPR_LOCATION (c, expr_loc);
10558 if (c == boolean_true_node)
10560 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10561 t = integer_one_node;
10564 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10566 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10567 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10568 OMP_CLAUSE_CHAIN (c) = list;
10569 list = c;
10572 return list;
10575 /* OpenMP 4.0:
10576 thread_limit ( expression ) */
10578 static tree
10579 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10581 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
10582 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10584 location_t expr_loc = c_parser_peek_token (parser)->location;
10585 tree c, t = c_parser_expression (parser).value;
10586 mark_exp_read (t);
10587 t = c_fully_fold (t, false, NULL);
10589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10591 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10593 c_parser_error (parser, "expected integer expression");
10594 return list;
10597 /* Attempt to statically determine when the number isn't positive. */
10598 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10599 build_int_cst (TREE_TYPE (t), 0));
10600 if (CAN_HAVE_LOCATION_P (c))
10601 SET_EXPR_LOCATION (c, expr_loc);
10602 if (c == boolean_true_node)
10604 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10605 t = integer_one_node;
10608 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10609 "thread_limit");
10611 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
10612 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10613 OMP_CLAUSE_CHAIN (c) = list;
10614 list = c;
10617 return list;
10620 /* OpenMP 4.0:
10621 aligned ( variable-list )
10622 aligned ( variable-list : constant-expression ) */
10624 static tree
10625 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10627 location_t clause_loc = c_parser_peek_token (parser)->location;
10628 tree nl, c;
10630 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10631 return list;
10633 nl = c_parser_omp_variable_list (parser, clause_loc,
10634 OMP_CLAUSE_ALIGNED, list);
10636 if (c_parser_next_token_is (parser, CPP_COLON))
10638 c_parser_consume_token (parser);
10639 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10640 mark_exp_read (alignment);
10641 alignment = c_fully_fold (alignment, false, NULL);
10642 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10643 && TREE_CODE (alignment) != INTEGER_CST
10644 && tree_int_cst_sgn (alignment) != 1)
10646 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10647 "be positive constant integer expression");
10648 alignment = NULL_TREE;
10651 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10652 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10655 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10656 return nl;
10659 /* OpenMP 4.0:
10660 linear ( variable-list )
10661 linear ( variable-list : expression ) */
10663 static tree
10664 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
10666 location_t clause_loc = c_parser_peek_token (parser)->location;
10667 tree nl, c, step;
10669 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10670 return list;
10672 nl = c_parser_omp_variable_list (parser, clause_loc,
10673 OMP_CLAUSE_LINEAR, list);
10675 if (c_parser_next_token_is (parser, CPP_COLON))
10677 c_parser_consume_token (parser);
10678 step = c_parser_expression (parser).value;
10679 mark_exp_read (step);
10680 step = c_fully_fold (step, false, NULL);
10681 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10683 sorry ("using parameters for %<linear%> step is not supported yet");
10684 step = integer_one_node;
10686 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10688 error_at (clause_loc, "%<linear%> clause step expression must "
10689 "be integral");
10690 step = integer_one_node;
10694 else
10695 step = integer_one_node;
10697 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10699 OMP_CLAUSE_LINEAR_STEP (c) = step;
10702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10703 return nl;
10706 /* OpenMP 4.0:
10707 safelen ( constant-expression ) */
10709 static tree
10710 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10712 location_t clause_loc = c_parser_peek_token (parser)->location;
10713 tree c, t;
10715 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10716 return list;
10718 t = c_parser_expr_no_commas (parser, NULL).value;
10719 mark_exp_read (t);
10720 t = c_fully_fold (t, false, NULL);
10721 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10722 && TREE_CODE (t) != INTEGER_CST
10723 && tree_int_cst_sgn (t) != 1)
10725 error_at (clause_loc, "%<safelen%> clause expression must "
10726 "be positive constant integer expression");
10727 t = NULL_TREE;
10730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10731 if (t == NULL_TREE || t == error_mark_node)
10732 return list;
10734 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10736 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10737 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10738 OMP_CLAUSE_CHAIN (c) = list;
10739 return c;
10742 /* OpenMP 4.0:
10743 simdlen ( constant-expression ) */
10745 static tree
10746 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10748 location_t clause_loc = c_parser_peek_token (parser)->location;
10749 tree c, t;
10751 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10752 return list;
10754 t = c_parser_expr_no_commas (parser, NULL).value;
10755 mark_exp_read (t);
10756 t = c_fully_fold (t, false, NULL);
10757 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10758 && TREE_CODE (t) != INTEGER_CST
10759 && tree_int_cst_sgn (t) != 1)
10761 error_at (clause_loc, "%<simdlen%> clause expression must "
10762 "be positive constant integer expression");
10763 t = NULL_TREE;
10766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10767 if (t == NULL_TREE || t == error_mark_node)
10768 return list;
10770 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10772 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10773 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10774 OMP_CLAUSE_CHAIN (c) = list;
10775 return c;
10778 /* OpenMP 4.0:
10779 depend ( depend-kind: variable-list )
10781 depend-kind:
10782 in | out | inout */
10784 static tree
10785 c_parser_omp_clause_depend (c_parser *parser, tree list)
10787 location_t clause_loc = c_parser_peek_token (parser)->location;
10788 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10789 tree nl, c;
10791 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10792 return list;
10794 if (c_parser_next_token_is (parser, CPP_NAME))
10796 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10797 if (strcmp ("in", p) == 0)
10798 kind = OMP_CLAUSE_DEPEND_IN;
10799 else if (strcmp ("inout", p) == 0)
10800 kind = OMP_CLAUSE_DEPEND_INOUT;
10801 else if (strcmp ("out", p) == 0)
10802 kind = OMP_CLAUSE_DEPEND_OUT;
10803 else
10804 goto invalid_kind;
10806 else
10807 goto invalid_kind;
10809 c_parser_consume_token (parser);
10810 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10811 goto resync_fail;
10813 nl = c_parser_omp_variable_list (parser, clause_loc,
10814 OMP_CLAUSE_DEPEND, list);
10816 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10817 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10820 return nl;
10822 invalid_kind:
10823 c_parser_error (parser, "invalid depend kind");
10824 resync_fail:
10825 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10826 return list;
10829 /* OpenMP 4.0:
10830 map ( map-kind: variable-list )
10831 map ( variable-list )
10833 map-kind:
10834 alloc | to | from | tofrom */
10836 static tree
10837 c_parser_omp_clause_map (c_parser *parser, tree list)
10839 location_t clause_loc = c_parser_peek_token (parser)->location;
10840 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10841 tree nl, c;
10843 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10844 return list;
10846 if (c_parser_next_token_is (parser, CPP_NAME)
10847 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10849 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10850 if (strcmp ("alloc", p) == 0)
10851 kind = OMP_CLAUSE_MAP_ALLOC;
10852 else if (strcmp ("to", p) == 0)
10853 kind = OMP_CLAUSE_MAP_TO;
10854 else if (strcmp ("from", p) == 0)
10855 kind = OMP_CLAUSE_MAP_FROM;
10856 else if (strcmp ("tofrom", p) == 0)
10857 kind = OMP_CLAUSE_MAP_TOFROM;
10858 else
10860 c_parser_error (parser, "invalid map kind");
10861 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10862 "expected %<)%>");
10863 return list;
10865 c_parser_consume_token (parser);
10866 c_parser_consume_token (parser);
10869 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10871 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10872 OMP_CLAUSE_MAP_KIND (c) = kind;
10874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10875 return nl;
10878 /* OpenMP 4.0:
10879 device ( expression ) */
10881 static tree
10882 c_parser_omp_clause_device (c_parser *parser, tree list)
10884 location_t clause_loc = c_parser_peek_token (parser)->location;
10885 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10887 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10888 mark_exp_read (t);
10889 t = c_fully_fold (t, false, NULL);
10891 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10893 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10895 c_parser_error (parser, "expected integer expression");
10896 return list;
10899 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10901 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10902 OMP_CLAUSE_DEVICE_ID (c) = t;
10903 OMP_CLAUSE_CHAIN (c) = list;
10904 list = c;
10907 return list;
10910 /* OpenMP 4.0:
10911 dist_schedule ( static )
10912 dist_schedule ( static , expression ) */
10914 static tree
10915 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10917 tree c, t = NULL_TREE;
10918 location_t loc = c_parser_peek_token (parser)->location;
10920 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10921 return list;
10923 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10925 c_parser_error (parser, "invalid dist_schedule kind");
10926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10927 "expected %<)%>");
10928 return list;
10931 c_parser_consume_token (parser);
10932 if (c_parser_next_token_is (parser, CPP_COMMA))
10934 c_parser_consume_token (parser);
10936 t = c_parser_expr_no_commas (parser, NULL).value;
10937 mark_exp_read (t);
10938 t = c_fully_fold (t, false, NULL);
10939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10941 else
10942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10943 "expected %<,%> or %<)%>");
10945 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10946 if (t == error_mark_node)
10947 return list;
10949 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10950 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10951 OMP_CLAUSE_CHAIN (c) = list;
10952 return c;
10955 /* OpenMP 4.0:
10956 proc_bind ( proc-bind-kind )
10958 proc-bind-kind:
10959 master | close | spread */
10961 static tree
10962 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10964 location_t clause_loc = c_parser_peek_token (parser)->location;
10965 enum omp_clause_proc_bind_kind kind;
10966 tree c;
10968 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10969 return list;
10971 if (c_parser_next_token_is (parser, CPP_NAME))
10973 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10974 if (strcmp ("master", p) == 0)
10975 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10976 else if (strcmp ("close", p) == 0)
10977 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10978 else if (strcmp ("spread", p) == 0)
10979 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10980 else
10981 goto invalid_kind;
10983 else
10984 goto invalid_kind;
10986 c_parser_consume_token (parser);
10987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10988 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10989 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10990 OMP_CLAUSE_CHAIN (c) = list;
10991 return c;
10993 invalid_kind:
10994 c_parser_error (parser, "invalid proc_bind kind");
10995 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10996 return list;
10999 /* OpenMP 4.0:
11000 to ( variable-list ) */
11002 static tree
11003 c_parser_omp_clause_to (c_parser *parser, tree list)
11005 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11008 /* OpenMP 4.0:
11009 from ( variable-list ) */
11011 static tree
11012 c_parser_omp_clause_from (c_parser *parser, tree list)
11014 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11017 /* OpenMP 4.0:
11018 uniform ( variable-list ) */
11020 static tree
11021 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11023 /* The clauses location. */
11024 location_t loc = c_parser_peek_token (parser)->location;
11026 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11028 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11029 list);
11030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11032 return list;
11035 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11036 is a bitmask in MASK. Return the list of clauses found; the result
11037 of clause default goes in *pdefault. */
11039 static tree
11040 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11041 const char *where, bool finish_p = true)
11043 tree clauses = NULL;
11044 bool first = true, cilk_simd_fn = false;
11046 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11048 location_t here;
11049 pragma_omp_clause c_kind;
11050 const char *c_name;
11051 tree prev = clauses;
11053 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11054 c_parser_consume_token (parser);
11056 here = c_parser_peek_token (parser)->location;
11057 c_kind = c_parser_omp_clause_name (parser);
11059 switch (c_kind)
11061 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11062 clauses = c_parser_omp_clause_collapse (parser, clauses);
11063 c_name = "collapse";
11064 break;
11065 case PRAGMA_OMP_CLAUSE_COPYIN:
11066 clauses = c_parser_omp_clause_copyin (parser, clauses);
11067 c_name = "copyin";
11068 break;
11069 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11070 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11071 c_name = "copyprivate";
11072 break;
11073 case PRAGMA_OMP_CLAUSE_DEFAULT:
11074 clauses = c_parser_omp_clause_default (parser, clauses);
11075 c_name = "default";
11076 break;
11077 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11078 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11079 c_name = "firstprivate";
11080 break;
11081 case PRAGMA_OMP_CLAUSE_FINAL:
11082 clauses = c_parser_omp_clause_final (parser, clauses);
11083 c_name = "final";
11084 break;
11085 case PRAGMA_OMP_CLAUSE_IF:
11086 clauses = c_parser_omp_clause_if (parser, clauses);
11087 c_name = "if";
11088 break;
11089 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11090 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11091 c_name = "lastprivate";
11092 break;
11093 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11094 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11095 c_name = "mergeable";
11096 break;
11097 case PRAGMA_OMP_CLAUSE_NOWAIT:
11098 clauses = c_parser_omp_clause_nowait (parser, clauses);
11099 c_name = "nowait";
11100 break;
11101 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11102 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11103 c_name = "num_threads";
11104 break;
11105 case PRAGMA_OMP_CLAUSE_ORDERED:
11106 clauses = c_parser_omp_clause_ordered (parser, clauses);
11107 c_name = "ordered";
11108 break;
11109 case PRAGMA_OMP_CLAUSE_PRIVATE:
11110 clauses = c_parser_omp_clause_private (parser, clauses);
11111 c_name = "private";
11112 break;
11113 case PRAGMA_OMP_CLAUSE_REDUCTION:
11114 clauses = c_parser_omp_clause_reduction (parser, clauses);
11115 c_name = "reduction";
11116 break;
11117 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11118 clauses = c_parser_omp_clause_schedule (parser, clauses);
11119 c_name = "schedule";
11120 break;
11121 case PRAGMA_OMP_CLAUSE_SHARED:
11122 clauses = c_parser_omp_clause_shared (parser, clauses);
11123 c_name = "shared";
11124 break;
11125 case PRAGMA_OMP_CLAUSE_UNTIED:
11126 clauses = c_parser_omp_clause_untied (parser, clauses);
11127 c_name = "untied";
11128 break;
11129 case PRAGMA_OMP_CLAUSE_INBRANCH:
11130 case PRAGMA_CILK_CLAUSE_MASK:
11131 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11132 clauses);
11133 c_name = "inbranch";
11134 break;
11135 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11136 case PRAGMA_CILK_CLAUSE_NOMASK:
11137 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11138 clauses);
11139 c_name = "notinbranch";
11140 break;
11141 case PRAGMA_OMP_CLAUSE_PARALLEL:
11142 clauses
11143 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11144 clauses);
11145 c_name = "parallel";
11146 if (!first)
11148 clause_not_first:
11149 error_at (here, "%qs must be the first clause of %qs",
11150 c_name, where);
11151 clauses = prev;
11153 break;
11154 case PRAGMA_OMP_CLAUSE_FOR:
11155 clauses
11156 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11157 clauses);
11158 c_name = "for";
11159 if (!first)
11160 goto clause_not_first;
11161 break;
11162 case PRAGMA_OMP_CLAUSE_SECTIONS:
11163 clauses
11164 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11165 clauses);
11166 c_name = "sections";
11167 if (!first)
11168 goto clause_not_first;
11169 break;
11170 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11171 clauses
11172 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11173 clauses);
11174 c_name = "taskgroup";
11175 if (!first)
11176 goto clause_not_first;
11177 break;
11178 case PRAGMA_OMP_CLAUSE_TO:
11179 clauses = c_parser_omp_clause_to (parser, clauses);
11180 c_name = "to";
11181 break;
11182 case PRAGMA_OMP_CLAUSE_FROM:
11183 clauses = c_parser_omp_clause_from (parser, clauses);
11184 c_name = "from";
11185 break;
11186 case PRAGMA_OMP_CLAUSE_UNIFORM:
11187 clauses = c_parser_omp_clause_uniform (parser, clauses);
11188 c_name = "uniform";
11189 break;
11190 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11191 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11192 c_name = "num_teams";
11193 break;
11194 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11195 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11196 c_name = "thread_limit";
11197 break;
11198 case PRAGMA_OMP_CLAUSE_ALIGNED:
11199 clauses = c_parser_omp_clause_aligned (parser, clauses);
11200 c_name = "aligned";
11201 break;
11202 case PRAGMA_OMP_CLAUSE_LINEAR:
11203 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11204 cilk_simd_fn = true;
11205 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11206 c_name = "linear";
11207 break;
11208 case PRAGMA_OMP_CLAUSE_DEPEND:
11209 clauses = c_parser_omp_clause_depend (parser, clauses);
11210 c_name = "depend";
11211 break;
11212 case PRAGMA_OMP_CLAUSE_MAP:
11213 clauses = c_parser_omp_clause_map (parser, clauses);
11214 c_name = "map";
11215 break;
11216 case PRAGMA_OMP_CLAUSE_DEVICE:
11217 clauses = c_parser_omp_clause_device (parser, clauses);
11218 c_name = "device";
11219 break;
11220 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11221 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11222 c_name = "dist_schedule";
11223 break;
11224 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11225 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11226 c_name = "proc_bind";
11227 break;
11228 case PRAGMA_OMP_CLAUSE_SAFELEN:
11229 clauses = c_parser_omp_clause_safelen (parser, clauses);
11230 c_name = "safelen";
11231 break;
11232 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11233 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11234 c_name = "simdlen";
11235 break;
11236 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11237 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11238 c_name = "simdlen";
11239 break;
11240 default:
11241 c_parser_error (parser, "expected %<#pragma omp%> clause");
11242 goto saw_error;
11245 first = false;
11247 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11249 /* Remove the invalid clause(s) from the list to avoid
11250 confusing the rest of the compiler. */
11251 clauses = prev;
11252 error_at (here, "%qs is not valid for %qs", c_name, where);
11256 saw_error:
11257 c_parser_skip_to_pragma_eol (parser);
11259 if (finish_p)
11260 return c_finish_omp_clauses (clauses);
11262 return clauses;
11265 /* OpenMP 2.5:
11266 structured-block:
11267 statement
11269 In practice, we're also interested in adding the statement to an
11270 outer node. So it is convenient if we work around the fact that
11271 c_parser_statement calls add_stmt. */
11273 static tree
11274 c_parser_omp_structured_block (c_parser *parser)
11276 tree stmt = push_stmt_list ();
11277 c_parser_statement (parser);
11278 return pop_stmt_list (stmt);
11281 /* OpenMP 2.5:
11282 # pragma omp atomic new-line
11283 expression-stmt
11285 expression-stmt:
11286 x binop= expr | x++ | ++x | x-- | --x
11287 binop:
11288 +, *, -, /, &, ^, |, <<, >>
11290 where x is an lvalue expression with scalar type.
11292 OpenMP 3.1:
11293 # pragma omp atomic new-line
11294 update-stmt
11296 # pragma omp atomic read new-line
11297 read-stmt
11299 # pragma omp atomic write new-line
11300 write-stmt
11302 # pragma omp atomic update new-line
11303 update-stmt
11305 # pragma omp atomic capture new-line
11306 capture-stmt
11308 # pragma omp atomic capture new-line
11309 capture-block
11311 read-stmt:
11312 v = x
11313 write-stmt:
11314 x = expr
11315 update-stmt:
11316 expression-stmt | x = x binop expr
11317 capture-stmt:
11318 v = expression-stmt
11319 capture-block:
11320 { v = x; update-stmt; } | { update-stmt; v = x; }
11322 OpenMP 4.0:
11323 update-stmt:
11324 expression-stmt | x = x binop expr | x = expr binop x
11325 capture-stmt:
11326 v = update-stmt
11327 capture-block:
11328 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11330 where x and v are lvalue expressions with scalar type.
11332 LOC is the location of the #pragma token. */
11334 static void
11335 c_parser_omp_atomic (location_t loc, c_parser *parser)
11337 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11338 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11339 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11340 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11341 struct c_expr expr;
11342 location_t eloc;
11343 bool structured_block = false;
11344 bool swapped = false;
11345 bool seq_cst = false;
11347 if (c_parser_next_token_is (parser, CPP_NAME))
11349 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11350 if (!strcmp (p, "seq_cst"))
11352 seq_cst = true;
11353 c_parser_consume_token (parser);
11354 if (c_parser_next_token_is (parser, CPP_COMMA)
11355 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11356 c_parser_consume_token (parser);
11359 if (c_parser_next_token_is (parser, CPP_NAME))
11361 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11363 if (!strcmp (p, "read"))
11364 code = OMP_ATOMIC_READ;
11365 else if (!strcmp (p, "write"))
11366 code = NOP_EXPR;
11367 else if (!strcmp (p, "update"))
11368 code = OMP_ATOMIC;
11369 else if (!strcmp (p, "capture"))
11370 code = OMP_ATOMIC_CAPTURE_NEW;
11371 else
11372 p = NULL;
11373 if (p)
11374 c_parser_consume_token (parser);
11376 if (!seq_cst)
11378 if (c_parser_next_token_is (parser, CPP_COMMA)
11379 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11380 c_parser_consume_token (parser);
11382 if (c_parser_next_token_is (parser, CPP_NAME))
11384 const char *p
11385 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11386 if (!strcmp (p, "seq_cst"))
11388 seq_cst = true;
11389 c_parser_consume_token (parser);
11393 c_parser_skip_to_pragma_eol (parser);
11395 switch (code)
11397 case OMP_ATOMIC_READ:
11398 case NOP_EXPR: /* atomic write */
11399 v = c_parser_unary_expression (parser).value;
11400 v = c_fully_fold (v, false, NULL);
11401 if (v == error_mark_node)
11402 goto saw_error;
11403 loc = c_parser_peek_token (parser)->location;
11404 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11405 goto saw_error;
11406 if (code == NOP_EXPR)
11407 lhs = c_parser_expression (parser).value;
11408 else
11409 lhs = c_parser_unary_expression (parser).value;
11410 lhs = c_fully_fold (lhs, false, NULL);
11411 if (lhs == error_mark_node)
11412 goto saw_error;
11413 if (code == NOP_EXPR)
11415 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11416 opcode. */
11417 code = OMP_ATOMIC;
11418 rhs = lhs;
11419 lhs = v;
11420 v = NULL_TREE;
11422 goto done;
11423 case OMP_ATOMIC_CAPTURE_NEW:
11424 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11426 c_parser_consume_token (parser);
11427 structured_block = true;
11429 else
11431 v = c_parser_unary_expression (parser).value;
11432 v = c_fully_fold (v, false, NULL);
11433 if (v == error_mark_node)
11434 goto saw_error;
11435 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11436 goto saw_error;
11438 break;
11439 default:
11440 break;
11443 /* For structured_block case we don't know yet whether
11444 old or new x should be captured. */
11445 restart:
11446 eloc = c_parser_peek_token (parser)->location;
11447 expr = c_parser_unary_expression (parser);
11448 lhs = expr.value;
11449 expr = default_function_array_conversion (eloc, expr);
11450 unfolded_lhs = expr.value;
11451 lhs = c_fully_fold (lhs, false, NULL);
11452 orig_lhs = lhs;
11453 switch (TREE_CODE (lhs))
11455 case ERROR_MARK:
11456 saw_error:
11457 c_parser_skip_to_end_of_block_or_statement (parser);
11458 if (structured_block)
11460 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11461 c_parser_consume_token (parser);
11462 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11464 c_parser_skip_to_end_of_block_or_statement (parser);
11465 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11466 c_parser_consume_token (parser);
11469 return;
11471 case POSTINCREMENT_EXPR:
11472 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11473 code = OMP_ATOMIC_CAPTURE_OLD;
11474 /* FALLTHROUGH */
11475 case PREINCREMENT_EXPR:
11476 lhs = TREE_OPERAND (lhs, 0);
11477 unfolded_lhs = NULL_TREE;
11478 opcode = PLUS_EXPR;
11479 rhs = integer_one_node;
11480 break;
11482 case POSTDECREMENT_EXPR:
11483 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11484 code = OMP_ATOMIC_CAPTURE_OLD;
11485 /* FALLTHROUGH */
11486 case PREDECREMENT_EXPR:
11487 lhs = TREE_OPERAND (lhs, 0);
11488 unfolded_lhs = NULL_TREE;
11489 opcode = MINUS_EXPR;
11490 rhs = integer_one_node;
11491 break;
11493 case COMPOUND_EXPR:
11494 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11495 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11496 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11497 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11498 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11499 (TREE_OPERAND (lhs, 1), 0), 0)))
11500 == BOOLEAN_TYPE)
11501 /* Undo effects of boolean_increment for post {in,de}crement. */
11502 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11503 /* FALLTHRU */
11504 case MODIFY_EXPR:
11505 if (TREE_CODE (lhs) == MODIFY_EXPR
11506 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11508 /* Undo effects of boolean_increment. */
11509 if (integer_onep (TREE_OPERAND (lhs, 1)))
11511 /* This is pre or post increment. */
11512 rhs = TREE_OPERAND (lhs, 1);
11513 lhs = TREE_OPERAND (lhs, 0);
11514 unfolded_lhs = NULL_TREE;
11515 opcode = NOP_EXPR;
11516 if (code == OMP_ATOMIC_CAPTURE_NEW
11517 && !structured_block
11518 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11519 code = OMP_ATOMIC_CAPTURE_OLD;
11520 break;
11522 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11523 && TREE_OPERAND (lhs, 0)
11524 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11526 /* This is pre or post decrement. */
11527 rhs = TREE_OPERAND (lhs, 1);
11528 lhs = TREE_OPERAND (lhs, 0);
11529 unfolded_lhs = NULL_TREE;
11530 opcode = NOP_EXPR;
11531 if (code == OMP_ATOMIC_CAPTURE_NEW
11532 && !structured_block
11533 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11534 code = OMP_ATOMIC_CAPTURE_OLD;
11535 break;
11538 /* FALLTHRU */
11539 default:
11540 switch (c_parser_peek_token (parser)->type)
11542 case CPP_MULT_EQ:
11543 opcode = MULT_EXPR;
11544 break;
11545 case CPP_DIV_EQ:
11546 opcode = TRUNC_DIV_EXPR;
11547 break;
11548 case CPP_PLUS_EQ:
11549 opcode = PLUS_EXPR;
11550 break;
11551 case CPP_MINUS_EQ:
11552 opcode = MINUS_EXPR;
11553 break;
11554 case CPP_LSHIFT_EQ:
11555 opcode = LSHIFT_EXPR;
11556 break;
11557 case CPP_RSHIFT_EQ:
11558 opcode = RSHIFT_EXPR;
11559 break;
11560 case CPP_AND_EQ:
11561 opcode = BIT_AND_EXPR;
11562 break;
11563 case CPP_OR_EQ:
11564 opcode = BIT_IOR_EXPR;
11565 break;
11566 case CPP_XOR_EQ:
11567 opcode = BIT_XOR_EXPR;
11568 break;
11569 case CPP_EQ:
11570 c_parser_consume_token (parser);
11571 eloc = c_parser_peek_token (parser)->location;
11572 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11573 rhs1 = expr.value;
11574 switch (TREE_CODE (rhs1))
11576 case MULT_EXPR:
11577 case TRUNC_DIV_EXPR:
11578 case PLUS_EXPR:
11579 case MINUS_EXPR:
11580 case LSHIFT_EXPR:
11581 case RSHIFT_EXPR:
11582 case BIT_AND_EXPR:
11583 case BIT_IOR_EXPR:
11584 case BIT_XOR_EXPR:
11585 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11587 opcode = TREE_CODE (rhs1);
11588 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11589 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11590 goto stmt_done;
11592 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11594 opcode = TREE_CODE (rhs1);
11595 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11596 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11597 swapped = !commutative_tree_code (opcode);
11598 goto stmt_done;
11600 break;
11601 case ERROR_MARK:
11602 goto saw_error;
11603 default:
11604 break;
11606 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11608 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11610 code = OMP_ATOMIC_CAPTURE_OLD;
11611 v = lhs;
11612 lhs = NULL_TREE;
11613 expr = default_function_array_read_conversion (eloc, expr);
11614 unfolded_lhs1 = expr.value;
11615 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11616 rhs1 = NULL_TREE;
11617 c_parser_consume_token (parser);
11618 goto restart;
11620 if (structured_block)
11622 opcode = NOP_EXPR;
11623 expr = default_function_array_read_conversion (eloc, expr);
11624 rhs = c_fully_fold (expr.value, false, NULL);
11625 rhs1 = NULL_TREE;
11626 goto stmt_done;
11629 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11630 goto saw_error;
11631 default:
11632 c_parser_error (parser,
11633 "invalid operator for %<#pragma omp atomic%>");
11634 goto saw_error;
11637 /* Arrange to pass the location of the assignment operator to
11638 c_finish_omp_atomic. */
11639 loc = c_parser_peek_token (parser)->location;
11640 c_parser_consume_token (parser);
11641 eloc = c_parser_peek_token (parser)->location;
11642 expr = c_parser_expression (parser);
11643 expr = default_function_array_read_conversion (eloc, expr);
11644 rhs = expr.value;
11645 rhs = c_fully_fold (rhs, false, NULL);
11646 break;
11648 stmt_done:
11649 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11651 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11652 goto saw_error;
11653 v = c_parser_unary_expression (parser).value;
11654 v = c_fully_fold (v, false, NULL);
11655 if (v == error_mark_node)
11656 goto saw_error;
11657 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11658 goto saw_error;
11659 eloc = c_parser_peek_token (parser)->location;
11660 expr = c_parser_unary_expression (parser);
11661 lhs1 = expr.value;
11662 expr = default_function_array_read_conversion (eloc, expr);
11663 unfolded_lhs1 = expr.value;
11664 lhs1 = c_fully_fold (lhs1, false, NULL);
11665 if (lhs1 == error_mark_node)
11666 goto saw_error;
11668 if (structured_block)
11670 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11671 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11673 done:
11674 if (unfolded_lhs && unfolded_lhs1
11675 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11677 error ("%<#pragma omp atomic capture%> uses two different "
11678 "expressions for memory");
11679 stmt = error_mark_node;
11681 else
11682 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11683 swapped, seq_cst);
11684 if (stmt != error_mark_node)
11685 add_stmt (stmt);
11687 if (!structured_block)
11688 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11692 /* OpenMP 2.5:
11693 # pragma omp barrier new-line
11696 static void
11697 c_parser_omp_barrier (c_parser *parser)
11699 location_t loc = c_parser_peek_token (parser)->location;
11700 c_parser_consume_pragma (parser);
11701 c_parser_skip_to_pragma_eol (parser);
11703 c_finish_omp_barrier (loc);
11706 /* OpenMP 2.5:
11707 # pragma omp critical [(name)] new-line
11708 structured-block
11710 LOC is the location of the #pragma itself. */
11712 static tree
11713 c_parser_omp_critical (location_t loc, c_parser *parser)
11715 tree stmt, name = NULL;
11717 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11719 c_parser_consume_token (parser);
11720 if (c_parser_next_token_is (parser, CPP_NAME))
11722 name = c_parser_peek_token (parser)->value;
11723 c_parser_consume_token (parser);
11724 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11726 else
11727 c_parser_error (parser, "expected identifier");
11729 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11730 c_parser_error (parser, "expected %<(%> or end of line");
11731 c_parser_skip_to_pragma_eol (parser);
11733 stmt = c_parser_omp_structured_block (parser);
11734 return c_finish_omp_critical (loc, stmt, name);
11737 /* OpenMP 2.5:
11738 # pragma omp flush flush-vars[opt] new-line
11740 flush-vars:
11741 ( variable-list ) */
11743 static void
11744 c_parser_omp_flush (c_parser *parser)
11746 location_t loc = c_parser_peek_token (parser)->location;
11747 c_parser_consume_pragma (parser);
11748 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11749 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11750 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11751 c_parser_error (parser, "expected %<(%> or end of line");
11752 c_parser_skip_to_pragma_eol (parser);
11754 c_finish_omp_flush (loc);
11757 /* Parse the restricted form of the for statement allowed by OpenMP.
11758 The real trick here is to determine the loop control variable early
11759 so that we can push a new decl if necessary to make it private.
11760 LOC is the location of the OMP in "#pragma omp". */
11762 static tree
11763 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11764 tree clauses, tree *cclauses)
11766 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11767 tree declv, condv, incrv, initv, ret = NULL;
11768 bool fail = false, open_brace_parsed = false;
11769 int i, collapse = 1, nbraces = 0;
11770 location_t for_loc;
11771 vec<tree, va_gc> *for_block = make_tree_vector ();
11773 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11774 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11775 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
11777 gcc_assert (collapse >= 1);
11779 declv = make_tree_vec (collapse);
11780 initv = make_tree_vec (collapse);
11781 condv = make_tree_vec (collapse);
11782 incrv = make_tree_vec (collapse);
11784 if (code != CILK_FOR
11785 && !c_parser_next_token_is_keyword (parser, RID_FOR))
11787 c_parser_error (parser, "for statement expected");
11788 return NULL;
11790 if (code == CILK_FOR
11791 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
11793 c_parser_error (parser, "_Cilk_for statement expected");
11794 return NULL;
11796 for_loc = c_parser_peek_token (parser)->location;
11797 c_parser_consume_token (parser);
11799 for (i = 0; i < collapse; i++)
11801 int bracecount = 0;
11803 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11804 goto pop_scopes;
11806 /* Parse the initialization declaration or expression. */
11807 if (c_parser_next_tokens_start_declaration (parser))
11809 if (i > 0)
11810 vec_safe_push (for_block, c_begin_compound_stmt (true));
11811 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11812 NULL, vNULL);
11813 decl = check_for_loop_decls (for_loc, flag_isoc99);
11814 if (decl == NULL)
11815 goto error_init;
11816 if (DECL_INITIAL (decl) == error_mark_node)
11817 decl = error_mark_node;
11818 init = decl;
11820 else if (c_parser_next_token_is (parser, CPP_NAME)
11821 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11823 struct c_expr decl_exp;
11824 struct c_expr init_exp;
11825 location_t init_loc;
11827 decl_exp = c_parser_postfix_expression (parser);
11828 decl = decl_exp.value;
11830 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11832 init_loc = c_parser_peek_token (parser)->location;
11833 init_exp = c_parser_expr_no_commas (parser, NULL);
11834 init_exp = default_function_array_read_conversion (init_loc,
11835 init_exp);
11836 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11837 NOP_EXPR, init_loc, init_exp.value,
11838 init_exp.original_type);
11839 init = c_process_expr_stmt (init_loc, init);
11841 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11843 else
11845 error_init:
11846 c_parser_error (parser,
11847 "expected iteration declaration or initialization");
11848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11849 "expected %<)%>");
11850 fail = true;
11851 goto parse_next;
11854 /* Parse the loop condition. */
11855 cond = NULL_TREE;
11856 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11858 location_t cond_loc = c_parser_peek_token (parser)->location;
11859 struct c_expr cond_expr
11860 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11862 cond = cond_expr.value;
11863 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11864 cond = c_fully_fold (cond, false, NULL);
11865 switch (cond_expr.original_code)
11867 case GT_EXPR:
11868 case GE_EXPR:
11869 case LT_EXPR:
11870 case LE_EXPR:
11871 break;
11872 case NE_EXPR:
11873 if (code == CILK_SIMD || code == CILK_FOR)
11874 break;
11875 /* FALLTHRU. */
11876 default:
11877 /* Can't be cond = error_mark_node, because we want to preserve
11878 the location until c_finish_omp_for. */
11879 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11880 break;
11882 protected_set_expr_location (cond, cond_loc);
11884 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11886 /* Parse the increment expression. */
11887 incr = NULL_TREE;
11888 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11890 location_t incr_loc = c_parser_peek_token (parser)->location;
11892 incr = c_process_expr_stmt (incr_loc,
11893 c_parser_expression (parser).value);
11895 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11897 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11898 fail = true;
11899 else
11901 TREE_VEC_ELT (declv, i) = decl;
11902 TREE_VEC_ELT (initv, i) = init;
11903 TREE_VEC_ELT (condv, i) = cond;
11904 TREE_VEC_ELT (incrv, i) = incr;
11907 parse_next:
11908 if (i == collapse - 1)
11909 break;
11911 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11912 in between the collapsed for loops to be still considered perfectly
11913 nested. Hopefully the final version clarifies this.
11914 For now handle (multiple) {'s and empty statements. */
11917 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11919 c_parser_consume_token (parser);
11920 break;
11922 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11924 c_parser_consume_token (parser);
11925 bracecount++;
11927 else if (bracecount
11928 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11929 c_parser_consume_token (parser);
11930 else
11932 c_parser_error (parser, "not enough perfectly nested loops");
11933 if (bracecount)
11935 open_brace_parsed = true;
11936 bracecount--;
11938 fail = true;
11939 collapse = 0;
11940 break;
11943 while (1);
11945 nbraces += bracecount;
11948 save_break = c_break_label;
11949 if (code == CILK_SIMD)
11950 c_break_label = build_int_cst (size_type_node, 2);
11951 else
11952 c_break_label = size_one_node;
11953 save_cont = c_cont_label;
11954 c_cont_label = NULL_TREE;
11955 body = push_stmt_list ();
11957 if (open_brace_parsed)
11959 location_t here = c_parser_peek_token (parser)->location;
11960 stmt = c_begin_compound_stmt (true);
11961 c_parser_compound_statement_nostart (parser);
11962 add_stmt (c_end_compound_stmt (here, stmt, true));
11964 else
11965 add_stmt (c_parser_c99_block_statement (parser));
11966 if (c_cont_label)
11968 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11969 SET_EXPR_LOCATION (t, loc);
11970 add_stmt (t);
11973 body = pop_stmt_list (body);
11974 c_break_label = save_break;
11975 c_cont_label = save_cont;
11977 while (nbraces)
11979 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11981 c_parser_consume_token (parser);
11982 nbraces--;
11984 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11985 c_parser_consume_token (parser);
11986 else
11988 c_parser_error (parser, "collapsed loops not perfectly nested");
11989 while (nbraces)
11991 location_t here = c_parser_peek_token (parser)->location;
11992 stmt = c_begin_compound_stmt (true);
11993 add_stmt (body);
11994 c_parser_compound_statement_nostart (parser);
11995 body = c_end_compound_stmt (here, stmt, true);
11996 nbraces--;
11998 goto pop_scopes;
12002 /* Only bother calling c_finish_omp_for if we haven't already generated
12003 an error from the initialization parsing. */
12004 if (!fail)
12006 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
12007 incrv, body, NULL);
12008 if (stmt)
12010 if (cclauses != NULL
12011 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
12013 tree *c;
12014 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
12015 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
12016 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
12017 c = &OMP_CLAUSE_CHAIN (*c);
12018 else
12020 for (i = 0; i < collapse; i++)
12021 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
12022 break;
12023 if (i == collapse)
12024 c = &OMP_CLAUSE_CHAIN (*c);
12025 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
12027 error_at (loc,
12028 "iteration variable %qD should not be firstprivate",
12029 OMP_CLAUSE_DECL (*c));
12030 *c = OMP_CLAUSE_CHAIN (*c);
12032 else
12034 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
12035 change it to shared (decl) in
12036 OMP_PARALLEL_CLAUSES. */
12037 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
12038 OMP_CLAUSE_LASTPRIVATE);
12039 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
12040 if (code == OMP_SIMD)
12042 OMP_CLAUSE_CHAIN (l)
12043 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12044 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
12046 else
12048 OMP_CLAUSE_CHAIN (l) = clauses;
12049 clauses = l;
12051 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
12055 OMP_FOR_CLAUSES (stmt) = clauses;
12057 ret = stmt;
12059 pop_scopes:
12060 while (!for_block->is_empty ())
12062 /* FIXME diagnostics: LOC below should be the actual location of
12063 this particular for block. We need to build a list of
12064 locations to go along with FOR_BLOCK. */
12065 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
12066 add_stmt (stmt);
12068 release_tree_vector (for_block);
12069 return ret;
12072 /* Helper function for OpenMP parsing, split clauses and call
12073 finish_omp_clauses on each of the set of clauses afterwards. */
12075 static void
12076 omp_split_clauses (location_t loc, enum tree_code code,
12077 omp_clause_mask mask, tree clauses, tree *cclauses)
12079 int i;
12080 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
12081 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
12082 if (cclauses[i])
12083 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
12086 /* OpenMP 4.0:
12087 #pragma omp simd simd-clause[optseq] new-line
12088 for-loop
12090 LOC is the location of the #pragma token.
12093 #define OMP_SIMD_CLAUSE_MASK \
12094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
12095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12102 static tree
12103 c_parser_omp_simd (location_t loc, c_parser *parser,
12104 char *p_name, omp_clause_mask mask, tree *cclauses)
12106 tree block, clauses, ret;
12108 strcat (p_name, " simd");
12109 mask |= OMP_SIMD_CLAUSE_MASK;
12110 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
12112 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12113 if (cclauses)
12115 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
12116 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
12119 block = c_begin_compound_stmt (true);
12120 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
12121 block = c_end_compound_stmt (loc, block, true);
12122 add_stmt (block);
12124 return ret;
12127 /* OpenMP 2.5:
12128 #pragma omp for for-clause[optseq] new-line
12129 for-loop
12131 OpenMP 4.0:
12132 #pragma omp for simd for-simd-clause[optseq] new-line
12133 for-loop
12135 LOC is the location of the #pragma token.
12138 #define OMP_FOR_CLAUSE_MASK \
12139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12148 static tree
12149 c_parser_omp_for (location_t loc, c_parser *parser,
12150 char *p_name, omp_clause_mask mask, tree *cclauses)
12152 tree block, clauses, ret;
12154 strcat (p_name, " for");
12155 mask |= OMP_FOR_CLAUSE_MASK;
12156 if (cclauses)
12157 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12159 if (c_parser_next_token_is (parser, CPP_NAME))
12161 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12163 if (strcmp (p, "simd") == 0)
12165 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12166 if (cclauses == NULL)
12167 cclauses = cclauses_buf;
12169 c_parser_consume_token (parser);
12170 if (!flag_openmp) /* flag_openmp_simd */
12171 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12172 block = c_begin_compound_stmt (true);
12173 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12174 block = c_end_compound_stmt (loc, block, true);
12175 if (ret == NULL_TREE)
12176 return ret;
12177 ret = make_node (OMP_FOR);
12178 TREE_TYPE (ret) = void_type_node;
12179 OMP_FOR_BODY (ret) = block;
12180 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12181 SET_EXPR_LOCATION (ret, loc);
12182 add_stmt (ret);
12183 return ret;
12186 if (!flag_openmp) /* flag_openmp_simd */
12188 c_parser_skip_to_pragma_eol (parser);
12189 return NULL_TREE;
12192 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12193 if (cclauses)
12195 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12196 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12199 block = c_begin_compound_stmt (true);
12200 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12201 block = c_end_compound_stmt (loc, block, true);
12202 add_stmt (block);
12204 return ret;
12207 /* OpenMP 2.5:
12208 # pragma omp master new-line
12209 structured-block
12211 LOC is the location of the #pragma token.
12214 static tree
12215 c_parser_omp_master (location_t loc, c_parser *parser)
12217 c_parser_skip_to_pragma_eol (parser);
12218 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12221 /* OpenMP 2.5:
12222 # pragma omp ordered new-line
12223 structured-block
12225 LOC is the location of the #pragma itself.
12228 static tree
12229 c_parser_omp_ordered (location_t loc, c_parser *parser)
12231 c_parser_skip_to_pragma_eol (parser);
12232 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12235 /* OpenMP 2.5:
12237 section-scope:
12238 { section-sequence }
12240 section-sequence:
12241 section-directive[opt] structured-block
12242 section-sequence section-directive structured-block
12244 SECTIONS_LOC is the location of the #pragma omp sections. */
12246 static tree
12247 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12249 tree stmt, substmt;
12250 bool error_suppress = false;
12251 location_t loc;
12253 loc = c_parser_peek_token (parser)->location;
12254 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12256 /* Avoid skipping until the end of the block. */
12257 parser->error = false;
12258 return NULL_TREE;
12261 stmt = push_stmt_list ();
12263 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12265 substmt = c_parser_omp_structured_block (parser);
12266 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12267 SET_EXPR_LOCATION (substmt, loc);
12268 add_stmt (substmt);
12271 while (1)
12273 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12274 break;
12275 if (c_parser_next_token_is (parser, CPP_EOF))
12276 break;
12278 loc = c_parser_peek_token (parser)->location;
12279 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12281 c_parser_consume_pragma (parser);
12282 c_parser_skip_to_pragma_eol (parser);
12283 error_suppress = false;
12285 else if (!error_suppress)
12287 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12288 error_suppress = true;
12291 substmt = c_parser_omp_structured_block (parser);
12292 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12293 SET_EXPR_LOCATION (substmt, loc);
12294 add_stmt (substmt);
12296 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12297 "expected %<#pragma omp section%> or %<}%>");
12299 substmt = pop_stmt_list (stmt);
12301 stmt = make_node (OMP_SECTIONS);
12302 SET_EXPR_LOCATION (stmt, sections_loc);
12303 TREE_TYPE (stmt) = void_type_node;
12304 OMP_SECTIONS_BODY (stmt) = substmt;
12306 return add_stmt (stmt);
12309 /* OpenMP 2.5:
12310 # pragma omp sections sections-clause[optseq] newline
12311 sections-scope
12313 LOC is the location of the #pragma token.
12316 #define OMP_SECTIONS_CLAUSE_MASK \
12317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12323 static tree
12324 c_parser_omp_sections (location_t loc, c_parser *parser,
12325 char *p_name, omp_clause_mask mask, tree *cclauses)
12327 tree block, clauses, ret;
12329 strcat (p_name, " sections");
12330 mask |= OMP_SECTIONS_CLAUSE_MASK;
12331 if (cclauses)
12332 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12334 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12335 if (cclauses)
12337 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12338 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12341 block = c_begin_compound_stmt (true);
12342 ret = c_parser_omp_sections_scope (loc, parser);
12343 if (ret)
12344 OMP_SECTIONS_CLAUSES (ret) = clauses;
12345 block = c_end_compound_stmt (loc, block, true);
12346 add_stmt (block);
12348 return ret;
12351 /* OpenMP 2.5:
12352 # pragma omp parallel parallel-clause[optseq] new-line
12353 structured-block
12354 # pragma omp parallel for parallel-for-clause[optseq] new-line
12355 structured-block
12356 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12357 structured-block
12359 OpenMP 4.0:
12360 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12361 structured-block
12363 LOC is the location of the #pragma token.
12366 #define OMP_PARALLEL_CLAUSE_MASK \
12367 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12377 static tree
12378 c_parser_omp_parallel (location_t loc, c_parser *parser,
12379 char *p_name, omp_clause_mask mask, tree *cclauses)
12381 tree stmt, clauses, block;
12383 strcat (p_name, " parallel");
12384 mask |= OMP_PARALLEL_CLAUSE_MASK;
12386 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12388 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12389 if (cclauses == NULL)
12390 cclauses = cclauses_buf;
12392 c_parser_consume_token (parser);
12393 if (!flag_openmp) /* flag_openmp_simd */
12394 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12395 block = c_begin_omp_parallel ();
12396 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12397 stmt
12398 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12399 block);
12400 if (ret == NULL_TREE)
12401 return ret;
12402 OMP_PARALLEL_COMBINED (stmt) = 1;
12403 return stmt;
12405 else if (cclauses)
12407 error_at (loc, "expected %<for%> after %qs", p_name);
12408 c_parser_skip_to_pragma_eol (parser);
12409 return NULL_TREE;
12411 else if (!flag_openmp) /* flag_openmp_simd */
12413 c_parser_skip_to_pragma_eol (parser);
12414 return NULL_TREE;
12416 else if (c_parser_next_token_is (parser, CPP_NAME))
12418 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12419 if (strcmp (p, "sections") == 0)
12421 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12422 if (cclauses == NULL)
12423 cclauses = cclauses_buf;
12425 c_parser_consume_token (parser);
12426 block = c_begin_omp_parallel ();
12427 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12428 stmt = c_finish_omp_parallel (loc,
12429 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12430 block);
12431 OMP_PARALLEL_COMBINED (stmt) = 1;
12432 return stmt;
12436 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12438 block = c_begin_omp_parallel ();
12439 c_parser_statement (parser);
12440 stmt = c_finish_omp_parallel (loc, clauses, block);
12442 return stmt;
12445 /* OpenMP 2.5:
12446 # pragma omp single single-clause[optseq] new-line
12447 structured-block
12449 LOC is the location of the #pragma.
12452 #define OMP_SINGLE_CLAUSE_MASK \
12453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12458 static tree
12459 c_parser_omp_single (location_t loc, c_parser *parser)
12461 tree stmt = make_node (OMP_SINGLE);
12462 SET_EXPR_LOCATION (stmt, loc);
12463 TREE_TYPE (stmt) = void_type_node;
12465 OMP_SINGLE_CLAUSES (stmt)
12466 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12467 "#pragma omp single");
12468 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12470 return add_stmt (stmt);
12473 /* OpenMP 3.0:
12474 # pragma omp task task-clause[optseq] new-line
12476 LOC is the location of the #pragma.
12479 #define OMP_TASK_CLAUSE_MASK \
12480 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12490 static tree
12491 c_parser_omp_task (location_t loc, c_parser *parser)
12493 tree clauses, block;
12495 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12496 "#pragma omp task");
12498 block = c_begin_omp_task ();
12499 c_parser_statement (parser);
12500 return c_finish_omp_task (loc, clauses, block);
12503 /* OpenMP 3.0:
12504 # pragma omp taskwait new-line
12507 static void
12508 c_parser_omp_taskwait (c_parser *parser)
12510 location_t loc = c_parser_peek_token (parser)->location;
12511 c_parser_consume_pragma (parser);
12512 c_parser_skip_to_pragma_eol (parser);
12514 c_finish_omp_taskwait (loc);
12517 /* OpenMP 3.1:
12518 # pragma omp taskyield new-line
12521 static void
12522 c_parser_omp_taskyield (c_parser *parser)
12524 location_t loc = c_parser_peek_token (parser)->location;
12525 c_parser_consume_pragma (parser);
12526 c_parser_skip_to_pragma_eol (parser);
12528 c_finish_omp_taskyield (loc);
12531 /* OpenMP 4.0:
12532 # pragma omp taskgroup new-line
12535 static tree
12536 c_parser_omp_taskgroup (c_parser *parser)
12538 location_t loc = c_parser_peek_token (parser)->location;
12539 c_parser_skip_to_pragma_eol (parser);
12540 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12543 /* OpenMP 4.0:
12544 # pragma omp cancel cancel-clause[optseq] new-line
12546 LOC is the location of the #pragma.
12549 #define OMP_CANCEL_CLAUSE_MASK \
12550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12556 static void
12557 c_parser_omp_cancel (c_parser *parser)
12559 location_t loc = c_parser_peek_token (parser)->location;
12561 c_parser_consume_pragma (parser);
12562 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12563 "#pragma omp cancel");
12565 c_finish_omp_cancel (loc, clauses);
12568 /* OpenMP 4.0:
12569 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12571 LOC is the location of the #pragma.
12574 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12580 static void
12581 c_parser_omp_cancellation_point (c_parser *parser)
12583 location_t loc = c_parser_peek_token (parser)->location;
12584 tree clauses;
12585 bool point_seen = false;
12587 c_parser_consume_pragma (parser);
12588 if (c_parser_next_token_is (parser, CPP_NAME))
12590 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12591 if (strcmp (p, "point") == 0)
12593 c_parser_consume_token (parser);
12594 point_seen = true;
12597 if (!point_seen)
12599 c_parser_error (parser, "expected %<point%>");
12600 c_parser_skip_to_pragma_eol (parser);
12601 return;
12604 clauses
12605 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12606 "#pragma omp cancellation point");
12608 c_finish_omp_cancellation_point (loc, clauses);
12611 /* OpenMP 4.0:
12612 #pragma omp distribute distribute-clause[optseq] new-line
12613 for-loop */
12615 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12616 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12621 static tree
12622 c_parser_omp_distribute (location_t loc, c_parser *parser,
12623 char *p_name, omp_clause_mask mask, tree *cclauses)
12625 tree clauses, block, ret;
12627 strcat (p_name, " distribute");
12628 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12630 if (c_parser_next_token_is (parser, CPP_NAME))
12632 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12633 bool simd = false;
12634 bool parallel = false;
12636 if (strcmp (p, "simd") == 0)
12637 simd = true;
12638 else
12639 parallel = strcmp (p, "parallel") == 0;
12640 if (parallel || simd)
12642 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12643 if (cclauses == NULL)
12644 cclauses = cclauses_buf;
12645 c_parser_consume_token (parser);
12646 if (!flag_openmp) /* flag_openmp_simd */
12648 if (simd)
12649 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12650 else
12651 return c_parser_omp_parallel (loc, parser, p_name, mask,
12652 cclauses);
12654 block = c_begin_compound_stmt (true);
12655 if (simd)
12656 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12657 else
12658 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12659 block = c_end_compound_stmt (loc, block, true);
12660 if (ret == NULL)
12661 return ret;
12662 ret = make_node (OMP_DISTRIBUTE);
12663 TREE_TYPE (ret) = void_type_node;
12664 OMP_FOR_BODY (ret) = block;
12665 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12666 SET_EXPR_LOCATION (ret, loc);
12667 add_stmt (ret);
12668 return ret;
12671 if (!flag_openmp) /* flag_openmp_simd */
12673 c_parser_skip_to_pragma_eol (parser);
12674 return NULL_TREE;
12677 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12678 if (cclauses)
12680 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12681 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12684 block = c_begin_compound_stmt (true);
12685 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12686 block = c_end_compound_stmt (loc, block, true);
12687 add_stmt (block);
12689 return ret;
12692 /* OpenMP 4.0:
12693 # pragma omp teams teams-clause[optseq] new-line
12694 structured-block */
12696 #define OMP_TEAMS_CLAUSE_MASK \
12697 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12705 static tree
12706 c_parser_omp_teams (location_t loc, c_parser *parser,
12707 char *p_name, omp_clause_mask mask, tree *cclauses)
12709 tree clauses, block, ret;
12711 strcat (p_name, " teams");
12712 mask |= OMP_TEAMS_CLAUSE_MASK;
12714 if (c_parser_next_token_is (parser, CPP_NAME))
12716 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12717 if (strcmp (p, "distribute") == 0)
12719 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12720 if (cclauses == NULL)
12721 cclauses = cclauses_buf;
12723 c_parser_consume_token (parser);
12724 if (!flag_openmp) /* flag_openmp_simd */
12725 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12726 block = c_begin_compound_stmt (true);
12727 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12728 block = c_end_compound_stmt (loc, block, true);
12729 if (ret == NULL)
12730 return ret;
12731 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12732 ret = make_node (OMP_TEAMS);
12733 TREE_TYPE (ret) = void_type_node;
12734 OMP_TEAMS_CLAUSES (ret) = clauses;
12735 OMP_TEAMS_BODY (ret) = block;
12736 return add_stmt (ret);
12739 if (!flag_openmp) /* flag_openmp_simd */
12741 c_parser_skip_to_pragma_eol (parser);
12742 return NULL_TREE;
12745 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12746 if (cclauses)
12748 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12749 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12752 tree stmt = make_node (OMP_TEAMS);
12753 TREE_TYPE (stmt) = void_type_node;
12754 OMP_TEAMS_CLAUSES (stmt) = clauses;
12755 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12757 return add_stmt (stmt);
12760 /* OpenMP 4.0:
12761 # pragma omp target data target-data-clause[optseq] new-line
12762 structured-block */
12764 #define OMP_TARGET_DATA_CLAUSE_MASK \
12765 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12769 static tree
12770 c_parser_omp_target_data (location_t loc, c_parser *parser)
12772 tree stmt = make_node (OMP_TARGET_DATA);
12773 TREE_TYPE (stmt) = void_type_node;
12775 OMP_TARGET_DATA_CLAUSES (stmt)
12776 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12777 "#pragma omp target data");
12778 keep_next_level ();
12779 tree block = c_begin_compound_stmt (true);
12780 add_stmt (c_parser_omp_structured_block (parser));
12781 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12783 SET_EXPR_LOCATION (stmt, loc);
12784 return add_stmt (stmt);
12787 /* OpenMP 4.0:
12788 # pragma omp target update target-update-clause[optseq] new-line */
12790 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12796 static bool
12797 c_parser_omp_target_update (location_t loc, c_parser *parser,
12798 enum pragma_context context)
12800 if (context == pragma_stmt)
12802 error_at (loc,
12803 "%<#pragma omp target update%> may only be "
12804 "used in compound statements");
12805 c_parser_skip_to_pragma_eol (parser);
12806 return false;
12809 tree clauses
12810 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12811 "#pragma omp target update");
12812 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12813 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12815 error_at (loc,
12816 "%<#pragma omp target update must contain at least one "
12817 "%<from%> or %<to%> clauses");
12818 return false;
12821 tree stmt = make_node (OMP_TARGET_UPDATE);
12822 TREE_TYPE (stmt) = void_type_node;
12823 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12824 SET_EXPR_LOCATION (stmt, loc);
12825 add_stmt (stmt);
12826 return false;
12829 /* OpenMP 4.0:
12830 # pragma omp target target-clause[optseq] new-line
12831 structured-block */
12833 #define OMP_TARGET_CLAUSE_MASK \
12834 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12838 static bool
12839 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12841 location_t loc = c_parser_peek_token (parser)->location;
12842 c_parser_consume_pragma (parser);
12844 if (context != pragma_stmt && context != pragma_compound)
12846 c_parser_error (parser, "expected declaration specifiers");
12847 c_parser_skip_to_pragma_eol (parser);
12848 return false;
12851 if (c_parser_next_token_is (parser, CPP_NAME))
12853 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12855 if (strcmp (p, "teams") == 0)
12857 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12858 char p_name[sizeof ("#pragma omp target teams distribute "
12859 "parallel for simd")];
12861 c_parser_consume_token (parser);
12862 strcpy (p_name, "#pragma omp target");
12863 if (!flag_openmp) /* flag_openmp_simd */
12865 tree stmt = c_parser_omp_teams (loc, parser, p_name,
12866 OMP_TARGET_CLAUSE_MASK,
12867 cclauses);
12868 return stmt != NULL_TREE;
12870 keep_next_level ();
12871 tree block = c_begin_compound_stmt (true);
12872 tree ret = c_parser_omp_teams (loc, parser, p_name,
12873 OMP_TARGET_CLAUSE_MASK, cclauses);
12874 block = c_end_compound_stmt (loc, block, true);
12875 if (ret == NULL_TREE)
12876 return false;
12877 tree stmt = make_node (OMP_TARGET);
12878 TREE_TYPE (stmt) = void_type_node;
12879 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12880 OMP_TARGET_BODY (stmt) = block;
12881 add_stmt (stmt);
12882 return true;
12884 else if (!flag_openmp) /* flag_openmp_simd */
12886 c_parser_skip_to_pragma_eol (parser);
12887 return false;
12889 else if (strcmp (p, "data") == 0)
12891 c_parser_consume_token (parser);
12892 c_parser_omp_target_data (loc, parser);
12893 return true;
12895 else if (strcmp (p, "update") == 0)
12897 c_parser_consume_token (parser);
12898 return c_parser_omp_target_update (loc, parser, context);
12902 tree stmt = make_node (OMP_TARGET);
12903 TREE_TYPE (stmt) = void_type_node;
12905 OMP_TARGET_CLAUSES (stmt)
12906 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12907 "#pragma omp target");
12908 keep_next_level ();
12909 tree block = c_begin_compound_stmt (true);
12910 add_stmt (c_parser_omp_structured_block (parser));
12911 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12913 SET_EXPR_LOCATION (stmt, loc);
12914 add_stmt (stmt);
12915 return true;
12918 /* OpenMP 4.0:
12919 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12921 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12922 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12929 static void
12930 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12932 vec<c_token> clauses = vNULL;
12933 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12935 c_token *token = c_parser_peek_token (parser);
12936 if (token->type == CPP_EOF)
12938 c_parser_skip_to_pragma_eol (parser);
12939 clauses.release ();
12940 return;
12942 clauses.safe_push (*token);
12943 c_parser_consume_token (parser);
12945 clauses.safe_push (*c_parser_peek_token (parser));
12946 c_parser_skip_to_pragma_eol (parser);
12948 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12950 if (c_parser_peek_token (parser)->pragma_kind
12951 != PRAGMA_OMP_DECLARE_REDUCTION
12952 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12953 || strcmp (IDENTIFIER_POINTER
12954 (c_parser_peek_2nd_token (parser)->value),
12955 "simd") != 0)
12957 c_parser_error (parser,
12958 "%<#pragma omp declare simd%> must be followed by "
12959 "function declaration or definition or another "
12960 "%<#pragma omp declare simd%>");
12961 clauses.release ();
12962 return;
12964 c_parser_consume_pragma (parser);
12965 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12967 c_token *token = c_parser_peek_token (parser);
12968 if (token->type == CPP_EOF)
12970 c_parser_skip_to_pragma_eol (parser);
12971 clauses.release ();
12972 return;
12974 clauses.safe_push (*token);
12975 c_parser_consume_token (parser);
12977 clauses.safe_push (*c_parser_peek_token (parser));
12978 c_parser_skip_to_pragma_eol (parser);
12981 /* Make sure nothing tries to read past the end of the tokens. */
12982 c_token eof_token;
12983 memset (&eof_token, 0, sizeof (eof_token));
12984 eof_token.type = CPP_EOF;
12985 clauses.safe_push (eof_token);
12986 clauses.safe_push (eof_token);
12988 switch (context)
12990 case pragma_external:
12991 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12992 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12994 int ext = disable_extension_diagnostics ();
12996 c_parser_consume_token (parser);
12997 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12998 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12999 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13000 NULL, clauses);
13001 restore_extension_diagnostics (ext);
13003 else
13004 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13005 NULL, clauses);
13006 break;
13007 case pragma_struct:
13008 case pragma_param:
13009 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13010 "function declaration or definition");
13011 break;
13012 case pragma_compound:
13013 case pragma_stmt:
13014 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13015 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13017 int ext = disable_extension_diagnostics ();
13019 c_parser_consume_token (parser);
13020 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13021 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13022 if (c_parser_next_tokens_start_declaration (parser))
13024 c_parser_declaration_or_fndef (parser, true, true, true, true,
13025 true, NULL, clauses);
13026 restore_extension_diagnostics (ext);
13027 break;
13029 restore_extension_diagnostics (ext);
13031 else if (c_parser_next_tokens_start_declaration (parser))
13033 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
13034 NULL, clauses);
13035 break;
13037 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13038 "function declaration or definition");
13039 break;
13040 default:
13041 gcc_unreachable ();
13043 clauses.release ();
13046 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
13047 and put that into "omp declare simd" attribute. */
13049 static void
13050 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
13051 vec<c_token> clauses)
13053 if (flag_cilkplus
13054 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13056 error ("%<#pragma omp declare simd%> cannot be used in the same "
13057 "function marked as a Cilk Plus SIMD-enabled function");
13058 vec_free (parser->cilk_simd_fn_tokens);
13059 return;
13062 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
13063 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
13064 has already processed the tokens. */
13065 if (clauses.exists () && clauses[0].type == CPP_EOF)
13066 return;
13067 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
13069 error ("%<#pragma omp declare simd%> not immediately followed by "
13070 "a function declaration or definition");
13071 clauses[0].type = CPP_EOF;
13072 return;
13074 if (clauses.exists () && clauses[0].type != CPP_NAME)
13076 error_at (DECL_SOURCE_LOCATION (fndecl),
13077 "%<#pragma omp declare simd%> not immediately followed by "
13078 "a single function declaration or definition");
13079 clauses[0].type = CPP_EOF;
13080 return;
13083 if (parms == NULL_TREE)
13084 parms = DECL_ARGUMENTS (fndecl);
13086 unsigned int tokens_avail = parser->tokens_avail;
13087 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13088 bool is_cilkplus_cilk_simd_fn = false;
13090 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13092 parser->tokens = parser->cilk_simd_fn_tokens->address ();
13093 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
13094 is_cilkplus_cilk_simd_fn = true;
13096 else
13098 parser->tokens = clauses.address ();
13099 parser->tokens_avail = clauses.length ();
13102 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
13103 while (parser->tokens_avail > 3)
13105 c_token *token = c_parser_peek_token (parser);
13106 if (!is_cilkplus_cilk_simd_fn)
13107 gcc_assert (token->type == CPP_NAME
13108 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
13109 else
13110 gcc_assert (token->type == CPP_NAME
13111 && is_cilkplus_vector_p (token->value));
13112 c_parser_consume_token (parser);
13113 parser->in_pragma = true;
13115 tree c = NULL_TREE;
13116 if (is_cilkplus_cilk_simd_fn)
13117 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
13118 "SIMD-enabled functions attribute");
13119 else
13120 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
13121 "#pragma omp declare simd");
13122 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
13123 if (c != NULL_TREE)
13124 c = tree_cons (NULL_TREE, c, NULL_TREE);
13125 if (is_cilkplus_cilk_simd_fn)
13127 tree k = build_tree_list (get_identifier ("cilk simd function"),
13128 NULL_TREE);
13129 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
13130 DECL_ATTRIBUTES (fndecl) = k;
13132 c = build_tree_list (get_identifier ("omp declare simd"), c);
13133 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
13134 DECL_ATTRIBUTES (fndecl) = c;
13137 parser->tokens = &parser->tokens_buf[0];
13138 parser->tokens_avail = tokens_avail;
13139 if (clauses.exists ())
13140 clauses[0].type = CPP_PRAGMA;
13142 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13143 vec_free (parser->cilk_simd_fn_tokens);
13147 /* OpenMP 4.0:
13148 # pragma omp declare target new-line
13149 declarations and definitions
13150 # pragma omp end declare target new-line */
13152 static void
13153 c_parser_omp_declare_target (c_parser *parser)
13155 c_parser_skip_to_pragma_eol (parser);
13156 current_omp_declare_target_attribute++;
13159 static void
13160 c_parser_omp_end_declare_target (c_parser *parser)
13162 location_t loc = c_parser_peek_token (parser)->location;
13163 c_parser_consume_pragma (parser);
13164 if (c_parser_next_token_is (parser, CPP_NAME)
13165 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13166 "declare") == 0)
13168 c_parser_consume_token (parser);
13169 if (c_parser_next_token_is (parser, CPP_NAME)
13170 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13171 "target") == 0)
13172 c_parser_consume_token (parser);
13173 else
13175 c_parser_error (parser, "expected %<target%>");
13176 c_parser_skip_to_pragma_eol (parser);
13177 return;
13180 else
13182 c_parser_error (parser, "expected %<declare%>");
13183 c_parser_skip_to_pragma_eol (parser);
13184 return;
13186 c_parser_skip_to_pragma_eol (parser);
13187 if (!current_omp_declare_target_attribute)
13188 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13189 "%<#pragma omp declare target%>");
13190 else
13191 current_omp_declare_target_attribute--;
13195 /* OpenMP 4.0
13196 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13197 initializer-clause[opt] new-line
13199 initializer-clause:
13200 initializer (omp_priv = initializer)
13201 initializer (function-name (argument-list)) */
13203 static void
13204 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13206 unsigned int tokens_avail = 0, i;
13207 vec<tree> types = vNULL;
13208 vec<c_token> clauses = vNULL;
13209 enum tree_code reduc_code = ERROR_MARK;
13210 tree reduc_id = NULL_TREE;
13211 tree type;
13212 location_t rloc = c_parser_peek_token (parser)->location;
13214 if (context == pragma_struct || context == pragma_param)
13216 error ("%<#pragma omp declare reduction%> not at file or block scope");
13217 goto fail;
13220 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13221 goto fail;
13223 switch (c_parser_peek_token (parser)->type)
13225 case CPP_PLUS:
13226 reduc_code = PLUS_EXPR;
13227 break;
13228 case CPP_MULT:
13229 reduc_code = MULT_EXPR;
13230 break;
13231 case CPP_MINUS:
13232 reduc_code = MINUS_EXPR;
13233 break;
13234 case CPP_AND:
13235 reduc_code = BIT_AND_EXPR;
13236 break;
13237 case CPP_XOR:
13238 reduc_code = BIT_XOR_EXPR;
13239 break;
13240 case CPP_OR:
13241 reduc_code = BIT_IOR_EXPR;
13242 break;
13243 case CPP_AND_AND:
13244 reduc_code = TRUTH_ANDIF_EXPR;
13245 break;
13246 case CPP_OR_OR:
13247 reduc_code = TRUTH_ORIF_EXPR;
13248 break;
13249 case CPP_NAME:
13250 const char *p;
13251 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13252 if (strcmp (p, "min") == 0)
13254 reduc_code = MIN_EXPR;
13255 break;
13257 if (strcmp (p, "max") == 0)
13259 reduc_code = MAX_EXPR;
13260 break;
13262 reduc_id = c_parser_peek_token (parser)->value;
13263 break;
13264 default:
13265 c_parser_error (parser,
13266 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13267 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13268 goto fail;
13271 tree orig_reduc_id, reduc_decl;
13272 orig_reduc_id = reduc_id;
13273 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13274 reduc_decl = c_omp_reduction_decl (reduc_id);
13275 c_parser_consume_token (parser);
13277 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13278 goto fail;
13280 while (true)
13282 location_t loc = c_parser_peek_token (parser)->location;
13283 struct c_type_name *ctype = c_parser_type_name (parser);
13284 if (ctype != NULL)
13286 type = groktypename (ctype, NULL, NULL);
13287 if (type == error_mark_node)
13289 else if ((INTEGRAL_TYPE_P (type)
13290 || TREE_CODE (type) == REAL_TYPE
13291 || TREE_CODE (type) == COMPLEX_TYPE)
13292 && orig_reduc_id == NULL_TREE)
13293 error_at (loc, "predeclared arithmetic type in "
13294 "%<#pragma omp declare reduction%>");
13295 else if (TREE_CODE (type) == FUNCTION_TYPE
13296 || TREE_CODE (type) == ARRAY_TYPE)
13297 error_at (loc, "function or array type in "
13298 "%<#pragma omp declare reduction%>");
13299 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13300 error_at (loc, "const, volatile or restrict qualified type in "
13301 "%<#pragma omp declare reduction%>");
13302 else
13304 tree t;
13305 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13306 if (comptypes (TREE_PURPOSE (t), type))
13308 error_at (loc, "redeclaration of %qs "
13309 "%<#pragma omp declare reduction%> for "
13310 "type %qT",
13311 IDENTIFIER_POINTER (reduc_id)
13312 + sizeof ("omp declare reduction ") - 1,
13313 type);
13314 location_t ploc
13315 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13316 0));
13317 error_at (ploc, "previous %<#pragma omp declare "
13318 "reduction%>");
13319 break;
13321 if (t == NULL_TREE)
13322 types.safe_push (type);
13324 if (c_parser_next_token_is (parser, CPP_COMMA))
13325 c_parser_consume_token (parser);
13326 else
13327 break;
13329 else
13330 break;
13333 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13334 || types.is_empty ())
13336 fail:
13337 clauses.release ();
13338 types.release ();
13339 while (true)
13341 c_token *token = c_parser_peek_token (parser);
13342 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13343 break;
13344 c_parser_consume_token (parser);
13346 c_parser_skip_to_pragma_eol (parser);
13347 return;
13350 if (types.length () > 1)
13352 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13354 c_token *token = c_parser_peek_token (parser);
13355 if (token->type == CPP_EOF)
13356 goto fail;
13357 clauses.safe_push (*token);
13358 c_parser_consume_token (parser);
13360 clauses.safe_push (*c_parser_peek_token (parser));
13361 c_parser_skip_to_pragma_eol (parser);
13363 /* Make sure nothing tries to read past the end of the tokens. */
13364 c_token eof_token;
13365 memset (&eof_token, 0, sizeof (eof_token));
13366 eof_token.type = CPP_EOF;
13367 clauses.safe_push (eof_token);
13368 clauses.safe_push (eof_token);
13371 int errs = errorcount;
13372 FOR_EACH_VEC_ELT (types, i, type)
13374 tokens_avail = parser->tokens_avail;
13375 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13376 if (!clauses.is_empty ())
13378 parser->tokens = clauses.address ();
13379 parser->tokens_avail = clauses.length ();
13380 parser->in_pragma = true;
13383 bool nested = current_function_decl != NULL_TREE;
13384 if (nested)
13385 c_push_function_context ();
13386 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13387 reduc_id, default_function_type);
13388 current_function_decl = fndecl;
13389 allocate_struct_function (fndecl, true);
13390 push_scope ();
13391 tree stmt = push_stmt_list ();
13392 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13393 warn about these. */
13394 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13395 get_identifier ("omp_out"), type);
13396 DECL_ARTIFICIAL (omp_out) = 1;
13397 DECL_CONTEXT (omp_out) = fndecl;
13398 pushdecl (omp_out);
13399 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13400 get_identifier ("omp_in"), type);
13401 DECL_ARTIFICIAL (omp_in) = 1;
13402 DECL_CONTEXT (omp_in) = fndecl;
13403 pushdecl (omp_in);
13404 struct c_expr combiner = c_parser_expression (parser);
13405 struct c_expr initializer;
13406 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13407 bool bad = false;
13408 initializer.value = error_mark_node;
13409 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13410 bad = true;
13411 else if (c_parser_next_token_is (parser, CPP_NAME)
13412 && strcmp (IDENTIFIER_POINTER
13413 (c_parser_peek_token (parser)->value),
13414 "initializer") == 0)
13416 c_parser_consume_token (parser);
13417 pop_scope ();
13418 push_scope ();
13419 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13420 get_identifier ("omp_priv"), type);
13421 DECL_ARTIFICIAL (omp_priv) = 1;
13422 DECL_INITIAL (omp_priv) = error_mark_node;
13423 DECL_CONTEXT (omp_priv) = fndecl;
13424 pushdecl (omp_priv);
13425 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13426 get_identifier ("omp_orig"), type);
13427 DECL_ARTIFICIAL (omp_orig) = 1;
13428 DECL_CONTEXT (omp_orig) = fndecl;
13429 pushdecl (omp_orig);
13430 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13431 bad = true;
13432 else if (!c_parser_next_token_is (parser, CPP_NAME))
13434 c_parser_error (parser, "expected %<omp_priv%> or "
13435 "function-name");
13436 bad = true;
13438 else if (strcmp (IDENTIFIER_POINTER
13439 (c_parser_peek_token (parser)->value),
13440 "omp_priv") != 0)
13442 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13443 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13445 c_parser_error (parser, "expected function-name %<(%>");
13446 bad = true;
13448 else
13449 initializer = c_parser_postfix_expression (parser);
13450 if (initializer.value
13451 && TREE_CODE (initializer.value) == CALL_EXPR)
13453 int j;
13454 tree c = initializer.value;
13455 for (j = 0; j < call_expr_nargs (c); j++)
13456 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13457 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13458 break;
13459 if (j == call_expr_nargs (c))
13460 error ("one of the initializer call arguments should be "
13461 "%<&omp_priv%>");
13464 else
13466 c_parser_consume_token (parser);
13467 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13468 bad = true;
13469 else
13471 tree st = push_stmt_list ();
13472 start_init (omp_priv, NULL_TREE, 0);
13473 location_t loc = c_parser_peek_token (parser)->location;
13474 struct c_expr init = c_parser_initializer (parser);
13475 finish_init ();
13476 finish_decl (omp_priv, loc, init.value,
13477 init.original_type, NULL_TREE);
13478 pop_stmt_list (st);
13481 if (!bad
13482 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13483 bad = true;
13486 if (!bad)
13488 c_parser_skip_to_pragma_eol (parser);
13490 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13491 DECL_INITIAL (reduc_decl));
13492 DECL_INITIAL (reduc_decl) = t;
13493 DECL_SOURCE_LOCATION (omp_out) = rloc;
13494 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13495 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13496 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13497 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13498 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13499 if (omp_priv)
13501 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13502 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13503 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13504 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13505 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13506 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13507 walk_tree (&DECL_INITIAL (omp_priv),
13508 c_check_omp_declare_reduction_r,
13509 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13513 pop_stmt_list (stmt);
13514 pop_scope ();
13515 if (cfun->language != NULL)
13517 ggc_free (cfun->language);
13518 cfun->language = NULL;
13520 set_cfun (NULL);
13521 current_function_decl = NULL_TREE;
13522 if (nested)
13523 c_pop_function_context ();
13525 if (!clauses.is_empty ())
13527 parser->tokens = &parser->tokens_buf[0];
13528 parser->tokens_avail = tokens_avail;
13530 if (bad)
13531 goto fail;
13532 if (errs != errorcount)
13533 break;
13536 clauses.release ();
13537 types.release ();
13541 /* OpenMP 4.0
13542 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13543 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13544 initializer-clause[opt] new-line
13545 #pragma omp declare target new-line */
13547 static void
13548 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13550 c_parser_consume_pragma (parser);
13551 if (c_parser_next_token_is (parser, CPP_NAME))
13553 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13554 if (strcmp (p, "simd") == 0)
13556 /* c_parser_consume_token (parser); done in
13557 c_parser_omp_declare_simd. */
13558 c_parser_omp_declare_simd (parser, context);
13559 return;
13561 if (strcmp (p, "reduction") == 0)
13563 c_parser_consume_token (parser);
13564 c_parser_omp_declare_reduction (parser, context);
13565 return;
13567 if (!flag_openmp) /* flag_openmp_simd */
13569 c_parser_skip_to_pragma_eol (parser);
13570 return;
13572 if (strcmp (p, "target") == 0)
13574 c_parser_consume_token (parser);
13575 c_parser_omp_declare_target (parser);
13576 return;
13580 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13581 "or %<target%>");
13582 c_parser_skip_to_pragma_eol (parser);
13585 /* Main entry point to parsing most OpenMP pragmas. */
13587 static void
13588 c_parser_omp_construct (c_parser *parser)
13590 enum pragma_kind p_kind;
13591 location_t loc;
13592 tree stmt;
13593 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13594 omp_clause_mask mask (0);
13596 loc = c_parser_peek_token (parser)->location;
13597 p_kind = c_parser_peek_token (parser)->pragma_kind;
13598 c_parser_consume_pragma (parser);
13600 switch (p_kind)
13602 case PRAGMA_OMP_ATOMIC:
13603 c_parser_omp_atomic (loc, parser);
13604 return;
13605 case PRAGMA_OMP_CRITICAL:
13606 stmt = c_parser_omp_critical (loc, parser);
13607 break;
13608 case PRAGMA_OMP_DISTRIBUTE:
13609 strcpy (p_name, "#pragma omp");
13610 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13611 break;
13612 case PRAGMA_OMP_FOR:
13613 strcpy (p_name, "#pragma omp");
13614 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13615 break;
13616 case PRAGMA_OMP_MASTER:
13617 stmt = c_parser_omp_master (loc, parser);
13618 break;
13619 case PRAGMA_OMP_ORDERED:
13620 stmt = c_parser_omp_ordered (loc, parser);
13621 break;
13622 case PRAGMA_OMP_PARALLEL:
13623 strcpy (p_name, "#pragma omp");
13624 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13625 break;
13626 case PRAGMA_OMP_SECTIONS:
13627 strcpy (p_name, "#pragma omp");
13628 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13629 break;
13630 case PRAGMA_OMP_SIMD:
13631 strcpy (p_name, "#pragma omp");
13632 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13633 break;
13634 case PRAGMA_OMP_SINGLE:
13635 stmt = c_parser_omp_single (loc, parser);
13636 break;
13637 case PRAGMA_OMP_TASK:
13638 stmt = c_parser_omp_task (loc, parser);
13639 break;
13640 case PRAGMA_OMP_TASKGROUP:
13641 stmt = c_parser_omp_taskgroup (parser);
13642 break;
13643 case PRAGMA_OMP_TEAMS:
13644 strcpy (p_name, "#pragma omp");
13645 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13646 break;
13647 default:
13648 gcc_unreachable ();
13651 if (stmt)
13652 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13656 /* OpenMP 2.5:
13657 # pragma omp threadprivate (variable-list) */
13659 static void
13660 c_parser_omp_threadprivate (c_parser *parser)
13662 tree vars, t;
13663 location_t loc;
13665 c_parser_consume_pragma (parser);
13666 loc = c_parser_peek_token (parser)->location;
13667 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13669 /* Mark every variable in VARS to be assigned thread local storage. */
13670 for (t = vars; t; t = TREE_CHAIN (t))
13672 tree v = TREE_PURPOSE (t);
13674 /* FIXME diagnostics: Ideally we should keep individual
13675 locations for all the variables in the var list to make the
13676 following errors more precise. Perhaps
13677 c_parser_omp_var_list_parens() should construct a list of
13678 locations to go along with the var list. */
13680 /* If V had already been marked threadprivate, it doesn't matter
13681 whether it had been used prior to this point. */
13682 if (TREE_CODE (v) != VAR_DECL)
13683 error_at (loc, "%qD is not a variable", v);
13684 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13685 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13686 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13687 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13688 else if (TREE_TYPE (v) == error_mark_node)
13690 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13691 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13692 else
13694 if (! DECL_THREAD_LOCAL_P (v))
13696 set_decl_tls_model (v, decl_default_tls_model (v));
13697 /* If rtl has been already set for this var, call
13698 make_decl_rtl once again, so that encode_section_info
13699 has a chance to look at the new decl flags. */
13700 if (DECL_RTL_SET_P (v))
13701 make_decl_rtl (v);
13703 C_DECL_THREADPRIVATE_P (v) = 1;
13707 c_parser_skip_to_pragma_eol (parser);
13710 /* Cilk Plus <#pragma simd> parsing routines. */
13712 /* Helper function for c_parser_pragma. Perform some sanity checking
13713 for <#pragma simd> constructs. Returns FALSE if there was a
13714 problem. */
13716 static bool
13717 c_parser_cilk_verify_simd (c_parser *parser,
13718 enum pragma_context context)
13720 if (!flag_cilkplus)
13722 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13723 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13724 return false;
13726 if (context == pragma_external)
13728 c_parser_error (parser,"pragma simd must be inside a function");
13729 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13730 return false;
13732 return true;
13735 /* Cilk Plus:
13736 This function is shared by SIMD-enabled functions and #pragma simd.
13737 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13738 CLAUSES is unused. The main purpose of this function is to parse a
13739 vectorlength attribute or clause and check for parse errors.
13740 When IS_SIMD_FN is true then the function is merely caching the tokens
13741 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13742 cache is cleared since there is no reason to continue.
13743 Syntax:
13744 vectorlength ( constant-expression ) */
13746 static tree
13747 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13748 bool is_simd_fn)
13750 if (is_simd_fn)
13751 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13752 else
13753 /* The vectorlength clause behaves exactly like OpenMP's safelen
13754 clause. Represent it in OpenMP terms. */
13755 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
13757 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13758 return clauses;
13760 location_t loc = c_parser_peek_token (parser)->location;
13761 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13762 expr = c_fully_fold (expr, false, NULL);
13764 /* If expr is an error_mark_node then the above function would have
13765 emitted an error. No reason to do it twice. */
13766 if (expr == error_mark_node)
13768 else if (!TREE_TYPE (expr)
13769 || !TREE_CONSTANT (expr)
13770 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13772 error_at (loc, "vectorlength must be an integer constant");
13773 else if (wi::exact_log2 (expr) == -1)
13774 error_at (loc, "vectorlength must be a power of 2");
13775 else
13777 if (is_simd_fn)
13779 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13780 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13781 OMP_CLAUSE_CHAIN (u) = clauses;
13782 clauses = u;
13784 else
13786 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13787 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13788 OMP_CLAUSE_CHAIN (u) = clauses;
13789 clauses = u;
13793 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13795 return clauses;
13798 /* Cilk Plus:
13799 linear ( simd-linear-variable-list )
13801 simd-linear-variable-list:
13802 simd-linear-variable
13803 simd-linear-variable-list , simd-linear-variable
13805 simd-linear-variable:
13806 id-expression
13807 id-expression : simd-linear-step
13809 simd-linear-step:
13810 conditional-expression */
13812 static tree
13813 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13815 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13816 return clauses;
13818 location_t loc = c_parser_peek_token (parser)->location;
13820 if (c_parser_next_token_is_not (parser, CPP_NAME)
13821 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13822 c_parser_error (parser, "expected identifier");
13824 while (c_parser_next_token_is (parser, CPP_NAME)
13825 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13827 tree var = lookup_name (c_parser_peek_token (parser)->value);
13829 if (var == NULL)
13831 undeclared_variable (c_parser_peek_token (parser)->location,
13832 c_parser_peek_token (parser)->value);
13833 c_parser_consume_token (parser);
13835 else if (var == error_mark_node)
13836 c_parser_consume_token (parser);
13837 else
13839 tree step = integer_one_node;
13841 /* Parse the linear step if present. */
13842 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13844 c_parser_consume_token (parser);
13845 c_parser_consume_token (parser);
13847 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13848 expr = c_fully_fold (expr, false, NULL);
13850 if (TREE_TYPE (expr)
13851 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13852 && (TREE_CONSTANT (expr)
13853 || DECL_P (expr)))
13854 step = expr;
13855 else
13856 c_parser_error (parser,
13857 "step size must be an integer constant "
13858 "expression or an integer variable");
13860 else
13861 c_parser_consume_token (parser);
13863 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13864 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13865 OMP_CLAUSE_DECL (u) = var;
13866 OMP_CLAUSE_LINEAR_STEP (u) = step;
13867 OMP_CLAUSE_CHAIN (u) = clauses;
13868 clauses = u;
13871 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13872 break;
13874 c_parser_consume_token (parser);
13877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13879 return clauses;
13882 /* Returns the name of the next clause. If the clause is not
13883 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13884 not consumed. Otherwise, the appropriate pragma_simd_clause is
13885 returned and the token is consumed. */
13887 static pragma_omp_clause
13888 c_parser_cilk_clause_name (c_parser *parser)
13890 pragma_omp_clause result;
13891 c_token *token = c_parser_peek_token (parser);
13893 if (!token->value || token->type != CPP_NAME)
13894 return PRAGMA_CILK_CLAUSE_NONE;
13896 const char *p = IDENTIFIER_POINTER (token->value);
13898 if (!strcmp (p, "vectorlength"))
13899 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13900 else if (!strcmp (p, "linear"))
13901 result = PRAGMA_CILK_CLAUSE_LINEAR;
13902 else if (!strcmp (p, "private"))
13903 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13904 else if (!strcmp (p, "firstprivate"))
13905 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13906 else if (!strcmp (p, "lastprivate"))
13907 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13908 else if (!strcmp (p, "reduction"))
13909 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13910 else
13911 return PRAGMA_CILK_CLAUSE_NONE;
13913 c_parser_consume_token (parser);
13914 return result;
13917 /* Parse all #<pragma simd> clauses. Return the list of clauses
13918 found. */
13920 static tree
13921 c_parser_cilk_all_clauses (c_parser *parser)
13923 tree clauses = NULL;
13925 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13927 pragma_omp_clause c_kind;
13929 c_kind = c_parser_cilk_clause_name (parser);
13931 switch (c_kind)
13933 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13934 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
13935 break;
13936 case PRAGMA_CILK_CLAUSE_LINEAR:
13937 clauses = c_parser_cilk_clause_linear (parser, clauses);
13938 break;
13939 case PRAGMA_CILK_CLAUSE_PRIVATE:
13940 /* Use the OpenMP counterpart. */
13941 clauses = c_parser_omp_clause_private (parser, clauses);
13942 break;
13943 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13944 /* Use the OpenMP counterpart. */
13945 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13946 break;
13947 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13948 /* Use the OpenMP counterpart. */
13949 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13950 break;
13951 case PRAGMA_CILK_CLAUSE_REDUCTION:
13952 /* Use the OpenMP counterpart. */
13953 clauses = c_parser_omp_clause_reduction (parser, clauses);
13954 break;
13955 default:
13956 c_parser_error (parser, "expected %<#pragma simd%> clause");
13957 goto saw_error;
13961 saw_error:
13962 c_parser_skip_to_pragma_eol (parser);
13963 return c_finish_cilk_clauses (clauses);
13966 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
13967 Here is the correct syntax of this pragma:
13968 #pragma cilk grainsize = <EXP>
13971 static void
13972 c_parser_cilk_grainsize (c_parser *parser)
13974 extern tree convert_to_integer (tree, tree);
13976 /* consume the 'grainsize' keyword. */
13977 c_parser_consume_pragma (parser);
13979 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
13981 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
13982 if (g_expr.value == error_mark_node)
13984 c_parser_skip_to_pragma_eol (parser);
13985 return;
13987 tree grain = convert_to_integer (long_integer_type_node,
13988 c_fully_fold (g_expr.value, false,
13989 NULL));
13990 c_parser_skip_to_pragma_eol (parser);
13991 c_token *token = c_parser_peek_token (parser);
13992 if (token && token->type == CPP_KEYWORD
13993 && token->keyword == RID_CILK_FOR)
13995 if (grain == NULL_TREE || grain == error_mark_node)
13996 grain = integer_zero_node;
13997 c_parser_cilk_for (parser, grain);
13999 else
14000 warning (0, "%<#pragma cilk grainsize%> is not followed by "
14001 "%<_Cilk_for%>");
14003 else
14004 c_parser_skip_to_pragma_eol (parser);
14007 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
14009 static void
14010 c_parser_cilk_simd (c_parser *parser)
14012 tree clauses = c_parser_cilk_all_clauses (parser);
14013 tree block = c_begin_compound_stmt (true);
14014 location_t loc = c_parser_peek_token (parser)->location;
14015 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
14016 block = c_end_compound_stmt (loc, block, true);
14017 add_stmt (block);
14020 /* Create an artificial decl with TYPE and emit initialization of it with
14021 INIT. */
14023 static tree
14024 c_get_temp_regvar (tree type, tree init)
14026 location_t loc = EXPR_LOCATION (init);
14027 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
14028 DECL_ARTIFICIAL (decl) = 1;
14029 DECL_IGNORED_P (decl) = 1;
14030 pushdecl (decl);
14031 tree t = build2 (INIT_EXPR, type, decl, init);
14032 add_stmt (t);
14033 return decl;
14036 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
14037 GRAIN is the grain value passed in through pragma or 0. */
14039 static void
14040 c_parser_cilk_for (c_parser *parser, tree grain)
14042 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
14043 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
14044 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
14045 clauses = c_finish_omp_clauses (clauses);
14047 tree block = c_begin_compound_stmt (true);
14048 tree sb = push_stmt_list ();
14049 location_t loc = c_parser_peek_token (parser)->location;
14050 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
14051 sb = pop_stmt_list (sb);
14053 if (omp_for)
14055 tree omp_par = make_node (OMP_PARALLEL);
14056 TREE_TYPE (omp_par) = void_type_node;
14057 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
14058 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
14059 TREE_SIDE_EFFECTS (bind) = 1;
14060 BIND_EXPR_BODY (bind) = sb;
14061 OMP_PARALLEL_BODY (omp_par) = bind;
14062 if (OMP_FOR_PRE_BODY (omp_for))
14064 add_stmt (OMP_FOR_PRE_BODY (omp_for));
14065 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
14067 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
14068 tree decl = TREE_OPERAND (init, 0);
14069 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
14070 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
14071 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
14072 if (TREE_CODE (t) != INTEGER_CST)
14074 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14075 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14076 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
14077 OMP_CLAUSE_CHAIN (c) = clauses;
14078 clauses = c;
14080 if (TREE_CODE (incr) == MODIFY_EXPR)
14082 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14083 if (TREE_CODE (t) != INTEGER_CST)
14085 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
14086 = c_get_temp_regvar (TREE_TYPE (t), t);
14087 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14088 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14089 OMP_CLAUSE_CHAIN (c) = clauses;
14090 clauses = c;
14093 t = TREE_OPERAND (init, 1);
14094 if (TREE_CODE (t) != INTEGER_CST)
14096 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14097 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14098 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
14099 OMP_CLAUSE_CHAIN (c) = clauses;
14100 clauses = c;
14102 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14103 OMP_CLAUSE_DECL (c) = decl;
14104 OMP_CLAUSE_CHAIN (c) = clauses;
14105 clauses = c;
14106 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
14107 OMP_CLAUSE_OPERAND (c, 0)
14108 = cilk_for_number_of_iterations (omp_for);
14109 OMP_CLAUSE_CHAIN (c) = clauses;
14110 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
14111 add_stmt (omp_par);
14114 block = c_end_compound_stmt (loc, block, true);
14115 add_stmt (block);
14119 /* Parse a transaction attribute (GCC Extension).
14121 transaction-attribute:
14122 attributes
14123 [ [ any-word ] ]
14125 The transactional memory language description is written for C++,
14126 and uses the C++0x attribute syntax. For compatibility, allow the
14127 bracket style for transactions in C as well. */
14129 static tree
14130 c_parser_transaction_attributes (c_parser *parser)
14132 tree attr_name, attr = NULL;
14134 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
14135 return c_parser_attributes (parser);
14137 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
14138 return NULL_TREE;
14139 c_parser_consume_token (parser);
14140 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
14141 goto error1;
14143 attr_name = c_parser_attribute_any_word (parser);
14144 if (attr_name)
14146 c_parser_consume_token (parser);
14147 attr = build_tree_list (attr_name, NULL_TREE);
14149 else
14150 c_parser_error (parser, "expected identifier");
14152 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14153 error1:
14154 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14155 return attr;
14158 /* Parse a __transaction_atomic or __transaction_relaxed statement
14159 (GCC Extension).
14161 transaction-statement:
14162 __transaction_atomic transaction-attribute[opt] compound-statement
14163 __transaction_relaxed compound-statement
14165 Note that the only valid attribute is: "outer".
14168 static tree
14169 c_parser_transaction (c_parser *parser, enum rid keyword)
14171 unsigned int old_in = parser->in_transaction;
14172 unsigned int this_in = 1, new_in;
14173 location_t loc = c_parser_peek_token (parser)->location;
14174 tree stmt, attrs;
14176 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14177 || keyword == RID_TRANSACTION_RELAXED)
14178 && c_parser_next_token_is_keyword (parser, keyword));
14179 c_parser_consume_token (parser);
14181 if (keyword == RID_TRANSACTION_RELAXED)
14182 this_in |= TM_STMT_ATTR_RELAXED;
14183 else
14185 attrs = c_parser_transaction_attributes (parser);
14186 if (attrs)
14187 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
14190 /* Keep track if we're in the lexical scope of an outer transaction. */
14191 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
14193 parser->in_transaction = new_in;
14194 stmt = c_parser_compound_statement (parser);
14195 parser->in_transaction = old_in;
14197 if (flag_tm)
14198 stmt = c_finish_transaction (loc, stmt, this_in);
14199 else
14200 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
14201 "%<__transaction_atomic%> without transactional memory support enabled"
14202 : "%<__transaction_relaxed %> "
14203 "without transactional memory support enabled"));
14205 return stmt;
14208 /* Parse a __transaction_atomic or __transaction_relaxed expression
14209 (GCC Extension).
14211 transaction-expression:
14212 __transaction_atomic ( expression )
14213 __transaction_relaxed ( expression )
14216 static struct c_expr
14217 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
14219 struct c_expr ret;
14220 unsigned int old_in = parser->in_transaction;
14221 unsigned int this_in = 1;
14222 location_t loc = c_parser_peek_token (parser)->location;
14223 tree attrs;
14225 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
14226 || keyword == RID_TRANSACTION_RELAXED)
14227 && c_parser_next_token_is_keyword (parser, keyword));
14228 c_parser_consume_token (parser);
14230 if (keyword == RID_TRANSACTION_RELAXED)
14231 this_in |= TM_STMT_ATTR_RELAXED;
14232 else
14234 attrs = c_parser_transaction_attributes (parser);
14235 if (attrs)
14236 this_in |= parse_tm_stmt_attr (attrs, 0);
14239 parser->in_transaction = this_in;
14240 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14242 tree expr = c_parser_expression (parser).value;
14243 ret.original_type = TREE_TYPE (expr);
14244 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
14245 if (this_in & TM_STMT_ATTR_RELAXED)
14246 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
14247 SET_EXPR_LOCATION (ret.value, loc);
14248 ret.original_code = TRANSACTION_EXPR;
14249 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
14252 goto error;
14255 else
14257 error:
14258 ret.value = error_mark_node;
14259 ret.original_code = ERROR_MARK;
14260 ret.original_type = NULL;
14262 parser->in_transaction = old_in;
14264 if (!flag_tm)
14265 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
14266 "%<__transaction_atomic%> without transactional memory support enabled"
14267 : "%<__transaction_relaxed %> "
14268 "without transactional memory support enabled"));
14270 return ret;
14273 /* Parse a __transaction_cancel statement (GCC Extension).
14275 transaction-cancel-statement:
14276 __transaction_cancel transaction-attribute[opt] ;
14278 Note that the only valid attribute is "outer".
14281 static tree
14282 c_parser_transaction_cancel (c_parser *parser)
14284 location_t loc = c_parser_peek_token (parser)->location;
14285 tree attrs;
14286 bool is_outer = false;
14288 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
14289 c_parser_consume_token (parser);
14291 attrs = c_parser_transaction_attributes (parser);
14292 if (attrs)
14293 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
14295 if (!flag_tm)
14297 error_at (loc, "%<__transaction_cancel%> without "
14298 "transactional memory support enabled");
14299 goto ret_error;
14301 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
14303 error_at (loc, "%<__transaction_cancel%> within a "
14304 "%<__transaction_relaxed%>");
14305 goto ret_error;
14307 else if (is_outer)
14309 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14310 && !is_tm_may_cancel_outer (current_function_decl))
14312 error_at (loc, "outer %<__transaction_cancel%> not "
14313 "within outer %<__transaction_atomic%>");
14314 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14315 goto ret_error;
14318 else if (parser->in_transaction == 0)
14320 error_at (loc, "%<__transaction_cancel%> not within "
14321 "%<__transaction_atomic%>");
14322 goto ret_error;
14325 return add_stmt (build_tm_abort_call (loc, is_outer));
14327 ret_error:
14328 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14331 /* Parse a single source file. */
14333 void
14334 c_parse_file (void)
14336 /* Use local storage to begin. If the first token is a pragma, parse it.
14337 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14338 which will cause garbage collection. */
14339 c_parser tparser;
14341 memset (&tparser, 0, sizeof tparser);
14342 tparser.tokens = &tparser.tokens_buf[0];
14343 the_parser = &tparser;
14345 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14346 c_parser_pragma_pch_preprocess (&tparser);
14348 the_parser = ggc_alloc<c_parser> ();
14349 *the_parser = tparser;
14350 if (tparser.tokens == &tparser.tokens_buf[0])
14351 the_parser->tokens = &the_parser->tokens_buf[0];
14353 /* Initialize EH, if we've been told to do so. */
14354 if (flag_exceptions)
14355 using_eh_for_cleanups ();
14357 c_parser_translation_unit (the_parser);
14358 the_parser = NULL;
14361 /* This function parses Cilk Plus array notation. The starting index is
14362 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14363 return value of this function is a tree_node called VALUE_TREE of type
14364 ARRAY_NOTATION_REF. */
14366 static tree
14367 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14368 tree array_value)
14370 c_token *token = NULL;
14371 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14372 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14373 tree array_type_domain = NULL_TREE;
14375 if (array_value == error_mark_node || initial_index == error_mark_node)
14377 /* No need to continue. If either of these 2 were true, then an error
14378 must be emitted already. Thus, no need to emit them twice. */
14379 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14380 return error_mark_node;
14383 array_type = TREE_TYPE (array_value);
14384 gcc_assert (array_type);
14385 if (TREE_CODE (array_type) != ARRAY_TYPE
14386 && TREE_CODE (array_type) != POINTER_TYPE)
14388 error_at (loc, "base of array section must be pointer or array type");
14389 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14390 return error_mark_node;
14392 type = TREE_TYPE (array_type);
14393 token = c_parser_peek_token (parser);
14395 if (token->type == CPP_EOF)
14397 c_parser_error (parser, "expected %<:%> or numeral");
14398 return value_tree;
14400 else if (token->type == CPP_COLON)
14402 if (!initial_index)
14404 /* If we are here, then we have a case like this A[:]. */
14405 c_parser_consume_token (parser);
14406 if (TREE_CODE (array_type) == POINTER_TYPE)
14408 error_at (loc, "start-index and length fields necessary for "
14409 "using array notations in pointers");
14410 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14411 return error_mark_node;
14413 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14415 error_at (loc, "array notations cannot be used with function "
14416 "type");
14417 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14418 return error_mark_node;
14420 array_type_domain = TYPE_DOMAIN (array_type);
14422 if (!array_type_domain)
14424 error_at (loc, "start-index and length fields necessary for "
14425 "using array notations in dimensionless arrays");
14426 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14427 return error_mark_node;
14430 start_index = TYPE_MINVAL (array_type_domain);
14431 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14432 start_index);
14433 if (!TYPE_MAXVAL (array_type_domain)
14434 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14436 error_at (loc, "start-index and length fields necessary for "
14437 "using array notations in variable-length arrays");
14438 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14439 return error_mark_node;
14441 end_index = TYPE_MAXVAL (array_type_domain);
14442 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14443 end_index, integer_one_node);
14444 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14445 stride = build_int_cst (integer_type_node, 1);
14446 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14448 else if (initial_index != error_mark_node)
14450 /* If we are here, then there should be 2 possibilities:
14451 1. Array [EXPR : EXPR]
14452 2. Array [EXPR : EXPR : EXPR]
14454 start_index = initial_index;
14456 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14458 error_at (loc, "array notations cannot be used with function "
14459 "type");
14460 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14461 return error_mark_node;
14463 c_parser_consume_token (parser); /* consume the ':' */
14464 struct c_expr ce = c_parser_expression (parser);
14465 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14466 end_index = ce.value;
14467 if (!end_index || end_index == error_mark_node)
14469 c_parser_skip_to_end_of_block_or_statement (parser);
14470 return error_mark_node;
14472 if (c_parser_peek_token (parser)->type == CPP_COLON)
14474 c_parser_consume_token (parser);
14475 ce = c_parser_expression (parser);
14476 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14477 stride = ce.value;
14478 if (!stride || stride == error_mark_node)
14480 c_parser_skip_to_end_of_block_or_statement (parser);
14481 return error_mark_node;
14485 else
14486 c_parser_error (parser, "expected array notation expression");
14488 else
14489 c_parser_error (parser, "expected array notation expression");
14491 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14493 value_tree = build_array_notation_ref (loc, array_value, start_index,
14494 end_index, stride, type);
14495 if (value_tree != error_mark_node)
14496 SET_EXPR_LOCATION (value_tree, loc);
14497 return value_tree;
14500 #include "gt-c-c-parser.h"