2015-05-22 Pascal Obry <obry@adacore.com>
[official-gcc.git] / gcc / c / c-parser.c
blob965b4b9e5f3c8bbad1e5f8beec7c90edc85315ff
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2015 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 "hash-set.h"
43 #include "vec.h"
44 #include "symtab.h"
45 #include "input.h"
46 #include "alias.h"
47 #include "double-int.h"
48 #include "machmode.h"
49 #include "flags.h"
50 #include "inchash.h"
51 #include "tree.h"
52 #include "fold-const.h"
53 #include "stringpool.h"
54 #include "attribs.h"
55 #include "stor-layout.h"
56 #include "varasm.h"
57 #include "trans-mem.h"
58 #include "langhooks.h"
59 #include "input.h"
60 #include "cpplib.h"
61 #include "timevar.h"
62 #include "c-family/c-pragma.h"
63 #include "c-tree.h"
64 #include "c-lang.h"
65 #include "flags.h"
66 #include "ggc.h"
67 #include "c-family/c-common.h"
68 #include "c-family/c-objc.h"
69 #include "vec.h"
70 #include "target.h"
71 #include "hash-map.h"
72 #include "is-a.h"
73 #include "plugin-api.h"
74 #include "hashtab.h"
75 #include "hash-set.h"
76 #include "machmode.h"
77 #include "hard-reg-set.h"
78 #include "function.h"
79 #include "ipa-ref.h"
80 #include "cgraph.h"
81 #include "plugin.h"
82 #include "omp-low.h"
83 #include "builtins.h"
84 #include "gomp-constants.h"
87 /* Initialization routine for this file. */
89 void
90 c_parse_init (void)
92 /* The only initialization required is of the reserved word
93 identifiers. */
94 unsigned int i;
95 tree id;
96 int mask = 0;
98 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
99 the c_token structure. */
100 gcc_assert (RID_MAX <= 255);
102 mask |= D_CXXONLY;
103 if (!flag_isoc99)
104 mask |= D_C99;
105 if (flag_no_asm)
107 mask |= D_ASM | D_EXT;
108 if (!flag_isoc99)
109 mask |= D_EXT89;
111 if (!c_dialect_objc ())
112 mask |= D_OBJC | D_CXX_OBJC;
114 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
115 for (i = 0; i < num_c_common_reswords; i++)
117 /* If a keyword is disabled, do not enter it into the table
118 and so create a canonical spelling that isn't a keyword. */
119 if (c_common_reswords[i].disable & mask)
121 if (warn_cxx_compat
122 && (c_common_reswords[i].disable & D_CXXWARN))
124 id = get_identifier (c_common_reswords[i].word);
125 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
126 C_IS_RESERVED_WORD (id) = 1;
128 continue;
131 id = get_identifier (c_common_reswords[i].word);
132 C_SET_RID_CODE (id, c_common_reswords[i].rid);
133 C_IS_RESERVED_WORD (id) = 1;
134 ridpointers [(int) c_common_reswords[i].rid] = id;
137 for (i = 0; i < NUM_INT_N_ENTS; i++)
139 /* We always create the symbols but they aren't always supported. */
140 char name[50];
141 sprintf (name, "__int%d", int_n_data[i].bitsize);
142 id = get_identifier (name);
143 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
144 C_IS_RESERVED_WORD (id) = 1;
148 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
149 and the C parser. Unlike the C++ lexer, the parser structure
150 stores the lexer information instead of using a separate structure.
151 Identifiers are separated into ordinary identifiers, type names,
152 keywords and some other Objective-C types of identifiers, and some
153 look-ahead is maintained.
155 ??? It might be a good idea to lex the whole file up front (as for
156 C++). It would then be possible to share more of the C and C++
157 lexer code, if desired. */
159 /* More information about the type of a CPP_NAME token. */
160 typedef enum c_id_kind {
161 /* An ordinary identifier. */
162 C_ID_ID,
163 /* An identifier declared as a typedef name. */
164 C_ID_TYPENAME,
165 /* An identifier declared as an Objective-C class name. */
166 C_ID_CLASSNAME,
167 /* An address space identifier. */
168 C_ID_ADDRSPACE,
169 /* Not an identifier. */
170 C_ID_NONE
171 } c_id_kind;
173 /* A single C token after string literal concatenation and conversion
174 of preprocessing tokens to tokens. */
175 typedef struct GTY (()) c_token {
176 /* The kind of token. */
177 ENUM_BITFIELD (cpp_ttype) type : 8;
178 /* If this token is a CPP_NAME, this value indicates whether also
179 declared as some kind of type. Otherwise, it is C_ID_NONE. */
180 ENUM_BITFIELD (c_id_kind) id_kind : 8;
181 /* If this token is a keyword, this value indicates which keyword.
182 Otherwise, this value is RID_MAX. */
183 ENUM_BITFIELD (rid) keyword : 8;
184 /* If this token is a CPP_PRAGMA, this indicates the pragma that
185 was seen. Otherwise it is PRAGMA_NONE. */
186 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
187 /* The location at which this token was found. */
188 location_t location;
189 /* The value associated with this token, if any. */
190 tree value;
191 } c_token;
193 /* A parser structure recording information about the state and
194 context of parsing. Includes lexer information with up to two
195 tokens of look-ahead; more are not needed for C. */
196 typedef struct GTY(()) c_parser {
197 /* The look-ahead tokens. */
198 c_token * GTY((skip)) tokens;
199 /* Buffer for look-ahead tokens. */
200 c_token tokens_buf[2];
201 /* How many look-ahead tokens are available (0, 1 or 2, or
202 more if parsing from pre-lexed tokens). */
203 unsigned int tokens_avail;
204 /* True if a syntax error is being recovered from; false otherwise.
205 c_parser_error sets this flag. It should clear this flag when
206 enough tokens have been consumed to recover from the error. */
207 BOOL_BITFIELD error : 1;
208 /* True if we're processing a pragma, and shouldn't automatically
209 consume CPP_PRAGMA_EOL. */
210 BOOL_BITFIELD in_pragma : 1;
211 /* True if we're parsing the outermost block of an if statement. */
212 BOOL_BITFIELD in_if_block : 1;
213 /* True if we want to lex an untranslated string. */
214 BOOL_BITFIELD lex_untranslated_string : 1;
216 /* Objective-C specific parser/lexer information. */
218 /* True if we are in a context where the Objective-C "PQ" keywords
219 are considered keywords. */
220 BOOL_BITFIELD objc_pq_context : 1;
221 /* True if we are parsing a (potential) Objective-C foreach
222 statement. This is set to true after we parsed 'for (' and while
223 we wait for 'in' or ';' to decide if it's a standard C for loop or an
224 Objective-C foreach loop. */
225 BOOL_BITFIELD objc_could_be_foreach_context : 1;
226 /* The following flag is needed to contextualize Objective-C lexical
227 analysis. In some cases (e.g., 'int NSObject;'), it is
228 undesirable to bind an identifier to an Objective-C class, even
229 if a class with that name exists. */
230 BOOL_BITFIELD objc_need_raw_identifier : 1;
231 /* Nonzero if we're processing a __transaction statement. The value
232 is 1 | TM_STMT_ATTR_*. */
233 unsigned int in_transaction : 4;
234 /* True if we are in a context where the Objective-C "Property attribute"
235 keywords are valid. */
236 BOOL_BITFIELD objc_property_attr_context : 1;
238 /* Cilk Plus specific parser/lexer information. */
240 /* Buffer to hold all the tokens from parsing the vector attribute for the
241 SIMD-enabled functions (formerly known as elemental functions). */
242 vec <c_token, va_gc> *cilk_simd_fn_tokens;
243 } c_parser;
246 /* The actual parser and external interface. ??? Does this need to be
247 garbage-collected? */
249 static GTY (()) c_parser *the_parser;
251 /* Read in and lex a single token, storing it in *TOKEN. */
253 static void
254 c_lex_one_token (c_parser *parser, c_token *token)
256 timevar_push (TV_LEX);
258 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
259 (parser->lex_untranslated_string
260 ? C_LEX_STRING_NO_TRANSLATE : 0));
261 token->id_kind = C_ID_NONE;
262 token->keyword = RID_MAX;
263 token->pragma_kind = PRAGMA_NONE;
265 switch (token->type)
267 case CPP_NAME:
269 tree decl;
271 bool objc_force_identifier = parser->objc_need_raw_identifier;
272 if (c_dialect_objc ())
273 parser->objc_need_raw_identifier = false;
275 if (C_IS_RESERVED_WORD (token->value))
277 enum rid rid_code = C_RID_CODE (token->value);
279 if (rid_code == RID_CXX_COMPAT_WARN)
281 warning_at (token->location,
282 OPT_Wc___compat,
283 "identifier %qE conflicts with C++ keyword",
284 token->value);
286 else if (rid_code >= RID_FIRST_ADDR_SPACE
287 && rid_code <= RID_LAST_ADDR_SPACE)
289 token->id_kind = C_ID_ADDRSPACE;
290 token->keyword = rid_code;
291 break;
293 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
295 /* We found an Objective-C "pq" keyword (in, out,
296 inout, bycopy, byref, oneway). They need special
297 care because the interpretation depends on the
298 context. */
299 if (parser->objc_pq_context)
301 token->type = CPP_KEYWORD;
302 token->keyword = rid_code;
303 break;
305 else if (parser->objc_could_be_foreach_context
306 && rid_code == RID_IN)
308 /* We are in Objective-C, inside a (potential)
309 foreach context (which means after having
310 parsed 'for (', but before having parsed ';'),
311 and we found 'in'. We consider it the keyword
312 which terminates the declaration at the
313 beginning of a foreach-statement. Note that
314 this means you can't use 'in' for anything else
315 in that context; in particular, in Objective-C
316 you can't use 'in' as the name of the running
317 variable in a C for loop. We could potentially
318 try to add code here to disambiguate, but it
319 seems a reasonable limitation. */
320 token->type = CPP_KEYWORD;
321 token->keyword = rid_code;
322 break;
324 /* Else, "pq" keywords outside of the "pq" context are
325 not keywords, and we fall through to the code for
326 normal tokens. */
328 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
330 /* We found an Objective-C "property attribute"
331 keyword (getter, setter, readonly, etc). These are
332 only valid in the property context. */
333 if (parser->objc_property_attr_context)
335 token->type = CPP_KEYWORD;
336 token->keyword = rid_code;
337 break;
339 /* Else they are not special keywords.
342 else if (c_dialect_objc ()
343 && (OBJC_IS_AT_KEYWORD (rid_code)
344 || OBJC_IS_CXX_KEYWORD (rid_code)))
346 /* We found one of the Objective-C "@" keywords (defs,
347 selector, synchronized, etc) or one of the
348 Objective-C "cxx" keywords (class, private,
349 protected, public, try, catch, throw) without a
350 preceding '@' sign. Do nothing and fall through to
351 the code for normal tokens (in C++ we would still
352 consider the CXX ones keywords, but not in C). */
355 else
357 token->type = CPP_KEYWORD;
358 token->keyword = rid_code;
359 break;
363 decl = lookup_name (token->value);
364 if (decl)
366 if (TREE_CODE (decl) == TYPE_DECL)
368 token->id_kind = C_ID_TYPENAME;
369 break;
372 else if (c_dialect_objc ())
374 tree objc_interface_decl = objc_is_class_name (token->value);
375 /* Objective-C class names are in the same namespace as
376 variables and typedefs, and hence are shadowed by local
377 declarations. */
378 if (objc_interface_decl
379 && (!objc_force_identifier || global_bindings_p ()))
381 token->value = objc_interface_decl;
382 token->id_kind = C_ID_CLASSNAME;
383 break;
386 token->id_kind = C_ID_ID;
388 break;
389 case CPP_AT_NAME:
390 /* This only happens in Objective-C; it must be a keyword. */
391 token->type = CPP_KEYWORD;
392 switch (C_RID_CODE (token->value))
394 /* Replace 'class' with '@class', 'private' with '@private',
395 etc. This prevents confusion with the C++ keyword
396 'class', and makes the tokens consistent with other
397 Objective-C 'AT' keywords. For example '@class' is
398 reported as RID_AT_CLASS which is consistent with
399 '@synchronized', which is reported as
400 RID_AT_SYNCHRONIZED.
402 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
403 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
404 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
405 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
406 case RID_THROW: token->keyword = RID_AT_THROW; break;
407 case RID_TRY: token->keyword = RID_AT_TRY; break;
408 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
409 default: token->keyword = C_RID_CODE (token->value);
411 break;
412 case CPP_COLON:
413 case CPP_COMMA:
414 case CPP_CLOSE_PAREN:
415 case CPP_SEMICOLON:
416 /* These tokens may affect the interpretation of any identifiers
417 following, if doing Objective-C. */
418 if (c_dialect_objc ())
419 parser->objc_need_raw_identifier = false;
420 break;
421 case CPP_PRAGMA:
422 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
423 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
424 token->value = NULL;
425 break;
426 default:
427 break;
429 timevar_pop (TV_LEX);
432 /* Return a pointer to the next token from PARSER, reading it in if
433 necessary. */
435 static inline c_token *
436 c_parser_peek_token (c_parser *parser)
438 if (parser->tokens_avail == 0)
440 c_lex_one_token (parser, &parser->tokens[0]);
441 parser->tokens_avail = 1;
443 return &parser->tokens[0];
446 /* Return true if the next token from PARSER has the indicated
447 TYPE. */
449 static inline bool
450 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
452 return c_parser_peek_token (parser)->type == type;
455 /* Return true if the next token from PARSER does not have the
456 indicated TYPE. */
458 static inline bool
459 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
461 return !c_parser_next_token_is (parser, type);
464 /* Return true if the next token from PARSER is the indicated
465 KEYWORD. */
467 static inline bool
468 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
470 return c_parser_peek_token (parser)->keyword == keyword;
473 /* Return a pointer to the next-but-one token from PARSER, reading it
474 in if necessary. The next token is already read in. */
476 static c_token *
477 c_parser_peek_2nd_token (c_parser *parser)
479 if (parser->tokens_avail >= 2)
480 return &parser->tokens[1];
481 gcc_assert (parser->tokens_avail == 1);
482 gcc_assert (parser->tokens[0].type != CPP_EOF);
483 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
484 c_lex_one_token (parser, &parser->tokens[1]);
485 parser->tokens_avail = 2;
486 return &parser->tokens[1];
489 /* Return true if TOKEN can start a type name,
490 false otherwise. */
491 static bool
492 c_token_starts_typename (c_token *token)
494 switch (token->type)
496 case CPP_NAME:
497 switch (token->id_kind)
499 case C_ID_ID:
500 return false;
501 case C_ID_ADDRSPACE:
502 return true;
503 case C_ID_TYPENAME:
504 return true;
505 case C_ID_CLASSNAME:
506 gcc_assert (c_dialect_objc ());
507 return true;
508 default:
509 gcc_unreachable ();
511 case CPP_KEYWORD:
512 switch (token->keyword)
514 case RID_UNSIGNED:
515 case RID_LONG:
516 case RID_SHORT:
517 case RID_SIGNED:
518 case RID_COMPLEX:
519 case RID_INT:
520 case RID_CHAR:
521 case RID_FLOAT:
522 case RID_DOUBLE:
523 case RID_VOID:
524 case RID_DFLOAT32:
525 case RID_DFLOAT64:
526 case RID_DFLOAT128:
527 case RID_BOOL:
528 case RID_ENUM:
529 case RID_STRUCT:
530 case RID_UNION:
531 case RID_TYPEOF:
532 case RID_CONST:
533 case RID_ATOMIC:
534 case RID_VOLATILE:
535 case RID_RESTRICT:
536 case RID_ATTRIBUTE:
537 case RID_FRACT:
538 case RID_ACCUM:
539 case RID_SAT:
540 case RID_AUTO_TYPE:
541 return true;
542 default:
543 if (token->keyword >= RID_FIRST_INT_N
544 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
545 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
546 return true;
547 return false;
549 case CPP_LESS:
550 if (c_dialect_objc ())
551 return true;
552 return false;
553 default:
554 return false;
558 enum c_lookahead_kind {
559 /* Always treat unknown identifiers as typenames. */
560 cla_prefer_type,
562 /* Could be parsing a nonabstract declarator. Only treat an identifier
563 as a typename if followed by another identifier or a star. */
564 cla_nonabstract_decl,
566 /* Never treat identifiers as typenames. */
567 cla_prefer_id
570 /* Return true if the next token from PARSER can start a type name,
571 false otherwise. LA specifies how to do lookahead in order to
572 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
574 static inline bool
575 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
577 c_token *token = c_parser_peek_token (parser);
578 if (c_token_starts_typename (token))
579 return true;
581 /* Try a bit harder to detect an unknown typename. */
582 if (la != cla_prefer_id
583 && token->type == CPP_NAME
584 && token->id_kind == C_ID_ID
586 /* Do not try too hard when we could have "object in array". */
587 && !parser->objc_could_be_foreach_context
589 && (la == cla_prefer_type
590 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
591 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
593 /* Only unknown identifiers. */
594 && !lookup_name (token->value))
595 return true;
597 return false;
600 /* Return true if TOKEN is a type qualifier, false otherwise. */
601 static bool
602 c_token_is_qualifier (c_token *token)
604 switch (token->type)
606 case CPP_NAME:
607 switch (token->id_kind)
609 case C_ID_ADDRSPACE:
610 return true;
611 default:
612 return false;
614 case CPP_KEYWORD:
615 switch (token->keyword)
617 case RID_CONST:
618 case RID_VOLATILE:
619 case RID_RESTRICT:
620 case RID_ATTRIBUTE:
621 case RID_ATOMIC:
622 return true;
623 default:
624 return false;
626 case CPP_LESS:
627 return false;
628 default:
629 gcc_unreachable ();
633 /* Return true if the next token from PARSER is a type qualifier,
634 false otherwise. */
635 static inline bool
636 c_parser_next_token_is_qualifier (c_parser *parser)
638 c_token *token = c_parser_peek_token (parser);
639 return c_token_is_qualifier (token);
642 /* Return true if TOKEN can start declaration specifiers, false
643 otherwise. */
644 static bool
645 c_token_starts_declspecs (c_token *token)
647 switch (token->type)
649 case CPP_NAME:
650 switch (token->id_kind)
652 case C_ID_ID:
653 return false;
654 case C_ID_ADDRSPACE:
655 return true;
656 case C_ID_TYPENAME:
657 return true;
658 case C_ID_CLASSNAME:
659 gcc_assert (c_dialect_objc ());
660 return true;
661 default:
662 gcc_unreachable ();
664 case CPP_KEYWORD:
665 switch (token->keyword)
667 case RID_STATIC:
668 case RID_EXTERN:
669 case RID_REGISTER:
670 case RID_TYPEDEF:
671 case RID_INLINE:
672 case RID_NORETURN:
673 case RID_AUTO:
674 case RID_THREAD:
675 case RID_UNSIGNED:
676 case RID_LONG:
677 case RID_SHORT:
678 case RID_SIGNED:
679 case RID_COMPLEX:
680 case RID_INT:
681 case RID_CHAR:
682 case RID_FLOAT:
683 case RID_DOUBLE:
684 case RID_VOID:
685 case RID_DFLOAT32:
686 case RID_DFLOAT64:
687 case RID_DFLOAT128:
688 case RID_BOOL:
689 case RID_ENUM:
690 case RID_STRUCT:
691 case RID_UNION:
692 case RID_TYPEOF:
693 case RID_CONST:
694 case RID_VOLATILE:
695 case RID_RESTRICT:
696 case RID_ATTRIBUTE:
697 case RID_FRACT:
698 case RID_ACCUM:
699 case RID_SAT:
700 case RID_ALIGNAS:
701 case RID_ATOMIC:
702 case RID_AUTO_TYPE:
703 return true;
704 default:
705 if (token->keyword >= RID_FIRST_INT_N
706 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
707 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
708 return true;
709 return false;
711 case CPP_LESS:
712 if (c_dialect_objc ())
713 return true;
714 return false;
715 default:
716 return false;
721 /* Return true if TOKEN can start declaration specifiers or a static
722 assertion, false otherwise. */
723 static bool
724 c_token_starts_declaration (c_token *token)
726 if (c_token_starts_declspecs (token)
727 || token->keyword == RID_STATIC_ASSERT)
728 return true;
729 else
730 return false;
733 /* Return true if the next token from PARSER can start declaration
734 specifiers, false otherwise. */
735 static inline bool
736 c_parser_next_token_starts_declspecs (c_parser *parser)
738 c_token *token = c_parser_peek_token (parser);
740 /* In Objective-C, a classname normally starts a declspecs unless it
741 is immediately followed by a dot. In that case, it is the
742 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
743 setter/getter on the class. c_token_starts_declspecs() can't
744 differentiate between the two cases because it only checks the
745 current token, so we have a special check here. */
746 if (c_dialect_objc ()
747 && token->type == CPP_NAME
748 && token->id_kind == C_ID_CLASSNAME
749 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
750 return false;
752 return c_token_starts_declspecs (token);
755 /* Return true if the next tokens from PARSER can start declaration
756 specifiers or a static assertion, false otherwise. */
757 static inline bool
758 c_parser_next_tokens_start_declaration (c_parser *parser)
760 c_token *token = c_parser_peek_token (parser);
762 /* Same as above. */
763 if (c_dialect_objc ()
764 && token->type == CPP_NAME
765 && token->id_kind == C_ID_CLASSNAME
766 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
767 return false;
769 /* Labels do not start declarations. */
770 if (token->type == CPP_NAME
771 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
772 return false;
774 if (c_token_starts_declaration (token))
775 return true;
777 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
778 return true;
780 return false;
783 /* Consume the next token from PARSER. */
785 static void
786 c_parser_consume_token (c_parser *parser)
788 gcc_assert (parser->tokens_avail >= 1);
789 gcc_assert (parser->tokens[0].type != CPP_EOF);
790 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
791 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
792 if (parser->tokens != &parser->tokens_buf[0])
793 parser->tokens++;
794 else if (parser->tokens_avail == 2)
795 parser->tokens[0] = parser->tokens[1];
796 parser->tokens_avail--;
799 /* Expect the current token to be a #pragma. Consume it and remember
800 that we've begun parsing a pragma. */
802 static void
803 c_parser_consume_pragma (c_parser *parser)
805 gcc_assert (!parser->in_pragma);
806 gcc_assert (parser->tokens_avail >= 1);
807 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
808 if (parser->tokens != &parser->tokens_buf[0])
809 parser->tokens++;
810 else if (parser->tokens_avail == 2)
811 parser->tokens[0] = parser->tokens[1];
812 parser->tokens_avail--;
813 parser->in_pragma = true;
816 /* Update the global input_location from TOKEN. */
817 static inline void
818 c_parser_set_source_position_from_token (c_token *token)
820 if (token->type != CPP_EOF)
822 input_location = token->location;
826 /* Issue a diagnostic of the form
827 FILE:LINE: MESSAGE before TOKEN
828 where TOKEN is the next token in the input stream of PARSER.
829 MESSAGE (specified by the caller) is usually of the form "expected
830 OTHER-TOKEN".
832 Do not issue a diagnostic if still recovering from an error.
834 ??? This is taken from the C++ parser, but building up messages in
835 this way is not i18n-friendly and some other approach should be
836 used. */
838 static void
839 c_parser_error (c_parser *parser, const char *gmsgid)
841 c_token *token = c_parser_peek_token (parser);
842 if (parser->error)
843 return;
844 parser->error = true;
845 if (!gmsgid)
846 return;
847 /* This diagnostic makes more sense if it is tagged to the line of
848 the token we just peeked at. */
849 c_parser_set_source_position_from_token (token);
850 c_parse_error (gmsgid,
851 /* Because c_parse_error does not understand
852 CPP_KEYWORD, keywords are treated like
853 identifiers. */
854 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
855 /* ??? The C parser does not save the cpp flags of a
856 token, we need to pass 0 here and we will not get
857 the source spelling of some tokens but rather the
858 canonical spelling. */
859 token->value, /*flags=*/0);
862 /* If the next token is of the indicated TYPE, consume it. Otherwise,
863 issue the error MSGID. If MSGID is NULL then a message has already
864 been produced and no message will be produced this time. Returns
865 true if found, false otherwise. */
867 static bool
868 c_parser_require (c_parser *parser,
869 enum cpp_ttype type,
870 const char *msgid)
872 if (c_parser_next_token_is (parser, type))
874 c_parser_consume_token (parser);
875 return true;
877 else
879 c_parser_error (parser, msgid);
880 return false;
884 /* If the next token is the indicated keyword, consume it. Otherwise,
885 issue the error MSGID. Returns true if found, false otherwise. */
887 static bool
888 c_parser_require_keyword (c_parser *parser,
889 enum rid keyword,
890 const char *msgid)
892 if (c_parser_next_token_is_keyword (parser, keyword))
894 c_parser_consume_token (parser);
895 return true;
897 else
899 c_parser_error (parser, msgid);
900 return false;
904 /* Like c_parser_require, except that tokens will be skipped until the
905 desired token is found. An error message is still produced if the
906 next token is not as expected. If MSGID is NULL then a message has
907 already been produced and no message will be produced this
908 time. */
910 static void
911 c_parser_skip_until_found (c_parser *parser,
912 enum cpp_ttype type,
913 const char *msgid)
915 unsigned nesting_depth = 0;
917 if (c_parser_require (parser, type, msgid))
918 return;
920 /* Skip tokens until the desired token is found. */
921 while (true)
923 /* Peek at the next token. */
924 c_token *token = c_parser_peek_token (parser);
925 /* If we've reached the token we want, consume it and stop. */
926 if (token->type == type && !nesting_depth)
928 c_parser_consume_token (parser);
929 break;
932 /* If we've run out of tokens, stop. */
933 if (token->type == CPP_EOF)
934 return;
935 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
936 return;
937 if (token->type == CPP_OPEN_BRACE
938 || token->type == CPP_OPEN_PAREN
939 || token->type == CPP_OPEN_SQUARE)
940 ++nesting_depth;
941 else if (token->type == CPP_CLOSE_BRACE
942 || token->type == CPP_CLOSE_PAREN
943 || token->type == CPP_CLOSE_SQUARE)
945 if (nesting_depth-- == 0)
946 break;
948 /* Consume this token. */
949 c_parser_consume_token (parser);
951 parser->error = false;
954 /* Skip tokens until the end of a parameter is found, but do not
955 consume the comma, semicolon or closing delimiter. */
957 static void
958 c_parser_skip_to_end_of_parameter (c_parser *parser)
960 unsigned nesting_depth = 0;
962 while (true)
964 c_token *token = c_parser_peek_token (parser);
965 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
966 && !nesting_depth)
967 break;
968 /* If we've run out of tokens, stop. */
969 if (token->type == CPP_EOF)
970 return;
971 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
972 return;
973 if (token->type == CPP_OPEN_BRACE
974 || token->type == CPP_OPEN_PAREN
975 || token->type == CPP_OPEN_SQUARE)
976 ++nesting_depth;
977 else if (token->type == CPP_CLOSE_BRACE
978 || token->type == CPP_CLOSE_PAREN
979 || token->type == CPP_CLOSE_SQUARE)
981 if (nesting_depth-- == 0)
982 break;
984 /* Consume this token. */
985 c_parser_consume_token (parser);
987 parser->error = false;
990 /* Expect to be at the end of the pragma directive and consume an
991 end of line marker. */
993 static void
994 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
996 gcc_assert (parser->in_pragma);
997 parser->in_pragma = false;
999 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1000 c_parser_error (parser, "expected end of line");
1002 cpp_ttype token_type;
1005 c_token *token = c_parser_peek_token (parser);
1006 token_type = token->type;
1007 if (token_type == CPP_EOF)
1008 break;
1009 c_parser_consume_token (parser);
1011 while (token_type != CPP_PRAGMA_EOL);
1013 parser->error = false;
1016 /* Skip tokens until we have consumed an entire block, or until we
1017 have consumed a non-nested ';'. */
1019 static void
1020 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1022 unsigned nesting_depth = 0;
1023 bool save_error = parser->error;
1025 while (true)
1027 c_token *token;
1029 /* Peek at the next token. */
1030 token = c_parser_peek_token (parser);
1032 switch (token->type)
1034 case CPP_EOF:
1035 return;
1037 case CPP_PRAGMA_EOL:
1038 if (parser->in_pragma)
1039 return;
1040 break;
1042 case CPP_SEMICOLON:
1043 /* If the next token is a ';', we have reached the
1044 end of the statement. */
1045 if (!nesting_depth)
1047 /* Consume the ';'. */
1048 c_parser_consume_token (parser);
1049 goto finished;
1051 break;
1053 case CPP_CLOSE_BRACE:
1054 /* If the next token is a non-nested '}', then we have
1055 reached the end of the current block. */
1056 if (nesting_depth == 0 || --nesting_depth == 0)
1058 c_parser_consume_token (parser);
1059 goto finished;
1061 break;
1063 case CPP_OPEN_BRACE:
1064 /* If it the next token is a '{', then we are entering a new
1065 block. Consume the entire block. */
1066 ++nesting_depth;
1067 break;
1069 case CPP_PRAGMA:
1070 /* If we see a pragma, consume the whole thing at once. We
1071 have some safeguards against consuming pragmas willy-nilly.
1072 Normally, we'd expect to be here with parser->error set,
1073 which disables these safeguards. But it's possible to get
1074 here for secondary error recovery, after parser->error has
1075 been cleared. */
1076 c_parser_consume_pragma (parser);
1077 c_parser_skip_to_pragma_eol (parser);
1078 parser->error = save_error;
1079 continue;
1081 default:
1082 break;
1085 c_parser_consume_token (parser);
1088 finished:
1089 parser->error = false;
1092 /* CPP's options (initialized by c-opts.c). */
1093 extern cpp_options *cpp_opts;
1095 /* Save the warning flags which are controlled by __extension__. */
1097 static inline int
1098 disable_extension_diagnostics (void)
1100 int ret = (pedantic
1101 | (warn_pointer_arith << 1)
1102 | (warn_traditional << 2)
1103 | (flag_iso << 3)
1104 | (warn_long_long << 4)
1105 | (warn_cxx_compat << 5)
1106 | (warn_overlength_strings << 6)
1107 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1108 play tricks to properly restore it. */
1109 | ((warn_c90_c99_compat == 1) << 7)
1110 | ((warn_c90_c99_compat == -1) << 8)
1111 /* Similarly for warn_c99_c11_compat. */
1112 | ((warn_c99_c11_compat == 1) << 9)
1113 | ((warn_c99_c11_compat == -1) << 10)
1115 cpp_opts->cpp_pedantic = pedantic = 0;
1116 warn_pointer_arith = 0;
1117 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1118 flag_iso = 0;
1119 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1120 warn_cxx_compat = 0;
1121 warn_overlength_strings = 0;
1122 warn_c90_c99_compat = 0;
1123 warn_c99_c11_compat = 0;
1124 return ret;
1127 /* Restore the warning flags which are controlled by __extension__.
1128 FLAGS is the return value from disable_extension_diagnostics. */
1130 static inline void
1131 restore_extension_diagnostics (int flags)
1133 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1134 warn_pointer_arith = (flags >> 1) & 1;
1135 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1136 flag_iso = (flags >> 3) & 1;
1137 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1138 warn_cxx_compat = (flags >> 5) & 1;
1139 warn_overlength_strings = (flags >> 6) & 1;
1140 /* See above for why is this needed. */
1141 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1142 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1145 /* Possibly kinds of declarator to parse. */
1146 typedef enum c_dtr_syn {
1147 /* A normal declarator with an identifier. */
1148 C_DTR_NORMAL,
1149 /* An abstract declarator (maybe empty). */
1150 C_DTR_ABSTRACT,
1151 /* A parameter declarator: may be either, but after a type name does
1152 not redeclare a typedef name as an identifier if it can
1153 alternatively be interpreted as a typedef name; see DR#009,
1154 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1155 following DR#249. For example, given a typedef T, "int T" and
1156 "int *T" are valid parameter declarations redeclaring T, while
1157 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1158 abstract declarators rather than involving redundant parentheses;
1159 the same applies with attributes inside the parentheses before
1160 "T". */
1161 C_DTR_PARM
1162 } c_dtr_syn;
1164 /* The binary operation precedence levels, where 0 is a dummy lowest level
1165 used for the bottom of the stack. */
1166 enum c_parser_prec {
1167 PREC_NONE,
1168 PREC_LOGOR,
1169 PREC_LOGAND,
1170 PREC_BITOR,
1171 PREC_BITXOR,
1172 PREC_BITAND,
1173 PREC_EQ,
1174 PREC_REL,
1175 PREC_SHIFT,
1176 PREC_ADD,
1177 PREC_MULT,
1178 NUM_PRECS
1181 static void c_parser_external_declaration (c_parser *);
1182 static void c_parser_asm_definition (c_parser *);
1183 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1184 bool, bool, tree *, vec<c_token>);
1185 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1186 static void c_parser_static_assert_declaration (c_parser *);
1187 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1188 bool, bool, bool, enum c_lookahead_kind);
1189 static struct c_typespec c_parser_enum_specifier (c_parser *);
1190 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1191 static tree c_parser_struct_declaration (c_parser *);
1192 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1193 static tree c_parser_alignas_specifier (c_parser *);
1194 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1195 bool *);
1196 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1197 c_dtr_syn, bool *);
1198 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1199 bool,
1200 struct c_declarator *);
1201 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1202 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1203 tree);
1204 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1205 static tree c_parser_simple_asm_expr (c_parser *);
1206 static tree c_parser_attributes (c_parser *);
1207 static struct c_type_name *c_parser_type_name (c_parser *);
1208 static struct c_expr c_parser_initializer (c_parser *);
1209 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1210 static void c_parser_initelt (c_parser *, struct obstack *);
1211 static void c_parser_initval (c_parser *, struct c_expr *,
1212 struct obstack *);
1213 static tree c_parser_compound_statement (c_parser *);
1214 static void c_parser_compound_statement_nostart (c_parser *);
1215 static void c_parser_label (c_parser *);
1216 static void c_parser_statement (c_parser *);
1217 static void c_parser_statement_after_labels (c_parser *);
1218 static void c_parser_if_statement (c_parser *);
1219 static void c_parser_switch_statement (c_parser *);
1220 static void c_parser_while_statement (c_parser *, bool);
1221 static void c_parser_do_statement (c_parser *, bool);
1222 static void c_parser_for_statement (c_parser *, bool);
1223 static tree c_parser_asm_statement (c_parser *);
1224 static tree c_parser_asm_operands (c_parser *);
1225 static tree c_parser_asm_goto_operands (c_parser *);
1226 static tree c_parser_asm_clobbers (c_parser *);
1227 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1228 tree = NULL_TREE);
1229 static struct c_expr c_parser_conditional_expression (c_parser *,
1230 struct c_expr *, tree);
1231 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1232 tree);
1233 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1234 static struct c_expr c_parser_unary_expression (c_parser *);
1235 static struct c_expr c_parser_sizeof_expression (c_parser *);
1236 static struct c_expr c_parser_alignof_expression (c_parser *);
1237 static struct c_expr c_parser_postfix_expression (c_parser *);
1238 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1239 struct c_type_name *,
1240 location_t);
1241 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1242 location_t loc,
1243 struct c_expr);
1244 static tree c_parser_transaction (c_parser *, enum rid);
1245 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1246 static tree c_parser_transaction_cancel (c_parser *);
1247 static struct c_expr c_parser_expression (c_parser *);
1248 static struct c_expr c_parser_expression_conv (c_parser *);
1249 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1250 vec<tree, va_gc> **, location_t *,
1251 tree *, vec<location_t> *,
1252 unsigned int * = NULL);
1253 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1254 static void c_parser_oacc_update (c_parser *);
1255 static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1256 static void c_parser_omp_construct (c_parser *);
1257 static void c_parser_omp_threadprivate (c_parser *);
1258 static void c_parser_omp_barrier (c_parser *);
1259 static void c_parser_omp_flush (c_parser *);
1260 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1261 tree, tree *);
1262 static void c_parser_omp_taskwait (c_parser *);
1263 static void c_parser_omp_taskyield (c_parser *);
1264 static void c_parser_omp_cancel (c_parser *);
1265 static void c_parser_omp_cancellation_point (c_parser *);
1267 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1268 pragma_stmt, pragma_compound };
1269 static bool c_parser_pragma (c_parser *, enum pragma_context);
1270 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1271 static void c_parser_omp_end_declare_target (c_parser *);
1272 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1274 /* These Objective-C parser functions are only ever called when
1275 compiling Objective-C. */
1276 static void c_parser_objc_class_definition (c_parser *, tree);
1277 static void c_parser_objc_class_instance_variables (c_parser *);
1278 static void c_parser_objc_class_declaration (c_parser *);
1279 static void c_parser_objc_alias_declaration (c_parser *);
1280 static void c_parser_objc_protocol_definition (c_parser *, tree);
1281 static bool c_parser_objc_method_type (c_parser *);
1282 static void c_parser_objc_method_definition (c_parser *);
1283 static void c_parser_objc_methodprotolist (c_parser *);
1284 static void c_parser_objc_methodproto (c_parser *);
1285 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1286 static tree c_parser_objc_type_name (c_parser *);
1287 static tree c_parser_objc_protocol_refs (c_parser *);
1288 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1289 static void c_parser_objc_synchronized_statement (c_parser *);
1290 static tree c_parser_objc_selector (c_parser *);
1291 static tree c_parser_objc_selector_arg (c_parser *);
1292 static tree c_parser_objc_receiver (c_parser *);
1293 static tree c_parser_objc_message_args (c_parser *);
1294 static tree c_parser_objc_keywordexpr (c_parser *);
1295 static void c_parser_objc_at_property_declaration (c_parser *);
1296 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1297 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1298 static bool c_parser_objc_diagnose_bad_element_prefix
1299 (c_parser *, struct c_declspecs *);
1301 /* Cilk Plus supporting routines. */
1302 static void c_parser_cilk_simd (c_parser *);
1303 static void c_parser_cilk_for (c_parser *, tree);
1304 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1305 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1306 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1307 static void c_parser_cilk_grainsize (c_parser *);
1309 /* Parse a translation unit (C90 6.7, C99 6.9).
1311 translation-unit:
1312 external-declarations
1314 external-declarations:
1315 external-declaration
1316 external-declarations external-declaration
1318 GNU extensions:
1320 translation-unit:
1321 empty
1324 static void
1325 c_parser_translation_unit (c_parser *parser)
1327 if (c_parser_next_token_is (parser, CPP_EOF))
1329 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1330 "ISO C forbids an empty translation unit");
1332 else
1334 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1335 mark_valid_location_for_stdc_pragma (false);
1338 ggc_collect ();
1339 c_parser_external_declaration (parser);
1340 obstack_free (&parser_obstack, obstack_position);
1342 while (c_parser_next_token_is_not (parser, CPP_EOF));
1346 /* Parse an external declaration (C90 6.7, C99 6.9).
1348 external-declaration:
1349 function-definition
1350 declaration
1352 GNU extensions:
1354 external-declaration:
1355 asm-definition
1357 __extension__ external-declaration
1359 Objective-C:
1361 external-declaration:
1362 objc-class-definition
1363 objc-class-declaration
1364 objc-alias-declaration
1365 objc-protocol-definition
1366 objc-method-definition
1367 @end
1370 static void
1371 c_parser_external_declaration (c_parser *parser)
1373 int ext;
1374 switch (c_parser_peek_token (parser)->type)
1376 case CPP_KEYWORD:
1377 switch (c_parser_peek_token (parser)->keyword)
1379 case RID_EXTENSION:
1380 ext = disable_extension_diagnostics ();
1381 c_parser_consume_token (parser);
1382 c_parser_external_declaration (parser);
1383 restore_extension_diagnostics (ext);
1384 break;
1385 case RID_ASM:
1386 c_parser_asm_definition (parser);
1387 break;
1388 case RID_AT_INTERFACE:
1389 case RID_AT_IMPLEMENTATION:
1390 gcc_assert (c_dialect_objc ());
1391 c_parser_objc_class_definition (parser, NULL_TREE);
1392 break;
1393 case RID_AT_CLASS:
1394 gcc_assert (c_dialect_objc ());
1395 c_parser_objc_class_declaration (parser);
1396 break;
1397 case RID_AT_ALIAS:
1398 gcc_assert (c_dialect_objc ());
1399 c_parser_objc_alias_declaration (parser);
1400 break;
1401 case RID_AT_PROTOCOL:
1402 gcc_assert (c_dialect_objc ());
1403 c_parser_objc_protocol_definition (parser, NULL_TREE);
1404 break;
1405 case RID_AT_PROPERTY:
1406 gcc_assert (c_dialect_objc ());
1407 c_parser_objc_at_property_declaration (parser);
1408 break;
1409 case RID_AT_SYNTHESIZE:
1410 gcc_assert (c_dialect_objc ());
1411 c_parser_objc_at_synthesize_declaration (parser);
1412 break;
1413 case RID_AT_DYNAMIC:
1414 gcc_assert (c_dialect_objc ());
1415 c_parser_objc_at_dynamic_declaration (parser);
1416 break;
1417 case RID_AT_END:
1418 gcc_assert (c_dialect_objc ());
1419 c_parser_consume_token (parser);
1420 objc_finish_implementation ();
1421 break;
1422 default:
1423 goto decl_or_fndef;
1425 break;
1426 case CPP_SEMICOLON:
1427 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1428 "ISO C does not allow extra %<;%> outside of a function");
1429 c_parser_consume_token (parser);
1430 break;
1431 case CPP_PRAGMA:
1432 mark_valid_location_for_stdc_pragma (true);
1433 c_parser_pragma (parser, pragma_external);
1434 mark_valid_location_for_stdc_pragma (false);
1435 break;
1436 case CPP_PLUS:
1437 case CPP_MINUS:
1438 if (c_dialect_objc ())
1440 c_parser_objc_method_definition (parser);
1441 break;
1443 /* Else fall through, and yield a syntax error trying to parse
1444 as a declaration or function definition. */
1445 default:
1446 decl_or_fndef:
1447 /* A declaration or a function definition (or, in Objective-C,
1448 an @interface or @protocol with prefix attributes). We can
1449 only tell which after parsing the declaration specifiers, if
1450 any, and the first declarator. */
1451 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1452 NULL, vNULL);
1453 break;
1457 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1459 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1460 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1461 accepted; otherwise (old-style parameter declarations) only other
1462 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1463 assertion is accepted; otherwise (old-style parameter declarations)
1464 it is not. If NESTED is true, we are inside a function or parsing
1465 old-style parameter declarations; any functions encountered are
1466 nested functions and declaration specifiers are required; otherwise
1467 we are at top level and functions are normal functions and
1468 declaration specifiers may be optional. If EMPTY_OK is true, empty
1469 declarations are OK (subject to all other constraints); otherwise
1470 (old-style parameter declarations) they are diagnosed. If
1471 START_ATTR_OK is true, the declaration specifiers may start with
1472 attributes; otherwise they may not.
1473 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1474 declaration when parsing an Objective-C foreach statement.
1476 declaration:
1477 declaration-specifiers init-declarator-list[opt] ;
1478 static_assert-declaration
1480 function-definition:
1481 declaration-specifiers[opt] declarator declaration-list[opt]
1482 compound-statement
1484 declaration-list:
1485 declaration
1486 declaration-list declaration
1488 init-declarator-list:
1489 init-declarator
1490 init-declarator-list , init-declarator
1492 init-declarator:
1493 declarator simple-asm-expr[opt] attributes[opt]
1494 declarator simple-asm-expr[opt] attributes[opt] = initializer
1496 GNU extensions:
1498 nested-function-definition:
1499 declaration-specifiers declarator declaration-list[opt]
1500 compound-statement
1502 Objective-C:
1503 attributes objc-class-definition
1504 attributes objc-category-definition
1505 attributes objc-protocol-definition
1507 The simple-asm-expr and attributes are GNU extensions.
1509 This function does not handle __extension__; that is handled in its
1510 callers. ??? Following the old parser, __extension__ may start
1511 external declarations, declarations in functions and declarations
1512 at the start of "for" loops, but not old-style parameter
1513 declarations.
1515 C99 requires declaration specifiers in a function definition; the
1516 absence is diagnosed through the diagnosis of implicit int. In GNU
1517 C we also allow but diagnose declarations without declaration
1518 specifiers, but only at top level (elsewhere they conflict with
1519 other syntax).
1521 In Objective-C, declarations of the looping variable in a foreach
1522 statement are exceptionally terminated by 'in' (for example, 'for
1523 (NSObject *object in array) { ... }').
1525 OpenMP:
1527 declaration:
1528 threadprivate-directive */
1530 static void
1531 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1532 bool static_assert_ok, bool empty_ok,
1533 bool nested, bool start_attr_ok,
1534 tree *objc_foreach_object_declaration,
1535 vec<c_token> omp_declare_simd_clauses)
1537 struct c_declspecs *specs;
1538 tree prefix_attrs;
1539 tree all_prefix_attrs;
1540 bool diagnosed_no_specs = false;
1541 location_t here = c_parser_peek_token (parser)->location;
1543 if (static_assert_ok
1544 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1546 c_parser_static_assert_declaration (parser);
1547 return;
1549 specs = build_null_declspecs ();
1551 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1552 if (c_parser_peek_token (parser)->type == CPP_NAME
1553 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1554 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1555 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1556 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1558 error_at (here, "unknown type name %qE",
1559 c_parser_peek_token (parser)->value);
1561 /* Parse declspecs normally to get a correct pointer type, but avoid
1562 a further "fails to be a type name" error. Refuse nested functions
1563 since it is not how the user likely wants us to recover. */
1564 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1565 c_parser_peek_token (parser)->keyword = RID_VOID;
1566 c_parser_peek_token (parser)->value = error_mark_node;
1567 fndef_ok = !nested;
1570 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1571 true, true, cla_nonabstract_decl);
1572 if (parser->error)
1574 c_parser_skip_to_end_of_block_or_statement (parser);
1575 return;
1577 if (nested && !specs->declspecs_seen_p)
1579 c_parser_error (parser, "expected declaration specifiers");
1580 c_parser_skip_to_end_of_block_or_statement (parser);
1581 return;
1583 finish_declspecs (specs);
1584 bool auto_type_p = specs->typespec_word == cts_auto_type;
1585 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1587 if (auto_type_p)
1588 error_at (here, "%<__auto_type%> in empty declaration");
1589 else if (empty_ok)
1590 shadow_tag (specs);
1591 else
1593 shadow_tag_warned (specs, 1);
1594 pedwarn (here, 0, "empty declaration");
1596 c_parser_consume_token (parser);
1597 return;
1600 /* Provide better error recovery. Note that a type name here is usually
1601 better diagnosed as a redeclaration. */
1602 if (empty_ok
1603 && specs->typespec_kind == ctsk_tagdef
1604 && c_parser_next_token_starts_declspecs (parser)
1605 && !c_parser_next_token_is (parser, CPP_NAME))
1607 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1608 parser->error = false;
1609 shadow_tag_warned (specs, 1);
1610 return;
1612 else if (c_dialect_objc () && !auto_type_p)
1614 /* Prefix attributes are an error on method decls. */
1615 switch (c_parser_peek_token (parser)->type)
1617 case CPP_PLUS:
1618 case CPP_MINUS:
1619 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1620 return;
1621 if (specs->attrs)
1623 warning_at (c_parser_peek_token (parser)->location,
1624 OPT_Wattributes,
1625 "prefix attributes are ignored for methods");
1626 specs->attrs = NULL_TREE;
1628 if (fndef_ok)
1629 c_parser_objc_method_definition (parser);
1630 else
1631 c_parser_objc_methodproto (parser);
1632 return;
1633 break;
1634 default:
1635 break;
1637 /* This is where we parse 'attributes @interface ...',
1638 'attributes @implementation ...', 'attributes @protocol ...'
1639 (where attributes could be, for example, __attribute__
1640 ((deprecated)).
1642 switch (c_parser_peek_token (parser)->keyword)
1644 case RID_AT_INTERFACE:
1646 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1647 return;
1648 c_parser_objc_class_definition (parser, specs->attrs);
1649 return;
1651 break;
1652 case RID_AT_IMPLEMENTATION:
1654 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1655 return;
1656 if (specs->attrs)
1658 warning_at (c_parser_peek_token (parser)->location,
1659 OPT_Wattributes,
1660 "prefix attributes are ignored for implementations");
1661 specs->attrs = NULL_TREE;
1663 c_parser_objc_class_definition (parser, NULL_TREE);
1664 return;
1666 break;
1667 case RID_AT_PROTOCOL:
1669 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1670 return;
1671 c_parser_objc_protocol_definition (parser, specs->attrs);
1672 return;
1674 break;
1675 case RID_AT_ALIAS:
1676 case RID_AT_CLASS:
1677 case RID_AT_END:
1678 case RID_AT_PROPERTY:
1679 if (specs->attrs)
1681 c_parser_error (parser, "unexpected attribute");
1682 specs->attrs = NULL;
1684 break;
1685 default:
1686 break;
1690 pending_xref_error ();
1691 prefix_attrs = specs->attrs;
1692 all_prefix_attrs = prefix_attrs;
1693 specs->attrs = NULL_TREE;
1694 while (true)
1696 struct c_declarator *declarator;
1697 bool dummy = false;
1698 timevar_id_t tv;
1699 tree fnbody;
1700 /* Declaring either one or more declarators (in which case we
1701 should diagnose if there were no declaration specifiers) or a
1702 function definition (in which case the diagnostic for
1703 implicit int suffices). */
1704 declarator = c_parser_declarator (parser,
1705 specs->typespec_kind != ctsk_none,
1706 C_DTR_NORMAL, &dummy);
1707 if (declarator == NULL)
1709 if (omp_declare_simd_clauses.exists ()
1710 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1711 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1712 omp_declare_simd_clauses);
1713 c_parser_skip_to_end_of_block_or_statement (parser);
1714 return;
1716 if (auto_type_p && declarator->kind != cdk_id)
1718 error_at (here,
1719 "%<__auto_type%> requires a plain identifier"
1720 " as declarator");
1721 c_parser_skip_to_end_of_block_or_statement (parser);
1722 return;
1724 if (c_parser_next_token_is (parser, CPP_EQ)
1725 || c_parser_next_token_is (parser, CPP_COMMA)
1726 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1727 || c_parser_next_token_is_keyword (parser, RID_ASM)
1728 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1729 || c_parser_next_token_is_keyword (parser, RID_IN))
1731 tree asm_name = NULL_TREE;
1732 tree postfix_attrs = NULL_TREE;
1733 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1735 diagnosed_no_specs = true;
1736 pedwarn (here, 0, "data definition has no type or storage class");
1738 /* Having seen a data definition, there cannot now be a
1739 function definition. */
1740 fndef_ok = false;
1741 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1742 asm_name = c_parser_simple_asm_expr (parser);
1743 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1745 postfix_attrs = c_parser_attributes (parser);
1746 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1748 /* This means there is an attribute specifier after
1749 the declarator in a function definition. Provide
1750 some more information for the user. */
1751 error_at (here, "attributes should be specified before the "
1752 "declarator in a function definition");
1753 c_parser_skip_to_end_of_block_or_statement (parser);
1754 return;
1757 if (c_parser_next_token_is (parser, CPP_EQ))
1759 tree d;
1760 struct c_expr init;
1761 location_t init_loc;
1762 c_parser_consume_token (parser);
1763 if (auto_type_p)
1765 start_init (NULL_TREE, asm_name, global_bindings_p ());
1766 init_loc = c_parser_peek_token (parser)->location;
1767 init = c_parser_expr_no_commas (parser, NULL);
1768 if (TREE_CODE (init.value) == COMPONENT_REF
1769 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1770 error_at (here,
1771 "%<__auto_type%> used with a bit-field"
1772 " initializer");
1773 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1774 tree init_type = TREE_TYPE (init.value);
1775 /* As with typeof, remove all qualifiers from atomic types. */
1776 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1777 init_type
1778 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1779 bool vm_type = variably_modified_type_p (init_type,
1780 NULL_TREE);
1781 if (vm_type)
1782 init.value = c_save_expr (init.value);
1783 finish_init ();
1784 specs->typespec_kind = ctsk_typeof;
1785 specs->locations[cdw_typedef] = init_loc;
1786 specs->typedef_p = true;
1787 specs->type = init_type;
1788 if (vm_type)
1790 bool maybe_const = true;
1791 tree type_expr = c_fully_fold (init.value, false,
1792 &maybe_const);
1793 specs->expr_const_operands &= maybe_const;
1794 if (specs->expr)
1795 specs->expr = build2 (COMPOUND_EXPR,
1796 TREE_TYPE (type_expr),
1797 specs->expr, type_expr);
1798 else
1799 specs->expr = type_expr;
1801 d = start_decl (declarator, specs, true,
1802 chainon (postfix_attrs, all_prefix_attrs));
1803 if (!d)
1804 d = error_mark_node;
1805 if (omp_declare_simd_clauses.exists ()
1806 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1807 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1808 omp_declare_simd_clauses);
1810 else
1812 /* The declaration of the variable is in effect while
1813 its initializer is parsed. */
1814 d = start_decl (declarator, specs, true,
1815 chainon (postfix_attrs, all_prefix_attrs));
1816 if (!d)
1817 d = error_mark_node;
1818 if (omp_declare_simd_clauses.exists ()
1819 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1820 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1821 omp_declare_simd_clauses);
1822 start_init (d, asm_name, global_bindings_p ());
1823 init_loc = c_parser_peek_token (parser)->location;
1824 init = c_parser_initializer (parser);
1825 finish_init ();
1827 if (d != error_mark_node)
1829 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1830 finish_decl (d, init_loc, init.value,
1831 init.original_type, asm_name);
1834 else
1836 if (auto_type_p)
1838 error_at (here,
1839 "%<__auto_type%> requires an initialized "
1840 "data declaration");
1841 c_parser_skip_to_end_of_block_or_statement (parser);
1842 return;
1844 tree d = start_decl (declarator, specs, false,
1845 chainon (postfix_attrs,
1846 all_prefix_attrs));
1847 if (omp_declare_simd_clauses.exists ()
1848 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1850 tree parms = NULL_TREE;
1851 if (d && TREE_CODE (d) == FUNCTION_DECL)
1853 struct c_declarator *ce = declarator;
1854 while (ce != NULL)
1855 if (ce->kind == cdk_function)
1857 parms = ce->u.arg_info->parms;
1858 break;
1860 else
1861 ce = ce->declarator;
1863 if (parms)
1864 temp_store_parm_decls (d, parms);
1865 c_finish_omp_declare_simd (parser, d, parms,
1866 omp_declare_simd_clauses);
1867 if (parms)
1868 temp_pop_parm_decls ();
1870 if (d)
1871 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1872 NULL_TREE, asm_name);
1874 if (c_parser_next_token_is_keyword (parser, RID_IN))
1876 if (d)
1877 *objc_foreach_object_declaration = d;
1878 else
1879 *objc_foreach_object_declaration = error_mark_node;
1882 if (c_parser_next_token_is (parser, CPP_COMMA))
1884 if (auto_type_p)
1886 error_at (here,
1887 "%<__auto_type%> may only be used with"
1888 " a single declarator");
1889 c_parser_skip_to_end_of_block_or_statement (parser);
1890 return;
1892 c_parser_consume_token (parser);
1893 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1894 all_prefix_attrs = chainon (c_parser_attributes (parser),
1895 prefix_attrs);
1896 else
1897 all_prefix_attrs = prefix_attrs;
1898 continue;
1900 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1902 c_parser_consume_token (parser);
1903 return;
1905 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1907 /* This can only happen in Objective-C: we found the
1908 'in' that terminates the declaration inside an
1909 Objective-C foreach statement. Do not consume the
1910 token, so that the caller can use it to determine
1911 that this indeed is a foreach context. */
1912 return;
1914 else
1916 c_parser_error (parser, "expected %<,%> or %<;%>");
1917 c_parser_skip_to_end_of_block_or_statement (parser);
1918 return;
1921 else if (auto_type_p)
1923 error_at (here,
1924 "%<__auto_type%> requires an initialized data declaration");
1925 c_parser_skip_to_end_of_block_or_statement (parser);
1926 return;
1928 else if (!fndef_ok)
1930 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1931 "%<asm%> or %<__attribute__%>");
1932 c_parser_skip_to_end_of_block_or_statement (parser);
1933 return;
1935 /* Function definition (nested or otherwise). */
1936 if (nested)
1938 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1939 c_push_function_context ();
1941 if (!start_function (specs, declarator, all_prefix_attrs))
1943 /* This can appear in many cases looking nothing like a
1944 function definition, so we don't give a more specific
1945 error suggesting there was one. */
1946 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1947 "or %<__attribute__%>");
1948 if (nested)
1949 c_pop_function_context ();
1950 break;
1953 if (DECL_DECLARED_INLINE_P (current_function_decl))
1954 tv = TV_PARSE_INLINE;
1955 else
1956 tv = TV_PARSE_FUNC;
1957 timevar_push (tv);
1959 /* Parse old-style parameter declarations. ??? Attributes are
1960 not allowed to start declaration specifiers here because of a
1961 syntax conflict between a function declaration with attribute
1962 suffix and a function definition with an attribute prefix on
1963 first old-style parameter declaration. Following the old
1964 parser, they are not accepted on subsequent old-style
1965 parameter declarations either. However, there is no
1966 ambiguity after the first declaration, nor indeed on the
1967 first as long as we don't allow postfix attributes after a
1968 declarator with a nonempty identifier list in a definition;
1969 and postfix attributes have never been accepted here in
1970 function definitions either. */
1971 while (c_parser_next_token_is_not (parser, CPP_EOF)
1972 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1973 c_parser_declaration_or_fndef (parser, false, false, false,
1974 true, false, NULL, vNULL);
1975 store_parm_decls ();
1976 if (omp_declare_simd_clauses.exists ()
1977 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1978 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1979 omp_declare_simd_clauses);
1980 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1981 = c_parser_peek_token (parser)->location;
1982 fnbody = c_parser_compound_statement (parser);
1983 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1984 fnbody = expand_array_notation_exprs (fnbody);
1985 if (nested)
1987 tree decl = current_function_decl;
1988 /* Mark nested functions as needing static-chain initially.
1989 lower_nested_functions will recompute it but the
1990 DECL_STATIC_CHAIN flag is also used before that happens,
1991 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1992 DECL_STATIC_CHAIN (decl) = 1;
1993 add_stmt (fnbody);
1994 finish_function ();
1995 c_pop_function_context ();
1996 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1998 else
2000 add_stmt (fnbody);
2001 finish_function ();
2004 timevar_pop (tv);
2005 break;
2009 /* Parse an asm-definition (asm() outside a function body). This is a
2010 GNU extension.
2012 asm-definition:
2013 simple-asm-expr ;
2016 static void
2017 c_parser_asm_definition (c_parser *parser)
2019 tree asm_str = c_parser_simple_asm_expr (parser);
2020 if (asm_str)
2021 symtab->finalize_toplevel_asm (asm_str);
2022 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2025 /* Parse a static assertion (C11 6.7.10).
2027 static_assert-declaration:
2028 static_assert-declaration-no-semi ;
2031 static void
2032 c_parser_static_assert_declaration (c_parser *parser)
2034 c_parser_static_assert_declaration_no_semi (parser);
2035 if (parser->error
2036 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2037 c_parser_skip_to_end_of_block_or_statement (parser);
2040 /* Parse a static assertion (C11 6.7.10), without the trailing
2041 semicolon.
2043 static_assert-declaration-no-semi:
2044 _Static_assert ( constant-expression , string-literal )
2047 static void
2048 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2050 location_t assert_loc, value_loc;
2051 tree value;
2052 tree string;
2054 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2055 assert_loc = c_parser_peek_token (parser)->location;
2056 if (flag_isoc99)
2057 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2058 "ISO C99 does not support %<_Static_assert%>");
2059 else
2060 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2061 "ISO C90 does not support %<_Static_assert%>");
2062 c_parser_consume_token (parser);
2063 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2064 return;
2065 value_loc = c_parser_peek_token (parser)->location;
2066 value = c_parser_expr_no_commas (parser, NULL).value;
2067 parser->lex_untranslated_string = true;
2068 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2070 parser->lex_untranslated_string = false;
2071 return;
2073 switch (c_parser_peek_token (parser)->type)
2075 case CPP_STRING:
2076 case CPP_STRING16:
2077 case CPP_STRING32:
2078 case CPP_WSTRING:
2079 case CPP_UTF8STRING:
2080 string = c_parser_peek_token (parser)->value;
2081 c_parser_consume_token (parser);
2082 parser->lex_untranslated_string = false;
2083 break;
2084 default:
2085 c_parser_error (parser, "expected string literal");
2086 parser->lex_untranslated_string = false;
2087 return;
2089 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2091 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2093 error_at (value_loc, "expression in static assertion is not an integer");
2094 return;
2096 if (TREE_CODE (value) != INTEGER_CST)
2098 value = c_fully_fold (value, false, NULL);
2099 /* Strip no-op conversions. */
2100 STRIP_TYPE_NOPS (value);
2101 if (TREE_CODE (value) == INTEGER_CST)
2102 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2103 "is not an integer constant expression");
2105 if (TREE_CODE (value) != INTEGER_CST)
2107 error_at (value_loc, "expression in static assertion is not constant");
2108 return;
2110 constant_expression_warning (value);
2111 if (integer_zerop (value))
2112 error_at (assert_loc, "static assertion failed: %E", string);
2115 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2116 6.7), adding them to SPECS (which may already include some).
2117 Storage class specifiers are accepted iff SCSPEC_OK; type
2118 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2119 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2120 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2122 declaration-specifiers:
2123 storage-class-specifier declaration-specifiers[opt]
2124 type-specifier declaration-specifiers[opt]
2125 type-qualifier declaration-specifiers[opt]
2126 function-specifier declaration-specifiers[opt]
2127 alignment-specifier declaration-specifiers[opt]
2129 Function specifiers (inline) are from C99, and are currently
2130 handled as storage class specifiers, as is __thread. Alignment
2131 specifiers are from C11.
2133 C90 6.5.1, C99 6.7.1:
2134 storage-class-specifier:
2135 typedef
2136 extern
2137 static
2138 auto
2139 register
2140 _Thread_local
2142 (_Thread_local is new in C11.)
2144 C99 6.7.4:
2145 function-specifier:
2146 inline
2147 _Noreturn
2149 (_Noreturn is new in C11.)
2151 C90 6.5.2, C99 6.7.2:
2152 type-specifier:
2153 void
2154 char
2155 short
2157 long
2158 float
2159 double
2160 signed
2161 unsigned
2162 _Bool
2163 _Complex
2164 [_Imaginary removed in C99 TC2]
2165 struct-or-union-specifier
2166 enum-specifier
2167 typedef-name
2168 atomic-type-specifier
2170 (_Bool and _Complex are new in C99.)
2171 (atomic-type-specifier is new in C11.)
2173 C90 6.5.3, C99 6.7.3:
2175 type-qualifier:
2176 const
2177 restrict
2178 volatile
2179 address-space-qualifier
2180 _Atomic
2182 (restrict is new in C99.)
2183 (_Atomic is new in C11.)
2185 GNU extensions:
2187 declaration-specifiers:
2188 attributes declaration-specifiers[opt]
2190 type-qualifier:
2191 address-space
2193 address-space:
2194 identifier recognized by the target
2196 storage-class-specifier:
2197 __thread
2199 type-specifier:
2200 typeof-specifier
2201 __auto_type
2202 __intN
2203 _Decimal32
2204 _Decimal64
2205 _Decimal128
2206 _Fract
2207 _Accum
2208 _Sat
2210 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2211 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2213 atomic-type-specifier
2214 _Atomic ( type-name )
2216 Objective-C:
2218 type-specifier:
2219 class-name objc-protocol-refs[opt]
2220 typedef-name objc-protocol-refs
2221 objc-protocol-refs
2224 static void
2225 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2226 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2227 bool alignspec_ok, bool auto_type_ok,
2228 enum c_lookahead_kind la)
2230 bool attrs_ok = start_attr_ok;
2231 bool seen_type = specs->typespec_kind != ctsk_none;
2233 if (!typespec_ok)
2234 gcc_assert (la == cla_prefer_id);
2236 while (c_parser_next_token_is (parser, CPP_NAME)
2237 || c_parser_next_token_is (parser, CPP_KEYWORD)
2238 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2240 struct c_typespec t;
2241 tree attrs;
2242 tree align;
2243 location_t loc = c_parser_peek_token (parser)->location;
2245 /* If we cannot accept a type, exit if the next token must start
2246 one. Also, if we already have seen a tagged definition,
2247 a typename would be an error anyway and likely the user
2248 has simply forgotten a semicolon, so we exit. */
2249 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2250 && c_parser_next_tokens_start_typename (parser, la)
2251 && !c_parser_next_token_is_qualifier (parser))
2252 break;
2254 if (c_parser_next_token_is (parser, CPP_NAME))
2256 c_token *name_token = c_parser_peek_token (parser);
2257 tree value = name_token->value;
2258 c_id_kind kind = name_token->id_kind;
2260 if (kind == C_ID_ADDRSPACE)
2262 addr_space_t as
2263 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2264 declspecs_add_addrspace (name_token->location, specs, as);
2265 c_parser_consume_token (parser);
2266 attrs_ok = true;
2267 continue;
2270 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2272 /* If we cannot accept a type, and the next token must start one,
2273 exit. Do the same if we already have seen a tagged definition,
2274 since it would be an error anyway and likely the user has simply
2275 forgotten a semicolon. */
2276 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2277 break;
2279 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2280 a C_ID_CLASSNAME. */
2281 c_parser_consume_token (parser);
2282 seen_type = true;
2283 attrs_ok = true;
2284 if (kind == C_ID_ID)
2286 error_at (loc, "unknown type name %qE", value);
2287 t.kind = ctsk_typedef;
2288 t.spec = error_mark_node;
2290 else if (kind == C_ID_TYPENAME
2291 && (!c_dialect_objc ()
2292 || c_parser_next_token_is_not (parser, CPP_LESS)))
2294 t.kind = ctsk_typedef;
2295 /* For a typedef name, record the meaning, not the name.
2296 In case of 'foo foo, bar;'. */
2297 t.spec = lookup_name (value);
2299 else
2301 tree proto = NULL_TREE;
2302 gcc_assert (c_dialect_objc ());
2303 t.kind = ctsk_objc;
2304 if (c_parser_next_token_is (parser, CPP_LESS))
2305 proto = c_parser_objc_protocol_refs (parser);
2306 t.spec = objc_get_protocol_qualified_type (value, proto);
2308 t.expr = NULL_TREE;
2309 t.expr_const_operands = true;
2310 declspecs_add_type (name_token->location, specs, t);
2311 continue;
2313 if (c_parser_next_token_is (parser, CPP_LESS))
2315 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2316 nisse@lysator.liu.se. */
2317 tree proto;
2318 gcc_assert (c_dialect_objc ());
2319 if (!typespec_ok || seen_type)
2320 break;
2321 proto = c_parser_objc_protocol_refs (parser);
2322 t.kind = ctsk_objc;
2323 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2324 t.expr = NULL_TREE;
2325 t.expr_const_operands = true;
2326 declspecs_add_type (loc, specs, t);
2327 continue;
2329 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2330 switch (c_parser_peek_token (parser)->keyword)
2332 case RID_STATIC:
2333 case RID_EXTERN:
2334 case RID_REGISTER:
2335 case RID_TYPEDEF:
2336 case RID_INLINE:
2337 case RID_NORETURN:
2338 case RID_AUTO:
2339 case RID_THREAD:
2340 if (!scspec_ok)
2341 goto out;
2342 attrs_ok = true;
2343 /* TODO: Distinguish between function specifiers (inline, noreturn)
2344 and storage class specifiers, either here or in
2345 declspecs_add_scspec. */
2346 declspecs_add_scspec (loc, specs,
2347 c_parser_peek_token (parser)->value);
2348 c_parser_consume_token (parser);
2349 break;
2350 case RID_AUTO_TYPE:
2351 if (!auto_type_ok)
2352 goto out;
2353 /* Fall through. */
2354 case RID_UNSIGNED:
2355 case RID_LONG:
2356 case RID_SHORT:
2357 case RID_SIGNED:
2358 case RID_COMPLEX:
2359 case RID_INT:
2360 case RID_CHAR:
2361 case RID_FLOAT:
2362 case RID_DOUBLE:
2363 case RID_VOID:
2364 case RID_DFLOAT32:
2365 case RID_DFLOAT64:
2366 case RID_DFLOAT128:
2367 case RID_BOOL:
2368 case RID_FRACT:
2369 case RID_ACCUM:
2370 case RID_SAT:
2371 case RID_INT_N_0:
2372 case RID_INT_N_1:
2373 case RID_INT_N_2:
2374 case RID_INT_N_3:
2375 if (!typespec_ok)
2376 goto out;
2377 attrs_ok = true;
2378 seen_type = true;
2379 if (c_dialect_objc ())
2380 parser->objc_need_raw_identifier = true;
2381 t.kind = ctsk_resword;
2382 t.spec = c_parser_peek_token (parser)->value;
2383 t.expr = NULL_TREE;
2384 t.expr_const_operands = true;
2385 declspecs_add_type (loc, specs, t);
2386 c_parser_consume_token (parser);
2387 break;
2388 case RID_ENUM:
2389 if (!typespec_ok)
2390 goto out;
2391 attrs_ok = true;
2392 seen_type = true;
2393 t = c_parser_enum_specifier (parser);
2394 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2395 declspecs_add_type (loc, specs, t);
2396 break;
2397 case RID_STRUCT:
2398 case RID_UNION:
2399 if (!typespec_ok)
2400 goto out;
2401 attrs_ok = true;
2402 seen_type = true;
2403 t = c_parser_struct_or_union_specifier (parser);
2404 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2405 declspecs_add_type (loc, specs, t);
2406 break;
2407 case RID_TYPEOF:
2408 /* ??? The old parser rejected typeof after other type
2409 specifiers, but is a syntax error the best way of
2410 handling this? */
2411 if (!typespec_ok || seen_type)
2412 goto out;
2413 attrs_ok = true;
2414 seen_type = true;
2415 t = c_parser_typeof_specifier (parser);
2416 declspecs_add_type (loc, specs, t);
2417 break;
2418 case RID_ATOMIC:
2419 /* C parser handling of Objective-C constructs needs
2420 checking for correct lvalue-to-rvalue conversions, and
2421 the code in build_modify_expr handling various
2422 Objective-C cases, and that in build_unary_op handling
2423 Objective-C cases for increment / decrement, also needs
2424 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2425 and objc_types_are_equivalent may also need updates. */
2426 if (c_dialect_objc ())
2427 sorry ("%<_Atomic%> in Objective-C");
2428 /* C parser handling of OpenMP constructs needs checking for
2429 correct lvalue-to-rvalue conversions. */
2430 if (flag_openmp)
2431 sorry ("%<_Atomic%> with OpenMP");
2432 if (flag_isoc99)
2433 pedwarn_c99 (loc, OPT_Wpedantic,
2434 "ISO C99 does not support the %<_Atomic%> qualifier");
2435 else
2436 pedwarn_c99 (loc, OPT_Wpedantic,
2437 "ISO C90 does not support the %<_Atomic%> qualifier");
2438 attrs_ok = true;
2439 tree value;
2440 value = c_parser_peek_token (parser)->value;
2441 c_parser_consume_token (parser);
2442 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2444 /* _Atomic ( type-name ). */
2445 seen_type = true;
2446 c_parser_consume_token (parser);
2447 struct c_type_name *type = c_parser_type_name (parser);
2448 t.kind = ctsk_typeof;
2449 t.spec = error_mark_node;
2450 t.expr = NULL_TREE;
2451 t.expr_const_operands = true;
2452 if (type != NULL)
2453 t.spec = groktypename (type, &t.expr,
2454 &t.expr_const_operands);
2455 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2456 "expected %<)%>");
2457 if (t.spec != error_mark_node)
2459 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2460 error_at (loc, "%<_Atomic%>-qualified array type");
2461 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2462 error_at (loc, "%<_Atomic%>-qualified function type");
2463 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2464 error_at (loc, "%<_Atomic%> applied to a qualified type");
2465 else
2466 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2468 declspecs_add_type (loc, specs, t);
2470 else
2471 declspecs_add_qual (loc, specs, value);
2472 break;
2473 case RID_CONST:
2474 case RID_VOLATILE:
2475 case RID_RESTRICT:
2476 attrs_ok = true;
2477 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2478 c_parser_consume_token (parser);
2479 break;
2480 case RID_ATTRIBUTE:
2481 if (!attrs_ok)
2482 goto out;
2483 attrs = c_parser_attributes (parser);
2484 declspecs_add_attrs (loc, specs, attrs);
2485 break;
2486 case RID_ALIGNAS:
2487 if (!alignspec_ok)
2488 goto out;
2489 align = c_parser_alignas_specifier (parser);
2490 declspecs_add_alignas (loc, specs, align);
2491 break;
2492 default:
2493 goto out;
2496 out: ;
2499 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2501 enum-specifier:
2502 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2503 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2504 enum attributes[opt] identifier
2506 The form with trailing comma is new in C99. The forms with
2507 attributes are GNU extensions. In GNU C, we accept any expression
2508 without commas in the syntax (assignment expressions, not just
2509 conditional expressions); assignment expressions will be diagnosed
2510 as non-constant.
2512 enumerator-list:
2513 enumerator
2514 enumerator-list , enumerator
2516 enumerator:
2517 enumeration-constant
2518 enumeration-constant = constant-expression
2520 GNU Extensions:
2522 enumerator:
2523 enumeration-constant attributes[opt]
2524 enumeration-constant attributes[opt] = constant-expression
2528 static struct c_typespec
2529 c_parser_enum_specifier (c_parser *parser)
2531 struct c_typespec ret;
2532 tree attrs;
2533 tree ident = NULL_TREE;
2534 location_t enum_loc;
2535 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2536 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2537 enum_loc = c_parser_peek_token (parser)->location;
2538 c_parser_consume_token (parser);
2539 attrs = c_parser_attributes (parser);
2540 enum_loc = c_parser_peek_token (parser)->location;
2541 /* Set the location in case we create a decl now. */
2542 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2543 if (c_parser_next_token_is (parser, CPP_NAME))
2545 ident = c_parser_peek_token (parser)->value;
2546 ident_loc = c_parser_peek_token (parser)->location;
2547 enum_loc = ident_loc;
2548 c_parser_consume_token (parser);
2550 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2552 /* Parse an enum definition. */
2553 struct c_enum_contents the_enum;
2554 tree type;
2555 tree postfix_attrs;
2556 /* We chain the enumerators in reverse order, then put them in
2557 forward order at the end. */
2558 tree values;
2559 timevar_push (TV_PARSE_ENUM);
2560 type = start_enum (enum_loc, &the_enum, ident);
2561 values = NULL_TREE;
2562 c_parser_consume_token (parser);
2563 while (true)
2565 tree enum_id;
2566 tree enum_value;
2567 tree enum_decl;
2568 bool seen_comma;
2569 c_token *token;
2570 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2571 location_t decl_loc, value_loc;
2572 if (c_parser_next_token_is_not (parser, CPP_NAME))
2574 c_parser_error (parser, "expected identifier");
2575 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2576 values = error_mark_node;
2577 break;
2579 token = c_parser_peek_token (parser);
2580 enum_id = token->value;
2581 /* Set the location in case we create a decl now. */
2582 c_parser_set_source_position_from_token (token);
2583 decl_loc = value_loc = token->location;
2584 c_parser_consume_token (parser);
2585 /* Parse any specified attributes. */
2586 tree enum_attrs = c_parser_attributes (parser);
2587 if (c_parser_next_token_is (parser, CPP_EQ))
2589 c_parser_consume_token (parser);
2590 value_loc = c_parser_peek_token (parser)->location;
2591 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2593 else
2594 enum_value = NULL_TREE;
2595 enum_decl = build_enumerator (decl_loc, value_loc,
2596 &the_enum, enum_id, enum_value);
2597 if (enum_attrs)
2598 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2599 TREE_CHAIN (enum_decl) = values;
2600 values = enum_decl;
2601 seen_comma = false;
2602 if (c_parser_next_token_is (parser, CPP_COMMA))
2604 comma_loc = c_parser_peek_token (parser)->location;
2605 seen_comma = true;
2606 c_parser_consume_token (parser);
2608 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2610 if (seen_comma)
2611 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2612 "comma at end of enumerator list");
2613 c_parser_consume_token (parser);
2614 break;
2616 if (!seen_comma)
2618 c_parser_error (parser, "expected %<,%> or %<}%>");
2619 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2620 values = error_mark_node;
2621 break;
2624 postfix_attrs = c_parser_attributes (parser);
2625 ret.spec = finish_enum (type, nreverse (values),
2626 chainon (attrs, postfix_attrs));
2627 ret.kind = ctsk_tagdef;
2628 ret.expr = NULL_TREE;
2629 ret.expr_const_operands = true;
2630 timevar_pop (TV_PARSE_ENUM);
2631 return ret;
2633 else if (!ident)
2635 c_parser_error (parser, "expected %<{%>");
2636 ret.spec = error_mark_node;
2637 ret.kind = ctsk_tagref;
2638 ret.expr = NULL_TREE;
2639 ret.expr_const_operands = true;
2640 return ret;
2642 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2643 /* In ISO C, enumerated types can be referred to only if already
2644 defined. */
2645 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2647 gcc_assert (ident);
2648 pedwarn (enum_loc, OPT_Wpedantic,
2649 "ISO C forbids forward references to %<enum%> types");
2651 return ret;
2654 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2656 struct-or-union-specifier:
2657 struct-or-union attributes[opt] identifier[opt]
2658 { struct-contents } attributes[opt]
2659 struct-or-union attributes[opt] identifier
2661 struct-contents:
2662 struct-declaration-list
2664 struct-declaration-list:
2665 struct-declaration ;
2666 struct-declaration-list struct-declaration ;
2668 GNU extensions:
2670 struct-contents:
2671 empty
2672 struct-declaration
2673 struct-declaration-list struct-declaration
2675 struct-declaration-list:
2676 struct-declaration-list ;
2679 (Note that in the syntax here, unlike that in ISO C, the semicolons
2680 are included here rather than in struct-declaration, in order to
2681 describe the syntax with extra semicolons and missing semicolon at
2682 end.)
2684 Objective-C:
2686 struct-declaration-list:
2687 @defs ( class-name )
2689 (Note this does not include a trailing semicolon, but can be
2690 followed by further declarations, and gets a pedwarn-if-pedantic
2691 when followed by a semicolon.) */
2693 static struct c_typespec
2694 c_parser_struct_or_union_specifier (c_parser *parser)
2696 struct c_typespec ret;
2697 tree attrs;
2698 tree ident = NULL_TREE;
2699 location_t struct_loc;
2700 location_t ident_loc = UNKNOWN_LOCATION;
2701 enum tree_code code;
2702 switch (c_parser_peek_token (parser)->keyword)
2704 case RID_STRUCT:
2705 code = RECORD_TYPE;
2706 break;
2707 case RID_UNION:
2708 code = UNION_TYPE;
2709 break;
2710 default:
2711 gcc_unreachable ();
2713 struct_loc = c_parser_peek_token (parser)->location;
2714 c_parser_consume_token (parser);
2715 attrs = c_parser_attributes (parser);
2717 /* Set the location in case we create a decl now. */
2718 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2720 if (c_parser_next_token_is (parser, CPP_NAME))
2722 ident = c_parser_peek_token (parser)->value;
2723 ident_loc = c_parser_peek_token (parser)->location;
2724 struct_loc = ident_loc;
2725 c_parser_consume_token (parser);
2727 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2729 /* Parse a struct or union definition. Start the scope of the
2730 tag before parsing components. */
2731 struct c_struct_parse_info *struct_info;
2732 tree type = start_struct (struct_loc, code, ident, &struct_info);
2733 tree postfix_attrs;
2734 /* We chain the components in reverse order, then put them in
2735 forward order at the end. Each struct-declaration may
2736 declare multiple components (comma-separated), so we must use
2737 chainon to join them, although when parsing each
2738 struct-declaration we can use TREE_CHAIN directly.
2740 The theory behind all this is that there will be more
2741 semicolon separated fields than comma separated fields, and
2742 so we'll be minimizing the number of node traversals required
2743 by chainon. */
2744 tree contents;
2745 timevar_push (TV_PARSE_STRUCT);
2746 contents = NULL_TREE;
2747 c_parser_consume_token (parser);
2748 /* Handle the Objective-C @defs construct,
2749 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2750 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2752 tree name;
2753 gcc_assert (c_dialect_objc ());
2754 c_parser_consume_token (parser);
2755 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2756 goto end_at_defs;
2757 if (c_parser_next_token_is (parser, CPP_NAME)
2758 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2760 name = c_parser_peek_token (parser)->value;
2761 c_parser_consume_token (parser);
2763 else
2765 c_parser_error (parser, "expected class name");
2766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2767 goto end_at_defs;
2769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2770 "expected %<)%>");
2771 contents = nreverse (objc_get_class_ivars (name));
2773 end_at_defs:
2774 /* Parse the struct-declarations and semicolons. Problems with
2775 semicolons are diagnosed here; empty structures are diagnosed
2776 elsewhere. */
2777 while (true)
2779 tree decls;
2780 /* Parse any stray semicolon. */
2781 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2783 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2784 "extra semicolon in struct or union specified");
2785 c_parser_consume_token (parser);
2786 continue;
2788 /* Stop if at the end of the struct or union contents. */
2789 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2791 c_parser_consume_token (parser);
2792 break;
2794 /* Accept #pragmas at struct scope. */
2795 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2797 c_parser_pragma (parser, pragma_struct);
2798 continue;
2800 /* Parse some comma-separated declarations, but not the
2801 trailing semicolon if any. */
2802 decls = c_parser_struct_declaration (parser);
2803 contents = chainon (decls, contents);
2804 /* If no semicolon follows, either we have a parse error or
2805 are at the end of the struct or union and should
2806 pedwarn. */
2807 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2808 c_parser_consume_token (parser);
2809 else
2811 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2812 pedwarn (c_parser_peek_token (parser)->location, 0,
2813 "no semicolon at end of struct or union");
2814 else if (parser->error
2815 || !c_parser_next_token_starts_declspecs (parser))
2817 c_parser_error (parser, "expected %<;%>");
2818 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2819 break;
2822 /* If we come here, we have already emitted an error
2823 for an expected `;', identifier or `(', and we also
2824 recovered already. Go on with the next field. */
2827 postfix_attrs = c_parser_attributes (parser);
2828 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2829 chainon (attrs, postfix_attrs), struct_info);
2830 ret.kind = ctsk_tagdef;
2831 ret.expr = NULL_TREE;
2832 ret.expr_const_operands = true;
2833 timevar_pop (TV_PARSE_STRUCT);
2834 return ret;
2836 else if (!ident)
2838 c_parser_error (parser, "expected %<{%>");
2839 ret.spec = error_mark_node;
2840 ret.kind = ctsk_tagref;
2841 ret.expr = NULL_TREE;
2842 ret.expr_const_operands = true;
2843 return ret;
2845 ret = parser_xref_tag (ident_loc, code, ident);
2846 return ret;
2849 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2850 the trailing semicolon.
2852 struct-declaration:
2853 specifier-qualifier-list struct-declarator-list
2854 static_assert-declaration-no-semi
2856 specifier-qualifier-list:
2857 type-specifier specifier-qualifier-list[opt]
2858 type-qualifier specifier-qualifier-list[opt]
2859 attributes specifier-qualifier-list[opt]
2861 struct-declarator-list:
2862 struct-declarator
2863 struct-declarator-list , attributes[opt] struct-declarator
2865 struct-declarator:
2866 declarator attributes[opt]
2867 declarator[opt] : constant-expression attributes[opt]
2869 GNU extensions:
2871 struct-declaration:
2872 __extension__ struct-declaration
2873 specifier-qualifier-list
2875 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2876 of attributes where shown is a GNU extension. In GNU C, we accept
2877 any expression without commas in the syntax (assignment
2878 expressions, not just conditional expressions); assignment
2879 expressions will be diagnosed as non-constant. */
2881 static tree
2882 c_parser_struct_declaration (c_parser *parser)
2884 struct c_declspecs *specs;
2885 tree prefix_attrs;
2886 tree all_prefix_attrs;
2887 tree decls;
2888 location_t decl_loc;
2889 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2891 int ext;
2892 tree decl;
2893 ext = disable_extension_diagnostics ();
2894 c_parser_consume_token (parser);
2895 decl = c_parser_struct_declaration (parser);
2896 restore_extension_diagnostics (ext);
2897 return decl;
2899 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2901 c_parser_static_assert_declaration_no_semi (parser);
2902 return NULL_TREE;
2904 specs = build_null_declspecs ();
2905 decl_loc = c_parser_peek_token (parser)->location;
2906 /* Strictly by the standard, we shouldn't allow _Alignas here,
2907 but it appears to have been intended to allow it there, so
2908 we're keeping it as it is until WG14 reaches a conclusion
2909 of N1731.
2910 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2911 c_parser_declspecs (parser, specs, false, true, true,
2912 true, false, cla_nonabstract_decl);
2913 if (parser->error)
2914 return NULL_TREE;
2915 if (!specs->declspecs_seen_p)
2917 c_parser_error (parser, "expected specifier-qualifier-list");
2918 return NULL_TREE;
2920 finish_declspecs (specs);
2921 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2922 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2924 tree ret;
2925 if (specs->typespec_kind == ctsk_none)
2927 pedwarn (decl_loc, OPT_Wpedantic,
2928 "ISO C forbids member declarations with no members");
2929 shadow_tag_warned (specs, pedantic);
2930 ret = NULL_TREE;
2932 else
2934 /* Support for unnamed structs or unions as members of
2935 structs or unions (which is [a] useful and [b] supports
2936 MS P-SDK). */
2937 tree attrs = NULL;
2939 ret = grokfield (c_parser_peek_token (parser)->location,
2940 build_id_declarator (NULL_TREE), specs,
2941 NULL_TREE, &attrs);
2942 if (ret)
2943 decl_attributes (&ret, attrs, 0);
2945 return ret;
2948 /* Provide better error recovery. Note that a type name here is valid,
2949 and will be treated as a field name. */
2950 if (specs->typespec_kind == ctsk_tagdef
2951 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2952 && c_parser_next_token_starts_declspecs (parser)
2953 && !c_parser_next_token_is (parser, CPP_NAME))
2955 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2956 parser->error = false;
2957 return NULL_TREE;
2960 pending_xref_error ();
2961 prefix_attrs = specs->attrs;
2962 all_prefix_attrs = prefix_attrs;
2963 specs->attrs = NULL_TREE;
2964 decls = NULL_TREE;
2965 while (true)
2967 /* Declaring one or more declarators or un-named bit-fields. */
2968 struct c_declarator *declarator;
2969 bool dummy = false;
2970 if (c_parser_next_token_is (parser, CPP_COLON))
2971 declarator = build_id_declarator (NULL_TREE);
2972 else
2973 declarator = c_parser_declarator (parser,
2974 specs->typespec_kind != ctsk_none,
2975 C_DTR_NORMAL, &dummy);
2976 if (declarator == NULL)
2978 c_parser_skip_to_end_of_block_or_statement (parser);
2979 break;
2981 if (c_parser_next_token_is (parser, CPP_COLON)
2982 || c_parser_next_token_is (parser, CPP_COMMA)
2983 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2984 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2985 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2987 tree postfix_attrs = NULL_TREE;
2988 tree width = NULL_TREE;
2989 tree d;
2990 if (c_parser_next_token_is (parser, CPP_COLON))
2992 c_parser_consume_token (parser);
2993 width = c_parser_expr_no_commas (parser, NULL).value;
2995 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2996 postfix_attrs = c_parser_attributes (parser);
2997 d = grokfield (c_parser_peek_token (parser)->location,
2998 declarator, specs, width, &all_prefix_attrs);
2999 decl_attributes (&d, chainon (postfix_attrs,
3000 all_prefix_attrs), 0);
3001 DECL_CHAIN (d) = decls;
3002 decls = d;
3003 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3004 all_prefix_attrs = chainon (c_parser_attributes (parser),
3005 prefix_attrs);
3006 else
3007 all_prefix_attrs = prefix_attrs;
3008 if (c_parser_next_token_is (parser, CPP_COMMA))
3009 c_parser_consume_token (parser);
3010 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3011 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3013 /* Semicolon consumed in caller. */
3014 break;
3016 else
3018 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3019 break;
3022 else
3024 c_parser_error (parser,
3025 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3026 "%<__attribute__%>");
3027 break;
3030 return decls;
3033 /* Parse a typeof specifier (a GNU extension).
3035 typeof-specifier:
3036 typeof ( expression )
3037 typeof ( type-name )
3040 static struct c_typespec
3041 c_parser_typeof_specifier (c_parser *parser)
3043 struct c_typespec ret;
3044 ret.kind = ctsk_typeof;
3045 ret.spec = error_mark_node;
3046 ret.expr = NULL_TREE;
3047 ret.expr_const_operands = true;
3048 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3049 c_parser_consume_token (parser);
3050 c_inhibit_evaluation_warnings++;
3051 in_typeof++;
3052 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3054 c_inhibit_evaluation_warnings--;
3055 in_typeof--;
3056 return ret;
3058 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3060 struct c_type_name *type = c_parser_type_name (parser);
3061 c_inhibit_evaluation_warnings--;
3062 in_typeof--;
3063 if (type != NULL)
3065 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3066 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3069 else
3071 bool was_vm;
3072 location_t here = c_parser_peek_token (parser)->location;
3073 struct c_expr expr = c_parser_expression (parser);
3074 c_inhibit_evaluation_warnings--;
3075 in_typeof--;
3076 if (TREE_CODE (expr.value) == COMPONENT_REF
3077 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3078 error_at (here, "%<typeof%> applied to a bit-field");
3079 mark_exp_read (expr.value);
3080 ret.spec = TREE_TYPE (expr.value);
3081 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3082 /* This is returned with the type so that when the type is
3083 evaluated, this can be evaluated. */
3084 if (was_vm)
3085 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3086 pop_maybe_used (was_vm);
3087 /* For use in macros such as those in <stdatomic.h>, remove all
3088 qualifiers from atomic types. (const can be an issue for more macros
3089 using typeof than just the <stdatomic.h> ones.) */
3090 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3091 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3093 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3094 return ret;
3097 /* Parse an alignment-specifier.
3099 C11 6.7.5:
3101 alignment-specifier:
3102 _Alignas ( type-name )
3103 _Alignas ( constant-expression )
3106 static tree
3107 c_parser_alignas_specifier (c_parser * parser)
3109 tree ret = error_mark_node;
3110 location_t loc = c_parser_peek_token (parser)->location;
3111 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3112 c_parser_consume_token (parser);
3113 if (flag_isoc99)
3114 pedwarn_c99 (loc, OPT_Wpedantic,
3115 "ISO C99 does not support %<_Alignas%>");
3116 else
3117 pedwarn_c99 (loc, OPT_Wpedantic,
3118 "ISO C90 does not support %<_Alignas%>");
3119 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3120 return ret;
3121 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3123 struct c_type_name *type = c_parser_type_name (parser);
3124 if (type != NULL)
3125 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3126 false, true, 1);
3128 else
3129 ret = c_parser_expr_no_commas (parser, NULL).value;
3130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3131 return ret;
3134 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3135 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3136 be redeclared; otherwise it may not. KIND indicates which kind of
3137 declarator is wanted. Returns a valid declarator except in the
3138 case of a syntax error in which case NULL is returned. *SEEN_ID is
3139 set to true if an identifier being declared is seen; this is used
3140 to diagnose bad forms of abstract array declarators and to
3141 determine whether an identifier list is syntactically permitted.
3143 declarator:
3144 pointer[opt] direct-declarator
3146 direct-declarator:
3147 identifier
3148 ( attributes[opt] declarator )
3149 direct-declarator array-declarator
3150 direct-declarator ( parameter-type-list )
3151 direct-declarator ( identifier-list[opt] )
3153 pointer:
3154 * type-qualifier-list[opt]
3155 * type-qualifier-list[opt] pointer
3157 type-qualifier-list:
3158 type-qualifier
3159 attributes
3160 type-qualifier-list type-qualifier
3161 type-qualifier-list attributes
3163 array-declarator:
3164 [ type-qualifier-list[opt] assignment-expression[opt] ]
3165 [ static type-qualifier-list[opt] assignment-expression ]
3166 [ type-qualifier-list static assignment-expression ]
3167 [ type-qualifier-list[opt] * ]
3169 parameter-type-list:
3170 parameter-list
3171 parameter-list , ...
3173 parameter-list:
3174 parameter-declaration
3175 parameter-list , parameter-declaration
3177 parameter-declaration:
3178 declaration-specifiers declarator attributes[opt]
3179 declaration-specifiers abstract-declarator[opt] attributes[opt]
3181 identifier-list:
3182 identifier
3183 identifier-list , identifier
3185 abstract-declarator:
3186 pointer
3187 pointer[opt] direct-abstract-declarator
3189 direct-abstract-declarator:
3190 ( attributes[opt] abstract-declarator )
3191 direct-abstract-declarator[opt] array-declarator
3192 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3194 GNU extensions:
3196 direct-declarator:
3197 direct-declarator ( parameter-forward-declarations
3198 parameter-type-list[opt] )
3200 direct-abstract-declarator:
3201 direct-abstract-declarator[opt] ( parameter-forward-declarations
3202 parameter-type-list[opt] )
3204 parameter-forward-declarations:
3205 parameter-list ;
3206 parameter-forward-declarations parameter-list ;
3208 The uses of attributes shown above are GNU extensions.
3210 Some forms of array declarator are not included in C99 in the
3211 syntax for abstract declarators; these are disallowed elsewhere.
3212 This may be a defect (DR#289).
3214 This function also accepts an omitted abstract declarator as being
3215 an abstract declarator, although not part of the formal syntax. */
3217 static struct c_declarator *
3218 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3219 bool *seen_id)
3221 /* Parse any initial pointer part. */
3222 if (c_parser_next_token_is (parser, CPP_MULT))
3224 struct c_declspecs *quals_attrs = build_null_declspecs ();
3225 struct c_declarator *inner;
3226 c_parser_consume_token (parser);
3227 c_parser_declspecs (parser, quals_attrs, false, false, true,
3228 false, false, cla_prefer_id);
3229 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3230 if (inner == NULL)
3231 return NULL;
3232 else
3233 return make_pointer_declarator (quals_attrs, inner);
3235 /* Now we have a direct declarator, direct abstract declarator or
3236 nothing (which counts as a direct abstract declarator here). */
3237 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3240 /* Parse a direct declarator or direct abstract declarator; arguments
3241 as c_parser_declarator. */
3243 static struct c_declarator *
3244 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3245 bool *seen_id)
3247 /* The direct declarator must start with an identifier (possibly
3248 omitted) or a parenthesized declarator (possibly abstract). In
3249 an ordinary declarator, initial parentheses must start a
3250 parenthesized declarator. In an abstract declarator or parameter
3251 declarator, they could start a parenthesized declarator or a
3252 parameter list. To tell which, the open parenthesis and any
3253 following attributes must be read. If a declaration specifier
3254 follows, then it is a parameter list; if the specifier is a
3255 typedef name, there might be an ambiguity about redeclaring it,
3256 which is resolved in the direction of treating it as a typedef
3257 name. If a close parenthesis follows, it is also an empty
3258 parameter list, as the syntax does not permit empty abstract
3259 declarators. Otherwise, it is a parenthesized declarator (in
3260 which case the analysis may be repeated inside it, recursively).
3262 ??? There is an ambiguity in a parameter declaration "int
3263 (__attribute__((foo)) x)", where x is not a typedef name: it
3264 could be an abstract declarator for a function, or declare x with
3265 parentheses. The proper resolution of this ambiguity needs
3266 documenting. At present we follow an accident of the old
3267 parser's implementation, whereby the first parameter must have
3268 some declaration specifiers other than just attributes. Thus as
3269 a parameter declaration it is treated as a parenthesized
3270 parameter named x, and as an abstract declarator it is
3271 rejected.
3273 ??? Also following the old parser, attributes inside an empty
3274 parameter list are ignored, making it a list not yielding a
3275 prototype, rather than giving an error or making it have one
3276 parameter with implicit type int.
3278 ??? Also following the old parser, typedef names may be
3279 redeclared in declarators, but not Objective-C class names. */
3281 if (kind != C_DTR_ABSTRACT
3282 && c_parser_next_token_is (parser, CPP_NAME)
3283 && ((type_seen_p
3284 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3285 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3286 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3288 struct c_declarator *inner
3289 = build_id_declarator (c_parser_peek_token (parser)->value);
3290 *seen_id = true;
3291 inner->id_loc = c_parser_peek_token (parser)->location;
3292 c_parser_consume_token (parser);
3293 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3296 if (kind != C_DTR_NORMAL
3297 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3299 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3300 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3303 /* Either we are at the end of an abstract declarator, or we have
3304 parentheses. */
3306 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3308 tree attrs;
3309 struct c_declarator *inner;
3310 c_parser_consume_token (parser);
3311 attrs = c_parser_attributes (parser);
3312 if (kind != C_DTR_NORMAL
3313 && (c_parser_next_token_starts_declspecs (parser)
3314 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3316 struct c_arg_info *args
3317 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3318 attrs);
3319 if (args == NULL)
3320 return NULL;
3321 else
3323 inner
3324 = build_function_declarator (args,
3325 build_id_declarator (NULL_TREE));
3326 return c_parser_direct_declarator_inner (parser, *seen_id,
3327 inner);
3330 /* A parenthesized declarator. */
3331 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3332 if (inner != NULL && attrs != NULL)
3333 inner = build_attrs_declarator (attrs, inner);
3334 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3336 c_parser_consume_token (parser);
3337 if (inner == NULL)
3338 return NULL;
3339 else
3340 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3342 else
3344 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3345 "expected %<)%>");
3346 return NULL;
3349 else
3351 if (kind == C_DTR_NORMAL)
3353 c_parser_error (parser, "expected identifier or %<(%>");
3354 return NULL;
3356 else
3357 return build_id_declarator (NULL_TREE);
3361 /* Parse part of a direct declarator or direct abstract declarator,
3362 given that some (in INNER) has already been parsed; ID_PRESENT is
3363 true if an identifier is present, false for an abstract
3364 declarator. */
3366 static struct c_declarator *
3367 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3368 struct c_declarator *inner)
3370 /* Parse a sequence of array declarators and parameter lists. */
3371 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3373 location_t brace_loc = c_parser_peek_token (parser)->location;
3374 struct c_declarator *declarator;
3375 struct c_declspecs *quals_attrs = build_null_declspecs ();
3376 bool static_seen;
3377 bool star_seen;
3378 struct c_expr dimen;
3379 dimen.value = NULL_TREE;
3380 dimen.original_code = ERROR_MARK;
3381 dimen.original_type = NULL_TREE;
3382 c_parser_consume_token (parser);
3383 c_parser_declspecs (parser, quals_attrs, false, false, true,
3384 false, false, cla_prefer_id);
3385 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3386 if (static_seen)
3387 c_parser_consume_token (parser);
3388 if (static_seen && !quals_attrs->declspecs_seen_p)
3389 c_parser_declspecs (parser, quals_attrs, false, false, true,
3390 false, false, cla_prefer_id);
3391 if (!quals_attrs->declspecs_seen_p)
3392 quals_attrs = NULL;
3393 /* If "static" is present, there must be an array dimension.
3394 Otherwise, there may be a dimension, "*", or no
3395 dimension. */
3396 if (static_seen)
3398 star_seen = false;
3399 dimen = c_parser_expr_no_commas (parser, NULL);
3401 else
3403 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3405 dimen.value = NULL_TREE;
3406 star_seen = false;
3408 else if (flag_cilkplus
3409 && c_parser_next_token_is (parser, CPP_COLON))
3411 dimen.value = error_mark_node;
3412 star_seen = false;
3413 error_at (c_parser_peek_token (parser)->location,
3414 "array notations cannot be used in declaration");
3415 c_parser_consume_token (parser);
3417 else if (c_parser_next_token_is (parser, CPP_MULT))
3419 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3421 dimen.value = NULL_TREE;
3422 star_seen = true;
3423 c_parser_consume_token (parser);
3425 else
3427 star_seen = false;
3428 dimen = c_parser_expr_no_commas (parser, NULL);
3431 else
3433 star_seen = false;
3434 dimen = c_parser_expr_no_commas (parser, NULL);
3437 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3438 c_parser_consume_token (parser);
3439 else if (flag_cilkplus
3440 && c_parser_next_token_is (parser, CPP_COLON))
3442 error_at (c_parser_peek_token (parser)->location,
3443 "array notations cannot be used in declaration");
3444 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3445 return NULL;
3447 else
3449 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3450 "expected %<]%>");
3451 return NULL;
3453 if (dimen.value)
3454 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3455 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3456 static_seen, star_seen);
3457 if (declarator == NULL)
3458 return NULL;
3459 inner = set_array_declarator_inner (declarator, inner);
3460 return c_parser_direct_declarator_inner (parser, id_present, inner);
3462 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3464 tree attrs;
3465 struct c_arg_info *args;
3466 c_parser_consume_token (parser);
3467 attrs = c_parser_attributes (parser);
3468 args = c_parser_parms_declarator (parser, id_present, attrs);
3469 if (args == NULL)
3470 return NULL;
3471 else
3473 inner = build_function_declarator (args, inner);
3474 return c_parser_direct_declarator_inner (parser, id_present, inner);
3477 return inner;
3480 /* Parse a parameter list or identifier list, including the closing
3481 parenthesis but not the opening one. ATTRS are the attributes at
3482 the start of the list. ID_LIST_OK is true if an identifier list is
3483 acceptable; such a list must not have attributes at the start. */
3485 static struct c_arg_info *
3486 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3488 push_scope ();
3489 declare_parm_level ();
3490 /* If the list starts with an identifier, it is an identifier list.
3491 Otherwise, it is either a prototype list or an empty list. */
3492 if (id_list_ok
3493 && !attrs
3494 && c_parser_next_token_is (parser, CPP_NAME)
3495 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3497 /* Look ahead to detect typos in type names. */
3498 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3499 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3500 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3501 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3503 tree list = NULL_TREE, *nextp = &list;
3504 while (c_parser_next_token_is (parser, CPP_NAME)
3505 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3507 *nextp = build_tree_list (NULL_TREE,
3508 c_parser_peek_token (parser)->value);
3509 nextp = & TREE_CHAIN (*nextp);
3510 c_parser_consume_token (parser);
3511 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3512 break;
3513 c_parser_consume_token (parser);
3514 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3516 c_parser_error (parser, "expected identifier");
3517 break;
3520 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3522 struct c_arg_info *ret = build_arg_info ();
3523 ret->types = list;
3524 c_parser_consume_token (parser);
3525 pop_scope ();
3526 return ret;
3528 else
3530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3531 "expected %<)%>");
3532 pop_scope ();
3533 return NULL;
3536 else
3538 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3539 NULL);
3540 pop_scope ();
3541 return ret;
3545 /* Parse a parameter list (possibly empty), including the closing
3546 parenthesis but not the opening one. ATTRS are the attributes at
3547 the start of the list. EXPR is NULL or an expression that needs to
3548 be evaluated for the side effects of array size expressions in the
3549 parameters. */
3551 static struct c_arg_info *
3552 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3554 bool bad_parm = false;
3556 /* ??? Following the old parser, forward parameter declarations may
3557 use abstract declarators, and if no real parameter declarations
3558 follow the forward declarations then this is not diagnosed. Also
3559 note as above that attributes are ignored as the only contents of
3560 the parentheses, or as the only contents after forward
3561 declarations. */
3562 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3564 struct c_arg_info *ret = build_arg_info ();
3565 c_parser_consume_token (parser);
3566 return ret;
3568 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3570 struct c_arg_info *ret = build_arg_info ();
3572 if (flag_allow_parameterless_variadic_functions)
3574 /* F (...) is allowed. */
3575 ret->types = NULL_TREE;
3577 else
3579 /* Suppress -Wold-style-definition for this case. */
3580 ret->types = error_mark_node;
3581 error_at (c_parser_peek_token (parser)->location,
3582 "ISO C requires a named argument before %<...%>");
3584 c_parser_consume_token (parser);
3585 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3587 c_parser_consume_token (parser);
3588 return ret;
3590 else
3592 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3593 "expected %<)%>");
3594 return NULL;
3597 /* Nonempty list of parameters, either terminated with semicolon
3598 (forward declarations; recurse) or with close parenthesis (normal
3599 function) or with ", ... )" (variadic function). */
3600 while (true)
3602 /* Parse a parameter. */
3603 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3604 attrs = NULL_TREE;
3605 if (parm == NULL)
3606 bad_parm = true;
3607 else
3608 push_parm_decl (parm, &expr);
3609 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3611 tree new_attrs;
3612 c_parser_consume_token (parser);
3613 mark_forward_parm_decls ();
3614 new_attrs = c_parser_attributes (parser);
3615 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3617 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3619 c_parser_consume_token (parser);
3620 if (bad_parm)
3621 return NULL;
3622 else
3623 return get_parm_info (false, expr);
3625 if (!c_parser_require (parser, CPP_COMMA,
3626 "expected %<;%>, %<,%> or %<)%>"))
3628 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3629 return NULL;
3631 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3633 c_parser_consume_token (parser);
3634 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3636 c_parser_consume_token (parser);
3637 if (bad_parm)
3638 return NULL;
3639 else
3640 return get_parm_info (true, expr);
3642 else
3644 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3645 "expected %<)%>");
3646 return NULL;
3652 /* Parse a parameter declaration. ATTRS are the attributes at the
3653 start of the declaration if it is the first parameter. */
3655 static struct c_parm *
3656 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3658 struct c_declspecs *specs;
3659 struct c_declarator *declarator;
3660 tree prefix_attrs;
3661 tree postfix_attrs = NULL_TREE;
3662 bool dummy = false;
3664 /* Accept #pragmas between parameter declarations. */
3665 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3666 c_parser_pragma (parser, pragma_param);
3668 if (!c_parser_next_token_starts_declspecs (parser))
3670 c_token *token = c_parser_peek_token (parser);
3671 if (parser->error)
3672 return NULL;
3673 c_parser_set_source_position_from_token (token);
3674 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3676 error_at (token->location, "unknown type name %qE", token->value);
3677 parser->error = true;
3679 /* ??? In some Objective-C cases '...' isn't applicable so there
3680 should be a different message. */
3681 else
3682 c_parser_error (parser,
3683 "expected declaration specifiers or %<...%>");
3684 c_parser_skip_to_end_of_parameter (parser);
3685 return NULL;
3687 specs = build_null_declspecs ();
3688 if (attrs)
3690 declspecs_add_attrs (input_location, specs, attrs);
3691 attrs = NULL_TREE;
3693 c_parser_declspecs (parser, specs, true, true, true, true, false,
3694 cla_nonabstract_decl);
3695 finish_declspecs (specs);
3696 pending_xref_error ();
3697 prefix_attrs = specs->attrs;
3698 specs->attrs = NULL_TREE;
3699 declarator = c_parser_declarator (parser,
3700 specs->typespec_kind != ctsk_none,
3701 C_DTR_PARM, &dummy);
3702 if (declarator == NULL)
3704 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3705 return NULL;
3707 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3708 postfix_attrs = c_parser_attributes (parser);
3709 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3710 declarator);
3713 /* Parse a string literal in an asm expression. It should not be
3714 translated, and wide string literals are an error although
3715 permitted by the syntax. This is a GNU extension.
3717 asm-string-literal:
3718 string-literal
3720 ??? At present, following the old parser, the caller needs to have
3721 set lex_untranslated_string to 1. It would be better to follow the
3722 C++ parser rather than using this kludge. */
3724 static tree
3725 c_parser_asm_string_literal (c_parser *parser)
3727 tree str;
3728 int save_flag = warn_overlength_strings;
3729 warn_overlength_strings = 0;
3730 if (c_parser_next_token_is (parser, CPP_STRING))
3732 str = c_parser_peek_token (parser)->value;
3733 c_parser_consume_token (parser);
3735 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3737 error_at (c_parser_peek_token (parser)->location,
3738 "wide string literal in %<asm%>");
3739 str = build_string (1, "");
3740 c_parser_consume_token (parser);
3742 else
3744 c_parser_error (parser, "expected string literal");
3745 str = NULL_TREE;
3747 warn_overlength_strings = save_flag;
3748 return str;
3751 /* Parse a simple asm expression. This is used in restricted
3752 contexts, where a full expression with inputs and outputs does not
3753 make sense. This is a GNU extension.
3755 simple-asm-expr:
3756 asm ( asm-string-literal )
3759 static tree
3760 c_parser_simple_asm_expr (c_parser *parser)
3762 tree str;
3763 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3764 /* ??? Follow the C++ parser rather than using the
3765 lex_untranslated_string kludge. */
3766 parser->lex_untranslated_string = true;
3767 c_parser_consume_token (parser);
3768 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3770 parser->lex_untranslated_string = false;
3771 return NULL_TREE;
3773 str = c_parser_asm_string_literal (parser);
3774 parser->lex_untranslated_string = false;
3775 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3777 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3778 return NULL_TREE;
3780 return str;
3783 static tree
3784 c_parser_attribute_any_word (c_parser *parser)
3786 tree attr_name = NULL_TREE;
3788 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3790 /* ??? See comment above about what keywords are accepted here. */
3791 bool ok;
3792 switch (c_parser_peek_token (parser)->keyword)
3794 case RID_STATIC:
3795 case RID_UNSIGNED:
3796 case RID_LONG:
3797 case RID_CONST:
3798 case RID_EXTERN:
3799 case RID_REGISTER:
3800 case RID_TYPEDEF:
3801 case RID_SHORT:
3802 case RID_INLINE:
3803 case RID_NORETURN:
3804 case RID_VOLATILE:
3805 case RID_SIGNED:
3806 case RID_AUTO:
3807 case RID_RESTRICT:
3808 case RID_COMPLEX:
3809 case RID_THREAD:
3810 case RID_INT:
3811 case RID_CHAR:
3812 case RID_FLOAT:
3813 case RID_DOUBLE:
3814 case RID_VOID:
3815 case RID_DFLOAT32:
3816 case RID_DFLOAT64:
3817 case RID_DFLOAT128:
3818 case RID_BOOL:
3819 case RID_FRACT:
3820 case RID_ACCUM:
3821 case RID_SAT:
3822 case RID_TRANSACTION_ATOMIC:
3823 case RID_TRANSACTION_CANCEL:
3824 case RID_ATOMIC:
3825 case RID_AUTO_TYPE:
3826 case RID_INT_N_0:
3827 case RID_INT_N_1:
3828 case RID_INT_N_2:
3829 case RID_INT_N_3:
3830 ok = true;
3831 break;
3832 default:
3833 ok = false;
3834 break;
3836 if (!ok)
3837 return NULL_TREE;
3839 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3840 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3842 else if (c_parser_next_token_is (parser, CPP_NAME))
3843 attr_name = c_parser_peek_token (parser)->value;
3845 return attr_name;
3848 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3849 "__vector" or "__vector__." */
3851 static inline bool
3852 is_cilkplus_vector_p (tree name)
3854 if (flag_cilkplus && is_attribute_p ("vector", name))
3855 return true;
3856 return false;
3859 #define CILK_SIMD_FN_CLAUSE_MASK \
3860 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3861 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3862 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3863 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3864 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3866 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3867 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3868 pushed into the token list.
3869 Syntax:
3870 vector
3871 vector (<vector attributes>). */
3873 static void
3874 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3876 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3878 int paren_scope = 0;
3879 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3880 /* Consume the "vector" token. */
3881 c_parser_consume_token (parser);
3883 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3885 c_parser_consume_token (parser);
3886 paren_scope++;
3888 while (paren_scope > 0)
3890 c_token *token = c_parser_peek_token (parser);
3891 if (token->type == CPP_OPEN_PAREN)
3892 paren_scope++;
3893 else if (token->type == CPP_CLOSE_PAREN)
3894 paren_scope--;
3895 /* Do not push the last ')' since we are not pushing the '('. */
3896 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3897 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3898 c_parser_consume_token (parser);
3901 /* Since we are converting an attribute to a pragma, we need to end the
3902 attribute with PRAGMA_EOL. */
3903 c_token eol_token;
3904 memset (&eol_token, 0, sizeof (eol_token));
3905 eol_token.type = CPP_PRAGMA_EOL;
3906 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3909 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3911 static void
3912 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3914 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3916 /* c_parser_attributes is called in several places, so if these EOF
3917 tokens are already inserted, then don't do them again. */
3918 if (last_token.type == CPP_EOF)
3919 return;
3921 /* Two CPP_EOF token are added as a safety net since the normal C
3922 front-end has two token look-ahead. */
3923 c_token eof_token;
3924 eof_token.type = CPP_EOF;
3925 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3926 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3929 /* Parse (possibly empty) attributes. This is a GNU extension.
3931 attributes:
3932 empty
3933 attributes attribute
3935 attribute:
3936 __attribute__ ( ( attribute-list ) )
3938 attribute-list:
3939 attrib
3940 attribute_list , attrib
3942 attrib:
3943 empty
3944 any-word
3945 any-word ( identifier )
3946 any-word ( identifier , nonempty-expr-list )
3947 any-word ( expr-list )
3949 where the "identifier" must not be declared as a type, and
3950 "any-word" may be any identifier (including one declared as a
3951 type), a reserved word storage class specifier, type specifier or
3952 type qualifier. ??? This still leaves out most reserved keywords
3953 (following the old parser), shouldn't we include them, and why not
3954 allow identifiers declared as types to start the arguments? */
3956 static tree
3957 c_parser_attributes (c_parser *parser)
3959 tree attrs = NULL_TREE;
3960 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3962 /* ??? Follow the C++ parser rather than using the
3963 lex_untranslated_string kludge. */
3964 parser->lex_untranslated_string = true;
3965 c_parser_consume_token (parser);
3966 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3968 parser->lex_untranslated_string = false;
3969 return attrs;
3971 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3973 parser->lex_untranslated_string = false;
3974 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3975 return attrs;
3977 /* Parse the attribute list. */
3978 while (c_parser_next_token_is (parser, CPP_COMMA)
3979 || c_parser_next_token_is (parser, CPP_NAME)
3980 || c_parser_next_token_is (parser, CPP_KEYWORD))
3982 tree attr, attr_name, attr_args;
3983 vec<tree, va_gc> *expr_list;
3984 if (c_parser_next_token_is (parser, CPP_COMMA))
3986 c_parser_consume_token (parser);
3987 continue;
3990 attr_name = c_parser_attribute_any_word (parser);
3991 if (attr_name == NULL)
3992 break;
3993 if (is_cilkplus_vector_p (attr_name))
3995 c_token *v_token = c_parser_peek_token (parser);
3996 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3997 continue;
3999 c_parser_consume_token (parser);
4000 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4002 attr = build_tree_list (attr_name, NULL_TREE);
4003 attrs = chainon (attrs, attr);
4004 continue;
4006 c_parser_consume_token (parser);
4007 /* Parse the attribute contents. If they start with an
4008 identifier which is followed by a comma or close
4009 parenthesis, then the arguments start with that
4010 identifier; otherwise they are an expression list.
4011 In objective-c the identifier may be a classname. */
4012 if (c_parser_next_token_is (parser, CPP_NAME)
4013 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4014 || (c_dialect_objc ()
4015 && c_parser_peek_token (parser)->id_kind
4016 == C_ID_CLASSNAME))
4017 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4018 || (c_parser_peek_2nd_token (parser)->type
4019 == CPP_CLOSE_PAREN))
4020 && (attribute_takes_identifier_p (attr_name)
4021 || (c_dialect_objc ()
4022 && c_parser_peek_token (parser)->id_kind
4023 == C_ID_CLASSNAME)))
4025 tree arg1 = c_parser_peek_token (parser)->value;
4026 c_parser_consume_token (parser);
4027 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4028 attr_args = build_tree_list (NULL_TREE, arg1);
4029 else
4031 tree tree_list;
4032 c_parser_consume_token (parser);
4033 expr_list = c_parser_expr_list (parser, false, true,
4034 NULL, NULL, NULL, NULL);
4035 tree_list = build_tree_list_vec (expr_list);
4036 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4037 release_tree_vector (expr_list);
4040 else
4042 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4043 attr_args = NULL_TREE;
4044 else
4046 expr_list = c_parser_expr_list (parser, false, true,
4047 NULL, NULL, NULL, NULL);
4048 attr_args = build_tree_list_vec (expr_list);
4049 release_tree_vector (expr_list);
4052 attr = build_tree_list (attr_name, attr_args);
4053 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4054 c_parser_consume_token (parser);
4055 else
4057 parser->lex_untranslated_string = false;
4058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4059 "expected %<)%>");
4060 return attrs;
4062 attrs = chainon (attrs, attr);
4064 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4065 c_parser_consume_token (parser);
4066 else
4068 parser->lex_untranslated_string = false;
4069 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4070 "expected %<)%>");
4071 return attrs;
4073 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4074 c_parser_consume_token (parser);
4075 else
4077 parser->lex_untranslated_string = false;
4078 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4079 "expected %<)%>");
4080 return attrs;
4082 parser->lex_untranslated_string = false;
4085 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4086 c_finish_cilk_simd_fn_tokens (parser);
4087 return attrs;
4090 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4092 type-name:
4093 specifier-qualifier-list abstract-declarator[opt]
4096 static struct c_type_name *
4097 c_parser_type_name (c_parser *parser)
4099 struct c_declspecs *specs = build_null_declspecs ();
4100 struct c_declarator *declarator;
4101 struct c_type_name *ret;
4102 bool dummy = false;
4103 c_parser_declspecs (parser, specs, false, true, true, false, false,
4104 cla_prefer_type);
4105 if (!specs->declspecs_seen_p)
4107 c_parser_error (parser, "expected specifier-qualifier-list");
4108 return NULL;
4110 if (specs->type != error_mark_node)
4112 pending_xref_error ();
4113 finish_declspecs (specs);
4115 declarator = c_parser_declarator (parser,
4116 specs->typespec_kind != ctsk_none,
4117 C_DTR_ABSTRACT, &dummy);
4118 if (declarator == NULL)
4119 return NULL;
4120 ret = XOBNEW (&parser_obstack, struct c_type_name);
4121 ret->specs = specs;
4122 ret->declarator = declarator;
4123 return ret;
4126 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4128 initializer:
4129 assignment-expression
4130 { initializer-list }
4131 { initializer-list , }
4133 initializer-list:
4134 designation[opt] initializer
4135 initializer-list , designation[opt] initializer
4137 designation:
4138 designator-list =
4140 designator-list:
4141 designator
4142 designator-list designator
4144 designator:
4145 array-designator
4146 . identifier
4148 array-designator:
4149 [ constant-expression ]
4151 GNU extensions:
4153 initializer:
4156 designation:
4157 array-designator
4158 identifier :
4160 array-designator:
4161 [ constant-expression ... constant-expression ]
4163 Any expression without commas is accepted in the syntax for the
4164 constant-expressions, with non-constant expressions rejected later.
4166 This function is only used for top-level initializers; for nested
4167 ones, see c_parser_initval. */
4169 static struct c_expr
4170 c_parser_initializer (c_parser *parser)
4172 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4173 return c_parser_braced_init (parser, NULL_TREE, false);
4174 else
4176 struct c_expr ret;
4177 location_t loc = c_parser_peek_token (parser)->location;
4178 ret = c_parser_expr_no_commas (parser, NULL);
4179 if (TREE_CODE (ret.value) != STRING_CST
4180 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4181 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4182 return ret;
4186 /* Parse a braced initializer list. TYPE is the type specified for a
4187 compound literal, and NULL_TREE for other initializers and for
4188 nested braced lists. NESTED_P is true for nested braced lists,
4189 false for the list of a compound literal or the list that is the
4190 top-level initializer in a declaration. */
4192 static struct c_expr
4193 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4195 struct c_expr ret;
4196 struct obstack braced_init_obstack;
4197 location_t brace_loc = c_parser_peek_token (parser)->location;
4198 gcc_obstack_init (&braced_init_obstack);
4199 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4200 c_parser_consume_token (parser);
4201 if (nested_p)
4202 push_init_level (brace_loc, 0, &braced_init_obstack);
4203 else
4204 really_start_incremental_init (type);
4205 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4207 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4209 else
4211 /* Parse a non-empty initializer list, possibly with a trailing
4212 comma. */
4213 while (true)
4215 c_parser_initelt (parser, &braced_init_obstack);
4216 if (parser->error)
4217 break;
4218 if (c_parser_next_token_is (parser, CPP_COMMA))
4219 c_parser_consume_token (parser);
4220 else
4221 break;
4222 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4223 break;
4226 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4228 ret.value = error_mark_node;
4229 ret.original_code = ERROR_MARK;
4230 ret.original_type = NULL;
4231 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4232 pop_init_level (brace_loc, 0, &braced_init_obstack);
4233 obstack_free (&braced_init_obstack, NULL);
4234 return ret;
4236 c_parser_consume_token (parser);
4237 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4238 obstack_free (&braced_init_obstack, NULL);
4239 return ret;
4242 /* Parse a nested initializer, including designators. */
4244 static void
4245 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4247 /* Parse any designator or designator list. A single array
4248 designator may have the subsequent "=" omitted in GNU C, but a
4249 longer list or a structure member designator may not. */
4250 if (c_parser_next_token_is (parser, CPP_NAME)
4251 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4253 /* Old-style structure member designator. */
4254 set_init_label (c_parser_peek_token (parser)->location,
4255 c_parser_peek_token (parser)->value,
4256 braced_init_obstack);
4257 /* Use the colon as the error location. */
4258 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4259 "obsolete use of designated initializer with %<:%>");
4260 c_parser_consume_token (parser);
4261 c_parser_consume_token (parser);
4263 else
4265 /* des_seen is 0 if there have been no designators, 1 if there
4266 has been a single array designator and 2 otherwise. */
4267 int des_seen = 0;
4268 /* Location of a designator. */
4269 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4270 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4271 || c_parser_next_token_is (parser, CPP_DOT))
4273 int des_prev = des_seen;
4274 if (!des_seen)
4275 des_loc = c_parser_peek_token (parser)->location;
4276 if (des_seen < 2)
4277 des_seen++;
4278 if (c_parser_next_token_is (parser, CPP_DOT))
4280 des_seen = 2;
4281 c_parser_consume_token (parser);
4282 if (c_parser_next_token_is (parser, CPP_NAME))
4284 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4285 braced_init_obstack);
4286 c_parser_consume_token (parser);
4288 else
4290 struct c_expr init;
4291 init.value = error_mark_node;
4292 init.original_code = ERROR_MARK;
4293 init.original_type = NULL;
4294 c_parser_error (parser, "expected identifier");
4295 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4296 process_init_element (input_location, init, false,
4297 braced_init_obstack);
4298 return;
4301 else
4303 tree first, second;
4304 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4305 location_t array_index_loc = UNKNOWN_LOCATION;
4306 /* ??? Following the old parser, [ objc-receiver
4307 objc-message-args ] is accepted as an initializer,
4308 being distinguished from a designator by what follows
4309 the first assignment expression inside the square
4310 brackets, but after a first array designator a
4311 subsequent square bracket is for Objective-C taken to
4312 start an expression, using the obsolete form of
4313 designated initializer without '=', rather than
4314 possibly being a second level of designation: in LALR
4315 terms, the '[' is shifted rather than reducing
4316 designator to designator-list. */
4317 if (des_prev == 1 && c_dialect_objc ())
4319 des_seen = des_prev;
4320 break;
4322 if (des_prev == 0 && c_dialect_objc ())
4324 /* This might be an array designator or an
4325 Objective-C message expression. If the former,
4326 continue parsing here; if the latter, parse the
4327 remainder of the initializer given the starting
4328 primary-expression. ??? It might make sense to
4329 distinguish when des_prev == 1 as well; see
4330 previous comment. */
4331 tree rec, args;
4332 struct c_expr mexpr;
4333 c_parser_consume_token (parser);
4334 if (c_parser_peek_token (parser)->type == CPP_NAME
4335 && ((c_parser_peek_token (parser)->id_kind
4336 == C_ID_TYPENAME)
4337 || (c_parser_peek_token (parser)->id_kind
4338 == C_ID_CLASSNAME)))
4340 /* Type name receiver. */
4341 tree id = c_parser_peek_token (parser)->value;
4342 c_parser_consume_token (parser);
4343 rec = objc_get_class_reference (id);
4344 goto parse_message_args;
4346 first = c_parser_expr_no_commas (parser, NULL).value;
4347 mark_exp_read (first);
4348 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4349 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4350 goto array_desig_after_first;
4351 /* Expression receiver. So far only one part
4352 without commas has been parsed; there might be
4353 more of the expression. */
4354 rec = first;
4355 while (c_parser_next_token_is (parser, CPP_COMMA))
4357 struct c_expr next;
4358 location_t comma_loc, exp_loc;
4359 comma_loc = c_parser_peek_token (parser)->location;
4360 c_parser_consume_token (parser);
4361 exp_loc = c_parser_peek_token (parser)->location;
4362 next = c_parser_expr_no_commas (parser, NULL);
4363 next = convert_lvalue_to_rvalue (exp_loc, next,
4364 true, true);
4365 rec = build_compound_expr (comma_loc, rec, next.value);
4367 parse_message_args:
4368 /* Now parse the objc-message-args. */
4369 args = c_parser_objc_message_args (parser);
4370 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4371 "expected %<]%>");
4372 mexpr.value
4373 = objc_build_message_expr (rec, args);
4374 mexpr.original_code = ERROR_MARK;
4375 mexpr.original_type = NULL;
4376 /* Now parse and process the remainder of the
4377 initializer, starting with this message
4378 expression as a primary-expression. */
4379 c_parser_initval (parser, &mexpr, braced_init_obstack);
4380 return;
4382 c_parser_consume_token (parser);
4383 array_index_loc = c_parser_peek_token (parser)->location;
4384 first = c_parser_expr_no_commas (parser, NULL).value;
4385 mark_exp_read (first);
4386 array_desig_after_first:
4387 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4389 ellipsis_loc = c_parser_peek_token (parser)->location;
4390 c_parser_consume_token (parser);
4391 second = c_parser_expr_no_commas (parser, NULL).value;
4392 mark_exp_read (second);
4394 else
4395 second = NULL_TREE;
4396 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4398 c_parser_consume_token (parser);
4399 set_init_index (array_index_loc, first, second,
4400 braced_init_obstack);
4401 if (second)
4402 pedwarn (ellipsis_loc, OPT_Wpedantic,
4403 "ISO C forbids specifying range of elements to initialize");
4405 else
4406 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4407 "expected %<]%>");
4410 if (des_seen >= 1)
4412 if (c_parser_next_token_is (parser, CPP_EQ))
4414 pedwarn_c90 (des_loc, OPT_Wpedantic,
4415 "ISO C90 forbids specifying subobject "
4416 "to initialize");
4417 c_parser_consume_token (parser);
4419 else
4421 if (des_seen == 1)
4422 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4423 "obsolete use of designated initializer without %<=%>");
4424 else
4426 struct c_expr init;
4427 init.value = error_mark_node;
4428 init.original_code = ERROR_MARK;
4429 init.original_type = NULL;
4430 c_parser_error (parser, "expected %<=%>");
4431 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4432 process_init_element (input_location, init, false,
4433 braced_init_obstack);
4434 return;
4439 c_parser_initval (parser, NULL, braced_init_obstack);
4442 /* Parse a nested initializer; as c_parser_initializer but parses
4443 initializers within braced lists, after any designators have been
4444 applied. If AFTER is not NULL then it is an Objective-C message
4445 expression which is the primary-expression starting the
4446 initializer. */
4448 static void
4449 c_parser_initval (c_parser *parser, struct c_expr *after,
4450 struct obstack * braced_init_obstack)
4452 struct c_expr init;
4453 gcc_assert (!after || c_dialect_objc ());
4454 location_t loc = c_parser_peek_token (parser)->location;
4456 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4457 init = c_parser_braced_init (parser, NULL_TREE, true);
4458 else
4460 init = c_parser_expr_no_commas (parser, after);
4461 if (init.value != NULL_TREE
4462 && TREE_CODE (init.value) != STRING_CST
4463 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4464 init = convert_lvalue_to_rvalue (loc, init, true, true);
4466 process_init_element (loc, init, false, braced_init_obstack);
4469 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4470 C99 6.8.2).
4472 compound-statement:
4473 { block-item-list[opt] }
4474 { label-declarations block-item-list }
4476 block-item-list:
4477 block-item
4478 block-item-list block-item
4480 block-item:
4481 nested-declaration
4482 statement
4484 nested-declaration:
4485 declaration
4487 GNU extensions:
4489 compound-statement:
4490 { label-declarations block-item-list }
4492 nested-declaration:
4493 __extension__ nested-declaration
4494 nested-function-definition
4496 label-declarations:
4497 label-declaration
4498 label-declarations label-declaration
4500 label-declaration:
4501 __label__ identifier-list ;
4503 Allowing the mixing of declarations and code is new in C99. The
4504 GNU syntax also permits (not shown above) labels at the end of
4505 compound statements, which yield an error. We don't allow labels
4506 on declarations; this might seem like a natural extension, but
4507 there would be a conflict between attributes on the label and
4508 prefix attributes on the declaration. ??? The syntax follows the
4509 old parser in requiring something after label declarations.
4510 Although they are erroneous if the labels declared aren't defined,
4511 is it useful for the syntax to be this way?
4513 OpenACC:
4515 block-item:
4516 openacc-directive
4518 openacc-directive:
4519 update-directive
4521 OpenMP:
4523 block-item:
4524 openmp-directive
4526 openmp-directive:
4527 barrier-directive
4528 flush-directive
4529 taskwait-directive
4530 taskyield-directive
4531 cancel-directive
4532 cancellation-point-directive */
4534 static tree
4535 c_parser_compound_statement (c_parser *parser)
4537 tree stmt;
4538 location_t brace_loc;
4539 brace_loc = c_parser_peek_token (parser)->location;
4540 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4542 /* Ensure a scope is entered and left anyway to avoid confusion
4543 if we have just prepared to enter a function body. */
4544 stmt = c_begin_compound_stmt (true);
4545 c_end_compound_stmt (brace_loc, stmt, true);
4546 return error_mark_node;
4548 stmt = c_begin_compound_stmt (true);
4549 c_parser_compound_statement_nostart (parser);
4551 /* If the compound stmt contains array notations, then we expand them. */
4552 if (flag_cilkplus && contains_array_notation_expr (stmt))
4553 stmt = expand_array_notation_exprs (stmt);
4554 return c_end_compound_stmt (brace_loc, stmt, true);
4557 /* Parse a compound statement except for the opening brace. This is
4558 used for parsing both compound statements and statement expressions
4559 (which follow different paths to handling the opening). */
4561 static void
4562 c_parser_compound_statement_nostart (c_parser *parser)
4564 bool last_stmt = false;
4565 bool last_label = false;
4566 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4567 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4568 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4570 c_parser_consume_token (parser);
4571 return;
4573 mark_valid_location_for_stdc_pragma (true);
4574 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4576 /* Read zero or more forward-declarations for labels that nested
4577 functions can jump to. */
4578 mark_valid_location_for_stdc_pragma (false);
4579 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4581 label_loc = c_parser_peek_token (parser)->location;
4582 c_parser_consume_token (parser);
4583 /* Any identifiers, including those declared as type names,
4584 are OK here. */
4585 while (true)
4587 tree label;
4588 if (c_parser_next_token_is_not (parser, CPP_NAME))
4590 c_parser_error (parser, "expected identifier");
4591 break;
4593 label
4594 = declare_label (c_parser_peek_token (parser)->value);
4595 C_DECLARED_LABEL_FLAG (label) = 1;
4596 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4597 c_parser_consume_token (parser);
4598 if (c_parser_next_token_is (parser, CPP_COMMA))
4599 c_parser_consume_token (parser);
4600 else
4601 break;
4603 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4605 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4607 /* We must now have at least one statement, label or declaration. */
4608 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4610 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4611 c_parser_error (parser, "expected declaration or statement");
4612 c_parser_consume_token (parser);
4613 return;
4615 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4617 location_t loc = c_parser_peek_token (parser)->location;
4618 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4619 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4620 || (c_parser_next_token_is (parser, CPP_NAME)
4621 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4623 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4624 label_loc = c_parser_peek_2nd_token (parser)->location;
4625 else
4626 label_loc = c_parser_peek_token (parser)->location;
4627 last_label = true;
4628 last_stmt = false;
4629 mark_valid_location_for_stdc_pragma (false);
4630 c_parser_label (parser);
4632 else if (!last_label
4633 && c_parser_next_tokens_start_declaration (parser))
4635 last_label = false;
4636 mark_valid_location_for_stdc_pragma (false);
4637 c_parser_declaration_or_fndef (parser, true, true, true, true,
4638 true, NULL, vNULL);
4639 if (last_stmt)
4640 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4641 "ISO C90 forbids mixed declarations and code");
4642 last_stmt = false;
4644 else if (!last_label
4645 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4647 /* __extension__ can start a declaration, but is also an
4648 unary operator that can start an expression. Consume all
4649 but the last of a possible series of __extension__ to
4650 determine which. */
4651 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4652 && (c_parser_peek_2nd_token (parser)->keyword
4653 == RID_EXTENSION))
4654 c_parser_consume_token (parser);
4655 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4657 int ext;
4658 ext = disable_extension_diagnostics ();
4659 c_parser_consume_token (parser);
4660 last_label = false;
4661 mark_valid_location_for_stdc_pragma (false);
4662 c_parser_declaration_or_fndef (parser, true, true, true, true,
4663 true, NULL, vNULL);
4664 /* Following the old parser, __extension__ does not
4665 disable this diagnostic. */
4666 restore_extension_diagnostics (ext);
4667 if (last_stmt)
4668 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4669 "ISO C90 forbids mixed declarations and code");
4670 last_stmt = false;
4672 else
4673 goto statement;
4675 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4677 /* External pragmas, and some omp pragmas, are not associated
4678 with regular c code, and so are not to be considered statements
4679 syntactically. This ensures that the user doesn't put them
4680 places that would turn into syntax errors if the directive
4681 were ignored. */
4682 if (c_parser_pragma (parser, pragma_compound))
4683 last_label = false, last_stmt = true;
4685 else if (c_parser_next_token_is (parser, CPP_EOF))
4687 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4688 c_parser_error (parser, "expected declaration or statement");
4689 return;
4691 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4693 if (parser->in_if_block)
4695 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4696 error_at (loc, """expected %<}%> before %<else%>");
4697 return;
4699 else
4701 error_at (loc, "%<else%> without a previous %<if%>");
4702 c_parser_consume_token (parser);
4703 continue;
4706 else
4708 statement:
4709 last_label = false;
4710 last_stmt = true;
4711 mark_valid_location_for_stdc_pragma (false);
4712 c_parser_statement_after_labels (parser);
4715 parser->error = false;
4717 if (last_label)
4718 error_at (label_loc, "label at end of compound statement");
4719 c_parser_consume_token (parser);
4720 /* Restore the value we started with. */
4721 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4724 /* Parse all consecutive labels. */
4726 static void
4727 c_parser_all_labels (c_parser *parser)
4729 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4730 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4731 || (c_parser_next_token_is (parser, CPP_NAME)
4732 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4733 c_parser_label (parser);
4736 /* Parse a label (C90 6.6.1, C99 6.8.1).
4738 label:
4739 identifier : attributes[opt]
4740 case constant-expression :
4741 default :
4743 GNU extensions:
4745 label:
4746 case constant-expression ... constant-expression :
4748 The use of attributes on labels is a GNU extension. The syntax in
4749 GNU C accepts any expressions without commas, non-constant
4750 expressions being rejected later. */
4752 static void
4753 c_parser_label (c_parser *parser)
4755 location_t loc1 = c_parser_peek_token (parser)->location;
4756 tree label = NULL_TREE;
4757 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4759 tree exp1, exp2;
4760 c_parser_consume_token (parser);
4761 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4762 if (c_parser_next_token_is (parser, CPP_COLON))
4764 c_parser_consume_token (parser);
4765 label = do_case (loc1, exp1, NULL_TREE);
4767 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4769 c_parser_consume_token (parser);
4770 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4771 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4772 label = do_case (loc1, exp1, exp2);
4774 else
4775 c_parser_error (parser, "expected %<:%> or %<...%>");
4777 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4779 c_parser_consume_token (parser);
4780 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4781 label = do_case (loc1, NULL_TREE, NULL_TREE);
4783 else
4785 tree name = c_parser_peek_token (parser)->value;
4786 tree tlab;
4787 tree attrs;
4788 location_t loc2 = c_parser_peek_token (parser)->location;
4789 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4790 c_parser_consume_token (parser);
4791 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4792 c_parser_consume_token (parser);
4793 attrs = c_parser_attributes (parser);
4794 tlab = define_label (loc2, name);
4795 if (tlab)
4797 decl_attributes (&tlab, attrs, 0);
4798 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4801 if (label)
4803 if (c_parser_next_tokens_start_declaration (parser))
4805 error_at (c_parser_peek_token (parser)->location,
4806 "a label can only be part of a statement and "
4807 "a declaration is not a statement");
4808 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4809 /*static_assert_ok*/ true,
4810 /*empty_ok*/ true, /*nested*/ true,
4811 /*start_attr_ok*/ true, NULL,
4812 vNULL);
4817 /* Parse a statement (C90 6.6, C99 6.8).
4819 statement:
4820 labeled-statement
4821 compound-statement
4822 expression-statement
4823 selection-statement
4824 iteration-statement
4825 jump-statement
4827 labeled-statement:
4828 label statement
4830 expression-statement:
4831 expression[opt] ;
4833 selection-statement:
4834 if-statement
4835 switch-statement
4837 iteration-statement:
4838 while-statement
4839 do-statement
4840 for-statement
4842 jump-statement:
4843 goto identifier ;
4844 continue ;
4845 break ;
4846 return expression[opt] ;
4848 GNU extensions:
4850 statement:
4851 asm-statement
4853 jump-statement:
4854 goto * expression ;
4856 Objective-C:
4858 statement:
4859 objc-throw-statement
4860 objc-try-catch-statement
4861 objc-synchronized-statement
4863 objc-throw-statement:
4864 @throw expression ;
4865 @throw ;
4867 OpenACC:
4869 statement:
4870 openacc-construct
4872 openacc-construct:
4873 parallel-construct
4874 kernels-construct
4875 data-construct
4876 loop-construct
4878 parallel-construct:
4879 parallel-directive structured-block
4881 kernels-construct:
4882 kernels-directive structured-block
4884 data-construct:
4885 data-directive structured-block
4887 loop-construct:
4888 loop-directive structured-block
4890 OpenMP:
4892 statement:
4893 openmp-construct
4895 openmp-construct:
4896 parallel-construct
4897 for-construct
4898 simd-construct
4899 for-simd-construct
4900 sections-construct
4901 single-construct
4902 parallel-for-construct
4903 parallel-for-simd-construct
4904 parallel-sections-construct
4905 master-construct
4906 critical-construct
4907 atomic-construct
4908 ordered-construct
4910 parallel-construct:
4911 parallel-directive structured-block
4913 for-construct:
4914 for-directive iteration-statement
4916 simd-construct:
4917 simd-directive iteration-statements
4919 for-simd-construct:
4920 for-simd-directive iteration-statements
4922 sections-construct:
4923 sections-directive section-scope
4925 single-construct:
4926 single-directive structured-block
4928 parallel-for-construct:
4929 parallel-for-directive iteration-statement
4931 parallel-for-simd-construct:
4932 parallel-for-simd-directive iteration-statement
4934 parallel-sections-construct:
4935 parallel-sections-directive section-scope
4937 master-construct:
4938 master-directive structured-block
4940 critical-construct:
4941 critical-directive structured-block
4943 atomic-construct:
4944 atomic-directive expression-statement
4946 ordered-construct:
4947 ordered-directive structured-block
4949 Transactional Memory:
4951 statement:
4952 transaction-statement
4953 transaction-cancel-statement
4956 static void
4957 c_parser_statement (c_parser *parser)
4959 c_parser_all_labels (parser);
4960 c_parser_statement_after_labels (parser);
4963 /* Parse a statement, other than a labeled statement. */
4965 static void
4966 c_parser_statement_after_labels (c_parser *parser)
4968 location_t loc = c_parser_peek_token (parser)->location;
4969 tree stmt = NULL_TREE;
4970 bool in_if_block = parser->in_if_block;
4971 parser->in_if_block = false;
4972 switch (c_parser_peek_token (parser)->type)
4974 case CPP_OPEN_BRACE:
4975 add_stmt (c_parser_compound_statement (parser));
4976 break;
4977 case CPP_KEYWORD:
4978 switch (c_parser_peek_token (parser)->keyword)
4980 case RID_IF:
4981 c_parser_if_statement (parser);
4982 break;
4983 case RID_SWITCH:
4984 c_parser_switch_statement (parser);
4985 break;
4986 case RID_WHILE:
4987 c_parser_while_statement (parser, false);
4988 break;
4989 case RID_DO:
4990 c_parser_do_statement (parser, false);
4991 break;
4992 case RID_FOR:
4993 c_parser_for_statement (parser, false);
4994 break;
4995 case RID_CILK_FOR:
4996 if (!flag_cilkplus)
4998 error_at (c_parser_peek_token (parser)->location,
4999 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5000 c_parser_skip_to_end_of_block_or_statement (parser);
5002 else
5003 c_parser_cilk_for (parser, integer_zero_node);
5004 break;
5005 case RID_CILK_SYNC:
5006 c_parser_consume_token (parser);
5007 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5008 if (!flag_cilkplus)
5009 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5010 else
5011 add_stmt (build_cilk_sync ());
5012 break;
5013 case RID_GOTO:
5014 c_parser_consume_token (parser);
5015 if (c_parser_next_token_is (parser, CPP_NAME))
5017 stmt = c_finish_goto_label (loc,
5018 c_parser_peek_token (parser)->value);
5019 c_parser_consume_token (parser);
5021 else if (c_parser_next_token_is (parser, CPP_MULT))
5023 struct c_expr val;
5025 c_parser_consume_token (parser);
5026 val = c_parser_expression (parser);
5027 if (check_no_cilk (val.value,
5028 "Cilk array notation cannot be used as a computed goto expression",
5029 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5030 loc))
5031 val.value = error_mark_node;
5032 val = convert_lvalue_to_rvalue (loc, val, false, true);
5033 stmt = c_finish_goto_ptr (loc, val.value);
5035 else
5036 c_parser_error (parser, "expected identifier or %<*%>");
5037 goto expect_semicolon;
5038 case RID_CONTINUE:
5039 c_parser_consume_token (parser);
5040 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5041 goto expect_semicolon;
5042 case RID_BREAK:
5043 c_parser_consume_token (parser);
5044 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5045 goto expect_semicolon;
5046 case RID_RETURN:
5047 c_parser_consume_token (parser);
5048 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5050 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5051 c_parser_consume_token (parser);
5053 else
5055 location_t xloc = c_parser_peek_token (parser)->location;
5056 struct c_expr expr = c_parser_expression_conv (parser);
5057 mark_exp_read (expr.value);
5058 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5059 goto expect_semicolon;
5061 break;
5062 case RID_ASM:
5063 stmt = c_parser_asm_statement (parser);
5064 break;
5065 case RID_TRANSACTION_ATOMIC:
5066 case RID_TRANSACTION_RELAXED:
5067 stmt = c_parser_transaction (parser,
5068 c_parser_peek_token (parser)->keyword);
5069 break;
5070 case RID_TRANSACTION_CANCEL:
5071 stmt = c_parser_transaction_cancel (parser);
5072 goto expect_semicolon;
5073 case RID_AT_THROW:
5074 gcc_assert (c_dialect_objc ());
5075 c_parser_consume_token (parser);
5076 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5078 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5079 c_parser_consume_token (parser);
5081 else
5083 struct c_expr expr = c_parser_expression (parser);
5084 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5085 if (check_no_cilk (expr.value,
5086 "Cilk array notation cannot be used for a throw expression",
5087 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5088 expr.value = error_mark_node;
5089 else
5091 expr.value = c_fully_fold (expr.value, false, NULL);
5092 stmt = objc_build_throw_stmt (loc, expr.value);
5094 goto expect_semicolon;
5096 break;
5097 case RID_AT_TRY:
5098 gcc_assert (c_dialect_objc ());
5099 c_parser_objc_try_catch_finally_statement (parser);
5100 break;
5101 case RID_AT_SYNCHRONIZED:
5102 gcc_assert (c_dialect_objc ());
5103 c_parser_objc_synchronized_statement (parser);
5104 break;
5105 default:
5106 goto expr_stmt;
5108 break;
5109 case CPP_SEMICOLON:
5110 c_parser_consume_token (parser);
5111 break;
5112 case CPP_CLOSE_PAREN:
5113 case CPP_CLOSE_SQUARE:
5114 /* Avoid infinite loop in error recovery:
5115 c_parser_skip_until_found stops at a closing nesting
5116 delimiter without consuming it, but here we need to consume
5117 it to proceed further. */
5118 c_parser_error (parser, "expected statement");
5119 c_parser_consume_token (parser);
5120 break;
5121 case CPP_PRAGMA:
5122 c_parser_pragma (parser, pragma_stmt);
5123 break;
5124 default:
5125 expr_stmt:
5126 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5127 expect_semicolon:
5128 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5129 break;
5131 /* Two cases cannot and do not have line numbers associated: If stmt
5132 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5133 cannot hold line numbers. But that's OK because the statement
5134 will either be changed to a MODIFY_EXPR during gimplification of
5135 the statement expr, or discarded. If stmt was compound, but
5136 without new variables, we will have skipped the creation of a
5137 BIND and will have a bare STATEMENT_LIST. But that's OK because
5138 (recursively) all of the component statements should already have
5139 line numbers assigned. ??? Can we discard no-op statements
5140 earlier? */
5141 if (CAN_HAVE_LOCATION_P (stmt)
5142 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5143 SET_EXPR_LOCATION (stmt, loc);
5145 parser->in_if_block = in_if_block;
5148 /* Parse the condition from an if, do, while or for statements. */
5150 static tree
5151 c_parser_condition (c_parser *parser)
5153 location_t loc = c_parser_peek_token (parser)->location;
5154 tree cond;
5155 cond = c_parser_expression_conv (parser).value;
5156 cond = c_objc_common_truthvalue_conversion (loc, cond);
5157 cond = c_fully_fold (cond, false, NULL);
5158 if (warn_sequence_point)
5159 verify_sequence_points (cond);
5160 return cond;
5163 /* Parse a parenthesized condition from an if, do or while statement.
5165 condition:
5166 ( expression )
5168 static tree
5169 c_parser_paren_condition (c_parser *parser)
5171 tree cond;
5172 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5173 return error_mark_node;
5174 cond = c_parser_condition (parser);
5175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5176 return cond;
5179 /* Parse a statement which is a block in C99. */
5181 static tree
5182 c_parser_c99_block_statement (c_parser *parser)
5184 tree block = c_begin_compound_stmt (flag_isoc99);
5185 location_t loc = c_parser_peek_token (parser)->location;
5186 c_parser_statement (parser);
5187 return c_end_compound_stmt (loc, block, flag_isoc99);
5190 /* Parse the body of an if statement. This is just parsing a
5191 statement but (a) it is a block in C99, (b) we track whether the
5192 body is an if statement for the sake of -Wparentheses warnings, (c)
5193 we handle an empty body specially for the sake of -Wempty-body
5194 warnings, and (d) we call parser_compound_statement directly
5195 because c_parser_statement_after_labels resets
5196 parser->in_if_block. */
5198 static tree
5199 c_parser_if_body (c_parser *parser, bool *if_p, location_t if_loc)
5201 tree block = c_begin_compound_stmt (flag_isoc99);
5202 location_t body_loc = c_parser_peek_token (parser)->location;
5203 c_parser_all_labels (parser);
5204 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5205 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5207 location_t loc = c_parser_peek_token (parser)->location;
5208 add_stmt (build_empty_stmt (loc));
5209 c_parser_consume_token (parser);
5210 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5211 warning_at (loc, OPT_Wempty_body,
5212 "suggest braces around empty body in an %<if%> statement");
5214 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5215 add_stmt (c_parser_compound_statement (parser));
5216 else
5218 c_parser_statement_after_labels (parser);
5219 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5220 warn_for_misleading_indentation (if_loc, body_loc,
5221 c_parser_peek_token (parser)->location,
5222 c_parser_peek_token (parser)->type,
5223 "if");
5226 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5229 /* Parse the else body of an if statement. This is just parsing a
5230 statement but (a) it is a block in C99, (b) we handle an empty body
5231 specially for the sake of -Wempty-body warnings. */
5233 static tree
5234 c_parser_else_body (c_parser *parser, location_t else_loc)
5236 location_t body_loc = c_parser_peek_token (parser)->location;
5237 tree block = c_begin_compound_stmt (flag_isoc99);
5238 c_parser_all_labels (parser);
5239 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5241 location_t loc = c_parser_peek_token (parser)->location;
5242 warning_at (loc,
5243 OPT_Wempty_body,
5244 "suggest braces around empty body in an %<else%> statement");
5245 add_stmt (build_empty_stmt (loc));
5246 c_parser_consume_token (parser);
5248 else
5250 c_parser_statement_after_labels (parser);
5251 warn_for_misleading_indentation (else_loc, body_loc,
5252 c_parser_peek_token (parser)->location,
5253 c_parser_peek_token (parser)->type,
5254 "else");
5256 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5259 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5261 if-statement:
5262 if ( expression ) statement
5263 if ( expression ) statement else statement
5266 static void
5267 c_parser_if_statement (c_parser *parser)
5269 tree block;
5270 location_t loc;
5271 tree cond;
5272 bool first_if = false;
5273 tree first_body, second_body;
5274 bool in_if_block;
5275 tree if_stmt;
5277 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5278 location_t if_loc = c_parser_peek_token (parser)->location;
5279 c_parser_consume_token (parser);
5280 block = c_begin_compound_stmt (flag_isoc99);
5281 loc = c_parser_peek_token (parser)->location;
5282 cond = c_parser_paren_condition (parser);
5283 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5285 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5286 cond = error_mark_node;
5288 in_if_block = parser->in_if_block;
5289 parser->in_if_block = true;
5290 first_body = c_parser_if_body (parser, &first_if, if_loc);
5291 parser->in_if_block = in_if_block;
5292 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5294 location_t else_loc = c_parser_peek_token (parser)->location;
5295 c_parser_consume_token (parser);
5296 second_body = c_parser_else_body (parser, else_loc);
5298 else
5299 second_body = NULL_TREE;
5300 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5301 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5303 /* If the if statement contains array notations, then we expand them. */
5304 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5305 if_stmt = fix_conditional_array_notations (if_stmt);
5306 add_stmt (if_stmt);
5309 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5311 switch-statement:
5312 switch (expression) statement
5315 static void
5316 c_parser_switch_statement (c_parser *parser)
5318 struct c_expr ce;
5319 tree block, expr, body, save_break;
5320 location_t switch_loc = c_parser_peek_token (parser)->location;
5321 location_t switch_cond_loc;
5322 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5323 c_parser_consume_token (parser);
5324 block = c_begin_compound_stmt (flag_isoc99);
5325 bool explicit_cast_p = false;
5326 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5328 switch_cond_loc = c_parser_peek_token (parser)->location;
5329 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5330 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5331 explicit_cast_p = true;
5332 ce = c_parser_expression (parser);
5333 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5334 expr = ce.value;
5335 /* ??? expr has no valid location? */
5336 if (check_no_cilk (expr,
5337 "Cilk array notation cannot be used as a condition for switch statement",
5338 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5339 switch_cond_loc))
5340 expr = error_mark_node;
5341 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5343 else
5345 switch_cond_loc = UNKNOWN_LOCATION;
5346 expr = error_mark_node;
5348 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5349 save_break = c_break_label;
5350 c_break_label = NULL_TREE;
5351 body = c_parser_c99_block_statement (parser);
5352 c_finish_case (body, ce.original_type);
5353 if (c_break_label)
5355 location_t here = c_parser_peek_token (parser)->location;
5356 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5357 SET_EXPR_LOCATION (t, here);
5358 add_stmt (t);
5360 c_break_label = save_break;
5361 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5364 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5366 while-statement:
5367 while (expression) statement
5370 static void
5371 c_parser_while_statement (c_parser *parser, bool ivdep)
5373 tree block, cond, body, save_break, save_cont;
5374 location_t loc;
5375 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5376 location_t while_loc = c_parser_peek_token (parser)->location;
5377 c_parser_consume_token (parser);
5378 block = c_begin_compound_stmt (flag_isoc99);
5379 loc = c_parser_peek_token (parser)->location;
5380 cond = c_parser_paren_condition (parser);
5381 if (check_no_cilk (cond,
5382 "Cilk array notation cannot be used as a condition for while statement",
5383 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5384 cond = error_mark_node;
5385 if (ivdep && cond != error_mark_node)
5386 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5387 build_int_cst (integer_type_node,
5388 annot_expr_ivdep_kind));
5389 save_break = c_break_label;
5390 c_break_label = NULL_TREE;
5391 save_cont = c_cont_label;
5392 c_cont_label = NULL_TREE;
5394 location_t body_loc = UNKNOWN_LOCATION;
5395 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5396 body_loc = c_parser_peek_token (parser)->location;
5397 body = c_parser_c99_block_statement (parser);
5398 warn_for_misleading_indentation (while_loc, body_loc,
5399 c_parser_peek_token (parser)->location,
5400 c_parser_peek_token (parser)->type,
5401 "while");
5403 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5404 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5405 c_break_label = save_break;
5406 c_cont_label = save_cont;
5409 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5411 do-statement:
5412 do statement while ( expression ) ;
5415 static void
5416 c_parser_do_statement (c_parser *parser, bool ivdep)
5418 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5419 location_t loc;
5420 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5421 c_parser_consume_token (parser);
5422 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5423 warning_at (c_parser_peek_token (parser)->location,
5424 OPT_Wempty_body,
5425 "suggest braces around empty body in %<do%> statement");
5426 block = c_begin_compound_stmt (flag_isoc99);
5427 loc = c_parser_peek_token (parser)->location;
5428 save_break = c_break_label;
5429 c_break_label = NULL_TREE;
5430 save_cont = c_cont_label;
5431 c_cont_label = NULL_TREE;
5432 body = c_parser_c99_block_statement (parser);
5433 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5434 new_break = c_break_label;
5435 c_break_label = save_break;
5436 new_cont = c_cont_label;
5437 c_cont_label = save_cont;
5438 cond = c_parser_paren_condition (parser);
5439 if (check_no_cilk (cond,
5440 "Cilk array notation cannot be used as a condition for a do-while statement",
5441 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5442 cond = error_mark_node;
5443 if (ivdep && cond != error_mark_node)
5444 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5445 build_int_cst (integer_type_node,
5446 annot_expr_ivdep_kind));
5447 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5448 c_parser_skip_to_end_of_block_or_statement (parser);
5449 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5450 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5453 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5455 for-statement:
5456 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5457 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5459 The form with a declaration is new in C99.
5461 ??? In accordance with the old parser, the declaration may be a
5462 nested function, which is then rejected in check_for_loop_decls,
5463 but does it make any sense for this to be included in the grammar?
5464 Note in particular that the nested function does not include a
5465 trailing ';', whereas the "declaration" production includes one.
5466 Also, can we reject bad declarations earlier and cheaper than
5467 check_for_loop_decls?
5469 In Objective-C, there are two additional variants:
5471 foreach-statement:
5472 for ( expression in expresssion ) statement
5473 for ( declaration in expression ) statement
5475 This is inconsistent with C, because the second variant is allowed
5476 even if c99 is not enabled.
5478 The rest of the comment documents these Objective-C foreach-statement.
5480 Here is the canonical example of the first variant:
5481 for (object in array) { do something with object }
5482 we call the first expression ("object") the "object_expression" and
5483 the second expression ("array") the "collection_expression".
5484 object_expression must be an lvalue of type "id" (a generic Objective-C
5485 object) because the loop works by assigning to object_expression the
5486 various objects from the collection_expression. collection_expression
5487 must evaluate to something of type "id" which responds to the method
5488 countByEnumeratingWithState:objects:count:.
5490 The canonical example of the second variant is:
5491 for (id object in array) { do something with object }
5492 which is completely equivalent to
5494 id object;
5495 for (object in array) { do something with object }
5497 Note that initizializing 'object' in some way (eg, "for ((object =
5498 xxx) in array) { do something with object }") is possibly
5499 technically valid, but completely pointless as 'object' will be
5500 assigned to something else as soon as the loop starts. We should
5501 most likely reject it (TODO).
5503 The beginning of the Objective-C foreach-statement looks exactly
5504 like the beginning of the for-statement, and we can tell it is a
5505 foreach-statement only because the initial declaration or
5506 expression is terminated by 'in' instead of ';'.
5509 static void
5510 c_parser_for_statement (c_parser *parser, bool ivdep)
5512 tree block, cond, incr, save_break, save_cont, body;
5513 /* The following are only used when parsing an ObjC foreach statement. */
5514 tree object_expression;
5515 /* Silence the bogus uninitialized warning. */
5516 tree collection_expression = NULL;
5517 location_t loc = c_parser_peek_token (parser)->location;
5518 location_t for_loc = c_parser_peek_token (parser)->location;
5519 bool is_foreach_statement = false;
5520 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5521 c_parser_consume_token (parser);
5522 /* Open a compound statement in Objective-C as well, just in case this is
5523 as foreach expression. */
5524 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5525 cond = error_mark_node;
5526 incr = error_mark_node;
5527 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5529 /* Parse the initialization declaration or expression. */
5530 object_expression = error_mark_node;
5531 parser->objc_could_be_foreach_context = c_dialect_objc ();
5532 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5534 parser->objc_could_be_foreach_context = false;
5535 c_parser_consume_token (parser);
5536 c_finish_expr_stmt (loc, NULL_TREE);
5538 else if (c_parser_next_tokens_start_declaration (parser))
5540 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5541 &object_expression, vNULL);
5542 parser->objc_could_be_foreach_context = false;
5544 if (c_parser_next_token_is_keyword (parser, RID_IN))
5546 c_parser_consume_token (parser);
5547 is_foreach_statement = true;
5548 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5549 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5551 else
5552 check_for_loop_decls (for_loc, flag_isoc99);
5554 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5556 /* __extension__ can start a declaration, but is also an
5557 unary operator that can start an expression. Consume all
5558 but the last of a possible series of __extension__ to
5559 determine which. */
5560 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5561 && (c_parser_peek_2nd_token (parser)->keyword
5562 == RID_EXTENSION))
5563 c_parser_consume_token (parser);
5564 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5566 int ext;
5567 ext = disable_extension_diagnostics ();
5568 c_parser_consume_token (parser);
5569 c_parser_declaration_or_fndef (parser, true, true, true, true,
5570 true, &object_expression, vNULL);
5571 parser->objc_could_be_foreach_context = false;
5573 restore_extension_diagnostics (ext);
5574 if (c_parser_next_token_is_keyword (parser, RID_IN))
5576 c_parser_consume_token (parser);
5577 is_foreach_statement = true;
5578 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5579 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5581 else
5582 check_for_loop_decls (for_loc, flag_isoc99);
5584 else
5585 goto init_expr;
5587 else
5589 init_expr:
5591 struct c_expr ce;
5592 tree init_expression;
5593 ce = c_parser_expression (parser);
5594 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5595 level statement", but it works just fine, so allow it. */
5596 init_expression = ce.value;
5597 parser->objc_could_be_foreach_context = false;
5598 if (c_parser_next_token_is_keyword (parser, RID_IN))
5600 c_parser_consume_token (parser);
5601 is_foreach_statement = true;
5602 if (! lvalue_p (init_expression))
5603 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5604 object_expression = c_fully_fold (init_expression, false, NULL);
5606 else
5608 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5609 init_expression = ce.value;
5610 c_finish_expr_stmt (loc, init_expression);
5611 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5615 /* Parse the loop condition. In the case of a foreach
5616 statement, there is no loop condition. */
5617 gcc_assert (!parser->objc_could_be_foreach_context);
5618 if (!is_foreach_statement)
5620 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5622 if (ivdep)
5624 c_parser_error (parser, "missing loop condition in loop with "
5625 "%<GCC ivdep%> pragma");
5626 cond = error_mark_node;
5628 else
5630 c_parser_consume_token (parser);
5631 cond = NULL_TREE;
5634 else
5636 cond = c_parser_condition (parser);
5637 if (check_no_cilk (cond,
5638 "Cilk array notation cannot be used in a condition for a for-loop",
5639 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5640 cond = error_mark_node;
5641 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5642 "expected %<;%>");
5644 if (ivdep && cond != error_mark_node)
5645 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5646 build_int_cst (integer_type_node,
5647 annot_expr_ivdep_kind));
5649 /* Parse the increment expression (the third expression in a
5650 for-statement). In the case of a foreach-statement, this is
5651 the expression that follows the 'in'. */
5652 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5654 if (is_foreach_statement)
5656 c_parser_error (parser, "missing collection in fast enumeration");
5657 collection_expression = error_mark_node;
5659 else
5660 incr = c_process_expr_stmt (loc, NULL_TREE);
5662 else
5664 if (is_foreach_statement)
5665 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5666 false, NULL);
5667 else
5669 struct c_expr ce = c_parser_expression (parser);
5670 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5671 incr = c_process_expr_stmt (loc, ce.value);
5674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5676 save_break = c_break_label;
5677 c_break_label = NULL_TREE;
5678 save_cont = c_cont_label;
5679 c_cont_label = NULL_TREE;
5681 location_t body_loc = UNKNOWN_LOCATION;
5682 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5683 body_loc = c_parser_peek_token (parser)->location;
5684 body = c_parser_c99_block_statement (parser);
5685 warn_for_misleading_indentation (for_loc, body_loc,
5686 c_parser_peek_token (parser)->location,
5687 c_parser_peek_token (parser)->type,
5688 "for");
5690 if (is_foreach_statement)
5691 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5692 else
5693 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5694 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5695 c_break_label = save_break;
5696 c_cont_label = save_cont;
5699 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5700 statement with inputs, outputs, clobbers, and volatile tag
5701 allowed.
5703 asm-statement:
5704 asm type-qualifier[opt] ( asm-argument ) ;
5705 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5707 asm-argument:
5708 asm-string-literal
5709 asm-string-literal : asm-operands[opt]
5710 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5711 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5713 asm-goto-argument:
5714 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5715 : asm-goto-operands
5717 Qualifiers other than volatile are accepted in the syntax but
5718 warned for. */
5720 static tree
5721 c_parser_asm_statement (c_parser *parser)
5723 tree quals, str, outputs, inputs, clobbers, labels, ret;
5724 bool simple, is_goto;
5725 location_t asm_loc = c_parser_peek_token (parser)->location;
5726 int section, nsections;
5728 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5729 c_parser_consume_token (parser);
5730 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5732 quals = c_parser_peek_token (parser)->value;
5733 c_parser_consume_token (parser);
5735 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5736 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5738 warning_at (c_parser_peek_token (parser)->location,
5740 "%E qualifier ignored on asm",
5741 c_parser_peek_token (parser)->value);
5742 quals = NULL_TREE;
5743 c_parser_consume_token (parser);
5745 else
5746 quals = NULL_TREE;
5748 is_goto = false;
5749 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5751 c_parser_consume_token (parser);
5752 is_goto = true;
5755 /* ??? Follow the C++ parser rather than using the
5756 lex_untranslated_string kludge. */
5757 parser->lex_untranslated_string = true;
5758 ret = NULL;
5760 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5761 goto error;
5763 str = c_parser_asm_string_literal (parser);
5764 if (str == NULL_TREE)
5765 goto error_close_paren;
5767 simple = true;
5768 outputs = NULL_TREE;
5769 inputs = NULL_TREE;
5770 clobbers = NULL_TREE;
5771 labels = NULL_TREE;
5773 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5774 goto done_asm;
5776 /* Parse each colon-delimited section of operands. */
5777 nsections = 3 + is_goto;
5778 for (section = 0; section < nsections; ++section)
5780 if (!c_parser_require (parser, CPP_COLON,
5781 is_goto
5782 ? "expected %<:%>"
5783 : "expected %<:%> or %<)%>"))
5784 goto error_close_paren;
5786 /* Once past any colon, we're no longer a simple asm. */
5787 simple = false;
5789 if ((!c_parser_next_token_is (parser, CPP_COLON)
5790 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5791 || section == 3)
5792 switch (section)
5794 case 0:
5795 /* For asm goto, we don't allow output operands, but reserve
5796 the slot for a future extension that does allow them. */
5797 if (!is_goto)
5798 outputs = c_parser_asm_operands (parser);
5799 break;
5800 case 1:
5801 inputs = c_parser_asm_operands (parser);
5802 break;
5803 case 2:
5804 clobbers = c_parser_asm_clobbers (parser);
5805 break;
5806 case 3:
5807 labels = c_parser_asm_goto_operands (parser);
5808 break;
5809 default:
5810 gcc_unreachable ();
5813 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5814 goto done_asm;
5817 done_asm:
5818 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5820 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5821 goto error;
5824 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5825 c_parser_skip_to_end_of_block_or_statement (parser);
5827 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5828 clobbers, labels, simple));
5830 error:
5831 parser->lex_untranslated_string = false;
5832 return ret;
5834 error_close_paren:
5835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5836 goto error;
5839 /* Parse asm operands, a GNU extension.
5841 asm-operands:
5842 asm-operand
5843 asm-operands , asm-operand
5845 asm-operand:
5846 asm-string-literal ( expression )
5847 [ identifier ] asm-string-literal ( expression )
5850 static tree
5851 c_parser_asm_operands (c_parser *parser)
5853 tree list = NULL_TREE;
5854 while (true)
5856 tree name, str;
5857 struct c_expr expr;
5858 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5860 c_parser_consume_token (parser);
5861 if (c_parser_next_token_is (parser, CPP_NAME))
5863 tree id = c_parser_peek_token (parser)->value;
5864 c_parser_consume_token (parser);
5865 name = build_string (IDENTIFIER_LENGTH (id),
5866 IDENTIFIER_POINTER (id));
5868 else
5870 c_parser_error (parser, "expected identifier");
5871 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5872 return NULL_TREE;
5874 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5875 "expected %<]%>");
5877 else
5878 name = NULL_TREE;
5879 str = c_parser_asm_string_literal (parser);
5880 if (str == NULL_TREE)
5881 return NULL_TREE;
5882 parser->lex_untranslated_string = false;
5883 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5885 parser->lex_untranslated_string = true;
5886 return NULL_TREE;
5888 expr = c_parser_expression (parser);
5889 mark_exp_read (expr.value);
5890 parser->lex_untranslated_string = true;
5891 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5893 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5894 return NULL_TREE;
5896 list = chainon (list, build_tree_list (build_tree_list (name, str),
5897 expr.value));
5898 if (c_parser_next_token_is (parser, CPP_COMMA))
5899 c_parser_consume_token (parser);
5900 else
5901 break;
5903 return list;
5906 /* Parse asm clobbers, a GNU extension.
5908 asm-clobbers:
5909 asm-string-literal
5910 asm-clobbers , asm-string-literal
5913 static tree
5914 c_parser_asm_clobbers (c_parser *parser)
5916 tree list = NULL_TREE;
5917 while (true)
5919 tree str = c_parser_asm_string_literal (parser);
5920 if (str)
5921 list = tree_cons (NULL_TREE, str, list);
5922 else
5923 return NULL_TREE;
5924 if (c_parser_next_token_is (parser, CPP_COMMA))
5925 c_parser_consume_token (parser);
5926 else
5927 break;
5929 return list;
5932 /* Parse asm goto labels, a GNU extension.
5934 asm-goto-operands:
5935 identifier
5936 asm-goto-operands , identifier
5939 static tree
5940 c_parser_asm_goto_operands (c_parser *parser)
5942 tree list = NULL_TREE;
5943 while (true)
5945 tree name, label;
5947 if (c_parser_next_token_is (parser, CPP_NAME))
5949 c_token *tok = c_parser_peek_token (parser);
5950 name = tok->value;
5951 label = lookup_label_for_goto (tok->location, name);
5952 c_parser_consume_token (parser);
5953 TREE_USED (label) = 1;
5955 else
5957 c_parser_error (parser, "expected identifier");
5958 return NULL_TREE;
5961 name = build_string (IDENTIFIER_LENGTH (name),
5962 IDENTIFIER_POINTER (name));
5963 list = tree_cons (name, label, list);
5964 if (c_parser_next_token_is (parser, CPP_COMMA))
5965 c_parser_consume_token (parser);
5966 else
5967 return nreverse (list);
5971 /* Parse an expression other than a compound expression; that is, an
5972 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5973 NULL then it is an Objective-C message expression which is the
5974 primary-expression starting the expression as an initializer.
5976 assignment-expression:
5977 conditional-expression
5978 unary-expression assignment-operator assignment-expression
5980 assignment-operator: one of
5981 = *= /= %= += -= <<= >>= &= ^= |=
5983 In GNU C we accept any conditional expression on the LHS and
5984 diagnose the invalid lvalue rather than producing a syntax
5985 error. */
5987 static struct c_expr
5988 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5989 tree omp_atomic_lhs)
5991 struct c_expr lhs, rhs, ret;
5992 enum tree_code code;
5993 location_t op_location, exp_location;
5994 gcc_assert (!after || c_dialect_objc ());
5995 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5996 op_location = c_parser_peek_token (parser)->location;
5997 switch (c_parser_peek_token (parser)->type)
5999 case CPP_EQ:
6000 code = NOP_EXPR;
6001 break;
6002 case CPP_MULT_EQ:
6003 code = MULT_EXPR;
6004 break;
6005 case CPP_DIV_EQ:
6006 code = TRUNC_DIV_EXPR;
6007 break;
6008 case CPP_MOD_EQ:
6009 code = TRUNC_MOD_EXPR;
6010 break;
6011 case CPP_PLUS_EQ:
6012 code = PLUS_EXPR;
6013 break;
6014 case CPP_MINUS_EQ:
6015 code = MINUS_EXPR;
6016 break;
6017 case CPP_LSHIFT_EQ:
6018 code = LSHIFT_EXPR;
6019 break;
6020 case CPP_RSHIFT_EQ:
6021 code = RSHIFT_EXPR;
6022 break;
6023 case CPP_AND_EQ:
6024 code = BIT_AND_EXPR;
6025 break;
6026 case CPP_XOR_EQ:
6027 code = BIT_XOR_EXPR;
6028 break;
6029 case CPP_OR_EQ:
6030 code = BIT_IOR_EXPR;
6031 break;
6032 default:
6033 return lhs;
6035 c_parser_consume_token (parser);
6036 exp_location = c_parser_peek_token (parser)->location;
6037 rhs = c_parser_expr_no_commas (parser, NULL);
6038 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6040 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6041 code, exp_location, rhs.value,
6042 rhs.original_type);
6043 if (code == NOP_EXPR)
6044 ret.original_code = MODIFY_EXPR;
6045 else
6047 TREE_NO_WARNING (ret.value) = 1;
6048 ret.original_code = ERROR_MARK;
6050 ret.original_type = NULL;
6051 return ret;
6054 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6055 is not NULL then it is an Objective-C message expression which is
6056 the primary-expression starting the expression as an initializer.
6058 conditional-expression:
6059 logical-OR-expression
6060 logical-OR-expression ? expression : conditional-expression
6062 GNU extensions:
6064 conditional-expression:
6065 logical-OR-expression ? : conditional-expression
6068 static struct c_expr
6069 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6070 tree omp_atomic_lhs)
6072 struct c_expr cond, exp1, exp2, ret;
6073 location_t cond_loc, colon_loc, middle_loc;
6075 gcc_assert (!after || c_dialect_objc ());
6077 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6079 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6080 return cond;
6081 cond_loc = c_parser_peek_token (parser)->location;
6082 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6083 c_parser_consume_token (parser);
6084 if (c_parser_next_token_is (parser, CPP_COLON))
6086 tree eptype = NULL_TREE;
6088 middle_loc = c_parser_peek_token (parser)->location;
6089 pedwarn (middle_loc, OPT_Wpedantic,
6090 "ISO C forbids omitting the middle term of a ?: expression");
6091 warn_for_omitted_condop (middle_loc, cond.value);
6092 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6094 eptype = TREE_TYPE (cond.value);
6095 cond.value = TREE_OPERAND (cond.value, 0);
6097 /* Make sure first operand is calculated only once. */
6098 exp1.value = c_save_expr (default_conversion (cond.value));
6099 if (eptype)
6100 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6101 exp1.original_type = NULL;
6102 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6103 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6105 else
6107 cond.value
6108 = c_objc_common_truthvalue_conversion
6109 (cond_loc, default_conversion (cond.value));
6110 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6111 exp1 = c_parser_expression_conv (parser);
6112 mark_exp_read (exp1.value);
6113 c_inhibit_evaluation_warnings +=
6114 ((cond.value == truthvalue_true_node)
6115 - (cond.value == truthvalue_false_node));
6118 colon_loc = c_parser_peek_token (parser)->location;
6119 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6121 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6122 ret.value = error_mark_node;
6123 ret.original_code = ERROR_MARK;
6124 ret.original_type = NULL;
6125 return ret;
6128 location_t exp2_loc = c_parser_peek_token (parser)->location;
6129 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6130 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6132 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6133 ret.value = build_conditional_expr (colon_loc, cond.value,
6134 cond.original_code == C_MAYBE_CONST_EXPR,
6135 exp1.value, exp1.original_type,
6136 exp2.value, exp2.original_type);
6137 ret.original_code = ERROR_MARK;
6138 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6139 ret.original_type = NULL;
6140 else
6142 tree t1, t2;
6144 /* If both sides are enum type, the default conversion will have
6145 made the type of the result be an integer type. We want to
6146 remember the enum types we started with. */
6147 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6148 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6149 ret.original_type = ((t1 != error_mark_node
6150 && t2 != error_mark_node
6151 && (TYPE_MAIN_VARIANT (t1)
6152 == TYPE_MAIN_VARIANT (t2)))
6153 ? t1
6154 : NULL);
6156 return ret;
6159 /* Parse a binary expression; that is, a logical-OR-expression (C90
6160 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6161 an Objective-C message expression which is the primary-expression
6162 starting the expression as an initializer.
6164 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6165 when it should be the unfolded lhs. In a valid OpenMP source,
6166 one of the operands of the toplevel binary expression must be equal
6167 to it. In that case, just return a build2 created binary operation
6168 rather than result of parser_build_binary_op.
6170 multiplicative-expression:
6171 cast-expression
6172 multiplicative-expression * cast-expression
6173 multiplicative-expression / cast-expression
6174 multiplicative-expression % cast-expression
6176 additive-expression:
6177 multiplicative-expression
6178 additive-expression + multiplicative-expression
6179 additive-expression - multiplicative-expression
6181 shift-expression:
6182 additive-expression
6183 shift-expression << additive-expression
6184 shift-expression >> additive-expression
6186 relational-expression:
6187 shift-expression
6188 relational-expression < shift-expression
6189 relational-expression > shift-expression
6190 relational-expression <= shift-expression
6191 relational-expression >= shift-expression
6193 equality-expression:
6194 relational-expression
6195 equality-expression == relational-expression
6196 equality-expression != relational-expression
6198 AND-expression:
6199 equality-expression
6200 AND-expression & equality-expression
6202 exclusive-OR-expression:
6203 AND-expression
6204 exclusive-OR-expression ^ AND-expression
6206 inclusive-OR-expression:
6207 exclusive-OR-expression
6208 inclusive-OR-expression | exclusive-OR-expression
6210 logical-AND-expression:
6211 inclusive-OR-expression
6212 logical-AND-expression && inclusive-OR-expression
6214 logical-OR-expression:
6215 logical-AND-expression
6216 logical-OR-expression || logical-AND-expression
6219 static struct c_expr
6220 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6221 tree omp_atomic_lhs)
6223 /* A binary expression is parsed using operator-precedence parsing,
6224 with the operands being cast expressions. All the binary
6225 operators are left-associative. Thus a binary expression is of
6226 form:
6228 E0 op1 E1 op2 E2 ...
6230 which we represent on a stack. On the stack, the precedence
6231 levels are strictly increasing. When a new operator is
6232 encountered of higher precedence than that at the top of the
6233 stack, it is pushed; its LHS is the top expression, and its RHS
6234 is everything parsed until it is popped. When a new operator is
6235 encountered with precedence less than or equal to that at the top
6236 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6237 by the result of the operation until the operator at the top of
6238 the stack has lower precedence than the new operator or there is
6239 only one element on the stack; then the top expression is the LHS
6240 of the new operator. In the case of logical AND and OR
6241 expressions, we also need to adjust c_inhibit_evaluation_warnings
6242 as appropriate when the operators are pushed and popped. */
6244 struct {
6245 /* The expression at this stack level. */
6246 struct c_expr expr;
6247 /* The precedence of the operator on its left, PREC_NONE at the
6248 bottom of the stack. */
6249 enum c_parser_prec prec;
6250 /* The operation on its left. */
6251 enum tree_code op;
6252 /* The source location of this operation. */
6253 location_t loc;
6254 } stack[NUM_PRECS];
6255 int sp;
6256 /* Location of the binary operator. */
6257 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6258 #define POP \
6259 do { \
6260 switch (stack[sp].op) \
6262 case TRUTH_ANDIF_EXPR: \
6263 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6264 == truthvalue_false_node); \
6265 break; \
6266 case TRUTH_ORIF_EXPR: \
6267 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6268 == truthvalue_true_node); \
6269 break; \
6270 default: \
6271 break; \
6273 stack[sp - 1].expr \
6274 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6275 stack[sp - 1].expr, true, true); \
6276 stack[sp].expr \
6277 = convert_lvalue_to_rvalue (stack[sp].loc, \
6278 stack[sp].expr, true, true); \
6279 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6280 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6281 && ((1 << stack[sp].prec) \
6282 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6283 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6284 && stack[sp].op != TRUNC_MOD_EXPR \
6285 && stack[0].expr.value != error_mark_node \
6286 && stack[1].expr.value != error_mark_node \
6287 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6288 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6289 stack[0].expr.value \
6290 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6291 stack[0].expr.value, stack[1].expr.value); \
6292 else \
6293 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6294 stack[sp].op, \
6295 stack[sp - 1].expr, \
6296 stack[sp].expr); \
6297 sp--; \
6298 } while (0)
6299 gcc_assert (!after || c_dialect_objc ());
6300 stack[0].loc = c_parser_peek_token (parser)->location;
6301 stack[0].expr = c_parser_cast_expression (parser, after);
6302 stack[0].prec = PREC_NONE;
6303 sp = 0;
6304 while (true)
6306 enum c_parser_prec oprec;
6307 enum tree_code ocode;
6308 if (parser->error)
6309 goto out;
6310 switch (c_parser_peek_token (parser)->type)
6312 case CPP_MULT:
6313 oprec = PREC_MULT;
6314 ocode = MULT_EXPR;
6315 break;
6316 case CPP_DIV:
6317 oprec = PREC_MULT;
6318 ocode = TRUNC_DIV_EXPR;
6319 break;
6320 case CPP_MOD:
6321 oprec = PREC_MULT;
6322 ocode = TRUNC_MOD_EXPR;
6323 break;
6324 case CPP_PLUS:
6325 oprec = PREC_ADD;
6326 ocode = PLUS_EXPR;
6327 break;
6328 case CPP_MINUS:
6329 oprec = PREC_ADD;
6330 ocode = MINUS_EXPR;
6331 break;
6332 case CPP_LSHIFT:
6333 oprec = PREC_SHIFT;
6334 ocode = LSHIFT_EXPR;
6335 break;
6336 case CPP_RSHIFT:
6337 oprec = PREC_SHIFT;
6338 ocode = RSHIFT_EXPR;
6339 break;
6340 case CPP_LESS:
6341 oprec = PREC_REL;
6342 ocode = LT_EXPR;
6343 break;
6344 case CPP_GREATER:
6345 oprec = PREC_REL;
6346 ocode = GT_EXPR;
6347 break;
6348 case CPP_LESS_EQ:
6349 oprec = PREC_REL;
6350 ocode = LE_EXPR;
6351 break;
6352 case CPP_GREATER_EQ:
6353 oprec = PREC_REL;
6354 ocode = GE_EXPR;
6355 break;
6356 case CPP_EQ_EQ:
6357 oprec = PREC_EQ;
6358 ocode = EQ_EXPR;
6359 break;
6360 case CPP_NOT_EQ:
6361 oprec = PREC_EQ;
6362 ocode = NE_EXPR;
6363 break;
6364 case CPP_AND:
6365 oprec = PREC_BITAND;
6366 ocode = BIT_AND_EXPR;
6367 break;
6368 case CPP_XOR:
6369 oprec = PREC_BITXOR;
6370 ocode = BIT_XOR_EXPR;
6371 break;
6372 case CPP_OR:
6373 oprec = PREC_BITOR;
6374 ocode = BIT_IOR_EXPR;
6375 break;
6376 case CPP_AND_AND:
6377 oprec = PREC_LOGAND;
6378 ocode = TRUTH_ANDIF_EXPR;
6379 break;
6380 case CPP_OR_OR:
6381 oprec = PREC_LOGOR;
6382 ocode = TRUTH_ORIF_EXPR;
6383 break;
6384 default:
6385 /* Not a binary operator, so end of the binary
6386 expression. */
6387 goto out;
6389 binary_loc = c_parser_peek_token (parser)->location;
6390 while (oprec <= stack[sp].prec)
6391 POP;
6392 c_parser_consume_token (parser);
6393 switch (ocode)
6395 case TRUTH_ANDIF_EXPR:
6396 stack[sp].expr
6397 = convert_lvalue_to_rvalue (stack[sp].loc,
6398 stack[sp].expr, true, true);
6399 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6400 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6401 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6402 == truthvalue_false_node);
6403 break;
6404 case TRUTH_ORIF_EXPR:
6405 stack[sp].expr
6406 = convert_lvalue_to_rvalue (stack[sp].loc,
6407 stack[sp].expr, true, true);
6408 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6409 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6410 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6411 == truthvalue_true_node);
6412 break;
6413 default:
6414 break;
6416 sp++;
6417 stack[sp].loc = binary_loc;
6418 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6419 stack[sp].prec = oprec;
6420 stack[sp].op = ocode;
6422 out:
6423 while (sp > 0)
6424 POP;
6425 return stack[0].expr;
6426 #undef POP
6429 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6430 NULL then it is an Objective-C message expression which is the
6431 primary-expression starting the expression as an initializer.
6433 cast-expression:
6434 unary-expression
6435 ( type-name ) unary-expression
6438 static struct c_expr
6439 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6441 location_t cast_loc = c_parser_peek_token (parser)->location;
6442 gcc_assert (!after || c_dialect_objc ());
6443 if (after)
6444 return c_parser_postfix_expression_after_primary (parser,
6445 cast_loc, *after);
6446 /* If the expression begins with a parenthesized type name, it may
6447 be either a cast or a compound literal; we need to see whether
6448 the next character is '{' to tell the difference. If not, it is
6449 an unary expression. Full detection of unknown typenames here
6450 would require a 3-token lookahead. */
6451 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6452 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6454 struct c_type_name *type_name;
6455 struct c_expr ret;
6456 struct c_expr expr;
6457 c_parser_consume_token (parser);
6458 type_name = c_parser_type_name (parser);
6459 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6460 if (type_name == NULL)
6462 ret.value = error_mark_node;
6463 ret.original_code = ERROR_MARK;
6464 ret.original_type = NULL;
6465 return ret;
6468 /* Save casted types in the function's used types hash table. */
6469 used_types_insert (type_name->specs->type);
6471 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6472 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6473 cast_loc);
6475 location_t expr_loc = c_parser_peek_token (parser)->location;
6476 expr = c_parser_cast_expression (parser, NULL);
6477 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6479 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6480 ret.original_code = ERROR_MARK;
6481 ret.original_type = NULL;
6482 return ret;
6484 else
6485 return c_parser_unary_expression (parser);
6488 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6490 unary-expression:
6491 postfix-expression
6492 ++ unary-expression
6493 -- unary-expression
6494 unary-operator cast-expression
6495 sizeof unary-expression
6496 sizeof ( type-name )
6498 unary-operator: one of
6499 & * + - ~ !
6501 GNU extensions:
6503 unary-expression:
6504 __alignof__ unary-expression
6505 __alignof__ ( type-name )
6506 && identifier
6508 (C11 permits _Alignof with type names only.)
6510 unary-operator: one of
6511 __extension__ __real__ __imag__
6513 Transactional Memory:
6515 unary-expression:
6516 transaction-expression
6518 In addition, the GNU syntax treats ++ and -- as unary operators, so
6519 they may be applied to cast expressions with errors for non-lvalues
6520 given later. */
6522 static struct c_expr
6523 c_parser_unary_expression (c_parser *parser)
6525 int ext;
6526 struct c_expr ret, op;
6527 location_t op_loc = c_parser_peek_token (parser)->location;
6528 location_t exp_loc;
6529 ret.original_code = ERROR_MARK;
6530 ret.original_type = NULL;
6531 switch (c_parser_peek_token (parser)->type)
6533 case CPP_PLUS_PLUS:
6534 c_parser_consume_token (parser);
6535 exp_loc = c_parser_peek_token (parser)->location;
6536 op = c_parser_cast_expression (parser, NULL);
6538 /* If there is array notations in op, we expand them. */
6539 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6540 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6541 else
6543 op = default_function_array_read_conversion (exp_loc, op);
6544 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6546 case CPP_MINUS_MINUS:
6547 c_parser_consume_token (parser);
6548 exp_loc = c_parser_peek_token (parser)->location;
6549 op = c_parser_cast_expression (parser, NULL);
6551 /* If there is array notations in op, we expand them. */
6552 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6553 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6554 else
6556 op = default_function_array_read_conversion (exp_loc, op);
6557 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6559 case CPP_AND:
6560 c_parser_consume_token (parser);
6561 op = c_parser_cast_expression (parser, NULL);
6562 mark_exp_read (op.value);
6563 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6564 case CPP_MULT:
6565 c_parser_consume_token (parser);
6566 exp_loc = c_parser_peek_token (parser)->location;
6567 op = c_parser_cast_expression (parser, NULL);
6568 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6569 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6570 return ret;
6571 case CPP_PLUS:
6572 if (!c_dialect_objc () && !in_system_header_at (input_location))
6573 warning_at (op_loc,
6574 OPT_Wtraditional,
6575 "traditional C rejects the unary plus operator");
6576 c_parser_consume_token (parser);
6577 exp_loc = c_parser_peek_token (parser)->location;
6578 op = c_parser_cast_expression (parser, NULL);
6579 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6580 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6581 case CPP_MINUS:
6582 c_parser_consume_token (parser);
6583 exp_loc = c_parser_peek_token (parser)->location;
6584 op = c_parser_cast_expression (parser, NULL);
6585 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6586 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6587 case CPP_COMPL:
6588 c_parser_consume_token (parser);
6589 exp_loc = c_parser_peek_token (parser)->location;
6590 op = c_parser_cast_expression (parser, NULL);
6591 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6592 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6593 case CPP_NOT:
6594 c_parser_consume_token (parser);
6595 exp_loc = c_parser_peek_token (parser)->location;
6596 op = c_parser_cast_expression (parser, NULL);
6597 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6598 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6599 case CPP_AND_AND:
6600 /* Refer to the address of a label as a pointer. */
6601 c_parser_consume_token (parser);
6602 if (c_parser_next_token_is (parser, CPP_NAME))
6604 ret.value = finish_label_address_expr
6605 (c_parser_peek_token (parser)->value, op_loc);
6606 c_parser_consume_token (parser);
6608 else
6610 c_parser_error (parser, "expected identifier");
6611 ret.value = error_mark_node;
6613 return ret;
6614 case CPP_KEYWORD:
6615 switch (c_parser_peek_token (parser)->keyword)
6617 case RID_SIZEOF:
6618 return c_parser_sizeof_expression (parser);
6619 case RID_ALIGNOF:
6620 return c_parser_alignof_expression (parser);
6621 case RID_EXTENSION:
6622 c_parser_consume_token (parser);
6623 ext = disable_extension_diagnostics ();
6624 ret = c_parser_cast_expression (parser, NULL);
6625 restore_extension_diagnostics (ext);
6626 return ret;
6627 case RID_REALPART:
6628 c_parser_consume_token (parser);
6629 exp_loc = c_parser_peek_token (parser)->location;
6630 op = c_parser_cast_expression (parser, NULL);
6631 op = default_function_array_conversion (exp_loc, op);
6632 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6633 case RID_IMAGPART:
6634 c_parser_consume_token (parser);
6635 exp_loc = c_parser_peek_token (parser)->location;
6636 op = c_parser_cast_expression (parser, NULL);
6637 op = default_function_array_conversion (exp_loc, op);
6638 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6639 case RID_TRANSACTION_ATOMIC:
6640 case RID_TRANSACTION_RELAXED:
6641 return c_parser_transaction_expression (parser,
6642 c_parser_peek_token (parser)->keyword);
6643 default:
6644 return c_parser_postfix_expression (parser);
6646 default:
6647 return c_parser_postfix_expression (parser);
6651 /* Parse a sizeof expression. */
6653 static struct c_expr
6654 c_parser_sizeof_expression (c_parser *parser)
6656 struct c_expr expr;
6657 location_t expr_loc;
6658 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6659 c_parser_consume_token (parser);
6660 c_inhibit_evaluation_warnings++;
6661 in_sizeof++;
6662 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6663 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6665 /* Either sizeof ( type-name ) or sizeof unary-expression
6666 starting with a compound literal. */
6667 struct c_type_name *type_name;
6668 c_parser_consume_token (parser);
6669 expr_loc = c_parser_peek_token (parser)->location;
6670 type_name = c_parser_type_name (parser);
6671 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6672 if (type_name == NULL)
6674 struct c_expr ret;
6675 c_inhibit_evaluation_warnings--;
6676 in_sizeof--;
6677 ret.value = error_mark_node;
6678 ret.original_code = ERROR_MARK;
6679 ret.original_type = NULL;
6680 return ret;
6682 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6684 expr = c_parser_postfix_expression_after_paren_type (parser,
6685 type_name,
6686 expr_loc);
6687 goto sizeof_expr;
6689 /* sizeof ( type-name ). */
6690 c_inhibit_evaluation_warnings--;
6691 in_sizeof--;
6692 return c_expr_sizeof_type (expr_loc, type_name);
6694 else
6696 expr_loc = c_parser_peek_token (parser)->location;
6697 expr = c_parser_unary_expression (parser);
6698 sizeof_expr:
6699 c_inhibit_evaluation_warnings--;
6700 in_sizeof--;
6701 mark_exp_read (expr.value);
6702 if (TREE_CODE (expr.value) == COMPONENT_REF
6703 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6704 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6705 return c_expr_sizeof_expr (expr_loc, expr);
6709 /* Parse an alignof expression. */
6711 static struct c_expr
6712 c_parser_alignof_expression (c_parser *parser)
6714 struct c_expr expr;
6715 location_t loc = c_parser_peek_token (parser)->location;
6716 tree alignof_spelling = c_parser_peek_token (parser)->value;
6717 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6718 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6719 "_Alignof") == 0;
6720 /* A diagnostic is not required for the use of this identifier in
6721 the implementation namespace; only diagnose it for the C11
6722 spelling because of existing code using the other spellings. */
6723 if (is_c11_alignof)
6725 if (flag_isoc99)
6726 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6727 alignof_spelling);
6728 else
6729 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6730 alignof_spelling);
6732 c_parser_consume_token (parser);
6733 c_inhibit_evaluation_warnings++;
6734 in_alignof++;
6735 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6736 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6738 /* Either __alignof__ ( type-name ) or __alignof__
6739 unary-expression starting with a compound literal. */
6740 location_t loc;
6741 struct c_type_name *type_name;
6742 struct c_expr ret;
6743 c_parser_consume_token (parser);
6744 loc = c_parser_peek_token (parser)->location;
6745 type_name = c_parser_type_name (parser);
6746 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6747 if (type_name == NULL)
6749 struct c_expr ret;
6750 c_inhibit_evaluation_warnings--;
6751 in_alignof--;
6752 ret.value = error_mark_node;
6753 ret.original_code = ERROR_MARK;
6754 ret.original_type = NULL;
6755 return ret;
6757 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6759 expr = c_parser_postfix_expression_after_paren_type (parser,
6760 type_name,
6761 loc);
6762 goto alignof_expr;
6764 /* alignof ( type-name ). */
6765 c_inhibit_evaluation_warnings--;
6766 in_alignof--;
6767 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6768 NULL, NULL),
6769 false, is_c11_alignof, 1);
6770 ret.original_code = ERROR_MARK;
6771 ret.original_type = NULL;
6772 return ret;
6774 else
6776 struct c_expr ret;
6777 expr = c_parser_unary_expression (parser);
6778 alignof_expr:
6779 mark_exp_read (expr.value);
6780 c_inhibit_evaluation_warnings--;
6781 in_alignof--;
6782 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6783 alignof_spelling);
6784 ret.value = c_alignof_expr (loc, expr.value);
6785 ret.original_code = ERROR_MARK;
6786 ret.original_type = NULL;
6787 return ret;
6791 /* Helper function to read arguments of builtins which are interfaces
6792 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6793 others. The name of the builtin is passed using BNAME parameter.
6794 Function returns true if there were no errors while parsing and
6795 stores the arguments in CEXPR_LIST. */
6796 static bool
6797 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6798 vec<c_expr_t, va_gc> **ret_cexpr_list,
6799 bool choose_expr_p)
6801 location_t loc = c_parser_peek_token (parser)->location;
6802 vec<c_expr_t, va_gc> *cexpr_list;
6803 c_expr_t expr;
6804 bool saved_force_folding_builtin_constant_p;
6806 *ret_cexpr_list = NULL;
6807 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6809 error_at (loc, "cannot take address of %qs", bname);
6810 return false;
6813 c_parser_consume_token (parser);
6815 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6817 c_parser_consume_token (parser);
6818 return true;
6821 saved_force_folding_builtin_constant_p
6822 = force_folding_builtin_constant_p;
6823 force_folding_builtin_constant_p |= choose_expr_p;
6824 expr = c_parser_expr_no_commas (parser, NULL);
6825 force_folding_builtin_constant_p
6826 = saved_force_folding_builtin_constant_p;
6827 vec_alloc (cexpr_list, 1);
6828 vec_safe_push (cexpr_list, expr);
6829 while (c_parser_next_token_is (parser, CPP_COMMA))
6831 c_parser_consume_token (parser);
6832 expr = c_parser_expr_no_commas (parser, NULL);
6833 vec_safe_push (cexpr_list, expr);
6836 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6837 return false;
6839 *ret_cexpr_list = cexpr_list;
6840 return true;
6843 /* This represents a single generic-association. */
6845 struct c_generic_association
6847 /* The location of the starting token of the type. */
6848 location_t type_location;
6849 /* The association's type, or NULL_TREE for 'default'. */
6850 tree type;
6851 /* The association's expression. */
6852 struct c_expr expression;
6855 /* Parse a generic-selection. (C11 6.5.1.1).
6857 generic-selection:
6858 _Generic ( assignment-expression , generic-assoc-list )
6860 generic-assoc-list:
6861 generic-association
6862 generic-assoc-list , generic-association
6864 generic-association:
6865 type-name : assignment-expression
6866 default : assignment-expression
6869 static struct c_expr
6870 c_parser_generic_selection (c_parser *parser)
6872 vec<c_generic_association> associations = vNULL;
6873 struct c_expr selector, error_expr;
6874 tree selector_type;
6875 struct c_generic_association matched_assoc;
6876 bool match_found = false;
6877 location_t generic_loc, selector_loc;
6879 error_expr.original_code = ERROR_MARK;
6880 error_expr.original_type = NULL;
6881 error_expr.value = error_mark_node;
6882 matched_assoc.type_location = UNKNOWN_LOCATION;
6883 matched_assoc.type = NULL_TREE;
6884 matched_assoc.expression = error_expr;
6886 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6887 generic_loc = c_parser_peek_token (parser)->location;
6888 c_parser_consume_token (parser);
6889 if (flag_isoc99)
6890 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6891 "ISO C99 does not support %<_Generic%>");
6892 else
6893 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6894 "ISO C90 does not support %<_Generic%>");
6896 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6897 return error_expr;
6899 c_inhibit_evaluation_warnings++;
6900 selector_loc = c_parser_peek_token (parser)->location;
6901 selector = c_parser_expr_no_commas (parser, NULL);
6902 selector = default_function_array_conversion (selector_loc, selector);
6903 c_inhibit_evaluation_warnings--;
6905 if (selector.value == error_mark_node)
6907 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6908 return selector;
6910 selector_type = TREE_TYPE (selector.value);
6911 /* In ISO C terms, rvalues (including the controlling expression of
6912 _Generic) do not have qualified types. */
6913 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6914 selector_type = TYPE_MAIN_VARIANT (selector_type);
6915 /* In ISO C terms, _Noreturn is not part of the type of expressions
6916 such as &abort, but in GCC it is represented internally as a type
6917 qualifier. */
6918 if (FUNCTION_POINTER_TYPE_P (selector_type)
6919 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6920 selector_type
6921 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6923 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6926 return error_expr;
6929 while (1)
6931 struct c_generic_association assoc, *iter;
6932 unsigned int ix;
6933 c_token *token = c_parser_peek_token (parser);
6935 assoc.type_location = token->location;
6936 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6938 c_parser_consume_token (parser);
6939 assoc.type = NULL_TREE;
6941 else
6943 struct c_type_name *type_name;
6945 type_name = c_parser_type_name (parser);
6946 if (type_name == NULL)
6948 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6949 goto error_exit;
6951 assoc.type = groktypename (type_name, NULL, NULL);
6952 if (assoc.type == error_mark_node)
6954 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6955 goto error_exit;
6958 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6959 error_at (assoc.type_location,
6960 "%<_Generic%> association has function type");
6961 else if (!COMPLETE_TYPE_P (assoc.type))
6962 error_at (assoc.type_location,
6963 "%<_Generic%> association has incomplete type");
6965 if (variably_modified_type_p (assoc.type, NULL_TREE))
6966 error_at (assoc.type_location,
6967 "%<_Generic%> association has "
6968 "variable length type");
6971 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6973 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6974 goto error_exit;
6977 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6978 if (assoc.expression.value == error_mark_node)
6980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6981 goto error_exit;
6984 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6986 if (assoc.type == NULL_TREE)
6988 if (iter->type == NULL_TREE)
6990 error_at (assoc.type_location,
6991 "duplicate %<default%> case in %<_Generic%>");
6992 inform (iter->type_location, "original %<default%> is here");
6995 else if (iter->type != NULL_TREE)
6997 if (comptypes (assoc.type, iter->type))
6999 error_at (assoc.type_location,
7000 "%<_Generic%> specifies two compatible types");
7001 inform (iter->type_location, "compatible type is here");
7006 if (assoc.type == NULL_TREE)
7008 if (!match_found)
7010 matched_assoc = assoc;
7011 match_found = true;
7014 else if (comptypes (assoc.type, selector_type))
7016 if (!match_found || matched_assoc.type == NULL_TREE)
7018 matched_assoc = assoc;
7019 match_found = true;
7021 else
7023 error_at (assoc.type_location,
7024 "%<_Generic> selector matches multiple associations");
7025 inform (matched_assoc.type_location,
7026 "other match is here");
7030 associations.safe_push (assoc);
7032 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7033 break;
7034 c_parser_consume_token (parser);
7037 associations.release ();
7039 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7042 return error_expr;
7045 if (!match_found)
7047 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7048 "compatible with any association",
7049 selector_type);
7050 return error_expr;
7053 return matched_assoc.expression;
7055 error_exit:
7056 associations.release ();
7057 return error_expr;
7060 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7062 postfix-expression:
7063 primary-expression
7064 postfix-expression [ expression ]
7065 postfix-expression ( argument-expression-list[opt] )
7066 postfix-expression . identifier
7067 postfix-expression -> identifier
7068 postfix-expression ++
7069 postfix-expression --
7070 ( type-name ) { initializer-list }
7071 ( type-name ) { initializer-list , }
7073 argument-expression-list:
7074 argument-expression
7075 argument-expression-list , argument-expression
7077 primary-expression:
7078 identifier
7079 constant
7080 string-literal
7081 ( expression )
7082 generic-selection
7084 GNU extensions:
7086 primary-expression:
7087 __func__
7088 (treated as a keyword in GNU C)
7089 __FUNCTION__
7090 __PRETTY_FUNCTION__
7091 ( compound-statement )
7092 __builtin_va_arg ( assignment-expression , type-name )
7093 __builtin_offsetof ( type-name , offsetof-member-designator )
7094 __builtin_choose_expr ( assignment-expression ,
7095 assignment-expression ,
7096 assignment-expression )
7097 __builtin_types_compatible_p ( type-name , type-name )
7098 __builtin_complex ( assignment-expression , assignment-expression )
7099 __builtin_shuffle ( assignment-expression , assignment-expression )
7100 __builtin_shuffle ( assignment-expression ,
7101 assignment-expression ,
7102 assignment-expression, )
7104 offsetof-member-designator:
7105 identifier
7106 offsetof-member-designator . identifier
7107 offsetof-member-designator [ expression ]
7109 Objective-C:
7111 primary-expression:
7112 [ objc-receiver objc-message-args ]
7113 @selector ( objc-selector-arg )
7114 @protocol ( identifier )
7115 @encode ( type-name )
7116 objc-string-literal
7117 Classname . identifier
7120 static struct c_expr
7121 c_parser_postfix_expression (c_parser *parser)
7123 struct c_expr expr, e1;
7124 struct c_type_name *t1, *t2;
7125 location_t loc = c_parser_peek_token (parser)->location;;
7126 expr.original_code = ERROR_MARK;
7127 expr.original_type = NULL;
7128 switch (c_parser_peek_token (parser)->type)
7130 case CPP_NUMBER:
7131 expr.value = c_parser_peek_token (parser)->value;
7132 loc = c_parser_peek_token (parser)->location;
7133 c_parser_consume_token (parser);
7134 if (TREE_CODE (expr.value) == FIXED_CST
7135 && !targetm.fixed_point_supported_p ())
7137 error_at (loc, "fixed-point types not supported for this target");
7138 expr.value = error_mark_node;
7140 break;
7141 case CPP_CHAR:
7142 case CPP_CHAR16:
7143 case CPP_CHAR32:
7144 case CPP_WCHAR:
7145 expr.value = c_parser_peek_token (parser)->value;
7146 c_parser_consume_token (parser);
7147 break;
7148 case CPP_STRING:
7149 case CPP_STRING16:
7150 case CPP_STRING32:
7151 case CPP_WSTRING:
7152 case CPP_UTF8STRING:
7153 expr.value = c_parser_peek_token (parser)->value;
7154 expr.original_code = STRING_CST;
7155 c_parser_consume_token (parser);
7156 break;
7157 case CPP_OBJC_STRING:
7158 gcc_assert (c_dialect_objc ());
7159 expr.value
7160 = objc_build_string_object (c_parser_peek_token (parser)->value);
7161 c_parser_consume_token (parser);
7162 break;
7163 case CPP_NAME:
7164 switch (c_parser_peek_token (parser)->id_kind)
7166 case C_ID_ID:
7168 tree id = c_parser_peek_token (parser)->value;
7169 c_parser_consume_token (parser);
7170 expr.value = build_external_ref (loc, id,
7171 (c_parser_peek_token (parser)->type
7172 == CPP_OPEN_PAREN),
7173 &expr.original_type);
7174 break;
7176 case C_ID_CLASSNAME:
7178 /* Here we parse the Objective-C 2.0 Class.name dot
7179 syntax. */
7180 tree class_name = c_parser_peek_token (parser)->value;
7181 tree component;
7182 c_parser_consume_token (parser);
7183 gcc_assert (c_dialect_objc ());
7184 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7186 expr.value = error_mark_node;
7187 break;
7189 if (c_parser_next_token_is_not (parser, CPP_NAME))
7191 c_parser_error (parser, "expected identifier");
7192 expr.value = error_mark_node;
7193 break;
7195 component = c_parser_peek_token (parser)->value;
7196 c_parser_consume_token (parser);
7197 expr.value = objc_build_class_component_ref (class_name,
7198 component);
7199 break;
7201 default:
7202 c_parser_error (parser, "expected expression");
7203 expr.value = error_mark_node;
7204 break;
7206 break;
7207 case CPP_OPEN_PAREN:
7208 /* A parenthesized expression, statement expression or compound
7209 literal. */
7210 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7212 /* A statement expression. */
7213 tree stmt;
7214 location_t brace_loc;
7215 c_parser_consume_token (parser);
7216 brace_loc = c_parser_peek_token (parser)->location;
7217 c_parser_consume_token (parser);
7218 if (!building_stmt_list_p ())
7220 error_at (loc, "braced-group within expression allowed "
7221 "only inside a function");
7222 parser->error = true;
7223 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7224 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7225 expr.value = error_mark_node;
7226 break;
7228 stmt = c_begin_stmt_expr ();
7229 c_parser_compound_statement_nostart (parser);
7230 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7231 "expected %<)%>");
7232 pedwarn (loc, OPT_Wpedantic,
7233 "ISO C forbids braced-groups within expressions");
7234 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7235 mark_exp_read (expr.value);
7237 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7239 /* A compound literal. ??? Can we actually get here rather
7240 than going directly to
7241 c_parser_postfix_expression_after_paren_type from
7242 elsewhere? */
7243 location_t loc;
7244 struct c_type_name *type_name;
7245 c_parser_consume_token (parser);
7246 loc = c_parser_peek_token (parser)->location;
7247 type_name = c_parser_type_name (parser);
7248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7249 "expected %<)%>");
7250 if (type_name == NULL)
7252 expr.value = error_mark_node;
7254 else
7255 expr = c_parser_postfix_expression_after_paren_type (parser,
7256 type_name,
7257 loc);
7259 else
7261 /* A parenthesized expression. */
7262 c_parser_consume_token (parser);
7263 expr = c_parser_expression (parser);
7264 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7265 TREE_NO_WARNING (expr.value) = 1;
7266 if (expr.original_code != C_MAYBE_CONST_EXPR)
7267 expr.original_code = ERROR_MARK;
7268 /* Don't change EXPR.ORIGINAL_TYPE. */
7269 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7270 "expected %<)%>");
7272 break;
7273 case CPP_KEYWORD:
7274 switch (c_parser_peek_token (parser)->keyword)
7276 case RID_FUNCTION_NAME:
7277 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7278 "%<__FUNCTION__%> predefined identifier");
7279 expr.value = fname_decl (loc,
7280 c_parser_peek_token (parser)->keyword,
7281 c_parser_peek_token (parser)->value);
7282 c_parser_consume_token (parser);
7283 break;
7284 case RID_PRETTY_FUNCTION_NAME:
7285 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7286 "%<__PRETTY_FUNCTION__%> predefined identifier");
7287 expr.value = fname_decl (loc,
7288 c_parser_peek_token (parser)->keyword,
7289 c_parser_peek_token (parser)->value);
7290 c_parser_consume_token (parser);
7291 break;
7292 case RID_C99_FUNCTION_NAME:
7293 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7294 "%<__func__%> predefined identifier");
7295 expr.value = fname_decl (loc,
7296 c_parser_peek_token (parser)->keyword,
7297 c_parser_peek_token (parser)->value);
7298 c_parser_consume_token (parser);
7299 break;
7300 case RID_VA_ARG:
7301 c_parser_consume_token (parser);
7302 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7304 expr.value = error_mark_node;
7305 break;
7307 e1 = c_parser_expr_no_commas (parser, NULL);
7308 mark_exp_read (e1.value);
7309 e1.value = c_fully_fold (e1.value, false, NULL);
7310 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7313 expr.value = error_mark_node;
7314 break;
7316 loc = c_parser_peek_token (parser)->location;
7317 t1 = c_parser_type_name (parser);
7318 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7319 "expected %<)%>");
7320 if (t1 == NULL)
7322 expr.value = error_mark_node;
7324 else
7326 tree type_expr = NULL_TREE;
7327 expr.value = c_build_va_arg (loc, e1.value,
7328 groktypename (t1, &type_expr, NULL));
7329 if (type_expr)
7331 expr.value = build2 (C_MAYBE_CONST_EXPR,
7332 TREE_TYPE (expr.value), type_expr,
7333 expr.value);
7334 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7337 break;
7338 case RID_OFFSETOF:
7339 c_parser_consume_token (parser);
7340 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7342 expr.value = error_mark_node;
7343 break;
7345 t1 = c_parser_type_name (parser);
7346 if (t1 == NULL)
7347 parser->error = true;
7348 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7349 gcc_assert (parser->error);
7350 if (parser->error)
7352 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7353 expr.value = error_mark_node;
7354 break;
7358 tree type = groktypename (t1, NULL, NULL);
7359 tree offsetof_ref;
7360 if (type == error_mark_node)
7361 offsetof_ref = error_mark_node;
7362 else
7364 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7365 SET_EXPR_LOCATION (offsetof_ref, loc);
7367 /* Parse the second argument to __builtin_offsetof. We
7368 must have one identifier, and beyond that we want to
7369 accept sub structure and sub array references. */
7370 if (c_parser_next_token_is (parser, CPP_NAME))
7372 offsetof_ref = build_component_ref
7373 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7374 c_parser_consume_token (parser);
7375 while (c_parser_next_token_is (parser, CPP_DOT)
7376 || c_parser_next_token_is (parser,
7377 CPP_OPEN_SQUARE)
7378 || c_parser_next_token_is (parser,
7379 CPP_DEREF))
7381 if (c_parser_next_token_is (parser, CPP_DEREF))
7383 loc = c_parser_peek_token (parser)->location;
7384 offsetof_ref = build_array_ref (loc,
7385 offsetof_ref,
7386 integer_zero_node);
7387 goto do_dot;
7389 else if (c_parser_next_token_is (parser, CPP_DOT))
7391 do_dot:
7392 c_parser_consume_token (parser);
7393 if (c_parser_next_token_is_not (parser,
7394 CPP_NAME))
7396 c_parser_error (parser, "expected identifier");
7397 break;
7399 offsetof_ref = build_component_ref
7400 (loc, offsetof_ref,
7401 c_parser_peek_token (parser)->value);
7402 c_parser_consume_token (parser);
7404 else
7406 struct c_expr ce;
7407 tree idx;
7408 loc = c_parser_peek_token (parser)->location;
7409 c_parser_consume_token (parser);
7410 ce = c_parser_expression (parser);
7411 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7412 idx = ce.value;
7413 idx = c_fully_fold (idx, false, NULL);
7414 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7415 "expected %<]%>");
7416 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7420 else
7421 c_parser_error (parser, "expected identifier");
7422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7423 "expected %<)%>");
7424 expr.value = fold_offsetof (offsetof_ref);
7426 break;
7427 case RID_CHOOSE_EXPR:
7429 vec<c_expr_t, va_gc> *cexpr_list;
7430 c_expr_t *e1_p, *e2_p, *e3_p;
7431 tree c;
7433 c_parser_consume_token (parser);
7434 if (!c_parser_get_builtin_args (parser,
7435 "__builtin_choose_expr",
7436 &cexpr_list, true))
7438 expr.value = error_mark_node;
7439 break;
7442 if (vec_safe_length (cexpr_list) != 3)
7444 error_at (loc, "wrong number of arguments to "
7445 "%<__builtin_choose_expr%>");
7446 expr.value = error_mark_node;
7447 break;
7450 e1_p = &(*cexpr_list)[0];
7451 e2_p = &(*cexpr_list)[1];
7452 e3_p = &(*cexpr_list)[2];
7454 c = e1_p->value;
7455 mark_exp_read (e2_p->value);
7456 mark_exp_read (e3_p->value);
7457 if (TREE_CODE (c) != INTEGER_CST
7458 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7459 error_at (loc,
7460 "first argument to %<__builtin_choose_expr%> not"
7461 " a constant");
7462 constant_expression_warning (c);
7463 expr = integer_zerop (c) ? *e3_p : *e2_p;
7464 break;
7466 case RID_TYPES_COMPATIBLE_P:
7467 c_parser_consume_token (parser);
7468 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7470 expr.value = error_mark_node;
7471 break;
7473 t1 = c_parser_type_name (parser);
7474 if (t1 == NULL)
7476 expr.value = error_mark_node;
7477 break;
7479 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7481 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7482 expr.value = error_mark_node;
7483 break;
7485 t2 = c_parser_type_name (parser);
7486 if (t2 == NULL)
7488 expr.value = error_mark_node;
7489 break;
7491 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7492 "expected %<)%>");
7494 tree e1, e2;
7495 e1 = groktypename (t1, NULL, NULL);
7496 e2 = groktypename (t2, NULL, NULL);
7497 if (e1 == error_mark_node || e2 == error_mark_node)
7499 expr.value = error_mark_node;
7500 break;
7503 e1 = TYPE_MAIN_VARIANT (e1);
7504 e2 = TYPE_MAIN_VARIANT (e2);
7506 expr.value
7507 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7509 break;
7510 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7512 vec<c_expr_t, va_gc> *cexpr_list;
7513 c_expr_t *e2_p;
7514 tree chain_value;
7516 c_parser_consume_token (parser);
7517 if (!c_parser_get_builtin_args (parser,
7518 "__builtin_call_with_static_chain",
7519 &cexpr_list, false))
7521 expr.value = error_mark_node;
7522 break;
7524 if (vec_safe_length (cexpr_list) != 2)
7526 error_at (loc, "wrong number of arguments to "
7527 "%<__builtin_call_with_static_chain%>");
7528 expr.value = error_mark_node;
7529 break;
7532 expr = (*cexpr_list)[0];
7533 e2_p = &(*cexpr_list)[1];
7534 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7535 chain_value = e2_p->value;
7536 mark_exp_read (chain_value);
7538 if (TREE_CODE (expr.value) != CALL_EXPR)
7539 error_at (loc, "first argument to "
7540 "%<__builtin_call_with_static_chain%> "
7541 "must be a call expression");
7542 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7543 error_at (loc, "second argument to "
7544 "%<__builtin_call_with_static_chain%> "
7545 "must be a pointer type");
7546 else
7547 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7548 break;
7550 case RID_BUILTIN_COMPLEX:
7552 vec<c_expr_t, va_gc> *cexpr_list;
7553 c_expr_t *e1_p, *e2_p;
7555 c_parser_consume_token (parser);
7556 if (!c_parser_get_builtin_args (parser,
7557 "__builtin_complex",
7558 &cexpr_list, false))
7560 expr.value = error_mark_node;
7561 break;
7564 if (vec_safe_length (cexpr_list) != 2)
7566 error_at (loc, "wrong number of arguments to "
7567 "%<__builtin_complex%>");
7568 expr.value = error_mark_node;
7569 break;
7572 e1_p = &(*cexpr_list)[0];
7573 e2_p = &(*cexpr_list)[1];
7575 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7576 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7577 e1_p->value = convert (TREE_TYPE (e1_p->value),
7578 TREE_OPERAND (e1_p->value, 0));
7579 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7580 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7581 e2_p->value = convert (TREE_TYPE (e2_p->value),
7582 TREE_OPERAND (e2_p->value, 0));
7583 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7584 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7585 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7586 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7588 error_at (loc, "%<__builtin_complex%> operand "
7589 "not of real binary floating-point type");
7590 expr.value = error_mark_node;
7591 break;
7593 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7594 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7596 error_at (loc,
7597 "%<__builtin_complex%> operands of different types");
7598 expr.value = error_mark_node;
7599 break;
7601 pedwarn_c90 (loc, OPT_Wpedantic,
7602 "ISO C90 does not support complex types");
7603 expr.value = build2 (COMPLEX_EXPR,
7604 build_complex_type
7605 (TYPE_MAIN_VARIANT
7606 (TREE_TYPE (e1_p->value))),
7607 e1_p->value, e2_p->value);
7608 break;
7610 case RID_BUILTIN_SHUFFLE:
7612 vec<c_expr_t, va_gc> *cexpr_list;
7613 unsigned int i;
7614 c_expr_t *p;
7616 c_parser_consume_token (parser);
7617 if (!c_parser_get_builtin_args (parser,
7618 "__builtin_shuffle",
7619 &cexpr_list, false))
7621 expr.value = error_mark_node;
7622 break;
7625 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7626 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7628 if (vec_safe_length (cexpr_list) == 2)
7629 expr.value =
7630 c_build_vec_perm_expr
7631 (loc, (*cexpr_list)[0].value,
7632 NULL_TREE, (*cexpr_list)[1].value);
7634 else if (vec_safe_length (cexpr_list) == 3)
7635 expr.value =
7636 c_build_vec_perm_expr
7637 (loc, (*cexpr_list)[0].value,
7638 (*cexpr_list)[1].value,
7639 (*cexpr_list)[2].value);
7640 else
7642 error_at (loc, "wrong number of arguments to "
7643 "%<__builtin_shuffle%>");
7644 expr.value = error_mark_node;
7646 break;
7648 case RID_AT_SELECTOR:
7649 gcc_assert (c_dialect_objc ());
7650 c_parser_consume_token (parser);
7651 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7653 expr.value = error_mark_node;
7654 break;
7657 tree sel = c_parser_objc_selector_arg (parser);
7658 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7659 "expected %<)%>");
7660 expr.value = objc_build_selector_expr (loc, sel);
7662 break;
7663 case RID_AT_PROTOCOL:
7664 gcc_assert (c_dialect_objc ());
7665 c_parser_consume_token (parser);
7666 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7668 expr.value = error_mark_node;
7669 break;
7671 if (c_parser_next_token_is_not (parser, CPP_NAME))
7673 c_parser_error (parser, "expected identifier");
7674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7675 expr.value = error_mark_node;
7676 break;
7679 tree id = c_parser_peek_token (parser)->value;
7680 c_parser_consume_token (parser);
7681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7682 "expected %<)%>");
7683 expr.value = objc_build_protocol_expr (id);
7685 break;
7686 case RID_AT_ENCODE:
7687 /* Extension to support C-structures in the archiver. */
7688 gcc_assert (c_dialect_objc ());
7689 c_parser_consume_token (parser);
7690 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7692 expr.value = error_mark_node;
7693 break;
7695 t1 = c_parser_type_name (parser);
7696 if (t1 == NULL)
7698 expr.value = error_mark_node;
7699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7700 break;
7702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7703 "expected %<)%>");
7705 tree type = groktypename (t1, NULL, NULL);
7706 expr.value = objc_build_encode_expr (type);
7708 break;
7709 case RID_GENERIC:
7710 expr = c_parser_generic_selection (parser);
7711 break;
7712 case RID_CILK_SPAWN:
7713 c_parser_consume_token (parser);
7714 if (!flag_cilkplus)
7716 error_at (loc, "-fcilkplus must be enabled to use "
7717 "%<_Cilk_spawn%>");
7718 expr = c_parser_postfix_expression (parser);
7719 expr.value = error_mark_node;
7721 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7723 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7724 "are not permitted");
7725 /* Now flush out all the _Cilk_spawns. */
7726 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7727 c_parser_consume_token (parser);
7728 expr = c_parser_postfix_expression (parser);
7730 else
7732 expr = c_parser_postfix_expression (parser);
7733 expr.value = build_cilk_spawn (loc, expr.value);
7735 break;
7736 default:
7737 c_parser_error (parser, "expected expression");
7738 expr.value = error_mark_node;
7739 break;
7741 break;
7742 case CPP_OPEN_SQUARE:
7743 if (c_dialect_objc ())
7745 tree receiver, args;
7746 c_parser_consume_token (parser);
7747 receiver = c_parser_objc_receiver (parser);
7748 args = c_parser_objc_message_args (parser);
7749 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7750 "expected %<]%>");
7751 expr.value = objc_build_message_expr (receiver, args);
7752 break;
7754 /* Else fall through to report error. */
7755 default:
7756 c_parser_error (parser, "expected expression");
7757 expr.value = error_mark_node;
7758 break;
7760 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7763 /* Parse a postfix expression after a parenthesized type name: the
7764 brace-enclosed initializer of a compound literal, possibly followed
7765 by some postfix operators. This is separate because it is not
7766 possible to tell until after the type name whether a cast
7767 expression has a cast or a compound literal, or whether the operand
7768 of sizeof is a parenthesized type name or starts with a compound
7769 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7770 location of the first token after the parentheses around the type
7771 name. */
7773 static struct c_expr
7774 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7775 struct c_type_name *type_name,
7776 location_t type_loc)
7778 tree type;
7779 struct c_expr init;
7780 bool non_const;
7781 struct c_expr expr;
7782 location_t start_loc;
7783 tree type_expr = NULL_TREE;
7784 bool type_expr_const = true;
7785 check_compound_literal_type (type_loc, type_name);
7786 start_init (NULL_TREE, NULL, 0);
7787 type = groktypename (type_name, &type_expr, &type_expr_const);
7788 start_loc = c_parser_peek_token (parser)->location;
7789 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7791 error_at (type_loc, "compound literal has variable size");
7792 type = error_mark_node;
7794 init = c_parser_braced_init (parser, type, false);
7795 finish_init ();
7796 maybe_warn_string_init (type_loc, type, init);
7798 if (type != error_mark_node
7799 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7800 && current_function_decl)
7802 error ("compound literal qualified by address-space qualifier");
7803 type = error_mark_node;
7806 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7807 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7808 ? CONSTRUCTOR_NON_CONST (init.value)
7809 : init.original_code == C_MAYBE_CONST_EXPR);
7810 non_const |= !type_expr_const;
7811 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7812 expr.original_code = ERROR_MARK;
7813 expr.original_type = NULL;
7814 if (type_expr)
7816 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7818 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7819 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7821 else
7823 gcc_assert (!non_const);
7824 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7825 type_expr, expr.value);
7828 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7831 /* Callback function for sizeof_pointer_memaccess_warning to compare
7832 types. */
7834 static bool
7835 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7837 return comptypes (type1, type2) == 1;
7840 /* Parse a postfix expression after the initial primary or compound
7841 literal; that is, parse a series of postfix operators.
7843 EXPR_LOC is the location of the primary expression. */
7845 static struct c_expr
7846 c_parser_postfix_expression_after_primary (c_parser *parser,
7847 location_t expr_loc,
7848 struct c_expr expr)
7850 struct c_expr orig_expr;
7851 tree ident, idx;
7852 location_t sizeof_arg_loc[3];
7853 tree sizeof_arg[3];
7854 unsigned int literal_zero_mask;
7855 unsigned int i;
7856 vec<tree, va_gc> *exprlist;
7857 vec<tree, va_gc> *origtypes = NULL;
7858 vec<location_t> arg_loc = vNULL;
7860 while (true)
7862 location_t op_loc = c_parser_peek_token (parser)->location;
7863 switch (c_parser_peek_token (parser)->type)
7865 case CPP_OPEN_SQUARE:
7866 /* Array reference. */
7867 c_parser_consume_token (parser);
7868 if (flag_cilkplus
7869 && c_parser_peek_token (parser)->type == CPP_COLON)
7870 /* If we are here, then we have something like this:
7871 Array [ : ]
7873 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7874 expr.value);
7875 else
7877 idx = c_parser_expression (parser).value;
7878 /* Here we have 3 options:
7879 1. Array [EXPR] -- Normal Array call.
7880 2. Array [EXPR : EXPR] -- Array notation without stride.
7881 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7883 For 1, we just handle it just like a normal array expression.
7884 For 2 and 3 we handle it like we handle array notations. The
7885 idx value we have above becomes the initial/start index.
7887 if (flag_cilkplus
7888 && c_parser_peek_token (parser)->type == CPP_COLON)
7889 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7890 expr.value);
7891 else
7893 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7894 "expected %<]%>");
7895 expr.value = build_array_ref (op_loc, expr.value, idx);
7898 expr.original_code = ERROR_MARK;
7899 expr.original_type = NULL;
7900 break;
7901 case CPP_OPEN_PAREN:
7902 /* Function call. */
7903 c_parser_consume_token (parser);
7904 for (i = 0; i < 3; i++)
7906 sizeof_arg[i] = NULL_TREE;
7907 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7909 literal_zero_mask = 0;
7910 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7911 exprlist = NULL;
7912 else
7913 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7914 sizeof_arg_loc, sizeof_arg,
7915 &arg_loc, &literal_zero_mask);
7916 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7917 "expected %<)%>");
7918 orig_expr = expr;
7919 mark_exp_read (expr.value);
7920 if (warn_sizeof_pointer_memaccess)
7921 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7922 expr.value, exprlist,
7923 sizeof_arg,
7924 sizeof_ptr_memacc_comptypes);
7925 if (warn_memset_transposed_args
7926 && TREE_CODE (expr.value) == FUNCTION_DECL
7927 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7928 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7929 && vec_safe_length (exprlist) == 3
7930 && integer_zerop ((*exprlist)[2])
7931 && (literal_zero_mask & (1 << 2)) != 0
7932 && (!integer_zerop ((*exprlist)[1])
7933 || (literal_zero_mask & (1 << 1)) == 0))
7934 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7935 "%<memset%> used with constant zero length parameter; "
7936 "this could be due to transposed parameters");
7938 expr.value
7939 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7940 exprlist, origtypes);
7941 expr.original_code = ERROR_MARK;
7942 if (TREE_CODE (expr.value) == INTEGER_CST
7943 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7944 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7945 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7946 expr.original_code = C_MAYBE_CONST_EXPR;
7947 expr.original_type = NULL;
7948 if (exprlist)
7950 release_tree_vector (exprlist);
7951 release_tree_vector (origtypes);
7953 arg_loc.release ();
7954 break;
7955 case CPP_DOT:
7956 /* Structure element reference. */
7957 c_parser_consume_token (parser);
7958 expr = default_function_array_conversion (expr_loc, expr);
7959 if (c_parser_next_token_is (parser, CPP_NAME))
7960 ident = c_parser_peek_token (parser)->value;
7961 else
7963 c_parser_error (parser, "expected identifier");
7964 expr.value = error_mark_node;
7965 expr.original_code = ERROR_MARK;
7966 expr.original_type = NULL;
7967 return expr;
7969 c_parser_consume_token (parser);
7970 expr.value = build_component_ref (op_loc, expr.value, ident);
7971 expr.original_code = ERROR_MARK;
7972 if (TREE_CODE (expr.value) != COMPONENT_REF)
7973 expr.original_type = NULL;
7974 else
7976 /* Remember the original type of a bitfield. */
7977 tree field = TREE_OPERAND (expr.value, 1);
7978 if (TREE_CODE (field) != FIELD_DECL)
7979 expr.original_type = NULL;
7980 else
7981 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7983 break;
7984 case CPP_DEREF:
7985 /* Structure element reference. */
7986 c_parser_consume_token (parser);
7987 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7988 if (c_parser_next_token_is (parser, CPP_NAME))
7989 ident = c_parser_peek_token (parser)->value;
7990 else
7992 c_parser_error (parser, "expected identifier");
7993 expr.value = error_mark_node;
7994 expr.original_code = ERROR_MARK;
7995 expr.original_type = NULL;
7996 return expr;
7998 c_parser_consume_token (parser);
7999 expr.value = build_component_ref (op_loc,
8000 build_indirect_ref (op_loc,
8001 expr.value,
8002 RO_ARROW),
8003 ident);
8004 expr.original_code = ERROR_MARK;
8005 if (TREE_CODE (expr.value) != COMPONENT_REF)
8006 expr.original_type = NULL;
8007 else
8009 /* Remember the original type of a bitfield. */
8010 tree field = TREE_OPERAND (expr.value, 1);
8011 if (TREE_CODE (field) != FIELD_DECL)
8012 expr.original_type = NULL;
8013 else
8014 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8016 break;
8017 case CPP_PLUS_PLUS:
8018 /* Postincrement. */
8019 c_parser_consume_token (parser);
8020 /* If the expressions have array notations, we expand them. */
8021 if (flag_cilkplus
8022 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8023 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8024 else
8026 expr = default_function_array_read_conversion (expr_loc, expr);
8027 expr.value = build_unary_op (op_loc,
8028 POSTINCREMENT_EXPR, expr.value, 0);
8030 expr.original_code = ERROR_MARK;
8031 expr.original_type = NULL;
8032 break;
8033 case CPP_MINUS_MINUS:
8034 /* Postdecrement. */
8035 c_parser_consume_token (parser);
8036 /* If the expressions have array notations, we expand them. */
8037 if (flag_cilkplus
8038 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8039 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8040 else
8042 expr = default_function_array_read_conversion (expr_loc, expr);
8043 expr.value = build_unary_op (op_loc,
8044 POSTDECREMENT_EXPR, expr.value, 0);
8046 expr.original_code = ERROR_MARK;
8047 expr.original_type = NULL;
8048 break;
8049 default:
8050 return expr;
8055 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8057 expression:
8058 assignment-expression
8059 expression , assignment-expression
8062 static struct c_expr
8063 c_parser_expression (c_parser *parser)
8065 location_t tloc = c_parser_peek_token (parser)->location;
8066 struct c_expr expr;
8067 expr = c_parser_expr_no_commas (parser, NULL);
8068 if (c_parser_next_token_is (parser, CPP_COMMA))
8069 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8070 while (c_parser_next_token_is (parser, CPP_COMMA))
8072 struct c_expr next;
8073 tree lhsval;
8074 location_t loc = c_parser_peek_token (parser)->location;
8075 location_t expr_loc;
8076 c_parser_consume_token (parser);
8077 expr_loc = c_parser_peek_token (parser)->location;
8078 lhsval = expr.value;
8079 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8080 lhsval = TREE_OPERAND (lhsval, 1);
8081 if (DECL_P (lhsval) || handled_component_p (lhsval))
8082 mark_exp_read (lhsval);
8083 next = c_parser_expr_no_commas (parser, NULL);
8084 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8085 expr.value = build_compound_expr (loc, expr.value, next.value);
8086 expr.original_code = COMPOUND_EXPR;
8087 expr.original_type = next.original_type;
8089 return expr;
8092 /* Parse an expression and convert functions or arrays to pointers and
8093 lvalues to rvalues. */
8095 static struct c_expr
8096 c_parser_expression_conv (c_parser *parser)
8098 struct c_expr expr;
8099 location_t loc = c_parser_peek_token (parser)->location;
8100 expr = c_parser_expression (parser);
8101 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8102 return expr;
8105 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8106 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8108 static inline void
8109 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8110 unsigned int idx)
8112 if (idx >= HOST_BITS_PER_INT)
8113 return;
8115 c_token *tok = c_parser_peek_token (parser);
8116 switch (tok->type)
8118 case CPP_NUMBER:
8119 case CPP_CHAR:
8120 case CPP_WCHAR:
8121 case CPP_CHAR16:
8122 case CPP_CHAR32:
8123 /* If a parameter is literal zero alone, remember it
8124 for -Wmemset-transposed-args warning. */
8125 if (integer_zerop (tok->value)
8126 && !TREE_OVERFLOW (tok->value)
8127 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8128 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8129 *literal_zero_mask |= 1U << idx;
8130 default:
8131 break;
8135 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8136 functions and arrays to pointers and lvalues to rvalues. If
8137 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8138 locations of function arguments into this vector.
8140 nonempty-expr-list:
8141 assignment-expression
8142 nonempty-expr-list , assignment-expression
8145 static vec<tree, va_gc> *
8146 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8147 vec<tree, va_gc> **p_orig_types,
8148 location_t *sizeof_arg_loc, tree *sizeof_arg,
8149 vec<location_t> *locations,
8150 unsigned int *literal_zero_mask)
8152 vec<tree, va_gc> *ret;
8153 vec<tree, va_gc> *orig_types;
8154 struct c_expr expr;
8155 location_t loc = c_parser_peek_token (parser)->location;
8156 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8157 unsigned int idx = 0;
8159 ret = make_tree_vector ();
8160 if (p_orig_types == NULL)
8161 orig_types = NULL;
8162 else
8163 orig_types = make_tree_vector ();
8165 if (sizeof_arg != NULL
8166 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8167 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8168 if (literal_zero_mask)
8169 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8170 expr = c_parser_expr_no_commas (parser, NULL);
8171 if (convert_p)
8172 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8173 if (fold_p)
8174 expr.value = c_fully_fold (expr.value, false, NULL);
8175 ret->quick_push (expr.value);
8176 if (orig_types)
8177 orig_types->quick_push (expr.original_type);
8178 if (locations)
8179 locations->safe_push (loc);
8180 if (sizeof_arg != NULL
8181 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8182 && expr.original_code == SIZEOF_EXPR)
8184 sizeof_arg[0] = c_last_sizeof_arg;
8185 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8187 while (c_parser_next_token_is (parser, CPP_COMMA))
8189 c_parser_consume_token (parser);
8190 loc = c_parser_peek_token (parser)->location;
8191 if (sizeof_arg != NULL
8192 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8193 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8194 else
8195 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8196 if (literal_zero_mask)
8197 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8198 expr = c_parser_expr_no_commas (parser, NULL);
8199 if (convert_p)
8200 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8201 if (fold_p)
8202 expr.value = c_fully_fold (expr.value, false, NULL);
8203 vec_safe_push (ret, expr.value);
8204 if (orig_types)
8205 vec_safe_push (orig_types, expr.original_type);
8206 if (locations)
8207 locations->safe_push (loc);
8208 if (++idx < 3
8209 && sizeof_arg != NULL
8210 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8211 && expr.original_code == SIZEOF_EXPR)
8213 sizeof_arg[idx] = c_last_sizeof_arg;
8214 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8217 if (orig_types)
8218 *p_orig_types = orig_types;
8219 return ret;
8222 /* Parse Objective-C-specific constructs. */
8224 /* Parse an objc-class-definition.
8226 objc-class-definition:
8227 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8228 objc-class-instance-variables[opt] objc-methodprotolist @end
8229 @implementation identifier objc-superclass[opt]
8230 objc-class-instance-variables[opt]
8231 @interface identifier ( identifier ) objc-protocol-refs[opt]
8232 objc-methodprotolist @end
8233 @interface identifier ( ) objc-protocol-refs[opt]
8234 objc-methodprotolist @end
8235 @implementation identifier ( identifier )
8237 objc-superclass:
8238 : identifier
8240 "@interface identifier (" must start "@interface identifier (
8241 identifier ) ...": objc-methodprotolist in the first production may
8242 not start with a parenthesized identifier as a declarator of a data
8243 definition with no declaration specifiers if the objc-superclass,
8244 objc-protocol-refs and objc-class-instance-variables are omitted. */
8246 static void
8247 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8249 bool iface_p;
8250 tree id1;
8251 tree superclass;
8252 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8253 iface_p = true;
8254 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8255 iface_p = false;
8256 else
8257 gcc_unreachable ();
8259 c_parser_consume_token (parser);
8260 if (c_parser_next_token_is_not (parser, CPP_NAME))
8262 c_parser_error (parser, "expected identifier");
8263 return;
8265 id1 = c_parser_peek_token (parser)->value;
8266 c_parser_consume_token (parser);
8267 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8269 /* We have a category or class extension. */
8270 tree id2;
8271 tree proto = NULL_TREE;
8272 c_parser_consume_token (parser);
8273 if (c_parser_next_token_is_not (parser, CPP_NAME))
8275 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8277 /* We have a class extension. */
8278 id2 = NULL_TREE;
8280 else
8282 c_parser_error (parser, "expected identifier or %<)%>");
8283 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8284 return;
8287 else
8289 id2 = c_parser_peek_token (parser)->value;
8290 c_parser_consume_token (parser);
8292 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8293 if (!iface_p)
8295 objc_start_category_implementation (id1, id2);
8296 return;
8298 if (c_parser_next_token_is (parser, CPP_LESS))
8299 proto = c_parser_objc_protocol_refs (parser);
8300 objc_start_category_interface (id1, id2, proto, attributes);
8301 c_parser_objc_methodprotolist (parser);
8302 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8303 objc_finish_interface ();
8304 return;
8306 if (c_parser_next_token_is (parser, CPP_COLON))
8308 c_parser_consume_token (parser);
8309 if (c_parser_next_token_is_not (parser, CPP_NAME))
8311 c_parser_error (parser, "expected identifier");
8312 return;
8314 superclass = c_parser_peek_token (parser)->value;
8315 c_parser_consume_token (parser);
8317 else
8318 superclass = NULL_TREE;
8319 if (iface_p)
8321 tree proto = NULL_TREE;
8322 if (c_parser_next_token_is (parser, CPP_LESS))
8323 proto = c_parser_objc_protocol_refs (parser);
8324 objc_start_class_interface (id1, superclass, proto, attributes);
8326 else
8327 objc_start_class_implementation (id1, superclass);
8328 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8329 c_parser_objc_class_instance_variables (parser);
8330 if (iface_p)
8332 objc_continue_interface ();
8333 c_parser_objc_methodprotolist (parser);
8334 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8335 objc_finish_interface ();
8337 else
8339 objc_continue_implementation ();
8340 return;
8344 /* Parse objc-class-instance-variables.
8346 objc-class-instance-variables:
8347 { objc-instance-variable-decl-list[opt] }
8349 objc-instance-variable-decl-list:
8350 objc-visibility-spec
8351 objc-instance-variable-decl ;
8353 objc-instance-variable-decl-list objc-visibility-spec
8354 objc-instance-variable-decl-list objc-instance-variable-decl ;
8355 objc-instance-variable-decl-list ;
8357 objc-visibility-spec:
8358 @private
8359 @protected
8360 @public
8362 objc-instance-variable-decl:
8363 struct-declaration
8366 static void
8367 c_parser_objc_class_instance_variables (c_parser *parser)
8369 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8370 c_parser_consume_token (parser);
8371 while (c_parser_next_token_is_not (parser, CPP_EOF))
8373 tree decls;
8374 /* Parse any stray semicolon. */
8375 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8377 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8378 "extra semicolon");
8379 c_parser_consume_token (parser);
8380 continue;
8382 /* Stop if at the end of the instance variables. */
8383 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8385 c_parser_consume_token (parser);
8386 break;
8388 /* Parse any objc-visibility-spec. */
8389 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8391 c_parser_consume_token (parser);
8392 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8393 continue;
8395 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8397 c_parser_consume_token (parser);
8398 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8399 continue;
8401 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8403 c_parser_consume_token (parser);
8404 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8405 continue;
8407 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8409 c_parser_consume_token (parser);
8410 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8411 continue;
8413 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8415 c_parser_pragma (parser, pragma_external);
8416 continue;
8419 /* Parse some comma-separated declarations. */
8420 decls = c_parser_struct_declaration (parser);
8421 if (decls == NULL)
8423 /* There is a syntax error. We want to skip the offending
8424 tokens up to the next ';' (included) or '}'
8425 (excluded). */
8427 /* First, skip manually a ')' or ']'. This is because they
8428 reduce the nesting level, so c_parser_skip_until_found()
8429 wouldn't be able to skip past them. */
8430 c_token *token = c_parser_peek_token (parser);
8431 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8432 c_parser_consume_token (parser);
8434 /* Then, do the standard skipping. */
8435 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8437 /* We hopefully recovered. Start normal parsing again. */
8438 parser->error = false;
8439 continue;
8441 else
8443 /* Comma-separated instance variables are chained together
8444 in reverse order; add them one by one. */
8445 tree ivar = nreverse (decls);
8446 for (; ivar; ivar = DECL_CHAIN (ivar))
8447 objc_add_instance_variable (copy_node (ivar));
8449 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8453 /* Parse an objc-class-declaration.
8455 objc-class-declaration:
8456 @class identifier-list ;
8459 static void
8460 c_parser_objc_class_declaration (c_parser *parser)
8462 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8463 c_parser_consume_token (parser);
8464 /* Any identifiers, including those declared as type names, are OK
8465 here. */
8466 while (true)
8468 tree id;
8469 if (c_parser_next_token_is_not (parser, CPP_NAME))
8471 c_parser_error (parser, "expected identifier");
8472 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8473 parser->error = false;
8474 return;
8476 id = c_parser_peek_token (parser)->value;
8477 objc_declare_class (id);
8478 c_parser_consume_token (parser);
8479 if (c_parser_next_token_is (parser, CPP_COMMA))
8480 c_parser_consume_token (parser);
8481 else
8482 break;
8484 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8487 /* Parse an objc-alias-declaration.
8489 objc-alias-declaration:
8490 @compatibility_alias identifier identifier ;
8493 static void
8494 c_parser_objc_alias_declaration (c_parser *parser)
8496 tree id1, id2;
8497 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8498 c_parser_consume_token (parser);
8499 if (c_parser_next_token_is_not (parser, CPP_NAME))
8501 c_parser_error (parser, "expected identifier");
8502 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8503 return;
8505 id1 = c_parser_peek_token (parser)->value;
8506 c_parser_consume_token (parser);
8507 if (c_parser_next_token_is_not (parser, CPP_NAME))
8509 c_parser_error (parser, "expected identifier");
8510 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8511 return;
8513 id2 = c_parser_peek_token (parser)->value;
8514 c_parser_consume_token (parser);
8515 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8516 objc_declare_alias (id1, id2);
8519 /* Parse an objc-protocol-definition.
8521 objc-protocol-definition:
8522 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8523 @protocol identifier-list ;
8525 "@protocol identifier ;" should be resolved as "@protocol
8526 identifier-list ;": objc-methodprotolist may not start with a
8527 semicolon in the first alternative if objc-protocol-refs are
8528 omitted. */
8530 static void
8531 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8533 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8535 c_parser_consume_token (parser);
8536 if (c_parser_next_token_is_not (parser, CPP_NAME))
8538 c_parser_error (parser, "expected identifier");
8539 return;
8541 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8542 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8544 /* Any identifiers, including those declared as type names, are
8545 OK here. */
8546 while (true)
8548 tree id;
8549 if (c_parser_next_token_is_not (parser, CPP_NAME))
8551 c_parser_error (parser, "expected identifier");
8552 break;
8554 id = c_parser_peek_token (parser)->value;
8555 objc_declare_protocol (id, attributes);
8556 c_parser_consume_token (parser);
8557 if (c_parser_next_token_is (parser, CPP_COMMA))
8558 c_parser_consume_token (parser);
8559 else
8560 break;
8562 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8564 else
8566 tree id = c_parser_peek_token (parser)->value;
8567 tree proto = NULL_TREE;
8568 c_parser_consume_token (parser);
8569 if (c_parser_next_token_is (parser, CPP_LESS))
8570 proto = c_parser_objc_protocol_refs (parser);
8571 parser->objc_pq_context = true;
8572 objc_start_protocol (id, proto, attributes);
8573 c_parser_objc_methodprotolist (parser);
8574 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8575 parser->objc_pq_context = false;
8576 objc_finish_interface ();
8580 /* Parse an objc-method-type.
8582 objc-method-type:
8586 Return true if it is a class method (+) and false if it is
8587 an instance method (-).
8589 static inline bool
8590 c_parser_objc_method_type (c_parser *parser)
8592 switch (c_parser_peek_token (parser)->type)
8594 case CPP_PLUS:
8595 c_parser_consume_token (parser);
8596 return true;
8597 case CPP_MINUS:
8598 c_parser_consume_token (parser);
8599 return false;
8600 default:
8601 gcc_unreachable ();
8605 /* Parse an objc-method-definition.
8607 objc-method-definition:
8608 objc-method-type objc-method-decl ;[opt] compound-statement
8611 static void
8612 c_parser_objc_method_definition (c_parser *parser)
8614 bool is_class_method = c_parser_objc_method_type (parser);
8615 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8616 parser->objc_pq_context = true;
8617 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8618 &expr);
8619 if (decl == error_mark_node)
8620 return; /* Bail here. */
8622 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8624 c_parser_consume_token (parser);
8625 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8626 "extra semicolon in method definition specified");
8629 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8631 c_parser_error (parser, "expected %<{%>");
8632 return;
8635 parser->objc_pq_context = false;
8636 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8638 add_stmt (c_parser_compound_statement (parser));
8639 objc_finish_method_definition (current_function_decl);
8641 else
8643 /* This code is executed when we find a method definition
8644 outside of an @implementation context (or invalid for other
8645 reasons). Parse the method (to keep going) but do not emit
8646 any code.
8648 c_parser_compound_statement (parser);
8652 /* Parse an objc-methodprotolist.
8654 objc-methodprotolist:
8655 empty
8656 objc-methodprotolist objc-methodproto
8657 objc-methodprotolist declaration
8658 objc-methodprotolist ;
8659 @optional
8660 @required
8662 The declaration is a data definition, which may be missing
8663 declaration specifiers under the same rules and diagnostics as
8664 other data definitions outside functions, and the stray semicolon
8665 is diagnosed the same way as a stray semicolon outside a
8666 function. */
8668 static void
8669 c_parser_objc_methodprotolist (c_parser *parser)
8671 while (true)
8673 /* The list is terminated by @end. */
8674 switch (c_parser_peek_token (parser)->type)
8676 case CPP_SEMICOLON:
8677 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8678 "ISO C does not allow extra %<;%> outside of a function");
8679 c_parser_consume_token (parser);
8680 break;
8681 case CPP_PLUS:
8682 case CPP_MINUS:
8683 c_parser_objc_methodproto (parser);
8684 break;
8685 case CPP_PRAGMA:
8686 c_parser_pragma (parser, pragma_external);
8687 break;
8688 case CPP_EOF:
8689 return;
8690 default:
8691 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8692 return;
8693 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8694 c_parser_objc_at_property_declaration (parser);
8695 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8697 objc_set_method_opt (true);
8698 c_parser_consume_token (parser);
8700 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8702 objc_set_method_opt (false);
8703 c_parser_consume_token (parser);
8705 else
8706 c_parser_declaration_or_fndef (parser, false, false, true,
8707 false, true, NULL, vNULL);
8708 break;
8713 /* Parse an objc-methodproto.
8715 objc-methodproto:
8716 objc-method-type objc-method-decl ;
8719 static void
8720 c_parser_objc_methodproto (c_parser *parser)
8722 bool is_class_method = c_parser_objc_method_type (parser);
8723 tree decl, attributes = NULL_TREE;
8725 /* Remember protocol qualifiers in prototypes. */
8726 parser->objc_pq_context = true;
8727 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8728 NULL);
8729 /* Forget protocol qualifiers now. */
8730 parser->objc_pq_context = false;
8732 /* Do not allow the presence of attributes to hide an erroneous
8733 method implementation in the interface section. */
8734 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8736 c_parser_error (parser, "expected %<;%>");
8737 return;
8740 if (decl != error_mark_node)
8741 objc_add_method_declaration (is_class_method, decl, attributes);
8743 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8746 /* If we are at a position that method attributes may be present, check that
8747 there are not any parsed already (a syntax error) and then collect any
8748 specified at the current location. Finally, if new attributes were present,
8749 check that the next token is legal ( ';' for decls and '{' for defs). */
8751 static bool
8752 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8754 bool bad = false;
8755 if (*attributes)
8757 c_parser_error (parser,
8758 "method attributes must be specified at the end only");
8759 *attributes = NULL_TREE;
8760 bad = true;
8763 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8764 *attributes = c_parser_attributes (parser);
8766 /* If there were no attributes here, just report any earlier error. */
8767 if (*attributes == NULL_TREE || bad)
8768 return bad;
8770 /* If the attributes are followed by a ; or {, then just report any earlier
8771 error. */
8772 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8773 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8774 return bad;
8776 /* We've got attributes, but not at the end. */
8777 c_parser_error (parser,
8778 "expected %<;%> or %<{%> after method attribute definition");
8779 return true;
8782 /* Parse an objc-method-decl.
8784 objc-method-decl:
8785 ( objc-type-name ) objc-selector
8786 objc-selector
8787 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8788 objc-keyword-selector objc-optparmlist
8789 attributes
8791 objc-keyword-selector:
8792 objc-keyword-decl
8793 objc-keyword-selector objc-keyword-decl
8795 objc-keyword-decl:
8796 objc-selector : ( objc-type-name ) identifier
8797 objc-selector : identifier
8798 : ( objc-type-name ) identifier
8799 : identifier
8801 objc-optparmlist:
8802 objc-optparms objc-optellipsis
8804 objc-optparms:
8805 empty
8806 objc-opt-parms , parameter-declaration
8808 objc-optellipsis:
8809 empty
8810 , ...
8813 static tree
8814 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8815 tree *attributes, tree *expr)
8817 tree type = NULL_TREE;
8818 tree sel;
8819 tree parms = NULL_TREE;
8820 bool ellipsis = false;
8821 bool attr_err = false;
8823 *attributes = NULL_TREE;
8824 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8826 c_parser_consume_token (parser);
8827 type = c_parser_objc_type_name (parser);
8828 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8830 sel = c_parser_objc_selector (parser);
8831 /* If there is no selector, or a colon follows, we have an
8832 objc-keyword-selector. If there is a selector, and a colon does
8833 not follow, that selector ends the objc-method-decl. */
8834 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8836 tree tsel = sel;
8837 tree list = NULL_TREE;
8838 while (true)
8840 tree atype = NULL_TREE, id, keyworddecl;
8841 tree param_attr = NULL_TREE;
8842 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8843 break;
8844 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8846 c_parser_consume_token (parser);
8847 atype = c_parser_objc_type_name (parser);
8848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8849 "expected %<)%>");
8851 /* New ObjC allows attributes on method parameters. */
8852 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8853 param_attr = c_parser_attributes (parser);
8854 if (c_parser_next_token_is_not (parser, CPP_NAME))
8856 c_parser_error (parser, "expected identifier");
8857 return error_mark_node;
8859 id = c_parser_peek_token (parser)->value;
8860 c_parser_consume_token (parser);
8861 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8862 list = chainon (list, keyworddecl);
8863 tsel = c_parser_objc_selector (parser);
8864 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8865 break;
8868 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8870 /* Parse the optional parameter list. Optional Objective-C
8871 method parameters follow the C syntax, and may include '...'
8872 to denote a variable number of arguments. */
8873 parms = make_node (TREE_LIST);
8874 while (c_parser_next_token_is (parser, CPP_COMMA))
8876 struct c_parm *parm;
8877 c_parser_consume_token (parser);
8878 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8880 ellipsis = true;
8881 c_parser_consume_token (parser);
8882 attr_err |= c_parser_objc_maybe_method_attributes
8883 (parser, attributes) ;
8884 break;
8886 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8887 if (parm == NULL)
8888 break;
8889 parms = chainon (parms,
8890 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8892 sel = list;
8894 else
8895 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8897 if (sel == NULL)
8899 c_parser_error (parser, "objective-c method declaration is expected");
8900 return error_mark_node;
8903 if (attr_err)
8904 return error_mark_node;
8906 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8909 /* Parse an objc-type-name.
8911 objc-type-name:
8912 objc-type-qualifiers[opt] type-name
8913 objc-type-qualifiers[opt]
8915 objc-type-qualifiers:
8916 objc-type-qualifier
8917 objc-type-qualifiers objc-type-qualifier
8919 objc-type-qualifier: one of
8920 in out inout bycopy byref oneway
8923 static tree
8924 c_parser_objc_type_name (c_parser *parser)
8926 tree quals = NULL_TREE;
8927 struct c_type_name *type_name = NULL;
8928 tree type = NULL_TREE;
8929 while (true)
8931 c_token *token = c_parser_peek_token (parser);
8932 if (token->type == CPP_KEYWORD
8933 && (token->keyword == RID_IN
8934 || token->keyword == RID_OUT
8935 || token->keyword == RID_INOUT
8936 || token->keyword == RID_BYCOPY
8937 || token->keyword == RID_BYREF
8938 || token->keyword == RID_ONEWAY))
8940 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8941 c_parser_consume_token (parser);
8943 else
8944 break;
8946 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8947 type_name = c_parser_type_name (parser);
8948 if (type_name)
8949 type = groktypename (type_name, NULL, NULL);
8951 /* If the type is unknown, and error has already been produced and
8952 we need to recover from the error. In that case, use NULL_TREE
8953 for the type, as if no type had been specified; this will use the
8954 default type ('id') which is good for error recovery. */
8955 if (type == error_mark_node)
8956 type = NULL_TREE;
8958 return build_tree_list (quals, type);
8961 /* Parse objc-protocol-refs.
8963 objc-protocol-refs:
8964 < identifier-list >
8967 static tree
8968 c_parser_objc_protocol_refs (c_parser *parser)
8970 tree list = NULL_TREE;
8971 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8972 c_parser_consume_token (parser);
8973 /* Any identifiers, including those declared as type names, are OK
8974 here. */
8975 while (true)
8977 tree id;
8978 if (c_parser_next_token_is_not (parser, CPP_NAME))
8980 c_parser_error (parser, "expected identifier");
8981 break;
8983 id = c_parser_peek_token (parser)->value;
8984 list = chainon (list, build_tree_list (NULL_TREE, id));
8985 c_parser_consume_token (parser);
8986 if (c_parser_next_token_is (parser, CPP_COMMA))
8987 c_parser_consume_token (parser);
8988 else
8989 break;
8991 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8992 return list;
8995 /* Parse an objc-try-catch-finally-statement.
8997 objc-try-catch-finally-statement:
8998 @try compound-statement objc-catch-list[opt]
8999 @try compound-statement objc-catch-list[opt] @finally compound-statement
9001 objc-catch-list:
9002 @catch ( objc-catch-parameter-declaration ) compound-statement
9003 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9005 objc-catch-parameter-declaration:
9006 parameter-declaration
9007 '...'
9009 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9011 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9012 for C++. Keep them in sync. */
9014 static void
9015 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9017 location_t location;
9018 tree stmt;
9020 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9021 c_parser_consume_token (parser);
9022 location = c_parser_peek_token (parser)->location;
9023 objc_maybe_warn_exceptions (location);
9024 stmt = c_parser_compound_statement (parser);
9025 objc_begin_try_stmt (location, stmt);
9027 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9029 struct c_parm *parm;
9030 tree parameter_declaration = error_mark_node;
9031 bool seen_open_paren = false;
9033 c_parser_consume_token (parser);
9034 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9035 seen_open_paren = true;
9036 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9038 /* We have "@catch (...)" (where the '...' are literally
9039 what is in the code). Skip the '...'.
9040 parameter_declaration is set to NULL_TREE, and
9041 objc_being_catch_clauses() knows that that means
9042 '...'. */
9043 c_parser_consume_token (parser);
9044 parameter_declaration = NULL_TREE;
9046 else
9048 /* We have "@catch (NSException *exception)" or something
9049 like that. Parse the parameter declaration. */
9050 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9051 if (parm == NULL)
9052 parameter_declaration = error_mark_node;
9053 else
9054 parameter_declaration = grokparm (parm, NULL);
9056 if (seen_open_paren)
9057 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9058 else
9060 /* If there was no open parenthesis, we are recovering from
9061 an error, and we are trying to figure out what mistake
9062 the user has made. */
9064 /* If there is an immediate closing parenthesis, the user
9065 probably forgot the opening one (ie, they typed "@catch
9066 NSException *e)". Parse the closing parenthesis and keep
9067 going. */
9068 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9069 c_parser_consume_token (parser);
9071 /* If these is no immediate closing parenthesis, the user
9072 probably doesn't know that parenthesis are required at
9073 all (ie, they typed "@catch NSException *e"). So, just
9074 forget about the closing parenthesis and keep going. */
9076 objc_begin_catch_clause (parameter_declaration);
9077 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9078 c_parser_compound_statement_nostart (parser);
9079 objc_finish_catch_clause ();
9081 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9083 c_parser_consume_token (parser);
9084 location = c_parser_peek_token (parser)->location;
9085 stmt = c_parser_compound_statement (parser);
9086 objc_build_finally_clause (location, stmt);
9088 objc_finish_try_stmt ();
9091 /* Parse an objc-synchronized-statement.
9093 objc-synchronized-statement:
9094 @synchronized ( expression ) compound-statement
9097 static void
9098 c_parser_objc_synchronized_statement (c_parser *parser)
9100 location_t loc;
9101 tree expr, stmt;
9102 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9103 c_parser_consume_token (parser);
9104 loc = c_parser_peek_token (parser)->location;
9105 objc_maybe_warn_exceptions (loc);
9106 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9108 struct c_expr ce = c_parser_expression (parser);
9109 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9110 expr = ce.value;
9111 expr = c_fully_fold (expr, false, NULL);
9112 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9114 else
9115 expr = error_mark_node;
9116 stmt = c_parser_compound_statement (parser);
9117 objc_build_synchronized (loc, expr, stmt);
9120 /* Parse an objc-selector; return NULL_TREE without an error if the
9121 next token is not an objc-selector.
9123 objc-selector:
9124 identifier
9125 one of
9126 enum struct union if else while do for switch case default
9127 break continue return goto asm sizeof typeof __alignof
9128 unsigned long const short volatile signed restrict _Complex
9129 in out inout bycopy byref oneway int char float double void _Bool
9130 _Atomic
9132 ??? Why this selection of keywords but not, for example, storage
9133 class specifiers? */
9135 static tree
9136 c_parser_objc_selector (c_parser *parser)
9138 c_token *token = c_parser_peek_token (parser);
9139 tree value = token->value;
9140 if (token->type == CPP_NAME)
9142 c_parser_consume_token (parser);
9143 return value;
9145 if (token->type != CPP_KEYWORD)
9146 return NULL_TREE;
9147 switch (token->keyword)
9149 case RID_ENUM:
9150 case RID_STRUCT:
9151 case RID_UNION:
9152 case RID_IF:
9153 case RID_ELSE:
9154 case RID_WHILE:
9155 case RID_DO:
9156 case RID_FOR:
9157 case RID_SWITCH:
9158 case RID_CASE:
9159 case RID_DEFAULT:
9160 case RID_BREAK:
9161 case RID_CONTINUE:
9162 case RID_RETURN:
9163 case RID_GOTO:
9164 case RID_ASM:
9165 case RID_SIZEOF:
9166 case RID_TYPEOF:
9167 case RID_ALIGNOF:
9168 case RID_UNSIGNED:
9169 case RID_LONG:
9170 case RID_CONST:
9171 case RID_SHORT:
9172 case RID_VOLATILE:
9173 case RID_SIGNED:
9174 case RID_RESTRICT:
9175 case RID_COMPLEX:
9176 case RID_IN:
9177 case RID_OUT:
9178 case RID_INOUT:
9179 case RID_BYCOPY:
9180 case RID_BYREF:
9181 case RID_ONEWAY:
9182 case RID_INT:
9183 case RID_CHAR:
9184 case RID_FLOAT:
9185 case RID_DOUBLE:
9186 case RID_VOID:
9187 case RID_BOOL:
9188 case RID_ATOMIC:
9189 case RID_AUTO_TYPE:
9190 case RID_INT_N_0:
9191 case RID_INT_N_1:
9192 case RID_INT_N_2:
9193 case RID_INT_N_3:
9194 c_parser_consume_token (parser);
9195 return value;
9196 default:
9197 return NULL_TREE;
9201 /* Parse an objc-selector-arg.
9203 objc-selector-arg:
9204 objc-selector
9205 objc-keywordname-list
9207 objc-keywordname-list:
9208 objc-keywordname
9209 objc-keywordname-list objc-keywordname
9211 objc-keywordname:
9212 objc-selector :
9216 static tree
9217 c_parser_objc_selector_arg (c_parser *parser)
9219 tree sel = c_parser_objc_selector (parser);
9220 tree list = NULL_TREE;
9221 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9222 return sel;
9223 while (true)
9225 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9226 return list;
9227 list = chainon (list, build_tree_list (sel, NULL_TREE));
9228 sel = c_parser_objc_selector (parser);
9229 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9230 break;
9232 return list;
9235 /* Parse an objc-receiver.
9237 objc-receiver:
9238 expression
9239 class-name
9240 type-name
9243 static tree
9244 c_parser_objc_receiver (c_parser *parser)
9246 location_t loc = c_parser_peek_token (parser)->location;
9248 if (c_parser_peek_token (parser)->type == CPP_NAME
9249 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9250 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9252 tree id = c_parser_peek_token (parser)->value;
9253 c_parser_consume_token (parser);
9254 return objc_get_class_reference (id);
9256 struct c_expr ce = c_parser_expression (parser);
9257 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9258 return c_fully_fold (ce.value, false, NULL);
9261 /* Parse objc-message-args.
9263 objc-message-args:
9264 objc-selector
9265 objc-keywordarg-list
9267 objc-keywordarg-list:
9268 objc-keywordarg
9269 objc-keywordarg-list objc-keywordarg
9271 objc-keywordarg:
9272 objc-selector : objc-keywordexpr
9273 : objc-keywordexpr
9276 static tree
9277 c_parser_objc_message_args (c_parser *parser)
9279 tree sel = c_parser_objc_selector (parser);
9280 tree list = NULL_TREE;
9281 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9282 return sel;
9283 while (true)
9285 tree keywordexpr;
9286 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9287 return error_mark_node;
9288 keywordexpr = c_parser_objc_keywordexpr (parser);
9289 list = chainon (list, build_tree_list (sel, keywordexpr));
9290 sel = c_parser_objc_selector (parser);
9291 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9292 break;
9294 return list;
9297 /* Parse an objc-keywordexpr.
9299 objc-keywordexpr:
9300 nonempty-expr-list
9303 static tree
9304 c_parser_objc_keywordexpr (c_parser *parser)
9306 tree ret;
9307 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9308 NULL, NULL, NULL, NULL);
9309 if (vec_safe_length (expr_list) == 1)
9311 /* Just return the expression, remove a level of
9312 indirection. */
9313 ret = (*expr_list)[0];
9315 else
9317 /* We have a comma expression, we will collapse later. */
9318 ret = build_tree_list_vec (expr_list);
9320 release_tree_vector (expr_list);
9321 return ret;
9324 /* A check, needed in several places, that ObjC interface, implementation or
9325 method definitions are not prefixed by incorrect items. */
9326 static bool
9327 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9328 struct c_declspecs *specs)
9330 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9331 || specs->typespec_kind != ctsk_none)
9333 c_parser_error (parser,
9334 "no type or storage class may be specified here,");
9335 c_parser_skip_to_end_of_block_or_statement (parser);
9336 return true;
9338 return false;
9341 /* Parse an Objective-C @property declaration. The syntax is:
9343 objc-property-declaration:
9344 '@property' objc-property-attributes[opt] struct-declaration ;
9346 objc-property-attributes:
9347 '(' objc-property-attribute-list ')'
9349 objc-property-attribute-list:
9350 objc-property-attribute
9351 objc-property-attribute-list, objc-property-attribute
9353 objc-property-attribute
9354 'getter' = identifier
9355 'setter' = identifier
9356 'readonly'
9357 'readwrite'
9358 'assign'
9359 'retain'
9360 'copy'
9361 'nonatomic'
9363 For example:
9364 @property NSString *name;
9365 @property (readonly) id object;
9366 @property (retain, nonatomic, getter=getTheName) id name;
9367 @property int a, b, c;
9369 PS: This function is identical to cp_parser_objc_at_propery_declaration
9370 for C++. Keep them in sync. */
9371 static void
9372 c_parser_objc_at_property_declaration (c_parser *parser)
9374 /* The following variables hold the attributes of the properties as
9375 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9376 seen. When we see an attribute, we set them to 'true' (if they
9377 are boolean properties) or to the identifier (if they have an
9378 argument, ie, for getter and setter). Note that here we only
9379 parse the list of attributes, check the syntax and accumulate the
9380 attributes that we find. objc_add_property_declaration() will
9381 then process the information. */
9382 bool property_assign = false;
9383 bool property_copy = false;
9384 tree property_getter_ident = NULL_TREE;
9385 bool property_nonatomic = false;
9386 bool property_readonly = false;
9387 bool property_readwrite = false;
9388 bool property_retain = false;
9389 tree property_setter_ident = NULL_TREE;
9391 /* 'properties' is the list of properties that we read. Usually a
9392 single one, but maybe more (eg, in "@property int a, b, c;" there
9393 are three). */
9394 tree properties;
9395 location_t loc;
9397 loc = c_parser_peek_token (parser)->location;
9398 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9400 c_parser_consume_token (parser); /* Eat '@property'. */
9402 /* Parse the optional attribute list... */
9403 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9405 /* Eat the '(' */
9406 c_parser_consume_token (parser);
9408 /* Property attribute keywords are valid now. */
9409 parser->objc_property_attr_context = true;
9411 while (true)
9413 bool syntax_error = false;
9414 c_token *token = c_parser_peek_token (parser);
9415 enum rid keyword;
9417 if (token->type != CPP_KEYWORD)
9419 if (token->type == CPP_CLOSE_PAREN)
9420 c_parser_error (parser, "expected identifier");
9421 else
9423 c_parser_consume_token (parser);
9424 c_parser_error (parser, "unknown property attribute");
9426 break;
9428 keyword = token->keyword;
9429 c_parser_consume_token (parser);
9430 switch (keyword)
9432 case RID_ASSIGN: property_assign = true; break;
9433 case RID_COPY: property_copy = true; break;
9434 case RID_NONATOMIC: property_nonatomic = true; break;
9435 case RID_READONLY: property_readonly = true; break;
9436 case RID_READWRITE: property_readwrite = true; break;
9437 case RID_RETAIN: property_retain = true; break;
9439 case RID_GETTER:
9440 case RID_SETTER:
9441 if (c_parser_next_token_is_not (parser, CPP_EQ))
9443 if (keyword == RID_GETTER)
9444 c_parser_error (parser,
9445 "missing %<=%> (after %<getter%> attribute)");
9446 else
9447 c_parser_error (parser,
9448 "missing %<=%> (after %<setter%> attribute)");
9449 syntax_error = true;
9450 break;
9452 c_parser_consume_token (parser); /* eat the = */
9453 if (c_parser_next_token_is_not (parser, CPP_NAME))
9455 c_parser_error (parser, "expected identifier");
9456 syntax_error = true;
9457 break;
9459 if (keyword == RID_SETTER)
9461 if (property_setter_ident != NULL_TREE)
9462 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9463 else
9464 property_setter_ident = c_parser_peek_token (parser)->value;
9465 c_parser_consume_token (parser);
9466 if (c_parser_next_token_is_not (parser, CPP_COLON))
9467 c_parser_error (parser, "setter name must terminate with %<:%>");
9468 else
9469 c_parser_consume_token (parser);
9471 else
9473 if (property_getter_ident != NULL_TREE)
9474 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9475 else
9476 property_getter_ident = c_parser_peek_token (parser)->value;
9477 c_parser_consume_token (parser);
9479 break;
9480 default:
9481 c_parser_error (parser, "unknown property attribute");
9482 syntax_error = true;
9483 break;
9486 if (syntax_error)
9487 break;
9489 if (c_parser_next_token_is (parser, CPP_COMMA))
9490 c_parser_consume_token (parser);
9491 else
9492 break;
9494 parser->objc_property_attr_context = false;
9495 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9497 /* ... and the property declaration(s). */
9498 properties = c_parser_struct_declaration (parser);
9500 if (properties == error_mark_node)
9502 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9503 parser->error = false;
9504 return;
9507 if (properties == NULL_TREE)
9508 c_parser_error (parser, "expected identifier");
9509 else
9511 /* Comma-separated properties are chained together in
9512 reverse order; add them one by one. */
9513 properties = nreverse (properties);
9515 for (; properties; properties = TREE_CHAIN (properties))
9516 objc_add_property_declaration (loc, copy_node (properties),
9517 property_readonly, property_readwrite,
9518 property_assign, property_retain,
9519 property_copy, property_nonatomic,
9520 property_getter_ident, property_setter_ident);
9523 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9524 parser->error = false;
9527 /* Parse an Objective-C @synthesize declaration. The syntax is:
9529 objc-synthesize-declaration:
9530 @synthesize objc-synthesize-identifier-list ;
9532 objc-synthesize-identifier-list:
9533 objc-synthesize-identifier
9534 objc-synthesize-identifier-list, objc-synthesize-identifier
9536 objc-synthesize-identifier
9537 identifier
9538 identifier = identifier
9540 For example:
9541 @synthesize MyProperty;
9542 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9544 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9545 for C++. Keep them in sync.
9547 static void
9548 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9550 tree list = NULL_TREE;
9551 location_t loc;
9552 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9553 loc = c_parser_peek_token (parser)->location;
9555 c_parser_consume_token (parser);
9556 while (true)
9558 tree property, ivar;
9559 if (c_parser_next_token_is_not (parser, CPP_NAME))
9561 c_parser_error (parser, "expected identifier");
9562 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9563 /* Once we find the semicolon, we can resume normal parsing.
9564 We have to reset parser->error manually because
9565 c_parser_skip_until_found() won't reset it for us if the
9566 next token is precisely a semicolon. */
9567 parser->error = false;
9568 return;
9570 property = c_parser_peek_token (parser)->value;
9571 c_parser_consume_token (parser);
9572 if (c_parser_next_token_is (parser, CPP_EQ))
9574 c_parser_consume_token (parser);
9575 if (c_parser_next_token_is_not (parser, CPP_NAME))
9577 c_parser_error (parser, "expected identifier");
9578 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9579 parser->error = false;
9580 return;
9582 ivar = c_parser_peek_token (parser)->value;
9583 c_parser_consume_token (parser);
9585 else
9586 ivar = NULL_TREE;
9587 list = chainon (list, build_tree_list (ivar, property));
9588 if (c_parser_next_token_is (parser, CPP_COMMA))
9589 c_parser_consume_token (parser);
9590 else
9591 break;
9593 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9594 objc_add_synthesize_declaration (loc, list);
9597 /* Parse an Objective-C @dynamic declaration. The syntax is:
9599 objc-dynamic-declaration:
9600 @dynamic identifier-list ;
9602 For example:
9603 @dynamic MyProperty;
9604 @dynamic MyProperty, AnotherProperty;
9606 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9607 for C++. Keep them in sync.
9609 static void
9610 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9612 tree list = NULL_TREE;
9613 location_t loc;
9614 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9615 loc = c_parser_peek_token (parser)->location;
9617 c_parser_consume_token (parser);
9618 while (true)
9620 tree property;
9621 if (c_parser_next_token_is_not (parser, CPP_NAME))
9623 c_parser_error (parser, "expected identifier");
9624 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9625 parser->error = false;
9626 return;
9628 property = c_parser_peek_token (parser)->value;
9629 list = chainon (list, build_tree_list (NULL_TREE, property));
9630 c_parser_consume_token (parser);
9631 if (c_parser_next_token_is (parser, CPP_COMMA))
9632 c_parser_consume_token (parser);
9633 else
9634 break;
9636 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9637 objc_add_dynamic_declaration (loc, list);
9641 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9642 should be considered, statements. ALLOW_STMT is true if we're within
9643 the context of a function and such pragmas are to be allowed. Returns
9644 true if we actually parsed such a pragma. */
9646 static bool
9647 c_parser_pragma (c_parser *parser, enum pragma_context context)
9649 unsigned int id;
9651 id = c_parser_peek_token (parser)->pragma_kind;
9652 gcc_assert (id != PRAGMA_NONE);
9654 switch (id)
9656 case PRAGMA_OACC_ENTER_DATA:
9657 c_parser_oacc_enter_exit_data (parser, true);
9658 return false;
9660 case PRAGMA_OACC_EXIT_DATA:
9661 c_parser_oacc_enter_exit_data (parser, false);
9662 return false;
9664 case PRAGMA_OACC_UPDATE:
9665 if (context != pragma_compound)
9667 if (context == pragma_stmt)
9668 c_parser_error (parser, "%<#pragma acc update%> may only be "
9669 "used in compound statements");
9670 goto bad_stmt;
9672 c_parser_oacc_update (parser);
9673 return false;
9675 case PRAGMA_OMP_BARRIER:
9676 if (context != pragma_compound)
9678 if (context == pragma_stmt)
9679 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9680 "used in compound statements");
9681 goto bad_stmt;
9683 c_parser_omp_barrier (parser);
9684 return false;
9686 case PRAGMA_OMP_FLUSH:
9687 if (context != pragma_compound)
9689 if (context == pragma_stmt)
9690 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9691 "used in compound statements");
9692 goto bad_stmt;
9694 c_parser_omp_flush (parser);
9695 return false;
9697 case PRAGMA_OMP_TASKWAIT:
9698 if (context != pragma_compound)
9700 if (context == pragma_stmt)
9701 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9702 "used in compound statements");
9703 goto bad_stmt;
9705 c_parser_omp_taskwait (parser);
9706 return false;
9708 case PRAGMA_OMP_TASKYIELD:
9709 if (context != pragma_compound)
9711 if (context == pragma_stmt)
9712 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9713 "used in compound statements");
9714 goto bad_stmt;
9716 c_parser_omp_taskyield (parser);
9717 return false;
9719 case PRAGMA_OMP_CANCEL:
9720 if (context != pragma_compound)
9722 if (context == pragma_stmt)
9723 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9724 "used in compound statements");
9725 goto bad_stmt;
9727 c_parser_omp_cancel (parser);
9728 return false;
9730 case PRAGMA_OMP_CANCELLATION_POINT:
9731 if (context != pragma_compound)
9733 if (context == pragma_stmt)
9734 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9735 "only be used in compound statements");
9736 goto bad_stmt;
9738 c_parser_omp_cancellation_point (parser);
9739 return false;
9741 case PRAGMA_OMP_THREADPRIVATE:
9742 c_parser_omp_threadprivate (parser);
9743 return false;
9745 case PRAGMA_OMP_TARGET:
9746 return c_parser_omp_target (parser, context);
9748 case PRAGMA_OMP_END_DECLARE_TARGET:
9749 c_parser_omp_end_declare_target (parser);
9750 return false;
9752 case PRAGMA_OMP_SECTION:
9753 error_at (c_parser_peek_token (parser)->location,
9754 "%<#pragma omp section%> may only be used in "
9755 "%<#pragma omp sections%> construct");
9756 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9757 return false;
9759 case PRAGMA_OMP_DECLARE_REDUCTION:
9760 c_parser_omp_declare (parser, context);
9761 return false;
9762 case PRAGMA_IVDEP:
9763 c_parser_consume_pragma (parser);
9764 c_parser_skip_to_pragma_eol (parser);
9765 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9766 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9767 && !c_parser_next_token_is_keyword (parser, RID_DO))
9769 c_parser_error (parser, "for, while or do statement expected");
9770 return false;
9772 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9773 c_parser_for_statement (parser, true);
9774 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9775 c_parser_while_statement (parser, true);
9776 else
9777 c_parser_do_statement (parser, true);
9778 return false;
9780 case PRAGMA_GCC_PCH_PREPROCESS:
9781 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9782 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9783 return false;
9785 case PRAGMA_CILK_SIMD:
9786 if (!c_parser_cilk_verify_simd (parser, context))
9787 return false;
9788 c_parser_consume_pragma (parser);
9789 c_parser_cilk_simd (parser);
9790 return false;
9791 case PRAGMA_CILK_GRAINSIZE:
9792 if (!flag_cilkplus)
9794 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9795 " enabled");
9796 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9797 return false;
9799 if (context == pragma_external)
9801 error_at (c_parser_peek_token (parser)->location,
9802 "%<#pragma grainsize%> must be inside a function");
9803 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9804 return false;
9806 c_parser_cilk_grainsize (parser);
9807 return false;
9809 default:
9810 if (id < PRAGMA_FIRST_EXTERNAL)
9812 if (context != pragma_stmt && context != pragma_compound)
9814 bad_stmt:
9815 c_parser_error (parser, "expected declaration specifiers");
9816 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9817 return false;
9819 c_parser_omp_construct (parser);
9820 return true;
9822 break;
9825 c_parser_consume_pragma (parser);
9826 c_invoke_pragma_handler (id);
9828 /* Skip to EOL, but suppress any error message. Those will have been
9829 generated by the handler routine through calling error, as opposed
9830 to calling c_parser_error. */
9831 parser->error = true;
9832 c_parser_skip_to_pragma_eol (parser);
9834 return false;
9837 /* The interface the pragma parsers have to the lexer. */
9839 enum cpp_ttype
9840 pragma_lex (tree *value)
9842 c_token *tok = c_parser_peek_token (the_parser);
9843 enum cpp_ttype ret = tok->type;
9845 *value = tok->value;
9846 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9847 ret = CPP_EOF;
9848 else
9850 if (ret == CPP_KEYWORD)
9851 ret = CPP_NAME;
9852 c_parser_consume_token (the_parser);
9855 return ret;
9858 static void
9859 c_parser_pragma_pch_preprocess (c_parser *parser)
9861 tree name = NULL;
9863 c_parser_consume_pragma (parser);
9864 if (c_parser_next_token_is (parser, CPP_STRING))
9866 name = c_parser_peek_token (parser)->value;
9867 c_parser_consume_token (parser);
9869 else
9870 c_parser_error (parser, "expected string literal");
9871 c_parser_skip_to_pragma_eol (parser);
9873 if (name)
9874 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9877 /* OpenACC and OpenMP parsing routines. */
9879 /* Returns name of the next clause.
9880 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9881 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9882 returned and the token is consumed. */
9884 static pragma_omp_clause
9885 c_parser_omp_clause_name (c_parser *parser)
9887 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9889 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9890 result = PRAGMA_OACC_CLAUSE_AUTO;
9891 else if (c_parser_next_token_is_keyword (parser, RID_IF))
9892 result = PRAGMA_OMP_CLAUSE_IF;
9893 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9894 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9895 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9896 result = PRAGMA_OMP_CLAUSE_FOR;
9897 else if (c_parser_next_token_is (parser, CPP_NAME))
9899 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9901 switch (p[0])
9903 case 'a':
9904 if (!strcmp ("aligned", p))
9905 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9906 else if (!strcmp ("async", p))
9907 result = PRAGMA_OACC_CLAUSE_ASYNC;
9908 break;
9909 case 'c':
9910 if (!strcmp ("collapse", p))
9911 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9912 else if (!strcmp ("copy", p))
9913 result = PRAGMA_OACC_CLAUSE_COPY;
9914 else if (!strcmp ("copyin", p))
9915 result = PRAGMA_OMP_CLAUSE_COPYIN;
9916 else if (!strcmp ("copyout", p))
9917 result = PRAGMA_OACC_CLAUSE_COPYOUT;
9918 else if (!strcmp ("copyprivate", p))
9919 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9920 else if (!strcmp ("create", p))
9921 result = PRAGMA_OACC_CLAUSE_CREATE;
9922 break;
9923 case 'd':
9924 if (!strcmp ("delete", p))
9925 result = PRAGMA_OACC_CLAUSE_DELETE;
9926 else if (!strcmp ("depend", p))
9927 result = PRAGMA_OMP_CLAUSE_DEPEND;
9928 else if (!strcmp ("device", p))
9929 result = PRAGMA_OMP_CLAUSE_DEVICE;
9930 else if (!strcmp ("deviceptr", p))
9931 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
9932 else if (!strcmp ("dist_schedule", p))
9933 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9934 break;
9935 case 'f':
9936 if (!strcmp ("final", p))
9937 result = PRAGMA_OMP_CLAUSE_FINAL;
9938 else if (!strcmp ("firstprivate", p))
9939 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9940 else if (!strcmp ("from", p))
9941 result = PRAGMA_OMP_CLAUSE_FROM;
9942 break;
9943 case 'g':
9944 if (!strcmp ("gang", p))
9945 result = PRAGMA_OACC_CLAUSE_GANG;
9946 break;
9947 case 'h':
9948 if (!strcmp ("host", p))
9949 result = PRAGMA_OACC_CLAUSE_HOST;
9950 break;
9951 case 'i':
9952 if (!strcmp ("inbranch", p))
9953 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9954 break;
9955 case 'l':
9956 if (!strcmp ("lastprivate", p))
9957 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9958 else if (!strcmp ("linear", p))
9959 result = PRAGMA_OMP_CLAUSE_LINEAR;
9960 break;
9961 case 'm':
9962 if (!strcmp ("map", p))
9963 result = PRAGMA_OMP_CLAUSE_MAP;
9964 else if (!strcmp ("mergeable", p))
9965 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9966 else if (flag_cilkplus && !strcmp ("mask", p))
9967 result = PRAGMA_CILK_CLAUSE_MASK;
9968 break;
9969 case 'n':
9970 if (!strcmp ("notinbranch", p))
9971 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9972 else if (!strcmp ("nowait", p))
9973 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9974 else if (!strcmp ("num_gangs", p))
9975 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
9976 else if (!strcmp ("num_teams", p))
9977 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9978 else if (!strcmp ("num_threads", p))
9979 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9980 else if (!strcmp ("num_workers", p))
9981 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
9982 else if (flag_cilkplus && !strcmp ("nomask", p))
9983 result = PRAGMA_CILK_CLAUSE_NOMASK;
9984 break;
9985 case 'o':
9986 if (!strcmp ("ordered", p))
9987 result = PRAGMA_OMP_CLAUSE_ORDERED;
9988 break;
9989 case 'p':
9990 if (!strcmp ("parallel", p))
9991 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9992 else if (!strcmp ("present", p))
9993 result = PRAGMA_OACC_CLAUSE_PRESENT;
9994 else if (!strcmp ("present_or_copy", p)
9995 || !strcmp ("pcopy", p))
9996 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
9997 else if (!strcmp ("present_or_copyin", p)
9998 || !strcmp ("pcopyin", p))
9999 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10000 else if (!strcmp ("present_or_copyout", p)
10001 || !strcmp ("pcopyout", p))
10002 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10003 else if (!strcmp ("present_or_create", p)
10004 || !strcmp ("pcreate", p))
10005 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10006 else if (!strcmp ("private", p))
10007 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10008 else if (!strcmp ("proc_bind", p))
10009 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10010 break;
10011 case 'r':
10012 if (!strcmp ("reduction", p))
10013 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10014 break;
10015 case 's':
10016 if (!strcmp ("safelen", p))
10017 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10018 else if (!strcmp ("schedule", p))
10019 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10020 else if (!strcmp ("sections", p))
10021 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10022 else if (!strcmp ("seq", p))
10023 result = PRAGMA_OACC_CLAUSE_SEQ;
10024 else if (!strcmp ("shared", p))
10025 result = PRAGMA_OMP_CLAUSE_SHARED;
10026 else if (!strcmp ("simdlen", p))
10027 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10028 else if (!strcmp ("self", p))
10029 result = PRAGMA_OACC_CLAUSE_SELF;
10030 break;
10031 case 't':
10032 if (!strcmp ("taskgroup", p))
10033 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10034 else if (!strcmp ("thread_limit", p))
10035 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10036 else if (!strcmp ("to", p))
10037 result = PRAGMA_OMP_CLAUSE_TO;
10038 break;
10039 case 'u':
10040 if (!strcmp ("uniform", p))
10041 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10042 else if (!strcmp ("untied", p))
10043 result = PRAGMA_OMP_CLAUSE_UNTIED;
10044 break;
10045 case 'v':
10046 if (!strcmp ("vector", p))
10047 result = PRAGMA_OACC_CLAUSE_VECTOR;
10048 else if (!strcmp ("vector_length", p))
10049 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10050 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10051 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10052 break;
10053 case 'w':
10054 if (!strcmp ("wait", p))
10055 result = PRAGMA_OACC_CLAUSE_WAIT;
10056 else if (!strcmp ("worker", p))
10057 result = PRAGMA_OACC_CLAUSE_WORKER;
10058 break;
10062 if (result != PRAGMA_OMP_CLAUSE_NONE)
10063 c_parser_consume_token (parser);
10065 return result;
10068 /* Validate that a clause of the given type does not already exist. */
10070 static void
10071 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10072 const char *name)
10074 tree c;
10076 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10077 if (OMP_CLAUSE_CODE (c) == code)
10079 location_t loc = OMP_CLAUSE_LOCATION (c);
10080 error_at (loc, "too many %qs clauses", name);
10081 break;
10085 /* OpenACC 2.0
10086 Parse wait clause or wait directive parameters. */
10088 static tree
10089 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10091 vec<tree, va_gc> *args;
10092 tree t, args_tree;
10094 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10095 return list;
10097 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10099 if (args->length () == 0)
10101 c_parser_error (parser, "expected integer expression before ')'");
10102 release_tree_vector (args);
10103 return list;
10106 args_tree = build_tree_list_vec (args);
10108 for (t = args_tree; t; t = TREE_CHAIN (t))
10110 tree targ = TREE_VALUE (t);
10112 if (targ != error_mark_node)
10114 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10116 c_parser_error (parser, "expression must be integral");
10117 targ = error_mark_node;
10119 else
10121 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10123 OMP_CLAUSE_DECL (c) = targ;
10124 OMP_CLAUSE_CHAIN (c) = list;
10125 list = c;
10130 release_tree_vector (args);
10131 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10132 return list;
10135 /* OpenACC 2.0, OpenMP 2.5:
10136 variable-list:
10137 identifier
10138 variable-list , identifier
10140 If KIND is nonzero, create the appropriate node and install the
10141 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10142 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10144 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10145 return the list created. */
10147 static tree
10148 c_parser_omp_variable_list (c_parser *parser,
10149 location_t clause_loc,
10150 enum omp_clause_code kind, tree list)
10152 if (c_parser_next_token_is_not (parser, CPP_NAME)
10153 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10154 c_parser_error (parser, "expected identifier");
10156 while (c_parser_next_token_is (parser, CPP_NAME)
10157 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10159 tree t = lookup_name (c_parser_peek_token (parser)->value);
10161 if (t == NULL_TREE)
10163 undeclared_variable (c_parser_peek_token (parser)->location,
10164 c_parser_peek_token (parser)->value);
10165 t = error_mark_node;
10168 c_parser_consume_token (parser);
10170 if (t == error_mark_node)
10172 else if (kind != 0)
10174 switch (kind)
10176 case OMP_CLAUSE__CACHE_:
10177 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10179 c_parser_error (parser, "expected %<[%>");
10180 t = error_mark_node;
10181 break;
10183 /* FALL THROUGH. */
10184 case OMP_CLAUSE_MAP:
10185 case OMP_CLAUSE_FROM:
10186 case OMP_CLAUSE_TO:
10187 case OMP_CLAUSE_DEPEND:
10188 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10190 tree low_bound = NULL_TREE, length = NULL_TREE;
10192 c_parser_consume_token (parser);
10193 if (!c_parser_next_token_is (parser, CPP_COLON))
10195 low_bound = c_parser_expression (parser).value;
10196 mark_exp_read (low_bound);
10198 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10199 length = integer_one_node;
10200 else
10202 /* Look for `:'. */
10203 if (!c_parser_require (parser, CPP_COLON,
10204 "expected %<:%>"))
10206 t = error_mark_node;
10207 break;
10209 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10211 length = c_parser_expression (parser).value;
10212 mark_exp_read (length);
10215 /* Look for the closing `]'. */
10216 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10217 "expected %<]%>"))
10219 t = error_mark_node;
10220 break;
10223 if (kind == OMP_CLAUSE__CACHE_)
10225 if (TREE_CODE (low_bound) != INTEGER_CST
10226 && !TREE_READONLY (low_bound))
10228 error_at (clause_loc,
10229 "%qD is not a constant", low_bound);
10230 t = error_mark_node;
10233 if (TREE_CODE (length) != INTEGER_CST
10234 && !TREE_READONLY (length))
10236 error_at (clause_loc,
10237 "%qD is not a constant", length);
10238 t = error_mark_node;
10242 t = tree_cons (low_bound, length, t);
10244 break;
10245 default:
10246 break;
10249 if (t != error_mark_node)
10251 tree u = build_omp_clause (clause_loc, kind);
10252 OMP_CLAUSE_DECL (u) = t;
10253 OMP_CLAUSE_CHAIN (u) = list;
10254 list = u;
10257 else
10258 list = tree_cons (t, NULL_TREE, list);
10260 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10261 break;
10263 c_parser_consume_token (parser);
10266 return list;
10269 /* Similarly, but expect leading and trailing parenthesis. This is a very
10270 common case for OpenACC and OpenMP clauses. */
10272 static tree
10273 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10274 tree list)
10276 /* The clauses location. */
10277 location_t loc = c_parser_peek_token (parser)->location;
10279 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10281 list = c_parser_omp_variable_list (parser, loc, kind, list);
10282 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10284 return list;
10287 /* OpenACC 2.0:
10288 copy ( variable-list )
10289 copyin ( variable-list )
10290 copyout ( variable-list )
10291 create ( variable-list )
10292 delete ( variable-list )
10293 present ( variable-list )
10294 present_or_copy ( variable-list )
10295 pcopy ( variable-list )
10296 present_or_copyin ( variable-list )
10297 pcopyin ( variable-list )
10298 present_or_copyout ( variable-list )
10299 pcopyout ( variable-list )
10300 present_or_create ( variable-list )
10301 pcreate ( variable-list ) */
10303 static tree
10304 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10305 tree list)
10307 enum gomp_map_kind kind;
10308 switch (c_kind)
10310 case PRAGMA_OACC_CLAUSE_COPY:
10311 kind = GOMP_MAP_FORCE_TOFROM;
10312 break;
10313 case PRAGMA_OACC_CLAUSE_COPYIN:
10314 kind = GOMP_MAP_FORCE_TO;
10315 break;
10316 case PRAGMA_OACC_CLAUSE_COPYOUT:
10317 kind = GOMP_MAP_FORCE_FROM;
10318 break;
10319 case PRAGMA_OACC_CLAUSE_CREATE:
10320 kind = GOMP_MAP_FORCE_ALLOC;
10321 break;
10322 case PRAGMA_OACC_CLAUSE_DELETE:
10323 kind = GOMP_MAP_FORCE_DEALLOC;
10324 break;
10325 case PRAGMA_OACC_CLAUSE_DEVICE:
10326 kind = GOMP_MAP_FORCE_TO;
10327 break;
10328 case PRAGMA_OACC_CLAUSE_HOST:
10329 case PRAGMA_OACC_CLAUSE_SELF:
10330 kind = GOMP_MAP_FORCE_FROM;
10331 break;
10332 case PRAGMA_OACC_CLAUSE_PRESENT:
10333 kind = GOMP_MAP_FORCE_PRESENT;
10334 break;
10335 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10336 kind = GOMP_MAP_TOFROM;
10337 break;
10338 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10339 kind = GOMP_MAP_TO;
10340 break;
10341 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10342 kind = GOMP_MAP_FROM;
10343 break;
10344 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10345 kind = GOMP_MAP_ALLOC;
10346 break;
10347 default:
10348 gcc_unreachable ();
10350 tree nl, c;
10351 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10353 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10354 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10356 return nl;
10359 /* OpenACC 2.0:
10360 deviceptr ( variable-list ) */
10362 static tree
10363 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10365 location_t loc = c_parser_peek_token (parser)->location;
10366 tree vars, t;
10368 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10369 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10370 variable-list must only allow for pointer variables. */
10371 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10372 for (t = vars; t && t; t = TREE_CHAIN (t))
10374 tree v = TREE_PURPOSE (t);
10376 /* FIXME diagnostics: Ideally we should keep individual
10377 locations for all the variables in the var list to make the
10378 following errors more precise. Perhaps
10379 c_parser_omp_var_list_parens() should construct a list of
10380 locations to go along with the var list. */
10382 if (TREE_CODE (v) != VAR_DECL)
10383 error_at (loc, "%qD is not a variable", v);
10384 else if (TREE_TYPE (v) == error_mark_node)
10386 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10387 error_at (loc, "%qD is not a pointer variable", v);
10389 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10390 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10391 OMP_CLAUSE_DECL (u) = v;
10392 OMP_CLAUSE_CHAIN (u) = list;
10393 list = u;
10396 return list;
10399 /* OpenACC 2.0, OpenMP 3.0:
10400 collapse ( constant-expression ) */
10402 static tree
10403 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10405 tree c, num = error_mark_node;
10406 HOST_WIDE_INT n;
10407 location_t loc;
10409 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10411 loc = c_parser_peek_token (parser)->location;
10412 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10414 num = c_parser_expr_no_commas (parser, NULL).value;
10415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10417 if (num == error_mark_node)
10418 return list;
10419 mark_exp_read (num);
10420 num = c_fully_fold (num, false, NULL);
10421 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10422 || !tree_fits_shwi_p (num)
10423 || (n = tree_to_shwi (num)) <= 0
10424 || (int) n != n)
10426 error_at (loc,
10427 "collapse argument needs positive constant integer expression");
10428 return list;
10430 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10431 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10432 OMP_CLAUSE_CHAIN (c) = list;
10433 return c;
10436 /* OpenMP 2.5:
10437 copyin ( variable-list ) */
10439 static tree
10440 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10442 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10445 /* OpenMP 2.5:
10446 copyprivate ( variable-list ) */
10448 static tree
10449 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10451 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10454 /* OpenMP 2.5:
10455 default ( shared | none ) */
10457 static tree
10458 c_parser_omp_clause_default (c_parser *parser, tree list)
10460 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10461 location_t loc = c_parser_peek_token (parser)->location;
10462 tree c;
10464 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10465 return list;
10466 if (c_parser_next_token_is (parser, CPP_NAME))
10468 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10470 switch (p[0])
10472 case 'n':
10473 if (strcmp ("none", p) != 0)
10474 goto invalid_kind;
10475 kind = OMP_CLAUSE_DEFAULT_NONE;
10476 break;
10478 case 's':
10479 if (strcmp ("shared", p) != 0)
10480 goto invalid_kind;
10481 kind = OMP_CLAUSE_DEFAULT_SHARED;
10482 break;
10484 default:
10485 goto invalid_kind;
10488 c_parser_consume_token (parser);
10490 else
10492 invalid_kind:
10493 c_parser_error (parser, "expected %<none%> or %<shared%>");
10495 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10497 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10498 return list;
10500 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10501 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10502 OMP_CLAUSE_CHAIN (c) = list;
10503 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10505 return c;
10508 /* OpenMP 2.5:
10509 firstprivate ( variable-list ) */
10511 static tree
10512 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10514 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10517 /* OpenMP 3.1:
10518 final ( expression ) */
10520 static tree
10521 c_parser_omp_clause_final (c_parser *parser, tree list)
10523 location_t loc = c_parser_peek_token (parser)->location;
10524 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10526 tree t = c_parser_paren_condition (parser);
10527 tree c;
10529 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10531 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10532 OMP_CLAUSE_FINAL_EXPR (c) = t;
10533 OMP_CLAUSE_CHAIN (c) = list;
10534 list = c;
10536 else
10537 c_parser_error (parser, "expected %<(%>");
10539 return list;
10542 /* OpenACC, OpenMP 2.5:
10543 if ( expression ) */
10545 static tree
10546 c_parser_omp_clause_if (c_parser *parser, tree list)
10548 location_t loc = c_parser_peek_token (parser)->location;
10549 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10551 tree t = c_parser_paren_condition (parser);
10552 tree c;
10554 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10556 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10557 OMP_CLAUSE_IF_EXPR (c) = t;
10558 OMP_CLAUSE_CHAIN (c) = list;
10559 list = c;
10561 else
10562 c_parser_error (parser, "expected %<(%>");
10564 return list;
10567 /* OpenMP 2.5:
10568 lastprivate ( variable-list ) */
10570 static tree
10571 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10573 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10576 /* OpenMP 3.1:
10577 mergeable */
10579 static tree
10580 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10582 tree c;
10584 /* FIXME: Should we allow duplicates? */
10585 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10587 c = build_omp_clause (c_parser_peek_token (parser)->location,
10588 OMP_CLAUSE_MERGEABLE);
10589 OMP_CLAUSE_CHAIN (c) = list;
10591 return c;
10594 /* OpenMP 2.5:
10595 nowait */
10597 static tree
10598 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10600 tree c;
10601 location_t loc = c_parser_peek_token (parser)->location;
10603 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10605 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10606 OMP_CLAUSE_CHAIN (c) = list;
10607 return c;
10610 /* OpenACC:
10611 num_gangs ( expression ) */
10613 static tree
10614 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10616 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10617 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10619 location_t expr_loc = c_parser_peek_token (parser)->location;
10620 tree c, t = c_parser_expression (parser).value;
10621 mark_exp_read (t);
10622 t = c_fully_fold (t, false, NULL);
10624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10626 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10628 c_parser_error (parser, "expected integer expression");
10629 return list;
10632 /* Attempt to statically determine when the number isn't positive. */
10633 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10634 build_int_cst (TREE_TYPE (t), 0));
10635 if (CAN_HAVE_LOCATION_P (c))
10636 SET_EXPR_LOCATION (c, expr_loc);
10637 if (c == boolean_true_node)
10639 warning_at (expr_loc, 0,
10640 "%<num_gangs%> value must be positive");
10641 t = integer_one_node;
10644 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10646 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10647 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10648 OMP_CLAUSE_CHAIN (c) = list;
10649 list = c;
10652 return list;
10655 /* OpenMP 2.5:
10656 num_threads ( expression ) */
10658 static tree
10659 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10661 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10662 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10664 location_t expr_loc = c_parser_peek_token (parser)->location;
10665 tree c, t = c_parser_expression (parser).value;
10666 mark_exp_read (t);
10667 t = c_fully_fold (t, false, NULL);
10669 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10671 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10673 c_parser_error (parser, "expected integer expression");
10674 return list;
10677 /* Attempt to statically determine when the number isn't positive. */
10678 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10679 build_int_cst (TREE_TYPE (t), 0));
10680 if (CAN_HAVE_LOCATION_P (c))
10681 SET_EXPR_LOCATION (c, expr_loc);
10682 if (c == boolean_true_node)
10684 warning_at (expr_loc, 0,
10685 "%<num_threads%> value must be positive");
10686 t = integer_one_node;
10689 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10691 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10692 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10693 OMP_CLAUSE_CHAIN (c) = list;
10694 list = c;
10697 return list;
10700 /* OpenACC:
10701 num_workers ( expression ) */
10703 static tree
10704 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
10706 location_t num_workers_loc = c_parser_peek_token (parser)->location;
10707 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10709 location_t expr_loc = c_parser_peek_token (parser)->location;
10710 tree c, t = c_parser_expression (parser).value;
10711 mark_exp_read (t);
10712 t = c_fully_fold (t, false, NULL);
10714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10716 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10718 c_parser_error (parser, "expected integer expression");
10719 return list;
10722 /* Attempt to statically determine when the number isn't positive. */
10723 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10724 build_int_cst (TREE_TYPE (t), 0));
10725 if (CAN_HAVE_LOCATION_P (c))
10726 SET_EXPR_LOCATION (c, expr_loc);
10727 if (c == boolean_true_node)
10729 warning_at (expr_loc, 0,
10730 "%<num_workers%> value must be positive");
10731 t = integer_one_node;
10734 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
10736 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
10737 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
10738 OMP_CLAUSE_CHAIN (c) = list;
10739 list = c;
10742 return list;
10745 /* OpenACC:
10746 async [( int-expr )] */
10748 static tree
10749 c_parser_oacc_clause_async (c_parser *parser, tree list)
10751 tree c, t;
10752 location_t loc = c_parser_peek_token (parser)->location;
10754 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
10756 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10758 c_parser_consume_token (parser);
10760 t = c_parser_expression (parser).value;
10761 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10762 c_parser_error (parser, "expected integer expression");
10763 else if (t == error_mark_node
10764 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10765 return list;
10767 else
10768 t = c_fully_fold (t, false, NULL);
10770 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
10772 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
10773 OMP_CLAUSE_ASYNC_EXPR (c) = t;
10774 OMP_CLAUSE_CHAIN (c) = list;
10775 list = c;
10777 return list;
10780 /* OpenACC:
10781 wait ( int-expr-list ) */
10783 static tree
10784 c_parser_oacc_clause_wait (c_parser *parser, tree list)
10786 location_t clause_loc = c_parser_peek_token (parser)->location;
10788 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10789 list = c_parser_oacc_wait_list (parser, clause_loc, list);
10791 return list;
10794 /* OpenMP 2.5:
10795 ordered */
10797 static tree
10798 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10800 tree c;
10802 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10804 c = build_omp_clause (c_parser_peek_token (parser)->location,
10805 OMP_CLAUSE_ORDERED);
10806 OMP_CLAUSE_CHAIN (c) = list;
10808 return c;
10811 /* OpenMP 2.5:
10812 private ( variable-list ) */
10814 static tree
10815 c_parser_omp_clause_private (c_parser *parser, tree list)
10817 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10820 /* OpenMP 2.5:
10821 reduction ( reduction-operator : variable-list )
10823 reduction-operator:
10824 One of: + * - & ^ | && ||
10826 OpenMP 3.1:
10828 reduction-operator:
10829 One of: + * - & ^ | && || max min
10831 OpenMP 4.0:
10833 reduction-operator:
10834 One of: + * - & ^ | && ||
10835 identifier */
10837 static tree
10838 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10840 location_t clause_loc = c_parser_peek_token (parser)->location;
10841 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10843 enum tree_code code = ERROR_MARK;
10844 tree reduc_id = NULL_TREE;
10846 switch (c_parser_peek_token (parser)->type)
10848 case CPP_PLUS:
10849 code = PLUS_EXPR;
10850 break;
10851 case CPP_MULT:
10852 code = MULT_EXPR;
10853 break;
10854 case CPP_MINUS:
10855 code = MINUS_EXPR;
10856 break;
10857 case CPP_AND:
10858 code = BIT_AND_EXPR;
10859 break;
10860 case CPP_XOR:
10861 code = BIT_XOR_EXPR;
10862 break;
10863 case CPP_OR:
10864 code = BIT_IOR_EXPR;
10865 break;
10866 case CPP_AND_AND:
10867 code = TRUTH_ANDIF_EXPR;
10868 break;
10869 case CPP_OR_OR:
10870 code = TRUTH_ORIF_EXPR;
10871 break;
10872 case CPP_NAME:
10874 const char *p
10875 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10876 if (strcmp (p, "min") == 0)
10878 code = MIN_EXPR;
10879 break;
10881 if (strcmp (p, "max") == 0)
10883 code = MAX_EXPR;
10884 break;
10886 reduc_id = c_parser_peek_token (parser)->value;
10887 break;
10889 default:
10890 c_parser_error (parser,
10891 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10892 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10893 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10894 return list;
10896 c_parser_consume_token (parser);
10897 reduc_id = c_omp_reduction_id (code, reduc_id);
10898 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10900 tree nl, c;
10902 nl = c_parser_omp_variable_list (parser, clause_loc,
10903 OMP_CLAUSE_REDUCTION, list);
10904 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10906 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10907 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10908 if (code == ERROR_MARK
10909 || !(INTEGRAL_TYPE_P (type)
10910 || TREE_CODE (type) == REAL_TYPE
10911 || TREE_CODE (type) == COMPLEX_TYPE))
10912 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10913 = c_omp_reduction_lookup (reduc_id,
10914 TYPE_MAIN_VARIANT (type));
10917 list = nl;
10919 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10921 return list;
10924 /* OpenMP 2.5:
10925 schedule ( schedule-kind )
10926 schedule ( schedule-kind , expression )
10928 schedule-kind:
10929 static | dynamic | guided | runtime | auto
10932 static tree
10933 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10935 tree c, t;
10936 location_t loc = c_parser_peek_token (parser)->location;
10938 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10939 return list;
10941 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10943 if (c_parser_next_token_is (parser, CPP_NAME))
10945 tree kind = c_parser_peek_token (parser)->value;
10946 const char *p = IDENTIFIER_POINTER (kind);
10948 switch (p[0])
10950 case 'd':
10951 if (strcmp ("dynamic", p) != 0)
10952 goto invalid_kind;
10953 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10954 break;
10956 case 'g':
10957 if (strcmp ("guided", p) != 0)
10958 goto invalid_kind;
10959 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10960 break;
10962 case 'r':
10963 if (strcmp ("runtime", p) != 0)
10964 goto invalid_kind;
10965 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10966 break;
10968 default:
10969 goto invalid_kind;
10972 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10973 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10974 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10975 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10976 else
10977 goto invalid_kind;
10979 c_parser_consume_token (parser);
10980 if (c_parser_next_token_is (parser, CPP_COMMA))
10982 location_t here;
10983 c_parser_consume_token (parser);
10985 here = c_parser_peek_token (parser)->location;
10986 t = c_parser_expr_no_commas (parser, NULL).value;
10987 mark_exp_read (t);
10988 t = c_fully_fold (t, false, NULL);
10990 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10991 error_at (here, "schedule %<runtime%> does not take "
10992 "a %<chunk_size%> parameter");
10993 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10994 error_at (here,
10995 "schedule %<auto%> does not take "
10996 "a %<chunk_size%> parameter");
10997 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10998 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10999 else
11000 c_parser_error (parser, "expected integer expression");
11002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11004 else
11005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11006 "expected %<,%> or %<)%>");
11008 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11009 OMP_CLAUSE_CHAIN (c) = list;
11010 return c;
11012 invalid_kind:
11013 c_parser_error (parser, "invalid schedule kind");
11014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11015 return list;
11018 /* OpenMP 2.5:
11019 shared ( variable-list ) */
11021 static tree
11022 c_parser_omp_clause_shared (c_parser *parser, tree list)
11024 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
11027 /* OpenMP 3.0:
11028 untied */
11030 static tree
11031 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11033 tree c;
11035 /* FIXME: Should we allow duplicates? */
11036 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
11038 c = build_omp_clause (c_parser_peek_token (parser)->location,
11039 OMP_CLAUSE_UNTIED);
11040 OMP_CLAUSE_CHAIN (c) = list;
11042 return c;
11045 /* OpenACC:
11046 vector_length ( expression ) */
11048 static tree
11049 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
11051 location_t vector_length_loc = c_parser_peek_token (parser)->location;
11052 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11054 location_t expr_loc = c_parser_peek_token (parser)->location;
11055 tree c, t = c_parser_expression (parser).value;
11056 mark_exp_read (t);
11057 t = c_fully_fold (t, false, NULL);
11059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11061 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11063 c_parser_error (parser, "expected integer expression");
11064 return list;
11067 /* Attempt to statically determine when the number isn't positive. */
11068 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11069 build_int_cst (TREE_TYPE (t), 0));
11070 if (CAN_HAVE_LOCATION_P (c))
11071 SET_EXPR_LOCATION (c, expr_loc);
11072 if (c == boolean_true_node)
11074 warning_at (expr_loc, 0,
11075 "%<vector_length%> value must be positive");
11076 t = integer_one_node;
11079 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
11081 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
11082 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
11083 OMP_CLAUSE_CHAIN (c) = list;
11084 list = c;
11087 return list;
11090 /* OpenMP 4.0:
11091 inbranch
11092 notinbranch */
11094 static tree
11095 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
11096 enum omp_clause_code code, tree list)
11098 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11100 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11101 OMP_CLAUSE_CHAIN (c) = list;
11103 return c;
11106 /* OpenMP 4.0:
11107 parallel
11109 sections
11110 taskgroup */
11112 static tree
11113 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
11114 enum omp_clause_code code, tree list)
11116 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11117 OMP_CLAUSE_CHAIN (c) = list;
11119 return c;
11122 /* OpenMP 4.0:
11123 num_teams ( expression ) */
11125 static tree
11126 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
11128 location_t num_teams_loc = c_parser_peek_token (parser)->location;
11129 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11131 location_t expr_loc = c_parser_peek_token (parser)->location;
11132 tree c, t = c_parser_expression (parser).value;
11133 mark_exp_read (t);
11134 t = c_fully_fold (t, false, NULL);
11136 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11138 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11140 c_parser_error (parser, "expected integer expression");
11141 return list;
11144 /* Attempt to statically determine when the number isn't positive. */
11145 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11146 build_int_cst (TREE_TYPE (t), 0));
11147 if (CAN_HAVE_LOCATION_P (c))
11148 SET_EXPR_LOCATION (c, expr_loc);
11149 if (c == boolean_true_node)
11151 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
11152 t = integer_one_node;
11155 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
11157 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11158 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
11159 OMP_CLAUSE_CHAIN (c) = list;
11160 list = c;
11163 return list;
11166 /* OpenMP 4.0:
11167 thread_limit ( expression ) */
11169 static tree
11170 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
11172 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
11173 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11175 location_t expr_loc = c_parser_peek_token (parser)->location;
11176 tree c, t = c_parser_expression (parser).value;
11177 mark_exp_read (t);
11178 t = c_fully_fold (t, false, NULL);
11180 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11182 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11184 c_parser_error (parser, "expected integer expression");
11185 return list;
11188 /* Attempt to statically determine when the number isn't positive. */
11189 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11190 build_int_cst (TREE_TYPE (t), 0));
11191 if (CAN_HAVE_LOCATION_P (c))
11192 SET_EXPR_LOCATION (c, expr_loc);
11193 if (c == boolean_true_node)
11195 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11196 t = integer_one_node;
11199 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11200 "thread_limit");
11202 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11203 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11204 OMP_CLAUSE_CHAIN (c) = list;
11205 list = c;
11208 return list;
11211 /* OpenMP 4.0:
11212 aligned ( variable-list )
11213 aligned ( variable-list : constant-expression ) */
11215 static tree
11216 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11218 location_t clause_loc = c_parser_peek_token (parser)->location;
11219 tree nl, c;
11221 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11222 return list;
11224 nl = c_parser_omp_variable_list (parser, clause_loc,
11225 OMP_CLAUSE_ALIGNED, list);
11227 if (c_parser_next_token_is (parser, CPP_COLON))
11229 c_parser_consume_token (parser);
11230 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11231 mark_exp_read (alignment);
11232 alignment = c_fully_fold (alignment, false, NULL);
11233 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11234 && TREE_CODE (alignment) != INTEGER_CST
11235 && tree_int_cst_sgn (alignment) != 1)
11237 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11238 "be positive constant integer expression");
11239 alignment = NULL_TREE;
11242 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11243 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11246 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11247 return nl;
11250 /* OpenMP 4.0:
11251 linear ( variable-list )
11252 linear ( variable-list : expression ) */
11254 static tree
11255 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11257 location_t clause_loc = c_parser_peek_token (parser)->location;
11258 tree nl, c, step;
11260 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11261 return list;
11263 nl = c_parser_omp_variable_list (parser, clause_loc,
11264 OMP_CLAUSE_LINEAR, list);
11266 if (c_parser_next_token_is (parser, CPP_COLON))
11268 c_parser_consume_token (parser);
11269 step = c_parser_expression (parser).value;
11270 mark_exp_read (step);
11271 step = c_fully_fold (step, false, NULL);
11272 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11274 sorry ("using parameters for %<linear%> step is not supported yet");
11275 step = integer_one_node;
11277 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11279 error_at (clause_loc, "%<linear%> clause step expression must "
11280 "be integral");
11281 step = integer_one_node;
11285 else
11286 step = integer_one_node;
11288 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11290 OMP_CLAUSE_LINEAR_STEP (c) = step;
11293 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11294 return nl;
11297 /* OpenMP 4.0:
11298 safelen ( constant-expression ) */
11300 static tree
11301 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11303 location_t clause_loc = c_parser_peek_token (parser)->location;
11304 tree c, t;
11306 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11307 return list;
11309 t = c_parser_expr_no_commas (parser, NULL).value;
11310 mark_exp_read (t);
11311 t = c_fully_fold (t, false, NULL);
11312 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11313 && TREE_CODE (t) != INTEGER_CST
11314 && tree_int_cst_sgn (t) != 1)
11316 error_at (clause_loc, "%<safelen%> clause expression must "
11317 "be positive constant integer expression");
11318 t = NULL_TREE;
11321 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11322 if (t == NULL_TREE || t == error_mark_node)
11323 return list;
11325 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11327 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11328 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11329 OMP_CLAUSE_CHAIN (c) = list;
11330 return c;
11333 /* OpenMP 4.0:
11334 simdlen ( constant-expression ) */
11336 static tree
11337 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11339 location_t clause_loc = c_parser_peek_token (parser)->location;
11340 tree c, t;
11342 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11343 return list;
11345 t = c_parser_expr_no_commas (parser, NULL).value;
11346 mark_exp_read (t);
11347 t = c_fully_fold (t, false, NULL);
11348 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11349 && TREE_CODE (t) != INTEGER_CST
11350 && tree_int_cst_sgn (t) != 1)
11352 error_at (clause_loc, "%<simdlen%> clause expression must "
11353 "be positive constant integer expression");
11354 t = NULL_TREE;
11357 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11358 if (t == NULL_TREE || t == error_mark_node)
11359 return list;
11361 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11363 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11364 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11365 OMP_CLAUSE_CHAIN (c) = list;
11366 return c;
11369 /* OpenMP 4.0:
11370 depend ( depend-kind: variable-list )
11372 depend-kind:
11373 in | out | inout */
11375 static tree
11376 c_parser_omp_clause_depend (c_parser *parser, tree list)
11378 location_t clause_loc = c_parser_peek_token (parser)->location;
11379 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11380 tree nl, c;
11382 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11383 return list;
11385 if (c_parser_next_token_is (parser, CPP_NAME))
11387 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11388 if (strcmp ("in", p) == 0)
11389 kind = OMP_CLAUSE_DEPEND_IN;
11390 else if (strcmp ("inout", p) == 0)
11391 kind = OMP_CLAUSE_DEPEND_INOUT;
11392 else if (strcmp ("out", p) == 0)
11393 kind = OMP_CLAUSE_DEPEND_OUT;
11394 else
11395 goto invalid_kind;
11397 else
11398 goto invalid_kind;
11400 c_parser_consume_token (parser);
11401 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11402 goto resync_fail;
11404 nl = c_parser_omp_variable_list (parser, clause_loc,
11405 OMP_CLAUSE_DEPEND, list);
11407 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11408 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11410 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11411 return nl;
11413 invalid_kind:
11414 c_parser_error (parser, "invalid depend kind");
11415 resync_fail:
11416 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11417 return list;
11420 /* OpenMP 4.0:
11421 map ( map-kind: variable-list )
11422 map ( variable-list )
11424 map-kind:
11425 alloc | to | from | tofrom */
11427 static tree
11428 c_parser_omp_clause_map (c_parser *parser, tree list)
11430 location_t clause_loc = c_parser_peek_token (parser)->location;
11431 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
11432 tree nl, c;
11434 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11435 return list;
11437 if (c_parser_next_token_is (parser, CPP_NAME)
11438 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11440 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11441 if (strcmp ("alloc", p) == 0)
11442 kind = GOMP_MAP_ALLOC;
11443 else if (strcmp ("to", p) == 0)
11444 kind = GOMP_MAP_TO;
11445 else if (strcmp ("from", p) == 0)
11446 kind = GOMP_MAP_FROM;
11447 else if (strcmp ("tofrom", p) == 0)
11448 kind = GOMP_MAP_TOFROM;
11449 else
11451 c_parser_error (parser, "invalid map kind");
11452 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11453 "expected %<)%>");
11454 return list;
11456 c_parser_consume_token (parser);
11457 c_parser_consume_token (parser);
11460 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11462 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11463 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11466 return nl;
11469 /* OpenMP 4.0:
11470 device ( expression ) */
11472 static tree
11473 c_parser_omp_clause_device (c_parser *parser, tree list)
11475 location_t clause_loc = c_parser_peek_token (parser)->location;
11476 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11478 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11479 mark_exp_read (t);
11480 t = c_fully_fold (t, false, NULL);
11482 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11484 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11486 c_parser_error (parser, "expected integer expression");
11487 return list;
11490 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11492 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11493 OMP_CLAUSE_DEVICE_ID (c) = t;
11494 OMP_CLAUSE_CHAIN (c) = list;
11495 list = c;
11498 return list;
11501 /* OpenMP 4.0:
11502 dist_schedule ( static )
11503 dist_schedule ( static , expression ) */
11505 static tree
11506 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11508 tree c, t = NULL_TREE;
11509 location_t loc = c_parser_peek_token (parser)->location;
11511 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11512 return list;
11514 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11516 c_parser_error (parser, "invalid dist_schedule kind");
11517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11518 "expected %<)%>");
11519 return list;
11522 c_parser_consume_token (parser);
11523 if (c_parser_next_token_is (parser, CPP_COMMA))
11525 c_parser_consume_token (parser);
11527 t = c_parser_expr_no_commas (parser, NULL).value;
11528 mark_exp_read (t);
11529 t = c_fully_fold (t, false, NULL);
11530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11532 else
11533 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11534 "expected %<,%> or %<)%>");
11536 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11537 if (t == error_mark_node)
11538 return list;
11540 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11541 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11542 OMP_CLAUSE_CHAIN (c) = list;
11543 return c;
11546 /* OpenMP 4.0:
11547 proc_bind ( proc-bind-kind )
11549 proc-bind-kind:
11550 master | close | spread */
11552 static tree
11553 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11555 location_t clause_loc = c_parser_peek_token (parser)->location;
11556 enum omp_clause_proc_bind_kind kind;
11557 tree c;
11559 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11560 return list;
11562 if (c_parser_next_token_is (parser, CPP_NAME))
11564 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11565 if (strcmp ("master", p) == 0)
11566 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11567 else if (strcmp ("close", p) == 0)
11568 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11569 else if (strcmp ("spread", p) == 0)
11570 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11571 else
11572 goto invalid_kind;
11574 else
11575 goto invalid_kind;
11577 c_parser_consume_token (parser);
11578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11579 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11580 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11581 OMP_CLAUSE_CHAIN (c) = list;
11582 return c;
11584 invalid_kind:
11585 c_parser_error (parser, "invalid proc_bind kind");
11586 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11587 return list;
11590 /* OpenMP 4.0:
11591 to ( variable-list ) */
11593 static tree
11594 c_parser_omp_clause_to (c_parser *parser, tree list)
11596 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11599 /* OpenMP 4.0:
11600 from ( variable-list ) */
11602 static tree
11603 c_parser_omp_clause_from (c_parser *parser, tree list)
11605 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11608 /* OpenMP 4.0:
11609 uniform ( variable-list ) */
11611 static tree
11612 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11614 /* The clauses location. */
11615 location_t loc = c_parser_peek_token (parser)->location;
11617 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11619 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11620 list);
11621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11623 return list;
11626 /* Parse all OpenACC clauses. The set clauses allowed by the directive
11627 is a bitmask in MASK. Return the list of clauses found. */
11629 static tree
11630 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
11631 const char *where, bool finish_p = true)
11633 tree clauses = NULL;
11634 bool first = true;
11636 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11638 location_t here;
11639 pragma_omp_clause c_kind;
11640 const char *c_name;
11641 tree prev = clauses;
11643 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11644 c_parser_consume_token (parser);
11646 here = c_parser_peek_token (parser)->location;
11647 c_kind = c_parser_omp_clause_name (parser);
11649 switch (c_kind)
11651 case PRAGMA_OACC_CLAUSE_ASYNC:
11652 clauses = c_parser_oacc_clause_async (parser, clauses);
11653 c_name = "async";
11654 break;
11655 case PRAGMA_OACC_CLAUSE_COLLAPSE:
11656 clauses = c_parser_omp_clause_collapse (parser, clauses);
11657 c_name = "collapse";
11658 break;
11659 case PRAGMA_OACC_CLAUSE_COPY:
11660 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11661 c_name = "copy";
11662 break;
11663 case PRAGMA_OACC_CLAUSE_COPYIN:
11664 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11665 c_name = "copyin";
11666 break;
11667 case PRAGMA_OACC_CLAUSE_COPYOUT:
11668 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11669 c_name = "copyout";
11670 break;
11671 case PRAGMA_OACC_CLAUSE_CREATE:
11672 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11673 c_name = "create";
11674 break;
11675 case PRAGMA_OACC_CLAUSE_DELETE:
11676 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11677 c_name = "delete";
11678 break;
11679 case PRAGMA_OACC_CLAUSE_DEVICE:
11680 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11681 c_name = "device";
11682 break;
11683 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
11684 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
11685 c_name = "deviceptr";
11686 break;
11687 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
11688 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11689 c_name = "firstprivate";
11690 break;
11691 case PRAGMA_OACC_CLAUSE_HOST:
11692 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11693 c_name = "host";
11694 break;
11695 case PRAGMA_OACC_CLAUSE_IF:
11696 clauses = c_parser_omp_clause_if (parser, clauses);
11697 c_name = "if";
11698 break;
11699 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
11700 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
11701 c_name = "num_gangs";
11702 break;
11703 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
11704 clauses = c_parser_omp_clause_num_workers (parser, clauses);
11705 c_name = "num_workers";
11706 break;
11707 case PRAGMA_OACC_CLAUSE_PRESENT:
11708 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11709 c_name = "present";
11710 break;
11711 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11712 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11713 c_name = "present_or_copy";
11714 break;
11715 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11716 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11717 c_name = "present_or_copyin";
11718 break;
11719 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11720 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11721 c_name = "present_or_copyout";
11722 break;
11723 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11724 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11725 c_name = "present_or_create";
11726 break;
11727 case PRAGMA_OACC_CLAUSE_PRIVATE:
11728 clauses = c_parser_omp_clause_private (parser, clauses);
11729 c_name = "private";
11730 break;
11731 case PRAGMA_OACC_CLAUSE_REDUCTION:
11732 clauses = c_parser_omp_clause_reduction (parser, clauses);
11733 c_name = "reduction";
11734 break;
11735 case PRAGMA_OACC_CLAUSE_SELF:
11736 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11737 c_name = "self";
11738 break;
11739 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
11740 clauses = c_parser_omp_clause_vector_length (parser, clauses);
11741 c_name = "vector_length";
11742 break;
11743 case PRAGMA_OACC_CLAUSE_WAIT:
11744 clauses = c_parser_oacc_clause_wait (parser, clauses);
11745 c_name = "wait";
11746 break;
11747 default:
11748 c_parser_error (parser, "expected %<#pragma acc%> clause");
11749 goto saw_error;
11752 first = false;
11754 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11756 /* Remove the invalid clause(s) from the list to avoid
11757 confusing the rest of the compiler. */
11758 clauses = prev;
11759 error_at (here, "%qs is not valid for %qs", c_name, where);
11763 saw_error:
11764 c_parser_skip_to_pragma_eol (parser);
11766 if (finish_p)
11767 return c_finish_omp_clauses (clauses);
11769 return clauses;
11772 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11773 is a bitmask in MASK. Return the list of clauses found. */
11775 static tree
11776 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11777 const char *where, bool finish_p = true)
11779 tree clauses = NULL;
11780 bool first = true, cilk_simd_fn = false;
11782 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11784 location_t here;
11785 pragma_omp_clause c_kind;
11786 const char *c_name;
11787 tree prev = clauses;
11789 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11790 c_parser_consume_token (parser);
11792 here = c_parser_peek_token (parser)->location;
11793 c_kind = c_parser_omp_clause_name (parser);
11795 switch (c_kind)
11797 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11798 clauses = c_parser_omp_clause_collapse (parser, clauses);
11799 c_name = "collapse";
11800 break;
11801 case PRAGMA_OMP_CLAUSE_COPYIN:
11802 clauses = c_parser_omp_clause_copyin (parser, clauses);
11803 c_name = "copyin";
11804 break;
11805 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11806 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11807 c_name = "copyprivate";
11808 break;
11809 case PRAGMA_OMP_CLAUSE_DEFAULT:
11810 clauses = c_parser_omp_clause_default (parser, clauses);
11811 c_name = "default";
11812 break;
11813 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11814 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11815 c_name = "firstprivate";
11816 break;
11817 case PRAGMA_OMP_CLAUSE_FINAL:
11818 clauses = c_parser_omp_clause_final (parser, clauses);
11819 c_name = "final";
11820 break;
11821 case PRAGMA_OMP_CLAUSE_IF:
11822 clauses = c_parser_omp_clause_if (parser, clauses);
11823 c_name = "if";
11824 break;
11825 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11826 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11827 c_name = "lastprivate";
11828 break;
11829 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11830 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11831 c_name = "mergeable";
11832 break;
11833 case PRAGMA_OMP_CLAUSE_NOWAIT:
11834 clauses = c_parser_omp_clause_nowait (parser, clauses);
11835 c_name = "nowait";
11836 break;
11837 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11838 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11839 c_name = "num_threads";
11840 break;
11841 case PRAGMA_OMP_CLAUSE_ORDERED:
11842 clauses = c_parser_omp_clause_ordered (parser, clauses);
11843 c_name = "ordered";
11844 break;
11845 case PRAGMA_OMP_CLAUSE_PRIVATE:
11846 clauses = c_parser_omp_clause_private (parser, clauses);
11847 c_name = "private";
11848 break;
11849 case PRAGMA_OMP_CLAUSE_REDUCTION:
11850 clauses = c_parser_omp_clause_reduction (parser, clauses);
11851 c_name = "reduction";
11852 break;
11853 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11854 clauses = c_parser_omp_clause_schedule (parser, clauses);
11855 c_name = "schedule";
11856 break;
11857 case PRAGMA_OMP_CLAUSE_SHARED:
11858 clauses = c_parser_omp_clause_shared (parser, clauses);
11859 c_name = "shared";
11860 break;
11861 case PRAGMA_OMP_CLAUSE_UNTIED:
11862 clauses = c_parser_omp_clause_untied (parser, clauses);
11863 c_name = "untied";
11864 break;
11865 case PRAGMA_OMP_CLAUSE_INBRANCH:
11866 case PRAGMA_CILK_CLAUSE_MASK:
11867 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11868 clauses);
11869 c_name = "inbranch";
11870 break;
11871 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11872 case PRAGMA_CILK_CLAUSE_NOMASK:
11873 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11874 clauses);
11875 c_name = "notinbranch";
11876 break;
11877 case PRAGMA_OMP_CLAUSE_PARALLEL:
11878 clauses
11879 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11880 clauses);
11881 c_name = "parallel";
11882 if (!first)
11884 clause_not_first:
11885 error_at (here, "%qs must be the first clause of %qs",
11886 c_name, where);
11887 clauses = prev;
11889 break;
11890 case PRAGMA_OMP_CLAUSE_FOR:
11891 clauses
11892 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11893 clauses);
11894 c_name = "for";
11895 if (!first)
11896 goto clause_not_first;
11897 break;
11898 case PRAGMA_OMP_CLAUSE_SECTIONS:
11899 clauses
11900 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11901 clauses);
11902 c_name = "sections";
11903 if (!first)
11904 goto clause_not_first;
11905 break;
11906 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11907 clauses
11908 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11909 clauses);
11910 c_name = "taskgroup";
11911 if (!first)
11912 goto clause_not_first;
11913 break;
11914 case PRAGMA_OMP_CLAUSE_TO:
11915 clauses = c_parser_omp_clause_to (parser, clauses);
11916 c_name = "to";
11917 break;
11918 case PRAGMA_OMP_CLAUSE_FROM:
11919 clauses = c_parser_omp_clause_from (parser, clauses);
11920 c_name = "from";
11921 break;
11922 case PRAGMA_OMP_CLAUSE_UNIFORM:
11923 clauses = c_parser_omp_clause_uniform (parser, clauses);
11924 c_name = "uniform";
11925 break;
11926 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11927 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11928 c_name = "num_teams";
11929 break;
11930 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11931 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11932 c_name = "thread_limit";
11933 break;
11934 case PRAGMA_OMP_CLAUSE_ALIGNED:
11935 clauses = c_parser_omp_clause_aligned (parser, clauses);
11936 c_name = "aligned";
11937 break;
11938 case PRAGMA_OMP_CLAUSE_LINEAR:
11939 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11940 cilk_simd_fn = true;
11941 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11942 c_name = "linear";
11943 break;
11944 case PRAGMA_OMP_CLAUSE_DEPEND:
11945 clauses = c_parser_omp_clause_depend (parser, clauses);
11946 c_name = "depend";
11947 break;
11948 case PRAGMA_OMP_CLAUSE_MAP:
11949 clauses = c_parser_omp_clause_map (parser, clauses);
11950 c_name = "map";
11951 break;
11952 case PRAGMA_OMP_CLAUSE_DEVICE:
11953 clauses = c_parser_omp_clause_device (parser, clauses);
11954 c_name = "device";
11955 break;
11956 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11957 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11958 c_name = "dist_schedule";
11959 break;
11960 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11961 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11962 c_name = "proc_bind";
11963 break;
11964 case PRAGMA_OMP_CLAUSE_SAFELEN:
11965 clauses = c_parser_omp_clause_safelen (parser, clauses);
11966 c_name = "safelen";
11967 break;
11968 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11969 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11970 c_name = "simdlen";
11971 break;
11972 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11973 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11974 c_name = "simdlen";
11975 break;
11976 default:
11977 c_parser_error (parser, "expected %<#pragma omp%> clause");
11978 goto saw_error;
11981 first = false;
11983 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11985 /* Remove the invalid clause(s) from the list to avoid
11986 confusing the rest of the compiler. */
11987 clauses = prev;
11988 error_at (here, "%qs is not valid for %qs", c_name, where);
11992 saw_error:
11993 c_parser_skip_to_pragma_eol (parser);
11995 if (finish_p)
11996 return c_finish_omp_clauses (clauses);
11998 return clauses;
12001 /* OpenACC 2.0, OpenMP 2.5:
12002 structured-block:
12003 statement
12005 In practice, we're also interested in adding the statement to an
12006 outer node. So it is convenient if we work around the fact that
12007 c_parser_statement calls add_stmt. */
12009 static tree
12010 c_parser_omp_structured_block (c_parser *parser)
12012 tree stmt = push_stmt_list ();
12013 c_parser_statement (parser);
12014 return pop_stmt_list (stmt);
12017 /* OpenACC 2.0:
12018 # pragma acc cache (variable-list) new-line
12020 LOC is the location of the #pragma token.
12023 static tree
12024 c_parser_oacc_cache (location_t loc, c_parser *parser)
12026 tree stmt, clauses;
12028 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
12029 clauses = c_finish_omp_clauses (clauses);
12031 c_parser_skip_to_pragma_eol (parser);
12033 stmt = make_node (OACC_CACHE);
12034 TREE_TYPE (stmt) = void_type_node;
12035 OACC_CACHE_CLAUSES (stmt) = clauses;
12036 SET_EXPR_LOCATION (stmt, loc);
12037 add_stmt (stmt);
12039 return stmt;
12042 /* OpenACC 2.0:
12043 # pragma acc data oacc-data-clause[optseq] new-line
12044 structured-block
12046 LOC is the location of the #pragma token.
12049 #define OACC_DATA_CLAUSE_MASK \
12050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
12062 static tree
12063 c_parser_oacc_data (location_t loc, c_parser *parser)
12065 tree stmt, clauses, block;
12067 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
12068 "#pragma acc data");
12070 block = c_begin_omp_parallel ();
12071 add_stmt (c_parser_omp_structured_block (parser));
12073 stmt = c_finish_oacc_data (loc, clauses, block);
12075 return stmt;
12078 /* OpenACC 2.0:
12079 # pragma acc kernels oacc-kernels-clause[optseq] new-line
12080 structured-block
12082 LOC is the location of the #pragma token.
12085 #define OACC_KERNELS_CLAUSE_MASK \
12086 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12100 static tree
12101 c_parser_oacc_kernels (location_t loc, c_parser *parser, char *p_name)
12103 tree stmt, clauses = NULL_TREE, block;
12105 strcat (p_name, " kernels");
12107 if (c_parser_next_token_is (parser, CPP_NAME))
12109 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12110 if (strcmp (p, "loop") == 0)
12112 c_parser_consume_token (parser);
12113 block = c_begin_omp_parallel ();
12114 c_parser_oacc_loop (loc, parser, p_name);
12115 stmt = c_finish_oacc_kernels (loc, clauses, block);
12116 OACC_KERNELS_COMBINED (stmt) = 1;
12117 return stmt;
12121 clauses = c_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
12122 p_name);
12124 block = c_begin_omp_parallel ();
12125 add_stmt (c_parser_omp_structured_block (parser));
12127 stmt = c_finish_oacc_kernels (loc, clauses, block);
12129 return stmt;
12132 /* OpenACC 2.0:
12133 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
12137 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
12140 LOC is the location of the #pragma token.
12143 #define OACC_ENTER_DATA_CLAUSE_MASK \
12144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12152 #define OACC_EXIT_DATA_CLAUSE_MASK \
12153 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
12157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12159 static void
12160 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
12162 location_t loc = c_parser_peek_token (parser)->location;
12163 tree clauses, stmt;
12165 c_parser_consume_pragma (parser);
12167 if (!c_parser_next_token_is (parser, CPP_NAME))
12169 c_parser_error (parser, enter
12170 ? "expected %<data%> in %<#pragma acc enter data%>"
12171 : "expected %<data%> in %<#pragma acc exit data%>");
12172 c_parser_skip_to_pragma_eol (parser);
12173 return;
12176 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12177 if (strcmp (p, "data") != 0)
12179 c_parser_error (parser, "invalid pragma");
12180 c_parser_skip_to_pragma_eol (parser);
12181 return;
12184 c_parser_consume_token (parser);
12186 if (enter)
12187 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
12188 "#pragma acc enter data");
12189 else
12190 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
12191 "#pragma acc exit data");
12193 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12195 error_at (loc, enter
12196 ? "%<#pragma acc enter data%> has no data movement clause"
12197 : "%<#pragma acc exit data%> has no data movement clause");
12198 return;
12201 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
12202 TREE_TYPE (stmt) = void_type_node;
12203 OMP_STANDALONE_CLAUSES (stmt) = clauses;
12204 SET_EXPR_LOCATION (stmt, loc);
12205 add_stmt (stmt);
12209 /* OpenACC 2.0:
12211 # pragma acc loop oacc-loop-clause[optseq] new-line
12212 structured-block
12214 LOC is the location of the #pragma token.
12217 #define OACC_LOOP_CLAUSE_MASK \
12218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
12219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) )
12221 static tree
12222 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name)
12224 tree stmt, clauses, block;
12226 strcat (p_name, " loop");
12228 clauses = c_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK, p_name);
12230 block = c_begin_compound_stmt (true);
12231 stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
12232 block = c_end_compound_stmt (loc, block, true);
12233 add_stmt (block);
12235 return stmt;
12238 /* OpenACC 2.0:
12239 # pragma acc parallel oacc-parallel-clause[optseq] new-line
12240 structured-block
12242 LOC is the location of the #pragma token.
12245 #define OACC_PARALLEL_CLAUSE_MASK \
12246 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
12248 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
12249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
12250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
12251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
12252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
12254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
12255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
12256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
12257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
12258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
12259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
12260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
12261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
12262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12264 static tree
12265 c_parser_oacc_parallel (location_t loc, c_parser *parser, char *p_name)
12267 tree stmt, clauses = NULL_TREE, block;
12269 strcat (p_name, " parallel");
12271 if (c_parser_next_token_is (parser, CPP_NAME))
12273 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12274 if (strcmp (p, "loop") == 0)
12276 c_parser_consume_token (parser);
12277 block = c_begin_omp_parallel ();
12278 c_parser_oacc_loop (loc, parser, p_name);
12279 stmt = c_finish_oacc_parallel (loc, clauses, block);
12280 OACC_PARALLEL_COMBINED (stmt) = 1;
12281 return stmt;
12285 clauses = c_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
12286 p_name);
12288 block = c_begin_omp_parallel ();
12289 add_stmt (c_parser_omp_structured_block (parser));
12291 stmt = c_finish_oacc_parallel (loc, clauses, block);
12293 return stmt;
12296 /* OpenACC 2.0:
12297 # pragma acc update oacc-update-clause[optseq] new-line
12300 #define OACC_UPDATE_CLAUSE_MASK \
12301 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
12302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
12303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
12304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
12305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
12306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
12308 static void
12309 c_parser_oacc_update (c_parser *parser)
12311 location_t loc = c_parser_peek_token (parser)->location;
12313 c_parser_consume_pragma (parser);
12315 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
12316 "#pragma acc update");
12317 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12319 error_at (loc,
12320 "%<#pragma acc update%> must contain at least one "
12321 "%<device%> or %<host/self%> clause");
12322 return;
12325 if (parser->error)
12326 return;
12328 tree stmt = make_node (OACC_UPDATE);
12329 TREE_TYPE (stmt) = void_type_node;
12330 OACC_UPDATE_CLAUSES (stmt) = clauses;
12331 SET_EXPR_LOCATION (stmt, loc);
12332 add_stmt (stmt);
12335 /* OpenACC 2.0:
12336 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
12338 LOC is the location of the #pragma token.
12341 #define OACC_WAIT_CLAUSE_MASK \
12342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
12344 static tree
12345 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
12347 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
12349 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12350 list = c_parser_oacc_wait_list (parser, loc, list);
12352 strcpy (p_name, " wait");
12353 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
12354 stmt = c_finish_oacc_wait (loc, list, clauses);
12356 return stmt;
12359 /* OpenMP 2.5:
12360 # pragma omp atomic new-line
12361 expression-stmt
12363 expression-stmt:
12364 x binop= expr | x++ | ++x | x-- | --x
12365 binop:
12366 +, *, -, /, &, ^, |, <<, >>
12368 where x is an lvalue expression with scalar type.
12370 OpenMP 3.1:
12371 # pragma omp atomic new-line
12372 update-stmt
12374 # pragma omp atomic read new-line
12375 read-stmt
12377 # pragma omp atomic write new-line
12378 write-stmt
12380 # pragma omp atomic update new-line
12381 update-stmt
12383 # pragma omp atomic capture new-line
12384 capture-stmt
12386 # pragma omp atomic capture new-line
12387 capture-block
12389 read-stmt:
12390 v = x
12391 write-stmt:
12392 x = expr
12393 update-stmt:
12394 expression-stmt | x = x binop expr
12395 capture-stmt:
12396 v = expression-stmt
12397 capture-block:
12398 { v = x; update-stmt; } | { update-stmt; v = x; }
12400 OpenMP 4.0:
12401 update-stmt:
12402 expression-stmt | x = x binop expr | x = expr binop x
12403 capture-stmt:
12404 v = update-stmt
12405 capture-block:
12406 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
12408 where x and v are lvalue expressions with scalar type.
12410 LOC is the location of the #pragma token. */
12412 static void
12413 c_parser_omp_atomic (location_t loc, c_parser *parser)
12415 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
12416 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
12417 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
12418 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
12419 struct c_expr expr;
12420 location_t eloc;
12421 bool structured_block = false;
12422 bool swapped = false;
12423 bool seq_cst = false;
12425 if (c_parser_next_token_is (parser, CPP_NAME))
12427 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12428 if (!strcmp (p, "seq_cst"))
12430 seq_cst = true;
12431 c_parser_consume_token (parser);
12432 if (c_parser_next_token_is (parser, CPP_COMMA)
12433 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12434 c_parser_consume_token (parser);
12437 if (c_parser_next_token_is (parser, CPP_NAME))
12439 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12441 if (!strcmp (p, "read"))
12442 code = OMP_ATOMIC_READ;
12443 else if (!strcmp (p, "write"))
12444 code = NOP_EXPR;
12445 else if (!strcmp (p, "update"))
12446 code = OMP_ATOMIC;
12447 else if (!strcmp (p, "capture"))
12448 code = OMP_ATOMIC_CAPTURE_NEW;
12449 else
12450 p = NULL;
12451 if (p)
12452 c_parser_consume_token (parser);
12454 if (!seq_cst)
12456 if (c_parser_next_token_is (parser, CPP_COMMA)
12457 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12458 c_parser_consume_token (parser);
12460 if (c_parser_next_token_is (parser, CPP_NAME))
12462 const char *p
12463 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12464 if (!strcmp (p, "seq_cst"))
12466 seq_cst = true;
12467 c_parser_consume_token (parser);
12471 c_parser_skip_to_pragma_eol (parser);
12473 switch (code)
12475 case OMP_ATOMIC_READ:
12476 case NOP_EXPR: /* atomic write */
12477 v = c_parser_unary_expression (parser).value;
12478 v = c_fully_fold (v, false, NULL);
12479 if (v == error_mark_node)
12480 goto saw_error;
12481 loc = c_parser_peek_token (parser)->location;
12482 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12483 goto saw_error;
12484 if (code == NOP_EXPR)
12485 lhs = c_parser_expression (parser).value;
12486 else
12487 lhs = c_parser_unary_expression (parser).value;
12488 lhs = c_fully_fold (lhs, false, NULL);
12489 if (lhs == error_mark_node)
12490 goto saw_error;
12491 if (code == NOP_EXPR)
12493 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
12494 opcode. */
12495 code = OMP_ATOMIC;
12496 rhs = lhs;
12497 lhs = v;
12498 v = NULL_TREE;
12500 goto done;
12501 case OMP_ATOMIC_CAPTURE_NEW:
12502 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12504 c_parser_consume_token (parser);
12505 structured_block = true;
12507 else
12509 v = c_parser_unary_expression (parser).value;
12510 v = c_fully_fold (v, false, NULL);
12511 if (v == error_mark_node)
12512 goto saw_error;
12513 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12514 goto saw_error;
12516 break;
12517 default:
12518 break;
12521 /* For structured_block case we don't know yet whether
12522 old or new x should be captured. */
12523 restart:
12524 eloc = c_parser_peek_token (parser)->location;
12525 expr = c_parser_unary_expression (parser);
12526 lhs = expr.value;
12527 expr = default_function_array_conversion (eloc, expr);
12528 unfolded_lhs = expr.value;
12529 lhs = c_fully_fold (lhs, false, NULL);
12530 orig_lhs = lhs;
12531 switch (TREE_CODE (lhs))
12533 case ERROR_MARK:
12534 saw_error:
12535 c_parser_skip_to_end_of_block_or_statement (parser);
12536 if (structured_block)
12538 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12539 c_parser_consume_token (parser);
12540 else if (code == OMP_ATOMIC_CAPTURE_NEW)
12542 c_parser_skip_to_end_of_block_or_statement (parser);
12543 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12544 c_parser_consume_token (parser);
12547 return;
12549 case POSTINCREMENT_EXPR:
12550 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12551 code = OMP_ATOMIC_CAPTURE_OLD;
12552 /* FALLTHROUGH */
12553 case PREINCREMENT_EXPR:
12554 lhs = TREE_OPERAND (lhs, 0);
12555 unfolded_lhs = NULL_TREE;
12556 opcode = PLUS_EXPR;
12557 rhs = integer_one_node;
12558 break;
12560 case POSTDECREMENT_EXPR:
12561 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12562 code = OMP_ATOMIC_CAPTURE_OLD;
12563 /* FALLTHROUGH */
12564 case PREDECREMENT_EXPR:
12565 lhs = TREE_OPERAND (lhs, 0);
12566 unfolded_lhs = NULL_TREE;
12567 opcode = MINUS_EXPR;
12568 rhs = integer_one_node;
12569 break;
12571 case COMPOUND_EXPR:
12572 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
12573 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
12574 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
12575 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
12576 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
12577 (TREE_OPERAND (lhs, 1), 0), 0)))
12578 == BOOLEAN_TYPE)
12579 /* Undo effects of boolean_increment for post {in,de}crement. */
12580 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
12581 /* FALLTHRU */
12582 case MODIFY_EXPR:
12583 if (TREE_CODE (lhs) == MODIFY_EXPR
12584 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
12586 /* Undo effects of boolean_increment. */
12587 if (integer_onep (TREE_OPERAND (lhs, 1)))
12589 /* This is pre or post increment. */
12590 rhs = TREE_OPERAND (lhs, 1);
12591 lhs = TREE_OPERAND (lhs, 0);
12592 unfolded_lhs = NULL_TREE;
12593 opcode = NOP_EXPR;
12594 if (code == OMP_ATOMIC_CAPTURE_NEW
12595 && !structured_block
12596 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12597 code = OMP_ATOMIC_CAPTURE_OLD;
12598 break;
12600 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
12601 && TREE_OPERAND (lhs, 0)
12602 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
12604 /* This is pre or post decrement. */
12605 rhs = TREE_OPERAND (lhs, 1);
12606 lhs = TREE_OPERAND (lhs, 0);
12607 unfolded_lhs = NULL_TREE;
12608 opcode = NOP_EXPR;
12609 if (code == OMP_ATOMIC_CAPTURE_NEW
12610 && !structured_block
12611 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12612 code = OMP_ATOMIC_CAPTURE_OLD;
12613 break;
12616 /* FALLTHRU */
12617 default:
12618 switch (c_parser_peek_token (parser)->type)
12620 case CPP_MULT_EQ:
12621 opcode = MULT_EXPR;
12622 break;
12623 case CPP_DIV_EQ:
12624 opcode = TRUNC_DIV_EXPR;
12625 break;
12626 case CPP_PLUS_EQ:
12627 opcode = PLUS_EXPR;
12628 break;
12629 case CPP_MINUS_EQ:
12630 opcode = MINUS_EXPR;
12631 break;
12632 case CPP_LSHIFT_EQ:
12633 opcode = LSHIFT_EXPR;
12634 break;
12635 case CPP_RSHIFT_EQ:
12636 opcode = RSHIFT_EXPR;
12637 break;
12638 case CPP_AND_EQ:
12639 opcode = BIT_AND_EXPR;
12640 break;
12641 case CPP_OR_EQ:
12642 opcode = BIT_IOR_EXPR;
12643 break;
12644 case CPP_XOR_EQ:
12645 opcode = BIT_XOR_EXPR;
12646 break;
12647 case CPP_EQ:
12648 c_parser_consume_token (parser);
12649 eloc = c_parser_peek_token (parser)->location;
12650 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
12651 rhs1 = expr.value;
12652 switch (TREE_CODE (rhs1))
12654 case MULT_EXPR:
12655 case TRUNC_DIV_EXPR:
12656 case RDIV_EXPR:
12657 case PLUS_EXPR:
12658 case MINUS_EXPR:
12659 case LSHIFT_EXPR:
12660 case RSHIFT_EXPR:
12661 case BIT_AND_EXPR:
12662 case BIT_IOR_EXPR:
12663 case BIT_XOR_EXPR:
12664 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
12666 opcode = TREE_CODE (rhs1);
12667 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12668 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12669 goto stmt_done;
12671 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
12673 opcode = TREE_CODE (rhs1);
12674 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12675 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12676 swapped = !commutative_tree_code (opcode);
12677 goto stmt_done;
12679 break;
12680 case ERROR_MARK:
12681 goto saw_error;
12682 default:
12683 break;
12685 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
12687 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12689 code = OMP_ATOMIC_CAPTURE_OLD;
12690 v = lhs;
12691 lhs = NULL_TREE;
12692 expr = default_function_array_read_conversion (eloc, expr);
12693 unfolded_lhs1 = expr.value;
12694 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
12695 rhs1 = NULL_TREE;
12696 c_parser_consume_token (parser);
12697 goto restart;
12699 if (structured_block)
12701 opcode = NOP_EXPR;
12702 expr = default_function_array_read_conversion (eloc, expr);
12703 rhs = c_fully_fold (expr.value, false, NULL);
12704 rhs1 = NULL_TREE;
12705 goto stmt_done;
12708 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12709 goto saw_error;
12710 default:
12711 c_parser_error (parser,
12712 "invalid operator for %<#pragma omp atomic%>");
12713 goto saw_error;
12716 /* Arrange to pass the location of the assignment operator to
12717 c_finish_omp_atomic. */
12718 loc = c_parser_peek_token (parser)->location;
12719 c_parser_consume_token (parser);
12720 eloc = c_parser_peek_token (parser)->location;
12721 expr = c_parser_expression (parser);
12722 expr = default_function_array_read_conversion (eloc, expr);
12723 rhs = expr.value;
12724 rhs = c_fully_fold (rhs, false, NULL);
12725 break;
12727 stmt_done:
12728 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12730 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12731 goto saw_error;
12732 v = c_parser_unary_expression (parser).value;
12733 v = c_fully_fold (v, false, NULL);
12734 if (v == error_mark_node)
12735 goto saw_error;
12736 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12737 goto saw_error;
12738 eloc = c_parser_peek_token (parser)->location;
12739 expr = c_parser_unary_expression (parser);
12740 lhs1 = expr.value;
12741 expr = default_function_array_read_conversion (eloc, expr);
12742 unfolded_lhs1 = expr.value;
12743 lhs1 = c_fully_fold (lhs1, false, NULL);
12744 if (lhs1 == error_mark_node)
12745 goto saw_error;
12747 if (structured_block)
12749 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12750 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12752 done:
12753 if (unfolded_lhs && unfolded_lhs1
12754 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12756 error ("%<#pragma omp atomic capture%> uses two different "
12757 "expressions for memory");
12758 stmt = error_mark_node;
12760 else
12761 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12762 swapped, seq_cst);
12763 if (stmt != error_mark_node)
12764 add_stmt (stmt);
12766 if (!structured_block)
12767 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12771 /* OpenMP 2.5:
12772 # pragma omp barrier new-line
12775 static void
12776 c_parser_omp_barrier (c_parser *parser)
12778 location_t loc = c_parser_peek_token (parser)->location;
12779 c_parser_consume_pragma (parser);
12780 c_parser_skip_to_pragma_eol (parser);
12782 c_finish_omp_barrier (loc);
12785 /* OpenMP 2.5:
12786 # pragma omp critical [(name)] new-line
12787 structured-block
12789 LOC is the location of the #pragma itself. */
12791 static tree
12792 c_parser_omp_critical (location_t loc, c_parser *parser)
12794 tree stmt, name = NULL;
12796 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12798 c_parser_consume_token (parser);
12799 if (c_parser_next_token_is (parser, CPP_NAME))
12801 name = c_parser_peek_token (parser)->value;
12802 c_parser_consume_token (parser);
12803 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12805 else
12806 c_parser_error (parser, "expected identifier");
12808 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12809 c_parser_error (parser, "expected %<(%> or end of line");
12810 c_parser_skip_to_pragma_eol (parser);
12812 stmt = c_parser_omp_structured_block (parser);
12813 return c_finish_omp_critical (loc, stmt, name);
12816 /* OpenMP 2.5:
12817 # pragma omp flush flush-vars[opt] new-line
12819 flush-vars:
12820 ( variable-list ) */
12822 static void
12823 c_parser_omp_flush (c_parser *parser)
12825 location_t loc = c_parser_peek_token (parser)->location;
12826 c_parser_consume_pragma (parser);
12827 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12828 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12829 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12830 c_parser_error (parser, "expected %<(%> or end of line");
12831 c_parser_skip_to_pragma_eol (parser);
12833 c_finish_omp_flush (loc);
12836 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
12837 The real trick here is to determine the loop control variable early
12838 so that we can push a new decl if necessary to make it private.
12839 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
12840 respectively. */
12842 static tree
12843 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12844 tree clauses, tree *cclauses)
12846 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12847 tree declv, condv, incrv, initv, ret = NULL;
12848 bool fail = false, open_brace_parsed = false;
12849 int i, collapse = 1, nbraces = 0;
12850 location_t for_loc;
12851 vec<tree, va_gc> *for_block = make_tree_vector ();
12853 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12854 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12855 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12857 gcc_assert (collapse >= 1);
12859 declv = make_tree_vec (collapse);
12860 initv = make_tree_vec (collapse);
12861 condv = make_tree_vec (collapse);
12862 incrv = make_tree_vec (collapse);
12864 if (code != CILK_FOR
12865 && !c_parser_next_token_is_keyword (parser, RID_FOR))
12867 c_parser_error (parser, "for statement expected");
12868 return NULL;
12870 if (code == CILK_FOR
12871 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
12873 c_parser_error (parser, "_Cilk_for statement expected");
12874 return NULL;
12876 for_loc = c_parser_peek_token (parser)->location;
12877 c_parser_consume_token (parser);
12879 for (i = 0; i < collapse; i++)
12881 int bracecount = 0;
12883 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12884 goto pop_scopes;
12886 /* Parse the initialization declaration or expression. */
12887 if (c_parser_next_tokens_start_declaration (parser))
12889 if (i > 0)
12890 vec_safe_push (for_block, c_begin_compound_stmt (true));
12891 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12892 NULL, vNULL);
12893 decl = check_for_loop_decls (for_loc, flag_isoc99);
12894 if (decl == NULL)
12895 goto error_init;
12896 if (DECL_INITIAL (decl) == error_mark_node)
12897 decl = error_mark_node;
12898 init = decl;
12900 else if (c_parser_next_token_is (parser, CPP_NAME)
12901 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12903 struct c_expr decl_exp;
12904 struct c_expr init_exp;
12905 location_t init_loc;
12907 decl_exp = c_parser_postfix_expression (parser);
12908 decl = decl_exp.value;
12910 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12912 init_loc = c_parser_peek_token (parser)->location;
12913 init_exp = c_parser_expr_no_commas (parser, NULL);
12914 init_exp = default_function_array_read_conversion (init_loc,
12915 init_exp);
12916 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12917 NOP_EXPR, init_loc, init_exp.value,
12918 init_exp.original_type);
12919 init = c_process_expr_stmt (init_loc, init);
12921 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12923 else
12925 error_init:
12926 c_parser_error (parser,
12927 "expected iteration declaration or initialization");
12928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12929 "expected %<)%>");
12930 fail = true;
12931 goto parse_next;
12934 /* Parse the loop condition. */
12935 cond = NULL_TREE;
12936 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12938 location_t cond_loc = c_parser_peek_token (parser)->location;
12939 struct c_expr cond_expr
12940 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12942 cond = cond_expr.value;
12943 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12944 cond = c_fully_fold (cond, false, NULL);
12945 switch (cond_expr.original_code)
12947 case GT_EXPR:
12948 case GE_EXPR:
12949 case LT_EXPR:
12950 case LE_EXPR:
12951 break;
12952 case NE_EXPR:
12953 if (code == CILK_SIMD || code == CILK_FOR)
12954 break;
12955 /* FALLTHRU. */
12956 default:
12957 /* Can't be cond = error_mark_node, because we want to preserve
12958 the location until c_finish_omp_for. */
12959 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
12960 break;
12962 protected_set_expr_location (cond, cond_loc);
12964 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12966 /* Parse the increment expression. */
12967 incr = NULL_TREE;
12968 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
12970 location_t incr_loc = c_parser_peek_token (parser)->location;
12972 incr = c_process_expr_stmt (incr_loc,
12973 c_parser_expression (parser).value);
12975 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12977 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
12978 fail = true;
12979 else
12981 TREE_VEC_ELT (declv, i) = decl;
12982 TREE_VEC_ELT (initv, i) = init;
12983 TREE_VEC_ELT (condv, i) = cond;
12984 TREE_VEC_ELT (incrv, i) = incr;
12987 parse_next:
12988 if (i == collapse - 1)
12989 break;
12991 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
12992 in between the collapsed for loops to be still considered perfectly
12993 nested. Hopefully the final version clarifies this.
12994 For now handle (multiple) {'s and empty statements. */
12997 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12999 c_parser_consume_token (parser);
13000 break;
13002 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
13004 c_parser_consume_token (parser);
13005 bracecount++;
13007 else if (bracecount
13008 && c_parser_next_token_is (parser, CPP_SEMICOLON))
13009 c_parser_consume_token (parser);
13010 else
13012 c_parser_error (parser, "not enough perfectly nested loops");
13013 if (bracecount)
13015 open_brace_parsed = true;
13016 bracecount--;
13018 fail = true;
13019 collapse = 0;
13020 break;
13023 while (1);
13025 nbraces += bracecount;
13028 save_break = c_break_label;
13029 if (code == CILK_SIMD)
13030 c_break_label = build_int_cst (size_type_node, 2);
13031 else
13032 c_break_label = size_one_node;
13033 save_cont = c_cont_label;
13034 c_cont_label = NULL_TREE;
13035 body = push_stmt_list ();
13037 if (open_brace_parsed)
13039 location_t here = c_parser_peek_token (parser)->location;
13040 stmt = c_begin_compound_stmt (true);
13041 c_parser_compound_statement_nostart (parser);
13042 add_stmt (c_end_compound_stmt (here, stmt, true));
13044 else
13045 add_stmt (c_parser_c99_block_statement (parser));
13046 if (c_cont_label)
13048 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
13049 SET_EXPR_LOCATION (t, loc);
13050 add_stmt (t);
13053 body = pop_stmt_list (body);
13054 c_break_label = save_break;
13055 c_cont_label = save_cont;
13057 while (nbraces)
13059 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13061 c_parser_consume_token (parser);
13062 nbraces--;
13064 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
13065 c_parser_consume_token (parser);
13066 else
13068 c_parser_error (parser, "collapsed loops not perfectly nested");
13069 while (nbraces)
13071 location_t here = c_parser_peek_token (parser)->location;
13072 stmt = c_begin_compound_stmt (true);
13073 add_stmt (body);
13074 c_parser_compound_statement_nostart (parser);
13075 body = c_end_compound_stmt (here, stmt, true);
13076 nbraces--;
13078 goto pop_scopes;
13082 /* Only bother calling c_finish_omp_for if we haven't already generated
13083 an error from the initialization parsing. */
13084 if (!fail)
13086 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
13087 incrv, body, NULL);
13088 if (stmt)
13090 if (cclauses != NULL
13091 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
13093 tree *c;
13094 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
13095 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
13096 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
13097 c = &OMP_CLAUSE_CHAIN (*c);
13098 else
13100 for (i = 0; i < collapse; i++)
13101 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
13102 break;
13103 if (i == collapse)
13104 c = &OMP_CLAUSE_CHAIN (*c);
13105 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
13107 error_at (loc,
13108 "iteration variable %qD should not be firstprivate",
13109 OMP_CLAUSE_DECL (*c));
13110 *c = OMP_CLAUSE_CHAIN (*c);
13112 else
13114 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
13115 tree l = *c;
13116 *c = OMP_CLAUSE_CHAIN (*c);
13117 if (code == OMP_SIMD)
13119 OMP_CLAUSE_CHAIN (l)
13120 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13121 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
13123 else
13125 OMP_CLAUSE_CHAIN (l) = clauses;
13126 clauses = l;
13131 OMP_FOR_CLAUSES (stmt) = clauses;
13133 ret = stmt;
13135 pop_scopes:
13136 while (!for_block->is_empty ())
13138 /* FIXME diagnostics: LOC below should be the actual location of
13139 this particular for block. We need to build a list of
13140 locations to go along with FOR_BLOCK. */
13141 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
13142 add_stmt (stmt);
13144 release_tree_vector (for_block);
13145 return ret;
13148 /* Helper function for OpenMP parsing, split clauses and call
13149 finish_omp_clauses on each of the set of clauses afterwards. */
13151 static void
13152 omp_split_clauses (location_t loc, enum tree_code code,
13153 omp_clause_mask mask, tree clauses, tree *cclauses)
13155 int i;
13156 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
13157 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
13158 if (cclauses[i])
13159 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
13162 /* OpenMP 4.0:
13163 #pragma omp simd simd-clause[optseq] new-line
13164 for-loop
13166 LOC is the location of the #pragma token.
13169 #define OMP_SIMD_CLAUSE_MASK \
13170 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
13171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13178 static tree
13179 c_parser_omp_simd (location_t loc, c_parser *parser,
13180 char *p_name, omp_clause_mask mask, tree *cclauses)
13182 tree block, clauses, ret;
13184 strcat (p_name, " simd");
13185 mask |= OMP_SIMD_CLAUSE_MASK;
13186 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
13188 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13189 if (cclauses)
13191 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
13192 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
13195 block = c_begin_compound_stmt (true);
13196 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
13197 block = c_end_compound_stmt (loc, block, true);
13198 add_stmt (block);
13200 return ret;
13203 /* OpenMP 2.5:
13204 #pragma omp for for-clause[optseq] new-line
13205 for-loop
13207 OpenMP 4.0:
13208 #pragma omp for simd for-simd-clause[optseq] new-line
13209 for-loop
13211 LOC is the location of the #pragma token.
13214 #define OMP_FOR_CLAUSE_MASK \
13215 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
13220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
13221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
13222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13224 static tree
13225 c_parser_omp_for (location_t loc, c_parser *parser,
13226 char *p_name, omp_clause_mask mask, tree *cclauses)
13228 tree block, clauses, ret;
13230 strcat (p_name, " for");
13231 mask |= OMP_FOR_CLAUSE_MASK;
13232 if (cclauses)
13233 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13235 if (c_parser_next_token_is (parser, CPP_NAME))
13237 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13239 if (strcmp (p, "simd") == 0)
13241 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13242 if (cclauses == NULL)
13243 cclauses = cclauses_buf;
13245 c_parser_consume_token (parser);
13246 if (!flag_openmp) /* flag_openmp_simd */
13247 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13248 block = c_begin_compound_stmt (true);
13249 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13250 block = c_end_compound_stmt (loc, block, true);
13251 if (ret == NULL_TREE)
13252 return ret;
13253 ret = make_node (OMP_FOR);
13254 TREE_TYPE (ret) = void_type_node;
13255 OMP_FOR_BODY (ret) = block;
13256 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13257 SET_EXPR_LOCATION (ret, loc);
13258 add_stmt (ret);
13259 return ret;
13262 if (!flag_openmp) /* flag_openmp_simd */
13264 c_parser_skip_to_pragma_eol (parser, false);
13265 return NULL_TREE;
13268 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13269 if (cclauses)
13271 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
13272 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13275 block = c_begin_compound_stmt (true);
13276 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
13277 block = c_end_compound_stmt (loc, block, true);
13278 add_stmt (block);
13280 return ret;
13283 /* OpenMP 2.5:
13284 # pragma omp master new-line
13285 structured-block
13287 LOC is the location of the #pragma token.
13290 static tree
13291 c_parser_omp_master (location_t loc, c_parser *parser)
13293 c_parser_skip_to_pragma_eol (parser);
13294 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
13297 /* OpenMP 2.5:
13298 # pragma omp ordered new-line
13299 structured-block
13301 LOC is the location of the #pragma itself.
13304 static tree
13305 c_parser_omp_ordered (location_t loc, c_parser *parser)
13307 c_parser_skip_to_pragma_eol (parser);
13308 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
13311 /* OpenMP 2.5:
13313 section-scope:
13314 { section-sequence }
13316 section-sequence:
13317 section-directive[opt] structured-block
13318 section-sequence section-directive structured-block
13320 SECTIONS_LOC is the location of the #pragma omp sections. */
13322 static tree
13323 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
13325 tree stmt, substmt;
13326 bool error_suppress = false;
13327 location_t loc;
13329 loc = c_parser_peek_token (parser)->location;
13330 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
13332 /* Avoid skipping until the end of the block. */
13333 parser->error = false;
13334 return NULL_TREE;
13337 stmt = push_stmt_list ();
13339 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
13341 substmt = c_parser_omp_structured_block (parser);
13342 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13343 SET_EXPR_LOCATION (substmt, loc);
13344 add_stmt (substmt);
13347 while (1)
13349 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13350 break;
13351 if (c_parser_next_token_is (parser, CPP_EOF))
13352 break;
13354 loc = c_parser_peek_token (parser)->location;
13355 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
13357 c_parser_consume_pragma (parser);
13358 c_parser_skip_to_pragma_eol (parser);
13359 error_suppress = false;
13361 else if (!error_suppress)
13363 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
13364 error_suppress = true;
13367 substmt = c_parser_omp_structured_block (parser);
13368 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13369 SET_EXPR_LOCATION (substmt, loc);
13370 add_stmt (substmt);
13372 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
13373 "expected %<#pragma omp section%> or %<}%>");
13375 substmt = pop_stmt_list (stmt);
13377 stmt = make_node (OMP_SECTIONS);
13378 SET_EXPR_LOCATION (stmt, sections_loc);
13379 TREE_TYPE (stmt) = void_type_node;
13380 OMP_SECTIONS_BODY (stmt) = substmt;
13382 return add_stmt (stmt);
13385 /* OpenMP 2.5:
13386 # pragma omp sections sections-clause[optseq] newline
13387 sections-scope
13389 LOC is the location of the #pragma token.
13392 #define OMP_SECTIONS_CLAUSE_MASK \
13393 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13399 static tree
13400 c_parser_omp_sections (location_t loc, c_parser *parser,
13401 char *p_name, omp_clause_mask mask, tree *cclauses)
13403 tree block, clauses, ret;
13405 strcat (p_name, " sections");
13406 mask |= OMP_SECTIONS_CLAUSE_MASK;
13407 if (cclauses)
13408 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13410 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13411 if (cclauses)
13413 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
13414 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
13417 block = c_begin_compound_stmt (true);
13418 ret = c_parser_omp_sections_scope (loc, parser);
13419 if (ret)
13420 OMP_SECTIONS_CLAUSES (ret) = clauses;
13421 block = c_end_compound_stmt (loc, block, true);
13422 add_stmt (block);
13424 return ret;
13427 /* OpenMP 2.5:
13428 # pragma omp parallel parallel-clause[optseq] new-line
13429 structured-block
13430 # pragma omp parallel for parallel-for-clause[optseq] new-line
13431 structured-block
13432 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
13433 structured-block
13435 OpenMP 4.0:
13436 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
13437 structured-block
13439 LOC is the location of the #pragma token.
13442 #define OMP_PARALLEL_CLAUSE_MASK \
13443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
13449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
13451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
13453 static tree
13454 c_parser_omp_parallel (location_t loc, c_parser *parser,
13455 char *p_name, omp_clause_mask mask, tree *cclauses)
13457 tree stmt, clauses, block;
13459 strcat (p_name, " parallel");
13460 mask |= OMP_PARALLEL_CLAUSE_MASK;
13462 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13464 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13465 if (cclauses == NULL)
13466 cclauses = cclauses_buf;
13468 c_parser_consume_token (parser);
13469 if (!flag_openmp) /* flag_openmp_simd */
13470 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13471 block = c_begin_omp_parallel ();
13472 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13473 stmt
13474 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13475 block);
13476 if (ret == NULL_TREE)
13477 return ret;
13478 OMP_PARALLEL_COMBINED (stmt) = 1;
13479 return stmt;
13481 else if (cclauses)
13483 error_at (loc, "expected %<for%> after %qs", p_name);
13484 c_parser_skip_to_pragma_eol (parser);
13485 return NULL_TREE;
13487 else if (!flag_openmp) /* flag_openmp_simd */
13489 c_parser_skip_to_pragma_eol (parser, false);
13490 return NULL_TREE;
13492 else if (c_parser_next_token_is (parser, CPP_NAME))
13494 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13495 if (strcmp (p, "sections") == 0)
13497 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13498 if (cclauses == NULL)
13499 cclauses = cclauses_buf;
13501 c_parser_consume_token (parser);
13502 block = c_begin_omp_parallel ();
13503 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
13504 stmt = c_finish_omp_parallel (loc,
13505 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13506 block);
13507 OMP_PARALLEL_COMBINED (stmt) = 1;
13508 return stmt;
13512 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13514 block = c_begin_omp_parallel ();
13515 c_parser_statement (parser);
13516 stmt = c_finish_omp_parallel (loc, clauses, block);
13518 return stmt;
13521 /* OpenMP 2.5:
13522 # pragma omp single single-clause[optseq] new-line
13523 structured-block
13525 LOC is the location of the #pragma.
13528 #define OMP_SINGLE_CLAUSE_MASK \
13529 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
13532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13534 static tree
13535 c_parser_omp_single (location_t loc, c_parser *parser)
13537 tree stmt = make_node (OMP_SINGLE);
13538 SET_EXPR_LOCATION (stmt, loc);
13539 TREE_TYPE (stmt) = void_type_node;
13541 OMP_SINGLE_CLAUSES (stmt)
13542 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
13543 "#pragma omp single");
13544 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
13546 return add_stmt (stmt);
13549 /* OpenMP 3.0:
13550 # pragma omp task task-clause[optseq] new-line
13552 LOC is the location of the #pragma.
13555 #define OMP_TASK_CLAUSE_MASK \
13556 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
13558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
13563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
13564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
13566 static tree
13567 c_parser_omp_task (location_t loc, c_parser *parser)
13569 tree clauses, block;
13571 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
13572 "#pragma omp task");
13574 block = c_begin_omp_task ();
13575 c_parser_statement (parser);
13576 return c_finish_omp_task (loc, clauses, block);
13579 /* OpenMP 3.0:
13580 # pragma omp taskwait new-line
13583 static void
13584 c_parser_omp_taskwait (c_parser *parser)
13586 location_t loc = c_parser_peek_token (parser)->location;
13587 c_parser_consume_pragma (parser);
13588 c_parser_skip_to_pragma_eol (parser);
13590 c_finish_omp_taskwait (loc);
13593 /* OpenMP 3.1:
13594 # pragma omp taskyield new-line
13597 static void
13598 c_parser_omp_taskyield (c_parser *parser)
13600 location_t loc = c_parser_peek_token (parser)->location;
13601 c_parser_consume_pragma (parser);
13602 c_parser_skip_to_pragma_eol (parser);
13604 c_finish_omp_taskyield (loc);
13607 /* OpenMP 4.0:
13608 # pragma omp taskgroup new-line
13611 static tree
13612 c_parser_omp_taskgroup (c_parser *parser)
13614 location_t loc = c_parser_peek_token (parser)->location;
13615 c_parser_skip_to_pragma_eol (parser);
13616 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
13619 /* OpenMP 4.0:
13620 # pragma omp cancel cancel-clause[optseq] new-line
13622 LOC is the location of the #pragma.
13625 #define OMP_CANCEL_CLAUSE_MASK \
13626 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
13630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13632 static void
13633 c_parser_omp_cancel (c_parser *parser)
13635 location_t loc = c_parser_peek_token (parser)->location;
13637 c_parser_consume_pragma (parser);
13638 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
13639 "#pragma omp cancel");
13641 c_finish_omp_cancel (loc, clauses);
13644 /* OpenMP 4.0:
13645 # pragma omp cancellation point cancelpt-clause[optseq] new-line
13647 LOC is the location of the #pragma.
13650 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
13651 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
13656 static void
13657 c_parser_omp_cancellation_point (c_parser *parser)
13659 location_t loc = c_parser_peek_token (parser)->location;
13660 tree clauses;
13661 bool point_seen = false;
13663 c_parser_consume_pragma (parser);
13664 if (c_parser_next_token_is (parser, CPP_NAME))
13666 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13667 if (strcmp (p, "point") == 0)
13669 c_parser_consume_token (parser);
13670 point_seen = true;
13673 if (!point_seen)
13675 c_parser_error (parser, "expected %<point%>");
13676 c_parser_skip_to_pragma_eol (parser);
13677 return;
13680 clauses
13681 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
13682 "#pragma omp cancellation point");
13684 c_finish_omp_cancellation_point (loc, clauses);
13687 /* OpenMP 4.0:
13688 #pragma omp distribute distribute-clause[optseq] new-line
13689 for-loop */
13691 #define OMP_DISTRIBUTE_CLAUSE_MASK \
13692 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
13695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13697 static tree
13698 c_parser_omp_distribute (location_t loc, c_parser *parser,
13699 char *p_name, omp_clause_mask mask, tree *cclauses)
13701 tree clauses, block, ret;
13703 strcat (p_name, " distribute");
13704 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
13706 if (c_parser_next_token_is (parser, CPP_NAME))
13708 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13709 bool simd = false;
13710 bool parallel = false;
13712 if (strcmp (p, "simd") == 0)
13713 simd = true;
13714 else
13715 parallel = strcmp (p, "parallel") == 0;
13716 if (parallel || simd)
13718 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13719 if (cclauses == NULL)
13720 cclauses = cclauses_buf;
13721 c_parser_consume_token (parser);
13722 if (!flag_openmp) /* flag_openmp_simd */
13724 if (simd)
13725 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13726 else
13727 return c_parser_omp_parallel (loc, parser, p_name, mask,
13728 cclauses);
13730 block = c_begin_compound_stmt (true);
13731 if (simd)
13732 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13733 else
13734 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13735 block = c_end_compound_stmt (loc, block, true);
13736 if (ret == NULL)
13737 return ret;
13738 ret = make_node (OMP_DISTRIBUTE);
13739 TREE_TYPE (ret) = void_type_node;
13740 OMP_FOR_BODY (ret) = block;
13741 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13742 SET_EXPR_LOCATION (ret, loc);
13743 add_stmt (ret);
13744 return ret;
13747 if (!flag_openmp) /* flag_openmp_simd */
13749 c_parser_skip_to_pragma_eol (parser, false);
13750 return NULL_TREE;
13753 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13754 if (cclauses)
13756 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13757 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13760 block = c_begin_compound_stmt (true);
13761 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13762 block = c_end_compound_stmt (loc, block, true);
13763 add_stmt (block);
13765 return ret;
13768 /* OpenMP 4.0:
13769 # pragma omp teams teams-clause[optseq] new-line
13770 structured-block */
13772 #define OMP_TEAMS_CLAUSE_MASK \
13773 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13781 static tree
13782 c_parser_omp_teams (location_t loc, c_parser *parser,
13783 char *p_name, omp_clause_mask mask, tree *cclauses)
13785 tree clauses, block, ret;
13787 strcat (p_name, " teams");
13788 mask |= OMP_TEAMS_CLAUSE_MASK;
13790 if (c_parser_next_token_is (parser, CPP_NAME))
13792 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13793 if (strcmp (p, "distribute") == 0)
13795 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13796 if (cclauses == NULL)
13797 cclauses = cclauses_buf;
13799 c_parser_consume_token (parser);
13800 if (!flag_openmp) /* flag_openmp_simd */
13801 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13802 block = c_begin_compound_stmt (true);
13803 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13804 block = c_end_compound_stmt (loc, block, true);
13805 if (ret == NULL)
13806 return ret;
13807 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13808 ret = make_node (OMP_TEAMS);
13809 TREE_TYPE (ret) = void_type_node;
13810 OMP_TEAMS_CLAUSES (ret) = clauses;
13811 OMP_TEAMS_BODY (ret) = block;
13812 OMP_TEAMS_COMBINED (ret) = 1;
13813 return add_stmt (ret);
13816 if (!flag_openmp) /* flag_openmp_simd */
13818 c_parser_skip_to_pragma_eol (parser, false);
13819 return NULL_TREE;
13822 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13823 if (cclauses)
13825 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13826 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13829 tree stmt = make_node (OMP_TEAMS);
13830 TREE_TYPE (stmt) = void_type_node;
13831 OMP_TEAMS_CLAUSES (stmt) = clauses;
13832 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13834 return add_stmt (stmt);
13837 /* OpenMP 4.0:
13838 # pragma omp target data target-data-clause[optseq] new-line
13839 structured-block */
13841 #define OMP_TARGET_DATA_CLAUSE_MASK \
13842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13846 static tree
13847 c_parser_omp_target_data (location_t loc, c_parser *parser)
13849 tree stmt = make_node (OMP_TARGET_DATA);
13850 TREE_TYPE (stmt) = void_type_node;
13852 OMP_TARGET_DATA_CLAUSES (stmt)
13853 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13854 "#pragma omp target data");
13855 keep_next_level ();
13856 tree block = c_begin_compound_stmt (true);
13857 add_stmt (c_parser_omp_structured_block (parser));
13858 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13860 SET_EXPR_LOCATION (stmt, loc);
13861 return add_stmt (stmt);
13864 /* OpenMP 4.0:
13865 # pragma omp target update target-update-clause[optseq] new-line */
13867 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13868 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13873 static bool
13874 c_parser_omp_target_update (location_t loc, c_parser *parser,
13875 enum pragma_context context)
13877 if (context == pragma_stmt)
13879 error_at (loc,
13880 "%<#pragma omp target update%> may only be "
13881 "used in compound statements");
13882 c_parser_skip_to_pragma_eol (parser);
13883 return false;
13886 tree clauses
13887 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13888 "#pragma omp target update");
13889 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13890 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13892 error_at (loc,
13893 "%<#pragma omp target update%> must contain at least one "
13894 "%<from%> or %<to%> clauses");
13895 return false;
13898 tree stmt = make_node (OMP_TARGET_UPDATE);
13899 TREE_TYPE (stmt) = void_type_node;
13900 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13901 SET_EXPR_LOCATION (stmt, loc);
13902 add_stmt (stmt);
13903 return false;
13906 /* OpenMP 4.0:
13907 # pragma omp target target-clause[optseq] new-line
13908 structured-block */
13910 #define OMP_TARGET_CLAUSE_MASK \
13911 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13915 static bool
13916 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13918 location_t loc = c_parser_peek_token (parser)->location;
13919 c_parser_consume_pragma (parser);
13921 if (context != pragma_stmt && context != pragma_compound)
13923 c_parser_error (parser, "expected declaration specifiers");
13924 c_parser_skip_to_pragma_eol (parser);
13925 return false;
13928 if (c_parser_next_token_is (parser, CPP_NAME))
13930 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13932 if (strcmp (p, "teams") == 0)
13934 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13935 char p_name[sizeof ("#pragma omp target teams distribute "
13936 "parallel for simd")];
13938 c_parser_consume_token (parser);
13939 strcpy (p_name, "#pragma omp target");
13940 if (!flag_openmp) /* flag_openmp_simd */
13942 tree stmt = c_parser_omp_teams (loc, parser, p_name,
13943 OMP_TARGET_CLAUSE_MASK,
13944 cclauses);
13945 return stmt != NULL_TREE;
13947 keep_next_level ();
13948 tree block = c_begin_compound_stmt (true);
13949 tree ret = c_parser_omp_teams (loc, parser, p_name,
13950 OMP_TARGET_CLAUSE_MASK, cclauses);
13951 block = c_end_compound_stmt (loc, block, true);
13952 if (ret == NULL_TREE)
13953 return false;
13954 tree stmt = make_node (OMP_TARGET);
13955 TREE_TYPE (stmt) = void_type_node;
13956 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
13957 OMP_TARGET_BODY (stmt) = block;
13958 add_stmt (stmt);
13959 return true;
13961 else if (!flag_openmp) /* flag_openmp_simd */
13963 c_parser_skip_to_pragma_eol (parser, false);
13964 return false;
13966 else if (strcmp (p, "data") == 0)
13968 c_parser_consume_token (parser);
13969 c_parser_omp_target_data (loc, parser);
13970 return true;
13972 else if (strcmp (p, "update") == 0)
13974 c_parser_consume_token (parser);
13975 return c_parser_omp_target_update (loc, parser, context);
13979 tree stmt = make_node (OMP_TARGET);
13980 TREE_TYPE (stmt) = void_type_node;
13982 OMP_TARGET_CLAUSES (stmt)
13983 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
13984 "#pragma omp target");
13985 keep_next_level ();
13986 tree block = c_begin_compound_stmt (true);
13987 add_stmt (c_parser_omp_structured_block (parser));
13988 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13990 SET_EXPR_LOCATION (stmt, loc);
13991 add_stmt (stmt);
13992 return true;
13995 /* OpenMP 4.0:
13996 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
13998 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
13999 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
14000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
14002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
14003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
14004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
14006 static void
14007 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
14009 vec<c_token> clauses = vNULL;
14010 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14012 c_token *token = c_parser_peek_token (parser);
14013 if (token->type == CPP_EOF)
14015 c_parser_skip_to_pragma_eol (parser);
14016 clauses.release ();
14017 return;
14019 clauses.safe_push (*token);
14020 c_parser_consume_token (parser);
14022 clauses.safe_push (*c_parser_peek_token (parser));
14023 c_parser_skip_to_pragma_eol (parser);
14025 while (c_parser_next_token_is (parser, CPP_PRAGMA))
14027 if (c_parser_peek_token (parser)->pragma_kind
14028 != PRAGMA_OMP_DECLARE_REDUCTION
14029 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
14030 || strcmp (IDENTIFIER_POINTER
14031 (c_parser_peek_2nd_token (parser)->value),
14032 "simd") != 0)
14034 c_parser_error (parser,
14035 "%<#pragma omp declare simd%> must be followed by "
14036 "function declaration or definition or another "
14037 "%<#pragma omp declare simd%>");
14038 clauses.release ();
14039 return;
14041 c_parser_consume_pragma (parser);
14042 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14044 c_token *token = c_parser_peek_token (parser);
14045 if (token->type == CPP_EOF)
14047 c_parser_skip_to_pragma_eol (parser);
14048 clauses.release ();
14049 return;
14051 clauses.safe_push (*token);
14052 c_parser_consume_token (parser);
14054 clauses.safe_push (*c_parser_peek_token (parser));
14055 c_parser_skip_to_pragma_eol (parser);
14058 /* Make sure nothing tries to read past the end of the tokens. */
14059 c_token eof_token;
14060 memset (&eof_token, 0, sizeof (eof_token));
14061 eof_token.type = CPP_EOF;
14062 clauses.safe_push (eof_token);
14063 clauses.safe_push (eof_token);
14065 switch (context)
14067 case pragma_external:
14068 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14069 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14071 int ext = disable_extension_diagnostics ();
14073 c_parser_consume_token (parser);
14074 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14075 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14076 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14077 NULL, clauses);
14078 restore_extension_diagnostics (ext);
14080 else
14081 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14082 NULL, clauses);
14083 break;
14084 case pragma_struct:
14085 case pragma_param:
14086 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14087 "function declaration or definition");
14088 break;
14089 case pragma_compound:
14090 case pragma_stmt:
14091 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14092 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14094 int ext = disable_extension_diagnostics ();
14096 c_parser_consume_token (parser);
14097 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14098 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14099 if (c_parser_next_tokens_start_declaration (parser))
14101 c_parser_declaration_or_fndef (parser, true, true, true, true,
14102 true, NULL, clauses);
14103 restore_extension_diagnostics (ext);
14104 break;
14106 restore_extension_diagnostics (ext);
14108 else if (c_parser_next_tokens_start_declaration (parser))
14110 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14111 NULL, clauses);
14112 break;
14114 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
14115 "function declaration or definition");
14116 break;
14117 default:
14118 gcc_unreachable ();
14120 clauses.release ();
14123 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
14124 and put that into "omp declare simd" attribute. */
14126 static void
14127 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
14128 vec<c_token> clauses)
14130 if (flag_cilkplus
14131 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14133 error ("%<#pragma omp declare simd%> cannot be used in the same "
14134 "function marked as a Cilk Plus SIMD-enabled function");
14135 vec_free (parser->cilk_simd_fn_tokens);
14136 return;
14139 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
14140 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
14141 has already processed the tokens. */
14142 if (clauses.exists () && clauses[0].type == CPP_EOF)
14143 return;
14144 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14146 error ("%<#pragma omp declare simd%> not immediately followed by "
14147 "a function declaration or definition");
14148 clauses[0].type = CPP_EOF;
14149 return;
14151 if (clauses.exists () && clauses[0].type != CPP_NAME)
14153 error_at (DECL_SOURCE_LOCATION (fndecl),
14154 "%<#pragma omp declare simd%> not immediately followed by "
14155 "a single function declaration or definition");
14156 clauses[0].type = CPP_EOF;
14157 return;
14160 if (parms == NULL_TREE)
14161 parms = DECL_ARGUMENTS (fndecl);
14163 unsigned int tokens_avail = parser->tokens_avail;
14164 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14165 bool is_cilkplus_cilk_simd_fn = false;
14167 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14169 parser->tokens = parser->cilk_simd_fn_tokens->address ();
14170 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
14171 is_cilkplus_cilk_simd_fn = true;
14173 else
14175 parser->tokens = clauses.address ();
14176 parser->tokens_avail = clauses.length ();
14179 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
14180 while (parser->tokens_avail > 3)
14182 c_token *token = c_parser_peek_token (parser);
14183 if (!is_cilkplus_cilk_simd_fn)
14184 gcc_assert (token->type == CPP_NAME
14185 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
14186 else
14187 gcc_assert (token->type == CPP_NAME
14188 && is_cilkplus_vector_p (token->value));
14189 c_parser_consume_token (parser);
14190 parser->in_pragma = true;
14192 tree c = NULL_TREE;
14193 if (is_cilkplus_cilk_simd_fn)
14194 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
14195 "SIMD-enabled functions attribute");
14196 else
14197 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
14198 "#pragma omp declare simd");
14199 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
14200 if (c != NULL_TREE)
14201 c = tree_cons (NULL_TREE, c, NULL_TREE);
14202 if (is_cilkplus_cilk_simd_fn)
14204 tree k = build_tree_list (get_identifier ("cilk simd function"),
14205 NULL_TREE);
14206 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
14207 DECL_ATTRIBUTES (fndecl) = k;
14209 c = build_tree_list (get_identifier ("omp declare simd"), c);
14210 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
14211 DECL_ATTRIBUTES (fndecl) = c;
14214 parser->tokens = &parser->tokens_buf[0];
14215 parser->tokens_avail = tokens_avail;
14216 if (clauses.exists ())
14217 clauses[0].type = CPP_PRAGMA;
14219 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
14220 vec_free (parser->cilk_simd_fn_tokens);
14224 /* OpenMP 4.0:
14225 # pragma omp declare target new-line
14226 declarations and definitions
14227 # pragma omp end declare target new-line */
14229 static void
14230 c_parser_omp_declare_target (c_parser *parser)
14232 c_parser_skip_to_pragma_eol (parser);
14233 current_omp_declare_target_attribute++;
14236 static void
14237 c_parser_omp_end_declare_target (c_parser *parser)
14239 location_t loc = c_parser_peek_token (parser)->location;
14240 c_parser_consume_pragma (parser);
14241 if (c_parser_next_token_is (parser, CPP_NAME)
14242 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14243 "declare") == 0)
14245 c_parser_consume_token (parser);
14246 if (c_parser_next_token_is (parser, CPP_NAME)
14247 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
14248 "target") == 0)
14249 c_parser_consume_token (parser);
14250 else
14252 c_parser_error (parser, "expected %<target%>");
14253 c_parser_skip_to_pragma_eol (parser);
14254 return;
14257 else
14259 c_parser_error (parser, "expected %<declare%>");
14260 c_parser_skip_to_pragma_eol (parser);
14261 return;
14263 c_parser_skip_to_pragma_eol (parser);
14264 if (!current_omp_declare_target_attribute)
14265 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
14266 "%<#pragma omp declare target%>");
14267 else
14268 current_omp_declare_target_attribute--;
14272 /* OpenMP 4.0
14273 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14274 initializer-clause[opt] new-line
14276 initializer-clause:
14277 initializer (omp_priv = initializer)
14278 initializer (function-name (argument-list)) */
14280 static void
14281 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
14283 unsigned int tokens_avail = 0, i;
14284 vec<tree> types = vNULL;
14285 vec<c_token> clauses = vNULL;
14286 enum tree_code reduc_code = ERROR_MARK;
14287 tree reduc_id = NULL_TREE;
14288 tree type;
14289 location_t rloc = c_parser_peek_token (parser)->location;
14291 if (context == pragma_struct || context == pragma_param)
14293 error ("%<#pragma omp declare reduction%> not at file or block scope");
14294 goto fail;
14297 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14298 goto fail;
14300 switch (c_parser_peek_token (parser)->type)
14302 case CPP_PLUS:
14303 reduc_code = PLUS_EXPR;
14304 break;
14305 case CPP_MULT:
14306 reduc_code = MULT_EXPR;
14307 break;
14308 case CPP_MINUS:
14309 reduc_code = MINUS_EXPR;
14310 break;
14311 case CPP_AND:
14312 reduc_code = BIT_AND_EXPR;
14313 break;
14314 case CPP_XOR:
14315 reduc_code = BIT_XOR_EXPR;
14316 break;
14317 case CPP_OR:
14318 reduc_code = BIT_IOR_EXPR;
14319 break;
14320 case CPP_AND_AND:
14321 reduc_code = TRUTH_ANDIF_EXPR;
14322 break;
14323 case CPP_OR_OR:
14324 reduc_code = TRUTH_ORIF_EXPR;
14325 break;
14326 case CPP_NAME:
14327 const char *p;
14328 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14329 if (strcmp (p, "min") == 0)
14331 reduc_code = MIN_EXPR;
14332 break;
14334 if (strcmp (p, "max") == 0)
14336 reduc_code = MAX_EXPR;
14337 break;
14339 reduc_id = c_parser_peek_token (parser)->value;
14340 break;
14341 default:
14342 c_parser_error (parser,
14343 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14344 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
14345 goto fail;
14348 tree orig_reduc_id, reduc_decl;
14349 orig_reduc_id = reduc_id;
14350 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
14351 reduc_decl = c_omp_reduction_decl (reduc_id);
14352 c_parser_consume_token (parser);
14354 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14355 goto fail;
14357 while (true)
14359 location_t loc = c_parser_peek_token (parser)->location;
14360 struct c_type_name *ctype = c_parser_type_name (parser);
14361 if (ctype != NULL)
14363 type = groktypename (ctype, NULL, NULL);
14364 if (type == error_mark_node)
14366 else if ((INTEGRAL_TYPE_P (type)
14367 || TREE_CODE (type) == REAL_TYPE
14368 || TREE_CODE (type) == COMPLEX_TYPE)
14369 && orig_reduc_id == NULL_TREE)
14370 error_at (loc, "predeclared arithmetic type in "
14371 "%<#pragma omp declare reduction%>");
14372 else if (TREE_CODE (type) == FUNCTION_TYPE
14373 || TREE_CODE (type) == ARRAY_TYPE)
14374 error_at (loc, "function or array type in "
14375 "%<#pragma omp declare reduction%>");
14376 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
14377 error_at (loc, "const, volatile or restrict qualified type in "
14378 "%<#pragma omp declare reduction%>");
14379 else
14381 tree t;
14382 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
14383 if (comptypes (TREE_PURPOSE (t), type))
14385 error_at (loc, "redeclaration of %qs "
14386 "%<#pragma omp declare reduction%> for "
14387 "type %qT",
14388 IDENTIFIER_POINTER (reduc_id)
14389 + sizeof ("omp declare reduction ") - 1,
14390 type);
14391 location_t ploc
14392 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
14393 0));
14394 error_at (ploc, "previous %<#pragma omp declare "
14395 "reduction%>");
14396 break;
14398 if (t == NULL_TREE)
14399 types.safe_push (type);
14401 if (c_parser_next_token_is (parser, CPP_COMMA))
14402 c_parser_consume_token (parser);
14403 else
14404 break;
14406 else
14407 break;
14410 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
14411 || types.is_empty ())
14413 fail:
14414 clauses.release ();
14415 types.release ();
14416 while (true)
14418 c_token *token = c_parser_peek_token (parser);
14419 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
14420 break;
14421 c_parser_consume_token (parser);
14423 c_parser_skip_to_pragma_eol (parser);
14424 return;
14427 if (types.length () > 1)
14429 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14431 c_token *token = c_parser_peek_token (parser);
14432 if (token->type == CPP_EOF)
14433 goto fail;
14434 clauses.safe_push (*token);
14435 c_parser_consume_token (parser);
14437 clauses.safe_push (*c_parser_peek_token (parser));
14438 c_parser_skip_to_pragma_eol (parser);
14440 /* Make sure nothing tries to read past the end of the tokens. */
14441 c_token eof_token;
14442 memset (&eof_token, 0, sizeof (eof_token));
14443 eof_token.type = CPP_EOF;
14444 clauses.safe_push (eof_token);
14445 clauses.safe_push (eof_token);
14448 int errs = errorcount;
14449 FOR_EACH_VEC_ELT (types, i, type)
14451 tokens_avail = parser->tokens_avail;
14452 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14453 if (!clauses.is_empty ())
14455 parser->tokens = clauses.address ();
14456 parser->tokens_avail = clauses.length ();
14457 parser->in_pragma = true;
14460 bool nested = current_function_decl != NULL_TREE;
14461 if (nested)
14462 c_push_function_context ();
14463 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
14464 reduc_id, default_function_type);
14465 current_function_decl = fndecl;
14466 allocate_struct_function (fndecl, true);
14467 push_scope ();
14468 tree stmt = push_stmt_list ();
14469 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
14470 warn about these. */
14471 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
14472 get_identifier ("omp_out"), type);
14473 DECL_ARTIFICIAL (omp_out) = 1;
14474 DECL_CONTEXT (omp_out) = fndecl;
14475 pushdecl (omp_out);
14476 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
14477 get_identifier ("omp_in"), type);
14478 DECL_ARTIFICIAL (omp_in) = 1;
14479 DECL_CONTEXT (omp_in) = fndecl;
14480 pushdecl (omp_in);
14481 struct c_expr combiner = c_parser_expression (parser);
14482 struct c_expr initializer;
14483 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
14484 bool bad = false;
14485 initializer.value = error_mark_node;
14486 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14487 bad = true;
14488 else if (c_parser_next_token_is (parser, CPP_NAME)
14489 && strcmp (IDENTIFIER_POINTER
14490 (c_parser_peek_token (parser)->value),
14491 "initializer") == 0)
14493 c_parser_consume_token (parser);
14494 pop_scope ();
14495 push_scope ();
14496 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
14497 get_identifier ("omp_priv"), type);
14498 DECL_ARTIFICIAL (omp_priv) = 1;
14499 DECL_INITIAL (omp_priv) = error_mark_node;
14500 DECL_CONTEXT (omp_priv) = fndecl;
14501 pushdecl (omp_priv);
14502 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
14503 get_identifier ("omp_orig"), type);
14504 DECL_ARTIFICIAL (omp_orig) = 1;
14505 DECL_CONTEXT (omp_orig) = fndecl;
14506 pushdecl (omp_orig);
14507 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14508 bad = true;
14509 else if (!c_parser_next_token_is (parser, CPP_NAME))
14511 c_parser_error (parser, "expected %<omp_priv%> or "
14512 "function-name");
14513 bad = true;
14515 else if (strcmp (IDENTIFIER_POINTER
14516 (c_parser_peek_token (parser)->value),
14517 "omp_priv") != 0)
14519 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
14520 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14522 c_parser_error (parser, "expected function-name %<(%>");
14523 bad = true;
14525 else
14526 initializer = c_parser_postfix_expression (parser);
14527 if (initializer.value
14528 && TREE_CODE (initializer.value) == CALL_EXPR)
14530 int j;
14531 tree c = initializer.value;
14532 for (j = 0; j < call_expr_nargs (c); j++)
14533 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
14534 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
14535 break;
14536 if (j == call_expr_nargs (c))
14537 error ("one of the initializer call arguments should be "
14538 "%<&omp_priv%>");
14541 else
14543 c_parser_consume_token (parser);
14544 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14545 bad = true;
14546 else
14548 tree st = push_stmt_list ();
14549 start_init (omp_priv, NULL_TREE, 0);
14550 location_t loc = c_parser_peek_token (parser)->location;
14551 struct c_expr init = c_parser_initializer (parser);
14552 finish_init ();
14553 finish_decl (omp_priv, loc, init.value,
14554 init.original_type, NULL_TREE);
14555 pop_stmt_list (st);
14558 if (!bad
14559 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14560 bad = true;
14563 if (!bad)
14565 c_parser_skip_to_pragma_eol (parser);
14567 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
14568 DECL_INITIAL (reduc_decl));
14569 DECL_INITIAL (reduc_decl) = t;
14570 DECL_SOURCE_LOCATION (omp_out) = rloc;
14571 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
14572 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
14573 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
14574 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
14575 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
14576 if (omp_priv)
14578 DECL_SOURCE_LOCATION (omp_priv) = rloc;
14579 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
14580 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
14581 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
14582 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
14583 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14584 walk_tree (&DECL_INITIAL (omp_priv),
14585 c_check_omp_declare_reduction_r,
14586 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14590 pop_stmt_list (stmt);
14591 pop_scope ();
14592 if (cfun->language != NULL)
14594 ggc_free (cfun->language);
14595 cfun->language = NULL;
14597 set_cfun (NULL);
14598 current_function_decl = NULL_TREE;
14599 if (nested)
14600 c_pop_function_context ();
14602 if (!clauses.is_empty ())
14604 parser->tokens = &parser->tokens_buf[0];
14605 parser->tokens_avail = tokens_avail;
14607 if (bad)
14608 goto fail;
14609 if (errs != errorcount)
14610 break;
14613 clauses.release ();
14614 types.release ();
14618 /* OpenMP 4.0
14619 #pragma omp declare simd declare-simd-clauses[optseq] new-line
14620 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14621 initializer-clause[opt] new-line
14622 #pragma omp declare target new-line */
14624 static void
14625 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
14627 c_parser_consume_pragma (parser);
14628 if (c_parser_next_token_is (parser, CPP_NAME))
14630 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14631 if (strcmp (p, "simd") == 0)
14633 /* c_parser_consume_token (parser); done in
14634 c_parser_omp_declare_simd. */
14635 c_parser_omp_declare_simd (parser, context);
14636 return;
14638 if (strcmp (p, "reduction") == 0)
14640 c_parser_consume_token (parser);
14641 c_parser_omp_declare_reduction (parser, context);
14642 return;
14644 if (!flag_openmp) /* flag_openmp_simd */
14646 c_parser_skip_to_pragma_eol (parser, false);
14647 return;
14649 if (strcmp (p, "target") == 0)
14651 c_parser_consume_token (parser);
14652 c_parser_omp_declare_target (parser);
14653 return;
14657 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
14658 "or %<target%>");
14659 c_parser_skip_to_pragma_eol (parser);
14662 /* Main entry point to parsing most OpenMP pragmas. */
14664 static void
14665 c_parser_omp_construct (c_parser *parser)
14667 enum pragma_kind p_kind;
14668 location_t loc;
14669 tree stmt;
14670 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
14671 omp_clause_mask mask (0);
14673 loc = c_parser_peek_token (parser)->location;
14674 p_kind = c_parser_peek_token (parser)->pragma_kind;
14675 c_parser_consume_pragma (parser);
14677 switch (p_kind)
14679 case PRAGMA_OACC_CACHE:
14680 strcpy (p_name, "#pragma acc");
14681 stmt = c_parser_oacc_cache (loc, parser);
14682 break;
14683 case PRAGMA_OACC_DATA:
14684 stmt = c_parser_oacc_data (loc, parser);
14685 break;
14686 case PRAGMA_OACC_KERNELS:
14687 strcpy (p_name, "#pragma acc");
14688 stmt = c_parser_oacc_kernels (loc, parser, p_name);
14689 break;
14690 case PRAGMA_OACC_LOOP:
14691 strcpy (p_name, "#pragma acc");
14692 stmt = c_parser_oacc_loop (loc, parser, p_name);
14693 break;
14694 case PRAGMA_OACC_PARALLEL:
14695 strcpy (p_name, "#pragma acc");
14696 stmt = c_parser_oacc_parallel (loc, parser, p_name);
14697 break;
14698 case PRAGMA_OACC_WAIT:
14699 strcpy (p_name, "#pragma wait");
14700 stmt = c_parser_oacc_wait (loc, parser, p_name);
14701 break;
14702 case PRAGMA_OMP_ATOMIC:
14703 c_parser_omp_atomic (loc, parser);
14704 return;
14705 case PRAGMA_OMP_CRITICAL:
14706 stmt = c_parser_omp_critical (loc, parser);
14707 break;
14708 case PRAGMA_OMP_DISTRIBUTE:
14709 strcpy (p_name, "#pragma omp");
14710 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
14711 break;
14712 case PRAGMA_OMP_FOR:
14713 strcpy (p_name, "#pragma omp");
14714 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
14715 break;
14716 case PRAGMA_OMP_MASTER:
14717 stmt = c_parser_omp_master (loc, parser);
14718 break;
14719 case PRAGMA_OMP_ORDERED:
14720 stmt = c_parser_omp_ordered (loc, parser);
14721 break;
14722 case PRAGMA_OMP_PARALLEL:
14723 strcpy (p_name, "#pragma omp");
14724 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
14725 break;
14726 case PRAGMA_OMP_SECTIONS:
14727 strcpy (p_name, "#pragma omp");
14728 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
14729 break;
14730 case PRAGMA_OMP_SIMD:
14731 strcpy (p_name, "#pragma omp");
14732 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
14733 break;
14734 case PRAGMA_OMP_SINGLE:
14735 stmt = c_parser_omp_single (loc, parser);
14736 break;
14737 case PRAGMA_OMP_TASK:
14738 stmt = c_parser_omp_task (loc, parser);
14739 break;
14740 case PRAGMA_OMP_TASKGROUP:
14741 stmt = c_parser_omp_taskgroup (parser);
14742 break;
14743 case PRAGMA_OMP_TEAMS:
14744 strcpy (p_name, "#pragma omp");
14745 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14746 break;
14747 default:
14748 gcc_unreachable ();
14751 if (stmt)
14752 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14756 /* OpenMP 2.5:
14757 # pragma omp threadprivate (variable-list) */
14759 static void
14760 c_parser_omp_threadprivate (c_parser *parser)
14762 tree vars, t;
14763 location_t loc;
14765 c_parser_consume_pragma (parser);
14766 loc = c_parser_peek_token (parser)->location;
14767 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14769 /* Mark every variable in VARS to be assigned thread local storage. */
14770 for (t = vars; t; t = TREE_CHAIN (t))
14772 tree v = TREE_PURPOSE (t);
14774 /* FIXME diagnostics: Ideally we should keep individual
14775 locations for all the variables in the var list to make the
14776 following errors more precise. Perhaps
14777 c_parser_omp_var_list_parens() should construct a list of
14778 locations to go along with the var list. */
14780 /* If V had already been marked threadprivate, it doesn't matter
14781 whether it had been used prior to this point. */
14782 if (TREE_CODE (v) != VAR_DECL)
14783 error_at (loc, "%qD is not a variable", v);
14784 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14785 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14786 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
14787 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14788 else if (TREE_TYPE (v) == error_mark_node)
14790 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14791 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14792 else
14794 if (! DECL_THREAD_LOCAL_P (v))
14796 set_decl_tls_model (v, decl_default_tls_model (v));
14797 /* If rtl has been already set for this var, call
14798 make_decl_rtl once again, so that encode_section_info
14799 has a chance to look at the new decl flags. */
14800 if (DECL_RTL_SET_P (v))
14801 make_decl_rtl (v);
14803 C_DECL_THREADPRIVATE_P (v) = 1;
14807 c_parser_skip_to_pragma_eol (parser);
14810 /* Cilk Plus <#pragma simd> parsing routines. */
14812 /* Helper function for c_parser_pragma. Perform some sanity checking
14813 for <#pragma simd> constructs. Returns FALSE if there was a
14814 problem. */
14816 static bool
14817 c_parser_cilk_verify_simd (c_parser *parser,
14818 enum pragma_context context)
14820 if (!flag_cilkplus)
14822 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14823 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14824 return false;
14826 if (context == pragma_external)
14828 c_parser_error (parser,"pragma simd must be inside a function");
14829 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14830 return false;
14832 return true;
14835 /* Cilk Plus:
14836 This function is shared by SIMD-enabled functions and #pragma simd.
14837 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14838 CLAUSES is unused. The main purpose of this function is to parse a
14839 vectorlength attribute or clause and check for parse errors.
14840 When IS_SIMD_FN is true then the function is merely caching the tokens
14841 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14842 cache is cleared since there is no reason to continue.
14843 Syntax:
14844 vectorlength ( constant-expression ) */
14846 static tree
14847 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14848 bool is_simd_fn)
14850 if (is_simd_fn)
14851 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14852 else
14853 /* The vectorlength clause behaves exactly like OpenMP's safelen
14854 clause. Represent it in OpenMP terms. */
14855 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14857 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14858 return clauses;
14860 location_t loc = c_parser_peek_token (parser)->location;
14861 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14862 expr = c_fully_fold (expr, false, NULL);
14864 /* If expr is an error_mark_node then the above function would have
14865 emitted an error. No reason to do it twice. */
14866 if (expr == error_mark_node)
14868 else if (!TREE_TYPE (expr)
14869 || !TREE_CONSTANT (expr)
14870 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14872 error_at (loc, "vectorlength must be an integer constant");
14873 else if (wi::exact_log2 (expr) == -1)
14874 error_at (loc, "vectorlength must be a power of 2");
14875 else
14877 if (is_simd_fn)
14879 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14880 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14881 OMP_CLAUSE_CHAIN (u) = clauses;
14882 clauses = u;
14884 else
14886 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14887 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14888 OMP_CLAUSE_CHAIN (u) = clauses;
14889 clauses = u;
14893 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14895 return clauses;
14898 /* Cilk Plus:
14899 linear ( simd-linear-variable-list )
14901 simd-linear-variable-list:
14902 simd-linear-variable
14903 simd-linear-variable-list , simd-linear-variable
14905 simd-linear-variable:
14906 id-expression
14907 id-expression : simd-linear-step
14909 simd-linear-step:
14910 conditional-expression */
14912 static tree
14913 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14915 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14916 return clauses;
14918 location_t loc = c_parser_peek_token (parser)->location;
14920 if (c_parser_next_token_is_not (parser, CPP_NAME)
14921 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14922 c_parser_error (parser, "expected identifier");
14924 while (c_parser_next_token_is (parser, CPP_NAME)
14925 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14927 tree var = lookup_name (c_parser_peek_token (parser)->value);
14929 if (var == NULL)
14931 undeclared_variable (c_parser_peek_token (parser)->location,
14932 c_parser_peek_token (parser)->value);
14933 c_parser_consume_token (parser);
14935 else if (var == error_mark_node)
14936 c_parser_consume_token (parser);
14937 else
14939 tree step = integer_one_node;
14941 /* Parse the linear step if present. */
14942 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14944 c_parser_consume_token (parser);
14945 c_parser_consume_token (parser);
14947 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14948 expr = c_fully_fold (expr, false, NULL);
14950 if (TREE_TYPE (expr)
14951 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
14952 && (TREE_CONSTANT (expr)
14953 || DECL_P (expr)))
14954 step = expr;
14955 else
14956 c_parser_error (parser,
14957 "step size must be an integer constant "
14958 "expression or an integer variable");
14960 else
14961 c_parser_consume_token (parser);
14963 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
14964 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
14965 OMP_CLAUSE_DECL (u) = var;
14966 OMP_CLAUSE_LINEAR_STEP (u) = step;
14967 OMP_CLAUSE_CHAIN (u) = clauses;
14968 clauses = u;
14971 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14972 break;
14974 c_parser_consume_token (parser);
14977 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14979 return clauses;
14982 /* Returns the name of the next clause. If the clause is not
14983 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
14984 not consumed. Otherwise, the appropriate pragma_simd_clause is
14985 returned and the token is consumed. */
14987 static pragma_omp_clause
14988 c_parser_cilk_clause_name (c_parser *parser)
14990 pragma_omp_clause result;
14991 c_token *token = c_parser_peek_token (parser);
14993 if (!token->value || token->type != CPP_NAME)
14994 return PRAGMA_CILK_CLAUSE_NONE;
14996 const char *p = IDENTIFIER_POINTER (token->value);
14998 if (!strcmp (p, "vectorlength"))
14999 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
15000 else if (!strcmp (p, "linear"))
15001 result = PRAGMA_CILK_CLAUSE_LINEAR;
15002 else if (!strcmp (p, "private"))
15003 result = PRAGMA_CILK_CLAUSE_PRIVATE;
15004 else if (!strcmp (p, "firstprivate"))
15005 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
15006 else if (!strcmp (p, "lastprivate"))
15007 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
15008 else if (!strcmp (p, "reduction"))
15009 result = PRAGMA_CILK_CLAUSE_REDUCTION;
15010 else
15011 return PRAGMA_CILK_CLAUSE_NONE;
15013 c_parser_consume_token (parser);
15014 return result;
15017 /* Parse all #<pragma simd> clauses. Return the list of clauses
15018 found. */
15020 static tree
15021 c_parser_cilk_all_clauses (c_parser *parser)
15023 tree clauses = NULL;
15025 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15027 pragma_omp_clause c_kind;
15029 c_kind = c_parser_cilk_clause_name (parser);
15031 switch (c_kind)
15033 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
15034 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
15035 break;
15036 case PRAGMA_CILK_CLAUSE_LINEAR:
15037 clauses = c_parser_cilk_clause_linear (parser, clauses);
15038 break;
15039 case PRAGMA_CILK_CLAUSE_PRIVATE:
15040 /* Use the OpenMP counterpart. */
15041 clauses = c_parser_omp_clause_private (parser, clauses);
15042 break;
15043 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
15044 /* Use the OpenMP counterpart. */
15045 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
15046 break;
15047 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
15048 /* Use the OpenMP counterpart. */
15049 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
15050 break;
15051 case PRAGMA_CILK_CLAUSE_REDUCTION:
15052 /* Use the OpenMP counterpart. */
15053 clauses = c_parser_omp_clause_reduction (parser, clauses);
15054 break;
15055 default:
15056 c_parser_error (parser, "expected %<#pragma simd%> clause");
15057 goto saw_error;
15061 saw_error:
15062 c_parser_skip_to_pragma_eol (parser);
15063 return c_finish_cilk_clauses (clauses);
15066 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
15067 Here is the correct syntax of this pragma:
15068 #pragma cilk grainsize = <EXP>
15071 static void
15072 c_parser_cilk_grainsize (c_parser *parser)
15074 extern tree convert_to_integer (tree, tree);
15076 /* consume the 'grainsize' keyword. */
15077 c_parser_consume_pragma (parser);
15079 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
15081 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
15082 if (g_expr.value == error_mark_node)
15084 c_parser_skip_to_pragma_eol (parser);
15085 return;
15087 tree grain = convert_to_integer (long_integer_type_node,
15088 c_fully_fold (g_expr.value, false,
15089 NULL));
15090 c_parser_skip_to_pragma_eol (parser);
15091 c_token *token = c_parser_peek_token (parser);
15092 if (token && token->type == CPP_KEYWORD
15093 && token->keyword == RID_CILK_FOR)
15095 if (grain == NULL_TREE || grain == error_mark_node)
15096 grain = integer_zero_node;
15097 c_parser_cilk_for (parser, grain);
15099 else
15100 warning (0, "%<#pragma cilk grainsize%> is not followed by "
15101 "%<_Cilk_for%>");
15103 else
15104 c_parser_skip_to_pragma_eol (parser);
15107 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
15109 static void
15110 c_parser_cilk_simd (c_parser *parser)
15112 tree clauses = c_parser_cilk_all_clauses (parser);
15113 tree block = c_begin_compound_stmt (true);
15114 location_t loc = c_parser_peek_token (parser)->location;
15115 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
15116 block = c_end_compound_stmt (loc, block, true);
15117 add_stmt (block);
15120 /* Create an artificial decl with TYPE and emit initialization of it with
15121 INIT. */
15123 static tree
15124 c_get_temp_regvar (tree type, tree init)
15126 location_t loc = EXPR_LOCATION (init);
15127 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
15128 DECL_ARTIFICIAL (decl) = 1;
15129 DECL_IGNORED_P (decl) = 1;
15130 pushdecl (decl);
15131 tree t = build2 (INIT_EXPR, type, decl, init);
15132 add_stmt (t);
15133 return decl;
15136 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
15137 GRAIN is the grain value passed in through pragma or 0. */
15139 static void
15140 c_parser_cilk_for (c_parser *parser, tree grain)
15142 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
15143 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
15144 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
15145 clauses = c_finish_omp_clauses (clauses);
15147 tree block = c_begin_compound_stmt (true);
15148 tree sb = push_stmt_list ();
15149 location_t loc = c_parser_peek_token (parser)->location;
15150 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
15151 sb = pop_stmt_list (sb);
15153 if (omp_for)
15155 tree omp_par = make_node (OMP_PARALLEL);
15156 TREE_TYPE (omp_par) = void_type_node;
15157 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
15158 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
15159 TREE_SIDE_EFFECTS (bind) = 1;
15160 BIND_EXPR_BODY (bind) = sb;
15161 OMP_PARALLEL_BODY (omp_par) = bind;
15162 if (OMP_FOR_PRE_BODY (omp_for))
15164 add_stmt (OMP_FOR_PRE_BODY (omp_for));
15165 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
15167 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
15168 tree decl = TREE_OPERAND (init, 0);
15169 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
15170 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
15171 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
15172 if (TREE_CODE (t) != INTEGER_CST)
15174 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15175 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15176 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
15177 OMP_CLAUSE_CHAIN (c) = clauses;
15178 clauses = c;
15180 if (TREE_CODE (incr) == MODIFY_EXPR)
15182 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15183 if (TREE_CODE (t) != INTEGER_CST)
15185 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
15186 = c_get_temp_regvar (TREE_TYPE (t), t);
15187 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15188 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
15189 OMP_CLAUSE_CHAIN (c) = clauses;
15190 clauses = c;
15193 t = TREE_OPERAND (init, 1);
15194 if (TREE_CODE (t) != INTEGER_CST)
15196 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
15197 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
15198 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
15199 OMP_CLAUSE_CHAIN (c) = clauses;
15200 clauses = c;
15202 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15203 OMP_CLAUSE_DECL (c) = decl;
15204 OMP_CLAUSE_CHAIN (c) = clauses;
15205 clauses = c;
15206 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
15207 OMP_CLAUSE_OPERAND (c, 0)
15208 = cilk_for_number_of_iterations (omp_for);
15209 OMP_CLAUSE_CHAIN (c) = clauses;
15210 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
15211 add_stmt (omp_par);
15214 block = c_end_compound_stmt (loc, block, true);
15215 add_stmt (block);
15219 /* Parse a transaction attribute (GCC Extension).
15221 transaction-attribute:
15222 attributes
15223 [ [ any-word ] ]
15225 The transactional memory language description is written for C++,
15226 and uses the C++0x attribute syntax. For compatibility, allow the
15227 bracket style for transactions in C as well. */
15229 static tree
15230 c_parser_transaction_attributes (c_parser *parser)
15232 tree attr_name, attr = NULL;
15234 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
15235 return c_parser_attributes (parser);
15237 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
15238 return NULL_TREE;
15239 c_parser_consume_token (parser);
15240 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
15241 goto error1;
15243 attr_name = c_parser_attribute_any_word (parser);
15244 if (attr_name)
15246 c_parser_consume_token (parser);
15247 attr = build_tree_list (attr_name, NULL_TREE);
15249 else
15250 c_parser_error (parser, "expected identifier");
15252 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15253 error1:
15254 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15255 return attr;
15258 /* Parse a __transaction_atomic or __transaction_relaxed statement
15259 (GCC Extension).
15261 transaction-statement:
15262 __transaction_atomic transaction-attribute[opt] compound-statement
15263 __transaction_relaxed compound-statement
15265 Note that the only valid attribute is: "outer".
15268 static tree
15269 c_parser_transaction (c_parser *parser, enum rid keyword)
15271 unsigned int old_in = parser->in_transaction;
15272 unsigned int this_in = 1, new_in;
15273 location_t loc = c_parser_peek_token (parser)->location;
15274 tree stmt, attrs;
15276 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15277 || keyword == RID_TRANSACTION_RELAXED)
15278 && c_parser_next_token_is_keyword (parser, keyword));
15279 c_parser_consume_token (parser);
15281 if (keyword == RID_TRANSACTION_RELAXED)
15282 this_in |= TM_STMT_ATTR_RELAXED;
15283 else
15285 attrs = c_parser_transaction_attributes (parser);
15286 if (attrs)
15287 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
15290 /* Keep track if we're in the lexical scope of an outer transaction. */
15291 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
15293 parser->in_transaction = new_in;
15294 stmt = c_parser_compound_statement (parser);
15295 parser->in_transaction = old_in;
15297 if (flag_tm)
15298 stmt = c_finish_transaction (loc, stmt, this_in);
15299 else
15300 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15301 "%<__transaction_atomic%> without transactional memory support enabled"
15302 : "%<__transaction_relaxed %> "
15303 "without transactional memory support enabled"));
15305 return stmt;
15308 /* Parse a __transaction_atomic or __transaction_relaxed expression
15309 (GCC Extension).
15311 transaction-expression:
15312 __transaction_atomic ( expression )
15313 __transaction_relaxed ( expression )
15316 static struct c_expr
15317 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
15319 struct c_expr ret;
15320 unsigned int old_in = parser->in_transaction;
15321 unsigned int this_in = 1;
15322 location_t loc = c_parser_peek_token (parser)->location;
15323 tree attrs;
15325 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15326 || keyword == RID_TRANSACTION_RELAXED)
15327 && c_parser_next_token_is_keyword (parser, keyword));
15328 c_parser_consume_token (parser);
15330 if (keyword == RID_TRANSACTION_RELAXED)
15331 this_in |= TM_STMT_ATTR_RELAXED;
15332 else
15334 attrs = c_parser_transaction_attributes (parser);
15335 if (attrs)
15336 this_in |= parse_tm_stmt_attr (attrs, 0);
15339 parser->in_transaction = this_in;
15340 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15342 tree expr = c_parser_expression (parser).value;
15343 ret.original_type = TREE_TYPE (expr);
15344 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
15345 if (this_in & TM_STMT_ATTR_RELAXED)
15346 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
15347 SET_EXPR_LOCATION (ret.value, loc);
15348 ret.original_code = TRANSACTION_EXPR;
15349 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
15352 goto error;
15355 else
15357 error:
15358 ret.value = error_mark_node;
15359 ret.original_code = ERROR_MARK;
15360 ret.original_type = NULL;
15362 parser->in_transaction = old_in;
15364 if (!flag_tm)
15365 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15366 "%<__transaction_atomic%> without transactional memory support enabled"
15367 : "%<__transaction_relaxed %> "
15368 "without transactional memory support enabled"));
15370 return ret;
15373 /* Parse a __transaction_cancel statement (GCC Extension).
15375 transaction-cancel-statement:
15376 __transaction_cancel transaction-attribute[opt] ;
15378 Note that the only valid attribute is "outer".
15381 static tree
15382 c_parser_transaction_cancel (c_parser *parser)
15384 location_t loc = c_parser_peek_token (parser)->location;
15385 tree attrs;
15386 bool is_outer = false;
15388 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
15389 c_parser_consume_token (parser);
15391 attrs = c_parser_transaction_attributes (parser);
15392 if (attrs)
15393 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
15395 if (!flag_tm)
15397 error_at (loc, "%<__transaction_cancel%> without "
15398 "transactional memory support enabled");
15399 goto ret_error;
15401 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
15403 error_at (loc, "%<__transaction_cancel%> within a "
15404 "%<__transaction_relaxed%>");
15405 goto ret_error;
15407 else if (is_outer)
15409 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
15410 && !is_tm_may_cancel_outer (current_function_decl))
15412 error_at (loc, "outer %<__transaction_cancel%> not "
15413 "within outer %<__transaction_atomic%>");
15414 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
15415 goto ret_error;
15418 else if (parser->in_transaction == 0)
15420 error_at (loc, "%<__transaction_cancel%> not within "
15421 "%<__transaction_atomic%>");
15422 goto ret_error;
15425 return add_stmt (build_tm_abort_call (loc, is_outer));
15427 ret_error:
15428 return build1 (NOP_EXPR, void_type_node, error_mark_node);
15431 /* Parse a single source file. */
15433 void
15434 c_parse_file (void)
15436 /* Use local storage to begin. If the first token is a pragma, parse it.
15437 If it is #pragma GCC pch_preprocess, then this will load a PCH file
15438 which will cause garbage collection. */
15439 c_parser tparser;
15441 memset (&tparser, 0, sizeof tparser);
15442 tparser.tokens = &tparser.tokens_buf[0];
15443 the_parser = &tparser;
15445 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
15446 c_parser_pragma_pch_preprocess (&tparser);
15448 the_parser = ggc_alloc<c_parser> ();
15449 *the_parser = tparser;
15450 if (tparser.tokens == &tparser.tokens_buf[0])
15451 the_parser->tokens = &the_parser->tokens_buf[0];
15453 /* Initialize EH, if we've been told to do so. */
15454 if (flag_exceptions)
15455 using_eh_for_cleanups ();
15457 c_parser_translation_unit (the_parser);
15458 the_parser = NULL;
15461 /* This function parses Cilk Plus array notation. The starting index is
15462 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
15463 return value of this function is a tree_node called VALUE_TREE of type
15464 ARRAY_NOTATION_REF. */
15466 static tree
15467 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
15468 tree array_value)
15470 c_token *token = NULL;
15471 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
15472 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
15473 tree array_type_domain = NULL_TREE;
15475 if (array_value == error_mark_node || initial_index == error_mark_node)
15477 /* No need to continue. If either of these 2 were true, then an error
15478 must be emitted already. Thus, no need to emit them twice. */
15479 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15480 return error_mark_node;
15483 array_type = TREE_TYPE (array_value);
15484 gcc_assert (array_type);
15485 if (TREE_CODE (array_type) != ARRAY_TYPE
15486 && TREE_CODE (array_type) != POINTER_TYPE)
15488 error_at (loc, "base of array section must be pointer or array type");
15489 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15490 return error_mark_node;
15492 type = TREE_TYPE (array_type);
15493 token = c_parser_peek_token (parser);
15495 if (token->type == CPP_EOF)
15497 c_parser_error (parser, "expected %<:%> or numeral");
15498 return value_tree;
15500 else if (token->type == CPP_COLON)
15502 if (!initial_index)
15504 /* If we are here, then we have a case like this A[:]. */
15505 c_parser_consume_token (parser);
15506 if (TREE_CODE (array_type) == POINTER_TYPE)
15508 error_at (loc, "start-index and length fields necessary for "
15509 "using array notations in pointers");
15510 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15511 return error_mark_node;
15513 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15515 error_at (loc, "array notations cannot be used with function "
15516 "type");
15517 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15518 return error_mark_node;
15520 array_type_domain = TYPE_DOMAIN (array_type);
15522 if (!array_type_domain)
15524 error_at (loc, "start-index and length fields necessary for "
15525 "using array notations in dimensionless arrays");
15526 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15527 return error_mark_node;
15530 start_index = TYPE_MINVAL (array_type_domain);
15531 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
15532 start_index);
15533 if (!TYPE_MAXVAL (array_type_domain)
15534 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
15536 error_at (loc, "start-index and length fields necessary for "
15537 "using array notations in variable-length arrays");
15538 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15539 return error_mark_node;
15541 end_index = TYPE_MAXVAL (array_type_domain);
15542 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
15543 end_index, integer_one_node);
15544 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
15545 stride = build_int_cst (integer_type_node, 1);
15546 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
15548 else if (initial_index != error_mark_node)
15550 /* If we are here, then there should be 2 possibilities:
15551 1. Array [EXPR : EXPR]
15552 2. Array [EXPR : EXPR : EXPR]
15554 start_index = initial_index;
15556 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15558 error_at (loc, "array notations cannot be used with function "
15559 "type");
15560 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15561 return error_mark_node;
15563 c_parser_consume_token (parser); /* consume the ':' */
15564 struct c_expr ce = c_parser_expression (parser);
15565 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15566 end_index = ce.value;
15567 if (!end_index || end_index == error_mark_node)
15569 c_parser_skip_to_end_of_block_or_statement (parser);
15570 return error_mark_node;
15572 if (c_parser_peek_token (parser)->type == CPP_COLON)
15574 c_parser_consume_token (parser);
15575 ce = c_parser_expression (parser);
15576 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15577 stride = ce.value;
15578 if (!stride || stride == error_mark_node)
15580 c_parser_skip_to_end_of_block_or_statement (parser);
15581 return error_mark_node;
15585 else
15586 c_parser_error (parser, "expected array notation expression");
15588 else
15589 c_parser_error (parser, "expected array notation expression");
15591 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15593 value_tree = build_array_notation_ref (loc, array_value, start_index,
15594 end_index, stride, type);
15595 if (value_tree != error_mark_node)
15596 SET_EXPR_LOCATION (value_tree, loc);
15597 return value_tree;
15600 #include "gt-c-c-parser.h"